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