sbp.c revision 1.17 1 /* $NetBSD: sbp.c,v 1.17 2007/07/09 21:00:41 ad Exp $ */
2 /*-
3 * Copyright (c) 2003 Hidetoshi Shimokawa
4 * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the acknowledgement as bellow:
17 *
18 * This product includes software developed by K. Kobayashi and H. Shimokawa
19 *
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 *
35 * $FreeBSD: /repoman/r/ncvs/src/sys/dev/firewire/sbp.c,v 1.89 2007/03/16 05:39:33 simokawa Exp $
36 *
37 */
38
39 #if defined(__FreeBSD__)
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/conf.h>
43 #include <sys/module.h>
44 #include <sys/bus.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <machine/bus.h>
48 #include <sys/malloc.h>
49 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #endif
53
54 #if defined(__DragonFly__) || __FreeBSD_version < 500106
55 #include <sys/devicestat.h> /* for struct devstat */
56 #endif
57
58 #ifdef __DragonFly__
59 #include <bus/cam/cam.h>
60 #include <bus/cam/cam_ccb.h>
61 #include <bus/cam/cam_sim.h>
62 #include <bus/cam/cam_xpt_sim.h>
63 #include <bus/cam/cam_debug.h>
64 #include <bus/cam/cam_periph.h>
65 #include <bus/cam/scsi/scsi_all.h>
66
67 #include <bus/firewire/fw_port.h>
68 #include <bus/firewire/firewire.h>
69 #include <bus/firewire/firewirereg.h>
70 #include <bus/firewire/fwdma.h>
71 #include <bus/firewire/iec13213.h>
72 #include "sbp.h"
73 #else
74 #include <cam/cam.h>
75 #include <cam/cam_ccb.h>
76 #include <cam/cam_sim.h>
77 #include <cam/cam_xpt_sim.h>
78 #include <cam/cam_debug.h>
79 #include <cam/cam_periph.h>
80 #include <cam/scsi/scsi_all.h>
81
82 #include <dev/firewire/fw_port.h>
83 #include <dev/firewire/firewire.h>
84 #include <dev/firewire/firewirereg.h>
85 #include <dev/firewire/fwdma.h>
86 #include <dev/firewire/iec13213.h>
87 #include <dev/firewire/sbp.h>
88 #endif
89 #elif defined(__NetBSD__)
90 #include <sys/param.h>
91 #include <sys/device.h>
92 #include <sys/errno.h>
93 #include <sys/buf.h>
94 #include <sys/kernel.h>
95 #include <sys/kthread.h>
96 #include <sys/malloc.h>
97 #include <sys/proc.h>
98 #include <sys/sysctl.h>
99
100 #include <machine/bus.h>
101
102 #include <dev/scsipi/scsi_spc.h>
103 #include <dev/scsipi/scsi_all.h>
104 #include <dev/scsipi/scsipi_all.h>
105 #include <dev/scsipi/scsiconf.h>
106 #include <dev/scsipi/scsipiconf.h>
107
108 #include <dev/ieee1394/fw_port.h>
109 #include <dev/ieee1394/firewire.h>
110 #include <dev/ieee1394/firewirereg.h>
111 #include <dev/ieee1394/fwdma.h>
112 #include <dev/ieee1394/iec13213.h>
113 #include <dev/ieee1394/sbp.h>
114
115 #include "locators.h"
116 #endif
117
118 #define ccb_sdev_ptr spriv_ptr0
119 #define ccb_sbp_ptr spriv_ptr1
120
121 #define SBP_NUM_TARGETS 8 /* MAX 64 */
122 /*
123 * Scan_bus doesn't work for more than 8 LUNs
124 * because of CAM_SCSI2_MAXLUN in cam_xpt.c
125 */
126 #define SBP_NUM_LUNS 64
127 #define SBP_MAXPHYS MIN(MAXPHYS, (512*1024) /* 512KB */)
128 #define SBP_DMA_SIZE PAGE_SIZE
129 #define SBP_LOGIN_SIZE sizeof(struct sbp_login_res)
130 #define SBP_QUEUE_LEN ((SBP_DMA_SIZE - SBP_LOGIN_SIZE) / sizeof(struct sbp_ocb))
131 #define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS)
132
133 /*
134 * STATUS FIFO addressing
135 * bit
136 * -----------------------
137 * 0- 1( 2): 0 (alignment)
138 * 2- 9( 8): lun
139 * 10-31(14): unit
140 * 32-47(16): SBP_BIND_HI
141 * 48-64(16): bus_id, node_id
142 */
143 #define SBP_BIND_HI 0x1
144 #define SBP_DEV2ADDR(u, l) \
145 (((u_int64_t)SBP_BIND_HI << 32) \
146 | (((u) & 0x3fff) << 10) \
147 | (((l) & 0xff) << 2))
148 #define SBP_ADDR2UNIT(a) (((a) >> 10) & 0x3fff)
149 #define SBP_ADDR2LUN(a) (((a) >> 2) & 0xff)
150 #define SBP_INITIATOR 7
151
152 static const char *orb_fun_name[] = {
153 ORB_FUN_NAMES
154 };
155
156 static int debug = 0;
157 static int auto_login = 1;
158 static int max_speed = -1;
159 static int sbp_cold = 1;
160 static int ex_login = 1;
161 static int login_delay = 1000; /* msec */
162 static int scan_delay = 500; /* msec */
163 static int use_doorbell = 0;
164 static int sbp_tags = 0;
165
166 #if defined(__FreeBSD__)
167 SYSCTL_DECL(_hw_firewire);
168 SYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem");
169 SYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0,
170 "SBP debug flag");
171 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0,
172 "SBP perform login automatically");
173 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0,
174 "SBP transfer max speed");
175 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, exclusive_login, CTLFLAG_RW,
176 &ex_login, 0, "SBP enable exclusive login");
177 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, login_delay, CTLFLAG_RW,
178 &login_delay, 0, "SBP login delay in msec");
179 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, scan_delay, CTLFLAG_RW,
180 &scan_delay, 0, "SBP scan delay in msec");
181 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, use_doorbell, CTLFLAG_RW,
182 &use_doorbell, 0, "SBP use doorbell request");
183 SYSCTL_INT(_hw_firewire_sbp, OID_AUTO, tags, CTLFLAG_RW, &sbp_tags, 0,
184 "SBP tagged queuing support");
185
186 TUNABLE_INT("hw.firewire.sbp.auto_login", &auto_login);
187 TUNABLE_INT("hw.firewire.sbp.max_speed", &max_speed);
188 TUNABLE_INT("hw.firewire.sbp.exclusive_login", &ex_login);
189 TUNABLE_INT("hw.firewire.sbp.login_delay", &login_delay);
190 TUNABLE_INT("hw.firewire.sbp.scan_delay", &scan_delay);
191 TUNABLE_INT("hw.firewire.sbp.use_doorbell", &use_doorbell);
192 TUNABLE_INT("hw.firewire.sbp.tags", &sbp_tags);
193 #elif defined(__NetBSD__)
194 static int sysctl_sbp_verify(SYSCTLFN_PROTO, int lower, int upper);
195 static int sysctl_sbp_verify_max_speed(SYSCTLFN_PROTO);
196 static int sysctl_sbp_verify_tags(SYSCTLFN_PROTO);
197
198 /*
199 * Setup sysctl(3) MIB, hw.sbp.*
200 *
201 * TBD condition CTLFLAG_PERMANENT on being an LKM or not
202 */
203 SYSCTL_SETUP(sysctl_sbp, "sysctl sbp(4) subtree setup")
204 {
205 int rc, sbp_node_num;
206 const struct sysctlnode *node;
207
208 if ((rc = sysctl_createv(clog, 0, NULL, NULL,
209 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
210 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
211 goto err;
212 }
213
214 if ((rc = sysctl_createv(clog, 0, NULL, &node,
215 CTLFLAG_PERMANENT, CTLTYPE_NODE, "sbp",
216 SYSCTL_DESCR("sbp controls"), NULL, 0, NULL,
217 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
218 goto err;
219 }
220 sbp_node_num = node->sysctl_num;
221
222 /* sbp auto login flag */
223 if ((rc = sysctl_createv(clog, 0, NULL, &node,
224 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
225 "auto_login", SYSCTL_DESCR("SBP perform login automatically"),
226 NULL, 0, &auto_login,
227 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0) {
228 goto err;
229 }
230
231 /* sbp max speed */
232 if ((rc = sysctl_createv(clog, 0, NULL, &node,
233 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
234 "max_speed", SYSCTL_DESCR("SBP transfer max speed"),
235 sysctl_sbp_verify_max_speed, 0, &max_speed,
236 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0) {
237 goto err;
238 }
239
240 /* sbp exclusive login flag */
241 if ((rc = sysctl_createv(clog, 0, NULL, &node,
242 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
243 "exclusive_login", SYSCTL_DESCR("SBP enable exclusive login"),
244 NULL, 0, &ex_login,
245 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0) {
246 goto err;
247 }
248
249 /* sbp login delay */
250 if ((rc = sysctl_createv(clog, 0, NULL, &node,
251 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
252 "login_delay", SYSCTL_DESCR("SBP login delay in msec"),
253 NULL, 0, &login_delay,
254 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0) {
255 goto err;
256 }
257
258 /* sbp scan delay */
259 if ((rc = sysctl_createv(clog, 0, NULL, &node,
260 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
261 "scan_delay", SYSCTL_DESCR("SBP scan delay in msec"),
262 NULL, 0, &scan_delay,
263 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0) {
264 goto err;
265 }
266
267 /* sbp use doorbell flag */
268 if ((rc = sysctl_createv(clog, 0, NULL, &node,
269 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
270 "use_doorbell", SYSCTL_DESCR("SBP use doorbell request"),
271 NULL, 0, &use_doorbell,
272 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0) {
273 goto err;
274 }
275
276 /* sbp force tagged queuing */
277 if ((rc = sysctl_createv(clog, 0, NULL, &node,
278 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
279 "tags", SYSCTL_DESCR("SBP tagged queuing support"),
280 sysctl_sbp_verify_tags, 0, &sbp_tags,
281 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0) {
282 goto err;
283 }
284
285 /* sbp driver debug flag */
286 if ((rc = sysctl_createv(clog, 0, NULL, &node,
287 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
288 "sbp_debug", SYSCTL_DESCR("SBP debug flag"),
289 NULL, 0, &debug,
290 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0) {
291 goto err;
292 }
293
294 return;
295
296 err:
297 printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
298 }
299
300 static int
301 sysctl_sbp_verify(SYSCTLFN_ARGS, int lower, int upper)
302 {
303 int error, t;
304 struct sysctlnode node;
305
306 node = *rnode;
307 t = *(int*)rnode->sysctl_data;
308 node.sysctl_data = &t;
309 error = sysctl_lookup(SYSCTLFN_CALL(&node));
310 if (error || newp == NULL)
311 return (error);
312
313 if (t < lower || t > upper)
314 return (EINVAL);
315
316 *(int*)rnode->sysctl_data = t;
317
318 return (0);
319 }
320
321 static int
322 sysctl_sbp_verify_max_speed(SYSCTLFN_ARGS)
323 {
324 return (sysctl_sbp_verify(SYSCTLFN_CALL(rnode), 0, FWSPD_S400));
325 }
326
327 static int
328 sysctl_sbp_verify_tags(SYSCTLFN_ARGS)
329 {
330 return (sysctl_sbp_verify(SYSCTLFN_CALL(rnode), -1, 1));
331 }
332 #endif
333
334 #define NEED_RESPONSE 0
335
336 #define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE)
337 #ifdef __sparc64__ /* iommu */
338 #define SBP_IND_MAX howmany(SBP_MAXPHYS, SBP_SEG_MAX)
339 #else
340 #define SBP_IND_MAX howmany(SBP_MAXPHYS, PAGE_SIZE)
341 #endif
342 struct sbp_ocb {
343 STAILQ_ENTRY(sbp_ocb) ocb;
344 sbp_scsi_xfer *sxfer;
345 bus_addr_t bus_addr;
346 uint32_t orb[8];
347 #define IND_PTR_OFFSET (8*sizeof(uint32_t))
348 struct ind_ptr ind_ptr[SBP_IND_MAX];
349 struct sbp_dev *sdev;
350 int flags; /* XXX should be removed */
351 bus_dmamap_t dmamap;
352 };
353
354 #define OCB_ACT_MGM 0
355 #define OCB_ACT_CMD 1
356 #define OCB_MATCH(o,s) ((o)->bus_addr == ntohl((s)->orb_lo))
357
358 struct sbp_dev{
359 #define SBP_DEV_RESET 0 /* accept login */
360 #define SBP_DEV_LOGIN 1 /* to login */
361 #if 0
362 #define SBP_DEV_RECONN 2 /* to reconnect */
363 #endif
364 #define SBP_DEV_TOATTACH 3 /* to attach */
365 #define SBP_DEV_PROBE 4 /* scan lun */
366 #define SBP_DEV_ATTACHED 5 /* in operation */
367 #define SBP_DEV_DEAD 6 /* unavailable unit */
368 #define SBP_DEV_RETRY 7 /* unavailable unit */
369 uint8_t status:4,
370 timeout:4;
371 uint8_t type;
372 uint16_t lun_id;
373 uint16_t freeze;
374 #define ORB_LINK_DEAD (1 << 0)
375 #define VALID_LUN (1 << 1)
376 #define ORB_POINTER_ACTIVE (1 << 2)
377 #define ORB_POINTER_NEED (1 << 3)
378 #define ORB_DOORBELL_ACTIVE (1 << 4)
379 #define ORB_DOORBELL_NEED (1 << 5)
380 #define ORB_SHORTAGE (1 << 6)
381 uint16_t flags;
382 #if defined(__FreeBSD__)
383 struct cam_path *path;
384 #elif defined(__NetBSD__)
385 struct scsipi_periph *periph;
386 #endif
387 struct sbp_target *target;
388 struct fwdma_alloc dma;
389 struct sbp_login_res *login;
390 struct callout login_callout;
391 struct sbp_ocb *ocb;
392 STAILQ_HEAD(, sbp_ocb) ocbs;
393 STAILQ_HEAD(, sbp_ocb) free_ocbs;
394 struct sbp_ocb *last_ocb;
395 char vendor[32];
396 char product[32];
397 char revision[10];
398 };
399
400 struct sbp_target {
401 int target_id;
402 int num_lun;
403 struct sbp_dev **luns;
404 struct sbp_softc *sbp;
405 struct fw_device *fwdev;
406 uint32_t mgm_hi, mgm_lo;
407 struct sbp_ocb *mgm_ocb_cur;
408 STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue;
409 struct callout mgm_ocb_timeout;
410 struct callout scan_callout;
411 STAILQ_HEAD(, fw_xfer) xferlist;
412 int n_xfer;
413 };
414
415 struct sbp_softc {
416 struct firewire_dev_comm fd;
417 #if defined(__FreeBSD__)
418 struct cam_sim *sim;
419 struct cam_path *path;
420 #elif defined(__NetBSD__)
421 struct scsipi_adapter sc_adapter;
422 struct scsipi_channel sc_channel;
423 struct device *sc_bus;
424 struct lwp *lwp;
425 #endif
426 struct sbp_target target;
427 struct fw_bind fwb;
428 fw_bus_dma_tag_t dmat;
429 struct timeval last_busreset;
430 #define SIMQ_FREEZED 1
431 int flags;
432 };
433
434 #if defined(__NetBSD__)
435 int sbpmatch (struct device *, struct cfdata *, void *);
436 void sbpattach (struct device *parent, struct device *self, void *aux);
437 int sbpdetach (struct device *self, int flags);
438 #endif
439 static void sbp_post_explore (void *);
440 static void sbp_recv (struct fw_xfer *);
441 static void sbp_mgm_callback (struct fw_xfer *);
442 #if 0
443 static void sbp_cmd_callback (struct fw_xfer *);
444 #endif
445 static void sbp_orb_pointer (struct sbp_dev *, struct sbp_ocb *);
446 static void sbp_doorbell(struct sbp_dev *);
447 static void sbp_execute_ocb (void *, bus_dma_segment_t *, int, int);
448 static void sbp_free_ocb (struct sbp_dev *, struct sbp_ocb *);
449 static void sbp_abort_ocb (struct sbp_ocb *, int);
450 static void sbp_abort_all_ocbs (struct sbp_dev *, int);
451 static struct fw_xfer * sbp_write_cmd (struct sbp_dev *, int, int);
452 static struct sbp_ocb * sbp_get_ocb (struct sbp_dev *);
453 static struct sbp_ocb * sbp_enqueue_ocb (struct sbp_dev *, struct sbp_ocb *);
454 static struct sbp_ocb * sbp_dequeue_ocb (struct sbp_dev *, struct sbp_status *);
455 static void sbp_free_sdev(struct sbp_dev *);
456 static void sbp_free_target (struct sbp_target *);
457 static void sbp_mgm_timeout (void *arg);
458 static void sbp_timeout (void *arg);
459 static void sbp_mgm_orb (struct sbp_dev *, int, struct sbp_ocb *);
460 #if defined(__FreeBSD__)
461
462 MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire");
463 #elif defined(__NetBSD__)
464 static void sbp_scsipi_request(
465 struct scsipi_channel *, scsipi_adapter_req_t, void *);
466 static void sbp_minphys(struct buf *);
467
468 MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/IEEE1394");
469 #endif
470
471 #if defined(__FreeBSD__)
472 /* cam related functions */
473 static void sbp_action(struct cam_sim *, sbp_scsi_xfer *sxfer);
474 static void sbp_poll(struct cam_sim *);
475 static void sbp_cam_scan_lun(struct cam_periph *, sbp_scsi_xfer *);
476 static void sbp_cam_scan_target(void *);
477 static void sbp_cam_detach_sdev(struct sbp_dev *);
478 static void sbp_cam_detach_target (struct sbp_target *);
479 #define SBP_DETACH_SDEV(sd) sbp_cam_detach_sdev((sd))
480 #define SBP_DETACH_TARGET(st) sbp_cam_detach_target((st))
481 #elif defined(__NetBSD__)
482 /* scsipi related functions */
483 static void fw_kthread_create0(void *);
484 static void sbp_scsipi_scan_target(void *);
485 static void sbp_scsipi_detach_sdev(struct sbp_dev *);
486 static void sbp_scsipi_detach_target (struct sbp_target *);
487 #define SBP_DETACH_SDEV(sd) sbp_scsipi_detach_sdev((sd))
488 #define SBP_DETACH_TARGET(st) sbp_scsipi_detach_target((st))
489 #endif
490
491 static const char *orb_status0[] = {
492 /* 0 */ "No additional information to report",
493 /* 1 */ "Request type not supported",
494 /* 2 */ "Speed not supported",
495 /* 3 */ "Page size not supported",
496 /* 4 */ "Access denied",
497 /* 5 */ "Logical unit not supported",
498 /* 6 */ "Maximum payload too small",
499 /* 7 */ "Reserved for future standardization",
500 /* 8 */ "Resources unavailable",
501 /* 9 */ "Function rejected",
502 /* A */ "Login ID not recognized",
503 /* B */ "Dummy ORB completed",
504 /* C */ "Request aborted",
505 /* FF */ "Unspecified error"
506 #define MAX_ORB_STATUS0 0xd
507 };
508
509 static const char *orb_status1_object[] = {
510 /* 0 */ "Operation request block (ORB)",
511 /* 1 */ "Data buffer",
512 /* 2 */ "Page table",
513 /* 3 */ "Unable to specify"
514 };
515
516 static const char *orb_status1_serial_bus_error[] = {
517 /* 0 */ "Missing acknowledge",
518 /* 1 */ "Reserved; not to be used",
519 /* 2 */ "Time-out error",
520 /* 3 */ "Reserved; not to be used",
521 /* 4 */ "Busy retry limit exceeded(X)",
522 /* 5 */ "Busy retry limit exceeded(A)",
523 /* 6 */ "Busy retry limit exceeded(B)",
524 /* 7 */ "Reserved for future standardization",
525 /* 8 */ "Reserved for future standardization",
526 /* 9 */ "Reserved for future standardization",
527 /* A */ "Reserved for future standardization",
528 /* B */ "Tardy retry limit exceeded",
529 /* C */ "Conflict error",
530 /* D */ "Data error",
531 /* E */ "Type error",
532 /* F */ "Address error"
533 };
534
535 #if defined(__FreeBSD__)
536 #if 0
537 static void
538 sbp_identify(driver_t *driver, device_t parent)
539 {
540 device_t child;
541 SBP_DEBUG(0)
542 printf("sbp_identify\n");
543 END_DEBUG
544
545 child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent));
546 }
547 #endif
548
549 /*
550 * sbp_probe()
551 */
552 static int
553 sbp_probe(device_t dev)
554 {
555 device_t pa;
556
557 SBP_DEBUG(0)
558 printf("sbp_probe\n");
559 END_DEBUG
560
561 pa = device_get_parent(dev);
562 if(device_get_unit(dev) != device_get_unit(pa)){
563 return(ENXIO);
564 }
565
566 device_set_desc(dev, "SBP-2/SCSI over FireWire");
567
568 #if 0
569 if (bootverbose)
570 debug = bootverbose;
571 #endif
572
573 return (0);
574 }
575 #elif defined(__NetBSD__)
576 int
577 sbpmatch(struct device *parent, struct cfdata *cf, void *aux)
578 {
579 struct fw_attach_args *fwa = aux;
580
581 if (strcmp(fwa->name, "sbp") == 0)
582 return (1);
583 return (0);
584 }
585 #endif
586
587 static void
588 sbp_show_sdev_info(struct sbp_dev *sdev, int new)
589 {
590 struct fw_device *fwdev;
591
592 printf("%s:%d:%d ",
593 device_get_nameunit(sdev->target->sbp->fd.dev),
594 sdev->target->target_id,
595 sdev->lun_id
596 );
597 if (new == 2) {
598 return;
599 }
600 fwdev = sdev->target->fwdev;
601 printf("ordered:%d type:%d EUI:%08x%08x node:%d "
602 "speed:%d maxrec:%d",
603 (sdev->type & 0x40) >> 6,
604 (sdev->type & 0x1f),
605 fwdev->eui.hi,
606 fwdev->eui.lo,
607 fwdev->dst,
608 fwdev->speed,
609 fwdev->maxrec
610 );
611 if (new)
612 printf(" new!\n");
613 else
614 printf("\n");
615 sbp_show_sdev_info(sdev, 2);
616 printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision);
617 }
618
619 static void
620 sbp_alloc_lun(struct sbp_target *target)
621 {
622 struct crom_context cc;
623 struct csrreg *reg;
624 struct sbp_dev *sdev, **newluns;
625 struct sbp_softc *sbp;
626 int maxlun, lun, i;
627
628 sbp = target->sbp;
629 crom_init_context(&cc, target->fwdev->csrrom);
630 /* XXX shoud parse appropriate unit directories only */
631 maxlun = -1;
632 while (cc.depth >= 0) {
633 reg = crom_search_key(&cc, CROM_LUN);
634 if (reg == NULL)
635 break;
636 lun = reg->val & 0xffff;
637 SBP_DEBUG(0)
638 printf("target %d lun %d found\n", target->target_id, lun);
639 END_DEBUG
640 if (maxlun < lun)
641 maxlun = lun;
642 crom_next(&cc);
643 }
644 if (maxlun < 0)
645 printf("%s:%d no LUN found\n",
646 device_get_nameunit(target->sbp->fd.dev),
647 target->target_id);
648
649 maxlun ++;
650 if (maxlun >= SBP_NUM_LUNS)
651 maxlun = SBP_NUM_LUNS;
652
653 /* Invalidiate stale devices */
654 for (lun = 0; lun < target->num_lun; lun ++) {
655 sdev = target->luns[lun];
656 if (sdev == NULL)
657 continue;
658 sdev->flags &= ~VALID_LUN;
659 if (lun >= maxlun) {
660 /* lost device */
661 SBP_DETACH_SDEV(sdev);
662 sbp_free_sdev(sdev);
663 }
664 }
665
666 /* Reallocate */
667 if (maxlun != target->num_lun) {
668 newluns = (struct sbp_dev **) realloc(target->luns,
669 sizeof(struct sbp_dev *) * maxlun,
670 M_SBP, M_NOWAIT | M_ZERO);
671
672 if (newluns == NULL) {
673 printf("%s: realloc failed\n", __func__);
674 newluns = target->luns;
675 maxlun = target->num_lun;
676 }
677
678 /*
679 * We must zero the extended region for the case
680 * realloc() doesn't allocate new buffer.
681 */
682 if (maxlun > target->num_lun)
683 bzero(&newluns[target->num_lun],
684 sizeof(struct sbp_dev *) *
685 (maxlun - target->num_lun));
686
687 target->luns = newluns;
688 target->num_lun = maxlun;
689 }
690
691 crom_init_context(&cc, target->fwdev->csrrom);
692 while (cc.depth >= 0) {
693 int new = 0;
694
695 reg = crom_search_key(&cc, CROM_LUN);
696 if (reg == NULL)
697 break;
698 lun = reg->val & 0xffff;
699 if (lun >= SBP_NUM_LUNS) {
700 printf("too large lun %d\n", lun);
701 goto next;
702 }
703
704 sdev = target->luns[lun];
705 if (sdev == NULL) {
706 sdev = malloc(sizeof(struct sbp_dev),
707 M_SBP, M_NOWAIT | M_ZERO);
708 if (sdev == NULL) {
709 printf("%s: malloc failed\n", __func__);
710 goto next;
711 }
712 target->luns[lun] = sdev;
713 sdev->lun_id = lun;
714 sdev->target = target;
715 STAILQ_INIT(&sdev->ocbs);
716 CALLOUT_INIT(&sdev->login_callout);
717 sdev->status = SBP_DEV_RESET;
718 new = 1;
719 SBP_DEVICE_PREATTACH();
720 }
721 sdev->flags |= VALID_LUN;
722 sdev->type = (reg->val & 0xff0000) >> 16;
723
724 if (new == 0)
725 goto next;
726
727 fwdma_malloc(sbp->fd.fc,
728 /* alignment */ sizeof(uint32_t),
729 SBP_DMA_SIZE, &sdev->dma, BUS_DMA_NOWAIT);
730 if (sdev->dma.v_addr == NULL) {
731 printf("%s: dma space allocation failed\n",
732 __func__);
733 free(sdev, M_SBP);
734 target->luns[lun] = NULL;
735 goto next;
736 }
737 sdev->login = (struct sbp_login_res *) sdev->dma.v_addr;
738 sdev->ocb = (struct sbp_ocb *)
739 ((char *)sdev->dma.v_addr + SBP_LOGIN_SIZE);
740 bzero((char *)sdev->ocb,
741 sizeof (struct sbp_ocb) * SBP_QUEUE_LEN);
742
743 STAILQ_INIT(&sdev->free_ocbs);
744 for (i = 0; i < SBP_QUEUE_LEN; i++) {
745 struct sbp_ocb *ocb;
746 ocb = &sdev->ocb[i];
747 ocb->bus_addr = sdev->dma.bus_addr
748 + SBP_LOGIN_SIZE
749 + sizeof(struct sbp_ocb) * i
750 + offsetof(struct sbp_ocb, orb[0]);
751 if (fw_bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
752 printf("sbp_attach: cannot create dmamap\n");
753 /* XXX */
754 goto next;
755 }
756 sbp_free_ocb(sdev, ocb);
757 }
758 next:
759 crom_next(&cc);
760 }
761
762 for (lun = 0; lun < target->num_lun; lun ++) {
763 sdev = target->luns[lun];
764 if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) {
765 SBP_DETACH_SDEV(sdev);
766 sbp_free_sdev(sdev);
767 target->luns[lun] = NULL;
768 }
769 }
770 }
771
772 static struct sbp_target *
773 sbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
774 {
775 struct sbp_target *target;
776 struct crom_context cc;
777 struct csrreg *reg;
778
779 SBP_DEBUG(1)
780 printf("sbp_alloc_target\n");
781 END_DEBUG
782 /* new target */
783 target = &sbp->target;
784 target->sbp = sbp;
785 target->fwdev = fwdev;
786 target->target_id = 0;
787 /* XXX we may want to reload mgm port after each bus reset */
788 /* XXX there might be multiple management agents */
789 crom_init_context(&cc, target->fwdev->csrrom);
790 reg = crom_search_key(&cc, CROM_MGM);
791 if (reg == NULL || reg->val == 0) {
792 printf("NULL management address\n");
793 target->fwdev = NULL;
794 return NULL;
795 }
796 target->mgm_hi = 0xffff;
797 target->mgm_lo = 0xf0000000 | (reg->val << 2);
798 target->mgm_ocb_cur = NULL;
799 SBP_DEBUG(1)
800 printf("target: mgm_port: %x\n", target->mgm_lo);
801 END_DEBUG
802 STAILQ_INIT(&target->xferlist);
803 target->n_xfer = 0;
804 STAILQ_INIT(&target->mgm_ocb_queue);
805 CALLOUT_INIT(&target->mgm_ocb_timeout);
806 CALLOUT_INIT(&target->scan_callout);
807
808 target->luns = NULL;
809 target->num_lun = 0;
810 return target;
811 }
812
813 static void
814 sbp_probe_lun(struct sbp_dev *sdev)
815 {
816 struct fw_device *fwdev;
817 struct crom_context c, *cc = &c;
818 struct csrreg *reg;
819
820 bzero(sdev->vendor, sizeof(sdev->vendor));
821 bzero(sdev->product, sizeof(sdev->product));
822
823 fwdev = sdev->target->fwdev;
824 crom_init_context(cc, fwdev->csrrom);
825 /* get vendor string */
826 crom_search_key(cc, CSRKEY_VENDOR);
827 crom_next(cc);
828 crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor));
829 /* skip to the unit directory for SBP-2 */
830 while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) {
831 if (reg->val == CSRVAL_T10SBP2)
832 break;
833 crom_next(cc);
834 }
835 /* get firmware revision */
836 reg = crom_search_key(cc, CSRKEY_FIRM_VER);
837 if (reg != NULL)
838 snprintf(sdev->revision, sizeof(sdev->revision),
839 "%06x", reg->val);
840 /* get product string */
841 crom_search_key(cc, CSRKEY_MODEL);
842 crom_next(cc);
843 crom_parse_text(cc, sdev->product, sizeof(sdev->product));
844 }
845
846 static void
847 sbp_login_callout(void *arg)
848 {
849 struct sbp_dev *sdev = (struct sbp_dev *)arg;
850 sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
851 }
852
853 static void
854 sbp_login(struct sbp_dev *sdev)
855 {
856 struct timeval delta;
857 struct timeval t;
858 int ticks = 0;
859
860 microtime(&delta);
861 timevalsub(&delta, &sdev->target->sbp->last_busreset);
862 t.tv_sec = login_delay / 1000;
863 t.tv_usec = (login_delay % 1000) * 1000;
864 timevalsub(&t, &delta);
865 if (t.tv_sec >= 0 && t.tv_usec > 0)
866 ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000;
867 SBP_DEBUG(0)
868 printf("%s: sec = %jd usec = %ld ticks = %d\n", __func__,
869 (intmax_t)t.tv_sec, t.tv_usec, ticks);
870 END_DEBUG
871 callout_reset(&sdev->login_callout, ticks,
872 sbp_login_callout, (void *)(sdev));
873 }
874
875 static void
876 sbp_probe_target(void *arg)
877 {
878 struct sbp_target *target = (struct sbp_target *)arg;
879 struct sbp_softc *sbp;
880 struct sbp_dev *sdev;
881 struct firewire_comm *fc;
882 int i;
883
884 SBP_DEBUG(1)
885 printf("sbp_probe_target %d\n", target->target_id);
886 END_DEBUG
887
888 sbp = target->sbp;
889 fc = target->sbp->fd.fc;
890 sbp_alloc_lun(target);
891
892 /* XXX untimeout mgm_ocb and dequeue */
893 for (i=0; i < target->num_lun; i++) {
894 sdev = target->luns[i];
895 if (sdev == NULL)
896 continue;
897 if (sdev->status != SBP_DEV_DEAD) {
898 if (SBP_DEVICE(sdev) != NULL) {
899 SBP_DEVICE_FREEZE(sdev, 1);
900 sdev->freeze ++;
901 }
902 sbp_probe_lun(sdev);
903 SBP_DEBUG(0)
904 sbp_show_sdev_info(sdev,
905 (sdev->status == SBP_DEV_RESET));
906 END_DEBUG
907
908 sbp_abort_all_ocbs(sdev, XS_SCSI_BUS_RESET);
909 switch (sdev->status) {
910 case SBP_DEV_RESET:
911 /* new or revived target */
912 if (auto_login)
913 sbp_login(sdev);
914 break;
915 case SBP_DEV_TOATTACH:
916 case SBP_DEV_PROBE:
917 case SBP_DEV_ATTACHED:
918 case SBP_DEV_RETRY:
919 default:
920 sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
921 break;
922 }
923 } else {
924 switch (sdev->status) {
925 case SBP_DEV_ATTACHED:
926 SBP_DEBUG(0)
927 /* the device has gone */
928 sbp_show_sdev_info(sdev, 2);
929 printf("lost target\n");
930 END_DEBUG
931 if (SBP_DEVICE(sdev) != NULL) {
932 SBP_DEVICE_FREEZE(sdev, 1);
933 sdev->freeze ++;
934 }
935 sdev->status = SBP_DEV_RETRY;
936 sbp_abort_all_ocbs(sdev, XS_SCSI_BUS_RESET);
937 break;
938 case SBP_DEV_PROBE:
939 case SBP_DEV_TOATTACH:
940 sdev->status = SBP_DEV_RESET;
941 break;
942 case SBP_DEV_RETRY:
943 case SBP_DEV_RESET:
944 case SBP_DEV_DEAD:
945 break;
946 }
947 }
948 }
949 }
950
951 #define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
952 && crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
953
954 static void
955 sbp_post_busreset(void *arg)
956 {
957 struct sbp_softc *sbp = (struct sbp_softc *)arg;
958 struct sbp_target *target = &sbp->target;
959 struct fw_device *fwdev = target->fwdev;
960 int alive;
961
962 alive = SBP_FWDEV_ALIVE(fwdev);
963 SBP_DEBUG(0)
964 printf("sbp_post_busreset\n");
965 if (!alive)
966 printf("not alive\n");
967 END_DEBUG
968 microtime(&sbp->last_busreset);
969
970 if (!alive)
971 return;
972
973 SBP_BUS_FREEZE(sbp);
974 }
975
976 static void
977 sbp_post_explore(void *arg)
978 {
979 struct sbp_softc *sbp = (struct sbp_softc *)arg;
980 struct sbp_target *target = &sbp->target;
981 struct fw_device *fwdev = target->fwdev;
982 int alive;
983
984 alive = SBP_FWDEV_ALIVE(fwdev);
985 SBP_DEBUG(0)
986 printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
987 if (!alive)
988 printf("not alive\n");
989 END_DEBUG
990 if (!alive)
991 return;
992
993 if (sbp_cold > 0)
994 sbp_cold --;
995
996 #if 0
997 /*
998 * XXX don't let CAM the bus rest.
999 * CAM tries to do something with freezed (DEV_RETRY) devices.
1000 */
1001 xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
1002 #endif
1003
1004 SBP_DEBUG(0)
1005 printf("sbp_post_explore: EUI:%08x%08x ", fwdev->eui.hi, fwdev->eui.lo);
1006 END_DEBUG
1007 sbp_probe_target((void *)target);
1008 if (target->num_lun == 0)
1009 sbp_free_target(target);
1010
1011 SBP_BUS_THAW(sbp);
1012 }
1013
1014 #if NEED_RESPONSE
1015 static void
1016 sbp_loginres_callback(struct fw_xfer *xfer){
1017 int s;
1018 struct sbp_dev *sdev;
1019 sdev = (struct sbp_dev *)xfer->sc;
1020 SBP_DEBUG(1)
1021 sbp_show_sdev_info(sdev, 2);
1022 printf("sbp_loginres_callback\n");
1023 END_DEBUG
1024 /* recycle */
1025 s = splfw();
1026 STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link);
1027 splx(s);
1028 return;
1029 }
1030 #endif
1031
1032 static inline void
1033 sbp_xfer_free(struct fw_xfer *xfer)
1034 {
1035 struct sbp_dev *sdev;
1036 int s;
1037
1038 sdev = (struct sbp_dev *)xfer->sc;
1039 fw_xfer_unload(xfer);
1040 s = splfw();
1041 STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
1042 splx(s);
1043 }
1044
1045 static void
1046 sbp_reset_start_callback(struct fw_xfer *xfer)
1047 {
1048 struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
1049 struct sbp_target *target = sdev->target;
1050 int i;
1051
1052 if (xfer->resp != 0) {
1053 sbp_show_sdev_info(sdev, 2);
1054 printf("sbp_reset_start failed: resp=%d\n", xfer->resp);
1055 }
1056
1057 for (i = 0; i < target->num_lun; i++) {
1058 tsdev = target->luns[i];
1059 if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN)
1060 sbp_login(tsdev);
1061 }
1062 }
1063
1064 static void
1065 sbp_reset_start(struct sbp_dev *sdev)
1066 {
1067 struct fw_xfer *xfer;
1068 struct fw_pkt *fp;
1069
1070 SBP_DEBUG(0)
1071 sbp_show_sdev_info(sdev, 2);
1072 printf("sbp_reset_start\n");
1073 END_DEBUG
1074
1075 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1076 xfer->hand = sbp_reset_start_callback;
1077 fp = &xfer->send.hdr;
1078 fp->mode.wreqq.dest_hi = 0xffff;
1079 fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
1080 fp->mode.wreqq.data = htonl(0xf);
1081 fw_asyreq(xfer->fc, -1, xfer);
1082 }
1083
1084 static void
1085 sbp_mgm_callback(struct fw_xfer *xfer)
1086 {
1087 struct sbp_dev *sdev;
1088 int resp;
1089
1090 sdev = (struct sbp_dev *)xfer->sc;
1091
1092 SBP_DEBUG(1)
1093 sbp_show_sdev_info(sdev, 2);
1094 printf("sbp_mgm_callback\n");
1095 END_DEBUG
1096 resp = xfer->resp;
1097 sbp_xfer_free(xfer);
1098 #if 0
1099 if (resp != 0) {
1100 sbp_show_sdev_info(sdev, 2);
1101 printf("management ORB failed(%d) ... RESET_START\n", resp);
1102 sbp_reset_start(sdev);
1103 }
1104 #endif
1105 return;
1106 }
1107
1108 #if defined(__FreeBSD__)
1109 static struct sbp_dev *
1110 sbp_next_dev(struct sbp_target *target, int lun)
1111 {
1112 struct sbp_dev **sdevp;
1113 int i;
1114
1115 for (i = lun, sdevp = &target->luns[lun]; i < target->num_lun;
1116 i++, sdevp++)
1117 if (*sdevp != NULL && (*sdevp)->status == SBP_DEV_PROBE)
1118 return(*sdevp);
1119 return(NULL);
1120 }
1121
1122 #define SCAN_PRI 1
1123 static void
1124 sbp_cam_scan_lun(struct cam_periph *periph, sbp_scsi_xfer *sxfer)
1125 {
1126 struct sbp_target *target;
1127 struct sbp_dev *sdev;
1128
1129 sdev = (struct sbp_dev *) sxfer->ccb_h.ccb_sdev_ptr;
1130 target = sdev->target;
1131 SBP_DEBUG(0)
1132 sbp_show_sdev_info(sdev, 2);
1133 printf("sbp_cam_scan_lun\n");
1134 END_DEBUG
1135 if ((SCSI_XFER_ERROR(sxfer) & CAM_STATUS_MASK) == XS_REQ_CMP) {
1136 sdev->status = SBP_DEV_ATTACHED;
1137 } else {
1138 sbp_show_sdev_info(sdev, 2);
1139 printf("scan failed\n");
1140 }
1141 sdev = sbp_next_dev(target, sdev->lun_id + 1);
1142 if (sdev == NULL) {
1143 free(sxfer, M_SBP);
1144 return;
1145 }
1146 /* reuse sxfer */
1147 xpt_setup_ccb(&sxfer->ccb_h, sdev->path, SCAN_PRI);
1148 sxfer->ccb_h.ccb_sdev_ptr = sdev;
1149 xpt_action(sxfer);
1150 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1151 sdev->freeze = 1;
1152 }
1153
1154 static void
1155 sbp_cam_scan_target(void *arg)
1156 {
1157 struct sbp_target *target = (struct sbp_target *)arg;
1158 struct sbp_dev *sdev;
1159 sbp_scsi_xfer *sxfer;
1160
1161 sdev = sbp_next_dev(target, 0);
1162 if (sdev == NULL) {
1163 printf("sbp_cam_scan_target: nothing to do for target%d\n",
1164 target->target_id);
1165 return;
1166 }
1167 SBP_DEBUG(0)
1168 sbp_show_sdev_info(sdev, 2);
1169 printf("sbp_cam_scan_target\n");
1170 END_DEBUG
1171 sxfer = malloc(sizeof(sbp_scsi_xfer), M_SBP, M_NOWAIT | M_ZERO);
1172 if (sxfer == NULL) {
1173 printf("sbp_cam_scan_target: malloc failed\n");
1174 return;
1175 }
1176 xpt_setup_ccb(&sxfer->ccb_h, sdev->path, SCAN_PRI);
1177 sxfer->ccb_h.func_code = XPT_SCAN_LUN;
1178 sxfer->ccb_h.cbfcnp = sbp_cam_scan_lun;
1179 sxfer->ccb_h.flags |= CAM_DEV_QFREEZE;
1180 sxfer->crcn.flags = CAM_FLAG_NONE;
1181 sxfer->ccb_h.ccb_sdev_ptr = sdev;
1182
1183 /* The scan is in progress now. */
1184 xpt_action(sxfer);
1185 xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1186 sdev->freeze = 1;
1187 }
1188
1189 static inline void
1190 sbp_scan_dev(struct sbp_dev *sdev)
1191 {
1192 sdev->status = SBP_DEV_PROBE;
1193 callout_reset(&sdev->target->scan_callout, scan_delay * hz / 1000,
1194 sbp_cam_scan_target, (void *)sdev->target);
1195 }
1196 #else
1197 static void
1198 fw_kthread_create0(void *arg)
1199 {
1200 struct sbp_softc *sbp = (struct sbp_softc *)arg;
1201
1202 /* create thread */
1203 if (kthread_create(PRI_NONE, 0, NULL, sbp_scsipi_scan_target,
1204 &sbp->target, &sbp->lwp, "sbp%d_attach",
1205 device_unit(sbp->fd.dev))) {
1206
1207 device_printf(sbp->fd.dev, "unable to create thread");
1208 panic("fw_kthread_create");
1209 }
1210 }
1211
1212 static void
1213 sbp_scsipi_scan_target(void *arg)
1214 {
1215 struct sbp_target *target = (struct sbp_target *)arg;
1216 struct sbp_softc *sbp = target->sbp;
1217 struct sbp_dev *sdev;
1218 struct scsipi_channel *chan = &sbp->sc_channel;
1219 struct scsibus_softc *sc_bus = (struct scsibus_softc *)sbp->sc_bus;
1220 int lun, yet;
1221
1222 do {
1223 tsleep(target, PWAIT|PCATCH, "-", 0);
1224 yet = 0;
1225
1226 for (lun = 0; lun < target->num_lun; lun ++) {
1227 sdev = target->luns[lun];
1228 if (sdev == NULL)
1229 continue;
1230 if (sdev->status != SBP_DEV_PROBE) {
1231 yet ++;
1232 continue;
1233 }
1234
1235 if (sdev->periph == NULL) {
1236 if (chan->chan_nluns < target->num_lun)
1237 chan->chan_nluns = target->num_lun;
1238
1239 scsi_probe_bus(sc_bus,
1240 target->target_id, sdev->lun_id);
1241 sdev->periph = scsipi_lookup_periph(
1242 chan, target->target_id, lun);
1243 }
1244 sdev->status = SBP_DEV_ATTACHED;
1245 }
1246 } while (yet > 0);
1247
1248 sbp->lwp = NULL;
1249 kthread_exit(0);
1250 }
1251
1252 static inline void
1253 sbp_scan_dev(struct sbp_dev *sdev)
1254 {
1255 sdev->status = SBP_DEV_PROBE;
1256 wakeup(sdev->target);
1257 }
1258 #endif
1259
1260
1261 static void
1262 sbp_do_attach(struct fw_xfer *xfer)
1263 {
1264 struct sbp_dev *sdev;
1265 struct sbp_target *target;
1266 struct sbp_softc *sbp;
1267
1268 sdev = (struct sbp_dev *)xfer->sc;
1269 target = sdev->target;
1270 sbp = target->sbp;
1271
1272 SBP_DEBUG(0)
1273 sbp_show_sdev_info(sdev, 2);
1274 printf("sbp_do_attach\n");
1275 END_DEBUG
1276 sbp_xfer_free(xfer);
1277
1278 #if defined(__FreeBSD__)
1279 if (sdev->path == NULL)
1280 xpt_create_path(&sdev->path, xpt_periph,
1281 cam_sim_path(target->sbp->sim),
1282 target->target_id, sdev->lun_id);
1283
1284 /*
1285 * Let CAM scan the bus if we are in the boot process.
1286 * XXX xpt_scan_bus cannot detect LUN larger than 0
1287 * if LUN 0 doesn't exists.
1288 */
1289 if (sbp_cold > 0) {
1290 sdev->status = SBP_DEV_ATTACHED;
1291 return;
1292 }
1293 #endif
1294
1295 sbp_scan_dev(sdev);
1296 return;
1297 }
1298
1299 static void
1300 sbp_agent_reset_callback(struct fw_xfer *xfer)
1301 {
1302 struct sbp_dev *sdev;
1303
1304 sdev = (struct sbp_dev *)xfer->sc;
1305 SBP_DEBUG(1)
1306 sbp_show_sdev_info(sdev, 2);
1307 printf("%s\n", __func__);
1308 END_DEBUG
1309 if (xfer->resp != 0) {
1310 sbp_show_sdev_info(sdev, 2);
1311 printf("%s: resp=%d\n", __func__, xfer->resp);
1312 }
1313
1314 sbp_xfer_free(xfer);
1315 if (SBP_DEVICE(sdev)) {
1316 SBP_DEVICE_THAW(sdev, sdev->freeze);
1317 sdev->freeze = 0;
1318 }
1319 }
1320
1321 static void
1322 sbp_agent_reset(struct sbp_dev *sdev)
1323 {
1324 struct fw_xfer *xfer;
1325 struct fw_pkt *fp;
1326
1327 SBP_DEBUG(0)
1328 sbp_show_sdev_info(sdev, 2);
1329 printf("sbp_agent_reset\n");
1330 END_DEBUG
1331 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1332 if (xfer == NULL)
1333 return;
1334 if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE)
1335 xfer->hand = sbp_agent_reset_callback;
1336 else
1337 xfer->hand = sbp_do_attach;
1338 fp = &xfer->send.hdr;
1339 fp->mode.wreqq.data = htonl(0xf);
1340 fw_asyreq(xfer->fc, -1, xfer);
1341 sbp_abort_all_ocbs(sdev, XS_BDR_SENT);
1342 }
1343
1344 static void
1345 sbp_busy_timeout_callback(struct fw_xfer *xfer)
1346 {
1347 struct sbp_dev *sdev;
1348
1349 sdev = (struct sbp_dev *)xfer->sc;
1350 SBP_DEBUG(1)
1351 sbp_show_sdev_info(sdev, 2);
1352 printf("sbp_busy_timeout_callback\n");
1353 END_DEBUG
1354 sbp_xfer_free(xfer);
1355 sbp_agent_reset(sdev);
1356 }
1357
1358 static void
1359 sbp_busy_timeout(struct sbp_dev *sdev)
1360 {
1361 struct fw_pkt *fp;
1362 struct fw_xfer *xfer;
1363 SBP_DEBUG(0)
1364 sbp_show_sdev_info(sdev, 2);
1365 printf("sbp_busy_timeout\n");
1366 END_DEBUG
1367 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1368
1369 xfer->hand = sbp_busy_timeout_callback;
1370 fp = &xfer->send.hdr;
1371 fp->mode.wreqq.dest_hi = 0xffff;
1372 fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1373 fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1374 fw_asyreq(xfer->fc, -1, xfer);
1375 }
1376
1377 static void
1378 sbp_orb_pointer_callback(struct fw_xfer *xfer)
1379 {
1380 struct sbp_dev *sdev;
1381 sdev = (struct sbp_dev *)xfer->sc;
1382
1383 SBP_DEBUG(1)
1384 sbp_show_sdev_info(sdev, 2);
1385 printf("%s\n", __func__);
1386 END_DEBUG
1387 if (xfer->resp != 0) {
1388 /* XXX */
1389 printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1390 }
1391 sbp_xfer_free(xfer);
1392 sdev->flags &= ~ORB_POINTER_ACTIVE;
1393
1394 if ((sdev->flags & ORB_POINTER_NEED) != 0) {
1395 struct sbp_ocb *ocb;
1396
1397 sdev->flags &= ~ORB_POINTER_NEED;
1398 ocb = STAILQ_FIRST(&sdev->ocbs);
1399 if (ocb != NULL)
1400 sbp_orb_pointer(sdev, ocb);
1401 }
1402 return;
1403 }
1404
1405 static void
1406 sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1407 {
1408 struct fw_xfer *xfer;
1409 struct fw_pkt *fp;
1410 SBP_DEBUG(1)
1411 sbp_show_sdev_info(sdev, 2);
1412 printf("%s: 0x%08x\n", __func__, (uint32_t)ocb->bus_addr);
1413 END_DEBUG
1414
1415 if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) {
1416 SBP_DEBUG(0)
1417 printf("%s: orb pointer active\n", __func__);
1418 END_DEBUG
1419 sdev->flags |= ORB_POINTER_NEED;
1420 return;
1421 }
1422
1423 sdev->flags |= ORB_POINTER_ACTIVE;
1424 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1425 if (xfer == NULL)
1426 return;
1427 xfer->hand = sbp_orb_pointer_callback;
1428
1429 fp = &xfer->send.hdr;
1430 fp->mode.wreqb.len = 8;
1431 fp->mode.wreqb.extcode = 0;
1432 xfer->send.payload[0] =
1433 htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1434 xfer->send.payload[1] = htonl((uint32_t)ocb->bus_addr);
1435
1436 if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1437 sbp_xfer_free(xfer);
1438 SCSI_XFER_ERROR(ocb->sxfer) = XS_REQ_INVALID;
1439 SCSI_TRANSFER_DONE(ocb->sxfer);
1440 }
1441 }
1442
1443 static void
1444 sbp_doorbell_callback(struct fw_xfer *xfer)
1445 {
1446 struct sbp_dev *sdev;
1447 sdev = (struct sbp_dev *)xfer->sc;
1448
1449 SBP_DEBUG(1)
1450 sbp_show_sdev_info(sdev, 2);
1451 printf("sbp_doorbell_callback\n");
1452 END_DEBUG
1453 if (xfer->resp != 0) {
1454 /* XXX */
1455 printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1456 }
1457 sbp_xfer_free(xfer);
1458 sdev->flags &= ~ORB_DOORBELL_ACTIVE;
1459 if ((sdev->flags & ORB_DOORBELL_NEED) != 0) {
1460 sdev->flags &= ~ORB_DOORBELL_NEED;
1461 sbp_doorbell(sdev);
1462 }
1463 return;
1464 }
1465
1466 static void
1467 sbp_doorbell(struct sbp_dev *sdev)
1468 {
1469 struct fw_xfer *xfer;
1470 struct fw_pkt *fp;
1471 SBP_DEBUG(1)
1472 sbp_show_sdev_info(sdev, 2);
1473 printf("sbp_doorbell\n");
1474 END_DEBUG
1475
1476 if ((sdev->flags & ORB_DOORBELL_ACTIVE) != 0) {
1477 sdev->flags |= ORB_DOORBELL_NEED;
1478 return;
1479 }
1480 sdev->flags |= ORB_DOORBELL_ACTIVE;
1481 xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1482 if (xfer == NULL)
1483 return;
1484 xfer->hand = sbp_doorbell_callback;
1485 fp = &xfer->send.hdr;
1486 fp->mode.wreqq.data = htonl(0xf);
1487 fw_asyreq(xfer->fc, -1, xfer);
1488 }
1489
1490 static struct fw_xfer *
1491 sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1492 {
1493 struct fw_xfer *xfer;
1494 struct fw_pkt *fp;
1495 struct sbp_target *target;
1496 int s, new = 0;
1497
1498 target = sdev->target;
1499 s = splfw();
1500 xfer = STAILQ_FIRST(&target->xferlist);
1501 if (xfer == NULL) {
1502 if (target->n_xfer > 5 /* XXX */) {
1503 printf("sbp: no more xfer for this target\n");
1504 splx(s);
1505 return(NULL);
1506 }
1507 xfer = fw_xfer_alloc_buf(M_SBP, 8, 0);
1508 if(xfer == NULL){
1509 printf("sbp: fw_xfer_alloc_buf failed\n");
1510 splx(s);
1511 return NULL;
1512 }
1513 target->n_xfer ++;
1514 if (debug)
1515 printf("sbp: alloc %d xfer\n", target->n_xfer);
1516 new = 1;
1517 } else {
1518 STAILQ_REMOVE_HEAD(&target->xferlist, link);
1519 }
1520 splx(s);
1521
1522 microtime(&xfer->tv);
1523
1524 if (new) {
1525 xfer->recv.pay_len = 0;
1526 xfer->send.spd = min(sdev->target->fwdev->speed, max_speed);
1527 xfer->fc = sdev->target->sbp->fd.fc;
1528 }
1529
1530 if (tcode == FWTCODE_WREQB)
1531 xfer->send.pay_len = 8;
1532 else
1533 xfer->send.pay_len = 0;
1534
1535 xfer->sc = (void *)sdev;
1536 fp = &xfer->send.hdr;
1537 fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
1538 fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
1539 fp->mode.wreqq.tlrt = 0;
1540 fp->mode.wreqq.tcode = tcode;
1541 fp->mode.wreqq.pri = 0;
1542 fp->mode.wreqq.dst = FWLOCALBUS | sdev->target->fwdev->dst;
1543
1544 return xfer;
1545
1546 }
1547
1548 static void
1549 sbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1550 {
1551 struct fw_xfer *xfer;
1552 struct fw_pkt *fp;
1553 struct sbp_ocb *ocb;
1554 struct sbp_target *target;
1555 int s, nid, dv_unit;
1556
1557 target = sdev->target;
1558 nid = target->sbp->fd.fc->nodeid | FWLOCALBUS;
1559 dv_unit = device_get_unit(target->sbp->fd.dev);
1560
1561 s = splfw();
1562 if (func == ORB_FUN_RUNQUEUE) {
1563 ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1564 if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1565 splx(s);
1566 return;
1567 }
1568 STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1569 goto start;
1570 }
1571 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
1572 splx(s);
1573 /* XXX */
1574 return;
1575 }
1576 ocb->flags = OCB_ACT_MGM;
1577 ocb->sdev = sdev;
1578
1579 bzero((void *)ocb->orb, sizeof(ocb->orb));
1580 ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1581 ocb->orb[7] = htonl(SBP_DEV2ADDR(dv_unit, sdev->lun_id));
1582
1583 SBP_DEBUG(0)
1584 sbp_show_sdev_info(sdev, 2);
1585 printf("%s\n", orb_fun_name[(func>>16)&0xf]);
1586 END_DEBUG
1587 switch (func) {
1588 case ORB_FUN_LGI:
1589 ocb->orb[0] = ocb->orb[1] = 0; /* password */
1590 ocb->orb[2] = htonl(nid << 16);
1591 ocb->orb[3] = htonl(sdev->dma.bus_addr);
1592 ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id);
1593 if (ex_login)
1594 ocb->orb[4] |= htonl(ORB_EXV);
1595 ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
1596 break;
1597 case ORB_FUN_ATA:
1598 ocb->orb[0] = htonl((0 << 16) | 0);
1599 ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
1600 /* fall through */
1601 case ORB_FUN_RCN:
1602 case ORB_FUN_LGO:
1603 case ORB_FUN_LUR:
1604 case ORB_FUN_RST:
1605 case ORB_FUN_ATS:
1606 ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
1607 break;
1608 }
1609
1610 if (target->mgm_ocb_cur != NULL) {
1611 /* there is a standing ORB */
1612 STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1613 splx(s);
1614 return;
1615 }
1616 start:
1617 target->mgm_ocb_cur = ocb;
1618 splx(s);
1619
1620 callout_reset(&target->mgm_ocb_timeout, 5*hz,
1621 sbp_mgm_timeout, (void *)ocb);
1622 xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1623 if(xfer == NULL){
1624 return;
1625 }
1626 xfer->hand = sbp_mgm_callback;
1627
1628 fp = &xfer->send.hdr;
1629 fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
1630 fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
1631 fp->mode.wreqb.len = 8;
1632 fp->mode.wreqb.extcode = 0;
1633 xfer->send.payload[0] = htonl(nid << 16);
1634 xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff);
1635 SBP_DEBUG(0)
1636 sbp_show_sdev_info(sdev, 2);
1637 printf("mgm orb: %08x\n", (uint32_t)ocb->bus_addr);
1638 END_DEBUG
1639
1640 /* cache writeback & invalidate(required ORB_FUN_LGI func) */
1641 /* when abort_ocb, should sync POST ope ? */
1642 fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1643 fw_asyreq(xfer->fc, -1, xfer);
1644 }
1645
1646 static void
1647 sbp_print_scsi_cmd(struct sbp_ocb *ocb)
1648 {
1649 #if defined(__FreeBSD__)
1650 struct ccb_scsiio *csio;
1651
1652 csio = &ocb->sxfer->csio;
1653 #endif
1654 printf("%s:%d:%d XPT_SCSI_IO: "
1655 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1656 ", flags: 0x%02x, "
1657 "%db cmd/%db data/%db sense\n",
1658 device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1659 SCSI_XFER_TARGET(ocb->sxfer), SCSI_XFER_LUN(ocb->sxfer),
1660 SCSI_XFER_10BCMD_DUMP(ocb->sxfer),
1661 SCSI_XFER_DIR(ocb->sxfer),
1662 SCSI_XFER_CMDLEN(ocb->sxfer), SCSI_XFER_DATALEN(ocb->sxfer),
1663 SCSI_XFER_SENSELEN(ocb->sxfer));
1664 }
1665
1666 static void
1667 sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1668 {
1669 struct sbp_cmd_status *sbp_cmd_status;
1670 scsi3_sense_data_t sense =
1671 (scsi3_sense_data_t)SCSI_SENSE_DATA(ocb->sxfer);
1672
1673 sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1674
1675 SBP_DEBUG(0)
1676 sbp_print_scsi_cmd(ocb);
1677 /* XXX need decode status */
1678 sbp_show_sdev_info(ocb->sdev, 2);
1679 printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1680 sbp_cmd_status->status,
1681 sbp_cmd_status->sfmt,
1682 sbp_cmd_status->valid,
1683 sbp_cmd_status->s_key,
1684 sbp_cmd_status->s_code,
1685 sbp_cmd_status->s_qlfr,
1686 sbp_status->len
1687 );
1688 END_DEBUG
1689
1690 switch (sbp_cmd_status->status) {
1691 case SCSI_STATUS_CHECK_COND:
1692 case SCSI_STATUS_BUSY:
1693 case SCSI_STATUS_CMD_TERMINATED:
1694 if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1695 sense->response_code = SSD_CURRENT_ERROR;
1696 }else{
1697 sense->response_code = SSD_DEFERRED_ERROR;
1698 }
1699 if(sbp_cmd_status->valid)
1700 sense->response_code |= SSD_RESPONSE_CODE_VALID;
1701 sense->flags = sbp_cmd_status->s_key;
1702 if(sbp_cmd_status->mark)
1703 sense->flags |= SSD_FILEMARK;
1704 if(sbp_cmd_status->eom)
1705 sense->flags |= SSD_EOM;
1706 if(sbp_cmd_status->ill_len)
1707 sense->flags |= SSD_ILI;
1708
1709 bcopy(&sbp_cmd_status->info, &sense->information[0], 4);
1710
1711 if (sbp_status->len <= 1)
1712 /* XXX not scsi status. shouldn't be happened */
1713 sense->asl = 0;
1714 else if (sbp_status->len <= 4)
1715 /* add_sense_code(_qual), info, cmd_spec_info */
1716 sense->asl = 6;
1717 else
1718 /* fru, sense_key_spec */
1719 sense->asl = 10;
1720
1721 bcopy(&sbp_cmd_status->cdb, &sense->csi[0], 4);
1722
1723 sense->asc = sbp_cmd_status->s_code;
1724 sense->ascq = sbp_cmd_status->s_qlfr;
1725 sense->fruc = sbp_cmd_status->fru;
1726
1727 bcopy(&sbp_cmd_status->s_keydep[0], &sense->sks[0], 3);
1728 SCSI_XFER_ERROR(ocb->sxfer) = XS_SENSE;
1729 SCSI_XFER_STATUS(ocb->sxfer) = sbp_cmd_status->status;
1730 /*
1731 {
1732 uint8_t j, *tmp;
1733 tmp = sense;
1734 for( j = 0 ; j < 32 ; j+=8){
1735 printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1736 tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1737 tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1738 }
1739
1740 }
1741 */
1742 break;
1743 default:
1744 sbp_show_sdev_info(ocb->sdev, 2);
1745 printf("sbp_scsi_status: unknown scsi status 0x%x\n",
1746 sbp_cmd_status->status);
1747 }
1748 }
1749
1750 static void
1751 sbp_fix_inq_data(struct sbp_ocb *ocb)
1752 {
1753 sbp_scsi_xfer *sxfer = ocb->sxfer;
1754 struct sbp_dev *sdev;
1755 scsi3_inquiry_data_t inq =
1756 (scsi3_inquiry_data_t)SCSI_INQUIRY_DATA(sxfer);
1757 sdev = ocb->sdev;
1758
1759 if (SCSI_XFER_EVPD(sxfer))
1760 return;
1761 SBP_DEBUG(1)
1762 sbp_show_sdev_info(sdev, 2);
1763 printf("sbp_fix_inq_data\n");
1764 END_DEBUG
1765 switch (inq->device & SID_TYPE) {
1766 case T_DIRECT:
1767 #if 0
1768 /*
1769 * XXX Convert Direct Access device to RBC.
1770 * I've never seen FireWire DA devices which support READ_6.
1771 */
1772 if ((inq->device & SID_TYPE) == T_DIRECT)
1773 inq->device |= T_RBC; /* T_DIRECT == 0 */
1774 #endif
1775 /* fall through */
1776 case T_RBC:
1777 /*
1778 * Override vendor/product/revision information.
1779 * Some devices sometimes return strange strings.
1780 */
1781 #if 1
1782 bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1783 bcopy(sdev->product, inq->product, sizeof(inq->product));
1784 bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1785 #endif
1786 break;
1787 }
1788 /*
1789 * Force to enable/disable tagged queuing.
1790 * XXX CAM also checks SCP_QUEUE_DQUE flag in the control mode page.
1791 */
1792 if (sbp_tags > 0)
1793 inq->flags[1] |= SID_CmdQue;
1794 else if (sbp_tags < 0)
1795 inq->flags[1] &= ~SID_CmdQue;
1796
1797 }
1798
1799 static void
1800 sbp_recv1(struct fw_xfer *xfer)
1801 {
1802 struct fw_pkt *rfp;
1803 #if NEED_RESPONSE
1804 struct fw_pkt *sfp;
1805 #endif
1806 struct sbp_softc *sbp;
1807 struct sbp_dev *sdev;
1808 struct sbp_ocb *ocb;
1809 struct sbp_login_res *login_res = NULL;
1810 struct sbp_status *sbp_status;
1811 struct sbp_target *target;
1812 int orb_fun, status_valid0, status_valid, l, reset_agent = 0;
1813 uint32_t addr;
1814 /*
1815 uint32_t *ld;
1816 ld = xfer->recv.buf;
1817 printf("sbp %x %d %d %08x %08x %08x %08x\n",
1818 xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1819 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1820 printf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1821 */
1822 sbp = (struct sbp_softc *)xfer->sc;
1823 if (xfer->resp != 0){
1824 printf("sbp_recv: xfer->resp = %d\n", xfer->resp);
1825 goto done0;
1826 }
1827 if (xfer->recv.payload == NULL){
1828 printf("sbp_recv: xfer->recv.payload == NULL\n");
1829 goto done0;
1830 }
1831 rfp = &xfer->recv.hdr;
1832 if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1833 printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1834 goto done0;
1835 }
1836 sbp_status = (struct sbp_status *)xfer->recv.payload;
1837 addr = rfp->mode.wreqb.dest_lo;
1838 SBP_DEBUG(2)
1839 printf("received address 0x%x\n", addr);
1840 END_DEBUG
1841 target = &sbp->target;
1842 l = SBP_ADDR2LUN(addr);
1843 if (l >= target->num_lun || target->luns[l] == NULL) {
1844 device_printf(sbp->fd.dev,
1845 "sbp_recv1: invalid lun %d (target=%d)\n",
1846 l, target->target_id);
1847 goto done0;
1848 }
1849 sdev = target->luns[l];
1850
1851 fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1852
1853 ocb = NULL;
1854 switch (sbp_status->src) {
1855 case SRC_NEXT_EXISTS:
1856 case SRC_NO_NEXT:
1857 /* check mgm_ocb_cur first */
1858 ocb = target->mgm_ocb_cur;
1859 if (ocb != NULL) {
1860 if (OCB_MATCH(ocb, sbp_status)) {
1861 callout_stop(&target->mgm_ocb_timeout);
1862 target->mgm_ocb_cur = NULL;
1863 break;
1864 }
1865 }
1866 ocb = sbp_dequeue_ocb(sdev, sbp_status);
1867 if (ocb == NULL) {
1868 sbp_show_sdev_info(sdev, 2);
1869 #if defined(__DragonFly__) || \
1870 (defined(__FreeBSD__) && __FreeBSD_version < 500000)
1871 printf("No ocb(%lx) on the queue\n",
1872 #else
1873 printf("No ocb(%x) on the queue\n",
1874 #endif
1875 ntohl(sbp_status->orb_lo));
1876 }
1877 break;
1878 case SRC_UNSOL:
1879 /* unsolicit */
1880 sbp_show_sdev_info(sdev, 2);
1881 printf("unsolicit status received\n");
1882 break;
1883 default:
1884 sbp_show_sdev_info(sdev, 2);
1885 printf("unknown sbp_status->src\n");
1886 }
1887
1888 status_valid0 = (sbp_status->src < 2
1889 && sbp_status->resp == SBP_REQ_CMP
1890 && sbp_status->dead == 0);
1891 status_valid = (status_valid0 && sbp_status->status == 0);
1892
1893 if (!status_valid0 || debug > 2){
1894 int status;
1895 SBP_DEBUG(0)
1896 sbp_show_sdev_info(sdev, 2);
1897 printf("ORB status src:%x resp:%x dead:%x"
1898 #if defined(__DragonFly__) || \
1899 (defined(__FreeBSD__) && __FreeBSD_version < 500000)
1900 " len:%x stat:%x orb:%x%08lx\n",
1901 #else
1902 " len:%x stat:%x orb:%x%08x\n",
1903 #endif
1904 sbp_status->src, sbp_status->resp, sbp_status->dead,
1905 sbp_status->len, sbp_status->status,
1906 ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1907 END_DEBUG
1908 sbp_show_sdev_info(sdev, 2);
1909 status = sbp_status->status;
1910 switch(sbp_status->resp) {
1911 case SBP_REQ_CMP:
1912 if (status > MAX_ORB_STATUS0)
1913 printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1914 else
1915 printf("%s\n", orb_status0[status]);
1916 break;
1917 case SBP_TRANS_FAIL:
1918 printf("Obj: %s, Error: %s\n",
1919 orb_status1_object[(status>>6) & 3],
1920 orb_status1_serial_bus_error[status & 0xf]);
1921 break;
1922 case SBP_ILLE_REQ:
1923 printf("Illegal request\n");
1924 break;
1925 case SBP_VEND_DEP:
1926 printf("Vendor dependent\n");
1927 break;
1928 default:
1929 printf("unknown respose code %d\n", sbp_status->resp);
1930 }
1931 }
1932
1933 /* we have to reset the fetch agent if it's dead */
1934 if (sbp_status->dead) {
1935 if (SBP_DEVICE(sdev) != NULL) {
1936 SBP_DEVICE_FREEZE(sdev, 1);
1937 sdev->freeze ++;
1938 }
1939 reset_agent = 1;
1940 }
1941
1942 if (ocb == NULL)
1943 goto done;
1944
1945 switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1946 case ORB_FMT_NOP:
1947 break;
1948 case ORB_FMT_VED:
1949 break;
1950 case ORB_FMT_STD:
1951 switch(ocb->flags) {
1952 case OCB_ACT_MGM:
1953 orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1954 reset_agent = 0;
1955 switch(orb_fun) {
1956 case ORB_FUN_LGI:
1957 login_res = sdev->login;
1958 login_res->len = ntohs(login_res->len);
1959 login_res->id = ntohs(login_res->id);
1960 login_res->cmd_hi = ntohs(login_res->cmd_hi);
1961 login_res->cmd_lo = ntohl(login_res->cmd_lo);
1962 if (status_valid) {
1963 SBP_DEBUG(0)
1964 sbp_show_sdev_info(sdev, 2);
1965 printf("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));
1966 END_DEBUG
1967 sbp_busy_timeout(sdev);
1968 } else {
1969 /* forgot logout? */
1970 sbp_show_sdev_info(sdev, 2);
1971 printf("login failed\n");
1972 sdev->status = SBP_DEV_RESET;
1973 }
1974 break;
1975 case ORB_FUN_RCN:
1976 login_res = sdev->login;
1977 if (status_valid) {
1978 SBP_DEBUG(0)
1979 sbp_show_sdev_info(sdev, 2);
1980 printf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo);
1981 END_DEBUG
1982 #if 1
1983 #if defined(__FreeBSD__)
1984 if (sdev->status == SBP_DEV_ATTACHED)
1985 sbp_scan_dev(sdev);
1986 else
1987 #endif
1988 sbp_agent_reset(sdev);
1989 #else
1990 sdev->status = SBP_DEV_ATTACHED;
1991 sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL);
1992 #endif
1993 } else {
1994 /* reconnection hold time exceed? */
1995 SBP_DEBUG(0)
1996 sbp_show_sdev_info(sdev, 2);
1997 printf("reconnect failed\n");
1998 END_DEBUG
1999 sbp_login(sdev);
2000 }
2001 break;
2002 case ORB_FUN_LGO:
2003 sdev->status = SBP_DEV_RESET;
2004 break;
2005 case ORB_FUN_RST:
2006 sbp_busy_timeout(sdev);
2007 break;
2008 case ORB_FUN_LUR:
2009 case ORB_FUN_ATA:
2010 case ORB_FUN_ATS:
2011 sbp_agent_reset(sdev);
2012 break;
2013 default:
2014 sbp_show_sdev_info(sdev, 2);
2015 printf("unknown function %d\n", orb_fun);
2016 break;
2017 }
2018 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2019 break;
2020 case OCB_ACT_CMD:
2021 sdev->timeout = 0;
2022 if(ocb->sxfer != NULL){
2023 sbp_scsi_xfer *sxfer = ocb->sxfer;
2024 /*
2025 uint32_t *ld = SCSI_XFER_DATA(ocb->sxfer);
2026 if(ld != NULL &&
2027 SCSI_XFER_DATALEN(ocb->sxfer) != 0)
2028 printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]);
2029 else
2030 printf("ptr NULL\n");
2031 printf("len %d\n", sbp_status->len);
2032 */
2033 if(sbp_status->len > 1){
2034 sbp_scsi_status(sbp_status, ocb);
2035 }else{
2036 if(sbp_status->resp != SBP_REQ_CMP){
2037 SCSI_XFER_ERROR(sxfer) =
2038 XS_REQ_CMP_ERR;
2039 }else{
2040 SCSI_XFER_ERROR(sxfer) =
2041 XS_REQ_CMP;
2042 SCSI_XFER_REQUEST_COMPLETE(
2043 sxfer);
2044 }
2045 }
2046 /* fix up inq data */
2047 if (SCSI_XFER_OPECODE(sxfer) == INQUIRY)
2048 sbp_fix_inq_data(ocb);
2049 SCSI_TRANSFER_DONE(sxfer);
2050 }
2051 break;
2052 default:
2053 break;
2054 }
2055 }
2056
2057 if (!use_doorbell)
2058 sbp_free_ocb(sdev, ocb);
2059 done:
2060 if (reset_agent)
2061 sbp_agent_reset(sdev);
2062
2063 done0:
2064 xfer->recv.pay_len = SBP_RECV_LEN;
2065 /* The received packet is usually small enough to be stored within
2066 * the buffer. In that case, the controller return ack_complete and
2067 * no respose is necessary.
2068 *
2069 * XXX fwohci.c and firewire.c should inform event_code such as
2070 * ack_complete or ack_pending to upper driver.
2071 */
2072 #if NEED_RESPONSE
2073 xfer->send.off = 0;
2074 sfp = (struct fw_pkt *)xfer->send.buf;
2075 sfp->mode.wres.dst = rfp->mode.wreqb.src;
2076 xfer->dst = sfp->mode.wres.dst;
2077 xfer->spd = min(sdev->target->fwdev->speed, max_speed);
2078 xfer->hand = sbp_loginres_callback;
2079
2080 sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
2081 sfp->mode.wres.tcode = FWTCODE_WRES;
2082 sfp->mode.wres.rtcode = 0;
2083 sfp->mode.wres.pri = 0;
2084
2085 fw_asyreq(xfer->fc, -1, xfer);
2086 #else
2087 /* recycle */
2088 STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
2089 #endif
2090
2091 return;
2092
2093 }
2094
2095 static void
2096 sbp_recv(struct fw_xfer *xfer)
2097 {
2098 int s;
2099
2100 s = splfwsbp();
2101 sbp_recv1(xfer);
2102 splx(s);
2103 }
2104 /*
2105 * sbp_attach()
2106 */
2107 FW_ATTACH(sbp)
2108 {
2109 FW_ATTACH_START(sbp, sbp, fwa);
2110 int dv_unit, error, s;
2111 SBP_ATTACH_START;
2112
2113 if (DFLTPHYS > SBP_MAXPHYS)
2114 device_printf(sbp->fd.dev,
2115 "Warning, DFLTPHYS(%dKB) is larger than "
2116 "SBP_MAXPHYS(%dKB).\n", DFLTPHYS / 1024,
2117 SBP_MAXPHYS / 1024);
2118 SBP_DEBUG(0)
2119 printf("sbp_attach (cold=%d)\n", cold);
2120 END_DEBUG
2121
2122 if (cold)
2123 sbp_cold ++;
2124 sbp->fd.fc = fwa->fc;
2125
2126 if (max_speed < 0)
2127 max_speed = sbp->fd.fc->speed;
2128
2129 error = fw_bus_dma_tag_create(/*parent*/sbp->fd.fc->dmat,
2130 /* XXX shoud be 4 for sane backend? */
2131 /*alignment*/1,
2132 /*boundary*/0,
2133 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
2134 /*highaddr*/BUS_SPACE_MAXADDR,
2135 /*filter*/NULL, /*filterarg*/NULL,
2136 /*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
2137 /*maxsegsz*/SBP_SEG_MAX,
2138 /*flags*/BUS_DMA_ALLOCNOW,
2139 /*lockfunc*/busdma_lock_mutex,
2140 /*lockarg*/&Giant,
2141 &sbp->dmat);
2142 if (error != 0) {
2143 printf("sbp_attach: Could not allocate DMA tag "
2144 "- error %d\n", error);
2145 FW_ATTACH_RETURN(ENOMEM);
2146 }
2147
2148 #if defined(__FreeBSD__)
2149 devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
2150 if (devq == NULL)
2151 return (ENXIO);
2152 #endif
2153
2154 sbp->target.fwdev = NULL;
2155 sbp->target.luns = NULL;
2156
2157 if (sbp_alloc_target(sbp, fwa->fwdev) == NULL)
2158 FW_ATTACH_RETURN(ENXIO);
2159
2160 SBP_SCSIBUS_ATTACH;
2161
2162 /* We reserve 16 bit space (4 bytes X 64 unit X 256 luns) */
2163 dv_unit = device_get_unit(sbp->fd.dev);
2164 sbp->fwb.start = SBP_DEV2ADDR(dv_unit, 0);
2165 sbp->fwb.end = SBP_DEV2ADDR(dv_unit, -1);
2166 /* pre-allocate xfer */
2167 STAILQ_INIT(&sbp->fwb.xferlist);
2168 fw_xferlist_add(&sbp->fwb.xferlist, M_SBP,
2169 /*send*/ 0, /*recv*/ SBP_RECV_LEN, SBP_NUM_OCB/2,
2170 sbp->fd.fc, (void *)sbp, sbp_recv);
2171 fw_bindadd(sbp->fd.fc, &sbp->fwb);
2172
2173 sbp->fd.post_busreset = sbp_post_busreset;
2174 sbp->fd.post_explore = sbp_post_explore;
2175
2176 if (sbp->fd.fc->status != FWBUSNOTREADY) {
2177 s = splfw();
2178 sbp_post_busreset((void *)sbp);
2179 sbp_post_explore((void *)sbp);
2180 splx(s);
2181 }
2182
2183 FW_ATTACH_RETURN(0);
2184 #if defined(__FreeBSD__)
2185 fail:
2186 cam_sim_free(sbp->sim, /*free_devq*/TRUE);
2187 return (ENXIO);
2188 #endif
2189 }
2190
2191 static int
2192 sbp_logout_all(struct sbp_softc *sbp)
2193 {
2194 struct sbp_target *target;
2195 struct sbp_dev *sdev;
2196 int i;
2197
2198 SBP_DEBUG(0)
2199 printf("sbp_logout_all\n");
2200 END_DEBUG
2201 target = &sbp->target;
2202 if (target->luns != NULL)
2203 for (i = 0; i < target->num_lun; i++) {
2204 sdev = target->luns[i];
2205 if (sdev == NULL)
2206 continue;
2207 callout_stop(&sdev->login_callout);
2208 if (sdev->status >= SBP_DEV_TOATTACH &&
2209 sdev->status <= SBP_DEV_ATTACHED)
2210 sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
2211 }
2212
2213 return 0;
2214 }
2215
2216 #if defined(__FreeBSD__)
2217 static int
2218 sbp_shutdown(device_t dev)
2219 {
2220 struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2221
2222 sbp_logout_all(sbp);
2223 return (0);
2224 }
2225 #endif
2226
2227 static void
2228 sbp_free_sdev(struct sbp_dev *sdev)
2229 {
2230 int i;
2231
2232 if (sdev == NULL)
2233 return;
2234 for (i = 0; i < SBP_QUEUE_LEN; i++)
2235 fw_bus_dmamap_destroy(sdev->target->sbp->dmat,
2236 sdev->ocb[i].dmamap);
2237 fwdma_free(sdev->target->sbp->fd.fc, &sdev->dma);
2238 free(sdev, M_SBP);
2239 }
2240
2241 static void
2242 sbp_free_target(struct sbp_target *target)
2243 {
2244 struct sbp_softc *sbp;
2245 struct fw_xfer *xfer, *next;
2246 int i;
2247
2248 if (target->luns == NULL)
2249 return;
2250 callout_stop(&target->mgm_ocb_timeout);
2251 sbp = target->sbp;
2252 for (i = 0; i < target->num_lun; i++)
2253 sbp_free_sdev(target->luns[i]);
2254
2255 for (xfer = STAILQ_FIRST(&target->xferlist);
2256 xfer != NULL; xfer = next) {
2257 next = STAILQ_NEXT(xfer, link);
2258 fw_xfer_free_buf(xfer);
2259 }
2260 STAILQ_INIT(&target->xferlist);
2261 free(target->luns, M_SBP);
2262 target->num_lun = 0;;
2263 target->luns = NULL;
2264 target->fwdev = NULL;
2265 }
2266
2267 FW_DETACH(sbp)
2268 {
2269 FW_DETACH_START(sbp, sbp);
2270 struct firewire_comm *fc = sbp->fd.fc;
2271 int i;
2272
2273 SBP_DEBUG(0)
2274 printf("sbp_detach\n");
2275 END_DEBUG
2276
2277 SBP_DETACH_TARGET(&sbp->target);
2278 #if defined(__FreeBSD__)
2279 xpt_async(AC_LOST_DEVICE, sbp->path, NULL);
2280 xpt_free_path(sbp->path);
2281 xpt_bus_deregister(cam_sim_path(sbp->sim));
2282 cam_sim_free(sbp->sim, /*free_devq*/ TRUE),
2283 #endif
2284
2285 sbp_logout_all(sbp);
2286
2287 /* XXX wait for logout completion */
2288 tsleep(&i, FWPRI, "sbpdtc", hz/2);
2289
2290 sbp_free_target(&sbp->target);
2291
2292 fw_bindremove(fc, &sbp->fwb);
2293 fw_xferlist_remove(&sbp->fwb.xferlist);
2294
2295 fw_bus_dma_tag_destroy(sbp->dmat);
2296
2297 return (0);
2298 }
2299
2300 #if defined(__FreeBSD__)
2301 static void
2302 sbp_cam_detach_sdev(struct sbp_dev *sdev)
2303 {
2304 if (sdev == NULL)
2305 return;
2306 if (sdev->status == SBP_DEV_DEAD)
2307 return;
2308 if (sdev->status == SBP_DEV_RESET)
2309 return;
2310 if (sdev->path) {
2311 xpt_release_devq(sdev->path,
2312 sdev->freeze, TRUE);
2313 sdev->freeze = 0;
2314 xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
2315 xpt_free_path(sdev->path);
2316 sdev->path = NULL;
2317 }
2318 sbp_abort_all_ocbs(sdev, XS_DEV_NOT_THERE);
2319 }
2320
2321 static void
2322 sbp_cam_detach_target(struct sbp_target *target)
2323 {
2324 int i;
2325
2326 if (target->luns != NULL) {
2327 SBP_DEBUG(0)
2328 printf("sbp_detach_target %d\n", target->target_id);
2329 END_DEBUG
2330 callout_stop(&target->scan_callout);
2331 for (i = 0; i < target->num_lun; i++)
2332 sbp_cam_detach_sdev(target->luns[i]);
2333 }
2334 }
2335 #elif defined(__NetBSD__)
2336 static void
2337 sbp_scsipi_detach_sdev(struct sbp_dev *sdev)
2338 {
2339 struct sbp_target *target;
2340 struct sbp_softc *sbp;
2341
2342 if (sdev == NULL)
2343 return;
2344
2345 target = sdev->target;
2346 if (target == NULL)
2347 return;
2348
2349 sbp = target->sbp;
2350
2351 if (sdev->status == SBP_DEV_DEAD)
2352 return;
2353 if (sdev->status == SBP_DEV_RESET)
2354 return;
2355 if (sdev->periph) {
2356 scsipi_periph_thaw(sdev->periph, sdev->freeze);
2357 scsipi_channel_thaw(&sbp->sc_channel, 0); /* XXXX */
2358 sdev->freeze = 0;
2359 if (scsipi_target_detach(&sbp->sc_channel,
2360 target->target_id, sdev->lun_id, DETACH_FORCE) != 0) {
2361 sbp_show_sdev_info(sdev, 2);
2362 printf("detach failed\n");
2363 }
2364 sdev->periph = NULL;
2365 }
2366 sbp_abort_all_ocbs(sdev, XS_DEV_NOT_THERE);
2367 }
2368
2369 static void
2370 sbp_scsipi_detach_target(struct sbp_target *target)
2371 {
2372 struct sbp_softc *sbp = target->sbp;
2373 int i;
2374
2375 if (target->luns != NULL) {
2376 SBP_DEBUG(0)
2377 printf("sbp_detach_target %d\n", target->target_id);
2378 END_DEBUG
2379 callout_stop(&target->scan_callout);
2380 for (i = 0; i < target->num_lun; i++)
2381 sbp_scsipi_detach_sdev(target->luns[i]);
2382 if (config_detach(sbp->sc_bus, DETACH_FORCE) != 0)
2383 device_printf(sbp->fd.dev, "%d detach failed\n",
2384 target->target_id);
2385 sbp->sc_bus = NULL;
2386 }
2387 }
2388 #endif
2389
2390 static void
2391 sbp_target_reset(struct sbp_dev *sdev, int method)
2392 {
2393 int i;
2394 struct sbp_target *target = sdev->target;
2395 struct sbp_dev *tsdev;
2396
2397 for (i = 0; i < target->num_lun; i++) {
2398 tsdev = target->luns[i];
2399 if (tsdev == NULL)
2400 continue;
2401 if (tsdev->status == SBP_DEV_DEAD)
2402 continue;
2403 if (tsdev->status == SBP_DEV_RESET)
2404 continue;
2405 SBP_DEVICE_FREEZE(tsdev, 1);
2406 tsdev->freeze ++;
2407 sbp_abort_all_ocbs(tsdev, XS_CMD_TIMEOUT);
2408 if (method == 2)
2409 tsdev->status = SBP_DEV_LOGIN;
2410 }
2411 switch(method) {
2412 case 1:
2413 printf("target reset\n");
2414 sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2415 break;
2416 case 2:
2417 printf("reset start\n");
2418 sbp_reset_start(sdev);
2419 break;
2420 }
2421
2422 }
2423
2424 static void
2425 sbp_mgm_timeout(void *arg)
2426 {
2427 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2428 struct sbp_dev *sdev = ocb->sdev;
2429 struct sbp_target *target = sdev->target;
2430
2431 sbp_show_sdev_info(sdev, 2);
2432 printf("request timeout(mgm orb:0x%08x) ... ",
2433 (uint32_t)ocb->bus_addr);
2434 target->mgm_ocb_cur = NULL;
2435 sbp_free_ocb(sdev, ocb);
2436 #if 0
2437 /* XXX */
2438 printf("run next request\n");
2439 sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2440 #endif
2441 #if 1
2442 printf("reset start\n");
2443 sbp_reset_start(sdev);
2444 #endif
2445 }
2446
2447 static void
2448 sbp_timeout(void *arg)
2449 {
2450 struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2451 struct sbp_dev *sdev = ocb->sdev;
2452
2453 sbp_show_sdev_info(sdev, 2);
2454 printf("request timeout(cmd orb:0x%08x) ... ",
2455 (uint32_t)ocb->bus_addr);
2456
2457 sdev->timeout ++;
2458 switch(sdev->timeout) {
2459 case 1:
2460 printf("agent reset\n");
2461 SBP_DEVICE_FREEZE(sdev, 1);
2462 sdev->freeze ++;
2463 sbp_abort_all_ocbs(sdev, XS_CMD_TIMEOUT);
2464 sbp_agent_reset(sdev);
2465 break;
2466 case 2:
2467 case 3:
2468 sbp_target_reset(sdev, sdev->timeout - 1);
2469 break;
2470 #if 0
2471 default:
2472 /* XXX give up */
2473 SBP_DETACH_TARGET(target);
2474 if (target->luns != NULL)
2475 free(target->luns, M_SBP);
2476 target->num_lun = 0;;
2477 target->luns = NULL;
2478 target->fwdev = NULL;
2479 #endif
2480 }
2481 }
2482
2483 static void
2484 sbp_action1(struct sbp_softc *sbp, sbp_scsi_xfer *sxfer)
2485 {
2486
2487 struct sbp_target *target = NULL;
2488 struct sbp_dev *sdev = NULL;
2489
2490 /* target:lun -> sdev mapping */
2491 if (sbp != NULL) {
2492 target = &sbp->target;
2493 if (target->fwdev != NULL
2494 && NOT_LUN_WILDCARD(SCSI_XFER_LUN(sxfer))
2495 && SCSI_XFER_LUN(sxfer) < target->num_lun) {
2496 sdev = target->luns[SCSI_XFER_LUN(sxfer)];
2497 if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED &&
2498 sdev->status != SBP_DEV_PROBE)
2499 sdev = NULL;
2500 }
2501 }
2502
2503 SBP_DEBUG(1)
2504 if (sdev == NULL)
2505 printf("invalid target %d lun %d\n",
2506 SCSI_XFER_TARGET(sxfer), SCSI_XFER_LUN(sxfer));
2507 END_DEBUG
2508
2509 switch (SCSI_XFER_FUNCCODE(sxfer)) {
2510 case XPT_SCSI_IO:
2511 #if defined(__FreeBSD__)
2512 case XPT_RESET_DEV:
2513 case XPT_GET_TRAN_SETTINGS:
2514 case XPT_SET_TRAN_SETTINGS:
2515 case XPT_CALC_GEOMETRY:
2516 #endif
2517 if (sdev == NULL) {
2518 SBP_DEBUG(1)
2519 printf("%s:%d:%d:func_code 0x%04x: "
2520 "Invalid target (target needed)\n",
2521 sbp ? device_get_nameunit(sbp->fd.dev) : "???",
2522 SCSI_XFER_TARGET(sxfer), SCSI_XFER_LUN(sxfer),
2523 SCSI_XFER_FUNCCODE(sxfer));
2524 END_DEBUG
2525
2526 SCSI_XFER_ERROR(sxfer) = XS_DEV_NOT_THERE;
2527 SCSI_TRANSFER_DONE(sxfer);
2528 return;
2529 }
2530 break;
2531 #if defined(__FreeBSD__)
2532 case XPT_PATH_INQ:
2533 case XPT_NOOP:
2534 /* The opcodes sometimes aimed at a target (sc is valid),
2535 * sometimes aimed at the SIM (sc is invalid and target is
2536 * CAM_TARGET_WILDCARD)
2537 */
2538 if (sbp == NULL &&
2539 sxfer->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2540 SBP_DEBUG(0)
2541 printf("%s:%d:%d func_code 0x%04x: "
2542 "Invalid target (no wildcard)\n",
2543 device_get_nameunit(sbp->fd.dev),
2544 sxfer->ccb_h.target_id, sxfer->ccb_h.target_lun,
2545 sxfer->ccb_h.func_code);
2546 END_DEBUG
2547 SCSI_XFER_ERROR(sxfer) = XS_DEV_NOT_THERE;
2548 SCSI_TRANSFER_DONE(sxfer);
2549 return;
2550 }
2551 break;
2552 #endif
2553 default:
2554 /* XXX Hm, we should check the input parameters */
2555 break;
2556 }
2557
2558 switch (SCSI_XFER_FUNCCODE(sxfer)) {
2559 case XPT_SCSI_IO:
2560 {
2561 struct sbp_ocb *ocb;
2562 int speed;
2563 void *cdb;
2564
2565 SBP_DEBUG(2)
2566 printf("%s:%d:%d XPT_SCSI_IO: "
2567 "cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2568 ", flags: 0x%02x, "
2569 "%db cmd/%db data/%db sense\n",
2570 device_get_nameunit(sbp->fd.dev),
2571 SCSI_XFER_TARGET(sxfer), SCSI_XFER_LUN(sxfer),
2572 SCSI_XFER_10BCMD_DUMP(sxfer),
2573 SCSI_XFER_DIR(sxfer),
2574 SCSI_XFER_CMDLEN(sxfer), SCSI_XFER_DATALEN(sxfer),
2575 SCSI_XFER_SENSELEN(sxfer));
2576 END_DEBUG
2577 if(sdev == NULL){
2578 SCSI_XFER_ERROR(sxfer) = XS_DEV_NOT_THERE;
2579 SCSI_TRANSFER_DONE(sxfer);
2580 return;
2581 }
2582 #if 0
2583 /* if we are in probe stage, pass only probe commands */
2584 if (sdev->status == SBP_DEV_PROBE) {
2585 char *name;
2586 name = xpt_path_periph(sxfer->ccb_h.path)->periph_name;
2587 printf("probe stage, periph name: %s\n", name);
2588 if (strcmp(name, "probe") != 0) {
2589 SCSI_XFER_ERROR(sxfer) = XS_REQUEUE_REQ;
2590 SCSI_TRANSFER_DONE(sxfer);
2591 return;
2592 }
2593 }
2594 #endif
2595 if ((ocb = sbp_get_ocb(sdev)) == NULL) {
2596 SCSI_XFER_ERROR(sxfer) = XS_REQUEUE_REQ;
2597 if (sdev->freeze == 0) {
2598 SBP_DEVICE_FREEZE(sdev, 1);
2599 sdev->freeze ++;
2600 }
2601 SCSI_TRANSFER_DONE(sxfer);
2602 return;
2603 }
2604
2605 ocb->flags = OCB_ACT_CMD;
2606 ocb->sdev = sdev;
2607 ocb->sxfer = sxfer;
2608 #if defined(__FreeBSD__)
2609 sxfer->ccb_h.ccb_sdev_ptr = sdev;
2610 #endif
2611 ocb->orb[0] = htonl(1 << 31);
2612 ocb->orb[1] = 0;
2613 ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2614 ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2615 speed = min(target->fwdev->speed, max_speed);
2616 ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2617 | ORB_CMD_MAXP(speed + 7));
2618 if(SCSI_XFER_DIR(sxfer) == SCSI_XFER_DATA_IN){
2619 ocb->orb[4] |= htonl(ORB_CMD_IN);
2620 }
2621
2622 if (CAM_XFER_FLAGS(sxfer) & CAM_SCATTER_VALID)
2623 printf("sbp: CAM_SCATTER_VALID\n");
2624 if (CAM_XFER_FLAGS(sxfer) & CAM_DATA_PHYS)
2625 printf("sbp: CAM_DATA_PHYS\n");
2626
2627 cdb = SCSI_XFER_CMD(sxfer);
2628 bcopy(cdb, (void *)&ocb->orb[5], SCSI_XFER_CMDLEN(sxfer));
2629 /*
2630 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2631 printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2632 */
2633 if (SCSI_XFER_DATALEN(sxfer) > 0) {
2634 int s, error;
2635
2636 s = splsoftvm();
2637 error = fw_bus_dmamap_load(/*dma tag*/sbp->dmat,
2638 /*dma map*/ocb->dmamap,
2639 SCSI_XFER_DATA(sxfer),
2640 SCSI_XFER_DATALEN(sxfer),
2641 sbp_execute_ocb,
2642 ocb,
2643 /*flags*/0);
2644 splx(s);
2645 if (error)
2646 printf("sbp: bus_dmamap_load error %d\n", error);
2647 } else
2648 sbp_execute_ocb(ocb, NULL, 0, 0);
2649 break;
2650 }
2651 #if defined(__FreeBSD__)
2652 case XPT_CALC_GEOMETRY:
2653 {
2654 struct ccb_calc_geometry *ccg;
2655 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2656 uint32_t size_mb;
2657 uint32_t secs_per_cylinder;
2658 int extended = 1;
2659 #endif
2660
2661 ccg = &sxfer->ccg;
2662 if (ccg->block_size == 0) {
2663 printf("sbp_action1: block_size is 0.\n");
2664 SCSI_XFER_ERROR(sxfer) = XS_REQ_INVALID;
2665 SCSI_TRANSFER_DONE(sxfer);
2666 break;
2667 }
2668 SBP_DEBUG(1)
2669 printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2670 #if defined(__DragonFly__) || __FreeBSD_version < 500000
2671 "Volume size = %d\n",
2672 #else
2673 "Volume size = %jd\n",
2674 #endif
2675 device_get_nameunit(sbp->fd.dev),
2676 cam_sim_path(sbp->sim),
2677 sxfer->ccb_h.target_id, sxfer->ccb_h.target_lun,
2678 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2679 (uintmax_t)
2680 #endif
2681 ccg->volume_size);
2682 END_DEBUG
2683
2684 #if defined(__DragonFly__) || __FreeBSD_version < 501100
2685 size_mb = ccg->volume_size
2686 / ((1024L * 1024L) / ccg->block_size);
2687
2688 if (size_mb > 1024 && extended) {
2689 ccg->heads = 255;
2690 ccg->secs_per_track = 63;
2691 } else {
2692 ccg->heads = 64;
2693 ccg->secs_per_track = 32;
2694 }
2695 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2696 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2697 SCSI_XFER_ERROR(sxfer) = XS_REQ_CMP;
2698 #else
2699 cam_calc_geometry(ccg, /*extended*/1);
2700 #endif
2701 SCSI_TRANSFER_DONE(sxfer);
2702 break;
2703 }
2704 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
2705 {
2706
2707 SBP_DEBUG(1)
2708 printf("%s:%d:XPT_RESET_BUS: \n",
2709 device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2710 END_DEBUG
2711
2712 SCSI_XFER_ERROR(sxfer) = XS_REQ_INVALID;
2713 SCSI_TRANSFER_DONE(sxfer);
2714 break;
2715 }
2716 case XPT_PATH_INQ: /* Path routing inquiry */
2717 {
2718 struct ccb_pathinq *cpi = &sxfer->cpi;
2719 struct cam_sim *sim = sbp->sim;
2720
2721 SBP_DEBUG(1)
2722 printf("%s:%d:%d XPT_PATH_INQ:.\n",
2723 device_get_nameunit(sbp->fd.dev),
2724 sxfer->ccb_h.target_id, sxfer->ccb_h.target_lun);
2725 END_DEBUG
2726 cpi->version_num = 1; /* XXX??? */
2727 cpi->hba_inquiry = PI_TAG_ABLE;
2728 cpi->target_sprt = 0;
2729 cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE;
2730 cpi->hba_eng_cnt = 0;
2731 cpi->max_target = SBP_NUM_TARGETS - 1;
2732 cpi->max_lun = SBP_NUM_LUNS - 1;
2733 cpi->initiator_id = SBP_INITIATOR;
2734 cpi->bus_id = sim->bus_id;
2735 cpi->base_transfer_speed = 400 * 1000 / 8;
2736 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2737 strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2738 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2739 cpi->unit_number = sim->unit_number;
2740 cpi->transport = XPORT_SPI; /* XX should havea FireWire */
2741 cpi->transport_version = 2;
2742 cpi->protocol = PROTO_SCSI;
2743 cpi->protocol_version = SCSI_REV_2;
2744
2745 SCSI_XFER_ERROR(cpi) = XS_REQ_CMP;
2746 SCSI_TRANSFER_DONE(sxfer);
2747 break;
2748 }
2749 case XPT_GET_TRAN_SETTINGS:
2750 {
2751 struct ccb_trans_settings *cts = &sxfer->cts;
2752 struct ccb_trans_settings_scsi *scsi =
2753 &cts->proto_specific.scsi;
2754 struct ccb_trans_settings_spi *spi =
2755 &cts->xport_specific.spi;
2756
2757 cts->protocol = PROTO_SCSI;
2758 cts->protocol_version = SCSI_REV_2;
2759 cts->transport = XPORT_SPI; /* should have a FireWire */
2760 cts->transport_version = 2;
2761 spi->valid = CTS_SPI_VALID_DISC;
2762 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
2763 scsi->valid = CTS_SCSI_VALID_TQ;
2764 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2765 SBP_DEBUG(1)
2766 printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2767 device_get_nameunit(sbp->fd.dev),
2768 sxfer->ccb_h.target_id, sxfer->ccb_h.target_lun);
2769 END_DEBUG
2770 SCSI_XFER_ERROR(cts) = XS_REQ_CMP;
2771 SCSI_TRANSFER_DONE(sxfer);
2772 break;
2773 }
2774 case XPT_ABORT:
2775 SCSI_XFER_ERROR(sxfer) = XS_UA_ABORT;
2776 SCSI_TRANSFER_DONE(sxfer);
2777 break;
2778 case XPT_SET_TRAN_SETTINGS:
2779 /* XXX */
2780 default:
2781 SCSI_XFER_ERROR(sxfer) = XS_REQ_INVALID;
2782 SCSI_TRANSFER_DONE(sxfer);
2783 break;
2784 #endif
2785 }
2786 return;
2787 }
2788
2789 #if defined(__FreeBSD__)
2790 static void
2791 sbp_action(struct cam_sim *sim, sbp_scsi_xfer *sxfer)
2792 {
2793 int s;
2794
2795 s = splfw();
2796 sbp_action1(sim->softc, sxfer);
2797 splx(s);
2798 }
2799 #endif
2800
2801 static void
2802 sbp_execute_ocb(void *arg, bus_dma_segment_t *segments, int seg, int error)
2803 {
2804 int i;
2805 struct sbp_ocb *ocb;
2806 struct sbp_ocb *prev;
2807 bus_dma_segment_t *s;
2808
2809 if (error)
2810 printf("sbp_execute_ocb: error=%d\n", error);
2811
2812 ocb = (struct sbp_ocb *)arg;
2813
2814 SBP_DEBUG(2)
2815 printf("sbp_execute_ocb: seg %d", seg);
2816 for (i = 0; i < seg; i++)
2817 #if defined(__DragonFly__) || \
2818 (defined(__FreeBSD__) && __FreeBSD_version < 500000)
2819 printf(", %x:%d", segments[i].ds_addr, segments[i].ds_len);
2820 #else
2821 printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2822 (uintmax_t)segments[i].ds_len);
2823 #endif
2824 printf("\n");
2825 END_DEBUG
2826
2827 if (seg == 1) {
2828 /* direct pointer */
2829 s = &segments[0];
2830 if (s->ds_len > SBP_SEG_MAX)
2831 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2832 ocb->orb[3] = htonl(s->ds_addr);
2833 ocb->orb[4] |= htonl(s->ds_len);
2834 } else if(seg > 1) {
2835 /* page table */
2836 for (i = 0; i < seg; i++) {
2837 s = &segments[i];
2838 SBP_DEBUG(0)
2839 /* XXX LSI Logic "< 16 byte" bug might be hit */
2840 if (s->ds_len < 16)
2841 printf("sbp_execute_ocb: warning, "
2842 #if defined(__DragonFly__) || \
2843 (defined(__FreeBSD__) && __FreeBSD_version < 500000)
2844 "segment length(%d) is less than 16."
2845 #else
2846 "segment length(%jd) is less than 16."
2847 #endif
2848 "(seg=%d/%d)\n", (uintmax_t)s->ds_len, i+1, seg);
2849 END_DEBUG
2850 if (s->ds_len > SBP_SEG_MAX)
2851 panic("ds_len > SBP_SEG_MAX, fix busdma code");
2852 ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2853 ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2854 }
2855 ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2856 }
2857
2858 if (seg > 0)
2859 fw_bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2860 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2861 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2862 prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2863 fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2864 if (use_doorbell) {
2865 if (prev == NULL) {
2866 if (ocb->sdev->last_ocb != NULL)
2867 sbp_doorbell(ocb->sdev);
2868 else
2869 sbp_orb_pointer(ocb->sdev, ocb);
2870 }
2871 } else {
2872 if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) {
2873 ocb->sdev->flags &= ~ORB_LINK_DEAD;
2874 sbp_orb_pointer(ocb->sdev, ocb);
2875 }
2876 }
2877 }
2878
2879 #if defined(__FreeBSD__)
2880 static void
2881 sbp_poll(struct cam_sim *sim)
2882 {
2883 struct sbp_softc *sbp;
2884 struct firewire_comm *fc;
2885
2886 sbp = (struct sbp_softc *)sim->softc;
2887 fc = sbp->fd.fc;
2888
2889 fc->poll(fc, 0, -1);
2890
2891 return;
2892 }
2893
2894 #endif
2895 static struct sbp_ocb *
2896 sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2897 {
2898 struct sbp_ocb *ocb;
2899 struct sbp_ocb *next;
2900 int s = splfw(), order = 0;
2901 int flags;
2902
2903 SBP_DEBUG(1)
2904 sbp_show_sdev_info(sdev, 2);
2905 #if defined(__DragonFly__) || \
2906 (defined(__FreeBSD__) && __FreeBSD_version < 500000)
2907 printf("%s: 0x%08lx src %d\n",
2908 #else
2909 printf("%s: 0x%08x src %d\n",
2910 #endif
2911 __func__, ntohl(sbp_status->orb_lo), sbp_status->src);
2912 END_DEBUG
2913 for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2914 next = STAILQ_NEXT(ocb, ocb);
2915 flags = ocb->flags;
2916 if (OCB_MATCH(ocb, sbp_status)) {
2917 /* found */
2918 STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2919 if (ocb->sxfer != NULL)
2920 #if defined(__DragonFly__) || defined(__NetBSD__)
2921 callout_stop(&SCSI_XFER_CALLOUT(ocb->sxfer));
2922 #else
2923 untimeout(sbp_timeout, (void *)ocb,
2924 SCSI_XFER_CALLOUT(ocb->sxfer));
2925 #endif
2926 if (ntohl(ocb->orb[4]) & 0xffff) {
2927 fw_bus_dmamap_sync(sdev->target->sbp->dmat,
2928 ocb->dmamap,
2929 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2930 BUS_DMASYNC_POSTREAD :
2931 BUS_DMASYNC_POSTWRITE);
2932 fw_bus_dmamap_unload(sdev->target->sbp->dmat,
2933 ocb->dmamap);
2934 }
2935 if (!use_doorbell) {
2936 if (sbp_status->src == SRC_NO_NEXT) {
2937 if (next != NULL)
2938 sbp_orb_pointer(sdev, next);
2939 else if (order > 0) {
2940 /*
2941 * Unordered execution
2942 * We need to send pointer for
2943 * next ORB
2944 */
2945 sdev->flags |= ORB_LINK_DEAD;
2946 }
2947 }
2948 } else {
2949 /*
2950 * XXX this is not correct for unordered
2951 * execution.
2952 */
2953 if (sdev->last_ocb != NULL)
2954 sbp_free_ocb(sdev, sdev->last_ocb);
2955 sdev->last_ocb = ocb;
2956 if (next != NULL &&
2957 sbp_status->src == SRC_NO_NEXT)
2958 sbp_doorbell(sdev);
2959 }
2960 break;
2961 } else
2962 order ++;
2963 }
2964 splx(s);
2965 SBP_DEBUG(0)
2966 if (ocb && order > 0) {
2967 sbp_show_sdev_info(sdev, 2);
2968 printf("unordered execution order:%d\n", order);
2969 }
2970 END_DEBUG
2971 return (ocb);
2972 }
2973
2974 static struct sbp_ocb *
2975 sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2976 {
2977 int s = splfw();
2978 struct sbp_ocb *prev, *prev2;
2979
2980 SBP_DEBUG(1)
2981 sbp_show_sdev_info(sdev, 2);
2982 #if defined(__DragonFly__) || \
2983 (defined(__FreeBSD__) && __FreeBSD_version < 500000)
2984 printf("%s: 0x%08x\n", __func__, ocb->bus_addr);
2985 #else
2986 printf("%s: 0x%08jx\n", __func__, (uintmax_t)ocb->bus_addr);
2987 #endif
2988 END_DEBUG
2989 prev2 = prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2990 STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2991
2992 if (ocb->sxfer != NULL)
2993 #if defined(__DragonFly__) || defined(__NetBSD__)
2994 callout_reset(&SCSI_XFER_CALLOUT(ocb->sxfer),
2995 mstohz(SCSI_XFER_TIMEOUT(ocb->sxfer)), sbp_timeout, ocb);
2996 #else
2997 SCSI_XFER_CALLOUT(ocb->sxfer) = timeout(sbp_timeout,
2998 (void *)ocb, mstohz(SCSI_XFER_TIMEOUT(ocb->sxfer)));
2999 #endif
3000
3001 if (use_doorbell && prev == NULL)
3002 prev2 = sdev->last_ocb;
3003
3004 if (prev2 != NULL) {
3005 SBP_DEBUG(2)
3006 #if defined(__DragonFly__) || \
3007 (defined(__FreeBSD__) && __FreeBSD_version < 500000)
3008 printf("linking chain 0x%x -> 0x%x\n",
3009 prev2->bus_addr, ocb->bus_addr);
3010 #else
3011 printf("linking chain 0x%jx -> 0x%jx\n",
3012 (uintmax_t)prev2->bus_addr, (uintmax_t)ocb->bus_addr);
3013 #endif
3014 END_DEBUG
3015 prev2->orb[1] = htonl(ocb->bus_addr);
3016 prev2->orb[0] = 0;
3017 }
3018 splx(s);
3019
3020 return prev;
3021 }
3022
3023 static struct sbp_ocb *
3024 sbp_get_ocb(struct sbp_dev *sdev)
3025 {
3026 struct sbp_ocb *ocb;
3027 int s = splfw();
3028 ocb = STAILQ_FIRST(&sdev->free_ocbs);
3029 if (ocb == NULL) {
3030 sdev->flags |= ORB_SHORTAGE;
3031 printf("ocb shortage!!!\n");
3032 splx(s);
3033 return NULL;
3034 }
3035 STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
3036 splx(s);
3037 ocb->sxfer = NULL;
3038 return (ocb);
3039 }
3040
3041 static void
3042 sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
3043 {
3044 ocb->flags = 0;
3045 ocb->sxfer = NULL;
3046 STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
3047 if ((sdev->flags & ORB_SHORTAGE) != 0) {
3048 int count;
3049
3050 sdev->flags &= ~ORB_SHORTAGE;
3051 count = sdev->freeze;
3052 sdev->freeze = 0;
3053 SBP_DEVICE_THAW(sdev, count);
3054 }
3055 }
3056
3057 static void
3058 sbp_abort_ocb(struct sbp_ocb *ocb, int status)
3059 {
3060 struct sbp_dev *sdev;
3061
3062 sdev = ocb->sdev;
3063 SBP_DEBUG(0)
3064 sbp_show_sdev_info(sdev, 2);
3065 #if defined(__DragonFly__) || \
3066 (defined(__FreeBSD__) && __FreeBSD_version < 500000)
3067 printf("sbp_abort_ocb 0x%x\n", ocb->bus_addr);
3068 #else
3069 printf("sbp_abort_ocb 0x%jx\n", (uintmax_t)ocb->bus_addr);
3070 #endif
3071 END_DEBUG
3072 SBP_DEBUG(1)
3073 if (ocb->sxfer != NULL)
3074 sbp_print_scsi_cmd(ocb);
3075 END_DEBUG
3076 if (ntohl(ocb->orb[4]) & 0xffff) {
3077 fw_bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
3078 (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
3079 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
3080 fw_bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
3081 }
3082 if (ocb->sxfer != NULL) {
3083 #if defined(__DragonFly__ ) || defined(__NetBSD__)
3084 callout_stop(&SCSI_XFER_CALLOUT(ocb->sxfer));
3085 #else
3086 untimeout(sbp_timeout, (void *)ocb,
3087 SCSI_XFER_CALLOUT(ocb->sxfer));
3088 #endif
3089 SCSI_XFER_ERROR(ocb->sxfer) = status;
3090 SCSI_TRANSFER_DONE(ocb->sxfer);
3091 }
3092 sbp_free_ocb(sdev, ocb);
3093 }
3094
3095 static void
3096 sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
3097 {
3098 int s;
3099 struct sbp_ocb *ocb, *next;
3100 STAILQ_HEAD(, sbp_ocb) temp;
3101
3102 s = splfw();
3103
3104 bcopy(&sdev->ocbs, &temp, sizeof(temp));
3105 STAILQ_INIT(&sdev->ocbs);
3106 for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
3107 next = STAILQ_NEXT(ocb, ocb);
3108 sbp_abort_ocb(ocb, status);
3109 }
3110 if (sdev->last_ocb != NULL) {
3111 sbp_free_ocb(sdev, sdev->last_ocb);
3112 sdev->last_ocb = NULL;
3113 }
3114
3115 splx(s);
3116 }
3117
3118 #if defined(__FreeBSD__)
3119 static devclass_t sbp_devclass;
3120
3121 static device_method_t sbp_methods[] = {
3122 /* device interface */
3123 DEVMETHOD(device_probe, sbp_probe),
3124 DEVMETHOD(device_attach, sbp_attach),
3125 DEVMETHOD(device_detach, sbp_detach),
3126 DEVMETHOD(device_shutdown, sbp_shutdown),
3127
3128 { 0, 0 }
3129 };
3130
3131 static driver_t sbp_driver = {
3132 "sbp",
3133 sbp_methods,
3134 sizeof(struct sbp_softc),
3135 };
3136 #ifdef __DragonFly__
3137 DECLARE_DUMMY_MODULE(sbp);
3138 #endif
3139 DRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
3140 MODULE_VERSION(sbp, 1);
3141 MODULE_DEPEND(sbp, firewire, 1, 1, 1);
3142 MODULE_DEPEND(sbp, cam, 1, 1, 1);
3143 #elif defined(__NetBSD__)
3144 static void
3145 sbp_scsipi_request(
3146 struct scsipi_channel *channel, scsipi_adapter_req_t req, void *arg)
3147 {
3148 int i, s;
3149 struct sbp_softc *sbp =
3150 (struct sbp_softc *)channel->chan_adapter->adapt_dev;
3151 struct scsipi_xfer *xs = arg;
3152
3153 if (debug > 1)
3154 printf("Called sbpscsi_scsipi_request\n");
3155
3156 switch (req) {
3157 case ADAPTER_REQ_RUN_XFER:
3158 if (debug > 1) {
3159 printf("Got req_run_xfer\n");
3160 printf("xs control: 0x%08x, timeout: %d\n",
3161 xs->xs_control, xs->timeout);
3162 printf("opcode: 0x%02x\n", (int)xs->cmd->opcode);
3163 for (i = 0; i < 15; i++)
3164 printf("0x%02x ",(int)xs->cmd->bytes[i]);
3165 printf("\n");
3166 }
3167 if (xs->xs_control & XS_CTL_RESET) {
3168 if (debug > 1)
3169 printf("XS_CTL_RESET not support\n");
3170 break;
3171 }
3172 #define SBPSCSI_SBP2_MAX_CDB 12
3173 if (xs->cmdlen > SBPSCSI_SBP2_MAX_CDB) {
3174 if (debug > 0)
3175 printf(
3176 "sbp doesn't support cdb's larger than %d "
3177 "bytes\n", SBPSCSI_SBP2_MAX_CDB);
3178 SCSI_XFER_ERROR(xs) = XS_REQ_INVALID;
3179 SCSI_TRANSFER_DONE(xs);
3180 return;
3181 }
3182 s = splfw();
3183 sbp_action1(sbp, xs);
3184 splx(s);
3185
3186 break;
3187 case ADAPTER_REQ_GROW_RESOURCES:
3188 if (debug > 1)
3189 printf("Got req_grow_resources\n");
3190 break;
3191 case ADAPTER_REQ_SET_XFER_MODE:
3192 if (debug > 1)
3193 printf("Got set xfer mode\n");
3194 break;
3195 default:
3196 panic("Unknown request: %d\n", (int)req);
3197 }
3198 }
3199
3200 static void
3201 sbp_minphys(struct buf *bp)
3202 {
3203 minphys(bp);
3204 }
3205
3206 CFATTACH_DECL(sbp, sizeof (struct sbp_softc),
3207 sbpmatch, sbpattach, sbpdetach, NULL);
3208 #endif
3209