sbp.c revision 1.39 1 1.39 chs /* $NetBSD: sbp.c,v 1.39 2019/11/10 21:16:35 chs Exp $ */
2 1.1 kiyohara /*-
3 1.1 kiyohara * Copyright (c) 2003 Hidetoshi Shimokawa
4 1.1 kiyohara * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
5 1.1 kiyohara * All rights reserved.
6 1.1 kiyohara *
7 1.1 kiyohara * Redistribution and use in source and binary forms, with or without
8 1.1 kiyohara * modification, are permitted provided that the following conditions
9 1.1 kiyohara * are met:
10 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright
11 1.1 kiyohara * notice, this list of conditions and the following disclaimer.
12 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the
14 1.1 kiyohara * documentation and/or other materials provided with the distribution.
15 1.1 kiyohara * 3. All advertising materials mentioning features or use of this software
16 1.1 kiyohara * must display the acknowledgement as bellow:
17 1.1 kiyohara *
18 1.1 kiyohara * This product includes software developed by K. Kobayashi and H. Shimokawa
19 1.1 kiyohara *
20 1.1 kiyohara * 4. The name of the author may not be used to endorse or promote products
21 1.1 kiyohara * derived from this software without specific prior written permission.
22 1.1 kiyohara *
23 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.1 kiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 1.1 kiyohara * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 1.1 kiyohara * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27 1.1 kiyohara * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 1.1 kiyohara * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 1.1 kiyohara * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 1.1 kiyohara * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 1.1 kiyohara * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.1 kiyohara * POSSIBILITY OF SUCH DAMAGE.
34 1.29 kiyohara *
35 1.29 kiyohara * $FreeBSD: src/sys/dev/firewire/sbp.c,v 1.100 2009/02/18 18:41:34 sbruno Exp $
36 1.1 kiyohara *
37 1.1 kiyohara */
38 1.1 kiyohara
39 1.20 lukem #include <sys/cdefs.h>
40 1.39 chs __KERNEL_RCSID(0, "$NetBSD: sbp.c,v 1.39 2019/11/10 21:16:35 chs Exp $");
41 1.1 kiyohara
42 1.1 kiyohara
43 1.1 kiyohara #include <sys/param.h>
44 1.1 kiyohara #include <sys/device.h>
45 1.1 kiyohara #include <sys/errno.h>
46 1.1 kiyohara #include <sys/buf.h>
47 1.29 kiyohara #include <sys/callout.h>
48 1.29 kiyohara #include <sys/condvar.h>
49 1.1 kiyohara #include <sys/kernel.h>
50 1.1 kiyohara #include <sys/kthread.h>
51 1.32 christos #include <sys/malloc.h>
52 1.29 kiyohara #include <sys/mutex.h>
53 1.1 kiyohara #include <sys/proc.h>
54 1.1 kiyohara #include <sys/sysctl.h>
55 1.1 kiyohara
56 1.18 ad #include <sys/bus.h>
57 1.1 kiyohara
58 1.1 kiyohara #include <dev/scsipi/scsi_spc.h>
59 1.1 kiyohara #include <dev/scsipi/scsi_all.h>
60 1.1 kiyohara #include <dev/scsipi/scsipi_all.h>
61 1.1 kiyohara #include <dev/scsipi/scsiconf.h>
62 1.1 kiyohara #include <dev/scsipi/scsipiconf.h>
63 1.1 kiyohara
64 1.1 kiyohara #include <dev/ieee1394/firewire.h>
65 1.1 kiyohara #include <dev/ieee1394/firewirereg.h>
66 1.1 kiyohara #include <dev/ieee1394/fwdma.h>
67 1.1 kiyohara #include <dev/ieee1394/iec13213.h>
68 1.1 kiyohara #include <dev/ieee1394/sbp.h>
69 1.1 kiyohara
70 1.1 kiyohara #include "locators.h"
71 1.1 kiyohara
72 1.1 kiyohara
73 1.29 kiyohara #define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
74 1.29 kiyohara && crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
75 1.29 kiyohara
76 1.29 kiyohara #define SBP_NUM_TARGETS 8 /* MAX 64 */
77 1.29 kiyohara #define SBP_NUM_LUNS 64
78 1.29 kiyohara #define SBP_MAXPHYS MIN(MAXPHYS, (512*1024) /* 512KB */)
79 1.29 kiyohara #define SBP_DMA_SIZE PAGE_SIZE
80 1.29 kiyohara #define SBP_LOGIN_SIZE sizeof(struct sbp_login_res)
81 1.1 kiyohara #define SBP_QUEUE_LEN ((SBP_DMA_SIZE - SBP_LOGIN_SIZE) / sizeof(struct sbp_ocb))
82 1.29 kiyohara #define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS)
83 1.1 kiyohara
84 1.29 kiyohara /*
85 1.1 kiyohara * STATUS FIFO addressing
86 1.1 kiyohara * bit
87 1.1 kiyohara * -----------------------
88 1.1 kiyohara * 0- 1( 2): 0 (alignment)
89 1.1 kiyohara * 2- 9( 8): lun
90 1.1 kiyohara * 10-31(14): unit
91 1.29 kiyohara * 32-47(16): SBP_BIND_HI
92 1.29 kiyohara * 48-64(16): bus_id, node_id
93 1.1 kiyohara */
94 1.1 kiyohara #define SBP_BIND_HI 0x1
95 1.30 kiyohara #define SBP_DEV2ADDR(u, l) \
96 1.30 kiyohara (((uint64_t)SBP_BIND_HI << 32) |\
97 1.30 kiyohara (((u) & 0x3fff) << 10) |\
98 1.30 kiyohara (((l) & 0xff) << 2))
99 1.1 kiyohara #define SBP_ADDR2UNIT(a) (((a) >> 10) & 0x3fff)
100 1.1 kiyohara #define SBP_ADDR2LUN(a) (((a) >> 2) & 0xff)
101 1.1 kiyohara #define SBP_INITIATOR 7
102 1.1 kiyohara
103 1.1 kiyohara static const char *orb_fun_name[] = {
104 1.1 kiyohara ORB_FUN_NAMES
105 1.1 kiyohara };
106 1.1 kiyohara
107 1.1 kiyohara static int debug = 0;
108 1.1 kiyohara static int auto_login = 1;
109 1.1 kiyohara static int max_speed = -1;
110 1.1 kiyohara static int sbp_cold = 1;
111 1.1 kiyohara static int ex_login = 1;
112 1.1 kiyohara static int login_delay = 1000; /* msec */
113 1.1 kiyohara static int scan_delay = 500; /* msec */
114 1.1 kiyohara static int use_doorbell = 0;
115 1.1 kiyohara static int sbp_tags = 0;
116 1.1 kiyohara
117 1.1 kiyohara static int sysctl_sbp_verify(SYSCTLFN_PROTO, int lower, int upper);
118 1.1 kiyohara static int sysctl_sbp_verify_max_speed(SYSCTLFN_PROTO);
119 1.1 kiyohara static int sysctl_sbp_verify_tags(SYSCTLFN_PROTO);
120 1.1 kiyohara
121 1.1 kiyohara /*
122 1.1 kiyohara * Setup sysctl(3) MIB, hw.sbp.*
123 1.1 kiyohara *
124 1.22 ad * TBD condition CTLFLAG_PERMANENT on being a module or not
125 1.1 kiyohara */
126 1.1 kiyohara SYSCTL_SETUP(sysctl_sbp, "sysctl sbp(4) subtree setup")
127 1.1 kiyohara {
128 1.1 kiyohara int rc, sbp_node_num;
129 1.1 kiyohara const struct sysctlnode *node;
130 1.1 kiyohara
131 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node,
132 1.1 kiyohara CTLFLAG_PERMANENT, CTLTYPE_NODE, "sbp",
133 1.1 kiyohara SYSCTL_DESCR("sbp controls"), NULL, 0, NULL,
134 1.29 kiyohara 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
135 1.1 kiyohara goto err;
136 1.1 kiyohara sbp_node_num = node->sysctl_num;
137 1.1 kiyohara
138 1.1 kiyohara /* sbp auto login flag */
139 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node,
140 1.1 kiyohara CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
141 1.1 kiyohara "auto_login", SYSCTL_DESCR("SBP perform login automatically"),
142 1.1 kiyohara NULL, 0, &auto_login,
143 1.29 kiyohara 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0)
144 1.1 kiyohara goto err;
145 1.1 kiyohara
146 1.1 kiyohara /* sbp max speed */
147 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node,
148 1.1 kiyohara CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
149 1.1 kiyohara "max_speed", SYSCTL_DESCR("SBP transfer max speed"),
150 1.1 kiyohara sysctl_sbp_verify_max_speed, 0, &max_speed,
151 1.29 kiyohara 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0)
152 1.1 kiyohara goto err;
153 1.1 kiyohara
154 1.1 kiyohara /* sbp exclusive login flag */
155 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node,
156 1.1 kiyohara CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
157 1.1 kiyohara "exclusive_login", SYSCTL_DESCR("SBP enable exclusive login"),
158 1.1 kiyohara NULL, 0, &ex_login,
159 1.29 kiyohara 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0)
160 1.1 kiyohara goto err;
161 1.1 kiyohara
162 1.1 kiyohara /* sbp login delay */
163 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node,
164 1.1 kiyohara CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
165 1.1 kiyohara "login_delay", SYSCTL_DESCR("SBP login delay in msec"),
166 1.1 kiyohara NULL, 0, &login_delay,
167 1.29 kiyohara 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0)
168 1.1 kiyohara goto err;
169 1.1 kiyohara
170 1.1 kiyohara /* sbp scan delay */
171 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node,
172 1.1 kiyohara CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
173 1.1 kiyohara "scan_delay", SYSCTL_DESCR("SBP scan delay in msec"),
174 1.1 kiyohara NULL, 0, &scan_delay,
175 1.29 kiyohara 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0)
176 1.1 kiyohara goto err;
177 1.1 kiyohara
178 1.1 kiyohara /* sbp use doorbell flag */
179 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node,
180 1.1 kiyohara CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
181 1.1 kiyohara "use_doorbell", SYSCTL_DESCR("SBP use doorbell request"),
182 1.1 kiyohara NULL, 0, &use_doorbell,
183 1.29 kiyohara 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0)
184 1.1 kiyohara goto err;
185 1.1 kiyohara
186 1.1 kiyohara /* sbp force tagged queuing */
187 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node,
188 1.1 kiyohara CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
189 1.1 kiyohara "tags", SYSCTL_DESCR("SBP tagged queuing support"),
190 1.1 kiyohara sysctl_sbp_verify_tags, 0, &sbp_tags,
191 1.29 kiyohara 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0)
192 1.1 kiyohara goto err;
193 1.1 kiyohara
194 1.1 kiyohara /* sbp driver debug flag */
195 1.1 kiyohara if ((rc = sysctl_createv(clog, 0, NULL, &node,
196 1.1 kiyohara CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
197 1.1 kiyohara "sbp_debug", SYSCTL_DESCR("SBP debug flag"),
198 1.1 kiyohara NULL, 0, &debug,
199 1.29 kiyohara 0, CTL_HW, sbp_node_num, CTL_CREATE, CTL_EOL)) != 0)
200 1.1 kiyohara goto err;
201 1.1 kiyohara
202 1.1 kiyohara return;
203 1.1 kiyohara
204 1.1 kiyohara err:
205 1.29 kiyohara aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
206 1.1 kiyohara }
207 1.1 kiyohara
208 1.1 kiyohara static int
209 1.1 kiyohara sysctl_sbp_verify(SYSCTLFN_ARGS, int lower, int upper)
210 1.1 kiyohara {
211 1.1 kiyohara int error, t;
212 1.1 kiyohara struct sysctlnode node;
213 1.1 kiyohara
214 1.1 kiyohara node = *rnode;
215 1.1 kiyohara t = *(int*)rnode->sysctl_data;
216 1.1 kiyohara node.sysctl_data = &t;
217 1.1 kiyohara error = sysctl_lookup(SYSCTLFN_CALL(&node));
218 1.1 kiyohara if (error || newp == NULL)
219 1.29 kiyohara return error;
220 1.1 kiyohara
221 1.1 kiyohara if (t < lower || t > upper)
222 1.29 kiyohara return EINVAL;
223 1.1 kiyohara
224 1.1 kiyohara *(int*)rnode->sysctl_data = t;
225 1.1 kiyohara
226 1.29 kiyohara return 0;
227 1.1 kiyohara }
228 1.1 kiyohara
229 1.1 kiyohara static int
230 1.1 kiyohara sysctl_sbp_verify_max_speed(SYSCTLFN_ARGS)
231 1.1 kiyohara {
232 1.29 kiyohara
233 1.29 kiyohara return sysctl_sbp_verify(SYSCTLFN_CALL(rnode), 0, FWSPD_S400);
234 1.1 kiyohara }
235 1.1 kiyohara
236 1.1 kiyohara static int
237 1.1 kiyohara sysctl_sbp_verify_tags(SYSCTLFN_ARGS)
238 1.1 kiyohara {
239 1.29 kiyohara
240 1.29 kiyohara return sysctl_sbp_verify(SYSCTLFN_CALL(rnode), -1, 1);
241 1.1 kiyohara }
242 1.1 kiyohara
243 1.1 kiyohara #define NEED_RESPONSE 0
244 1.1 kiyohara
245 1.1 kiyohara #define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE)
246 1.1 kiyohara #ifdef __sparc64__ /* iommu */
247 1.16 kiyohara #define SBP_IND_MAX howmany(SBP_MAXPHYS, SBP_SEG_MAX)
248 1.1 kiyohara #else
249 1.16 kiyohara #define SBP_IND_MAX howmany(SBP_MAXPHYS, PAGE_SIZE)
250 1.1 kiyohara #endif
251 1.1 kiyohara struct sbp_ocb {
252 1.1 kiyohara uint32_t orb[8];
253 1.29 kiyohara #define IND_PTR_OFFSET (sizeof(uint32_t) * 8)
254 1.29 kiyohara struct ind_ptr ind_ptr[SBP_IND_MAX];
255 1.29 kiyohara struct scsipi_xfer *xs;
256 1.1 kiyohara struct sbp_dev *sdev;
257 1.29 kiyohara uint16_t index;
258 1.29 kiyohara uint16_t flags; /* XXX should be removed */
259 1.1 kiyohara bus_dmamap_t dmamap;
260 1.29 kiyohara bus_addr_t bus_addr;
261 1.29 kiyohara STAILQ_ENTRY(sbp_ocb) ocb;
262 1.1 kiyohara };
263 1.1 kiyohara
264 1.29 kiyohara #define SBP_ORB_DMA_SYNC(dma, i, op) \
265 1.29 kiyohara bus_dmamap_sync((dma).dma_tag, (dma).dma_map, \
266 1.29 kiyohara sizeof(struct sbp_ocb) * (i), \
267 1.29 kiyohara sizeof(ocb->orb) + sizeof(ocb->ind_ptr), (op));
268 1.29 kiyohara
269 1.1 kiyohara #define OCB_ACT_MGM 0
270 1.1 kiyohara #define OCB_ACT_CMD 1
271 1.1 kiyohara #define OCB_MATCH(o,s) ((o)->bus_addr == ntohl((s)->orb_lo))
272 1.1 kiyohara
273 1.1 kiyohara struct sbp_dev{
274 1.1 kiyohara #define SBP_DEV_RESET 0 /* accept login */
275 1.1 kiyohara #define SBP_DEV_LOGIN 1 /* to login */
276 1.1 kiyohara #if 0
277 1.1 kiyohara #define SBP_DEV_RECONN 2 /* to reconnect */
278 1.1 kiyohara #endif
279 1.1 kiyohara #define SBP_DEV_TOATTACH 3 /* to attach */
280 1.1 kiyohara #define SBP_DEV_PROBE 4 /* scan lun */
281 1.1 kiyohara #define SBP_DEV_ATTACHED 5 /* in operation */
282 1.1 kiyohara #define SBP_DEV_DEAD 6 /* unavailable unit */
283 1.1 kiyohara #define SBP_DEV_RETRY 7 /* unavailable unit */
284 1.1 kiyohara uint8_t status:4,
285 1.1 kiyohara timeout:4;
286 1.1 kiyohara uint8_t type;
287 1.1 kiyohara uint16_t lun_id;
288 1.1 kiyohara uint16_t freeze;
289 1.1 kiyohara #define ORB_LINK_DEAD (1 << 0)
290 1.1 kiyohara #define VALID_LUN (1 << 1)
291 1.1 kiyohara #define ORB_POINTER_ACTIVE (1 << 2)
292 1.1 kiyohara #define ORB_POINTER_NEED (1 << 3)
293 1.1 kiyohara #define ORB_DOORBELL_ACTIVE (1 << 4)
294 1.1 kiyohara #define ORB_DOORBELL_NEED (1 << 5)
295 1.1 kiyohara #define ORB_SHORTAGE (1 << 6)
296 1.1 kiyohara uint16_t flags;
297 1.1 kiyohara struct scsipi_periph *periph;
298 1.1 kiyohara struct sbp_target *target;
299 1.1 kiyohara struct fwdma_alloc dma;
300 1.1 kiyohara struct sbp_login_res *login;
301 1.1 kiyohara struct callout login_callout;
302 1.1 kiyohara struct sbp_ocb *ocb;
303 1.1 kiyohara STAILQ_HEAD(, sbp_ocb) ocbs;
304 1.1 kiyohara STAILQ_HEAD(, sbp_ocb) free_ocbs;
305 1.1 kiyohara struct sbp_ocb *last_ocb;
306 1.1 kiyohara char vendor[32];
307 1.1 kiyohara char product[32];
308 1.1 kiyohara char revision[10];
309 1.29 kiyohara char bustgtlun[32];
310 1.1 kiyohara };
311 1.1 kiyohara
312 1.1 kiyohara struct sbp_target {
313 1.1 kiyohara int target_id;
314 1.1 kiyohara int num_lun;
315 1.1 kiyohara struct sbp_dev **luns;
316 1.1 kiyohara struct sbp_softc *sbp;
317 1.1 kiyohara struct fw_device *fwdev;
318 1.1 kiyohara uint32_t mgm_hi, mgm_lo;
319 1.1 kiyohara struct sbp_ocb *mgm_ocb_cur;
320 1.1 kiyohara STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue;
321 1.1 kiyohara struct callout mgm_ocb_timeout;
322 1.1 kiyohara STAILQ_HEAD(, fw_xfer) xferlist;
323 1.1 kiyohara int n_xfer;
324 1.1 kiyohara };
325 1.1 kiyohara
326 1.1 kiyohara struct sbp_softc {
327 1.29 kiyohara struct firewire_dev_comm sc_fd;
328 1.29 kiyohara struct scsipi_adapter sc_adapter;
329 1.1 kiyohara struct scsipi_channel sc_channel;
330 1.21 kiyohara device_t sc_bus;
331 1.29 kiyohara struct lwp *sc_lwp;
332 1.29 kiyohara struct sbp_target sc_target;
333 1.29 kiyohara struct fw_bind sc_fwb;
334 1.29 kiyohara bus_dma_tag_t sc_dmat;
335 1.29 kiyohara struct timeval sc_last_busreset;
336 1.29 kiyohara int sc_flags;
337 1.29 kiyohara kmutex_t sc_mtx;
338 1.29 kiyohara kcondvar_t sc_cv;
339 1.1 kiyohara };
340 1.1 kiyohara
341 1.29 kiyohara MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/IEEE1394");
342 1.34 dsl MALLOC_DECLARE(M_SBP);
343 1.29 kiyohara
344 1.29 kiyohara
345 1.29 kiyohara static int sbpmatch(device_t, cfdata_t, void *);
346 1.29 kiyohara static void sbpattach(device_t, device_t, void *);
347 1.29 kiyohara static int sbpdetach(device_t, int);
348 1.29 kiyohara
349 1.29 kiyohara static void sbp_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t,
350 1.29 kiyohara void *);
351 1.29 kiyohara static void sbp_minphys(struct buf *);
352 1.29 kiyohara
353 1.29 kiyohara static void sbp_show_sdev_info(struct sbp_dev *);
354 1.29 kiyohara static void sbp_alloc_lun(struct sbp_target *);
355 1.29 kiyohara static struct sbp_target *sbp_alloc_target(struct sbp_softc *,
356 1.29 kiyohara struct fw_device *);
357 1.29 kiyohara static void sbp_probe_lun(struct sbp_dev *);
358 1.29 kiyohara static void sbp_login_callout(void *);
359 1.29 kiyohara static void sbp_login(struct sbp_dev *);
360 1.29 kiyohara static void sbp_probe_target(void *);
361 1.29 kiyohara static void sbp_post_busreset(void *);
362 1.29 kiyohara static void sbp_post_explore(void *);
363 1.29 kiyohara #if NEED_RESPONSE
364 1.29 kiyohara static void sbp_loginres_callback(struct fw_xfer *);
365 1.1 kiyohara #endif
366 1.29 kiyohara static inline void sbp_xfer_free(struct fw_xfer *);
367 1.29 kiyohara static void sbp_reset_start_callback(struct fw_xfer *);
368 1.29 kiyohara static void sbp_reset_start(struct sbp_dev *);
369 1.29 kiyohara static void sbp_mgm_callback(struct fw_xfer *);
370 1.29 kiyohara static void sbp_scsipi_scan_target(void *);
371 1.29 kiyohara static inline void sbp_scan_dev(struct sbp_dev *);
372 1.29 kiyohara static void sbp_do_attach(struct fw_xfer *);
373 1.29 kiyohara static void sbp_agent_reset_callback(struct fw_xfer *);
374 1.29 kiyohara static void sbp_agent_reset(struct sbp_dev *);
375 1.29 kiyohara static void sbp_busy_timeout_callback(struct fw_xfer *);
376 1.29 kiyohara static void sbp_busy_timeout(struct sbp_dev *);
377 1.29 kiyohara static void sbp_orb_pointer_callback(struct fw_xfer *);
378 1.29 kiyohara static void sbp_orb_pointer(struct sbp_dev *, struct sbp_ocb *);
379 1.29 kiyohara static void sbp_doorbell_callback(struct fw_xfer *);
380 1.1 kiyohara static void sbp_doorbell(struct sbp_dev *);
381 1.29 kiyohara static struct fw_xfer *sbp_write_cmd(struct sbp_dev *, int, int);
382 1.29 kiyohara static void sbp_mgm_orb(struct sbp_dev *, int, struct sbp_ocb *);
383 1.29 kiyohara static void sbp_print_scsi_cmd(struct sbp_ocb *);
384 1.29 kiyohara static void sbp_scsi_status(struct sbp_status *, struct sbp_ocb *);
385 1.29 kiyohara static void sbp_fix_inq_data(struct sbp_ocb *);
386 1.29 kiyohara static void sbp_recv(struct fw_xfer *);
387 1.29 kiyohara static int sbp_logout_all(struct sbp_softc *);
388 1.1 kiyohara static void sbp_free_sdev(struct sbp_dev *);
389 1.29 kiyohara static void sbp_free_target(struct sbp_target *);
390 1.29 kiyohara static void sbp_scsipi_detach_sdev(struct sbp_dev *);
391 1.29 kiyohara static void sbp_scsipi_detach_target(struct sbp_target *);
392 1.29 kiyohara static void sbp_target_reset(struct sbp_dev *, int);
393 1.29 kiyohara static void sbp_mgm_timeout(void *);
394 1.29 kiyohara static void sbp_timeout(void *);
395 1.29 kiyohara static void sbp_action1(struct sbp_softc *, struct scsipi_xfer *);
396 1.29 kiyohara static void sbp_execute_ocb(struct sbp_ocb *, bus_dma_segment_t *, int);
397 1.29 kiyohara static struct sbp_ocb *sbp_dequeue_ocb(struct sbp_dev *, struct sbp_status *);
398 1.29 kiyohara static struct sbp_ocb *sbp_enqueue_ocb(struct sbp_dev *, struct sbp_ocb *);
399 1.29 kiyohara static struct sbp_ocb *sbp_get_ocb(struct sbp_dev *);
400 1.29 kiyohara static void sbp_free_ocb(struct sbp_dev *, struct sbp_ocb *);
401 1.29 kiyohara static void sbp_abort_ocb(struct sbp_ocb *, int);
402 1.29 kiyohara static void sbp_abort_all_ocbs(struct sbp_dev *, int);
403 1.1 kiyohara
404 1.1 kiyohara
405 1.1 kiyohara static const char *orb_status0[] = {
406 1.1 kiyohara /* 0 */ "No additional information to report",
407 1.1 kiyohara /* 1 */ "Request type not supported",
408 1.1 kiyohara /* 2 */ "Speed not supported",
409 1.1 kiyohara /* 3 */ "Page size not supported",
410 1.1 kiyohara /* 4 */ "Access denied",
411 1.1 kiyohara /* 5 */ "Logical unit not supported",
412 1.1 kiyohara /* 6 */ "Maximum payload too small",
413 1.1 kiyohara /* 7 */ "Reserved for future standardization",
414 1.1 kiyohara /* 8 */ "Resources unavailable",
415 1.1 kiyohara /* 9 */ "Function rejected",
416 1.1 kiyohara /* A */ "Login ID not recognized",
417 1.1 kiyohara /* B */ "Dummy ORB completed",
418 1.1 kiyohara /* C */ "Request aborted",
419 1.1 kiyohara /* FF */ "Unspecified error"
420 1.1 kiyohara #define MAX_ORB_STATUS0 0xd
421 1.1 kiyohara };
422 1.1 kiyohara
423 1.1 kiyohara static const char *orb_status1_object[] = {
424 1.1 kiyohara /* 0 */ "Operation request block (ORB)",
425 1.1 kiyohara /* 1 */ "Data buffer",
426 1.1 kiyohara /* 2 */ "Page table",
427 1.1 kiyohara /* 3 */ "Unable to specify"
428 1.1 kiyohara };
429 1.1 kiyohara
430 1.1 kiyohara static const char *orb_status1_serial_bus_error[] = {
431 1.1 kiyohara /* 0 */ "Missing acknowledge",
432 1.1 kiyohara /* 1 */ "Reserved; not to be used",
433 1.1 kiyohara /* 2 */ "Time-out error",
434 1.1 kiyohara /* 3 */ "Reserved; not to be used",
435 1.1 kiyohara /* 4 */ "Busy retry limit exceeded(X)",
436 1.1 kiyohara /* 5 */ "Busy retry limit exceeded(A)",
437 1.1 kiyohara /* 6 */ "Busy retry limit exceeded(B)",
438 1.1 kiyohara /* 7 */ "Reserved for future standardization",
439 1.1 kiyohara /* 8 */ "Reserved for future standardization",
440 1.1 kiyohara /* 9 */ "Reserved for future standardization",
441 1.1 kiyohara /* A */ "Reserved for future standardization",
442 1.1 kiyohara /* B */ "Tardy retry limit exceeded",
443 1.1 kiyohara /* C */ "Conflict error",
444 1.1 kiyohara /* D */ "Data error",
445 1.1 kiyohara /* E */ "Type error",
446 1.1 kiyohara /* F */ "Address error"
447 1.1 kiyohara };
448 1.1 kiyohara
449 1.29 kiyohara
450 1.29 kiyohara CFATTACH_DECL_NEW(sbp, sizeof(struct sbp_softc),
451 1.29 kiyohara sbpmatch, sbpattach, sbpdetach, NULL);
452 1.29 kiyohara
453 1.29 kiyohara
454 1.29 kiyohara int
455 1.29 kiyohara sbpmatch(device_t parent, cfdata_t cf, void *aux)
456 1.29 kiyohara {
457 1.29 kiyohara struct fw_attach_args *fwa = aux;
458 1.29 kiyohara
459 1.29 kiyohara if (strcmp(fwa->name, "sbp") == 0)
460 1.29 kiyohara return 1;
461 1.29 kiyohara return 0;
462 1.29 kiyohara }
463 1.29 kiyohara
464 1.1 kiyohara static void
465 1.29 kiyohara sbpattach(device_t parent, device_t self, void *aux)
466 1.1 kiyohara {
467 1.29 kiyohara struct sbp_softc *sc = device_private(self);
468 1.29 kiyohara struct fw_attach_args *fwa = (struct fw_attach_args *)aux;
469 1.29 kiyohara struct firewire_comm *fc;
470 1.29 kiyohara struct scsipi_adapter *sc_adapter = &sc->sc_adapter;
471 1.29 kiyohara struct scsipi_channel *sc_channel = &sc->sc_channel;
472 1.29 kiyohara struct sbp_target *target = &sc->sc_target;
473 1.29 kiyohara int dv_unit;
474 1.29 kiyohara
475 1.29 kiyohara aprint_naive("\n");
476 1.29 kiyohara aprint_normal(": SBP-2/SCSI over IEEE1394\n");
477 1.29 kiyohara
478 1.29 kiyohara sc->sc_fd.dev = self;
479 1.29 kiyohara
480 1.29 kiyohara if (cold)
481 1.29 kiyohara sbp_cold++;
482 1.29 kiyohara sc->sc_fd.fc = fc = fwa->fc;
483 1.29 kiyohara mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_VM);
484 1.29 kiyohara cv_init(&sc->sc_cv, "sbp");
485 1.29 kiyohara
486 1.29 kiyohara if (max_speed < 0)
487 1.29 kiyohara max_speed = fc->speed;
488 1.29 kiyohara
489 1.29 kiyohara sc->sc_dmat = fc->dmat;
490 1.29 kiyohara
491 1.29 kiyohara sc->sc_target.fwdev = NULL;
492 1.29 kiyohara sc->sc_target.luns = NULL;
493 1.29 kiyohara
494 1.33 cegger /* Initialize mutexes and lists before we can error out
495 1.33 cegger * to prevent crashes on detach
496 1.33 cegger */
497 1.33 cegger mutex_init(&sc->sc_fwb.fwb_mtx, MUTEX_DEFAULT, IPL_VM);
498 1.33 cegger STAILQ_INIT(&sc->sc_fwb.xferlist);
499 1.33 cegger
500 1.29 kiyohara if (sbp_alloc_target(sc, fwa->fwdev) == NULL)
501 1.29 kiyohara return;
502 1.29 kiyohara
503 1.29 kiyohara sc_adapter->adapt_dev = sc->sc_fd.dev;
504 1.29 kiyohara sc_adapter->adapt_nchannels = 1;
505 1.29 kiyohara sc_adapter->adapt_max_periph = 1;
506 1.29 kiyohara sc_adapter->adapt_request = sbp_scsipi_request;
507 1.29 kiyohara sc_adapter->adapt_minphys = sbp_minphys;
508 1.29 kiyohara sc_adapter->adapt_openings = 8;
509 1.29 kiyohara
510 1.29 kiyohara sc_channel->chan_adapter = sc_adapter;
511 1.29 kiyohara sc_channel->chan_bustype = &scsi_bustype;
512 1.29 kiyohara sc_channel->chan_defquirks = PQUIRK_ONLYBIG;
513 1.29 kiyohara sc_channel->chan_channel = 0;
514 1.29 kiyohara sc_channel->chan_flags = SCSIPI_CHAN_CANGROW | SCSIPI_CHAN_NOSETTLE;
515 1.29 kiyohara
516 1.29 kiyohara sc_channel->chan_ntargets = 1;
517 1.29 kiyohara sc_channel->chan_nluns = target->num_lun; /* We set nluns 0 now */
518 1.29 kiyohara sc_channel->chan_id = 1;
519 1.29 kiyohara
520 1.29 kiyohara sc->sc_bus = config_found(sc->sc_fd.dev, sc_channel, scsiprint);
521 1.29 kiyohara if (sc->sc_bus == NULL) {
522 1.29 kiyohara aprint_error_dev(self, "attach failed\n");
523 1.29 kiyohara return;
524 1.29 kiyohara }
525 1.29 kiyohara
526 1.29 kiyohara /* We reserve 16 bit space (4 bytes X 64 unit X 256 luns) */
527 1.29 kiyohara dv_unit = device_unit(sc->sc_fd.dev);
528 1.29 kiyohara sc->sc_fwb.start = SBP_DEV2ADDR(dv_unit, 0);
529 1.29 kiyohara sc->sc_fwb.end = SBP_DEV2ADDR(dv_unit, -1);
530 1.29 kiyohara /* pre-allocate xfer */
531 1.29 kiyohara fw_xferlist_add(&sc->sc_fwb.xferlist, M_SBP,
532 1.29 kiyohara /*send*/ 0, /*recv*/ SBP_RECV_LEN, SBP_NUM_OCB / 2,
533 1.29 kiyohara fc, (void *)sc, sbp_recv);
534 1.29 kiyohara fw_bindadd(fc, &sc->sc_fwb);
535 1.29 kiyohara
536 1.29 kiyohara sc->sc_fd.post_busreset = sbp_post_busreset;
537 1.29 kiyohara sc->sc_fd.post_explore = sbp_post_explore;
538 1.1 kiyohara
539 1.29 kiyohara if (fc->status != FWBUSNOTREADY) {
540 1.29 kiyohara sbp_post_busreset((void *)sc);
541 1.29 kiyohara sbp_post_explore((void *)sc);
542 1.29 kiyohara }
543 1.1 kiyohara }
544 1.1 kiyohara
545 1.1 kiyohara static int
546 1.29 kiyohara sbpdetach(device_t self, int flags)
547 1.1 kiyohara {
548 1.29 kiyohara struct sbp_softc *sc = device_private(self);
549 1.29 kiyohara struct firewire_comm *fc = sc->sc_fd.fc;
550 1.29 kiyohara
551 1.29 kiyohara sbp_scsipi_detach_target(&sc->sc_target);
552 1.1 kiyohara
553 1.33 cegger if (sc->sc_target.fwdev && SBP_FWDEV_ALIVE(sc->sc_target.fwdev)) {
554 1.29 kiyohara sbp_logout_all(sc);
555 1.1 kiyohara
556 1.29 kiyohara /* XXX wait for logout completion */
557 1.29 kiyohara mutex_enter(&sc->sc_mtx);
558 1.29 kiyohara cv_timedwait_sig(&sc->sc_cv, &sc->sc_mtx, hz/2);
559 1.29 kiyohara mutex_exit(&sc->sc_mtx);
560 1.1 kiyohara }
561 1.1 kiyohara
562 1.29 kiyohara sbp_free_target(&sc->sc_target);
563 1.29 kiyohara
564 1.29 kiyohara fw_bindremove(fc, &sc->sc_fwb);
565 1.29 kiyohara fw_xferlist_remove(&sc->sc_fwb.xferlist);
566 1.29 kiyohara mutex_destroy(&sc->sc_fwb.fwb_mtx);
567 1.29 kiyohara
568 1.29 kiyohara mutex_destroy(&sc->sc_mtx);
569 1.33 cegger cv_destroy(&sc->sc_cv);
570 1.29 kiyohara
571 1.29 kiyohara return 0;
572 1.29 kiyohara }
573 1.29 kiyohara
574 1.29 kiyohara
575 1.29 kiyohara static void
576 1.29 kiyohara sbp_scsipi_request(struct scsipi_channel *channel, scsipi_adapter_req_t req,
577 1.29 kiyohara void *arg)
578 1.29 kiyohara {
579 1.29 kiyohara struct sbp_softc *sc = device_private(channel->chan_adapter->adapt_dev);
580 1.29 kiyohara struct scsipi_xfer *xs = arg;
581 1.29 kiyohara int i;
582 1.29 kiyohara
583 1.29 kiyohara SBP_DEBUG(1)
584 1.29 kiyohara printf("Called sbp_scsipi_request\n");
585 1.29 kiyohara END_DEBUG
586 1.1 kiyohara
587 1.29 kiyohara switch (req) {
588 1.29 kiyohara case ADAPTER_REQ_RUN_XFER:
589 1.29 kiyohara SBP_DEBUG(1)
590 1.29 kiyohara printf("Got req_run_xfer\n");
591 1.29 kiyohara printf("xs control: 0x%08x, timeout: %d\n",
592 1.29 kiyohara xs->xs_control, xs->timeout);
593 1.29 kiyohara printf("opcode: 0x%02x\n", (int)xs->cmd->opcode);
594 1.29 kiyohara for (i = 0; i < 15; i++)
595 1.29 kiyohara printf("0x%02x ",(int)xs->cmd->bytes[i]);
596 1.29 kiyohara printf("\n");
597 1.29 kiyohara END_DEBUG
598 1.29 kiyohara if (xs->xs_control & XS_CTL_RESET) {
599 1.29 kiyohara SBP_DEBUG(1)
600 1.29 kiyohara printf("XS_CTL_RESET not support\n");
601 1.29 kiyohara END_DEBUG
602 1.29 kiyohara break;
603 1.29 kiyohara }
604 1.29 kiyohara #define SBPSCSI_SBP2_MAX_CDB 12
605 1.29 kiyohara if (xs->cmdlen > SBPSCSI_SBP2_MAX_CDB) {
606 1.29 kiyohara SBP_DEBUG(0)
607 1.29 kiyohara printf(
608 1.29 kiyohara "sbp doesn't support cdb's larger than %d bytes\n",
609 1.29 kiyohara SBPSCSI_SBP2_MAX_CDB);
610 1.29 kiyohara END_DEBUG
611 1.29 kiyohara xs->error = XS_DRIVER_STUFFUP;
612 1.29 kiyohara scsipi_done(xs);
613 1.29 kiyohara return;
614 1.29 kiyohara }
615 1.29 kiyohara sbp_action1(sc, xs);
616 1.1 kiyohara
617 1.29 kiyohara break;
618 1.29 kiyohara case ADAPTER_REQ_GROW_RESOURCES:
619 1.29 kiyohara SBP_DEBUG(1)
620 1.29 kiyohara printf("Got req_grow_resources\n");
621 1.29 kiyohara END_DEBUG
622 1.29 kiyohara break;
623 1.29 kiyohara case ADAPTER_REQ_SET_XFER_MODE:
624 1.29 kiyohara SBP_DEBUG(1)
625 1.29 kiyohara printf("Got set xfer mode\n");
626 1.29 kiyohara END_DEBUG
627 1.29 kiyohara break;
628 1.29 kiyohara default:
629 1.29 kiyohara panic("Unknown request: %d\n", (int)req);
630 1.29 kiyohara }
631 1.1 kiyohara }
632 1.29 kiyohara
633 1.29 kiyohara static void
634 1.29 kiyohara sbp_minphys(struct buf *bp)
635 1.1 kiyohara {
636 1.1 kiyohara
637 1.29 kiyohara minphys(bp);
638 1.1 kiyohara }
639 1.1 kiyohara
640 1.29 kiyohara
641 1.29 kiyohara /*
642 1.29 kiyohara * Display device characteristics on the console
643 1.29 kiyohara */
644 1.1 kiyohara static void
645 1.29 kiyohara sbp_show_sdev_info(struct sbp_dev *sdev)
646 1.1 kiyohara {
647 1.29 kiyohara struct fw_device *fwdev = sdev->target->fwdev;
648 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp;
649 1.1 kiyohara
650 1.29 kiyohara aprint_normal_dev(sc->sc_fd.dev,
651 1.29 kiyohara "ordered:%d type:%d EUI:%08x%08x node:%d speed:%d maxrec:%d\n",
652 1.29 kiyohara (sdev->type & 0x40) >> 6,
653 1.29 kiyohara (sdev->type & 0x1f),
654 1.29 kiyohara fwdev->eui.hi,
655 1.29 kiyohara fwdev->eui.lo,
656 1.29 kiyohara fwdev->dst,
657 1.29 kiyohara fwdev->speed,
658 1.29 kiyohara fwdev->maxrec);
659 1.29 kiyohara aprint_normal_dev(sc->sc_fd.dev, "%s '%s' '%s' '%s'\n",
660 1.29 kiyohara sdev->bustgtlun, sdev->vendor, sdev->product, sdev->revision);
661 1.1 kiyohara }
662 1.1 kiyohara
663 1.1 kiyohara static void
664 1.1 kiyohara sbp_alloc_lun(struct sbp_target *target)
665 1.1 kiyohara {
666 1.1 kiyohara struct crom_context cc;
667 1.1 kiyohara struct csrreg *reg;
668 1.1 kiyohara struct sbp_dev *sdev, **newluns;
669 1.29 kiyohara struct sbp_softc *sc;
670 1.1 kiyohara int maxlun, lun, i;
671 1.1 kiyohara
672 1.29 kiyohara sc = target->sbp;
673 1.1 kiyohara crom_init_context(&cc, target->fwdev->csrrom);
674 1.1 kiyohara /* XXX shoud parse appropriate unit directories only */
675 1.1 kiyohara maxlun = -1;
676 1.1 kiyohara while (cc.depth >= 0) {
677 1.1 kiyohara reg = crom_search_key(&cc, CROM_LUN);
678 1.1 kiyohara if (reg == NULL)
679 1.1 kiyohara break;
680 1.1 kiyohara lun = reg->val & 0xffff;
681 1.1 kiyohara SBP_DEBUG(0)
682 1.1 kiyohara printf("target %d lun %d found\n", target->target_id, lun);
683 1.1 kiyohara END_DEBUG
684 1.1 kiyohara if (maxlun < lun)
685 1.1 kiyohara maxlun = lun;
686 1.1 kiyohara crom_next(&cc);
687 1.1 kiyohara }
688 1.1 kiyohara if (maxlun < 0)
689 1.29 kiyohara aprint_normal_dev(sc->sc_fd.dev, "%d: no LUN found\n",
690 1.1 kiyohara target->target_id);
691 1.1 kiyohara
692 1.29 kiyohara maxlun++;
693 1.1 kiyohara if (maxlun >= SBP_NUM_LUNS)
694 1.1 kiyohara maxlun = SBP_NUM_LUNS;
695 1.1 kiyohara
696 1.1 kiyohara /* Invalidiate stale devices */
697 1.29 kiyohara for (lun = 0; lun < target->num_lun; lun++) {
698 1.1 kiyohara sdev = target->luns[lun];
699 1.1 kiyohara if (sdev == NULL)
700 1.1 kiyohara continue;
701 1.1 kiyohara sdev->flags &= ~VALID_LUN;
702 1.1 kiyohara if (lun >= maxlun) {
703 1.1 kiyohara /* lost device */
704 1.29 kiyohara sbp_scsipi_detach_sdev(sdev);
705 1.1 kiyohara sbp_free_sdev(sdev);
706 1.29 kiyohara target->luns[lun] = NULL;
707 1.1 kiyohara }
708 1.1 kiyohara }
709 1.1 kiyohara
710 1.1 kiyohara /* Reallocate */
711 1.1 kiyohara if (maxlun != target->num_lun) {
712 1.32 christos newluns = (struct sbp_dev **) realloc(target->luns,
713 1.32 christos sizeof(struct sbp_dev *) * maxlun,
714 1.39 chs M_SBP, M_WAITOK | M_ZERO);
715 1.1 kiyohara
716 1.1 kiyohara /*
717 1.1 kiyohara * We must zero the extended region for the case
718 1.1 kiyohara * realloc() doesn't allocate new buffer.
719 1.1 kiyohara */
720 1.32 christos if (maxlun > target->num_lun) {
721 1.32 christos const int sbp_dev_p_sz = sizeof(struct sbp_dev *);
722 1.32 christos
723 1.25 cegger memset(&newluns[target->num_lun], 0,
724 1.29 kiyohara sbp_dev_p_sz * (maxlun - target->num_lun));
725 1.32 christos }
726 1.1 kiyohara
727 1.1 kiyohara target->luns = newluns;
728 1.1 kiyohara target->num_lun = maxlun;
729 1.1 kiyohara }
730 1.1 kiyohara
731 1.1 kiyohara crom_init_context(&cc, target->fwdev->csrrom);
732 1.1 kiyohara while (cc.depth >= 0) {
733 1.1 kiyohara int new = 0;
734 1.1 kiyohara
735 1.1 kiyohara reg = crom_search_key(&cc, CROM_LUN);
736 1.1 kiyohara if (reg == NULL)
737 1.1 kiyohara break;
738 1.1 kiyohara lun = reg->val & 0xffff;
739 1.1 kiyohara if (lun >= SBP_NUM_LUNS) {
740 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, "too large lun %d\n",
741 1.29 kiyohara lun);
742 1.1 kiyohara goto next;
743 1.1 kiyohara }
744 1.1 kiyohara
745 1.1 kiyohara sdev = target->luns[lun];
746 1.1 kiyohara if (sdev == NULL) {
747 1.32 christos sdev = malloc(sizeof(struct sbp_dev),
748 1.39 chs M_SBP, M_WAITOK | M_ZERO);
749 1.1 kiyohara target->luns[lun] = sdev;
750 1.1 kiyohara sdev->lun_id = lun;
751 1.1 kiyohara sdev->target = target;
752 1.1 kiyohara STAILQ_INIT(&sdev->ocbs);
753 1.29 kiyohara callout_init(&sdev->login_callout, CALLOUT_MPSAFE);
754 1.29 kiyohara callout_setfunc(&sdev->login_callout,
755 1.29 kiyohara sbp_login_callout, sdev);
756 1.1 kiyohara sdev->status = SBP_DEV_RESET;
757 1.1 kiyohara new = 1;
758 1.29 kiyohara snprintf(sdev->bustgtlun, 32, "%s:%d:%d",
759 1.29 kiyohara device_xname(sc->sc_fd.dev),
760 1.29 kiyohara sdev->target->target_id,
761 1.29 kiyohara sdev->lun_id);
762 1.29 kiyohara if (!sc->sc_lwp)
763 1.29 kiyohara if (kthread_create(
764 1.29 kiyohara PRI_NONE, KTHREAD_MPSAFE, NULL,
765 1.29 kiyohara sbp_scsipi_scan_target, &sc->sc_target,
766 1.29 kiyohara &sc->sc_lwp,
767 1.29 kiyohara "sbp%d_attach", device_unit(sc->sc_fd.dev)))
768 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev,
769 1.29 kiyohara "unable to create thread");
770 1.1 kiyohara }
771 1.1 kiyohara sdev->flags |= VALID_LUN;
772 1.1 kiyohara sdev->type = (reg->val & 0xff0000) >> 16;
773 1.1 kiyohara
774 1.1 kiyohara if (new == 0)
775 1.1 kiyohara goto next;
776 1.1 kiyohara
777 1.29 kiyohara fwdma_alloc_setup(sc->sc_fd.dev, sc->sc_dmat, SBP_DMA_SIZE,
778 1.29 kiyohara &sdev->dma, sizeof(uint32_t), BUS_DMA_NOWAIT);
779 1.1 kiyohara if (sdev->dma.v_addr == NULL) {
780 1.32 christos free(sdev, M_SBP);
781 1.1 kiyohara target->luns[lun] = NULL;
782 1.1 kiyohara goto next;
783 1.1 kiyohara }
784 1.29 kiyohara sdev->ocb = (struct sbp_ocb *)sdev->dma.v_addr;
785 1.29 kiyohara sdev->login = (struct sbp_login_res *)&sdev->ocb[SBP_QUEUE_LEN];
786 1.25 cegger memset((char *)sdev->ocb, 0,
787 1.29 kiyohara sizeof(struct sbp_ocb) * SBP_QUEUE_LEN);
788 1.1 kiyohara
789 1.1 kiyohara STAILQ_INIT(&sdev->free_ocbs);
790 1.1 kiyohara for (i = 0; i < SBP_QUEUE_LEN; i++) {
791 1.29 kiyohara struct sbp_ocb *ocb = &sdev->ocb[i];
792 1.29 kiyohara
793 1.29 kiyohara ocb->index = i;
794 1.29 kiyohara ocb->bus_addr =
795 1.29 kiyohara sdev->dma.bus_addr + sizeof(struct sbp_ocb) * i;
796 1.29 kiyohara if (bus_dmamap_create(sc->sc_dmat, 0x100000,
797 1.29 kiyohara SBP_IND_MAX, SBP_SEG_MAX, 0, 0, &ocb->dmamap)) {
798 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev,
799 1.29 kiyohara "cannot create dmamap %d\n", i);
800 1.1 kiyohara /* XXX */
801 1.1 kiyohara goto next;
802 1.1 kiyohara }
803 1.29 kiyohara sbp_free_ocb(sdev, ocb); /* into free queue */
804 1.1 kiyohara }
805 1.1 kiyohara next:
806 1.1 kiyohara crom_next(&cc);
807 1.1 kiyohara }
808 1.1 kiyohara
809 1.29 kiyohara for (lun = 0; lun < target->num_lun; lun++) {
810 1.1 kiyohara sdev = target->luns[lun];
811 1.1 kiyohara if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) {
812 1.29 kiyohara sbp_scsipi_detach_sdev(sdev);
813 1.1 kiyohara sbp_free_sdev(sdev);
814 1.1 kiyohara target->luns[lun] = NULL;
815 1.1 kiyohara }
816 1.1 kiyohara }
817 1.1 kiyohara }
818 1.1 kiyohara
819 1.1 kiyohara static struct sbp_target *
820 1.29 kiyohara sbp_alloc_target(struct sbp_softc *sc, struct fw_device *fwdev)
821 1.1 kiyohara {
822 1.1 kiyohara struct sbp_target *target;
823 1.1 kiyohara struct crom_context cc;
824 1.1 kiyohara struct csrreg *reg;
825 1.1 kiyohara
826 1.1 kiyohara SBP_DEBUG(1)
827 1.1 kiyohara printf("sbp_alloc_target\n");
828 1.1 kiyohara END_DEBUG
829 1.1 kiyohara /* new target */
830 1.29 kiyohara target = &sc->sc_target;
831 1.29 kiyohara target->sbp = sc;
832 1.1 kiyohara target->fwdev = fwdev;
833 1.1 kiyohara target->target_id = 0;
834 1.33 cegger target->mgm_ocb_cur = NULL;
835 1.33 cegger SBP_DEBUG(1)
836 1.33 cegger printf("target: mgm_port: %x\n", target->mgm_lo);
837 1.33 cegger END_DEBUG
838 1.33 cegger STAILQ_INIT(&target->xferlist);
839 1.33 cegger target->n_xfer = 0;
840 1.33 cegger STAILQ_INIT(&target->mgm_ocb_queue);
841 1.33 cegger callout_init(&target->mgm_ocb_timeout, CALLOUT_MPSAFE);
842 1.33 cegger
843 1.33 cegger target->luns = NULL;
844 1.33 cegger target->num_lun = 0;
845 1.33 cegger
846 1.1 kiyohara /* XXX we may want to reload mgm port after each bus reset */
847 1.1 kiyohara /* XXX there might be multiple management agents */
848 1.1 kiyohara crom_init_context(&cc, target->fwdev->csrrom);
849 1.1 kiyohara reg = crom_search_key(&cc, CROM_MGM);
850 1.1 kiyohara if (reg == NULL || reg->val == 0) {
851 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, "NULL management address\n");
852 1.1 kiyohara target->fwdev = NULL;
853 1.1 kiyohara return NULL;
854 1.1 kiyohara }
855 1.33 cegger
856 1.1 kiyohara target->mgm_hi = 0xffff;
857 1.1 kiyohara target->mgm_lo = 0xf0000000 | (reg->val << 2);
858 1.1 kiyohara
859 1.1 kiyohara return target;
860 1.1 kiyohara }
861 1.1 kiyohara
862 1.1 kiyohara static void
863 1.1 kiyohara sbp_probe_lun(struct sbp_dev *sdev)
864 1.1 kiyohara {
865 1.1 kiyohara struct fw_device *fwdev;
866 1.1 kiyohara struct crom_context c, *cc = &c;
867 1.1 kiyohara struct csrreg *reg;
868 1.1 kiyohara
869 1.25 cegger memset(sdev->vendor, 0, sizeof(sdev->vendor));
870 1.25 cegger memset(sdev->product, 0, sizeof(sdev->product));
871 1.1 kiyohara
872 1.1 kiyohara fwdev = sdev->target->fwdev;
873 1.1 kiyohara crom_init_context(cc, fwdev->csrrom);
874 1.1 kiyohara /* get vendor string */
875 1.1 kiyohara crom_search_key(cc, CSRKEY_VENDOR);
876 1.1 kiyohara crom_next(cc);
877 1.1 kiyohara crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor));
878 1.1 kiyohara /* skip to the unit directory for SBP-2 */
879 1.1 kiyohara while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) {
880 1.1 kiyohara if (reg->val == CSRVAL_T10SBP2)
881 1.1 kiyohara break;
882 1.1 kiyohara crom_next(cc);
883 1.1 kiyohara }
884 1.1 kiyohara /* get firmware revision */
885 1.1 kiyohara reg = crom_search_key(cc, CSRKEY_FIRM_VER);
886 1.1 kiyohara if (reg != NULL)
887 1.29 kiyohara snprintf(sdev->revision, sizeof(sdev->revision), "%06x",
888 1.29 kiyohara reg->val);
889 1.1 kiyohara /* get product string */
890 1.1 kiyohara crom_search_key(cc, CSRKEY_MODEL);
891 1.1 kiyohara crom_next(cc);
892 1.1 kiyohara crom_parse_text(cc, sdev->product, sizeof(sdev->product));
893 1.1 kiyohara }
894 1.1 kiyohara
895 1.1 kiyohara static void
896 1.1 kiyohara sbp_login_callout(void *arg)
897 1.1 kiyohara {
898 1.1 kiyohara struct sbp_dev *sdev = (struct sbp_dev *)arg;
899 1.29 kiyohara
900 1.1 kiyohara sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
901 1.1 kiyohara }
902 1.1 kiyohara
903 1.1 kiyohara static void
904 1.1 kiyohara sbp_login(struct sbp_dev *sdev)
905 1.1 kiyohara {
906 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp;
907 1.1 kiyohara struct timeval delta;
908 1.1 kiyohara struct timeval t;
909 1.1 kiyohara int ticks = 0;
910 1.1 kiyohara
911 1.1 kiyohara microtime(&delta);
912 1.29 kiyohara timersub(&delta, &sc->sc_last_busreset, &delta);
913 1.1 kiyohara t.tv_sec = login_delay / 1000;
914 1.1 kiyohara t.tv_usec = (login_delay % 1000) * 1000;
915 1.29 kiyohara timersub(&t, &delta, &t);
916 1.1 kiyohara if (t.tv_sec >= 0 && t.tv_usec > 0)
917 1.1 kiyohara ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000;
918 1.1 kiyohara SBP_DEBUG(0)
919 1.24 christos printf("%s: sec = %lld usec = %ld ticks = %d\n", __func__,
920 1.24 christos (long long)t.tv_sec, (long)t.tv_usec, ticks);
921 1.1 kiyohara END_DEBUG
922 1.29 kiyohara callout_schedule(&sdev->login_callout, ticks);
923 1.1 kiyohara }
924 1.1 kiyohara
925 1.1 kiyohara static void
926 1.1 kiyohara sbp_probe_target(void *arg)
927 1.1 kiyohara {
928 1.1 kiyohara struct sbp_target *target = (struct sbp_target *)arg;
929 1.1 kiyohara struct sbp_dev *sdev;
930 1.1 kiyohara int i;
931 1.1 kiyohara
932 1.1 kiyohara SBP_DEBUG(1)
933 1.30 kiyohara printf("%s %d\n", __func__, target->target_id);
934 1.1 kiyohara END_DEBUG
935 1.1 kiyohara
936 1.1 kiyohara sbp_alloc_lun(target);
937 1.1 kiyohara
938 1.1 kiyohara /* XXX untimeout mgm_ocb and dequeue */
939 1.29 kiyohara for (i = 0; i < target->num_lun; i++) {
940 1.1 kiyohara sdev = target->luns[i];
941 1.29 kiyohara if (sdev == NULL || sdev->status == SBP_DEV_DEAD)
942 1.1 kiyohara continue;
943 1.1 kiyohara
944 1.29 kiyohara if (sdev->periph != NULL) {
945 1.29 kiyohara scsipi_periph_freeze(sdev->periph, 1);
946 1.29 kiyohara sdev->freeze++;
947 1.29 kiyohara }
948 1.29 kiyohara sbp_probe_lun(sdev);
949 1.29 kiyohara sbp_show_sdev_info(sdev);
950 1.29 kiyohara
951 1.29 kiyohara sbp_abort_all_ocbs(sdev, XS_RESET);
952 1.29 kiyohara switch (sdev->status) {
953 1.29 kiyohara case SBP_DEV_RESET:
954 1.29 kiyohara /* new or revived target */
955 1.29 kiyohara if (auto_login)
956 1.29 kiyohara sbp_login(sdev);
957 1.29 kiyohara break;
958 1.29 kiyohara case SBP_DEV_TOATTACH:
959 1.29 kiyohara case SBP_DEV_PROBE:
960 1.29 kiyohara case SBP_DEV_ATTACHED:
961 1.29 kiyohara case SBP_DEV_RETRY:
962 1.29 kiyohara default:
963 1.29 kiyohara sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
964 1.29 kiyohara break;
965 1.1 kiyohara }
966 1.1 kiyohara }
967 1.1 kiyohara }
968 1.1 kiyohara
969 1.1 kiyohara static void
970 1.1 kiyohara sbp_post_busreset(void *arg)
971 1.1 kiyohara {
972 1.29 kiyohara struct sbp_softc *sc = (struct sbp_softc *)arg;
973 1.29 kiyohara struct sbp_target *target = &sc->sc_target;
974 1.1 kiyohara struct fw_device *fwdev = target->fwdev;
975 1.1 kiyohara int alive;
976 1.1 kiyohara
977 1.1 kiyohara alive = SBP_FWDEV_ALIVE(fwdev);
978 1.1 kiyohara SBP_DEBUG(0)
979 1.1 kiyohara printf("sbp_post_busreset\n");
980 1.1 kiyohara if (!alive)
981 1.1 kiyohara printf("not alive\n");
982 1.1 kiyohara END_DEBUG
983 1.29 kiyohara microtime(&sc->sc_last_busreset);
984 1.10 kiyohara
985 1.1 kiyohara if (!alive)
986 1.1 kiyohara return;
987 1.1 kiyohara
988 1.29 kiyohara scsipi_channel_freeze(&sc->sc_channel, 1);
989 1.1 kiyohara }
990 1.1 kiyohara
991 1.1 kiyohara static void
992 1.1 kiyohara sbp_post_explore(void *arg)
993 1.1 kiyohara {
994 1.29 kiyohara struct sbp_softc *sc = (struct sbp_softc *)arg;
995 1.29 kiyohara struct sbp_target *target = &sc->sc_target;
996 1.1 kiyohara struct fw_device *fwdev = target->fwdev;
997 1.1 kiyohara int alive;
998 1.1 kiyohara
999 1.1 kiyohara alive = SBP_FWDEV_ALIVE(fwdev);
1000 1.1 kiyohara SBP_DEBUG(0)
1001 1.1 kiyohara printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
1002 1.1 kiyohara if (!alive)
1003 1.1 kiyohara printf("not alive\n");
1004 1.1 kiyohara END_DEBUG
1005 1.1 kiyohara if (!alive)
1006 1.1 kiyohara return;
1007 1.1 kiyohara
1008 1.29 kiyohara if (!firewire_phydma_enable)
1009 1.29 kiyohara return;
1010 1.29 kiyohara
1011 1.1 kiyohara if (sbp_cold > 0)
1012 1.29 kiyohara sbp_cold--;
1013 1.1 kiyohara
1014 1.1 kiyohara SBP_DEBUG(0)
1015 1.1 kiyohara printf("sbp_post_explore: EUI:%08x%08x ", fwdev->eui.hi, fwdev->eui.lo);
1016 1.1 kiyohara END_DEBUG
1017 1.1 kiyohara sbp_probe_target((void *)target);
1018 1.1 kiyohara if (target->num_lun == 0)
1019 1.1 kiyohara sbp_free_target(target);
1020 1.1 kiyohara
1021 1.29 kiyohara scsipi_channel_thaw(&sc->sc_channel, 1);
1022 1.1 kiyohara }
1023 1.1 kiyohara
1024 1.1 kiyohara #if NEED_RESPONSE
1025 1.1 kiyohara static void
1026 1.29 kiyohara sbp_loginres_callback(struct fw_xfer *xfer)
1027 1.29 kiyohara {
1028 1.29 kiyohara struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc;
1029 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp;
1030 1.29 kiyohara
1031 1.1 kiyohara SBP_DEBUG(1)
1032 1.1 kiyohara printf("sbp_loginres_callback\n");
1033 1.1 kiyohara END_DEBUG
1034 1.1 kiyohara /* recycle */
1035 1.29 kiyohara mutex_enter(&sc->sc_fwb.fwb_mtx);
1036 1.29 kiyohara STAILQ_INSERT_TAIL(&sc->sc_fwb.xferlist, xfer, link);
1037 1.29 kiyohara mutex_exit(&sc->sc_fwb.fwb_mtx);
1038 1.1 kiyohara return;
1039 1.1 kiyohara }
1040 1.1 kiyohara #endif
1041 1.1 kiyohara
1042 1.4 perry static inline void
1043 1.1 kiyohara sbp_xfer_free(struct fw_xfer *xfer)
1044 1.1 kiyohara {
1045 1.29 kiyohara struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc;
1046 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp;
1047 1.1 kiyohara
1048 1.1 kiyohara fw_xfer_unload(xfer);
1049 1.29 kiyohara mutex_enter(&sc->sc_mtx);
1050 1.1 kiyohara STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
1051 1.29 kiyohara mutex_exit(&sc->sc_mtx);
1052 1.1 kiyohara }
1053 1.1 kiyohara
1054 1.1 kiyohara static void
1055 1.1 kiyohara sbp_reset_start_callback(struct fw_xfer *xfer)
1056 1.1 kiyohara {
1057 1.1 kiyohara struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
1058 1.1 kiyohara struct sbp_target *target = sdev->target;
1059 1.1 kiyohara int i;
1060 1.1 kiyohara
1061 1.29 kiyohara if (xfer->resp != 0)
1062 1.29 kiyohara aprint_error("%s: sbp_reset_start failed: resp=%d\n",
1063 1.29 kiyohara sdev->bustgtlun, xfer->resp);
1064 1.1 kiyohara
1065 1.1 kiyohara for (i = 0; i < target->num_lun; i++) {
1066 1.1 kiyohara tsdev = target->luns[i];
1067 1.1 kiyohara if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN)
1068 1.1 kiyohara sbp_login(tsdev);
1069 1.1 kiyohara }
1070 1.1 kiyohara }
1071 1.1 kiyohara
1072 1.1 kiyohara static void
1073 1.1 kiyohara sbp_reset_start(struct sbp_dev *sdev)
1074 1.1 kiyohara {
1075 1.1 kiyohara struct fw_xfer *xfer;
1076 1.1 kiyohara struct fw_pkt *fp;
1077 1.1 kiyohara
1078 1.1 kiyohara SBP_DEBUG(0)
1079 1.29 kiyohara printf("%s: sbp_reset_start: %s\n",
1080 1.29 kiyohara device_xname(sdev->target->sbp->sc_fd.dev), sdev->bustgtlun);
1081 1.1 kiyohara END_DEBUG
1082 1.1 kiyohara
1083 1.1 kiyohara xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1084 1.29 kiyohara if (xfer == NULL)
1085 1.29 kiyohara return;
1086 1.1 kiyohara xfer->hand = sbp_reset_start_callback;
1087 1.1 kiyohara fp = &xfer->send.hdr;
1088 1.1 kiyohara fp->mode.wreqq.dest_hi = 0xffff;
1089 1.1 kiyohara fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
1090 1.1 kiyohara fp->mode.wreqq.data = htonl(0xf);
1091 1.29 kiyohara if (fw_asyreq(xfer->fc, -1, xfer) != 0)
1092 1.29 kiyohara sbp_xfer_free(xfer);
1093 1.1 kiyohara }
1094 1.1 kiyohara
1095 1.1 kiyohara static void
1096 1.1 kiyohara sbp_mgm_callback(struct fw_xfer *xfer)
1097 1.1 kiyohara {
1098 1.1 kiyohara struct sbp_dev *sdev;
1099 1.1 kiyohara
1100 1.1 kiyohara sdev = (struct sbp_dev *)xfer->sc;
1101 1.1 kiyohara
1102 1.1 kiyohara SBP_DEBUG(1)
1103 1.29 kiyohara printf("%s: sbp_mgm_callback: %s\n",
1104 1.29 kiyohara device_xname(sdev->target->sbp->sc_fd.dev), sdev->bustgtlun);
1105 1.1 kiyohara END_DEBUG
1106 1.1 kiyohara sbp_xfer_free(xfer);
1107 1.1 kiyohara return;
1108 1.1 kiyohara }
1109 1.1 kiyohara
1110 1.29 kiyohara static void
1111 1.29 kiyohara sbp_scsipi_scan_target(void *arg)
1112 1.1 kiyohara {
1113 1.29 kiyohara struct sbp_target *target = (struct sbp_target *)arg;
1114 1.29 kiyohara struct sbp_softc *sc = target->sbp;
1115 1.29 kiyohara struct sbp_dev *sdev;
1116 1.29 kiyohara struct scsipi_channel *chan = &sc->sc_channel;
1117 1.29 kiyohara struct scsibus_softc *sc_bus = device_private(sc->sc_bus);
1118 1.29 kiyohara int lun, yet;
1119 1.29 kiyohara
1120 1.29 kiyohara do {
1121 1.29 kiyohara mutex_enter(&sc->sc_mtx);
1122 1.29 kiyohara cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
1123 1.29 kiyohara mutex_exit(&sc->sc_mtx);
1124 1.29 kiyohara yet = 0;
1125 1.29 kiyohara
1126 1.29 kiyohara for (lun = 0; lun < target->num_lun; lun++) {
1127 1.29 kiyohara sdev = target->luns[lun];
1128 1.29 kiyohara if (sdev == NULL)
1129 1.29 kiyohara continue;
1130 1.29 kiyohara if (sdev->status != SBP_DEV_PROBE) {
1131 1.29 kiyohara yet++;
1132 1.29 kiyohara continue;
1133 1.29 kiyohara }
1134 1.29 kiyohara
1135 1.29 kiyohara if (sdev->periph == NULL) {
1136 1.29 kiyohara if (chan->chan_nluns < target->num_lun)
1137 1.29 kiyohara chan->chan_nluns = target->num_lun;
1138 1.29 kiyohara
1139 1.29 kiyohara scsi_probe_bus(sc_bus, target->target_id,
1140 1.29 kiyohara sdev->lun_id);
1141 1.29 kiyohara sdev->periph = scsipi_lookup_periph(chan,
1142 1.29 kiyohara target->target_id, lun);
1143 1.29 kiyohara }
1144 1.29 kiyohara sdev->status = SBP_DEV_ATTACHED;
1145 1.29 kiyohara }
1146 1.29 kiyohara } while (yet > 0);
1147 1.29 kiyohara
1148 1.29 kiyohara sc->sc_lwp = NULL;
1149 1.29 kiyohara kthread_exit(0);
1150 1.1 kiyohara
1151 1.29 kiyohara /* NOTREACHED */
1152 1.1 kiyohara }
1153 1.1 kiyohara
1154 1.29 kiyohara static inline void
1155 1.29 kiyohara sbp_scan_dev(struct sbp_dev *sdev)
1156 1.1 kiyohara {
1157 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp;
1158 1.29 kiyohara
1159 1.29 kiyohara sdev->status = SBP_DEV_PROBE;
1160 1.29 kiyohara mutex_enter(&sc->sc_mtx);
1161 1.29 kiyohara cv_signal(&sdev->target->sbp->sc_cv);
1162 1.29 kiyohara mutex_exit(&sc->sc_mtx);
1163 1.29 kiyohara }
1164 1.1 kiyohara
1165 1.1 kiyohara
1166 1.1 kiyohara static void
1167 1.1 kiyohara sbp_do_attach(struct fw_xfer *xfer)
1168 1.1 kiyohara {
1169 1.1 kiyohara struct sbp_dev *sdev;
1170 1.1 kiyohara struct sbp_target *target;
1171 1.29 kiyohara struct sbp_softc *sc;
1172 1.1 kiyohara
1173 1.1 kiyohara sdev = (struct sbp_dev *)xfer->sc;
1174 1.1 kiyohara target = sdev->target;
1175 1.29 kiyohara sc = target->sbp;
1176 1.1 kiyohara
1177 1.1 kiyohara SBP_DEBUG(0)
1178 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sc->sc_fd.dev), __func__,
1179 1.29 kiyohara sdev->bustgtlun);
1180 1.1 kiyohara END_DEBUG
1181 1.1 kiyohara sbp_xfer_free(xfer);
1182 1.1 kiyohara
1183 1.1 kiyohara sbp_scan_dev(sdev);
1184 1.1 kiyohara return;
1185 1.1 kiyohara }
1186 1.1 kiyohara
1187 1.1 kiyohara static void
1188 1.1 kiyohara sbp_agent_reset_callback(struct fw_xfer *xfer)
1189 1.1 kiyohara {
1190 1.29 kiyohara struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc;
1191 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp;
1192 1.1 kiyohara
1193 1.1 kiyohara SBP_DEBUG(1)
1194 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sc->sc_fd.dev), __func__,
1195 1.29 kiyohara sdev->bustgtlun);
1196 1.1 kiyohara END_DEBUG
1197 1.29 kiyohara if (xfer->resp != 0)
1198 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, "%s:%s: resp=%d\n", __func__,
1199 1.29 kiyohara sdev->bustgtlun, xfer->resp);
1200 1.1 kiyohara
1201 1.1 kiyohara sbp_xfer_free(xfer);
1202 1.29 kiyohara if (sdev->periph != NULL) {
1203 1.29 kiyohara scsipi_periph_thaw(sdev->periph, sdev->freeze);
1204 1.29 kiyohara scsipi_channel_thaw(&sc->sc_channel, 0);
1205 1.1 kiyohara sdev->freeze = 0;
1206 1.1 kiyohara }
1207 1.1 kiyohara }
1208 1.1 kiyohara
1209 1.1 kiyohara static void
1210 1.1 kiyohara sbp_agent_reset(struct sbp_dev *sdev)
1211 1.1 kiyohara {
1212 1.1 kiyohara struct fw_xfer *xfer;
1213 1.1 kiyohara struct fw_pkt *fp;
1214 1.1 kiyohara
1215 1.1 kiyohara SBP_DEBUG(0)
1216 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sdev->target->sbp->sc_fd.dev),
1217 1.29 kiyohara __func__, sdev->bustgtlun);
1218 1.1 kiyohara END_DEBUG
1219 1.1 kiyohara xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1220 1.1 kiyohara if (xfer == NULL)
1221 1.1 kiyohara return;
1222 1.1 kiyohara if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE)
1223 1.1 kiyohara xfer->hand = sbp_agent_reset_callback;
1224 1.1 kiyohara else
1225 1.1 kiyohara xfer->hand = sbp_do_attach;
1226 1.1 kiyohara fp = &xfer->send.hdr;
1227 1.1 kiyohara fp->mode.wreqq.data = htonl(0xf);
1228 1.29 kiyohara if (fw_asyreq(xfer->fc, -1, xfer) != 0)
1229 1.29 kiyohara sbp_xfer_free(xfer);
1230 1.29 kiyohara sbp_abort_all_ocbs(sdev, XS_RESET);
1231 1.1 kiyohara }
1232 1.1 kiyohara
1233 1.1 kiyohara static void
1234 1.1 kiyohara sbp_busy_timeout_callback(struct fw_xfer *xfer)
1235 1.1 kiyohara {
1236 1.29 kiyohara struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc;
1237 1.1 kiyohara
1238 1.1 kiyohara SBP_DEBUG(1)
1239 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sdev->target->sbp->sc_fd.dev),
1240 1.29 kiyohara __func__, sdev->bustgtlun);
1241 1.1 kiyohara END_DEBUG
1242 1.1 kiyohara sbp_xfer_free(xfer);
1243 1.1 kiyohara sbp_agent_reset(sdev);
1244 1.1 kiyohara }
1245 1.1 kiyohara
1246 1.1 kiyohara static void
1247 1.1 kiyohara sbp_busy_timeout(struct sbp_dev *sdev)
1248 1.1 kiyohara {
1249 1.1 kiyohara struct fw_pkt *fp;
1250 1.1 kiyohara struct fw_xfer *xfer;
1251 1.29 kiyohara
1252 1.1 kiyohara SBP_DEBUG(0)
1253 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sdev->target->sbp->sc_fd.dev),
1254 1.29 kiyohara __func__, sdev->bustgtlun);
1255 1.1 kiyohara END_DEBUG
1256 1.1 kiyohara xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1257 1.29 kiyohara if (xfer == NULL)
1258 1.29 kiyohara return;
1259 1.1 kiyohara xfer->hand = sbp_busy_timeout_callback;
1260 1.1 kiyohara fp = &xfer->send.hdr;
1261 1.1 kiyohara fp->mode.wreqq.dest_hi = 0xffff;
1262 1.1 kiyohara fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1263 1.1 kiyohara fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1264 1.29 kiyohara if (fw_asyreq(xfer->fc, -1, xfer) != 0)
1265 1.29 kiyohara sbp_xfer_free(xfer);
1266 1.1 kiyohara }
1267 1.1 kiyohara
1268 1.1 kiyohara static void
1269 1.1 kiyohara sbp_orb_pointer_callback(struct fw_xfer *xfer)
1270 1.1 kiyohara {
1271 1.29 kiyohara struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc;
1272 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp;
1273 1.1 kiyohara
1274 1.1 kiyohara SBP_DEBUG(1)
1275 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sc->sc_fd.dev), __func__,
1276 1.29 kiyohara sdev->bustgtlun);
1277 1.1 kiyohara END_DEBUG
1278 1.29 kiyohara if (xfer->resp != 0)
1279 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, "%s:%s: xfer->resp = %d\n",
1280 1.29 kiyohara __func__, sdev->bustgtlun, xfer->resp);
1281 1.1 kiyohara sbp_xfer_free(xfer);
1282 1.1 kiyohara sdev->flags &= ~ORB_POINTER_ACTIVE;
1283 1.1 kiyohara
1284 1.1 kiyohara if ((sdev->flags & ORB_POINTER_NEED) != 0) {
1285 1.1 kiyohara struct sbp_ocb *ocb;
1286 1.1 kiyohara
1287 1.1 kiyohara sdev->flags &= ~ORB_POINTER_NEED;
1288 1.1 kiyohara ocb = STAILQ_FIRST(&sdev->ocbs);
1289 1.1 kiyohara if (ocb != NULL)
1290 1.1 kiyohara sbp_orb_pointer(sdev, ocb);
1291 1.1 kiyohara }
1292 1.1 kiyohara return;
1293 1.1 kiyohara }
1294 1.1 kiyohara
1295 1.1 kiyohara static void
1296 1.1 kiyohara sbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1297 1.1 kiyohara {
1298 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp;
1299 1.1 kiyohara struct fw_xfer *xfer;
1300 1.1 kiyohara struct fw_pkt *fp;
1301 1.29 kiyohara
1302 1.1 kiyohara SBP_DEBUG(1)
1303 1.29 kiyohara printf("%s:%s:%s: 0x%08x\n", device_xname(sc->sc_fd.dev), __func__,
1304 1.29 kiyohara sdev->bustgtlun, (uint32_t)ocb->bus_addr);
1305 1.1 kiyohara END_DEBUG
1306 1.1 kiyohara
1307 1.1 kiyohara if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) {
1308 1.1 kiyohara SBP_DEBUG(0)
1309 1.1 kiyohara printf("%s: orb pointer active\n", __func__);
1310 1.1 kiyohara END_DEBUG
1311 1.1 kiyohara sdev->flags |= ORB_POINTER_NEED;
1312 1.1 kiyohara return;
1313 1.1 kiyohara }
1314 1.1 kiyohara
1315 1.1 kiyohara sdev->flags |= ORB_POINTER_ACTIVE;
1316 1.1 kiyohara xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1317 1.1 kiyohara if (xfer == NULL)
1318 1.1 kiyohara return;
1319 1.1 kiyohara xfer->hand = sbp_orb_pointer_callback;
1320 1.1 kiyohara
1321 1.1 kiyohara fp = &xfer->send.hdr;
1322 1.1 kiyohara fp->mode.wreqb.len = 8;
1323 1.1 kiyohara fp->mode.wreqb.extcode = 0;
1324 1.29 kiyohara xfer->send.payload[0] =
1325 1.29 kiyohara htonl(((sc->sc_fd.fc->nodeid | FWLOCALBUS) << 16));
1326 1.1 kiyohara xfer->send.payload[1] = htonl((uint32_t)ocb->bus_addr);
1327 1.1 kiyohara
1328 1.29 kiyohara if (fw_asyreq(xfer->fc, -1, xfer) != 0) {
1329 1.29 kiyohara sbp_xfer_free(xfer);
1330 1.29 kiyohara ocb->xs->error = XS_DRIVER_STUFFUP;
1331 1.29 kiyohara scsipi_done(ocb->xs);
1332 1.1 kiyohara }
1333 1.1 kiyohara }
1334 1.1 kiyohara
1335 1.1 kiyohara static void
1336 1.1 kiyohara sbp_doorbell_callback(struct fw_xfer *xfer)
1337 1.1 kiyohara {
1338 1.29 kiyohara struct sbp_dev *sdev = (struct sbp_dev *)xfer->sc;
1339 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp;
1340 1.1 kiyohara
1341 1.1 kiyohara SBP_DEBUG(1)
1342 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sc->sc_fd.dev), __func__,
1343 1.29 kiyohara sdev->bustgtlun);
1344 1.1 kiyohara END_DEBUG
1345 1.1 kiyohara if (xfer->resp != 0) {
1346 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, "%s: xfer->resp = %d\n",
1347 1.29 kiyohara __func__, xfer->resp);
1348 1.1 kiyohara }
1349 1.1 kiyohara sbp_xfer_free(xfer);
1350 1.1 kiyohara sdev->flags &= ~ORB_DOORBELL_ACTIVE;
1351 1.1 kiyohara if ((sdev->flags & ORB_DOORBELL_NEED) != 0) {
1352 1.1 kiyohara sdev->flags &= ~ORB_DOORBELL_NEED;
1353 1.1 kiyohara sbp_doorbell(sdev);
1354 1.1 kiyohara }
1355 1.1 kiyohara return;
1356 1.1 kiyohara }
1357 1.1 kiyohara
1358 1.1 kiyohara static void
1359 1.1 kiyohara sbp_doorbell(struct sbp_dev *sdev)
1360 1.1 kiyohara {
1361 1.1 kiyohara struct fw_xfer *xfer;
1362 1.1 kiyohara struct fw_pkt *fp;
1363 1.29 kiyohara
1364 1.1 kiyohara SBP_DEBUG(1)
1365 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sdev->target->sbp->sc_fd.dev),
1366 1.29 kiyohara __func__, sdev->bustgtlun);
1367 1.1 kiyohara END_DEBUG
1368 1.1 kiyohara
1369 1.1 kiyohara if ((sdev->flags & ORB_DOORBELL_ACTIVE) != 0) {
1370 1.1 kiyohara sdev->flags |= ORB_DOORBELL_NEED;
1371 1.1 kiyohara return;
1372 1.1 kiyohara }
1373 1.1 kiyohara sdev->flags |= ORB_DOORBELL_ACTIVE;
1374 1.1 kiyohara xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1375 1.1 kiyohara if (xfer == NULL)
1376 1.1 kiyohara return;
1377 1.1 kiyohara xfer->hand = sbp_doorbell_callback;
1378 1.1 kiyohara fp = &xfer->send.hdr;
1379 1.1 kiyohara fp->mode.wreqq.data = htonl(0xf);
1380 1.29 kiyohara if (fw_asyreq(xfer->fc, -1, xfer) != 0)
1381 1.29 kiyohara sbp_xfer_free(xfer);
1382 1.1 kiyohara }
1383 1.1 kiyohara
1384 1.1 kiyohara static struct fw_xfer *
1385 1.1 kiyohara sbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1386 1.1 kiyohara {
1387 1.29 kiyohara struct sbp_softc *sc;
1388 1.1 kiyohara struct fw_xfer *xfer;
1389 1.1 kiyohara struct fw_pkt *fp;
1390 1.1 kiyohara struct sbp_target *target;
1391 1.29 kiyohara int new = 0;
1392 1.1 kiyohara
1393 1.1 kiyohara target = sdev->target;
1394 1.29 kiyohara sc = target->sbp;
1395 1.29 kiyohara mutex_enter(&sc->sc_mtx);
1396 1.1 kiyohara xfer = STAILQ_FIRST(&target->xferlist);
1397 1.1 kiyohara if (xfer == NULL) {
1398 1.1 kiyohara if (target->n_xfer > 5 /* XXX */) {
1399 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev,
1400 1.29 kiyohara "no more xfer for this target\n");
1401 1.29 kiyohara mutex_exit(&sc->sc_mtx);
1402 1.29 kiyohara return NULL;
1403 1.1 kiyohara }
1404 1.1 kiyohara xfer = fw_xfer_alloc_buf(M_SBP, 8, 0);
1405 1.29 kiyohara if (xfer == NULL) {
1406 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev,
1407 1.29 kiyohara "fw_xfer_alloc_buf failed\n");
1408 1.29 kiyohara mutex_exit(&sc->sc_mtx);
1409 1.1 kiyohara return NULL;
1410 1.1 kiyohara }
1411 1.29 kiyohara target->n_xfer++;
1412 1.29 kiyohara SBP_DEBUG(0)
1413 1.1 kiyohara printf("sbp: alloc %d xfer\n", target->n_xfer);
1414 1.29 kiyohara END_DEBUG
1415 1.1 kiyohara new = 1;
1416 1.29 kiyohara } else
1417 1.1 kiyohara STAILQ_REMOVE_HEAD(&target->xferlist, link);
1418 1.29 kiyohara mutex_exit(&sc->sc_mtx);
1419 1.1 kiyohara
1420 1.1 kiyohara microtime(&xfer->tv);
1421 1.1 kiyohara
1422 1.1 kiyohara if (new) {
1423 1.1 kiyohara xfer->recv.pay_len = 0;
1424 1.37 riastrad xfer->send.spd = uimin(target->fwdev->speed, max_speed);
1425 1.29 kiyohara xfer->fc = target->sbp->sc_fd.fc;
1426 1.1 kiyohara }
1427 1.1 kiyohara
1428 1.1 kiyohara if (tcode == FWTCODE_WREQB)
1429 1.1 kiyohara xfer->send.pay_len = 8;
1430 1.1 kiyohara else
1431 1.1 kiyohara xfer->send.pay_len = 0;
1432 1.1 kiyohara
1433 1.15 christos xfer->sc = (void *)sdev;
1434 1.1 kiyohara fp = &xfer->send.hdr;
1435 1.1 kiyohara fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
1436 1.1 kiyohara fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
1437 1.1 kiyohara fp->mode.wreqq.tlrt = 0;
1438 1.1 kiyohara fp->mode.wreqq.tcode = tcode;
1439 1.1 kiyohara fp->mode.wreqq.pri = 0;
1440 1.29 kiyohara fp->mode.wreqq.dst = FWLOCALBUS | target->fwdev->dst;
1441 1.1 kiyohara
1442 1.1 kiyohara return xfer;
1443 1.1 kiyohara }
1444 1.1 kiyohara
1445 1.1 kiyohara static void
1446 1.1 kiyohara sbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1447 1.1 kiyohara {
1448 1.1 kiyohara struct fw_xfer *xfer;
1449 1.1 kiyohara struct fw_pkt *fp;
1450 1.1 kiyohara struct sbp_ocb *ocb;
1451 1.1 kiyohara struct sbp_target *target;
1452 1.29 kiyohara int nid, dv_unit;
1453 1.1 kiyohara
1454 1.1 kiyohara target = sdev->target;
1455 1.29 kiyohara nid = target->sbp->sc_fd.fc->nodeid | FWLOCALBUS;
1456 1.29 kiyohara dv_unit = device_unit(target->sbp->sc_fd.dev);
1457 1.1 kiyohara
1458 1.29 kiyohara mutex_enter(&target->sbp->sc_mtx);
1459 1.1 kiyohara if (func == ORB_FUN_RUNQUEUE) {
1460 1.1 kiyohara ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1461 1.1 kiyohara if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1462 1.29 kiyohara mutex_exit(&target->sbp->sc_mtx);
1463 1.1 kiyohara return;
1464 1.1 kiyohara }
1465 1.1 kiyohara STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1466 1.29 kiyohara mutex_exit(&target->sbp->sc_mtx);
1467 1.1 kiyohara goto start;
1468 1.1 kiyohara }
1469 1.1 kiyohara if ((ocb = sbp_get_ocb(sdev)) == NULL) {
1470 1.29 kiyohara mutex_exit(&target->sbp->sc_mtx);
1471 1.1 kiyohara /* XXX */
1472 1.1 kiyohara return;
1473 1.1 kiyohara }
1474 1.29 kiyohara mutex_exit(&target->sbp->sc_mtx);
1475 1.1 kiyohara ocb->flags = OCB_ACT_MGM;
1476 1.1 kiyohara ocb->sdev = sdev;
1477 1.1 kiyohara
1478 1.30 kiyohara memset(ocb->orb, 0, sizeof(ocb->orb));
1479 1.1 kiyohara ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1480 1.1 kiyohara ocb->orb[7] = htonl(SBP_DEV2ADDR(dv_unit, sdev->lun_id));
1481 1.1 kiyohara
1482 1.1 kiyohara SBP_DEBUG(0)
1483 1.29 kiyohara printf("%s:%s:%s: %s\n", device_xname(sdev->target->sbp->sc_fd.dev),
1484 1.29 kiyohara __func__, sdev->bustgtlun, orb_fun_name[(func>>16)&0xf]);
1485 1.1 kiyohara END_DEBUG
1486 1.1 kiyohara switch (func) {
1487 1.1 kiyohara case ORB_FUN_LGI:
1488 1.29 kiyohara {
1489 1.29 kiyohara const off_t sbp_login_off =
1490 1.29 kiyohara sizeof(struct sbp_ocb) * SBP_QUEUE_LEN;
1491 1.29 kiyohara
1492 1.1 kiyohara ocb->orb[0] = ocb->orb[1] = 0; /* password */
1493 1.1 kiyohara ocb->orb[2] = htonl(nid << 16);
1494 1.29 kiyohara ocb->orb[3] = htonl(sdev->dma.bus_addr + sbp_login_off);
1495 1.1 kiyohara ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id);
1496 1.1 kiyohara if (ex_login)
1497 1.1 kiyohara ocb->orb[4] |= htonl(ORB_EXV);
1498 1.1 kiyohara ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
1499 1.29 kiyohara bus_dmamap_sync(sdev->dma.dma_tag, sdev->dma.dma_map,
1500 1.29 kiyohara sbp_login_off, SBP_LOGIN_SIZE, BUS_DMASYNC_PREREAD);
1501 1.1 kiyohara break;
1502 1.29 kiyohara }
1503 1.29 kiyohara
1504 1.1 kiyohara case ORB_FUN_ATA:
1505 1.1 kiyohara ocb->orb[0] = htonl((0 << 16) | 0);
1506 1.1 kiyohara ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
1507 1.1 kiyohara /* fall through */
1508 1.1 kiyohara case ORB_FUN_RCN:
1509 1.1 kiyohara case ORB_FUN_LGO:
1510 1.1 kiyohara case ORB_FUN_LUR:
1511 1.1 kiyohara case ORB_FUN_RST:
1512 1.1 kiyohara case ORB_FUN_ATS:
1513 1.1 kiyohara ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
1514 1.1 kiyohara break;
1515 1.1 kiyohara }
1516 1.1 kiyohara
1517 1.1 kiyohara if (target->mgm_ocb_cur != NULL) {
1518 1.1 kiyohara /* there is a standing ORB */
1519 1.29 kiyohara mutex_enter(&target->sbp->sc_mtx);
1520 1.1 kiyohara STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1521 1.29 kiyohara mutex_exit(&target->sbp->sc_mtx);
1522 1.1 kiyohara return;
1523 1.1 kiyohara }
1524 1.1 kiyohara start:
1525 1.1 kiyohara target->mgm_ocb_cur = ocb;
1526 1.1 kiyohara
1527 1.29 kiyohara callout_reset(&target->mgm_ocb_timeout, 5 * hz, sbp_mgm_timeout, ocb);
1528 1.1 kiyohara xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1529 1.29 kiyohara if (xfer == NULL)
1530 1.1 kiyohara return;
1531 1.1 kiyohara xfer->hand = sbp_mgm_callback;
1532 1.1 kiyohara
1533 1.1 kiyohara fp = &xfer->send.hdr;
1534 1.1 kiyohara fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
1535 1.1 kiyohara fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
1536 1.1 kiyohara fp->mode.wreqb.len = 8;
1537 1.1 kiyohara fp->mode.wreqb.extcode = 0;
1538 1.1 kiyohara xfer->send.payload[0] = htonl(nid << 16);
1539 1.1 kiyohara xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff);
1540 1.1 kiyohara
1541 1.1 kiyohara /* cache writeback & invalidate(required ORB_FUN_LGI func) */
1542 1.1 kiyohara /* when abort_ocb, should sync POST ope ? */
1543 1.29 kiyohara SBP_ORB_DMA_SYNC(sdev->dma, ocb->index, BUS_DMASYNC_PREWRITE);
1544 1.29 kiyohara if (fw_asyreq(xfer->fc, -1, xfer) != 0)
1545 1.29 kiyohara sbp_xfer_free(xfer);
1546 1.1 kiyohara }
1547 1.1 kiyohara
1548 1.1 kiyohara static void
1549 1.1 kiyohara sbp_print_scsi_cmd(struct sbp_ocb *ocb)
1550 1.1 kiyohara {
1551 1.29 kiyohara struct scsipi_xfer *xs = ocb->xs;
1552 1.1 kiyohara
1553 1.29 kiyohara printf("%s:%d:%d:"
1554 1.29 kiyohara " cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x,"
1555 1.29 kiyohara " flags: 0x%02x, %db cmd/%db data\n",
1556 1.29 kiyohara device_xname(ocb->sdev->target->sbp->sc_fd.dev),
1557 1.29 kiyohara xs->xs_periph->periph_target,
1558 1.29 kiyohara xs->xs_periph->periph_lun,
1559 1.29 kiyohara xs->cmd->opcode,
1560 1.29 kiyohara xs->cmd->bytes[0], xs->cmd->bytes[1],
1561 1.29 kiyohara xs->cmd->bytes[2], xs->cmd->bytes[3],
1562 1.29 kiyohara xs->cmd->bytes[4], xs->cmd->bytes[5],
1563 1.29 kiyohara xs->cmd->bytes[6], xs->cmd->bytes[7],
1564 1.29 kiyohara xs->cmd->bytes[8],
1565 1.29 kiyohara xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT),
1566 1.29 kiyohara xs->cmdlen, xs->datalen);
1567 1.1 kiyohara }
1568 1.1 kiyohara
1569 1.1 kiyohara static void
1570 1.1 kiyohara sbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1571 1.1 kiyohara {
1572 1.1 kiyohara struct sbp_cmd_status *sbp_cmd_status;
1573 1.30 kiyohara struct scsi_sense_data *sense = &ocb->xs->sense.scsi_sense;
1574 1.1 kiyohara
1575 1.1 kiyohara sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1576 1.1 kiyohara
1577 1.1 kiyohara SBP_DEBUG(0)
1578 1.1 kiyohara sbp_print_scsi_cmd(ocb);
1579 1.1 kiyohara /* XXX need decode status */
1580 1.29 kiyohara printf("%s:"
1581 1.29 kiyohara " SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1582 1.29 kiyohara ocb->sdev->bustgtlun,
1583 1.29 kiyohara sbp_cmd_status->status,
1584 1.29 kiyohara sbp_cmd_status->sfmt,
1585 1.29 kiyohara sbp_cmd_status->valid,
1586 1.29 kiyohara sbp_cmd_status->s_key,
1587 1.29 kiyohara sbp_cmd_status->s_code,
1588 1.29 kiyohara sbp_cmd_status->s_qlfr,
1589 1.29 kiyohara sbp_status->len);
1590 1.1 kiyohara END_DEBUG
1591 1.1 kiyohara
1592 1.1 kiyohara switch (sbp_cmd_status->status) {
1593 1.29 kiyohara case SCSI_CHECK:
1594 1.29 kiyohara case SCSI_BUSY:
1595 1.29 kiyohara case SCSI_TERMINATED:
1596 1.29 kiyohara if (sbp_cmd_status->sfmt == SBP_SFMT_CURR)
1597 1.29 kiyohara sense->response_code = SSD_RCODE_CURRENT;
1598 1.29 kiyohara else
1599 1.29 kiyohara sense->response_code = SSD_RCODE_DEFERRED;
1600 1.29 kiyohara if (sbp_cmd_status->valid)
1601 1.30 kiyohara sense->response_code |= SSD_RCODE_VALID;
1602 1.1 kiyohara sense->flags = sbp_cmd_status->s_key;
1603 1.29 kiyohara if (sbp_cmd_status->mark)
1604 1.1 kiyohara sense->flags |= SSD_FILEMARK;
1605 1.29 kiyohara if (sbp_cmd_status->eom)
1606 1.1 kiyohara sense->flags |= SSD_EOM;
1607 1.29 kiyohara if (sbp_cmd_status->ill_len)
1608 1.1 kiyohara sense->flags |= SSD_ILI;
1609 1.1 kiyohara
1610 1.30 kiyohara memcpy(sense->info, &sbp_cmd_status->info, 4);
1611 1.1 kiyohara
1612 1.1 kiyohara if (sbp_status->len <= 1)
1613 1.29 kiyohara /* XXX not scsi status. shouldn't be happened */
1614 1.30 kiyohara sense->extra_len = 0;
1615 1.1 kiyohara else if (sbp_status->len <= 4)
1616 1.1 kiyohara /* add_sense_code(_qual), info, cmd_spec_info */
1617 1.30 kiyohara sense->extra_len = 6;
1618 1.1 kiyohara else
1619 1.1 kiyohara /* fru, sense_key_spec */
1620 1.30 kiyohara sense->extra_len = 10;
1621 1.1 kiyohara
1622 1.29 kiyohara memcpy(sense->csi, &sbp_cmd_status->cdb, 4);
1623 1.1 kiyohara
1624 1.1 kiyohara sense->asc = sbp_cmd_status->s_code;
1625 1.1 kiyohara sense->ascq = sbp_cmd_status->s_qlfr;
1626 1.30 kiyohara sense->fru = sbp_cmd_status->fru;
1627 1.1 kiyohara
1628 1.30 kiyohara memcpy(sense->sks.sks_bytes, sbp_cmd_status->s_keydep, 3);
1629 1.29 kiyohara ocb->xs->error = XS_SENSE;
1630 1.29 kiyohara ocb->xs->xs_status = sbp_cmd_status->status;
1631 1.1 kiyohara /*
1632 1.1 kiyohara {
1633 1.1 kiyohara uint8_t j, *tmp;
1634 1.1 kiyohara tmp = sense;
1635 1.29 kiyohara for (j = 0; j < 32; j += 8)
1636 1.29 kiyohara aprint_normal(
1637 1.29 kiyohara "sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1638 1.29 kiyohara tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1639 1.29 kiyohara tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1640 1.1 kiyohara
1641 1.1 kiyohara }
1642 1.1 kiyohara */
1643 1.1 kiyohara break;
1644 1.1 kiyohara default:
1645 1.29 kiyohara aprint_error_dev(ocb->sdev->target->sbp->sc_fd.dev,
1646 1.29 kiyohara "%s:%s: unknown scsi status 0x%x\n",
1647 1.29 kiyohara __func__, ocb->sdev->bustgtlun, sbp_cmd_status->status);
1648 1.1 kiyohara }
1649 1.1 kiyohara }
1650 1.1 kiyohara
1651 1.1 kiyohara static void
1652 1.1 kiyohara sbp_fix_inq_data(struct sbp_ocb *ocb)
1653 1.1 kiyohara {
1654 1.29 kiyohara struct scsipi_xfer *xs = ocb->xs;
1655 1.1 kiyohara struct sbp_dev *sdev;
1656 1.30 kiyohara struct scsipi_inquiry_data *inq =
1657 1.30 kiyohara (struct scsipi_inquiry_data *)xs->data;
1658 1.29 kiyohara
1659 1.1 kiyohara sdev = ocb->sdev;
1660 1.1 kiyohara
1661 1.29 kiyohara #if 0
1662 1.29 kiyohara /*
1663 1.29 kiyohara * NetBSD is assuming always 0 for EVPD-bit and 'Page Code'.
1664 1.29 kiyohara */
1665 1.29 kiyohara #define SI_EVPD 0x01
1666 1.29 kiyohara if (xs->cmd->bytes[0] & SI_EVPD)
1667 1.1 kiyohara return;
1668 1.29 kiyohara #endif
1669 1.1 kiyohara SBP_DEBUG(1)
1670 1.29 kiyohara printf("%s:%s:%s\n", device_xname(sdev->target->sbp->sc_fd.dev),
1671 1.29 kiyohara __func__, sdev->bustgtlun);
1672 1.1 kiyohara END_DEBUG
1673 1.1 kiyohara switch (inq->device & SID_TYPE) {
1674 1.1 kiyohara case T_DIRECT:
1675 1.1 kiyohara #if 0
1676 1.29 kiyohara /*
1677 1.1 kiyohara * XXX Convert Direct Access device to RBC.
1678 1.1 kiyohara * I've never seen FireWire DA devices which support READ_6.
1679 1.1 kiyohara */
1680 1.1 kiyohara if ((inq->device & SID_TYPE) == T_DIRECT)
1681 1.29 kiyohara inq->device |= T_SIMPLE_DIRECT; /* T_DIRECT == 0 */
1682 1.1 kiyohara #endif
1683 1.29 kiyohara /* FALLTHROUGH */
1684 1.29 kiyohara
1685 1.29 kiyohara case T_SIMPLE_DIRECT:
1686 1.1 kiyohara /*
1687 1.1 kiyohara * Override vendor/product/revision information.
1688 1.1 kiyohara * Some devices sometimes return strange strings.
1689 1.1 kiyohara */
1690 1.1 kiyohara #if 1
1691 1.27 tsutsui memcpy(inq->vendor, sdev->vendor, sizeof(inq->vendor));
1692 1.27 tsutsui memcpy(inq->product, sdev->product, sizeof(inq->product));
1693 1.29 kiyohara memcpy(inq->revision + 2, sdev->revision,
1694 1.29 kiyohara sizeof(inq->revision));
1695 1.1 kiyohara #endif
1696 1.1 kiyohara break;
1697 1.1 kiyohara }
1698 1.1 kiyohara /*
1699 1.1 kiyohara * Force to enable/disable tagged queuing.
1700 1.1 kiyohara * XXX CAM also checks SCP_QUEUE_DQUE flag in the control mode page.
1701 1.1 kiyohara */
1702 1.1 kiyohara if (sbp_tags > 0)
1703 1.30 kiyohara inq->flags3 |= SID_CmdQue;
1704 1.1 kiyohara else if (sbp_tags < 0)
1705 1.30 kiyohara inq->flags3 &= ~SID_CmdQue;
1706 1.1 kiyohara
1707 1.1 kiyohara }
1708 1.1 kiyohara
1709 1.1 kiyohara static void
1710 1.29 kiyohara sbp_recv(struct fw_xfer *xfer)
1711 1.1 kiyohara {
1712 1.1 kiyohara struct fw_pkt *rfp;
1713 1.1 kiyohara #if NEED_RESPONSE
1714 1.1 kiyohara struct fw_pkt *sfp;
1715 1.1 kiyohara #endif
1716 1.29 kiyohara struct sbp_softc *sc;
1717 1.1 kiyohara struct sbp_dev *sdev;
1718 1.1 kiyohara struct sbp_ocb *ocb;
1719 1.1 kiyohara struct sbp_login_res *login_res = NULL;
1720 1.1 kiyohara struct sbp_status *sbp_status;
1721 1.1 kiyohara struct sbp_target *target;
1722 1.1 kiyohara int orb_fun, status_valid0, status_valid, l, reset_agent = 0;
1723 1.1 kiyohara uint32_t addr;
1724 1.1 kiyohara /*
1725 1.1 kiyohara uint32_t *ld;
1726 1.1 kiyohara ld = xfer->recv.buf;
1727 1.1 kiyohara printf("sbp %x %d %d %08x %08x %08x %08x\n",
1728 1.1 kiyohara xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1729 1.1 kiyohara printf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1730 1.1 kiyohara printf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1731 1.1 kiyohara */
1732 1.29 kiyohara
1733 1.29 kiyohara sc = (struct sbp_softc *)xfer->sc;
1734 1.29 kiyohara if (xfer->resp != 0) {
1735 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev,
1736 1.29 kiyohara "sbp_recv: xfer->resp = %d\n", xfer->resp);
1737 1.1 kiyohara goto done0;
1738 1.1 kiyohara }
1739 1.29 kiyohara if (xfer->recv.payload == NULL) {
1740 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev,
1741 1.29 kiyohara "sbp_recv: xfer->recv.payload == NULL\n");
1742 1.1 kiyohara goto done0;
1743 1.1 kiyohara }
1744 1.1 kiyohara rfp = &xfer->recv.hdr;
1745 1.29 kiyohara if (rfp->mode.wreqb.tcode != FWTCODE_WREQB) {
1746 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev,
1747 1.29 kiyohara "sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1748 1.1 kiyohara goto done0;
1749 1.1 kiyohara }
1750 1.1 kiyohara sbp_status = (struct sbp_status *)xfer->recv.payload;
1751 1.1 kiyohara addr = rfp->mode.wreqb.dest_lo;
1752 1.1 kiyohara SBP_DEBUG(2)
1753 1.1 kiyohara printf("received address 0x%x\n", addr);
1754 1.1 kiyohara END_DEBUG
1755 1.29 kiyohara target = &sc->sc_target;
1756 1.1 kiyohara l = SBP_ADDR2LUN(addr);
1757 1.1 kiyohara if (l >= target->num_lun || target->luns[l] == NULL) {
1758 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev,
1759 1.1 kiyohara "sbp_recv1: invalid lun %d (target=%d)\n",
1760 1.1 kiyohara l, target->target_id);
1761 1.1 kiyohara goto done0;
1762 1.1 kiyohara }
1763 1.1 kiyohara sdev = target->luns[l];
1764 1.1 kiyohara
1765 1.1 kiyohara ocb = NULL;
1766 1.1 kiyohara switch (sbp_status->src) {
1767 1.1 kiyohara case SRC_NEXT_EXISTS:
1768 1.1 kiyohara case SRC_NO_NEXT:
1769 1.1 kiyohara /* check mgm_ocb_cur first */
1770 1.29 kiyohara ocb = target->mgm_ocb_cur;
1771 1.29 kiyohara if (ocb != NULL)
1772 1.1 kiyohara if (OCB_MATCH(ocb, sbp_status)) {
1773 1.29 kiyohara callout_stop(&target->mgm_ocb_timeout);
1774 1.1 kiyohara target->mgm_ocb_cur = NULL;
1775 1.1 kiyohara break;
1776 1.1 kiyohara }
1777 1.1 kiyohara ocb = sbp_dequeue_ocb(sdev, sbp_status);
1778 1.29 kiyohara if (ocb == NULL)
1779 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev,
1780 1.29 kiyohara "%s:%s: No ocb(%x) on the queue\n", __func__,
1781 1.29 kiyohara sdev->bustgtlun, ntohl(sbp_status->orb_lo));
1782 1.1 kiyohara break;
1783 1.1 kiyohara case SRC_UNSOL:
1784 1.1 kiyohara /* unsolicit */
1785 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev,
1786 1.29 kiyohara "%s:%s: unsolicit status received\n",
1787 1.29 kiyohara __func__, sdev->bustgtlun);
1788 1.1 kiyohara break;
1789 1.1 kiyohara default:
1790 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev,
1791 1.29 kiyohara "%s:%s: unknown sbp_status->src\n",
1792 1.29 kiyohara __func__, sdev->bustgtlun);
1793 1.1 kiyohara }
1794 1.1 kiyohara
1795 1.1 kiyohara status_valid0 = (sbp_status->src < 2
1796 1.1 kiyohara && sbp_status->resp == SBP_REQ_CMP
1797 1.1 kiyohara && sbp_status->dead == 0);
1798 1.1 kiyohara status_valid = (status_valid0 && sbp_status->status == 0);
1799 1.1 kiyohara
1800 1.29 kiyohara if (!status_valid0 || debug > 2) {
1801 1.1 kiyohara int status;
1802 1.1 kiyohara SBP_DEBUG(0)
1803 1.29 kiyohara printf("%s:%s:%s: ORB status src:%x resp:%x dead:%x"
1804 1.29 kiyohara " len:%x stat:%x orb:%x%08x\n",
1805 1.29 kiyohara device_xname(sc->sc_fd.dev), __func__, sdev->bustgtlun,
1806 1.29 kiyohara sbp_status->src, sbp_status->resp, sbp_status->dead,
1807 1.29 kiyohara sbp_status->len, sbp_status->status,
1808 1.29 kiyohara ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1809 1.1 kiyohara END_DEBUG
1810 1.29 kiyohara printf("%s:%s\n", device_xname(sc->sc_fd.dev), sdev->bustgtlun);
1811 1.1 kiyohara status = sbp_status->status;
1812 1.30 kiyohara switch (sbp_status->resp) {
1813 1.1 kiyohara case SBP_REQ_CMP:
1814 1.1 kiyohara if (status > MAX_ORB_STATUS0)
1815 1.1 kiyohara printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1816 1.1 kiyohara else
1817 1.1 kiyohara printf("%s\n", orb_status0[status]);
1818 1.1 kiyohara break;
1819 1.1 kiyohara case SBP_TRANS_FAIL:
1820 1.1 kiyohara printf("Obj: %s, Error: %s\n",
1821 1.29 kiyohara orb_status1_object[(status>>6) & 3],
1822 1.29 kiyohara orb_status1_serial_bus_error[status & 0xf]);
1823 1.1 kiyohara break;
1824 1.1 kiyohara case SBP_ILLE_REQ:
1825 1.1 kiyohara printf("Illegal request\n");
1826 1.1 kiyohara break;
1827 1.1 kiyohara case SBP_VEND_DEP:
1828 1.1 kiyohara printf("Vendor dependent\n");
1829 1.1 kiyohara break;
1830 1.1 kiyohara default:
1831 1.1 kiyohara printf("unknown respose code %d\n", sbp_status->resp);
1832 1.1 kiyohara }
1833 1.1 kiyohara }
1834 1.1 kiyohara
1835 1.1 kiyohara /* we have to reset the fetch agent if it's dead */
1836 1.1 kiyohara if (sbp_status->dead) {
1837 1.29 kiyohara if (sdev->periph != NULL) {
1838 1.29 kiyohara scsipi_periph_freeze(sdev->periph, 1);
1839 1.29 kiyohara sdev->freeze++;
1840 1.1 kiyohara }
1841 1.1 kiyohara reset_agent = 1;
1842 1.1 kiyohara }
1843 1.1 kiyohara
1844 1.1 kiyohara if (ocb == NULL)
1845 1.1 kiyohara goto done;
1846 1.1 kiyohara
1847 1.30 kiyohara switch (ntohl(ocb->orb[4]) & ORB_FMT_MSK) {
1848 1.1 kiyohara case ORB_FMT_NOP:
1849 1.1 kiyohara break;
1850 1.1 kiyohara case ORB_FMT_VED:
1851 1.1 kiyohara break;
1852 1.1 kiyohara case ORB_FMT_STD:
1853 1.30 kiyohara switch (ocb->flags) {
1854 1.1 kiyohara case OCB_ACT_MGM:
1855 1.1 kiyohara orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1856 1.1 kiyohara reset_agent = 0;
1857 1.30 kiyohara switch (orb_fun) {
1858 1.1 kiyohara case ORB_FUN_LGI:
1859 1.29 kiyohara {
1860 1.29 kiyohara const struct fwdma_alloc *dma = &sdev->dma;
1861 1.29 kiyohara const off_t sbp_login_off =
1862 1.29 kiyohara sizeof(struct sbp_ocb) * SBP_QUEUE_LEN;
1863 1.29 kiyohara
1864 1.29 kiyohara bus_dmamap_sync(dma->dma_tag, dma->dma_map,
1865 1.29 kiyohara sbp_login_off, SBP_LOGIN_SIZE,
1866 1.29 kiyohara BUS_DMASYNC_POSTREAD);
1867 1.1 kiyohara login_res = sdev->login;
1868 1.1 kiyohara login_res->len = ntohs(login_res->len);
1869 1.1 kiyohara login_res->id = ntohs(login_res->id);
1870 1.1 kiyohara login_res->cmd_hi = ntohs(login_res->cmd_hi);
1871 1.1 kiyohara login_res->cmd_lo = ntohl(login_res->cmd_lo);
1872 1.1 kiyohara if (status_valid) {
1873 1.1 kiyohara SBP_DEBUG(0)
1874 1.29 kiyohara printf("%s:%s:%s: login:"
1875 1.29 kiyohara " len %d, ID %d, cmd %08x%08x,"
1876 1.29 kiyohara " recon_hold %d\n",
1877 1.29 kiyohara device_xname(sc->sc_fd.dev),
1878 1.29 kiyohara __func__, sdev->bustgtlun,
1879 1.29 kiyohara login_res->len, login_res->id,
1880 1.29 kiyohara login_res->cmd_hi,
1881 1.29 kiyohara login_res->cmd_lo,
1882 1.29 kiyohara ntohs(login_res->recon_hold));
1883 1.1 kiyohara END_DEBUG
1884 1.1 kiyohara sbp_busy_timeout(sdev);
1885 1.1 kiyohara } else {
1886 1.1 kiyohara /* forgot logout? */
1887 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev,
1888 1.29 kiyohara "%s:%s: login failed\n",
1889 1.29 kiyohara __func__, sdev->bustgtlun);
1890 1.1 kiyohara sdev->status = SBP_DEV_RESET;
1891 1.1 kiyohara }
1892 1.1 kiyohara break;
1893 1.29 kiyohara }
1894 1.1 kiyohara case ORB_FUN_RCN:
1895 1.1 kiyohara login_res = sdev->login;
1896 1.1 kiyohara if (status_valid) {
1897 1.1 kiyohara SBP_DEBUG(0)
1898 1.29 kiyohara printf("%s:%s:%s: reconnect:"
1899 1.29 kiyohara " len %d, ID %d, cmd %08x%08x\n",
1900 1.29 kiyohara device_xname(sc->sc_fd.dev),
1901 1.29 kiyohara __func__, sdev->bustgtlun,
1902 1.29 kiyohara login_res->len, login_res->id,
1903 1.29 kiyohara login_res->cmd_hi,
1904 1.29 kiyohara login_res->cmd_lo);
1905 1.1 kiyohara END_DEBUG
1906 1.29 kiyohara sbp_agent_reset(sdev);
1907 1.1 kiyohara } else {
1908 1.1 kiyohara /* reconnection hold time exceed? */
1909 1.1 kiyohara SBP_DEBUG(0)
1910 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev,
1911 1.29 kiyohara "%s:%s: reconnect failed\n",
1912 1.29 kiyohara __func__, sdev->bustgtlun);
1913 1.1 kiyohara END_DEBUG
1914 1.1 kiyohara sbp_login(sdev);
1915 1.1 kiyohara }
1916 1.1 kiyohara break;
1917 1.1 kiyohara case ORB_FUN_LGO:
1918 1.1 kiyohara sdev->status = SBP_DEV_RESET;
1919 1.1 kiyohara break;
1920 1.1 kiyohara case ORB_FUN_RST:
1921 1.1 kiyohara sbp_busy_timeout(sdev);
1922 1.1 kiyohara break;
1923 1.1 kiyohara case ORB_FUN_LUR:
1924 1.1 kiyohara case ORB_FUN_ATA:
1925 1.1 kiyohara case ORB_FUN_ATS:
1926 1.1 kiyohara sbp_agent_reset(sdev);
1927 1.1 kiyohara break;
1928 1.1 kiyohara default:
1929 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev,
1930 1.29 kiyohara "%s:%s: unknown function %d\n",
1931 1.29 kiyohara __func__, sdev->bustgtlun, orb_fun);
1932 1.1 kiyohara break;
1933 1.1 kiyohara }
1934 1.1 kiyohara sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
1935 1.1 kiyohara break;
1936 1.1 kiyohara case OCB_ACT_CMD:
1937 1.1 kiyohara sdev->timeout = 0;
1938 1.29 kiyohara if (ocb->xs != NULL) {
1939 1.29 kiyohara struct scsipi_xfer *xs = ocb->xs;
1940 1.29 kiyohara
1941 1.29 kiyohara if (sbp_status->len > 1)
1942 1.29 kiyohara sbp_scsi_status(sbp_status, ocb);
1943 1.1 kiyohara else
1944 1.29 kiyohara if (sbp_status->resp != SBP_REQ_CMP)
1945 1.29 kiyohara xs->error = XS_DRIVER_STUFFUP;
1946 1.29 kiyohara else {
1947 1.29 kiyohara xs->error = XS_NOERROR;
1948 1.29 kiyohara xs->resid = 0;
1949 1.1 kiyohara }
1950 1.1 kiyohara /* fix up inq data */
1951 1.29 kiyohara if (xs->cmd->opcode == INQUIRY)
1952 1.1 kiyohara sbp_fix_inq_data(ocb);
1953 1.29 kiyohara scsipi_done(xs);
1954 1.1 kiyohara }
1955 1.1 kiyohara break;
1956 1.1 kiyohara default:
1957 1.1 kiyohara break;
1958 1.1 kiyohara }
1959 1.1 kiyohara }
1960 1.1 kiyohara
1961 1.1 kiyohara if (!use_doorbell)
1962 1.1 kiyohara sbp_free_ocb(sdev, ocb);
1963 1.1 kiyohara done:
1964 1.1 kiyohara if (reset_agent)
1965 1.1 kiyohara sbp_agent_reset(sdev);
1966 1.1 kiyohara
1967 1.1 kiyohara done0:
1968 1.1 kiyohara xfer->recv.pay_len = SBP_RECV_LEN;
1969 1.1 kiyohara /* The received packet is usually small enough to be stored within
1970 1.1 kiyohara * the buffer. In that case, the controller return ack_complete and
1971 1.1 kiyohara * no respose is necessary.
1972 1.1 kiyohara *
1973 1.29 kiyohara * XXX fwohci.c and firewire.c should inform event_code such as
1974 1.1 kiyohara * ack_complete or ack_pending to upper driver.
1975 1.1 kiyohara */
1976 1.1 kiyohara #if NEED_RESPONSE
1977 1.1 kiyohara xfer->send.off = 0;
1978 1.1 kiyohara sfp = (struct fw_pkt *)xfer->send.buf;
1979 1.1 kiyohara sfp->mode.wres.dst = rfp->mode.wreqb.src;
1980 1.1 kiyohara xfer->dst = sfp->mode.wres.dst;
1981 1.37 riastrad xfer->spd = uimin(sdev->target->fwdev->speed, max_speed);
1982 1.1 kiyohara xfer->hand = sbp_loginres_callback;
1983 1.1 kiyohara
1984 1.1 kiyohara sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1985 1.1 kiyohara sfp->mode.wres.tcode = FWTCODE_WRES;
1986 1.1 kiyohara sfp->mode.wres.rtcode = 0;
1987 1.1 kiyohara sfp->mode.wres.pri = 0;
1988 1.1 kiyohara
1989 1.29 kiyohara if (fw_asyreq(xfer->fc, -1, xfer) != 0) {
1990 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev, "mgm_orb failed\n");
1991 1.29 kiyohara mutex_enter(&sc->sc_fwb.fwb_mtx);
1992 1.29 kiyohara STAILQ_INSERT_TAIL(&sc->sc_fwb.xferlist, xfer, link);
1993 1.29 kiyohara mutex_exit(&sc->sc_fwb.fwb_mtx);
1994 1.29 kiyohara }
1995 1.1 kiyohara #else
1996 1.1 kiyohara /* recycle */
1997 1.29 kiyohara mutex_enter(&sc->sc_fwb.fwb_mtx);
1998 1.29 kiyohara STAILQ_INSERT_TAIL(&sc->sc_fwb.xferlist, xfer, link);
1999 1.29 kiyohara mutex_exit(&sc->sc_fwb.fwb_mtx);
2000 1.1 kiyohara #endif
2001 1.1 kiyohara
2002 1.1 kiyohara return;
2003 1.1 kiyohara
2004 1.1 kiyohara }
2005 1.1 kiyohara
2006 1.1 kiyohara static int
2007 1.1 kiyohara sbp_logout_all(struct sbp_softc *sbp)
2008 1.1 kiyohara {
2009 1.1 kiyohara struct sbp_target *target;
2010 1.1 kiyohara struct sbp_dev *sdev;
2011 1.1 kiyohara int i;
2012 1.1 kiyohara
2013 1.1 kiyohara SBP_DEBUG(0)
2014 1.1 kiyohara printf("sbp_logout_all\n");
2015 1.1 kiyohara END_DEBUG
2016 1.29 kiyohara target = &sbp->sc_target;
2017 1.33 cegger if (target->luns != NULL) {
2018 1.1 kiyohara for (i = 0; i < target->num_lun; i++) {
2019 1.1 kiyohara sdev = target->luns[i];
2020 1.1 kiyohara if (sdev == NULL)
2021 1.1 kiyohara continue;
2022 1.29 kiyohara callout_stop(&sdev->login_callout);
2023 1.1 kiyohara if (sdev->status >= SBP_DEV_TOATTACH &&
2024 1.29 kiyohara sdev->status <= SBP_DEV_ATTACHED)
2025 1.1 kiyohara sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
2026 1.1 kiyohara }
2027 1.33 cegger }
2028 1.1 kiyohara
2029 1.1 kiyohara return 0;
2030 1.1 kiyohara }
2031 1.1 kiyohara
2032 1.1 kiyohara static void
2033 1.1 kiyohara sbp_free_sdev(struct sbp_dev *sdev)
2034 1.1 kiyohara {
2035 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp;
2036 1.1 kiyohara int i;
2037 1.1 kiyohara
2038 1.1 kiyohara if (sdev == NULL)
2039 1.1 kiyohara return;
2040 1.1 kiyohara for (i = 0; i < SBP_QUEUE_LEN; i++)
2041 1.29 kiyohara bus_dmamap_destroy(sc->sc_dmat, sdev->ocb[i].dmamap);
2042 1.29 kiyohara fwdma_free(sdev->dma.dma_tag, sdev->dma.dma_map, sdev->dma.v_addr);
2043 1.32 christos free(sdev, M_SBP);
2044 1.1 kiyohara }
2045 1.1 kiyohara
2046 1.1 kiyohara static void
2047 1.1 kiyohara sbp_free_target(struct sbp_target *target)
2048 1.1 kiyohara {
2049 1.1 kiyohara struct fw_xfer *xfer, *next;
2050 1.1 kiyohara int i;
2051 1.1 kiyohara
2052 1.1 kiyohara if (target->luns == NULL)
2053 1.1 kiyohara return;
2054 1.29 kiyohara callout_stop(&target->mgm_ocb_timeout);
2055 1.1 kiyohara for (i = 0; i < target->num_lun; i++)
2056 1.1 kiyohara sbp_free_sdev(target->luns[i]);
2057 1.1 kiyohara
2058 1.1 kiyohara for (xfer = STAILQ_FIRST(&target->xferlist);
2059 1.29 kiyohara xfer != NULL; xfer = next) {
2060 1.1 kiyohara next = STAILQ_NEXT(xfer, link);
2061 1.1 kiyohara fw_xfer_free_buf(xfer);
2062 1.1 kiyohara }
2063 1.1 kiyohara STAILQ_INIT(&target->xferlist);
2064 1.32 christos free(target->luns, M_SBP);
2065 1.23 yamt target->num_lun = 0;
2066 1.1 kiyohara target->luns = NULL;
2067 1.1 kiyohara target->fwdev = NULL;
2068 1.1 kiyohara }
2069 1.1 kiyohara
2070 1.1 kiyohara static void
2071 1.1 kiyohara sbp_scsipi_detach_sdev(struct sbp_dev *sdev)
2072 1.1 kiyohara {
2073 1.7 rpaulo struct sbp_target *target;
2074 1.7 rpaulo struct sbp_softc *sbp;
2075 1.7 rpaulo
2076 1.1 kiyohara if (sdev == NULL)
2077 1.1 kiyohara return;
2078 1.7 rpaulo
2079 1.7 rpaulo target = sdev->target;
2080 1.7 rpaulo if (target == NULL)
2081 1.7 rpaulo return;
2082 1.7 rpaulo
2083 1.7 rpaulo sbp = target->sbp;
2084 1.7 rpaulo
2085 1.1 kiyohara if (sdev->status == SBP_DEV_DEAD)
2086 1.1 kiyohara return;
2087 1.1 kiyohara if (sdev->status == SBP_DEV_RESET)
2088 1.1 kiyohara return;
2089 1.29 kiyohara if (sdev->periph != NULL) {
2090 1.1 kiyohara scsipi_periph_thaw(sdev->periph, sdev->freeze);
2091 1.1 kiyohara scsipi_channel_thaw(&sbp->sc_channel, 0); /* XXXX */
2092 1.1 kiyohara sdev->freeze = 0;
2093 1.1 kiyohara if (scsipi_target_detach(&sbp->sc_channel,
2094 1.1 kiyohara target->target_id, sdev->lun_id, DETACH_FORCE) != 0) {
2095 1.29 kiyohara aprint_error_dev(sbp->sc_fd.dev, "detach failed\n");
2096 1.1 kiyohara }
2097 1.1 kiyohara sdev->periph = NULL;
2098 1.1 kiyohara }
2099 1.29 kiyohara sbp_abort_all_ocbs(sdev, XS_DRIVER_STUFFUP);
2100 1.1 kiyohara }
2101 1.1 kiyohara
2102 1.1 kiyohara static void
2103 1.1 kiyohara sbp_scsipi_detach_target(struct sbp_target *target)
2104 1.1 kiyohara {
2105 1.1 kiyohara struct sbp_softc *sbp = target->sbp;
2106 1.1 kiyohara int i;
2107 1.1 kiyohara
2108 1.1 kiyohara if (target->luns != NULL) {
2109 1.1 kiyohara SBP_DEBUG(0)
2110 1.1 kiyohara printf("sbp_detach_target %d\n", target->target_id);
2111 1.1 kiyohara END_DEBUG
2112 1.1 kiyohara for (i = 0; i < target->num_lun; i++)
2113 1.1 kiyohara sbp_scsipi_detach_sdev(target->luns[i]);
2114 1.1 kiyohara if (config_detach(sbp->sc_bus, DETACH_FORCE) != 0)
2115 1.29 kiyohara aprint_error_dev(sbp->sc_fd.dev, "%d detach failed\n",
2116 1.29 kiyohara target->target_id);
2117 1.1 kiyohara sbp->sc_bus = NULL;
2118 1.1 kiyohara }
2119 1.1 kiyohara }
2120 1.1 kiyohara
2121 1.1 kiyohara static void
2122 1.1 kiyohara sbp_target_reset(struct sbp_dev *sdev, int method)
2123 1.1 kiyohara {
2124 1.1 kiyohara struct sbp_target *target = sdev->target;
2125 1.1 kiyohara struct sbp_dev *tsdev;
2126 1.29 kiyohara int i;
2127 1.1 kiyohara
2128 1.1 kiyohara for (i = 0; i < target->num_lun; i++) {
2129 1.1 kiyohara tsdev = target->luns[i];
2130 1.1 kiyohara if (tsdev == NULL)
2131 1.1 kiyohara continue;
2132 1.1 kiyohara if (tsdev->status == SBP_DEV_DEAD)
2133 1.1 kiyohara continue;
2134 1.1 kiyohara if (tsdev->status == SBP_DEV_RESET)
2135 1.1 kiyohara continue;
2136 1.29 kiyohara if (sdev->periph != NULL) {
2137 1.29 kiyohara scsipi_periph_freeze(tsdev->periph, 1);
2138 1.29 kiyohara tsdev->freeze++;
2139 1.29 kiyohara }
2140 1.29 kiyohara sbp_abort_all_ocbs(tsdev, XS_TIMEOUT);
2141 1.1 kiyohara if (method == 2)
2142 1.1 kiyohara tsdev->status = SBP_DEV_LOGIN;
2143 1.1 kiyohara }
2144 1.30 kiyohara switch (method) {
2145 1.1 kiyohara case 1:
2146 1.29 kiyohara aprint_error("target reset\n");
2147 1.1 kiyohara sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2148 1.1 kiyohara break;
2149 1.1 kiyohara case 2:
2150 1.29 kiyohara aprint_error("reset start\n");
2151 1.1 kiyohara sbp_reset_start(sdev);
2152 1.1 kiyohara break;
2153 1.1 kiyohara }
2154 1.1 kiyohara }
2155 1.1 kiyohara
2156 1.1 kiyohara static void
2157 1.1 kiyohara sbp_mgm_timeout(void *arg)
2158 1.1 kiyohara {
2159 1.1 kiyohara struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2160 1.1 kiyohara struct sbp_dev *sdev = ocb->sdev;
2161 1.1 kiyohara struct sbp_target *target = sdev->target;
2162 1.1 kiyohara
2163 1.29 kiyohara aprint_error_dev(sdev->target->sbp->sc_fd.dev,
2164 1.29 kiyohara "%s:%s: request timeout(mgm orb:0x%08x) ... ",
2165 1.29 kiyohara __func__, sdev->bustgtlun, (uint32_t)ocb->bus_addr);
2166 1.1 kiyohara target->mgm_ocb_cur = NULL;
2167 1.1 kiyohara sbp_free_ocb(sdev, ocb);
2168 1.1 kiyohara #if 0
2169 1.1 kiyohara /* XXX */
2170 1.29 kiyohara aprint_error("run next request\n");
2171 1.1 kiyohara sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2172 1.1 kiyohara #endif
2173 1.29 kiyohara aprint_error_dev(sdev->target->sbp->sc_fd.dev,
2174 1.29 kiyohara "%s:%s: reset start\n", __func__, sdev->bustgtlun);
2175 1.1 kiyohara sbp_reset_start(sdev);
2176 1.1 kiyohara }
2177 1.1 kiyohara
2178 1.1 kiyohara static void
2179 1.1 kiyohara sbp_timeout(void *arg)
2180 1.1 kiyohara {
2181 1.1 kiyohara struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2182 1.1 kiyohara struct sbp_dev *sdev = ocb->sdev;
2183 1.1 kiyohara
2184 1.29 kiyohara aprint_error_dev(sdev->target->sbp->sc_fd.dev,
2185 1.29 kiyohara "%s:%s: request timeout(cmd orb:0x%08x) ... ",
2186 1.29 kiyohara __func__, sdev->bustgtlun, (uint32_t)ocb->bus_addr);
2187 1.1 kiyohara
2188 1.29 kiyohara sdev->timeout++;
2189 1.30 kiyohara switch (sdev->timeout) {
2190 1.1 kiyohara case 1:
2191 1.29 kiyohara aprint_error("agent reset\n");
2192 1.29 kiyohara if (sdev->periph != NULL) {
2193 1.29 kiyohara scsipi_periph_freeze(sdev->periph, 1);
2194 1.29 kiyohara sdev->freeze++;
2195 1.29 kiyohara }
2196 1.29 kiyohara sbp_abort_all_ocbs(sdev, XS_TIMEOUT);
2197 1.1 kiyohara sbp_agent_reset(sdev);
2198 1.1 kiyohara break;
2199 1.1 kiyohara case 2:
2200 1.1 kiyohara case 3:
2201 1.1 kiyohara sbp_target_reset(sdev, sdev->timeout - 1);
2202 1.1 kiyohara break;
2203 1.29 kiyohara default:
2204 1.29 kiyohara aprint_error("\n");
2205 1.1 kiyohara #if 0
2206 1.1 kiyohara /* XXX give up */
2207 1.29 kiyohara sbp_scsipi_detach_target(target);
2208 1.1 kiyohara if (target->luns != NULL)
2209 1.32 christos free(target->luns, M_SBP);
2210 1.23 yamt target->num_lun = 0;
2211 1.1 kiyohara target->luns = NULL;
2212 1.1 kiyohara target->fwdev = NULL;
2213 1.1 kiyohara #endif
2214 1.1 kiyohara }
2215 1.1 kiyohara }
2216 1.1 kiyohara
2217 1.1 kiyohara static void
2218 1.29 kiyohara sbp_action1(struct sbp_softc *sc, struct scsipi_xfer *xs)
2219 1.1 kiyohara {
2220 1.29 kiyohara struct sbp_target *target = &sc->sc_target;
2221 1.1 kiyohara struct sbp_dev *sdev = NULL;
2222 1.29 kiyohara struct sbp_ocb *ocb;
2223 1.29 kiyohara int speed, flag, error;
2224 1.29 kiyohara void *cdb;
2225 1.1 kiyohara
2226 1.1 kiyohara /* target:lun -> sdev mapping */
2227 1.29 kiyohara if (target->fwdev != NULL &&
2228 1.29 kiyohara xs->xs_periph->periph_lun < target->num_lun) {
2229 1.29 kiyohara sdev = target->luns[xs->xs_periph->periph_lun];
2230 1.29 kiyohara if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED &&
2231 1.29 kiyohara sdev->status != SBP_DEV_PROBE)
2232 1.29 kiyohara sdev = NULL;
2233 1.1 kiyohara }
2234 1.1 kiyohara
2235 1.29 kiyohara if (sdev == NULL) {
2236 1.1 kiyohara SBP_DEBUG(1)
2237 1.29 kiyohara printf("%s:%d:%d: Invalid target (target needed)\n",
2238 1.29 kiyohara sc ? device_xname(sc->sc_fd.dev) : "???",
2239 1.29 kiyohara xs->xs_periph->periph_target,
2240 1.29 kiyohara xs->xs_periph->periph_lun);
2241 1.1 kiyohara END_DEBUG
2242 1.1 kiyohara
2243 1.29 kiyohara xs->error = XS_DRIVER_STUFFUP;
2244 1.29 kiyohara scsipi_done(xs);
2245 1.29 kiyohara return;
2246 1.1 kiyohara }
2247 1.1 kiyohara
2248 1.1 kiyohara SBP_DEBUG(2)
2249 1.29 kiyohara printf("%s:%d:%d:"
2250 1.29 kiyohara " cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x,"
2251 1.29 kiyohara " flags: 0x%02x, %db cmd/%db data\n",
2252 1.29 kiyohara device_xname(sc->sc_fd.dev),
2253 1.29 kiyohara xs->xs_periph->periph_target,
2254 1.29 kiyohara xs->xs_periph->periph_lun,
2255 1.29 kiyohara xs->cmd->opcode,
2256 1.29 kiyohara xs->cmd->bytes[0], xs->cmd->bytes[1],
2257 1.29 kiyohara xs->cmd->bytes[2], xs->cmd->bytes[3],
2258 1.29 kiyohara xs->cmd->bytes[4], xs->cmd->bytes[5],
2259 1.29 kiyohara xs->cmd->bytes[6], xs->cmd->bytes[7],
2260 1.29 kiyohara xs->cmd->bytes[8],
2261 1.29 kiyohara xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT),
2262 1.29 kiyohara xs->cmdlen, xs->datalen);
2263 1.29 kiyohara END_DEBUG
2264 1.29 kiyohara mutex_enter(&sc->sc_mtx);
2265 1.29 kiyohara ocb = sbp_get_ocb(sdev);
2266 1.29 kiyohara mutex_exit(&sc->sc_mtx);
2267 1.29 kiyohara if (ocb == NULL) {
2268 1.29 kiyohara xs->error = XS_REQUEUE;
2269 1.29 kiyohara if (sdev->freeze == 0) {
2270 1.29 kiyohara scsipi_periph_freeze(sdev->periph, 1);
2271 1.29 kiyohara sdev->freeze++;
2272 1.1 kiyohara }
2273 1.29 kiyohara scsipi_done(xs);
2274 1.29 kiyohara return;
2275 1.29 kiyohara }
2276 1.1 kiyohara
2277 1.29 kiyohara ocb->flags = OCB_ACT_CMD;
2278 1.29 kiyohara ocb->sdev = sdev;
2279 1.29 kiyohara ocb->xs = xs;
2280 1.29 kiyohara ocb->orb[0] = htonl(1 << 31);
2281 1.29 kiyohara ocb->orb[1] = 0;
2282 1.29 kiyohara ocb->orb[2] = htonl(((sc->sc_fd.fc->nodeid | FWLOCALBUS) << 16));
2283 1.29 kiyohara ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2284 1.37 riastrad speed = uimin(target->fwdev->speed, max_speed);
2285 1.29 kiyohara ocb->orb[4] =
2286 1.29 kiyohara htonl(ORB_NOTIFY | ORB_CMD_SPD(speed) | ORB_CMD_MAXP(speed + 7));
2287 1.29 kiyohara if ((xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) ==
2288 1.29 kiyohara XS_CTL_DATA_IN) {
2289 1.29 kiyohara ocb->orb[4] |= htonl(ORB_CMD_IN);
2290 1.29 kiyohara flag = BUS_DMA_READ;
2291 1.29 kiyohara } else
2292 1.29 kiyohara flag = BUS_DMA_WRITE;
2293 1.1 kiyohara
2294 1.29 kiyohara cdb = xs->cmd;
2295 1.29 kiyohara memcpy((void *)&ocb->orb[5], cdb, xs->cmdlen);
2296 1.1 kiyohara /*
2297 1.1 kiyohara printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2298 1.1 kiyohara printf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2299 1.1 kiyohara */
2300 1.29 kiyohara if (xs->datalen > 0) {
2301 1.29 kiyohara error = bus_dmamap_load(sc->sc_dmat, ocb->dmamap,
2302 1.29 kiyohara xs->data, xs->datalen, NULL, BUS_DMA_NOWAIT | flag);
2303 1.29 kiyohara if (error) {
2304 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev,
2305 1.29 kiyohara "DMA map load error %d\n", error);
2306 1.29 kiyohara xs->error = XS_DRIVER_STUFFUP;
2307 1.29 kiyohara scsipi_done(xs);
2308 1.1 kiyohara } else
2309 1.29 kiyohara sbp_execute_ocb(ocb, ocb->dmamap->dm_segs,
2310 1.29 kiyohara ocb->dmamap->dm_nsegs);
2311 1.29 kiyohara } else
2312 1.29 kiyohara sbp_execute_ocb(ocb, NULL, 0);
2313 1.1 kiyohara
2314 1.1 kiyohara return;
2315 1.1 kiyohara }
2316 1.1 kiyohara
2317 1.1 kiyohara static void
2318 1.29 kiyohara sbp_execute_ocb(struct sbp_ocb *ocb, bus_dma_segment_t *segments, int seg)
2319 1.1 kiyohara {
2320 1.1 kiyohara struct sbp_ocb *prev;
2321 1.1 kiyohara bus_dma_segment_t *s;
2322 1.29 kiyohara int i;
2323 1.1 kiyohara
2324 1.1 kiyohara SBP_DEBUG(2)
2325 1.1 kiyohara printf("sbp_execute_ocb: seg %d", seg);
2326 1.1 kiyohara for (i = 0; i < seg; i++)
2327 1.1 kiyohara printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2328 1.29 kiyohara (uintmax_t)segments[i].ds_len);
2329 1.1 kiyohara printf("\n");
2330 1.1 kiyohara END_DEBUG
2331 1.1 kiyohara
2332 1.1 kiyohara if (seg == 1) {
2333 1.1 kiyohara /* direct pointer */
2334 1.29 kiyohara s = segments;
2335 1.1 kiyohara if (s->ds_len > SBP_SEG_MAX)
2336 1.1 kiyohara panic("ds_len > SBP_SEG_MAX, fix busdma code");
2337 1.1 kiyohara ocb->orb[3] = htonl(s->ds_addr);
2338 1.1 kiyohara ocb->orb[4] |= htonl(s->ds_len);
2339 1.29 kiyohara } else if (seg > 1) {
2340 1.1 kiyohara /* page table */
2341 1.1 kiyohara for (i = 0; i < seg; i++) {
2342 1.1 kiyohara s = &segments[i];
2343 1.1 kiyohara SBP_DEBUG(0)
2344 1.1 kiyohara /* XXX LSI Logic "< 16 byte" bug might be hit */
2345 1.1 kiyohara if (s->ds_len < 16)
2346 1.1 kiyohara printf("sbp_execute_ocb: warning, "
2347 1.29 kiyohara "segment length(%jd) is less than 16."
2348 1.29 kiyohara "(seg=%d/%d)\n",
2349 1.29 kiyohara (uintmax_t)s->ds_len, i + 1, seg);
2350 1.1 kiyohara END_DEBUG
2351 1.1 kiyohara if (s->ds_len > SBP_SEG_MAX)
2352 1.1 kiyohara panic("ds_len > SBP_SEG_MAX, fix busdma code");
2353 1.1 kiyohara ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2354 1.1 kiyohara ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2355 1.1 kiyohara }
2356 1.1 kiyohara ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2357 1.1 kiyohara }
2358 1.29 kiyohara
2359 1.29 kiyohara if (seg > 0) {
2360 1.29 kiyohara struct sbp_softc *sc = ocb->sdev->target->sbp;
2361 1.29 kiyohara const int flag = (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2362 1.29 kiyohara BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE;
2363 1.29 kiyohara
2364 1.29 kiyohara bus_dmamap_sync(sc->sc_dmat, ocb->dmamap,
2365 1.29 kiyohara 0, ocb->dmamap->dm_mapsize, flag);
2366 1.29 kiyohara }
2367 1.1 kiyohara prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2368 1.29 kiyohara SBP_ORB_DMA_SYNC(ocb->sdev->dma, ocb->index, BUS_DMASYNC_PREWRITE);
2369 1.1 kiyohara if (use_doorbell) {
2370 1.1 kiyohara if (prev == NULL) {
2371 1.1 kiyohara if (ocb->sdev->last_ocb != NULL)
2372 1.1 kiyohara sbp_doorbell(ocb->sdev);
2373 1.1 kiyohara else
2374 1.29 kiyohara sbp_orb_pointer(ocb->sdev, ocb);
2375 1.1 kiyohara }
2376 1.29 kiyohara } else
2377 1.1 kiyohara if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) {
2378 1.1 kiyohara ocb->sdev->flags &= ~ORB_LINK_DEAD;
2379 1.29 kiyohara sbp_orb_pointer(ocb->sdev, ocb);
2380 1.1 kiyohara }
2381 1.1 kiyohara }
2382 1.1 kiyohara
2383 1.1 kiyohara static struct sbp_ocb *
2384 1.1 kiyohara sbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2385 1.1 kiyohara {
2386 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp;
2387 1.1 kiyohara struct sbp_ocb *ocb;
2388 1.1 kiyohara struct sbp_ocb *next;
2389 1.29 kiyohara int order = 0;
2390 1.1 kiyohara
2391 1.1 kiyohara SBP_DEBUG(1)
2392 1.29 kiyohara printf("%s:%s:%s: 0x%08x src %d\n", device_xname(sc->sc_fd.dev),
2393 1.29 kiyohara __func__, sdev->bustgtlun, ntohl(sbp_status->orb_lo),
2394 1.29 kiyohara sbp_status->src);
2395 1.1 kiyohara END_DEBUG
2396 1.29 kiyohara mutex_enter(&sc->sc_mtx);
2397 1.1 kiyohara for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2398 1.1 kiyohara next = STAILQ_NEXT(ocb, ocb);
2399 1.1 kiyohara if (OCB_MATCH(ocb, sbp_status)) {
2400 1.1 kiyohara /* found */
2401 1.29 kiyohara SBP_ORB_DMA_SYNC(sdev->dma, ocb->index,
2402 1.29 kiyohara BUS_DMASYNC_POSTWRITE);
2403 1.1 kiyohara STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2404 1.29 kiyohara if (ocb->xs != NULL)
2405 1.29 kiyohara callout_stop(&ocb->xs->xs_callout);
2406 1.1 kiyohara if (ntohl(ocb->orb[4]) & 0xffff) {
2407 1.29 kiyohara const int flag =
2408 1.29 kiyohara (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2409 1.29 kiyohara BUS_DMASYNC_POSTREAD :
2410 1.29 kiyohara BUS_DMASYNC_POSTWRITE;
2411 1.29 kiyohara
2412 1.29 kiyohara bus_dmamap_sync(sc->sc_dmat, ocb->dmamap,
2413 1.29 kiyohara 0, ocb->dmamap->dm_mapsize, flag);
2414 1.29 kiyohara bus_dmamap_unload(sc->sc_dmat, ocb->dmamap);
2415 1.29 kiyohara
2416 1.1 kiyohara }
2417 1.1 kiyohara if (!use_doorbell) {
2418 1.1 kiyohara if (sbp_status->src == SRC_NO_NEXT) {
2419 1.1 kiyohara if (next != NULL)
2420 1.29 kiyohara sbp_orb_pointer(sdev, next);
2421 1.29 kiyohara else if (order > 0)
2422 1.1 kiyohara /*
2423 1.1 kiyohara * Unordered execution
2424 1.1 kiyohara * We need to send pointer for
2425 1.1 kiyohara * next ORB
2426 1.1 kiyohara */
2427 1.1 kiyohara sdev->flags |= ORB_LINK_DEAD;
2428 1.1 kiyohara }
2429 1.1 kiyohara }
2430 1.1 kiyohara break;
2431 1.1 kiyohara } else
2432 1.29 kiyohara order++;
2433 1.1 kiyohara }
2434 1.29 kiyohara mutex_exit(&sc->sc_mtx);
2435 1.29 kiyohara
2436 1.29 kiyohara if (ocb && use_doorbell) {
2437 1.29 kiyohara /*
2438 1.29 kiyohara * XXX this is not correct for unordered
2439 1.29 kiyohara * execution.
2440 1.29 kiyohara */
2441 1.29 kiyohara if (sdev->last_ocb != NULL)
2442 1.29 kiyohara sbp_free_ocb(sdev, sdev->last_ocb);
2443 1.29 kiyohara sdev->last_ocb = ocb;
2444 1.29 kiyohara if (next != NULL &&
2445 1.29 kiyohara sbp_status->src == SRC_NO_NEXT)
2446 1.29 kiyohara sbp_doorbell(sdev);
2447 1.29 kiyohara }
2448 1.29 kiyohara
2449 1.1 kiyohara SBP_DEBUG(0)
2450 1.29 kiyohara if (ocb && order > 0)
2451 1.29 kiyohara printf("%s:%s:%s: unordered execution order:%d\n",
2452 1.29 kiyohara device_xname(sc->sc_fd.dev), __func__, sdev->bustgtlun,
2453 1.29 kiyohara order);
2454 1.1 kiyohara END_DEBUG
2455 1.29 kiyohara return ocb;
2456 1.1 kiyohara }
2457 1.1 kiyohara
2458 1.1 kiyohara static struct sbp_ocb *
2459 1.1 kiyohara sbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2460 1.1 kiyohara {
2461 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp;
2462 1.29 kiyohara struct sbp_ocb *tocb, *prev, *prev2;
2463 1.1 kiyohara
2464 1.1 kiyohara SBP_DEBUG(1)
2465 1.29 kiyohara printf("%s:%s:%s: 0x%08jx\n", device_xname(sc->sc_fd.dev),
2466 1.29 kiyohara __func__, sdev->bustgtlun, (uintmax_t)ocb->bus_addr);
2467 1.1 kiyohara END_DEBUG
2468 1.29 kiyohara mutex_enter(&sc->sc_mtx);
2469 1.29 kiyohara prev = NULL;
2470 1.29 kiyohara STAILQ_FOREACH(tocb, &sdev->ocbs, ocb)
2471 1.29 kiyohara prev = tocb;
2472 1.29 kiyohara prev2 = prev;
2473 1.1 kiyohara STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2474 1.29 kiyohara mutex_exit(&sc->sc_mtx);
2475 1.1 kiyohara
2476 1.29 kiyohara callout_reset(&ocb->xs->xs_callout, mstohz(ocb->xs->timeout),
2477 1.29 kiyohara sbp_timeout, ocb);
2478 1.1 kiyohara
2479 1.1 kiyohara if (use_doorbell && prev == NULL)
2480 1.1 kiyohara prev2 = sdev->last_ocb;
2481 1.1 kiyohara
2482 1.1 kiyohara if (prev2 != NULL) {
2483 1.1 kiyohara SBP_DEBUG(2)
2484 1.1 kiyohara printf("linking chain 0x%jx -> 0x%jx\n",
2485 1.1 kiyohara (uintmax_t)prev2->bus_addr, (uintmax_t)ocb->bus_addr);
2486 1.1 kiyohara END_DEBUG
2487 1.29 kiyohara /*
2488 1.29 kiyohara * Suppress compiler optimization so that orb[1] must be
2489 1.29 kiyohara * written first.
2490 1.29 kiyohara * XXX We may need an explicit memory barrier for other
2491 1.29 kiyohara * architectures other than i386/amd64.
2492 1.29 kiyohara */
2493 1.29 kiyohara *(volatile uint32_t *)&prev2->orb[1] = htonl(ocb->bus_addr);
2494 1.29 kiyohara *(volatile uint32_t *)&prev2->orb[0] = 0;
2495 1.1 kiyohara }
2496 1.1 kiyohara
2497 1.1 kiyohara return prev;
2498 1.1 kiyohara }
2499 1.1 kiyohara
2500 1.1 kiyohara static struct sbp_ocb *
2501 1.1 kiyohara sbp_get_ocb(struct sbp_dev *sdev)
2502 1.1 kiyohara {
2503 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp;
2504 1.1 kiyohara struct sbp_ocb *ocb;
2505 1.19 kiyohara
2506 1.29 kiyohara KASSERT(mutex_owned(&sc->sc_mtx));
2507 1.29 kiyohara
2508 1.1 kiyohara ocb = STAILQ_FIRST(&sdev->free_ocbs);
2509 1.1 kiyohara if (ocb == NULL) {
2510 1.1 kiyohara sdev->flags |= ORB_SHORTAGE;
2511 1.29 kiyohara aprint_error_dev(sc->sc_fd.dev,
2512 1.29 kiyohara "ocb shortage!!!\n");
2513 1.1 kiyohara return NULL;
2514 1.1 kiyohara }
2515 1.1 kiyohara STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2516 1.29 kiyohara ocb->xs = NULL;
2517 1.29 kiyohara return ocb;
2518 1.1 kiyohara }
2519 1.1 kiyohara
2520 1.1 kiyohara static void
2521 1.1 kiyohara sbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2522 1.1 kiyohara {
2523 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp;
2524 1.29 kiyohara int count;
2525 1.29 kiyohara
2526 1.1 kiyohara ocb->flags = 0;
2527 1.29 kiyohara ocb->xs = NULL;
2528 1.19 kiyohara
2529 1.29 kiyohara mutex_enter(&sc->sc_mtx);
2530 1.1 kiyohara STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2531 1.29 kiyohara mutex_exit(&sc->sc_mtx);
2532 1.29 kiyohara if (sdev->flags & ORB_SHORTAGE) {
2533 1.1 kiyohara sdev->flags &= ~ORB_SHORTAGE;
2534 1.1 kiyohara count = sdev->freeze;
2535 1.1 kiyohara sdev->freeze = 0;
2536 1.29 kiyohara if (sdev->periph)
2537 1.29 kiyohara scsipi_periph_thaw(sdev->periph, count);
2538 1.29 kiyohara scsipi_channel_thaw(&sc->sc_channel, 0);
2539 1.1 kiyohara }
2540 1.1 kiyohara }
2541 1.1 kiyohara
2542 1.1 kiyohara static void
2543 1.1 kiyohara sbp_abort_ocb(struct sbp_ocb *ocb, int status)
2544 1.1 kiyohara {
2545 1.29 kiyohara struct sbp_softc *sc;
2546 1.1 kiyohara struct sbp_dev *sdev;
2547 1.1 kiyohara
2548 1.1 kiyohara sdev = ocb->sdev;
2549 1.29 kiyohara sc = sdev->target->sbp;
2550 1.1 kiyohara SBP_DEBUG(0)
2551 1.29 kiyohara printf("%s:%s:%s: sbp_abort_ocb 0x%jx\n", device_xname(sc->sc_fd.dev),
2552 1.29 kiyohara __func__, sdev->bustgtlun, (uintmax_t)ocb->bus_addr);
2553 1.1 kiyohara END_DEBUG
2554 1.1 kiyohara SBP_DEBUG(1)
2555 1.29 kiyohara if (ocb->xs != NULL)
2556 1.1 kiyohara sbp_print_scsi_cmd(ocb);
2557 1.1 kiyohara END_DEBUG
2558 1.1 kiyohara if (ntohl(ocb->orb[4]) & 0xffff) {
2559 1.29 kiyohara const int flag = (ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2560 1.29 kiyohara BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE;
2561 1.29 kiyohara
2562 1.29 kiyohara bus_dmamap_sync(sc->sc_dmat, ocb->dmamap,
2563 1.29 kiyohara 0, ocb->dmamap->dm_mapsize, flag);
2564 1.29 kiyohara bus_dmamap_unload(sc->sc_dmat, ocb->dmamap);
2565 1.29 kiyohara }
2566 1.29 kiyohara if (ocb->xs != NULL) {
2567 1.29 kiyohara callout_stop(&ocb->xs->xs_callout);
2568 1.29 kiyohara ocb->xs->error = status;
2569 1.29 kiyohara scsipi_done(ocb->xs);
2570 1.1 kiyohara }
2571 1.1 kiyohara sbp_free_ocb(sdev, ocb);
2572 1.1 kiyohara }
2573 1.1 kiyohara
2574 1.1 kiyohara static void
2575 1.1 kiyohara sbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2576 1.1 kiyohara {
2577 1.29 kiyohara struct sbp_softc *sc = sdev->target->sbp;
2578 1.1 kiyohara struct sbp_ocb *ocb, *next;
2579 1.1 kiyohara STAILQ_HEAD(, sbp_ocb) temp;
2580 1.1 kiyohara
2581 1.29 kiyohara mutex_enter(&sc->sc_mtx);
2582 1.29 kiyohara STAILQ_INIT(&temp);
2583 1.29 kiyohara STAILQ_CONCAT(&temp, &sdev->ocbs);
2584 1.29 kiyohara STAILQ_INIT(&sdev->ocbs);
2585 1.29 kiyohara mutex_exit(&sc->sc_mtx);
2586 1.1 kiyohara
2587 1.1 kiyohara for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2588 1.1 kiyohara next = STAILQ_NEXT(ocb, ocb);
2589 1.1 kiyohara sbp_abort_ocb(ocb, status);
2590 1.1 kiyohara }
2591 1.1 kiyohara if (sdev->last_ocb != NULL) {
2592 1.1 kiyohara sbp_free_ocb(sdev, sdev->last_ocb);
2593 1.1 kiyohara sdev->last_ocb = NULL;
2594 1.1 kiyohara }
2595 1.1 kiyohara }
2596