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