sbp.c revision 111704
1103285Sikob/* 2103285Sikob * Copyright (c) 1998,1999,2000,2001 Katsushi Kobayashi and Hidetosh Shimokawa 3103285Sikob * All rights reserved. 4103285Sikob * 5103285Sikob * Redistribution and use in source and binary forms, with or without 6103285Sikob * modification, are permitted provided that the following conditions 7103285Sikob * are met: 8103285Sikob * 1. Redistributions of source code must retain the above copyright 9103285Sikob * notice, this list of conditions and the following disclaimer. 10103285Sikob * 2. Redistributions in binary form must reproduce the above copyright 11103285Sikob * notice, this list of conditions and the following disclaimer in the 12103285Sikob * documentation and/or other materials provided with the distribution. 13103285Sikob * 3. All advertising materials mentioning features or use of this software 14103285Sikob * must display the acknowledgement as bellow: 15103285Sikob * 16103285Sikob * This product includes software developed by K. Kobayashi and H. Shimokawa 17103285Sikob * 18103285Sikob * 4. The name of the author may not be used to endorse or promote products 19103285Sikob * derived from this software without specific prior written permission. 20103285Sikob * 21103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24103285Sikob * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 25103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 29103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 30103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31103285Sikob * POSSIBILITY OF SUCH DAMAGE. 32103285Sikob * 33103285Sikob * $FreeBSD: head/sys/dev/firewire/sbp.c 111704 2003-03-01 16:50:40Z simokawa $ 34103285Sikob * 35103285Sikob */ 36103285Sikob 37103285Sikob#include <sys/param.h> 38103285Sikob#include <sys/systm.h> 39103285Sikob#include <sys/module.h> 40103285Sikob#include <sys/bus.h> 41103285Sikob#include <sys/mbuf.h> 42103285Sikob#include <sys/sysctl.h> 43103285Sikob#include <machine/bus.h> 44103285Sikob#include <sys/malloc.h> 45103285Sikob#include <sys/devicestat.h> /* for struct devstat */ 46103285Sikob 47103285Sikob 48103285Sikob#include <cam/cam.h> 49103285Sikob#include <cam/cam_ccb.h> 50103285Sikob#include <cam/cam_sim.h> 51103285Sikob#include <cam/cam_xpt_sim.h> 52103285Sikob#include <cam/cam_debug.h> 53103285Sikob#include <cam/cam_periph.h> 54103285Sikob 55103285Sikob#include <cam/scsi/scsi_all.h> 56103285Sikob#include <cam/scsi/scsi_message.h> 57103285Sikob#include <cam/scsi/scsi_da.h> 58103285Sikob 59103285Sikob#include <sys/kernel.h> 60103285Sikob 61103285Sikob#include <vm/vm.h> 62103285Sikob#include <vm/pmap.h> 63103285Sikob 64103285Sikob#include <dev/firewire/firewire.h> 65103285Sikob#include <dev/firewire/firewirereg.h> 66103285Sikob#include <dev/firewire/iec13213.h> 67103285Sikob 68103285Sikob#define ccb_sdev_ptr spriv_ptr0 69103285Sikob#define ccb_sbp_ptr spriv_ptr1 70103285Sikob 71111615Ssimokawa#define SBP_NUM_TARGETS 8 /* MAX 64 */ 72103285Sikob#define SBP_NUM_LUNS 8 /* limited by CAM_SCSI2_MAXLUN in cam_xpt.c */ 73103285Sikob#define SBP_QUEUE_LEN 4 74103285Sikob#define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS) 75103285Sikob#define SBP_INITIATOR 7 76111615Ssimokawa 77111615Ssimokawa#define LOGIN_DELAY 2 78111615Ssimokawa 79111615Ssimokawa/* 80111615Ssimokawa * STATUS FIFO addressing 81111615Ssimokawa * bit 82111615Ssimokawa * ----------------------- 83111615Ssimokawa * 0- 1( 2): 0 (alingment) 84111615Ssimokawa * 2- 7( 6): target 85111615Ssimokawa * 8-15( 8): lun 86111615Ssimokawa * 16-23( 8): unit 87111615Ssimokawa * 24-31( 8): reserved 88111615Ssimokawa * 32-47(16): SBP_BIND_HI 89111615Ssimokawa * 48-64(16): bus_id, node_id 90111615Ssimokawa */ 91103285Sikob#define SBP_BIND_HI 0x1 92111615Ssimokawa#define SBP_DEV2ADDR(u, t, l) \ 93103285Sikob ((((u) & 0xff) << 16) | (((l) & 0xff) << 8) | (((t) & 0x3f) << 2)) 94103285Sikob#define SBP_ADDR2TRG(a) (((a) >> 2) & 0x3f) 95103285Sikob#define SBP_ADDR2LUN(a) (((a) >> 8) & 0xff) 96103285Sikob 97103285Sikob#define ORB_NOTIFY (1 << 31) 98103285Sikob#define ORB_FMT_STD (0 << 29) 99103285Sikob#define ORB_FMT_VED (2 << 29) 100103285Sikob#define ORB_FMT_NOP (3 << 29) 101103285Sikob#define ORB_FMT_MSK (3 << 29) 102103285Sikob#define ORB_EXV (1 << 28) 103103285Sikob/* */ 104103285Sikob#define ORB_CMD_IN (1 << 27) 105103285Sikob/* */ 106103285Sikob#define ORB_CMD_SPD(x) ((x) << 24) 107103285Sikob#define ORB_CMD_MAXP(x) ((x) << 20) 108103285Sikob#define ORB_RCN_TMO(x) ((x) << 20) 109103285Sikob#define ORB_CMD_PTBL (1 << 19) 110103285Sikob#define ORB_CMD_PSZ(x) ((x) << 16) 111103285Sikob 112103285Sikob#define ORB_FUN_LGI (0 << 16) 113103285Sikob#define ORB_FUN_QLG (1 << 16) 114103285Sikob#define ORB_FUN_RCN (3 << 16) 115103285Sikob#define ORB_FUN_LGO (7 << 16) 116103285Sikob#define ORB_FUN_ATA (0xb << 16) 117103285Sikob#define ORB_FUN_ATS (0xc << 16) 118103285Sikob#define ORB_FUN_LUR (0xe << 16) 119103285Sikob#define ORB_FUN_RST (0xf << 16) 120103285Sikob#define ORB_FUN_MSK (0xf << 16) 121111615Ssimokawa#define ORB_FUN_RUNQUEUE 0xffff 122103285Sikob 123103285Sikobstatic char *orb_fun_name[] = { 124103285Sikob /* 0 */ "LOGIN", 125103285Sikob /* 1 */ "QUERY LOGINS", 126103285Sikob /* 2 */ "Reserved", 127103285Sikob /* 3 */ "RECONNECT", 128103285Sikob /* 4 */ "SET PASSWORD", 129103285Sikob /* 5 */ "Reserved", 130103285Sikob /* 6 */ "Reserved", 131103285Sikob /* 7 */ "LOGOUT", 132103285Sikob /* 8 */ "Reserved", 133103285Sikob /* 9 */ "Reserved", 134103285Sikob /* A */ "Reserved", 135103285Sikob /* B */ "ABORT TASK", 136103285Sikob /* C */ "ABORT TASK SET", 137103285Sikob /* D */ "Reserved", 138103285Sikob /* E */ "LOGICAL UNIT RESET", 139103285Sikob /* F */ "TARGET RESET" 140103285Sikob}; 141103285Sikob 142103285Sikob#define ORB_RES_CMPL 0 143103285Sikob#define ORB_RES_FAIL 1 144103285Sikob#define ORB_RES_ILLE 2 145103285Sikob#define ORB_RES_VEND 3 146103285Sikob 147111203Ssimokawastatic int debug = 0; 148103285Sikobstatic int auto_login = 1; 149103285Sikobstatic int max_speed = 2; 150111199Ssimokawastatic int sbp_cold = 1; 151103285Sikob 152103285SikobSYSCTL_DECL(_hw_firewire); 153103285SikobSYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem"); 154103285SikobSYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0, 155103285Sikob "SBP debug flag"); 156103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0, 157103285Sikob "SBP perform login automatically"); 158103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0, 159103285Sikob "SBP transfer max speed"); 160103285Sikob 161103285Sikob#define SBP_DEBUG(x) if (debug > x) { 162103285Sikob#define END_DEBUG } 163103285Sikob 164103285Sikob#define NEED_RESPONSE 0 165103285Sikob 166103285Sikobstruct ind_ptr { 167103285Sikob u_int32_t hi,lo; 168103285Sikob}; 169103285Sikob#define SBP_IND_MAX 0x20 170103285Sikobstruct sbp_ocb { 171103285Sikob STAILQ_ENTRY(sbp_ocb) ocb; 172103285Sikob union ccb *ccb; 173103285Sikob volatile u_int32_t orb[8]; 174103285Sikob volatile struct ind_ptr ind_ptr[SBP_IND_MAX]; 175103285Sikob struct sbp_dev *sdev; 176103285Sikob int flags; 177103285Sikob bus_dmamap_t dmamap; 178103285Sikob}; 179103285Sikob#define OCB_ACT_MGM 0 180103285Sikob#define OCB_ACT_CMD 1 181103285Sikob#define OCB_ACT_MASK 3 182103285Sikob#define OCB_RESERVED 0x10 183103285Sikob#define OCB_DONE 0x20 184111615Ssimokawa#define OCB_MATCH(o,s) (vtophys(&(o)->orb[0]) == ntohl((s)->orb_lo)) 185103285Sikob 186103285Sikob 187103285Sikobstruct sbp_login_res{ 188103285Sikob u_int16_t len; 189103285Sikob u_int16_t id; 190103285Sikob u_int16_t res0; 191103285Sikob u_int16_t cmd_hi; 192103285Sikob u_int32_t cmd_lo; 193103285Sikob u_int16_t res1; 194103285Sikob u_int16_t recon_hold; 195103285Sikob}; 196103285Sikobstruct sbp_status{ 197103285Sikob u_int8_t len:3, 198103285Sikob dead:1, 199103285Sikob resp:2, 200103285Sikob src:2; 201103285Sikob u_int8_t status:8; 202103285Sikob u_int16_t orb_hi; 203103285Sikob u_int32_t orb_lo; 204103285Sikob u_int32_t data[6]; 205103285Sikob}; 206103285Sikobstruct sbp_cmd_status{ 207103285Sikob#define SBP_SFMT_CURR 0 208103285Sikob#define SBP_SFMT_DEFER 1 209103285Sikob u_int8_t status:6, 210103285Sikob sfmt:2; 211103285Sikob u_int8_t s_key:4, 212103285Sikob ill_len:1, 213103285Sikob eom:1, 214103285Sikob mark:1, 215103285Sikob valid:1; 216103285Sikob u_int8_t s_code; 217103285Sikob u_int8_t s_qlfr; 218103285Sikob u_int32_t info; 219103285Sikob u_int32_t cdb; 220103285Sikob u_int32_t fru:8, 221103285Sikob s_keydep:24; 222103285Sikob u_int32_t vend[2]; 223103285Sikob}; 224103285Sikob 225103285Sikobstruct sbp_dev{ 226103285Sikob#define SBP_DEV_RESET 0 /* accept login */ 227110336Ssimokawa#if 0 228103285Sikob#define SBP_DEV_LOGIN 1 /* to login */ 229103285Sikob#define SBP_DEV_RECONN 2 /* to reconnect */ 230110336Ssimokawa#endif 231103285Sikob#define SBP_DEV_TOATTACH 3 /* to attach */ 232103285Sikob#define SBP_DEV_PROBE 4 /* scan lun */ 233103285Sikob#define SBP_DEV_ATTACHED 5 /* in operation */ 234103285Sikob#define SBP_DEV_DEAD 6 /* unavailable unit */ 235103285Sikob#define SBP_DEV_RETRY 7 /* unavailable unit */ 236110336Ssimokawa u_int8_t status:4, 237110336Ssimokawa#define SBP_DEV_TIMEOUT 1 238110336Ssimokawa flags:4; 239108503Ssimokawa u_int8_t type; 240108503Ssimokawa u_int16_t lun_id; 241111615Ssimokawa int freeze; 242103285Sikob struct cam_path *path; 243103285Sikob struct sbp_target *target; 244103285Sikob struct sbp_login_res login; 245111615Ssimokawa struct callout login_callout; 246103285Sikob STAILQ_HEAD(, sbp_ocb) ocbs; 247103285Sikob char vendor[32]; 248103285Sikob char product[32]; 249103285Sikob char revision[10]; 250103285Sikob}; 251103285Sikob 252103285Sikobstruct sbp_target { 253103285Sikob int target_id; 254103285Sikob int num_lun; 255103285Sikob struct sbp_dev *luns; 256103285Sikob struct sbp_softc *sbp; 257103285Sikob struct fw_device *fwdev; 258103285Sikob u_int32_t mgm_hi, mgm_lo; 259111615Ssimokawa struct sbp_ocb *mgm_ocb_cur; 260111615Ssimokawa STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue; 261111615Ssimokawa struct callout mgm_ocb_timeout; 262111615Ssimokawa#define SCAN_DELAY 2 263111615Ssimokawa struct callout scan_callout; 264103285Sikob}; 265103285Sikob 266103285Sikobstruct sbp_softc { 267103285Sikob struct firewire_dev_comm fd; 268103285Sikob struct cam_sim *sim; 269111615Ssimokawa struct cam_path *path; 270103285Sikob struct sbp_target targets[SBP_NUM_TARGETS]; 271103285Sikob struct fw_bind fwb; 272103285Sikob STAILQ_HEAD(, sbp_ocb) free_ocbs; 273103285Sikob struct sbp_ocb *ocb; 274103285Sikob bus_dma_tag_t dmat; 275111615Ssimokawa#define SBP_RESOURCE_SHORTAGE 0x10 276111615Ssimokawa unsigned char flags; 277103285Sikob}; 278103285Sikobstatic void sbp_post_explore __P((void *)); 279103285Sikobstatic void sbp_recv __P((struct fw_xfer *)); 280103285Sikobstatic void sbp_login_callback __P((struct fw_xfer *)); 281103285Sikobstatic void sbp_cmd_callback __P((struct fw_xfer *)); 282103285Sikobstatic void sbp_orb_pointer __P((struct sbp_dev *, struct sbp_ocb *)); 283103285Sikobstatic void sbp_execute_ocb __P((void *, bus_dma_segment_t *, int, int)); 284103285Sikobstatic void sbp_free_ocb __P((struct sbp_softc *, struct sbp_ocb *)); 285103285Sikobstatic void sbp_abort_ocb __P((struct sbp_ocb *, int)); 286103285Sikobstatic void sbp_abort_all_ocbs __P((struct sbp_dev *, int)); 287103285Sikobstatic struct fw_xfer * sbp_write_cmd __P((struct sbp_dev *, int, int)); 288103285Sikobstatic struct sbp_ocb * sbp_get_ocb __P((struct sbp_softc *)); 289103285Sikobstatic struct sbp_ocb * sbp_enqueue_ocb __P((struct sbp_dev *, struct sbp_ocb *)); 290111615Ssimokawastatic struct sbp_ocb * sbp_dequeue_ocb __P((struct sbp_dev *, struct sbp_status *)); 291110145Ssimokawastatic void sbp_cam_detach_target __P((struct sbp_target *)); 292103285Sikobstatic void sbp_timeout __P((void *arg)); 293111615Ssimokawastatic void sbp_mgm_orb __P((struct sbp_dev *, int, struct sbp_ocb *)); 294111615Ssimokawa#define sbp_login(sdev) \ 295111615Ssimokawa callout_reset(&(sdev)->login_callout, LOGIN_DELAY * hz, \ 296111615Ssimokawa sbp_login_callout, (void *)(sdev)); 297103285Sikob 298108281SsimokawaMALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire"); 299103285Sikob 300103285Sikob/* cam related functions */ 301103285Sikobstatic void sbp_action(struct cam_sim *sim, union ccb *ccb); 302103285Sikobstatic void sbp_poll(struct cam_sim *sim); 303111615Ssimokawastatic void sbp_cam_scan_lun(struct cam_periph *, union ccb *); 304111615Ssimokawastatic void sbp_cam_scan_target(void *arg); 305103285Sikob 306103285Sikobstatic char *orb_status0[] = { 307103285Sikob /* 0 */ "No additional information to report", 308103285Sikob /* 1 */ "Request type not supported", 309103285Sikob /* 2 */ "Speed not supported", 310103285Sikob /* 3 */ "Page size not supported", 311103285Sikob /* 4 */ "Access denied", 312103285Sikob /* 5 */ "Logical unit not supported", 313103285Sikob /* 6 */ "Maximum payload too small", 314103285Sikob /* 7 */ "Reserved for future standardization", 315103285Sikob /* 8 */ "Resources unavailable", 316103285Sikob /* 9 */ "Function rejected", 317103285Sikob /* A */ "Login ID not recognized", 318103285Sikob /* B */ "Dummy ORB completed", 319103285Sikob /* C */ "Request aborted", 320103285Sikob /* FF */ "Unspecified error" 321103285Sikob#define MAX_ORB_STATUS0 0xd 322103285Sikob}; 323103285Sikob 324103285Sikobstatic char *orb_status1_object[] = { 325103285Sikob /* 0 */ "Operation request block (ORB)", 326103285Sikob /* 1 */ "Data buffer", 327103285Sikob /* 2 */ "Page table", 328103285Sikob /* 3 */ "Unable to specify" 329103285Sikob}; 330103285Sikob 331103285Sikobstatic char *orb_status1_serial_bus_error[] = { 332103285Sikob /* 0 */ "Missing acknowledge", 333103285Sikob /* 1 */ "Reserved; not to be used", 334103285Sikob /* 2 */ "Time-out error", 335103285Sikob /* 3 */ "Reserved; not to be used", 336103285Sikob /* 4 */ "Busy retry limit exceeded(X)", 337103285Sikob /* 5 */ "Busy retry limit exceeded(A)", 338103285Sikob /* 6 */ "Busy retry limit exceeded(B)", 339103285Sikob /* 7 */ "Reserved for future standardization", 340103285Sikob /* 8 */ "Reserved for future standardization", 341103285Sikob /* 9 */ "Reserved for future standardization", 342103285Sikob /* A */ "Reserved for future standardization", 343103285Sikob /* B */ "Tardy retry limit exceeded", 344103285Sikob /* C */ "Conflict error", 345103285Sikob /* D */ "Data error", 346103285Sikob /* E */ "Type error", 347103285Sikob /* F */ "Address error" 348103285Sikob}; 349103285Sikob 350103285Sikobstatic void 351103285Sikobsbp_identify(driver_t *driver, device_t parent) 352103285Sikob{ 353103285Sikob device_t child; 354103285SikobSBP_DEBUG(0) 355103285Sikob printf("sbp_identify\n"); 356103285SikobEND_DEBUG 357103285Sikob 358103285Sikob child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent)); 359103285Sikob} 360103285Sikob 361103285Sikob/* 362103285Sikob * sbp_probe() 363103285Sikob */ 364103285Sikobstatic int 365103285Sikobsbp_probe(device_t dev) 366103285Sikob{ 367103285Sikob device_t pa; 368103285Sikob 369103285SikobSBP_DEBUG(0) 370103285Sikob printf("sbp_probe\n"); 371103285SikobEND_DEBUG 372103285Sikob 373103285Sikob pa = device_get_parent(dev); 374103285Sikob if(device_get_unit(dev) != device_get_unit(pa)){ 375103285Sikob return(ENXIO); 376103285Sikob } 377103285Sikob 378103285Sikob device_set_desc(dev, "SBP2/SCSI over firewire"); 379111615Ssimokawa 380111615Ssimokawa if (bootverbose) 381111615Ssimokawa debug = bootverbose; 382103285Sikob return (0); 383103285Sikob} 384103285Sikob 385103285Sikobstatic void 386103285Sikobsbp_show_sdev_info(struct sbp_dev *sdev, int new) 387103285Sikob{ 388103285Sikob struct fw_device *fwdev; 389103285Sikob 390103285Sikob printf("%s:%d:%d ", 391103285Sikob device_get_nameunit(sdev->target->sbp->fd.dev), 392103285Sikob sdev->target->target_id, 393103285Sikob sdev->lun_id 394103285Sikob ); 395103285Sikob if (new == 2) { 396103285Sikob return; 397103285Sikob } 398103285Sikob fwdev = sdev->target->fwdev; 399103285Sikob printf("ordered:%d type:%d EUI:%08x%08x node:%d " 400103285Sikob "speed:%d maxrec:%d", 401108503Ssimokawa (sdev->type & 0x40) >> 6, 402108503Ssimokawa (sdev->type & 0x1f), 403103285Sikob fwdev->eui.hi, 404103285Sikob fwdev->eui.lo, 405103285Sikob fwdev->dst, 406103285Sikob fwdev->speed, 407103285Sikob fwdev->maxrec 408103285Sikob ); 409103285Sikob if (new) 410103285Sikob printf(" new!\n"); 411103285Sikob else 412103285Sikob printf("\n"); 413103285Sikob sbp_show_sdev_info(sdev, 2); 414103285Sikob printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision); 415103285Sikob} 416103285Sikob 417110184Ssimokawastatic struct { 418110184Ssimokawa int bus; 419110184Ssimokawa int target; 420110184Ssimokawa struct fw_eui64 eui; 421110184Ssimokawa} wired[] = { 422110184Ssimokawa /* Bus Target EUI64 */ 423110184Ssimokawa#if 0 424110184Ssimokawa {0, 2, {0x00018ea0, 0x01fd0154}}, /* Logitec HDD */ 425110184Ssimokawa {0, 0, {0x00018ea6, 0x00100682}}, /* Logitec DVD */ 426110184Ssimokawa {0, 1, {0x00d03200, 0xa412006a}}, /* Yano HDD */ 427110184Ssimokawa#endif 428110184Ssimokawa {-1, -1, {0,0}} 429110184Ssimokawa}; 430110184Ssimokawa 431110184Ssimokawastatic int 432110184Ssimokawasbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev) 433110184Ssimokawa{ 434110184Ssimokawa int bus, i, target=-1; 435110184Ssimokawa char w[SBP_NUM_TARGETS]; 436110184Ssimokawa 437110184Ssimokawa bzero(w, sizeof(w)); 438110184Ssimokawa bus = device_get_unit(sbp->fd.dev); 439110184Ssimokawa 440110184Ssimokawa /* XXX wired-down configuration should be gotten from 441110184Ssimokawa tunable or device hint */ 442110184Ssimokawa for (i = 0; wired[i].bus >= 0; i ++) { 443110184Ssimokawa if (wired[i].bus == bus) { 444110184Ssimokawa w[wired[i].target] = 1; 445110184Ssimokawa if (wired[i].eui.hi == fwdev->eui.hi && 446110184Ssimokawa wired[i].eui.lo == fwdev->eui.lo) 447110184Ssimokawa target = wired[i].target; 448110184Ssimokawa } 449110184Ssimokawa } 450110184Ssimokawa if (target >= 0) { 451110184Ssimokawa if(target < SBP_NUM_TARGETS && 452110184Ssimokawa sbp->targets[target].fwdev == NULL) 453110184Ssimokawa return(target); 454110184Ssimokawa device_printf(sbp->fd.dev, 455110184Ssimokawa "target %d is not free for %08x:%08x\n", 456110184Ssimokawa target, fwdev->eui.hi, fwdev->eui.lo); 457110184Ssimokawa target = -1; 458110184Ssimokawa } 459110184Ssimokawa /* non-wired target */ 460110184Ssimokawa for (i = 0; i < SBP_NUM_TARGETS; i ++) 461110184Ssimokawa if (sbp->targets[i].fwdev == NULL && w[i] == 0) { 462110184Ssimokawa target = i; 463110184Ssimokawa break; 464110184Ssimokawa } 465110184Ssimokawa 466110184Ssimokawa return target; 467110184Ssimokawa} 468110184Ssimokawa 469103285Sikobstatic struct sbp_target * 470103285Sikobsbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev) 471103285Sikob{ 472108503Ssimokawa int i, maxlun, lun; 473103285Sikob struct sbp_target *target; 474103285Sikob struct sbp_dev *sdev; 475108503Ssimokawa struct crom_context cc; 476108503Ssimokawa struct csrreg *reg; 477103285Sikob 478103285SikobSBP_DEBUG(1) 479103285Sikob printf("sbp_alloc_target\n"); 480103285SikobEND_DEBUG 481110184Ssimokawa i = sbp_new_target(sbp, fwdev); 482110184Ssimokawa if (i < 0) { 483110184Ssimokawa device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n"); 484103285Sikob return NULL; 485103285Sikob } 486103285Sikob /* new target */ 487103285Sikob target = &sbp->targets[i]; 488103285Sikob target->sbp = sbp; 489103285Sikob target->fwdev = fwdev; 490103285Sikob target->target_id = i; 491111704Ssimokawa /* XXX we may want to reload mgm port after each bus reset */ 492103285Sikob if((target->mgm_lo = getcsrdata(fwdev, 0x54)) == 0 ){ 493103285Sikob /* bad target */ 494103285Sikob printf("NULL management address\n"); 495103285Sikob target->fwdev = NULL; 496103285Sikob return NULL; 497103285Sikob } 498103285Sikob target->mgm_hi = 0xffff; 499103285Sikob target->mgm_lo = 0xf0000000 | target->mgm_lo << 2; 500111615Ssimokawa target->mgm_ocb_cur = NULL; 501111704SsimokawaSBP_DEBUG(1) 502111704Ssimokawa printf("target:%d mgm_port: %x\n", i, target->mgm_lo); 503111704SsimokawaEND_DEBUG 504111615Ssimokawa STAILQ_INIT(&target->mgm_ocb_queue); 505111615Ssimokawa CALLOUT_INIT(&target->mgm_ocb_timeout); 506111615Ssimokawa CALLOUT_INIT(&target->scan_callout); 507111615Ssimokawa 508103285Sikob /* XXX num_lun may be changed. realloc luns? */ 509108503Ssimokawa crom_init_context(&cc, target->fwdev->csrrom); 510108503Ssimokawa /* XXX shoud parse appropriate unit directories only */ 511108503Ssimokawa maxlun = -1; 512108503Ssimokawa while (cc.depth >= 0) { 513108503Ssimokawa reg = crom_search_key(&cc, CROM_LUN); 514108503Ssimokawa if (reg == NULL) 515108503Ssimokawa break; 516110839Ssimokawa lun = reg->val & 0xffff; 517108642SsimokawaSBP_DEBUG(0) 518108642Ssimokawa printf("target %d lun %d found\n", target->target_id, lun); 519108642SsimokawaEND_DEBUG 520108503Ssimokawa if (maxlun < lun) 521108503Ssimokawa maxlun = lun; 522108503Ssimokawa crom_next(&cc); 523108503Ssimokawa } 524110839Ssimokawa if (maxlun < 0) 525110839Ssimokawa printf("no lun found!\n"); 526110839Ssimokawa if (maxlun >= SBP_NUM_LUNS) 527110839Ssimokawa maxlun = SBP_NUM_LUNS; 528108503Ssimokawa target->num_lun = maxlun + 1; 529103285Sikob target->luns = (struct sbp_dev *) malloc( 530103285Sikob sizeof(struct sbp_dev) * target->num_lun, 531103285Sikob M_SBP, M_NOWAIT | M_ZERO); 532103285Sikob for (i = 0; i < target->num_lun; i++) { 533103285Sikob sdev = &target->luns[i]; 534103285Sikob sdev->lun_id = i; 535103285Sikob sdev->target = target; 536103285Sikob STAILQ_INIT(&sdev->ocbs); 537111615Ssimokawa CALLOUT_INIT(&sdev->login_callout); 538108503Ssimokawa sdev->status = SBP_DEV_DEAD; 539103285Sikob } 540108503Ssimokawa crom_init_context(&cc, target->fwdev->csrrom); 541108503Ssimokawa while (cc.depth >= 0) { 542108503Ssimokawa reg = crom_search_key(&cc, CROM_LUN); 543108503Ssimokawa if (reg == NULL) 544108503Ssimokawa break; 545110839Ssimokawa lun = reg->val & 0xffff; 546110839Ssimokawa if (lun >= SBP_NUM_LUNS) { 547110839Ssimokawa printf("too large lun %d\n", lun); 548110839Ssimokawa continue; 549110839Ssimokawa } 550108503Ssimokawa target->luns[lun].status = SBP_DEV_RESET; 551110839Ssimokawa target->luns[lun].type = (reg->val & 0xf0000) >> 16; 552108503Ssimokawa crom_next(&cc); 553110839Ssimokawa } 554110839Ssimokawa return target; 555110839Ssimokawa } 556103285Sikob 557103285Sikobstatic void 558103285Sikobsbp_get_text_leaf(struct fw_device *fwdev, int key, char *buf, int len) 559103285Sikob{ 560103285Sikob static char *nullstr = "(null)"; 561103285Sikob int i, clen, found=0; 562103285Sikob struct csrhdr *chdr; 563103285Sikob struct csrreg *creg; 564103285Sikob u_int32_t *src, *dst; 565103285Sikob 566103285Sikob chdr = (struct csrhdr *)&fwdev->csrrom[0]; 567110839Ssimokawa /* skip crom header, bus info and root directory */ 568110839Ssimokawa creg = (struct csrreg *)chdr + chdr->info_len + 2; 569110839Ssimokawa /* search unitl the one before the last. */ 570110839Ssimokawa for (i = chdr->info_len + 2; i < fwdev->rommax / 4; i++) { 571103285Sikob if((creg++)->key == key){ 572103285Sikob found = 1; 573103285Sikob break; 574103285Sikob } 575103285Sikob } 576110798Ssimokawa if (!found || creg->key != CROM_TEXTLEAF) { 577103285Sikob strncpy(buf, nullstr, len); 578103285Sikob return; 579103285Sikob } 580103285Sikob src = (u_int32_t *) creg + creg->val; 581103285Sikob clen = ((*src >> 16) - 2) * 4; 582103285Sikob src += 3; 583103285Sikob dst = (u_int32_t *) buf; 584103285Sikob if (len < clen) 585103285Sikob clen = len; 586103285Sikob for (i = 0; i < clen/4; i++) 587103285Sikob *dst++ = htonl(*src++); 588103285Sikob buf[clen] = 0; 589103285Sikob} 590103285Sikob 591103285Sikobstatic void 592103285Sikobsbp_probe_lun(struct sbp_dev *sdev) 593103285Sikob{ 594103285Sikob struct fw_device *fwdev; 595103285Sikob int rev; 596103285Sikob 597103285Sikob fwdev = sdev->target->fwdev; 598103285Sikob bzero(sdev->vendor, sizeof(sdev->vendor)); 599103285Sikob bzero(sdev->product, sizeof(sdev->product)); 600103285Sikob sbp_get_text_leaf(fwdev, 0x03, sdev->vendor, sizeof(sdev->vendor)); 601103285Sikob sbp_get_text_leaf(fwdev, 0x17, sdev->product, sizeof(sdev->product)); 602103285Sikob rev = getcsrdata(sdev->target->fwdev, 0x3c); 603103285Sikob snprintf(sdev->revision, sizeof(sdev->revision), "%06x", rev); 604103285Sikob} 605111615Ssimokawa 606111615Ssimokawa 607103285Sikobstatic void 608111615Ssimokawasbp_login_callout(void *arg) 609103285Sikob{ 610111615Ssimokawa struct sbp_dev *sdev = (struct sbp_dev *)arg; 611111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL); 612111615Ssimokawa} 613111615Ssimokawa 614111615Ssimokawa#define SBP_FWDEV_ALIVE(fwdev) \ 615111615Ssimokawa ((fwdev->status == FWDEVATTACHED) \ 616111615Ssimokawa && (getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10) \ 617111615Ssimokawa && (getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2)) 618111615Ssimokawa 619111615Ssimokawastatic void 620111615Ssimokawasbp_probe_target(void *arg) 621111615Ssimokawa{ 622111615Ssimokawa struct sbp_target *target = (struct sbp_target *)arg; 623103285Sikob struct sbp_softc *sbp; 624103285Sikob struct sbp_dev *sdev; 625103285Sikob struct firewire_comm *fc; 626111615Ssimokawa int i, alive; 627103285Sikob 628111615Ssimokawa alive = SBP_FWDEV_ALIVE(target->fwdev); 629103285SikobSBP_DEBUG(1) 630103285Sikob printf("sbp_probe_target %d\n", target->target_id); 631103285Sikob if (!alive) 632103285Sikob printf("not alive\n"); 633103285SikobEND_DEBUG 634103285Sikob 635103285Sikob sbp = target->sbp; 636103285Sikob fc = target->sbp->fd.fc; 637111615Ssimokawa /* XXX untimeout mgm_ocb and dequeue */ 638103285Sikob for (i=0; i < target->num_lun; i++) { 639103285Sikob sdev = &target->luns[i]; 640103285Sikob if (alive && (sdev->status != SBP_DEV_DEAD)) { 641103285Sikob if (sdev->path != NULL) { 642103285Sikob xpt_freeze_devq(sdev->path, 1); 643111615Ssimokawa sdev->freeze ++; 644103285Sikob } 645111615Ssimokawa sbp_probe_lun(sdev); 646111615SsimokawaSBP_DEBUG(0) 647111615Ssimokawa sbp_show_sdev_info(sdev, 648111615Ssimokawa#if 0 649111615Ssimokawa (sdev->status == SBP_DEV_TOATTACH)); 650111615Ssimokawa#else 651111615Ssimokawa (sdev->status == SBP_DEV_RESET)); 652111615Ssimokawa#endif 653111615SsimokawaEND_DEBUG 654111615Ssimokawa 655111615Ssimokawa sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET); 656103285Sikob switch (sdev->status) { 657110336Ssimokawa case SBP_DEV_RESET: 658103285Sikob /* new or revived target */ 659103285Sikob if (auto_login) { 660111615Ssimokawa#if 0 661103285Sikob sdev->status = SBP_DEV_TOATTACH; 662111615Ssimokawa#endif 663111615Ssimokawa sbp_login(sdev); 664103285Sikob } 665103285Sikob break; 666111615Ssimokawa case SBP_DEV_TOATTACH: 667111615Ssimokawa case SBP_DEV_PROBE: 668111615Ssimokawa case SBP_DEV_ATTACHED: 669110336Ssimokawa case SBP_DEV_RETRY: 670110336Ssimokawa default: 671111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL); 672110336Ssimokawa break; 673103285Sikob } 674103285Sikob } else { 675103285Sikob switch (sdev->status) { 676103285Sikob case SBP_DEV_ATTACHED: 677103285SikobSBP_DEBUG(0) 678103285Sikob /* the device has gone */ 679103285Sikob sbp_show_sdev_info(sdev, 2); 680103285Sikob printf("lost target\n"); 681103285SikobEND_DEBUG 682111615Ssimokawa if (sdev->path) { 683103285Sikob xpt_freeze_devq(sdev->path, 1); 684111615Ssimokawa sdev->freeze ++; 685111615Ssimokawa } 686103285Sikob sdev->status = SBP_DEV_RETRY; 687111615Ssimokawa sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET); 688103285Sikob break; 689103285Sikob case SBP_DEV_PROBE: 690103285Sikob case SBP_DEV_TOATTACH: 691103285Sikob sdev->status = SBP_DEV_RESET; 692103285Sikob break; 693103285Sikob case SBP_DEV_RETRY: 694103285Sikob case SBP_DEV_RESET: 695103285Sikob case SBP_DEV_DEAD: 696103285Sikob break; 697103285Sikob } 698103285Sikob } 699103285Sikob } 700103285Sikob} 701103285Sikob 702103285Sikobstatic void 703103285Sikobsbp_post_explore(void *arg) 704103285Sikob{ 705103285Sikob struct sbp_softc *sbp = (struct sbp_softc *)arg; 706103285Sikob struct sbp_target *target; 707103285Sikob struct fw_device *fwdev; 708103285Sikob int i, alive; 709103285Sikob 710111199SsimokawaSBP_DEBUG(0) 711111199Ssimokawa printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold); 712103285SikobEND_DEBUG 713111615Ssimokawa#if 0 /* 714111615Ssimokawa * XXX don't let CAM the bus rest. CAM tries to do something with 715111615Ssimokawa * freezed (DEV_RETRY) devices 716111615Ssimokawa */ 717111615Ssimokawa xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL); 718103285Sikob#endif 719111199Ssimokawa if (sbp_cold > 0) 720111199Ssimokawa sbp_cold --; 721111615Ssimokawa 722103285Sikob /* Gabage Collection */ 723103285Sikob for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){ 724103285Sikob target = &sbp->targets[i]; 725110193Ssimokawa STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) 726110193Ssimokawa if (target->fwdev == NULL || target->fwdev == fwdev) 727110193Ssimokawa break; 728103285Sikob if(fwdev == NULL){ 729103285Sikob /* device has removed in lower driver */ 730110145Ssimokawa sbp_cam_detach_target(target); 731110145Ssimokawa if (target->luns != NULL) 732110145Ssimokawa free(target->luns, M_SBP); 733110145Ssimokawa target->num_lun = 0;; 734110145Ssimokawa target->luns = NULL; 735110145Ssimokawa target->fwdev = NULL; 736103285Sikob } 737103285Sikob } 738103285Sikob /* traverse device list */ 739110193Ssimokawa STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) { 740103285SikobSBP_DEBUG(0) 741103285Sikob printf("sbp_post_explore: EUI:%08x%08x ", 742103285Sikob fwdev->eui.hi, fwdev->eui.lo); 743103285Sikob if (fwdev->status == FWDEVATTACHED) { 744103285Sikob printf("spec=%d key=%d.\n", 745103285Sikob getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10, 746103285Sikob getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2); 747103285Sikob } else { 748103285Sikob printf("not attached, state=%d.\n", fwdev->status); 749103285Sikob } 750103285SikobEND_DEBUG 751111615Ssimokawa alive = SBP_FWDEV_ALIVE(fwdev); 752103285Sikob for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){ 753103285Sikob target = &sbp->targets[i]; 754103285Sikob if(target->fwdev == fwdev ) { 755103285Sikob /* known target */ 756103285Sikob break; 757103285Sikob } 758103285Sikob } 759103285Sikob if(i == SBP_NUM_TARGETS){ 760103285Sikob if (alive) { 761103285Sikob /* new target */ 762103285Sikob target = sbp_alloc_target(sbp, fwdev); 763103285Sikob if (target == NULL) 764103285Sikob continue; 765103285Sikob } else { 766103285Sikob continue; 767103285Sikob } 768103285Sikob } 769111615Ssimokawa sbp_probe_target((void *)target); 770103285Sikob } 771103285Sikob#if 0 772103285Sikob timeout(sbp_release_queue, (caddr_t)sbp, bus_reset_rest * hz / 1000); 773103285Sikob#endif 774103285Sikob} 775103285Sikob 776103285Sikob#if NEED_RESPONSE 777103285Sikobstatic void 778103285Sikobsbp_loginres_callback(struct fw_xfer *xfer){ 779103285SikobSBP_DEBUG(1) 780103285Sikob struct sbp_dev *sdev; 781103285Sikob sdev = (struct sbp_dev *)xfer->sc; 782103285Sikob sbp_show_sdev_info(sdev, 2); 783103285Sikob printf("sbp_loginres_callback\n"); 784103285SikobEND_DEBUG 785103285Sikob fw_xfer_free(xfer); 786103285Sikob return; 787103285Sikob} 788103285Sikob#endif 789103285Sikob 790103285Sikobstatic void 791103285Sikobsbp_login_callback(struct fw_xfer *xfer) 792103285Sikob{ 793103285SikobSBP_DEBUG(1) 794103285Sikob struct sbp_dev *sdev; 795103285Sikob sdev = (struct sbp_dev *)xfer->sc; 796103285Sikob sbp_show_sdev_info(sdev, 2); 797103285Sikob printf("sbp_login_callback\n"); 798103285SikobEND_DEBUG 799103285Sikob fw_xfer_free(xfer); 800103285Sikob return; 801103285Sikob} 802103285Sikob 803103285Sikobstatic void 804103285Sikobsbp_cmd_callback(struct fw_xfer *xfer) 805103285Sikob{ 806103285SikobSBP_DEBUG(2) 807103285Sikob struct sbp_dev *sdev; 808103285Sikob sdev = (struct sbp_dev *)xfer->sc; 809103285Sikob sbp_show_sdev_info(sdev, 2); 810103285Sikob printf("sbp_cmd_callback\n"); 811103285SikobEND_DEBUG 812103285Sikob fw_xfer_free(xfer); 813103285Sikob return; 814103285Sikob} 815103285Sikob 816111615Ssimokawastatic struct sbp_dev * 817111615Ssimokawasbp_next_dev(struct sbp_target *target, int lun) 818111615Ssimokawa{ 819111615Ssimokawa struct sbp_dev *sdev; 820111615Ssimokawa int i; 821111615Ssimokawa 822111615Ssimokawa for (i = lun, sdev = &target->luns[lun]; 823111615Ssimokawa i < target->num_lun; i++, sdev++) { 824111615Ssimokawa if (sdev->status == SBP_DEV_PROBE) 825111615Ssimokawa break; 826111615Ssimokawa } 827111615Ssimokawa if (i >= target->num_lun) 828111615Ssimokawa return(NULL); 829111615Ssimokawa return(sdev); 830111615Ssimokawa} 831111615Ssimokawa 832111615Ssimokawa#define SCAN_PRI 1 833103285Sikobstatic void 834111615Ssimokawasbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb) 835103285Sikob{ 836111615Ssimokawa struct sbp_target *target; 837103285Sikob struct sbp_dev *sdev; 838111615Ssimokawa 839103285Sikob sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr; 840111615Ssimokawa target = sdev->target; 841110269SsimokawaSBP_DEBUG(0) 842103285Sikob sbp_show_sdev_info(sdev, 2); 843111615Ssimokawa printf("sbp_cam_scan_lun\n"); 844103285SikobEND_DEBUG 845111615Ssimokawa if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { 846111615Ssimokawa sdev->status = SBP_DEV_ATTACHED; 847111615Ssimokawa } else { 848111615Ssimokawa sbp_show_sdev_info(sdev, 2); 849111615Ssimokawa printf("scan failed\n"); 850111615Ssimokawa } 851111615Ssimokawa sdev = sbp_next_dev(target, sdev->lun_id + 1); 852111615Ssimokawa if (sdev == NULL) { 853111615Ssimokawa free(ccb, M_SBP); 854111615Ssimokawa return; 855111615Ssimokawa } 856111615Ssimokawa /* reuse ccb */ 857111615Ssimokawa xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI); 858111615Ssimokawa ccb->ccb_h.ccb_sdev_ptr = sdev; 859111615Ssimokawa xpt_action(ccb); 860111615Ssimokawa xpt_release_devq(sdev->path, sdev->freeze, TRUE); 861111615Ssimokawa sdev->freeze = 1; 862103285Sikob} 863103285Sikob 864103285Sikobstatic void 865111615Ssimokawasbp_cam_scan_target(void *arg) 866103285Sikob{ 867111615Ssimokawa struct sbp_target *target = (struct sbp_target *)arg; 868111615Ssimokawa struct sbp_dev *sdev; 869110798Ssimokawa union ccb *ccb; 870103285Sikob 871111615Ssimokawa sdev = sbp_next_dev(target, 0); 872111615Ssimokawa if (sdev == NULL) { 873111615Ssimokawa printf("sbp_cam_scan_target: nothing to do for target%d\n", 874111615Ssimokawa target->target_id); 875110798Ssimokawa return; 876110798Ssimokawa } 877103285SikobSBP_DEBUG(0) 878103285Sikob sbp_show_sdev_info(sdev, 2); 879111615Ssimokawa printf("sbp_cam_scan_target\n"); 880103285SikobEND_DEBUG 881111615Ssimokawa ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO); 882111615Ssimokawa if (ccb == NULL) { 883111615Ssimokawa printf("sbp_cam_scan_target: malloc failed\n"); 884111615Ssimokawa return; 885111615Ssimokawa } 886111615Ssimokawa xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI); 887103285Sikob ccb->ccb_h.func_code = XPT_SCAN_LUN; 888111615Ssimokawa ccb->ccb_h.cbfcnp = sbp_cam_scan_lun; 889111615Ssimokawa ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 890103285Sikob ccb->crcn.flags = CAM_FLAG_NONE; 891103285Sikob ccb->ccb_h.ccb_sdev_ptr = sdev; 892103285Sikob 893103285Sikob /* The scan is in progress now. */ 894111040Ssimokawa xpt_action(ccb); 895111615Ssimokawa xpt_release_devq(sdev->path, sdev->freeze, TRUE); 896111615Ssimokawa sdev->freeze = 1; 897103285Sikob} 898103285Sikob 899103285Sikob 900111615Ssimokawa#if 0 901103285Sikobstatic void 902103285Sikobsbp_ping_unit_callback(struct cam_periph *periph, union ccb *ccb) 903103285Sikob{ 904103285Sikob struct sbp_dev *sdev; 905103285Sikob sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr; 906110269SsimokawaSBP_DEBUG(0) 907103285Sikob sbp_show_sdev_info(sdev, 2); 908103285Sikob printf("sbp_ping_unit_callback\n"); 909103285SikobEND_DEBUG 910103285Sikob if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 911103285Sikob if (--ccb->ccb_h.retry_count == 0) { 912103285Sikob sbp_show_sdev_info(sdev, 2); 913110336Ssimokawa printf("sbp_ping_unit_callback: " 914110336Ssimokawa "retry count exceeded\n"); 915103285Sikob sdev->status = SBP_DEV_RETRY; 916103285Sikob free(ccb, M_SBP); 917103285Sikob } else { 918103285Sikob /* requeue */ 919103285Sikob xpt_action(ccb); 920111615Ssimokawa xpt_release_devq(sdev->path, sdev->freeze, TRUE); 921111615Ssimokawa sdev->freeze = 1; /* we will freeze */ 922103285Sikob } 923103285Sikob } else { 924103285Sikob free(ccb->csio.data_ptr, M_SBP); 925103285Sikob free(ccb, M_SBP); 926103285Sikob sdev->status = SBP_DEV_ATTACHED; 927111615Ssimokawa xpt_release_devq(sdev->path, sdev->freeze, TRUE); 928111615Ssimokawa sdev->freeze = 0; 929103285Sikob } 930103285Sikob} 931103285Sikob 932103285Sikob/* 933103285Sikob * XXX Some devices need to execute inquiry or read_capacity 934103285Sikob * after bus_rest during busy transfer. 935103285Sikob * Otherwise they return incorrect result for READ(and WRITE?) 936103285Sikob * command without any SBP-II/SCSI error. 937103285Sikob * 938103285Sikob * e.g. Maxtor 3000XT, Yano A-dish. 939103285Sikob */ 940103285Sikobstatic void 941103285Sikobsbp_ping_unit(struct sbp_dev *sdev) 942103285Sikob{ 943103285Sikob union ccb *ccb; 944103285Sikob struct scsi_inquiry_data *inq_buf; 945103285Sikob 946110798Ssimokawa 947110798Ssimokawa ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO); 948110798Ssimokawa if (ccb == NULL) { 949110798Ssimokawa printf("sbp_ping_unit: malloc failed\n"); 950110798Ssimokawa return; 951110798Ssimokawa } 952110798Ssimokawa 953103285Sikob inq_buf = (struct scsi_inquiry_data *) 954110798Ssimokawa malloc(sizeof(*inq_buf), M_SBP, M_NOWAIT); 955110798Ssimokawa if (inq_buf == NULL) { 956110798Ssimokawa free(ccb, M_SBP); 957110798Ssimokawa printf("sbp_ping_unit: malloc failed\n"); 958110798Ssimokawa return; 959110798Ssimokawa } 960103285Sikob 961110269SsimokawaSBP_DEBUG(0) 962103285Sikob sbp_show_sdev_info(sdev, 2); 963103285Sikob printf("sbp_ping_unit\n"); 964103285SikobEND_DEBUG 965103285Sikob 966103285Sikob /* 967103285Sikob * We need to execute this command before any other queued command. 968111615Ssimokawa * Make priority 0 and freeze the queue after execution for retry. 969103285Sikob * cam's scan_lun command doesn't provide this feature. 970103285Sikob */ 971103285Sikob xpt_setup_ccb(&ccb->ccb_h, sdev->path, 0/*priority (high)*/); 972103285Sikob scsi_inquiry( 973103285Sikob &ccb->csio, 974103285Sikob /*retries*/ 5, 975103285Sikob sbp_ping_unit_callback, 976103285Sikob MSG_SIMPLE_Q_TAG, 977103285Sikob (u_int8_t *)inq_buf, 978103285Sikob SHORT_INQUIRY_LENGTH, 979103285Sikob /*evpd*/FALSE, 980103285Sikob /*page_code*/0, 981103285Sikob SSD_MIN_SIZE, 982103285Sikob /*timeout*/60000 983103285Sikob ); 984103285Sikob ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 985111615Ssimokawa ccb->ccb_h.ccb_sdev_ptr = sdev; 986103285Sikob xpt_action(ccb); 987111615Ssimokawa if (sdev->status != SBP_DEV_ATTACHED) 988111615Ssimokawa sdev->status = SBP_DEV_PROBE; 989111615Ssimokawa xpt_release_devq(sdev->path, sdev->freeze, TRUE); 990111615Ssimokawa sdev->freeze = 1; /* We will freeze the queue */ 991111615Ssimokawa} 992111615Ssimokawa#endif 993111615Ssimokawa 994111615Ssimokawastatic __inline void 995111615Ssimokawasbp_scan_dev(struct sbp_dev *sdev) 996111615Ssimokawa{ 997111040Ssimokawa sdev->status = SBP_DEV_PROBE; 998111615Ssimokawa callout_reset(&sdev->target->scan_callout, SCAN_DELAY * hz, 999111615Ssimokawa sbp_cam_scan_target, (void *)sdev->target); 1000103285Sikob} 1001103285Sikob 1002103285Sikobstatic void 1003103285Sikobsbp_do_attach(struct fw_xfer *xfer) 1004103285Sikob{ 1005103285Sikob struct sbp_dev *sdev; 1006111615Ssimokawa struct sbp_target *target; 1007111615Ssimokawa struct sbp_softc *sbp; 1008103285Sikob 1009103285Sikob sdev = (struct sbp_dev *)xfer->sc; 1010111615Ssimokawa target = sdev->target; 1011111615Ssimokawa sbp = target->sbp; 1012103285SikobSBP_DEBUG(0) 1013103285Sikob sbp_show_sdev_info(sdev, 2); 1014103285Sikob printf("sbp_do_attach\n"); 1015103285SikobEND_DEBUG 1016103285Sikob fw_xfer_free(xfer); 1017111199Ssimokawa 1018103285Sikob if (sdev->path == NULL) 1019103285Sikob xpt_create_path(&sdev->path, xpt_periph, 1020111615Ssimokawa cam_sim_path(target->sbp->sim), 1021111615Ssimokawa target->target_id, sdev->lun_id); 1022103285Sikob 1023111199Ssimokawa /* 1024111199Ssimokawa * Let CAM scan the bus if we are in the boot process. 1025111199Ssimokawa * XXX xpt_scan_bus cannot detect LUN larger than 0 1026111199Ssimokawa * if LUN 0 doesn't exists. 1027111199Ssimokawa */ 1028111199Ssimokawa if (sbp_cold > 0) { 1029111615Ssimokawa sdev->status = SBP_DEV_ATTACHED; 1030111199Ssimokawa return; 1031111199Ssimokawa } 1032111199Ssimokawa 1033111615Ssimokawa sbp_scan_dev(sdev); 1034103285Sikob return; 1035103285Sikob} 1036103285Sikob 1037103285Sikobstatic void 1038103285Sikobsbp_agent_reset_callback(struct fw_xfer *xfer) 1039103285Sikob{ 1040103285Sikob struct sbp_dev *sdev; 1041103285Sikob 1042103285Sikob sdev = (struct sbp_dev *)xfer->sc; 1043103285SikobSBP_DEBUG(1) 1044103285Sikob sbp_show_sdev_info(sdev, 2); 1045103285Sikob printf("sbp_cmd_callback\n"); 1046103285SikobEND_DEBUG 1047103285Sikob fw_xfer_free(xfer); 1048111615Ssimokawa if (sdev->path) { 1049111615Ssimokawa xpt_release_devq(sdev->path, sdev->freeze, TRUE); 1050111615Ssimokawa sdev->freeze = 0; 1051111615Ssimokawa } 1052103285Sikob} 1053103285Sikob 1054103285Sikobstatic void 1055110336Ssimokawasbp_agent_reset(struct sbp_dev *sdev) 1056103285Sikob{ 1057103285Sikob struct fw_xfer *xfer; 1058103285Sikob struct fw_pkt *fp; 1059103285Sikob 1060103285SikobSBP_DEBUG(0) 1061103285Sikob sbp_show_sdev_info(sdev, 2); 1062103285Sikob printf("sbp_agent_reset\n"); 1063103285SikobEND_DEBUG 1064103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04); 1065103285Sikob if (xfer == NULL) 1066103285Sikob return; 1067110336Ssimokawa if (sdev->status == SBP_DEV_ATTACHED) 1068110336Ssimokawa xfer->act.hand = sbp_agent_reset_callback; 1069110336Ssimokawa else 1070103285Sikob xfer->act.hand = sbp_do_attach; 1071103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1072103285Sikob fp->mode.wreqq.data = htonl(0xf); 1073103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1074111615Ssimokawa sbp_abort_all_ocbs(sdev, CAM_BDR_SENT); 1075103285Sikob} 1076103285Sikob 1077103285Sikobstatic void 1078103285Sikobsbp_busy_timeout_callback(struct fw_xfer *xfer) 1079103285Sikob{ 1080103285Sikob struct sbp_dev *sdev; 1081103285Sikob 1082103285Sikob sdev = (struct sbp_dev *)xfer->sc; 1083103285SikobSBP_DEBUG(1) 1084103285Sikob sbp_show_sdev_info(sdev, 2); 1085110336Ssimokawa printf("sbp_busy_timeout_callback\n"); 1086103285SikobEND_DEBUG 1087103285Sikob fw_xfer_free(xfer); 1088110336Ssimokawa sbp_agent_reset(sdev); 1089103285Sikob} 1090103285Sikob 1091103285Sikobstatic void 1092103285Sikobsbp_busy_timeout(struct sbp_dev *sdev) 1093103285Sikob{ 1094103285Sikob struct fw_pkt *fp; 1095103285Sikob struct fw_xfer *xfer; 1096103285SikobSBP_DEBUG(0) 1097103285Sikob sbp_show_sdev_info(sdev, 2); 1098103285Sikob printf("sbp_busy_timeout\n"); 1099103285SikobEND_DEBUG 1100103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 1101103285Sikob 1102103285Sikob xfer->act.hand = sbp_busy_timeout_callback; 1103103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1104103285Sikob fp->mode.wreqq.dest_hi = htons(0xffff); 1105110129Ssimokawa fp->mode.wreqq.dest_lo = htonl(0xf0000000 | BUSY_TIMEOUT); 1106110129Ssimokawa fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf); 1107103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1108103285Sikob} 1109103285Sikob 1110103285Sikob#if 0 1111103285Sikobstatic void 1112103285Sikobsbp_reset_start(struct sbp_dev *sdev) 1113103285Sikob{ 1114103285Sikob struct fw_xfer *xfer; 1115103285Sikob struct fw_pkt *fp; 1116103285Sikob 1117103285SikobSBP_DEBUG(0) 1118103285Sikob sbp_show_sdev_info(sdev, 2); 1119103285Sikob printf("sbp_reset_start\n"); 1120103285SikobEND_DEBUG 1121103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 1122103285Sikob 1123103285Sikob xfer->act.hand = sbp_busy_timeout; 1124103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1125103285Sikob fp->mode.wreqq.dest_hi = htons(0xffff); 1126103285Sikob fp->mode.wreqq.dest_lo = htonl(0xf0000000 | RESET_START); 1127103285Sikob fp->mode.wreqq.data = htonl(0xf); 1128103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1129103285Sikob} 1130103285Sikob#endif 1131103285Sikob 1132103285Sikobstatic void 1133103285Sikobsbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb) 1134103285Sikob{ 1135103285Sikob struct fw_xfer *xfer; 1136103285Sikob struct fw_pkt *fp; 1137103285SikobSBP_DEBUG(2) 1138103285Sikob sbp_show_sdev_info(sdev, 2); 1139103285Sikob printf("sbp_orb_pointer\n"); 1140103285SikobEND_DEBUG 1141103285Sikob 1142103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08); 1143103285Sikob if (xfer == NULL) 1144103285Sikob return; 1145103285Sikob xfer->act.hand = sbp_cmd_callback; 1146103285Sikob 1147103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1148103285Sikob fp->mode.wreqb.len = htons(8); 1149103285Sikob fp->mode.wreqb.extcode = 0; 1150103285Sikob fp->mode.wreqb.payload[0] = 1151103285Sikob htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16)); 1152103285Sikob fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0])); 1153103285Sikob 1154103285Sikob if(fw_asyreq(xfer->fc, -1, xfer) != 0){ 1155103285Sikob fw_xfer_free(xfer); 1156103285Sikob ocb->ccb->ccb_h.status = CAM_REQ_INVALID; 1157103285Sikob xpt_done(ocb->ccb); 1158103285Sikob } 1159103285Sikob} 1160103285Sikob 1161103285Sikobstatic void 1162103285Sikobsbp_doorbell(struct sbp_dev *sdev) 1163103285Sikob{ 1164103285Sikob struct fw_xfer *xfer; 1165103285Sikob struct fw_pkt *fp; 1166103285SikobSBP_DEBUG(1) 1167103285Sikob sbp_show_sdev_info(sdev, 2); 1168103285Sikob printf("sbp_doorbell\n"); 1169103285SikobEND_DEBUG 1170103285Sikob 1171103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10); 1172103285Sikob if (xfer == NULL) 1173103285Sikob return; 1174103285Sikob xfer->act.hand = sbp_cmd_callback; 1175103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1176103285Sikob fp->mode.wreqq.data = htonl(0xf); 1177103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1178103285Sikob} 1179103285Sikob 1180103285Sikobstatic struct fw_xfer * 1181103285Sikobsbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset) 1182103285Sikob{ 1183103285Sikob struct fw_xfer *xfer; 1184103285Sikob struct fw_pkt *fp; 1185103285Sikob 1186110269Ssimokawa xfer = fw_xfer_alloc(M_SBP); 1187103285Sikob if(xfer == NULL){ 1188103285Sikob return NULL; 1189103285Sikob } 1190103285Sikob if (tcode == FWTCODE_WREQQ) 1191103285Sikob xfer->send.len = 16; 1192103285Sikob else 1193103285Sikob xfer->send.len = 24; 1194103285Sikob 1195110195Ssimokawa xfer->send.buf = malloc(xfer->send.len, M_FW, M_NOWAIT); 1196103285Sikob if(xfer->send.buf == NULL){ 1197110195Ssimokawa fw_xfer_free(xfer); 1198103285Sikob return NULL; 1199103285Sikob } 1200103285Sikob 1201103285Sikob xfer->send.off = 0; 1202103285Sikob xfer->spd = min(sdev->target->fwdev->speed, max_speed); 1203103285Sikob xfer->sc = (caddr_t)sdev; 1204103285Sikob xfer->fc = sdev->target->sbp->fd.fc; 1205103285Sikob xfer->retry_req = fw_asybusy; 1206103285Sikob 1207103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1208103285Sikob fp->mode.wreqq.dest_hi = htons(sdev->login.cmd_hi); 1209103285Sikob fp->mode.wreqq.dest_lo = htonl(sdev->login.cmd_lo + offset); 1210103285Sikob fp->mode.wreqq.tlrt = 0; 1211103285Sikob fp->mode.wreqq.tcode = tcode; 1212103285Sikob fp->mode.wreqq.pri = 0; 1213103285Sikob xfer->dst = FWLOCALBUS | sdev->target->fwdev->dst; 1214103285Sikob fp->mode.wreqq.dst = htons(xfer->dst); 1215103285Sikob 1216103285Sikob return xfer; 1217103285Sikob 1218103285Sikob} 1219103285Sikob 1220103285Sikobstatic void 1221111615Ssimokawasbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb) 1222103285Sikob{ 1223103285Sikob struct fw_xfer *xfer; 1224103285Sikob struct fw_pkt *fp; 1225103285Sikob struct sbp_ocb *ocb; 1226111615Ssimokawa struct sbp_target *target; 1227103285Sikob int s, nid; 1228103285Sikob 1229111615Ssimokawa target = sdev->target; 1230111615Ssimokawa nid = target->sbp->fd.fc->nodeid | FWLOCALBUS; 1231111615Ssimokawa 1232111615Ssimokawa s = splfw(); 1233111615Ssimokawa if (func == ORB_FUN_RUNQUEUE) { 1234111615Ssimokawa ocb = STAILQ_FIRST(&target->mgm_ocb_queue); 1235111615Ssimokawa if (target->mgm_ocb_cur != NULL || ocb == NULL) { 1236111615Ssimokawa splx(s); 1237111615Ssimokawa return; 1238111615Ssimokawa } 1239111615Ssimokawa STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb); 1240111615Ssimokawa goto start; 1241111615Ssimokawa } 1242111615Ssimokawa if ((ocb = sbp_get_ocb(target->sbp)) == NULL) { 1243111615Ssimokawa target->sbp->flags |= SBP_RESOURCE_SHORTAGE; 1244103285Sikob splx(s); 1245103285Sikob return; 1246103285Sikob } 1247103285Sikob ocb->flags = OCB_ACT_MGM; 1248103285Sikob ocb->sdev = sdev; 1249103285Sikob 1250103285Sikob bzero((void *)(uintptr_t)(volatile void *)ocb->orb, sizeof(ocb->orb)); 1251103285Sikob ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI); 1252103285Sikob ocb->orb[7] = htonl(SBP_DEV2ADDR( 1253111615Ssimokawa device_get_unit(target->sbp->fd.dev), 1254111615Ssimokawa target->target_id, 1255103285Sikob sdev->lun_id)); 1256103285Sikob 1257107653SsimokawaSBP_DEBUG(0) 1258103285Sikob sbp_show_sdev_info(sdev, 2); 1259103285Sikob printf("%s\n", orb_fun_name[(func>>16)&0xf]); 1260107653SsimokawaEND_DEBUG 1261103285Sikob switch (func) { 1262103285Sikob case ORB_FUN_LGI: 1263103285Sikob ocb->orb[2] = htonl(nid << 16); 1264103285Sikob ocb->orb[3] = htonl(vtophys(&sdev->login)); 1265103285Sikob ocb->orb[4] = htonl(ORB_NOTIFY | ORB_EXV | sdev->lun_id); 1266103285Sikob ocb->orb[5] = htonl(sizeof(struct sbp_login_res)); 1267103285Sikob break; 1268110336Ssimokawa case ORB_FUN_ATA: 1269111615Ssimokawa ocb->orb[0] = htonl((0 << 16) | 0); 1270111615Ssimokawa ocb->orb[1] = htonl(vtophys(&aocb->orb[0])); 1271110336Ssimokawa /* fall through */ 1272103285Sikob case ORB_FUN_RCN: 1273103285Sikob case ORB_FUN_LGO: 1274103285Sikob case ORB_FUN_LUR: 1275103285Sikob case ORB_FUN_RST: 1276103285Sikob case ORB_FUN_ATS: 1277103285Sikob ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login.id); 1278103285Sikob break; 1279103285Sikob } 1280103285Sikob 1281111615Ssimokawa if (target->mgm_ocb_cur != NULL) { 1282111615Ssimokawa /* there is a standing ORB */ 1283111615Ssimokawa STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb); 1284111615Ssimokawa splx(s); 1285111615Ssimokawa return; 1286111615Ssimokawa } 1287111615Ssimokawastart: 1288111615Ssimokawa target->mgm_ocb_cur = ocb; 1289111615Ssimokawa splx(s); 1290111615Ssimokawa 1291111615Ssimokawa callout_reset(&target->mgm_ocb_timeout, 5*hz, 1292111615Ssimokawa sbp_timeout, (caddr_t)ocb); 1293103285Sikob xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0); 1294103285Sikob if(xfer == NULL){ 1295103285Sikob return; 1296103285Sikob } 1297103285Sikob xfer->act.hand = sbp_login_callback; 1298103285Sikob 1299103285Sikob fp = (struct fw_pkt *)xfer->send.buf; 1300103285Sikob fp->mode.wreqb.dest_hi = htons(sdev->target->mgm_hi); 1301103285Sikob fp->mode.wreqb.dest_lo = htonl(sdev->target->mgm_lo); 1302103285Sikob fp->mode.wreqb.len = htons(8); 1303103285Sikob fp->mode.wreqb.extcode = 0; 1304108503Ssimokawa fp->mode.wreqb.payload[0] = htonl(nid << 16); 1305103285Sikob fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0])); 1306103285Sikob 1307103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1308103285Sikob} 1309103285Sikob 1310103285Sikobstatic void 1311105792Ssimokawasbp_print_scsi_cmd(struct sbp_ocb *ocb) 1312103285Sikob{ 1313103285Sikob struct ccb_scsiio *csio; 1314103285Sikob 1315103285Sikob csio = &ocb->ccb->csio; 1316103285Sikob printf("%s:%d:%d XPT_SCSI_IO: " 1317103285Sikob "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" 1318103285Sikob ", flags: 0x%02x, " 1319103285Sikob "%db cmd/%db data/%db sense\n", 1320103285Sikob device_get_nameunit(ocb->sdev->target->sbp->fd.dev), 1321103285Sikob ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun, 1322103285Sikob csio->cdb_io.cdb_bytes[0], 1323103285Sikob csio->cdb_io.cdb_bytes[1], 1324103285Sikob csio->cdb_io.cdb_bytes[2], 1325103285Sikob csio->cdb_io.cdb_bytes[3], 1326103285Sikob csio->cdb_io.cdb_bytes[4], 1327103285Sikob csio->cdb_io.cdb_bytes[5], 1328103285Sikob csio->cdb_io.cdb_bytes[6], 1329103285Sikob csio->cdb_io.cdb_bytes[7], 1330103285Sikob csio->cdb_io.cdb_bytes[8], 1331103285Sikob csio->cdb_io.cdb_bytes[9], 1332103285Sikob ocb->ccb->ccb_h.flags & CAM_DIR_MASK, 1333103285Sikob csio->cdb_len, csio->dxfer_len, 1334103285Sikob csio->sense_len); 1335105792Ssimokawa} 1336105792Ssimokawa 1337105792Ssimokawastatic void 1338105792Ssimokawasbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb) 1339105792Ssimokawa{ 1340105792Ssimokawa struct sbp_cmd_status *sbp_cmd_status; 1341105792Ssimokawa struct scsi_sense_data *sense; 1342105792Ssimokawa 1343105792Ssimokawa sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data; 1344105792Ssimokawa sense = &ocb->ccb->csio.sense_data; 1345105792Ssimokawa 1346105792SsimokawaSBP_DEBUG(0) 1347105792Ssimokawa sbp_print_scsi_cmd(ocb); 1348103285Sikob /* XXX need decode status */ 1349103285Sikob sbp_show_sdev_info(ocb->sdev, 2); 1350103285Sikob printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d", 1351103285Sikob sbp_cmd_status->status, 1352103285Sikob sbp_cmd_status->sfmt, 1353103285Sikob sbp_cmd_status->valid, 1354103285Sikob sbp_cmd_status->s_key, 1355103285Sikob sbp_cmd_status->s_code, 1356103285Sikob sbp_cmd_status->s_qlfr, 1357103285Sikob sbp_status->len 1358103285Sikob ); 1359103285Sikob#if 0 /* XXX */ 1360103285Sikob if (sbp_cmd_status->status == SCSI_STATUS_CHECK_COND) { 1361103285Sikob printf(" %s\n", scsi_sense_key_text[sbp_cmd_status->s_key]); 1362103285Sikob scsi_sense_desc( 1363103285Sikob sbp_cmd_status->s_code, 1364103285Sikob sbp_cmd_status->s_qlfr, 1365103285Sikob ocb->ccb->ccb_h.path->device->inq_data 1366103285Sikob ) 1367103285Sikob } else { 1368103285Sikob printf("\n"); 1369103285Sikob } 1370103285Sikob#else 1371103285Sikob printf("\n"); 1372103285Sikob#endif 1373103285SikobEND_DEBUG 1374103285Sikob 1375110071Ssimokawa switch (sbp_cmd_status->status) { 1376110071Ssimokawa case SCSI_STATUS_CHECK_COND: 1377110071Ssimokawa case SCSI_STATUS_BUSY: 1378110071Ssimokawa case SCSI_STATUS_CMD_TERMINATED: 1379103285Sikob if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){ 1380103285Sikob sense->error_code = SSD_CURRENT_ERROR; 1381103285Sikob }else{ 1382103285Sikob sense->error_code = SSD_DEFERRED_ERROR; 1383103285Sikob } 1384103285Sikob if(sbp_cmd_status->valid) 1385103285Sikob sense->error_code |= SSD_ERRCODE_VALID; 1386103285Sikob sense->flags = sbp_cmd_status->s_key; 1387103285Sikob if(sbp_cmd_status->mark) 1388103285Sikob sense->flags |= SSD_FILEMARK; 1389103285Sikob if(sbp_cmd_status->eom) 1390103285Sikob sense->flags |= SSD_EOM; 1391103285Sikob if(sbp_cmd_status->ill_len) 1392103285Sikob sense->flags |= SSD_ILI; 1393103285Sikob sense->info[0] = ntohl(sbp_cmd_status->info) & 0xff; 1394103285Sikob sense->info[1] =(ntohl(sbp_cmd_status->info) >> 8) & 0xff; 1395103285Sikob sense->info[2] =(ntohl(sbp_cmd_status->info) >> 16) & 0xff; 1396103285Sikob sense->info[3] =(ntohl(sbp_cmd_status->info) >> 24) & 0xff; 1397103285Sikob if (sbp_status->len <= 1) 1398103285Sikob /* XXX not scsi status. shouldn't be happened */ 1399103285Sikob sense->extra_len = 0; 1400103285Sikob else if (sbp_status->len <= 4) 1401103285Sikob /* add_sense_code(_qual), info, cmd_spec_info */ 1402103285Sikob sense->extra_len = 6; 1403103285Sikob else 1404103285Sikob /* fru, sense_key_spec */ 1405103285Sikob sense->extra_len = 10; 1406103285Sikob sense->cmd_spec_info[0] = ntohl(sbp_cmd_status->cdb) & 0xff; 1407103285Sikob sense->cmd_spec_info[1] = (ntohl(sbp_cmd_status->cdb) >> 8) & 0xff; 1408103285Sikob sense->cmd_spec_info[2] = (ntohl(sbp_cmd_status->cdb) >> 16) & 0xff; 1409103285Sikob sense->cmd_spec_info[3] = (ntohl(sbp_cmd_status->cdb) >> 24) & 0xff; 1410103285Sikob sense->add_sense_code = sbp_cmd_status->s_code; 1411103285Sikob sense->add_sense_code_qual = sbp_cmd_status->s_qlfr; 1412103285Sikob sense->fru = sbp_cmd_status->fru; 1413103285Sikob sense->sense_key_spec[0] = ntohl(sbp_cmd_status->s_keydep) & 0xff; 1414103285Sikob sense->sense_key_spec[1] = (ntohl(sbp_cmd_status->s_keydep) >>8) & 0xff; 1415103285Sikob sense->sense_key_spec[2] = (ntohl(sbp_cmd_status->s_keydep) >>16) & 0xff; 1416103285Sikob 1417103285Sikob ocb->ccb->csio.scsi_status = sbp_cmd_status->status;; 1418103285Sikob ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR 1419103285Sikob | CAM_AUTOSNS_VALID; 1420103285Sikob/* 1421103285Sikob{ 1422103285Sikob u_int8_t j, *tmp; 1423103285Sikob tmp = sense; 1424103285Sikob for( j = 0 ; j < 32 ; j+=8){ 1425103285Sikob printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n", 1426103285Sikob tmp[j], tmp[j+1], tmp[j+2], tmp[j+3], 1427103285Sikob tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]); 1428103285Sikob } 1429103285Sikob 1430103285Sikob} 1431103285Sikob*/ 1432110071Ssimokawa break; 1433110071Ssimokawa default: 1434110071Ssimokawa sbp_show_sdev_info(ocb->sdev, 2); 1435110071Ssimokawa printf("sbp_scsi_status: unknown scsi status 0x%x\n", 1436110071Ssimokawa sbp_cmd_status->status); 1437103285Sikob } 1438103285Sikob} 1439103285Sikob 1440103285Sikobstatic void 1441103285Sikobsbp_fix_inq_data(struct sbp_ocb *ocb) 1442103285Sikob{ 1443103285Sikob union ccb *ccb; 1444103285Sikob struct sbp_dev *sdev; 1445103285Sikob struct scsi_inquiry_data *inq; 1446103285Sikob 1447103285Sikob ccb = ocb->ccb; 1448103285Sikob sdev = ocb->sdev; 1449103285Sikob 1450103285Sikob if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD) 1451103285Sikob return; 1452103285SikobSBP_DEBUG(1) 1453103285Sikob sbp_show_sdev_info(sdev, 2); 1454103285Sikob printf("sbp_fix_inq_data\n"); 1455103285SikobEND_DEBUG 1456103285Sikob inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr; 1457103285Sikob switch (SID_TYPE(inq)) { 1458103285Sikob case T_DIRECT: 1459103285Sikob /* 1460103285Sikob * XXX Convert Direct Access device to RBC. 1461108281Ssimokawa * I've never seen FireWire DA devices which support READ_6. 1462103285Sikob */ 1463103285Sikob#if 1 1464103285Sikob if (SID_TYPE(inq) == T_DIRECT) 1465103285Sikob inq->device |= T_RBC; /* T_DIRECT == 0 */ 1466103285Sikob#endif 1467103285Sikob /* fall through */ 1468103285Sikob case T_RBC: 1469111615Ssimokawa /* enable tag queuing */ 1470111615Ssimokawa#if 1 1471111615Ssimokawa inq->flags |= SID_CmdQue; 1472111615Ssimokawa#endif 1473103285Sikob /* 1474103285Sikob * Override vendor/product/revision information. 1475103285Sikob * Some devices sometimes return strange strings. 1476103285Sikob */ 1477111615Ssimokawa#if 1 1478103285Sikob bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor)); 1479103285Sikob bcopy(sdev->product, inq->product, sizeof(inq->product)); 1480103285Sikob bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision)); 1481111615Ssimokawa#endif 1482103285Sikob break; 1483103285Sikob } 1484103285Sikob} 1485103285Sikob 1486103285Sikobstatic void 1487103285Sikobsbp_recv1(struct fw_xfer *xfer){ 1488103285Sikob struct fw_pkt *rfp; 1489103285Sikob#if NEED_RESPONSE 1490103285Sikob struct fw_pkt *sfp; 1491103285Sikob#endif 1492103285Sikob struct sbp_softc *sbp; 1493103285Sikob struct sbp_dev *sdev; 1494103285Sikob struct sbp_ocb *ocb; 1495103285Sikob struct sbp_login_res *login_res = NULL; 1496103285Sikob struct sbp_status *sbp_status; 1497103285Sikob struct sbp_target *target; 1498111704Ssimokawa int orb_fun, status_valid0, status_valid, t, l, reset_agent = 0; 1499103285Sikob u_int32_t addr; 1500103285Sikob/* 1501103285Sikob u_int32_t *ld; 1502103285Sikob ld = xfer->recv.buf; 1503103285Sikobprintf("sbp %x %d %d %08x %08x %08x %08x\n", 1504103285Sikob xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3])); 1505103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 1506103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11])); 1507103285Sikob*/ 1508103285Sikob if(xfer->resp != 0){ 1509103285Sikob printf("sbp_recv: xfer->resp != 0\n"); 1510103285Sikob fw_xfer_free( xfer); 1511103285Sikob return; 1512103285Sikob } 1513103285Sikob if(xfer->recv.buf == NULL){ 1514103285Sikob printf("sbp_recv: xfer->recv.buf == NULL\n"); 1515103285Sikob fw_xfer_free( xfer); 1516103285Sikob return; 1517103285Sikob } 1518103285Sikob sbp = (struct sbp_softc *)xfer->sc; 1519103285Sikob rfp = (struct fw_pkt *)xfer->recv.buf; 1520103285Sikob if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){ 1521103285Sikob printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode); 1522103285Sikob fw_xfer_free( xfer); 1523103285Sikob return; 1524103285Sikob } 1525103285Sikob sbp_status = (struct sbp_status *)rfp->mode.wreqb.payload; 1526103285Sikob addr = ntohl(rfp->mode.wreqb.dest_lo); 1527103285SikobSBP_DEBUG(2) 1528103285Sikob printf("received address 0x%x\n", addr); 1529103285SikobEND_DEBUG 1530110189Ssimokawa t = SBP_ADDR2TRG(addr); 1531110189Ssimokawa if (t >= SBP_NUM_TARGETS) { 1532110189Ssimokawa device_printf(sbp->fd.dev, 1533110189Ssimokawa "sbp_recv1: invalid target %d\n", t); 1534110189Ssimokawa fw_xfer_free(xfer); 1535110189Ssimokawa return; 1536110189Ssimokawa } 1537110189Ssimokawa target = &sbp->targets[t]; 1538110189Ssimokawa l = SBP_ADDR2LUN(addr); 1539110189Ssimokawa if (l >= target->num_lun) { 1540110189Ssimokawa device_printf(sbp->fd.dev, 1541110189Ssimokawa "sbp_recv1: invalid lun %d (target=%d)\n", l, t); 1542110189Ssimokawa fw_xfer_free(xfer); 1543110189Ssimokawa return; 1544110189Ssimokawa } 1545110189Ssimokawa sdev = &target->luns[l]; 1546103285Sikob 1547110189Ssimokawa ocb = NULL; 1548110189Ssimokawa switch (sbp_status->src) { 1549110189Ssimokawa case 0: 1550110189Ssimokawa case 1: 1551111615Ssimokawa /* check mgm_ocb_cur first */ 1552111615Ssimokawa ocb = target->mgm_ocb_cur; 1553111615Ssimokawa if (ocb != NULL) { 1554111615Ssimokawa if (OCB_MATCH(ocb, sbp_status)) { 1555111615Ssimokawa callout_stop(&target->mgm_ocb_timeout); 1556111615Ssimokawa target->mgm_ocb_cur = NULL; 1557111615Ssimokawa break; 1558111615Ssimokawa } 1559111615Ssimokawa } 1560111615Ssimokawa ocb = sbp_dequeue_ocb(sdev, sbp_status); 1561110189Ssimokawa if (ocb == NULL) { 1562110189Ssimokawa sbp_show_sdev_info(sdev, 2); 1563110189Ssimokawa printf("No ocb on the queue\n"); 1564110189Ssimokawa } 1565110189Ssimokawa break; 1566110189Ssimokawa case 2: 1567110189Ssimokawa /* unsolicit */ 1568110189Ssimokawa sbp_show_sdev_info(sdev, 2); 1569110189Ssimokawa printf("unsolicit status received\n"); 1570110189Ssimokawa break; 1571110189Ssimokawa default: 1572110189Ssimokawa sbp_show_sdev_info(sdev, 2); 1573110189Ssimokawa printf("unknown sbp_status->src\n"); 1574110189Ssimokawa } 1575110189Ssimokawa 1576111615Ssimokawa status_valid0 = (sbp_status->src < 2 1577110189Ssimokawa && sbp_status->resp == ORB_RES_CMPL 1578111615Ssimokawa && sbp_status->dead == 0); 1579111615Ssimokawa status_valid = (status_valid0 && sbp_status->status == 0); 1580103285Sikob 1581111615Ssimokawa if (!status_valid0 || debug > 1){ 1582103285Sikob int status; 1583110129SsimokawaSBP_DEBUG(0) 1584103285Sikob sbp_show_sdev_info(sdev, 2); 1585103285Sikob printf("ORB status src:%x resp:%x dead:%x" 1586108712Ssimokawa#if __FreeBSD_version >= 500000 1587103285Sikob " len:%x stat:%x orb:%x%08x\n", 1588108712Ssimokawa#else 1589108712Ssimokawa " len:%x stat:%x orb:%x%08lx\n", 1590108712Ssimokawa#endif 1591103285Sikob sbp_status->src, sbp_status->resp, sbp_status->dead, 1592103285Sikob sbp_status->len, sbp_status->status, 1593108280Ssimokawa ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo)); 1594110129SsimokawaEND_DEBUG 1595103285Sikob sbp_show_sdev_info(sdev, 2); 1596103285Sikob status = sbp_status->status; 1597103285Sikob switch(sbp_status->resp) { 1598103285Sikob case 0: 1599103285Sikob if (status > MAX_ORB_STATUS0) 1600103285Sikob printf("%s\n", orb_status0[MAX_ORB_STATUS0]); 1601110189Ssimokawa else 1602110129Ssimokawa printf("%s\n", orb_status0[status]); 1603103285Sikob break; 1604103285Sikob case 1: 1605110336Ssimokawa printf("Obj: %s, Error: %s\n", 1606103285Sikob orb_status1_object[(status>>6) & 3], 1607103285Sikob orb_status1_serial_bus_error[status & 0xf]); 1608103285Sikob break; 1609110129Ssimokawa case 2: 1610110129Ssimokawa printf("Illegal request\n"); 1611110129Ssimokawa break; 1612110129Ssimokawa case 3: 1613110129Ssimokawa printf("Vendor dependent\n"); 1614110129Ssimokawa break; 1615103285Sikob default: 1616110129Ssimokawa printf("unknown respose code %d\n", sbp_status->resp); 1617103285Sikob } 1618103285Sikob } 1619103285Sikob 1620103285Sikob /* we have to reset the fetch agent if it's dead */ 1621103285Sikob if (sbp_status->dead) { 1622111615Ssimokawa if (sdev->path) { 1623103285Sikob xpt_freeze_devq(sdev->path, 1); 1624111615Ssimokawa sdev->freeze ++; 1625111615Ssimokawa } 1626111704Ssimokawa reset_agent = 1; 1627103285Sikob } 1628103285Sikob 1629111704Ssimokawa if (ocb == NULL) 1630111704Ssimokawa goto done; 1631103285Sikob 1632110336Ssimokawa sdev->flags &= ~SBP_DEV_TIMEOUT; 1633110336Ssimokawa 1634103285Sikob switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){ 1635103285Sikob case ORB_FMT_NOP: 1636103285Sikob break; 1637103285Sikob case ORB_FMT_VED: 1638103285Sikob break; 1639103285Sikob case ORB_FMT_STD: 1640103285Sikob switch(ocb->flags & OCB_ACT_MASK){ 1641103285Sikob case OCB_ACT_MGM: 1642103285Sikob orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK; 1643103285Sikob switch(orb_fun) { 1644103285Sikob case ORB_FUN_LGI: 1645103285Sikob login_res = &sdev->login; 1646103285Sikob login_res->len = ntohs(login_res->len); 1647103285Sikob login_res->id = ntohs(login_res->id); 1648103285Sikob login_res->cmd_hi = ntohs(login_res->cmd_hi); 1649103285Sikob login_res->cmd_lo = ntohl(login_res->cmd_lo); 1650103285Sikob if (status_valid) { 1651103285SikobSBP_DEBUG(0) 1652103285Sikobsbp_show_sdev_info(sdev, 2); 1653103285Sikobprintf("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)); 1654103285SikobEND_DEBUG 1655111615Ssimokawa#if 0 1656111615Ssimokawa sdev->status = SBP_DEV_TOATTACH; 1657111615Ssimokawa#endif 1658103285Sikob#if 1 1659103285Sikob sbp_busy_timeout(sdev); 1660103285Sikob#else 1661111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL); 1662103285Sikob#endif 1663103285Sikob } else { 1664110336Ssimokawa /* forgot logout? */ 1665110336Ssimokawa sbp_show_sdev_info(sdev, 2); 1666103285Sikob printf("login failed\n"); 1667103285Sikob sdev->status = SBP_DEV_RESET; 1668103285Sikob } 1669103285Sikob break; 1670103285Sikob case ORB_FUN_RCN: 1671103285Sikob login_res = &sdev->login; 1672103285Sikob if (status_valid) { 1673103285SikobSBP_DEBUG(0) 1674103285Sikobsbp_show_sdev_info(sdev, 2); 1675103285Sikobprintf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo); 1676103285SikobEND_DEBUG 1677103285Sikob#if 1 1678111615Ssimokawa if (sdev->status == SBP_DEV_ATTACHED) 1679111615Ssimokawa sbp_scan_dev(sdev); 1680111615Ssimokawa else 1681111615Ssimokawa sbp_agent_reset(sdev); 1682103285Sikob#else 1683110336Ssimokawa sdev->status = SBP_DEV_ATTACHED; 1684111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL); 1685103285Sikob#endif 1686103285Sikob } else { 1687103285Sikob /* reconnection hold time exceed? */ 1688110336SsimokawaSBP_DEBUG(0) 1689110336Ssimokawa sbp_show_sdev_info(sdev, 2); 1690103285Sikob printf("reconnect failed\n"); 1691110336SsimokawaEND_DEBUG 1692111615Ssimokawa sbp_login(sdev); 1693103285Sikob } 1694103285Sikob break; 1695103285Sikob case ORB_FUN_LGO: 1696103285Sikob sdev->status = SBP_DEV_RESET; 1697103285Sikob break; 1698110336Ssimokawa case ORB_FUN_RST: 1699110336Ssimokawa sbp_busy_timeout(sdev); 1700110336Ssimokawa break; 1701103285Sikob case ORB_FUN_LUR: 1702103285Sikob case ORB_FUN_ATA: 1703103285Sikob case ORB_FUN_ATS: 1704110336Ssimokawa sbp_agent_reset(sdev); 1705103285Sikob break; 1706103285Sikob default: 1707110336Ssimokawa sbp_show_sdev_info(sdev, 2); 1708110336Ssimokawa printf("unknown function %d\n", orb_fun); 1709103285Sikob break; 1710103285Sikob } 1711111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL); 1712103285Sikob break; 1713103285Sikob case OCB_ACT_CMD: 1714103285Sikob if(ocb->ccb != NULL){ 1715103285Sikob union ccb *ccb; 1716103285Sikob/* 1717103285Sikob u_int32_t *ld; 1718103285Sikob ld = ocb->ccb->csio.data_ptr; 1719103285Sikob if(ld != NULL && ocb->ccb->csio.dxfer_len != 0) 1720103285Sikob printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]); 1721103285Sikob else 1722103285Sikob printf("ptr NULL\n"); 1723103285Sikobprintf("len %d\n", sbp_status->len); 1724103285Sikob*/ 1725103285Sikob ccb = ocb->ccb; 1726103285Sikob if(sbp_status->len > 1){ 1727103285Sikob sbp_scsi_status(sbp_status, ocb); 1728103285Sikob }else{ 1729103285Sikob if(sbp_status->resp != ORB_RES_CMPL){ 1730103285Sikob ccb->ccb_h.status = CAM_REQ_CMP_ERR; 1731103285Sikob }else{ 1732103285Sikob ccb->ccb_h.status = CAM_REQ_CMP; 1733103285Sikob } 1734103285Sikob } 1735103285Sikob /* fix up inq data */ 1736103285Sikob if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY) 1737103285Sikob sbp_fix_inq_data(ocb); 1738103285Sikob xpt_done(ccb); 1739103285Sikob } 1740103285Sikob break; 1741103285Sikob default: 1742103285Sikob break; 1743103285Sikob } 1744103285Sikob } 1745103285Sikob 1746103285Sikob if (!(ocb->flags & OCB_RESERVED)) 1747103285Sikob sbp_free_ocb(sbp, ocb); 1748111704Ssimokawadone: 1749111704Ssimokawa if (reset_agent) 1750111704Ssimokawa sbp_agent_reset(sdev); 1751103285Sikob 1752103285Sikob/* The received packet is usually small enough to be stored within 1753103285Sikob * the buffer. In that case, the controller return ack_complete and 1754103285Sikob * no respose is necessary. 1755103285Sikob * 1756103285Sikob * XXX fwohci.c and firewire.c should inform event_code such as 1757103285Sikob * ack_complete or ack_pending to upper driver. 1758103285Sikob */ 1759103285Sikob#if NEED_RESPONSE 1760103285Sikob xfer->send.buf = malloc(12, M_SBP, M_NOWAIT | M_ZERO); 1761103285Sikob xfer->send.len = 12; 1762103285Sikob xfer->send.off = 0; 1763103285Sikob sfp = (struct fw_pkt *)xfer->send.buf; 1764103285Sikob sfp->mode.wres.dst = rfp->mode.wreqb.src; 1765103285Sikob xfer->dst = ntohs(sfp->mode.wres.dst); 1766103285Sikob xfer->spd = min(sdev->target->fwdev->speed, max_speed); 1767103285Sikob xfer->act.hand = sbp_loginres_callback; 1768103285Sikob xfer->retry_req = fw_asybusy; 1769103285Sikob 1770103285Sikob sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt; 1771103285Sikob sfp->mode.wres.tcode = FWTCODE_WRES; 1772103285Sikob sfp->mode.wres.rtcode = 0; 1773103285Sikob sfp->mode.wres.pri = 0; 1774103285Sikob 1775103285Sikob fw_asyreq(xfer->fc, -1, xfer); 1776103285Sikob#else 1777103285Sikob fw_xfer_free(xfer); 1778103285Sikob#endif 1779103285Sikob 1780103285Sikob return; 1781103285Sikob 1782103285Sikob} 1783103285Sikob 1784103285Sikobstatic void 1785103285Sikobsbp_recv(struct fw_xfer *xfer) 1786103285Sikob{ 1787103285Sikob int s; 1788103285Sikob 1789103285Sikob s = splcam(); 1790103285Sikob sbp_recv1(xfer); 1791103285Sikob splx(s); 1792103285Sikob} 1793103285Sikob/* 1794103285Sikob * sbp_attach() 1795103285Sikob */ 1796103285Sikobstatic int 1797103285Sikobsbp_attach(device_t dev) 1798103285Sikob{ 1799103285Sikob struct sbp_softc *sbp; 1800103285Sikob struct cam_devq *devq; 1801103285Sikob struct fw_xfer *xfer; 1802103285Sikob int i, s, error; 1803103285Sikob 1804103285SikobSBP_DEBUG(0) 1805111199Ssimokawa printf("sbp_attach (cold=%d)\n", cold); 1806103285SikobEND_DEBUG 1807103285Sikob 1808111199Ssimokawa if (cold) 1809111199Ssimokawa sbp_cold ++; 1810103285Sikob sbp = ((struct sbp_softc *)device_get_softc(dev)); 1811103285Sikob bzero(sbp, sizeof(struct sbp_softc)); 1812103285Sikob sbp->fd.dev = dev; 1813103285Sikob sbp->fd.fc = device_get_ivars(dev); 1814103285Sikob error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1, 1815103285Sikob /*boundary*/0, 1816103285Sikob /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 1817103285Sikob /*highaddr*/BUS_SPACE_MAXADDR, 1818103285Sikob /*filter*/NULL, /*filterarg*/NULL, 1819103285Sikob /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX, 1820103285Sikob /*maxsegsz*/0x8000, 1821103285Sikob /*flags*/BUS_DMA_ALLOCNOW, 1822103285Sikob &sbp->dmat); 1823103285Sikob if (error != 0) { 1824103285Sikob printf("sbp_attach: Could not allocate DMA tag " 1825103285Sikob "- error %d\n", error); 1826103285Sikob return (ENOMEM); 1827103285Sikob } 1828103285Sikob 1829103285Sikob devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB); 1830103285Sikob if (devq == NULL) 1831103285Sikob return (ENXIO); 1832103285Sikob 1833103285Sikob for( i = 0 ; i < SBP_NUM_TARGETS ; i++){ 1834103285Sikob sbp->targets[i].fwdev = NULL; 1835103285Sikob sbp->targets[i].luns = NULL; 1836103285Sikob } 1837103285Sikob 1838103285Sikob sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp, 1839103285Sikob device_get_unit(dev), 1840111615Ssimokawa /*untagged*/ 1, 1841111615Ssimokawa /*tagged*/ SBP_QUEUE_LEN, 1842111615Ssimokawa devq); 1843103285Sikob 1844103285Sikob if (sbp->sim == NULL) { 1845103285Sikob cam_simq_free(devq); 1846103285Sikob return (ENXIO); 1847103285Sikob } 1848103285Sikob 1849103285Sikob sbp->ocb = (struct sbp_ocb *) contigmalloc( 1850103285Sikob sizeof (struct sbp_ocb) * SBP_NUM_OCB, 1851109424Ssimokawa M_SBP, M_NOWAIT, 0x10000, 0xffffffff, PAGE_SIZE, 0ul); 1852103285Sikob bzero(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB); 1853103285Sikob 1854103285Sikob if (sbp->ocb == NULL) { 1855103285Sikob printf("sbp0: ocb alloction failure\n"); 1856103285Sikob return (ENOMEM); 1857103285Sikob } 1858103285Sikob 1859103285Sikob STAILQ_INIT(&sbp->free_ocbs); 1860103285Sikob for (i = 0; i < SBP_NUM_OCB; i++) { 1861103285Sikob sbp_free_ocb(sbp, &sbp->ocb[i]); 1862103285Sikob } 1863103285Sikob 1864111615Ssimokawa if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS) 1865111615Ssimokawa goto fail; 1866103285Sikob 1867111615Ssimokawa if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim), 1868111615Ssimokawa CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) 1869111615Ssimokawa goto fail; 1870111615Ssimokawa 1871110269Ssimokawa xfer = fw_xfer_alloc(M_SBP); 1872103285Sikob xfer->act.hand = sbp_recv; 1873103285Sikob xfer->act_type = FWACT_XFER; 1874103285Sikob#if NEED_RESPONSE 1875103285Sikob xfer->fc = sbp->fd.fc; 1876103285Sikob#endif 1877103285Sikob xfer->sc = (caddr_t)sbp; 1878103285Sikob 1879103285Sikob sbp->fwb.start_hi = SBP_BIND_HI; 1880103285Sikob sbp->fwb.start_lo = SBP_DEV2ADDR(device_get_unit(sbp->fd.dev), 0, 0); 1881103285Sikob /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */ 1882103285Sikob sbp->fwb.addrlen = 0xffff; 1883103285Sikob sbp->fwb.xfer = xfer; 1884103285Sikob fw_bindadd(sbp->fd.fc, &sbp->fwb); 1885103285Sikob 1886103285Sikob sbp->fd.post_explore = sbp_post_explore; 1887103285Sikob 1888111199Ssimokawa if (sbp->fd.fc->status != -1) { 1889111199Ssimokawa s = splfw(); 1890111199Ssimokawa sbp_post_explore((void *)sbp); 1891111199Ssimokawa splx(s); 1892111199Ssimokawa } 1893111199Ssimokawa 1894103285Sikob return (0); 1895111615Ssimokawafail: 1896111615Ssimokawa cam_sim_free(sbp->sim, /*free_devq*/TRUE); 1897111615Ssimokawa contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB, M_SBP); 1898111615Ssimokawa return (ENXIO); 1899103285Sikob} 1900103285Sikob 1901103285Sikobstatic int 1902110145Ssimokawasbp_logout_all(struct sbp_softc *sbp) 1903110145Ssimokawa{ 1904110145Ssimokawa struct sbp_target *target; 1905110145Ssimokawa struct sbp_dev *sdev; 1906110145Ssimokawa int i, j; 1907110145Ssimokawa 1908110145SsimokawaSBP_DEBUG(0) 1909110145Ssimokawa printf("sbp_logout_all\n"); 1910110145SsimokawaEND_DEBUG 1911110145Ssimokawa for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) { 1912110145Ssimokawa target = &sbp->targets[i]; 1913110145Ssimokawa if (target->luns == NULL) 1914110145Ssimokawa continue; 1915110145Ssimokawa for (j = 0; j < target->num_lun; j++) { 1916110145Ssimokawa sdev = &target->luns[j]; 1917110336Ssimokawa if (sdev->status >= SBP_DEV_TOATTACH && 1918110336Ssimokawa sdev->status <= SBP_DEV_ATTACHED) 1919111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL); 1920110145Ssimokawa } 1921110145Ssimokawa } 1922110145Ssimokawa return 0; 1923110145Ssimokawa} 1924110145Ssimokawa 1925110145Ssimokawastatic int 1926110145Ssimokawasbp_shutdown(device_t dev) 1927110145Ssimokawa{ 1928110145Ssimokawa struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev)); 1929110145Ssimokawa 1930110145Ssimokawa sbp_logout_all(sbp); 1931110145Ssimokawa return (0); 1932110145Ssimokawa} 1933110145Ssimokawa 1934110145Ssimokawastatic int 1935103285Sikobsbp_detach(device_t dev) 1936103285Sikob{ 1937103285Sikob struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev)); 1938103285Sikob struct firewire_comm *fc = sbp->fd.fc; 1939103285Sikob int i; 1940103285Sikob 1941103285SikobSBP_DEBUG(0) 1942103285Sikob printf("sbp_detach\n"); 1943103285SikobEND_DEBUG 1944110145Ssimokawa#if 0 1945103285Sikob /* bus reset for logout */ 1946103285Sikob sbp->fd.post_explore = NULL; 1947103285Sikob fc->ibr(fc); 1948110145Ssimokawa#endif 1949103285Sikob 1950103285Sikob for (i = 0; i < SBP_NUM_TARGETS; i ++) 1951110145Ssimokawa sbp_cam_detach_target(&sbp->targets[i]); 1952111615Ssimokawa xpt_free_path(sbp->path); 1953103285Sikob xpt_bus_deregister(cam_sim_path(sbp->sim)); 1954110145Ssimokawa 1955110145Ssimokawa sbp_logout_all(sbp); 1956110145Ssimokawa /* XXX wait for logout completion */ 1957110145Ssimokawa tsleep(&i, FWPRI, "sbpdtc", hz/2); 1958110145Ssimokawa 1959110145Ssimokawa fw_bindremove(fc, &sbp->fwb); 1960110145Ssimokawa contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB, M_SBP); 1961103285Sikob bus_dma_tag_destroy(sbp->dmat); 1962110145Ssimokawa 1963110145Ssimokawa for (i = 0; i < SBP_NUM_TARGETS; i ++) 1964110145Ssimokawa if (sbp->targets[i].luns != NULL) 1965110145Ssimokawa free(sbp->targets[i].luns, M_SBP); 1966110145Ssimokawa 1967103285Sikob return (0); 1968103285Sikob} 1969103285Sikob 1970103285Sikobstatic void 1971110145Ssimokawasbp_cam_detach_target(struct sbp_target *target) 1972103285Sikob{ 1973103285Sikob int i; 1974103285Sikob struct sbp_dev *sdev; 1975103285Sikob 1976103285Sikob if (target->luns != NULL) { 1977108529SsimokawaSBP_DEBUG(0) 1978103285Sikob printf("sbp_detach_target %d\n", target->target_id); 1979108529SsimokawaEND_DEBUG 1980111615Ssimokawa callout_stop(&target->scan_callout); 1981111615Ssimokawa callout_stop(&target->mgm_ocb_timeout); 1982110193Ssimokawa for (i = 0; i < target->num_lun; i++) { 1983103285Sikob sdev = &target->luns[i]; 1984111615Ssimokawa callout_stop(&sdev->login_callout); 1985103285Sikob if (sdev->status == SBP_DEV_RESET || 1986103285Sikob sdev->status == SBP_DEV_DEAD) 1987103285Sikob continue; 1988110336Ssimokawa if (sdev->path) { 1989103285Sikob xpt_async(AC_LOST_DEVICE, sdev->path, NULL); 1990110336Ssimokawa xpt_free_path(sdev->path); 1991110336Ssimokawa sdev->path = NULL; 1992110336Ssimokawa } 1993103285Sikob sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE); 1994103285Sikob } 1995103285Sikob } 1996103285Sikob} 1997103285Sikob 1998103285Sikobstatic void 1999103285Sikobsbp_timeout(void *arg) 2000103285Sikob{ 2001103285Sikob struct sbp_ocb *ocb = (struct sbp_ocb *)arg; 2002103285Sikob struct sbp_dev *sdev = ocb->sdev; 2003103285Sikob 2004103285Sikob sbp_show_sdev_info(sdev, 2); 2005110336Ssimokawa printf("request timeout ... "); 2006103285Sikob 2007111615Ssimokawa if (ocb->flags == OCB_ACT_MGM) { 2008111615Ssimokawa printf("management ORB\n"); 2009111615Ssimokawa /* XXX just ignore for now */ 2010111615Ssimokawa sdev->target->mgm_ocb_cur = NULL; 2011111704Ssimokawa sbp_free_ocb(sdev->target->sbp, ocb); 2012111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL); 2013111615Ssimokawa return; 2014111615Ssimokawa } 2015111615Ssimokawa 2016110336Ssimokawa xpt_freeze_devq(sdev->path, 1); 2017111615Ssimokawa sdev->freeze ++; 2018110336Ssimokawa sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT); 2019110336Ssimokawa if (sdev->flags & SBP_DEV_TIMEOUT) { 2020110269Ssimokawa#if 0 2021110336Ssimokawa struct firewire_comm *fc; 2022110336Ssimokawa 2023110336Ssimokawa printf("bus reset\n"); 2024110336Ssimokawa fc = sdev->target->sbp->fd.fc; 2025110336Ssimokawa fc->ibr(fc); 2026110336Ssimokawa sdev->status == SBP_DEV_RETRY; 2027110269Ssimokawa#else 2028110336Ssimokawa printf("target reset\n"); 2029111615Ssimokawa sbp_mgm_orb(sdev, ORB_FUN_RST, NULL); 2030110269Ssimokawa#endif 2031110336Ssimokawa sdev->flags &= ~SBP_DEV_TIMEOUT; 2032110336Ssimokawa } else { 2033110336Ssimokawa printf("agent reset\n"); 2034110336Ssimokawa sdev->flags |= SBP_DEV_TIMEOUT; 2035110336Ssimokawa sbp_agent_reset(sdev); 2036110336Ssimokawa } 2037103285Sikob return; 2038103285Sikob} 2039103285Sikob 2040103285Sikobstatic void 2041103285Sikobsbp_action1(struct cam_sim *sim, union ccb *ccb) 2042103285Sikob{ 2043103285Sikob 2044103285Sikob struct sbp_softc *sbp = (struct sbp_softc *)sim->softc; 2045103285Sikob struct sbp_target *target = NULL; 2046103285Sikob struct sbp_dev *sdev = NULL; 2047103285Sikob 2048103285Sikob /* target:lun -> sdev mapping */ 2049103285Sikob if (sbp != NULL 2050103285Sikob && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD 2051103285Sikob && ccb->ccb_h.target_id < SBP_NUM_TARGETS) { 2052103285Sikob target = &sbp->targets[ccb->ccb_h.target_id]; 2053103285Sikob if (target->fwdev != NULL 2054103285Sikob && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD 2055103285Sikob && ccb->ccb_h.target_lun < target->num_lun) { 2056103285Sikob sdev = &target->luns[ccb->ccb_h.target_lun]; 2057103285Sikob if (sdev->status != SBP_DEV_ATTACHED && 2058103285Sikob sdev->status != SBP_DEV_PROBE) 2059103285Sikob sdev = NULL; 2060103285Sikob } 2061103285Sikob } 2062103285Sikob 2063103285SikobSBP_DEBUG(1) 2064103285Sikob if (sdev == NULL) 2065103285Sikob printf("invalid target %d lun %d\n", 2066103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2067103285SikobEND_DEBUG 2068103285Sikob 2069103285Sikob switch (ccb->ccb_h.func_code) { 2070103285Sikob case XPT_SCSI_IO: 2071103285Sikob case XPT_RESET_DEV: 2072103285Sikob case XPT_GET_TRAN_SETTINGS: 2073103285Sikob case XPT_SET_TRAN_SETTINGS: 2074103285Sikob case XPT_CALC_GEOMETRY: 2075103285Sikob if (sdev == NULL) { 2076103285SikobSBP_DEBUG(1) 2077103285Sikob printf("%s:%d:%d:func_code 0x%04x: " 2078103285Sikob "Invalid target (target needed)\n", 2079103285Sikob device_get_nameunit(sbp->fd.dev), 2080103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2081103285Sikob ccb->ccb_h.func_code); 2082103285SikobEND_DEBUG 2083103285Sikob 2084108276Ssimokawa ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2085103285Sikob xpt_done(ccb); 2086103285Sikob return; 2087103285Sikob } 2088103285Sikob break; 2089103285Sikob case XPT_PATH_INQ: 2090103285Sikob case XPT_NOOP: 2091103285Sikob /* The opcodes sometimes aimed at a target (sc is valid), 2092103285Sikob * sometimes aimed at the SIM (sc is invalid and target is 2093103285Sikob * CAM_TARGET_WILDCARD) 2094103285Sikob */ 2095103285Sikob if (sbp == NULL && 2096103285Sikob ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) { 2097103285SikobSBP_DEBUG(0) 2098103285Sikob printf("%s:%d:%d func_code 0x%04x: " 2099103285Sikob "Invalid target (no wildcard)\n", 2100103285Sikob device_get_nameunit(sbp->fd.dev), 2101103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2102103285Sikob ccb->ccb_h.func_code); 2103103285SikobEND_DEBUG 2104108276Ssimokawa ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2105103285Sikob xpt_done(ccb); 2106103285Sikob return; 2107103285Sikob } 2108103285Sikob break; 2109103285Sikob default: 2110103285Sikob /* XXX Hm, we should check the input parameters */ 2111103285Sikob break; 2112103285Sikob } 2113103285Sikob 2114103285Sikob switch (ccb->ccb_h.func_code) { 2115103285Sikob case XPT_SCSI_IO: 2116103285Sikob { 2117103285Sikob struct ccb_scsiio *csio; 2118103285Sikob struct sbp_ocb *ocb; 2119103285Sikob int s, speed; 2120106506Ssimokawa void *cdb; 2121103285Sikob 2122103285Sikob csio = &ccb->csio; 2123103285Sikob 2124103285SikobSBP_DEBUG(1) 2125103285Sikob printf("%s:%d:%d XPT_SCSI_IO: " 2126103285Sikob "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x" 2127103285Sikob ", flags: 0x%02x, " 2128103285Sikob "%db cmd/%db data/%db sense\n", 2129103285Sikob device_get_nameunit(sbp->fd.dev), 2130103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2131103285Sikob csio->cdb_io.cdb_bytes[0], 2132103285Sikob csio->cdb_io.cdb_bytes[1], 2133103285Sikob csio->cdb_io.cdb_bytes[2], 2134103285Sikob csio->cdb_io.cdb_bytes[3], 2135103285Sikob csio->cdb_io.cdb_bytes[4], 2136103285Sikob csio->cdb_io.cdb_bytes[5], 2137103285Sikob csio->cdb_io.cdb_bytes[6], 2138103285Sikob csio->cdb_io.cdb_bytes[7], 2139103285Sikob csio->cdb_io.cdb_bytes[8], 2140103285Sikob csio->cdb_io.cdb_bytes[9], 2141103285Sikob ccb->ccb_h.flags & CAM_DIR_MASK, 2142103285Sikob csio->cdb_len, csio->dxfer_len, 2143103285Sikob csio->sense_len); 2144103285SikobEND_DEBUG 2145103285Sikob if(sdev == NULL){ 2146103285Sikob ccb->ccb_h.status = CAM_DEV_NOT_THERE; 2147103285Sikob xpt_done(ccb); 2148103285Sikob return; 2149103285Sikob } 2150103285Sikob#if 0 2151103285Sikob /* if we are in probe stage, pass only probe commands */ 2152103285Sikob if (sdev->status == SBP_DEV_PROBE) { 2153103285Sikob char *name; 2154103285Sikob name = xpt_path_periph(ccb->ccb_h.path)->periph_name; 2155103285Sikob printf("probe stage, periph name: %s\n", name); 2156103285Sikob if (strcmp(name, "probe") != 0) { 2157103285Sikob ccb->ccb_h.status = CAM_REQUEUE_REQ; 2158103285Sikob xpt_done(ccb); 2159103285Sikob return; 2160103285Sikob } 2161103285Sikob } 2162103285Sikob#endif 2163103285Sikob if ((ocb = sbp_get_ocb(sbp)) == NULL) { 2164103285Sikob s = splfw(); 2165103285Sikob sbp->flags |= SBP_RESOURCE_SHORTAGE; 2166103285Sikob splx(s); 2167103285Sikob return; 2168103285Sikob } 2169103285Sikob ocb->flags = OCB_ACT_CMD; 2170103285Sikob ocb->sdev = sdev; 2171103285Sikob ocb->ccb = ccb; 2172103285Sikob ccb->ccb_h.ccb_sdev_ptr = sdev; 2173103285Sikob ocb->orb[0] = htonl(1 << 31); 2174103285Sikob ocb->orb[1] = 0; 2175103285Sikob ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) ); 2176103285Sikob ocb->orb[3] = htonl(vtophys(ocb->ind_ptr)); 2177103285Sikob speed = min(target->fwdev->speed, max_speed); 2178103285Sikob ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed) 2179103285Sikob | ORB_CMD_MAXP(speed + 7)); 2180103285Sikob if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){ 2181103285Sikob ocb->orb[4] |= htonl(ORB_CMD_IN); 2182103285Sikob } 2183103285Sikob 2184103285Sikob if (csio->ccb_h.flags & CAM_SCATTER_VALID) 2185103285Sikob printf("sbp: CAM_SCATTER_VALID\n"); 2186103285Sikob if (csio->ccb_h.flags & CAM_DATA_PHYS) 2187103285Sikob printf("sbp: CAM_DATA_PHYS\n"); 2188103285Sikob 2189106506Ssimokawa if (csio->ccb_h.flags & CAM_CDB_POINTER) 2190106506Ssimokawa cdb = (void *)csio->cdb_io.cdb_ptr; 2191106506Ssimokawa else 2192106506Ssimokawa cdb = (void *)&csio->cdb_io.cdb_bytes; 2193106506Ssimokawa bcopy(cdb, 2194103285Sikob (void *)(uintptr_t)(volatile void *)&ocb->orb[5], 2195106506Ssimokawa csio->cdb_len); 2196103285Sikob/* 2197103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3])); 2198103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7])); 2199103285Sikob*/ 2200103285Sikob if (ccb->csio.dxfer_len > 0) { 2201103285Sikob int s; 2202103285Sikob 2203103285Sikob if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) { 2204103285Sikob printf("sbp_action1: cannot create dmamap\n"); 2205103285Sikob break; 2206103285Sikob } 2207103285Sikob 2208103285Sikob s = splsoftvm(); 2209103285Sikob bus_dmamap_load(/*dma tag*/sbp->dmat, 2210103285Sikob /*dma map*/ocb->dmamap, 2211103285Sikob ccb->csio.data_ptr, 2212103285Sikob ccb->csio.dxfer_len, 2213103285Sikob sbp_execute_ocb, 2214103285Sikob ocb, 2215103285Sikob /*flags*/0); 2216103285Sikob splx(s); 2217103285Sikob } else 2218103285Sikob sbp_execute_ocb(ocb, NULL, 0, 0); 2219103285Sikob break; 2220103285Sikob } 2221103285Sikob case XPT_CALC_GEOMETRY: 2222103285Sikob { 2223103285Sikob struct ccb_calc_geometry *ccg; 2224103285Sikob u_int32_t size_mb; 2225103285Sikob u_int32_t secs_per_cylinder; 2226103285Sikob int extended = 1; 2227103285Sikob ccg = &ccb->ccg; 2228103285Sikob 2229103285Sikob if (ccg->block_size == 0) { 2230103285Sikob printf("sbp_action1: block_size is 0.\n"); 2231103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 2232103285Sikob xpt_done(ccb); 2233103285Sikob break; 2234103285Sikob } 2235103285SikobSBP_DEBUG(1) 2236103285Sikob printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: " 2237103285Sikob "Volume size = %d\n", 2238103285Sikob device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim), 2239103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2240103285Sikob ccg->volume_size); 2241103285SikobEND_DEBUG 2242103285Sikob 2243103285Sikob size_mb = ccg->volume_size 2244103285Sikob / ((1024L * 1024L) / ccg->block_size); 2245103285Sikob 2246103285Sikob if (size_mb >= 1024 && extended) { 2247103285Sikob ccg->heads = 255; 2248103285Sikob ccg->secs_per_track = 63; 2249103285Sikob } else { 2250103285Sikob ccg->heads = 64; 2251103285Sikob ccg->secs_per_track = 32; 2252103285Sikob } 2253103285Sikob secs_per_cylinder = ccg->heads * ccg->secs_per_track; 2254103285Sikob ccg->cylinders = ccg->volume_size / secs_per_cylinder; 2255103285Sikob ccb->ccb_h.status = CAM_REQ_CMP; 2256103285Sikob xpt_done(ccb); 2257103285Sikob break; 2258103285Sikob } 2259103285Sikob case XPT_RESET_BUS: /* Reset the specified SCSI bus */ 2260103285Sikob { 2261103285Sikob 2262103285SikobSBP_DEBUG(1) 2263103285Sikob printf("%s:%d:XPT_RESET_BUS: \n", 2264103285Sikob device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim)); 2265103285SikobEND_DEBUG 2266103285Sikob 2267103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 2268103285Sikob xpt_done(ccb); 2269103285Sikob break; 2270103285Sikob } 2271103285Sikob case XPT_PATH_INQ: /* Path routing inquiry */ 2272103285Sikob { 2273103285Sikob struct ccb_pathinq *cpi = &ccb->cpi; 2274103285Sikob 2275103285SikobSBP_DEBUG(1) 2276103285Sikob printf("%s:%d:%d XPT_PATH_INQ:.\n", 2277103285Sikob device_get_nameunit(sbp->fd.dev), 2278103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2279103285SikobEND_DEBUG 2280103285Sikob cpi->version_num = 1; /* XXX??? */ 2281111615Ssimokawa cpi->hba_inquiry = PI_TAG_ABLE; 2282103285Sikob cpi->target_sprt = 0; 2283111199Ssimokawa cpi->hba_misc = PIM_NOBUSRESET; 2284103285Sikob cpi->hba_eng_cnt = 0; 2285103285Sikob cpi->max_target = SBP_NUM_TARGETS - 1; 2286103285Sikob cpi->max_lun = SBP_NUM_LUNS - 1; 2287103285Sikob cpi->initiator_id = SBP_INITIATOR; 2288103285Sikob cpi->bus_id = sim->bus_id; 2289103285Sikob cpi->base_transfer_speed = 400 * 1000 / 8; 2290103285Sikob strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2291103285Sikob strncpy(cpi->hba_vid, "SBP", HBA_IDLEN); 2292103285Sikob strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN); 2293103285Sikob cpi->unit_number = sim->unit_number; 2294103285Sikob 2295103285Sikob cpi->ccb_h.status = CAM_REQ_CMP; 2296103285Sikob xpt_done(ccb); 2297103285Sikob break; 2298103285Sikob } 2299103285Sikob case XPT_GET_TRAN_SETTINGS: 2300103285Sikob { 2301103285Sikob struct ccb_trans_settings *cts = &ccb->cts; 2302103285SikobSBP_DEBUG(1) 2303103285Sikob printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n", 2304103285Sikob device_get_nameunit(sbp->fd.dev), 2305103285Sikob ccb->ccb_h.target_id, ccb->ccb_h.target_lun); 2306103285SikobEND_DEBUG 2307111615Ssimokawa /* Enable disconnect and tagged queuing */ 2308103285Sikob cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID; 2309111615Ssimokawa cts->flags = CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB; 2310103285Sikob 2311103285Sikob cts->ccb_h.status = CAM_REQ_CMP; 2312103285Sikob xpt_done(ccb); 2313103285Sikob break; 2314103285Sikob } 2315103285Sikob case XPT_ABORT: 2316103285Sikob ccb->ccb_h.status = CAM_UA_ABORT; 2317103285Sikob xpt_done(ccb); 2318103285Sikob break; 2319111615Ssimokawa case XPT_SET_TRAN_SETTINGS: 2320111615Ssimokawa /* XXX */ 2321103285Sikob default: 2322103285Sikob ccb->ccb_h.status = CAM_REQ_INVALID; 2323103285Sikob xpt_done(ccb); 2324103285Sikob break; 2325103285Sikob } 2326103285Sikob return; 2327103285Sikob} 2328103285Sikob 2329103285Sikobstatic void 2330103285Sikobsbp_action(struct cam_sim *sim, union ccb *ccb) 2331103285Sikob{ 2332103285Sikob int s; 2333103285Sikob 2334103285Sikob s = splfw(); 2335103285Sikob sbp_action1(sim, ccb); 2336103285Sikob splx(s); 2337103285Sikob} 2338103285Sikob 2339103285Sikobstatic void 2340103285Sikobsbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error) 2341103285Sikob{ 2342103285Sikob int i; 2343103285Sikob struct sbp_ocb *ocb; 2344103285Sikob struct sbp_ocb *prev; 2345103285Sikob union ccb *ccb; 2346105633Ssimokawa bus_dma_segment_t *s; 2347103285Sikob 2348103285Sikob if (error) 2349103285Sikob printf("sbp_execute_ocb: error=%d\n", error); 2350103285Sikob 2351103285Sikob ocb = (struct sbp_ocb *)arg; 2352103285Sikob if (seg == 1) { 2353103285Sikob /* direct pointer */ 2354103285Sikob ocb->orb[3] = htonl(segments[0].ds_addr); 2355103285Sikob ocb->orb[4] |= htonl(segments[0].ds_len); 2356103285Sikob } else if(seg > 1) { 2357103285Sikob /* page table */ 2358103285SikobSBP_DEBUG(1) 2359103285Sikob printf("sbp_execute_ocb: seg %d", seg); 2360103285Sikob for (i = 0; i < seg; i++) 2361108712Ssimokawa#if __FreeBSD_version >= 500000 2362106543Ssimokawa printf(", %tx:%zd", segments[i].ds_addr, 2363108712Ssimokawa#else 2364108877Ssimokawa printf(", %x:%d", segments[i].ds_addr, 2365108712Ssimokawa#endif 2366103285Sikob segments[i].ds_len); 2367103285Sikob printf("\n"); 2368103285SikobEND_DEBUG 2369103285Sikob for (i = 0; i < seg; i++) { 2370105633Ssimokawa s = &segments[i]; 2371108877SsimokawaSBP_DEBUG(0) 2372108877Ssimokawa /* XXX LSI Logic "< 16 byte" bug might be hit */ 2373105633Ssimokawa if (s->ds_len < 16) 2374105633Ssimokawa printf("sbp_execute_ocb: warning, " 2375108877Ssimokawa#if __FreeBSD_version >= 500000 2376106543Ssimokawa "segment length(%zd) is less than 16." 2377108877Ssimokawa#else 2378108877Ssimokawa "segment length(%d) is less than 16." 2379108877Ssimokawa#endif 2380105633Ssimokawa "(seg=%d/%d)\n", s->ds_len, i+1, seg); 2381108877SsimokawaEND_DEBUG 2382105633Ssimokawa ocb->ind_ptr[i].hi = htonl(s->ds_len << 16); 2383105633Ssimokawa ocb->ind_ptr[i].lo = htonl(s->ds_addr); 2384103285Sikob } 2385103285Sikob ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg); 2386103285Sikob } 2387103285Sikob 2388103285Sikob ccb = ocb->ccb; 2389103285Sikob prev = sbp_enqueue_ocb(ocb->sdev, ocb); 2390103285Sikob if (prev) 2391103285Sikob sbp_doorbell(ocb->sdev); 2392103285Sikob else 2393103285Sikob sbp_orb_pointer(ocb->sdev, ocb); 2394103285Sikob} 2395103285Sikob 2396103285Sikobstatic void 2397103285Sikobsbp_poll(struct cam_sim *sim) 2398103285Sikob{ 2399103285Sikob /* should call fwohci_intr? */ 2400103285Sikob return; 2401103285Sikob} 2402103285Sikobstatic struct sbp_ocb * 2403111615Ssimokawasbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status) 2404103285Sikob{ 2405103285Sikob struct sbp_ocb *ocb; 2406103285Sikob struct sbp_ocb *next; 2407103285Sikob int s = splfw(), order = 0; 2408103285Sikob int flags; 2409103285Sikob 2410103285Sikob for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) { 2411103285Sikob next = STAILQ_NEXT(ocb, ocb); 2412103285Sikob flags = ocb->flags; 2413103285SikobSBP_DEBUG(1) 2414110336Ssimokawa sbp_show_sdev_info(sdev, 2); 2415108712Ssimokawa#if __FreeBSD_version >= 500000 2416106543Ssimokawa printf("orb: 0x%tx next: 0x%x, flags %x\n", 2417108712Ssimokawa#else 2418108712Ssimokawa printf("orb: 0x%x next: 0x%lx, flags %x\n", 2419108712Ssimokawa#endif 2420103285Sikob vtophys(&ocb->orb[0]), ntohl(ocb->orb[1]), flags); 2421103285SikobEND_DEBUG 2422111615Ssimokawa if (OCB_MATCH(ocb, sbp_status)) { 2423103285Sikob /* found */ 2424103285Sikob if (ocb->flags & OCB_RESERVED) 2425103285Sikob ocb->flags |= OCB_DONE; 2426103285Sikob else 2427103285Sikob STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb); 2428103285Sikob if (ocb->ccb != NULL) 2429103285Sikob untimeout(sbp_timeout, (caddr_t)ocb, 2430103285Sikob ocb->ccb->ccb_h.timeout_ch); 2431103285Sikob if (ocb->dmamap != NULL) { 2432103285Sikob bus_dmamap_destroy(sdev->target->sbp->dmat, 2433103285Sikob ocb->dmamap); 2434103285Sikob ocb->dmamap = NULL; 2435103285Sikob } 2436103285Sikob break; 2437103285Sikob } else { 2438103285Sikob if ((ocb->flags & OCB_RESERVED) && 2439103285Sikob (ocb->flags & OCB_DONE)) { 2440103285Sikob /* next orb must be fetched already */ 2441103285Sikob STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb); 2442103285Sikob sbp_free_ocb(sdev->target->sbp, ocb); 2443103285Sikob } else 2444103285Sikob order ++; 2445103285Sikob } 2446103285Sikob } 2447103285Sikob splx(s); 2448103285SikobSBP_DEBUG(0) 2449103285Sikob if (ocb && order > 0) { 2450103285Sikob sbp_show_sdev_info(sdev, 2); 2451103285Sikob printf("unordered execution order:%d\n", order); 2452103285Sikob } 2453103285SikobEND_DEBUG 2454103285Sikob return (ocb); 2455103285Sikob} 2456103285Sikob 2457103285Sikobstatic struct sbp_ocb * 2458103285Sikobsbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb) 2459103285Sikob{ 2460103285Sikob int s = splfw(); 2461103285Sikob struct sbp_ocb *prev; 2462103285Sikob 2463103285SikobSBP_DEBUG(2) 2464103285Sikob sbp_show_sdev_info(sdev, 2); 2465108712Ssimokawa#if __FreeBSD_version >= 500000 2466106543Ssimokawa printf("sbp_enqueue_ocb orb=0x%tx in physical memory\n", vtophys(&ocb->orb[0])); 2467108712Ssimokawa#else 2468108712Ssimokawa printf("sbp_enqueue_ocb orb=0x%x in physical memory\n", vtophys(&ocb->orb[0])); 2469108712Ssimokawa#endif 2470103285SikobEND_DEBUG 2471103285Sikob prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb); 2472103285Sikob STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb); 2473103285Sikob 2474103285Sikob if (ocb->ccb != NULL) 2475103285Sikob ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb, 2476103285Sikob (ocb->ccb->ccb_h.timeout * hz) / 1000); 2477103285Sikob 2478110579Ssimokawa if (prev != NULL ) { 2479103285SikobSBP_DEBUG(1) 2480108712Ssimokawa#if __FreeBSD_version >= 500000 2481106543Ssimokawa printf("linking chain 0x%tx -> 0x%tx\n", vtophys(&prev->orb[0]), 2482108712Ssimokawa#else 2483108712Ssimokawa printf("linking chain 0x%x -> 0x%x\n", vtophys(&prev->orb[0]), 2484108712Ssimokawa#endif 2485103285Sikob vtophys(&ocb->orb[0])); 2486103285SikobEND_DEBUG 2487103285Sikob prev->flags |= OCB_RESERVED; 2488103285Sikob prev->orb[1] = htonl(vtophys(&ocb->orb[0])); 2489103285Sikob prev->orb[0] = 0; 2490103285Sikob } 2491103285Sikob splx(s); 2492103285Sikob 2493103285Sikob return prev; 2494103285Sikob} 2495103285Sikob 2496103285Sikobstatic struct sbp_ocb * 2497103285Sikobsbp_get_ocb(struct sbp_softc *sbp) 2498103285Sikob{ 2499103285Sikob struct sbp_ocb *ocb; 2500103285Sikob int s = splfw(); 2501103285Sikob ocb = STAILQ_FIRST(&sbp->free_ocbs); 2502103285Sikob if (ocb == NULL) { 2503103285Sikob printf("ocb shortage!!!\n"); 2504103285Sikob return NULL; 2505103285Sikob } 2506111615Ssimokawa STAILQ_REMOVE_HEAD(&sbp->free_ocbs, ocb); 2507103285Sikob splx(s); 2508103285Sikob ocb->ccb = NULL; 2509103285Sikob return (ocb); 2510103285Sikob} 2511103285Sikob 2512103285Sikobstatic void 2513103285Sikobsbp_free_ocb(struct sbp_softc *sbp, struct sbp_ocb *ocb) 2514103285Sikob{ 2515103285Sikob#if 0 /* XXX make sure that ocb has ccb */ 2516103285Sikob if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0 && 2517103285Sikob (ocb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) { 2518103285Sikob ocb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 2519103285Sikob sbp->flags &= ~SBP_RESOURCE_SHORTAGE; 2520103285Sikob } 2521103285Sikob#else 2522103285Sikob if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0) 2523103285Sikob sbp->flags &= ~SBP_RESOURCE_SHORTAGE; 2524103285Sikob#endif 2525103285Sikob ocb->flags = 0; 2526103285Sikob ocb->ccb = NULL; 2527103285Sikob STAILQ_INSERT_TAIL(&sbp->free_ocbs, ocb, ocb); 2528103285Sikob} 2529103285Sikob 2530103285Sikobstatic void 2531103285Sikobsbp_abort_ocb(struct sbp_ocb *ocb, int status) 2532103285Sikob{ 2533103285Sikob struct sbp_dev *sdev; 2534103285Sikob 2535103285Sikob sdev = ocb->sdev; 2536110269SsimokawaSBP_DEBUG(1) 2537103285Sikob sbp_show_sdev_info(sdev, 2); 2538103285Sikob printf("sbp_abort_ocb 0x%x\n", status); 2539105792Ssimokawa if (ocb->ccb != NULL) 2540105792Ssimokawa sbp_print_scsi_cmd(ocb); 2541103285SikobEND_DEBUG 2542103285Sikob if (ocb->ccb != NULL && !(ocb->flags & OCB_DONE)) { 2543110336Ssimokawa untimeout(sbp_timeout, (caddr_t)ocb, 2544110336Ssimokawa ocb->ccb->ccb_h.timeout_ch); 2545103285Sikob ocb->ccb->ccb_h.status = status; 2546103285Sikob xpt_done(ocb->ccb); 2547103285Sikob } 2548103285Sikob if (ocb->dmamap != NULL) { 2549103285Sikob bus_dmamap_destroy(sdev->target->sbp->dmat, ocb->dmamap); 2550103285Sikob ocb->dmamap = NULL; 2551103285Sikob } 2552103285Sikob sbp_free_ocb(sdev->target->sbp, ocb); 2553103285Sikob} 2554103285Sikob 2555103285Sikobstatic void 2556103285Sikobsbp_abort_all_ocbs(struct sbp_dev *sdev, int status) 2557103285Sikob{ 2558103285Sikob int s; 2559105792Ssimokawa struct sbp_ocb *ocb, *next; 2560105792Ssimokawa STAILQ_HEAD(, sbp_ocb) temp; 2561103285Sikob 2562103285Sikob s = splfw(); 2563105792Ssimokawa 2564105792Ssimokawa bcopy(&sdev->ocbs, &temp, sizeof(temp)); 2565105792Ssimokawa STAILQ_INIT(&sdev->ocbs); 2566105792Ssimokawa for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) { 2567105792Ssimokawa next = STAILQ_NEXT(ocb, ocb); 2568103285Sikob sbp_abort_ocb(ocb, status); 2569103285Sikob } 2570105792Ssimokawa 2571103285Sikob splx(s); 2572103285Sikob} 2573103285Sikob 2574103285Sikobstatic devclass_t sbp_devclass; 2575103285Sikob 2576103285Sikobstatic device_method_t sbp_methods[] = { 2577103285Sikob /* device interface */ 2578103285Sikob DEVMETHOD(device_identify, sbp_identify), 2579103285Sikob DEVMETHOD(device_probe, sbp_probe), 2580103285Sikob DEVMETHOD(device_attach, sbp_attach), 2581103285Sikob DEVMETHOD(device_detach, sbp_detach), 2582110145Ssimokawa DEVMETHOD(device_shutdown, sbp_shutdown), 2583103285Sikob 2584103285Sikob { 0, 0 } 2585103285Sikob}; 2586103285Sikob 2587103285Sikobstatic driver_t sbp_driver = { 2588103285Sikob "sbp", 2589103285Sikob sbp_methods, 2590103285Sikob sizeof(struct sbp_softc), 2591103285Sikob}; 2592103285SikobDRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0); 2593103285SikobMODULE_VERSION(sbp, 1); 2594103285SikobMODULE_DEPEND(sbp, firewire, 1, 1, 1); 2595103285SikobMODULE_DEPEND(sbp, cam, 1, 1, 1); 2596