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