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