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.86 2007/03/16 01:23:36 simokawa Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/module.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 #include <sys/malloc.h>
46 #include <sys/thread2.h>
48 #include <bus/cam/cam.h>
49 #include <bus/cam/cam_ccb.h>
50 #include <bus/cam/cam_sim.h>
51 #include <bus/cam/cam_xpt_sim.h>
52 #include <bus/cam/cam_debug.h>
53 #include <bus/cam/cam_periph.h>
54 #include <bus/cam/scsi/scsi_all.h>
56 #include <bus/firewire/firewire.h>
57 #include <bus/firewire/firewirereg.h>
58 #include <bus/firewire/fwdma.h>
59 #include <bus/firewire/iec13213.h>
62 #define ccb_sdev_ptr spriv_ptr0
63 #define ccb_sbp_ptr spriv_ptr1
65 #define SBP_NUM_TARGETS 8 /* MAX 64 */
67 * Scan_bus doesn't work for more than 8 LUNs
68 * because of CAM_SCSI2_MAXLUN in cam_xpt.c
70 #define SBP_NUM_LUNS 64
71 #define SBP_MAXPHYS MIN(MAXPHYS, (512*1024) /* 512KB */)
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(SBP_MAXPHYS, SBP_SEG_MAX)
142 #define SBP_IND_MAX howmany(SBP_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_dev *sdev;
665 alive = SBP_FWDEV_ALIVE(target->fwdev);
667 kprintf("sbp_probe_target %d\n", target->target_id);
669 kprintf("not alive\n");
672 sbp_alloc_lun(target);
674 /* XXX callout_stop mgm_ocb and dequeue */
675 for (i=0; i < target->num_lun; i++) {
676 sdev = target->luns[i];
679 if (alive && (sdev->status != SBP_DEV_DEAD)) {
680 if (sdev->path != NULL) {
681 xpt_freeze_devq(sdev->path, 1);
686 sbp_show_sdev_info(sdev,
687 (sdev->status == SBP_DEV_RESET));
690 sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
691 switch (sdev->status) {
693 /* new or revived target */
697 case SBP_DEV_TOATTACH:
699 case SBP_DEV_ATTACHED:
702 sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
706 switch (sdev->status) {
707 case SBP_DEV_ATTACHED:
709 /* the device has gone */
710 sbp_show_sdev_info(sdev, 2);
711 kprintf("lost target\n");
715 xpt_freeze_devq(sdev->path, 1);
718 sdev->status = SBP_DEV_RETRY;
719 sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
721 sbp_cam_detach_target(sdev->target);
722 sdev->status = SBP_DEV_RESET;
725 case SBP_DEV_TOATTACH:
726 sdev->status = SBP_DEV_RESET;
738 sbp_post_busreset(void *arg)
740 struct sbp_softc *sbp;
742 sbp = (struct sbp_softc *)arg;
744 kprintf("sbp_post_busreset\n");
746 if ((sbp->sim->flags & SIMQ_FREEZED) == 0) {
747 xpt_freeze_simq(sbp->sim, /*count*/1);
748 sbp->sim->flags |= SIMQ_FREEZED;
750 microtime(&sbp->last_busreset);
754 sbp_post_explore(void *arg)
756 struct sbp_softc *sbp = (struct sbp_softc *)arg;
757 struct sbp_target *target;
758 struct fw_device *fwdev;
762 kprintf("sbp_post_explore\n");
771 * XXX don't let CAM the bus rest.
772 * CAM tries to do something with freezed (DEV_RETRY) devices.
774 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
777 /* Gabage Collection */
778 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
779 target = &sbp->targets[i];
780 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
781 if (target->fwdev == NULL || target->fwdev == fwdev)
784 /* device has removed in lower driver */
785 sbp_cam_detach_target(target);
786 sbp_free_target(target);
789 /* traverse device list */
790 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
792 kprintf("sbp_post_explore: EUI:%08x%08x ",
793 fwdev->eui.hi, fwdev->eui.lo);
794 if (fwdev->status != FWDEVATTACHED)
795 kprintf("not attached, state=%d.\n", fwdev->status);
797 kprintf("attached\n");
799 alive = SBP_FWDEV_ALIVE(fwdev);
800 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
801 target = &sbp->targets[i];
802 if(target->fwdev == fwdev ) {
807 if(i == SBP_NUM_TARGETS){
810 target = sbp_alloc_target(sbp, fwdev);
817 sbp_probe_target((void *)target);
818 if (target->num_lun == 0)
819 sbp_free_target(target);
821 xpt_release_simq(sbp->sim, /*run queue*/TRUE);
822 sbp->sim->flags &= ~SIMQ_FREEZED;
827 sbp_loginres_callback(struct fw_xfer *xfer){
828 struct sbp_dev *sdev;
829 sdev = (struct sbp_dev *)xfer->sc;
831 sbp_show_sdev_info(sdev, 2);
832 kprintf("sbp_loginres_callback\n");
836 STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link);
843 sbp_xfer_free(struct fw_xfer *xfer)
845 struct sbp_dev *sdev;
847 sdev = (struct sbp_dev *)xfer->sc;
848 fw_xfer_unload(xfer);
850 STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
855 sbp_reset_start_callback(struct fw_xfer *xfer)
857 struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
858 struct sbp_target *target = sdev->target;
861 if (xfer->resp != 0) {
862 sbp_show_sdev_info(sdev, 2);
863 kprintf("sbp_reset_start failed: resp=%d\n", xfer->resp);
866 for (i = 0; i < target->num_lun; i++) {
867 tsdev = target->luns[i];
868 if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN)
874 sbp_reset_start(struct sbp_dev *sdev)
876 struct fw_xfer *xfer;
880 sbp_show_sdev_info(sdev, 2);
881 kprintf("sbp_reset_start\n");
884 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
885 xfer->act.hand = sbp_reset_start_callback;
886 fp = &xfer->send.hdr;
887 fp->mode.wreqq.dest_hi = 0xffff;
888 fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
889 fp->mode.wreqq.data = htonl(0xf);
890 fw_asyreq(xfer->fc, -1, xfer);
894 sbp_mgm_callback(struct fw_xfer *xfer)
896 struct sbp_dev *sdev;
901 sdev = (struct sbp_dev *)xfer->sc;
904 sbp_show_sdev_info(sdev, 2);
905 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;
1011 sdev = (struct sbp_dev *)xfer->sc;
1012 target = sdev->target;
1014 sbp_show_sdev_info(sdev, 2);
1015 kprintf("sbp_do_attach\n");
1017 sbp_xfer_free(xfer);
1019 if (sdev->path == NULL)
1020 xpt_create_path(&sdev->path, xpt_periph,
1021 cam_sim_path(target->sbp->sim),
1022 target->target_id, sdev->lun_id);
1026 * Let CAM scan the bus if we are in the boot process.
1027 * XXX xpt_scan_bus cannot detect LUN larger than 0
1028 * if LUN 0 doesn't exists.
1031 sdev->status = SBP_DEV_ATTACHED;
1041 sbp_agent_reset_callback(struct fw_xfer *xfer)
1043 struct sbp_dev *sdev;
1045 sdev = (struct sbp_dev *)xfer->sc;
1047 sbp_show_sdev_info(sdev, 2);
1048 kprintf("%s\n", __func__);
1050 if (xfer->resp != 0) {
1051 sbp_show_sdev_info(sdev, 2);
1052 kprintf("%s: resp=%d\n", __func__, xfer->resp);
1055 sbp_xfer_free(xfer);
1057 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1063 sbp_agent_reset(struct sbp_dev *sdev)
1065 struct fw_xfer *xfer;
1069 sbp_show_sdev_info(sdev, 2);
1070 kprintf("sbp_agent_reset\n");
1072 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1075 if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE)
1076 xfer->act.hand = sbp_agent_reset_callback;
1078 xfer->act.hand = sbp_do_attach;
1079 fp = &xfer->send.hdr;
1080 fp->mode.wreqq.data = htonl(0xf);
1081 fw_asyreq(xfer->fc, -1, xfer);
1082 sbp_abort_all_ocbs(sdev, CAM_BDR_SENT);
1086 sbp_busy_timeout_callback(struct fw_xfer *xfer)
1088 struct sbp_dev *sdev;
1090 sdev = (struct sbp_dev *)xfer->sc;
1092 sbp_show_sdev_info(sdev, 2);
1093 kprintf("sbp_busy_timeout_callback\n");
1095 sbp_xfer_free(xfer);
1096 sbp_agent_reset(sdev);
1100 sbp_busy_timeout(struct sbp_dev *sdev)
1103 struct fw_xfer *xfer;
1105 sbp_show_sdev_info(sdev, 2);
1106 kprintf("sbp_busy_timeout\n");
1108 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1110 xfer->act.hand = sbp_busy_timeout_callback;
1111 fp = &xfer->send.hdr;
1112 fp->mode.wreqq.dest_hi = 0xffff;
1113 fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1114 fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1115 fw_asyreq(xfer->fc, -1, xfer);
1119 sbp_orb_pointer_callback(struct fw_xfer *xfer)
1121 struct sbp_dev *sdev;
1122 sdev = (struct sbp_dev *)xfer->sc;
1125 sbp_show_sdev_info(sdev, 2);
1126 kprintf("%s\n", __func__);
1128 if (xfer->resp != 0) {
1130 kprintf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1132 sbp_xfer_free(xfer);
1133 sdev->flags &= ~ORB_POINTER_ACTIVE;
1135 if ((sdev->flags & ORB_POINTER_NEED) != 0) {
1136 struct sbp_ocb *ocb;
1138 sdev->flags &= ~ORB_POINTER_NEED;
1139 ocb = STAILQ_FIRST(&sdev->ocbs);
1141 sbp_orb_pointer(sdev, ocb);
1147 sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1149 struct fw_xfer *xfer;
1152 sbp_show_sdev_info(sdev, 2);
1153 kprintf("%s: 0x%08x\n", __func__, (u_int32_t)ocb->bus_addr);
1156 if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) {
1158 kprintf("%s: orb pointer active\n", __func__);
1160 sdev->flags |= ORB_POINTER_NEED;
1164 sdev->flags |= ORB_POINTER_ACTIVE;
1165 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1168 xfer->act.hand = sbp_orb_pointer_callback;
1170 fp = &xfer->send.hdr;
1171 fp->mode.wreqb.len = 8;
1172 fp->mode.wreqb.extcode = 0;
1173 xfer->send.payload[0] =
1174 htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1175 xfer->send.payload[1] = htonl((u_int32_t)ocb->bus_addr);
1177 if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1178 sbp_xfer_free(xfer);
1179 ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1186 sbp_cmd_callback(struct fw_xfer *xfer)
1189 struct sbp_dev *sdev;
1190 sdev = (struct sbp_dev *)xfer->sc;
1191 sbp_show_sdev_info(sdev, 2);
1192 kprintf("sbp_cmd_callback\n");
1194 if (xfer->resp != 0) {
1196 kprintf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1198 sbp_xfer_free(xfer);
1203 sbp_doorbell(struct sbp_dev *sdev)
1205 struct fw_xfer *xfer;
1208 sbp_show_sdev_info(sdev, 2);
1209 kprintf("sbp_doorbell\n");
1212 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1215 xfer->act.hand = sbp_cmd_callback;
1216 fp = (struct fw_pkt *)xfer->send.buf;
1217 fp->mode.wreqq.data = htonl(0xf);
1218 fw_asyreq(xfer->fc, -1, xfer);
1222 static struct fw_xfer *
1223 sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1225 struct fw_xfer *xfer;
1227 struct sbp_target *target;
1230 target = sdev->target;
1232 xfer = STAILQ_FIRST(&target->xferlist);
1234 if (target->n_xfer > 5 /* XXX */) {
1235 kprintf("sbp: no more xfer for this target\n");
1239 xfer = fw_xfer_alloc_buf(M_SBP, 8, 0);
1241 kprintf("sbp: fw_xfer_alloc_buf failed\n");
1247 kprintf("sbp: alloc %d xfer\n", target->n_xfer);
1250 STAILQ_REMOVE_HEAD(&target->xferlist, link);
1254 microtime(&xfer->tv);
1257 xfer->recv.pay_len = 0;
1258 xfer->send.spd = min(sdev->target->fwdev->speed, max_speed);
1259 xfer->fc = sdev->target->sbp->fd.fc;
1260 xfer->retry_req = fw_asybusy;
1263 if (tcode == FWTCODE_WREQB)
1264 xfer->send.pay_len = 8;
1266 xfer->send.pay_len = 0;
1268 xfer->sc = (caddr_t)sdev;
1269 fp = &xfer->send.hdr;
1270 fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
1271 fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
1272 fp->mode.wreqq.tlrt = 0;
1273 fp->mode.wreqq.tcode = tcode;
1274 fp->mode.wreqq.pri = 0;
1275 fp->mode.wreqq.dst = FWLOCALBUS | sdev->target->fwdev->dst;
1282 sbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1284 struct fw_xfer *xfer;
1286 struct sbp_ocb *ocb;
1287 struct sbp_target *target;
1290 target = sdev->target;
1291 nid = target->sbp->fd.fc->nodeid | FWLOCALBUS;
1294 if (func == ORB_FUN_RUNQUEUE) {
1295 ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1296 if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1300 STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1303 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
1308 ocb->flags = OCB_ACT_MGM;
1311 bzero((void *)ocb->orb, sizeof(ocb->orb));
1312 ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1313 ocb->orb[7] = htonl(SBP_DEV2ADDR(target->target_id, sdev->lun_id));
1316 sbp_show_sdev_info(sdev, 2);
1317 kprintf("%s\n", orb_fun_name[(func>>16)&0xf]);
1321 ocb->orb[0] = ocb->orb[1] = 0; /* password */
1322 ocb->orb[2] = htonl(nid << 16);
1323 ocb->orb[3] = htonl(sdev->dma.bus_addr);
1324 ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id);
1326 ocb->orb[4] |= htonl(ORB_EXV);
1327 ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
1328 fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD);
1331 ocb->orb[0] = htonl((0 << 16) | 0);
1332 ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
1339 ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
1343 if (target->mgm_ocb_cur != NULL) {
1344 /* there is a standing ORB */
1345 STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1350 target->mgm_ocb_cur = ocb;
1353 callout_reset(&target->mgm_ocb_timeout, 5*hz,
1354 sbp_mgm_timeout, (caddr_t)ocb);
1355 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1359 xfer->act.hand = sbp_mgm_callback;
1361 fp = &xfer->send.hdr;
1362 fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
1363 fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
1364 fp->mode.wreqb.len = 8;
1365 fp->mode.wreqb.extcode = 0;
1366 xfer->send.payload[0] = htonl(nid << 16);
1367 xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff);
1369 sbp_show_sdev_info(sdev, 2);
1370 kprintf("mgm orb: %08x\n", (u_int32_t)ocb->bus_addr);
1373 fw_asyreq(xfer->fc, -1, xfer);
1377 sbp_print_scsi_cmd(struct sbp_ocb *ocb)
1379 struct ccb_scsiio *csio;
1381 csio = &ocb->ccb->csio;
1382 kprintf("%s:%d:%d XPT_SCSI_IO: "
1383 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1385 "%db cmd/%db data/%db sense\n",
1386 device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1387 ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun,
1388 csio->cdb_io.cdb_bytes[0],
1389 csio->cdb_io.cdb_bytes[1],
1390 csio->cdb_io.cdb_bytes[2],
1391 csio->cdb_io.cdb_bytes[3],
1392 csio->cdb_io.cdb_bytes[4],
1393 csio->cdb_io.cdb_bytes[5],
1394 csio->cdb_io.cdb_bytes[6],
1395 csio->cdb_io.cdb_bytes[7],
1396 csio->cdb_io.cdb_bytes[8],
1397 csio->cdb_io.cdb_bytes[9],
1398 ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1399 csio->cdb_len, csio->dxfer_len,
1404 sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1406 struct sbp_cmd_status *sbp_cmd_status;
1407 struct scsi_sense_data *sense;
1409 sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1410 sense = &ocb->ccb->csio.sense_data;
1413 sbp_print_scsi_cmd(ocb);
1414 /* XXX need decode status */
1415 sbp_show_sdev_info(ocb->sdev, 2);
1416 kprintf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1417 sbp_cmd_status->status,
1418 sbp_cmd_status->sfmt,
1419 sbp_cmd_status->valid,
1420 sbp_cmd_status->s_key,
1421 sbp_cmd_status->s_code,
1422 sbp_cmd_status->s_qlfr,
1427 switch (sbp_cmd_status->status) {
1428 case SCSI_STATUS_CHECK_COND:
1429 case SCSI_STATUS_BUSY:
1430 case SCSI_STATUS_CMD_TERMINATED:
1431 if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1432 sense->error_code = SSD_CURRENT_ERROR;
1434 sense->error_code = SSD_DEFERRED_ERROR;
1436 if(sbp_cmd_status->valid)
1437 sense->error_code |= SSD_ERRCODE_VALID;
1438 sense->flags = sbp_cmd_status->s_key;
1439 if(sbp_cmd_status->mark)
1440 sense->flags |= SSD_FILEMARK;
1441 if(sbp_cmd_status->eom)
1442 sense->flags |= SSD_EOM;
1443 if(sbp_cmd_status->ill_len)
1444 sense->flags |= SSD_ILI;
1446 bcopy(&sbp_cmd_status->info, &sense->info[0], 4);
1448 if (sbp_status->len <= 1)
1449 /* XXX not scsi status. shouldn't be happened */
1450 sense->extra_len = 0;
1451 else if (sbp_status->len <= 4)
1452 /* add_sense_code(_qual), info, cmd_spec_info */
1453 sense->extra_len = 6;
1455 /* fru, sense_key_spec */
1456 sense->extra_len = 10;
1458 bcopy(&sbp_cmd_status->cdb, &sense->cmd_spec_info[0], 4);
1460 sense->add_sense_code = sbp_cmd_status->s_code;
1461 sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1462 sense->fru = sbp_cmd_status->fru;
1464 bcopy(&sbp_cmd_status->s_keydep[0],
1465 &sense->sense_key_spec[0], 3);
1467 ocb->ccb->csio.scsi_status = sbp_cmd_status->status;
1468 ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1469 | CAM_AUTOSNS_VALID;
1474 for( j = 0 ; j < 32 ; j+=8){
1475 kprintf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1476 tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1477 tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1484 sbp_show_sdev_info(ocb->sdev, 2);
1485 kprintf("sbp_scsi_status: unknown scsi status 0x%x\n",
1486 sbp_cmd_status->status);
1491 sbp_fix_inq_data(struct sbp_ocb *ocb)
1494 struct sbp_dev *sdev;
1495 struct scsi_inquiry_data *inq;
1500 if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1503 sbp_show_sdev_info(sdev, 2);
1504 kprintf("sbp_fix_inq_data\n");
1506 inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1507 switch (SID_TYPE(inq)) {
1511 * XXX Convert Direct Access device to RBC.
1512 * I've never seen FireWire DA devices which support READ_6.
1514 if (SID_TYPE(inq) == T_DIRECT)
1515 inq->device |= T_RBC; /* T_DIRECT == 0 */
1519 /* enable tagged queuing */
1521 inq->flags |= SID_CmdQue;
1523 inq->flags &= ~SID_CmdQue;
1525 * Override vendor/product/revision information.
1526 * Some devices sometimes return strange strings.
1529 bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1530 bcopy(sdev->product, inq->product, sizeof(inq->product));
1531 bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1538 sbp_recv1(struct fw_xfer *xfer)
1544 struct sbp_softc *sbp;
1545 struct sbp_dev *sdev;
1546 struct sbp_ocb *ocb;
1547 struct sbp_login_res *login_res = NULL;
1548 struct sbp_status *sbp_status;
1549 struct sbp_target *target;
1550 int orb_fun, status_valid0, status_valid, t, l, reset_agent = 0;
1554 ld = xfer->recv.buf;
1555 kprintf("sbp %x %d %d %08x %08x %08x %08x\n",
1556 xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1557 kprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1558 kprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1560 sbp = (struct sbp_softc *)xfer->sc;
1561 if (xfer->resp != 0){
1562 kprintf("sbp_recv: xfer->resp = %d\n", xfer->resp);
1565 if (xfer->recv.payload == NULL){
1566 kprintf("sbp_recv: xfer->recv.payload == NULL\n");
1569 rfp = &xfer->recv.hdr;
1570 if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1571 kprintf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1574 sbp_status = (struct sbp_status *)xfer->recv.payload;
1575 addr = rfp->mode.wreqb.dest_lo;
1577 kprintf("received address 0x%x\n", addr);
1579 t = SBP_ADDR2TRG(addr);
1580 if (t >= SBP_NUM_TARGETS) {
1581 device_printf(sbp->fd.dev,
1582 "sbp_recv1: invalid target %d\n", t);
1585 target = &sbp->targets[t];
1586 l = SBP_ADDR2LUN(addr);
1587 if (l >= target->num_lun || target->luns[l] == NULL) {
1588 device_printf(sbp->fd.dev,
1589 "sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1592 sdev = target->luns[l];
1595 switch (sbp_status->src) {
1598 /* check mgm_ocb_cur first */
1599 ocb = target->mgm_ocb_cur;
1601 if (OCB_MATCH(ocb, sbp_status)) {
1602 callout_stop(&target->mgm_ocb_timeout);
1603 target->mgm_ocb_cur = NULL;
1607 ocb = sbp_dequeue_ocb(sdev, sbp_status);
1609 sbp_show_sdev_info(sdev, 2);
1610 kprintf("No ocb(%x) on the queue\n",
1611 ntohl(sbp_status->orb_lo));
1616 sbp_show_sdev_info(sdev, 2);
1617 kprintf("unsolicit status received\n");
1620 sbp_show_sdev_info(sdev, 2);
1621 kprintf("unknown sbp_status->src\n");
1624 status_valid0 = (sbp_status->src < 2
1625 && sbp_status->resp == ORB_RES_CMPL
1626 && sbp_status->dead == 0);
1627 status_valid = (status_valid0 && sbp_status->status == 0);
1629 if (!status_valid0 || debug > 2){
1632 sbp_show_sdev_info(sdev, 2);
1633 kprintf("ORB status src:%x resp:%x dead:%x"
1634 " len:%x stat:%x orb:%x%08x\n",
1635 sbp_status->src, sbp_status->resp, sbp_status->dead,
1636 sbp_status->len, sbp_status->status,
1637 ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1639 sbp_show_sdev_info(sdev, 2);
1640 status = sbp_status->status;
1641 switch(sbp_status->resp) {
1643 if (status > MAX_ORB_STATUS0)
1644 kprintf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1646 kprintf("%s\n", orb_status0[status]);
1649 kprintf("Obj: %s, Error: %s\n",
1650 orb_status1_object[(status>>6) & 3],
1651 orb_status1_serial_bus_error[status & 0xf]);
1654 kprintf("Illegal request\n");
1657 kprintf("Vendor dependent\n");
1660 kprintf("unknown respose code %d\n", sbp_status->resp);
1664 /* we have to reset the fetch agent if it's dead */
1665 if (sbp_status->dead) {
1667 xpt_freeze_devq(sdev->path, 1);
1676 switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1682 switch(ocb->flags) {
1684 orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1688 fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD);
1689 login_res = sdev->login;
1690 login_res->len = ntohs(login_res->len);
1691 login_res->id = ntohs(login_res->id);
1692 login_res->cmd_hi = ntohs(login_res->cmd_hi);
1693 login_res->cmd_lo = ntohl(login_res->cmd_lo);
1696 sbp_show_sdev_info(sdev, 2);
1697 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));
1699 sbp_busy_timeout(sdev);
1701 /* forgot logout? */
1702 sbp_show_sdev_info(sdev, 2);
1703 kprintf("login failed\n");
1704 sdev->status = SBP_DEV_RESET;
1708 login_res = sdev->login;
1711 sbp_show_sdev_info(sdev, 2);
1712 kprintf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo);
1715 if (sdev->status == SBP_DEV_ATTACHED)
1718 sbp_agent_reset(sdev);
1720 sdev->status = SBP_DEV_ATTACHED;
1721 sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL);
1724 /* reconnection hold time exceed? */
1726 sbp_show_sdev_info(sdev, 2);
1727 kprintf("reconnect failed\n");
1733 sdev->status = SBP_DEV_RESET;
1736 sbp_busy_timeout(sdev);
1741 sbp_agent_reset(sdev);
1744 sbp_show_sdev_info(sdev, 2);
1745 kprintf("unknown function %d\n", orb_fun);
1748 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
1752 if(ocb->ccb != NULL){
1756 ld = ocb->ccb->csio.data_ptr;
1757 if(ld != NULL && ocb->ccb->csio.dxfer_len != 0)
1758 kprintf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]);
1760 kprintf("ptr NULL\n");
1761 kprintf("len %d\n", sbp_status->len);
1764 if(sbp_status->len > 1){
1765 sbp_scsi_status(sbp_status, ocb);
1767 if(sbp_status->resp != ORB_RES_CMPL){
1768 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1770 ccb->ccb_h.status = CAM_REQ_CMP;
1773 /* fix up inq data */
1774 if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1775 sbp_fix_inq_data(ocb);
1784 sbp_free_ocb(sdev, ocb);
1787 sbp_agent_reset(sdev);
1790 xfer->recv.pay_len = SBP_RECV_LEN;
1791 /* The received packet is usually small enough to be stored within
1792 * the buffer. In that case, the controller return ack_complete and
1793 * no respose is necessary.
1795 * XXX fwohci.c and firewire.c should inform event_code such as
1796 * ack_complete or ack_pending to upper driver.
1800 sfp = (struct fw_pkt *)xfer->send.buf;
1801 sfp->mode.wres.dst = rfp->mode.wreqb.src;
1802 xfer->dst = sfp->mode.wres.dst;
1803 xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1804 xfer->act.hand = sbp_loginres_callback;
1805 xfer->retry_req = fw_asybusy;
1807 sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1808 sfp->mode.wres.tcode = FWTCODE_WRES;
1809 sfp->mode.wres.rtcode = 0;
1810 sfp->mode.wres.pri = 0;
1812 fw_asyreq(xfer->fc, -1, xfer);
1815 STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1823 sbp_recv(struct fw_xfer *xfer)
1833 sbp_attach(device_t dev)
1835 struct sbp_softc *sbp;
1836 struct cam_devq *devq;
1837 struct fw_xfer *xfer;
1841 kprintf("sbp_attach (cold=%d)\n", cold);
1848 sbp = ((struct sbp_softc *)device_get_softc(dev));
1849 bzero(sbp, sizeof(struct sbp_softc));
1851 sbp->fd.fc = device_get_ivars(dev);
1854 max_speed = sbp->fd.fc->speed;
1856 error = bus_dma_tag_create(/*parent*/sbp->fd.fc->dmat,
1857 /* XXX shoud be 4 for sane backend? */
1860 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1861 /*highaddr*/BUS_SPACE_MAXADDR,
1862 /*filter*/NULL, /*filterarg*/NULL,
1863 /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1864 /*maxsegsz*/SBP_SEG_MAX,
1865 /*flags*/BUS_DMA_ALLOCNOW,
1866 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
1867 /*lockfunc*/busdma_lock_mutex,
1872 kprintf("sbp_attach: Could not allocate DMA tag "
1873 "- error %d\n", error);
1877 devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1881 for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1882 sbp->targets[i].fwdev = NULL;
1883 sbp->targets[i].luns = NULL;
1886 sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1887 device_get_unit(dev),
1890 /*tagged*/ SBP_QUEUE_LEN - 1,
1892 cam_simq_release(devq);
1893 if (sbp->sim == NULL)
1896 if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS)
1899 if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim),
1900 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1901 xpt_bus_deregister(cam_sim_path(sbp->sim));
1905 /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1906 sbp->fwb.start = ((u_int64_t)SBP_BIND_HI << 32) | SBP_DEV2ADDR(0, 0);
1907 sbp->fwb.end = sbp->fwb.start + 0xffff;
1908 sbp->fwb.act_type = FWACT_XFER;
1909 /* pre-allocate xfer */
1910 STAILQ_INIT(&sbp->fwb.xferlist);
1911 for (i = 0; i < SBP_NUM_OCB/2; i ++) {
1912 xfer = fw_xfer_alloc_buf(M_SBP,
1914 /* recv */SBP_RECV_LEN);
1915 xfer->act.hand = sbp_recv;
1917 xfer->fc = sbp->fd.fc;
1919 xfer->sc = (caddr_t)sbp;
1920 STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1922 fw_bindadd(sbp->fd.fc, &sbp->fwb);
1924 sbp->fd.post_busreset = sbp_post_busreset;
1925 sbp->fd.post_explore = sbp_post_explore;
1927 if (sbp->fd.fc->status != -1) {
1929 sbp_post_busreset((void *)sbp);
1930 sbp_post_explore((void *)sbp);
1933 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
1937 cam_sim_free(sbp->sim);
1942 sbp_logout_all(struct sbp_softc *sbp)
1944 struct sbp_target *target;
1945 struct sbp_dev *sdev;
1949 kprintf("sbp_logout_all\n");
1951 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
1952 target = &sbp->targets[i];
1953 if (target->luns == NULL)
1955 for (j = 0; j < target->num_lun; j++) {
1956 sdev = target->luns[j];
1959 callout_stop(&sdev->login_callout);
1960 if (sdev->status >= SBP_DEV_TOATTACH &&
1961 sdev->status <= SBP_DEV_ATTACHED)
1962 sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
1970 sbp_shutdown(device_t dev)
1972 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1974 sbp_logout_all(sbp);
1979 sbp_free_sdev(struct sbp_dev *sdev)
1985 for (i = 0; i < SBP_QUEUE_LEN; i++)
1986 bus_dmamap_destroy(sdev->target->sbp->dmat,
1987 sdev->ocb[i].dmamap);
1988 fwdma_free(sdev->target->sbp->fd.fc, &sdev->dma);
1993 sbp_free_target(struct sbp_target *target)
1995 struct fw_xfer *xfer, *next;
1998 if (target->luns == NULL)
2000 callout_stop(&target->mgm_ocb_timeout);
2001 for (i = 0; i < target->num_lun; i++)
2002 sbp_free_sdev(target->luns[i]);
2004 for (xfer = STAILQ_FIRST(&target->xferlist);
2005 xfer != NULL; xfer = next) {
2006 next = STAILQ_NEXT(xfer, link);
2007 fw_xfer_free_buf(xfer);
2009 STAILQ_INIT(&target->xferlist);
2010 kfree(target->luns, M_SBP);
2011 target->num_lun = 0;
2012 target->luns = NULL;
2013 target->fwdev = NULL;
2017 sbp_detach(device_t dev)
2019 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2020 struct firewire_comm *fc = sbp->fd.fc;
2021 struct fw_xfer *xfer, *next;
2025 kprintf("sbp_detach\n");
2028 for (i = 0; i < SBP_NUM_TARGETS; i ++)
2029 sbp_cam_detach_target(&sbp->targets[i]);
2030 xpt_async(AC_LOST_DEVICE, sbp->path, NULL);
2031 xpt_free_path(sbp->path);
2032 xpt_bus_deregister(cam_sim_path(sbp->sim));
2033 cam_sim_free(sbp->sim);
2035 sbp_logout_all(sbp);
2037 /* XXX wait for logout completion */
2038 tsleep(&i, FWPRI, "sbpdtc", hz/2);
2040 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++)
2041 sbp_free_target(&sbp->targets[i]);
2043 for (xfer = STAILQ_FIRST(&sbp->fwb.xferlist);
2044 xfer != NULL; xfer = next) {
2045 next = STAILQ_NEXT(xfer, link);
2046 fw_xfer_free_buf(xfer);
2048 STAILQ_INIT(&sbp->fwb.xferlist);
2049 fw_bindremove(fc, &sbp->fwb);
2051 bus_dma_tag_destroy(sbp->dmat);
2057 sbp_cam_detach_sdev(struct sbp_dev *sdev)
2061 if (sdev->status == SBP_DEV_DEAD)
2063 if (sdev->status == SBP_DEV_RESET)
2066 xpt_release_devq(sdev->path,
2067 sdev->freeze, TRUE);
2069 xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
2070 xpt_free_path(sdev->path);
2073 sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
2077 sbp_cam_detach_target(struct sbp_target *target)
2081 if (target->luns != NULL) {
2083 kprintf("sbp_detach_target %d\n", target->target_id);
2085 callout_stop(&target->scan_callout);
2086 for (i = 0; i < target->num_lun; i++)
2087 sbp_cam_detach_sdev(target->luns[i]);
2092 sbp_target_reset(struct sbp_dev *sdev, int method)
2095 struct sbp_target *target = sdev->target;
2096 struct sbp_dev *tsdev;
2098 for (i = 0; i < target->num_lun; i++) {
2099 tsdev = target->luns[i];
2102 if (tsdev->status == SBP_DEV_DEAD)
2104 if (tsdev->status == SBP_DEV_RESET)
2106 xpt_freeze_devq(tsdev->path, 1);
2108 sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT);
2110 tsdev->status = SBP_DEV_LOGIN;
2114 kprintf("target reset\n");
2115 sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2118 kprintf("reset start\n");
2119 sbp_reset_start(sdev);
2126 sbp_mgm_timeout(void *arg)
2128 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2129 struct sbp_dev *sdev = ocb->sdev;
2130 struct sbp_target *target = sdev->target;
2132 sbp_show_sdev_info(sdev, 2);
2133 kprintf("request timeout(mgm orb:0x%08x) ... ",
2134 (u_int32_t)ocb->bus_addr);
2135 target->mgm_ocb_cur = NULL;
2136 sbp_free_ocb(sdev, ocb);
2139 kprintf("run next request\n");
2140 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2143 kprintf("reset start\n");
2144 sbp_reset_start(sdev);
2149 sbp_timeout(void *arg)
2151 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2152 struct sbp_dev *sdev = ocb->sdev;
2154 sbp_show_sdev_info(sdev, 2);
2155 kprintf("request timeout(cmd orb:0x%08x) ... ",
2156 (u_int32_t)ocb->bus_addr);
2159 switch(sdev->timeout) {
2161 kprintf("agent reset\n");
2162 xpt_freeze_devq(sdev->path, 1);
2164 sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2165 sbp_agent_reset(sdev);
2169 sbp_target_reset(sdev, sdev->timeout - 1);
2174 sbp_cam_detach_target(target);
2175 if (target->luns != NULL)
2176 kfree(target->luns, M_SBP);
2177 target->num_lun = 0;
2178 target->luns = NULL;
2179 target->fwdev = NULL;
2185 sbp_action1(struct cam_sim *sim, union ccb *ccb)
2188 struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2189 struct sbp_target *target = NULL;
2190 struct sbp_dev *sdev = NULL;
2192 /* target:lun -> sdev mapping */
2194 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2195 && ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2196 target = &sbp->targets[ccb->ccb_h.target_id];
2197 if (target->fwdev != NULL
2198 && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2199 && ccb->ccb_h.target_lun < target->num_lun) {
2200 sdev = target->luns[ccb->ccb_h.target_lun];
2201 if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED &&
2202 sdev->status != SBP_DEV_PROBE)
2209 kprintf("invalid target %d lun %d\n",
2210 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2213 switch (ccb->ccb_h.func_code) {
2216 case XPT_GET_TRAN_SETTINGS:
2217 case XPT_SET_TRAN_SETTINGS:
2218 case XPT_CALC_GEOMETRY:
2221 kprintf("%s:%d:%d:func_code 0x%04x: "
2222 "Invalid target (target needed)\n",
2223 device_get_nameunit(sbp->fd.dev),
2224 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2225 ccb->ccb_h.func_code);
2228 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2235 /* The opcodes sometimes aimed at a target (sc is valid),
2236 * sometimes aimed at the SIM (sc is invalid and target is
2237 * CAM_TARGET_WILDCARD)
2240 ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2242 kprintf("%s:%d:%d func_code 0x%04x: "
2243 "Invalid target (no wildcard)\n",
2244 device_get_nameunit(sbp->fd.dev),
2245 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2246 ccb->ccb_h.func_code);
2248 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2254 /* XXX Hm, we should check the input parameters */
2258 switch (ccb->ccb_h.func_code) {
2261 struct ccb_scsiio *csio;
2262 struct sbp_ocb *ocb;
2269 kprintf("%s:%d:%d XPT_SCSI_IO: "
2270 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2272 "%db cmd/%db data/%db sense\n",
2273 device_get_nameunit(sbp->fd.dev),
2274 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2275 csio->cdb_io.cdb_bytes[0],
2276 csio->cdb_io.cdb_bytes[1],
2277 csio->cdb_io.cdb_bytes[2],
2278 csio->cdb_io.cdb_bytes[3],
2279 csio->cdb_io.cdb_bytes[4],
2280 csio->cdb_io.cdb_bytes[5],
2281 csio->cdb_io.cdb_bytes[6],
2282 csio->cdb_io.cdb_bytes[7],
2283 csio->cdb_io.cdb_bytes[8],
2284 csio->cdb_io.cdb_bytes[9],
2285 ccb->ccb_h.flags & CAM_DIR_MASK,
2286 csio->cdb_len, csio->dxfer_len,
2290 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2295 /* if we are in probe stage, pass only probe commands */
2296 if (sdev->status == SBP_DEV_PROBE) {
2298 name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2299 kprintf("probe stage, periph name: %s\n", name);
2300 if (strcmp(name, "probe") != 0) {
2301 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2307 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
2308 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2313 ocb->flags = OCB_ACT_CMD;
2316 ccb->ccb_h.ccb_sdev_ptr = sdev;
2317 ocb->orb[0] = htonl(1 << 31);
2319 ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2320 ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2321 speed = min(target->fwdev->speed, max_speed);
2322 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2323 | ORB_CMD_MAXP(speed + 7));
2324 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
2325 ocb->orb[4] |= htonl(ORB_CMD_IN);
2328 if (csio->ccb_h.flags & CAM_SCATTER_VALID)
2329 kprintf("sbp: CAM_SCATTER_VALID\n");
2330 if (csio->ccb_h.flags & CAM_DATA_PHYS)
2331 kprintf("sbp: CAM_DATA_PHYS\n");
2333 if (csio->ccb_h.flags & CAM_CDB_POINTER)
2334 cdb = (void *)csio->cdb_io.cdb_ptr;
2336 cdb = (void *)&csio->cdb_io.cdb_bytes;
2337 bcopy(cdb, (void *)&ocb->orb[5], csio->cdb_len);
2339 kprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2340 kprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2342 if (ccb->csio.dxfer_len > 0) {
2346 error = bus_dmamap_load(/*dma tag*/sbp->dmat,
2347 /*dma map*/ocb->dmamap,
2349 ccb->csio.dxfer_len,
2355 kprintf("sbp: bus_dmamap_load error %d\n", error);
2357 sbp_execute_ocb(ocb, NULL, 0, 0);
2360 case XPT_CALC_GEOMETRY:
2362 struct ccb_calc_geometry *ccg;
2363 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2365 u_int32_t secs_per_cylinder;
2370 if (ccg->block_size == 0) {
2371 kprintf("sbp_action1: block_size is 0.\n");
2372 ccb->ccb_h.status = CAM_REQ_INVALID;
2377 kprintf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: Volume size = %ju\n",
2378 device_get_nameunit(sbp->fd.dev),
2379 cam_sim_path(sbp->sim),
2380 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2381 (uintmax_t)ccg->volume_size);
2384 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2385 size_mb = ccg->volume_size
2386 / ((1024L * 1024L) / ccg->block_size);
2388 if (size_mb > 1024 && extended) {
2390 ccg->secs_per_track = 63;
2393 ccg->secs_per_track = 32;
2395 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2396 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2397 ccb->ccb_h.status = CAM_REQ_CMP;
2399 cam_calc_geometry(ccg, /*extended*/1);
2404 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
2408 kprintf("%s:%d:XPT_RESET_BUS: \n",
2409 device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2412 ccb->ccb_h.status = CAM_REQ_INVALID;
2416 case XPT_PATH_INQ: /* Path routing inquiry */
2418 struct ccb_pathinq *cpi = &ccb->cpi;
2421 kprintf("%s:%d:%d XPT_PATH_INQ:.\n",
2422 device_get_nameunit(sbp->fd.dev),
2423 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2425 cpi->version_num = 1; /* XXX??? */
2426 cpi->hba_inquiry = PI_TAG_ABLE;
2427 cpi->target_sprt = 0;
2428 cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE;
2429 cpi->hba_eng_cnt = 0;
2430 cpi->max_target = SBP_NUM_TARGETS - 1;
2431 cpi->max_lun = SBP_NUM_LUNS - 1;
2432 cpi->initiator_id = SBP_INITIATOR;
2433 cpi->bus_id = sim->bus_id;
2434 cpi->base_transfer_speed = 400 * 1000 / 8;
2435 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2436 strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2437 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2438 cpi->unit_number = sim->unit_number;
2439 cpi->transport = XPORT_SPI; /* XX should have a FireWire */
2440 cpi->transport_version = 2;
2441 cpi->protocol = PROTO_SCSI;
2442 cpi->protocol_version = SCSI_REV_2;
2443 cpi->maxio = SBP_MAXPHYS;
2445 cpi->ccb_h.status = CAM_REQ_CMP;
2449 case XPT_GET_TRAN_SETTINGS:
2451 struct ccb_trans_settings *cts = &ccb->cts;
2452 struct ccb_trans_settings_scsi *scsi =
2453 &cts->proto_specific.scsi;
2454 struct ccb_trans_settings_spi *spi =
2455 &cts->xport_specific.spi;
2457 cts->protocol = PROTO_SCSI;
2458 cts->protocol_version = SCSI_REV_2;
2459 cts->transport = XPORT_SPI; /* should have a FireWire */
2460 cts->transport_version = 2;
2461 spi->valid = CTS_SPI_VALID_DISC;
2462 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
2463 scsi->valid = CTS_SCSI_VALID_TQ;
2464 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2466 kprintf("%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 cts->ccb_h.status = CAM_REQ_CMP;
2475 ccb->ccb_h.status = CAM_UA_ABORT;
2478 case XPT_SET_TRAN_SETTINGS:
2481 ccb->ccb_h.status = CAM_REQ_INVALID;
2489 sbp_action(struct cam_sim *sim, union ccb *ccb)
2492 sbp_action1(sim, ccb);
2497 sbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error)
2500 struct sbp_ocb *ocb;
2501 struct sbp_ocb *prev;
2502 bus_dma_segment_t *s;
2505 kprintf("sbp_execute_ocb: error=%d\n", error);
2507 ocb = (struct sbp_ocb *)arg;
2510 kprintf("sbp_execute_ocb: seg %d", seg);
2511 for (i = 0; i < seg; i++)
2512 kprintf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2513 (uintmax_t)segments[i].ds_len);
2518 /* direct pointer */
2520 if (s->ds_len > SBP_SEG_MAX)
2521 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2522 ocb->orb[3] = htonl(s->ds_addr);
2523 ocb->orb[4] |= htonl(s->ds_len);
2524 } else if(seg > 1) {
2526 for (i = 0; i < seg; i++) {
2529 /* XXX LSI Logic "< 16 byte" bug might be hit */
2531 kprintf("sbp_execute_ocb: warning, "
2532 "segment length(%zd) is less than 16."
2534 (size_t)s->ds_len, i+1, (intmax_t)seg);
2536 if (s->ds_len > SBP_SEG_MAX)
2537 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2538 ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2539 ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2541 ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2545 bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2546 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2547 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2548 prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2549 fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2550 if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) {
2551 ocb->sdev->flags &= ~ORB_LINK_DEAD;
2552 sbp_orb_pointer(ocb->sdev, ocb);
2557 sbp_poll(struct cam_sim *sim)
2559 struct sbp_softc *sbp;
2560 struct firewire_comm *fc;
2562 sbp = (struct sbp_softc *)sim->softc;
2565 fc->poll(fc, 0, -1);
2570 static struct sbp_ocb *
2571 sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2573 struct sbp_ocb *ocb;
2574 struct sbp_ocb *next;
2580 sbp_show_sdev_info(sdev, 2);
2581 kprintf("%s: 0x%08x src %d\n",
2582 __func__, ntohl(sbp_status->orb_lo), sbp_status->src);
2584 for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2585 next = STAILQ_NEXT(ocb, ocb);
2586 if (OCB_MATCH(ocb, sbp_status)) {
2588 STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2589 if (ocb->ccb != NULL)
2590 callout_stop(&ocb->ccb->ccb_h.timeout_ch);
2591 if (ntohl(ocb->orb[4]) & 0xffff) {
2592 bus_dmamap_sync(sdev->target->sbp->dmat,
2594 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2595 BUS_DMASYNC_POSTREAD :
2596 BUS_DMASYNC_POSTWRITE);
2597 bus_dmamap_unload(sdev->target->sbp->dmat,
2600 if (sbp_status->src == SRC_NO_NEXT) {
2602 sbp_orb_pointer(sdev, next);
2603 else if (order > 0) {
2605 * Unordered execution
2606 * We need to send pointer for
2609 sdev->flags |= ORB_LINK_DEAD;
2618 if (ocb && order > 0) {
2619 sbp_show_sdev_info(sdev, 2);
2620 kprintf("unordered execution order:%d\n", order);
2626 static struct sbp_ocb *
2627 sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2629 struct sbp_ocb *prev;
2634 sbp_show_sdev_info(sdev, 2);
2635 kprintf("%s: 0x%08jx\n", __func__, (uintmax_t)ocb->bus_addr);
2637 prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2638 STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2640 if (ocb->ccb != NULL)
2641 callout_reset(&ocb->ccb->ccb_h.timeout_ch,
2642 (ocb->ccb->ccb_h.timeout * hz) / 1000, sbp_timeout, ocb);
2646 kprintf("linking chain 0x%jx -> 0x%jx\n",
2647 (uintmax_t)prev->bus_addr, (uintmax_t)ocb->bus_addr);
2649 prev->orb[1] = htonl(ocb->bus_addr);
2657 static struct sbp_ocb *
2658 sbp_get_ocb(struct sbp_dev *sdev)
2660 struct sbp_ocb *ocb;
2663 ocb = STAILQ_FIRST(&sdev->free_ocbs);
2665 kprintf("ocb shortage!!!\n");
2669 STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2676 sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2680 STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2684 sbp_abort_ocb(struct sbp_ocb *ocb, int status)
2686 struct sbp_dev *sdev;
2690 sbp_show_sdev_info(sdev, 2);
2691 kprintf("sbp_abort_ocb 0x%jx\n", (uintmax_t)ocb->bus_addr);
2694 if (ocb->ccb != NULL)
2695 sbp_print_scsi_cmd(ocb);
2697 if (ntohl(ocb->orb[4]) & 0xffff) {
2698 bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
2699 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2700 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2701 bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
2703 if (ocb->ccb != NULL) {
2704 callout_stop(&ocb->ccb->ccb_h.timeout_ch);
2705 ocb->ccb->ccb_h.status = status;
2708 sbp_free_ocb(sdev, ocb);
2712 sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2714 struct sbp_ocb *ocb, *next;
2715 STAILQ_HEAD(, sbp_ocb) temp;
2719 STAILQ_CONCAT(&temp, &sdev->ocbs);
2720 for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2721 next = STAILQ_NEXT(ocb, ocb);
2722 sbp_abort_ocb(ocb, status);
2727 static devclass_t sbp_devclass;
2730 * Because sbp is a static device that always exists under any attached
2731 * firewire device, and not scanned by the firewire device, we need an
2732 * identify function to install the device. For our sanity we want
2733 * the sbp device to have the same unit number as the fireweire device.
2736 static device_method_t sbp_methods[] = {
2737 /* device interface */
2738 DEVMETHOD(device_identify, bus_generic_identify_sameunit),
2739 DEVMETHOD(device_probe, sbp_probe),
2740 DEVMETHOD(device_attach, sbp_attach),
2741 DEVMETHOD(device_detach, sbp_detach),
2742 DEVMETHOD(device_shutdown, sbp_shutdown),
2747 static driver_t sbp_driver = {
2750 sizeof(struct sbp_softc),
2753 DECLARE_DUMMY_MODULE(sbp);
2754 DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, NULL, NULL);
2755 MODULE_VERSION(sbp, 1);
2756 MODULE_DEPEND(sbp, firewire, 1, 1, 1);
2757 MODULE_DEPEND(sbp, cam, 1, 1, 1);