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