xref: /freebsd/sys/dev/firewire/sbp.c (revision e50dfdc9abb9eebc78636ee930ece699a837de52)
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 	/* get firmware revision */
645 	reg = crom_search_key(cc, CSRKEY_FIRM_VER);
646 	if (reg != NULL)
647 		snprintf(sdev->revision, sizeof(sdev->revision),
648 						"%06x", reg->val);
649 	/* get product string */
650 	crom_search_key(cc, CSRKEY_MODEL);
651 	crom_next(cc);
652 	crom_parse_text(cc, sdev->product, sizeof(sdev->product));
653 }
654 
655 static void
656 sbp_login_callout(void *arg)
657 {
658 	struct sbp_dev *sdev = (struct sbp_dev *)arg;
659 	sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
660 }
661 
662 #define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
663 	&& crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
664 
665 static void
666 sbp_probe_target(void *arg)
667 {
668 	struct sbp_target *target = (struct sbp_target *)arg;
669 	struct sbp_softc *sbp;
670 	struct sbp_dev *sdev;
671 	struct firewire_comm *fc;
672 	int i, alive;
673 
674 	alive = SBP_FWDEV_ALIVE(target->fwdev);
675 SBP_DEBUG(1)
676 	printf("sbp_probe_target %d\n", target->target_id);
677 	if (!alive)
678 		printf("not alive\n");
679 END_DEBUG
680 
681 	sbp = target->sbp;
682 	fc = target->sbp->fd.fc;
683 	/* XXX untimeout mgm_ocb and dequeue */
684 	for (i=0; i < target->num_lun; i++) {
685 		sdev = &target->luns[i];
686 		if (alive && (sdev->status != SBP_DEV_DEAD)) {
687 			if (sdev->path != NULL) {
688 				xpt_freeze_devq(sdev->path, 1);
689 				sdev->freeze ++;
690 			}
691 			sbp_probe_lun(sdev);
692 SBP_DEBUG(0)
693 			sbp_show_sdev_info(sdev,
694 					(sdev->status == SBP_DEV_RESET));
695 END_DEBUG
696 
697 			sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
698 			switch (sdev->status) {
699 			case SBP_DEV_RESET:
700 				/* new or revived target */
701 				if (auto_login)
702 					sbp_login(sdev);
703 				break;
704 			case SBP_DEV_TOATTACH:
705 			case SBP_DEV_PROBE:
706 			case SBP_DEV_ATTACHED:
707 			case SBP_DEV_RETRY:
708 			default:
709 				sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
710 				break;
711 			}
712 		} else {
713 			switch (sdev->status) {
714 			case SBP_DEV_ATTACHED:
715 SBP_DEBUG(0)
716 				/* the device has gone */
717 				sbp_show_sdev_info(sdev, 2);
718 				printf("lost target\n");
719 END_DEBUG
720 				if (sdev->path) {
721 					xpt_freeze_devq(sdev->path, 1);
722 					sdev->freeze ++;
723 				}
724 				sdev->status = SBP_DEV_RETRY;
725 				sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
726 				break;
727 			case SBP_DEV_PROBE:
728 			case SBP_DEV_TOATTACH:
729 				sdev->status = SBP_DEV_RESET;
730 				break;
731 			case SBP_DEV_RETRY:
732 			case SBP_DEV_RESET:
733 			case SBP_DEV_DEAD:
734 				break;
735 			}
736 		}
737 	}
738 }
739 
740 static void
741 sbp_post_busreset(void *arg)
742 {
743 	struct sbp_softc *sbp;
744 
745 	sbp = (struct sbp_softc *)arg;
746 SBP_DEBUG(0)
747 	printf("sbp_post_busreset\n");
748 END_DEBUG
749 }
750 
751 static void
752 sbp_post_explore(void *arg)
753 {
754 	struct sbp_softc *sbp = (struct sbp_softc *)arg;
755 	struct sbp_target *target;
756 	struct fw_device *fwdev;
757 	int i, alive;
758 
759 SBP_DEBUG(0)
760 	printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
761 END_DEBUG
762 #if 0	/*
763 	 * XXX don't let CAM the bus rest. CAM tries to do something with
764 	 * freezed (DEV_RETRY) devices
765 	 */
766 	xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
767 #endif
768 	if (sbp_cold > 0)
769 		sbp_cold --;
770 
771 	/* Gabage Collection */
772 	for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
773 		target = &sbp->targets[i];
774 		STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
775 			if (target->fwdev == NULL || target->fwdev == fwdev)
776 				break;
777 		if(fwdev == NULL){
778 			/* device has removed in lower driver */
779 			sbp_cam_detach_target(target);
780 			if (target->luns != NULL)
781 				free(target->luns, M_SBP);
782 			target->num_lun = 0;;
783 			target->luns = NULL;
784 			target->fwdev = NULL;
785 		}
786 	}
787 	/* traverse device list */
788 	STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
789 SBP_DEBUG(0)
790 		printf("sbp_post_explore: EUI:%08x%08x ",
791 				fwdev->eui.hi, fwdev->eui.lo);
792 		if (fwdev->status != FWDEVATTACHED)
793 			printf("not attached, state=%d.\n", fwdev->status);
794 		else
795 			printf("attached\n");
796 END_DEBUG
797 		alive = SBP_FWDEV_ALIVE(fwdev);
798 		for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
799 			target = &sbp->targets[i];
800 			if(target->fwdev == fwdev ) {
801 				/* known target */
802 				break;
803 			}
804 		}
805 		if(i == SBP_NUM_TARGETS){
806 			if (alive) {
807 				/* new target */
808 				target = sbp_alloc_target(sbp, fwdev);
809 				if (target == NULL)
810 					continue;
811 			} else {
812 				continue;
813 			}
814 		}
815 		sbp_probe_target((void *)target);
816 	}
817 }
818 
819 #if NEED_RESPONSE
820 static void
821 sbp_loginres_callback(struct fw_xfer *xfer){
822 	int s;
823 	struct sbp_dev *sdev;
824 	sdev = (struct sbp_dev *)xfer->sc;
825 SBP_DEBUG(1)
826 	sbp_show_sdev_info(sdev, 2);
827 	printf("sbp_loginres_callback\n");
828 END_DEBUG
829 	/* recycle */
830 	s = splfw();
831 	STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link);
832 	splx(s);
833 	return;
834 }
835 #endif
836 
837 static __inline void
838 sbp_xfer_free(struct fw_xfer *xfer)
839 {
840 	struct sbp_dev *sdev;
841 	int s;
842 
843 	sdev = (struct sbp_dev *)xfer->sc;
844 	fw_xfer_unload(xfer);
845 	s = splfw();
846 	STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
847 	splx(s);
848 }
849 
850 static void
851 sbp_login_callback(struct fw_xfer *xfer)
852 {
853 SBP_DEBUG(1)
854 	struct sbp_dev *sdev;
855 	sdev = (struct sbp_dev *)xfer->sc;
856 	sbp_show_sdev_info(sdev, 2);
857 	printf("sbp_login_callback\n");
858 END_DEBUG
859 	sbp_xfer_free(xfer);
860 	return;
861 }
862 
863 static void
864 sbp_cmd_callback(struct fw_xfer *xfer)
865 {
866 SBP_DEBUG(2)
867 	struct sbp_dev *sdev;
868 	sdev = (struct sbp_dev *)xfer->sc;
869 	sbp_show_sdev_info(sdev, 2);
870 	printf("sbp_cmd_callback\n");
871 END_DEBUG
872 	sbp_xfer_free(xfer);
873 	return;
874 }
875 
876 static struct sbp_dev *
877 sbp_next_dev(struct sbp_target *target, int lun)
878 {
879 	struct sbp_dev *sdev;
880 	int i;
881 
882 	for (i = lun, sdev = &target->luns[lun];
883 			i < target->num_lun; i++, sdev++) {
884 		if (sdev->status == SBP_DEV_PROBE)
885 			break;
886 	}
887 	if (i >= target->num_lun)
888 		return(NULL);
889 	return(sdev);
890 }
891 
892 #define SCAN_PRI 1
893 static void
894 sbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb)
895 {
896 	struct sbp_target *target;
897 	struct sbp_dev *sdev;
898 
899 	sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
900 	target = sdev->target;
901 SBP_DEBUG(0)
902 	sbp_show_sdev_info(sdev, 2);
903 	printf("sbp_cam_scan_lun\n");
904 END_DEBUG
905 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
906 		sdev->status = SBP_DEV_ATTACHED;
907 	} else {
908 		sbp_show_sdev_info(sdev, 2);
909 		printf("scan failed\n");
910 	}
911 	sdev = sbp_next_dev(target, sdev->lun_id + 1);
912 	if (sdev == NULL) {
913 		free(ccb, M_SBP);
914 		return;
915 	}
916 	/* reuse ccb */
917 	xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
918 	ccb->ccb_h.ccb_sdev_ptr = sdev;
919 	xpt_action(ccb);
920 	xpt_release_devq(sdev->path, sdev->freeze, TRUE);
921 	sdev->freeze = 1;
922 }
923 
924 static void
925 sbp_cam_scan_target(void *arg)
926 {
927 	struct sbp_target *target = (struct sbp_target *)arg;
928 	struct sbp_dev *sdev;
929 	union ccb *ccb;
930 
931 	sdev = sbp_next_dev(target, 0);
932 	if (sdev == NULL) {
933 		printf("sbp_cam_scan_target: nothing to do for target%d\n",
934 							target->target_id);
935 		return;
936 	}
937 SBP_DEBUG(0)
938 	sbp_show_sdev_info(sdev, 2);
939 	printf("sbp_cam_scan_target\n");
940 END_DEBUG
941 	ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO);
942 	if (ccb == NULL) {
943 		printf("sbp_cam_scan_target: malloc failed\n");
944 		return;
945 	}
946 	xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
947 	ccb->ccb_h.func_code = XPT_SCAN_LUN;
948 	ccb->ccb_h.cbfcnp = sbp_cam_scan_lun;
949 	ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
950 	ccb->crcn.flags = CAM_FLAG_NONE;
951 	ccb->ccb_h.ccb_sdev_ptr = sdev;
952 
953 	/* The scan is in progress now. */
954 	xpt_action(ccb);
955 	xpt_release_devq(sdev->path, sdev->freeze, TRUE);
956 	sdev->freeze = 1;
957 }
958 
959 static __inline void
960 sbp_scan_dev(struct sbp_dev *sdev)
961 {
962 	sdev->status = SBP_DEV_PROBE;
963 	callout_reset(&sdev->target->scan_callout, SCAN_DELAY * hz,
964 			sbp_cam_scan_target, (void *)sdev->target);
965 }
966 
967 static void
968 sbp_do_attach(struct fw_xfer *xfer)
969 {
970 	struct sbp_dev *sdev;
971 	struct sbp_target *target;
972 	struct sbp_softc *sbp;
973 
974 	sdev = (struct sbp_dev *)xfer->sc;
975 	target = sdev->target;
976 	sbp = target->sbp;
977 SBP_DEBUG(0)
978 	sbp_show_sdev_info(sdev, 2);
979 	printf("sbp_do_attach\n");
980 END_DEBUG
981 	sbp_xfer_free(xfer);
982 
983 	if (sdev->path == NULL)
984 		xpt_create_path(&sdev->path, xpt_periph,
985 			cam_sim_path(target->sbp->sim),
986 			target->target_id, sdev->lun_id);
987 
988 	/*
989 	 * Let CAM scan the bus if we are in the boot process.
990 	 * XXX xpt_scan_bus cannot detect LUN larger than 0
991 	 * if LUN 0 doesn't exists.
992 	 */
993 	if (sbp_cold > 0) {
994 		sdev->status = SBP_DEV_ATTACHED;
995 		return;
996 	}
997 
998 	sbp_scan_dev(sdev);
999 	return;
1000 }
1001 
1002 static void
1003 sbp_agent_reset_callback(struct fw_xfer *xfer)
1004 {
1005 	struct sbp_dev *sdev;
1006 
1007 	sdev = (struct sbp_dev *)xfer->sc;
1008 SBP_DEBUG(1)
1009 	sbp_show_sdev_info(sdev, 2);
1010 	printf("sbp_cmd_callback\n");
1011 END_DEBUG
1012 	sbp_xfer_free(xfer);
1013 	if (sdev->path) {
1014 		xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1015 		sdev->freeze = 0;
1016 	}
1017 }
1018 
1019 static void
1020 sbp_agent_reset(struct sbp_dev *sdev)
1021 {
1022 	struct fw_xfer *xfer;
1023 	struct fw_pkt *fp;
1024 
1025 SBP_DEBUG(0)
1026 	sbp_show_sdev_info(sdev, 2);
1027 	printf("sbp_agent_reset\n");
1028 END_DEBUG
1029 	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1030 	if (xfer == NULL)
1031 		return;
1032 	if (sdev->status == SBP_DEV_ATTACHED)
1033 		xfer->act.hand = sbp_agent_reset_callback;
1034 	else
1035 		xfer->act.hand = sbp_do_attach;
1036 	fp = (struct fw_pkt *)xfer->send.buf;
1037 	fp->mode.wreqq.data = htonl(0xf);
1038 	fw_asyreq(xfer->fc, -1, xfer);
1039 	sbp_abort_all_ocbs(sdev, CAM_BDR_SENT);
1040 }
1041 
1042 static void
1043 sbp_busy_timeout_callback(struct fw_xfer *xfer)
1044 {
1045 	struct sbp_dev *sdev;
1046 
1047 	sdev = (struct sbp_dev *)xfer->sc;
1048 SBP_DEBUG(1)
1049 	sbp_show_sdev_info(sdev, 2);
1050 	printf("sbp_busy_timeout_callback\n");
1051 END_DEBUG
1052 	sbp_xfer_free(xfer);
1053 	sbp_agent_reset(sdev);
1054 }
1055 
1056 static void
1057 sbp_busy_timeout(struct sbp_dev *sdev)
1058 {
1059 	struct fw_pkt *fp;
1060 	struct fw_xfer *xfer;
1061 SBP_DEBUG(0)
1062 	sbp_show_sdev_info(sdev, 2);
1063 	printf("sbp_busy_timeout\n");
1064 END_DEBUG
1065 	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1066 
1067 	xfer->act.hand = sbp_busy_timeout_callback;
1068 	fp = (struct fw_pkt *)xfer->send.buf;
1069 	fp->mode.wreqq.dest_hi = 0xffff;
1070 	fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1071 	fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1072 	fw_asyreq(xfer->fc, -1, xfer);
1073 }
1074 
1075 #if 0
1076 static void
1077 sbp_reset_start(struct sbp_dev *sdev)
1078 {
1079 	struct fw_xfer *xfer;
1080 	struct fw_pkt *fp;
1081 
1082 SBP_DEBUG(0)
1083 	sbp_show_sdev_info(sdev, 2);
1084 	printf("sbp_reset_start\n");
1085 END_DEBUG
1086 	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1087 
1088 	xfer->act.hand = sbp_busy_timeout;
1089 	fp = (struct fw_pkt *)xfer->send.buf;
1090 	fp->mode.wreqq.dest_hi = 0xffff;
1091 	fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
1092 	fp->mode.wreqq.data = htonl(0xf);
1093 	fw_asyreq(xfer->fc, -1, xfer);
1094 }
1095 #endif
1096 
1097 static void
1098 sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1099 {
1100 	struct fw_xfer *xfer;
1101 	struct fw_pkt *fp;
1102 SBP_DEBUG(2)
1103 	sbp_show_sdev_info(sdev, 2);
1104 	printf("sbp_orb_pointer\n");
1105 END_DEBUG
1106 
1107 	xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1108 	if (xfer == NULL)
1109 		return;
1110 	xfer->act.hand = sbp_cmd_callback;
1111 
1112 	fp = (struct fw_pkt *)xfer->send.buf;
1113 	fp->mode.wreqb.len = 8;
1114 	fp->mode.wreqb.extcode = 0;
1115 	fp->mode.wreqb.payload[0] =
1116 		htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1117 	fp->mode.wreqb.payload[1] = htonl(ocb->bus_addr);
1118 
1119 	if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1120 			sbp_xfer_free(xfer);
1121 			ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1122 			xpt_done(ocb->ccb);
1123 	}
1124 }
1125 
1126 #if 0
1127 static void
1128 sbp_doorbell(struct sbp_dev *sdev)
1129 {
1130 	struct fw_xfer *xfer;
1131 	struct fw_pkt *fp;
1132 SBP_DEBUG(1)
1133 	sbp_show_sdev_info(sdev, 2);
1134 	printf("sbp_doorbell\n");
1135 END_DEBUG
1136 
1137 	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1138 	if (xfer == NULL)
1139 		return;
1140 	xfer->act.hand = sbp_cmd_callback;
1141 	fp = (struct fw_pkt *)xfer->send.buf;
1142 	fp->mode.wreqq.data = htonl(0xf);
1143 	fw_asyreq(xfer->fc, -1, xfer);
1144 }
1145 #endif
1146 
1147 static struct fw_xfer *
1148 sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1149 {
1150 	struct fw_xfer *xfer;
1151 	struct fw_pkt *fp;
1152 	struct sbp_target *target;
1153 	int s, new = 0;
1154 
1155 	target = sdev->target;
1156 	s = splfw();
1157 	xfer = STAILQ_FIRST(&target->xferlist);
1158 	if (xfer == NULL) {
1159 		if (target->n_xfer > 5 /* XXX */) {
1160 			printf("sbp: no more xfer for this target\n");
1161 			splx(s);
1162 			return(NULL);
1163 		}
1164 		xfer = fw_xfer_alloc_buf(M_SBP, 24, 12);
1165 		if(xfer == NULL){
1166 			printf("sbp: fw_xfer_alloc_buf failed\n");
1167 			splx(s);
1168 			return NULL;
1169 		}
1170 		target->n_xfer ++;
1171 		if (debug)
1172 			printf("sbp: alloc %d xfer\n", target->n_xfer);
1173 		new = 1;
1174 	} else {
1175 		STAILQ_REMOVE_HEAD(&target->xferlist, link);
1176 	}
1177 	splx(s);
1178 
1179 	microtime(&xfer->tv);
1180 
1181 	if (tcode == FWTCODE_WREQQ)
1182 		xfer->send.len = 16;
1183 	else
1184 		xfer->send.len = 24;
1185 	xfer->recv.len = 12;
1186 
1187 	if (new) {
1188 		xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1189 		xfer->fc = sdev->target->sbp->fd.fc;
1190 		xfer->retry_req = fw_asybusy;
1191 	}
1192 	xfer->sc = (caddr_t)sdev;
1193 	fp = (struct fw_pkt *)xfer->send.buf;
1194 	fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
1195 	fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
1196 	fp->mode.wreqq.tlrt = 0;
1197 	fp->mode.wreqq.tcode = tcode;
1198 	fp->mode.wreqq.pri = 0;
1199 	xfer->dst = FWLOCALBUS | sdev->target->fwdev->dst;
1200 	fp->mode.wreqq.dst = xfer->dst;
1201 
1202 	return xfer;
1203 
1204 }
1205 
1206 static void
1207 sbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1208 {
1209 	struct fw_xfer *xfer;
1210 	struct fw_pkt *fp;
1211 	struct sbp_ocb *ocb;
1212 	struct sbp_target *target;
1213 	int s, nid;
1214 
1215 	target = sdev->target;
1216 	nid = target->sbp->fd.fc->nodeid | FWLOCALBUS;
1217 
1218 	s = splfw();
1219 	if (func == ORB_FUN_RUNQUEUE) {
1220 		ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1221 		if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1222 			splx(s);
1223 			return;
1224 		}
1225 		STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1226 		goto start;
1227 	}
1228 	if ((ocb = sbp_get_ocb(sdev)) == NULL) {
1229 		splx(s);
1230 		return;
1231 	}
1232 	ocb->flags = OCB_ACT_MGM;
1233 	ocb->sdev = sdev;
1234 
1235 	bzero((void *)(uintptr_t)(volatile void *)ocb->orb, sizeof(ocb->orb));
1236 	ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1237 	ocb->orb[7] = htonl(SBP_DEV2ADDR(
1238 		device_get_unit(target->sbp->fd.dev),
1239 		target->target_id,
1240 		sdev->lun_id));
1241 
1242 SBP_DEBUG(0)
1243 	sbp_show_sdev_info(sdev, 2);
1244 	printf("%s\n", orb_fun_name[(func>>16)&0xf]);
1245 END_DEBUG
1246 	switch (func) {
1247 	case ORB_FUN_LGI:
1248 		ocb->orb[2] = htonl(nid << 16);
1249 		ocb->orb[3] = htonl(sdev->dma.bus_addr);
1250 		ocb->orb[4] = htonl(ORB_NOTIFY | ORB_EXV | sdev->lun_id);
1251 		ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
1252 		fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD);
1253 		break;
1254 	case ORB_FUN_ATA:
1255 		ocb->orb[0] = htonl((0 << 16) | 0);
1256 		ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
1257 		/* fall through */
1258 	case ORB_FUN_RCN:
1259 	case ORB_FUN_LGO:
1260 	case ORB_FUN_LUR:
1261 	case ORB_FUN_RST:
1262 	case ORB_FUN_ATS:
1263 		ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
1264 		break;
1265 	}
1266 
1267 	if (target->mgm_ocb_cur != NULL) {
1268 		/* there is a standing ORB */
1269 		STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1270 		splx(s);
1271 		return;
1272 	}
1273 start:
1274 	target->mgm_ocb_cur = ocb;
1275 	splx(s);
1276 
1277 	callout_reset(&target->mgm_ocb_timeout, 5*hz,
1278 				sbp_timeout, (caddr_t)ocb);
1279 	xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1280 	if(xfer == NULL){
1281 		return;
1282 	}
1283 	xfer->act.hand = sbp_login_callback;
1284 
1285 	fp = (struct fw_pkt *)xfer->send.buf;
1286 	fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
1287 	fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
1288 	fp->mode.wreqb.len = 8;
1289 	fp->mode.wreqb.extcode = 0;
1290 	fp->mode.wreqb.payload[0] = htonl(nid << 16);
1291 	fp->mode.wreqb.payload[1] = htonl(ocb->bus_addr);
1292 
1293 	fw_asyreq(xfer->fc, -1, xfer);
1294 }
1295 
1296 static void
1297 sbp_print_scsi_cmd(struct sbp_ocb *ocb)
1298 {
1299 	struct ccb_scsiio *csio;
1300 
1301 	csio = &ocb->ccb->csio;
1302 	printf("%s:%d:%d XPT_SCSI_IO: "
1303 		"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1304 		", flags: 0x%02x, "
1305 		"%db cmd/%db data/%db sense\n",
1306 		device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1307 		ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun,
1308 		csio->cdb_io.cdb_bytes[0],
1309 		csio->cdb_io.cdb_bytes[1],
1310 		csio->cdb_io.cdb_bytes[2],
1311 		csio->cdb_io.cdb_bytes[3],
1312 		csio->cdb_io.cdb_bytes[4],
1313 		csio->cdb_io.cdb_bytes[5],
1314 		csio->cdb_io.cdb_bytes[6],
1315 		csio->cdb_io.cdb_bytes[7],
1316 		csio->cdb_io.cdb_bytes[8],
1317 		csio->cdb_io.cdb_bytes[9],
1318 		ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1319 		csio->cdb_len, csio->dxfer_len,
1320 		csio->sense_len);
1321 }
1322 
1323 static void
1324 sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1325 {
1326 	struct sbp_cmd_status *sbp_cmd_status;
1327 	struct scsi_sense_data *sense;
1328 
1329 	sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1330 	sense = &ocb->ccb->csio.sense_data;
1331 
1332 SBP_DEBUG(0)
1333 	sbp_print_scsi_cmd(ocb);
1334 	/* XXX need decode status */
1335 	sbp_show_sdev_info(ocb->sdev, 2);
1336 	printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1337 		sbp_cmd_status->status,
1338 		sbp_cmd_status->sfmt,
1339 		sbp_cmd_status->valid,
1340 		sbp_cmd_status->s_key,
1341 		sbp_cmd_status->s_code,
1342 		sbp_cmd_status->s_qlfr,
1343 		sbp_status->len
1344 	);
1345 END_DEBUG
1346 
1347 	switch (sbp_cmd_status->status) {
1348 	case SCSI_STATUS_CHECK_COND:
1349 	case SCSI_STATUS_BUSY:
1350 	case SCSI_STATUS_CMD_TERMINATED:
1351 		if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1352 			sense->error_code = SSD_CURRENT_ERROR;
1353 		}else{
1354 			sense->error_code = SSD_DEFERRED_ERROR;
1355 		}
1356 		if(sbp_cmd_status->valid)
1357 			sense->error_code |= SSD_ERRCODE_VALID;
1358 		sense->flags = sbp_cmd_status->s_key;
1359 		if(sbp_cmd_status->mark)
1360 			sense->flags |= SSD_FILEMARK;
1361 		if(sbp_cmd_status->eom)
1362 			sense->flags |= SSD_EOM;
1363 		if(sbp_cmd_status->ill_len)
1364 			sense->flags |= SSD_ILI;
1365 		sense->info[0] = ntohl(sbp_cmd_status->info) & 0xff;
1366 		sense->info[1] =(ntohl(sbp_cmd_status->info) >> 8) & 0xff;
1367 		sense->info[2] =(ntohl(sbp_cmd_status->info) >> 16) & 0xff;
1368 		sense->info[3] =(ntohl(sbp_cmd_status->info) >> 24) & 0xff;
1369 		if (sbp_status->len <= 1)
1370 			/* XXX not scsi status. shouldn't be happened */
1371 			sense->extra_len = 0;
1372 		else if (sbp_status->len <= 4)
1373 			/* add_sense_code(_qual), info, cmd_spec_info */
1374 			sense->extra_len = 6;
1375 		else
1376 			/* fru, sense_key_spec */
1377 			sense->extra_len = 10;
1378 		sense->cmd_spec_info[0] = ntohl(sbp_cmd_status->cdb) & 0xff;
1379 		sense->cmd_spec_info[1] = (ntohl(sbp_cmd_status->cdb) >> 8) & 0xff;
1380 		sense->cmd_spec_info[2] = (ntohl(sbp_cmd_status->cdb) >> 16) & 0xff;
1381 		sense->cmd_spec_info[3] = (ntohl(sbp_cmd_status->cdb) >> 24) & 0xff;
1382 		sense->add_sense_code = sbp_cmd_status->s_code;
1383 		sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1384 		sense->fru = sbp_cmd_status->fru;
1385 		sense->sense_key_spec[0] = ntohl(sbp_cmd_status->s_keydep) & 0xff;
1386 		sense->sense_key_spec[1] = (ntohl(sbp_cmd_status->s_keydep) >>8) & 0xff;
1387 		sense->sense_key_spec[2] = (ntohl(sbp_cmd_status->s_keydep) >>16) & 0xff;
1388 
1389 		ocb->ccb->csio.scsi_status = sbp_cmd_status->status;;
1390 		ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1391 							| CAM_AUTOSNS_VALID;
1392 /*
1393 {
1394 		u_int8_t j, *tmp;
1395 		tmp = sense;
1396 		for( j = 0 ; j < 32 ; j+=8){
1397 			printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1398 				tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1399 				tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1400 		}
1401 
1402 }
1403 */
1404 		break;
1405 	default:
1406 		sbp_show_sdev_info(ocb->sdev, 2);
1407 		printf("sbp_scsi_status: unknown scsi status 0x%x\n",
1408 						sbp_cmd_status->status);
1409 	}
1410 }
1411 
1412 static void
1413 sbp_fix_inq_data(struct sbp_ocb *ocb)
1414 {
1415 	union ccb *ccb;
1416 	struct sbp_dev *sdev;
1417 	struct scsi_inquiry_data *inq;
1418 
1419 	ccb = ocb->ccb;
1420 	sdev = ocb->sdev;
1421 
1422 	if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1423 		return;
1424 SBP_DEBUG(1)
1425 	sbp_show_sdev_info(sdev, 2);
1426 	printf("sbp_fix_inq_data\n");
1427 END_DEBUG
1428 	inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1429 	switch (SID_TYPE(inq)) {
1430 	case T_DIRECT:
1431 		/*
1432 		 * XXX Convert Direct Access device to RBC.
1433 		 * I've never seen FireWire DA devices which support READ_6.
1434 		 */
1435 #if 1
1436 		if (SID_TYPE(inq) == T_DIRECT)
1437 			inq->device |= T_RBC; /*  T_DIRECT == 0 */
1438 #endif
1439 		/* fall through */
1440 	case T_RBC:
1441 		/* enable tag queuing */
1442 #if 1
1443 		inq->flags |= SID_CmdQue;
1444 #endif
1445 		/*
1446 		 * Override vendor/product/revision information.
1447 		 * Some devices sometimes return strange strings.
1448 		 */
1449 #if 1
1450 		bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1451 		bcopy(sdev->product, inq->product, sizeof(inq->product));
1452 		bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1453 #endif
1454 		break;
1455 	}
1456 }
1457 
1458 static void
1459 sbp_recv1(struct fw_xfer *xfer)
1460 {
1461 	struct fw_pkt *rfp;
1462 #if NEED_RESPONSE
1463 	struct fw_pkt *sfp;
1464 #endif
1465 	struct sbp_softc *sbp;
1466 	struct sbp_dev *sdev;
1467 	struct sbp_ocb *ocb;
1468 	struct sbp_login_res *login_res = NULL;
1469 	struct sbp_status *sbp_status;
1470 	struct sbp_target *target;
1471 	int	orb_fun, status_valid0, status_valid, t, l, reset_agent = 0;
1472 	u_int32_t addr;
1473 /*
1474 	u_int32_t *ld;
1475 	ld = xfer->recv.buf;
1476 printf("sbp %x %d %d %08x %08x %08x %08x\n",
1477 			xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1478 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1479 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1480 */
1481 
1482 	sbp = (struct sbp_softc *)xfer->sc;
1483 	if(xfer->resp != 0){
1484 		printf("sbp_recv: xfer->resp != 0\n");
1485 		goto done0;
1486 	}
1487 	if(xfer->recv.buf == NULL){
1488 		printf("sbp_recv: xfer->recv.buf == NULL\n");
1489 		goto done0;
1490 	}
1491 	sbp = (struct sbp_softc *)xfer->sc;
1492 	rfp = (struct fw_pkt *)xfer->recv.buf;
1493 	if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1494 		printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1495 		goto done0;
1496 	}
1497 	sbp_status = (struct sbp_status *)rfp->mode.wreqb.payload;
1498 	addr = rfp->mode.wreqb.dest_lo;
1499 SBP_DEBUG(2)
1500 	printf("received address 0x%x\n", addr);
1501 END_DEBUG
1502 	t = SBP_ADDR2TRG(addr);
1503 	if (t >= SBP_NUM_TARGETS) {
1504 		device_printf(sbp->fd.dev,
1505 			"sbp_recv1: invalid target %d\n", t);
1506 		goto done0;
1507 	}
1508 	target = &sbp->targets[t];
1509 	l = SBP_ADDR2LUN(addr);
1510 	if (l >= target->num_lun) {
1511 		device_printf(sbp->fd.dev,
1512 			"sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1513 		goto done0;
1514 	}
1515 	sdev = &target->luns[l];
1516 
1517 	ocb = NULL;
1518 	switch (sbp_status->src) {
1519 	case 0:
1520 	case 1:
1521 		/* check mgm_ocb_cur first */
1522 		ocb  = target->mgm_ocb_cur;
1523 		if (ocb != NULL) {
1524 			if (OCB_MATCH(ocb, sbp_status)) {
1525 				callout_stop(&target->mgm_ocb_timeout);
1526 				target->mgm_ocb_cur = NULL;
1527 				break;
1528 			}
1529 		}
1530 		ocb = sbp_dequeue_ocb(sdev, sbp_status);
1531 		if (ocb == NULL) {
1532 			sbp_show_sdev_info(sdev, 2);
1533 #if __FreeBSD_version >= 500000
1534 			printf("No ocb(%x) on the queue\n",
1535 #else
1536 			printf("No ocb(%lx) on the queue\n",
1537 #endif
1538 					ntohl(sbp_status->orb_lo));
1539 		}
1540 		break;
1541 	case 2:
1542 		/* unsolicit */
1543 		sbp_show_sdev_info(sdev, 2);
1544 		printf("unsolicit status received\n");
1545 		break;
1546 	default:
1547 		sbp_show_sdev_info(sdev, 2);
1548 		printf("unknown sbp_status->src\n");
1549 	}
1550 
1551 	status_valid0 = (sbp_status->src < 2
1552 			&& sbp_status->resp == ORB_RES_CMPL
1553 			&& sbp_status->dead == 0);
1554 	status_valid = (status_valid0 && sbp_status->status == 0);
1555 
1556 	if (!status_valid0 || debug > 1){
1557 		int status;
1558 SBP_DEBUG(0)
1559 		sbp_show_sdev_info(sdev, 2);
1560 		printf("ORB status src:%x resp:%x dead:%x"
1561 #if __FreeBSD_version >= 500000
1562 				" len:%x stat:%x orb:%x%08x\n",
1563 #else
1564 				" len:%x stat:%x orb:%x%08lx\n",
1565 #endif
1566 			sbp_status->src, sbp_status->resp, sbp_status->dead,
1567 			sbp_status->len, sbp_status->status,
1568 			ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1569 END_DEBUG
1570 		sbp_show_sdev_info(sdev, 2);
1571 		status = sbp_status->status;
1572 		switch(sbp_status->resp) {
1573 		case 0:
1574 			if (status > MAX_ORB_STATUS0)
1575 				printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1576 			else
1577 				printf("%s\n", orb_status0[status]);
1578 			break;
1579 		case 1:
1580 			printf("Obj: %s, Error: %s\n",
1581 				orb_status1_object[(status>>6) & 3],
1582 				orb_status1_serial_bus_error[status & 0xf]);
1583 			break;
1584 		case 2:
1585 			printf("Illegal request\n");
1586 			break;
1587 		case 3:
1588 			printf("Vendor dependent\n");
1589 			break;
1590 		default:
1591 			printf("unknown respose code %d\n", sbp_status->resp);
1592 		}
1593 	}
1594 
1595 	/* we have to reset the fetch agent if it's dead */
1596 	if (sbp_status->dead) {
1597 		if (sdev->path) {
1598 			xpt_freeze_devq(sdev->path, 1);
1599 			sdev->freeze ++;
1600 		}
1601 		reset_agent = 1;
1602 	}
1603 
1604 	if (ocb == NULL)
1605 		goto done;
1606 
1607 	sdev->flags &= ~SBP_DEV_TIMEOUT;
1608 
1609 	switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1610 	case ORB_FMT_NOP:
1611 		break;
1612 	case ORB_FMT_VED:
1613 		break;
1614 	case ORB_FMT_STD:
1615 		switch(ocb->flags) {
1616 		case OCB_ACT_MGM:
1617 			orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1618 			switch(orb_fun) {
1619 			case ORB_FUN_LGI:
1620 				fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD);
1621 				login_res = sdev->login;
1622 				login_res->len = ntohs(login_res->len);
1623 				login_res->id = ntohs(login_res->id);
1624 				login_res->cmd_hi = ntohs(login_res->cmd_hi);
1625 				login_res->cmd_lo = ntohl(login_res->cmd_lo);
1626 				if (status_valid) {
1627 SBP_DEBUG(0)
1628 sbp_show_sdev_info(sdev, 2);
1629 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));
1630 END_DEBUG
1631 					sbp_busy_timeout(sdev);
1632 				} else {
1633 					/* forgot logout? */
1634 					sbp_show_sdev_info(sdev, 2);
1635 					printf("login failed\n");
1636 					sdev->status = SBP_DEV_RESET;
1637 				}
1638 				break;
1639 			case ORB_FUN_RCN:
1640 				login_res = sdev->login;
1641 				if (status_valid) {
1642 SBP_DEBUG(0)
1643 sbp_show_sdev_info(sdev, 2);
1644 printf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo);
1645 END_DEBUG
1646 #if 1
1647 					if (sdev->status == SBP_DEV_ATTACHED)
1648 						sbp_scan_dev(sdev);
1649 					else
1650 						sbp_agent_reset(sdev);
1651 #else
1652 					sdev->status = SBP_DEV_ATTACHED;
1653 					sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL);
1654 #endif
1655 				} else {
1656 					/* reconnection hold time exceed? */
1657 SBP_DEBUG(0)
1658 					sbp_show_sdev_info(sdev, 2);
1659 					printf("reconnect failed\n");
1660 END_DEBUG
1661 					sbp_login(sdev);
1662 				}
1663 				break;
1664 			case ORB_FUN_LGO:
1665 				sdev->status = SBP_DEV_RESET;
1666 				break;
1667 			case ORB_FUN_RST:
1668 				sbp_busy_timeout(sdev);
1669 				break;
1670 			case ORB_FUN_LUR:
1671 			case ORB_FUN_ATA:
1672 			case ORB_FUN_ATS:
1673 				sbp_agent_reset(sdev);
1674 				break;
1675 			default:
1676 				sbp_show_sdev_info(sdev, 2);
1677 				printf("unknown function %d\n", orb_fun);
1678 				break;
1679 			}
1680 			sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
1681 			break;
1682 		case OCB_ACT_CMD:
1683 			if(ocb->ccb != NULL){
1684 				union ccb *ccb;
1685 /*
1686 				u_int32_t *ld;
1687 				ld = ocb->ccb->csio.data_ptr;
1688 				if(ld != NULL && ocb->ccb->csio.dxfer_len != 0)
1689 					printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]);
1690 				else
1691 					printf("ptr NULL\n");
1692 printf("len %d\n", sbp_status->len);
1693 */
1694 				ccb = ocb->ccb;
1695 				if(sbp_status->len > 1){
1696 					sbp_scsi_status(sbp_status, ocb);
1697 				}else{
1698 					if(sbp_status->resp != ORB_RES_CMPL){
1699 						ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1700 					}else{
1701 						ccb->ccb_h.status = CAM_REQ_CMP;
1702 					}
1703 				}
1704 				/* fix up inq data */
1705 				if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1706 					sbp_fix_inq_data(ocb);
1707 				xpt_done(ccb);
1708 			}
1709 			break;
1710 		default:
1711 			break;
1712 		}
1713 	}
1714 
1715 	sbp_free_ocb(sdev, ocb);
1716 done:
1717 	if (reset_agent)
1718 		sbp_agent_reset(sdev);
1719 
1720 done0:
1721 /* The received packet is usually small enough to be stored within
1722  * the buffer. In that case, the controller return ack_complete and
1723  * no respose is necessary.
1724  *
1725  * XXX fwohci.c and firewire.c should inform event_code such as
1726  * ack_complete or ack_pending to upper driver.
1727  */
1728 #if NEED_RESPONSE
1729 	xfer->send.off = 0;
1730 	sfp = (struct fw_pkt *)xfer->send.buf;
1731 	sfp->mode.wres.dst = rfp->mode.wreqb.src;
1732 	xfer->dst = sfp->mode.wres.dst;
1733 	xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1734 	xfer->act.hand = sbp_loginres_callback;
1735 	xfer->retry_req = fw_asybusy;
1736 
1737 	sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1738 	sfp->mode.wres.tcode = FWTCODE_WRES;
1739 	sfp->mode.wres.rtcode = 0;
1740 	sfp->mode.wres.pri = 0;
1741 
1742 	fw_asyreq(xfer->fc, -1, xfer);
1743 #else
1744 	/* recycle */
1745 	xfer->recv.len = SBP_RECV_LEN;
1746 	STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1747 #endif
1748 
1749 	return;
1750 
1751 }
1752 
1753 static void
1754 sbp_recv(struct fw_xfer *xfer)
1755 {
1756 	int s;
1757 
1758 	s = splcam();
1759 	sbp_recv1(xfer);
1760 	splx(s);
1761 }
1762 /*
1763  * sbp_attach()
1764  */
1765 static int
1766 sbp_attach(device_t dev)
1767 {
1768 	struct sbp_softc *sbp;
1769 	struct cam_devq *devq;
1770 	struct fw_xfer *xfer;
1771 	int i, s, error;
1772 
1773 SBP_DEBUG(0)
1774 	printf("sbp_attach (cold=%d)\n", cold);
1775 END_DEBUG
1776 
1777 	if (cold)
1778 		sbp_cold ++;
1779 	sbp = ((struct sbp_softc *)device_get_softc(dev));
1780 	bzero(sbp, sizeof(struct sbp_softc));
1781 	sbp->fd.dev = dev;
1782 	sbp->fd.fc = device_get_ivars(dev);
1783 	error = bus_dma_tag_create(/*parent*/sbp->fd.fc->dmat,
1784 				/* XXX shoud be 4 for sane backend? */
1785 				/*alignment*/1,
1786 				/*boundary*/0,
1787 				/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1788 				/*highaddr*/BUS_SPACE_MAXADDR,
1789 				/*filter*/NULL, /*filterarg*/NULL,
1790 				/*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1791 				/*maxsegsz*/SBP_SEG_MAX,
1792 				/*flags*/BUS_DMA_ALLOCNOW,
1793 				&sbp->dmat);
1794 	if (error != 0) {
1795 		printf("sbp_attach: Could not allocate DMA tag "
1796 			"- error %d\n", error);
1797 			return (ENOMEM);
1798 	}
1799 
1800 	devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1801 	if (devq == NULL)
1802 		return (ENXIO);
1803 
1804 	for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1805 		sbp->targets[i].fwdev = NULL;
1806 		sbp->targets[i].luns = NULL;
1807 	}
1808 
1809 	sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1810 				 device_get_unit(dev),
1811 				 /*untagged*/ 1,
1812 				 /*tagged*/ SBP_QUEUE_LEN,
1813 				 devq);
1814 
1815 	if (sbp->sim == NULL) {
1816 		cam_simq_free(devq);
1817 		return (ENXIO);
1818 	}
1819 
1820 
1821 	if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS)
1822 		goto fail;
1823 
1824 	if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim),
1825 			CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1826 		goto fail;
1827 
1828 	sbp->fwb.start_hi = SBP_BIND_HI;
1829 	sbp->fwb.start_lo = SBP_DEV2ADDR(device_get_unit(sbp->fd.dev), 0, 0);
1830 	/* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1831 	sbp->fwb.addrlen = 0xffff;
1832 	sbp->fwb.act_type = FWACT_XFER;
1833 	/* pre-allocate xfer */
1834 	STAILQ_INIT(&sbp->fwb.xferlist);
1835 	for (i = 0; i < SBP_NUM_OCB/2; i ++) {
1836 		xfer = fw_xfer_alloc_buf(M_SBP,
1837 #if NEED_RESPONSE
1838 			/* send */12,
1839 #else
1840 			/* send */0,
1841 #endif
1842 			/* recv */SBP_RECV_LEN);
1843 		xfer->act.hand = sbp_recv;
1844 #if NEED_RESPONSE
1845 		xfer->fc = sbp->fd.fc;
1846 #endif
1847 		xfer->sc = (caddr_t)sbp;
1848 		STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1849 	}
1850 	fw_bindadd(sbp->fd.fc, &sbp->fwb);
1851 
1852 	sbp->fd.post_busreset = sbp_post_busreset;
1853 	sbp->fd.post_explore = sbp_post_explore;
1854 
1855 	if (sbp->fd.fc->status != -1) {
1856 		s = splfw();
1857 		sbp_post_explore((void *)sbp);
1858 		splx(s);
1859 	}
1860 
1861 	return (0);
1862 fail:
1863 	cam_sim_free(sbp->sim, /*free_devq*/TRUE);
1864 	return (ENXIO);
1865 }
1866 
1867 static int
1868 sbp_logout_all(struct sbp_softc *sbp)
1869 {
1870 	struct sbp_target *target;
1871 	struct sbp_dev *sdev;
1872 	int i, j;
1873 
1874 SBP_DEBUG(0)
1875 	printf("sbp_logout_all\n");
1876 END_DEBUG
1877 	for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
1878 		target = &sbp->targets[i];
1879 		if (target->luns == NULL)
1880 			continue;
1881 		for (j = 0; j < target->num_lun; j++) {
1882 			sdev = &target->luns[j];
1883 			callout_stop(&sdev->login_callout);
1884 			if (sdev->status >= SBP_DEV_TOATTACH &&
1885 					sdev->status <= SBP_DEV_ATTACHED)
1886 				sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
1887 		}
1888 	}
1889 
1890 	return 0;
1891 }
1892 
1893 static int
1894 sbp_shutdown(device_t dev)
1895 {
1896 	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1897 
1898 	sbp_logout_all(sbp);
1899 	return (0);
1900 }
1901 
1902 static int
1903 sbp_detach(device_t dev)
1904 {
1905 	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1906 	struct firewire_comm *fc = sbp->fd.fc;
1907 	struct sbp_target *target;
1908 	struct sbp_dev *sdev;
1909 	struct fw_xfer *xfer, *next;
1910 	int i, j;
1911 
1912 SBP_DEBUG(0)
1913 	printf("sbp_detach\n");
1914 END_DEBUG
1915 
1916 	for (i = 0; i < SBP_NUM_TARGETS; i ++)
1917 		sbp_cam_detach_target(&sbp->targets[i]);
1918 	xpt_free_path(sbp->path);
1919 	xpt_bus_deregister(cam_sim_path(sbp->sim));
1920 
1921 	sbp_logout_all(sbp);
1922 
1923 	/* XXX wait for logout completion */
1924 	tsleep(&i, FWPRI, "sbpdtc", hz/2);
1925 
1926 	for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
1927 		target = &sbp->targets[i];
1928 		if (target->luns == NULL)
1929 			continue;
1930 		callout_stop(&target->mgm_ocb_timeout);
1931 		for (j = 0; j < target->num_lun; j++) {
1932 			sdev = &target->luns[j];
1933 			if (sdev->status != SBP_DEV_DEAD) {
1934 				for (i = 0; i < SBP_QUEUE_LEN; i++)
1935 					bus_dmamap_destroy(sbp->dmat,
1936 						sdev->ocb[i].dmamap);
1937 				fwdma_free(sbp->fd.fc, &sdev->dma);
1938 			}
1939 		}
1940 		for (xfer = STAILQ_FIRST(&target->xferlist);
1941 				xfer != NULL; xfer = next) {
1942 			next = STAILQ_NEXT(xfer, link);
1943 			fw_xfer_free(xfer);
1944 		}
1945 		free(target->luns, M_SBP);
1946 	}
1947 
1948 	for (xfer = STAILQ_FIRST(&sbp->fwb.xferlist);
1949 				xfer != NULL; xfer = next) {
1950 		next = STAILQ_NEXT(xfer, link);
1951 		fw_xfer_free(xfer);
1952 	}
1953 	STAILQ_INIT(&sbp->fwb.xferlist);
1954 	fw_bindremove(fc, &sbp->fwb);
1955 
1956 	bus_dma_tag_destroy(sbp->dmat);
1957 
1958 	return (0);
1959 }
1960 
1961 static void
1962 sbp_cam_detach_target(struct sbp_target *target)
1963 {
1964 	struct sbp_dev *sdev;
1965 	int i;
1966 
1967 	if (target->luns != NULL) {
1968 SBP_DEBUG(0)
1969 		printf("sbp_detach_target %d\n", target->target_id);
1970 END_DEBUG
1971 		callout_stop(&target->scan_callout);
1972 		for (i = 0; i < target->num_lun; i++) {
1973 			sdev = &target->luns[i];
1974 			if (sdev->status == SBP_DEV_DEAD)
1975 				continue;
1976 			if (sdev->status == SBP_DEV_RESET)
1977 				continue;
1978 			if (sdev->path) {
1979 				xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
1980 				xpt_free_path(sdev->path);
1981 				sdev->path = NULL;
1982 			}
1983 			sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
1984 		}
1985 	}
1986 }
1987 
1988 static void
1989 sbp_timeout(void *arg)
1990 {
1991 	struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
1992 	struct sbp_dev *sdev = ocb->sdev;
1993 
1994 	sbp_show_sdev_info(sdev, 2);
1995 	printf("request timeout ... ");
1996 
1997 	if (ocb->flags == OCB_ACT_MGM) {
1998 		printf("management ORB\n");
1999 		/* XXX just ignore for now */
2000 		sdev->target->mgm_ocb_cur = NULL;
2001 		sbp_free_ocb(sdev, ocb);
2002 		sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2003 		return;
2004 	}
2005 
2006 	xpt_freeze_devq(sdev->path, 1);
2007 	sdev->freeze ++;
2008 	sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2009 	if (sdev->flags & SBP_DEV_TIMEOUT) {
2010 		printf("target reset\n");
2011 		sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2012 		sdev->flags &= ~SBP_DEV_TIMEOUT;
2013 	} else {
2014 		printf("agent reset\n");
2015 		sdev->flags |= SBP_DEV_TIMEOUT;
2016 		sbp_agent_reset(sdev);
2017 	}
2018 	return;
2019 }
2020 
2021 static void
2022 sbp_action1(struct cam_sim *sim, union ccb *ccb)
2023 {
2024 
2025 	struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2026 	struct sbp_target *target = NULL;
2027 	struct sbp_dev *sdev = NULL;
2028 
2029 	/* target:lun -> sdev mapping */
2030 	if (sbp != NULL
2031 			&& ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2032 			&& ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2033 		target = &sbp->targets[ccb->ccb_h.target_id];
2034 		if (target->fwdev != NULL
2035 				&& ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2036 				&& ccb->ccb_h.target_lun < target->num_lun) {
2037 			sdev = &target->luns[ccb->ccb_h.target_lun];
2038 			if (sdev->status != SBP_DEV_ATTACHED &&
2039 				sdev->status != SBP_DEV_PROBE)
2040 				sdev = NULL;
2041 		}
2042 	}
2043 
2044 SBP_DEBUG(1)
2045 	if (sdev == NULL)
2046 		printf("invalid target %d lun %d\n",
2047 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2048 END_DEBUG
2049 
2050 	switch (ccb->ccb_h.func_code) {
2051 	case XPT_SCSI_IO:
2052 	case XPT_RESET_DEV:
2053 	case XPT_GET_TRAN_SETTINGS:
2054 	case XPT_SET_TRAN_SETTINGS:
2055 	case XPT_CALC_GEOMETRY:
2056 		if (sdev == NULL) {
2057 SBP_DEBUG(1)
2058 			printf("%s:%d:%d:func_code 0x%04x: "
2059 				"Invalid target (target needed)\n",
2060 				device_get_nameunit(sbp->fd.dev),
2061 				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2062 				ccb->ccb_h.func_code);
2063 END_DEBUG
2064 
2065 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2066 			xpt_done(ccb);
2067 			return;
2068 		}
2069 		break;
2070 	case XPT_PATH_INQ:
2071 	case XPT_NOOP:
2072 		/* The opcodes sometimes aimed at a target (sc is valid),
2073 		 * sometimes aimed at the SIM (sc is invalid and target is
2074 		 * CAM_TARGET_WILDCARD)
2075 		 */
2076 		if (sbp == NULL &&
2077 			ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2078 SBP_DEBUG(0)
2079 			printf("%s:%d:%d func_code 0x%04x: "
2080 				"Invalid target (no wildcard)\n",
2081 				device_get_nameunit(sbp->fd.dev),
2082 				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2083 				ccb->ccb_h.func_code);
2084 END_DEBUG
2085 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2086 			xpt_done(ccb);
2087 			return;
2088 		}
2089 		break;
2090 	default:
2091 		/* XXX Hm, we should check the input parameters */
2092 		break;
2093 	}
2094 
2095 	switch (ccb->ccb_h.func_code) {
2096 	case XPT_SCSI_IO:
2097 	{
2098 		struct ccb_scsiio *csio;
2099 		struct sbp_ocb *ocb;
2100 		int speed;
2101 		void *cdb;
2102 
2103 		csio = &ccb->csio;
2104 
2105 SBP_DEBUG(1)
2106 		printf("%s:%d:%d XPT_SCSI_IO: "
2107 			"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2108 			", flags: 0x%02x, "
2109 			"%db cmd/%db data/%db sense\n",
2110 			device_get_nameunit(sbp->fd.dev),
2111 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2112 			csio->cdb_io.cdb_bytes[0],
2113 			csio->cdb_io.cdb_bytes[1],
2114 			csio->cdb_io.cdb_bytes[2],
2115 			csio->cdb_io.cdb_bytes[3],
2116 			csio->cdb_io.cdb_bytes[4],
2117 			csio->cdb_io.cdb_bytes[5],
2118 			csio->cdb_io.cdb_bytes[6],
2119 			csio->cdb_io.cdb_bytes[7],
2120 			csio->cdb_io.cdb_bytes[8],
2121 			csio->cdb_io.cdb_bytes[9],
2122 			ccb->ccb_h.flags & CAM_DIR_MASK,
2123 			csio->cdb_len, csio->dxfer_len,
2124 			csio->sense_len);
2125 END_DEBUG
2126 		if(sdev == NULL){
2127 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2128 			xpt_done(ccb);
2129 			return;
2130 		}
2131 #if 0
2132 		/* if we are in probe stage, pass only probe commands */
2133 		if (sdev->status == SBP_DEV_PROBE) {
2134 			char *name;
2135 			name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2136 			printf("probe stage, periph name: %s\n", name);
2137 			if (strcmp(name, "probe") != 0) {
2138 				ccb->ccb_h.status = CAM_REQUEUE_REQ;
2139 				xpt_done(ccb);
2140 				return;
2141 			}
2142 		}
2143 #endif
2144 		if ((ocb = sbp_get_ocb(sdev)) == NULL)
2145 			return;
2146 
2147 		ocb->flags = OCB_ACT_CMD;
2148 		ocb->sdev = sdev;
2149 		ocb->ccb = ccb;
2150 		ccb->ccb_h.ccb_sdev_ptr = sdev;
2151 		ocb->orb[0] = htonl(1 << 31);
2152 		ocb->orb[1] = 0;
2153 		ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2154 		ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2155 		speed = min(target->fwdev->speed, max_speed);
2156 		ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2157 						| ORB_CMD_MAXP(speed + 7));
2158 		if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
2159 			ocb->orb[4] |= htonl(ORB_CMD_IN);
2160 		}
2161 
2162 		if (csio->ccb_h.flags & CAM_SCATTER_VALID)
2163 			printf("sbp: CAM_SCATTER_VALID\n");
2164 		if (csio->ccb_h.flags & CAM_DATA_PHYS)
2165 			printf("sbp: CAM_DATA_PHYS\n");
2166 
2167 		if (csio->ccb_h.flags & CAM_CDB_POINTER)
2168 			cdb = (void *)csio->cdb_io.cdb_ptr;
2169 		else
2170 			cdb = (void *)&csio->cdb_io.cdb_bytes;
2171 		bcopy(cdb,
2172 			(void *)(uintptr_t)(volatile void *)&ocb->orb[5],
2173 				csio->cdb_len);
2174 /*
2175 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2176 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2177 */
2178 		if (ccb->csio.dxfer_len > 0) {
2179 			int s, error;
2180 
2181 			s = splsoftvm();
2182 			error = bus_dmamap_load(/*dma tag*/sbp->dmat,
2183 					/*dma map*/ocb->dmamap,
2184 					ccb->csio.data_ptr,
2185 					ccb->csio.dxfer_len,
2186 					sbp_execute_ocb,
2187 					ocb,
2188 					/*flags*/0);
2189 			splx(s);
2190 			if (error)
2191 				printf("sbp: bus_dmamap_load error %d\n", error);
2192 		} else
2193 			sbp_execute_ocb(ocb, NULL, 0, 0);
2194 		break;
2195 	}
2196 	case XPT_CALC_GEOMETRY:
2197 	{
2198 		struct ccb_calc_geometry *ccg;
2199 		u_int32_t size_mb;
2200 		u_int32_t secs_per_cylinder;
2201 		int extended = 1;
2202 		ccg = &ccb->ccg;
2203 
2204 		if (ccg->block_size == 0) {
2205 			printf("sbp_action1: block_size is 0.\n");
2206 			ccb->ccb_h.status = CAM_REQ_INVALID;
2207 			xpt_done(ccb);
2208 			break;
2209 		}
2210 SBP_DEBUG(1)
2211 		printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2212 			"Volume size = %d\n",
2213 			device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim),
2214 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2215 			ccg->volume_size);
2216 END_DEBUG
2217 
2218 		size_mb = ccg->volume_size
2219 			/ ((1024L * 1024L) / ccg->block_size);
2220 
2221 		if (size_mb >= 1024 && extended) {
2222 			ccg->heads = 255;
2223 			ccg->secs_per_track = 63;
2224 		} else {
2225 			ccg->heads = 64;
2226 			ccg->secs_per_track = 32;
2227 		}
2228 		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2229 		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2230 		ccb->ccb_h.status = CAM_REQ_CMP;
2231 		xpt_done(ccb);
2232 		break;
2233 	}
2234 	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
2235 	{
2236 
2237 SBP_DEBUG(1)
2238 		printf("%s:%d:XPT_RESET_BUS: \n",
2239 			device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2240 END_DEBUG
2241 
2242 		ccb->ccb_h.status = CAM_REQ_INVALID;
2243 		xpt_done(ccb);
2244 		break;
2245 	}
2246 	case XPT_PATH_INQ:		/* Path routing inquiry */
2247 	{
2248 		struct ccb_pathinq *cpi = &ccb->cpi;
2249 
2250 SBP_DEBUG(1)
2251 		printf("%s:%d:%d XPT_PATH_INQ:.\n",
2252 			device_get_nameunit(sbp->fd.dev),
2253 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2254 END_DEBUG
2255 		cpi->version_num = 1; /* XXX??? */
2256 		cpi->hba_inquiry = PI_TAG_ABLE;
2257 		cpi->target_sprt = 0;
2258 		cpi->hba_misc = PIM_NOBUSRESET;
2259 		cpi->hba_eng_cnt = 0;
2260 		cpi->max_target = SBP_NUM_TARGETS - 1;
2261 		cpi->max_lun = SBP_NUM_LUNS - 1;
2262 		cpi->initiator_id = SBP_INITIATOR;
2263 		cpi->bus_id = sim->bus_id;
2264 		cpi->base_transfer_speed = 400 * 1000 / 8;
2265 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2266 		strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2267 		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2268 		cpi->unit_number = sim->unit_number;
2269 
2270 		cpi->ccb_h.status = CAM_REQ_CMP;
2271 		xpt_done(ccb);
2272 		break;
2273 	}
2274 	case XPT_GET_TRAN_SETTINGS:
2275 	{
2276 		struct ccb_trans_settings *cts = &ccb->cts;
2277 SBP_DEBUG(1)
2278 		printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2279 			device_get_nameunit(sbp->fd.dev),
2280 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2281 END_DEBUG
2282 		/* Enable disconnect and tagged queuing */
2283 		cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2284 		cts->flags = CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB;
2285 
2286 		cts->ccb_h.status = CAM_REQ_CMP;
2287 		xpt_done(ccb);
2288 		break;
2289 	}
2290 	case XPT_ABORT:
2291 		ccb->ccb_h.status = CAM_UA_ABORT;
2292 		xpt_done(ccb);
2293 		break;
2294 	case XPT_SET_TRAN_SETTINGS:
2295 		/* XXX */
2296 	default:
2297 		ccb->ccb_h.status = CAM_REQ_INVALID;
2298 		xpt_done(ccb);
2299 		break;
2300 	}
2301 	return;
2302 }
2303 
2304 static void
2305 sbp_action(struct cam_sim *sim, union ccb *ccb)
2306 {
2307 	int s;
2308 
2309 	s = splfw();
2310 	sbp_action1(sim, ccb);
2311 	splx(s);
2312 }
2313 
2314 static void
2315 sbp_execute_ocb(void *arg,  bus_dma_segment_t *segments, int seg, int error)
2316 {
2317 	int i;
2318 	struct sbp_ocb *ocb;
2319 	struct sbp_ocb *prev;
2320 	bus_dma_segment_t *s;
2321 
2322 	if (error)
2323 		printf("sbp_execute_ocb: error=%d\n", error);
2324 
2325 	ocb = (struct sbp_ocb *)arg;
2326 
2327 SBP_DEBUG(1)
2328 	printf("sbp_execute_ocb: seg %d", seg);
2329 	for (i = 0; i < seg; i++)
2330 #if __FreeBSD_version >= 500000
2331 		printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2332 					(uintmax_t)segments[i].ds_len);
2333 #else
2334 		printf(", %x:%d", segments[i].ds_addr, segments[i].ds_len);
2335 #endif
2336 	printf("\n");
2337 END_DEBUG
2338 
2339 	if (seg == 1) {
2340 		/* direct pointer */
2341 		s = &segments[0];
2342 		if (s->ds_len > SBP_SEG_MAX)
2343 			panic("ds_len > SBP_SEG_MAX, fix busdma code");
2344 		ocb->orb[3] = htonl(s->ds_addr);
2345 		ocb->orb[4] |= htonl(s->ds_len);
2346 	} else if(seg > 1) {
2347 		/* page table */
2348 		for (i = 0; i < seg; i++) {
2349 			s = &segments[i];
2350 SBP_DEBUG(0)
2351 			/* XXX LSI Logic "< 16 byte" bug might be hit */
2352 			if (s->ds_len < 16)
2353 				printf("sbp_execute_ocb: warning, "
2354 #if __FreeBSD_version >= 500000
2355 					"segment length(%zd) is less than 16."
2356 #else
2357 					"segment length(%d) is less than 16."
2358 #endif
2359 					"(seg=%d/%d)\n", s->ds_len, i+1, seg);
2360 END_DEBUG
2361 			if (s->ds_len > SBP_SEG_MAX)
2362 				panic("ds_len > SBP_SEG_MAX, fix busdma code");
2363 			ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2364 			ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2365 		}
2366 		ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2367 	}
2368 
2369 	if (seg > 0)
2370 		bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2371 			(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2372 			BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2373 	prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2374 	fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2375 	if (prev == NULL)
2376 		sbp_orb_pointer(ocb->sdev, ocb);
2377 }
2378 
2379 static void
2380 sbp_poll(struct cam_sim *sim)
2381 {
2382 	/* should call fwohci_intr? */
2383 	return;
2384 }
2385 static struct sbp_ocb *
2386 sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2387 {
2388 	struct sbp_ocb *ocb;
2389 	struct sbp_ocb *next;
2390 	int s = splfw(), order = 0;
2391 	int flags;
2392 
2393 	for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2394 		next = STAILQ_NEXT(ocb, ocb);
2395 		flags = ocb->flags;
2396 SBP_DEBUG(1)
2397 		sbp_show_sdev_info(sdev, 2);
2398 #if __FreeBSD_version >= 500000
2399 		printf("orb: 0x%jx next: 0x%x, flags %x\n",
2400 			(uintmax_t)ocb->bus_addr,
2401 #else
2402 		printf("orb: 0x%x next: 0x%lx, flags %x\n",
2403 			ocb->bus_addr,
2404 #endif
2405 			ntohl(ocb->orb[1]), flags);
2406 END_DEBUG
2407 		if (OCB_MATCH(ocb, sbp_status)) {
2408 			/* found */
2409 			STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2410 			if (ocb->ccb != NULL)
2411 				untimeout(sbp_timeout, (caddr_t)ocb,
2412 						ocb->ccb->ccb_h.timeout_ch);
2413 			if (ntohl(ocb->orb[4]) & 0xffff) {
2414 				bus_dmamap_sync(sdev->target->sbp->dmat,
2415 					ocb->dmamap,
2416 					(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2417 					BUS_DMASYNC_POSTREAD :
2418 					BUS_DMASYNC_POSTWRITE);
2419 				bus_dmamap_unload(sdev->target->sbp->dmat,
2420 					ocb->dmamap);
2421 			}
2422 			if (next != NULL && sbp_status->src == 1)
2423 				sbp_orb_pointer(sdev, next);
2424 			break;
2425 		} else
2426 			order ++;
2427 	}
2428 	splx(s);
2429 SBP_DEBUG(0)
2430 	if (ocb && order > 0) {
2431 		sbp_show_sdev_info(sdev, 2);
2432 		printf("unordered execution order:%d\n", order);
2433 	}
2434 END_DEBUG
2435 	return (ocb);
2436 }
2437 
2438 static struct sbp_ocb *
2439 sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2440 {
2441 	int s = splfw();
2442 	struct sbp_ocb *prev;
2443 
2444 SBP_DEBUG(2)
2445 	sbp_show_sdev_info(sdev, 2);
2446 #if __FreeBSD_version >= 500000
2447 	printf("sbp_enqueue_ocb orb=0x%jx in physical memory\n",
2448 		(uintmax_t)ocb->bus_addr);
2449 #else
2450 	printf("sbp_enqueue_ocb orb=0x%x in physical memory\n", ocb->bus_addr);
2451 #endif
2452 END_DEBUG
2453 	prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2454 	STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2455 
2456 	if (ocb->ccb != NULL)
2457 		ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb,
2458 					(ocb->ccb->ccb_h.timeout * hz) / 1000);
2459 
2460 	if (prev != NULL ) {
2461 SBP_DEBUG(1)
2462 #if __FreeBSD_version >= 500000
2463 	printf("linking chain 0x%jx -> 0x%jx\n",
2464 		(uintmax_t)prev->bus_addr, (uintmax_t)ocb->bus_addr);
2465 #else
2466 	printf("linking chain 0x%x -> 0x%x\n", prev->bus_addr, ocb->bus_addr);
2467 #endif
2468 END_DEBUG
2469 		prev->orb[1] = htonl(ocb->bus_addr);
2470 		prev->orb[0] = 0;
2471 	}
2472 	splx(s);
2473 
2474 	return prev;
2475 }
2476 
2477 static struct sbp_ocb *
2478 sbp_get_ocb(struct sbp_dev *sdev)
2479 {
2480 	struct sbp_ocb *ocb;
2481 	int s = splfw();
2482 	ocb = STAILQ_FIRST(&sdev->free_ocbs);
2483 	if (ocb == NULL) {
2484 		printf("ocb shortage!!!\n");
2485 		return NULL;
2486 	}
2487 	STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2488 	splx(s);
2489 	ocb->ccb = NULL;
2490 	return (ocb);
2491 }
2492 
2493 static void
2494 sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2495 {
2496 	ocb->flags = 0;
2497 	ocb->ccb = NULL;
2498 	STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2499 }
2500 
2501 static void
2502 sbp_abort_ocb(struct sbp_ocb *ocb, int status)
2503 {
2504 	struct sbp_dev *sdev;
2505 
2506 	sdev = ocb->sdev;
2507 SBP_DEBUG(0)
2508 	sbp_show_sdev_info(sdev, 2);
2509 #if __FreeBSD_version >= 500000
2510 	printf("sbp_abort_ocb 0x%jx\n", (uintmax_t)ocb->bus_addr);
2511 #else
2512 	printf("sbp_abort_ocb 0x%x\n", ocb->bus_addr);
2513 #endif
2514 END_DEBUG
2515 SBP_DEBUG(1)
2516 	if (ocb->ccb != NULL)
2517 		sbp_print_scsi_cmd(ocb);
2518 END_DEBUG
2519 	if (ntohl(ocb->orb[4]) & 0xffff) {
2520 		bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
2521 			(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2522 			BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2523 		bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
2524 	}
2525 	if (ocb->ccb != NULL) {
2526 		untimeout(sbp_timeout, (caddr_t)ocb,
2527 					ocb->ccb->ccb_h.timeout_ch);
2528 		ocb->ccb->ccb_h.status = status;
2529 		xpt_done(ocb->ccb);
2530 	}
2531 	sbp_free_ocb(sdev, ocb);
2532 }
2533 
2534 static void
2535 sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2536 {
2537 	int s;
2538 	struct sbp_ocb *ocb, *next;
2539 	STAILQ_HEAD(, sbp_ocb) temp;
2540 
2541 	s = splfw();
2542 
2543 	bcopy(&sdev->ocbs, &temp, sizeof(temp));
2544 	STAILQ_INIT(&sdev->ocbs);
2545 	for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2546 		next = STAILQ_NEXT(ocb, ocb);
2547 		sbp_abort_ocb(ocb, status);
2548 	}
2549 
2550 	splx(s);
2551 }
2552 
2553 static devclass_t sbp_devclass;
2554 
2555 static device_method_t sbp_methods[] = {
2556 	/* device interface */
2557 	DEVMETHOD(device_identify,	sbp_identify),
2558 	DEVMETHOD(device_probe,		sbp_probe),
2559 	DEVMETHOD(device_attach,	sbp_attach),
2560 	DEVMETHOD(device_detach,	sbp_detach),
2561 	DEVMETHOD(device_shutdown,	sbp_shutdown),
2562 
2563 	{ 0, 0 }
2564 };
2565 
2566 static driver_t sbp_driver = {
2567 	"sbp",
2568 	sbp_methods,
2569 	sizeof(struct sbp_softc),
2570 };
2571 DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2572 MODULE_VERSION(sbp, 1);
2573 MODULE_DEPEND(sbp, firewire, 1, 1, 1);
2574 MODULE_DEPEND(sbp, cam, 1, 1, 1);
2575