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