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