sbp.c revision 168752
1139749Simp/*-
2122526Ssimokawa * Copyright (c) 2003 Hidetoshi Shimokawa
3122526Ssimokawa * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
4103285Sikob * All rights reserved.
5103285Sikob *
6103285Sikob * Redistribution and use in source and binary forms, with or without
7103285Sikob * modification, are permitted provided that the following conditions
8103285Sikob * are met:
9103285Sikob * 1. Redistributions of source code must retain the above copyright
10103285Sikob *    notice, this list of conditions and the following disclaimer.
11103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
12103285Sikob *    notice, this list of conditions and the following disclaimer in the
13103285Sikob *    documentation and/or other materials provided with the distribution.
14103285Sikob * 3. All advertising materials mentioning features or use of this software
15103285Sikob *    must display the acknowledgement as bellow:
16103285Sikob *
17103285Sikob *    This product includes software developed by K. Kobayashi and H. Shimokawa
18103285Sikob *
19103285Sikob * 4. The name of the author may not be used to endorse or promote products
20103285Sikob *    derived from this software without specific prior written permission.
21103285Sikob *
22103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25103285Sikob * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32103285Sikob * POSSIBILITY OF SUCH DAMAGE.
33103285Sikob *
34103285Sikob * $FreeBSD: head/sys/dev/firewire/sbp.c 168752 2007-04-15 08:49:19Z scottl $
35103285Sikob *
36103285Sikob */
37103285Sikob
38103285Sikob#include <sys/param.h>
39103285Sikob#include <sys/systm.h>
40103285Sikob#include <sys/module.h>
41103285Sikob#include <sys/bus.h>
42127468Ssimokawa#include <sys/kernel.h>
43103285Sikob#include <sys/sysctl.h>
44103285Sikob#include <machine/bus.h>
45103285Sikob#include <sys/malloc.h>
46127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 501102
47117126Sscottl#include <sys/lock.h>
48117126Sscottl#include <sys/mutex.h>
49117732Ssimokawa#endif
50103285Sikob
51127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500106
52112136Ssimokawa#include <sys/devicestat.h>	/* for struct devstat */
53112136Ssimokawa#endif
54103285Sikob
55127468Ssimokawa#ifdef __DragonFly__
56127468Ssimokawa#include <bus/cam/cam.h>
57127468Ssimokawa#include <bus/cam/cam_ccb.h>
58127468Ssimokawa#include <bus/cam/cam_sim.h>
59127468Ssimokawa#include <bus/cam/cam_xpt_sim.h>
60127468Ssimokawa#include <bus/cam/cam_debug.h>
61127468Ssimokawa#include <bus/cam/cam_periph.h>
62127468Ssimokawa#include <bus/cam/scsi/scsi_all.h>
63127468Ssimokawa
64127468Ssimokawa#include <bus/firewire/firewire.h>
65127468Ssimokawa#include <bus/firewire/firewirereg.h>
66127468Ssimokawa#include <bus/firewire/fwdma.h>
67127468Ssimokawa#include <bus/firewire/iec13213.h>
68127468Ssimokawa#include "sbp.h"
69127468Ssimokawa#else
70103285Sikob#include <cam/cam.h>
71103285Sikob#include <cam/cam_ccb.h>
72103285Sikob#include <cam/cam_sim.h>
73103285Sikob#include <cam/cam_xpt_sim.h>
74103285Sikob#include <cam/cam_debug.h>
75103285Sikob#include <cam/cam_periph.h>
76103285Sikob#include <cam/scsi/scsi_all.h>
77103285Sikob
78103285Sikob#include <dev/firewire/firewire.h>
79103285Sikob#include <dev/firewire/firewirereg.h>
80113584Ssimokawa#include <dev/firewire/fwdma.h>
81103285Sikob#include <dev/firewire/iec13213.h>
82120660Ssimokawa#include <dev/firewire/sbp.h>
83127468Ssimokawa#endif
84103285Sikob
85103285Sikob#define ccb_sdev_ptr	spriv_ptr0
86103285Sikob#define ccb_sbp_ptr	spriv_ptr1
87103285Sikob
88111615Ssimokawa#define SBP_NUM_TARGETS 8 /* MAX 64 */
89121185Ssimokawa/*
90121185Ssimokawa * Scan_bus doesn't work for more than 8 LUNs
91121185Ssimokawa * because of CAM_SCSI2_MAXLUN in cam_xpt.c
92121185Ssimokawa */
93121185Ssimokawa#define SBP_NUM_LUNS 64
94167622Ssimokawa#define SBP_MAXPHYS  MIN(MAXPHYS, (512*1024) /* 512KB */)
95113584Ssimokawa#define SBP_DMA_SIZE PAGE_SIZE
96113584Ssimokawa#define SBP_LOGIN_SIZE sizeof(struct sbp_login_res)
97113584Ssimokawa#define SBP_QUEUE_LEN ((SBP_DMA_SIZE - SBP_LOGIN_SIZE) / sizeof(struct sbp_ocb))
98103285Sikob#define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS)
99113584Ssimokawa
100111615Ssimokawa/*
101111615Ssimokawa * STATUS FIFO addressing
102111615Ssimokawa *   bit
103111615Ssimokawa * -----------------------
104130532Sdfr *  0- 1( 2): 0 (alignment)
105111615Ssimokawa *  2- 7( 6): target
106111615Ssimokawa *  8-15( 8): lun
107120660Ssimokawa * 16-31( 8): reserved
108111615Ssimokawa * 32-47(16): SBP_BIND_HI
109111615Ssimokawa * 48-64(16): bus_id, node_id
110111615Ssimokawa */
111103285Sikob#define SBP_BIND_HI 0x1
112120660Ssimokawa#define SBP_DEV2ADDR(t, l) \
113120660Ssimokawa	(((u_int64_t)SBP_BIND_HI << 32) \
114120660Ssimokawa	| (((l) & 0xff) << 8) \
115120660Ssimokawa	| (((t) & 0x3f) << 2))
116103285Sikob#define SBP_ADDR2TRG(a)	(((a) >> 2) & 0x3f)
117103285Sikob#define SBP_ADDR2LUN(a)	(((a) >> 8) & 0xff)
118120660Ssimokawa#define SBP_INITIATOR 7
119103285Sikob
120103285Sikobstatic char *orb_fun_name[] = {
121120660Ssimokawa	ORB_FUN_NAMES
122103285Sikob};
123103285Sikob
124111203Ssimokawastatic int debug = 0;
125103285Sikobstatic int auto_login = 1;
126124251Ssimokawastatic int max_speed = -1;
127111199Ssimokawastatic int sbp_cold = 1;
128122387Ssimokawastatic int ex_login = 1;
129122387Ssimokawastatic int login_delay = 1000;	/* msec */
130122387Ssimokawastatic int scan_delay = 500;	/* msec */
131127468Ssimokawastatic int use_doorbell = 0;
132127468Ssimokawastatic int sbp_tags = 0;
133103285Sikob
134103285SikobSYSCTL_DECL(_hw_firewire);
135103285SikobSYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem");
136103285SikobSYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0,
137103285Sikob	"SBP debug flag");
138103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0,
139103285Sikob	"SBP perform login automatically");
140103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0,
141103285Sikob	"SBP transfer max speed");
142121792SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, exclusive_login, CTLFLAG_RW,
143130677Ssimokawa	&ex_login, 0, "SBP enable exclusive login");
144122387SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, login_delay, CTLFLAG_RW,
145122387Ssimokawa	&login_delay, 0, "SBP login delay in msec");
146122387SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, scan_delay, CTLFLAG_RW,
147122387Ssimokawa	&scan_delay, 0, "SBP scan delay in msec");
148127468SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, use_doorbell, CTLFLAG_RW,
149127468Ssimokawa	&use_doorbell, 0, "SBP use doorbell request");
150127468SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, tags, CTLFLAG_RW, &sbp_tags, 0,
151127468Ssimokawa	"SBP tagged queuing support");
152103285Sikob
153122387SsimokawaTUNABLE_INT("hw.firewire.sbp.auto_login", &auto_login);
154122387SsimokawaTUNABLE_INT("hw.firewire.sbp.max_speed", &max_speed);
155122387SsimokawaTUNABLE_INT("hw.firewire.sbp.exclusive_login", &ex_login);
156122387SsimokawaTUNABLE_INT("hw.firewire.sbp.login_delay", &login_delay);
157122387SsimokawaTUNABLE_INT("hw.firewire.sbp.scan_delay", &scan_delay);
158127468SsimokawaTUNABLE_INT("hw.firewire.sbp.use_doorbell", &use_doorbell);
159127468SsimokawaTUNABLE_INT("hw.firewire.sbp.tags", &sbp_tags);
160122387Ssimokawa
161103285Sikob#define NEED_RESPONSE 0
162103285Sikob
163113584Ssimokawa#define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE)
164113584Ssimokawa#ifdef __sparc64__ /* iommu */
165167622Ssimokawa#define SBP_IND_MAX howmany(SBP_MAXPHYS, SBP_SEG_MAX)
166113584Ssimokawa#else
167167622Ssimokawa#define SBP_IND_MAX howmany(SBP_MAXPHYS, PAGE_SIZE)
168113584Ssimokawa#endif
169103285Sikobstruct sbp_ocb {
170103285Sikob	STAILQ_ENTRY(sbp_ocb)	ocb;
171103285Sikob	union ccb	*ccb;
172113584Ssimokawa	bus_addr_t	bus_addr;
173129585Sdfr	uint32_t	orb[8];
174129585Sdfr#define IND_PTR_OFFSET	(8*sizeof(uint32_t))
175120660Ssimokawa	struct ind_ptr  ind_ptr[SBP_IND_MAX];
176103285Sikob	struct sbp_dev	*sdev;
177113584Ssimokawa	int		flags; /* XXX should be removed */
178103285Sikob	bus_dmamap_t	dmamap;
179103285Sikob};
180113584Ssimokawa
181103285Sikob#define OCB_ACT_MGM 0
182103285Sikob#define OCB_ACT_CMD 1
183113584Ssimokawa#define OCB_MATCH(o,s)	((o)->bus_addr == ntohl((s)->orb_lo))
184103285Sikob
185103285Sikobstruct sbp_dev{
186103285Sikob#define SBP_DEV_RESET		0	/* accept login */
187114732Ssimokawa#define SBP_DEV_LOGIN		1	/* to login */
188110336Ssimokawa#if 0
189103285Sikob#define SBP_DEV_RECONN		2	/* to reconnect */
190110336Ssimokawa#endif
191103285Sikob#define SBP_DEV_TOATTACH	3	/* to attach */
192103285Sikob#define SBP_DEV_PROBE		4	/* scan lun */
193103285Sikob#define SBP_DEV_ATTACHED	5	/* in operation */
194103285Sikob#define SBP_DEV_DEAD		6	/* unavailable unit */
195103285Sikob#define SBP_DEV_RETRY		7	/* unavailable unit */
196129585Sdfr	uint8_t status:4,
197114732Ssimokawa		 timeout:4;
198129585Sdfr	uint8_t type;
199129585Sdfr	uint16_t lun_id;
200129585Sdfr	uint16_t freeze;
201121185Ssimokawa#define	ORB_LINK_DEAD		(1 << 0)
202121185Ssimokawa#define	VALID_LUN		(1 << 1)
203121185Ssimokawa#define	ORB_POINTER_ACTIVE	(1 << 2)
204121185Ssimokawa#define	ORB_POINTER_NEED	(1 << 3)
205127468Ssimokawa#define	ORB_DOORBELL_ACTIVE	(1 << 4)
206127468Ssimokawa#define	ORB_DOORBELL_NEED	(1 << 5)
207127468Ssimokawa#define	ORB_SHORTAGE		(1 << 6)
208129585Sdfr	uint16_t flags;
209103285Sikob	struct cam_path *path;
210103285Sikob	struct sbp_target *target;
211113584Ssimokawa	struct fwdma_alloc dma;
212113584Ssimokawa	struct sbp_login_res *login;
213111615Ssimokawa	struct callout login_callout;
214113584Ssimokawa	struct sbp_ocb *ocb;
215103285Sikob	STAILQ_HEAD(, sbp_ocb) ocbs;
216113584Ssimokawa	STAILQ_HEAD(, sbp_ocb) free_ocbs;
217127468Ssimokawa	struct sbp_ocb *last_ocb;
218103285Sikob	char vendor[32];
219103285Sikob	char product[32];
220103285Sikob	char revision[10];
221103285Sikob};
222103285Sikob
223103285Sikobstruct sbp_target {
224103285Sikob	int target_id;
225103285Sikob	int num_lun;
226121185Ssimokawa	struct sbp_dev	**luns;
227103285Sikob	struct sbp_softc *sbp;
228103285Sikob	struct fw_device *fwdev;
229129585Sdfr	uint32_t mgm_hi, mgm_lo;
230111615Ssimokawa	struct sbp_ocb *mgm_ocb_cur;
231111615Ssimokawa	STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue;
232111615Ssimokawa	struct callout mgm_ocb_timeout;
233111615Ssimokawa	struct callout scan_callout;
234113584Ssimokawa	STAILQ_HEAD(, fw_xfer) xferlist;
235113584Ssimokawa	int n_xfer;
236103285Sikob};
237103285Sikob
238103285Sikobstruct sbp_softc {
239103285Sikob	struct firewire_dev_comm fd;
240103285Sikob	struct cam_sim  *sim;
241111615Ssimokawa	struct cam_path  *path;
242103285Sikob	struct sbp_target targets[SBP_NUM_TARGETS];
243103285Sikob	struct fw_bind fwb;
244103285Sikob	bus_dma_tag_t	dmat;
245122387Ssimokawa	struct timeval last_busreset;
246124145Ssimokawa#define SIMQ_FREEZED 1
247124145Ssimokawa	int flags;
248103285Sikob};
249122387Ssimokawa
250124169Ssimokawastatic void sbp_post_explore (void *);
251124169Ssimokawastatic void sbp_recv (struct fw_xfer *);
252124169Ssimokawastatic void sbp_mgm_callback (struct fw_xfer *);
253121185Ssimokawa#if 0
254124169Ssimokawastatic void sbp_cmd_callback (struct fw_xfer *);
255121185Ssimokawa#endif
256124169Ssimokawastatic void sbp_orb_pointer (struct sbp_dev *, struct sbp_ocb *);
257127468Ssimokawastatic void sbp_doorbell(struct sbp_dev *);
258124169Ssimokawastatic void sbp_execute_ocb (void *,  bus_dma_segment_t *, int, int);
259124169Ssimokawastatic void sbp_free_ocb (struct sbp_dev *, struct sbp_ocb *);
260124169Ssimokawastatic void sbp_abort_ocb (struct sbp_ocb *, int);
261124169Ssimokawastatic void sbp_abort_all_ocbs (struct sbp_dev *, int);
262124169Ssimokawastatic struct fw_xfer * sbp_write_cmd (struct sbp_dev *, int, int);
263124169Ssimokawastatic struct sbp_ocb * sbp_get_ocb (struct sbp_dev *);
264124169Ssimokawastatic struct sbp_ocb * sbp_enqueue_ocb (struct sbp_dev *, struct sbp_ocb *);
265124169Ssimokawastatic struct sbp_ocb * sbp_dequeue_ocb (struct sbp_dev *, struct sbp_status *);
266121185Ssimokawastatic void sbp_cam_detach_sdev(struct sbp_dev *);
267121185Ssimokawastatic void sbp_free_sdev(struct sbp_dev *);
268124169Ssimokawastatic void sbp_cam_detach_target (struct sbp_target *);
269124169Ssimokawastatic void sbp_free_target (struct sbp_target *);
270124169Ssimokawastatic void sbp_mgm_timeout (void *arg);
271124169Ssimokawastatic void sbp_timeout (void *arg);
272124169Ssimokawastatic void sbp_mgm_orb (struct sbp_dev *, int, struct sbp_ocb *);
273103285Sikob
274108281SsimokawaMALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire");
275103285Sikob
276103285Sikob/* cam related functions */
277103285Sikobstatic void	sbp_action(struct cam_sim *sim, union ccb *ccb);
278103285Sikobstatic void	sbp_poll(struct cam_sim *sim);
279111615Ssimokawastatic void	sbp_cam_scan_lun(struct cam_periph *, union ccb *);
280111615Ssimokawastatic void	sbp_cam_scan_target(void *arg);
281103285Sikob
282103285Sikobstatic char *orb_status0[] = {
283103285Sikob	/* 0 */ "No additional information to report",
284103285Sikob	/* 1 */ "Request type not supported",
285103285Sikob	/* 2 */ "Speed not supported",
286103285Sikob	/* 3 */ "Page size not supported",
287103285Sikob	/* 4 */ "Access denied",
288103285Sikob	/* 5 */ "Logical unit not supported",
289103285Sikob	/* 6 */ "Maximum payload too small",
290103285Sikob	/* 7 */ "Reserved for future standardization",
291103285Sikob	/* 8 */ "Resources unavailable",
292103285Sikob	/* 9 */ "Function rejected",
293103285Sikob	/* A */ "Login ID not recognized",
294103285Sikob	/* B */ "Dummy ORB completed",
295103285Sikob	/* C */ "Request aborted",
296103285Sikob	/* FF */ "Unspecified error"
297103285Sikob#define MAX_ORB_STATUS0 0xd
298103285Sikob};
299103285Sikob
300103285Sikobstatic char *orb_status1_object[] = {
301103285Sikob	/* 0 */ "Operation request block (ORB)",
302103285Sikob	/* 1 */ "Data buffer",
303103285Sikob	/* 2 */ "Page table",
304103285Sikob	/* 3 */ "Unable to specify"
305103285Sikob};
306103285Sikob
307103285Sikobstatic char *orb_status1_serial_bus_error[] = {
308103285Sikob	/* 0 */ "Missing acknowledge",
309103285Sikob	/* 1 */ "Reserved; not to be used",
310103285Sikob	/* 2 */ "Time-out error",
311103285Sikob	/* 3 */ "Reserved; not to be used",
312103285Sikob	/* 4 */ "Busy retry limit exceeded(X)",
313103285Sikob	/* 5 */ "Busy retry limit exceeded(A)",
314103285Sikob	/* 6 */ "Busy retry limit exceeded(B)",
315103285Sikob	/* 7 */ "Reserved for future standardization",
316103285Sikob	/* 8 */ "Reserved for future standardization",
317103285Sikob	/* 9 */ "Reserved for future standardization",
318103285Sikob	/* A */ "Reserved for future standardization",
319103285Sikob	/* B */ "Tardy retry limit exceeded",
320103285Sikob	/* C */ "Conflict error",
321103285Sikob	/* D */ "Data error",
322103285Sikob	/* E */ "Type error",
323103285Sikob	/* F */ "Address error"
324103285Sikob};
325103285Sikob
326103285Sikobstatic void
327103285Sikobsbp_identify(driver_t *driver, device_t parent)
328103285Sikob{
329103285Sikob	device_t child;
330103285SikobSBP_DEBUG(0)
331103285Sikob	printf("sbp_identify\n");
332103285SikobEND_DEBUG
333103285Sikob
334103285Sikob	child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent));
335103285Sikob}
336103285Sikob
337103285Sikob/*
338103285Sikob * sbp_probe()
339103285Sikob */
340103285Sikobstatic int
341103285Sikobsbp_probe(device_t dev)
342103285Sikob{
343103285Sikob	device_t pa;
344103285Sikob
345103285SikobSBP_DEBUG(0)
346103285Sikob	printf("sbp_probe\n");
347103285SikobEND_DEBUG
348103285Sikob
349103285Sikob	pa = device_get_parent(dev);
350103285Sikob	if(device_get_unit(dev) != device_get_unit(pa)){
351103285Sikob		return(ENXIO);
352103285Sikob	}
353103285Sikob
354120660Ssimokawa	device_set_desc(dev, "SBP-2/SCSI over FireWire");
355111615Ssimokawa
356132432Ssimokawa#if 0
357111615Ssimokawa	if (bootverbose)
358111615Ssimokawa		debug = bootverbose;
359132432Ssimokawa#endif
360132432Ssimokawa
361103285Sikob	return (0);
362103285Sikob}
363103285Sikob
364103285Sikobstatic void
365103285Sikobsbp_show_sdev_info(struct sbp_dev *sdev, int new)
366103285Sikob{
367103285Sikob	struct fw_device *fwdev;
368103285Sikob
369103285Sikob	printf("%s:%d:%d ",
370103285Sikob		device_get_nameunit(sdev->target->sbp->fd.dev),
371103285Sikob		sdev->target->target_id,
372103285Sikob		sdev->lun_id
373103285Sikob	);
374103285Sikob	if (new == 2) {
375103285Sikob		return;
376103285Sikob	}
377103285Sikob	fwdev = sdev->target->fwdev;
378103285Sikob	printf("ordered:%d type:%d EUI:%08x%08x node:%d "
379103285Sikob		"speed:%d maxrec:%d",
380108503Ssimokawa		(sdev->type & 0x40) >> 6,
381108503Ssimokawa		(sdev->type & 0x1f),
382103285Sikob		fwdev->eui.hi,
383103285Sikob		fwdev->eui.lo,
384103285Sikob		fwdev->dst,
385103285Sikob		fwdev->speed,
386103285Sikob		fwdev->maxrec
387103285Sikob	);
388103285Sikob	if (new)
389103285Sikob		printf(" new!\n");
390103285Sikob	else
391103285Sikob		printf("\n");
392103285Sikob	sbp_show_sdev_info(sdev, 2);
393103285Sikob	printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision);
394103285Sikob}
395103285Sikob
396110184Ssimokawastatic struct {
397110184Ssimokawa	int bus;
398110184Ssimokawa	int target;
399110184Ssimokawa	struct fw_eui64 eui;
400110184Ssimokawa} wired[] = {
401110184Ssimokawa	/* Bus	Target	EUI64 */
402110184Ssimokawa#if 0
403110184Ssimokawa	{0,	2,	{0x00018ea0, 0x01fd0154}},	/* Logitec HDD */
404110184Ssimokawa	{0,	0,	{0x00018ea6, 0x00100682}},	/* Logitec DVD */
405110184Ssimokawa	{0,	1,	{0x00d03200, 0xa412006a}},	/* Yano HDD */
406110184Ssimokawa#endif
407110184Ssimokawa	{-1,	-1,	{0,0}}
408110184Ssimokawa};
409110184Ssimokawa
410110184Ssimokawastatic int
411110184Ssimokawasbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev)
412110184Ssimokawa{
413110184Ssimokawa	int bus, i, target=-1;
414110184Ssimokawa	char w[SBP_NUM_TARGETS];
415110184Ssimokawa
416110184Ssimokawa	bzero(w, sizeof(w));
417110184Ssimokawa	bus = device_get_unit(sbp->fd.dev);
418110184Ssimokawa
419110184Ssimokawa	/* XXX wired-down configuration should be gotten from
420110184Ssimokawa					tunable or device hint */
421110184Ssimokawa	for (i = 0; wired[i].bus >= 0; i ++) {
422110184Ssimokawa		if (wired[i].bus == bus) {
423110184Ssimokawa			w[wired[i].target] = 1;
424110184Ssimokawa			if (wired[i].eui.hi == fwdev->eui.hi &&
425110184Ssimokawa					wired[i].eui.lo == fwdev->eui.lo)
426110184Ssimokawa				target = wired[i].target;
427110184Ssimokawa		}
428110184Ssimokawa	}
429110184Ssimokawa	if (target >= 0) {
430110184Ssimokawa		if(target < SBP_NUM_TARGETS &&
431110184Ssimokawa				sbp->targets[target].fwdev == NULL)
432110184Ssimokawa			return(target);
433110184Ssimokawa		device_printf(sbp->fd.dev,
434110184Ssimokawa			"target %d is not free for %08x:%08x\n",
435110184Ssimokawa			target, fwdev->eui.hi, fwdev->eui.lo);
436110184Ssimokawa		target = -1;
437110184Ssimokawa	}
438110184Ssimokawa	/* non-wired target */
439110184Ssimokawa	for (i = 0; i < SBP_NUM_TARGETS; i ++)
440110184Ssimokawa		if (sbp->targets[i].fwdev == NULL && w[i] == 0) {
441110184Ssimokawa			target = i;
442110184Ssimokawa			break;
443110184Ssimokawa		}
444110184Ssimokawa
445110184Ssimokawa	return target;
446110184Ssimokawa}
447110184Ssimokawa
448121185Ssimokawastatic void
449121185Ssimokawasbp_alloc_lun(struct sbp_target *target)
450103285Sikob{
451108503Ssimokawa	struct crom_context cc;
452108503Ssimokawa	struct csrreg *reg;
453121185Ssimokawa	struct sbp_dev *sdev, **newluns;
454121185Ssimokawa	struct sbp_softc *sbp;
455121185Ssimokawa	int maxlun, lun, i;
456103285Sikob
457121185Ssimokawa	sbp = target->sbp;
458114069Ssimokawa	crom_init_context(&cc, target->fwdev->csrrom);
459108503Ssimokawa	/* XXX shoud parse appropriate unit directories only */
460108503Ssimokawa	maxlun = -1;
461108503Ssimokawa	while (cc.depth >= 0) {
462108503Ssimokawa		reg = crom_search_key(&cc, CROM_LUN);
463108503Ssimokawa		if (reg == NULL)
464108503Ssimokawa			break;
465110839Ssimokawa		lun = reg->val & 0xffff;
466108642SsimokawaSBP_DEBUG(0)
467108642Ssimokawa		printf("target %d lun %d found\n", target->target_id, lun);
468108642SsimokawaEND_DEBUG
469108503Ssimokawa		if (maxlun < lun)
470108503Ssimokawa			maxlun = lun;
471108503Ssimokawa		crom_next(&cc);
472108503Ssimokawa	}
473110839Ssimokawa	if (maxlun < 0)
474121185Ssimokawa		printf("%s:%d no LUN found\n",
475121185Ssimokawa		    device_get_nameunit(target->sbp->fd.dev),
476121185Ssimokawa		    target->target_id);
477121185Ssimokawa
478121185Ssimokawa	maxlun ++;
479110839Ssimokawa	if (maxlun >= SBP_NUM_LUNS)
480110839Ssimokawa		maxlun = SBP_NUM_LUNS;
481121185Ssimokawa
482121185Ssimokawa	/* Invalidiate stale devices */
483121185Ssimokawa	for (lun = 0; lun < target->num_lun; lun ++) {
484121185Ssimokawa		sdev = target->luns[lun];
485121185Ssimokawa		if (sdev == NULL)
486121185Ssimokawa			continue;
487121185Ssimokawa		sdev->flags &= ~VALID_LUN;
488121185Ssimokawa		if (lun >= maxlun) {
489121185Ssimokawa			/* lost device */
490121185Ssimokawa			sbp_cam_detach_sdev(sdev);
491121185Ssimokawa			sbp_free_sdev(sdev);
492121185Ssimokawa		}
493103285Sikob	}
494121185Ssimokawa
495121185Ssimokawa	/* Reallocate */
496121185Ssimokawa	if (maxlun != target->num_lun) {
497121185Ssimokawa		newluns = (struct sbp_dev **) realloc(target->luns,
498121185Ssimokawa		    sizeof(struct sbp_dev *) * maxlun,
499121185Ssimokawa		    M_SBP, M_NOWAIT | M_ZERO);
500121185Ssimokawa
501121185Ssimokawa		if (newluns == NULL) {
502127468Ssimokawa			printf("%s: realloc failed\n", __func__);
503121185Ssimokawa			newluns = target->luns;
504121185Ssimokawa			maxlun = target->num_lun;
505121185Ssimokawa		}
506121185Ssimokawa
507121185Ssimokawa		/*
508121185Ssimokawa		 * We must zero the extended region for the case
509121185Ssimokawa		 * realloc() doesn't allocate new buffer.
510121185Ssimokawa		 */
511121185Ssimokawa		if (maxlun > target->num_lun)
512121185Ssimokawa			bzero(&newluns[target->num_lun],
513121185Ssimokawa			    sizeof(struct sbp_dev *) *
514121185Ssimokawa			    (maxlun - target->num_lun));
515121185Ssimokawa
516121185Ssimokawa		target->luns = newluns;
517121185Ssimokawa		target->num_lun = maxlun;
518121185Ssimokawa	}
519121185Ssimokawa
520108503Ssimokawa	crom_init_context(&cc, target->fwdev->csrrom);
521108503Ssimokawa	while (cc.depth >= 0) {
522121185Ssimokawa		int new = 0;
523121185Ssimokawa
524108503Ssimokawa		reg = crom_search_key(&cc, CROM_LUN);
525108503Ssimokawa		if (reg == NULL)
526108503Ssimokawa			break;
527110839Ssimokawa		lun = reg->val & 0xffff;
528110839Ssimokawa		if (lun >= SBP_NUM_LUNS) {
529110839Ssimokawa			printf("too large lun %d\n", lun);
530121185Ssimokawa			goto next;
531110839Ssimokawa		}
532121185Ssimokawa
533121185Ssimokawa		sdev = target->luns[lun];
534121185Ssimokawa		if (sdev == NULL) {
535121185Ssimokawa			sdev = malloc(sizeof(struct sbp_dev),
536121185Ssimokawa			    M_SBP, M_NOWAIT | M_ZERO);
537121185Ssimokawa			if (sdev == NULL) {
538127468Ssimokawa				printf("%s: malloc failed\n", __func__);
539121185Ssimokawa				goto next;
540121185Ssimokawa			}
541121185Ssimokawa			target->luns[lun] = sdev;
542121185Ssimokawa			sdev->lun_id = lun;
543121185Ssimokawa			sdev->target = target;
544121185Ssimokawa			STAILQ_INIT(&sdev->ocbs);
545121185Ssimokawa			CALLOUT_INIT(&sdev->login_callout);
546121185Ssimokawa			sdev->status = SBP_DEV_RESET;
547121185Ssimokawa			new = 1;
548121185Ssimokawa		}
549121185Ssimokawa		sdev->flags |= VALID_LUN;
550119196Ssimokawa		sdev->type = (reg->val & 0xff0000) >> 16;
551113584Ssimokawa
552121185Ssimokawa		if (new == 0)
553121185Ssimokawa			goto next;
554121185Ssimokawa
555113584Ssimokawa		fwdma_malloc(sbp->fd.fc,
556129585Sdfr			/* alignment */ sizeof(uint32_t),
557113584Ssimokawa			SBP_DMA_SIZE, &sdev->dma, BUS_DMA_NOWAIT);
558113584Ssimokawa		if (sdev->dma.v_addr == NULL) {
559113584Ssimokawa			printf("%s: dma space allocation failed\n",
560127468Ssimokawa							__func__);
561121185Ssimokawa			free(sdev, M_SBP);
562121185Ssimokawa			target->luns[lun] = NULL;
563121185Ssimokawa			goto next;
564113584Ssimokawa		}
565113584Ssimokawa		sdev->login = (struct sbp_login_res *) sdev->dma.v_addr;
566113584Ssimokawa		sdev->ocb = (struct sbp_ocb *)
567113584Ssimokawa				((char *)sdev->dma.v_addr + SBP_LOGIN_SIZE);
568113584Ssimokawa		bzero((char *)sdev->ocb,
569113584Ssimokawa			sizeof (struct sbp_ocb) * SBP_QUEUE_LEN);
570113584Ssimokawa
571113584Ssimokawa		STAILQ_INIT(&sdev->free_ocbs);
572113584Ssimokawa		for (i = 0; i < SBP_QUEUE_LEN; i++) {
573113584Ssimokawa			struct sbp_ocb *ocb;
574113584Ssimokawa			ocb = &sdev->ocb[i];
575113584Ssimokawa			ocb->bus_addr = sdev->dma.bus_addr
576113584Ssimokawa				+ SBP_LOGIN_SIZE
577113584Ssimokawa				+ sizeof(struct sbp_ocb) * i
578113584Ssimokawa				+ offsetof(struct sbp_ocb, orb[0]);
579113584Ssimokawa			if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
580113584Ssimokawa				printf("sbp_attach: cannot create dmamap\n");
581121185Ssimokawa				/* XXX */
582121185Ssimokawa				goto next;
583113584Ssimokawa			}
584113584Ssimokawa			sbp_free_ocb(sdev, ocb);
585113584Ssimokawa		}
586121185Ssimokawanext:
587108503Ssimokawa		crom_next(&cc);
588103285Sikob	}
589121185Ssimokawa
590121185Ssimokawa	for (lun = 0; lun < target->num_lun; lun ++) {
591121185Ssimokawa		sdev = target->luns[lun];
592121185Ssimokawa		if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) {
593121185Ssimokawa			sbp_cam_detach_sdev(sdev);
594121185Ssimokawa			sbp_free_sdev(sdev);
595121185Ssimokawa			target->luns[lun] = NULL;
596121185Ssimokawa		}
597121185Ssimokawa	}
598121185Ssimokawa}
599121185Ssimokawa
600121185Ssimokawastatic struct sbp_target *
601121185Ssimokawasbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
602121185Ssimokawa{
603121185Ssimokawa	int i;
604121185Ssimokawa	struct sbp_target *target;
605121185Ssimokawa	struct crom_context cc;
606121185Ssimokawa	struct csrreg *reg;
607121185Ssimokawa
608121185SsimokawaSBP_DEBUG(1)
609121185Ssimokawa	printf("sbp_alloc_target\n");
610121185SsimokawaEND_DEBUG
611121185Ssimokawa	i = sbp_new_target(sbp, fwdev);
612121185Ssimokawa	if (i < 0) {
613121185Ssimokawa		device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n");
614121185Ssimokawa		return NULL;
615121185Ssimokawa	}
616121185Ssimokawa	/* new target */
617121185Ssimokawa	target = &sbp->targets[i];
618121185Ssimokawa	target->sbp = sbp;
619121185Ssimokawa	target->fwdev = fwdev;
620121185Ssimokawa	target->target_id = i;
621121185Ssimokawa	/* XXX we may want to reload mgm port after each bus reset */
622121185Ssimokawa	/* XXX there might be multiple management agents */
623121185Ssimokawa	crom_init_context(&cc, target->fwdev->csrrom);
624121185Ssimokawa	reg = crom_search_key(&cc, CROM_MGM);
625121185Ssimokawa	if (reg == NULL || reg->val == 0) {
626121185Ssimokawa		printf("NULL management address\n");
627121185Ssimokawa		target->fwdev = NULL;
628121185Ssimokawa		return NULL;
629121185Ssimokawa	}
630121185Ssimokawa	target->mgm_hi = 0xffff;
631121185Ssimokawa	target->mgm_lo = 0xf0000000 | (reg->val << 2);
632121185Ssimokawa	target->mgm_ocb_cur = NULL;
633121185SsimokawaSBP_DEBUG(1)
634121185Ssimokawa	printf("target:%d mgm_port: %x\n", i, target->mgm_lo);
635121185SsimokawaEND_DEBUG
636121185Ssimokawa	STAILQ_INIT(&target->xferlist);
637121185Ssimokawa	target->n_xfer = 0;
638121185Ssimokawa	STAILQ_INIT(&target->mgm_ocb_queue);
639121185Ssimokawa	CALLOUT_INIT(&target->mgm_ocb_timeout);
640121185Ssimokawa	CALLOUT_INIT(&target->scan_callout);
641121185Ssimokawa
642121185Ssimokawa	target->luns = NULL;
643121185Ssimokawa	target->num_lun = 0;
644114069Ssimokawa	return target;
645103285Sikob}
646103285Sikob
647103285Sikobstatic void
648103285Sikobsbp_probe_lun(struct sbp_dev *sdev)
649103285Sikob{
650103285Sikob	struct fw_device *fwdev;
651114069Ssimokawa	struct crom_context c, *cc = &c;
652114069Ssimokawa	struct csrreg *reg;
653103285Sikob
654103285Sikob	bzero(sdev->vendor, sizeof(sdev->vendor));
655103285Sikob	bzero(sdev->product, sizeof(sdev->product));
656114069Ssimokawa
657114069Ssimokawa	fwdev = sdev->target->fwdev;
658114069Ssimokawa	crom_init_context(cc, fwdev->csrrom);
659114069Ssimokawa	/* get vendor string */
660114069Ssimokawa	crom_search_key(cc, CSRKEY_VENDOR);
661114069Ssimokawa	crom_next(cc);
662114069Ssimokawa	crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor));
663114223Ssimokawa	/* skip to the unit directory for SBP-2 */
664114260Ssimokawa	while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) {
665114223Ssimokawa		if (reg->val == CSRVAL_T10SBP2)
666114223Ssimokawa			break;
667114260Ssimokawa		crom_next(cc);
668114260Ssimokawa	}
669114069Ssimokawa	/* get firmware revision */
670114069Ssimokawa	reg = crom_search_key(cc, CSRKEY_FIRM_VER);
671114069Ssimokawa	if (reg != NULL)
672114069Ssimokawa		snprintf(sdev->revision, sizeof(sdev->revision),
673114069Ssimokawa						"%06x", reg->val);
674114069Ssimokawa	/* get product string */
675114069Ssimokawa	crom_search_key(cc, CSRKEY_MODEL);
676114069Ssimokawa	crom_next(cc);
677114069Ssimokawa	crom_parse_text(cc, sdev->product, sizeof(sdev->product));
678103285Sikob}
679111615Ssimokawa
680103285Sikobstatic void
681111615Ssimokawasbp_login_callout(void *arg)
682103285Sikob{
683111615Ssimokawa	struct sbp_dev *sdev = (struct sbp_dev *)arg;
684111615Ssimokawa	sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
685111615Ssimokawa}
686111615Ssimokawa
687122387Ssimokawastatic void
688122387Ssimokawasbp_login(struct sbp_dev *sdev)
689122387Ssimokawa{
690122387Ssimokawa	struct timeval delta;
691122387Ssimokawa	struct timeval t;
692122387Ssimokawa	int ticks = 0;
693122387Ssimokawa
694122387Ssimokawa	microtime(&delta);
695122387Ssimokawa	timevalsub(&delta, &sdev->target->sbp->last_busreset);
696122387Ssimokawa	t.tv_sec = login_delay / 1000;
697122387Ssimokawa	t.tv_usec = (login_delay % 1000) * 1000;
698122387Ssimokawa	timevalsub(&t, &delta);
699122387Ssimokawa	if (t.tv_sec >= 0 && t.tv_usec > 0)
700122387Ssimokawa		ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000;
701122387SsimokawaSBP_DEBUG(0)
702153706Strhodes	printf("%s: sec = %jd usec = %ld ticks = %d\n", __func__,
703153706Strhodes	    (intmax_t)t.tv_sec, t.tv_usec, ticks);
704122387SsimokawaEND_DEBUG
705122387Ssimokawa	callout_reset(&sdev->login_callout, ticks,
706122387Ssimokawa			sbp_login_callout, (void *)(sdev));
707122387Ssimokawa}
708122387Ssimokawa
709114069Ssimokawa#define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
710114069Ssimokawa	&& crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
711111615Ssimokawa
712111615Ssimokawastatic void
713111615Ssimokawasbp_probe_target(void *arg)
714111615Ssimokawa{
715111615Ssimokawa	struct sbp_target *target = (struct sbp_target *)arg;
716103285Sikob	struct sbp_softc *sbp;
717103285Sikob	struct sbp_dev *sdev;
718103285Sikob	struct firewire_comm *fc;
719111615Ssimokawa	int i, alive;
720103285Sikob
721111615Ssimokawa	alive = SBP_FWDEV_ALIVE(target->fwdev);
722103285SikobSBP_DEBUG(1)
723103285Sikob	printf("sbp_probe_target %d\n", target->target_id);
724103285Sikob	if (!alive)
725103285Sikob		printf("not alive\n");
726103285SikobEND_DEBUG
727103285Sikob
728103285Sikob	sbp = target->sbp;
729103285Sikob	fc = target->sbp->fd.fc;
730121185Ssimokawa	sbp_alloc_lun(target);
731121185Ssimokawa
732111615Ssimokawa	/* XXX untimeout mgm_ocb and dequeue */
733103285Sikob	for (i=0; i < target->num_lun; i++) {
734121185Ssimokawa		sdev = target->luns[i];
735121185Ssimokawa		if (sdev == NULL)
736121185Ssimokawa			continue;
737103285Sikob		if (alive && (sdev->status != SBP_DEV_DEAD)) {
738103285Sikob			if (sdev->path != NULL) {
739103285Sikob				xpt_freeze_devq(sdev->path, 1);
740111615Ssimokawa				sdev->freeze ++;
741103285Sikob			}
742111615Ssimokawa			sbp_probe_lun(sdev);
743111615SsimokawaSBP_DEBUG(0)
744111615Ssimokawa			sbp_show_sdev_info(sdev,
745111615Ssimokawa					(sdev->status == SBP_DEV_RESET));
746111615SsimokawaEND_DEBUG
747111615Ssimokawa
748111615Ssimokawa			sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
749103285Sikob			switch (sdev->status) {
750110336Ssimokawa			case SBP_DEV_RESET:
751103285Sikob				/* new or revived target */
752113584Ssimokawa				if (auto_login)
753111615Ssimokawa					sbp_login(sdev);
754103285Sikob				break;
755111615Ssimokawa			case SBP_DEV_TOATTACH:
756111615Ssimokawa			case SBP_DEV_PROBE:
757111615Ssimokawa			case SBP_DEV_ATTACHED:
758110336Ssimokawa			case SBP_DEV_RETRY:
759110336Ssimokawa			default:
760111615Ssimokawa				sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
761110336Ssimokawa				break;
762103285Sikob			}
763103285Sikob		} else {
764103285Sikob			switch (sdev->status) {
765103285Sikob			case SBP_DEV_ATTACHED:
766103285SikobSBP_DEBUG(0)
767103285Sikob				/* the device has gone */
768103285Sikob				sbp_show_sdev_info(sdev, 2);
769103285Sikob				printf("lost target\n");
770103285SikobEND_DEBUG
771111615Ssimokawa				if (sdev->path) {
772103285Sikob					xpt_freeze_devq(sdev->path, 1);
773111615Ssimokawa					sdev->freeze ++;
774111615Ssimokawa				}
775103285Sikob				sdev->status = SBP_DEV_RETRY;
776111615Ssimokawa				sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
777103285Sikob				break;
778103285Sikob			case SBP_DEV_PROBE:
779103285Sikob			case SBP_DEV_TOATTACH:
780103285Sikob				sdev->status = SBP_DEV_RESET;
781103285Sikob				break;
782103285Sikob			case SBP_DEV_RETRY:
783103285Sikob			case SBP_DEV_RESET:
784103285Sikob			case SBP_DEV_DEAD:
785103285Sikob				break;
786103285Sikob			}
787103285Sikob		}
788103285Sikob	}
789103285Sikob}
790103285Sikob
791103285Sikobstatic void
792113584Ssimokawasbp_post_busreset(void *arg)
793113584Ssimokawa{
794113584Ssimokawa	struct sbp_softc *sbp;
795113584Ssimokawa
796113584Ssimokawa	sbp = (struct sbp_softc *)arg;
797113584SsimokawaSBP_DEBUG(0)
798113584Ssimokawa	printf("sbp_post_busreset\n");
799113584SsimokawaEND_DEBUG
800124145Ssimokawa	if ((sbp->sim->flags & SIMQ_FREEZED) == 0) {
801124145Ssimokawa		xpt_freeze_simq(sbp->sim, /*count*/1);
802124145Ssimokawa		sbp->sim->flags |= SIMQ_FREEZED;
803124145Ssimokawa	}
804122387Ssimokawa	microtime(&sbp->last_busreset);
805113584Ssimokawa}
806113584Ssimokawa
807113584Ssimokawastatic void
808103285Sikobsbp_post_explore(void *arg)
809103285Sikob{
810103285Sikob	struct sbp_softc *sbp = (struct sbp_softc *)arg;
811103285Sikob	struct sbp_target *target;
812103285Sikob	struct fw_device *fwdev;
813103285Sikob	int i, alive;
814103285Sikob
815111199SsimokawaSBP_DEBUG(0)
816111199Ssimokawa	printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
817103285SikobEND_DEBUG
818122387Ssimokawa	if (sbp_cold > 0)
819122387Ssimokawa		sbp_cold --;
820122387Ssimokawa
821122387Ssimokawa#if 0
822122387Ssimokawa	/*
823122387Ssimokawa	 * XXX don't let CAM the bus rest.
824122387Ssimokawa	 * CAM tries to do something with freezed (DEV_RETRY) devices.
825111615Ssimokawa	 */
826111615Ssimokawa	xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
827103285Sikob#endif
828111615Ssimokawa
829130532Sdfr	/* Garbage Collection */
830103285Sikob	for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
831103285Sikob		target = &sbp->targets[i];
832110193Ssimokawa		STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
833110193Ssimokawa			if (target->fwdev == NULL || target->fwdev == fwdev)
834110193Ssimokawa				break;
835121185Ssimokawa		if (fwdev == NULL) {
836103285Sikob			/* device has removed in lower driver */
837110145Ssimokawa			sbp_cam_detach_target(target);
838121185Ssimokawa			sbp_free_target(target);
839103285Sikob		}
840103285Sikob	}
841103285Sikob	/* traverse device list */
842110193Ssimokawa	STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
843103285SikobSBP_DEBUG(0)
844103285Sikob		printf("sbp_post_explore: EUI:%08x%08x ",
845103285Sikob				fwdev->eui.hi, fwdev->eui.lo);
846114069Ssimokawa		if (fwdev->status != FWDEVATTACHED)
847103285Sikob			printf("not attached, state=%d.\n", fwdev->status);
848114069Ssimokawa		else
849114069Ssimokawa			printf("attached\n");
850103285SikobEND_DEBUG
851111615Ssimokawa		alive = SBP_FWDEV_ALIVE(fwdev);
852103285Sikob		for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
853103285Sikob			target = &sbp->targets[i];
854103285Sikob			if(target->fwdev == fwdev ) {
855103285Sikob				/* known target */
856103285Sikob				break;
857103285Sikob			}
858103285Sikob		}
859103285Sikob		if(i == SBP_NUM_TARGETS){
860103285Sikob			if (alive) {
861103285Sikob				/* new target */
862103285Sikob				target = sbp_alloc_target(sbp, fwdev);
863103285Sikob				if (target == NULL)
864103285Sikob					continue;
865103285Sikob			} else {
866103285Sikob				continue;
867103285Sikob			}
868103285Sikob		}
869111615Ssimokawa		sbp_probe_target((void *)target);
870121185Ssimokawa		if (target->num_lun == 0)
871121185Ssimokawa			sbp_free_target(target);
872103285Sikob	}
873124145Ssimokawa	xpt_release_simq(sbp->sim, /*run queue*/TRUE);
874124145Ssimokawa	sbp->sim->flags &= ~SIMQ_FREEZED;
875103285Sikob}
876103285Sikob
877103285Sikob#if NEED_RESPONSE
878103285Sikobstatic void
879103285Sikobsbp_loginres_callback(struct fw_xfer *xfer){
880113584Ssimokawa	int s;
881103285Sikob	struct sbp_dev *sdev;
882103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
883113584SsimokawaSBP_DEBUG(1)
884103285Sikob	sbp_show_sdev_info(sdev, 2);
885103285Sikob	printf("sbp_loginres_callback\n");
886103285SikobEND_DEBUG
887113584Ssimokawa	/* recycle */
888113584Ssimokawa	s = splfw();
889113584Ssimokawa	STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link);
890113584Ssimokawa	splx(s);
891103285Sikob	return;
892103285Sikob}
893103285Sikob#endif
894103285Sikob
895113584Ssimokawastatic __inline void
896113584Ssimokawasbp_xfer_free(struct fw_xfer *xfer)
897113584Ssimokawa{
898113584Ssimokawa	struct sbp_dev *sdev;
899113584Ssimokawa	int s;
900113584Ssimokawa
901113584Ssimokawa	sdev = (struct sbp_dev *)xfer->sc;
902113584Ssimokawa	fw_xfer_unload(xfer);
903113584Ssimokawa	s = splfw();
904113584Ssimokawa	STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
905113584Ssimokawa	splx(s);
906113584Ssimokawa}
907113584Ssimokawa
908103285Sikobstatic void
909114732Ssimokawasbp_reset_start_callback(struct fw_xfer *xfer)
910103285Sikob{
911114732Ssimokawa	struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
912114732Ssimokawa	struct sbp_target *target = sdev->target;
913114732Ssimokawa	int i;
914114732Ssimokawa
915114732Ssimokawa	if (xfer->resp != 0) {
916114732Ssimokawa		sbp_show_sdev_info(sdev, 2);
917114732Ssimokawa		printf("sbp_reset_start failed: resp=%d\n", xfer->resp);
918114732Ssimokawa	}
919114732Ssimokawa
920114732Ssimokawa	for (i = 0; i < target->num_lun; i++) {
921121185Ssimokawa		tsdev = target->luns[i];
922121185Ssimokawa		if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN)
923121185Ssimokawa			sbp_login(tsdev);
924114732Ssimokawa	}
925114732Ssimokawa}
926114732Ssimokawa
927114732Ssimokawastatic void
928114732Ssimokawasbp_reset_start(struct sbp_dev *sdev)
929114732Ssimokawa{
930114732Ssimokawa	struct fw_xfer *xfer;
931114732Ssimokawa	struct fw_pkt *fp;
932114732Ssimokawa
933114732SsimokawaSBP_DEBUG(0)
934114732Ssimokawa	sbp_show_sdev_info(sdev, 2);
935114732Ssimokawa	printf("sbp_reset_start\n");
936114732SsimokawaEND_DEBUG
937114732Ssimokawa
938114732Ssimokawa	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
939167632Ssimokawa	xfer->hand = sbp_reset_start_callback;
940120660Ssimokawa	fp = &xfer->send.hdr;
941114732Ssimokawa	fp->mode.wreqq.dest_hi = 0xffff;
942114732Ssimokawa	fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
943114732Ssimokawa	fp->mode.wreqq.data = htonl(0xf);
944114732Ssimokawa	fw_asyreq(xfer->fc, -1, xfer);
945114732Ssimokawa}
946114732Ssimokawa
947114732Ssimokawastatic void
948114732Ssimokawasbp_mgm_callback(struct fw_xfer *xfer)
949114732Ssimokawa{
950103285Sikob	struct sbp_dev *sdev;
951114732Ssimokawa	int resp;
952114732Ssimokawa
953103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
954114732Ssimokawa
955114732SsimokawaSBP_DEBUG(1)
956103285Sikob	sbp_show_sdev_info(sdev, 2);
957114732Ssimokawa	printf("sbp_mgm_callback\n");
958103285SikobEND_DEBUG
959114732Ssimokawa	resp = xfer->resp;
960113584Ssimokawa	sbp_xfer_free(xfer);
961114732Ssimokawa#if 0
962114732Ssimokawa	if (resp != 0) {
963114732Ssimokawa		sbp_show_sdev_info(sdev, 2);
964114732Ssimokawa		printf("management ORB failed(%d) ... RESET_START\n", resp);
965114732Ssimokawa		sbp_reset_start(sdev);
966114732Ssimokawa	}
967114732Ssimokawa#endif
968103285Sikob	return;
969103285Sikob}
970103285Sikob
971111615Ssimokawastatic struct sbp_dev *
972111615Ssimokawasbp_next_dev(struct sbp_target *target, int lun)
973111615Ssimokawa{
974121185Ssimokawa	struct sbp_dev **sdevp;
975111615Ssimokawa	int i;
976111615Ssimokawa
977121185Ssimokawa	for (i = lun, sdevp = &target->luns[lun]; i < target->num_lun;
978121185Ssimokawa	    i++, sdevp++)
979121185Ssimokawa		if (*sdevp != NULL && (*sdevp)->status == SBP_DEV_PROBE)
980121185Ssimokawa			return(*sdevp);
981121185Ssimokawa	return(NULL);
982111615Ssimokawa}
983111615Ssimokawa
984111615Ssimokawa#define SCAN_PRI 1
985103285Sikobstatic void
986111615Ssimokawasbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb)
987103285Sikob{
988111615Ssimokawa	struct sbp_target *target;
989103285Sikob	struct sbp_dev *sdev;
990111615Ssimokawa
991103285Sikob	sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
992111615Ssimokawa	target = sdev->target;
993110269SsimokawaSBP_DEBUG(0)
994103285Sikob	sbp_show_sdev_info(sdev, 2);
995111615Ssimokawa	printf("sbp_cam_scan_lun\n");
996103285SikobEND_DEBUG
997111615Ssimokawa	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
998111615Ssimokawa		sdev->status = SBP_DEV_ATTACHED;
999111615Ssimokawa	} else {
1000111615Ssimokawa		sbp_show_sdev_info(sdev, 2);
1001111615Ssimokawa		printf("scan failed\n");
1002111615Ssimokawa	}
1003111615Ssimokawa	sdev = sbp_next_dev(target, sdev->lun_id + 1);
1004111615Ssimokawa	if (sdev == NULL) {
1005111615Ssimokawa		free(ccb, M_SBP);
1006111615Ssimokawa		return;
1007111615Ssimokawa	}
1008111615Ssimokawa	/* reuse ccb */
1009111615Ssimokawa	xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
1010111615Ssimokawa	ccb->ccb_h.ccb_sdev_ptr = sdev;
1011111615Ssimokawa	xpt_action(ccb);
1012111615Ssimokawa	xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1013111615Ssimokawa	sdev->freeze = 1;
1014103285Sikob}
1015103285Sikob
1016103285Sikobstatic void
1017111615Ssimokawasbp_cam_scan_target(void *arg)
1018103285Sikob{
1019111615Ssimokawa	struct sbp_target *target = (struct sbp_target *)arg;
1020111615Ssimokawa	struct sbp_dev *sdev;
1021110798Ssimokawa	union ccb *ccb;
1022103285Sikob
1023111615Ssimokawa	sdev = sbp_next_dev(target, 0);
1024111615Ssimokawa	if (sdev == NULL) {
1025111615Ssimokawa		printf("sbp_cam_scan_target: nothing to do for target%d\n",
1026111615Ssimokawa							target->target_id);
1027110798Ssimokawa		return;
1028110798Ssimokawa	}
1029103285SikobSBP_DEBUG(0)
1030103285Sikob	sbp_show_sdev_info(sdev, 2);
1031111615Ssimokawa	printf("sbp_cam_scan_target\n");
1032103285SikobEND_DEBUG
1033111615Ssimokawa	ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO);
1034111615Ssimokawa	if (ccb == NULL) {
1035111615Ssimokawa		printf("sbp_cam_scan_target: malloc failed\n");
1036111615Ssimokawa		return;
1037111615Ssimokawa	}
1038111615Ssimokawa	xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
1039103285Sikob	ccb->ccb_h.func_code = XPT_SCAN_LUN;
1040111615Ssimokawa	ccb->ccb_h.cbfcnp = sbp_cam_scan_lun;
1041111615Ssimokawa	ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1042103285Sikob	ccb->crcn.flags = CAM_FLAG_NONE;
1043103285Sikob	ccb->ccb_h.ccb_sdev_ptr = sdev;
1044103285Sikob
1045103285Sikob	/* The scan is in progress now. */
1046111040Ssimokawa	xpt_action(ccb);
1047111615Ssimokawa	xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1048111615Ssimokawa	sdev->freeze = 1;
1049103285Sikob}
1050103285Sikob
1051111615Ssimokawastatic __inline void
1052111615Ssimokawasbp_scan_dev(struct sbp_dev *sdev)
1053111615Ssimokawa{
1054111040Ssimokawa	sdev->status = SBP_DEV_PROBE;
1055122387Ssimokawa	callout_reset(&sdev->target->scan_callout, scan_delay * hz / 1000,
1056111615Ssimokawa			sbp_cam_scan_target, (void *)sdev->target);
1057103285Sikob}
1058103285Sikob
1059103285Sikobstatic void
1060103285Sikobsbp_do_attach(struct fw_xfer *xfer)
1061103285Sikob{
1062103285Sikob	struct sbp_dev *sdev;
1063111615Ssimokawa	struct sbp_target *target;
1064111615Ssimokawa	struct sbp_softc *sbp;
1065103285Sikob
1066103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
1067111615Ssimokawa	target = sdev->target;
1068111615Ssimokawa	sbp = target->sbp;
1069103285SikobSBP_DEBUG(0)
1070103285Sikob	sbp_show_sdev_info(sdev, 2);
1071103285Sikob	printf("sbp_do_attach\n");
1072103285SikobEND_DEBUG
1073113584Ssimokawa	sbp_xfer_free(xfer);
1074111199Ssimokawa
1075103285Sikob	if (sdev->path == NULL)
1076103285Sikob		xpt_create_path(&sdev->path, xpt_periph,
1077111615Ssimokawa			cam_sim_path(target->sbp->sim),
1078111615Ssimokawa			target->target_id, sdev->lun_id);
1079103285Sikob
1080111199Ssimokawa	/*
1081111199Ssimokawa	 * Let CAM scan the bus if we are in the boot process.
1082111199Ssimokawa	 * XXX xpt_scan_bus cannot detect LUN larger than 0
1083111199Ssimokawa	 * if LUN 0 doesn't exists.
1084111199Ssimokawa	 */
1085111199Ssimokawa	if (sbp_cold > 0) {
1086111615Ssimokawa		sdev->status = SBP_DEV_ATTACHED;
1087111199Ssimokawa		return;
1088111199Ssimokawa	}
1089111199Ssimokawa
1090111615Ssimokawa	sbp_scan_dev(sdev);
1091103285Sikob	return;
1092103285Sikob}
1093103285Sikob
1094103285Sikobstatic void
1095103285Sikobsbp_agent_reset_callback(struct fw_xfer *xfer)
1096103285Sikob{
1097103285Sikob	struct sbp_dev *sdev;
1098103285Sikob
1099103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
1100103285SikobSBP_DEBUG(1)
1101103285Sikob	sbp_show_sdev_info(sdev, 2);
1102127468Ssimokawa	printf("%s\n", __func__);
1103103285SikobEND_DEBUG
1104114732Ssimokawa	if (xfer->resp != 0) {
1105114732Ssimokawa		sbp_show_sdev_info(sdev, 2);
1106127468Ssimokawa		printf("%s: resp=%d\n", __func__, xfer->resp);
1107114732Ssimokawa	}
1108114732Ssimokawa
1109113584Ssimokawa	sbp_xfer_free(xfer);
1110111615Ssimokawa	if (sdev->path) {
1111111615Ssimokawa		xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1112111615Ssimokawa		sdev->freeze = 0;
1113111615Ssimokawa	}
1114103285Sikob}
1115103285Sikob
1116103285Sikobstatic void
1117110336Ssimokawasbp_agent_reset(struct sbp_dev *sdev)
1118103285Sikob{
1119103285Sikob	struct fw_xfer *xfer;
1120103285Sikob	struct fw_pkt *fp;
1121103285Sikob
1122103285SikobSBP_DEBUG(0)
1123103285Sikob	sbp_show_sdev_info(sdev, 2);
1124103285Sikob	printf("sbp_agent_reset\n");
1125103285SikobEND_DEBUG
1126103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1127103285Sikob	if (xfer == NULL)
1128103285Sikob		return;
1129115788Ssimokawa	if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE)
1130167632Ssimokawa		xfer->hand = sbp_agent_reset_callback;
1131110336Ssimokawa	else
1132167632Ssimokawa		xfer->hand = sbp_do_attach;
1133120660Ssimokawa	fp = &xfer->send.hdr;
1134103285Sikob	fp->mode.wreqq.data = htonl(0xf);
1135103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1136111615Ssimokawa	sbp_abort_all_ocbs(sdev, CAM_BDR_SENT);
1137103285Sikob}
1138103285Sikob
1139103285Sikobstatic void
1140103285Sikobsbp_busy_timeout_callback(struct fw_xfer *xfer)
1141103285Sikob{
1142103285Sikob	struct sbp_dev *sdev;
1143103285Sikob
1144103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
1145103285SikobSBP_DEBUG(1)
1146103285Sikob	sbp_show_sdev_info(sdev, 2);
1147110336Ssimokawa	printf("sbp_busy_timeout_callback\n");
1148103285SikobEND_DEBUG
1149113584Ssimokawa	sbp_xfer_free(xfer);
1150110336Ssimokawa	sbp_agent_reset(sdev);
1151103285Sikob}
1152103285Sikob
1153103285Sikobstatic void
1154103285Sikobsbp_busy_timeout(struct sbp_dev *sdev)
1155103285Sikob{
1156103285Sikob	struct fw_pkt *fp;
1157103285Sikob	struct fw_xfer *xfer;
1158103285SikobSBP_DEBUG(0)
1159103285Sikob	sbp_show_sdev_info(sdev, 2);
1160103285Sikob	printf("sbp_busy_timeout\n");
1161103285SikobEND_DEBUG
1162103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1163103285Sikob
1164167632Ssimokawa	xfer->hand = sbp_busy_timeout_callback;
1165120660Ssimokawa	fp = &xfer->send.hdr;
1166113584Ssimokawa	fp->mode.wreqq.dest_hi = 0xffff;
1167113584Ssimokawa	fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1168110129Ssimokawa	fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1169103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1170103285Sikob}
1171103285Sikob
1172103285Sikobstatic void
1173121185Ssimokawasbp_orb_pointer_callback(struct fw_xfer *xfer)
1174121185Ssimokawa{
1175121185Ssimokawa	struct sbp_dev *sdev;
1176121185Ssimokawa	sdev = (struct sbp_dev *)xfer->sc;
1177121185Ssimokawa
1178121185SsimokawaSBP_DEBUG(1)
1179121185Ssimokawa	sbp_show_sdev_info(sdev, 2);
1180127468Ssimokawa	printf("%s\n", __func__);
1181121185SsimokawaEND_DEBUG
1182121185Ssimokawa	if (xfer->resp != 0) {
1183121185Ssimokawa		/* XXX */
1184127468Ssimokawa		printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1185121185Ssimokawa	}
1186121185Ssimokawa	sbp_xfer_free(xfer);
1187121185Ssimokawa	sdev->flags &= ~ORB_POINTER_ACTIVE;
1188121185Ssimokawa
1189121185Ssimokawa	if ((sdev->flags & ORB_POINTER_NEED) != 0) {
1190121185Ssimokawa		struct sbp_ocb *ocb;
1191121185Ssimokawa
1192121185Ssimokawa		sdev->flags &= ~ORB_POINTER_NEED;
1193121185Ssimokawa		ocb = STAILQ_FIRST(&sdev->ocbs);
1194121185Ssimokawa		if (ocb != NULL)
1195121185Ssimokawa			sbp_orb_pointer(sdev, ocb);
1196121185Ssimokawa	}
1197121185Ssimokawa	return;
1198121185Ssimokawa}
1199121185Ssimokawa
1200121185Ssimokawastatic void
1201103285Sikobsbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1202103285Sikob{
1203103285Sikob	struct fw_xfer *xfer;
1204103285Sikob	struct fw_pkt *fp;
1205121185SsimokawaSBP_DEBUG(1)
1206103285Sikob	sbp_show_sdev_info(sdev, 2);
1207129585Sdfr	printf("%s: 0x%08x\n", __func__, (uint32_t)ocb->bus_addr);
1208103285SikobEND_DEBUG
1209103285Sikob
1210121185Ssimokawa	if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) {
1211122421SsimokawaSBP_DEBUG(0)
1212127468Ssimokawa		printf("%s: orb pointer active\n", __func__);
1213122421SsimokawaEND_DEBUG
1214121185Ssimokawa		sdev->flags |= ORB_POINTER_NEED;
1215121185Ssimokawa		return;
1216121185Ssimokawa	}
1217121185Ssimokawa
1218121185Ssimokawa	sdev->flags |= ORB_POINTER_ACTIVE;
1219103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1220103285Sikob	if (xfer == NULL)
1221103285Sikob		return;
1222167632Ssimokawa	xfer->hand = sbp_orb_pointer_callback;
1223103285Sikob
1224120660Ssimokawa	fp = &xfer->send.hdr;
1225113584Ssimokawa	fp->mode.wreqb.len = 8;
1226103285Sikob	fp->mode.wreqb.extcode = 0;
1227120660Ssimokawa	xfer->send.payload[0] =
1228103285Sikob		htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1229129585Sdfr	xfer->send.payload[1] = htonl((uint32_t)ocb->bus_addr);
1230103285Sikob
1231103285Sikob	if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1232113584Ssimokawa			sbp_xfer_free(xfer);
1233103285Sikob			ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1234103285Sikob			xpt_done(ocb->ccb);
1235103285Sikob	}
1236103285Sikob}
1237103285Sikob
1238103285Sikobstatic void
1239127468Ssimokawasbp_doorbell_callback(struct fw_xfer *xfer)
1240121185Ssimokawa{
1241121185Ssimokawa	struct sbp_dev *sdev;
1242121185Ssimokawa	sdev = (struct sbp_dev *)xfer->sc;
1243127468Ssimokawa
1244127468SsimokawaSBP_DEBUG(1)
1245121185Ssimokawa	sbp_show_sdev_info(sdev, 2);
1246127468Ssimokawa	printf("sbp_doorbell_callback\n");
1247121185SsimokawaEND_DEBUG
1248121185Ssimokawa	if (xfer->resp != 0) {
1249121185Ssimokawa		/* XXX */
1250127468Ssimokawa		printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1251121185Ssimokawa	}
1252121185Ssimokawa	sbp_xfer_free(xfer);
1253127468Ssimokawa	sdev->flags &= ~ORB_DOORBELL_ACTIVE;
1254127468Ssimokawa	if ((sdev->flags & ORB_DOORBELL_NEED) != 0) {
1255127468Ssimokawa		sdev->flags &= ~ORB_DOORBELL_NEED;
1256127468Ssimokawa		sbp_doorbell(sdev);
1257127468Ssimokawa	}
1258121185Ssimokawa	return;
1259121185Ssimokawa}
1260121185Ssimokawa
1261121185Ssimokawastatic void
1262103285Sikobsbp_doorbell(struct sbp_dev *sdev)
1263103285Sikob{
1264103285Sikob	struct fw_xfer *xfer;
1265103285Sikob	struct fw_pkt *fp;
1266103285SikobSBP_DEBUG(1)
1267103285Sikob	sbp_show_sdev_info(sdev, 2);
1268103285Sikob	printf("sbp_doorbell\n");
1269103285SikobEND_DEBUG
1270103285Sikob
1271127468Ssimokawa	if ((sdev->flags & ORB_DOORBELL_ACTIVE) != 0) {
1272127468Ssimokawa		sdev->flags |= ORB_DOORBELL_NEED;
1273127468Ssimokawa		return;
1274127468Ssimokawa	}
1275127468Ssimokawa	sdev->flags |= ORB_DOORBELL_ACTIVE;
1276103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1277103285Sikob	if (xfer == NULL)
1278103285Sikob		return;
1279167632Ssimokawa	xfer->hand = sbp_doorbell_callback;
1280127468Ssimokawa	fp = &xfer->send.hdr;
1281103285Sikob	fp->mode.wreqq.data = htonl(0xf);
1282103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1283103285Sikob}
1284103285Sikob
1285103285Sikobstatic struct fw_xfer *
1286103285Sikobsbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1287103285Sikob{
1288103285Sikob	struct fw_xfer *xfer;
1289103285Sikob	struct fw_pkt *fp;
1290113584Ssimokawa	struct sbp_target *target;
1291113584Ssimokawa	int s, new = 0;
1292103285Sikob
1293113584Ssimokawa	target = sdev->target;
1294113584Ssimokawa	s = splfw();
1295113584Ssimokawa	xfer = STAILQ_FIRST(&target->xferlist);
1296113584Ssimokawa	if (xfer == NULL) {
1297113584Ssimokawa		if (target->n_xfer > 5 /* XXX */) {
1298113584Ssimokawa			printf("sbp: no more xfer for this target\n");
1299113584Ssimokawa			splx(s);
1300113584Ssimokawa			return(NULL);
1301113584Ssimokawa		}
1302120660Ssimokawa		xfer = fw_xfer_alloc_buf(M_SBP, 8, 0);
1303113584Ssimokawa		if(xfer == NULL){
1304113584Ssimokawa			printf("sbp: fw_xfer_alloc_buf failed\n");
1305113584Ssimokawa			splx(s);
1306113584Ssimokawa			return NULL;
1307113584Ssimokawa		}
1308113584Ssimokawa		target->n_xfer ++;
1309113584Ssimokawa		if (debug)
1310113584Ssimokawa			printf("sbp: alloc %d xfer\n", target->n_xfer);
1311113584Ssimokawa		new = 1;
1312113584Ssimokawa	} else {
1313113584Ssimokawa		STAILQ_REMOVE_HEAD(&target->xferlist, link);
1314103285Sikob	}
1315113584Ssimokawa	splx(s);
1316113584Ssimokawa
1317113584Ssimokawa	microtime(&xfer->tv);
1318113584Ssimokawa
1319113584Ssimokawa	if (new) {
1320120660Ssimokawa		xfer->recv.pay_len = 0;
1321120660Ssimokawa		xfer->send.spd = min(sdev->target->fwdev->speed, max_speed);
1322113584Ssimokawa		xfer->fc = sdev->target->sbp->fd.fc;
1323103285Sikob	}
1324120660Ssimokawa
1325120660Ssimokawa	if (tcode == FWTCODE_WREQB)
1326120660Ssimokawa		xfer->send.pay_len = 8;
1327120660Ssimokawa	else
1328120660Ssimokawa		xfer->send.pay_len = 0;
1329120660Ssimokawa
1330103285Sikob	xfer->sc = (caddr_t)sdev;
1331120660Ssimokawa	fp = &xfer->send.hdr;
1332113584Ssimokawa	fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
1333113584Ssimokawa	fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
1334103285Sikob	fp->mode.wreqq.tlrt = 0;
1335103285Sikob	fp->mode.wreqq.tcode = tcode;
1336103285Sikob	fp->mode.wreqq.pri = 0;
1337120660Ssimokawa	fp->mode.wreqq.dst = FWLOCALBUS | sdev->target->fwdev->dst;
1338103285Sikob
1339103285Sikob	return xfer;
1340103285Sikob
1341103285Sikob}
1342103285Sikob
1343103285Sikobstatic void
1344111615Ssimokawasbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1345103285Sikob{
1346103285Sikob	struct fw_xfer *xfer;
1347103285Sikob	struct fw_pkt *fp;
1348103285Sikob	struct sbp_ocb *ocb;
1349111615Ssimokawa	struct sbp_target *target;
1350103285Sikob	int s, nid;
1351103285Sikob
1352111615Ssimokawa	target = sdev->target;
1353111615Ssimokawa	nid = target->sbp->fd.fc->nodeid | FWLOCALBUS;
1354111615Ssimokawa
1355111615Ssimokawa	s = splfw();
1356111615Ssimokawa	if (func == ORB_FUN_RUNQUEUE) {
1357111615Ssimokawa		ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1358111615Ssimokawa		if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1359111615Ssimokawa			splx(s);
1360111615Ssimokawa			return;
1361111615Ssimokawa		}
1362111615Ssimokawa		STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1363111615Ssimokawa		goto start;
1364111615Ssimokawa	}
1365113584Ssimokawa	if ((ocb = sbp_get_ocb(sdev)) == NULL) {
1366103285Sikob		splx(s);
1367122528Ssimokawa		/* XXX */
1368103285Sikob		return;
1369103285Sikob	}
1370103285Sikob	ocb->flags = OCB_ACT_MGM;
1371103285Sikob	ocb->sdev = sdev;
1372103285Sikob
1373120660Ssimokawa	bzero((void *)ocb->orb, sizeof(ocb->orb));
1374103285Sikob	ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1375120660Ssimokawa	ocb->orb[7] = htonl(SBP_DEV2ADDR(target->target_id, sdev->lun_id));
1376103285Sikob
1377107653SsimokawaSBP_DEBUG(0)
1378103285Sikob	sbp_show_sdev_info(sdev, 2);
1379103285Sikob	printf("%s\n", orb_fun_name[(func>>16)&0xf]);
1380107653SsimokawaEND_DEBUG
1381103285Sikob	switch (func) {
1382103285Sikob	case ORB_FUN_LGI:
1383120660Ssimokawa		ocb->orb[0] = ocb->orb[1] = 0; /* password */
1384103285Sikob		ocb->orb[2] = htonl(nid << 16);
1385113584Ssimokawa		ocb->orb[3] = htonl(sdev->dma.bus_addr);
1386121792Ssimokawa		ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id);
1387122387Ssimokawa		if (ex_login)
1388121792Ssimokawa			ocb->orb[4] |= htonl(ORB_EXV);
1389113584Ssimokawa		ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
1390113584Ssimokawa		fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD);
1391103285Sikob		break;
1392110336Ssimokawa	case ORB_FUN_ATA:
1393111615Ssimokawa		ocb->orb[0] = htonl((0 << 16) | 0);
1394113584Ssimokawa		ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
1395110336Ssimokawa		/* fall through */
1396103285Sikob	case ORB_FUN_RCN:
1397103285Sikob	case ORB_FUN_LGO:
1398103285Sikob	case ORB_FUN_LUR:
1399103285Sikob	case ORB_FUN_RST:
1400103285Sikob	case ORB_FUN_ATS:
1401113584Ssimokawa		ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
1402103285Sikob		break;
1403103285Sikob	}
1404103285Sikob
1405111615Ssimokawa	if (target->mgm_ocb_cur != NULL) {
1406111615Ssimokawa		/* there is a standing ORB */
1407111615Ssimokawa		STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1408111615Ssimokawa		splx(s);
1409111615Ssimokawa		return;
1410111615Ssimokawa	}
1411111615Ssimokawastart:
1412111615Ssimokawa	target->mgm_ocb_cur = ocb;
1413111615Ssimokawa	splx(s);
1414111615Ssimokawa
1415111615Ssimokawa	callout_reset(&target->mgm_ocb_timeout, 5*hz,
1416114732Ssimokawa				sbp_mgm_timeout, (caddr_t)ocb);
1417103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1418103285Sikob	if(xfer == NULL){
1419103285Sikob		return;
1420103285Sikob	}
1421167632Ssimokawa	xfer->hand = sbp_mgm_callback;
1422103285Sikob
1423120660Ssimokawa	fp = &xfer->send.hdr;
1424113584Ssimokawa	fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
1425113584Ssimokawa	fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
1426113584Ssimokawa	fp->mode.wreqb.len = 8;
1427103285Sikob	fp->mode.wreqb.extcode = 0;
1428120660Ssimokawa	xfer->send.payload[0] = htonl(nid << 16);
1429120660Ssimokawa	xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff);
1430120660SsimokawaSBP_DEBUG(0)
1431120660Ssimokawa	sbp_show_sdev_info(sdev, 2);
1432129585Sdfr	printf("mgm orb: %08x\n", (uint32_t)ocb->bus_addr);
1433120660SsimokawaEND_DEBUG
1434103285Sikob
1435103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1436103285Sikob}
1437103285Sikob
1438103285Sikobstatic void
1439105792Ssimokawasbp_print_scsi_cmd(struct sbp_ocb *ocb)
1440103285Sikob{
1441103285Sikob	struct ccb_scsiio *csio;
1442103285Sikob
1443103285Sikob	csio = &ocb->ccb->csio;
1444103285Sikob	printf("%s:%d:%d XPT_SCSI_IO: "
1445103285Sikob		"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1446103285Sikob		", flags: 0x%02x, "
1447103285Sikob		"%db cmd/%db data/%db sense\n",
1448103285Sikob		device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1449103285Sikob		ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun,
1450103285Sikob		csio->cdb_io.cdb_bytes[0],
1451103285Sikob		csio->cdb_io.cdb_bytes[1],
1452103285Sikob		csio->cdb_io.cdb_bytes[2],
1453103285Sikob		csio->cdb_io.cdb_bytes[3],
1454103285Sikob		csio->cdb_io.cdb_bytes[4],
1455103285Sikob		csio->cdb_io.cdb_bytes[5],
1456103285Sikob		csio->cdb_io.cdb_bytes[6],
1457103285Sikob		csio->cdb_io.cdb_bytes[7],
1458103285Sikob		csio->cdb_io.cdb_bytes[8],
1459103285Sikob		csio->cdb_io.cdb_bytes[9],
1460103285Sikob		ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1461103285Sikob		csio->cdb_len, csio->dxfer_len,
1462103285Sikob		csio->sense_len);
1463105792Ssimokawa}
1464105792Ssimokawa
1465105792Ssimokawastatic void
1466105792Ssimokawasbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1467105792Ssimokawa{
1468105792Ssimokawa	struct sbp_cmd_status *sbp_cmd_status;
1469105792Ssimokawa	struct scsi_sense_data *sense;
1470105792Ssimokawa
1471105792Ssimokawa	sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1472105792Ssimokawa	sense = &ocb->ccb->csio.sense_data;
1473105792Ssimokawa
1474105792SsimokawaSBP_DEBUG(0)
1475105792Ssimokawa	sbp_print_scsi_cmd(ocb);
1476103285Sikob	/* XXX need decode status */
1477103285Sikob	sbp_show_sdev_info(ocb->sdev, 2);
1478113584Ssimokawa	printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1479103285Sikob		sbp_cmd_status->status,
1480103285Sikob		sbp_cmd_status->sfmt,
1481103285Sikob		sbp_cmd_status->valid,
1482103285Sikob		sbp_cmd_status->s_key,
1483103285Sikob		sbp_cmd_status->s_code,
1484103285Sikob		sbp_cmd_status->s_qlfr,
1485103285Sikob		sbp_status->len
1486103285Sikob	);
1487103285SikobEND_DEBUG
1488103285Sikob
1489110071Ssimokawa	switch (sbp_cmd_status->status) {
1490110071Ssimokawa	case SCSI_STATUS_CHECK_COND:
1491110071Ssimokawa	case SCSI_STATUS_BUSY:
1492110071Ssimokawa	case SCSI_STATUS_CMD_TERMINATED:
1493103285Sikob		if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1494103285Sikob			sense->error_code = SSD_CURRENT_ERROR;
1495103285Sikob		}else{
1496103285Sikob			sense->error_code = SSD_DEFERRED_ERROR;
1497103285Sikob		}
1498103285Sikob		if(sbp_cmd_status->valid)
1499103285Sikob			sense->error_code |= SSD_ERRCODE_VALID;
1500103285Sikob		sense->flags = sbp_cmd_status->s_key;
1501103285Sikob		if(sbp_cmd_status->mark)
1502103285Sikob			sense->flags |= SSD_FILEMARK;
1503103285Sikob		if(sbp_cmd_status->eom)
1504103285Sikob			sense->flags |= SSD_EOM;
1505103285Sikob		if(sbp_cmd_status->ill_len)
1506103285Sikob			sense->flags |= SSD_ILI;
1507119556Ssimokawa
1508119556Ssimokawa		bcopy(&sbp_cmd_status->info, &sense->info[0], 4);
1509119556Ssimokawa
1510103285Sikob		if (sbp_status->len <= 1)
1511103285Sikob			/* XXX not scsi status. shouldn't be happened */
1512103285Sikob			sense->extra_len = 0;
1513103285Sikob		else if (sbp_status->len <= 4)
1514103285Sikob			/* add_sense_code(_qual), info, cmd_spec_info */
1515103285Sikob			sense->extra_len = 6;
1516103285Sikob		else
1517103285Sikob			/* fru, sense_key_spec */
1518103285Sikob			sense->extra_len = 10;
1519119556Ssimokawa
1520119556Ssimokawa		bcopy(&sbp_cmd_status->cdb, &sense->cmd_spec_info[0], 4);
1521119556Ssimokawa
1522103285Sikob		sense->add_sense_code = sbp_cmd_status->s_code;
1523103285Sikob		sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1524103285Sikob		sense->fru = sbp_cmd_status->fru;
1525103285Sikob
1526119556Ssimokawa		bcopy(&sbp_cmd_status->s_keydep[0],
1527119556Ssimokawa		    &sense->sense_key_spec[0], 3);
1528119556Ssimokawa
1529103285Sikob		ocb->ccb->csio.scsi_status = sbp_cmd_status->status;;
1530103285Sikob		ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1531103285Sikob							| CAM_AUTOSNS_VALID;
1532103285Sikob/*
1533103285Sikob{
1534129585Sdfr		uint8_t j, *tmp;
1535103285Sikob		tmp = sense;
1536103285Sikob		for( j = 0 ; j < 32 ; j+=8){
1537103285Sikob			printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1538103285Sikob				tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1539103285Sikob				tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1540103285Sikob		}
1541103285Sikob
1542103285Sikob}
1543103285Sikob*/
1544110071Ssimokawa		break;
1545110071Ssimokawa	default:
1546110071Ssimokawa		sbp_show_sdev_info(ocb->sdev, 2);
1547110071Ssimokawa		printf("sbp_scsi_status: unknown scsi status 0x%x\n",
1548110071Ssimokawa						sbp_cmd_status->status);
1549103285Sikob	}
1550103285Sikob}
1551103285Sikob
1552103285Sikobstatic void
1553103285Sikobsbp_fix_inq_data(struct sbp_ocb *ocb)
1554103285Sikob{
1555103285Sikob	union ccb *ccb;
1556103285Sikob	struct sbp_dev *sdev;
1557103285Sikob	struct scsi_inquiry_data *inq;
1558103285Sikob
1559103285Sikob	ccb = ocb->ccb;
1560103285Sikob	sdev = ocb->sdev;
1561103285Sikob
1562103285Sikob	if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1563103285Sikob		return;
1564103285SikobSBP_DEBUG(1)
1565103285Sikob	sbp_show_sdev_info(sdev, 2);
1566103285Sikob	printf("sbp_fix_inq_data\n");
1567103285SikobEND_DEBUG
1568103285Sikob	inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1569103285Sikob	switch (SID_TYPE(inq)) {
1570103285Sikob	case T_DIRECT:
1571118291Ssimokawa#if 0
1572103285Sikob		/*
1573103285Sikob		 * XXX Convert Direct Access device to RBC.
1574108281Ssimokawa		 * I've never seen FireWire DA devices which support READ_6.
1575103285Sikob		 */
1576103285Sikob		if (SID_TYPE(inq) == T_DIRECT)
1577103285Sikob			inq->device |= T_RBC; /*  T_DIRECT == 0 */
1578103285Sikob#endif
1579103285Sikob		/* fall through */
1580103285Sikob	case T_RBC:
1581103285Sikob		/*
1582103285Sikob		 * Override vendor/product/revision information.
1583103285Sikob		 * Some devices sometimes return strange strings.
1584103285Sikob		 */
1585111615Ssimokawa#if 1
1586103285Sikob		bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1587103285Sikob		bcopy(sdev->product, inq->product, sizeof(inq->product));
1588103285Sikob		bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1589111615Ssimokawa#endif
1590103285Sikob		break;
1591103285Sikob	}
1592127468Ssimokawa	/*
1593127468Ssimokawa	 * Force to enable/disable tagged queuing.
1594127468Ssimokawa	 * XXX CAM also checks SCP_QUEUE_DQUE flag in the control mode page.
1595127468Ssimokawa	 */
1596127468Ssimokawa	if (sbp_tags > 0)
1597127468Ssimokawa		inq->flags |= SID_CmdQue;
1598127468Ssimokawa	else if (sbp_tags < 0)
1599127468Ssimokawa		inq->flags &= ~SID_CmdQue;
1600127468Ssimokawa
1601103285Sikob}
1602103285Sikob
1603103285Sikobstatic void
1604113584Ssimokawasbp_recv1(struct fw_xfer *xfer)
1605113584Ssimokawa{
1606103285Sikob	struct fw_pkt *rfp;
1607103285Sikob#if NEED_RESPONSE
1608103285Sikob	struct fw_pkt *sfp;
1609103285Sikob#endif
1610103285Sikob	struct sbp_softc *sbp;
1611103285Sikob	struct sbp_dev *sdev;
1612103285Sikob	struct sbp_ocb *ocb;
1613103285Sikob	struct sbp_login_res *login_res = NULL;
1614103285Sikob	struct sbp_status *sbp_status;
1615103285Sikob	struct sbp_target *target;
1616111704Ssimokawa	int	orb_fun, status_valid0, status_valid, t, l, reset_agent = 0;
1617129585Sdfr	uint32_t addr;
1618103285Sikob/*
1619129585Sdfr	uint32_t *ld;
1620103285Sikob	ld = xfer->recv.buf;
1621103285Sikobprintf("sbp %x %d %d %08x %08x %08x %08x\n",
1622103285Sikob			xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1623103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1624103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1625103285Sikob*/
1626113584Ssimokawa	sbp = (struct sbp_softc *)xfer->sc;
1627120660Ssimokawa	if (xfer->resp != 0){
1628122529Ssimokawa		printf("sbp_recv: xfer->resp = %d\n", xfer->resp);
1629113584Ssimokawa		goto done0;
1630103285Sikob	}
1631120660Ssimokawa	if (xfer->recv.payload == NULL){
1632120660Ssimokawa		printf("sbp_recv: xfer->recv.payload == NULL\n");
1633113584Ssimokawa		goto done0;
1634103285Sikob	}
1635120660Ssimokawa	rfp = &xfer->recv.hdr;
1636103285Sikob	if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1637103285Sikob		printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1638113584Ssimokawa		goto done0;
1639103285Sikob	}
1640120660Ssimokawa	sbp_status = (struct sbp_status *)xfer->recv.payload;
1641113584Ssimokawa	addr = rfp->mode.wreqb.dest_lo;
1642103285SikobSBP_DEBUG(2)
1643103285Sikob	printf("received address 0x%x\n", addr);
1644103285SikobEND_DEBUG
1645110189Ssimokawa	t = SBP_ADDR2TRG(addr);
1646110189Ssimokawa	if (t >= SBP_NUM_TARGETS) {
1647110189Ssimokawa		device_printf(sbp->fd.dev,
1648110189Ssimokawa			"sbp_recv1: invalid target %d\n", t);
1649113584Ssimokawa		goto done0;
1650110189Ssimokawa	}
1651110189Ssimokawa	target = &sbp->targets[t];
1652110189Ssimokawa	l = SBP_ADDR2LUN(addr);
1653121185Ssimokawa	if (l >= target->num_lun || target->luns[l] == NULL) {
1654110189Ssimokawa		device_printf(sbp->fd.dev,
1655110189Ssimokawa			"sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1656113584Ssimokawa		goto done0;
1657110189Ssimokawa	}
1658121185Ssimokawa	sdev = target->luns[l];
1659103285Sikob
1660110189Ssimokawa	ocb = NULL;
1661110189Ssimokawa	switch (sbp_status->src) {
1662110189Ssimokawa	case 0:
1663110189Ssimokawa	case 1:
1664111615Ssimokawa		/* check mgm_ocb_cur first */
1665111615Ssimokawa		ocb  = target->mgm_ocb_cur;
1666111615Ssimokawa		if (ocb != NULL) {
1667111615Ssimokawa			if (OCB_MATCH(ocb, sbp_status)) {
1668111615Ssimokawa				callout_stop(&target->mgm_ocb_timeout);
1669111615Ssimokawa				target->mgm_ocb_cur = NULL;
1670111615Ssimokawa				break;
1671111615Ssimokawa			}
1672111615Ssimokawa		}
1673111615Ssimokawa		ocb = sbp_dequeue_ocb(sdev, sbp_status);
1674110189Ssimokawa		if (ocb == NULL) {
1675110189Ssimokawa			sbp_show_sdev_info(sdev, 2);
1676127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
1677127468Ssimokawa			printf("No ocb(%lx) on the queue\n",
1678127468Ssimokawa#else
1679111819Ssimokawa			printf("No ocb(%x) on the queue\n",
1680111858Ssimokawa#endif
1681111819Ssimokawa					ntohl(sbp_status->orb_lo));
1682110189Ssimokawa		}
1683110189Ssimokawa		break;
1684110189Ssimokawa	case 2:
1685110189Ssimokawa		/* unsolicit */
1686110189Ssimokawa		sbp_show_sdev_info(sdev, 2);
1687110189Ssimokawa		printf("unsolicit status received\n");
1688110189Ssimokawa		break;
1689110189Ssimokawa	default:
1690110189Ssimokawa		sbp_show_sdev_info(sdev, 2);
1691110189Ssimokawa		printf("unknown sbp_status->src\n");
1692110189Ssimokawa	}
1693110189Ssimokawa
1694111615Ssimokawa	status_valid0 = (sbp_status->src < 2
1695110189Ssimokawa			&& sbp_status->resp == ORB_RES_CMPL
1696111615Ssimokawa			&& sbp_status->dead == 0);
1697111615Ssimokawa	status_valid = (status_valid0 && sbp_status->status == 0);
1698103285Sikob
1699121185Ssimokawa	if (!status_valid0 || debug > 2){
1700103285Sikob		int status;
1701110129SsimokawaSBP_DEBUG(0)
1702103285Sikob		sbp_show_sdev_info(sdev, 2);
1703103285Sikob		printf("ORB status src:%x resp:%x dead:%x"
1704127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
1705127468Ssimokawa				" len:%x stat:%x orb:%x%08lx\n",
1706127468Ssimokawa#else
1707103285Sikob				" len:%x stat:%x orb:%x%08x\n",
1708108712Ssimokawa#endif
1709103285Sikob			sbp_status->src, sbp_status->resp, sbp_status->dead,
1710103285Sikob			sbp_status->len, sbp_status->status,
1711108280Ssimokawa			ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1712110129SsimokawaEND_DEBUG
1713103285Sikob		sbp_show_sdev_info(sdev, 2);
1714103285Sikob		status = sbp_status->status;
1715103285Sikob		switch(sbp_status->resp) {
1716103285Sikob		case 0:
1717103285Sikob			if (status > MAX_ORB_STATUS0)
1718103285Sikob				printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1719110189Ssimokawa			else
1720110129Ssimokawa				printf("%s\n", orb_status0[status]);
1721103285Sikob			break;
1722103285Sikob		case 1:
1723110336Ssimokawa			printf("Obj: %s, Error: %s\n",
1724103285Sikob				orb_status1_object[(status>>6) & 3],
1725103285Sikob				orb_status1_serial_bus_error[status & 0xf]);
1726103285Sikob			break;
1727110129Ssimokawa		case 2:
1728110129Ssimokawa			printf("Illegal request\n");
1729110129Ssimokawa			break;
1730110129Ssimokawa		case 3:
1731110129Ssimokawa			printf("Vendor dependent\n");
1732110129Ssimokawa			break;
1733103285Sikob		default:
1734110129Ssimokawa			printf("unknown respose code %d\n", sbp_status->resp);
1735103285Sikob		}
1736103285Sikob	}
1737103285Sikob
1738103285Sikob	/* we have to reset the fetch agent if it's dead */
1739103285Sikob	if (sbp_status->dead) {
1740111615Ssimokawa		if (sdev->path) {
1741103285Sikob			xpt_freeze_devq(sdev->path, 1);
1742111615Ssimokawa			sdev->freeze ++;
1743111615Ssimokawa		}
1744111704Ssimokawa		reset_agent = 1;
1745103285Sikob	}
1746103285Sikob
1747111704Ssimokawa	if (ocb == NULL)
1748111704Ssimokawa		goto done;
1749103285Sikob
1750103285Sikob	switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1751103285Sikob	case ORB_FMT_NOP:
1752103285Sikob		break;
1753103285Sikob	case ORB_FMT_VED:
1754103285Sikob		break;
1755103285Sikob	case ORB_FMT_STD:
1756111819Ssimokawa		switch(ocb->flags) {
1757103285Sikob		case OCB_ACT_MGM:
1758103285Sikob			orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1759120842Ssimokawa			reset_agent = 0;
1760103285Sikob			switch(orb_fun) {
1761103285Sikob			case ORB_FUN_LGI:
1762113584Ssimokawa				fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD);
1763113584Ssimokawa				login_res = sdev->login;
1764103285Sikob				login_res->len = ntohs(login_res->len);
1765103285Sikob				login_res->id = ntohs(login_res->id);
1766103285Sikob				login_res->cmd_hi = ntohs(login_res->cmd_hi);
1767103285Sikob				login_res->cmd_lo = ntohl(login_res->cmd_lo);
1768103285Sikob				if (status_valid) {
1769103285SikobSBP_DEBUG(0)
1770103285Sikobsbp_show_sdev_info(sdev, 2);
1771103285Sikobprintf("login: len %d, ID %d, cmd %08x%08x, recon_hold %d\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo, ntohs(login_res->recon_hold));
1772103285SikobEND_DEBUG
1773103285Sikob					sbp_busy_timeout(sdev);
1774103285Sikob				} else {
1775110336Ssimokawa					/* forgot logout? */
1776110336Ssimokawa					sbp_show_sdev_info(sdev, 2);
1777103285Sikob					printf("login failed\n");
1778103285Sikob					sdev->status = SBP_DEV_RESET;
1779103285Sikob				}
1780103285Sikob				break;
1781103285Sikob			case ORB_FUN_RCN:
1782113584Ssimokawa				login_res = sdev->login;
1783103285Sikob				if (status_valid) {
1784103285SikobSBP_DEBUG(0)
1785103285Sikobsbp_show_sdev_info(sdev, 2);
1786103285Sikobprintf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo);
1787103285SikobEND_DEBUG
1788103285Sikob#if 1
1789111615Ssimokawa					if (sdev->status == SBP_DEV_ATTACHED)
1790111615Ssimokawa						sbp_scan_dev(sdev);
1791111615Ssimokawa					else
1792111615Ssimokawa						sbp_agent_reset(sdev);
1793103285Sikob#else
1794110336Ssimokawa					sdev->status = SBP_DEV_ATTACHED;
1795111615Ssimokawa					sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL);
1796103285Sikob#endif
1797103285Sikob				} else {
1798103285Sikob					/* reconnection hold time exceed? */
1799110336SsimokawaSBP_DEBUG(0)
1800110336Ssimokawa					sbp_show_sdev_info(sdev, 2);
1801103285Sikob					printf("reconnect failed\n");
1802110336SsimokawaEND_DEBUG
1803111615Ssimokawa					sbp_login(sdev);
1804103285Sikob				}
1805103285Sikob				break;
1806103285Sikob			case ORB_FUN_LGO:
1807103285Sikob				sdev->status = SBP_DEV_RESET;
1808103285Sikob				break;
1809110336Ssimokawa			case ORB_FUN_RST:
1810110336Ssimokawa				sbp_busy_timeout(sdev);
1811110336Ssimokawa				break;
1812103285Sikob			case ORB_FUN_LUR:
1813103285Sikob			case ORB_FUN_ATA:
1814103285Sikob			case ORB_FUN_ATS:
1815110336Ssimokawa				sbp_agent_reset(sdev);
1816103285Sikob				break;
1817103285Sikob			default:
1818110336Ssimokawa				sbp_show_sdev_info(sdev, 2);
1819110336Ssimokawa				printf("unknown function %d\n", orb_fun);
1820103285Sikob				break;
1821103285Sikob			}
1822111615Ssimokawa			sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
1823103285Sikob			break;
1824103285Sikob		case OCB_ACT_CMD:
1825114732Ssimokawa			sdev->timeout = 0;
1826103285Sikob			if(ocb->ccb != NULL){
1827103285Sikob				union ccb *ccb;
1828103285Sikob/*
1829129585Sdfr				uint32_t *ld;
1830103285Sikob				ld = ocb->ccb->csio.data_ptr;
1831103285Sikob				if(ld != NULL && ocb->ccb->csio.dxfer_len != 0)
1832103285Sikob					printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]);
1833103285Sikob				else
1834103285Sikob					printf("ptr NULL\n");
1835103285Sikobprintf("len %d\n", sbp_status->len);
1836103285Sikob*/
1837103285Sikob				ccb = ocb->ccb;
1838103285Sikob				if(sbp_status->len > 1){
1839103285Sikob					sbp_scsi_status(sbp_status, ocb);
1840103285Sikob				}else{
1841103285Sikob					if(sbp_status->resp != ORB_RES_CMPL){
1842103285Sikob						ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1843103285Sikob					}else{
1844103285Sikob						ccb->ccb_h.status = CAM_REQ_CMP;
1845103285Sikob					}
1846103285Sikob				}
1847103285Sikob				/* fix up inq data */
1848103285Sikob				if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1849103285Sikob					sbp_fix_inq_data(ocb);
1850103285Sikob				xpt_done(ccb);
1851103285Sikob			}
1852103285Sikob			break;
1853103285Sikob		default:
1854103285Sikob			break;
1855103285Sikob		}
1856103285Sikob	}
1857103285Sikob
1858127468Ssimokawa	if (!use_doorbell)
1859127468Ssimokawa		sbp_free_ocb(sdev, ocb);
1860111704Ssimokawadone:
1861111704Ssimokawa	if (reset_agent)
1862111704Ssimokawa		sbp_agent_reset(sdev);
1863103285Sikob
1864113584Ssimokawadone0:
1865120660Ssimokawa	xfer->recv.pay_len = SBP_RECV_LEN;
1866103285Sikob/* The received packet is usually small enough to be stored within
1867103285Sikob * the buffer. In that case, the controller return ack_complete and
1868103285Sikob * no respose is necessary.
1869103285Sikob *
1870103285Sikob * XXX fwohci.c and firewire.c should inform event_code such as
1871103285Sikob * ack_complete or ack_pending to upper driver.
1872103285Sikob */
1873103285Sikob#if NEED_RESPONSE
1874103285Sikob	xfer->send.off = 0;
1875103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
1876103285Sikob	sfp->mode.wres.dst = rfp->mode.wreqb.src;
1877113584Ssimokawa	xfer->dst = sfp->mode.wres.dst;
1878103285Sikob	xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1879167632Ssimokawa	xfer->hand = sbp_loginres_callback;
1880103285Sikob
1881103285Sikob	sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1882103285Sikob	sfp->mode.wres.tcode = FWTCODE_WRES;
1883103285Sikob	sfp->mode.wres.rtcode = 0;
1884103285Sikob	sfp->mode.wres.pri = 0;
1885103285Sikob
1886103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1887103285Sikob#else
1888113584Ssimokawa	/* recycle */
1889113584Ssimokawa	STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1890103285Sikob#endif
1891103285Sikob
1892103285Sikob	return;
1893103285Sikob
1894103285Sikob}
1895103285Sikob
1896103285Sikobstatic void
1897103285Sikobsbp_recv(struct fw_xfer *xfer)
1898103285Sikob{
1899103285Sikob	int s;
1900103285Sikob
1901103285Sikob	s = splcam();
1902103285Sikob	sbp_recv1(xfer);
1903103285Sikob	splx(s);
1904103285Sikob}
1905103285Sikob/*
1906103285Sikob * sbp_attach()
1907103285Sikob */
1908103285Sikobstatic int
1909103285Sikobsbp_attach(device_t dev)
1910103285Sikob{
1911103285Sikob	struct sbp_softc *sbp;
1912103285Sikob	struct cam_devq *devq;
1913103285Sikob	struct fw_xfer *xfer;
1914103285Sikob	int i, s, error;
1915103285Sikob
1916167624Ssimokawa	if (DFLTPHYS > SBP_MAXPHYS)
1917167624Ssimokawa		device_printf(dev, "Warning, DFLTPHYS(%dKB) is larger than "
1918167624Ssimokawa			"SBP_MAXPHYS(%dKB).\n", DFLTPHYS / 1024,
1919167624Ssimokawa			SBP_MAXPHYS / 1024);
1920167624Ssimokawa
1921103285SikobSBP_DEBUG(0)
1922111199Ssimokawa	printf("sbp_attach (cold=%d)\n", cold);
1923103285SikobEND_DEBUG
1924103285Sikob
1925111199Ssimokawa	if (cold)
1926111199Ssimokawa		sbp_cold ++;
1927103285Sikob	sbp = ((struct sbp_softc *)device_get_softc(dev));
1928103285Sikob	bzero(sbp, sizeof(struct sbp_softc));
1929103285Sikob	sbp->fd.dev = dev;
1930103285Sikob	sbp->fd.fc = device_get_ivars(dev);
1931124251Ssimokawa
1932124251Ssimokawa	if (max_speed < 0)
1933124251Ssimokawa		max_speed = sbp->fd.fc->speed;
1934124251Ssimokawa
1935113584Ssimokawa	error = bus_dma_tag_create(/*parent*/sbp->fd.fc->dmat,
1936113584Ssimokawa				/* XXX shoud be 4 for sane backend? */
1937113584Ssimokawa				/*alignment*/1,
1938103285Sikob				/*boundary*/0,
1939103285Sikob				/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1940103285Sikob				/*highaddr*/BUS_SPACE_MAXADDR,
1941103285Sikob				/*filter*/NULL, /*filterarg*/NULL,
1942103285Sikob				/*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1943113474Ssimokawa				/*maxsegsz*/SBP_SEG_MAX,
1944103285Sikob				/*flags*/BUS_DMA_ALLOCNOW,
1945127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 501102
1946117126Sscottl				/*lockfunc*/busdma_lock_mutex,
1947117228Ssimokawa				/*lockarg*/&Giant,
1948117228Ssimokawa#endif
1949117228Ssimokawa				&sbp->dmat);
1950103285Sikob	if (error != 0) {
1951103285Sikob		printf("sbp_attach: Could not allocate DMA tag "
1952103285Sikob			"- error %d\n", error);
1953103285Sikob			return (ENOMEM);
1954103285Sikob	}
1955103285Sikob
1956103285Sikob	devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1957103285Sikob	if (devq == NULL)
1958103285Sikob		return (ENXIO);
1959103285Sikob
1960103285Sikob	for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1961103285Sikob		sbp->targets[i].fwdev = NULL;
1962103285Sikob		sbp->targets[i].luns = NULL;
1963103285Sikob	}
1964103285Sikob
1965103285Sikob	sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1966103285Sikob				 device_get_unit(dev),
1967168752Sscottl				 &Giant,
1968111615Ssimokawa				 /*untagged*/ 1,
1969122528Ssimokawa				 /*tagged*/ SBP_QUEUE_LEN - 1,
1970111615Ssimokawa				 devq);
1971103285Sikob
1972103285Sikob	if (sbp->sim == NULL) {
1973103285Sikob		cam_simq_free(devq);
1974103285Sikob		return (ENXIO);
1975103285Sikob	}
1976103285Sikob
1977103285Sikob
1978111615Ssimokawa	if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS)
1979111615Ssimokawa		goto fail;
1980103285Sikob
1981111615Ssimokawa	if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim),
1982120660Ssimokawa	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1983120660Ssimokawa		xpt_bus_deregister(cam_sim_path(sbp->sim));
1984111615Ssimokawa		goto fail;
1985120660Ssimokawa	}
1986111615Ssimokawa
1987103285Sikob	/* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1988120660Ssimokawa	sbp->fwb.start = ((u_int64_t)SBP_BIND_HI << 32) | SBP_DEV2ADDR(0, 0);
1989120660Ssimokawa	sbp->fwb.end = sbp->fwb.start + 0xffff;
1990113584Ssimokawa	sbp->fwb.act_type = FWACT_XFER;
1991113584Ssimokawa	/* pre-allocate xfer */
1992113584Ssimokawa	STAILQ_INIT(&sbp->fwb.xferlist);
1993113584Ssimokawa	for (i = 0; i < SBP_NUM_OCB/2; i ++) {
1994113584Ssimokawa		xfer = fw_xfer_alloc_buf(M_SBP,
1995113584Ssimokawa			/* send */0,
1996113584Ssimokawa			/* recv */SBP_RECV_LEN);
1997167632Ssimokawa		xfer->hand = sbp_recv;
1998113584Ssimokawa#if NEED_RESPONSE
1999113584Ssimokawa		xfer->fc = sbp->fd.fc;
2000113584Ssimokawa#endif
2001113584Ssimokawa		xfer->sc = (caddr_t)sbp;
2002113584Ssimokawa		STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
2003113584Ssimokawa	}
2004103285Sikob	fw_bindadd(sbp->fd.fc, &sbp->fwb);
2005103285Sikob
2006113584Ssimokawa	sbp->fd.post_busreset = sbp_post_busreset;
2007103285Sikob	sbp->fd.post_explore = sbp_post_explore;
2008103285Sikob
2009111199Ssimokawa	if (sbp->fd.fc->status != -1) {
2010111199Ssimokawa		s = splfw();
2011122387Ssimokawa		sbp_post_busreset((void *)sbp);
2012111199Ssimokawa		sbp_post_explore((void *)sbp);
2013111199Ssimokawa		splx(s);
2014111199Ssimokawa	}
2015122387Ssimokawa	xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
2016111199Ssimokawa
2017103285Sikob	return (0);
2018111615Ssimokawafail:
2019111615Ssimokawa	cam_sim_free(sbp->sim, /*free_devq*/TRUE);
2020111615Ssimokawa	return (ENXIO);
2021103285Sikob}
2022103285Sikob
2023103285Sikobstatic int
2024110145Ssimokawasbp_logout_all(struct sbp_softc *sbp)
2025110145Ssimokawa{
2026110145Ssimokawa	struct sbp_target *target;
2027110145Ssimokawa	struct sbp_dev *sdev;
2028110145Ssimokawa	int i, j;
2029110145Ssimokawa
2030110145SsimokawaSBP_DEBUG(0)
2031110145Ssimokawa	printf("sbp_logout_all\n");
2032110145SsimokawaEND_DEBUG
2033110145Ssimokawa	for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
2034110145Ssimokawa		target = &sbp->targets[i];
2035110145Ssimokawa		if (target->luns == NULL)
2036110145Ssimokawa			continue;
2037110145Ssimokawa		for (j = 0; j < target->num_lun; j++) {
2038121185Ssimokawa			sdev = target->luns[j];
2039121185Ssimokawa			if (sdev == NULL)
2040121185Ssimokawa				continue;
2041113584Ssimokawa			callout_stop(&sdev->login_callout);
2042110336Ssimokawa			if (sdev->status >= SBP_DEV_TOATTACH &&
2043110336Ssimokawa					sdev->status <= SBP_DEV_ATTACHED)
2044111615Ssimokawa				sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
2045110145Ssimokawa		}
2046110145Ssimokawa	}
2047113584Ssimokawa
2048110145Ssimokawa	return 0;
2049110145Ssimokawa}
2050110145Ssimokawa
2051110145Ssimokawastatic int
2052110145Ssimokawasbp_shutdown(device_t dev)
2053110145Ssimokawa{
2054110145Ssimokawa	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2055110145Ssimokawa
2056110145Ssimokawa	sbp_logout_all(sbp);
2057110145Ssimokawa	return (0);
2058110145Ssimokawa}
2059110145Ssimokawa
2060121185Ssimokawastatic void
2061121185Ssimokawasbp_free_sdev(struct sbp_dev *sdev)
2062121185Ssimokawa{
2063121185Ssimokawa	int i;
2064121185Ssimokawa
2065121185Ssimokawa	if (sdev == NULL)
2066121185Ssimokawa		return;
2067121185Ssimokawa	for (i = 0; i < SBP_QUEUE_LEN; i++)
2068121185Ssimokawa		bus_dmamap_destroy(sdev->target->sbp->dmat,
2069121185Ssimokawa		    sdev->ocb[i].dmamap);
2070121185Ssimokawa	fwdma_free(sdev->target->sbp->fd.fc, &sdev->dma);
2071121185Ssimokawa	free(sdev, M_SBP);
2072121185Ssimokawa}
2073121185Ssimokawa
2074121185Ssimokawastatic void
2075121185Ssimokawasbp_free_target(struct sbp_target *target)
2076121185Ssimokawa{
2077121185Ssimokawa	struct sbp_softc *sbp;
2078121185Ssimokawa	struct fw_xfer *xfer, *next;
2079121185Ssimokawa	int i;
2080121185Ssimokawa
2081121185Ssimokawa	if (target->luns == NULL)
2082121185Ssimokawa		return;
2083121185Ssimokawa	callout_stop(&target->mgm_ocb_timeout);
2084121185Ssimokawa	sbp = target->sbp;
2085121185Ssimokawa	for (i = 0; i < target->num_lun; i++)
2086121185Ssimokawa		sbp_free_sdev(target->luns[i]);
2087121185Ssimokawa
2088121185Ssimokawa	for (xfer = STAILQ_FIRST(&target->xferlist);
2089121185Ssimokawa			xfer != NULL; xfer = next) {
2090121185Ssimokawa		next = STAILQ_NEXT(xfer, link);
2091121185Ssimokawa		fw_xfer_free_buf(xfer);
2092121185Ssimokawa	}
2093121185Ssimokawa	STAILQ_INIT(&target->xferlist);
2094121185Ssimokawa	free(target->luns, M_SBP);
2095121185Ssimokawa	target->num_lun = 0;;
2096121185Ssimokawa	target->luns = NULL;
2097121185Ssimokawa	target->fwdev = NULL;
2098121185Ssimokawa}
2099121185Ssimokawa
2100110145Ssimokawastatic int
2101103285Sikobsbp_detach(device_t dev)
2102103285Sikob{
2103103285Sikob	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2104103285Sikob	struct firewire_comm *fc = sbp->fd.fc;
2105113584Ssimokawa	struct fw_xfer *xfer, *next;
2106121185Ssimokawa	int i;
2107103285Sikob
2108103285SikobSBP_DEBUG(0)
2109103285Sikob	printf("sbp_detach\n");
2110103285SikobEND_DEBUG
2111113584Ssimokawa
2112103285Sikob	for (i = 0; i < SBP_NUM_TARGETS; i ++)
2113110145Ssimokawa		sbp_cam_detach_target(&sbp->targets[i]);
2114120660Ssimokawa	xpt_async(AC_LOST_DEVICE, sbp->path, NULL);
2115111615Ssimokawa	xpt_free_path(sbp->path);
2116103285Sikob	xpt_bus_deregister(cam_sim_path(sbp->sim));
2117120660Ssimokawa	cam_sim_free(sbp->sim, /*free_devq*/ TRUE),
2118110145Ssimokawa
2119110145Ssimokawa	sbp_logout_all(sbp);
2120113584Ssimokawa
2121110145Ssimokawa	/* XXX wait for logout completion */
2122167086Sjhb	pause("sbpdtc", hz/2);
2123110145Ssimokawa
2124121185Ssimokawa	for (i = 0 ; i < SBP_NUM_TARGETS ; i ++)
2125121185Ssimokawa		sbp_free_target(&sbp->targets[i]);
2126113584Ssimokawa
2127113584Ssimokawa	for (xfer = STAILQ_FIRST(&sbp->fwb.xferlist);
2128113584Ssimokawa				xfer != NULL; xfer = next) {
2129113584Ssimokawa		next = STAILQ_NEXT(xfer, link);
2130120660Ssimokawa		fw_xfer_free_buf(xfer);
2131113584Ssimokawa	}
2132113584Ssimokawa	STAILQ_INIT(&sbp->fwb.xferlist);
2133110145Ssimokawa	fw_bindremove(fc, &sbp->fwb);
2134113584Ssimokawa
2135103285Sikob	bus_dma_tag_destroy(sbp->dmat);
2136110145Ssimokawa
2137103285Sikob	return (0);
2138103285Sikob}
2139103285Sikob
2140103285Sikobstatic void
2141121185Ssimokawasbp_cam_detach_sdev(struct sbp_dev *sdev)
2142121185Ssimokawa{
2143121185Ssimokawa	if (sdev == NULL)
2144121185Ssimokawa		return;
2145121185Ssimokawa	if (sdev->status == SBP_DEV_DEAD)
2146121185Ssimokawa		return;
2147121185Ssimokawa	if (sdev->status == SBP_DEV_RESET)
2148121185Ssimokawa		return;
2149121185Ssimokawa	if (sdev->path) {
2150121185Ssimokawa		xpt_release_devq(sdev->path,
2151121185Ssimokawa				 sdev->freeze, TRUE);
2152121185Ssimokawa		sdev->freeze = 0;
2153121185Ssimokawa		xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
2154121185Ssimokawa		xpt_free_path(sdev->path);
2155121185Ssimokawa		sdev->path = NULL;
2156121185Ssimokawa	}
2157121185Ssimokawa	sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
2158121185Ssimokawa}
2159121185Ssimokawa
2160121185Ssimokawastatic void
2161110145Ssimokawasbp_cam_detach_target(struct sbp_target *target)
2162103285Sikob{
2163103285Sikob	int i;
2164103285Sikob
2165103285Sikob	if (target->luns != NULL) {
2166108529SsimokawaSBP_DEBUG(0)
2167103285Sikob		printf("sbp_detach_target %d\n", target->target_id);
2168108529SsimokawaEND_DEBUG
2169111615Ssimokawa		callout_stop(&target->scan_callout);
2170121185Ssimokawa		for (i = 0; i < target->num_lun; i++)
2171121185Ssimokawa			sbp_cam_detach_sdev(target->luns[i]);
2172103285Sikob	}
2173103285Sikob}
2174103285Sikob
2175103285Sikobstatic void
2176114732Ssimokawasbp_target_reset(struct sbp_dev *sdev, int method)
2177114732Ssimokawa{
2178114732Ssimokawa	int i;
2179114732Ssimokawa	struct sbp_target *target = sdev->target;
2180114732Ssimokawa	struct sbp_dev *tsdev;
2181114732Ssimokawa
2182114732Ssimokawa	for (i = 0; i < target->num_lun; i++) {
2183121185Ssimokawa		tsdev = target->luns[i];
2184121185Ssimokawa		if (tsdev == NULL)
2185121185Ssimokawa			continue;
2186114732Ssimokawa		if (tsdev->status == SBP_DEV_DEAD)
2187114732Ssimokawa			continue;
2188114732Ssimokawa		if (tsdev->status == SBP_DEV_RESET)
2189114732Ssimokawa			continue;
2190114732Ssimokawa		xpt_freeze_devq(tsdev->path, 1);
2191114732Ssimokawa		tsdev->freeze ++;
2192114732Ssimokawa		sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT);
2193114732Ssimokawa		if (method == 2)
2194114732Ssimokawa			tsdev->status = SBP_DEV_LOGIN;
2195114732Ssimokawa	}
2196114732Ssimokawa	switch(method) {
2197114732Ssimokawa	case 1:
2198114732Ssimokawa		printf("target reset\n");
2199114732Ssimokawa		sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2200114732Ssimokawa		break;
2201114732Ssimokawa	case 2:
2202114732Ssimokawa		printf("reset start\n");
2203114732Ssimokawa		sbp_reset_start(sdev);
2204114732Ssimokawa		break;
2205114732Ssimokawa	}
2206114732Ssimokawa
2207114732Ssimokawa}
2208114732Ssimokawa
2209114732Ssimokawastatic void
2210114732Ssimokawasbp_mgm_timeout(void *arg)
2211114732Ssimokawa{
2212114732Ssimokawa	struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2213114732Ssimokawa	struct sbp_dev *sdev = ocb->sdev;
2214114732Ssimokawa	struct sbp_target *target = sdev->target;
2215114732Ssimokawa
2216114732Ssimokawa	sbp_show_sdev_info(sdev, 2);
2217120842Ssimokawa	printf("request timeout(mgm orb:0x%08x) ... ",
2218129585Sdfr	    (uint32_t)ocb->bus_addr);
2219114732Ssimokawa	target->mgm_ocb_cur = NULL;
2220114732Ssimokawa	sbp_free_ocb(sdev, ocb);
2221114732Ssimokawa#if 0
2222114732Ssimokawa	/* XXX */
2223121185Ssimokawa	printf("run next request\n");
2224114732Ssimokawa	sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2225114732Ssimokawa#endif
2226121185Ssimokawa#if 1
2227121185Ssimokawa	printf("reset start\n");
2228114732Ssimokawa	sbp_reset_start(sdev);
2229114732Ssimokawa#endif
2230114732Ssimokawa}
2231114732Ssimokawa
2232114732Ssimokawastatic void
2233103285Sikobsbp_timeout(void *arg)
2234103285Sikob{
2235103285Sikob	struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2236103285Sikob	struct sbp_dev *sdev = ocb->sdev;
2237103285Sikob
2238103285Sikob	sbp_show_sdev_info(sdev, 2);
2239120842Ssimokawa	printf("request timeout(cmd orb:0x%08x) ... ",
2240129585Sdfr	    (uint32_t)ocb->bus_addr);
2241103285Sikob
2242114732Ssimokawa	sdev->timeout ++;
2243114732Ssimokawa	switch(sdev->timeout) {
2244114732Ssimokawa	case 1:
2245110336Ssimokawa		printf("agent reset\n");
2246114732Ssimokawa		xpt_freeze_devq(sdev->path, 1);
2247114732Ssimokawa		sdev->freeze ++;
2248114732Ssimokawa		sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2249110336Ssimokawa		sbp_agent_reset(sdev);
2250114732Ssimokawa		break;
2251114732Ssimokawa	case 2:
2252114732Ssimokawa	case 3:
2253114732Ssimokawa		sbp_target_reset(sdev, sdev->timeout - 1);
2254114732Ssimokawa		break;
2255114732Ssimokawa#if 0
2256114732Ssimokawa	default:
2257114732Ssimokawa		/* XXX give up */
2258114732Ssimokawa		sbp_cam_detach_target(target);
2259114732Ssimokawa		if (target->luns != NULL)
2260114732Ssimokawa			free(target->luns, M_SBP);
2261114732Ssimokawa		target->num_lun = 0;;
2262114732Ssimokawa		target->luns = NULL;
2263114732Ssimokawa		target->fwdev = NULL;
2264114732Ssimokawa#endif
2265110336Ssimokawa	}
2266103285Sikob}
2267103285Sikob
2268103285Sikobstatic void
2269103285Sikobsbp_action1(struct cam_sim *sim, union ccb *ccb)
2270103285Sikob{
2271103285Sikob
2272103285Sikob	struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2273103285Sikob	struct sbp_target *target = NULL;
2274103285Sikob	struct sbp_dev *sdev = NULL;
2275103285Sikob
2276103285Sikob	/* target:lun -> sdev mapping */
2277103285Sikob	if (sbp != NULL
2278103285Sikob			&& ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2279103285Sikob			&& ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2280103285Sikob		target = &sbp->targets[ccb->ccb_h.target_id];
2281103285Sikob		if (target->fwdev != NULL
2282103285Sikob				&& ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2283103285Sikob				&& ccb->ccb_h.target_lun < target->num_lun) {
2284121185Ssimokawa			sdev = target->luns[ccb->ccb_h.target_lun];
2285121185Ssimokawa			if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED &&
2286103285Sikob				sdev->status != SBP_DEV_PROBE)
2287103285Sikob				sdev = NULL;
2288103285Sikob		}
2289103285Sikob	}
2290103285Sikob
2291103285SikobSBP_DEBUG(1)
2292103285Sikob	if (sdev == NULL)
2293103285Sikob		printf("invalid target %d lun %d\n",
2294103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2295103285SikobEND_DEBUG
2296103285Sikob
2297103285Sikob	switch (ccb->ccb_h.func_code) {
2298103285Sikob	case XPT_SCSI_IO:
2299103285Sikob	case XPT_RESET_DEV:
2300103285Sikob	case XPT_GET_TRAN_SETTINGS:
2301103285Sikob	case XPT_SET_TRAN_SETTINGS:
2302103285Sikob	case XPT_CALC_GEOMETRY:
2303103285Sikob		if (sdev == NULL) {
2304103285SikobSBP_DEBUG(1)
2305103285Sikob			printf("%s:%d:%d:func_code 0x%04x: "
2306103285Sikob				"Invalid target (target needed)\n",
2307103285Sikob				device_get_nameunit(sbp->fd.dev),
2308103285Sikob				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2309103285Sikob				ccb->ccb_h.func_code);
2310103285SikobEND_DEBUG
2311103285Sikob
2312108276Ssimokawa			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2313103285Sikob			xpt_done(ccb);
2314103285Sikob			return;
2315103285Sikob		}
2316103285Sikob		break;
2317103285Sikob	case XPT_PATH_INQ:
2318103285Sikob	case XPT_NOOP:
2319103285Sikob		/* The opcodes sometimes aimed at a target (sc is valid),
2320103285Sikob		 * sometimes aimed at the SIM (sc is invalid and target is
2321103285Sikob		 * CAM_TARGET_WILDCARD)
2322103285Sikob		 */
2323103285Sikob		if (sbp == NULL &&
2324103285Sikob			ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2325103285SikobSBP_DEBUG(0)
2326103285Sikob			printf("%s:%d:%d func_code 0x%04x: "
2327103285Sikob				"Invalid target (no wildcard)\n",
2328103285Sikob				device_get_nameunit(sbp->fd.dev),
2329103285Sikob				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2330103285Sikob				ccb->ccb_h.func_code);
2331103285SikobEND_DEBUG
2332108276Ssimokawa			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2333103285Sikob			xpt_done(ccb);
2334103285Sikob			return;
2335103285Sikob		}
2336103285Sikob		break;
2337103285Sikob	default:
2338103285Sikob		/* XXX Hm, we should check the input parameters */
2339103285Sikob		break;
2340103285Sikob	}
2341103285Sikob
2342103285Sikob	switch (ccb->ccb_h.func_code) {
2343103285Sikob	case XPT_SCSI_IO:
2344103285Sikob	{
2345103285Sikob		struct ccb_scsiio *csio;
2346103285Sikob		struct sbp_ocb *ocb;
2347113584Ssimokawa		int speed;
2348106506Ssimokawa		void *cdb;
2349103285Sikob
2350103285Sikob		csio = &ccb->csio;
2351103285Sikob
2352121185SsimokawaSBP_DEBUG(2)
2353103285Sikob		printf("%s:%d:%d XPT_SCSI_IO: "
2354103285Sikob			"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2355103285Sikob			", flags: 0x%02x, "
2356103285Sikob			"%db cmd/%db data/%db sense\n",
2357103285Sikob			device_get_nameunit(sbp->fd.dev),
2358103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2359103285Sikob			csio->cdb_io.cdb_bytes[0],
2360103285Sikob			csio->cdb_io.cdb_bytes[1],
2361103285Sikob			csio->cdb_io.cdb_bytes[2],
2362103285Sikob			csio->cdb_io.cdb_bytes[3],
2363103285Sikob			csio->cdb_io.cdb_bytes[4],
2364103285Sikob			csio->cdb_io.cdb_bytes[5],
2365103285Sikob			csio->cdb_io.cdb_bytes[6],
2366103285Sikob			csio->cdb_io.cdb_bytes[7],
2367103285Sikob			csio->cdb_io.cdb_bytes[8],
2368103285Sikob			csio->cdb_io.cdb_bytes[9],
2369103285Sikob			ccb->ccb_h.flags & CAM_DIR_MASK,
2370103285Sikob			csio->cdb_len, csio->dxfer_len,
2371103285Sikob			csio->sense_len);
2372103285SikobEND_DEBUG
2373103285Sikob		if(sdev == NULL){
2374103285Sikob			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2375103285Sikob			xpt_done(ccb);
2376103285Sikob			return;
2377103285Sikob		}
2378103285Sikob#if 0
2379103285Sikob		/* if we are in probe stage, pass only probe commands */
2380103285Sikob		if (sdev->status == SBP_DEV_PROBE) {
2381103285Sikob			char *name;
2382103285Sikob			name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2383103285Sikob			printf("probe stage, periph name: %s\n", name);
2384103285Sikob			if (strcmp(name, "probe") != 0) {
2385103285Sikob				ccb->ccb_h.status = CAM_REQUEUE_REQ;
2386103285Sikob				xpt_done(ccb);
2387103285Sikob				return;
2388103285Sikob			}
2389103285Sikob		}
2390103285Sikob#endif
2391122528Ssimokawa		if ((ocb = sbp_get_ocb(sdev)) == NULL) {
2392122528Ssimokawa			ccb->ccb_h.status = CAM_REQUEUE_REQ;
2393127468Ssimokawa			if (sdev->freeze == 0) {
2394127468Ssimokawa				xpt_freeze_devq(sdev->path, 1);
2395127468Ssimokawa				sdev->freeze ++;
2396127468Ssimokawa			}
2397122528Ssimokawa			xpt_done(ccb);
2398103285Sikob			return;
2399122528Ssimokawa		}
2400113584Ssimokawa
2401103285Sikob		ocb->flags = OCB_ACT_CMD;
2402103285Sikob		ocb->sdev = sdev;
2403103285Sikob		ocb->ccb = ccb;
2404103285Sikob		ccb->ccb_h.ccb_sdev_ptr = sdev;
2405103285Sikob		ocb->orb[0] = htonl(1 << 31);
2406103285Sikob		ocb->orb[1] = 0;
2407103285Sikob		ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2408113584Ssimokawa		ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2409103285Sikob		speed = min(target->fwdev->speed, max_speed);
2410103285Sikob		ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2411103285Sikob						| ORB_CMD_MAXP(speed + 7));
2412103285Sikob		if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
2413103285Sikob			ocb->orb[4] |= htonl(ORB_CMD_IN);
2414103285Sikob		}
2415103285Sikob
2416103285Sikob		if (csio->ccb_h.flags & CAM_SCATTER_VALID)
2417103285Sikob			printf("sbp: CAM_SCATTER_VALID\n");
2418103285Sikob		if (csio->ccb_h.flags & CAM_DATA_PHYS)
2419103285Sikob			printf("sbp: CAM_DATA_PHYS\n");
2420103285Sikob
2421106506Ssimokawa		if (csio->ccb_h.flags & CAM_CDB_POINTER)
2422106506Ssimokawa			cdb = (void *)csio->cdb_io.cdb_ptr;
2423106506Ssimokawa		else
2424106506Ssimokawa			cdb = (void *)&csio->cdb_io.cdb_bytes;
2425120660Ssimokawa		bcopy(cdb, (void *)&ocb->orb[5], csio->cdb_len);
2426103285Sikob/*
2427103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2428103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2429103285Sikob*/
2430103285Sikob		if (ccb->csio.dxfer_len > 0) {
2431113584Ssimokawa			int s, error;
2432103285Sikob
2433103285Sikob			s = splsoftvm();
2434113584Ssimokawa			error = bus_dmamap_load(/*dma tag*/sbp->dmat,
2435103285Sikob					/*dma map*/ocb->dmamap,
2436103285Sikob					ccb->csio.data_ptr,
2437103285Sikob					ccb->csio.dxfer_len,
2438103285Sikob					sbp_execute_ocb,
2439103285Sikob					ocb,
2440103285Sikob					/*flags*/0);
2441103285Sikob			splx(s);
2442113584Ssimokawa			if (error)
2443113584Ssimokawa				printf("sbp: bus_dmamap_load error %d\n", error);
2444103285Sikob		} else
2445103285Sikob			sbp_execute_ocb(ocb, NULL, 0, 0);
2446103285Sikob		break;
2447103285Sikob	}
2448103285Sikob	case XPT_CALC_GEOMETRY:
2449103285Sikob	{
2450103285Sikob		struct ccb_calc_geometry *ccg;
2451127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 501100
2452129585Sdfr		uint32_t size_mb;
2453129585Sdfr		uint32_t secs_per_cylinder;
2454116429Ssimokawa		int extended = 1;
2455116429Ssimokawa#endif
2456116351Snjl
2457103285Sikob		ccg = &ccb->ccg;
2458103285Sikob		if (ccg->block_size == 0) {
2459103285Sikob			printf("sbp_action1: block_size is 0.\n");
2460103285Sikob			ccb->ccb_h.status = CAM_REQ_INVALID;
2461103285Sikob			xpt_done(ccb);
2462103285Sikob			break;
2463103285Sikob		}
2464103285SikobSBP_DEBUG(1)
2465103285Sikob		printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2466127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2467127468Ssimokawa			"Volume size = %d\n",
2468127468Ssimokawa#else
2469114540Sjake			"Volume size = %jd\n",
2470114927Ssimokawa#endif
2471114927Ssimokawa			device_get_nameunit(sbp->fd.dev),
2472114927Ssimokawa			cam_sim_path(sbp->sim),
2473103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2474127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2475114927Ssimokawa			(uintmax_t)
2476114927Ssimokawa#endif
2477114927Ssimokawa				ccg->volume_size);
2478103285SikobEND_DEBUG
2479103285Sikob
2480127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 501100
2481116429Ssimokawa		size_mb = ccg->volume_size
2482116429Ssimokawa			/ ((1024L * 1024L) / ccg->block_size);
2483116429Ssimokawa
2484116429Ssimokawa		if (size_mb > 1024 && extended) {
2485116429Ssimokawa			ccg->heads = 255;
2486116429Ssimokawa			ccg->secs_per_track = 63;
2487116429Ssimokawa		} else {
2488116429Ssimokawa			ccg->heads = 64;
2489116429Ssimokawa			ccg->secs_per_track = 32;
2490116429Ssimokawa		}
2491116429Ssimokawa		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2492116429Ssimokawa		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2493116429Ssimokawa		ccb->ccb_h.status = CAM_REQ_CMP;
2494116429Ssimokawa#else
2495116351Snjl		cam_calc_geometry(ccg, /*extended*/1);
2496116429Ssimokawa#endif
2497103285Sikob		xpt_done(ccb);
2498103285Sikob		break;
2499103285Sikob	}
2500103285Sikob	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
2501103285Sikob	{
2502103285Sikob
2503103285SikobSBP_DEBUG(1)
2504103285Sikob		printf("%s:%d:XPT_RESET_BUS: \n",
2505103285Sikob			device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2506103285SikobEND_DEBUG
2507103285Sikob
2508103285Sikob		ccb->ccb_h.status = CAM_REQ_INVALID;
2509103285Sikob		xpt_done(ccb);
2510103285Sikob		break;
2511103285Sikob	}
2512103285Sikob	case XPT_PATH_INQ:		/* Path routing inquiry */
2513103285Sikob	{
2514103285Sikob		struct ccb_pathinq *cpi = &ccb->cpi;
2515103285Sikob
2516103285SikobSBP_DEBUG(1)
2517103285Sikob		printf("%s:%d:%d XPT_PATH_INQ:.\n",
2518103285Sikob			device_get_nameunit(sbp->fd.dev),
2519103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2520103285SikobEND_DEBUG
2521103285Sikob		cpi->version_num = 1; /* XXX??? */
2522111615Ssimokawa		cpi->hba_inquiry = PI_TAG_ABLE;
2523103285Sikob		cpi->target_sprt = 0;
2524118105Snjl		cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE;
2525103285Sikob		cpi->hba_eng_cnt = 0;
2526103285Sikob		cpi->max_target = SBP_NUM_TARGETS - 1;
2527103285Sikob		cpi->max_lun = SBP_NUM_LUNS - 1;
2528103285Sikob		cpi->initiator_id = SBP_INITIATOR;
2529103285Sikob		cpi->bus_id = sim->bus_id;
2530103285Sikob		cpi->base_transfer_speed = 400 * 1000 / 8;
2531103285Sikob		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2532103285Sikob		strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2533103285Sikob		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2534103285Sikob		cpi->unit_number = sim->unit_number;
2535163816Smjacob                cpi->transport = XPORT_SPI;	/* XX should have a FireWire */
2536163816Smjacob                cpi->transport_version = 2;
2537163816Smjacob                cpi->protocol = PROTO_SCSI;
2538163816Smjacob                cpi->protocol_version = SCSI_REV_2;
2539103285Sikob
2540103285Sikob		cpi->ccb_h.status = CAM_REQ_CMP;
2541103285Sikob		xpt_done(ccb);
2542103285Sikob		break;
2543103285Sikob	}
2544103285Sikob	case XPT_GET_TRAN_SETTINGS:
2545103285Sikob	{
2546103285Sikob		struct ccb_trans_settings *cts = &ccb->cts;
2547163816Smjacob		struct ccb_trans_settings_scsi *scsi =
2548163816Smjacob		    &cts->proto_specific.scsi;
2549163816Smjacob		struct ccb_trans_settings_spi *spi =
2550163816Smjacob		    &cts->xport_specific.spi;
2551163816Smjacob
2552163816Smjacob		cts->protocol = PROTO_SCSI;
2553163816Smjacob		cts->protocol_version = SCSI_REV_2;
2554163816Smjacob		cts->transport = XPORT_SPI;	/* should have a FireWire */
2555163816Smjacob		cts->transport_version = 2;
2556163816Smjacob		spi->valid = CTS_SPI_VALID_DISC;
2557163816Smjacob		spi->flags = CTS_SPI_FLAGS_DISC_ENB;
2558163816Smjacob		scsi->valid = CTS_SCSI_VALID_TQ;
2559163816Smjacob		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2560103285SikobSBP_DEBUG(1)
2561103285Sikob		printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2562103285Sikob			device_get_nameunit(sbp->fd.dev),
2563103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2564103285SikobEND_DEBUG
2565103285Sikob		cts->ccb_h.status = CAM_REQ_CMP;
2566103285Sikob		xpt_done(ccb);
2567103285Sikob		break;
2568103285Sikob	}
2569103285Sikob	case XPT_ABORT:
2570103285Sikob		ccb->ccb_h.status = CAM_UA_ABORT;
2571103285Sikob		xpt_done(ccb);
2572103285Sikob		break;
2573111615Ssimokawa	case XPT_SET_TRAN_SETTINGS:
2574111615Ssimokawa		/* XXX */
2575103285Sikob	default:
2576103285Sikob		ccb->ccb_h.status = CAM_REQ_INVALID;
2577103285Sikob		xpt_done(ccb);
2578103285Sikob		break;
2579103285Sikob	}
2580103285Sikob	return;
2581103285Sikob}
2582103285Sikob
2583103285Sikobstatic void
2584103285Sikobsbp_action(struct cam_sim *sim, union ccb *ccb)
2585103285Sikob{
2586103285Sikob	int s;
2587103285Sikob
2588103285Sikob	s = splfw();
2589103285Sikob	sbp_action1(sim, ccb);
2590103285Sikob	splx(s);
2591103285Sikob}
2592103285Sikob
2593103285Sikobstatic void
2594103285Sikobsbp_execute_ocb(void *arg,  bus_dma_segment_t *segments, int seg, int error)
2595103285Sikob{
2596103285Sikob	int i;
2597103285Sikob	struct sbp_ocb *ocb;
2598103285Sikob	struct sbp_ocb *prev;
2599105633Ssimokawa	bus_dma_segment_t *s;
2600103285Sikob
2601103285Sikob	if (error)
2602103285Sikob		printf("sbp_execute_ocb: error=%d\n", error);
2603103285Sikob
2604103285Sikob	ocb = (struct sbp_ocb *)arg;
2605113584Ssimokawa
2606121185SsimokawaSBP_DEBUG(2)
2607113584Ssimokawa	printf("sbp_execute_ocb: seg %d", seg);
2608113584Ssimokawa	for (i = 0; i < seg; i++)
2609127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2610127468Ssimokawa		printf(", %x:%d", segments[i].ds_addr, segments[i].ds_len);
2611127468Ssimokawa#else
2612113972Ssimokawa		printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2613113972Ssimokawa					(uintmax_t)segments[i].ds_len);
2614113584Ssimokawa#endif
2615113584Ssimokawa	printf("\n");
2616113584SsimokawaEND_DEBUG
2617113584Ssimokawa
2618103285Sikob	if (seg == 1) {
2619103285Sikob		/* direct pointer */
2620113474Ssimokawa		s = &segments[0];
2621113474Ssimokawa		if (s->ds_len > SBP_SEG_MAX)
2622113474Ssimokawa			panic("ds_len > SBP_SEG_MAX, fix busdma code");
2623113474Ssimokawa		ocb->orb[3] = htonl(s->ds_addr);
2624113474Ssimokawa		ocb->orb[4] |= htonl(s->ds_len);
2625103285Sikob	} else if(seg > 1) {
2626103285Sikob		/* page table */
2627103285Sikob		for (i = 0; i < seg; i++) {
2628105633Ssimokawa			s = &segments[i];
2629108877SsimokawaSBP_DEBUG(0)
2630108877Ssimokawa			/* XXX LSI Logic "< 16 byte" bug might be hit */
2631105633Ssimokawa			if (s->ds_len < 16)
2632105633Ssimokawa				printf("sbp_execute_ocb: warning, "
2633127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2634127468Ssimokawa					"segment length(%d) is less than 16."
2635127468Ssimokawa#else
2636106543Ssimokawa					"segment length(%zd) is less than 16."
2637108877Ssimokawa#endif
2638105633Ssimokawa					"(seg=%d/%d)\n", s->ds_len, i+1, seg);
2639108877SsimokawaEND_DEBUG
2640113474Ssimokawa			if (s->ds_len > SBP_SEG_MAX)
2641113474Ssimokawa				panic("ds_len > SBP_SEG_MAX, fix busdma code");
2642105633Ssimokawa			ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2643105633Ssimokawa			ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2644103285Sikob		}
2645103285Sikob		ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2646103285Sikob	}
2647103285Sikob
2648113584Ssimokawa	if (seg > 0)
2649113584Ssimokawa		bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2650113584Ssimokawa			(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2651113584Ssimokawa			BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2652103285Sikob	prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2653113584Ssimokawa	fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2654127468Ssimokawa	if (use_doorbell) {
2655127468Ssimokawa		if (prev == NULL) {
2656127468Ssimokawa			if (ocb->sdev->last_ocb != NULL)
2657127468Ssimokawa				sbp_doorbell(ocb->sdev);
2658127468Ssimokawa			else
2659127468Ssimokawa				sbp_orb_pointer(ocb->sdev, ocb);
2660127468Ssimokawa		}
2661127468Ssimokawa	} else {
2662127468Ssimokawa		if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) {
2663127468Ssimokawa			ocb->sdev->flags &= ~ORB_LINK_DEAD;
2664127468Ssimokawa			sbp_orb_pointer(ocb->sdev, ocb);
2665127468Ssimokawa		}
2666120842Ssimokawa	}
2667103285Sikob}
2668103285Sikob
2669103285Sikobstatic void
2670103285Sikobsbp_poll(struct cam_sim *sim)
2671103285Sikob{
2672120847Ssimokawa	struct sbp_softc *sbp;
2673120847Ssimokawa	struct firewire_comm *fc;
2674120847Ssimokawa
2675120847Ssimokawa	sbp = (struct sbp_softc *)sim->softc;
2676120847Ssimokawa	fc = sbp->fd.fc;
2677120847Ssimokawa
2678120847Ssimokawa	fc->poll(fc, 0, -1);
2679120847Ssimokawa
2680103285Sikob	return;
2681103285Sikob}
2682120847Ssimokawa
2683103285Sikobstatic struct sbp_ocb *
2684111615Ssimokawasbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2685103285Sikob{
2686103285Sikob	struct sbp_ocb *ocb;
2687103285Sikob	struct sbp_ocb *next;
2688103285Sikob	int s = splfw(), order = 0;
2689103285Sikob	int flags;
2690103285Sikob
2691103285SikobSBP_DEBUG(1)
2692121185Ssimokawa	sbp_show_sdev_info(sdev, 2);
2693127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2694127468Ssimokawa	printf("%s: 0x%08lx src %d\n",
2695127468Ssimokawa#else
2696121185Ssimokawa	printf("%s: 0x%08x src %d\n",
2697108712Ssimokawa#endif
2698127468Ssimokawa	    __func__, ntohl(sbp_status->orb_lo), sbp_status->src);
2699103285SikobEND_DEBUG
2700121185Ssimokawa	for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2701121185Ssimokawa		next = STAILQ_NEXT(ocb, ocb);
2702121185Ssimokawa		flags = ocb->flags;
2703111615Ssimokawa		if (OCB_MATCH(ocb, sbp_status)) {
2704103285Sikob			/* found */
2705111819Ssimokawa			STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2706103285Sikob			if (ocb->ccb != NULL)
2707103285Sikob				untimeout(sbp_timeout, (caddr_t)ocb,
2708103285Sikob						ocb->ccb->ccb_h.timeout_ch);
2709113584Ssimokawa			if (ntohl(ocb->orb[4]) & 0xffff) {
2710113584Ssimokawa				bus_dmamap_sync(sdev->target->sbp->dmat,
2711113584Ssimokawa					ocb->dmamap,
2712113584Ssimokawa					(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2713113584Ssimokawa					BUS_DMASYNC_POSTREAD :
2714113584Ssimokawa					BUS_DMASYNC_POSTWRITE);
2715113584Ssimokawa				bus_dmamap_unload(sdev->target->sbp->dmat,
2716113584Ssimokawa					ocb->dmamap);
2717103285Sikob			}
2718127468Ssimokawa			if (!use_doorbell) {
2719127468Ssimokawa				if (sbp_status->src == SRC_NO_NEXT) {
2720127468Ssimokawa					if (next != NULL)
2721127468Ssimokawa						sbp_orb_pointer(sdev, next);
2722127468Ssimokawa					else if (order > 0) {
2723127468Ssimokawa						/*
2724127468Ssimokawa						 * Unordered execution
2725127468Ssimokawa						 * We need to send pointer for
2726127468Ssimokawa						 * next ORB
2727127468Ssimokawa						 */
2728127468Ssimokawa						sdev->flags |= ORB_LINK_DEAD;
2729127468Ssimokawa					}
2730120842Ssimokawa				}
2731127468Ssimokawa			} else {
2732127468Ssimokawa				/*
2733127468Ssimokawa				 * XXX this is not correct for unordered
2734127468Ssimokawa				 * execution.
2735127468Ssimokawa				 */
2736127468Ssimokawa				if (sdev->last_ocb != NULL)
2737127468Ssimokawa					sbp_free_ocb(sdev, sdev->last_ocb);
2738127468Ssimokawa				sdev->last_ocb = ocb;
2739127468Ssimokawa				if (next != NULL &&
2740127468Ssimokawa				    sbp_status->src == SRC_NO_NEXT)
2741127468Ssimokawa					sbp_doorbell(sdev);
2742120842Ssimokawa			}
2743103285Sikob			break;
2744111819Ssimokawa		} else
2745111819Ssimokawa			order ++;
2746103285Sikob	}
2747103285Sikob	splx(s);
2748103285SikobSBP_DEBUG(0)
2749103285Sikob	if (ocb && order > 0) {
2750103285Sikob		sbp_show_sdev_info(sdev, 2);
2751103285Sikob		printf("unordered execution order:%d\n", order);
2752103285Sikob	}
2753103285SikobEND_DEBUG
2754103285Sikob	return (ocb);
2755103285Sikob}
2756103285Sikob
2757103285Sikobstatic struct sbp_ocb *
2758103285Sikobsbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2759103285Sikob{
2760103285Sikob	int s = splfw();
2761127468Ssimokawa	struct sbp_ocb *prev, *prev2;
2762103285Sikob
2763121185SsimokawaSBP_DEBUG(1)
2764103285Sikob	sbp_show_sdev_info(sdev, 2);
2765127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2766127468Ssimokawa	printf("%s: 0x%08x\n", __func__, ocb->bus_addr);
2767108712Ssimokawa#else
2768127468Ssimokawa	printf("%s: 0x%08jx\n", __func__, (uintmax_t)ocb->bus_addr);
2769108712Ssimokawa#endif
2770103285SikobEND_DEBUG
2771127468Ssimokawa	prev2 = prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2772103285Sikob	STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2773103285Sikob
2774103285Sikob	if (ocb->ccb != NULL)
2775103285Sikob		ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb,
2776103285Sikob					(ocb->ccb->ccb_h.timeout * hz) / 1000);
2777103285Sikob
2778127468Ssimokawa	if (use_doorbell && prev == NULL)
2779127468Ssimokawa		prev2 = sdev->last_ocb;
2780127468Ssimokawa
2781127468Ssimokawa	if (prev2 != NULL) {
2782121185SsimokawaSBP_DEBUG(2)
2783127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2784127468Ssimokawa		printf("linking chain 0x%x -> 0x%x\n",
2785127468Ssimokawa		    prev2->bus_addr, ocb->bus_addr);
2786127468Ssimokawa#else
2787120842Ssimokawa		printf("linking chain 0x%jx -> 0x%jx\n",
2788127468Ssimokawa		    (uintmax_t)prev2->bus_addr, (uintmax_t)ocb->bus_addr);
2789108712Ssimokawa#endif
2790103285SikobEND_DEBUG
2791127468Ssimokawa		prev2->orb[1] = htonl(ocb->bus_addr);
2792127468Ssimokawa		prev2->orb[0] = 0;
2793103285Sikob	}
2794103285Sikob	splx(s);
2795103285Sikob
2796103285Sikob	return prev;
2797103285Sikob}
2798103285Sikob
2799103285Sikobstatic struct sbp_ocb *
2800113584Ssimokawasbp_get_ocb(struct sbp_dev *sdev)
2801103285Sikob{
2802103285Sikob	struct sbp_ocb *ocb;
2803103285Sikob	int s = splfw();
2804113584Ssimokawa	ocb = STAILQ_FIRST(&sdev->free_ocbs);
2805103285Sikob	if (ocb == NULL) {
2806127468Ssimokawa		sdev->flags |= ORB_SHORTAGE;
2807103285Sikob		printf("ocb shortage!!!\n");
2808134868Ssimokawa		splx(s);
2809103285Sikob		return NULL;
2810103285Sikob	}
2811113584Ssimokawa	STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2812103285Sikob	splx(s);
2813103285Sikob	ocb->ccb = NULL;
2814103285Sikob	return (ocb);
2815103285Sikob}
2816103285Sikob
2817103285Sikobstatic void
2818113584Ssimokawasbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2819103285Sikob{
2820103285Sikob	ocb->flags = 0;
2821103285Sikob	ocb->ccb = NULL;
2822113584Ssimokawa	STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2823127468Ssimokawa	if ((sdev->flags & ORB_SHORTAGE) != 0) {
2824127468Ssimokawa		int count;
2825127468Ssimokawa
2826127468Ssimokawa		sdev->flags &= ~ORB_SHORTAGE;
2827127468Ssimokawa		count = sdev->freeze;
2828127468Ssimokawa		sdev->freeze = 0;
2829127468Ssimokawa		xpt_release_devq(sdev->path, count, TRUE);
2830127468Ssimokawa	}
2831103285Sikob}
2832103285Sikob
2833103285Sikobstatic void
2834103285Sikobsbp_abort_ocb(struct sbp_ocb *ocb, int status)
2835103285Sikob{
2836103285Sikob	struct sbp_dev *sdev;
2837103285Sikob
2838103285Sikob	sdev = ocb->sdev;
2839113584SsimokawaSBP_DEBUG(0)
2840103285Sikob	sbp_show_sdev_info(sdev, 2);
2841127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2842127468Ssimokawa	printf("sbp_abort_ocb 0x%x\n", ocb->bus_addr);
2843127468Ssimokawa#else
2844113972Ssimokawa	printf("sbp_abort_ocb 0x%jx\n", (uintmax_t)ocb->bus_addr);
2845111819Ssimokawa#endif
2846113584SsimokawaEND_DEBUG
2847113584SsimokawaSBP_DEBUG(1)
2848105792Ssimokawa	if (ocb->ccb != NULL)
2849105792Ssimokawa		sbp_print_scsi_cmd(ocb);
2850103285SikobEND_DEBUG
2851113584Ssimokawa	if (ntohl(ocb->orb[4]) & 0xffff) {
2852113584Ssimokawa		bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
2853113584Ssimokawa			(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2854113584Ssimokawa			BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2855113584Ssimokawa		bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
2856113584Ssimokawa	}
2857111819Ssimokawa	if (ocb->ccb != NULL) {
2858110336Ssimokawa		untimeout(sbp_timeout, (caddr_t)ocb,
2859110336Ssimokawa					ocb->ccb->ccb_h.timeout_ch);
2860103285Sikob		ocb->ccb->ccb_h.status = status;
2861103285Sikob		xpt_done(ocb->ccb);
2862103285Sikob	}
2863113584Ssimokawa	sbp_free_ocb(sdev, ocb);
2864103285Sikob}
2865103285Sikob
2866103285Sikobstatic void
2867103285Sikobsbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2868103285Sikob{
2869103285Sikob	int s;
2870105792Ssimokawa	struct sbp_ocb *ocb, *next;
2871105792Ssimokawa	STAILQ_HEAD(, sbp_ocb) temp;
2872103285Sikob
2873103285Sikob	s = splfw();
2874105792Ssimokawa
2875105792Ssimokawa	bcopy(&sdev->ocbs, &temp, sizeof(temp));
2876105792Ssimokawa	STAILQ_INIT(&sdev->ocbs);
2877105792Ssimokawa	for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2878105792Ssimokawa		next = STAILQ_NEXT(ocb, ocb);
2879103285Sikob		sbp_abort_ocb(ocb, status);
2880103285Sikob	}
2881127468Ssimokawa	if (sdev->last_ocb != NULL) {
2882127468Ssimokawa		sbp_free_ocb(sdev, sdev->last_ocb);
2883127468Ssimokawa		sdev->last_ocb = NULL;
2884127468Ssimokawa	}
2885105792Ssimokawa
2886103285Sikob	splx(s);
2887103285Sikob}
2888103285Sikob
2889103285Sikobstatic devclass_t sbp_devclass;
2890103285Sikob
2891103285Sikobstatic device_method_t sbp_methods[] = {
2892103285Sikob	/* device interface */
2893103285Sikob	DEVMETHOD(device_identify,	sbp_identify),
2894103285Sikob	DEVMETHOD(device_probe,		sbp_probe),
2895103285Sikob	DEVMETHOD(device_attach,	sbp_attach),
2896103285Sikob	DEVMETHOD(device_detach,	sbp_detach),
2897110145Ssimokawa	DEVMETHOD(device_shutdown,	sbp_shutdown),
2898103285Sikob
2899103285Sikob	{ 0, 0 }
2900103285Sikob};
2901103285Sikob
2902103285Sikobstatic driver_t sbp_driver = {
2903103285Sikob	"sbp",
2904103285Sikob	sbp_methods,
2905103285Sikob	sizeof(struct sbp_softc),
2906103285Sikob};
2907127468Ssimokawa#ifdef __DragonFly__
2908127468SsimokawaDECLARE_DUMMY_MODULE(sbp);
2909127468Ssimokawa#endif
2910103285SikobDRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2911103285SikobMODULE_VERSION(sbp, 1);
2912103285SikobMODULE_DEPEND(sbp, firewire, 1, 1, 1);
2913103285SikobMODULE_DEPEND(sbp, cam, 1, 1, 1);
2914