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