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 $
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/module.h>
42 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
44 #include <sys/malloc.h>
46 #include <sys/devicestat.h> /* for struct devstat */
47 #include <sys/thread2.h>
49 #include <bus/cam/cam.h>
50 #include <bus/cam/cam_ccb.h>
51 #include <bus/cam/cam_sim.h>
52 #include <bus/cam/cam_xpt_sim.h>
53 #include <bus/cam/cam_debug.h>
54 #include <bus/cam/cam_periph.h>
55 #include <bus/cam/scsi/scsi_all.h>
57 #include <bus/firewire/firewire.h>
58 #include <bus/firewire/firewirereg.h>
59 #include <bus/firewire/fwdma.h>
60 #include <bus/firewire/iec13213.h>
63 #define ccb_sdev_ptr spriv_ptr0
64 #define ccb_sbp_ptr spriv_ptr1
66 #define SBP_NUM_TARGETS 8 /* MAX 64 */
68 * Scan_bus doesn't work for more than 8 LUNs
69 * because of CAM_SCSI2_MAXLUN in cam_xpt.c
71 #define SBP_NUM_LUNS 64
72 #define SBP_DMA_SIZE PAGE_SIZE
73 #define SBP_LOGIN_SIZE sizeof(struct sbp_login_res)
74 #define SBP_QUEUE_LEN ((SBP_DMA_SIZE - SBP_LOGIN_SIZE) / sizeof(struct sbp_ocb))
75 #define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS)
78 * STATUS FIFO addressing
80 * -----------------------
81 * 0- 1( 2): 0 (alingment)
85 * 32-47(16): SBP_BIND_HI
86 * 48-64(16): bus_id, node_id
88 #define SBP_BIND_HI 0x1
89 #define SBP_DEV2ADDR(t, l) \
90 (((u_int64_t)SBP_BIND_HI << 32) \
91 | (((l) & 0xff) << 8) \
92 | (((t) & 0x3f) << 2))
93 #define SBP_ADDR2TRG(a) (((a) >> 2) & 0x3f)
94 #define SBP_ADDR2LUN(a) (((a) >> 8) & 0xff)
95 #define SBP_INITIATOR 7
97 static char *orb_fun_name[] = {
101 static int debug = 0;
102 static int auto_login = 1;
103 static int max_speed = -1;
105 static int sbp_cold = 1;
107 static int ex_login = 1;
108 static int login_delay = 1000; /* msec */
109 static int scan_delay = 500; /* msec */
110 static int sbp_tags = 0;
112 SYSCTL_DECL(_hw_firewire);
113 SYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem");
114 SYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0,
116 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0,
117 "SBP perform login automatically");
118 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0,
119 "SBP transfer max speed");
120 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, exclusive_login, CTLFLAG_RW,
121 &ex_login, 0, "SBP transfer max speed");
122 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, login_delay, CTLFLAG_RW,
123 &login_delay, 0, "SBP login delay in msec");
124 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, scan_delay, CTLFLAG_RW,
125 &scan_delay, 0, "SBP scan delay in msec");
126 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, tags, CTLFLAG_RW, &sbp_tags, 0,
127 "SBP tagged queuing support");
129 TUNABLE_INT("hw.firewire.sbp.auto_login", &auto_login);
130 TUNABLE_INT("hw.firewire.sbp.max_speed", &max_speed);
131 TUNABLE_INT("hw.firewire.sbp.exclusive_login", &ex_login);
132 TUNABLE_INT("hw.firewire.sbp.login_delay", &login_delay);
133 TUNABLE_INT("hw.firewire.sbp.scan_delay", &scan_delay);
134 TUNABLE_INT("hw.firewire.sbp.tags", &sbp_tags);
136 #define NEED_RESPONSE 0
138 #define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE)
139 #ifdef __sparc64__ /* iommu */
140 #define SBP_IND_MAX howmany(MAXPHYS, SBP_SEG_MAX)
142 #define SBP_IND_MAX howmany(MAXPHYS, PAGE_SIZE)
145 STAILQ_ENTRY(sbp_ocb) ocb;
149 #define IND_PTR_OFFSET (8*sizeof(u_int32_t))
150 struct ind_ptr ind_ptr[SBP_IND_MAX];
151 struct sbp_dev *sdev;
152 int flags; /* XXX should be removed */
156 #define OCB_ACT_MGM 0
157 #define OCB_ACT_CMD 1
158 #define OCB_MATCH(o,s) ((o)->bus_addr == ntohl((s)->orb_lo))
161 #define SBP_DEV_RESET 0 /* accept login */
162 #define SBP_DEV_LOGIN 1 /* to login */
164 #define SBP_DEV_RECONN 2 /* to reconnect */
166 #define SBP_DEV_TOATTACH 3 /* to attach */
167 #define SBP_DEV_PROBE 4 /* scan lun */
168 #define SBP_DEV_ATTACHED 5 /* in operation */
169 #define SBP_DEV_DEAD 6 /* unavailable unit */
170 #define SBP_DEV_RETRY 7 /* unavailable unit */
176 #define ORB_LINK_DEAD (1 << 0)
177 #define VALID_LUN (1 << 1)
178 #define ORB_POINTER_ACTIVE (1 << 2)
179 #define ORB_POINTER_NEED (1 << 3)
181 struct cam_path *path;
182 struct sbp_target *target;
183 struct fwdma_alloc dma;
184 struct sbp_login_res *login;
185 struct callout login_callout;
187 STAILQ_HEAD(, sbp_ocb) ocbs;
188 STAILQ_HEAD(, sbp_ocb) free_ocbs;
197 struct sbp_dev **luns;
198 struct sbp_softc *sbp;
199 struct fw_device *fwdev;
200 u_int32_t mgm_hi, mgm_lo;
201 struct sbp_ocb *mgm_ocb_cur;
202 STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue;
203 struct callout mgm_ocb_timeout;
204 struct callout scan_callout;
205 STAILQ_HEAD(, fw_xfer) xferlist;
210 struct firewire_dev_comm fd;
212 struct cam_path *path;
213 struct sbp_target targets[SBP_NUM_TARGETS];
216 struct timeval last_busreset;
217 #define SIMQ_FREEZED 1
221 static void sbp_post_explore (void *);
222 static void sbp_recv (struct fw_xfer *);
223 static void sbp_mgm_callback (struct fw_xfer *);
225 static void sbp_cmd_callback (struct fw_xfer *);
227 static void sbp_orb_pointer (struct sbp_dev *, struct sbp_ocb *);
228 static void sbp_execute_ocb (void *, bus_dma_segment_t *, int, int);
229 static void sbp_free_ocb (struct sbp_dev *, struct sbp_ocb *);
230 static void sbp_abort_ocb (struct sbp_ocb *, int);
231 static void sbp_abort_all_ocbs (struct sbp_dev *, int);
232 static struct fw_xfer * sbp_write_cmd (struct sbp_dev *, int, int);
233 static struct sbp_ocb * sbp_get_ocb (struct sbp_dev *);
234 static struct sbp_ocb * sbp_enqueue_ocb (struct sbp_dev *, struct sbp_ocb *);
235 static struct sbp_ocb * sbp_dequeue_ocb (struct sbp_dev *, struct sbp_status *);
236 static void sbp_cam_detach_sdev(struct sbp_dev *);
237 static void sbp_free_sdev(struct sbp_dev *);
238 static void sbp_cam_detach_target (struct sbp_target *);
239 static void sbp_free_target (struct sbp_target *);
240 static void sbp_mgm_timeout (void *arg);
241 static void sbp_timeout (void *arg);
242 static void sbp_mgm_orb (struct sbp_dev *, int, struct sbp_ocb *);
244 MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire");
246 /* cam related functions */
247 static void sbp_action(struct cam_sim *sim, union ccb *ccb);
248 static void sbp_poll(struct cam_sim *sim);
249 static void sbp_cam_scan_lun(struct cam_periph *, union ccb *);
250 static void sbp_cam_scan_target(void *arg);
252 static char *orb_status0[] = {
253 /* 0 */ "No additional information to report",
254 /* 1 */ "Request type not supported",
255 /* 2 */ "Speed not supported",
256 /* 3 */ "Page size not supported",
257 /* 4 */ "Access denied",
258 /* 5 */ "Logical unit not supported",
259 /* 6 */ "Maximum payload too small",
260 /* 7 */ "Reserved for future standardization",
261 /* 8 */ "Resources unavailable",
262 /* 9 */ "Function rejected",
263 /* A */ "Login ID not recognized",
264 /* B */ "Dummy ORB completed",
265 /* C */ "Request aborted",
266 /* FF */ "Unspecified error"
267 #define MAX_ORB_STATUS0 0xd
270 static char *orb_status1_object[] = {
271 /* 0 */ "Operation request block (ORB)",
272 /* 1 */ "Data buffer",
273 /* 2 */ "Page table",
274 /* 3 */ "Unable to specify"
277 static char *orb_status1_serial_bus_error[] = {
278 /* 0 */ "Missing acknowledge",
279 /* 1 */ "Reserved; not to be used",
280 /* 2 */ "Time-out error",
281 /* 3 */ "Reserved; not to be used",
282 /* 4 */ "Busy retry limit exceeded(X)",
283 /* 5 */ "Busy retry limit exceeded(A)",
284 /* 6 */ "Busy retry limit exceeded(B)",
285 /* 7 */ "Reserved for future standardization",
286 /* 8 */ "Reserved for future standardization",
287 /* 9 */ "Reserved for future standardization",
288 /* A */ "Reserved for future standardization",
289 /* B */ "Tardy retry limit exceeded",
290 /* C */ "Conflict error",
291 /* D */ "Data error",
292 /* E */ "Type error",
293 /* F */ "Address error"
300 sbp_probe(device_t dev)
305 kprintf("sbp_probe\n");
308 pa = device_get_parent(dev);
309 if(device_get_unit(dev) != device_get_unit(pa)){
313 device_set_desc(dev, "SBP-2/SCSI over FireWire");
321 sbp_show_sdev_info(struct sbp_dev *sdev, int new)
323 struct fw_device *fwdev;
326 device_get_nameunit(sdev->target->sbp->fd.dev),
327 sdev->target->target_id,
333 fwdev = sdev->target->fwdev;
334 kprintf("ordered:%d type:%d EUI:%08x%08x node:%d "
335 "speed:%d maxrec:%d",
336 (sdev->type & 0x40) >> 6,
348 sbp_show_sdev_info(sdev, 2);
349 kprintf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision);
357 /* Bus Target EUI64 */
359 {0, 2, {0x00018ea0, 0x01fd0154}}, /* Logitec HDD */
360 {0, 0, {0x00018ea6, 0x00100682}}, /* Logitec DVD */
361 {0, 1, {0x00d03200, 0xa412006a}}, /* Yano HDD */
367 sbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev)
369 int bus, i, target=-1;
370 char w[SBP_NUM_TARGETS];
373 bus = device_get_unit(sbp->fd.dev);
375 /* XXX wired-down configuration should be gotten from
376 tunable or device hint */
377 for (i = 0; wired[i].bus >= 0; i ++) {
378 if (wired[i].bus == bus) {
379 w[wired[i].target] = 1;
380 if (wired[i].eui.hi == fwdev->eui.hi &&
381 wired[i].eui.lo == fwdev->eui.lo)
382 target = wired[i].target;
386 if(target < SBP_NUM_TARGETS &&
387 sbp->targets[target].fwdev == NULL)
389 device_printf(sbp->fd.dev,
390 "target %d is not free for %08x:%08x\n",
391 target, fwdev->eui.hi, fwdev->eui.lo);
394 /* non-wired target */
395 for (i = 0; i < SBP_NUM_TARGETS; i ++)
396 if (sbp->targets[i].fwdev == NULL && w[i] == 0) {
405 sbp_alloc_lun(struct sbp_target *target)
407 struct crom_context cc;
409 struct sbp_dev *sdev, **newluns;
410 struct sbp_softc *sbp;
414 crom_init_context(&cc, target->fwdev->csrrom);
415 /* XXX shoud parse appropriate unit directories only */
417 while (cc.depth >= 0) {
418 reg = crom_search_key(&cc, CROM_LUN);
421 lun = reg->val & 0xffff;
423 kprintf("target %d lun %d found\n", target->target_id, lun);
430 kprintf("%s:%d no LUN found\n",
431 device_get_nameunit(target->sbp->fd.dev),
435 if (maxlun >= SBP_NUM_LUNS)
436 maxlun = SBP_NUM_LUNS;
438 /* Invalidiate stale devices */
439 for (lun = 0; lun < target->num_lun; lun ++) {
440 sdev = target->luns[lun];
443 sdev->flags &= ~VALID_LUN;
446 sbp_cam_detach_sdev(sdev);
452 if (maxlun != target->num_lun) {
454 * note: krealloc() does not support M_ZERO. We must zero
455 * the extended region manually.
457 newluns = krealloc(target->luns,
458 sizeof(struct sbp_dev *) * maxlun,
461 if (maxlun > target->num_lun) {
462 bzero(&newluns[target->num_lun],
463 sizeof(struct sbp_dev *) *
464 (maxlun - target->num_lun));
466 target->luns = newluns;
467 target->num_lun = maxlun;
470 crom_init_context(&cc, target->fwdev->csrrom);
471 while (cc.depth >= 0) {
474 reg = crom_search_key(&cc, CROM_LUN);
477 lun = reg->val & 0xffff;
478 if (lun >= SBP_NUM_LUNS) {
479 kprintf("too large lun %d\n", lun);
483 sdev = target->luns[lun];
485 sdev = kmalloc(sizeof(struct sbp_dev),
486 M_SBP, M_WAITOK | M_ZERO);
487 target->luns[lun] = sdev;
489 sdev->target = target;
490 STAILQ_INIT(&sdev->ocbs);
491 CALLOUT_INIT(&sdev->login_callout);
492 sdev->status = SBP_DEV_RESET;
495 sdev->flags |= VALID_LUN;
496 sdev->type = (reg->val & 0xff0000) >> 16;
501 fwdma_malloc(sbp->fd.fc,
502 /* alignment */ sizeof(u_int32_t),
503 SBP_DMA_SIZE, &sdev->dma, BUS_DMA_NOWAIT);
504 if (sdev->dma.v_addr == NULL) {
505 kprintf("%s: dma space allocation failed\n",
508 target->luns[lun] = NULL;
511 sdev->login = (struct sbp_login_res *) sdev->dma.v_addr;
512 sdev->ocb = (struct sbp_ocb *)
513 ((char *)sdev->dma.v_addr + SBP_LOGIN_SIZE);
514 bzero((char *)sdev->ocb,
515 sizeof (struct sbp_ocb) * SBP_QUEUE_LEN);
517 STAILQ_INIT(&sdev->free_ocbs);
518 for (i = 0; i < SBP_QUEUE_LEN; i++) {
521 ocb->bus_addr = sdev->dma.bus_addr
523 + sizeof(struct sbp_ocb) * i
524 + offsetof(struct sbp_ocb, orb[0]);
525 if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
526 kprintf("sbp_attach: cannot create dmamap\n");
530 sbp_free_ocb(sdev, ocb);
536 for (lun = 0; lun < target->num_lun; lun ++) {
537 sdev = target->luns[lun];
538 if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) {
539 sbp_cam_detach_sdev(sdev);
541 target->luns[lun] = NULL;
546 static struct sbp_target *
547 sbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
550 struct sbp_target *target;
551 struct crom_context cc;
555 kprintf("sbp_alloc_target\n");
557 i = sbp_new_target(sbp, fwdev);
559 device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n");
563 target = &sbp->targets[i];
565 target->fwdev = fwdev;
566 target->target_id = i;
567 /* XXX we may want to reload mgm port after each bus reset */
568 /* XXX there might be multiple management agents */
569 crom_init_context(&cc, target->fwdev->csrrom);
570 reg = crom_search_key(&cc, CROM_MGM);
571 if (reg == NULL || reg->val == 0) {
572 kprintf("NULL management address\n");
573 target->fwdev = NULL;
576 target->mgm_hi = 0xffff;
577 target->mgm_lo = 0xf0000000 | (reg->val << 2);
578 target->mgm_ocb_cur = NULL;
580 kprintf("target:%d mgm_port: %x\n", i, target->mgm_lo);
582 STAILQ_INIT(&target->xferlist);
584 STAILQ_INIT(&target->mgm_ocb_queue);
585 CALLOUT_INIT(&target->mgm_ocb_timeout);
586 CALLOUT_INIT(&target->scan_callout);
594 sbp_probe_lun(struct sbp_dev *sdev)
596 struct fw_device *fwdev;
597 struct crom_context c, *cc = &c;
600 bzero(sdev->vendor, sizeof(sdev->vendor));
601 bzero(sdev->product, sizeof(sdev->product));
603 fwdev = sdev->target->fwdev;
604 crom_init_context(cc, fwdev->csrrom);
605 /* get vendor string */
606 crom_search_key(cc, CSRKEY_VENDOR);
608 crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor));
609 /* skip to the unit directory for SBP-2 */
610 while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) {
611 if (reg->val == CSRVAL_T10SBP2)
615 /* get firmware revision */
616 reg = crom_search_key(cc, CSRKEY_FIRM_VER);
618 ksnprintf(sdev->revision, sizeof(sdev->revision),
620 /* get product string */
621 crom_search_key(cc, CSRKEY_MODEL);
623 crom_parse_text(cc, sdev->product, sizeof(sdev->product));
627 sbp_login_callout(void *arg)
629 struct sbp_dev *sdev = (struct sbp_dev *)arg;
630 sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
634 sbp_login(struct sbp_dev *sdev)
636 struct timeval delta;
641 timevalsub(&delta, &sdev->target->sbp->last_busreset);
642 t.tv_sec = login_delay / 1000;
643 t.tv_usec = (login_delay % 1000) * 1000;
644 timevalsub(&t, &delta);
645 if (t.tv_sec >= 0 && t.tv_usec > 0)
646 ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000;
648 kprintf("%s: sec = %ld usec = %ld ticks = %d\n", __func__,
649 t.tv_sec, t.tv_usec, ticks);
651 callout_reset(&sdev->login_callout, ticks,
652 sbp_login_callout, (void *)(sdev));
655 #define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
656 && crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
659 sbp_probe_target(void *arg)
661 struct sbp_target *target = (struct sbp_target *)arg;
662 struct sbp_softc *sbp;
663 struct sbp_dev *sdev;
664 struct firewire_comm *fc;
667 alive = SBP_FWDEV_ALIVE(target->fwdev);
669 kprintf("sbp_probe_target %d\n", target->target_id);
671 kprintf("not alive\n");
675 fc = target->sbp->fd.fc;
676 sbp_alloc_lun(target);
678 /* XXX callout_stop mgm_ocb and dequeue */
679 for (i=0; i < target->num_lun; i++) {
680 sdev = target->luns[i];
683 if (alive && (sdev->status != SBP_DEV_DEAD)) {
684 if (sdev->path != NULL) {
685 xpt_freeze_devq(sdev->path, 1);
690 sbp_show_sdev_info(sdev,
691 (sdev->status == SBP_DEV_RESET));
694 sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
695 switch (sdev->status) {
697 /* new or revived target */
701 case SBP_DEV_TOATTACH:
703 case SBP_DEV_ATTACHED:
706 sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
710 switch (sdev->status) {
711 case SBP_DEV_ATTACHED:
713 /* the device has gone */
714 sbp_show_sdev_info(sdev, 2);
715 kprintf("lost target\n");
719 xpt_freeze_devq(sdev->path, 1);
722 sdev->status = SBP_DEV_RETRY;
723 sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
725 sbp_cam_detach_target(sdev->target);
726 sdev->status = SBP_DEV_RESET;
729 case SBP_DEV_TOATTACH:
730 sdev->status = SBP_DEV_RESET;
742 sbp_post_busreset(void *arg)
744 struct sbp_softc *sbp;
746 sbp = (struct sbp_softc *)arg;
748 kprintf("sbp_post_busreset\n");
750 if ((sbp->sim->flags & SIMQ_FREEZED) == 0) {
751 xpt_freeze_simq(sbp->sim, /*count*/1);
752 sbp->sim->flags |= SIMQ_FREEZED;
754 microtime(&sbp->last_busreset);
758 sbp_post_explore(void *arg)
760 struct sbp_softc *sbp = (struct sbp_softc *)arg;
761 struct sbp_target *target;
762 struct fw_device *fwdev;
766 kprintf("sbp_post_explore\n");
775 * XXX don't let CAM the bus rest.
776 * CAM tries to do something with freezed (DEV_RETRY) devices.
778 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
781 /* Gabage Collection */
782 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
783 target = &sbp->targets[i];
784 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
785 if (target->fwdev == NULL || target->fwdev == fwdev)
788 /* device has removed in lower driver */
789 sbp_cam_detach_target(target);
790 sbp_free_target(target);
793 /* traverse device list */
794 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
796 kprintf("sbp_post_explore: EUI:%08x%08x ",
797 fwdev->eui.hi, fwdev->eui.lo);
798 if (fwdev->status != FWDEVATTACHED)
799 kprintf("not attached, state=%d.\n", fwdev->status);
801 kprintf("attached\n");
803 alive = SBP_FWDEV_ALIVE(fwdev);
804 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
805 target = &sbp->targets[i];
806 if(target->fwdev == fwdev ) {
811 if(i == SBP_NUM_TARGETS){
814 target = sbp_alloc_target(sbp, fwdev);
821 sbp_probe_target((void *)target);
822 if (target->num_lun == 0)
823 sbp_free_target(target);
825 xpt_release_simq(sbp->sim, /*run queue*/TRUE);
826 sbp->sim->flags &= ~SIMQ_FREEZED;
831 sbp_loginres_callback(struct fw_xfer *xfer){
832 struct sbp_dev *sdev;
833 sdev = (struct sbp_dev *)xfer->sc;
835 sbp_show_sdev_info(sdev, 2);
836 kprintf("sbp_loginres_callback\n");
840 STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link);
847 sbp_xfer_free(struct fw_xfer *xfer)
849 struct sbp_dev *sdev;
851 sdev = (struct sbp_dev *)xfer->sc;
852 fw_xfer_unload(xfer);
854 STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
859 sbp_reset_start_callback(struct fw_xfer *xfer)
861 struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
862 struct sbp_target *target = sdev->target;
865 if (xfer->resp != 0) {
866 sbp_show_sdev_info(sdev, 2);
867 kprintf("sbp_reset_start failed: resp=%d\n", xfer->resp);
870 for (i = 0; i < target->num_lun; i++) {
871 tsdev = target->luns[i];
872 if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN)
878 sbp_reset_start(struct sbp_dev *sdev)
880 struct fw_xfer *xfer;
884 sbp_show_sdev_info(sdev, 2);
885 kprintf("sbp_reset_start\n");
888 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
889 xfer->act.hand = sbp_reset_start_callback;
890 fp = &xfer->send.hdr;
891 fp->mode.wreqq.dest_hi = 0xffff;
892 fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
893 fp->mode.wreqq.data = htonl(0xf);
894 fw_asyreq(xfer->fc, -1, xfer);
898 sbp_mgm_callback(struct fw_xfer *xfer)
900 struct sbp_dev *sdev;
903 sdev = (struct sbp_dev *)xfer->sc;
906 sbp_show_sdev_info(sdev, 2);
907 kprintf("sbp_mgm_callback\n");
913 sbp_show_sdev_info(sdev, 2);
914 kprintf("management ORB failed(%d) ... RESET_START\n", resp);
915 sbp_reset_start(sdev);
921 static struct sbp_dev *
922 sbp_next_dev(struct sbp_target *target, int lun)
924 struct sbp_dev **sdevp;
927 for (i = lun, sdevp = &target->luns[lun]; i < target->num_lun;
929 if (*sdevp != NULL && (*sdevp)->status == SBP_DEV_PROBE)
936 sbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb)
938 struct sbp_target *target;
939 struct sbp_dev *sdev;
941 sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
942 target = sdev->target;
944 sbp_show_sdev_info(sdev, 2);
945 kprintf("sbp_cam_scan_lun\n");
947 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
948 sdev->status = SBP_DEV_ATTACHED;
950 sbp_show_sdev_info(sdev, 2);
951 kprintf("scan failed\n");
953 sdev = sbp_next_dev(target, sdev->lun_id + 1);
959 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
960 ccb->ccb_h.ccb_sdev_ptr = sdev;
962 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
967 sbp_cam_scan_target(void *arg)
969 struct sbp_target *target = (struct sbp_target *)arg;
970 struct sbp_dev *sdev;
973 sdev = sbp_next_dev(target, 0);
975 kprintf("sbp_cam_scan_target: nothing to do for target%d\n",
980 sbp_show_sdev_info(sdev, 2);
981 kprintf("sbp_cam_scan_target\n");
983 ccb = kmalloc(sizeof(union ccb), M_SBP, M_WAITOK | M_ZERO);
984 xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
985 ccb->ccb_h.func_code = XPT_SCAN_LUN;
986 ccb->ccb_h.cbfcnp = sbp_cam_scan_lun;
987 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
988 ccb->crcn.flags = CAM_FLAG_NONE;
989 ccb->ccb_h.ccb_sdev_ptr = sdev;
991 /* The scan is in progress now. */
993 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
998 sbp_scan_dev(struct sbp_dev *sdev)
1000 sdev->status = SBP_DEV_PROBE;
1001 callout_reset(&sdev->target->scan_callout, scan_delay * hz / 1000,
1002 sbp_cam_scan_target, (void *)sdev->target);
1006 sbp_do_attach(struct fw_xfer *xfer)
1008 struct sbp_dev *sdev;
1009 struct sbp_target *target;
1010 struct sbp_softc *sbp;
1012 sdev = (struct sbp_dev *)xfer->sc;
1013 target = sdev->target;
1016 sbp_show_sdev_info(sdev, 2);
1017 kprintf("sbp_do_attach\n");
1019 sbp_xfer_free(xfer);
1021 if (sdev->path == NULL)
1022 xpt_create_path(&sdev->path, xpt_periph,
1023 cam_sim_path(target->sbp->sim),
1024 target->target_id, sdev->lun_id);
1028 * Let CAM scan the bus if we are in the boot process.
1029 * XXX xpt_scan_bus cannot detect LUN larger than 0
1030 * if LUN 0 doesn't exists.
1033 sdev->status = SBP_DEV_ATTACHED;
1043 sbp_agent_reset_callback(struct fw_xfer *xfer)
1045 struct sbp_dev *sdev;
1047 sdev = (struct sbp_dev *)xfer->sc;
1049 sbp_show_sdev_info(sdev, 2);
1050 kprintf("%s\n", __func__);
1052 if (xfer->resp != 0) {
1053 sbp_show_sdev_info(sdev, 2);
1054 kprintf("%s: resp=%d\n", __func__, xfer->resp);
1057 sbp_xfer_free(xfer);
1059 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1065 sbp_agent_reset(struct sbp_dev *sdev)
1067 struct fw_xfer *xfer;
1071 sbp_show_sdev_info(sdev, 2);
1072 kprintf("sbp_agent_reset\n");
1074 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1077 if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE)
1078 xfer->act.hand = sbp_agent_reset_callback;
1080 xfer->act.hand = sbp_do_attach;
1081 fp = &xfer->send.hdr;
1082 fp->mode.wreqq.data = htonl(0xf);
1083 fw_asyreq(xfer->fc, -1, xfer);
1084 sbp_abort_all_ocbs(sdev, CAM_BDR_SENT);
1088 sbp_busy_timeout_callback(struct fw_xfer *xfer)
1090 struct sbp_dev *sdev;
1092 sdev = (struct sbp_dev *)xfer->sc;
1094 sbp_show_sdev_info(sdev, 2);
1095 kprintf("sbp_busy_timeout_callback\n");
1097 sbp_xfer_free(xfer);
1098 sbp_agent_reset(sdev);
1102 sbp_busy_timeout(struct sbp_dev *sdev)
1105 struct fw_xfer *xfer;
1107 sbp_show_sdev_info(sdev, 2);
1108 kprintf("sbp_busy_timeout\n");
1110 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1112 xfer->act.hand = sbp_busy_timeout_callback;
1113 fp = &xfer->send.hdr;
1114 fp->mode.wreqq.dest_hi = 0xffff;
1115 fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1116 fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1117 fw_asyreq(xfer->fc, -1, xfer);
1121 sbp_orb_pointer_callback(struct fw_xfer *xfer)
1123 struct sbp_dev *sdev;
1124 sdev = (struct sbp_dev *)xfer->sc;
1127 sbp_show_sdev_info(sdev, 2);
1128 kprintf("%s\n", __func__);
1130 if (xfer->resp != 0) {
1132 kprintf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1134 sbp_xfer_free(xfer);
1135 sdev->flags &= ~ORB_POINTER_ACTIVE;
1137 if ((sdev->flags & ORB_POINTER_NEED) != 0) {
1138 struct sbp_ocb *ocb;
1140 sdev->flags &= ~ORB_POINTER_NEED;
1141 ocb = STAILQ_FIRST(&sdev->ocbs);
1143 sbp_orb_pointer(sdev, ocb);
1149 sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1151 struct fw_xfer *xfer;
1154 sbp_show_sdev_info(sdev, 2);
1155 kprintf("%s: 0x%08x\n", __func__, (u_int32_t)ocb->bus_addr);
1158 if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) {
1160 kprintf("%s: orb pointer active\n", __func__);
1162 sdev->flags |= ORB_POINTER_NEED;
1166 sdev->flags |= ORB_POINTER_ACTIVE;
1167 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1170 xfer->act.hand = sbp_orb_pointer_callback;
1172 fp = &xfer->send.hdr;
1173 fp->mode.wreqb.len = 8;
1174 fp->mode.wreqb.extcode = 0;
1175 xfer->send.payload[0] =
1176 htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1177 xfer->send.payload[1] = htonl((u_int32_t)ocb->bus_addr);
1179 if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1180 sbp_xfer_free(xfer);
1181 ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1188 sbp_cmd_callback(struct fw_xfer *xfer)
1191 struct sbp_dev *sdev;
1192 sdev = (struct sbp_dev *)xfer->sc;
1193 sbp_show_sdev_info(sdev, 2);
1194 kprintf("sbp_cmd_callback\n");
1196 if (xfer->resp != 0) {
1198 kprintf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1200 sbp_xfer_free(xfer);
1205 sbp_doorbell(struct sbp_dev *sdev)
1207 struct fw_xfer *xfer;
1210 sbp_show_sdev_info(sdev, 2);
1211 kprintf("sbp_doorbell\n");
1214 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1217 xfer->act.hand = sbp_cmd_callback;
1218 fp = (struct fw_pkt *)xfer->send.buf;
1219 fp->mode.wreqq.data = htonl(0xf);
1220 fw_asyreq(xfer->fc, -1, xfer);
1224 static struct fw_xfer *
1225 sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1227 struct fw_xfer *xfer;
1229 struct sbp_target *target;
1232 target = sdev->target;
1234 xfer = STAILQ_FIRST(&target->xferlist);
1236 if (target->n_xfer > 5 /* XXX */) {
1237 kprintf("sbp: no more xfer for this target\n");
1241 xfer = fw_xfer_alloc_buf(M_SBP, 8, 0);
1243 kprintf("sbp: fw_xfer_alloc_buf failed\n");
1249 kprintf("sbp: alloc %d xfer\n", target->n_xfer);
1252 STAILQ_REMOVE_HEAD(&target->xferlist, link);
1256 microtime(&xfer->tv);
1259 xfer->recv.pay_len = 0;
1260 xfer->send.spd = min(sdev->target->fwdev->speed, max_speed);
1261 xfer->fc = sdev->target->sbp->fd.fc;
1262 xfer->retry_req = fw_asybusy;
1265 if (tcode == FWTCODE_WREQB)
1266 xfer->send.pay_len = 8;
1268 xfer->send.pay_len = 0;
1270 xfer->sc = (caddr_t)sdev;
1271 fp = &xfer->send.hdr;
1272 fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
1273 fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
1274 fp->mode.wreqq.tlrt = 0;
1275 fp->mode.wreqq.tcode = tcode;
1276 fp->mode.wreqq.pri = 0;
1277 fp->mode.wreqq.dst = FWLOCALBUS | sdev->target->fwdev->dst;
1284 sbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1286 struct fw_xfer *xfer;
1288 struct sbp_ocb *ocb;
1289 struct sbp_target *target;
1292 target = sdev->target;
1293 nid = target->sbp->fd.fc->nodeid | FWLOCALBUS;
1296 if (func == ORB_FUN_RUNQUEUE) {
1297 ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1298 if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1302 STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1305 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
1310 ocb->flags = OCB_ACT_MGM;
1313 bzero((void *)ocb->orb, sizeof(ocb->orb));
1314 ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1315 ocb->orb[7] = htonl(SBP_DEV2ADDR(target->target_id, sdev->lun_id));
1318 sbp_show_sdev_info(sdev, 2);
1319 kprintf("%s\n", orb_fun_name[(func>>16)&0xf]);
1323 ocb->orb[0] = ocb->orb[1] = 0; /* password */
1324 ocb->orb[2] = htonl(nid << 16);
1325 ocb->orb[3] = htonl(sdev->dma.bus_addr);
1326 ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id);
1328 ocb->orb[4] |= htonl(ORB_EXV);
1329 ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
1330 fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD);
1333 ocb->orb[0] = htonl((0 << 16) | 0);
1334 ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
1341 ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
1345 if (target->mgm_ocb_cur != NULL) {
1346 /* there is a standing ORB */
1347 STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1352 target->mgm_ocb_cur = ocb;
1355 callout_reset(&target->mgm_ocb_timeout, 5*hz,
1356 sbp_mgm_timeout, (caddr_t)ocb);
1357 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1361 xfer->act.hand = sbp_mgm_callback;
1363 fp = &xfer->send.hdr;
1364 fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
1365 fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
1366 fp->mode.wreqb.len = 8;
1367 fp->mode.wreqb.extcode = 0;
1368 xfer->send.payload[0] = htonl(nid << 16);
1369 xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff);
1371 sbp_show_sdev_info(sdev, 2);
1372 kprintf("mgm orb: %08x\n", (u_int32_t)ocb->bus_addr);
1375 fw_asyreq(xfer->fc, -1, xfer);
1379 sbp_print_scsi_cmd(struct sbp_ocb *ocb)
1381 struct ccb_scsiio *csio;
1383 csio = &ocb->ccb->csio;
1384 kprintf("%s:%d:%d XPT_SCSI_IO: "
1385 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1387 "%db cmd/%db data/%db sense\n",
1388 device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1389 ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun,
1390 csio->cdb_io.cdb_bytes[0],
1391 csio->cdb_io.cdb_bytes[1],
1392 csio->cdb_io.cdb_bytes[2],
1393 csio->cdb_io.cdb_bytes[3],
1394 csio->cdb_io.cdb_bytes[4],
1395 csio->cdb_io.cdb_bytes[5],
1396 csio->cdb_io.cdb_bytes[6],
1397 csio->cdb_io.cdb_bytes[7],
1398 csio->cdb_io.cdb_bytes[8],
1399 csio->cdb_io.cdb_bytes[9],
1400 ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1401 csio->cdb_len, csio->dxfer_len,
1406 sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1408 struct sbp_cmd_status *sbp_cmd_status;
1409 struct scsi_sense_data *sense;
1411 sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1412 sense = &ocb->ccb->csio.sense_data;
1415 sbp_print_scsi_cmd(ocb);
1416 /* XXX need decode status */
1417 sbp_show_sdev_info(ocb->sdev, 2);
1418 kprintf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1419 sbp_cmd_status->status,
1420 sbp_cmd_status->sfmt,
1421 sbp_cmd_status->valid,
1422 sbp_cmd_status->s_key,
1423 sbp_cmd_status->s_code,
1424 sbp_cmd_status->s_qlfr,
1429 switch (sbp_cmd_status->status) {
1430 case SCSI_STATUS_CHECK_COND:
1431 case SCSI_STATUS_BUSY:
1432 case SCSI_STATUS_CMD_TERMINATED:
1433 if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1434 sense->error_code = SSD_CURRENT_ERROR;
1436 sense->error_code = SSD_DEFERRED_ERROR;
1438 if(sbp_cmd_status->valid)
1439 sense->error_code |= SSD_ERRCODE_VALID;
1440 sense->flags = sbp_cmd_status->s_key;
1441 if(sbp_cmd_status->mark)
1442 sense->flags |= SSD_FILEMARK;
1443 if(sbp_cmd_status->eom)
1444 sense->flags |= SSD_EOM;
1445 if(sbp_cmd_status->ill_len)
1446 sense->flags |= SSD_ILI;
1448 bcopy(&sbp_cmd_status->info, &sense->info[0], 4);
1450 if (sbp_status->len <= 1)
1451 /* XXX not scsi status. shouldn't be happened */
1452 sense->extra_len = 0;
1453 else if (sbp_status->len <= 4)
1454 /* add_sense_code(_qual), info, cmd_spec_info */
1455 sense->extra_len = 6;
1457 /* fru, sense_key_spec */
1458 sense->extra_len = 10;
1460 bcopy(&sbp_cmd_status->cdb, &sense->cmd_spec_info[0], 4);
1462 sense->add_sense_code = sbp_cmd_status->s_code;
1463 sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1464 sense->fru = sbp_cmd_status->fru;
1466 bcopy(&sbp_cmd_status->s_keydep[0],
1467 &sense->sense_key_spec[0], 3);
1469 ocb->ccb->csio.scsi_status = sbp_cmd_status->status;
1470 ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1471 | CAM_AUTOSNS_VALID;
1476 for( j = 0 ; j < 32 ; j+=8){
1477 kprintf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1478 tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1479 tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1486 sbp_show_sdev_info(ocb->sdev, 2);
1487 kprintf("sbp_scsi_status: unknown scsi status 0x%x\n",
1488 sbp_cmd_status->status);
1493 sbp_fix_inq_data(struct sbp_ocb *ocb)
1496 struct sbp_dev *sdev;
1497 struct scsi_inquiry_data *inq;
1502 if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1505 sbp_show_sdev_info(sdev, 2);
1506 kprintf("sbp_fix_inq_data\n");
1508 inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1509 switch (SID_TYPE(inq)) {
1513 * XXX Convert Direct Access device to RBC.
1514 * I've never seen FireWire DA devices which support READ_6.
1516 if (SID_TYPE(inq) == T_DIRECT)
1517 inq->device |= T_RBC; /* T_DIRECT == 0 */
1521 /* enable tagged queuing */
1523 inq->flags |= SID_CmdQue;
1525 inq->flags &= ~SID_CmdQue;
1527 * Override vendor/product/revision information.
1528 * Some devices sometimes return strange strings.
1531 bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1532 bcopy(sdev->product, inq->product, sizeof(inq->product));
1533 bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1540 sbp_recv1(struct fw_xfer *xfer)
1546 struct sbp_softc *sbp;
1547 struct sbp_dev *sdev;
1548 struct sbp_ocb *ocb;
1549 struct sbp_login_res *login_res = NULL;
1550 struct sbp_status *sbp_status;
1551 struct sbp_target *target;
1552 int orb_fun, status_valid0, status_valid, t, l, reset_agent = 0;
1556 ld = xfer->recv.buf;
1557 kprintf("sbp %x %d %d %08x %08x %08x %08x\n",
1558 xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1559 kprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1560 kprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1562 sbp = (struct sbp_softc *)xfer->sc;
1563 if (xfer->resp != 0){
1564 kprintf("sbp_recv: xfer->resp = %d\n", xfer->resp);
1567 if (xfer->recv.payload == NULL){
1568 kprintf("sbp_recv: xfer->recv.payload == NULL\n");
1571 rfp = &xfer->recv.hdr;
1572 if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1573 kprintf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1576 sbp_status = (struct sbp_status *)xfer->recv.payload;
1577 addr = rfp->mode.wreqb.dest_lo;
1579 kprintf("received address 0x%x\n", addr);
1581 t = SBP_ADDR2TRG(addr);
1582 if (t >= SBP_NUM_TARGETS) {
1583 device_printf(sbp->fd.dev,
1584 "sbp_recv1: invalid target %d\n", t);
1587 target = &sbp->targets[t];
1588 l = SBP_ADDR2LUN(addr);
1589 if (l >= target->num_lun || target->luns[l] == NULL) {
1590 device_printf(sbp->fd.dev,
1591 "sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1594 sdev = target->luns[l];
1597 switch (sbp_status->src) {
1600 /* check mgm_ocb_cur first */
1601 ocb = target->mgm_ocb_cur;
1603 if (OCB_MATCH(ocb, sbp_status)) {
1604 callout_stop(&target->mgm_ocb_timeout);
1605 target->mgm_ocb_cur = NULL;
1609 ocb = sbp_dequeue_ocb(sdev, sbp_status);
1611 sbp_show_sdev_info(sdev, 2);
1612 kprintf("No ocb(%x) on the queue\n",
1613 ntohl(sbp_status->orb_lo));
1618 sbp_show_sdev_info(sdev, 2);
1619 kprintf("unsolicit status received\n");
1622 sbp_show_sdev_info(sdev, 2);
1623 kprintf("unknown sbp_status->src\n");
1626 status_valid0 = (sbp_status->src < 2
1627 && sbp_status->resp == ORB_RES_CMPL
1628 && sbp_status->dead == 0);
1629 status_valid = (status_valid0 && sbp_status->status == 0);
1631 if (!status_valid0 || debug > 2){
1634 sbp_show_sdev_info(sdev, 2);
1635 kprintf("ORB status src:%x resp:%x dead:%x"
1636 " len:%x stat:%x orb:%x%08x\n",
1637 sbp_status->src, sbp_status->resp, sbp_status->dead,
1638 sbp_status->len, sbp_status->status,
1639 ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1641 sbp_show_sdev_info(sdev, 2);
1642 status = sbp_status->status;
1643 switch(sbp_status->resp) {
1645 if (status > MAX_ORB_STATUS0)
1646 kprintf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1648 kprintf("%s\n", orb_status0[status]);
1651 kprintf("Obj: %s, Error: %s\n",
1652 orb_status1_object[(status>>6) & 3],
1653 orb_status1_serial_bus_error[status & 0xf]);
1656 kprintf("Illegal request\n");
1659 kprintf("Vendor dependent\n");
1662 kprintf("unknown respose code %d\n", sbp_status->resp);
1666 /* we have to reset the fetch agent if it's dead */
1667 if (sbp_status->dead) {
1669 xpt_freeze_devq(sdev->path, 1);
1678 switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1684 switch(ocb->flags) {
1686 orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1690 fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD);
1691 login_res = sdev->login;
1692 login_res->len = ntohs(login_res->len);
1693 login_res->id = ntohs(login_res->id);
1694 login_res->cmd_hi = ntohs(login_res->cmd_hi);
1695 login_res->cmd_lo = ntohl(login_res->cmd_lo);
1698 sbp_show_sdev_info(sdev, 2);
1699 kprintf("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));
1701 sbp_busy_timeout(sdev);
1703 /* forgot logout? */
1704 sbp_show_sdev_info(sdev, 2);
1705 kprintf("login failed\n");
1706 sdev->status = SBP_DEV_RESET;
1710 login_res = sdev->login;
1713 sbp_show_sdev_info(sdev, 2);
1714 kprintf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo);
1717 if (sdev->status == SBP_DEV_ATTACHED)
1720 sbp_agent_reset(sdev);
1722 sdev->status = SBP_DEV_ATTACHED;
1723 sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL);
1726 /* reconnection hold time exceed? */
1728 sbp_show_sdev_info(sdev, 2);
1729 kprintf("reconnect failed\n");
1735 sdev->status = SBP_DEV_RESET;
1738 sbp_busy_timeout(sdev);
1743 sbp_agent_reset(sdev);
1746 sbp_show_sdev_info(sdev, 2);
1747 kprintf("unknown function %d\n", orb_fun);
1750 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
1754 if(ocb->ccb != NULL){
1758 ld = ocb->ccb->csio.data_ptr;
1759 if(ld != NULL && ocb->ccb->csio.dxfer_len != 0)
1760 kprintf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]);
1762 kprintf("ptr NULL\n");
1763 kprintf("len %d\n", sbp_status->len);
1766 if(sbp_status->len > 1){
1767 sbp_scsi_status(sbp_status, ocb);
1769 if(sbp_status->resp != ORB_RES_CMPL){
1770 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1772 ccb->ccb_h.status = CAM_REQ_CMP;
1775 /* fix up inq data */
1776 if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1777 sbp_fix_inq_data(ocb);
1786 sbp_free_ocb(sdev, ocb);
1789 sbp_agent_reset(sdev);
1792 xfer->recv.pay_len = SBP_RECV_LEN;
1793 /* The received packet is usually small enough to be stored within
1794 * the buffer. In that case, the controller return ack_complete and
1795 * no respose is necessary.
1797 * XXX fwohci.c and firewire.c should inform event_code such as
1798 * ack_complete or ack_pending to upper driver.
1802 sfp = (struct fw_pkt *)xfer->send.buf;
1803 sfp->mode.wres.dst = rfp->mode.wreqb.src;
1804 xfer->dst = sfp->mode.wres.dst;
1805 xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1806 xfer->act.hand = sbp_loginres_callback;
1807 xfer->retry_req = fw_asybusy;
1809 sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1810 sfp->mode.wres.tcode = FWTCODE_WRES;
1811 sfp->mode.wres.rtcode = 0;
1812 sfp->mode.wres.pri = 0;
1814 fw_asyreq(xfer->fc, -1, xfer);
1817 STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1825 sbp_recv(struct fw_xfer *xfer)
1835 sbp_attach(device_t dev)
1837 struct sbp_softc *sbp;
1838 struct cam_devq *devq;
1839 struct fw_xfer *xfer;
1843 kprintf("sbp_attach (cold=%d)\n", cold);
1850 sbp = ((struct sbp_softc *)device_get_softc(dev));
1851 bzero(sbp, sizeof(struct sbp_softc));
1853 sbp->fd.fc = device_get_ivars(dev);
1856 max_speed = sbp->fd.fc->speed;
1858 error = bus_dma_tag_create(/*parent*/sbp->fd.fc->dmat,
1859 /* XXX shoud be 4 for sane backend? */
1862 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1863 /*highaddr*/BUS_SPACE_MAXADDR,
1864 /*filter*/NULL, /*filterarg*/NULL,
1865 /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1866 /*maxsegsz*/SBP_SEG_MAX,
1867 /*flags*/BUS_DMA_ALLOCNOW,
1868 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
1869 /*lockfunc*/busdma_lock_mutex,
1874 kprintf("sbp_attach: Could not allocate DMA tag "
1875 "- error %d\n", error);
1879 devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1883 for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1884 sbp->targets[i].fwdev = NULL;
1885 sbp->targets[i].luns = NULL;
1888 sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1889 device_get_unit(dev),
1892 /*tagged*/ SBP_QUEUE_LEN - 1,
1894 cam_simq_release(devq);
1895 if (sbp->sim == NULL)
1898 if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS)
1901 if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim),
1902 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1903 xpt_bus_deregister(cam_sim_path(sbp->sim));
1907 /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1908 sbp->fwb.start = ((u_int64_t)SBP_BIND_HI << 32) | SBP_DEV2ADDR(0, 0);
1909 sbp->fwb.end = sbp->fwb.start + 0xffff;
1910 sbp->fwb.act_type = FWACT_XFER;
1911 /* pre-allocate xfer */
1912 STAILQ_INIT(&sbp->fwb.xferlist);
1913 for (i = 0; i < SBP_NUM_OCB/2; i ++) {
1914 xfer = fw_xfer_alloc_buf(M_SBP,
1916 /* recv */SBP_RECV_LEN);
1917 xfer->act.hand = sbp_recv;
1919 xfer->fc = sbp->fd.fc;
1921 xfer->sc = (caddr_t)sbp;
1922 STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1924 fw_bindadd(sbp->fd.fc, &sbp->fwb);
1926 sbp->fd.post_busreset = sbp_post_busreset;
1927 sbp->fd.post_explore = sbp_post_explore;
1929 if (sbp->fd.fc->status != -1) {
1931 sbp_post_busreset((void *)sbp);
1932 sbp_post_explore((void *)sbp);
1935 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
1939 cam_sim_free(sbp->sim);
1944 sbp_logout_all(struct sbp_softc *sbp)
1946 struct sbp_target *target;
1947 struct sbp_dev *sdev;
1951 kprintf("sbp_logout_all\n");
1953 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
1954 target = &sbp->targets[i];
1955 if (target->luns == NULL)
1957 for (j = 0; j < target->num_lun; j++) {
1958 sdev = target->luns[j];
1961 callout_stop(&sdev->login_callout);
1962 if (sdev->status >= SBP_DEV_TOATTACH &&
1963 sdev->status <= SBP_DEV_ATTACHED)
1964 sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
1972 sbp_shutdown(device_t dev)
1974 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1976 sbp_logout_all(sbp);
1981 sbp_free_sdev(struct sbp_dev *sdev)
1987 for (i = 0; i < SBP_QUEUE_LEN; i++)
1988 bus_dmamap_destroy(sdev->target->sbp->dmat,
1989 sdev->ocb[i].dmamap);
1990 fwdma_free(sdev->target->sbp->fd.fc, &sdev->dma);
1995 sbp_free_target(struct sbp_target *target)
1997 struct sbp_softc *sbp;
1998 struct fw_xfer *xfer, *next;
2001 if (target->luns == NULL)
2003 callout_stop(&target->mgm_ocb_timeout);
2005 for (i = 0; i < target->num_lun; i++)
2006 sbp_free_sdev(target->luns[i]);
2008 for (xfer = STAILQ_FIRST(&target->xferlist);
2009 xfer != NULL; xfer = next) {
2010 next = STAILQ_NEXT(xfer, link);
2011 fw_xfer_free_buf(xfer);
2013 STAILQ_INIT(&target->xferlist);
2014 kfree(target->luns, M_SBP);
2015 target->num_lun = 0;
2016 target->luns = NULL;
2017 target->fwdev = NULL;
2021 sbp_detach(device_t dev)
2023 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2024 struct firewire_comm *fc = sbp->fd.fc;
2025 struct fw_xfer *xfer, *next;
2029 kprintf("sbp_detach\n");
2032 for (i = 0; i < SBP_NUM_TARGETS; i ++)
2033 sbp_cam_detach_target(&sbp->targets[i]);
2034 xpt_async(AC_LOST_DEVICE, sbp->path, NULL);
2035 xpt_free_path(sbp->path);
2036 xpt_bus_deregister(cam_sim_path(sbp->sim));
2037 cam_sim_free(sbp->sim);
2039 sbp_logout_all(sbp);
2041 /* XXX wait for logout completion */
2042 tsleep(&i, FWPRI, "sbpdtc", hz/2);
2044 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++)
2045 sbp_free_target(&sbp->targets[i]);
2047 for (xfer = STAILQ_FIRST(&sbp->fwb.xferlist);
2048 xfer != NULL; xfer = next) {
2049 next = STAILQ_NEXT(xfer, link);
2050 fw_xfer_free_buf(xfer);
2052 STAILQ_INIT(&sbp->fwb.xferlist);
2053 fw_bindremove(fc, &sbp->fwb);
2055 bus_dma_tag_destroy(sbp->dmat);
2061 sbp_cam_detach_sdev(struct sbp_dev *sdev)
2065 if (sdev->status == SBP_DEV_DEAD)
2067 if (sdev->status == SBP_DEV_RESET)
2070 xpt_release_devq(sdev->path,
2071 sdev->freeze, TRUE);
2073 xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
2074 xpt_free_path(sdev->path);
2077 sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
2081 sbp_cam_detach_target(struct sbp_target *target)
2085 if (target->luns != NULL) {
2087 kprintf("sbp_detach_target %d\n", target->target_id);
2089 callout_stop(&target->scan_callout);
2090 for (i = 0; i < target->num_lun; i++)
2091 sbp_cam_detach_sdev(target->luns[i]);
2096 sbp_target_reset(struct sbp_dev *sdev, int method)
2099 struct sbp_target *target = sdev->target;
2100 struct sbp_dev *tsdev;
2102 for (i = 0; i < target->num_lun; i++) {
2103 tsdev = target->luns[i];
2106 if (tsdev->status == SBP_DEV_DEAD)
2108 if (tsdev->status == SBP_DEV_RESET)
2110 xpt_freeze_devq(tsdev->path, 1);
2112 sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT);
2114 tsdev->status = SBP_DEV_LOGIN;
2118 kprintf("target reset\n");
2119 sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2122 kprintf("reset start\n");
2123 sbp_reset_start(sdev);
2130 sbp_mgm_timeout(void *arg)
2132 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2133 struct sbp_dev *sdev = ocb->sdev;
2134 struct sbp_target *target = sdev->target;
2136 sbp_show_sdev_info(sdev, 2);
2137 kprintf("request timeout(mgm orb:0x%08x) ... ",
2138 (u_int32_t)ocb->bus_addr);
2139 target->mgm_ocb_cur = NULL;
2140 sbp_free_ocb(sdev, ocb);
2143 kprintf("run next request\n");
2144 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2147 kprintf("reset start\n");
2148 sbp_reset_start(sdev);
2153 sbp_timeout(void *arg)
2155 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2156 struct sbp_dev *sdev = ocb->sdev;
2158 sbp_show_sdev_info(sdev, 2);
2159 kprintf("request timeout(cmd orb:0x%08x) ... ",
2160 (u_int32_t)ocb->bus_addr);
2163 switch(sdev->timeout) {
2165 kprintf("agent reset\n");
2166 xpt_freeze_devq(sdev->path, 1);
2168 sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2169 sbp_agent_reset(sdev);
2173 sbp_target_reset(sdev, sdev->timeout - 1);
2178 sbp_cam_detach_target(target);
2179 if (target->luns != NULL)
2180 kfree(target->luns, M_SBP);
2181 target->num_lun = 0;
2182 target->luns = NULL;
2183 target->fwdev = NULL;
2189 sbp_action1(struct cam_sim *sim, union ccb *ccb)
2192 struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2193 struct sbp_target *target = NULL;
2194 struct sbp_dev *sdev = NULL;
2196 /* target:lun -> sdev mapping */
2198 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2199 && ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2200 target = &sbp->targets[ccb->ccb_h.target_id];
2201 if (target->fwdev != NULL
2202 && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2203 && ccb->ccb_h.target_lun < target->num_lun) {
2204 sdev = target->luns[ccb->ccb_h.target_lun];
2205 if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED &&
2206 sdev->status != SBP_DEV_PROBE)
2213 kprintf("invalid target %d lun %d\n",
2214 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2217 switch (ccb->ccb_h.func_code) {
2220 case XPT_GET_TRAN_SETTINGS:
2221 case XPT_SET_TRAN_SETTINGS:
2222 case XPT_CALC_GEOMETRY:
2225 kprintf("%s:%d:%d:func_code 0x%04x: "
2226 "Invalid target (target needed)\n",
2227 device_get_nameunit(sbp->fd.dev),
2228 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2229 ccb->ccb_h.func_code);
2232 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2239 /* The opcodes sometimes aimed at a target (sc is valid),
2240 * sometimes aimed at the SIM (sc is invalid and target is
2241 * CAM_TARGET_WILDCARD)
2244 ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2246 kprintf("%s:%d:%d func_code 0x%04x: "
2247 "Invalid target (no wildcard)\n",
2248 device_get_nameunit(sbp->fd.dev),
2249 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2250 ccb->ccb_h.func_code);
2252 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2258 /* XXX Hm, we should check the input parameters */
2262 switch (ccb->ccb_h.func_code) {
2265 struct ccb_scsiio *csio;
2266 struct sbp_ocb *ocb;
2273 kprintf("%s:%d:%d XPT_SCSI_IO: "
2274 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2276 "%db cmd/%db data/%db sense\n",
2277 device_get_nameunit(sbp->fd.dev),
2278 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2279 csio->cdb_io.cdb_bytes[0],
2280 csio->cdb_io.cdb_bytes[1],
2281 csio->cdb_io.cdb_bytes[2],
2282 csio->cdb_io.cdb_bytes[3],
2283 csio->cdb_io.cdb_bytes[4],
2284 csio->cdb_io.cdb_bytes[5],
2285 csio->cdb_io.cdb_bytes[6],
2286 csio->cdb_io.cdb_bytes[7],
2287 csio->cdb_io.cdb_bytes[8],
2288 csio->cdb_io.cdb_bytes[9],
2289 ccb->ccb_h.flags & CAM_DIR_MASK,
2290 csio->cdb_len, csio->dxfer_len,
2294 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2299 /* if we are in probe stage, pass only probe commands */
2300 if (sdev->status == SBP_DEV_PROBE) {
2302 name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2303 kprintf("probe stage, periph name: %s\n", name);
2304 if (strcmp(name, "probe") != 0) {
2305 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2311 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
2312 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2317 ocb->flags = OCB_ACT_CMD;
2320 ccb->ccb_h.ccb_sdev_ptr = sdev;
2321 ocb->orb[0] = htonl(1 << 31);
2323 ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2324 ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2325 speed = min(target->fwdev->speed, max_speed);
2326 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2327 | ORB_CMD_MAXP(speed + 7));
2328 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
2329 ocb->orb[4] |= htonl(ORB_CMD_IN);
2332 if (csio->ccb_h.flags & CAM_SCATTER_VALID)
2333 kprintf("sbp: CAM_SCATTER_VALID\n");
2334 if (csio->ccb_h.flags & CAM_DATA_PHYS)
2335 kprintf("sbp: CAM_DATA_PHYS\n");
2337 if (csio->ccb_h.flags & CAM_CDB_POINTER)
2338 cdb = (void *)csio->cdb_io.cdb_ptr;
2340 cdb = (void *)&csio->cdb_io.cdb_bytes;
2341 bcopy(cdb, (void *)&ocb->orb[5], csio->cdb_len);
2343 kprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2344 kprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2346 if (ccb->csio.dxfer_len > 0) {
2350 error = bus_dmamap_load(/*dma tag*/sbp->dmat,
2351 /*dma map*/ocb->dmamap,
2353 ccb->csio.dxfer_len,
2359 kprintf("sbp: bus_dmamap_load error %d\n", error);
2361 sbp_execute_ocb(ocb, NULL, 0, 0);
2364 case XPT_CALC_GEOMETRY:
2366 struct ccb_calc_geometry *ccg;
2367 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2369 u_int32_t secs_per_cylinder;
2374 if (ccg->block_size == 0) {
2375 kprintf("sbp_action1: block_size is 0.\n");
2376 ccb->ccb_h.status = CAM_REQ_INVALID;
2381 kprintf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: Volume size = %ju\n",
2382 device_get_nameunit(sbp->fd.dev),
2383 cam_sim_path(sbp->sim),
2384 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2385 (uintmax_t)ccg->volume_size);
2388 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2389 size_mb = ccg->volume_size
2390 / ((1024L * 1024L) / ccg->block_size);
2392 if (size_mb > 1024 && extended) {
2394 ccg->secs_per_track = 63;
2397 ccg->secs_per_track = 32;
2399 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2400 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2401 ccb->ccb_h.status = CAM_REQ_CMP;
2403 cam_calc_geometry(ccg, /*extended*/1);
2408 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
2412 kprintf("%s:%d:XPT_RESET_BUS: \n",
2413 device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2416 ccb->ccb_h.status = CAM_REQ_INVALID;
2420 case XPT_PATH_INQ: /* Path routing inquiry */
2422 struct ccb_pathinq *cpi = &ccb->cpi;
2425 kprintf("%s:%d:%d XPT_PATH_INQ:.\n",
2426 device_get_nameunit(sbp->fd.dev),
2427 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2429 cpi->version_num = 1; /* XXX??? */
2430 cpi->hba_inquiry = PI_TAG_ABLE;
2431 cpi->target_sprt = 0;
2432 cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE;
2433 cpi->hba_eng_cnt = 0;
2434 cpi->max_target = SBP_NUM_TARGETS - 1;
2435 cpi->max_lun = SBP_NUM_LUNS - 1;
2436 cpi->initiator_id = SBP_INITIATOR;
2437 cpi->bus_id = sim->bus_id;
2438 cpi->base_transfer_speed = 400 * 1000 / 8;
2439 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2440 strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2441 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2442 cpi->unit_number = sim->unit_number;
2443 cpi->transport = XPORT_SPI; /* XX should have a FireWire */
2444 cpi->transport_version = 2;
2445 cpi->protocol = PROTO_SCSI;
2446 cpi->protocol_version = SCSI_REV_2;
2448 cpi->ccb_h.status = CAM_REQ_CMP;
2452 case XPT_GET_TRAN_SETTINGS:
2454 struct ccb_trans_settings *cts = &ccb->cts;
2455 struct ccb_trans_settings_scsi *scsi =
2456 &cts->proto_specific.scsi;
2457 struct ccb_trans_settings_spi *spi =
2458 &cts->xport_specific.spi;
2460 cts->protocol = PROTO_SCSI;
2461 cts->protocol_version = SCSI_REV_2;
2462 cts->transport = XPORT_SPI; /* should have a FireWire */
2463 cts->transport_version = 2;
2464 spi->valid = CTS_SPI_VALID_DISC;
2465 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
2466 scsi->valid = CTS_SCSI_VALID_TQ;
2467 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2469 kprintf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2470 device_get_nameunit(sbp->fd.dev),
2471 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2473 cts->ccb_h.status = CAM_REQ_CMP;
2478 ccb->ccb_h.status = CAM_UA_ABORT;
2481 case XPT_SET_TRAN_SETTINGS:
2484 ccb->ccb_h.status = CAM_REQ_INVALID;
2492 sbp_action(struct cam_sim *sim, union ccb *ccb)
2495 sbp_action1(sim, ccb);
2500 sbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error)
2503 struct sbp_ocb *ocb;
2504 struct sbp_ocb *prev;
2505 bus_dma_segment_t *s;
2508 kprintf("sbp_execute_ocb: error=%d\n", error);
2510 ocb = (struct sbp_ocb *)arg;
2513 kprintf("sbp_execute_ocb: seg %d", seg);
2514 for (i = 0; i < seg; i++)
2515 kprintf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2516 (uintmax_t)segments[i].ds_len);
2521 /* direct pointer */
2523 if (s->ds_len > SBP_SEG_MAX)
2524 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2525 ocb->orb[3] = htonl(s->ds_addr);
2526 ocb->orb[4] |= htonl(s->ds_len);
2527 } else if(seg > 1) {
2529 for (i = 0; i < seg; i++) {
2532 /* XXX LSI Logic "< 16 byte" bug might be hit */
2534 kprintf("sbp_execute_ocb: warning, "
2535 "segment length(%zd) is less than 16."
2537 (size_t)s->ds_len, i+1, (intmax_t)seg);
2539 if (s->ds_len > SBP_SEG_MAX)
2540 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2541 ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2542 ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2544 ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2548 bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2549 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2550 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2551 prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2552 fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2553 if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) {
2554 ocb->sdev->flags &= ~ORB_LINK_DEAD;
2555 sbp_orb_pointer(ocb->sdev, ocb);
2560 sbp_poll(struct cam_sim *sim)
2562 struct sbp_softc *sbp;
2563 struct firewire_comm *fc;
2565 sbp = (struct sbp_softc *)sim->softc;
2568 fc->poll(fc, 0, -1);
2573 static struct sbp_ocb *
2574 sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2576 struct sbp_ocb *ocb;
2577 struct sbp_ocb *next;
2584 sbp_show_sdev_info(sdev, 2);
2585 kprintf("%s: 0x%08x src %d\n",
2586 __func__, ntohl(sbp_status->orb_lo), sbp_status->src);
2588 for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2589 next = STAILQ_NEXT(ocb, ocb);
2591 if (OCB_MATCH(ocb, sbp_status)) {
2593 STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2594 if (ocb->ccb != NULL)
2595 callout_stop(&ocb->ccb->ccb_h.timeout_ch);
2596 if (ntohl(ocb->orb[4]) & 0xffff) {
2597 bus_dmamap_sync(sdev->target->sbp->dmat,
2599 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2600 BUS_DMASYNC_POSTREAD :
2601 BUS_DMASYNC_POSTWRITE);
2602 bus_dmamap_unload(sdev->target->sbp->dmat,
2605 if (sbp_status->src == SRC_NO_NEXT) {
2607 sbp_orb_pointer(sdev, next);
2608 else if (order > 0) {
2610 * Unordered execution
2611 * We need to send pointer for
2614 sdev->flags |= ORB_LINK_DEAD;
2623 if (ocb && order > 0) {
2624 sbp_show_sdev_info(sdev, 2);
2625 kprintf("unordered execution order:%d\n", order);
2631 static struct sbp_ocb *
2632 sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2634 struct sbp_ocb *prev;
2639 sbp_show_sdev_info(sdev, 2);
2640 kprintf("%s: 0x%08jx\n", __func__, (uintmax_t)ocb->bus_addr);
2642 prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2643 STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2645 if (ocb->ccb != NULL)
2646 callout_reset(&ocb->ccb->ccb_h.timeout_ch,
2647 (ocb->ccb->ccb_h.timeout * hz) / 1000, sbp_timeout, ocb);
2651 kprintf("linking chain 0x%jx -> 0x%jx\n",
2652 (uintmax_t)prev->bus_addr, (uintmax_t)ocb->bus_addr);
2654 prev->orb[1] = htonl(ocb->bus_addr);
2662 static struct sbp_ocb *
2663 sbp_get_ocb(struct sbp_dev *sdev)
2665 struct sbp_ocb *ocb;
2668 ocb = STAILQ_FIRST(&sdev->free_ocbs);
2670 kprintf("ocb shortage!!!\n");
2673 STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2680 sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2684 STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2688 sbp_abort_ocb(struct sbp_ocb *ocb, int status)
2690 struct sbp_dev *sdev;
2694 sbp_show_sdev_info(sdev, 2);
2695 kprintf("sbp_abort_ocb 0x%jx\n", (uintmax_t)ocb->bus_addr);
2698 if (ocb->ccb != NULL)
2699 sbp_print_scsi_cmd(ocb);
2701 if (ntohl(ocb->orb[4]) & 0xffff) {
2702 bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
2703 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2704 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2705 bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
2707 if (ocb->ccb != NULL) {
2708 callout_stop(&ocb->ccb->ccb_h.timeout_ch);
2709 ocb->ccb->ccb_h.status = status;
2712 sbp_free_ocb(sdev, ocb);
2716 sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2718 struct sbp_ocb *ocb, *next;
2719 STAILQ_HEAD(, sbp_ocb) temp;
2723 STAILQ_CONCAT(&temp, &sdev->ocbs);
2724 for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2725 next = STAILQ_NEXT(ocb, ocb);
2726 sbp_abort_ocb(ocb, status);
2731 static devclass_t sbp_devclass;
2734 * Because sbp is a static device that always exists under any attached
2735 * firewire device, and not scanned by the firewire device, we need an
2736 * identify function to install the device. For our sanity we want
2737 * the sbp device to have the same unit number as the fireweire device.
2740 static device_method_t sbp_methods[] = {
2741 /* device interface */
2742 DEVMETHOD(device_identify, bus_generic_identify_sameunit),
2743 DEVMETHOD(device_probe, sbp_probe),
2744 DEVMETHOD(device_attach, sbp_attach),
2745 DEVMETHOD(device_detach, sbp_detach),
2746 DEVMETHOD(device_shutdown, sbp_shutdown),
2751 static driver_t sbp_driver = {
2754 sizeof(struct sbp_softc),
2757 DECLARE_DUMMY_MODULE(sbp);
2758 DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, NULL, NULL);
2759 MODULE_VERSION(sbp, 1);
2760 MODULE_DEPEND(sbp, firewire, 1, 1, 1);
2761 MODULE_DEPEND(sbp, cam, 1, 1, 1);