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