2 * Copyright (c) 2003 Hidetoshi Shimokawa
3 * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the acknowledgement as bellow:
17 * This product includes software developed by K. Kobayashi and H. Shimokawa
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * $FreeBSD: src/sys/dev/firewire/sbp.c,v 1.74 2004/01/08 14:58:09 simokawa Exp $
35 * $DragonFly: src/sys/dev/disk/sbp/sbp.c,v 1.17 2006/09/05 00:55:38 dillon Exp $
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/module.h>
44 #include <sys/kernel.h>
45 #include <sys/sysctl.h>
46 #include <machine/bus.h>
47 #include <sys/malloc.h>
48 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
50 #include <sys/mutex.h>
53 #if defined(__DragonFly__) || __FreeBSD_version < 500106
54 #include <sys/devicestat.h> /* for struct devstat */
56 #include <sys/thread2.h>
59 #include <bus/cam/cam.h>
60 #include <bus/cam/cam_ccb.h>
61 #include <bus/cam/cam_sim.h>
62 #include <bus/cam/cam_xpt_sim.h>
63 #include <bus/cam/cam_debug.h>
64 #include <bus/cam/cam_periph.h>
65 #include <bus/cam/scsi/scsi_all.h>
67 #include <bus/firewire/firewire.h>
68 #include <bus/firewire/firewirereg.h>
69 #include <bus/firewire/fwdma.h>
70 #include <bus/firewire/iec13213.h>
74 #include <cam/cam_ccb.h>
75 #include <cam/cam_sim.h>
76 #include <cam/cam_xpt_sim.h>
77 #include <cam/cam_debug.h>
78 #include <cam/cam_periph.h>
79 #include <cam/scsi/scsi_all.h>
82 #define ccb_sdev_ptr spriv_ptr0
83 #define ccb_sbp_ptr spriv_ptr1
85 #define SBP_NUM_TARGETS 8 /* MAX 64 */
87 * Scan_bus doesn't work for more than 8 LUNs
88 * because of CAM_SCSI2_MAXLUN in cam_xpt.c
90 #define SBP_NUM_LUNS 64
91 #define SBP_DMA_SIZE PAGE_SIZE
92 #define SBP_LOGIN_SIZE sizeof(struct sbp_login_res)
93 #define SBP_QUEUE_LEN ((SBP_DMA_SIZE - SBP_LOGIN_SIZE) / sizeof(struct sbp_ocb))
94 #define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS)
97 * STATUS FIFO addressing
99 * -----------------------
100 * 0- 1( 2): 0 (alingment)
103 * 16-31( 8): reserved
104 * 32-47(16): SBP_BIND_HI
105 * 48-64(16): bus_id, node_id
107 #define SBP_BIND_HI 0x1
108 #define SBP_DEV2ADDR(t, l) \
109 (((u_int64_t)SBP_BIND_HI << 32) \
110 | (((l) & 0xff) << 8) \
111 | (((t) & 0x3f) << 2))
112 #define SBP_ADDR2TRG(a) (((a) >> 2) & 0x3f)
113 #define SBP_ADDR2LUN(a) (((a) >> 8) & 0xff)
114 #define SBP_INITIATOR 7
116 static char *orb_fun_name[] = {
120 static int debug = 0;
121 static int auto_login = 1;
122 static int max_speed = -1;
123 static int sbp_cold = 1;
124 static int ex_login = 1;
125 static int login_delay = 1000; /* msec */
126 static int scan_delay = 500; /* msec */
127 static int sbp_tags = 0;
129 SYSCTL_DECL(_hw_firewire);
130 SYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem");
131 SYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0,
133 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0,
134 "SBP perform login automatically");
135 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0,
136 "SBP transfer max speed");
137 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, exclusive_login, CTLFLAG_RW,
138 &ex_login, 0, "SBP transfer max speed");
139 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, login_delay, CTLFLAG_RW,
140 &login_delay, 0, "SBP login delay in msec");
141 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, scan_delay, CTLFLAG_RW,
142 &scan_delay, 0, "SBP scan delay in msec");
143 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, tags, CTLFLAG_RW, &sbp_tags, 0,
144 "SBP tagged queuing support");
146 TUNABLE_INT("hw.firewire.sbp.auto_login", &auto_login);
147 TUNABLE_INT("hw.firewire.sbp.max_speed", &max_speed);
148 TUNABLE_INT("hw.firewire.sbp.exclusive_login", &ex_login);
149 TUNABLE_INT("hw.firewire.sbp.login_delay", &login_delay);
150 TUNABLE_INT("hw.firewire.sbp.scan_delay", &scan_delay);
151 TUNABLE_INT("hw.firewire.sbp.tags", &sbp_tags);
153 #define NEED_RESPONSE 0
155 #define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE)
156 #ifdef __sparc64__ /* iommu */
157 #define SBP_IND_MAX howmany(MAXPHYS, SBP_SEG_MAX)
159 #define SBP_IND_MAX howmany(MAXPHYS, PAGE_SIZE)
162 STAILQ_ENTRY(sbp_ocb) ocb;
166 #define IND_PTR_OFFSET (8*sizeof(u_int32_t))
167 struct ind_ptr ind_ptr[SBP_IND_MAX];
168 struct sbp_dev *sdev;
169 int flags; /* XXX should be removed */
173 #define OCB_ACT_MGM 0
174 #define OCB_ACT_CMD 1
175 #define OCB_MATCH(o,s) ((o)->bus_addr == ntohl((s)->orb_lo))
178 #define SBP_DEV_RESET 0 /* accept login */
179 #define SBP_DEV_LOGIN 1 /* to login */
181 #define SBP_DEV_RECONN 2 /* to reconnect */
183 #define SBP_DEV_TOATTACH 3 /* to attach */
184 #define SBP_DEV_PROBE 4 /* scan lun */
185 #define SBP_DEV_ATTACHED 5 /* in operation */
186 #define SBP_DEV_DEAD 6 /* unavailable unit */
187 #define SBP_DEV_RETRY 7 /* unavailable unit */
193 #define ORB_LINK_DEAD (1 << 0)
194 #define VALID_LUN (1 << 1)
195 #define ORB_POINTER_ACTIVE (1 << 2)
196 #define ORB_POINTER_NEED (1 << 3)
198 struct cam_path *path;
199 struct sbp_target *target;
200 struct fwdma_alloc dma;
201 struct sbp_login_res *login;
202 struct callout login_callout;
204 STAILQ_HEAD(, sbp_ocb) ocbs;
205 STAILQ_HEAD(, sbp_ocb) free_ocbs;
214 struct sbp_dev **luns;
215 struct sbp_softc *sbp;
216 struct fw_device *fwdev;
217 u_int32_t mgm_hi, mgm_lo;
218 struct sbp_ocb *mgm_ocb_cur;
219 STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue;
220 struct callout mgm_ocb_timeout;
221 struct callout scan_callout;
222 STAILQ_HEAD(, fw_xfer) xferlist;
227 struct firewire_dev_comm fd;
229 struct cam_path *path;
230 struct sbp_target targets[SBP_NUM_TARGETS];
233 struct timeval last_busreset;
234 #define SIMQ_FREEZED 1
238 static void sbp_post_explore (void *);
239 static void sbp_recv (struct fw_xfer *);
240 static void sbp_mgm_callback (struct fw_xfer *);
242 static void sbp_cmd_callback (struct fw_xfer *);
244 static void sbp_orb_pointer (struct sbp_dev *, struct sbp_ocb *);
245 static void sbp_execute_ocb (void *, bus_dma_segment_t *, int, int);
246 static void sbp_free_ocb (struct sbp_dev *, struct sbp_ocb *);
247 static void sbp_abort_ocb (struct sbp_ocb *, int);
248 static void sbp_abort_all_ocbs (struct sbp_dev *, int);
249 static struct fw_xfer * sbp_write_cmd (struct sbp_dev *, int, int);
250 static struct sbp_ocb * sbp_get_ocb (struct sbp_dev *);
251 static struct sbp_ocb * sbp_enqueue_ocb (struct sbp_dev *, struct sbp_ocb *);
252 static struct sbp_ocb * sbp_dequeue_ocb (struct sbp_dev *, struct sbp_status *);
253 static void sbp_cam_detach_sdev(struct sbp_dev *);
254 static void sbp_free_sdev(struct sbp_dev *);
255 static void sbp_cam_detach_target (struct sbp_target *);
256 static void sbp_free_target (struct sbp_target *);
257 static void sbp_mgm_timeout (void *arg);
258 static void sbp_timeout (void *arg);
259 static void sbp_mgm_orb (struct sbp_dev *, int, struct sbp_ocb *);
261 MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire");
263 /* cam related functions */
264 static void sbp_action(struct cam_sim *sim, union ccb *ccb);
265 static void sbp_poll(struct cam_sim *sim);
266 static void sbp_cam_scan_lun(struct cam_periph *, union ccb *);
267 static void sbp_cam_scan_target(void *arg);
269 static char *orb_status0[] = {
270 /* 0 */ "No additional information to report",
271 /* 1 */ "Request type not supported",
272 /* 2 */ "Speed not supported",
273 /* 3 */ "Page size not supported",
274 /* 4 */ "Access denied",
275 /* 5 */ "Logical unit not supported",
276 /* 6 */ "Maximum payload too small",
277 /* 7 */ "Reserved for future standardization",
278 /* 8 */ "Resources unavailable",
279 /* 9 */ "Function rejected",
280 /* A */ "Login ID not recognized",
281 /* B */ "Dummy ORB completed",
282 /* C */ "Request aborted",
283 /* FF */ "Unspecified error"
284 #define MAX_ORB_STATUS0 0xd
287 static char *orb_status1_object[] = {
288 /* 0 */ "Operation request block (ORB)",
289 /* 1 */ "Data buffer",
290 /* 2 */ "Page table",
291 /* 3 */ "Unable to specify"
294 static char *orb_status1_serial_bus_error[] = {
295 /* 0 */ "Missing acknowledge",
296 /* 1 */ "Reserved; not to be used",
297 /* 2 */ "Time-out error",
298 /* 3 */ "Reserved; not to be used",
299 /* 4 */ "Busy retry limit exceeded(X)",
300 /* 5 */ "Busy retry limit exceeded(A)",
301 /* 6 */ "Busy retry limit exceeded(B)",
302 /* 7 */ "Reserved for future standardization",
303 /* 8 */ "Reserved for future standardization",
304 /* 9 */ "Reserved for future standardization",
305 /* A */ "Reserved for future standardization",
306 /* B */ "Tardy retry limit exceeded",
307 /* C */ "Conflict error",
308 /* D */ "Data error",
309 /* E */ "Type error",
310 /* F */ "Address error"
317 sbp_probe(device_t dev)
322 printf("sbp_probe\n");
325 pa = device_get_parent(dev);
326 if(device_get_unit(dev) != device_get_unit(pa)){
330 device_set_desc(dev, "SBP-2/SCSI over FireWire");
338 sbp_show_sdev_info(struct sbp_dev *sdev, int new)
340 struct fw_device *fwdev;
343 device_get_nameunit(sdev->target->sbp->fd.dev),
344 sdev->target->target_id,
350 fwdev = sdev->target->fwdev;
351 printf("ordered:%d type:%d EUI:%08x%08x node:%d "
352 "speed:%d maxrec:%d",
353 (sdev->type & 0x40) >> 6,
365 sbp_show_sdev_info(sdev, 2);
366 printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision);
374 /* Bus Target EUI64 */
376 {0, 2, {0x00018ea0, 0x01fd0154}}, /* Logitec HDD */
377 {0, 0, {0x00018ea6, 0x00100682}}, /* Logitec DVD */
378 {0, 1, {0x00d03200, 0xa412006a}}, /* Yano HDD */
384 sbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev)
386 int bus, i, target=-1;
387 char w[SBP_NUM_TARGETS];
390 bus = device_get_unit(sbp->fd.dev);
392 /* XXX wired-down configuration should be gotten from
393 tunable or device hint */
394 for (i = 0; wired[i].bus >= 0; i ++) {
395 if (wired[i].bus == bus) {
396 w[wired[i].target] = 1;
397 if (wired[i].eui.hi == fwdev->eui.hi &&
398 wired[i].eui.lo == fwdev->eui.lo)
399 target = wired[i].target;
403 if(target < SBP_NUM_TARGETS &&
404 sbp->targets[target].fwdev == NULL)
406 device_printf(sbp->fd.dev,
407 "target %d is not free for %08x:%08x\n",
408 target, fwdev->eui.hi, fwdev->eui.lo);
411 /* non-wired target */
412 for (i = 0; i < SBP_NUM_TARGETS; i ++)
413 if (sbp->targets[i].fwdev == NULL && w[i] == 0) {
422 sbp_alloc_lun(struct sbp_target *target)
424 struct crom_context cc;
426 struct sbp_dev *sdev, **newluns;
427 struct sbp_softc *sbp;
431 crom_init_context(&cc, target->fwdev->csrrom);
432 /* XXX shoud parse appropriate unit directories only */
434 while (cc.depth >= 0) {
435 reg = crom_search_key(&cc, CROM_LUN);
438 lun = reg->val & 0xffff;
440 printf("target %d lun %d found\n", target->target_id, lun);
447 printf("%s:%d no LUN found\n",
448 device_get_nameunit(target->sbp->fd.dev),
452 if (maxlun >= SBP_NUM_LUNS)
453 maxlun = SBP_NUM_LUNS;
455 /* Invalidiate stale devices */
456 for (lun = 0; lun < target->num_lun; lun ++) {
457 sdev = target->luns[lun];
460 sdev->flags &= ~VALID_LUN;
463 sbp_cam_detach_sdev(sdev);
469 if (maxlun != target->num_lun) {
471 * note: realloc() does not support M_ZERO. We must zero
472 * the extended region manually.
474 newluns = realloc(target->luns,
475 sizeof(struct sbp_dev *) * maxlun,
478 if (maxlun > target->num_lun) {
479 bzero(&newluns[target->num_lun],
480 sizeof(struct sbp_dev *) *
481 (maxlun - target->num_lun));
483 target->luns = newluns;
484 target->num_lun = maxlun;
487 crom_init_context(&cc, target->fwdev->csrrom);
488 while (cc.depth >= 0) {
491 reg = crom_search_key(&cc, CROM_LUN);
494 lun = reg->val & 0xffff;
495 if (lun >= SBP_NUM_LUNS) {
496 printf("too large lun %d\n", lun);
500 sdev = target->luns[lun];
502 sdev = malloc(sizeof(struct sbp_dev),
503 M_SBP, M_WAITOK | M_ZERO);
504 target->luns[lun] = sdev;
506 sdev->target = target;
507 STAILQ_INIT(&sdev->ocbs);
508 CALLOUT_INIT(&sdev->login_callout);
509 sdev->status = SBP_DEV_RESET;
512 sdev->flags |= VALID_LUN;
513 sdev->type = (reg->val & 0xff0000) >> 16;
518 fwdma_malloc(sbp->fd.fc,
519 /* alignment */ sizeof(u_int32_t),
520 SBP_DMA_SIZE, &sdev->dma, BUS_DMA_NOWAIT);
521 if (sdev->dma.v_addr == NULL) {
522 printf("%s: dma space allocation failed\n",
525 target->luns[lun] = NULL;
528 sdev->login = (struct sbp_login_res *) sdev->dma.v_addr;
529 sdev->ocb = (struct sbp_ocb *)
530 ((char *)sdev->dma.v_addr + SBP_LOGIN_SIZE);
531 bzero((char *)sdev->ocb,
532 sizeof (struct sbp_ocb) * SBP_QUEUE_LEN);
534 STAILQ_INIT(&sdev->free_ocbs);
535 for (i = 0; i < SBP_QUEUE_LEN; i++) {
538 ocb->bus_addr = sdev->dma.bus_addr
540 + sizeof(struct sbp_ocb) * i
541 + offsetof(struct sbp_ocb, orb[0]);
542 if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
543 printf("sbp_attach: cannot create dmamap\n");
547 sbp_free_ocb(sdev, ocb);
553 for (lun = 0; lun < target->num_lun; lun ++) {
554 sdev = target->luns[lun];
555 if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) {
556 sbp_cam_detach_sdev(sdev);
558 target->luns[lun] = NULL;
563 static struct sbp_target *
564 sbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
567 struct sbp_target *target;
568 struct crom_context cc;
572 printf("sbp_alloc_target\n");
574 i = sbp_new_target(sbp, fwdev);
576 device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n");
580 target = &sbp->targets[i];
582 target->fwdev = fwdev;
583 target->target_id = i;
584 /* XXX we may want to reload mgm port after each bus reset */
585 /* XXX there might be multiple management agents */
586 crom_init_context(&cc, target->fwdev->csrrom);
587 reg = crom_search_key(&cc, CROM_MGM);
588 if (reg == NULL || reg->val == 0) {
589 printf("NULL management address\n");
590 target->fwdev = NULL;
593 target->mgm_hi = 0xffff;
594 target->mgm_lo = 0xf0000000 | (reg->val << 2);
595 target->mgm_ocb_cur = NULL;
597 printf("target:%d mgm_port: %x\n", i, target->mgm_lo);
599 STAILQ_INIT(&target->xferlist);
601 STAILQ_INIT(&target->mgm_ocb_queue);
602 CALLOUT_INIT(&target->mgm_ocb_timeout);
603 CALLOUT_INIT(&target->scan_callout);
611 sbp_probe_lun(struct sbp_dev *sdev)
613 struct fw_device *fwdev;
614 struct crom_context c, *cc = &c;
617 bzero(sdev->vendor, sizeof(sdev->vendor));
618 bzero(sdev->product, sizeof(sdev->product));
620 fwdev = sdev->target->fwdev;
621 crom_init_context(cc, fwdev->csrrom);
622 /* get vendor string */
623 crom_search_key(cc, CSRKEY_VENDOR);
625 crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor));
626 /* skip to the unit directory for SBP-2 */
627 while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) {
628 if (reg->val == CSRVAL_T10SBP2)
632 /* get firmware revision */
633 reg = crom_search_key(cc, CSRKEY_FIRM_VER);
635 snprintf(sdev->revision, sizeof(sdev->revision),
637 /* get product string */
638 crom_search_key(cc, CSRKEY_MODEL);
640 crom_parse_text(cc, sdev->product, sizeof(sdev->product));
644 sbp_login_callout(void *arg)
646 struct sbp_dev *sdev = (struct sbp_dev *)arg;
647 sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
651 sbp_login(struct sbp_dev *sdev)
653 struct timeval delta;
658 timevalsub(&delta, &sdev->target->sbp->last_busreset);
659 t.tv_sec = login_delay / 1000;
660 t.tv_usec = (login_delay % 1000) * 1000;
661 timevalsub(&t, &delta);
662 if (t.tv_sec >= 0 && t.tv_usec > 0)
663 ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000;
665 printf("%s: sec = %ld usec = %ld ticks = %d\n", __func__,
666 t.tv_sec, t.tv_usec, ticks);
668 callout_reset(&sdev->login_callout, ticks,
669 sbp_login_callout, (void *)(sdev));
672 #define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
673 && crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
676 sbp_probe_target(void *arg)
678 struct sbp_target *target = (struct sbp_target *)arg;
679 struct sbp_softc *sbp;
680 struct sbp_dev *sdev;
681 struct firewire_comm *fc;
684 alive = SBP_FWDEV_ALIVE(target->fwdev);
686 printf("sbp_probe_target %d\n", target->target_id);
688 printf("not alive\n");
692 fc = target->sbp->fd.fc;
693 sbp_alloc_lun(target);
695 /* XXX untimeout mgm_ocb and dequeue */
696 for (i=0; i < target->num_lun; i++) {
697 sdev = target->luns[i];
700 if (alive && (sdev->status != SBP_DEV_DEAD)) {
701 if (sdev->path != NULL) {
702 xpt_freeze_devq(sdev->path, 1);
707 sbp_show_sdev_info(sdev,
708 (sdev->status == SBP_DEV_RESET));
711 sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
712 switch (sdev->status) {
714 /* new or revived target */
718 case SBP_DEV_TOATTACH:
720 case SBP_DEV_ATTACHED:
723 sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
727 switch (sdev->status) {
728 case SBP_DEV_ATTACHED:
730 /* the device has gone */
731 sbp_show_sdev_info(sdev, 2);
732 printf("lost target\n");
735 xpt_freeze_devq(sdev->path, 1);
738 sdev->status = SBP_DEV_RETRY;
739 sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
742 case SBP_DEV_TOATTACH:
743 sdev->status = SBP_DEV_RESET;
755 sbp_post_busreset(void *arg)
757 struct sbp_softc *sbp;
759 sbp = (struct sbp_softc *)arg;
761 printf("sbp_post_busreset\n");
763 if ((sbp->sim->flags & SIMQ_FREEZED) == 0) {
764 xpt_freeze_simq(sbp->sim, /*count*/1);
765 sbp->sim->flags |= SIMQ_FREEZED;
767 microtime(&sbp->last_busreset);
771 sbp_post_explore(void *arg)
773 struct sbp_softc *sbp = (struct sbp_softc *)arg;
774 struct sbp_target *target;
775 struct fw_device *fwdev;
779 printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
786 * XXX don't let CAM the bus rest.
787 * CAM tries to do something with freezed (DEV_RETRY) devices.
789 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
792 /* Gabage Collection */
793 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
794 target = &sbp->targets[i];
795 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
796 if (target->fwdev == NULL || target->fwdev == fwdev)
799 /* device has removed in lower driver */
800 sbp_cam_detach_target(target);
801 sbp_free_target(target);
804 /* traverse device list */
805 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
807 printf("sbp_post_explore: EUI:%08x%08x ",
808 fwdev->eui.hi, fwdev->eui.lo);
809 if (fwdev->status != FWDEVATTACHED)
810 printf("not attached, state=%d.\n", fwdev->status);
812 printf("attached\n");
814 alive = SBP_FWDEV_ALIVE(fwdev);
815 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
816 target = &sbp->targets[i];
817 if(target->fwdev == fwdev ) {
822 if(i == SBP_NUM_TARGETS){
825 target = sbp_alloc_target(sbp, fwdev);
832 sbp_probe_target((void *)target);
833 if (target->num_lun == 0)
834 sbp_free_target(target);
836 xpt_release_simq(sbp->sim, /*run queue*/TRUE);
837 sbp->sim->flags &= ~SIMQ_FREEZED;
842 sbp_loginres_callback(struct fw_xfer *xfer){
843 struct sbp_dev *sdev;
844 sdev = (struct sbp_dev *)xfer->sc;
846 sbp_show_sdev_info(sdev, 2);
847 printf("sbp_loginres_callback\n");
851 STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link);
858 sbp_xfer_free(struct fw_xfer *xfer)
860 struct sbp_dev *sdev;
862 sdev = (struct sbp_dev *)xfer->sc;
863 fw_xfer_unload(xfer);
865 STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
870 sbp_reset_start_callback(struct fw_xfer *xfer)
872 struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
873 struct sbp_target *target = sdev->target;
876 if (xfer->resp != 0) {
877 sbp_show_sdev_info(sdev, 2);
878 printf("sbp_reset_start failed: resp=%d\n", xfer->resp);
881 for (i = 0; i < target->num_lun; i++) {
882 tsdev = target->luns[i];
883 if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN)
889 sbp_reset_start(struct sbp_dev *sdev)
891 struct fw_xfer *xfer;
895 sbp_show_sdev_info(sdev, 2);
896 printf("sbp_reset_start\n");
899 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
900 xfer->act.hand = sbp_reset_start_callback;
901 fp = &xfer->send.hdr;
902 fp->mode.wreqq.dest_hi = 0xffff;
903 fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
904 fp->mode.wreqq.data = htonl(0xf);
905 fw_asyreq(xfer->fc, -1, xfer);
909 sbp_mgm_callback(struct fw_xfer *xfer)
911 struct sbp_dev *sdev;
914 sdev = (struct sbp_dev *)xfer->sc;
917 sbp_show_sdev_info(sdev, 2);
918 printf("sbp_mgm_callback\n");
924 sbp_show_sdev_info(sdev, 2);
925 printf("management ORB failed(%d) ... RESET_START\n", resp);
926 sbp_reset_start(sdev);
932 static struct sbp_dev *
933 sbp_next_dev(struct sbp_target *target, int lun)
935 struct sbp_dev **sdevp;
938 for (i = lun, sdevp = &target->luns[lun]; i < target->num_lun;
940 if (*sdevp != NULL && (*sdevp)->status == SBP_DEV_PROBE)
947 sbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb)
949 struct sbp_target *target;
950 struct sbp_dev *sdev;
952 sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
953 target = sdev->target;
955 sbp_show_sdev_info(sdev, 2);
956 printf("sbp_cam_scan_lun\n");
958 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
959 sdev->status = SBP_DEV_ATTACHED;
961 sbp_show_sdev_info(sdev, 2);
962 printf("scan failed\n");
964 sdev = sbp_next_dev(target, sdev->lun_id + 1);
970 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
971 ccb->ccb_h.ccb_sdev_ptr = sdev;
973 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
978 sbp_cam_scan_target(void *arg)
980 struct sbp_target *target = (struct sbp_target *)arg;
981 struct sbp_dev *sdev;
984 sdev = sbp_next_dev(target, 0);
986 printf("sbp_cam_scan_target: nothing to do for target%d\n",
991 sbp_show_sdev_info(sdev, 2);
992 printf("sbp_cam_scan_target\n");
994 ccb = kmalloc(sizeof(union ccb), M_SBP, M_WAITOK | M_ZERO);
995 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
996 ccb->ccb_h.func_code = XPT_SCAN_LUN;
997 ccb->ccb_h.cbfcnp = sbp_cam_scan_lun;
998 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
999 ccb->crcn.flags = CAM_FLAG_NONE;
1000 ccb->ccb_h.ccb_sdev_ptr = sdev;
1002 /* The scan is in progress now. */
1004 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1008 static __inline void
1009 sbp_scan_dev(struct sbp_dev *sdev)
1011 sdev->status = SBP_DEV_PROBE;
1012 callout_reset(&sdev->target->scan_callout, scan_delay * hz / 1000,
1013 sbp_cam_scan_target, (void *)sdev->target);
1017 sbp_do_attach(struct fw_xfer *xfer)
1019 struct sbp_dev *sdev;
1020 struct sbp_target *target;
1021 struct sbp_softc *sbp;
1023 sdev = (struct sbp_dev *)xfer->sc;
1024 target = sdev->target;
1027 sbp_show_sdev_info(sdev, 2);
1028 printf("sbp_do_attach\n");
1030 sbp_xfer_free(xfer);
1032 if (sdev->path == NULL)
1033 xpt_create_path(&sdev->path, xpt_periph,
1034 cam_sim_path(target->sbp->sim),
1035 target->target_id, sdev->lun_id);
1038 * Let CAM scan the bus if we are in the boot process.
1039 * XXX xpt_scan_bus cannot detect LUN larger than 0
1040 * if LUN 0 doesn't exists.
1043 sdev->status = SBP_DEV_ATTACHED;
1052 sbp_agent_reset_callback(struct fw_xfer *xfer)
1054 struct sbp_dev *sdev;
1056 sdev = (struct sbp_dev *)xfer->sc;
1058 sbp_show_sdev_info(sdev, 2);
1059 printf("%s\n", __func__);
1061 if (xfer->resp != 0) {
1062 sbp_show_sdev_info(sdev, 2);
1063 printf("%s: resp=%d\n", __func__, xfer->resp);
1066 sbp_xfer_free(xfer);
1068 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1074 sbp_agent_reset(struct sbp_dev *sdev)
1076 struct fw_xfer *xfer;
1080 sbp_show_sdev_info(sdev, 2);
1081 printf("sbp_agent_reset\n");
1083 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1086 if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE)
1087 xfer->act.hand = sbp_agent_reset_callback;
1089 xfer->act.hand = sbp_do_attach;
1090 fp = &xfer->send.hdr;
1091 fp->mode.wreqq.data = htonl(0xf);
1092 fw_asyreq(xfer->fc, -1, xfer);
1093 sbp_abort_all_ocbs(sdev, CAM_BDR_SENT);
1097 sbp_busy_timeout_callback(struct fw_xfer *xfer)
1099 struct sbp_dev *sdev;
1101 sdev = (struct sbp_dev *)xfer->sc;
1103 sbp_show_sdev_info(sdev, 2);
1104 printf("sbp_busy_timeout_callback\n");
1106 sbp_xfer_free(xfer);
1107 sbp_agent_reset(sdev);
1111 sbp_busy_timeout(struct sbp_dev *sdev)
1114 struct fw_xfer *xfer;
1116 sbp_show_sdev_info(sdev, 2);
1117 printf("sbp_busy_timeout\n");
1119 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1121 xfer->act.hand = sbp_busy_timeout_callback;
1122 fp = &xfer->send.hdr;
1123 fp->mode.wreqq.dest_hi = 0xffff;
1124 fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1125 fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1126 fw_asyreq(xfer->fc, -1, xfer);
1130 sbp_orb_pointer_callback(struct fw_xfer *xfer)
1132 struct sbp_dev *sdev;
1133 sdev = (struct sbp_dev *)xfer->sc;
1136 sbp_show_sdev_info(sdev, 2);
1137 printf("%s\n", __func__);
1139 if (xfer->resp != 0) {
1141 printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1143 sbp_xfer_free(xfer);
1144 sdev->flags &= ~ORB_POINTER_ACTIVE;
1146 if ((sdev->flags & ORB_POINTER_NEED) != 0) {
1147 struct sbp_ocb *ocb;
1149 sdev->flags &= ~ORB_POINTER_NEED;
1150 ocb = STAILQ_FIRST(&sdev->ocbs);
1152 sbp_orb_pointer(sdev, ocb);
1158 sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1160 struct fw_xfer *xfer;
1163 sbp_show_sdev_info(sdev, 2);
1164 printf("%s: 0x%08x\n", __func__, (u_int32_t)ocb->bus_addr);
1167 if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) {
1169 printf("%s: orb pointer active\n", __func__);
1171 sdev->flags |= ORB_POINTER_NEED;
1175 sdev->flags |= ORB_POINTER_ACTIVE;
1176 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1179 xfer->act.hand = sbp_orb_pointer_callback;
1181 fp = &xfer->send.hdr;
1182 fp->mode.wreqb.len = 8;
1183 fp->mode.wreqb.extcode = 0;
1184 xfer->send.payload[0] =
1185 htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1186 xfer->send.payload[1] = htonl((u_int32_t)ocb->bus_addr);
1188 if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1189 sbp_xfer_free(xfer);
1190 ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1197 sbp_cmd_callback(struct fw_xfer *xfer)
1200 struct sbp_dev *sdev;
1201 sdev = (struct sbp_dev *)xfer->sc;
1202 sbp_show_sdev_info(sdev, 2);
1203 printf("sbp_cmd_callback\n");
1205 if (xfer->resp != 0) {
1207 printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1209 sbp_xfer_free(xfer);
1214 sbp_doorbell(struct sbp_dev *sdev)
1216 struct fw_xfer *xfer;
1219 sbp_show_sdev_info(sdev, 2);
1220 printf("sbp_doorbell\n");
1223 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1226 xfer->act.hand = sbp_cmd_callback;
1227 fp = (struct fw_pkt *)xfer->send.buf;
1228 fp->mode.wreqq.data = htonl(0xf);
1229 fw_asyreq(xfer->fc, -1, xfer);
1233 static struct fw_xfer *
1234 sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1236 struct fw_xfer *xfer;
1238 struct sbp_target *target;
1241 target = sdev->target;
1243 xfer = STAILQ_FIRST(&target->xferlist);
1245 if (target->n_xfer > 5 /* XXX */) {
1246 printf("sbp: no more xfer for this target\n");
1250 xfer = fw_xfer_alloc_buf(M_SBP, 8, 0);
1252 printf("sbp: fw_xfer_alloc_buf failed\n");
1258 printf("sbp: alloc %d xfer\n", target->n_xfer);
1261 STAILQ_REMOVE_HEAD(&target->xferlist, link);
1265 microtime(&xfer->tv);
1268 xfer->recv.pay_len = 0;
1269 xfer->send.spd = min(sdev->target->fwdev->speed, max_speed);
1270 xfer->fc = sdev->target->sbp->fd.fc;
1271 xfer->retry_req = fw_asybusy;
1274 if (tcode == FWTCODE_WREQB)
1275 xfer->send.pay_len = 8;
1277 xfer->send.pay_len = 0;
1279 xfer->sc = (caddr_t)sdev;
1280 fp = &xfer->send.hdr;
1281 fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
1282 fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
1283 fp->mode.wreqq.tlrt = 0;
1284 fp->mode.wreqq.tcode = tcode;
1285 fp->mode.wreqq.pri = 0;
1286 fp->mode.wreqq.dst = FWLOCALBUS | sdev->target->fwdev->dst;
1293 sbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1295 struct fw_xfer *xfer;
1297 struct sbp_ocb *ocb;
1298 struct sbp_target *target;
1301 target = sdev->target;
1302 nid = target->sbp->fd.fc->nodeid | FWLOCALBUS;
1305 if (func == ORB_FUN_RUNQUEUE) {
1306 ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1307 if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1311 STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1314 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
1319 ocb->flags = OCB_ACT_MGM;
1322 bzero((void *)ocb->orb, sizeof(ocb->orb));
1323 ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1324 ocb->orb[7] = htonl(SBP_DEV2ADDR(target->target_id, sdev->lun_id));
1327 sbp_show_sdev_info(sdev, 2);
1328 printf("%s\n", orb_fun_name[(func>>16)&0xf]);
1332 ocb->orb[0] = ocb->orb[1] = 0; /* password */
1333 ocb->orb[2] = htonl(nid << 16);
1334 ocb->orb[3] = htonl(sdev->dma.bus_addr);
1335 ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id);
1337 ocb->orb[4] |= htonl(ORB_EXV);
1338 ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
1339 fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD);
1342 ocb->orb[0] = htonl((0 << 16) | 0);
1343 ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
1350 ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
1354 if (target->mgm_ocb_cur != NULL) {
1355 /* there is a standing ORB */
1356 STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1361 target->mgm_ocb_cur = ocb;
1364 callout_reset(&target->mgm_ocb_timeout, 5*hz,
1365 sbp_mgm_timeout, (caddr_t)ocb);
1366 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1370 xfer->act.hand = sbp_mgm_callback;
1372 fp = &xfer->send.hdr;
1373 fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
1374 fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
1375 fp->mode.wreqb.len = 8;
1376 fp->mode.wreqb.extcode = 0;
1377 xfer->send.payload[0] = htonl(nid << 16);
1378 xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff);
1380 sbp_show_sdev_info(sdev, 2);
1381 printf("mgm orb: %08x\n", (u_int32_t)ocb->bus_addr);
1384 fw_asyreq(xfer->fc, -1, xfer);
1388 sbp_print_scsi_cmd(struct sbp_ocb *ocb)
1390 struct ccb_scsiio *csio;
1392 csio = &ocb->ccb->csio;
1393 printf("%s:%d:%d XPT_SCSI_IO: "
1394 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1396 "%db cmd/%db data/%db sense\n",
1397 device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1398 ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun,
1399 csio->cdb_io.cdb_bytes[0],
1400 csio->cdb_io.cdb_bytes[1],
1401 csio->cdb_io.cdb_bytes[2],
1402 csio->cdb_io.cdb_bytes[3],
1403 csio->cdb_io.cdb_bytes[4],
1404 csio->cdb_io.cdb_bytes[5],
1405 csio->cdb_io.cdb_bytes[6],
1406 csio->cdb_io.cdb_bytes[7],
1407 csio->cdb_io.cdb_bytes[8],
1408 csio->cdb_io.cdb_bytes[9],
1409 ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1410 csio->cdb_len, csio->dxfer_len,
1415 sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1417 struct sbp_cmd_status *sbp_cmd_status;
1418 struct scsi_sense_data *sense;
1420 sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1421 sense = &ocb->ccb->csio.sense_data;
1424 sbp_print_scsi_cmd(ocb);
1425 /* XXX need decode status */
1426 sbp_show_sdev_info(ocb->sdev, 2);
1427 printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1428 sbp_cmd_status->status,
1429 sbp_cmd_status->sfmt,
1430 sbp_cmd_status->valid,
1431 sbp_cmd_status->s_key,
1432 sbp_cmd_status->s_code,
1433 sbp_cmd_status->s_qlfr,
1438 switch (sbp_cmd_status->status) {
1439 case SCSI_STATUS_CHECK_COND:
1440 case SCSI_STATUS_BUSY:
1441 case SCSI_STATUS_CMD_TERMINATED:
1442 if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1443 sense->error_code = SSD_CURRENT_ERROR;
1445 sense->error_code = SSD_DEFERRED_ERROR;
1447 if(sbp_cmd_status->valid)
1448 sense->error_code |= SSD_ERRCODE_VALID;
1449 sense->flags = sbp_cmd_status->s_key;
1450 if(sbp_cmd_status->mark)
1451 sense->flags |= SSD_FILEMARK;
1452 if(sbp_cmd_status->eom)
1453 sense->flags |= SSD_EOM;
1454 if(sbp_cmd_status->ill_len)
1455 sense->flags |= SSD_ILI;
1457 bcopy(&sbp_cmd_status->info, &sense->info[0], 4);
1459 if (sbp_status->len <= 1)
1460 /* XXX not scsi status. shouldn't be happened */
1461 sense->extra_len = 0;
1462 else if (sbp_status->len <= 4)
1463 /* add_sense_code(_qual), info, cmd_spec_info */
1464 sense->extra_len = 6;
1466 /* fru, sense_key_spec */
1467 sense->extra_len = 10;
1469 bcopy(&sbp_cmd_status->cdb, &sense->cmd_spec_info[0], 4);
1471 sense->add_sense_code = sbp_cmd_status->s_code;
1472 sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1473 sense->fru = sbp_cmd_status->fru;
1475 bcopy(&sbp_cmd_status->s_keydep[0],
1476 &sense->sense_key_spec[0], 3);
1478 ocb->ccb->csio.scsi_status = sbp_cmd_status->status;
1479 ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1480 | CAM_AUTOSNS_VALID;
1485 for( j = 0 ; j < 32 ; j+=8){
1486 printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1487 tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1488 tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1495 sbp_show_sdev_info(ocb->sdev, 2);
1496 printf("sbp_scsi_status: unknown scsi status 0x%x\n",
1497 sbp_cmd_status->status);
1502 sbp_fix_inq_data(struct sbp_ocb *ocb)
1505 struct sbp_dev *sdev;
1506 struct scsi_inquiry_data *inq;
1511 if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1514 sbp_show_sdev_info(sdev, 2);
1515 printf("sbp_fix_inq_data\n");
1517 inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1518 switch (SID_TYPE(inq)) {
1522 * XXX Convert Direct Access device to RBC.
1523 * I've never seen FireWire DA devices which support READ_6.
1525 if (SID_TYPE(inq) == T_DIRECT)
1526 inq->device |= T_RBC; /* T_DIRECT == 0 */
1530 /* enable tagged queuing */
1532 inq->flags |= SID_CmdQue;
1534 inq->flags &= ~SID_CmdQue;
1536 * Override vendor/product/revision information.
1537 * Some devices sometimes return strange strings.
1540 bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1541 bcopy(sdev->product, inq->product, sizeof(inq->product));
1542 bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1549 sbp_recv1(struct fw_xfer *xfer)
1555 struct sbp_softc *sbp;
1556 struct sbp_dev *sdev;
1557 struct sbp_ocb *ocb;
1558 struct sbp_login_res *login_res = NULL;
1559 struct sbp_status *sbp_status;
1560 struct sbp_target *target;
1561 int orb_fun, status_valid0, status_valid, t, l, reset_agent = 0;
1565 ld = xfer->recv.buf;
1566 printf("sbp %x %d %d %08x %08x %08x %08x\n",
1567 xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1568 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1569 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1571 sbp = (struct sbp_softc *)xfer->sc;
1572 if (xfer->resp != 0){
1573 printf("sbp_recv: xfer->resp = %d\n", xfer->resp);
1576 if (xfer->recv.payload == NULL){
1577 printf("sbp_recv: xfer->recv.payload == NULL\n");
1580 rfp = &xfer->recv.hdr;
1581 if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1582 printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1585 sbp_status = (struct sbp_status *)xfer->recv.payload;
1586 addr = rfp->mode.wreqb.dest_lo;
1588 printf("received address 0x%x\n", addr);
1590 t = SBP_ADDR2TRG(addr);
1591 if (t >= SBP_NUM_TARGETS) {
1592 device_printf(sbp->fd.dev,
1593 "sbp_recv1: invalid target %d\n", t);
1596 target = &sbp->targets[t];
1597 l = SBP_ADDR2LUN(addr);
1598 if (l >= target->num_lun || target->luns[l] == NULL) {
1599 device_printf(sbp->fd.dev,
1600 "sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1603 sdev = target->luns[l];
1606 switch (sbp_status->src) {
1609 /* check mgm_ocb_cur first */
1610 ocb = target->mgm_ocb_cur;
1612 if (OCB_MATCH(ocb, sbp_status)) {
1613 callout_stop(&target->mgm_ocb_timeout);
1614 target->mgm_ocb_cur = NULL;
1618 ocb = sbp_dequeue_ocb(sdev, sbp_status);
1620 sbp_show_sdev_info(sdev, 2);
1621 printf("No ocb(%x) on the queue\n",
1622 ntohl(sbp_status->orb_lo));
1627 sbp_show_sdev_info(sdev, 2);
1628 printf("unsolicit status received\n");
1631 sbp_show_sdev_info(sdev, 2);
1632 printf("unknown sbp_status->src\n");
1635 status_valid0 = (sbp_status->src < 2
1636 && sbp_status->resp == ORB_RES_CMPL
1637 && sbp_status->dead == 0);
1638 status_valid = (status_valid0 && sbp_status->status == 0);
1640 if (!status_valid0 || debug > 2){
1643 sbp_show_sdev_info(sdev, 2);
1644 printf("ORB status src:%x resp:%x dead:%x"
1645 " len:%x stat:%x orb:%x%08x\n",
1646 sbp_status->src, sbp_status->resp, sbp_status->dead,
1647 sbp_status->len, sbp_status->status,
1648 ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1650 sbp_show_sdev_info(sdev, 2);
1651 status = sbp_status->status;
1652 switch(sbp_status->resp) {
1654 if (status > MAX_ORB_STATUS0)
1655 printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1657 printf("%s\n", orb_status0[status]);
1660 printf("Obj: %s, Error: %s\n",
1661 orb_status1_object[(status>>6) & 3],
1662 orb_status1_serial_bus_error[status & 0xf]);
1665 printf("Illegal request\n");
1668 printf("Vendor dependent\n");
1671 printf("unknown respose code %d\n", sbp_status->resp);
1675 /* we have to reset the fetch agent if it's dead */
1676 if (sbp_status->dead) {
1678 xpt_freeze_devq(sdev->path, 1);
1687 switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1693 switch(ocb->flags) {
1695 orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1699 fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD);
1700 login_res = sdev->login;
1701 login_res->len = ntohs(login_res->len);
1702 login_res->id = ntohs(login_res->id);
1703 login_res->cmd_hi = ntohs(login_res->cmd_hi);
1704 login_res->cmd_lo = ntohl(login_res->cmd_lo);
1707 sbp_show_sdev_info(sdev, 2);
1708 printf("login: len %d, ID %d, cmd %08x%08x, recon_hold %d\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo, ntohs(login_res->recon_hold));
1710 sbp_busy_timeout(sdev);
1712 /* forgot logout? */
1713 sbp_show_sdev_info(sdev, 2);
1714 printf("login failed\n");
1715 sdev->status = SBP_DEV_RESET;
1719 login_res = sdev->login;
1722 sbp_show_sdev_info(sdev, 2);
1723 printf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo);
1726 if (sdev->status == SBP_DEV_ATTACHED)
1729 sbp_agent_reset(sdev);
1731 sdev->status = SBP_DEV_ATTACHED;
1732 sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL);
1735 /* reconnection hold time exceed? */
1737 sbp_show_sdev_info(sdev, 2);
1738 printf("reconnect failed\n");
1744 sdev->status = SBP_DEV_RESET;
1747 sbp_busy_timeout(sdev);
1752 sbp_agent_reset(sdev);
1755 sbp_show_sdev_info(sdev, 2);
1756 printf("unknown function %d\n", orb_fun);
1759 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
1763 if(ocb->ccb != NULL){
1767 ld = ocb->ccb->csio.data_ptr;
1768 if(ld != NULL && ocb->ccb->csio.dxfer_len != 0)
1769 printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]);
1771 printf("ptr NULL\n");
1772 printf("len %d\n", sbp_status->len);
1775 if(sbp_status->len > 1){
1776 sbp_scsi_status(sbp_status, ocb);
1778 if(sbp_status->resp != ORB_RES_CMPL){
1779 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1781 ccb->ccb_h.status = CAM_REQ_CMP;
1784 /* fix up inq data */
1785 if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1786 sbp_fix_inq_data(ocb);
1795 sbp_free_ocb(sdev, ocb);
1798 sbp_agent_reset(sdev);
1801 xfer->recv.pay_len = SBP_RECV_LEN;
1802 /* The received packet is usually small enough to be stored within
1803 * the buffer. In that case, the controller return ack_complete and
1804 * no respose is necessary.
1806 * XXX fwohci.c and firewire.c should inform event_code such as
1807 * ack_complete or ack_pending to upper driver.
1811 sfp = (struct fw_pkt *)xfer->send.buf;
1812 sfp->mode.wres.dst = rfp->mode.wreqb.src;
1813 xfer->dst = sfp->mode.wres.dst;
1814 xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1815 xfer->act.hand = sbp_loginres_callback;
1816 xfer->retry_req = fw_asybusy;
1818 sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1819 sfp->mode.wres.tcode = FWTCODE_WRES;
1820 sfp->mode.wres.rtcode = 0;
1821 sfp->mode.wres.pri = 0;
1823 fw_asyreq(xfer->fc, -1, xfer);
1826 STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1834 sbp_recv(struct fw_xfer *xfer)
1844 sbp_attach(device_t dev)
1846 struct sbp_softc *sbp;
1847 struct cam_devq *devq;
1848 struct fw_xfer *xfer;
1852 printf("sbp_attach (cold=%d)\n", cold);
1857 sbp = ((struct sbp_softc *)device_get_softc(dev));
1858 bzero(sbp, sizeof(struct sbp_softc));
1860 sbp->fd.fc = device_get_ivars(dev);
1863 max_speed = sbp->fd.fc->speed;
1865 error = bus_dma_tag_create(/*parent*/sbp->fd.fc->dmat,
1866 /* XXX shoud be 4 for sane backend? */
1869 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1870 /*highaddr*/BUS_SPACE_MAXADDR,
1871 /*filter*/NULL, /*filterarg*/NULL,
1872 /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1873 /*maxsegsz*/SBP_SEG_MAX,
1874 /*flags*/BUS_DMA_ALLOCNOW,
1875 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
1876 /*lockfunc*/busdma_lock_mutex,
1881 printf("sbp_attach: Could not allocate DMA tag "
1882 "- error %d\n", error);
1886 devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1890 for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1891 sbp->targets[i].fwdev = NULL;
1892 sbp->targets[i].luns = NULL;
1895 sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1896 device_get_unit(dev),
1898 /*tagged*/ SBP_QUEUE_LEN - 1,
1900 cam_simq_release(devq);
1901 if (sbp->sim == NULL)
1904 if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS)
1907 if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim),
1908 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1909 xpt_bus_deregister(cam_sim_path(sbp->sim));
1913 /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1914 sbp->fwb.start = ((u_int64_t)SBP_BIND_HI << 32) | SBP_DEV2ADDR(0, 0);
1915 sbp->fwb.end = sbp->fwb.start + 0xffff;
1916 sbp->fwb.act_type = FWACT_XFER;
1917 /* pre-allocate xfer */
1918 STAILQ_INIT(&sbp->fwb.xferlist);
1919 for (i = 0; i < SBP_NUM_OCB/2; i ++) {
1920 xfer = fw_xfer_alloc_buf(M_SBP,
1922 /* recv */SBP_RECV_LEN);
1923 xfer->act.hand = sbp_recv;
1925 xfer->fc = sbp->fd.fc;
1927 xfer->sc = (caddr_t)sbp;
1928 STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1930 fw_bindadd(sbp->fd.fc, &sbp->fwb);
1932 sbp->fd.post_busreset = sbp_post_busreset;
1933 sbp->fd.post_explore = sbp_post_explore;
1935 if (sbp->fd.fc->status != -1) {
1937 sbp_post_busreset((void *)sbp);
1938 sbp_post_explore((void *)sbp);
1941 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
1945 cam_sim_free(sbp->sim);
1950 sbp_logout_all(struct sbp_softc *sbp)
1952 struct sbp_target *target;
1953 struct sbp_dev *sdev;
1957 printf("sbp_logout_all\n");
1959 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
1960 target = &sbp->targets[i];
1961 if (target->luns == NULL)
1963 for (j = 0; j < target->num_lun; j++) {
1964 sdev = target->luns[j];
1967 callout_stop(&sdev->login_callout);
1968 if (sdev->status >= SBP_DEV_TOATTACH &&
1969 sdev->status <= SBP_DEV_ATTACHED)
1970 sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
1978 sbp_shutdown(device_t dev)
1980 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1982 sbp_logout_all(sbp);
1987 sbp_free_sdev(struct sbp_dev *sdev)
1993 for (i = 0; i < SBP_QUEUE_LEN; i++)
1994 bus_dmamap_destroy(sdev->target->sbp->dmat,
1995 sdev->ocb[i].dmamap);
1996 fwdma_free(sdev->target->sbp->fd.fc, &sdev->dma);
2001 sbp_free_target(struct sbp_target *target)
2003 struct sbp_softc *sbp;
2004 struct fw_xfer *xfer, *next;
2007 if (target->luns == NULL)
2009 callout_stop(&target->mgm_ocb_timeout);
2011 for (i = 0; i < target->num_lun; i++)
2012 sbp_free_sdev(target->luns[i]);
2014 for (xfer = STAILQ_FIRST(&target->xferlist);
2015 xfer != NULL; xfer = next) {
2016 next = STAILQ_NEXT(xfer, link);
2017 fw_xfer_free_buf(xfer);
2019 STAILQ_INIT(&target->xferlist);
2020 kfree(target->luns, M_SBP);
2021 target->num_lun = 0;
2022 target->luns = NULL;
2023 target->fwdev = NULL;
2027 sbp_detach(device_t dev)
2029 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2030 struct firewire_comm *fc = sbp->fd.fc;
2031 struct fw_xfer *xfer, *next;
2035 printf("sbp_detach\n");
2038 for (i = 0; i < SBP_NUM_TARGETS; i ++)
2039 sbp_cam_detach_target(&sbp->targets[i]);
2040 xpt_async(AC_LOST_DEVICE, sbp->path, NULL);
2041 xpt_free_path(sbp->path);
2042 xpt_bus_deregister(cam_sim_path(sbp->sim));
2043 cam_sim_free(sbp->sim);
2045 sbp_logout_all(sbp);
2047 /* XXX wait for logout completion */
2048 tsleep(&i, FWPRI, "sbpdtc", hz/2);
2050 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++)
2051 sbp_free_target(&sbp->targets[i]);
2053 for (xfer = STAILQ_FIRST(&sbp->fwb.xferlist);
2054 xfer != NULL; xfer = next) {
2055 next = STAILQ_NEXT(xfer, link);
2056 fw_xfer_free_buf(xfer);
2058 STAILQ_INIT(&sbp->fwb.xferlist);
2059 fw_bindremove(fc, &sbp->fwb);
2061 bus_dma_tag_destroy(sbp->dmat);
2067 sbp_cam_detach_sdev(struct sbp_dev *sdev)
2071 if (sdev->status == SBP_DEV_DEAD)
2073 if (sdev->status == SBP_DEV_RESET)
2076 xpt_release_devq(sdev->path,
2077 sdev->freeze, TRUE);
2079 xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
2080 xpt_free_path(sdev->path);
2083 sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
2087 sbp_cam_detach_target(struct sbp_target *target)
2091 if (target->luns != NULL) {
2093 printf("sbp_detach_target %d\n", target->target_id);
2095 callout_stop(&target->scan_callout);
2096 for (i = 0; i < target->num_lun; i++)
2097 sbp_cam_detach_sdev(target->luns[i]);
2102 sbp_target_reset(struct sbp_dev *sdev, int method)
2105 struct sbp_target *target = sdev->target;
2106 struct sbp_dev *tsdev;
2108 for (i = 0; i < target->num_lun; i++) {
2109 tsdev = target->luns[i];
2112 if (tsdev->status == SBP_DEV_DEAD)
2114 if (tsdev->status == SBP_DEV_RESET)
2116 xpt_freeze_devq(tsdev->path, 1);
2118 sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT);
2120 tsdev->status = SBP_DEV_LOGIN;
2124 printf("target reset\n");
2125 sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2128 printf("reset start\n");
2129 sbp_reset_start(sdev);
2136 sbp_mgm_timeout(void *arg)
2138 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2139 struct sbp_dev *sdev = ocb->sdev;
2140 struct sbp_target *target = sdev->target;
2142 sbp_show_sdev_info(sdev, 2);
2143 printf("request timeout(mgm orb:0x%08x) ... ",
2144 (u_int32_t)ocb->bus_addr);
2145 target->mgm_ocb_cur = NULL;
2146 sbp_free_ocb(sdev, ocb);
2149 printf("run next request\n");
2150 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2153 printf("reset start\n");
2154 sbp_reset_start(sdev);
2159 sbp_timeout(void *arg)
2161 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2162 struct sbp_dev *sdev = ocb->sdev;
2164 sbp_show_sdev_info(sdev, 2);
2165 printf("request timeout(cmd orb:0x%08x) ... ",
2166 (u_int32_t)ocb->bus_addr);
2169 switch(sdev->timeout) {
2171 printf("agent reset\n");
2172 xpt_freeze_devq(sdev->path, 1);
2174 sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2175 sbp_agent_reset(sdev);
2179 sbp_target_reset(sdev, sdev->timeout - 1);
2184 sbp_cam_detach_target(target);
2185 if (target->luns != NULL)
2186 kfree(target->luns, M_SBP);
2187 target->num_lun = 0;
2188 target->luns = NULL;
2189 target->fwdev = NULL;
2195 sbp_action1(struct cam_sim *sim, union ccb *ccb)
2198 struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2199 struct sbp_target *target = NULL;
2200 struct sbp_dev *sdev = NULL;
2202 /* target:lun -> sdev mapping */
2204 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2205 && ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2206 target = &sbp->targets[ccb->ccb_h.target_id];
2207 if (target->fwdev != NULL
2208 && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2209 && ccb->ccb_h.target_lun < target->num_lun) {
2210 sdev = target->luns[ccb->ccb_h.target_lun];
2211 if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED &&
2212 sdev->status != SBP_DEV_PROBE)
2219 printf("invalid target %d lun %d\n",
2220 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2223 switch (ccb->ccb_h.func_code) {
2226 case XPT_GET_TRAN_SETTINGS:
2227 case XPT_SET_TRAN_SETTINGS:
2228 case XPT_CALC_GEOMETRY:
2231 printf("%s:%d:%d:func_code 0x%04x: "
2232 "Invalid target (target needed)\n",
2233 device_get_nameunit(sbp->fd.dev),
2234 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2235 ccb->ccb_h.func_code);
2238 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2245 /* The opcodes sometimes aimed at a target (sc is valid),
2246 * sometimes aimed at the SIM (sc is invalid and target is
2247 * CAM_TARGET_WILDCARD)
2250 ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2252 printf("%s:%d:%d func_code 0x%04x: "
2253 "Invalid target (no wildcard)\n",
2254 device_get_nameunit(sbp->fd.dev),
2255 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2256 ccb->ccb_h.func_code);
2258 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2264 /* XXX Hm, we should check the input parameters */
2268 switch (ccb->ccb_h.func_code) {
2271 struct ccb_scsiio *csio;
2272 struct sbp_ocb *ocb;
2279 printf("%s:%d:%d XPT_SCSI_IO: "
2280 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2282 "%db cmd/%db data/%db sense\n",
2283 device_get_nameunit(sbp->fd.dev),
2284 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2285 csio->cdb_io.cdb_bytes[0],
2286 csio->cdb_io.cdb_bytes[1],
2287 csio->cdb_io.cdb_bytes[2],
2288 csio->cdb_io.cdb_bytes[3],
2289 csio->cdb_io.cdb_bytes[4],
2290 csio->cdb_io.cdb_bytes[5],
2291 csio->cdb_io.cdb_bytes[6],
2292 csio->cdb_io.cdb_bytes[7],
2293 csio->cdb_io.cdb_bytes[8],
2294 csio->cdb_io.cdb_bytes[9],
2295 ccb->ccb_h.flags & CAM_DIR_MASK,
2296 csio->cdb_len, csio->dxfer_len,
2300 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2305 /* if we are in probe stage, pass only probe commands */
2306 if (sdev->status == SBP_DEV_PROBE) {
2308 name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2309 printf("probe stage, periph name: %s\n", name);
2310 if (strcmp(name, "probe") != 0) {
2311 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2317 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
2318 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2323 ocb->flags = OCB_ACT_CMD;
2326 ccb->ccb_h.ccb_sdev_ptr = sdev;
2327 ocb->orb[0] = htonl(1 << 31);
2329 ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2330 ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2331 speed = min(target->fwdev->speed, max_speed);
2332 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2333 | ORB_CMD_MAXP(speed + 7));
2334 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
2335 ocb->orb[4] |= htonl(ORB_CMD_IN);
2338 if (csio->ccb_h.flags & CAM_SCATTER_VALID)
2339 printf("sbp: CAM_SCATTER_VALID\n");
2340 if (csio->ccb_h.flags & CAM_DATA_PHYS)
2341 printf("sbp: CAM_DATA_PHYS\n");
2343 if (csio->ccb_h.flags & CAM_CDB_POINTER)
2344 cdb = (void *)csio->cdb_io.cdb_ptr;
2346 cdb = (void *)&csio->cdb_io.cdb_bytes;
2347 bcopy(cdb, (void *)&ocb->orb[5], csio->cdb_len);
2349 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2350 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2352 if (ccb->csio.dxfer_len > 0) {
2356 error = bus_dmamap_load(/*dma tag*/sbp->dmat,
2357 /*dma map*/ocb->dmamap,
2359 ccb->csio.dxfer_len,
2365 printf("sbp: bus_dmamap_load error %d\n", error);
2367 sbp_execute_ocb(ocb, NULL, 0, 0);
2370 case XPT_CALC_GEOMETRY:
2372 struct ccb_calc_geometry *ccg;
2373 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2375 u_int32_t secs_per_cylinder;
2380 if (ccg->block_size == 0) {
2381 printf("sbp_action1: block_size is 0.\n");
2382 ccb->ccb_h.status = CAM_REQ_INVALID;
2387 printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2388 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2389 "Volume size = %d\n",
2391 "Volume size = %jd\n",
2393 device_get_nameunit(sbp->fd.dev),
2394 cam_sim_path(sbp->sim),
2395 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2396 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2402 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2403 size_mb = ccg->volume_size
2404 / ((1024L * 1024L) / ccg->block_size);
2406 if (size_mb > 1024 && extended) {
2408 ccg->secs_per_track = 63;
2411 ccg->secs_per_track = 32;
2413 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2414 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2415 ccb->ccb_h.status = CAM_REQ_CMP;
2417 cam_calc_geometry(ccg, /*extended*/1);
2422 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
2426 printf("%s:%d:XPT_RESET_BUS: \n",
2427 device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2430 ccb->ccb_h.status = CAM_REQ_INVALID;
2434 case XPT_PATH_INQ: /* Path routing inquiry */
2436 struct ccb_pathinq *cpi = &ccb->cpi;
2439 printf("%s:%d:%d XPT_PATH_INQ:.\n",
2440 device_get_nameunit(sbp->fd.dev),
2441 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2443 cpi->version_num = 1; /* XXX??? */
2444 cpi->hba_inquiry = PI_TAG_ABLE;
2445 cpi->target_sprt = 0;
2446 cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE;
2447 cpi->hba_eng_cnt = 0;
2448 cpi->max_target = SBP_NUM_TARGETS - 1;
2449 cpi->max_lun = SBP_NUM_LUNS - 1;
2450 cpi->initiator_id = SBP_INITIATOR;
2451 cpi->bus_id = sim->bus_id;
2452 cpi->base_transfer_speed = 400 * 1000 / 8;
2453 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2454 strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2455 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2456 cpi->unit_number = sim->unit_number;
2458 cpi->ccb_h.status = CAM_REQ_CMP;
2462 case XPT_GET_TRAN_SETTINGS:
2464 struct ccb_trans_settings *cts = &ccb->cts;
2466 printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2467 device_get_nameunit(sbp->fd.dev),
2468 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2470 /* Enable disconnect and tagged queuing */
2471 cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2472 cts->flags = CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB;
2474 cts->ccb_h.status = CAM_REQ_CMP;
2479 ccb->ccb_h.status = CAM_UA_ABORT;
2482 case XPT_SET_TRAN_SETTINGS:
2485 ccb->ccb_h.status = CAM_REQ_INVALID;
2493 sbp_action(struct cam_sim *sim, union ccb *ccb)
2496 sbp_action1(sim, ccb);
2501 sbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error)
2504 struct sbp_ocb *ocb;
2505 struct sbp_ocb *prev;
2506 bus_dma_segment_t *s;
2509 printf("sbp_execute_ocb: error=%d\n", error);
2511 ocb = (struct sbp_ocb *)arg;
2514 printf("sbp_execute_ocb: seg %d", seg);
2515 for (i = 0; i < seg; i++)
2516 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2517 printf(", %x:%d", segments[i].ds_addr, segments[i].ds_len);
2519 printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2520 (uintmax_t)segments[i].ds_len);
2526 /* direct pointer */
2528 if (s->ds_len > SBP_SEG_MAX)
2529 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2530 ocb->orb[3] = htonl(s->ds_addr);
2531 ocb->orb[4] |= htonl(s->ds_len);
2532 } else if(seg > 1) {
2534 for (i = 0; i < seg; i++) {
2537 /* XXX LSI Logic "< 16 byte" bug might be hit */
2539 printf("sbp_execute_ocb: warning, "
2540 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2541 "segment length(%d) is less than 16."
2543 "segment length(%zd) is less than 16."
2545 "(seg=%d/%d)\n", s->ds_len, i+1, seg);
2547 if (s->ds_len > SBP_SEG_MAX)
2548 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2549 ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2550 ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2552 ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2556 bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2557 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2558 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2559 prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2560 fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2561 if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) {
2562 ocb->sdev->flags &= ~ORB_LINK_DEAD;
2563 sbp_orb_pointer(ocb->sdev, ocb);
2568 sbp_poll(struct cam_sim *sim)
2570 struct sbp_softc *sbp;
2571 struct firewire_comm *fc;
2573 sbp = (struct sbp_softc *)sim->softc;
2576 fc->poll(fc, 0, -1);
2581 static struct sbp_ocb *
2582 sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2584 struct sbp_ocb *ocb;
2585 struct sbp_ocb *next;
2592 sbp_show_sdev_info(sdev, 2);
2593 printf("%s: 0x%08x src %d\n",
2594 __func__, ntohl(sbp_status->orb_lo), sbp_status->src);
2596 for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2597 next = STAILQ_NEXT(ocb, ocb);
2599 if (OCB_MATCH(ocb, sbp_status)) {
2601 STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2602 if (ocb->ccb != NULL)
2603 callout_stop(&ocb->ccb->ccb_h.timeout_ch);
2604 if (ntohl(ocb->orb[4]) & 0xffff) {
2605 bus_dmamap_sync(sdev->target->sbp->dmat,
2607 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2608 BUS_DMASYNC_POSTREAD :
2609 BUS_DMASYNC_POSTWRITE);
2610 bus_dmamap_unload(sdev->target->sbp->dmat,
2613 if (sbp_status->src == SRC_NO_NEXT) {
2615 sbp_orb_pointer(sdev, next);
2616 else if (order > 0) {
2618 * Unordered execution
2619 * We need to send pointer for
2622 sdev->flags |= ORB_LINK_DEAD;
2631 if (ocb && order > 0) {
2632 sbp_show_sdev_info(sdev, 2);
2633 printf("unordered execution order:%d\n", order);
2639 static struct sbp_ocb *
2640 sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2642 struct sbp_ocb *prev;
2647 sbp_show_sdev_info(sdev, 2);
2648 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2649 printf("%s: 0x%08x\n", __func__, ocb->bus_addr);
2651 printf("%s: 0x%08jx\n", __func__, (uintmax_t)ocb->bus_addr);
2654 prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2655 STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2657 if (ocb->ccb != NULL)
2658 callout_reset(&ocb->ccb->ccb_h.timeout_ch,
2659 (ocb->ccb->ccb_h.timeout * hz) / 1000, sbp_timeout, ocb);
2663 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2664 printf("linking chain 0x%x -> 0x%x\n",
2665 prev->bus_addr, ocb->bus_addr);
2667 printf("linking chain 0x%jx -> 0x%jx\n",
2668 (uintmax_t)prev->bus_addr, (uintmax_t)ocb->bus_addr);
2671 prev->orb[1] = htonl(ocb->bus_addr);
2679 static struct sbp_ocb *
2680 sbp_get_ocb(struct sbp_dev *sdev)
2682 struct sbp_ocb *ocb;
2685 ocb = STAILQ_FIRST(&sdev->free_ocbs);
2687 printf("ocb shortage!!!\n");
2690 STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2697 sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2701 STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2705 sbp_abort_ocb(struct sbp_ocb *ocb, int status)
2707 struct sbp_dev *sdev;
2711 sbp_show_sdev_info(sdev, 2);
2712 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2713 printf("sbp_abort_ocb 0x%x\n", ocb->bus_addr);
2715 printf("sbp_abort_ocb 0x%jx\n", (uintmax_t)ocb->bus_addr);
2719 if (ocb->ccb != NULL)
2720 sbp_print_scsi_cmd(ocb);
2722 if (ntohl(ocb->orb[4]) & 0xffff) {
2723 bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
2724 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2725 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2726 bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
2728 if (ocb->ccb != NULL) {
2729 callout_stop(&ocb->ccb->ccb_h.timeout_ch);
2730 ocb->ccb->ccb_h.status = status;
2733 sbp_free_ocb(sdev, ocb);
2737 sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2739 struct sbp_ocb *ocb, *next;
2740 STAILQ_HEAD(, sbp_ocb) temp;
2743 bcopy(&sdev->ocbs, &temp, sizeof(temp));
2744 STAILQ_INIT(&sdev->ocbs);
2745 for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2746 next = STAILQ_NEXT(ocb, ocb);
2747 sbp_abort_ocb(ocb, status);
2752 static devclass_t sbp_devclass;
2755 * Because sbp is a static device that always exists under any attached
2756 * firewire device, and not scanned by the firewire device, we need an
2757 * identify function to install the device. For our sanity we want
2758 * the sbp device to have the same unit number as the fireweire device.
2761 static device_method_t sbp_methods[] = {
2762 /* device interface */
2763 DEVMETHOD(device_identify, bus_generic_identify_sameunit),
2764 DEVMETHOD(device_probe, sbp_probe),
2765 DEVMETHOD(device_attach, sbp_attach),
2766 DEVMETHOD(device_detach, sbp_detach),
2767 DEVMETHOD(device_shutdown, sbp_shutdown),
2772 static driver_t sbp_driver = {
2775 sizeof(struct sbp_softc),
2778 DECLARE_DUMMY_MODULE(sbp);
2779 DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2780 MODULE_VERSION(sbp, 1);
2781 MODULE_DEPEND(sbp, firewire, 1, 1, 1);
2782 MODULE_DEPEND(sbp, cam, 1, 1, 1);