sbp.c revision 139749
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 139749 2005-01-06 01:43:34Z imp $ 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 94113584Ssimokawa#define SBP_DMA_SIZE PAGE_SIZE 95113584Ssimokawa#define SBP_LOGIN_SIZE sizeof(struct sbp_login_res) 96113584Ssimokawa#define SBP_QUEUE_LEN ((SBP_DMA_SIZE - SBP_LOGIN_SIZE) / sizeof(struct sbp_ocb)) 97103285Sikob#define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS) 98113584Ssimokawa 99111615Ssimokawa/* 100111615Ssimokawa * STATUS FIFO addressing 101111615Ssimokawa * bit 102111615Ssimokawa * ----------------------- 103130532Sdfr * 0- 1( 2): 0 (alignment) 104111615Ssimokawa * 2- 7( 6): target 105111615Ssimokawa * 8-15( 8): lun 106120660Ssimokawa * 16-31( 8): reserved 107111615Ssimokawa * 32-47(16): SBP_BIND_HI 108111615Ssimokawa * 48-64(16): bus_id, node_id 109111615Ssimokawa */ 110103285Sikob#define SBP_BIND_HI 0x1 111120660Ssimokawa#define SBP_DEV2ADDR(t, l) \ 112120660Ssimokawa (((u_int64_t)SBP_BIND_HI << 32) \ 113120660Ssimokawa | (((l) & 0xff) << 8) \ 114120660Ssimokawa | (((t) & 0x3f) << 2)) 115103285Sikob#define SBP_ADDR2TRG(a) (((a) >> 2) & 0x3f) 116103285Sikob#define SBP_ADDR2LUN(a) (((a) >> 8) & 0xff) 117120660Ssimokawa#define SBP_INITIATOR 7 118103285Sikob 119103285Sikobstatic char *orb_fun_name[] = { 120120660Ssimokawa ORB_FUN_NAMES 121103285Sikob}; 122103285Sikob 123111203Ssimokawastatic int debug = 0; 124103285Sikobstatic int auto_login = 1; 125124251Ssimokawastatic int max_speed = -1; 126111199Ssimokawastatic int sbp_cold = 1; 127122387Ssimokawastatic int ex_login = 1; 128122387Ssimokawastatic int login_delay = 1000; /* msec */ 129122387Ssimokawastatic int scan_delay = 500; /* msec */ 130127468Ssimokawastatic int use_doorbell = 0; 131127468Ssimokawastatic int sbp_tags = 0; 132103285Sikob 133103285SikobSYSCTL_DECL(_hw_firewire); 134103285SikobSYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem"); 135103285SikobSYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0, 136103285Sikob "SBP debug flag"); 137103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0, 138103285Sikob "SBP perform login automatically"); 139103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0, 140103285Sikob "SBP transfer max speed"); 141121792SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, exclusive_login, CTLFLAG_RW, 142130677Ssimokawa &ex_login, 0, "SBP enable exclusive login"); 143122387SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, login_delay, CTLFLAG_RW, 144122387Ssimokawa &login_delay, 0, "SBP login delay in msec"); 145122387SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, scan_delay, CTLFLAG_RW, 146122387Ssimokawa &scan_delay, 0, "SBP scan delay in msec"); 147127468SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, use_doorbell, CTLFLAG_RW, 148127468Ssimokawa &use_doorbell, 0, "SBP use doorbell request"); 149127468SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, tags, CTLFLAG_RW, &sbp_tags, 0, 150127468Ssimokawa "SBP tagged queuing support"); 151103285Sikob 152122387SsimokawaTUNABLE_INT("hw.firewire.sbp.auto_login", &auto_login); 153122387SsimokawaTUNABLE_INT("hw.firewire.sbp.max_speed", &max_speed); 154122387SsimokawaTUNABLE_INT("hw.firewire.sbp.exclusive_login", &ex_login); 155122387SsimokawaTUNABLE_INT("hw.firewire.sbp.login_delay", &login_delay); 156122387SsimokawaTUNABLE_INT("hw.firewire.sbp.scan_delay", &scan_delay); 157127468SsimokawaTUNABLE_INT("hw.firewire.sbp.use_doorbell", &use_doorbell); 158127468SsimokawaTUNABLE_INT("hw.firewire.sbp.tags", &sbp_tags); 159122387Ssimokawa 160103285Sikob#define NEED_RESPONSE 0 161103285Sikob 162113584Ssimokawa#define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE) 163113584Ssimokawa#ifdef __sparc64__ /* iommu */ 164113584Ssimokawa#define SBP_IND_MAX howmany(MAXPHYS, SBP_SEG_MAX) 165113584Ssimokawa#else 166113584Ssimokawa#define SBP_IND_MAX howmany(MAXPHYS, PAGE_SIZE) 167113584Ssimokawa#endif 168103285Sikobstruct sbp_ocb { 169103285Sikob STAILQ_ENTRY(sbp_ocb) ocb; 170103285Sikob union ccb *ccb; 171113584Ssimokawa bus_addr_t bus_addr; 172129585Sdfr uint32_t orb[8]; 173129585Sdfr#define IND_PTR_OFFSET (8*sizeof(uint32_t)) 174120660Ssimokawa struct ind_ptr ind_ptr[SBP_IND_MAX]; 175103285Sikob struct sbp_dev *sdev; 176113584Ssimokawa int flags; /* XXX should be removed */ 177103285Sikob bus_dmamap_t dmamap; 178103285Sikob}; 179113584Ssimokawa 180103285Sikob#define OCB_ACT_MGM 0 181103285Sikob#define OCB_ACT_CMD 1 182113584Ssimokawa#define OCB_MATCH(o,s) ((o)->bus_addr == ntohl((s)->orb_lo)) 183103285Sikob 184103285Sikobstruct sbp_dev{ 185103285Sikob#define SBP_DEV_RESET 0 /* accept login */ 186114732Ssimokawa#define SBP_DEV_LOGIN 1 /* to login */ 187110336Ssimokawa#if 0 188103285Sikob#define SBP_DEV_RECONN 2 /* to reconnect */ 189110336Ssimokawa#endif 190103285Sikob#define SBP_DEV_TOATTACH 3 /* to attach */ 191103285Sikob#define SBP_DEV_PROBE 4 /* scan lun */ 192103285Sikob#define SBP_DEV_ATTACHED 5 /* in operation */ 193103285Sikob#define SBP_DEV_DEAD 6 /* unavailable unit */ 194103285Sikob#define SBP_DEV_RETRY 7 /* unavailable unit */ 195129585Sdfr uint8_t status:4, 196114732Ssimokawa timeout:4; 197129585Sdfr uint8_t type; 198129585Sdfr uint16_t lun_id; 199129585Sdfr uint16_t freeze; 200121185Ssimokawa#define ORB_LINK_DEAD (1 << 0) 201121185Ssimokawa#define VALID_LUN (1 << 1) 202121185Ssimokawa#define ORB_POINTER_ACTIVE (1 << 2) 203121185Ssimokawa#define ORB_POINTER_NEED (1 << 3) 204127468Ssimokawa#define ORB_DOORBELL_ACTIVE (1 << 4) 205127468Ssimokawa#define ORB_DOORBELL_NEED (1 << 5) 206127468Ssimokawa#define ORB_SHORTAGE (1 << 6) 207129585Sdfr uint16_t flags; 208103285Sikob struct cam_path *path; 209103285Sikob struct sbp_target *target; 210113584Ssimokawa struct fwdma_alloc dma; 211113584Ssimokawa struct sbp_login_res *login; 212111615Ssimokawa struct callout login_callout; 213113584Ssimokawa struct sbp_ocb *ocb; 214103285Sikob STAILQ_HEAD(, sbp_ocb) ocbs; 215113584Ssimokawa STAILQ_HEAD(, sbp_ocb) free_ocbs; 216127468Ssimokawa struct sbp_ocb *last_ocb; 217103285Sikob char vendor[32]; 218103285Sikob char product[32]; 219103285Sikob char revision[10]; 220103285Sikob}; 221103285Sikob 222103285Sikobstruct sbp_target { 223103285Sikob int target_id; 224103285Sikob int num_lun; 225121185Ssimokawa struct sbp_dev **luns; 226103285Sikob struct sbp_softc *sbp; 227103285Sikob struct fw_device *fwdev; 228129585Sdfr uint32_t mgm_hi, mgm_lo; 229111615Ssimokawa struct sbp_ocb *mgm_ocb_cur; 230111615Ssimokawa STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue; 231111615Ssimokawa struct callout mgm_ocb_timeout; 232111615Ssimokawa struct callout scan_callout; 233113584Ssimokawa STAILQ_HEAD(, fw_xfer) xferlist; 234113584Ssimokawa int n_xfer; 235103285Sikob}; 236103285Sikob 237103285Sikobstruct sbp_softc { 238103285Sikob struct firewire_dev_comm fd; 239103285Sikob struct cam_sim *sim; 240111615Ssimokawa struct cam_path *path; 241103285Sikob struct sbp_target targets[SBP_NUM_TARGETS]; 242103285Sikob struct fw_bind fwb; 243103285Sikob bus_dma_tag_t dmat; 244122387Ssimokawa struct timeval last_busreset; 245124145Ssimokawa#define SIMQ_FREEZED 1 246124145Ssimokawa int flags; 247103285Sikob}; 248122387Ssimokawa 249124169Ssimokawastatic void sbp_post_explore (void *); 250124169Ssimokawastatic void sbp_recv (struct fw_xfer *); 251124169Ssimokawastatic void sbp_mgm_callback (struct fw_xfer *); 252121185Ssimokawa#if 0 253124169Ssimokawastatic void sbp_cmd_callback (struct fw_xfer *); 254121185Ssimokawa#endif 255124169Ssimokawastatic void sbp_orb_pointer (struct sbp_dev *, struct sbp_ocb *); 256127468Ssimokawastatic void sbp_doorbell(struct sbp_dev *); 257124169Ssimokawastatic void sbp_execute_ocb (void *, bus_dma_segment_t *, int, int); 258124169Ssimokawastatic void sbp_free_ocb (struct sbp_dev *, struct sbp_ocb *); 259124169Ssimokawastatic void sbp_abort_ocb (struct sbp_ocb *, int); 260124169Ssimokawastatic void sbp_abort_all_ocbs (struct sbp_dev *, int); 261124169Ssimokawastatic struct fw_xfer * sbp_write_cmd (struct sbp_dev *, int, int); 262124169Ssimokawastatic struct sbp_ocb * sbp_get_ocb (struct sbp_dev *); 263124169Ssimokawastatic struct sbp_ocb * sbp_enqueue_ocb (struct sbp_dev *, struct sbp_ocb *); 264124169Ssimokawastatic struct sbp_ocb * sbp_dequeue_ocb (struct sbp_dev *, struct sbp_status *); 265121185Ssimokawastatic void sbp_cam_detach_sdev(struct sbp_dev *); 266121185Ssimokawastatic void sbp_free_sdev(struct sbp_dev *); 267124169Ssimokawastatic void sbp_cam_detach_target (struct sbp_target *); 268124169Ssimokawastatic void sbp_free_target (struct sbp_target *); 269124169Ssimokawastatic void sbp_mgm_timeout (void *arg); 270124169Ssimokawastatic void sbp_timeout (void *arg); 271124169Ssimokawastatic void sbp_mgm_orb (struct sbp_dev *, int, struct sbp_ocb *); 272103285Sikob 273108281SsimokawaMALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire"); 274103285Sikob 275103285Sikob/* cam related functions */ 276103285Sikobstatic void sbp_action(struct cam_sim *sim, union ccb *ccb); 277103285Sikobstatic void sbp_poll(struct cam_sim *sim); 278111615Ssimokawastatic void sbp_cam_scan_lun(struct cam_periph *, union ccb *); 279111615Ssimokawastatic void sbp_cam_scan_target(void *arg); 280103285Sikob 281103285Sikobstatic char *orb_status0[] = { 282103285Sikob /* 0 */ "No additional information to report", 283103285Sikob /* 1 */ "Request type not supported", 284103285Sikob /* 2 */ "Speed not supported", 285103285Sikob /* 3 */ "Page size not supported", 286103285Sikob /* 4 */ "Access denied", 287103285Sikob /* 5 */ "Logical unit not supported", 288103285Sikob /* 6 */ "Maximum payload too small", 289103285Sikob /* 7 */ "Reserved for future standardization", 290103285Sikob /* 8 */ "Resources unavailable", 291103285Sikob /* 9 */ "Function rejected", 292103285Sikob /* A */ "Login ID not recognized", 293103285Sikob /* B */ "Dummy ORB completed", 294103285Sikob /* C */ "Request aborted", 295103285Sikob /* FF */ "Unspecified error" 296103285Sikob#define MAX_ORB_STATUS0 0xd 297103285Sikob}; 298103285Sikob 299103285Sikobstatic char *orb_status1_object[] = { 300103285Sikob /* 0 */ "Operation request block (ORB)", 301103285Sikob /* 1 */ "Data buffer", 302103285Sikob /* 2 */ "Page table", 303103285Sikob /* 3 */ "Unable to specify" 304103285Sikob}; 305103285Sikob 306103285Sikobstatic char *orb_status1_serial_bus_error[] = { 307103285Sikob /* 0 */ "Missing acknowledge", 308103285Sikob /* 1 */ "Reserved; not to be used", 309103285Sikob /* 2 */ "Time-out error", 310103285Sikob /* 3 */ "Reserved; not to be used", 311103285Sikob /* 4 */ "Busy retry limit exceeded(X)", 312103285Sikob /* 5 */ "Busy retry limit exceeded(A)", 313103285Sikob /* 6 */ "Busy retry limit exceeded(B)", 314103285Sikob /* 7 */ "Reserved for future standardization", 315103285Sikob /* 8 */ "Reserved for future standardization", 316103285Sikob /* 9 */ "Reserved for future standardization", 317103285Sikob /* A */ "Reserved for future standardization", 318103285Sikob /* B */ "Tardy retry limit exceeded", 319103285Sikob /* C */ "Conflict error", 320103285Sikob /* D */ "Data error", 321103285Sikob /* E */ "Type error", 322103285Sikob /* F */ "Address error" 323103285Sikob}; 324103285Sikob 325103285Sikobstatic void 326103285Sikobsbp_identify(driver_t *driver, device_t parent) 327103285Sikob{ 328103285Sikob device_t child; 329103285SikobSBP_DEBUG(0) 330103285Sikob printf("sbp_identify\n"); 331103285SikobEND_DEBUG 332103285Sikob 333103285Sikob child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent)); 334103285Sikob} 335103285Sikob 336103285Sikob/* 337103285Sikob * sbp_probe() 338103285Sikob */ 339103285Sikobstatic int 340103285Sikobsbp_probe(device_t dev) 341103285Sikob{ 342103285Sikob device_t pa; 343103285Sikob 344103285SikobSBP_DEBUG(0) 345103285Sikob printf("sbp_probe\n"); 346103285SikobEND_DEBUG 347103285Sikob 348103285Sikob pa = device_get_parent(dev); 349103285Sikob if(device_get_unit(dev) != device_get_unit(pa)){ 350103285Sikob return(ENXIO); 351103285Sikob } 352103285Sikob 353120660Ssimokawa device_set_desc(dev, "SBP-2/SCSI over FireWire"); 354111615Ssimokawa 355132432Ssimokawa#if 0 356111615Ssimokawa if (bootverbose) 357111615Ssimokawa debug = bootverbose; 358132432Ssimokawa#endif 359132432Ssimokawa 360103285Sikob return (0); 361103285Sikob} 362103285Sikob 363103285Sikobstatic void 364103285Sikobsbp_show_sdev_info(struct sbp_dev *sdev, int new) 365103285Sikob{ 366103285Sikob struct fw_device *fwdev; 367103285Sikob 368103285Sikob printf("%s:%d:%d ", 369103285Sikob device_get_nameunit(sdev->target->sbp->fd.dev), 370103285Sikob sdev->target->target_id, 371103285Sikob sdev->lun_id 372103285Sikob ); 373103285Sikob if (new == 2) { 374103285Sikob return; 375103285Sikob } 376103285Sikob fwdev = sdev->target->fwdev; 377103285Sikob printf("ordered:%d type:%d EUI:%08x%08x node:%d " 378103285Sikob "speed:%d maxrec:%d", 379108503Ssimokawa (sdev->type & 0x40) >> 6, 380108503Ssimokawa (sdev->type & 0x1f), 381103285Sikob fwdev->eui.hi, 382103285Sikob fwdev->eui.lo, 383103285Sikob fwdev->dst, 384103285Sikob fwdev->speed, 385103285Sikob fwdev->maxrec 386103285Sikob ); 387103285Sikob if (new) 388103285Sikob printf(" new!\n"); 389103285Sikob else 390103285Sikob printf("\n"); 391103285Sikob sbp_show_sdev_info(sdev, 2); 392103285Sikob printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision); 393103285Sikob} 394103285Sikob 395110184Ssimokawastatic struct { 396110184Ssimokawa int bus; 397110184Ssimokawa int target; 398110184Ssimokawa struct fw_eui64 eui; 399110184Ssimokawa} wired[] = { 400110184Ssimokawa /* Bus Target EUI64 */ 401110184Ssimokawa#if 0 402110184Ssimokawa {0, 2, {0x00018ea0, 0x01fd0154}}, /* Logitec HDD */ 403110184Ssimokawa {0, 0, {0x00018ea6, 0x00100682}}, /* Logitec DVD */ 404110184Ssimokawa {0, 1, {0x00d03200, 0xa412006a}}, /* Yano HDD */ 405110184Ssimokawa#endif 406110184Ssimokawa {-1, -1, {0,0}} 407110184Ssimokawa}; 408110184Ssimokawa 409110184Ssimokawastatic int 410110184Ssimokawasbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev) 411110184Ssimokawa{ 412110184Ssimokawa int bus, i, target=-1; 413110184Ssimokawa char w[SBP_NUM_TARGETS]; 414110184Ssimokawa 415110184Ssimokawa bzero(w, sizeof(w)); 416110184Ssimokawa bus = device_get_unit(sbp->fd.dev); 417110184Ssimokawa 418110184Ssimokawa /* XXX wired-down configuration should be gotten from 419110184Ssimokawa tunable or device hint */ 420110184Ssimokawa for (i = 0; wired[i].bus >= 0; i ++) { 421110184Ssimokawa if (wired[i].bus == bus) { 422110184Ssimokawa w[wired[i].target] = 1; 423110184Ssimokawa if (wired[i].eui.hi == fwdev->eui.hi && 424110184Ssimokawa wired[i].eui.lo == fwdev->eui.lo) 425110184Ssimokawa target = wired[i].target; 426110184Ssimokawa } 427110184Ssimokawa } 428110184Ssimokawa if (target >= 0) { 429110184Ssimokawa if(target < SBP_NUM_TARGETS && 430110184Ssimokawa sbp->targets[target].fwdev == NULL) 431110184Ssimokawa return(target); 432110184Ssimokawa device_printf(sbp->fd.dev, 433110184Ssimokawa "target %d is not free for %08x:%08x\n", 434110184Ssimokawa target, fwdev->eui.hi, fwdev->eui.lo); 435110184Ssimokawa target = -1; 436110184Ssimokawa } 437110184Ssimokawa /* non-wired target */ 438110184Ssimokawa for (i = 0; i < SBP_NUM_TARGETS; i ++) 439110184Ssimokawa if (sbp->targets[i].fwdev == NULL && w[i] == 0) { 440110184Ssimokawa target = i; 441110184Ssimokawa break; 442110184Ssimokawa } 443110184Ssimokawa 444110184Ssimokawa return target; 445110184Ssimokawa} 446110184Ssimokawa 447121185Ssimokawastatic void 448121185Ssimokawasbp_alloc_lun(struct sbp_target *target) 449103285Sikob{ 450108503Ssimokawa struct crom_context cc; 451108503Ssimokawa struct csrreg *reg; 452121185Ssimokawa struct sbp_dev *sdev, **newluns; 453121185Ssimokawa struct sbp_softc *sbp; 454121185Ssimokawa int maxlun, lun, i; 455103285Sikob 456121185Ssimokawa sbp = target->sbp; 457114069Ssimokawa crom_init_context(&cc, target->fwdev->csrrom); 458108503Ssimokawa /* XXX shoud parse appropriate unit directories only */ 459108503Ssimokawa maxlun = -1; 460108503Ssimokawa while (cc.depth >= 0) { 461108503Ssimokawa reg = crom_search_key(&cc, CROM_LUN); 462108503Ssimokawa if (reg == NULL) 463108503Ssimokawa break; 464110839Ssimokawa lun = reg->val & 0xffff; 465108642SsimokawaSBP_DEBUG(0) 466108642Ssimokawa printf("target %d lun %d found\n", target->target_id, lun); 467108642SsimokawaEND_DEBUG 468108503Ssimokawa if (maxlun < lun) 469108503Ssimokawa maxlun = lun; 470108503Ssimokawa crom_next(&cc); 471108503Ssimokawa } 472110839Ssimokawa if (maxlun < 0) 473121185Ssimokawa printf("%s:%d no LUN found\n", 474121185Ssimokawa device_get_nameunit(target->sbp->fd.dev), 475121185Ssimokawa target->target_id); 476121185Ssimokawa 477121185Ssimokawa maxlun ++; 478110839Ssimokawa if (maxlun >= SBP_NUM_LUNS) 479110839Ssimokawa maxlun = SBP_NUM_LUNS; 480121185Ssimokawa 481121185Ssimokawa /* Invalidiate stale devices */ 482121185Ssimokawa for (lun = 0; lun < target->num_lun; lun ++) { 483121185Ssimokawa sdev = target->luns[lun]; 484121185Ssimokawa if (sdev == NULL) 485121185Ssimokawa continue; 486121185Ssimokawa sdev->flags &= ~VALID_LUN; 487121185Ssimokawa if (lun >= maxlun) { 488121185Ssimokawa /* lost device */ 489121185Ssimokawa sbp_cam_detach_sdev(sdev); 490121185Ssimokawa sbp_free_sdev(sdev); 491121185Ssimokawa } 492103285Sikob } 493121185Ssimokawa 494121185Ssimokawa /* Reallocate */ 495121185Ssimokawa if (maxlun != target->num_lun) { 496121185Ssimokawa newluns = (struct sbp_dev **) realloc(target->luns, 497121185Ssimokawa sizeof(struct sbp_dev *) * maxlun, 498121185Ssimokawa M_SBP, M_NOWAIT | M_ZERO); 499121185Ssimokawa 500121185Ssimokawa if (newluns == NULL) { 501127468Ssimokawa printf("%s: realloc failed\n", __func__); 502121185Ssimokawa newluns = target->luns; 503121185Ssimokawa maxlun = target->num_lun; 504121185Ssimokawa } 505121185Ssimokawa 506121185Ssimokawa /* 507121185Ssimokawa * We must zero the extended region for the case 508121185Ssimokawa * realloc() doesn't allocate new buffer. 509121185Ssimokawa */ 510121185Ssimokawa if (maxlun > target->num_lun) 511121185Ssimokawa bzero(&newluns[target->num_lun], 512121185Ssimokawa sizeof(struct sbp_dev *) * 513121185Ssimokawa (maxlun - target->num_lun)); 514121185Ssimokawa 515121185Ssimokawa target->luns = newluns; 516121185Ssimokawa target->num_lun = maxlun; 517121185Ssimokawa } 518121185Ssimokawa 519108503Ssimokawa crom_init_context(&cc, target->fwdev->csrrom); 520108503Ssimokawa while (cc.depth >= 0) { 521121185Ssimokawa int new = 0; 522121185Ssimokawa 523108503Ssimokawa reg = crom_search_key(&cc, CROM_LUN); 524108503Ssimokawa if (reg == NULL) 525108503Ssimokawa break; 526110839Ssimokawa lun = reg->val & 0xffff; 527110839Ssimokawa if (lun >= SBP_NUM_LUNS) { 528110839Ssimokawa printf("too large lun %d\n", lun); 529121185Ssimokawa goto next; 530110839Ssimokawa } 531121185Ssimokawa 532121185Ssimokawa sdev = target->luns[lun]; 533121185Ssimokawa if (sdev == NULL) { 534121185Ssimokawa sdev = malloc(sizeof(struct sbp_dev), 535121185Ssimokawa M_SBP, M_NOWAIT | M_ZERO); 536121185Ssimokawa if (sdev == NULL) { 537127468Ssimokawa printf("%s: malloc failed\n", __func__); 538121185Ssimokawa goto next; 539121185Ssimokawa } 540121185Ssimokawa target->luns[lun] = sdev; 541121185Ssimokawa sdev->lun_id = lun; 542121185Ssimokawa sdev->target = target; 543121185Ssimokawa STAILQ_INIT(&sdev->ocbs); 544121185Ssimokawa CALLOUT_INIT(&sdev->login_callout); 545121185Ssimokawa sdev->status = SBP_DEV_RESET; 546121185Ssimokawa new = 1; 547121185Ssimokawa } 548121185Ssimokawa sdev->flags |= VALID_LUN; 549119196Ssimokawa sdev->type = (reg->val & 0xff0000) >> 16; 550113584Ssimokawa 551121185Ssimokawa if (new == 0) 552121185Ssimokawa goto next; 553121185Ssimokawa 554113584Ssimokawa fwdma_malloc(sbp->fd.fc, 555129585Sdfr /* alignment */ sizeof(uint32_t), 556113584Ssimokawa SBP_DMA_SIZE, &sdev->dma, BUS_DMA_NOWAIT); 557113584Ssimokawa if (sdev->dma.v_addr == NULL) { 558113584Ssimokawa printf("%s: dma space allocation failed\n", 559127468Ssimokawa __func__); 560121185Ssimokawa free(sdev, M_SBP); 561121185Ssimokawa target->luns[lun] = NULL; 562121185Ssimokawa goto next; 563113584Ssimokawa } 564113584Ssimokawa sdev->login = (struct sbp_login_res *) sdev->dma.v_addr; 565113584Ssimokawa sdev->ocb = (struct sbp_ocb *) 566113584Ssimokawa ((char *)sdev->dma.v_addr + SBP_LOGIN_SIZE); 567113584Ssimokawa bzero((char *)sdev->ocb, 568113584Ssimokawa sizeof (struct sbp_ocb) * SBP_QUEUE_LEN); 569113584Ssimokawa 570113584Ssimokawa STAILQ_INIT(&sdev->free_ocbs); 571113584Ssimokawa for (i = 0; i < SBP_QUEUE_LEN; i++) { 572113584Ssimokawa struct sbp_ocb *ocb; 573113584Ssimokawa ocb = &sdev->ocb[i]; 574113584Ssimokawa ocb->bus_addr = sdev->dma.bus_addr 575113584Ssimokawa + SBP_LOGIN_SIZE 576113584Ssimokawa + sizeof(struct sbp_ocb) * i 577113584Ssimokawa + offsetof(struct sbp_ocb, orb[0]); 578113584Ssimokawa if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) { 579113584Ssimokawa printf("sbp_attach: cannot create dmamap\n"); 580121185Ssimokawa /* XXX */ 581121185Ssimokawa goto next; 582113584Ssimokawa } 583113584Ssimokawa sbp_free_ocb(sdev, ocb); 584113584Ssimokawa } 585121185Ssimokawanext: 586108503Ssimokawa crom_next(&cc); 587103285Sikob } 588121185Ssimokawa 589121185Ssimokawa for (lun = 0; lun < target->num_lun; lun ++) { 590121185Ssimokawa sdev = target->luns[lun]; 591121185Ssimokawa if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) { 592121185Ssimokawa sbp_cam_detach_sdev(sdev); 593121185Ssimokawa sbp_free_sdev(sdev); 594121185Ssimokawa target->luns[lun] = NULL; 595121185Ssimokawa } 596121185Ssimokawa } 597121185Ssimokawa} 598121185Ssimokawa 599121185Ssimokawastatic struct sbp_target * 600121185Ssimokawasbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev) 601121185Ssimokawa{ 602121185Ssimokawa int i; 603121185Ssimokawa struct sbp_target *target; 604121185Ssimokawa struct crom_context cc; 605121185Ssimokawa struct csrreg *reg; 606121185Ssimokawa 607121185SsimokawaSBP_DEBUG(1) 608121185Ssimokawa printf("sbp_alloc_target\n"); 609121185SsimokawaEND_DEBUG 610121185Ssimokawa i = sbp_new_target(sbp, fwdev); 611121185Ssimokawa if (i < 0) { 612121185Ssimokawa device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n"); 613121185Ssimokawa return NULL; 614121185Ssimokawa } 615121185Ssimokawa /* new target */ 616121185Ssimokawa target = &sbp->targets[i]; 617121185Ssimokawa target->sbp = sbp; 618121185Ssimokawa target->fwdev = fwdev; 619121185Ssimokawa target->target_id = i; 620121185Ssimokawa /* XXX we may want to reload mgm port after each bus reset */ 621121185Ssimokawa /* XXX there might be multiple management agents */ 622121185Ssimokawa crom_init_context(&cc, target->fwdev->csrrom); 623121185Ssimokawa reg = crom_search_key(&cc, CROM_MGM); 624121185Ssimokawa if (reg == NULL || reg->val == 0) { 625121185Ssimokawa printf("NULL management address\n"); 626121185Ssimokawa target->fwdev = NULL; 627121185Ssimokawa return NULL; 628121185Ssimokawa } 629121185Ssimokawa target->mgm_hi = 0xffff; 630121185Ssimokawa target->mgm_lo = 0xf0000000 | (reg->val << 2); 631121185Ssimokawa target->mgm_ocb_cur = NULL; 632121185SsimokawaSBP_DEBUG(1) 633121185Ssimokawa printf("target:%d mgm_port: %x\n", i, target->mgm_lo); 634121185SsimokawaEND_DEBUG 635121185Ssimokawa STAILQ_INIT(&target->xferlist); 636121185Ssimokawa target->n_xfer = 0; 637121185Ssimokawa STAILQ_INIT(&target->mgm_ocb_queue); 638121185Ssimokawa CALLOUT_INIT(&target->mgm_ocb_timeout); 639121185Ssimokawa CALLOUT_INIT(&target->scan_callout); 640121185Ssimokawa 641121185Ssimokawa target->luns = NULL; 642121185Ssimokawa target->num_lun = 0; 643114069Ssimokawa return target; 644103285Sikob} 645103285Sikob 646103285Sikobstatic void 647103285Sikobsbp_probe_lun(struct sbp_dev *sdev) 648103285Sikob{ 649103285Sikob struct fw_device *fwdev; 650114069Ssimokawa struct crom_context c, *cc = &c; 651114069Ssimokawa struct csrreg *reg; 652103285Sikob 653103285Sikob bzero(sdev->vendor, sizeof(sdev->vendor)); 654103285Sikob bzero(sdev->product, sizeof(sdev->product)); 655114069Ssimokawa 656114069Ssimokawa fwdev = sdev->target->fwdev; 657114069Ssimokawa crom_init_context(cc, fwdev->csrrom); 658114069Ssimokawa /* get vendor string */ 659114069Ssimokawa crom_search_key(cc, CSRKEY_VENDOR); 660114069Ssimokawa crom_next(cc); 661114069Ssimokawa crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor)); 662114223Ssimokawa /* skip to the unit directory for SBP-2 */ 663114260Ssimokawa while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) { 664114223Ssimokawa if (reg->val == CSRVAL_T10SBP2) 665114223Ssimokawa break; 666114260Ssimokawa crom_next(cc); 667114260Ssimokawa } 668114069Ssimokawa /* get firmware revision */ 669114069Ssimokawa reg = crom_search_key(cc, CSRKEY_FIRM_VER); 670114069Ssimokawa if (reg != NULL) 671114069Ssimokawa snprintf(sdev->revision, sizeof(sdev->revision), 672114069Ssimokawa "%06x", reg->val); 673114069Ssimokawa /* get product string */ 674114069Ssimokawa crom_search_key(cc, CSRKEY_MODEL); 675114069Ssimokawa crom_next(cc); 676114069Ssimokawa crom_parse_text(cc, sdev->product, sizeof(sdev->product)); 677103285Sikob} 678111615Ssimokawa 679103285Sikobstatic void 680111615Ssimokawasbp_login_callout(void *arg) 681103285Sikob{ 682111615Ssimokawa struct sbp_dev *sdev = (struct sbp_dev *)arg; 683111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL); 684111615Ssimokawa} 685111615Ssimokawa 686122387Ssimokawastatic void 687122387Ssimokawasbp_login(struct sbp_dev *sdev) 688122387Ssimokawa{ 689122387Ssimokawa struct timeval delta; 690122387Ssimokawa struct timeval t; 691122387Ssimokawa int ticks = 0; 692122387Ssimokawa 693122387Ssimokawa microtime(&delta); 694122387Ssimokawa timevalsub(&delta, &sdev->target->sbp->last_busreset); 695122387Ssimokawa t.tv_sec = login_delay / 1000; 696122387Ssimokawa t.tv_usec = (login_delay % 1000) * 1000; 697122387Ssimokawa timevalsub(&t, &delta); 698122387Ssimokawa if (t.tv_sec >= 0 && t.tv_usec > 0) 699122387Ssimokawa ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000; 700122387SsimokawaSBP_DEBUG(0) 701127468Ssimokawa printf("%s: sec = %ld usec = %ld ticks = %d\n", __func__, 702122387Ssimokawa t.tv_sec, t.tv_usec, ticks); 703122387SsimokawaEND_DEBUG 704122387Ssimokawa callout_reset(&sdev->login_callout, ticks, 705122387Ssimokawa sbp_login_callout, (void *)(sdev)); 706122387Ssimokawa} 707122387Ssimokawa 708114069Ssimokawa#define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \ 709114069Ssimokawa && crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2)) 710111615Ssimokawa 711111615Ssimokawastatic void 712111615Ssimokawasbp_probe_target(void *arg) 713111615Ssimokawa{ 714111615Ssimokawa struct sbp_target *target = (struct sbp_target *)arg; 715103285Sikob struct sbp_softc *sbp; 716103285Sikob struct sbp_dev *sdev; 717103285Sikob struct firewire_comm *fc; 718111615Ssimokawa int i, alive; 719103285Sikob 720111615Ssimokawa alive = SBP_FWDEV_ALIVE(target->fwdev); 721103285SikobSBP_DEBUG(1) 722103285Sikob printf("sbp_probe_target %d\n", target->target_id); 723103285Sikob if (!alive) 724103285Sikob printf("not alive\n"); 725103285SikobEND_DEBUG 726103285Sikob 727103285Sikob sbp = target->sbp; 728103285Sikob fc = target->sbp->fd.fc; 729121185Ssimokawa sbp_alloc_lun(target); 730121185Ssimokawa 731111615Ssimokawa /* XXX untimeout mgm_ocb and dequeue */ 732103285Sikob for (i=0; i < target->num_lun; i++) { 733121185Ssimokawa sdev = target->luns[i]; 734121185Ssimokawa if (sdev == NULL) 735121185Ssimokawa continue; 736103285Sikob if (alive && (sdev->status != SBP_DEV_DEAD)) { 737103285Sikob if (sdev->path != NULL) { 738103285Sikob xpt_freeze_devq(sdev->path, 1); 739111615Ssimokawa sdev->freeze ++; 740103285Sikob } 741111615Ssimokawa sbp_probe_lun(sdev); 742111615SsimokawaSBP_DEBUG(0) 743111615Ssimokawa sbp_show_sdev_info(sdev, 744111615Ssimokawa (sdev->status == SBP_DEV_RESET)); 745111615SsimokawaEND_DEBUG 746111615Ssimokawa 747111615Ssimokawa sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET); 748103285Sikob switch (sdev->status) { 749110336Ssimokawa case SBP_DEV_RESET: 750103285Sikob /* new or revived target */ 751113584Ssimokawa if (auto_login) 752111615Ssimokawa sbp_login(sdev); 753103285Sikob break; 754111615Ssimokawa case SBP_DEV_TOATTACH: 755111615Ssimokawa case SBP_DEV_PROBE: 756111615Ssimokawa case SBP_DEV_ATTACHED: 757110336Ssimokawa case SBP_DEV_RETRY: 758110336Ssimokawa default: 759111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL); 760110336Ssimokawa break; 761103285Sikob } 762103285Sikob } else { 763103285Sikob switch (sdev->status) { 764103285Sikob case SBP_DEV_ATTACHED: 765103285SikobSBP_DEBUG(0) 766103285Sikob /* the device has gone */ 767103285Sikob sbp_show_sdev_info(sdev, 2); 768103285Sikob printf("lost target\n"); 769103285SikobEND_DEBUG 770111615Ssimokawa if (sdev->path) { 771103285Sikob xpt_freeze_devq(sdev->path, 1); 772111615Ssimokawa sdev->freeze ++; 773111615Ssimokawa } 774103285Sikob sdev->status = SBP_DEV_RETRY; 775111615Ssimokawa sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET); 776103285Sikob break; 777103285Sikob case SBP_DEV_PROBE: 778103285Sikob case SBP_DEV_TOATTACH: 779103285Sikob sdev->status = SBP_DEV_RESET; 780103285Sikob break; 781103285Sikob case SBP_DEV_RETRY: 782103285Sikob case SBP_DEV_RESET: 783103285Sikob case SBP_DEV_DEAD: 784103285Sikob break; 785103285Sikob } 786103285Sikob } 787103285Sikob } 788103285Sikob} 789103285Sikob 790103285Sikobstatic void 791113584Ssimokawasbp_post_busreset(void *arg) 792113584Ssimokawa{ 793113584Ssimokawa struct sbp_softc *sbp; 794113584Ssimokawa 795113584Ssimokawa sbp = (struct sbp_softc *)arg; 796113584SsimokawaSBP_DEBUG(0) 797113584Ssimokawa printf("sbp_post_busreset\n"); 798113584SsimokawaEND_DEBUG 799124145Ssimokawa if ((sbp->sim->flags & SIMQ_FREEZED) == 0) { 800124145Ssimokawa xpt_freeze_simq(sbp->sim, /*count*/1); 801124145Ssimokawa sbp->sim->flags |= SIMQ_FREEZED; 802124145Ssimokawa } 803122387Ssimokawa microtime(&sbp->last_busreset); 804113584Ssimokawa} 805113584Ssimokawa 806113584Ssimokawastatic void 807103285Sikobsbp_post_explore(void *arg) 808103285Sikob{ 809103285Sikob struct sbp_softc *sbp = (struct sbp_softc *)arg; 810103285Sikob struct sbp_target *target; 811103285Sikob struct fw_device *fwdev; 812103285Sikob int i, alive; 813103285Sikob 814111199SsimokawaSBP_DEBUG(0) 815111199Ssimokawa printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold); 816103285SikobEND_DEBUG 817122387Ssimokawa if (sbp_cold > 0) 818122387Ssimokawa sbp_cold --; 819122387Ssimokawa 820122387Ssimokawa#if 0 821122387Ssimokawa /* 822122387Ssimokawa * XXX don't let CAM the bus rest. 823122387Ssimokawa * CAM tries to do something with freezed (DEV_RETRY) devices. 824111615Ssimokawa */ 825111615Ssimokawa xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL); 826103285Sikob#endif 827111615Ssimokawa 828130532Sdfr /* Garbage Collection */ 829103285Sikob for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){ 830103285Sikob target = &sbp->targets[i]; 831110193Ssimokawa STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) 832110193Ssimokawa if (target->fwdev == NULL || target->fwdev == fwdev) 833110193Ssimokawa break; 834121185Ssimokawa if (fwdev == NULL) { 835103285Sikob /* device has removed in lower driver */ 836110145Ssimokawa sbp_cam_detach_target(target); 837121185Ssimokawa sbp_free_target(target); 838103285Sikob } 839103285Sikob } 840103285Sikob /* traverse device list */ 841110193Ssimokawa STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) { 842103285SikobSBP_DEBUG(0) 843103285Sikob printf("sbp_post_explore: EUI:%08x%08x ", 844103285Sikob fwdev->eui.hi, fwdev->eui.lo); 845114069Ssimokawa if (fwdev->status != FWDEVATTACHED) 846103285Sikob printf("not attached, state=%d.\n", fwdev->status); 847114069Ssimokawa else 848114069Ssimokawa printf("attached\n"); 849103285SikobEND_DEBUG 850111615Ssimokawa alive = SBP_FWDEV_ALIVE(fwdev); 851103285Sikob for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){ 852103285Sikob target = &sbp->targets[i]; 853103285Sikob if(target->fwdev == fwdev ) { 854103285Sikob /* known target */ 855103285Sikob break; 856103285Sikob } 857103285Sikob } 858103285Sikob if(i == SBP_NUM_TARGETS){ 859103285Sikob if (alive) { 860103285Sikob /* new target */ 861103285Sikob target = sbp_alloc_target(sbp, fwdev); 862103285Sikob if (target == NULL) 863103285Sikob continue; 864103285Sikob } else { 865103285Sikob continue; 866103285Sikob } 867103285Sikob } 868111615Ssimokawa sbp_probe_target((void *)target); 869121185Ssimokawa if (target->num_lun == 0) 870121185Ssimokawa sbp_free_target(target); 871103285Sikob } 872124145Ssimokawa xpt_release_simq(sbp->sim, /*run queue*/TRUE); 873124145Ssimokawa sbp->sim->flags &= ~SIMQ_FREEZED; 874103285Sikob} 875103285Sikob 876103285Sikob#if NEED_RESPONSE 877103285Sikobstatic void 878103285Sikobsbp_loginres_callback(struct fw_xfer *xfer){ 879113584Ssimokawa int s; 880103285Sikob struct sbp_dev *sdev; 881103285Sikob sdev = (struct sbp_dev *)xfer->sc; 882113584SsimokawaSBP_DEBUG(1) 883103285Sikob sbp_show_sdev_info(sdev, 2); 884103285Sikob printf("sbp_loginres_callback\n"); 885103285SikobEND_DEBUG 886113584Ssimokawa /* recycle */ 887113584Ssimokawa s = splfw(); 888113584Ssimokawa STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link); 889113584Ssimokawa splx(s); 890103285Sikob return; 891103285Sikob} 892103285Sikob#endif 893103285Sikob 894113584Ssimokawastatic __inline void 895113584Ssimokawasbp_xfer_free(struct fw_xfer *xfer) 896113584Ssimokawa{ 897113584Ssimokawa struct sbp_dev *sdev; 898113584Ssimokawa int s; 899113584Ssimokawa 900113584Ssimokawa sdev = (struct sbp_dev *)xfer->sc; 901113584Ssimokawa fw_xfer_unload(xfer); 902113584Ssimokawa s = splfw(); 903113584Ssimokawa STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link); 904113584Ssimokawa splx(s); 905113584Ssimokawa} 906113584Ssimokawa 907103285Sikobstatic void 908114732Ssimokawasbp_reset_start_callback(struct fw_xfer *xfer) 909103285Sikob{ 910114732Ssimokawa struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc; 911114732Ssimokawa struct sbp_target *target = sdev->target; 912114732Ssimokawa int i; 913114732Ssimokawa 914114732Ssimokawa if (xfer->resp != 0) { 915114732Ssimokawa sbp_show_sdev_info(sdev, 2); 916114732Ssimokawa printf("sbp_reset_start failed: resp=%d\n", xfer->resp); 917114732Ssimokawa } 918114732Ssimokawa 919114732Ssimokawa for (i = 0; i < target->num_lun; i++) { 920121185Ssimokawa tsdev = target->luns[i]; 921121185Ssimokawa if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN) 922121185Ssimokawa sbp_login(tsdev); 923114732Ssimokawa } 924114732Ssimokawa} 925114732Ssimokawa 926114732Ssimokawastatic void 927114732Ssimokawasbp_reset_start(struct sbp_dev *sdev) 928114732Ssimokawa{ 929114732Ssimokawa struct fw_xfer *xfer; 930114732Ssimokawa struct fw_pkt *fp; 931114732Ssimokawa 932114732SsimokawaSBP_DEBUG(0) 933114732Ssimokawa sbp_show_sdev_info(sdev, 2); 934114732Ssimokawa printf("sbp_reset_start\n"); 935114732SsimokawaEND_DEBUG 936114732Ssimokawa 937114732Ssimokawa xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 938114732Ssimokawa xfer->act.hand = sbp_reset_start_callback; 939120660Ssimokawa fp = &xfer->send.hdr; 940114732Ssimokawa fp->mode.wreqq.dest_hi = 0xffff; 941114732Ssimokawa fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START; 942114732Ssimokawa fp->mode.wreqq.data = htonl(0xf); 943114732Ssimokawa fw_asyreq(xfer->fc, -1, xfer); 944114732Ssimokawa} 945114732Ssimokawa 946114732Ssimokawastatic void 947114732Ssimokawasbp_mgm_callback(struct fw_xfer *xfer) 948114732Ssimokawa{ 949103285Sikob struct sbp_dev *sdev; 950114732Ssimokawa int resp; 951114732Ssimokawa 952103285Sikob sdev = (struct sbp_dev *)xfer->sc; 953114732Ssimokawa 954114732SsimokawaSBP_DEBUG(1) 955103285Sikob sbp_show_sdev_info(sdev, 2); 956114732Ssimokawa printf("sbp_mgm_callback\n"); 957103285SikobEND_DEBUG 958114732Ssimokawa resp = xfer->resp; 959113584Ssimokawa sbp_xfer_free(xfer); 960114732Ssimokawa#if 0 961114732Ssimokawa if (resp != 0) { 962114732Ssimokawa sbp_show_sdev_info(sdev, 2); 963114732Ssimokawa printf("management ORB failed(%d) ... RESET_START\n", resp); 964114732Ssimokawa sbp_reset_start(sdev); 965114732Ssimokawa } 966114732Ssimokawa#endif 967103285Sikob return; 968103285Sikob} 969103285Sikob 970111615Ssimokawastatic struct sbp_dev * 971111615Ssimokawasbp_next_dev(struct sbp_target *target, int lun) 972111615Ssimokawa{ 973121185Ssimokawa struct sbp_dev **sdevp; 974111615Ssimokawa int i; 975111615Ssimokawa 976121185Ssimokawa for (i = lun, sdevp = &target->luns[lun]; i < target->num_lun; 977121185Ssimokawa i++, sdevp++) 978121185Ssimokawa if (*sdevp != NULL && (*sdevp)->status == SBP_DEV_PROBE) 979121185Ssimokawa return(*sdevp); 980121185Ssimokawa return(NULL); 981111615Ssimokawa} 982111615Ssimokawa 983111615Ssimokawa#define SCAN_PRI 1 984103285Sikobstatic void 985111615Ssimokawasbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb) 986103285Sikob{ 987111615Ssimokawa struct sbp_target *target; 988103285Sikob struct sbp_dev *sdev; 989111615Ssimokawa 990103285Sikob sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr; 991111615Ssimokawa target = sdev->target; 992110269SsimokawaSBP_DEBUG(0) 993103285Sikob sbp_show_sdev_info(sdev, 2); 994111615Ssimokawa printf("sbp_cam_scan_lun\n"); 995103285SikobEND_DEBUG 996111615Ssimokawa if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 997111615Ssimokawa sdev->status = SBP_DEV_ATTACHED; 998111615Ssimokawa } else { 999111615Ssimokawa sbp_show_sdev_info(sdev, 2); 1000111615Ssimokawa printf("scan failed\n"); 1001111615Ssimokawa } 1002111615Ssimokawa sdev = sbp_next_dev(target, sdev->lun_id + 1); 1003111615Ssimokawa if (sdev == NULL) { 1004111615Ssimokawa free(ccb, M_SBP); 1005111615Ssimokawa return; 1006111615Ssimokawa } 1007111615Ssimokawa /* reuse ccb */ 1008111615Ssimokawa xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI); 1009111615Ssimokawa ccb->ccb_h.ccb_sdev_ptr = sdev; 1010111615Ssimokawa xpt_action(ccb); 1011111615Ssimokawa xpt_release_devq(sdev->path, sdev->freeze, TRUE); 1012111615Ssimokawa sdev->freeze = 1; 1013103285Sikob} 1014103285Sikob 1015103285Sikobstatic void 1016111615Ssimokawasbp_cam_scan_target(void *arg) 1017103285Sikob{ 1018111615Ssimokawa struct sbp_target *target = (struct sbp_target *)arg; 1019111615Ssimokawa struct sbp_dev *sdev; 1020110798Ssimokawa union ccb *ccb; 1021103285Sikob 1022111615Ssimokawa sdev = sbp_next_dev(target, 0); 1023111615Ssimokawa if (sdev == NULL) { 1024111615Ssimokawa printf("sbp_cam_scan_target: nothing to do for target%d\n", 1025111615Ssimokawa target->target_id); 1026110798Ssimokawa return; 1027110798Ssimokawa } 1028103285SikobSBP_DEBUG(0) 1029103285Sikob sbp_show_sdev_info(sdev, 2); 1030111615Ssimokawa printf("sbp_cam_scan_target\n"); 1031103285SikobEND_DEBUG 1032111615Ssimokawa ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO); 1033111615Ssimokawa if (ccb == NULL) { 1034111615Ssimokawa printf("sbp_cam_scan_target: malloc failed\n"); 1035111615Ssimokawa return; 1036111615Ssimokawa } 1037111615Ssimokawa xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI); 1038103285Sikob ccb->ccb_h.func_code = XPT_SCAN_LUN; 1039111615Ssimokawa ccb->ccb_h.cbfcnp = sbp_cam_scan_lun; 1040111615Ssimokawa ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 1041103285Sikob ccb->crcn.flags = CAM_FLAG_NONE; 1042103285Sikob ccb->ccb_h.ccb_sdev_ptr = sdev; 1043103285Sikob 1044103285Sikob /* The scan is in progress now. */ 1045111040Ssimokawa xpt_action(ccb); 1046111615Ssimokawa xpt_release_devq(sdev->path, sdev->freeze, TRUE); 1047111615Ssimokawa sdev->freeze = 1; 1048103285Sikob} 1049103285Sikob 1050111615Ssimokawastatic __inline void 1051111615Ssimokawasbp_scan_dev(struct sbp_dev *sdev) 1052111615Ssimokawa{ 1053111040Ssimokawa sdev->status = SBP_DEV_PROBE; 1054122387Ssimokawa callout_reset(&sdev->target->scan_callout, scan_delay * hz / 1000, 1055111615Ssimokawa sbp_cam_scan_target, (void *)sdev->target); 1056103285Sikob} 1057103285Sikob 1058103285Sikobstatic void 1059103285Sikobsbp_do_attach(struct fw_xfer *xfer) 1060103285Sikob{ 1061103285Sikob struct sbp_dev *sdev; 1062111615Ssimokawa struct sbp_target *target; 1063111615Ssimokawa struct sbp_softc *sbp; 1064103285Sikob 1065103285Sikob sdev = (struct sbp_dev *)xfer->sc; 1066111615Ssimokawa target = sdev->target; 1067111615Ssimokawa sbp = target->sbp; 1068103285SikobSBP_DEBUG(0) 1069103285Sikob sbp_show_sdev_info(sdev, 2); 1070103285Sikob printf("sbp_do_attach\n"); 1071103285SikobEND_DEBUG 1072113584Ssimokawa sbp_xfer_free(xfer); 1073111199Ssimokawa 1074103285Sikob if (sdev->path == NULL) 1075103285Sikob xpt_create_path(&sdev->path, xpt_periph, 1076111615Ssimokawa cam_sim_path(target->sbp->sim), 1077111615Ssimokawa target->target_id, sdev->lun_id); 1078103285Sikob 1079111199Ssimokawa /* 1080111199Ssimokawa * Let CAM scan the bus if we are in the boot process. 1081111199Ssimokawa * XXX xpt_scan_bus cannot detect LUN larger than 0 1082111199Ssimokawa * if LUN 0 doesn't exists. 1083111199Ssimokawa */ 1084111199Ssimokawa if (sbp_cold > 0) { 1085111615Ssimokawa sdev->status = SBP_DEV_ATTACHED; 1086111199Ssimokawa return; 1087111199Ssimokawa } 1088111199Ssimokawa 1089111615Ssimokawa sbp_scan_dev(sdev); 1090103285Sikob return; 1091103285Sikob} 1092103285Sikob 1093103285Sikobstatic void 1094103285Sikobsbp_agent_reset_callback(struct fw_xfer *xfer) 1095103285Sikob{ 1096103285Sikob struct sbp_dev *sdev; 1097103285Sikob 1098103285Sikob sdev = (struct sbp_dev *)xfer->sc; 1099103285SikobSBP_DEBUG(1) 1100103285Sikob sbp_show_sdev_info(sdev, 2); 1101127468Ssimokawa printf("%s\n", __func__); 1102103285SikobEND_DEBUG 1103114732Ssimokawa if (xfer->resp != 0) { 1104114732Ssimokawa sbp_show_sdev_info(sdev, 2); 1105127468Ssimokawa printf("%s: resp=%d\n", __func__, xfer->resp); 1106114732Ssimokawa } 1107114732Ssimokawa 1108113584Ssimokawa sbp_xfer_free(xfer); 1109111615Ssimokawa if (sdev->path) { 1110111615Ssimokawa xpt_release_devq(sdev->path, sdev->freeze, TRUE); 1111111615Ssimokawa sdev->freeze = 0; 1112111615Ssimokawa } 1113103285Sikob} 1114103285Sikob 1115103285Sikobstatic void 1116110336Ssimokawasbp_agent_reset(struct sbp_dev *sdev) 1117103285Sikob{ 1118103285Sikob struct fw_xfer *xfer; 1119103285Sikob struct fw_pkt *fp; 1120103285Sikob 1121103285SikobSBP_DEBUG(0) 1122103285Sikob sbp_show_sdev_info(sdev, 2); 1123103285Sikob printf("sbp_agent_reset\n"); 1124103285SikobEND_DEBUG 1125103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04); 1126103285Sikob if (xfer == NULL) 1127103285Sikob return; 1128115788Ssimokawa if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE) 1129110336Ssimokawa xfer->act.hand = sbp_agent_reset_callback; 1130110336Ssimokawa else 1131103285Sikob xfer->act.hand = sbp_do_attach; 1132120660Ssimokawa fp = &xfer->send.hdr; 1133103285Sikob fp->mode.wreqq.data = htonl(0xf); 1134103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1135111615Ssimokawa sbp_abort_all_ocbs(sdev, CAM_BDR_SENT); 1136103285Sikob} 1137103285Sikob 1138103285Sikobstatic void 1139103285Sikobsbp_busy_timeout_callback(struct fw_xfer *xfer) 1140103285Sikob{ 1141103285Sikob struct sbp_dev *sdev; 1142103285Sikob 1143103285Sikob sdev = (struct sbp_dev *)xfer->sc; 1144103285SikobSBP_DEBUG(1) 1145103285Sikob sbp_show_sdev_info(sdev, 2); 1146110336Ssimokawa printf("sbp_busy_timeout_callback\n"); 1147103285SikobEND_DEBUG 1148113584Ssimokawa sbp_xfer_free(xfer); 1149110336Ssimokawa sbp_agent_reset(sdev); 1150103285Sikob} 1151103285Sikob 1152103285Sikobstatic void 1153103285Sikobsbp_busy_timeout(struct sbp_dev *sdev) 1154103285Sikob{ 1155103285Sikob struct fw_pkt *fp; 1156103285Sikob struct fw_xfer *xfer; 1157103285SikobSBP_DEBUG(0) 1158103285Sikob sbp_show_sdev_info(sdev, 2); 1159103285Sikob printf("sbp_busy_timeout\n"); 1160103285SikobEND_DEBUG 1161103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 1162103285Sikob 1163103285Sikob xfer->act.hand = sbp_busy_timeout_callback; 1164120660Ssimokawa fp = &xfer->send.hdr; 1165113584Ssimokawa fp->mode.wreqq.dest_hi = 0xffff; 1166113584Ssimokawa fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT; 1167110129Ssimokawa fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf); 1168103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1169103285Sikob} 1170103285Sikob 1171103285Sikobstatic void 1172121185Ssimokawasbp_orb_pointer_callback(struct fw_xfer *xfer) 1173121185Ssimokawa{ 1174121185Ssimokawa struct sbp_dev *sdev; 1175121185Ssimokawa sdev = (struct sbp_dev *)xfer->sc; 1176121185Ssimokawa 1177121185SsimokawaSBP_DEBUG(1) 1178121185Ssimokawa sbp_show_sdev_info(sdev, 2); 1179127468Ssimokawa printf("%s\n", __func__); 1180121185SsimokawaEND_DEBUG 1181121185Ssimokawa if (xfer->resp != 0) { 1182121185Ssimokawa /* XXX */ 1183127468Ssimokawa printf("%s: xfer->resp = %d\n", __func__, xfer->resp); 1184121185Ssimokawa } 1185121185Ssimokawa sbp_xfer_free(xfer); 1186121185Ssimokawa sdev->flags &= ~ORB_POINTER_ACTIVE; 1187121185Ssimokawa 1188121185Ssimokawa if ((sdev->flags & ORB_POINTER_NEED) != 0) { 1189121185Ssimokawa struct sbp_ocb *ocb; 1190121185Ssimokawa 1191121185Ssimokawa sdev->flags &= ~ORB_POINTER_NEED; 1192121185Ssimokawa ocb = STAILQ_FIRST(&sdev->ocbs); 1193121185Ssimokawa if (ocb != NULL) 1194121185Ssimokawa sbp_orb_pointer(sdev, ocb); 1195121185Ssimokawa } 1196121185Ssimokawa return; 1197121185Ssimokawa} 1198121185Ssimokawa 1199121185Ssimokawastatic void 1200103285Sikobsbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb) 1201103285Sikob{ 1202103285Sikob struct fw_xfer *xfer; 1203103285Sikob struct fw_pkt *fp; 1204121185SsimokawaSBP_DEBUG(1) 1205103285Sikob sbp_show_sdev_info(sdev, 2); 1206129585Sdfr printf("%s: 0x%08x\n", __func__, (uint32_t)ocb->bus_addr); 1207103285SikobEND_DEBUG 1208103285Sikob 1209121185Ssimokawa if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) { 1210122421SsimokawaSBP_DEBUG(0) 1211127468Ssimokawa printf("%s: orb pointer active\n", __func__); 1212122421SsimokawaEND_DEBUG 1213121185Ssimokawa sdev->flags |= ORB_POINTER_NEED; 1214121185Ssimokawa return; 1215121185Ssimokawa } 1216121185Ssimokawa 1217121185Ssimokawa sdev->flags |= ORB_POINTER_ACTIVE; 1218103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08); 1219103285Sikob if (xfer == NULL) 1220103285Sikob return; 1221121185Ssimokawa xfer->act.hand = sbp_orb_pointer_callback; 1222103285Sikob 1223120660Ssimokawa fp = &xfer->send.hdr; 1224113584Ssimokawa fp->mode.wreqb.len = 8; 1225103285Sikob fp->mode.wreqb.extcode = 0; 1226120660Ssimokawa xfer->send.payload[0] = 1227103285Sikob htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16)); 1228129585Sdfr xfer->send.payload[1] = htonl((uint32_t)ocb->bus_addr); 1229103285Sikob 1230103285Sikob if(fw_asyreq(xfer->fc, -1, xfer) != 0){ 1231113584Ssimokawa sbp_xfer_free(xfer); 1232103285Sikob ocb->ccb->ccb_h.status = CAM_REQ_INVALID; 1233103285Sikob xpt_done(ocb->ccb); 1234103285Sikob } 1235103285Sikob} 1236103285Sikob 1237103285Sikobstatic void 1238127468Ssimokawasbp_doorbell_callback(struct fw_xfer *xfer) 1239121185Ssimokawa{ 1240121185Ssimokawa struct sbp_dev *sdev; 1241121185Ssimokawa sdev = (struct sbp_dev *)xfer->sc; 1242127468Ssimokawa 1243127468SsimokawaSBP_DEBUG(1) 1244121185Ssimokawa sbp_show_sdev_info(sdev, 2); 1245127468Ssimokawa printf("sbp_doorbell_callback\n"); 1246121185SsimokawaEND_DEBUG 1247121185Ssimokawa if (xfer->resp != 0) { 1248121185Ssimokawa /* XXX */ 1249127468Ssimokawa printf("%s: xfer->resp = %d\n", __func__, xfer->resp); 1250121185Ssimokawa } 1251121185Ssimokawa sbp_xfer_free(xfer); 1252127468Ssimokawa sdev->flags &= ~ORB_DOORBELL_ACTIVE; 1253127468Ssimokawa if ((sdev->flags & ORB_DOORBELL_NEED) != 0) { 1254127468Ssimokawa sdev->flags &= ~ORB_DOORBELL_NEED; 1255127468Ssimokawa sbp_doorbell(sdev); 1256127468Ssimokawa } 1257121185Ssimokawa return; 1258121185Ssimokawa} 1259121185Ssimokawa 1260121185Ssimokawastatic void 1261103285Sikobsbp_doorbell(struct sbp_dev *sdev) 1262103285Sikob{ 1263103285Sikob struct fw_xfer *xfer; 1264103285Sikob struct fw_pkt *fp; 1265103285SikobSBP_DEBUG(1) 1266103285Sikob sbp_show_sdev_info(sdev, 2); 1267103285Sikob printf("sbp_doorbell\n"); 1268103285SikobEND_DEBUG 1269103285Sikob 1270127468Ssimokawa if ((sdev->flags & ORB_DOORBELL_ACTIVE) != 0) { 1271127468Ssimokawa sdev->flags |= ORB_DOORBELL_NEED; 1272127468Ssimokawa return; 1273127468Ssimokawa } 1274127468Ssimokawa sdev->flags |= ORB_DOORBELL_ACTIVE; 1275103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10); 1276103285Sikob if (xfer == NULL) 1277103285Sikob return; 1278127468Ssimokawa xfer->act.hand = sbp_doorbell_callback; 1279127468Ssimokawa fp = &xfer->send.hdr; 1280103285Sikob fp->mode.wreqq.data = htonl(0xf); 1281103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1282103285Sikob} 1283103285Sikob 1284103285Sikobstatic struct fw_xfer * 1285103285Sikobsbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset) 1286103285Sikob{ 1287103285Sikob struct fw_xfer *xfer; 1288103285Sikob struct fw_pkt *fp; 1289113584Ssimokawa struct sbp_target *target; 1290113584Ssimokawa int s, new = 0; 1291103285Sikob 1292113584Ssimokawa target = sdev->target; 1293113584Ssimokawa s = splfw(); 1294113584Ssimokawa xfer = STAILQ_FIRST(&target->xferlist); 1295113584Ssimokawa if (xfer == NULL) { 1296113584Ssimokawa if (target->n_xfer > 5 /* XXX */) { 1297113584Ssimokawa printf("sbp: no more xfer for this target\n"); 1298113584Ssimokawa splx(s); 1299113584Ssimokawa return(NULL); 1300113584Ssimokawa } 1301120660Ssimokawa xfer = fw_xfer_alloc_buf(M_SBP, 8, 0); 1302113584Ssimokawa if(xfer == NULL){ 1303113584Ssimokawa printf("sbp: fw_xfer_alloc_buf failed\n"); 1304113584Ssimokawa splx(s); 1305113584Ssimokawa return NULL; 1306113584Ssimokawa } 1307113584Ssimokawa target->n_xfer ++; 1308113584Ssimokawa if (debug) 1309113584Ssimokawa printf("sbp: alloc %d xfer\n", target->n_xfer); 1310113584Ssimokawa new = 1; 1311113584Ssimokawa } else { 1312113584Ssimokawa STAILQ_REMOVE_HEAD(&target->xferlist, link); 1313103285Sikob } 1314113584Ssimokawa splx(s); 1315113584Ssimokawa 1316113584Ssimokawa microtime(&xfer->tv); 1317113584Ssimokawa 1318113584Ssimokawa if (new) { 1319120660Ssimokawa xfer->recv.pay_len = 0; 1320120660Ssimokawa xfer->send.spd = min(sdev->target->fwdev->speed, max_speed); 1321113584Ssimokawa xfer->fc = sdev->target->sbp->fd.fc; 1322113584Ssimokawa xfer->retry_req = fw_asybusy; 1323103285Sikob } 1324120660Ssimokawa 1325120660Ssimokawa if (tcode == FWTCODE_WREQB) 1326120660Ssimokawa xfer->send.pay_len = 8; 1327120660Ssimokawa else 1328120660Ssimokawa xfer->send.pay_len = 0; 1329120660Ssimokawa 1330103285Sikob xfer->sc = (caddr_t)sdev; 1331120660Ssimokawa fp = &xfer->send.hdr; 1332113584Ssimokawa fp->mode.wreqq.dest_hi = sdev->login->cmd_hi; 1333113584Ssimokawa fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset; 1334103285Sikob fp->mode.wreqq.tlrt = 0; 1335103285Sikob fp->mode.wreqq.tcode = tcode; 1336103285Sikob fp->mode.wreqq.pri = 0; 1337120660Ssimokawa fp->mode.wreqq.dst = FWLOCALBUS | sdev->target->fwdev->dst; 1338103285Sikob 1339103285Sikob return xfer; 1340103285Sikob 1341103285Sikob} 1342103285Sikob 1343103285Sikobstatic void 1344111615Ssimokawasbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb) 1345103285Sikob{ 1346103285Sikob struct fw_xfer *xfer; 1347103285Sikob struct fw_pkt *fp; 1348103285Sikob struct sbp_ocb *ocb; 1349111615Ssimokawa struct sbp_target *target; 1350103285Sikob int s, nid; 1351103285Sikob 1352111615Ssimokawa target = sdev->target; 1353111615Ssimokawa nid = target->sbp->fd.fc->nodeid | FWLOCALBUS; 1354111615Ssimokawa 1355111615Ssimokawa s = splfw(); 1356111615Ssimokawa if (func == ORB_FUN_RUNQUEUE) { 1357111615Ssimokawa ocb = STAILQ_FIRST(&target->mgm_ocb_queue); 1358111615Ssimokawa if (target->mgm_ocb_cur != NULL || ocb == NULL) { 1359111615Ssimokawa splx(s); 1360111615Ssimokawa return; 1361111615Ssimokawa } 1362111615Ssimokawa STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb); 1363111615Ssimokawa goto start; 1364111615Ssimokawa } 1365113584Ssimokawa if ((ocb = sbp_get_ocb(sdev)) == NULL) { 1366103285Sikob splx(s); 1367122528Ssimokawa /* XXX */ 1368103285Sikob return; 1369103285Sikob } 1370103285Sikob ocb->flags = OCB_ACT_MGM; 1371103285Sikob ocb->sdev = sdev; 1372103285Sikob 1373120660Ssimokawa bzero((void *)ocb->orb, sizeof(ocb->orb)); 1374103285Sikob ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI); 1375120660Ssimokawa ocb->orb[7] = htonl(SBP_DEV2ADDR(target->target_id, sdev->lun_id)); 1376103285Sikob 1377107653SsimokawaSBP_DEBUG(0) 1378103285Sikob sbp_show_sdev_info(sdev, 2); 1379103285Sikob printf("%s\n", orb_fun_name[(func>>16)&0xf]); 1380107653SsimokawaEND_DEBUG 1381103285Sikob switch (func) { 1382103285Sikob case ORB_FUN_LGI: 1383120660Ssimokawa ocb->orb[0] = ocb->orb[1] = 0; /* password */ 1384103285Sikob ocb->orb[2] = htonl(nid << 16); 1385113584Ssimokawa ocb->orb[3] = htonl(sdev->dma.bus_addr); 1386121792Ssimokawa ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id); 1387122387Ssimokawa if (ex_login) 1388121792Ssimokawa ocb->orb[4] |= htonl(ORB_EXV); 1389113584Ssimokawa ocb->orb[5] = htonl(SBP_LOGIN_SIZE); 1390113584Ssimokawa fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD); 1391103285Sikob break; 1392110336Ssimokawa case ORB_FUN_ATA: 1393111615Ssimokawa ocb->orb[0] = htonl((0 << 16) | 0); 1394113584Ssimokawa ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff); 1395110336Ssimokawa /* fall through */ 1396103285Sikob case ORB_FUN_RCN: 1397103285Sikob case ORB_FUN_LGO: 1398103285Sikob case ORB_FUN_LUR: 1399103285Sikob case ORB_FUN_RST: 1400103285Sikob case ORB_FUN_ATS: 1401113584Ssimokawa ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id); 1402103285Sikob break; 1403103285Sikob } 1404103285Sikob 1405111615Ssimokawa if (target->mgm_ocb_cur != NULL) { 1406111615Ssimokawa /* there is a standing ORB */ 1407111615Ssimokawa STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb); 1408111615Ssimokawa splx(s); 1409111615Ssimokawa return; 1410111615Ssimokawa } 1411111615Ssimokawastart: 1412111615Ssimokawa target->mgm_ocb_cur = ocb; 1413111615Ssimokawa splx(s); 1414111615Ssimokawa 1415111615Ssimokawa callout_reset(&target->mgm_ocb_timeout, 5*hz, 1416114732Ssimokawa sbp_mgm_timeout, (caddr_t)ocb); 1417103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0); 1418103285Sikob if(xfer == NULL){ 1419103285Sikob return; 1420103285Sikob } 1421114732Ssimokawa xfer->act.hand = sbp_mgm_callback; 1422103285Sikob 1423120660Ssimokawa fp = &xfer->send.hdr; 1424113584Ssimokawa fp->mode.wreqb.dest_hi = sdev->target->mgm_hi; 1425113584Ssimokawa fp->mode.wreqb.dest_lo = sdev->target->mgm_lo; 1426113584Ssimokawa fp->mode.wreqb.len = 8; 1427103285Sikob fp->mode.wreqb.extcode = 0; 1428120660Ssimokawa xfer->send.payload[0] = htonl(nid << 16); 1429120660Ssimokawa xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff); 1430120660SsimokawaSBP_DEBUG(0) 1431120660Ssimokawa sbp_show_sdev_info(sdev, 2); 1432129585Sdfr printf("mgm orb: %08x\n", (uint32_t)ocb->bus_addr); 1433120660SsimokawaEND_DEBUG 1434103285Sikob 1435103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1436103285Sikob} 1437103285Sikob 1438103285Sikobstatic void 1439105792Ssimokawasbp_print_scsi_cmd(struct sbp_ocb *ocb) 1440103285Sikob{ 1441103285Sikob struct ccb_scsiio *csio; 1442103285Sikob 1443103285Sikob csio = &ocb->ccb->csio; 1444103285Sikob printf("%s:%d:%d XPT_SCSI_IO: " 1445103285Sikob "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" 1446103285Sikob ", flags: 0x%02x, " 1447103285Sikob "%db cmd/%db data/%db sense\n", 1448103285Sikob device_get_nameunit(ocb->sdev->target->sbp->fd.dev), 1449103285Sikob ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun, 1450103285Sikob csio->cdb_io.cdb_bytes[0], 1451103285Sikob csio->cdb_io.cdb_bytes[1], 1452103285Sikob csio->cdb_io.cdb_bytes[2], 1453103285Sikob csio->cdb_io.cdb_bytes[3], 1454103285Sikob csio->cdb_io.cdb_bytes[4], 1455103285Sikob csio->cdb_io.cdb_bytes[5], 1456103285Sikob csio->cdb_io.cdb_bytes[6], 1457103285Sikob csio->cdb_io.cdb_bytes[7], 1458103285Sikob csio->cdb_io.cdb_bytes[8], 1459103285Sikob csio->cdb_io.cdb_bytes[9], 1460103285Sikob ocb->ccb->ccb_h.flags & CAM_DIR_MASK, 1461103285Sikob csio->cdb_len, csio->dxfer_len, 1462103285Sikob csio->sense_len); 1463105792Ssimokawa} 1464105792Ssimokawa 1465105792Ssimokawastatic void 1466105792Ssimokawasbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb) 1467105792Ssimokawa{ 1468105792Ssimokawa struct sbp_cmd_status *sbp_cmd_status; 1469105792Ssimokawa struct scsi_sense_data *sense; 1470105792Ssimokawa 1471105792Ssimokawa sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data; 1472105792Ssimokawa sense = &ocb->ccb->csio.sense_data; 1473105792Ssimokawa 1474105792SsimokawaSBP_DEBUG(0) 1475105792Ssimokawa sbp_print_scsi_cmd(ocb); 1476103285Sikob /* XXX need decode status */ 1477103285Sikob sbp_show_sdev_info(ocb->sdev, 2); 1478113584Ssimokawa printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n", 1479103285Sikob sbp_cmd_status->status, 1480103285Sikob sbp_cmd_status->sfmt, 1481103285Sikob sbp_cmd_status->valid, 1482103285Sikob sbp_cmd_status->s_key, 1483103285Sikob sbp_cmd_status->s_code, 1484103285Sikob sbp_cmd_status->s_qlfr, 1485103285Sikob sbp_status->len 1486103285Sikob ); 1487103285SikobEND_DEBUG 1488103285Sikob 1489110071Ssimokawa switch (sbp_cmd_status->status) { 1490110071Ssimokawa case SCSI_STATUS_CHECK_COND: 1491110071Ssimokawa case SCSI_STATUS_BUSY: 1492110071Ssimokawa case SCSI_STATUS_CMD_TERMINATED: 1493103285Sikob if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){ 1494103285Sikob sense->error_code = SSD_CURRENT_ERROR; 1495103285Sikob }else{ 1496103285Sikob sense->error_code = SSD_DEFERRED_ERROR; 1497103285Sikob } 1498103285Sikob if(sbp_cmd_status->valid) 1499103285Sikob sense->error_code |= SSD_ERRCODE_VALID; 1500103285Sikob sense->flags = sbp_cmd_status->s_key; 1501103285Sikob if(sbp_cmd_status->mark) 1502103285Sikob sense->flags |= SSD_FILEMARK; 1503103285Sikob if(sbp_cmd_status->eom) 1504103285Sikob sense->flags |= SSD_EOM; 1505103285Sikob if(sbp_cmd_status->ill_len) 1506103285Sikob sense->flags |= SSD_ILI; 1507119556Ssimokawa 1508119556Ssimokawa bcopy(&sbp_cmd_status->info, &sense->info[0], 4); 1509119556Ssimokawa 1510103285Sikob if (sbp_status->len <= 1) 1511103285Sikob /* XXX not scsi status. shouldn't be happened */ 1512103285Sikob sense->extra_len = 0; 1513103285Sikob else if (sbp_status->len <= 4) 1514103285Sikob /* add_sense_code(_qual), info, cmd_spec_info */ 1515103285Sikob sense->extra_len = 6; 1516103285Sikob else 1517103285Sikob /* fru, sense_key_spec */ 1518103285Sikob sense->extra_len = 10; 1519119556Ssimokawa 1520119556Ssimokawa bcopy(&sbp_cmd_status->cdb, &sense->cmd_spec_info[0], 4); 1521119556Ssimokawa 1522103285Sikob sense->add_sense_code = sbp_cmd_status->s_code; 1523103285Sikob sense->add_sense_code_qual = sbp_cmd_status->s_qlfr; 1524103285Sikob sense->fru = sbp_cmd_status->fru; 1525103285Sikob 1526119556Ssimokawa bcopy(&sbp_cmd_status->s_keydep[0], 1527119556Ssimokawa &sense->sense_key_spec[0], 3); 1528119556Ssimokawa 1529103285Sikob ocb->ccb->csio.scsi_status = sbp_cmd_status->status;; 1530103285Sikob ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR 1531103285Sikob | CAM_AUTOSNS_VALID; 1532103285Sikob/* 1533103285Sikob{ 1534129585Sdfr uint8_t j, *tmp; 1535103285Sikob tmp = sense; 1536103285Sikob for( j = 0 ; j < 32 ; j+=8){ 1537103285Sikob printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n", 1538103285Sikob tmp[j], tmp[j+1], tmp[j+2], tmp[j+3], 1539103285Sikob tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]); 1540103285Sikob } 1541103285Sikob 1542103285Sikob} 1543103285Sikob*/ 1544110071Ssimokawa break; 1545110071Ssimokawa default: 1546110071Ssimokawa sbp_show_sdev_info(ocb->sdev, 2); 1547110071Ssimokawa printf("sbp_scsi_status: unknown scsi status 0x%x\n", 1548110071Ssimokawa sbp_cmd_status->status); 1549103285Sikob } 1550103285Sikob} 1551103285Sikob 1552103285Sikobstatic void 1553103285Sikobsbp_fix_inq_data(struct sbp_ocb *ocb) 1554103285Sikob{ 1555103285Sikob union ccb *ccb; 1556103285Sikob struct sbp_dev *sdev; 1557103285Sikob struct scsi_inquiry_data *inq; 1558103285Sikob 1559103285Sikob ccb = ocb->ccb; 1560103285Sikob sdev = ocb->sdev; 1561103285Sikob 1562103285Sikob if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD) 1563103285Sikob return; 1564103285SikobSBP_DEBUG(1) 1565103285Sikob sbp_show_sdev_info(sdev, 2); 1566103285Sikob printf("sbp_fix_inq_data\n"); 1567103285SikobEND_DEBUG 1568103285Sikob inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr; 1569103285Sikob switch (SID_TYPE(inq)) { 1570103285Sikob case T_DIRECT: 1571118291Ssimokawa#if 0 1572103285Sikob /* 1573103285Sikob * XXX Convert Direct Access device to RBC. 1574108281Ssimokawa * I've never seen FireWire DA devices which support READ_6. 1575103285Sikob */ 1576103285Sikob if (SID_TYPE(inq) == T_DIRECT) 1577103285Sikob inq->device |= T_RBC; /* T_DIRECT == 0 */ 1578103285Sikob#endif 1579103285Sikob /* fall through */ 1580103285Sikob case T_RBC: 1581103285Sikob /* 1582103285Sikob * Override vendor/product/revision information. 1583103285Sikob * Some devices sometimes return strange strings. 1584103285Sikob */ 1585111615Ssimokawa#if 1 1586103285Sikob bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor)); 1587103285Sikob bcopy(sdev->product, inq->product, sizeof(inq->product)); 1588103285Sikob bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision)); 1589111615Ssimokawa#endif 1590103285Sikob break; 1591103285Sikob } 1592127468Ssimokawa /* 1593127468Ssimokawa * Force to enable/disable tagged queuing. 1594127468Ssimokawa * XXX CAM also checks SCP_QUEUE_DQUE flag in the control mode page. 1595127468Ssimokawa */ 1596127468Ssimokawa if (sbp_tags > 0) 1597127468Ssimokawa inq->flags |= SID_CmdQue; 1598127468Ssimokawa else if (sbp_tags < 0) 1599127468Ssimokawa inq->flags &= ~SID_CmdQue; 1600127468Ssimokawa 1601103285Sikob} 1602103285Sikob 1603103285Sikobstatic void 1604113584Ssimokawasbp_recv1(struct fw_xfer *xfer) 1605113584Ssimokawa{ 1606103285Sikob struct fw_pkt *rfp; 1607103285Sikob#if NEED_RESPONSE 1608103285Sikob struct fw_pkt *sfp; 1609103285Sikob#endif 1610103285Sikob struct sbp_softc *sbp; 1611103285Sikob struct sbp_dev *sdev; 1612103285Sikob struct sbp_ocb *ocb; 1613103285Sikob struct sbp_login_res *login_res = NULL; 1614103285Sikob struct sbp_status *sbp_status; 1615103285Sikob struct sbp_target *target; 1616111704Ssimokawa int orb_fun, status_valid0, status_valid, t, l, reset_agent = 0; 1617129585Sdfr uint32_t addr; 1618103285Sikob/* 1619129585Sdfr uint32_t *ld; 1620103285Sikob ld = xfer->recv.buf; 1621103285Sikobprintf("sbp %x %d %d %08x %08x %08x %08x\n", 1622103285Sikob xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3])); 1623103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 1624103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11])); 1625103285Sikob*/ 1626113584Ssimokawa sbp = (struct sbp_softc *)xfer->sc; 1627120660Ssimokawa if (xfer->resp != 0){ 1628122529Ssimokawa printf("sbp_recv: xfer->resp = %d\n", xfer->resp); 1629113584Ssimokawa goto done0; 1630103285Sikob } 1631120660Ssimokawa if (xfer->recv.payload == NULL){ 1632120660Ssimokawa printf("sbp_recv: xfer->recv.payload == NULL\n"); 1633113584Ssimokawa goto done0; 1634103285Sikob } 1635120660Ssimokawa rfp = &xfer->recv.hdr; 1636103285Sikob if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){ 1637103285Sikob printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode); 1638113584Ssimokawa goto done0; 1639103285Sikob } 1640120660Ssimokawa sbp_status = (struct sbp_status *)xfer->recv.payload; 1641113584Ssimokawa addr = rfp->mode.wreqb.dest_lo; 1642103285SikobSBP_DEBUG(2) 1643103285Sikob printf("received address 0x%x\n", addr); 1644103285SikobEND_DEBUG 1645110189Ssimokawa t = SBP_ADDR2TRG(addr); 1646110189Ssimokawa if (t >= SBP_NUM_TARGETS) { 1647110189Ssimokawa device_printf(sbp->fd.dev, 1648110189Ssimokawa "sbp_recv1: invalid target %d\n", t); 1649113584Ssimokawa goto done0; 1650110189Ssimokawa } 1651110189Ssimokawa target = &sbp->targets[t]; 1652110189Ssimokawa l = SBP_ADDR2LUN(addr); 1653121185Ssimokawa if (l >= target->num_lun || target->luns[l] == NULL) { 1654110189Ssimokawa device_printf(sbp->fd.dev, 1655110189Ssimokawa "sbp_recv1: invalid lun %d (target=%d)\n", l, t); 1656113584Ssimokawa goto done0; 1657110189Ssimokawa } 1658121185Ssimokawa sdev = target->luns[l]; 1659103285Sikob 1660110189Ssimokawa ocb = NULL; 1661110189Ssimokawa switch (sbp_status->src) { 1662110189Ssimokawa case 0: 1663110189Ssimokawa case 1: 1664111615Ssimokawa /* check mgm_ocb_cur first */ 1665111615Ssimokawa ocb = target->mgm_ocb_cur; 1666111615Ssimokawa if (ocb != NULL) { 1667111615Ssimokawa if (OCB_MATCH(ocb, sbp_status)) { 1668111615Ssimokawa callout_stop(&target->mgm_ocb_timeout); 1669111615Ssimokawa target->mgm_ocb_cur = NULL; 1670111615Ssimokawa break; 1671111615Ssimokawa } 1672111615Ssimokawa } 1673111615Ssimokawa ocb = sbp_dequeue_ocb(sdev, sbp_status); 1674110189Ssimokawa if (ocb == NULL) { 1675110189Ssimokawa sbp_show_sdev_info(sdev, 2); 1676127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 1677127468Ssimokawa printf("No ocb(%lx) on the queue\n", 1678127468Ssimokawa#else 1679111819Ssimokawa printf("No ocb(%x) on the queue\n", 1680111858Ssimokawa#endif 1681111819Ssimokawa ntohl(sbp_status->orb_lo)); 1682110189Ssimokawa } 1683110189Ssimokawa break; 1684110189Ssimokawa case 2: 1685110189Ssimokawa /* unsolicit */ 1686110189Ssimokawa sbp_show_sdev_info(sdev, 2); 1687110189Ssimokawa printf("unsolicit status received\n"); 1688110189Ssimokawa break; 1689110189Ssimokawa default: 1690110189Ssimokawa sbp_show_sdev_info(sdev, 2); 1691110189Ssimokawa printf("unknown sbp_status->src\n"); 1692110189Ssimokawa } 1693110189Ssimokawa 1694111615Ssimokawa status_valid0 = (sbp_status->src < 2 1695110189Ssimokawa && sbp_status->resp == ORB_RES_CMPL 1696111615Ssimokawa && sbp_status->dead == 0); 1697111615Ssimokawa status_valid = (status_valid0 && sbp_status->status == 0); 1698103285Sikob 1699121185Ssimokawa if (!status_valid0 || debug > 2){ 1700103285Sikob int status; 1701110129SsimokawaSBP_DEBUG(0) 1702103285Sikob sbp_show_sdev_info(sdev, 2); 1703103285Sikob printf("ORB status src:%x resp:%x dead:%x" 1704127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 1705127468Ssimokawa " len:%x stat:%x orb:%x%08lx\n", 1706127468Ssimokawa#else 1707103285Sikob " len:%x stat:%x orb:%x%08x\n", 1708108712Ssimokawa#endif 1709103285Sikob sbp_status->src, sbp_status->resp, sbp_status->dead, 1710103285Sikob sbp_status->len, sbp_status->status, 1711108280Ssimokawa ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo)); 1712110129SsimokawaEND_DEBUG 1713103285Sikob sbp_show_sdev_info(sdev, 2); 1714103285Sikob status = sbp_status->status; 1715103285Sikob switch(sbp_status->resp) { 1716103285Sikob case 0: 1717103285Sikob if (status > MAX_ORB_STATUS0) 1718103285Sikob printf("%s\n", orb_status0[MAX_ORB_STATUS0]); 1719110189Ssimokawa else 1720110129Ssimokawa printf("%s\n", orb_status0[status]); 1721103285Sikob break; 1722103285Sikob case 1: 1723110336Ssimokawa printf("Obj: %s, Error: %s\n", 1724103285Sikob orb_status1_object[(status>>6) & 3], 1725103285Sikob orb_status1_serial_bus_error[status & 0xf]); 1726103285Sikob break; 1727110129Ssimokawa case 2: 1728110129Ssimokawa printf("Illegal request\n"); 1729110129Ssimokawa break; 1730110129Ssimokawa case 3: 1731110129Ssimokawa printf("Vendor dependent\n"); 1732110129Ssimokawa break; 1733103285Sikob default: 1734110129Ssimokawa printf("unknown respose code %d\n", sbp_status->resp); 1735103285Sikob } 1736103285Sikob } 1737103285Sikob 1738103285Sikob /* we have to reset the fetch agent if it's dead */ 1739103285Sikob if (sbp_status->dead) { 1740111615Ssimokawa if (sdev->path) { 1741103285Sikob xpt_freeze_devq(sdev->path, 1); 1742111615Ssimokawa sdev->freeze ++; 1743111615Ssimokawa } 1744111704Ssimokawa reset_agent = 1; 1745103285Sikob } 1746103285Sikob 1747111704Ssimokawa if (ocb == NULL) 1748111704Ssimokawa goto done; 1749103285Sikob 1750103285Sikob switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){ 1751103285Sikob case ORB_FMT_NOP: 1752103285Sikob break; 1753103285Sikob case ORB_FMT_VED: 1754103285Sikob break; 1755103285Sikob case ORB_FMT_STD: 1756111819Ssimokawa switch(ocb->flags) { 1757103285Sikob case OCB_ACT_MGM: 1758103285Sikob orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK; 1759120842Ssimokawa reset_agent = 0; 1760103285Sikob switch(orb_fun) { 1761103285Sikob case ORB_FUN_LGI: 1762113584Ssimokawa fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD); 1763113584Ssimokawa login_res = sdev->login; 1764103285Sikob login_res->len = ntohs(login_res->len); 1765103285Sikob login_res->id = ntohs(login_res->id); 1766103285Sikob login_res->cmd_hi = ntohs(login_res->cmd_hi); 1767103285Sikob login_res->cmd_lo = ntohl(login_res->cmd_lo); 1768103285Sikob if (status_valid) { 1769103285SikobSBP_DEBUG(0) 1770103285Sikobsbp_show_sdev_info(sdev, 2); 1771103285Sikobprintf("login: len %d, ID %d, cmd %08x%08x, recon_hold %d\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo, ntohs(login_res->recon_hold)); 1772103285SikobEND_DEBUG 1773103285Sikob sbp_busy_timeout(sdev); 1774103285Sikob } else { 1775110336Ssimokawa /* forgot logout? */ 1776110336Ssimokawa sbp_show_sdev_info(sdev, 2); 1777103285Sikob printf("login failed\n"); 1778103285Sikob sdev->status = SBP_DEV_RESET; 1779103285Sikob } 1780103285Sikob break; 1781103285Sikob case ORB_FUN_RCN: 1782113584Ssimokawa login_res = sdev->login; 1783103285Sikob if (status_valid) { 1784103285SikobSBP_DEBUG(0) 1785103285Sikobsbp_show_sdev_info(sdev, 2); 1786103285Sikobprintf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo); 1787103285SikobEND_DEBUG 1788103285Sikob#if 1 1789111615Ssimokawa if (sdev->status == SBP_DEV_ATTACHED) 1790111615Ssimokawa sbp_scan_dev(sdev); 1791111615Ssimokawa else 1792111615Ssimokawa sbp_agent_reset(sdev); 1793103285Sikob#else 1794110336Ssimokawa sdev->status = SBP_DEV_ATTACHED; 1795111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL); 1796103285Sikob#endif 1797103285Sikob } else { 1798103285Sikob /* reconnection hold time exceed? */ 1799110336SsimokawaSBP_DEBUG(0) 1800110336Ssimokawa sbp_show_sdev_info(sdev, 2); 1801103285Sikob printf("reconnect failed\n"); 1802110336SsimokawaEND_DEBUG 1803111615Ssimokawa sbp_login(sdev); 1804103285Sikob } 1805103285Sikob break; 1806103285Sikob case ORB_FUN_LGO: 1807103285Sikob sdev->status = SBP_DEV_RESET; 1808103285Sikob break; 1809110336Ssimokawa case ORB_FUN_RST: 1810110336Ssimokawa sbp_busy_timeout(sdev); 1811110336Ssimokawa break; 1812103285Sikob case ORB_FUN_LUR: 1813103285Sikob case ORB_FUN_ATA: 1814103285Sikob case ORB_FUN_ATS: 1815110336Ssimokawa sbp_agent_reset(sdev); 1816103285Sikob break; 1817103285Sikob default: 1818110336Ssimokawa sbp_show_sdev_info(sdev, 2); 1819110336Ssimokawa printf("unknown function %d\n", orb_fun); 1820103285Sikob break; 1821103285Sikob } 1822111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL); 1823103285Sikob break; 1824103285Sikob case OCB_ACT_CMD: 1825114732Ssimokawa sdev->timeout = 0; 1826103285Sikob if(ocb->ccb != NULL){ 1827103285Sikob union ccb *ccb; 1828103285Sikob/* 1829129585Sdfr uint32_t *ld; 1830103285Sikob ld = ocb->ccb->csio.data_ptr; 1831103285Sikob if(ld != NULL && ocb->ccb->csio.dxfer_len != 0) 1832103285Sikob printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]); 1833103285Sikob else 1834103285Sikob printf("ptr NULL\n"); 1835103285Sikobprintf("len %d\n", sbp_status->len); 1836103285Sikob*/ 1837103285Sikob ccb = ocb->ccb; 1838103285Sikob if(sbp_status->len > 1){ 1839103285Sikob sbp_scsi_status(sbp_status, ocb); 1840103285Sikob }else{ 1841103285Sikob if(sbp_status->resp != ORB_RES_CMPL){ 1842103285Sikob ccb->ccb_h.status = CAM_REQ_CMP_ERR; 1843103285Sikob }else{ 1844103285Sikob ccb->ccb_h.status = CAM_REQ_CMP; 1845103285Sikob } 1846103285Sikob } 1847103285Sikob /* fix up inq data */ 1848103285Sikob if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY) 1849103285Sikob sbp_fix_inq_data(ocb); 1850103285Sikob xpt_done(ccb); 1851103285Sikob } 1852103285Sikob break; 1853103285Sikob default: 1854103285Sikob break; 1855103285Sikob } 1856103285Sikob } 1857103285Sikob 1858127468Ssimokawa if (!use_doorbell) 1859127468Ssimokawa sbp_free_ocb(sdev, ocb); 1860111704Ssimokawadone: 1861111704Ssimokawa if (reset_agent) 1862111704Ssimokawa sbp_agent_reset(sdev); 1863103285Sikob 1864113584Ssimokawadone0: 1865120660Ssimokawa xfer->recv.pay_len = SBP_RECV_LEN; 1866103285Sikob/* The received packet is usually small enough to be stored within 1867103285Sikob * the buffer. In that case, the controller return ack_complete and 1868103285Sikob * no respose is necessary. 1869103285Sikob * 1870103285Sikob * XXX fwohci.c and firewire.c should inform event_code such as 1871103285Sikob * ack_complete or ack_pending to upper driver. 1872103285Sikob */ 1873103285Sikob#if NEED_RESPONSE 1874103285Sikob xfer->send.off = 0; 1875103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 1876103285Sikob sfp->mode.wres.dst = rfp->mode.wreqb.src; 1877113584Ssimokawa xfer->dst = sfp->mode.wres.dst; 1878103285Sikob xfer->spd = min(sdev->target->fwdev->speed, max_speed); 1879103285Sikob xfer->act.hand = sbp_loginres_callback; 1880103285Sikob xfer->retry_req = fw_asybusy; 1881103285Sikob 1882103285Sikob sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt; 1883103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 1884103285Sikob sfp->mode.wres.rtcode = 0; 1885103285Sikob sfp->mode.wres.pri = 0; 1886103285Sikob 1887103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1888103285Sikob#else 1889113584Ssimokawa /* recycle */ 1890113584Ssimokawa STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link); 1891103285Sikob#endif 1892103285Sikob 1893103285Sikob return; 1894103285Sikob 1895103285Sikob} 1896103285Sikob 1897103285Sikobstatic void 1898103285Sikobsbp_recv(struct fw_xfer *xfer) 1899103285Sikob{ 1900103285Sikob int s; 1901103285Sikob 1902103285Sikob s = splcam(); 1903103285Sikob sbp_recv1(xfer); 1904103285Sikob splx(s); 1905103285Sikob} 1906103285Sikob/* 1907103285Sikob * sbp_attach() 1908103285Sikob */ 1909103285Sikobstatic int 1910103285Sikobsbp_attach(device_t dev) 1911103285Sikob{ 1912103285Sikob struct sbp_softc *sbp; 1913103285Sikob struct cam_devq *devq; 1914103285Sikob struct fw_xfer *xfer; 1915103285Sikob int i, s, error; 1916103285Sikob 1917103285SikobSBP_DEBUG(0) 1918111199Ssimokawa printf("sbp_attach (cold=%d)\n", cold); 1919103285SikobEND_DEBUG 1920103285Sikob 1921111199Ssimokawa if (cold) 1922111199Ssimokawa sbp_cold ++; 1923103285Sikob sbp = ((struct sbp_softc *)device_get_softc(dev)); 1924103285Sikob bzero(sbp, sizeof(struct sbp_softc)); 1925103285Sikob sbp->fd.dev = dev; 1926103285Sikob sbp->fd.fc = device_get_ivars(dev); 1927124251Ssimokawa 1928124251Ssimokawa if (max_speed < 0) 1929124251Ssimokawa max_speed = sbp->fd.fc->speed; 1930124251Ssimokawa 1931113584Ssimokawa error = bus_dma_tag_create(/*parent*/sbp->fd.fc->dmat, 1932113584Ssimokawa /* XXX shoud be 4 for sane backend? */ 1933113584Ssimokawa /*alignment*/1, 1934103285Sikob /*boundary*/0, 1935103285Sikob /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 1936103285Sikob /*highaddr*/BUS_SPACE_MAXADDR, 1937103285Sikob /*filter*/NULL, /*filterarg*/NULL, 1938103285Sikob /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX, 1939113474Ssimokawa /*maxsegsz*/SBP_SEG_MAX, 1940103285Sikob /*flags*/BUS_DMA_ALLOCNOW, 1941127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 501102 1942117126Sscottl /*lockfunc*/busdma_lock_mutex, 1943117228Ssimokawa /*lockarg*/&Giant, 1944117228Ssimokawa#endif 1945117228Ssimokawa &sbp->dmat); 1946103285Sikob if (error != 0) { 1947103285Sikob printf("sbp_attach: Could not allocate DMA tag " 1948103285Sikob "- error %d\n", error); 1949103285Sikob return (ENOMEM); 1950103285Sikob } 1951103285Sikob 1952103285Sikob devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB); 1953103285Sikob if (devq == NULL) 1954103285Sikob return (ENXIO); 1955103285Sikob 1956103285Sikob for( i = 0 ; i < SBP_NUM_TARGETS ; i++){ 1957103285Sikob sbp->targets[i].fwdev = NULL; 1958103285Sikob sbp->targets[i].luns = NULL; 1959103285Sikob } 1960103285Sikob 1961103285Sikob sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp, 1962103285Sikob device_get_unit(dev), 1963111615Ssimokawa /*untagged*/ 1, 1964122528Ssimokawa /*tagged*/ SBP_QUEUE_LEN - 1, 1965111615Ssimokawa devq); 1966103285Sikob 1967103285Sikob if (sbp->sim == NULL) { 1968103285Sikob cam_simq_free(devq); 1969103285Sikob return (ENXIO); 1970103285Sikob } 1971103285Sikob 1972103285Sikob 1973111615Ssimokawa if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS) 1974111615Ssimokawa goto fail; 1975103285Sikob 1976111615Ssimokawa if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim), 1977120660Ssimokawa CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 1978120660Ssimokawa xpt_bus_deregister(cam_sim_path(sbp->sim)); 1979111615Ssimokawa goto fail; 1980120660Ssimokawa } 1981111615Ssimokawa 1982103285Sikob /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */ 1983120660Ssimokawa sbp->fwb.start = ((u_int64_t)SBP_BIND_HI << 32) | SBP_DEV2ADDR(0, 0); 1984120660Ssimokawa sbp->fwb.end = sbp->fwb.start + 0xffff; 1985113584Ssimokawa sbp->fwb.act_type = FWACT_XFER; 1986113584Ssimokawa /* pre-allocate xfer */ 1987113584Ssimokawa STAILQ_INIT(&sbp->fwb.xferlist); 1988113584Ssimokawa for (i = 0; i < SBP_NUM_OCB/2; i ++) { 1989113584Ssimokawa xfer = fw_xfer_alloc_buf(M_SBP, 1990113584Ssimokawa /* send */0, 1991113584Ssimokawa /* recv */SBP_RECV_LEN); 1992113584Ssimokawa xfer->act.hand = sbp_recv; 1993113584Ssimokawa#if NEED_RESPONSE 1994113584Ssimokawa xfer->fc = sbp->fd.fc; 1995113584Ssimokawa#endif 1996113584Ssimokawa xfer->sc = (caddr_t)sbp; 1997113584Ssimokawa STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link); 1998113584Ssimokawa } 1999103285Sikob fw_bindadd(sbp->fd.fc, &sbp->fwb); 2000103285Sikob 2001113584Ssimokawa sbp->fd.post_busreset = sbp_post_busreset; 2002103285Sikob sbp->fd.post_explore = sbp_post_explore; 2003103285Sikob 2004111199Ssimokawa if (sbp->fd.fc->status != -1) { 2005111199Ssimokawa s = splfw(); 2006122387Ssimokawa sbp_post_busreset((void *)sbp); 2007111199Ssimokawa sbp_post_explore((void *)sbp); 2008111199Ssimokawa splx(s); 2009111199Ssimokawa } 2010122387Ssimokawa xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL); 2011111199Ssimokawa 2012103285Sikob return (0); 2013111615Ssimokawafail: 2014111615Ssimokawa cam_sim_free(sbp->sim, /*free_devq*/TRUE); 2015111615Ssimokawa return (ENXIO); 2016103285Sikob} 2017103285Sikob 2018103285Sikobstatic int 2019110145Ssimokawasbp_logout_all(struct sbp_softc *sbp) 2020110145Ssimokawa{ 2021110145Ssimokawa struct sbp_target *target; 2022110145Ssimokawa struct sbp_dev *sdev; 2023110145Ssimokawa int i, j; 2024110145Ssimokawa 2025110145SsimokawaSBP_DEBUG(0) 2026110145Ssimokawa printf("sbp_logout_all\n"); 2027110145SsimokawaEND_DEBUG 2028110145Ssimokawa for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) { 2029110145Ssimokawa target = &sbp->targets[i]; 2030110145Ssimokawa if (target->luns == NULL) 2031110145Ssimokawa continue; 2032110145Ssimokawa for (j = 0; j < target->num_lun; j++) { 2033121185Ssimokawa sdev = target->luns[j]; 2034121185Ssimokawa if (sdev == NULL) 2035121185Ssimokawa continue; 2036113584Ssimokawa callout_stop(&sdev->login_callout); 2037110336Ssimokawa if (sdev->status >= SBP_DEV_TOATTACH && 2038110336Ssimokawa sdev->status <= SBP_DEV_ATTACHED) 2039111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL); 2040110145Ssimokawa } 2041110145Ssimokawa } 2042113584Ssimokawa 2043110145Ssimokawa return 0; 2044110145Ssimokawa} 2045110145Ssimokawa 2046110145Ssimokawastatic int 2047110145Ssimokawasbp_shutdown(device_t dev) 2048110145Ssimokawa{ 2049110145Ssimokawa struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev)); 2050110145Ssimokawa 2051110145Ssimokawa sbp_logout_all(sbp); 2052110145Ssimokawa return (0); 2053110145Ssimokawa} 2054110145Ssimokawa 2055121185Ssimokawastatic void 2056121185Ssimokawasbp_free_sdev(struct sbp_dev *sdev) 2057121185Ssimokawa{ 2058121185Ssimokawa int i; 2059121185Ssimokawa 2060121185Ssimokawa if (sdev == NULL) 2061121185Ssimokawa return; 2062121185Ssimokawa for (i = 0; i < SBP_QUEUE_LEN; i++) 2063121185Ssimokawa bus_dmamap_destroy(sdev->target->sbp->dmat, 2064121185Ssimokawa sdev->ocb[i].dmamap); 2065121185Ssimokawa fwdma_free(sdev->target->sbp->fd.fc, &sdev->dma); 2066121185Ssimokawa free(sdev, M_SBP); 2067121185Ssimokawa} 2068121185Ssimokawa 2069121185Ssimokawastatic void 2070121185Ssimokawasbp_free_target(struct sbp_target *target) 2071121185Ssimokawa{ 2072121185Ssimokawa struct sbp_softc *sbp; 2073121185Ssimokawa struct fw_xfer *xfer, *next; 2074121185Ssimokawa int i; 2075121185Ssimokawa 2076121185Ssimokawa if (target->luns == NULL) 2077121185Ssimokawa return; 2078121185Ssimokawa callout_stop(&target->mgm_ocb_timeout); 2079121185Ssimokawa sbp = target->sbp; 2080121185Ssimokawa for (i = 0; i < target->num_lun; i++) 2081121185Ssimokawa sbp_free_sdev(target->luns[i]); 2082121185Ssimokawa 2083121185Ssimokawa for (xfer = STAILQ_FIRST(&target->xferlist); 2084121185Ssimokawa xfer != NULL; xfer = next) { 2085121185Ssimokawa next = STAILQ_NEXT(xfer, link); 2086121185Ssimokawa fw_xfer_free_buf(xfer); 2087121185Ssimokawa } 2088121185Ssimokawa STAILQ_INIT(&target->xferlist); 2089121185Ssimokawa free(target->luns, M_SBP); 2090121185Ssimokawa target->num_lun = 0;; 2091121185Ssimokawa target->luns = NULL; 2092121185Ssimokawa target->fwdev = NULL; 2093121185Ssimokawa} 2094121185Ssimokawa 2095110145Ssimokawastatic int 2096103285Sikobsbp_detach(device_t dev) 2097103285Sikob{ 2098103285Sikob struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev)); 2099103285Sikob struct firewire_comm *fc = sbp->fd.fc; 2100113584Ssimokawa struct fw_xfer *xfer, *next; 2101121185Ssimokawa int i; 2102103285Sikob 2103103285SikobSBP_DEBUG(0) 2104103285Sikob printf("sbp_detach\n"); 2105103285SikobEND_DEBUG 2106113584Ssimokawa 2107103285Sikob for (i = 0; i < SBP_NUM_TARGETS; i ++) 2108110145Ssimokawa sbp_cam_detach_target(&sbp->targets[i]); 2109120660Ssimokawa xpt_async(AC_LOST_DEVICE, sbp->path, NULL); 2110111615Ssimokawa xpt_free_path(sbp->path); 2111103285Sikob xpt_bus_deregister(cam_sim_path(sbp->sim)); 2112120660Ssimokawa cam_sim_free(sbp->sim, /*free_devq*/ TRUE), 2113110145Ssimokawa 2114110145Ssimokawa sbp_logout_all(sbp); 2115113584Ssimokawa 2116110145Ssimokawa /* XXX wait for logout completion */ 2117110145Ssimokawa tsleep(&i, FWPRI, "sbpdtc", hz/2); 2118110145Ssimokawa 2119121185Ssimokawa for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) 2120121185Ssimokawa sbp_free_target(&sbp->targets[i]); 2121113584Ssimokawa 2122113584Ssimokawa for (xfer = STAILQ_FIRST(&sbp->fwb.xferlist); 2123113584Ssimokawa xfer != NULL; xfer = next) { 2124113584Ssimokawa next = STAILQ_NEXT(xfer, link); 2125120660Ssimokawa fw_xfer_free_buf(xfer); 2126113584Ssimokawa } 2127113584Ssimokawa STAILQ_INIT(&sbp->fwb.xferlist); 2128110145Ssimokawa fw_bindremove(fc, &sbp->fwb); 2129113584Ssimokawa 2130103285Sikob bus_dma_tag_destroy(sbp->dmat); 2131110145Ssimokawa 2132103285Sikob return (0); 2133103285Sikob} 2134103285Sikob 2135103285Sikobstatic void 2136121185Ssimokawasbp_cam_detach_sdev(struct sbp_dev *sdev) 2137121185Ssimokawa{ 2138121185Ssimokawa if (sdev == NULL) 2139121185Ssimokawa return; 2140121185Ssimokawa if (sdev->status == SBP_DEV_DEAD) 2141121185Ssimokawa return; 2142121185Ssimokawa if (sdev->status == SBP_DEV_RESET) 2143121185Ssimokawa return; 2144121185Ssimokawa if (sdev->path) { 2145121185Ssimokawa xpt_release_devq(sdev->path, 2146121185Ssimokawa sdev->freeze, TRUE); 2147121185Ssimokawa sdev->freeze = 0; 2148121185Ssimokawa xpt_async(AC_LOST_DEVICE, sdev->path, NULL); 2149121185Ssimokawa xpt_free_path(sdev->path); 2150121185Ssimokawa sdev->path = NULL; 2151121185Ssimokawa } 2152121185Ssimokawa sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE); 2153121185Ssimokawa} 2154121185Ssimokawa 2155121185Ssimokawastatic void 2156110145Ssimokawasbp_cam_detach_target(struct sbp_target *target) 2157103285Sikob{ 2158103285Sikob int i; 2159103285Sikob 2160103285Sikob if (target->luns != NULL) { 2161108529SsimokawaSBP_DEBUG(0) 2162103285Sikob printf("sbp_detach_target %d\n", target->target_id); 2163108529SsimokawaEND_DEBUG 2164111615Ssimokawa callout_stop(&target->scan_callout); 2165121185Ssimokawa for (i = 0; i < target->num_lun; i++) 2166121185Ssimokawa sbp_cam_detach_sdev(target->luns[i]); 2167103285Sikob } 2168103285Sikob} 2169103285Sikob 2170103285Sikobstatic void 2171114732Ssimokawasbp_target_reset(struct sbp_dev *sdev, int method) 2172114732Ssimokawa{ 2173114732Ssimokawa int i; 2174114732Ssimokawa struct sbp_target *target = sdev->target; 2175114732Ssimokawa struct sbp_dev *tsdev; 2176114732Ssimokawa 2177114732Ssimokawa for (i = 0; i < target->num_lun; i++) { 2178121185Ssimokawa tsdev = target->luns[i]; 2179121185Ssimokawa if (tsdev == NULL) 2180121185Ssimokawa continue; 2181114732Ssimokawa if (tsdev->status == SBP_DEV_DEAD) 2182114732Ssimokawa continue; 2183114732Ssimokawa if (tsdev->status == SBP_DEV_RESET) 2184114732Ssimokawa continue; 2185114732Ssimokawa xpt_freeze_devq(tsdev->path, 1); 2186114732Ssimokawa tsdev->freeze ++; 2187114732Ssimokawa sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT); 2188114732Ssimokawa if (method == 2) 2189114732Ssimokawa tsdev->status = SBP_DEV_LOGIN; 2190114732Ssimokawa } 2191114732Ssimokawa switch(method) { 2192114732Ssimokawa case 1: 2193114732Ssimokawa printf("target reset\n"); 2194114732Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RST, NULL); 2195114732Ssimokawa break; 2196114732Ssimokawa case 2: 2197114732Ssimokawa printf("reset start\n"); 2198114732Ssimokawa sbp_reset_start(sdev); 2199114732Ssimokawa break; 2200114732Ssimokawa } 2201114732Ssimokawa 2202114732Ssimokawa} 2203114732Ssimokawa 2204114732Ssimokawastatic void 2205114732Ssimokawasbp_mgm_timeout(void *arg) 2206114732Ssimokawa{ 2207114732Ssimokawa struct sbp_ocb *ocb = (struct sbp_ocb *)arg; 2208114732Ssimokawa struct sbp_dev *sdev = ocb->sdev; 2209114732Ssimokawa struct sbp_target *target = sdev->target; 2210114732Ssimokawa 2211114732Ssimokawa sbp_show_sdev_info(sdev, 2); 2212120842Ssimokawa printf("request timeout(mgm orb:0x%08x) ... ", 2213129585Sdfr (uint32_t)ocb->bus_addr); 2214114732Ssimokawa target->mgm_ocb_cur = NULL; 2215114732Ssimokawa sbp_free_ocb(sdev, ocb); 2216114732Ssimokawa#if 0 2217114732Ssimokawa /* XXX */ 2218121185Ssimokawa printf("run next request\n"); 2219114732Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL); 2220114732Ssimokawa#endif 2221121185Ssimokawa#if 1 2222121185Ssimokawa printf("reset start\n"); 2223114732Ssimokawa sbp_reset_start(sdev); 2224114732Ssimokawa#endif 2225114732Ssimokawa} 2226114732Ssimokawa 2227114732Ssimokawastatic void 2228103285Sikobsbp_timeout(void *arg) 2229103285Sikob{ 2230103285Sikob struct sbp_ocb *ocb = (struct sbp_ocb *)arg; 2231103285Sikob struct sbp_dev *sdev = ocb->sdev; 2232103285Sikob 2233103285Sikob sbp_show_sdev_info(sdev, 2); 2234120842Ssimokawa printf("request timeout(cmd orb:0x%08x) ... ", 2235129585Sdfr (uint32_t)ocb->bus_addr); 2236103285Sikob 2237114732Ssimokawa sdev->timeout ++; 2238114732Ssimokawa switch(sdev->timeout) { 2239114732Ssimokawa case 1: 2240110336Ssimokawa printf("agent reset\n"); 2241114732Ssimokawa xpt_freeze_devq(sdev->path, 1); 2242114732Ssimokawa sdev->freeze ++; 2243114732Ssimokawa sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT); 2244110336Ssimokawa sbp_agent_reset(sdev); 2245114732Ssimokawa break; 2246114732Ssimokawa case 2: 2247114732Ssimokawa case 3: 2248114732Ssimokawa sbp_target_reset(sdev, sdev->timeout - 1); 2249114732Ssimokawa break; 2250114732Ssimokawa#if 0 2251114732Ssimokawa default: 2252114732Ssimokawa /* XXX give up */ 2253114732Ssimokawa sbp_cam_detach_target(target); 2254114732Ssimokawa if (target->luns != NULL) 2255114732Ssimokawa free(target->luns, M_SBP); 2256114732Ssimokawa target->num_lun = 0;; 2257114732Ssimokawa target->luns = NULL; 2258114732Ssimokawa target->fwdev = NULL; 2259114732Ssimokawa#endif 2260110336Ssimokawa } 2261103285Sikob} 2262103285Sikob 2263103285Sikobstatic void 2264103285Sikobsbp_action1(struct cam_sim *sim, union ccb *ccb) 2265103285Sikob{ 2266103285Sikob 2267103285Sikob struct sbp_softc *sbp = (struct sbp_softc *)sim->softc; 2268103285Sikob struct sbp_target *target = NULL; 2269103285Sikob struct sbp_dev *sdev = NULL; 2270103285Sikob 2271103285Sikob /* target:lun -> sdev mapping */ 2272103285Sikob if (sbp != NULL 2273103285Sikob && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD 2274103285Sikob && ccb->ccb_h.target_id < SBP_NUM_TARGETS) { 2275103285Sikob target = &sbp->targets[ccb->ccb_h.target_id]; 2276103285Sikob if (target->fwdev != NULL 2277103285Sikob && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD 2278103285Sikob && ccb->ccb_h.target_lun < target->num_lun) { 2279121185Ssimokawa sdev = target->luns[ccb->ccb_h.target_lun]; 2280121185Ssimokawa if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED && 2281103285Sikob sdev->status != SBP_DEV_PROBE) 2282103285Sikob sdev = NULL; 2283103285Sikob } 2284103285Sikob } 2285103285Sikob 2286103285SikobSBP_DEBUG(1) 2287103285Sikob if (sdev == NULL) 2288103285Sikob printf("invalid target %d lun %d\n", 2289103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2290103285SikobEND_DEBUG 2291103285Sikob 2292103285Sikob switch (ccb->ccb_h.func_code) { 2293103285Sikob case XPT_SCSI_IO: 2294103285Sikob case XPT_RESET_DEV: 2295103285Sikob case XPT_GET_TRAN_SETTINGS: 2296103285Sikob case XPT_SET_TRAN_SETTINGS: 2297103285Sikob case XPT_CALC_GEOMETRY: 2298103285Sikob if (sdev == NULL) { 2299103285SikobSBP_DEBUG(1) 2300103285Sikob printf("%s:%d:%d:func_code 0x%04x: " 2301103285Sikob "Invalid target (target needed)\n", 2302103285Sikob device_get_nameunit(sbp->fd.dev), 2303103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2304103285Sikob ccb->ccb_h.func_code); 2305103285SikobEND_DEBUG 2306103285Sikob 2307108276Ssimokawa ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2308103285Sikob xpt_done(ccb); 2309103285Sikob return; 2310103285Sikob } 2311103285Sikob break; 2312103285Sikob case XPT_PATH_INQ: 2313103285Sikob case XPT_NOOP: 2314103285Sikob /* The opcodes sometimes aimed at a target (sc is valid), 2315103285Sikob * sometimes aimed at the SIM (sc is invalid and target is 2316103285Sikob * CAM_TARGET_WILDCARD) 2317103285Sikob */ 2318103285Sikob if (sbp == NULL && 2319103285Sikob ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) { 2320103285SikobSBP_DEBUG(0) 2321103285Sikob printf("%s:%d:%d func_code 0x%04x: " 2322103285Sikob "Invalid target (no wildcard)\n", 2323103285Sikob device_get_nameunit(sbp->fd.dev), 2324103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2325103285Sikob ccb->ccb_h.func_code); 2326103285SikobEND_DEBUG 2327108276Ssimokawa ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2328103285Sikob xpt_done(ccb); 2329103285Sikob return; 2330103285Sikob } 2331103285Sikob break; 2332103285Sikob default: 2333103285Sikob /* XXX Hm, we should check the input parameters */ 2334103285Sikob break; 2335103285Sikob } 2336103285Sikob 2337103285Sikob switch (ccb->ccb_h.func_code) { 2338103285Sikob case XPT_SCSI_IO: 2339103285Sikob { 2340103285Sikob struct ccb_scsiio *csio; 2341103285Sikob struct sbp_ocb *ocb; 2342113584Ssimokawa int speed; 2343106506Ssimokawa void *cdb; 2344103285Sikob 2345103285Sikob csio = &ccb->csio; 2346103285Sikob 2347121185SsimokawaSBP_DEBUG(2) 2348103285Sikob printf("%s:%d:%d XPT_SCSI_IO: " 2349103285Sikob "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" 2350103285Sikob ", flags: 0x%02x, " 2351103285Sikob "%db cmd/%db data/%db sense\n", 2352103285Sikob device_get_nameunit(sbp->fd.dev), 2353103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2354103285Sikob csio->cdb_io.cdb_bytes[0], 2355103285Sikob csio->cdb_io.cdb_bytes[1], 2356103285Sikob csio->cdb_io.cdb_bytes[2], 2357103285Sikob csio->cdb_io.cdb_bytes[3], 2358103285Sikob csio->cdb_io.cdb_bytes[4], 2359103285Sikob csio->cdb_io.cdb_bytes[5], 2360103285Sikob csio->cdb_io.cdb_bytes[6], 2361103285Sikob csio->cdb_io.cdb_bytes[7], 2362103285Sikob csio->cdb_io.cdb_bytes[8], 2363103285Sikob csio->cdb_io.cdb_bytes[9], 2364103285Sikob ccb->ccb_h.flags & CAM_DIR_MASK, 2365103285Sikob csio->cdb_len, csio->dxfer_len, 2366103285Sikob csio->sense_len); 2367103285SikobEND_DEBUG 2368103285Sikob if(sdev == NULL){ 2369103285Sikob ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2370103285Sikob xpt_done(ccb); 2371103285Sikob return; 2372103285Sikob } 2373103285Sikob#if 0 2374103285Sikob /* if we are in probe stage, pass only probe commands */ 2375103285Sikob if (sdev->status == SBP_DEV_PROBE) { 2376103285Sikob char *name; 2377103285Sikob name = xpt_path_periph(ccb->ccb_h.path)->periph_name; 2378103285Sikob printf("probe stage, periph name: %s\n", name); 2379103285Sikob if (strcmp(name, "probe") != 0) { 2380103285Sikob ccb->ccb_h.status = CAM_REQUEUE_REQ; 2381103285Sikob xpt_done(ccb); 2382103285Sikob return; 2383103285Sikob } 2384103285Sikob } 2385103285Sikob#endif 2386122528Ssimokawa if ((ocb = sbp_get_ocb(sdev)) == NULL) { 2387122528Ssimokawa ccb->ccb_h.status = CAM_REQUEUE_REQ; 2388127468Ssimokawa if (sdev->freeze == 0) { 2389127468Ssimokawa xpt_freeze_devq(sdev->path, 1); 2390127468Ssimokawa sdev->freeze ++; 2391127468Ssimokawa } 2392122528Ssimokawa xpt_done(ccb); 2393103285Sikob return; 2394122528Ssimokawa } 2395113584Ssimokawa 2396103285Sikob ocb->flags = OCB_ACT_CMD; 2397103285Sikob ocb->sdev = sdev; 2398103285Sikob ocb->ccb = ccb; 2399103285Sikob ccb->ccb_h.ccb_sdev_ptr = sdev; 2400103285Sikob ocb->orb[0] = htonl(1 << 31); 2401103285Sikob ocb->orb[1] = 0; 2402103285Sikob ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) ); 2403113584Ssimokawa ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET); 2404103285Sikob speed = min(target->fwdev->speed, max_speed); 2405103285Sikob ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed) 2406103285Sikob | ORB_CMD_MAXP(speed + 7)); 2407103285Sikob if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){ 2408103285Sikob ocb->orb[4] |= htonl(ORB_CMD_IN); 2409103285Sikob } 2410103285Sikob 2411103285Sikob if (csio->ccb_h.flags & CAM_SCATTER_VALID) 2412103285Sikob printf("sbp: CAM_SCATTER_VALID\n"); 2413103285Sikob if (csio->ccb_h.flags & CAM_DATA_PHYS) 2414103285Sikob printf("sbp: CAM_DATA_PHYS\n"); 2415103285Sikob 2416106506Ssimokawa if (csio->ccb_h.flags & CAM_CDB_POINTER) 2417106506Ssimokawa cdb = (void *)csio->cdb_io.cdb_ptr; 2418106506Ssimokawa else 2419106506Ssimokawa cdb = (void *)&csio->cdb_io.cdb_bytes; 2420120660Ssimokawa bcopy(cdb, (void *)&ocb->orb[5], csio->cdb_len); 2421103285Sikob/* 2422103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3])); 2423103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7])); 2424103285Sikob*/ 2425103285Sikob if (ccb->csio.dxfer_len > 0) { 2426113584Ssimokawa int s, error; 2427103285Sikob 2428103285Sikob s = splsoftvm(); 2429113584Ssimokawa error = bus_dmamap_load(/*dma tag*/sbp->dmat, 2430103285Sikob /*dma map*/ocb->dmamap, 2431103285Sikob ccb->csio.data_ptr, 2432103285Sikob ccb->csio.dxfer_len, 2433103285Sikob sbp_execute_ocb, 2434103285Sikob ocb, 2435103285Sikob /*flags*/0); 2436103285Sikob splx(s); 2437113584Ssimokawa if (error) 2438113584Ssimokawa printf("sbp: bus_dmamap_load error %d\n", error); 2439103285Sikob } else 2440103285Sikob sbp_execute_ocb(ocb, NULL, 0, 0); 2441103285Sikob break; 2442103285Sikob } 2443103285Sikob case XPT_CALC_GEOMETRY: 2444103285Sikob { 2445103285Sikob struct ccb_calc_geometry *ccg; 2446127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 501100 2447129585Sdfr uint32_t size_mb; 2448129585Sdfr uint32_t secs_per_cylinder; 2449116429Ssimokawa int extended = 1; 2450116429Ssimokawa#endif 2451116351Snjl 2452103285Sikob ccg = &ccb->ccg; 2453103285Sikob if (ccg->block_size == 0) { 2454103285Sikob printf("sbp_action1: block_size is 0.\n"); 2455103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 2456103285Sikob xpt_done(ccb); 2457103285Sikob break; 2458103285Sikob } 2459103285SikobSBP_DEBUG(1) 2460103285Sikob printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: " 2461127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2462127468Ssimokawa "Volume size = %d\n", 2463127468Ssimokawa#else 2464114540Sjake "Volume size = %jd\n", 2465114927Ssimokawa#endif 2466114927Ssimokawa device_get_nameunit(sbp->fd.dev), 2467114927Ssimokawa cam_sim_path(sbp->sim), 2468103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2469127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000 2470114927Ssimokawa (uintmax_t) 2471114927Ssimokawa#endif 2472114927Ssimokawa ccg->volume_size); 2473103285SikobEND_DEBUG 2474103285Sikob 2475127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 501100 2476116429Ssimokawa size_mb = ccg->volume_size 2477116429Ssimokawa / ((1024L * 1024L) / ccg->block_size); 2478116429Ssimokawa 2479116429Ssimokawa if (size_mb > 1024 && extended) { 2480116429Ssimokawa ccg->heads = 255; 2481116429Ssimokawa ccg->secs_per_track = 63; 2482116429Ssimokawa } else { 2483116429Ssimokawa ccg->heads = 64; 2484116429Ssimokawa ccg->secs_per_track = 32; 2485116429Ssimokawa } 2486116429Ssimokawa secs_per_cylinder = ccg->heads * ccg->secs_per_track; 2487116429Ssimokawa ccg->cylinders = ccg->volume_size / secs_per_cylinder; 2488116429Ssimokawa ccb->ccb_h.status = CAM_REQ_CMP; 2489116429Ssimokawa#else 2490116351Snjl cam_calc_geometry(ccg, /*extended*/1); 2491116429Ssimokawa#endif 2492103285Sikob xpt_done(ccb); 2493103285Sikob break; 2494103285Sikob } 2495103285Sikob case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 2496103285Sikob { 2497103285Sikob 2498103285SikobSBP_DEBUG(1) 2499103285Sikob printf("%s:%d:XPT_RESET_BUS: \n", 2500103285Sikob device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim)); 2501103285SikobEND_DEBUG 2502103285Sikob 2503103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 2504103285Sikob xpt_done(ccb); 2505103285Sikob break; 2506103285Sikob } 2507103285Sikob case XPT_PATH_INQ: /* Path routing inquiry */ 2508103285Sikob { 2509103285Sikob struct ccb_pathinq *cpi = &ccb->cpi; 2510103285Sikob 2511103285SikobSBP_DEBUG(1) 2512103285Sikob printf("%s:%d:%d XPT_PATH_INQ:.\n", 2513103285Sikob device_get_nameunit(sbp->fd.dev), 2514103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2515103285SikobEND_DEBUG 2516103285Sikob cpi->version_num = 1; /* XXX??? */ 2517111615Ssimokawa cpi->hba_inquiry = PI_TAG_ABLE; 2518103285Sikob cpi->target_sprt = 0; 2519118105Snjl cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE; 2520103285Sikob cpi->hba_eng_cnt = 0; 2521103285Sikob cpi->max_target = SBP_NUM_TARGETS - 1; 2522103285Sikob cpi->max_lun = SBP_NUM_LUNS - 1; 2523103285Sikob cpi->initiator_id = SBP_INITIATOR; 2524103285Sikob cpi->bus_id = sim->bus_id; 2525103285Sikob cpi->base_transfer_speed = 400 * 1000 / 8; 2526103285Sikob strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2527103285Sikob strncpy(cpi->hba_vid, "SBP", HBA_IDLEN); 2528103285Sikob strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN); 2529103285Sikob cpi->unit_number = sim->unit_number; 2530103285Sikob 2531103285Sikob cpi->ccb_h.status = CAM_REQ_CMP; 2532103285Sikob xpt_done(ccb); 2533103285Sikob break; 2534103285Sikob } 2535103285Sikob case XPT_GET_TRAN_SETTINGS: 2536103285Sikob { 2537103285Sikob struct ccb_trans_settings *cts = &ccb->cts; 2538103285SikobSBP_DEBUG(1) 2539103285Sikob printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n", 2540103285Sikob device_get_nameunit(sbp->fd.dev), 2541103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2542103285SikobEND_DEBUG 2543111615Ssimokawa /* Enable disconnect and tagged queuing */ 2544103285Sikob cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID; 2545111615Ssimokawa cts->flags = CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB; 2546103285Sikob 2547103285Sikob cts->ccb_h.status = CAM_REQ_CMP; 2548103285Sikob xpt_done(ccb); 2549103285Sikob break; 2550103285Sikob } 2551103285Sikob case XPT_ABORT: 2552103285Sikob ccb->ccb_h.status = CAM_UA_ABORT; 2553103285Sikob xpt_done(ccb); 2554103285Sikob break; 2555111615Ssimokawa case XPT_SET_TRAN_SETTINGS: 2556111615Ssimokawa /* XXX */ 2557103285Sikob default: 2558103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 2559103285Sikob xpt_done(ccb); 2560103285Sikob break; 2561103285Sikob } 2562103285Sikob return; 2563103285Sikob} 2564103285Sikob 2565103285Sikobstatic void 2566103285Sikobsbp_action(struct cam_sim *sim, union ccb *ccb) 2567103285Sikob{ 2568103285Sikob int s; 2569103285Sikob 2570103285Sikob s = splfw(); 2571103285Sikob sbp_action1(sim, ccb); 2572103285Sikob splx(s); 2573103285Sikob} 2574103285Sikob 2575103285Sikobstatic void 2576103285Sikobsbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error) 2577103285Sikob{ 2578103285Sikob int i; 2579103285Sikob struct sbp_ocb *ocb; 2580103285Sikob struct sbp_ocb *prev; 2581105633Ssimokawa bus_dma_segment_t *s; 2582103285Sikob 2583103285Sikob if (error) 2584103285Sikob printf("sbp_execute_ocb: error=%d\n", error); 2585103285Sikob 2586103285Sikob ocb = (struct sbp_ocb *)arg; 2587113584Ssimokawa 2588121185SsimokawaSBP_DEBUG(2) 2589113584Ssimokawa printf("sbp_execute_ocb: seg %d", seg); 2590113584Ssimokawa for (i = 0; i < seg; i++) 2591127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2592127468Ssimokawa printf(", %x:%d", segments[i].ds_addr, segments[i].ds_len); 2593127468Ssimokawa#else 2594113972Ssimokawa printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr, 2595113972Ssimokawa (uintmax_t)segments[i].ds_len); 2596113584Ssimokawa#endif 2597113584Ssimokawa printf("\n"); 2598113584SsimokawaEND_DEBUG 2599113584Ssimokawa 2600103285Sikob if (seg == 1) { 2601103285Sikob /* direct pointer */ 2602113474Ssimokawa s = &segments[0]; 2603113474Ssimokawa if (s->ds_len > SBP_SEG_MAX) 2604113474Ssimokawa panic("ds_len > SBP_SEG_MAX, fix busdma code"); 2605113474Ssimokawa ocb->orb[3] = htonl(s->ds_addr); 2606113474Ssimokawa ocb->orb[4] |= htonl(s->ds_len); 2607103285Sikob } else if(seg > 1) { 2608103285Sikob /* page table */ 2609103285Sikob for (i = 0; i < seg; i++) { 2610105633Ssimokawa s = &segments[i]; 2611108877SsimokawaSBP_DEBUG(0) 2612108877Ssimokawa /* XXX LSI Logic "< 16 byte" bug might be hit */ 2613105633Ssimokawa if (s->ds_len < 16) 2614105633Ssimokawa printf("sbp_execute_ocb: warning, " 2615127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2616127468Ssimokawa "segment length(%d) is less than 16." 2617127468Ssimokawa#else 2618106543Ssimokawa "segment length(%zd) is less than 16." 2619108877Ssimokawa#endif 2620105633Ssimokawa "(seg=%d/%d)\n", s->ds_len, i+1, seg); 2621108877SsimokawaEND_DEBUG 2622113474Ssimokawa if (s->ds_len > SBP_SEG_MAX) 2623113474Ssimokawa panic("ds_len > SBP_SEG_MAX, fix busdma code"); 2624105633Ssimokawa ocb->ind_ptr[i].hi = htonl(s->ds_len << 16); 2625105633Ssimokawa ocb->ind_ptr[i].lo = htonl(s->ds_addr); 2626103285Sikob } 2627103285Sikob ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg); 2628103285Sikob } 2629103285Sikob 2630113584Ssimokawa if (seg > 0) 2631113584Ssimokawa bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap, 2632113584Ssimokawa (ntohl(ocb->orb[4]) & ORB_CMD_IN) ? 2633113584Ssimokawa BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 2634103285Sikob prev = sbp_enqueue_ocb(ocb->sdev, ocb); 2635113584Ssimokawa fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE); 2636127468Ssimokawa if (use_doorbell) { 2637127468Ssimokawa if (prev == NULL) { 2638127468Ssimokawa if (ocb->sdev->last_ocb != NULL) 2639127468Ssimokawa sbp_doorbell(ocb->sdev); 2640127468Ssimokawa else 2641127468Ssimokawa sbp_orb_pointer(ocb->sdev, ocb); 2642127468Ssimokawa } 2643127468Ssimokawa } else { 2644127468Ssimokawa if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) { 2645127468Ssimokawa ocb->sdev->flags &= ~ORB_LINK_DEAD; 2646127468Ssimokawa sbp_orb_pointer(ocb->sdev, ocb); 2647127468Ssimokawa } 2648120842Ssimokawa } 2649103285Sikob} 2650103285Sikob 2651103285Sikobstatic void 2652103285Sikobsbp_poll(struct cam_sim *sim) 2653103285Sikob{ 2654120847Ssimokawa struct sbp_softc *sbp; 2655120847Ssimokawa struct firewire_comm *fc; 2656120847Ssimokawa 2657120847Ssimokawa sbp = (struct sbp_softc *)sim->softc; 2658120847Ssimokawa fc = sbp->fd.fc; 2659120847Ssimokawa 2660120847Ssimokawa fc->poll(fc, 0, -1); 2661120847Ssimokawa 2662103285Sikob return; 2663103285Sikob} 2664120847Ssimokawa 2665103285Sikobstatic struct sbp_ocb * 2666111615Ssimokawasbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status) 2667103285Sikob{ 2668103285Sikob struct sbp_ocb *ocb; 2669103285Sikob struct sbp_ocb *next; 2670103285Sikob int s = splfw(), order = 0; 2671103285Sikob int flags; 2672103285Sikob 2673103285SikobSBP_DEBUG(1) 2674121185Ssimokawa sbp_show_sdev_info(sdev, 2); 2675127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2676127468Ssimokawa printf("%s: 0x%08lx src %d\n", 2677127468Ssimokawa#else 2678121185Ssimokawa printf("%s: 0x%08x src %d\n", 2679108712Ssimokawa#endif 2680127468Ssimokawa __func__, ntohl(sbp_status->orb_lo), sbp_status->src); 2681103285SikobEND_DEBUG 2682121185Ssimokawa for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) { 2683121185Ssimokawa next = STAILQ_NEXT(ocb, ocb); 2684121185Ssimokawa flags = ocb->flags; 2685111615Ssimokawa if (OCB_MATCH(ocb, sbp_status)) { 2686103285Sikob /* found */ 2687111819Ssimokawa STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb); 2688103285Sikob if (ocb->ccb != NULL) 2689103285Sikob untimeout(sbp_timeout, (caddr_t)ocb, 2690103285Sikob ocb->ccb->ccb_h.timeout_ch); 2691113584Ssimokawa if (ntohl(ocb->orb[4]) & 0xffff) { 2692113584Ssimokawa bus_dmamap_sync(sdev->target->sbp->dmat, 2693113584Ssimokawa ocb->dmamap, 2694113584Ssimokawa (ntohl(ocb->orb[4]) & ORB_CMD_IN) ? 2695113584Ssimokawa BUS_DMASYNC_POSTREAD : 2696113584Ssimokawa BUS_DMASYNC_POSTWRITE); 2697113584Ssimokawa bus_dmamap_unload(sdev->target->sbp->dmat, 2698113584Ssimokawa ocb->dmamap); 2699103285Sikob } 2700127468Ssimokawa if (!use_doorbell) { 2701127468Ssimokawa if (sbp_status->src == SRC_NO_NEXT) { 2702127468Ssimokawa if (next != NULL) 2703127468Ssimokawa sbp_orb_pointer(sdev, next); 2704127468Ssimokawa else if (order > 0) { 2705127468Ssimokawa /* 2706127468Ssimokawa * Unordered execution 2707127468Ssimokawa * We need to send pointer for 2708127468Ssimokawa * next ORB 2709127468Ssimokawa */ 2710127468Ssimokawa sdev->flags |= ORB_LINK_DEAD; 2711127468Ssimokawa } 2712120842Ssimokawa } 2713127468Ssimokawa } else { 2714127468Ssimokawa /* 2715127468Ssimokawa * XXX this is not correct for unordered 2716127468Ssimokawa * execution. 2717127468Ssimokawa */ 2718127468Ssimokawa if (sdev->last_ocb != NULL) 2719127468Ssimokawa sbp_free_ocb(sdev, sdev->last_ocb); 2720127468Ssimokawa sdev->last_ocb = ocb; 2721127468Ssimokawa if (next != NULL && 2722127468Ssimokawa sbp_status->src == SRC_NO_NEXT) 2723127468Ssimokawa sbp_doorbell(sdev); 2724120842Ssimokawa } 2725103285Sikob break; 2726111819Ssimokawa } else 2727111819Ssimokawa order ++; 2728103285Sikob } 2729103285Sikob splx(s); 2730103285SikobSBP_DEBUG(0) 2731103285Sikob if (ocb && order > 0) { 2732103285Sikob sbp_show_sdev_info(sdev, 2); 2733103285Sikob printf("unordered execution order:%d\n", order); 2734103285Sikob } 2735103285SikobEND_DEBUG 2736103285Sikob return (ocb); 2737103285Sikob} 2738103285Sikob 2739103285Sikobstatic struct sbp_ocb * 2740103285Sikobsbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb) 2741103285Sikob{ 2742103285Sikob int s = splfw(); 2743127468Ssimokawa struct sbp_ocb *prev, *prev2; 2744103285Sikob 2745121185SsimokawaSBP_DEBUG(1) 2746103285Sikob sbp_show_sdev_info(sdev, 2); 2747127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2748127468Ssimokawa printf("%s: 0x%08x\n", __func__, ocb->bus_addr); 2749108712Ssimokawa#else 2750127468Ssimokawa printf("%s: 0x%08jx\n", __func__, (uintmax_t)ocb->bus_addr); 2751108712Ssimokawa#endif 2752103285SikobEND_DEBUG 2753127468Ssimokawa prev2 = prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb); 2754103285Sikob STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb); 2755103285Sikob 2756103285Sikob if (ocb->ccb != NULL) 2757103285Sikob ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb, 2758103285Sikob (ocb->ccb->ccb_h.timeout * hz) / 1000); 2759103285Sikob 2760127468Ssimokawa if (use_doorbell && prev == NULL) 2761127468Ssimokawa prev2 = sdev->last_ocb; 2762127468Ssimokawa 2763127468Ssimokawa if (prev2 != NULL) { 2764121185SsimokawaSBP_DEBUG(2) 2765127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2766127468Ssimokawa printf("linking chain 0x%x -> 0x%x\n", 2767127468Ssimokawa prev2->bus_addr, ocb->bus_addr); 2768127468Ssimokawa#else 2769120842Ssimokawa printf("linking chain 0x%jx -> 0x%jx\n", 2770127468Ssimokawa (uintmax_t)prev2->bus_addr, (uintmax_t)ocb->bus_addr); 2771108712Ssimokawa#endif 2772103285SikobEND_DEBUG 2773127468Ssimokawa prev2->orb[1] = htonl(ocb->bus_addr); 2774127468Ssimokawa prev2->orb[0] = 0; 2775103285Sikob } 2776103285Sikob splx(s); 2777103285Sikob 2778103285Sikob return prev; 2779103285Sikob} 2780103285Sikob 2781103285Sikobstatic struct sbp_ocb * 2782113584Ssimokawasbp_get_ocb(struct sbp_dev *sdev) 2783103285Sikob{ 2784103285Sikob struct sbp_ocb *ocb; 2785103285Sikob int s = splfw(); 2786113584Ssimokawa ocb = STAILQ_FIRST(&sdev->free_ocbs); 2787103285Sikob if (ocb == NULL) { 2788127468Ssimokawa sdev->flags |= ORB_SHORTAGE; 2789103285Sikob printf("ocb shortage!!!\n"); 2790134868Ssimokawa splx(s); 2791103285Sikob return NULL; 2792103285Sikob } 2793113584Ssimokawa STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb); 2794103285Sikob splx(s); 2795103285Sikob ocb->ccb = NULL; 2796103285Sikob return (ocb); 2797103285Sikob} 2798103285Sikob 2799103285Sikobstatic void 2800113584Ssimokawasbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb) 2801103285Sikob{ 2802103285Sikob ocb->flags = 0; 2803103285Sikob ocb->ccb = NULL; 2804113584Ssimokawa STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb); 2805127468Ssimokawa if ((sdev->flags & ORB_SHORTAGE) != 0) { 2806127468Ssimokawa int count; 2807127468Ssimokawa 2808127468Ssimokawa sdev->flags &= ~ORB_SHORTAGE; 2809127468Ssimokawa count = sdev->freeze; 2810127468Ssimokawa sdev->freeze = 0; 2811127468Ssimokawa xpt_release_devq(sdev->path, count, TRUE); 2812127468Ssimokawa } 2813103285Sikob} 2814103285Sikob 2815103285Sikobstatic void 2816103285Sikobsbp_abort_ocb(struct sbp_ocb *ocb, int status) 2817103285Sikob{ 2818103285Sikob struct sbp_dev *sdev; 2819103285Sikob 2820103285Sikob sdev = ocb->sdev; 2821113584SsimokawaSBP_DEBUG(0) 2822103285Sikob sbp_show_sdev_info(sdev, 2); 2823127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000 2824127468Ssimokawa printf("sbp_abort_ocb 0x%x\n", ocb->bus_addr); 2825127468Ssimokawa#else 2826113972Ssimokawa printf("sbp_abort_ocb 0x%jx\n", (uintmax_t)ocb->bus_addr); 2827111819Ssimokawa#endif 2828113584SsimokawaEND_DEBUG 2829113584SsimokawaSBP_DEBUG(1) 2830105792Ssimokawa if (ocb->ccb != NULL) 2831105792Ssimokawa sbp_print_scsi_cmd(ocb); 2832103285SikobEND_DEBUG 2833113584Ssimokawa if (ntohl(ocb->orb[4]) & 0xffff) { 2834113584Ssimokawa bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap, 2835113584Ssimokawa (ntohl(ocb->orb[4]) & ORB_CMD_IN) ? 2836113584Ssimokawa BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 2837113584Ssimokawa bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap); 2838113584Ssimokawa } 2839111819Ssimokawa if (ocb->ccb != NULL) { 2840110336Ssimokawa untimeout(sbp_timeout, (caddr_t)ocb, 2841110336Ssimokawa ocb->ccb->ccb_h.timeout_ch); 2842103285Sikob ocb->ccb->ccb_h.status = status; 2843103285Sikob xpt_done(ocb->ccb); 2844103285Sikob } 2845113584Ssimokawa sbp_free_ocb(sdev, ocb); 2846103285Sikob} 2847103285Sikob 2848103285Sikobstatic void 2849103285Sikobsbp_abort_all_ocbs(struct sbp_dev *sdev, int status) 2850103285Sikob{ 2851103285Sikob int s; 2852105792Ssimokawa struct sbp_ocb *ocb, *next; 2853105792Ssimokawa STAILQ_HEAD(, sbp_ocb) temp; 2854103285Sikob 2855103285Sikob s = splfw(); 2856105792Ssimokawa 2857105792Ssimokawa bcopy(&sdev->ocbs, &temp, sizeof(temp)); 2858105792Ssimokawa STAILQ_INIT(&sdev->ocbs); 2859105792Ssimokawa for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) { 2860105792Ssimokawa next = STAILQ_NEXT(ocb, ocb); 2861103285Sikob sbp_abort_ocb(ocb, status); 2862103285Sikob } 2863127468Ssimokawa if (sdev->last_ocb != NULL) { 2864127468Ssimokawa sbp_free_ocb(sdev, sdev->last_ocb); 2865127468Ssimokawa sdev->last_ocb = NULL; 2866127468Ssimokawa } 2867105792Ssimokawa 2868103285Sikob splx(s); 2869103285Sikob} 2870103285Sikob 2871103285Sikobstatic devclass_t sbp_devclass; 2872103285Sikob 2873103285Sikobstatic device_method_t sbp_methods[] = { 2874103285Sikob /* device interface */ 2875103285Sikob DEVMETHOD(device_identify, sbp_identify), 2876103285Sikob DEVMETHOD(device_probe, sbp_probe), 2877103285Sikob DEVMETHOD(device_attach, sbp_attach), 2878103285Sikob DEVMETHOD(device_detach, sbp_detach), 2879110145Ssimokawa DEVMETHOD(device_shutdown, sbp_shutdown), 2880103285Sikob 2881103285Sikob { 0, 0 } 2882103285Sikob}; 2883103285Sikob 2884103285Sikobstatic driver_t sbp_driver = { 2885103285Sikob "sbp", 2886103285Sikob sbp_methods, 2887103285Sikob sizeof(struct sbp_softc), 2888103285Sikob}; 2889127468Ssimokawa#ifdef __DragonFly__ 2890127468SsimokawaDECLARE_DUMMY_MODULE(sbp); 2891127468Ssimokawa#endif 2892103285SikobDRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0); 2893103285SikobMODULE_VERSION(sbp, 1); 2894103285SikobMODULE_DEPEND(sbp, firewire, 1, 1, 1); 2895103285SikobMODULE_DEPEND(sbp, cam, 1, 1, 1); 2896