Add hw.firewire.sbp.tags to control tagging for SBP devices. The default
[dragonfly.git] / sys / dev / disk / sbp / sbp.c
1 /*
2  * Copyright (c) 2003 Hidetosh Shimokawa
3  * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetosh Shimokawa
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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:
16  *
17  *    This product includes software developed by K. Kobayashi and H. Shimokawa
18  *
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.
21  *
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.
33  * 
34  * $FreeBSD: src/sys/dev/firewire/sbp.c,v 1.5.2.19 2003/05/12 04:16:30 simokawa Exp $
35  * $DragonFly: src/sys/dev/disk/sbp/sbp.c,v 1.6 2004/01/13 17:32:12 joerg Exp $
36  *
37  */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/module.h>
42 #include <sys/bus.h>
43 #include <sys/mbuf.h>
44 #include <sys/sysctl.h>
45 #include <machine/bus.h>
46 #include <sys/malloc.h>
47
48 #if __FreeBSD_version < 500106
49 #include <sys/devicestat.h>     /* for struct devstat */
50 #endif
51
52 #include <bus/cam/cam.h>
53 #include <bus/cam/cam_ccb.h>
54 #include <bus/cam/cam_sim.h>
55 #include <bus/cam/cam_xpt_sim.h>
56 #include <bus/cam/cam_debug.h>
57 #include <bus/cam/cam_periph.h>
58
59 #include <bus/cam/scsi/scsi_all.h>
60 #include <bus/cam/scsi/scsi_message.h>
61 #include <bus/cam/scsi/scsi_da.h>
62
63 #include <sys/kernel.h>
64
65 #include <bus/firewire/firewire.h>
66 #include <bus/firewire/firewirereg.h>
67 #include <bus/firewire/fwdma.h>
68 #include <bus/firewire/iec13213.h>
69
70 #define ccb_sdev_ptr    spriv_ptr0
71 #define ccb_sbp_ptr     spriv_ptr1
72
73 #define SBP_NUM_TARGETS 8 /* MAX 64 */
74 #define SBP_NUM_LUNS 8  /* limited by CAM_SCSI2_MAXLUN in cam_xpt.c */
75 #define SBP_DMA_SIZE PAGE_SIZE
76 #define SBP_LOGIN_SIZE sizeof(struct sbp_login_res)
77 #define SBP_QUEUE_LEN ((SBP_DMA_SIZE - SBP_LOGIN_SIZE) / sizeof(struct sbp_ocb))
78 #define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS)
79
80 #define SBP_INITIATOR 7
81
82 #define LOGIN_DELAY 2
83
84 /* 
85  * STATUS FIFO addressing
86  *   bit
87  * -----------------------
88  *  0- 1( 2): 0 (alingment)
89  *  2- 7( 6): target
90  *  8-15( 8): lun
91  * 16-23( 8): unit
92  * 24-31( 8): reserved
93  * 32-47(16): SBP_BIND_HI 
94  * 48-64(16): bus_id, node_id 
95  */
96 #define SBP_BIND_HI 0x1
97 #define SBP_DEV2ADDR(u, t, l) \
98         ((((u) & 0xff) << 16) | (((l) & 0xff) << 8) | (((t) & 0x3f) << 2))
99 #define SBP_ADDR2TRG(a) (((a) >> 2) & 0x3f)
100 #define SBP_ADDR2LUN(a) (((a) >> 8) & 0xff)
101
102 #define ORB_NOTIFY      (1 << 31)
103 #define ORB_FMT_STD     (0 << 29)
104 #define ORB_FMT_VED     (2 << 29)
105 #define ORB_FMT_NOP     (3 << 29)
106 #define ORB_FMT_MSK     (3 << 29)
107 #define ORB_EXV         (1 << 28)
108 /* */
109 #define ORB_CMD_IN      (1 << 27)
110 /* */
111 #define ORB_CMD_SPD(x)  ((x) << 24)
112 #define ORB_CMD_MAXP(x) ((x) << 20)
113 #define ORB_RCN_TMO(x)  ((x) << 20)
114 #define ORB_CMD_PTBL    (1 << 19)
115 #define ORB_CMD_PSZ(x)  ((x) << 16)
116
117 #define ORB_FUN_LGI     (0 << 16)
118 #define ORB_FUN_QLG     (1 << 16)
119 #define ORB_FUN_RCN     (3 << 16)
120 #define ORB_FUN_LGO     (7 << 16)
121 #define ORB_FUN_ATA     (0xb << 16)
122 #define ORB_FUN_ATS     (0xc << 16)
123 #define ORB_FUN_LUR     (0xe << 16)
124 #define ORB_FUN_RST     (0xf << 16)
125 #define ORB_FUN_MSK     (0xf << 16)
126 #define ORB_FUN_RUNQUEUE 0xffff
127
128 static char *orb_fun_name[] = {
129         /* 0 */ "LOGIN",
130         /* 1 */ "QUERY LOGINS",
131         /* 2 */ "Reserved",
132         /* 3 */ "RECONNECT",
133         /* 4 */ "SET PASSWORD",
134         /* 5 */ "Reserved",
135         /* 6 */ "Reserved",
136         /* 7 */ "LOGOUT",
137         /* 8 */ "Reserved",
138         /* 9 */ "Reserved",
139         /* A */ "Reserved",
140         /* B */ "ABORT TASK",
141         /* C */ "ABORT TASK SET",
142         /* D */ "Reserved",
143         /* E */ "LOGICAL UNIT RESET",
144         /* F */ "TARGET RESET"
145 };
146
147 #define ORB_RES_CMPL 0
148 #define ORB_RES_FAIL 1
149 #define ORB_RES_ILLE 2
150 #define ORB_RES_VEND 3
151
152 static int debug = 0;
153 static int auto_login = 1;
154 static int max_speed = 2;
155 static int sbp_cold = 1;
156 static int sbp_tags = 0;
157
158 SYSCTL_DECL(_hw_firewire);
159 SYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem");
160 SYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0,
161         "SBP debug flag");
162 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0,
163         "SBP perform login automatically");
164 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0,
165         "SBP transfer max speed");
166 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, tags, CTLFLAG_RW, &sbp_tags, 0,
167         "SBP tagged queuing support");
168
169 #define SBP_DEBUG(x)    if (debug > x) {
170 #define END_DEBUG       }
171
172 #define NEED_RESPONSE 0
173
174 struct ind_ptr {
175         u_int32_t hi,lo;
176 };
177 #define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE)
178 #ifdef __sparc64__ /* iommu */
179 #define SBP_IND_MAX howmany(MAXPHYS, SBP_SEG_MAX)
180 #else
181 #define SBP_IND_MAX howmany(MAXPHYS, PAGE_SIZE)
182 #endif
183 struct sbp_ocb {
184         STAILQ_ENTRY(sbp_ocb)   ocb;
185         union ccb       *ccb;
186         bus_addr_t      bus_addr;
187         volatile u_int32_t      orb[8];
188 #define IND_PTR_OFFSET  (8*sizeof(u_int32_t))
189         volatile struct ind_ptr  ind_ptr[SBP_IND_MAX];
190         struct sbp_dev  *sdev;
191         int             flags; /* XXX should be removed */
192         bus_dmamap_t    dmamap;
193 };
194
195 #define OCB_ACT_MGM 0
196 #define OCB_ACT_CMD 1
197 #define OCB_MATCH(o,s)  ((o)->bus_addr == ntohl((s)->orb_lo))
198
199 #define SBP_RECV_LEN (16 + 32) /* header + payload */
200
201 struct sbp_login_res{
202         u_int16_t       len;
203         u_int16_t       id;
204         u_int16_t       res0;
205         u_int16_t       cmd_hi;
206         u_int32_t       cmd_lo;
207         u_int16_t       res1;
208         u_int16_t       recon_hold;
209 };
210 struct sbp_status{
211 #if BYTE_ORDER == BIG_ENDIAN
212         u_int8_t        src:2,
213                         resp:2,
214                         dead:1,
215                         len:3;
216 #else
217         u_int8_t        len:3,
218                         dead:1,
219                         resp:2,
220                         src:2;
221 #endif
222         u_int8_t        status;
223         u_int16_t       orb_hi;
224         u_int32_t       orb_lo;
225         u_int32_t       data[6];
226 };
227 struct sbp_cmd_status{
228 #define SBP_SFMT_CURR 0
229 #define SBP_SFMT_DEFER 1
230 #if BYTE_ORDER == BIG_ENDIAN
231         u_int8_t        sfmt:2,
232                         status:6;
233         u_int8_t        valid:1,
234                         mark:1,
235                         eom:1,
236                         ill_len:1,
237                         s_key:4;
238 #else
239         u_int8_t        status:6,
240                         sfmt:2;
241         u_int8_t        s_key:4,
242                         ill_len:1,
243                         eom:1,
244                         mark:1,
245                         valid:1;
246 #endif
247         u_int8_t        s_code;
248         u_int8_t        s_qlfr;
249         u_int32_t       info;
250         u_int32_t       cdb;
251
252 #if BYTE_ORDER == BIG_ENDIAN
253         u_int32_t       s_keydep:24,
254                         fru:8;
255 #else
256         u_int32_t       fru:8,
257                         s_keydep:24;
258 #endif
259         u_int32_t       vend[2];
260
261 };
262
263 struct sbp_dev{
264 #define SBP_DEV_RESET           0       /* accept login */
265 #define SBP_DEV_LOGIN           1       /* to login */
266 #if 0
267 #define SBP_DEV_RECONN          2       /* to reconnect */
268 #endif
269 #define SBP_DEV_TOATTACH        3       /* to attach */
270 #define SBP_DEV_PROBE           4       /* scan lun */
271 #define SBP_DEV_ATTACHED        5       /* in operation */
272 #define SBP_DEV_DEAD            6       /* unavailable unit */
273 #define SBP_DEV_RETRY           7       /* unavailable unit */
274         u_int8_t status:4,
275                  timeout:4;
276         u_int8_t type;
277         u_int16_t lun_id;
278         int freeze;
279         struct cam_path *path;
280         struct sbp_target *target;
281         struct fwdma_alloc dma;
282         struct sbp_login_res *login;
283         struct callout login_callout;
284         struct sbp_ocb *ocb;
285         STAILQ_HEAD(, sbp_ocb) ocbs;
286         STAILQ_HEAD(, sbp_ocb) free_ocbs;
287         char vendor[32];
288         char product[32];
289         char revision[10];
290 };
291
292 struct sbp_target {
293         int target_id;
294         int num_lun;
295         struct sbp_dev  *luns;
296         struct sbp_softc *sbp;
297         struct fw_device *fwdev;
298         u_int32_t mgm_hi, mgm_lo;
299         struct sbp_ocb *mgm_ocb_cur;
300         STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue;
301         struct callout mgm_ocb_timeout;
302 #define SCAN_DELAY 2
303         struct callout scan_callout;
304         STAILQ_HEAD(, fw_xfer) xferlist;
305         int n_xfer;
306 };
307
308 struct sbp_softc {
309         struct firewire_dev_comm fd;
310         struct cam_sim  *sim;
311         struct cam_path  *path;
312         struct sbp_target targets[SBP_NUM_TARGETS];
313         struct fw_bind fwb;
314         bus_dma_tag_t   dmat;
315 #define SBP_RESOURCE_SHORTAGE 0x10
316         unsigned char flags;
317 };
318 static void sbp_post_explore (void *);
319 static void sbp_recv (struct fw_xfer *);
320 static void sbp_mgm_callback (struct fw_xfer *);
321 static void sbp_cmd_callback (struct fw_xfer *);
322 static void sbp_orb_pointer (struct sbp_dev *, struct sbp_ocb *);
323 static void sbp_execute_ocb (void *,  bus_dma_segment_t *, int, int);
324 static void sbp_free_ocb (struct sbp_dev *, struct sbp_ocb *);
325 static void sbp_abort_ocb (struct sbp_ocb *, int);
326 static void sbp_abort_all_ocbs (struct sbp_dev *, int);
327 static struct fw_xfer * sbp_write_cmd (struct sbp_dev *, int, int);
328 static struct sbp_ocb * sbp_get_ocb (struct sbp_dev *);
329 static struct sbp_ocb * sbp_enqueue_ocb (struct sbp_dev *, struct sbp_ocb *);
330 static struct sbp_ocb * sbp_dequeue_ocb (struct sbp_dev *, struct sbp_status *);
331 static void sbp_cam_detach_target (struct sbp_target *);
332 static void sbp_mgm_timeout (void *arg);
333 static void sbp_timeout (void *arg);
334 static void sbp_mgm_orb (struct sbp_dev *, int, struct sbp_ocb *);
335 #define sbp_login(sdev) \
336         callout_reset(&(sdev)->login_callout, LOGIN_DELAY * hz, \
337                         sbp_login_callout, (void *)(sdev));
338
339 MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire");
340
341 /* cam related functions */
342 static void     sbp_action(struct cam_sim *sim, union ccb *ccb);
343 static void     sbp_poll(struct cam_sim *sim);
344 static void     sbp_cam_scan_lun(struct cam_periph *, union ccb *);
345 static void     sbp_cam_scan_target(void *arg);
346
347 static char *orb_status0[] = {
348         /* 0 */ "No additional information to report",
349         /* 1 */ "Request type not supported",
350         /* 2 */ "Speed not supported",
351         /* 3 */ "Page size not supported",
352         /* 4 */ "Access denied",
353         /* 5 */ "Logical unit not supported",
354         /* 6 */ "Maximum payload too small",
355         /* 7 */ "Reserved for future standardization",
356         /* 8 */ "Resources unavailable",
357         /* 9 */ "Function rejected",
358         /* A */ "Login ID not recognized",
359         /* B */ "Dummy ORB completed",
360         /* C */ "Request aborted",
361         /* FF */ "Unspecified error"
362 #define MAX_ORB_STATUS0 0xd
363 };
364
365 static char *orb_status1_object[] = {
366         /* 0 */ "Operation request block (ORB)",
367         /* 1 */ "Data buffer",
368         /* 2 */ "Page table",
369         /* 3 */ "Unable to specify"
370 };
371
372 static char *orb_status1_serial_bus_error[] = {
373         /* 0 */ "Missing acknowledge",
374         /* 1 */ "Reserved; not to be used",
375         /* 2 */ "Time-out error",
376         /* 3 */ "Reserved; not to be used",
377         /* 4 */ "Busy retry limit exceeded(X)",
378         /* 5 */ "Busy retry limit exceeded(A)",
379         /* 6 */ "Busy retry limit exceeded(B)",
380         /* 7 */ "Reserved for future standardization",
381         /* 8 */ "Reserved for future standardization",
382         /* 9 */ "Reserved for future standardization",
383         /* A */ "Reserved for future standardization",
384         /* B */ "Tardy retry limit exceeded",
385         /* C */ "Conflict error",
386         /* D */ "Data error",
387         /* E */ "Type error",
388         /* F */ "Address error"
389 };
390
391 static void
392 sbp_identify(driver_t *driver, device_t parent)
393 {
394         device_t child;
395 SBP_DEBUG(0)
396         printf("sbp_identify\n");
397 END_DEBUG
398
399         child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent));
400 }
401
402 /*
403  * sbp_probe()
404  */
405 static int
406 sbp_probe(device_t dev)
407 {
408         device_t pa;
409
410 SBP_DEBUG(0)
411         printf("sbp_probe\n");
412 END_DEBUG
413
414         pa = device_get_parent(dev);
415         if(device_get_unit(dev) != device_get_unit(pa)){
416                 return(ENXIO);
417         }
418
419         device_set_desc(dev, "SBP2/SCSI over firewire");
420
421         if (bootverbose)
422                 debug = bootverbose;
423         return (0);
424 }
425
426 static void
427 sbp_show_sdev_info(struct sbp_dev *sdev, int new)
428 {
429         struct fw_device *fwdev;
430
431         printf("%s:%d:%d ",
432                 device_get_nameunit(sdev->target->sbp->fd.dev),
433                 sdev->target->target_id,
434                 sdev->lun_id
435         );
436         if (new == 2) {
437                 return;
438         }
439         fwdev = sdev->target->fwdev;
440         printf("ordered:%d type:%d EUI:%08x%08x node:%d "
441                 "speed:%d maxrec:%d",
442                 (sdev->type & 0x40) >> 6,
443                 (sdev->type & 0x1f),
444                 fwdev->eui.hi,
445                 fwdev->eui.lo,
446                 fwdev->dst,
447                 fwdev->speed,
448                 fwdev->maxrec
449         );
450         if (new)
451                 printf(" new!\n");
452         else
453                 printf("\n");
454         sbp_show_sdev_info(sdev, 2);
455         printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision);
456 }
457
458 static struct {
459         int bus;
460         int target;
461         struct fw_eui64 eui;
462 } wired[] = {
463         /* Bus  Target  EUI64 */
464 #if 0
465         {0,     2,      {0x00018ea0, 0x01fd0154}},      /* Logitec HDD */
466         {0,     0,      {0x00018ea6, 0x00100682}},      /* Logitec DVD */
467         {0,     1,      {0x00d03200, 0xa412006a}},      /* Yano HDD */
468 #endif
469         {-1,    -1,     {0,0}}
470 };
471
472 static int
473 sbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev)
474 {
475         int bus, i, target=-1;
476         char w[SBP_NUM_TARGETS];
477
478         bzero(w, sizeof(w));
479         bus = device_get_unit(sbp->fd.dev);
480
481         /* XXX wired-down configuration should be gotten from
482                                         tunable or device hint */
483         for (i = 0; wired[i].bus >= 0; i ++) {
484                 if (wired[i].bus == bus) {
485                         w[wired[i].target] = 1;
486                         if (wired[i].eui.hi == fwdev->eui.hi &&
487                                         wired[i].eui.lo == fwdev->eui.lo)
488                                 target = wired[i].target;
489                 }
490         }
491         if (target >= 0) {
492                 if(target < SBP_NUM_TARGETS &&
493                                 sbp->targets[target].fwdev == NULL)
494                         return(target);
495                 device_printf(sbp->fd.dev,
496                         "target %d is not free for %08x:%08x\n", 
497                         target, fwdev->eui.hi, fwdev->eui.lo);
498                 target = -1;
499         }
500         /* non-wired target */
501         for (i = 0; i < SBP_NUM_TARGETS; i ++)
502                 if (sbp->targets[i].fwdev == NULL && w[i] == 0) {
503                         target = i;
504                         break;
505                 }
506
507         return target;
508 }
509
510 static struct sbp_target *
511 sbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
512 {
513         int i, maxlun, lun;
514         struct sbp_target *target;
515         struct sbp_dev *sdev;
516         struct crom_context cc;
517         struct csrreg *reg;
518
519 SBP_DEBUG(1)
520         printf("sbp_alloc_target\n");
521 END_DEBUG
522         i = sbp_new_target(sbp, fwdev);
523         if (i < 0) {
524                 device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n");
525                 return NULL;
526         }
527         /* new target */
528         target = &sbp->targets[i];
529         target->sbp = sbp;
530         target->fwdev = fwdev;
531         target->target_id = i;
532         /* XXX we may want to reload mgm port after each bus reset */
533         /* XXX there might be multiple management agents */
534         crom_init_context(&cc, target->fwdev->csrrom);
535         reg = crom_search_key(&cc, CROM_MGM);
536         if (reg == NULL || reg->val == 0) {
537                 printf("NULL management address\n");
538                 target->fwdev = NULL;
539                 return NULL;
540         }
541         target->mgm_hi = 0xffff;
542         target->mgm_lo = 0xf0000000 | (reg->val << 2);
543         target->mgm_ocb_cur = NULL;
544 SBP_DEBUG(1)
545         printf("target:%d mgm_port: %x\n", i, target->mgm_lo);
546 END_DEBUG
547         STAILQ_INIT(&target->xferlist);
548         target->n_xfer = 0;
549         STAILQ_INIT(&target->mgm_ocb_queue);
550         CALLOUT_INIT(&target->mgm_ocb_timeout);
551         CALLOUT_INIT(&target->scan_callout);
552
553         /* XXX num_lun may be changed. realloc luns? */
554         crom_init_context(&cc, target->fwdev->csrrom);
555         /* XXX shoud parse appropriate unit directories only */
556         maxlun = -1;
557         while (cc.depth >= 0) {
558                 reg = crom_search_key(&cc, CROM_LUN);
559                 if (reg == NULL)
560                         break;
561                 lun = reg->val & 0xffff;
562 SBP_DEBUG(0)
563                 printf("target %d lun %d found\n", target->target_id, lun);
564 END_DEBUG
565                 if (maxlun < lun)
566                         maxlun = lun;
567                 crom_next(&cc);
568         }
569         if (maxlun < 0)
570                 printf("no lun found!\n");
571         if (maxlun >= SBP_NUM_LUNS)
572                 maxlun = SBP_NUM_LUNS;
573         target->num_lun = maxlun + 1;
574         target->luns = (struct sbp_dev *) malloc(
575                                 sizeof(struct sbp_dev) * target->num_lun, 
576                                 M_SBP, M_NOWAIT | M_ZERO);
577         for (i = 0; i < target->num_lun; i++) {
578                 sdev = &target->luns[i];
579                 sdev->lun_id = i;
580                 sdev->target = target;
581                 STAILQ_INIT(&sdev->ocbs);
582                 CALLOUT_INIT(&sdev->login_callout);
583                 sdev->status = SBP_DEV_DEAD;
584         }
585         crom_init_context(&cc, target->fwdev->csrrom);
586         while (cc.depth >= 0) {
587                 reg = crom_search_key(&cc, CROM_LUN);
588                 if (reg == NULL)
589                         break;
590                 lun = reg->val & 0xffff;
591                 if (lun >= SBP_NUM_LUNS) {
592                         printf("too large lun %d\n", lun);
593                         continue;
594                 }
595                 sdev = &target->luns[lun];
596                 sdev->status = SBP_DEV_RESET;
597                 sdev->type = (reg->val & 0xf0000) >> 16;
598
599                 fwdma_malloc(sbp->fd.fc, 
600                         /* alignment */ sizeof(u_int32_t),
601                         SBP_DMA_SIZE, &sdev->dma, BUS_DMA_NOWAIT);
602                 if (sdev->dma.v_addr == NULL) {
603                         printf("%s: dma space allocation failed\n",
604                                                         __FUNCTION__);
605                         return (NULL);
606                 }
607                 sdev->login = (struct sbp_login_res *) sdev->dma.v_addr;
608                 sdev->ocb = (struct sbp_ocb *)
609                                 ((char *)sdev->dma.v_addr + SBP_LOGIN_SIZE);
610                 bzero((char *)sdev->ocb,
611                         sizeof (struct sbp_ocb) * SBP_QUEUE_LEN);
612
613                 STAILQ_INIT(&sdev->free_ocbs);
614                 for (i = 0; i < SBP_QUEUE_LEN; i++) {
615                         struct sbp_ocb *ocb;
616                         ocb = &sdev->ocb[i];
617                         ocb->bus_addr = sdev->dma.bus_addr
618                                 + SBP_LOGIN_SIZE
619                                 + sizeof(struct sbp_ocb) * i
620                                 + offsetof(struct sbp_ocb, orb[0]);
621                         if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
622                                 printf("sbp_attach: cannot create dmamap\n");
623                                 return (NULL);
624                         }
625                         sbp_free_ocb(sdev, ocb);
626                 }
627                 crom_next(&cc);
628         }
629         return target;
630 }
631
632 static void
633 sbp_probe_lun(struct sbp_dev *sdev)
634 {
635         struct fw_device *fwdev;
636         struct crom_context c, *cc = &c;
637         struct csrreg *reg;
638
639         bzero(sdev->vendor, sizeof(sdev->vendor));
640         bzero(sdev->product, sizeof(sdev->product));
641
642         fwdev = sdev->target->fwdev;
643         crom_init_context(cc, fwdev->csrrom);
644         /* get vendor string */
645         crom_search_key(cc, CSRKEY_VENDOR);
646         crom_next(cc);
647         crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor));
648         /* skip to the unit directory for SBP-2 */
649         while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) {
650                 if (reg->val == CSRVAL_T10SBP2)
651                         break;
652                 crom_next(cc);
653         }
654         /* get firmware revision */
655         reg = crom_search_key(cc, CSRKEY_FIRM_VER);
656         if (reg != NULL)
657                 snprintf(sdev->revision, sizeof(sdev->revision),
658                                                 "%06x", reg->val);
659         /* get product string */
660         crom_search_key(cc, CSRKEY_MODEL);
661         crom_next(cc);
662         crom_parse_text(cc, sdev->product, sizeof(sdev->product));
663 }
664
665 static void
666 sbp_login_callout(void *arg)
667 {
668         struct sbp_dev *sdev = (struct sbp_dev *)arg;
669         sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
670 }
671
672 #define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
673         && crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
674
675 static void
676 sbp_probe_target(void *arg)
677 {
678         struct sbp_target *target = (struct sbp_target *)arg;
679         struct sbp_softc *sbp;
680         struct sbp_dev *sdev;
681         struct firewire_comm *fc;
682         int i, alive;
683
684         alive = SBP_FWDEV_ALIVE(target->fwdev);
685 SBP_DEBUG(1)
686         printf("sbp_probe_target %d\n", target->target_id);
687         if (!alive)
688                 printf("not alive\n");
689 END_DEBUG
690
691         sbp = target->sbp;
692         fc = target->sbp->fd.fc;
693         /* XXX untimeout mgm_ocb and dequeue */
694         for (i=0; i < target->num_lun; i++) {
695                 sdev = &target->luns[i];
696                 if (alive && (sdev->status != SBP_DEV_DEAD)) {
697                         if (sdev->path != NULL) {
698                                 xpt_freeze_devq(sdev->path, 1);
699                                 sdev->freeze ++;
700                         }
701                         sbp_probe_lun(sdev);
702 SBP_DEBUG(0)
703                         sbp_show_sdev_info(sdev, 
704                                         (sdev->status == SBP_DEV_RESET));
705 END_DEBUG
706
707                         sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
708                         switch (sdev->status) {
709                         case SBP_DEV_RESET:
710                                 /* new or revived target */
711                                 if (auto_login)
712                                         sbp_login(sdev);
713                                 break;
714                         case SBP_DEV_TOATTACH:
715                         case SBP_DEV_PROBE:
716                         case SBP_DEV_ATTACHED:
717                         case SBP_DEV_RETRY:
718                         default:
719                                 sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
720                                 break;
721                         }
722                 } else {
723                         switch (sdev->status) {
724                         case SBP_DEV_ATTACHED:
725 SBP_DEBUG(0)
726                                 /* the device has gone */
727                                 sbp_show_sdev_info(sdev, 2);
728                                 printf("lost target\n");
729 END_DEBUG
730                                 if (sdev->path) {
731                                         xpt_freeze_devq(sdev->path, 1);
732                                         sdev->freeze ++;
733                                 }
734                                 sdev->status = SBP_DEV_RETRY;
735                                 sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
736                                 break;
737                         case SBP_DEV_PROBE:
738                         case SBP_DEV_TOATTACH:
739                                 sdev->status = SBP_DEV_RESET;
740                                 break;
741                         case SBP_DEV_RETRY:
742                         case SBP_DEV_RESET:
743                         case SBP_DEV_DEAD:
744                                 break;
745                         }
746                 }
747         }
748 }
749
750 static void
751 sbp_post_busreset(void *arg)
752 {
753         struct sbp_softc *sbp;
754
755         sbp = (struct sbp_softc *)arg;
756 SBP_DEBUG(0)
757         printf("sbp_post_busreset\n");
758 END_DEBUG
759 }
760
761 static void
762 sbp_post_explore(void *arg)
763 {
764         struct sbp_softc *sbp = (struct sbp_softc *)arg;
765         struct sbp_target *target;
766         struct fw_device *fwdev;
767         int i, alive;
768
769 SBP_DEBUG(0)
770         printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
771 END_DEBUG
772 #if 0   /*
773          * XXX don't let CAM the bus rest. CAM tries to do something with
774          * freezed (DEV_RETRY) devices 
775          */
776         xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
777 #endif
778         if (sbp_cold > 0)
779                 sbp_cold --;
780
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)
786                                 break;
787                 if(fwdev == NULL){
788                         /* device has removed in lower driver */
789                         sbp_cam_detach_target(target);
790                         if (target->luns != NULL)
791                                 free(target->luns, M_SBP);
792                         target->num_lun = 0;;
793                         target->luns = NULL;
794                         target->fwdev = NULL;
795                 }
796         }
797         /* traverse device list */
798         STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
799 SBP_DEBUG(0)
800                 printf("sbp_post_explore: EUI:%08x%08x ",
801                                 fwdev->eui.hi, fwdev->eui.lo);
802                 if (fwdev->status != FWDEVATTACHED)
803                         printf("not attached, state=%d.\n", fwdev->status);
804                 else
805                         printf("attached\n");
806 END_DEBUG
807                 alive = SBP_FWDEV_ALIVE(fwdev);
808                 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
809                         target = &sbp->targets[i];
810                         if(target->fwdev == fwdev ) {
811                                 /* known target */
812                                 break;
813                         }
814                 }
815                 if(i == SBP_NUM_TARGETS){
816                         if (alive) {
817                                 /* new target */
818                                 target = sbp_alloc_target(sbp, fwdev);
819                                 if (target == NULL)
820                                         continue;
821                         } else {
822                                 continue;
823                         }
824                 }
825                 sbp_probe_target((void *)target);
826         }
827 }
828
829 #if NEED_RESPONSE
830 static void
831 sbp_loginres_callback(struct fw_xfer *xfer){
832         int s;
833         struct sbp_dev *sdev;
834         sdev = (struct sbp_dev *)xfer->sc;
835 SBP_DEBUG(1)
836         sbp_show_sdev_info(sdev, 2);
837         printf("sbp_loginres_callback\n");
838 END_DEBUG
839         /* recycle */
840         s = splfw();
841         STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link);
842         splx(s);
843         return;
844 }
845 #endif
846
847 static __inline void
848 sbp_xfer_free(struct fw_xfer *xfer)
849 {
850         struct sbp_dev *sdev;
851         int s;
852
853         sdev = (struct sbp_dev *)xfer->sc;
854         fw_xfer_unload(xfer);
855         s = splfw();
856         STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
857         splx(s);
858 }
859
860 static void
861 sbp_reset_start_callback(struct fw_xfer *xfer)
862 {
863         struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
864         struct sbp_target *target = sdev->target;
865         int i;
866
867         if (xfer->resp != 0) {
868                 sbp_show_sdev_info(sdev, 2);
869                 printf("sbp_reset_start failed: resp=%d\n", xfer->resp);
870         }
871
872         for (i = 0; i < target->num_lun; i++) {
873                 tsdev = &target->luns[i];
874                 if (tsdev->status == SBP_DEV_LOGIN)
875                         sbp_login(sdev);
876         }
877 }
878
879 static void
880 sbp_reset_start(struct sbp_dev *sdev)
881 {
882         struct fw_xfer *xfer;
883         struct fw_pkt *fp;
884
885 SBP_DEBUG(0)
886         sbp_show_sdev_info(sdev, 2);
887         printf("sbp_reset_start\n");
888 END_DEBUG
889
890         xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
891         xfer->act.hand = sbp_reset_start_callback;
892         fp = (struct fw_pkt *)xfer->send.buf;
893         fp->mode.wreqq.dest_hi = 0xffff;
894         fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
895         fp->mode.wreqq.data = htonl(0xf);
896         fw_asyreq(xfer->fc, -1, xfer);
897 }
898
899 static void
900 sbp_mgm_callback(struct fw_xfer *xfer)
901 {
902         struct sbp_dev *sdev;
903         int resp;
904
905         sdev = (struct sbp_dev *)xfer->sc;
906
907 SBP_DEBUG(1)
908         sbp_show_sdev_info(sdev, 2);
909         printf("sbp_mgm_callback\n");
910 END_DEBUG
911         resp = xfer->resp;
912         sbp_xfer_free(xfer);
913 #if 0
914         if (resp != 0) {
915                 sbp_show_sdev_info(sdev, 2);
916                 printf("management ORB failed(%d) ... RESET_START\n", resp);
917                 sbp_reset_start(sdev);
918         }
919 #endif
920         return;
921 }
922
923 static void
924 sbp_cmd_callback(struct fw_xfer *xfer)
925 {
926 SBP_DEBUG(2)
927         struct sbp_dev *sdev;
928         sdev = (struct sbp_dev *)xfer->sc;
929         sbp_show_sdev_info(sdev, 2);
930         printf("sbp_cmd_callback\n");
931 END_DEBUG
932         sbp_xfer_free(xfer);
933         return;
934 }
935
936 static struct sbp_dev *
937 sbp_next_dev(struct sbp_target *target, int lun)
938 {
939         struct sbp_dev *sdev;
940         int i;
941
942         for (i = lun, sdev = &target->luns[lun];
943                         i < target->num_lun; i++, sdev++) {
944                 if (sdev->status == SBP_DEV_PROBE)
945                         break;
946         }
947         if (i >= target->num_lun)
948                 return(NULL);
949         return(sdev);
950 }
951
952 #define SCAN_PRI 1
953 static void
954 sbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb)
955 {
956         struct sbp_target *target;
957         struct sbp_dev *sdev;
958
959         sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
960         target = sdev->target;
961 SBP_DEBUG(0)
962         sbp_show_sdev_info(sdev, 2);
963         printf("sbp_cam_scan_lun\n");
964 END_DEBUG
965         if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
966                 sdev->status = SBP_DEV_ATTACHED;
967         } else {
968                 sbp_show_sdev_info(sdev, 2);
969                 printf("scan failed\n");
970         }
971         sdev = sbp_next_dev(target, sdev->lun_id + 1);
972         if (sdev == NULL) {
973                 free(ccb, M_SBP);
974                 return;
975         }
976         /* reuse ccb */
977         xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
978         ccb->ccb_h.ccb_sdev_ptr = sdev;
979         xpt_action(ccb);
980         xpt_release_devq(sdev->path, sdev->freeze, TRUE);
981         sdev->freeze = 1;
982 }
983
984 static void
985 sbp_cam_scan_target(void *arg)
986 {
987         struct sbp_target *target = (struct sbp_target *)arg;
988         struct sbp_dev *sdev;
989         union ccb *ccb;
990
991         sdev = sbp_next_dev(target, 0);
992         if (sdev == NULL) {
993                 printf("sbp_cam_scan_target: nothing to do for target%d\n",
994                                                         target->target_id);
995                 return;
996         }
997 SBP_DEBUG(0)
998         sbp_show_sdev_info(sdev, 2);
999         printf("sbp_cam_scan_target\n");
1000 END_DEBUG
1001         ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO);
1002         if (ccb == NULL) {
1003                 printf("sbp_cam_scan_target: malloc failed\n");
1004                 return;
1005         }
1006         xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
1007         ccb->ccb_h.func_code = XPT_SCAN_LUN;
1008         ccb->ccb_h.cbfcnp = sbp_cam_scan_lun;
1009         ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1010         ccb->crcn.flags = CAM_FLAG_NONE;
1011         ccb->ccb_h.ccb_sdev_ptr = sdev;
1012
1013         /* The scan is in progress now. */
1014         xpt_action(ccb);
1015         xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1016         sdev->freeze = 1;
1017 }
1018
1019 static __inline void
1020 sbp_scan_dev(struct sbp_dev *sdev)
1021 {
1022         sdev->status = SBP_DEV_PROBE;
1023         callout_reset(&sdev->target->scan_callout, SCAN_DELAY * hz,
1024                         sbp_cam_scan_target, (void *)sdev->target);
1025 }
1026
1027 static void
1028 sbp_do_attach(struct fw_xfer *xfer)
1029 {
1030         struct sbp_dev *sdev;
1031         struct sbp_target *target;
1032         struct sbp_softc *sbp;
1033
1034         sdev = (struct sbp_dev *)xfer->sc;
1035         target = sdev->target;
1036         sbp = target->sbp;
1037 SBP_DEBUG(0)
1038         sbp_show_sdev_info(sdev, 2);
1039         printf("sbp_do_attach\n");
1040 END_DEBUG
1041         sbp_xfer_free(xfer);
1042
1043         if (sdev->path == NULL)
1044                 xpt_create_path(&sdev->path, xpt_periph,
1045                         cam_sim_path(target->sbp->sim),
1046                         target->target_id, sdev->lun_id);
1047
1048         /*
1049          * Let CAM scan the bus if we are in the boot process.
1050          * XXX xpt_scan_bus cannot detect LUN larger than 0
1051          * if LUN 0 doesn't exists.
1052          */
1053         if (sbp_cold > 0) {
1054                 sdev->status = SBP_DEV_ATTACHED;
1055                 return;
1056         }
1057
1058         sbp_scan_dev(sdev);
1059         return;
1060 }
1061
1062 static void
1063 sbp_agent_reset_callback(struct fw_xfer *xfer)
1064 {
1065         struct sbp_dev *sdev;
1066
1067         sdev = (struct sbp_dev *)xfer->sc;
1068 SBP_DEBUG(1)
1069         sbp_show_sdev_info(sdev, 2);
1070         printf("sbp_cmd_callback\n");
1071 END_DEBUG
1072         if (xfer->resp != 0) {
1073                 sbp_show_sdev_info(sdev, 2);
1074                 printf("sbp_cmd_callback resp=%d\n", xfer->resp);
1075         }
1076
1077         sbp_xfer_free(xfer);
1078         if (sdev->path) {
1079                 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1080                 sdev->freeze = 0;
1081         }
1082 }
1083
1084 static void
1085 sbp_agent_reset(struct sbp_dev *sdev)
1086 {
1087         struct fw_xfer *xfer;
1088         struct fw_pkt *fp;
1089
1090 SBP_DEBUG(0)
1091         sbp_show_sdev_info(sdev, 2);
1092         printf("sbp_agent_reset\n");
1093 END_DEBUG
1094         xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1095         if (xfer == NULL)
1096                 return;
1097         if (sdev->status == SBP_DEV_ATTACHED)
1098                 xfer->act.hand = sbp_agent_reset_callback;
1099         else
1100                 xfer->act.hand = sbp_do_attach;
1101         fp = (struct fw_pkt *)xfer->send.buf;
1102         fp->mode.wreqq.data = htonl(0xf);
1103         fw_asyreq(xfer->fc, -1, xfer);
1104         sbp_abort_all_ocbs(sdev, CAM_BDR_SENT);
1105 }
1106
1107 static void
1108 sbp_busy_timeout_callback(struct fw_xfer *xfer)
1109 {
1110         struct sbp_dev *sdev;
1111
1112         sdev = (struct sbp_dev *)xfer->sc;
1113 SBP_DEBUG(1)
1114         sbp_show_sdev_info(sdev, 2);
1115         printf("sbp_busy_timeout_callback\n");
1116 END_DEBUG
1117         sbp_xfer_free(xfer);
1118         sbp_agent_reset(sdev);
1119 }
1120
1121 static void
1122 sbp_busy_timeout(struct sbp_dev *sdev)
1123 {
1124         struct fw_pkt *fp;
1125         struct fw_xfer *xfer;
1126 SBP_DEBUG(0)
1127         sbp_show_sdev_info(sdev, 2);
1128         printf("sbp_busy_timeout\n");
1129 END_DEBUG
1130         xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1131
1132         xfer->act.hand = sbp_busy_timeout_callback;
1133         fp = (struct fw_pkt *)xfer->send.buf;
1134         fp->mode.wreqq.dest_hi = 0xffff;
1135         fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1136         fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1137         fw_asyreq(xfer->fc, -1, xfer);
1138 }
1139
1140 static void
1141 sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1142 {
1143         struct fw_xfer *xfer;
1144         struct fw_pkt *fp;
1145 SBP_DEBUG(2)
1146         sbp_show_sdev_info(sdev, 2);
1147         printf("sbp_orb_pointer\n");
1148 END_DEBUG
1149
1150         xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1151         if (xfer == NULL)
1152                 return;
1153         xfer->act.hand = sbp_cmd_callback;
1154
1155         fp = (struct fw_pkt *)xfer->send.buf;
1156         fp->mode.wreqb.len = 8;
1157         fp->mode.wreqb.extcode = 0;
1158         fp->mode.wreqb.payload[0] = 
1159                 htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1160         fp->mode.wreqb.payload[1] = htonl(ocb->bus_addr);
1161
1162         if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1163                         sbp_xfer_free(xfer);
1164                         ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1165                         xpt_done(ocb->ccb);
1166         }
1167 }
1168
1169 #if 0
1170 static void
1171 sbp_doorbell(struct sbp_dev *sdev)
1172 {
1173         struct fw_xfer *xfer;
1174         struct fw_pkt *fp;
1175 SBP_DEBUG(1)
1176         sbp_show_sdev_info(sdev, 2);
1177         printf("sbp_doorbell\n");
1178 END_DEBUG
1179
1180         xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1181         if (xfer == NULL)
1182                 return;
1183         xfer->act.hand = sbp_cmd_callback;
1184         fp = (struct fw_pkt *)xfer->send.buf;
1185         fp->mode.wreqq.data = htonl(0xf);
1186         fw_asyreq(xfer->fc, -1, xfer);
1187 }
1188 #endif
1189
1190 static struct fw_xfer *
1191 sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1192 {
1193         struct fw_xfer *xfer;
1194         struct fw_pkt *fp;
1195         struct sbp_target *target;
1196         int s, new = 0;
1197
1198         target = sdev->target;
1199         s = splfw();
1200         xfer = STAILQ_FIRST(&target->xferlist);
1201         if (xfer == NULL) {
1202                 if (target->n_xfer > 5 /* XXX */) {
1203                         printf("sbp: no more xfer for this target\n");
1204                         splx(s);
1205                         return(NULL);
1206                 }
1207                 xfer = fw_xfer_alloc_buf(M_SBP, 24, 12);
1208                 if(xfer == NULL){
1209                         printf("sbp: fw_xfer_alloc_buf failed\n");
1210                         splx(s);
1211                         return NULL;
1212                 }
1213                 target->n_xfer ++;
1214                 if (debug)
1215                         printf("sbp: alloc %d xfer\n", target->n_xfer);
1216                 new = 1;
1217         } else {
1218                 STAILQ_REMOVE_HEAD(&target->xferlist, link);
1219         }
1220         splx(s);
1221
1222         microtime(&xfer->tv);
1223
1224         if (tcode == FWTCODE_WREQQ)
1225                 xfer->send.len = 16;
1226         else
1227                 xfer->send.len = 24;
1228         xfer->recv.len = 12;
1229
1230         if (new) {
1231                 xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1232                 xfer->fc = sdev->target->sbp->fd.fc;
1233                 xfer->retry_req = fw_asybusy;
1234         }
1235         xfer->sc = (caddr_t)sdev;
1236         fp = (struct fw_pkt *)xfer->send.buf;
1237         fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
1238         fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
1239         fp->mode.wreqq.tlrt = 0;
1240         fp->mode.wreqq.tcode = tcode;
1241         fp->mode.wreqq.pri = 0;
1242         xfer->dst = FWLOCALBUS | sdev->target->fwdev->dst;
1243         fp->mode.wreqq.dst = xfer->dst;
1244
1245         return xfer;
1246
1247 }
1248
1249 static void
1250 sbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1251 {
1252         struct fw_xfer *xfer;
1253         struct fw_pkt *fp;
1254         struct sbp_ocb *ocb;
1255         struct sbp_target *target;
1256         int s, nid;
1257
1258         target = sdev->target;
1259         nid = target->sbp->fd.fc->nodeid | FWLOCALBUS;
1260
1261         s = splfw();
1262         if (func == ORB_FUN_RUNQUEUE) {
1263                 ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1264                 if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1265                         splx(s);
1266                         return;
1267                 }
1268                 STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1269                 goto start;
1270         }
1271         if ((ocb = sbp_get_ocb(sdev)) == NULL) {
1272                 splx(s);
1273                 return;
1274         }
1275         ocb->flags = OCB_ACT_MGM;
1276         ocb->sdev = sdev;
1277
1278         bzero((void *)(uintptr_t)(volatile void *)ocb->orb, sizeof(ocb->orb));
1279         ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1280         ocb->orb[7] = htonl(SBP_DEV2ADDR(
1281                 device_get_unit(target->sbp->fd.dev),
1282                 target->target_id,
1283                 sdev->lun_id));
1284
1285 SBP_DEBUG(0)
1286         sbp_show_sdev_info(sdev, 2);
1287         printf("%s\n", orb_fun_name[(func>>16)&0xf]);
1288 END_DEBUG
1289         switch (func) {
1290         case ORB_FUN_LGI:
1291                 ocb->orb[2] = htonl(nid << 16);
1292                 ocb->orb[3] = htonl(sdev->dma.bus_addr);
1293                 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_EXV | sdev->lun_id);
1294                 ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
1295                 fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD);
1296                 break;
1297         case ORB_FUN_ATA:
1298                 ocb->orb[0] = htonl((0 << 16) | 0);
1299                 ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
1300                 /* fall through */
1301         case ORB_FUN_RCN:
1302         case ORB_FUN_LGO:
1303         case ORB_FUN_LUR:
1304         case ORB_FUN_RST:
1305         case ORB_FUN_ATS:
1306                 ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
1307                 break;
1308         }
1309
1310         if (target->mgm_ocb_cur != NULL) {
1311                 /* there is a standing ORB */
1312                 STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1313                 splx(s);
1314                 return;
1315         }
1316 start:
1317         target->mgm_ocb_cur = ocb;
1318         splx(s);
1319
1320         callout_reset(&target->mgm_ocb_timeout, 5*hz,
1321                                 sbp_mgm_timeout, (caddr_t)ocb);
1322         xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1323         if(xfer == NULL){
1324                 return;
1325         }
1326         xfer->act.hand = sbp_mgm_callback;
1327
1328         fp = (struct fw_pkt *)xfer->send.buf;
1329         fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
1330         fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
1331         fp->mode.wreqb.len = 8;
1332         fp->mode.wreqb.extcode = 0;
1333         fp->mode.wreqb.payload[0] = htonl(nid << 16);
1334         fp->mode.wreqb.payload[1] = htonl(ocb->bus_addr);
1335
1336         fw_asyreq(xfer->fc, -1, xfer);
1337 }
1338
1339 static void
1340 sbp_print_scsi_cmd(struct sbp_ocb *ocb)
1341 {
1342         struct ccb_scsiio *csio;
1343
1344         csio = &ocb->ccb->csio;
1345         printf("%s:%d:%d XPT_SCSI_IO: "
1346                 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1347                 ", flags: 0x%02x, "
1348                 "%db cmd/%db data/%db sense\n",
1349                 device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1350                 ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun,
1351                 csio->cdb_io.cdb_bytes[0],
1352                 csio->cdb_io.cdb_bytes[1],
1353                 csio->cdb_io.cdb_bytes[2],
1354                 csio->cdb_io.cdb_bytes[3],
1355                 csio->cdb_io.cdb_bytes[4],
1356                 csio->cdb_io.cdb_bytes[5],
1357                 csio->cdb_io.cdb_bytes[6],
1358                 csio->cdb_io.cdb_bytes[7],
1359                 csio->cdb_io.cdb_bytes[8],
1360                 csio->cdb_io.cdb_bytes[9],
1361                 ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1362                 csio->cdb_len, csio->dxfer_len,
1363                 csio->sense_len);
1364 }
1365
1366 static void
1367 sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1368 {
1369         struct sbp_cmd_status *sbp_cmd_status;
1370         struct scsi_sense_data *sense;
1371
1372         sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1373         sense = &ocb->ccb->csio.sense_data;
1374
1375 SBP_DEBUG(0)
1376         sbp_print_scsi_cmd(ocb);
1377         /* XXX need decode status */
1378         sbp_show_sdev_info(ocb->sdev, 2);
1379         printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1380                 sbp_cmd_status->status,
1381                 sbp_cmd_status->sfmt,
1382                 sbp_cmd_status->valid,
1383                 sbp_cmd_status->s_key,
1384                 sbp_cmd_status->s_code,
1385                 sbp_cmd_status->s_qlfr,
1386                 sbp_status->len
1387         );
1388 END_DEBUG
1389
1390         switch (sbp_cmd_status->status) {
1391         case SCSI_STATUS_CHECK_COND:
1392         case SCSI_STATUS_BUSY:
1393         case SCSI_STATUS_CMD_TERMINATED:
1394                 if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1395                         sense->error_code = SSD_CURRENT_ERROR;
1396                 }else{
1397                         sense->error_code = SSD_DEFERRED_ERROR;
1398                 }
1399                 if(sbp_cmd_status->valid)
1400                         sense->error_code |= SSD_ERRCODE_VALID;
1401                 sense->flags = sbp_cmd_status->s_key;
1402                 if(sbp_cmd_status->mark)
1403                         sense->flags |= SSD_FILEMARK;
1404                 if(sbp_cmd_status->eom)
1405                         sense->flags |= SSD_EOM;
1406                 if(sbp_cmd_status->ill_len)
1407                         sense->flags |= SSD_ILI;
1408                 sense->info[0] = ntohl(sbp_cmd_status->info) & 0xff;
1409                 sense->info[1] =(ntohl(sbp_cmd_status->info) >> 8) & 0xff;
1410                 sense->info[2] =(ntohl(sbp_cmd_status->info) >> 16) & 0xff;
1411                 sense->info[3] =(ntohl(sbp_cmd_status->info) >> 24) & 0xff;
1412                 if (sbp_status->len <= 1)
1413                         /* XXX not scsi status. shouldn't be happened */ 
1414                         sense->extra_len = 0;
1415                 else if (sbp_status->len <= 4)
1416                         /* add_sense_code(_qual), info, cmd_spec_info */
1417                         sense->extra_len = 6;
1418                 else
1419                         /* fru, sense_key_spec */
1420                         sense->extra_len = 10;
1421                 sense->cmd_spec_info[0] = ntohl(sbp_cmd_status->cdb) & 0xff;
1422                 sense->cmd_spec_info[1] = (ntohl(sbp_cmd_status->cdb) >> 8) & 0xff;
1423                 sense->cmd_spec_info[2] = (ntohl(sbp_cmd_status->cdb) >> 16) & 0xff;
1424                 sense->cmd_spec_info[3] = (ntohl(sbp_cmd_status->cdb) >> 24) & 0xff;
1425                 sense->add_sense_code = sbp_cmd_status->s_code;
1426                 sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1427                 sense->fru = sbp_cmd_status->fru;
1428                 sense->sense_key_spec[0] = ntohl(sbp_cmd_status->s_keydep) & 0xff;
1429                 sense->sense_key_spec[1] = (ntohl(sbp_cmd_status->s_keydep) >>8) & 0xff;
1430                 sense->sense_key_spec[2] = (ntohl(sbp_cmd_status->s_keydep) >>16) & 0xff;
1431
1432                 ocb->ccb->csio.scsi_status = sbp_cmd_status->status;;
1433                 ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1434                                                         | CAM_AUTOSNS_VALID;
1435 /*
1436 {
1437                 u_int8_t j, *tmp;
1438                 tmp = sense;
1439                 for( j = 0 ; j < 32 ; j+=8){
1440                         printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n", 
1441                                 tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1442                                 tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1443                 }
1444
1445 }
1446 */
1447                 break;
1448         default:
1449                 sbp_show_sdev_info(ocb->sdev, 2);
1450                 printf("sbp_scsi_status: unknown scsi status 0x%x\n",
1451                                                 sbp_cmd_status->status);
1452         }
1453 }
1454
1455 static void
1456 sbp_fix_inq_data(struct sbp_ocb *ocb)
1457 {
1458         union ccb *ccb;
1459         struct sbp_dev *sdev;
1460         struct scsi_inquiry_data *inq;
1461
1462         ccb = ocb->ccb;
1463         sdev = ocb->sdev;
1464
1465         if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1466                 return;
1467 SBP_DEBUG(1)
1468         sbp_show_sdev_info(sdev, 2);
1469         printf("sbp_fix_inq_data\n");
1470 END_DEBUG
1471         inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1472         switch (SID_TYPE(inq)) {
1473         case T_DIRECT:
1474                 /* 
1475                  * XXX Convert Direct Access device to RBC.
1476                  * I've never seen FireWire DA devices which support READ_6.
1477                  */
1478 #if 1
1479                 if (SID_TYPE(inq) == T_DIRECT)
1480                         inq->device |= T_RBC; /*  T_DIRECT == 0 */
1481 #endif
1482                 /* fall through */
1483         case T_RBC:
1484                 /* enable tag queuing */
1485                 if (sbp_tags)
1486                         inq->flags |= SID_CmdQue;
1487                 else
1488                         inq->flags &= ~SID_CmdQue;
1489                 /*
1490                  * Override vendor/product/revision information.
1491                  * Some devices sometimes return strange strings.
1492                  */
1493 #if 1
1494                 bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1495                 bcopy(sdev->product, inq->product, sizeof(inq->product));
1496                 bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1497 #endif
1498                 break;
1499         }
1500 }
1501
1502 static void
1503 sbp_recv1(struct fw_xfer *xfer)
1504 {
1505         struct fw_pkt *rfp;
1506 #if NEED_RESPONSE
1507         struct fw_pkt *sfp;
1508 #endif
1509         struct sbp_softc *sbp;
1510         struct sbp_dev *sdev;
1511         struct sbp_ocb *ocb;
1512         struct sbp_login_res *login_res = NULL;
1513         struct sbp_status *sbp_status;
1514         struct sbp_target *target;
1515         int     orb_fun, status_valid0, status_valid, t, l, reset_agent = 0;
1516         u_int32_t addr;
1517 /*
1518         u_int32_t *ld;
1519         ld = xfer->recv.buf;
1520 printf("sbp %x %d %d %08x %08x %08x %08x\n",
1521                         xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1522 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1523 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1524 */
1525
1526         sbp = (struct sbp_softc *)xfer->sc;
1527         if(xfer->resp != 0){
1528                 printf("sbp_recv: xfer->resp != 0\n");
1529                 goto done0;
1530         }
1531         if(xfer->recv.buf == NULL){
1532                 printf("sbp_recv: xfer->recv.buf == NULL\n");
1533                 goto done0;
1534         }
1535         sbp = (struct sbp_softc *)xfer->sc;
1536         rfp = (struct fw_pkt *)xfer->recv.buf;
1537         if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1538                 printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1539                 goto done0;
1540         }
1541         sbp_status = (struct sbp_status *)rfp->mode.wreqb.payload;
1542         addr = rfp->mode.wreqb.dest_lo;
1543 SBP_DEBUG(2)
1544         printf("received address 0x%x\n", addr);
1545 END_DEBUG
1546         t = SBP_ADDR2TRG(addr);
1547         if (t >= SBP_NUM_TARGETS) {
1548                 device_printf(sbp->fd.dev,
1549                         "sbp_recv1: invalid target %d\n", t);
1550                 goto done0;
1551         }
1552         target = &sbp->targets[t];
1553         l = SBP_ADDR2LUN(addr);
1554         if (l >= target->num_lun) {
1555                 device_printf(sbp->fd.dev,
1556                         "sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1557                 goto done0;
1558         }
1559         sdev = &target->luns[l];
1560
1561         ocb = NULL;
1562         switch (sbp_status->src) {
1563         case 0:
1564         case 1:
1565                 /* check mgm_ocb_cur first */
1566                 ocb  = target->mgm_ocb_cur;
1567                 if (ocb != NULL) {
1568                         if (OCB_MATCH(ocb, sbp_status)) {
1569                                 callout_stop(&target->mgm_ocb_timeout);
1570                                 target->mgm_ocb_cur = NULL;
1571                                 break;
1572                         }
1573                 }
1574                 ocb = sbp_dequeue_ocb(sdev, sbp_status);
1575                 if (ocb == NULL) {
1576                         sbp_show_sdev_info(sdev, 2);
1577 #if __FreeBSD_version >= 500000
1578                         printf("No ocb(%x) on the queue\n",
1579 #else
1580                         printf("No ocb(%lx) on the queue\n",
1581 #endif
1582                                         ntohl(sbp_status->orb_lo));
1583                 }
1584                 break;
1585         case 2:
1586                 /* unsolicit */
1587                 sbp_show_sdev_info(sdev, 2);
1588                 printf("unsolicit status received\n");
1589                 break;
1590         default:
1591                 sbp_show_sdev_info(sdev, 2);
1592                 printf("unknown sbp_status->src\n");
1593         }
1594
1595         status_valid0 = (sbp_status->src < 2
1596                         && sbp_status->resp == ORB_RES_CMPL
1597                         && sbp_status->dead == 0);
1598         status_valid = (status_valid0 && sbp_status->status == 0);
1599
1600         if (!status_valid0 || debug > 1){
1601                 int status;
1602 SBP_DEBUG(0)
1603                 sbp_show_sdev_info(sdev, 2);
1604                 printf("ORB status src:%x resp:%x dead:%x"
1605 #if __FreeBSD_version >= 500000
1606                                 " len:%x stat:%x orb:%x%08x\n",
1607 #else
1608                                 " len:%x stat:%x orb:%x%08lx\n",
1609 #endif
1610                         sbp_status->src, sbp_status->resp, sbp_status->dead,
1611                         sbp_status->len, sbp_status->status,
1612                         ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1613 END_DEBUG
1614                 sbp_show_sdev_info(sdev, 2);
1615                 status = sbp_status->status;
1616                 switch(sbp_status->resp) {
1617                 case 0:
1618                         if (status > MAX_ORB_STATUS0)
1619                                 printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1620                         else
1621                                 printf("%s\n", orb_status0[status]);
1622                         break;
1623                 case 1:
1624                         printf("Obj: %s, Error: %s\n",
1625                                 orb_status1_object[(status>>6) & 3],
1626                                 orb_status1_serial_bus_error[status & 0xf]);
1627                         break;
1628                 case 2:
1629                         printf("Illegal request\n");
1630                         break;
1631                 case 3:
1632                         printf("Vendor dependent\n");
1633                         break;
1634                 default:
1635                         printf("unknown respose code %d\n", sbp_status->resp);
1636                 }
1637         }
1638
1639         /* we have to reset the fetch agent if it's dead */
1640         if (sbp_status->dead) {
1641                 if (sdev->path) {
1642                         xpt_freeze_devq(sdev->path, 1);
1643                         sdev->freeze ++;
1644                 }
1645                 reset_agent = 1;
1646         }
1647
1648         if (ocb == NULL)
1649                 goto done;
1650
1651         switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1652         case ORB_FMT_NOP:
1653                 break;
1654         case ORB_FMT_VED:
1655                 break;
1656         case ORB_FMT_STD:
1657                 switch(ocb->flags) {
1658                 case OCB_ACT_MGM:
1659                         orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1660                         switch(orb_fun) {
1661                         case ORB_FUN_LGI:
1662                                 fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD);
1663                                 login_res = sdev->login;
1664                                 login_res->len = ntohs(login_res->len);
1665                                 login_res->id = ntohs(login_res->id);
1666                                 login_res->cmd_hi = ntohs(login_res->cmd_hi);
1667                                 login_res->cmd_lo = ntohl(login_res->cmd_lo);
1668                                 if (status_valid) {
1669 SBP_DEBUG(0)
1670 sbp_show_sdev_info(sdev, 2);
1671 printf("login: len %d, ID %d, cmd %08x%08x, recon_hold %d\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo, ntohs(login_res->recon_hold));
1672 END_DEBUG
1673                                         sbp_busy_timeout(sdev);
1674                                 } else {
1675                                         /* forgot logout? */
1676                                         sbp_show_sdev_info(sdev, 2);
1677                                         printf("login failed\n");
1678                                         sdev->status = SBP_DEV_RESET;
1679                                 }
1680                                 break;
1681                         case ORB_FUN_RCN:
1682                                 login_res = sdev->login;
1683                                 if (status_valid) {
1684 SBP_DEBUG(0)
1685 sbp_show_sdev_info(sdev, 2);
1686 printf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo);
1687 END_DEBUG
1688 #if 1
1689                                         if (sdev->status == SBP_DEV_ATTACHED)
1690                                                 sbp_scan_dev(sdev);
1691                                         else
1692                                                 sbp_agent_reset(sdev);
1693 #else
1694                                         sdev->status = SBP_DEV_ATTACHED;
1695                                         sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL);
1696 #endif
1697                                 } else {
1698                                         /* reconnection hold time exceed? */
1699 SBP_DEBUG(0)
1700                                         sbp_show_sdev_info(sdev, 2);
1701                                         printf("reconnect failed\n");
1702 END_DEBUG
1703                                         sbp_login(sdev);
1704                                 }
1705                                 break;
1706                         case ORB_FUN_LGO:
1707                                 sdev->status = SBP_DEV_RESET;
1708                                 break;
1709                         case ORB_FUN_RST:
1710                                 sbp_busy_timeout(sdev);
1711                                 break;
1712                         case ORB_FUN_LUR:
1713                         case ORB_FUN_ATA:
1714                         case ORB_FUN_ATS:
1715                                 sbp_agent_reset(sdev);
1716                                 break;
1717                         default:
1718                                 sbp_show_sdev_info(sdev, 2);
1719                                 printf("unknown function %d\n", orb_fun);
1720                                 break;
1721                         }
1722                         sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
1723                         break;
1724                 case OCB_ACT_CMD:
1725                         sdev->timeout = 0;
1726                         if(ocb->ccb != NULL){
1727                                 union ccb *ccb;
1728 /*
1729                                 u_int32_t *ld;
1730                                 ld = ocb->ccb->csio.data_ptr;
1731                                 if(ld != NULL && ocb->ccb->csio.dxfer_len != 0)
1732                                         printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]);
1733                                 else
1734                                         printf("ptr NULL\n");
1735 printf("len %d\n", sbp_status->len);
1736 */
1737                                 ccb = ocb->ccb;
1738                                 if(sbp_status->len > 1){
1739                                         sbp_scsi_status(sbp_status, ocb);
1740                                 }else{
1741                                         if(sbp_status->resp != ORB_RES_CMPL){
1742                                                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1743                                         }else{
1744                                                 ccb->ccb_h.status = CAM_REQ_CMP;
1745                                         }
1746                                 }
1747                                 /* fix up inq data */
1748                                 if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1749                                         sbp_fix_inq_data(ocb);
1750                                 xpt_done(ccb);
1751                         }
1752                         break;
1753                 default:
1754                         break;
1755                 }
1756         }
1757
1758         sbp_free_ocb(sdev, ocb);
1759 done:
1760         if (reset_agent)
1761                 sbp_agent_reset(sdev);
1762
1763 done0:
1764 /* The received packet is usually small enough to be stored within
1765  * the buffer. In that case, the controller return ack_complete and
1766  * no respose is necessary.
1767  *
1768  * XXX fwohci.c and firewire.c should inform event_code such as 
1769  * ack_complete or ack_pending to upper driver.
1770  */
1771 #if NEED_RESPONSE
1772         xfer->send.off = 0;
1773         sfp = (struct fw_pkt *)xfer->send.buf;
1774         sfp->mode.wres.dst = rfp->mode.wreqb.src;
1775         xfer->dst = sfp->mode.wres.dst;
1776         xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1777         xfer->act.hand = sbp_loginres_callback;
1778         xfer->retry_req = fw_asybusy;
1779
1780         sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1781         sfp->mode.wres.tcode = FWTCODE_WRES;
1782         sfp->mode.wres.rtcode = 0;
1783         sfp->mode.wres.pri = 0;
1784
1785         fw_asyreq(xfer->fc, -1, xfer);
1786 #else
1787         /* recycle */
1788         xfer->recv.len = SBP_RECV_LEN;
1789         STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1790 #endif
1791
1792         return;
1793
1794 }
1795
1796 static void
1797 sbp_recv(struct fw_xfer *xfer)
1798 {
1799         int s;
1800
1801         s = splcam();
1802         sbp_recv1(xfer);
1803         splx(s);
1804 }
1805 /*
1806  * sbp_attach()
1807  */
1808 static int
1809 sbp_attach(device_t dev)
1810 {
1811         struct sbp_softc *sbp;
1812         struct cam_devq *devq;
1813         struct fw_xfer *xfer;
1814         int i, s, error;
1815
1816 SBP_DEBUG(0)
1817         printf("sbp_attach (cold=%d)\n", cold);
1818 END_DEBUG
1819
1820         if (cold)
1821                 sbp_cold ++;
1822         sbp = ((struct sbp_softc *)device_get_softc(dev));
1823         bzero(sbp, sizeof(struct sbp_softc));
1824         sbp->fd.dev = dev;
1825         sbp->fd.fc = device_get_ivars(dev);
1826         error = bus_dma_tag_create(/*parent*/sbp->fd.fc->dmat,
1827                                 /* XXX shoud be 4 for sane backend? */
1828                                 /*alignment*/1,
1829                                 /*boundary*/0,
1830                                 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1831                                 /*highaddr*/BUS_SPACE_MAXADDR,
1832                                 /*filter*/NULL, /*filterarg*/NULL,
1833                                 /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1834                                 /*maxsegsz*/SBP_SEG_MAX,
1835                                 /*flags*/BUS_DMA_ALLOCNOW,
1836                                 &sbp->dmat);
1837         if (error != 0) {
1838                 printf("sbp_attach: Could not allocate DMA tag "
1839                         "- error %d\n", error);
1840                         return (ENOMEM);
1841         }
1842
1843         devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1844         if (devq == NULL)
1845                 return (ENXIO);
1846
1847         for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1848                 sbp->targets[i].fwdev = NULL;
1849                 sbp->targets[i].luns = NULL;
1850         }
1851
1852         sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1853                                  device_get_unit(dev),
1854                                  /*untagged*/ 1,
1855                                  /*tagged*/ SBP_QUEUE_LEN,
1856                                  devq);
1857
1858         if (sbp->sim == NULL) {
1859                 cam_simq_free(devq);
1860                 return (ENXIO);
1861         }
1862
1863
1864         if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS)
1865                 goto fail;
1866
1867         if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim),
1868                         CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1869                 goto fail;
1870
1871         sbp->fwb.start_hi = SBP_BIND_HI;
1872         sbp->fwb.start_lo = SBP_DEV2ADDR(device_get_unit(sbp->fd.dev), 0, 0);
1873         /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1874         sbp->fwb.addrlen = 0xffff;
1875         sbp->fwb.act_type = FWACT_XFER;
1876         /* pre-allocate xfer */
1877         STAILQ_INIT(&sbp->fwb.xferlist);
1878         for (i = 0; i < SBP_NUM_OCB/2; i ++) {
1879                 xfer = fw_xfer_alloc_buf(M_SBP,
1880 #if NEED_RESPONSE
1881                         /* send */12,
1882 #else
1883                         /* send */0,
1884 #endif
1885                         /* recv */SBP_RECV_LEN);
1886                 xfer->act.hand = sbp_recv;
1887 #if NEED_RESPONSE
1888                 xfer->fc = sbp->fd.fc;
1889 #endif
1890                 xfer->sc = (caddr_t)sbp;
1891                 STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1892         }
1893         fw_bindadd(sbp->fd.fc, &sbp->fwb);
1894
1895         sbp->fd.post_busreset = sbp_post_busreset;
1896         sbp->fd.post_explore = sbp_post_explore;
1897
1898         if (sbp->fd.fc->status != -1) {
1899                 s = splfw();
1900                 sbp_post_explore((void *)sbp);
1901                 splx(s);
1902         }
1903
1904         return (0);
1905 fail:
1906         cam_sim_free(sbp->sim, /*free_devq*/TRUE);
1907         return (ENXIO);
1908 }
1909
1910 static int
1911 sbp_logout_all(struct sbp_softc *sbp)
1912 {
1913         struct sbp_target *target;
1914         struct sbp_dev *sdev;
1915         int i, j;
1916
1917 SBP_DEBUG(0)
1918         printf("sbp_logout_all\n");
1919 END_DEBUG
1920         for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
1921                 target = &sbp->targets[i];
1922                 if (target->luns == NULL)
1923                         continue;
1924                 for (j = 0; j < target->num_lun; j++) {
1925                         sdev = &target->luns[j];
1926                         callout_stop(&sdev->login_callout);
1927                         if (sdev->status >= SBP_DEV_TOATTACH &&
1928                                         sdev->status <= SBP_DEV_ATTACHED)
1929                                 sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
1930                 }
1931         }
1932
1933         return 0;
1934 }
1935
1936 static int
1937 sbp_shutdown(device_t dev)
1938 {
1939         struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1940
1941         sbp_logout_all(sbp);
1942         return (0);
1943 }
1944
1945 static int
1946 sbp_detach(device_t dev)
1947 {
1948         struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1949         struct firewire_comm *fc = sbp->fd.fc;
1950         struct sbp_target *target;
1951         struct sbp_dev *sdev;
1952         struct fw_xfer *xfer, *next;
1953         int i, j;
1954
1955 SBP_DEBUG(0)
1956         printf("sbp_detach\n");
1957 END_DEBUG
1958
1959         for (i = 0; i < SBP_NUM_TARGETS; i ++) 
1960                 sbp_cam_detach_target(&sbp->targets[i]);
1961         xpt_free_path(sbp->path);
1962         xpt_bus_deregister(cam_sim_path(sbp->sim));
1963
1964         sbp_logout_all(sbp);
1965
1966         /* XXX wait for logout completion */
1967         tsleep(&i, FWPRI, "sbpdtc", hz/2);
1968
1969         for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
1970                 target = &sbp->targets[i];
1971                 if (target->luns == NULL)
1972                         continue;
1973                 callout_stop(&target->mgm_ocb_timeout);
1974                 for (j = 0; j < target->num_lun; j++) {
1975                         sdev = &target->luns[j];
1976                         if (sdev->status != SBP_DEV_DEAD) {
1977                                 for (i = 0; i < SBP_QUEUE_LEN; i++)
1978                                         bus_dmamap_destroy(sbp->dmat,
1979                                                 sdev->ocb[i].dmamap);
1980                                 fwdma_free(sbp->fd.fc, &sdev->dma);
1981                         }
1982                 }
1983                 for (xfer = STAILQ_FIRST(&target->xferlist);
1984                                 xfer != NULL; xfer = next) {
1985                         next = STAILQ_NEXT(xfer, link);
1986                         fw_xfer_free(xfer);
1987                 }
1988                 free(target->luns, M_SBP);
1989         }
1990
1991         for (xfer = STAILQ_FIRST(&sbp->fwb.xferlist);
1992                                 xfer != NULL; xfer = next) {
1993                 next = STAILQ_NEXT(xfer, link);
1994                 fw_xfer_free(xfer);
1995         }
1996         STAILQ_INIT(&sbp->fwb.xferlist);
1997         fw_bindremove(fc, &sbp->fwb);
1998
1999         bus_dma_tag_destroy(sbp->dmat);
2000
2001         return (0);
2002 }
2003
2004 static void
2005 sbp_cam_detach_target(struct sbp_target *target)
2006 {
2007         struct sbp_dev *sdev;
2008         int i;
2009
2010         if (target->luns != NULL) {
2011 SBP_DEBUG(0)
2012                 printf("sbp_detach_target %d\n", target->target_id);
2013 END_DEBUG
2014                 callout_stop(&target->scan_callout);
2015                 for (i = 0; i < target->num_lun; i++) {
2016                         sdev = &target->luns[i];
2017                         if (sdev->status == SBP_DEV_DEAD)
2018                                 continue;
2019                         if (sdev->status == SBP_DEV_RESET)
2020                                 continue;
2021                         if (sdev->path) {
2022                                 xpt_release_devq(sdev->path,
2023                                                  sdev->freeze, TRUE);
2024                                 sdev->freeze = 0;
2025                                 xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
2026                                 xpt_free_path(sdev->path);
2027                                 sdev->path = NULL;
2028                         }
2029                         sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
2030                 }
2031         }
2032 }
2033
2034 static void
2035 sbp_target_reset(struct sbp_dev *sdev, int method)
2036 {
2037         int i;
2038         struct sbp_target *target = sdev->target;
2039         struct sbp_dev *tsdev;
2040
2041         for (i = 0; i < target->num_lun; i++) {
2042                 tsdev = &target->luns[i];
2043                 if (tsdev->status == SBP_DEV_DEAD)
2044                         continue;
2045                 if (tsdev->status == SBP_DEV_RESET)
2046                         continue;
2047                 xpt_freeze_devq(tsdev->path, 1);
2048                 tsdev->freeze ++;
2049                 sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT);
2050                 if (method == 2)
2051                         tsdev->status = SBP_DEV_LOGIN;
2052         }
2053         switch(method) {
2054         case 1:
2055                 printf("target reset\n");
2056                 sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2057                 break;
2058         case 2:
2059                 printf("reset start\n");
2060                 sbp_reset_start(sdev);
2061                 break;
2062         }
2063                         
2064 }
2065
2066 static void
2067 sbp_mgm_timeout(void *arg)
2068 {
2069         struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2070         struct sbp_dev *sdev = ocb->sdev;
2071         struct sbp_target *target = sdev->target;
2072
2073         sbp_show_sdev_info(sdev, 2);
2074         printf("management ORB timeout\n");
2075         target->mgm_ocb_cur = NULL;
2076         sbp_free_ocb(sdev, ocb);
2077 #if 0
2078         /* XXX */
2079         sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2080 #endif
2081 #if 0
2082         sbp_reset_start(sdev);
2083 #endif
2084 }
2085
2086 static void
2087 sbp_timeout(void *arg)
2088 {
2089         struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2090         struct sbp_dev *sdev = ocb->sdev;
2091
2092         sbp_show_sdev_info(sdev, 2);
2093         printf("request timeout ... ");
2094
2095         sdev->timeout ++;
2096         switch(sdev->timeout) {
2097         case 1:
2098                 printf("agent reset\n");
2099                 xpt_freeze_devq(sdev->path, 1);
2100                 sdev->freeze ++;
2101                 sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2102                 sbp_agent_reset(sdev);
2103                 break;
2104         case 2:
2105         case 3:
2106                 sbp_target_reset(sdev, sdev->timeout - 1);
2107                 break;
2108 #if 0
2109         default:
2110                 /* XXX give up */
2111                 sbp_cam_detach_target(target);
2112                 if (target->luns != NULL)
2113                         free(target->luns, M_SBP);
2114                 target->num_lun = 0;;
2115                 target->luns = NULL;
2116                 target->fwdev = NULL;
2117 #endif
2118         }
2119 }
2120
2121 static void
2122 sbp_action1(struct cam_sim *sim, union ccb *ccb)
2123 {
2124
2125         struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2126         struct sbp_target *target = NULL;
2127         struct sbp_dev *sdev = NULL;
2128
2129         /* target:lun -> sdev mapping */
2130         if (sbp != NULL
2131                         && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2132                         && ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2133                 target = &sbp->targets[ccb->ccb_h.target_id];
2134                 if (target->fwdev != NULL
2135                                 && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2136                                 && ccb->ccb_h.target_lun < target->num_lun) {
2137                         sdev = &target->luns[ccb->ccb_h.target_lun];
2138                         if (sdev->status != SBP_DEV_ATTACHED &&
2139                                 sdev->status != SBP_DEV_PROBE)
2140                                 sdev = NULL;
2141                 }
2142         }
2143
2144 SBP_DEBUG(1)
2145         if (sdev == NULL)
2146                 printf("invalid target %d lun %d\n",
2147                         ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2148 END_DEBUG
2149
2150         switch (ccb->ccb_h.func_code) {
2151         case XPT_SCSI_IO:
2152         case XPT_RESET_DEV:
2153         case XPT_GET_TRAN_SETTINGS:
2154         case XPT_SET_TRAN_SETTINGS:
2155         case XPT_CALC_GEOMETRY:
2156                 if (sdev == NULL) {
2157 SBP_DEBUG(1)
2158                         printf("%s:%d:%d:func_code 0x%04x: "
2159                                 "Invalid target (target needed)\n",
2160                                 device_get_nameunit(sbp->fd.dev),
2161                                 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2162                                 ccb->ccb_h.func_code);
2163 END_DEBUG
2164
2165                         ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2166                         xpt_done(ccb);
2167                         return;
2168                 }
2169                 break;
2170         case XPT_PATH_INQ:
2171         case XPT_NOOP:
2172                 /* The opcodes sometimes aimed at a target (sc is valid),
2173                  * sometimes aimed at the SIM (sc is invalid and target is
2174                  * CAM_TARGET_WILDCARD)
2175                  */
2176                 if (sbp == NULL && 
2177                         ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2178 SBP_DEBUG(0)
2179                         printf("%s:%d:%d func_code 0x%04x: "
2180                                 "Invalid target (no wildcard)\n",
2181                                 device_get_nameunit(sbp->fd.dev),
2182                                 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2183                                 ccb->ccb_h.func_code);
2184 END_DEBUG
2185                         ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2186                         xpt_done(ccb);
2187                         return;
2188                 }
2189                 break;
2190         default:
2191                 /* XXX Hm, we should check the input parameters */
2192                 break;
2193         }
2194
2195         switch (ccb->ccb_h.func_code) {
2196         case XPT_SCSI_IO:
2197         {
2198                 struct ccb_scsiio *csio;
2199                 struct sbp_ocb *ocb;
2200                 int speed;
2201                 void *cdb;
2202
2203                 csio = &ccb->csio;
2204
2205 SBP_DEBUG(1)
2206                 printf("%s:%d:%d XPT_SCSI_IO: "
2207                         "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2208                         ", flags: 0x%02x, "
2209                         "%db cmd/%db data/%db sense\n",
2210                         device_get_nameunit(sbp->fd.dev),
2211                         ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2212                         csio->cdb_io.cdb_bytes[0],
2213                         csio->cdb_io.cdb_bytes[1],
2214                         csio->cdb_io.cdb_bytes[2],
2215                         csio->cdb_io.cdb_bytes[3],
2216                         csio->cdb_io.cdb_bytes[4],
2217                         csio->cdb_io.cdb_bytes[5],
2218                         csio->cdb_io.cdb_bytes[6],
2219                         csio->cdb_io.cdb_bytes[7],
2220                         csio->cdb_io.cdb_bytes[8],
2221                         csio->cdb_io.cdb_bytes[9],
2222                         ccb->ccb_h.flags & CAM_DIR_MASK,
2223                         csio->cdb_len, csio->dxfer_len,
2224                         csio->sense_len);
2225 END_DEBUG
2226                 if(sdev == NULL){
2227                         ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2228                         xpt_done(ccb);
2229                         return;
2230                 }
2231 #if 0
2232                 /* if we are in probe stage, pass only probe commands */
2233                 if (sdev->status == SBP_DEV_PROBE) {
2234                         char *name;
2235                         name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2236                         printf("probe stage, periph name: %s\n", name);
2237                         if (strcmp(name, "probe") != 0) {
2238                                 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2239                                 xpt_done(ccb);
2240                                 return;
2241                         }
2242                 }
2243 #endif
2244                 if ((ocb = sbp_get_ocb(sdev)) == NULL)
2245                         return;
2246
2247                 ocb->flags = OCB_ACT_CMD;
2248                 ocb->sdev = sdev;
2249                 ocb->ccb = ccb;
2250                 ccb->ccb_h.ccb_sdev_ptr = sdev;
2251                 ocb->orb[0] = htonl(1 << 31);
2252                 ocb->orb[1] = 0;
2253                 ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2254                 ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2255                 speed = min(target->fwdev->speed, max_speed);
2256                 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2257                                                 | ORB_CMD_MAXP(speed + 7));
2258                 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
2259                         ocb->orb[4] |= htonl(ORB_CMD_IN);
2260                 }
2261
2262                 if (csio->ccb_h.flags & CAM_SCATTER_VALID)
2263                         printf("sbp: CAM_SCATTER_VALID\n");
2264                 if (csio->ccb_h.flags & CAM_DATA_PHYS)
2265                         printf("sbp: CAM_DATA_PHYS\n");
2266
2267                 if (csio->ccb_h.flags & CAM_CDB_POINTER)
2268                         cdb = (void *)csio->cdb_io.cdb_ptr;
2269                 else
2270                         cdb = (void *)&csio->cdb_io.cdb_bytes;
2271                 bcopy(cdb,
2272                         (void *)(uintptr_t)(volatile void *)&ocb->orb[5],
2273                                 csio->cdb_len);
2274 /*
2275 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2276 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2277 */
2278                 if (ccb->csio.dxfer_len > 0) {
2279                         int s, error;
2280
2281                         s = splsoftvm();
2282                         error = bus_dmamap_load(/*dma tag*/sbp->dmat,
2283                                         /*dma map*/ocb->dmamap,
2284                                         ccb->csio.data_ptr,
2285                                         ccb->csio.dxfer_len,
2286                                         sbp_execute_ocb,
2287                                         ocb,
2288                                         /*flags*/0);
2289                         splx(s);
2290                         if (error)
2291                                 printf("sbp: bus_dmamap_load error %d\n", error);
2292                 } else
2293                         sbp_execute_ocb(ocb, NULL, 0, 0);
2294                 break;
2295         }
2296         case XPT_CALC_GEOMETRY:
2297         {
2298                 struct ccb_calc_geometry *ccg;
2299                 u_int32_t size_mb;
2300                 u_int32_t secs_per_cylinder;
2301                 int extended = 1;
2302                 ccg = &ccb->ccg;
2303
2304                 if (ccg->block_size == 0) {
2305                         printf("sbp_action1: block_size is 0.\n");
2306                         ccb->ccb_h.status = CAM_REQ_INVALID;
2307                         xpt_done(ccb);
2308                         break;
2309                 }
2310 SBP_DEBUG(1)
2311                 printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2312 #if __FreeBSD_version >= 500000
2313                         "Volume size = %jd\n",
2314 #else
2315                         "Volume size = %d\n",
2316 #endif
2317                         device_get_nameunit(sbp->fd.dev),
2318                         cam_sim_path(sbp->sim),
2319                         ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2320 #if __FreeBSD_version >= 500000
2321                         (uintmax_t)
2322 #endif
2323                                 ccg->volume_size);
2324 END_DEBUG
2325
2326                 size_mb = ccg->volume_size
2327                         / ((1024L * 1024L) / ccg->block_size);
2328
2329                 if (size_mb >= 1024 && extended) {
2330                         ccg->heads = 255;
2331                         ccg->secs_per_track = 63;
2332                 } else {
2333                         ccg->heads = 64;
2334                         ccg->secs_per_track = 32;
2335                 }
2336                 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2337                 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2338                 ccb->ccb_h.status = CAM_REQ_CMP;
2339                 xpt_done(ccb);
2340                 break;
2341         }
2342         case XPT_RESET_BUS:             /* Reset the specified SCSI bus */
2343         {
2344
2345 SBP_DEBUG(1)
2346                 printf("%s:%d:XPT_RESET_BUS: \n",
2347                         device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2348 END_DEBUG
2349
2350                 ccb->ccb_h.status = CAM_REQ_INVALID;
2351                 xpt_done(ccb);
2352                 break;
2353         }
2354         case XPT_PATH_INQ:              /* Path routing inquiry */
2355         {
2356                 struct ccb_pathinq *cpi = &ccb->cpi;
2357                 
2358 SBP_DEBUG(1)
2359                 printf("%s:%d:%d XPT_PATH_INQ:.\n",
2360                         device_get_nameunit(sbp->fd.dev),
2361                         ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2362 END_DEBUG
2363                 cpi->version_num = 1; /* XXX??? */
2364                 cpi->hba_inquiry = PI_TAG_ABLE;
2365                 cpi->target_sprt = 0;
2366                 cpi->hba_misc = PIM_NOBUSRESET;
2367                 cpi->hba_eng_cnt = 0;
2368                 cpi->max_target = SBP_NUM_TARGETS - 1;
2369                 cpi->max_lun = SBP_NUM_LUNS - 1;
2370                 cpi->initiator_id = SBP_INITIATOR;
2371                 cpi->bus_id = sim->bus_id;
2372                 cpi->base_transfer_speed = 400 * 1000 / 8;
2373                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2374                 strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2375                 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2376                 cpi->unit_number = sim->unit_number;
2377
2378                 cpi->ccb_h.status = CAM_REQ_CMP;
2379                 xpt_done(ccb);
2380                 break;
2381         }
2382         case XPT_GET_TRAN_SETTINGS:
2383         {
2384                 struct ccb_trans_settings *cts = &ccb->cts;
2385 SBP_DEBUG(1)
2386                 printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2387                         device_get_nameunit(sbp->fd.dev),
2388                         ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2389 END_DEBUG
2390                 /* Enable disconnect and tagged queuing */
2391                 cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2392                 cts->flags = CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB;
2393
2394                 cts->ccb_h.status = CAM_REQ_CMP;
2395                 xpt_done(ccb);
2396                 break;
2397         }
2398         case XPT_ABORT:
2399                 ccb->ccb_h.status = CAM_UA_ABORT;
2400                 xpt_done(ccb);
2401                 break;
2402         case XPT_SET_TRAN_SETTINGS:
2403                 /* XXX */
2404         default:
2405                 ccb->ccb_h.status = CAM_REQ_INVALID;
2406                 xpt_done(ccb);
2407                 break;
2408         }
2409         return;
2410 }
2411
2412 static void
2413 sbp_action(struct cam_sim *sim, union ccb *ccb)
2414 {
2415         int s;
2416
2417         s = splfw();
2418         sbp_action1(sim, ccb);
2419         splx(s);
2420 }
2421
2422 static void
2423 sbp_execute_ocb(void *arg,  bus_dma_segment_t *segments, int seg, int error)
2424 {
2425         int i;
2426         struct sbp_ocb *ocb;
2427         struct sbp_ocb *prev;
2428         bus_dma_segment_t *s;
2429
2430         if (error)
2431                 printf("sbp_execute_ocb: error=%d\n", error);
2432
2433         ocb = (struct sbp_ocb *)arg;
2434
2435 SBP_DEBUG(1)
2436         printf("sbp_execute_ocb: seg %d", seg);
2437         for (i = 0; i < seg; i++)
2438 #if __FreeBSD_version >= 500000
2439                 printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2440                                         (uintmax_t)segments[i].ds_len);
2441 #else
2442                 printf(", %x:%d", segments[i].ds_addr, segments[i].ds_len);
2443 #endif
2444         printf("\n");
2445 END_DEBUG
2446
2447         if (seg == 1) {
2448                 /* direct pointer */
2449                 s = &segments[0];
2450                 if (s->ds_len > SBP_SEG_MAX)
2451                         panic("ds_len > SBP_SEG_MAX, fix busdma code");
2452                 ocb->orb[3] = htonl(s->ds_addr);
2453                 ocb->orb[4] |= htonl(s->ds_len);
2454         } else if(seg > 1) {
2455                 /* page table */
2456                 for (i = 0; i < seg; i++) {
2457                         s = &segments[i];
2458 SBP_DEBUG(0)
2459                         /* XXX LSI Logic "< 16 byte" bug might be hit */
2460                         if (s->ds_len < 16)
2461                                 printf("sbp_execute_ocb: warning, "
2462 #if __FreeBSD_version >= 500000
2463                                         "segment length(%zd) is less than 16."
2464 #else
2465                                         "segment length(%d) is less than 16."
2466 #endif
2467                                         "(seg=%d/%d)\n", s->ds_len, i+1, seg);
2468 END_DEBUG
2469                         if (s->ds_len > SBP_SEG_MAX)
2470                                 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2471                         ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2472                         ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2473                 }
2474                 ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2475         }
2476         
2477         if (seg > 0)
2478                 bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2479                         (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2480                         BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2481         prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2482         fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2483         if (prev == NULL)
2484                 sbp_orb_pointer(ocb->sdev, ocb); 
2485 }
2486
2487 static void
2488 sbp_poll(struct cam_sim *sim)
2489 {       
2490         /* should call fwohci_intr? */
2491         return;
2492 }
2493 static struct sbp_ocb *
2494 sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2495 {
2496         struct sbp_ocb *ocb;
2497         struct sbp_ocb *next;
2498         int s = splfw(), order = 0;
2499         int flags;
2500
2501         for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2502                 next = STAILQ_NEXT(ocb, ocb);
2503                 flags = ocb->flags;
2504 SBP_DEBUG(1)
2505                 sbp_show_sdev_info(sdev, 2);
2506 #if __FreeBSD_version >= 500000
2507                 printf("orb: 0x%jx next: 0x%x, flags %x\n",
2508                         (uintmax_t)ocb->bus_addr,
2509 #else
2510                 printf("orb: 0x%x next: 0x%lx, flags %x\n",
2511                         ocb->bus_addr,
2512 #endif
2513                         ntohl(ocb->orb[1]), flags);
2514 END_DEBUG
2515                 if (OCB_MATCH(ocb, sbp_status)) {
2516                         /* found */
2517                         STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2518                         if (ocb->ccb != NULL)
2519                                 untimeout(sbp_timeout, (caddr_t)ocb,
2520                                                 ocb->ccb->ccb_h.timeout_ch);
2521                         if (ntohl(ocb->orb[4]) & 0xffff) {
2522                                 bus_dmamap_sync(sdev->target->sbp->dmat,
2523                                         ocb->dmamap,
2524                                         (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2525                                         BUS_DMASYNC_POSTREAD :
2526                                         BUS_DMASYNC_POSTWRITE);
2527                                 bus_dmamap_unload(sdev->target->sbp->dmat,
2528                                         ocb->dmamap);
2529                         }
2530                         if (next != NULL && sbp_status->src == 1)
2531                                 sbp_orb_pointer(sdev, next); 
2532                         break;
2533                 } else
2534                         order ++;
2535         }
2536         splx(s);
2537 SBP_DEBUG(0)
2538         if (ocb && order > 0) {
2539                 sbp_show_sdev_info(sdev, 2);
2540                 printf("unordered execution order:%d\n", order);
2541         }
2542 END_DEBUG
2543         return (ocb);
2544 }
2545
2546 static struct sbp_ocb *
2547 sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2548 {
2549         int s = splfw();
2550         struct sbp_ocb *prev;
2551
2552 SBP_DEBUG(2)
2553         sbp_show_sdev_info(sdev, 2);
2554 #if __FreeBSD_version >= 500000
2555         printf("sbp_enqueue_ocb orb=0x%jx in physical memory\n", 
2556                 (uintmax_t)ocb->bus_addr);
2557 #else
2558         printf("sbp_enqueue_ocb orb=0x%x in physical memory\n", ocb->bus_addr);
2559 #endif
2560 END_DEBUG
2561         prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2562         STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2563
2564         if (ocb->ccb != NULL)
2565                 ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb,
2566                                         (ocb->ccb->ccb_h.timeout * hz) / 1000);
2567
2568         if (prev != NULL ) {
2569 SBP_DEBUG(1)
2570 #if __FreeBSD_version >= 500000
2571         printf("linking chain 0x%jx -> 0x%jx\n",
2572                 (uintmax_t)prev->bus_addr, (uintmax_t)ocb->bus_addr);
2573 #else
2574         printf("linking chain 0x%x -> 0x%x\n", prev->bus_addr, ocb->bus_addr);
2575 #endif
2576 END_DEBUG
2577                 prev->orb[1] = htonl(ocb->bus_addr);
2578                 prev->orb[0] = 0;
2579         }
2580         splx(s);
2581
2582         return prev;
2583 }
2584
2585 static struct sbp_ocb *
2586 sbp_get_ocb(struct sbp_dev *sdev)
2587 {
2588         struct sbp_ocb *ocb;
2589         int s = splfw();
2590         ocb = STAILQ_FIRST(&sdev->free_ocbs);
2591         if (ocb == NULL) {
2592                 printf("ocb shortage!!!\n");
2593                 return NULL;
2594         }
2595         STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2596         splx(s);
2597         ocb->ccb = NULL;
2598         return (ocb);
2599 }
2600
2601 static void
2602 sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2603 {
2604         ocb->flags = 0;
2605         ocb->ccb = NULL;
2606         STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2607 }
2608
2609 static void
2610 sbp_abort_ocb(struct sbp_ocb *ocb, int status)
2611 {
2612         struct sbp_dev *sdev;
2613
2614         sdev = ocb->sdev;
2615 SBP_DEBUG(0)
2616         sbp_show_sdev_info(sdev, 2);
2617 #if __FreeBSD_version >= 500000
2618         printf("sbp_abort_ocb 0x%jx\n", (uintmax_t)ocb->bus_addr);
2619 #else
2620         printf("sbp_abort_ocb 0x%x\n", ocb->bus_addr);
2621 #endif
2622 END_DEBUG
2623 SBP_DEBUG(1)
2624         if (ocb->ccb != NULL)
2625                 sbp_print_scsi_cmd(ocb);
2626 END_DEBUG
2627         if (ntohl(ocb->orb[4]) & 0xffff) {
2628                 bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
2629                         (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2630                         BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2631                 bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
2632         }
2633         if (ocb->ccb != NULL) {
2634                 untimeout(sbp_timeout, (caddr_t)ocb,
2635                                         ocb->ccb->ccb_h.timeout_ch);
2636                 ocb->ccb->ccb_h.status = status;
2637                 xpt_done(ocb->ccb);
2638         }
2639         sbp_free_ocb(sdev, ocb);
2640 }
2641
2642 static void
2643 sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2644 {
2645         int s;
2646         struct sbp_ocb *ocb, *next;
2647         STAILQ_HEAD(, sbp_ocb) temp;
2648
2649         s = splfw();
2650
2651         bcopy(&sdev->ocbs, &temp, sizeof(temp));
2652         STAILQ_INIT(&sdev->ocbs);
2653         for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2654                 next = STAILQ_NEXT(ocb, ocb);
2655                 sbp_abort_ocb(ocb, status);
2656         }
2657
2658         splx(s);
2659 }
2660
2661 static devclass_t sbp_devclass;
2662
2663 static device_method_t sbp_methods[] = {
2664         /* device interface */
2665         DEVMETHOD(device_identify,      sbp_identify),
2666         DEVMETHOD(device_probe,         sbp_probe),
2667         DEVMETHOD(device_attach,        sbp_attach),
2668         DEVMETHOD(device_detach,        sbp_detach),
2669         DEVMETHOD(device_shutdown,      sbp_shutdown),
2670
2671         { 0, 0 }
2672 };
2673
2674 static driver_t sbp_driver = {
2675         "sbp",
2676         sbp_methods,
2677         sizeof(struct sbp_softc),
2678 };
2679
2680 DECLARE_DUMMY_MODULE(sbp);
2681 DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2682 MODULE_VERSION(sbp, 1);
2683 MODULE_DEPEND(sbp, firewire, 1, 1, 1);
2684 MODULE_DEPEND(sbp, cam, 1, 1, 1);