2 * Copyright (c) 2003 Hidetosh Shimokawa
3 * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetosh 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.5.2.19 2003/05/12 04:16:30 simokawa Exp $
35 * $DragonFly: src/sys/dev/disk/sbp/sbp.c,v 1.6 2004/01/13 17:32:12 joerg Exp $
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/module.h>
44 #include <sys/sysctl.h>
45 #include <machine/bus.h>
46 #include <sys/malloc.h>
48 #if __FreeBSD_version < 500106
49 #include <sys/devicestat.h> /* for struct devstat */
52 #include <bus/cam/cam.h>
53 #include <bus/cam/cam_ccb.h>
54 #include <bus/cam/cam_sim.h>
55 #include <bus/cam/cam_xpt_sim.h>
56 #include <bus/cam/cam_debug.h>
57 #include <bus/cam/cam_periph.h>
59 #include <bus/cam/scsi/scsi_all.h>
60 #include <bus/cam/scsi/scsi_message.h>
61 #include <bus/cam/scsi/scsi_da.h>
63 #include <sys/kernel.h>
65 #include <bus/firewire/firewire.h>
66 #include <bus/firewire/firewirereg.h>
67 #include <bus/firewire/fwdma.h>
68 #include <bus/firewire/iec13213.h>
70 #define ccb_sdev_ptr spriv_ptr0
71 #define ccb_sbp_ptr spriv_ptr1
73 #define SBP_NUM_TARGETS 8 /* MAX 64 */
74 #define SBP_NUM_LUNS 8 /* limited by CAM_SCSI2_MAXLUN in cam_xpt.c */
75 #define SBP_DMA_SIZE PAGE_SIZE
76 #define SBP_LOGIN_SIZE sizeof(struct sbp_login_res)
77 #define SBP_QUEUE_LEN ((SBP_DMA_SIZE - SBP_LOGIN_SIZE) / sizeof(struct sbp_ocb))
78 #define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS)
80 #define SBP_INITIATOR 7
85 * STATUS FIFO addressing
87 * -----------------------
88 * 0- 1( 2): 0 (alingment)
93 * 32-47(16): SBP_BIND_HI
94 * 48-64(16): bus_id, node_id
96 #define SBP_BIND_HI 0x1
97 #define SBP_DEV2ADDR(u, t, l) \
98 ((((u) & 0xff) << 16) | (((l) & 0xff) << 8) | (((t) & 0x3f) << 2))
99 #define SBP_ADDR2TRG(a) (((a) >> 2) & 0x3f)
100 #define SBP_ADDR2LUN(a) (((a) >> 8) & 0xff)
102 #define ORB_NOTIFY (1 << 31)
103 #define ORB_FMT_STD (0 << 29)
104 #define ORB_FMT_VED (2 << 29)
105 #define ORB_FMT_NOP (3 << 29)
106 #define ORB_FMT_MSK (3 << 29)
107 #define ORB_EXV (1 << 28)
109 #define ORB_CMD_IN (1 << 27)
111 #define ORB_CMD_SPD(x) ((x) << 24)
112 #define ORB_CMD_MAXP(x) ((x) << 20)
113 #define ORB_RCN_TMO(x) ((x) << 20)
114 #define ORB_CMD_PTBL (1 << 19)
115 #define ORB_CMD_PSZ(x) ((x) << 16)
117 #define ORB_FUN_LGI (0 << 16)
118 #define ORB_FUN_QLG (1 << 16)
119 #define ORB_FUN_RCN (3 << 16)
120 #define ORB_FUN_LGO (7 << 16)
121 #define ORB_FUN_ATA (0xb << 16)
122 #define ORB_FUN_ATS (0xc << 16)
123 #define ORB_FUN_LUR (0xe << 16)
124 #define ORB_FUN_RST (0xf << 16)
125 #define ORB_FUN_MSK (0xf << 16)
126 #define ORB_FUN_RUNQUEUE 0xffff
128 static char *orb_fun_name[] = {
130 /* 1 */ "QUERY LOGINS",
133 /* 4 */ "SET PASSWORD",
140 /* B */ "ABORT TASK",
141 /* C */ "ABORT TASK SET",
143 /* E */ "LOGICAL UNIT RESET",
144 /* F */ "TARGET RESET"
147 #define ORB_RES_CMPL 0
148 #define ORB_RES_FAIL 1
149 #define ORB_RES_ILLE 2
150 #define ORB_RES_VEND 3
152 static int debug = 0;
153 static int auto_login = 1;
154 static int max_speed = 2;
155 static int sbp_cold = 1;
156 static int sbp_tags = 0;
158 SYSCTL_DECL(_hw_firewire);
159 SYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem");
160 SYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0,
162 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0,
163 "SBP perform login automatically");
164 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0,
165 "SBP transfer max speed");
166 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, tags, CTLFLAG_RW, &sbp_tags, 0,
167 "SBP tagged queuing support");
169 #define SBP_DEBUG(x) if (debug > x) {
172 #define NEED_RESPONSE 0
177 #define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE)
178 #ifdef __sparc64__ /* iommu */
179 #define SBP_IND_MAX howmany(MAXPHYS, SBP_SEG_MAX)
181 #define SBP_IND_MAX howmany(MAXPHYS, PAGE_SIZE)
184 STAILQ_ENTRY(sbp_ocb) ocb;
187 volatile u_int32_t orb[8];
188 #define IND_PTR_OFFSET (8*sizeof(u_int32_t))
189 volatile struct ind_ptr ind_ptr[SBP_IND_MAX];
190 struct sbp_dev *sdev;
191 int flags; /* XXX should be removed */
195 #define OCB_ACT_MGM 0
196 #define OCB_ACT_CMD 1
197 #define OCB_MATCH(o,s) ((o)->bus_addr == ntohl((s)->orb_lo))
199 #define SBP_RECV_LEN (16 + 32) /* header + payload */
201 struct sbp_login_res{
208 u_int16_t recon_hold;
211 #if BYTE_ORDER == BIG_ENDIAN
227 struct sbp_cmd_status{
228 #define SBP_SFMT_CURR 0
229 #define SBP_SFMT_DEFER 1
230 #if BYTE_ORDER == BIG_ENDIAN
252 #if BYTE_ORDER == BIG_ENDIAN
253 u_int32_t s_keydep:24,
264 #define SBP_DEV_RESET 0 /* accept login */
265 #define SBP_DEV_LOGIN 1 /* to login */
267 #define SBP_DEV_RECONN 2 /* to reconnect */
269 #define SBP_DEV_TOATTACH 3 /* to attach */
270 #define SBP_DEV_PROBE 4 /* scan lun */
271 #define SBP_DEV_ATTACHED 5 /* in operation */
272 #define SBP_DEV_DEAD 6 /* unavailable unit */
273 #define SBP_DEV_RETRY 7 /* unavailable unit */
279 struct cam_path *path;
280 struct sbp_target *target;
281 struct fwdma_alloc dma;
282 struct sbp_login_res *login;
283 struct callout login_callout;
285 STAILQ_HEAD(, sbp_ocb) ocbs;
286 STAILQ_HEAD(, sbp_ocb) free_ocbs;
295 struct sbp_dev *luns;
296 struct sbp_softc *sbp;
297 struct fw_device *fwdev;
298 u_int32_t mgm_hi, mgm_lo;
299 struct sbp_ocb *mgm_ocb_cur;
300 STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue;
301 struct callout mgm_ocb_timeout;
303 struct callout scan_callout;
304 STAILQ_HEAD(, fw_xfer) xferlist;
309 struct firewire_dev_comm fd;
311 struct cam_path *path;
312 struct sbp_target targets[SBP_NUM_TARGETS];
315 #define SBP_RESOURCE_SHORTAGE 0x10
318 static void sbp_post_explore (void *);
319 static void sbp_recv (struct fw_xfer *);
320 static void sbp_mgm_callback (struct fw_xfer *);
321 static void sbp_cmd_callback (struct fw_xfer *);
322 static void sbp_orb_pointer (struct sbp_dev *, struct sbp_ocb *);
323 static void sbp_execute_ocb (void *, bus_dma_segment_t *, int, int);
324 static void sbp_free_ocb (struct sbp_dev *, struct sbp_ocb *);
325 static void sbp_abort_ocb (struct sbp_ocb *, int);
326 static void sbp_abort_all_ocbs (struct sbp_dev *, int);
327 static struct fw_xfer * sbp_write_cmd (struct sbp_dev *, int, int);
328 static struct sbp_ocb * sbp_get_ocb (struct sbp_dev *);
329 static struct sbp_ocb * sbp_enqueue_ocb (struct sbp_dev *, struct sbp_ocb *);
330 static struct sbp_ocb * sbp_dequeue_ocb (struct sbp_dev *, struct sbp_status *);
331 static void sbp_cam_detach_target (struct sbp_target *);
332 static void sbp_mgm_timeout (void *arg);
333 static void sbp_timeout (void *arg);
334 static void sbp_mgm_orb (struct sbp_dev *, int, struct sbp_ocb *);
335 #define sbp_login(sdev) \
336 callout_reset(&(sdev)->login_callout, LOGIN_DELAY * hz, \
337 sbp_login_callout, (void *)(sdev));
339 MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire");
341 /* cam related functions */
342 static void sbp_action(struct cam_sim *sim, union ccb *ccb);
343 static void sbp_poll(struct cam_sim *sim);
344 static void sbp_cam_scan_lun(struct cam_periph *, union ccb *);
345 static void sbp_cam_scan_target(void *arg);
347 static char *orb_status0[] = {
348 /* 0 */ "No additional information to report",
349 /* 1 */ "Request type not supported",
350 /* 2 */ "Speed not supported",
351 /* 3 */ "Page size not supported",
352 /* 4 */ "Access denied",
353 /* 5 */ "Logical unit not supported",
354 /* 6 */ "Maximum payload too small",
355 /* 7 */ "Reserved for future standardization",
356 /* 8 */ "Resources unavailable",
357 /* 9 */ "Function rejected",
358 /* A */ "Login ID not recognized",
359 /* B */ "Dummy ORB completed",
360 /* C */ "Request aborted",
361 /* FF */ "Unspecified error"
362 #define MAX_ORB_STATUS0 0xd
365 static char *orb_status1_object[] = {
366 /* 0 */ "Operation request block (ORB)",
367 /* 1 */ "Data buffer",
368 /* 2 */ "Page table",
369 /* 3 */ "Unable to specify"
372 static char *orb_status1_serial_bus_error[] = {
373 /* 0 */ "Missing acknowledge",
374 /* 1 */ "Reserved; not to be used",
375 /* 2 */ "Time-out error",
376 /* 3 */ "Reserved; not to be used",
377 /* 4 */ "Busy retry limit exceeded(X)",
378 /* 5 */ "Busy retry limit exceeded(A)",
379 /* 6 */ "Busy retry limit exceeded(B)",
380 /* 7 */ "Reserved for future standardization",
381 /* 8 */ "Reserved for future standardization",
382 /* 9 */ "Reserved for future standardization",
383 /* A */ "Reserved for future standardization",
384 /* B */ "Tardy retry limit exceeded",
385 /* C */ "Conflict error",
386 /* D */ "Data error",
387 /* E */ "Type error",
388 /* F */ "Address error"
392 sbp_identify(driver_t *driver, device_t parent)
396 printf("sbp_identify\n");
399 child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent));
406 sbp_probe(device_t dev)
411 printf("sbp_probe\n");
414 pa = device_get_parent(dev);
415 if(device_get_unit(dev) != device_get_unit(pa)){
419 device_set_desc(dev, "SBP2/SCSI over firewire");
427 sbp_show_sdev_info(struct sbp_dev *sdev, int new)
429 struct fw_device *fwdev;
432 device_get_nameunit(sdev->target->sbp->fd.dev),
433 sdev->target->target_id,
439 fwdev = sdev->target->fwdev;
440 printf("ordered:%d type:%d EUI:%08x%08x node:%d "
441 "speed:%d maxrec:%d",
442 (sdev->type & 0x40) >> 6,
454 sbp_show_sdev_info(sdev, 2);
455 printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision);
463 /* Bus Target EUI64 */
465 {0, 2, {0x00018ea0, 0x01fd0154}}, /* Logitec HDD */
466 {0, 0, {0x00018ea6, 0x00100682}}, /* Logitec DVD */
467 {0, 1, {0x00d03200, 0xa412006a}}, /* Yano HDD */
473 sbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev)
475 int bus, i, target=-1;
476 char w[SBP_NUM_TARGETS];
479 bus = device_get_unit(sbp->fd.dev);
481 /* XXX wired-down configuration should be gotten from
482 tunable or device hint */
483 for (i = 0; wired[i].bus >= 0; i ++) {
484 if (wired[i].bus == bus) {
485 w[wired[i].target] = 1;
486 if (wired[i].eui.hi == fwdev->eui.hi &&
487 wired[i].eui.lo == fwdev->eui.lo)
488 target = wired[i].target;
492 if(target < SBP_NUM_TARGETS &&
493 sbp->targets[target].fwdev == NULL)
495 device_printf(sbp->fd.dev,
496 "target %d is not free for %08x:%08x\n",
497 target, fwdev->eui.hi, fwdev->eui.lo);
500 /* non-wired target */
501 for (i = 0; i < SBP_NUM_TARGETS; i ++)
502 if (sbp->targets[i].fwdev == NULL && w[i] == 0) {
510 static struct sbp_target *
511 sbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
514 struct sbp_target *target;
515 struct sbp_dev *sdev;
516 struct crom_context cc;
520 printf("sbp_alloc_target\n");
522 i = sbp_new_target(sbp, fwdev);
524 device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n");
528 target = &sbp->targets[i];
530 target->fwdev = fwdev;
531 target->target_id = i;
532 /* XXX we may want to reload mgm port after each bus reset */
533 /* XXX there might be multiple management agents */
534 crom_init_context(&cc, target->fwdev->csrrom);
535 reg = crom_search_key(&cc, CROM_MGM);
536 if (reg == NULL || reg->val == 0) {
537 printf("NULL management address\n");
538 target->fwdev = NULL;
541 target->mgm_hi = 0xffff;
542 target->mgm_lo = 0xf0000000 | (reg->val << 2);
543 target->mgm_ocb_cur = NULL;
545 printf("target:%d mgm_port: %x\n", i, target->mgm_lo);
547 STAILQ_INIT(&target->xferlist);
549 STAILQ_INIT(&target->mgm_ocb_queue);
550 CALLOUT_INIT(&target->mgm_ocb_timeout);
551 CALLOUT_INIT(&target->scan_callout);
553 /* XXX num_lun may be changed. realloc luns? */
554 crom_init_context(&cc, target->fwdev->csrrom);
555 /* XXX shoud parse appropriate unit directories only */
557 while (cc.depth >= 0) {
558 reg = crom_search_key(&cc, CROM_LUN);
561 lun = reg->val & 0xffff;
563 printf("target %d lun %d found\n", target->target_id, lun);
570 printf("no lun found!\n");
571 if (maxlun >= SBP_NUM_LUNS)
572 maxlun = SBP_NUM_LUNS;
573 target->num_lun = maxlun + 1;
574 target->luns = (struct sbp_dev *) malloc(
575 sizeof(struct sbp_dev) * target->num_lun,
576 M_SBP, M_NOWAIT | M_ZERO);
577 for (i = 0; i < target->num_lun; i++) {
578 sdev = &target->luns[i];
580 sdev->target = target;
581 STAILQ_INIT(&sdev->ocbs);
582 CALLOUT_INIT(&sdev->login_callout);
583 sdev->status = SBP_DEV_DEAD;
585 crom_init_context(&cc, target->fwdev->csrrom);
586 while (cc.depth >= 0) {
587 reg = crom_search_key(&cc, CROM_LUN);
590 lun = reg->val & 0xffff;
591 if (lun >= SBP_NUM_LUNS) {
592 printf("too large lun %d\n", lun);
595 sdev = &target->luns[lun];
596 sdev->status = SBP_DEV_RESET;
597 sdev->type = (reg->val & 0xf0000) >> 16;
599 fwdma_malloc(sbp->fd.fc,
600 /* alignment */ sizeof(u_int32_t),
601 SBP_DMA_SIZE, &sdev->dma, BUS_DMA_NOWAIT);
602 if (sdev->dma.v_addr == NULL) {
603 printf("%s: dma space allocation failed\n",
607 sdev->login = (struct sbp_login_res *) sdev->dma.v_addr;
608 sdev->ocb = (struct sbp_ocb *)
609 ((char *)sdev->dma.v_addr + SBP_LOGIN_SIZE);
610 bzero((char *)sdev->ocb,
611 sizeof (struct sbp_ocb) * SBP_QUEUE_LEN);
613 STAILQ_INIT(&sdev->free_ocbs);
614 for (i = 0; i < SBP_QUEUE_LEN; i++) {
617 ocb->bus_addr = sdev->dma.bus_addr
619 + sizeof(struct sbp_ocb) * i
620 + offsetof(struct sbp_ocb, orb[0]);
621 if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
622 printf("sbp_attach: cannot create dmamap\n");
625 sbp_free_ocb(sdev, ocb);
633 sbp_probe_lun(struct sbp_dev *sdev)
635 struct fw_device *fwdev;
636 struct crom_context c, *cc = &c;
639 bzero(sdev->vendor, sizeof(sdev->vendor));
640 bzero(sdev->product, sizeof(sdev->product));
642 fwdev = sdev->target->fwdev;
643 crom_init_context(cc, fwdev->csrrom);
644 /* get vendor string */
645 crom_search_key(cc, CSRKEY_VENDOR);
647 crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor));
648 /* skip to the unit directory for SBP-2 */
649 while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) {
650 if (reg->val == CSRVAL_T10SBP2)
654 /* get firmware revision */
655 reg = crom_search_key(cc, CSRKEY_FIRM_VER);
657 snprintf(sdev->revision, sizeof(sdev->revision),
659 /* get product string */
660 crom_search_key(cc, CSRKEY_MODEL);
662 crom_parse_text(cc, sdev->product, sizeof(sdev->product));
666 sbp_login_callout(void *arg)
668 struct sbp_dev *sdev = (struct sbp_dev *)arg;
669 sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
672 #define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
673 && crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
676 sbp_probe_target(void *arg)
678 struct sbp_target *target = (struct sbp_target *)arg;
679 struct sbp_softc *sbp;
680 struct sbp_dev *sdev;
681 struct firewire_comm *fc;
684 alive = SBP_FWDEV_ALIVE(target->fwdev);
686 printf("sbp_probe_target %d\n", target->target_id);
688 printf("not alive\n");
692 fc = target->sbp->fd.fc;
693 /* XXX untimeout mgm_ocb and dequeue */
694 for (i=0; i < target->num_lun; i++) {
695 sdev = &target->luns[i];
696 if (alive && (sdev->status != SBP_DEV_DEAD)) {
697 if (sdev->path != NULL) {
698 xpt_freeze_devq(sdev->path, 1);
703 sbp_show_sdev_info(sdev,
704 (sdev->status == SBP_DEV_RESET));
707 sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
708 switch (sdev->status) {
710 /* new or revived target */
714 case SBP_DEV_TOATTACH:
716 case SBP_DEV_ATTACHED:
719 sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
723 switch (sdev->status) {
724 case SBP_DEV_ATTACHED:
726 /* the device has gone */
727 sbp_show_sdev_info(sdev, 2);
728 printf("lost target\n");
731 xpt_freeze_devq(sdev->path, 1);
734 sdev->status = SBP_DEV_RETRY;
735 sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
738 case SBP_DEV_TOATTACH:
739 sdev->status = SBP_DEV_RESET;
751 sbp_post_busreset(void *arg)
753 struct sbp_softc *sbp;
755 sbp = (struct sbp_softc *)arg;
757 printf("sbp_post_busreset\n");
762 sbp_post_explore(void *arg)
764 struct sbp_softc *sbp = (struct sbp_softc *)arg;
765 struct sbp_target *target;
766 struct fw_device *fwdev;
770 printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
773 * XXX don't let CAM the bus rest. CAM tries to do something with
774 * freezed (DEV_RETRY) devices
776 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
781 /* Gabage Collection */
782 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
783 target = &sbp->targets[i];
784 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
785 if (target->fwdev == NULL || target->fwdev == fwdev)
788 /* device has removed in lower driver */
789 sbp_cam_detach_target(target);
790 if (target->luns != NULL)
791 free(target->luns, M_SBP);
792 target->num_lun = 0;;
794 target->fwdev = NULL;
797 /* traverse device list */
798 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
800 printf("sbp_post_explore: EUI:%08x%08x ",
801 fwdev->eui.hi, fwdev->eui.lo);
802 if (fwdev->status != FWDEVATTACHED)
803 printf("not attached, state=%d.\n", fwdev->status);
805 printf("attached\n");
807 alive = SBP_FWDEV_ALIVE(fwdev);
808 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
809 target = &sbp->targets[i];
810 if(target->fwdev == fwdev ) {
815 if(i == SBP_NUM_TARGETS){
818 target = sbp_alloc_target(sbp, fwdev);
825 sbp_probe_target((void *)target);
831 sbp_loginres_callback(struct fw_xfer *xfer){
833 struct sbp_dev *sdev;
834 sdev = (struct sbp_dev *)xfer->sc;
836 sbp_show_sdev_info(sdev, 2);
837 printf("sbp_loginres_callback\n");
841 STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link);
848 sbp_xfer_free(struct fw_xfer *xfer)
850 struct sbp_dev *sdev;
853 sdev = (struct sbp_dev *)xfer->sc;
854 fw_xfer_unload(xfer);
856 STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
861 sbp_reset_start_callback(struct fw_xfer *xfer)
863 struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
864 struct sbp_target *target = sdev->target;
867 if (xfer->resp != 0) {
868 sbp_show_sdev_info(sdev, 2);
869 printf("sbp_reset_start failed: resp=%d\n", xfer->resp);
872 for (i = 0; i < target->num_lun; i++) {
873 tsdev = &target->luns[i];
874 if (tsdev->status == SBP_DEV_LOGIN)
880 sbp_reset_start(struct sbp_dev *sdev)
882 struct fw_xfer *xfer;
886 sbp_show_sdev_info(sdev, 2);
887 printf("sbp_reset_start\n");
890 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
891 xfer->act.hand = sbp_reset_start_callback;
892 fp = (struct fw_pkt *)xfer->send.buf;
893 fp->mode.wreqq.dest_hi = 0xffff;
894 fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
895 fp->mode.wreqq.data = htonl(0xf);
896 fw_asyreq(xfer->fc, -1, xfer);
900 sbp_mgm_callback(struct fw_xfer *xfer)
902 struct sbp_dev *sdev;
905 sdev = (struct sbp_dev *)xfer->sc;
908 sbp_show_sdev_info(sdev, 2);
909 printf("sbp_mgm_callback\n");
915 sbp_show_sdev_info(sdev, 2);
916 printf("management ORB failed(%d) ... RESET_START\n", resp);
917 sbp_reset_start(sdev);
924 sbp_cmd_callback(struct fw_xfer *xfer)
927 struct sbp_dev *sdev;
928 sdev = (struct sbp_dev *)xfer->sc;
929 sbp_show_sdev_info(sdev, 2);
930 printf("sbp_cmd_callback\n");
936 static struct sbp_dev *
937 sbp_next_dev(struct sbp_target *target, int lun)
939 struct sbp_dev *sdev;
942 for (i = lun, sdev = &target->luns[lun];
943 i < target->num_lun; i++, sdev++) {
944 if (sdev->status == SBP_DEV_PROBE)
947 if (i >= target->num_lun)
954 sbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb)
956 struct sbp_target *target;
957 struct sbp_dev *sdev;
959 sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
960 target = sdev->target;
962 sbp_show_sdev_info(sdev, 2);
963 printf("sbp_cam_scan_lun\n");
965 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
966 sdev->status = SBP_DEV_ATTACHED;
968 sbp_show_sdev_info(sdev, 2);
969 printf("scan failed\n");
971 sdev = sbp_next_dev(target, sdev->lun_id + 1);
977 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
978 ccb->ccb_h.ccb_sdev_ptr = sdev;
980 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
985 sbp_cam_scan_target(void *arg)
987 struct sbp_target *target = (struct sbp_target *)arg;
988 struct sbp_dev *sdev;
991 sdev = sbp_next_dev(target, 0);
993 printf("sbp_cam_scan_target: nothing to do for target%d\n",
998 sbp_show_sdev_info(sdev, 2);
999 printf("sbp_cam_scan_target\n");
1001 ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO);
1003 printf("sbp_cam_scan_target: malloc failed\n");
1006 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
1007 ccb->ccb_h.func_code = XPT_SCAN_LUN;
1008 ccb->ccb_h.cbfcnp = sbp_cam_scan_lun;
1009 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1010 ccb->crcn.flags = CAM_FLAG_NONE;
1011 ccb->ccb_h.ccb_sdev_ptr = sdev;
1013 /* The scan is in progress now. */
1015 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1019 static __inline void
1020 sbp_scan_dev(struct sbp_dev *sdev)
1022 sdev->status = SBP_DEV_PROBE;
1023 callout_reset(&sdev->target->scan_callout, SCAN_DELAY * hz,
1024 sbp_cam_scan_target, (void *)sdev->target);
1028 sbp_do_attach(struct fw_xfer *xfer)
1030 struct sbp_dev *sdev;
1031 struct sbp_target *target;
1032 struct sbp_softc *sbp;
1034 sdev = (struct sbp_dev *)xfer->sc;
1035 target = sdev->target;
1038 sbp_show_sdev_info(sdev, 2);
1039 printf("sbp_do_attach\n");
1041 sbp_xfer_free(xfer);
1043 if (sdev->path == NULL)
1044 xpt_create_path(&sdev->path, xpt_periph,
1045 cam_sim_path(target->sbp->sim),
1046 target->target_id, sdev->lun_id);
1049 * Let CAM scan the bus if we are in the boot process.
1050 * XXX xpt_scan_bus cannot detect LUN larger than 0
1051 * if LUN 0 doesn't exists.
1054 sdev->status = SBP_DEV_ATTACHED;
1063 sbp_agent_reset_callback(struct fw_xfer *xfer)
1065 struct sbp_dev *sdev;
1067 sdev = (struct sbp_dev *)xfer->sc;
1069 sbp_show_sdev_info(sdev, 2);
1070 printf("sbp_cmd_callback\n");
1072 if (xfer->resp != 0) {
1073 sbp_show_sdev_info(sdev, 2);
1074 printf("sbp_cmd_callback resp=%d\n", xfer->resp);
1077 sbp_xfer_free(xfer);
1079 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1085 sbp_agent_reset(struct sbp_dev *sdev)
1087 struct fw_xfer *xfer;
1091 sbp_show_sdev_info(sdev, 2);
1092 printf("sbp_agent_reset\n");
1094 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1097 if (sdev->status == SBP_DEV_ATTACHED)
1098 xfer->act.hand = sbp_agent_reset_callback;
1100 xfer->act.hand = sbp_do_attach;
1101 fp = (struct fw_pkt *)xfer->send.buf;
1102 fp->mode.wreqq.data = htonl(0xf);
1103 fw_asyreq(xfer->fc, -1, xfer);
1104 sbp_abort_all_ocbs(sdev, CAM_BDR_SENT);
1108 sbp_busy_timeout_callback(struct fw_xfer *xfer)
1110 struct sbp_dev *sdev;
1112 sdev = (struct sbp_dev *)xfer->sc;
1114 sbp_show_sdev_info(sdev, 2);
1115 printf("sbp_busy_timeout_callback\n");
1117 sbp_xfer_free(xfer);
1118 sbp_agent_reset(sdev);
1122 sbp_busy_timeout(struct sbp_dev *sdev)
1125 struct fw_xfer *xfer;
1127 sbp_show_sdev_info(sdev, 2);
1128 printf("sbp_busy_timeout\n");
1130 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1132 xfer->act.hand = sbp_busy_timeout_callback;
1133 fp = (struct fw_pkt *)xfer->send.buf;
1134 fp->mode.wreqq.dest_hi = 0xffff;
1135 fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1136 fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1137 fw_asyreq(xfer->fc, -1, xfer);
1141 sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1143 struct fw_xfer *xfer;
1146 sbp_show_sdev_info(sdev, 2);
1147 printf("sbp_orb_pointer\n");
1150 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1153 xfer->act.hand = sbp_cmd_callback;
1155 fp = (struct fw_pkt *)xfer->send.buf;
1156 fp->mode.wreqb.len = 8;
1157 fp->mode.wreqb.extcode = 0;
1158 fp->mode.wreqb.payload[0] =
1159 htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1160 fp->mode.wreqb.payload[1] = htonl(ocb->bus_addr);
1162 if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1163 sbp_xfer_free(xfer);
1164 ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1171 sbp_doorbell(struct sbp_dev *sdev)
1173 struct fw_xfer *xfer;
1176 sbp_show_sdev_info(sdev, 2);
1177 printf("sbp_doorbell\n");
1180 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1183 xfer->act.hand = sbp_cmd_callback;
1184 fp = (struct fw_pkt *)xfer->send.buf;
1185 fp->mode.wreqq.data = htonl(0xf);
1186 fw_asyreq(xfer->fc, -1, xfer);
1190 static struct fw_xfer *
1191 sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1193 struct fw_xfer *xfer;
1195 struct sbp_target *target;
1198 target = sdev->target;
1200 xfer = STAILQ_FIRST(&target->xferlist);
1202 if (target->n_xfer > 5 /* XXX */) {
1203 printf("sbp: no more xfer for this target\n");
1207 xfer = fw_xfer_alloc_buf(M_SBP, 24, 12);
1209 printf("sbp: fw_xfer_alloc_buf failed\n");
1215 printf("sbp: alloc %d xfer\n", target->n_xfer);
1218 STAILQ_REMOVE_HEAD(&target->xferlist, link);
1222 microtime(&xfer->tv);
1224 if (tcode == FWTCODE_WREQQ)
1225 xfer->send.len = 16;
1227 xfer->send.len = 24;
1228 xfer->recv.len = 12;
1231 xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1232 xfer->fc = sdev->target->sbp->fd.fc;
1233 xfer->retry_req = fw_asybusy;
1235 xfer->sc = (caddr_t)sdev;
1236 fp = (struct fw_pkt *)xfer->send.buf;
1237 fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
1238 fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
1239 fp->mode.wreqq.tlrt = 0;
1240 fp->mode.wreqq.tcode = tcode;
1241 fp->mode.wreqq.pri = 0;
1242 xfer->dst = FWLOCALBUS | sdev->target->fwdev->dst;
1243 fp->mode.wreqq.dst = xfer->dst;
1250 sbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1252 struct fw_xfer *xfer;
1254 struct sbp_ocb *ocb;
1255 struct sbp_target *target;
1258 target = sdev->target;
1259 nid = target->sbp->fd.fc->nodeid | FWLOCALBUS;
1262 if (func == ORB_FUN_RUNQUEUE) {
1263 ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1264 if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1268 STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1271 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
1275 ocb->flags = OCB_ACT_MGM;
1278 bzero((void *)(uintptr_t)(volatile void *)ocb->orb, sizeof(ocb->orb));
1279 ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1280 ocb->orb[7] = htonl(SBP_DEV2ADDR(
1281 device_get_unit(target->sbp->fd.dev),
1286 sbp_show_sdev_info(sdev, 2);
1287 printf("%s\n", orb_fun_name[(func>>16)&0xf]);
1291 ocb->orb[2] = htonl(nid << 16);
1292 ocb->orb[3] = htonl(sdev->dma.bus_addr);
1293 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_EXV | sdev->lun_id);
1294 ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
1295 fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD);
1298 ocb->orb[0] = htonl((0 << 16) | 0);
1299 ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
1306 ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
1310 if (target->mgm_ocb_cur != NULL) {
1311 /* there is a standing ORB */
1312 STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1317 target->mgm_ocb_cur = ocb;
1320 callout_reset(&target->mgm_ocb_timeout, 5*hz,
1321 sbp_mgm_timeout, (caddr_t)ocb);
1322 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1326 xfer->act.hand = sbp_mgm_callback;
1328 fp = (struct fw_pkt *)xfer->send.buf;
1329 fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
1330 fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
1331 fp->mode.wreqb.len = 8;
1332 fp->mode.wreqb.extcode = 0;
1333 fp->mode.wreqb.payload[0] = htonl(nid << 16);
1334 fp->mode.wreqb.payload[1] = htonl(ocb->bus_addr);
1336 fw_asyreq(xfer->fc, -1, xfer);
1340 sbp_print_scsi_cmd(struct sbp_ocb *ocb)
1342 struct ccb_scsiio *csio;
1344 csio = &ocb->ccb->csio;
1345 printf("%s:%d:%d XPT_SCSI_IO: "
1346 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1348 "%db cmd/%db data/%db sense\n",
1349 device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1350 ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun,
1351 csio->cdb_io.cdb_bytes[0],
1352 csio->cdb_io.cdb_bytes[1],
1353 csio->cdb_io.cdb_bytes[2],
1354 csio->cdb_io.cdb_bytes[3],
1355 csio->cdb_io.cdb_bytes[4],
1356 csio->cdb_io.cdb_bytes[5],
1357 csio->cdb_io.cdb_bytes[6],
1358 csio->cdb_io.cdb_bytes[7],
1359 csio->cdb_io.cdb_bytes[8],
1360 csio->cdb_io.cdb_bytes[9],
1361 ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1362 csio->cdb_len, csio->dxfer_len,
1367 sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1369 struct sbp_cmd_status *sbp_cmd_status;
1370 struct scsi_sense_data *sense;
1372 sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1373 sense = &ocb->ccb->csio.sense_data;
1376 sbp_print_scsi_cmd(ocb);
1377 /* XXX need decode status */
1378 sbp_show_sdev_info(ocb->sdev, 2);
1379 printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1380 sbp_cmd_status->status,
1381 sbp_cmd_status->sfmt,
1382 sbp_cmd_status->valid,
1383 sbp_cmd_status->s_key,
1384 sbp_cmd_status->s_code,
1385 sbp_cmd_status->s_qlfr,
1390 switch (sbp_cmd_status->status) {
1391 case SCSI_STATUS_CHECK_COND:
1392 case SCSI_STATUS_BUSY:
1393 case SCSI_STATUS_CMD_TERMINATED:
1394 if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1395 sense->error_code = SSD_CURRENT_ERROR;
1397 sense->error_code = SSD_DEFERRED_ERROR;
1399 if(sbp_cmd_status->valid)
1400 sense->error_code |= SSD_ERRCODE_VALID;
1401 sense->flags = sbp_cmd_status->s_key;
1402 if(sbp_cmd_status->mark)
1403 sense->flags |= SSD_FILEMARK;
1404 if(sbp_cmd_status->eom)
1405 sense->flags |= SSD_EOM;
1406 if(sbp_cmd_status->ill_len)
1407 sense->flags |= SSD_ILI;
1408 sense->info[0] = ntohl(sbp_cmd_status->info) & 0xff;
1409 sense->info[1] =(ntohl(sbp_cmd_status->info) >> 8) & 0xff;
1410 sense->info[2] =(ntohl(sbp_cmd_status->info) >> 16) & 0xff;
1411 sense->info[3] =(ntohl(sbp_cmd_status->info) >> 24) & 0xff;
1412 if (sbp_status->len <= 1)
1413 /* XXX not scsi status. shouldn't be happened */
1414 sense->extra_len = 0;
1415 else if (sbp_status->len <= 4)
1416 /* add_sense_code(_qual), info, cmd_spec_info */
1417 sense->extra_len = 6;
1419 /* fru, sense_key_spec */
1420 sense->extra_len = 10;
1421 sense->cmd_spec_info[0] = ntohl(sbp_cmd_status->cdb) & 0xff;
1422 sense->cmd_spec_info[1] = (ntohl(sbp_cmd_status->cdb) >> 8) & 0xff;
1423 sense->cmd_spec_info[2] = (ntohl(sbp_cmd_status->cdb) >> 16) & 0xff;
1424 sense->cmd_spec_info[3] = (ntohl(sbp_cmd_status->cdb) >> 24) & 0xff;
1425 sense->add_sense_code = sbp_cmd_status->s_code;
1426 sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1427 sense->fru = sbp_cmd_status->fru;
1428 sense->sense_key_spec[0] = ntohl(sbp_cmd_status->s_keydep) & 0xff;
1429 sense->sense_key_spec[1] = (ntohl(sbp_cmd_status->s_keydep) >>8) & 0xff;
1430 sense->sense_key_spec[2] = (ntohl(sbp_cmd_status->s_keydep) >>16) & 0xff;
1432 ocb->ccb->csio.scsi_status = sbp_cmd_status->status;;
1433 ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1434 | CAM_AUTOSNS_VALID;
1439 for( j = 0 ; j < 32 ; j+=8){
1440 printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1441 tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1442 tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1449 sbp_show_sdev_info(ocb->sdev, 2);
1450 printf("sbp_scsi_status: unknown scsi status 0x%x\n",
1451 sbp_cmd_status->status);
1456 sbp_fix_inq_data(struct sbp_ocb *ocb)
1459 struct sbp_dev *sdev;
1460 struct scsi_inquiry_data *inq;
1465 if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1468 sbp_show_sdev_info(sdev, 2);
1469 printf("sbp_fix_inq_data\n");
1471 inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1472 switch (SID_TYPE(inq)) {
1475 * XXX Convert Direct Access device to RBC.
1476 * I've never seen FireWire DA devices which support READ_6.
1479 if (SID_TYPE(inq) == T_DIRECT)
1480 inq->device |= T_RBC; /* T_DIRECT == 0 */
1484 /* enable tag queuing */
1486 inq->flags |= SID_CmdQue;
1488 inq->flags &= ~SID_CmdQue;
1490 * Override vendor/product/revision information.
1491 * Some devices sometimes return strange strings.
1494 bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1495 bcopy(sdev->product, inq->product, sizeof(inq->product));
1496 bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1503 sbp_recv1(struct fw_xfer *xfer)
1509 struct sbp_softc *sbp;
1510 struct sbp_dev *sdev;
1511 struct sbp_ocb *ocb;
1512 struct sbp_login_res *login_res = NULL;
1513 struct sbp_status *sbp_status;
1514 struct sbp_target *target;
1515 int orb_fun, status_valid0, status_valid, t, l, reset_agent = 0;
1519 ld = xfer->recv.buf;
1520 printf("sbp %x %d %d %08x %08x %08x %08x\n",
1521 xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1522 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1523 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1526 sbp = (struct sbp_softc *)xfer->sc;
1527 if(xfer->resp != 0){
1528 printf("sbp_recv: xfer->resp != 0\n");
1531 if(xfer->recv.buf == NULL){
1532 printf("sbp_recv: xfer->recv.buf == NULL\n");
1535 sbp = (struct sbp_softc *)xfer->sc;
1536 rfp = (struct fw_pkt *)xfer->recv.buf;
1537 if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1538 printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1541 sbp_status = (struct sbp_status *)rfp->mode.wreqb.payload;
1542 addr = rfp->mode.wreqb.dest_lo;
1544 printf("received address 0x%x\n", addr);
1546 t = SBP_ADDR2TRG(addr);
1547 if (t >= SBP_NUM_TARGETS) {
1548 device_printf(sbp->fd.dev,
1549 "sbp_recv1: invalid target %d\n", t);
1552 target = &sbp->targets[t];
1553 l = SBP_ADDR2LUN(addr);
1554 if (l >= target->num_lun) {
1555 device_printf(sbp->fd.dev,
1556 "sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1559 sdev = &target->luns[l];
1562 switch (sbp_status->src) {
1565 /* check mgm_ocb_cur first */
1566 ocb = target->mgm_ocb_cur;
1568 if (OCB_MATCH(ocb, sbp_status)) {
1569 callout_stop(&target->mgm_ocb_timeout);
1570 target->mgm_ocb_cur = NULL;
1574 ocb = sbp_dequeue_ocb(sdev, sbp_status);
1576 sbp_show_sdev_info(sdev, 2);
1577 #if __FreeBSD_version >= 500000
1578 printf("No ocb(%x) on the queue\n",
1580 printf("No ocb(%lx) on the queue\n",
1582 ntohl(sbp_status->orb_lo));
1587 sbp_show_sdev_info(sdev, 2);
1588 printf("unsolicit status received\n");
1591 sbp_show_sdev_info(sdev, 2);
1592 printf("unknown sbp_status->src\n");
1595 status_valid0 = (sbp_status->src < 2
1596 && sbp_status->resp == ORB_RES_CMPL
1597 && sbp_status->dead == 0);
1598 status_valid = (status_valid0 && sbp_status->status == 0);
1600 if (!status_valid0 || debug > 1){
1603 sbp_show_sdev_info(sdev, 2);
1604 printf("ORB status src:%x resp:%x dead:%x"
1605 #if __FreeBSD_version >= 500000
1606 " len:%x stat:%x orb:%x%08x\n",
1608 " len:%x stat:%x orb:%x%08lx\n",
1610 sbp_status->src, sbp_status->resp, sbp_status->dead,
1611 sbp_status->len, sbp_status->status,
1612 ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1614 sbp_show_sdev_info(sdev, 2);
1615 status = sbp_status->status;
1616 switch(sbp_status->resp) {
1618 if (status > MAX_ORB_STATUS0)
1619 printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1621 printf("%s\n", orb_status0[status]);
1624 printf("Obj: %s, Error: %s\n",
1625 orb_status1_object[(status>>6) & 3],
1626 orb_status1_serial_bus_error[status & 0xf]);
1629 printf("Illegal request\n");
1632 printf("Vendor dependent\n");
1635 printf("unknown respose code %d\n", sbp_status->resp);
1639 /* we have to reset the fetch agent if it's dead */
1640 if (sbp_status->dead) {
1642 xpt_freeze_devq(sdev->path, 1);
1651 switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1657 switch(ocb->flags) {
1659 orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1662 fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD);
1663 login_res = sdev->login;
1664 login_res->len = ntohs(login_res->len);
1665 login_res->id = ntohs(login_res->id);
1666 login_res->cmd_hi = ntohs(login_res->cmd_hi);
1667 login_res->cmd_lo = ntohl(login_res->cmd_lo);
1670 sbp_show_sdev_info(sdev, 2);
1671 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));
1673 sbp_busy_timeout(sdev);
1675 /* forgot logout? */
1676 sbp_show_sdev_info(sdev, 2);
1677 printf("login failed\n");
1678 sdev->status = SBP_DEV_RESET;
1682 login_res = sdev->login;
1685 sbp_show_sdev_info(sdev, 2);
1686 printf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo);
1689 if (sdev->status == SBP_DEV_ATTACHED)
1692 sbp_agent_reset(sdev);
1694 sdev->status = SBP_DEV_ATTACHED;
1695 sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL);
1698 /* reconnection hold time exceed? */
1700 sbp_show_sdev_info(sdev, 2);
1701 printf("reconnect failed\n");
1707 sdev->status = SBP_DEV_RESET;
1710 sbp_busy_timeout(sdev);
1715 sbp_agent_reset(sdev);
1718 sbp_show_sdev_info(sdev, 2);
1719 printf("unknown function %d\n", orb_fun);
1722 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
1726 if(ocb->ccb != NULL){
1730 ld = ocb->ccb->csio.data_ptr;
1731 if(ld != NULL && ocb->ccb->csio.dxfer_len != 0)
1732 printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]);
1734 printf("ptr NULL\n");
1735 printf("len %d\n", sbp_status->len);
1738 if(sbp_status->len > 1){
1739 sbp_scsi_status(sbp_status, ocb);
1741 if(sbp_status->resp != ORB_RES_CMPL){
1742 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1744 ccb->ccb_h.status = CAM_REQ_CMP;
1747 /* fix up inq data */
1748 if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1749 sbp_fix_inq_data(ocb);
1758 sbp_free_ocb(sdev, ocb);
1761 sbp_agent_reset(sdev);
1764 /* The received packet is usually small enough to be stored within
1765 * the buffer. In that case, the controller return ack_complete and
1766 * no respose is necessary.
1768 * XXX fwohci.c and firewire.c should inform event_code such as
1769 * ack_complete or ack_pending to upper driver.
1773 sfp = (struct fw_pkt *)xfer->send.buf;
1774 sfp->mode.wres.dst = rfp->mode.wreqb.src;
1775 xfer->dst = sfp->mode.wres.dst;
1776 xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1777 xfer->act.hand = sbp_loginres_callback;
1778 xfer->retry_req = fw_asybusy;
1780 sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1781 sfp->mode.wres.tcode = FWTCODE_WRES;
1782 sfp->mode.wres.rtcode = 0;
1783 sfp->mode.wres.pri = 0;
1785 fw_asyreq(xfer->fc, -1, xfer);
1788 xfer->recv.len = SBP_RECV_LEN;
1789 STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1797 sbp_recv(struct fw_xfer *xfer)
1809 sbp_attach(device_t dev)
1811 struct sbp_softc *sbp;
1812 struct cam_devq *devq;
1813 struct fw_xfer *xfer;
1817 printf("sbp_attach (cold=%d)\n", cold);
1822 sbp = ((struct sbp_softc *)device_get_softc(dev));
1823 bzero(sbp, sizeof(struct sbp_softc));
1825 sbp->fd.fc = device_get_ivars(dev);
1826 error = bus_dma_tag_create(/*parent*/sbp->fd.fc->dmat,
1827 /* XXX shoud be 4 for sane backend? */
1830 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1831 /*highaddr*/BUS_SPACE_MAXADDR,
1832 /*filter*/NULL, /*filterarg*/NULL,
1833 /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1834 /*maxsegsz*/SBP_SEG_MAX,
1835 /*flags*/BUS_DMA_ALLOCNOW,
1838 printf("sbp_attach: Could not allocate DMA tag "
1839 "- error %d\n", error);
1843 devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1847 for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1848 sbp->targets[i].fwdev = NULL;
1849 sbp->targets[i].luns = NULL;
1852 sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1853 device_get_unit(dev),
1855 /*tagged*/ SBP_QUEUE_LEN,
1858 if (sbp->sim == NULL) {
1859 cam_simq_free(devq);
1864 if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS)
1867 if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim),
1868 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1871 sbp->fwb.start_hi = SBP_BIND_HI;
1872 sbp->fwb.start_lo = SBP_DEV2ADDR(device_get_unit(sbp->fd.dev), 0, 0);
1873 /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1874 sbp->fwb.addrlen = 0xffff;
1875 sbp->fwb.act_type = FWACT_XFER;
1876 /* pre-allocate xfer */
1877 STAILQ_INIT(&sbp->fwb.xferlist);
1878 for (i = 0; i < SBP_NUM_OCB/2; i ++) {
1879 xfer = fw_xfer_alloc_buf(M_SBP,
1885 /* recv */SBP_RECV_LEN);
1886 xfer->act.hand = sbp_recv;
1888 xfer->fc = sbp->fd.fc;
1890 xfer->sc = (caddr_t)sbp;
1891 STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1893 fw_bindadd(sbp->fd.fc, &sbp->fwb);
1895 sbp->fd.post_busreset = sbp_post_busreset;
1896 sbp->fd.post_explore = sbp_post_explore;
1898 if (sbp->fd.fc->status != -1) {
1900 sbp_post_explore((void *)sbp);
1906 cam_sim_free(sbp->sim, /*free_devq*/TRUE);
1911 sbp_logout_all(struct sbp_softc *sbp)
1913 struct sbp_target *target;
1914 struct sbp_dev *sdev;
1918 printf("sbp_logout_all\n");
1920 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
1921 target = &sbp->targets[i];
1922 if (target->luns == NULL)
1924 for (j = 0; j < target->num_lun; j++) {
1925 sdev = &target->luns[j];
1926 callout_stop(&sdev->login_callout);
1927 if (sdev->status >= SBP_DEV_TOATTACH &&
1928 sdev->status <= SBP_DEV_ATTACHED)
1929 sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
1937 sbp_shutdown(device_t dev)
1939 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1941 sbp_logout_all(sbp);
1946 sbp_detach(device_t dev)
1948 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1949 struct firewire_comm *fc = sbp->fd.fc;
1950 struct sbp_target *target;
1951 struct sbp_dev *sdev;
1952 struct fw_xfer *xfer, *next;
1956 printf("sbp_detach\n");
1959 for (i = 0; i < SBP_NUM_TARGETS; i ++)
1960 sbp_cam_detach_target(&sbp->targets[i]);
1961 xpt_free_path(sbp->path);
1962 xpt_bus_deregister(cam_sim_path(sbp->sim));
1964 sbp_logout_all(sbp);
1966 /* XXX wait for logout completion */
1967 tsleep(&i, FWPRI, "sbpdtc", hz/2);
1969 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
1970 target = &sbp->targets[i];
1971 if (target->luns == NULL)
1973 callout_stop(&target->mgm_ocb_timeout);
1974 for (j = 0; j < target->num_lun; j++) {
1975 sdev = &target->luns[j];
1976 if (sdev->status != SBP_DEV_DEAD) {
1977 for (i = 0; i < SBP_QUEUE_LEN; i++)
1978 bus_dmamap_destroy(sbp->dmat,
1979 sdev->ocb[i].dmamap);
1980 fwdma_free(sbp->fd.fc, &sdev->dma);
1983 for (xfer = STAILQ_FIRST(&target->xferlist);
1984 xfer != NULL; xfer = next) {
1985 next = STAILQ_NEXT(xfer, link);
1988 free(target->luns, M_SBP);
1991 for (xfer = STAILQ_FIRST(&sbp->fwb.xferlist);
1992 xfer != NULL; xfer = next) {
1993 next = STAILQ_NEXT(xfer, link);
1996 STAILQ_INIT(&sbp->fwb.xferlist);
1997 fw_bindremove(fc, &sbp->fwb);
1999 bus_dma_tag_destroy(sbp->dmat);
2005 sbp_cam_detach_target(struct sbp_target *target)
2007 struct sbp_dev *sdev;
2010 if (target->luns != NULL) {
2012 printf("sbp_detach_target %d\n", target->target_id);
2014 callout_stop(&target->scan_callout);
2015 for (i = 0; i < target->num_lun; i++) {
2016 sdev = &target->luns[i];
2017 if (sdev->status == SBP_DEV_DEAD)
2019 if (sdev->status == SBP_DEV_RESET)
2022 xpt_release_devq(sdev->path,
2023 sdev->freeze, TRUE);
2025 xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
2026 xpt_free_path(sdev->path);
2029 sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
2035 sbp_target_reset(struct sbp_dev *sdev, int method)
2038 struct sbp_target *target = sdev->target;
2039 struct sbp_dev *tsdev;
2041 for (i = 0; i < target->num_lun; i++) {
2042 tsdev = &target->luns[i];
2043 if (tsdev->status == SBP_DEV_DEAD)
2045 if (tsdev->status == SBP_DEV_RESET)
2047 xpt_freeze_devq(tsdev->path, 1);
2049 sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT);
2051 tsdev->status = SBP_DEV_LOGIN;
2055 printf("target reset\n");
2056 sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2059 printf("reset start\n");
2060 sbp_reset_start(sdev);
2067 sbp_mgm_timeout(void *arg)
2069 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2070 struct sbp_dev *sdev = ocb->sdev;
2071 struct sbp_target *target = sdev->target;
2073 sbp_show_sdev_info(sdev, 2);
2074 printf("management ORB timeout\n");
2075 target->mgm_ocb_cur = NULL;
2076 sbp_free_ocb(sdev, ocb);
2079 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2082 sbp_reset_start(sdev);
2087 sbp_timeout(void *arg)
2089 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2090 struct sbp_dev *sdev = ocb->sdev;
2092 sbp_show_sdev_info(sdev, 2);
2093 printf("request timeout ... ");
2096 switch(sdev->timeout) {
2098 printf("agent reset\n");
2099 xpt_freeze_devq(sdev->path, 1);
2101 sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2102 sbp_agent_reset(sdev);
2106 sbp_target_reset(sdev, sdev->timeout - 1);
2111 sbp_cam_detach_target(target);
2112 if (target->luns != NULL)
2113 free(target->luns, M_SBP);
2114 target->num_lun = 0;;
2115 target->luns = NULL;
2116 target->fwdev = NULL;
2122 sbp_action1(struct cam_sim *sim, union ccb *ccb)
2125 struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2126 struct sbp_target *target = NULL;
2127 struct sbp_dev *sdev = NULL;
2129 /* target:lun -> sdev mapping */
2131 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2132 && ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2133 target = &sbp->targets[ccb->ccb_h.target_id];
2134 if (target->fwdev != NULL
2135 && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2136 && ccb->ccb_h.target_lun < target->num_lun) {
2137 sdev = &target->luns[ccb->ccb_h.target_lun];
2138 if (sdev->status != SBP_DEV_ATTACHED &&
2139 sdev->status != SBP_DEV_PROBE)
2146 printf("invalid target %d lun %d\n",
2147 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2150 switch (ccb->ccb_h.func_code) {
2153 case XPT_GET_TRAN_SETTINGS:
2154 case XPT_SET_TRAN_SETTINGS:
2155 case XPT_CALC_GEOMETRY:
2158 printf("%s:%d:%d:func_code 0x%04x: "
2159 "Invalid target (target needed)\n",
2160 device_get_nameunit(sbp->fd.dev),
2161 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2162 ccb->ccb_h.func_code);
2165 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2172 /* The opcodes sometimes aimed at a target (sc is valid),
2173 * sometimes aimed at the SIM (sc is invalid and target is
2174 * CAM_TARGET_WILDCARD)
2177 ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2179 printf("%s:%d:%d func_code 0x%04x: "
2180 "Invalid target (no wildcard)\n",
2181 device_get_nameunit(sbp->fd.dev),
2182 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2183 ccb->ccb_h.func_code);
2185 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2191 /* XXX Hm, we should check the input parameters */
2195 switch (ccb->ccb_h.func_code) {
2198 struct ccb_scsiio *csio;
2199 struct sbp_ocb *ocb;
2206 printf("%s:%d:%d XPT_SCSI_IO: "
2207 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2209 "%db cmd/%db data/%db sense\n",
2210 device_get_nameunit(sbp->fd.dev),
2211 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2212 csio->cdb_io.cdb_bytes[0],
2213 csio->cdb_io.cdb_bytes[1],
2214 csio->cdb_io.cdb_bytes[2],
2215 csio->cdb_io.cdb_bytes[3],
2216 csio->cdb_io.cdb_bytes[4],
2217 csio->cdb_io.cdb_bytes[5],
2218 csio->cdb_io.cdb_bytes[6],
2219 csio->cdb_io.cdb_bytes[7],
2220 csio->cdb_io.cdb_bytes[8],
2221 csio->cdb_io.cdb_bytes[9],
2222 ccb->ccb_h.flags & CAM_DIR_MASK,
2223 csio->cdb_len, csio->dxfer_len,
2227 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2232 /* if we are in probe stage, pass only probe commands */
2233 if (sdev->status == SBP_DEV_PROBE) {
2235 name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2236 printf("probe stage, periph name: %s\n", name);
2237 if (strcmp(name, "probe") != 0) {
2238 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2244 if ((ocb = sbp_get_ocb(sdev)) == NULL)
2247 ocb->flags = OCB_ACT_CMD;
2250 ccb->ccb_h.ccb_sdev_ptr = sdev;
2251 ocb->orb[0] = htonl(1 << 31);
2253 ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2254 ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2255 speed = min(target->fwdev->speed, max_speed);
2256 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2257 | ORB_CMD_MAXP(speed + 7));
2258 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
2259 ocb->orb[4] |= htonl(ORB_CMD_IN);
2262 if (csio->ccb_h.flags & CAM_SCATTER_VALID)
2263 printf("sbp: CAM_SCATTER_VALID\n");
2264 if (csio->ccb_h.flags & CAM_DATA_PHYS)
2265 printf("sbp: CAM_DATA_PHYS\n");
2267 if (csio->ccb_h.flags & CAM_CDB_POINTER)
2268 cdb = (void *)csio->cdb_io.cdb_ptr;
2270 cdb = (void *)&csio->cdb_io.cdb_bytes;
2272 (void *)(uintptr_t)(volatile void *)&ocb->orb[5],
2275 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2276 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2278 if (ccb->csio.dxfer_len > 0) {
2282 error = bus_dmamap_load(/*dma tag*/sbp->dmat,
2283 /*dma map*/ocb->dmamap,
2285 ccb->csio.dxfer_len,
2291 printf("sbp: bus_dmamap_load error %d\n", error);
2293 sbp_execute_ocb(ocb, NULL, 0, 0);
2296 case XPT_CALC_GEOMETRY:
2298 struct ccb_calc_geometry *ccg;
2300 u_int32_t secs_per_cylinder;
2304 if (ccg->block_size == 0) {
2305 printf("sbp_action1: block_size is 0.\n");
2306 ccb->ccb_h.status = CAM_REQ_INVALID;
2311 printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2312 #if __FreeBSD_version >= 500000
2313 "Volume size = %jd\n",
2315 "Volume size = %d\n",
2317 device_get_nameunit(sbp->fd.dev),
2318 cam_sim_path(sbp->sim),
2319 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2320 #if __FreeBSD_version >= 500000
2326 size_mb = ccg->volume_size
2327 / ((1024L * 1024L) / ccg->block_size);
2329 if (size_mb >= 1024 && extended) {
2331 ccg->secs_per_track = 63;
2334 ccg->secs_per_track = 32;
2336 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2337 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2338 ccb->ccb_h.status = CAM_REQ_CMP;
2342 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
2346 printf("%s:%d:XPT_RESET_BUS: \n",
2347 device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2350 ccb->ccb_h.status = CAM_REQ_INVALID;
2354 case XPT_PATH_INQ: /* Path routing inquiry */
2356 struct ccb_pathinq *cpi = &ccb->cpi;
2359 printf("%s:%d:%d XPT_PATH_INQ:.\n",
2360 device_get_nameunit(sbp->fd.dev),
2361 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2363 cpi->version_num = 1; /* XXX??? */
2364 cpi->hba_inquiry = PI_TAG_ABLE;
2365 cpi->target_sprt = 0;
2366 cpi->hba_misc = PIM_NOBUSRESET;
2367 cpi->hba_eng_cnt = 0;
2368 cpi->max_target = SBP_NUM_TARGETS - 1;
2369 cpi->max_lun = SBP_NUM_LUNS - 1;
2370 cpi->initiator_id = SBP_INITIATOR;
2371 cpi->bus_id = sim->bus_id;
2372 cpi->base_transfer_speed = 400 * 1000 / 8;
2373 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2374 strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2375 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2376 cpi->unit_number = sim->unit_number;
2378 cpi->ccb_h.status = CAM_REQ_CMP;
2382 case XPT_GET_TRAN_SETTINGS:
2384 struct ccb_trans_settings *cts = &ccb->cts;
2386 printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2387 device_get_nameunit(sbp->fd.dev),
2388 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2390 /* Enable disconnect and tagged queuing */
2391 cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2392 cts->flags = CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB;
2394 cts->ccb_h.status = CAM_REQ_CMP;
2399 ccb->ccb_h.status = CAM_UA_ABORT;
2402 case XPT_SET_TRAN_SETTINGS:
2405 ccb->ccb_h.status = CAM_REQ_INVALID;
2413 sbp_action(struct cam_sim *sim, union ccb *ccb)
2418 sbp_action1(sim, ccb);
2423 sbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error)
2426 struct sbp_ocb *ocb;
2427 struct sbp_ocb *prev;
2428 bus_dma_segment_t *s;
2431 printf("sbp_execute_ocb: error=%d\n", error);
2433 ocb = (struct sbp_ocb *)arg;
2436 printf("sbp_execute_ocb: seg %d", seg);
2437 for (i = 0; i < seg; i++)
2438 #if __FreeBSD_version >= 500000
2439 printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2440 (uintmax_t)segments[i].ds_len);
2442 printf(", %x:%d", segments[i].ds_addr, segments[i].ds_len);
2448 /* direct pointer */
2450 if (s->ds_len > SBP_SEG_MAX)
2451 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2452 ocb->orb[3] = htonl(s->ds_addr);
2453 ocb->orb[4] |= htonl(s->ds_len);
2454 } else if(seg > 1) {
2456 for (i = 0; i < seg; i++) {
2459 /* XXX LSI Logic "< 16 byte" bug might be hit */
2461 printf("sbp_execute_ocb: warning, "
2462 #if __FreeBSD_version >= 500000
2463 "segment length(%zd) is less than 16."
2465 "segment length(%d) is less than 16."
2467 "(seg=%d/%d)\n", s->ds_len, i+1, seg);
2469 if (s->ds_len > SBP_SEG_MAX)
2470 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2471 ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2472 ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2474 ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2478 bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2479 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2480 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2481 prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2482 fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2484 sbp_orb_pointer(ocb->sdev, ocb);
2488 sbp_poll(struct cam_sim *sim)
2490 /* should call fwohci_intr? */
2493 static struct sbp_ocb *
2494 sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2496 struct sbp_ocb *ocb;
2497 struct sbp_ocb *next;
2498 int s = splfw(), order = 0;
2501 for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2502 next = STAILQ_NEXT(ocb, ocb);
2505 sbp_show_sdev_info(sdev, 2);
2506 #if __FreeBSD_version >= 500000
2507 printf("orb: 0x%jx next: 0x%x, flags %x\n",
2508 (uintmax_t)ocb->bus_addr,
2510 printf("orb: 0x%x next: 0x%lx, flags %x\n",
2513 ntohl(ocb->orb[1]), flags);
2515 if (OCB_MATCH(ocb, sbp_status)) {
2517 STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2518 if (ocb->ccb != NULL)
2519 untimeout(sbp_timeout, (caddr_t)ocb,
2520 ocb->ccb->ccb_h.timeout_ch);
2521 if (ntohl(ocb->orb[4]) & 0xffff) {
2522 bus_dmamap_sync(sdev->target->sbp->dmat,
2524 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2525 BUS_DMASYNC_POSTREAD :
2526 BUS_DMASYNC_POSTWRITE);
2527 bus_dmamap_unload(sdev->target->sbp->dmat,
2530 if (next != NULL && sbp_status->src == 1)
2531 sbp_orb_pointer(sdev, next);
2538 if (ocb && order > 0) {
2539 sbp_show_sdev_info(sdev, 2);
2540 printf("unordered execution order:%d\n", order);
2546 static struct sbp_ocb *
2547 sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2550 struct sbp_ocb *prev;
2553 sbp_show_sdev_info(sdev, 2);
2554 #if __FreeBSD_version >= 500000
2555 printf("sbp_enqueue_ocb orb=0x%jx in physical memory\n",
2556 (uintmax_t)ocb->bus_addr);
2558 printf("sbp_enqueue_ocb orb=0x%x in physical memory\n", ocb->bus_addr);
2561 prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2562 STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2564 if (ocb->ccb != NULL)
2565 ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb,
2566 (ocb->ccb->ccb_h.timeout * hz) / 1000);
2568 if (prev != NULL ) {
2570 #if __FreeBSD_version >= 500000
2571 printf("linking chain 0x%jx -> 0x%jx\n",
2572 (uintmax_t)prev->bus_addr, (uintmax_t)ocb->bus_addr);
2574 printf("linking chain 0x%x -> 0x%x\n", prev->bus_addr, ocb->bus_addr);
2577 prev->orb[1] = htonl(ocb->bus_addr);
2585 static struct sbp_ocb *
2586 sbp_get_ocb(struct sbp_dev *sdev)
2588 struct sbp_ocb *ocb;
2590 ocb = STAILQ_FIRST(&sdev->free_ocbs);
2592 printf("ocb shortage!!!\n");
2595 STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2602 sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2606 STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2610 sbp_abort_ocb(struct sbp_ocb *ocb, int status)
2612 struct sbp_dev *sdev;
2616 sbp_show_sdev_info(sdev, 2);
2617 #if __FreeBSD_version >= 500000
2618 printf("sbp_abort_ocb 0x%jx\n", (uintmax_t)ocb->bus_addr);
2620 printf("sbp_abort_ocb 0x%x\n", ocb->bus_addr);
2624 if (ocb->ccb != NULL)
2625 sbp_print_scsi_cmd(ocb);
2627 if (ntohl(ocb->orb[4]) & 0xffff) {
2628 bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
2629 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2630 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2631 bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
2633 if (ocb->ccb != NULL) {
2634 untimeout(sbp_timeout, (caddr_t)ocb,
2635 ocb->ccb->ccb_h.timeout_ch);
2636 ocb->ccb->ccb_h.status = status;
2639 sbp_free_ocb(sdev, ocb);
2643 sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2646 struct sbp_ocb *ocb, *next;
2647 STAILQ_HEAD(, sbp_ocb) temp;
2651 bcopy(&sdev->ocbs, &temp, sizeof(temp));
2652 STAILQ_INIT(&sdev->ocbs);
2653 for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2654 next = STAILQ_NEXT(ocb, ocb);
2655 sbp_abort_ocb(ocb, status);
2661 static devclass_t sbp_devclass;
2663 static device_method_t sbp_methods[] = {
2664 /* device interface */
2665 DEVMETHOD(device_identify, sbp_identify),
2666 DEVMETHOD(device_probe, sbp_probe),
2667 DEVMETHOD(device_attach, sbp_attach),
2668 DEVMETHOD(device_detach, sbp_detach),
2669 DEVMETHOD(device_shutdown, sbp_shutdown),
2674 static driver_t sbp_driver = {
2677 sizeof(struct sbp_softc),
2680 DECLARE_DUMMY_MODULE(sbp);
2681 DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2682 MODULE_VERSION(sbp, 1);
2683 MODULE_DEPEND(sbp, firewire, 1, 1, 1);
2684 MODULE_DEPEND(sbp, cam, 1, 1, 1);