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