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