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