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