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.13 2005/02/17 13:59:35 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) {
481 * note: realloc() does not support M_ZERO. We must zero
482 * the extended region manually.
484 newluns = realloc(target->luns,
485 sizeof(struct sbp_dev *) * maxlun,
488 if (maxlun > target->num_lun) {
489 bzero(&newluns[target->num_lun],
490 sizeof(struct sbp_dev *) *
491 (maxlun - target->num_lun));
493 target->luns = newluns;
494 target->num_lun = maxlun;
497 crom_init_context(&cc, target->fwdev->csrrom);
498 while (cc.depth >= 0) {
501 reg = crom_search_key(&cc, CROM_LUN);
504 lun = reg->val & 0xffff;
505 if (lun >= SBP_NUM_LUNS) {
506 printf("too large lun %d\n", lun);
510 sdev = target->luns[lun];
512 sdev = malloc(sizeof(struct sbp_dev),
513 M_SBP, M_WAITOK | M_ZERO);
514 target->luns[lun] = sdev;
516 sdev->target = target;
517 STAILQ_INIT(&sdev->ocbs);
518 CALLOUT_INIT(&sdev->login_callout);
519 sdev->status = SBP_DEV_RESET;
522 sdev->flags |= VALID_LUN;
523 sdev->type = (reg->val & 0xff0000) >> 16;
528 fwdma_malloc(sbp->fd.fc,
529 /* alignment */ sizeof(u_int32_t),
530 SBP_DMA_SIZE, &sdev->dma, BUS_DMA_NOWAIT);
531 if (sdev->dma.v_addr == NULL) {
532 printf("%s: dma space allocation failed\n",
535 target->luns[lun] = NULL;
538 sdev->login = (struct sbp_login_res *) sdev->dma.v_addr;
539 sdev->ocb = (struct sbp_ocb *)
540 ((char *)sdev->dma.v_addr + SBP_LOGIN_SIZE);
541 bzero((char *)sdev->ocb,
542 sizeof (struct sbp_ocb) * SBP_QUEUE_LEN);
544 STAILQ_INIT(&sdev->free_ocbs);
545 for (i = 0; i < SBP_QUEUE_LEN; i++) {
548 ocb->bus_addr = sdev->dma.bus_addr
550 + sizeof(struct sbp_ocb) * i
551 + offsetof(struct sbp_ocb, orb[0]);
552 if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
553 printf("sbp_attach: cannot create dmamap\n");
557 sbp_free_ocb(sdev, ocb);
563 for (lun = 0; lun < target->num_lun; lun ++) {
564 sdev = target->luns[lun];
565 if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) {
566 sbp_cam_detach_sdev(sdev);
568 target->luns[lun] = NULL;
573 static struct sbp_target *
574 sbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
577 struct sbp_target *target;
578 struct crom_context cc;
582 printf("sbp_alloc_target\n");
584 i = sbp_new_target(sbp, fwdev);
586 device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n");
590 target = &sbp->targets[i];
592 target->fwdev = fwdev;
593 target->target_id = i;
594 /* XXX we may want to reload mgm port after each bus reset */
595 /* XXX there might be multiple management agents */
596 crom_init_context(&cc, target->fwdev->csrrom);
597 reg = crom_search_key(&cc, CROM_MGM);
598 if (reg == NULL || reg->val == 0) {
599 printf("NULL management address\n");
600 target->fwdev = NULL;
603 target->mgm_hi = 0xffff;
604 target->mgm_lo = 0xf0000000 | (reg->val << 2);
605 target->mgm_ocb_cur = NULL;
607 printf("target:%d mgm_port: %x\n", i, target->mgm_lo);
609 STAILQ_INIT(&target->xferlist);
611 STAILQ_INIT(&target->mgm_ocb_queue);
612 CALLOUT_INIT(&target->mgm_ocb_timeout);
613 CALLOUT_INIT(&target->scan_callout);
621 sbp_probe_lun(struct sbp_dev *sdev)
623 struct fw_device *fwdev;
624 struct crom_context c, *cc = &c;
627 bzero(sdev->vendor, sizeof(sdev->vendor));
628 bzero(sdev->product, sizeof(sdev->product));
630 fwdev = sdev->target->fwdev;
631 crom_init_context(cc, fwdev->csrrom);
632 /* get vendor string */
633 crom_search_key(cc, CSRKEY_VENDOR);
635 crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor));
636 /* skip to the unit directory for SBP-2 */
637 while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) {
638 if (reg->val == CSRVAL_T10SBP2)
642 /* get firmware revision */
643 reg = crom_search_key(cc, CSRKEY_FIRM_VER);
645 snprintf(sdev->revision, sizeof(sdev->revision),
647 /* get product string */
648 crom_search_key(cc, CSRKEY_MODEL);
650 crom_parse_text(cc, sdev->product, sizeof(sdev->product));
654 sbp_login_callout(void *arg)
656 struct sbp_dev *sdev = (struct sbp_dev *)arg;
657 sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
661 sbp_login(struct sbp_dev *sdev)
663 struct timeval delta;
668 timevalsub(&delta, &sdev->target->sbp->last_busreset);
669 t.tv_sec = login_delay / 1000;
670 t.tv_usec = (login_delay % 1000) * 1000;
671 timevalsub(&t, &delta);
672 if (t.tv_sec >= 0 && t.tv_usec > 0)
673 ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000;
675 printf("%s: sec = %ld usec = %ld ticks = %d\n", __func__,
676 t.tv_sec, t.tv_usec, ticks);
678 callout_reset(&sdev->login_callout, ticks,
679 sbp_login_callout, (void *)(sdev));
682 #define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
683 && crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
686 sbp_probe_target(void *arg)
688 struct sbp_target *target = (struct sbp_target *)arg;
689 struct sbp_softc *sbp;
690 struct sbp_dev *sdev;
691 struct firewire_comm *fc;
694 alive = SBP_FWDEV_ALIVE(target->fwdev);
696 printf("sbp_probe_target %d\n", target->target_id);
698 printf("not alive\n");
702 fc = target->sbp->fd.fc;
703 sbp_alloc_lun(target);
705 /* XXX untimeout mgm_ocb and dequeue */
706 for (i=0; i < target->num_lun; i++) {
707 sdev = target->luns[i];
710 if (alive && (sdev->status != SBP_DEV_DEAD)) {
711 if (sdev->path != NULL) {
712 xpt_freeze_devq(sdev->path, 1);
717 sbp_show_sdev_info(sdev,
718 (sdev->status == SBP_DEV_RESET));
721 sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
722 switch (sdev->status) {
724 /* new or revived target */
728 case SBP_DEV_TOATTACH:
730 case SBP_DEV_ATTACHED:
733 sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
737 switch (sdev->status) {
738 case SBP_DEV_ATTACHED:
740 /* the device has gone */
741 sbp_show_sdev_info(sdev, 2);
742 printf("lost target\n");
745 xpt_freeze_devq(sdev->path, 1);
748 sdev->status = SBP_DEV_RETRY;
749 sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
752 case SBP_DEV_TOATTACH:
753 sdev->status = SBP_DEV_RESET;
765 sbp_post_busreset(void *arg)
767 struct sbp_softc *sbp;
769 sbp = (struct sbp_softc *)arg;
771 printf("sbp_post_busreset\n");
773 if ((sbp->sim->flags & SIMQ_FREEZED) == 0) {
774 xpt_freeze_simq(sbp->sim, /*count*/1);
775 sbp->sim->flags |= SIMQ_FREEZED;
777 microtime(&sbp->last_busreset);
781 sbp_post_explore(void *arg)
783 struct sbp_softc *sbp = (struct sbp_softc *)arg;
784 struct sbp_target *target;
785 struct fw_device *fwdev;
789 printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
796 * XXX don't let CAM the bus rest.
797 * CAM tries to do something with freezed (DEV_RETRY) devices.
799 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
802 /* Gabage Collection */
803 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
804 target = &sbp->targets[i];
805 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
806 if (target->fwdev == NULL || target->fwdev == fwdev)
809 /* device has removed in lower driver */
810 sbp_cam_detach_target(target);
811 sbp_free_target(target);
814 /* traverse device list */
815 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
817 printf("sbp_post_explore: EUI:%08x%08x ",
818 fwdev->eui.hi, fwdev->eui.lo);
819 if (fwdev->status != FWDEVATTACHED)
820 printf("not attached, state=%d.\n", fwdev->status);
822 printf("attached\n");
824 alive = SBP_FWDEV_ALIVE(fwdev);
825 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
826 target = &sbp->targets[i];
827 if(target->fwdev == fwdev ) {
832 if(i == SBP_NUM_TARGETS){
835 target = sbp_alloc_target(sbp, fwdev);
842 sbp_probe_target((void *)target);
843 if (target->num_lun == 0)
844 sbp_free_target(target);
846 xpt_release_simq(sbp->sim, /*run queue*/TRUE);
847 sbp->sim->flags &= ~SIMQ_FREEZED;
852 sbp_loginres_callback(struct fw_xfer *xfer){
854 struct sbp_dev *sdev;
855 sdev = (struct sbp_dev *)xfer->sc;
857 sbp_show_sdev_info(sdev, 2);
858 printf("sbp_loginres_callback\n");
862 STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link);
869 sbp_xfer_free(struct fw_xfer *xfer)
871 struct sbp_dev *sdev;
874 sdev = (struct sbp_dev *)xfer->sc;
875 fw_xfer_unload(xfer);
877 STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
882 sbp_reset_start_callback(struct fw_xfer *xfer)
884 struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
885 struct sbp_target *target = sdev->target;
888 if (xfer->resp != 0) {
889 sbp_show_sdev_info(sdev, 2);
890 printf("sbp_reset_start failed: resp=%d\n", xfer->resp);
893 for (i = 0; i < target->num_lun; i++) {
894 tsdev = target->luns[i];
895 if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN)
901 sbp_reset_start(struct sbp_dev *sdev)
903 struct fw_xfer *xfer;
907 sbp_show_sdev_info(sdev, 2);
908 printf("sbp_reset_start\n");
911 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
912 xfer->act.hand = sbp_reset_start_callback;
913 fp = &xfer->send.hdr;
914 fp->mode.wreqq.dest_hi = 0xffff;
915 fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
916 fp->mode.wreqq.data = htonl(0xf);
917 fw_asyreq(xfer->fc, -1, xfer);
921 sbp_mgm_callback(struct fw_xfer *xfer)
923 struct sbp_dev *sdev;
926 sdev = (struct sbp_dev *)xfer->sc;
929 sbp_show_sdev_info(sdev, 2);
930 printf("sbp_mgm_callback\n");
936 sbp_show_sdev_info(sdev, 2);
937 printf("management ORB failed(%d) ... RESET_START\n", resp);
938 sbp_reset_start(sdev);
944 static struct sbp_dev *
945 sbp_next_dev(struct sbp_target *target, int lun)
947 struct sbp_dev **sdevp;
950 for (i = lun, sdevp = &target->luns[lun]; i < target->num_lun;
952 if (*sdevp != NULL && (*sdevp)->status == SBP_DEV_PROBE)
959 sbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb)
961 struct sbp_target *target;
962 struct sbp_dev *sdev;
964 sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
965 target = sdev->target;
967 sbp_show_sdev_info(sdev, 2);
968 printf("sbp_cam_scan_lun\n");
970 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
971 sdev->status = SBP_DEV_ATTACHED;
973 sbp_show_sdev_info(sdev, 2);
974 printf("scan failed\n");
976 sdev = sbp_next_dev(target, sdev->lun_id + 1);
982 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
983 ccb->ccb_h.ccb_sdev_ptr = sdev;
985 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
990 sbp_cam_scan_target(void *arg)
992 struct sbp_target *target = (struct sbp_target *)arg;
993 struct sbp_dev *sdev;
996 sdev = sbp_next_dev(target, 0);
998 printf("sbp_cam_scan_target: nothing to do for target%d\n",
1003 sbp_show_sdev_info(sdev, 2);
1004 printf("sbp_cam_scan_target\n");
1006 ccb = malloc(sizeof(union ccb), M_SBP, M_WAITOK | M_ZERO);
1007 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
1008 ccb->ccb_h.func_code = XPT_SCAN_LUN;
1009 ccb->ccb_h.cbfcnp = sbp_cam_scan_lun;
1010 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1011 ccb->crcn.flags = CAM_FLAG_NONE;
1012 ccb->ccb_h.ccb_sdev_ptr = sdev;
1014 /* The scan is in progress now. */
1016 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1020 static __inline void
1021 sbp_scan_dev(struct sbp_dev *sdev)
1023 sdev->status = SBP_DEV_PROBE;
1024 callout_reset(&sdev->target->scan_callout, scan_delay * hz / 1000,
1025 sbp_cam_scan_target, (void *)sdev->target);
1029 sbp_do_attach(struct fw_xfer *xfer)
1031 struct sbp_dev *sdev;
1032 struct sbp_target *target;
1033 struct sbp_softc *sbp;
1035 sdev = (struct sbp_dev *)xfer->sc;
1036 target = sdev->target;
1039 sbp_show_sdev_info(sdev, 2);
1040 printf("sbp_do_attach\n");
1042 sbp_xfer_free(xfer);
1044 if (sdev->path == NULL)
1045 xpt_create_path(&sdev->path, xpt_periph,
1046 cam_sim_path(target->sbp->sim),
1047 target->target_id, sdev->lun_id);
1050 * Let CAM scan the bus if we are in the boot process.
1051 * XXX xpt_scan_bus cannot detect LUN larger than 0
1052 * if LUN 0 doesn't exists.
1055 sdev->status = SBP_DEV_ATTACHED;
1064 sbp_agent_reset_callback(struct fw_xfer *xfer)
1066 struct sbp_dev *sdev;
1068 sdev = (struct sbp_dev *)xfer->sc;
1070 sbp_show_sdev_info(sdev, 2);
1071 printf("%s\n", __func__);
1073 if (xfer->resp != 0) {
1074 sbp_show_sdev_info(sdev, 2);
1075 printf("%s: resp=%d\n", __func__, xfer->resp);
1078 sbp_xfer_free(xfer);
1080 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1086 sbp_agent_reset(struct sbp_dev *sdev)
1088 struct fw_xfer *xfer;
1092 sbp_show_sdev_info(sdev, 2);
1093 printf("sbp_agent_reset\n");
1095 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1098 if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE)
1099 xfer->act.hand = sbp_agent_reset_callback;
1101 xfer->act.hand = sbp_do_attach;
1102 fp = &xfer->send.hdr;
1103 fp->mode.wreqq.data = htonl(0xf);
1104 fw_asyreq(xfer->fc, -1, xfer);
1105 sbp_abort_all_ocbs(sdev, CAM_BDR_SENT);
1109 sbp_busy_timeout_callback(struct fw_xfer *xfer)
1111 struct sbp_dev *sdev;
1113 sdev = (struct sbp_dev *)xfer->sc;
1115 sbp_show_sdev_info(sdev, 2);
1116 printf("sbp_busy_timeout_callback\n");
1118 sbp_xfer_free(xfer);
1119 sbp_agent_reset(sdev);
1123 sbp_busy_timeout(struct sbp_dev *sdev)
1126 struct fw_xfer *xfer;
1128 sbp_show_sdev_info(sdev, 2);
1129 printf("sbp_busy_timeout\n");
1131 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1133 xfer->act.hand = sbp_busy_timeout_callback;
1134 fp = &xfer->send.hdr;
1135 fp->mode.wreqq.dest_hi = 0xffff;
1136 fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1137 fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1138 fw_asyreq(xfer->fc, -1, xfer);
1142 sbp_orb_pointer_callback(struct fw_xfer *xfer)
1144 struct sbp_dev *sdev;
1145 sdev = (struct sbp_dev *)xfer->sc;
1148 sbp_show_sdev_info(sdev, 2);
1149 printf("%s\n", __func__);
1151 if (xfer->resp != 0) {
1153 printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1155 sbp_xfer_free(xfer);
1156 sdev->flags &= ~ORB_POINTER_ACTIVE;
1158 if ((sdev->flags & ORB_POINTER_NEED) != 0) {
1159 struct sbp_ocb *ocb;
1161 sdev->flags &= ~ORB_POINTER_NEED;
1162 ocb = STAILQ_FIRST(&sdev->ocbs);
1164 sbp_orb_pointer(sdev, ocb);
1170 sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1172 struct fw_xfer *xfer;
1175 sbp_show_sdev_info(sdev, 2);
1176 printf("%s: 0x%08x\n", __func__, (u_int32_t)ocb->bus_addr);
1179 if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) {
1181 printf("%s: orb pointer active\n", __func__);
1183 sdev->flags |= ORB_POINTER_NEED;
1187 sdev->flags |= ORB_POINTER_ACTIVE;
1188 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1191 xfer->act.hand = sbp_orb_pointer_callback;
1193 fp = &xfer->send.hdr;
1194 fp->mode.wreqb.len = 8;
1195 fp->mode.wreqb.extcode = 0;
1196 xfer->send.payload[0] =
1197 htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1198 xfer->send.payload[1] = htonl((u_int32_t)ocb->bus_addr);
1200 if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1201 sbp_xfer_free(xfer);
1202 ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1209 sbp_cmd_callback(struct fw_xfer *xfer)
1212 struct sbp_dev *sdev;
1213 sdev = (struct sbp_dev *)xfer->sc;
1214 sbp_show_sdev_info(sdev, 2);
1215 printf("sbp_cmd_callback\n");
1217 if (xfer->resp != 0) {
1219 printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1221 sbp_xfer_free(xfer);
1226 sbp_doorbell(struct sbp_dev *sdev)
1228 struct fw_xfer *xfer;
1231 sbp_show_sdev_info(sdev, 2);
1232 printf("sbp_doorbell\n");
1235 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1238 xfer->act.hand = sbp_cmd_callback;
1239 fp = (struct fw_pkt *)xfer->send.buf;
1240 fp->mode.wreqq.data = htonl(0xf);
1241 fw_asyreq(xfer->fc, -1, xfer);
1245 static struct fw_xfer *
1246 sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1248 struct fw_xfer *xfer;
1250 struct sbp_target *target;
1253 target = sdev->target;
1255 xfer = STAILQ_FIRST(&target->xferlist);
1257 if (target->n_xfer > 5 /* XXX */) {
1258 printf("sbp: no more xfer for this target\n");
1262 xfer = fw_xfer_alloc_buf(M_SBP, 8, 0);
1264 printf("sbp: fw_xfer_alloc_buf failed\n");
1270 printf("sbp: alloc %d xfer\n", target->n_xfer);
1273 STAILQ_REMOVE_HEAD(&target->xferlist, link);
1277 microtime(&xfer->tv);
1280 xfer->recv.pay_len = 0;
1281 xfer->send.spd = min(sdev->target->fwdev->speed, max_speed);
1282 xfer->fc = sdev->target->sbp->fd.fc;
1283 xfer->retry_req = fw_asybusy;
1286 if (tcode == FWTCODE_WREQB)
1287 xfer->send.pay_len = 8;
1289 xfer->send.pay_len = 0;
1291 xfer->sc = (caddr_t)sdev;
1292 fp = &xfer->send.hdr;
1293 fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
1294 fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
1295 fp->mode.wreqq.tlrt = 0;
1296 fp->mode.wreqq.tcode = tcode;
1297 fp->mode.wreqq.pri = 0;
1298 fp->mode.wreqq.dst = FWLOCALBUS | sdev->target->fwdev->dst;
1305 sbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1307 struct fw_xfer *xfer;
1309 struct sbp_ocb *ocb;
1310 struct sbp_target *target;
1313 target = sdev->target;
1314 nid = target->sbp->fd.fc->nodeid | FWLOCALBUS;
1317 if (func == ORB_FUN_RUNQUEUE) {
1318 ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1319 if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1323 STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1326 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
1331 ocb->flags = OCB_ACT_MGM;
1334 bzero((void *)ocb->orb, sizeof(ocb->orb));
1335 ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1336 ocb->orb[7] = htonl(SBP_DEV2ADDR(target->target_id, sdev->lun_id));
1339 sbp_show_sdev_info(sdev, 2);
1340 printf("%s\n", orb_fun_name[(func>>16)&0xf]);
1344 ocb->orb[0] = ocb->orb[1] = 0; /* password */
1345 ocb->orb[2] = htonl(nid << 16);
1346 ocb->orb[3] = htonl(sdev->dma.bus_addr);
1347 ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id);
1349 ocb->orb[4] |= htonl(ORB_EXV);
1350 ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
1351 fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD);
1354 ocb->orb[0] = htonl((0 << 16) | 0);
1355 ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
1362 ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
1366 if (target->mgm_ocb_cur != NULL) {
1367 /* there is a standing ORB */
1368 STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1373 target->mgm_ocb_cur = ocb;
1376 callout_reset(&target->mgm_ocb_timeout, 5*hz,
1377 sbp_mgm_timeout, (caddr_t)ocb);
1378 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1382 xfer->act.hand = sbp_mgm_callback;
1384 fp = &xfer->send.hdr;
1385 fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
1386 fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
1387 fp->mode.wreqb.len = 8;
1388 fp->mode.wreqb.extcode = 0;
1389 xfer->send.payload[0] = htonl(nid << 16);
1390 xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff);
1392 sbp_show_sdev_info(sdev, 2);
1393 printf("mgm orb: %08x\n", (u_int32_t)ocb->bus_addr);
1396 fw_asyreq(xfer->fc, -1, xfer);
1400 sbp_print_scsi_cmd(struct sbp_ocb *ocb)
1402 struct ccb_scsiio *csio;
1404 csio = &ocb->ccb->csio;
1405 printf("%s:%d:%d XPT_SCSI_IO: "
1406 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1408 "%db cmd/%db data/%db sense\n",
1409 device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1410 ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun,
1411 csio->cdb_io.cdb_bytes[0],
1412 csio->cdb_io.cdb_bytes[1],
1413 csio->cdb_io.cdb_bytes[2],
1414 csio->cdb_io.cdb_bytes[3],
1415 csio->cdb_io.cdb_bytes[4],
1416 csio->cdb_io.cdb_bytes[5],
1417 csio->cdb_io.cdb_bytes[6],
1418 csio->cdb_io.cdb_bytes[7],
1419 csio->cdb_io.cdb_bytes[8],
1420 csio->cdb_io.cdb_bytes[9],
1421 ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1422 csio->cdb_len, csio->dxfer_len,
1427 sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1429 struct sbp_cmd_status *sbp_cmd_status;
1430 struct scsi_sense_data *sense;
1432 sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1433 sense = &ocb->ccb->csio.sense_data;
1436 sbp_print_scsi_cmd(ocb);
1437 /* XXX need decode status */
1438 sbp_show_sdev_info(ocb->sdev, 2);
1439 printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1440 sbp_cmd_status->status,
1441 sbp_cmd_status->sfmt,
1442 sbp_cmd_status->valid,
1443 sbp_cmd_status->s_key,
1444 sbp_cmd_status->s_code,
1445 sbp_cmd_status->s_qlfr,
1450 switch (sbp_cmd_status->status) {
1451 case SCSI_STATUS_CHECK_COND:
1452 case SCSI_STATUS_BUSY:
1453 case SCSI_STATUS_CMD_TERMINATED:
1454 if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1455 sense->error_code = SSD_CURRENT_ERROR;
1457 sense->error_code = SSD_DEFERRED_ERROR;
1459 if(sbp_cmd_status->valid)
1460 sense->error_code |= SSD_ERRCODE_VALID;
1461 sense->flags = sbp_cmd_status->s_key;
1462 if(sbp_cmd_status->mark)
1463 sense->flags |= SSD_FILEMARK;
1464 if(sbp_cmd_status->eom)
1465 sense->flags |= SSD_EOM;
1466 if(sbp_cmd_status->ill_len)
1467 sense->flags |= SSD_ILI;
1469 bcopy(&sbp_cmd_status->info, &sense->info[0], 4);
1471 if (sbp_status->len <= 1)
1472 /* XXX not scsi status. shouldn't be happened */
1473 sense->extra_len = 0;
1474 else if (sbp_status->len <= 4)
1475 /* add_sense_code(_qual), info, cmd_spec_info */
1476 sense->extra_len = 6;
1478 /* fru, sense_key_spec */
1479 sense->extra_len = 10;
1481 bcopy(&sbp_cmd_status->cdb, &sense->cmd_spec_info[0], 4);
1483 sense->add_sense_code = sbp_cmd_status->s_code;
1484 sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1485 sense->fru = sbp_cmd_status->fru;
1487 bcopy(&sbp_cmd_status->s_keydep[0],
1488 &sense->sense_key_spec[0], 3);
1490 ocb->ccb->csio.scsi_status = sbp_cmd_status->status;;
1491 ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1492 | CAM_AUTOSNS_VALID;
1497 for( j = 0 ; j < 32 ; j+=8){
1498 printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1499 tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1500 tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1507 sbp_show_sdev_info(ocb->sdev, 2);
1508 printf("sbp_scsi_status: unknown scsi status 0x%x\n",
1509 sbp_cmd_status->status);
1514 sbp_fix_inq_data(struct sbp_ocb *ocb)
1517 struct sbp_dev *sdev;
1518 struct scsi_inquiry_data *inq;
1523 if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1526 sbp_show_sdev_info(sdev, 2);
1527 printf("sbp_fix_inq_data\n");
1529 inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1530 switch (SID_TYPE(inq)) {
1534 * XXX Convert Direct Access device to RBC.
1535 * I've never seen FireWire DA devices which support READ_6.
1537 if (SID_TYPE(inq) == T_DIRECT)
1538 inq->device |= T_RBC; /* T_DIRECT == 0 */
1542 /* enable tagged queuing */
1544 inq->flags |= SID_CmdQue;
1546 inq->flags &= ~SID_CmdQue;
1548 * Override vendor/product/revision information.
1549 * Some devices sometimes return strange strings.
1552 bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1553 bcopy(sdev->product, inq->product, sizeof(inq->product));
1554 bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1561 sbp_recv1(struct fw_xfer *xfer)
1567 struct sbp_softc *sbp;
1568 struct sbp_dev *sdev;
1569 struct sbp_ocb *ocb;
1570 struct sbp_login_res *login_res = NULL;
1571 struct sbp_status *sbp_status;
1572 struct sbp_target *target;
1573 int orb_fun, status_valid0, status_valid, t, l, reset_agent = 0;
1577 ld = xfer->recv.buf;
1578 printf("sbp %x %d %d %08x %08x %08x %08x\n",
1579 xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1580 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1581 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1583 sbp = (struct sbp_softc *)xfer->sc;
1584 if (xfer->resp != 0){
1585 printf("sbp_recv: xfer->resp = %d\n", xfer->resp);
1588 if (xfer->recv.payload == NULL){
1589 printf("sbp_recv: xfer->recv.payload == NULL\n");
1592 rfp = &xfer->recv.hdr;
1593 if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1594 printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1597 sbp_status = (struct sbp_status *)xfer->recv.payload;
1598 addr = rfp->mode.wreqb.dest_lo;
1600 printf("received address 0x%x\n", addr);
1602 t = SBP_ADDR2TRG(addr);
1603 if (t >= SBP_NUM_TARGETS) {
1604 device_printf(sbp->fd.dev,
1605 "sbp_recv1: invalid target %d\n", t);
1608 target = &sbp->targets[t];
1609 l = SBP_ADDR2LUN(addr);
1610 if (l >= target->num_lun || target->luns[l] == NULL) {
1611 device_printf(sbp->fd.dev,
1612 "sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1615 sdev = target->luns[l];
1618 switch (sbp_status->src) {
1621 /* check mgm_ocb_cur first */
1622 ocb = target->mgm_ocb_cur;
1624 if (OCB_MATCH(ocb, sbp_status)) {
1625 callout_stop(&target->mgm_ocb_timeout);
1626 target->mgm_ocb_cur = NULL;
1630 ocb = sbp_dequeue_ocb(sdev, sbp_status);
1632 sbp_show_sdev_info(sdev, 2);
1633 printf("No ocb(%x) on the queue\n",
1634 ntohl(sbp_status->orb_lo));
1639 sbp_show_sdev_info(sdev, 2);
1640 printf("unsolicit status received\n");
1643 sbp_show_sdev_info(sdev, 2);
1644 printf("unknown sbp_status->src\n");
1647 status_valid0 = (sbp_status->src < 2
1648 && sbp_status->resp == ORB_RES_CMPL
1649 && sbp_status->dead == 0);
1650 status_valid = (status_valid0 && sbp_status->status == 0);
1652 if (!status_valid0 || debug > 2){
1655 sbp_show_sdev_info(sdev, 2);
1656 printf("ORB status src:%x resp:%x dead:%x"
1657 " len:%x stat:%x orb:%x%08x\n",
1658 sbp_status->src, sbp_status->resp, sbp_status->dead,
1659 sbp_status->len, sbp_status->status,
1660 ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1662 sbp_show_sdev_info(sdev, 2);
1663 status = sbp_status->status;
1664 switch(sbp_status->resp) {
1666 if (status > MAX_ORB_STATUS0)
1667 printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1669 printf("%s\n", orb_status0[status]);
1672 printf("Obj: %s, Error: %s\n",
1673 orb_status1_object[(status>>6) & 3],
1674 orb_status1_serial_bus_error[status & 0xf]);
1677 printf("Illegal request\n");
1680 printf("Vendor dependent\n");
1683 printf("unknown respose code %d\n", sbp_status->resp);
1687 /* we have to reset the fetch agent if it's dead */
1688 if (sbp_status->dead) {
1690 xpt_freeze_devq(sdev->path, 1);
1699 switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1705 switch(ocb->flags) {
1707 orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1711 fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD);
1712 login_res = sdev->login;
1713 login_res->len = ntohs(login_res->len);
1714 login_res->id = ntohs(login_res->id);
1715 login_res->cmd_hi = ntohs(login_res->cmd_hi);
1716 login_res->cmd_lo = ntohl(login_res->cmd_lo);
1719 sbp_show_sdev_info(sdev, 2);
1720 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));
1722 sbp_busy_timeout(sdev);
1724 /* forgot logout? */
1725 sbp_show_sdev_info(sdev, 2);
1726 printf("login failed\n");
1727 sdev->status = SBP_DEV_RESET;
1731 login_res = sdev->login;
1734 sbp_show_sdev_info(sdev, 2);
1735 printf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo);
1738 if (sdev->status == SBP_DEV_ATTACHED)
1741 sbp_agent_reset(sdev);
1743 sdev->status = SBP_DEV_ATTACHED;
1744 sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL);
1747 /* reconnection hold time exceed? */
1749 sbp_show_sdev_info(sdev, 2);
1750 printf("reconnect failed\n");
1756 sdev->status = SBP_DEV_RESET;
1759 sbp_busy_timeout(sdev);
1764 sbp_agent_reset(sdev);
1767 sbp_show_sdev_info(sdev, 2);
1768 printf("unknown function %d\n", orb_fun);
1771 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
1775 if(ocb->ccb != NULL){
1779 ld = ocb->ccb->csio.data_ptr;
1780 if(ld != NULL && ocb->ccb->csio.dxfer_len != 0)
1781 printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]);
1783 printf("ptr NULL\n");
1784 printf("len %d\n", sbp_status->len);
1787 if(sbp_status->len > 1){
1788 sbp_scsi_status(sbp_status, ocb);
1790 if(sbp_status->resp != ORB_RES_CMPL){
1791 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1793 ccb->ccb_h.status = CAM_REQ_CMP;
1796 /* fix up inq data */
1797 if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1798 sbp_fix_inq_data(ocb);
1807 sbp_free_ocb(sdev, ocb);
1810 sbp_agent_reset(sdev);
1813 xfer->recv.pay_len = SBP_RECV_LEN;
1814 /* The received packet is usually small enough to be stored within
1815 * the buffer. In that case, the controller return ack_complete and
1816 * no respose is necessary.
1818 * XXX fwohci.c and firewire.c should inform event_code such as
1819 * ack_complete or ack_pending to upper driver.
1823 sfp = (struct fw_pkt *)xfer->send.buf;
1824 sfp->mode.wres.dst = rfp->mode.wreqb.src;
1825 xfer->dst = sfp->mode.wres.dst;
1826 xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1827 xfer->act.hand = sbp_loginres_callback;
1828 xfer->retry_req = fw_asybusy;
1830 sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1831 sfp->mode.wres.tcode = FWTCODE_WRES;
1832 sfp->mode.wres.rtcode = 0;
1833 sfp->mode.wres.pri = 0;
1835 fw_asyreq(xfer->fc, -1, xfer);
1838 STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1846 sbp_recv(struct fw_xfer *xfer)
1858 sbp_attach(device_t dev)
1860 struct sbp_softc *sbp;
1861 struct cam_devq *devq;
1862 struct fw_xfer *xfer;
1866 printf("sbp_attach (cold=%d)\n", cold);
1871 sbp = ((struct sbp_softc *)device_get_softc(dev));
1872 bzero(sbp, sizeof(struct sbp_softc));
1874 sbp->fd.fc = device_get_ivars(dev);
1877 max_speed = sbp->fd.fc->speed;
1879 error = bus_dma_tag_create(/*parent*/sbp->fd.fc->dmat,
1880 /* XXX shoud be 4 for sane backend? */
1883 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1884 /*highaddr*/BUS_SPACE_MAXADDR,
1885 /*filter*/NULL, /*filterarg*/NULL,
1886 /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1887 /*maxsegsz*/SBP_SEG_MAX,
1888 /*flags*/BUS_DMA_ALLOCNOW,
1889 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
1890 /*lockfunc*/busdma_lock_mutex,
1895 printf("sbp_attach: Could not allocate DMA tag "
1896 "- error %d\n", error);
1900 devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1904 for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1905 sbp->targets[i].fwdev = NULL;
1906 sbp->targets[i].luns = NULL;
1909 sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1910 device_get_unit(dev),
1912 /*tagged*/ SBP_QUEUE_LEN - 1,
1914 cam_simq_release(devq);
1915 if (sbp->sim == NULL)
1918 if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS)
1921 if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim),
1922 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1923 xpt_bus_deregister(cam_sim_path(sbp->sim));
1927 /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1928 sbp->fwb.start = ((u_int64_t)SBP_BIND_HI << 32) | SBP_DEV2ADDR(0, 0);
1929 sbp->fwb.end = sbp->fwb.start + 0xffff;
1930 sbp->fwb.act_type = FWACT_XFER;
1931 /* pre-allocate xfer */
1932 STAILQ_INIT(&sbp->fwb.xferlist);
1933 for (i = 0; i < SBP_NUM_OCB/2; i ++) {
1934 xfer = fw_xfer_alloc_buf(M_SBP,
1936 /* recv */SBP_RECV_LEN);
1937 xfer->act.hand = sbp_recv;
1939 xfer->fc = sbp->fd.fc;
1941 xfer->sc = (caddr_t)sbp;
1942 STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1944 fw_bindadd(sbp->fd.fc, &sbp->fwb);
1946 sbp->fd.post_busreset = sbp_post_busreset;
1947 sbp->fd.post_explore = sbp_post_explore;
1949 if (sbp->fd.fc->status != -1) {
1951 sbp_post_busreset((void *)sbp);
1952 sbp_post_explore((void *)sbp);
1955 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
1959 cam_sim_free(sbp->sim);
1964 sbp_logout_all(struct sbp_softc *sbp)
1966 struct sbp_target *target;
1967 struct sbp_dev *sdev;
1971 printf("sbp_logout_all\n");
1973 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
1974 target = &sbp->targets[i];
1975 if (target->luns == NULL)
1977 for (j = 0; j < target->num_lun; j++) {
1978 sdev = target->luns[j];
1981 callout_stop(&sdev->login_callout);
1982 if (sdev->status >= SBP_DEV_TOATTACH &&
1983 sdev->status <= SBP_DEV_ATTACHED)
1984 sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
1992 sbp_shutdown(device_t dev)
1994 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1996 sbp_logout_all(sbp);
2001 sbp_free_sdev(struct sbp_dev *sdev)
2007 for (i = 0; i < SBP_QUEUE_LEN; i++)
2008 bus_dmamap_destroy(sdev->target->sbp->dmat,
2009 sdev->ocb[i].dmamap);
2010 fwdma_free(sdev->target->sbp->fd.fc, &sdev->dma);
2015 sbp_free_target(struct sbp_target *target)
2017 struct sbp_softc *sbp;
2018 struct fw_xfer *xfer, *next;
2021 if (target->luns == NULL)
2023 callout_stop(&target->mgm_ocb_timeout);
2025 for (i = 0; i < target->num_lun; i++)
2026 sbp_free_sdev(target->luns[i]);
2028 for (xfer = STAILQ_FIRST(&target->xferlist);
2029 xfer != NULL; xfer = next) {
2030 next = STAILQ_NEXT(xfer, link);
2031 fw_xfer_free_buf(xfer);
2033 STAILQ_INIT(&target->xferlist);
2034 free(target->luns, M_SBP);
2035 target->num_lun = 0;;
2036 target->luns = NULL;
2037 target->fwdev = NULL;
2041 sbp_detach(device_t dev)
2043 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2044 struct firewire_comm *fc = sbp->fd.fc;
2045 struct fw_xfer *xfer, *next;
2049 printf("sbp_detach\n");
2052 for (i = 0; i < SBP_NUM_TARGETS; i ++)
2053 sbp_cam_detach_target(&sbp->targets[i]);
2054 xpt_async(AC_LOST_DEVICE, sbp->path, NULL);
2055 xpt_free_path(sbp->path);
2056 xpt_bus_deregister(cam_sim_path(sbp->sim));
2057 cam_sim_free(sbp->sim);
2059 sbp_logout_all(sbp);
2061 /* XXX wait for logout completion */
2062 tsleep(&i, FWPRI, "sbpdtc", hz/2);
2064 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++)
2065 sbp_free_target(&sbp->targets[i]);
2067 for (xfer = STAILQ_FIRST(&sbp->fwb.xferlist);
2068 xfer != NULL; xfer = next) {
2069 next = STAILQ_NEXT(xfer, link);
2070 fw_xfer_free_buf(xfer);
2072 STAILQ_INIT(&sbp->fwb.xferlist);
2073 fw_bindremove(fc, &sbp->fwb);
2075 bus_dma_tag_destroy(sbp->dmat);
2081 sbp_cam_detach_sdev(struct sbp_dev *sdev)
2085 if (sdev->status == SBP_DEV_DEAD)
2087 if (sdev->status == SBP_DEV_RESET)
2090 xpt_release_devq(sdev->path,
2091 sdev->freeze, TRUE);
2093 xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
2094 xpt_free_path(sdev->path);
2097 sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
2101 sbp_cam_detach_target(struct sbp_target *target)
2105 if (target->luns != NULL) {
2107 printf("sbp_detach_target %d\n", target->target_id);
2109 callout_stop(&target->scan_callout);
2110 for (i = 0; i < target->num_lun; i++)
2111 sbp_cam_detach_sdev(target->luns[i]);
2116 sbp_target_reset(struct sbp_dev *sdev, int method)
2119 struct sbp_target *target = sdev->target;
2120 struct sbp_dev *tsdev;
2122 for (i = 0; i < target->num_lun; i++) {
2123 tsdev = target->luns[i];
2126 if (tsdev->status == SBP_DEV_DEAD)
2128 if (tsdev->status == SBP_DEV_RESET)
2130 xpt_freeze_devq(tsdev->path, 1);
2132 sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT);
2134 tsdev->status = SBP_DEV_LOGIN;
2138 printf("target reset\n");
2139 sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2142 printf("reset start\n");
2143 sbp_reset_start(sdev);
2150 sbp_mgm_timeout(void *arg)
2152 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2153 struct sbp_dev *sdev = ocb->sdev;
2154 struct sbp_target *target = sdev->target;
2156 sbp_show_sdev_info(sdev, 2);
2157 printf("request timeout(mgm orb:0x%08x) ... ",
2158 (u_int32_t)ocb->bus_addr);
2159 target->mgm_ocb_cur = NULL;
2160 sbp_free_ocb(sdev, ocb);
2163 printf("run next request\n");
2164 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2167 printf("reset start\n");
2168 sbp_reset_start(sdev);
2173 sbp_timeout(void *arg)
2175 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2176 struct sbp_dev *sdev = ocb->sdev;
2178 sbp_show_sdev_info(sdev, 2);
2179 printf("request timeout(cmd orb:0x%08x) ... ",
2180 (u_int32_t)ocb->bus_addr);
2183 switch(sdev->timeout) {
2185 printf("agent reset\n");
2186 xpt_freeze_devq(sdev->path, 1);
2188 sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2189 sbp_agent_reset(sdev);
2193 sbp_target_reset(sdev, sdev->timeout - 1);
2198 sbp_cam_detach_target(target);
2199 if (target->luns != NULL)
2200 free(target->luns, M_SBP);
2201 target->num_lun = 0;;
2202 target->luns = NULL;
2203 target->fwdev = NULL;
2209 sbp_action1(struct cam_sim *sim, union ccb *ccb)
2212 struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2213 struct sbp_target *target = NULL;
2214 struct sbp_dev *sdev = NULL;
2216 /* target:lun -> sdev mapping */
2218 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2219 && ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2220 target = &sbp->targets[ccb->ccb_h.target_id];
2221 if (target->fwdev != NULL
2222 && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2223 && ccb->ccb_h.target_lun < target->num_lun) {
2224 sdev = target->luns[ccb->ccb_h.target_lun];
2225 if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED &&
2226 sdev->status != SBP_DEV_PROBE)
2233 printf("invalid target %d lun %d\n",
2234 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2237 switch (ccb->ccb_h.func_code) {
2240 case XPT_GET_TRAN_SETTINGS:
2241 case XPT_SET_TRAN_SETTINGS:
2242 case XPT_CALC_GEOMETRY:
2245 printf("%s:%d:%d:func_code 0x%04x: "
2246 "Invalid target (target needed)\n",
2247 device_get_nameunit(sbp->fd.dev),
2248 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2249 ccb->ccb_h.func_code);
2252 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2259 /* The opcodes sometimes aimed at a target (sc is valid),
2260 * sometimes aimed at the SIM (sc is invalid and target is
2261 * CAM_TARGET_WILDCARD)
2264 ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2266 printf("%s:%d:%d func_code 0x%04x: "
2267 "Invalid target (no wildcard)\n",
2268 device_get_nameunit(sbp->fd.dev),
2269 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2270 ccb->ccb_h.func_code);
2272 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2278 /* XXX Hm, we should check the input parameters */
2282 switch (ccb->ccb_h.func_code) {
2285 struct ccb_scsiio *csio;
2286 struct sbp_ocb *ocb;
2293 printf("%s:%d:%d XPT_SCSI_IO: "
2294 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2296 "%db cmd/%db data/%db sense\n",
2297 device_get_nameunit(sbp->fd.dev),
2298 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2299 csio->cdb_io.cdb_bytes[0],
2300 csio->cdb_io.cdb_bytes[1],
2301 csio->cdb_io.cdb_bytes[2],
2302 csio->cdb_io.cdb_bytes[3],
2303 csio->cdb_io.cdb_bytes[4],
2304 csio->cdb_io.cdb_bytes[5],
2305 csio->cdb_io.cdb_bytes[6],
2306 csio->cdb_io.cdb_bytes[7],
2307 csio->cdb_io.cdb_bytes[8],
2308 csio->cdb_io.cdb_bytes[9],
2309 ccb->ccb_h.flags & CAM_DIR_MASK,
2310 csio->cdb_len, csio->dxfer_len,
2314 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2319 /* if we are in probe stage, pass only probe commands */
2320 if (sdev->status == SBP_DEV_PROBE) {
2322 name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2323 printf("probe stage, periph name: %s\n", name);
2324 if (strcmp(name, "probe") != 0) {
2325 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2331 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
2332 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2337 ocb->flags = OCB_ACT_CMD;
2340 ccb->ccb_h.ccb_sdev_ptr = sdev;
2341 ocb->orb[0] = htonl(1 << 31);
2343 ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2344 ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2345 speed = min(target->fwdev->speed, max_speed);
2346 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2347 | ORB_CMD_MAXP(speed + 7));
2348 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
2349 ocb->orb[4] |= htonl(ORB_CMD_IN);
2352 if (csio->ccb_h.flags & CAM_SCATTER_VALID)
2353 printf("sbp: CAM_SCATTER_VALID\n");
2354 if (csio->ccb_h.flags & CAM_DATA_PHYS)
2355 printf("sbp: CAM_DATA_PHYS\n");
2357 if (csio->ccb_h.flags & CAM_CDB_POINTER)
2358 cdb = (void *)csio->cdb_io.cdb_ptr;
2360 cdb = (void *)&csio->cdb_io.cdb_bytes;
2361 bcopy(cdb, (void *)&ocb->orb[5], csio->cdb_len);
2363 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2364 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2366 if (ccb->csio.dxfer_len > 0) {
2370 error = bus_dmamap_load(/*dma tag*/sbp->dmat,
2371 /*dma map*/ocb->dmamap,
2373 ccb->csio.dxfer_len,
2379 printf("sbp: bus_dmamap_load error %d\n", error);
2381 sbp_execute_ocb(ocb, NULL, 0, 0);
2384 case XPT_CALC_GEOMETRY:
2386 struct ccb_calc_geometry *ccg;
2387 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2389 u_int32_t secs_per_cylinder;
2394 if (ccg->block_size == 0) {
2395 printf("sbp_action1: block_size is 0.\n");
2396 ccb->ccb_h.status = CAM_REQ_INVALID;
2401 printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2402 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2403 "Volume size = %d\n",
2405 "Volume size = %jd\n",
2407 device_get_nameunit(sbp->fd.dev),
2408 cam_sim_path(sbp->sim),
2409 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2410 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2416 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2417 size_mb = ccg->volume_size
2418 / ((1024L * 1024L) / ccg->block_size);
2420 if (size_mb > 1024 && extended) {
2422 ccg->secs_per_track = 63;
2425 ccg->secs_per_track = 32;
2427 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2428 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2429 ccb->ccb_h.status = CAM_REQ_CMP;
2431 cam_calc_geometry(ccg, /*extended*/1);
2436 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
2440 printf("%s:%d:XPT_RESET_BUS: \n",
2441 device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2444 ccb->ccb_h.status = CAM_REQ_INVALID;
2448 case XPT_PATH_INQ: /* Path routing inquiry */
2450 struct ccb_pathinq *cpi = &ccb->cpi;
2453 printf("%s:%d:%d XPT_PATH_INQ:.\n",
2454 device_get_nameunit(sbp->fd.dev),
2455 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2457 cpi->version_num = 1; /* XXX??? */
2458 cpi->hba_inquiry = PI_TAG_ABLE;
2459 cpi->target_sprt = 0;
2460 cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE;
2461 cpi->hba_eng_cnt = 0;
2462 cpi->max_target = SBP_NUM_TARGETS - 1;
2463 cpi->max_lun = SBP_NUM_LUNS - 1;
2464 cpi->initiator_id = SBP_INITIATOR;
2465 cpi->bus_id = sim->bus_id;
2466 cpi->base_transfer_speed = 400 * 1000 / 8;
2467 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2468 strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2469 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2470 cpi->unit_number = sim->unit_number;
2472 cpi->ccb_h.status = CAM_REQ_CMP;
2476 case XPT_GET_TRAN_SETTINGS:
2478 struct ccb_trans_settings *cts = &ccb->cts;
2480 printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2481 device_get_nameunit(sbp->fd.dev),
2482 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2484 /* Enable disconnect and tagged queuing */
2485 cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2486 cts->flags = CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB;
2488 cts->ccb_h.status = CAM_REQ_CMP;
2493 ccb->ccb_h.status = CAM_UA_ABORT;
2496 case XPT_SET_TRAN_SETTINGS:
2499 ccb->ccb_h.status = CAM_REQ_INVALID;
2507 sbp_action(struct cam_sim *sim, union ccb *ccb)
2512 sbp_action1(sim, ccb);
2517 sbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error)
2520 struct sbp_ocb *ocb;
2521 struct sbp_ocb *prev;
2522 bus_dma_segment_t *s;
2525 printf("sbp_execute_ocb: error=%d\n", error);
2527 ocb = (struct sbp_ocb *)arg;
2530 printf("sbp_execute_ocb: seg %d", seg);
2531 for (i = 0; i < seg; i++)
2532 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2533 printf(", %x:%d", segments[i].ds_addr, segments[i].ds_len);
2535 printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2536 (uintmax_t)segments[i].ds_len);
2542 /* direct pointer */
2544 if (s->ds_len > SBP_SEG_MAX)
2545 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2546 ocb->orb[3] = htonl(s->ds_addr);
2547 ocb->orb[4] |= htonl(s->ds_len);
2548 } else if(seg > 1) {
2550 for (i = 0; i < seg; i++) {
2553 /* XXX LSI Logic "< 16 byte" bug might be hit */
2555 printf("sbp_execute_ocb: warning, "
2556 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2557 "segment length(%d) is less than 16."
2559 "segment length(%zd) is less than 16."
2561 "(seg=%d/%d)\n", s->ds_len, i+1, seg);
2563 if (s->ds_len > SBP_SEG_MAX)
2564 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2565 ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2566 ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2568 ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2572 bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2573 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2574 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2575 prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2576 fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2577 if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) {
2578 ocb->sdev->flags &= ~ORB_LINK_DEAD;
2579 sbp_orb_pointer(ocb->sdev, ocb);
2584 sbp_poll(struct cam_sim *sim)
2586 struct sbp_softc *sbp;
2587 struct firewire_comm *fc;
2589 sbp = (struct sbp_softc *)sim->softc;
2592 fc->poll(fc, 0, -1);
2597 static struct sbp_ocb *
2598 sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2600 struct sbp_ocb *ocb;
2601 struct sbp_ocb *next;
2602 int s = splfw(), order = 0;
2606 sbp_show_sdev_info(sdev, 2);
2607 printf("%s: 0x%08x src %d\n",
2608 __func__, ntohl(sbp_status->orb_lo), sbp_status->src);
2610 for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2611 next = STAILQ_NEXT(ocb, ocb);
2613 if (OCB_MATCH(ocb, sbp_status)) {
2615 STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2616 if (ocb->ccb != NULL)
2617 callout_stop(&ocb->ccb->ccb_h.timeout_ch);
2618 if (ntohl(ocb->orb[4]) & 0xffff) {
2619 bus_dmamap_sync(sdev->target->sbp->dmat,
2621 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2622 BUS_DMASYNC_POSTREAD :
2623 BUS_DMASYNC_POSTWRITE);
2624 bus_dmamap_unload(sdev->target->sbp->dmat,
2627 if (sbp_status->src == SRC_NO_NEXT) {
2629 sbp_orb_pointer(sdev, next);
2630 else if (order > 0) {
2632 * Unordered execution
2633 * We need to send pointer for
2636 sdev->flags |= ORB_LINK_DEAD;
2645 if (ocb && order > 0) {
2646 sbp_show_sdev_info(sdev, 2);
2647 printf("unordered execution order:%d\n", order);
2653 static struct sbp_ocb *
2654 sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2657 struct sbp_ocb *prev;
2660 sbp_show_sdev_info(sdev, 2);
2661 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2662 printf("%s: 0x%08x\n", __func__, ocb->bus_addr);
2664 printf("%s: 0x%08jx\n", __func__, (uintmax_t)ocb->bus_addr);
2667 prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2668 STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2670 if (ocb->ccb != NULL)
2671 callout_reset(&ocb->ccb->ccb_h.timeout_ch,
2672 (ocb->ccb->ccb_h.timeout * hz) / 1000, sbp_timeout, ocb);
2676 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2677 printf("linking chain 0x%x -> 0x%x\n",
2678 prev->bus_addr, ocb->bus_addr);
2680 printf("linking chain 0x%jx -> 0x%jx\n",
2681 (uintmax_t)prev->bus_addr, (uintmax_t)ocb->bus_addr);
2684 prev->orb[1] = htonl(ocb->bus_addr);
2692 static struct sbp_ocb *
2693 sbp_get_ocb(struct sbp_dev *sdev)
2695 struct sbp_ocb *ocb;
2697 ocb = STAILQ_FIRST(&sdev->free_ocbs);
2699 printf("ocb shortage!!!\n");
2702 STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2709 sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2713 STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2717 sbp_abort_ocb(struct sbp_ocb *ocb, int status)
2719 struct sbp_dev *sdev;
2723 sbp_show_sdev_info(sdev, 2);
2724 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2725 printf("sbp_abort_ocb 0x%x\n", ocb->bus_addr);
2727 printf("sbp_abort_ocb 0x%jx\n", (uintmax_t)ocb->bus_addr);
2731 if (ocb->ccb != NULL)
2732 sbp_print_scsi_cmd(ocb);
2734 if (ntohl(ocb->orb[4]) & 0xffff) {
2735 bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
2736 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2737 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2738 bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
2740 if (ocb->ccb != NULL) {
2741 callout_stop(&ocb->ccb->ccb_h.timeout_ch);
2742 ocb->ccb->ccb_h.status = status;
2745 sbp_free_ocb(sdev, ocb);
2749 sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2752 struct sbp_ocb *ocb, *next;
2753 STAILQ_HEAD(, sbp_ocb) temp;
2757 bcopy(&sdev->ocbs, &temp, sizeof(temp));
2758 STAILQ_INIT(&sdev->ocbs);
2759 for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2760 next = STAILQ_NEXT(ocb, ocb);
2761 sbp_abort_ocb(ocb, status);
2767 static devclass_t sbp_devclass;
2769 static device_method_t sbp_methods[] = {
2770 /* device interface */
2771 DEVMETHOD(device_identify, sbp_identify),
2772 DEVMETHOD(device_probe, sbp_probe),
2773 DEVMETHOD(device_attach, sbp_attach),
2774 DEVMETHOD(device_detach, sbp_detach),
2775 DEVMETHOD(device_shutdown, sbp_shutdown),
2780 static driver_t sbp_driver = {
2783 sizeof(struct sbp_softc),
2786 DECLARE_DUMMY_MODULE(sbp);
2787 DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2788 MODULE_VERSION(sbp, 1);
2789 MODULE_DEPEND(sbp, firewire, 1, 1, 1);
2790 MODULE_DEPEND(sbp, cam, 1, 1, 1);