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