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