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.
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/module.h>
42 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
44 #include <machine/bus.h>
45 #include <sys/malloc.h>
47 #include <sys/mutex.h>
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_sim.h>
52 #include <cam/cam_xpt_sim.h>
53 #include <cam/cam_debug.h>
54 #include <cam/cam_periph.h>
55 #include <cam/scsi/scsi_all.h>
57 #include <dev/firewire/firewire.h>
58 #include <dev/firewire/firewirereg.h>
59 #include <dev/firewire/fwdma.h>
60 #include <dev/firewire/iec13213.h>
61 #include <dev/firewire/sbp.h>
63 #define ccb_sdev_ptr spriv_ptr0
64 #define ccb_sbp_ptr spriv_ptr1
66 #define SBP_NUM_TARGETS 8 /* MAX 64 */
68 * Scan_bus doesn't work for more than 8 LUNs
69 * because of CAM_SCSI2_MAXLUN in cam_xpt.c
71 #define SBP_NUM_LUNS 64
72 #define SBP_MAXPHYS MIN(MAXPHYS, (512*1024) /* 512KB */)
73 #define SBP_DMA_SIZE PAGE_SIZE
74 #define SBP_LOGIN_SIZE sizeof(struct sbp_login_res)
75 #define SBP_QUEUE_LEN ((SBP_DMA_SIZE - SBP_LOGIN_SIZE) / sizeof(struct sbp_ocb))
76 #define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS)
79 * STATUS FIFO addressing
81 *-----------------------
82 * 0- 1( 2): 0 (alignment)
86 * 32-47(16): SBP_BIND_HI
87 * 48-64(16): bus_id, node_id
89 #define SBP_BIND_HI 0x1
90 #define SBP_DEV2ADDR(t, l) \
91 (((u_int64_t)SBP_BIND_HI << 32) \
92 | (((l) & 0xff) << 8) \
93 | (((t) & 0x3f) << 2))
94 #define SBP_ADDR2TRG(a) (((a) >> 2) & 0x3f)
95 #define SBP_ADDR2LUN(a) (((a) >> 8) & 0xff)
96 #define SBP_INITIATOR 7
98 static char *orb_fun_name[] = {
102 static int debug = 0;
103 static int auto_login = 1;
104 static int max_speed = -1;
105 static int sbp_cold = 1;
106 static int ex_login = 1;
107 static int login_delay = 1000; /* msec */
108 static int scan_delay = 500; /* msec */
109 static int use_doorbell = 0;
110 static int sbp_tags = 0;
112 SYSCTL_DECL(_hw_firewire);
113 static SYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0,
115 SYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RWTUN, &debug, 0,
117 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RWTUN, &auto_login, 0,
118 "SBP perform login automatically");
119 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RWTUN, &max_speed, 0,
120 "SBP transfer max speed");
121 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, exclusive_login, CTLFLAG_RWTUN,
122 &ex_login, 0, "SBP enable exclusive login");
123 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, login_delay, CTLFLAG_RWTUN,
124 &login_delay, 0, "SBP login delay in msec");
125 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, scan_delay, CTLFLAG_RWTUN,
126 &scan_delay, 0, "SBP scan delay in msec");
127 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, use_doorbell, CTLFLAG_RWTUN,
128 &use_doorbell, 0, "SBP use doorbell request");
129 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, tags, CTLFLAG_RWTUN, &sbp_tags, 0,
130 "SBP tagged queuing support");
132 #define NEED_RESPONSE 0
134 #define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE)
135 #ifdef __sparc64__ /* iommu */
136 #define SBP_IND_MAX howmany(SBP_MAXPHYS, SBP_SEG_MAX)
138 #define SBP_IND_MAX howmany(SBP_MAXPHYS, PAGE_SIZE)
141 STAILQ_ENTRY(sbp_ocb) ocb;
145 #define IND_PTR_OFFSET (8*sizeof(uint32_t))
146 struct ind_ptr ind_ptr[SBP_IND_MAX];
147 struct sbp_dev *sdev;
148 int flags; /* XXX should be removed */
150 struct callout timer;
153 #define OCB_ACT_MGM 0
154 #define OCB_ACT_CMD 1
155 #define OCB_MATCH(o,s) ((o)->bus_addr == ntohl((s)->orb_lo))
158 #define SBP_DEV_RESET 0 /* accept login */
159 #define SBP_DEV_LOGIN 1 /* to login */
161 #define SBP_DEV_RECONN 2 /* to reconnect */
163 #define SBP_DEV_TOATTACH 3 /* to attach */
164 #define SBP_DEV_PROBE 4 /* scan lun */
165 #define SBP_DEV_ATTACHED 5 /* in operation */
166 #define SBP_DEV_DEAD 6 /* unavailable unit */
167 #define SBP_DEV_RETRY 7 /* unavailable unit */
173 #define ORB_LINK_DEAD (1 << 0)
174 #define VALID_LUN (1 << 1)
175 #define ORB_POINTER_ACTIVE (1 << 2)
176 #define ORB_POINTER_NEED (1 << 3)
177 #define ORB_DOORBELL_ACTIVE (1 << 4)
178 #define ORB_DOORBELL_NEED (1 << 5)
179 #define ORB_SHORTAGE (1 << 6)
181 struct cam_path *path;
182 struct sbp_target *target;
183 struct fwdma_alloc dma;
184 struct sbp_login_res *login;
185 struct callout login_callout;
187 STAILQ_HEAD(, sbp_ocb) ocbs;
188 STAILQ_HEAD(, sbp_ocb) free_ocbs;
189 struct sbp_ocb *last_ocb;
199 struct sbp_dev **luns;
200 struct sbp_softc *sbp;
201 struct fw_device *fwdev;
202 uint32_t mgm_hi, mgm_lo;
203 struct sbp_ocb *mgm_ocb_cur;
204 STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue;
205 struct callout mgm_ocb_timeout;
206 struct callout scan_callout;
207 STAILQ_HEAD(, fw_xfer) xferlist;
212 struct firewire_dev_comm fd;
214 struct cam_path *path;
215 struct sbp_target targets[SBP_NUM_TARGETS];
218 struct timeval last_busreset;
219 #define SIMQ_FREEZED 1
223 #define SBP_LOCK(sbp) mtx_lock(&(sbp)->mtx)
224 #define SBP_UNLOCK(sbp) mtx_unlock(&(sbp)->mtx)
225 #define SBP_LOCK_ASSERT(sbp) mtx_assert(&(sbp)->mtx, MA_OWNED)
227 static void sbp_post_explore (void *);
228 static void sbp_recv (struct fw_xfer *);
229 static void sbp_mgm_callback (struct fw_xfer *);
231 static void sbp_cmd_callback (struct fw_xfer *);
233 static void sbp_orb_pointer (struct sbp_dev *, struct sbp_ocb *);
234 static void sbp_doorbell(struct sbp_dev *);
235 static void sbp_execute_ocb (void *, bus_dma_segment_t *, int, int);
236 static void sbp_free_ocb (struct sbp_dev *, struct sbp_ocb *);
237 static void sbp_abort_ocb (struct sbp_ocb *, int);
238 static void sbp_abort_all_ocbs (struct sbp_dev *, int);
239 static struct fw_xfer * sbp_write_cmd (struct sbp_dev *, int, int);
240 static struct sbp_ocb * sbp_get_ocb (struct sbp_dev *);
241 static struct sbp_ocb * sbp_enqueue_ocb (struct sbp_dev *, struct sbp_ocb *);
242 static struct sbp_ocb * sbp_dequeue_ocb (struct sbp_dev *, struct sbp_status *);
243 static void sbp_cam_detach_sdev(struct sbp_dev *);
244 static void sbp_free_sdev(struct sbp_dev *);
245 static void sbp_cam_detach_target (struct sbp_target *);
246 static void sbp_free_target (struct sbp_target *);
247 static void sbp_mgm_timeout (void *arg);
248 static void sbp_timeout (void *arg);
249 static void sbp_mgm_orb (struct sbp_dev *, int, struct sbp_ocb *);
251 static MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire");
253 /* cam related functions */
254 static void sbp_action(struct cam_sim *sim, union ccb *ccb);
255 static void sbp_poll(struct cam_sim *sim);
256 static void sbp_cam_scan_lun(struct cam_periph *, union ccb *);
257 static void sbp_cam_scan_target(void *arg);
259 static char *orb_status0[] = {
260 /* 0 */ "No additional information to report",
261 /* 1 */ "Request type not supported",
262 /* 2 */ "Speed not supported",
263 /* 3 */ "Page size not supported",
264 /* 4 */ "Access denied",
265 /* 5 */ "Logical unit not supported",
266 /* 6 */ "Maximum payload too small",
267 /* 7 */ "Reserved for future standardization",
268 /* 8 */ "Resources unavailable",
269 /* 9 */ "Function rejected",
270 /* A */ "Login ID not recognized",
271 /* B */ "Dummy ORB completed",
272 /* C */ "Request aborted",
273 /* FF */ "Unspecified error"
274 #define MAX_ORB_STATUS0 0xd
277 static char *orb_status1_object[] = {
278 /* 0 */ "Operation request block (ORB)",
279 /* 1 */ "Data buffer",
280 /* 2 */ "Page table",
281 /* 3 */ "Unable to specify"
284 static char *orb_status1_serial_bus_error[] = {
285 /* 0 */ "Missing acknowledge",
286 /* 1 */ "Reserved; not to be used",
287 /* 2 */ "Time-out error",
288 /* 3 */ "Reserved; not to be used",
289 /* 4 */ "Busy retry limit exceeded(X)",
290 /* 5 */ "Busy retry limit exceeded(A)",
291 /* 6 */ "Busy retry limit exceeded(B)",
292 /* 7 */ "Reserved for future standardization",
293 /* 8 */ "Reserved for future standardization",
294 /* 9 */ "Reserved for future standardization",
295 /* A */ "Reserved for future standardization",
296 /* B */ "Tardy retry limit exceeded",
297 /* C */ "Conflict error",
298 /* D */ "Data error",
299 /* E */ "Type error",
300 /* F */ "Address error"
304 sbp_identify(driver_t *driver, device_t parent)
307 printf("sbp_identify\n");
310 if (device_find_child(parent, "sbp", -1) == NULL)
311 BUS_ADD_CHILD(parent, 0, "sbp", -1);
318 sbp_probe(device_t dev)
322 printf("sbp_probe\n");
325 device_set_desc(dev, "SBP-2/SCSI over FireWire");
336 * Display device characteristics on the console
339 sbp_show_sdev_info(struct sbp_dev *sdev)
341 struct fw_device *fwdev;
343 fwdev = sdev->target->fwdev;
344 device_printf(sdev->target->sbp->fd.dev,
345 "%s: %s: ordered:%d type:%d EUI:%08x%08x node:%d "
346 "speed:%d maxrec:%d\n",
349 (sdev->type & 0x40) >> 6,
357 device_printf(sdev->target->sbp->fd.dev,
358 "%s: %s '%s' '%s' '%s'\n",
371 /* Bus Target EUI64 */
373 {0, 2, {0x00018ea0, 0x01fd0154}}, /* Logitec HDD */
374 {0, 0, {0x00018ea6, 0x00100682}}, /* Logitec DVD */
375 {0, 1, {0x00d03200, 0xa412006a}}, /* Yano HDD */
381 sbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev)
383 int bus, i, target=-1;
384 char w[SBP_NUM_TARGETS];
387 bus = device_get_unit(sbp->fd.dev);
389 /* XXX wired-down configuration should be gotten from
390 tunable or device hint */
391 for (i = 0; wired[i].bus >= 0; i++) {
392 if (wired[i].bus == bus) {
393 w[wired[i].target] = 1;
394 if (wired[i].eui.hi == fwdev->eui.hi &&
395 wired[i].eui.lo == fwdev->eui.lo)
396 target = wired[i].target;
400 if (target < SBP_NUM_TARGETS &&
401 sbp->targets[target].fwdev == NULL)
403 device_printf(sbp->fd.dev,
404 "target %d is not free for %08x:%08x\n",
405 target, fwdev->eui.hi, fwdev->eui.lo);
408 /* non-wired target */
409 for (i = 0; i < SBP_NUM_TARGETS; i++)
410 if (sbp->targets[i].fwdev == NULL && w[i] == 0) {
419 sbp_alloc_lun(struct sbp_target *target)
421 struct crom_context cc;
423 struct sbp_dev *sdev, **newluns;
424 struct sbp_softc *sbp;
428 crom_init_context(&cc, target->fwdev->csrrom);
429 /* XXX shoud parse appropriate unit directories only */
431 while (cc.depth >= 0) {
432 reg = crom_search_key(&cc, CROM_LUN);
435 lun = reg->val & 0xffff;
437 printf("target %d lun %d found\n", target->target_id, lun);
444 device_printf(target->sbp->fd.dev, "%d no LUN found\n",
448 if (maxlun >= SBP_NUM_LUNS)
449 maxlun = SBP_NUM_LUNS;
451 /* Invalidiate stale devices */
452 for (lun = 0; lun < target->num_lun; lun++) {
453 sdev = target->luns[lun];
456 sdev->flags &= ~VALID_LUN;
459 sbp_cam_detach_sdev(sdev);
461 target->luns[lun] = NULL;
466 if (maxlun != target->num_lun) {
467 newluns = (struct sbp_dev **) realloc(target->luns,
468 sizeof(struct sbp_dev *) * maxlun,
469 M_SBP, M_NOWAIT | M_ZERO);
471 if (newluns == NULL) {
472 printf("%s: realloc failed\n", __func__);
473 newluns = target->luns;
474 maxlun = target->num_lun;
478 * We must zero the extended region for the case
479 * realloc() doesn't allocate new buffer.
481 if (maxlun > target->num_lun)
482 bzero(&newluns[target->num_lun],
483 sizeof(struct sbp_dev *) *
484 (maxlun - target->num_lun));
486 target->luns = newluns;
487 target->num_lun = maxlun;
490 crom_init_context(&cc, target->fwdev->csrrom);
491 while (cc.depth >= 0) {
494 reg = crom_search_key(&cc, CROM_LUN);
497 lun = reg->val & 0xffff;
498 if (lun >= SBP_NUM_LUNS) {
499 printf("too large lun %d\n", lun);
503 sdev = target->luns[lun];
505 sdev = malloc(sizeof(struct sbp_dev),
506 M_SBP, M_NOWAIT | M_ZERO);
508 printf("%s: malloc failed\n", __func__);
511 target->luns[lun] = sdev;
513 sdev->target = target;
514 STAILQ_INIT(&sdev->ocbs);
515 callout_init_mtx(&sdev->login_callout, &sbp->mtx, 0);
516 sdev->status = SBP_DEV_RESET;
518 snprintf(sdev->bustgtlun, 32, "%s:%d:%d",
519 device_get_nameunit(sdev->target->sbp->fd.dev),
520 sdev->target->target_id,
523 sdev->flags |= VALID_LUN;
524 sdev->type = (reg->val & 0xff0000) >> 16;
529 fwdma_malloc(sbp->fd.fc,
530 /* alignment */ sizeof(uint32_t),
531 SBP_DMA_SIZE, &sdev->dma, BUS_DMA_NOWAIT |
533 if (sdev->dma.v_addr == NULL) {
534 printf("%s: dma space allocation failed\n",
537 target->luns[lun] = NULL;
540 sdev->login = (struct sbp_login_res *) sdev->dma.v_addr;
541 sdev->ocb = (struct sbp_ocb *)
542 ((char *)sdev->dma.v_addr + SBP_LOGIN_SIZE);
543 bzero((char *)sdev->ocb,
544 sizeof(struct sbp_ocb) * SBP_QUEUE_LEN);
546 STAILQ_INIT(&sdev->free_ocbs);
547 for (i = 0; i < SBP_QUEUE_LEN; i++) {
550 ocb->bus_addr = sdev->dma.bus_addr
552 + sizeof(struct sbp_ocb) * i
553 + offsetof(struct sbp_ocb, orb[0]);
554 if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
555 printf("sbp_attach: cannot create dmamap\n");
559 callout_init_mtx(&ocb->timer, &sbp->mtx, 0);
561 sbp_free_ocb(sdev, ocb);
568 for (lun = 0; lun < target->num_lun; lun++) {
569 sdev = target->luns[lun];
570 if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) {
571 sbp_cam_detach_sdev(sdev);
573 target->luns[lun] = NULL;
578 static struct sbp_target *
579 sbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
582 struct sbp_target *target;
583 struct crom_context cc;
587 printf("sbp_alloc_target\n");
589 i = sbp_new_target(sbp, fwdev);
591 device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n");
595 target = &sbp->targets[i];
596 target->fwdev = fwdev;
597 target->target_id = i;
598 /* XXX we may want to reload mgm port after each bus reset */
599 /* XXX there might be multiple management agents */
600 crom_init_context(&cc, target->fwdev->csrrom);
601 reg = crom_search_key(&cc, CROM_MGM);
602 if (reg == NULL || reg->val == 0) {
603 printf("NULL management address\n");
604 target->fwdev = NULL;
607 target->mgm_hi = 0xffff;
608 target->mgm_lo = 0xf0000000 | (reg->val << 2);
609 target->mgm_ocb_cur = NULL;
611 printf("target:%d mgm_port: %x\n", i, target->mgm_lo);
613 STAILQ_INIT(&target->xferlist);
615 STAILQ_INIT(&target->mgm_ocb_queue);
616 callout_init_mtx(&target->mgm_ocb_timeout, &sbp->mtx, 0);
617 callout_init_mtx(&target->scan_callout, &sbp->mtx, 0);
625 sbp_probe_lun(struct sbp_dev *sdev)
627 struct fw_device *fwdev;
628 struct crom_context c, *cc = &c;
631 bzero(sdev->vendor, sizeof(sdev->vendor));
632 bzero(sdev->product, sizeof(sdev->product));
634 fwdev = sdev->target->fwdev;
635 crom_init_context(cc, fwdev->csrrom);
636 /* get vendor string */
637 crom_search_key(cc, CSRKEY_VENDOR);
639 crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor));
640 /* skip to the unit directory for SBP-2 */
641 while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) {
642 if (reg->val == CSRVAL_T10SBP2)
646 /* get firmware revision */
647 reg = crom_search_key(cc, CSRKEY_FIRM_VER);
649 snprintf(sdev->revision, sizeof(sdev->revision),
651 /* get product string */
652 crom_search_key(cc, CSRKEY_MODEL);
654 crom_parse_text(cc, sdev->product, sizeof(sdev->product));
658 sbp_login_callout(void *arg)
660 struct sbp_dev *sdev = (struct sbp_dev *)arg;
661 SBP_LOCK_ASSERT(sdev->target->sbp);
662 sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
666 sbp_login(struct sbp_dev *sdev)
668 struct timeval delta;
673 timevalsub(&delta, &sdev->target->sbp->last_busreset);
674 t.tv_sec = login_delay / 1000;
675 t.tv_usec = (login_delay % 1000) * 1000;
676 timevalsub(&t, &delta);
677 if (t.tv_sec >= 0 && t.tv_usec > 0)
678 ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000;
680 printf("%s: sec = %jd usec = %ld ticks = %d\n", __func__,
681 (intmax_t)t.tv_sec, t.tv_usec, ticks);
683 callout_reset(&sdev->login_callout, ticks,
684 sbp_login_callout, (void *)(sdev));
687 #define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
688 && crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
691 sbp_probe_target(struct sbp_target *target)
693 struct sbp_softc *sbp = target->sbp;
694 struct sbp_dev *sdev;
697 alive = SBP_FWDEV_ALIVE(target->fwdev);
699 device_printf(sbp->fd.dev, "%s %d%salive\n",
700 __func__, target->target_id,
701 (!alive) ? " not " : "");
704 sbp_alloc_lun(target);
706 /* XXX untimeout mgm_ocb and dequeue */
707 for (i=0; i < target->num_lun; i++) {
708 sdev = target->luns[i];
711 if (alive && (sdev->status != SBP_DEV_DEAD)) {
712 if (sdev->path != NULL) {
713 xpt_freeze_devq(sdev->path, 1);
717 sbp_show_sdev_info(sdev);
720 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 device_printf(sbp->fd.dev, "%s: lost target\n",
745 xpt_freeze_devq(sdev->path, 1);
748 sdev->status = SBP_DEV_RETRY;
749 sbp_cam_detach_sdev(sdev);
751 target->luns[i] = NULL;
754 case SBP_DEV_TOATTACH:
755 sdev->status = SBP_DEV_RESET;
767 sbp_post_busreset(void *arg)
769 struct sbp_softc *sbp;
771 sbp = (struct sbp_softc *)arg;
773 printf("sbp_post_busreset\n");
776 if ((sbp->flags & SIMQ_FREEZED) == 0) {
777 xpt_freeze_simq(sbp->sim, /*count*/1);
778 sbp->flags |= SIMQ_FREEZED;
780 microtime(&sbp->last_busreset);
785 sbp_post_explore(void *arg)
787 struct sbp_softc *sbp = (struct sbp_softc *)arg;
788 struct sbp_target *target;
789 struct fw_device *fwdev;
793 printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
795 /* We need physical access */
796 if (!firewire_phydma_enable)
804 /* Garbage Collection */
805 for (i = 0; i < SBP_NUM_TARGETS; i++) {
806 target = &sbp->targets[i];
807 if (target->fwdev == NULL)
810 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
811 if (target->fwdev == fwdev)
814 /* device has removed in lower driver */
815 sbp_cam_detach_target(target);
816 sbp_free_target(target);
820 /* traverse device list */
821 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
823 device_printf(sbp->fd.dev,"%s:: EUI:%08x%08x %s attached, state=%d\n",
824 __func__, fwdev->eui.hi, fwdev->eui.lo,
825 (fwdev->status != FWDEVATTACHED) ? "not" : "",
828 alive = SBP_FWDEV_ALIVE(fwdev);
829 for (i = 0; i < SBP_NUM_TARGETS; i++) {
830 target = &sbp->targets[i];
831 if (target->fwdev == fwdev) {
836 if (i == SBP_NUM_TARGETS) {
839 target = sbp_alloc_target(sbp, fwdev);
848 * It is safe to drop the lock here as the target is already
849 * reserved, so there should be no contenders for it.
850 * And the target is not yet exposed, so there should not be
851 * any other accesses to it.
852 * Finally, the list being iterated is protected somewhere else.
855 sbp_probe_target(target);
857 if (target->num_lun == 0)
858 sbp_free_target(target);
860 if ((sbp->flags & SIMQ_FREEZED) != 0) {
861 xpt_release_simq(sbp->sim, /*run queue*/TRUE);
862 sbp->flags &= ~SIMQ_FREEZED;
869 sbp_loginres_callback(struct fw_xfer *xfer)
871 struct sbp_dev *sdev;
872 sdev = (struct sbp_dev *)xfer->sc;
874 device_printf(sdev->target->sbp->fd.dev,"%s\n", __func__);
877 SBP_LOCK(sdev->target->sbp);
878 STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link);
879 SBP_UNLOCK(sdev->target->sbp);
885 sbp_xfer_free(struct fw_xfer *xfer)
887 struct sbp_dev *sdev;
889 sdev = (struct sbp_dev *)xfer->sc;
890 fw_xfer_unload(xfer);
891 SBP_LOCK_ASSERT(sdev->target->sbp);
892 STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
896 sbp_reset_start_callback(struct fw_xfer *xfer)
898 struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
899 struct sbp_target *target = sdev->target;
902 if (xfer->resp != 0) {
903 device_printf(sdev->target->sbp->fd.dev,
904 "%s: %s failed: resp=%d\n", __func__, sdev->bustgtlun, xfer->resp);
907 SBP_LOCK(target->sbp);
908 for (i = 0; i < target->num_lun; i++) {
909 tsdev = target->luns[i];
910 if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN)
913 SBP_UNLOCK(target->sbp);
917 sbp_reset_start(struct sbp_dev *sdev)
919 struct fw_xfer *xfer;
923 device_printf(sdev->target->sbp->fd.dev,
924 "%s:%s\n", __func__,sdev->bustgtlun);
927 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
928 xfer->hand = sbp_reset_start_callback;
929 fp = &xfer->send.hdr;
930 fp->mode.wreqq.dest_hi = 0xffff;
931 fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
932 fp->mode.wreqq.data = htonl(0xf);
933 fw_asyreq(xfer->fc, -1, xfer);
937 sbp_mgm_callback(struct fw_xfer *xfer)
939 struct sbp_dev *sdev;
942 sdev = (struct sbp_dev *)xfer->sc;
945 device_printf(sdev->target->sbp->fd.dev,
946 "%s:%s\n", __func__, sdev->bustgtlun);
949 SBP_LOCK(sdev->target->sbp);
951 SBP_UNLOCK(sdev->target->sbp);
954 static struct sbp_dev *
955 sbp_next_dev(struct sbp_target *target, int lun)
957 struct sbp_dev **sdevp;
960 for (i = lun, sdevp = &target->luns[lun]; i < target->num_lun;
962 if (*sdevp != NULL && (*sdevp)->status == SBP_DEV_PROBE)
969 sbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb)
971 struct sbp_softc *sbp;
972 struct sbp_target *target;
973 struct sbp_dev *sdev;
975 sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
976 target = sdev->target;
980 device_printf(sbp->fd.dev,
981 "%s:%s\n", __func__, sdev->bustgtlun);
983 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
984 sdev->status = SBP_DEV_ATTACHED;
986 device_printf(sbp->fd.dev,
987 "%s:%s failed\n", __func__, sdev->bustgtlun);
989 sdev = sbp_next_dev(target, sdev->lun_id + 1);
996 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
997 ccb->ccb_h.ccb_sdev_ptr = sdev;
998 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1002 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1007 sbp_cam_scan_target(void *arg)
1009 struct sbp_target *target = (struct sbp_target *)arg;
1010 struct sbp_dev *sdev;
1013 SBP_LOCK_ASSERT(target->sbp);
1014 sdev = sbp_next_dev(target, 0);
1016 printf("sbp_cam_scan_target: nothing to do for target%d\n",
1021 device_printf(sdev->target->sbp->fd.dev,
1022 "%s:%s\n", __func__, sdev->bustgtlun);
1024 ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO);
1026 printf("sbp_cam_scan_target: malloc failed\n");
1029 SBP_UNLOCK(target->sbp);
1031 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
1032 ccb->ccb_h.func_code = XPT_SCAN_LUN;
1033 ccb->ccb_h.cbfcnp = sbp_cam_scan_lun;
1034 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1035 ccb->crcn.flags = CAM_FLAG_NONE;
1036 ccb->ccb_h.ccb_sdev_ptr = sdev;
1038 /* The scan is in progress now. */
1041 SBP_LOCK(target->sbp);
1042 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1046 static __inline void
1047 sbp_scan_dev(struct sbp_dev *sdev)
1049 sdev->status = SBP_DEV_PROBE;
1050 callout_reset_sbt(&sdev->target->scan_callout, SBT_1MS * scan_delay, 0,
1051 sbp_cam_scan_target, (void *)sdev->target, 0);
1055 sbp_do_attach(struct fw_xfer *xfer)
1057 struct sbp_dev *sdev;
1058 struct sbp_target *target;
1059 struct sbp_softc *sbp;
1061 sdev = (struct sbp_dev *)xfer->sc;
1062 target = sdev->target;
1066 device_printf(sdev->target->sbp->fd.dev,
1067 "%s:%s\n", __func__, sdev->bustgtlun);
1069 sbp_xfer_free(xfer);
1071 if (sdev->path == NULL)
1072 xpt_create_path(&sdev->path, NULL,
1073 cam_sim_path(target->sbp->sim),
1074 target->target_id, sdev->lun_id);
1077 * Let CAM scan the bus if we are in the boot process.
1078 * XXX xpt_scan_bus cannot detect LUN larger than 0
1079 * if LUN 0 doesn't exist.
1082 sdev->status = SBP_DEV_ATTACHED;
1092 sbp_agent_reset_callback(struct fw_xfer *xfer)
1094 struct sbp_dev *sdev;
1096 sdev = (struct sbp_dev *)xfer->sc;
1098 device_printf(sdev->target->sbp->fd.dev,
1099 "%s:%s\n", __func__, sdev->bustgtlun);
1101 if (xfer->resp != 0) {
1102 device_printf(sdev->target->sbp->fd.dev,
1103 "%s:%s resp=%d\n", __func__, sdev->bustgtlun, xfer->resp);
1106 SBP_LOCK(sdev->target->sbp);
1107 sbp_xfer_free(xfer);
1109 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1112 SBP_UNLOCK(sdev->target->sbp);
1116 sbp_agent_reset(struct sbp_dev *sdev)
1118 struct fw_xfer *xfer;
1121 SBP_LOCK_ASSERT(sdev->target->sbp);
1123 device_printf(sdev->target->sbp->fd.dev,
1124 "%s:%s\n", __func__, sdev->bustgtlun);
1126 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1129 if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE)
1130 xfer->hand = sbp_agent_reset_callback;
1132 xfer->hand = sbp_do_attach;
1133 fp = &xfer->send.hdr;
1134 fp->mode.wreqq.data = htonl(0xf);
1135 fw_asyreq(xfer->fc, -1, xfer);
1136 sbp_abort_all_ocbs(sdev, CAM_BDR_SENT);
1140 sbp_busy_timeout_callback(struct fw_xfer *xfer)
1142 struct sbp_dev *sdev;
1144 sdev = (struct sbp_dev *)xfer->sc;
1146 device_printf(sdev->target->sbp->fd.dev,
1147 "%s:%s\n", __func__, sdev->bustgtlun);
1149 SBP_LOCK(sdev->target->sbp);
1150 sbp_xfer_free(xfer);
1151 sbp_agent_reset(sdev);
1152 SBP_UNLOCK(sdev->target->sbp);
1156 sbp_busy_timeout(struct sbp_dev *sdev)
1159 struct fw_xfer *xfer;
1161 device_printf(sdev->target->sbp->fd.dev,
1162 "%s:%s\n", __func__, sdev->bustgtlun);
1164 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1166 xfer->hand = sbp_busy_timeout_callback;
1167 fp = &xfer->send.hdr;
1168 fp->mode.wreqq.dest_hi = 0xffff;
1169 fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1170 fp->mode.wreqq.data = htonl((1 << (13 + 12)) | 0xf);
1171 fw_asyreq(xfer->fc, -1, xfer);
1175 sbp_orb_pointer_callback(struct fw_xfer *xfer)
1177 struct sbp_dev *sdev;
1178 sdev = (struct sbp_dev *)xfer->sc;
1181 device_printf(sdev->target->sbp->fd.dev,
1182 "%s:%s\n", __func__, sdev->bustgtlun);
1184 if (xfer->resp != 0) {
1186 printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1188 SBP_LOCK(sdev->target->sbp);
1189 sbp_xfer_free(xfer);
1191 sdev->flags &= ~ORB_POINTER_ACTIVE;
1193 if ((sdev->flags & ORB_POINTER_NEED) != 0) {
1194 struct sbp_ocb *ocb;
1196 sdev->flags &= ~ORB_POINTER_NEED;
1197 ocb = STAILQ_FIRST(&sdev->ocbs);
1199 sbp_orb_pointer(sdev, ocb);
1201 SBP_UNLOCK(sdev->target->sbp);
1206 sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1208 struct fw_xfer *xfer;
1211 device_printf(sdev->target->sbp->fd.dev,
1213 __func__, sdev->bustgtlun,
1214 (uint32_t)ocb->bus_addr);
1217 SBP_LOCK_ASSERT(sdev->target->sbp);
1219 if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) {
1221 printf("%s: orb pointer active\n", __func__);
1223 sdev->flags |= ORB_POINTER_NEED;
1227 sdev->flags |= ORB_POINTER_ACTIVE;
1228 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1231 xfer->hand = sbp_orb_pointer_callback;
1233 fp = &xfer->send.hdr;
1234 fp->mode.wreqb.len = 8;
1235 fp->mode.wreqb.extcode = 0;
1236 xfer->send.payload[0] =
1237 htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS) << 16));
1238 xfer->send.payload[1] = htonl((uint32_t)ocb->bus_addr);
1240 if (fw_asyreq(xfer->fc, -1, xfer) != 0) {
1241 sbp_xfer_free(xfer);
1242 ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1248 sbp_doorbell_callback(struct fw_xfer *xfer)
1250 struct sbp_dev *sdev;
1251 sdev = (struct sbp_dev *)xfer->sc;
1254 device_printf(sdev->target->sbp->fd.dev,
1255 "%s:%s\n", __func__, sdev->bustgtlun);
1257 if (xfer->resp != 0) {
1259 device_printf(sdev->target->sbp->fd.dev,
1260 "%s: xfer->resp = %d\n", __func__, xfer->resp);
1262 SBP_LOCK(sdev->target->sbp);
1263 sbp_xfer_free(xfer);
1264 sdev->flags &= ~ORB_DOORBELL_ACTIVE;
1265 if ((sdev->flags & ORB_DOORBELL_NEED) != 0) {
1266 sdev->flags &= ~ORB_DOORBELL_NEED;
1269 SBP_UNLOCK(sdev->target->sbp);
1273 sbp_doorbell(struct sbp_dev *sdev)
1275 struct fw_xfer *xfer;
1278 device_printf(sdev->target->sbp->fd.dev,
1279 "%s:%s\n", __func__, sdev->bustgtlun);
1282 if ((sdev->flags & ORB_DOORBELL_ACTIVE) != 0) {
1283 sdev->flags |= ORB_DOORBELL_NEED;
1286 sdev->flags |= ORB_DOORBELL_ACTIVE;
1287 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1290 xfer->hand = sbp_doorbell_callback;
1291 fp = &xfer->send.hdr;
1292 fp->mode.wreqq.data = htonl(0xf);
1293 fw_asyreq(xfer->fc, -1, xfer);
1296 static struct fw_xfer *
1297 sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1299 struct fw_xfer *xfer;
1301 struct sbp_target *target;
1304 SBP_LOCK_ASSERT(sdev->target->sbp);
1306 target = sdev->target;
1307 xfer = STAILQ_FIRST(&target->xferlist);
1309 if (target->n_xfer > 5 /* XXX */) {
1310 printf("sbp: no more xfer for this target\n");
1313 xfer = fw_xfer_alloc_buf(M_SBP, 8, 0);
1315 printf("sbp: fw_xfer_alloc_buf failed\n");
1320 printf("sbp: alloc %d xfer\n", target->n_xfer);
1323 STAILQ_REMOVE_HEAD(&target->xferlist, link);
1327 xfer->recv.pay_len = 0;
1328 xfer->send.spd = min(sdev->target->fwdev->speed, max_speed);
1329 xfer->fc = sdev->target->sbp->fd.fc;
1332 if (tcode == FWTCODE_WREQB)
1333 xfer->send.pay_len = 8;
1335 xfer->send.pay_len = 0;
1337 xfer->sc = (caddr_t)sdev;
1338 fp = &xfer->send.hdr;
1339 fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
1340 fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
1341 fp->mode.wreqq.tlrt = 0;
1342 fp->mode.wreqq.tcode = tcode;
1343 fp->mode.wreqq.pri = 0;
1344 fp->mode.wreqq.dst = FWLOCALBUS | sdev->target->fwdev->dst;
1350 sbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1352 struct fw_xfer *xfer;
1354 struct sbp_ocb *ocb;
1355 struct sbp_target *target;
1358 target = sdev->target;
1359 nid = target->sbp->fd.fc->nodeid | FWLOCALBUS;
1361 SBP_LOCK_ASSERT(target->sbp);
1362 if (func == ORB_FUN_RUNQUEUE) {
1363 ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1364 if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1367 STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1370 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
1374 ocb->flags = OCB_ACT_MGM;
1377 bzero((void *)ocb->orb, sizeof(ocb->orb));
1378 ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1379 ocb->orb[7] = htonl(SBP_DEV2ADDR(target->target_id, sdev->lun_id));
1382 device_printf(sdev->target->sbp->fd.dev,
1384 __func__,sdev->bustgtlun,
1385 orb_fun_name[(func >> 16) & 0xf]);
1389 ocb->orb[0] = ocb->orb[1] = 0; /* password */
1390 ocb->orb[2] = htonl(nid << 16);
1391 ocb->orb[3] = htonl(sdev->dma.bus_addr);
1392 ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id);
1394 ocb->orb[4] |= htonl(ORB_EXV);
1395 ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
1396 fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD);
1399 ocb->orb[0] = htonl((0 << 16) | 0);
1400 ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
1407 ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
1411 if (target->mgm_ocb_cur != NULL) {
1412 /* there is a standing ORB */
1413 STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1417 target->mgm_ocb_cur = ocb;
1419 callout_reset(&target->mgm_ocb_timeout, 5 * hz,
1420 sbp_mgm_timeout, (caddr_t)ocb);
1421 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1425 xfer->hand = sbp_mgm_callback;
1427 fp = &xfer->send.hdr;
1428 fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
1429 fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
1430 fp->mode.wreqb.len = 8;
1431 fp->mode.wreqb.extcode = 0;
1432 xfer->send.payload[0] = htonl(nid << 16);
1433 xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff);
1435 fw_asyreq(xfer->fc, -1, xfer);
1439 sbp_print_scsi_cmd(struct sbp_ocb *ocb)
1441 struct ccb_scsiio *csio;
1443 csio = &ocb->ccb->csio;
1444 printf("%s:%d:%jx XPT_SCSI_IO: "
1445 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1447 "%db cmd/%db data/%db sense\n",
1448 device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1449 ocb->ccb->ccb_h.target_id,
1450 (uintmax_t)ocb->ccb->ccb_h.target_lun,
1451 csio->cdb_io.cdb_bytes[0],
1452 csio->cdb_io.cdb_bytes[1],
1453 csio->cdb_io.cdb_bytes[2],
1454 csio->cdb_io.cdb_bytes[3],
1455 csio->cdb_io.cdb_bytes[4],
1456 csio->cdb_io.cdb_bytes[5],
1457 csio->cdb_io.cdb_bytes[6],
1458 csio->cdb_io.cdb_bytes[7],
1459 csio->cdb_io.cdb_bytes[8],
1460 csio->cdb_io.cdb_bytes[9],
1461 ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1462 csio->cdb_len, csio->dxfer_len,
1467 sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1469 struct sbp_cmd_status *sbp_cmd_status;
1470 struct scsi_sense_data_fixed *sense;
1472 sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1473 sense = (struct scsi_sense_data_fixed *)&ocb->ccb->csio.sense_data;
1476 sbp_print_scsi_cmd(ocb);
1477 /* XXX need decode status */
1478 printf("%s: SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1479 ocb->sdev->bustgtlun,
1480 sbp_cmd_status->status,
1481 sbp_cmd_status->sfmt,
1482 sbp_cmd_status->valid,
1483 sbp_cmd_status->s_key,
1484 sbp_cmd_status->s_code,
1485 sbp_cmd_status->s_qlfr,
1489 switch (sbp_cmd_status->status) {
1490 case SCSI_STATUS_CHECK_COND:
1491 case SCSI_STATUS_BUSY:
1492 case SCSI_STATUS_CMD_TERMINATED:
1493 if (sbp_cmd_status->sfmt == SBP_SFMT_CURR) {
1494 sense->error_code = SSD_CURRENT_ERROR;
1496 sense->error_code = SSD_DEFERRED_ERROR;
1498 if (sbp_cmd_status->valid)
1499 sense->error_code |= SSD_ERRCODE_VALID;
1500 sense->flags = sbp_cmd_status->s_key;
1501 if (sbp_cmd_status->mark)
1502 sense->flags |= SSD_FILEMARK;
1503 if (sbp_cmd_status->eom)
1504 sense->flags |= SSD_EOM;
1505 if (sbp_cmd_status->ill_len)
1506 sense->flags |= SSD_ILI;
1508 bcopy(&sbp_cmd_status->info, &sense->info[0], 4);
1510 if (sbp_status->len <= 1)
1511 /* XXX not scsi status. shouldn't be happened */
1512 sense->extra_len = 0;
1513 else if (sbp_status->len <= 4)
1514 /* add_sense_code(_qual), info, cmd_spec_info */
1515 sense->extra_len = 6;
1517 /* fru, sense_key_spec */
1518 sense->extra_len = 10;
1520 bcopy(&sbp_cmd_status->cdb, &sense->cmd_spec_info[0], 4);
1522 sense->add_sense_code = sbp_cmd_status->s_code;
1523 sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1524 sense->fru = sbp_cmd_status->fru;
1526 bcopy(&sbp_cmd_status->s_keydep[0],
1527 &sense->sense_key_spec[0], 3);
1529 ocb->ccb->csio.scsi_status = sbp_cmd_status->status;
1530 ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1531 | CAM_AUTOSNS_VALID;
1536 for (j = 0; j < 32; j += 8) {
1537 printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1538 tmp[j], tmp[j + 1], tmp[j + 2], tmp[j + 3],
1539 tmp[j + 4], tmp[j + 5], tmp[j + 6], tmp[j + 7]);
1546 device_printf(ocb->sdev->target->sbp->fd.dev,
1547 "%s:%s unknown scsi status 0x%x\n",
1548 __func__, ocb->sdev->bustgtlun,
1549 sbp_cmd_status->status);
1554 sbp_fix_inq_data(struct sbp_ocb *ocb)
1557 struct sbp_dev *sdev;
1558 struct scsi_inquiry_data *inq;
1563 if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1566 device_printf(sdev->target->sbp->fd.dev,
1567 "%s:%s\n", __func__, sdev->bustgtlun);
1569 inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1570 switch (SID_TYPE(inq)) {
1574 * XXX Convert Direct Access device to RBC.
1575 * I've never seen FireWire DA devices which support READ_6.
1577 if (SID_TYPE(inq) == T_DIRECT)
1578 inq->device |= T_RBC; /* T_DIRECT == 0 */
1583 * Override vendor/product/revision information.
1584 * Some devices sometimes return strange strings.
1587 bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1588 bcopy(sdev->product, inq->product, sizeof(inq->product));
1589 bcopy(sdev->revision + 2, inq->revision, sizeof(inq->revision));
1594 * Force to enable/disable tagged queuing.
1595 * XXX CAM also checks SCP_QUEUE_DQUE flag in the control mode page.
1598 inq->flags |= SID_CmdQue;
1599 else if (sbp_tags < 0)
1600 inq->flags &= ~SID_CmdQue;
1605 sbp_recv1(struct fw_xfer *xfer)
1611 struct sbp_softc *sbp;
1612 struct sbp_dev *sdev;
1613 struct sbp_ocb *ocb;
1614 struct sbp_login_res *login_res = NULL;
1615 struct sbp_status *sbp_status;
1616 struct sbp_target *target;
1617 int orb_fun, status_valid0, status_valid, t, l, reset_agent = 0;
1621 ld = xfer->recv.buf;
1622 printf("sbp %x %d %d %08x %08x %08x %08x\n",
1623 xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1624 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1625 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1627 sbp = (struct sbp_softc *)xfer->sc;
1628 SBP_LOCK_ASSERT(sbp);
1629 if (xfer->resp != 0) {
1630 printf("sbp_recv: xfer->resp = %d\n", xfer->resp);
1633 if (xfer->recv.payload == NULL) {
1634 printf("sbp_recv: xfer->recv.payload == NULL\n");
1637 rfp = &xfer->recv.hdr;
1638 if (rfp->mode.wreqb.tcode != FWTCODE_WREQB) {
1639 printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1642 sbp_status = (struct sbp_status *)xfer->recv.payload;
1643 addr = rfp->mode.wreqb.dest_lo;
1645 printf("received address 0x%x\n", addr);
1647 t = SBP_ADDR2TRG(addr);
1648 if (t >= SBP_NUM_TARGETS) {
1649 device_printf(sbp->fd.dev,
1650 "sbp_recv1: invalid target %d\n", t);
1653 target = &sbp->targets[t];
1654 l = SBP_ADDR2LUN(addr);
1655 if (l >= target->num_lun || target->luns[l] == NULL) {
1656 device_printf(sbp->fd.dev,
1657 "sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1660 sdev = target->luns[l];
1663 switch (sbp_status->src) {
1666 /* check mgm_ocb_cur first */
1667 ocb = target->mgm_ocb_cur;
1669 if (OCB_MATCH(ocb, sbp_status)) {
1670 callout_stop(&target->mgm_ocb_timeout);
1671 target->mgm_ocb_cur = NULL;
1675 ocb = sbp_dequeue_ocb(sdev, sbp_status);
1677 device_printf(sdev->target->sbp->fd.dev,
1678 "%s:%s No ocb(%x) on the queue\n",
1679 __func__,sdev->bustgtlun,
1680 ntohl(sbp_status->orb_lo));
1685 device_printf(sdev->target->sbp->fd.dev,
1686 "%s:%s unsolicit status received\n",
1687 __func__, sdev->bustgtlun);
1690 device_printf(sdev->target->sbp->fd.dev,
1691 "%s:%s unknown sbp_status->src\n",
1692 __func__, sdev->bustgtlun);
1695 status_valid0 = (sbp_status->src < 2
1696 && sbp_status->resp == ORB_RES_CMPL
1697 && sbp_status->dead == 0);
1698 status_valid = (status_valid0 && sbp_status->status == 0);
1700 if (!status_valid0 || debug > 2) {
1703 device_printf(sdev->target->sbp->fd.dev,
1704 "%s:%s ORB status src:%x resp:%x dead:%x"
1705 " len:%x stat:%x orb:%x%08x\n",
1706 __func__, sdev->bustgtlun,
1707 sbp_status->src, sbp_status->resp, sbp_status->dead,
1708 sbp_status->len, sbp_status->status,
1709 ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1711 device_printf(sdev->target->sbp->fd.dev,
1712 "%s\n", sdev->bustgtlun);
1713 status = sbp_status->status;
1714 switch (sbp_status->resp) {
1716 if (status > MAX_ORB_STATUS0)
1717 printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1719 printf("%s\n", orb_status0[status]);
1722 printf("Obj: %s, Error: %s\n",
1723 orb_status1_object[(status >> 6) & 3],
1724 orb_status1_serial_bus_error[status & 0xf]);
1727 printf("Illegal request\n");
1730 printf("Vendor dependent\n");
1733 printf("unknown respose code %d\n", sbp_status->resp);
1737 /* we have to reset the fetch agent if it's dead */
1738 if (sbp_status->dead) {
1740 xpt_freeze_devq(sdev->path, 1);
1749 switch (ntohl(ocb->orb[4]) & ORB_FMT_MSK) {
1755 switch (ocb->flags) {
1757 orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1761 fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD);
1762 login_res = sdev->login;
1763 login_res->len = ntohs(login_res->len);
1764 login_res->id = ntohs(login_res->id);
1765 login_res->cmd_hi = ntohs(login_res->cmd_hi);
1766 login_res->cmd_lo = ntohl(login_res->cmd_lo);
1769 device_printf(sdev->target->sbp->fd.dev,
1770 "%s:%s login: len %d, ID %d, cmd %08x%08x, recon_hold %d\n",
1771 __func__, sdev->bustgtlun,
1772 login_res->len, login_res->id,
1773 login_res->cmd_hi, login_res->cmd_lo,
1774 ntohs(login_res->recon_hold));
1776 sbp_busy_timeout(sdev);
1778 /* forgot logout? */
1779 device_printf(sdev->target->sbp->fd.dev,
1780 "%s:%s login failed\n",
1781 __func__, sdev->bustgtlun);
1782 sdev->status = SBP_DEV_RESET;
1786 login_res = sdev->login;
1789 device_printf(sdev->target->sbp->fd.dev,
1790 "%s:%s reconnect: len %d, ID %d, cmd %08x%08x\n",
1791 __func__, sdev->bustgtlun,
1792 login_res->len, login_res->id,
1793 login_res->cmd_hi, login_res->cmd_lo);
1795 if (sdev->status == SBP_DEV_ATTACHED)
1798 sbp_agent_reset(sdev);
1800 /* reconnection hold time exceed? */
1802 device_printf(sdev->target->sbp->fd.dev,
1803 "%s:%s reconnect failed\n",
1804 __func__, sdev->bustgtlun);
1810 sdev->status = SBP_DEV_RESET;
1813 sbp_busy_timeout(sdev);
1818 sbp_agent_reset(sdev);
1821 device_printf(sdev->target->sbp->fd.dev,
1822 "%s:%s unknown function %d\n",
1823 __func__, sdev->bustgtlun, orb_fun);
1826 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
1830 if (ocb->ccb != NULL) {
1834 if (sbp_status->len > 1) {
1835 sbp_scsi_status(sbp_status, ocb);
1837 if (sbp_status->resp != ORB_RES_CMPL) {
1838 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1840 ccb->ccb_h.status = CAM_REQ_CMP;
1843 /* fix up inq data */
1844 if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1845 sbp_fix_inq_data(ocb);
1855 sbp_free_ocb(sdev, ocb);
1858 sbp_agent_reset(sdev);
1861 xfer->recv.pay_len = SBP_RECV_LEN;
1862 /* The received packet is usually small enough to be stored within
1863 * the buffer. In that case, the controller return ack_complete and
1864 * no respose is necessary.
1866 * XXX fwohci.c and firewire.c should inform event_code such as
1867 * ack_complete or ack_pending to upper driver.
1871 sfp = (struct fw_pkt *)xfer->send.buf;
1872 sfp->mode.wres.dst = rfp->mode.wreqb.src;
1873 xfer->dst = sfp->mode.wres.dst;
1874 xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1875 xfer->hand = sbp_loginres_callback;
1877 sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1878 sfp->mode.wres.tcode = FWTCODE_WRES;
1879 sfp->mode.wres.rtcode = 0;
1880 sfp->mode.wres.pri = 0;
1882 fw_asyreq(xfer->fc, -1, xfer);
1885 STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1890 sbp_recv(struct fw_xfer *xfer)
1892 struct sbp_softc *sbp;
1894 sbp = (struct sbp_softc *)xfer->sc;
1903 sbp_attach(device_t dev)
1905 struct sbp_softc *sbp;
1906 struct cam_devq *devq;
1907 struct firewire_comm *fc;
1910 if (DFLTPHYS > SBP_MAXPHYS)
1911 device_printf(dev, "Warning, DFLTPHYS(%dKB) is larger than "
1912 "SBP_MAXPHYS(%dKB).\n", DFLTPHYS / 1024,
1913 SBP_MAXPHYS / 1024);
1915 if (!firewire_phydma_enable)
1916 device_printf(dev, "Warning, hw.firewire.phydma_enable must be 1 "
1917 "for SBP over FireWire.\n");
1919 printf("sbp_attach (cold=%d)\n", cold);
1924 sbp = device_get_softc(dev);
1926 sbp->fd.fc = fc = device_get_ivars(dev);
1927 mtx_init(&sbp->mtx, "sbp", NULL, MTX_DEF);
1930 max_speed = fc->speed;
1932 error = bus_dma_tag_create(/*parent*/fc->dmat,
1933 /* XXX shoud be 4 for sane backend? */
1936 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1937 /*highaddr*/BUS_SPACE_MAXADDR,
1938 /*filter*/NULL, /*filterarg*/NULL,
1939 /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1940 /*maxsegsz*/SBP_SEG_MAX,
1941 /*flags*/BUS_DMA_ALLOCNOW,
1942 /*lockfunc*/busdma_lock_mutex,
1943 /*lockarg*/&sbp->mtx,
1946 printf("sbp_attach: Could not allocate DMA tag "
1947 "- error %d\n", error);
1951 devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1955 for (i = 0; i < SBP_NUM_TARGETS; i++) {
1956 sbp->targets[i].fwdev = NULL;
1957 sbp->targets[i].luns = NULL;
1958 sbp->targets[i].sbp = sbp;
1961 sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1962 device_get_unit(dev),
1965 /*tagged*/ SBP_QUEUE_LEN - 1,
1968 if (sbp->sim == NULL) {
1969 cam_simq_free(devq);
1974 if (xpt_bus_register(sbp->sim, dev, /*bus*/0) != CAM_SUCCESS)
1977 if (xpt_create_path(&sbp->path, NULL, cam_sim_path(sbp->sim),
1978 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1979 xpt_bus_deregister(cam_sim_path(sbp->sim));
1984 /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1985 sbp->fwb.start = ((u_int64_t)SBP_BIND_HI << 32) | SBP_DEV2ADDR(0, 0);
1986 sbp->fwb.end = sbp->fwb.start + 0xffff;
1987 /* pre-allocate xfer */
1988 STAILQ_INIT(&sbp->fwb.xferlist);
1989 fw_xferlist_add(&sbp->fwb.xferlist, M_SBP,
1990 /*send*/ 0, /*recv*/ SBP_RECV_LEN, SBP_NUM_OCB/2,
1991 fc, (void *)sbp, sbp_recv);
1993 fw_bindadd(fc, &sbp->fwb);
1995 sbp->fd.post_busreset = sbp_post_busreset;
1996 sbp->fd.post_explore = sbp_post_explore;
1998 if (fc->status != -1) {
1999 sbp_post_busreset(sbp);
2000 sbp_post_explore(sbp);
2003 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
2009 cam_sim_free(sbp->sim, /*free_devq*/TRUE);
2014 sbp_logout_all(struct sbp_softc *sbp)
2016 struct sbp_target *target;
2017 struct sbp_dev *sdev;
2021 printf("sbp_logout_all\n");
2023 SBP_LOCK_ASSERT(sbp);
2024 for (i = 0; i < SBP_NUM_TARGETS; i++) {
2025 target = &sbp->targets[i];
2026 if (target->luns == NULL)
2028 for (j = 0; j < target->num_lun; j++) {
2029 sdev = target->luns[j];
2032 callout_stop(&sdev->login_callout);
2033 if (sdev->status >= SBP_DEV_TOATTACH &&
2034 sdev->status <= SBP_DEV_ATTACHED)
2035 sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
2043 sbp_shutdown(device_t dev)
2045 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2048 sbp_logout_all(sbp);
2054 sbp_free_sdev(struct sbp_dev *sdev)
2056 struct sbp_softc *sbp;
2061 sbp = sdev->target->sbp;
2063 callout_drain(&sdev->login_callout);
2064 for (i = 0; i < SBP_QUEUE_LEN; i++) {
2065 callout_drain(&sdev->ocb[i].timer);
2066 bus_dmamap_destroy(sbp->dmat, sdev->ocb[i].dmamap);
2068 fwdma_free(sbp->fd.fc, &sdev->dma);
2074 sbp_free_target(struct sbp_target *target)
2076 struct sbp_softc *sbp;
2077 struct fw_xfer *xfer, *next;
2080 if (target->luns == NULL)
2083 SBP_LOCK_ASSERT(sbp);
2085 callout_drain(&target->mgm_ocb_timeout);
2086 callout_drain(&target->scan_callout);
2088 for (i = 0; i < target->num_lun; i++)
2089 sbp_free_sdev(target->luns[i]);
2091 STAILQ_FOREACH_SAFE(xfer, &target->xferlist, link, next) {
2092 fw_xfer_free_buf(xfer);
2094 STAILQ_INIT(&target->xferlist);
2095 free(target->luns, M_SBP);
2096 target->num_lun = 0;
2097 target->luns = NULL;
2098 target->fwdev = NULL;
2102 sbp_detach(device_t dev)
2104 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2105 struct firewire_comm *fc = sbp->fd.fc;
2109 printf("sbp_detach\n");
2113 for (i = 0; i < SBP_NUM_TARGETS; i++)
2114 sbp_cam_detach_target(&sbp->targets[i]);
2116 xpt_async(AC_LOST_DEVICE, sbp->path, NULL);
2117 xpt_free_path(sbp->path);
2118 xpt_bus_deregister(cam_sim_path(sbp->sim));
2119 cam_sim_free(sbp->sim, /*free_devq*/ TRUE);
2121 sbp_logout_all(sbp);
2124 /* XXX wait for logout completion */
2125 pause("sbpdtc", hz/2);
2128 for (i = 0; i < SBP_NUM_TARGETS; i++)
2129 sbp_free_target(&sbp->targets[i]);
2132 fw_bindremove(fc, &sbp->fwb);
2133 fw_xferlist_remove(&sbp->fwb.xferlist);
2135 bus_dma_tag_destroy(sbp->dmat);
2136 mtx_destroy(&sbp->mtx);
2142 sbp_cam_detach_sdev(struct sbp_dev *sdev)
2146 if (sdev->status == SBP_DEV_DEAD)
2148 if (sdev->status == SBP_DEV_RESET)
2150 SBP_LOCK_ASSERT(sdev->target->sbp);
2151 sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
2153 xpt_release_devq(sdev->path,
2154 sdev->freeze, TRUE);
2156 xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
2157 xpt_free_path(sdev->path);
2163 sbp_cam_detach_target(struct sbp_target *target)
2167 SBP_LOCK_ASSERT(target->sbp);
2168 if (target->luns != NULL) {
2170 printf("sbp_detach_target %d\n", target->target_id);
2172 callout_stop(&target->scan_callout);
2173 for (i = 0; i < target->num_lun; i++)
2174 sbp_cam_detach_sdev(target->luns[i]);
2179 sbp_target_reset(struct sbp_dev *sdev, int method)
2182 struct sbp_target *target = sdev->target;
2183 struct sbp_dev *tsdev;
2185 SBP_LOCK_ASSERT(target->sbp);
2186 for (i = 0; i < target->num_lun; i++) {
2187 tsdev = target->luns[i];
2190 if (tsdev->status == SBP_DEV_DEAD)
2192 if (tsdev->status == SBP_DEV_RESET)
2194 xpt_freeze_devq(tsdev->path, 1);
2196 sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT);
2198 tsdev->status = SBP_DEV_LOGIN;
2202 printf("target reset\n");
2203 sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2206 printf("reset start\n");
2207 sbp_reset_start(sdev);
2214 sbp_mgm_timeout(void *arg)
2216 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2217 struct sbp_dev *sdev = ocb->sdev;
2218 struct sbp_target *target = sdev->target;
2220 SBP_LOCK_ASSERT(target->sbp);
2221 device_printf(sdev->target->sbp->fd.dev,
2222 "%s:%s request timeout(mgm orb:0x%08x)\n",
2223 __func__, sdev->bustgtlun, (uint32_t)ocb->bus_addr);
2224 target->mgm_ocb_cur = NULL;
2225 sbp_free_ocb(sdev, ocb);
2228 printf("run next request\n");
2229 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2231 device_printf(sdev->target->sbp->fd.dev,
2232 "%s:%s reset start\n",
2233 __func__, sdev->bustgtlun);
2234 sbp_reset_start(sdev);
2238 sbp_timeout(void *arg)
2240 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2241 struct sbp_dev *sdev = ocb->sdev;
2243 device_printf(sdev->target->sbp->fd.dev,
2244 "%s:%s request timeout(cmd orb:0x%08x) ... ",
2245 __func__, sdev->bustgtlun, (uint32_t)ocb->bus_addr);
2247 SBP_LOCK_ASSERT(sdev->target->sbp);
2249 switch (sdev->timeout) {
2251 printf("agent reset\n");
2252 xpt_freeze_devq(sdev->path, 1);
2254 sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2255 sbp_agent_reset(sdev);
2259 sbp_target_reset(sdev, sdev->timeout - 1);
2264 sbp_cam_detach_target(target);
2265 if (target->luns != NULL)
2266 free(target->luns, M_SBP);
2267 target->num_lun = 0;
2268 target->luns = NULL;
2269 target->fwdev = NULL;
2275 sbp_action(struct cam_sim *sim, union ccb *ccb)
2278 struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2279 struct sbp_target *target = NULL;
2280 struct sbp_dev *sdev = NULL;
2283 SBP_LOCK_ASSERT(sbp);
2284 /* target:lun -> sdev mapping */
2286 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2287 && ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2288 target = &sbp->targets[ccb->ccb_h.target_id];
2289 if (target->fwdev != NULL
2290 && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2291 && ccb->ccb_h.target_lun < target->num_lun) {
2292 sdev = target->luns[ccb->ccb_h.target_lun];
2293 if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED &&
2294 sdev->status != SBP_DEV_PROBE)
2301 printf("invalid target %d lun %jx\n",
2302 ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun);
2305 switch (ccb->ccb_h.func_code) {
2308 case XPT_GET_TRAN_SETTINGS:
2309 case XPT_SET_TRAN_SETTINGS:
2310 case XPT_CALC_GEOMETRY:
2313 printf("%s:%d:%jx:func_code 0x%04x: "
2314 "Invalid target (target needed)\n",
2315 device_get_nameunit(sbp->fd.dev),
2316 ccb->ccb_h.target_id,
2317 (uintmax_t)ccb->ccb_h.target_lun,
2318 ccb->ccb_h.func_code);
2321 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2328 /* The opcodes sometimes aimed at a target (sc is valid),
2329 * sometimes aimed at the SIM (sc is invalid and target is
2330 * CAM_TARGET_WILDCARD)
2333 ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2335 printf("%s:%d:%jx func_code 0x%04x: "
2336 "Invalid target (no wildcard)\n",
2337 device_get_nameunit(sbp->fd.dev),
2338 ccb->ccb_h.target_id,
2339 (uintmax_t)ccb->ccb_h.target_lun,
2340 ccb->ccb_h.func_code);
2342 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2348 /* XXX Hm, we should check the input parameters */
2352 switch (ccb->ccb_h.func_code) {
2355 struct ccb_scsiio *csio;
2356 struct sbp_ocb *ocb;
2361 mtx_assert(sim->mtx, MA_OWNED);
2364 printf("%s:%d:%jx XPT_SCSI_IO: "
2365 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2367 "%db cmd/%db data/%db sense\n",
2368 device_get_nameunit(sbp->fd.dev),
2369 ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun,
2370 csio->cdb_io.cdb_bytes[0],
2371 csio->cdb_io.cdb_bytes[1],
2372 csio->cdb_io.cdb_bytes[2],
2373 csio->cdb_io.cdb_bytes[3],
2374 csio->cdb_io.cdb_bytes[4],
2375 csio->cdb_io.cdb_bytes[5],
2376 csio->cdb_io.cdb_bytes[6],
2377 csio->cdb_io.cdb_bytes[7],
2378 csio->cdb_io.cdb_bytes[8],
2379 csio->cdb_io.cdb_bytes[9],
2380 ccb->ccb_h.flags & CAM_DIR_MASK,
2381 csio->cdb_len, csio->dxfer_len,
2385 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2389 if (csio->cdb_len > sizeof(ocb->orb) - 5 * sizeof(uint32_t)) {
2390 ccb->ccb_h.status = CAM_REQ_INVALID;
2395 /* if we are in probe stage, pass only probe commands */
2396 if (sdev->status == SBP_DEV_PROBE) {
2398 name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2399 printf("probe stage, periph name: %s\n", name);
2400 if (strcmp(name, "probe") != 0) {
2401 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2407 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
2408 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2409 if (sdev->freeze == 0) {
2410 xpt_freeze_devq(sdev->path, 1);
2417 ocb->flags = OCB_ACT_CMD;
2420 ccb->ccb_h.ccb_sdev_ptr = sdev;
2421 ocb->orb[0] = htonl(1U << 31);
2423 ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS) << 16));
2424 ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2425 speed = min(target->fwdev->speed, max_speed);
2426 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2427 | ORB_CMD_MAXP(speed + 7));
2428 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2429 ocb->orb[4] |= htonl(ORB_CMD_IN);
2432 if (csio->ccb_h.flags & CAM_CDB_POINTER)
2433 cdb = (void *)csio->cdb_io.cdb_ptr;
2435 cdb = (void *)&csio->cdb_io.cdb_bytes;
2436 bcopy(cdb, (void *)&ocb->orb[5], csio->cdb_len);
2438 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2439 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2441 if (ccb->csio.dxfer_len > 0) {
2444 error = bus_dmamap_load_ccb(/*dma tag*/sbp->dmat,
2445 /*dma map*/ocb->dmamap,
2451 printf("sbp: bus_dmamap_load error %d\n", error);
2453 sbp_execute_ocb(ocb, NULL, 0, 0);
2456 case XPT_CALC_GEOMETRY:
2458 struct ccb_calc_geometry *ccg;
2461 if (ccg->block_size == 0) {
2462 printf("sbp_action: block_size is 0.\n");
2463 ccb->ccb_h.status = CAM_REQ_INVALID;
2468 printf("%s:%d:%d:%jx:XPT_CALC_GEOMETRY: "
2469 "Volume size = %jd\n",
2470 device_get_nameunit(sbp->fd.dev),
2471 cam_sim_path(sbp->sim),
2472 ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun,
2473 (uintmax_t)ccg->volume_size);
2476 cam_calc_geometry(ccg, /*extended*/1);
2480 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
2484 printf("%s:%d:XPT_RESET_BUS: \n",
2485 device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2488 ccb->ccb_h.status = CAM_REQ_INVALID;
2492 case XPT_PATH_INQ: /* Path routing inquiry */
2494 struct ccb_pathinq *cpi = &ccb->cpi;
2497 printf("%s:%d:%jx XPT_PATH_INQ:.\n",
2498 device_get_nameunit(sbp->fd.dev),
2499 ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun);
2501 cpi->version_num = 1; /* XXX??? */
2502 cpi->hba_inquiry = PI_TAG_ABLE;
2503 cpi->target_sprt = 0;
2504 cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE;
2505 cpi->hba_eng_cnt = 0;
2506 cpi->max_target = SBP_NUM_TARGETS - 1;
2507 cpi->max_lun = SBP_NUM_LUNS - 1;
2508 cpi->initiator_id = SBP_INITIATOR;
2509 cpi->bus_id = sim->bus_id;
2510 cpi->base_transfer_speed = 400 * 1000 / 8;
2511 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2512 strlcpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2513 strlcpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2514 cpi->unit_number = sim->unit_number;
2515 cpi->transport = XPORT_SPI; /* XX should have a FireWire */
2516 cpi->transport_version = 2;
2517 cpi->protocol = PROTO_SCSI;
2518 cpi->protocol_version = SCSI_REV_2;
2520 cpi->ccb_h.status = CAM_REQ_CMP;
2524 case XPT_GET_TRAN_SETTINGS:
2526 struct ccb_trans_settings *cts = &ccb->cts;
2527 struct ccb_trans_settings_scsi *scsi =
2528 &cts->proto_specific.scsi;
2529 struct ccb_trans_settings_spi *spi =
2530 &cts->xport_specific.spi;
2532 cts->protocol = PROTO_SCSI;
2533 cts->protocol_version = SCSI_REV_2;
2534 cts->transport = XPORT_SPI; /* should have a FireWire */
2535 cts->transport_version = 2;
2536 spi->valid = CTS_SPI_VALID_DISC;
2537 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
2538 scsi->valid = CTS_SCSI_VALID_TQ;
2539 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2541 printf("%s:%d:%jx XPT_GET_TRAN_SETTINGS:.\n",
2542 device_get_nameunit(sbp->fd.dev),
2543 ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun);
2545 cts->ccb_h.status = CAM_REQ_CMP;
2550 ccb->ccb_h.status = CAM_UA_ABORT;
2553 case XPT_SET_TRAN_SETTINGS:
2556 ccb->ccb_h.status = CAM_REQ_INVALID;
2564 sbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error)
2567 struct sbp_ocb *ocb;
2568 struct sbp_ocb *prev;
2569 bus_dma_segment_t *s;
2572 printf("sbp_execute_ocb: error=%d\n", error);
2574 ocb = (struct sbp_ocb *)arg;
2577 printf("sbp_execute_ocb: seg %d", seg);
2578 for (i = 0; i < seg; i++)
2579 printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2580 (uintmax_t)segments[i].ds_len);
2585 /* direct pointer */
2587 if (s->ds_len > SBP_SEG_MAX)
2588 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2589 ocb->orb[3] = htonl(s->ds_addr);
2590 ocb->orb[4] |= htonl(s->ds_len);
2591 } else if (seg > 1) {
2593 for (i = 0; i < seg; i++) {
2596 /* XXX LSI Logic "< 16 byte" bug might be hit */
2598 printf("sbp_execute_ocb: warning, "
2599 "segment length(%zd) is less than 16."
2600 "(seg=%d/%d)\n", (size_t)s->ds_len, i + 1, seg);
2602 if (s->ds_len > SBP_SEG_MAX)
2603 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2604 ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2605 ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2607 ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2611 bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2612 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2613 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2614 prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2615 fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2618 if (ocb->sdev->last_ocb != NULL)
2619 sbp_doorbell(ocb->sdev);
2621 sbp_orb_pointer(ocb->sdev, ocb);
2624 if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) {
2625 ocb->sdev->flags &= ~ORB_LINK_DEAD;
2626 sbp_orb_pointer(ocb->sdev, ocb);
2632 sbp_poll(struct cam_sim *sim)
2634 struct sbp_softc *sbp;
2635 struct firewire_comm *fc;
2637 sbp = (struct sbp_softc *)sim->softc;
2640 fc->poll(fc, 0, -1);
2645 static struct sbp_ocb *
2646 sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2648 struct sbp_ocb *ocb;
2649 struct sbp_ocb *next;
2653 device_printf(sdev->target->sbp->fd.dev,
2654 "%s:%s 0x%08x src %d\n",
2655 __func__, sdev->bustgtlun, ntohl(sbp_status->orb_lo), sbp_status->src);
2657 SBP_LOCK_ASSERT(sdev->target->sbp);
2658 STAILQ_FOREACH_SAFE(ocb, &sdev->ocbs, ocb, next) {
2659 if (OCB_MATCH(ocb, sbp_status)) {
2661 STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2662 if (ocb->ccb != NULL)
2663 callout_stop(&ocb->timer);
2664 if (ntohl(ocb->orb[4]) & 0xffff) {
2665 bus_dmamap_sync(sdev->target->sbp->dmat,
2667 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2668 BUS_DMASYNC_POSTREAD :
2669 BUS_DMASYNC_POSTWRITE);
2670 bus_dmamap_unload(sdev->target->sbp->dmat,
2673 if (!use_doorbell) {
2674 if (sbp_status->src == SRC_NO_NEXT) {
2676 sbp_orb_pointer(sdev, next);
2677 else if (order > 0) {
2679 * Unordered execution
2680 * We need to send pointer for
2683 sdev->flags |= ORB_LINK_DEAD;
2688 * XXX this is not correct for unordered
2691 if (sdev->last_ocb != NULL) {
2692 sbp_free_ocb(sdev, sdev->last_ocb);
2694 sdev->last_ocb = ocb;
2696 sbp_status->src == SRC_NO_NEXT)
2704 if (ocb && order > 0) {
2705 device_printf(sdev->target->sbp->fd.dev,
2706 "%s:%s unordered execution order:%d\n",
2707 __func__, sdev->bustgtlun, order);
2713 static struct sbp_ocb *
2714 sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2716 struct sbp_ocb *prev, *prev2;
2718 SBP_LOCK_ASSERT(sdev->target->sbp);
2720 device_printf(sdev->target->sbp->fd.dev,
2721 "%s:%s 0x%08jx\n", __func__, sdev->bustgtlun, (uintmax_t)ocb->bus_addr);
2723 prev2 = prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2724 STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2726 if (ocb->ccb != NULL) {
2727 callout_reset_sbt(&ocb->timer,
2728 SBT_1MS * ocb->ccb->ccb_h.timeout, 0, sbp_timeout,
2732 if (use_doorbell && prev == NULL)
2733 prev2 = sdev->last_ocb;
2735 if (prev2 != NULL && (ocb->sdev->flags & ORB_LINK_DEAD) == 0) {
2737 printf("linking chain 0x%jx -> 0x%jx\n",
2738 (uintmax_t)prev2->bus_addr, (uintmax_t)ocb->bus_addr);
2741 * Suppress compiler optimization so that orb[1] must be written first.
2742 * XXX We may need an explicit memory barrier for other architectures
2743 * other than i386/amd64.
2745 *(volatile uint32_t *)&prev2->orb[1] = htonl(ocb->bus_addr);
2746 *(volatile uint32_t *)&prev2->orb[0] = 0;
2752 static struct sbp_ocb *
2753 sbp_get_ocb(struct sbp_dev *sdev)
2755 struct sbp_ocb *ocb;
2757 SBP_LOCK_ASSERT(sdev->target->sbp);
2758 ocb = STAILQ_FIRST(&sdev->free_ocbs);
2760 sdev->flags |= ORB_SHORTAGE;
2761 printf("ocb shortage!!!\n");
2764 STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2770 sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2775 SBP_LOCK_ASSERT(sdev->target->sbp);
2776 STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2777 if ((sdev->flags & ORB_SHORTAGE) != 0) {
2780 sdev->flags &= ~ORB_SHORTAGE;
2781 count = sdev->freeze;
2783 xpt_release_devq(sdev->path, count, TRUE);
2788 sbp_abort_ocb(struct sbp_ocb *ocb, int status)
2790 struct sbp_dev *sdev;
2793 SBP_LOCK_ASSERT(sdev->target->sbp);
2795 device_printf(sdev->target->sbp->fd.dev,
2796 "%s:%s 0x%jx\n", __func__, sdev->bustgtlun, (uintmax_t)ocb->bus_addr);
2799 if (ocb->ccb != NULL)
2800 sbp_print_scsi_cmd(ocb);
2802 if (ntohl(ocb->orb[4]) & 0xffff) {
2803 bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
2804 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2805 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2806 bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
2808 if (ocb->ccb != NULL) {
2809 callout_stop(&ocb->timer);
2810 ocb->ccb->ccb_h.status = status;
2813 sbp_free_ocb(sdev, ocb);
2817 sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2819 struct sbp_ocb *ocb, *next;
2820 STAILQ_HEAD(, sbp_ocb) temp;
2823 SBP_LOCK_ASSERT(sdev->target->sbp);
2824 STAILQ_CONCAT(&temp, &sdev->ocbs);
2825 STAILQ_INIT(&sdev->ocbs);
2827 STAILQ_FOREACH_SAFE(ocb, &temp, ocb, next) {
2828 sbp_abort_ocb(ocb, status);
2830 if (sdev->last_ocb != NULL) {
2831 sbp_free_ocb(sdev, sdev->last_ocb);
2832 sdev->last_ocb = NULL;
2836 static devclass_t sbp_devclass;
2838 static device_method_t sbp_methods[] = {
2839 /* device interface */
2840 DEVMETHOD(device_identify, sbp_identify),
2841 DEVMETHOD(device_probe, sbp_probe),
2842 DEVMETHOD(device_attach, sbp_attach),
2843 DEVMETHOD(device_detach, sbp_detach),
2844 DEVMETHOD(device_shutdown, sbp_shutdown),
2849 static driver_t sbp_driver = {
2852 sizeof(struct sbp_softc),
2854 DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2855 MODULE_VERSION(sbp, 1);
2856 MODULE_DEPEND(sbp, firewire, 1, 1, 1);
2857 MODULE_DEPEND(sbp, cam, 1, 1, 1);