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