sbp.c revision 188756
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 188756 2009-02-18 18:41:34Z sbruno $ 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]; 221188756Ssbruno char bustgtlun[32]; 222103285Sikob}; 223103285Sikob 224103285Sikobstruct sbp_target { 225103285Sikob int target_id; 226103285Sikob int num_lun; 227121185Ssimokawa struct sbp_dev **luns; 228103285Sikob struct sbp_softc *sbp; 229103285Sikob struct fw_device *fwdev; 230129585Sdfr uint32_t mgm_hi, mgm_lo; 231111615Ssimokawa struct sbp_ocb *mgm_ocb_cur; 232111615Ssimokawa STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue; 233111615Ssimokawa struct callout mgm_ocb_timeout; 234111615Ssimokawa struct callout scan_callout; 235113584Ssimokawa STAILQ_HEAD(, fw_xfer) xferlist; 236113584Ssimokawa int n_xfer; 237103285Sikob}; 238103285Sikob 239103285Sikobstruct sbp_softc { 240103285Sikob struct firewire_dev_comm fd; 241103285Sikob struct cam_sim *sim; 242111615Ssimokawa struct cam_path *path; 243103285Sikob struct sbp_target targets[SBP_NUM_TARGETS]; 244103285Sikob struct fw_bind fwb; 245103285Sikob bus_dma_tag_t dmat; 246122387Ssimokawa struct timeval last_busreset; 247124145Ssimokawa#define SIMQ_FREEZED 1 248124145Ssimokawa int flags; 249170374Ssimokawa struct mtx mtx; 250103285Sikob}; 251170374Ssimokawa#define SBP_LOCK(sbp) mtx_lock(&(sbp)->mtx) 252170374Ssimokawa#define SBP_UNLOCK(sbp) mtx_unlock(&(sbp)->mtx) 253122387Ssimokawa 254124169Ssimokawastatic void sbp_post_explore (void *); 255124169Ssimokawastatic void sbp_recv (struct fw_xfer *); 256124169Ssimokawastatic void sbp_mgm_callback (struct fw_xfer *); 257121185Ssimokawa#if 0 258124169Ssimokawastatic void sbp_cmd_callback (struct fw_xfer *); 259121185Ssimokawa#endif 260124169Ssimokawastatic void sbp_orb_pointer (struct sbp_dev *, struct sbp_ocb *); 261127468Ssimokawastatic void sbp_doorbell(struct sbp_dev *); 262124169Ssimokawastatic void sbp_execute_ocb (void *, bus_dma_segment_t *, int, int); 263124169Ssimokawastatic void sbp_free_ocb (struct sbp_dev *, struct sbp_ocb *); 264124169Ssimokawastatic void sbp_abort_ocb (struct sbp_ocb *, int); 265124169Ssimokawastatic void sbp_abort_all_ocbs (struct sbp_dev *, int); 266170807Ssimokawastatic struct fw_xfer * sbp_write_cmd_locked (struct sbp_dev *, int, int); 267124169Ssimokawastatic struct fw_xfer * sbp_write_cmd (struct sbp_dev *, int, int); 268124169Ssimokawastatic struct sbp_ocb * sbp_get_ocb (struct sbp_dev *); 269124169Ssimokawastatic struct sbp_ocb * sbp_enqueue_ocb (struct sbp_dev *, struct sbp_ocb *); 270124169Ssimokawastatic struct sbp_ocb * sbp_dequeue_ocb (struct sbp_dev *, struct sbp_status *); 271121185Ssimokawastatic void sbp_cam_detach_sdev(struct sbp_dev *); 272121185Ssimokawastatic void sbp_free_sdev(struct sbp_dev *); 273124169Ssimokawastatic void sbp_cam_detach_target (struct sbp_target *); 274124169Ssimokawastatic void sbp_free_target (struct sbp_target *); 275124169Ssimokawastatic void sbp_mgm_timeout (void *arg); 276124169Ssimokawastatic void sbp_timeout (void *arg); 277124169Ssimokawastatic void sbp_mgm_orb (struct sbp_dev *, int, struct sbp_ocb *); 278103285Sikob 279108281SsimokawaMALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire"); 280103285Sikob 281103285Sikob/* cam related functions */ 282103285Sikobstatic void sbp_action(struct cam_sim *sim, union ccb *ccb); 283103285Sikobstatic void sbp_poll(struct cam_sim *sim); 284111615Ssimokawastatic void sbp_cam_scan_lun(struct cam_periph *, union ccb *); 285111615Ssimokawastatic void sbp_cam_scan_target(void *arg); 286103285Sikob 287103285Sikobstatic char *orb_status0[] = { 288103285Sikob /* 0 */ "No additional information to report", 289103285Sikob /* 1 */ "Request type not supported", 290103285Sikob /* 2 */ "Speed not supported", 291103285Sikob /* 3 */ "Page size not supported", 292103285Sikob /* 4 */ "Access denied", 293103285Sikob /* 5 */ "Logical unit not supported", 294103285Sikob /* 6 */ "Maximum payload too small", 295103285Sikob /* 7 */ "Reserved for future standardization", 296103285Sikob /* 8 */ "Resources unavailable", 297103285Sikob /* 9 */ "Function rejected", 298103285Sikob /* A */ "Login ID not recognized", 299103285Sikob /* B */ "Dummy ORB completed", 300103285Sikob /* C */ "Request aborted", 301103285Sikob /* FF */ "Unspecified error" 302103285Sikob#define MAX_ORB_STATUS0 0xd 303103285Sikob}; 304103285Sikob 305103285Sikobstatic char *orb_status1_object[] = { 306103285Sikob /* 0 */ "Operation request block (ORB)", 307103285Sikob /* 1 */ "Data buffer", 308103285Sikob /* 2 */ "Page table", 309103285Sikob /* 3 */ "Unable to specify" 310103285Sikob}; 311103285Sikob 312103285Sikobstatic char *orb_status1_serial_bus_error[] = { 313103285Sikob /* 0 */ "Missing acknowledge", 314103285Sikob /* 1 */ "Reserved; not to be used", 315103285Sikob /* 2 */ "Time-out error", 316103285Sikob /* 3 */ "Reserved; not to be used", 317103285Sikob /* 4 */ "Busy retry limit exceeded(X)", 318103285Sikob /* 5 */ "Busy retry limit exceeded(A)", 319103285Sikob /* 6 */ "Busy retry limit exceeded(B)", 320103285Sikob /* 7 */ "Reserved for future standardization", 321103285Sikob /* 8 */ "Reserved for future standardization", 322103285Sikob /* 9 */ "Reserved for future standardization", 323103285Sikob /* A */ "Reserved for future standardization", 324103285Sikob /* B */ "Tardy retry limit exceeded", 325103285Sikob /* C */ "Conflict error", 326103285Sikob /* D */ "Data error", 327103285Sikob /* E */ "Type error", 328103285Sikob /* F */ "Address error" 329103285Sikob}; 330103285Sikob 331103285Sikobstatic void 332103285Sikobsbp_identify(driver_t *driver, device_t parent) 333103285Sikob{ 334103285SikobSBP_DEBUG(0) 335103285Sikob printf("sbp_identify\n"); 336103285SikobEND_DEBUG 337103285Sikob 338188393Sfjoe BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent)); 339103285Sikob} 340103285Sikob 341103285Sikob/* 342103285Sikob * sbp_probe() 343103285Sikob */ 344103285Sikobstatic int 345103285Sikobsbp_probe(device_t dev) 346103285Sikob{ 347103285Sikob device_t pa; 348103285Sikob 349103285SikobSBP_DEBUG(0) 350103285Sikob printf("sbp_probe\n"); 351103285SikobEND_DEBUG 352103285Sikob 353103285Sikob pa = device_get_parent(dev); 354103285Sikob if(device_get_unit(dev) != device_get_unit(pa)){ 355103285Sikob return(ENXIO); 356103285Sikob } 357103285Sikob 358120660Ssimokawa device_set_desc(dev, "SBP-2/SCSI over FireWire"); 359111615Ssimokawa 360132432Ssimokawa#if 0 361111615Ssimokawa if (bootverbose) 362111615Ssimokawa debug = bootverbose; 363132432Ssimokawa#endif 364132432Ssimokawa 365103285Sikob return (0); 366103285Sikob} 367103285Sikob 368188756Ssbruno/* 369188756Ssbruno * Display device characteristics on the console 370188756Ssbruno */ 371103285Sikobstatic void 372188756Ssbrunosbp_show_sdev_info(struct sbp_dev *sdev) 373103285Sikob{ 374103285Sikob struct fw_device *fwdev; 375103285Sikob 376103285Sikob fwdev = sdev->target->fwdev; 377188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 378188756Ssbruno "%s: %s: ordered:%d type:%d EUI:%08x%08x node:%d " 379188756Ssbruno "speed:%d maxrec:%d\n", 380188756Ssbruno __func__, 381188756Ssbruno sdev->bustgtlun, 382108503Ssimokawa (sdev->type & 0x40) >> 6, 383108503Ssimokawa (sdev->type & 0x1f), 384103285Sikob fwdev->eui.hi, 385103285Sikob fwdev->eui.lo, 386103285Sikob fwdev->dst, 387103285Sikob fwdev->speed, 388188756Ssbruno fwdev->maxrec); 389188756Ssbruno 390188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 391188756Ssbruno "%s: %s '%s' '%s' '%s'\n", 392188756Ssbruno __func__, 393188756Ssbruno sdev->bustgtlun, 394188756Ssbruno sdev->vendor, 395188756Ssbruno sdev->product, 396188756Ssbruno sdev->revision); 397103285Sikob} 398103285Sikob 399110184Ssimokawastatic struct { 400110184Ssimokawa int bus; 401110184Ssimokawa int target; 402110184Ssimokawa struct fw_eui64 eui; 403110184Ssimokawa} wired[] = { 404110184Ssimokawa /* Bus Target EUI64 */ 405110184Ssimokawa#if 0 406110184Ssimokawa {0, 2, {0x00018ea0, 0x01fd0154}}, /* Logitec HDD */ 407110184Ssimokawa {0, 0, {0x00018ea6, 0x00100682}}, /* Logitec DVD */ 408110184Ssimokawa {0, 1, {0x00d03200, 0xa412006a}}, /* Yano HDD */ 409110184Ssimokawa#endif 410110184Ssimokawa {-1, -1, {0,0}} 411110184Ssimokawa}; 412110184Ssimokawa 413110184Ssimokawastatic int 414110184Ssimokawasbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev) 415110184Ssimokawa{ 416110184Ssimokawa int bus, i, target=-1; 417110184Ssimokawa char w[SBP_NUM_TARGETS]; 418110184Ssimokawa 419110184Ssimokawa bzero(w, sizeof(w)); 420110184Ssimokawa bus = device_get_unit(sbp->fd.dev); 421110184Ssimokawa 422110184Ssimokawa /* XXX wired-down configuration should be gotten from 423110184Ssimokawa tunable or device hint */ 424110184Ssimokawa for (i = 0; wired[i].bus >= 0; i ++) { 425110184Ssimokawa if (wired[i].bus == bus) { 426110184Ssimokawa w[wired[i].target] = 1; 427110184Ssimokawa if (wired[i].eui.hi == fwdev->eui.hi && 428110184Ssimokawa wired[i].eui.lo == fwdev->eui.lo) 429110184Ssimokawa target = wired[i].target; 430110184Ssimokawa } 431110184Ssimokawa } 432110184Ssimokawa if (target >= 0) { 433110184Ssimokawa if(target < SBP_NUM_TARGETS && 434110184Ssimokawa sbp->targets[target].fwdev == NULL) 435110184Ssimokawa return(target); 436110184Ssimokawa device_printf(sbp->fd.dev, 437110184Ssimokawa "target %d is not free for %08x:%08x\n", 438110184Ssimokawa target, fwdev->eui.hi, fwdev->eui.lo); 439110184Ssimokawa target = -1; 440110184Ssimokawa } 441110184Ssimokawa /* non-wired target */ 442110184Ssimokawa for (i = 0; i < SBP_NUM_TARGETS; i ++) 443110184Ssimokawa if (sbp->targets[i].fwdev == NULL && w[i] == 0) { 444110184Ssimokawa target = i; 445110184Ssimokawa break; 446110184Ssimokawa } 447110184Ssimokawa 448110184Ssimokawa return target; 449110184Ssimokawa} 450110184Ssimokawa 451121185Ssimokawastatic void 452121185Ssimokawasbp_alloc_lun(struct sbp_target *target) 453103285Sikob{ 454108503Ssimokawa struct crom_context cc; 455108503Ssimokawa struct csrreg *reg; 456121185Ssimokawa struct sbp_dev *sdev, **newluns; 457121185Ssimokawa struct sbp_softc *sbp; 458121185Ssimokawa int maxlun, lun, i; 459103285Sikob 460121185Ssimokawa sbp = target->sbp; 461114069Ssimokawa crom_init_context(&cc, target->fwdev->csrrom); 462108503Ssimokawa /* XXX shoud parse appropriate unit directories only */ 463108503Ssimokawa maxlun = -1; 464108503Ssimokawa while (cc.depth >= 0) { 465108503Ssimokawa reg = crom_search_key(&cc, CROM_LUN); 466108503Ssimokawa if (reg == NULL) 467108503Ssimokawa break; 468110839Ssimokawa lun = reg->val & 0xffff; 469108642SsimokawaSBP_DEBUG(0) 470108642Ssimokawa printf("target %d lun %d found\n", target->target_id, lun); 471108642SsimokawaEND_DEBUG 472108503Ssimokawa if (maxlun < lun) 473108503Ssimokawa maxlun = lun; 474108503Ssimokawa crom_next(&cc); 475108503Ssimokawa } 476110839Ssimokawa if (maxlun < 0) 477121185Ssimokawa printf("%s:%d no LUN found\n", 478121185Ssimokawa device_get_nameunit(target->sbp->fd.dev), 479121185Ssimokawa target->target_id); 480121185Ssimokawa 481121185Ssimokawa maxlun ++; 482110839Ssimokawa if (maxlun >= SBP_NUM_LUNS) 483110839Ssimokawa maxlun = SBP_NUM_LUNS; 484121185Ssimokawa 485121185Ssimokawa /* Invalidiate stale devices */ 486121185Ssimokawa for (lun = 0; lun < target->num_lun; lun ++) { 487121185Ssimokawa sdev = target->luns[lun]; 488121185Ssimokawa if (sdev == NULL) 489121185Ssimokawa continue; 490121185Ssimokawa sdev->flags &= ~VALID_LUN; 491121185Ssimokawa if (lun >= maxlun) { 492121185Ssimokawa /* lost device */ 493121185Ssimokawa sbp_cam_detach_sdev(sdev); 494121185Ssimokawa sbp_free_sdev(sdev); 495187993Ssbruno target->luns[lun] = NULL; 496121185Ssimokawa } 497103285Sikob } 498121185Ssimokawa 499121185Ssimokawa /* Reallocate */ 500121185Ssimokawa if (maxlun != target->num_lun) { 501121185Ssimokawa newluns = (struct sbp_dev **) realloc(target->luns, 502121185Ssimokawa sizeof(struct sbp_dev *) * maxlun, 503121185Ssimokawa M_SBP, M_NOWAIT | M_ZERO); 504121185Ssimokawa 505121185Ssimokawa if (newluns == NULL) { 506127468Ssimokawa printf("%s: realloc failed\n", __func__); 507121185Ssimokawa newluns = target->luns; 508121185Ssimokawa maxlun = target->num_lun; 509121185Ssimokawa } 510121185Ssimokawa 511121185Ssimokawa /* 512121185Ssimokawa * We must zero the extended region for the case 513121185Ssimokawa * realloc() doesn't allocate new buffer. 514121185Ssimokawa */ 515121185Ssimokawa if (maxlun > target->num_lun) 516121185Ssimokawa bzero(&newluns[target->num_lun], 517121185Ssimokawa sizeof(struct sbp_dev *) * 518121185Ssimokawa (maxlun - target->num_lun)); 519121185Ssimokawa 520121185Ssimokawa target->luns = newluns; 521121185Ssimokawa target->num_lun = maxlun; 522121185Ssimokawa } 523121185Ssimokawa 524108503Ssimokawa crom_init_context(&cc, target->fwdev->csrrom); 525108503Ssimokawa while (cc.depth >= 0) { 526121185Ssimokawa int new = 0; 527121185Ssimokawa 528108503Ssimokawa reg = crom_search_key(&cc, CROM_LUN); 529108503Ssimokawa if (reg == NULL) 530108503Ssimokawa break; 531110839Ssimokawa lun = reg->val & 0xffff; 532110839Ssimokawa if (lun >= SBP_NUM_LUNS) { 533110839Ssimokawa printf("too large lun %d\n", lun); 534121185Ssimokawa goto next; 535110839Ssimokawa } 536121185Ssimokawa 537121185Ssimokawa sdev = target->luns[lun]; 538121185Ssimokawa if (sdev == NULL) { 539121185Ssimokawa sdev = malloc(sizeof(struct sbp_dev), 540121185Ssimokawa M_SBP, M_NOWAIT | M_ZERO); 541121185Ssimokawa if (sdev == NULL) { 542127468Ssimokawa printf("%s: malloc failed\n", __func__); 543121185Ssimokawa goto next; 544121185Ssimokawa } 545121185Ssimokawa target->luns[lun] = sdev; 546121185Ssimokawa sdev->lun_id = lun; 547121185Ssimokawa sdev->target = target; 548121185Ssimokawa STAILQ_INIT(&sdev->ocbs); 549121185Ssimokawa CALLOUT_INIT(&sdev->login_callout); 550121185Ssimokawa sdev->status = SBP_DEV_RESET; 551121185Ssimokawa new = 1; 552188756Ssbruno snprintf(sdev->bustgtlun, 32, "%s:%d:%d", 553188756Ssbruno device_get_nameunit(sdev->target->sbp->fd.dev), 554188756Ssbruno sdev->target->target_id, 555188756Ssbruno sdev->lun_id); 556121185Ssimokawa } 557121185Ssimokawa sdev->flags |= VALID_LUN; 558119196Ssimokawa sdev->type = (reg->val & 0xff0000) >> 16; 559113584Ssimokawa 560121185Ssimokawa if (new == 0) 561121185Ssimokawa goto next; 562121185Ssimokawa 563113584Ssimokawa fwdma_malloc(sbp->fd.fc, 564129585Sdfr /* alignment */ sizeof(uint32_t), 565113584Ssimokawa SBP_DMA_SIZE, &sdev->dma, BUS_DMA_NOWAIT); 566113584Ssimokawa if (sdev->dma.v_addr == NULL) { 567113584Ssimokawa printf("%s: dma space allocation failed\n", 568127468Ssimokawa __func__); 569121185Ssimokawa free(sdev, M_SBP); 570121185Ssimokawa target->luns[lun] = NULL; 571121185Ssimokawa goto next; 572113584Ssimokawa } 573113584Ssimokawa sdev->login = (struct sbp_login_res *) sdev->dma.v_addr; 574113584Ssimokawa sdev->ocb = (struct sbp_ocb *) 575113584Ssimokawa ((char *)sdev->dma.v_addr + SBP_LOGIN_SIZE); 576113584Ssimokawa bzero((char *)sdev->ocb, 577113584Ssimokawa sizeof (struct sbp_ocb) * SBP_QUEUE_LEN); 578113584Ssimokawa 579113584Ssimokawa STAILQ_INIT(&sdev->free_ocbs); 580113584Ssimokawa for (i = 0; i < SBP_QUEUE_LEN; i++) { 581113584Ssimokawa struct sbp_ocb *ocb; 582113584Ssimokawa ocb = &sdev->ocb[i]; 583113584Ssimokawa ocb->bus_addr = sdev->dma.bus_addr 584113584Ssimokawa + SBP_LOGIN_SIZE 585113584Ssimokawa + sizeof(struct sbp_ocb) * i 586113584Ssimokawa + offsetof(struct sbp_ocb, orb[0]); 587113584Ssimokawa if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) { 588113584Ssimokawa printf("sbp_attach: cannot create dmamap\n"); 589121185Ssimokawa /* XXX */ 590121185Ssimokawa goto next; 591113584Ssimokawa } 592113584Ssimokawa sbp_free_ocb(sdev, ocb); 593113584Ssimokawa } 594121185Ssimokawanext: 595108503Ssimokawa crom_next(&cc); 596103285Sikob } 597121185Ssimokawa 598121185Ssimokawa for (lun = 0; lun < target->num_lun; lun ++) { 599121185Ssimokawa sdev = target->luns[lun]; 600121185Ssimokawa if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) { 601121185Ssimokawa sbp_cam_detach_sdev(sdev); 602121185Ssimokawa sbp_free_sdev(sdev); 603121185Ssimokawa target->luns[lun] = NULL; 604121185Ssimokawa } 605121185Ssimokawa } 606121185Ssimokawa} 607121185Ssimokawa 608121185Ssimokawastatic struct sbp_target * 609121185Ssimokawasbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev) 610121185Ssimokawa{ 611121185Ssimokawa int i; 612121185Ssimokawa struct sbp_target *target; 613121185Ssimokawa struct crom_context cc; 614121185Ssimokawa struct csrreg *reg; 615121185Ssimokawa 616121185SsimokawaSBP_DEBUG(1) 617121185Ssimokawa printf("sbp_alloc_target\n"); 618121185SsimokawaEND_DEBUG 619121185Ssimokawa i = sbp_new_target(sbp, fwdev); 620121185Ssimokawa if (i < 0) { 621121185Ssimokawa device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n"); 622121185Ssimokawa return NULL; 623121185Ssimokawa } 624121185Ssimokawa /* new target */ 625121185Ssimokawa target = &sbp->targets[i]; 626121185Ssimokawa target->sbp = sbp; 627121185Ssimokawa target->fwdev = fwdev; 628121185Ssimokawa target->target_id = i; 629121185Ssimokawa /* XXX we may want to reload mgm port after each bus reset */ 630121185Ssimokawa /* XXX there might be multiple management agents */ 631121185Ssimokawa crom_init_context(&cc, target->fwdev->csrrom); 632121185Ssimokawa reg = crom_search_key(&cc, CROM_MGM); 633121185Ssimokawa if (reg == NULL || reg->val == 0) { 634121185Ssimokawa printf("NULL management address\n"); 635121185Ssimokawa target->fwdev = NULL; 636121185Ssimokawa return NULL; 637121185Ssimokawa } 638121185Ssimokawa target->mgm_hi = 0xffff; 639121185Ssimokawa target->mgm_lo = 0xf0000000 | (reg->val << 2); 640121185Ssimokawa target->mgm_ocb_cur = NULL; 641121185SsimokawaSBP_DEBUG(1) 642121185Ssimokawa printf("target:%d mgm_port: %x\n", i, target->mgm_lo); 643121185SsimokawaEND_DEBUG 644121185Ssimokawa STAILQ_INIT(&target->xferlist); 645121185Ssimokawa target->n_xfer = 0; 646121185Ssimokawa STAILQ_INIT(&target->mgm_ocb_queue); 647121185Ssimokawa CALLOUT_INIT(&target->mgm_ocb_timeout); 648121185Ssimokawa CALLOUT_INIT(&target->scan_callout); 649121185Ssimokawa 650121185Ssimokawa target->luns = NULL; 651121185Ssimokawa target->num_lun = 0; 652114069Ssimokawa return target; 653103285Sikob} 654103285Sikob 655103285Sikobstatic void 656103285Sikobsbp_probe_lun(struct sbp_dev *sdev) 657103285Sikob{ 658103285Sikob struct fw_device *fwdev; 659114069Ssimokawa struct crom_context c, *cc = &c; 660114069Ssimokawa struct csrreg *reg; 661103285Sikob 662103285Sikob bzero(sdev->vendor, sizeof(sdev->vendor)); 663103285Sikob bzero(sdev->product, sizeof(sdev->product)); 664114069Ssimokawa 665114069Ssimokawa fwdev = sdev->target->fwdev; 666114069Ssimokawa crom_init_context(cc, fwdev->csrrom); 667114069Ssimokawa /* get vendor string */ 668114069Ssimokawa crom_search_key(cc, CSRKEY_VENDOR); 669114069Ssimokawa crom_next(cc); 670114069Ssimokawa crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor)); 671114223Ssimokawa /* skip to the unit directory for SBP-2 */ 672114260Ssimokawa while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) { 673114223Ssimokawa if (reg->val == CSRVAL_T10SBP2) 674114223Ssimokawa break; 675114260Ssimokawa crom_next(cc); 676114260Ssimokawa } 677114069Ssimokawa /* get firmware revision */ 678114069Ssimokawa reg = crom_search_key(cc, CSRKEY_FIRM_VER); 679114069Ssimokawa if (reg != NULL) 680114069Ssimokawa snprintf(sdev->revision, sizeof(sdev->revision), 681114069Ssimokawa "%06x", reg->val); 682114069Ssimokawa /* get product string */ 683114069Ssimokawa crom_search_key(cc, CSRKEY_MODEL); 684114069Ssimokawa crom_next(cc); 685114069Ssimokawa crom_parse_text(cc, sdev->product, sizeof(sdev->product)); 686103285Sikob} 687111615Ssimokawa 688103285Sikobstatic void 689111615Ssimokawasbp_login_callout(void *arg) 690103285Sikob{ 691111615Ssimokawa struct sbp_dev *sdev = (struct sbp_dev *)arg; 692111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL); 693111615Ssimokawa} 694111615Ssimokawa 695122387Ssimokawastatic void 696122387Ssimokawasbp_login(struct sbp_dev *sdev) 697122387Ssimokawa{ 698122387Ssimokawa struct timeval delta; 699122387Ssimokawa struct timeval t; 700122387Ssimokawa int ticks = 0; 701122387Ssimokawa 702122387Ssimokawa microtime(&delta); 703122387Ssimokawa timevalsub(&delta, &sdev->target->sbp->last_busreset); 704122387Ssimokawa t.tv_sec = login_delay / 1000; 705122387Ssimokawa t.tv_usec = (login_delay % 1000) * 1000; 706122387Ssimokawa timevalsub(&t, &delta); 707122387Ssimokawa if (t.tv_sec >= 0 && t.tv_usec > 0) 708122387Ssimokawa ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000; 709122387SsimokawaSBP_DEBUG(0) 710153706Strhodes printf("%s: sec = %jd usec = %ld ticks = %d\n", __func__, 711153706Strhodes (intmax_t)t.tv_sec, t.tv_usec, ticks); 712122387SsimokawaEND_DEBUG 713122387Ssimokawa callout_reset(&sdev->login_callout, ticks, 714122387Ssimokawa sbp_login_callout, (void *)(sdev)); 715122387Ssimokawa} 716122387Ssimokawa 717114069Ssimokawa#define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \ 718114069Ssimokawa && crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2)) 719111615Ssimokawa 720111615Ssimokawastatic void 721111615Ssimokawasbp_probe_target(void *arg) 722111615Ssimokawa{ 723111615Ssimokawa struct sbp_target *target = (struct sbp_target *)arg; 724188756Ssbruno struct sbp_softc *sbp = target->sbp; 725103285Sikob struct sbp_dev *sdev; 726111615Ssimokawa int i, alive; 727103285Sikob 728111615Ssimokawa alive = SBP_FWDEV_ALIVE(target->fwdev); 729103285SikobSBP_DEBUG(1) 730188756Ssbruno device_printf(sbp->fd.dev, "%s %d%salive\n", 731188756Ssbruno __func__, target->target_id, 732188756Ssbruno (!alive) ? " not " : ""); 733103285SikobEND_DEBUG 734103285Sikob 735103285Sikob sbp = target->sbp; 736121185Ssimokawa sbp_alloc_lun(target); 737121185Ssimokawa 738111615Ssimokawa /* XXX untimeout mgm_ocb and dequeue */ 739103285Sikob for (i=0; i < target->num_lun; i++) { 740121185Ssimokawa sdev = target->luns[i]; 741121185Ssimokawa if (sdev == NULL) 742121185Ssimokawa continue; 743103285Sikob if (alive && (sdev->status != SBP_DEV_DEAD)) { 744103285Sikob if (sdev->path != NULL) { 745170374Ssimokawa SBP_LOCK(sbp); 746103285Sikob xpt_freeze_devq(sdev->path, 1); 747111615Ssimokawa sdev->freeze ++; 748170374Ssimokawa SBP_UNLOCK(sbp); 749103285Sikob } 750111615Ssimokawa sbp_probe_lun(sdev); 751188756Ssbruno sbp_show_sdev_info(sdev); 752111615Ssimokawa 753111615Ssimokawa sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET); 754103285Sikob switch (sdev->status) { 755110336Ssimokawa case SBP_DEV_RESET: 756103285Sikob /* new or revived target */ 757113584Ssimokawa if (auto_login) 758111615Ssimokawa sbp_login(sdev); 759103285Sikob break; 760111615Ssimokawa case SBP_DEV_TOATTACH: 761111615Ssimokawa case SBP_DEV_PROBE: 762111615Ssimokawa case SBP_DEV_ATTACHED: 763110336Ssimokawa case SBP_DEV_RETRY: 764110336Ssimokawa default: 765111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL); 766110336Ssimokawa break; 767103285Sikob } 768103285Sikob } else { 769103285Sikob switch (sdev->status) { 770103285Sikob case SBP_DEV_ATTACHED: 771103285SikobSBP_DEBUG(0) 772103285Sikob /* the device has gone */ 773188756Ssbruno device_printf(sbp->fd.dev, "%s: lost target\n", 774188756Ssbruno __func__); 775103285SikobEND_DEBUG 776111615Ssimokawa if (sdev->path) { 777170374Ssimokawa SBP_LOCK(sbp); 778103285Sikob xpt_freeze_devq(sdev->path, 1); 779111615Ssimokawa sdev->freeze ++; 780170374Ssimokawa SBP_UNLOCK(sbp); 781111615Ssimokawa } 782103285Sikob sdev->status = SBP_DEV_RETRY; 783187993Ssbruno sbp_cam_detach_sdev(sdev); 784187993Ssbruno sbp_free_sdev(sdev); 785187993Ssbruno target->luns[i] = NULL; 786103285Sikob break; 787103285Sikob case SBP_DEV_PROBE: 788103285Sikob case SBP_DEV_TOATTACH: 789103285Sikob sdev->status = SBP_DEV_RESET; 790103285Sikob break; 791103285Sikob case SBP_DEV_RETRY: 792103285Sikob case SBP_DEV_RESET: 793103285Sikob case SBP_DEV_DEAD: 794103285Sikob break; 795103285Sikob } 796103285Sikob } 797103285Sikob } 798103285Sikob} 799103285Sikob 800103285Sikobstatic void 801113584Ssimokawasbp_post_busreset(void *arg) 802113584Ssimokawa{ 803113584Ssimokawa struct sbp_softc *sbp; 804113584Ssimokawa 805113584Ssimokawa sbp = (struct sbp_softc *)arg; 806113584SsimokawaSBP_DEBUG(0) 807113584Ssimokawa printf("sbp_post_busreset\n"); 808113584SsimokawaEND_DEBUG 809124145Ssimokawa if ((sbp->sim->flags & SIMQ_FREEZED) == 0) { 810170374Ssimokawa SBP_LOCK(sbp); 811124145Ssimokawa xpt_freeze_simq(sbp->sim, /*count*/1); 812124145Ssimokawa sbp->sim->flags |= SIMQ_FREEZED; 813170374Ssimokawa SBP_UNLOCK(sbp); 814124145Ssimokawa } 815122387Ssimokawa microtime(&sbp->last_busreset); 816113584Ssimokawa} 817113584Ssimokawa 818113584Ssimokawastatic void 819103285Sikobsbp_post_explore(void *arg) 820103285Sikob{ 821103285Sikob struct sbp_softc *sbp = (struct sbp_softc *)arg; 822103285Sikob struct sbp_target *target; 823103285Sikob struct fw_device *fwdev; 824103285Sikob int i, alive; 825103285Sikob 826111199SsimokawaSBP_DEBUG(0) 827111199Ssimokawa printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold); 828103285SikobEND_DEBUG 829170400Ssimokawa /* We need physical access */ 830170400Ssimokawa if (!firewire_phydma_enable) 831170400Ssimokawa return; 832170400Ssimokawa 833122387Ssimokawa if (sbp_cold > 0) 834122387Ssimokawa sbp_cold --; 835122387Ssimokawa 836122387Ssimokawa#if 0 837122387Ssimokawa /* 838122387Ssimokawa * XXX don't let CAM the bus rest. 839122387Ssimokawa * CAM tries to do something with freezed (DEV_RETRY) devices. 840111615Ssimokawa */ 841111615Ssimokawa xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL); 842103285Sikob#endif 843111615Ssimokawa 844130532Sdfr /* Garbage Collection */ 845103285Sikob for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){ 846103285Sikob target = &sbp->targets[i]; 847110193Ssimokawa STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) 848110193Ssimokawa if (target->fwdev == NULL || target->fwdev == fwdev) 849110193Ssimokawa break; 850121185Ssimokawa if (fwdev == NULL) { 851103285Sikob /* device has removed in lower driver */ 852110145Ssimokawa sbp_cam_detach_target(target); 853121185Ssimokawa sbp_free_target(target); 854103285Sikob } 855103285Sikob } 856103285Sikob /* traverse device list */ 857110193Ssimokawa STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) { 858103285SikobSBP_DEBUG(0) 859188756Ssbruno device_printf(sbp->fd.dev,"%s:: EUI:%08x%08x %s attached, state=%d\n", 860188756Ssbruno __func__, fwdev->eui.hi, fwdev->eui.lo, 861188756Ssbruno (fwdev->status != FWDEVATTACHED) ? "not" : "", 862188756Ssbruno fwdev->status); 863103285SikobEND_DEBUG 864111615Ssimokawa alive = SBP_FWDEV_ALIVE(fwdev); 865103285Sikob for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){ 866103285Sikob target = &sbp->targets[i]; 867103285Sikob if(target->fwdev == fwdev ) { 868103285Sikob /* known target */ 869103285Sikob break; 870103285Sikob } 871103285Sikob } 872103285Sikob if(i == SBP_NUM_TARGETS){ 873103285Sikob if (alive) { 874103285Sikob /* new target */ 875103285Sikob target = sbp_alloc_target(sbp, fwdev); 876103285Sikob if (target == NULL) 877103285Sikob continue; 878103285Sikob } else { 879103285Sikob continue; 880103285Sikob } 881103285Sikob } 882111615Ssimokawa sbp_probe_target((void *)target); 883121185Ssimokawa if (target->num_lun == 0) 884121185Ssimokawa sbp_free_target(target); 885103285Sikob } 886170374Ssimokawa SBP_LOCK(sbp); 887124145Ssimokawa xpt_release_simq(sbp->sim, /*run queue*/TRUE); 888124145Ssimokawa sbp->sim->flags &= ~SIMQ_FREEZED; 889170374Ssimokawa SBP_UNLOCK(sbp); 890103285Sikob} 891103285Sikob 892103285Sikob#if NEED_RESPONSE 893103285Sikobstatic void 894103285Sikobsbp_loginres_callback(struct fw_xfer *xfer){ 895113584Ssimokawa int s; 896103285Sikob struct sbp_dev *sdev; 897103285Sikob sdev = (struct sbp_dev *)xfer->sc; 898113584SsimokawaSBP_DEBUG(1) 899188756Ssbruno device_printf(sdev->target->sbp->fd.dev,"%s\n", __func__); 900103285SikobEND_DEBUG 901113584Ssimokawa /* recycle */ 902113584Ssimokawa s = splfw(); 903113584Ssimokawa STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link); 904113584Ssimokawa splx(s); 905103285Sikob return; 906103285Sikob} 907103285Sikob#endif 908103285Sikob 909113584Ssimokawastatic __inline void 910113584Ssimokawasbp_xfer_free(struct fw_xfer *xfer) 911113584Ssimokawa{ 912113584Ssimokawa struct sbp_dev *sdev; 913113584Ssimokawa int s; 914113584Ssimokawa 915113584Ssimokawa sdev = (struct sbp_dev *)xfer->sc; 916113584Ssimokawa fw_xfer_unload(xfer); 917113584Ssimokawa s = splfw(); 918170374Ssimokawa SBP_LOCK(sdev->target->sbp); 919113584Ssimokawa STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link); 920170374Ssimokawa SBP_UNLOCK(sdev->target->sbp); 921113584Ssimokawa splx(s); 922113584Ssimokawa} 923113584Ssimokawa 924103285Sikobstatic void 925114732Ssimokawasbp_reset_start_callback(struct fw_xfer *xfer) 926103285Sikob{ 927114732Ssimokawa struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc; 928114732Ssimokawa struct sbp_target *target = sdev->target; 929114732Ssimokawa int i; 930114732Ssimokawa 931114732Ssimokawa if (xfer->resp != 0) { 932188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 933188756Ssbruno "%s: %s failed: resp=%d\n", __func__, sdev->bustgtlun, xfer->resp); 934114732Ssimokawa } 935114732Ssimokawa 936114732Ssimokawa for (i = 0; i < target->num_lun; i++) { 937121185Ssimokawa tsdev = target->luns[i]; 938121185Ssimokawa if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN) 939121185Ssimokawa sbp_login(tsdev); 940114732Ssimokawa } 941114732Ssimokawa} 942114732Ssimokawa 943114732Ssimokawastatic void 944114732Ssimokawasbp_reset_start(struct sbp_dev *sdev) 945114732Ssimokawa{ 946114732Ssimokawa struct fw_xfer *xfer; 947114732Ssimokawa struct fw_pkt *fp; 948114732Ssimokawa 949114732SsimokawaSBP_DEBUG(0) 950188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 951188756Ssbruno "%s:%s\n", __func__,sdev->bustgtlun); 952114732SsimokawaEND_DEBUG 953114732Ssimokawa 954114732Ssimokawa xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 955167632Ssimokawa xfer->hand = sbp_reset_start_callback; 956120660Ssimokawa fp = &xfer->send.hdr; 957114732Ssimokawa fp->mode.wreqq.dest_hi = 0xffff; 958114732Ssimokawa fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START; 959114732Ssimokawa fp->mode.wreqq.data = htonl(0xf); 960114732Ssimokawa fw_asyreq(xfer->fc, -1, xfer); 961114732Ssimokawa} 962114732Ssimokawa 963114732Ssimokawastatic void 964114732Ssimokawasbp_mgm_callback(struct fw_xfer *xfer) 965114732Ssimokawa{ 966103285Sikob struct sbp_dev *sdev; 967114732Ssimokawa int resp; 968114732Ssimokawa 969103285Sikob sdev = (struct sbp_dev *)xfer->sc; 970114732Ssimokawa 971114732SsimokawaSBP_DEBUG(1) 972188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 973188756Ssbruno "%s:%s\n", __func__, sdev->bustgtlun); 974103285SikobEND_DEBUG 975114732Ssimokawa resp = xfer->resp; 976113584Ssimokawa sbp_xfer_free(xfer); 977103285Sikob return; 978103285Sikob} 979103285Sikob 980111615Ssimokawastatic struct sbp_dev * 981111615Ssimokawasbp_next_dev(struct sbp_target *target, int lun) 982111615Ssimokawa{ 983121185Ssimokawa struct sbp_dev **sdevp; 984111615Ssimokawa int i; 985111615Ssimokawa 986121185Ssimokawa for (i = lun, sdevp = &target->luns[lun]; i < target->num_lun; 987121185Ssimokawa i++, sdevp++) 988121185Ssimokawa if (*sdevp != NULL && (*sdevp)->status == SBP_DEV_PROBE) 989121185Ssimokawa return(*sdevp); 990121185Ssimokawa return(NULL); 991111615Ssimokawa} 992111615Ssimokawa 993111615Ssimokawa#define SCAN_PRI 1 994103285Sikobstatic void 995111615Ssimokawasbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb) 996103285Sikob{ 997111615Ssimokawa struct sbp_target *target; 998103285Sikob struct sbp_dev *sdev; 999111615Ssimokawa 1000103285Sikob sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr; 1001111615Ssimokawa target = sdev->target; 1002110269SsimokawaSBP_DEBUG(0) 1003188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1004188756Ssbruno "%s:%s\n", __func__, sdev->bustgtlun); 1005103285SikobEND_DEBUG 1006111615Ssimokawa if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 1007111615Ssimokawa sdev->status = SBP_DEV_ATTACHED; 1008111615Ssimokawa } else { 1009188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1010188756Ssbruno "%s:%s failed\n", __func__, sdev->bustgtlun); 1011111615Ssimokawa } 1012111615Ssimokawa sdev = sbp_next_dev(target, sdev->lun_id + 1); 1013111615Ssimokawa if (sdev == NULL) { 1014111615Ssimokawa free(ccb, M_SBP); 1015111615Ssimokawa return; 1016111615Ssimokawa } 1017111615Ssimokawa /* reuse ccb */ 1018111615Ssimokawa xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI); 1019111615Ssimokawa ccb->ccb_h.ccb_sdev_ptr = sdev; 1020111615Ssimokawa xpt_action(ccb); 1021111615Ssimokawa xpt_release_devq(sdev->path, sdev->freeze, TRUE); 1022111615Ssimokawa sdev->freeze = 1; 1023103285Sikob} 1024103285Sikob 1025103285Sikobstatic void 1026111615Ssimokawasbp_cam_scan_target(void *arg) 1027103285Sikob{ 1028111615Ssimokawa struct sbp_target *target = (struct sbp_target *)arg; 1029111615Ssimokawa struct sbp_dev *sdev; 1030110798Ssimokawa union ccb *ccb; 1031103285Sikob 1032111615Ssimokawa sdev = sbp_next_dev(target, 0); 1033111615Ssimokawa if (sdev == NULL) { 1034111615Ssimokawa printf("sbp_cam_scan_target: nothing to do for target%d\n", 1035111615Ssimokawa target->target_id); 1036110798Ssimokawa return; 1037110798Ssimokawa } 1038103285SikobSBP_DEBUG(0) 1039188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1040188756Ssbruno "%s:%s\n", __func__, sdev->bustgtlun); 1041103285SikobEND_DEBUG 1042111615Ssimokawa ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO); 1043111615Ssimokawa if (ccb == NULL) { 1044111615Ssimokawa printf("sbp_cam_scan_target: malloc failed\n"); 1045111615Ssimokawa return; 1046111615Ssimokawa } 1047111615Ssimokawa xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI); 1048103285Sikob ccb->ccb_h.func_code = XPT_SCAN_LUN; 1049111615Ssimokawa ccb->ccb_h.cbfcnp = sbp_cam_scan_lun; 1050111615Ssimokawa ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 1051103285Sikob ccb->crcn.flags = CAM_FLAG_NONE; 1052103285Sikob ccb->ccb_h.ccb_sdev_ptr = sdev; 1053103285Sikob 1054103285Sikob /* The scan is in progress now. */ 1055170374Ssimokawa SBP_LOCK(target->sbp); 1056111040Ssimokawa xpt_action(ccb); 1057111615Ssimokawa xpt_release_devq(sdev->path, sdev->freeze, TRUE); 1058111615Ssimokawa sdev->freeze = 1; 1059170374Ssimokawa SBP_UNLOCK(target->sbp); 1060103285Sikob} 1061103285Sikob 1062111615Ssimokawastatic __inline void 1063111615Ssimokawasbp_scan_dev(struct sbp_dev *sdev) 1064111615Ssimokawa{ 1065111040Ssimokawa sdev->status = SBP_DEV_PROBE; 1066122387Ssimokawa callout_reset(&sdev->target->scan_callout, scan_delay * hz / 1000, 1067111615Ssimokawa sbp_cam_scan_target, (void *)sdev->target); 1068103285Sikob} 1069103285Sikob 1070103285Sikobstatic void 1071103285Sikobsbp_do_attach(struct fw_xfer *xfer) 1072103285Sikob{ 1073103285Sikob struct sbp_dev *sdev; 1074111615Ssimokawa struct sbp_target *target; 1075111615Ssimokawa struct sbp_softc *sbp; 1076103285Sikob 1077103285Sikob sdev = (struct sbp_dev *)xfer->sc; 1078111615Ssimokawa target = sdev->target; 1079111615Ssimokawa sbp = target->sbp; 1080103285SikobSBP_DEBUG(0) 1081188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1082188756Ssbruno "%s:%s\n", __func__, sdev->bustgtlun); 1083103285SikobEND_DEBUG 1084113584Ssimokawa sbp_xfer_free(xfer); 1085111199Ssimokawa 1086103285Sikob if (sdev->path == NULL) 1087103285Sikob xpt_create_path(&sdev->path, xpt_periph, 1088111615Ssimokawa cam_sim_path(target->sbp->sim), 1089111615Ssimokawa target->target_id, sdev->lun_id); 1090103285Sikob 1091111199Ssimokawa /* 1092111199Ssimokawa * Let CAM scan the bus if we are in the boot process. 1093111199Ssimokawa * XXX xpt_scan_bus cannot detect LUN larger than 0 1094111199Ssimokawa * if LUN 0 doesn't exists. 1095111199Ssimokawa */ 1096111199Ssimokawa if (sbp_cold > 0) { 1097111615Ssimokawa sdev->status = SBP_DEV_ATTACHED; 1098111199Ssimokawa return; 1099111199Ssimokawa } 1100111199Ssimokawa 1101111615Ssimokawa sbp_scan_dev(sdev); 1102103285Sikob return; 1103103285Sikob} 1104103285Sikob 1105103285Sikobstatic void 1106103285Sikobsbp_agent_reset_callback(struct fw_xfer *xfer) 1107103285Sikob{ 1108103285Sikob struct sbp_dev *sdev; 1109103285Sikob 1110103285Sikob sdev = (struct sbp_dev *)xfer->sc; 1111103285SikobSBP_DEBUG(1) 1112188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1113188756Ssbruno "%s:%s\n", __func__, sdev->bustgtlun); 1114103285SikobEND_DEBUG 1115114732Ssimokawa if (xfer->resp != 0) { 1116188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1117188756Ssbruno "%s:%s resp=%d\n", __func__, sdev->bustgtlun, xfer->resp); 1118114732Ssimokawa } 1119114732Ssimokawa 1120113584Ssimokawa sbp_xfer_free(xfer); 1121111615Ssimokawa if (sdev->path) { 1122170374Ssimokawa SBP_LOCK(sdev->target->sbp); 1123111615Ssimokawa xpt_release_devq(sdev->path, sdev->freeze, TRUE); 1124111615Ssimokawa sdev->freeze = 0; 1125170374Ssimokawa SBP_UNLOCK(sdev->target->sbp); 1126111615Ssimokawa } 1127103285Sikob} 1128103285Sikob 1129103285Sikobstatic void 1130110336Ssimokawasbp_agent_reset(struct sbp_dev *sdev) 1131103285Sikob{ 1132103285Sikob struct fw_xfer *xfer; 1133103285Sikob struct fw_pkt *fp; 1134103285Sikob 1135103285SikobSBP_DEBUG(0) 1136188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1137188756Ssbruno "%s:%s\n", __func__, sdev->bustgtlun); 1138103285SikobEND_DEBUG 1139103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04); 1140103285Sikob if (xfer == NULL) 1141103285Sikob return; 1142115788Ssimokawa if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE) 1143167632Ssimokawa xfer->hand = sbp_agent_reset_callback; 1144110336Ssimokawa else 1145167632Ssimokawa xfer->hand = sbp_do_attach; 1146120660Ssimokawa fp = &xfer->send.hdr; 1147103285Sikob fp->mode.wreqq.data = htonl(0xf); 1148103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1149111615Ssimokawa sbp_abort_all_ocbs(sdev, CAM_BDR_SENT); 1150103285Sikob} 1151103285Sikob 1152103285Sikobstatic void 1153103285Sikobsbp_busy_timeout_callback(struct fw_xfer *xfer) 1154103285Sikob{ 1155103285Sikob struct sbp_dev *sdev; 1156103285Sikob 1157103285Sikob sdev = (struct sbp_dev *)xfer->sc; 1158103285SikobSBP_DEBUG(1) 1159188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1160188756Ssbruno "%s:%s\n", __func__, sdev->bustgtlun); 1161103285SikobEND_DEBUG 1162113584Ssimokawa sbp_xfer_free(xfer); 1163110336Ssimokawa sbp_agent_reset(sdev); 1164103285Sikob} 1165103285Sikob 1166103285Sikobstatic void 1167103285Sikobsbp_busy_timeout(struct sbp_dev *sdev) 1168103285Sikob{ 1169103285Sikob struct fw_pkt *fp; 1170103285Sikob struct fw_xfer *xfer; 1171103285SikobSBP_DEBUG(0) 1172188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1173188756Ssbruno "%s:%s\n", __func__, sdev->bustgtlun); 1174103285SikobEND_DEBUG 1175103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 1176103285Sikob 1177167632Ssimokawa xfer->hand = sbp_busy_timeout_callback; 1178120660Ssimokawa fp = &xfer->send.hdr; 1179113584Ssimokawa fp->mode.wreqq.dest_hi = 0xffff; 1180113584Ssimokawa fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT; 1181110129Ssimokawa fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf); 1182103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1183103285Sikob} 1184103285Sikob 1185103285Sikobstatic void 1186121185Ssimokawasbp_orb_pointer_callback(struct fw_xfer *xfer) 1187121185Ssimokawa{ 1188121185Ssimokawa struct sbp_dev *sdev; 1189121185Ssimokawa sdev = (struct sbp_dev *)xfer->sc; 1190121185Ssimokawa 1191170807SsimokawaSBP_DEBUG(2) 1192188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1193188756Ssbruno "%s:%s\n", __func__, sdev->bustgtlun); 1194121185SsimokawaEND_DEBUG 1195121185Ssimokawa if (xfer->resp != 0) { 1196121185Ssimokawa /* XXX */ 1197127468Ssimokawa printf("%s: xfer->resp = %d\n", __func__, xfer->resp); 1198121185Ssimokawa } 1199121185Ssimokawa sbp_xfer_free(xfer); 1200170807Ssimokawa 1201170807Ssimokawa SBP_LOCK(sdev->target->sbp); 1202121185Ssimokawa sdev->flags &= ~ORB_POINTER_ACTIVE; 1203121185Ssimokawa 1204121185Ssimokawa if ((sdev->flags & ORB_POINTER_NEED) != 0) { 1205121185Ssimokawa struct sbp_ocb *ocb; 1206121185Ssimokawa 1207121185Ssimokawa sdev->flags &= ~ORB_POINTER_NEED; 1208121185Ssimokawa ocb = STAILQ_FIRST(&sdev->ocbs); 1209121185Ssimokawa if (ocb != NULL) 1210121185Ssimokawa sbp_orb_pointer(sdev, ocb); 1211121185Ssimokawa } 1212170807Ssimokawa SBP_UNLOCK(sdev->target->sbp); 1213121185Ssimokawa return; 1214121185Ssimokawa} 1215121185Ssimokawa 1216121185Ssimokawastatic void 1217103285Sikobsbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb) 1218103285Sikob{ 1219103285Sikob struct fw_xfer *xfer; 1220103285Sikob struct fw_pkt *fp; 1221121185SsimokawaSBP_DEBUG(1) 1222188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1223188756Ssbruno "%s:%s 0x%08x\n", 1224188756Ssbruno __func__, sdev->bustgtlun, 1225188756Ssbruno (uint32_t)ocb->bus_addr); 1226103285SikobEND_DEBUG 1227103285Sikob 1228170807Ssimokawa mtx_assert(&sdev->target->sbp->mtx, MA_OWNED); 1229170807Ssimokawa 1230121185Ssimokawa if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) { 1231122421SsimokawaSBP_DEBUG(0) 1232127468Ssimokawa printf("%s: orb pointer active\n", __func__); 1233122421SsimokawaEND_DEBUG 1234121185Ssimokawa sdev->flags |= ORB_POINTER_NEED; 1235121185Ssimokawa return; 1236121185Ssimokawa } 1237121185Ssimokawa 1238121185Ssimokawa sdev->flags |= ORB_POINTER_ACTIVE; 1239170807Ssimokawa xfer = sbp_write_cmd_locked(sdev, FWTCODE_WREQB, 0x08); 1240103285Sikob if (xfer == NULL) 1241103285Sikob return; 1242167632Ssimokawa xfer->hand = sbp_orb_pointer_callback; 1243103285Sikob 1244120660Ssimokawa fp = &xfer->send.hdr; 1245113584Ssimokawa fp->mode.wreqb.len = 8; 1246103285Sikob fp->mode.wreqb.extcode = 0; 1247120660Ssimokawa xfer->send.payload[0] = 1248103285Sikob htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16)); 1249129585Sdfr xfer->send.payload[1] = htonl((uint32_t)ocb->bus_addr); 1250103285Sikob 1251187993Ssbruno /* 1252187993Ssbruno * sbp_xfer_free() will attempt to acquire 1253187993Ssbruno * the SBP lock on entrance. Also, this removes 1254187993Ssbruno * a LOR between the firewire layer and sbp 1255187993Ssbruno */ 1256187993Ssbruno SBP_UNLOCK(sdev->target->sbp); 1257103285Sikob if(fw_asyreq(xfer->fc, -1, xfer) != 0){ 1258113584Ssimokawa sbp_xfer_free(xfer); 1259103285Sikob ocb->ccb->ccb_h.status = CAM_REQ_INVALID; 1260103285Sikob xpt_done(ocb->ccb); 1261103285Sikob } 1262187993Ssbruno SBP_LOCK(sdev->target->sbp); 1263103285Sikob} 1264103285Sikob 1265103285Sikobstatic void 1266127468Ssimokawasbp_doorbell_callback(struct fw_xfer *xfer) 1267121185Ssimokawa{ 1268121185Ssimokawa struct sbp_dev *sdev; 1269121185Ssimokawa sdev = (struct sbp_dev *)xfer->sc; 1270127468Ssimokawa 1271127468SsimokawaSBP_DEBUG(1) 1272188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1273188756Ssbruno "%s:%s\n", __func__, sdev->bustgtlun); 1274121185SsimokawaEND_DEBUG 1275121185Ssimokawa if (xfer->resp != 0) { 1276121185Ssimokawa /* XXX */ 1277188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1278188756Ssbruno "%s: xfer->resp = %d\n", __func__, xfer->resp); 1279121185Ssimokawa } 1280121185Ssimokawa sbp_xfer_free(xfer); 1281127468Ssimokawa sdev->flags &= ~ORB_DOORBELL_ACTIVE; 1282127468Ssimokawa if ((sdev->flags & ORB_DOORBELL_NEED) != 0) { 1283127468Ssimokawa sdev->flags &= ~ORB_DOORBELL_NEED; 1284170807Ssimokawa SBP_LOCK(sdev->target->sbp); 1285127468Ssimokawa sbp_doorbell(sdev); 1286170807Ssimokawa SBP_UNLOCK(sdev->target->sbp); 1287127468Ssimokawa } 1288121185Ssimokawa return; 1289121185Ssimokawa} 1290121185Ssimokawa 1291121185Ssimokawastatic void 1292103285Sikobsbp_doorbell(struct sbp_dev *sdev) 1293103285Sikob{ 1294103285Sikob struct fw_xfer *xfer; 1295103285Sikob struct fw_pkt *fp; 1296103285SikobSBP_DEBUG(1) 1297188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1298188756Ssbruno "%s:%s\n", __func__, sdev->bustgtlun); 1299103285SikobEND_DEBUG 1300103285Sikob 1301127468Ssimokawa if ((sdev->flags & ORB_DOORBELL_ACTIVE) != 0) { 1302127468Ssimokawa sdev->flags |= ORB_DOORBELL_NEED; 1303127468Ssimokawa return; 1304127468Ssimokawa } 1305127468Ssimokawa sdev->flags |= ORB_DOORBELL_ACTIVE; 1306170807Ssimokawa xfer = sbp_write_cmd_locked(sdev, FWTCODE_WREQQ, 0x10); 1307103285Sikob if (xfer == NULL) 1308103285Sikob return; 1309167632Ssimokawa xfer->hand = sbp_doorbell_callback; 1310127468Ssimokawa fp = &xfer->send.hdr; 1311103285Sikob fp->mode.wreqq.data = htonl(0xf); 1312103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1313103285Sikob} 1314103285Sikob 1315103285Sikobstatic struct fw_xfer * 1316170807Ssimokawasbp_write_cmd_locked(struct sbp_dev *sdev, int tcode, int offset) 1317103285Sikob{ 1318103285Sikob struct fw_xfer *xfer; 1319103285Sikob struct fw_pkt *fp; 1320113584Ssimokawa struct sbp_target *target; 1321113584Ssimokawa int s, new = 0; 1322103285Sikob 1323170807Ssimokawa mtx_assert(&sdev->target->sbp->mtx, MA_OWNED); 1324170807Ssimokawa 1325113584Ssimokawa target = sdev->target; 1326113584Ssimokawa s = splfw(); 1327113584Ssimokawa xfer = STAILQ_FIRST(&target->xferlist); 1328113584Ssimokawa if (xfer == NULL) { 1329113584Ssimokawa if (target->n_xfer > 5 /* XXX */) { 1330113584Ssimokawa printf("sbp: no more xfer for this target\n"); 1331113584Ssimokawa splx(s); 1332113584Ssimokawa return(NULL); 1333113584Ssimokawa } 1334120660Ssimokawa xfer = fw_xfer_alloc_buf(M_SBP, 8, 0); 1335113584Ssimokawa if(xfer == NULL){ 1336113584Ssimokawa printf("sbp: fw_xfer_alloc_buf failed\n"); 1337113584Ssimokawa splx(s); 1338113584Ssimokawa return NULL; 1339113584Ssimokawa } 1340113584Ssimokawa target->n_xfer ++; 1341113584Ssimokawa if (debug) 1342113584Ssimokawa printf("sbp: alloc %d xfer\n", target->n_xfer); 1343113584Ssimokawa new = 1; 1344113584Ssimokawa } else { 1345113584Ssimokawa STAILQ_REMOVE_HEAD(&target->xferlist, link); 1346103285Sikob } 1347113584Ssimokawa splx(s); 1348113584Ssimokawa 1349113584Ssimokawa if (new) { 1350120660Ssimokawa xfer->recv.pay_len = 0; 1351120660Ssimokawa xfer->send.spd = min(sdev->target->fwdev->speed, max_speed); 1352113584Ssimokawa xfer->fc = sdev->target->sbp->fd.fc; 1353103285Sikob } 1354120660Ssimokawa 1355120660Ssimokawa if (tcode == FWTCODE_WREQB) 1356120660Ssimokawa xfer->send.pay_len = 8; 1357120660Ssimokawa else 1358120660Ssimokawa xfer->send.pay_len = 0; 1359120660Ssimokawa 1360103285Sikob xfer->sc = (caddr_t)sdev; 1361120660Ssimokawa fp = &xfer->send.hdr; 1362113584Ssimokawa fp->mode.wreqq.dest_hi = sdev->login->cmd_hi; 1363113584Ssimokawa fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset; 1364103285Sikob fp->mode.wreqq.tlrt = 0; 1365103285Sikob fp->mode.wreqq.tcode = tcode; 1366103285Sikob fp->mode.wreqq.pri = 0; 1367120660Ssimokawa fp->mode.wreqq.dst = FWLOCALBUS | sdev->target->fwdev->dst; 1368103285Sikob 1369103285Sikob return xfer; 1370103285Sikob 1371103285Sikob} 1372103285Sikob 1373170807Ssimokawastatic struct fw_xfer * 1374170807Ssimokawasbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset) 1375170807Ssimokawa{ 1376170807Ssimokawa struct sbp_softc *sbp = sdev->target->sbp; 1377170807Ssimokawa struct fw_xfer *xfer; 1378170807Ssimokawa 1379170807Ssimokawa SBP_LOCK(sbp); 1380170807Ssimokawa xfer = sbp_write_cmd_locked(sdev, tcode, offset); 1381170807Ssimokawa SBP_UNLOCK(sbp); 1382170807Ssimokawa 1383170807Ssimokawa return (xfer); 1384170807Ssimokawa} 1385170807Ssimokawa 1386103285Sikobstatic void 1387111615Ssimokawasbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb) 1388103285Sikob{ 1389103285Sikob struct fw_xfer *xfer; 1390103285Sikob struct fw_pkt *fp; 1391103285Sikob struct sbp_ocb *ocb; 1392111615Ssimokawa struct sbp_target *target; 1393103285Sikob int s, nid; 1394103285Sikob 1395111615Ssimokawa target = sdev->target; 1396111615Ssimokawa nid = target->sbp->fd.fc->nodeid | FWLOCALBUS; 1397111615Ssimokawa 1398111615Ssimokawa s = splfw(); 1399170374Ssimokawa SBP_LOCK(target->sbp); 1400111615Ssimokawa if (func == ORB_FUN_RUNQUEUE) { 1401111615Ssimokawa ocb = STAILQ_FIRST(&target->mgm_ocb_queue); 1402111615Ssimokawa if (target->mgm_ocb_cur != NULL || ocb == NULL) { 1403170374Ssimokawa SBP_UNLOCK(target->sbp); 1404111615Ssimokawa splx(s); 1405111615Ssimokawa return; 1406111615Ssimokawa } 1407111615Ssimokawa STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb); 1408170374Ssimokawa SBP_UNLOCK(target->sbp); 1409111615Ssimokawa goto start; 1410111615Ssimokawa } 1411113584Ssimokawa if ((ocb = sbp_get_ocb(sdev)) == NULL) { 1412170374Ssimokawa SBP_UNLOCK(target->sbp); 1413103285Sikob splx(s); 1414122528Ssimokawa /* XXX */ 1415103285Sikob return; 1416103285Sikob } 1417170374Ssimokawa SBP_UNLOCK(target->sbp); 1418103285Sikob ocb->flags = OCB_ACT_MGM; 1419103285Sikob ocb->sdev = sdev; 1420103285Sikob 1421120660Ssimokawa bzero((void *)ocb->orb, sizeof(ocb->orb)); 1422103285Sikob ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI); 1423120660Ssimokawa ocb->orb[7] = htonl(SBP_DEV2ADDR(target->target_id, sdev->lun_id)); 1424103285Sikob 1425107653SsimokawaSBP_DEBUG(0) 1426188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1427188756Ssbruno "%s:%s %s\n", 1428188756Ssbruno __func__,sdev->bustgtlun, 1429188756Ssbruno orb_fun_name[(func>>16)&0xf]); 1430107653SsimokawaEND_DEBUG 1431103285Sikob switch (func) { 1432103285Sikob case ORB_FUN_LGI: 1433120660Ssimokawa ocb->orb[0] = ocb->orb[1] = 0; /* password */ 1434103285Sikob ocb->orb[2] = htonl(nid << 16); 1435113584Ssimokawa ocb->orb[3] = htonl(sdev->dma.bus_addr); 1436121792Ssimokawa ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id); 1437122387Ssimokawa if (ex_login) 1438121792Ssimokawa ocb->orb[4] |= htonl(ORB_EXV); 1439113584Ssimokawa ocb->orb[5] = htonl(SBP_LOGIN_SIZE); 1440113584Ssimokawa fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD); 1441103285Sikob break; 1442110336Ssimokawa case ORB_FUN_ATA: 1443111615Ssimokawa ocb->orb[0] = htonl((0 << 16) | 0); 1444113584Ssimokawa ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff); 1445110336Ssimokawa /* fall through */ 1446103285Sikob case ORB_FUN_RCN: 1447103285Sikob case ORB_FUN_LGO: 1448103285Sikob case ORB_FUN_LUR: 1449103285Sikob case ORB_FUN_RST: 1450103285Sikob case ORB_FUN_ATS: 1451113584Ssimokawa ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id); 1452103285Sikob break; 1453103285Sikob } 1454103285Sikob 1455111615Ssimokawa if (target->mgm_ocb_cur != NULL) { 1456111615Ssimokawa /* there is a standing ORB */ 1457170374Ssimokawa SBP_LOCK(target->sbp); 1458111615Ssimokawa STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb); 1459170374Ssimokawa SBP_UNLOCK(target->sbp); 1460111615Ssimokawa splx(s); 1461111615Ssimokawa return; 1462111615Ssimokawa } 1463111615Ssimokawastart: 1464111615Ssimokawa target->mgm_ocb_cur = ocb; 1465111615Ssimokawa splx(s); 1466111615Ssimokawa 1467111615Ssimokawa callout_reset(&target->mgm_ocb_timeout, 5*hz, 1468114732Ssimokawa sbp_mgm_timeout, (caddr_t)ocb); 1469103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0); 1470103285Sikob if(xfer == NULL){ 1471103285Sikob return; 1472103285Sikob } 1473167632Ssimokawa xfer->hand = sbp_mgm_callback; 1474103285Sikob 1475120660Ssimokawa fp = &xfer->send.hdr; 1476113584Ssimokawa fp->mode.wreqb.dest_hi = sdev->target->mgm_hi; 1477113584Ssimokawa fp->mode.wreqb.dest_lo = sdev->target->mgm_lo; 1478113584Ssimokawa fp->mode.wreqb.len = 8; 1479103285Sikob fp->mode.wreqb.extcode = 0; 1480120660Ssimokawa xfer->send.payload[0] = htonl(nid << 16); 1481120660Ssimokawa xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff); 1482103285Sikob 1483103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1484103285Sikob} 1485103285Sikob 1486103285Sikobstatic void 1487105792Ssimokawasbp_print_scsi_cmd(struct sbp_ocb *ocb) 1488103285Sikob{ 1489103285Sikob struct ccb_scsiio *csio; 1490103285Sikob 1491103285Sikob csio = &ocb->ccb->csio; 1492103285Sikob printf("%s:%d:%d XPT_SCSI_IO: " 1493103285Sikob "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" 1494103285Sikob ", flags: 0x%02x, " 1495103285Sikob "%db cmd/%db data/%db sense\n", 1496103285Sikob device_get_nameunit(ocb->sdev->target->sbp->fd.dev), 1497103285Sikob ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun, 1498103285Sikob csio->cdb_io.cdb_bytes[0], 1499103285Sikob csio->cdb_io.cdb_bytes[1], 1500103285Sikob csio->cdb_io.cdb_bytes[2], 1501103285Sikob csio->cdb_io.cdb_bytes[3], 1502103285Sikob csio->cdb_io.cdb_bytes[4], 1503103285Sikob csio->cdb_io.cdb_bytes[5], 1504103285Sikob csio->cdb_io.cdb_bytes[6], 1505103285Sikob csio->cdb_io.cdb_bytes[7], 1506103285Sikob csio->cdb_io.cdb_bytes[8], 1507103285Sikob csio->cdb_io.cdb_bytes[9], 1508103285Sikob ocb->ccb->ccb_h.flags & CAM_DIR_MASK, 1509103285Sikob csio->cdb_len, csio->dxfer_len, 1510103285Sikob csio->sense_len); 1511105792Ssimokawa} 1512105792Ssimokawa 1513105792Ssimokawastatic void 1514105792Ssimokawasbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb) 1515105792Ssimokawa{ 1516105792Ssimokawa struct sbp_cmd_status *sbp_cmd_status; 1517105792Ssimokawa struct scsi_sense_data *sense; 1518105792Ssimokawa 1519105792Ssimokawa sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data; 1520105792Ssimokawa sense = &ocb->ccb->csio.sense_data; 1521105792Ssimokawa 1522105792SsimokawaSBP_DEBUG(0) 1523105792Ssimokawa sbp_print_scsi_cmd(ocb); 1524103285Sikob /* XXX need decode status */ 1525188756Ssbruno printf("%s: SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n", 1526188756Ssbruno ocb->sdev->bustgtlun, 1527103285Sikob sbp_cmd_status->status, 1528103285Sikob sbp_cmd_status->sfmt, 1529103285Sikob sbp_cmd_status->valid, 1530103285Sikob sbp_cmd_status->s_key, 1531103285Sikob sbp_cmd_status->s_code, 1532103285Sikob sbp_cmd_status->s_qlfr, 1533188756Ssbruno sbp_status->len); 1534103285SikobEND_DEBUG 1535103285Sikob 1536110071Ssimokawa switch (sbp_cmd_status->status) { 1537110071Ssimokawa case SCSI_STATUS_CHECK_COND: 1538110071Ssimokawa case SCSI_STATUS_BUSY: 1539110071Ssimokawa case SCSI_STATUS_CMD_TERMINATED: 1540103285Sikob if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){ 1541103285Sikob sense->error_code = SSD_CURRENT_ERROR; 1542103285Sikob }else{ 1543103285Sikob sense->error_code = SSD_DEFERRED_ERROR; 1544103285Sikob } 1545103285Sikob if(sbp_cmd_status->valid) 1546103285Sikob sense->error_code |= SSD_ERRCODE_VALID; 1547103285Sikob sense->flags = sbp_cmd_status->s_key; 1548103285Sikob if(sbp_cmd_status->mark) 1549103285Sikob sense->flags |= SSD_FILEMARK; 1550103285Sikob if(sbp_cmd_status->eom) 1551103285Sikob sense->flags |= SSD_EOM; 1552103285Sikob if(sbp_cmd_status->ill_len) 1553103285Sikob sense->flags |= SSD_ILI; 1554119556Ssimokawa 1555119556Ssimokawa bcopy(&sbp_cmd_status->info, &sense->info[0], 4); 1556119556Ssimokawa 1557103285Sikob if (sbp_status->len <= 1) 1558103285Sikob /* XXX not scsi status. shouldn't be happened */ 1559103285Sikob sense->extra_len = 0; 1560103285Sikob else if (sbp_status->len <= 4) 1561103285Sikob /* add_sense_code(_qual), info, cmd_spec_info */ 1562103285Sikob sense->extra_len = 6; 1563103285Sikob else 1564103285Sikob /* fru, sense_key_spec */ 1565103285Sikob sense->extra_len = 10; 1566119556Ssimokawa 1567119556Ssimokawa bcopy(&sbp_cmd_status->cdb, &sense->cmd_spec_info[0], 4); 1568119556Ssimokawa 1569103285Sikob sense->add_sense_code = sbp_cmd_status->s_code; 1570103285Sikob sense->add_sense_code_qual = sbp_cmd_status->s_qlfr; 1571103285Sikob sense->fru = sbp_cmd_status->fru; 1572103285Sikob 1573119556Ssimokawa bcopy(&sbp_cmd_status->s_keydep[0], 1574119556Ssimokawa &sense->sense_key_spec[0], 3); 1575119556Ssimokawa 1576103285Sikob ocb->ccb->csio.scsi_status = sbp_cmd_status->status;; 1577103285Sikob ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR 1578103285Sikob | CAM_AUTOSNS_VALID; 1579103285Sikob/* 1580103285Sikob{ 1581129585Sdfr uint8_t j, *tmp; 1582103285Sikob tmp = sense; 1583103285Sikob for( j = 0 ; j < 32 ; j+=8){ 1584103285Sikob printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n", 1585103285Sikob tmp[j], tmp[j+1], tmp[j+2], tmp[j+3], 1586103285Sikob tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]); 1587103285Sikob } 1588103285Sikob 1589103285Sikob} 1590103285Sikob*/ 1591110071Ssimokawa break; 1592110071Ssimokawa default: 1593188756Ssbruno device_printf(ocb->sdev->target->sbp->fd.dev, 1594188756Ssbruno "%s:%s unknown scsi status 0x%x\n", 1595188756Ssbruno __func__, ocb->sdev->bustgtlun, 1596188756Ssbruno sbp_cmd_status->status); 1597103285Sikob } 1598103285Sikob} 1599103285Sikob 1600103285Sikobstatic void 1601103285Sikobsbp_fix_inq_data(struct sbp_ocb *ocb) 1602103285Sikob{ 1603103285Sikob union ccb *ccb; 1604103285Sikob struct sbp_dev *sdev; 1605103285Sikob struct scsi_inquiry_data *inq; 1606103285Sikob 1607103285Sikob ccb = ocb->ccb; 1608103285Sikob sdev = ocb->sdev; 1609103285Sikob 1610103285Sikob if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD) 1611103285Sikob return; 1612103285SikobSBP_DEBUG(1) 1613188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1614188756Ssbruno "%s:%s\n", __func__, sdev->bustgtlun); 1615103285SikobEND_DEBUG 1616103285Sikob inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr; 1617103285Sikob switch (SID_TYPE(inq)) { 1618103285Sikob case T_DIRECT: 1619118291Ssimokawa#if 0 1620103285Sikob /* 1621103285Sikob * XXX Convert Direct Access device to RBC. 1622108281Ssimokawa * I've never seen FireWire DA devices which support READ_6. 1623103285Sikob */ 1624103285Sikob if (SID_TYPE(inq) == T_DIRECT) 1625103285Sikob inq->device |= T_RBC; /* T_DIRECT == 0 */ 1626103285Sikob#endif 1627103285Sikob /* fall through */ 1628103285Sikob case T_RBC: 1629103285Sikob /* 1630103285Sikob * Override vendor/product/revision information. 1631103285Sikob * Some devices sometimes return strange strings. 1632103285Sikob */ 1633111615Ssimokawa#if 1 1634103285Sikob bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor)); 1635103285Sikob bcopy(sdev->product, inq->product, sizeof(inq->product)); 1636103285Sikob bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision)); 1637111615Ssimokawa#endif 1638103285Sikob break; 1639103285Sikob } 1640127468Ssimokawa /* 1641127468Ssimokawa * Force to enable/disable tagged queuing. 1642127468Ssimokawa * XXX CAM also checks SCP_QUEUE_DQUE flag in the control mode page. 1643127468Ssimokawa */ 1644127468Ssimokawa if (sbp_tags > 0) 1645127468Ssimokawa inq->flags |= SID_CmdQue; 1646127468Ssimokawa else if (sbp_tags < 0) 1647127468Ssimokawa inq->flags &= ~SID_CmdQue; 1648127468Ssimokawa 1649103285Sikob} 1650103285Sikob 1651103285Sikobstatic void 1652113584Ssimokawasbp_recv1(struct fw_xfer *xfer) 1653113584Ssimokawa{ 1654103285Sikob struct fw_pkt *rfp; 1655103285Sikob#if NEED_RESPONSE 1656103285Sikob struct fw_pkt *sfp; 1657103285Sikob#endif 1658103285Sikob struct sbp_softc *sbp; 1659103285Sikob struct sbp_dev *sdev; 1660103285Sikob struct sbp_ocb *ocb; 1661103285Sikob struct sbp_login_res *login_res = NULL; 1662103285Sikob struct sbp_status *sbp_status; 1663103285Sikob struct sbp_target *target; 1664111704Ssimokawa int orb_fun, status_valid0, status_valid, t, l, reset_agent = 0; 1665129585Sdfr uint32_t addr; 1666103285Sikob/* 1667129585Sdfr uint32_t *ld; 1668103285Sikob ld = xfer->recv.buf; 1669103285Sikobprintf("sbp %x %d %d %08x %08x %08x %08x\n", 1670103285Sikob xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3])); 1671103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 1672103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11])); 1673103285Sikob*/ 1674113584Ssimokawa sbp = (struct sbp_softc *)xfer->sc; 1675120660Ssimokawa if (xfer->resp != 0){ 1676122529Ssimokawa printf("sbp_recv: xfer->resp = %d\n", xfer->resp); 1677113584Ssimokawa goto done0; 1678103285Sikob } 1679120660Ssimokawa if (xfer->recv.payload == NULL){ 1680120660Ssimokawa printf("sbp_recv: xfer->recv.payload == NULL\n"); 1681113584Ssimokawa goto done0; 1682103285Sikob } 1683120660Ssimokawa rfp = &xfer->recv.hdr; 1684103285Sikob if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){ 1685103285Sikob printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode); 1686113584Ssimokawa goto done0; 1687103285Sikob } 1688120660Ssimokawa sbp_status = (struct sbp_status *)xfer->recv.payload; 1689113584Ssimokawa addr = rfp->mode.wreqb.dest_lo; 1690103285SikobSBP_DEBUG(2) 1691103285Sikob printf("received address 0x%x\n", addr); 1692103285SikobEND_DEBUG 1693110189Ssimokawa t = SBP_ADDR2TRG(addr); 1694110189Ssimokawa if (t >= SBP_NUM_TARGETS) { 1695110189Ssimokawa device_printf(sbp->fd.dev, 1696110189Ssimokawa "sbp_recv1: invalid target %d\n", t); 1697113584Ssimokawa goto done0; 1698110189Ssimokawa } 1699110189Ssimokawa target = &sbp->targets[t]; 1700110189Ssimokawa l = SBP_ADDR2LUN(addr); 1701121185Ssimokawa if (l >= target->num_lun || target->luns[l] == NULL) { 1702110189Ssimokawa device_printf(sbp->fd.dev, 1703110189Ssimokawa "sbp_recv1: invalid lun %d (target=%d)\n", l, t); 1704113584Ssimokawa goto done0; 1705110189Ssimokawa } 1706121185Ssimokawa sdev = target->luns[l]; 1707103285Sikob 1708110189Ssimokawa ocb = NULL; 1709110189Ssimokawa switch (sbp_status->src) { 1710110189Ssimokawa case 0: 1711110189Ssimokawa case 1: 1712111615Ssimokawa /* check mgm_ocb_cur first */ 1713111615Ssimokawa ocb = target->mgm_ocb_cur; 1714111615Ssimokawa if (ocb != NULL) { 1715111615Ssimokawa if (OCB_MATCH(ocb, sbp_status)) { 1716111615Ssimokawa callout_stop(&target->mgm_ocb_timeout); 1717111615Ssimokawa target->mgm_ocb_cur = NULL; 1718111615Ssimokawa break; 1719111615Ssimokawa } 1720111615Ssimokawa } 1721111615Ssimokawa ocb = sbp_dequeue_ocb(sdev, sbp_status); 1722110189Ssimokawa if (ocb == NULL) { 1723188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1724127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 1725188756Ssbruno "%s:%s No ocb(%lx) on the queue\n", 1726127468Ssimokawa#else 1727188756Ssbruno "%s:%s No ocb(%x) on the queue\n", 1728111858Ssimokawa#endif 1729188756Ssbruno __func__,sdev->bustgtlun, 1730188756Ssbruno ntohl(sbp_status->orb_lo)); 1731110189Ssimokawa } 1732110189Ssimokawa break; 1733110189Ssimokawa case 2: 1734110189Ssimokawa /* unsolicit */ 1735188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1736188756Ssbruno "%s:%s unsolicit status received\n", 1737188756Ssbruno __func__, sdev->bustgtlun); 1738110189Ssimokawa break; 1739110189Ssimokawa default: 1740188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1741188756Ssbruno "%s:%s unknown sbp_status->src\n", 1742188756Ssbruno __func__, sdev->bustgtlun); 1743110189Ssimokawa } 1744110189Ssimokawa 1745111615Ssimokawa status_valid0 = (sbp_status->src < 2 1746110189Ssimokawa && sbp_status->resp == ORB_RES_CMPL 1747111615Ssimokawa && sbp_status->dead == 0); 1748111615Ssimokawa status_valid = (status_valid0 && sbp_status->status == 0); 1749103285Sikob 1750121185Ssimokawa if (!status_valid0 || debug > 2){ 1751103285Sikob int status; 1752110129SsimokawaSBP_DEBUG(0) 1753188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1754188756Ssbruno "%s:%s ORB status src:%x resp:%x dead:%x" 1755127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 1756127468Ssimokawa " len:%x stat:%x orb:%x%08lx\n", 1757127468Ssimokawa#else 1758103285Sikob " len:%x stat:%x orb:%x%08x\n", 1759108712Ssimokawa#endif 1760188756Ssbruno __func__, sdev->bustgtlun, 1761103285Sikob sbp_status->src, sbp_status->resp, sbp_status->dead, 1762103285Sikob sbp_status->len, sbp_status->status, 1763108280Ssimokawa ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo)); 1764110129SsimokawaEND_DEBUG 1765188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1766188756Ssbruno "%s\n", sdev->bustgtlun); 1767103285Sikob status = sbp_status->status; 1768103285Sikob switch(sbp_status->resp) { 1769103285Sikob case 0: 1770103285Sikob if (status > MAX_ORB_STATUS0) 1771103285Sikob printf("%s\n", orb_status0[MAX_ORB_STATUS0]); 1772110189Ssimokawa else 1773110129Ssimokawa printf("%s\n", orb_status0[status]); 1774103285Sikob break; 1775103285Sikob case 1: 1776110336Ssimokawa printf("Obj: %s, Error: %s\n", 1777103285Sikob orb_status1_object[(status>>6) & 3], 1778103285Sikob orb_status1_serial_bus_error[status & 0xf]); 1779103285Sikob break; 1780110129Ssimokawa case 2: 1781110129Ssimokawa printf("Illegal request\n"); 1782110129Ssimokawa break; 1783110129Ssimokawa case 3: 1784110129Ssimokawa printf("Vendor dependent\n"); 1785110129Ssimokawa break; 1786103285Sikob default: 1787110129Ssimokawa printf("unknown respose code %d\n", sbp_status->resp); 1788103285Sikob } 1789103285Sikob } 1790103285Sikob 1791103285Sikob /* we have to reset the fetch agent if it's dead */ 1792103285Sikob if (sbp_status->dead) { 1793111615Ssimokawa if (sdev->path) { 1794170374Ssimokawa SBP_LOCK(sbp); 1795103285Sikob xpt_freeze_devq(sdev->path, 1); 1796111615Ssimokawa sdev->freeze ++; 1797170374Ssimokawa SBP_UNLOCK(sbp); 1798111615Ssimokawa } 1799111704Ssimokawa reset_agent = 1; 1800103285Sikob } 1801103285Sikob 1802111704Ssimokawa if (ocb == NULL) 1803111704Ssimokawa goto done; 1804103285Sikob 1805103285Sikob switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){ 1806103285Sikob case ORB_FMT_NOP: 1807103285Sikob break; 1808103285Sikob case ORB_FMT_VED: 1809103285Sikob break; 1810103285Sikob case ORB_FMT_STD: 1811111819Ssimokawa switch(ocb->flags) { 1812103285Sikob case OCB_ACT_MGM: 1813103285Sikob orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK; 1814120842Ssimokawa reset_agent = 0; 1815103285Sikob switch(orb_fun) { 1816103285Sikob case ORB_FUN_LGI: 1817113584Ssimokawa fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD); 1818113584Ssimokawa login_res = sdev->login; 1819103285Sikob login_res->len = ntohs(login_res->len); 1820103285Sikob login_res->id = ntohs(login_res->id); 1821103285Sikob login_res->cmd_hi = ntohs(login_res->cmd_hi); 1822103285Sikob login_res->cmd_lo = ntohl(login_res->cmd_lo); 1823103285Sikob if (status_valid) { 1824103285SikobSBP_DEBUG(0) 1825188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1826188756Ssbruno "%s:%s login: len %d, ID %d, cmd %08x%08x, recon_hold %d\n", 1827188756Ssbruno __func__, sdev->bustgtlun, 1828188756Ssbruno login_res->len, login_res->id, 1829188756Ssbruno login_res->cmd_hi, login_res->cmd_lo, 1830188756Ssbruno ntohs(login_res->recon_hold)); 1831103285SikobEND_DEBUG 1832103285Sikob sbp_busy_timeout(sdev); 1833103285Sikob } else { 1834110336Ssimokawa /* forgot logout? */ 1835188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1836188756Ssbruno "%s:%s login failed\n", 1837188756Ssbruno __func__, sdev->bustgtlun); 1838103285Sikob sdev->status = SBP_DEV_RESET; 1839103285Sikob } 1840103285Sikob break; 1841103285Sikob case ORB_FUN_RCN: 1842113584Ssimokawa login_res = sdev->login; 1843103285Sikob if (status_valid) { 1844103285SikobSBP_DEBUG(0) 1845188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1846188756Ssbruno "%s:%s reconnect: len %d, ID %d, cmd %08x%08x\n", 1847188756Ssbruno __func__, sdev->bustgtlun, 1848188756Ssbruno login_res->len, login_res->id, 1849188756Ssbruno login_res->cmd_hi, login_res->cmd_lo); 1850103285SikobEND_DEBUG 1851111615Ssimokawa if (sdev->status == SBP_DEV_ATTACHED) 1852111615Ssimokawa sbp_scan_dev(sdev); 1853111615Ssimokawa else 1854111615Ssimokawa sbp_agent_reset(sdev); 1855103285Sikob } else { 1856103285Sikob /* reconnection hold time exceed? */ 1857110336SsimokawaSBP_DEBUG(0) 1858188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1859188756Ssbruno "%s:%s reconnect failed\n", 1860188756Ssbruno __func__, sdev->bustgtlun); 1861110336SsimokawaEND_DEBUG 1862111615Ssimokawa sbp_login(sdev); 1863103285Sikob } 1864103285Sikob break; 1865103285Sikob case ORB_FUN_LGO: 1866103285Sikob sdev->status = SBP_DEV_RESET; 1867103285Sikob break; 1868110336Ssimokawa case ORB_FUN_RST: 1869110336Ssimokawa sbp_busy_timeout(sdev); 1870110336Ssimokawa break; 1871103285Sikob case ORB_FUN_LUR: 1872103285Sikob case ORB_FUN_ATA: 1873103285Sikob case ORB_FUN_ATS: 1874110336Ssimokawa sbp_agent_reset(sdev); 1875103285Sikob break; 1876103285Sikob default: 1877188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 1878188756Ssbruno "%s:%s unknown function %d\n", 1879188756Ssbruno __func__, sdev->bustgtlun, orb_fun); 1880103285Sikob break; 1881103285Sikob } 1882111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL); 1883103285Sikob break; 1884103285Sikob case OCB_ACT_CMD: 1885114732Ssimokawa sdev->timeout = 0; 1886103285Sikob if(ocb->ccb != NULL){ 1887103285Sikob union ccb *ccb; 1888188756Ssbruno 1889103285Sikob ccb = ocb->ccb; 1890103285Sikob if(sbp_status->len > 1){ 1891103285Sikob sbp_scsi_status(sbp_status, ocb); 1892103285Sikob }else{ 1893103285Sikob if(sbp_status->resp != ORB_RES_CMPL){ 1894103285Sikob ccb->ccb_h.status = CAM_REQ_CMP_ERR; 1895103285Sikob }else{ 1896103285Sikob ccb->ccb_h.status = CAM_REQ_CMP; 1897103285Sikob } 1898103285Sikob } 1899103285Sikob /* fix up inq data */ 1900103285Sikob if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY) 1901103285Sikob sbp_fix_inq_data(ocb); 1902170374Ssimokawa SBP_LOCK(sbp); 1903103285Sikob xpt_done(ccb); 1904170374Ssimokawa SBP_UNLOCK(sbp); 1905103285Sikob } 1906103285Sikob break; 1907103285Sikob default: 1908103285Sikob break; 1909103285Sikob } 1910103285Sikob } 1911103285Sikob 1912127468Ssimokawa if (!use_doorbell) 1913127468Ssimokawa sbp_free_ocb(sdev, ocb); 1914111704Ssimokawadone: 1915111704Ssimokawa if (reset_agent) 1916111704Ssimokawa sbp_agent_reset(sdev); 1917103285Sikob 1918113584Ssimokawadone0: 1919120660Ssimokawa xfer->recv.pay_len = SBP_RECV_LEN; 1920103285Sikob/* The received packet is usually small enough to be stored within 1921103285Sikob * the buffer. In that case, the controller return ack_complete and 1922103285Sikob * no respose is necessary. 1923103285Sikob * 1924103285Sikob * XXX fwohci.c and firewire.c should inform event_code such as 1925103285Sikob * ack_complete or ack_pending to upper driver. 1926103285Sikob */ 1927103285Sikob#if NEED_RESPONSE 1928103285Sikob xfer->send.off = 0; 1929103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 1930103285Sikob sfp->mode.wres.dst = rfp->mode.wreqb.src; 1931113584Ssimokawa xfer->dst = sfp->mode.wres.dst; 1932103285Sikob xfer->spd = min(sdev->target->fwdev->speed, max_speed); 1933167632Ssimokawa xfer->hand = sbp_loginres_callback; 1934103285Sikob 1935103285Sikob sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt; 1936103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 1937103285Sikob sfp->mode.wres.rtcode = 0; 1938103285Sikob sfp->mode.wres.pri = 0; 1939103285Sikob 1940103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1941103285Sikob#else 1942113584Ssimokawa /* recycle */ 1943170807Ssimokawa /* we don't need a lock here because bottom half is serialized */ 1944113584Ssimokawa STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link); 1945103285Sikob#endif 1946103285Sikob 1947103285Sikob return; 1948103285Sikob 1949103285Sikob} 1950103285Sikob 1951103285Sikobstatic void 1952103285Sikobsbp_recv(struct fw_xfer *xfer) 1953103285Sikob{ 1954103285Sikob int s; 1955103285Sikob 1956103285Sikob s = splcam(); 1957103285Sikob sbp_recv1(xfer); 1958103285Sikob splx(s); 1959103285Sikob} 1960103285Sikob/* 1961103285Sikob * sbp_attach() 1962103285Sikob */ 1963103285Sikobstatic int 1964103285Sikobsbp_attach(device_t dev) 1965103285Sikob{ 1966103285Sikob struct sbp_softc *sbp; 1967103285Sikob struct cam_devq *devq; 1968170374Ssimokawa struct firewire_comm *fc; 1969103285Sikob int i, s, error; 1970103285Sikob 1971167624Ssimokawa if (DFLTPHYS > SBP_MAXPHYS) 1972167624Ssimokawa device_printf(dev, "Warning, DFLTPHYS(%dKB) is larger than " 1973167624Ssimokawa "SBP_MAXPHYS(%dKB).\n", DFLTPHYS / 1024, 1974167624Ssimokawa SBP_MAXPHYS / 1024); 1975167624Ssimokawa 1976170400Ssimokawa if (!firewire_phydma_enable) 1977170400Ssimokawa device_printf(dev, "Warning, hw.firewire.phydma_enable must be 1 " 1978170400Ssimokawa "for SBP over FireWire.\n"); 1979103285SikobSBP_DEBUG(0) 1980111199Ssimokawa printf("sbp_attach (cold=%d)\n", cold); 1981103285SikobEND_DEBUG 1982103285Sikob 1983111199Ssimokawa if (cold) 1984111199Ssimokawa sbp_cold ++; 1985103285Sikob sbp = ((struct sbp_softc *)device_get_softc(dev)); 1986103285Sikob bzero(sbp, sizeof(struct sbp_softc)); 1987103285Sikob sbp->fd.dev = dev; 1988170374Ssimokawa sbp->fd.fc = fc = device_get_ivars(dev); 1989170374Ssimokawa mtx_init(&sbp->mtx, "sbp", NULL, MTX_DEF); 1990124251Ssimokawa 1991124251Ssimokawa if (max_speed < 0) 1992170374Ssimokawa max_speed = fc->speed; 1993124251Ssimokawa 1994170374Ssimokawa error = bus_dma_tag_create(/*parent*/fc->dmat, 1995113584Ssimokawa /* XXX shoud be 4 for sane backend? */ 1996113584Ssimokawa /*alignment*/1, 1997103285Sikob /*boundary*/0, 1998103285Sikob /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 1999103285Sikob /*highaddr*/BUS_SPACE_MAXADDR, 2000103285Sikob /*filter*/NULL, /*filterarg*/NULL, 2001103285Sikob /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX, 2002113474Ssimokawa /*maxsegsz*/SBP_SEG_MAX, 2003103285Sikob /*flags*/BUS_DMA_ALLOCNOW, 2004127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 501102 2005117126Sscottl /*lockfunc*/busdma_lock_mutex, 2006170374Ssimokawa /*lockarg*/&sbp->mtx, 2007117228Ssimokawa#endif 2008117228Ssimokawa &sbp->dmat); 2009103285Sikob if (error != 0) { 2010103285Sikob printf("sbp_attach: Could not allocate DMA tag " 2011103285Sikob "- error %d\n", error); 2012103285Sikob return (ENOMEM); 2013103285Sikob } 2014103285Sikob 2015103285Sikob devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB); 2016103285Sikob if (devq == NULL) 2017103285Sikob return (ENXIO); 2018103285Sikob 2019103285Sikob for( i = 0 ; i < SBP_NUM_TARGETS ; i++){ 2020103285Sikob sbp->targets[i].fwdev = NULL; 2021103285Sikob sbp->targets[i].luns = NULL; 2022103285Sikob } 2023103285Sikob 2024103285Sikob sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp, 2025103285Sikob device_get_unit(dev), 2026170374Ssimokawa &sbp->mtx, 2027111615Ssimokawa /*untagged*/ 1, 2028122528Ssimokawa /*tagged*/ SBP_QUEUE_LEN - 1, 2029111615Ssimokawa devq); 2030103285Sikob 2031103285Sikob if (sbp->sim == NULL) { 2032103285Sikob cam_simq_free(devq); 2033103285Sikob return (ENXIO); 2034103285Sikob } 2035103285Sikob 2036170374Ssimokawa SBP_LOCK(sbp); 2037170872Sscottl if (xpt_bus_register(sbp->sim, dev, /*bus*/0) != CAM_SUCCESS) 2038111615Ssimokawa goto fail; 2039103285Sikob 2040111615Ssimokawa if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim), 2041120660Ssimokawa CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 2042120660Ssimokawa xpt_bus_deregister(cam_sim_path(sbp->sim)); 2043111615Ssimokawa goto fail; 2044120660Ssimokawa } 2045170374Ssimokawa SBP_UNLOCK(sbp); 2046111615Ssimokawa 2047103285Sikob /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */ 2048120660Ssimokawa sbp->fwb.start = ((u_int64_t)SBP_BIND_HI << 32) | SBP_DEV2ADDR(0, 0); 2049120660Ssimokawa sbp->fwb.end = sbp->fwb.start + 0xffff; 2050113584Ssimokawa /* pre-allocate xfer */ 2051113584Ssimokawa STAILQ_INIT(&sbp->fwb.xferlist); 2052169130Ssimokawa fw_xferlist_add(&sbp->fwb.xferlist, M_SBP, 2053169130Ssimokawa /*send*/ 0, /*recv*/ SBP_RECV_LEN, SBP_NUM_OCB/2, 2054170374Ssimokawa fc, (void *)sbp, sbp_recv); 2055103285Sikob 2056170374Ssimokawa fw_bindadd(fc, &sbp->fwb); 2057170374Ssimokawa 2058113584Ssimokawa sbp->fd.post_busreset = sbp_post_busreset; 2059103285Sikob sbp->fd.post_explore = sbp_post_explore; 2060103285Sikob 2061170374Ssimokawa if (fc->status != -1) { 2062111199Ssimokawa s = splfw(); 2063122387Ssimokawa sbp_post_busreset((void *)sbp); 2064111199Ssimokawa sbp_post_explore((void *)sbp); 2065111199Ssimokawa splx(s); 2066111199Ssimokawa } 2067170374Ssimokawa SBP_LOCK(sbp); 2068122387Ssimokawa xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL); 2069170374Ssimokawa SBP_UNLOCK(sbp); 2070111199Ssimokawa 2071103285Sikob return (0); 2072111615Ssimokawafail: 2073170374Ssimokawa SBP_UNLOCK(sbp); 2074111615Ssimokawa cam_sim_free(sbp->sim, /*free_devq*/TRUE); 2075111615Ssimokawa return (ENXIO); 2076103285Sikob} 2077103285Sikob 2078103285Sikobstatic int 2079110145Ssimokawasbp_logout_all(struct sbp_softc *sbp) 2080110145Ssimokawa{ 2081110145Ssimokawa struct sbp_target *target; 2082110145Ssimokawa struct sbp_dev *sdev; 2083110145Ssimokawa int i, j; 2084110145Ssimokawa 2085110145SsimokawaSBP_DEBUG(0) 2086110145Ssimokawa printf("sbp_logout_all\n"); 2087110145SsimokawaEND_DEBUG 2088110145Ssimokawa for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) { 2089110145Ssimokawa target = &sbp->targets[i]; 2090110145Ssimokawa if (target->luns == NULL) 2091110145Ssimokawa continue; 2092110145Ssimokawa for (j = 0; j < target->num_lun; j++) { 2093121185Ssimokawa sdev = target->luns[j]; 2094121185Ssimokawa if (sdev == NULL) 2095121185Ssimokawa continue; 2096113584Ssimokawa callout_stop(&sdev->login_callout); 2097110336Ssimokawa if (sdev->status >= SBP_DEV_TOATTACH && 2098110336Ssimokawa sdev->status <= SBP_DEV_ATTACHED) 2099111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL); 2100110145Ssimokawa } 2101110145Ssimokawa } 2102113584Ssimokawa 2103110145Ssimokawa return 0; 2104110145Ssimokawa} 2105110145Ssimokawa 2106110145Ssimokawastatic int 2107110145Ssimokawasbp_shutdown(device_t dev) 2108110145Ssimokawa{ 2109110145Ssimokawa struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev)); 2110110145Ssimokawa 2111110145Ssimokawa sbp_logout_all(sbp); 2112110145Ssimokawa return (0); 2113110145Ssimokawa} 2114110145Ssimokawa 2115121185Ssimokawastatic void 2116121185Ssimokawasbp_free_sdev(struct sbp_dev *sdev) 2117121185Ssimokawa{ 2118121185Ssimokawa int i; 2119121185Ssimokawa 2120121185Ssimokawa if (sdev == NULL) 2121121185Ssimokawa return; 2122121185Ssimokawa for (i = 0; i < SBP_QUEUE_LEN; i++) 2123121185Ssimokawa bus_dmamap_destroy(sdev->target->sbp->dmat, 2124121185Ssimokawa sdev->ocb[i].dmamap); 2125121185Ssimokawa fwdma_free(sdev->target->sbp->fd.fc, &sdev->dma); 2126121185Ssimokawa free(sdev, M_SBP); 2127187993Ssbruno sdev = NULL; 2128121185Ssimokawa} 2129121185Ssimokawa 2130121185Ssimokawastatic void 2131121185Ssimokawasbp_free_target(struct sbp_target *target) 2132121185Ssimokawa{ 2133121185Ssimokawa struct sbp_softc *sbp; 2134121185Ssimokawa struct fw_xfer *xfer, *next; 2135121185Ssimokawa int i; 2136121185Ssimokawa 2137121185Ssimokawa if (target->luns == NULL) 2138121185Ssimokawa return; 2139121185Ssimokawa callout_stop(&target->mgm_ocb_timeout); 2140121185Ssimokawa sbp = target->sbp; 2141121185Ssimokawa for (i = 0; i < target->num_lun; i++) 2142121185Ssimokawa sbp_free_sdev(target->luns[i]); 2143121185Ssimokawa 2144121185Ssimokawa for (xfer = STAILQ_FIRST(&target->xferlist); 2145121185Ssimokawa xfer != NULL; xfer = next) { 2146121185Ssimokawa next = STAILQ_NEXT(xfer, link); 2147121185Ssimokawa fw_xfer_free_buf(xfer); 2148121185Ssimokawa } 2149121185Ssimokawa STAILQ_INIT(&target->xferlist); 2150121185Ssimokawa free(target->luns, M_SBP); 2151121185Ssimokawa target->num_lun = 0;; 2152121185Ssimokawa target->luns = NULL; 2153121185Ssimokawa target->fwdev = NULL; 2154121185Ssimokawa} 2155121185Ssimokawa 2156110145Ssimokawastatic int 2157103285Sikobsbp_detach(device_t dev) 2158103285Sikob{ 2159103285Sikob struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev)); 2160103285Sikob struct firewire_comm *fc = sbp->fd.fc; 2161121185Ssimokawa int i; 2162103285Sikob 2163103285SikobSBP_DEBUG(0) 2164103285Sikob printf("sbp_detach\n"); 2165103285SikobEND_DEBUG 2166113584Ssimokawa 2167103285Sikob for (i = 0; i < SBP_NUM_TARGETS; i ++) 2168110145Ssimokawa sbp_cam_detach_target(&sbp->targets[i]); 2169170374Ssimokawa 2170170374Ssimokawa SBP_LOCK(sbp); 2171120660Ssimokawa xpt_async(AC_LOST_DEVICE, sbp->path, NULL); 2172111615Ssimokawa xpt_free_path(sbp->path); 2173103285Sikob xpt_bus_deregister(cam_sim_path(sbp->sim)); 2174170378Sgallatin cam_sim_free(sbp->sim, /*free_devq*/ TRUE); 2175170374Ssimokawa SBP_UNLOCK(sbp); 2176110145Ssimokawa 2177110145Ssimokawa sbp_logout_all(sbp); 2178113584Ssimokawa 2179110145Ssimokawa /* XXX wait for logout completion */ 2180167086Sjhb pause("sbpdtc", hz/2); 2181110145Ssimokawa 2182121185Ssimokawa for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) 2183121185Ssimokawa sbp_free_target(&sbp->targets[i]); 2184113584Ssimokawa 2185110145Ssimokawa fw_bindremove(fc, &sbp->fwb); 2186169130Ssimokawa fw_xferlist_remove(&sbp->fwb.xferlist); 2187113584Ssimokawa 2188103285Sikob bus_dma_tag_destroy(sbp->dmat); 2189170374Ssimokawa mtx_destroy(&sbp->mtx); 2190110145Ssimokawa 2191103285Sikob return (0); 2192103285Sikob} 2193103285Sikob 2194103285Sikobstatic void 2195121185Ssimokawasbp_cam_detach_sdev(struct sbp_dev *sdev) 2196121185Ssimokawa{ 2197121185Ssimokawa if (sdev == NULL) 2198121185Ssimokawa return; 2199121185Ssimokawa if (sdev->status == SBP_DEV_DEAD) 2200121185Ssimokawa return; 2201121185Ssimokawa if (sdev->status == SBP_DEV_RESET) 2202121185Ssimokawa return; 2203170374Ssimokawa sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE); 2204121185Ssimokawa if (sdev->path) { 2205170374Ssimokawa SBP_LOCK(sdev->target->sbp); 2206121185Ssimokawa xpt_release_devq(sdev->path, 2207121185Ssimokawa sdev->freeze, TRUE); 2208121185Ssimokawa sdev->freeze = 0; 2209121185Ssimokawa xpt_async(AC_LOST_DEVICE, sdev->path, NULL); 2210121185Ssimokawa xpt_free_path(sdev->path); 2211121185Ssimokawa sdev->path = NULL; 2212170374Ssimokawa SBP_UNLOCK(sdev->target->sbp); 2213121185Ssimokawa } 2214121185Ssimokawa} 2215121185Ssimokawa 2216121185Ssimokawastatic void 2217110145Ssimokawasbp_cam_detach_target(struct sbp_target *target) 2218103285Sikob{ 2219103285Sikob int i; 2220103285Sikob 2221103285Sikob if (target->luns != NULL) { 2222108529SsimokawaSBP_DEBUG(0) 2223103285Sikob printf("sbp_detach_target %d\n", target->target_id); 2224108529SsimokawaEND_DEBUG 2225111615Ssimokawa callout_stop(&target->scan_callout); 2226121185Ssimokawa for (i = 0; i < target->num_lun; i++) 2227121185Ssimokawa sbp_cam_detach_sdev(target->luns[i]); 2228103285Sikob } 2229103285Sikob} 2230103285Sikob 2231103285Sikobstatic void 2232114732Ssimokawasbp_target_reset(struct sbp_dev *sdev, int method) 2233114732Ssimokawa{ 2234114732Ssimokawa int i; 2235114732Ssimokawa struct sbp_target *target = sdev->target; 2236114732Ssimokawa struct sbp_dev *tsdev; 2237114732Ssimokawa 2238114732Ssimokawa for (i = 0; i < target->num_lun; i++) { 2239121185Ssimokawa tsdev = target->luns[i]; 2240121185Ssimokawa if (tsdev == NULL) 2241121185Ssimokawa continue; 2242114732Ssimokawa if (tsdev->status == SBP_DEV_DEAD) 2243114732Ssimokawa continue; 2244114732Ssimokawa if (tsdev->status == SBP_DEV_RESET) 2245114732Ssimokawa continue; 2246170374Ssimokawa SBP_LOCK(target->sbp); 2247114732Ssimokawa xpt_freeze_devq(tsdev->path, 1); 2248114732Ssimokawa tsdev->freeze ++; 2249170374Ssimokawa SBP_UNLOCK(target->sbp); 2250114732Ssimokawa sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT); 2251114732Ssimokawa if (method == 2) 2252114732Ssimokawa tsdev->status = SBP_DEV_LOGIN; 2253114732Ssimokawa } 2254114732Ssimokawa switch(method) { 2255114732Ssimokawa case 1: 2256114732Ssimokawa printf("target reset\n"); 2257114732Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RST, NULL); 2258114732Ssimokawa break; 2259114732Ssimokawa case 2: 2260114732Ssimokawa printf("reset start\n"); 2261114732Ssimokawa sbp_reset_start(sdev); 2262114732Ssimokawa break; 2263114732Ssimokawa } 2264114732Ssimokawa 2265114732Ssimokawa} 2266114732Ssimokawa 2267114732Ssimokawastatic void 2268114732Ssimokawasbp_mgm_timeout(void *arg) 2269114732Ssimokawa{ 2270114732Ssimokawa struct sbp_ocb *ocb = (struct sbp_ocb *)arg; 2271114732Ssimokawa struct sbp_dev *sdev = ocb->sdev; 2272114732Ssimokawa struct sbp_target *target = sdev->target; 2273114732Ssimokawa 2274188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 2275188756Ssbruno "%s:%s request timeout(mgm orb:0x%08x)\n", 2276188756Ssbruno __func__, sdev->bustgtlun, (uint32_t)ocb->bus_addr); 2277114732Ssimokawa target->mgm_ocb_cur = NULL; 2278114732Ssimokawa sbp_free_ocb(sdev, ocb); 2279114732Ssimokawa#if 0 2280114732Ssimokawa /* XXX */ 2281121185Ssimokawa printf("run next request\n"); 2282114732Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL); 2283114732Ssimokawa#endif 2284188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 2285188756Ssbruno "%s:%s reset start\n", 2286188756Ssbruno __func__, sdev->bustgtlun); 2287114732Ssimokawa sbp_reset_start(sdev); 2288114732Ssimokawa} 2289114732Ssimokawa 2290114732Ssimokawastatic void 2291103285Sikobsbp_timeout(void *arg) 2292103285Sikob{ 2293103285Sikob struct sbp_ocb *ocb = (struct sbp_ocb *)arg; 2294103285Sikob struct sbp_dev *sdev = ocb->sdev; 2295103285Sikob 2296188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 2297188756Ssbruno "%s:%s request timeout(cmd orb:0x%08x) ... ", 2298188756Ssbruno __func__, sdev->bustgtlun, (uint32_t)ocb->bus_addr); 2299103285Sikob 2300114732Ssimokawa sdev->timeout ++; 2301114732Ssimokawa switch(sdev->timeout) { 2302114732Ssimokawa case 1: 2303110336Ssimokawa printf("agent reset\n"); 2304170374Ssimokawa SBP_LOCK(sdev->target->sbp); 2305114732Ssimokawa xpt_freeze_devq(sdev->path, 1); 2306114732Ssimokawa sdev->freeze ++; 2307170374Ssimokawa SBP_UNLOCK(sdev->target->sbp); 2308114732Ssimokawa sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT); 2309110336Ssimokawa sbp_agent_reset(sdev); 2310114732Ssimokawa break; 2311114732Ssimokawa case 2: 2312114732Ssimokawa case 3: 2313114732Ssimokawa sbp_target_reset(sdev, sdev->timeout - 1); 2314114732Ssimokawa break; 2315114732Ssimokawa#if 0 2316114732Ssimokawa default: 2317114732Ssimokawa /* XXX give up */ 2318114732Ssimokawa sbp_cam_detach_target(target); 2319114732Ssimokawa if (target->luns != NULL) 2320114732Ssimokawa free(target->luns, M_SBP); 2321114732Ssimokawa target->num_lun = 0;; 2322114732Ssimokawa target->luns = NULL; 2323114732Ssimokawa target->fwdev = NULL; 2324114732Ssimokawa#endif 2325110336Ssimokawa } 2326103285Sikob} 2327103285Sikob 2328103285Sikobstatic void 2329103285Sikobsbp_action1(struct cam_sim *sim, union ccb *ccb) 2330103285Sikob{ 2331103285Sikob 2332103285Sikob struct sbp_softc *sbp = (struct sbp_softc *)sim->softc; 2333103285Sikob struct sbp_target *target = NULL; 2334103285Sikob struct sbp_dev *sdev = NULL; 2335103285Sikob 2336103285Sikob /* target:lun -> sdev mapping */ 2337103285Sikob if (sbp != NULL 2338103285Sikob && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD 2339103285Sikob && ccb->ccb_h.target_id < SBP_NUM_TARGETS) { 2340103285Sikob target = &sbp->targets[ccb->ccb_h.target_id]; 2341103285Sikob if (target->fwdev != NULL 2342103285Sikob && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD 2343103285Sikob && ccb->ccb_h.target_lun < target->num_lun) { 2344121185Ssimokawa sdev = target->luns[ccb->ccb_h.target_lun]; 2345121185Ssimokawa if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED && 2346103285Sikob sdev->status != SBP_DEV_PROBE) 2347103285Sikob sdev = NULL; 2348103285Sikob } 2349103285Sikob } 2350103285Sikob 2351103285SikobSBP_DEBUG(1) 2352103285Sikob if (sdev == NULL) 2353103285Sikob printf("invalid target %d lun %d\n", 2354103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2355103285SikobEND_DEBUG 2356103285Sikob 2357103285Sikob switch (ccb->ccb_h.func_code) { 2358103285Sikob case XPT_SCSI_IO: 2359103285Sikob case XPT_RESET_DEV: 2360103285Sikob case XPT_GET_TRAN_SETTINGS: 2361103285Sikob case XPT_SET_TRAN_SETTINGS: 2362103285Sikob case XPT_CALC_GEOMETRY: 2363103285Sikob if (sdev == NULL) { 2364103285SikobSBP_DEBUG(1) 2365103285Sikob printf("%s:%d:%d:func_code 0x%04x: " 2366103285Sikob "Invalid target (target needed)\n", 2367103285Sikob device_get_nameunit(sbp->fd.dev), 2368103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2369103285Sikob ccb->ccb_h.func_code); 2370103285SikobEND_DEBUG 2371103285Sikob 2372108276Ssimokawa ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2373103285Sikob xpt_done(ccb); 2374103285Sikob return; 2375103285Sikob } 2376103285Sikob break; 2377103285Sikob case XPT_PATH_INQ: 2378103285Sikob case XPT_NOOP: 2379103285Sikob /* The opcodes sometimes aimed at a target (sc is valid), 2380103285Sikob * sometimes aimed at the SIM (sc is invalid and target is 2381103285Sikob * CAM_TARGET_WILDCARD) 2382103285Sikob */ 2383103285Sikob if (sbp == NULL && 2384103285Sikob ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) { 2385103285SikobSBP_DEBUG(0) 2386103285Sikob printf("%s:%d:%d func_code 0x%04x: " 2387103285Sikob "Invalid target (no wildcard)\n", 2388103285Sikob device_get_nameunit(sbp->fd.dev), 2389103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2390103285Sikob ccb->ccb_h.func_code); 2391103285SikobEND_DEBUG 2392108276Ssimokawa ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2393103285Sikob xpt_done(ccb); 2394103285Sikob return; 2395103285Sikob } 2396103285Sikob break; 2397103285Sikob default: 2398103285Sikob /* XXX Hm, we should check the input parameters */ 2399103285Sikob break; 2400103285Sikob } 2401103285Sikob 2402103285Sikob switch (ccb->ccb_h.func_code) { 2403103285Sikob case XPT_SCSI_IO: 2404103285Sikob { 2405103285Sikob struct ccb_scsiio *csio; 2406103285Sikob struct sbp_ocb *ocb; 2407113584Ssimokawa int speed; 2408106506Ssimokawa void *cdb; 2409103285Sikob 2410103285Sikob csio = &ccb->csio; 2411170374Ssimokawa mtx_assert(sim->mtx, MA_OWNED); 2412103285Sikob 2413121185SsimokawaSBP_DEBUG(2) 2414103285Sikob printf("%s:%d:%d XPT_SCSI_IO: " 2415103285Sikob "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" 2416103285Sikob ", flags: 0x%02x, " 2417103285Sikob "%db cmd/%db data/%db sense\n", 2418103285Sikob device_get_nameunit(sbp->fd.dev), 2419103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2420103285Sikob csio->cdb_io.cdb_bytes[0], 2421103285Sikob csio->cdb_io.cdb_bytes[1], 2422103285Sikob csio->cdb_io.cdb_bytes[2], 2423103285Sikob csio->cdb_io.cdb_bytes[3], 2424103285Sikob csio->cdb_io.cdb_bytes[4], 2425103285Sikob csio->cdb_io.cdb_bytes[5], 2426103285Sikob csio->cdb_io.cdb_bytes[6], 2427103285Sikob csio->cdb_io.cdb_bytes[7], 2428103285Sikob csio->cdb_io.cdb_bytes[8], 2429103285Sikob csio->cdb_io.cdb_bytes[9], 2430103285Sikob ccb->ccb_h.flags & CAM_DIR_MASK, 2431103285Sikob csio->cdb_len, csio->dxfer_len, 2432103285Sikob csio->sense_len); 2433103285SikobEND_DEBUG 2434103285Sikob if(sdev == NULL){ 2435103285Sikob ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2436103285Sikob xpt_done(ccb); 2437103285Sikob return; 2438103285Sikob } 2439103285Sikob#if 0 2440103285Sikob /* if we are in probe stage, pass only probe commands */ 2441103285Sikob if (sdev->status == SBP_DEV_PROBE) { 2442103285Sikob char *name; 2443103285Sikob name = xpt_path_periph(ccb->ccb_h.path)->periph_name; 2444103285Sikob printf("probe stage, periph name: %s\n", name); 2445103285Sikob if (strcmp(name, "probe") != 0) { 2446103285Sikob ccb->ccb_h.status = CAM_REQUEUE_REQ; 2447103285Sikob xpt_done(ccb); 2448103285Sikob return; 2449103285Sikob } 2450103285Sikob } 2451103285Sikob#endif 2452122528Ssimokawa if ((ocb = sbp_get_ocb(sdev)) == NULL) { 2453170807Ssimokawa ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 2454127468Ssimokawa if (sdev->freeze == 0) { 2455170374Ssimokawa SBP_LOCK(sdev->target->sbp); 2456127468Ssimokawa xpt_freeze_devq(sdev->path, 1); 2457127468Ssimokawa sdev->freeze ++; 2458170374Ssimokawa SBP_UNLOCK(sdev->target->sbp); 2459127468Ssimokawa } 2460122528Ssimokawa xpt_done(ccb); 2461103285Sikob return; 2462122528Ssimokawa } 2463113584Ssimokawa 2464103285Sikob ocb->flags = OCB_ACT_CMD; 2465103285Sikob ocb->sdev = sdev; 2466103285Sikob ocb->ccb = ccb; 2467103285Sikob ccb->ccb_h.ccb_sdev_ptr = sdev; 2468103285Sikob ocb->orb[0] = htonl(1 << 31); 2469103285Sikob ocb->orb[1] = 0; 2470103285Sikob ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) ); 2471113584Ssimokawa ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET); 2472103285Sikob speed = min(target->fwdev->speed, max_speed); 2473103285Sikob ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed) 2474103285Sikob | ORB_CMD_MAXP(speed + 7)); 2475103285Sikob if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){ 2476103285Sikob ocb->orb[4] |= htonl(ORB_CMD_IN); 2477103285Sikob } 2478103285Sikob 2479103285Sikob if (csio->ccb_h.flags & CAM_SCATTER_VALID) 2480103285Sikob printf("sbp: CAM_SCATTER_VALID\n"); 2481103285Sikob if (csio->ccb_h.flags & CAM_DATA_PHYS) 2482103285Sikob printf("sbp: CAM_DATA_PHYS\n"); 2483103285Sikob 2484106506Ssimokawa if (csio->ccb_h.flags & CAM_CDB_POINTER) 2485106506Ssimokawa cdb = (void *)csio->cdb_io.cdb_ptr; 2486106506Ssimokawa else 2487106506Ssimokawa cdb = (void *)&csio->cdb_io.cdb_bytes; 2488120660Ssimokawa bcopy(cdb, (void *)&ocb->orb[5], csio->cdb_len); 2489103285Sikob/* 2490103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3])); 2491103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7])); 2492103285Sikob*/ 2493103285Sikob if (ccb->csio.dxfer_len > 0) { 2494113584Ssimokawa int s, error; 2495103285Sikob 2496103285Sikob s = splsoftvm(); 2497113584Ssimokawa error = bus_dmamap_load(/*dma tag*/sbp->dmat, 2498103285Sikob /*dma map*/ocb->dmamap, 2499103285Sikob ccb->csio.data_ptr, 2500103285Sikob ccb->csio.dxfer_len, 2501103285Sikob sbp_execute_ocb, 2502103285Sikob ocb, 2503103285Sikob /*flags*/0); 2504103285Sikob splx(s); 2505113584Ssimokawa if (error) 2506113584Ssimokawa printf("sbp: bus_dmamap_load error %d\n", error); 2507103285Sikob } else 2508103285Sikob sbp_execute_ocb(ocb, NULL, 0, 0); 2509103285Sikob break; 2510103285Sikob } 2511103285Sikob case XPT_CALC_GEOMETRY: 2512103285Sikob { 2513103285Sikob struct ccb_calc_geometry *ccg; 2514127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 501100 2515129585Sdfr uint32_t size_mb; 2516129585Sdfr uint32_t secs_per_cylinder; 2517116429Ssimokawa int extended = 1; 2518116429Ssimokawa#endif 2519116351Snjl 2520103285Sikob ccg = &ccb->ccg; 2521103285Sikob if (ccg->block_size == 0) { 2522103285Sikob printf("sbp_action1: block_size is 0.\n"); 2523103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 2524103285Sikob xpt_done(ccb); 2525103285Sikob break; 2526103285Sikob } 2527103285SikobSBP_DEBUG(1) 2528103285Sikob printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: " 2529127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2530127468Ssimokawa "Volume size = %d\n", 2531127468Ssimokawa#else 2532114540Sjake "Volume size = %jd\n", 2533114927Ssimokawa#endif 2534114927Ssimokawa device_get_nameunit(sbp->fd.dev), 2535114927Ssimokawa cam_sim_path(sbp->sim), 2536103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2537127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000 2538114927Ssimokawa (uintmax_t) 2539114927Ssimokawa#endif 2540114927Ssimokawa ccg->volume_size); 2541103285SikobEND_DEBUG 2542103285Sikob 2543127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 501100 2544116429Ssimokawa size_mb = ccg->volume_size 2545116429Ssimokawa / ((1024L * 1024L) / ccg->block_size); 2546116429Ssimokawa 2547116429Ssimokawa if (size_mb > 1024 && extended) { 2548116429Ssimokawa ccg->heads = 255; 2549116429Ssimokawa ccg->secs_per_track = 63; 2550116429Ssimokawa } else { 2551116429Ssimokawa ccg->heads = 64; 2552116429Ssimokawa ccg->secs_per_track = 32; 2553116429Ssimokawa } 2554116429Ssimokawa secs_per_cylinder = ccg->heads * ccg->secs_per_track; 2555116429Ssimokawa ccg->cylinders = ccg->volume_size / secs_per_cylinder; 2556116429Ssimokawa ccb->ccb_h.status = CAM_REQ_CMP; 2557116429Ssimokawa#else 2558116351Snjl cam_calc_geometry(ccg, /*extended*/1); 2559116429Ssimokawa#endif 2560103285Sikob xpt_done(ccb); 2561103285Sikob break; 2562103285Sikob } 2563103285Sikob case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 2564103285Sikob { 2565103285Sikob 2566103285SikobSBP_DEBUG(1) 2567103285Sikob printf("%s:%d:XPT_RESET_BUS: \n", 2568103285Sikob device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim)); 2569103285SikobEND_DEBUG 2570103285Sikob 2571103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 2572103285Sikob xpt_done(ccb); 2573103285Sikob break; 2574103285Sikob } 2575103285Sikob case XPT_PATH_INQ: /* Path routing inquiry */ 2576103285Sikob { 2577103285Sikob struct ccb_pathinq *cpi = &ccb->cpi; 2578103285Sikob 2579103285SikobSBP_DEBUG(1) 2580103285Sikob printf("%s:%d:%d XPT_PATH_INQ:.\n", 2581103285Sikob device_get_nameunit(sbp->fd.dev), 2582103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2583103285SikobEND_DEBUG 2584103285Sikob cpi->version_num = 1; /* XXX??? */ 2585111615Ssimokawa cpi->hba_inquiry = PI_TAG_ABLE; 2586103285Sikob cpi->target_sprt = 0; 2587118105Snjl cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE; 2588103285Sikob cpi->hba_eng_cnt = 0; 2589103285Sikob cpi->max_target = SBP_NUM_TARGETS - 1; 2590103285Sikob cpi->max_lun = SBP_NUM_LUNS - 1; 2591103285Sikob cpi->initiator_id = SBP_INITIATOR; 2592103285Sikob cpi->bus_id = sim->bus_id; 2593103285Sikob cpi->base_transfer_speed = 400 * 1000 / 8; 2594103285Sikob strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2595103285Sikob strncpy(cpi->hba_vid, "SBP", HBA_IDLEN); 2596103285Sikob strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN); 2597103285Sikob cpi->unit_number = sim->unit_number; 2598163816Smjacob cpi->transport = XPORT_SPI; /* XX should have a FireWire */ 2599163816Smjacob cpi->transport_version = 2; 2600163816Smjacob cpi->protocol = PROTO_SCSI; 2601163816Smjacob cpi->protocol_version = SCSI_REV_2; 2602103285Sikob 2603103285Sikob cpi->ccb_h.status = CAM_REQ_CMP; 2604103285Sikob xpt_done(ccb); 2605103285Sikob break; 2606103285Sikob } 2607103285Sikob case XPT_GET_TRAN_SETTINGS: 2608103285Sikob { 2609103285Sikob struct ccb_trans_settings *cts = &ccb->cts; 2610163816Smjacob struct ccb_trans_settings_scsi *scsi = 2611163816Smjacob &cts->proto_specific.scsi; 2612163816Smjacob struct ccb_trans_settings_spi *spi = 2613163816Smjacob &cts->xport_specific.spi; 2614163816Smjacob 2615163816Smjacob cts->protocol = PROTO_SCSI; 2616163816Smjacob cts->protocol_version = SCSI_REV_2; 2617163816Smjacob cts->transport = XPORT_SPI; /* should have a FireWire */ 2618163816Smjacob cts->transport_version = 2; 2619163816Smjacob spi->valid = CTS_SPI_VALID_DISC; 2620163816Smjacob spi->flags = CTS_SPI_FLAGS_DISC_ENB; 2621163816Smjacob scsi->valid = CTS_SCSI_VALID_TQ; 2622163816Smjacob scsi->flags = CTS_SCSI_FLAGS_TAG_ENB; 2623103285SikobSBP_DEBUG(1) 2624103285Sikob printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n", 2625103285Sikob device_get_nameunit(sbp->fd.dev), 2626103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2627103285SikobEND_DEBUG 2628103285Sikob cts->ccb_h.status = CAM_REQ_CMP; 2629103285Sikob xpt_done(ccb); 2630103285Sikob break; 2631103285Sikob } 2632103285Sikob case XPT_ABORT: 2633103285Sikob ccb->ccb_h.status = CAM_UA_ABORT; 2634103285Sikob xpt_done(ccb); 2635103285Sikob break; 2636111615Ssimokawa case XPT_SET_TRAN_SETTINGS: 2637111615Ssimokawa /* XXX */ 2638103285Sikob default: 2639103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 2640103285Sikob xpt_done(ccb); 2641103285Sikob break; 2642103285Sikob } 2643103285Sikob return; 2644103285Sikob} 2645103285Sikob 2646103285Sikobstatic void 2647103285Sikobsbp_action(struct cam_sim *sim, union ccb *ccb) 2648103285Sikob{ 2649103285Sikob int s; 2650103285Sikob 2651103285Sikob s = splfw(); 2652103285Sikob sbp_action1(sim, ccb); 2653103285Sikob splx(s); 2654103285Sikob} 2655103285Sikob 2656103285Sikobstatic void 2657103285Sikobsbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error) 2658103285Sikob{ 2659103285Sikob int i; 2660103285Sikob struct sbp_ocb *ocb; 2661103285Sikob struct sbp_ocb *prev; 2662105633Ssimokawa bus_dma_segment_t *s; 2663103285Sikob 2664103285Sikob if (error) 2665103285Sikob printf("sbp_execute_ocb: error=%d\n", error); 2666103285Sikob 2667103285Sikob ocb = (struct sbp_ocb *)arg; 2668113584Ssimokawa 2669121185SsimokawaSBP_DEBUG(2) 2670113584Ssimokawa printf("sbp_execute_ocb: seg %d", seg); 2671113584Ssimokawa for (i = 0; i < seg; i++) 2672127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2673127468Ssimokawa printf(", %x:%d", segments[i].ds_addr, segments[i].ds_len); 2674127468Ssimokawa#else 2675113972Ssimokawa printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr, 2676113972Ssimokawa (uintmax_t)segments[i].ds_len); 2677113584Ssimokawa#endif 2678113584Ssimokawa printf("\n"); 2679113584SsimokawaEND_DEBUG 2680113584Ssimokawa 2681103285Sikob if (seg == 1) { 2682103285Sikob /* direct pointer */ 2683113474Ssimokawa s = &segments[0]; 2684113474Ssimokawa if (s->ds_len > SBP_SEG_MAX) 2685113474Ssimokawa panic("ds_len > SBP_SEG_MAX, fix busdma code"); 2686113474Ssimokawa ocb->orb[3] = htonl(s->ds_addr); 2687113474Ssimokawa ocb->orb[4] |= htonl(s->ds_len); 2688103285Sikob } else if(seg > 1) { 2689103285Sikob /* page table */ 2690103285Sikob for (i = 0; i < seg; i++) { 2691105633Ssimokawa s = &segments[i]; 2692108877SsimokawaSBP_DEBUG(0) 2693108877Ssimokawa /* XXX LSI Logic "< 16 byte" bug might be hit */ 2694105633Ssimokawa if (s->ds_len < 16) 2695105633Ssimokawa printf("sbp_execute_ocb: warning, " 2696127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2697127468Ssimokawa "segment length(%d) is less than 16." 2698127468Ssimokawa#else 2699106543Ssimokawa "segment length(%zd) is less than 16." 2700108877Ssimokawa#endif 2701105633Ssimokawa "(seg=%d/%d)\n", s->ds_len, i+1, seg); 2702108877SsimokawaEND_DEBUG 2703113474Ssimokawa if (s->ds_len > SBP_SEG_MAX) 2704113474Ssimokawa panic("ds_len > SBP_SEG_MAX, fix busdma code"); 2705105633Ssimokawa ocb->ind_ptr[i].hi = htonl(s->ds_len << 16); 2706105633Ssimokawa ocb->ind_ptr[i].lo = htonl(s->ds_addr); 2707103285Sikob } 2708103285Sikob ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg); 2709103285Sikob } 2710103285Sikob 2711113584Ssimokawa if (seg > 0) 2712113584Ssimokawa bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap, 2713113584Ssimokawa (ntohl(ocb->orb[4]) & ORB_CMD_IN) ? 2714113584Ssimokawa BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 2715103285Sikob prev = sbp_enqueue_ocb(ocb->sdev, ocb); 2716113584Ssimokawa fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE); 2717127468Ssimokawa if (use_doorbell) { 2718127468Ssimokawa if (prev == NULL) { 2719127468Ssimokawa if (ocb->sdev->last_ocb != NULL) 2720127468Ssimokawa sbp_doorbell(ocb->sdev); 2721127468Ssimokawa else 2722127468Ssimokawa sbp_orb_pointer(ocb->sdev, ocb); 2723127468Ssimokawa } 2724127468Ssimokawa } else { 2725127468Ssimokawa if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) { 2726127468Ssimokawa ocb->sdev->flags &= ~ORB_LINK_DEAD; 2727127468Ssimokawa sbp_orb_pointer(ocb->sdev, ocb); 2728127468Ssimokawa } 2729120842Ssimokawa } 2730103285Sikob} 2731103285Sikob 2732103285Sikobstatic void 2733103285Sikobsbp_poll(struct cam_sim *sim) 2734103285Sikob{ 2735120847Ssimokawa struct sbp_softc *sbp; 2736120847Ssimokawa struct firewire_comm *fc; 2737120847Ssimokawa 2738120847Ssimokawa sbp = (struct sbp_softc *)sim->softc; 2739120847Ssimokawa fc = sbp->fd.fc; 2740120847Ssimokawa 2741120847Ssimokawa fc->poll(fc, 0, -1); 2742120847Ssimokawa 2743103285Sikob return; 2744103285Sikob} 2745120847Ssimokawa 2746103285Sikobstatic struct sbp_ocb * 2747111615Ssimokawasbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status) 2748103285Sikob{ 2749103285Sikob struct sbp_ocb *ocb; 2750103285Sikob struct sbp_ocb *next; 2751103285Sikob int s = splfw(), order = 0; 2752103285Sikob int flags; 2753103285Sikob 2754103285SikobSBP_DEBUG(1) 2755188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 2756127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2757188756Ssbruno "%s:%s 0x%08lx src %d\n", 2758127468Ssimokawa#else 2759188756Ssbruno "%s:%s 0x%08x src %d\n", 2760108712Ssimokawa#endif 2761188756Ssbruno __func__, sdev->bustgtlun, ntohl(sbp_status->orb_lo), sbp_status->src); 2762103285SikobEND_DEBUG 2763170374Ssimokawa SBP_LOCK(sdev->target->sbp); 2764121185Ssimokawa for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) { 2765121185Ssimokawa next = STAILQ_NEXT(ocb, ocb); 2766121185Ssimokawa flags = ocb->flags; 2767111615Ssimokawa if (OCB_MATCH(ocb, sbp_status)) { 2768103285Sikob /* found */ 2769111819Ssimokawa STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb); 2770103285Sikob if (ocb->ccb != NULL) 2771103285Sikob untimeout(sbp_timeout, (caddr_t)ocb, 2772103285Sikob ocb->ccb->ccb_h.timeout_ch); 2773113584Ssimokawa if (ntohl(ocb->orb[4]) & 0xffff) { 2774113584Ssimokawa bus_dmamap_sync(sdev->target->sbp->dmat, 2775113584Ssimokawa ocb->dmamap, 2776113584Ssimokawa (ntohl(ocb->orb[4]) & ORB_CMD_IN) ? 2777113584Ssimokawa BUS_DMASYNC_POSTREAD : 2778113584Ssimokawa BUS_DMASYNC_POSTWRITE); 2779113584Ssimokawa bus_dmamap_unload(sdev->target->sbp->dmat, 2780113584Ssimokawa ocb->dmamap); 2781103285Sikob } 2782127468Ssimokawa if (!use_doorbell) { 2783127468Ssimokawa if (sbp_status->src == SRC_NO_NEXT) { 2784127468Ssimokawa if (next != NULL) 2785127468Ssimokawa sbp_orb_pointer(sdev, next); 2786127468Ssimokawa else if (order > 0) { 2787127468Ssimokawa /* 2788127468Ssimokawa * Unordered execution 2789127468Ssimokawa * We need to send pointer for 2790127468Ssimokawa * next ORB 2791127468Ssimokawa */ 2792127468Ssimokawa sdev->flags |= ORB_LINK_DEAD; 2793127468Ssimokawa } 2794120842Ssimokawa } 2795127468Ssimokawa } else { 2796127468Ssimokawa /* 2797127468Ssimokawa * XXX this is not correct for unordered 2798127468Ssimokawa * execution. 2799127468Ssimokawa */ 2800170807Ssimokawa if (sdev->last_ocb != NULL) { 2801170807Ssimokawa SBP_UNLOCK(sdev->target->sbp); 2802127468Ssimokawa sbp_free_ocb(sdev, sdev->last_ocb); 2803170807Ssimokawa SBP_LOCK(sdev->target->sbp); 2804170807Ssimokawa } 2805127468Ssimokawa sdev->last_ocb = ocb; 2806127468Ssimokawa if (next != NULL && 2807127468Ssimokawa sbp_status->src == SRC_NO_NEXT) 2808127468Ssimokawa sbp_doorbell(sdev); 2809120842Ssimokawa } 2810103285Sikob break; 2811111819Ssimokawa } else 2812111819Ssimokawa order ++; 2813103285Sikob } 2814170374Ssimokawa SBP_UNLOCK(sdev->target->sbp); 2815103285Sikob splx(s); 2816103285SikobSBP_DEBUG(0) 2817103285Sikob if (ocb && order > 0) { 2818188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 2819188756Ssbruno "%s:%s unordered execution order:%d\n", 2820188756Ssbruno __func__, sdev->bustgtlun, order); 2821103285Sikob } 2822103285SikobEND_DEBUG 2823103285Sikob return (ocb); 2824103285Sikob} 2825103285Sikob 2826103285Sikobstatic struct sbp_ocb * 2827103285Sikobsbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb) 2828103285Sikob{ 2829103285Sikob int s = splfw(); 2830127468Ssimokawa struct sbp_ocb *prev, *prev2; 2831103285Sikob 2832170807Ssimokawa mtx_assert(&sdev->target->sbp->mtx, MA_OWNED); 2833121185SsimokawaSBP_DEBUG(1) 2834188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 2835127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2836188756Ssbruno "%s:%s 0x%08x\n", __func__, sdev->bustgtlun, ocb->bus_addr); 2837108712Ssimokawa#else 2838188756Ssbruno "%s:%s 0x%08jx\n", __func__, sdev->bustgtlun, (uintmax_t)ocb->bus_addr); 2839108712Ssimokawa#endif 2840103285SikobEND_DEBUG 2841127468Ssimokawa prev2 = prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb); 2842103285Sikob STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb); 2843103285Sikob 2844103285Sikob if (ocb->ccb != NULL) 2845103285Sikob ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb, 2846103285Sikob (ocb->ccb->ccb_h.timeout * hz) / 1000); 2847103285Sikob 2848127468Ssimokawa if (use_doorbell && prev == NULL) 2849127468Ssimokawa prev2 = sdev->last_ocb; 2850127468Ssimokawa 2851170807Ssimokawa if (prev2 != NULL && (ocb->sdev->flags & ORB_LINK_DEAD) == 0) { 2852170807SsimokawaSBP_DEBUG(1) 2853127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2854127468Ssimokawa printf("linking chain 0x%x -> 0x%x\n", 2855127468Ssimokawa prev2->bus_addr, ocb->bus_addr); 2856127468Ssimokawa#else 2857120842Ssimokawa printf("linking chain 0x%jx -> 0x%jx\n", 2858127468Ssimokawa (uintmax_t)prev2->bus_addr, (uintmax_t)ocb->bus_addr); 2859108712Ssimokawa#endif 2860103285SikobEND_DEBUG 2861170724Ssimokawa /* 2862170724Ssimokawa * Suppress compiler optimization so that orb[1] must be written first. 2863170724Ssimokawa * XXX We may need an explicit memory barrier for other architectures 2864170724Ssimokawa * other than i386/amd64. 2865170724Ssimokawa */ 2866170724Ssimokawa *(volatile uint32_t *)&prev2->orb[1] = htonl(ocb->bus_addr); 2867170724Ssimokawa *(volatile uint32_t *)&prev2->orb[0] = 0; 2868103285Sikob } 2869103285Sikob splx(s); 2870103285Sikob 2871103285Sikob return prev; 2872103285Sikob} 2873103285Sikob 2874103285Sikobstatic struct sbp_ocb * 2875113584Ssimokawasbp_get_ocb(struct sbp_dev *sdev) 2876103285Sikob{ 2877103285Sikob struct sbp_ocb *ocb; 2878103285Sikob int s = splfw(); 2879170374Ssimokawa 2880170374Ssimokawa mtx_assert(&sdev->target->sbp->mtx, MA_OWNED); 2881113584Ssimokawa ocb = STAILQ_FIRST(&sdev->free_ocbs); 2882103285Sikob if (ocb == NULL) { 2883127468Ssimokawa sdev->flags |= ORB_SHORTAGE; 2884103285Sikob printf("ocb shortage!!!\n"); 2885134868Ssimokawa splx(s); 2886103285Sikob return NULL; 2887103285Sikob } 2888113584Ssimokawa STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb); 2889103285Sikob splx(s); 2890103285Sikob ocb->ccb = NULL; 2891103285Sikob return (ocb); 2892103285Sikob} 2893103285Sikob 2894103285Sikobstatic void 2895113584Ssimokawasbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb) 2896103285Sikob{ 2897103285Sikob ocb->flags = 0; 2898103285Sikob ocb->ccb = NULL; 2899170374Ssimokawa 2900170374Ssimokawa SBP_LOCK(sdev->target->sbp); 2901113584Ssimokawa STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb); 2902127468Ssimokawa if ((sdev->flags & ORB_SHORTAGE) != 0) { 2903127468Ssimokawa int count; 2904127468Ssimokawa 2905127468Ssimokawa sdev->flags &= ~ORB_SHORTAGE; 2906127468Ssimokawa count = sdev->freeze; 2907127468Ssimokawa sdev->freeze = 0; 2908127468Ssimokawa xpt_release_devq(sdev->path, count, TRUE); 2909127468Ssimokawa } 2910170374Ssimokawa SBP_UNLOCK(sdev->target->sbp); 2911103285Sikob} 2912103285Sikob 2913103285Sikobstatic void 2914103285Sikobsbp_abort_ocb(struct sbp_ocb *ocb, int status) 2915103285Sikob{ 2916103285Sikob struct sbp_dev *sdev; 2917103285Sikob 2918103285Sikob sdev = ocb->sdev; 2919113584SsimokawaSBP_DEBUG(0) 2920188756Ssbruno device_printf(sdev->target->sbp->fd.dev, 2921127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2922188756Ssbruno "%s:%s 0x%x\n", __func__, sdev->bustgtlun, ocb->bus_addr); 2923127468Ssimokawa#else 2924188756Ssbruno "%s:%s 0x%jx\n", __func__, sdev->bustgtlun, (uintmax_t)ocb->bus_addr); 2925111819Ssimokawa#endif 2926113584SsimokawaEND_DEBUG 2927113584SsimokawaSBP_DEBUG(1) 2928105792Ssimokawa if (ocb->ccb != NULL) 2929105792Ssimokawa sbp_print_scsi_cmd(ocb); 2930103285SikobEND_DEBUG 2931113584Ssimokawa if (ntohl(ocb->orb[4]) & 0xffff) { 2932113584Ssimokawa bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap, 2933113584Ssimokawa (ntohl(ocb->orb[4]) & ORB_CMD_IN) ? 2934113584Ssimokawa BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 2935113584Ssimokawa bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap); 2936113584Ssimokawa } 2937111819Ssimokawa if (ocb->ccb != NULL) { 2938110336Ssimokawa untimeout(sbp_timeout, (caddr_t)ocb, 2939110336Ssimokawa ocb->ccb->ccb_h.timeout_ch); 2940103285Sikob ocb->ccb->ccb_h.status = status; 2941170374Ssimokawa SBP_LOCK(sdev->target->sbp); 2942103285Sikob xpt_done(ocb->ccb); 2943170374Ssimokawa SBP_UNLOCK(sdev->target->sbp); 2944103285Sikob } 2945113584Ssimokawa sbp_free_ocb(sdev, ocb); 2946103285Sikob} 2947103285Sikob 2948103285Sikobstatic void 2949103285Sikobsbp_abort_all_ocbs(struct sbp_dev *sdev, int status) 2950103285Sikob{ 2951103285Sikob int s; 2952105792Ssimokawa struct sbp_ocb *ocb, *next; 2953105792Ssimokawa STAILQ_HEAD(, sbp_ocb) temp; 2954103285Sikob 2955103285Sikob s = splfw(); 2956105792Ssimokawa 2957170807Ssimokawa STAILQ_INIT(&temp); 2958170807Ssimokawa SBP_LOCK(sdev->target->sbp); 2959170807Ssimokawa STAILQ_CONCAT(&temp, &sdev->ocbs); 2960105792Ssimokawa STAILQ_INIT(&sdev->ocbs); 2961170807Ssimokawa SBP_UNLOCK(sdev->target->sbp); 2962170807Ssimokawa 2963105792Ssimokawa for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) { 2964105792Ssimokawa next = STAILQ_NEXT(ocb, ocb); 2965103285Sikob sbp_abort_ocb(ocb, status); 2966103285Sikob } 2967127468Ssimokawa if (sdev->last_ocb != NULL) { 2968127468Ssimokawa sbp_free_ocb(sdev, sdev->last_ocb); 2969127468Ssimokawa sdev->last_ocb = NULL; 2970127468Ssimokawa } 2971105792Ssimokawa 2972103285Sikob splx(s); 2973103285Sikob} 2974103285Sikob 2975103285Sikobstatic devclass_t sbp_devclass; 2976103285Sikob 2977103285Sikobstatic device_method_t sbp_methods[] = { 2978103285Sikob /* device interface */ 2979103285Sikob DEVMETHOD(device_identify, sbp_identify), 2980103285Sikob DEVMETHOD(device_probe, sbp_probe), 2981103285Sikob DEVMETHOD(device_attach, sbp_attach), 2982103285Sikob DEVMETHOD(device_detach, sbp_detach), 2983110145Ssimokawa DEVMETHOD(device_shutdown, sbp_shutdown), 2984103285Sikob 2985103285Sikob { 0, 0 } 2986103285Sikob}; 2987103285Sikob 2988103285Sikobstatic driver_t sbp_driver = { 2989103285Sikob "sbp", 2990103285Sikob sbp_methods, 2991103285Sikob sizeof(struct sbp_softc), 2992103285Sikob}; 2993127468Ssimokawa#ifdef __DragonFly__ 2994127468SsimokawaDECLARE_DUMMY_MODULE(sbp); 2995127468Ssimokawa#endif 2996103285SikobDRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0); 2997103285SikobMODULE_VERSION(sbp, 1); 2998103285SikobMODULE_DEPEND(sbp, firewire, 1, 1, 1); 2999103285SikobMODULE_DEPEND(sbp, cam, 1, 1, 1); 3000