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