sbp.c revision 124169
1/*
2 * Copyright (c) 2003 Hidetoshi Shimokawa
3 * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the acknowledgement as bellow:
16 *
17 *    This product includes software developed by K. Kobayashi and H. Shimokawa
18 *
19 * 4. The name of the author may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 * $FreeBSD: head/sys/dev/firewire/sbp.c 124169 2004-01-06 14:30: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/*
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 = 2;
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	error = bus_dma_tag_create(/*parent*/sbp->fd.fc->dmat,
1883				/* XXX shoud be 4 for sane backend? */
1884				/*alignment*/1,
1885				/*boundary*/0,
1886				/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1887				/*highaddr*/BUS_SPACE_MAXADDR,
1888				/*filter*/NULL, /*filterarg*/NULL,
1889				/*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1890				/*maxsegsz*/SBP_SEG_MAX,
1891				/*flags*/BUS_DMA_ALLOCNOW,
1892#if __FreeBSD_version >= 501102
1893				/*lockfunc*/busdma_lock_mutex,
1894				/*lockarg*/&Giant,
1895#endif
1896				&sbp->dmat);
1897	if (error != 0) {
1898		printf("sbp_attach: Could not allocate DMA tag "
1899			"- error %d\n", error);
1900			return (ENOMEM);
1901	}
1902
1903	devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1904	if (devq == NULL)
1905		return (ENXIO);
1906
1907	for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1908		sbp->targets[i].fwdev = NULL;
1909		sbp->targets[i].luns = NULL;
1910	}
1911
1912	sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1913				 device_get_unit(dev),
1914				 /*untagged*/ 1,
1915				 /*tagged*/ SBP_QUEUE_LEN - 1,
1916				 devq);
1917
1918	if (sbp->sim == NULL) {
1919		cam_simq_free(devq);
1920		return (ENXIO);
1921	}
1922
1923
1924	if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS)
1925		goto fail;
1926
1927	if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim),
1928	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1929		xpt_bus_deregister(cam_sim_path(sbp->sim));
1930		goto fail;
1931	}
1932
1933	/* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1934	sbp->fwb.start = ((u_int64_t)SBP_BIND_HI << 32) | SBP_DEV2ADDR(0, 0);
1935	sbp->fwb.end = sbp->fwb.start + 0xffff;
1936	sbp->fwb.act_type = FWACT_XFER;
1937	/* pre-allocate xfer */
1938	STAILQ_INIT(&sbp->fwb.xferlist);
1939	for (i = 0; i < SBP_NUM_OCB/2; i ++) {
1940		xfer = fw_xfer_alloc_buf(M_SBP,
1941			/* send */0,
1942			/* recv */SBP_RECV_LEN);
1943		xfer->act.hand = sbp_recv;
1944#if NEED_RESPONSE
1945		xfer->fc = sbp->fd.fc;
1946#endif
1947		xfer->sc = (caddr_t)sbp;
1948		STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1949	}
1950	fw_bindadd(sbp->fd.fc, &sbp->fwb);
1951
1952	sbp->fd.post_busreset = sbp_post_busreset;
1953	sbp->fd.post_explore = sbp_post_explore;
1954
1955	if (sbp->fd.fc->status != -1) {
1956		s = splfw();
1957		sbp_post_busreset((void *)sbp);
1958		sbp_post_explore((void *)sbp);
1959		splx(s);
1960	}
1961	xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
1962
1963	return (0);
1964fail:
1965	cam_sim_free(sbp->sim, /*free_devq*/TRUE);
1966	return (ENXIO);
1967}
1968
1969static int
1970sbp_logout_all(struct sbp_softc *sbp)
1971{
1972	struct sbp_target *target;
1973	struct sbp_dev *sdev;
1974	int i, j;
1975
1976SBP_DEBUG(0)
1977	printf("sbp_logout_all\n");
1978END_DEBUG
1979	for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
1980		target = &sbp->targets[i];
1981		if (target->luns == NULL)
1982			continue;
1983		for (j = 0; j < target->num_lun; j++) {
1984			sdev = target->luns[j];
1985			if (sdev == NULL)
1986				continue;
1987			callout_stop(&sdev->login_callout);
1988			if (sdev->status >= SBP_DEV_TOATTACH &&
1989					sdev->status <= SBP_DEV_ATTACHED)
1990				sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
1991		}
1992	}
1993
1994	return 0;
1995}
1996
1997static int
1998sbp_shutdown(device_t dev)
1999{
2000	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2001
2002	sbp_logout_all(sbp);
2003	return (0);
2004}
2005
2006static void
2007sbp_free_sdev(struct sbp_dev *sdev)
2008{
2009	int i;
2010
2011	if (sdev == NULL)
2012		return;
2013	for (i = 0; i < SBP_QUEUE_LEN; i++)
2014		bus_dmamap_destroy(sdev->target->sbp->dmat,
2015		    sdev->ocb[i].dmamap);
2016	fwdma_free(sdev->target->sbp->fd.fc, &sdev->dma);
2017	free(sdev, M_SBP);
2018}
2019
2020static void
2021sbp_free_target(struct sbp_target *target)
2022{
2023	struct sbp_softc *sbp;
2024	struct fw_xfer *xfer, *next;
2025	int i;
2026
2027	if (target->luns == NULL)
2028		return;
2029	callout_stop(&target->mgm_ocb_timeout);
2030	sbp = target->sbp;
2031	for (i = 0; i < target->num_lun; i++)
2032		sbp_free_sdev(target->luns[i]);
2033
2034	for (xfer = STAILQ_FIRST(&target->xferlist);
2035			xfer != NULL; xfer = next) {
2036		next = STAILQ_NEXT(xfer, link);
2037		fw_xfer_free_buf(xfer);
2038	}
2039	STAILQ_INIT(&target->xferlist);
2040	free(target->luns, M_SBP);
2041	target->num_lun = 0;;
2042	target->luns = NULL;
2043	target->fwdev = NULL;
2044}
2045
2046static int
2047sbp_detach(device_t dev)
2048{
2049	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2050	struct firewire_comm *fc = sbp->fd.fc;
2051	struct fw_xfer *xfer, *next;
2052	int i;
2053
2054SBP_DEBUG(0)
2055	printf("sbp_detach\n");
2056END_DEBUG
2057
2058	for (i = 0; i < SBP_NUM_TARGETS; i ++)
2059		sbp_cam_detach_target(&sbp->targets[i]);
2060	xpt_async(AC_LOST_DEVICE, sbp->path, NULL);
2061	xpt_free_path(sbp->path);
2062	xpt_bus_deregister(cam_sim_path(sbp->sim));
2063	cam_sim_free(sbp->sim, /*free_devq*/ TRUE),
2064
2065	sbp_logout_all(sbp);
2066
2067	/* XXX wait for logout completion */
2068	tsleep(&i, FWPRI, "sbpdtc", hz/2);
2069
2070	for (i = 0 ; i < SBP_NUM_TARGETS ; i ++)
2071		sbp_free_target(&sbp->targets[i]);
2072
2073	for (xfer = STAILQ_FIRST(&sbp->fwb.xferlist);
2074				xfer != NULL; xfer = next) {
2075		next = STAILQ_NEXT(xfer, link);
2076		fw_xfer_free_buf(xfer);
2077	}
2078	STAILQ_INIT(&sbp->fwb.xferlist);
2079	fw_bindremove(fc, &sbp->fwb);
2080
2081	bus_dma_tag_destroy(sbp->dmat);
2082
2083	return (0);
2084}
2085
2086static void
2087sbp_cam_detach_sdev(struct sbp_dev *sdev)
2088{
2089	if (sdev == NULL)
2090		return;
2091	if (sdev->status == SBP_DEV_DEAD)
2092		return;
2093	if (sdev->status == SBP_DEV_RESET)
2094		return;
2095	if (sdev->path) {
2096		xpt_release_devq(sdev->path,
2097				 sdev->freeze, TRUE);
2098		sdev->freeze = 0;
2099		xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
2100		xpt_free_path(sdev->path);
2101		sdev->path = NULL;
2102	}
2103	sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
2104}
2105
2106static void
2107sbp_cam_detach_target(struct sbp_target *target)
2108{
2109	int i;
2110
2111	if (target->luns != NULL) {
2112SBP_DEBUG(0)
2113		printf("sbp_detach_target %d\n", target->target_id);
2114END_DEBUG
2115		callout_stop(&target->scan_callout);
2116		for (i = 0; i < target->num_lun; i++)
2117			sbp_cam_detach_sdev(target->luns[i]);
2118	}
2119}
2120
2121static void
2122sbp_target_reset(struct sbp_dev *sdev, int method)
2123{
2124	int i;
2125	struct sbp_target *target = sdev->target;
2126	struct sbp_dev *tsdev;
2127
2128	for (i = 0; i < target->num_lun; i++) {
2129		tsdev = target->luns[i];
2130		if (tsdev == NULL)
2131			continue;
2132		if (tsdev->status == SBP_DEV_DEAD)
2133			continue;
2134		if (tsdev->status == SBP_DEV_RESET)
2135			continue;
2136		xpt_freeze_devq(tsdev->path, 1);
2137		tsdev->freeze ++;
2138		sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT);
2139		if (method == 2)
2140			tsdev->status = SBP_DEV_LOGIN;
2141	}
2142	switch(method) {
2143	case 1:
2144		printf("target reset\n");
2145		sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2146		break;
2147	case 2:
2148		printf("reset start\n");
2149		sbp_reset_start(sdev);
2150		break;
2151	}
2152
2153}
2154
2155static void
2156sbp_mgm_timeout(void *arg)
2157{
2158	struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2159	struct sbp_dev *sdev = ocb->sdev;
2160	struct sbp_target *target = sdev->target;
2161
2162	sbp_show_sdev_info(sdev, 2);
2163	printf("request timeout(mgm orb:0x%08x) ... ",
2164	    (u_int32_t)ocb->bus_addr);
2165	target->mgm_ocb_cur = NULL;
2166	sbp_free_ocb(sdev, ocb);
2167#if 0
2168	/* XXX */
2169	printf("run next request\n");
2170	sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2171#endif
2172#if 1
2173	printf("reset start\n");
2174	sbp_reset_start(sdev);
2175#endif
2176}
2177
2178static void
2179sbp_timeout(void *arg)
2180{
2181	struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2182	struct sbp_dev *sdev = ocb->sdev;
2183
2184	sbp_show_sdev_info(sdev, 2);
2185	printf("request timeout(cmd orb:0x%08x) ... ",
2186	    (u_int32_t)ocb->bus_addr);
2187
2188	sdev->timeout ++;
2189	switch(sdev->timeout) {
2190	case 1:
2191		printf("agent reset\n");
2192		xpt_freeze_devq(sdev->path, 1);
2193		sdev->freeze ++;
2194		sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2195		sbp_agent_reset(sdev);
2196		break;
2197	case 2:
2198	case 3:
2199		sbp_target_reset(sdev, sdev->timeout - 1);
2200		break;
2201#if 0
2202	default:
2203		/* XXX give up */
2204		sbp_cam_detach_target(target);
2205		if (target->luns != NULL)
2206			free(target->luns, M_SBP);
2207		target->num_lun = 0;;
2208		target->luns = NULL;
2209		target->fwdev = NULL;
2210#endif
2211	}
2212}
2213
2214static void
2215sbp_action1(struct cam_sim *sim, union ccb *ccb)
2216{
2217
2218	struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2219	struct sbp_target *target = NULL;
2220	struct sbp_dev *sdev = NULL;
2221
2222	/* target:lun -> sdev mapping */
2223	if (sbp != NULL
2224			&& ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2225			&& ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2226		target = &sbp->targets[ccb->ccb_h.target_id];
2227		if (target->fwdev != NULL
2228				&& ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2229				&& ccb->ccb_h.target_lun < target->num_lun) {
2230			sdev = target->luns[ccb->ccb_h.target_lun];
2231			if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED &&
2232				sdev->status != SBP_DEV_PROBE)
2233				sdev = NULL;
2234		}
2235	}
2236
2237SBP_DEBUG(1)
2238	if (sdev == NULL)
2239		printf("invalid target %d lun %d\n",
2240			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2241END_DEBUG
2242
2243	switch (ccb->ccb_h.func_code) {
2244	case XPT_SCSI_IO:
2245	case XPT_RESET_DEV:
2246	case XPT_GET_TRAN_SETTINGS:
2247	case XPT_SET_TRAN_SETTINGS:
2248	case XPT_CALC_GEOMETRY:
2249		if (sdev == NULL) {
2250SBP_DEBUG(1)
2251			printf("%s:%d:%d:func_code 0x%04x: "
2252				"Invalid target (target needed)\n",
2253				device_get_nameunit(sbp->fd.dev),
2254				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2255				ccb->ccb_h.func_code);
2256END_DEBUG
2257
2258			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2259			xpt_done(ccb);
2260			return;
2261		}
2262		break;
2263	case XPT_PATH_INQ:
2264	case XPT_NOOP:
2265		/* The opcodes sometimes aimed at a target (sc is valid),
2266		 * sometimes aimed at the SIM (sc is invalid and target is
2267		 * CAM_TARGET_WILDCARD)
2268		 */
2269		if (sbp == NULL &&
2270			ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2271SBP_DEBUG(0)
2272			printf("%s:%d:%d func_code 0x%04x: "
2273				"Invalid target (no wildcard)\n",
2274				device_get_nameunit(sbp->fd.dev),
2275				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2276				ccb->ccb_h.func_code);
2277END_DEBUG
2278			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2279			xpt_done(ccb);
2280			return;
2281		}
2282		break;
2283	default:
2284		/* XXX Hm, we should check the input parameters */
2285		break;
2286	}
2287
2288	switch (ccb->ccb_h.func_code) {
2289	case XPT_SCSI_IO:
2290	{
2291		struct ccb_scsiio *csio;
2292		struct sbp_ocb *ocb;
2293		int speed;
2294		void *cdb;
2295
2296		csio = &ccb->csio;
2297
2298SBP_DEBUG(2)
2299		printf("%s:%d:%d XPT_SCSI_IO: "
2300			"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2301			", flags: 0x%02x, "
2302			"%db cmd/%db data/%db sense\n",
2303			device_get_nameunit(sbp->fd.dev),
2304			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2305			csio->cdb_io.cdb_bytes[0],
2306			csio->cdb_io.cdb_bytes[1],
2307			csio->cdb_io.cdb_bytes[2],
2308			csio->cdb_io.cdb_bytes[3],
2309			csio->cdb_io.cdb_bytes[4],
2310			csio->cdb_io.cdb_bytes[5],
2311			csio->cdb_io.cdb_bytes[6],
2312			csio->cdb_io.cdb_bytes[7],
2313			csio->cdb_io.cdb_bytes[8],
2314			csio->cdb_io.cdb_bytes[9],
2315			ccb->ccb_h.flags & CAM_DIR_MASK,
2316			csio->cdb_len, csio->dxfer_len,
2317			csio->sense_len);
2318END_DEBUG
2319		if(sdev == NULL){
2320			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2321			xpt_done(ccb);
2322			return;
2323		}
2324#if 0
2325		/* if we are in probe stage, pass only probe commands */
2326		if (sdev->status == SBP_DEV_PROBE) {
2327			char *name;
2328			name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2329			printf("probe stage, periph name: %s\n", name);
2330			if (strcmp(name, "probe") != 0) {
2331				ccb->ccb_h.status = CAM_REQUEUE_REQ;
2332				xpt_done(ccb);
2333				return;
2334			}
2335		}
2336#endif
2337		if ((ocb = sbp_get_ocb(sdev)) == NULL) {
2338			ccb->ccb_h.status = CAM_REQUEUE_REQ;
2339			xpt_done(ccb);
2340			return;
2341		}
2342
2343		ocb->flags = OCB_ACT_CMD;
2344		ocb->sdev = sdev;
2345		ocb->ccb = ccb;
2346		ccb->ccb_h.ccb_sdev_ptr = sdev;
2347		ocb->orb[0] = htonl(1 << 31);
2348		ocb->orb[1] = 0;
2349		ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2350		ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2351		speed = min(target->fwdev->speed, max_speed);
2352		ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2353						| ORB_CMD_MAXP(speed + 7));
2354		if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
2355			ocb->orb[4] |= htonl(ORB_CMD_IN);
2356		}
2357
2358		if (csio->ccb_h.flags & CAM_SCATTER_VALID)
2359			printf("sbp: CAM_SCATTER_VALID\n");
2360		if (csio->ccb_h.flags & CAM_DATA_PHYS)
2361			printf("sbp: CAM_DATA_PHYS\n");
2362
2363		if (csio->ccb_h.flags & CAM_CDB_POINTER)
2364			cdb = (void *)csio->cdb_io.cdb_ptr;
2365		else
2366			cdb = (void *)&csio->cdb_io.cdb_bytes;
2367		bcopy(cdb, (void *)&ocb->orb[5], csio->cdb_len);
2368/*
2369printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2370printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2371*/
2372		if (ccb->csio.dxfer_len > 0) {
2373			int s, error;
2374
2375			s = splsoftvm();
2376			error = bus_dmamap_load(/*dma tag*/sbp->dmat,
2377					/*dma map*/ocb->dmamap,
2378					ccb->csio.data_ptr,
2379					ccb->csio.dxfer_len,
2380					sbp_execute_ocb,
2381					ocb,
2382					/*flags*/0);
2383			splx(s);
2384			if (error)
2385				printf("sbp: bus_dmamap_load error %d\n", error);
2386		} else
2387			sbp_execute_ocb(ocb, NULL, 0, 0);
2388		break;
2389	}
2390	case XPT_CALC_GEOMETRY:
2391	{
2392		struct ccb_calc_geometry *ccg;
2393#if __FreeBSD_version < 501100
2394		u_int32_t size_mb;
2395		u_int32_t secs_per_cylinder;
2396		int extended = 1;
2397#endif
2398
2399		ccg = &ccb->ccg;
2400		if (ccg->block_size == 0) {
2401			printf("sbp_action1: block_size is 0.\n");
2402			ccb->ccb_h.status = CAM_REQ_INVALID;
2403			xpt_done(ccb);
2404			break;
2405		}
2406SBP_DEBUG(1)
2407		printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2408#if __FreeBSD_version >= 500000
2409			"Volume size = %jd\n",
2410#else
2411			"Volume size = %d\n",
2412#endif
2413			device_get_nameunit(sbp->fd.dev),
2414			cam_sim_path(sbp->sim),
2415			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2416#if __FreeBSD_version >= 500000
2417			(uintmax_t)
2418#endif
2419				ccg->volume_size);
2420END_DEBUG
2421
2422#if __FreeBSD_version < 501100
2423		size_mb = ccg->volume_size
2424			/ ((1024L * 1024L) / ccg->block_size);
2425
2426		if (size_mb > 1024 && extended) {
2427			ccg->heads = 255;
2428			ccg->secs_per_track = 63;
2429		} else {
2430			ccg->heads = 64;
2431			ccg->secs_per_track = 32;
2432		}
2433		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2434		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2435		ccb->ccb_h.status = CAM_REQ_CMP;
2436#else
2437		cam_calc_geometry(ccg, /*extended*/1);
2438#endif
2439		xpt_done(ccb);
2440		break;
2441	}
2442	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
2443	{
2444
2445SBP_DEBUG(1)
2446		printf("%s:%d:XPT_RESET_BUS: \n",
2447			device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2448END_DEBUG
2449
2450		ccb->ccb_h.status = CAM_REQ_INVALID;
2451		xpt_done(ccb);
2452		break;
2453	}
2454	case XPT_PATH_INQ:		/* Path routing inquiry */
2455	{
2456		struct ccb_pathinq *cpi = &ccb->cpi;
2457
2458SBP_DEBUG(1)
2459		printf("%s:%d:%d XPT_PATH_INQ:.\n",
2460			device_get_nameunit(sbp->fd.dev),
2461			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2462END_DEBUG
2463		cpi->version_num = 1; /* XXX??? */
2464		cpi->hba_inquiry = PI_TAG_ABLE;
2465		cpi->target_sprt = 0;
2466		cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE;
2467		cpi->hba_eng_cnt = 0;
2468		cpi->max_target = SBP_NUM_TARGETS - 1;
2469		cpi->max_lun = SBP_NUM_LUNS - 1;
2470		cpi->initiator_id = SBP_INITIATOR;
2471		cpi->bus_id = sim->bus_id;
2472		cpi->base_transfer_speed = 400 * 1000 / 8;
2473		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2474		strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2475		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2476		cpi->unit_number = sim->unit_number;
2477
2478		cpi->ccb_h.status = CAM_REQ_CMP;
2479		xpt_done(ccb);
2480		break;
2481	}
2482	case XPT_GET_TRAN_SETTINGS:
2483	{
2484		struct ccb_trans_settings *cts = &ccb->cts;
2485SBP_DEBUG(1)
2486		printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2487			device_get_nameunit(sbp->fd.dev),
2488			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2489END_DEBUG
2490		/* Enable disconnect and tagged queuing */
2491		cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2492		cts->flags = CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB;
2493
2494		cts->ccb_h.status = CAM_REQ_CMP;
2495		xpt_done(ccb);
2496		break;
2497	}
2498	case XPT_ABORT:
2499		ccb->ccb_h.status = CAM_UA_ABORT;
2500		xpt_done(ccb);
2501		break;
2502	case XPT_SET_TRAN_SETTINGS:
2503		/* XXX */
2504	default:
2505		ccb->ccb_h.status = CAM_REQ_INVALID;
2506		xpt_done(ccb);
2507		break;
2508	}
2509	return;
2510}
2511
2512static void
2513sbp_action(struct cam_sim *sim, union ccb *ccb)
2514{
2515	int s;
2516
2517	s = splfw();
2518	sbp_action1(sim, ccb);
2519	splx(s);
2520}
2521
2522static void
2523sbp_execute_ocb(void *arg,  bus_dma_segment_t *segments, int seg, int error)
2524{
2525	int i;
2526	struct sbp_ocb *ocb;
2527	struct sbp_ocb *prev;
2528	bus_dma_segment_t *s;
2529
2530	if (error)
2531		printf("sbp_execute_ocb: error=%d\n", error);
2532
2533	ocb = (struct sbp_ocb *)arg;
2534
2535SBP_DEBUG(2)
2536	printf("sbp_execute_ocb: seg %d", seg);
2537	for (i = 0; i < seg; i++)
2538#if __FreeBSD_version >= 500000
2539		printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2540					(uintmax_t)segments[i].ds_len);
2541#else
2542		printf(", %x:%d", segments[i].ds_addr, segments[i].ds_len);
2543#endif
2544	printf("\n");
2545END_DEBUG
2546
2547	if (seg == 1) {
2548		/* direct pointer */
2549		s = &segments[0];
2550		if (s->ds_len > SBP_SEG_MAX)
2551			panic("ds_len > SBP_SEG_MAX, fix busdma code");
2552		ocb->orb[3] = htonl(s->ds_addr);
2553		ocb->orb[4] |= htonl(s->ds_len);
2554	} else if(seg > 1) {
2555		/* page table */
2556		for (i = 0; i < seg; i++) {
2557			s = &segments[i];
2558SBP_DEBUG(0)
2559			/* XXX LSI Logic "< 16 byte" bug might be hit */
2560			if (s->ds_len < 16)
2561				printf("sbp_execute_ocb: warning, "
2562#if __FreeBSD_version >= 500000
2563					"segment length(%zd) is less than 16."
2564#else
2565					"segment length(%d) is less than 16."
2566#endif
2567					"(seg=%d/%d)\n", s->ds_len, i+1, seg);
2568END_DEBUG
2569			if (s->ds_len > SBP_SEG_MAX)
2570				panic("ds_len > SBP_SEG_MAX, fix busdma code");
2571			ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2572			ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2573		}
2574		ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2575	}
2576
2577	if (seg > 0)
2578		bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2579			(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2580			BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2581	prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2582	fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2583	if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) {
2584		ocb->sdev->flags &= ~ORB_LINK_DEAD;
2585		sbp_orb_pointer(ocb->sdev, ocb);
2586	}
2587}
2588
2589static void
2590sbp_poll(struct cam_sim *sim)
2591{
2592	struct sbp_softc *sbp;
2593	struct firewire_comm *fc;
2594
2595	sbp = (struct sbp_softc *)sim->softc;
2596	fc = sbp->fd.fc;
2597
2598	fc->poll(fc, 0, -1);
2599
2600	return;
2601}
2602
2603static struct sbp_ocb *
2604sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2605{
2606	struct sbp_ocb *ocb;
2607	struct sbp_ocb *next;
2608	int s = splfw(), order = 0;
2609	int flags;
2610
2611SBP_DEBUG(1)
2612	sbp_show_sdev_info(sdev, 2);
2613#if __FreeBSD_version >= 500000
2614	printf("%s: 0x%08x src %d\n",
2615#else
2616	printf("%s: 0x%08lx src %d\n",
2617#endif
2618	    __FUNCTION__, ntohl(sbp_status->orb_lo), sbp_status->src);
2619END_DEBUG
2620	for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2621		next = STAILQ_NEXT(ocb, ocb);
2622		flags = ocb->flags;
2623		if (OCB_MATCH(ocb, sbp_status)) {
2624			/* found */
2625			STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2626			if (ocb->ccb != NULL)
2627				untimeout(sbp_timeout, (caddr_t)ocb,
2628						ocb->ccb->ccb_h.timeout_ch);
2629			if (ntohl(ocb->orb[4]) & 0xffff) {
2630				bus_dmamap_sync(sdev->target->sbp->dmat,
2631					ocb->dmamap,
2632					(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2633					BUS_DMASYNC_POSTREAD :
2634					BUS_DMASYNC_POSTWRITE);
2635				bus_dmamap_unload(sdev->target->sbp->dmat,
2636					ocb->dmamap);
2637			}
2638			if (sbp_status->src == SRC_NO_NEXT) {
2639				if (next != NULL)
2640					sbp_orb_pointer(sdev, next);
2641				else if (order > 0) {
2642					/*
2643					 * Unordered execution
2644					 * We need to send pointer for
2645					 * next ORB
2646					 */
2647					sdev->flags |= ORB_LINK_DEAD;
2648				}
2649			}
2650			break;
2651		} else
2652			order ++;
2653	}
2654	splx(s);
2655SBP_DEBUG(0)
2656	if (ocb && order > 0) {
2657		sbp_show_sdev_info(sdev, 2);
2658		printf("unordered execution order:%d\n", order);
2659	}
2660END_DEBUG
2661	return (ocb);
2662}
2663
2664static struct sbp_ocb *
2665sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2666{
2667	int s = splfw();
2668	struct sbp_ocb *prev;
2669
2670SBP_DEBUG(1)
2671	sbp_show_sdev_info(sdev, 2);
2672#if __FreeBSD_version >= 500000
2673	printf("%s: 0x%08jx\n", __FUNCTION__, (uintmax_t)ocb->bus_addr);
2674#else
2675	printf("%s: 0x%08x\n", __FUNCTION__, ocb->bus_addr);
2676#endif
2677END_DEBUG
2678	prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2679	STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2680
2681	if (ocb->ccb != NULL)
2682		ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb,
2683					(ocb->ccb->ccb_h.timeout * hz) / 1000);
2684
2685	if (prev != NULL) {
2686SBP_DEBUG(2)
2687#if __FreeBSD_version >= 500000
2688		printf("linking chain 0x%jx -> 0x%jx\n",
2689		    (uintmax_t)prev->bus_addr, (uintmax_t)ocb->bus_addr);
2690#else
2691		printf("linking chain 0x%x -> 0x%x\n",
2692		    prev->bus_addr, ocb->bus_addr);
2693#endif
2694END_DEBUG
2695		prev->orb[1] = htonl(ocb->bus_addr);
2696		prev->orb[0] = 0;
2697	}
2698	splx(s);
2699
2700	return prev;
2701}
2702
2703static struct sbp_ocb *
2704sbp_get_ocb(struct sbp_dev *sdev)
2705{
2706	struct sbp_ocb *ocb;
2707	int s = splfw();
2708	ocb = STAILQ_FIRST(&sdev->free_ocbs);
2709	if (ocb == NULL) {
2710		printf("ocb shortage!!!\n");
2711		return NULL;
2712	}
2713	STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2714	splx(s);
2715	ocb->ccb = NULL;
2716	return (ocb);
2717}
2718
2719static void
2720sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2721{
2722	ocb->flags = 0;
2723	ocb->ccb = NULL;
2724	STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2725}
2726
2727static void
2728sbp_abort_ocb(struct sbp_ocb *ocb, int status)
2729{
2730	struct sbp_dev *sdev;
2731
2732	sdev = ocb->sdev;
2733SBP_DEBUG(0)
2734	sbp_show_sdev_info(sdev, 2);
2735#if __FreeBSD_version >= 500000
2736	printf("sbp_abort_ocb 0x%jx\n", (uintmax_t)ocb->bus_addr);
2737#else
2738	printf("sbp_abort_ocb 0x%x\n", ocb->bus_addr);
2739#endif
2740END_DEBUG
2741SBP_DEBUG(1)
2742	if (ocb->ccb != NULL)
2743		sbp_print_scsi_cmd(ocb);
2744END_DEBUG
2745	if (ntohl(ocb->orb[4]) & 0xffff) {
2746		bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
2747			(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2748			BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2749		bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
2750	}
2751	if (ocb->ccb != NULL) {
2752		untimeout(sbp_timeout, (caddr_t)ocb,
2753					ocb->ccb->ccb_h.timeout_ch);
2754		ocb->ccb->ccb_h.status = status;
2755		xpt_done(ocb->ccb);
2756	}
2757	sbp_free_ocb(sdev, ocb);
2758}
2759
2760static void
2761sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2762{
2763	int s;
2764	struct sbp_ocb *ocb, *next;
2765	STAILQ_HEAD(, sbp_ocb) temp;
2766
2767	s = splfw();
2768
2769	bcopy(&sdev->ocbs, &temp, sizeof(temp));
2770	STAILQ_INIT(&sdev->ocbs);
2771	for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2772		next = STAILQ_NEXT(ocb, ocb);
2773		sbp_abort_ocb(ocb, status);
2774	}
2775
2776	splx(s);
2777}
2778
2779static devclass_t sbp_devclass;
2780
2781static device_method_t sbp_methods[] = {
2782	/* device interface */
2783	DEVMETHOD(device_identify,	sbp_identify),
2784	DEVMETHOD(device_probe,		sbp_probe),
2785	DEVMETHOD(device_attach,	sbp_attach),
2786	DEVMETHOD(device_detach,	sbp_detach),
2787	DEVMETHOD(device_shutdown,	sbp_shutdown),
2788
2789	{ 0, 0 }
2790};
2791
2792static driver_t sbp_driver = {
2793	"sbp",
2794	sbp_methods,
2795	sizeof(struct sbp_softc),
2796};
2797DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2798MODULE_VERSION(sbp, 1);
2799MODULE_DEPEND(sbp, firewire, 1, 1, 1);
2800MODULE_DEPEND(sbp, cam, 1, 1, 1);
2801