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>
46 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
48 #include <sys/mutex.h>
51 #if defined(__DragonFly__) || __FreeBSD_version < 500106
52 #include <sys/devicestat.h> /* for struct devstat */
56 #include <bus/cam/cam.h>
57 #include <bus/cam/cam_ccb.h>
58 #include <bus/cam/cam_sim.h>
59 #include <bus/cam/cam_xpt_sim.h>
60 #include <bus/cam/cam_debug.h>
61 #include <bus/cam/cam_periph.h>
62 #include <bus/cam/scsi/scsi_all.h>
64 #include <bus/firewire/firewire.h>
65 #include <bus/firewire/firewirereg.h>
66 #include <bus/firewire/fwdma.h>
67 #include <bus/firewire/iec13213.h>
71 #include <cam/cam_ccb.h>
72 #include <cam/cam_sim.h>
73 #include <cam/cam_xpt_sim.h>
74 #include <cam/cam_debug.h>
75 #include <cam/cam_periph.h>
76 #include <cam/scsi/scsi_all.h>
78 #include <dev/firewire/firewire.h>
79 #include <dev/firewire/firewirereg.h>
80 #include <dev/firewire/fwdma.h>
81 #include <dev/firewire/iec13213.h>
82 #include <dev/firewire/sbp.h>
85 #define ccb_sdev_ptr spriv_ptr0
86 #define ccb_sbp_ptr spriv_ptr1
88 #define SBP_NUM_TARGETS 8 /* MAX 64 */
90 * Scan_bus doesn't work for more than 8 LUNs
91 * because of CAM_SCSI2_MAXLUN in cam_xpt.c
93 #define SBP_NUM_LUNS 64
94 #define SBP_MAXPHYS MIN(MAXPHYS, (512*1024) /* 512KB */)
95 #define SBP_DMA_SIZE PAGE_SIZE
96 #define SBP_LOGIN_SIZE sizeof(struct sbp_login_res)
97 #define SBP_QUEUE_LEN ((SBP_DMA_SIZE - SBP_LOGIN_SIZE) / sizeof(struct sbp_ocb))
98 #define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS)
101 * STATUS FIFO addressing
103 * -----------------------
104 * 0- 1( 2): 0 (alignment)
107 * 16-31( 8): reserved
108 * 32-47(16): SBP_BIND_HI
109 * 48-64(16): bus_id, node_id
111 #define SBP_BIND_HI 0x1
112 #define SBP_DEV2ADDR(t, l) \
113 (((u_int64_t)SBP_BIND_HI << 32) \
114 | (((l) & 0xff) << 8) \
115 | (((t) & 0x3f) << 2))
116 #define SBP_ADDR2TRG(a) (((a) >> 2) & 0x3f)
117 #define SBP_ADDR2LUN(a) (((a) >> 8) & 0xff)
118 #define SBP_INITIATOR 7
120 static char *orb_fun_name[] = {
124 static int debug = 0;
125 static int auto_login = 1;
126 static int max_speed = -1;
127 static int sbp_cold = 1;
128 static int ex_login = 1;
129 static int login_delay = 1000; /* msec */
130 static int scan_delay = 500; /* msec */
131 static int use_doorbell = 0;
132 static int sbp_tags = 0;
134 SYSCTL_DECL(_hw_firewire);
135 static SYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0,
137 SYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0,
139 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0,
140 "SBP perform login automatically");
141 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0,
142 "SBP transfer max speed");
143 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, exclusive_login, CTLFLAG_RW,
144 &ex_login, 0, "SBP enable exclusive login");
145 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, login_delay, CTLFLAG_RW,
146 &login_delay, 0, "SBP login delay in msec");
147 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, scan_delay, CTLFLAG_RW,
148 &scan_delay, 0, "SBP scan delay in msec");
149 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, use_doorbell, CTLFLAG_RW,
150 &use_doorbell, 0, "SBP use doorbell request");
151 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, tags, CTLFLAG_RW, &sbp_tags, 0,
152 "SBP tagged queuing support");
154 TUNABLE_INT("hw.firewire.sbp.auto_login", &auto_login);
155 TUNABLE_INT("hw.firewire.sbp.max_speed", &max_speed);
156 TUNABLE_INT("hw.firewire.sbp.exclusive_login", &ex_login);
157 TUNABLE_INT("hw.firewire.sbp.login_delay", &login_delay);
158 TUNABLE_INT("hw.firewire.sbp.scan_delay", &scan_delay);
159 TUNABLE_INT("hw.firewire.sbp.use_doorbell", &use_doorbell);
160 TUNABLE_INT("hw.firewire.sbp.tags", &sbp_tags);
162 #define NEED_RESPONSE 0
164 #define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE)
165 #ifdef __sparc64__ /* iommu */
166 #define SBP_IND_MAX howmany(SBP_MAXPHYS, SBP_SEG_MAX)
168 #define SBP_IND_MAX howmany(SBP_MAXPHYS, PAGE_SIZE)
171 STAILQ_ENTRY(sbp_ocb) ocb;
175 #define IND_PTR_OFFSET (8*sizeof(uint32_t))
176 struct ind_ptr ind_ptr[SBP_IND_MAX];
177 struct sbp_dev *sdev;
178 int flags; /* XXX should be removed */
180 struct callout timer;
183 #define OCB_ACT_MGM 0
184 #define OCB_ACT_CMD 1
185 #define OCB_MATCH(o,s) ((o)->bus_addr == ntohl((s)->orb_lo))
188 #define SBP_DEV_RESET 0 /* accept login */
189 #define SBP_DEV_LOGIN 1 /* to login */
191 #define SBP_DEV_RECONN 2 /* to reconnect */
193 #define SBP_DEV_TOATTACH 3 /* to attach */
194 #define SBP_DEV_PROBE 4 /* scan lun */
195 #define SBP_DEV_ATTACHED 5 /* in operation */
196 #define SBP_DEV_DEAD 6 /* unavailable unit */
197 #define SBP_DEV_RETRY 7 /* unavailable unit */
203 #define ORB_LINK_DEAD (1 << 0)
204 #define VALID_LUN (1 << 1)
205 #define ORB_POINTER_ACTIVE (1 << 2)
206 #define ORB_POINTER_NEED (1 << 3)
207 #define ORB_DOORBELL_ACTIVE (1 << 4)
208 #define ORB_DOORBELL_NEED (1 << 5)
209 #define ORB_SHORTAGE (1 << 6)
211 struct cam_path *path;
212 struct sbp_target *target;
213 struct fwdma_alloc dma;
214 struct sbp_login_res *login;
215 struct callout login_callout;
217 STAILQ_HEAD(, sbp_ocb) ocbs;
218 STAILQ_HEAD(, sbp_ocb) free_ocbs;
219 struct sbp_ocb *last_ocb;
229 struct sbp_dev **luns;
230 struct sbp_softc *sbp;
231 struct fw_device *fwdev;
232 uint32_t mgm_hi, mgm_lo;
233 struct sbp_ocb *mgm_ocb_cur;
234 STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue;
235 struct callout mgm_ocb_timeout;
236 struct callout scan_callout;
237 STAILQ_HEAD(, fw_xfer) xferlist;
242 struct firewire_dev_comm fd;
244 struct cam_path *path;
245 struct sbp_target targets[SBP_NUM_TARGETS];
248 struct timeval last_busreset;
249 #define SIMQ_FREEZED 1
253 #define SBP_LOCK(sbp) mtx_lock(&(sbp)->mtx)
254 #define SBP_UNLOCK(sbp) mtx_unlock(&(sbp)->mtx)
255 #define SBP_LOCK_ASSERT(sbp) mtx_assert(&(sbp)->mtx, MA_OWNED)
257 static void sbp_post_explore (void *);
258 static void sbp_recv (struct fw_xfer *);
259 static void sbp_mgm_callback (struct fw_xfer *);
261 static void sbp_cmd_callback (struct fw_xfer *);
263 static void sbp_orb_pointer (struct sbp_dev *, struct sbp_ocb *);
264 static void sbp_doorbell(struct sbp_dev *);
265 static void sbp_execute_ocb (void *, bus_dma_segment_t *, int, int);
266 static void sbp_free_ocb (struct sbp_dev *, struct sbp_ocb *);
267 static void sbp_abort_ocb (struct sbp_ocb *, int);
268 static void sbp_abort_all_ocbs (struct sbp_dev *, int);
269 static struct fw_xfer * sbp_write_cmd (struct sbp_dev *, int, int);
270 static struct sbp_ocb * sbp_get_ocb (struct sbp_dev *);
271 static struct sbp_ocb * sbp_enqueue_ocb (struct sbp_dev *, struct sbp_ocb *);
272 static struct sbp_ocb * sbp_dequeue_ocb (struct sbp_dev *, struct sbp_status *);
273 static void sbp_cam_detach_sdev(struct sbp_dev *);
274 static void sbp_free_sdev(struct sbp_dev *);
275 static void sbp_cam_detach_target (struct sbp_target *);
276 static void sbp_free_target (struct sbp_target *);
277 static void sbp_mgm_timeout (void *arg);
278 static void sbp_timeout (void *arg);
279 static void sbp_mgm_orb (struct sbp_dev *, int, struct sbp_ocb *);
281 static MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire");
283 /* cam related functions */
284 static void sbp_action(struct cam_sim *sim, union ccb *ccb);
285 static void sbp_poll(struct cam_sim *sim);
286 static void sbp_cam_scan_lun(struct cam_periph *, union ccb *);
287 static void sbp_cam_scan_target(void *arg);
289 static char *orb_status0[] = {
290 /* 0 */ "No additional information to report",
291 /* 1 */ "Request type not supported",
292 /* 2 */ "Speed not supported",
293 /* 3 */ "Page size not supported",
294 /* 4 */ "Access denied",
295 /* 5 */ "Logical unit not supported",
296 /* 6 */ "Maximum payload too small",
297 /* 7 */ "Reserved for future standardization",
298 /* 8 */ "Resources unavailable",
299 /* 9 */ "Function rejected",
300 /* A */ "Login ID not recognized",
301 /* B */ "Dummy ORB completed",
302 /* C */ "Request aborted",
303 /* FF */ "Unspecified error"
304 #define MAX_ORB_STATUS0 0xd
307 static char *orb_status1_object[] = {
308 /* 0 */ "Operation request block (ORB)",
309 /* 1 */ "Data buffer",
310 /* 2 */ "Page table",
311 /* 3 */ "Unable to specify"
314 static char *orb_status1_serial_bus_error[] = {
315 /* 0 */ "Missing acknowledge",
316 /* 1 */ "Reserved; not to be used",
317 /* 2 */ "Time-out error",
318 /* 3 */ "Reserved; not to be used",
319 /* 4 */ "Busy retry limit exceeded(X)",
320 /* 5 */ "Busy retry limit exceeded(A)",
321 /* 6 */ "Busy retry limit exceeded(B)",
322 /* 7 */ "Reserved for future standardization",
323 /* 8 */ "Reserved for future standardization",
324 /* 9 */ "Reserved for future standardization",
325 /* A */ "Reserved for future standardization",
326 /* B */ "Tardy retry limit exceeded",
327 /* C */ "Conflict error",
328 /* D */ "Data error",
329 /* E */ "Type error",
330 /* F */ "Address error"
334 sbp_identify(driver_t *driver, device_t parent)
337 printf("sbp_identify\n");
340 if (device_find_child(parent, "sbp", -1) == NULL)
341 BUS_ADD_CHILD(parent, 0, "sbp", -1);
348 sbp_probe(device_t dev)
352 printf("sbp_probe\n");
355 device_set_desc(dev, "SBP-2/SCSI over FireWire");
366 * Display device characteristics on the console
369 sbp_show_sdev_info(struct sbp_dev *sdev)
371 struct fw_device *fwdev;
373 fwdev = sdev->target->fwdev;
374 device_printf(sdev->target->sbp->fd.dev,
375 "%s: %s: ordered:%d type:%d EUI:%08x%08x node:%d "
376 "speed:%d maxrec:%d\n",
379 (sdev->type & 0x40) >> 6,
387 device_printf(sdev->target->sbp->fd.dev,
388 "%s: %s '%s' '%s' '%s'\n",
401 /* Bus Target EUI64 */
403 {0, 2, {0x00018ea0, 0x01fd0154}}, /* Logitec HDD */
404 {0, 0, {0x00018ea6, 0x00100682}}, /* Logitec DVD */
405 {0, 1, {0x00d03200, 0xa412006a}}, /* Yano HDD */
411 sbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev)
413 int bus, i, target=-1;
414 char w[SBP_NUM_TARGETS];
417 bus = device_get_unit(sbp->fd.dev);
419 /* XXX wired-down configuration should be gotten from
420 tunable or device hint */
421 for (i = 0; wired[i].bus >= 0; i ++) {
422 if (wired[i].bus == bus) {
423 w[wired[i].target] = 1;
424 if (wired[i].eui.hi == fwdev->eui.hi &&
425 wired[i].eui.lo == fwdev->eui.lo)
426 target = wired[i].target;
430 if(target < SBP_NUM_TARGETS &&
431 sbp->targets[target].fwdev == NULL)
433 device_printf(sbp->fd.dev,
434 "target %d is not free for %08x:%08x\n",
435 target, fwdev->eui.hi, fwdev->eui.lo);
438 /* non-wired target */
439 for (i = 0; i < SBP_NUM_TARGETS; i ++)
440 if (sbp->targets[i].fwdev == NULL && w[i] == 0) {
449 sbp_alloc_lun(struct sbp_target *target)
451 struct crom_context cc;
453 struct sbp_dev *sdev, **newluns;
454 struct sbp_softc *sbp;
458 SBP_LOCK_ASSERT(sbp);
459 crom_init_context(&cc, target->fwdev->csrrom);
460 /* XXX shoud parse appropriate unit directories only */
462 while (cc.depth >= 0) {
463 reg = crom_search_key(&cc, CROM_LUN);
466 lun = reg->val & 0xffff;
468 printf("target %d lun %d found\n", target->target_id, lun);
475 device_printf(target->sbp->fd.dev, "%d no LUN found\n",
479 if (maxlun >= SBP_NUM_LUNS)
480 maxlun = SBP_NUM_LUNS;
482 /* Invalidiate stale devices */
483 for (lun = 0; lun < target->num_lun; lun ++) {
484 sdev = target->luns[lun];
487 sdev->flags &= ~VALID_LUN;
490 sbp_cam_detach_sdev(sdev);
492 target->luns[lun] = NULL;
497 if (maxlun != target->num_lun) {
498 newluns = (struct sbp_dev **) realloc(target->luns,
499 sizeof(struct sbp_dev *) * maxlun,
500 M_SBP, M_NOWAIT | M_ZERO);
502 if (newluns == NULL) {
503 printf("%s: realloc failed\n", __func__);
504 newluns = target->luns;
505 maxlun = target->num_lun;
509 * We must zero the extended region for the case
510 * realloc() doesn't allocate new buffer.
512 if (maxlun > target->num_lun)
513 bzero(&newluns[target->num_lun],
514 sizeof(struct sbp_dev *) *
515 (maxlun - target->num_lun));
517 target->luns = newluns;
518 target->num_lun = maxlun;
521 crom_init_context(&cc, target->fwdev->csrrom);
522 while (cc.depth >= 0) {
525 reg = crom_search_key(&cc, CROM_LUN);
528 lun = reg->val & 0xffff;
529 if (lun >= SBP_NUM_LUNS) {
530 printf("too large lun %d\n", lun);
534 sdev = target->luns[lun];
536 sdev = malloc(sizeof(struct sbp_dev),
537 M_SBP, M_NOWAIT | M_ZERO);
539 printf("%s: malloc failed\n", __func__);
542 target->luns[lun] = sdev;
544 sdev->target = target;
545 STAILQ_INIT(&sdev->ocbs);
546 callout_init_mtx(&sdev->login_callout, &sbp->mtx, 0);
547 sdev->status = SBP_DEV_RESET;
549 snprintf(sdev->bustgtlun, 32, "%s:%d:%d",
550 device_get_nameunit(sdev->target->sbp->fd.dev),
551 sdev->target->target_id,
554 sdev->flags |= VALID_LUN;
555 sdev->type = (reg->val & 0xff0000) >> 16;
560 fwdma_malloc(sbp->fd.fc,
561 /* alignment */ sizeof(uint32_t),
562 SBP_DMA_SIZE, &sdev->dma, BUS_DMA_NOWAIT |
564 if (sdev->dma.v_addr == NULL) {
565 printf("%s: dma space allocation failed\n",
568 target->luns[lun] = NULL;
571 sdev->login = (struct sbp_login_res *) sdev->dma.v_addr;
572 sdev->ocb = (struct sbp_ocb *)
573 ((char *)sdev->dma.v_addr + SBP_LOGIN_SIZE);
574 bzero((char *)sdev->ocb,
575 sizeof (struct sbp_ocb) * SBP_QUEUE_LEN);
577 STAILQ_INIT(&sdev->free_ocbs);
578 for (i = 0; i < SBP_QUEUE_LEN; i++) {
581 ocb->bus_addr = sdev->dma.bus_addr
583 + sizeof(struct sbp_ocb) * i
584 + offsetof(struct sbp_ocb, orb[0]);
585 if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
586 printf("sbp_attach: cannot create dmamap\n");
590 callout_init_mtx(&ocb->timer, &sbp->mtx, 0);
591 sbp_free_ocb(sdev, ocb);
597 for (lun = 0; lun < target->num_lun; lun ++) {
598 sdev = target->luns[lun];
599 if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) {
600 sbp_cam_detach_sdev(sdev);
602 target->luns[lun] = NULL;
607 static struct sbp_target *
608 sbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
611 struct sbp_target *target;
612 struct crom_context cc;
616 printf("sbp_alloc_target\n");
618 i = sbp_new_target(sbp, fwdev);
620 device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n");
624 target = &sbp->targets[i];
625 target->fwdev = fwdev;
626 target->target_id = i;
627 /* XXX we may want to reload mgm port after each bus reset */
628 /* XXX there might be multiple management agents */
629 crom_init_context(&cc, target->fwdev->csrrom);
630 reg = crom_search_key(&cc, CROM_MGM);
631 if (reg == NULL || reg->val == 0) {
632 printf("NULL management address\n");
633 target->fwdev = NULL;
636 target->mgm_hi = 0xffff;
637 target->mgm_lo = 0xf0000000 | (reg->val << 2);
638 target->mgm_ocb_cur = NULL;
640 printf("target:%d mgm_port: %x\n", i, target->mgm_lo);
642 STAILQ_INIT(&target->xferlist);
644 STAILQ_INIT(&target->mgm_ocb_queue);
645 callout_init_mtx(&target->mgm_ocb_timeout, &sbp->mtx, 0);
646 callout_init_mtx(&target->scan_callout, &sbp->mtx, 0);
654 sbp_probe_lun(struct sbp_dev *sdev)
656 struct fw_device *fwdev;
657 struct crom_context c, *cc = &c;
660 bzero(sdev->vendor, sizeof(sdev->vendor));
661 bzero(sdev->product, sizeof(sdev->product));
663 fwdev = sdev->target->fwdev;
664 crom_init_context(cc, fwdev->csrrom);
665 /* get vendor string */
666 crom_search_key(cc, CSRKEY_VENDOR);
668 crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor));
669 /* skip to the unit directory for SBP-2 */
670 while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) {
671 if (reg->val == CSRVAL_T10SBP2)
675 /* get firmware revision */
676 reg = crom_search_key(cc, CSRKEY_FIRM_VER);
678 snprintf(sdev->revision, sizeof(sdev->revision),
680 /* get product string */
681 crom_search_key(cc, CSRKEY_MODEL);
683 crom_parse_text(cc, sdev->product, sizeof(sdev->product));
687 sbp_login_callout(void *arg)
689 struct sbp_dev *sdev = (struct sbp_dev *)arg;
690 SBP_LOCK_ASSERT(sdev->target->sbp);
691 sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
695 sbp_login(struct sbp_dev *sdev)
697 struct timeval delta;
702 timevalsub(&delta, &sdev->target->sbp->last_busreset);
703 t.tv_sec = login_delay / 1000;
704 t.tv_usec = (login_delay % 1000) * 1000;
705 timevalsub(&t, &delta);
706 if (t.tv_sec >= 0 && t.tv_usec > 0)
707 ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000;
709 printf("%s: sec = %jd usec = %ld ticks = %d\n", __func__,
710 (intmax_t)t.tv_sec, t.tv_usec, ticks);
712 callout_reset(&sdev->login_callout, ticks,
713 sbp_login_callout, (void *)(sdev));
716 #define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
717 && crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
720 sbp_probe_target(void *arg)
722 struct sbp_target *target = (struct sbp_target *)arg;
723 struct sbp_softc *sbp = target->sbp;
724 struct sbp_dev *sdev;
727 alive = SBP_FWDEV_ALIVE(target->fwdev);
729 device_printf(sbp->fd.dev, "%s %d%salive\n",
730 __func__, target->target_id,
731 (!alive) ? " not " : "");
735 SBP_LOCK_ASSERT(sbp);
736 sbp_alloc_lun(target);
738 /* XXX untimeout mgm_ocb and dequeue */
739 for (i=0; i < target->num_lun; i++) {
740 sdev = target->luns[i];
743 if (alive && (sdev->status != SBP_DEV_DEAD)) {
744 if (sdev->path != NULL) {
745 xpt_freeze_devq(sdev->path, 1);
749 sbp_show_sdev_info(sdev);
751 sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
752 switch (sdev->status) {
754 /* new or revived target */
758 case SBP_DEV_TOATTACH:
760 case SBP_DEV_ATTACHED:
763 sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
767 switch (sdev->status) {
768 case SBP_DEV_ATTACHED:
770 /* the device has gone */
771 device_printf(sbp->fd.dev, "%s: lost target\n",
775 xpt_freeze_devq(sdev->path, 1);
778 sdev->status = SBP_DEV_RETRY;
779 sbp_cam_detach_sdev(sdev);
781 target->luns[i] = NULL;
784 case SBP_DEV_TOATTACH:
785 sdev->status = SBP_DEV_RESET;
797 sbp_post_busreset(void *arg)
799 struct sbp_softc *sbp;
801 sbp = (struct sbp_softc *)arg;
803 printf("sbp_post_busreset\n");
806 if ((sbp->sim->flags & SIMQ_FREEZED) == 0) {
807 xpt_freeze_simq(sbp->sim, /*count*/1);
808 sbp->sim->flags |= SIMQ_FREEZED;
810 microtime(&sbp->last_busreset);
815 sbp_post_explore(void *arg)
817 struct sbp_softc *sbp = (struct sbp_softc *)arg;
818 struct sbp_target *target;
819 struct fw_device *fwdev;
823 printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
825 /* We need physical access */
826 if (!firewire_phydma_enable)
835 * XXX don't let CAM the bus rest.
836 * CAM tries to do something with freezed (DEV_RETRY) devices.
838 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
841 /* Garbage Collection */
842 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
843 target = &sbp->targets[i];
844 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
845 if (target->fwdev == NULL || target->fwdev == fwdev)
848 /* device has removed in lower driver */
849 sbp_cam_detach_target(target);
850 sbp_free_target(target);
853 /* traverse device list */
854 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
856 device_printf(sbp->fd.dev,"%s:: EUI:%08x%08x %s attached, state=%d\n",
857 __func__, fwdev->eui.hi, fwdev->eui.lo,
858 (fwdev->status != FWDEVATTACHED) ? "not" : "",
861 alive = SBP_FWDEV_ALIVE(fwdev);
862 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
863 target = &sbp->targets[i];
864 if(target->fwdev == fwdev ) {
869 if(i == SBP_NUM_TARGETS){
872 target = sbp_alloc_target(sbp, fwdev);
879 sbp_probe_target((void *)target);
880 if (target->num_lun == 0)
881 sbp_free_target(target);
883 xpt_release_simq(sbp->sim, /*run queue*/TRUE);
884 sbp->sim->flags &= ~SIMQ_FREEZED;
890 sbp_loginres_callback(struct fw_xfer *xfer){
891 struct sbp_dev *sdev;
892 sdev = (struct sbp_dev *)xfer->sc;
894 device_printf(sdev->target->sbp->fd.dev,"%s\n", __func__);
897 SBP_LOCK(sdev->target->sbp);
898 STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link);
899 SBP_UNLOCK(sdev->target->sbp);
905 sbp_xfer_free(struct fw_xfer *xfer)
907 struct sbp_dev *sdev;
909 sdev = (struct sbp_dev *)xfer->sc;
910 fw_xfer_unload(xfer);
911 SBP_LOCK_ASSERT(sdev->target->sbp);
912 STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
916 sbp_reset_start_callback(struct fw_xfer *xfer)
918 struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
919 struct sbp_target *target = sdev->target;
922 if (xfer->resp != 0) {
923 device_printf(sdev->target->sbp->fd.dev,
924 "%s: %s failed: resp=%d\n", __func__, sdev->bustgtlun, xfer->resp);
927 SBP_LOCK(target->sbp);
928 for (i = 0; i < target->num_lun; i++) {
929 tsdev = target->luns[i];
930 if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN)
933 SBP_UNLOCK(target->sbp);
937 sbp_reset_start(struct sbp_dev *sdev)
939 struct fw_xfer *xfer;
943 device_printf(sdev->target->sbp->fd.dev,
944 "%s:%s\n", __func__,sdev->bustgtlun);
947 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
948 xfer->hand = sbp_reset_start_callback;
949 fp = &xfer->send.hdr;
950 fp->mode.wreqq.dest_hi = 0xffff;
951 fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
952 fp->mode.wreqq.data = htonl(0xf);
953 fw_asyreq(xfer->fc, -1, xfer);
957 sbp_mgm_callback(struct fw_xfer *xfer)
959 struct sbp_dev *sdev;
962 sdev = (struct sbp_dev *)xfer->sc;
965 device_printf(sdev->target->sbp->fd.dev,
966 "%s:%s\n", __func__, sdev->bustgtlun);
969 SBP_LOCK(sdev->target->sbp);
971 SBP_UNLOCK(sdev->target->sbp);
974 static struct sbp_dev *
975 sbp_next_dev(struct sbp_target *target, int lun)
977 struct sbp_dev **sdevp;
980 for (i = lun, sdevp = &target->luns[lun]; i < target->num_lun;
982 if (*sdevp != NULL && (*sdevp)->status == SBP_DEV_PROBE)
989 sbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb)
991 struct sbp_target *target;
992 struct sbp_dev *sdev;
994 sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
995 target = sdev->target;
996 SBP_LOCK_ASSERT(target->sbp);
998 device_printf(sdev->target->sbp->fd.dev,
999 "%s:%s\n", __func__, sdev->bustgtlun);
1001 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1002 sdev->status = SBP_DEV_ATTACHED;
1004 device_printf(sdev->target->sbp->fd.dev,
1005 "%s:%s failed\n", __func__, sdev->bustgtlun);
1007 sdev = sbp_next_dev(target, sdev->lun_id + 1);
1013 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
1014 ccb->ccb_h.ccb_sdev_ptr = sdev;
1016 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1021 sbp_cam_scan_target(void *arg)
1023 struct sbp_target *target = (struct sbp_target *)arg;
1024 struct sbp_dev *sdev;
1027 SBP_LOCK_ASSERT(target->sbp);
1028 sdev = sbp_next_dev(target, 0);
1030 printf("sbp_cam_scan_target: nothing to do for target%d\n",
1035 device_printf(sdev->target->sbp->fd.dev,
1036 "%s:%s\n", __func__, sdev->bustgtlun);
1038 ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO);
1040 printf("sbp_cam_scan_target: malloc failed\n");
1043 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
1044 ccb->ccb_h.func_code = XPT_SCAN_LUN;
1045 ccb->ccb_h.cbfcnp = sbp_cam_scan_lun;
1046 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1047 ccb->crcn.flags = CAM_FLAG_NONE;
1048 ccb->ccb_h.ccb_sdev_ptr = sdev;
1050 /* The scan is in progress now. */
1052 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1056 static __inline void
1057 sbp_scan_dev(struct sbp_dev *sdev)
1059 sdev->status = SBP_DEV_PROBE;
1060 callout_reset(&sdev->target->scan_callout, scan_delay * hz / 1000,
1061 sbp_cam_scan_target, (void *)sdev->target);
1065 sbp_do_attach(struct fw_xfer *xfer)
1067 struct sbp_dev *sdev;
1068 struct sbp_target *target;
1069 struct sbp_softc *sbp;
1071 sdev = (struct sbp_dev *)xfer->sc;
1072 target = sdev->target;
1076 device_printf(sdev->target->sbp->fd.dev,
1077 "%s:%s\n", __func__, sdev->bustgtlun);
1079 sbp_xfer_free(xfer);
1081 if (sdev->path == NULL)
1082 xpt_create_path(&sdev->path, NULL,
1083 cam_sim_path(target->sbp->sim),
1084 target->target_id, sdev->lun_id);
1087 * Let CAM scan the bus if we are in the boot process.
1088 * XXX xpt_scan_bus cannot detect LUN larger than 0
1089 * if LUN 0 doesn't exist.
1092 sdev->status = SBP_DEV_ATTACHED;
1102 sbp_agent_reset_callback(struct fw_xfer *xfer)
1104 struct sbp_dev *sdev;
1106 sdev = (struct sbp_dev *)xfer->sc;
1108 device_printf(sdev->target->sbp->fd.dev,
1109 "%s:%s\n", __func__, sdev->bustgtlun);
1111 if (xfer->resp != 0) {
1112 device_printf(sdev->target->sbp->fd.dev,
1113 "%s:%s resp=%d\n", __func__, sdev->bustgtlun, xfer->resp);
1116 SBP_LOCK(sdev->target->sbp);
1117 sbp_xfer_free(xfer);
1119 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1122 SBP_UNLOCK(sdev->target->sbp);
1126 sbp_agent_reset(struct sbp_dev *sdev)
1128 struct fw_xfer *xfer;
1131 SBP_LOCK_ASSERT(sdev->target->sbp);
1133 device_printf(sdev->target->sbp->fd.dev,
1134 "%s:%s\n", __func__, sdev->bustgtlun);
1136 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1139 if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE)
1140 xfer->hand = sbp_agent_reset_callback;
1142 xfer->hand = sbp_do_attach;
1143 fp = &xfer->send.hdr;
1144 fp->mode.wreqq.data = htonl(0xf);
1145 fw_asyreq(xfer->fc, -1, xfer);
1146 sbp_abort_all_ocbs(sdev, CAM_BDR_SENT);
1150 sbp_busy_timeout_callback(struct fw_xfer *xfer)
1152 struct sbp_dev *sdev;
1154 sdev = (struct sbp_dev *)xfer->sc;
1156 device_printf(sdev->target->sbp->fd.dev,
1157 "%s:%s\n", __func__, sdev->bustgtlun);
1159 SBP_LOCK(sdev->target->sbp);
1160 sbp_xfer_free(xfer);
1161 sbp_agent_reset(sdev);
1162 SBP_UNLOCK(sdev->target->sbp);
1166 sbp_busy_timeout(struct sbp_dev *sdev)
1169 struct fw_xfer *xfer;
1171 device_printf(sdev->target->sbp->fd.dev,
1172 "%s:%s\n", __func__, sdev->bustgtlun);
1174 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1176 xfer->hand = sbp_busy_timeout_callback;
1177 fp = &xfer->send.hdr;
1178 fp->mode.wreqq.dest_hi = 0xffff;
1179 fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1180 fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1181 fw_asyreq(xfer->fc, -1, xfer);
1185 sbp_orb_pointer_callback(struct fw_xfer *xfer)
1187 struct sbp_dev *sdev;
1188 sdev = (struct sbp_dev *)xfer->sc;
1191 device_printf(sdev->target->sbp->fd.dev,
1192 "%s:%s\n", __func__, sdev->bustgtlun);
1194 if (xfer->resp != 0) {
1196 printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1198 SBP_LOCK(sdev->target->sbp);
1199 sbp_xfer_free(xfer);
1201 sdev->flags &= ~ORB_POINTER_ACTIVE;
1203 if ((sdev->flags & ORB_POINTER_NEED) != 0) {
1204 struct sbp_ocb *ocb;
1206 sdev->flags &= ~ORB_POINTER_NEED;
1207 ocb = STAILQ_FIRST(&sdev->ocbs);
1209 sbp_orb_pointer(sdev, ocb);
1211 SBP_UNLOCK(sdev->target->sbp);
1216 sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1218 struct fw_xfer *xfer;
1221 device_printf(sdev->target->sbp->fd.dev,
1223 __func__, sdev->bustgtlun,
1224 (uint32_t)ocb->bus_addr);
1227 SBP_LOCK_ASSERT(sdev->target->sbp);
1229 if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) {
1231 printf("%s: orb pointer active\n", __func__);
1233 sdev->flags |= ORB_POINTER_NEED;
1237 sdev->flags |= ORB_POINTER_ACTIVE;
1238 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1241 xfer->hand = sbp_orb_pointer_callback;
1243 fp = &xfer->send.hdr;
1244 fp->mode.wreqb.len = 8;
1245 fp->mode.wreqb.extcode = 0;
1246 xfer->send.payload[0] =
1247 htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1248 xfer->send.payload[1] = htonl((uint32_t)ocb->bus_addr);
1250 if (fw_asyreq(xfer->fc, -1, xfer) != 0) {
1251 sbp_xfer_free(xfer);
1252 ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1258 sbp_doorbell_callback(struct fw_xfer *xfer)
1260 struct sbp_dev *sdev;
1261 sdev = (struct sbp_dev *)xfer->sc;
1264 device_printf(sdev->target->sbp->fd.dev,
1265 "%s:%s\n", __func__, sdev->bustgtlun);
1267 if (xfer->resp != 0) {
1269 device_printf(sdev->target->sbp->fd.dev,
1270 "%s: xfer->resp = %d\n", __func__, xfer->resp);
1272 SBP_LOCK(sdev->target->sbp);
1273 sbp_xfer_free(xfer);
1274 sdev->flags &= ~ORB_DOORBELL_ACTIVE;
1275 if ((sdev->flags & ORB_DOORBELL_NEED) != 0) {
1276 sdev->flags &= ~ORB_DOORBELL_NEED;
1279 SBP_UNLOCK(sdev->target->sbp);
1283 sbp_doorbell(struct sbp_dev *sdev)
1285 struct fw_xfer *xfer;
1288 device_printf(sdev->target->sbp->fd.dev,
1289 "%s:%s\n", __func__, sdev->bustgtlun);
1292 if ((sdev->flags & ORB_DOORBELL_ACTIVE) != 0) {
1293 sdev->flags |= ORB_DOORBELL_NEED;
1296 sdev->flags |= ORB_DOORBELL_ACTIVE;
1297 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1300 xfer->hand = sbp_doorbell_callback;
1301 fp = &xfer->send.hdr;
1302 fp->mode.wreqq.data = htonl(0xf);
1303 fw_asyreq(xfer->fc, -1, xfer);
1306 static struct fw_xfer *
1307 sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1309 struct fw_xfer *xfer;
1311 struct sbp_target *target;
1314 SBP_LOCK_ASSERT(sdev->target->sbp);
1316 target = sdev->target;
1317 xfer = STAILQ_FIRST(&target->xferlist);
1319 if (target->n_xfer > 5 /* XXX */) {
1320 printf("sbp: no more xfer for this target\n");
1323 xfer = fw_xfer_alloc_buf(M_SBP, 8, 0);
1325 printf("sbp: fw_xfer_alloc_buf failed\n");
1330 printf("sbp: alloc %d xfer\n", target->n_xfer);
1333 STAILQ_REMOVE_HEAD(&target->xferlist, link);
1337 xfer->recv.pay_len = 0;
1338 xfer->send.spd = min(sdev->target->fwdev->speed, max_speed);
1339 xfer->fc = sdev->target->sbp->fd.fc;
1342 if (tcode == FWTCODE_WREQB)
1343 xfer->send.pay_len = 8;
1345 xfer->send.pay_len = 0;
1347 xfer->sc = (caddr_t)sdev;
1348 fp = &xfer->send.hdr;
1349 fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
1350 fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
1351 fp->mode.wreqq.tlrt = 0;
1352 fp->mode.wreqq.tcode = tcode;
1353 fp->mode.wreqq.pri = 0;
1354 fp->mode.wreqq.dst = FWLOCALBUS | sdev->target->fwdev->dst;
1360 sbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1362 struct fw_xfer *xfer;
1364 struct sbp_ocb *ocb;
1365 struct sbp_target *target;
1368 target = sdev->target;
1369 nid = target->sbp->fd.fc->nodeid | FWLOCALBUS;
1371 SBP_LOCK_ASSERT(target->sbp);
1372 if (func == ORB_FUN_RUNQUEUE) {
1373 ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1374 if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1377 STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1380 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
1384 ocb->flags = OCB_ACT_MGM;
1387 bzero((void *)ocb->orb, sizeof(ocb->orb));
1388 ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1389 ocb->orb[7] = htonl(SBP_DEV2ADDR(target->target_id, sdev->lun_id));
1392 device_printf(sdev->target->sbp->fd.dev,
1394 __func__,sdev->bustgtlun,
1395 orb_fun_name[(func>>16)&0xf]);
1399 ocb->orb[0] = ocb->orb[1] = 0; /* password */
1400 ocb->orb[2] = htonl(nid << 16);
1401 ocb->orb[3] = htonl(sdev->dma.bus_addr);
1402 ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id);
1404 ocb->orb[4] |= htonl(ORB_EXV);
1405 ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
1406 fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD);
1409 ocb->orb[0] = htonl((0 << 16) | 0);
1410 ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
1417 ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
1421 if (target->mgm_ocb_cur != NULL) {
1422 /* there is a standing ORB */
1423 STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1427 target->mgm_ocb_cur = ocb;
1429 callout_reset(&target->mgm_ocb_timeout, 5*hz,
1430 sbp_mgm_timeout, (caddr_t)ocb);
1431 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1435 xfer->hand = sbp_mgm_callback;
1437 fp = &xfer->send.hdr;
1438 fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
1439 fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
1440 fp->mode.wreqb.len = 8;
1441 fp->mode.wreqb.extcode = 0;
1442 xfer->send.payload[0] = htonl(nid << 16);
1443 xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff);
1445 fw_asyreq(xfer->fc, -1, xfer);
1449 sbp_print_scsi_cmd(struct sbp_ocb *ocb)
1451 struct ccb_scsiio *csio;
1453 csio = &ocb->ccb->csio;
1454 printf("%s:%d:%jx XPT_SCSI_IO: "
1455 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1457 "%db cmd/%db data/%db sense\n",
1458 device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1459 ocb->ccb->ccb_h.target_id,
1460 (uintmax_t)ocb->ccb->ccb_h.target_lun,
1461 csio->cdb_io.cdb_bytes[0],
1462 csio->cdb_io.cdb_bytes[1],
1463 csio->cdb_io.cdb_bytes[2],
1464 csio->cdb_io.cdb_bytes[3],
1465 csio->cdb_io.cdb_bytes[4],
1466 csio->cdb_io.cdb_bytes[5],
1467 csio->cdb_io.cdb_bytes[6],
1468 csio->cdb_io.cdb_bytes[7],
1469 csio->cdb_io.cdb_bytes[8],
1470 csio->cdb_io.cdb_bytes[9],
1471 ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1472 csio->cdb_len, csio->dxfer_len,
1477 sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1479 struct sbp_cmd_status *sbp_cmd_status;
1480 struct scsi_sense_data_fixed *sense;
1482 sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1483 sense = (struct scsi_sense_data_fixed *)&ocb->ccb->csio.sense_data;
1486 sbp_print_scsi_cmd(ocb);
1487 /* XXX need decode status */
1488 printf("%s: SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1489 ocb->sdev->bustgtlun,
1490 sbp_cmd_status->status,
1491 sbp_cmd_status->sfmt,
1492 sbp_cmd_status->valid,
1493 sbp_cmd_status->s_key,
1494 sbp_cmd_status->s_code,
1495 sbp_cmd_status->s_qlfr,
1499 switch (sbp_cmd_status->status) {
1500 case SCSI_STATUS_CHECK_COND:
1501 case SCSI_STATUS_BUSY:
1502 case SCSI_STATUS_CMD_TERMINATED:
1503 if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1504 sense->error_code = SSD_CURRENT_ERROR;
1506 sense->error_code = SSD_DEFERRED_ERROR;
1508 if(sbp_cmd_status->valid)
1509 sense->error_code |= SSD_ERRCODE_VALID;
1510 sense->flags = sbp_cmd_status->s_key;
1511 if(sbp_cmd_status->mark)
1512 sense->flags |= SSD_FILEMARK;
1513 if(sbp_cmd_status->eom)
1514 sense->flags |= SSD_EOM;
1515 if(sbp_cmd_status->ill_len)
1516 sense->flags |= SSD_ILI;
1518 bcopy(&sbp_cmd_status->info, &sense->info[0], 4);
1520 if (sbp_status->len <= 1)
1521 /* XXX not scsi status. shouldn't be happened */
1522 sense->extra_len = 0;
1523 else if (sbp_status->len <= 4)
1524 /* add_sense_code(_qual), info, cmd_spec_info */
1525 sense->extra_len = 6;
1527 /* fru, sense_key_spec */
1528 sense->extra_len = 10;
1530 bcopy(&sbp_cmd_status->cdb, &sense->cmd_spec_info[0], 4);
1532 sense->add_sense_code = sbp_cmd_status->s_code;
1533 sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1534 sense->fru = sbp_cmd_status->fru;
1536 bcopy(&sbp_cmd_status->s_keydep[0],
1537 &sense->sense_key_spec[0], 3);
1539 ocb->ccb->csio.scsi_status = sbp_cmd_status->status;
1540 ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1541 | CAM_AUTOSNS_VALID;
1546 for( j = 0 ; j < 32 ; j+=8){
1547 printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1548 tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1549 tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1556 device_printf(ocb->sdev->target->sbp->fd.dev,
1557 "%s:%s unknown scsi status 0x%x\n",
1558 __func__, ocb->sdev->bustgtlun,
1559 sbp_cmd_status->status);
1564 sbp_fix_inq_data(struct sbp_ocb *ocb)
1567 struct sbp_dev *sdev;
1568 struct scsi_inquiry_data *inq;
1573 if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1576 device_printf(sdev->target->sbp->fd.dev,
1577 "%s:%s\n", __func__, sdev->bustgtlun);
1579 inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1580 switch (SID_TYPE(inq)) {
1584 * XXX Convert Direct Access device to RBC.
1585 * I've never seen FireWire DA devices which support READ_6.
1587 if (SID_TYPE(inq) == T_DIRECT)
1588 inq->device |= T_RBC; /* T_DIRECT == 0 */
1593 * Override vendor/product/revision information.
1594 * Some devices sometimes return strange strings.
1597 bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1598 bcopy(sdev->product, inq->product, sizeof(inq->product));
1599 bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1604 * Force to enable/disable tagged queuing.
1605 * XXX CAM also checks SCP_QUEUE_DQUE flag in the control mode page.
1608 inq->flags |= SID_CmdQue;
1609 else if (sbp_tags < 0)
1610 inq->flags &= ~SID_CmdQue;
1615 sbp_recv1(struct fw_xfer *xfer)
1621 struct sbp_softc *sbp;
1622 struct sbp_dev *sdev;
1623 struct sbp_ocb *ocb;
1624 struct sbp_login_res *login_res = NULL;
1625 struct sbp_status *sbp_status;
1626 struct sbp_target *target;
1627 int orb_fun, status_valid0, status_valid, t, l, reset_agent = 0;
1631 ld = xfer->recv.buf;
1632 printf("sbp %x %d %d %08x %08x %08x %08x\n",
1633 xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1634 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1635 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1637 sbp = (struct sbp_softc *)xfer->sc;
1638 SBP_LOCK_ASSERT(sbp);
1639 if (xfer->resp != 0){
1640 printf("sbp_recv: xfer->resp = %d\n", xfer->resp);
1643 if (xfer->recv.payload == NULL){
1644 printf("sbp_recv: xfer->recv.payload == NULL\n");
1647 rfp = &xfer->recv.hdr;
1648 if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1649 printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1652 sbp_status = (struct sbp_status *)xfer->recv.payload;
1653 addr = rfp->mode.wreqb.dest_lo;
1655 printf("received address 0x%x\n", addr);
1657 t = SBP_ADDR2TRG(addr);
1658 if (t >= SBP_NUM_TARGETS) {
1659 device_printf(sbp->fd.dev,
1660 "sbp_recv1: invalid target %d\n", t);
1663 target = &sbp->targets[t];
1664 l = SBP_ADDR2LUN(addr);
1665 if (l >= target->num_lun || target->luns[l] == NULL) {
1666 device_printf(sbp->fd.dev,
1667 "sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1670 sdev = target->luns[l];
1673 switch (sbp_status->src) {
1676 /* check mgm_ocb_cur first */
1677 ocb = target->mgm_ocb_cur;
1679 if (OCB_MATCH(ocb, sbp_status)) {
1680 callout_stop(&target->mgm_ocb_timeout);
1681 target->mgm_ocb_cur = NULL;
1685 ocb = sbp_dequeue_ocb(sdev, sbp_status);
1687 device_printf(sdev->target->sbp->fd.dev,
1688 #if defined(__DragonFly__) || __FreeBSD_version < 500000
1689 "%s:%s No ocb(%lx) on the queue\n",
1691 "%s:%s No ocb(%x) on the queue\n",
1693 __func__,sdev->bustgtlun,
1694 ntohl(sbp_status->orb_lo));
1699 device_printf(sdev->target->sbp->fd.dev,
1700 "%s:%s unsolicit status received\n",
1701 __func__, sdev->bustgtlun);
1704 device_printf(sdev->target->sbp->fd.dev,
1705 "%s:%s unknown sbp_status->src\n",
1706 __func__, sdev->bustgtlun);
1709 status_valid0 = (sbp_status->src < 2
1710 && sbp_status->resp == ORB_RES_CMPL
1711 && sbp_status->dead == 0);
1712 status_valid = (status_valid0 && sbp_status->status == 0);
1714 if (!status_valid0 || debug > 2){
1717 device_printf(sdev->target->sbp->fd.dev,
1718 "%s:%s ORB status src:%x resp:%x dead:%x"
1719 #if defined(__DragonFly__) || __FreeBSD_version < 500000
1720 " len:%x stat:%x orb:%x%08lx\n",
1722 " len:%x stat:%x orb:%x%08x\n",
1724 __func__, sdev->bustgtlun,
1725 sbp_status->src, sbp_status->resp, sbp_status->dead,
1726 sbp_status->len, sbp_status->status,
1727 ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1729 device_printf(sdev->target->sbp->fd.dev,
1730 "%s\n", sdev->bustgtlun);
1731 status = sbp_status->status;
1732 switch(sbp_status->resp) {
1734 if (status > MAX_ORB_STATUS0)
1735 printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1737 printf("%s\n", orb_status0[status]);
1740 printf("Obj: %s, Error: %s\n",
1741 orb_status1_object[(status>>6) & 3],
1742 orb_status1_serial_bus_error[status & 0xf]);
1745 printf("Illegal request\n");
1748 printf("Vendor dependent\n");
1751 printf("unknown respose code %d\n", sbp_status->resp);
1755 /* we have to reset the fetch agent if it's dead */
1756 if (sbp_status->dead) {
1758 xpt_freeze_devq(sdev->path, 1);
1767 switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1773 switch(ocb->flags) {
1775 orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1779 fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD);
1780 login_res = sdev->login;
1781 login_res->len = ntohs(login_res->len);
1782 login_res->id = ntohs(login_res->id);
1783 login_res->cmd_hi = ntohs(login_res->cmd_hi);
1784 login_res->cmd_lo = ntohl(login_res->cmd_lo);
1787 device_printf(sdev->target->sbp->fd.dev,
1788 "%s:%s login: len %d, ID %d, cmd %08x%08x, recon_hold %d\n",
1789 __func__, sdev->bustgtlun,
1790 login_res->len, login_res->id,
1791 login_res->cmd_hi, login_res->cmd_lo,
1792 ntohs(login_res->recon_hold));
1794 sbp_busy_timeout(sdev);
1796 /* forgot logout? */
1797 device_printf(sdev->target->sbp->fd.dev,
1798 "%s:%s login failed\n",
1799 __func__, sdev->bustgtlun);
1800 sdev->status = SBP_DEV_RESET;
1804 login_res = sdev->login;
1807 device_printf(sdev->target->sbp->fd.dev,
1808 "%s:%s reconnect: len %d, ID %d, cmd %08x%08x\n",
1809 __func__, sdev->bustgtlun,
1810 login_res->len, login_res->id,
1811 login_res->cmd_hi, login_res->cmd_lo);
1813 if (sdev->status == SBP_DEV_ATTACHED)
1816 sbp_agent_reset(sdev);
1818 /* reconnection hold time exceed? */
1820 device_printf(sdev->target->sbp->fd.dev,
1821 "%s:%s reconnect failed\n",
1822 __func__, sdev->bustgtlun);
1828 sdev->status = SBP_DEV_RESET;
1831 sbp_busy_timeout(sdev);
1836 sbp_agent_reset(sdev);
1839 device_printf(sdev->target->sbp->fd.dev,
1840 "%s:%s unknown function %d\n",
1841 __func__, sdev->bustgtlun, orb_fun);
1844 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
1848 if(ocb->ccb != NULL){
1852 if(sbp_status->len > 1){
1853 sbp_scsi_status(sbp_status, ocb);
1855 if(sbp_status->resp != ORB_RES_CMPL){
1856 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1858 ccb->ccb_h.status = CAM_REQ_CMP;
1861 /* fix up inq data */
1862 if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1863 sbp_fix_inq_data(ocb);
1873 sbp_free_ocb(sdev, ocb);
1876 sbp_agent_reset(sdev);
1879 xfer->recv.pay_len = SBP_RECV_LEN;
1880 /* The received packet is usually small enough to be stored within
1881 * the buffer. In that case, the controller return ack_complete and
1882 * no respose is necessary.
1884 * XXX fwohci.c and firewire.c should inform event_code such as
1885 * ack_complete or ack_pending to upper driver.
1889 sfp = (struct fw_pkt *)xfer->send.buf;
1890 sfp->mode.wres.dst = rfp->mode.wreqb.src;
1891 xfer->dst = sfp->mode.wres.dst;
1892 xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1893 xfer->hand = sbp_loginres_callback;
1895 sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1896 sfp->mode.wres.tcode = FWTCODE_WRES;
1897 sfp->mode.wres.rtcode = 0;
1898 sfp->mode.wres.pri = 0;
1900 fw_asyreq(xfer->fc, -1, xfer);
1903 STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1908 sbp_recv(struct fw_xfer *xfer)
1910 struct sbp_softc *sbp;
1912 sbp = (struct sbp_softc *)xfer->sc;
1921 sbp_attach(device_t dev)
1923 struct sbp_softc *sbp;
1924 struct cam_devq *devq;
1925 struct firewire_comm *fc;
1928 if (DFLTPHYS > SBP_MAXPHYS)
1929 device_printf(dev, "Warning, DFLTPHYS(%dKB) is larger than "
1930 "SBP_MAXPHYS(%dKB).\n", DFLTPHYS / 1024,
1931 SBP_MAXPHYS / 1024);
1933 if (!firewire_phydma_enable)
1934 device_printf(dev, "Warning, hw.firewire.phydma_enable must be 1 "
1935 "for SBP over FireWire.\n");
1937 printf("sbp_attach (cold=%d)\n", cold);
1942 sbp = device_get_softc(dev);
1944 sbp->fd.fc = fc = device_get_ivars(dev);
1945 mtx_init(&sbp->mtx, "sbp", NULL, MTX_DEF);
1948 max_speed = fc->speed;
1950 error = bus_dma_tag_create(/*parent*/fc->dmat,
1951 /* XXX shoud be 4 for sane backend? */
1954 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1955 /*highaddr*/BUS_SPACE_MAXADDR,
1956 /*filter*/NULL, /*filterarg*/NULL,
1957 /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1958 /*maxsegsz*/SBP_SEG_MAX,
1959 /*flags*/BUS_DMA_ALLOCNOW,
1960 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
1961 /*lockfunc*/busdma_lock_mutex,
1962 /*lockarg*/&sbp->mtx,
1966 printf("sbp_attach: Could not allocate DMA tag "
1967 "- error %d\n", error);
1971 devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1975 for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1976 sbp->targets[i].fwdev = NULL;
1977 sbp->targets[i].luns = NULL;
1978 sbp->targets[i].sbp = sbp;
1981 sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1982 device_get_unit(dev),
1985 /*tagged*/ SBP_QUEUE_LEN - 1,
1988 if (sbp->sim == NULL) {
1989 cam_simq_free(devq);
1994 if (xpt_bus_register(sbp->sim, dev, /*bus*/0) != CAM_SUCCESS)
1997 if (xpt_create_path(&sbp->path, NULL, cam_sim_path(sbp->sim),
1998 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1999 xpt_bus_deregister(cam_sim_path(sbp->sim));
2004 /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
2005 sbp->fwb.start = ((u_int64_t)SBP_BIND_HI << 32) | SBP_DEV2ADDR(0, 0);
2006 sbp->fwb.end = sbp->fwb.start + 0xffff;
2007 /* pre-allocate xfer */
2008 STAILQ_INIT(&sbp->fwb.xferlist);
2009 fw_xferlist_add(&sbp->fwb.xferlist, M_SBP,
2010 /*send*/ 0, /*recv*/ SBP_RECV_LEN, SBP_NUM_OCB/2,
2011 fc, (void *)sbp, sbp_recv);
2013 fw_bindadd(fc, &sbp->fwb);
2015 sbp->fd.post_busreset = sbp_post_busreset;
2016 sbp->fd.post_explore = sbp_post_explore;
2018 if (fc->status != -1) {
2019 sbp_post_busreset((void *)sbp);
2020 sbp_post_explore((void *)sbp);
2023 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
2029 cam_sim_free(sbp->sim, /*free_devq*/TRUE);
2034 sbp_logout_all(struct sbp_softc *sbp)
2036 struct sbp_target *target;
2037 struct sbp_dev *sdev;
2041 printf("sbp_logout_all\n");
2043 SBP_LOCK_ASSERT(sbp);
2044 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
2045 target = &sbp->targets[i];
2046 if (target->luns == NULL)
2048 for (j = 0; j < target->num_lun; j++) {
2049 sdev = target->luns[j];
2052 callout_stop(&sdev->login_callout);
2053 if (sdev->status >= SBP_DEV_TOATTACH &&
2054 sdev->status <= SBP_DEV_ATTACHED)
2055 sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
2063 sbp_shutdown(device_t dev)
2065 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2068 sbp_logout_all(sbp);
2074 sbp_free_sdev(struct sbp_dev *sdev)
2076 struct sbp_softc *sbp;
2081 sbp = sdev->target->sbp;
2083 callout_drain(&sdev->login_callout);
2084 for (i = 0; i < SBP_QUEUE_LEN; i++) {
2085 callout_drain(&sdev->ocb[i].timer);
2086 bus_dmamap_destroy(sbp->dmat, sdev->ocb[i].dmamap);
2088 fwdma_free(sbp->fd.fc, &sdev->dma);
2094 sbp_free_target(struct sbp_target *target)
2096 struct sbp_softc *sbp;
2097 struct fw_xfer *xfer, *next;
2100 if (target->luns == NULL)
2103 SBP_LOCK_ASSERT(sbp);
2105 callout_drain(&target->mgm_ocb_timeout);
2106 callout_drain(&target->scan_callout);
2108 for (i = 0; i < target->num_lun; i++)
2109 sbp_free_sdev(target->luns[i]);
2111 STAILQ_FOREACH_SAFE(xfer, &target->xferlist, link, next) {
2112 fw_xfer_free_buf(xfer);
2114 STAILQ_INIT(&target->xferlist);
2115 free(target->luns, M_SBP);
2116 target->num_lun = 0;
2117 target->luns = NULL;
2118 target->fwdev = NULL;
2122 sbp_detach(device_t dev)
2124 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2125 struct firewire_comm *fc = sbp->fd.fc;
2129 printf("sbp_detach\n");
2133 for (i = 0; i < SBP_NUM_TARGETS; i ++)
2134 sbp_cam_detach_target(&sbp->targets[i]);
2136 xpt_async(AC_LOST_DEVICE, sbp->path, NULL);
2137 xpt_free_path(sbp->path);
2138 xpt_bus_deregister(cam_sim_path(sbp->sim));
2139 cam_sim_free(sbp->sim, /*free_devq*/ TRUE);
2141 sbp_logout_all(sbp);
2144 /* XXX wait for logout completion */
2145 pause("sbpdtc", hz/2);
2148 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++)
2149 sbp_free_target(&sbp->targets[i]);
2152 fw_bindremove(fc, &sbp->fwb);
2153 fw_xferlist_remove(&sbp->fwb.xferlist);
2155 bus_dma_tag_destroy(sbp->dmat);
2156 mtx_destroy(&sbp->mtx);
2162 sbp_cam_detach_sdev(struct sbp_dev *sdev)
2166 if (sdev->status == SBP_DEV_DEAD)
2168 if (sdev->status == SBP_DEV_RESET)
2170 SBP_LOCK_ASSERT(sdev->target->sbp);
2171 sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
2173 xpt_release_devq(sdev->path,
2174 sdev->freeze, TRUE);
2176 xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
2177 xpt_free_path(sdev->path);
2183 sbp_cam_detach_target(struct sbp_target *target)
2187 SBP_LOCK_ASSERT(target->sbp);
2188 if (target->luns != NULL) {
2190 printf("sbp_detach_target %d\n", target->target_id);
2192 callout_stop(&target->scan_callout);
2193 for (i = 0; i < target->num_lun; i++)
2194 sbp_cam_detach_sdev(target->luns[i]);
2199 sbp_target_reset(struct sbp_dev *sdev, int method)
2202 struct sbp_target *target = sdev->target;
2203 struct sbp_dev *tsdev;
2205 SBP_LOCK_ASSERT(target->sbp);
2206 for (i = 0; i < target->num_lun; i++) {
2207 tsdev = target->luns[i];
2210 if (tsdev->status == SBP_DEV_DEAD)
2212 if (tsdev->status == SBP_DEV_RESET)
2214 xpt_freeze_devq(tsdev->path, 1);
2216 sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT);
2218 tsdev->status = SBP_DEV_LOGIN;
2222 printf("target reset\n");
2223 sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2226 printf("reset start\n");
2227 sbp_reset_start(sdev);
2234 sbp_mgm_timeout(void *arg)
2236 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2237 struct sbp_dev *sdev = ocb->sdev;
2238 struct sbp_target *target = sdev->target;
2240 SBP_LOCK_ASSERT(target->sbp);
2241 device_printf(sdev->target->sbp->fd.dev,
2242 "%s:%s request timeout(mgm orb:0x%08x)\n",
2243 __func__, sdev->bustgtlun, (uint32_t)ocb->bus_addr);
2244 target->mgm_ocb_cur = NULL;
2245 sbp_free_ocb(sdev, ocb);
2248 printf("run next request\n");
2249 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2251 device_printf(sdev->target->sbp->fd.dev,
2252 "%s:%s reset start\n",
2253 __func__, sdev->bustgtlun);
2254 sbp_reset_start(sdev);
2258 sbp_timeout(void *arg)
2260 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2261 struct sbp_dev *sdev = ocb->sdev;
2263 device_printf(sdev->target->sbp->fd.dev,
2264 "%s:%s request timeout(cmd orb:0x%08x) ... ",
2265 __func__, sdev->bustgtlun, (uint32_t)ocb->bus_addr);
2267 SBP_LOCK_ASSERT(sdev->target->sbp);
2269 switch(sdev->timeout) {
2271 printf("agent reset\n");
2272 xpt_freeze_devq(sdev->path, 1);
2274 sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2275 sbp_agent_reset(sdev);
2279 sbp_target_reset(sdev, sdev->timeout - 1);
2284 sbp_cam_detach_target(target);
2285 if (target->luns != NULL)
2286 free(target->luns, M_SBP);
2287 target->num_lun = 0;
2288 target->luns = NULL;
2289 target->fwdev = NULL;
2295 sbp_action(struct cam_sim *sim, union ccb *ccb)
2298 struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2299 struct sbp_target *target = NULL;
2300 struct sbp_dev *sdev = NULL;
2303 SBP_LOCK_ASSERT(sbp);
2304 /* target:lun -> sdev mapping */
2306 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2307 && ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2308 target = &sbp->targets[ccb->ccb_h.target_id];
2309 if (target->fwdev != NULL
2310 && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2311 && ccb->ccb_h.target_lun < target->num_lun) {
2312 sdev = target->luns[ccb->ccb_h.target_lun];
2313 if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED &&
2314 sdev->status != SBP_DEV_PROBE)
2321 printf("invalid target %d lun %jx\n",
2322 ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun);
2325 switch (ccb->ccb_h.func_code) {
2328 case XPT_GET_TRAN_SETTINGS:
2329 case XPT_SET_TRAN_SETTINGS:
2330 case XPT_CALC_GEOMETRY:
2333 printf("%s:%d:%jx:func_code 0x%04x: "
2334 "Invalid target (target needed)\n",
2335 device_get_nameunit(sbp->fd.dev),
2336 ccb->ccb_h.target_id,
2337 (uintmax_t)ccb->ccb_h.target_lun,
2338 ccb->ccb_h.func_code);
2341 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2348 /* The opcodes sometimes aimed at a target (sc is valid),
2349 * sometimes aimed at the SIM (sc is invalid and target is
2350 * CAM_TARGET_WILDCARD)
2353 ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2355 printf("%s:%d:%jx func_code 0x%04x: "
2356 "Invalid target (no wildcard)\n",
2357 device_get_nameunit(sbp->fd.dev),
2358 ccb->ccb_h.target_id,
2359 (uintmax_t)ccb->ccb_h.target_lun,
2360 ccb->ccb_h.func_code);
2362 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2368 /* XXX Hm, we should check the input parameters */
2372 switch (ccb->ccb_h.func_code) {
2375 struct ccb_scsiio *csio;
2376 struct sbp_ocb *ocb;
2381 mtx_assert(sim->mtx, MA_OWNED);
2384 printf("%s:%d:%jx XPT_SCSI_IO: "
2385 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2387 "%db cmd/%db data/%db sense\n",
2388 device_get_nameunit(sbp->fd.dev),
2389 ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun,
2390 csio->cdb_io.cdb_bytes[0],
2391 csio->cdb_io.cdb_bytes[1],
2392 csio->cdb_io.cdb_bytes[2],
2393 csio->cdb_io.cdb_bytes[3],
2394 csio->cdb_io.cdb_bytes[4],
2395 csio->cdb_io.cdb_bytes[5],
2396 csio->cdb_io.cdb_bytes[6],
2397 csio->cdb_io.cdb_bytes[7],
2398 csio->cdb_io.cdb_bytes[8],
2399 csio->cdb_io.cdb_bytes[9],
2400 ccb->ccb_h.flags & CAM_DIR_MASK,
2401 csio->cdb_len, csio->dxfer_len,
2405 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2410 /* if we are in probe stage, pass only probe commands */
2411 if (sdev->status == SBP_DEV_PROBE) {
2413 name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2414 printf("probe stage, periph name: %s\n", name);
2415 if (strcmp(name, "probe") != 0) {
2416 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2422 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
2423 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2424 if (sdev->freeze == 0) {
2425 xpt_freeze_devq(sdev->path, 1);
2432 ocb->flags = OCB_ACT_CMD;
2435 ccb->ccb_h.ccb_sdev_ptr = sdev;
2436 ocb->orb[0] = htonl(1U << 31);
2438 ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2439 ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2440 speed = min(target->fwdev->speed, max_speed);
2441 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2442 | ORB_CMD_MAXP(speed + 7));
2443 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
2444 ocb->orb[4] |= htonl(ORB_CMD_IN);
2447 if (csio->ccb_h.flags & CAM_CDB_POINTER)
2448 cdb = (void *)csio->cdb_io.cdb_ptr;
2450 cdb = (void *)&csio->cdb_io.cdb_bytes;
2451 bcopy(cdb, (void *)&ocb->orb[5], csio->cdb_len);
2453 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2454 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2456 if (ccb->csio.dxfer_len > 0) {
2459 error = bus_dmamap_load_ccb(/*dma tag*/sbp->dmat,
2460 /*dma map*/ocb->dmamap,
2466 printf("sbp: bus_dmamap_load error %d\n", error);
2468 sbp_execute_ocb(ocb, NULL, 0, 0);
2471 case XPT_CALC_GEOMETRY:
2473 struct ccb_calc_geometry *ccg;
2474 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2476 uint32_t secs_per_cylinder;
2481 if (ccg->block_size == 0) {
2482 printf("sbp_action: block_size is 0.\n");
2483 ccb->ccb_h.status = CAM_REQ_INVALID;
2488 printf("%s:%d:%d:%jx:XPT_CALC_GEOMETRY: "
2489 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2490 "Volume size = %d\n",
2492 "Volume size = %jd\n",
2494 device_get_nameunit(sbp->fd.dev),
2495 cam_sim_path(sbp->sim),
2496 ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun,
2497 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2503 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2504 size_mb = ccg->volume_size
2505 / ((1024L * 1024L) / ccg->block_size);
2507 if (size_mb > 1024 && extended) {
2509 ccg->secs_per_track = 63;
2512 ccg->secs_per_track = 32;
2514 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2515 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2516 ccb->ccb_h.status = CAM_REQ_CMP;
2518 cam_calc_geometry(ccg, /*extended*/1);
2523 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
2527 printf("%s:%d:XPT_RESET_BUS: \n",
2528 device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2531 ccb->ccb_h.status = CAM_REQ_INVALID;
2535 case XPT_PATH_INQ: /* Path routing inquiry */
2537 struct ccb_pathinq *cpi = &ccb->cpi;
2540 printf("%s:%d:%jx XPT_PATH_INQ:.\n",
2541 device_get_nameunit(sbp->fd.dev),
2542 ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun);
2544 cpi->version_num = 1; /* XXX??? */
2545 cpi->hba_inquiry = PI_TAG_ABLE;
2546 cpi->target_sprt = 0;
2547 cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE;
2548 cpi->hba_eng_cnt = 0;
2549 cpi->max_target = SBP_NUM_TARGETS - 1;
2550 cpi->max_lun = SBP_NUM_LUNS - 1;
2551 cpi->initiator_id = SBP_INITIATOR;
2552 cpi->bus_id = sim->bus_id;
2553 cpi->base_transfer_speed = 400 * 1000 / 8;
2554 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2555 strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2556 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2557 cpi->unit_number = sim->unit_number;
2558 cpi->transport = XPORT_SPI; /* XX should have a FireWire */
2559 cpi->transport_version = 2;
2560 cpi->protocol = PROTO_SCSI;
2561 cpi->protocol_version = SCSI_REV_2;
2563 cpi->ccb_h.status = CAM_REQ_CMP;
2567 case XPT_GET_TRAN_SETTINGS:
2569 struct ccb_trans_settings *cts = &ccb->cts;
2570 struct ccb_trans_settings_scsi *scsi =
2571 &cts->proto_specific.scsi;
2572 struct ccb_trans_settings_spi *spi =
2573 &cts->xport_specific.spi;
2575 cts->protocol = PROTO_SCSI;
2576 cts->protocol_version = SCSI_REV_2;
2577 cts->transport = XPORT_SPI; /* should have a FireWire */
2578 cts->transport_version = 2;
2579 spi->valid = CTS_SPI_VALID_DISC;
2580 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
2581 scsi->valid = CTS_SCSI_VALID_TQ;
2582 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2584 printf("%s:%d:%jx XPT_GET_TRAN_SETTINGS:.\n",
2585 device_get_nameunit(sbp->fd.dev),
2586 ccb->ccb_h.target_id, (uintmax_t)ccb->ccb_h.target_lun);
2588 cts->ccb_h.status = CAM_REQ_CMP;
2593 ccb->ccb_h.status = CAM_UA_ABORT;
2596 case XPT_SET_TRAN_SETTINGS:
2599 ccb->ccb_h.status = CAM_REQ_INVALID;
2607 sbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error)
2610 struct sbp_ocb *ocb;
2611 struct sbp_ocb *prev;
2612 bus_dma_segment_t *s;
2615 printf("sbp_execute_ocb: error=%d\n", error);
2617 ocb = (struct sbp_ocb *)arg;
2620 printf("sbp_execute_ocb: seg %d", seg);
2621 for (i = 0; i < seg; i++)
2622 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2623 printf(", %x:%d", segments[i].ds_addr, segments[i].ds_len);
2625 printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2626 (uintmax_t)segments[i].ds_len);
2632 /* direct pointer */
2634 if (s->ds_len > SBP_SEG_MAX)
2635 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2636 ocb->orb[3] = htonl(s->ds_addr);
2637 ocb->orb[4] |= htonl(s->ds_len);
2638 } else if(seg > 1) {
2640 for (i = 0; i < seg; i++) {
2643 /* XXX LSI Logic "< 16 byte" bug might be hit */
2645 printf("sbp_execute_ocb: warning, "
2646 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2647 "segment length(%d) is less than 16."
2649 "segment length(%zd) is less than 16."
2651 "(seg=%d/%d)\n", (size_t)s->ds_len, i+1, seg);
2653 if (s->ds_len > SBP_SEG_MAX)
2654 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2655 ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2656 ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2658 ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2662 bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2663 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2664 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2665 prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2666 fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2669 if (ocb->sdev->last_ocb != NULL)
2670 sbp_doorbell(ocb->sdev);
2672 sbp_orb_pointer(ocb->sdev, ocb);
2675 if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) {
2676 ocb->sdev->flags &= ~ORB_LINK_DEAD;
2677 sbp_orb_pointer(ocb->sdev, ocb);
2683 sbp_poll(struct cam_sim *sim)
2685 struct sbp_softc *sbp;
2686 struct firewire_comm *fc;
2688 sbp = (struct sbp_softc *)sim->softc;
2691 fc->poll(fc, 0, -1);
2696 static struct sbp_ocb *
2697 sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2699 struct sbp_ocb *ocb;
2700 struct sbp_ocb *next;
2704 device_printf(sdev->target->sbp->fd.dev,
2705 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2706 "%s:%s 0x%08lx src %d\n",
2708 "%s:%s 0x%08x src %d\n",
2710 __func__, sdev->bustgtlun, ntohl(sbp_status->orb_lo), sbp_status->src);
2712 SBP_LOCK_ASSERT(sdev->target->sbp);
2713 STAILQ_FOREACH_SAFE(ocb, &sdev->ocbs, ocb, next) {
2714 if (OCB_MATCH(ocb, sbp_status)) {
2716 STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2717 if (ocb->ccb != NULL)
2718 callout_stop(&ocb->timer);
2719 if (ntohl(ocb->orb[4]) & 0xffff) {
2720 bus_dmamap_sync(sdev->target->sbp->dmat,
2722 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2723 BUS_DMASYNC_POSTREAD :
2724 BUS_DMASYNC_POSTWRITE);
2725 bus_dmamap_unload(sdev->target->sbp->dmat,
2728 if (!use_doorbell) {
2729 if (sbp_status->src == SRC_NO_NEXT) {
2731 sbp_orb_pointer(sdev, next);
2732 else if (order > 0) {
2734 * Unordered execution
2735 * We need to send pointer for
2738 sdev->flags |= ORB_LINK_DEAD;
2743 * XXX this is not correct for unordered
2746 if (sdev->last_ocb != NULL) {
2747 sbp_free_ocb(sdev, sdev->last_ocb);
2749 sdev->last_ocb = ocb;
2751 sbp_status->src == SRC_NO_NEXT)
2759 if (ocb && order > 0) {
2760 device_printf(sdev->target->sbp->fd.dev,
2761 "%s:%s unordered execution order:%d\n",
2762 __func__, sdev->bustgtlun, order);
2768 static struct sbp_ocb *
2769 sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2771 struct sbp_ocb *prev, *prev2;
2773 SBP_LOCK_ASSERT(sdev->target->sbp);
2775 device_printf(sdev->target->sbp->fd.dev,
2776 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2777 "%s:%s 0x%08x\n", __func__, sdev->bustgtlun, ocb->bus_addr);
2779 "%s:%s 0x%08jx\n", __func__, sdev->bustgtlun, (uintmax_t)ocb->bus_addr);
2782 prev2 = prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2783 STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2785 if (ocb->ccb != NULL)
2786 callout_reset(&ocb->timer, (ocb->ccb->ccb_h.timeout * hz) / 1000,
2789 if (use_doorbell && prev == NULL)
2790 prev2 = sdev->last_ocb;
2792 if (prev2 != NULL && (ocb->sdev->flags & ORB_LINK_DEAD) == 0) {
2794 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2795 printf("linking chain 0x%x -> 0x%x\n",
2796 prev2->bus_addr, ocb->bus_addr);
2798 printf("linking chain 0x%jx -> 0x%jx\n",
2799 (uintmax_t)prev2->bus_addr, (uintmax_t)ocb->bus_addr);
2803 * Suppress compiler optimization so that orb[1] must be written first.
2804 * XXX We may need an explicit memory barrier for other architectures
2805 * other than i386/amd64.
2807 *(volatile uint32_t *)&prev2->orb[1] = htonl(ocb->bus_addr);
2808 *(volatile uint32_t *)&prev2->orb[0] = 0;
2814 static struct sbp_ocb *
2815 sbp_get_ocb(struct sbp_dev *sdev)
2817 struct sbp_ocb *ocb;
2819 SBP_LOCK_ASSERT(sdev->target->sbp);
2820 ocb = STAILQ_FIRST(&sdev->free_ocbs);
2822 sdev->flags |= ORB_SHORTAGE;
2823 printf("ocb shortage!!!\n");
2826 STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2832 sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2837 SBP_LOCK_ASSERT(sdev->target->sbp);
2838 STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2839 if ((sdev->flags & ORB_SHORTAGE) != 0) {
2842 sdev->flags &= ~ORB_SHORTAGE;
2843 count = sdev->freeze;
2845 xpt_release_devq(sdev->path, count, TRUE);
2850 sbp_abort_ocb(struct sbp_ocb *ocb, int status)
2852 struct sbp_dev *sdev;
2855 SBP_LOCK_ASSERT(sdev->target->sbp);
2857 device_printf(sdev->target->sbp->fd.dev,
2858 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2859 "%s:%s 0x%x\n", __func__, sdev->bustgtlun, ocb->bus_addr);
2861 "%s:%s 0x%jx\n", __func__, sdev->bustgtlun, (uintmax_t)ocb->bus_addr);
2865 if (ocb->ccb != NULL)
2866 sbp_print_scsi_cmd(ocb);
2868 if (ntohl(ocb->orb[4]) & 0xffff) {
2869 bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
2870 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2871 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2872 bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
2874 if (ocb->ccb != NULL) {
2875 callout_stop(&ocb->timer);
2876 ocb->ccb->ccb_h.status = status;
2879 sbp_free_ocb(sdev, ocb);
2883 sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2885 struct sbp_ocb *ocb, *next;
2886 STAILQ_HEAD(, sbp_ocb) temp;
2889 SBP_LOCK_ASSERT(sdev->target->sbp);
2890 STAILQ_CONCAT(&temp, &sdev->ocbs);
2891 STAILQ_INIT(&sdev->ocbs);
2893 STAILQ_FOREACH_SAFE(ocb, &temp, ocb, next) {
2894 sbp_abort_ocb(ocb, status);
2896 if (sdev->last_ocb != NULL) {
2897 sbp_free_ocb(sdev, sdev->last_ocb);
2898 sdev->last_ocb = NULL;
2902 static devclass_t sbp_devclass;
2904 static device_method_t sbp_methods[] = {
2905 /* device interface */
2906 DEVMETHOD(device_identify, sbp_identify),
2907 DEVMETHOD(device_probe, sbp_probe),
2908 DEVMETHOD(device_attach, sbp_attach),
2909 DEVMETHOD(device_detach, sbp_detach),
2910 DEVMETHOD(device_shutdown, sbp_shutdown),
2915 static driver_t sbp_driver = {
2918 sizeof(struct sbp_softc),
2920 #ifdef __DragonFly__
2921 DECLARE_DUMMY_MODULE(sbp);
2923 DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2924 MODULE_VERSION(sbp, 1);
2925 MODULE_DEPEND(sbp, firewire, 1, 1, 1);
2926 MODULE_DEPEND(sbp, cam, 1, 1, 1);