The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/ieee1394/sbp.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 389eff56c89d14fa3aa4fe01e792a058


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.