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