sbp.c revision 170378
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 170378 2007-06-06 18:33:38Z gallatin $
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;
248170374Ssimokawa	struct mtx mtx;
249103285Sikob};
250170374Ssimokawa#define SBP_LOCK(sbp) mtx_lock(&(sbp)->mtx)
251170374Ssimokawa#define SBP_UNLOCK(sbp) mtx_unlock(&(sbp)->mtx)
252122387Ssimokawa
253124169Ssimokawastatic void sbp_post_explore (void *);
254124169Ssimokawastatic void sbp_recv (struct fw_xfer *);
255124169Ssimokawastatic void sbp_mgm_callback (struct fw_xfer *);
256121185Ssimokawa#if 0
257124169Ssimokawastatic void sbp_cmd_callback (struct fw_xfer *);
258121185Ssimokawa#endif
259124169Ssimokawastatic void sbp_orb_pointer (struct sbp_dev *, struct sbp_ocb *);
260127468Ssimokawastatic void sbp_doorbell(struct sbp_dev *);
261124169Ssimokawastatic void sbp_execute_ocb (void *,  bus_dma_segment_t *, int, int);
262124169Ssimokawastatic void sbp_free_ocb (struct sbp_dev *, struct sbp_ocb *);
263124169Ssimokawastatic void sbp_abort_ocb (struct sbp_ocb *, int);
264124169Ssimokawastatic void sbp_abort_all_ocbs (struct sbp_dev *, int);
265124169Ssimokawastatic struct fw_xfer * sbp_write_cmd (struct sbp_dev *, int, int);
266124169Ssimokawastatic struct sbp_ocb * sbp_get_ocb (struct sbp_dev *);
267124169Ssimokawastatic struct sbp_ocb * sbp_enqueue_ocb (struct sbp_dev *, struct sbp_ocb *);
268124169Ssimokawastatic struct sbp_ocb * sbp_dequeue_ocb (struct sbp_dev *, struct sbp_status *);
269121185Ssimokawastatic void sbp_cam_detach_sdev(struct sbp_dev *);
270121185Ssimokawastatic void sbp_free_sdev(struct sbp_dev *);
271124169Ssimokawastatic void sbp_cam_detach_target (struct sbp_target *);
272124169Ssimokawastatic void sbp_free_target (struct sbp_target *);
273124169Ssimokawastatic void sbp_mgm_timeout (void *arg);
274124169Ssimokawastatic void sbp_timeout (void *arg);
275124169Ssimokawastatic void sbp_mgm_orb (struct sbp_dev *, int, struct sbp_ocb *);
276103285Sikob
277108281SsimokawaMALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire");
278103285Sikob
279103285Sikob/* cam related functions */
280103285Sikobstatic void	sbp_action(struct cam_sim *sim, union ccb *ccb);
281103285Sikobstatic void	sbp_poll(struct cam_sim *sim);
282111615Ssimokawastatic void	sbp_cam_scan_lun(struct cam_periph *, union ccb *);
283111615Ssimokawastatic void	sbp_cam_scan_target(void *arg);
284103285Sikob
285103285Sikobstatic char *orb_status0[] = {
286103285Sikob	/* 0 */ "No additional information to report",
287103285Sikob	/* 1 */ "Request type not supported",
288103285Sikob	/* 2 */ "Speed not supported",
289103285Sikob	/* 3 */ "Page size not supported",
290103285Sikob	/* 4 */ "Access denied",
291103285Sikob	/* 5 */ "Logical unit not supported",
292103285Sikob	/* 6 */ "Maximum payload too small",
293103285Sikob	/* 7 */ "Reserved for future standardization",
294103285Sikob	/* 8 */ "Resources unavailable",
295103285Sikob	/* 9 */ "Function rejected",
296103285Sikob	/* A */ "Login ID not recognized",
297103285Sikob	/* B */ "Dummy ORB completed",
298103285Sikob	/* C */ "Request aborted",
299103285Sikob	/* FF */ "Unspecified error"
300103285Sikob#define MAX_ORB_STATUS0 0xd
301103285Sikob};
302103285Sikob
303103285Sikobstatic char *orb_status1_object[] = {
304103285Sikob	/* 0 */ "Operation request block (ORB)",
305103285Sikob	/* 1 */ "Data buffer",
306103285Sikob	/* 2 */ "Page table",
307103285Sikob	/* 3 */ "Unable to specify"
308103285Sikob};
309103285Sikob
310103285Sikobstatic char *orb_status1_serial_bus_error[] = {
311103285Sikob	/* 0 */ "Missing acknowledge",
312103285Sikob	/* 1 */ "Reserved; not to be used",
313103285Sikob	/* 2 */ "Time-out error",
314103285Sikob	/* 3 */ "Reserved; not to be used",
315103285Sikob	/* 4 */ "Busy retry limit exceeded(X)",
316103285Sikob	/* 5 */ "Busy retry limit exceeded(A)",
317103285Sikob	/* 6 */ "Busy retry limit exceeded(B)",
318103285Sikob	/* 7 */ "Reserved for future standardization",
319103285Sikob	/* 8 */ "Reserved for future standardization",
320103285Sikob	/* 9 */ "Reserved for future standardization",
321103285Sikob	/* A */ "Reserved for future standardization",
322103285Sikob	/* B */ "Tardy retry limit exceeded",
323103285Sikob	/* C */ "Conflict error",
324103285Sikob	/* D */ "Data error",
325103285Sikob	/* E */ "Type error",
326103285Sikob	/* F */ "Address error"
327103285Sikob};
328103285Sikob
329103285Sikobstatic void
330103285Sikobsbp_identify(driver_t *driver, device_t parent)
331103285Sikob{
332103285Sikob	device_t child;
333103285SikobSBP_DEBUG(0)
334103285Sikob	printf("sbp_identify\n");
335103285SikobEND_DEBUG
336103285Sikob
337103285Sikob	child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent));
338103285Sikob}
339103285Sikob
340103285Sikob/*
341103285Sikob * sbp_probe()
342103285Sikob */
343103285Sikobstatic int
344103285Sikobsbp_probe(device_t dev)
345103285Sikob{
346103285Sikob	device_t pa;
347103285Sikob
348103285SikobSBP_DEBUG(0)
349103285Sikob	printf("sbp_probe\n");
350103285SikobEND_DEBUG
351103285Sikob
352103285Sikob	pa = device_get_parent(dev);
353103285Sikob	if(device_get_unit(dev) != device_get_unit(pa)){
354103285Sikob		return(ENXIO);
355103285Sikob	}
356103285Sikob
357120660Ssimokawa	device_set_desc(dev, "SBP-2/SCSI over FireWire");
358111615Ssimokawa
359132432Ssimokawa#if 0
360111615Ssimokawa	if (bootverbose)
361111615Ssimokawa		debug = bootverbose;
362132432Ssimokawa#endif
363132432Ssimokawa
364103285Sikob	return (0);
365103285Sikob}
366103285Sikob
367103285Sikobstatic void
368103285Sikobsbp_show_sdev_info(struct sbp_dev *sdev, int new)
369103285Sikob{
370103285Sikob	struct fw_device *fwdev;
371103285Sikob
372103285Sikob	printf("%s:%d:%d ",
373103285Sikob		device_get_nameunit(sdev->target->sbp->fd.dev),
374103285Sikob		sdev->target->target_id,
375103285Sikob		sdev->lun_id
376103285Sikob	);
377103285Sikob	if (new == 2) {
378103285Sikob		return;
379103285Sikob	}
380103285Sikob	fwdev = sdev->target->fwdev;
381103285Sikob	printf("ordered:%d type:%d EUI:%08x%08x node:%d "
382103285Sikob		"speed:%d maxrec:%d",
383108503Ssimokawa		(sdev->type & 0x40) >> 6,
384108503Ssimokawa		(sdev->type & 0x1f),
385103285Sikob		fwdev->eui.hi,
386103285Sikob		fwdev->eui.lo,
387103285Sikob		fwdev->dst,
388103285Sikob		fwdev->speed,
389103285Sikob		fwdev->maxrec
390103285Sikob	);
391103285Sikob	if (new)
392103285Sikob		printf(" new!\n");
393103285Sikob	else
394103285Sikob		printf("\n");
395103285Sikob	sbp_show_sdev_info(sdev, 2);
396103285Sikob	printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision);
397103285Sikob}
398103285Sikob
399110184Ssimokawastatic struct {
400110184Ssimokawa	int bus;
401110184Ssimokawa	int target;
402110184Ssimokawa	struct fw_eui64 eui;
403110184Ssimokawa} wired[] = {
404110184Ssimokawa	/* Bus	Target	EUI64 */
405110184Ssimokawa#if 0
406110184Ssimokawa	{0,	2,	{0x00018ea0, 0x01fd0154}},	/* Logitec HDD */
407110184Ssimokawa	{0,	0,	{0x00018ea6, 0x00100682}},	/* Logitec DVD */
408110184Ssimokawa	{0,	1,	{0x00d03200, 0xa412006a}},	/* Yano HDD */
409110184Ssimokawa#endif
410110184Ssimokawa	{-1,	-1,	{0,0}}
411110184Ssimokawa};
412110184Ssimokawa
413110184Ssimokawastatic int
414110184Ssimokawasbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev)
415110184Ssimokawa{
416110184Ssimokawa	int bus, i, target=-1;
417110184Ssimokawa	char w[SBP_NUM_TARGETS];
418110184Ssimokawa
419110184Ssimokawa	bzero(w, sizeof(w));
420110184Ssimokawa	bus = device_get_unit(sbp->fd.dev);
421110184Ssimokawa
422110184Ssimokawa	/* XXX wired-down configuration should be gotten from
423110184Ssimokawa					tunable or device hint */
424110184Ssimokawa	for (i = 0; wired[i].bus >= 0; i ++) {
425110184Ssimokawa		if (wired[i].bus == bus) {
426110184Ssimokawa			w[wired[i].target] = 1;
427110184Ssimokawa			if (wired[i].eui.hi == fwdev->eui.hi &&
428110184Ssimokawa					wired[i].eui.lo == fwdev->eui.lo)
429110184Ssimokawa				target = wired[i].target;
430110184Ssimokawa		}
431110184Ssimokawa	}
432110184Ssimokawa	if (target >= 0) {
433110184Ssimokawa		if(target < SBP_NUM_TARGETS &&
434110184Ssimokawa				sbp->targets[target].fwdev == NULL)
435110184Ssimokawa			return(target);
436110184Ssimokawa		device_printf(sbp->fd.dev,
437110184Ssimokawa			"target %d is not free for %08x:%08x\n",
438110184Ssimokawa			target, fwdev->eui.hi, fwdev->eui.lo);
439110184Ssimokawa		target = -1;
440110184Ssimokawa	}
441110184Ssimokawa	/* non-wired target */
442110184Ssimokawa	for (i = 0; i < SBP_NUM_TARGETS; i ++)
443110184Ssimokawa		if (sbp->targets[i].fwdev == NULL && w[i] == 0) {
444110184Ssimokawa			target = i;
445110184Ssimokawa			break;
446110184Ssimokawa		}
447110184Ssimokawa
448110184Ssimokawa	return target;
449110184Ssimokawa}
450110184Ssimokawa
451121185Ssimokawastatic void
452121185Ssimokawasbp_alloc_lun(struct sbp_target *target)
453103285Sikob{
454108503Ssimokawa	struct crom_context cc;
455108503Ssimokawa	struct csrreg *reg;
456121185Ssimokawa	struct sbp_dev *sdev, **newluns;
457121185Ssimokawa	struct sbp_softc *sbp;
458121185Ssimokawa	int maxlun, lun, i;
459103285Sikob
460121185Ssimokawa	sbp = target->sbp;
461114069Ssimokawa	crom_init_context(&cc, target->fwdev->csrrom);
462108503Ssimokawa	/* XXX shoud parse appropriate unit directories only */
463108503Ssimokawa	maxlun = -1;
464108503Ssimokawa	while (cc.depth >= 0) {
465108503Ssimokawa		reg = crom_search_key(&cc, CROM_LUN);
466108503Ssimokawa		if (reg == NULL)
467108503Ssimokawa			break;
468110839Ssimokawa		lun = reg->val & 0xffff;
469108642SsimokawaSBP_DEBUG(0)
470108642Ssimokawa		printf("target %d lun %d found\n", target->target_id, lun);
471108642SsimokawaEND_DEBUG
472108503Ssimokawa		if (maxlun < lun)
473108503Ssimokawa			maxlun = lun;
474108503Ssimokawa		crom_next(&cc);
475108503Ssimokawa	}
476110839Ssimokawa	if (maxlun < 0)
477121185Ssimokawa		printf("%s:%d no LUN found\n",
478121185Ssimokawa		    device_get_nameunit(target->sbp->fd.dev),
479121185Ssimokawa		    target->target_id);
480121185Ssimokawa
481121185Ssimokawa	maxlun ++;
482110839Ssimokawa	if (maxlun >= SBP_NUM_LUNS)
483110839Ssimokawa		maxlun = SBP_NUM_LUNS;
484121185Ssimokawa
485121185Ssimokawa	/* Invalidiate stale devices */
486121185Ssimokawa	for (lun = 0; lun < target->num_lun; lun ++) {
487121185Ssimokawa		sdev = target->luns[lun];
488121185Ssimokawa		if (sdev == NULL)
489121185Ssimokawa			continue;
490121185Ssimokawa		sdev->flags &= ~VALID_LUN;
491121185Ssimokawa		if (lun >= maxlun) {
492121185Ssimokawa			/* lost device */
493121185Ssimokawa			sbp_cam_detach_sdev(sdev);
494121185Ssimokawa			sbp_free_sdev(sdev);
495121185Ssimokawa		}
496103285Sikob	}
497121185Ssimokawa
498121185Ssimokawa	/* Reallocate */
499121185Ssimokawa	if (maxlun != target->num_lun) {
500121185Ssimokawa		newluns = (struct sbp_dev **) realloc(target->luns,
501121185Ssimokawa		    sizeof(struct sbp_dev *) * maxlun,
502121185Ssimokawa		    M_SBP, M_NOWAIT | M_ZERO);
503121185Ssimokawa
504121185Ssimokawa		if (newluns == NULL) {
505127468Ssimokawa			printf("%s: realloc failed\n", __func__);
506121185Ssimokawa			newluns = target->luns;
507121185Ssimokawa			maxlun = target->num_lun;
508121185Ssimokawa		}
509121185Ssimokawa
510121185Ssimokawa		/*
511121185Ssimokawa		 * We must zero the extended region for the case
512121185Ssimokawa		 * realloc() doesn't allocate new buffer.
513121185Ssimokawa		 */
514121185Ssimokawa		if (maxlun > target->num_lun)
515121185Ssimokawa			bzero(&newluns[target->num_lun],
516121185Ssimokawa			    sizeof(struct sbp_dev *) *
517121185Ssimokawa			    (maxlun - target->num_lun));
518121185Ssimokawa
519121185Ssimokawa		target->luns = newluns;
520121185Ssimokawa		target->num_lun = maxlun;
521121185Ssimokawa	}
522121185Ssimokawa
523108503Ssimokawa	crom_init_context(&cc, target->fwdev->csrrom);
524108503Ssimokawa	while (cc.depth >= 0) {
525121185Ssimokawa		int new = 0;
526121185Ssimokawa
527108503Ssimokawa		reg = crom_search_key(&cc, CROM_LUN);
528108503Ssimokawa		if (reg == NULL)
529108503Ssimokawa			break;
530110839Ssimokawa		lun = reg->val & 0xffff;
531110839Ssimokawa		if (lun >= SBP_NUM_LUNS) {
532110839Ssimokawa			printf("too large lun %d\n", lun);
533121185Ssimokawa			goto next;
534110839Ssimokawa		}
535121185Ssimokawa
536121185Ssimokawa		sdev = target->luns[lun];
537121185Ssimokawa		if (sdev == NULL) {
538121185Ssimokawa			sdev = malloc(sizeof(struct sbp_dev),
539121185Ssimokawa			    M_SBP, M_NOWAIT | M_ZERO);
540121185Ssimokawa			if (sdev == NULL) {
541127468Ssimokawa				printf("%s: malloc failed\n", __func__);
542121185Ssimokawa				goto next;
543121185Ssimokawa			}
544121185Ssimokawa			target->luns[lun] = sdev;
545121185Ssimokawa			sdev->lun_id = lun;
546121185Ssimokawa			sdev->target = target;
547121185Ssimokawa			STAILQ_INIT(&sdev->ocbs);
548121185Ssimokawa			CALLOUT_INIT(&sdev->login_callout);
549121185Ssimokawa			sdev->status = SBP_DEV_RESET;
550121185Ssimokawa			new = 1;
551121185Ssimokawa		}
552121185Ssimokawa		sdev->flags |= VALID_LUN;
553119196Ssimokawa		sdev->type = (reg->val & 0xff0000) >> 16;
554113584Ssimokawa
555121185Ssimokawa		if (new == 0)
556121185Ssimokawa			goto next;
557121185Ssimokawa
558113584Ssimokawa		fwdma_malloc(sbp->fd.fc,
559129585Sdfr			/* alignment */ sizeof(uint32_t),
560113584Ssimokawa			SBP_DMA_SIZE, &sdev->dma, BUS_DMA_NOWAIT);
561113584Ssimokawa		if (sdev->dma.v_addr == NULL) {
562113584Ssimokawa			printf("%s: dma space allocation failed\n",
563127468Ssimokawa							__func__);
564121185Ssimokawa			free(sdev, M_SBP);
565121185Ssimokawa			target->luns[lun] = NULL;
566121185Ssimokawa			goto next;
567113584Ssimokawa		}
568113584Ssimokawa		sdev->login = (struct sbp_login_res *) sdev->dma.v_addr;
569113584Ssimokawa		sdev->ocb = (struct sbp_ocb *)
570113584Ssimokawa				((char *)sdev->dma.v_addr + SBP_LOGIN_SIZE);
571113584Ssimokawa		bzero((char *)sdev->ocb,
572113584Ssimokawa			sizeof (struct sbp_ocb) * SBP_QUEUE_LEN);
573113584Ssimokawa
574113584Ssimokawa		STAILQ_INIT(&sdev->free_ocbs);
575113584Ssimokawa		for (i = 0; i < SBP_QUEUE_LEN; i++) {
576113584Ssimokawa			struct sbp_ocb *ocb;
577113584Ssimokawa			ocb = &sdev->ocb[i];
578113584Ssimokawa			ocb->bus_addr = sdev->dma.bus_addr
579113584Ssimokawa				+ SBP_LOGIN_SIZE
580113584Ssimokawa				+ sizeof(struct sbp_ocb) * i
581113584Ssimokawa				+ offsetof(struct sbp_ocb, orb[0]);
582113584Ssimokawa			if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
583113584Ssimokawa				printf("sbp_attach: cannot create dmamap\n");
584121185Ssimokawa				/* XXX */
585121185Ssimokawa				goto next;
586113584Ssimokawa			}
587113584Ssimokawa			sbp_free_ocb(sdev, ocb);
588113584Ssimokawa		}
589121185Ssimokawanext:
590108503Ssimokawa		crom_next(&cc);
591103285Sikob	}
592121185Ssimokawa
593121185Ssimokawa	for (lun = 0; lun < target->num_lun; lun ++) {
594121185Ssimokawa		sdev = target->luns[lun];
595121185Ssimokawa		if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) {
596121185Ssimokawa			sbp_cam_detach_sdev(sdev);
597121185Ssimokawa			sbp_free_sdev(sdev);
598121185Ssimokawa			target->luns[lun] = NULL;
599121185Ssimokawa		}
600121185Ssimokawa	}
601121185Ssimokawa}
602121185Ssimokawa
603121185Ssimokawastatic struct sbp_target *
604121185Ssimokawasbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
605121185Ssimokawa{
606121185Ssimokawa	int i;
607121185Ssimokawa	struct sbp_target *target;
608121185Ssimokawa	struct crom_context cc;
609121185Ssimokawa	struct csrreg *reg;
610121185Ssimokawa
611121185SsimokawaSBP_DEBUG(1)
612121185Ssimokawa	printf("sbp_alloc_target\n");
613121185SsimokawaEND_DEBUG
614121185Ssimokawa	i = sbp_new_target(sbp, fwdev);
615121185Ssimokawa	if (i < 0) {
616121185Ssimokawa		device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n");
617121185Ssimokawa		return NULL;
618121185Ssimokawa	}
619121185Ssimokawa	/* new target */
620121185Ssimokawa	target = &sbp->targets[i];
621121185Ssimokawa	target->sbp = sbp;
622121185Ssimokawa	target->fwdev = fwdev;
623121185Ssimokawa	target->target_id = i;
624121185Ssimokawa	/* XXX we may want to reload mgm port after each bus reset */
625121185Ssimokawa	/* XXX there might be multiple management agents */
626121185Ssimokawa	crom_init_context(&cc, target->fwdev->csrrom);
627121185Ssimokawa	reg = crom_search_key(&cc, CROM_MGM);
628121185Ssimokawa	if (reg == NULL || reg->val == 0) {
629121185Ssimokawa		printf("NULL management address\n");
630121185Ssimokawa		target->fwdev = NULL;
631121185Ssimokawa		return NULL;
632121185Ssimokawa	}
633121185Ssimokawa	target->mgm_hi = 0xffff;
634121185Ssimokawa	target->mgm_lo = 0xf0000000 | (reg->val << 2);
635121185Ssimokawa	target->mgm_ocb_cur = NULL;
636121185SsimokawaSBP_DEBUG(1)
637121185Ssimokawa	printf("target:%d mgm_port: %x\n", i, target->mgm_lo);
638121185SsimokawaEND_DEBUG
639121185Ssimokawa	STAILQ_INIT(&target->xferlist);
640121185Ssimokawa	target->n_xfer = 0;
641121185Ssimokawa	STAILQ_INIT(&target->mgm_ocb_queue);
642121185Ssimokawa	CALLOUT_INIT(&target->mgm_ocb_timeout);
643121185Ssimokawa	CALLOUT_INIT(&target->scan_callout);
644121185Ssimokawa
645121185Ssimokawa	target->luns = NULL;
646121185Ssimokawa	target->num_lun = 0;
647114069Ssimokawa	return target;
648103285Sikob}
649103285Sikob
650103285Sikobstatic void
651103285Sikobsbp_probe_lun(struct sbp_dev *sdev)
652103285Sikob{
653103285Sikob	struct fw_device *fwdev;
654114069Ssimokawa	struct crom_context c, *cc = &c;
655114069Ssimokawa	struct csrreg *reg;
656103285Sikob
657103285Sikob	bzero(sdev->vendor, sizeof(sdev->vendor));
658103285Sikob	bzero(sdev->product, sizeof(sdev->product));
659114069Ssimokawa
660114069Ssimokawa	fwdev = sdev->target->fwdev;
661114069Ssimokawa	crom_init_context(cc, fwdev->csrrom);
662114069Ssimokawa	/* get vendor string */
663114069Ssimokawa	crom_search_key(cc, CSRKEY_VENDOR);
664114069Ssimokawa	crom_next(cc);
665114069Ssimokawa	crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor));
666114223Ssimokawa	/* skip to the unit directory for SBP-2 */
667114260Ssimokawa	while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) {
668114223Ssimokawa		if (reg->val == CSRVAL_T10SBP2)
669114223Ssimokawa			break;
670114260Ssimokawa		crom_next(cc);
671114260Ssimokawa	}
672114069Ssimokawa	/* get firmware revision */
673114069Ssimokawa	reg = crom_search_key(cc, CSRKEY_FIRM_VER);
674114069Ssimokawa	if (reg != NULL)
675114069Ssimokawa		snprintf(sdev->revision, sizeof(sdev->revision),
676114069Ssimokawa						"%06x", reg->val);
677114069Ssimokawa	/* get product string */
678114069Ssimokawa	crom_search_key(cc, CSRKEY_MODEL);
679114069Ssimokawa	crom_next(cc);
680114069Ssimokawa	crom_parse_text(cc, sdev->product, sizeof(sdev->product));
681103285Sikob}
682111615Ssimokawa
683103285Sikobstatic void
684111615Ssimokawasbp_login_callout(void *arg)
685103285Sikob{
686111615Ssimokawa	struct sbp_dev *sdev = (struct sbp_dev *)arg;
687111615Ssimokawa	sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
688111615Ssimokawa}
689111615Ssimokawa
690122387Ssimokawastatic void
691122387Ssimokawasbp_login(struct sbp_dev *sdev)
692122387Ssimokawa{
693122387Ssimokawa	struct timeval delta;
694122387Ssimokawa	struct timeval t;
695122387Ssimokawa	int ticks = 0;
696122387Ssimokawa
697122387Ssimokawa	microtime(&delta);
698122387Ssimokawa	timevalsub(&delta, &sdev->target->sbp->last_busreset);
699122387Ssimokawa	t.tv_sec = login_delay / 1000;
700122387Ssimokawa	t.tv_usec = (login_delay % 1000) * 1000;
701122387Ssimokawa	timevalsub(&t, &delta);
702122387Ssimokawa	if (t.tv_sec >= 0 && t.tv_usec > 0)
703122387Ssimokawa		ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000;
704122387SsimokawaSBP_DEBUG(0)
705153706Strhodes	printf("%s: sec = %jd usec = %ld ticks = %d\n", __func__,
706153706Strhodes	    (intmax_t)t.tv_sec, t.tv_usec, ticks);
707122387SsimokawaEND_DEBUG
708122387Ssimokawa	callout_reset(&sdev->login_callout, ticks,
709122387Ssimokawa			sbp_login_callout, (void *)(sdev));
710122387Ssimokawa}
711122387Ssimokawa
712114069Ssimokawa#define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
713114069Ssimokawa	&& crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
714111615Ssimokawa
715111615Ssimokawastatic void
716111615Ssimokawasbp_probe_target(void *arg)
717111615Ssimokawa{
718111615Ssimokawa	struct sbp_target *target = (struct sbp_target *)arg;
719103285Sikob	struct sbp_softc *sbp;
720103285Sikob	struct sbp_dev *sdev;
721103285Sikob	struct firewire_comm *fc;
722111615Ssimokawa	int i, alive;
723103285Sikob
724111615Ssimokawa	alive = SBP_FWDEV_ALIVE(target->fwdev);
725103285SikobSBP_DEBUG(1)
726103285Sikob	printf("sbp_probe_target %d\n", target->target_id);
727103285Sikob	if (!alive)
728103285Sikob		printf("not alive\n");
729103285SikobEND_DEBUG
730103285Sikob
731103285Sikob	sbp = target->sbp;
732103285Sikob	fc = target->sbp->fd.fc;
733121185Ssimokawa	sbp_alloc_lun(target);
734121185Ssimokawa
735111615Ssimokawa	/* XXX untimeout mgm_ocb and dequeue */
736103285Sikob	for (i=0; i < target->num_lun; i++) {
737121185Ssimokawa		sdev = target->luns[i];
738121185Ssimokawa		if (sdev == NULL)
739121185Ssimokawa			continue;
740103285Sikob		if (alive && (sdev->status != SBP_DEV_DEAD)) {
741103285Sikob			if (sdev->path != NULL) {
742170374Ssimokawa				SBP_LOCK(sbp);
743103285Sikob				xpt_freeze_devq(sdev->path, 1);
744111615Ssimokawa				sdev->freeze ++;
745170374Ssimokawa				SBP_UNLOCK(sbp);
746103285Sikob			}
747111615Ssimokawa			sbp_probe_lun(sdev);
748111615SsimokawaSBP_DEBUG(0)
749111615Ssimokawa			sbp_show_sdev_info(sdev,
750111615Ssimokawa					(sdev->status == SBP_DEV_RESET));
751111615SsimokawaEND_DEBUG
752111615Ssimokawa
753111615Ssimokawa			sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
754103285Sikob			switch (sdev->status) {
755110336Ssimokawa			case SBP_DEV_RESET:
756103285Sikob				/* new or revived target */
757113584Ssimokawa				if (auto_login)
758111615Ssimokawa					sbp_login(sdev);
759103285Sikob				break;
760111615Ssimokawa			case SBP_DEV_TOATTACH:
761111615Ssimokawa			case SBP_DEV_PROBE:
762111615Ssimokawa			case SBP_DEV_ATTACHED:
763110336Ssimokawa			case SBP_DEV_RETRY:
764110336Ssimokawa			default:
765111615Ssimokawa				sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
766110336Ssimokawa				break;
767103285Sikob			}
768103285Sikob		} else {
769103285Sikob			switch (sdev->status) {
770103285Sikob			case SBP_DEV_ATTACHED:
771103285SikobSBP_DEBUG(0)
772103285Sikob				/* the device has gone */
773103285Sikob				sbp_show_sdev_info(sdev, 2);
774103285Sikob				printf("lost target\n");
775103285SikobEND_DEBUG
776111615Ssimokawa				if (sdev->path) {
777170374Ssimokawa					SBP_LOCK(sbp);
778103285Sikob					xpt_freeze_devq(sdev->path, 1);
779111615Ssimokawa					sdev->freeze ++;
780170374Ssimokawa					SBP_UNLOCK(sbp);
781111615Ssimokawa				}
782103285Sikob				sdev->status = SBP_DEV_RETRY;
783111615Ssimokawa				sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
784103285Sikob				break;
785103285Sikob			case SBP_DEV_PROBE:
786103285Sikob			case SBP_DEV_TOATTACH:
787103285Sikob				sdev->status = SBP_DEV_RESET;
788103285Sikob				break;
789103285Sikob			case SBP_DEV_RETRY:
790103285Sikob			case SBP_DEV_RESET:
791103285Sikob			case SBP_DEV_DEAD:
792103285Sikob				break;
793103285Sikob			}
794103285Sikob		}
795103285Sikob	}
796103285Sikob}
797103285Sikob
798103285Sikobstatic void
799113584Ssimokawasbp_post_busreset(void *arg)
800113584Ssimokawa{
801113584Ssimokawa	struct sbp_softc *sbp;
802113584Ssimokawa
803113584Ssimokawa	sbp = (struct sbp_softc *)arg;
804113584SsimokawaSBP_DEBUG(0)
805113584Ssimokawa	printf("sbp_post_busreset\n");
806113584SsimokawaEND_DEBUG
807124145Ssimokawa	if ((sbp->sim->flags & SIMQ_FREEZED) == 0) {
808170374Ssimokawa		SBP_LOCK(sbp);
809124145Ssimokawa		xpt_freeze_simq(sbp->sim, /*count*/1);
810124145Ssimokawa		sbp->sim->flags |= SIMQ_FREEZED;
811170374Ssimokawa		SBP_UNLOCK(sbp);
812124145Ssimokawa	}
813122387Ssimokawa	microtime(&sbp->last_busreset);
814113584Ssimokawa}
815113584Ssimokawa
816113584Ssimokawastatic void
817103285Sikobsbp_post_explore(void *arg)
818103285Sikob{
819103285Sikob	struct sbp_softc *sbp = (struct sbp_softc *)arg;
820103285Sikob	struct sbp_target *target;
821103285Sikob	struct fw_device *fwdev;
822103285Sikob	int i, alive;
823103285Sikob
824111199SsimokawaSBP_DEBUG(0)
825111199Ssimokawa	printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
826103285SikobEND_DEBUG
827122387Ssimokawa	if (sbp_cold > 0)
828122387Ssimokawa		sbp_cold --;
829122387Ssimokawa
830122387Ssimokawa#if 0
831122387Ssimokawa	/*
832122387Ssimokawa	 * XXX don't let CAM the bus rest.
833122387Ssimokawa	 * CAM tries to do something with freezed (DEV_RETRY) devices.
834111615Ssimokawa	 */
835111615Ssimokawa	xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
836103285Sikob#endif
837111615Ssimokawa
838130532Sdfr	/* Garbage Collection */
839103285Sikob	for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
840103285Sikob		target = &sbp->targets[i];
841110193Ssimokawa		STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
842110193Ssimokawa			if (target->fwdev == NULL || target->fwdev == fwdev)
843110193Ssimokawa				break;
844121185Ssimokawa		if (fwdev == NULL) {
845103285Sikob			/* device has removed in lower driver */
846110145Ssimokawa			sbp_cam_detach_target(target);
847121185Ssimokawa			sbp_free_target(target);
848103285Sikob		}
849103285Sikob	}
850103285Sikob	/* traverse device list */
851110193Ssimokawa	STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
852103285SikobSBP_DEBUG(0)
853103285Sikob		printf("sbp_post_explore: EUI:%08x%08x ",
854103285Sikob				fwdev->eui.hi, fwdev->eui.lo);
855114069Ssimokawa		if (fwdev->status != FWDEVATTACHED)
856103285Sikob			printf("not attached, state=%d.\n", fwdev->status);
857114069Ssimokawa		else
858114069Ssimokawa			printf("attached\n");
859103285SikobEND_DEBUG
860111615Ssimokawa		alive = SBP_FWDEV_ALIVE(fwdev);
861103285Sikob		for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
862103285Sikob			target = &sbp->targets[i];
863103285Sikob			if(target->fwdev == fwdev ) {
864103285Sikob				/* known target */
865103285Sikob				break;
866103285Sikob			}
867103285Sikob		}
868103285Sikob		if(i == SBP_NUM_TARGETS){
869103285Sikob			if (alive) {
870103285Sikob				/* new target */
871103285Sikob				target = sbp_alloc_target(sbp, fwdev);
872103285Sikob				if (target == NULL)
873103285Sikob					continue;
874103285Sikob			} else {
875103285Sikob				continue;
876103285Sikob			}
877103285Sikob		}
878111615Ssimokawa		sbp_probe_target((void *)target);
879121185Ssimokawa		if (target->num_lun == 0)
880121185Ssimokawa			sbp_free_target(target);
881103285Sikob	}
882170374Ssimokawa	SBP_LOCK(sbp);
883124145Ssimokawa	xpt_release_simq(sbp->sim, /*run queue*/TRUE);
884124145Ssimokawa	sbp->sim->flags &= ~SIMQ_FREEZED;
885170374Ssimokawa	SBP_UNLOCK(sbp);
886103285Sikob}
887103285Sikob
888103285Sikob#if NEED_RESPONSE
889103285Sikobstatic void
890103285Sikobsbp_loginres_callback(struct fw_xfer *xfer){
891113584Ssimokawa	int s;
892103285Sikob	struct sbp_dev *sdev;
893103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
894113584SsimokawaSBP_DEBUG(1)
895103285Sikob	sbp_show_sdev_info(sdev, 2);
896103285Sikob	printf("sbp_loginres_callback\n");
897103285SikobEND_DEBUG
898113584Ssimokawa	/* recycle */
899113584Ssimokawa	s = splfw();
900113584Ssimokawa	STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link);
901113584Ssimokawa	splx(s);
902103285Sikob	return;
903103285Sikob}
904103285Sikob#endif
905103285Sikob
906113584Ssimokawastatic __inline void
907113584Ssimokawasbp_xfer_free(struct fw_xfer *xfer)
908113584Ssimokawa{
909113584Ssimokawa	struct sbp_dev *sdev;
910113584Ssimokawa	int s;
911113584Ssimokawa
912113584Ssimokawa	sdev = (struct sbp_dev *)xfer->sc;
913113584Ssimokawa	fw_xfer_unload(xfer);
914113584Ssimokawa	s = splfw();
915170374Ssimokawa	SBP_LOCK(sdev->target->sbp);
916113584Ssimokawa	STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
917170374Ssimokawa	SBP_UNLOCK(sdev->target->sbp);
918113584Ssimokawa	splx(s);
919113584Ssimokawa}
920113584Ssimokawa
921103285Sikobstatic void
922114732Ssimokawasbp_reset_start_callback(struct fw_xfer *xfer)
923103285Sikob{
924114732Ssimokawa	struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
925114732Ssimokawa	struct sbp_target *target = sdev->target;
926114732Ssimokawa	int i;
927114732Ssimokawa
928114732Ssimokawa	if (xfer->resp != 0) {
929114732Ssimokawa		sbp_show_sdev_info(sdev, 2);
930114732Ssimokawa		printf("sbp_reset_start failed: resp=%d\n", xfer->resp);
931114732Ssimokawa	}
932114732Ssimokawa
933114732Ssimokawa	for (i = 0; i < target->num_lun; i++) {
934121185Ssimokawa		tsdev = target->luns[i];
935121185Ssimokawa		if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN)
936121185Ssimokawa			sbp_login(tsdev);
937114732Ssimokawa	}
938114732Ssimokawa}
939114732Ssimokawa
940114732Ssimokawastatic void
941114732Ssimokawasbp_reset_start(struct sbp_dev *sdev)
942114732Ssimokawa{
943114732Ssimokawa	struct fw_xfer *xfer;
944114732Ssimokawa	struct fw_pkt *fp;
945114732Ssimokawa
946114732SsimokawaSBP_DEBUG(0)
947114732Ssimokawa	sbp_show_sdev_info(sdev, 2);
948114732Ssimokawa	printf("sbp_reset_start\n");
949114732SsimokawaEND_DEBUG
950114732Ssimokawa
951114732Ssimokawa	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
952167632Ssimokawa	xfer->hand = sbp_reset_start_callback;
953120660Ssimokawa	fp = &xfer->send.hdr;
954114732Ssimokawa	fp->mode.wreqq.dest_hi = 0xffff;
955114732Ssimokawa	fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
956114732Ssimokawa	fp->mode.wreqq.data = htonl(0xf);
957114732Ssimokawa	fw_asyreq(xfer->fc, -1, xfer);
958114732Ssimokawa}
959114732Ssimokawa
960114732Ssimokawastatic void
961114732Ssimokawasbp_mgm_callback(struct fw_xfer *xfer)
962114732Ssimokawa{
963103285Sikob	struct sbp_dev *sdev;
964114732Ssimokawa	int resp;
965114732Ssimokawa
966103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
967114732Ssimokawa
968114732SsimokawaSBP_DEBUG(1)
969103285Sikob	sbp_show_sdev_info(sdev, 2);
970114732Ssimokawa	printf("sbp_mgm_callback\n");
971103285SikobEND_DEBUG
972114732Ssimokawa	resp = xfer->resp;
973113584Ssimokawa	sbp_xfer_free(xfer);
974114732Ssimokawa#if 0
975114732Ssimokawa	if (resp != 0) {
976114732Ssimokawa		sbp_show_sdev_info(sdev, 2);
977114732Ssimokawa		printf("management ORB failed(%d) ... RESET_START\n", resp);
978114732Ssimokawa		sbp_reset_start(sdev);
979114732Ssimokawa	}
980114732Ssimokawa#endif
981103285Sikob	return;
982103285Sikob}
983103285Sikob
984111615Ssimokawastatic struct sbp_dev *
985111615Ssimokawasbp_next_dev(struct sbp_target *target, int lun)
986111615Ssimokawa{
987121185Ssimokawa	struct sbp_dev **sdevp;
988111615Ssimokawa	int i;
989111615Ssimokawa
990121185Ssimokawa	for (i = lun, sdevp = &target->luns[lun]; i < target->num_lun;
991121185Ssimokawa	    i++, sdevp++)
992121185Ssimokawa		if (*sdevp != NULL && (*sdevp)->status == SBP_DEV_PROBE)
993121185Ssimokawa			return(*sdevp);
994121185Ssimokawa	return(NULL);
995111615Ssimokawa}
996111615Ssimokawa
997111615Ssimokawa#define SCAN_PRI 1
998103285Sikobstatic void
999111615Ssimokawasbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb)
1000103285Sikob{
1001111615Ssimokawa	struct sbp_target *target;
1002103285Sikob	struct sbp_dev *sdev;
1003111615Ssimokawa
1004103285Sikob	sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
1005111615Ssimokawa	target = sdev->target;
1006110269SsimokawaSBP_DEBUG(0)
1007103285Sikob	sbp_show_sdev_info(sdev, 2);
1008111615Ssimokawa	printf("sbp_cam_scan_lun\n");
1009103285SikobEND_DEBUG
1010111615Ssimokawa	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1011111615Ssimokawa		sdev->status = SBP_DEV_ATTACHED;
1012111615Ssimokawa	} else {
1013111615Ssimokawa		sbp_show_sdev_info(sdev, 2);
1014111615Ssimokawa		printf("scan failed\n");
1015111615Ssimokawa	}
1016111615Ssimokawa	sdev = sbp_next_dev(target, sdev->lun_id + 1);
1017111615Ssimokawa	if (sdev == NULL) {
1018111615Ssimokawa		free(ccb, M_SBP);
1019111615Ssimokawa		return;
1020111615Ssimokawa	}
1021111615Ssimokawa	/* reuse ccb */
1022111615Ssimokawa	xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
1023111615Ssimokawa	ccb->ccb_h.ccb_sdev_ptr = sdev;
1024111615Ssimokawa	xpt_action(ccb);
1025111615Ssimokawa	xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1026111615Ssimokawa	sdev->freeze = 1;
1027103285Sikob}
1028103285Sikob
1029103285Sikobstatic void
1030111615Ssimokawasbp_cam_scan_target(void *arg)
1031103285Sikob{
1032111615Ssimokawa	struct sbp_target *target = (struct sbp_target *)arg;
1033111615Ssimokawa	struct sbp_dev *sdev;
1034110798Ssimokawa	union ccb *ccb;
1035103285Sikob
1036111615Ssimokawa	sdev = sbp_next_dev(target, 0);
1037111615Ssimokawa	if (sdev == NULL) {
1038111615Ssimokawa		printf("sbp_cam_scan_target: nothing to do for target%d\n",
1039111615Ssimokawa							target->target_id);
1040110798Ssimokawa		return;
1041110798Ssimokawa	}
1042103285SikobSBP_DEBUG(0)
1043103285Sikob	sbp_show_sdev_info(sdev, 2);
1044111615Ssimokawa	printf("sbp_cam_scan_target\n");
1045103285SikobEND_DEBUG
1046111615Ssimokawa	ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO);
1047111615Ssimokawa	if (ccb == NULL) {
1048111615Ssimokawa		printf("sbp_cam_scan_target: malloc failed\n");
1049111615Ssimokawa		return;
1050111615Ssimokawa	}
1051111615Ssimokawa	xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
1052103285Sikob	ccb->ccb_h.func_code = XPT_SCAN_LUN;
1053111615Ssimokawa	ccb->ccb_h.cbfcnp = sbp_cam_scan_lun;
1054111615Ssimokawa	ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1055103285Sikob	ccb->crcn.flags = CAM_FLAG_NONE;
1056103285Sikob	ccb->ccb_h.ccb_sdev_ptr = sdev;
1057103285Sikob
1058103285Sikob	/* The scan is in progress now. */
1059170374Ssimokawa	SBP_LOCK(target->sbp);
1060111040Ssimokawa	xpt_action(ccb);
1061111615Ssimokawa	xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1062111615Ssimokawa	sdev->freeze = 1;
1063170374Ssimokawa	SBP_UNLOCK(target->sbp);
1064103285Sikob}
1065103285Sikob
1066111615Ssimokawastatic __inline void
1067111615Ssimokawasbp_scan_dev(struct sbp_dev *sdev)
1068111615Ssimokawa{
1069111040Ssimokawa	sdev->status = SBP_DEV_PROBE;
1070122387Ssimokawa	callout_reset(&sdev->target->scan_callout, scan_delay * hz / 1000,
1071111615Ssimokawa			sbp_cam_scan_target, (void *)sdev->target);
1072103285Sikob}
1073103285Sikob
1074103285Sikobstatic void
1075103285Sikobsbp_do_attach(struct fw_xfer *xfer)
1076103285Sikob{
1077103285Sikob	struct sbp_dev *sdev;
1078111615Ssimokawa	struct sbp_target *target;
1079111615Ssimokawa	struct sbp_softc *sbp;
1080103285Sikob
1081103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
1082111615Ssimokawa	target = sdev->target;
1083111615Ssimokawa	sbp = target->sbp;
1084103285SikobSBP_DEBUG(0)
1085103285Sikob	sbp_show_sdev_info(sdev, 2);
1086103285Sikob	printf("sbp_do_attach\n");
1087103285SikobEND_DEBUG
1088113584Ssimokawa	sbp_xfer_free(xfer);
1089111199Ssimokawa
1090103285Sikob	if (sdev->path == NULL)
1091103285Sikob		xpt_create_path(&sdev->path, xpt_periph,
1092111615Ssimokawa			cam_sim_path(target->sbp->sim),
1093111615Ssimokawa			target->target_id, sdev->lun_id);
1094103285Sikob
1095111199Ssimokawa	/*
1096111199Ssimokawa	 * Let CAM scan the bus if we are in the boot process.
1097111199Ssimokawa	 * XXX xpt_scan_bus cannot detect LUN larger than 0
1098111199Ssimokawa	 * if LUN 0 doesn't exists.
1099111199Ssimokawa	 */
1100111199Ssimokawa	if (sbp_cold > 0) {
1101111615Ssimokawa		sdev->status = SBP_DEV_ATTACHED;
1102111199Ssimokawa		return;
1103111199Ssimokawa	}
1104111199Ssimokawa
1105111615Ssimokawa	sbp_scan_dev(sdev);
1106103285Sikob	return;
1107103285Sikob}
1108103285Sikob
1109103285Sikobstatic void
1110103285Sikobsbp_agent_reset_callback(struct fw_xfer *xfer)
1111103285Sikob{
1112103285Sikob	struct sbp_dev *sdev;
1113103285Sikob
1114103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
1115103285SikobSBP_DEBUG(1)
1116103285Sikob	sbp_show_sdev_info(sdev, 2);
1117127468Ssimokawa	printf("%s\n", __func__);
1118103285SikobEND_DEBUG
1119114732Ssimokawa	if (xfer->resp != 0) {
1120114732Ssimokawa		sbp_show_sdev_info(sdev, 2);
1121127468Ssimokawa		printf("%s: resp=%d\n", __func__, xfer->resp);
1122114732Ssimokawa	}
1123114732Ssimokawa
1124113584Ssimokawa	sbp_xfer_free(xfer);
1125111615Ssimokawa	if (sdev->path) {
1126170374Ssimokawa		SBP_LOCK(sdev->target->sbp);
1127111615Ssimokawa		xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1128111615Ssimokawa		sdev->freeze = 0;
1129170374Ssimokawa		SBP_UNLOCK(sdev->target->sbp);
1130111615Ssimokawa	}
1131103285Sikob}
1132103285Sikob
1133103285Sikobstatic void
1134110336Ssimokawasbp_agent_reset(struct sbp_dev *sdev)
1135103285Sikob{
1136103285Sikob	struct fw_xfer *xfer;
1137103285Sikob	struct fw_pkt *fp;
1138103285Sikob
1139103285SikobSBP_DEBUG(0)
1140103285Sikob	sbp_show_sdev_info(sdev, 2);
1141103285Sikob	printf("sbp_agent_reset\n");
1142103285SikobEND_DEBUG
1143103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1144103285Sikob	if (xfer == NULL)
1145103285Sikob		return;
1146115788Ssimokawa	if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE)
1147167632Ssimokawa		xfer->hand = sbp_agent_reset_callback;
1148110336Ssimokawa	else
1149167632Ssimokawa		xfer->hand = sbp_do_attach;
1150120660Ssimokawa	fp = &xfer->send.hdr;
1151103285Sikob	fp->mode.wreqq.data = htonl(0xf);
1152103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1153111615Ssimokawa	sbp_abort_all_ocbs(sdev, CAM_BDR_SENT);
1154103285Sikob}
1155103285Sikob
1156103285Sikobstatic void
1157103285Sikobsbp_busy_timeout_callback(struct fw_xfer *xfer)
1158103285Sikob{
1159103285Sikob	struct sbp_dev *sdev;
1160103285Sikob
1161103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
1162103285SikobSBP_DEBUG(1)
1163103285Sikob	sbp_show_sdev_info(sdev, 2);
1164110336Ssimokawa	printf("sbp_busy_timeout_callback\n");
1165103285SikobEND_DEBUG
1166113584Ssimokawa	sbp_xfer_free(xfer);
1167110336Ssimokawa	sbp_agent_reset(sdev);
1168103285Sikob}
1169103285Sikob
1170103285Sikobstatic void
1171103285Sikobsbp_busy_timeout(struct sbp_dev *sdev)
1172103285Sikob{
1173103285Sikob	struct fw_pkt *fp;
1174103285Sikob	struct fw_xfer *xfer;
1175103285SikobSBP_DEBUG(0)
1176103285Sikob	sbp_show_sdev_info(sdev, 2);
1177103285Sikob	printf("sbp_busy_timeout\n");
1178103285SikobEND_DEBUG
1179103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1180103285Sikob
1181167632Ssimokawa	xfer->hand = sbp_busy_timeout_callback;
1182120660Ssimokawa	fp = &xfer->send.hdr;
1183113584Ssimokawa	fp->mode.wreqq.dest_hi = 0xffff;
1184113584Ssimokawa	fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1185110129Ssimokawa	fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1186103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1187103285Sikob}
1188103285Sikob
1189103285Sikobstatic void
1190121185Ssimokawasbp_orb_pointer_callback(struct fw_xfer *xfer)
1191121185Ssimokawa{
1192121185Ssimokawa	struct sbp_dev *sdev;
1193121185Ssimokawa	sdev = (struct sbp_dev *)xfer->sc;
1194121185Ssimokawa
1195121185SsimokawaSBP_DEBUG(1)
1196121185Ssimokawa	sbp_show_sdev_info(sdev, 2);
1197127468Ssimokawa	printf("%s\n", __func__);
1198121185SsimokawaEND_DEBUG
1199121185Ssimokawa	if (xfer->resp != 0) {
1200121185Ssimokawa		/* XXX */
1201127468Ssimokawa		printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1202121185Ssimokawa	}
1203121185Ssimokawa	sbp_xfer_free(xfer);
1204121185Ssimokawa	sdev->flags &= ~ORB_POINTER_ACTIVE;
1205121185Ssimokawa
1206121185Ssimokawa	if ((sdev->flags & ORB_POINTER_NEED) != 0) {
1207121185Ssimokawa		struct sbp_ocb *ocb;
1208121185Ssimokawa
1209121185Ssimokawa		sdev->flags &= ~ORB_POINTER_NEED;
1210121185Ssimokawa		ocb = STAILQ_FIRST(&sdev->ocbs);
1211121185Ssimokawa		if (ocb != NULL)
1212121185Ssimokawa			sbp_orb_pointer(sdev, ocb);
1213121185Ssimokawa	}
1214121185Ssimokawa	return;
1215121185Ssimokawa}
1216121185Ssimokawa
1217121185Ssimokawastatic void
1218103285Sikobsbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1219103285Sikob{
1220103285Sikob	struct fw_xfer *xfer;
1221103285Sikob	struct fw_pkt *fp;
1222121185SsimokawaSBP_DEBUG(1)
1223103285Sikob	sbp_show_sdev_info(sdev, 2);
1224129585Sdfr	printf("%s: 0x%08x\n", __func__, (uint32_t)ocb->bus_addr);
1225103285SikobEND_DEBUG
1226103285Sikob
1227121185Ssimokawa	if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) {
1228122421SsimokawaSBP_DEBUG(0)
1229127468Ssimokawa		printf("%s: orb pointer active\n", __func__);
1230122421SsimokawaEND_DEBUG
1231121185Ssimokawa		sdev->flags |= ORB_POINTER_NEED;
1232121185Ssimokawa		return;
1233121185Ssimokawa	}
1234121185Ssimokawa
1235121185Ssimokawa	sdev->flags |= ORB_POINTER_ACTIVE;
1236103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1237103285Sikob	if (xfer == NULL)
1238103285Sikob		return;
1239167632Ssimokawa	xfer->hand = sbp_orb_pointer_callback;
1240103285Sikob
1241120660Ssimokawa	fp = &xfer->send.hdr;
1242113584Ssimokawa	fp->mode.wreqb.len = 8;
1243103285Sikob	fp->mode.wreqb.extcode = 0;
1244120660Ssimokawa	xfer->send.payload[0] =
1245103285Sikob		htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1246129585Sdfr	xfer->send.payload[1] = htonl((uint32_t)ocb->bus_addr);
1247103285Sikob
1248103285Sikob	if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1249113584Ssimokawa			sbp_xfer_free(xfer);
1250103285Sikob			ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1251103285Sikob			xpt_done(ocb->ccb);
1252103285Sikob	}
1253103285Sikob}
1254103285Sikob
1255103285Sikobstatic void
1256127468Ssimokawasbp_doorbell_callback(struct fw_xfer *xfer)
1257121185Ssimokawa{
1258121185Ssimokawa	struct sbp_dev *sdev;
1259121185Ssimokawa	sdev = (struct sbp_dev *)xfer->sc;
1260127468Ssimokawa
1261127468SsimokawaSBP_DEBUG(1)
1262121185Ssimokawa	sbp_show_sdev_info(sdev, 2);
1263127468Ssimokawa	printf("sbp_doorbell_callback\n");
1264121185SsimokawaEND_DEBUG
1265121185Ssimokawa	if (xfer->resp != 0) {
1266121185Ssimokawa		/* XXX */
1267127468Ssimokawa		printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1268121185Ssimokawa	}
1269121185Ssimokawa	sbp_xfer_free(xfer);
1270127468Ssimokawa	sdev->flags &= ~ORB_DOORBELL_ACTIVE;
1271127468Ssimokawa	if ((sdev->flags & ORB_DOORBELL_NEED) != 0) {
1272127468Ssimokawa		sdev->flags &= ~ORB_DOORBELL_NEED;
1273127468Ssimokawa		sbp_doorbell(sdev);
1274127468Ssimokawa	}
1275121185Ssimokawa	return;
1276121185Ssimokawa}
1277121185Ssimokawa
1278121185Ssimokawastatic void
1279103285Sikobsbp_doorbell(struct sbp_dev *sdev)
1280103285Sikob{
1281103285Sikob	struct fw_xfer *xfer;
1282103285Sikob	struct fw_pkt *fp;
1283103285SikobSBP_DEBUG(1)
1284103285Sikob	sbp_show_sdev_info(sdev, 2);
1285103285Sikob	printf("sbp_doorbell\n");
1286103285SikobEND_DEBUG
1287103285Sikob
1288127468Ssimokawa	if ((sdev->flags & ORB_DOORBELL_ACTIVE) != 0) {
1289127468Ssimokawa		sdev->flags |= ORB_DOORBELL_NEED;
1290127468Ssimokawa		return;
1291127468Ssimokawa	}
1292127468Ssimokawa	sdev->flags |= ORB_DOORBELL_ACTIVE;
1293103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1294103285Sikob	if (xfer == NULL)
1295103285Sikob		return;
1296167632Ssimokawa	xfer->hand = sbp_doorbell_callback;
1297127468Ssimokawa	fp = &xfer->send.hdr;
1298103285Sikob	fp->mode.wreqq.data = htonl(0xf);
1299103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1300103285Sikob}
1301103285Sikob
1302103285Sikobstatic struct fw_xfer *
1303103285Sikobsbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1304103285Sikob{
1305103285Sikob	struct fw_xfer *xfer;
1306103285Sikob	struct fw_pkt *fp;
1307113584Ssimokawa	struct sbp_target *target;
1308113584Ssimokawa	int s, new = 0;
1309103285Sikob
1310113584Ssimokawa	target = sdev->target;
1311113584Ssimokawa	s = splfw();
1312113584Ssimokawa	xfer = STAILQ_FIRST(&target->xferlist);
1313113584Ssimokawa	if (xfer == NULL) {
1314113584Ssimokawa		if (target->n_xfer > 5 /* XXX */) {
1315113584Ssimokawa			printf("sbp: no more xfer for this target\n");
1316113584Ssimokawa			splx(s);
1317113584Ssimokawa			return(NULL);
1318113584Ssimokawa		}
1319120660Ssimokawa		xfer = fw_xfer_alloc_buf(M_SBP, 8, 0);
1320113584Ssimokawa		if(xfer == NULL){
1321113584Ssimokawa			printf("sbp: fw_xfer_alloc_buf failed\n");
1322113584Ssimokawa			splx(s);
1323113584Ssimokawa			return NULL;
1324113584Ssimokawa		}
1325113584Ssimokawa		target->n_xfer ++;
1326113584Ssimokawa		if (debug)
1327113584Ssimokawa			printf("sbp: alloc %d xfer\n", target->n_xfer);
1328113584Ssimokawa		new = 1;
1329113584Ssimokawa	} else {
1330113584Ssimokawa		STAILQ_REMOVE_HEAD(&target->xferlist, link);
1331103285Sikob	}
1332113584Ssimokawa	splx(s);
1333113584Ssimokawa
1334113584Ssimokawa	microtime(&xfer->tv);
1335113584Ssimokawa
1336113584Ssimokawa	if (new) {
1337120660Ssimokawa		xfer->recv.pay_len = 0;
1338120660Ssimokawa		xfer->send.spd = min(sdev->target->fwdev->speed, max_speed);
1339113584Ssimokawa		xfer->fc = sdev->target->sbp->fd.fc;
1340103285Sikob	}
1341120660Ssimokawa
1342120660Ssimokawa	if (tcode == FWTCODE_WREQB)
1343120660Ssimokawa		xfer->send.pay_len = 8;
1344120660Ssimokawa	else
1345120660Ssimokawa		xfer->send.pay_len = 0;
1346120660Ssimokawa
1347103285Sikob	xfer->sc = (caddr_t)sdev;
1348120660Ssimokawa	fp = &xfer->send.hdr;
1349113584Ssimokawa	fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
1350113584Ssimokawa	fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
1351103285Sikob	fp->mode.wreqq.tlrt = 0;
1352103285Sikob	fp->mode.wreqq.tcode = tcode;
1353103285Sikob	fp->mode.wreqq.pri = 0;
1354120660Ssimokawa	fp->mode.wreqq.dst = FWLOCALBUS | sdev->target->fwdev->dst;
1355103285Sikob
1356103285Sikob	return xfer;
1357103285Sikob
1358103285Sikob}
1359103285Sikob
1360103285Sikobstatic void
1361111615Ssimokawasbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1362103285Sikob{
1363103285Sikob	struct fw_xfer *xfer;
1364103285Sikob	struct fw_pkt *fp;
1365103285Sikob	struct sbp_ocb *ocb;
1366111615Ssimokawa	struct sbp_target *target;
1367103285Sikob	int s, nid;
1368103285Sikob
1369111615Ssimokawa	target = sdev->target;
1370111615Ssimokawa	nid = target->sbp->fd.fc->nodeid | FWLOCALBUS;
1371111615Ssimokawa
1372111615Ssimokawa	s = splfw();
1373170374Ssimokawa	SBP_LOCK(target->sbp);
1374111615Ssimokawa	if (func == ORB_FUN_RUNQUEUE) {
1375111615Ssimokawa		ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1376111615Ssimokawa		if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1377170374Ssimokawa			SBP_UNLOCK(target->sbp);
1378111615Ssimokawa			splx(s);
1379111615Ssimokawa			return;
1380111615Ssimokawa		}
1381111615Ssimokawa		STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1382170374Ssimokawa		SBP_UNLOCK(target->sbp);
1383111615Ssimokawa		goto start;
1384111615Ssimokawa	}
1385113584Ssimokawa	if ((ocb = sbp_get_ocb(sdev)) == NULL) {
1386170374Ssimokawa		SBP_UNLOCK(target->sbp);
1387103285Sikob		splx(s);
1388122528Ssimokawa		/* XXX */
1389103285Sikob		return;
1390103285Sikob	}
1391170374Ssimokawa	SBP_UNLOCK(target->sbp);
1392103285Sikob	ocb->flags = OCB_ACT_MGM;
1393103285Sikob	ocb->sdev = sdev;
1394103285Sikob
1395120660Ssimokawa	bzero((void *)ocb->orb, sizeof(ocb->orb));
1396103285Sikob	ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1397120660Ssimokawa	ocb->orb[7] = htonl(SBP_DEV2ADDR(target->target_id, sdev->lun_id));
1398103285Sikob
1399107653SsimokawaSBP_DEBUG(0)
1400103285Sikob	sbp_show_sdev_info(sdev, 2);
1401103285Sikob	printf("%s\n", orb_fun_name[(func>>16)&0xf]);
1402107653SsimokawaEND_DEBUG
1403103285Sikob	switch (func) {
1404103285Sikob	case ORB_FUN_LGI:
1405120660Ssimokawa		ocb->orb[0] = ocb->orb[1] = 0; /* password */
1406103285Sikob		ocb->orb[2] = htonl(nid << 16);
1407113584Ssimokawa		ocb->orb[3] = htonl(sdev->dma.bus_addr);
1408121792Ssimokawa		ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id);
1409122387Ssimokawa		if (ex_login)
1410121792Ssimokawa			ocb->orb[4] |= htonl(ORB_EXV);
1411113584Ssimokawa		ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
1412113584Ssimokawa		fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD);
1413103285Sikob		break;
1414110336Ssimokawa	case ORB_FUN_ATA:
1415111615Ssimokawa		ocb->orb[0] = htonl((0 << 16) | 0);
1416113584Ssimokawa		ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
1417110336Ssimokawa		/* fall through */
1418103285Sikob	case ORB_FUN_RCN:
1419103285Sikob	case ORB_FUN_LGO:
1420103285Sikob	case ORB_FUN_LUR:
1421103285Sikob	case ORB_FUN_RST:
1422103285Sikob	case ORB_FUN_ATS:
1423113584Ssimokawa		ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
1424103285Sikob		break;
1425103285Sikob	}
1426103285Sikob
1427111615Ssimokawa	if (target->mgm_ocb_cur != NULL) {
1428111615Ssimokawa		/* there is a standing ORB */
1429170374Ssimokawa		SBP_LOCK(target->sbp);
1430111615Ssimokawa		STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1431170374Ssimokawa		SBP_UNLOCK(target->sbp);
1432111615Ssimokawa		splx(s);
1433111615Ssimokawa		return;
1434111615Ssimokawa	}
1435111615Ssimokawastart:
1436111615Ssimokawa	target->mgm_ocb_cur = ocb;
1437111615Ssimokawa	splx(s);
1438111615Ssimokawa
1439111615Ssimokawa	callout_reset(&target->mgm_ocb_timeout, 5*hz,
1440114732Ssimokawa				sbp_mgm_timeout, (caddr_t)ocb);
1441103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1442103285Sikob	if(xfer == NULL){
1443103285Sikob		return;
1444103285Sikob	}
1445167632Ssimokawa	xfer->hand = sbp_mgm_callback;
1446103285Sikob
1447120660Ssimokawa	fp = &xfer->send.hdr;
1448113584Ssimokawa	fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
1449113584Ssimokawa	fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
1450113584Ssimokawa	fp->mode.wreqb.len = 8;
1451103285Sikob	fp->mode.wreqb.extcode = 0;
1452120660Ssimokawa	xfer->send.payload[0] = htonl(nid << 16);
1453120660Ssimokawa	xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff);
1454120660SsimokawaSBP_DEBUG(0)
1455120660Ssimokawa	sbp_show_sdev_info(sdev, 2);
1456129585Sdfr	printf("mgm orb: %08x\n", (uint32_t)ocb->bus_addr);
1457120660SsimokawaEND_DEBUG
1458103285Sikob
1459103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1460103285Sikob}
1461103285Sikob
1462103285Sikobstatic void
1463105792Ssimokawasbp_print_scsi_cmd(struct sbp_ocb *ocb)
1464103285Sikob{
1465103285Sikob	struct ccb_scsiio *csio;
1466103285Sikob
1467103285Sikob	csio = &ocb->ccb->csio;
1468103285Sikob	printf("%s:%d:%d XPT_SCSI_IO: "
1469103285Sikob		"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1470103285Sikob		", flags: 0x%02x, "
1471103285Sikob		"%db cmd/%db data/%db sense\n",
1472103285Sikob		device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1473103285Sikob		ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun,
1474103285Sikob		csio->cdb_io.cdb_bytes[0],
1475103285Sikob		csio->cdb_io.cdb_bytes[1],
1476103285Sikob		csio->cdb_io.cdb_bytes[2],
1477103285Sikob		csio->cdb_io.cdb_bytes[3],
1478103285Sikob		csio->cdb_io.cdb_bytes[4],
1479103285Sikob		csio->cdb_io.cdb_bytes[5],
1480103285Sikob		csio->cdb_io.cdb_bytes[6],
1481103285Sikob		csio->cdb_io.cdb_bytes[7],
1482103285Sikob		csio->cdb_io.cdb_bytes[8],
1483103285Sikob		csio->cdb_io.cdb_bytes[9],
1484103285Sikob		ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1485103285Sikob		csio->cdb_len, csio->dxfer_len,
1486103285Sikob		csio->sense_len);
1487105792Ssimokawa}
1488105792Ssimokawa
1489105792Ssimokawastatic void
1490105792Ssimokawasbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1491105792Ssimokawa{
1492105792Ssimokawa	struct sbp_cmd_status *sbp_cmd_status;
1493105792Ssimokawa	struct scsi_sense_data *sense;
1494105792Ssimokawa
1495105792Ssimokawa	sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1496105792Ssimokawa	sense = &ocb->ccb->csio.sense_data;
1497105792Ssimokawa
1498105792SsimokawaSBP_DEBUG(0)
1499105792Ssimokawa	sbp_print_scsi_cmd(ocb);
1500103285Sikob	/* XXX need decode status */
1501103285Sikob	sbp_show_sdev_info(ocb->sdev, 2);
1502113584Ssimokawa	printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1503103285Sikob		sbp_cmd_status->status,
1504103285Sikob		sbp_cmd_status->sfmt,
1505103285Sikob		sbp_cmd_status->valid,
1506103285Sikob		sbp_cmd_status->s_key,
1507103285Sikob		sbp_cmd_status->s_code,
1508103285Sikob		sbp_cmd_status->s_qlfr,
1509103285Sikob		sbp_status->len
1510103285Sikob	);
1511103285SikobEND_DEBUG
1512103285Sikob
1513110071Ssimokawa	switch (sbp_cmd_status->status) {
1514110071Ssimokawa	case SCSI_STATUS_CHECK_COND:
1515110071Ssimokawa	case SCSI_STATUS_BUSY:
1516110071Ssimokawa	case SCSI_STATUS_CMD_TERMINATED:
1517103285Sikob		if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1518103285Sikob			sense->error_code = SSD_CURRENT_ERROR;
1519103285Sikob		}else{
1520103285Sikob			sense->error_code = SSD_DEFERRED_ERROR;
1521103285Sikob		}
1522103285Sikob		if(sbp_cmd_status->valid)
1523103285Sikob			sense->error_code |= SSD_ERRCODE_VALID;
1524103285Sikob		sense->flags = sbp_cmd_status->s_key;
1525103285Sikob		if(sbp_cmd_status->mark)
1526103285Sikob			sense->flags |= SSD_FILEMARK;
1527103285Sikob		if(sbp_cmd_status->eom)
1528103285Sikob			sense->flags |= SSD_EOM;
1529103285Sikob		if(sbp_cmd_status->ill_len)
1530103285Sikob			sense->flags |= SSD_ILI;
1531119556Ssimokawa
1532119556Ssimokawa		bcopy(&sbp_cmd_status->info, &sense->info[0], 4);
1533119556Ssimokawa
1534103285Sikob		if (sbp_status->len <= 1)
1535103285Sikob			/* XXX not scsi status. shouldn't be happened */
1536103285Sikob			sense->extra_len = 0;
1537103285Sikob		else if (sbp_status->len <= 4)
1538103285Sikob			/* add_sense_code(_qual), info, cmd_spec_info */
1539103285Sikob			sense->extra_len = 6;
1540103285Sikob		else
1541103285Sikob			/* fru, sense_key_spec */
1542103285Sikob			sense->extra_len = 10;
1543119556Ssimokawa
1544119556Ssimokawa		bcopy(&sbp_cmd_status->cdb, &sense->cmd_spec_info[0], 4);
1545119556Ssimokawa
1546103285Sikob		sense->add_sense_code = sbp_cmd_status->s_code;
1547103285Sikob		sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1548103285Sikob		sense->fru = sbp_cmd_status->fru;
1549103285Sikob
1550119556Ssimokawa		bcopy(&sbp_cmd_status->s_keydep[0],
1551119556Ssimokawa		    &sense->sense_key_spec[0], 3);
1552119556Ssimokawa
1553103285Sikob		ocb->ccb->csio.scsi_status = sbp_cmd_status->status;;
1554103285Sikob		ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1555103285Sikob							| CAM_AUTOSNS_VALID;
1556103285Sikob/*
1557103285Sikob{
1558129585Sdfr		uint8_t j, *tmp;
1559103285Sikob		tmp = sense;
1560103285Sikob		for( j = 0 ; j < 32 ; j+=8){
1561103285Sikob			printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1562103285Sikob				tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1563103285Sikob				tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1564103285Sikob		}
1565103285Sikob
1566103285Sikob}
1567103285Sikob*/
1568110071Ssimokawa		break;
1569110071Ssimokawa	default:
1570110071Ssimokawa		sbp_show_sdev_info(ocb->sdev, 2);
1571110071Ssimokawa		printf("sbp_scsi_status: unknown scsi status 0x%x\n",
1572110071Ssimokawa						sbp_cmd_status->status);
1573103285Sikob	}
1574103285Sikob}
1575103285Sikob
1576103285Sikobstatic void
1577103285Sikobsbp_fix_inq_data(struct sbp_ocb *ocb)
1578103285Sikob{
1579103285Sikob	union ccb *ccb;
1580103285Sikob	struct sbp_dev *sdev;
1581103285Sikob	struct scsi_inquiry_data *inq;
1582103285Sikob
1583103285Sikob	ccb = ocb->ccb;
1584103285Sikob	sdev = ocb->sdev;
1585103285Sikob
1586103285Sikob	if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1587103285Sikob		return;
1588103285SikobSBP_DEBUG(1)
1589103285Sikob	sbp_show_sdev_info(sdev, 2);
1590103285Sikob	printf("sbp_fix_inq_data\n");
1591103285SikobEND_DEBUG
1592103285Sikob	inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1593103285Sikob	switch (SID_TYPE(inq)) {
1594103285Sikob	case T_DIRECT:
1595118291Ssimokawa#if 0
1596103285Sikob		/*
1597103285Sikob		 * XXX Convert Direct Access device to RBC.
1598108281Ssimokawa		 * I've never seen FireWire DA devices which support READ_6.
1599103285Sikob		 */
1600103285Sikob		if (SID_TYPE(inq) == T_DIRECT)
1601103285Sikob			inq->device |= T_RBC; /*  T_DIRECT == 0 */
1602103285Sikob#endif
1603103285Sikob		/* fall through */
1604103285Sikob	case T_RBC:
1605103285Sikob		/*
1606103285Sikob		 * Override vendor/product/revision information.
1607103285Sikob		 * Some devices sometimes return strange strings.
1608103285Sikob		 */
1609111615Ssimokawa#if 1
1610103285Sikob		bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1611103285Sikob		bcopy(sdev->product, inq->product, sizeof(inq->product));
1612103285Sikob		bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1613111615Ssimokawa#endif
1614103285Sikob		break;
1615103285Sikob	}
1616127468Ssimokawa	/*
1617127468Ssimokawa	 * Force to enable/disable tagged queuing.
1618127468Ssimokawa	 * XXX CAM also checks SCP_QUEUE_DQUE flag in the control mode page.
1619127468Ssimokawa	 */
1620127468Ssimokawa	if (sbp_tags > 0)
1621127468Ssimokawa		inq->flags |= SID_CmdQue;
1622127468Ssimokawa	else if (sbp_tags < 0)
1623127468Ssimokawa		inq->flags &= ~SID_CmdQue;
1624127468Ssimokawa
1625103285Sikob}
1626103285Sikob
1627103285Sikobstatic void
1628113584Ssimokawasbp_recv1(struct fw_xfer *xfer)
1629113584Ssimokawa{
1630103285Sikob	struct fw_pkt *rfp;
1631103285Sikob#if NEED_RESPONSE
1632103285Sikob	struct fw_pkt *sfp;
1633103285Sikob#endif
1634103285Sikob	struct sbp_softc *sbp;
1635103285Sikob	struct sbp_dev *sdev;
1636103285Sikob	struct sbp_ocb *ocb;
1637103285Sikob	struct sbp_login_res *login_res = NULL;
1638103285Sikob	struct sbp_status *sbp_status;
1639103285Sikob	struct sbp_target *target;
1640111704Ssimokawa	int	orb_fun, status_valid0, status_valid, t, l, reset_agent = 0;
1641129585Sdfr	uint32_t addr;
1642103285Sikob/*
1643129585Sdfr	uint32_t *ld;
1644103285Sikob	ld = xfer->recv.buf;
1645103285Sikobprintf("sbp %x %d %d %08x %08x %08x %08x\n",
1646103285Sikob			xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1647103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1648103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1649103285Sikob*/
1650113584Ssimokawa	sbp = (struct sbp_softc *)xfer->sc;
1651120660Ssimokawa	if (xfer->resp != 0){
1652122529Ssimokawa		printf("sbp_recv: xfer->resp = %d\n", xfer->resp);
1653113584Ssimokawa		goto done0;
1654103285Sikob	}
1655120660Ssimokawa	if (xfer->recv.payload == NULL){
1656120660Ssimokawa		printf("sbp_recv: xfer->recv.payload == NULL\n");
1657113584Ssimokawa		goto done0;
1658103285Sikob	}
1659120660Ssimokawa	rfp = &xfer->recv.hdr;
1660103285Sikob	if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1661103285Sikob		printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1662113584Ssimokawa		goto done0;
1663103285Sikob	}
1664120660Ssimokawa	sbp_status = (struct sbp_status *)xfer->recv.payload;
1665113584Ssimokawa	addr = rfp->mode.wreqb.dest_lo;
1666103285SikobSBP_DEBUG(2)
1667103285Sikob	printf("received address 0x%x\n", addr);
1668103285SikobEND_DEBUG
1669110189Ssimokawa	t = SBP_ADDR2TRG(addr);
1670110189Ssimokawa	if (t >= SBP_NUM_TARGETS) {
1671110189Ssimokawa		device_printf(sbp->fd.dev,
1672110189Ssimokawa			"sbp_recv1: invalid target %d\n", t);
1673113584Ssimokawa		goto done0;
1674110189Ssimokawa	}
1675110189Ssimokawa	target = &sbp->targets[t];
1676110189Ssimokawa	l = SBP_ADDR2LUN(addr);
1677121185Ssimokawa	if (l >= target->num_lun || target->luns[l] == NULL) {
1678110189Ssimokawa		device_printf(sbp->fd.dev,
1679110189Ssimokawa			"sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1680113584Ssimokawa		goto done0;
1681110189Ssimokawa	}
1682121185Ssimokawa	sdev = target->luns[l];
1683103285Sikob
1684110189Ssimokawa	ocb = NULL;
1685110189Ssimokawa	switch (sbp_status->src) {
1686110189Ssimokawa	case 0:
1687110189Ssimokawa	case 1:
1688111615Ssimokawa		/* check mgm_ocb_cur first */
1689111615Ssimokawa		ocb  = target->mgm_ocb_cur;
1690111615Ssimokawa		if (ocb != NULL) {
1691111615Ssimokawa			if (OCB_MATCH(ocb, sbp_status)) {
1692111615Ssimokawa				callout_stop(&target->mgm_ocb_timeout);
1693111615Ssimokawa				target->mgm_ocb_cur = NULL;
1694111615Ssimokawa				break;
1695111615Ssimokawa			}
1696111615Ssimokawa		}
1697111615Ssimokawa		ocb = sbp_dequeue_ocb(sdev, sbp_status);
1698110189Ssimokawa		if (ocb == NULL) {
1699110189Ssimokawa			sbp_show_sdev_info(sdev, 2);
1700127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
1701127468Ssimokawa			printf("No ocb(%lx) on the queue\n",
1702127468Ssimokawa#else
1703111819Ssimokawa			printf("No ocb(%x) on the queue\n",
1704111858Ssimokawa#endif
1705111819Ssimokawa					ntohl(sbp_status->orb_lo));
1706110189Ssimokawa		}
1707110189Ssimokawa		break;
1708110189Ssimokawa	case 2:
1709110189Ssimokawa		/* unsolicit */
1710110189Ssimokawa		sbp_show_sdev_info(sdev, 2);
1711110189Ssimokawa		printf("unsolicit status received\n");
1712110189Ssimokawa		break;
1713110189Ssimokawa	default:
1714110189Ssimokawa		sbp_show_sdev_info(sdev, 2);
1715110189Ssimokawa		printf("unknown sbp_status->src\n");
1716110189Ssimokawa	}
1717110189Ssimokawa
1718111615Ssimokawa	status_valid0 = (sbp_status->src < 2
1719110189Ssimokawa			&& sbp_status->resp == ORB_RES_CMPL
1720111615Ssimokawa			&& sbp_status->dead == 0);
1721111615Ssimokawa	status_valid = (status_valid0 && sbp_status->status == 0);
1722103285Sikob
1723121185Ssimokawa	if (!status_valid0 || debug > 2){
1724103285Sikob		int status;
1725110129SsimokawaSBP_DEBUG(0)
1726103285Sikob		sbp_show_sdev_info(sdev, 2);
1727103285Sikob		printf("ORB status src:%x resp:%x dead:%x"
1728127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
1729127468Ssimokawa				" len:%x stat:%x orb:%x%08lx\n",
1730127468Ssimokawa#else
1731103285Sikob				" len:%x stat:%x orb:%x%08x\n",
1732108712Ssimokawa#endif
1733103285Sikob			sbp_status->src, sbp_status->resp, sbp_status->dead,
1734103285Sikob			sbp_status->len, sbp_status->status,
1735108280Ssimokawa			ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1736110129SsimokawaEND_DEBUG
1737103285Sikob		sbp_show_sdev_info(sdev, 2);
1738103285Sikob		status = sbp_status->status;
1739103285Sikob		switch(sbp_status->resp) {
1740103285Sikob		case 0:
1741103285Sikob			if (status > MAX_ORB_STATUS0)
1742103285Sikob				printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1743110189Ssimokawa			else
1744110129Ssimokawa				printf("%s\n", orb_status0[status]);
1745103285Sikob			break;
1746103285Sikob		case 1:
1747110336Ssimokawa			printf("Obj: %s, Error: %s\n",
1748103285Sikob				orb_status1_object[(status>>6) & 3],
1749103285Sikob				orb_status1_serial_bus_error[status & 0xf]);
1750103285Sikob			break;
1751110129Ssimokawa		case 2:
1752110129Ssimokawa			printf("Illegal request\n");
1753110129Ssimokawa			break;
1754110129Ssimokawa		case 3:
1755110129Ssimokawa			printf("Vendor dependent\n");
1756110129Ssimokawa			break;
1757103285Sikob		default:
1758110129Ssimokawa			printf("unknown respose code %d\n", sbp_status->resp);
1759103285Sikob		}
1760103285Sikob	}
1761103285Sikob
1762103285Sikob	/* we have to reset the fetch agent if it's dead */
1763103285Sikob	if (sbp_status->dead) {
1764111615Ssimokawa		if (sdev->path) {
1765170374Ssimokawa			SBP_LOCK(sbp);
1766103285Sikob			xpt_freeze_devq(sdev->path, 1);
1767111615Ssimokawa			sdev->freeze ++;
1768170374Ssimokawa			SBP_UNLOCK(sbp);
1769111615Ssimokawa		}
1770111704Ssimokawa		reset_agent = 1;
1771103285Sikob	}
1772103285Sikob
1773111704Ssimokawa	if (ocb == NULL)
1774111704Ssimokawa		goto done;
1775103285Sikob
1776103285Sikob	switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1777103285Sikob	case ORB_FMT_NOP:
1778103285Sikob		break;
1779103285Sikob	case ORB_FMT_VED:
1780103285Sikob		break;
1781103285Sikob	case ORB_FMT_STD:
1782111819Ssimokawa		switch(ocb->flags) {
1783103285Sikob		case OCB_ACT_MGM:
1784103285Sikob			orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1785120842Ssimokawa			reset_agent = 0;
1786103285Sikob			switch(orb_fun) {
1787103285Sikob			case ORB_FUN_LGI:
1788113584Ssimokawa				fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD);
1789113584Ssimokawa				login_res = sdev->login;
1790103285Sikob				login_res->len = ntohs(login_res->len);
1791103285Sikob				login_res->id = ntohs(login_res->id);
1792103285Sikob				login_res->cmd_hi = ntohs(login_res->cmd_hi);
1793103285Sikob				login_res->cmd_lo = ntohl(login_res->cmd_lo);
1794103285Sikob				if (status_valid) {
1795103285SikobSBP_DEBUG(0)
1796103285Sikobsbp_show_sdev_info(sdev, 2);
1797103285Sikobprintf("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));
1798103285SikobEND_DEBUG
1799103285Sikob					sbp_busy_timeout(sdev);
1800103285Sikob				} else {
1801110336Ssimokawa					/* forgot logout? */
1802110336Ssimokawa					sbp_show_sdev_info(sdev, 2);
1803103285Sikob					printf("login failed\n");
1804103285Sikob					sdev->status = SBP_DEV_RESET;
1805103285Sikob				}
1806103285Sikob				break;
1807103285Sikob			case ORB_FUN_RCN:
1808113584Ssimokawa				login_res = sdev->login;
1809103285Sikob				if (status_valid) {
1810103285SikobSBP_DEBUG(0)
1811103285Sikobsbp_show_sdev_info(sdev, 2);
1812103285Sikobprintf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo);
1813103285SikobEND_DEBUG
1814103285Sikob#if 1
1815111615Ssimokawa					if (sdev->status == SBP_DEV_ATTACHED)
1816111615Ssimokawa						sbp_scan_dev(sdev);
1817111615Ssimokawa					else
1818111615Ssimokawa						sbp_agent_reset(sdev);
1819103285Sikob#else
1820110336Ssimokawa					sdev->status = SBP_DEV_ATTACHED;
1821111615Ssimokawa					sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL);
1822103285Sikob#endif
1823103285Sikob				} else {
1824103285Sikob					/* reconnection hold time exceed? */
1825110336SsimokawaSBP_DEBUG(0)
1826110336Ssimokawa					sbp_show_sdev_info(sdev, 2);
1827103285Sikob					printf("reconnect failed\n");
1828110336SsimokawaEND_DEBUG
1829111615Ssimokawa					sbp_login(sdev);
1830103285Sikob				}
1831103285Sikob				break;
1832103285Sikob			case ORB_FUN_LGO:
1833103285Sikob				sdev->status = SBP_DEV_RESET;
1834103285Sikob				break;
1835110336Ssimokawa			case ORB_FUN_RST:
1836110336Ssimokawa				sbp_busy_timeout(sdev);
1837110336Ssimokawa				break;
1838103285Sikob			case ORB_FUN_LUR:
1839103285Sikob			case ORB_FUN_ATA:
1840103285Sikob			case ORB_FUN_ATS:
1841110336Ssimokawa				sbp_agent_reset(sdev);
1842103285Sikob				break;
1843103285Sikob			default:
1844110336Ssimokawa				sbp_show_sdev_info(sdev, 2);
1845110336Ssimokawa				printf("unknown function %d\n", orb_fun);
1846103285Sikob				break;
1847103285Sikob			}
1848111615Ssimokawa			sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
1849103285Sikob			break;
1850103285Sikob		case OCB_ACT_CMD:
1851114732Ssimokawa			sdev->timeout = 0;
1852103285Sikob			if(ocb->ccb != NULL){
1853103285Sikob				union ccb *ccb;
1854103285Sikob/*
1855129585Sdfr				uint32_t *ld;
1856103285Sikob				ld = ocb->ccb->csio.data_ptr;
1857103285Sikob				if(ld != NULL && ocb->ccb->csio.dxfer_len != 0)
1858103285Sikob					printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]);
1859103285Sikob				else
1860103285Sikob					printf("ptr NULL\n");
1861103285Sikobprintf("len %d\n", sbp_status->len);
1862103285Sikob*/
1863103285Sikob				ccb = ocb->ccb;
1864103285Sikob				if(sbp_status->len > 1){
1865103285Sikob					sbp_scsi_status(sbp_status, ocb);
1866103285Sikob				}else{
1867103285Sikob					if(sbp_status->resp != ORB_RES_CMPL){
1868103285Sikob						ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1869103285Sikob					}else{
1870103285Sikob						ccb->ccb_h.status = CAM_REQ_CMP;
1871103285Sikob					}
1872103285Sikob				}
1873103285Sikob				/* fix up inq data */
1874103285Sikob				if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1875103285Sikob					sbp_fix_inq_data(ocb);
1876170374Ssimokawa				SBP_LOCK(sbp);
1877103285Sikob				xpt_done(ccb);
1878170374Ssimokawa				SBP_UNLOCK(sbp);
1879103285Sikob			}
1880103285Sikob			break;
1881103285Sikob		default:
1882103285Sikob			break;
1883103285Sikob		}
1884103285Sikob	}
1885103285Sikob
1886127468Ssimokawa	if (!use_doorbell)
1887127468Ssimokawa		sbp_free_ocb(sdev, ocb);
1888111704Ssimokawadone:
1889111704Ssimokawa	if (reset_agent)
1890111704Ssimokawa		sbp_agent_reset(sdev);
1891103285Sikob
1892113584Ssimokawadone0:
1893120660Ssimokawa	xfer->recv.pay_len = SBP_RECV_LEN;
1894103285Sikob/* The received packet is usually small enough to be stored within
1895103285Sikob * the buffer. In that case, the controller return ack_complete and
1896103285Sikob * no respose is necessary.
1897103285Sikob *
1898103285Sikob * XXX fwohci.c and firewire.c should inform event_code such as
1899103285Sikob * ack_complete or ack_pending to upper driver.
1900103285Sikob */
1901103285Sikob#if NEED_RESPONSE
1902103285Sikob	xfer->send.off = 0;
1903103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
1904103285Sikob	sfp->mode.wres.dst = rfp->mode.wreqb.src;
1905113584Ssimokawa	xfer->dst = sfp->mode.wres.dst;
1906103285Sikob	xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1907167632Ssimokawa	xfer->hand = sbp_loginres_callback;
1908103285Sikob
1909103285Sikob	sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1910103285Sikob	sfp->mode.wres.tcode = FWTCODE_WRES;
1911103285Sikob	sfp->mode.wres.rtcode = 0;
1912103285Sikob	sfp->mode.wres.pri = 0;
1913103285Sikob
1914103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1915103285Sikob#else
1916113584Ssimokawa	/* recycle */
1917113584Ssimokawa	STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1918103285Sikob#endif
1919103285Sikob
1920103285Sikob	return;
1921103285Sikob
1922103285Sikob}
1923103285Sikob
1924103285Sikobstatic void
1925103285Sikobsbp_recv(struct fw_xfer *xfer)
1926103285Sikob{
1927103285Sikob	int s;
1928103285Sikob
1929103285Sikob	s = splcam();
1930103285Sikob	sbp_recv1(xfer);
1931103285Sikob	splx(s);
1932103285Sikob}
1933103285Sikob/*
1934103285Sikob * sbp_attach()
1935103285Sikob */
1936103285Sikobstatic int
1937103285Sikobsbp_attach(device_t dev)
1938103285Sikob{
1939103285Sikob	struct sbp_softc *sbp;
1940103285Sikob	struct cam_devq *devq;
1941170374Ssimokawa	struct firewire_comm *fc;
1942103285Sikob	int i, s, error;
1943103285Sikob
1944167624Ssimokawa	if (DFLTPHYS > SBP_MAXPHYS)
1945167624Ssimokawa		device_printf(dev, "Warning, DFLTPHYS(%dKB) is larger than "
1946167624Ssimokawa			"SBP_MAXPHYS(%dKB).\n", DFLTPHYS / 1024,
1947167624Ssimokawa			SBP_MAXPHYS / 1024);
1948167624Ssimokawa
1949103285SikobSBP_DEBUG(0)
1950111199Ssimokawa	printf("sbp_attach (cold=%d)\n", cold);
1951103285SikobEND_DEBUG
1952103285Sikob
1953111199Ssimokawa	if (cold)
1954111199Ssimokawa		sbp_cold ++;
1955103285Sikob	sbp = ((struct sbp_softc *)device_get_softc(dev));
1956103285Sikob	bzero(sbp, sizeof(struct sbp_softc));
1957103285Sikob	sbp->fd.dev = dev;
1958170374Ssimokawa	sbp->fd.fc = fc = device_get_ivars(dev);
1959170374Ssimokawa	mtx_init(&sbp->mtx, "sbp", NULL, MTX_DEF);
1960124251Ssimokawa
1961124251Ssimokawa	if (max_speed < 0)
1962170374Ssimokawa		max_speed = fc->speed;
1963124251Ssimokawa
1964170374Ssimokawa	error = bus_dma_tag_create(/*parent*/fc->dmat,
1965113584Ssimokawa				/* XXX shoud be 4 for sane backend? */
1966113584Ssimokawa				/*alignment*/1,
1967103285Sikob				/*boundary*/0,
1968103285Sikob				/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1969103285Sikob				/*highaddr*/BUS_SPACE_MAXADDR,
1970103285Sikob				/*filter*/NULL, /*filterarg*/NULL,
1971103285Sikob				/*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1972113474Ssimokawa				/*maxsegsz*/SBP_SEG_MAX,
1973103285Sikob				/*flags*/BUS_DMA_ALLOCNOW,
1974127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 501102
1975117126Sscottl				/*lockfunc*/busdma_lock_mutex,
1976170374Ssimokawa				/*lockarg*/&sbp->mtx,
1977117228Ssimokawa#endif
1978117228Ssimokawa				&sbp->dmat);
1979103285Sikob	if (error != 0) {
1980103285Sikob		printf("sbp_attach: Could not allocate DMA tag "
1981103285Sikob			"- error %d\n", error);
1982103285Sikob			return (ENOMEM);
1983103285Sikob	}
1984103285Sikob
1985103285Sikob	devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1986103285Sikob	if (devq == NULL)
1987103285Sikob		return (ENXIO);
1988103285Sikob
1989103285Sikob	for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1990103285Sikob		sbp->targets[i].fwdev = NULL;
1991103285Sikob		sbp->targets[i].luns = NULL;
1992103285Sikob	}
1993103285Sikob
1994103285Sikob	sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1995103285Sikob				 device_get_unit(dev),
1996170374Ssimokawa				 &sbp->mtx,
1997111615Ssimokawa				 /*untagged*/ 1,
1998122528Ssimokawa				 /*tagged*/ SBP_QUEUE_LEN - 1,
1999111615Ssimokawa				 devq);
2000103285Sikob
2001103285Sikob	if (sbp->sim == NULL) {
2002103285Sikob		cam_simq_free(devq);
2003103285Sikob		return (ENXIO);
2004103285Sikob	}
2005103285Sikob
2006170374Ssimokawa	SBP_LOCK(sbp);
2007111615Ssimokawa	if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS)
2008111615Ssimokawa		goto fail;
2009103285Sikob
2010111615Ssimokawa	if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim),
2011120660Ssimokawa	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2012120660Ssimokawa		xpt_bus_deregister(cam_sim_path(sbp->sim));
2013111615Ssimokawa		goto fail;
2014120660Ssimokawa	}
2015170374Ssimokawa	SBP_UNLOCK(sbp);
2016111615Ssimokawa
2017103285Sikob	/* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
2018120660Ssimokawa	sbp->fwb.start = ((u_int64_t)SBP_BIND_HI << 32) | SBP_DEV2ADDR(0, 0);
2019120660Ssimokawa	sbp->fwb.end = sbp->fwb.start + 0xffff;
2020113584Ssimokawa	/* pre-allocate xfer */
2021113584Ssimokawa	STAILQ_INIT(&sbp->fwb.xferlist);
2022169130Ssimokawa	fw_xferlist_add(&sbp->fwb.xferlist, M_SBP,
2023169130Ssimokawa	    /*send*/ 0, /*recv*/ SBP_RECV_LEN, SBP_NUM_OCB/2,
2024170374Ssimokawa	    fc, (void *)sbp, sbp_recv);
2025103285Sikob
2026170374Ssimokawa	fw_bindadd(fc, &sbp->fwb);
2027170374Ssimokawa
2028113584Ssimokawa	sbp->fd.post_busreset = sbp_post_busreset;
2029103285Sikob	sbp->fd.post_explore = sbp_post_explore;
2030103285Sikob
2031170374Ssimokawa	if (fc->status != -1) {
2032111199Ssimokawa		s = splfw();
2033122387Ssimokawa		sbp_post_busreset((void *)sbp);
2034111199Ssimokawa		sbp_post_explore((void *)sbp);
2035111199Ssimokawa		splx(s);
2036111199Ssimokawa	}
2037170374Ssimokawa	SBP_LOCK(sbp);
2038122387Ssimokawa	xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
2039170374Ssimokawa	SBP_UNLOCK(sbp);
2040111199Ssimokawa
2041103285Sikob	return (0);
2042111615Ssimokawafail:
2043170374Ssimokawa	SBP_UNLOCK(sbp);
2044111615Ssimokawa	cam_sim_free(sbp->sim, /*free_devq*/TRUE);
2045111615Ssimokawa	return (ENXIO);
2046103285Sikob}
2047103285Sikob
2048103285Sikobstatic int
2049110145Ssimokawasbp_logout_all(struct sbp_softc *sbp)
2050110145Ssimokawa{
2051110145Ssimokawa	struct sbp_target *target;
2052110145Ssimokawa	struct sbp_dev *sdev;
2053110145Ssimokawa	int i, j;
2054110145Ssimokawa
2055110145SsimokawaSBP_DEBUG(0)
2056110145Ssimokawa	printf("sbp_logout_all\n");
2057110145SsimokawaEND_DEBUG
2058110145Ssimokawa	for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
2059110145Ssimokawa		target = &sbp->targets[i];
2060110145Ssimokawa		if (target->luns == NULL)
2061110145Ssimokawa			continue;
2062110145Ssimokawa		for (j = 0; j < target->num_lun; j++) {
2063121185Ssimokawa			sdev = target->luns[j];
2064121185Ssimokawa			if (sdev == NULL)
2065121185Ssimokawa				continue;
2066113584Ssimokawa			callout_stop(&sdev->login_callout);
2067110336Ssimokawa			if (sdev->status >= SBP_DEV_TOATTACH &&
2068110336Ssimokawa					sdev->status <= SBP_DEV_ATTACHED)
2069111615Ssimokawa				sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
2070110145Ssimokawa		}
2071110145Ssimokawa	}
2072113584Ssimokawa
2073110145Ssimokawa	return 0;
2074110145Ssimokawa}
2075110145Ssimokawa
2076110145Ssimokawastatic int
2077110145Ssimokawasbp_shutdown(device_t dev)
2078110145Ssimokawa{
2079110145Ssimokawa	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2080110145Ssimokawa
2081110145Ssimokawa	sbp_logout_all(sbp);
2082110145Ssimokawa	return (0);
2083110145Ssimokawa}
2084110145Ssimokawa
2085121185Ssimokawastatic void
2086121185Ssimokawasbp_free_sdev(struct sbp_dev *sdev)
2087121185Ssimokawa{
2088121185Ssimokawa	int i;
2089121185Ssimokawa
2090121185Ssimokawa	if (sdev == NULL)
2091121185Ssimokawa		return;
2092121185Ssimokawa	for (i = 0; i < SBP_QUEUE_LEN; i++)
2093121185Ssimokawa		bus_dmamap_destroy(sdev->target->sbp->dmat,
2094121185Ssimokawa		    sdev->ocb[i].dmamap);
2095121185Ssimokawa	fwdma_free(sdev->target->sbp->fd.fc, &sdev->dma);
2096121185Ssimokawa	free(sdev, M_SBP);
2097121185Ssimokawa}
2098121185Ssimokawa
2099121185Ssimokawastatic void
2100121185Ssimokawasbp_free_target(struct sbp_target *target)
2101121185Ssimokawa{
2102121185Ssimokawa	struct sbp_softc *sbp;
2103121185Ssimokawa	struct fw_xfer *xfer, *next;
2104121185Ssimokawa	int i;
2105121185Ssimokawa
2106121185Ssimokawa	if (target->luns == NULL)
2107121185Ssimokawa		return;
2108121185Ssimokawa	callout_stop(&target->mgm_ocb_timeout);
2109121185Ssimokawa	sbp = target->sbp;
2110121185Ssimokawa	for (i = 0; i < target->num_lun; i++)
2111121185Ssimokawa		sbp_free_sdev(target->luns[i]);
2112121185Ssimokawa
2113121185Ssimokawa	for (xfer = STAILQ_FIRST(&target->xferlist);
2114121185Ssimokawa			xfer != NULL; xfer = next) {
2115121185Ssimokawa		next = STAILQ_NEXT(xfer, link);
2116121185Ssimokawa		fw_xfer_free_buf(xfer);
2117121185Ssimokawa	}
2118121185Ssimokawa	STAILQ_INIT(&target->xferlist);
2119121185Ssimokawa	free(target->luns, M_SBP);
2120121185Ssimokawa	target->num_lun = 0;;
2121121185Ssimokawa	target->luns = NULL;
2122121185Ssimokawa	target->fwdev = NULL;
2123121185Ssimokawa}
2124121185Ssimokawa
2125110145Ssimokawastatic int
2126103285Sikobsbp_detach(device_t dev)
2127103285Sikob{
2128103285Sikob	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2129103285Sikob	struct firewire_comm *fc = sbp->fd.fc;
2130121185Ssimokawa	int i;
2131103285Sikob
2132103285SikobSBP_DEBUG(0)
2133103285Sikob	printf("sbp_detach\n");
2134103285SikobEND_DEBUG
2135113584Ssimokawa
2136103285Sikob	for (i = 0; i < SBP_NUM_TARGETS; i ++)
2137110145Ssimokawa		sbp_cam_detach_target(&sbp->targets[i]);
2138170374Ssimokawa
2139170374Ssimokawa	SBP_LOCK(sbp);
2140120660Ssimokawa	xpt_async(AC_LOST_DEVICE, sbp->path, NULL);
2141111615Ssimokawa	xpt_free_path(sbp->path);
2142103285Sikob	xpt_bus_deregister(cam_sim_path(sbp->sim));
2143170378Sgallatin	cam_sim_free(sbp->sim, /*free_devq*/ TRUE);
2144170374Ssimokawa	SBP_UNLOCK(sbp);
2145110145Ssimokawa
2146110145Ssimokawa	sbp_logout_all(sbp);
2147113584Ssimokawa
2148110145Ssimokawa	/* XXX wait for logout completion */
2149167086Sjhb	pause("sbpdtc", hz/2);
2150110145Ssimokawa
2151121185Ssimokawa	for (i = 0 ; i < SBP_NUM_TARGETS ; i ++)
2152121185Ssimokawa		sbp_free_target(&sbp->targets[i]);
2153113584Ssimokawa
2154110145Ssimokawa	fw_bindremove(fc, &sbp->fwb);
2155169130Ssimokawa	fw_xferlist_remove(&sbp->fwb.xferlist);
2156113584Ssimokawa
2157103285Sikob	bus_dma_tag_destroy(sbp->dmat);
2158170374Ssimokawa	mtx_destroy(&sbp->mtx);
2159110145Ssimokawa
2160103285Sikob	return (0);
2161103285Sikob}
2162103285Sikob
2163103285Sikobstatic void
2164121185Ssimokawasbp_cam_detach_sdev(struct sbp_dev *sdev)
2165121185Ssimokawa{
2166121185Ssimokawa	if (sdev == NULL)
2167121185Ssimokawa		return;
2168121185Ssimokawa	if (sdev->status == SBP_DEV_DEAD)
2169121185Ssimokawa		return;
2170121185Ssimokawa	if (sdev->status == SBP_DEV_RESET)
2171121185Ssimokawa		return;
2172170374Ssimokawa	sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
2173121185Ssimokawa	if (sdev->path) {
2174170374Ssimokawa		SBP_LOCK(sdev->target->sbp);
2175121185Ssimokawa		xpt_release_devq(sdev->path,
2176121185Ssimokawa				 sdev->freeze, TRUE);
2177121185Ssimokawa		sdev->freeze = 0;
2178121185Ssimokawa		xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
2179121185Ssimokawa		xpt_free_path(sdev->path);
2180121185Ssimokawa		sdev->path = NULL;
2181170374Ssimokawa		SBP_UNLOCK(sdev->target->sbp);
2182121185Ssimokawa	}
2183121185Ssimokawa}
2184121185Ssimokawa
2185121185Ssimokawastatic void
2186110145Ssimokawasbp_cam_detach_target(struct sbp_target *target)
2187103285Sikob{
2188103285Sikob	int i;
2189103285Sikob
2190103285Sikob	if (target->luns != NULL) {
2191108529SsimokawaSBP_DEBUG(0)
2192103285Sikob		printf("sbp_detach_target %d\n", target->target_id);
2193108529SsimokawaEND_DEBUG
2194111615Ssimokawa		callout_stop(&target->scan_callout);
2195121185Ssimokawa		for (i = 0; i < target->num_lun; i++)
2196121185Ssimokawa			sbp_cam_detach_sdev(target->luns[i]);
2197103285Sikob	}
2198103285Sikob}
2199103285Sikob
2200103285Sikobstatic void
2201114732Ssimokawasbp_target_reset(struct sbp_dev *sdev, int method)
2202114732Ssimokawa{
2203114732Ssimokawa	int i;
2204114732Ssimokawa	struct sbp_target *target = sdev->target;
2205114732Ssimokawa	struct sbp_dev *tsdev;
2206114732Ssimokawa
2207114732Ssimokawa	for (i = 0; i < target->num_lun; i++) {
2208121185Ssimokawa		tsdev = target->luns[i];
2209121185Ssimokawa		if (tsdev == NULL)
2210121185Ssimokawa			continue;
2211114732Ssimokawa		if (tsdev->status == SBP_DEV_DEAD)
2212114732Ssimokawa			continue;
2213114732Ssimokawa		if (tsdev->status == SBP_DEV_RESET)
2214114732Ssimokawa			continue;
2215170374Ssimokawa		SBP_LOCK(target->sbp);
2216114732Ssimokawa		xpt_freeze_devq(tsdev->path, 1);
2217114732Ssimokawa		tsdev->freeze ++;
2218170374Ssimokawa		SBP_UNLOCK(target->sbp);
2219114732Ssimokawa		sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT);
2220114732Ssimokawa		if (method == 2)
2221114732Ssimokawa			tsdev->status = SBP_DEV_LOGIN;
2222114732Ssimokawa	}
2223114732Ssimokawa	switch(method) {
2224114732Ssimokawa	case 1:
2225114732Ssimokawa		printf("target reset\n");
2226114732Ssimokawa		sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2227114732Ssimokawa		break;
2228114732Ssimokawa	case 2:
2229114732Ssimokawa		printf("reset start\n");
2230114732Ssimokawa		sbp_reset_start(sdev);
2231114732Ssimokawa		break;
2232114732Ssimokawa	}
2233114732Ssimokawa
2234114732Ssimokawa}
2235114732Ssimokawa
2236114732Ssimokawastatic void
2237114732Ssimokawasbp_mgm_timeout(void *arg)
2238114732Ssimokawa{
2239114732Ssimokawa	struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2240114732Ssimokawa	struct sbp_dev *sdev = ocb->sdev;
2241114732Ssimokawa	struct sbp_target *target = sdev->target;
2242114732Ssimokawa
2243114732Ssimokawa	sbp_show_sdev_info(sdev, 2);
2244120842Ssimokawa	printf("request timeout(mgm orb:0x%08x) ... ",
2245129585Sdfr	    (uint32_t)ocb->bus_addr);
2246114732Ssimokawa	target->mgm_ocb_cur = NULL;
2247114732Ssimokawa	sbp_free_ocb(sdev, ocb);
2248114732Ssimokawa#if 0
2249114732Ssimokawa	/* XXX */
2250121185Ssimokawa	printf("run next request\n");
2251114732Ssimokawa	sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2252114732Ssimokawa#endif
2253121185Ssimokawa#if 1
2254121185Ssimokawa	printf("reset start\n");
2255114732Ssimokawa	sbp_reset_start(sdev);
2256114732Ssimokawa#endif
2257114732Ssimokawa}
2258114732Ssimokawa
2259114732Ssimokawastatic void
2260103285Sikobsbp_timeout(void *arg)
2261103285Sikob{
2262103285Sikob	struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2263103285Sikob	struct sbp_dev *sdev = ocb->sdev;
2264103285Sikob
2265103285Sikob	sbp_show_sdev_info(sdev, 2);
2266120842Ssimokawa	printf("request timeout(cmd orb:0x%08x) ... ",
2267129585Sdfr	    (uint32_t)ocb->bus_addr);
2268103285Sikob
2269114732Ssimokawa	sdev->timeout ++;
2270114732Ssimokawa	switch(sdev->timeout) {
2271114732Ssimokawa	case 1:
2272110336Ssimokawa		printf("agent reset\n");
2273170374Ssimokawa		SBP_LOCK(sdev->target->sbp);
2274114732Ssimokawa		xpt_freeze_devq(sdev->path, 1);
2275114732Ssimokawa		sdev->freeze ++;
2276170374Ssimokawa		SBP_UNLOCK(sdev->target->sbp);
2277114732Ssimokawa		sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2278110336Ssimokawa		sbp_agent_reset(sdev);
2279114732Ssimokawa		break;
2280114732Ssimokawa	case 2:
2281114732Ssimokawa	case 3:
2282114732Ssimokawa		sbp_target_reset(sdev, sdev->timeout - 1);
2283114732Ssimokawa		break;
2284114732Ssimokawa#if 0
2285114732Ssimokawa	default:
2286114732Ssimokawa		/* XXX give up */
2287114732Ssimokawa		sbp_cam_detach_target(target);
2288114732Ssimokawa		if (target->luns != NULL)
2289114732Ssimokawa			free(target->luns, M_SBP);
2290114732Ssimokawa		target->num_lun = 0;;
2291114732Ssimokawa		target->luns = NULL;
2292114732Ssimokawa		target->fwdev = NULL;
2293114732Ssimokawa#endif
2294110336Ssimokawa	}
2295103285Sikob}
2296103285Sikob
2297103285Sikobstatic void
2298103285Sikobsbp_action1(struct cam_sim *sim, union ccb *ccb)
2299103285Sikob{
2300103285Sikob
2301103285Sikob	struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2302103285Sikob	struct sbp_target *target = NULL;
2303103285Sikob	struct sbp_dev *sdev = NULL;
2304103285Sikob
2305103285Sikob	/* target:lun -> sdev mapping */
2306103285Sikob	if (sbp != NULL
2307103285Sikob			&& ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2308103285Sikob			&& ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2309103285Sikob		target = &sbp->targets[ccb->ccb_h.target_id];
2310103285Sikob		if (target->fwdev != NULL
2311103285Sikob				&& ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2312103285Sikob				&& ccb->ccb_h.target_lun < target->num_lun) {
2313121185Ssimokawa			sdev = target->luns[ccb->ccb_h.target_lun];
2314121185Ssimokawa			if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED &&
2315103285Sikob				sdev->status != SBP_DEV_PROBE)
2316103285Sikob				sdev = NULL;
2317103285Sikob		}
2318103285Sikob	}
2319103285Sikob
2320103285SikobSBP_DEBUG(1)
2321103285Sikob	if (sdev == NULL)
2322103285Sikob		printf("invalid target %d lun %d\n",
2323103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2324103285SikobEND_DEBUG
2325103285Sikob
2326103285Sikob	switch (ccb->ccb_h.func_code) {
2327103285Sikob	case XPT_SCSI_IO:
2328103285Sikob	case XPT_RESET_DEV:
2329103285Sikob	case XPT_GET_TRAN_SETTINGS:
2330103285Sikob	case XPT_SET_TRAN_SETTINGS:
2331103285Sikob	case XPT_CALC_GEOMETRY:
2332103285Sikob		if (sdev == NULL) {
2333103285SikobSBP_DEBUG(1)
2334103285Sikob			printf("%s:%d:%d:func_code 0x%04x: "
2335103285Sikob				"Invalid target (target needed)\n",
2336103285Sikob				device_get_nameunit(sbp->fd.dev),
2337103285Sikob				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2338103285Sikob				ccb->ccb_h.func_code);
2339103285SikobEND_DEBUG
2340103285Sikob
2341108276Ssimokawa			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2342103285Sikob			xpt_done(ccb);
2343103285Sikob			return;
2344103285Sikob		}
2345103285Sikob		break;
2346103285Sikob	case XPT_PATH_INQ:
2347103285Sikob	case XPT_NOOP:
2348103285Sikob		/* The opcodes sometimes aimed at a target (sc is valid),
2349103285Sikob		 * sometimes aimed at the SIM (sc is invalid and target is
2350103285Sikob		 * CAM_TARGET_WILDCARD)
2351103285Sikob		 */
2352103285Sikob		if (sbp == NULL &&
2353103285Sikob			ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2354103285SikobSBP_DEBUG(0)
2355103285Sikob			printf("%s:%d:%d func_code 0x%04x: "
2356103285Sikob				"Invalid target (no wildcard)\n",
2357103285Sikob				device_get_nameunit(sbp->fd.dev),
2358103285Sikob				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2359103285Sikob				ccb->ccb_h.func_code);
2360103285SikobEND_DEBUG
2361108276Ssimokawa			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2362103285Sikob			xpt_done(ccb);
2363103285Sikob			return;
2364103285Sikob		}
2365103285Sikob		break;
2366103285Sikob	default:
2367103285Sikob		/* XXX Hm, we should check the input parameters */
2368103285Sikob		break;
2369103285Sikob	}
2370103285Sikob
2371103285Sikob	switch (ccb->ccb_h.func_code) {
2372103285Sikob	case XPT_SCSI_IO:
2373103285Sikob	{
2374103285Sikob		struct ccb_scsiio *csio;
2375103285Sikob		struct sbp_ocb *ocb;
2376113584Ssimokawa		int speed;
2377106506Ssimokawa		void *cdb;
2378103285Sikob
2379103285Sikob		csio = &ccb->csio;
2380170374Ssimokawa		mtx_assert(sim->mtx, MA_OWNED);
2381103285Sikob
2382121185SsimokawaSBP_DEBUG(2)
2383103285Sikob		printf("%s:%d:%d XPT_SCSI_IO: "
2384103285Sikob			"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2385103285Sikob			", flags: 0x%02x, "
2386103285Sikob			"%db cmd/%db data/%db sense\n",
2387103285Sikob			device_get_nameunit(sbp->fd.dev),
2388103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2389103285Sikob			csio->cdb_io.cdb_bytes[0],
2390103285Sikob			csio->cdb_io.cdb_bytes[1],
2391103285Sikob			csio->cdb_io.cdb_bytes[2],
2392103285Sikob			csio->cdb_io.cdb_bytes[3],
2393103285Sikob			csio->cdb_io.cdb_bytes[4],
2394103285Sikob			csio->cdb_io.cdb_bytes[5],
2395103285Sikob			csio->cdb_io.cdb_bytes[6],
2396103285Sikob			csio->cdb_io.cdb_bytes[7],
2397103285Sikob			csio->cdb_io.cdb_bytes[8],
2398103285Sikob			csio->cdb_io.cdb_bytes[9],
2399103285Sikob			ccb->ccb_h.flags & CAM_DIR_MASK,
2400103285Sikob			csio->cdb_len, csio->dxfer_len,
2401103285Sikob			csio->sense_len);
2402103285SikobEND_DEBUG
2403103285Sikob		if(sdev == NULL){
2404103285Sikob			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2405103285Sikob			xpt_done(ccb);
2406103285Sikob			return;
2407103285Sikob		}
2408103285Sikob#if 0
2409103285Sikob		/* if we are in probe stage, pass only probe commands */
2410103285Sikob		if (sdev->status == SBP_DEV_PROBE) {
2411103285Sikob			char *name;
2412103285Sikob			name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2413103285Sikob			printf("probe stage, periph name: %s\n", name);
2414103285Sikob			if (strcmp(name, "probe") != 0) {
2415103285Sikob				ccb->ccb_h.status = CAM_REQUEUE_REQ;
2416103285Sikob				xpt_done(ccb);
2417103285Sikob				return;
2418103285Sikob			}
2419103285Sikob		}
2420103285Sikob#endif
2421122528Ssimokawa		if ((ocb = sbp_get_ocb(sdev)) == NULL) {
2422122528Ssimokawa			ccb->ccb_h.status = CAM_REQUEUE_REQ;
2423127468Ssimokawa			if (sdev->freeze == 0) {
2424170374Ssimokawa				SBP_LOCK(sdev->target->sbp);
2425127468Ssimokawa				xpt_freeze_devq(sdev->path, 1);
2426127468Ssimokawa				sdev->freeze ++;
2427170374Ssimokawa				SBP_UNLOCK(sdev->target->sbp);
2428127468Ssimokawa			}
2429122528Ssimokawa			xpt_done(ccb);
2430103285Sikob			return;
2431122528Ssimokawa		}
2432113584Ssimokawa
2433103285Sikob		ocb->flags = OCB_ACT_CMD;
2434103285Sikob		ocb->sdev = sdev;
2435103285Sikob		ocb->ccb = ccb;
2436103285Sikob		ccb->ccb_h.ccb_sdev_ptr = sdev;
2437103285Sikob		ocb->orb[0] = htonl(1 << 31);
2438103285Sikob		ocb->orb[1] = 0;
2439103285Sikob		ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2440113584Ssimokawa		ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2441103285Sikob		speed = min(target->fwdev->speed, max_speed);
2442103285Sikob		ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2443103285Sikob						| ORB_CMD_MAXP(speed + 7));
2444103285Sikob		if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
2445103285Sikob			ocb->orb[4] |= htonl(ORB_CMD_IN);
2446103285Sikob		}
2447103285Sikob
2448103285Sikob		if (csio->ccb_h.flags & CAM_SCATTER_VALID)
2449103285Sikob			printf("sbp: CAM_SCATTER_VALID\n");
2450103285Sikob		if (csio->ccb_h.flags & CAM_DATA_PHYS)
2451103285Sikob			printf("sbp: CAM_DATA_PHYS\n");
2452103285Sikob
2453106506Ssimokawa		if (csio->ccb_h.flags & CAM_CDB_POINTER)
2454106506Ssimokawa			cdb = (void *)csio->cdb_io.cdb_ptr;
2455106506Ssimokawa		else
2456106506Ssimokawa			cdb = (void *)&csio->cdb_io.cdb_bytes;
2457120660Ssimokawa		bcopy(cdb, (void *)&ocb->orb[5], csio->cdb_len);
2458103285Sikob/*
2459103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2460103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2461103285Sikob*/
2462103285Sikob		if (ccb->csio.dxfer_len > 0) {
2463113584Ssimokawa			int s, error;
2464103285Sikob
2465103285Sikob			s = splsoftvm();
2466113584Ssimokawa			error = bus_dmamap_load(/*dma tag*/sbp->dmat,
2467103285Sikob					/*dma map*/ocb->dmamap,
2468103285Sikob					ccb->csio.data_ptr,
2469103285Sikob					ccb->csio.dxfer_len,
2470103285Sikob					sbp_execute_ocb,
2471103285Sikob					ocb,
2472103285Sikob					/*flags*/0);
2473103285Sikob			splx(s);
2474113584Ssimokawa			if (error)
2475113584Ssimokawa				printf("sbp: bus_dmamap_load error %d\n", error);
2476103285Sikob		} else
2477103285Sikob			sbp_execute_ocb(ocb, NULL, 0, 0);
2478103285Sikob		break;
2479103285Sikob	}
2480103285Sikob	case XPT_CALC_GEOMETRY:
2481103285Sikob	{
2482103285Sikob		struct ccb_calc_geometry *ccg;
2483127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 501100
2484129585Sdfr		uint32_t size_mb;
2485129585Sdfr		uint32_t secs_per_cylinder;
2486116429Ssimokawa		int extended = 1;
2487116429Ssimokawa#endif
2488116351Snjl
2489103285Sikob		ccg = &ccb->ccg;
2490103285Sikob		if (ccg->block_size == 0) {
2491103285Sikob			printf("sbp_action1: block_size is 0.\n");
2492103285Sikob			ccb->ccb_h.status = CAM_REQ_INVALID;
2493103285Sikob			xpt_done(ccb);
2494103285Sikob			break;
2495103285Sikob		}
2496103285SikobSBP_DEBUG(1)
2497103285Sikob		printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2498127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2499127468Ssimokawa			"Volume size = %d\n",
2500127468Ssimokawa#else
2501114540Sjake			"Volume size = %jd\n",
2502114927Ssimokawa#endif
2503114927Ssimokawa			device_get_nameunit(sbp->fd.dev),
2504114927Ssimokawa			cam_sim_path(sbp->sim),
2505103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2506127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2507114927Ssimokawa			(uintmax_t)
2508114927Ssimokawa#endif
2509114927Ssimokawa				ccg->volume_size);
2510103285SikobEND_DEBUG
2511103285Sikob
2512127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 501100
2513116429Ssimokawa		size_mb = ccg->volume_size
2514116429Ssimokawa			/ ((1024L * 1024L) / ccg->block_size);
2515116429Ssimokawa
2516116429Ssimokawa		if (size_mb > 1024 && extended) {
2517116429Ssimokawa			ccg->heads = 255;
2518116429Ssimokawa			ccg->secs_per_track = 63;
2519116429Ssimokawa		} else {
2520116429Ssimokawa			ccg->heads = 64;
2521116429Ssimokawa			ccg->secs_per_track = 32;
2522116429Ssimokawa		}
2523116429Ssimokawa		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2524116429Ssimokawa		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2525116429Ssimokawa		ccb->ccb_h.status = CAM_REQ_CMP;
2526116429Ssimokawa#else
2527116351Snjl		cam_calc_geometry(ccg, /*extended*/1);
2528116429Ssimokawa#endif
2529103285Sikob		xpt_done(ccb);
2530103285Sikob		break;
2531103285Sikob	}
2532103285Sikob	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
2533103285Sikob	{
2534103285Sikob
2535103285SikobSBP_DEBUG(1)
2536103285Sikob		printf("%s:%d:XPT_RESET_BUS: \n",
2537103285Sikob			device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2538103285SikobEND_DEBUG
2539103285Sikob
2540103285Sikob		ccb->ccb_h.status = CAM_REQ_INVALID;
2541103285Sikob		xpt_done(ccb);
2542103285Sikob		break;
2543103285Sikob	}
2544103285Sikob	case XPT_PATH_INQ:		/* Path routing inquiry */
2545103285Sikob	{
2546103285Sikob		struct ccb_pathinq *cpi = &ccb->cpi;
2547103285Sikob
2548103285SikobSBP_DEBUG(1)
2549103285Sikob		printf("%s:%d:%d XPT_PATH_INQ:.\n",
2550103285Sikob			device_get_nameunit(sbp->fd.dev),
2551103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2552103285SikobEND_DEBUG
2553103285Sikob		cpi->version_num = 1; /* XXX??? */
2554111615Ssimokawa		cpi->hba_inquiry = PI_TAG_ABLE;
2555103285Sikob		cpi->target_sprt = 0;
2556118105Snjl		cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE;
2557103285Sikob		cpi->hba_eng_cnt = 0;
2558103285Sikob		cpi->max_target = SBP_NUM_TARGETS - 1;
2559103285Sikob		cpi->max_lun = SBP_NUM_LUNS - 1;
2560103285Sikob		cpi->initiator_id = SBP_INITIATOR;
2561103285Sikob		cpi->bus_id = sim->bus_id;
2562103285Sikob		cpi->base_transfer_speed = 400 * 1000 / 8;
2563103285Sikob		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2564103285Sikob		strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2565103285Sikob		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2566103285Sikob		cpi->unit_number = sim->unit_number;
2567163816Smjacob                cpi->transport = XPORT_SPI;	/* XX should have a FireWire */
2568163816Smjacob                cpi->transport_version = 2;
2569163816Smjacob                cpi->protocol = PROTO_SCSI;
2570163816Smjacob                cpi->protocol_version = SCSI_REV_2;
2571103285Sikob
2572103285Sikob		cpi->ccb_h.status = CAM_REQ_CMP;
2573103285Sikob		xpt_done(ccb);
2574103285Sikob		break;
2575103285Sikob	}
2576103285Sikob	case XPT_GET_TRAN_SETTINGS:
2577103285Sikob	{
2578103285Sikob		struct ccb_trans_settings *cts = &ccb->cts;
2579163816Smjacob		struct ccb_trans_settings_scsi *scsi =
2580163816Smjacob		    &cts->proto_specific.scsi;
2581163816Smjacob		struct ccb_trans_settings_spi *spi =
2582163816Smjacob		    &cts->xport_specific.spi;
2583163816Smjacob
2584163816Smjacob		cts->protocol = PROTO_SCSI;
2585163816Smjacob		cts->protocol_version = SCSI_REV_2;
2586163816Smjacob		cts->transport = XPORT_SPI;	/* should have a FireWire */
2587163816Smjacob		cts->transport_version = 2;
2588163816Smjacob		spi->valid = CTS_SPI_VALID_DISC;
2589163816Smjacob		spi->flags = CTS_SPI_FLAGS_DISC_ENB;
2590163816Smjacob		scsi->valid = CTS_SCSI_VALID_TQ;
2591163816Smjacob		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2592103285SikobSBP_DEBUG(1)
2593103285Sikob		printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2594103285Sikob			device_get_nameunit(sbp->fd.dev),
2595103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2596103285SikobEND_DEBUG
2597103285Sikob		cts->ccb_h.status = CAM_REQ_CMP;
2598103285Sikob		xpt_done(ccb);
2599103285Sikob		break;
2600103285Sikob	}
2601103285Sikob	case XPT_ABORT:
2602103285Sikob		ccb->ccb_h.status = CAM_UA_ABORT;
2603103285Sikob		xpt_done(ccb);
2604103285Sikob		break;
2605111615Ssimokawa	case XPT_SET_TRAN_SETTINGS:
2606111615Ssimokawa		/* XXX */
2607103285Sikob	default:
2608103285Sikob		ccb->ccb_h.status = CAM_REQ_INVALID;
2609103285Sikob		xpt_done(ccb);
2610103285Sikob		break;
2611103285Sikob	}
2612103285Sikob	return;
2613103285Sikob}
2614103285Sikob
2615103285Sikobstatic void
2616103285Sikobsbp_action(struct cam_sim *sim, union ccb *ccb)
2617103285Sikob{
2618103285Sikob	int s;
2619103285Sikob
2620103285Sikob	s = splfw();
2621103285Sikob	sbp_action1(sim, ccb);
2622103285Sikob	splx(s);
2623103285Sikob}
2624103285Sikob
2625103285Sikobstatic void
2626103285Sikobsbp_execute_ocb(void *arg,  bus_dma_segment_t *segments, int seg, int error)
2627103285Sikob{
2628103285Sikob	int i;
2629103285Sikob	struct sbp_ocb *ocb;
2630103285Sikob	struct sbp_ocb *prev;
2631105633Ssimokawa	bus_dma_segment_t *s;
2632103285Sikob
2633103285Sikob	if (error)
2634103285Sikob		printf("sbp_execute_ocb: error=%d\n", error);
2635103285Sikob
2636103285Sikob	ocb = (struct sbp_ocb *)arg;
2637113584Ssimokawa
2638121185SsimokawaSBP_DEBUG(2)
2639113584Ssimokawa	printf("sbp_execute_ocb: seg %d", seg);
2640113584Ssimokawa	for (i = 0; i < seg; i++)
2641127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2642127468Ssimokawa		printf(", %x:%d", segments[i].ds_addr, segments[i].ds_len);
2643127468Ssimokawa#else
2644113972Ssimokawa		printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2645113972Ssimokawa					(uintmax_t)segments[i].ds_len);
2646113584Ssimokawa#endif
2647113584Ssimokawa	printf("\n");
2648113584SsimokawaEND_DEBUG
2649113584Ssimokawa
2650103285Sikob	if (seg == 1) {
2651103285Sikob		/* direct pointer */
2652113474Ssimokawa		s = &segments[0];
2653113474Ssimokawa		if (s->ds_len > SBP_SEG_MAX)
2654113474Ssimokawa			panic("ds_len > SBP_SEG_MAX, fix busdma code");
2655113474Ssimokawa		ocb->orb[3] = htonl(s->ds_addr);
2656113474Ssimokawa		ocb->orb[4] |= htonl(s->ds_len);
2657103285Sikob	} else if(seg > 1) {
2658103285Sikob		/* page table */
2659103285Sikob		for (i = 0; i < seg; i++) {
2660105633Ssimokawa			s = &segments[i];
2661108877SsimokawaSBP_DEBUG(0)
2662108877Ssimokawa			/* XXX LSI Logic "< 16 byte" bug might be hit */
2663105633Ssimokawa			if (s->ds_len < 16)
2664105633Ssimokawa				printf("sbp_execute_ocb: warning, "
2665127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2666127468Ssimokawa					"segment length(%d) is less than 16."
2667127468Ssimokawa#else
2668106543Ssimokawa					"segment length(%zd) is less than 16."
2669108877Ssimokawa#endif
2670105633Ssimokawa					"(seg=%d/%d)\n", s->ds_len, i+1, seg);
2671108877SsimokawaEND_DEBUG
2672113474Ssimokawa			if (s->ds_len > SBP_SEG_MAX)
2673113474Ssimokawa				panic("ds_len > SBP_SEG_MAX, fix busdma code");
2674105633Ssimokawa			ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2675105633Ssimokawa			ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2676103285Sikob		}
2677103285Sikob		ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2678103285Sikob	}
2679103285Sikob
2680113584Ssimokawa	if (seg > 0)
2681113584Ssimokawa		bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2682113584Ssimokawa			(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2683113584Ssimokawa			BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2684103285Sikob	prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2685113584Ssimokawa	fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2686127468Ssimokawa	if (use_doorbell) {
2687127468Ssimokawa		if (prev == NULL) {
2688127468Ssimokawa			if (ocb->sdev->last_ocb != NULL)
2689127468Ssimokawa				sbp_doorbell(ocb->sdev);
2690127468Ssimokawa			else
2691127468Ssimokawa				sbp_orb_pointer(ocb->sdev, ocb);
2692127468Ssimokawa		}
2693127468Ssimokawa	} else {
2694127468Ssimokawa		if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) {
2695127468Ssimokawa			ocb->sdev->flags &= ~ORB_LINK_DEAD;
2696127468Ssimokawa			sbp_orb_pointer(ocb->sdev, ocb);
2697127468Ssimokawa		}
2698120842Ssimokawa	}
2699103285Sikob}
2700103285Sikob
2701103285Sikobstatic void
2702103285Sikobsbp_poll(struct cam_sim *sim)
2703103285Sikob{
2704120847Ssimokawa	struct sbp_softc *sbp;
2705120847Ssimokawa	struct firewire_comm *fc;
2706120847Ssimokawa
2707120847Ssimokawa	sbp = (struct sbp_softc *)sim->softc;
2708120847Ssimokawa	fc = sbp->fd.fc;
2709120847Ssimokawa
2710120847Ssimokawa	fc->poll(fc, 0, -1);
2711120847Ssimokawa
2712103285Sikob	return;
2713103285Sikob}
2714120847Ssimokawa
2715103285Sikobstatic struct sbp_ocb *
2716111615Ssimokawasbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2717103285Sikob{
2718103285Sikob	struct sbp_ocb *ocb;
2719103285Sikob	struct sbp_ocb *next;
2720103285Sikob	int s = splfw(), order = 0;
2721103285Sikob	int flags;
2722103285Sikob
2723103285SikobSBP_DEBUG(1)
2724121185Ssimokawa	sbp_show_sdev_info(sdev, 2);
2725127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2726127468Ssimokawa	printf("%s: 0x%08lx src %d\n",
2727127468Ssimokawa#else
2728121185Ssimokawa	printf("%s: 0x%08x src %d\n",
2729108712Ssimokawa#endif
2730127468Ssimokawa	    __func__, ntohl(sbp_status->orb_lo), sbp_status->src);
2731103285SikobEND_DEBUG
2732170374Ssimokawa	SBP_LOCK(sdev->target->sbp);
2733121185Ssimokawa	for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2734121185Ssimokawa		next = STAILQ_NEXT(ocb, ocb);
2735121185Ssimokawa		flags = ocb->flags;
2736111615Ssimokawa		if (OCB_MATCH(ocb, sbp_status)) {
2737103285Sikob			/* found */
2738111819Ssimokawa			STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2739103285Sikob			if (ocb->ccb != NULL)
2740103285Sikob				untimeout(sbp_timeout, (caddr_t)ocb,
2741103285Sikob						ocb->ccb->ccb_h.timeout_ch);
2742113584Ssimokawa			if (ntohl(ocb->orb[4]) & 0xffff) {
2743113584Ssimokawa				bus_dmamap_sync(sdev->target->sbp->dmat,
2744113584Ssimokawa					ocb->dmamap,
2745113584Ssimokawa					(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2746113584Ssimokawa					BUS_DMASYNC_POSTREAD :
2747113584Ssimokawa					BUS_DMASYNC_POSTWRITE);
2748113584Ssimokawa				bus_dmamap_unload(sdev->target->sbp->dmat,
2749113584Ssimokawa					ocb->dmamap);
2750103285Sikob			}
2751127468Ssimokawa			if (!use_doorbell) {
2752127468Ssimokawa				if (sbp_status->src == SRC_NO_NEXT) {
2753127468Ssimokawa					if (next != NULL)
2754127468Ssimokawa						sbp_orb_pointer(sdev, next);
2755127468Ssimokawa					else if (order > 0) {
2756127468Ssimokawa						/*
2757127468Ssimokawa						 * Unordered execution
2758127468Ssimokawa						 * We need to send pointer for
2759127468Ssimokawa						 * next ORB
2760127468Ssimokawa						 */
2761127468Ssimokawa						sdev->flags |= ORB_LINK_DEAD;
2762127468Ssimokawa					}
2763120842Ssimokawa				}
2764127468Ssimokawa			} else {
2765127468Ssimokawa				/*
2766127468Ssimokawa				 * XXX this is not correct for unordered
2767127468Ssimokawa				 * execution.
2768127468Ssimokawa				 */
2769127468Ssimokawa				if (sdev->last_ocb != NULL)
2770127468Ssimokawa					sbp_free_ocb(sdev, sdev->last_ocb);
2771127468Ssimokawa				sdev->last_ocb = ocb;
2772127468Ssimokawa				if (next != NULL &&
2773127468Ssimokawa				    sbp_status->src == SRC_NO_NEXT)
2774127468Ssimokawa					sbp_doorbell(sdev);
2775120842Ssimokawa			}
2776103285Sikob			break;
2777111819Ssimokawa		} else
2778111819Ssimokawa			order ++;
2779103285Sikob	}
2780170374Ssimokawa	SBP_UNLOCK(sdev->target->sbp);
2781103285Sikob	splx(s);
2782103285SikobSBP_DEBUG(0)
2783103285Sikob	if (ocb && order > 0) {
2784103285Sikob		sbp_show_sdev_info(sdev, 2);
2785103285Sikob		printf("unordered execution order:%d\n", order);
2786103285Sikob	}
2787103285SikobEND_DEBUG
2788103285Sikob	return (ocb);
2789103285Sikob}
2790103285Sikob
2791103285Sikobstatic struct sbp_ocb *
2792103285Sikobsbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2793103285Sikob{
2794103285Sikob	int s = splfw();
2795127468Ssimokawa	struct sbp_ocb *prev, *prev2;
2796103285Sikob
2797121185SsimokawaSBP_DEBUG(1)
2798103285Sikob	sbp_show_sdev_info(sdev, 2);
2799127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2800127468Ssimokawa	printf("%s: 0x%08x\n", __func__, ocb->bus_addr);
2801108712Ssimokawa#else
2802127468Ssimokawa	printf("%s: 0x%08jx\n", __func__, (uintmax_t)ocb->bus_addr);
2803108712Ssimokawa#endif
2804103285SikobEND_DEBUG
2805127468Ssimokawa	prev2 = prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2806103285Sikob	STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2807103285Sikob
2808103285Sikob	if (ocb->ccb != NULL)
2809103285Sikob		ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb,
2810103285Sikob					(ocb->ccb->ccb_h.timeout * hz) / 1000);
2811103285Sikob
2812127468Ssimokawa	if (use_doorbell && prev == NULL)
2813127468Ssimokawa		prev2 = sdev->last_ocb;
2814127468Ssimokawa
2815127468Ssimokawa	if (prev2 != NULL) {
2816121185SsimokawaSBP_DEBUG(2)
2817127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2818127468Ssimokawa		printf("linking chain 0x%x -> 0x%x\n",
2819127468Ssimokawa		    prev2->bus_addr, ocb->bus_addr);
2820127468Ssimokawa#else
2821120842Ssimokawa		printf("linking chain 0x%jx -> 0x%jx\n",
2822127468Ssimokawa		    (uintmax_t)prev2->bus_addr, (uintmax_t)ocb->bus_addr);
2823108712Ssimokawa#endif
2824103285SikobEND_DEBUG
2825127468Ssimokawa		prev2->orb[1] = htonl(ocb->bus_addr);
2826127468Ssimokawa		prev2->orb[0] = 0;
2827103285Sikob	}
2828103285Sikob	splx(s);
2829103285Sikob
2830103285Sikob	return prev;
2831103285Sikob}
2832103285Sikob
2833103285Sikobstatic struct sbp_ocb *
2834113584Ssimokawasbp_get_ocb(struct sbp_dev *sdev)
2835103285Sikob{
2836103285Sikob	struct sbp_ocb *ocb;
2837103285Sikob	int s = splfw();
2838170374Ssimokawa
2839170374Ssimokawa	mtx_assert(&sdev->target->sbp->mtx, MA_OWNED);
2840113584Ssimokawa	ocb = STAILQ_FIRST(&sdev->free_ocbs);
2841103285Sikob	if (ocb == NULL) {
2842127468Ssimokawa		sdev->flags |= ORB_SHORTAGE;
2843103285Sikob		printf("ocb shortage!!!\n");
2844134868Ssimokawa		splx(s);
2845103285Sikob		return NULL;
2846103285Sikob	}
2847113584Ssimokawa	STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2848103285Sikob	splx(s);
2849103285Sikob	ocb->ccb = NULL;
2850103285Sikob	return (ocb);
2851103285Sikob}
2852103285Sikob
2853103285Sikobstatic void
2854113584Ssimokawasbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2855103285Sikob{
2856103285Sikob	ocb->flags = 0;
2857103285Sikob	ocb->ccb = NULL;
2858170374Ssimokawa
2859170374Ssimokawa	SBP_LOCK(sdev->target->sbp);
2860113584Ssimokawa	STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2861127468Ssimokawa	if ((sdev->flags & ORB_SHORTAGE) != 0) {
2862127468Ssimokawa		int count;
2863127468Ssimokawa
2864127468Ssimokawa		sdev->flags &= ~ORB_SHORTAGE;
2865127468Ssimokawa		count = sdev->freeze;
2866127468Ssimokawa		sdev->freeze = 0;
2867127468Ssimokawa		xpt_release_devq(sdev->path, count, TRUE);
2868127468Ssimokawa	}
2869170374Ssimokawa	SBP_UNLOCK(sdev->target->sbp);
2870103285Sikob}
2871103285Sikob
2872103285Sikobstatic void
2873103285Sikobsbp_abort_ocb(struct sbp_ocb *ocb, int status)
2874103285Sikob{
2875103285Sikob	struct sbp_dev *sdev;
2876103285Sikob
2877103285Sikob	sdev = ocb->sdev;
2878113584SsimokawaSBP_DEBUG(0)
2879103285Sikob	sbp_show_sdev_info(sdev, 2);
2880127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2881127468Ssimokawa	printf("sbp_abort_ocb 0x%x\n", ocb->bus_addr);
2882127468Ssimokawa#else
2883113972Ssimokawa	printf("sbp_abort_ocb 0x%jx\n", (uintmax_t)ocb->bus_addr);
2884111819Ssimokawa#endif
2885113584SsimokawaEND_DEBUG
2886113584SsimokawaSBP_DEBUG(1)
2887105792Ssimokawa	if (ocb->ccb != NULL)
2888105792Ssimokawa		sbp_print_scsi_cmd(ocb);
2889103285SikobEND_DEBUG
2890113584Ssimokawa	if (ntohl(ocb->orb[4]) & 0xffff) {
2891113584Ssimokawa		bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
2892113584Ssimokawa			(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2893113584Ssimokawa			BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2894113584Ssimokawa		bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
2895113584Ssimokawa	}
2896111819Ssimokawa	if (ocb->ccb != NULL) {
2897110336Ssimokawa		untimeout(sbp_timeout, (caddr_t)ocb,
2898110336Ssimokawa					ocb->ccb->ccb_h.timeout_ch);
2899103285Sikob		ocb->ccb->ccb_h.status = status;
2900170374Ssimokawa		SBP_LOCK(sdev->target->sbp);
2901103285Sikob		xpt_done(ocb->ccb);
2902170374Ssimokawa		SBP_UNLOCK(sdev->target->sbp);
2903103285Sikob	}
2904113584Ssimokawa	sbp_free_ocb(sdev, ocb);
2905103285Sikob}
2906103285Sikob
2907103285Sikobstatic void
2908103285Sikobsbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2909103285Sikob{
2910103285Sikob	int s;
2911105792Ssimokawa	struct sbp_ocb *ocb, *next;
2912105792Ssimokawa	STAILQ_HEAD(, sbp_ocb) temp;
2913103285Sikob
2914103285Sikob	s = splfw();
2915105792Ssimokawa
2916105792Ssimokawa	bcopy(&sdev->ocbs, &temp, sizeof(temp));
2917105792Ssimokawa	STAILQ_INIT(&sdev->ocbs);
2918105792Ssimokawa	for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2919105792Ssimokawa		next = STAILQ_NEXT(ocb, ocb);
2920103285Sikob		sbp_abort_ocb(ocb, status);
2921103285Sikob	}
2922127468Ssimokawa	if (sdev->last_ocb != NULL) {
2923127468Ssimokawa		sbp_free_ocb(sdev, sdev->last_ocb);
2924127468Ssimokawa		sdev->last_ocb = NULL;
2925127468Ssimokawa	}
2926105792Ssimokawa
2927103285Sikob	splx(s);
2928103285Sikob}
2929103285Sikob
2930103285Sikobstatic devclass_t sbp_devclass;
2931103285Sikob
2932103285Sikobstatic device_method_t sbp_methods[] = {
2933103285Sikob	/* device interface */
2934103285Sikob	DEVMETHOD(device_identify,	sbp_identify),
2935103285Sikob	DEVMETHOD(device_probe,		sbp_probe),
2936103285Sikob	DEVMETHOD(device_attach,	sbp_attach),
2937103285Sikob	DEVMETHOD(device_detach,	sbp_detach),
2938110145Ssimokawa	DEVMETHOD(device_shutdown,	sbp_shutdown),
2939103285Sikob
2940103285Sikob	{ 0, 0 }
2941103285Sikob};
2942103285Sikob
2943103285Sikobstatic driver_t sbp_driver = {
2944103285Sikob	"sbp",
2945103285Sikob	sbp_methods,
2946103285Sikob	sizeof(struct sbp_softc),
2947103285Sikob};
2948127468Ssimokawa#ifdef __DragonFly__
2949127468SsimokawaDECLARE_DUMMY_MODULE(sbp);
2950127468Ssimokawa#endif
2951103285SikobDRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2952103285SikobMODULE_VERSION(sbp, 1);
2953103285SikobMODULE_DEPEND(sbp, firewire, 1, 1, 1);
2954103285SikobMODULE_DEPEND(sbp, cam, 1, 1, 1);
2955