2 * Copyright (c) 2003 Hidetoshi Shimokawa
3 * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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:
17 * This product includes software developed by K. Kobayashi and H. Shimokawa
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.
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.
34 * $FreeBSD: src/sys/dev/firewire/sbp.c,v 1.86 2007/03/16 01:23:36 simokawa Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/module.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 #include <sys/malloc.h>
46 #include <sys/thread2.h>
48 #include <bus/cam/cam.h>
49 #include <bus/cam/cam_ccb.h>
50 #include <bus/cam/cam_sim.h>
51 #include <bus/cam/cam_xpt_sim.h>
52 #include <bus/cam/cam_debug.h>
53 #include <bus/cam/cam_periph.h>
54 #include <bus/cam/scsi/scsi_all.h>
56 #include <bus/firewire/firewire.h>
57 #include <bus/firewire/firewirereg.h>
58 #include <bus/firewire/fwdma.h>
59 #include <bus/firewire/iec13213.h>
62 #define ccb_sdev_ptr spriv_ptr0
63 #define ccb_sbp_ptr spriv_ptr1
65 #define SBP_NUM_TARGETS 8 /* MAX 64 */
67 * Scan_bus doesn't work for more than 8 LUNs
68 * because of CAM_SCSI2_MAXLUN in cam_xpt.c
70 #define SBP_NUM_LUNS 64
71 #define SBP_MAXPHYS MIN(MAXPHYS, (512*1024) /* 512KB */)
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)
78 * STATUS FIFO addressing
80 * -----------------------
81 * 0- 1( 2): 0 (alingment)
85 * 32-47(16): SBP_BIND_HI
86 * 48-64(16): bus_id, node_id
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
97 static char *orb_fun_name
[] = {
101 static int debug
= 0;
102 static int auto_login
= 1;
103 static int max_speed
= -1;
105 static int sbp_cold
= 1;
107 static int ex_login
= 1;
108 static int login_delay
= 1000; /* msec */
109 static int scan_delay
= 500; /* msec */
110 static int sbp_tags
= 0;
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,
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");
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
);
136 #define NEED_RESPONSE 0
138 #define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE)
139 #define SBP_IND_MAX howmany(SBP_MAXPHYS, PAGE_SIZE)
142 STAILQ_ENTRY(sbp_ocb
) ocb
;
146 #define IND_PTR_OFFSET (8*sizeof(u_int32_t))
147 struct ind_ptr ind_ptr
[SBP_IND_MAX
];
148 struct sbp_dev
*sdev
;
149 int flags
; /* XXX should be removed */
153 #define OCB_ACT_MGM 0
154 #define OCB_ACT_CMD 1
155 #define OCB_MATCH(o,s) ((o)->bus_addr == ntohl((s)->orb_lo))
158 #define SBP_DEV_RESET 0 /* accept login */
159 #define SBP_DEV_LOGIN 1 /* to login */
161 #define SBP_DEV_RECONN 2 /* to reconnect */
163 #define SBP_DEV_TOATTACH 3 /* to attach */
164 #define SBP_DEV_PROBE 4 /* scan lun */
165 #define SBP_DEV_ATTACHED 5 /* in operation */
166 #define SBP_DEV_DEAD 6 /* unavailable unit */
167 #define SBP_DEV_RETRY 7 /* unavailable unit */
173 #define ORB_LINK_DEAD (1 << 0)
174 #define VALID_LUN (1 << 1)
175 #define ORB_POINTER_ACTIVE (1 << 2)
176 #define ORB_POINTER_NEED (1 << 3)
178 struct cam_path
*path
;
179 struct sbp_target
*target
;
180 struct fwdma_alloc dma
;
181 struct sbp_login_res
*login
;
182 struct callout login_callout
;
184 STAILQ_HEAD(, sbp_ocb
) ocbs
;
185 STAILQ_HEAD(, sbp_ocb
) free_ocbs
;
194 struct sbp_dev
**luns
;
195 struct sbp_softc
*sbp
;
196 struct fw_device
*fwdev
;
197 u_int32_t mgm_hi
, mgm_lo
;
198 struct sbp_ocb
*mgm_ocb_cur
;
199 STAILQ_HEAD(, sbp_ocb
) mgm_ocb_queue
;
200 struct callout mgm_ocb_timeout
;
201 struct callout scan_callout
;
202 STAILQ_HEAD(, fw_xfer
) xferlist
;
207 struct firewire_dev_comm fd
;
209 struct cam_path
*path
;
210 struct sbp_target targets
[SBP_NUM_TARGETS
];
213 struct timeval last_busreset
;
214 #define SIMQ_FREEZED 1
218 static void sbp_post_explore (void *);
219 static void sbp_recv (struct fw_xfer
*);
220 static void sbp_mgm_callback (struct fw_xfer
*);
222 static void sbp_cmd_callback (struct fw_xfer
*);
224 static void sbp_orb_pointer (struct sbp_dev
*, struct sbp_ocb
*);
225 static void sbp_execute_ocb (void *, bus_dma_segment_t
*, int, int);
226 static void sbp_free_ocb (struct sbp_dev
*, struct sbp_ocb
*);
227 static void sbp_abort_ocb (struct sbp_ocb
*, int);
228 static void sbp_abort_all_ocbs (struct sbp_dev
*, int);
229 static struct fw_xfer
* sbp_write_cmd (struct sbp_dev
*, int, int);
230 static struct sbp_ocb
* sbp_get_ocb (struct sbp_dev
*);
231 static struct sbp_ocb
* sbp_enqueue_ocb (struct sbp_dev
*, struct sbp_ocb
*);
232 static struct sbp_ocb
* sbp_dequeue_ocb (struct sbp_dev
*, struct sbp_status
*);
233 static void sbp_cam_detach_sdev(struct sbp_dev
*);
234 static void sbp_free_sdev(struct sbp_dev
*);
235 static void sbp_cam_detach_target (struct sbp_target
*);
236 static void sbp_free_target (struct sbp_target
*);
237 static void sbp_mgm_timeout (void *arg
);
238 static void sbp_timeout (void *arg
);
239 static void sbp_mgm_orb (struct sbp_dev
*, int, struct sbp_ocb
*);
241 MALLOC_DEFINE(M_SBP
, "sbp", "SBP-II/FireWire");
243 /* cam related functions */
244 static void sbp_action(struct cam_sim
*sim
, union ccb
*ccb
);
245 static void sbp_poll(struct cam_sim
*sim
);
246 static void sbp_cam_scan_lun(struct cam_periph
*, union ccb
*);
247 static void sbp_cam_scan_target(void *arg
);
249 static char *orb_status0
[] = {
250 /* 0 */ "No additional information to report",
251 /* 1 */ "Request type not supported",
252 /* 2 */ "Speed not supported",
253 /* 3 */ "Page size not supported",
254 /* 4 */ "Access denied",
255 /* 5 */ "Logical unit not supported",
256 /* 6 */ "Maximum payload too small",
257 /* 7 */ "Reserved for future standardization",
258 /* 8 */ "Resources unavailable",
259 /* 9 */ "Function rejected",
260 /* A */ "Login ID not recognized",
261 /* B */ "Dummy ORB completed",
262 /* C */ "Request aborted",
263 /* FF */ "Unspecified error"
264 #define MAX_ORB_STATUS0 0xd
267 static char *orb_status1_object
[] = {
268 /* 0 */ "Operation request block (ORB)",
269 /* 1 */ "Data buffer",
270 /* 2 */ "Page table",
271 /* 3 */ "Unable to specify"
274 static char *orb_status1_serial_bus_error
[] = {
275 /* 0 */ "Missing acknowledge",
276 /* 1 */ "Reserved; not to be used",
277 /* 2 */ "Time-out error",
278 /* 3 */ "Reserved; not to be used",
279 /* 4 */ "Busy retry limit exceeded(X)",
280 /* 5 */ "Busy retry limit exceeded(A)",
281 /* 6 */ "Busy retry limit exceeded(B)",
282 /* 7 */ "Reserved for future standardization",
283 /* 8 */ "Reserved for future standardization",
284 /* 9 */ "Reserved for future standardization",
285 /* A */ "Reserved for future standardization",
286 /* B */ "Tardy retry limit exceeded",
287 /* C */ "Conflict error",
288 /* D */ "Data error",
289 /* E */ "Type error",
290 /* F */ "Address error"
297 sbp_probe(device_t dev
)
302 kprintf("sbp_probe\n");
305 pa
= device_get_parent(dev
);
306 if(device_get_unit(dev
) != device_get_unit(pa
)){
310 device_set_desc(dev
, "SBP-2/SCSI over FireWire");
318 sbp_show_sdev_info(struct sbp_dev
*sdev
, int new)
320 struct fw_device
*fwdev
;
323 device_get_nameunit(sdev
->target
->sbp
->fd
.dev
),
324 sdev
->target
->target_id
,
330 fwdev
= sdev
->target
->fwdev
;
331 kprintf("ordered:%d type:%d EUI:%08x%08x node:%d "
332 "speed:%d maxrec:%d",
333 (sdev
->type
& 0x40) >> 6,
345 sbp_show_sdev_info(sdev
, 2);
346 kprintf("'%s' '%s' '%s'\n", sdev
->vendor
, sdev
->product
, sdev
->revision
);
354 /* Bus Target EUI64 */
356 {0, 2, {0x00018ea0, 0x01fd0154}}, /* Logitec HDD */
357 {0, 0, {0x00018ea6, 0x00100682}}, /* Logitec DVD */
358 {0, 1, {0x00d03200, 0xa412006a}}, /* Yano HDD */
364 sbp_new_target(struct sbp_softc
*sbp
, struct fw_device
*fwdev
)
366 int bus
, i
, target
=-1;
367 char w
[SBP_NUM_TARGETS
];
370 bus
= device_get_unit(sbp
->fd
.dev
);
372 /* XXX wired-down configuration should be gotten from
373 tunable or device hint */
374 for (i
= 0; wired
[i
].bus
>= 0; i
++) {
375 if (wired
[i
].bus
== bus
) {
376 w
[wired
[i
].target
] = 1;
377 if (wired
[i
].eui
.hi
== fwdev
->eui
.hi
&&
378 wired
[i
].eui
.lo
== fwdev
->eui
.lo
)
379 target
= wired
[i
].target
;
383 if(target
< SBP_NUM_TARGETS
&&
384 sbp
->targets
[target
].fwdev
== NULL
)
386 device_printf(sbp
->fd
.dev
,
387 "target %d is not free for %08x:%08x\n",
388 target
, fwdev
->eui
.hi
, fwdev
->eui
.lo
);
391 /* non-wired target */
392 for (i
= 0; i
< SBP_NUM_TARGETS
; i
++)
393 if (sbp
->targets
[i
].fwdev
== NULL
&& w
[i
] == 0) {
402 sbp_alloc_lun(struct sbp_target
*target
)
404 struct crom_context cc
;
406 struct sbp_dev
*sdev
, **newluns
;
407 struct sbp_softc
*sbp
;
411 crom_init_context(&cc
, target
->fwdev
->csrrom
);
412 /* XXX shoud parse appropriate unit directories only */
414 while (cc
.depth
>= 0) {
415 reg
= crom_search_key(&cc
, CROM_LUN
);
418 lun
= reg
->val
& 0xffff;
420 kprintf("target %d lun %d found\n", target
->target_id
, lun
);
427 kprintf("%s:%d no LUN found\n",
428 device_get_nameunit(target
->sbp
->fd
.dev
),
432 if (maxlun
>= SBP_NUM_LUNS
)
433 maxlun
= SBP_NUM_LUNS
;
435 /* Invalidiate stale devices */
436 for (lun
= 0; lun
< target
->num_lun
; lun
++) {
437 sdev
= target
->luns
[lun
];
440 sdev
->flags
&= ~VALID_LUN
;
443 sbp_cam_detach_sdev(sdev
);
449 if (maxlun
!= target
->num_lun
) {
451 * note: krealloc() does not support M_ZERO. We must zero
452 * the extended region manually.
454 newluns
= krealloc(target
->luns
,
455 sizeof(struct sbp_dev
*) * maxlun
,
458 if (maxlun
> target
->num_lun
) {
459 bzero(&newluns
[target
->num_lun
],
460 sizeof(struct sbp_dev
*) *
461 (maxlun
- target
->num_lun
));
463 target
->luns
= newluns
;
464 target
->num_lun
= maxlun
;
467 crom_init_context(&cc
, target
->fwdev
->csrrom
);
468 while (cc
.depth
>= 0) {
471 reg
= crom_search_key(&cc
, CROM_LUN
);
474 lun
= reg
->val
& 0xffff;
475 if (lun
>= SBP_NUM_LUNS
) {
476 kprintf("too large lun %d\n", lun
);
480 sdev
= target
->luns
[lun
];
482 sdev
= kmalloc(sizeof(struct sbp_dev
),
483 M_SBP
, M_WAITOK
| M_ZERO
);
484 target
->luns
[lun
] = sdev
;
486 sdev
->target
= target
;
487 STAILQ_INIT(&sdev
->ocbs
);
488 CALLOUT_INIT(&sdev
->login_callout
);
489 sdev
->status
= SBP_DEV_RESET
;
492 sdev
->flags
|= VALID_LUN
;
493 sdev
->type
= (reg
->val
& 0xff0000) >> 16;
498 fwdma_malloc(sbp
->fd
.fc
,
499 /* alignment */ sizeof(u_int32_t
),
500 SBP_DMA_SIZE
, &sdev
->dma
, BUS_DMA_NOWAIT
);
501 if (sdev
->dma
.v_addr
== NULL
) {
502 kprintf("%s: dma space allocation failed\n",
505 target
->luns
[lun
] = NULL
;
508 sdev
->login
= (struct sbp_login_res
*) sdev
->dma
.v_addr
;
509 sdev
->ocb
= (struct sbp_ocb
*)
510 ((char *)sdev
->dma
.v_addr
+ SBP_LOGIN_SIZE
);
511 bzero((char *)sdev
->ocb
,
512 sizeof (struct sbp_ocb
) * SBP_QUEUE_LEN
);
514 STAILQ_INIT(&sdev
->free_ocbs
);
515 for (i
= 0; i
< SBP_QUEUE_LEN
; i
++) {
518 ocb
->bus_addr
= sdev
->dma
.bus_addr
520 + sizeof(struct sbp_ocb
) * i
521 + offsetof(struct sbp_ocb
, orb
[0]);
522 if (bus_dmamap_create(sbp
->dmat
, 0, &ocb
->dmamap
)) {
523 kprintf("sbp_attach: cannot create dmamap\n");
527 sbp_free_ocb(sdev
, ocb
);
533 for (lun
= 0; lun
< target
->num_lun
; lun
++) {
534 sdev
= target
->luns
[lun
];
535 if (sdev
!= NULL
&& (sdev
->flags
& VALID_LUN
) == 0) {
536 sbp_cam_detach_sdev(sdev
);
538 target
->luns
[lun
] = NULL
;
543 static struct sbp_target
*
544 sbp_alloc_target(struct sbp_softc
*sbp
, struct fw_device
*fwdev
)
547 struct sbp_target
*target
;
548 struct crom_context cc
;
552 kprintf("sbp_alloc_target\n");
554 i
= sbp_new_target(sbp
, fwdev
);
556 device_printf(sbp
->fd
.dev
, "increase SBP_NUM_TARGETS!\n");
560 target
= &sbp
->targets
[i
];
562 target
->fwdev
= fwdev
;
563 target
->target_id
= i
;
564 /* XXX we may want to reload mgm port after each bus reset */
565 /* XXX there might be multiple management agents */
566 crom_init_context(&cc
, target
->fwdev
->csrrom
);
567 reg
= crom_search_key(&cc
, CROM_MGM
);
568 if (reg
== NULL
|| reg
->val
== 0) {
569 kprintf("NULL management address\n");
570 target
->fwdev
= NULL
;
573 target
->mgm_hi
= 0xffff;
574 target
->mgm_lo
= 0xf0000000 | (reg
->val
<< 2);
575 target
->mgm_ocb_cur
= NULL
;
577 kprintf("target:%d mgm_port: %x\n", i
, target
->mgm_lo
);
579 STAILQ_INIT(&target
->xferlist
);
581 STAILQ_INIT(&target
->mgm_ocb_queue
);
582 CALLOUT_INIT(&target
->mgm_ocb_timeout
);
583 CALLOUT_INIT(&target
->scan_callout
);
591 sbp_probe_lun(struct sbp_dev
*sdev
)
593 struct fw_device
*fwdev
;
594 struct crom_context c
, *cc
= &c
;
597 bzero(sdev
->vendor
, sizeof(sdev
->vendor
));
598 bzero(sdev
->product
, sizeof(sdev
->product
));
600 fwdev
= sdev
->target
->fwdev
;
601 crom_init_context(cc
, fwdev
->csrrom
);
602 /* get vendor string */
603 crom_search_key(cc
, CSRKEY_VENDOR
);
605 crom_parse_text(cc
, sdev
->vendor
, sizeof(sdev
->vendor
));
606 /* skip to the unit directory for SBP-2 */
607 while ((reg
= crom_search_key(cc
, CSRKEY_VER
)) != NULL
) {
608 if (reg
->val
== CSRVAL_T10SBP2
)
612 /* get firmware revision */
613 reg
= crom_search_key(cc
, CSRKEY_FIRM_VER
);
615 ksnprintf(sdev
->revision
, sizeof(sdev
->revision
),
617 /* get product string */
618 crom_search_key(cc
, CSRKEY_MODEL
);
620 crom_parse_text(cc
, sdev
->product
, sizeof(sdev
->product
));
624 sbp_login_callout(void *arg
)
626 struct sbp_dev
*sdev
= (struct sbp_dev
*)arg
;
627 sbp_mgm_orb(sdev
, ORB_FUN_LGI
, NULL
);
631 sbp_login(struct sbp_dev
*sdev
)
633 struct timeval delta
;
638 timevalsub(&delta
, &sdev
->target
->sbp
->last_busreset
);
639 t
.tv_sec
= login_delay
/ 1000;
640 t
.tv_usec
= (login_delay
% 1000) * 1000;
641 timevalsub(&t
, &delta
);
642 if (t
.tv_sec
>= 0 && t
.tv_usec
> 0)
643 ticks
= (t
.tv_sec
* 1000 + t
.tv_usec
/ 1000) * hz
/ 1000;
645 kprintf("%s: sec = %ld usec = %ld ticks = %d\n", __func__
,
646 t
.tv_sec
, t
.tv_usec
, ticks
);
648 callout_reset(&sdev
->login_callout
, ticks
,
649 sbp_login_callout
, (void *)(sdev
));
652 #define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
653 && crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
656 sbp_probe_target(void *arg
)
658 struct sbp_target
*target
= (struct sbp_target
*)arg
;
659 struct sbp_dev
*sdev
;
662 alive
= SBP_FWDEV_ALIVE(target
->fwdev
);
664 kprintf("sbp_probe_target %d\n", target
->target_id
);
666 kprintf("not alive\n");
669 sbp_alloc_lun(target
);
671 /* XXX callout_stop mgm_ocb and dequeue */
672 for (i
=0; i
< target
->num_lun
; i
++) {
673 sdev
= target
->luns
[i
];
676 if (alive
&& (sdev
->status
!= SBP_DEV_DEAD
)) {
677 if (sdev
->path
!= NULL
) {
678 xpt_freeze_devq(sdev
->path
, 1);
683 sbp_show_sdev_info(sdev
,
684 (sdev
->status
== SBP_DEV_RESET
));
687 sbp_abort_all_ocbs(sdev
, CAM_SCSI_BUS_RESET
);
688 switch (sdev
->status
) {
690 /* new or revived target */
694 case SBP_DEV_TOATTACH
:
696 case SBP_DEV_ATTACHED
:
699 sbp_mgm_orb(sdev
, ORB_FUN_RCN
, NULL
);
703 switch (sdev
->status
) {
704 case SBP_DEV_ATTACHED
:
706 /* the device has gone */
707 sbp_show_sdev_info(sdev
, 2);
708 kprintf("lost target\n");
712 xpt_freeze_devq(sdev
->path
, 1);
715 sdev
->status
= SBP_DEV_RETRY
;
716 sbp_abort_all_ocbs(sdev
, CAM_SCSI_BUS_RESET
);
718 sbp_cam_detach_target(sdev
->target
);
719 sdev
->status
= SBP_DEV_RESET
;
722 case SBP_DEV_TOATTACH
:
723 sdev
->status
= SBP_DEV_RESET
;
735 sbp_post_busreset(void *arg
)
737 struct sbp_softc
*sbp
;
739 sbp
= (struct sbp_softc
*)arg
;
741 kprintf("sbp_post_busreset\n");
743 if ((sbp
->sim
->flags
& SIMQ_FREEZED
) == 0) {
744 xpt_freeze_simq(sbp
->sim
, /*count*/1);
745 sbp
->sim
->flags
|= SIMQ_FREEZED
;
747 microtime(&sbp
->last_busreset
);
751 sbp_post_explore(void *arg
)
753 struct sbp_softc
*sbp
= (struct sbp_softc
*)arg
;
754 struct sbp_target
*target
;
755 struct fw_device
*fwdev
;
759 kprintf("sbp_post_explore\n");
768 * XXX don't let CAM the bus rest.
769 * CAM tries to do something with freezed (DEV_RETRY) devices.
771 xpt_async(AC_BUS_RESET
, sbp
->path
, /*arg*/ NULL
);
774 /* Gabage Collection */
775 for(i
= 0 ; i
< SBP_NUM_TARGETS
; i
++){
776 target
= &sbp
->targets
[i
];
777 STAILQ_FOREACH(fwdev
, &sbp
->fd
.fc
->devices
, link
)
778 if (target
->fwdev
== NULL
|| target
->fwdev
== fwdev
)
781 /* device has removed in lower driver */
782 sbp_cam_detach_target(target
);
783 sbp_free_target(target
);
786 /* traverse device list */
787 STAILQ_FOREACH(fwdev
, &sbp
->fd
.fc
->devices
, link
) {
789 kprintf("sbp_post_explore: EUI:%08x%08x ",
790 fwdev
->eui
.hi
, fwdev
->eui
.lo
);
791 if (fwdev
->status
!= FWDEVATTACHED
)
792 kprintf("not attached, state=%d.\n", fwdev
->status
);
794 kprintf("attached\n");
796 alive
= SBP_FWDEV_ALIVE(fwdev
);
797 for(i
= 0 ; i
< SBP_NUM_TARGETS
; i
++){
798 target
= &sbp
->targets
[i
];
799 if(target
->fwdev
== fwdev
) {
804 if(i
== SBP_NUM_TARGETS
){
807 target
= sbp_alloc_target(sbp
, fwdev
);
814 sbp_probe_target((void *)target
);
815 if (target
->num_lun
== 0)
816 sbp_free_target(target
);
818 xpt_release_simq(sbp
->sim
, /*run queue*/TRUE
);
819 sbp
->sim
->flags
&= ~SIMQ_FREEZED
;
824 sbp_loginres_callback(struct fw_xfer
*xfer
){
825 struct sbp_dev
*sdev
;
826 sdev
= (struct sbp_dev
*)xfer
->sc
;
828 sbp_show_sdev_info(sdev
, 2);
829 kprintf("sbp_loginres_callback\n");
833 STAILQ_INSERT_TAIL(&sdev
->target
->sbp
->fwb
.xferlist
, xfer
, link
);
840 sbp_xfer_free(struct fw_xfer
*xfer
)
842 struct sbp_dev
*sdev
;
844 sdev
= (struct sbp_dev
*)xfer
->sc
;
845 fw_xfer_unload(xfer
);
847 STAILQ_INSERT_TAIL(&sdev
->target
->xferlist
, xfer
, link
);
852 sbp_reset_start_callback(struct fw_xfer
*xfer
)
854 struct sbp_dev
*tsdev
, *sdev
= (struct sbp_dev
*)xfer
->sc
;
855 struct sbp_target
*target
= sdev
->target
;
858 if (xfer
->resp
!= 0) {
859 sbp_show_sdev_info(sdev
, 2);
860 kprintf("sbp_reset_start failed: resp=%d\n", xfer
->resp
);
863 for (i
= 0; i
< target
->num_lun
; i
++) {
864 tsdev
= target
->luns
[i
];
865 if (tsdev
!= NULL
&& tsdev
->status
== SBP_DEV_LOGIN
)
871 sbp_reset_start(struct sbp_dev
*sdev
)
873 struct fw_xfer
*xfer
;
877 sbp_show_sdev_info(sdev
, 2);
878 kprintf("sbp_reset_start\n");
881 xfer
= sbp_write_cmd(sdev
, FWTCODE_WREQQ
, 0);
882 xfer
->act
.hand
= sbp_reset_start_callback
;
883 fp
= &xfer
->send
.hdr
;
884 fp
->mode
.wreqq
.dest_hi
= 0xffff;
885 fp
->mode
.wreqq
.dest_lo
= 0xf0000000 | RESET_START
;
886 fp
->mode
.wreqq
.data
= htonl(0xf);
887 fw_asyreq(xfer
->fc
, -1, xfer
);
891 sbp_mgm_callback(struct fw_xfer
*xfer
)
893 struct sbp_dev
*sdev
;
898 sdev
= (struct sbp_dev
*)xfer
->sc
;
901 sbp_show_sdev_info(sdev
, 2);
902 kprintf("sbp_mgm_callback\n");
910 sbp_show_sdev_info(sdev
, 2);
911 kprintf("management ORB failed(%d) ... RESET_START\n", resp
);
912 sbp_reset_start(sdev
);
918 static struct sbp_dev
*
919 sbp_next_dev(struct sbp_target
*target
, int lun
)
921 struct sbp_dev
**sdevp
;
924 for (i
= lun
, sdevp
= &target
->luns
[lun
]; i
< target
->num_lun
;
926 if (*sdevp
!= NULL
&& (*sdevp
)->status
== SBP_DEV_PROBE
)
933 sbp_cam_scan_lun(struct cam_periph
*periph
, union ccb
*ccb
)
935 struct sbp_target
*target
;
936 struct sbp_dev
*sdev
;
938 sdev
= (struct sbp_dev
*) ccb
->ccb_h
.ccb_sdev_ptr
;
939 target
= sdev
->target
;
941 sbp_show_sdev_info(sdev
, 2);
942 kprintf("sbp_cam_scan_lun\n");
944 if ((ccb
->ccb_h
.status
& CAM_STATUS_MASK
) == CAM_REQ_CMP
) {
945 sdev
->status
= SBP_DEV_ATTACHED
;
947 sbp_show_sdev_info(sdev
, 2);
948 kprintf("scan failed\n");
950 sdev
= sbp_next_dev(target
, sdev
->lun_id
+ 1);
956 xpt_setup_ccb(&ccb
->ccb_h
, sdev
->path
, SCAN_PRI
);
957 ccb
->ccb_h
.ccb_sdev_ptr
= sdev
;
959 xpt_release_devq(sdev
->path
, sdev
->freeze
, TRUE
);
964 sbp_cam_scan_target(void *arg
)
966 struct sbp_target
*target
= (struct sbp_target
*)arg
;
967 struct sbp_dev
*sdev
;
970 sdev
= sbp_next_dev(target
, 0);
972 kprintf("sbp_cam_scan_target: nothing to do for target%d\n",
977 sbp_show_sdev_info(sdev
, 2);
978 kprintf("sbp_cam_scan_target\n");
980 ccb
= kmalloc(sizeof(union ccb
), M_SBP
, M_WAITOK
| M_ZERO
);
981 xpt_setup_ccb(&ccb
->ccb_h
, sdev
->path
, SCAN_PRI
);
982 ccb
->ccb_h
.func_code
= XPT_SCAN_LUN
;
983 ccb
->ccb_h
.cbfcnp
= sbp_cam_scan_lun
;
984 ccb
->ccb_h
.flags
|= CAM_DEV_QFREEZE
;
985 ccb
->crcn
.flags
= CAM_FLAG_NONE
;
986 ccb
->ccb_h
.ccb_sdev_ptr
= sdev
;
988 /* The scan is in progress now. */
990 xpt_release_devq(sdev
->path
, sdev
->freeze
, TRUE
);
995 sbp_scan_dev(struct sbp_dev
*sdev
)
997 sdev
->status
= SBP_DEV_PROBE
;
998 callout_reset(&sdev
->target
->scan_callout
, scan_delay
* hz
/ 1000,
999 sbp_cam_scan_target
, (void *)sdev
->target
);
1003 sbp_do_attach(struct fw_xfer
*xfer
)
1005 struct sbp_dev
*sdev
;
1006 struct sbp_target
*target
;
1008 sdev
= (struct sbp_dev
*)xfer
->sc
;
1009 target
= sdev
->target
;
1011 sbp_show_sdev_info(sdev
, 2);
1012 kprintf("sbp_do_attach\n");
1014 sbp_xfer_free(xfer
);
1016 if (sdev
->path
== NULL
)
1017 xpt_create_path(&sdev
->path
, xpt_periph
,
1018 cam_sim_path(target
->sbp
->sim
),
1019 target
->target_id
, sdev
->lun_id
);
1023 * Let CAM scan the bus if we are in the boot process.
1024 * XXX xpt_scan_bus cannot detect LUN larger than 0
1025 * if LUN 0 doesn't exists.
1028 sdev
->status
= SBP_DEV_ATTACHED
;
1038 sbp_agent_reset_callback(struct fw_xfer
*xfer
)
1040 struct sbp_dev
*sdev
;
1042 sdev
= (struct sbp_dev
*)xfer
->sc
;
1044 sbp_show_sdev_info(sdev
, 2);
1045 kprintf("%s\n", __func__
);
1047 if (xfer
->resp
!= 0) {
1048 sbp_show_sdev_info(sdev
, 2);
1049 kprintf("%s: resp=%d\n", __func__
, xfer
->resp
);
1052 sbp_xfer_free(xfer
);
1054 xpt_release_devq(sdev
->path
, sdev
->freeze
, TRUE
);
1060 sbp_agent_reset(struct sbp_dev
*sdev
)
1062 struct fw_xfer
*xfer
;
1066 sbp_show_sdev_info(sdev
, 2);
1067 kprintf("sbp_agent_reset\n");
1069 xfer
= sbp_write_cmd(sdev
, FWTCODE_WREQQ
, 0x04);
1072 if (sdev
->status
== SBP_DEV_ATTACHED
|| sdev
->status
== SBP_DEV_PROBE
)
1073 xfer
->act
.hand
= sbp_agent_reset_callback
;
1075 xfer
->act
.hand
= sbp_do_attach
;
1076 fp
= &xfer
->send
.hdr
;
1077 fp
->mode
.wreqq
.data
= htonl(0xf);
1078 fw_asyreq(xfer
->fc
, -1, xfer
);
1079 sbp_abort_all_ocbs(sdev
, CAM_BDR_SENT
);
1083 sbp_busy_timeout_callback(struct fw_xfer
*xfer
)
1085 struct sbp_dev
*sdev
;
1087 sdev
= (struct sbp_dev
*)xfer
->sc
;
1089 sbp_show_sdev_info(sdev
, 2);
1090 kprintf("sbp_busy_timeout_callback\n");
1092 sbp_xfer_free(xfer
);
1093 sbp_agent_reset(sdev
);
1097 sbp_busy_timeout(struct sbp_dev
*sdev
)
1100 struct fw_xfer
*xfer
;
1102 sbp_show_sdev_info(sdev
, 2);
1103 kprintf("sbp_busy_timeout\n");
1105 xfer
= sbp_write_cmd(sdev
, FWTCODE_WREQQ
, 0);
1107 xfer
->act
.hand
= sbp_busy_timeout_callback
;
1108 fp
= &xfer
->send
.hdr
;
1109 fp
->mode
.wreqq
.dest_hi
= 0xffff;
1110 fp
->mode
.wreqq
.dest_lo
= 0xf0000000 | BUSY_TIMEOUT
;
1111 fp
->mode
.wreqq
.data
= htonl((1 << (13+12)) | 0xf);
1112 fw_asyreq(xfer
->fc
, -1, xfer
);
1116 sbp_orb_pointer_callback(struct fw_xfer
*xfer
)
1118 struct sbp_dev
*sdev
;
1119 sdev
= (struct sbp_dev
*)xfer
->sc
;
1122 sbp_show_sdev_info(sdev
, 2);
1123 kprintf("%s\n", __func__
);
1125 if (xfer
->resp
!= 0) {
1127 kprintf("%s: xfer->resp = %d\n", __func__
, xfer
->resp
);
1129 sbp_xfer_free(xfer
);
1130 sdev
->flags
&= ~ORB_POINTER_ACTIVE
;
1132 if ((sdev
->flags
& ORB_POINTER_NEED
) != 0) {
1133 struct sbp_ocb
*ocb
;
1135 sdev
->flags
&= ~ORB_POINTER_NEED
;
1136 ocb
= STAILQ_FIRST(&sdev
->ocbs
);
1138 sbp_orb_pointer(sdev
, ocb
);
1144 sbp_orb_pointer(struct sbp_dev
*sdev
, struct sbp_ocb
*ocb
)
1146 struct fw_xfer
*xfer
;
1149 sbp_show_sdev_info(sdev
, 2);
1150 kprintf("%s: 0x%08x\n", __func__
, (u_int32_t
)ocb
->bus_addr
);
1153 if ((sdev
->flags
& ORB_POINTER_ACTIVE
) != 0) {
1155 kprintf("%s: orb pointer active\n", __func__
);
1157 sdev
->flags
|= ORB_POINTER_NEED
;
1161 sdev
->flags
|= ORB_POINTER_ACTIVE
;
1162 xfer
= sbp_write_cmd(sdev
, FWTCODE_WREQB
, 0x08);
1165 xfer
->act
.hand
= sbp_orb_pointer_callback
;
1167 fp
= &xfer
->send
.hdr
;
1168 fp
->mode
.wreqb
.len
= 8;
1169 fp
->mode
.wreqb
.extcode
= 0;
1170 xfer
->send
.payload
[0] =
1171 htonl(((sdev
->target
->sbp
->fd
.fc
->nodeid
| FWLOCALBUS
)<< 16));
1172 xfer
->send
.payload
[1] = htonl((u_int32_t
)ocb
->bus_addr
);
1174 if(fw_asyreq(xfer
->fc
, -1, xfer
) != 0){
1175 sbp_xfer_free(xfer
);
1176 ocb
->ccb
->ccb_h
.status
= CAM_REQ_INVALID
;
1183 sbp_cmd_callback(struct fw_xfer
*xfer
)
1186 struct sbp_dev
*sdev
;
1187 sdev
= (struct sbp_dev
*)xfer
->sc
;
1188 sbp_show_sdev_info(sdev
, 2);
1189 kprintf("sbp_cmd_callback\n");
1191 if (xfer
->resp
!= 0) {
1193 kprintf("%s: xfer->resp = %d\n", __func__
, xfer
->resp
);
1195 sbp_xfer_free(xfer
);
1200 sbp_doorbell(struct sbp_dev
*sdev
)
1202 struct fw_xfer
*xfer
;
1205 sbp_show_sdev_info(sdev
, 2);
1206 kprintf("sbp_doorbell\n");
1209 xfer
= sbp_write_cmd(sdev
, FWTCODE_WREQQ
, 0x10);
1212 xfer
->act
.hand
= sbp_cmd_callback
;
1213 fp
= (struct fw_pkt
*)xfer
->send
.buf
;
1214 fp
->mode
.wreqq
.data
= htonl(0xf);
1215 fw_asyreq(xfer
->fc
, -1, xfer
);
1219 static struct fw_xfer
*
1220 sbp_write_cmd(struct sbp_dev
*sdev
, int tcode
, int offset
)
1222 struct fw_xfer
*xfer
;
1224 struct sbp_target
*target
;
1227 target
= sdev
->target
;
1229 xfer
= STAILQ_FIRST(&target
->xferlist
);
1231 if (target
->n_xfer
> 5 /* XXX */) {
1232 kprintf("sbp: no more xfer for this target\n");
1236 xfer
= fw_xfer_alloc_buf(M_SBP
, 8, 0);
1238 kprintf("sbp: fw_xfer_alloc_buf failed\n");
1244 kprintf("sbp: alloc %d xfer\n", target
->n_xfer
);
1247 STAILQ_REMOVE_HEAD(&target
->xferlist
, link
);
1251 microtime(&xfer
->tv
);
1254 xfer
->recv
.pay_len
= 0;
1255 xfer
->send
.spd
= min(sdev
->target
->fwdev
->speed
, max_speed
);
1256 xfer
->fc
= sdev
->target
->sbp
->fd
.fc
;
1257 xfer
->retry_req
= fw_asybusy
;
1260 if (tcode
== FWTCODE_WREQB
)
1261 xfer
->send
.pay_len
= 8;
1263 xfer
->send
.pay_len
= 0;
1265 xfer
->sc
= (caddr_t
)sdev
;
1266 fp
= &xfer
->send
.hdr
;
1267 fp
->mode
.wreqq
.dest_hi
= sdev
->login
->cmd_hi
;
1268 fp
->mode
.wreqq
.dest_lo
= sdev
->login
->cmd_lo
+ offset
;
1269 fp
->mode
.wreqq
.tlrt
= 0;
1270 fp
->mode
.wreqq
.tcode
= tcode
;
1271 fp
->mode
.wreqq
.pri
= 0;
1272 fp
->mode
.wreqq
.dst
= FWLOCALBUS
| sdev
->target
->fwdev
->dst
;
1279 sbp_mgm_orb(struct sbp_dev
*sdev
, int func
, struct sbp_ocb
*aocb
)
1281 struct fw_xfer
*xfer
;
1283 struct sbp_ocb
*ocb
;
1284 struct sbp_target
*target
;
1287 target
= sdev
->target
;
1288 nid
= target
->sbp
->fd
.fc
->nodeid
| FWLOCALBUS
;
1291 if (func
== ORB_FUN_RUNQUEUE
) {
1292 ocb
= STAILQ_FIRST(&target
->mgm_ocb_queue
);
1293 if (target
->mgm_ocb_cur
!= NULL
|| ocb
== NULL
) {
1297 STAILQ_REMOVE_HEAD(&target
->mgm_ocb_queue
, ocb
);
1300 if ((ocb
= sbp_get_ocb(sdev
)) == NULL
) {
1305 ocb
->flags
= OCB_ACT_MGM
;
1308 bzero((void *)ocb
->orb
, sizeof(ocb
->orb
));
1309 ocb
->orb
[6] = htonl((nid
<< 16) | SBP_BIND_HI
);
1310 ocb
->orb
[7] = htonl(SBP_DEV2ADDR(target
->target_id
, sdev
->lun_id
));
1313 sbp_show_sdev_info(sdev
, 2);
1314 kprintf("%s\n", orb_fun_name
[(func
>>16)&0xf]);
1318 ocb
->orb
[0] = ocb
->orb
[1] = 0; /* password */
1319 ocb
->orb
[2] = htonl(nid
<< 16);
1320 ocb
->orb
[3] = htonl(sdev
->dma
.bus_addr
);
1321 ocb
->orb
[4] = htonl(ORB_NOTIFY
| sdev
->lun_id
);
1323 ocb
->orb
[4] |= htonl(ORB_EXV
);
1324 ocb
->orb
[5] = htonl(SBP_LOGIN_SIZE
);
1325 fwdma_sync(&sdev
->dma
, BUS_DMASYNC_PREREAD
);
1328 ocb
->orb
[0] = htonl((0 << 16) | 0);
1329 ocb
->orb
[1] = htonl(aocb
->bus_addr
& 0xffffffff);
1336 ocb
->orb
[4] = htonl(ORB_NOTIFY
| func
| sdev
->login
->id
);
1340 if (target
->mgm_ocb_cur
!= NULL
) {
1341 /* there is a standing ORB */
1342 STAILQ_INSERT_TAIL(&sdev
->target
->mgm_ocb_queue
, ocb
, ocb
);
1347 target
->mgm_ocb_cur
= ocb
;
1350 callout_reset(&target
->mgm_ocb_timeout
, 5*hz
,
1351 sbp_mgm_timeout
, (caddr_t
)ocb
);
1352 xfer
= sbp_write_cmd(sdev
, FWTCODE_WREQB
, 0);
1356 xfer
->act
.hand
= sbp_mgm_callback
;
1358 fp
= &xfer
->send
.hdr
;
1359 fp
->mode
.wreqb
.dest_hi
= sdev
->target
->mgm_hi
;
1360 fp
->mode
.wreqb
.dest_lo
= sdev
->target
->mgm_lo
;
1361 fp
->mode
.wreqb
.len
= 8;
1362 fp
->mode
.wreqb
.extcode
= 0;
1363 xfer
->send
.payload
[0] = htonl(nid
<< 16);
1364 xfer
->send
.payload
[1] = htonl(ocb
->bus_addr
& 0xffffffff);
1366 sbp_show_sdev_info(sdev
, 2);
1367 kprintf("mgm orb: %08x\n", (u_int32_t
)ocb
->bus_addr
);
1370 fw_asyreq(xfer
->fc
, -1, xfer
);
1374 sbp_print_scsi_cmd(struct sbp_ocb
*ocb
)
1376 struct ccb_scsiio
*csio
;
1378 csio
= &ocb
->ccb
->csio
;
1379 kprintf("%s:%d:%d XPT_SCSI_IO: "
1380 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1382 "%db cmd/%db data/%db sense\n",
1383 device_get_nameunit(ocb
->sdev
->target
->sbp
->fd
.dev
),
1384 ocb
->ccb
->ccb_h
.target_id
, ocb
->ccb
->ccb_h
.target_lun
,
1385 csio
->cdb_io
.cdb_bytes
[0],
1386 csio
->cdb_io
.cdb_bytes
[1],
1387 csio
->cdb_io
.cdb_bytes
[2],
1388 csio
->cdb_io
.cdb_bytes
[3],
1389 csio
->cdb_io
.cdb_bytes
[4],
1390 csio
->cdb_io
.cdb_bytes
[5],
1391 csio
->cdb_io
.cdb_bytes
[6],
1392 csio
->cdb_io
.cdb_bytes
[7],
1393 csio
->cdb_io
.cdb_bytes
[8],
1394 csio
->cdb_io
.cdb_bytes
[9],
1395 ocb
->ccb
->ccb_h
.flags
& CAM_DIR_MASK
,
1396 csio
->cdb_len
, csio
->dxfer_len
,
1401 sbp_scsi_status(struct sbp_status
*sbp_status
, struct sbp_ocb
*ocb
)
1403 struct sbp_cmd_status
*sbp_cmd_status
;
1404 struct scsi_sense_data
*sense
;
1406 sbp_cmd_status
= (struct sbp_cmd_status
*)sbp_status
->data
;
1407 sense
= &ocb
->ccb
->csio
.sense_data
;
1410 sbp_print_scsi_cmd(ocb
);
1411 /* XXX need decode status */
1412 sbp_show_sdev_info(ocb
->sdev
, 2);
1413 kprintf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1414 sbp_cmd_status
->status
,
1415 sbp_cmd_status
->sfmt
,
1416 sbp_cmd_status
->valid
,
1417 sbp_cmd_status
->s_key
,
1418 sbp_cmd_status
->s_code
,
1419 sbp_cmd_status
->s_qlfr
,
1424 switch (sbp_cmd_status
->status
) {
1425 case SCSI_STATUS_CHECK_COND
:
1426 case SCSI_STATUS_BUSY
:
1427 case SCSI_STATUS_CMD_TERMINATED
:
1428 if(sbp_cmd_status
->sfmt
== SBP_SFMT_CURR
){
1429 sense
->error_code
= SSD_CURRENT_ERROR
;
1431 sense
->error_code
= SSD_DEFERRED_ERROR
;
1433 if(sbp_cmd_status
->valid
)
1434 sense
->error_code
|= SSD_ERRCODE_VALID
;
1435 sense
->flags
= sbp_cmd_status
->s_key
;
1436 if(sbp_cmd_status
->mark
)
1437 sense
->flags
|= SSD_FILEMARK
;
1438 if(sbp_cmd_status
->eom
)
1439 sense
->flags
|= SSD_EOM
;
1440 if(sbp_cmd_status
->ill_len
)
1441 sense
->flags
|= SSD_ILI
;
1443 bcopy(&sbp_cmd_status
->info
, &sense
->info
[0], 4);
1445 if (sbp_status
->len
<= 1)
1446 /* XXX not scsi status. shouldn't be happened */
1447 sense
->extra_len
= 0;
1448 else if (sbp_status
->len
<= 4)
1449 /* add_sense_code(_qual), info, cmd_spec_info */
1450 sense
->extra_len
= 6;
1452 /* fru, sense_key_spec */
1453 sense
->extra_len
= 10;
1455 bcopy(&sbp_cmd_status
->cdb
, &sense
->cmd_spec_info
[0], 4);
1457 sense
->add_sense_code
= sbp_cmd_status
->s_code
;
1458 sense
->add_sense_code_qual
= sbp_cmd_status
->s_qlfr
;
1459 sense
->fru
= sbp_cmd_status
->fru
;
1461 bcopy(&sbp_cmd_status
->s_keydep
[0],
1462 &sense
->sense_key_spec
[0], 3);
1464 ocb
->ccb
->csio
.scsi_status
= sbp_cmd_status
->status
;
1465 ocb
->ccb
->ccb_h
.status
= CAM_SCSI_STATUS_ERROR
1466 | CAM_AUTOSNS_VALID
;
1471 for( j = 0 ; j < 32 ; j+=8){
1472 kprintf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1473 tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1474 tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1481 sbp_show_sdev_info(ocb
->sdev
, 2);
1482 kprintf("sbp_scsi_status: unknown scsi status 0x%x\n",
1483 sbp_cmd_status
->status
);
1488 sbp_fix_inq_data(struct sbp_ocb
*ocb
)
1491 struct sbp_dev
*sdev
;
1492 struct scsi_inquiry_data
*inq
;
1497 if (ccb
->csio
.cdb_io
.cdb_bytes
[1] & SI_EVPD
)
1500 sbp_show_sdev_info(sdev
, 2);
1501 kprintf("sbp_fix_inq_data\n");
1503 inq
= (struct scsi_inquiry_data
*) ccb
->csio
.data_ptr
;
1504 switch (SID_TYPE(inq
)) {
1508 * XXX Convert Direct Access device to RBC.
1509 * I've never seen FireWire DA devices which support READ_6.
1511 if (SID_TYPE(inq
) == T_DIRECT
)
1512 inq
->device
|= T_RBC
; /* T_DIRECT == 0 */
1516 /* enable tagged queuing */
1518 inq
->flags
|= SID_CmdQue
;
1520 inq
->flags
&= ~SID_CmdQue
;
1522 * Override vendor/product/revision information.
1523 * Some devices sometimes return strange strings.
1526 bcopy(sdev
->vendor
, inq
->vendor
, sizeof(inq
->vendor
));
1527 bcopy(sdev
->product
, inq
->product
, sizeof(inq
->product
));
1528 bcopy(sdev
->revision
+2, inq
->revision
, sizeof(inq
->revision
));
1535 sbp_recv1(struct fw_xfer
*xfer
)
1541 struct sbp_softc
*sbp
;
1542 struct sbp_dev
*sdev
;
1543 struct sbp_ocb
*ocb
;
1544 struct sbp_login_res
*login_res
= NULL
;
1545 struct sbp_status
*sbp_status
;
1546 struct sbp_target
*target
;
1547 int orb_fun
, status_valid0
, status_valid
, t
, l
, reset_agent
= 0;
1551 ld = xfer->recv.buf;
1552 kprintf("sbp %x %d %d %08x %08x %08x %08x\n",
1553 xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1554 kprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1555 kprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1557 sbp
= (struct sbp_softc
*)xfer
->sc
;
1558 if (xfer
->resp
!= 0){
1559 kprintf("sbp_recv: xfer->resp = %d\n", xfer
->resp
);
1562 if (xfer
->recv
.payload
== NULL
){
1563 kprintf("sbp_recv: xfer->recv.payload == NULL\n");
1566 rfp
= &xfer
->recv
.hdr
;
1567 if(rfp
->mode
.wreqb
.tcode
!= FWTCODE_WREQB
){
1568 kprintf("sbp_recv: tcode = %d\n", rfp
->mode
.wreqb
.tcode
);
1571 sbp_status
= (struct sbp_status
*)xfer
->recv
.payload
;
1572 addr
= rfp
->mode
.wreqb
.dest_lo
;
1574 kprintf("received address 0x%x\n", addr
);
1576 t
= SBP_ADDR2TRG(addr
);
1577 if (t
>= SBP_NUM_TARGETS
) {
1578 device_printf(sbp
->fd
.dev
,
1579 "sbp_recv1: invalid target %d\n", t
);
1582 target
= &sbp
->targets
[t
];
1583 l
= SBP_ADDR2LUN(addr
);
1584 if (l
>= target
->num_lun
|| target
->luns
[l
] == NULL
) {
1585 device_printf(sbp
->fd
.dev
,
1586 "sbp_recv1: invalid lun %d (target=%d)\n", l
, t
);
1589 sdev
= target
->luns
[l
];
1592 switch (sbp_status
->src
) {
1595 /* check mgm_ocb_cur first */
1596 ocb
= target
->mgm_ocb_cur
;
1598 if (OCB_MATCH(ocb
, sbp_status
)) {
1599 callout_stop(&target
->mgm_ocb_timeout
);
1600 target
->mgm_ocb_cur
= NULL
;
1604 ocb
= sbp_dequeue_ocb(sdev
, sbp_status
);
1606 sbp_show_sdev_info(sdev
, 2);
1607 kprintf("No ocb(%x) on the queue\n",
1608 ntohl(sbp_status
->orb_lo
));
1613 sbp_show_sdev_info(sdev
, 2);
1614 kprintf("unsolicit status received\n");
1617 sbp_show_sdev_info(sdev
, 2);
1618 kprintf("unknown sbp_status->src\n");
1621 status_valid0
= (sbp_status
->src
< 2
1622 && sbp_status
->resp
== ORB_RES_CMPL
1623 && sbp_status
->dead
== 0);
1624 status_valid
= (status_valid0
&& sbp_status
->status
== 0);
1626 if (!status_valid0
|| debug
> 2){
1629 sbp_show_sdev_info(sdev
, 2);
1630 kprintf("ORB status src:%x resp:%x dead:%x"
1631 " len:%x stat:%x orb:%x%08x\n",
1632 sbp_status
->src
, sbp_status
->resp
, sbp_status
->dead
,
1633 sbp_status
->len
, sbp_status
->status
,
1634 ntohs(sbp_status
->orb_hi
), ntohl(sbp_status
->orb_lo
));
1636 sbp_show_sdev_info(sdev
, 2);
1637 status
= sbp_status
->status
;
1638 switch(sbp_status
->resp
) {
1640 if (status
> MAX_ORB_STATUS0
)
1641 kprintf("%s\n", orb_status0
[MAX_ORB_STATUS0
]);
1643 kprintf("%s\n", orb_status0
[status
]);
1646 kprintf("Obj: %s, Error: %s\n",
1647 orb_status1_object
[(status
>>6) & 3],
1648 orb_status1_serial_bus_error
[status
& 0xf]);
1651 kprintf("Illegal request\n");
1654 kprintf("Vendor dependent\n");
1657 kprintf("unknown respose code %d\n", sbp_status
->resp
);
1661 /* we have to reset the fetch agent if it's dead */
1662 if (sbp_status
->dead
) {
1664 xpt_freeze_devq(sdev
->path
, 1);
1673 switch(ntohl(ocb
->orb
[4]) & ORB_FMT_MSK
){
1679 switch(ocb
->flags
) {
1681 orb_fun
= ntohl(ocb
->orb
[4]) & ORB_FUN_MSK
;
1685 fwdma_sync(&sdev
->dma
, BUS_DMASYNC_POSTREAD
);
1686 login_res
= sdev
->login
;
1687 login_res
->len
= ntohs(login_res
->len
);
1688 login_res
->id
= ntohs(login_res
->id
);
1689 login_res
->cmd_hi
= ntohs(login_res
->cmd_hi
);
1690 login_res
->cmd_lo
= ntohl(login_res
->cmd_lo
);
1693 sbp_show_sdev_info(sdev
, 2);
1694 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
));
1696 sbp_busy_timeout(sdev
);
1698 /* forgot logout? */
1699 sbp_show_sdev_info(sdev
, 2);
1700 kprintf("login failed\n");
1701 sdev
->status
= SBP_DEV_RESET
;
1705 login_res
= sdev
->login
;
1708 sbp_show_sdev_info(sdev
, 2);
1709 kprintf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res
->len
, login_res
->id
, login_res
->cmd_hi
, login_res
->cmd_lo
);
1712 if (sdev
->status
== SBP_DEV_ATTACHED
)
1715 sbp_agent_reset(sdev
);
1717 sdev
->status
= SBP_DEV_ATTACHED
;
1718 sbp_mgm_orb(sdev
, ORB_FUN_ATS
, NULL
);
1721 /* reconnection hold time exceed? */
1723 sbp_show_sdev_info(sdev
, 2);
1724 kprintf("reconnect failed\n");
1730 sdev
->status
= SBP_DEV_RESET
;
1733 sbp_busy_timeout(sdev
);
1738 sbp_agent_reset(sdev
);
1741 sbp_show_sdev_info(sdev
, 2);
1742 kprintf("unknown function %d\n", orb_fun
);
1745 sbp_mgm_orb(sdev
, ORB_FUN_RUNQUEUE
, NULL
);
1749 if(ocb
->ccb
!= NULL
){
1753 ld = ocb->ccb->csio.data_ptr;
1754 if(ld != NULL && ocb->ccb->csio.dxfer_len != 0)
1755 kprintf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]);
1757 kprintf("ptr NULL\n");
1758 kprintf("len %d\n", sbp_status->len);
1761 if(sbp_status
->len
> 1){
1762 sbp_scsi_status(sbp_status
, ocb
);
1764 if(sbp_status
->resp
!= ORB_RES_CMPL
){
1765 ccb
->ccb_h
.status
= CAM_REQ_CMP_ERR
;
1767 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
1770 /* fix up inq data */
1771 if (ccb
->csio
.cdb_io
.cdb_bytes
[0] == INQUIRY
)
1772 sbp_fix_inq_data(ocb
);
1781 sbp_free_ocb(sdev
, ocb
);
1784 sbp_agent_reset(sdev
);
1787 xfer
->recv
.pay_len
= SBP_RECV_LEN
;
1788 /* The received packet is usually small enough to be stored within
1789 * the buffer. In that case, the controller return ack_complete and
1790 * no respose is necessary.
1792 * XXX fwohci.c and firewire.c should inform event_code such as
1793 * ack_complete or ack_pending to upper driver.
1797 sfp
= (struct fw_pkt
*)xfer
->send
.buf
;
1798 sfp
->mode
.wres
.dst
= rfp
->mode
.wreqb
.src
;
1799 xfer
->dst
= sfp
->mode
.wres
.dst
;
1800 xfer
->spd
= min(sdev
->target
->fwdev
->speed
, max_speed
);
1801 xfer
->act
.hand
= sbp_loginres_callback
;
1802 xfer
->retry_req
= fw_asybusy
;
1804 sfp
->mode
.wres
.tlrt
= rfp
->mode
.wreqb
.tlrt
;
1805 sfp
->mode
.wres
.tcode
= FWTCODE_WRES
;
1806 sfp
->mode
.wres
.rtcode
= 0;
1807 sfp
->mode
.wres
.pri
= 0;
1809 fw_asyreq(xfer
->fc
, -1, xfer
);
1812 STAILQ_INSERT_TAIL(&sbp
->fwb
.xferlist
, xfer
, link
);
1820 sbp_recv(struct fw_xfer
*xfer
)
1830 sbp_attach(device_t dev
)
1832 struct sbp_softc
*sbp
;
1833 struct cam_devq
*devq
;
1834 struct fw_xfer
*xfer
;
1838 kprintf("sbp_attach (cold=%d)\n", cold
);
1845 sbp
= ((struct sbp_softc
*)device_get_softc(dev
));
1846 bzero(sbp
, sizeof(struct sbp_softc
));
1848 sbp
->fd
.fc
= device_get_ivars(dev
);
1851 max_speed
= sbp
->fd
.fc
->speed
;
1853 error
= bus_dma_tag_create(/*parent*/sbp
->fd
.fc
->dmat
,
1854 /* XXX shoud be 4 for sane backend? */
1857 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT
,
1858 /*highaddr*/BUS_SPACE_MAXADDR
,
1859 /*filter*/NULL
, /*filterarg*/NULL
,
1860 /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX
,
1861 /*maxsegsz*/SBP_SEG_MAX
,
1862 /*flags*/BUS_DMA_ALLOCNOW
,
1863 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
1864 /*lockfunc*/busdma_lock_mutex
,
1869 kprintf("sbp_attach: Could not allocate DMA tag "
1870 "- error %d\n", error
);
1874 devq
= cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB
);
1878 for( i
= 0 ; i
< SBP_NUM_TARGETS
; i
++){
1879 sbp
->targets
[i
].fwdev
= NULL
;
1880 sbp
->targets
[i
].luns
= NULL
;
1883 sbp
->sim
= cam_sim_alloc(sbp_action
, sbp_poll
, "sbp", sbp
,
1884 device_get_unit(dev
),
1887 /*tagged*/ SBP_QUEUE_LEN
- 1,
1889 cam_simq_release(devq
);
1890 if (sbp
->sim
== NULL
)
1893 if (xpt_bus_register(sbp
->sim
, /*bus*/0) != CAM_SUCCESS
)
1896 if (xpt_create_path(&sbp
->path
, xpt_periph
, cam_sim_path(sbp
->sim
),
1897 CAM_TARGET_WILDCARD
, CAM_LUN_WILDCARD
) != CAM_REQ_CMP
) {
1898 xpt_bus_deregister(cam_sim_path(sbp
->sim
));
1902 /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1903 sbp
->fwb
.start
= ((u_int64_t
)SBP_BIND_HI
<< 32) | SBP_DEV2ADDR(0, 0);
1904 sbp
->fwb
.end
= sbp
->fwb
.start
+ 0xffff;
1905 sbp
->fwb
.act_type
= FWACT_XFER
;
1906 /* pre-allocate xfer */
1907 STAILQ_INIT(&sbp
->fwb
.xferlist
);
1908 for (i
= 0; i
< SBP_NUM_OCB
/2; i
++) {
1909 xfer
= fw_xfer_alloc_buf(M_SBP
,
1911 /* recv */SBP_RECV_LEN
);
1912 xfer
->act
.hand
= sbp_recv
;
1914 xfer
->fc
= sbp
->fd
.fc
;
1916 xfer
->sc
= (caddr_t
)sbp
;
1917 STAILQ_INSERT_TAIL(&sbp
->fwb
.xferlist
, xfer
, link
);
1919 fw_bindadd(sbp
->fd
.fc
, &sbp
->fwb
);
1921 sbp
->fd
.post_busreset
= sbp_post_busreset
;
1922 sbp
->fd
.post_explore
= sbp_post_explore
;
1924 if (sbp
->fd
.fc
->status
!= -1) {
1926 sbp_post_busreset((void *)sbp
);
1927 sbp_post_explore((void *)sbp
);
1930 xpt_async(AC_BUS_RESET
, sbp
->path
, /*arg*/ NULL
);
1934 cam_sim_free(sbp
->sim
);
1939 sbp_logout_all(struct sbp_softc
*sbp
)
1941 struct sbp_target
*target
;
1942 struct sbp_dev
*sdev
;
1946 kprintf("sbp_logout_all\n");
1948 for (i
= 0 ; i
< SBP_NUM_TARGETS
; i
++) {
1949 target
= &sbp
->targets
[i
];
1950 if (target
->luns
== NULL
)
1952 for (j
= 0; j
< target
->num_lun
; j
++) {
1953 sdev
= target
->luns
[j
];
1956 callout_stop(&sdev
->login_callout
);
1957 if (sdev
->status
>= SBP_DEV_TOATTACH
&&
1958 sdev
->status
<= SBP_DEV_ATTACHED
)
1959 sbp_mgm_orb(sdev
, ORB_FUN_LGO
, NULL
);
1967 sbp_shutdown(device_t dev
)
1969 struct sbp_softc
*sbp
= ((struct sbp_softc
*)device_get_softc(dev
));
1971 sbp_logout_all(sbp
);
1976 sbp_free_sdev(struct sbp_dev
*sdev
)
1982 for (i
= 0; i
< SBP_QUEUE_LEN
; i
++)
1983 bus_dmamap_destroy(sdev
->target
->sbp
->dmat
,
1984 sdev
->ocb
[i
].dmamap
);
1985 fwdma_free(sdev
->target
->sbp
->fd
.fc
, &sdev
->dma
);
1990 sbp_free_target(struct sbp_target
*target
)
1992 struct fw_xfer
*xfer
, *next
;
1995 if (target
->luns
== NULL
)
1997 callout_stop(&target
->mgm_ocb_timeout
);
1998 for (i
= 0; i
< target
->num_lun
; i
++)
1999 sbp_free_sdev(target
->luns
[i
]);
2001 for (xfer
= STAILQ_FIRST(&target
->xferlist
);
2002 xfer
!= NULL
; xfer
= next
) {
2003 next
= STAILQ_NEXT(xfer
, link
);
2004 fw_xfer_free_buf(xfer
);
2006 STAILQ_INIT(&target
->xferlist
);
2007 kfree(target
->luns
, M_SBP
);
2008 target
->num_lun
= 0;
2009 target
->luns
= NULL
;
2010 target
->fwdev
= NULL
;
2014 sbp_detach(device_t dev
)
2016 struct sbp_softc
*sbp
= ((struct sbp_softc
*)device_get_softc(dev
));
2017 struct firewire_comm
*fc
= sbp
->fd
.fc
;
2018 struct fw_xfer
*xfer
, *next
;
2022 kprintf("sbp_detach\n");
2025 for (i
= 0; i
< SBP_NUM_TARGETS
; i
++)
2026 sbp_cam_detach_target(&sbp
->targets
[i
]);
2027 xpt_async(AC_LOST_DEVICE
, sbp
->path
, NULL
);
2028 xpt_free_path(sbp
->path
);
2029 xpt_bus_deregister(cam_sim_path(sbp
->sim
));
2030 cam_sim_free(sbp
->sim
);
2032 sbp_logout_all(sbp
);
2034 /* XXX wait for logout completion */
2035 tsleep(&i
, FWPRI
, "sbpdtc", hz
/2);
2037 for (i
= 0 ; i
< SBP_NUM_TARGETS
; i
++)
2038 sbp_free_target(&sbp
->targets
[i
]);
2040 for (xfer
= STAILQ_FIRST(&sbp
->fwb
.xferlist
);
2041 xfer
!= NULL
; xfer
= next
) {
2042 next
= STAILQ_NEXT(xfer
, link
);
2043 fw_xfer_free_buf(xfer
);
2045 STAILQ_INIT(&sbp
->fwb
.xferlist
);
2046 fw_bindremove(fc
, &sbp
->fwb
);
2048 bus_dma_tag_destroy(sbp
->dmat
);
2054 sbp_cam_detach_sdev(struct sbp_dev
*sdev
)
2058 if (sdev
->status
== SBP_DEV_DEAD
)
2060 if (sdev
->status
== SBP_DEV_RESET
)
2063 xpt_release_devq(sdev
->path
,
2064 sdev
->freeze
, TRUE
);
2066 xpt_async(AC_LOST_DEVICE
, sdev
->path
, NULL
);
2067 xpt_free_path(sdev
->path
);
2070 sbp_abort_all_ocbs(sdev
, CAM_DEV_NOT_THERE
);
2074 sbp_cam_detach_target(struct sbp_target
*target
)
2078 if (target
->luns
!= NULL
) {
2080 kprintf("sbp_detach_target %d\n", target
->target_id
);
2082 callout_stop(&target
->scan_callout
);
2083 for (i
= 0; i
< target
->num_lun
; i
++)
2084 sbp_cam_detach_sdev(target
->luns
[i
]);
2089 sbp_target_reset(struct sbp_dev
*sdev
, int method
)
2092 struct sbp_target
*target
= sdev
->target
;
2093 struct sbp_dev
*tsdev
;
2095 for (i
= 0; i
< target
->num_lun
; i
++) {
2096 tsdev
= target
->luns
[i
];
2099 if (tsdev
->status
== SBP_DEV_DEAD
)
2101 if (tsdev
->status
== SBP_DEV_RESET
)
2103 xpt_freeze_devq(tsdev
->path
, 1);
2105 sbp_abort_all_ocbs(tsdev
, CAM_CMD_TIMEOUT
);
2107 tsdev
->status
= SBP_DEV_LOGIN
;
2111 kprintf("target reset\n");
2112 sbp_mgm_orb(sdev
, ORB_FUN_RST
, NULL
);
2115 kprintf("reset start\n");
2116 sbp_reset_start(sdev
);
2123 sbp_mgm_timeout(void *arg
)
2125 struct sbp_ocb
*ocb
= (struct sbp_ocb
*)arg
;
2126 struct sbp_dev
*sdev
= ocb
->sdev
;
2127 struct sbp_target
*target
= sdev
->target
;
2129 sbp_show_sdev_info(sdev
, 2);
2130 kprintf("request timeout(mgm orb:0x%08x) ... ",
2131 (u_int32_t
)ocb
->bus_addr
);
2132 target
->mgm_ocb_cur
= NULL
;
2133 sbp_free_ocb(sdev
, ocb
);
2136 kprintf("run next request\n");
2137 sbp_mgm_orb(sdev
, ORB_FUN_RUNQUEUE
, NULL
);
2140 kprintf("reset start\n");
2141 sbp_reset_start(sdev
);
2146 sbp_timeout(void *arg
)
2148 struct sbp_ocb
*ocb
= (struct sbp_ocb
*)arg
;
2149 struct sbp_dev
*sdev
= ocb
->sdev
;
2151 sbp_show_sdev_info(sdev
, 2);
2152 kprintf("request timeout(cmd orb:0x%08x) ... ",
2153 (u_int32_t
)ocb
->bus_addr
);
2156 switch(sdev
->timeout
) {
2158 kprintf("agent reset\n");
2159 xpt_freeze_devq(sdev
->path
, 1);
2161 sbp_abort_all_ocbs(sdev
, CAM_CMD_TIMEOUT
);
2162 sbp_agent_reset(sdev
);
2166 sbp_target_reset(sdev
, sdev
->timeout
- 1);
2171 sbp_cam_detach_target(target
);
2172 if (target
->luns
!= NULL
)
2173 kfree(target
->luns
, M_SBP
);
2174 target
->num_lun
= 0;
2175 target
->luns
= NULL
;
2176 target
->fwdev
= NULL
;
2182 sbp_action1(struct cam_sim
*sim
, union ccb
*ccb
)
2185 struct sbp_softc
*sbp
= (struct sbp_softc
*)sim
->softc
;
2186 struct sbp_target
*target
= NULL
;
2187 struct sbp_dev
*sdev
= NULL
;
2189 /* target:lun -> sdev mapping */
2191 && ccb
->ccb_h
.target_id
!= CAM_TARGET_WILDCARD
2192 && ccb
->ccb_h
.target_id
< SBP_NUM_TARGETS
) {
2193 target
= &sbp
->targets
[ccb
->ccb_h
.target_id
];
2194 if (target
->fwdev
!= NULL
2195 && ccb
->ccb_h
.target_lun
!= CAM_LUN_WILDCARD
2196 && ccb
->ccb_h
.target_lun
< target
->num_lun
) {
2197 sdev
= target
->luns
[ccb
->ccb_h
.target_lun
];
2198 if (sdev
!= NULL
&& sdev
->status
!= SBP_DEV_ATTACHED
&&
2199 sdev
->status
!= SBP_DEV_PROBE
)
2206 kprintf("invalid target %d lun %d\n",
2207 ccb
->ccb_h
.target_id
, ccb
->ccb_h
.target_lun
);
2210 switch (ccb
->ccb_h
.func_code
) {
2213 case XPT_GET_TRAN_SETTINGS
:
2214 case XPT_SET_TRAN_SETTINGS
:
2215 case XPT_CALC_GEOMETRY
:
2218 kprintf("%s:%d:%d:func_code 0x%04x: "
2219 "Invalid target (target needed)\n",
2220 device_get_nameunit(sbp
->fd
.dev
),
2221 ccb
->ccb_h
.target_id
, ccb
->ccb_h
.target_lun
,
2222 ccb
->ccb_h
.func_code
);
2225 ccb
->ccb_h
.status
= CAM_DEV_NOT_THERE
;
2232 /* The opcodes sometimes aimed at a target (sc is valid),
2233 * sometimes aimed at the SIM (sc is invalid and target is
2234 * CAM_TARGET_WILDCARD)
2237 ccb
->ccb_h
.target_id
!= CAM_TARGET_WILDCARD
) {
2239 kprintf("%s:%d:%d func_code 0x%04x: "
2240 "Invalid target (no wildcard)\n",
2241 device_get_nameunit(sbp
->fd
.dev
),
2242 ccb
->ccb_h
.target_id
, ccb
->ccb_h
.target_lun
,
2243 ccb
->ccb_h
.func_code
);
2245 ccb
->ccb_h
.status
= CAM_DEV_NOT_THERE
;
2251 /* XXX Hm, we should check the input parameters */
2255 switch (ccb
->ccb_h
.func_code
) {
2258 struct ccb_scsiio
*csio
;
2259 struct sbp_ocb
*ocb
;
2266 kprintf("%s:%d:%d XPT_SCSI_IO: "
2267 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2269 "%db cmd/%db data/%db sense\n",
2270 device_get_nameunit(sbp
->fd
.dev
),
2271 ccb
->ccb_h
.target_id
, ccb
->ccb_h
.target_lun
,
2272 csio
->cdb_io
.cdb_bytes
[0],
2273 csio
->cdb_io
.cdb_bytes
[1],
2274 csio
->cdb_io
.cdb_bytes
[2],
2275 csio
->cdb_io
.cdb_bytes
[3],
2276 csio
->cdb_io
.cdb_bytes
[4],
2277 csio
->cdb_io
.cdb_bytes
[5],
2278 csio
->cdb_io
.cdb_bytes
[6],
2279 csio
->cdb_io
.cdb_bytes
[7],
2280 csio
->cdb_io
.cdb_bytes
[8],
2281 csio
->cdb_io
.cdb_bytes
[9],
2282 ccb
->ccb_h
.flags
& CAM_DIR_MASK
,
2283 csio
->cdb_len
, csio
->dxfer_len
,
2287 ccb
->ccb_h
.status
= CAM_DEV_NOT_THERE
;
2292 /* if we are in probe stage, pass only probe commands */
2293 if (sdev
->status
== SBP_DEV_PROBE
) {
2295 name
= xpt_path_periph(ccb
->ccb_h
.path
)->periph_name
;
2296 kprintf("probe stage, periph name: %s\n", name
);
2297 if (strcmp(name
, "probe") != 0) {
2298 ccb
->ccb_h
.status
= CAM_REQUEUE_REQ
;
2304 if ((ocb
= sbp_get_ocb(sdev
)) == NULL
) {
2305 ccb
->ccb_h
.status
= CAM_REQUEUE_REQ
;
2310 ocb
->flags
= OCB_ACT_CMD
;
2313 ccb
->ccb_h
.ccb_sdev_ptr
= sdev
;
2314 ocb
->orb
[0] = htonl(1 << 31);
2316 ocb
->orb
[2] = htonl(((sbp
->fd
.fc
->nodeid
| FWLOCALBUS
)<< 16) );
2317 ocb
->orb
[3] = htonl(ocb
->bus_addr
+ IND_PTR_OFFSET
);
2318 speed
= min(target
->fwdev
->speed
, max_speed
);
2319 ocb
->orb
[4] = htonl(ORB_NOTIFY
| ORB_CMD_SPD(speed
)
2320 | ORB_CMD_MAXP(speed
+ 7));
2321 if((ccb
->ccb_h
.flags
& CAM_DIR_MASK
) == CAM_DIR_IN
){
2322 ocb
->orb
[4] |= htonl(ORB_CMD_IN
);
2325 if (csio
->ccb_h
.flags
& CAM_SCATTER_VALID
)
2326 kprintf("sbp: CAM_SCATTER_VALID\n");
2327 if (csio
->ccb_h
.flags
& CAM_DATA_PHYS
)
2328 kprintf("sbp: CAM_DATA_PHYS\n");
2330 if (csio
->ccb_h
.flags
& CAM_CDB_POINTER
)
2331 cdb
= (void *)csio
->cdb_io
.cdb_ptr
;
2333 cdb
= (void *)&csio
->cdb_io
.cdb_bytes
;
2334 bcopy(cdb
, (void *)&ocb
->orb
[5], csio
->cdb_len
);
2336 kprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2337 kprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2339 if (ccb
->csio
.dxfer_len
> 0) {
2343 error
= bus_dmamap_load(/*dma tag*/sbp
->dmat
,
2344 /*dma map*/ocb
->dmamap
,
2346 ccb
->csio
.dxfer_len
,
2352 kprintf("sbp: bus_dmamap_load error %d\n", error
);
2354 sbp_execute_ocb(ocb
, NULL
, 0, 0);
2357 case XPT_CALC_GEOMETRY
:
2359 struct ccb_calc_geometry
*ccg
;
2360 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2362 u_int32_t secs_per_cylinder
;
2367 if (ccg
->block_size
== 0) {
2368 kprintf("sbp_action1: block_size is 0.\n");
2369 ccb
->ccb_h
.status
= CAM_REQ_INVALID
;
2374 kprintf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: Volume size = %ju\n",
2375 device_get_nameunit(sbp
->fd
.dev
),
2376 cam_sim_path(sbp
->sim
),
2377 ccb
->ccb_h
.target_id
, ccb
->ccb_h
.target_lun
,
2378 (uintmax_t)ccg
->volume_size
);
2381 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2382 size_mb
= ccg
->volume_size
2383 / ((1024L * 1024L) / ccg
->block_size
);
2385 if (size_mb
> 1024 && extended
) {
2387 ccg
->secs_per_track
= 63;
2390 ccg
->secs_per_track
= 32;
2392 secs_per_cylinder
= ccg
->heads
* ccg
->secs_per_track
;
2393 ccg
->cylinders
= ccg
->volume_size
/ secs_per_cylinder
;
2394 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
2396 cam_calc_geometry(ccg
, /*extended*/1);
2401 case XPT_RESET_BUS
: /* Reset the specified SCSI bus */
2405 kprintf("%s:%d:XPT_RESET_BUS: \n",
2406 device_get_nameunit(sbp
->fd
.dev
), cam_sim_path(sbp
->sim
));
2409 ccb
->ccb_h
.status
= CAM_REQ_INVALID
;
2413 case XPT_PATH_INQ
: /* Path routing inquiry */
2415 struct ccb_pathinq
*cpi
= &ccb
->cpi
;
2418 kprintf("%s:%d:%d XPT_PATH_INQ:.\n",
2419 device_get_nameunit(sbp
->fd
.dev
),
2420 ccb
->ccb_h
.target_id
, ccb
->ccb_h
.target_lun
);
2422 cpi
->version_num
= 1; /* XXX??? */
2423 cpi
->hba_inquiry
= PI_TAG_ABLE
;
2424 cpi
->target_sprt
= 0;
2425 cpi
->hba_misc
= PIM_NOBUSRESET
| PIM_NO_6_BYTE
;
2426 cpi
->hba_eng_cnt
= 0;
2427 cpi
->max_target
= SBP_NUM_TARGETS
- 1;
2428 cpi
->max_lun
= SBP_NUM_LUNS
- 1;
2429 cpi
->initiator_id
= SBP_INITIATOR
;
2430 cpi
->bus_id
= sim
->bus_id
;
2431 cpi
->base_transfer_speed
= 400 * 1000 / 8;
2432 strncpy(cpi
->sim_vid
, "FreeBSD", SIM_IDLEN
);
2433 strncpy(cpi
->hba_vid
, "SBP", HBA_IDLEN
);
2434 strncpy(cpi
->dev_name
, sim
->sim_name
, DEV_IDLEN
);
2435 cpi
->unit_number
= sim
->unit_number
;
2436 cpi
->transport
= XPORT_SPI
; /* XX should have a FireWire */
2437 cpi
->transport_version
= 2;
2438 cpi
->protocol
= PROTO_SCSI
;
2439 cpi
->protocol_version
= SCSI_REV_2
;
2440 cpi
->maxio
= SBP_MAXPHYS
;
2442 cpi
->ccb_h
.status
= CAM_REQ_CMP
;
2446 case XPT_GET_TRAN_SETTINGS
:
2448 struct ccb_trans_settings
*cts
= &ccb
->cts
;
2449 struct ccb_trans_settings_scsi
*scsi
=
2450 &cts
->proto_specific
.scsi
;
2451 struct ccb_trans_settings_spi
*spi
=
2452 &cts
->xport_specific
.spi
;
2454 cts
->protocol
= PROTO_SCSI
;
2455 cts
->protocol_version
= SCSI_REV_2
;
2456 cts
->transport
= XPORT_SPI
; /* should have a FireWire */
2457 cts
->transport_version
= 2;
2458 spi
->valid
= CTS_SPI_VALID_DISC
;
2459 spi
->flags
= CTS_SPI_FLAGS_DISC_ENB
;
2460 scsi
->valid
= CTS_SCSI_VALID_TQ
;
2461 scsi
->flags
= CTS_SCSI_FLAGS_TAG_ENB
;
2463 kprintf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2464 device_get_nameunit(sbp
->fd
.dev
),
2465 ccb
->ccb_h
.target_id
, ccb
->ccb_h
.target_lun
);
2467 cts
->ccb_h
.status
= CAM_REQ_CMP
;
2472 ccb
->ccb_h
.status
= CAM_UA_ABORT
;
2475 case XPT_SET_TRAN_SETTINGS
:
2478 ccb
->ccb_h
.status
= CAM_REQ_INVALID
;
2486 sbp_action(struct cam_sim
*sim
, union ccb
*ccb
)
2489 sbp_action1(sim
, ccb
);
2494 sbp_execute_ocb(void *arg
, bus_dma_segment_t
*segments
, int seg
, int error
)
2497 struct sbp_ocb
*ocb
;
2498 struct sbp_ocb
*prev
;
2499 bus_dma_segment_t
*s
;
2502 kprintf("sbp_execute_ocb: error=%d\n", error
);
2504 ocb
= (struct sbp_ocb
*)arg
;
2507 kprintf("sbp_execute_ocb: seg %d", seg
);
2508 for (i
= 0; i
< seg
; i
++)
2509 kprintf(", %jx:%jd", (uintmax_t)segments
[i
].ds_addr
,
2510 (uintmax_t)segments
[i
].ds_len
);
2515 /* direct pointer */
2517 if (s
->ds_len
> SBP_SEG_MAX
)
2518 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2519 ocb
->orb
[3] = htonl(s
->ds_addr
);
2520 ocb
->orb
[4] |= htonl(s
->ds_len
);
2521 } else if(seg
> 1) {
2523 for (i
= 0; i
< seg
; i
++) {
2526 /* XXX LSI Logic "< 16 byte" bug might be hit */
2528 kprintf("sbp_execute_ocb: warning, "
2529 "segment length(%zd) is less than 16."
2531 (size_t)s
->ds_len
, i
+1, (intmax_t)seg
);
2533 if (s
->ds_len
> SBP_SEG_MAX
)
2534 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2535 ocb
->ind_ptr
[i
].hi
= htonl(s
->ds_len
<< 16);
2536 ocb
->ind_ptr
[i
].lo
= htonl(s
->ds_addr
);
2538 ocb
->orb
[4] |= htonl(ORB_CMD_PTBL
| seg
);
2542 bus_dmamap_sync(ocb
->sdev
->target
->sbp
->dmat
, ocb
->dmamap
,
2543 (ntohl(ocb
->orb
[4]) & ORB_CMD_IN
) ?
2544 BUS_DMASYNC_PREREAD
: BUS_DMASYNC_PREWRITE
);
2545 prev
= sbp_enqueue_ocb(ocb
->sdev
, ocb
);
2546 fwdma_sync(&ocb
->sdev
->dma
, BUS_DMASYNC_PREWRITE
);
2547 if (prev
== NULL
|| (ocb
->sdev
->flags
& ORB_LINK_DEAD
) != 0) {
2548 ocb
->sdev
->flags
&= ~ORB_LINK_DEAD
;
2549 sbp_orb_pointer(ocb
->sdev
, ocb
);
2554 sbp_poll(struct cam_sim
*sim
)
2556 struct sbp_softc
*sbp
;
2557 struct firewire_comm
*fc
;
2559 sbp
= (struct sbp_softc
*)sim
->softc
;
2562 fc
->poll(fc
, 0, -1);
2567 static struct sbp_ocb
*
2568 sbp_dequeue_ocb(struct sbp_dev
*sdev
, struct sbp_status
*sbp_status
)
2570 struct sbp_ocb
*ocb
;
2571 struct sbp_ocb
*next
;
2577 sbp_show_sdev_info(sdev
, 2);
2578 kprintf("%s: 0x%08x src %d\n",
2579 __func__
, ntohl(sbp_status
->orb_lo
), sbp_status
->src
);
2581 for (ocb
= STAILQ_FIRST(&sdev
->ocbs
); ocb
!= NULL
; ocb
= next
) {
2582 next
= STAILQ_NEXT(ocb
, ocb
);
2583 if (OCB_MATCH(ocb
, sbp_status
)) {
2585 STAILQ_REMOVE(&sdev
->ocbs
, ocb
, sbp_ocb
, ocb
);
2586 if (ocb
->ccb
!= NULL
)
2587 callout_stop(&ocb
->ccb
->ccb_h
.timeout_ch
);
2588 if (ntohl(ocb
->orb
[4]) & 0xffff) {
2589 bus_dmamap_sync(sdev
->target
->sbp
->dmat
,
2591 (ntohl(ocb
->orb
[4]) & ORB_CMD_IN
) ?
2592 BUS_DMASYNC_POSTREAD
:
2593 BUS_DMASYNC_POSTWRITE
);
2594 bus_dmamap_unload(sdev
->target
->sbp
->dmat
,
2597 if (sbp_status
->src
== SRC_NO_NEXT
) {
2599 sbp_orb_pointer(sdev
, next
);
2600 else if (order
> 0) {
2602 * Unordered execution
2603 * We need to send pointer for
2606 sdev
->flags
|= ORB_LINK_DEAD
;
2615 if (ocb
&& order
> 0) {
2616 sbp_show_sdev_info(sdev
, 2);
2617 kprintf("unordered execution order:%d\n", order
);
2623 static struct sbp_ocb
*
2624 sbp_enqueue_ocb(struct sbp_dev
*sdev
, struct sbp_ocb
*ocb
)
2626 struct sbp_ocb
*prev
;
2631 sbp_show_sdev_info(sdev
, 2);
2632 kprintf("%s: 0x%08jx\n", __func__
, (uintmax_t)ocb
->bus_addr
);
2634 prev
= STAILQ_LAST(&sdev
->ocbs
, sbp_ocb
, ocb
);
2635 STAILQ_INSERT_TAIL(&sdev
->ocbs
, ocb
, ocb
);
2637 if (ocb
->ccb
!= NULL
)
2638 callout_reset(&ocb
->ccb
->ccb_h
.timeout_ch
,
2639 (ocb
->ccb
->ccb_h
.timeout
* hz
) / 1000, sbp_timeout
, ocb
);
2643 kprintf("linking chain 0x%jx -> 0x%jx\n",
2644 (uintmax_t)prev
->bus_addr
, (uintmax_t)ocb
->bus_addr
);
2646 prev
->orb
[1] = htonl(ocb
->bus_addr
);
2654 static struct sbp_ocb
*
2655 sbp_get_ocb(struct sbp_dev
*sdev
)
2657 struct sbp_ocb
*ocb
;
2660 ocb
= STAILQ_FIRST(&sdev
->free_ocbs
);
2662 kprintf("ocb shortage!!!\n");
2666 STAILQ_REMOVE_HEAD(&sdev
->free_ocbs
, ocb
);
2673 sbp_free_ocb(struct sbp_dev
*sdev
, struct sbp_ocb
*ocb
)
2677 STAILQ_INSERT_TAIL(&sdev
->free_ocbs
, ocb
, ocb
);
2681 sbp_abort_ocb(struct sbp_ocb
*ocb
, int status
)
2683 struct sbp_dev
*sdev
;
2687 sbp_show_sdev_info(sdev
, 2);
2688 kprintf("sbp_abort_ocb 0x%jx\n", (uintmax_t)ocb
->bus_addr
);
2691 if (ocb
->ccb
!= NULL
)
2692 sbp_print_scsi_cmd(ocb
);
2694 if (ntohl(ocb
->orb
[4]) & 0xffff) {
2695 bus_dmamap_sync(sdev
->target
->sbp
->dmat
, ocb
->dmamap
,
2696 (ntohl(ocb
->orb
[4]) & ORB_CMD_IN
) ?
2697 BUS_DMASYNC_POSTREAD
: BUS_DMASYNC_POSTWRITE
);
2698 bus_dmamap_unload(sdev
->target
->sbp
->dmat
, ocb
->dmamap
);
2700 if (ocb
->ccb
!= NULL
) {
2701 callout_stop(&ocb
->ccb
->ccb_h
.timeout_ch
);
2702 ocb
->ccb
->ccb_h
.status
= status
;
2705 sbp_free_ocb(sdev
, ocb
);
2709 sbp_abort_all_ocbs(struct sbp_dev
*sdev
, int status
)
2711 struct sbp_ocb
*ocb
, *next
;
2712 STAILQ_HEAD(, sbp_ocb
) temp
;
2716 STAILQ_CONCAT(&temp
, &sdev
->ocbs
);
2717 for (ocb
= STAILQ_FIRST(&temp
); ocb
!= NULL
; ocb
= next
) {
2718 next
= STAILQ_NEXT(ocb
, ocb
);
2719 sbp_abort_ocb(ocb
, status
);
2724 static devclass_t sbp_devclass
;
2727 * Because sbp is a static device that always exists under any attached
2728 * firewire device, and not scanned by the firewire device, we need an
2729 * identify function to install the device. For our sanity we want
2730 * the sbp device to have the same unit number as the fireweire device.
2733 static device_method_t sbp_methods
[] = {
2734 /* device interface */
2735 DEVMETHOD(device_identify
, bus_generic_identify_sameunit
),
2736 DEVMETHOD(device_probe
, sbp_probe
),
2737 DEVMETHOD(device_attach
, sbp_attach
),
2738 DEVMETHOD(device_detach
, sbp_detach
),
2739 DEVMETHOD(device_shutdown
, sbp_shutdown
),
2744 static driver_t sbp_driver
= {
2747 sizeof(struct sbp_softc
),
2750 DECLARE_DUMMY_MODULE(sbp
);
2751 DRIVER_MODULE(sbp
, firewire
, sbp_driver
, sbp_devclass
, NULL
, NULL
);
2752 MODULE_VERSION(sbp
, 1);
2753 MODULE_DEPEND(sbp
, firewire
, 1, 1, 1);
2754 MODULE_DEPEND(sbp
, cam
, 1, 1, 1);