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