Deleted Added
full compact
1/*
2 * Copyright (c) 1998,1999,2000,2001 Katsushi Kobayashi and Hidetosh Shimokawa
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the acknowledgement as bellow:
15 *
16 * This product includes software developed by K. Kobayashi and H. Shimokawa
17 *
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 * $FreeBSD: head/sys/dev/firewire/sbp.c 110579 2003-02-09 07:40:27Z simokawa $
33 * $FreeBSD: head/sys/dev/firewire/sbp.c 110798 2003-02-13 13:35:57Z simokawa $
34 *
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/module.h>
40#include <sys/bus.h>
41#include <sys/mbuf.h>
42#include <sys/sysctl.h>
43#include <machine/bus.h>
44#include <sys/malloc.h>
45#include <sys/devicestat.h> /* for struct devstat */
46
47
48#include <cam/cam.h>
49#include <cam/cam_ccb.h>
50#include <cam/cam_sim.h>
51#include <cam/cam_xpt_sim.h>
52#include <cam/cam_debug.h>
53#include <cam/cam_periph.h>
54
55#include <cam/scsi/scsi_all.h>
56#include <cam/scsi/scsi_message.h>
57#include <cam/scsi/scsi_da.h>
58
59#include <sys/kernel.h>
60
61#include <vm/vm.h>
62#include <vm/pmap.h>
63
64#include <dev/firewire/firewire.h>
65#include <dev/firewire/firewirereg.h>
66#include <dev/firewire/iec13213.h>
67
68#define ccb_sdev_ptr spriv_ptr0
69#define ccb_sbp_ptr spriv_ptr1
70
71#define SBP_NUM_TARGETS 8
72#define SBP_NUM_LUNS 8 /* limited by CAM_SCSI2_MAXLUN in cam_xpt.c */
73#define SBP_QUEUE_LEN 4
74#define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS)
75#define SBP_INITIATOR 7
76#define SBP_ESELECT_TIMEOUT 1
77#define SBP_BIND_HI 0x1
78#define SBP_DEV2ADDR(u, t, l) \
79 ((((u) & 0xff) << 16) | (((l) & 0xff) << 8) | (((t) & 0x3f) << 2))
80#define SBP_ADDR2TRG(a) (((a) >> 2) & 0x3f)
81#define SBP_ADDR2LUN(a) (((a) >> 8) & 0xff)
82
83#define ORB_NOTIFY (1 << 31)
84#define ORB_FMT_STD (0 << 29)
85#define ORB_FMT_VED (2 << 29)
86#define ORB_FMT_NOP (3 << 29)
87#define ORB_FMT_MSK (3 << 29)
88#define ORB_EXV (1 << 28)
89/* */
90#define ORB_CMD_IN (1 << 27)
91/* */
92#define ORB_CMD_SPD(x) ((x) << 24)
93#define ORB_CMD_MAXP(x) ((x) << 20)
94#define ORB_RCN_TMO(x) ((x) << 20)
95#define ORB_CMD_PTBL (1 << 19)
96#define ORB_CMD_PSZ(x) ((x) << 16)
97
98#define ORB_FUN_LGI (0 << 16)
99#define ORB_FUN_QLG (1 << 16)
100#define ORB_FUN_RCN (3 << 16)
101#define ORB_FUN_LGO (7 << 16)
102#define ORB_FUN_ATA (0xb << 16)
103#define ORB_FUN_ATS (0xc << 16)
104#define ORB_FUN_LUR (0xe << 16)
105#define ORB_FUN_RST (0xf << 16)
106#define ORB_FUN_MSK (0xf << 16)
107
108static char *orb_fun_name[] = {
109 /* 0 */ "LOGIN",
110 /* 1 */ "QUERY LOGINS",
111 /* 2 */ "Reserved",
112 /* 3 */ "RECONNECT",
113 /* 4 */ "SET PASSWORD",
114 /* 5 */ "Reserved",
115 /* 6 */ "Reserved",
116 /* 7 */ "LOGOUT",
117 /* 8 */ "Reserved",
118 /* 9 */ "Reserved",
119 /* A */ "Reserved",
120 /* B */ "ABORT TASK",
121 /* C */ "ABORT TASK SET",
122 /* D */ "Reserved",
123 /* E */ "LOGICAL UNIT RESET",
124 /* F */ "TARGET RESET"
125};
126
127#define ORB_RES_CMPL 0
128#define ORB_RES_FAIL 1
129#define ORB_RES_ILLE 2
130#define ORB_RES_VEND 3
131
132static int debug = 0;
133static int auto_login = 1;
134static int max_speed = 2;
135
136SYSCTL_DECL(_hw_firewire);
137SYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem");
138SYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0,
139 "SBP debug flag");
140SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0,
141 "SBP perform login automatically");
142SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0,
143 "SBP transfer max speed");
144
145#define SBP_DEBUG(x) if (debug > x) {
146#define END_DEBUG }
147
148#define NEED_RESPONSE 0
149
150struct ind_ptr {
151 u_int32_t hi,lo;
152};
153#define SBP_IND_MAX 0x20
154struct sbp_ocb {
155 STAILQ_ENTRY(sbp_ocb) ocb;
156 union ccb *ccb;
157 volatile u_int32_t orb[8];
158 volatile struct ind_ptr ind_ptr[SBP_IND_MAX];
159 struct sbp_dev *sdev;
160 int flags;
161 bus_dmamap_t dmamap;
162};
163#define OCB_ACT_MGM 0
164#define OCB_ACT_CMD 1
165#define OCB_ACT_MASK 3
166#define OCB_RESERVED 0x10
167#define OCB_DONE 0x20
168
169#define SBP_RESOURCE_SHORTAGE 0x10
170
171struct sbp_login_res{
172 u_int16_t len;
173 u_int16_t id;
174 u_int16_t res0;
175 u_int16_t cmd_hi;
176 u_int32_t cmd_lo;
177 u_int16_t res1;
178 u_int16_t recon_hold;
179};
180struct sbp_status{
181 u_int8_t len:3,
182 dead:1,
183 resp:2,
184 src:2;
185 u_int8_t status:8;
186 u_int16_t orb_hi;
187 u_int32_t orb_lo;
188 u_int32_t data[6];
189};
190struct sbp_cmd_status{
191#define SBP_SFMT_CURR 0
192#define SBP_SFMT_DEFER 1
193 u_int8_t status:6,
194 sfmt:2;
195 u_int8_t s_key:4,
196 ill_len:1,
197 eom:1,
198 mark:1,
199 valid:1;
200 u_int8_t s_code;
201 u_int8_t s_qlfr;
202 u_int32_t info;
203 u_int32_t cdb;
204 u_int32_t fru:8,
205 s_keydep:24;
206 u_int32_t vend[2];
207};
208
209struct sbp_dev{
210#define SBP_DEV_RESET 0 /* accept login */
211#if 0
212#define SBP_DEV_LOGIN 1 /* to login */
213#define SBP_DEV_RECONN 2 /* to reconnect */
214#endif
215#define SBP_DEV_TOATTACH 3 /* to attach */
216#define SBP_DEV_PROBE 4 /* scan lun */
217#define SBP_DEV_ATTACHED 5 /* in operation */
218#define SBP_DEV_DEAD 6 /* unavailable unit */
219#define SBP_DEV_RETRY 7 /* unavailable unit */
220 u_int8_t status:4,
221#define SBP_DEV_TIMEOUT 1
222 flags:4;
223 u_int8_t type;
224 u_int16_t lun_id;
225 struct cam_path *path;
226 struct sbp_target *target;
227 struct sbp_login_res login;
228 STAILQ_HEAD(, sbp_ocb) ocbs;
229 char vendor[32];
230 char product[32];
231 char revision[10];
232};
233
234struct sbp_target {
235 int target_id;
236 int num_lun;
237 struct sbp_dev *luns;
238 struct sbp_softc *sbp;
239 struct fw_device *fwdev;
240 u_int32_t mgm_hi, mgm_lo;
241};
242
243struct sbp_softc {
244 struct firewire_dev_comm fd;
245 unsigned char flags;
246 struct cam_sim *sim;
247 struct sbp_target targets[SBP_NUM_TARGETS];
248 struct fw_bind fwb;
249 STAILQ_HEAD(, sbp_ocb) free_ocbs;
250 struct sbp_ocb *ocb;
251 bus_dma_tag_t dmat;
252};
253static void sbp_post_explore __P((void *));
254static void sbp_recv __P((struct fw_xfer *));
255static void sbp_login_callback __P((struct fw_xfer *));
256static void sbp_cmd_callback __P((struct fw_xfer *));
257static void sbp_orb_pointer __P((struct sbp_dev *, struct sbp_ocb *));
258static void sbp_execute_ocb __P((void *, bus_dma_segment_t *, int, int));
259static void sbp_free_ocb __P((struct sbp_softc *, struct sbp_ocb *));
260static void sbp_abort_ocb __P((struct sbp_ocb *, int));
261static void sbp_abort_all_ocbs __P((struct sbp_dev *, int));
262static struct fw_xfer * sbp_write_cmd __P((struct sbp_dev *, int, int));
263static struct sbp_ocb * sbp_get_ocb __P((struct sbp_softc *));
264static struct sbp_ocb * sbp_enqueue_ocb __P((struct sbp_dev *, struct sbp_ocb *));
265static struct sbp_ocb * sbp_dequeue_ocb __P((struct sbp_dev *, u_int32_t));
266static void sbp_cam_detach_target __P((struct sbp_target *));
267static void sbp_timeout __P((void *arg));
268static void sbp_mgm_orb __P((struct sbp_dev *, int, u_int16_t, u_int32_t));
269
270MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire");
271
272/* cam related functions */
273static void sbp_action(struct cam_sim *sim, union ccb *ccb);
274static void sbp_poll(struct cam_sim *sim);
275static void sbp_cam_callback(struct cam_periph *periph,
276 union ccb *ccb);
277static void sbp_cam_scan_lun(struct sbp_dev *sdev);
278
279static char *orb_status0[] = {
280 /* 0 */ "No additional information to report",
281 /* 1 */ "Request type not supported",
282 /* 2 */ "Speed not supported",
283 /* 3 */ "Page size not supported",
284 /* 4 */ "Access denied",
285 /* 5 */ "Logical unit not supported",
286 /* 6 */ "Maximum payload too small",
287 /* 7 */ "Reserved for future standardization",
288 /* 8 */ "Resources unavailable",
289 /* 9 */ "Function rejected",
290 /* A */ "Login ID not recognized",
291 /* B */ "Dummy ORB completed",
292 /* C */ "Request aborted",
293 /* FF */ "Unspecified error"
294#define MAX_ORB_STATUS0 0xd
295};
296
297static char *orb_status1_object[] = {
298 /* 0 */ "Operation request block (ORB)",
299 /* 1 */ "Data buffer",
300 /* 2 */ "Page table",
301 /* 3 */ "Unable to specify"
302};
303
304static char *orb_status1_serial_bus_error[] = {
305 /* 0 */ "Missing acknowledge",
306 /* 1 */ "Reserved; not to be used",
307 /* 2 */ "Time-out error",
308 /* 3 */ "Reserved; not to be used",
309 /* 4 */ "Busy retry limit exceeded(X)",
310 /* 5 */ "Busy retry limit exceeded(A)",
311 /* 6 */ "Busy retry limit exceeded(B)",
312 /* 7 */ "Reserved for future standardization",
313 /* 8 */ "Reserved for future standardization",
314 /* 9 */ "Reserved for future standardization",
315 /* A */ "Reserved for future standardization",
316 /* B */ "Tardy retry limit exceeded",
317 /* C */ "Conflict error",
318 /* D */ "Data error",
319 /* E */ "Type error",
320 /* F */ "Address error"
321};
322
323static void
324sbp_identify(driver_t *driver, device_t parent)
325{
326 device_t child;
327SBP_DEBUG(0)
328 printf("sbp_identify\n");
329END_DEBUG
330
331 child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent));
332}
333
334/*
335 * sbp_probe()
336 */
337static int
338sbp_probe(device_t dev)
339{
340 device_t pa;
341
342SBP_DEBUG(0)
343 printf("sbp_probe\n");
344END_DEBUG
345
346 pa = device_get_parent(dev);
347 if(device_get_unit(dev) != device_get_unit(pa)){
348 return(ENXIO);
349 }
350
351 device_set_desc(dev, "SBP2/SCSI over firewire");
352 return (0);
353}
354
355static void
356sbp_show_sdev_info(struct sbp_dev *sdev, int new)
357{
358 struct fw_device *fwdev;
359
360 printf("%s:%d:%d ",
361 device_get_nameunit(sdev->target->sbp->fd.dev),
362 sdev->target->target_id,
363 sdev->lun_id
364 );
365 if (new == 2) {
366 return;
367 }
368 fwdev = sdev->target->fwdev;
369 printf("ordered:%d type:%d EUI:%08x%08x node:%d "
370 "speed:%d maxrec:%d",
371 (sdev->type & 0x40) >> 6,
372 (sdev->type & 0x1f),
373 fwdev->eui.hi,
374 fwdev->eui.lo,
375 fwdev->dst,
376 fwdev->speed,
377 fwdev->maxrec
378 );
379 if (new)
380 printf(" new!\n");
381 else
382 printf("\n");
383 sbp_show_sdev_info(sdev, 2);
384 printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision);
385}
386
387static struct {
388 int bus;
389 int target;
390 struct fw_eui64 eui;
391} wired[] = {
392 /* Bus Target EUI64 */
393#if 0
394 {0, 2, {0x00018ea0, 0x01fd0154}}, /* Logitec HDD */
395 {0, 0, {0x00018ea6, 0x00100682}}, /* Logitec DVD */
396 {0, 1, {0x00d03200, 0xa412006a}}, /* Yano HDD */
397#endif
398 {-1, -1, {0,0}}
399};
400
401static int
402sbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev)
403{
404 int bus, i, target=-1;
405 char w[SBP_NUM_TARGETS];
406
407 bzero(w, sizeof(w));
408 bus = device_get_unit(sbp->fd.dev);
409
410 /* XXX wired-down configuration should be gotten from
411 tunable or device hint */
412 for (i = 0; wired[i].bus >= 0; i ++) {
413 if (wired[i].bus == bus) {
414 w[wired[i].target] = 1;
415 if (wired[i].eui.hi == fwdev->eui.hi &&
416 wired[i].eui.lo == fwdev->eui.lo)
417 target = wired[i].target;
418 }
419 }
420 if (target >= 0) {
421 if(target < SBP_NUM_TARGETS &&
422 sbp->targets[target].fwdev == NULL)
423 return(target);
424 device_printf(sbp->fd.dev,
425 "target %d is not free for %08x:%08x\n",
426 target, fwdev->eui.hi, fwdev->eui.lo);
427 target = -1;
428 }
429 /* non-wired target */
430 for (i = 0; i < SBP_NUM_TARGETS; i ++)
431 if (sbp->targets[i].fwdev == NULL && w[i] == 0) {
432 target = i;
433 break;
434 }
435
436 return target;
437}
438
439static struct sbp_target *
440sbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
441{
442 int i, maxlun, lun;
443 struct sbp_target *target;
444 struct sbp_dev *sdev;
445 struct crom_context cc;
446 struct csrreg *reg;
447
448SBP_DEBUG(1)
449 printf("sbp_alloc_target\n");
450END_DEBUG
451 i = sbp_new_target(sbp, fwdev);
452 if (i < 0) {
453 device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n");
454 return NULL;
455 }
456 /* new target */
457 target = &sbp->targets[i];
458 target->sbp = sbp;
459 target->fwdev = fwdev;
460 target->target_id = i;
461 if((target->mgm_lo = getcsrdata(fwdev, 0x54)) == 0 ){
462 /* bad target */
463 printf("NULL management address\n");
464 target->fwdev = NULL;
465 return NULL;
466 }
467 target->mgm_hi = 0xffff;
468 target->mgm_lo = 0xf0000000 | target->mgm_lo << 2;
469 /* XXX num_lun may be changed. realloc luns? */
470 crom_init_context(&cc, target->fwdev->csrrom);
471 /* XXX shoud parse appropriate unit directories only */
472 maxlun = -1;
473 while (cc.depth >= 0) {
474 reg = crom_search_key(&cc, CROM_LUN);
475 if (reg == NULL)
476 break;
477 lun = reg->val & 0xff;
478SBP_DEBUG(0)
479 printf("target %d lun %d found\n", target->target_id, lun);
480END_DEBUG
481 if (maxlun < lun)
482 maxlun = lun;
483 crom_next(&cc);
484 }
485 target->num_lun = maxlun + 1;
486 if (maxlun < 0) {
487 printf("no lun found!\n");
488 }
489 target->luns = (struct sbp_dev *) malloc(
490 sizeof(struct sbp_dev) * target->num_lun,
491 M_SBP, M_NOWAIT | M_ZERO);
492 for (i = 0; i < target->num_lun; i++) {
493 sdev = &target->luns[i];
494 sdev->lun_id = i;
495 sdev->target = target;
496 STAILQ_INIT(&sdev->ocbs);
497 sdev->status = SBP_DEV_DEAD;
498 }
499 crom_init_context(&cc, target->fwdev->csrrom);
500 while (cc.depth >= 0) {
501 reg = crom_search_key(&cc, CROM_LUN);
502 if (reg == NULL)
503 break;
504 lun = reg->val & 0xff;
505 target->luns[lun].status = SBP_DEV_RESET;
506 target->luns[lun].type = (reg->val & 0x0f00) >> 16;
507 crom_next(&cc);
508 }
509 return target;
510}
511
512static void
513sbp_get_text_leaf(struct fw_device *fwdev, int key, char *buf, int len)
514{
515 static char *nullstr = "(null)";
516 int i, clen, found=0;
517 struct csrhdr *chdr;
518 struct csrreg *creg;
519 u_int32_t *src, *dst;
520
521 chdr = (struct csrhdr *)&fwdev->csrrom[0];
522 creg = (struct csrreg *)chdr;
523 creg += chdr->info_len;
524 for( i = chdr->info_len + 4; i <= fwdev->rommax; i+=4){
524 for( i = chdr->info_len + 4; i <= fwdev->rommax - 4; i+=4){
525 if((creg++)->key == key){
526 found = 1;
527 break;
528 }
529 }
530 if (!found) {
530 if (!found || creg->key != CROM_TEXTLEAF) {
531 strncpy(buf, nullstr, len);
532 return;
533 }
534 src = (u_int32_t *) creg + creg->val;
535 clen = ((*src >> 16) - 2) * 4;
536 src += 3;
537 dst = (u_int32_t *) buf;
538 if (len < clen)
539 clen = len;
540 for (i = 0; i < clen/4; i++)
541 *dst++ = htonl(*src++);
542 buf[clen] = 0;
543}
544
545static void
546sbp_probe_lun(struct sbp_dev *sdev)
547{
548 struct fw_device *fwdev;
549 int rev;
550
551 fwdev = sdev->target->fwdev;
552 bzero(sdev->vendor, sizeof(sdev->vendor));
553 bzero(sdev->product, sizeof(sdev->product));
554 sbp_get_text_leaf(fwdev, 0x03, sdev->vendor, sizeof(sdev->vendor));
555 sbp_get_text_leaf(fwdev, 0x17, sdev->product, sizeof(sdev->product));
556 rev = getcsrdata(sdev->target->fwdev, 0x3c);
557 snprintf(sdev->revision, sizeof(sdev->revision), "%06x", rev);
558}
559static void
560sbp_probe_target(struct sbp_target *target, int alive)
561{
562 struct sbp_softc *sbp;
563 struct sbp_dev *sdev;
564 struct firewire_comm *fc;
565 int i;
566
567SBP_DEBUG(1)
568 printf("sbp_probe_target %d\n", target->target_id);
569 if (!alive)
570 printf("not alive\n");
571END_DEBUG
572
573 sbp = target->sbp;
574 fc = target->sbp->fd.fc;
575 for (i=0; i < target->num_lun; i++) {
576 sdev = &target->luns[i];
577 if (alive && (sdev->status != SBP_DEV_DEAD)) {
578 if (sdev->path != NULL) {
579 xpt_freeze_devq(sdev->path, 1);
580 }
581 sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ);
582 switch (sdev->status) {
583 case SBP_DEV_RESET:
584 /* new or revived target */
585 sbp_probe_lun(sdev);
586 if (auto_login) {
587 sdev->status = SBP_DEV_TOATTACH;
588 sbp_mgm_orb(sdev, ORB_FUN_LGI, 0, 0);
589 }
590 break;
591 case SBP_DEV_RETRY:
592 sbp_probe_lun(sdev);
593 default:
594 sbp_mgm_orb(sdev, ORB_FUN_RCN, 0, 0);
595 break;
596 }
597SBP_DEBUG(0)
598 sbp_show_sdev_info(sdev,
599 (sdev->status == SBP_DEV_TOATTACH));
600END_DEBUG
601 } else {
602 switch (sdev->status) {
603 case SBP_DEV_ATTACHED:
604SBP_DEBUG(0)
605 /* the device has gone */
606 sbp_show_sdev_info(sdev, 2);
607 printf("lost target\n");
608END_DEBUG
609 if (sdev->path)
610 xpt_freeze_devq(sdev->path, 1);
611 sdev->status = SBP_DEV_RETRY;
612 sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ);
613 break;
614 case SBP_DEV_PROBE:
615 case SBP_DEV_TOATTACH:
616 sdev->status = SBP_DEV_RESET;
617 break;
618 case SBP_DEV_RETRY:
619 case SBP_DEV_RESET:
620 case SBP_DEV_DEAD:
621 break;
622 }
623 }
624 }
625}
626
627#if 0
628static void
629sbp_release_queue(void *arg)
630{
631 struct sbp_softc *sbp;
632
633SBP_DEBUG(0)
634 printf("sbp_release_queue\n");
635END_DEBUG
636 sbp = (struct sbp_softc *)arg;
637 xpt_release_simq(sbp->sim, 1);
638}
639
640static void
641sbp_release_devq(void *arg)
642{
643 struct sbp_dev *sdev;
644 int s;
645
646 sdev = (struct sbp_dev *)arg;
647SBP_DEBUG(0)
648 sbp_show_sdev_info(sdev, 2);
649 printf("sbp_release_devq\n");
650END_DEBUG
651 s = splcam();
652 xpt_release_devq(sdev->path, 1, TRUE);
653 splx(s);
654}
655#endif
656
657static void
658sbp_post_explore(void *arg)
659{
660 struct sbp_softc *sbp = (struct sbp_softc *)arg;
661 struct sbp_target *target;
662 struct fw_device *fwdev;
663 int i, alive;
664
665SBP_DEBUG(1)
666 printf("sbp_post_explore\n");
667END_DEBUG
668#if 0
669 xpt_freeze_simq(sbp->sim, /*count*/ 1);
670#endif
671 /* Gabage Collection */
672 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
673 target = &sbp->targets[i];
674 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
675 if (target->fwdev == NULL || target->fwdev == fwdev)
676 break;
677 if(fwdev == NULL){
678 /* device has removed in lower driver */
679 sbp_cam_detach_target(target);
680 if (target->luns != NULL)
681 free(target->luns, M_SBP);
682 target->num_lun = 0;;
683 target->luns = NULL;
684 target->fwdev = NULL;
685 }
686 }
687 /* traverse device list */
688 STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
689SBP_DEBUG(0)
690 printf("sbp_post_explore: EUI:%08x%08x ",
691 fwdev->eui.hi, fwdev->eui.lo);
692 if (fwdev->status == FWDEVATTACHED) {
693 printf("spec=%d key=%d.\n",
694 getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10,
695 getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2);
696 } else {
697 printf("not attached, state=%d.\n", fwdev->status);
698 }
699END_DEBUG
700 alive = (fwdev->status == FWDEVATTACHED)
701 && (getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10)
702 && (getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2);
703 for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
704 target = &sbp->targets[i];
705 if(target->fwdev == fwdev ) {
706 /* known target */
707 break;
708 }
709 }
710 if(i == SBP_NUM_TARGETS){
711 if (alive) {
712 /* new target */
713 target = sbp_alloc_target(sbp, fwdev);
714 if (target == NULL)
715 continue;
716 } else {
717 continue;
718 }
719 }
720 sbp_probe_target(target, alive);
721 }
722#if 0
723 timeout(sbp_release_queue, (caddr_t)sbp, bus_reset_rest * hz / 1000);
724#endif
725}
726
727#if NEED_RESPONSE
728static void
729sbp_loginres_callback(struct fw_xfer *xfer){
730SBP_DEBUG(1)
731 struct sbp_dev *sdev;
732 sdev = (struct sbp_dev *)xfer->sc;
733 sbp_show_sdev_info(sdev, 2);
734 printf("sbp_loginres_callback\n");
735END_DEBUG
736 fw_xfer_free(xfer);
737 return;
738}
739#endif
740
741static void
742sbp_login_callback(struct fw_xfer *xfer)
743{
744SBP_DEBUG(1)
745 struct sbp_dev *sdev;
746 sdev = (struct sbp_dev *)xfer->sc;
747 sbp_show_sdev_info(sdev, 2);
748 printf("sbp_login_callback\n");
749END_DEBUG
750 fw_xfer_free(xfer);
751 return;
752}
753
754static void
755sbp_cmd_callback(struct fw_xfer *xfer)
756{
757SBP_DEBUG(2)
758 struct sbp_dev *sdev;
759 sdev = (struct sbp_dev *)xfer->sc;
760 sbp_show_sdev_info(sdev, 2);
761 printf("sbp_cmd_callback\n");
762END_DEBUG
763 fw_xfer_free(xfer);
764 return;
765}
766
767static void
768sbp_cam_callback(struct cam_periph *periph, union ccb *ccb)
769{
770 struct sbp_dev *sdev;
771 sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
772SBP_DEBUG(0)
773 sbp_show_sdev_info(sdev, 2);
774 printf("sbp_cam_callback\n");
775END_DEBUG
776 sdev->status = SBP_DEV_ATTACHED;
777 free(ccb, M_SBP);
778}
779
780static void
781sbp_cam_scan_lun(struct sbp_dev *sdev)
782{
783 union ccb *ccb = malloc(sizeof(union ccb), M_SBP, M_ZERO);
783 union ccb *ccb;
784
785 ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO);
786 if (ccb == NULL) {
787 printf("sbp_cam_scan_lun: malloc failed\n");
788 return;
789 }
790
791SBP_DEBUG(0)
792 sbp_show_sdev_info(sdev, 2);
793 printf("sbp_cam_scan_lun\n");
794END_DEBUG
795 xpt_setup_ccb(&ccb->ccb_h, sdev->path, 5/*priority (low)*/);
796 ccb->ccb_h.func_code = XPT_SCAN_LUN;
797 ccb->ccb_h.cbfcnp = sbp_cam_callback;
798 ccb->crcn.flags = CAM_FLAG_NONE;
799 ccb->ccb_h.ccb_sdev_ptr = sdev;
800 xpt_action(ccb);
801
802 /* The scan is in progress now. */
803}
804
805
806static void
807sbp_ping_unit_callback(struct cam_periph *periph, union ccb *ccb)
808{
809 struct sbp_dev *sdev;
810 sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
811SBP_DEBUG(0)
812 sbp_show_sdev_info(sdev, 2);
813 printf("sbp_ping_unit_callback\n");
814END_DEBUG
815 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
816 if (--ccb->ccb_h.retry_count == 0) {
817 sbp_show_sdev_info(sdev, 2);
818 printf("sbp_ping_unit_callback: "
819 "retry count exceeded\n");
820 sdev->status = SBP_DEV_RETRY;
821 free(ccb, M_SBP);
822 } else {
823 /* requeue */
824 xpt_action(ccb);
825 xpt_release_devq(sdev->path, 1, TRUE);
826 }
827 } else {
828 free(ccb->csio.data_ptr, M_SBP);
829 free(ccb, M_SBP);
830 sdev->status = SBP_DEV_ATTACHED;
831 xpt_release_devq(sdev->path, 1, TRUE);
832 }
833}
834
835/*
836 * XXX Some devices need to execute inquiry or read_capacity
837 * after bus_rest during busy transfer.
838 * Otherwise they return incorrect result for READ(and WRITE?)
839 * command without any SBP-II/SCSI error.
840 *
841 * e.g. Maxtor 3000XT, Yano A-dish.
842 */
843static void
844sbp_ping_unit(struct sbp_dev *sdev)
845{
846 union ccb *ccb;
847 struct scsi_inquiry_data *inq_buf;
848
843 ccb = malloc(sizeof(union ccb), M_SBP, M_ZERO);
849
850 ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO);
851 if (ccb == NULL) {
852 printf("sbp_ping_unit: malloc failed\n");
853 return;
854 }
855
856 inq_buf = (struct scsi_inquiry_data *)
845 malloc(sizeof(*inq_buf), M_SBP, 0);
857 malloc(sizeof(*inq_buf), M_SBP, M_NOWAIT);
858 if (inq_buf == NULL) {
859 free(ccb, M_SBP);
860 printf("sbp_ping_unit: malloc failed\n");
861 return;
862 }
863
864SBP_DEBUG(0)
865 sbp_show_sdev_info(sdev, 2);
866 printf("sbp_ping_unit\n");
867END_DEBUG
868
869 /*
870 * We need to execute this command before any other queued command.
871 * Make priority 0 and freeze queue after execution for retry.
872 * cam's scan_lun command doesn't provide this feature.
873 */
874 xpt_setup_ccb(&ccb->ccb_h, sdev->path, 0/*priority (high)*/);
875 scsi_inquiry(
876 &ccb->csio,
877 /*retries*/ 5,
878 sbp_ping_unit_callback,
879 MSG_SIMPLE_Q_TAG,
880 (u_int8_t *)inq_buf,
881 SHORT_INQUIRY_LENGTH,
882 /*evpd*/FALSE,
883 /*page_code*/0,
884 SSD_MIN_SIZE,
885 /*timeout*/60000
886 );
887 ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
888 xpt_action(ccb);
889
890 if (sdev->status == SBP_DEV_RETRY)
891 /* freezed twice */
892 xpt_release_devq(sdev->path, 1, TRUE);
893}
894
895static void
896sbp_do_attach(struct fw_xfer *xfer)
897{
898 struct sbp_dev *sdev;
899
900 sdev = (struct sbp_dev *)xfer->sc;
901SBP_DEBUG(0)
902 sbp_show_sdev_info(sdev, 2);
903 printf("sbp_do_attach\n");
904END_DEBUG
905 fw_xfer_free(xfer);
906 if (sdev->path == NULL)
907 xpt_create_path(&sdev->path, xpt_periph,
908 cam_sim_path(sdev->target->sbp->sim),
909 sdev->target->target_id, sdev->lun_id);
910
911 if (sdev->status == SBP_DEV_RETRY) {
912 sbp_ping_unit(sdev);
913 sdev->status = SBP_DEV_PROBE;
914 } else {
915 sdev->status = SBP_DEV_PROBE;
916 sbp_cam_scan_lun(sdev);
917 }
918 xpt_release_devq(sdev->path, 1, TRUE);
919 return;
920}
921
922static void
923sbp_agent_reset_callback(struct fw_xfer *xfer)
924{
925 struct sbp_dev *sdev;
926
927 sdev = (struct sbp_dev *)xfer->sc;
928SBP_DEBUG(1)
929 sbp_show_sdev_info(sdev, 2);
930 printf("sbp_cmd_callback\n");
931END_DEBUG
932 fw_xfer_free(xfer);
933 sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ);
934 if (sdev->path)
935 xpt_release_devq(sdev->path, 1, TRUE);
936}
937
938static void
939sbp_agent_reset(struct sbp_dev *sdev)
940{
941 struct fw_xfer *xfer;
942 struct fw_pkt *fp;
943
944SBP_DEBUG(0)
945 sbp_show_sdev_info(sdev, 2);
946 printf("sbp_agent_reset\n");
947END_DEBUG
948 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
949 if (xfer == NULL)
950 return;
951 if (sdev->status == SBP_DEV_ATTACHED)
952 xfer->act.hand = sbp_agent_reset_callback;
953 else
954 xfer->act.hand = sbp_do_attach;
955 fp = (struct fw_pkt *)xfer->send.buf;
956 fp->mode.wreqq.data = htonl(0xf);
957 fw_asyreq(xfer->fc, -1, xfer);
958}
959
960static void
961sbp_busy_timeout_callback(struct fw_xfer *xfer)
962{
963 struct sbp_dev *sdev;
964
965 sdev = (struct sbp_dev *)xfer->sc;
966SBP_DEBUG(1)
967 sbp_show_sdev_info(sdev, 2);
968 printf("sbp_busy_timeout_callback\n");
969END_DEBUG
970 fw_xfer_free(xfer);
971 sbp_agent_reset(sdev);
972}
973
974static void
975sbp_busy_timeout(struct sbp_dev *sdev)
976{
977 struct fw_pkt *fp;
978 struct fw_xfer *xfer;
979SBP_DEBUG(0)
980 sbp_show_sdev_info(sdev, 2);
981 printf("sbp_busy_timeout\n");
982END_DEBUG
983 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
984
985 xfer->act.hand = sbp_busy_timeout_callback;
986 fp = (struct fw_pkt *)xfer->send.buf;
987 fp->mode.wreqq.dest_hi = htons(0xffff);
988 fp->mode.wreqq.dest_lo = htonl(0xf0000000 | BUSY_TIMEOUT);
989 fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
990 fw_asyreq(xfer->fc, -1, xfer);
991}
992
993#if 0
994static void
995sbp_reset_start(struct sbp_dev *sdev)
996{
997 struct fw_xfer *xfer;
998 struct fw_pkt *fp;
999
1000SBP_DEBUG(0)
1001 sbp_show_sdev_info(sdev, 2);
1002 printf("sbp_reset_start\n");
1003END_DEBUG
1004 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1005
1006 xfer->act.hand = sbp_busy_timeout;
1007 fp = (struct fw_pkt *)xfer->send.buf;
1008 fp->mode.wreqq.dest_hi = htons(0xffff);
1009 fp->mode.wreqq.dest_lo = htonl(0xf0000000 | RESET_START);
1010 fp->mode.wreqq.data = htonl(0xf);
1011 fw_asyreq(xfer->fc, -1, xfer);
1012}
1013#endif
1014
1015static void
1016sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1017{
1018 struct fw_xfer *xfer;
1019 struct fw_pkt *fp;
1020SBP_DEBUG(2)
1021 sbp_show_sdev_info(sdev, 2);
1022 printf("sbp_orb_pointer\n");
1023END_DEBUG
1024
1025 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1026 if (xfer == NULL)
1027 return;
1028 xfer->act.hand = sbp_cmd_callback;
1029
1030 fp = (struct fw_pkt *)xfer->send.buf;
1031 fp->mode.wreqb.len = htons(8);
1032 fp->mode.wreqb.extcode = 0;
1033 fp->mode.wreqb.payload[0] =
1034 htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1035 fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0]));
1036
1037 if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1038 fw_xfer_free(xfer);
1039 ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1040 xpt_done(ocb->ccb);
1041 }
1042}
1043
1044static void
1045sbp_doorbell(struct sbp_dev *sdev)
1046{
1047 struct fw_xfer *xfer;
1048 struct fw_pkt *fp;
1049SBP_DEBUG(1)
1050 sbp_show_sdev_info(sdev, 2);
1051 printf("sbp_doorbell\n");
1052END_DEBUG
1053
1054 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1055 if (xfer == NULL)
1056 return;
1057 xfer->act.hand = sbp_cmd_callback;
1058 fp = (struct fw_pkt *)xfer->send.buf;
1059 fp->mode.wreqq.data = htonl(0xf);
1060 fw_asyreq(xfer->fc, -1, xfer);
1061}
1062
1063static struct fw_xfer *
1064sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1065{
1066 struct fw_xfer *xfer;
1067 struct fw_pkt *fp;
1068
1069 xfer = fw_xfer_alloc(M_SBP);
1070 if(xfer == NULL){
1071 return NULL;
1072 }
1073 if (tcode == FWTCODE_WREQQ)
1074 xfer->send.len = 16;
1075 else
1076 xfer->send.len = 24;
1077
1078 xfer->send.buf = malloc(xfer->send.len, M_FW, M_NOWAIT);
1079 if(xfer->send.buf == NULL){
1080 fw_xfer_free(xfer);
1081 return NULL;
1082 }
1083
1084 xfer->send.off = 0;
1085 xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1086 xfer->sc = (caddr_t)sdev;
1087 xfer->fc = sdev->target->sbp->fd.fc;
1088 xfer->retry_req = fw_asybusy;
1089
1090 fp = (struct fw_pkt *)xfer->send.buf;
1091 fp->mode.wreqq.dest_hi = htons(sdev->login.cmd_hi);
1092 fp->mode.wreqq.dest_lo = htonl(sdev->login.cmd_lo + offset);
1093 fp->mode.wreqq.tlrt = 0;
1094 fp->mode.wreqq.tcode = tcode;
1095 fp->mode.wreqq.pri = 0;
1096 xfer->dst = FWLOCALBUS | sdev->target->fwdev->dst;
1097 fp->mode.wreqq.dst = htons(xfer->dst);
1098
1099 return xfer;
1100
1101}
1102
1103static void
1104sbp_mgm_orb(struct sbp_dev *sdev, int func, u_int16_t orb_hi, u_int32_t orb_lo)
1105{
1106 struct fw_xfer *xfer;
1107 struct fw_pkt *fp;
1108 struct sbp_ocb *ocb;
1109 int s, nid;
1110
1111 if ((ocb = sbp_get_ocb(sdev->target->sbp)) == NULL) {
1112 s = splfw();
1113 sdev->target->sbp->flags |= SBP_RESOURCE_SHORTAGE;
1114 splx(s);
1115 return;
1116 }
1117 ocb->flags = OCB_ACT_MGM;
1118 ocb->sdev = sdev;
1119 ocb->ccb = NULL;
1120
1121 nid = sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS;
1122 bzero((void *)(uintptr_t)(volatile void *)ocb->orb, sizeof(ocb->orb));
1123 ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1124 ocb->orb[7] = htonl(SBP_DEV2ADDR(
1125 device_get_unit(sdev->target->sbp->fd.dev),
1126 sdev->target->target_id,
1127 sdev->lun_id));
1128
1129SBP_DEBUG(0)
1130 sbp_show_sdev_info(sdev, 2);
1131 printf("%s\n", orb_fun_name[(func>>16)&0xf]);
1132END_DEBUG
1133 switch (func) {
1134 case ORB_FUN_LGI:
1135 ocb->orb[2] = htonl(nid << 16);
1136 ocb->orb[3] = htonl(vtophys(&sdev->login));
1137 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_EXV | sdev->lun_id);
1138 ocb->orb[5] = htonl(sizeof(struct sbp_login_res));
1139 break;
1140 case ORB_FUN_ATA:
1141 ocb->orb[0] = htonl((0 << 16) | orb_hi);
1142 ocb->orb[1] = htonl(orb_lo);
1143 /* fall through */
1144 case ORB_FUN_RCN:
1145 case ORB_FUN_LGO:
1146 case ORB_FUN_LUR:
1147 case ORB_FUN_RST:
1148 case ORB_FUN_ATS:
1149 ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login.id);
1150 break;
1151 }
1152
1153 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1154 if(xfer == NULL){
1155 return;
1156 }
1157 xfer->act.hand = sbp_login_callback;
1158
1159 fp = (struct fw_pkt *)xfer->send.buf;
1160 fp->mode.wreqb.dest_hi = htons(sdev->target->mgm_hi);
1161 fp->mode.wreqb.dest_lo = htonl(sdev->target->mgm_lo);
1162 fp->mode.wreqb.len = htons(8);
1163 fp->mode.wreqb.extcode = 0;
1164 fp->mode.wreqb.payload[0] = htonl(nid << 16);
1165 fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0]));
1166 sbp_enqueue_ocb(sdev, ocb);
1167
1168 fw_asyreq(xfer->fc, -1, xfer);
1169}
1170
1171static void
1172sbp_print_scsi_cmd(struct sbp_ocb *ocb)
1173{
1174 struct ccb_scsiio *csio;
1175
1176 csio = &ocb->ccb->csio;
1177 printf("%s:%d:%d XPT_SCSI_IO: "
1178 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1179 ", flags: 0x%02x, "
1180 "%db cmd/%db data/%db sense\n",
1181 device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1182 ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun,
1183 csio->cdb_io.cdb_bytes[0],
1184 csio->cdb_io.cdb_bytes[1],
1185 csio->cdb_io.cdb_bytes[2],
1186 csio->cdb_io.cdb_bytes[3],
1187 csio->cdb_io.cdb_bytes[4],
1188 csio->cdb_io.cdb_bytes[5],
1189 csio->cdb_io.cdb_bytes[6],
1190 csio->cdb_io.cdb_bytes[7],
1191 csio->cdb_io.cdb_bytes[8],
1192 csio->cdb_io.cdb_bytes[9],
1193 ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1194 csio->cdb_len, csio->dxfer_len,
1195 csio->sense_len);
1196}
1197
1198static void
1199sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1200{
1201 struct sbp_cmd_status *sbp_cmd_status;
1202 struct scsi_sense_data *sense;
1203
1204 sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1205 sense = &ocb->ccb->csio.sense_data;
1206
1207SBP_DEBUG(0)
1208 sbp_print_scsi_cmd(ocb);
1209 /* XXX need decode status */
1210 sbp_show_sdev_info(ocb->sdev, 2);
1211 printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d",
1212 sbp_cmd_status->status,
1213 sbp_cmd_status->sfmt,
1214 sbp_cmd_status->valid,
1215 sbp_cmd_status->s_key,
1216 sbp_cmd_status->s_code,
1217 sbp_cmd_status->s_qlfr,
1218 sbp_status->len
1219 );
1220#if 0 /* XXX */
1221 if (sbp_cmd_status->status == SCSI_STATUS_CHECK_COND) {
1222 printf(" %s\n", scsi_sense_key_text[sbp_cmd_status->s_key]);
1223 scsi_sense_desc(
1224 sbp_cmd_status->s_code,
1225 sbp_cmd_status->s_qlfr,
1226 ocb->ccb->ccb_h.path->device->inq_data
1227 )
1228 } else {
1229 printf("\n");
1230 }
1231#else
1232 printf("\n");
1233#endif
1234END_DEBUG
1235
1236 switch (sbp_cmd_status->status) {
1237 case SCSI_STATUS_CHECK_COND:
1238 case SCSI_STATUS_BUSY:
1239 case SCSI_STATUS_CMD_TERMINATED:
1240 if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1241 sense->error_code = SSD_CURRENT_ERROR;
1242 }else{
1243 sense->error_code = SSD_DEFERRED_ERROR;
1244 }
1245 if(sbp_cmd_status->valid)
1246 sense->error_code |= SSD_ERRCODE_VALID;
1247 sense->flags = sbp_cmd_status->s_key;
1248 if(sbp_cmd_status->mark)
1249 sense->flags |= SSD_FILEMARK;
1250 if(sbp_cmd_status->eom)
1251 sense->flags |= SSD_EOM;
1252 if(sbp_cmd_status->ill_len)
1253 sense->flags |= SSD_ILI;
1254 sense->info[0] = ntohl(sbp_cmd_status->info) & 0xff;
1255 sense->info[1] =(ntohl(sbp_cmd_status->info) >> 8) & 0xff;
1256 sense->info[2] =(ntohl(sbp_cmd_status->info) >> 16) & 0xff;
1257 sense->info[3] =(ntohl(sbp_cmd_status->info) >> 24) & 0xff;
1258 if (sbp_status->len <= 1)
1259 /* XXX not scsi status. shouldn't be happened */
1260 sense->extra_len = 0;
1261 else if (sbp_status->len <= 4)
1262 /* add_sense_code(_qual), info, cmd_spec_info */
1263 sense->extra_len = 6;
1264 else
1265 /* fru, sense_key_spec */
1266 sense->extra_len = 10;
1267 sense->cmd_spec_info[0] = ntohl(sbp_cmd_status->cdb) & 0xff;
1268 sense->cmd_spec_info[1] = (ntohl(sbp_cmd_status->cdb) >> 8) & 0xff;
1269 sense->cmd_spec_info[2] = (ntohl(sbp_cmd_status->cdb) >> 16) & 0xff;
1270 sense->cmd_spec_info[3] = (ntohl(sbp_cmd_status->cdb) >> 24) & 0xff;
1271 sense->add_sense_code = sbp_cmd_status->s_code;
1272 sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1273 sense->fru = sbp_cmd_status->fru;
1274 sense->sense_key_spec[0] = ntohl(sbp_cmd_status->s_keydep) & 0xff;
1275 sense->sense_key_spec[1] = (ntohl(sbp_cmd_status->s_keydep) >>8) & 0xff;
1276 sense->sense_key_spec[2] = (ntohl(sbp_cmd_status->s_keydep) >>16) & 0xff;
1277
1278 ocb->ccb->csio.scsi_status = sbp_cmd_status->status;;
1279 ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1280 | CAM_AUTOSNS_VALID;
1281/*
1282{
1283 u_int8_t j, *tmp;
1284 tmp = sense;
1285 for( j = 0 ; j < 32 ; j+=8){
1286 printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1287 tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1288 tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1289 }
1290
1291}
1292*/
1293 break;
1294 default:
1295 sbp_show_sdev_info(ocb->sdev, 2);
1296 printf("sbp_scsi_status: unknown scsi status 0x%x\n",
1297 sbp_cmd_status->status);
1298 }
1299}
1300
1301static void
1302sbp_fix_inq_data(struct sbp_ocb *ocb)
1303{
1304 union ccb *ccb;
1305 struct sbp_dev *sdev;
1306 struct scsi_inquiry_data *inq;
1307
1308 ccb = ocb->ccb;
1309 sdev = ocb->sdev;
1310
1311 if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1312 return;
1313SBP_DEBUG(1)
1314 sbp_show_sdev_info(sdev, 2);
1315 printf("sbp_fix_inq_data\n");
1316END_DEBUG
1317 inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1318 switch (SID_TYPE(inq)) {
1319 case T_DIRECT:
1320 /*
1321 * XXX Convert Direct Access device to RBC.
1322 * I've never seen FireWire DA devices which support READ_6.
1323 */
1324#if 1
1325 if (SID_TYPE(inq) == T_DIRECT)
1326 inq->device |= T_RBC; /* T_DIRECT == 0 */
1327#endif
1328 /* fall through */
1329 case T_RBC:
1330 /* disable tag queuing */
1331 inq->flags &= ~SID_CmdQue;
1332 /*
1333 * Override vendor/product/revision information.
1334 * Some devices sometimes return strange strings.
1335 */
1336 bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1337 bcopy(sdev->product, inq->product, sizeof(inq->product));
1338 bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1339 break;
1340 }
1341}
1342
1343static void
1344sbp_recv1(struct fw_xfer *xfer){
1345 struct fw_pkt *rfp;
1346#if NEED_RESPONSE
1347 struct fw_pkt *sfp;
1348#endif
1349 struct sbp_softc *sbp;
1350 struct sbp_dev *sdev;
1351 struct sbp_ocb *ocb;
1352 struct sbp_login_res *login_res = NULL;
1353 struct sbp_status *sbp_status;
1354 struct sbp_target *target;
1355 int orb_fun, status_valid, t, l;
1356 u_int32_t addr;
1357/*
1358 u_int32_t *ld;
1359 ld = xfer->recv.buf;
1360printf("sbp %x %d %d %08x %08x %08x %08x\n",
1361 xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1362printf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1363printf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1364*/
1365 if(xfer->resp != 0){
1366 printf("sbp_recv: xfer->resp != 0\n");
1367 fw_xfer_free( xfer);
1368 return;
1369 }
1370 if(xfer->recv.buf == NULL){
1371 printf("sbp_recv: xfer->recv.buf == NULL\n");
1372 fw_xfer_free( xfer);
1373 return;
1374 }
1375 sbp = (struct sbp_softc *)xfer->sc;
1376 rfp = (struct fw_pkt *)xfer->recv.buf;
1377 if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1378 printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1379 fw_xfer_free( xfer);
1380 return;
1381 }
1382 sbp_status = (struct sbp_status *)rfp->mode.wreqb.payload;
1383 addr = ntohl(rfp->mode.wreqb.dest_lo);
1384SBP_DEBUG(2)
1385 printf("received address 0x%x\n", addr);
1386END_DEBUG
1387 t = SBP_ADDR2TRG(addr);
1388 if (t >= SBP_NUM_TARGETS) {
1389 device_printf(sbp->fd.dev,
1390 "sbp_recv1: invalid target %d\n", t);
1391 fw_xfer_free(xfer);
1392 return;
1393 }
1394 target = &sbp->targets[t];
1395 l = SBP_ADDR2LUN(addr);
1396 if (l >= target->num_lun) {
1397 device_printf(sbp->fd.dev,
1398 "sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1399 fw_xfer_free(xfer);
1400 return;
1401 }
1402 sdev = &target->luns[l];
1403
1404 ocb = NULL;
1405 switch (sbp_status->src) {
1406 case 0:
1407 case 1:
1408 ocb = sbp_dequeue_ocb(sdev, ntohl(sbp_status->orb_lo));
1409 if (ocb == NULL) {
1410 sbp_show_sdev_info(sdev, 2);
1411 printf("No ocb on the queue\n");
1412 }
1413 break;
1414 case 2:
1415 /* unsolicit */
1416 sbp_show_sdev_info(sdev, 2);
1417 printf("unsolicit status received\n");
1418 break;
1419 default:
1420 sbp_show_sdev_info(sdev, 2);
1421 printf("unknown sbp_status->src\n");
1422 }
1423
1424 status_valid = (sbp_status->src < 2
1425 && sbp_status->resp == ORB_RES_CMPL
1426 && sbp_status->dead == 0
1427 && sbp_status->status == 0);
1428
1429 if (!status_valid || debug > 1){
1430 int status;
1431SBP_DEBUG(0)
1432 sbp_show_sdev_info(sdev, 2);
1433 printf("ORB status src:%x resp:%x dead:%x"
1434#if __FreeBSD_version >= 500000
1435 " len:%x stat:%x orb:%x%08x\n",
1436#else
1437 " len:%x stat:%x orb:%x%08lx\n",
1438#endif
1439 sbp_status->src, sbp_status->resp, sbp_status->dead,
1440 sbp_status->len, sbp_status->status,
1441 ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1442END_DEBUG
1443 sbp_show_sdev_info(sdev, 2);
1444 status = sbp_status->status;
1445 switch(sbp_status->resp) {
1446 case 0:
1447 if (status > MAX_ORB_STATUS0)
1448 printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1449 else
1450 printf("%s\n", orb_status0[status]);
1451 break;
1452 case 1:
1453 printf("Obj: %s, Error: %s\n",
1454 orb_status1_object[(status>>6) & 3],
1455 orb_status1_serial_bus_error[status & 0xf]);
1456 break;
1457 case 2:
1458 printf("Illegal request\n");
1459 break;
1460 case 3:
1461 printf("Vendor dependent\n");
1462 break;
1463 default:
1464 printf("unknown respose code %d\n", sbp_status->resp);
1465 }
1466 }
1467
1468 /* we have to reset the fetch agent if it's dead */
1469 if (sbp_status->dead) {
1470 if (sdev->path)
1471 xpt_freeze_devq(sdev->path, 1);
1472 sbp_agent_reset(sdev);
1473 }
1474
1475 if (ocb == NULL) {
1476 fw_xfer_free(xfer);
1477 return;
1478 }
1479
1480 sdev->flags &= ~SBP_DEV_TIMEOUT;
1481
1482 switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1483 case ORB_FMT_NOP:
1484 break;
1485 case ORB_FMT_VED:
1486 break;
1487 case ORB_FMT_STD:
1488 switch(ocb->flags & OCB_ACT_MASK){
1489 case OCB_ACT_MGM:
1490 orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1491 switch(orb_fun) {
1492 case ORB_FUN_LGI:
1493 login_res = &sdev->login;
1494 login_res->len = ntohs(login_res->len);
1495 login_res->id = ntohs(login_res->id);
1496 login_res->cmd_hi = ntohs(login_res->cmd_hi);
1497 login_res->cmd_lo = ntohl(login_res->cmd_lo);
1498 if (status_valid) {
1499SBP_DEBUG(0)
1500sbp_show_sdev_info(sdev, 2);
1501printf("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));
1502END_DEBUG
1503#if 1
1504 sbp_busy_timeout(sdev);
1505#else
1506 sbp_mgm_orb(sdev, ORB_FUN_ATS, 0, 0);
1507#endif
1508 } else {
1509 /* forgot logout? */
1510 sbp_show_sdev_info(sdev, 2);
1511 printf("login failed\n");
1512 sdev->status = SBP_DEV_RESET;
1513 }
1514 break;
1515 case ORB_FUN_RCN:
1516 login_res = &sdev->login;
1517 if (status_valid) {
1518SBP_DEBUG(0)
1519sbp_show_sdev_info(sdev, 2);
1520printf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo);
1521END_DEBUG
1522#if 1
1523 sbp_ping_unit(sdev);
1524 sdev->status = SBP_DEV_ATTACHED;
1525 xpt_release_devq(sdev->path, 1, TRUE);
1526#else
1527 sdev->status = SBP_DEV_ATTACHED;
1528 sbp_mgm_orb(sdev, ORB_FUN_ATS, 0, 0);
1529#endif
1530 } else {
1531 /* reconnection hold time exceed? */
1532SBP_DEBUG(0)
1533 sbp_show_sdev_info(sdev, 2);
1534 printf("reconnect failed\n");
1535END_DEBUG
1536 sbp_mgm_orb(sdev, ORB_FUN_LGI, 0, 0);
1537 }
1538 break;
1539 case ORB_FUN_LGO:
1540 sdev->status = SBP_DEV_RESET;
1541 break;
1542 case ORB_FUN_RST:
1543 sbp_busy_timeout(sdev);
1544 break;
1545 case ORB_FUN_LUR:
1546 case ORB_FUN_ATA:
1547 case ORB_FUN_ATS:
1548 sbp_agent_reset(sdev);
1549 break;
1550 default:
1551 sbp_show_sdev_info(sdev, 2);
1552 printf("unknown function %d\n", orb_fun);
1553 break;
1554 }
1555 break;
1556 case OCB_ACT_CMD:
1557 if(ocb->ccb != NULL){
1558 union ccb *ccb;
1559/*
1560 u_int32_t *ld;
1561 ld = ocb->ccb->csio.data_ptr;
1562 if(ld != NULL && ocb->ccb->csio.dxfer_len != 0)
1563 printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]);
1564 else
1565 printf("ptr NULL\n");
1566printf("len %d\n", sbp_status->len);
1567*/
1568 ccb = ocb->ccb;
1569 if(sbp_status->len > 1){
1570 sbp_scsi_status(sbp_status, ocb);
1571 }else{
1572 if(sbp_status->resp != ORB_RES_CMPL){
1573 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1574 }else{
1575 ccb->ccb_h.status = CAM_REQ_CMP;
1576 }
1577 }
1578 /* fix up inq data */
1579 if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1580 sbp_fix_inq_data(ocb);
1581 xpt_done(ccb);
1582 }
1583 break;
1584 default:
1585 break;
1586 }
1587 }
1588
1589 if (!(ocb->flags & OCB_RESERVED))
1590 sbp_free_ocb(sbp, ocb);
1591
1592/* The received packet is usually small enough to be stored within
1593 * the buffer. In that case, the controller return ack_complete and
1594 * no respose is necessary.
1595 *
1596 * XXX fwohci.c and firewire.c should inform event_code such as
1597 * ack_complete or ack_pending to upper driver.
1598 */
1599#if NEED_RESPONSE
1600 xfer->send.buf = malloc(12, M_SBP, M_NOWAIT | M_ZERO);
1601 xfer->send.len = 12;
1602 xfer->send.off = 0;
1603 sfp = (struct fw_pkt *)xfer->send.buf;
1604 sfp->mode.wres.dst = rfp->mode.wreqb.src;
1605 xfer->dst = ntohs(sfp->mode.wres.dst);
1606 xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1607 xfer->act.hand = sbp_loginres_callback;
1608 xfer->retry_req = fw_asybusy;
1609
1610 sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1611 sfp->mode.wres.tcode = FWTCODE_WRES;
1612 sfp->mode.wres.rtcode = 0;
1613 sfp->mode.wres.pri = 0;
1614
1615 fw_asyreq(xfer->fc, -1, xfer);
1616#else
1617 fw_xfer_free(xfer);
1618#endif
1619
1620 return;
1621
1622}
1623
1624static void
1625sbp_recv(struct fw_xfer *xfer)
1626{
1627 int s;
1628
1629 s = splcam();
1630 sbp_recv1(xfer);
1631 splx(s);
1632}
1633/*
1634 * sbp_attach()
1635 */
1636static int
1637sbp_attach(device_t dev)
1638{
1639 struct sbp_softc *sbp;
1640 struct cam_devq *devq;
1641 struct fw_xfer *xfer;
1642 int i, s, error;
1643
1644SBP_DEBUG(0)
1645 printf("sbp_attach\n");
1646END_DEBUG
1647
1648 sbp = ((struct sbp_softc *)device_get_softc(dev));
1649 bzero(sbp, sizeof(struct sbp_softc));
1650 sbp->fd.dev = dev;
1651 sbp->fd.fc = device_get_ivars(dev);
1652 error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
1653 /*boundary*/0,
1654 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1655 /*highaddr*/BUS_SPACE_MAXADDR,
1656 /*filter*/NULL, /*filterarg*/NULL,
1657 /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1658 /*maxsegsz*/0x8000,
1659 /*flags*/BUS_DMA_ALLOCNOW,
1660 &sbp->dmat);
1661 if (error != 0) {
1662 printf("sbp_attach: Could not allocate DMA tag "
1663 "- error %d\n", error);
1664 return (ENOMEM);
1665 }
1666
1667 devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1668 if (devq == NULL)
1669 return (ENXIO);
1670
1671 for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1672 sbp->targets[i].fwdev = NULL;
1673 sbp->targets[i].luns = NULL;
1674 }
1675
1676 sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1677 device_get_unit(dev),
1678 /*untagged*/ SBP_QUEUE_LEN,
1679 /*tagged*/0, devq);
1680
1681 if (sbp->sim == NULL) {
1682 cam_simq_free(devq);
1683 return (ENXIO);
1684 }
1685
1686 sbp->ocb = (struct sbp_ocb *) contigmalloc(
1687 sizeof (struct sbp_ocb) * SBP_NUM_OCB,
1688 M_SBP, M_NOWAIT, 0x10000, 0xffffffff, PAGE_SIZE, 0ul);
1689 bzero(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB);
1690
1691 if (sbp->ocb == NULL) {
1692 printf("sbp0: ocb alloction failure\n");
1693 return (ENOMEM);
1694 }
1695
1696 STAILQ_INIT(&sbp->free_ocbs);
1697 for (i = 0; i < SBP_NUM_OCB; i++) {
1698 sbp_free_ocb(sbp, &sbp->ocb[i]);
1699 }
1700
1701 if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS) {
1702 cam_sim_free(sbp->sim, /*free_devq*/TRUE);
1703 contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB,
1704 M_SBP);
1705 return (ENXIO);
1706 }
1707
1708 xfer = fw_xfer_alloc(M_SBP);
1709 xfer->act.hand = sbp_recv;
1710 xfer->act_type = FWACT_XFER;
1711#if NEED_RESPONSE
1712 xfer->fc = sbp->fd.fc;
1713#endif
1714 xfer->sc = (caddr_t)sbp;
1715
1716 sbp->fwb.start_hi = SBP_BIND_HI;
1717 sbp->fwb.start_lo = SBP_DEV2ADDR(device_get_unit(sbp->fd.dev), 0, 0);
1718 /* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1719 sbp->fwb.addrlen = 0xffff;
1720 sbp->fwb.xfer = xfer;
1721 fw_bindadd(sbp->fd.fc, &sbp->fwb);
1722
1723 sbp->fd.post_explore = sbp_post_explore;
1724 s = splfw();
1725 sbp_post_explore((void *)sbp);
1726 splx(s);
1727
1728 return (0);
1729}
1730
1731static int
1732sbp_logout_all(struct sbp_softc *sbp)
1733{
1734 struct sbp_target *target;
1735 struct sbp_dev *sdev;
1736 int i, j;
1737
1738SBP_DEBUG(0)
1739 printf("sbp_logout_all\n");
1740END_DEBUG
1741 for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
1742 target = &sbp->targets[i];
1743 if (target->luns == NULL)
1744 continue;
1745 for (j = 0; j < target->num_lun; j++) {
1746 sdev = &target->luns[j];
1747 if (sdev->status >= SBP_DEV_TOATTACH &&
1748 sdev->status <= SBP_DEV_ATTACHED)
1749 sbp_mgm_orb(sdev, ORB_FUN_LGO, 0, 0);
1750 }
1751 }
1752 return 0;
1753}
1754
1755static int
1756sbp_shutdown(device_t dev)
1757{
1758 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1759
1760 sbp_logout_all(sbp);
1761 return (0);
1762}
1763
1764static int
1765sbp_detach(device_t dev)
1766{
1767 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1768 struct firewire_comm *fc = sbp->fd.fc;
1769 int i;
1770
1771SBP_DEBUG(0)
1772 printf("sbp_detach\n");
1773END_DEBUG
1774#if 0
1775 /* bus reset for logout */
1776 sbp->fd.post_explore = NULL;
1777 fc->ibr(fc);
1778#endif
1779
1780 for (i = 0; i < SBP_NUM_TARGETS; i ++)
1781 sbp_cam_detach_target(&sbp->targets[i]);
1782 xpt_bus_deregister(cam_sim_path(sbp->sim));
1783
1784 sbp_logout_all(sbp);
1785 /* XXX wait for logout completion */
1786 tsleep(&i, FWPRI, "sbpdtc", hz/2);
1787
1788 fw_bindremove(fc, &sbp->fwb);
1789 contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB, M_SBP);
1790 bus_dma_tag_destroy(sbp->dmat);
1791
1792 for (i = 0; i < SBP_NUM_TARGETS; i ++)
1793 if (sbp->targets[i].luns != NULL)
1794 free(sbp->targets[i].luns, M_SBP);
1795
1796 return (0);
1797}
1798
1799static void
1800sbp_cam_detach_target(struct sbp_target *target)
1801{
1802 int i;
1803 struct sbp_dev *sdev;
1804
1805 if (target->luns != NULL) {
1806SBP_DEBUG(0)
1807 printf("sbp_detach_target %d\n", target->target_id);
1808END_DEBUG
1809 for (i = 0; i < target->num_lun; i++) {
1810 sdev = &target->luns[i];
1811 if (sdev->status == SBP_DEV_RESET ||
1812 sdev->status == SBP_DEV_DEAD)
1813 continue;
1814 if (sdev->path) {
1815 xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
1816 xpt_free_path(sdev->path);
1817 sdev->path = NULL;
1818 }
1819 sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
1820 }
1821 }
1822}
1823
1824static void
1825sbp_timeout(void *arg)
1826{
1827 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
1828 struct sbp_dev *sdev = ocb->sdev;
1829
1830 sbp_show_sdev_info(sdev, 2);
1831 printf("request timeout ... ");
1832
1833 xpt_freeze_devq(sdev->path, 1);
1834 sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
1835 if (sdev->flags & SBP_DEV_TIMEOUT) {
1836#if 0
1837 struct firewire_comm *fc;
1838
1839 printf("bus reset\n");
1840 fc = sdev->target->sbp->fd.fc;
1841 fc->ibr(fc);
1842 sdev->status == SBP_DEV_RETRY;
1843#else
1844 printf("target reset\n");
1845 sbp_mgm_orb(sdev, ORB_FUN_RST, 0, 0);
1846#endif
1847 sdev->flags &= ~SBP_DEV_TIMEOUT;
1848 } else {
1849 printf("agent reset\n");
1850 sdev->flags |= SBP_DEV_TIMEOUT;
1851 sbp_agent_reset(sdev);
1852 }
1853 return;
1854}
1855
1856static void
1857sbp_action1(struct cam_sim *sim, union ccb *ccb)
1858{
1859
1860 struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
1861 struct sbp_target *target = NULL;
1862 struct sbp_dev *sdev = NULL;
1863
1864 /* target:lun -> sdev mapping */
1865 if (sbp != NULL
1866 && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
1867 && ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
1868 target = &sbp->targets[ccb->ccb_h.target_id];
1869 if (target->fwdev != NULL
1870 && ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
1871 && ccb->ccb_h.target_lun < target->num_lun) {
1872 sdev = &target->luns[ccb->ccb_h.target_lun];
1873 if (sdev->status != SBP_DEV_ATTACHED &&
1874 sdev->status != SBP_DEV_PROBE)
1875 sdev = NULL;
1876 }
1877 }
1878
1879SBP_DEBUG(1)
1880 if (sdev == NULL)
1881 printf("invalid target %d lun %d\n",
1882 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
1883END_DEBUG
1884
1885 switch (ccb->ccb_h.func_code) {
1886 case XPT_SCSI_IO:
1887 case XPT_RESET_DEV:
1888 case XPT_GET_TRAN_SETTINGS:
1889 case XPT_SET_TRAN_SETTINGS:
1890 case XPT_CALC_GEOMETRY:
1891 if (sdev == NULL) {
1892SBP_DEBUG(1)
1893 printf("%s:%d:%d:func_code 0x%04x: "
1894 "Invalid target (target needed)\n",
1895 device_get_nameunit(sbp->fd.dev),
1896 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
1897 ccb->ccb_h.func_code);
1898END_DEBUG
1899
1900 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1901 xpt_done(ccb);
1902 return;
1903 }
1904 break;
1905 case XPT_PATH_INQ:
1906 case XPT_NOOP:
1907 /* The opcodes sometimes aimed at a target (sc is valid),
1908 * sometimes aimed at the SIM (sc is invalid and target is
1909 * CAM_TARGET_WILDCARD)
1910 */
1911 if (sbp == NULL &&
1912 ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
1913SBP_DEBUG(0)
1914 printf("%s:%d:%d func_code 0x%04x: "
1915 "Invalid target (no wildcard)\n",
1916 device_get_nameunit(sbp->fd.dev),
1917 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
1918 ccb->ccb_h.func_code);
1919END_DEBUG
1920 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1921 xpt_done(ccb);
1922 return;
1923 }
1924 break;
1925 default:
1926 /* XXX Hm, we should check the input parameters */
1927 break;
1928 }
1929
1930 switch (ccb->ccb_h.func_code) {
1931 case XPT_SCSI_IO:
1932 {
1933 struct ccb_scsiio *csio;
1934 struct sbp_ocb *ocb;
1935 int s, speed;
1936 void *cdb;
1937
1938 csio = &ccb->csio;
1939
1940SBP_DEBUG(1)
1941 printf("%s:%d:%d XPT_SCSI_IO: "
1942 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1943 ", flags: 0x%02x, "
1944 "%db cmd/%db data/%db sense\n",
1945 device_get_nameunit(sbp->fd.dev),
1946 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
1947 csio->cdb_io.cdb_bytes[0],
1948 csio->cdb_io.cdb_bytes[1],
1949 csio->cdb_io.cdb_bytes[2],
1950 csio->cdb_io.cdb_bytes[3],
1951 csio->cdb_io.cdb_bytes[4],
1952 csio->cdb_io.cdb_bytes[5],
1953 csio->cdb_io.cdb_bytes[6],
1954 csio->cdb_io.cdb_bytes[7],
1955 csio->cdb_io.cdb_bytes[8],
1956 csio->cdb_io.cdb_bytes[9],
1957 ccb->ccb_h.flags & CAM_DIR_MASK,
1958 csio->cdb_len, csio->dxfer_len,
1959 csio->sense_len);
1960END_DEBUG
1961 if(sdev == NULL){
1962 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1963 xpt_done(ccb);
1964 return;
1965 }
1966#if 0
1967 /* if we are in probe stage, pass only probe commands */
1968 if (sdev->status == SBP_DEV_PROBE) {
1969 char *name;
1970 name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
1971 printf("probe stage, periph name: %s\n", name);
1972 if (strcmp(name, "probe") != 0) {
1973 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1974 xpt_done(ccb);
1975 return;
1976 }
1977 }
1978#endif
1979 if ((ocb = sbp_get_ocb(sbp)) == NULL) {
1980 s = splfw();
1981 sbp->flags |= SBP_RESOURCE_SHORTAGE;
1982 splx(s);
1983 return;
1984 }
1985 ocb->flags = OCB_ACT_CMD;
1986 ocb->sdev = sdev;
1987 ocb->ccb = ccb;
1988 ccb->ccb_h.ccb_sdev_ptr = sdev;
1989 ocb->orb[0] = htonl(1 << 31);
1990 ocb->orb[1] = 0;
1991 ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
1992 ocb->orb[3] = htonl(vtophys(ocb->ind_ptr));
1993 speed = min(target->fwdev->speed, max_speed);
1994 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
1995 | ORB_CMD_MAXP(speed + 7));
1996 if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
1997 ocb->orb[4] |= htonl(ORB_CMD_IN);
1998 }
1999
2000 if (csio->ccb_h.flags & CAM_SCATTER_VALID)
2001 printf("sbp: CAM_SCATTER_VALID\n");
2002 if (csio->ccb_h.flags & CAM_DATA_PHYS)
2003 printf("sbp: CAM_DATA_PHYS\n");
2004
2005 if (csio->ccb_h.flags & CAM_CDB_POINTER)
2006 cdb = (void *)csio->cdb_io.cdb_ptr;
2007 else
2008 cdb = (void *)&csio->cdb_io.cdb_bytes;
2009 bcopy(cdb,
2010 (void *)(uintptr_t)(volatile void *)&ocb->orb[5],
2011 csio->cdb_len);
2012/*
2013printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2014printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2015*/
2016 if (ccb->csio.dxfer_len > 0) {
2017 int s;
2018
2019 if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
2020 printf("sbp_action1: cannot create dmamap\n");
2021 break;
2022 }
2023
2024 s = splsoftvm();
2025 bus_dmamap_load(/*dma tag*/sbp->dmat,
2026 /*dma map*/ocb->dmamap,
2027 ccb->csio.data_ptr,
2028 ccb->csio.dxfer_len,
2029 sbp_execute_ocb,
2030 ocb,
2031 /*flags*/0);
2032 splx(s);
2033 } else
2034 sbp_execute_ocb(ocb, NULL, 0, 0);
2035 break;
2036 }
2037 case XPT_CALC_GEOMETRY:
2038 {
2039 struct ccb_calc_geometry *ccg;
2040 u_int32_t size_mb;
2041 u_int32_t secs_per_cylinder;
2042 int extended = 1;
2043 ccg = &ccb->ccg;
2044
2045 if (ccg->block_size == 0) {
2046 printf("sbp_action1: block_size is 0.\n");
2047 ccb->ccb_h.status = CAM_REQ_INVALID;
2048 xpt_done(ccb);
2049 break;
2050 }
2051SBP_DEBUG(1)
2052 printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2053 "Volume size = %d\n",
2054 device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim),
2055 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2056 ccg->volume_size);
2057END_DEBUG
2058
2059 size_mb = ccg->volume_size
2060 / ((1024L * 1024L) / ccg->block_size);
2061
2062 if (size_mb >= 1024 && extended) {
2063 ccg->heads = 255;
2064 ccg->secs_per_track = 63;
2065 } else {
2066 ccg->heads = 64;
2067 ccg->secs_per_track = 32;
2068 }
2069 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2070 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2071 ccb->ccb_h.status = CAM_REQ_CMP;
2072 xpt_done(ccb);
2073 break;
2074 }
2075 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
2076 {
2077
2078SBP_DEBUG(1)
2079 printf("%s:%d:XPT_RESET_BUS: \n",
2080 device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2081END_DEBUG
2082
2083 ccb->ccb_h.status = CAM_REQ_INVALID;
2084 xpt_done(ccb);
2085 break;
2086 }
2087 case XPT_PATH_INQ: /* Path routing inquiry */
2088 {
2089 struct ccb_pathinq *cpi = &ccb->cpi;
2090
2091SBP_DEBUG(1)
2092 printf("%s:%d:%d XPT_PATH_INQ:.\n",
2093 device_get_nameunit(sbp->fd.dev),
2094 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2095END_DEBUG
2096 cpi->version_num = 1; /* XXX??? */
2097 cpi->hba_inquiry = 0;
2098 cpi->target_sprt = 0;
2099 cpi->hba_misc = 0;
2100 cpi->hba_eng_cnt = 0;
2101 cpi->max_target = SBP_NUM_TARGETS - 1;
2102 cpi->max_lun = SBP_NUM_LUNS - 1;
2103 cpi->initiator_id = SBP_INITIATOR;
2104 cpi->bus_id = sim->bus_id;
2105 cpi->base_transfer_speed = 400 * 1000 / 8;
2106 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2107 strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2108 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2109 cpi->unit_number = sim->unit_number;
2110
2111 cpi->ccb_h.status = CAM_REQ_CMP;
2112 xpt_done(ccb);
2113 break;
2114 }
2115 case XPT_GET_TRAN_SETTINGS:
2116 {
2117 struct ccb_trans_settings *cts = &ccb->cts;
2118SBP_DEBUG(1)
2119 printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2120 device_get_nameunit(sbp->fd.dev),
2121 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2122END_DEBUG
2123 /* Disable disconnect and tagged queuing */
2124 cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2125 cts->flags = 0;
2126
2127 cts->ccb_h.status = CAM_REQ_CMP;
2128 xpt_done(ccb);
2129 break;
2130 }
2131 case XPT_ABORT:
2132 ccb->ccb_h.status = CAM_UA_ABORT;
2133 xpt_done(ccb);
2134 break;
2135 default:
2136 ccb->ccb_h.status = CAM_REQ_INVALID;
2137 xpt_done(ccb);
2138 break;
2139 }
2140 return;
2141}
2142
2143static void
2144sbp_action(struct cam_sim *sim, union ccb *ccb)
2145{
2146 int s;
2147
2148 s = splfw();
2149 sbp_action1(sim, ccb);
2150 splx(s);
2151}
2152
2153static void
2154sbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error)
2155{
2156 int i;
2157 struct sbp_ocb *ocb;
2158 struct sbp_ocb *prev;
2159 union ccb *ccb;
2160 bus_dma_segment_t *s;
2161
2162 if (error)
2163 printf("sbp_execute_ocb: error=%d\n", error);
2164
2165 ocb = (struct sbp_ocb *)arg;
2166 if (seg == 1) {
2167 /* direct pointer */
2168 ocb->orb[3] = htonl(segments[0].ds_addr);
2169 ocb->orb[4] |= htonl(segments[0].ds_len);
2170 } else if(seg > 1) {
2171 /* page table */
2172SBP_DEBUG(1)
2173 printf("sbp_execute_ocb: seg %d", seg);
2174 for (i = 0; i < seg; i++)
2175#if __FreeBSD_version >= 500000
2176 printf(", %tx:%zd", segments[i].ds_addr,
2177#else
2178 printf(", %x:%d", segments[i].ds_addr,
2179#endif
2180 segments[i].ds_len);
2181 printf("\n");
2182END_DEBUG
2183 for (i = 0; i < seg; i++) {
2184 s = &segments[i];
2185SBP_DEBUG(0)
2186 /* XXX LSI Logic "< 16 byte" bug might be hit */
2187 if (s->ds_len < 16)
2188 printf("sbp_execute_ocb: warning, "
2189#if __FreeBSD_version >= 500000
2190 "segment length(%zd) is less than 16."
2191#else
2192 "segment length(%d) is less than 16."
2193#endif
2194 "(seg=%d/%d)\n", s->ds_len, i+1, seg);
2195END_DEBUG
2196 ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2197 ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2198 }
2199 ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2200 }
2201
2202 ccb = ocb->ccb;
2203 prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2204 if (prev)
2205 sbp_doorbell(ocb->sdev);
2206 else
2207 sbp_orb_pointer(ocb->sdev, ocb);
2208}
2209
2210static void
2211sbp_poll(struct cam_sim *sim)
2212{
2213 /* should call fwohci_intr? */
2214 return;
2215}
2216static struct sbp_ocb *
2217sbp_dequeue_ocb(struct sbp_dev *sdev, u_int32_t orb_lo)
2218{
2219 struct sbp_ocb *ocb;
2220 struct sbp_ocb *next;
2221 int s = splfw(), order = 0;
2222 int flags;
2223
2224 for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2225 next = STAILQ_NEXT(ocb, ocb);
2226 flags = ocb->flags;
2227SBP_DEBUG(1)
2228 sbp_show_sdev_info(sdev, 2);
2229#if __FreeBSD_version >= 500000
2230 printf("orb: 0x%tx next: 0x%x, flags %x\n",
2231#else
2232 printf("orb: 0x%x next: 0x%lx, flags %x\n",
2233#endif
2234 vtophys(&ocb->orb[0]), ntohl(ocb->orb[1]), flags);
2235END_DEBUG
2236 if (vtophys(&ocb->orb[0]) == orb_lo) {
2237 /* found */
2238 if (ocb->flags & OCB_RESERVED)
2239 ocb->flags |= OCB_DONE;
2240 else
2241 STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2242 if (ocb->ccb != NULL)
2243 untimeout(sbp_timeout, (caddr_t)ocb,
2244 ocb->ccb->ccb_h.timeout_ch);
2245 if (ocb->dmamap != NULL) {
2246 bus_dmamap_destroy(sdev->target->sbp->dmat,
2247 ocb->dmamap);
2248 ocb->dmamap = NULL;
2249 }
2250 break;
2251 } else {
2252 if ((ocb->flags & OCB_RESERVED) &&
2253 (ocb->flags & OCB_DONE)) {
2254 /* next orb must be fetched already */
2255 STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2256 sbp_free_ocb(sdev->target->sbp, ocb);
2257 } else
2258 order ++;
2259 }
2260 }
2261 splx(s);
2262SBP_DEBUG(0)
2263 if (ocb && order > 0) {
2264 sbp_show_sdev_info(sdev, 2);
2265 printf("unordered execution order:%d\n", order);
2266 }
2267END_DEBUG
2268 return (ocb);
2269}
2270
2271static struct sbp_ocb *
2272sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2273{
2274 int s = splfw();
2275 struct sbp_ocb *prev;
2276
2277SBP_DEBUG(2)
2278 sbp_show_sdev_info(sdev, 2);
2279#if __FreeBSD_version >= 500000
2280 printf("sbp_enqueue_ocb orb=0x%tx in physical memory\n", vtophys(&ocb->orb[0]));
2281#else
2282 printf("sbp_enqueue_ocb orb=0x%x in physical memory\n", vtophys(&ocb->orb[0]));
2283#endif
2284END_DEBUG
2285 prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2286 STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2287
2288 if (ocb->ccb != NULL)
2289 ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb,
2290 (ocb->ccb->ccb_h.timeout * hz) / 1000);
2291
2292 if (prev != NULL ) {
2293SBP_DEBUG(1)
2294#if __FreeBSD_version >= 500000
2295 printf("linking chain 0x%tx -> 0x%tx\n", vtophys(&prev->orb[0]),
2296#else
2297 printf("linking chain 0x%x -> 0x%x\n", vtophys(&prev->orb[0]),
2298#endif
2299 vtophys(&ocb->orb[0]));
2300END_DEBUG
2301 prev->flags |= OCB_RESERVED;
2302 prev->orb[1] = htonl(vtophys(&ocb->orb[0]));
2303 prev->orb[0] = 0;
2304 }
2305 splx(s);
2306
2307 return prev;
2308}
2309
2310static struct sbp_ocb *
2311sbp_get_ocb(struct sbp_softc *sbp)
2312{
2313 struct sbp_ocb *ocb;
2314 int s = splfw();
2315 ocb = STAILQ_FIRST(&sbp->free_ocbs);
2316 if (ocb == NULL) {
2317 printf("ocb shortage!!!\n");
2318 return NULL;
2319 }
2320 STAILQ_REMOVE(&sbp->free_ocbs, ocb, sbp_ocb, ocb);
2321 splx(s);
2322 ocb->ccb = NULL;
2323 return (ocb);
2324}
2325
2326static void
2327sbp_free_ocb(struct sbp_softc *sbp, struct sbp_ocb *ocb)
2328{
2329#if 0 /* XXX make sure that ocb has ccb */
2330 if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0 &&
2331 (ocb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
2332 ocb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2333 sbp->flags &= ~SBP_RESOURCE_SHORTAGE;
2334 }
2335#else
2336 if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0)
2337 sbp->flags &= ~SBP_RESOURCE_SHORTAGE;
2338#endif
2339 ocb->flags = 0;
2340 ocb->ccb = NULL;
2341 STAILQ_INSERT_TAIL(&sbp->free_ocbs, ocb, ocb);
2342}
2343
2344static void
2345sbp_abort_ocb(struct sbp_ocb *ocb, int status)
2346{
2347 struct sbp_dev *sdev;
2348
2349 sdev = ocb->sdev;
2350SBP_DEBUG(1)
2351 sbp_show_sdev_info(sdev, 2);
2352 printf("sbp_abort_ocb 0x%x\n", status);
2353 if (ocb->ccb != NULL)
2354 sbp_print_scsi_cmd(ocb);
2355END_DEBUG
2356 if (ocb->ccb != NULL && !(ocb->flags & OCB_DONE)) {
2357 untimeout(sbp_timeout, (caddr_t)ocb,
2358 ocb->ccb->ccb_h.timeout_ch);
2359 ocb->ccb->ccb_h.status = status;
2360 xpt_done(ocb->ccb);
2361 }
2362 if (ocb->dmamap != NULL) {
2363 bus_dmamap_destroy(sdev->target->sbp->dmat, ocb->dmamap);
2364 ocb->dmamap = NULL;
2365 }
2366 sbp_free_ocb(sdev->target->sbp, ocb);
2367}
2368
2369static void
2370sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2371{
2372 int s;
2373 struct sbp_ocb *ocb, *next;
2374 STAILQ_HEAD(, sbp_ocb) temp;
2375
2376 s = splfw();
2377
2378 bcopy(&sdev->ocbs, &temp, sizeof(temp));
2379 STAILQ_INIT(&sdev->ocbs);
2380 for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2381 next = STAILQ_NEXT(ocb, ocb);
2382 sbp_abort_ocb(ocb, status);
2383 }
2384
2385 splx(s);
2386}
2387
2388static devclass_t sbp_devclass;
2389
2390static device_method_t sbp_methods[] = {
2391 /* device interface */
2392 DEVMETHOD(device_identify, sbp_identify),
2393 DEVMETHOD(device_probe, sbp_probe),
2394 DEVMETHOD(device_attach, sbp_attach),
2395 DEVMETHOD(device_detach, sbp_detach),
2396 DEVMETHOD(device_shutdown, sbp_shutdown),
2397
2398 { 0, 0 }
2399};
2400
2401static driver_t sbp_driver = {
2402 "sbp",
2403 sbp_methods,
2404 sizeof(struct sbp_softc),
2405};
2406DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2407MODULE_VERSION(sbp, 1);
2408MODULE_DEPEND(sbp, firewire, 1, 1, 1);
2409MODULE_DEPEND(sbp, cam, 1, 1, 1);