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