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