xref: /freebsd/sys/dev/firewire/sbp.c (revision 7bd6fde3951af84ef3b68e4d1eadc1840c2fc1b3)
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_DMA_SIZE PAGE_SIZE
95 #define SBP_LOGIN_SIZE sizeof(struct sbp_login_res)
96 #define SBP_QUEUE_LEN ((SBP_DMA_SIZE - SBP_LOGIN_SIZE) / sizeof(struct sbp_ocb))
97 #define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS)
98 
99 /*
100  * STATUS FIFO addressing
101  *   bit
102  * -----------------------
103  *  0- 1( 2): 0 (alignment)
104  *  2- 7( 6): target
105  *  8-15( 8): lun
106  * 16-31( 8): reserved
107  * 32-47(16): SBP_BIND_HI
108  * 48-64(16): bus_id, node_id
109  */
110 #define SBP_BIND_HI 0x1
111 #define SBP_DEV2ADDR(t, l) \
112 	(((u_int64_t)SBP_BIND_HI << 32) \
113 	| (((l) & 0xff) << 8) \
114 	| (((t) & 0x3f) << 2))
115 #define SBP_ADDR2TRG(a)	(((a) >> 2) & 0x3f)
116 #define SBP_ADDR2LUN(a)	(((a) >> 8) & 0xff)
117 #define SBP_INITIATOR 7
118 
119 static char *orb_fun_name[] = {
120 	ORB_FUN_NAMES
121 };
122 
123 static int debug = 0;
124 static int auto_login = 1;
125 static int max_speed = -1;
126 static int sbp_cold = 1;
127 static int ex_login = 1;
128 static int login_delay = 1000;	/* msec */
129 static int scan_delay = 500;	/* msec */
130 static int use_doorbell = 0;
131 static int sbp_tags = 0;
132 
133 SYSCTL_DECL(_hw_firewire);
134 SYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem");
135 SYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0,
136 	"SBP debug flag");
137 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0,
138 	"SBP perform login automatically");
139 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0,
140 	"SBP transfer max speed");
141 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, exclusive_login, CTLFLAG_RW,
142 	&ex_login, 0, "SBP enable exclusive login");
143 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, login_delay, CTLFLAG_RW,
144 	&login_delay, 0, "SBP login delay in msec");
145 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, scan_delay, CTLFLAG_RW,
146 	&scan_delay, 0, "SBP scan delay in msec");
147 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, use_doorbell, CTLFLAG_RW,
148 	&use_doorbell, 0, "SBP use doorbell request");
149 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, tags, CTLFLAG_RW, &sbp_tags, 0,
150 	"SBP tagged queuing support");
151 
152 TUNABLE_INT("hw.firewire.sbp.auto_login", &auto_login);
153 TUNABLE_INT("hw.firewire.sbp.max_speed", &max_speed);
154 TUNABLE_INT("hw.firewire.sbp.exclusive_login", &ex_login);
155 TUNABLE_INT("hw.firewire.sbp.login_delay", &login_delay);
156 TUNABLE_INT("hw.firewire.sbp.scan_delay", &scan_delay);
157 TUNABLE_INT("hw.firewire.sbp.use_doorbell", &use_doorbell);
158 TUNABLE_INT("hw.firewire.sbp.tags", &sbp_tags);
159 
160 #define NEED_RESPONSE 0
161 
162 #define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE)
163 #ifdef __sparc64__ /* iommu */
164 #define SBP_IND_MAX howmany(MAXPHYS, SBP_SEG_MAX)
165 #else
166 #define SBP_IND_MAX howmany(MAXPHYS, PAGE_SIZE)
167 #endif
168 struct sbp_ocb {
169 	STAILQ_ENTRY(sbp_ocb)	ocb;
170 	union ccb	*ccb;
171 	bus_addr_t	bus_addr;
172 	uint32_t	orb[8];
173 #define IND_PTR_OFFSET	(8*sizeof(uint32_t))
174 	struct ind_ptr  ind_ptr[SBP_IND_MAX];
175 	struct sbp_dev	*sdev;
176 	int		flags; /* XXX should be removed */
177 	bus_dmamap_t	dmamap;
178 };
179 
180 #define OCB_ACT_MGM 0
181 #define OCB_ACT_CMD 1
182 #define OCB_MATCH(o,s)	((o)->bus_addr == ntohl((s)->orb_lo))
183 
184 struct sbp_dev{
185 #define SBP_DEV_RESET		0	/* accept login */
186 #define SBP_DEV_LOGIN		1	/* to login */
187 #if 0
188 #define SBP_DEV_RECONN		2	/* to reconnect */
189 #endif
190 #define SBP_DEV_TOATTACH	3	/* to attach */
191 #define SBP_DEV_PROBE		4	/* scan lun */
192 #define SBP_DEV_ATTACHED	5	/* in operation */
193 #define SBP_DEV_DEAD		6	/* unavailable unit */
194 #define SBP_DEV_RETRY		7	/* unavailable unit */
195 	uint8_t status:4,
196 		 timeout:4;
197 	uint8_t type;
198 	uint16_t lun_id;
199 	uint16_t freeze;
200 #define	ORB_LINK_DEAD		(1 << 0)
201 #define	VALID_LUN		(1 << 1)
202 #define	ORB_POINTER_ACTIVE	(1 << 2)
203 #define	ORB_POINTER_NEED	(1 << 3)
204 #define	ORB_DOORBELL_ACTIVE	(1 << 4)
205 #define	ORB_DOORBELL_NEED	(1 << 5)
206 #define	ORB_SHORTAGE		(1 << 6)
207 	uint16_t flags;
208 	struct cam_path *path;
209 	struct sbp_target *target;
210 	struct fwdma_alloc dma;
211 	struct sbp_login_res *login;
212 	struct callout login_callout;
213 	struct sbp_ocb *ocb;
214 	STAILQ_HEAD(, sbp_ocb) ocbs;
215 	STAILQ_HEAD(, sbp_ocb) free_ocbs;
216 	struct sbp_ocb *last_ocb;
217 	char vendor[32];
218 	char product[32];
219 	char revision[10];
220 };
221 
222 struct sbp_target {
223 	int target_id;
224 	int num_lun;
225 	struct sbp_dev	**luns;
226 	struct sbp_softc *sbp;
227 	struct fw_device *fwdev;
228 	uint32_t mgm_hi, mgm_lo;
229 	struct sbp_ocb *mgm_ocb_cur;
230 	STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue;
231 	struct callout mgm_ocb_timeout;
232 	struct callout scan_callout;
233 	STAILQ_HEAD(, fw_xfer) xferlist;
234 	int n_xfer;
235 };
236 
237 struct sbp_softc {
238 	struct firewire_dev_comm fd;
239 	struct cam_sim  *sim;
240 	struct cam_path  *path;
241 	struct sbp_target targets[SBP_NUM_TARGETS];
242 	struct fw_bind fwb;
243 	bus_dma_tag_t	dmat;
244 	struct timeval last_busreset;
245 #define SIMQ_FREEZED 1
246 	int flags;
247 };
248 
249 static void sbp_post_explore (void *);
250 static void sbp_recv (struct fw_xfer *);
251 static void sbp_mgm_callback (struct fw_xfer *);
252 #if 0
253 static void sbp_cmd_callback (struct fw_xfer *);
254 #endif
255 static void sbp_orb_pointer (struct sbp_dev *, struct sbp_ocb *);
256 static void sbp_doorbell(struct sbp_dev *);
257 static void sbp_execute_ocb (void *,  bus_dma_segment_t *, int, int);
258 static void sbp_free_ocb (struct sbp_dev *, struct sbp_ocb *);
259 static void sbp_abort_ocb (struct sbp_ocb *, int);
260 static void sbp_abort_all_ocbs (struct sbp_dev *, int);
261 static struct fw_xfer * sbp_write_cmd (struct sbp_dev *, int, int);
262 static struct sbp_ocb * sbp_get_ocb (struct sbp_dev *);
263 static struct sbp_ocb * sbp_enqueue_ocb (struct sbp_dev *, struct sbp_ocb *);
264 static struct sbp_ocb * sbp_dequeue_ocb (struct sbp_dev *, struct sbp_status *);
265 static void sbp_cam_detach_sdev(struct sbp_dev *);
266 static void sbp_free_sdev(struct sbp_dev *);
267 static void sbp_cam_detach_target (struct sbp_target *);
268 static void sbp_free_target (struct sbp_target *);
269 static void sbp_mgm_timeout (void *arg);
270 static void sbp_timeout (void *arg);
271 static void sbp_mgm_orb (struct sbp_dev *, int, struct sbp_ocb *);
272 
273 MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire");
274 
275 /* cam related functions */
276 static void	sbp_action(struct cam_sim *sim, union ccb *ccb);
277 static void	sbp_poll(struct cam_sim *sim);
278 static void	sbp_cam_scan_lun(struct cam_periph *, union ccb *);
279 static void	sbp_cam_scan_target(void *arg);
280 
281 static char *orb_status0[] = {
282 	/* 0 */ "No additional information to report",
283 	/* 1 */ "Request type not supported",
284 	/* 2 */ "Speed not supported",
285 	/* 3 */ "Page size not supported",
286 	/* 4 */ "Access denied",
287 	/* 5 */ "Logical unit not supported",
288 	/* 6 */ "Maximum payload too small",
289 	/* 7 */ "Reserved for future standardization",
290 	/* 8 */ "Resources unavailable",
291 	/* 9 */ "Function rejected",
292 	/* A */ "Login ID not recognized",
293 	/* B */ "Dummy ORB completed",
294 	/* C */ "Request aborted",
295 	/* FF */ "Unspecified error"
296 #define MAX_ORB_STATUS0 0xd
297 };
298 
299 static char *orb_status1_object[] = {
300 	/* 0 */ "Operation request block (ORB)",
301 	/* 1 */ "Data buffer",
302 	/* 2 */ "Page table",
303 	/* 3 */ "Unable to specify"
304 };
305 
306 static char *orb_status1_serial_bus_error[] = {
307 	/* 0 */ "Missing acknowledge",
308 	/* 1 */ "Reserved; not to be used",
309 	/* 2 */ "Time-out error",
310 	/* 3 */ "Reserved; not to be used",
311 	/* 4 */ "Busy retry limit exceeded(X)",
312 	/* 5 */ "Busy retry limit exceeded(A)",
313 	/* 6 */ "Busy retry limit exceeded(B)",
314 	/* 7 */ "Reserved for future standardization",
315 	/* 8 */ "Reserved for future standardization",
316 	/* 9 */ "Reserved for future standardization",
317 	/* A */ "Reserved for future standardization",
318 	/* B */ "Tardy retry limit exceeded",
319 	/* C */ "Conflict error",
320 	/* D */ "Data error",
321 	/* E */ "Type error",
322 	/* F */ "Address error"
323 };
324 
325 static void
326 sbp_identify(driver_t *driver, device_t parent)
327 {
328 	device_t child;
329 SBP_DEBUG(0)
330 	printf("sbp_identify\n");
331 END_DEBUG
332 
333 	child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent));
334 }
335 
336 /*
337  * sbp_probe()
338  */
339 static int
340 sbp_probe(device_t dev)
341 {
342 	device_t pa;
343 
344 SBP_DEBUG(0)
345 	printf("sbp_probe\n");
346 END_DEBUG
347 
348 	pa = device_get_parent(dev);
349 	if(device_get_unit(dev) != device_get_unit(pa)){
350 		return(ENXIO);
351 	}
352 
353 	device_set_desc(dev, "SBP-2/SCSI over FireWire");
354 
355 #if 0
356 	if (bootverbose)
357 		debug = bootverbose;
358 #endif
359 
360 	return (0);
361 }
362 
363 static void
364 sbp_show_sdev_info(struct sbp_dev *sdev, int new)
365 {
366 	struct fw_device *fwdev;
367 
368 	printf("%s:%d:%d ",
369 		device_get_nameunit(sdev->target->sbp->fd.dev),
370 		sdev->target->target_id,
371 		sdev->lun_id
372 	);
373 	if (new == 2) {
374 		return;
375 	}
376 	fwdev = sdev->target->fwdev;
377 	printf("ordered:%d type:%d EUI:%08x%08x node:%d "
378 		"speed:%d maxrec:%d",
379 		(sdev->type & 0x40) >> 6,
380 		(sdev->type & 0x1f),
381 		fwdev->eui.hi,
382 		fwdev->eui.lo,
383 		fwdev->dst,
384 		fwdev->speed,
385 		fwdev->maxrec
386 	);
387 	if (new)
388 		printf(" new!\n");
389 	else
390 		printf("\n");
391 	sbp_show_sdev_info(sdev, 2);
392 	printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision);
393 }
394 
395 static struct {
396 	int bus;
397 	int target;
398 	struct fw_eui64 eui;
399 } wired[] = {
400 	/* Bus	Target	EUI64 */
401 #if 0
402 	{0,	2,	{0x00018ea0, 0x01fd0154}},	/* Logitec HDD */
403 	{0,	0,	{0x00018ea6, 0x00100682}},	/* Logitec DVD */
404 	{0,	1,	{0x00d03200, 0xa412006a}},	/* Yano HDD */
405 #endif
406 	{-1,	-1,	{0,0}}
407 };
408 
409 static int
410 sbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev)
411 {
412 	int bus, i, target=-1;
413 	char w[SBP_NUM_TARGETS];
414 
415 	bzero(w, sizeof(w));
416 	bus = device_get_unit(sbp->fd.dev);
417 
418 	/* XXX wired-down configuration should be gotten from
419 					tunable or device hint */
420 	for (i = 0; wired[i].bus >= 0; i ++) {
421 		if (wired[i].bus == bus) {
422 			w[wired[i].target] = 1;
423 			if (wired[i].eui.hi == fwdev->eui.hi &&
424 					wired[i].eui.lo == fwdev->eui.lo)
425 				target = wired[i].target;
426 		}
427 	}
428 	if (target >= 0) {
429 		if(target < SBP_NUM_TARGETS &&
430 				sbp->targets[target].fwdev == NULL)
431 			return(target);
432 		device_printf(sbp->fd.dev,
433 			"target %d is not free for %08x:%08x\n",
434 			target, fwdev->eui.hi, fwdev->eui.lo);
435 		target = -1;
436 	}
437 	/* non-wired target */
438 	for (i = 0; i < SBP_NUM_TARGETS; i ++)
439 		if (sbp->targets[i].fwdev == NULL && w[i] == 0) {
440 			target = i;
441 			break;
442 		}
443 
444 	return target;
445 }
446 
447 static void
448 sbp_alloc_lun(struct sbp_target *target)
449 {
450 	struct crom_context cc;
451 	struct csrreg *reg;
452 	struct sbp_dev *sdev, **newluns;
453 	struct sbp_softc *sbp;
454 	int maxlun, lun, i;
455 
456 	sbp = target->sbp;
457 	crom_init_context(&cc, target->fwdev->csrrom);
458 	/* XXX shoud parse appropriate unit directories only */
459 	maxlun = -1;
460 	while (cc.depth >= 0) {
461 		reg = crom_search_key(&cc, CROM_LUN);
462 		if (reg == NULL)
463 			break;
464 		lun = reg->val & 0xffff;
465 SBP_DEBUG(0)
466 		printf("target %d lun %d found\n", target->target_id, lun);
467 END_DEBUG
468 		if (maxlun < lun)
469 			maxlun = lun;
470 		crom_next(&cc);
471 	}
472 	if (maxlun < 0)
473 		printf("%s:%d no LUN found\n",
474 		    device_get_nameunit(target->sbp->fd.dev),
475 		    target->target_id);
476 
477 	maxlun ++;
478 	if (maxlun >= SBP_NUM_LUNS)
479 		maxlun = SBP_NUM_LUNS;
480 
481 	/* Invalidiate stale devices */
482 	for (lun = 0; lun < target->num_lun; lun ++) {
483 		sdev = target->luns[lun];
484 		if (sdev == NULL)
485 			continue;
486 		sdev->flags &= ~VALID_LUN;
487 		if (lun >= maxlun) {
488 			/* lost device */
489 			sbp_cam_detach_sdev(sdev);
490 			sbp_free_sdev(sdev);
491 		}
492 	}
493 
494 	/* Reallocate */
495 	if (maxlun != target->num_lun) {
496 		newluns = (struct sbp_dev **) realloc(target->luns,
497 		    sizeof(struct sbp_dev *) * maxlun,
498 		    M_SBP, M_NOWAIT | M_ZERO);
499 
500 		if (newluns == NULL) {
501 			printf("%s: realloc failed\n", __func__);
502 			newluns = target->luns;
503 			maxlun = target->num_lun;
504 		}
505 
506 		/*
507 		 * We must zero the extended region for the case
508 		 * realloc() doesn't allocate new buffer.
509 		 */
510 		if (maxlun > target->num_lun)
511 			bzero(&newluns[target->num_lun],
512 			    sizeof(struct sbp_dev *) *
513 			    (maxlun - target->num_lun));
514 
515 		target->luns = newluns;
516 		target->num_lun = maxlun;
517 	}
518 
519 	crom_init_context(&cc, target->fwdev->csrrom);
520 	while (cc.depth >= 0) {
521 		int new = 0;
522 
523 		reg = crom_search_key(&cc, CROM_LUN);
524 		if (reg == NULL)
525 			break;
526 		lun = reg->val & 0xffff;
527 		if (lun >= SBP_NUM_LUNS) {
528 			printf("too large lun %d\n", lun);
529 			goto next;
530 		}
531 
532 		sdev = target->luns[lun];
533 		if (sdev == NULL) {
534 			sdev = malloc(sizeof(struct sbp_dev),
535 			    M_SBP, M_NOWAIT | M_ZERO);
536 			if (sdev == NULL) {
537 				printf("%s: malloc failed\n", __func__);
538 				goto next;
539 			}
540 			target->luns[lun] = sdev;
541 			sdev->lun_id = lun;
542 			sdev->target = target;
543 			STAILQ_INIT(&sdev->ocbs);
544 			CALLOUT_INIT(&sdev->login_callout);
545 			sdev->status = SBP_DEV_RESET;
546 			new = 1;
547 		}
548 		sdev->flags |= VALID_LUN;
549 		sdev->type = (reg->val & 0xff0000) >> 16;
550 
551 		if (new == 0)
552 			goto next;
553 
554 		fwdma_malloc(sbp->fd.fc,
555 			/* alignment */ sizeof(uint32_t),
556 			SBP_DMA_SIZE, &sdev->dma, BUS_DMA_NOWAIT);
557 		if (sdev->dma.v_addr == NULL) {
558 			printf("%s: dma space allocation failed\n",
559 							__func__);
560 			free(sdev, M_SBP);
561 			target->luns[lun] = NULL;
562 			goto next;
563 		}
564 		sdev->login = (struct sbp_login_res *) sdev->dma.v_addr;
565 		sdev->ocb = (struct sbp_ocb *)
566 				((char *)sdev->dma.v_addr + SBP_LOGIN_SIZE);
567 		bzero((char *)sdev->ocb,
568 			sizeof (struct sbp_ocb) * SBP_QUEUE_LEN);
569 
570 		STAILQ_INIT(&sdev->free_ocbs);
571 		for (i = 0; i < SBP_QUEUE_LEN; i++) {
572 			struct sbp_ocb *ocb;
573 			ocb = &sdev->ocb[i];
574 			ocb->bus_addr = sdev->dma.bus_addr
575 				+ SBP_LOGIN_SIZE
576 				+ sizeof(struct sbp_ocb) * i
577 				+ offsetof(struct sbp_ocb, orb[0]);
578 			if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
579 				printf("sbp_attach: cannot create dmamap\n");
580 				/* XXX */
581 				goto next;
582 			}
583 			sbp_free_ocb(sdev, ocb);
584 		}
585 next:
586 		crom_next(&cc);
587 	}
588 
589 	for (lun = 0; lun < target->num_lun; lun ++) {
590 		sdev = target->luns[lun];
591 		if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) {
592 			sbp_cam_detach_sdev(sdev);
593 			sbp_free_sdev(sdev);
594 			target->luns[lun] = NULL;
595 		}
596 	}
597 }
598 
599 static struct sbp_target *
600 sbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
601 {
602 	int i;
603 	struct sbp_target *target;
604 	struct crom_context cc;
605 	struct csrreg *reg;
606 
607 SBP_DEBUG(1)
608 	printf("sbp_alloc_target\n");
609 END_DEBUG
610 	i = sbp_new_target(sbp, fwdev);
611 	if (i < 0) {
612 		device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n");
613 		return NULL;
614 	}
615 	/* new target */
616 	target = &sbp->targets[i];
617 	target->sbp = sbp;
618 	target->fwdev = fwdev;
619 	target->target_id = i;
620 	/* XXX we may want to reload mgm port after each bus reset */
621 	/* XXX there might be multiple management agents */
622 	crom_init_context(&cc, target->fwdev->csrrom);
623 	reg = crom_search_key(&cc, CROM_MGM);
624 	if (reg == NULL || reg->val == 0) {
625 		printf("NULL management address\n");
626 		target->fwdev = NULL;
627 		return NULL;
628 	}
629 	target->mgm_hi = 0xffff;
630 	target->mgm_lo = 0xf0000000 | (reg->val << 2);
631 	target->mgm_ocb_cur = NULL;
632 SBP_DEBUG(1)
633 	printf("target:%d mgm_port: %x\n", i, target->mgm_lo);
634 END_DEBUG
635 	STAILQ_INIT(&target->xferlist);
636 	target->n_xfer = 0;
637 	STAILQ_INIT(&target->mgm_ocb_queue);
638 	CALLOUT_INIT(&target->mgm_ocb_timeout);
639 	CALLOUT_INIT(&target->scan_callout);
640 
641 	target->luns = NULL;
642 	target->num_lun = 0;
643 	return target;
644 }
645 
646 static void
647 sbp_probe_lun(struct sbp_dev *sdev)
648 {
649 	struct fw_device *fwdev;
650 	struct crom_context c, *cc = &c;
651 	struct csrreg *reg;
652 
653 	bzero(sdev->vendor, sizeof(sdev->vendor));
654 	bzero(sdev->product, sizeof(sdev->product));
655 
656 	fwdev = sdev->target->fwdev;
657 	crom_init_context(cc, fwdev->csrrom);
658 	/* get vendor string */
659 	crom_search_key(cc, CSRKEY_VENDOR);
660 	crom_next(cc);
661 	crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor));
662 	/* skip to the unit directory for SBP-2 */
663 	while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) {
664 		if (reg->val == CSRVAL_T10SBP2)
665 			break;
666 		crom_next(cc);
667 	}
668 	/* get firmware revision */
669 	reg = crom_search_key(cc, CSRKEY_FIRM_VER);
670 	if (reg != NULL)
671 		snprintf(sdev->revision, sizeof(sdev->revision),
672 						"%06x", reg->val);
673 	/* get product string */
674 	crom_search_key(cc, CSRKEY_MODEL);
675 	crom_next(cc);
676 	crom_parse_text(cc, sdev->product, sizeof(sdev->product));
677 }
678 
679 static void
680 sbp_login_callout(void *arg)
681 {
682 	struct sbp_dev *sdev = (struct sbp_dev *)arg;
683 	sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
684 }
685 
686 static void
687 sbp_login(struct sbp_dev *sdev)
688 {
689 	struct timeval delta;
690 	struct timeval t;
691 	int ticks = 0;
692 
693 	microtime(&delta);
694 	timevalsub(&delta, &sdev->target->sbp->last_busreset);
695 	t.tv_sec = login_delay / 1000;
696 	t.tv_usec = (login_delay % 1000) * 1000;
697 	timevalsub(&t, &delta);
698 	if (t.tv_sec >= 0 && t.tv_usec > 0)
699 		ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000;
700 SBP_DEBUG(0)
701 	printf("%s: sec = %jd usec = %ld ticks = %d\n", __func__,
702 	    (intmax_t)t.tv_sec, t.tv_usec, ticks);
703 END_DEBUG
704 	callout_reset(&sdev->login_callout, ticks,
705 			sbp_login_callout, (void *)(sdev));
706 }
707 
708 #define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
709 	&& crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
710 
711 static void
712 sbp_probe_target(void *arg)
713 {
714 	struct sbp_target *target = (struct sbp_target *)arg;
715 	struct sbp_softc *sbp;
716 	struct sbp_dev *sdev;
717 	struct firewire_comm *fc;
718 	int i, alive;
719 
720 	alive = SBP_FWDEV_ALIVE(target->fwdev);
721 SBP_DEBUG(1)
722 	printf("sbp_probe_target %d\n", target->target_id);
723 	if (!alive)
724 		printf("not alive\n");
725 END_DEBUG
726 
727 	sbp = target->sbp;
728 	fc = target->sbp->fd.fc;
729 	sbp_alloc_lun(target);
730 
731 	/* XXX untimeout mgm_ocb and dequeue */
732 	for (i=0; i < target->num_lun; i++) {
733 		sdev = target->luns[i];
734 		if (sdev == NULL)
735 			continue;
736 		if (alive && (sdev->status != SBP_DEV_DEAD)) {
737 			if (sdev->path != NULL) {
738 				xpt_freeze_devq(sdev->path, 1);
739 				sdev->freeze ++;
740 			}
741 			sbp_probe_lun(sdev);
742 SBP_DEBUG(0)
743 			sbp_show_sdev_info(sdev,
744 					(sdev->status == SBP_DEV_RESET));
745 END_DEBUG
746 
747 			sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
748 			switch (sdev->status) {
749 			case SBP_DEV_RESET:
750 				/* new or revived target */
751 				if (auto_login)
752 					sbp_login(sdev);
753 				break;
754 			case SBP_DEV_TOATTACH:
755 			case SBP_DEV_PROBE:
756 			case SBP_DEV_ATTACHED:
757 			case SBP_DEV_RETRY:
758 			default:
759 				sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
760 				break;
761 			}
762 		} else {
763 			switch (sdev->status) {
764 			case SBP_DEV_ATTACHED:
765 SBP_DEBUG(0)
766 				/* the device has gone */
767 				sbp_show_sdev_info(sdev, 2);
768 				printf("lost target\n");
769 END_DEBUG
770 				if (sdev->path) {
771 					xpt_freeze_devq(sdev->path, 1);
772 					sdev->freeze ++;
773 				}
774 				sdev->status = SBP_DEV_RETRY;
775 				sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
776 				break;
777 			case SBP_DEV_PROBE:
778 			case SBP_DEV_TOATTACH:
779 				sdev->status = SBP_DEV_RESET;
780 				break;
781 			case SBP_DEV_RETRY:
782 			case SBP_DEV_RESET:
783 			case SBP_DEV_DEAD:
784 				break;
785 			}
786 		}
787 	}
788 }
789 
790 static void
791 sbp_post_busreset(void *arg)
792 {
793 	struct sbp_softc *sbp;
794 
795 	sbp = (struct sbp_softc *)arg;
796 SBP_DEBUG(0)
797 	printf("sbp_post_busreset\n");
798 END_DEBUG
799 	if ((sbp->sim->flags & SIMQ_FREEZED) == 0) {
800 		xpt_freeze_simq(sbp->sim, /*count*/1);
801 		sbp->sim->flags |= SIMQ_FREEZED;
802 	}
803 	microtime(&sbp->last_busreset);
804 }
805 
806 static void
807 sbp_post_explore(void *arg)
808 {
809 	struct sbp_softc *sbp = (struct sbp_softc *)arg;
810 	struct sbp_target *target;
811 	struct fw_device *fwdev;
812 	int i, alive;
813 
814 SBP_DEBUG(0)
815 	printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
816 END_DEBUG
817 	if (sbp_cold > 0)
818 		sbp_cold --;
819 
820 #if 0
821 	/*
822 	 * XXX don't let CAM the bus rest.
823 	 * CAM tries to do something with freezed (DEV_RETRY) devices.
824 	 */
825 	xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
826 #endif
827 
828 	/* Garbage Collection */
829 	for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
830 		target = &sbp->targets[i];
831 		STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
832 			if (target->fwdev == NULL || target->fwdev == fwdev)
833 				break;
834 		if (fwdev == NULL) {
835 			/* device has removed in lower driver */
836 			sbp_cam_detach_target(target);
837 			sbp_free_target(target);
838 		}
839 	}
840 	/* traverse device list */
841 	STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
842 SBP_DEBUG(0)
843 		printf("sbp_post_explore: EUI:%08x%08x ",
844 				fwdev->eui.hi, fwdev->eui.lo);
845 		if (fwdev->status != FWDEVATTACHED)
846 			printf("not attached, state=%d.\n", fwdev->status);
847 		else
848 			printf("attached\n");
849 END_DEBUG
850 		alive = SBP_FWDEV_ALIVE(fwdev);
851 		for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
852 			target = &sbp->targets[i];
853 			if(target->fwdev == fwdev ) {
854 				/* known target */
855 				break;
856 			}
857 		}
858 		if(i == SBP_NUM_TARGETS){
859 			if (alive) {
860 				/* new target */
861 				target = sbp_alloc_target(sbp, fwdev);
862 				if (target == NULL)
863 					continue;
864 			} else {
865 				continue;
866 			}
867 		}
868 		sbp_probe_target((void *)target);
869 		if (target->num_lun == 0)
870 			sbp_free_target(target);
871 	}
872 	xpt_release_simq(sbp->sim, /*run queue*/TRUE);
873 	sbp->sim->flags &= ~SIMQ_FREEZED;
874 }
875 
876 #if NEED_RESPONSE
877 static void
878 sbp_loginres_callback(struct fw_xfer *xfer){
879 	int s;
880 	struct sbp_dev *sdev;
881 	sdev = (struct sbp_dev *)xfer->sc;
882 SBP_DEBUG(1)
883 	sbp_show_sdev_info(sdev, 2);
884 	printf("sbp_loginres_callback\n");
885 END_DEBUG
886 	/* recycle */
887 	s = splfw();
888 	STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link);
889 	splx(s);
890 	return;
891 }
892 #endif
893 
894 static __inline void
895 sbp_xfer_free(struct fw_xfer *xfer)
896 {
897 	struct sbp_dev *sdev;
898 	int s;
899 
900 	sdev = (struct sbp_dev *)xfer->sc;
901 	fw_xfer_unload(xfer);
902 	s = splfw();
903 	STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
904 	splx(s);
905 }
906 
907 static void
908 sbp_reset_start_callback(struct fw_xfer *xfer)
909 {
910 	struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
911 	struct sbp_target *target = sdev->target;
912 	int i;
913 
914 	if (xfer->resp != 0) {
915 		sbp_show_sdev_info(sdev, 2);
916 		printf("sbp_reset_start failed: resp=%d\n", xfer->resp);
917 	}
918 
919 	for (i = 0; i < target->num_lun; i++) {
920 		tsdev = target->luns[i];
921 		if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN)
922 			sbp_login(tsdev);
923 	}
924 }
925 
926 static void
927 sbp_reset_start(struct sbp_dev *sdev)
928 {
929 	struct fw_xfer *xfer;
930 	struct fw_pkt *fp;
931 
932 SBP_DEBUG(0)
933 	sbp_show_sdev_info(sdev, 2);
934 	printf("sbp_reset_start\n");
935 END_DEBUG
936 
937 	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
938 	xfer->act.hand = sbp_reset_start_callback;
939 	fp = &xfer->send.hdr;
940 	fp->mode.wreqq.dest_hi = 0xffff;
941 	fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
942 	fp->mode.wreqq.data = htonl(0xf);
943 	fw_asyreq(xfer->fc, -1, xfer);
944 }
945 
946 static void
947 sbp_mgm_callback(struct fw_xfer *xfer)
948 {
949 	struct sbp_dev *sdev;
950 	int resp;
951 
952 	sdev = (struct sbp_dev *)xfer->sc;
953 
954 SBP_DEBUG(1)
955 	sbp_show_sdev_info(sdev, 2);
956 	printf("sbp_mgm_callback\n");
957 END_DEBUG
958 	resp = xfer->resp;
959 	sbp_xfer_free(xfer);
960 #if 0
961 	if (resp != 0) {
962 		sbp_show_sdev_info(sdev, 2);
963 		printf("management ORB failed(%d) ... RESET_START\n", resp);
964 		sbp_reset_start(sdev);
965 	}
966 #endif
967 	return;
968 }
969 
970 static struct sbp_dev *
971 sbp_next_dev(struct sbp_target *target, int lun)
972 {
973 	struct sbp_dev **sdevp;
974 	int i;
975 
976 	for (i = lun, sdevp = &target->luns[lun]; i < target->num_lun;
977 	    i++, sdevp++)
978 		if (*sdevp != NULL && (*sdevp)->status == SBP_DEV_PROBE)
979 			return(*sdevp);
980 	return(NULL);
981 }
982 
983 #define SCAN_PRI 1
984 static void
985 sbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb)
986 {
987 	struct sbp_target *target;
988 	struct sbp_dev *sdev;
989 
990 	sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
991 	target = sdev->target;
992 SBP_DEBUG(0)
993 	sbp_show_sdev_info(sdev, 2);
994 	printf("sbp_cam_scan_lun\n");
995 END_DEBUG
996 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
997 		sdev->status = SBP_DEV_ATTACHED;
998 	} else {
999 		sbp_show_sdev_info(sdev, 2);
1000 		printf("scan failed\n");
1001 	}
1002 	sdev = sbp_next_dev(target, sdev->lun_id + 1);
1003 	if (sdev == NULL) {
1004 		free(ccb, M_SBP);
1005 		return;
1006 	}
1007 	/* reuse ccb */
1008 	xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
1009 	ccb->ccb_h.ccb_sdev_ptr = sdev;
1010 	xpt_action(ccb);
1011 	xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1012 	sdev->freeze = 1;
1013 }
1014 
1015 static void
1016 sbp_cam_scan_target(void *arg)
1017 {
1018 	struct sbp_target *target = (struct sbp_target *)arg;
1019 	struct sbp_dev *sdev;
1020 	union ccb *ccb;
1021 
1022 	sdev = sbp_next_dev(target, 0);
1023 	if (sdev == NULL) {
1024 		printf("sbp_cam_scan_target: nothing to do for target%d\n",
1025 							target->target_id);
1026 		return;
1027 	}
1028 SBP_DEBUG(0)
1029 	sbp_show_sdev_info(sdev, 2);
1030 	printf("sbp_cam_scan_target\n");
1031 END_DEBUG
1032 	ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO);
1033 	if (ccb == NULL) {
1034 		printf("sbp_cam_scan_target: malloc failed\n");
1035 		return;
1036 	}
1037 	xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
1038 	ccb->ccb_h.func_code = XPT_SCAN_LUN;
1039 	ccb->ccb_h.cbfcnp = sbp_cam_scan_lun;
1040 	ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1041 	ccb->crcn.flags = CAM_FLAG_NONE;
1042 	ccb->ccb_h.ccb_sdev_ptr = sdev;
1043 
1044 	/* The scan is in progress now. */
1045 	xpt_action(ccb);
1046 	xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1047 	sdev->freeze = 1;
1048 }
1049 
1050 static __inline void
1051 sbp_scan_dev(struct sbp_dev *sdev)
1052 {
1053 	sdev->status = SBP_DEV_PROBE;
1054 	callout_reset(&sdev->target->scan_callout, scan_delay * hz / 1000,
1055 			sbp_cam_scan_target, (void *)sdev->target);
1056 }
1057 
1058 static void
1059 sbp_do_attach(struct fw_xfer *xfer)
1060 {
1061 	struct sbp_dev *sdev;
1062 	struct sbp_target *target;
1063 	struct sbp_softc *sbp;
1064 
1065 	sdev = (struct sbp_dev *)xfer->sc;
1066 	target = sdev->target;
1067 	sbp = target->sbp;
1068 SBP_DEBUG(0)
1069 	sbp_show_sdev_info(sdev, 2);
1070 	printf("sbp_do_attach\n");
1071 END_DEBUG
1072 	sbp_xfer_free(xfer);
1073 
1074 	if (sdev->path == NULL)
1075 		xpt_create_path(&sdev->path, xpt_periph,
1076 			cam_sim_path(target->sbp->sim),
1077 			target->target_id, sdev->lun_id);
1078 
1079 	/*
1080 	 * Let CAM scan the bus if we are in the boot process.
1081 	 * XXX xpt_scan_bus cannot detect LUN larger than 0
1082 	 * if LUN 0 doesn't exists.
1083 	 */
1084 	if (sbp_cold > 0) {
1085 		sdev->status = SBP_DEV_ATTACHED;
1086 		return;
1087 	}
1088 
1089 	sbp_scan_dev(sdev);
1090 	return;
1091 }
1092 
1093 static void
1094 sbp_agent_reset_callback(struct fw_xfer *xfer)
1095 {
1096 	struct sbp_dev *sdev;
1097 
1098 	sdev = (struct sbp_dev *)xfer->sc;
1099 SBP_DEBUG(1)
1100 	sbp_show_sdev_info(sdev, 2);
1101 	printf("%s\n", __func__);
1102 END_DEBUG
1103 	if (xfer->resp != 0) {
1104 		sbp_show_sdev_info(sdev, 2);
1105 		printf("%s: resp=%d\n", __func__, xfer->resp);
1106 	}
1107 
1108 	sbp_xfer_free(xfer);
1109 	if (sdev->path) {
1110 		xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1111 		sdev->freeze = 0;
1112 	}
1113 }
1114 
1115 static void
1116 sbp_agent_reset(struct sbp_dev *sdev)
1117 {
1118 	struct fw_xfer *xfer;
1119 	struct fw_pkt *fp;
1120 
1121 SBP_DEBUG(0)
1122 	sbp_show_sdev_info(sdev, 2);
1123 	printf("sbp_agent_reset\n");
1124 END_DEBUG
1125 	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1126 	if (xfer == NULL)
1127 		return;
1128 	if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE)
1129 		xfer->act.hand = sbp_agent_reset_callback;
1130 	else
1131 		xfer->act.hand = sbp_do_attach;
1132 	fp = &xfer->send.hdr;
1133 	fp->mode.wreqq.data = htonl(0xf);
1134 	fw_asyreq(xfer->fc, -1, xfer);
1135 	sbp_abort_all_ocbs(sdev, CAM_BDR_SENT);
1136 }
1137 
1138 static void
1139 sbp_busy_timeout_callback(struct fw_xfer *xfer)
1140 {
1141 	struct sbp_dev *sdev;
1142 
1143 	sdev = (struct sbp_dev *)xfer->sc;
1144 SBP_DEBUG(1)
1145 	sbp_show_sdev_info(sdev, 2);
1146 	printf("sbp_busy_timeout_callback\n");
1147 END_DEBUG
1148 	sbp_xfer_free(xfer);
1149 	sbp_agent_reset(sdev);
1150 }
1151 
1152 static void
1153 sbp_busy_timeout(struct sbp_dev *sdev)
1154 {
1155 	struct fw_pkt *fp;
1156 	struct fw_xfer *xfer;
1157 SBP_DEBUG(0)
1158 	sbp_show_sdev_info(sdev, 2);
1159 	printf("sbp_busy_timeout\n");
1160 END_DEBUG
1161 	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1162 
1163 	xfer->act.hand = sbp_busy_timeout_callback;
1164 	fp = &xfer->send.hdr;
1165 	fp->mode.wreqq.dest_hi = 0xffff;
1166 	fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1167 	fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1168 	fw_asyreq(xfer->fc, -1, xfer);
1169 }
1170 
1171 static void
1172 sbp_orb_pointer_callback(struct fw_xfer *xfer)
1173 {
1174 	struct sbp_dev *sdev;
1175 	sdev = (struct sbp_dev *)xfer->sc;
1176 
1177 SBP_DEBUG(1)
1178 	sbp_show_sdev_info(sdev, 2);
1179 	printf("%s\n", __func__);
1180 END_DEBUG
1181 	if (xfer->resp != 0) {
1182 		/* XXX */
1183 		printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1184 	}
1185 	sbp_xfer_free(xfer);
1186 	sdev->flags &= ~ORB_POINTER_ACTIVE;
1187 
1188 	if ((sdev->flags & ORB_POINTER_NEED) != 0) {
1189 		struct sbp_ocb *ocb;
1190 
1191 		sdev->flags &= ~ORB_POINTER_NEED;
1192 		ocb = STAILQ_FIRST(&sdev->ocbs);
1193 		if (ocb != NULL)
1194 			sbp_orb_pointer(sdev, ocb);
1195 	}
1196 	return;
1197 }
1198 
1199 static void
1200 sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1201 {
1202 	struct fw_xfer *xfer;
1203 	struct fw_pkt *fp;
1204 SBP_DEBUG(1)
1205 	sbp_show_sdev_info(sdev, 2);
1206 	printf("%s: 0x%08x\n", __func__, (uint32_t)ocb->bus_addr);
1207 END_DEBUG
1208 
1209 	if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) {
1210 SBP_DEBUG(0)
1211 		printf("%s: orb pointer active\n", __func__);
1212 END_DEBUG
1213 		sdev->flags |= ORB_POINTER_NEED;
1214 		return;
1215 	}
1216 
1217 	sdev->flags |= ORB_POINTER_ACTIVE;
1218 	xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1219 	if (xfer == NULL)
1220 		return;
1221 	xfer->act.hand = sbp_orb_pointer_callback;
1222 
1223 	fp = &xfer->send.hdr;
1224 	fp->mode.wreqb.len = 8;
1225 	fp->mode.wreqb.extcode = 0;
1226 	xfer->send.payload[0] =
1227 		htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1228 	xfer->send.payload[1] = htonl((uint32_t)ocb->bus_addr);
1229 
1230 	if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1231 			sbp_xfer_free(xfer);
1232 			ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1233 			xpt_done(ocb->ccb);
1234 	}
1235 }
1236 
1237 static void
1238 sbp_doorbell_callback(struct fw_xfer *xfer)
1239 {
1240 	struct sbp_dev *sdev;
1241 	sdev = (struct sbp_dev *)xfer->sc;
1242 
1243 SBP_DEBUG(1)
1244 	sbp_show_sdev_info(sdev, 2);
1245 	printf("sbp_doorbell_callback\n");
1246 END_DEBUG
1247 	if (xfer->resp != 0) {
1248 		/* XXX */
1249 		printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1250 	}
1251 	sbp_xfer_free(xfer);
1252 	sdev->flags &= ~ORB_DOORBELL_ACTIVE;
1253 	if ((sdev->flags & ORB_DOORBELL_NEED) != 0) {
1254 		sdev->flags &= ~ORB_DOORBELL_NEED;
1255 		sbp_doorbell(sdev);
1256 	}
1257 	return;
1258 }
1259 
1260 static void
1261 sbp_doorbell(struct sbp_dev *sdev)
1262 {
1263 	struct fw_xfer *xfer;
1264 	struct fw_pkt *fp;
1265 SBP_DEBUG(1)
1266 	sbp_show_sdev_info(sdev, 2);
1267 	printf("sbp_doorbell\n");
1268 END_DEBUG
1269 
1270 	if ((sdev->flags & ORB_DOORBELL_ACTIVE) != 0) {
1271 		sdev->flags |= ORB_DOORBELL_NEED;
1272 		return;
1273 	}
1274 	sdev->flags |= ORB_DOORBELL_ACTIVE;
1275 	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1276 	if (xfer == NULL)
1277 		return;
1278 	xfer->act.hand = sbp_doorbell_callback;
1279 	fp = &xfer->send.hdr;
1280 	fp->mode.wreqq.data = htonl(0xf);
1281 	fw_asyreq(xfer->fc, -1, xfer);
1282 }
1283 
1284 static struct fw_xfer *
1285 sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1286 {
1287 	struct fw_xfer *xfer;
1288 	struct fw_pkt *fp;
1289 	struct sbp_target *target;
1290 	int s, new = 0;
1291 
1292 	target = sdev->target;
1293 	s = splfw();
1294 	xfer = STAILQ_FIRST(&target->xferlist);
1295 	if (xfer == NULL) {
1296 		if (target->n_xfer > 5 /* XXX */) {
1297 			printf("sbp: no more xfer for this target\n");
1298 			splx(s);
1299 			return(NULL);
1300 		}
1301 		xfer = fw_xfer_alloc_buf(M_SBP, 8, 0);
1302 		if(xfer == NULL){
1303 			printf("sbp: fw_xfer_alloc_buf failed\n");
1304 			splx(s);
1305 			return NULL;
1306 		}
1307 		target->n_xfer ++;
1308 		if (debug)
1309 			printf("sbp: alloc %d xfer\n", target->n_xfer);
1310 		new = 1;
1311 	} else {
1312 		STAILQ_REMOVE_HEAD(&target->xferlist, link);
1313 	}
1314 	splx(s);
1315 
1316 	microtime(&xfer->tv);
1317 
1318 	if (new) {
1319 		xfer->recv.pay_len = 0;
1320 		xfer->send.spd = min(sdev->target->fwdev->speed, max_speed);
1321 		xfer->fc = sdev->target->sbp->fd.fc;
1322 		xfer->retry_req = fw_asybusy;
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->act.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->act.hand = sbp_loginres_callback;
1880 	xfer->retry_req = fw_asybusy;
1881 
1882 	sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1883 	sfp->mode.wres.tcode = FWTCODE_WRES;
1884 	sfp->mode.wres.rtcode = 0;
1885 	sfp->mode.wres.pri = 0;
1886 
1887 	fw_asyreq(xfer->fc, -1, xfer);
1888 #else
1889 	/* recycle */
1890 	STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1891 #endif
1892 
1893 	return;
1894 
1895 }
1896 
1897 static void
1898 sbp_recv(struct fw_xfer *xfer)
1899 {
1900 	int s;
1901 
1902 	s = splcam();
1903 	sbp_recv1(xfer);
1904 	splx(s);
1905 }
1906 /*
1907  * sbp_attach()
1908  */
1909 static int
1910 sbp_attach(device_t dev)
1911 {
1912 	struct sbp_softc *sbp;
1913 	struct cam_devq *devq;
1914 	struct fw_xfer *xfer;
1915 	int i, s, error;
1916 
1917 SBP_DEBUG(0)
1918 	printf("sbp_attach (cold=%d)\n", cold);
1919 END_DEBUG
1920 
1921 	if (cold)
1922 		sbp_cold ++;
1923 	sbp = ((struct sbp_softc *)device_get_softc(dev));
1924 	bzero(sbp, sizeof(struct sbp_softc));
1925 	sbp->fd.dev = dev;
1926 	sbp->fd.fc = device_get_ivars(dev);
1927 
1928 	if (max_speed < 0)
1929 		max_speed = sbp->fd.fc->speed;
1930 
1931 	error = bus_dma_tag_create(/*parent*/sbp->fd.fc->dmat,
1932 				/* XXX shoud be 4 for sane backend? */
1933 				/*alignment*/1,
1934 				/*boundary*/0,
1935 				/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1936 				/*highaddr*/BUS_SPACE_MAXADDR,
1937 				/*filter*/NULL, /*filterarg*/NULL,
1938 				/*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1939 				/*maxsegsz*/SBP_SEG_MAX,
1940 				/*flags*/BUS_DMA_ALLOCNOW,
1941 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
1942 				/*lockfunc*/busdma_lock_mutex,
1943 				/*lockarg*/&Giant,
1944 #endif
1945 				&sbp->dmat);
1946 	if (error != 0) {
1947 		printf("sbp_attach: Could not allocate DMA tag "
1948 			"- error %d\n", error);
1949 			return (ENOMEM);
1950 	}
1951 
1952 	devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1953 	if (devq == NULL)
1954 		return (ENXIO);
1955 
1956 	for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1957 		sbp->targets[i].fwdev = NULL;
1958 		sbp->targets[i].luns = NULL;
1959 	}
1960 
1961 	sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1962 				 device_get_unit(dev),
1963 				 /*untagged*/ 1,
1964 				 /*tagged*/ SBP_QUEUE_LEN - 1,
1965 				 devq);
1966 
1967 	if (sbp->sim == NULL) {
1968 		cam_simq_free(devq);
1969 		return (ENXIO);
1970 	}
1971 
1972 
1973 	if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS)
1974 		goto fail;
1975 
1976 	if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim),
1977 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1978 		xpt_bus_deregister(cam_sim_path(sbp->sim));
1979 		goto fail;
1980 	}
1981 
1982 	/* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1983 	sbp->fwb.start = ((u_int64_t)SBP_BIND_HI << 32) | SBP_DEV2ADDR(0, 0);
1984 	sbp->fwb.end = sbp->fwb.start + 0xffff;
1985 	sbp->fwb.act_type = FWACT_XFER;
1986 	/* pre-allocate xfer */
1987 	STAILQ_INIT(&sbp->fwb.xferlist);
1988 	for (i = 0; i < SBP_NUM_OCB/2; i ++) {
1989 		xfer = fw_xfer_alloc_buf(M_SBP,
1990 			/* send */0,
1991 			/* recv */SBP_RECV_LEN);
1992 		xfer->act.hand = sbp_recv;
1993 #if NEED_RESPONSE
1994 		xfer->fc = sbp->fd.fc;
1995 #endif
1996 		xfer->sc = (caddr_t)sbp;
1997 		STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1998 	}
1999 	fw_bindadd(sbp->fd.fc, &sbp->fwb);
2000 
2001 	sbp->fd.post_busreset = sbp_post_busreset;
2002 	sbp->fd.post_explore = sbp_post_explore;
2003 
2004 	if (sbp->fd.fc->status != -1) {
2005 		s = splfw();
2006 		sbp_post_busreset((void *)sbp);
2007 		sbp_post_explore((void *)sbp);
2008 		splx(s);
2009 	}
2010 	xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
2011 
2012 	return (0);
2013 fail:
2014 	cam_sim_free(sbp->sim, /*free_devq*/TRUE);
2015 	return (ENXIO);
2016 }
2017 
2018 static int
2019 sbp_logout_all(struct sbp_softc *sbp)
2020 {
2021 	struct sbp_target *target;
2022 	struct sbp_dev *sdev;
2023 	int i, j;
2024 
2025 SBP_DEBUG(0)
2026 	printf("sbp_logout_all\n");
2027 END_DEBUG
2028 	for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
2029 		target = &sbp->targets[i];
2030 		if (target->luns == NULL)
2031 			continue;
2032 		for (j = 0; j < target->num_lun; j++) {
2033 			sdev = target->luns[j];
2034 			if (sdev == NULL)
2035 				continue;
2036 			callout_stop(&sdev->login_callout);
2037 			if (sdev->status >= SBP_DEV_TOATTACH &&
2038 					sdev->status <= SBP_DEV_ATTACHED)
2039 				sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
2040 		}
2041 	}
2042 
2043 	return 0;
2044 }
2045 
2046 static int
2047 sbp_shutdown(device_t dev)
2048 {
2049 	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2050 
2051 	sbp_logout_all(sbp);
2052 	return (0);
2053 }
2054 
2055 static void
2056 sbp_free_sdev(struct sbp_dev *sdev)
2057 {
2058 	int i;
2059 
2060 	if (sdev == NULL)
2061 		return;
2062 	for (i = 0; i < SBP_QUEUE_LEN; i++)
2063 		bus_dmamap_destroy(sdev->target->sbp->dmat,
2064 		    sdev->ocb[i].dmamap);
2065 	fwdma_free(sdev->target->sbp->fd.fc, &sdev->dma);
2066 	free(sdev, M_SBP);
2067 }
2068 
2069 static void
2070 sbp_free_target(struct sbp_target *target)
2071 {
2072 	struct sbp_softc *sbp;
2073 	struct fw_xfer *xfer, *next;
2074 	int i;
2075 
2076 	if (target->luns == NULL)
2077 		return;
2078 	callout_stop(&target->mgm_ocb_timeout);
2079 	sbp = target->sbp;
2080 	for (i = 0; i < target->num_lun; i++)
2081 		sbp_free_sdev(target->luns[i]);
2082 
2083 	for (xfer = STAILQ_FIRST(&target->xferlist);
2084 			xfer != NULL; xfer = next) {
2085 		next = STAILQ_NEXT(xfer, link);
2086 		fw_xfer_free_buf(xfer);
2087 	}
2088 	STAILQ_INIT(&target->xferlist);
2089 	free(target->luns, M_SBP);
2090 	target->num_lun = 0;;
2091 	target->luns = NULL;
2092 	target->fwdev = NULL;
2093 }
2094 
2095 static int
2096 sbp_detach(device_t dev)
2097 {
2098 	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2099 	struct firewire_comm *fc = sbp->fd.fc;
2100 	struct fw_xfer *xfer, *next;
2101 	int i;
2102 
2103 SBP_DEBUG(0)
2104 	printf("sbp_detach\n");
2105 END_DEBUG
2106 
2107 	for (i = 0; i < SBP_NUM_TARGETS; i ++)
2108 		sbp_cam_detach_target(&sbp->targets[i]);
2109 	xpt_async(AC_LOST_DEVICE, sbp->path, NULL);
2110 	xpt_free_path(sbp->path);
2111 	xpt_bus_deregister(cam_sim_path(sbp->sim));
2112 	cam_sim_free(sbp->sim, /*free_devq*/ TRUE),
2113 
2114 	sbp_logout_all(sbp);
2115 
2116 	/* XXX wait for logout completion */
2117 	pause("sbpdtc", hz/2);
2118 
2119 	for (i = 0 ; i < SBP_NUM_TARGETS ; i ++)
2120 		sbp_free_target(&sbp->targets[i]);
2121 
2122 	for (xfer = STAILQ_FIRST(&sbp->fwb.xferlist);
2123 				xfer != NULL; xfer = next) {
2124 		next = STAILQ_NEXT(xfer, link);
2125 		fw_xfer_free_buf(xfer);
2126 	}
2127 	STAILQ_INIT(&sbp->fwb.xferlist);
2128 	fw_bindremove(fc, &sbp->fwb);
2129 
2130 	bus_dma_tag_destroy(sbp->dmat);
2131 
2132 	return (0);
2133 }
2134 
2135 static void
2136 sbp_cam_detach_sdev(struct sbp_dev *sdev)
2137 {
2138 	if (sdev == NULL)
2139 		return;
2140 	if (sdev->status == SBP_DEV_DEAD)
2141 		return;
2142 	if (sdev->status == SBP_DEV_RESET)
2143 		return;
2144 	if (sdev->path) {
2145 		xpt_release_devq(sdev->path,
2146 				 sdev->freeze, TRUE);
2147 		sdev->freeze = 0;
2148 		xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
2149 		xpt_free_path(sdev->path);
2150 		sdev->path = NULL;
2151 	}
2152 	sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
2153 }
2154 
2155 static void
2156 sbp_cam_detach_target(struct sbp_target *target)
2157 {
2158 	int i;
2159 
2160 	if (target->luns != NULL) {
2161 SBP_DEBUG(0)
2162 		printf("sbp_detach_target %d\n", target->target_id);
2163 END_DEBUG
2164 		callout_stop(&target->scan_callout);
2165 		for (i = 0; i < target->num_lun; i++)
2166 			sbp_cam_detach_sdev(target->luns[i]);
2167 	}
2168 }
2169 
2170 static void
2171 sbp_target_reset(struct sbp_dev *sdev, int method)
2172 {
2173 	int i;
2174 	struct sbp_target *target = sdev->target;
2175 	struct sbp_dev *tsdev;
2176 
2177 	for (i = 0; i < target->num_lun; i++) {
2178 		tsdev = target->luns[i];
2179 		if (tsdev == NULL)
2180 			continue;
2181 		if (tsdev->status == SBP_DEV_DEAD)
2182 			continue;
2183 		if (tsdev->status == SBP_DEV_RESET)
2184 			continue;
2185 		xpt_freeze_devq(tsdev->path, 1);
2186 		tsdev->freeze ++;
2187 		sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT);
2188 		if (method == 2)
2189 			tsdev->status = SBP_DEV_LOGIN;
2190 	}
2191 	switch(method) {
2192 	case 1:
2193 		printf("target reset\n");
2194 		sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2195 		break;
2196 	case 2:
2197 		printf("reset start\n");
2198 		sbp_reset_start(sdev);
2199 		break;
2200 	}
2201 
2202 }
2203 
2204 static void
2205 sbp_mgm_timeout(void *arg)
2206 {
2207 	struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2208 	struct sbp_dev *sdev = ocb->sdev;
2209 	struct sbp_target *target = sdev->target;
2210 
2211 	sbp_show_sdev_info(sdev, 2);
2212 	printf("request timeout(mgm orb:0x%08x) ... ",
2213 	    (uint32_t)ocb->bus_addr);
2214 	target->mgm_ocb_cur = NULL;
2215 	sbp_free_ocb(sdev, ocb);
2216 #if 0
2217 	/* XXX */
2218 	printf("run next request\n");
2219 	sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2220 #endif
2221 #if 1
2222 	printf("reset start\n");
2223 	sbp_reset_start(sdev);
2224 #endif
2225 }
2226 
2227 static void
2228 sbp_timeout(void *arg)
2229 {
2230 	struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2231 	struct sbp_dev *sdev = ocb->sdev;
2232 
2233 	sbp_show_sdev_info(sdev, 2);
2234 	printf("request timeout(cmd orb:0x%08x) ... ",
2235 	    (uint32_t)ocb->bus_addr);
2236 
2237 	sdev->timeout ++;
2238 	switch(sdev->timeout) {
2239 	case 1:
2240 		printf("agent reset\n");
2241 		xpt_freeze_devq(sdev->path, 1);
2242 		sdev->freeze ++;
2243 		sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2244 		sbp_agent_reset(sdev);
2245 		break;
2246 	case 2:
2247 	case 3:
2248 		sbp_target_reset(sdev, sdev->timeout - 1);
2249 		break;
2250 #if 0
2251 	default:
2252 		/* XXX give up */
2253 		sbp_cam_detach_target(target);
2254 		if (target->luns != NULL)
2255 			free(target->luns, M_SBP);
2256 		target->num_lun = 0;;
2257 		target->luns = NULL;
2258 		target->fwdev = NULL;
2259 #endif
2260 	}
2261 }
2262 
2263 static void
2264 sbp_action1(struct cam_sim *sim, union ccb *ccb)
2265 {
2266 
2267 	struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2268 	struct sbp_target *target = NULL;
2269 	struct sbp_dev *sdev = NULL;
2270 
2271 	/* target:lun -> sdev mapping */
2272 	if (sbp != NULL
2273 			&& ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2274 			&& ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2275 		target = &sbp->targets[ccb->ccb_h.target_id];
2276 		if (target->fwdev != NULL
2277 				&& ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2278 				&& ccb->ccb_h.target_lun < target->num_lun) {
2279 			sdev = target->luns[ccb->ccb_h.target_lun];
2280 			if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED &&
2281 				sdev->status != SBP_DEV_PROBE)
2282 				sdev = NULL;
2283 		}
2284 	}
2285 
2286 SBP_DEBUG(1)
2287 	if (sdev == NULL)
2288 		printf("invalid target %d lun %d\n",
2289 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2290 END_DEBUG
2291 
2292 	switch (ccb->ccb_h.func_code) {
2293 	case XPT_SCSI_IO:
2294 	case XPT_RESET_DEV:
2295 	case XPT_GET_TRAN_SETTINGS:
2296 	case XPT_SET_TRAN_SETTINGS:
2297 	case XPT_CALC_GEOMETRY:
2298 		if (sdev == NULL) {
2299 SBP_DEBUG(1)
2300 			printf("%s:%d:%d:func_code 0x%04x: "
2301 				"Invalid target (target needed)\n",
2302 				device_get_nameunit(sbp->fd.dev),
2303 				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2304 				ccb->ccb_h.func_code);
2305 END_DEBUG
2306 
2307 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2308 			xpt_done(ccb);
2309 			return;
2310 		}
2311 		break;
2312 	case XPT_PATH_INQ:
2313 	case XPT_NOOP:
2314 		/* The opcodes sometimes aimed at a target (sc is valid),
2315 		 * sometimes aimed at the SIM (sc is invalid and target is
2316 		 * CAM_TARGET_WILDCARD)
2317 		 */
2318 		if (sbp == NULL &&
2319 			ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2320 SBP_DEBUG(0)
2321 			printf("%s:%d:%d func_code 0x%04x: "
2322 				"Invalid target (no wildcard)\n",
2323 				device_get_nameunit(sbp->fd.dev),
2324 				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2325 				ccb->ccb_h.func_code);
2326 END_DEBUG
2327 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2328 			xpt_done(ccb);
2329 			return;
2330 		}
2331 		break;
2332 	default:
2333 		/* XXX Hm, we should check the input parameters */
2334 		break;
2335 	}
2336 
2337 	switch (ccb->ccb_h.func_code) {
2338 	case XPT_SCSI_IO:
2339 	{
2340 		struct ccb_scsiio *csio;
2341 		struct sbp_ocb *ocb;
2342 		int speed;
2343 		void *cdb;
2344 
2345 		csio = &ccb->csio;
2346 
2347 SBP_DEBUG(2)
2348 		printf("%s:%d:%d XPT_SCSI_IO: "
2349 			"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2350 			", flags: 0x%02x, "
2351 			"%db cmd/%db data/%db sense\n",
2352 			device_get_nameunit(sbp->fd.dev),
2353 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2354 			csio->cdb_io.cdb_bytes[0],
2355 			csio->cdb_io.cdb_bytes[1],
2356 			csio->cdb_io.cdb_bytes[2],
2357 			csio->cdb_io.cdb_bytes[3],
2358 			csio->cdb_io.cdb_bytes[4],
2359 			csio->cdb_io.cdb_bytes[5],
2360 			csio->cdb_io.cdb_bytes[6],
2361 			csio->cdb_io.cdb_bytes[7],
2362 			csio->cdb_io.cdb_bytes[8],
2363 			csio->cdb_io.cdb_bytes[9],
2364 			ccb->ccb_h.flags & CAM_DIR_MASK,
2365 			csio->cdb_len, csio->dxfer_len,
2366 			csio->sense_len);
2367 END_DEBUG
2368 		if(sdev == NULL){
2369 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2370 			xpt_done(ccb);
2371 			return;
2372 		}
2373 #if 0
2374 		/* if we are in probe stage, pass only probe commands */
2375 		if (sdev->status == SBP_DEV_PROBE) {
2376 			char *name;
2377 			name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2378 			printf("probe stage, periph name: %s\n", name);
2379 			if (strcmp(name, "probe") != 0) {
2380 				ccb->ccb_h.status = CAM_REQUEUE_REQ;
2381 				xpt_done(ccb);
2382 				return;
2383 			}
2384 		}
2385 #endif
2386 		if ((ocb = sbp_get_ocb(sdev)) == NULL) {
2387 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
2388 			if (sdev->freeze == 0) {
2389 				xpt_freeze_devq(sdev->path, 1);
2390 				sdev->freeze ++;
2391 			}
2392 			xpt_done(ccb);
2393 			return;
2394 		}
2395 
2396 		ocb->flags = OCB_ACT_CMD;
2397 		ocb->sdev = sdev;
2398 		ocb->ccb = ccb;
2399 		ccb->ccb_h.ccb_sdev_ptr = sdev;
2400 		ocb->orb[0] = htonl(1 << 31);
2401 		ocb->orb[1] = 0;
2402 		ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2403 		ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2404 		speed = min(target->fwdev->speed, max_speed);
2405 		ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2406 						| ORB_CMD_MAXP(speed + 7));
2407 		if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
2408 			ocb->orb[4] |= htonl(ORB_CMD_IN);
2409 		}
2410 
2411 		if (csio->ccb_h.flags & CAM_SCATTER_VALID)
2412 			printf("sbp: CAM_SCATTER_VALID\n");
2413 		if (csio->ccb_h.flags & CAM_DATA_PHYS)
2414 			printf("sbp: CAM_DATA_PHYS\n");
2415 
2416 		if (csio->ccb_h.flags & CAM_CDB_POINTER)
2417 			cdb = (void *)csio->cdb_io.cdb_ptr;
2418 		else
2419 			cdb = (void *)&csio->cdb_io.cdb_bytes;
2420 		bcopy(cdb, (void *)&ocb->orb[5], csio->cdb_len);
2421 /*
2422 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2423 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2424 */
2425 		if (ccb->csio.dxfer_len > 0) {
2426 			int s, error;
2427 
2428 			s = splsoftvm();
2429 			error = bus_dmamap_load(/*dma tag*/sbp->dmat,
2430 					/*dma map*/ocb->dmamap,
2431 					ccb->csio.data_ptr,
2432 					ccb->csio.dxfer_len,
2433 					sbp_execute_ocb,
2434 					ocb,
2435 					/*flags*/0);
2436 			splx(s);
2437 			if (error)
2438 				printf("sbp: bus_dmamap_load error %d\n", error);
2439 		} else
2440 			sbp_execute_ocb(ocb, NULL, 0, 0);
2441 		break;
2442 	}
2443 	case XPT_CALC_GEOMETRY:
2444 	{
2445 		struct ccb_calc_geometry *ccg;
2446 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2447 		uint32_t size_mb;
2448 		uint32_t secs_per_cylinder;
2449 		int extended = 1;
2450 #endif
2451 
2452 		ccg = &ccb->ccg;
2453 		if (ccg->block_size == 0) {
2454 			printf("sbp_action1: block_size is 0.\n");
2455 			ccb->ccb_h.status = CAM_REQ_INVALID;
2456 			xpt_done(ccb);
2457 			break;
2458 		}
2459 SBP_DEBUG(1)
2460 		printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2461 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2462 			"Volume size = %d\n",
2463 #else
2464 			"Volume size = %jd\n",
2465 #endif
2466 			device_get_nameunit(sbp->fd.dev),
2467 			cam_sim_path(sbp->sim),
2468 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2469 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2470 			(uintmax_t)
2471 #endif
2472 				ccg->volume_size);
2473 END_DEBUG
2474 
2475 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2476 		size_mb = ccg->volume_size
2477 			/ ((1024L * 1024L) / ccg->block_size);
2478 
2479 		if (size_mb > 1024 && extended) {
2480 			ccg->heads = 255;
2481 			ccg->secs_per_track = 63;
2482 		} else {
2483 			ccg->heads = 64;
2484 			ccg->secs_per_track = 32;
2485 		}
2486 		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2487 		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2488 		ccb->ccb_h.status = CAM_REQ_CMP;
2489 #else
2490 		cam_calc_geometry(ccg, /*extended*/1);
2491 #endif
2492 		xpt_done(ccb);
2493 		break;
2494 	}
2495 	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
2496 	{
2497 
2498 SBP_DEBUG(1)
2499 		printf("%s:%d:XPT_RESET_BUS: \n",
2500 			device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2501 END_DEBUG
2502 
2503 		ccb->ccb_h.status = CAM_REQ_INVALID;
2504 		xpt_done(ccb);
2505 		break;
2506 	}
2507 	case XPT_PATH_INQ:		/* Path routing inquiry */
2508 	{
2509 		struct ccb_pathinq *cpi = &ccb->cpi;
2510 
2511 SBP_DEBUG(1)
2512 		printf("%s:%d:%d XPT_PATH_INQ:.\n",
2513 			device_get_nameunit(sbp->fd.dev),
2514 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2515 END_DEBUG
2516 		cpi->version_num = 1; /* XXX??? */
2517 		cpi->hba_inquiry = PI_TAG_ABLE;
2518 		cpi->target_sprt = 0;
2519 		cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE;
2520 		cpi->hba_eng_cnt = 0;
2521 		cpi->max_target = SBP_NUM_TARGETS - 1;
2522 		cpi->max_lun = SBP_NUM_LUNS - 1;
2523 		cpi->initiator_id = SBP_INITIATOR;
2524 		cpi->bus_id = sim->bus_id;
2525 		cpi->base_transfer_speed = 400 * 1000 / 8;
2526 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2527 		strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2528 		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2529 		cpi->unit_number = sim->unit_number;
2530                 cpi->transport = XPORT_SPI;	/* XX should have a FireWire */
2531                 cpi->transport_version = 2;
2532                 cpi->protocol = PROTO_SCSI;
2533                 cpi->protocol_version = SCSI_REV_2;
2534 
2535 		cpi->ccb_h.status = CAM_REQ_CMP;
2536 		xpt_done(ccb);
2537 		break;
2538 	}
2539 	case XPT_GET_TRAN_SETTINGS:
2540 	{
2541 		struct ccb_trans_settings *cts = &ccb->cts;
2542 		struct ccb_trans_settings_scsi *scsi =
2543 		    &cts->proto_specific.scsi;
2544 		struct ccb_trans_settings_spi *spi =
2545 		    &cts->xport_specific.spi;
2546 
2547 		cts->protocol = PROTO_SCSI;
2548 		cts->protocol_version = SCSI_REV_2;
2549 		cts->transport = XPORT_SPI;	/* should have a FireWire */
2550 		cts->transport_version = 2;
2551 		spi->valid = CTS_SPI_VALID_DISC;
2552 		spi->flags = CTS_SPI_FLAGS_DISC_ENB;
2553 		scsi->valid = CTS_SCSI_VALID_TQ;
2554 		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2555 SBP_DEBUG(1)
2556 		printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2557 			device_get_nameunit(sbp->fd.dev),
2558 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2559 END_DEBUG
2560 		cts->ccb_h.status = CAM_REQ_CMP;
2561 		xpt_done(ccb);
2562 		break;
2563 	}
2564 	case XPT_ABORT:
2565 		ccb->ccb_h.status = CAM_UA_ABORT;
2566 		xpt_done(ccb);
2567 		break;
2568 	case XPT_SET_TRAN_SETTINGS:
2569 		/* XXX */
2570 	default:
2571 		ccb->ccb_h.status = CAM_REQ_INVALID;
2572 		xpt_done(ccb);
2573 		break;
2574 	}
2575 	return;
2576 }
2577 
2578 static void
2579 sbp_action(struct cam_sim *sim, union ccb *ccb)
2580 {
2581 	int s;
2582 
2583 	s = splfw();
2584 	sbp_action1(sim, ccb);
2585 	splx(s);
2586 }
2587 
2588 static void
2589 sbp_execute_ocb(void *arg,  bus_dma_segment_t *segments, int seg, int error)
2590 {
2591 	int i;
2592 	struct sbp_ocb *ocb;
2593 	struct sbp_ocb *prev;
2594 	bus_dma_segment_t *s;
2595 
2596 	if (error)
2597 		printf("sbp_execute_ocb: error=%d\n", error);
2598 
2599 	ocb = (struct sbp_ocb *)arg;
2600 
2601 SBP_DEBUG(2)
2602 	printf("sbp_execute_ocb: seg %d", seg);
2603 	for (i = 0; i < seg; i++)
2604 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2605 		printf(", %x:%d", segments[i].ds_addr, segments[i].ds_len);
2606 #else
2607 		printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2608 					(uintmax_t)segments[i].ds_len);
2609 #endif
2610 	printf("\n");
2611 END_DEBUG
2612 
2613 	if (seg == 1) {
2614 		/* direct pointer */
2615 		s = &segments[0];
2616 		if (s->ds_len > SBP_SEG_MAX)
2617 			panic("ds_len > SBP_SEG_MAX, fix busdma code");
2618 		ocb->orb[3] = htonl(s->ds_addr);
2619 		ocb->orb[4] |= htonl(s->ds_len);
2620 	} else if(seg > 1) {
2621 		/* page table */
2622 		for (i = 0; i < seg; i++) {
2623 			s = &segments[i];
2624 SBP_DEBUG(0)
2625 			/* XXX LSI Logic "< 16 byte" bug might be hit */
2626 			if (s->ds_len < 16)
2627 				printf("sbp_execute_ocb: warning, "
2628 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2629 					"segment length(%d) is less than 16."
2630 #else
2631 					"segment length(%zd) is less than 16."
2632 #endif
2633 					"(seg=%d/%d)\n", s->ds_len, i+1, seg);
2634 END_DEBUG
2635 			if (s->ds_len > SBP_SEG_MAX)
2636 				panic("ds_len > SBP_SEG_MAX, fix busdma code");
2637 			ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2638 			ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2639 		}
2640 		ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2641 	}
2642 
2643 	if (seg > 0)
2644 		bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2645 			(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2646 			BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2647 	prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2648 	fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2649 	if (use_doorbell) {
2650 		if (prev == NULL) {
2651 			if (ocb->sdev->last_ocb != NULL)
2652 				sbp_doorbell(ocb->sdev);
2653 			else
2654 				sbp_orb_pointer(ocb->sdev, ocb);
2655 		}
2656 	} else {
2657 		if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) {
2658 			ocb->sdev->flags &= ~ORB_LINK_DEAD;
2659 			sbp_orb_pointer(ocb->sdev, ocb);
2660 		}
2661 	}
2662 }
2663 
2664 static void
2665 sbp_poll(struct cam_sim *sim)
2666 {
2667 	struct sbp_softc *sbp;
2668 	struct firewire_comm *fc;
2669 
2670 	sbp = (struct sbp_softc *)sim->softc;
2671 	fc = sbp->fd.fc;
2672 
2673 	fc->poll(fc, 0, -1);
2674 
2675 	return;
2676 }
2677 
2678 static struct sbp_ocb *
2679 sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2680 {
2681 	struct sbp_ocb *ocb;
2682 	struct sbp_ocb *next;
2683 	int s = splfw(), order = 0;
2684 	int flags;
2685 
2686 SBP_DEBUG(1)
2687 	sbp_show_sdev_info(sdev, 2);
2688 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2689 	printf("%s: 0x%08lx src %d\n",
2690 #else
2691 	printf("%s: 0x%08x src %d\n",
2692 #endif
2693 	    __func__, ntohl(sbp_status->orb_lo), sbp_status->src);
2694 END_DEBUG
2695 	for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2696 		next = STAILQ_NEXT(ocb, ocb);
2697 		flags = ocb->flags;
2698 		if (OCB_MATCH(ocb, sbp_status)) {
2699 			/* found */
2700 			STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2701 			if (ocb->ccb != NULL)
2702 				untimeout(sbp_timeout, (caddr_t)ocb,
2703 						ocb->ccb->ccb_h.timeout_ch);
2704 			if (ntohl(ocb->orb[4]) & 0xffff) {
2705 				bus_dmamap_sync(sdev->target->sbp->dmat,
2706 					ocb->dmamap,
2707 					(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2708 					BUS_DMASYNC_POSTREAD :
2709 					BUS_DMASYNC_POSTWRITE);
2710 				bus_dmamap_unload(sdev->target->sbp->dmat,
2711 					ocb->dmamap);
2712 			}
2713 			if (!use_doorbell) {
2714 				if (sbp_status->src == SRC_NO_NEXT) {
2715 					if (next != NULL)
2716 						sbp_orb_pointer(sdev, next);
2717 					else if (order > 0) {
2718 						/*
2719 						 * Unordered execution
2720 						 * We need to send pointer for
2721 						 * next ORB
2722 						 */
2723 						sdev->flags |= ORB_LINK_DEAD;
2724 					}
2725 				}
2726 			} else {
2727 				/*
2728 				 * XXX this is not correct for unordered
2729 				 * execution.
2730 				 */
2731 				if (sdev->last_ocb != NULL)
2732 					sbp_free_ocb(sdev, sdev->last_ocb);
2733 				sdev->last_ocb = ocb;
2734 				if (next != NULL &&
2735 				    sbp_status->src == SRC_NO_NEXT)
2736 					sbp_doorbell(sdev);
2737 			}
2738 			break;
2739 		} else
2740 			order ++;
2741 	}
2742 	splx(s);
2743 SBP_DEBUG(0)
2744 	if (ocb && order > 0) {
2745 		sbp_show_sdev_info(sdev, 2);
2746 		printf("unordered execution order:%d\n", order);
2747 	}
2748 END_DEBUG
2749 	return (ocb);
2750 }
2751 
2752 static struct sbp_ocb *
2753 sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2754 {
2755 	int s = splfw();
2756 	struct sbp_ocb *prev, *prev2;
2757 
2758 SBP_DEBUG(1)
2759 	sbp_show_sdev_info(sdev, 2);
2760 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2761 	printf("%s: 0x%08x\n", __func__, ocb->bus_addr);
2762 #else
2763 	printf("%s: 0x%08jx\n", __func__, (uintmax_t)ocb->bus_addr);
2764 #endif
2765 END_DEBUG
2766 	prev2 = prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2767 	STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2768 
2769 	if (ocb->ccb != NULL)
2770 		ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb,
2771 					(ocb->ccb->ccb_h.timeout * hz) / 1000);
2772 
2773 	if (use_doorbell && prev == NULL)
2774 		prev2 = sdev->last_ocb;
2775 
2776 	if (prev2 != NULL) {
2777 SBP_DEBUG(2)
2778 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2779 		printf("linking chain 0x%x -> 0x%x\n",
2780 		    prev2->bus_addr, ocb->bus_addr);
2781 #else
2782 		printf("linking chain 0x%jx -> 0x%jx\n",
2783 		    (uintmax_t)prev2->bus_addr, (uintmax_t)ocb->bus_addr);
2784 #endif
2785 END_DEBUG
2786 		prev2->orb[1] = htonl(ocb->bus_addr);
2787 		prev2->orb[0] = 0;
2788 	}
2789 	splx(s);
2790 
2791 	return prev;
2792 }
2793 
2794 static struct sbp_ocb *
2795 sbp_get_ocb(struct sbp_dev *sdev)
2796 {
2797 	struct sbp_ocb *ocb;
2798 	int s = splfw();
2799 	ocb = STAILQ_FIRST(&sdev->free_ocbs);
2800 	if (ocb == NULL) {
2801 		sdev->flags |= ORB_SHORTAGE;
2802 		printf("ocb shortage!!!\n");
2803 		splx(s);
2804 		return NULL;
2805 	}
2806 	STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2807 	splx(s);
2808 	ocb->ccb = NULL;
2809 	return (ocb);
2810 }
2811 
2812 static void
2813 sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2814 {
2815 	ocb->flags = 0;
2816 	ocb->ccb = NULL;
2817 	STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2818 	if ((sdev->flags & ORB_SHORTAGE) != 0) {
2819 		int count;
2820 
2821 		sdev->flags &= ~ORB_SHORTAGE;
2822 		count = sdev->freeze;
2823 		sdev->freeze = 0;
2824 		xpt_release_devq(sdev->path, count, TRUE);
2825 	}
2826 }
2827 
2828 static void
2829 sbp_abort_ocb(struct sbp_ocb *ocb, int status)
2830 {
2831 	struct sbp_dev *sdev;
2832 
2833 	sdev = ocb->sdev;
2834 SBP_DEBUG(0)
2835 	sbp_show_sdev_info(sdev, 2);
2836 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2837 	printf("sbp_abort_ocb 0x%x\n", ocb->bus_addr);
2838 #else
2839 	printf("sbp_abort_ocb 0x%jx\n", (uintmax_t)ocb->bus_addr);
2840 #endif
2841 END_DEBUG
2842 SBP_DEBUG(1)
2843 	if (ocb->ccb != NULL)
2844 		sbp_print_scsi_cmd(ocb);
2845 END_DEBUG
2846 	if (ntohl(ocb->orb[4]) & 0xffff) {
2847 		bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
2848 			(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2849 			BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2850 		bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
2851 	}
2852 	if (ocb->ccb != NULL) {
2853 		untimeout(sbp_timeout, (caddr_t)ocb,
2854 					ocb->ccb->ccb_h.timeout_ch);
2855 		ocb->ccb->ccb_h.status = status;
2856 		xpt_done(ocb->ccb);
2857 	}
2858 	sbp_free_ocb(sdev, ocb);
2859 }
2860 
2861 static void
2862 sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2863 {
2864 	int s;
2865 	struct sbp_ocb *ocb, *next;
2866 	STAILQ_HEAD(, sbp_ocb) temp;
2867 
2868 	s = splfw();
2869 
2870 	bcopy(&sdev->ocbs, &temp, sizeof(temp));
2871 	STAILQ_INIT(&sdev->ocbs);
2872 	for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2873 		next = STAILQ_NEXT(ocb, ocb);
2874 		sbp_abort_ocb(ocb, status);
2875 	}
2876 	if (sdev->last_ocb != NULL) {
2877 		sbp_free_ocb(sdev, sdev->last_ocb);
2878 		sdev->last_ocb = NULL;
2879 	}
2880 
2881 	splx(s);
2882 }
2883 
2884 static devclass_t sbp_devclass;
2885 
2886 static device_method_t sbp_methods[] = {
2887 	/* device interface */
2888 	DEVMETHOD(device_identify,	sbp_identify),
2889 	DEVMETHOD(device_probe,		sbp_probe),
2890 	DEVMETHOD(device_attach,	sbp_attach),
2891 	DEVMETHOD(device_detach,	sbp_detach),
2892 	DEVMETHOD(device_shutdown,	sbp_shutdown),
2893 
2894 	{ 0, 0 }
2895 };
2896 
2897 static driver_t sbp_driver = {
2898 	"sbp",
2899 	sbp_methods,
2900 	sizeof(struct sbp_softc),
2901 };
2902 #ifdef __DragonFly__
2903 DECLARE_DUMMY_MODULE(sbp);
2904 #endif
2905 DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2906 MODULE_VERSION(sbp, 1);
2907 MODULE_DEPEND(sbp, firewire, 1, 1, 1);
2908 MODULE_DEPEND(sbp, cam, 1, 1, 1);
2909