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