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