xref: /dragonfly/sys/dev/disk/sbp/sbp.c (revision fb151170)
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  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/conf.h>
40 #include <sys/module.h>
41 #include <sys/bus.h>
42 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
44 #include <sys/malloc.h>
45 
46 #include <sys/devicestat.h>	/* for struct devstat */
47 #include <sys/thread2.h>
48 
49 #include <bus/cam/cam.h>
50 #include <bus/cam/cam_ccb.h>
51 #include <bus/cam/cam_sim.h>
52 #include <bus/cam/cam_xpt_sim.h>
53 #include <bus/cam/cam_debug.h>
54 #include <bus/cam/cam_periph.h>
55 #include <bus/cam/scsi/scsi_all.h>
56 
57 #include <bus/firewire/firewire.h>
58 #include <bus/firewire/firewirereg.h>
59 #include <bus/firewire/fwdma.h>
60 #include <bus/firewire/iec13213.h>
61 #include "sbp.h"
62 
63 #define ccb_sdev_ptr	spriv_ptr0
64 #define ccb_sbp_ptr	spriv_ptr1
65 
66 #define SBP_NUM_TARGETS 8 /* MAX 64 */
67 /*
68  * Scan_bus doesn't work for more than 8 LUNs
69  * because of CAM_SCSI2_MAXLUN in cam_xpt.c
70  */
71 #define SBP_NUM_LUNS 64
72 #define SBP_DMA_SIZE PAGE_SIZE
73 #define SBP_LOGIN_SIZE sizeof(struct sbp_login_res)
74 #define SBP_QUEUE_LEN ((SBP_DMA_SIZE - SBP_LOGIN_SIZE) / sizeof(struct sbp_ocb))
75 #define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS)
76 
77 /*
78  * STATUS FIFO addressing
79  *   bit
80  * -----------------------
81  *  0- 1( 2): 0 (alingment)
82  *  2- 7( 6): target
83  *  8-15( 8): lun
84  * 16-31( 8): reserved
85  * 32-47(16): SBP_BIND_HI
86  * 48-64(16): bus_id, node_id
87  */
88 #define SBP_BIND_HI 0x1
89 #define SBP_DEV2ADDR(t, l) \
90 	(((u_int64_t)SBP_BIND_HI << 32) \
91 	| (((l) & 0xff) << 8) \
92 	| (((t) & 0x3f) << 2))
93 #define SBP_ADDR2TRG(a)	(((a) >> 2) & 0x3f)
94 #define SBP_ADDR2LUN(a)	(((a) >> 8) & 0xff)
95 #define SBP_INITIATOR 7
96 
97 static char *orb_fun_name[] = {
98 	ORB_FUN_NAMES
99 };
100 
101 static int debug = 0;
102 static int auto_login = 1;
103 static int max_speed = -1;
104 #if 0
105 static int sbp_cold = 1;
106 #endif
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 callout_stop 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 0
718 				if (sdev->path) {
719 					xpt_freeze_devq(sdev->path, 1);
720 					sdev->freeze ++;
721 				}
722 				sdev->status = SBP_DEV_RETRY;
723 				sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
724 #endif
725 				sbp_cam_detach_target(sdev->target);
726 				sdev->status = SBP_DEV_RESET;
727 				break;
728 			case SBP_DEV_PROBE:
729 			case SBP_DEV_TOATTACH:
730 				sdev->status = SBP_DEV_RESET;
731 				break;
732 			case SBP_DEV_RETRY:
733 			case SBP_DEV_RESET:
734 			case SBP_DEV_DEAD:
735 				break;
736 			}
737 		}
738 	}
739 }
740 
741 static void
742 sbp_post_busreset(void *arg)
743 {
744 	struct sbp_softc *sbp;
745 
746 	sbp = (struct sbp_softc *)arg;
747 SBP_DEBUG(0)
748 	kprintf("sbp_post_busreset\n");
749 END_DEBUG
750 	if ((sbp->sim->flags & SIMQ_FREEZED) == 0) {
751 		xpt_freeze_simq(sbp->sim, /*count*/1);
752 		sbp->sim->flags |= SIMQ_FREEZED;
753 	}
754 	microtime(&sbp->last_busreset);
755 }
756 
757 static void
758 sbp_post_explore(void *arg)
759 {
760 	struct sbp_softc *sbp = (struct sbp_softc *)arg;
761 	struct sbp_target *target;
762 	struct fw_device *fwdev;
763 	int i, alive;
764 
765 SBP_DEBUG(0)
766 	kprintf("sbp_post_explore\n");
767 END_DEBUG
768 #if 0
769 	if (sbp_cold > 0)
770 		sbp_cold --;
771 #endif
772 
773 #if 0
774 	/*
775 	 * XXX don't let CAM the bus rest.
776 	 * CAM tries to do something with freezed (DEV_RETRY) devices.
777 	 */
778 	xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
779 #endif
780 
781 	/* Gabage Collection */
782 	for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
783 		target = &sbp->targets[i];
784 		STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
785 			if (target->fwdev == NULL || target->fwdev == fwdev)
786 				break;
787 		if (fwdev == NULL) {
788 			/* device has removed in lower driver */
789 			sbp_cam_detach_target(target);
790 			sbp_free_target(target);
791 		}
792 	}
793 	/* traverse device list */
794 	STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
795 SBP_DEBUG(0)
796 		kprintf("sbp_post_explore: EUI:%08x%08x ",
797 				fwdev->eui.hi, fwdev->eui.lo);
798 		if (fwdev->status != FWDEVATTACHED)
799 			kprintf("not attached, state=%d.\n", fwdev->status);
800 		else
801 			kprintf("attached\n");
802 END_DEBUG
803 		alive = SBP_FWDEV_ALIVE(fwdev);
804 		for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
805 			target = &sbp->targets[i];
806 			if(target->fwdev == fwdev ) {
807 				/* known target */
808 				break;
809 			}
810 		}
811 		if(i == SBP_NUM_TARGETS){
812 			if (alive) {
813 				/* new target */
814 				target = sbp_alloc_target(sbp, fwdev);
815 				if (target == NULL)
816 					continue;
817 			} else {
818 				continue;
819 			}
820 		}
821 		sbp_probe_target((void *)target);
822 		if (target->num_lun == 0)
823 			sbp_free_target(target);
824 	}
825 	xpt_release_simq(sbp->sim, /*run queue*/TRUE);
826 	sbp->sim->flags &= ~SIMQ_FREEZED;
827 }
828 
829 #if NEED_RESPONSE
830 static void
831 sbp_loginres_callback(struct fw_xfer *xfer){
832 	struct sbp_dev *sdev;
833 	sdev = (struct sbp_dev *)xfer->sc;
834 SBP_DEBUG(1)
835 	sbp_show_sdev_info(sdev, 2);
836 	kprintf("sbp_loginres_callback\n");
837 END_DEBUG
838 	/* recycle */
839 	crit_enter();
840 	STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link);
841 	crit_exit();
842 	return;
843 }
844 #endif
845 
846 static __inline void
847 sbp_xfer_free(struct fw_xfer *xfer)
848 {
849 	struct sbp_dev *sdev;
850 
851 	sdev = (struct sbp_dev *)xfer->sc;
852 	fw_xfer_unload(xfer);
853 	crit_enter();
854 	STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
855 	crit_exit();
856 }
857 
858 static void
859 sbp_reset_start_callback(struct fw_xfer *xfer)
860 {
861 	struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
862 	struct sbp_target *target = sdev->target;
863 	int i;
864 
865 	if (xfer->resp != 0) {
866 		sbp_show_sdev_info(sdev, 2);
867 		kprintf("sbp_reset_start failed: resp=%d\n", xfer->resp);
868 	}
869 
870 	for (i = 0; i < target->num_lun; i++) {
871 		tsdev = target->luns[i];
872 		if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN)
873 			sbp_login(tsdev);
874 	}
875 }
876 
877 static void
878 sbp_reset_start(struct sbp_dev *sdev)
879 {
880 	struct fw_xfer *xfer;
881 	struct fw_pkt *fp;
882 
883 SBP_DEBUG(0)
884 	sbp_show_sdev_info(sdev, 2);
885 	kprintf("sbp_reset_start\n");
886 END_DEBUG
887 
888 	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
889 	xfer->act.hand = sbp_reset_start_callback;
890 	fp = &xfer->send.hdr;
891 	fp->mode.wreqq.dest_hi = 0xffff;
892 	fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
893 	fp->mode.wreqq.data = htonl(0xf);
894 	fw_asyreq(xfer->fc, -1, xfer);
895 }
896 
897 static void
898 sbp_mgm_callback(struct fw_xfer *xfer)
899 {
900 	struct sbp_dev *sdev;
901 	int resp;
902 
903 	sdev = (struct sbp_dev *)xfer->sc;
904 
905 SBP_DEBUG(1)
906 	sbp_show_sdev_info(sdev, 2);
907 	kprintf("sbp_mgm_callback\n");
908 END_DEBUG
909 	resp = xfer->resp;
910 	sbp_xfer_free(xfer);
911 #if 0
912 	if (resp != 0) {
913 		sbp_show_sdev_info(sdev, 2);
914 		kprintf("management ORB failed(%d) ... RESET_START\n", resp);
915 		sbp_reset_start(sdev);
916 	}
917 #endif
918 	return;
919 }
920 
921 static struct sbp_dev *
922 sbp_next_dev(struct sbp_target *target, int lun)
923 {
924 	struct sbp_dev **sdevp;
925 	int i;
926 
927 	for (i = lun, sdevp = &target->luns[lun]; i < target->num_lun;
928 	    i++, sdevp++)
929 		if (*sdevp != NULL && (*sdevp)->status == SBP_DEV_PROBE)
930 			return(*sdevp);
931 	return(NULL);
932 }
933 
934 #define SCAN_PRI 1
935 static void
936 sbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb)
937 {
938 	struct sbp_target *target;
939 	struct sbp_dev *sdev;
940 
941 	sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
942 	target = sdev->target;
943 SBP_DEBUG(0)
944 	sbp_show_sdev_info(sdev, 2);
945 	kprintf("sbp_cam_scan_lun\n");
946 END_DEBUG
947 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
948 		sdev->status = SBP_DEV_ATTACHED;
949 	} else {
950 		sbp_show_sdev_info(sdev, 2);
951 		kprintf("scan failed\n");
952 	}
953 	sdev = sbp_next_dev(target, sdev->lun_id + 1);
954 	if (sdev == NULL) {
955 		kfree(ccb, M_SBP);
956 		return;
957 	}
958 	/* reuse ccb */
959 	xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
960 	ccb->ccb_h.ccb_sdev_ptr = sdev;
961 	xpt_action(ccb);
962 	xpt_release_devq(sdev->path, sdev->freeze, TRUE);
963 	sdev->freeze = 1;
964 }
965 
966 static void
967 sbp_cam_scan_target(void *arg)
968 {
969 	struct sbp_target *target = (struct sbp_target *)arg;
970 	struct sbp_dev *sdev;
971 	union ccb *ccb;
972 
973 	sdev = sbp_next_dev(target, 0);
974 	if (sdev == NULL) {
975 		kprintf("sbp_cam_scan_target: nothing to do for target%d\n",
976 							target->target_id);
977 		return;
978 	}
979 SBP_DEBUG(0)
980 	sbp_show_sdev_info(sdev, 2);
981 	kprintf("sbp_cam_scan_target\n");
982 END_DEBUG
983 	ccb = kmalloc(sizeof(union ccb), M_SBP, M_WAITOK | M_ZERO);
984 	xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
985 	ccb->ccb_h.func_code = XPT_SCAN_LUN;
986 	ccb->ccb_h.cbfcnp = sbp_cam_scan_lun;
987 	ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
988 	ccb->crcn.flags = CAM_FLAG_NONE;
989 	ccb->ccb_h.ccb_sdev_ptr = sdev;
990 
991 	/* The scan is in progress now. */
992 	xpt_action(ccb);
993 	xpt_release_devq(sdev->path, sdev->freeze, TRUE);
994 	sdev->freeze = 1;
995 }
996 
997 static __inline void
998 sbp_scan_dev(struct sbp_dev *sdev)
999 {
1000 	sdev->status = SBP_DEV_PROBE;
1001 	callout_reset(&sdev->target->scan_callout, scan_delay * hz / 1000,
1002 			sbp_cam_scan_target, (void *)sdev->target);
1003 }
1004 
1005 static void
1006 sbp_do_attach(struct fw_xfer *xfer)
1007 {
1008 	struct sbp_dev *sdev;
1009 	struct sbp_target *target;
1010 	struct sbp_softc *sbp;
1011 
1012 	sdev = (struct sbp_dev *)xfer->sc;
1013 	target = sdev->target;
1014 	sbp = target->sbp;
1015 SBP_DEBUG(0)
1016 	sbp_show_sdev_info(sdev, 2);
1017 	kprintf("sbp_do_attach\n");
1018 END_DEBUG
1019 	sbp_xfer_free(xfer);
1020 
1021 	if (sdev->path == NULL)
1022 		xpt_create_path(&sdev->path, xpt_periph,
1023 			cam_sim_path(target->sbp->sim),
1024 			target->target_id, sdev->lun_id);
1025 
1026 #if 0
1027 	/*
1028 	 * Let CAM scan the bus if we are in the boot process.
1029 	 * XXX xpt_scan_bus cannot detect LUN larger than 0
1030 	 * if LUN 0 doesn't exists.
1031 	 */
1032 	if (sbp_cold > 0) {
1033 		sdev->status = SBP_DEV_ATTACHED;
1034 		return;
1035 	}
1036 #endif
1037 
1038 	sbp_scan_dev(sdev);
1039 	return;
1040 }
1041 
1042 static void
1043 sbp_agent_reset_callback(struct fw_xfer *xfer)
1044 {
1045 	struct sbp_dev *sdev;
1046 
1047 	sdev = (struct sbp_dev *)xfer->sc;
1048 SBP_DEBUG(1)
1049 	sbp_show_sdev_info(sdev, 2);
1050 	kprintf("%s\n", __func__);
1051 END_DEBUG
1052 	if (xfer->resp != 0) {
1053 		sbp_show_sdev_info(sdev, 2);
1054 		kprintf("%s: resp=%d\n", __func__, xfer->resp);
1055 	}
1056 
1057 	sbp_xfer_free(xfer);
1058 	if (sdev->path) {
1059 		xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1060 		sdev->freeze = 0;
1061 	}
1062 }
1063 
1064 static void
1065 sbp_agent_reset(struct sbp_dev *sdev)
1066 {
1067 	struct fw_xfer *xfer;
1068 	struct fw_pkt *fp;
1069 
1070 SBP_DEBUG(0)
1071 	sbp_show_sdev_info(sdev, 2);
1072 	kprintf("sbp_agent_reset\n");
1073 END_DEBUG
1074 	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1075 	if (xfer == NULL)
1076 		return;
1077 	if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE)
1078 		xfer->act.hand = sbp_agent_reset_callback;
1079 	else
1080 		xfer->act.hand = sbp_do_attach;
1081 	fp = &xfer->send.hdr;
1082 	fp->mode.wreqq.data = htonl(0xf);
1083 	fw_asyreq(xfer->fc, -1, xfer);
1084 	sbp_abort_all_ocbs(sdev, CAM_BDR_SENT);
1085 }
1086 
1087 static void
1088 sbp_busy_timeout_callback(struct fw_xfer *xfer)
1089 {
1090 	struct sbp_dev *sdev;
1091 
1092 	sdev = (struct sbp_dev *)xfer->sc;
1093 SBP_DEBUG(1)
1094 	sbp_show_sdev_info(sdev, 2);
1095 	kprintf("sbp_busy_timeout_callback\n");
1096 END_DEBUG
1097 	sbp_xfer_free(xfer);
1098 	sbp_agent_reset(sdev);
1099 }
1100 
1101 static void
1102 sbp_busy_timeout(struct sbp_dev *sdev)
1103 {
1104 	struct fw_pkt *fp;
1105 	struct fw_xfer *xfer;
1106 SBP_DEBUG(0)
1107 	sbp_show_sdev_info(sdev, 2);
1108 	kprintf("sbp_busy_timeout\n");
1109 END_DEBUG
1110 	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1111 
1112 	xfer->act.hand = sbp_busy_timeout_callback;
1113 	fp = &xfer->send.hdr;
1114 	fp->mode.wreqq.dest_hi = 0xffff;
1115 	fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1116 	fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1117 	fw_asyreq(xfer->fc, -1, xfer);
1118 }
1119 
1120 static void
1121 sbp_orb_pointer_callback(struct fw_xfer *xfer)
1122 {
1123 	struct sbp_dev *sdev;
1124 	sdev = (struct sbp_dev *)xfer->sc;
1125 
1126 SBP_DEBUG(1)
1127 	sbp_show_sdev_info(sdev, 2);
1128 	kprintf("%s\n", __func__);
1129 END_DEBUG
1130 	if (xfer->resp != 0) {
1131 		/* XXX */
1132 		kprintf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1133 	}
1134 	sbp_xfer_free(xfer);
1135 	sdev->flags &= ~ORB_POINTER_ACTIVE;
1136 
1137 	if ((sdev->flags & ORB_POINTER_NEED) != 0) {
1138 		struct sbp_ocb *ocb;
1139 
1140 		sdev->flags &= ~ORB_POINTER_NEED;
1141 		ocb = STAILQ_FIRST(&sdev->ocbs);
1142 		if (ocb != NULL)
1143 			sbp_orb_pointer(sdev, ocb);
1144 	}
1145 	return;
1146 }
1147 
1148 static void
1149 sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1150 {
1151 	struct fw_xfer *xfer;
1152 	struct fw_pkt *fp;
1153 SBP_DEBUG(1)
1154 	sbp_show_sdev_info(sdev, 2);
1155 	kprintf("%s: 0x%08x\n", __func__, (u_int32_t)ocb->bus_addr);
1156 END_DEBUG
1157 
1158 	if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) {
1159 SBP_DEBUG(0)
1160 		kprintf("%s: orb pointer active\n", __func__);
1161 END_DEBUG
1162 		sdev->flags |= ORB_POINTER_NEED;
1163 		return;
1164 	}
1165 
1166 	sdev->flags |= ORB_POINTER_ACTIVE;
1167 	xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1168 	if (xfer == NULL)
1169 		return;
1170 	xfer->act.hand = sbp_orb_pointer_callback;
1171 
1172 	fp = &xfer->send.hdr;
1173 	fp->mode.wreqb.len = 8;
1174 	fp->mode.wreqb.extcode = 0;
1175 	xfer->send.payload[0] =
1176 		htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1177 	xfer->send.payload[1] = htonl((u_int32_t)ocb->bus_addr);
1178 
1179 	if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1180 			sbp_xfer_free(xfer);
1181 			ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1182 			xpt_done(ocb->ccb);
1183 	}
1184 }
1185 
1186 #if 0
1187 static void
1188 sbp_cmd_callback(struct fw_xfer *xfer)
1189 {
1190 SBP_DEBUG(1)
1191 	struct sbp_dev *sdev;
1192 	sdev = (struct sbp_dev *)xfer->sc;
1193 	sbp_show_sdev_info(sdev, 2);
1194 	kprintf("sbp_cmd_callback\n");
1195 END_DEBUG
1196 	if (xfer->resp != 0) {
1197 		/* XXX */
1198 		kprintf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1199 	}
1200 	sbp_xfer_free(xfer);
1201 	return;
1202 }
1203 
1204 static void
1205 sbp_doorbell(struct sbp_dev *sdev)
1206 {
1207 	struct fw_xfer *xfer;
1208 	struct fw_pkt *fp;
1209 SBP_DEBUG(1)
1210 	sbp_show_sdev_info(sdev, 2);
1211 	kprintf("sbp_doorbell\n");
1212 END_DEBUG
1213 
1214 	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1215 	if (xfer == NULL)
1216 		return;
1217 	xfer->act.hand = sbp_cmd_callback;
1218 	fp = (struct fw_pkt *)xfer->send.buf;
1219 	fp->mode.wreqq.data = htonl(0xf);
1220 	fw_asyreq(xfer->fc, -1, xfer);
1221 }
1222 #endif
1223 
1224 static struct fw_xfer *
1225 sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1226 {
1227 	struct fw_xfer *xfer;
1228 	struct fw_pkt *fp;
1229 	struct sbp_target *target;
1230 	int new = 0;
1231 
1232 	target = sdev->target;
1233 	crit_enter();
1234 	xfer = STAILQ_FIRST(&target->xferlist);
1235 	if (xfer == NULL) {
1236 		if (target->n_xfer > 5 /* XXX */) {
1237 			kprintf("sbp: no more xfer for this target\n");
1238 			crit_exit();
1239 			return(NULL);
1240 		}
1241 		xfer = fw_xfer_alloc_buf(M_SBP, 8, 0);
1242 		if(xfer == NULL){
1243 			kprintf("sbp: fw_xfer_alloc_buf failed\n");
1244 			crit_exit();
1245 			return NULL;
1246 		}
1247 		target->n_xfer ++;
1248 		if (debug)
1249 			kprintf("sbp: alloc %d xfer\n", target->n_xfer);
1250 		new = 1;
1251 	} else {
1252 		STAILQ_REMOVE_HEAD(&target->xferlist, link);
1253 	}
1254 	crit_exit();
1255 
1256 	microtime(&xfer->tv);
1257 
1258 	if (new) {
1259 		xfer->recv.pay_len = 0;
1260 		xfer->send.spd = min(sdev->target->fwdev->speed, max_speed);
1261 		xfer->fc = sdev->target->sbp->fd.fc;
1262 		xfer->retry_req = fw_asybusy;
1263 	}
1264 
1265 	if (tcode == FWTCODE_WREQB)
1266 		xfer->send.pay_len = 8;
1267 	else
1268 		xfer->send.pay_len = 0;
1269 
1270 	xfer->sc = (caddr_t)sdev;
1271 	fp = &xfer->send.hdr;
1272 	fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
1273 	fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
1274 	fp->mode.wreqq.tlrt = 0;
1275 	fp->mode.wreqq.tcode = tcode;
1276 	fp->mode.wreqq.pri = 0;
1277 	fp->mode.wreqq.dst = FWLOCALBUS | sdev->target->fwdev->dst;
1278 
1279 	return xfer;
1280 
1281 }
1282 
1283 static void
1284 sbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1285 {
1286 	struct fw_xfer *xfer;
1287 	struct fw_pkt *fp;
1288 	struct sbp_ocb *ocb;
1289 	struct sbp_target *target;
1290 	int nid;
1291 
1292 	target = sdev->target;
1293 	nid = target->sbp->fd.fc->nodeid | FWLOCALBUS;
1294 
1295 	crit_enter();
1296 	if (func == ORB_FUN_RUNQUEUE) {
1297 		ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1298 		if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1299 			crit_exit();
1300 			return;
1301 		}
1302 		STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1303 		goto start;
1304 	}
1305 	if ((ocb = sbp_get_ocb(sdev)) == NULL) {
1306 		crit_exit();
1307 		/* XXX */
1308 		return;
1309 	}
1310 	ocb->flags = OCB_ACT_MGM;
1311 	ocb->sdev = sdev;
1312 
1313 	bzero((void *)ocb->orb, sizeof(ocb->orb));
1314 	ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1315 	ocb->orb[7] = htonl(SBP_DEV2ADDR(target->target_id, sdev->lun_id));
1316 
1317 SBP_DEBUG(0)
1318 	sbp_show_sdev_info(sdev, 2);
1319 	kprintf("%s\n", orb_fun_name[(func>>16)&0xf]);
1320 END_DEBUG
1321 	switch (func) {
1322 	case ORB_FUN_LGI:
1323 		ocb->orb[0] = ocb->orb[1] = 0; /* password */
1324 		ocb->orb[2] = htonl(nid << 16);
1325 		ocb->orb[3] = htonl(sdev->dma.bus_addr);
1326 		ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id);
1327 		if (ex_login)
1328 			ocb->orb[4] |= htonl(ORB_EXV);
1329 		ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
1330 		fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD);
1331 		break;
1332 	case ORB_FUN_ATA:
1333 		ocb->orb[0] = htonl((0 << 16) | 0);
1334 		ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
1335 		/* fall through */
1336 	case ORB_FUN_RCN:
1337 	case ORB_FUN_LGO:
1338 	case ORB_FUN_LUR:
1339 	case ORB_FUN_RST:
1340 	case ORB_FUN_ATS:
1341 		ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
1342 		break;
1343 	}
1344 
1345 	if (target->mgm_ocb_cur != NULL) {
1346 		/* there is a standing ORB */
1347 		STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1348 		crit_exit();
1349 		return;
1350 	}
1351 start:
1352 	target->mgm_ocb_cur = ocb;
1353 	crit_exit();
1354 
1355 	callout_reset(&target->mgm_ocb_timeout, 5*hz,
1356 				sbp_mgm_timeout, (caddr_t)ocb);
1357 	xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1358 	if(xfer == NULL){
1359 		return;
1360 	}
1361 	xfer->act.hand = sbp_mgm_callback;
1362 
1363 	fp = &xfer->send.hdr;
1364 	fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
1365 	fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
1366 	fp->mode.wreqb.len = 8;
1367 	fp->mode.wreqb.extcode = 0;
1368 	xfer->send.payload[0] = htonl(nid << 16);
1369 	xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff);
1370 SBP_DEBUG(0)
1371 	sbp_show_sdev_info(sdev, 2);
1372 	kprintf("mgm orb: %08x\n", (u_int32_t)ocb->bus_addr);
1373 END_DEBUG
1374 
1375 	fw_asyreq(xfer->fc, -1, xfer);
1376 }
1377 
1378 static void
1379 sbp_print_scsi_cmd(struct sbp_ocb *ocb)
1380 {
1381 	struct ccb_scsiio *csio;
1382 
1383 	csio = &ocb->ccb->csio;
1384 	kprintf("%s:%d:%d XPT_SCSI_IO: "
1385 		"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1386 		", flags: 0x%02x, "
1387 		"%db cmd/%db data/%db sense\n",
1388 		device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1389 		ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun,
1390 		csio->cdb_io.cdb_bytes[0],
1391 		csio->cdb_io.cdb_bytes[1],
1392 		csio->cdb_io.cdb_bytes[2],
1393 		csio->cdb_io.cdb_bytes[3],
1394 		csio->cdb_io.cdb_bytes[4],
1395 		csio->cdb_io.cdb_bytes[5],
1396 		csio->cdb_io.cdb_bytes[6],
1397 		csio->cdb_io.cdb_bytes[7],
1398 		csio->cdb_io.cdb_bytes[8],
1399 		csio->cdb_io.cdb_bytes[9],
1400 		ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1401 		csio->cdb_len, csio->dxfer_len,
1402 		csio->sense_len);
1403 }
1404 
1405 static void
1406 sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1407 {
1408 	struct sbp_cmd_status *sbp_cmd_status;
1409 	struct scsi_sense_data *sense;
1410 
1411 	sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1412 	sense = &ocb->ccb->csio.sense_data;
1413 
1414 SBP_DEBUG(0)
1415 	sbp_print_scsi_cmd(ocb);
1416 	/* XXX need decode status */
1417 	sbp_show_sdev_info(ocb->sdev, 2);
1418 	kprintf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1419 		sbp_cmd_status->status,
1420 		sbp_cmd_status->sfmt,
1421 		sbp_cmd_status->valid,
1422 		sbp_cmd_status->s_key,
1423 		sbp_cmd_status->s_code,
1424 		sbp_cmd_status->s_qlfr,
1425 		sbp_status->len
1426 	);
1427 END_DEBUG
1428 
1429 	switch (sbp_cmd_status->status) {
1430 	case SCSI_STATUS_CHECK_COND:
1431 	case SCSI_STATUS_BUSY:
1432 	case SCSI_STATUS_CMD_TERMINATED:
1433 		if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1434 			sense->error_code = SSD_CURRENT_ERROR;
1435 		}else{
1436 			sense->error_code = SSD_DEFERRED_ERROR;
1437 		}
1438 		if(sbp_cmd_status->valid)
1439 			sense->error_code |= SSD_ERRCODE_VALID;
1440 		sense->flags = sbp_cmd_status->s_key;
1441 		if(sbp_cmd_status->mark)
1442 			sense->flags |= SSD_FILEMARK;
1443 		if(sbp_cmd_status->eom)
1444 			sense->flags |= SSD_EOM;
1445 		if(sbp_cmd_status->ill_len)
1446 			sense->flags |= SSD_ILI;
1447 
1448 		bcopy(&sbp_cmd_status->info, &sense->info[0], 4);
1449 
1450 		if (sbp_status->len <= 1)
1451 			/* XXX not scsi status. shouldn't be happened */
1452 			sense->extra_len = 0;
1453 		else if (sbp_status->len <= 4)
1454 			/* add_sense_code(_qual), info, cmd_spec_info */
1455 			sense->extra_len = 6;
1456 		else
1457 			/* fru, sense_key_spec */
1458 			sense->extra_len = 10;
1459 
1460 		bcopy(&sbp_cmd_status->cdb, &sense->cmd_spec_info[0], 4);
1461 
1462 		sense->add_sense_code = sbp_cmd_status->s_code;
1463 		sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1464 		sense->fru = sbp_cmd_status->fru;
1465 
1466 		bcopy(&sbp_cmd_status->s_keydep[0],
1467 		    &sense->sense_key_spec[0], 3);
1468 
1469 		ocb->ccb->csio.scsi_status = sbp_cmd_status->status;
1470 		ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1471 							| CAM_AUTOSNS_VALID;
1472 /*
1473 {
1474 		u_int8_t j, *tmp;
1475 		tmp = sense;
1476 		for( j = 0 ; j < 32 ; j+=8){
1477 			kprintf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1478 				tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1479 				tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1480 		}
1481 
1482 }
1483 */
1484 		break;
1485 	default:
1486 		sbp_show_sdev_info(ocb->sdev, 2);
1487 		kprintf("sbp_scsi_status: unknown scsi status 0x%x\n",
1488 						sbp_cmd_status->status);
1489 	}
1490 }
1491 
1492 static void
1493 sbp_fix_inq_data(struct sbp_ocb *ocb)
1494 {
1495 	union ccb *ccb;
1496 	struct sbp_dev *sdev;
1497 	struct scsi_inquiry_data *inq;
1498 
1499 	ccb = ocb->ccb;
1500 	sdev = ocb->sdev;
1501 
1502 	if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1503 		return;
1504 SBP_DEBUG(1)
1505 	sbp_show_sdev_info(sdev, 2);
1506 	kprintf("sbp_fix_inq_data\n");
1507 END_DEBUG
1508 	inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1509 	switch (SID_TYPE(inq)) {
1510 	case T_DIRECT:
1511 #if 0
1512 		/*
1513 		 * XXX Convert Direct Access device to RBC.
1514 		 * I've never seen FireWire DA devices which support READ_6.
1515 		 */
1516 		if (SID_TYPE(inq) == T_DIRECT)
1517 			inq->device |= T_RBC; /*  T_DIRECT == 0 */
1518 #endif
1519 		/* fall through */
1520 	case T_RBC:
1521 		/* enable tagged queuing */
1522 		if (sbp_tags)
1523 			inq->flags |= SID_CmdQue;
1524 		else
1525 			inq->flags &= ~SID_CmdQue;
1526 		/*
1527 		 * Override vendor/product/revision information.
1528 		 * Some devices sometimes return strange strings.
1529 		 */
1530 #if 1
1531 		bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1532 		bcopy(sdev->product, inq->product, sizeof(inq->product));
1533 		bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1534 #endif
1535 		break;
1536 	}
1537 }
1538 
1539 static void
1540 sbp_recv1(struct fw_xfer *xfer)
1541 {
1542 	struct fw_pkt *rfp;
1543 #if NEED_RESPONSE
1544 	struct fw_pkt *sfp;
1545 #endif
1546 	struct sbp_softc *sbp;
1547 	struct sbp_dev *sdev;
1548 	struct sbp_ocb *ocb;
1549 	struct sbp_login_res *login_res = NULL;
1550 	struct sbp_status *sbp_status;
1551 	struct sbp_target *target;
1552 	int	orb_fun, status_valid0, status_valid, t, l, reset_agent = 0;
1553 	u_int32_t addr;
1554 /*
1555 	u_int32_t *ld;
1556 	ld = xfer->recv.buf;
1557 kprintf("sbp %x %d %d %08x %08x %08x %08x\n",
1558 			xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1559 kprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1560 kprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1561 */
1562 	sbp = (struct sbp_softc *)xfer->sc;
1563 	if (xfer->resp != 0){
1564 		kprintf("sbp_recv: xfer->resp = %d\n", xfer->resp);
1565 		goto done0;
1566 	}
1567 	if (xfer->recv.payload == NULL){
1568 		kprintf("sbp_recv: xfer->recv.payload == NULL\n");
1569 		goto done0;
1570 	}
1571 	rfp = &xfer->recv.hdr;
1572 	if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1573 		kprintf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1574 		goto done0;
1575 	}
1576 	sbp_status = (struct sbp_status *)xfer->recv.payload;
1577 	addr = rfp->mode.wreqb.dest_lo;
1578 SBP_DEBUG(2)
1579 	kprintf("received address 0x%x\n", addr);
1580 END_DEBUG
1581 	t = SBP_ADDR2TRG(addr);
1582 	if (t >= SBP_NUM_TARGETS) {
1583 		device_printf(sbp->fd.dev,
1584 			"sbp_recv1: invalid target %d\n", t);
1585 		goto done0;
1586 	}
1587 	target = &sbp->targets[t];
1588 	l = SBP_ADDR2LUN(addr);
1589 	if (l >= target->num_lun || target->luns[l] == NULL) {
1590 		device_printf(sbp->fd.dev,
1591 			"sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1592 		goto done0;
1593 	}
1594 	sdev = target->luns[l];
1595 
1596 	ocb = NULL;
1597 	switch (sbp_status->src) {
1598 	case 0:
1599 	case 1:
1600 		/* check mgm_ocb_cur first */
1601 		ocb  = target->mgm_ocb_cur;
1602 		if (ocb != NULL) {
1603 			if (OCB_MATCH(ocb, sbp_status)) {
1604 				callout_stop(&target->mgm_ocb_timeout);
1605 				target->mgm_ocb_cur = NULL;
1606 				break;
1607 			}
1608 		}
1609 		ocb = sbp_dequeue_ocb(sdev, sbp_status);
1610 		if (ocb == NULL) {
1611 			sbp_show_sdev_info(sdev, 2);
1612 			kprintf("No ocb(%x) on the queue\n",
1613 					ntohl(sbp_status->orb_lo));
1614 		}
1615 		break;
1616 	case 2:
1617 		/* unsolicit */
1618 		sbp_show_sdev_info(sdev, 2);
1619 		kprintf("unsolicit status received\n");
1620 		break;
1621 	default:
1622 		sbp_show_sdev_info(sdev, 2);
1623 		kprintf("unknown sbp_status->src\n");
1624 	}
1625 
1626 	status_valid0 = (sbp_status->src < 2
1627 			&& sbp_status->resp == ORB_RES_CMPL
1628 			&& sbp_status->dead == 0);
1629 	status_valid = (status_valid0 && sbp_status->status == 0);
1630 
1631 	if (!status_valid0 || debug > 2){
1632 		int status;
1633 SBP_DEBUG(0)
1634 		sbp_show_sdev_info(sdev, 2);
1635 		kprintf("ORB status src:%x resp:%x dead:%x"
1636 				" len:%x stat:%x orb:%x%08x\n",
1637 			sbp_status->src, sbp_status->resp, sbp_status->dead,
1638 			sbp_status->len, sbp_status->status,
1639 			ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1640 END_DEBUG
1641 		sbp_show_sdev_info(sdev, 2);
1642 		status = sbp_status->status;
1643 		switch(sbp_status->resp) {
1644 		case 0:
1645 			if (status > MAX_ORB_STATUS0)
1646 				kprintf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1647 			else
1648 				kprintf("%s\n", orb_status0[status]);
1649 			break;
1650 		case 1:
1651 			kprintf("Obj: %s, Error: %s\n",
1652 				orb_status1_object[(status>>6) & 3],
1653 				orb_status1_serial_bus_error[status & 0xf]);
1654 			break;
1655 		case 2:
1656 			kprintf("Illegal request\n");
1657 			break;
1658 		case 3:
1659 			kprintf("Vendor dependent\n");
1660 			break;
1661 		default:
1662 			kprintf("unknown respose code %d\n", sbp_status->resp);
1663 		}
1664 	}
1665 
1666 	/* we have to reset the fetch agent if it's dead */
1667 	if (sbp_status->dead) {
1668 		if (sdev->path) {
1669 			xpt_freeze_devq(sdev->path, 1);
1670 			sdev->freeze ++;
1671 		}
1672 		reset_agent = 1;
1673 	}
1674 
1675 	if (ocb == NULL)
1676 		goto done;
1677 
1678 	switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1679 	case ORB_FMT_NOP:
1680 		break;
1681 	case ORB_FMT_VED:
1682 		break;
1683 	case ORB_FMT_STD:
1684 		switch(ocb->flags) {
1685 		case OCB_ACT_MGM:
1686 			orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1687 			reset_agent = 0;
1688 			switch(orb_fun) {
1689 			case ORB_FUN_LGI:
1690 				fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD);
1691 				login_res = sdev->login;
1692 				login_res->len = ntohs(login_res->len);
1693 				login_res->id = ntohs(login_res->id);
1694 				login_res->cmd_hi = ntohs(login_res->cmd_hi);
1695 				login_res->cmd_lo = ntohl(login_res->cmd_lo);
1696 				if (status_valid) {
1697 SBP_DEBUG(0)
1698 sbp_show_sdev_info(sdev, 2);
1699 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));
1700 END_DEBUG
1701 					sbp_busy_timeout(sdev);
1702 				} else {
1703 					/* forgot logout? */
1704 					sbp_show_sdev_info(sdev, 2);
1705 					kprintf("login failed\n");
1706 					sdev->status = SBP_DEV_RESET;
1707 				}
1708 				break;
1709 			case ORB_FUN_RCN:
1710 				login_res = sdev->login;
1711 				if (status_valid) {
1712 SBP_DEBUG(0)
1713 sbp_show_sdev_info(sdev, 2);
1714 kprintf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo);
1715 END_DEBUG
1716 #if 1
1717 					if (sdev->status == SBP_DEV_ATTACHED)
1718 						sbp_scan_dev(sdev);
1719 					else
1720 						sbp_agent_reset(sdev);
1721 #else
1722 					sdev->status = SBP_DEV_ATTACHED;
1723 					sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL);
1724 #endif
1725 				} else {
1726 					/* reconnection hold time exceed? */
1727 SBP_DEBUG(0)
1728 					sbp_show_sdev_info(sdev, 2);
1729 					kprintf("reconnect failed\n");
1730 END_DEBUG
1731 					sbp_login(sdev);
1732 				}
1733 				break;
1734 			case ORB_FUN_LGO:
1735 				sdev->status = SBP_DEV_RESET;
1736 				break;
1737 			case ORB_FUN_RST:
1738 				sbp_busy_timeout(sdev);
1739 				break;
1740 			case ORB_FUN_LUR:
1741 			case ORB_FUN_ATA:
1742 			case ORB_FUN_ATS:
1743 				sbp_agent_reset(sdev);
1744 				break;
1745 			default:
1746 				sbp_show_sdev_info(sdev, 2);
1747 				kprintf("unknown function %d\n", orb_fun);
1748 				break;
1749 			}
1750 			sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
1751 			break;
1752 		case OCB_ACT_CMD:
1753 			sdev->timeout = 0;
1754 			if(ocb->ccb != NULL){
1755 				union ccb *ccb;
1756 /*
1757 				u_int32_t *ld;
1758 				ld = ocb->ccb->csio.data_ptr;
1759 				if(ld != NULL && ocb->ccb->csio.dxfer_len != 0)
1760 					kprintf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]);
1761 				else
1762 					kprintf("ptr NULL\n");
1763 kprintf("len %d\n", sbp_status->len);
1764 */
1765 				ccb = ocb->ccb;
1766 				if(sbp_status->len > 1){
1767 					sbp_scsi_status(sbp_status, ocb);
1768 				}else{
1769 					if(sbp_status->resp != ORB_RES_CMPL){
1770 						ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1771 					}else{
1772 						ccb->ccb_h.status = CAM_REQ_CMP;
1773 					}
1774 				}
1775 				/* fix up inq data */
1776 				if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1777 					sbp_fix_inq_data(ocb);
1778 				xpt_done(ccb);
1779 			}
1780 			break;
1781 		default:
1782 			break;
1783 		}
1784 	}
1785 
1786 	sbp_free_ocb(sdev, ocb);
1787 done:
1788 	if (reset_agent)
1789 		sbp_agent_reset(sdev);
1790 
1791 done0:
1792 	xfer->recv.pay_len = SBP_RECV_LEN;
1793 /* The received packet is usually small enough to be stored within
1794  * the buffer. In that case, the controller return ack_complete and
1795  * no respose is necessary.
1796  *
1797  * XXX fwohci.c and firewire.c should inform event_code such as
1798  * ack_complete or ack_pending to upper driver.
1799  */
1800 #if NEED_RESPONSE
1801 	xfer->send.off = 0;
1802 	sfp = (struct fw_pkt *)xfer->send.buf;
1803 	sfp->mode.wres.dst = rfp->mode.wreqb.src;
1804 	xfer->dst = sfp->mode.wres.dst;
1805 	xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1806 	xfer->act.hand = sbp_loginres_callback;
1807 	xfer->retry_req = fw_asybusy;
1808 
1809 	sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1810 	sfp->mode.wres.tcode = FWTCODE_WRES;
1811 	sfp->mode.wres.rtcode = 0;
1812 	sfp->mode.wres.pri = 0;
1813 
1814 	fw_asyreq(xfer->fc, -1, xfer);
1815 #else
1816 	/* recycle */
1817 	STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1818 #endif
1819 
1820 	return;
1821 
1822 }
1823 
1824 static void
1825 sbp_recv(struct fw_xfer *xfer)
1826 {
1827 	crit_enter();
1828 	sbp_recv1(xfer);
1829 	crit_exit();
1830 }
1831 /*
1832  * sbp_attach()
1833  */
1834 static int
1835 sbp_attach(device_t dev)
1836 {
1837 	struct sbp_softc *sbp;
1838 	struct cam_devq *devq;
1839 	struct fw_xfer *xfer;
1840 	int i, error;
1841 
1842 SBP_DEBUG(0)
1843 	kprintf("sbp_attach (cold=%d)\n", cold);
1844 END_DEBUG
1845 
1846 #if 0
1847 	if (cold)
1848 		sbp_cold ++;
1849 #endif
1850 	sbp = ((struct sbp_softc *)device_get_softc(dev));
1851 	bzero(sbp, sizeof(struct sbp_softc));
1852 	sbp->fd.dev = dev;
1853 	sbp->fd.fc = device_get_ivars(dev);
1854 
1855 	if (max_speed < 0)
1856 		max_speed = sbp->fd.fc->speed;
1857 
1858 	error = bus_dma_tag_create(/*parent*/sbp->fd.fc->dmat,
1859 				/* XXX shoud be 4 for sane backend? */
1860 				/*alignment*/1,
1861 				/*boundary*/0,
1862 				/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1863 				/*highaddr*/BUS_SPACE_MAXADDR,
1864 				/*filter*/NULL, /*filterarg*/NULL,
1865 				/*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1866 				/*maxsegsz*/SBP_SEG_MAX,
1867 				/*flags*/BUS_DMA_ALLOCNOW,
1868 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
1869 				/*lockfunc*/busdma_lock_mutex,
1870 				/*lockarg*/&Giant,
1871 #endif
1872 				&sbp->dmat);
1873 	if (error != 0) {
1874 		kprintf("sbp_attach: Could not allocate DMA tag "
1875 			"- error %d\n", error);
1876 			return (ENOMEM);
1877 	}
1878 
1879 	devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1880 	if (devq == NULL)
1881 		return (ENXIO);
1882 
1883 	for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1884 		sbp->targets[i].fwdev = NULL;
1885 		sbp->targets[i].luns = NULL;
1886 	}
1887 
1888 	sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1889 				 device_get_unit(dev),
1890 				 &sim_mplock,
1891 				 /*untagged*/ 1,
1892 				 /*tagged*/ SBP_QUEUE_LEN - 1,
1893 				 devq);
1894 	cam_simq_release(devq);
1895 	if (sbp->sim == NULL)
1896 		return (ENXIO);
1897 
1898 	if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS)
1899 		goto fail;
1900 
1901 	if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim),
1902 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1903 		xpt_bus_deregister(cam_sim_path(sbp->sim));
1904 		goto fail;
1905 	}
1906 
1907 	/* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1908 	sbp->fwb.start = ((u_int64_t)SBP_BIND_HI << 32) | SBP_DEV2ADDR(0, 0);
1909 	sbp->fwb.end = sbp->fwb.start + 0xffff;
1910 	sbp->fwb.act_type = FWACT_XFER;
1911 	/* pre-allocate xfer */
1912 	STAILQ_INIT(&sbp->fwb.xferlist);
1913 	for (i = 0; i < SBP_NUM_OCB/2; i ++) {
1914 		xfer = fw_xfer_alloc_buf(M_SBP,
1915 			/* send */0,
1916 			/* recv */SBP_RECV_LEN);
1917 		xfer->act.hand = sbp_recv;
1918 #if NEED_RESPONSE
1919 		xfer->fc = sbp->fd.fc;
1920 #endif
1921 		xfer->sc = (caddr_t)sbp;
1922 		STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1923 	}
1924 	fw_bindadd(sbp->fd.fc, &sbp->fwb);
1925 
1926 	sbp->fd.post_busreset = sbp_post_busreset;
1927 	sbp->fd.post_explore = sbp_post_explore;
1928 
1929 	if (sbp->fd.fc->status != -1) {
1930 		crit_enter();
1931 		sbp_post_busreset((void *)sbp);
1932 		sbp_post_explore((void *)sbp);
1933 		crit_exit();
1934 	}
1935 	xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
1936 
1937 	return (0);
1938 fail:
1939 	cam_sim_free(sbp->sim);
1940 	return (ENXIO);
1941 }
1942 
1943 static int
1944 sbp_logout_all(struct sbp_softc *sbp)
1945 {
1946 	struct sbp_target *target;
1947 	struct sbp_dev *sdev;
1948 	int i, j;
1949 
1950 SBP_DEBUG(0)
1951 	kprintf("sbp_logout_all\n");
1952 END_DEBUG
1953 	for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
1954 		target = &sbp->targets[i];
1955 		if (target->luns == NULL)
1956 			continue;
1957 		for (j = 0; j < target->num_lun; j++) {
1958 			sdev = target->luns[j];
1959 			if (sdev == NULL)
1960 				continue;
1961 			callout_stop(&sdev->login_callout);
1962 			if (sdev->status >= SBP_DEV_TOATTACH &&
1963 					sdev->status <= SBP_DEV_ATTACHED)
1964 				sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
1965 		}
1966 	}
1967 
1968 	return 0;
1969 }
1970 
1971 static int
1972 sbp_shutdown(device_t dev)
1973 {
1974 	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1975 
1976 	sbp_logout_all(sbp);
1977 	return (0);
1978 }
1979 
1980 static void
1981 sbp_free_sdev(struct sbp_dev *sdev)
1982 {
1983 	int i;
1984 
1985 	if (sdev == NULL)
1986 		return;
1987 	for (i = 0; i < SBP_QUEUE_LEN; i++)
1988 		bus_dmamap_destroy(sdev->target->sbp->dmat,
1989 		    sdev->ocb[i].dmamap);
1990 	fwdma_free(sdev->target->sbp->fd.fc, &sdev->dma);
1991 	kfree(sdev, M_SBP);
1992 }
1993 
1994 static void
1995 sbp_free_target(struct sbp_target *target)
1996 {
1997 	struct sbp_softc *sbp;
1998 	struct fw_xfer *xfer, *next;
1999 	int i;
2000 
2001 	if (target->luns == NULL)
2002 		return;
2003 	callout_stop(&target->mgm_ocb_timeout);
2004 	sbp = target->sbp;
2005 	for (i = 0; i < target->num_lun; i++)
2006 		sbp_free_sdev(target->luns[i]);
2007 
2008 	for (xfer = STAILQ_FIRST(&target->xferlist);
2009 			xfer != NULL; xfer = next) {
2010 		next = STAILQ_NEXT(xfer, link);
2011 		fw_xfer_free_buf(xfer);
2012 	}
2013 	STAILQ_INIT(&target->xferlist);
2014 	kfree(target->luns, M_SBP);
2015 	target->num_lun = 0;
2016 	target->luns = NULL;
2017 	target->fwdev = NULL;
2018 }
2019 
2020 static int
2021 sbp_detach(device_t dev)
2022 {
2023 	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2024 	struct firewire_comm *fc = sbp->fd.fc;
2025 	struct fw_xfer *xfer, *next;
2026 	int i;
2027 
2028 SBP_DEBUG(0)
2029 	kprintf("sbp_detach\n");
2030 END_DEBUG
2031 
2032 	for (i = 0; i < SBP_NUM_TARGETS; i ++)
2033 		sbp_cam_detach_target(&sbp->targets[i]);
2034 	xpt_async(AC_LOST_DEVICE, sbp->path, NULL);
2035 	xpt_free_path(sbp->path);
2036 	xpt_bus_deregister(cam_sim_path(sbp->sim));
2037 	cam_sim_free(sbp->sim);
2038 
2039 	sbp_logout_all(sbp);
2040 
2041 	/* XXX wait for logout completion */
2042 	tsleep(&i, FWPRI, "sbpdtc", hz/2);
2043 
2044 	for (i = 0 ; i < SBP_NUM_TARGETS ; i ++)
2045 		sbp_free_target(&sbp->targets[i]);
2046 
2047 	for (xfer = STAILQ_FIRST(&sbp->fwb.xferlist);
2048 				xfer != NULL; xfer = next) {
2049 		next = STAILQ_NEXT(xfer, link);
2050 		fw_xfer_free_buf(xfer);
2051 	}
2052 	STAILQ_INIT(&sbp->fwb.xferlist);
2053 	fw_bindremove(fc, &sbp->fwb);
2054 
2055 	bus_dma_tag_destroy(sbp->dmat);
2056 
2057 	return (0);
2058 }
2059 
2060 static void
2061 sbp_cam_detach_sdev(struct sbp_dev *sdev)
2062 {
2063 	if (sdev == NULL)
2064 		return;
2065 	if (sdev->status == SBP_DEV_DEAD)
2066 		return;
2067 	if (sdev->status == SBP_DEV_RESET)
2068 		return;
2069 	if (sdev->path) {
2070 		xpt_release_devq(sdev->path,
2071 				 sdev->freeze, TRUE);
2072 		sdev->freeze = 0;
2073 		xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
2074 		xpt_free_path(sdev->path);
2075 		sdev->path = NULL;
2076 	}
2077 	sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
2078 }
2079 
2080 static void
2081 sbp_cam_detach_target(struct sbp_target *target)
2082 {
2083 	int i;
2084 
2085 	if (target->luns != NULL) {
2086 SBP_DEBUG(0)
2087 		kprintf("sbp_detach_target %d\n", target->target_id);
2088 END_DEBUG
2089 		callout_stop(&target->scan_callout);
2090 		for (i = 0; i < target->num_lun; i++)
2091 			sbp_cam_detach_sdev(target->luns[i]);
2092 	}
2093 }
2094 
2095 static void
2096 sbp_target_reset(struct sbp_dev *sdev, int method)
2097 {
2098 	int i;
2099 	struct sbp_target *target = sdev->target;
2100 	struct sbp_dev *tsdev;
2101 
2102 	for (i = 0; i < target->num_lun; i++) {
2103 		tsdev = target->luns[i];
2104 		if (tsdev == NULL)
2105 			continue;
2106 		if (tsdev->status == SBP_DEV_DEAD)
2107 			continue;
2108 		if (tsdev->status == SBP_DEV_RESET)
2109 			continue;
2110 		xpt_freeze_devq(tsdev->path, 1);
2111 		tsdev->freeze ++;
2112 		sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT);
2113 		if (method == 2)
2114 			tsdev->status = SBP_DEV_LOGIN;
2115 	}
2116 	switch(method) {
2117 	case 1:
2118 		kprintf("target reset\n");
2119 		sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2120 		break;
2121 	case 2:
2122 		kprintf("reset start\n");
2123 		sbp_reset_start(sdev);
2124 		break;
2125 	}
2126 
2127 }
2128 
2129 static void
2130 sbp_mgm_timeout(void *arg)
2131 {
2132 	struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2133 	struct sbp_dev *sdev = ocb->sdev;
2134 	struct sbp_target *target = sdev->target;
2135 
2136 	sbp_show_sdev_info(sdev, 2);
2137 	kprintf("request timeout(mgm orb:0x%08x) ... ",
2138 	    (u_int32_t)ocb->bus_addr);
2139 	target->mgm_ocb_cur = NULL;
2140 	sbp_free_ocb(sdev, ocb);
2141 #if 0
2142 	/* XXX */
2143 	kprintf("run next request\n");
2144 	sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2145 #endif
2146 #if 1
2147 	kprintf("reset start\n");
2148 	sbp_reset_start(sdev);
2149 #endif
2150 }
2151 
2152 static void
2153 sbp_timeout(void *arg)
2154 {
2155 	struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2156 	struct sbp_dev *sdev = ocb->sdev;
2157 
2158 	sbp_show_sdev_info(sdev, 2);
2159 	kprintf("request timeout(cmd orb:0x%08x) ... ",
2160 	    (u_int32_t)ocb->bus_addr);
2161 
2162 	sdev->timeout ++;
2163 	switch(sdev->timeout) {
2164 	case 1:
2165 		kprintf("agent reset\n");
2166 		xpt_freeze_devq(sdev->path, 1);
2167 		sdev->freeze ++;
2168 		sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2169 		sbp_agent_reset(sdev);
2170 		break;
2171 	case 2:
2172 	case 3:
2173 		sbp_target_reset(sdev, sdev->timeout - 1);
2174 		break;
2175 #if 0
2176 	default:
2177 		/* XXX give up */
2178 		sbp_cam_detach_target(target);
2179 		if (target->luns != NULL)
2180 			kfree(target->luns, M_SBP);
2181 		target->num_lun = 0;
2182 		target->luns = NULL;
2183 		target->fwdev = NULL;
2184 #endif
2185 	}
2186 }
2187 
2188 static void
2189 sbp_action1(struct cam_sim *sim, union ccb *ccb)
2190 {
2191 
2192 	struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2193 	struct sbp_target *target = NULL;
2194 	struct sbp_dev *sdev = NULL;
2195 
2196 	/* target:lun -> sdev mapping */
2197 	if (sbp != NULL
2198 			&& ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2199 			&& ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2200 		target = &sbp->targets[ccb->ccb_h.target_id];
2201 		if (target->fwdev != NULL
2202 				&& ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2203 				&& ccb->ccb_h.target_lun < target->num_lun) {
2204 			sdev = target->luns[ccb->ccb_h.target_lun];
2205 			if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED &&
2206 				sdev->status != SBP_DEV_PROBE)
2207 				sdev = NULL;
2208 		}
2209 	}
2210 
2211 SBP_DEBUG(1)
2212 	if (sdev == NULL)
2213 		kprintf("invalid target %d lun %d\n",
2214 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2215 END_DEBUG
2216 
2217 	switch (ccb->ccb_h.func_code) {
2218 	case XPT_SCSI_IO:
2219 	case XPT_RESET_DEV:
2220 	case XPT_GET_TRAN_SETTINGS:
2221 	case XPT_SET_TRAN_SETTINGS:
2222 	case XPT_CALC_GEOMETRY:
2223 		if (sdev == NULL) {
2224 SBP_DEBUG(1)
2225 			kprintf("%s:%d:%d:func_code 0x%04x: "
2226 				"Invalid target (target needed)\n",
2227 				device_get_nameunit(sbp->fd.dev),
2228 				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2229 				ccb->ccb_h.func_code);
2230 END_DEBUG
2231 
2232 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2233 			xpt_done(ccb);
2234 			return;
2235 		}
2236 		break;
2237 	case XPT_PATH_INQ:
2238 	case XPT_NOOP:
2239 		/* The opcodes sometimes aimed at a target (sc is valid),
2240 		 * sometimes aimed at the SIM (sc is invalid and target is
2241 		 * CAM_TARGET_WILDCARD)
2242 		 */
2243 		if (sbp == NULL &&
2244 			ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2245 SBP_DEBUG(0)
2246 			kprintf("%s:%d:%d func_code 0x%04x: "
2247 				"Invalid target (no wildcard)\n",
2248 				device_get_nameunit(sbp->fd.dev),
2249 				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2250 				ccb->ccb_h.func_code);
2251 END_DEBUG
2252 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2253 			xpt_done(ccb);
2254 			return;
2255 		}
2256 		break;
2257 	default:
2258 		/* XXX Hm, we should check the input parameters */
2259 		break;
2260 	}
2261 
2262 	switch (ccb->ccb_h.func_code) {
2263 	case XPT_SCSI_IO:
2264 	{
2265 		struct ccb_scsiio *csio;
2266 		struct sbp_ocb *ocb;
2267 		int speed;
2268 		void *cdb;
2269 
2270 		csio = &ccb->csio;
2271 
2272 SBP_DEBUG(2)
2273 		kprintf("%s:%d:%d XPT_SCSI_IO: "
2274 			"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2275 			", flags: 0x%02x, "
2276 			"%db cmd/%db data/%db sense\n",
2277 			device_get_nameunit(sbp->fd.dev),
2278 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2279 			csio->cdb_io.cdb_bytes[0],
2280 			csio->cdb_io.cdb_bytes[1],
2281 			csio->cdb_io.cdb_bytes[2],
2282 			csio->cdb_io.cdb_bytes[3],
2283 			csio->cdb_io.cdb_bytes[4],
2284 			csio->cdb_io.cdb_bytes[5],
2285 			csio->cdb_io.cdb_bytes[6],
2286 			csio->cdb_io.cdb_bytes[7],
2287 			csio->cdb_io.cdb_bytes[8],
2288 			csio->cdb_io.cdb_bytes[9],
2289 			ccb->ccb_h.flags & CAM_DIR_MASK,
2290 			csio->cdb_len, csio->dxfer_len,
2291 			csio->sense_len);
2292 END_DEBUG
2293 		if(sdev == NULL){
2294 			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2295 			xpt_done(ccb);
2296 			return;
2297 		}
2298 #if 0
2299 		/* if we are in probe stage, pass only probe commands */
2300 		if (sdev->status == SBP_DEV_PROBE) {
2301 			char *name;
2302 			name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2303 			kprintf("probe stage, periph name: %s\n", name);
2304 			if (strcmp(name, "probe") != 0) {
2305 				ccb->ccb_h.status = CAM_REQUEUE_REQ;
2306 				xpt_done(ccb);
2307 				return;
2308 			}
2309 		}
2310 #endif
2311 		if ((ocb = sbp_get_ocb(sdev)) == NULL) {
2312 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
2313 			xpt_done(ccb);
2314 			return;
2315 		}
2316 
2317 		ocb->flags = OCB_ACT_CMD;
2318 		ocb->sdev = sdev;
2319 		ocb->ccb = ccb;
2320 		ccb->ccb_h.ccb_sdev_ptr = sdev;
2321 		ocb->orb[0] = htonl(1 << 31);
2322 		ocb->orb[1] = 0;
2323 		ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2324 		ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2325 		speed = min(target->fwdev->speed, max_speed);
2326 		ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2327 						| ORB_CMD_MAXP(speed + 7));
2328 		if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
2329 			ocb->orb[4] |= htonl(ORB_CMD_IN);
2330 		}
2331 
2332 		if (csio->ccb_h.flags & CAM_SCATTER_VALID)
2333 			kprintf("sbp: CAM_SCATTER_VALID\n");
2334 		if (csio->ccb_h.flags & CAM_DATA_PHYS)
2335 			kprintf("sbp: CAM_DATA_PHYS\n");
2336 
2337 		if (csio->ccb_h.flags & CAM_CDB_POINTER)
2338 			cdb = (void *)csio->cdb_io.cdb_ptr;
2339 		else
2340 			cdb = (void *)&csio->cdb_io.cdb_bytes;
2341 		bcopy(cdb, (void *)&ocb->orb[5], csio->cdb_len);
2342 /*
2343 kprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2344 kprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2345 */
2346 		if (ccb->csio.dxfer_len > 0) {
2347 			int error;
2348 
2349 			crit_enter();
2350 			error = bus_dmamap_load(/*dma tag*/sbp->dmat,
2351 					/*dma map*/ocb->dmamap,
2352 					ccb->csio.data_ptr,
2353 					ccb->csio.dxfer_len,
2354 					sbp_execute_ocb,
2355 					ocb,
2356 					/*flags*/0);
2357 			crit_exit();
2358 			if (error)
2359 				kprintf("sbp: bus_dmamap_load error %d\n", error);
2360 		} else
2361 			sbp_execute_ocb(ocb, NULL, 0, 0);
2362 		break;
2363 	}
2364 	case XPT_CALC_GEOMETRY:
2365 	{
2366 		struct ccb_calc_geometry *ccg;
2367 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2368 		u_int32_t size_mb;
2369 		u_int32_t secs_per_cylinder;
2370 		int extended = 1;
2371 #endif
2372 
2373 		ccg = &ccb->ccg;
2374 		if (ccg->block_size == 0) {
2375 			kprintf("sbp_action1: block_size is 0.\n");
2376 			ccb->ccb_h.status = CAM_REQ_INVALID;
2377 			xpt_done(ccb);
2378 			break;
2379 		}
2380 SBP_DEBUG(1)
2381 		kprintf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: Volume size = %ju\n",
2382 			device_get_nameunit(sbp->fd.dev),
2383 			cam_sim_path(sbp->sim),
2384 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2385 			(uintmax_t)ccg->volume_size);
2386 END_DEBUG
2387 
2388 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2389 		size_mb = ccg->volume_size
2390 			/ ((1024L * 1024L) / ccg->block_size);
2391 
2392 		if (size_mb > 1024 && extended) {
2393 			ccg->heads = 255;
2394 			ccg->secs_per_track = 63;
2395 		} else {
2396 			ccg->heads = 64;
2397 			ccg->secs_per_track = 32;
2398 		}
2399 		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2400 		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2401 		ccb->ccb_h.status = CAM_REQ_CMP;
2402 #else
2403 		cam_calc_geometry(ccg, /*extended*/1);
2404 #endif
2405 		xpt_done(ccb);
2406 		break;
2407 	}
2408 	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
2409 	{
2410 
2411 SBP_DEBUG(1)
2412 		kprintf("%s:%d:XPT_RESET_BUS: \n",
2413 			device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2414 END_DEBUG
2415 
2416 		ccb->ccb_h.status = CAM_REQ_INVALID;
2417 		xpt_done(ccb);
2418 		break;
2419 	}
2420 	case XPT_PATH_INQ:		/* Path routing inquiry */
2421 	{
2422 		struct ccb_pathinq *cpi = &ccb->cpi;
2423 
2424 SBP_DEBUG(1)
2425 		kprintf("%s:%d:%d XPT_PATH_INQ:.\n",
2426 			device_get_nameunit(sbp->fd.dev),
2427 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2428 END_DEBUG
2429 		cpi->version_num = 1; /* XXX??? */
2430 		cpi->hba_inquiry = PI_TAG_ABLE;
2431 		cpi->target_sprt = 0;
2432 		cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE;
2433 		cpi->hba_eng_cnt = 0;
2434 		cpi->max_target = SBP_NUM_TARGETS - 1;
2435 		cpi->max_lun = SBP_NUM_LUNS - 1;
2436 		cpi->initiator_id = SBP_INITIATOR;
2437 		cpi->bus_id = sim->bus_id;
2438 		cpi->base_transfer_speed = 400 * 1000 / 8;
2439 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2440 		strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2441 		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2442 		cpi->unit_number = sim->unit_number;
2443                 cpi->transport = XPORT_SPI;	/* XX should have a FireWire */
2444                 cpi->transport_version = 2;
2445                 cpi->protocol = PROTO_SCSI;
2446                 cpi->protocol_version = SCSI_REV_2;
2447 
2448 		cpi->ccb_h.status = CAM_REQ_CMP;
2449 		xpt_done(ccb);
2450 		break;
2451 	}
2452 	case XPT_GET_TRAN_SETTINGS:
2453 	{
2454 		struct ccb_trans_settings *cts = &ccb->cts;
2455 		struct ccb_trans_settings_scsi *scsi =
2456 		    &cts->proto_specific.scsi;
2457 		struct ccb_trans_settings_spi *spi =
2458 		    &cts->xport_specific.spi;
2459 
2460 		cts->protocol = PROTO_SCSI;
2461 		cts->protocol_version = SCSI_REV_2;
2462 		cts->transport = XPORT_SPI;	/* should have a FireWire */
2463 		cts->transport_version = 2;
2464 		spi->valid = CTS_SPI_VALID_DISC;
2465 		spi->flags = CTS_SPI_FLAGS_DISC_ENB;
2466 		scsi->valid = CTS_SCSI_VALID_TQ;
2467 		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2468 SBP_DEBUG(1)
2469 		kprintf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2470 			device_get_nameunit(sbp->fd.dev),
2471 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2472 END_DEBUG
2473 		cts->ccb_h.status = CAM_REQ_CMP;
2474 		xpt_done(ccb);
2475 		break;
2476 	}
2477 	case XPT_ABORT:
2478 		ccb->ccb_h.status = CAM_UA_ABORT;
2479 		xpt_done(ccb);
2480 		break;
2481 	case XPT_SET_TRAN_SETTINGS:
2482 		/* XXX */
2483 	default:
2484 		ccb->ccb_h.status = CAM_REQ_INVALID;
2485 		xpt_done(ccb);
2486 		break;
2487 	}
2488 	return;
2489 }
2490 
2491 static void
2492 sbp_action(struct cam_sim *sim, union ccb *ccb)
2493 {
2494 	crit_enter();
2495 	sbp_action1(sim, ccb);
2496 	crit_exit();
2497 }
2498 
2499 static void
2500 sbp_execute_ocb(void *arg,  bus_dma_segment_t *segments, int seg, int error)
2501 {
2502 	int i;
2503 	struct sbp_ocb *ocb;
2504 	struct sbp_ocb *prev;
2505 	bus_dma_segment_t *s;
2506 
2507 	if (error)
2508 		kprintf("sbp_execute_ocb: error=%d\n", error);
2509 
2510 	ocb = (struct sbp_ocb *)arg;
2511 
2512 SBP_DEBUG(2)
2513 	kprintf("sbp_execute_ocb: seg %d", seg);
2514 	for (i = 0; i < seg; i++)
2515 		kprintf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2516 					(uintmax_t)segments[i].ds_len);
2517 	kprintf("\n");
2518 END_DEBUG
2519 
2520 	if (seg == 1) {
2521 		/* direct pointer */
2522 		s = &segments[0];
2523 		if (s->ds_len > SBP_SEG_MAX)
2524 			panic("ds_len > SBP_SEG_MAX, fix busdma code");
2525 		ocb->orb[3] = htonl(s->ds_addr);
2526 		ocb->orb[4] |= htonl(s->ds_len);
2527 	} else if(seg > 1) {
2528 		/* page table */
2529 		for (i = 0; i < seg; i++) {
2530 			s = &segments[i];
2531 SBP_DEBUG(0)
2532 			/* XXX LSI Logic "< 16 byte" bug might be hit */
2533 			if (s->ds_len < 16)
2534 				kprintf("sbp_execute_ocb: warning, "
2535 					"segment length(%zd) is less than 16."
2536 					"(seg=%d/%jd)\n",
2537 					(size_t)s->ds_len, i+1, (intmax_t)seg);
2538 END_DEBUG
2539 			if (s->ds_len > SBP_SEG_MAX)
2540 				panic("ds_len > SBP_SEG_MAX, fix busdma code");
2541 			ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2542 			ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2543 		}
2544 		ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2545 	}
2546 
2547 	if (seg > 0)
2548 		bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2549 			(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2550 			BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2551 	prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2552 	fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2553 	if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) {
2554 		ocb->sdev->flags &= ~ORB_LINK_DEAD;
2555 		sbp_orb_pointer(ocb->sdev, ocb);
2556 	}
2557 }
2558 
2559 static void
2560 sbp_poll(struct cam_sim *sim)
2561 {
2562 	struct sbp_softc *sbp;
2563 	struct firewire_comm *fc;
2564 
2565 	sbp = (struct sbp_softc *)sim->softc;
2566 	fc = sbp->fd.fc;
2567 
2568 	fc->poll(fc, 0, -1);
2569 
2570 	return;
2571 }
2572 
2573 static struct sbp_ocb *
2574 sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2575 {
2576 	struct sbp_ocb *ocb;
2577 	struct sbp_ocb *next;
2578 	int order = 0;
2579 	int flags;
2580 
2581 	crit_enter();
2582 
2583 SBP_DEBUG(1)
2584 	sbp_show_sdev_info(sdev, 2);
2585 	kprintf("%s: 0x%08x src %d\n",
2586 	    __func__, ntohl(sbp_status->orb_lo), sbp_status->src);
2587 END_DEBUG
2588 	for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2589 		next = STAILQ_NEXT(ocb, ocb);
2590 		flags = ocb->flags;
2591 		if (OCB_MATCH(ocb, sbp_status)) {
2592 			/* found */
2593 			STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2594 			if (ocb->ccb != NULL)
2595 				callout_stop(&ocb->ccb->ccb_h.timeout_ch);
2596 			if (ntohl(ocb->orb[4]) & 0xffff) {
2597 				bus_dmamap_sync(sdev->target->sbp->dmat,
2598 					ocb->dmamap,
2599 					(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2600 					BUS_DMASYNC_POSTREAD :
2601 					BUS_DMASYNC_POSTWRITE);
2602 				bus_dmamap_unload(sdev->target->sbp->dmat,
2603 					ocb->dmamap);
2604 			}
2605 			if (sbp_status->src == SRC_NO_NEXT) {
2606 				if (next != NULL)
2607 					sbp_orb_pointer(sdev, next);
2608 				else if (order > 0) {
2609 					/*
2610 					 * Unordered execution
2611 					 * We need to send pointer for
2612 					 * next ORB
2613 					 */
2614 					sdev->flags |= ORB_LINK_DEAD;
2615 				}
2616 			}
2617 			break;
2618 		} else
2619 			order ++;
2620 	}
2621 	crit_exit();
2622 SBP_DEBUG(0)
2623 	if (ocb && order > 0) {
2624 		sbp_show_sdev_info(sdev, 2);
2625 		kprintf("unordered execution order:%d\n", order);
2626 	}
2627 END_DEBUG
2628 	return (ocb);
2629 }
2630 
2631 static struct sbp_ocb *
2632 sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2633 {
2634 	struct sbp_ocb *prev;
2635 
2636 	crit_enter();
2637 
2638 SBP_DEBUG(1)
2639 	sbp_show_sdev_info(sdev, 2);
2640 	kprintf("%s: 0x%08jx\n", __func__, (uintmax_t)ocb->bus_addr);
2641 END_DEBUG
2642 	prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2643 	STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2644 
2645 	if (ocb->ccb != NULL)
2646 		callout_reset(&ocb->ccb->ccb_h.timeout_ch,
2647 		    (ocb->ccb->ccb_h.timeout * hz) / 1000, sbp_timeout, ocb);
2648 
2649 	if (prev != NULL) {
2650 SBP_DEBUG(2)
2651 		kprintf("linking chain 0x%jx -> 0x%jx\n",
2652 		    (uintmax_t)prev->bus_addr, (uintmax_t)ocb->bus_addr);
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 		crit_exit();
2672 		return NULL;
2673 	}
2674 	STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2675 	crit_exit();
2676 	ocb->ccb = NULL;
2677 	return (ocb);
2678 }
2679 
2680 static void
2681 sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2682 {
2683 	ocb->flags = 0;
2684 	ocb->ccb = NULL;
2685 	STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2686 }
2687 
2688 static void
2689 sbp_abort_ocb(struct sbp_ocb *ocb, int status)
2690 {
2691 	struct sbp_dev *sdev;
2692 
2693 	sdev = ocb->sdev;
2694 SBP_DEBUG(0)
2695 	sbp_show_sdev_info(sdev, 2);
2696 	kprintf("sbp_abort_ocb 0x%jx\n", (uintmax_t)ocb->bus_addr);
2697 END_DEBUG
2698 SBP_DEBUG(1)
2699 	if (ocb->ccb != NULL)
2700 		sbp_print_scsi_cmd(ocb);
2701 END_DEBUG
2702 	if (ntohl(ocb->orb[4]) & 0xffff) {
2703 		bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
2704 			(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2705 			BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2706 		bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
2707 	}
2708 	if (ocb->ccb != NULL) {
2709 		callout_stop(&ocb->ccb->ccb_h.timeout_ch);
2710 		ocb->ccb->ccb_h.status = status;
2711 		xpt_done(ocb->ccb);
2712 	}
2713 	sbp_free_ocb(sdev, ocb);
2714 }
2715 
2716 static void
2717 sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2718 {
2719 	struct sbp_ocb *ocb, *next;
2720 	STAILQ_HEAD(, sbp_ocb) temp;
2721 
2722 	crit_enter();
2723 	STAILQ_INIT(&temp);
2724 	STAILQ_CONCAT(&temp, &sdev->ocbs);
2725 	for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2726 		next = STAILQ_NEXT(ocb, ocb);
2727 		sbp_abort_ocb(ocb, status);
2728 	}
2729 	crit_exit();
2730 }
2731 
2732 static devclass_t sbp_devclass;
2733 
2734 /*
2735  * Because sbp is a static device that always exists under any attached
2736  * firewire device, and not scanned by the firewire device, we need an
2737  * identify function to install the device.  For our sanity we want
2738  * the sbp device to have the same unit number as the fireweire device.
2739  */
2740 
2741 static device_method_t sbp_methods[] = {
2742 	/* device interface */
2743 	DEVMETHOD(device_identify,	bus_generic_identify_sameunit),
2744 	DEVMETHOD(device_probe,		sbp_probe),
2745 	DEVMETHOD(device_attach,	sbp_attach),
2746 	DEVMETHOD(device_detach,	sbp_detach),
2747 	DEVMETHOD(device_shutdown,	sbp_shutdown),
2748 
2749 	{ 0, 0 }
2750 };
2751 
2752 static driver_t sbp_driver = {
2753 	"sbp",
2754 	sbp_methods,
2755 	sizeof(struct sbp_softc),
2756 };
2757 
2758 DECLARE_DUMMY_MODULE(sbp);
2759 DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, NULL, NULL);
2760 MODULE_VERSION(sbp, 1);
2761 MODULE_DEPEND(sbp, firewire, 1, 1, 1);
2762 MODULE_DEPEND(sbp, cam, 1, 1, 1);
2763