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 SYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem");
136 SYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0,
138 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0,
139 "SBP perform login automatically");
140 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0,
141 "SBP transfer max speed");
142 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, exclusive_login, CTLFLAG_RW,
143 &ex_login, 0, "SBP enable exclusive login");
144 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, login_delay, CTLFLAG_RW,
145 &login_delay, 0, "SBP login delay in msec");
146 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, scan_delay, CTLFLAG_RW,
147 &scan_delay, 0, "SBP scan delay in msec");
148 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, use_doorbell, CTLFLAG_RW,
149 &use_doorbell, 0, "SBP use doorbell request");
150 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, tags, CTLFLAG_RW, &sbp_tags, 0,
151 "SBP tagged queuing support");
153 TUNABLE_INT("hw.firewire.sbp.auto_login", &auto_login);
154 TUNABLE_INT("hw.firewire.sbp.max_speed", &max_speed);
155 TUNABLE_INT("hw.firewire.sbp.exclusive_login", &ex_login);
156 TUNABLE_INT("hw.firewire.sbp.login_delay", &login_delay);
157 TUNABLE_INT("hw.firewire.sbp.scan_delay", &scan_delay);
158 TUNABLE_INT("hw.firewire.sbp.use_doorbell", &use_doorbell);
159 TUNABLE_INT("hw.firewire.sbp.tags", &sbp_tags);
161 #define NEED_RESPONSE 0
163 #define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE)
164 #ifdef __sparc64__ /* iommu */
165 #define SBP_IND_MAX howmany(SBP_MAXPHYS, SBP_SEG_MAX)
167 #define SBP_IND_MAX howmany(SBP_MAXPHYS, PAGE_SIZE)
170 STAILQ_ENTRY(sbp_ocb) ocb;
174 #define IND_PTR_OFFSET (8*sizeof(uint32_t))
175 struct ind_ptr ind_ptr[SBP_IND_MAX];
176 struct sbp_dev *sdev;
177 int flags; /* XXX should be removed */
181 #define OCB_ACT_MGM 0
182 #define OCB_ACT_CMD 1
183 #define OCB_MATCH(o,s) ((o)->bus_addr == ntohl((s)->orb_lo))
186 #define SBP_DEV_RESET 0 /* accept login */
187 #define SBP_DEV_LOGIN 1 /* to login */
189 #define SBP_DEV_RECONN 2 /* to reconnect */
191 #define SBP_DEV_TOATTACH 3 /* to attach */
192 #define SBP_DEV_PROBE 4 /* scan lun */
193 #define SBP_DEV_ATTACHED 5 /* in operation */
194 #define SBP_DEV_DEAD 6 /* unavailable unit */
195 #define SBP_DEV_RETRY 7 /* unavailable unit */
201 #define ORB_LINK_DEAD (1 << 0)
202 #define VALID_LUN (1 << 1)
203 #define ORB_POINTER_ACTIVE (1 << 2)
204 #define ORB_POINTER_NEED (1 << 3)
205 #define ORB_DOORBELL_ACTIVE (1 << 4)
206 #define ORB_DOORBELL_NEED (1 << 5)
207 #define ORB_SHORTAGE (1 << 6)
209 struct cam_path *path;
210 struct sbp_target *target;
211 struct fwdma_alloc dma;
212 struct sbp_login_res *login;
213 struct callout login_callout;
215 STAILQ_HEAD(, sbp_ocb) ocbs;
216 STAILQ_HEAD(, sbp_ocb) free_ocbs;
217 struct sbp_ocb *last_ocb;
227 struct sbp_dev **luns;
228 struct sbp_softc *sbp;
229 struct fw_device *fwdev;
230 uint32_t mgm_hi, mgm_lo;
231 struct sbp_ocb *mgm_ocb_cur;
232 STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue;
233 struct callout mgm_ocb_timeout;
234 struct callout scan_callout;
235 STAILQ_HEAD(, fw_xfer) xferlist;
240 struct firewire_dev_comm fd;
242 struct cam_path *path;
243 struct sbp_target targets[SBP_NUM_TARGETS];
246 struct timeval last_busreset;
247 #define SIMQ_FREEZED 1
251 #define SBP_LOCK(sbp) mtx_lock(&(sbp)->mtx)
252 #define SBP_UNLOCK(sbp) mtx_unlock(&(sbp)->mtx)
254 static void sbp_post_explore (void *);
255 static void sbp_recv (struct fw_xfer *);
256 static void sbp_mgm_callback (struct fw_xfer *);
258 static void sbp_cmd_callback (struct fw_xfer *);
260 static void sbp_orb_pointer (struct sbp_dev *, struct sbp_ocb *);
261 static void sbp_doorbell(struct sbp_dev *);
262 static void sbp_execute_ocb (void *, bus_dma_segment_t *, int, int);
263 static void sbp_free_ocb (struct sbp_dev *, struct sbp_ocb *);
264 static void sbp_abort_ocb (struct sbp_ocb *, int);
265 static void sbp_abort_all_ocbs (struct sbp_dev *, int);
266 static struct fw_xfer * sbp_write_cmd_locked (struct sbp_dev *, int, int);
267 static struct fw_xfer * sbp_write_cmd (struct sbp_dev *, int, int);
268 static struct sbp_ocb * sbp_get_ocb (struct sbp_dev *);
269 static struct sbp_ocb * sbp_enqueue_ocb (struct sbp_dev *, struct sbp_ocb *);
270 static struct sbp_ocb * sbp_dequeue_ocb (struct sbp_dev *, struct sbp_status *);
271 static void sbp_cam_detach_sdev(struct sbp_dev *);
272 static void sbp_free_sdev(struct sbp_dev *);
273 static void sbp_cam_detach_target (struct sbp_target *);
274 static void sbp_free_target (struct sbp_target *);
275 static void sbp_mgm_timeout (void *arg);
276 static void sbp_timeout (void *arg);
277 static void sbp_mgm_orb (struct sbp_dev *, int, struct sbp_ocb *);
279 MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire");
281 /* cam related functions */
282 static void sbp_action(struct cam_sim *sim, union ccb *ccb);
283 static void sbp_poll(struct cam_sim *sim);
284 static void sbp_cam_scan_lun(struct cam_periph *, union ccb *);
285 static void sbp_cam_scan_target(void *arg);
287 static char *orb_status0[] = {
288 /* 0 */ "No additional information to report",
289 /* 1 */ "Request type not supported",
290 /* 2 */ "Speed not supported",
291 /* 3 */ "Page size not supported",
292 /* 4 */ "Access denied",
293 /* 5 */ "Logical unit not supported",
294 /* 6 */ "Maximum payload too small",
295 /* 7 */ "Reserved for future standardization",
296 /* 8 */ "Resources unavailable",
297 /* 9 */ "Function rejected",
298 /* A */ "Login ID not recognized",
299 /* B */ "Dummy ORB completed",
300 /* C */ "Request aborted",
301 /* FF */ "Unspecified error"
302 #define MAX_ORB_STATUS0 0xd
305 static char *orb_status1_object[] = {
306 /* 0 */ "Operation request block (ORB)",
307 /* 1 */ "Data buffer",
308 /* 2 */ "Page table",
309 /* 3 */ "Unable to specify"
312 static char *orb_status1_serial_bus_error[] = {
313 /* 0 */ "Missing acknowledge",
314 /* 1 */ "Reserved; not to be used",
315 /* 2 */ "Time-out error",
316 /* 3 */ "Reserved; not to be used",
317 /* 4 */ "Busy retry limit exceeded(X)",
318 /* 5 */ "Busy retry limit exceeded(A)",
319 /* 6 */ "Busy retry limit exceeded(B)",
320 /* 7 */ "Reserved for future standardization",
321 /* 8 */ "Reserved for future standardization",
322 /* 9 */ "Reserved for future standardization",
323 /* A */ "Reserved for future standardization",
324 /* B */ "Tardy retry limit exceeded",
325 /* C */ "Conflict error",
326 /* D */ "Data error",
327 /* E */ "Type error",
328 /* F */ "Address error"
332 sbp_identify(driver_t *driver, device_t parent)
335 printf("sbp_identify\n");
338 BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent));
345 sbp_probe(device_t dev)
350 printf("sbp_probe\n");
353 pa = device_get_parent(dev);
354 if(device_get_unit(dev) != device_get_unit(pa)){
358 device_set_desc(dev, "SBP-2/SCSI over FireWire");
369 * Display device characteristics on the console
372 sbp_show_sdev_info(struct sbp_dev *sdev)
374 struct fw_device *fwdev;
376 fwdev = sdev->target->fwdev;
377 device_printf(sdev->target->sbp->fd.dev,
378 "%s: %s: ordered:%d type:%d EUI:%08x%08x node:%d "
379 "speed:%d maxrec:%d\n",
382 (sdev->type & 0x40) >> 6,
390 device_printf(sdev->target->sbp->fd.dev,
391 "%s: %s '%s' '%s' '%s'\n",
404 /* Bus Target EUI64 */
406 {0, 2, {0x00018ea0, 0x01fd0154}}, /* Logitec HDD */
407 {0, 0, {0x00018ea6, 0x00100682}}, /* Logitec DVD */
408 {0, 1, {0x00d03200, 0xa412006a}}, /* Yano HDD */
414 sbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev)
416 int bus, i, target=-1;
417 char w[SBP_NUM_TARGETS];
420 bus = device_get_unit(sbp->fd.dev);
422 /* XXX wired-down configuration should be gotten from
423 tunable or device hint */
424 for (i = 0; wired[i].bus >= 0; i ++) {
425 if (wired[i].bus == bus) {
426 w[wired[i].target] = 1;
427 if (wired[i].eui.hi == fwdev->eui.hi &&
428 wired[i].eui.lo == fwdev->eui.lo)
429 target = wired[i].target;
433 if(target < SBP_NUM_TARGETS &&
434 sbp->targets[target].fwdev == NULL)
436 device_printf(sbp->fd.dev,
437 "target %d is not free for %08x:%08x\n",
438 target, fwdev->eui.hi, fwdev->eui.lo);
441 /* non-wired target */
442 for (i = 0; i < SBP_NUM_TARGETS; i ++)
443 if (sbp->targets[i].fwdev == NULL && w[i] == 0) {
452 sbp_alloc_lun(struct sbp_target *target)
454 struct crom_context cc;
456 struct sbp_dev *sdev, **newluns;
457 struct sbp_softc *sbp;
461 crom_init_context(&cc, target->fwdev->csrrom);
462 /* XXX shoud parse appropriate unit directories only */
464 while (cc.depth >= 0) {
465 reg = crom_search_key(&cc, CROM_LUN);
468 lun = reg->val & 0xffff;
470 printf("target %d lun %d found\n", target->target_id, lun);
477 printf("%s:%d no LUN found\n",
478 device_get_nameunit(target->sbp->fd.dev),
482 if (maxlun >= SBP_NUM_LUNS)
483 maxlun = SBP_NUM_LUNS;
485 /* Invalidiate stale devices */
486 for (lun = 0; lun < target->num_lun; lun ++) {
487 sdev = target->luns[lun];
490 sdev->flags &= ~VALID_LUN;
493 sbp_cam_detach_sdev(sdev);
495 target->luns[lun] = NULL;
500 if (maxlun != target->num_lun) {
501 newluns = (struct sbp_dev **) realloc(target->luns,
502 sizeof(struct sbp_dev *) * maxlun,
503 M_SBP, M_NOWAIT | M_ZERO);
505 if (newluns == NULL) {
506 printf("%s: realloc failed\n", __func__);
507 newluns = target->luns;
508 maxlun = target->num_lun;
512 * We must zero the extended region for the case
513 * realloc() doesn't allocate new buffer.
515 if (maxlun > target->num_lun)
516 bzero(&newluns[target->num_lun],
517 sizeof(struct sbp_dev *) *
518 (maxlun - target->num_lun));
520 target->luns = newluns;
521 target->num_lun = maxlun;
524 crom_init_context(&cc, target->fwdev->csrrom);
525 while (cc.depth >= 0) {
528 reg = crom_search_key(&cc, CROM_LUN);
531 lun = reg->val & 0xffff;
532 if (lun >= SBP_NUM_LUNS) {
533 printf("too large lun %d\n", lun);
537 sdev = target->luns[lun];
539 sdev = malloc(sizeof(struct sbp_dev),
540 M_SBP, M_NOWAIT | M_ZERO);
542 printf("%s: malloc failed\n", __func__);
545 target->luns[lun] = sdev;
547 sdev->target = target;
548 STAILQ_INIT(&sdev->ocbs);
549 CALLOUT_INIT(&sdev->login_callout);
550 sdev->status = SBP_DEV_RESET;
552 snprintf(sdev->bustgtlun, 32, "%s:%d:%d",
553 device_get_nameunit(sdev->target->sbp->fd.dev),
554 sdev->target->target_id,
557 sdev->flags |= VALID_LUN;
558 sdev->type = (reg->val & 0xff0000) >> 16;
563 fwdma_malloc(sbp->fd.fc,
564 /* alignment */ sizeof(uint32_t),
565 SBP_DMA_SIZE, &sdev->dma, BUS_DMA_NOWAIT);
566 if (sdev->dma.v_addr == NULL) {
567 printf("%s: dma space allocation failed\n",
570 target->luns[lun] = NULL;
573 sdev->login = (struct sbp_login_res *) sdev->dma.v_addr;
574 sdev->ocb = (struct sbp_ocb *)
575 ((char *)sdev->dma.v_addr + SBP_LOGIN_SIZE);
576 bzero((char *)sdev->ocb,
577 sizeof (struct sbp_ocb) * SBP_QUEUE_LEN);
579 STAILQ_INIT(&sdev->free_ocbs);
580 for (i = 0; i < SBP_QUEUE_LEN; i++) {
583 ocb->bus_addr = sdev->dma.bus_addr
585 + sizeof(struct sbp_ocb) * i
586 + offsetof(struct sbp_ocb, orb[0]);
587 if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
588 printf("sbp_attach: cannot create dmamap\n");
592 sbp_free_ocb(sdev, ocb);
598 for (lun = 0; lun < target->num_lun; lun ++) {
599 sdev = target->luns[lun];
600 if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) {
601 sbp_cam_detach_sdev(sdev);
603 target->luns[lun] = NULL;
608 static struct sbp_target *
609 sbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
612 struct sbp_target *target;
613 struct crom_context cc;
617 printf("sbp_alloc_target\n");
619 i = sbp_new_target(sbp, fwdev);
621 device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n");
625 target = &sbp->targets[i];
627 target->fwdev = fwdev;
628 target->target_id = i;
629 /* XXX we may want to reload mgm port after each bus reset */
630 /* XXX there might be multiple management agents */
631 crom_init_context(&cc, target->fwdev->csrrom);
632 reg = crom_search_key(&cc, CROM_MGM);
633 if (reg == NULL || reg->val == 0) {
634 printf("NULL management address\n");
635 target->fwdev = NULL;
638 target->mgm_hi = 0xffff;
639 target->mgm_lo = 0xf0000000 | (reg->val << 2);
640 target->mgm_ocb_cur = NULL;
642 printf("target:%d mgm_port: %x\n", i, target->mgm_lo);
644 STAILQ_INIT(&target->xferlist);
646 STAILQ_INIT(&target->mgm_ocb_queue);
647 CALLOUT_INIT(&target->mgm_ocb_timeout);
648 CALLOUT_INIT(&target->scan_callout);
656 sbp_probe_lun(struct sbp_dev *sdev)
658 struct fw_device *fwdev;
659 struct crom_context c, *cc = &c;
662 bzero(sdev->vendor, sizeof(sdev->vendor));
663 bzero(sdev->product, sizeof(sdev->product));
665 fwdev = sdev->target->fwdev;
666 crom_init_context(cc, fwdev->csrrom);
667 /* get vendor string */
668 crom_search_key(cc, CSRKEY_VENDOR);
670 crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor));
671 /* skip to the unit directory for SBP-2 */
672 while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) {
673 if (reg->val == CSRVAL_T10SBP2)
677 /* get firmware revision */
678 reg = crom_search_key(cc, CSRKEY_FIRM_VER);
680 snprintf(sdev->revision, sizeof(sdev->revision),
682 /* get product string */
683 crom_search_key(cc, CSRKEY_MODEL);
685 crom_parse_text(cc, sdev->product, sizeof(sdev->product));
689 sbp_login_callout(void *arg)
691 struct sbp_dev *sdev = (struct sbp_dev *)arg;
692 sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
696 sbp_login(struct sbp_dev *sdev)
698 struct timeval delta;
703 timevalsub(&delta, &sdev->target->sbp->last_busreset);
704 t.tv_sec = login_delay / 1000;
705 t.tv_usec = (login_delay % 1000) * 1000;
706 timevalsub(&t, &delta);
707 if (t.tv_sec >= 0 && t.tv_usec > 0)
708 ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000;
710 printf("%s: sec = %jd usec = %ld ticks = %d\n", __func__,
711 (intmax_t)t.tv_sec, t.tv_usec, ticks);
713 callout_reset(&sdev->login_callout, ticks,
714 sbp_login_callout, (void *)(sdev));
717 #define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
718 && crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
721 sbp_probe_target(void *arg)
723 struct sbp_target *target = (struct sbp_target *)arg;
724 struct sbp_softc *sbp = target->sbp;
725 struct sbp_dev *sdev;
728 alive = SBP_FWDEV_ALIVE(target->fwdev);
730 device_printf(sbp->fd.dev, "%s %d%salive\n",
731 __func__, target->target_id,
732 (!alive) ? " not " : "");
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) {
746 xpt_freeze_devq(sdev->path, 1);
751 sbp_show_sdev_info(sdev);
753 sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
754 switch (sdev->status) {
756 /* new or revived target */
760 case SBP_DEV_TOATTACH:
762 case SBP_DEV_ATTACHED:
765 sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
769 switch (sdev->status) {
770 case SBP_DEV_ATTACHED:
772 /* the device has gone */
773 device_printf(sbp->fd.dev, "%s: lost target\n",
778 xpt_freeze_devq(sdev->path, 1);
782 sdev->status = SBP_DEV_RETRY;
783 sbp_cam_detach_sdev(sdev);
785 target->luns[i] = NULL;
788 case SBP_DEV_TOATTACH:
789 sdev->status = SBP_DEV_RESET;
801 sbp_post_busreset(void *arg)
803 struct sbp_softc *sbp;
805 sbp = (struct sbp_softc *)arg;
807 printf("sbp_post_busreset\n");
809 if ((sbp->sim->flags & SIMQ_FREEZED) == 0) {
811 xpt_freeze_simq(sbp->sim, /*count*/1);
812 sbp->sim->flags |= SIMQ_FREEZED;
815 microtime(&sbp->last_busreset);
819 sbp_post_explore(void *arg)
821 struct sbp_softc *sbp = (struct sbp_softc *)arg;
822 struct sbp_target *target;
823 struct fw_device *fwdev;
827 printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
829 /* We need physical access */
830 if (!firewire_phydma_enable)
838 * XXX don't let CAM the bus rest.
839 * CAM tries to do something with freezed (DEV_RETRY) devices.
841 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
844 /* Garbage Collection */
845 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
846 target = &sbp->targets[i];
847 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
848 if (target->fwdev == NULL || target->fwdev == fwdev)
851 /* device has removed in lower driver */
852 sbp_cam_detach_target(target);
853 sbp_free_target(target);
856 /* traverse device list */
857 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
859 device_printf(sbp->fd.dev,"%s:: EUI:%08x%08x %s attached, state=%d\n",
860 __func__, fwdev->eui.hi, fwdev->eui.lo,
861 (fwdev->status != FWDEVATTACHED) ? "not" : "",
864 alive = SBP_FWDEV_ALIVE(fwdev);
865 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
866 target = &sbp->targets[i];
867 if(target->fwdev == fwdev ) {
872 if(i == SBP_NUM_TARGETS){
875 target = sbp_alloc_target(sbp, fwdev);
882 sbp_probe_target((void *)target);
883 if (target->num_lun == 0)
884 sbp_free_target(target);
887 xpt_release_simq(sbp->sim, /*run queue*/TRUE);
888 sbp->sim->flags &= ~SIMQ_FREEZED;
894 sbp_loginres_callback(struct fw_xfer *xfer){
896 struct sbp_dev *sdev;
897 sdev = (struct sbp_dev *)xfer->sc;
899 device_printf(sdev->target->sbp->fd.dev,"%s\n", __func__);
903 STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link);
910 sbp_xfer_free(struct fw_xfer *xfer)
912 struct sbp_dev *sdev;
915 sdev = (struct sbp_dev *)xfer->sc;
916 fw_xfer_unload(xfer);
918 SBP_LOCK(sdev->target->sbp);
919 STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
920 SBP_UNLOCK(sdev->target->sbp);
925 sbp_reset_start_callback(struct fw_xfer *xfer)
927 struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
928 struct sbp_target *target = sdev->target;
931 if (xfer->resp != 0) {
932 device_printf(sdev->target->sbp->fd.dev,
933 "%s: %s failed: resp=%d\n", __func__, sdev->bustgtlun, xfer->resp);
936 for (i = 0; i < target->num_lun; i++) {
937 tsdev = target->luns[i];
938 if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN)
944 sbp_reset_start(struct sbp_dev *sdev)
946 struct fw_xfer *xfer;
950 device_printf(sdev->target->sbp->fd.dev,
951 "%s:%s\n", __func__,sdev->bustgtlun);
954 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
955 xfer->hand = sbp_reset_start_callback;
956 fp = &xfer->send.hdr;
957 fp->mode.wreqq.dest_hi = 0xffff;
958 fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
959 fp->mode.wreqq.data = htonl(0xf);
960 fw_asyreq(xfer->fc, -1, xfer);
964 sbp_mgm_callback(struct fw_xfer *xfer)
966 struct sbp_dev *sdev;
969 sdev = (struct sbp_dev *)xfer->sc;
972 device_printf(sdev->target->sbp->fd.dev,
973 "%s:%s\n", __func__, sdev->bustgtlun);
980 static struct sbp_dev *
981 sbp_next_dev(struct sbp_target *target, int lun)
983 struct sbp_dev **sdevp;
986 for (i = lun, sdevp = &target->luns[lun]; i < target->num_lun;
988 if (*sdevp != NULL && (*sdevp)->status == SBP_DEV_PROBE)
995 sbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb)
997 struct sbp_target *target;
998 struct sbp_dev *sdev;
1000 sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
1001 target = sdev->target;
1003 device_printf(sdev->target->sbp->fd.dev,
1004 "%s:%s\n", __func__, sdev->bustgtlun);
1006 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1007 sdev->status = SBP_DEV_ATTACHED;
1009 device_printf(sdev->target->sbp->fd.dev,
1010 "%s:%s failed\n", __func__, sdev->bustgtlun);
1012 sdev = sbp_next_dev(target, sdev->lun_id + 1);
1018 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
1019 ccb->ccb_h.ccb_sdev_ptr = sdev;
1021 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1026 sbp_cam_scan_target(void *arg)
1028 struct sbp_target *target = (struct sbp_target *)arg;
1029 struct sbp_dev *sdev;
1032 sdev = sbp_next_dev(target, 0);
1034 printf("sbp_cam_scan_target: nothing to do for target%d\n",
1039 device_printf(sdev->target->sbp->fd.dev,
1040 "%s:%s\n", __func__, sdev->bustgtlun);
1042 ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO);
1044 printf("sbp_cam_scan_target: malloc failed\n");
1047 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
1048 ccb->ccb_h.func_code = XPT_SCAN_LUN;
1049 ccb->ccb_h.cbfcnp = sbp_cam_scan_lun;
1050 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1051 ccb->crcn.flags = CAM_FLAG_NONE;
1052 ccb->ccb_h.ccb_sdev_ptr = sdev;
1054 /* The scan is in progress now. */
1055 SBP_LOCK(target->sbp);
1057 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1059 SBP_UNLOCK(target->sbp);
1062 static __inline void
1063 sbp_scan_dev(struct sbp_dev *sdev)
1065 sdev->status = SBP_DEV_PROBE;
1066 callout_reset(&sdev->target->scan_callout, scan_delay * hz / 1000,
1067 sbp_cam_scan_target, (void *)sdev->target);
1071 sbp_do_attach(struct fw_xfer *xfer)
1073 struct sbp_dev *sdev;
1074 struct sbp_target *target;
1075 struct sbp_softc *sbp;
1077 sdev = (struct sbp_dev *)xfer->sc;
1078 target = sdev->target;
1081 device_printf(sdev->target->sbp->fd.dev,
1082 "%s:%s\n", __func__, sdev->bustgtlun);
1084 sbp_xfer_free(xfer);
1086 if (sdev->path == NULL)
1087 xpt_create_path(&sdev->path, xpt_periph,
1088 cam_sim_path(target->sbp->sim),
1089 target->target_id, sdev->lun_id);
1092 * Let CAM scan the bus if we are in the boot process.
1093 * XXX xpt_scan_bus cannot detect LUN larger than 0
1094 * if LUN 0 doesn't exists.
1097 sdev->status = SBP_DEV_ATTACHED;
1106 sbp_agent_reset_callback(struct fw_xfer *xfer)
1108 struct sbp_dev *sdev;
1110 sdev = (struct sbp_dev *)xfer->sc;
1112 device_printf(sdev->target->sbp->fd.dev,
1113 "%s:%s\n", __func__, sdev->bustgtlun);
1115 if (xfer->resp != 0) {
1116 device_printf(sdev->target->sbp->fd.dev,
1117 "%s:%s resp=%d\n", __func__, sdev->bustgtlun, xfer->resp);
1120 sbp_xfer_free(xfer);
1122 SBP_LOCK(sdev->target->sbp);
1123 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1125 SBP_UNLOCK(sdev->target->sbp);
1130 sbp_agent_reset(struct sbp_dev *sdev)
1132 struct fw_xfer *xfer;
1136 device_printf(sdev->target->sbp->fd.dev,
1137 "%s:%s\n", __func__, sdev->bustgtlun);
1139 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1142 if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE)
1143 xfer->hand = sbp_agent_reset_callback;
1145 xfer->hand = sbp_do_attach;
1146 fp = &xfer->send.hdr;
1147 fp->mode.wreqq.data = htonl(0xf);
1148 fw_asyreq(xfer->fc, -1, xfer);
1149 sbp_abort_all_ocbs(sdev, CAM_BDR_SENT);
1153 sbp_busy_timeout_callback(struct fw_xfer *xfer)
1155 struct sbp_dev *sdev;
1157 sdev = (struct sbp_dev *)xfer->sc;
1159 device_printf(sdev->target->sbp->fd.dev,
1160 "%s:%s\n", __func__, sdev->bustgtlun);
1162 sbp_xfer_free(xfer);
1163 sbp_agent_reset(sdev);
1167 sbp_busy_timeout(struct sbp_dev *sdev)
1170 struct fw_xfer *xfer;
1172 device_printf(sdev->target->sbp->fd.dev,
1173 "%s:%s\n", __func__, sdev->bustgtlun);
1175 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1177 xfer->hand = sbp_busy_timeout_callback;
1178 fp = &xfer->send.hdr;
1179 fp->mode.wreqq.dest_hi = 0xffff;
1180 fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1181 fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1182 fw_asyreq(xfer->fc, -1, xfer);
1186 sbp_orb_pointer_callback(struct fw_xfer *xfer)
1188 struct sbp_dev *sdev;
1189 sdev = (struct sbp_dev *)xfer->sc;
1192 device_printf(sdev->target->sbp->fd.dev,
1193 "%s:%s\n", __func__, sdev->bustgtlun);
1195 if (xfer->resp != 0) {
1197 printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1199 sbp_xfer_free(xfer);
1201 SBP_LOCK(sdev->target->sbp);
1202 sdev->flags &= ~ORB_POINTER_ACTIVE;
1204 if ((sdev->flags & ORB_POINTER_NEED) != 0) {
1205 struct sbp_ocb *ocb;
1207 sdev->flags &= ~ORB_POINTER_NEED;
1208 ocb = STAILQ_FIRST(&sdev->ocbs);
1210 sbp_orb_pointer(sdev, ocb);
1212 SBP_UNLOCK(sdev->target->sbp);
1217 sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1219 struct fw_xfer *xfer;
1222 device_printf(sdev->target->sbp->fd.dev,
1224 __func__, sdev->bustgtlun,
1225 (uint32_t)ocb->bus_addr);
1228 mtx_assert(&sdev->target->sbp->mtx, MA_OWNED);
1230 if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) {
1232 printf("%s: orb pointer active\n", __func__);
1234 sdev->flags |= ORB_POINTER_NEED;
1238 sdev->flags |= ORB_POINTER_ACTIVE;
1239 xfer = sbp_write_cmd_locked(sdev, FWTCODE_WREQB, 0x08);
1242 xfer->hand = sbp_orb_pointer_callback;
1244 fp = &xfer->send.hdr;
1245 fp->mode.wreqb.len = 8;
1246 fp->mode.wreqb.extcode = 0;
1247 xfer->send.payload[0] =
1248 htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1249 xfer->send.payload[1] = htonl((uint32_t)ocb->bus_addr);
1252 * sbp_xfer_free() will attempt to acquire
1253 * the SBP lock on entrance. Also, this removes
1254 * a LOR between the firewire layer and sbp
1256 SBP_UNLOCK(sdev->target->sbp);
1257 if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1258 sbp_xfer_free(xfer);
1259 ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1262 SBP_LOCK(sdev->target->sbp);
1266 sbp_doorbell_callback(struct fw_xfer *xfer)
1268 struct sbp_dev *sdev;
1269 sdev = (struct sbp_dev *)xfer->sc;
1272 device_printf(sdev->target->sbp->fd.dev,
1273 "%s:%s\n", __func__, sdev->bustgtlun);
1275 if (xfer->resp != 0) {
1277 device_printf(sdev->target->sbp->fd.dev,
1278 "%s: xfer->resp = %d\n", __func__, xfer->resp);
1280 sbp_xfer_free(xfer);
1281 sdev->flags &= ~ORB_DOORBELL_ACTIVE;
1282 if ((sdev->flags & ORB_DOORBELL_NEED) != 0) {
1283 sdev->flags &= ~ORB_DOORBELL_NEED;
1284 SBP_LOCK(sdev->target->sbp);
1286 SBP_UNLOCK(sdev->target->sbp);
1292 sbp_doorbell(struct sbp_dev *sdev)
1294 struct fw_xfer *xfer;
1297 device_printf(sdev->target->sbp->fd.dev,
1298 "%s:%s\n", __func__, sdev->bustgtlun);
1301 if ((sdev->flags & ORB_DOORBELL_ACTIVE) != 0) {
1302 sdev->flags |= ORB_DOORBELL_NEED;
1305 sdev->flags |= ORB_DOORBELL_ACTIVE;
1306 xfer = sbp_write_cmd_locked(sdev, FWTCODE_WREQQ, 0x10);
1309 xfer->hand = sbp_doorbell_callback;
1310 fp = &xfer->send.hdr;
1311 fp->mode.wreqq.data = htonl(0xf);
1312 fw_asyreq(xfer->fc, -1, xfer);
1315 static struct fw_xfer *
1316 sbp_write_cmd_locked(struct sbp_dev *sdev, int tcode, int offset)
1318 struct fw_xfer *xfer;
1320 struct sbp_target *target;
1323 mtx_assert(&sdev->target->sbp->mtx, MA_OWNED);
1325 target = sdev->target;
1327 xfer = STAILQ_FIRST(&target->xferlist);
1329 if (target->n_xfer > 5 /* XXX */) {
1330 printf("sbp: no more xfer for this target\n");
1334 xfer = fw_xfer_alloc_buf(M_SBP, 8, 0);
1336 printf("sbp: fw_xfer_alloc_buf failed\n");
1342 printf("sbp: alloc %d xfer\n", target->n_xfer);
1345 STAILQ_REMOVE_HEAD(&target->xferlist, link);
1350 xfer->recv.pay_len = 0;
1351 xfer->send.spd = min(sdev->target->fwdev->speed, max_speed);
1352 xfer->fc = sdev->target->sbp->fd.fc;
1355 if (tcode == FWTCODE_WREQB)
1356 xfer->send.pay_len = 8;
1358 xfer->send.pay_len = 0;
1360 xfer->sc = (caddr_t)sdev;
1361 fp = &xfer->send.hdr;
1362 fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
1363 fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
1364 fp->mode.wreqq.tlrt = 0;
1365 fp->mode.wreqq.tcode = tcode;
1366 fp->mode.wreqq.pri = 0;
1367 fp->mode.wreqq.dst = FWLOCALBUS | sdev->target->fwdev->dst;
1373 static struct fw_xfer *
1374 sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1376 struct sbp_softc *sbp = sdev->target->sbp;
1377 struct fw_xfer *xfer;
1380 xfer = sbp_write_cmd_locked(sdev, tcode, offset);
1387 sbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1389 struct fw_xfer *xfer;
1391 struct sbp_ocb *ocb;
1392 struct sbp_target *target;
1395 target = sdev->target;
1396 nid = target->sbp->fd.fc->nodeid | FWLOCALBUS;
1399 SBP_LOCK(target->sbp);
1400 if (func == ORB_FUN_RUNQUEUE) {
1401 ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1402 if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1403 SBP_UNLOCK(target->sbp);
1407 STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1408 SBP_UNLOCK(target->sbp);
1411 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
1412 SBP_UNLOCK(target->sbp);
1417 SBP_UNLOCK(target->sbp);
1418 ocb->flags = OCB_ACT_MGM;
1421 bzero((void *)ocb->orb, sizeof(ocb->orb));
1422 ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1423 ocb->orb[7] = htonl(SBP_DEV2ADDR(target->target_id, sdev->lun_id));
1426 device_printf(sdev->target->sbp->fd.dev,
1428 __func__,sdev->bustgtlun,
1429 orb_fun_name[(func>>16)&0xf]);
1433 ocb->orb[0] = ocb->orb[1] = 0; /* password */
1434 ocb->orb[2] = htonl(nid << 16);
1435 ocb->orb[3] = htonl(sdev->dma.bus_addr);
1436 ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id);
1438 ocb->orb[4] |= htonl(ORB_EXV);
1439 ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
1440 fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD);
1443 ocb->orb[0] = htonl((0 << 16) | 0);
1444 ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
1451 ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
1455 if (target->mgm_ocb_cur != NULL) {
1456 /* there is a standing ORB */
1457 SBP_LOCK(target->sbp);
1458 STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1459 SBP_UNLOCK(target->sbp);
1464 target->mgm_ocb_cur = ocb;
1467 callout_reset(&target->mgm_ocb_timeout, 5*hz,
1468 sbp_mgm_timeout, (caddr_t)ocb);
1469 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1473 xfer->hand = sbp_mgm_callback;
1475 fp = &xfer->send.hdr;
1476 fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
1477 fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
1478 fp->mode.wreqb.len = 8;
1479 fp->mode.wreqb.extcode = 0;
1480 xfer->send.payload[0] = htonl(nid << 16);
1481 xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff);
1483 fw_asyreq(xfer->fc, -1, xfer);
1487 sbp_print_scsi_cmd(struct sbp_ocb *ocb)
1489 struct ccb_scsiio *csio;
1491 csio = &ocb->ccb->csio;
1492 printf("%s:%d:%d XPT_SCSI_IO: "
1493 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1495 "%db cmd/%db data/%db sense\n",
1496 device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1497 ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun,
1498 csio->cdb_io.cdb_bytes[0],
1499 csio->cdb_io.cdb_bytes[1],
1500 csio->cdb_io.cdb_bytes[2],
1501 csio->cdb_io.cdb_bytes[3],
1502 csio->cdb_io.cdb_bytes[4],
1503 csio->cdb_io.cdb_bytes[5],
1504 csio->cdb_io.cdb_bytes[6],
1505 csio->cdb_io.cdb_bytes[7],
1506 csio->cdb_io.cdb_bytes[8],
1507 csio->cdb_io.cdb_bytes[9],
1508 ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1509 csio->cdb_len, csio->dxfer_len,
1514 sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1516 struct sbp_cmd_status *sbp_cmd_status;
1517 struct scsi_sense_data *sense;
1519 sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1520 sense = &ocb->ccb->csio.sense_data;
1523 sbp_print_scsi_cmd(ocb);
1524 /* XXX need decode status */
1525 printf("%s: SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1526 ocb->sdev->bustgtlun,
1527 sbp_cmd_status->status,
1528 sbp_cmd_status->sfmt,
1529 sbp_cmd_status->valid,
1530 sbp_cmd_status->s_key,
1531 sbp_cmd_status->s_code,
1532 sbp_cmd_status->s_qlfr,
1536 switch (sbp_cmd_status->status) {
1537 case SCSI_STATUS_CHECK_COND:
1538 case SCSI_STATUS_BUSY:
1539 case SCSI_STATUS_CMD_TERMINATED:
1540 if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1541 sense->error_code = SSD_CURRENT_ERROR;
1543 sense->error_code = SSD_DEFERRED_ERROR;
1545 if(sbp_cmd_status->valid)
1546 sense->error_code |= SSD_ERRCODE_VALID;
1547 sense->flags = sbp_cmd_status->s_key;
1548 if(sbp_cmd_status->mark)
1549 sense->flags |= SSD_FILEMARK;
1550 if(sbp_cmd_status->eom)
1551 sense->flags |= SSD_EOM;
1552 if(sbp_cmd_status->ill_len)
1553 sense->flags |= SSD_ILI;
1555 bcopy(&sbp_cmd_status->info, &sense->info[0], 4);
1557 if (sbp_status->len <= 1)
1558 /* XXX not scsi status. shouldn't be happened */
1559 sense->extra_len = 0;
1560 else if (sbp_status->len <= 4)
1561 /* add_sense_code(_qual), info, cmd_spec_info */
1562 sense->extra_len = 6;
1564 /* fru, sense_key_spec */
1565 sense->extra_len = 10;
1567 bcopy(&sbp_cmd_status->cdb, &sense->cmd_spec_info[0], 4);
1569 sense->add_sense_code = sbp_cmd_status->s_code;
1570 sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1571 sense->fru = sbp_cmd_status->fru;
1573 bcopy(&sbp_cmd_status->s_keydep[0],
1574 &sense->sense_key_spec[0], 3);
1576 ocb->ccb->csio.scsi_status = sbp_cmd_status->status;
1577 ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1578 | CAM_AUTOSNS_VALID;
1583 for( j = 0 ; j < 32 ; j+=8){
1584 printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1585 tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1586 tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1593 device_printf(ocb->sdev->target->sbp->fd.dev,
1594 "%s:%s unknown scsi status 0x%x\n",
1595 __func__, ocb->sdev->bustgtlun,
1596 sbp_cmd_status->status);
1601 sbp_fix_inq_data(struct sbp_ocb *ocb)
1604 struct sbp_dev *sdev;
1605 struct scsi_inquiry_data *inq;
1610 if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1613 device_printf(sdev->target->sbp->fd.dev,
1614 "%s:%s\n", __func__, sdev->bustgtlun);
1616 inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1617 switch (SID_TYPE(inq)) {
1621 * XXX Convert Direct Access device to RBC.
1622 * I've never seen FireWire DA devices which support READ_6.
1624 if (SID_TYPE(inq) == T_DIRECT)
1625 inq->device |= T_RBC; /* T_DIRECT == 0 */
1630 * Override vendor/product/revision information.
1631 * Some devices sometimes return strange strings.
1634 bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1635 bcopy(sdev->product, inq->product, sizeof(inq->product));
1636 bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1641 * Force to enable/disable tagged queuing.
1642 * XXX CAM also checks SCP_QUEUE_DQUE flag in the control mode page.
1645 inq->flags |= SID_CmdQue;
1646 else if (sbp_tags < 0)
1647 inq->flags &= ~SID_CmdQue;
1652 sbp_recv1(struct fw_xfer *xfer)
1658 struct sbp_softc *sbp;
1659 struct sbp_dev *sdev;
1660 struct sbp_ocb *ocb;
1661 struct sbp_login_res *login_res = NULL;
1662 struct sbp_status *sbp_status;
1663 struct sbp_target *target;
1664 int orb_fun, status_valid0, status_valid, t, l, reset_agent = 0;
1668 ld = xfer->recv.buf;
1669 printf("sbp %x %d %d %08x %08x %08x %08x\n",
1670 xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1671 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1672 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1674 sbp = (struct sbp_softc *)xfer->sc;
1675 if (xfer->resp != 0){
1676 printf("sbp_recv: xfer->resp = %d\n", xfer->resp);
1679 if (xfer->recv.payload == NULL){
1680 printf("sbp_recv: xfer->recv.payload == NULL\n");
1683 rfp = &xfer->recv.hdr;
1684 if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1685 printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1688 sbp_status = (struct sbp_status *)xfer->recv.payload;
1689 addr = rfp->mode.wreqb.dest_lo;
1691 printf("received address 0x%x\n", addr);
1693 t = SBP_ADDR2TRG(addr);
1694 if (t >= SBP_NUM_TARGETS) {
1695 device_printf(sbp->fd.dev,
1696 "sbp_recv1: invalid target %d\n", t);
1699 target = &sbp->targets[t];
1700 l = SBP_ADDR2LUN(addr);
1701 if (l >= target->num_lun || target->luns[l] == NULL) {
1702 device_printf(sbp->fd.dev,
1703 "sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1706 sdev = target->luns[l];
1709 switch (sbp_status->src) {
1712 /* check mgm_ocb_cur first */
1713 ocb = target->mgm_ocb_cur;
1715 if (OCB_MATCH(ocb, sbp_status)) {
1716 callout_stop(&target->mgm_ocb_timeout);
1717 target->mgm_ocb_cur = NULL;
1721 ocb = sbp_dequeue_ocb(sdev, sbp_status);
1723 device_printf(sdev->target->sbp->fd.dev,
1724 #if defined(__DragonFly__) || __FreeBSD_version < 500000
1725 "%s:%s No ocb(%lx) on the queue\n",
1727 "%s:%s No ocb(%x) on the queue\n",
1729 __func__,sdev->bustgtlun,
1730 ntohl(sbp_status->orb_lo));
1735 device_printf(sdev->target->sbp->fd.dev,
1736 "%s:%s unsolicit status received\n",
1737 __func__, sdev->bustgtlun);
1740 device_printf(sdev->target->sbp->fd.dev,
1741 "%s:%s unknown sbp_status->src\n",
1742 __func__, sdev->bustgtlun);
1745 status_valid0 = (sbp_status->src < 2
1746 && sbp_status->resp == ORB_RES_CMPL
1747 && sbp_status->dead == 0);
1748 status_valid = (status_valid0 && sbp_status->status == 0);
1750 if (!status_valid0 || debug > 2){
1753 device_printf(sdev->target->sbp->fd.dev,
1754 "%s:%s ORB status src:%x resp:%x dead:%x"
1755 #if defined(__DragonFly__) || __FreeBSD_version < 500000
1756 " len:%x stat:%x orb:%x%08lx\n",
1758 " len:%x stat:%x orb:%x%08x\n",
1760 __func__, sdev->bustgtlun,
1761 sbp_status->src, sbp_status->resp, sbp_status->dead,
1762 sbp_status->len, sbp_status->status,
1763 ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1765 device_printf(sdev->target->sbp->fd.dev,
1766 "%s\n", sdev->bustgtlun);
1767 status = sbp_status->status;
1768 switch(sbp_status->resp) {
1770 if (status > MAX_ORB_STATUS0)
1771 printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1773 printf("%s\n", orb_status0[status]);
1776 printf("Obj: %s, Error: %s\n",
1777 orb_status1_object[(status>>6) & 3],
1778 orb_status1_serial_bus_error[status & 0xf]);
1781 printf("Illegal request\n");
1784 printf("Vendor dependent\n");
1787 printf("unknown respose code %d\n", sbp_status->resp);
1791 /* we have to reset the fetch agent if it's dead */
1792 if (sbp_status->dead) {
1795 xpt_freeze_devq(sdev->path, 1);
1805 switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1811 switch(ocb->flags) {
1813 orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1817 fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD);
1818 login_res = sdev->login;
1819 login_res->len = ntohs(login_res->len);
1820 login_res->id = ntohs(login_res->id);
1821 login_res->cmd_hi = ntohs(login_res->cmd_hi);
1822 login_res->cmd_lo = ntohl(login_res->cmd_lo);
1825 device_printf(sdev->target->sbp->fd.dev,
1826 "%s:%s login: len %d, ID %d, cmd %08x%08x, recon_hold %d\n",
1827 __func__, sdev->bustgtlun,
1828 login_res->len, login_res->id,
1829 login_res->cmd_hi, login_res->cmd_lo,
1830 ntohs(login_res->recon_hold));
1832 sbp_busy_timeout(sdev);
1834 /* forgot logout? */
1835 device_printf(sdev->target->sbp->fd.dev,
1836 "%s:%s login failed\n",
1837 __func__, sdev->bustgtlun);
1838 sdev->status = SBP_DEV_RESET;
1842 login_res = sdev->login;
1845 device_printf(sdev->target->sbp->fd.dev,
1846 "%s:%s reconnect: len %d, ID %d, cmd %08x%08x\n",
1847 __func__, sdev->bustgtlun,
1848 login_res->len, login_res->id,
1849 login_res->cmd_hi, login_res->cmd_lo);
1851 if (sdev->status == SBP_DEV_ATTACHED)
1854 sbp_agent_reset(sdev);
1856 /* reconnection hold time exceed? */
1858 device_printf(sdev->target->sbp->fd.dev,
1859 "%s:%s reconnect failed\n",
1860 __func__, sdev->bustgtlun);
1866 sdev->status = SBP_DEV_RESET;
1869 sbp_busy_timeout(sdev);
1874 sbp_agent_reset(sdev);
1877 device_printf(sdev->target->sbp->fd.dev,
1878 "%s:%s unknown function %d\n",
1879 __func__, sdev->bustgtlun, orb_fun);
1882 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
1886 if(ocb->ccb != NULL){
1890 if(sbp_status->len > 1){
1891 sbp_scsi_status(sbp_status, ocb);
1893 if(sbp_status->resp != ORB_RES_CMPL){
1894 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1896 ccb->ccb_h.status = CAM_REQ_CMP;
1899 /* fix up inq data */
1900 if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1901 sbp_fix_inq_data(ocb);
1913 sbp_free_ocb(sdev, ocb);
1916 sbp_agent_reset(sdev);
1919 xfer->recv.pay_len = SBP_RECV_LEN;
1920 /* The received packet is usually small enough to be stored within
1921 * the buffer. In that case, the controller return ack_complete and
1922 * no respose is necessary.
1924 * XXX fwohci.c and firewire.c should inform event_code such as
1925 * ack_complete or ack_pending to upper driver.
1929 sfp = (struct fw_pkt *)xfer->send.buf;
1930 sfp->mode.wres.dst = rfp->mode.wreqb.src;
1931 xfer->dst = sfp->mode.wres.dst;
1932 xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1933 xfer->hand = sbp_loginres_callback;
1935 sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1936 sfp->mode.wres.tcode = FWTCODE_WRES;
1937 sfp->mode.wres.rtcode = 0;
1938 sfp->mode.wres.pri = 0;
1940 fw_asyreq(xfer->fc, -1, xfer);
1943 /* we don't need a lock here because bottom half is serialized */
1944 STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1952 sbp_recv(struct fw_xfer *xfer)
1964 sbp_attach(device_t dev)
1966 struct sbp_softc *sbp;
1967 struct cam_devq *devq;
1968 struct firewire_comm *fc;
1971 if (DFLTPHYS > SBP_MAXPHYS)
1972 device_printf(dev, "Warning, DFLTPHYS(%dKB) is larger than "
1973 "SBP_MAXPHYS(%dKB).\n", DFLTPHYS / 1024,
1974 SBP_MAXPHYS / 1024);
1976 if (!firewire_phydma_enable)
1977 device_printf(dev, "Warning, hw.firewire.phydma_enable must be 1 "
1978 "for SBP over FireWire.\n");
1980 printf("sbp_attach (cold=%d)\n", cold);
1985 sbp = ((struct sbp_softc *)device_get_softc(dev));
1986 bzero(sbp, sizeof(struct sbp_softc));
1988 sbp->fd.fc = fc = device_get_ivars(dev);
1989 mtx_init(&sbp->mtx, "sbp", NULL, MTX_DEF);
1992 max_speed = fc->speed;
1994 error = bus_dma_tag_create(/*parent*/fc->dmat,
1995 /* XXX shoud be 4 for sane backend? */
1998 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1999 /*highaddr*/BUS_SPACE_MAXADDR,
2000 /*filter*/NULL, /*filterarg*/NULL,
2001 /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
2002 /*maxsegsz*/SBP_SEG_MAX,
2003 /*flags*/BUS_DMA_ALLOCNOW,
2004 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
2005 /*lockfunc*/busdma_lock_mutex,
2006 /*lockarg*/&sbp->mtx,
2010 printf("sbp_attach: Could not allocate DMA tag "
2011 "- error %d\n", error);
2015 devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
2019 for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
2020 sbp->targets[i].fwdev = NULL;
2021 sbp->targets[i].luns = NULL;
2024 sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
2025 device_get_unit(dev),
2028 /*tagged*/ SBP_QUEUE_LEN - 1,
2031 if (sbp->sim == NULL) {
2032 cam_simq_free(devq);
2037 if (xpt_bus_register(sbp->sim, dev, /*bus*/0) != CAM_SUCCESS)
2040 if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim),
2041 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2042 xpt_bus_deregister(cam_sim_path(sbp->sim));
2047 /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
2048 sbp->fwb.start = ((u_int64_t)SBP_BIND_HI << 32) | SBP_DEV2ADDR(0, 0);
2049 sbp->fwb.end = sbp->fwb.start + 0xffff;
2050 /* pre-allocate xfer */
2051 STAILQ_INIT(&sbp->fwb.xferlist);
2052 fw_xferlist_add(&sbp->fwb.xferlist, M_SBP,
2053 /*send*/ 0, /*recv*/ SBP_RECV_LEN, SBP_NUM_OCB/2,
2054 fc, (void *)sbp, sbp_recv);
2056 fw_bindadd(fc, &sbp->fwb);
2058 sbp->fd.post_busreset = sbp_post_busreset;
2059 sbp->fd.post_explore = sbp_post_explore;
2061 if (fc->status != -1) {
2063 sbp_post_busreset((void *)sbp);
2064 sbp_post_explore((void *)sbp);
2068 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
2074 cam_sim_free(sbp->sim, /*free_devq*/TRUE);
2079 sbp_logout_all(struct sbp_softc *sbp)
2081 struct sbp_target *target;
2082 struct sbp_dev *sdev;
2086 printf("sbp_logout_all\n");
2088 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
2089 target = &sbp->targets[i];
2090 if (target->luns == NULL)
2092 for (j = 0; j < target->num_lun; j++) {
2093 sdev = target->luns[j];
2096 callout_stop(&sdev->login_callout);
2097 if (sdev->status >= SBP_DEV_TOATTACH &&
2098 sdev->status <= SBP_DEV_ATTACHED)
2099 sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
2107 sbp_shutdown(device_t dev)
2109 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2111 sbp_logout_all(sbp);
2116 sbp_free_sdev(struct sbp_dev *sdev)
2122 for (i = 0; i < SBP_QUEUE_LEN; i++)
2123 bus_dmamap_destroy(sdev->target->sbp->dmat,
2124 sdev->ocb[i].dmamap);
2125 fwdma_free(sdev->target->sbp->fd.fc, &sdev->dma);
2131 sbp_free_target(struct sbp_target *target)
2133 struct sbp_softc *sbp;
2134 struct fw_xfer *xfer, *next;
2137 if (target->luns == NULL)
2139 callout_stop(&target->mgm_ocb_timeout);
2141 for (i = 0; i < target->num_lun; i++)
2142 sbp_free_sdev(target->luns[i]);
2144 for (xfer = STAILQ_FIRST(&target->xferlist);
2145 xfer != NULL; xfer = next) {
2146 next = STAILQ_NEXT(xfer, link);
2147 fw_xfer_free_buf(xfer);
2149 STAILQ_INIT(&target->xferlist);
2150 free(target->luns, M_SBP);
2151 target->num_lun = 0;
2152 target->luns = NULL;
2153 target->fwdev = NULL;
2157 sbp_detach(device_t dev)
2159 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2160 struct firewire_comm *fc = sbp->fd.fc;
2164 printf("sbp_detach\n");
2167 for (i = 0; i < SBP_NUM_TARGETS; i ++)
2168 sbp_cam_detach_target(&sbp->targets[i]);
2171 xpt_async(AC_LOST_DEVICE, sbp->path, NULL);
2172 xpt_free_path(sbp->path);
2173 xpt_bus_deregister(cam_sim_path(sbp->sim));
2174 cam_sim_free(sbp->sim, /*free_devq*/ TRUE);
2177 sbp_logout_all(sbp);
2179 /* XXX wait for logout completion */
2180 pause("sbpdtc", hz/2);
2182 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++)
2183 sbp_free_target(&sbp->targets[i]);
2185 fw_bindremove(fc, &sbp->fwb);
2186 fw_xferlist_remove(&sbp->fwb.xferlist);
2188 bus_dma_tag_destroy(sbp->dmat);
2189 mtx_destroy(&sbp->mtx);
2195 sbp_cam_detach_sdev(struct sbp_dev *sdev)
2199 if (sdev->status == SBP_DEV_DEAD)
2201 if (sdev->status == SBP_DEV_RESET)
2203 sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
2205 SBP_LOCK(sdev->target->sbp);
2206 xpt_release_devq(sdev->path,
2207 sdev->freeze, TRUE);
2209 xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
2210 xpt_free_path(sdev->path);
2212 SBP_UNLOCK(sdev->target->sbp);
2217 sbp_cam_detach_target(struct sbp_target *target)
2221 if (target->luns != NULL) {
2223 printf("sbp_detach_target %d\n", target->target_id);
2225 callout_stop(&target->scan_callout);
2226 for (i = 0; i < target->num_lun; i++)
2227 sbp_cam_detach_sdev(target->luns[i]);
2232 sbp_target_reset(struct sbp_dev *sdev, int method)
2235 struct sbp_target *target = sdev->target;
2236 struct sbp_dev *tsdev;
2238 for (i = 0; i < target->num_lun; i++) {
2239 tsdev = target->luns[i];
2242 if (tsdev->status == SBP_DEV_DEAD)
2244 if (tsdev->status == SBP_DEV_RESET)
2246 SBP_LOCK(target->sbp);
2247 xpt_freeze_devq(tsdev->path, 1);
2249 SBP_UNLOCK(target->sbp);
2250 sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT);
2252 tsdev->status = SBP_DEV_LOGIN;
2256 printf("target reset\n");
2257 sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2260 printf("reset start\n");
2261 sbp_reset_start(sdev);
2268 sbp_mgm_timeout(void *arg)
2270 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2271 struct sbp_dev *sdev = ocb->sdev;
2272 struct sbp_target *target = sdev->target;
2274 device_printf(sdev->target->sbp->fd.dev,
2275 "%s:%s request timeout(mgm orb:0x%08x)\n",
2276 __func__, sdev->bustgtlun, (uint32_t)ocb->bus_addr);
2277 target->mgm_ocb_cur = NULL;
2278 sbp_free_ocb(sdev, ocb);
2281 printf("run next request\n");
2282 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2284 device_printf(sdev->target->sbp->fd.dev,
2285 "%s:%s reset start\n",
2286 __func__, sdev->bustgtlun);
2287 sbp_reset_start(sdev);
2291 sbp_timeout(void *arg)
2293 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2294 struct sbp_dev *sdev = ocb->sdev;
2296 device_printf(sdev->target->sbp->fd.dev,
2297 "%s:%s request timeout(cmd orb:0x%08x) ... ",
2298 __func__, sdev->bustgtlun, (uint32_t)ocb->bus_addr);
2301 switch(sdev->timeout) {
2303 printf("agent reset\n");
2304 SBP_LOCK(sdev->target->sbp);
2305 xpt_freeze_devq(sdev->path, 1);
2307 SBP_UNLOCK(sdev->target->sbp);
2308 sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2309 sbp_agent_reset(sdev);
2313 sbp_target_reset(sdev, sdev->timeout - 1);
2318 sbp_cam_detach_target(target);
2319 if (target->luns != NULL)
2320 free(target->luns, M_SBP);
2321 target->num_lun = 0;
2322 target->luns = NULL;
2323 target->fwdev = NULL;
2329 sbp_action1(struct cam_sim *sim, union ccb *ccb)
2332 struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2333 struct sbp_target *target = NULL;
2334 struct sbp_dev *sdev = NULL;
2336 /* target:lun -> sdev mapping */
2338 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2339 && ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2340 target = &sbp->targets[ccb->ccb_h.target_id];
2341 if (target->fwdev != NULL
2342 && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2343 && ccb->ccb_h.target_lun < target->num_lun) {
2344 sdev = target->luns[ccb->ccb_h.target_lun];
2345 if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED &&
2346 sdev->status != SBP_DEV_PROBE)
2353 printf("invalid target %d lun %d\n",
2354 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2357 switch (ccb->ccb_h.func_code) {
2360 case XPT_GET_TRAN_SETTINGS:
2361 case XPT_SET_TRAN_SETTINGS:
2362 case XPT_CALC_GEOMETRY:
2365 printf("%s:%d:%d:func_code 0x%04x: "
2366 "Invalid target (target needed)\n",
2367 device_get_nameunit(sbp->fd.dev),
2368 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2369 ccb->ccb_h.func_code);
2372 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2379 /* The opcodes sometimes aimed at a target (sc is valid),
2380 * sometimes aimed at the SIM (sc is invalid and target is
2381 * CAM_TARGET_WILDCARD)
2384 ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2386 printf("%s:%d:%d func_code 0x%04x: "
2387 "Invalid target (no wildcard)\n",
2388 device_get_nameunit(sbp->fd.dev),
2389 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2390 ccb->ccb_h.func_code);
2392 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2398 /* XXX Hm, we should check the input parameters */
2402 switch (ccb->ccb_h.func_code) {
2405 struct ccb_scsiio *csio;
2406 struct sbp_ocb *ocb;
2411 mtx_assert(sim->mtx, MA_OWNED);
2414 printf("%s:%d:%d XPT_SCSI_IO: "
2415 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2417 "%db cmd/%db data/%db sense\n",
2418 device_get_nameunit(sbp->fd.dev),
2419 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2420 csio->cdb_io.cdb_bytes[0],
2421 csio->cdb_io.cdb_bytes[1],
2422 csio->cdb_io.cdb_bytes[2],
2423 csio->cdb_io.cdb_bytes[3],
2424 csio->cdb_io.cdb_bytes[4],
2425 csio->cdb_io.cdb_bytes[5],
2426 csio->cdb_io.cdb_bytes[6],
2427 csio->cdb_io.cdb_bytes[7],
2428 csio->cdb_io.cdb_bytes[8],
2429 csio->cdb_io.cdb_bytes[9],
2430 ccb->ccb_h.flags & CAM_DIR_MASK,
2431 csio->cdb_len, csio->dxfer_len,
2435 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2440 /* if we are in probe stage, pass only probe commands */
2441 if (sdev->status == SBP_DEV_PROBE) {
2443 name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2444 printf("probe stage, periph name: %s\n", name);
2445 if (strcmp(name, "probe") != 0) {
2446 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2452 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
2453 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2454 if (sdev->freeze == 0) {
2455 SBP_LOCK(sdev->target->sbp);
2456 xpt_freeze_devq(sdev->path, 1);
2458 SBP_UNLOCK(sdev->target->sbp);
2464 ocb->flags = OCB_ACT_CMD;
2467 ccb->ccb_h.ccb_sdev_ptr = sdev;
2468 ocb->orb[0] = htonl(1 << 31);
2470 ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2471 ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2472 speed = min(target->fwdev->speed, max_speed);
2473 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2474 | ORB_CMD_MAXP(speed + 7));
2475 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
2476 ocb->orb[4] |= htonl(ORB_CMD_IN);
2479 if (csio->ccb_h.flags & CAM_SCATTER_VALID)
2480 printf("sbp: CAM_SCATTER_VALID\n");
2481 if (csio->ccb_h.flags & CAM_DATA_PHYS)
2482 printf("sbp: CAM_DATA_PHYS\n");
2484 if (csio->ccb_h.flags & CAM_CDB_POINTER)
2485 cdb = (void *)csio->cdb_io.cdb_ptr;
2487 cdb = (void *)&csio->cdb_io.cdb_bytes;
2488 bcopy(cdb, (void *)&ocb->orb[5], csio->cdb_len);
2490 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2491 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2493 if (ccb->csio.dxfer_len > 0) {
2497 error = bus_dmamap_load(/*dma tag*/sbp->dmat,
2498 /*dma map*/ocb->dmamap,
2500 ccb->csio.dxfer_len,
2506 printf("sbp: bus_dmamap_load error %d\n", error);
2508 sbp_execute_ocb(ocb, NULL, 0, 0);
2511 case XPT_CALC_GEOMETRY:
2513 struct ccb_calc_geometry *ccg;
2514 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2516 uint32_t secs_per_cylinder;
2521 if (ccg->block_size == 0) {
2522 printf("sbp_action1: block_size is 0.\n");
2523 ccb->ccb_h.status = CAM_REQ_INVALID;
2528 printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2529 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2530 "Volume size = %d\n",
2532 "Volume size = %jd\n",
2534 device_get_nameunit(sbp->fd.dev),
2535 cam_sim_path(sbp->sim),
2536 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2537 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2543 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2544 size_mb = ccg->volume_size
2545 / ((1024L * 1024L) / ccg->block_size);
2547 if (size_mb > 1024 && extended) {
2549 ccg->secs_per_track = 63;
2552 ccg->secs_per_track = 32;
2554 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2555 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2556 ccb->ccb_h.status = CAM_REQ_CMP;
2558 cam_calc_geometry(ccg, /*extended*/1);
2563 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
2567 printf("%s:%d:XPT_RESET_BUS: \n",
2568 device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2571 ccb->ccb_h.status = CAM_REQ_INVALID;
2575 case XPT_PATH_INQ: /* Path routing inquiry */
2577 struct ccb_pathinq *cpi = &ccb->cpi;
2580 printf("%s:%d:%d XPT_PATH_INQ:.\n",
2581 device_get_nameunit(sbp->fd.dev),
2582 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2584 cpi->version_num = 1; /* XXX??? */
2585 cpi->hba_inquiry = PI_TAG_ABLE;
2586 cpi->target_sprt = 0;
2587 cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE;
2588 cpi->hba_eng_cnt = 0;
2589 cpi->max_target = SBP_NUM_TARGETS - 1;
2590 cpi->max_lun = SBP_NUM_LUNS - 1;
2591 cpi->initiator_id = SBP_INITIATOR;
2592 cpi->bus_id = sim->bus_id;
2593 cpi->base_transfer_speed = 400 * 1000 / 8;
2594 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2595 strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2596 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2597 cpi->unit_number = sim->unit_number;
2598 cpi->transport = XPORT_SPI; /* XX should have a FireWire */
2599 cpi->transport_version = 2;
2600 cpi->protocol = PROTO_SCSI;
2601 cpi->protocol_version = SCSI_REV_2;
2603 cpi->ccb_h.status = CAM_REQ_CMP;
2607 case XPT_GET_TRAN_SETTINGS:
2609 struct ccb_trans_settings *cts = &ccb->cts;
2610 struct ccb_trans_settings_scsi *scsi =
2611 &cts->proto_specific.scsi;
2612 struct ccb_trans_settings_spi *spi =
2613 &cts->xport_specific.spi;
2615 cts->protocol = PROTO_SCSI;
2616 cts->protocol_version = SCSI_REV_2;
2617 cts->transport = XPORT_SPI; /* should have a FireWire */
2618 cts->transport_version = 2;
2619 spi->valid = CTS_SPI_VALID_DISC;
2620 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
2621 scsi->valid = CTS_SCSI_VALID_TQ;
2622 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2624 printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2625 device_get_nameunit(sbp->fd.dev),
2626 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2628 cts->ccb_h.status = CAM_REQ_CMP;
2633 ccb->ccb_h.status = CAM_UA_ABORT;
2636 case XPT_SET_TRAN_SETTINGS:
2639 ccb->ccb_h.status = CAM_REQ_INVALID;
2647 sbp_action(struct cam_sim *sim, union ccb *ccb)
2652 sbp_action1(sim, ccb);
2657 sbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error)
2660 struct sbp_ocb *ocb;
2661 struct sbp_ocb *prev;
2662 bus_dma_segment_t *s;
2665 printf("sbp_execute_ocb: error=%d\n", error);
2667 ocb = (struct sbp_ocb *)arg;
2670 printf("sbp_execute_ocb: seg %d", seg);
2671 for (i = 0; i < seg; i++)
2672 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2673 printf(", %x:%d", segments[i].ds_addr, segments[i].ds_len);
2675 printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2676 (uintmax_t)segments[i].ds_len);
2682 /* direct pointer */
2684 if (s->ds_len > SBP_SEG_MAX)
2685 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2686 ocb->orb[3] = htonl(s->ds_addr);
2687 ocb->orb[4] |= htonl(s->ds_len);
2688 } else if(seg > 1) {
2690 for (i = 0; i < seg; i++) {
2693 /* XXX LSI Logic "< 16 byte" bug might be hit */
2695 printf("sbp_execute_ocb: warning, "
2696 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2697 "segment length(%d) is less than 16."
2699 "segment length(%zd) is less than 16."
2701 "(seg=%d/%d)\n", s->ds_len, i+1, seg);
2703 if (s->ds_len > SBP_SEG_MAX)
2704 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2705 ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2706 ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2708 ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2712 bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2713 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2714 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2715 prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2716 fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2719 if (ocb->sdev->last_ocb != NULL)
2720 sbp_doorbell(ocb->sdev);
2722 sbp_orb_pointer(ocb->sdev, ocb);
2725 if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) {
2726 ocb->sdev->flags &= ~ORB_LINK_DEAD;
2727 sbp_orb_pointer(ocb->sdev, ocb);
2733 sbp_poll(struct cam_sim *sim)
2735 struct sbp_softc *sbp;
2736 struct firewire_comm *fc;
2738 sbp = (struct sbp_softc *)sim->softc;
2741 fc->poll(fc, 0, -1);
2746 static struct sbp_ocb *
2747 sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2749 struct sbp_ocb *ocb;
2750 struct sbp_ocb *next;
2751 int s = splfw(), order = 0;
2755 device_printf(sdev->target->sbp->fd.dev,
2756 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2757 "%s:%s 0x%08lx src %d\n",
2759 "%s:%s 0x%08x src %d\n",
2761 __func__, sdev->bustgtlun, ntohl(sbp_status->orb_lo), sbp_status->src);
2763 SBP_LOCK(sdev->target->sbp);
2764 for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2765 next = STAILQ_NEXT(ocb, ocb);
2767 if (OCB_MATCH(ocb, sbp_status)) {
2769 STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2770 if (ocb->ccb != NULL)
2771 untimeout(sbp_timeout, (caddr_t)ocb,
2772 ocb->ccb->ccb_h.timeout_ch);
2773 if (ntohl(ocb->orb[4]) & 0xffff) {
2774 bus_dmamap_sync(sdev->target->sbp->dmat,
2776 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2777 BUS_DMASYNC_POSTREAD :
2778 BUS_DMASYNC_POSTWRITE);
2779 bus_dmamap_unload(sdev->target->sbp->dmat,
2782 if (!use_doorbell) {
2783 if (sbp_status->src == SRC_NO_NEXT) {
2785 sbp_orb_pointer(sdev, next);
2786 else if (order > 0) {
2788 * Unordered execution
2789 * We need to send pointer for
2792 sdev->flags |= ORB_LINK_DEAD;
2797 * XXX this is not correct for unordered
2800 if (sdev->last_ocb != NULL) {
2801 SBP_UNLOCK(sdev->target->sbp);
2802 sbp_free_ocb(sdev, sdev->last_ocb);
2803 SBP_LOCK(sdev->target->sbp);
2805 sdev->last_ocb = ocb;
2807 sbp_status->src == SRC_NO_NEXT)
2814 SBP_UNLOCK(sdev->target->sbp);
2817 if (ocb && order > 0) {
2818 device_printf(sdev->target->sbp->fd.dev,
2819 "%s:%s unordered execution order:%d\n",
2820 __func__, sdev->bustgtlun, order);
2826 static struct sbp_ocb *
2827 sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2830 struct sbp_ocb *prev, *prev2;
2832 mtx_assert(&sdev->target->sbp->mtx, MA_OWNED);
2834 device_printf(sdev->target->sbp->fd.dev,
2835 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2836 "%s:%s 0x%08x\n", __func__, sdev->bustgtlun, ocb->bus_addr);
2838 "%s:%s 0x%08jx\n", __func__, sdev->bustgtlun, (uintmax_t)ocb->bus_addr);
2841 prev2 = prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2842 STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2844 if (ocb->ccb != NULL)
2845 ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb,
2846 (ocb->ccb->ccb_h.timeout * hz) / 1000);
2848 if (use_doorbell && prev == NULL)
2849 prev2 = sdev->last_ocb;
2851 if (prev2 != NULL && (ocb->sdev->flags & ORB_LINK_DEAD) == 0) {
2853 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2854 printf("linking chain 0x%x -> 0x%x\n",
2855 prev2->bus_addr, ocb->bus_addr);
2857 printf("linking chain 0x%jx -> 0x%jx\n",
2858 (uintmax_t)prev2->bus_addr, (uintmax_t)ocb->bus_addr);
2862 * Suppress compiler optimization so that orb[1] must be written first.
2863 * XXX We may need an explicit memory barrier for other architectures
2864 * other than i386/amd64.
2866 *(volatile uint32_t *)&prev2->orb[1] = htonl(ocb->bus_addr);
2867 *(volatile uint32_t *)&prev2->orb[0] = 0;
2874 static struct sbp_ocb *
2875 sbp_get_ocb(struct sbp_dev *sdev)
2877 struct sbp_ocb *ocb;
2880 mtx_assert(&sdev->target->sbp->mtx, MA_OWNED);
2881 ocb = STAILQ_FIRST(&sdev->free_ocbs);
2883 sdev->flags |= ORB_SHORTAGE;
2884 printf("ocb shortage!!!\n");
2888 STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2895 sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2900 SBP_LOCK(sdev->target->sbp);
2901 STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2902 if ((sdev->flags & ORB_SHORTAGE) != 0) {
2905 sdev->flags &= ~ORB_SHORTAGE;
2906 count = sdev->freeze;
2908 xpt_release_devq(sdev->path, count, TRUE);
2910 SBP_UNLOCK(sdev->target->sbp);
2914 sbp_abort_ocb(struct sbp_ocb *ocb, int status)
2916 struct sbp_dev *sdev;
2920 device_printf(sdev->target->sbp->fd.dev,
2921 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2922 "%s:%s 0x%x\n", __func__, sdev->bustgtlun, ocb->bus_addr);
2924 "%s:%s 0x%jx\n", __func__, sdev->bustgtlun, (uintmax_t)ocb->bus_addr);
2928 if (ocb->ccb != NULL)
2929 sbp_print_scsi_cmd(ocb);
2931 if (ntohl(ocb->orb[4]) & 0xffff) {
2932 bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
2933 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2934 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2935 bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
2937 if (ocb->ccb != NULL) {
2938 untimeout(sbp_timeout, (caddr_t)ocb,
2939 ocb->ccb->ccb_h.timeout_ch);
2940 ocb->ccb->ccb_h.status = status;
2941 SBP_LOCK(sdev->target->sbp);
2943 SBP_UNLOCK(sdev->target->sbp);
2945 sbp_free_ocb(sdev, ocb);
2949 sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2952 struct sbp_ocb *ocb, *next;
2953 STAILQ_HEAD(, sbp_ocb) temp;
2958 SBP_LOCK(sdev->target->sbp);
2959 STAILQ_CONCAT(&temp, &sdev->ocbs);
2960 STAILQ_INIT(&sdev->ocbs);
2961 SBP_UNLOCK(sdev->target->sbp);
2963 for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2964 next = STAILQ_NEXT(ocb, ocb);
2965 sbp_abort_ocb(ocb, status);
2967 if (sdev->last_ocb != NULL) {
2968 sbp_free_ocb(sdev, sdev->last_ocb);
2969 sdev->last_ocb = NULL;
2975 static devclass_t sbp_devclass;
2977 static device_method_t sbp_methods[] = {
2978 /* device interface */
2979 DEVMETHOD(device_identify, sbp_identify),
2980 DEVMETHOD(device_probe, sbp_probe),
2981 DEVMETHOD(device_attach, sbp_attach),
2982 DEVMETHOD(device_detach, sbp_detach),
2983 DEVMETHOD(device_shutdown, sbp_shutdown),
2988 static driver_t sbp_driver = {
2991 sizeof(struct sbp_softc),
2993 #ifdef __DragonFly__
2994 DECLARE_DUMMY_MODULE(sbp);
2996 DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2997 MODULE_VERSION(sbp, 1);
2998 MODULE_DEPEND(sbp, firewire, 1, 1, 1);
2999 MODULE_DEPEND(sbp, cam, 1, 1, 1);