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