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