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