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