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