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