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