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