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.74 2004/01/08 14:58:09 simokawa Exp $
35 * $DragonFly: src/sys/dev/disk/sbp/sbp.c,v 1.9 2004/02/05 17:51:44 joerg Exp $
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/module.h>
44 #include <sys/kernel.h>
45 #include <sys/sysctl.h>
46 #include <machine/bus.h>
47 #include <sys/malloc.h>
48 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
50 #include <sys/mutex.h>
53 #if defined(__DragonFly__) || __FreeBSD_version < 500106
54 #include <sys/devicestat.h> /* for struct devstat */
58 #include <bus/cam/cam.h>
59 #include <bus/cam/cam_ccb.h>
60 #include <bus/cam/cam_sim.h>
61 #include <bus/cam/cam_xpt_sim.h>
62 #include <bus/cam/cam_debug.h>
63 #include <bus/cam/cam_periph.h>
64 #include <bus/cam/scsi/scsi_all.h>
66 #include <bus/firewire/firewire.h>
67 #include <bus/firewire/firewirereg.h>
68 #include <bus/firewire/fwdma.h>
69 #include <bus/firewire/iec13213.h>
73 #include <cam/cam_ccb.h>
74 #include <cam/cam_sim.h>
75 #include <cam/cam_xpt_sim.h>
76 #include <cam/cam_debug.h>
77 #include <cam/cam_periph.h>
78 #include <cam/scsi/scsi_all.h>
81 #define ccb_sdev_ptr spriv_ptr0
82 #define ccb_sbp_ptr spriv_ptr1
84 #define SBP_NUM_TARGETS 8 /* MAX 64 */
86 * Scan_bus doesn't work for more than 8 LUNs
87 * because of CAM_SCSI2_MAXLUN in cam_xpt.c
89 #define SBP_NUM_LUNS 64
90 #define SBP_DMA_SIZE PAGE_SIZE
91 #define SBP_LOGIN_SIZE sizeof(struct sbp_login_res)
92 #define SBP_QUEUE_LEN ((SBP_DMA_SIZE - SBP_LOGIN_SIZE) / sizeof(struct sbp_ocb))
93 #define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS)
96 * STATUS FIFO addressing
98 * -----------------------
99 * 0- 1( 2): 0 (alingment)
102 * 16-31( 8): reserved
103 * 32-47(16): SBP_BIND_HI
104 * 48-64(16): bus_id, node_id
106 #define SBP_BIND_HI 0x1
107 #define SBP_DEV2ADDR(t, l) \
108 (((u_int64_t)SBP_BIND_HI << 32) \
109 | (((l) & 0xff) << 8) \
110 | (((t) & 0x3f) << 2))
111 #define SBP_ADDR2TRG(a) (((a) >> 2) & 0x3f)
112 #define SBP_ADDR2LUN(a) (((a) >> 8) & 0xff)
113 #define SBP_INITIATOR 7
115 static char *orb_fun_name[] = {
119 static int debug = 0;
120 static int auto_login = 1;
121 static int max_speed = -1;
122 static int sbp_cold = 1;
123 static int ex_login = 1;
124 static int login_delay = 1000; /* msec */
125 static int scan_delay = 500; /* msec */
126 static int sbp_tags = 0;
128 SYSCTL_DECL(_hw_firewire);
129 SYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem");
130 SYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0,
132 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0,
133 "SBP perform login automatically");
134 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0,
135 "SBP transfer max speed");
136 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, exclusive_login, CTLFLAG_RW,
137 &ex_login, 0, "SBP transfer max speed");
138 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, login_delay, CTLFLAG_RW,
139 &login_delay, 0, "SBP login delay in msec");
140 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, scan_delay, CTLFLAG_RW,
141 &scan_delay, 0, "SBP scan delay in msec");
142 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, tags, CTLFLAG_RW, &sbp_tags, 0,
143 "SBP tagged queuing support");
145 TUNABLE_INT("hw.firewire.sbp.auto_login", &auto_login);
146 TUNABLE_INT("hw.firewire.sbp.max_speed", &max_speed);
147 TUNABLE_INT("hw.firewire.sbp.exclusive_login", &ex_login);
148 TUNABLE_INT("hw.firewire.sbp.login_delay", &login_delay);
149 TUNABLE_INT("hw.firewire.sbp.scan_delay", &scan_delay);
150 TUNABLE_INT("hw.firewire.sbp.tags", &sbp_tags);
152 #define NEED_RESPONSE 0
154 #define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE)
155 #ifdef __sparc64__ /* iommu */
156 #define SBP_IND_MAX howmany(MAXPHYS, SBP_SEG_MAX)
158 #define SBP_IND_MAX howmany(MAXPHYS, PAGE_SIZE)
161 STAILQ_ENTRY(sbp_ocb) ocb;
165 #define IND_PTR_OFFSET (8*sizeof(u_int32_t))
166 struct ind_ptr ind_ptr[SBP_IND_MAX];
167 struct sbp_dev *sdev;
168 int flags; /* XXX should be removed */
172 #define OCB_ACT_MGM 0
173 #define OCB_ACT_CMD 1
174 #define OCB_MATCH(o,s) ((o)->bus_addr == ntohl((s)->orb_lo))
177 #define SBP_DEV_RESET 0 /* accept login */
178 #define SBP_DEV_LOGIN 1 /* to login */
180 #define SBP_DEV_RECONN 2 /* to reconnect */
182 #define SBP_DEV_TOATTACH 3 /* to attach */
183 #define SBP_DEV_PROBE 4 /* scan lun */
184 #define SBP_DEV_ATTACHED 5 /* in operation */
185 #define SBP_DEV_DEAD 6 /* unavailable unit */
186 #define SBP_DEV_RETRY 7 /* unavailable unit */
192 #define ORB_LINK_DEAD (1 << 0)
193 #define VALID_LUN (1 << 1)
194 #define ORB_POINTER_ACTIVE (1 << 2)
195 #define ORB_POINTER_NEED (1 << 3)
197 struct cam_path *path;
198 struct sbp_target *target;
199 struct fwdma_alloc dma;
200 struct sbp_login_res *login;
201 struct callout login_callout;
203 STAILQ_HEAD(, sbp_ocb) ocbs;
204 STAILQ_HEAD(, sbp_ocb) free_ocbs;
213 struct sbp_dev **luns;
214 struct sbp_softc *sbp;
215 struct fw_device *fwdev;
216 u_int32_t mgm_hi, mgm_lo;
217 struct sbp_ocb *mgm_ocb_cur;
218 STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue;
219 struct callout mgm_ocb_timeout;
220 struct callout scan_callout;
221 STAILQ_HEAD(, fw_xfer) xferlist;
226 struct firewire_dev_comm fd;
228 struct cam_path *path;
229 struct sbp_target targets[SBP_NUM_TARGETS];
232 struct timeval last_busreset;
233 #define SIMQ_FREEZED 1
237 static void sbp_post_explore (void *);
238 static void sbp_recv (struct fw_xfer *);
239 static void sbp_mgm_callback (struct fw_xfer *);
241 static void sbp_cmd_callback (struct fw_xfer *);
243 static void sbp_orb_pointer (struct sbp_dev *, struct sbp_ocb *);
244 static void sbp_execute_ocb (void *, bus_dma_segment_t *, int, int);
245 static void sbp_free_ocb (struct sbp_dev *, struct sbp_ocb *);
246 static void sbp_abort_ocb (struct sbp_ocb *, int);
247 static void sbp_abort_all_ocbs (struct sbp_dev *, int);
248 static struct fw_xfer * sbp_write_cmd (struct sbp_dev *, int, int);
249 static struct sbp_ocb * sbp_get_ocb (struct sbp_dev *);
250 static struct sbp_ocb * sbp_enqueue_ocb (struct sbp_dev *, struct sbp_ocb *);
251 static struct sbp_ocb * sbp_dequeue_ocb (struct sbp_dev *, struct sbp_status *);
252 static void sbp_cam_detach_sdev(struct sbp_dev *);
253 static void sbp_free_sdev(struct sbp_dev *);
254 static void sbp_cam_detach_target (struct sbp_target *);
255 static void sbp_free_target (struct sbp_target *);
256 static void sbp_mgm_timeout (void *arg);
257 static void sbp_timeout (void *arg);
258 static void sbp_mgm_orb (struct sbp_dev *, int, struct sbp_ocb *);
260 MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire");
262 /* cam related functions */
263 static void sbp_action(struct cam_sim *sim, union ccb *ccb);
264 static void sbp_poll(struct cam_sim *sim);
265 static void sbp_cam_scan_lun(struct cam_periph *, union ccb *);
266 static void sbp_cam_scan_target(void *arg);
268 static char *orb_status0[] = {
269 /* 0 */ "No additional information to report",
270 /* 1 */ "Request type not supported",
271 /* 2 */ "Speed not supported",
272 /* 3 */ "Page size not supported",
273 /* 4 */ "Access denied",
274 /* 5 */ "Logical unit not supported",
275 /* 6 */ "Maximum payload too small",
276 /* 7 */ "Reserved for future standardization",
277 /* 8 */ "Resources unavailable",
278 /* 9 */ "Function rejected",
279 /* A */ "Login ID not recognized",
280 /* B */ "Dummy ORB completed",
281 /* C */ "Request aborted",
282 /* FF */ "Unspecified error"
283 #define MAX_ORB_STATUS0 0xd
286 static char *orb_status1_object[] = {
287 /* 0 */ "Operation request block (ORB)",
288 /* 1 */ "Data buffer",
289 /* 2 */ "Page table",
290 /* 3 */ "Unable to specify"
293 static char *orb_status1_serial_bus_error[] = {
294 /* 0 */ "Missing acknowledge",
295 /* 1 */ "Reserved; not to be used",
296 /* 2 */ "Time-out error",
297 /* 3 */ "Reserved; not to be used",
298 /* 4 */ "Busy retry limit exceeded(X)",
299 /* 5 */ "Busy retry limit exceeded(A)",
300 /* 6 */ "Busy retry limit exceeded(B)",
301 /* 7 */ "Reserved for future standardization",
302 /* 8 */ "Reserved for future standardization",
303 /* 9 */ "Reserved for future standardization",
304 /* A */ "Reserved for future standardization",
305 /* B */ "Tardy retry limit exceeded",
306 /* C */ "Conflict error",
307 /* D */ "Data error",
308 /* E */ "Type error",
309 /* F */ "Address error"
313 sbp_identify(driver_t *driver, device_t parent)
317 printf("sbp_identify\n");
320 child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent));
327 sbp_probe(device_t dev)
332 printf("sbp_probe\n");
335 pa = device_get_parent(dev);
336 if(device_get_unit(dev) != device_get_unit(pa)){
340 device_set_desc(dev, "SBP-2/SCSI over FireWire");
348 sbp_show_sdev_info(struct sbp_dev *sdev, int new)
350 struct fw_device *fwdev;
353 device_get_nameunit(sdev->target->sbp->fd.dev),
354 sdev->target->target_id,
360 fwdev = sdev->target->fwdev;
361 printf("ordered:%d type:%d EUI:%08x%08x node:%d "
362 "speed:%d maxrec:%d",
363 (sdev->type & 0x40) >> 6,
375 sbp_show_sdev_info(sdev, 2);
376 printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision);
384 /* Bus Target EUI64 */
386 {0, 2, {0x00018ea0, 0x01fd0154}}, /* Logitec HDD */
387 {0, 0, {0x00018ea6, 0x00100682}}, /* Logitec DVD */
388 {0, 1, {0x00d03200, 0xa412006a}}, /* Yano HDD */
394 sbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev)
396 int bus, i, target=-1;
397 char w[SBP_NUM_TARGETS];
400 bus = device_get_unit(sbp->fd.dev);
402 /* XXX wired-down configuration should be gotten from
403 tunable or device hint */
404 for (i = 0; wired[i].bus >= 0; i ++) {
405 if (wired[i].bus == bus) {
406 w[wired[i].target] = 1;
407 if (wired[i].eui.hi == fwdev->eui.hi &&
408 wired[i].eui.lo == fwdev->eui.lo)
409 target = wired[i].target;
413 if(target < SBP_NUM_TARGETS &&
414 sbp->targets[target].fwdev == NULL)
416 device_printf(sbp->fd.dev,
417 "target %d is not free for %08x:%08x\n",
418 target, fwdev->eui.hi, fwdev->eui.lo);
421 /* non-wired target */
422 for (i = 0; i < SBP_NUM_TARGETS; i ++)
423 if (sbp->targets[i].fwdev == NULL && w[i] == 0) {
432 sbp_alloc_lun(struct sbp_target *target)
434 struct crom_context cc;
436 struct sbp_dev *sdev, **newluns;
437 struct sbp_softc *sbp;
441 crom_init_context(&cc, target->fwdev->csrrom);
442 /* XXX shoud parse appropriate unit directories only */
444 while (cc.depth >= 0) {
445 reg = crom_search_key(&cc, CROM_LUN);
448 lun = reg->val & 0xffff;
450 printf("target %d lun %d found\n", target->target_id, lun);
457 printf("%s:%d no LUN found\n",
458 device_get_nameunit(target->sbp->fd.dev),
462 if (maxlun >= SBP_NUM_LUNS)
463 maxlun = SBP_NUM_LUNS;
465 /* Invalidiate stale devices */
466 for (lun = 0; lun < target->num_lun; lun ++) {
467 sdev = target->luns[lun];
470 sdev->flags &= ~VALID_LUN;
473 sbp_cam_detach_sdev(sdev);
479 if (maxlun != target->num_lun) {
480 newluns = (struct sbp_dev **) realloc(target->luns,
481 sizeof(struct sbp_dev *) * maxlun,
482 M_SBP, M_NOWAIT | M_ZERO);
484 if (newluns == NULL) {
485 printf("%s: realloc failed\n", __FUNCTION__);
486 newluns = target->luns;
487 maxlun = target->num_lun;
491 * We must zero the extended region for the case
492 * realloc() doesn't allocate new buffer.
494 if (maxlun > target->num_lun)
495 bzero(&newluns[target->num_lun],
496 sizeof(struct sbp_dev *) *
497 (maxlun - target->num_lun));
499 target->luns = newluns;
500 target->num_lun = maxlun;
503 crom_init_context(&cc, target->fwdev->csrrom);
504 while (cc.depth >= 0) {
507 reg = crom_search_key(&cc, CROM_LUN);
510 lun = reg->val & 0xffff;
511 if (lun >= SBP_NUM_LUNS) {
512 printf("too large lun %d\n", lun);
516 sdev = target->luns[lun];
518 sdev = malloc(sizeof(struct sbp_dev),
519 M_SBP, M_NOWAIT | M_ZERO);
521 printf("%s: malloc failed\n", __FUNCTION__);
524 target->luns[lun] = sdev;
526 sdev->target = target;
527 STAILQ_INIT(&sdev->ocbs);
528 CALLOUT_INIT(&sdev->login_callout);
529 sdev->status = SBP_DEV_RESET;
532 sdev->flags |= VALID_LUN;
533 sdev->type = (reg->val & 0xff0000) >> 16;
538 fwdma_malloc(sbp->fd.fc,
539 /* alignment */ sizeof(u_int32_t),
540 SBP_DMA_SIZE, &sdev->dma, BUS_DMA_NOWAIT);
541 if (sdev->dma.v_addr == NULL) {
542 printf("%s: dma space allocation failed\n",
545 target->luns[lun] = NULL;
548 sdev->login = (struct sbp_login_res *) sdev->dma.v_addr;
549 sdev->ocb = (struct sbp_ocb *)
550 ((char *)sdev->dma.v_addr + SBP_LOGIN_SIZE);
551 bzero((char *)sdev->ocb,
552 sizeof (struct sbp_ocb) * SBP_QUEUE_LEN);
554 STAILQ_INIT(&sdev->free_ocbs);
555 for (i = 0; i < SBP_QUEUE_LEN; i++) {
558 ocb->bus_addr = sdev->dma.bus_addr
560 + sizeof(struct sbp_ocb) * i
561 + offsetof(struct sbp_ocb, orb[0]);
562 if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
563 printf("sbp_attach: cannot create dmamap\n");
567 sbp_free_ocb(sdev, ocb);
573 for (lun = 0; lun < target->num_lun; lun ++) {
574 sdev = target->luns[lun];
575 if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) {
576 sbp_cam_detach_sdev(sdev);
578 target->luns[lun] = NULL;
583 static struct sbp_target *
584 sbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
587 struct sbp_target *target;
588 struct crom_context cc;
592 printf("sbp_alloc_target\n");
594 i = sbp_new_target(sbp, fwdev);
596 device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n");
600 target = &sbp->targets[i];
602 target->fwdev = fwdev;
603 target->target_id = i;
604 /* XXX we may want to reload mgm port after each bus reset */
605 /* XXX there might be multiple management agents */
606 crom_init_context(&cc, target->fwdev->csrrom);
607 reg = crom_search_key(&cc, CROM_MGM);
608 if (reg == NULL || reg->val == 0) {
609 printf("NULL management address\n");
610 target->fwdev = NULL;
613 target->mgm_hi = 0xffff;
614 target->mgm_lo = 0xf0000000 | (reg->val << 2);
615 target->mgm_ocb_cur = NULL;
617 printf("target:%d mgm_port: %x\n", i, target->mgm_lo);
619 STAILQ_INIT(&target->xferlist);
621 STAILQ_INIT(&target->mgm_ocb_queue);
622 CALLOUT_INIT(&target->mgm_ocb_timeout);
623 CALLOUT_INIT(&target->scan_callout);
631 sbp_probe_lun(struct sbp_dev *sdev)
633 struct fw_device *fwdev;
634 struct crom_context c, *cc = &c;
637 bzero(sdev->vendor, sizeof(sdev->vendor));
638 bzero(sdev->product, sizeof(sdev->product));
640 fwdev = sdev->target->fwdev;
641 crom_init_context(cc, fwdev->csrrom);
642 /* get vendor string */
643 crom_search_key(cc, CSRKEY_VENDOR);
645 crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor));
646 /* skip to the unit directory for SBP-2 */
647 while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) {
648 if (reg->val == CSRVAL_T10SBP2)
652 /* get firmware revision */
653 reg = crom_search_key(cc, CSRKEY_FIRM_VER);
655 snprintf(sdev->revision, sizeof(sdev->revision),
657 /* get product string */
658 crom_search_key(cc, CSRKEY_MODEL);
660 crom_parse_text(cc, sdev->product, sizeof(sdev->product));
664 sbp_login_callout(void *arg)
666 struct sbp_dev *sdev = (struct sbp_dev *)arg;
667 sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
671 sbp_login(struct sbp_dev *sdev)
673 struct timeval delta;
678 timevalsub(&delta, &sdev->target->sbp->last_busreset);
679 t.tv_sec = login_delay / 1000;
680 t.tv_usec = (login_delay % 1000) * 1000;
681 timevalsub(&t, &delta);
682 if (t.tv_sec >= 0 && t.tv_usec > 0)
683 ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000;
685 printf("%s: sec = %ld usec = %ld ticks = %d\n", __FUNCTION__,
686 t.tv_sec, t.tv_usec, ticks);
688 callout_reset(&sdev->login_callout, ticks,
689 sbp_login_callout, (void *)(sdev));
692 #define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
693 && crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
696 sbp_probe_target(void *arg)
698 struct sbp_target *target = (struct sbp_target *)arg;
699 struct sbp_softc *sbp;
700 struct sbp_dev *sdev;
701 struct firewire_comm *fc;
704 alive = SBP_FWDEV_ALIVE(target->fwdev);
706 printf("sbp_probe_target %d\n", target->target_id);
708 printf("not alive\n");
712 fc = target->sbp->fd.fc;
713 sbp_alloc_lun(target);
715 /* XXX untimeout mgm_ocb and dequeue */
716 for (i=0; i < target->num_lun; i++) {
717 sdev = target->luns[i];
720 if (alive && (sdev->status != SBP_DEV_DEAD)) {
721 if (sdev->path != NULL) {
722 xpt_freeze_devq(sdev->path, 1);
727 sbp_show_sdev_info(sdev,
728 (sdev->status == SBP_DEV_RESET));
731 sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
732 switch (sdev->status) {
734 /* new or revived target */
738 case SBP_DEV_TOATTACH:
740 case SBP_DEV_ATTACHED:
743 sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
747 switch (sdev->status) {
748 case SBP_DEV_ATTACHED:
750 /* the device has gone */
751 sbp_show_sdev_info(sdev, 2);
752 printf("lost target\n");
755 xpt_freeze_devq(sdev->path, 1);
758 sdev->status = SBP_DEV_RETRY;
759 sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
762 case SBP_DEV_TOATTACH:
763 sdev->status = SBP_DEV_RESET;
775 sbp_post_busreset(void *arg)
777 struct sbp_softc *sbp;
779 sbp = (struct sbp_softc *)arg;
781 printf("sbp_post_busreset\n");
783 if ((sbp->sim->flags & SIMQ_FREEZED) == 0) {
784 xpt_freeze_simq(sbp->sim, /*count*/1);
785 sbp->sim->flags |= SIMQ_FREEZED;
787 microtime(&sbp->last_busreset);
791 sbp_post_explore(void *arg)
793 struct sbp_softc *sbp = (struct sbp_softc *)arg;
794 struct sbp_target *target;
795 struct fw_device *fwdev;
799 printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
806 * XXX don't let CAM the bus rest.
807 * CAM tries to do something with freezed (DEV_RETRY) devices.
809 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
812 /* Gabage Collection */
813 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
814 target = &sbp->targets[i];
815 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
816 if (target->fwdev == NULL || target->fwdev == fwdev)
819 /* device has removed in lower driver */
820 sbp_cam_detach_target(target);
821 sbp_free_target(target);
824 /* traverse device list */
825 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
827 printf("sbp_post_explore: EUI:%08x%08x ",
828 fwdev->eui.hi, fwdev->eui.lo);
829 if (fwdev->status != FWDEVATTACHED)
830 printf("not attached, state=%d.\n", fwdev->status);
832 printf("attached\n");
834 alive = SBP_FWDEV_ALIVE(fwdev);
835 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
836 target = &sbp->targets[i];
837 if(target->fwdev == fwdev ) {
842 if(i == SBP_NUM_TARGETS){
845 target = sbp_alloc_target(sbp, fwdev);
852 sbp_probe_target((void *)target);
853 if (target->num_lun == 0)
854 sbp_free_target(target);
856 xpt_release_simq(sbp->sim, /*run queue*/TRUE);
857 sbp->sim->flags &= ~SIMQ_FREEZED;
862 sbp_loginres_callback(struct fw_xfer *xfer){
864 struct sbp_dev *sdev;
865 sdev = (struct sbp_dev *)xfer->sc;
867 sbp_show_sdev_info(sdev, 2);
868 printf("sbp_loginres_callback\n");
872 STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link);
879 sbp_xfer_free(struct fw_xfer *xfer)
881 struct sbp_dev *sdev;
884 sdev = (struct sbp_dev *)xfer->sc;
885 fw_xfer_unload(xfer);
887 STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
892 sbp_reset_start_callback(struct fw_xfer *xfer)
894 struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
895 struct sbp_target *target = sdev->target;
898 if (xfer->resp != 0) {
899 sbp_show_sdev_info(sdev, 2);
900 printf("sbp_reset_start failed: resp=%d\n", xfer->resp);
903 for (i = 0; i < target->num_lun; i++) {
904 tsdev = target->luns[i];
905 if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN)
911 sbp_reset_start(struct sbp_dev *sdev)
913 struct fw_xfer *xfer;
917 sbp_show_sdev_info(sdev, 2);
918 printf("sbp_reset_start\n");
921 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
922 xfer->act.hand = sbp_reset_start_callback;
923 fp = &xfer->send.hdr;
924 fp->mode.wreqq.dest_hi = 0xffff;
925 fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
926 fp->mode.wreqq.data = htonl(0xf);
927 fw_asyreq(xfer->fc, -1, xfer);
931 sbp_mgm_callback(struct fw_xfer *xfer)
933 struct sbp_dev *sdev;
936 sdev = (struct sbp_dev *)xfer->sc;
939 sbp_show_sdev_info(sdev, 2);
940 printf("sbp_mgm_callback\n");
946 sbp_show_sdev_info(sdev, 2);
947 printf("management ORB failed(%d) ... RESET_START\n", resp);
948 sbp_reset_start(sdev);
954 static struct sbp_dev *
955 sbp_next_dev(struct sbp_target *target, int lun)
957 struct sbp_dev **sdevp;
960 for (i = lun, sdevp = &target->luns[lun]; i < target->num_lun;
962 if (*sdevp != NULL && (*sdevp)->status == SBP_DEV_PROBE)
969 sbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb)
971 struct sbp_target *target;
972 struct sbp_dev *sdev;
974 sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
975 target = sdev->target;
977 sbp_show_sdev_info(sdev, 2);
978 printf("sbp_cam_scan_lun\n");
980 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
981 sdev->status = SBP_DEV_ATTACHED;
983 sbp_show_sdev_info(sdev, 2);
984 printf("scan failed\n");
986 sdev = sbp_next_dev(target, sdev->lun_id + 1);
992 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
993 ccb->ccb_h.ccb_sdev_ptr = sdev;
995 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1000 sbp_cam_scan_target(void *arg)
1002 struct sbp_target *target = (struct sbp_target *)arg;
1003 struct sbp_dev *sdev;
1006 sdev = sbp_next_dev(target, 0);
1008 printf("sbp_cam_scan_target: nothing to do for target%d\n",
1013 sbp_show_sdev_info(sdev, 2);
1014 printf("sbp_cam_scan_target\n");
1016 ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO);
1018 printf("sbp_cam_scan_target: malloc failed\n");
1021 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
1022 ccb->ccb_h.func_code = XPT_SCAN_LUN;
1023 ccb->ccb_h.cbfcnp = sbp_cam_scan_lun;
1024 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1025 ccb->crcn.flags = CAM_FLAG_NONE;
1026 ccb->ccb_h.ccb_sdev_ptr = sdev;
1028 /* The scan is in progress now. */
1030 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1034 static __inline void
1035 sbp_scan_dev(struct sbp_dev *sdev)
1037 sdev->status = SBP_DEV_PROBE;
1038 callout_reset(&sdev->target->scan_callout, scan_delay * hz / 1000,
1039 sbp_cam_scan_target, (void *)sdev->target);
1043 sbp_do_attach(struct fw_xfer *xfer)
1045 struct sbp_dev *sdev;
1046 struct sbp_target *target;
1047 struct sbp_softc *sbp;
1049 sdev = (struct sbp_dev *)xfer->sc;
1050 target = sdev->target;
1053 sbp_show_sdev_info(sdev, 2);
1054 printf("sbp_do_attach\n");
1056 sbp_xfer_free(xfer);
1058 if (sdev->path == NULL)
1059 xpt_create_path(&sdev->path, xpt_periph,
1060 cam_sim_path(target->sbp->sim),
1061 target->target_id, sdev->lun_id);
1064 * Let CAM scan the bus if we are in the boot process.
1065 * XXX xpt_scan_bus cannot detect LUN larger than 0
1066 * if LUN 0 doesn't exists.
1069 sdev->status = SBP_DEV_ATTACHED;
1078 sbp_agent_reset_callback(struct fw_xfer *xfer)
1080 struct sbp_dev *sdev;
1082 sdev = (struct sbp_dev *)xfer->sc;
1084 sbp_show_sdev_info(sdev, 2);
1085 printf("%s\n", __FUNCTION__);
1087 if (xfer->resp != 0) {
1088 sbp_show_sdev_info(sdev, 2);
1089 printf("%s: resp=%d\n", __FUNCTION__, xfer->resp);
1092 sbp_xfer_free(xfer);
1094 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1100 sbp_agent_reset(struct sbp_dev *sdev)
1102 struct fw_xfer *xfer;
1106 sbp_show_sdev_info(sdev, 2);
1107 printf("sbp_agent_reset\n");
1109 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1112 if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE)
1113 xfer->act.hand = sbp_agent_reset_callback;
1115 xfer->act.hand = sbp_do_attach;
1116 fp = &xfer->send.hdr;
1117 fp->mode.wreqq.data = htonl(0xf);
1118 fw_asyreq(xfer->fc, -1, xfer);
1119 sbp_abort_all_ocbs(sdev, CAM_BDR_SENT);
1123 sbp_busy_timeout_callback(struct fw_xfer *xfer)
1125 struct sbp_dev *sdev;
1127 sdev = (struct sbp_dev *)xfer->sc;
1129 sbp_show_sdev_info(sdev, 2);
1130 printf("sbp_busy_timeout_callback\n");
1132 sbp_xfer_free(xfer);
1133 sbp_agent_reset(sdev);
1137 sbp_busy_timeout(struct sbp_dev *sdev)
1140 struct fw_xfer *xfer;
1142 sbp_show_sdev_info(sdev, 2);
1143 printf("sbp_busy_timeout\n");
1145 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1147 xfer->act.hand = sbp_busy_timeout_callback;
1148 fp = &xfer->send.hdr;
1149 fp->mode.wreqq.dest_hi = 0xffff;
1150 fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1151 fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1152 fw_asyreq(xfer->fc, -1, xfer);
1156 sbp_orb_pointer_callback(struct fw_xfer *xfer)
1158 struct sbp_dev *sdev;
1159 sdev = (struct sbp_dev *)xfer->sc;
1162 sbp_show_sdev_info(sdev, 2);
1163 printf("%s\n", __FUNCTION__);
1165 if (xfer->resp != 0) {
1167 printf("%s: xfer->resp = %d\n", __FUNCTION__, xfer->resp);
1169 sbp_xfer_free(xfer);
1170 sdev->flags &= ~ORB_POINTER_ACTIVE;
1172 if ((sdev->flags & ORB_POINTER_NEED) != 0) {
1173 struct sbp_ocb *ocb;
1175 sdev->flags &= ~ORB_POINTER_NEED;
1176 ocb = STAILQ_FIRST(&sdev->ocbs);
1178 sbp_orb_pointer(sdev, ocb);
1184 sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1186 struct fw_xfer *xfer;
1189 sbp_show_sdev_info(sdev, 2);
1190 printf("%s: 0x%08x\n", __FUNCTION__, (u_int32_t)ocb->bus_addr);
1193 if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) {
1195 printf("%s: orb pointer active\n", __FUNCTION__);
1197 sdev->flags |= ORB_POINTER_NEED;
1201 sdev->flags |= ORB_POINTER_ACTIVE;
1202 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1205 xfer->act.hand = sbp_orb_pointer_callback;
1207 fp = &xfer->send.hdr;
1208 fp->mode.wreqb.len = 8;
1209 fp->mode.wreqb.extcode = 0;
1210 xfer->send.payload[0] =
1211 htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1212 xfer->send.payload[1] = htonl((u_int32_t)ocb->bus_addr);
1214 if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1215 sbp_xfer_free(xfer);
1216 ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1223 sbp_cmd_callback(struct fw_xfer *xfer)
1226 struct sbp_dev *sdev;
1227 sdev = (struct sbp_dev *)xfer->sc;
1228 sbp_show_sdev_info(sdev, 2);
1229 printf("sbp_cmd_callback\n");
1231 if (xfer->resp != 0) {
1233 printf("%s: xfer->resp = %d\n", __FUNCTION__, xfer->resp);
1235 sbp_xfer_free(xfer);
1240 sbp_doorbell(struct sbp_dev *sdev)
1242 struct fw_xfer *xfer;
1245 sbp_show_sdev_info(sdev, 2);
1246 printf("sbp_doorbell\n");
1249 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1252 xfer->act.hand = sbp_cmd_callback;
1253 fp = (struct fw_pkt *)xfer->send.buf;
1254 fp->mode.wreqq.data = htonl(0xf);
1255 fw_asyreq(xfer->fc, -1, xfer);
1259 static struct fw_xfer *
1260 sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1262 struct fw_xfer *xfer;
1264 struct sbp_target *target;
1267 target = sdev->target;
1269 xfer = STAILQ_FIRST(&target->xferlist);
1271 if (target->n_xfer > 5 /* XXX */) {
1272 printf("sbp: no more xfer for this target\n");
1276 xfer = fw_xfer_alloc_buf(M_SBP, 8, 0);
1278 printf("sbp: fw_xfer_alloc_buf failed\n");
1284 printf("sbp: alloc %d xfer\n", target->n_xfer);
1287 STAILQ_REMOVE_HEAD(&target->xferlist, link);
1291 microtime(&xfer->tv);
1294 xfer->recv.pay_len = 0;
1295 xfer->send.spd = min(sdev->target->fwdev->speed, max_speed);
1296 xfer->fc = sdev->target->sbp->fd.fc;
1297 xfer->retry_req = fw_asybusy;
1300 if (tcode == FWTCODE_WREQB)
1301 xfer->send.pay_len = 8;
1303 xfer->send.pay_len = 0;
1305 xfer->sc = (caddr_t)sdev;
1306 fp = &xfer->send.hdr;
1307 fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
1308 fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
1309 fp->mode.wreqq.tlrt = 0;
1310 fp->mode.wreqq.tcode = tcode;
1311 fp->mode.wreqq.pri = 0;
1312 fp->mode.wreqq.dst = FWLOCALBUS | sdev->target->fwdev->dst;
1319 sbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1321 struct fw_xfer *xfer;
1323 struct sbp_ocb *ocb;
1324 struct sbp_target *target;
1327 target = sdev->target;
1328 nid = target->sbp->fd.fc->nodeid | FWLOCALBUS;
1331 if (func == ORB_FUN_RUNQUEUE) {
1332 ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1333 if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1337 STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1340 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
1345 ocb->flags = OCB_ACT_MGM;
1348 bzero((void *)ocb->orb, sizeof(ocb->orb));
1349 ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1350 ocb->orb[7] = htonl(SBP_DEV2ADDR(target->target_id, sdev->lun_id));
1353 sbp_show_sdev_info(sdev, 2);
1354 printf("%s\n", orb_fun_name[(func>>16)&0xf]);
1358 ocb->orb[0] = ocb->orb[1] = 0; /* password */
1359 ocb->orb[2] = htonl(nid << 16);
1360 ocb->orb[3] = htonl(sdev->dma.bus_addr);
1361 ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id);
1363 ocb->orb[4] |= htonl(ORB_EXV);
1364 ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
1365 fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD);
1368 ocb->orb[0] = htonl((0 << 16) | 0);
1369 ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
1376 ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
1380 if (target->mgm_ocb_cur != NULL) {
1381 /* there is a standing ORB */
1382 STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1387 target->mgm_ocb_cur = ocb;
1390 callout_reset(&target->mgm_ocb_timeout, 5*hz,
1391 sbp_mgm_timeout, (caddr_t)ocb);
1392 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1396 xfer->act.hand = sbp_mgm_callback;
1398 fp = &xfer->send.hdr;
1399 fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
1400 fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
1401 fp->mode.wreqb.len = 8;
1402 fp->mode.wreqb.extcode = 0;
1403 xfer->send.payload[0] = htonl(nid << 16);
1404 xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff);
1406 sbp_show_sdev_info(sdev, 2);
1407 printf("mgm orb: %08x\n", (u_int32_t)ocb->bus_addr);
1410 fw_asyreq(xfer->fc, -1, xfer);
1414 sbp_print_scsi_cmd(struct sbp_ocb *ocb)
1416 struct ccb_scsiio *csio;
1418 csio = &ocb->ccb->csio;
1419 printf("%s:%d:%d XPT_SCSI_IO: "
1420 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1422 "%db cmd/%db data/%db sense\n",
1423 device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1424 ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun,
1425 csio->cdb_io.cdb_bytes[0],
1426 csio->cdb_io.cdb_bytes[1],
1427 csio->cdb_io.cdb_bytes[2],
1428 csio->cdb_io.cdb_bytes[3],
1429 csio->cdb_io.cdb_bytes[4],
1430 csio->cdb_io.cdb_bytes[5],
1431 csio->cdb_io.cdb_bytes[6],
1432 csio->cdb_io.cdb_bytes[7],
1433 csio->cdb_io.cdb_bytes[8],
1434 csio->cdb_io.cdb_bytes[9],
1435 ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1436 csio->cdb_len, csio->dxfer_len,
1441 sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1443 struct sbp_cmd_status *sbp_cmd_status;
1444 struct scsi_sense_data *sense;
1446 sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1447 sense = &ocb->ccb->csio.sense_data;
1450 sbp_print_scsi_cmd(ocb);
1451 /* XXX need decode status */
1452 sbp_show_sdev_info(ocb->sdev, 2);
1453 printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1454 sbp_cmd_status->status,
1455 sbp_cmd_status->sfmt,
1456 sbp_cmd_status->valid,
1457 sbp_cmd_status->s_key,
1458 sbp_cmd_status->s_code,
1459 sbp_cmd_status->s_qlfr,
1464 switch (sbp_cmd_status->status) {
1465 case SCSI_STATUS_CHECK_COND:
1466 case SCSI_STATUS_BUSY:
1467 case SCSI_STATUS_CMD_TERMINATED:
1468 if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1469 sense->error_code = SSD_CURRENT_ERROR;
1471 sense->error_code = SSD_DEFERRED_ERROR;
1473 if(sbp_cmd_status->valid)
1474 sense->error_code |= SSD_ERRCODE_VALID;
1475 sense->flags = sbp_cmd_status->s_key;
1476 if(sbp_cmd_status->mark)
1477 sense->flags |= SSD_FILEMARK;
1478 if(sbp_cmd_status->eom)
1479 sense->flags |= SSD_EOM;
1480 if(sbp_cmd_status->ill_len)
1481 sense->flags |= SSD_ILI;
1483 bcopy(&sbp_cmd_status->info, &sense->info[0], 4);
1485 if (sbp_status->len <= 1)
1486 /* XXX not scsi status. shouldn't be happened */
1487 sense->extra_len = 0;
1488 else if (sbp_status->len <= 4)
1489 /* add_sense_code(_qual), info, cmd_spec_info */
1490 sense->extra_len = 6;
1492 /* fru, sense_key_spec */
1493 sense->extra_len = 10;
1495 bcopy(&sbp_cmd_status->cdb, &sense->cmd_spec_info[0], 4);
1497 sense->add_sense_code = sbp_cmd_status->s_code;
1498 sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1499 sense->fru = sbp_cmd_status->fru;
1501 bcopy(&sbp_cmd_status->s_keydep[0],
1502 &sense->sense_key_spec[0], 3);
1504 ocb->ccb->csio.scsi_status = sbp_cmd_status->status;;
1505 ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1506 | CAM_AUTOSNS_VALID;
1511 for( j = 0 ; j < 32 ; j+=8){
1512 printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1513 tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1514 tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1521 sbp_show_sdev_info(ocb->sdev, 2);
1522 printf("sbp_scsi_status: unknown scsi status 0x%x\n",
1523 sbp_cmd_status->status);
1528 sbp_fix_inq_data(struct sbp_ocb *ocb)
1531 struct sbp_dev *sdev;
1532 struct scsi_inquiry_data *inq;
1537 if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1540 sbp_show_sdev_info(sdev, 2);
1541 printf("sbp_fix_inq_data\n");
1543 inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1544 switch (SID_TYPE(inq)) {
1548 * XXX Convert Direct Access device to RBC.
1549 * I've never seen FireWire DA devices which support READ_6.
1551 if (SID_TYPE(inq) == T_DIRECT)
1552 inq->device |= T_RBC; /* T_DIRECT == 0 */
1556 /* enable tagged queuing */
1558 inq->flags |= SID_CmdQue;
1560 inq->flags &= ~SID_CmdQue;
1562 * Override vendor/product/revision information.
1563 * Some devices sometimes return strange strings.
1566 bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1567 bcopy(sdev->product, inq->product, sizeof(inq->product));
1568 bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1575 sbp_recv1(struct fw_xfer *xfer)
1581 struct sbp_softc *sbp;
1582 struct sbp_dev *sdev;
1583 struct sbp_ocb *ocb;
1584 struct sbp_login_res *login_res = NULL;
1585 struct sbp_status *sbp_status;
1586 struct sbp_target *target;
1587 int orb_fun, status_valid0, status_valid, t, l, reset_agent = 0;
1591 ld = xfer->recv.buf;
1592 printf("sbp %x %d %d %08x %08x %08x %08x\n",
1593 xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1594 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1595 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1597 sbp = (struct sbp_softc *)xfer->sc;
1598 if (xfer->resp != 0){
1599 printf("sbp_recv: xfer->resp = %d\n", xfer->resp);
1602 if (xfer->recv.payload == NULL){
1603 printf("sbp_recv: xfer->recv.payload == NULL\n");
1606 rfp = &xfer->recv.hdr;
1607 if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1608 printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1611 sbp_status = (struct sbp_status *)xfer->recv.payload;
1612 addr = rfp->mode.wreqb.dest_lo;
1614 printf("received address 0x%x\n", addr);
1616 t = SBP_ADDR2TRG(addr);
1617 if (t >= SBP_NUM_TARGETS) {
1618 device_printf(sbp->fd.dev,
1619 "sbp_recv1: invalid target %d\n", t);
1622 target = &sbp->targets[t];
1623 l = SBP_ADDR2LUN(addr);
1624 if (l >= target->num_lun || target->luns[l] == NULL) {
1625 device_printf(sbp->fd.dev,
1626 "sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1629 sdev = target->luns[l];
1632 switch (sbp_status->src) {
1635 /* check mgm_ocb_cur first */
1636 ocb = target->mgm_ocb_cur;
1638 if (OCB_MATCH(ocb, sbp_status)) {
1639 callout_stop(&target->mgm_ocb_timeout);
1640 target->mgm_ocb_cur = NULL;
1644 ocb = sbp_dequeue_ocb(sdev, sbp_status);
1646 sbp_show_sdev_info(sdev, 2);
1647 #if defined(__DragonFly__) || __FreeBSD_version < 500000
1648 printf("No ocb(%lx) on the queue\n",
1650 printf("No ocb(%x) on the queue\n",
1652 ntohl(sbp_status->orb_lo));
1657 sbp_show_sdev_info(sdev, 2);
1658 printf("unsolicit status received\n");
1661 sbp_show_sdev_info(sdev, 2);
1662 printf("unknown sbp_status->src\n");
1665 status_valid0 = (sbp_status->src < 2
1666 && sbp_status->resp == ORB_RES_CMPL
1667 && sbp_status->dead == 0);
1668 status_valid = (status_valid0 && sbp_status->status == 0);
1670 if (!status_valid0 || debug > 2){
1673 sbp_show_sdev_info(sdev, 2);
1674 printf("ORB status src:%x resp:%x dead:%x"
1675 #if defined(__DragonFly__) || __FreeBSD_version < 500000
1676 " len:%x stat:%x orb:%x%08lx\n",
1678 " len:%x stat:%x orb:%x%08x\n",
1680 sbp_status->src, sbp_status->resp, sbp_status->dead,
1681 sbp_status->len, sbp_status->status,
1682 ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1684 sbp_show_sdev_info(sdev, 2);
1685 status = sbp_status->status;
1686 switch(sbp_status->resp) {
1688 if (status > MAX_ORB_STATUS0)
1689 printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1691 printf("%s\n", orb_status0[status]);
1694 printf("Obj: %s, Error: %s\n",
1695 orb_status1_object[(status>>6) & 3],
1696 orb_status1_serial_bus_error[status & 0xf]);
1699 printf("Illegal request\n");
1702 printf("Vendor dependent\n");
1705 printf("unknown respose code %d\n", sbp_status->resp);
1709 /* we have to reset the fetch agent if it's dead */
1710 if (sbp_status->dead) {
1712 xpt_freeze_devq(sdev->path, 1);
1721 switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1727 switch(ocb->flags) {
1729 orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1733 fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD);
1734 login_res = sdev->login;
1735 login_res->len = ntohs(login_res->len);
1736 login_res->id = ntohs(login_res->id);
1737 login_res->cmd_hi = ntohs(login_res->cmd_hi);
1738 login_res->cmd_lo = ntohl(login_res->cmd_lo);
1741 sbp_show_sdev_info(sdev, 2);
1742 printf("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));
1744 sbp_busy_timeout(sdev);
1746 /* forgot logout? */
1747 sbp_show_sdev_info(sdev, 2);
1748 printf("login failed\n");
1749 sdev->status = SBP_DEV_RESET;
1753 login_res = sdev->login;
1756 sbp_show_sdev_info(sdev, 2);
1757 printf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo);
1760 if (sdev->status == SBP_DEV_ATTACHED)
1763 sbp_agent_reset(sdev);
1765 sdev->status = SBP_DEV_ATTACHED;
1766 sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL);
1769 /* reconnection hold time exceed? */
1771 sbp_show_sdev_info(sdev, 2);
1772 printf("reconnect failed\n");
1778 sdev->status = SBP_DEV_RESET;
1781 sbp_busy_timeout(sdev);
1786 sbp_agent_reset(sdev);
1789 sbp_show_sdev_info(sdev, 2);
1790 printf("unknown function %d\n", orb_fun);
1793 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
1797 if(ocb->ccb != NULL){
1801 ld = ocb->ccb->csio.data_ptr;
1802 if(ld != NULL && ocb->ccb->csio.dxfer_len != 0)
1803 printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]);
1805 printf("ptr NULL\n");
1806 printf("len %d\n", sbp_status->len);
1809 if(sbp_status->len > 1){
1810 sbp_scsi_status(sbp_status, ocb);
1812 if(sbp_status->resp != ORB_RES_CMPL){
1813 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1815 ccb->ccb_h.status = CAM_REQ_CMP;
1818 /* fix up inq data */
1819 if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1820 sbp_fix_inq_data(ocb);
1829 sbp_free_ocb(sdev, ocb);
1832 sbp_agent_reset(sdev);
1835 xfer->recv.pay_len = SBP_RECV_LEN;
1836 /* The received packet is usually small enough to be stored within
1837 * the buffer. In that case, the controller return ack_complete and
1838 * no respose is necessary.
1840 * XXX fwohci.c and firewire.c should inform event_code such as
1841 * ack_complete or ack_pending to upper driver.
1845 sfp = (struct fw_pkt *)xfer->send.buf;
1846 sfp->mode.wres.dst = rfp->mode.wreqb.src;
1847 xfer->dst = sfp->mode.wres.dst;
1848 xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1849 xfer->act.hand = sbp_loginres_callback;
1850 xfer->retry_req = fw_asybusy;
1852 sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1853 sfp->mode.wres.tcode = FWTCODE_WRES;
1854 sfp->mode.wres.rtcode = 0;
1855 sfp->mode.wres.pri = 0;
1857 fw_asyreq(xfer->fc, -1, xfer);
1860 STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1868 sbp_recv(struct fw_xfer *xfer)
1880 sbp_attach(device_t dev)
1882 struct sbp_softc *sbp;
1883 struct cam_devq *devq;
1884 struct fw_xfer *xfer;
1888 printf("sbp_attach (cold=%d)\n", cold);
1893 sbp = ((struct sbp_softc *)device_get_softc(dev));
1894 bzero(sbp, sizeof(struct sbp_softc));
1896 sbp->fd.fc = device_get_ivars(dev);
1899 max_speed = sbp->fd.fc->speed;
1901 error = bus_dma_tag_create(/*parent*/sbp->fd.fc->dmat,
1902 /* XXX shoud be 4 for sane backend? */
1905 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1906 /*highaddr*/BUS_SPACE_MAXADDR,
1907 /*filter*/NULL, /*filterarg*/NULL,
1908 /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1909 /*maxsegsz*/SBP_SEG_MAX,
1910 /*flags*/BUS_DMA_ALLOCNOW,
1911 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
1912 /*lockfunc*/busdma_lock_mutex,
1917 printf("sbp_attach: Could not allocate DMA tag "
1918 "- error %d\n", error);
1922 devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1926 for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1927 sbp->targets[i].fwdev = NULL;
1928 sbp->targets[i].luns = NULL;
1931 sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1932 device_get_unit(dev),
1934 /*tagged*/ SBP_QUEUE_LEN - 1,
1937 if (sbp->sim == NULL) {
1938 cam_simq_free(devq);
1943 if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS)
1946 if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim),
1947 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1948 xpt_bus_deregister(cam_sim_path(sbp->sim));
1952 /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1953 sbp->fwb.start = ((u_int64_t)SBP_BIND_HI << 32) | SBP_DEV2ADDR(0, 0);
1954 sbp->fwb.end = sbp->fwb.start + 0xffff;
1955 sbp->fwb.act_type = FWACT_XFER;
1956 /* pre-allocate xfer */
1957 STAILQ_INIT(&sbp->fwb.xferlist);
1958 for (i = 0; i < SBP_NUM_OCB/2; i ++) {
1959 xfer = fw_xfer_alloc_buf(M_SBP,
1961 /* recv */SBP_RECV_LEN);
1962 xfer->act.hand = sbp_recv;
1964 xfer->fc = sbp->fd.fc;
1966 xfer->sc = (caddr_t)sbp;
1967 STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1969 fw_bindadd(sbp->fd.fc, &sbp->fwb);
1971 sbp->fd.post_busreset = sbp_post_busreset;
1972 sbp->fd.post_explore = sbp_post_explore;
1974 if (sbp->fd.fc->status != -1) {
1976 sbp_post_busreset((void *)sbp);
1977 sbp_post_explore((void *)sbp);
1980 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
1984 cam_sim_free(sbp->sim, /*free_devq*/TRUE);
1989 sbp_logout_all(struct sbp_softc *sbp)
1991 struct sbp_target *target;
1992 struct sbp_dev *sdev;
1996 printf("sbp_logout_all\n");
1998 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
1999 target = &sbp->targets[i];
2000 if (target->luns == NULL)
2002 for (j = 0; j < target->num_lun; j++) {
2003 sdev = target->luns[j];
2006 callout_stop(&sdev->login_callout);
2007 if (sdev->status >= SBP_DEV_TOATTACH &&
2008 sdev->status <= SBP_DEV_ATTACHED)
2009 sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
2017 sbp_shutdown(device_t dev)
2019 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2021 sbp_logout_all(sbp);
2026 sbp_free_sdev(struct sbp_dev *sdev)
2032 for (i = 0; i < SBP_QUEUE_LEN; i++)
2033 bus_dmamap_destroy(sdev->target->sbp->dmat,
2034 sdev->ocb[i].dmamap);
2035 fwdma_free(sdev->target->sbp->fd.fc, &sdev->dma);
2040 sbp_free_target(struct sbp_target *target)
2042 struct sbp_softc *sbp;
2043 struct fw_xfer *xfer, *next;
2046 if (target->luns == NULL)
2048 callout_stop(&target->mgm_ocb_timeout);
2050 for (i = 0; i < target->num_lun; i++)
2051 sbp_free_sdev(target->luns[i]);
2053 for (xfer = STAILQ_FIRST(&target->xferlist);
2054 xfer != NULL; xfer = next) {
2055 next = STAILQ_NEXT(xfer, link);
2056 fw_xfer_free_buf(xfer);
2058 STAILQ_INIT(&target->xferlist);
2059 free(target->luns, M_SBP);
2060 target->num_lun = 0;;
2061 target->luns = NULL;
2062 target->fwdev = NULL;
2066 sbp_detach(device_t dev)
2068 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2069 struct firewire_comm *fc = sbp->fd.fc;
2070 struct fw_xfer *xfer, *next;
2074 printf("sbp_detach\n");
2077 for (i = 0; i < SBP_NUM_TARGETS; i ++)
2078 sbp_cam_detach_target(&sbp->targets[i]);
2079 xpt_async(AC_LOST_DEVICE, sbp->path, NULL);
2080 xpt_free_path(sbp->path);
2081 xpt_bus_deregister(cam_sim_path(sbp->sim));
2082 cam_sim_free(sbp->sim, /*free_devq*/ TRUE),
2084 sbp_logout_all(sbp);
2086 /* XXX wait for logout completion */
2087 tsleep(&i, FWPRI, "sbpdtc", hz/2);
2089 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++)
2090 sbp_free_target(&sbp->targets[i]);
2092 for (xfer = STAILQ_FIRST(&sbp->fwb.xferlist);
2093 xfer != NULL; xfer = next) {
2094 next = STAILQ_NEXT(xfer, link);
2095 fw_xfer_free_buf(xfer);
2097 STAILQ_INIT(&sbp->fwb.xferlist);
2098 fw_bindremove(fc, &sbp->fwb);
2100 bus_dma_tag_destroy(sbp->dmat);
2106 sbp_cam_detach_sdev(struct sbp_dev *sdev)
2110 if (sdev->status == SBP_DEV_DEAD)
2112 if (sdev->status == SBP_DEV_RESET)
2115 xpt_release_devq(sdev->path,
2116 sdev->freeze, TRUE);
2118 xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
2119 xpt_free_path(sdev->path);
2122 sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
2126 sbp_cam_detach_target(struct sbp_target *target)
2130 if (target->luns != NULL) {
2132 printf("sbp_detach_target %d\n", target->target_id);
2134 callout_stop(&target->scan_callout);
2135 for (i = 0; i < target->num_lun; i++)
2136 sbp_cam_detach_sdev(target->luns[i]);
2141 sbp_target_reset(struct sbp_dev *sdev, int method)
2144 struct sbp_target *target = sdev->target;
2145 struct sbp_dev *tsdev;
2147 for (i = 0; i < target->num_lun; i++) {
2148 tsdev = target->luns[i];
2151 if (tsdev->status == SBP_DEV_DEAD)
2153 if (tsdev->status == SBP_DEV_RESET)
2155 xpt_freeze_devq(tsdev->path, 1);
2157 sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT);
2159 tsdev->status = SBP_DEV_LOGIN;
2163 printf("target reset\n");
2164 sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2167 printf("reset start\n");
2168 sbp_reset_start(sdev);
2175 sbp_mgm_timeout(void *arg)
2177 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2178 struct sbp_dev *sdev = ocb->sdev;
2179 struct sbp_target *target = sdev->target;
2181 sbp_show_sdev_info(sdev, 2);
2182 printf("request timeout(mgm orb:0x%08x) ... ",
2183 (u_int32_t)ocb->bus_addr);
2184 target->mgm_ocb_cur = NULL;
2185 sbp_free_ocb(sdev, ocb);
2188 printf("run next request\n");
2189 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2192 printf("reset start\n");
2193 sbp_reset_start(sdev);
2198 sbp_timeout(void *arg)
2200 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2201 struct sbp_dev *sdev = ocb->sdev;
2203 sbp_show_sdev_info(sdev, 2);
2204 printf("request timeout(cmd orb:0x%08x) ... ",
2205 (u_int32_t)ocb->bus_addr);
2208 switch(sdev->timeout) {
2210 printf("agent reset\n");
2211 xpt_freeze_devq(sdev->path, 1);
2213 sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2214 sbp_agent_reset(sdev);
2218 sbp_target_reset(sdev, sdev->timeout - 1);
2223 sbp_cam_detach_target(target);
2224 if (target->luns != NULL)
2225 free(target->luns, M_SBP);
2226 target->num_lun = 0;;
2227 target->luns = NULL;
2228 target->fwdev = NULL;
2234 sbp_action1(struct cam_sim *sim, union ccb *ccb)
2237 struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2238 struct sbp_target *target = NULL;
2239 struct sbp_dev *sdev = NULL;
2241 /* target:lun -> sdev mapping */
2243 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2244 && ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2245 target = &sbp->targets[ccb->ccb_h.target_id];
2246 if (target->fwdev != NULL
2247 && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2248 && ccb->ccb_h.target_lun < target->num_lun) {
2249 sdev = target->luns[ccb->ccb_h.target_lun];
2250 if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED &&
2251 sdev->status != SBP_DEV_PROBE)
2258 printf("invalid target %d lun %d\n",
2259 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2262 switch (ccb->ccb_h.func_code) {
2265 case XPT_GET_TRAN_SETTINGS:
2266 case XPT_SET_TRAN_SETTINGS:
2267 case XPT_CALC_GEOMETRY:
2270 printf("%s:%d:%d:func_code 0x%04x: "
2271 "Invalid target (target needed)\n",
2272 device_get_nameunit(sbp->fd.dev),
2273 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2274 ccb->ccb_h.func_code);
2277 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2284 /* The opcodes sometimes aimed at a target (sc is valid),
2285 * sometimes aimed at the SIM (sc is invalid and target is
2286 * CAM_TARGET_WILDCARD)
2289 ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2291 printf("%s:%d:%d func_code 0x%04x: "
2292 "Invalid target (no wildcard)\n",
2293 device_get_nameunit(sbp->fd.dev),
2294 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2295 ccb->ccb_h.func_code);
2297 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2303 /* XXX Hm, we should check the input parameters */
2307 switch (ccb->ccb_h.func_code) {
2310 struct ccb_scsiio *csio;
2311 struct sbp_ocb *ocb;
2318 printf("%s:%d:%d XPT_SCSI_IO: "
2319 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2321 "%db cmd/%db data/%db sense\n",
2322 device_get_nameunit(sbp->fd.dev),
2323 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2324 csio->cdb_io.cdb_bytes[0],
2325 csio->cdb_io.cdb_bytes[1],
2326 csio->cdb_io.cdb_bytes[2],
2327 csio->cdb_io.cdb_bytes[3],
2328 csio->cdb_io.cdb_bytes[4],
2329 csio->cdb_io.cdb_bytes[5],
2330 csio->cdb_io.cdb_bytes[6],
2331 csio->cdb_io.cdb_bytes[7],
2332 csio->cdb_io.cdb_bytes[8],
2333 csio->cdb_io.cdb_bytes[9],
2334 ccb->ccb_h.flags & CAM_DIR_MASK,
2335 csio->cdb_len, csio->dxfer_len,
2339 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2344 /* if we are in probe stage, pass only probe commands */
2345 if (sdev->status == SBP_DEV_PROBE) {
2347 name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2348 printf("probe stage, periph name: %s\n", name);
2349 if (strcmp(name, "probe") != 0) {
2350 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2356 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
2357 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2362 ocb->flags = OCB_ACT_CMD;
2365 ccb->ccb_h.ccb_sdev_ptr = sdev;
2366 ocb->orb[0] = htonl(1 << 31);
2368 ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2369 ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2370 speed = min(target->fwdev->speed, max_speed);
2371 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2372 | ORB_CMD_MAXP(speed + 7));
2373 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
2374 ocb->orb[4] |= htonl(ORB_CMD_IN);
2377 if (csio->ccb_h.flags & CAM_SCATTER_VALID)
2378 printf("sbp: CAM_SCATTER_VALID\n");
2379 if (csio->ccb_h.flags & CAM_DATA_PHYS)
2380 printf("sbp: CAM_DATA_PHYS\n");
2382 if (csio->ccb_h.flags & CAM_CDB_POINTER)
2383 cdb = (void *)csio->cdb_io.cdb_ptr;
2385 cdb = (void *)&csio->cdb_io.cdb_bytes;
2386 bcopy(cdb, (void *)&ocb->orb[5], csio->cdb_len);
2388 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2389 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2391 if (ccb->csio.dxfer_len > 0) {
2395 error = bus_dmamap_load(/*dma tag*/sbp->dmat,
2396 /*dma map*/ocb->dmamap,
2398 ccb->csio.dxfer_len,
2404 printf("sbp: bus_dmamap_load error %d\n", error);
2406 sbp_execute_ocb(ocb, NULL, 0, 0);
2409 case XPT_CALC_GEOMETRY:
2411 struct ccb_calc_geometry *ccg;
2412 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2414 u_int32_t secs_per_cylinder;
2419 if (ccg->block_size == 0) {
2420 printf("sbp_action1: block_size is 0.\n");
2421 ccb->ccb_h.status = CAM_REQ_INVALID;
2426 printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2427 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2428 "Volume size = %d\n",
2430 "Volume size = %jd\n",
2432 device_get_nameunit(sbp->fd.dev),
2433 cam_sim_path(sbp->sim),
2434 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2435 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2441 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2442 size_mb = ccg->volume_size
2443 / ((1024L * 1024L) / ccg->block_size);
2445 if (size_mb > 1024 && extended) {
2447 ccg->secs_per_track = 63;
2450 ccg->secs_per_track = 32;
2452 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2453 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2454 ccb->ccb_h.status = CAM_REQ_CMP;
2456 cam_calc_geometry(ccg, /*extended*/1);
2461 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
2465 printf("%s:%d:XPT_RESET_BUS: \n",
2466 device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2469 ccb->ccb_h.status = CAM_REQ_INVALID;
2473 case XPT_PATH_INQ: /* Path routing inquiry */
2475 struct ccb_pathinq *cpi = &ccb->cpi;
2478 printf("%s:%d:%d XPT_PATH_INQ:.\n",
2479 device_get_nameunit(sbp->fd.dev),
2480 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2482 cpi->version_num = 1; /* XXX??? */
2483 cpi->hba_inquiry = PI_TAG_ABLE;
2484 cpi->target_sprt = 0;
2485 cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE;
2486 cpi->hba_eng_cnt = 0;
2487 cpi->max_target = SBP_NUM_TARGETS - 1;
2488 cpi->max_lun = SBP_NUM_LUNS - 1;
2489 cpi->initiator_id = SBP_INITIATOR;
2490 cpi->bus_id = sim->bus_id;
2491 cpi->base_transfer_speed = 400 * 1000 / 8;
2492 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2493 strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2494 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2495 cpi->unit_number = sim->unit_number;
2497 cpi->ccb_h.status = CAM_REQ_CMP;
2501 case XPT_GET_TRAN_SETTINGS:
2503 struct ccb_trans_settings *cts = &ccb->cts;
2505 printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2506 device_get_nameunit(sbp->fd.dev),
2507 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2509 /* Enable disconnect and tagged queuing */
2510 cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2511 cts->flags = CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB;
2513 cts->ccb_h.status = CAM_REQ_CMP;
2518 ccb->ccb_h.status = CAM_UA_ABORT;
2521 case XPT_SET_TRAN_SETTINGS:
2524 ccb->ccb_h.status = CAM_REQ_INVALID;
2532 sbp_action(struct cam_sim *sim, union ccb *ccb)
2537 sbp_action1(sim, ccb);
2542 sbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error)
2545 struct sbp_ocb *ocb;
2546 struct sbp_ocb *prev;
2547 bus_dma_segment_t *s;
2550 printf("sbp_execute_ocb: error=%d\n", error);
2552 ocb = (struct sbp_ocb *)arg;
2555 printf("sbp_execute_ocb: seg %d", seg);
2556 for (i = 0; i < seg; i++)
2557 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2558 printf(", %x:%d", segments[i].ds_addr, segments[i].ds_len);
2560 printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2561 (uintmax_t)segments[i].ds_len);
2567 /* direct pointer */
2569 if (s->ds_len > SBP_SEG_MAX)
2570 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2571 ocb->orb[3] = htonl(s->ds_addr);
2572 ocb->orb[4] |= htonl(s->ds_len);
2573 } else if(seg > 1) {
2575 for (i = 0; i < seg; i++) {
2578 /* XXX LSI Logic "< 16 byte" bug might be hit */
2580 printf("sbp_execute_ocb: warning, "
2581 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2582 "segment length(%d) is less than 16."
2584 "segment length(%zd) is less than 16."
2586 "(seg=%d/%d)\n", s->ds_len, i+1, seg);
2588 if (s->ds_len > SBP_SEG_MAX)
2589 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2590 ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2591 ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2593 ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2597 bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2598 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2599 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2600 prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2601 fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2602 if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) {
2603 ocb->sdev->flags &= ~ORB_LINK_DEAD;
2604 sbp_orb_pointer(ocb->sdev, ocb);
2609 sbp_poll(struct cam_sim *sim)
2611 struct sbp_softc *sbp;
2612 struct firewire_comm *fc;
2614 sbp = (struct sbp_softc *)sim->softc;
2617 fc->poll(fc, 0, -1);
2622 static struct sbp_ocb *
2623 sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2625 struct sbp_ocb *ocb;
2626 struct sbp_ocb *next;
2627 int s = splfw(), order = 0;
2631 sbp_show_sdev_info(sdev, 2);
2632 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2633 printf("%s: 0x%08lx src %d\n",
2635 printf("%s: 0x%08x src %d\n",
2637 __FUNCTION__, ntohl(sbp_status->orb_lo), sbp_status->src);
2639 for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2640 next = STAILQ_NEXT(ocb, ocb);
2642 if (OCB_MATCH(ocb, sbp_status)) {
2644 STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2645 if (ocb->ccb != NULL)
2646 untimeout(sbp_timeout, (caddr_t)ocb,
2647 ocb->ccb->ccb_h.timeout_ch);
2648 if (ntohl(ocb->orb[4]) & 0xffff) {
2649 bus_dmamap_sync(sdev->target->sbp->dmat,
2651 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2652 BUS_DMASYNC_POSTREAD :
2653 BUS_DMASYNC_POSTWRITE);
2654 bus_dmamap_unload(sdev->target->sbp->dmat,
2657 if (sbp_status->src == SRC_NO_NEXT) {
2659 sbp_orb_pointer(sdev, next);
2660 else if (order > 0) {
2662 * Unordered execution
2663 * We need to send pointer for
2666 sdev->flags |= ORB_LINK_DEAD;
2675 if (ocb && order > 0) {
2676 sbp_show_sdev_info(sdev, 2);
2677 printf("unordered execution order:%d\n", order);
2683 static struct sbp_ocb *
2684 sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2687 struct sbp_ocb *prev;
2690 sbp_show_sdev_info(sdev, 2);
2691 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2692 printf("%s: 0x%08x\n", __FUNCTION__, ocb->bus_addr);
2694 printf("%s: 0x%08jx\n", __FUNCTION__, (uintmax_t)ocb->bus_addr);
2697 prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2698 STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2700 if (ocb->ccb != NULL)
2701 ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb,
2702 (ocb->ccb->ccb_h.timeout * hz) / 1000);
2706 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2707 printf("linking chain 0x%x -> 0x%x\n",
2708 prev->bus_addr, ocb->bus_addr);
2710 printf("linking chain 0x%jx -> 0x%jx\n",
2711 (uintmax_t)prev->bus_addr, (uintmax_t)ocb->bus_addr);
2714 prev->orb[1] = htonl(ocb->bus_addr);
2722 static struct sbp_ocb *
2723 sbp_get_ocb(struct sbp_dev *sdev)
2725 struct sbp_ocb *ocb;
2727 ocb = STAILQ_FIRST(&sdev->free_ocbs);
2729 printf("ocb shortage!!!\n");
2732 STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2739 sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2743 STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2747 sbp_abort_ocb(struct sbp_ocb *ocb, int status)
2749 struct sbp_dev *sdev;
2753 sbp_show_sdev_info(sdev, 2);
2754 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2755 printf("sbp_abort_ocb 0x%x\n", ocb->bus_addr);
2757 printf("sbp_abort_ocb 0x%jx\n", (uintmax_t)ocb->bus_addr);
2761 if (ocb->ccb != NULL)
2762 sbp_print_scsi_cmd(ocb);
2764 if (ntohl(ocb->orb[4]) & 0xffff) {
2765 bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
2766 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2767 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2768 bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
2770 if (ocb->ccb != NULL) {
2771 untimeout(sbp_timeout, (caddr_t)ocb,
2772 ocb->ccb->ccb_h.timeout_ch);
2773 ocb->ccb->ccb_h.status = status;
2776 sbp_free_ocb(sdev, ocb);
2780 sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2783 struct sbp_ocb *ocb, *next;
2784 STAILQ_HEAD(, sbp_ocb) temp;
2788 bcopy(&sdev->ocbs, &temp, sizeof(temp));
2789 STAILQ_INIT(&sdev->ocbs);
2790 for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2791 next = STAILQ_NEXT(ocb, ocb);
2792 sbp_abort_ocb(ocb, status);
2798 static devclass_t sbp_devclass;
2800 static device_method_t sbp_methods[] = {
2801 /* device interface */
2802 DEVMETHOD(device_identify, sbp_identify),
2803 DEVMETHOD(device_probe, sbp_probe),
2804 DEVMETHOD(device_attach, sbp_attach),
2805 DEVMETHOD(device_detach, sbp_detach),
2806 DEVMETHOD(device_shutdown, sbp_shutdown),
2811 static driver_t sbp_driver = {
2814 sizeof(struct sbp_softc),
2817 DECLARE_DUMMY_MODULE(sbp);
2818 DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2819 MODULE_VERSION(sbp, 1);
2820 MODULE_DEPEND(sbp, firewire, 1, 1, 1);
2821 MODULE_DEPEND(sbp, cam, 1, 1, 1);