1 1.43 andvar /* $NetBSD: sbp.c,v 1.43 2025/07/11 22:19:53 andvar Exp $ */ 2 1.1 kiyohara /*- 3 1.1 kiyohara * Copyright (c) 2003 Hidetoshi Shimokawa 4 1.1 kiyohara * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa 5 1.1 kiyohara * All rights reserved. 6 1.1 kiyohara * 7 1.1 kiyohara * Redistribution and use in source and binary forms, with or without 8 1.1 kiyohara * modification, are permitted provided that the following conditions 9 1.1 kiyohara * are met: 10 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright 11 1.1 kiyohara * notice, this list of conditions and the following disclaimer. 12 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the 14 1.1 kiyohara * documentation and/or other materials provided with the distribution. 15 1.1 kiyohara * 3. All advertising materials mentioning features or use of this software 16 1.1 kiyohara * must display the acknowledgement as bellow: 17 1.1 kiyohara * 18 1.1 kiyohara * This product includes software developed by K. Kobayashi and H. Shimokawa 19 1.1 kiyohara * 20 1.1 kiyohara * 4. The name of the author may not be used to endorse or promote products 21 1.1 kiyohara * derived from this software without specific prior written permission. 22 1.1 kiyohara * 23 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 1.1 kiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 1.1 kiyohara * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 26 1.1 kiyohara * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 27 1.1 kiyohara * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 28 1.1 kiyohara * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 29 1.1 kiyohara * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 1.1 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31 1.1 kiyohara * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 1.1 kiyohara * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 1.1 kiyohara * POSSIBILITY OF SUCH DAMAGE. 34 1.29 kiyohara * 35 1.29 kiyohara * $FreeBSD: src/sys/dev/firewire/sbp.c,v 1.100 2009/02/18 18:41:34 sbruno Exp $ 36 1.1 kiyohara * 37 1.1 kiyohara */ 38 1.1 kiyohara 39 1.20 lukem #include <sys/cdefs.h> 40 1.43 andvar __KERNEL_RCSID(0, "$NetBSD: sbp.c,v 1.43 2025/07/11 22:19:53 andvar Exp $"); 41 1.1 kiyohara 42 1.1 kiyohara 43 1.1 kiyohara #include <sys/param.h> 44 1.1 kiyohara #include <sys/device.h> 45 1.1 kiyohara #include <sys/errno.h> 46 1.1 kiyohara #include <sys/buf.h> 47 1.29 kiyohara #include <sys/callout.h> 48 1.29 kiyohara #include <sys/condvar.h> 49 1.1 kiyohara #include <sys/kernel.h> 50 1.1 kiyohara #include <sys/kthread.h> 51 1.32 christos #include <sys/malloc.h> 52 1.29 kiyohara #include <sys/mutex.h> 53 1.1 kiyohara #include <sys/proc.h> 54 1.1 kiyohara #include <sys/sysctl.h> 55 1.1 kiyohara 56 1.18 ad #include <sys/bus.h> 57 1.1 kiyohara 58 1.1 kiyohara #include <dev/scsipi/scsi_spc.h> 59 1.1 kiyohara #include <dev/scsipi/scsi_all.h> 60 1.1 kiyohara #include <dev/scsipi/scsipi_all.h> 61 1.1 kiyohara #include <dev/scsipi/scsiconf.h> 62 1.1 kiyohara #include <dev/scsipi/scsipiconf.h> 63 1.1 kiyohara 64 1.1 kiyohara #include <dev/ieee1394/firewire.h> 65 1.1 kiyohara #include <dev/ieee1394/firewirereg.h> 66 1.1 kiyohara #include <dev/ieee1394/fwdma.h> 67 1.1 kiyohara #include <dev/ieee1394/iec13213.h> 68 1.1 kiyohara #include <dev/ieee1394/sbp.h> 69 1.1 kiyohara 70 1.1 kiyohara #include "locators.h" 71 1.1 kiyohara 72 1.1 kiyohara 73 1.29 kiyohara #define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \ 74 1.29 kiyohara && crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2)) 75 1.29 kiyohara 76 1.29 kiyohara #define SBP_NUM_TARGETS 8 /* MAX 64 */ 77 1.29 kiyohara #define SBP_NUM_LUNS 64 78 1.29 kiyohara #define SBP_MAXPHYS MIN(MAXPHYS, (512*1024) /* 512KB */) 79 1.29 kiyohara #define SBP_DMA_SIZE PAGE_SIZE 80 1.29 kiyohara #define SBP_LOGIN_SIZE sizeof(struct sbp_login_res) 81 1.1 kiyohara #define SBP_QUEUE_LEN ((SBP_DMA_SIZE - SBP_LOGIN_SIZE) / sizeof(struct sbp_ocb)) 82 1.29 kiyohara #define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS) 83 1.1 kiyohara 84 1.29 kiyohara /* 85 1.1 kiyohara * STATUS FIFO addressing 86 1.1 kiyohara * bit 87 1.1 kiyohara * ----------------------- 88 1.1 kiyohara * 0- 1( 2): 0 (alignment) 89 1.1 kiyohara * 2- 9( 8): lun 90 1.1 kiyohara * 10-31(14): unit 91 1.29 kiyohara * 32-47(16): SBP_BIND_HI 92 1.29 kiyohara * 48-64(16): bus_id, node_id 93 1.1 kiyohara */ 94 1.1 kiyohara #define SBP_BIND_HI 0x1 95 1.30 kiyohara #define SBP_DEV2ADDR(u, l) \ 96 1.30 kiyohara (((uint64_t)SBP_BIND_HI << 32) |\ 97 1.30 kiyohara (((u) & 0x3fff) << 10) |\ 98 1.30 kiyohara (((l) & 0xff) << 2)) 99 1.1 kiyohara #define SBP_ADDR2UNIT(a) (((a) >> 10) & 0x3fff) 100 1.1 kiyohara #define SBP_ADDR2LUN(a) (((a) >> 2) & 0xff) 101 1.1 kiyohara #define SBP_INITIATOR 7 102 1.1 kiyohara 103 1.1 kiyohara static const char *orb_fun_name[] = { 104 1.1 kiyohara ORB_FUN_NAMES 105 1.1 kiyohara }; 106 1.1 kiyohara 107 1.1 kiyohara static int debug = 0; 108 1.1 kiyohara static int auto_login = 1; 109 1.1 kiyohara static int max_speed = -1; 110 1.1 kiyohara static int sbp_cold = 1; 111 1.1 kiyohara static int ex_login = 1; 112 1.1 kiyohara static int login_delay = 1000; /* msec */ 113 1.1 kiyohara static int scan_delay = 500; /* msec */ 114 1.1 kiyohara static int use_doorbell = 0; 115 1.1 kiyohara static int sbp_tags = 0; 116 1.1 kiyohara 117 1.1 kiyohara static int sysctl_sbp_verify(SYSCTLFN_PROTO, int lower, int upper); 118 1.1 kiyohara static int sysctl_sbp_verify_max_speed(SYSCTLFN_PROTO); 119 1.1 kiyohara static int sysctl_sbp_verify_tags(SYSCTLFN_PROTO); 120 1.1 kiyohara 121 1.1 kiyohara /* 122 1.1 kiyohara * Setup sysctl(3) MIB, hw.sbp.* 123 1.1 kiyohara * 124 1.22 ad * TBD condition CTLFLAG_PERMANENT on being a module or not 125 1.1 kiyohara */ 126 1.1 kiyohara SYSCTL_SETUP(sysctl_sbp, "sysctl sbp(4) subtree setup") 127 1.1 kiyohara { 128 1.1 kiyohara int rc, sbp_node_num; 129 1.1 kiyohara const struct sysctlnode *node; 130 1.1 kiyohara 131 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node, 132 1.1 kiyohara CTLFLAG_PERMANENT, CTLTYPE_NODE, "sbp", 133 1.1 kiyohara SYSCTL_DESCR("sbp controls"), NULL, 0, NULL, 134 1.29 kiyohara 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) 135 1.1 kiyohara goto err; 136 1.1 kiyohara sbp_node_num = node->sysctl_num; 137 1.1 kiyohara 138 1.1 kiyohara /* sbp auto login flag */ 139 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node, 140 1.1 kiyohara CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, 141 1.1 kiyohara "auto_login", SYSCTL_DESCR("SBP perform login automatically"), 142 1.1 kiyohara NULL, 0, &auto_login, 143 1.29 kiyohara 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0) 144 1.1 kiyohara goto err; 145 1.1 kiyohara 146 1.1 kiyohara /* sbp max speed */ 147 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node, 148 1.1 kiyohara CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, 149 1.1 kiyohara "max_speed", SYSCTL_DESCR("SBP transfer max speed"), 150 1.1 kiyohara sysctl_sbp_verify_max_speed, 0, &max_speed, 151 1.29 kiyohara 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0) 152 1.1 kiyohara goto err; 153 1.1 kiyohara 154 1.1 kiyohara /* sbp exclusive login flag */ 155 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node, 156 1.1 kiyohara CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, 157 1.1 kiyohara "exclusive_login", SYSCTL_DESCR("SBP enable exclusive login"), 158 1.1 kiyohara NULL, 0, &ex_login, 159 1.29 kiyohara 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0) 160 1.1 kiyohara goto err; 161 1.1 kiyohara 162 1.1 kiyohara /* sbp login delay */ 163 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node, 164 1.1 kiyohara CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, 165 1.1 kiyohara "login_delay", SYSCTL_DESCR("SBP login delay in msec"), 166 1.1 kiyohara NULL, 0, &login_delay, 167 1.29 kiyohara 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0) 168 1.1 kiyohara goto err; 169 1.1 kiyohara 170 1.1 kiyohara /* sbp scan delay */ 171 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node, 172 1.1 kiyohara CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, 173 1.1 kiyohara "scan_delay", SYSCTL_DESCR("SBP scan delay in msec"), 174 1.1 kiyohara NULL, 0, &scan_delay, 175 1.29 kiyohara 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0) 176 1.1 kiyohara goto err; 177 1.1 kiyohara 178 1.1 kiyohara /* sbp use doorbell flag */ 179 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node, 180 1.1 kiyohara CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, 181 1.1 kiyohara "use_doorbell", SYSCTL_DESCR("SBP use doorbell request"), 182 1.1 kiyohara NULL, 0, &use_doorbell, 183 1.29 kiyohara 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0) 184 1.1 kiyohara goto err; 185 1.1 kiyohara 186 1.1 kiyohara /* sbp force tagged queuing */ 187 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node, 188 1.1 kiyohara CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, 189 1.1 kiyohara "tags", SYSCTL_DESCR("SBP tagged queuing support"), 190 1.1 kiyohara sysctl_sbp_verify_tags, 0, &sbp_tags, 191 1.29 kiyohara 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0) 192 1.1 kiyohara goto err; 193 1.1 kiyohara 194 1.1 kiyohara /* sbp driver debug flag */ 195 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node, 196 1.1 kiyohara CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, 197 1.1 kiyohara "sbp_debug", SYSCTL_DESCR("SBP debug flag"), 198 1.1 kiyohara NULL, 0, &debug, 199 1.29 kiyohara 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0) 200 1.1 kiyohara goto err; 201 1.1 kiyohara 202 1.1 kiyohara return; 203 1.1 kiyohara 204 1.1 kiyohara err: 205 1.29 kiyohara aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc); 206 1.1 kiyohara } 207 1.1 kiyohara 208 1.1 kiyohara static int 209 1.1 kiyohara sysctl_sbp_verify(SYSCTLFN_ARGS, int lower, int upper) 210 1.1 kiyohara { 211 1.1 kiyohara int error, t; 212 1.1 kiyohara struct sysctlnode node; 213 1.1 kiyohara 214 1.1 kiyohara node = *rnode; 215 1.1 kiyohara t = *(int*)rnode->sysctl_data; 216 1.1 kiyohara node.sysctl_data = &t; 217 1.1 kiyohara error = sysctl_lookup(SYSCTLFN_CALL(&node)); 218 1.1 kiyohara if (error || newp == NULL) 219 1.29 kiyohara return error; 220 1.1 kiyohara 221 1.1 kiyohara if (t < lower || t > upper) 222 1.29 kiyohara return EINVAL; 223 1.1 kiyohara 224 1.1 kiyohara *(int*)rnode->sysctl_data = t; 225 1.1 kiyohara 226 1.29 kiyohara return 0; 227 1.1 kiyohara } 228 1.1 kiyohara 229 1.1 kiyohara static int 230 1.1 kiyohara sysctl_sbp_verify_max_speed(SYSCTLFN_ARGS) 231 1.1 kiyohara { 232 1.29 kiyohara 233 1.29 kiyohara return sysctl_sbp_verify(SYSCTLFN_CALL(rnode), 0, FWSPD_S400); 234 1.1 kiyohara } 235 1.1 kiyohara 236 1.1 kiyohara static int 237 1.1 kiyohara sysctl_sbp_verify_tags(SYSCTLFN_ARGS) 238 1.1 kiyohara { 239 1.29 kiyohara 240 1.29 kiyohara return sysctl_sbp_verify(SYSCTLFN_CALL(rnode), -1, 1); 241 1.1 kiyohara } 242 1.1 kiyohara 243 1.1 kiyohara #define NEED_RESPONSE 0 244 1.1 kiyohara 245 1.1 kiyohara #define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE) 246 1.1 kiyohara #ifdef __sparc64__ /* iommu */ 247 1.16 kiyohara #define SBP_IND_MAX howmany(SBP_MAXPHYS, SBP_SEG_MAX) 248 1.1 kiyohara #else 249 1.16 kiyohara #define SBP_IND_MAX howmany(SBP_MAXPHYS, PAGE_SIZE) 250 1.1 kiyohara #endif 251 1.1 kiyohara struct sbp_ocb { 252 1.1 kiyohara uint32_t orb[8]; 253 1.29 kiyohara #define IND_PTR_OFFSET (sizeof(uint32_t) * 8) 254 1.29 kiyohara struct ind_ptr ind_ptr[SBP_IND_MAX]; 255 1.29 kiyohara struct scsipi_xfer *xs; 256 1.1 kiyohara struct sbp_dev *sdev; 257 1.29 kiyohara uint16_t index; 258 1.29 kiyohara uint16_t flags; /* XXX should be removed */ 259 1.1 kiyohara bus_dmamap_t dmamap; 260 1.29 kiyohara bus_addr_t bus_addr; 261 1.29 kiyohara STAILQ_ENTRY(sbp_ocb) ocb; 262 1.1 kiyohara }; 263 1.1 kiyohara 264 1.29 kiyohara #define SBP_ORB_DMA_SYNC(dma, i, op) \ 265 1.29 kiyohara bus_dmamap_sync((dma).dma_tag, (dma).dma_map, \ 266 1.29 kiyohara sizeof(struct sbp_ocb) * (i), \ 267 1.29 kiyohara sizeof(ocb->orb) + sizeof(ocb->ind_ptr), (op)); 268 1.29 kiyohara 269 1.1 kiyohara #define OCB_ACT_MGM 0 270 1.1 kiyohara #define OCB_ACT_CMD 1 271 1.1 kiyohara #define OCB_MATCH(o,s) ((o)->bus_addr == ntohl((s)->orb_lo)) 272 1.1 kiyohara 273 1.1 kiyohara struct sbp_dev{ 274 1.1 kiyohara #define SBP_DEV_RESET 0 /* accept login */ 275 1.1 kiyohara #define SBP_DEV_LOGIN 1 /* to login */ 276 1.1 kiyohara #if 0 277 1.1 kiyohara #define SBP_DEV_RECONN 2 /* to reconnect */ 278 1.1 kiyohara #endif 279 1.1 kiyohara #define SBP_DEV_TOATTACH 3 /* to attach */ 280 1.1 kiyohara #define SBP_DEV_PROBE 4 /* scan lun */ 281 1.1 kiyohara #define SBP_DEV_ATTACHED 5 /* in operation */ 282 1.1 kiyohara #define SBP_DEV_DEAD 6 /* unavailable unit */ 283 1.1 kiyohara #define SBP_DEV_RETRY 7 /* unavailable unit */ 284 1.1 kiyohara uint8_t status:4, 285 1.1 kiyohara timeout:4; 286 1.1 kiyohara uint8_t type; 287 1.1 kiyohara uint16_t lun_id; 288 1.1 kiyohara uint16_t freeze; 289 1.1 kiyohara #define ORB_LINK_DEAD (1 << 0) 290 1.1 kiyohara #define VALID_LUN (1 << 1) 291 1.1 kiyohara #define ORB_POINTER_ACTIVE (1 << 2) 292 1.1 kiyohara #define ORB_POINTER_NEED (1 << 3) 293 1.1 kiyohara #define ORB_DOORBELL_ACTIVE (1 << 4) 294 1.1 kiyohara #define ORB_DOORBELL_NEED (1 << 5) 295 1.1 kiyohara #define ORB_SHORTAGE (1 << 6) 296 1.1 kiyohara uint16_t flags; 297 1.1 kiyohara struct scsipi_periph *periph; 298 1.1 kiyohara struct sbp_target *target; 299 1.1 kiyohara struct fwdma_alloc dma; 300 1.1 kiyohara struct sbp_login_res *login; 301 1.1 kiyohara struct callout login_callout; 302 1.1 kiyohara struct sbp_ocb *ocb; 303 1.1 kiyohara STAILQ_HEAD(, sbp_ocb) ocbs; 304 1.1 kiyohara STAILQ_HEAD(, sbp_ocb) free_ocbs; 305 1.1 kiyohara struct sbp_ocb *last_ocb; 306 1.1 kiyohara char vendor[32]; 307 1.1 kiyohara char product[32]; 308 1.1 kiyohara char revision[10]; 309 1.29 kiyohara char bustgtlun[32]; 310 1.1 kiyohara }; 311 1.1 kiyohara 312 1.1 kiyohara struct sbp_target { 313 1.1 kiyohara int target_id; 314 1.1 kiyohara int num_lun; 315 1.1 kiyohara struct sbp_dev **luns; 316 1.1 kiyohara struct sbp_softc *sbp; 317 1.1 kiyohara struct fw_device *fwdev; 318 1.1 kiyohara uint32_t mgm_hi, mgm_lo; 319 1.1 kiyohara struct sbp_ocb *mgm_ocb_cur; 320 1.1 kiyohara STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue; 321 1.1 kiyohara struct callout mgm_ocb_timeout; 322 1.1 kiyohara STAILQ_HEAD(, fw_xfer) xferlist; 323 1.1 kiyohara int n_xfer; 324 1.1 kiyohara }; 325 1.1 kiyohara 326 1.1 kiyohara struct sbp_softc { 327 1.29 kiyohara struct firewire_dev_comm sc_fd; 328 1.29 kiyohara struct scsipi_adapter sc_adapter; 329 1.1 kiyohara struct scsipi_channel sc_channel; 330 1.21 kiyohara device_t sc_bus; 331 1.29 kiyohara struct lwp *sc_lwp; 332 1.29 kiyohara struct sbp_target sc_target; 333 1.29 kiyohara struct fw_bind sc_fwb; 334 1.29 kiyohara bus_dma_tag_t sc_dmat; 335 1.29 kiyohara struct timeval sc_last_busreset; 336 1.29 kiyohara int sc_flags; 337 1.29 kiyohara kmutex_t sc_mtx; 338 1.29 kiyohara kcondvar_t sc_cv; 339 1.1 kiyohara }; 340 1.1 kiyohara 341 1.29 kiyohara MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/IEEE1394"); 342 1.34 dsl MALLOC_DECLARE(M_SBP); 343 1.29 kiyohara 344 1.29 kiyohara 345 1.29 kiyohara static int sbpmatch(device_t, cfdata_t, void *); 346 1.29 kiyohara static void sbpattach(device_t, device_t, void *); 347 1.29 kiyohara static int sbpdetach(device_t, int); 348 1.29 kiyohara 349 1.29 kiyohara static void sbp_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t, 350 1.29 kiyohara void *); 351 1.29 kiyohara static void sbp_minphys(struct buf *); 352 1.29 kiyohara 353 1.29 kiyohara static void sbp_show_sdev_info(struct sbp_dev *); 354 1.29 kiyohara static void sbp_alloc_lun(struct sbp_target *); 355 1.29 kiyohara static struct sbp_target *sbp_alloc_target(struct sbp_softc *, 356 1.29 kiyohara struct fw_device *); 357 1.29 kiyohara static void sbp_probe_lun(struct sbp_dev *); 358 1.29 kiyohara static void sbp_login_callout(void *); 359 1.29 kiyohara static void sbp_login(struct sbp_dev *); 360 1.29 kiyohara static void sbp_probe_target(void *); 361 1.29 kiyohara static void sbp_post_busreset(void *); 362 1.29 kiyohara static void sbp_post_explore(void *); 363 1.29 kiyohara #if NEED_RESPONSE 364 1.29 kiyohara static void sbp_loginres_callback(struct fw_xfer *); 365 1.1 kiyohara #endif 366 1.29 kiyohara static inline void sbp_xfer_free(struct fw_xfer *); 367 1.29 kiyohara static void sbp_reset_start_callback(struct fw_xfer *); 368 1.29 kiyohara static void sbp_reset_start(struct sbp_dev *); 369 1.29 kiyohara static void sbp_mgm_callback(struct fw_xfer *); 370 1.29 kiyohara static void sbp_scsipi_scan_target(void *); 371 1.29 kiyohara static inline void sbp_scan_dev(struct sbp_dev *); 372 1.29 kiyohara static void sbp_do_attach(struct fw_xfer *); 373 1.29 kiyohara static void sbp_agent_reset_callback(struct fw_xfer *); 374 1.29 kiyohara static void sbp_agent_reset(struct sbp_dev *); 375 1.29 kiyohara static void sbp_busy_timeout_callback(struct fw_xfer *); 376 1.29 kiyohara static void sbp_busy_timeout(struct sbp_dev *); 377 1.29 kiyohara static void sbp_orb_pointer_callback(struct fw_xfer *); 378 1.29 kiyohara static void sbp_orb_pointer(struct sbp_dev *, struct sbp_ocb *); 379 1.29 kiyohara static void sbp_doorbell_callback(struct fw_xfer *); 380 1.1 kiyohara static void sbp_doorbell(struct sbp_dev *); 381 1.29 kiyohara static struct fw_xfer *sbp_write_cmd(struct sbp_dev *, int, int); 382 1.29 kiyohara static void sbp_mgm_orb(struct sbp_dev *, int, struct sbp_ocb *); 383 1.29 kiyohara static void sbp_print_scsi_cmd(struct sbp_ocb *); 384 1.29 kiyohara static void sbp_scsi_status(struct sbp_status *, struct sbp_ocb *); 385 1.29 kiyohara static void sbp_fix_inq_data(struct sbp_ocb *); 386 1.29 kiyohara static void sbp_recv(struct fw_xfer *); 387 1.29 kiyohara static int sbp_logout_all(struct sbp_softc *); 388 1.1 kiyohara static void sbp_free_sdev(struct sbp_dev *); 389 1.29 kiyohara static void sbp_free_target(struct sbp_target *); 390 1.29 kiyohara static void sbp_scsipi_detach_sdev(struct sbp_dev *); 391 1.29 kiyohara static void sbp_scsipi_detach_target(struct sbp_target *); 392 1.29 kiyohara static void sbp_target_reset(struct sbp_dev *, int); 393 1.29 kiyohara static void sbp_mgm_timeout(void *); 394 1.29 kiyohara static void sbp_timeout(void *); 395 1.29 kiyohara static void sbp_action1(struct sbp_softc *, struct scsipi_xfer *); 396 1.29 kiyohara static void sbp_execute_ocb(struct sbp_ocb *, bus_dma_segment_t *, int); 397 1.29 kiyohara static struct sbp_ocb *sbp_dequeue_ocb(struct sbp_dev *, struct sbp_status *); 398 1.29 kiyohara static struct sbp_ocb *sbp_enqueue_ocb(struct sbp_dev *, struct sbp_ocb *); 399 1.29 kiyohara static struct sbp_ocb *sbp_get_ocb(struct sbp_dev *); 400 1.29 kiyohara static void sbp_free_ocb(struct sbp_dev *, struct sbp_ocb *); 401 1.29 kiyohara static void sbp_abort_ocb(struct sbp_ocb *, int); 402 1.29 kiyohara static void sbp_abort_all_ocbs(struct sbp_dev *, int); 403 1.1 kiyohara 404 1.1 kiyohara 405 1.1 kiyohara static const char *orb_status0[] = { 406 1.1 kiyohara /* 0 */ "No additional information to report", 407 1.1 kiyohara /* 1 */ "Request type not supported", 408 1.1 kiyohara /* 2 */ "Speed not supported", 409 1.1 kiyohara /* 3 */ "Page size not supported", 410 1.1 kiyohara /* 4 */ "Access denied", 411 1.1 kiyohara /* 5 */ "Logical unit not supported", 412 1.1 kiyohara /* 6 */ "Maximum payload too small", 413 1.1 kiyohara /* 7 */ "Reserved for future standardization", 414 1.1 kiyohara /* 8 */ "Resources unavailable", 415 1.1 kiyohara /* 9 */ "Function rejected", 416 1.1 kiyohara /* A */ "Login ID not recognized", 417 1.1 kiyohara /* B */ "Dummy ORB completed", 418 1.1 kiyohara /* C */ "Request aborted", 419 1.1 kiyohara /* FF */ "Unspecified error" 420 1.1 kiyohara #define MAX_ORB_STATUS0 0xd 421 1.1 kiyohara }; 422 1.1 kiyohara 423 1.1 kiyohara static const char *orb_status1_object[] = { 424 1.1 kiyohara /* 0 */ "Operation request block (ORB)", 425 1.1 kiyohara /* 1 */ "Data buffer", 426 1.1 kiyohara /* 2 */ "Page table", 427 1.1 kiyohara /* 3 */ "Unable to specify" 428 1.1 kiyohara }; 429 1.1 kiyohara 430 1.1 kiyohara static const char *orb_status1_serial_bus_error[] = { 431 1.1 kiyohara /* 0 */ "Missing acknowledge", 432 1.1 kiyohara /* 1 */ "Reserved; not to be used", 433 1.1 kiyohara /* 2 */ "Time-out error", 434 1.1 kiyohara /* 3 */ "Reserved; not to be used", 435 1.1 kiyohara /* 4 */ "Busy retry limit exceeded(X)", 436 1.1 kiyohara /* 5 */ "Busy retry limit exceeded(A)", 437 1.1 kiyohara /* 6 */ "Busy retry limit exceeded(B)", 438 1.1 kiyohara /* 7 */ "Reserved for future standardization", 439 1.1 kiyohara /* 8 */ "Reserved for future standardization", 440 1.1 kiyohara /* 9 */ "Reserved for future standardization", 441 1.1 kiyohara /* A */ "Reserved for future standardization", 442 1.1 kiyohara /* B */ "Tardy retry limit exceeded", 443 1.1 kiyohara /* C */ "Conflict error", 444 1.1 kiyohara /* D */ "Data error", 445 1.1 kiyohara /* E */ "Type error", 446 1.1 kiyohara /* F */ "Address error" 447 1.1 kiyohara }; 448 1.1 kiyohara 449 1.29 kiyohara 450 1.29 kiyohara CFATTACH_DECL_NEW(sbp, sizeof(struct sbp_softc), 451 1.29 kiyohara sbpmatch, sbpattach, sbpdetach, NULL); 452 1.29 kiyohara 453 1.29 kiyohara 454 1.29 kiyohara int 455 1.29 kiyohara sbpmatch(device_t parent, cfdata_t cf, void *aux) 456 1.29 kiyohara { 457 1.29 kiyohara struct fw_attach_args *fwa = aux; 458 1.29 kiyohara 459 1.29 kiyohara if (strcmp(fwa->name, "sbp") == 0) 460 1.29 kiyohara return 1; 461 1.29 kiyohara return 0; 462 1.29 kiyohara } 463 1.29 kiyohara 464 1.1 kiyohara static void 465 1.29 kiyohara sbpattach(device_t parent, device_t self, void *aux) 466 1.1 kiyohara { 467 1.29 kiyohara struct sbp_softc *sc = device_private(self); 468 1.29 kiyohara struct fw_attach_args *fwa = (struct fw_attach_args *)aux; 469 1.29 kiyohara struct firewire_comm *fc; 470 1.29 kiyohara struct scsipi_adapter *sc_adapter = &sc->sc_adapter; 471 1.29 kiyohara struct scsipi_channel *sc_channel = &sc->sc_channel; 472 1.29 kiyohara struct sbp_target *target = &sc->sc_target; 473 1.29 kiyohara int dv_unit; 474 1.29 kiyohara 475 1.29 kiyohara aprint_naive("\n"); 476 1.29 kiyohara aprint_normal(": SBP-2/SCSI over IEEE1394\n"); 477 1.29 kiyohara 478 1.29 kiyohara sc->sc_fd.dev = self; 479 1.29 kiyohara 480 1.29 kiyohara if (cold) 481 1.29 kiyohara sbp_cold++; 482 1.29 kiyohara sc->sc_fd.fc = fc = fwa->fc; 483 1.29 kiyohara mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_VM); 484 1.29 kiyohara cv_init(&sc->sc_cv, "sbp"); 485 1.29 kiyohara 486 1.29 kiyohara if (max_speed < 0) 487 1.29 kiyohara max_speed = fc->speed; 488 1.29 kiyohara 489 1.29 kiyohara sc->sc_dmat = fc->dmat; 490 1.29 kiyohara 491 1.29 kiyohara sc->sc_target.fwdev = NULL; 492 1.29 kiyohara sc->sc_target.luns = NULL; 493 1.29 kiyohara 494 1.33 cegger /* Initialize mutexes and lists before we can error out 495 1.33 cegger * to prevent crashes on detach 496 1.33 cegger */ 497 1.33 cegger mutex_init(&sc->sc_fwb.fwb_mtx, MUTEX_DEFAULT, IPL_VM); 498 1.33 cegger STAILQ_INIT(&sc->sc_fwb.xferlist); 499 1.33 cegger 500 1.29 kiyohara if (sbp_alloc_target(sc, fwa->fwdev) == NULL) 501 1.29 kiyohara return; 502 1.29 kiyohara 503 1.29 kiyohara sc_adapter->adapt_dev = sc->sc_fd.dev; 504 1.29 kiyohara sc_adapter->adapt_nchannels = 1; 505 1.29 kiyohara sc_adapter->adapt_max_periph = 1; 506 1.29 kiyohara sc_adapter->adapt_request = sbp_scsipi_request; 507 1.29 kiyohara sc_adapter->adapt_minphys = sbp_minphys; 508 1.29 kiyohara sc_adapter->adapt_openings = 8; 509 1.29 kiyohara 510 1.29 kiyohara sc_channel->chan_adapter = sc_adapter; 511 1.29 kiyohara sc_channel->chan_bustype = &scsi_bustype; 512 1.29 kiyohara sc_channel->chan_defquirks = PQUIRK_ONLYBIG; 513 1.29 kiyohara sc_channel->chan_channel = 0; 514 1.29 kiyohara sc_channel->chan_flags = SCSIPI_CHAN_CANGROW | SCSIPI_CHAN_NOSETTLE; 515 1.29 kiyohara 516 1.29 kiyohara sc_channel->chan_ntargets = 1; 517 1.29 kiyohara sc_channel->chan_nluns = target->num_lun; /* We set nluns 0 now */ 518 1.29 kiyohara sc_channel->chan_id = 1; 519 1.29 kiyohara 520 1.40 thorpej sc->sc_bus = config_found(sc->sc_fd.dev, sc_channel, scsiprint, 521 1.41 thorpej CFARGS_NONE); 522 1.29 kiyohara if (sc->sc_bus == NULL) { 523 1.29 kiyohara aprint_error_dev(self, "attach failed\n"); 524 1.29 kiyohara return; 525 1.29 kiyohara } 526 1.29 kiyohara 527 1.29 kiyohara /* We reserve 16 bit space (4 bytes X 64 unit X 256 luns) */ 528 1.29 kiyohara dv_unit = device_unit(sc->sc_fd.dev); 529 1.29 kiyohara sc->sc_fwb.start = SBP_DEV2ADDR(dv_unit, 0); 530 1.29 kiyohara sc->sc_fwb.end = SBP_DEV2ADDR(dv_unit, -1); 531 1.29 kiyohara /* pre-allocate xfer */ 532 1.29 kiyohara fw_xferlist_add(&sc->sc_fwb.xferlist, M_SBP, 533 1.29 kiyohara /*send*/ 0, /*recv*/ SBP_RECV_LEN, SBP_NUM_OCB / 2, 534 1.29 kiyohara fc, (void *)sc, sbp_recv); 535 1.29 kiyohara fw_bindadd(fc, &sc->sc_fwb); 536 1.29 kiyohara 537 1.29 kiyohara sc->sc_fd.post_busreset = sbp_post_busreset; 538 1.29 kiyohara sc->sc_fd.post_explore = sbp_post_explore; 539 1.1 kiyohara 540 1.29 kiyohara if (fc->status != FWBUSNOTREADY) { 541 1.29 kiyohara sbp_post_busreset((void *)sc); 542 1.29 kiyohara sbp_post_explore((void *)sc); 543 1.29 kiyohara } 544 1.1 kiyohara } 545 1.1 kiyohara 546 1.1 kiyohara static int 547 1.29 kiyohara sbpdetach(device_t self, int flags) 548 1.1 kiyohara { 549 1.29 kiyohara struct sbp_softc *sc = device_private(self); 550 1.29 kiyohara struct firewire_comm *fc = sc->sc_fd.fc; 551 1.29 kiyohara 552 1.29 kiyohara sbp_scsipi_detach_target(&sc->sc_target); 553 1.1 kiyohara 554 1.33 cegger if (sc->sc_target.fwdev && SBP_FWDEV_ALIVE(sc->sc_target.fwdev)) { 555 1.29 kiyohara sbp_logout_all(sc); 556 1.1 kiyohara 557 1.29 kiyohara /* XXX wait for logout completion */ 558 1.29 kiyohara mutex_enter(&sc->sc_mtx); 559 1.29 kiyohara cv_timedwait_sig(&sc->sc_cv, &sc->sc_mtx, hz/2); 560 1.29 kiyohara mutex_exit(&sc->sc_mtx); 561 1.1 kiyohara } 562 1.1 kiyohara 563 1.29 kiyohara sbp_free_target(&sc->sc_target); 564 1.29 kiyohara 565 1.29 kiyohara fw_bindremove(fc, &sc->sc_fwb); 566 1.29 kiyohara fw_xferlist_remove(&sc->sc_fwb.xferlist); 567 1.29 kiyohara mutex_destroy(&sc->sc_fwb.fwb_mtx); 568 1.29 kiyohara 569 1.29 kiyohara mutex_destroy(&sc->sc_mtx); 570 1.33 cegger cv_destroy(&sc->sc_cv); 571 1.29 kiyohara 572 1.29 kiyohara return 0; 573 1.29 kiyohara } 574 1.29 kiyohara 575 1.29 kiyohara 576 1.29 kiyohara static void 577 1.29 kiyohara sbp_scsipi_request(struct scsipi_channel *channel, scsipi_adapter_req_t req, 578 1.29 kiyohara void *arg) 579 1.29 kiyohara { 580 1.29 kiyohara struct sbp_softc *sc = device_private(channel->chan_adapter->adapt_dev); 581 1.29 kiyohara struct scsipi_xfer *xs = arg; 582 1.29 kiyohara int i; 583 1.29 kiyohara 584 1.29 kiyohara SBP_DEBUG(1) 585 1.29 kiyohara printf("Called sbp_scsipi_request\n"); 586 1.29 kiyohara END_DEBUG 587 1.1 kiyohara 588 1.29 kiyohara switch (req) { 589 1.29 kiyohara case ADAPTER_REQ_RUN_XFER: 590 1.29 kiyohara SBP_DEBUG(1) 591 1.29 kiyohara printf("Got req_run_xfer\n"); 592 1.29 kiyohara printf("xs control: 0x%08x, timeout: %d\n", 593 1.29 kiyohara xs->xs_control, xs->timeout); 594 1.29 kiyohara printf("opcode: 0x%02x\n", (int)xs->cmd->opcode); 595 1.29 kiyohara for (i = 0; i < 15; i++) 596 1.29 kiyohara printf("0x%02x ",(int)xs->cmd->bytes[i]); 597 1.29 kiyohara printf("\n"); 598 1.29 kiyohara END_DEBUG 599 1.29 kiyohara if (xs->xs_control & XS_CTL_RESET) { 600 1.29 kiyohara SBP_DEBUG(1) 601 1.29 kiyohara printf("XS_CTL_RESET not support\n"); 602 1.29 kiyohara END_DEBUG 603 1.29 kiyohara break; 604 1.29 kiyohara } 605 1.29 kiyohara #define SBPSCSI_SBP2_MAX_CDB 12 606 1.29 kiyohara if (xs->cmdlen > SBPSCSI_SBP2_MAX_CDB) { 607 1.29 kiyohara SBP_DEBUG(0) 608 1.29 kiyohara printf( 609 1.29 kiyohara "sbp doesn't support cdb's larger than %d bytes\n", 610 1.29 kiyohara SBPSCSI_SBP2_MAX_CDB); 611 1.29 kiyohara END_DEBUG 612 1.29 kiyohara xs->error = XS_DRIVER_STUFFUP; 613 1.29 kiyohara scsipi_done(xs); 614 1.29 kiyohara return; 615 1.29 kiyohara } 616 1.29 kiyohara sbp_action1(sc, xs); 617 1.1 kiyohara 618 1.29 kiyohara break; 619 1.29 kiyohara case ADAPTER_REQ_GROW_RESOURCES: 620 1.29 kiyohara SBP_DEBUG(1) 621 1.29 kiyohara printf("Got req_grow_resources\n"); 622 1.29 kiyohara END_DEBUG 623 1.29 kiyohara break; 624 1.29 kiyohara case ADAPTER_REQ_SET_XFER_MODE: 625 1.29 kiyohara SBP_DEBUG(1) 626 1.29 kiyohara printf("Got set xfer mode\n"); 627 1.29 kiyohara END_DEBUG 628 1.29 kiyohara break; 629 1.29 kiyohara default: 630 1.29 kiyohara panic("Unknown request: %d\n", (int)req); 631 1.29 kiyohara } 632 1.1 kiyohara } 633 1.29 kiyohara 634 1.29 kiyohara static void 635 1.29 kiyohara sbp_minphys(struct buf *bp) 636 1.1 kiyohara { 637 1.1 kiyohara 638 1.29 kiyohara minphys(bp); 639 1.1 kiyohara } 640 1.1 kiyohara 641 1.29 kiyohara 642 1.29 kiyohara /* 643 1.29 kiyohara * Display device characteristics on the console 644 1.29 kiyohara */ 645 1.1 kiyohara static void 646 1.29 kiyohara sbp_show_sdev_info(struct sbp_dev *sdev) 647 1.1 kiyohara { 648 1.29 kiyohara struct fw_device *fwdev = sdev->target->fwdev; 649 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp; 650 1.1 kiyohara 651 1.29 kiyohara aprint_normal_dev(sc->sc_fd.dev, 652 1.29 kiyohara "ordered:%d type:%d EUI:%08x%08x node:%d speed:%d maxrec:%d\n", 653 1.29 kiyohara (sdev->type & 0x40) >> 6, 654 1.29 kiyohara (sdev->type & 0x1f), 655 1.29 kiyohara fwdev->eui.hi, 656 1.29 kiyohara fwdev->eui.lo, 657 1.29 kiyohara fwdev->dst, 658 1.29 kiyohara fwdev->speed, 659 1.29 kiyohara fwdev->maxrec); 660 1.29 kiyohara aprint_normal_dev(sc->sc_fd.dev, "%s '%s' '%s' '%s'\n", 661 1.29 kiyohara sdev->bustgtlun, sdev->vendor, sdev->product, sdev->revision); 662 1.1 kiyohara } 663 1.1 kiyohara 664 1.1 kiyohara static void 665 1.1 kiyohara sbp_alloc_lun(struct sbp_target *target) 666 1.1 kiyohara { 667 1.1 kiyohara struct crom_context cc; 668 1.1 kiyohara struct csrreg *reg; 669 1.1 kiyohara struct sbp_dev *sdev, **newluns; 670 1.29 kiyohara struct sbp_softc *sc; 671 1.1 kiyohara int maxlun, lun, i; 672 1.1 kiyohara 673 1.29 kiyohara sc = target->sbp; 674 1.1 kiyohara crom_init_context(&cc, target->fwdev->csrrom); 675 1.42 andvar /* XXX should parse appropriate unit directories only */ 676 1.1 kiyohara maxlun = -1; 677 1.1 kiyohara while (cc.depth >= 0) { 678 1.1 kiyohara reg = crom_search_key(&cc, CROM_LUN); 679 1.1 kiyohara if (reg == NULL) 680 1.1 kiyohara break; 681 1.1 kiyohara lun = reg->val & 0xffff; 682 1.1 kiyohara SBP_DEBUG(0) 683 1.1 kiyohara printf("target %d lun %d found\n", target->target_id, lun); 684 1.1 kiyohara END_DEBUG 685 1.1 kiyohara if (maxlun < lun) 686 1.1 kiyohara maxlun = lun; 687 1.1 kiyohara crom_next(&cc); 688 1.1 kiyohara } 689 1.1 kiyohara if (maxlun < 0) 690 1.29 kiyohara aprint_normal_dev(sc->sc_fd.dev, "%d: no LUN found\n", 691 1.1 kiyohara target->target_id); 692 1.1 kiyohara 693 1.29 kiyohara maxlun++; 694 1.1 kiyohara if (maxlun >= SBP_NUM_LUNS) 695 1.1 kiyohara maxlun = SBP_NUM_LUNS; 696 1.1 kiyohara 697 1.1 kiyohara /* Invalidiate stale devices */ 698 1.29 kiyohara for (lun = 0; lun < target->num_lun; lun++) { 699 1.1 kiyohara sdev = target->luns[lun]; 700 1.1 kiyohara if (sdev == NULL) 701 1.1 kiyohara continue; 702 1.1 kiyohara sdev->flags &= ~VALID_LUN; 703 1.1 kiyohara if (lun >= maxlun) { 704 1.1 kiyohara /* lost device */ 705 1.29 kiyohara sbp_scsipi_detach_sdev(sdev); 706 1.1 kiyohara sbp_free_sdev(sdev); 707 1.29 kiyohara target->luns[lun] = NULL; 708 1.1 kiyohara } 709 1.1 kiyohara } 710 1.1 kiyohara 711 1.1 kiyohara /* Reallocate */ 712 1.1 kiyohara if (maxlun != target->num_lun) { 713 1.32 christos newluns = (struct sbp_dev **) realloc(target->luns, 714 1.32 christos sizeof(struct sbp_dev *) * maxlun, 715 1.39 chs M_SBP, M_WAITOK | M_ZERO); 716 1.1 kiyohara 717 1.1 kiyohara /* 718 1.1 kiyohara * We must zero the extended region for the case 719 1.1 kiyohara * realloc() doesn't allocate new buffer. 720 1.1 kiyohara */ 721 1.32 christos if (maxlun > target->num_lun) { 722 1.32 christos const int sbp_dev_p_sz = sizeof(struct sbp_dev *); 723 1.32 christos 724 1.25 cegger memset(&newluns[target->num_lun], 0, 725 1.29 kiyohara sbp_dev_p_sz * (maxlun - target->num_lun)); 726 1.32 christos } 727 1.1 kiyohara 728 1.1 kiyohara target->luns = newluns; 729 1.1 kiyohara target->num_lun = maxlun; 730 1.1 kiyohara } 731 1.1 kiyohara 732 1.1 kiyohara crom_init_context(&cc, target->fwdev->csrrom); 733 1.1 kiyohara while (cc.depth >= 0) { 734 1.1 kiyohara int new = 0; 735 1.1 kiyohara 736 1.1 kiyohara reg = crom_search_key(&cc, CROM_LUN); 737 1.1 kiyohara if (reg == NULL) 738 1.1 kiyohara break; 739 1.1 kiyohara lun = reg->val & 0xffff; 740 1.1 kiyohara if (lun >= SBP_NUM_LUNS) { 741 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, "too large lun %d\n", 742 1.29 kiyohara lun); 743 1.1 kiyohara goto next; 744 1.1 kiyohara } 745 1.1 kiyohara 746 1.1 kiyohara sdev = target->luns[lun]; 747 1.1 kiyohara if (sdev == NULL) { 748 1.32 christos sdev = malloc(sizeof(struct sbp_dev), 749 1.39 chs M_SBP, M_WAITOK | M_ZERO); 750 1.1 kiyohara target->luns[lun] = sdev; 751 1.1 kiyohara sdev->lun_id = lun; 752 1.1 kiyohara sdev->target = target; 753 1.1 kiyohara STAILQ_INIT(&sdev->ocbs); 754 1.29 kiyohara callout_init(&sdev->login_callout, CALLOUT_MPSAFE); 755 1.29 kiyohara callout_setfunc(&sdev->login_callout, 756 1.29 kiyohara sbp_login_callout, sdev); 757 1.1 kiyohara sdev->status = SBP_DEV_RESET; 758 1.1 kiyohara new = 1; 759 1.29 kiyohara snprintf(sdev->bustgtlun, 32, "%s:%d:%d", 760 1.29 kiyohara device_xname(sc->sc_fd.dev), 761 1.29 kiyohara sdev->target->target_id, 762 1.29 kiyohara sdev->lun_id); 763 1.29 kiyohara if (!sc->sc_lwp) 764 1.29 kiyohara if (kthread_create( 765 1.29 kiyohara PRI_NONE, KTHREAD_MPSAFE, NULL, 766 1.29 kiyohara sbp_scsipi_scan_target, &sc->sc_target, 767 1.29 kiyohara &sc->sc_lwp, 768 1.29 kiyohara "sbp%d_attach", device_unit(sc->sc_fd.dev))) 769 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, 770 1.29 kiyohara "unable to create thread"); 771 1.1 kiyohara } 772 1.1 kiyohara sdev->flags |= VALID_LUN; 773 1.1 kiyohara sdev->type = (reg->val & 0xff0000) >> 16; 774 1.1 kiyohara 775 1.1 kiyohara if (new == 0) 776 1.1 kiyohara goto next; 777 1.1 kiyohara 778 1.29 kiyohara fwdma_alloc_setup(sc->sc_fd.dev, sc->sc_dmat, SBP_DMA_SIZE, 779 1.29 kiyohara &sdev->dma, sizeof(uint32_t), BUS_DMA_NOWAIT); 780 1.1 kiyohara if (sdev->dma.v_addr == NULL) { 781 1.32 christos free(sdev, M_SBP); 782 1.1 kiyohara target->luns[lun] = NULL; 783 1.1 kiyohara goto next; 784 1.1 kiyohara } 785 1.29 kiyohara sdev->ocb = (struct sbp_ocb *)sdev->dma.v_addr; 786 1.29 kiyohara sdev->login = (struct sbp_login_res *)&sdev->ocb[SBP_QUEUE_LEN]; 787 1.25 cegger memset((char *)sdev->ocb, 0, 788 1.29 kiyohara sizeof(struct sbp_ocb) * SBP_QUEUE_LEN); 789 1.1 kiyohara 790 1.1 kiyohara STAILQ_INIT(&sdev->free_ocbs); 791 1.1 kiyohara for (i = 0; i < SBP_QUEUE_LEN; i++) { 792 1.29 kiyohara struct sbp_ocb *ocb = &sdev->ocb[i]; 793 1.29 kiyohara 794 1.29 kiyohara ocb->index = i; 795 1.29 kiyohara ocb->bus_addr = 796 1.29 kiyohara sdev->dma.bus_addr + sizeof(struct sbp_ocb) * i; 797 1.29 kiyohara if (bus_dmamap_create(sc->sc_dmat, 0x100000, 798 1.29 kiyohara SBP_IND_MAX, SBP_SEG_MAX, 0, 0, &ocb->dmamap)) { 799 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, 800 1.29 kiyohara "cannot create dmamap %d\n", i); 801 1.1 kiyohara /* XXX */ 802 1.1 kiyohara goto next; 803 1.1 kiyohara } 804 1.29 kiyohara sbp_free_ocb(sdev, ocb); /* into free queue */ 805 1.1 kiyohara } 806 1.1 kiyohara next: 807 1.1 kiyohara crom_next(&cc); 808 1.1 kiyohara } 809 1.1 kiyohara 810 1.29 kiyohara for (lun = 0; lun < target->num_lun; lun++) { 811 1.1 kiyohara sdev = target->luns[lun]; 812 1.1 kiyohara if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) { 813 1.29 kiyohara sbp_scsipi_detach_sdev(sdev); 814 1.1 kiyohara sbp_free_sdev(sdev); 815 1.1 kiyohara target->luns[lun] = NULL; 816 1.1 kiyohara } 817 1.1 kiyohara } 818 1.1 kiyohara } 819 1.1 kiyohara 820 1.1 kiyohara static struct sbp_target * 821 1.29 kiyohara sbp_alloc_target(struct sbp_softc *sc, struct fw_device *fwdev) 822 1.1 kiyohara { 823 1.1 kiyohara struct sbp_target *target; 824 1.1 kiyohara struct crom_context cc; 825 1.1 kiyohara struct csrreg *reg; 826 1.1 kiyohara 827 1.1 kiyohara SBP_DEBUG(1) 828 1.1 kiyohara printf("sbp_alloc_target\n"); 829 1.1 kiyohara END_DEBUG 830 1.1 kiyohara /* new target */ 831 1.29 kiyohara target = &sc->sc_target; 832 1.29 kiyohara target->sbp = sc; 833 1.1 kiyohara target->fwdev = fwdev; 834 1.1 kiyohara target->target_id = 0; 835 1.33 cegger target->mgm_ocb_cur = NULL; 836 1.33 cegger SBP_DEBUG(1) 837 1.33 cegger printf("target: mgm_port: %x\n", target->mgm_lo); 838 1.33 cegger END_DEBUG 839 1.33 cegger STAILQ_INIT(&target->xferlist); 840 1.33 cegger target->n_xfer = 0; 841 1.33 cegger STAILQ_INIT(&target->mgm_ocb_queue); 842 1.33 cegger callout_init(&target->mgm_ocb_timeout, CALLOUT_MPSAFE); 843 1.33 cegger 844 1.33 cegger target->luns = NULL; 845 1.33 cegger target->num_lun = 0; 846 1.33 cegger 847 1.1 kiyohara /* XXX we may want to reload mgm port after each bus reset */ 848 1.1 kiyohara /* XXX there might be multiple management agents */ 849 1.1 kiyohara crom_init_context(&cc, target->fwdev->csrrom); 850 1.1 kiyohara reg = crom_search_key(&cc, CROM_MGM); 851 1.1 kiyohara if (reg == NULL || reg->val == 0) { 852 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, "NULL management address\n"); 853 1.1 kiyohara target->fwdev = NULL; 854 1.1 kiyohara return NULL; 855 1.1 kiyohara } 856 1.33 cegger 857 1.1 kiyohara target->mgm_hi = 0xffff; 858 1.1 kiyohara target->mgm_lo = 0xf0000000 | (reg->val << 2); 859 1.1 kiyohara 860 1.1 kiyohara return target; 861 1.1 kiyohara } 862 1.1 kiyohara 863 1.1 kiyohara static void 864 1.1 kiyohara sbp_probe_lun(struct sbp_dev *sdev) 865 1.1 kiyohara { 866 1.1 kiyohara struct fw_device *fwdev; 867 1.1 kiyohara struct crom_context c, *cc = &c; 868 1.1 kiyohara struct csrreg *reg; 869 1.1 kiyohara 870 1.25 cegger memset(sdev->vendor, 0, sizeof(sdev->vendor)); 871 1.25 cegger memset(sdev->product, 0, sizeof(sdev->product)); 872 1.1 kiyohara 873 1.1 kiyohara fwdev = sdev->target->fwdev; 874 1.1 kiyohara crom_init_context(cc, fwdev->csrrom); 875 1.1 kiyohara /* get vendor string */ 876 1.1 kiyohara crom_search_key(cc, CSRKEY_VENDOR); 877 1.1 kiyohara crom_next(cc); 878 1.1 kiyohara crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor)); 879 1.1 kiyohara /* skip to the unit directory for SBP-2 */ 880 1.1 kiyohara while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) { 881 1.1 kiyohara if (reg->val == CSRVAL_T10SBP2) 882 1.1 kiyohara break; 883 1.1 kiyohara crom_next(cc); 884 1.1 kiyohara } 885 1.1 kiyohara /* get firmware revision */ 886 1.1 kiyohara reg = crom_search_key(cc, CSRKEY_FIRM_VER); 887 1.1 kiyohara if (reg != NULL) 888 1.29 kiyohara snprintf(sdev->revision, sizeof(sdev->revision), "%06x", 889 1.29 kiyohara reg->val); 890 1.1 kiyohara /* get product string */ 891 1.1 kiyohara crom_search_key(cc, CSRKEY_MODEL); 892 1.1 kiyohara crom_next(cc); 893 1.1 kiyohara crom_parse_text(cc, sdev->product, sizeof(sdev->product)); 894 1.1 kiyohara } 895 1.1 kiyohara 896 1.1 kiyohara static void 897 1.1 kiyohara sbp_login_callout(void *arg) 898 1.1 kiyohara { 899 1.1 kiyohara struct sbp_dev *sdev = (struct sbp_dev *)arg; 900 1.29 kiyohara 901 1.1 kiyohara sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL); 902 1.1 kiyohara } 903 1.1 kiyohara 904 1.1 kiyohara static void 905 1.1 kiyohara sbp_login(struct sbp_dev *sdev) 906 1.1 kiyohara { 907 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp; 908 1.1 kiyohara struct timeval delta; 909 1.1 kiyohara struct timeval t; 910 1.1 kiyohara int ticks = 0; 911 1.1 kiyohara 912 1.1 kiyohara microtime(&delta); 913 1.29 kiyohara timersub(&delta, &sc->sc_last_busreset, &delta); 914 1.1 kiyohara t.tv_sec = login_delay / 1000; 915 1.1 kiyohara t.tv_usec = (login_delay % 1000) * 1000; 916 1.29 kiyohara timersub(&t, &delta, &t); 917 1.1 kiyohara if (t.tv_sec >= 0 && t.tv_usec > 0) 918 1.1 kiyohara ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000; 919 1.1 kiyohara SBP_DEBUG(0) 920 1.24 christos printf("%s: sec = %lld usec = %ld ticks = %d\n", __func__, 921 1.24 christos (long long)t.tv_sec, (long)t.tv_usec, ticks); 922 1.1 kiyohara END_DEBUG 923 1.29 kiyohara callout_schedule(&sdev->login_callout, ticks); 924 1.1 kiyohara } 925 1.1 kiyohara 926 1.1 kiyohara static void 927 1.1 kiyohara sbp_probe_target(void *arg) 928 1.1 kiyohara { 929 1.1 kiyohara struct sbp_target *target = (struct sbp_target *)arg; 930 1.1 kiyohara struct sbp_dev *sdev; 931 1.1 kiyohara int i; 932 1.1 kiyohara 933 1.1 kiyohara SBP_DEBUG(1) 934 1.30 kiyohara printf("%s %d\n", __func__, target->target_id); 935 1.1 kiyohara END_DEBUG 936 1.1 kiyohara 937 1.1 kiyohara sbp_alloc_lun(target); 938 1.1 kiyohara 939 1.1 kiyohara /* XXX untimeout mgm_ocb and dequeue */ 940 1.29 kiyohara for (i = 0; i < target->num_lun; i++) { 941 1.1 kiyohara sdev = target->luns[i]; 942 1.29 kiyohara if (sdev == NULL || sdev->status == SBP_DEV_DEAD) 943 1.1 kiyohara continue; 944 1.1 kiyohara 945 1.29 kiyohara if (sdev->periph != NULL) { 946 1.29 kiyohara scsipi_periph_freeze(sdev->periph, 1); 947 1.29 kiyohara sdev->freeze++; 948 1.29 kiyohara } 949 1.29 kiyohara sbp_probe_lun(sdev); 950 1.29 kiyohara sbp_show_sdev_info(sdev); 951 1.29 kiyohara 952 1.29 kiyohara sbp_abort_all_ocbs(sdev, XS_RESET); 953 1.29 kiyohara switch (sdev->status) { 954 1.29 kiyohara case SBP_DEV_RESET: 955 1.29 kiyohara /* new or revived target */ 956 1.29 kiyohara if (auto_login) 957 1.29 kiyohara sbp_login(sdev); 958 1.29 kiyohara break; 959 1.29 kiyohara case SBP_DEV_TOATTACH: 960 1.29 kiyohara case SBP_DEV_PROBE: 961 1.29 kiyohara case SBP_DEV_ATTACHED: 962 1.29 kiyohara case SBP_DEV_RETRY: 963 1.29 kiyohara default: 964 1.29 kiyohara sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL); 965 1.29 kiyohara break; 966 1.1 kiyohara } 967 1.1 kiyohara } 968 1.1 kiyohara } 969 1.1 kiyohara 970 1.1 kiyohara static void 971 1.1 kiyohara sbp_post_busreset(void *arg) 972 1.1 kiyohara { 973 1.29 kiyohara struct sbp_softc *sc = (struct sbp_softc *)arg; 974 1.29 kiyohara struct sbp_target *target = &sc->sc_target; 975 1.1 kiyohara struct fw_device *fwdev = target->fwdev; 976 1.1 kiyohara int alive; 977 1.1 kiyohara 978 1.1 kiyohara alive = SBP_FWDEV_ALIVE(fwdev); 979 1.1 kiyohara SBP_DEBUG(0) 980 1.1 kiyohara printf("sbp_post_busreset\n"); 981 1.1 kiyohara if (!alive) 982 1.1 kiyohara printf("not alive\n"); 983 1.1 kiyohara END_DEBUG 984 1.29 kiyohara microtime(&sc->sc_last_busreset); 985 1.10 kiyohara 986 1.1 kiyohara if (!alive) 987 1.1 kiyohara return; 988 1.1 kiyohara 989 1.29 kiyohara scsipi_channel_freeze(&sc->sc_channel, 1); 990 1.1 kiyohara } 991 1.1 kiyohara 992 1.1 kiyohara static void 993 1.1 kiyohara sbp_post_explore(void *arg) 994 1.1 kiyohara { 995 1.29 kiyohara struct sbp_softc *sc = (struct sbp_softc *)arg; 996 1.29 kiyohara struct sbp_target *target = &sc->sc_target; 997 1.1 kiyohara struct fw_device *fwdev = target->fwdev; 998 1.1 kiyohara int alive; 999 1.1 kiyohara 1000 1.1 kiyohara alive = SBP_FWDEV_ALIVE(fwdev); 1001 1.1 kiyohara SBP_DEBUG(0) 1002 1.1 kiyohara printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold); 1003 1.1 kiyohara if (!alive) 1004 1.1 kiyohara printf("not alive\n"); 1005 1.1 kiyohara END_DEBUG 1006 1.1 kiyohara if (!alive) 1007 1.1 kiyohara return; 1008 1.1 kiyohara 1009 1.29 kiyohara if (!firewire_phydma_enable) 1010 1.29 kiyohara return; 1011 1.29 kiyohara 1012 1.1 kiyohara if (sbp_cold > 0) 1013 1.29 kiyohara sbp_cold--; 1014 1.1 kiyohara 1015 1.1 kiyohara SBP_DEBUG(0) 1016 1.1 kiyohara printf("sbp_post_explore: EUI:%08x%08x ", fwdev->eui.hi, fwdev->eui.lo); 1017 1.1 kiyohara END_DEBUG 1018 1.1 kiyohara sbp_probe_target((void *)target); 1019 1.1 kiyohara if (target->num_lun == 0) 1020 1.1 kiyohara sbp_free_target(target); 1021 1.1 kiyohara 1022 1.29 kiyohara scsipi_channel_thaw(&sc->sc_channel, 1); 1023 1.1 kiyohara } 1024 1.1 kiyohara 1025 1.1 kiyohara #if NEED_RESPONSE 1026 1.1 kiyohara static void 1027 1.29 kiyohara sbp_loginres_callback(struct fw_xfer *xfer) 1028 1.29 kiyohara { 1029 1.29 kiyohara struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc; 1030 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp; 1031 1.29 kiyohara 1032 1.1 kiyohara SBP_DEBUG(1) 1033 1.1 kiyohara printf("sbp_loginres_callback\n"); 1034 1.1 kiyohara END_DEBUG 1035 1.1 kiyohara /* recycle */ 1036 1.29 kiyohara mutex_enter(&sc->sc_fwb.fwb_mtx); 1037 1.29 kiyohara STAILQ_INSERT_TAIL(&sc->sc_fwb.xferlist, xfer, link); 1038 1.29 kiyohara mutex_exit(&sc->sc_fwb.fwb_mtx); 1039 1.1 kiyohara return; 1040 1.1 kiyohara } 1041 1.1 kiyohara #endif 1042 1.1 kiyohara 1043 1.4 perry static inline void 1044 1.1 kiyohara sbp_xfer_free(struct fw_xfer *xfer) 1045 1.1 kiyohara { 1046 1.29 kiyohara struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc; 1047 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp; 1048 1.1 kiyohara 1049 1.1 kiyohara fw_xfer_unload(xfer); 1050 1.29 kiyohara mutex_enter(&sc->sc_mtx); 1051 1.1 kiyohara STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link); 1052 1.29 kiyohara mutex_exit(&sc->sc_mtx); 1053 1.1 kiyohara } 1054 1.1 kiyohara 1055 1.1 kiyohara static void 1056 1.1 kiyohara sbp_reset_start_callback(struct fw_xfer *xfer) 1057 1.1 kiyohara { 1058 1.1 kiyohara struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc; 1059 1.1 kiyohara struct sbp_target *target = sdev->target; 1060 1.1 kiyohara int i; 1061 1.1 kiyohara 1062 1.29 kiyohara if (xfer->resp != 0) 1063 1.29 kiyohara aprint_error("%s: sbp_reset_start failed: resp=%d\n", 1064 1.29 kiyohara sdev->bustgtlun, xfer->resp); 1065 1.1 kiyohara 1066 1.1 kiyohara for (i = 0; i < target->num_lun; i++) { 1067 1.1 kiyohara tsdev = target->luns[i]; 1068 1.1 kiyohara if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN) 1069 1.1 kiyohara sbp_login(tsdev); 1070 1.1 kiyohara } 1071 1.1 kiyohara } 1072 1.1 kiyohara 1073 1.1 kiyohara static void 1074 1.1 kiyohara sbp_reset_start(struct sbp_dev *sdev) 1075 1.1 kiyohara { 1076 1.1 kiyohara struct fw_xfer *xfer; 1077 1.1 kiyohara struct fw_pkt *fp; 1078 1.1 kiyohara 1079 1.1 kiyohara SBP_DEBUG(0) 1080 1.29 kiyohara printf("%s: sbp_reset_start: %s\n", 1081 1.29 kiyohara device_xname(sdev->target->sbp->sc_fd.dev), sdev->bustgtlun); 1082 1.1 kiyohara END_DEBUG 1083 1.1 kiyohara 1084 1.1 kiyohara xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 1085 1.29 kiyohara if (xfer == NULL) 1086 1.29 kiyohara return; 1087 1.1 kiyohara xfer->hand = sbp_reset_start_callback; 1088 1.1 kiyohara fp = &xfer->send.hdr; 1089 1.1 kiyohara fp->mode.wreqq.dest_hi = 0xffff; 1090 1.1 kiyohara fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START; 1091 1.1 kiyohara fp->mode.wreqq.data = htonl(0xf); 1092 1.29 kiyohara if (fw_asyreq(xfer->fc, -1, xfer) != 0) 1093 1.29 kiyohara sbp_xfer_free(xfer); 1094 1.1 kiyohara } 1095 1.1 kiyohara 1096 1.1 kiyohara static void 1097 1.1 kiyohara sbp_mgm_callback(struct fw_xfer *xfer) 1098 1.1 kiyohara { 1099 1.1 kiyohara struct sbp_dev *sdev; 1100 1.1 kiyohara 1101 1.1 kiyohara sdev = (struct sbp_dev *)xfer->sc; 1102 1.1 kiyohara 1103 1.1 kiyohara SBP_DEBUG(1) 1104 1.29 kiyohara printf("%s: sbp_mgm_callback: %s\n", 1105 1.29 kiyohara device_xname(sdev->target->sbp->sc_fd.dev), sdev->bustgtlun); 1106 1.1 kiyohara END_DEBUG 1107 1.1 kiyohara sbp_xfer_free(xfer); 1108 1.1 kiyohara return; 1109 1.1 kiyohara } 1110 1.1 kiyohara 1111 1.29 kiyohara static void 1112 1.29 kiyohara sbp_scsipi_scan_target(void *arg) 1113 1.1 kiyohara { 1114 1.29 kiyohara struct sbp_target *target = (struct sbp_target *)arg; 1115 1.29 kiyohara struct sbp_softc *sc = target->sbp; 1116 1.29 kiyohara struct sbp_dev *sdev; 1117 1.29 kiyohara struct scsipi_channel *chan = &sc->sc_channel; 1118 1.29 kiyohara struct scsibus_softc *sc_bus = device_private(sc->sc_bus); 1119 1.29 kiyohara int lun, yet; 1120 1.29 kiyohara 1121 1.29 kiyohara do { 1122 1.29 kiyohara mutex_enter(&sc->sc_mtx); 1123 1.29 kiyohara cv_wait_sig(&sc->sc_cv, &sc->sc_mtx); 1124 1.29 kiyohara mutex_exit(&sc->sc_mtx); 1125 1.29 kiyohara yet = 0; 1126 1.29 kiyohara 1127 1.29 kiyohara for (lun = 0; lun < target->num_lun; lun++) { 1128 1.29 kiyohara sdev = target->luns[lun]; 1129 1.29 kiyohara if (sdev == NULL) 1130 1.29 kiyohara continue; 1131 1.29 kiyohara if (sdev->status != SBP_DEV_PROBE) { 1132 1.29 kiyohara yet++; 1133 1.29 kiyohara continue; 1134 1.29 kiyohara } 1135 1.29 kiyohara 1136 1.29 kiyohara if (sdev->periph == NULL) { 1137 1.29 kiyohara if (chan->chan_nluns < target->num_lun) 1138 1.29 kiyohara chan->chan_nluns = target->num_lun; 1139 1.29 kiyohara 1140 1.29 kiyohara scsi_probe_bus(sc_bus, target->target_id, 1141 1.29 kiyohara sdev->lun_id); 1142 1.29 kiyohara sdev->periph = scsipi_lookup_periph(chan, 1143 1.29 kiyohara target->target_id, lun); 1144 1.29 kiyohara } 1145 1.29 kiyohara sdev->status = SBP_DEV_ATTACHED; 1146 1.29 kiyohara } 1147 1.29 kiyohara } while (yet > 0); 1148 1.29 kiyohara 1149 1.29 kiyohara sc->sc_lwp = NULL; 1150 1.29 kiyohara kthread_exit(0); 1151 1.1 kiyohara 1152 1.29 kiyohara /* NOTREACHED */ 1153 1.1 kiyohara } 1154 1.1 kiyohara 1155 1.29 kiyohara static inline void 1156 1.29 kiyohara sbp_scan_dev(struct sbp_dev *sdev) 1157 1.1 kiyohara { 1158 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp; 1159 1.29 kiyohara 1160 1.29 kiyohara sdev->status = SBP_DEV_PROBE; 1161 1.29 kiyohara mutex_enter(&sc->sc_mtx); 1162 1.29 kiyohara cv_signal(&sdev->target->sbp->sc_cv); 1163 1.29 kiyohara mutex_exit(&sc->sc_mtx); 1164 1.29 kiyohara } 1165 1.1 kiyohara 1166 1.1 kiyohara 1167 1.1 kiyohara static void 1168 1.1 kiyohara sbp_do_attach(struct fw_xfer *xfer) 1169 1.1 kiyohara { 1170 1.1 kiyohara struct sbp_dev *sdev; 1171 1.1 kiyohara struct sbp_target *target; 1172 1.29 kiyohara struct sbp_softc *sc; 1173 1.1 kiyohara 1174 1.1 kiyohara sdev = (struct sbp_dev *)xfer->sc; 1175 1.1 kiyohara target = sdev->target; 1176 1.29 kiyohara sc = target->sbp; 1177 1.1 kiyohara 1178 1.1 kiyohara SBP_DEBUG(0) 1179 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sc->sc_fd.dev), __func__, 1180 1.29 kiyohara sdev->bustgtlun); 1181 1.1 kiyohara END_DEBUG 1182 1.1 kiyohara sbp_xfer_free(xfer); 1183 1.1 kiyohara 1184 1.1 kiyohara sbp_scan_dev(sdev); 1185 1.1 kiyohara return; 1186 1.1 kiyohara } 1187 1.1 kiyohara 1188 1.1 kiyohara static void 1189 1.1 kiyohara sbp_agent_reset_callback(struct fw_xfer *xfer) 1190 1.1 kiyohara { 1191 1.29 kiyohara struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc; 1192 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp; 1193 1.1 kiyohara 1194 1.1 kiyohara SBP_DEBUG(1) 1195 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sc->sc_fd.dev), __func__, 1196 1.29 kiyohara sdev->bustgtlun); 1197 1.1 kiyohara END_DEBUG 1198 1.29 kiyohara if (xfer->resp != 0) 1199 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, "%s:%s: resp=%d\n", __func__, 1200 1.29 kiyohara sdev->bustgtlun, xfer->resp); 1201 1.1 kiyohara 1202 1.1 kiyohara sbp_xfer_free(xfer); 1203 1.29 kiyohara if (sdev->periph != NULL) { 1204 1.29 kiyohara scsipi_periph_thaw(sdev->periph, sdev->freeze); 1205 1.29 kiyohara scsipi_channel_thaw(&sc->sc_channel, 0); 1206 1.1 kiyohara sdev->freeze = 0; 1207 1.1 kiyohara } 1208 1.1 kiyohara } 1209 1.1 kiyohara 1210 1.1 kiyohara static void 1211 1.1 kiyohara sbp_agent_reset(struct sbp_dev *sdev) 1212 1.1 kiyohara { 1213 1.1 kiyohara struct fw_xfer *xfer; 1214 1.1 kiyohara struct fw_pkt *fp; 1215 1.1 kiyohara 1216 1.1 kiyohara SBP_DEBUG(0) 1217 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sdev->target->sbp->sc_fd.dev), 1218 1.29 kiyohara __func__, sdev->bustgtlun); 1219 1.1 kiyohara END_DEBUG 1220 1.1 kiyohara xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04); 1221 1.1 kiyohara if (xfer == NULL) 1222 1.1 kiyohara return; 1223 1.1 kiyohara if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE) 1224 1.1 kiyohara xfer->hand = sbp_agent_reset_callback; 1225 1.1 kiyohara else 1226 1.1 kiyohara xfer->hand = sbp_do_attach; 1227 1.1 kiyohara fp = &xfer->send.hdr; 1228 1.1 kiyohara fp->mode.wreqq.data = htonl(0xf); 1229 1.29 kiyohara if (fw_asyreq(xfer->fc, -1, xfer) != 0) 1230 1.29 kiyohara sbp_xfer_free(xfer); 1231 1.29 kiyohara sbp_abort_all_ocbs(sdev, XS_RESET); 1232 1.1 kiyohara } 1233 1.1 kiyohara 1234 1.1 kiyohara static void 1235 1.1 kiyohara sbp_busy_timeout_callback(struct fw_xfer *xfer) 1236 1.1 kiyohara { 1237 1.29 kiyohara struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc; 1238 1.1 kiyohara 1239 1.1 kiyohara SBP_DEBUG(1) 1240 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sdev->target->sbp->sc_fd.dev), 1241 1.29 kiyohara __func__, sdev->bustgtlun); 1242 1.1 kiyohara END_DEBUG 1243 1.1 kiyohara sbp_xfer_free(xfer); 1244 1.1 kiyohara sbp_agent_reset(sdev); 1245 1.1 kiyohara } 1246 1.1 kiyohara 1247 1.1 kiyohara static void 1248 1.1 kiyohara sbp_busy_timeout(struct sbp_dev *sdev) 1249 1.1 kiyohara { 1250 1.1 kiyohara struct fw_pkt *fp; 1251 1.1 kiyohara struct fw_xfer *xfer; 1252 1.29 kiyohara 1253 1.1 kiyohara SBP_DEBUG(0) 1254 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sdev->target->sbp->sc_fd.dev), 1255 1.29 kiyohara __func__, sdev->bustgtlun); 1256 1.1 kiyohara END_DEBUG 1257 1.1 kiyohara xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0); 1258 1.29 kiyohara if (xfer == NULL) 1259 1.29 kiyohara return; 1260 1.1 kiyohara xfer->hand = sbp_busy_timeout_callback; 1261 1.1 kiyohara fp = &xfer->send.hdr; 1262 1.1 kiyohara fp->mode.wreqq.dest_hi = 0xffff; 1263 1.1 kiyohara fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT; 1264 1.1 kiyohara fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf); 1265 1.29 kiyohara if (fw_asyreq(xfer->fc, -1, xfer) != 0) 1266 1.29 kiyohara sbp_xfer_free(xfer); 1267 1.1 kiyohara } 1268 1.1 kiyohara 1269 1.1 kiyohara static void 1270 1.1 kiyohara sbp_orb_pointer_callback(struct fw_xfer *xfer) 1271 1.1 kiyohara { 1272 1.29 kiyohara struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc; 1273 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp; 1274 1.1 kiyohara 1275 1.1 kiyohara SBP_DEBUG(1) 1276 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sc->sc_fd.dev), __func__, 1277 1.29 kiyohara sdev->bustgtlun); 1278 1.1 kiyohara END_DEBUG 1279 1.29 kiyohara if (xfer->resp != 0) 1280 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, "%s:%s: xfer->resp = %d\n", 1281 1.29 kiyohara __func__, sdev->bustgtlun, xfer->resp); 1282 1.1 kiyohara sbp_xfer_free(xfer); 1283 1.1 kiyohara sdev->flags &= ~ORB_POINTER_ACTIVE; 1284 1.1 kiyohara 1285 1.1 kiyohara if ((sdev->flags & ORB_POINTER_NEED) != 0) { 1286 1.1 kiyohara struct sbp_ocb *ocb; 1287 1.1 kiyohara 1288 1.1 kiyohara sdev->flags &= ~ORB_POINTER_NEED; 1289 1.1 kiyohara ocb = STAILQ_FIRST(&sdev->ocbs); 1290 1.1 kiyohara if (ocb != NULL) 1291 1.1 kiyohara sbp_orb_pointer(sdev, ocb); 1292 1.1 kiyohara } 1293 1.1 kiyohara return; 1294 1.1 kiyohara } 1295 1.1 kiyohara 1296 1.1 kiyohara static void 1297 1.1 kiyohara sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb) 1298 1.1 kiyohara { 1299 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp; 1300 1.1 kiyohara struct fw_xfer *xfer; 1301 1.1 kiyohara struct fw_pkt *fp; 1302 1.29 kiyohara 1303 1.1 kiyohara SBP_DEBUG(1) 1304 1.29 kiyohara printf("%s:%s:%s: 0x%08x\n", device_xname(sc->sc_fd.dev), __func__, 1305 1.29 kiyohara sdev->bustgtlun, (uint32_t)ocb->bus_addr); 1306 1.1 kiyohara END_DEBUG 1307 1.1 kiyohara 1308 1.1 kiyohara if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) { 1309 1.1 kiyohara SBP_DEBUG(0) 1310 1.1 kiyohara printf("%s: orb pointer active\n", __func__); 1311 1.1 kiyohara END_DEBUG 1312 1.1 kiyohara sdev->flags |= ORB_POINTER_NEED; 1313 1.1 kiyohara return; 1314 1.1 kiyohara } 1315 1.1 kiyohara 1316 1.1 kiyohara sdev->flags |= ORB_POINTER_ACTIVE; 1317 1.1 kiyohara xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08); 1318 1.1 kiyohara if (xfer == NULL) 1319 1.1 kiyohara return; 1320 1.1 kiyohara xfer->hand = sbp_orb_pointer_callback; 1321 1.1 kiyohara 1322 1.1 kiyohara fp = &xfer->send.hdr; 1323 1.1 kiyohara fp->mode.wreqb.len = 8; 1324 1.1 kiyohara fp->mode.wreqb.extcode = 0; 1325 1.29 kiyohara xfer->send.payload[0] = 1326 1.29 kiyohara htonl(((sc->sc_fd.fc->nodeid | FWLOCALBUS) << 16)); 1327 1.1 kiyohara xfer->send.payload[1] = htonl((uint32_t)ocb->bus_addr); 1328 1.1 kiyohara 1329 1.29 kiyohara if (fw_asyreq(xfer->fc, -1, xfer) != 0) { 1330 1.29 kiyohara sbp_xfer_free(xfer); 1331 1.29 kiyohara ocb->xs->error = XS_DRIVER_STUFFUP; 1332 1.29 kiyohara scsipi_done(ocb->xs); 1333 1.1 kiyohara } 1334 1.1 kiyohara } 1335 1.1 kiyohara 1336 1.1 kiyohara static void 1337 1.1 kiyohara sbp_doorbell_callback(struct fw_xfer *xfer) 1338 1.1 kiyohara { 1339 1.29 kiyohara struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc; 1340 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp; 1341 1.1 kiyohara 1342 1.1 kiyohara SBP_DEBUG(1) 1343 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sc->sc_fd.dev), __func__, 1344 1.29 kiyohara sdev->bustgtlun); 1345 1.1 kiyohara END_DEBUG 1346 1.1 kiyohara if (xfer->resp != 0) { 1347 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, "%s: xfer->resp = %d\n", 1348 1.29 kiyohara __func__, xfer->resp); 1349 1.1 kiyohara } 1350 1.1 kiyohara sbp_xfer_free(xfer); 1351 1.1 kiyohara sdev->flags &= ~ORB_DOORBELL_ACTIVE; 1352 1.1 kiyohara if ((sdev->flags & ORB_DOORBELL_NEED) != 0) { 1353 1.1 kiyohara sdev->flags &= ~ORB_DOORBELL_NEED; 1354 1.1 kiyohara sbp_doorbell(sdev); 1355 1.1 kiyohara } 1356 1.1 kiyohara return; 1357 1.1 kiyohara } 1358 1.1 kiyohara 1359 1.1 kiyohara static void 1360 1.1 kiyohara sbp_doorbell(struct sbp_dev *sdev) 1361 1.1 kiyohara { 1362 1.1 kiyohara struct fw_xfer *xfer; 1363 1.1 kiyohara struct fw_pkt *fp; 1364 1.29 kiyohara 1365 1.1 kiyohara SBP_DEBUG(1) 1366 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sdev->target->sbp->sc_fd.dev), 1367 1.29 kiyohara __func__, sdev->bustgtlun); 1368 1.1 kiyohara END_DEBUG 1369 1.1 kiyohara 1370 1.1 kiyohara if ((sdev->flags & ORB_DOORBELL_ACTIVE) != 0) { 1371 1.1 kiyohara sdev->flags |= ORB_DOORBELL_NEED; 1372 1.1 kiyohara return; 1373 1.1 kiyohara } 1374 1.1 kiyohara sdev->flags |= ORB_DOORBELL_ACTIVE; 1375 1.1 kiyohara xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10); 1376 1.1 kiyohara if (xfer == NULL) 1377 1.1 kiyohara return; 1378 1.1 kiyohara xfer->hand = sbp_doorbell_callback; 1379 1.1 kiyohara fp = &xfer->send.hdr; 1380 1.1 kiyohara fp->mode.wreqq.data = htonl(0xf); 1381 1.29 kiyohara if (fw_asyreq(xfer->fc, -1, xfer) != 0) 1382 1.29 kiyohara sbp_xfer_free(xfer); 1383 1.1 kiyohara } 1384 1.1 kiyohara 1385 1.1 kiyohara static struct fw_xfer * 1386 1.1 kiyohara sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset) 1387 1.1 kiyohara { 1388 1.29 kiyohara struct sbp_softc *sc; 1389 1.1 kiyohara struct fw_xfer *xfer; 1390 1.1 kiyohara struct fw_pkt *fp; 1391 1.1 kiyohara struct sbp_target *target; 1392 1.29 kiyohara int new = 0; 1393 1.1 kiyohara 1394 1.1 kiyohara target = sdev->target; 1395 1.29 kiyohara sc = target->sbp; 1396 1.29 kiyohara mutex_enter(&sc->sc_mtx); 1397 1.1 kiyohara xfer = STAILQ_FIRST(&target->xferlist); 1398 1.1 kiyohara if (xfer == NULL) { 1399 1.1 kiyohara if (target->n_xfer > 5 /* XXX */) { 1400 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, 1401 1.29 kiyohara "no more xfer for this target\n"); 1402 1.29 kiyohara mutex_exit(&sc->sc_mtx); 1403 1.29 kiyohara return NULL; 1404 1.1 kiyohara } 1405 1.1 kiyohara xfer = fw_xfer_alloc_buf(M_SBP, 8, 0); 1406 1.29 kiyohara if (xfer == NULL) { 1407 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, 1408 1.29 kiyohara "fw_xfer_alloc_buf failed\n"); 1409 1.29 kiyohara mutex_exit(&sc->sc_mtx); 1410 1.1 kiyohara return NULL; 1411 1.1 kiyohara } 1412 1.29 kiyohara target->n_xfer++; 1413 1.29 kiyohara SBP_DEBUG(0) 1414 1.1 kiyohara printf("sbp: alloc %d xfer\n", target->n_xfer); 1415 1.29 kiyohara END_DEBUG 1416 1.1 kiyohara new = 1; 1417 1.29 kiyohara } else 1418 1.1 kiyohara STAILQ_REMOVE_HEAD(&target->xferlist, link); 1419 1.29 kiyohara mutex_exit(&sc->sc_mtx); 1420 1.1 kiyohara 1421 1.1 kiyohara microtime(&xfer->tv); 1422 1.1 kiyohara 1423 1.1 kiyohara if (new) { 1424 1.1 kiyohara xfer->recv.pay_len = 0; 1425 1.37 riastrad xfer->send.spd = uimin(target->fwdev->speed, max_speed); 1426 1.29 kiyohara xfer->fc = target->sbp->sc_fd.fc; 1427 1.1 kiyohara } 1428 1.1 kiyohara 1429 1.1 kiyohara if (tcode == FWTCODE_WREQB) 1430 1.1 kiyohara xfer->send.pay_len = 8; 1431 1.1 kiyohara else 1432 1.1 kiyohara xfer->send.pay_len = 0; 1433 1.1 kiyohara 1434 1.15 christos xfer->sc = (void *)sdev; 1435 1.1 kiyohara fp = &xfer->send.hdr; 1436 1.1 kiyohara fp->mode.wreqq.dest_hi = sdev->login->cmd_hi; 1437 1.1 kiyohara fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset; 1438 1.1 kiyohara fp->mode.wreqq.tlrt = 0; 1439 1.1 kiyohara fp->mode.wreqq.tcode = tcode; 1440 1.1 kiyohara fp->mode.wreqq.pri = 0; 1441 1.29 kiyohara fp->mode.wreqq.dst = FWLOCALBUS | target->fwdev->dst; 1442 1.1 kiyohara 1443 1.1 kiyohara return xfer; 1444 1.1 kiyohara } 1445 1.1 kiyohara 1446 1.1 kiyohara static void 1447 1.1 kiyohara sbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb) 1448 1.1 kiyohara { 1449 1.1 kiyohara struct fw_xfer *xfer; 1450 1.1 kiyohara struct fw_pkt *fp; 1451 1.1 kiyohara struct sbp_ocb *ocb; 1452 1.1 kiyohara struct sbp_target *target; 1453 1.29 kiyohara int nid, dv_unit; 1454 1.1 kiyohara 1455 1.1 kiyohara target = sdev->target; 1456 1.29 kiyohara nid = target->sbp->sc_fd.fc->nodeid | FWLOCALBUS; 1457 1.29 kiyohara dv_unit = device_unit(target->sbp->sc_fd.dev); 1458 1.1 kiyohara 1459 1.29 kiyohara mutex_enter(&target->sbp->sc_mtx); 1460 1.1 kiyohara if (func == ORB_FUN_RUNQUEUE) { 1461 1.1 kiyohara ocb = STAILQ_FIRST(&target->mgm_ocb_queue); 1462 1.1 kiyohara if (target->mgm_ocb_cur != NULL || ocb == NULL) { 1463 1.29 kiyohara mutex_exit(&target->sbp->sc_mtx); 1464 1.1 kiyohara return; 1465 1.1 kiyohara } 1466 1.1 kiyohara STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb); 1467 1.29 kiyohara mutex_exit(&target->sbp->sc_mtx); 1468 1.1 kiyohara goto start; 1469 1.1 kiyohara } 1470 1.1 kiyohara if ((ocb = sbp_get_ocb(sdev)) == NULL) { 1471 1.29 kiyohara mutex_exit(&target->sbp->sc_mtx); 1472 1.1 kiyohara /* XXX */ 1473 1.1 kiyohara return; 1474 1.1 kiyohara } 1475 1.29 kiyohara mutex_exit(&target->sbp->sc_mtx); 1476 1.1 kiyohara ocb->flags = OCB_ACT_MGM; 1477 1.1 kiyohara ocb->sdev = sdev; 1478 1.1 kiyohara 1479 1.30 kiyohara memset(ocb->orb, 0, sizeof(ocb->orb)); 1480 1.1 kiyohara ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI); 1481 1.1 kiyohara ocb->orb[7] = htonl(SBP_DEV2ADDR(dv_unit, sdev->lun_id)); 1482 1.1 kiyohara 1483 1.1 kiyohara SBP_DEBUG(0) 1484 1.29 kiyohara printf("%s:%s:%s: %s\n", device_xname(sdev->target->sbp->sc_fd.dev), 1485 1.29 kiyohara __func__, sdev->bustgtlun, orb_fun_name[(func>>16)&0xf]); 1486 1.1 kiyohara END_DEBUG 1487 1.1 kiyohara switch (func) { 1488 1.1 kiyohara case ORB_FUN_LGI: 1489 1.29 kiyohara { 1490 1.29 kiyohara const off_t sbp_login_off = 1491 1.29 kiyohara sizeof(struct sbp_ocb) * SBP_QUEUE_LEN; 1492 1.29 kiyohara 1493 1.1 kiyohara ocb->orb[0] = ocb->orb[1] = 0; /* password */ 1494 1.1 kiyohara ocb->orb[2] = htonl(nid << 16); 1495 1.29 kiyohara ocb->orb[3] = htonl(sdev->dma.bus_addr + sbp_login_off); 1496 1.1 kiyohara ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id); 1497 1.1 kiyohara if (ex_login) 1498 1.1 kiyohara ocb->orb[4] |= htonl(ORB_EXV); 1499 1.1 kiyohara ocb->orb[5] = htonl(SBP_LOGIN_SIZE); 1500 1.29 kiyohara bus_dmamap_sync(sdev->dma.dma_tag, sdev->dma.dma_map, 1501 1.29 kiyohara sbp_login_off, SBP_LOGIN_SIZE, BUS_DMASYNC_PREREAD); 1502 1.1 kiyohara break; 1503 1.29 kiyohara } 1504 1.29 kiyohara 1505 1.1 kiyohara case ORB_FUN_ATA: 1506 1.1 kiyohara ocb->orb[0] = htonl((0 << 16) | 0); 1507 1.1 kiyohara ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff); 1508 1.1 kiyohara /* fall through */ 1509 1.1 kiyohara case ORB_FUN_RCN: 1510 1.1 kiyohara case ORB_FUN_LGO: 1511 1.1 kiyohara case ORB_FUN_LUR: 1512 1.1 kiyohara case ORB_FUN_RST: 1513 1.1 kiyohara case ORB_FUN_ATS: 1514 1.1 kiyohara ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id); 1515 1.1 kiyohara break; 1516 1.1 kiyohara } 1517 1.1 kiyohara 1518 1.1 kiyohara if (target->mgm_ocb_cur != NULL) { 1519 1.1 kiyohara /* there is a standing ORB */ 1520 1.29 kiyohara mutex_enter(&target->sbp->sc_mtx); 1521 1.1 kiyohara STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb); 1522 1.29 kiyohara mutex_exit(&target->sbp->sc_mtx); 1523 1.1 kiyohara return; 1524 1.1 kiyohara } 1525 1.1 kiyohara start: 1526 1.1 kiyohara target->mgm_ocb_cur = ocb; 1527 1.1 kiyohara 1528 1.29 kiyohara callout_reset(&target->mgm_ocb_timeout, 5 * hz, sbp_mgm_timeout, ocb); 1529 1.1 kiyohara xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0); 1530 1.29 kiyohara if (xfer == NULL) 1531 1.1 kiyohara return; 1532 1.1 kiyohara xfer->hand = sbp_mgm_callback; 1533 1.1 kiyohara 1534 1.1 kiyohara fp = &xfer->send.hdr; 1535 1.1 kiyohara fp->mode.wreqb.dest_hi = sdev->target->mgm_hi; 1536 1.1 kiyohara fp->mode.wreqb.dest_lo = sdev->target->mgm_lo; 1537 1.1 kiyohara fp->mode.wreqb.len = 8; 1538 1.1 kiyohara fp->mode.wreqb.extcode = 0; 1539 1.1 kiyohara xfer->send.payload[0] = htonl(nid << 16); 1540 1.1 kiyohara xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff); 1541 1.1 kiyohara 1542 1.1 kiyohara /* cache writeback & invalidate(required ORB_FUN_LGI func) */ 1543 1.1 kiyohara /* when abort_ocb, should sync POST ope ? */ 1544 1.29 kiyohara SBP_ORB_DMA_SYNC(sdev->dma, ocb->index, BUS_DMASYNC_PREWRITE); 1545 1.29 kiyohara if (fw_asyreq(xfer->fc, -1, xfer) != 0) 1546 1.29 kiyohara sbp_xfer_free(xfer); 1547 1.1 kiyohara } 1548 1.1 kiyohara 1549 1.1 kiyohara static void 1550 1.1 kiyohara sbp_print_scsi_cmd(struct sbp_ocb *ocb) 1551 1.1 kiyohara { 1552 1.29 kiyohara struct scsipi_xfer *xs = ocb->xs; 1553 1.1 kiyohara 1554 1.29 kiyohara printf("%s:%d:%d:" 1555 1.29 kiyohara " cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x," 1556 1.29 kiyohara " flags: 0x%02x, %db cmd/%db data\n", 1557 1.29 kiyohara device_xname(ocb->sdev->target->sbp->sc_fd.dev), 1558 1.29 kiyohara xs->xs_periph->periph_target, 1559 1.29 kiyohara xs->xs_periph->periph_lun, 1560 1.29 kiyohara xs->cmd->opcode, 1561 1.29 kiyohara xs->cmd->bytes[0], xs->cmd->bytes[1], 1562 1.29 kiyohara xs->cmd->bytes[2], xs->cmd->bytes[3], 1563 1.29 kiyohara xs->cmd->bytes[4], xs->cmd->bytes[5], 1564 1.29 kiyohara xs->cmd->bytes[6], xs->cmd->bytes[7], 1565 1.29 kiyohara xs->cmd->bytes[8], 1566 1.29 kiyohara xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT), 1567 1.29 kiyohara xs->cmdlen, xs->datalen); 1568 1.1 kiyohara } 1569 1.1 kiyohara 1570 1.1 kiyohara static void 1571 1.1 kiyohara sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb) 1572 1.1 kiyohara { 1573 1.1 kiyohara struct sbp_cmd_status *sbp_cmd_status; 1574 1.30 kiyohara struct scsi_sense_data *sense = &ocb->xs->sense.scsi_sense; 1575 1.1 kiyohara 1576 1.1 kiyohara sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data; 1577 1.1 kiyohara 1578 1.1 kiyohara SBP_DEBUG(0) 1579 1.1 kiyohara sbp_print_scsi_cmd(ocb); 1580 1.1 kiyohara /* XXX need decode status */ 1581 1.29 kiyohara printf("%s:" 1582 1.29 kiyohara " SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n", 1583 1.29 kiyohara ocb->sdev->bustgtlun, 1584 1.29 kiyohara sbp_cmd_status->status, 1585 1.29 kiyohara sbp_cmd_status->sfmt, 1586 1.29 kiyohara sbp_cmd_status->valid, 1587 1.29 kiyohara sbp_cmd_status->s_key, 1588 1.29 kiyohara sbp_cmd_status->s_code, 1589 1.29 kiyohara sbp_cmd_status->s_qlfr, 1590 1.29 kiyohara sbp_status->len); 1591 1.1 kiyohara END_DEBUG 1592 1.1 kiyohara 1593 1.1 kiyohara switch (sbp_cmd_status->status) { 1594 1.29 kiyohara case SCSI_CHECK: 1595 1.29 kiyohara case SCSI_BUSY: 1596 1.29 kiyohara case SCSI_TERMINATED: 1597 1.29 kiyohara if (sbp_cmd_status->sfmt == SBP_SFMT_CURR) 1598 1.29 kiyohara sense->response_code = SSD_RCODE_CURRENT; 1599 1.29 kiyohara else 1600 1.29 kiyohara sense->response_code = SSD_RCODE_DEFERRED; 1601 1.29 kiyohara if (sbp_cmd_status->valid) 1602 1.30 kiyohara sense->response_code |= SSD_RCODE_VALID; 1603 1.1 kiyohara sense->flags = sbp_cmd_status->s_key; 1604 1.29 kiyohara if (sbp_cmd_status->mark) 1605 1.1 kiyohara sense->flags |= SSD_FILEMARK; 1606 1.29 kiyohara if (sbp_cmd_status->eom) 1607 1.1 kiyohara sense->flags |= SSD_EOM; 1608 1.29 kiyohara if (sbp_cmd_status->ill_len) 1609 1.1 kiyohara sense->flags |= SSD_ILI; 1610 1.1 kiyohara 1611 1.30 kiyohara memcpy(sense->info, &sbp_cmd_status->info, 4); 1612 1.1 kiyohara 1613 1.1 kiyohara if (sbp_status->len <= 1) 1614 1.29 kiyohara /* XXX not scsi status. shouldn't be happened */ 1615 1.30 kiyohara sense->extra_len = 0; 1616 1.1 kiyohara else if (sbp_status->len <= 4) 1617 1.1 kiyohara /* add_sense_code(_qual), info, cmd_spec_info */ 1618 1.30 kiyohara sense->extra_len = 6; 1619 1.1 kiyohara else 1620 1.1 kiyohara /* fru, sense_key_spec */ 1621 1.30 kiyohara sense->extra_len = 10; 1622 1.1 kiyohara 1623 1.29 kiyohara memcpy(sense->csi, &sbp_cmd_status->cdb, 4); 1624 1.1 kiyohara 1625 1.1 kiyohara sense->asc = sbp_cmd_status->s_code; 1626 1.1 kiyohara sense->ascq = sbp_cmd_status->s_qlfr; 1627 1.30 kiyohara sense->fru = sbp_cmd_status->fru; 1628 1.1 kiyohara 1629 1.30 kiyohara memcpy(sense->sks.sks_bytes, sbp_cmd_status->s_keydep, 3); 1630 1.29 kiyohara ocb->xs->error = XS_SENSE; 1631 1.29 kiyohara ocb->xs->xs_status = sbp_cmd_status->status; 1632 1.1 kiyohara /* 1633 1.1 kiyohara { 1634 1.1 kiyohara uint8_t j, *tmp; 1635 1.1 kiyohara tmp = sense; 1636 1.29 kiyohara for (j = 0; j < 32; j += 8) 1637 1.29 kiyohara aprint_normal( 1638 1.29 kiyohara "sense %02x%02x %02x%02x %02x%02x %02x%02x\n", 1639 1.29 kiyohara tmp[j], tmp[j+1], tmp[j+2], tmp[j+3], 1640 1.29 kiyohara tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]); 1641 1.1 kiyohara 1642 1.1 kiyohara } 1643 1.1 kiyohara */ 1644 1.1 kiyohara break; 1645 1.1 kiyohara default: 1646 1.29 kiyohara aprint_error_dev(ocb->sdev->target->sbp->sc_fd.dev, 1647 1.29 kiyohara "%s:%s: unknown scsi status 0x%x\n", 1648 1.29 kiyohara __func__, ocb->sdev->bustgtlun, sbp_cmd_status->status); 1649 1.1 kiyohara } 1650 1.1 kiyohara } 1651 1.1 kiyohara 1652 1.1 kiyohara static void 1653 1.1 kiyohara sbp_fix_inq_data(struct sbp_ocb *ocb) 1654 1.1 kiyohara { 1655 1.29 kiyohara struct scsipi_xfer *xs = ocb->xs; 1656 1.1 kiyohara struct sbp_dev *sdev; 1657 1.30 kiyohara struct scsipi_inquiry_data *inq = 1658 1.30 kiyohara (struct scsipi_inquiry_data *)xs->data; 1659 1.29 kiyohara 1660 1.1 kiyohara sdev = ocb->sdev; 1661 1.1 kiyohara 1662 1.29 kiyohara #if 0 1663 1.29 kiyohara /* 1664 1.29 kiyohara * NetBSD is assuming always 0 for EVPD-bit and 'Page Code'. 1665 1.29 kiyohara */ 1666 1.29 kiyohara #define SI_EVPD 0x01 1667 1.29 kiyohara if (xs->cmd->bytes[0] & SI_EVPD) 1668 1.1 kiyohara return; 1669 1.29 kiyohara #endif 1670 1.1 kiyohara SBP_DEBUG(1) 1671 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sdev->target->sbp->sc_fd.dev), 1672 1.29 kiyohara __func__, sdev->bustgtlun); 1673 1.1 kiyohara END_DEBUG 1674 1.1 kiyohara switch (inq->device & SID_TYPE) { 1675 1.1 kiyohara case T_DIRECT: 1676 1.1 kiyohara #if 0 1677 1.29 kiyohara /* 1678 1.1 kiyohara * XXX Convert Direct Access device to RBC. 1679 1.1 kiyohara * I've never seen FireWire DA devices which support READ_6. 1680 1.1 kiyohara */ 1681 1.1 kiyohara if ((inq->device & SID_TYPE) == T_DIRECT) 1682 1.29 kiyohara inq->device |= T_SIMPLE_DIRECT; /* T_DIRECT == 0 */ 1683 1.1 kiyohara #endif 1684 1.29 kiyohara /* FALLTHROUGH */ 1685 1.29 kiyohara 1686 1.29 kiyohara case T_SIMPLE_DIRECT: 1687 1.1 kiyohara /* 1688 1.1 kiyohara * Override vendor/product/revision information. 1689 1.1 kiyohara * Some devices sometimes return strange strings. 1690 1.1 kiyohara */ 1691 1.1 kiyohara #if 1 1692 1.27 tsutsui memcpy(inq->vendor, sdev->vendor, sizeof(inq->vendor)); 1693 1.27 tsutsui memcpy(inq->product, sdev->product, sizeof(inq->product)); 1694 1.29 kiyohara memcpy(inq->revision + 2, sdev->revision, 1695 1.29 kiyohara sizeof(inq->revision)); 1696 1.1 kiyohara #endif 1697 1.1 kiyohara break; 1698 1.1 kiyohara } 1699 1.1 kiyohara /* 1700 1.1 kiyohara * Force to enable/disable tagged queuing. 1701 1.1 kiyohara * XXX CAM also checks SCP_QUEUE_DQUE flag in the control mode page. 1702 1.1 kiyohara */ 1703 1.1 kiyohara if (sbp_tags > 0) 1704 1.30 kiyohara inq->flags3 |= SID_CmdQue; 1705 1.1 kiyohara else if (sbp_tags < 0) 1706 1.30 kiyohara inq->flags3 &= ~SID_CmdQue; 1707 1.1 kiyohara 1708 1.1 kiyohara } 1709 1.1 kiyohara 1710 1.1 kiyohara static void 1711 1.29 kiyohara sbp_recv(struct fw_xfer *xfer) 1712 1.1 kiyohara { 1713 1.1 kiyohara struct fw_pkt *rfp; 1714 1.1 kiyohara #if NEED_RESPONSE 1715 1.1 kiyohara struct fw_pkt *sfp; 1716 1.1 kiyohara #endif 1717 1.29 kiyohara struct sbp_softc *sc; 1718 1.1 kiyohara struct sbp_dev *sdev; 1719 1.1 kiyohara struct sbp_ocb *ocb; 1720 1.1 kiyohara struct sbp_login_res *login_res = NULL; 1721 1.1 kiyohara struct sbp_status *sbp_status; 1722 1.1 kiyohara struct sbp_target *target; 1723 1.1 kiyohara int orb_fun, status_valid0, status_valid, l, reset_agent = 0; 1724 1.1 kiyohara uint32_t addr; 1725 1.1 kiyohara /* 1726 1.1 kiyohara uint32_t *ld; 1727 1.1 kiyohara ld = xfer->recv.buf; 1728 1.1 kiyohara printf("sbp %x %d %d %08x %08x %08x %08x\n", 1729 1.1 kiyohara xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3])); 1730 1.1 kiyohara printf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7])); 1731 1.1 kiyohara printf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11])); 1732 1.1 kiyohara */ 1733 1.29 kiyohara 1734 1.29 kiyohara sc = (struct sbp_softc *)xfer->sc; 1735 1.29 kiyohara if (xfer->resp != 0) { 1736 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, 1737 1.29 kiyohara "sbp_recv: xfer->resp = %d\n", xfer->resp); 1738 1.1 kiyohara goto done0; 1739 1.1 kiyohara } 1740 1.29 kiyohara if (xfer->recv.payload == NULL) { 1741 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, 1742 1.29 kiyohara "sbp_recv: xfer->recv.payload == NULL\n"); 1743 1.1 kiyohara goto done0; 1744 1.1 kiyohara } 1745 1.1 kiyohara rfp = &xfer->recv.hdr; 1746 1.29 kiyohara if (rfp->mode.wreqb.tcode != FWTCODE_WREQB) { 1747 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, 1748 1.29 kiyohara "sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode); 1749 1.1 kiyohara goto done0; 1750 1.1 kiyohara } 1751 1.1 kiyohara sbp_status = (struct sbp_status *)xfer->recv.payload; 1752 1.1 kiyohara addr = rfp->mode.wreqb.dest_lo; 1753 1.1 kiyohara SBP_DEBUG(2) 1754 1.1 kiyohara printf("received address 0x%x\n", addr); 1755 1.1 kiyohara END_DEBUG 1756 1.29 kiyohara target = &sc->sc_target; 1757 1.1 kiyohara l = SBP_ADDR2LUN(addr); 1758 1.1 kiyohara if (l >= target->num_lun || target->luns[l] == NULL) { 1759 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, 1760 1.1 kiyohara "sbp_recv1: invalid lun %d (target=%d)\n", 1761 1.1 kiyohara l, target->target_id); 1762 1.1 kiyohara goto done0; 1763 1.1 kiyohara } 1764 1.1 kiyohara sdev = target->luns[l]; 1765 1.1 kiyohara 1766 1.1 kiyohara ocb = NULL; 1767 1.1 kiyohara switch (sbp_status->src) { 1768 1.1 kiyohara case SRC_NEXT_EXISTS: 1769 1.1 kiyohara case SRC_NO_NEXT: 1770 1.1 kiyohara /* check mgm_ocb_cur first */ 1771 1.29 kiyohara ocb = target->mgm_ocb_cur; 1772 1.29 kiyohara if (ocb != NULL) 1773 1.1 kiyohara if (OCB_MATCH(ocb, sbp_status)) { 1774 1.29 kiyohara callout_stop(&target->mgm_ocb_timeout); 1775 1.1 kiyohara target->mgm_ocb_cur = NULL; 1776 1.1 kiyohara break; 1777 1.1 kiyohara } 1778 1.1 kiyohara ocb = sbp_dequeue_ocb(sdev, sbp_status); 1779 1.29 kiyohara if (ocb == NULL) 1780 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, 1781 1.29 kiyohara "%s:%s: No ocb(%x) on the queue\n", __func__, 1782 1.29 kiyohara sdev->bustgtlun, ntohl(sbp_status->orb_lo)); 1783 1.1 kiyohara break; 1784 1.1 kiyohara case SRC_UNSOL: 1785 1.1 kiyohara /* unsolicit */ 1786 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, 1787 1.29 kiyohara "%s:%s: unsolicit status received\n", 1788 1.29 kiyohara __func__, sdev->bustgtlun); 1789 1.1 kiyohara break; 1790 1.1 kiyohara default: 1791 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, 1792 1.29 kiyohara "%s:%s: unknown sbp_status->src\n", 1793 1.29 kiyohara __func__, sdev->bustgtlun); 1794 1.1 kiyohara } 1795 1.1 kiyohara 1796 1.1 kiyohara status_valid0 = (sbp_status->src < 2 1797 1.1 kiyohara && sbp_status->resp == SBP_REQ_CMP 1798 1.1 kiyohara && sbp_status->dead == 0); 1799 1.1 kiyohara status_valid = (status_valid0 && sbp_status->status == 0); 1800 1.1 kiyohara 1801 1.29 kiyohara if (!status_valid0 || debug > 2) { 1802 1.1 kiyohara int status; 1803 1.1 kiyohara SBP_DEBUG(0) 1804 1.29 kiyohara printf("%s:%s:%s: ORB status src:%x resp:%x dead:%x" 1805 1.29 kiyohara " len:%x stat:%x orb:%x%08x\n", 1806 1.29 kiyohara device_xname(sc->sc_fd.dev), __func__, sdev->bustgtlun, 1807 1.29 kiyohara sbp_status->src, sbp_status->resp, sbp_status->dead, 1808 1.29 kiyohara sbp_status->len, sbp_status->status, 1809 1.29 kiyohara ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo)); 1810 1.1 kiyohara END_DEBUG 1811 1.29 kiyohara printf("%s:%s\n", device_xname(sc->sc_fd.dev), sdev->bustgtlun); 1812 1.1 kiyohara status = sbp_status->status; 1813 1.30 kiyohara switch (sbp_status->resp) { 1814 1.1 kiyohara case SBP_REQ_CMP: 1815 1.1 kiyohara if (status > MAX_ORB_STATUS0) 1816 1.1 kiyohara printf("%s\n", orb_status0[MAX_ORB_STATUS0]); 1817 1.1 kiyohara else 1818 1.1 kiyohara printf("%s\n", orb_status0[status]); 1819 1.1 kiyohara break; 1820 1.1 kiyohara case SBP_TRANS_FAIL: 1821 1.1 kiyohara printf("Obj: %s, Error: %s\n", 1822 1.29 kiyohara orb_status1_object[(status>>6) & 3], 1823 1.29 kiyohara orb_status1_serial_bus_error[status & 0xf]); 1824 1.1 kiyohara break; 1825 1.1 kiyohara case SBP_ILLE_REQ: 1826 1.1 kiyohara printf("Illegal request\n"); 1827 1.1 kiyohara break; 1828 1.1 kiyohara case SBP_VEND_DEP: 1829 1.1 kiyohara printf("Vendor dependent\n"); 1830 1.1 kiyohara break; 1831 1.1 kiyohara default: 1832 1.43 andvar printf("unknown response code %d\n", sbp_status->resp); 1833 1.1 kiyohara } 1834 1.1 kiyohara } 1835 1.1 kiyohara 1836 1.1 kiyohara /* we have to reset the fetch agent if it's dead */ 1837 1.1 kiyohara if (sbp_status->dead) { 1838 1.29 kiyohara if (sdev->periph != NULL) { 1839 1.29 kiyohara scsipi_periph_freeze(sdev->periph, 1); 1840 1.29 kiyohara sdev->freeze++; 1841 1.1 kiyohara } 1842 1.1 kiyohara reset_agent = 1; 1843 1.1 kiyohara } 1844 1.1 kiyohara 1845 1.1 kiyohara if (ocb == NULL) 1846 1.1 kiyohara goto done; 1847 1.1 kiyohara 1848 1.30 kiyohara switch (ntohl(ocb->orb[4]) & ORB_FMT_MSK) { 1849 1.1 kiyohara case ORB_FMT_NOP: 1850 1.1 kiyohara break; 1851 1.1 kiyohara case ORB_FMT_VED: 1852 1.1 kiyohara break; 1853 1.1 kiyohara case ORB_FMT_STD: 1854 1.30 kiyohara switch (ocb->flags) { 1855 1.1 kiyohara case OCB_ACT_MGM: 1856 1.1 kiyohara orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK; 1857 1.1 kiyohara reset_agent = 0; 1858 1.30 kiyohara switch (orb_fun) { 1859 1.1 kiyohara case ORB_FUN_LGI: 1860 1.29 kiyohara { 1861 1.29 kiyohara const struct fwdma_alloc *dma = &sdev->dma; 1862 1.29 kiyohara const off_t sbp_login_off = 1863 1.29 kiyohara sizeof(struct sbp_ocb) * SBP_QUEUE_LEN; 1864 1.29 kiyohara 1865 1.29 kiyohara bus_dmamap_sync(dma->dma_tag, dma->dma_map, 1866 1.29 kiyohara sbp_login_off, SBP_LOGIN_SIZE, 1867 1.29 kiyohara BUS_DMASYNC_POSTREAD); 1868 1.1 kiyohara login_res = sdev->login; 1869 1.1 kiyohara login_res->len = ntohs(login_res->len); 1870 1.1 kiyohara login_res->id = ntohs(login_res->id); 1871 1.1 kiyohara login_res->cmd_hi = ntohs(login_res->cmd_hi); 1872 1.1 kiyohara login_res->cmd_lo = ntohl(login_res->cmd_lo); 1873 1.1 kiyohara if (status_valid) { 1874 1.1 kiyohara SBP_DEBUG(0) 1875 1.29 kiyohara printf("%s:%s:%s: login:" 1876 1.29 kiyohara " len %d, ID %d, cmd %08x%08x," 1877 1.29 kiyohara " recon_hold %d\n", 1878 1.29 kiyohara device_xname(sc->sc_fd.dev), 1879 1.29 kiyohara __func__, sdev->bustgtlun, 1880 1.29 kiyohara login_res->len, login_res->id, 1881 1.29 kiyohara login_res->cmd_hi, 1882 1.29 kiyohara login_res->cmd_lo, 1883 1.29 kiyohara ntohs(login_res->recon_hold)); 1884 1.1 kiyohara END_DEBUG 1885 1.1 kiyohara sbp_busy_timeout(sdev); 1886 1.1 kiyohara } else { 1887 1.1 kiyohara /* forgot logout? */ 1888 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, 1889 1.29 kiyohara "%s:%s: login failed\n", 1890 1.29 kiyohara __func__, sdev->bustgtlun); 1891 1.1 kiyohara sdev->status = SBP_DEV_RESET; 1892 1.1 kiyohara } 1893 1.1 kiyohara break; 1894 1.29 kiyohara } 1895 1.1 kiyohara case ORB_FUN_RCN: 1896 1.1 kiyohara login_res = sdev->login; 1897 1.1 kiyohara if (status_valid) { 1898 1.1 kiyohara SBP_DEBUG(0) 1899 1.29 kiyohara printf("%s:%s:%s: reconnect:" 1900 1.29 kiyohara " len %d, ID %d, cmd %08x%08x\n", 1901 1.29 kiyohara device_xname(sc->sc_fd.dev), 1902 1.29 kiyohara __func__, sdev->bustgtlun, 1903 1.29 kiyohara login_res->len, login_res->id, 1904 1.29 kiyohara login_res->cmd_hi, 1905 1.29 kiyohara login_res->cmd_lo); 1906 1.1 kiyohara END_DEBUG 1907 1.29 kiyohara sbp_agent_reset(sdev); 1908 1.1 kiyohara } else { 1909 1.1 kiyohara /* reconnection hold time exceed? */ 1910 1.1 kiyohara SBP_DEBUG(0) 1911 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, 1912 1.29 kiyohara "%s:%s: reconnect failed\n", 1913 1.29 kiyohara __func__, sdev->bustgtlun); 1914 1.1 kiyohara END_DEBUG 1915 1.1 kiyohara sbp_login(sdev); 1916 1.1 kiyohara } 1917 1.1 kiyohara break; 1918 1.1 kiyohara case ORB_FUN_LGO: 1919 1.1 kiyohara sdev->status = SBP_DEV_RESET; 1920 1.1 kiyohara break; 1921 1.1 kiyohara case ORB_FUN_RST: 1922 1.1 kiyohara sbp_busy_timeout(sdev); 1923 1.1 kiyohara break; 1924 1.1 kiyohara case ORB_FUN_LUR: 1925 1.1 kiyohara case ORB_FUN_ATA: 1926 1.1 kiyohara case ORB_FUN_ATS: 1927 1.1 kiyohara sbp_agent_reset(sdev); 1928 1.1 kiyohara break; 1929 1.1 kiyohara default: 1930 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, 1931 1.29 kiyohara "%s:%s: unknown function %d\n", 1932 1.29 kiyohara __func__, sdev->bustgtlun, orb_fun); 1933 1.1 kiyohara break; 1934 1.1 kiyohara } 1935 1.1 kiyohara sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL); 1936 1.1 kiyohara break; 1937 1.1 kiyohara case OCB_ACT_CMD: 1938 1.1 kiyohara sdev->timeout = 0; 1939 1.29 kiyohara if (ocb->xs != NULL) { 1940 1.29 kiyohara struct scsipi_xfer *xs = ocb->xs; 1941 1.29 kiyohara 1942 1.29 kiyohara if (sbp_status->len > 1) 1943 1.29 kiyohara sbp_scsi_status(sbp_status, ocb); 1944 1.1 kiyohara else 1945 1.29 kiyohara if (sbp_status->resp != SBP_REQ_CMP) 1946 1.29 kiyohara xs->error = XS_DRIVER_STUFFUP; 1947 1.29 kiyohara else { 1948 1.29 kiyohara xs->error = XS_NOERROR; 1949 1.29 kiyohara xs->resid = 0; 1950 1.1 kiyohara } 1951 1.1 kiyohara /* fix up inq data */ 1952 1.29 kiyohara if (xs->cmd->opcode == INQUIRY) 1953 1.1 kiyohara sbp_fix_inq_data(ocb); 1954 1.29 kiyohara scsipi_done(xs); 1955 1.1 kiyohara } 1956 1.1 kiyohara break; 1957 1.1 kiyohara default: 1958 1.1 kiyohara break; 1959 1.1 kiyohara } 1960 1.1 kiyohara } 1961 1.1 kiyohara 1962 1.1 kiyohara if (!use_doorbell) 1963 1.1 kiyohara sbp_free_ocb(sdev, ocb); 1964 1.1 kiyohara done: 1965 1.1 kiyohara if (reset_agent) 1966 1.1 kiyohara sbp_agent_reset(sdev); 1967 1.1 kiyohara 1968 1.1 kiyohara done0: 1969 1.1 kiyohara xfer->recv.pay_len = SBP_RECV_LEN; 1970 1.1 kiyohara /* The received packet is usually small enough to be stored within 1971 1.1 kiyohara * the buffer. In that case, the controller return ack_complete and 1972 1.43 andvar * no response is necessary. 1973 1.1 kiyohara * 1974 1.29 kiyohara * XXX fwohci.c and firewire.c should inform event_code such as 1975 1.1 kiyohara * ack_complete or ack_pending to upper driver. 1976 1.1 kiyohara */ 1977 1.1 kiyohara #if NEED_RESPONSE 1978 1.1 kiyohara xfer->send.off = 0; 1979 1.1 kiyohara sfp = (struct fw_pkt *)xfer->send.buf; 1980 1.1 kiyohara sfp->mode.wres.dst = rfp->mode.wreqb.src; 1981 1.1 kiyohara xfer->dst = sfp->mode.wres.dst; 1982 1.37 riastrad xfer->spd = uimin(sdev->target->fwdev->speed, max_speed); 1983 1.1 kiyohara xfer->hand = sbp_loginres_callback; 1984 1.1 kiyohara 1985 1.1 kiyohara sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt; 1986 1.1 kiyohara sfp->mode.wres.tcode = FWTCODE_WRES; 1987 1.1 kiyohara sfp->mode.wres.rtcode = 0; 1988 1.1 kiyohara sfp->mode.wres.pri = 0; 1989 1.1 kiyohara 1990 1.29 kiyohara if (fw_asyreq(xfer->fc, -1, xfer) != 0) { 1991 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, "mgm_orb failed\n"); 1992 1.29 kiyohara mutex_enter(&sc->sc_fwb.fwb_mtx); 1993 1.29 kiyohara STAILQ_INSERT_TAIL(&sc->sc_fwb.xferlist, xfer, link); 1994 1.29 kiyohara mutex_exit(&sc->sc_fwb.fwb_mtx); 1995 1.29 kiyohara } 1996 1.1 kiyohara #else 1997 1.1 kiyohara /* recycle */ 1998 1.29 kiyohara mutex_enter(&sc->sc_fwb.fwb_mtx); 1999 1.29 kiyohara STAILQ_INSERT_TAIL(&sc->sc_fwb.xferlist, xfer, link); 2000 1.29 kiyohara mutex_exit(&sc->sc_fwb.fwb_mtx); 2001 1.1 kiyohara #endif 2002 1.1 kiyohara 2003 1.1 kiyohara return; 2004 1.1 kiyohara 2005 1.1 kiyohara } 2006 1.1 kiyohara 2007 1.1 kiyohara static int 2008 1.1 kiyohara sbp_logout_all(struct sbp_softc *sbp) 2009 1.1 kiyohara { 2010 1.1 kiyohara struct sbp_target *target; 2011 1.1 kiyohara struct sbp_dev *sdev; 2012 1.1 kiyohara int i; 2013 1.1 kiyohara 2014 1.1 kiyohara SBP_DEBUG(0) 2015 1.1 kiyohara printf("sbp_logout_all\n"); 2016 1.1 kiyohara END_DEBUG 2017 1.29 kiyohara target = &sbp->sc_target; 2018 1.33 cegger if (target->luns != NULL) { 2019 1.1 kiyohara for (i = 0; i < target->num_lun; i++) { 2020 1.1 kiyohara sdev = target->luns[i]; 2021 1.1 kiyohara if (sdev == NULL) 2022 1.1 kiyohara continue; 2023 1.29 kiyohara callout_stop(&sdev->login_callout); 2024 1.1 kiyohara if (sdev->status >= SBP_DEV_TOATTACH && 2025 1.29 kiyohara sdev->status <= SBP_DEV_ATTACHED) 2026 1.1 kiyohara sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL); 2027 1.1 kiyohara } 2028 1.33 cegger } 2029 1.1 kiyohara 2030 1.1 kiyohara return 0; 2031 1.1 kiyohara } 2032 1.1 kiyohara 2033 1.1 kiyohara static void 2034 1.1 kiyohara sbp_free_sdev(struct sbp_dev *sdev) 2035 1.1 kiyohara { 2036 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp; 2037 1.1 kiyohara int i; 2038 1.1 kiyohara 2039 1.1 kiyohara if (sdev == NULL) 2040 1.1 kiyohara return; 2041 1.1 kiyohara for (i = 0; i < SBP_QUEUE_LEN; i++) 2042 1.29 kiyohara bus_dmamap_destroy(sc->sc_dmat, sdev->ocb[i].dmamap); 2043 1.29 kiyohara fwdma_free(sdev->dma.dma_tag, sdev->dma.dma_map, sdev->dma.v_addr); 2044 1.32 christos free(sdev, M_SBP); 2045 1.1 kiyohara } 2046 1.1 kiyohara 2047 1.1 kiyohara static void 2048 1.1 kiyohara sbp_free_target(struct sbp_target *target) 2049 1.1 kiyohara { 2050 1.1 kiyohara struct fw_xfer *xfer, *next; 2051 1.1 kiyohara int i; 2052 1.1 kiyohara 2053 1.1 kiyohara if (target->luns == NULL) 2054 1.1 kiyohara return; 2055 1.29 kiyohara callout_stop(&target->mgm_ocb_timeout); 2056 1.1 kiyohara for (i = 0; i < target->num_lun; i++) 2057 1.1 kiyohara sbp_free_sdev(target->luns[i]); 2058 1.1 kiyohara 2059 1.1 kiyohara for (xfer = STAILQ_FIRST(&target->xferlist); 2060 1.29 kiyohara xfer != NULL; xfer = next) { 2061 1.1 kiyohara next = STAILQ_NEXT(xfer, link); 2062 1.1 kiyohara fw_xfer_free_buf(xfer); 2063 1.1 kiyohara } 2064 1.1 kiyohara STAILQ_INIT(&target->xferlist); 2065 1.32 christos free(target->luns, M_SBP); 2066 1.23 yamt target->num_lun = 0; 2067 1.1 kiyohara target->luns = NULL; 2068 1.1 kiyohara target->fwdev = NULL; 2069 1.1 kiyohara } 2070 1.1 kiyohara 2071 1.1 kiyohara static void 2072 1.1 kiyohara sbp_scsipi_detach_sdev(struct sbp_dev *sdev) 2073 1.1 kiyohara { 2074 1.7 rpaulo struct sbp_target *target; 2075 1.7 rpaulo struct sbp_softc *sbp; 2076 1.7 rpaulo 2077 1.1 kiyohara if (sdev == NULL) 2078 1.1 kiyohara return; 2079 1.7 rpaulo 2080 1.7 rpaulo target = sdev->target; 2081 1.7 rpaulo if (target == NULL) 2082 1.7 rpaulo return; 2083 1.7 rpaulo 2084 1.7 rpaulo sbp = target->sbp; 2085 1.7 rpaulo 2086 1.1 kiyohara if (sdev->status == SBP_DEV_DEAD) 2087 1.1 kiyohara return; 2088 1.1 kiyohara if (sdev->status == SBP_DEV_RESET) 2089 1.1 kiyohara return; 2090 1.29 kiyohara if (sdev->periph != NULL) { 2091 1.1 kiyohara scsipi_periph_thaw(sdev->periph, sdev->freeze); 2092 1.1 kiyohara scsipi_channel_thaw(&sbp->sc_channel, 0); /* XXXX */ 2093 1.1 kiyohara sdev->freeze = 0; 2094 1.1 kiyohara if (scsipi_target_detach(&sbp->sc_channel, 2095 1.1 kiyohara target->target_id, sdev->lun_id, DETACH_FORCE) != 0) { 2096 1.29 kiyohara aprint_error_dev(sbp->sc_fd.dev, "detach failed\n"); 2097 1.1 kiyohara } 2098 1.1 kiyohara sdev->periph = NULL; 2099 1.1 kiyohara } 2100 1.29 kiyohara sbp_abort_all_ocbs(sdev, XS_DRIVER_STUFFUP); 2101 1.1 kiyohara } 2102 1.1 kiyohara 2103 1.1 kiyohara static void 2104 1.1 kiyohara sbp_scsipi_detach_target(struct sbp_target *target) 2105 1.1 kiyohara { 2106 1.1 kiyohara struct sbp_softc *sbp = target->sbp; 2107 1.1 kiyohara int i; 2108 1.1 kiyohara 2109 1.1 kiyohara if (target->luns != NULL) { 2110 1.1 kiyohara SBP_DEBUG(0) 2111 1.1 kiyohara printf("sbp_detach_target %d\n", target->target_id); 2112 1.1 kiyohara END_DEBUG 2113 1.1 kiyohara for (i = 0; i < target->num_lun; i++) 2114 1.1 kiyohara sbp_scsipi_detach_sdev(target->luns[i]); 2115 1.1 kiyohara if (config_detach(sbp->sc_bus, DETACH_FORCE) != 0) 2116 1.29 kiyohara aprint_error_dev(sbp->sc_fd.dev, "%d detach failed\n", 2117 1.29 kiyohara target->target_id); 2118 1.1 kiyohara sbp->sc_bus = NULL; 2119 1.1 kiyohara } 2120 1.1 kiyohara } 2121 1.1 kiyohara 2122 1.1 kiyohara static void 2123 1.1 kiyohara sbp_target_reset(struct sbp_dev *sdev, int method) 2124 1.1 kiyohara { 2125 1.1 kiyohara struct sbp_target *target = sdev->target; 2126 1.1 kiyohara struct sbp_dev *tsdev; 2127 1.29 kiyohara int i; 2128 1.1 kiyohara 2129 1.1 kiyohara for (i = 0; i < target->num_lun; i++) { 2130 1.1 kiyohara tsdev = target->luns[i]; 2131 1.1 kiyohara if (tsdev == NULL) 2132 1.1 kiyohara continue; 2133 1.1 kiyohara if (tsdev->status == SBP_DEV_DEAD) 2134 1.1 kiyohara continue; 2135 1.1 kiyohara if (tsdev->status == SBP_DEV_RESET) 2136 1.1 kiyohara continue; 2137 1.29 kiyohara if (sdev->periph != NULL) { 2138 1.29 kiyohara scsipi_periph_freeze(tsdev->periph, 1); 2139 1.29 kiyohara tsdev->freeze++; 2140 1.29 kiyohara } 2141 1.29 kiyohara sbp_abort_all_ocbs(tsdev, XS_TIMEOUT); 2142 1.1 kiyohara if (method == 2) 2143 1.1 kiyohara tsdev->status = SBP_DEV_LOGIN; 2144 1.1 kiyohara } 2145 1.30 kiyohara switch (method) { 2146 1.1 kiyohara case 1: 2147 1.29 kiyohara aprint_error("target reset\n"); 2148 1.1 kiyohara sbp_mgm_orb(sdev, ORB_FUN_RST, NULL); 2149 1.1 kiyohara break; 2150 1.1 kiyohara case 2: 2151 1.29 kiyohara aprint_error("reset start\n"); 2152 1.1 kiyohara sbp_reset_start(sdev); 2153 1.1 kiyohara break; 2154 1.1 kiyohara } 2155 1.1 kiyohara } 2156 1.1 kiyohara 2157 1.1 kiyohara static void 2158 1.1 kiyohara sbp_mgm_timeout(void *arg) 2159 1.1 kiyohara { 2160 1.1 kiyohara struct sbp_ocb *ocb = (struct sbp_ocb *)arg; 2161 1.1 kiyohara struct sbp_dev *sdev = ocb->sdev; 2162 1.1 kiyohara struct sbp_target *target = sdev->target; 2163 1.1 kiyohara 2164 1.29 kiyohara aprint_error_dev(sdev->target->sbp->sc_fd.dev, 2165 1.29 kiyohara "%s:%s: request timeout(mgm orb:0x%08x) ... ", 2166 1.29 kiyohara __func__, sdev->bustgtlun, (uint32_t)ocb->bus_addr); 2167 1.1 kiyohara target->mgm_ocb_cur = NULL; 2168 1.1 kiyohara sbp_free_ocb(sdev, ocb); 2169 1.1 kiyohara #if 0 2170 1.1 kiyohara /* XXX */ 2171 1.29 kiyohara aprint_error("run next request\n"); 2172 1.1 kiyohara sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL); 2173 1.1 kiyohara #endif 2174 1.29 kiyohara aprint_error_dev(sdev->target->sbp->sc_fd.dev, 2175 1.29 kiyohara "%s:%s: reset start\n", __func__, sdev->bustgtlun); 2176 1.1 kiyohara sbp_reset_start(sdev); 2177 1.1 kiyohara } 2178 1.1 kiyohara 2179 1.1 kiyohara static void 2180 1.1 kiyohara sbp_timeout(void *arg) 2181 1.1 kiyohara { 2182 1.1 kiyohara struct sbp_ocb *ocb = (struct sbp_ocb *)arg; 2183 1.1 kiyohara struct sbp_dev *sdev = ocb->sdev; 2184 1.1 kiyohara 2185 1.29 kiyohara aprint_error_dev(sdev->target->sbp->sc_fd.dev, 2186 1.29 kiyohara "%s:%s: request timeout(cmd orb:0x%08x) ... ", 2187 1.29 kiyohara __func__, sdev->bustgtlun, (uint32_t)ocb->bus_addr); 2188 1.1 kiyohara 2189 1.29 kiyohara sdev->timeout++; 2190 1.30 kiyohara switch (sdev->timeout) { 2191 1.1 kiyohara case 1: 2192 1.29 kiyohara aprint_error("agent reset\n"); 2193 1.29 kiyohara if (sdev->periph != NULL) { 2194 1.29 kiyohara scsipi_periph_freeze(sdev->periph, 1); 2195 1.29 kiyohara sdev->freeze++; 2196 1.29 kiyohara } 2197 1.29 kiyohara sbp_abort_all_ocbs(sdev, XS_TIMEOUT); 2198 1.1 kiyohara sbp_agent_reset(sdev); 2199 1.1 kiyohara break; 2200 1.1 kiyohara case 2: 2201 1.1 kiyohara case 3: 2202 1.1 kiyohara sbp_target_reset(sdev, sdev->timeout - 1); 2203 1.1 kiyohara break; 2204 1.29 kiyohara default: 2205 1.29 kiyohara aprint_error("\n"); 2206 1.1 kiyohara #if 0 2207 1.1 kiyohara /* XXX give up */ 2208 1.29 kiyohara sbp_scsipi_detach_target(target); 2209 1.1 kiyohara if (target->luns != NULL) 2210 1.32 christos free(target->luns, M_SBP); 2211 1.23 yamt target->num_lun = 0; 2212 1.1 kiyohara target->luns = NULL; 2213 1.1 kiyohara target->fwdev = NULL; 2214 1.1 kiyohara #endif 2215 1.1 kiyohara } 2216 1.1 kiyohara } 2217 1.1 kiyohara 2218 1.1 kiyohara static void 2219 1.29 kiyohara sbp_action1(struct sbp_softc *sc, struct scsipi_xfer *xs) 2220 1.1 kiyohara { 2221 1.29 kiyohara struct sbp_target *target = &sc->sc_target; 2222 1.1 kiyohara struct sbp_dev *sdev = NULL; 2223 1.29 kiyohara struct sbp_ocb *ocb; 2224 1.29 kiyohara int speed, flag, error; 2225 1.29 kiyohara void *cdb; 2226 1.1 kiyohara 2227 1.1 kiyohara /* target:lun -> sdev mapping */ 2228 1.29 kiyohara if (target->fwdev != NULL && 2229 1.29 kiyohara xs->xs_periph->periph_lun < target->num_lun) { 2230 1.29 kiyohara sdev = target->luns[xs->xs_periph->periph_lun]; 2231 1.29 kiyohara if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED && 2232 1.29 kiyohara sdev->status != SBP_DEV_PROBE) 2233 1.29 kiyohara sdev = NULL; 2234 1.1 kiyohara } 2235 1.1 kiyohara 2236 1.29 kiyohara if (sdev == NULL) { 2237 1.1 kiyohara SBP_DEBUG(1) 2238 1.29 kiyohara printf("%s:%d:%d: Invalid target (target needed)\n", 2239 1.29 kiyohara sc ? device_xname(sc->sc_fd.dev) : "???", 2240 1.29 kiyohara xs->xs_periph->periph_target, 2241 1.29 kiyohara xs->xs_periph->periph_lun); 2242 1.1 kiyohara END_DEBUG 2243 1.1 kiyohara 2244 1.29 kiyohara xs->error = XS_DRIVER_STUFFUP; 2245 1.29 kiyohara scsipi_done(xs); 2246 1.29 kiyohara return; 2247 1.1 kiyohara } 2248 1.1 kiyohara 2249 1.1 kiyohara SBP_DEBUG(2) 2250 1.29 kiyohara printf("%s:%d:%d:" 2251 1.29 kiyohara " cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x," 2252 1.29 kiyohara " flags: 0x%02x, %db cmd/%db data\n", 2253 1.29 kiyohara device_xname(sc->sc_fd.dev), 2254 1.29 kiyohara xs->xs_periph->periph_target, 2255 1.29 kiyohara xs->xs_periph->periph_lun, 2256 1.29 kiyohara xs->cmd->opcode, 2257 1.29 kiyohara xs->cmd->bytes[0], xs->cmd->bytes[1], 2258 1.29 kiyohara xs->cmd->bytes[2], xs->cmd->bytes[3], 2259 1.29 kiyohara xs->cmd->bytes[4], xs->cmd->bytes[5], 2260 1.29 kiyohara xs->cmd->bytes[6], xs->cmd->bytes[7], 2261 1.29 kiyohara xs->cmd->bytes[8], 2262 1.29 kiyohara xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT), 2263 1.29 kiyohara xs->cmdlen, xs->datalen); 2264 1.29 kiyohara END_DEBUG 2265 1.29 kiyohara mutex_enter(&sc->sc_mtx); 2266 1.29 kiyohara ocb = sbp_get_ocb(sdev); 2267 1.29 kiyohara mutex_exit(&sc->sc_mtx); 2268 1.29 kiyohara if (ocb == NULL) { 2269 1.29 kiyohara xs->error = XS_REQUEUE; 2270 1.29 kiyohara if (sdev->freeze == 0) { 2271 1.29 kiyohara scsipi_periph_freeze(sdev->periph, 1); 2272 1.29 kiyohara sdev->freeze++; 2273 1.1 kiyohara } 2274 1.29 kiyohara scsipi_done(xs); 2275 1.29 kiyohara return; 2276 1.29 kiyohara } 2277 1.1 kiyohara 2278 1.29 kiyohara ocb->flags = OCB_ACT_CMD; 2279 1.29 kiyohara ocb->sdev = sdev; 2280 1.29 kiyohara ocb->xs = xs; 2281 1.29 kiyohara ocb->orb[0] = htonl(1 << 31); 2282 1.29 kiyohara ocb->orb[1] = 0; 2283 1.29 kiyohara ocb->orb[2] = htonl(((sc->sc_fd.fc->nodeid | FWLOCALBUS) << 16)); 2284 1.29 kiyohara ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET); 2285 1.37 riastrad speed = uimin(target->fwdev->speed, max_speed); 2286 1.29 kiyohara ocb->orb[4] = 2287 1.29 kiyohara htonl(ORB_NOTIFY | ORB_CMD_SPD(speed) | ORB_CMD_MAXP(speed + 7)); 2288 1.29 kiyohara if ((xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) == 2289 1.29 kiyohara XS_CTL_DATA_IN) { 2290 1.29 kiyohara ocb->orb[4] |= htonl(ORB_CMD_IN); 2291 1.29 kiyohara flag = BUS_DMA_READ; 2292 1.29 kiyohara } else 2293 1.29 kiyohara flag = BUS_DMA_WRITE; 2294 1.1 kiyohara 2295 1.29 kiyohara cdb = xs->cmd; 2296 1.29 kiyohara memcpy((void *)&ocb->orb[5], cdb, xs->cmdlen); 2297 1.1 kiyohara /* 2298 1.1 kiyohara printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3])); 2299 1.1 kiyohara printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7])); 2300 1.1 kiyohara */ 2301 1.29 kiyohara if (xs->datalen > 0) { 2302 1.29 kiyohara error = bus_dmamap_load(sc->sc_dmat, ocb->dmamap, 2303 1.29 kiyohara xs->data, xs->datalen, NULL, BUS_DMA_NOWAIT | flag); 2304 1.29 kiyohara if (error) { 2305 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, 2306 1.29 kiyohara "DMA map load error %d\n", error); 2307 1.29 kiyohara xs->error = XS_DRIVER_STUFFUP; 2308 1.29 kiyohara scsipi_done(xs); 2309 1.1 kiyohara } else 2310 1.29 kiyohara sbp_execute_ocb(ocb, ocb->dmamap->dm_segs, 2311 1.29 kiyohara ocb->dmamap->dm_nsegs); 2312 1.29 kiyohara } else 2313 1.29 kiyohara sbp_execute_ocb(ocb, NULL, 0); 2314 1.1 kiyohara 2315 1.1 kiyohara return; 2316 1.1 kiyohara } 2317 1.1 kiyohara 2318 1.1 kiyohara static void 2319 1.29 kiyohara sbp_execute_ocb(struct sbp_ocb *ocb, bus_dma_segment_t *segments, int seg) 2320 1.1 kiyohara { 2321 1.1 kiyohara struct sbp_ocb *prev; 2322 1.1 kiyohara bus_dma_segment_t *s; 2323 1.29 kiyohara int i; 2324 1.1 kiyohara 2325 1.1 kiyohara SBP_DEBUG(2) 2326 1.1 kiyohara printf("sbp_execute_ocb: seg %d", seg); 2327 1.1 kiyohara for (i = 0; i < seg; i++) 2328 1.1 kiyohara printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr, 2329 1.29 kiyohara (uintmax_t)segments[i].ds_len); 2330 1.1 kiyohara printf("\n"); 2331 1.1 kiyohara END_DEBUG 2332 1.1 kiyohara 2333 1.1 kiyohara if (seg == 1) { 2334 1.1 kiyohara /* direct pointer */ 2335 1.29 kiyohara s = segments; 2336 1.1 kiyohara if (s->ds_len > SBP_SEG_MAX) 2337 1.1 kiyohara panic("ds_len > SBP_SEG_MAX, fix busdma code"); 2338 1.1 kiyohara ocb->orb[3] = htonl(s->ds_addr); 2339 1.1 kiyohara ocb->orb[4] |= htonl(s->ds_len); 2340 1.29 kiyohara } else if (seg > 1) { 2341 1.1 kiyohara /* page table */ 2342 1.1 kiyohara for (i = 0; i < seg; i++) { 2343 1.1 kiyohara s = &segments[i]; 2344 1.1 kiyohara SBP_DEBUG(0) 2345 1.1 kiyohara /* XXX LSI Logic "< 16 byte" bug might be hit */ 2346 1.1 kiyohara if (s->ds_len < 16) 2347 1.1 kiyohara printf("sbp_execute_ocb: warning, " 2348 1.29 kiyohara "segment length(%jd) is less than 16." 2349 1.29 kiyohara "(seg=%d/%d)\n", 2350 1.29 kiyohara (uintmax_t)s->ds_len, i + 1, seg); 2351 1.1 kiyohara END_DEBUG 2352 1.1 kiyohara if (s->ds_len > SBP_SEG_MAX) 2353 1.1 kiyohara panic("ds_len > SBP_SEG_MAX, fix busdma code"); 2354 1.1 kiyohara ocb->ind_ptr[i].hi = htonl(s->ds_len << 16); 2355 1.1 kiyohara ocb->ind_ptr[i].lo = htonl(s->ds_addr); 2356 1.1 kiyohara } 2357 1.1 kiyohara ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg); 2358 1.1 kiyohara } 2359 1.29 kiyohara 2360 1.29 kiyohara if (seg > 0) { 2361 1.29 kiyohara struct sbp_softc *sc = ocb->sdev->target->sbp; 2362 1.29 kiyohara const int flag = (ntohl(ocb->orb[4]) & ORB_CMD_IN) ? 2363 1.29 kiyohara BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE; 2364 1.29 kiyohara 2365 1.29 kiyohara bus_dmamap_sync(sc->sc_dmat, ocb->dmamap, 2366 1.29 kiyohara 0, ocb->dmamap->dm_mapsize, flag); 2367 1.29 kiyohara } 2368 1.1 kiyohara prev = sbp_enqueue_ocb(ocb->sdev, ocb); 2369 1.29 kiyohara SBP_ORB_DMA_SYNC(ocb->sdev->dma, ocb->index, BUS_DMASYNC_PREWRITE); 2370 1.1 kiyohara if (use_doorbell) { 2371 1.1 kiyohara if (prev == NULL) { 2372 1.1 kiyohara if (ocb->sdev->last_ocb != NULL) 2373 1.1 kiyohara sbp_doorbell(ocb->sdev); 2374 1.1 kiyohara else 2375 1.29 kiyohara sbp_orb_pointer(ocb->sdev, ocb); 2376 1.1 kiyohara } 2377 1.29 kiyohara } else 2378 1.1 kiyohara if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) { 2379 1.1 kiyohara ocb->sdev->flags &= ~ORB_LINK_DEAD; 2380 1.29 kiyohara sbp_orb_pointer(ocb->sdev, ocb); 2381 1.1 kiyohara } 2382 1.1 kiyohara } 2383 1.1 kiyohara 2384 1.1 kiyohara static struct sbp_ocb * 2385 1.1 kiyohara sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status) 2386 1.1 kiyohara { 2387 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp; 2388 1.1 kiyohara struct sbp_ocb *ocb; 2389 1.1 kiyohara struct sbp_ocb *next; 2390 1.29 kiyohara int order = 0; 2391 1.1 kiyohara 2392 1.1 kiyohara SBP_DEBUG(1) 2393 1.29 kiyohara printf("%s:%s:%s: 0x%08x src %d\n", device_xname(sc->sc_fd.dev), 2394 1.29 kiyohara __func__, sdev->bustgtlun, ntohl(sbp_status->orb_lo), 2395 1.29 kiyohara sbp_status->src); 2396 1.1 kiyohara END_DEBUG 2397 1.29 kiyohara mutex_enter(&sc->sc_mtx); 2398 1.1 kiyohara for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) { 2399 1.1 kiyohara next = STAILQ_NEXT(ocb, ocb); 2400 1.1 kiyohara if (OCB_MATCH(ocb, sbp_status)) { 2401 1.1 kiyohara /* found */ 2402 1.29 kiyohara SBP_ORB_DMA_SYNC(sdev->dma, ocb->index, 2403 1.29 kiyohara BUS_DMASYNC_POSTWRITE); 2404 1.1 kiyohara STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb); 2405 1.29 kiyohara if (ocb->xs != NULL) 2406 1.29 kiyohara callout_stop(&ocb->xs->xs_callout); 2407 1.1 kiyohara if (ntohl(ocb->orb[4]) & 0xffff) { 2408 1.29 kiyohara const int flag = 2409 1.29 kiyohara (ntohl(ocb->orb[4]) & ORB_CMD_IN) ? 2410 1.29 kiyohara BUS_DMASYNC_POSTREAD : 2411 1.29 kiyohara BUS_DMASYNC_POSTWRITE; 2412 1.29 kiyohara 2413 1.29 kiyohara bus_dmamap_sync(sc->sc_dmat, ocb->dmamap, 2414 1.29 kiyohara 0, ocb->dmamap->dm_mapsize, flag); 2415 1.29 kiyohara bus_dmamap_unload(sc->sc_dmat, ocb->dmamap); 2416 1.29 kiyohara 2417 1.1 kiyohara } 2418 1.1 kiyohara if (!use_doorbell) { 2419 1.1 kiyohara if (sbp_status->src == SRC_NO_NEXT) { 2420 1.1 kiyohara if (next != NULL) 2421 1.29 kiyohara sbp_orb_pointer(sdev, next); 2422 1.29 kiyohara else if (order > 0) 2423 1.1 kiyohara /* 2424 1.1 kiyohara * Unordered execution 2425 1.1 kiyohara * We need to send pointer for 2426 1.1 kiyohara * next ORB 2427 1.1 kiyohara */ 2428 1.1 kiyohara sdev->flags |= ORB_LINK_DEAD; 2429 1.1 kiyohara } 2430 1.1 kiyohara } 2431 1.1 kiyohara break; 2432 1.1 kiyohara } else 2433 1.29 kiyohara order++; 2434 1.1 kiyohara } 2435 1.29 kiyohara mutex_exit(&sc->sc_mtx); 2436 1.29 kiyohara 2437 1.29 kiyohara if (ocb && use_doorbell) { 2438 1.29 kiyohara /* 2439 1.29 kiyohara * XXX this is not correct for unordered 2440 1.29 kiyohara * execution. 2441 1.29 kiyohara */ 2442 1.29 kiyohara if (sdev->last_ocb != NULL) 2443 1.29 kiyohara sbp_free_ocb(sdev, sdev->last_ocb); 2444 1.29 kiyohara sdev->last_ocb = ocb; 2445 1.29 kiyohara if (next != NULL && 2446 1.29 kiyohara sbp_status->src == SRC_NO_NEXT) 2447 1.29 kiyohara sbp_doorbell(sdev); 2448 1.29 kiyohara } 2449 1.29 kiyohara 2450 1.1 kiyohara SBP_DEBUG(0) 2451 1.29 kiyohara if (ocb && order > 0) 2452 1.29 kiyohara printf("%s:%s:%s: unordered execution order:%d\n", 2453 1.29 kiyohara device_xname(sc->sc_fd.dev), __func__, sdev->bustgtlun, 2454 1.29 kiyohara order); 2455 1.1 kiyohara END_DEBUG 2456 1.29 kiyohara return ocb; 2457 1.1 kiyohara } 2458 1.1 kiyohara 2459 1.1 kiyohara static struct sbp_ocb * 2460 1.1 kiyohara sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb) 2461 1.1 kiyohara { 2462 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp; 2463 1.29 kiyohara struct sbp_ocb *tocb, *prev, *prev2; 2464 1.1 kiyohara 2465 1.1 kiyohara SBP_DEBUG(1) 2466 1.29 kiyohara printf("%s:%s:%s: 0x%08jx\n", device_xname(sc->sc_fd.dev), 2467 1.29 kiyohara __func__, sdev->bustgtlun, (uintmax_t)ocb->bus_addr); 2468 1.1 kiyohara END_DEBUG 2469 1.29 kiyohara mutex_enter(&sc->sc_mtx); 2470 1.29 kiyohara prev = NULL; 2471 1.29 kiyohara STAILQ_FOREACH(tocb, &sdev->ocbs, ocb) 2472 1.29 kiyohara prev = tocb; 2473 1.29 kiyohara prev2 = prev; 2474 1.1 kiyohara STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb); 2475 1.29 kiyohara mutex_exit(&sc->sc_mtx); 2476 1.1 kiyohara 2477 1.29 kiyohara callout_reset(&ocb->xs->xs_callout, mstohz(ocb->xs->timeout), 2478 1.29 kiyohara sbp_timeout, ocb); 2479 1.1 kiyohara 2480 1.1 kiyohara if (use_doorbell && prev == NULL) 2481 1.1 kiyohara prev2 = sdev->last_ocb; 2482 1.1 kiyohara 2483 1.1 kiyohara if (prev2 != NULL) { 2484 1.1 kiyohara SBP_DEBUG(2) 2485 1.1 kiyohara printf("linking chain 0x%jx -> 0x%jx\n", 2486 1.1 kiyohara (uintmax_t)prev2->bus_addr, (uintmax_t)ocb->bus_addr); 2487 1.1 kiyohara END_DEBUG 2488 1.29 kiyohara /* 2489 1.29 kiyohara * Suppress compiler optimization so that orb[1] must be 2490 1.29 kiyohara * written first. 2491 1.29 kiyohara * XXX We may need an explicit memory barrier for other 2492 1.29 kiyohara * architectures other than i386/amd64. 2493 1.29 kiyohara */ 2494 1.29 kiyohara *(volatile uint32_t *)&prev2->orb[1] = htonl(ocb->bus_addr); 2495 1.29 kiyohara *(volatile uint32_t *)&prev2->orb[0] = 0; 2496 1.1 kiyohara } 2497 1.1 kiyohara 2498 1.1 kiyohara return prev; 2499 1.1 kiyohara } 2500 1.1 kiyohara 2501 1.1 kiyohara static struct sbp_ocb * 2502 1.1 kiyohara sbp_get_ocb(struct sbp_dev *sdev) 2503 1.1 kiyohara { 2504 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp; 2505 1.1 kiyohara struct sbp_ocb *ocb; 2506 1.19 kiyohara 2507 1.29 kiyohara KASSERT(mutex_owned(&sc->sc_mtx)); 2508 1.29 kiyohara 2509 1.1 kiyohara ocb = STAILQ_FIRST(&sdev->free_ocbs); 2510 1.1 kiyohara if (ocb == NULL) { 2511 1.1 kiyohara sdev->flags |= ORB_SHORTAGE; 2512 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, 2513 1.29 kiyohara "ocb shortage!!!\n"); 2514 1.1 kiyohara return NULL; 2515 1.1 kiyohara } 2516 1.1 kiyohara STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb); 2517 1.29 kiyohara ocb->xs = NULL; 2518 1.29 kiyohara return ocb; 2519 1.1 kiyohara } 2520 1.1 kiyohara 2521 1.1 kiyohara static void 2522 1.1 kiyohara sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb) 2523 1.1 kiyohara { 2524 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp; 2525 1.29 kiyohara int count; 2526 1.29 kiyohara 2527 1.1 kiyohara ocb->flags = 0; 2528 1.29 kiyohara ocb->xs = NULL; 2529 1.19 kiyohara 2530 1.29 kiyohara mutex_enter(&sc->sc_mtx); 2531 1.1 kiyohara STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb); 2532 1.29 kiyohara mutex_exit(&sc->sc_mtx); 2533 1.29 kiyohara if (sdev->flags & ORB_SHORTAGE) { 2534 1.1 kiyohara sdev->flags &= ~ORB_SHORTAGE; 2535 1.1 kiyohara count = sdev->freeze; 2536 1.1 kiyohara sdev->freeze = 0; 2537 1.29 kiyohara if (sdev->periph) 2538 1.29 kiyohara scsipi_periph_thaw(sdev->periph, count); 2539 1.29 kiyohara scsipi_channel_thaw(&sc->sc_channel, 0); 2540 1.1 kiyohara } 2541 1.1 kiyohara } 2542 1.1 kiyohara 2543 1.1 kiyohara static void 2544 1.1 kiyohara sbp_abort_ocb(struct sbp_ocb *ocb, int status) 2545 1.1 kiyohara { 2546 1.29 kiyohara struct sbp_softc *sc; 2547 1.1 kiyohara struct sbp_dev *sdev; 2548 1.1 kiyohara 2549 1.1 kiyohara sdev = ocb->sdev; 2550 1.29 kiyohara sc = sdev->target->sbp; 2551 1.1 kiyohara SBP_DEBUG(0) 2552 1.29 kiyohara printf("%s:%s:%s: sbp_abort_ocb 0x%jx\n", device_xname(sc->sc_fd.dev), 2553 1.29 kiyohara __func__, sdev->bustgtlun, (uintmax_t)ocb->bus_addr); 2554 1.1 kiyohara END_DEBUG 2555 1.1 kiyohara SBP_DEBUG(1) 2556 1.29 kiyohara if (ocb->xs != NULL) 2557 1.1 kiyohara sbp_print_scsi_cmd(ocb); 2558 1.1 kiyohara END_DEBUG 2559 1.1 kiyohara if (ntohl(ocb->orb[4]) & 0xffff) { 2560 1.29 kiyohara const int flag = (ntohl(ocb->orb[4]) & ORB_CMD_IN) ? 2561 1.29 kiyohara BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE; 2562 1.29 kiyohara 2563 1.29 kiyohara bus_dmamap_sync(sc->sc_dmat, ocb->dmamap, 2564 1.29 kiyohara 0, ocb->dmamap->dm_mapsize, flag); 2565 1.29 kiyohara bus_dmamap_unload(sc->sc_dmat, ocb->dmamap); 2566 1.29 kiyohara } 2567 1.29 kiyohara if (ocb->xs != NULL) { 2568 1.29 kiyohara callout_stop(&ocb->xs->xs_callout); 2569 1.29 kiyohara ocb->xs->error = status; 2570 1.29 kiyohara scsipi_done(ocb->xs); 2571 1.1 kiyohara } 2572 1.1 kiyohara sbp_free_ocb(sdev, ocb); 2573 1.1 kiyohara } 2574 1.1 kiyohara 2575 1.1 kiyohara static void 2576 1.1 kiyohara sbp_abort_all_ocbs(struct sbp_dev *sdev, int status) 2577 1.1 kiyohara { 2578 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp; 2579 1.1 kiyohara struct sbp_ocb *ocb, *next; 2580 1.1 kiyohara STAILQ_HEAD(, sbp_ocb) temp; 2581 1.1 kiyohara 2582 1.29 kiyohara mutex_enter(&sc->sc_mtx); 2583 1.29 kiyohara STAILQ_INIT(&temp); 2584 1.29 kiyohara STAILQ_CONCAT(&temp, &sdev->ocbs); 2585 1.29 kiyohara STAILQ_INIT(&sdev->ocbs); 2586 1.29 kiyohara mutex_exit(&sc->sc_mtx); 2587 1.1 kiyohara 2588 1.1 kiyohara for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) { 2589 1.1 kiyohara next = STAILQ_NEXT(ocb, ocb); 2590 1.1 kiyohara sbp_abort_ocb(ocb, status); 2591 1.1 kiyohara } 2592 1.1 kiyohara if (sdev->last_ocb != NULL) { 2593 1.1 kiyohara sbp_free_ocb(sdev, sdev->last_ocb); 2594 1.1 kiyohara sdev->last_ocb = NULL; 2595 1.1 kiyohara } 2596 1.1 kiyohara } 2597