sbp.c revision 111615
1103285Sikob/*
2103285Sikob * Copyright (c) 1998,1999,2000,2001 Katsushi Kobayashi and Hidetosh Shimokawa
3103285Sikob * All rights reserved.
4103285Sikob *
5103285Sikob * Redistribution and use in source and binary forms, with or without
6103285Sikob * modification, are permitted provided that the following conditions
7103285Sikob * are met:
8103285Sikob * 1. Redistributions of source code must retain the above copyright
9103285Sikob *    notice, this list of conditions and the following disclaimer.
10103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
11103285Sikob *    notice, this list of conditions and the following disclaimer in the
12103285Sikob *    documentation and/or other materials provided with the distribution.
13103285Sikob * 3. All advertising materials mentioning features or use of this software
14103285Sikob *    must display the acknowledgement as bellow:
15103285Sikob *
16103285Sikob *    This product includes software developed by K. Kobayashi and H. Shimokawa
17103285Sikob *
18103285Sikob * 4. The name of the author may not be used to endorse or promote products
19103285Sikob *    derived from this software without specific prior written permission.
20103285Sikob *
21103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24103285Sikob * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31103285Sikob * POSSIBILITY OF SUCH DAMAGE.
32103285Sikob *
33103285Sikob * $FreeBSD: head/sys/dev/firewire/sbp.c 111615 2003-02-27 12:51:24Z simokawa $
34103285Sikob *
35103285Sikob */
36103285Sikob
37103285Sikob#include <sys/param.h>
38103285Sikob#include <sys/systm.h>
39103285Sikob#include <sys/module.h>
40103285Sikob#include <sys/bus.h>
41103285Sikob#include <sys/mbuf.h>
42103285Sikob#include <sys/sysctl.h>
43103285Sikob#include <machine/bus.h>
44103285Sikob#include <sys/malloc.h>
45103285Sikob#include <sys/devicestat.h>	/* for struct devstat */
46103285Sikob
47103285Sikob
48103285Sikob#include <cam/cam.h>
49103285Sikob#include <cam/cam_ccb.h>
50103285Sikob#include <cam/cam_sim.h>
51103285Sikob#include <cam/cam_xpt_sim.h>
52103285Sikob#include <cam/cam_debug.h>
53103285Sikob#include <cam/cam_periph.h>
54103285Sikob
55103285Sikob#include <cam/scsi/scsi_all.h>
56103285Sikob#include <cam/scsi/scsi_message.h>
57103285Sikob#include <cam/scsi/scsi_da.h>
58103285Sikob
59103285Sikob#include <sys/kernel.h>
60103285Sikob
61103285Sikob#include <vm/vm.h>
62103285Sikob#include <vm/pmap.h>
63103285Sikob
64103285Sikob#include <dev/firewire/firewire.h>
65103285Sikob#include <dev/firewire/firewirereg.h>
66103285Sikob#include <dev/firewire/iec13213.h>
67103285Sikob
68103285Sikob#define ccb_sdev_ptr	spriv_ptr0
69103285Sikob#define ccb_sbp_ptr	spriv_ptr1
70103285Sikob
71111615Ssimokawa#define SBP_NUM_TARGETS 8 /* MAX 64 */
72103285Sikob#define SBP_NUM_LUNS 8	/* limited by CAM_SCSI2_MAXLUN in cam_xpt.c */
73103285Sikob#define SBP_QUEUE_LEN 4
74103285Sikob#define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS)
75103285Sikob#define SBP_INITIATOR 7
76111615Ssimokawa
77111615Ssimokawa#define LOGIN_DELAY 2
78111615Ssimokawa
79111615Ssimokawa/*
80111615Ssimokawa * STATUS FIFO addressing
81111615Ssimokawa *   bit
82111615Ssimokawa * -----------------------
83111615Ssimokawa *  0- 1( 2): 0 (alingment)
84111615Ssimokawa *  2- 7( 6): target
85111615Ssimokawa *  8-15( 8): lun
86111615Ssimokawa * 16-23( 8): unit
87111615Ssimokawa * 24-31( 8): reserved
88111615Ssimokawa * 32-47(16): SBP_BIND_HI
89111615Ssimokawa * 48-64(16): bus_id, node_id
90111615Ssimokawa */
91103285Sikob#define SBP_BIND_HI 0x1
92111615Ssimokawa#define SBP_DEV2ADDR(u, t, l) \
93103285Sikob	((((u) & 0xff) << 16) | (((l) & 0xff) << 8) | (((t) & 0x3f) << 2))
94103285Sikob#define SBP_ADDR2TRG(a)	(((a) >> 2) & 0x3f)
95103285Sikob#define SBP_ADDR2LUN(a)	(((a) >> 8) & 0xff)
96103285Sikob
97103285Sikob#define ORB_NOTIFY	(1 << 31)
98103285Sikob#define	ORB_FMT_STD	(0 << 29)
99103285Sikob#define	ORB_FMT_VED	(2 << 29)
100103285Sikob#define	ORB_FMT_NOP	(3 << 29)
101103285Sikob#define	ORB_FMT_MSK	(3 << 29)
102103285Sikob#define	ORB_EXV		(1 << 28)
103103285Sikob/* */
104103285Sikob#define	ORB_CMD_IN	(1 << 27)
105103285Sikob/* */
106103285Sikob#define	ORB_CMD_SPD(x)	((x) << 24)
107103285Sikob#define	ORB_CMD_MAXP(x)	((x) << 20)
108103285Sikob#define	ORB_RCN_TMO(x)	((x) << 20)
109103285Sikob#define	ORB_CMD_PTBL	(1 << 19)
110103285Sikob#define	ORB_CMD_PSZ(x)	((x) << 16)
111103285Sikob
112103285Sikob#define	ORB_FUN_LGI	(0 << 16)
113103285Sikob#define	ORB_FUN_QLG	(1 << 16)
114103285Sikob#define	ORB_FUN_RCN	(3 << 16)
115103285Sikob#define	ORB_FUN_LGO	(7 << 16)
116103285Sikob#define	ORB_FUN_ATA	(0xb << 16)
117103285Sikob#define	ORB_FUN_ATS	(0xc << 16)
118103285Sikob#define	ORB_FUN_LUR	(0xe << 16)
119103285Sikob#define	ORB_FUN_RST	(0xf << 16)
120103285Sikob#define	ORB_FUN_MSK	(0xf << 16)
121111615Ssimokawa#define	ORB_FUN_RUNQUEUE 0xffff
122103285Sikob
123103285Sikobstatic char *orb_fun_name[] = {
124103285Sikob	/* 0 */ "LOGIN",
125103285Sikob	/* 1 */ "QUERY LOGINS",
126103285Sikob	/* 2 */ "Reserved",
127103285Sikob	/* 3 */ "RECONNECT",
128103285Sikob	/* 4 */ "SET PASSWORD",
129103285Sikob	/* 5 */ "Reserved",
130103285Sikob	/* 6 */ "Reserved",
131103285Sikob	/* 7 */ "LOGOUT",
132103285Sikob	/* 8 */ "Reserved",
133103285Sikob	/* 9 */ "Reserved",
134103285Sikob	/* A */ "Reserved",
135103285Sikob	/* B */ "ABORT TASK",
136103285Sikob	/* C */ "ABORT TASK SET",
137103285Sikob	/* D */ "Reserved",
138103285Sikob	/* E */ "LOGICAL UNIT RESET",
139103285Sikob	/* F */ "TARGET RESET"
140103285Sikob};
141103285Sikob
142103285Sikob#define ORB_RES_CMPL 0
143103285Sikob#define ORB_RES_FAIL 1
144103285Sikob#define ORB_RES_ILLE 2
145103285Sikob#define ORB_RES_VEND 3
146103285Sikob
147111203Ssimokawastatic int debug = 0;
148103285Sikobstatic int auto_login = 1;
149103285Sikobstatic int max_speed = 2;
150111199Ssimokawastatic int sbp_cold = 1;
151103285Sikob
152103285SikobSYSCTL_DECL(_hw_firewire);
153103285SikobSYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem");
154103285SikobSYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0,
155103285Sikob	"SBP debug flag");
156103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0,
157103285Sikob	"SBP perform login automatically");
158103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0,
159103285Sikob	"SBP transfer max speed");
160103285Sikob
161103285Sikob#define SBP_DEBUG(x)	if (debug > x) {
162103285Sikob#define END_DEBUG	}
163103285Sikob
164103285Sikob#define NEED_RESPONSE 0
165103285Sikob
166103285Sikobstruct ind_ptr {
167103285Sikob	u_int32_t hi,lo;
168103285Sikob};
169103285Sikob#define SBP_IND_MAX 0x20
170103285Sikobstruct sbp_ocb {
171103285Sikob	STAILQ_ENTRY(sbp_ocb)	ocb;
172103285Sikob	union ccb	*ccb;
173103285Sikob	volatile u_int32_t	orb[8];
174103285Sikob	volatile struct ind_ptr  ind_ptr[SBP_IND_MAX];
175103285Sikob	struct sbp_dev	*sdev;
176103285Sikob	int		flags;
177103285Sikob	bus_dmamap_t	dmamap;
178103285Sikob};
179103285Sikob#define OCB_ACT_MGM 0
180103285Sikob#define OCB_ACT_CMD 1
181103285Sikob#define OCB_ACT_MASK 3
182103285Sikob#define OCB_RESERVED 0x10
183103285Sikob#define OCB_DONE 0x20
184111615Ssimokawa#define OCB_MATCH(o,s)	(vtophys(&(o)->orb[0]) == ntohl((s)->orb_lo))
185103285Sikob
186103285Sikob
187103285Sikobstruct sbp_login_res{
188103285Sikob	u_int16_t	len;
189103285Sikob	u_int16_t	id;
190103285Sikob	u_int16_t	res0;
191103285Sikob	u_int16_t	cmd_hi;
192103285Sikob	u_int32_t	cmd_lo;
193103285Sikob	u_int16_t	res1;
194103285Sikob	u_int16_t	recon_hold;
195103285Sikob};
196103285Sikobstruct sbp_status{
197103285Sikob	u_int8_t	len:3,
198103285Sikob			dead:1,
199103285Sikob			resp:2,
200103285Sikob			src:2;
201103285Sikob	u_int8_t	status:8;
202103285Sikob	u_int16_t	orb_hi;
203103285Sikob	u_int32_t	orb_lo;
204103285Sikob	u_int32_t	data[6];
205103285Sikob};
206103285Sikobstruct sbp_cmd_status{
207103285Sikob#define SBP_SFMT_CURR 0
208103285Sikob#define SBP_SFMT_DEFER 1
209103285Sikob	u_int8_t	status:6,
210103285Sikob			sfmt:2;
211103285Sikob	u_int8_t	s_key:4,
212103285Sikob			ill_len:1,
213103285Sikob			eom:1,
214103285Sikob			mark:1,
215103285Sikob			valid:1;
216103285Sikob	u_int8_t	s_code;
217103285Sikob	u_int8_t	s_qlfr;
218103285Sikob	u_int32_t	info;
219103285Sikob	u_int32_t	cdb;
220103285Sikob	u_int32_t	fru:8,
221103285Sikob			s_keydep:24;
222103285Sikob	u_int32_t	vend[2];
223103285Sikob};
224103285Sikob
225103285Sikobstruct sbp_dev{
226103285Sikob#define SBP_DEV_RESET		0	/* accept login */
227110336Ssimokawa#if 0
228103285Sikob#define SBP_DEV_LOGIN		1	/* to login */
229103285Sikob#define SBP_DEV_RECONN		2	/* to reconnect */
230110336Ssimokawa#endif
231103285Sikob#define SBP_DEV_TOATTACH	3	/* to attach */
232103285Sikob#define SBP_DEV_PROBE		4	/* scan lun */
233103285Sikob#define SBP_DEV_ATTACHED	5	/* in operation */
234103285Sikob#define SBP_DEV_DEAD		6	/* unavailable unit */
235103285Sikob#define SBP_DEV_RETRY		7	/* unavailable unit */
236110336Ssimokawa	u_int8_t status:4,
237110336Ssimokawa#define SBP_DEV_TIMEOUT		1
238110336Ssimokawa		 flags:4;
239108503Ssimokawa	u_int8_t type;
240108503Ssimokawa	u_int16_t lun_id;
241111615Ssimokawa	int freeze;
242103285Sikob	struct cam_path *path;
243103285Sikob	struct sbp_target *target;
244103285Sikob	struct sbp_login_res login;
245111615Ssimokawa	struct callout login_callout;
246103285Sikob	STAILQ_HEAD(, sbp_ocb) ocbs;
247103285Sikob	char vendor[32];
248103285Sikob	char product[32];
249103285Sikob	char revision[10];
250103285Sikob};
251103285Sikob
252103285Sikobstruct sbp_target {
253103285Sikob	int target_id;
254103285Sikob	int num_lun;
255103285Sikob	struct sbp_dev	*luns;
256103285Sikob	struct sbp_softc *sbp;
257103285Sikob	struct fw_device *fwdev;
258103285Sikob	u_int32_t mgm_hi, mgm_lo;
259111615Ssimokawa	struct sbp_ocb *mgm_ocb_cur;
260111615Ssimokawa	STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue;
261111615Ssimokawa	struct callout mgm_ocb_timeout;
262111615Ssimokawa#define SCAN_DELAY 2
263111615Ssimokawa	struct callout scan_callout;
264103285Sikob};
265103285Sikob
266103285Sikobstruct sbp_softc {
267103285Sikob	struct firewire_dev_comm fd;
268103285Sikob	struct cam_sim  *sim;
269111615Ssimokawa	struct cam_path  *path;
270103285Sikob	struct sbp_target targets[SBP_NUM_TARGETS];
271103285Sikob	struct fw_bind fwb;
272103285Sikob	STAILQ_HEAD(, sbp_ocb) free_ocbs;
273103285Sikob	struct sbp_ocb *ocb;
274103285Sikob	bus_dma_tag_t	dmat;
275111615Ssimokawa#define SBP_RESOURCE_SHORTAGE 0x10
276111615Ssimokawa	unsigned char flags;
277103285Sikob};
278103285Sikobstatic void sbp_post_explore __P((void *));
279103285Sikobstatic void sbp_recv __P((struct fw_xfer *));
280103285Sikobstatic void sbp_login_callback __P((struct fw_xfer *));
281103285Sikobstatic void sbp_cmd_callback __P((struct fw_xfer *));
282103285Sikobstatic void sbp_orb_pointer __P((struct sbp_dev *, struct sbp_ocb *));
283103285Sikobstatic void sbp_execute_ocb __P((void *,  bus_dma_segment_t *, int, int));
284103285Sikobstatic void sbp_free_ocb __P((struct sbp_softc *, struct sbp_ocb *));
285103285Sikobstatic void sbp_abort_ocb __P((struct sbp_ocb *, int));
286103285Sikobstatic void sbp_abort_all_ocbs __P((struct sbp_dev *, int));
287103285Sikobstatic struct fw_xfer * sbp_write_cmd __P((struct sbp_dev *, int, int));
288103285Sikobstatic struct sbp_ocb * sbp_get_ocb __P((struct sbp_softc *));
289103285Sikobstatic struct sbp_ocb * sbp_enqueue_ocb __P((struct sbp_dev *, struct sbp_ocb *));
290111615Ssimokawastatic struct sbp_ocb * sbp_dequeue_ocb __P((struct sbp_dev *, struct sbp_status *));
291110145Ssimokawastatic void sbp_cam_detach_target __P((struct sbp_target *));
292103285Sikobstatic void sbp_timeout __P((void *arg));
293111615Ssimokawastatic void sbp_mgm_orb __P((struct sbp_dev *, int, struct sbp_ocb *));
294111615Ssimokawa#define sbp_login(sdev) \
295111615Ssimokawa	callout_reset(&(sdev)->login_callout, LOGIN_DELAY * hz, \
296111615Ssimokawa			sbp_login_callout, (void *)(sdev));
297103285Sikob
298108281SsimokawaMALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire");
299103285Sikob
300103285Sikob/* cam related functions */
301103285Sikobstatic void	sbp_action(struct cam_sim *sim, union ccb *ccb);
302103285Sikobstatic void	sbp_poll(struct cam_sim *sim);
303111615Ssimokawastatic void	sbp_cam_scan_lun(struct cam_periph *, union ccb *);
304111615Ssimokawastatic void	sbp_cam_scan_target(void *arg);
305103285Sikob
306103285Sikobstatic char *orb_status0[] = {
307103285Sikob	/* 0 */ "No additional information to report",
308103285Sikob	/* 1 */ "Request type not supported",
309103285Sikob	/* 2 */ "Speed not supported",
310103285Sikob	/* 3 */ "Page size not supported",
311103285Sikob	/* 4 */ "Access denied",
312103285Sikob	/* 5 */ "Logical unit not supported",
313103285Sikob	/* 6 */ "Maximum payload too small",
314103285Sikob	/* 7 */ "Reserved for future standardization",
315103285Sikob	/* 8 */ "Resources unavailable",
316103285Sikob	/* 9 */ "Function rejected",
317103285Sikob	/* A */ "Login ID not recognized",
318103285Sikob	/* B */ "Dummy ORB completed",
319103285Sikob	/* C */ "Request aborted",
320103285Sikob	/* FF */ "Unspecified error"
321103285Sikob#define MAX_ORB_STATUS0 0xd
322103285Sikob};
323103285Sikob
324103285Sikobstatic char *orb_status1_object[] = {
325103285Sikob	/* 0 */ "Operation request block (ORB)",
326103285Sikob	/* 1 */ "Data buffer",
327103285Sikob	/* 2 */ "Page table",
328103285Sikob	/* 3 */ "Unable to specify"
329103285Sikob};
330103285Sikob
331103285Sikobstatic char *orb_status1_serial_bus_error[] = {
332103285Sikob	/* 0 */ "Missing acknowledge",
333103285Sikob	/* 1 */ "Reserved; not to be used",
334103285Sikob	/* 2 */ "Time-out error",
335103285Sikob	/* 3 */ "Reserved; not to be used",
336103285Sikob	/* 4 */ "Busy retry limit exceeded(X)",
337103285Sikob	/* 5 */ "Busy retry limit exceeded(A)",
338103285Sikob	/* 6 */ "Busy retry limit exceeded(B)",
339103285Sikob	/* 7 */ "Reserved for future standardization",
340103285Sikob	/* 8 */ "Reserved for future standardization",
341103285Sikob	/* 9 */ "Reserved for future standardization",
342103285Sikob	/* A */ "Reserved for future standardization",
343103285Sikob	/* B */ "Tardy retry limit exceeded",
344103285Sikob	/* C */ "Conflict error",
345103285Sikob	/* D */ "Data error",
346103285Sikob	/* E */ "Type error",
347103285Sikob	/* F */ "Address error"
348103285Sikob};
349103285Sikob
350103285Sikobstatic void
351103285Sikobsbp_identify(driver_t *driver, device_t parent)
352103285Sikob{
353103285Sikob	device_t child;
354103285SikobSBP_DEBUG(0)
355103285Sikob	printf("sbp_identify\n");
356103285SikobEND_DEBUG
357103285Sikob
358103285Sikob	child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent));
359103285Sikob}
360103285Sikob
361103285Sikob/*
362103285Sikob * sbp_probe()
363103285Sikob */
364103285Sikobstatic int
365103285Sikobsbp_probe(device_t dev)
366103285Sikob{
367103285Sikob	device_t pa;
368103285Sikob
369103285SikobSBP_DEBUG(0)
370103285Sikob	printf("sbp_probe\n");
371103285SikobEND_DEBUG
372103285Sikob
373103285Sikob	pa = device_get_parent(dev);
374103285Sikob	if(device_get_unit(dev) != device_get_unit(pa)){
375103285Sikob		return(ENXIO);
376103285Sikob	}
377103285Sikob
378103285Sikob	device_set_desc(dev, "SBP2/SCSI over firewire");
379111615Ssimokawa
380111615Ssimokawa	if (bootverbose)
381111615Ssimokawa		debug = bootverbose;
382103285Sikob	return (0);
383103285Sikob}
384103285Sikob
385103285Sikobstatic void
386103285Sikobsbp_show_sdev_info(struct sbp_dev *sdev, int new)
387103285Sikob{
388103285Sikob	struct fw_device *fwdev;
389103285Sikob
390103285Sikob	printf("%s:%d:%d ",
391103285Sikob		device_get_nameunit(sdev->target->sbp->fd.dev),
392103285Sikob		sdev->target->target_id,
393103285Sikob		sdev->lun_id
394103285Sikob	);
395103285Sikob	if (new == 2) {
396103285Sikob		return;
397103285Sikob	}
398103285Sikob	fwdev = sdev->target->fwdev;
399103285Sikob	printf("ordered:%d type:%d EUI:%08x%08x node:%d "
400103285Sikob		"speed:%d maxrec:%d",
401108503Ssimokawa		(sdev->type & 0x40) >> 6,
402108503Ssimokawa		(sdev->type & 0x1f),
403103285Sikob		fwdev->eui.hi,
404103285Sikob		fwdev->eui.lo,
405103285Sikob		fwdev->dst,
406103285Sikob		fwdev->speed,
407103285Sikob		fwdev->maxrec
408103285Sikob	);
409103285Sikob	if (new)
410103285Sikob		printf(" new!\n");
411103285Sikob	else
412103285Sikob		printf("\n");
413103285Sikob	sbp_show_sdev_info(sdev, 2);
414103285Sikob	printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision);
415103285Sikob}
416103285Sikob
417110184Ssimokawastatic struct {
418110184Ssimokawa	int bus;
419110184Ssimokawa	int target;
420110184Ssimokawa	struct fw_eui64 eui;
421110184Ssimokawa} wired[] = {
422110184Ssimokawa	/* Bus	Target	EUI64 */
423110184Ssimokawa#if 0
424110184Ssimokawa	{0,	2,	{0x00018ea0, 0x01fd0154}},	/* Logitec HDD */
425110184Ssimokawa	{0,	0,	{0x00018ea6, 0x00100682}},	/* Logitec DVD */
426110184Ssimokawa	{0,	1,	{0x00d03200, 0xa412006a}},	/* Yano HDD */
427110184Ssimokawa#endif
428110184Ssimokawa	{-1,	-1,	{0,0}}
429110184Ssimokawa};
430110184Ssimokawa
431110184Ssimokawastatic int
432110184Ssimokawasbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev)
433110184Ssimokawa{
434110184Ssimokawa	int bus, i, target=-1;
435110184Ssimokawa	char w[SBP_NUM_TARGETS];
436110184Ssimokawa
437110184Ssimokawa	bzero(w, sizeof(w));
438110184Ssimokawa	bus = device_get_unit(sbp->fd.dev);
439110184Ssimokawa
440110184Ssimokawa	/* XXX wired-down configuration should be gotten from
441110184Ssimokawa					tunable or device hint */
442110184Ssimokawa	for (i = 0; wired[i].bus >= 0; i ++) {
443110184Ssimokawa		if (wired[i].bus == bus) {
444110184Ssimokawa			w[wired[i].target] = 1;
445110184Ssimokawa			if (wired[i].eui.hi == fwdev->eui.hi &&
446110184Ssimokawa					wired[i].eui.lo == fwdev->eui.lo)
447110184Ssimokawa				target = wired[i].target;
448110184Ssimokawa		}
449110184Ssimokawa	}
450110184Ssimokawa	if (target >= 0) {
451110184Ssimokawa		if(target < SBP_NUM_TARGETS &&
452110184Ssimokawa				sbp->targets[target].fwdev == NULL)
453110184Ssimokawa			return(target);
454110184Ssimokawa		device_printf(sbp->fd.dev,
455110184Ssimokawa			"target %d is not free for %08x:%08x\n",
456110184Ssimokawa			target, fwdev->eui.hi, fwdev->eui.lo);
457110184Ssimokawa		target = -1;
458110184Ssimokawa	}
459110184Ssimokawa	/* non-wired target */
460110184Ssimokawa	for (i = 0; i < SBP_NUM_TARGETS; i ++)
461110184Ssimokawa		if (sbp->targets[i].fwdev == NULL && w[i] == 0) {
462110184Ssimokawa			target = i;
463110184Ssimokawa			break;
464110184Ssimokawa		}
465110184Ssimokawa
466110184Ssimokawa	return target;
467110184Ssimokawa}
468110184Ssimokawa
469103285Sikobstatic struct sbp_target *
470103285Sikobsbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
471103285Sikob{
472108503Ssimokawa	int i, maxlun, lun;
473103285Sikob	struct sbp_target *target;
474103285Sikob	struct sbp_dev *sdev;
475108503Ssimokawa	struct crom_context cc;
476108503Ssimokawa	struct csrreg *reg;
477103285Sikob
478103285SikobSBP_DEBUG(1)
479103285Sikob	printf("sbp_alloc_target\n");
480103285SikobEND_DEBUG
481110184Ssimokawa	i = sbp_new_target(sbp, fwdev);
482110184Ssimokawa	if (i < 0) {
483110184Ssimokawa		device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n");
484103285Sikob		return NULL;
485103285Sikob	}
486103285Sikob	/* new target */
487103285Sikob	target = &sbp->targets[i];
488103285Sikob	target->sbp = sbp;
489103285Sikob	target->fwdev = fwdev;
490103285Sikob	target->target_id = i;
491103285Sikob	if((target->mgm_lo = getcsrdata(fwdev, 0x54)) == 0 ){
492103285Sikob		/* bad target */
493103285Sikob		printf("NULL management address\n");
494103285Sikob		target->fwdev = NULL;
495103285Sikob		return NULL;
496103285Sikob	}
497103285Sikob	target->mgm_hi = 0xffff;
498103285Sikob	target->mgm_lo = 0xf0000000 | target->mgm_lo << 2;
499111615Ssimokawa	target->mgm_ocb_cur = NULL;
500111615Ssimokawa	STAILQ_INIT(&target->mgm_ocb_queue);
501111615Ssimokawa	CALLOUT_INIT(&target->mgm_ocb_timeout);
502111615Ssimokawa	CALLOUT_INIT(&target->scan_callout);
503111615Ssimokawa
504103285Sikob	/* XXX num_lun may be changed. realloc luns? */
505108503Ssimokawa	crom_init_context(&cc, target->fwdev->csrrom);
506108503Ssimokawa	/* XXX shoud parse appropriate unit directories only */
507108503Ssimokawa	maxlun = -1;
508108503Ssimokawa	while (cc.depth >= 0) {
509108503Ssimokawa		reg = crom_search_key(&cc, CROM_LUN);
510108503Ssimokawa		if (reg == NULL)
511108503Ssimokawa			break;
512110839Ssimokawa		lun = reg->val & 0xffff;
513108642SsimokawaSBP_DEBUG(0)
514108642Ssimokawa		printf("target %d lun %d found\n", target->target_id, lun);
515108642SsimokawaEND_DEBUG
516108503Ssimokawa		if (maxlun < lun)
517108503Ssimokawa			maxlun = lun;
518108503Ssimokawa		crom_next(&cc);
519108503Ssimokawa	}
520110839Ssimokawa	if (maxlun < 0)
521110839Ssimokawa		printf("no lun found!\n");
522110839Ssimokawa	if (maxlun >= SBP_NUM_LUNS)
523110839Ssimokawa		maxlun = SBP_NUM_LUNS;
524108503Ssimokawa	target->num_lun = maxlun + 1;
525103285Sikob	target->luns = (struct sbp_dev *) malloc(
526103285Sikob				sizeof(struct sbp_dev) * target->num_lun,
527103285Sikob				M_SBP, M_NOWAIT | M_ZERO);
528103285Sikob	for (i = 0; i < target->num_lun; i++) {
529103285Sikob		sdev = &target->luns[i];
530103285Sikob		sdev->lun_id = i;
531103285Sikob		sdev->target = target;
532103285Sikob		STAILQ_INIT(&sdev->ocbs);
533111615Ssimokawa		CALLOUT_INIT(&sdev->login_callout);
534108503Ssimokawa		sdev->status = SBP_DEV_DEAD;
535103285Sikob	}
536108503Ssimokawa	crom_init_context(&cc, target->fwdev->csrrom);
537108503Ssimokawa	while (cc.depth >= 0) {
538108503Ssimokawa		reg = crom_search_key(&cc, CROM_LUN);
539108503Ssimokawa		if (reg == NULL)
540108503Ssimokawa			break;
541110839Ssimokawa		lun = reg->val & 0xffff;
542110839Ssimokawa		if (lun >= SBP_NUM_LUNS) {
543110839Ssimokawa			printf("too large lun %d\n", lun);
544110839Ssimokawa			continue;
545110839Ssimokawa		}
546108503Ssimokawa		target->luns[lun].status = SBP_DEV_RESET;
547110839Ssimokawa		target->luns[lun].type = (reg->val & 0xf0000) >> 16;
548108503Ssimokawa		crom_next(&cc);
549110839Ssimokawa	    }
550110839Ssimokawa	    return target;
551110839Ssimokawa    }
552103285Sikob
553103285Sikobstatic void
554103285Sikobsbp_get_text_leaf(struct fw_device *fwdev, int key, char *buf, int len)
555103285Sikob{
556103285Sikob	static char *nullstr = "(null)";
557103285Sikob	int i, clen, found=0;
558103285Sikob	struct csrhdr *chdr;
559103285Sikob	struct csrreg *creg;
560103285Sikob	u_int32_t *src, *dst;
561103285Sikob
562103285Sikob	chdr = (struct csrhdr *)&fwdev->csrrom[0];
563110839Ssimokawa	/* skip crom header, bus info and root directory */
564110839Ssimokawa	creg = (struct csrreg *)chdr + chdr->info_len + 2;
565110839Ssimokawa	/* search unitl the one before the last. */
566110839Ssimokawa	for (i = chdr->info_len + 2; i < fwdev->rommax / 4; i++) {
567103285Sikob		if((creg++)->key == key){
568103285Sikob			found = 1;
569103285Sikob			break;
570103285Sikob		}
571103285Sikob	}
572110798Ssimokawa	if (!found || creg->key != CROM_TEXTLEAF) {
573103285Sikob		strncpy(buf, nullstr, len);
574103285Sikob		return;
575103285Sikob	}
576103285Sikob	src = (u_int32_t *) creg + creg->val;
577103285Sikob	clen = ((*src >> 16) - 2) * 4;
578103285Sikob	src += 3;
579103285Sikob	dst = (u_int32_t *) buf;
580103285Sikob	if (len < clen)
581103285Sikob		clen = len;
582103285Sikob	for (i = 0; i < clen/4; i++)
583103285Sikob		*dst++ = htonl(*src++);
584103285Sikob	buf[clen] = 0;
585103285Sikob}
586103285Sikob
587103285Sikobstatic void
588103285Sikobsbp_probe_lun(struct sbp_dev *sdev)
589103285Sikob{
590103285Sikob	struct fw_device *fwdev;
591103285Sikob	int rev;
592103285Sikob
593103285Sikob	fwdev = sdev->target->fwdev;
594103285Sikob	bzero(sdev->vendor, sizeof(sdev->vendor));
595103285Sikob	bzero(sdev->product, sizeof(sdev->product));
596103285Sikob	sbp_get_text_leaf(fwdev, 0x03, sdev->vendor, sizeof(sdev->vendor));
597103285Sikob	sbp_get_text_leaf(fwdev, 0x17, sdev->product, sizeof(sdev->product));
598103285Sikob	rev = getcsrdata(sdev->target->fwdev, 0x3c);
599103285Sikob	snprintf(sdev->revision, sizeof(sdev->revision), "%06x", rev);
600103285Sikob}
601111615Ssimokawa
602111615Ssimokawa
603103285Sikobstatic void
604111615Ssimokawasbp_login_callout(void *arg)
605103285Sikob{
606111615Ssimokawa	struct sbp_dev *sdev = (struct sbp_dev *)arg;
607111615Ssimokawa	sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
608111615Ssimokawa}
609111615Ssimokawa
610111615Ssimokawa#define SBP_FWDEV_ALIVE(fwdev) \
611111615Ssimokawa	((fwdev->status == FWDEVATTACHED) \
612111615Ssimokawa		&& (getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10) \
613111615Ssimokawa		&& (getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2))
614111615Ssimokawa
615111615Ssimokawastatic void
616111615Ssimokawasbp_probe_target(void *arg)
617111615Ssimokawa{
618111615Ssimokawa	struct sbp_target *target = (struct sbp_target *)arg;
619103285Sikob	struct sbp_softc *sbp;
620103285Sikob	struct sbp_dev *sdev;
621103285Sikob	struct firewire_comm *fc;
622111615Ssimokawa	int i, alive;
623103285Sikob
624111615Ssimokawa	alive = SBP_FWDEV_ALIVE(target->fwdev);
625103285SikobSBP_DEBUG(1)
626103285Sikob	printf("sbp_probe_target %d\n", target->target_id);
627103285Sikob	if (!alive)
628103285Sikob		printf("not alive\n");
629103285SikobEND_DEBUG
630103285Sikob
631103285Sikob	sbp = target->sbp;
632103285Sikob	fc = target->sbp->fd.fc;
633111615Ssimokawa	/* XXX untimeout mgm_ocb and dequeue */
634103285Sikob	for (i=0; i < target->num_lun; i++) {
635103285Sikob		sdev = &target->luns[i];
636103285Sikob		if (alive && (sdev->status != SBP_DEV_DEAD)) {
637103285Sikob			if (sdev->path != NULL) {
638103285Sikob				xpt_freeze_devq(sdev->path, 1);
639111615Ssimokawa				sdev->freeze ++;
640103285Sikob			}
641111615Ssimokawa			sbp_probe_lun(sdev);
642111615SsimokawaSBP_DEBUG(0)
643111615Ssimokawa			sbp_show_sdev_info(sdev,
644111615Ssimokawa#if 0
645111615Ssimokawa					(sdev->status == SBP_DEV_TOATTACH));
646111615Ssimokawa#else
647111615Ssimokawa					(sdev->status == SBP_DEV_RESET));
648111615Ssimokawa#endif
649111615SsimokawaEND_DEBUG
650111615Ssimokawa
651111615Ssimokawa			sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
652103285Sikob			switch (sdev->status) {
653110336Ssimokawa			case SBP_DEV_RESET:
654103285Sikob				/* new or revived target */
655103285Sikob				if (auto_login) {
656111615Ssimokawa#if 0
657103285Sikob					sdev->status = SBP_DEV_TOATTACH;
658111615Ssimokawa#endif
659111615Ssimokawa					sbp_login(sdev);
660103285Sikob				}
661103285Sikob				break;
662111615Ssimokawa			case SBP_DEV_TOATTACH:
663111615Ssimokawa			case SBP_DEV_PROBE:
664111615Ssimokawa			case SBP_DEV_ATTACHED:
665110336Ssimokawa			case SBP_DEV_RETRY:
666110336Ssimokawa			default:
667111615Ssimokawa				sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
668110336Ssimokawa				break;
669103285Sikob			}
670103285Sikob		} else {
671103285Sikob			switch (sdev->status) {
672103285Sikob			case SBP_DEV_ATTACHED:
673103285SikobSBP_DEBUG(0)
674103285Sikob				/* the device has gone */
675103285Sikob				sbp_show_sdev_info(sdev, 2);
676103285Sikob				printf("lost target\n");
677103285SikobEND_DEBUG
678111615Ssimokawa				if (sdev->path) {
679103285Sikob					xpt_freeze_devq(sdev->path, 1);
680111615Ssimokawa					sdev->freeze ++;
681111615Ssimokawa				}
682103285Sikob				sdev->status = SBP_DEV_RETRY;
683111615Ssimokawa				sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
684103285Sikob				break;
685103285Sikob			case SBP_DEV_PROBE:
686103285Sikob			case SBP_DEV_TOATTACH:
687103285Sikob				sdev->status = SBP_DEV_RESET;
688103285Sikob				break;
689103285Sikob			case SBP_DEV_RETRY:
690103285Sikob			case SBP_DEV_RESET:
691103285Sikob			case SBP_DEV_DEAD:
692103285Sikob				break;
693103285Sikob			}
694103285Sikob		}
695103285Sikob	}
696103285Sikob}
697103285Sikob
698103285Sikobstatic void
699103285Sikobsbp_post_explore(void *arg)
700103285Sikob{
701103285Sikob	struct sbp_softc *sbp = (struct sbp_softc *)arg;
702103285Sikob	struct sbp_target *target;
703103285Sikob	struct fw_device *fwdev;
704103285Sikob	int i, alive;
705103285Sikob
706111199SsimokawaSBP_DEBUG(0)
707111199Ssimokawa	printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
708103285SikobEND_DEBUG
709111615Ssimokawa#if 0	/*
710111615Ssimokawa	 * XXX don't let CAM the bus rest. CAM tries to do something with
711111615Ssimokawa	 * freezed (DEV_RETRY) devices
712111615Ssimokawa	 */
713111615Ssimokawa	xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
714103285Sikob#endif
715111199Ssimokawa	if (sbp_cold > 0)
716111199Ssimokawa		sbp_cold --;
717111615Ssimokawa
718103285Sikob	/* Gabage Collection */
719103285Sikob	for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
720103285Sikob		target = &sbp->targets[i];
721110193Ssimokawa		STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
722110193Ssimokawa			if (target->fwdev == NULL || target->fwdev == fwdev)
723110193Ssimokawa				break;
724103285Sikob		if(fwdev == NULL){
725103285Sikob			/* device has removed in lower driver */
726110145Ssimokawa			sbp_cam_detach_target(target);
727110145Ssimokawa			if (target->luns != NULL)
728110145Ssimokawa				free(target->luns, M_SBP);
729110145Ssimokawa			target->num_lun = 0;;
730110145Ssimokawa			target->luns = NULL;
731110145Ssimokawa			target->fwdev = NULL;
732103285Sikob		}
733103285Sikob	}
734103285Sikob	/* traverse device list */
735110193Ssimokawa	STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
736103285SikobSBP_DEBUG(0)
737103285Sikob		printf("sbp_post_explore: EUI:%08x%08x ",
738103285Sikob				fwdev->eui.hi, fwdev->eui.lo);
739103285Sikob		if (fwdev->status == FWDEVATTACHED) {
740103285Sikob			printf("spec=%d key=%d.\n",
741103285Sikob			getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10,
742103285Sikob			getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2);
743103285Sikob		} else {
744103285Sikob			printf("not attached, state=%d.\n", fwdev->status);
745103285Sikob		}
746103285SikobEND_DEBUG
747111615Ssimokawa		alive = SBP_FWDEV_ALIVE(fwdev);
748103285Sikob		for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
749103285Sikob			target = &sbp->targets[i];
750103285Sikob			if(target->fwdev == fwdev ) {
751103285Sikob				/* known target */
752103285Sikob				break;
753103285Sikob			}
754103285Sikob		}
755103285Sikob		if(i == SBP_NUM_TARGETS){
756103285Sikob			if (alive) {
757103285Sikob				/* new target */
758103285Sikob				target = sbp_alloc_target(sbp, fwdev);
759103285Sikob				if (target == NULL)
760103285Sikob					continue;
761103285Sikob			} else {
762103285Sikob				continue;
763103285Sikob			}
764103285Sikob		}
765111615Ssimokawa		sbp_probe_target((void *)target);
766103285Sikob	}
767103285Sikob#if 0
768103285Sikob	timeout(sbp_release_queue, (caddr_t)sbp, bus_reset_rest * hz / 1000);
769103285Sikob#endif
770103285Sikob}
771103285Sikob
772103285Sikob#if NEED_RESPONSE
773103285Sikobstatic void
774103285Sikobsbp_loginres_callback(struct fw_xfer *xfer){
775103285SikobSBP_DEBUG(1)
776103285Sikob	struct sbp_dev *sdev;
777103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
778103285Sikob	sbp_show_sdev_info(sdev, 2);
779103285Sikob	printf("sbp_loginres_callback\n");
780103285SikobEND_DEBUG
781103285Sikob	fw_xfer_free(xfer);
782103285Sikob	return;
783103285Sikob}
784103285Sikob#endif
785103285Sikob
786103285Sikobstatic void
787103285Sikobsbp_login_callback(struct fw_xfer *xfer)
788103285Sikob{
789103285SikobSBP_DEBUG(1)
790103285Sikob	struct sbp_dev *sdev;
791103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
792103285Sikob	sbp_show_sdev_info(sdev, 2);
793103285Sikob	printf("sbp_login_callback\n");
794103285SikobEND_DEBUG
795103285Sikob	fw_xfer_free(xfer);
796103285Sikob	return;
797103285Sikob}
798103285Sikob
799103285Sikobstatic void
800103285Sikobsbp_cmd_callback(struct fw_xfer *xfer)
801103285Sikob{
802103285SikobSBP_DEBUG(2)
803103285Sikob	struct sbp_dev *sdev;
804103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
805103285Sikob	sbp_show_sdev_info(sdev, 2);
806103285Sikob	printf("sbp_cmd_callback\n");
807103285SikobEND_DEBUG
808103285Sikob	fw_xfer_free(xfer);
809103285Sikob	return;
810103285Sikob}
811103285Sikob
812111615Ssimokawastatic struct sbp_dev *
813111615Ssimokawasbp_next_dev(struct sbp_target *target, int lun)
814111615Ssimokawa{
815111615Ssimokawa	struct sbp_dev *sdev;
816111615Ssimokawa	int i;
817111615Ssimokawa
818111615Ssimokawa	for (i = lun, sdev = &target->luns[lun];
819111615Ssimokawa			i < target->num_lun; i++, sdev++) {
820111615Ssimokawa		if (sdev->status == SBP_DEV_PROBE)
821111615Ssimokawa			break;
822111615Ssimokawa	}
823111615Ssimokawa	if (i >= target->num_lun)
824111615Ssimokawa		return(NULL);
825111615Ssimokawa	return(sdev);
826111615Ssimokawa}
827111615Ssimokawa
828111615Ssimokawa#define SCAN_PRI 1
829103285Sikobstatic void
830111615Ssimokawasbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb)
831103285Sikob{
832111615Ssimokawa	struct sbp_target *target;
833103285Sikob	struct sbp_dev *sdev;
834111615Ssimokawa
835103285Sikob	sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
836111615Ssimokawa	target = sdev->target;
837110269SsimokawaSBP_DEBUG(0)
838103285Sikob	sbp_show_sdev_info(sdev, 2);
839111615Ssimokawa	printf("sbp_cam_scan_lun\n");
840103285SikobEND_DEBUG
841111615Ssimokawa	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
842111615Ssimokawa		sdev->status = SBP_DEV_ATTACHED;
843111615Ssimokawa	} else {
844111615Ssimokawa		sbp_show_sdev_info(sdev, 2);
845111615Ssimokawa		printf("scan failed\n");
846111615Ssimokawa	}
847111615Ssimokawa	sdev = sbp_next_dev(target, sdev->lun_id + 1);
848111615Ssimokawa	if (sdev == NULL) {
849111615Ssimokawa		free(ccb, M_SBP);
850111615Ssimokawa		return;
851111615Ssimokawa	}
852111615Ssimokawa	/* reuse ccb */
853111615Ssimokawa	xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
854111615Ssimokawa	ccb->ccb_h.ccb_sdev_ptr = sdev;
855111615Ssimokawa	xpt_action(ccb);
856111615Ssimokawa	xpt_release_devq(sdev->path, sdev->freeze, TRUE);
857111615Ssimokawa	sdev->freeze = 1;
858103285Sikob}
859103285Sikob
860103285Sikobstatic void
861111615Ssimokawasbp_cam_scan_target(void *arg)
862103285Sikob{
863111615Ssimokawa	struct sbp_target *target = (struct sbp_target *)arg;
864111615Ssimokawa	struct sbp_dev *sdev;
865110798Ssimokawa	union ccb *ccb;
866103285Sikob
867111615Ssimokawa	sdev = sbp_next_dev(target, 0);
868111615Ssimokawa	if (sdev == NULL) {
869111615Ssimokawa		printf("sbp_cam_scan_target: nothing to do for target%d\n",
870111615Ssimokawa							target->target_id);
871110798Ssimokawa		return;
872110798Ssimokawa	}
873103285SikobSBP_DEBUG(0)
874103285Sikob	sbp_show_sdev_info(sdev, 2);
875111615Ssimokawa	printf("sbp_cam_scan_target\n");
876103285SikobEND_DEBUG
877111615Ssimokawa	ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO);
878111615Ssimokawa	if (ccb == NULL) {
879111615Ssimokawa		printf("sbp_cam_scan_target: malloc failed\n");
880111615Ssimokawa		return;
881111615Ssimokawa	}
882111615Ssimokawa	xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
883103285Sikob	ccb->ccb_h.func_code = XPT_SCAN_LUN;
884111615Ssimokawa	ccb->ccb_h.cbfcnp = sbp_cam_scan_lun;
885111615Ssimokawa	ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
886103285Sikob	ccb->crcn.flags = CAM_FLAG_NONE;
887103285Sikob	ccb->ccb_h.ccb_sdev_ptr = sdev;
888103285Sikob
889103285Sikob	/* The scan is in progress now. */
890111040Ssimokawa	xpt_action(ccb);
891111615Ssimokawa	xpt_release_devq(sdev->path, sdev->freeze, TRUE);
892111615Ssimokawa	sdev->freeze = 1;
893103285Sikob}
894103285Sikob
895103285Sikob
896111615Ssimokawa#if 0
897103285Sikobstatic void
898103285Sikobsbp_ping_unit_callback(struct cam_periph *periph, union ccb *ccb)
899103285Sikob{
900103285Sikob	struct sbp_dev *sdev;
901103285Sikob	sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
902110269SsimokawaSBP_DEBUG(0)
903103285Sikob	sbp_show_sdev_info(sdev, 2);
904103285Sikob	printf("sbp_ping_unit_callback\n");
905103285SikobEND_DEBUG
906103285Sikob	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
907103285Sikob		if (--ccb->ccb_h.retry_count == 0) {
908103285Sikob			sbp_show_sdev_info(sdev, 2);
909110336Ssimokawa			printf("sbp_ping_unit_callback: "
910110336Ssimokawa				"retry count exceeded\n");
911103285Sikob			sdev->status = SBP_DEV_RETRY;
912103285Sikob			free(ccb, M_SBP);
913103285Sikob		} else {
914103285Sikob			/* requeue */
915103285Sikob			xpt_action(ccb);
916111615Ssimokawa			xpt_release_devq(sdev->path, sdev->freeze, TRUE);
917111615Ssimokawa			sdev->freeze = 1; /* we will freeze */
918103285Sikob		}
919103285Sikob	} else {
920103285Sikob		free(ccb->csio.data_ptr, M_SBP);
921103285Sikob		free(ccb, M_SBP);
922103285Sikob		sdev->status = SBP_DEV_ATTACHED;
923111615Ssimokawa		xpt_release_devq(sdev->path, sdev->freeze, TRUE);
924111615Ssimokawa		sdev->freeze = 0;
925103285Sikob	}
926103285Sikob}
927103285Sikob
928103285Sikob/*
929103285Sikob * XXX Some devices need to execute inquiry or read_capacity
930103285Sikob * after bus_rest during busy transfer.
931103285Sikob * Otherwise they return incorrect result for READ(and WRITE?)
932103285Sikob * command without any SBP-II/SCSI error.
933103285Sikob *
934103285Sikob * e.g. Maxtor 3000XT, Yano A-dish.
935103285Sikob */
936103285Sikobstatic void
937103285Sikobsbp_ping_unit(struct sbp_dev *sdev)
938103285Sikob{
939103285Sikob	union ccb *ccb;
940103285Sikob	struct scsi_inquiry_data *inq_buf;
941103285Sikob
942110798Ssimokawa
943110798Ssimokawa	ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO);
944110798Ssimokawa	if (ccb == NULL) {
945110798Ssimokawa		printf("sbp_ping_unit: malloc failed\n");
946110798Ssimokawa		return;
947110798Ssimokawa	}
948110798Ssimokawa
949103285Sikob	inq_buf = (struct scsi_inquiry_data *)
950110798Ssimokawa			malloc(sizeof(*inq_buf), M_SBP, M_NOWAIT);
951110798Ssimokawa	if (inq_buf == NULL) {
952110798Ssimokawa		free(ccb, M_SBP);
953110798Ssimokawa		printf("sbp_ping_unit: malloc failed\n");
954110798Ssimokawa		return;
955110798Ssimokawa	}
956103285Sikob
957110269SsimokawaSBP_DEBUG(0)
958103285Sikob	sbp_show_sdev_info(sdev, 2);
959103285Sikob	printf("sbp_ping_unit\n");
960103285SikobEND_DEBUG
961103285Sikob
962103285Sikob	/*
963103285Sikob	 * We need to execute this command before any other queued command.
964111615Ssimokawa	 * Make priority 0 and freeze the queue after execution for retry.
965103285Sikob	 * cam's scan_lun command doesn't provide this feature.
966103285Sikob	 */
967103285Sikob	xpt_setup_ccb(&ccb->ccb_h, sdev->path, 0/*priority (high)*/);
968103285Sikob	scsi_inquiry(
969103285Sikob		&ccb->csio,
970103285Sikob		/*retries*/ 5,
971103285Sikob		sbp_ping_unit_callback,
972103285Sikob		MSG_SIMPLE_Q_TAG,
973103285Sikob		(u_int8_t *)inq_buf,
974103285Sikob		SHORT_INQUIRY_LENGTH,
975103285Sikob		/*evpd*/FALSE,
976103285Sikob		/*page_code*/0,
977103285Sikob		SSD_MIN_SIZE,
978103285Sikob		/*timeout*/60000
979103285Sikob	);
980103285Sikob	ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
981111615Ssimokawa	ccb->ccb_h.ccb_sdev_ptr = sdev;
982103285Sikob	xpt_action(ccb);
983111615Ssimokawa	if (sdev->status != SBP_DEV_ATTACHED)
984111615Ssimokawa		sdev->status = SBP_DEV_PROBE;
985111615Ssimokawa	xpt_release_devq(sdev->path, sdev->freeze, TRUE);
986111615Ssimokawa	sdev->freeze = 1; /* We will freeze the queue */
987111615Ssimokawa}
988111615Ssimokawa#endif
989111615Ssimokawa
990111615Ssimokawastatic __inline void
991111615Ssimokawasbp_scan_dev(struct sbp_dev *sdev)
992111615Ssimokawa{
993111040Ssimokawa	sdev->status = SBP_DEV_PROBE;
994111615Ssimokawa	callout_reset(&sdev->target->scan_callout, SCAN_DELAY * hz,
995111615Ssimokawa			sbp_cam_scan_target, (void *)sdev->target);
996103285Sikob}
997103285Sikob
998103285Sikobstatic void
999103285Sikobsbp_do_attach(struct fw_xfer *xfer)
1000103285Sikob{
1001103285Sikob	struct sbp_dev *sdev;
1002111615Ssimokawa	struct sbp_target *target;
1003111615Ssimokawa	struct sbp_softc *sbp;
1004103285Sikob
1005103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
1006111615Ssimokawa	target = sdev->target;
1007111615Ssimokawa	sbp = target->sbp;
1008103285SikobSBP_DEBUG(0)
1009103285Sikob	sbp_show_sdev_info(sdev, 2);
1010103285Sikob	printf("sbp_do_attach\n");
1011103285SikobEND_DEBUG
1012103285Sikob	fw_xfer_free(xfer);
1013111199Ssimokawa
1014103285Sikob	if (sdev->path == NULL)
1015103285Sikob		xpt_create_path(&sdev->path, xpt_periph,
1016111615Ssimokawa			cam_sim_path(target->sbp->sim),
1017111615Ssimokawa			target->target_id, sdev->lun_id);
1018103285Sikob
1019111199Ssimokawa	/*
1020111199Ssimokawa	 * Let CAM scan the bus if we are in the boot process.
1021111199Ssimokawa	 * XXX xpt_scan_bus cannot detect LUN larger than 0
1022111199Ssimokawa	 * if LUN 0 doesn't exists.
1023111199Ssimokawa	 */
1024111199Ssimokawa	if (sbp_cold > 0) {
1025111615Ssimokawa		sdev->status = SBP_DEV_ATTACHED;
1026111199Ssimokawa		return;
1027111199Ssimokawa	}
1028111199Ssimokawa
1029111615Ssimokawa	sbp_scan_dev(sdev);
1030103285Sikob	return;
1031103285Sikob}
1032103285Sikob
1033103285Sikobstatic void
1034103285Sikobsbp_agent_reset_callback(struct fw_xfer *xfer)
1035103285Sikob{
1036103285Sikob	struct sbp_dev *sdev;
1037103285Sikob
1038103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
1039103285SikobSBP_DEBUG(1)
1040103285Sikob	sbp_show_sdev_info(sdev, 2);
1041103285Sikob	printf("sbp_cmd_callback\n");
1042103285SikobEND_DEBUG
1043103285Sikob	fw_xfer_free(xfer);
1044111615Ssimokawa	if (sdev->path) {
1045111615Ssimokawa		xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1046111615Ssimokawa		sdev->freeze = 0;
1047111615Ssimokawa	}
1048103285Sikob}
1049103285Sikob
1050103285Sikobstatic void
1051110336Ssimokawasbp_agent_reset(struct sbp_dev *sdev)
1052103285Sikob{
1053103285Sikob	struct fw_xfer *xfer;
1054103285Sikob	struct fw_pkt *fp;
1055103285Sikob
1056103285SikobSBP_DEBUG(0)
1057103285Sikob	sbp_show_sdev_info(sdev, 2);
1058103285Sikob	printf("sbp_agent_reset\n");
1059103285SikobEND_DEBUG
1060103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1061103285Sikob	if (xfer == NULL)
1062103285Sikob		return;
1063110336Ssimokawa	if (sdev->status == SBP_DEV_ATTACHED)
1064110336Ssimokawa		xfer->act.hand = sbp_agent_reset_callback;
1065110336Ssimokawa	else
1066103285Sikob		xfer->act.hand = sbp_do_attach;
1067103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1068103285Sikob	fp->mode.wreqq.data = htonl(0xf);
1069103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1070111615Ssimokawa	sbp_abort_all_ocbs(sdev, CAM_BDR_SENT);
1071103285Sikob}
1072103285Sikob
1073103285Sikobstatic void
1074103285Sikobsbp_busy_timeout_callback(struct fw_xfer *xfer)
1075103285Sikob{
1076103285Sikob	struct sbp_dev *sdev;
1077103285Sikob
1078103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
1079103285SikobSBP_DEBUG(1)
1080103285Sikob	sbp_show_sdev_info(sdev, 2);
1081110336Ssimokawa	printf("sbp_busy_timeout_callback\n");
1082103285SikobEND_DEBUG
1083103285Sikob	fw_xfer_free(xfer);
1084110336Ssimokawa	sbp_agent_reset(sdev);
1085103285Sikob}
1086103285Sikob
1087103285Sikobstatic void
1088103285Sikobsbp_busy_timeout(struct sbp_dev *sdev)
1089103285Sikob{
1090103285Sikob	struct fw_pkt *fp;
1091103285Sikob	struct fw_xfer *xfer;
1092103285SikobSBP_DEBUG(0)
1093103285Sikob	sbp_show_sdev_info(sdev, 2);
1094103285Sikob	printf("sbp_busy_timeout\n");
1095103285SikobEND_DEBUG
1096103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1097103285Sikob
1098103285Sikob	xfer->act.hand = sbp_busy_timeout_callback;
1099103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1100103285Sikob	fp->mode.wreqq.dest_hi = htons(0xffff);
1101110129Ssimokawa	fp->mode.wreqq.dest_lo = htonl(0xf0000000 | BUSY_TIMEOUT);
1102110129Ssimokawa	fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1103103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1104103285Sikob}
1105103285Sikob
1106103285Sikob#if 0
1107103285Sikobstatic void
1108103285Sikobsbp_reset_start(struct sbp_dev *sdev)
1109103285Sikob{
1110103285Sikob	struct fw_xfer *xfer;
1111103285Sikob	struct fw_pkt *fp;
1112103285Sikob
1113103285SikobSBP_DEBUG(0)
1114103285Sikob	sbp_show_sdev_info(sdev, 2);
1115103285Sikob	printf("sbp_reset_start\n");
1116103285SikobEND_DEBUG
1117103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1118103285Sikob
1119103285Sikob	xfer->act.hand = sbp_busy_timeout;
1120103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1121103285Sikob	fp->mode.wreqq.dest_hi = htons(0xffff);
1122103285Sikob	fp->mode.wreqq.dest_lo = htonl(0xf0000000 | RESET_START);
1123103285Sikob	fp->mode.wreqq.data = htonl(0xf);
1124103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1125103285Sikob}
1126103285Sikob#endif
1127103285Sikob
1128103285Sikobstatic void
1129103285Sikobsbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1130103285Sikob{
1131103285Sikob	struct fw_xfer *xfer;
1132103285Sikob	struct fw_pkt *fp;
1133103285SikobSBP_DEBUG(2)
1134103285Sikob	sbp_show_sdev_info(sdev, 2);
1135103285Sikob	printf("sbp_orb_pointer\n");
1136103285SikobEND_DEBUG
1137103285Sikob
1138103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1139103285Sikob	if (xfer == NULL)
1140103285Sikob		return;
1141103285Sikob	xfer->act.hand = sbp_cmd_callback;
1142103285Sikob
1143103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1144103285Sikob	fp->mode.wreqb.len = htons(8);
1145103285Sikob	fp->mode.wreqb.extcode = 0;
1146103285Sikob	fp->mode.wreqb.payload[0] =
1147103285Sikob		htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1148103285Sikob	fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0]));
1149103285Sikob
1150103285Sikob	if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1151103285Sikob			fw_xfer_free(xfer);
1152103285Sikob			ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1153103285Sikob			xpt_done(ocb->ccb);
1154103285Sikob	}
1155103285Sikob}
1156103285Sikob
1157103285Sikobstatic void
1158103285Sikobsbp_doorbell(struct sbp_dev *sdev)
1159103285Sikob{
1160103285Sikob	struct fw_xfer *xfer;
1161103285Sikob	struct fw_pkt *fp;
1162103285SikobSBP_DEBUG(1)
1163103285Sikob	sbp_show_sdev_info(sdev, 2);
1164103285Sikob	printf("sbp_doorbell\n");
1165103285SikobEND_DEBUG
1166103285Sikob
1167103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1168103285Sikob	if (xfer == NULL)
1169103285Sikob		return;
1170103285Sikob	xfer->act.hand = sbp_cmd_callback;
1171103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1172103285Sikob	fp->mode.wreqq.data = htonl(0xf);
1173103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1174103285Sikob}
1175103285Sikob
1176103285Sikobstatic struct fw_xfer *
1177103285Sikobsbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1178103285Sikob{
1179103285Sikob	struct fw_xfer *xfer;
1180103285Sikob	struct fw_pkt *fp;
1181103285Sikob
1182110269Ssimokawa	xfer = fw_xfer_alloc(M_SBP);
1183103285Sikob	if(xfer == NULL){
1184103285Sikob		return NULL;
1185103285Sikob	}
1186103285Sikob	if (tcode == FWTCODE_WREQQ)
1187103285Sikob		xfer->send.len = 16;
1188103285Sikob	else
1189103285Sikob		xfer->send.len = 24;
1190103285Sikob
1191110195Ssimokawa	xfer->send.buf = malloc(xfer->send.len, M_FW, M_NOWAIT);
1192103285Sikob	if(xfer->send.buf == NULL){
1193110195Ssimokawa		fw_xfer_free(xfer);
1194103285Sikob		return NULL;
1195103285Sikob	}
1196103285Sikob
1197103285Sikob	xfer->send.off = 0;
1198103285Sikob	xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1199103285Sikob	xfer->sc = (caddr_t)sdev;
1200103285Sikob	xfer->fc = sdev->target->sbp->fd.fc;
1201103285Sikob	xfer->retry_req = fw_asybusy;
1202103285Sikob
1203103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1204103285Sikob	fp->mode.wreqq.dest_hi = htons(sdev->login.cmd_hi);
1205103285Sikob	fp->mode.wreqq.dest_lo = htonl(sdev->login.cmd_lo + offset);
1206103285Sikob	fp->mode.wreqq.tlrt = 0;
1207103285Sikob	fp->mode.wreqq.tcode = tcode;
1208103285Sikob	fp->mode.wreqq.pri = 0;
1209103285Sikob	xfer->dst = FWLOCALBUS | sdev->target->fwdev->dst;
1210103285Sikob	fp->mode.wreqq.dst = htons(xfer->dst);
1211103285Sikob
1212103285Sikob	return xfer;
1213103285Sikob
1214103285Sikob}
1215103285Sikob
1216103285Sikobstatic void
1217111615Ssimokawasbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1218103285Sikob{
1219103285Sikob	struct fw_xfer *xfer;
1220103285Sikob	struct fw_pkt *fp;
1221103285Sikob	struct sbp_ocb *ocb;
1222111615Ssimokawa	struct sbp_target *target;
1223103285Sikob	int s, nid;
1224103285Sikob
1225111615Ssimokawa	target = sdev->target;
1226111615Ssimokawa	nid = target->sbp->fd.fc->nodeid | FWLOCALBUS;
1227111615Ssimokawa
1228111615Ssimokawa	s = splfw();
1229111615Ssimokawa	if (func == ORB_FUN_RUNQUEUE) {
1230111615Ssimokawa		ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1231111615Ssimokawa		if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1232111615Ssimokawa			splx(s);
1233111615Ssimokawa			return;
1234111615Ssimokawa		}
1235111615Ssimokawa		STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1236111615Ssimokawa		goto start;
1237111615Ssimokawa	}
1238111615Ssimokawa	if ((ocb = sbp_get_ocb(target->sbp)) == NULL) {
1239111615Ssimokawa		target->sbp->flags |= SBP_RESOURCE_SHORTAGE;
1240103285Sikob		splx(s);
1241103285Sikob		return;
1242103285Sikob	}
1243103285Sikob	ocb->flags = OCB_ACT_MGM;
1244103285Sikob	ocb->sdev = sdev;
1245103285Sikob
1246103285Sikob	bzero((void *)(uintptr_t)(volatile void *)ocb->orb, sizeof(ocb->orb));
1247103285Sikob	ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1248103285Sikob	ocb->orb[7] = htonl(SBP_DEV2ADDR(
1249111615Ssimokawa		device_get_unit(target->sbp->fd.dev),
1250111615Ssimokawa		target->target_id,
1251103285Sikob		sdev->lun_id));
1252103285Sikob
1253107653SsimokawaSBP_DEBUG(0)
1254103285Sikob	sbp_show_sdev_info(sdev, 2);
1255103285Sikob	printf("%s\n", orb_fun_name[(func>>16)&0xf]);
1256107653SsimokawaEND_DEBUG
1257103285Sikob	switch (func) {
1258103285Sikob	case ORB_FUN_LGI:
1259103285Sikob		ocb->orb[2] = htonl(nid << 16);
1260103285Sikob		ocb->orb[3] = htonl(vtophys(&sdev->login));
1261103285Sikob		ocb->orb[4] = htonl(ORB_NOTIFY | ORB_EXV | sdev->lun_id);
1262103285Sikob		ocb->orb[5] = htonl(sizeof(struct sbp_login_res));
1263103285Sikob		break;
1264110336Ssimokawa	case ORB_FUN_ATA:
1265111615Ssimokawa		ocb->orb[0] = htonl((0 << 16) | 0);
1266111615Ssimokawa		ocb->orb[1] = htonl(vtophys(&aocb->orb[0]));
1267110336Ssimokawa		/* fall through */
1268103285Sikob	case ORB_FUN_RCN:
1269103285Sikob	case ORB_FUN_LGO:
1270103285Sikob	case ORB_FUN_LUR:
1271103285Sikob	case ORB_FUN_RST:
1272103285Sikob	case ORB_FUN_ATS:
1273103285Sikob		ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login.id);
1274103285Sikob		break;
1275103285Sikob	}
1276103285Sikob
1277111615Ssimokawa	if (target->mgm_ocb_cur != NULL) {
1278111615Ssimokawa		/* there is a standing ORB */
1279111615Ssimokawa		STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1280111615Ssimokawa		splx(s);
1281111615Ssimokawa		return;
1282111615Ssimokawa	}
1283111615Ssimokawastart:
1284111615Ssimokawa	target->mgm_ocb_cur = ocb;
1285111615Ssimokawa	splx(s);
1286111615Ssimokawa
1287111615Ssimokawa	callout_reset(&target->mgm_ocb_timeout, 5*hz,
1288111615Ssimokawa				sbp_timeout, (caddr_t)ocb);
1289103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1290103285Sikob	if(xfer == NULL){
1291103285Sikob		return;
1292103285Sikob	}
1293103285Sikob	xfer->act.hand = sbp_login_callback;
1294103285Sikob
1295103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1296103285Sikob	fp->mode.wreqb.dest_hi = htons(sdev->target->mgm_hi);
1297103285Sikob	fp->mode.wreqb.dest_lo = htonl(sdev->target->mgm_lo);
1298103285Sikob	fp->mode.wreqb.len = htons(8);
1299103285Sikob	fp->mode.wreqb.extcode = 0;
1300108503Ssimokawa	fp->mode.wreqb.payload[0] = htonl(nid << 16);
1301103285Sikob	fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0]));
1302103285Sikob
1303103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1304103285Sikob}
1305103285Sikob
1306103285Sikobstatic void
1307105792Ssimokawasbp_print_scsi_cmd(struct sbp_ocb *ocb)
1308103285Sikob{
1309103285Sikob	struct ccb_scsiio *csio;
1310103285Sikob
1311103285Sikob	csio = &ocb->ccb->csio;
1312103285Sikob	printf("%s:%d:%d XPT_SCSI_IO: "
1313103285Sikob		"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1314103285Sikob		", flags: 0x%02x, "
1315103285Sikob		"%db cmd/%db data/%db sense\n",
1316103285Sikob		device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1317103285Sikob		ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun,
1318103285Sikob		csio->cdb_io.cdb_bytes[0],
1319103285Sikob		csio->cdb_io.cdb_bytes[1],
1320103285Sikob		csio->cdb_io.cdb_bytes[2],
1321103285Sikob		csio->cdb_io.cdb_bytes[3],
1322103285Sikob		csio->cdb_io.cdb_bytes[4],
1323103285Sikob		csio->cdb_io.cdb_bytes[5],
1324103285Sikob		csio->cdb_io.cdb_bytes[6],
1325103285Sikob		csio->cdb_io.cdb_bytes[7],
1326103285Sikob		csio->cdb_io.cdb_bytes[8],
1327103285Sikob		csio->cdb_io.cdb_bytes[9],
1328103285Sikob		ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1329103285Sikob		csio->cdb_len, csio->dxfer_len,
1330103285Sikob		csio->sense_len);
1331105792Ssimokawa}
1332105792Ssimokawa
1333105792Ssimokawastatic void
1334105792Ssimokawasbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1335105792Ssimokawa{
1336105792Ssimokawa	struct sbp_cmd_status *sbp_cmd_status;
1337105792Ssimokawa	struct scsi_sense_data *sense;
1338105792Ssimokawa
1339105792Ssimokawa	sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1340105792Ssimokawa	sense = &ocb->ccb->csio.sense_data;
1341105792Ssimokawa
1342105792SsimokawaSBP_DEBUG(0)
1343105792Ssimokawa	sbp_print_scsi_cmd(ocb);
1344103285Sikob	/* XXX need decode status */
1345103285Sikob	sbp_show_sdev_info(ocb->sdev, 2);
1346103285Sikob	printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d",
1347103285Sikob		sbp_cmd_status->status,
1348103285Sikob		sbp_cmd_status->sfmt,
1349103285Sikob		sbp_cmd_status->valid,
1350103285Sikob		sbp_cmd_status->s_key,
1351103285Sikob		sbp_cmd_status->s_code,
1352103285Sikob		sbp_cmd_status->s_qlfr,
1353103285Sikob		sbp_status->len
1354103285Sikob	);
1355103285Sikob#if 0	 /* XXX */
1356103285Sikob	if (sbp_cmd_status->status == SCSI_STATUS_CHECK_COND) {
1357103285Sikob		printf(" %s\n", scsi_sense_key_text[sbp_cmd_status->s_key]);
1358103285Sikob			scsi_sense_desc(
1359103285Sikob				sbp_cmd_status->s_code,
1360103285Sikob				sbp_cmd_status->s_qlfr,
1361103285Sikob				ocb->ccb->ccb_h.path->device->inq_data
1362103285Sikob			)
1363103285Sikob	} else {
1364103285Sikob		printf("\n");
1365103285Sikob	}
1366103285Sikob#else
1367103285Sikob	printf("\n");
1368103285Sikob#endif
1369103285SikobEND_DEBUG
1370103285Sikob
1371110071Ssimokawa	switch (sbp_cmd_status->status) {
1372110071Ssimokawa	case SCSI_STATUS_CHECK_COND:
1373110071Ssimokawa	case SCSI_STATUS_BUSY:
1374110071Ssimokawa	case SCSI_STATUS_CMD_TERMINATED:
1375103285Sikob		if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1376103285Sikob			sense->error_code = SSD_CURRENT_ERROR;
1377103285Sikob		}else{
1378103285Sikob			sense->error_code = SSD_DEFERRED_ERROR;
1379103285Sikob		}
1380103285Sikob		if(sbp_cmd_status->valid)
1381103285Sikob			sense->error_code |= SSD_ERRCODE_VALID;
1382103285Sikob		sense->flags = sbp_cmd_status->s_key;
1383103285Sikob		if(sbp_cmd_status->mark)
1384103285Sikob			sense->flags |= SSD_FILEMARK;
1385103285Sikob		if(sbp_cmd_status->eom)
1386103285Sikob			sense->flags |= SSD_EOM;
1387103285Sikob		if(sbp_cmd_status->ill_len)
1388103285Sikob			sense->flags |= SSD_ILI;
1389103285Sikob		sense->info[0] = ntohl(sbp_cmd_status->info) & 0xff;
1390103285Sikob		sense->info[1] =(ntohl(sbp_cmd_status->info) >> 8) & 0xff;
1391103285Sikob		sense->info[2] =(ntohl(sbp_cmd_status->info) >> 16) & 0xff;
1392103285Sikob		sense->info[3] =(ntohl(sbp_cmd_status->info) >> 24) & 0xff;
1393103285Sikob		if (sbp_status->len <= 1)
1394103285Sikob			/* XXX not scsi status. shouldn't be happened */
1395103285Sikob			sense->extra_len = 0;
1396103285Sikob		else if (sbp_status->len <= 4)
1397103285Sikob			/* add_sense_code(_qual), info, cmd_spec_info */
1398103285Sikob			sense->extra_len = 6;
1399103285Sikob		else
1400103285Sikob			/* fru, sense_key_spec */
1401103285Sikob			sense->extra_len = 10;
1402103285Sikob		sense->cmd_spec_info[0] = ntohl(sbp_cmd_status->cdb) & 0xff;
1403103285Sikob		sense->cmd_spec_info[1] = (ntohl(sbp_cmd_status->cdb) >> 8) & 0xff;
1404103285Sikob		sense->cmd_spec_info[2] = (ntohl(sbp_cmd_status->cdb) >> 16) & 0xff;
1405103285Sikob		sense->cmd_spec_info[3] = (ntohl(sbp_cmd_status->cdb) >> 24) & 0xff;
1406103285Sikob		sense->add_sense_code = sbp_cmd_status->s_code;
1407103285Sikob		sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1408103285Sikob		sense->fru = sbp_cmd_status->fru;
1409103285Sikob		sense->sense_key_spec[0] = ntohl(sbp_cmd_status->s_keydep) & 0xff;
1410103285Sikob		sense->sense_key_spec[1] = (ntohl(sbp_cmd_status->s_keydep) >>8) & 0xff;
1411103285Sikob		sense->sense_key_spec[2] = (ntohl(sbp_cmd_status->s_keydep) >>16) & 0xff;
1412103285Sikob
1413103285Sikob		ocb->ccb->csio.scsi_status = sbp_cmd_status->status;;
1414103285Sikob		ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1415103285Sikob							| CAM_AUTOSNS_VALID;
1416103285Sikob/*
1417103285Sikob{
1418103285Sikob		u_int8_t j, *tmp;
1419103285Sikob		tmp = sense;
1420103285Sikob		for( j = 0 ; j < 32 ; j+=8){
1421103285Sikob			printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1422103285Sikob				tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1423103285Sikob				tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1424103285Sikob		}
1425103285Sikob
1426103285Sikob}
1427103285Sikob*/
1428110071Ssimokawa		break;
1429110071Ssimokawa	default:
1430110071Ssimokawa		sbp_show_sdev_info(ocb->sdev, 2);
1431110071Ssimokawa		printf("sbp_scsi_status: unknown scsi status 0x%x\n",
1432110071Ssimokawa						sbp_cmd_status->status);
1433103285Sikob	}
1434103285Sikob}
1435103285Sikob
1436103285Sikobstatic void
1437103285Sikobsbp_fix_inq_data(struct sbp_ocb *ocb)
1438103285Sikob{
1439103285Sikob	union ccb *ccb;
1440103285Sikob	struct sbp_dev *sdev;
1441103285Sikob	struct scsi_inquiry_data *inq;
1442103285Sikob
1443103285Sikob	ccb = ocb->ccb;
1444103285Sikob	sdev = ocb->sdev;
1445103285Sikob
1446103285Sikob	if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1447103285Sikob		return;
1448103285SikobSBP_DEBUG(1)
1449103285Sikob	sbp_show_sdev_info(sdev, 2);
1450103285Sikob	printf("sbp_fix_inq_data\n");
1451103285SikobEND_DEBUG
1452103285Sikob	inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1453103285Sikob	switch (SID_TYPE(inq)) {
1454103285Sikob	case T_DIRECT:
1455103285Sikob		/*
1456103285Sikob		 * XXX Convert Direct Access device to RBC.
1457108281Ssimokawa		 * I've never seen FireWire DA devices which support READ_6.
1458103285Sikob		 */
1459103285Sikob#if 1
1460103285Sikob		if (SID_TYPE(inq) == T_DIRECT)
1461103285Sikob			inq->device |= T_RBC; /*  T_DIRECT == 0 */
1462103285Sikob#endif
1463103285Sikob		/* fall through */
1464103285Sikob	case T_RBC:
1465111615Ssimokawa		/* enable tag queuing */
1466111615Ssimokawa#if 1
1467111615Ssimokawa		inq->flags |= SID_CmdQue;
1468111615Ssimokawa#endif
1469103285Sikob		/*
1470103285Sikob		 * Override vendor/product/revision information.
1471103285Sikob		 * Some devices sometimes return strange strings.
1472103285Sikob		 */
1473111615Ssimokawa#if 1
1474103285Sikob		bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1475103285Sikob		bcopy(sdev->product, inq->product, sizeof(inq->product));
1476103285Sikob		bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1477111615Ssimokawa#endif
1478103285Sikob		break;
1479103285Sikob	}
1480103285Sikob}
1481103285Sikob
1482103285Sikobstatic void
1483103285Sikobsbp_recv1(struct fw_xfer *xfer){
1484103285Sikob	struct fw_pkt *rfp;
1485103285Sikob#if NEED_RESPONSE
1486103285Sikob	struct fw_pkt *sfp;
1487103285Sikob#endif
1488103285Sikob	struct sbp_softc *sbp;
1489103285Sikob	struct sbp_dev *sdev;
1490103285Sikob	struct sbp_ocb *ocb;
1491103285Sikob	struct sbp_login_res *login_res = NULL;
1492103285Sikob	struct sbp_status *sbp_status;
1493103285Sikob	struct sbp_target *target;
1494111615Ssimokawa	int	orb_fun, status_valid0, status_valid, t, l;
1495103285Sikob	u_int32_t addr;
1496103285Sikob/*
1497103285Sikob	u_int32_t *ld;
1498103285Sikob	ld = xfer->recv.buf;
1499103285Sikobprintf("sbp %x %d %d %08x %08x %08x %08x\n",
1500103285Sikob			xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1501103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1502103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1503103285Sikob*/
1504103285Sikob	if(xfer->resp != 0){
1505103285Sikob		printf("sbp_recv: xfer->resp != 0\n");
1506103285Sikob		fw_xfer_free( xfer);
1507103285Sikob		return;
1508103285Sikob	}
1509103285Sikob	if(xfer->recv.buf == NULL){
1510103285Sikob		printf("sbp_recv: xfer->recv.buf == NULL\n");
1511103285Sikob		fw_xfer_free( xfer);
1512103285Sikob		return;
1513103285Sikob	}
1514103285Sikob	sbp = (struct sbp_softc *)xfer->sc;
1515103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
1516103285Sikob	if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1517103285Sikob		printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1518103285Sikob		fw_xfer_free( xfer);
1519103285Sikob		return;
1520103285Sikob	}
1521103285Sikob	sbp_status = (struct sbp_status *)rfp->mode.wreqb.payload;
1522103285Sikob	addr = ntohl(rfp->mode.wreqb.dest_lo);
1523103285SikobSBP_DEBUG(2)
1524103285Sikob	printf("received address 0x%x\n", addr);
1525103285SikobEND_DEBUG
1526110189Ssimokawa	t = SBP_ADDR2TRG(addr);
1527110189Ssimokawa	if (t >= SBP_NUM_TARGETS) {
1528110189Ssimokawa		device_printf(sbp->fd.dev,
1529110189Ssimokawa			"sbp_recv1: invalid target %d\n", t);
1530110189Ssimokawa		fw_xfer_free(xfer);
1531110189Ssimokawa		return;
1532110189Ssimokawa	}
1533110189Ssimokawa	target = &sbp->targets[t];
1534110189Ssimokawa	l = SBP_ADDR2LUN(addr);
1535110189Ssimokawa	if (l >= target->num_lun) {
1536110189Ssimokawa		device_printf(sbp->fd.dev,
1537110189Ssimokawa			"sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1538110189Ssimokawa		fw_xfer_free(xfer);
1539110189Ssimokawa		return;
1540110189Ssimokawa	}
1541110189Ssimokawa	sdev = &target->luns[l];
1542103285Sikob
1543110189Ssimokawa	ocb = NULL;
1544110189Ssimokawa	switch (sbp_status->src) {
1545110189Ssimokawa	case 0:
1546110189Ssimokawa	case 1:
1547111615Ssimokawa		/* check mgm_ocb_cur first */
1548111615Ssimokawa		ocb  = target->mgm_ocb_cur;
1549111615Ssimokawa		if (ocb != NULL) {
1550111615Ssimokawa			if (OCB_MATCH(ocb, sbp_status)) {
1551111615Ssimokawa				callout_stop(&target->mgm_ocb_timeout);
1552111615Ssimokawa				target->mgm_ocb_cur = NULL;
1553111615Ssimokawa				break;
1554111615Ssimokawa			}
1555111615Ssimokawa		}
1556111615Ssimokawa		ocb = sbp_dequeue_ocb(sdev, sbp_status);
1557110189Ssimokawa		if (ocb == NULL) {
1558110189Ssimokawa			sbp_show_sdev_info(sdev, 2);
1559110189Ssimokawa			printf("No ocb on the queue\n");
1560110189Ssimokawa		}
1561110189Ssimokawa		break;
1562110189Ssimokawa	case 2:
1563110189Ssimokawa		/* unsolicit */
1564110189Ssimokawa		sbp_show_sdev_info(sdev, 2);
1565110189Ssimokawa		printf("unsolicit status received\n");
1566110189Ssimokawa		break;
1567110189Ssimokawa	default:
1568110189Ssimokawa		sbp_show_sdev_info(sdev, 2);
1569110189Ssimokawa		printf("unknown sbp_status->src\n");
1570110189Ssimokawa	}
1571110189Ssimokawa
1572111615Ssimokawa	status_valid0 = (sbp_status->src < 2
1573110189Ssimokawa			&& sbp_status->resp == ORB_RES_CMPL
1574111615Ssimokawa			&& sbp_status->dead == 0);
1575111615Ssimokawa	status_valid = (status_valid0 && sbp_status->status == 0);
1576103285Sikob
1577111615Ssimokawa	if (!status_valid0 || debug > 1){
1578103285Sikob		int status;
1579110129SsimokawaSBP_DEBUG(0)
1580103285Sikob		sbp_show_sdev_info(sdev, 2);
1581103285Sikob		printf("ORB status src:%x resp:%x dead:%x"
1582108712Ssimokawa#if __FreeBSD_version >= 500000
1583103285Sikob				" len:%x stat:%x orb:%x%08x\n",
1584108712Ssimokawa#else
1585108712Ssimokawa				" len:%x stat:%x orb:%x%08lx\n",
1586108712Ssimokawa#endif
1587103285Sikob			sbp_status->src, sbp_status->resp, sbp_status->dead,
1588103285Sikob			sbp_status->len, sbp_status->status,
1589108280Ssimokawa			ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1590110129SsimokawaEND_DEBUG
1591103285Sikob		sbp_show_sdev_info(sdev, 2);
1592103285Sikob		status = sbp_status->status;
1593103285Sikob		switch(sbp_status->resp) {
1594103285Sikob		case 0:
1595103285Sikob			if (status > MAX_ORB_STATUS0)
1596103285Sikob				printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1597110189Ssimokawa			else
1598110129Ssimokawa				printf("%s\n", orb_status0[status]);
1599103285Sikob			break;
1600103285Sikob		case 1:
1601110336Ssimokawa			printf("Obj: %s, Error: %s\n",
1602103285Sikob				orb_status1_object[(status>>6) & 3],
1603103285Sikob				orb_status1_serial_bus_error[status & 0xf]);
1604103285Sikob			break;
1605110129Ssimokawa		case 2:
1606110129Ssimokawa			printf("Illegal request\n");
1607110129Ssimokawa			break;
1608110129Ssimokawa		case 3:
1609110129Ssimokawa			printf("Vendor dependent\n");
1610110129Ssimokawa			break;
1611103285Sikob		default:
1612110129Ssimokawa			printf("unknown respose code %d\n", sbp_status->resp);
1613103285Sikob		}
1614103285Sikob	}
1615103285Sikob
1616103285Sikob	/* we have to reset the fetch agent if it's dead */
1617103285Sikob	if (sbp_status->dead) {
1618111615Ssimokawa		if (sdev->path) {
1619103285Sikob			xpt_freeze_devq(sdev->path, 1);
1620111615Ssimokawa			sdev->freeze ++;
1621111615Ssimokawa		}
1622110336Ssimokawa		sbp_agent_reset(sdev);
1623103285Sikob	}
1624103285Sikob
1625103285Sikob	if (ocb == NULL) {
1626110189Ssimokawa		fw_xfer_free(xfer);
1627103285Sikob		return;
1628103285Sikob	}
1629103285Sikob
1630110336Ssimokawa	sdev->flags &= ~SBP_DEV_TIMEOUT;
1631110336Ssimokawa
1632103285Sikob	switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1633103285Sikob	case ORB_FMT_NOP:
1634103285Sikob		break;
1635103285Sikob	case ORB_FMT_VED:
1636103285Sikob		break;
1637103285Sikob	case ORB_FMT_STD:
1638103285Sikob		switch(ocb->flags & OCB_ACT_MASK){
1639103285Sikob		case OCB_ACT_MGM:
1640103285Sikob			orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1641103285Sikob			switch(orb_fun) {
1642103285Sikob			case ORB_FUN_LGI:
1643103285Sikob				login_res = &sdev->login;
1644103285Sikob				login_res->len = ntohs(login_res->len);
1645103285Sikob				login_res->id = ntohs(login_res->id);
1646103285Sikob				login_res->cmd_hi = ntohs(login_res->cmd_hi);
1647103285Sikob				login_res->cmd_lo = ntohl(login_res->cmd_lo);
1648103285Sikob				if (status_valid) {
1649103285SikobSBP_DEBUG(0)
1650103285Sikobsbp_show_sdev_info(sdev, 2);
1651103285Sikobprintf("login: len %d, ID %d, cmd %08x%08x, recon_hold %d\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo, ntohs(login_res->recon_hold));
1652103285SikobEND_DEBUG
1653111615Ssimokawa#if 0
1654111615Ssimokawa					sdev->status = SBP_DEV_TOATTACH;
1655111615Ssimokawa#endif
1656103285Sikob#if 1
1657103285Sikob					sbp_busy_timeout(sdev);
1658103285Sikob#else
1659111615Ssimokawa					sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL);
1660103285Sikob#endif
1661103285Sikob				} else {
1662110336Ssimokawa					/* forgot logout? */
1663110336Ssimokawa					sbp_show_sdev_info(sdev, 2);
1664103285Sikob					printf("login failed\n");
1665103285Sikob					sdev->status = SBP_DEV_RESET;
1666103285Sikob				}
1667103285Sikob				break;
1668103285Sikob			case ORB_FUN_RCN:
1669103285Sikob				login_res = &sdev->login;
1670103285Sikob				if (status_valid) {
1671103285SikobSBP_DEBUG(0)
1672103285Sikobsbp_show_sdev_info(sdev, 2);
1673103285Sikobprintf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo);
1674103285SikobEND_DEBUG
1675103285Sikob#if 1
1676111615Ssimokawa					if (sdev->status == SBP_DEV_ATTACHED)
1677111615Ssimokawa						sbp_scan_dev(sdev);
1678111615Ssimokawa					else
1679111615Ssimokawa						sbp_agent_reset(sdev);
1680103285Sikob#else
1681110336Ssimokawa					sdev->status = SBP_DEV_ATTACHED;
1682111615Ssimokawa					sbp_mgm_orb(sdev, ORB_FUN_ATS, NULL);
1683103285Sikob#endif
1684103285Sikob				} else {
1685103285Sikob					/* reconnection hold time exceed? */
1686110336SsimokawaSBP_DEBUG(0)
1687110336Ssimokawa					sbp_show_sdev_info(sdev, 2);
1688103285Sikob					printf("reconnect failed\n");
1689110336SsimokawaEND_DEBUG
1690111615Ssimokawa					sbp_login(sdev);
1691103285Sikob				}
1692103285Sikob				break;
1693103285Sikob			case ORB_FUN_LGO:
1694103285Sikob				sdev->status = SBP_DEV_RESET;
1695103285Sikob				break;
1696110336Ssimokawa			case ORB_FUN_RST:
1697110336Ssimokawa				sbp_busy_timeout(sdev);
1698110336Ssimokawa				break;
1699103285Sikob			case ORB_FUN_LUR:
1700103285Sikob			case ORB_FUN_ATA:
1701103285Sikob			case ORB_FUN_ATS:
1702110336Ssimokawa				sbp_agent_reset(sdev);
1703103285Sikob				break;
1704103285Sikob			default:
1705110336Ssimokawa				sbp_show_sdev_info(sdev, 2);
1706110336Ssimokawa				printf("unknown function %d\n", orb_fun);
1707103285Sikob				break;
1708103285Sikob			}
1709111615Ssimokawa			sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
1710103285Sikob			break;
1711103285Sikob		case OCB_ACT_CMD:
1712103285Sikob			if(ocb->ccb != NULL){
1713103285Sikob				union ccb *ccb;
1714103285Sikob/*
1715103285Sikob				u_int32_t *ld;
1716103285Sikob				ld = ocb->ccb->csio.data_ptr;
1717103285Sikob				if(ld != NULL && ocb->ccb->csio.dxfer_len != 0)
1718103285Sikob					printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]);
1719103285Sikob				else
1720103285Sikob					printf("ptr NULL\n");
1721103285Sikobprintf("len %d\n", sbp_status->len);
1722103285Sikob*/
1723103285Sikob				ccb = ocb->ccb;
1724103285Sikob				if(sbp_status->len > 1){
1725103285Sikob					sbp_scsi_status(sbp_status, ocb);
1726103285Sikob				}else{
1727103285Sikob					if(sbp_status->resp != ORB_RES_CMPL){
1728103285Sikob						ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1729103285Sikob					}else{
1730103285Sikob						ccb->ccb_h.status = CAM_REQ_CMP;
1731103285Sikob					}
1732103285Sikob				}
1733103285Sikob				/* fix up inq data */
1734103285Sikob				if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1735103285Sikob					sbp_fix_inq_data(ocb);
1736103285Sikob				xpt_done(ccb);
1737103285Sikob			}
1738103285Sikob			break;
1739103285Sikob		default:
1740103285Sikob			break;
1741103285Sikob		}
1742103285Sikob	}
1743103285Sikob
1744103285Sikob	if (!(ocb->flags & OCB_RESERVED))
1745103285Sikob		sbp_free_ocb(sbp, ocb);
1746103285Sikob
1747103285Sikob/* The received packet is usually small enough to be stored within
1748103285Sikob * the buffer. In that case, the controller return ack_complete and
1749103285Sikob * no respose is necessary.
1750103285Sikob *
1751103285Sikob * XXX fwohci.c and firewire.c should inform event_code such as
1752103285Sikob * ack_complete or ack_pending to upper driver.
1753103285Sikob */
1754103285Sikob#if NEED_RESPONSE
1755103285Sikob	xfer->send.buf = malloc(12, M_SBP, M_NOWAIT | M_ZERO);
1756103285Sikob	xfer->send.len = 12;
1757103285Sikob	xfer->send.off = 0;
1758103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
1759103285Sikob	sfp->mode.wres.dst = rfp->mode.wreqb.src;
1760103285Sikob	xfer->dst = ntohs(sfp->mode.wres.dst);
1761103285Sikob	xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1762103285Sikob	xfer->act.hand = sbp_loginres_callback;
1763103285Sikob	xfer->retry_req = fw_asybusy;
1764103285Sikob
1765103285Sikob	sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1766103285Sikob	sfp->mode.wres.tcode = FWTCODE_WRES;
1767103285Sikob	sfp->mode.wres.rtcode = 0;
1768103285Sikob	sfp->mode.wres.pri = 0;
1769103285Sikob
1770103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1771103285Sikob#else
1772103285Sikob	fw_xfer_free(xfer);
1773103285Sikob#endif
1774103285Sikob
1775103285Sikob	return;
1776103285Sikob
1777103285Sikob}
1778103285Sikob
1779103285Sikobstatic void
1780103285Sikobsbp_recv(struct fw_xfer *xfer)
1781103285Sikob{
1782103285Sikob	int s;
1783103285Sikob
1784103285Sikob	s = splcam();
1785103285Sikob	sbp_recv1(xfer);
1786103285Sikob	splx(s);
1787103285Sikob}
1788103285Sikob/*
1789103285Sikob * sbp_attach()
1790103285Sikob */
1791103285Sikobstatic int
1792103285Sikobsbp_attach(device_t dev)
1793103285Sikob{
1794103285Sikob	struct sbp_softc *sbp;
1795103285Sikob	struct cam_devq *devq;
1796103285Sikob	struct fw_xfer *xfer;
1797103285Sikob	int i, s, error;
1798103285Sikob
1799103285SikobSBP_DEBUG(0)
1800111199Ssimokawa	printf("sbp_attach (cold=%d)\n", cold);
1801103285SikobEND_DEBUG
1802103285Sikob
1803111199Ssimokawa	if (cold)
1804111199Ssimokawa		sbp_cold ++;
1805103285Sikob	sbp = ((struct sbp_softc *)device_get_softc(dev));
1806103285Sikob	bzero(sbp, sizeof(struct sbp_softc));
1807103285Sikob	sbp->fd.dev = dev;
1808103285Sikob	sbp->fd.fc = device_get_ivars(dev);
1809103285Sikob	error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
1810103285Sikob				/*boundary*/0,
1811103285Sikob				/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1812103285Sikob				/*highaddr*/BUS_SPACE_MAXADDR,
1813103285Sikob				/*filter*/NULL, /*filterarg*/NULL,
1814103285Sikob				/*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1815103285Sikob				/*maxsegsz*/0x8000,
1816103285Sikob				/*flags*/BUS_DMA_ALLOCNOW,
1817103285Sikob				&sbp->dmat);
1818103285Sikob	if (error != 0) {
1819103285Sikob		printf("sbp_attach: Could not allocate DMA tag "
1820103285Sikob			"- error %d\n", error);
1821103285Sikob			return (ENOMEM);
1822103285Sikob	}
1823103285Sikob
1824103285Sikob	devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1825103285Sikob	if (devq == NULL)
1826103285Sikob		return (ENXIO);
1827103285Sikob
1828103285Sikob	for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1829103285Sikob		sbp->targets[i].fwdev = NULL;
1830103285Sikob		sbp->targets[i].luns = NULL;
1831103285Sikob	}
1832103285Sikob
1833103285Sikob	sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1834103285Sikob				 device_get_unit(dev),
1835111615Ssimokawa				 /*untagged*/ 1,
1836111615Ssimokawa				 /*tagged*/ SBP_QUEUE_LEN,
1837111615Ssimokawa				 devq);
1838103285Sikob
1839103285Sikob	if (sbp->sim == NULL) {
1840103285Sikob		cam_simq_free(devq);
1841103285Sikob		return (ENXIO);
1842103285Sikob	}
1843103285Sikob
1844103285Sikob	sbp->ocb = (struct sbp_ocb *) contigmalloc(
1845103285Sikob		sizeof (struct sbp_ocb) * SBP_NUM_OCB,
1846109424Ssimokawa		M_SBP, M_NOWAIT, 0x10000, 0xffffffff, PAGE_SIZE, 0ul);
1847103285Sikob	bzero(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB);
1848103285Sikob
1849103285Sikob	if (sbp->ocb == NULL) {
1850103285Sikob		printf("sbp0: ocb alloction failure\n");
1851103285Sikob		return (ENOMEM);
1852103285Sikob	}
1853103285Sikob
1854103285Sikob	STAILQ_INIT(&sbp->free_ocbs);
1855103285Sikob	for (i = 0; i < SBP_NUM_OCB; i++) {
1856103285Sikob		sbp_free_ocb(sbp, &sbp->ocb[i]);
1857103285Sikob	}
1858103285Sikob
1859111615Ssimokawa	if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS)
1860111615Ssimokawa		goto fail;
1861103285Sikob
1862111615Ssimokawa	if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim),
1863111615Ssimokawa			CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1864111615Ssimokawa		goto fail;
1865111615Ssimokawa
1866110269Ssimokawa	xfer = fw_xfer_alloc(M_SBP);
1867103285Sikob	xfer->act.hand = sbp_recv;
1868103285Sikob	xfer->act_type = FWACT_XFER;
1869103285Sikob#if NEED_RESPONSE
1870103285Sikob	xfer->fc = sbp->fd.fc;
1871103285Sikob#endif
1872103285Sikob	xfer->sc = (caddr_t)sbp;
1873103285Sikob
1874103285Sikob	sbp->fwb.start_hi = SBP_BIND_HI;
1875103285Sikob	sbp->fwb.start_lo = SBP_DEV2ADDR(device_get_unit(sbp->fd.dev), 0, 0);
1876103285Sikob	/* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1877103285Sikob	sbp->fwb.addrlen = 0xffff;
1878103285Sikob	sbp->fwb.xfer = xfer;
1879103285Sikob	fw_bindadd(sbp->fd.fc, &sbp->fwb);
1880103285Sikob
1881103285Sikob	sbp->fd.post_explore = sbp_post_explore;
1882103285Sikob
1883111199Ssimokawa	if (sbp->fd.fc->status != -1) {
1884111199Ssimokawa		s = splfw();
1885111199Ssimokawa		sbp_post_explore((void *)sbp);
1886111199Ssimokawa		splx(s);
1887111199Ssimokawa	}
1888111199Ssimokawa
1889103285Sikob	return (0);
1890111615Ssimokawafail:
1891111615Ssimokawa	cam_sim_free(sbp->sim, /*free_devq*/TRUE);
1892111615Ssimokawa	contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB, M_SBP);
1893111615Ssimokawa	return (ENXIO);
1894103285Sikob}
1895103285Sikob
1896103285Sikobstatic int
1897110145Ssimokawasbp_logout_all(struct sbp_softc *sbp)
1898110145Ssimokawa{
1899110145Ssimokawa	struct sbp_target *target;
1900110145Ssimokawa	struct sbp_dev *sdev;
1901110145Ssimokawa	int i, j;
1902110145Ssimokawa
1903110145SsimokawaSBP_DEBUG(0)
1904110145Ssimokawa	printf("sbp_logout_all\n");
1905110145SsimokawaEND_DEBUG
1906110145Ssimokawa	for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
1907110145Ssimokawa		target = &sbp->targets[i];
1908110145Ssimokawa		if (target->luns == NULL)
1909110145Ssimokawa			continue;
1910110145Ssimokawa		for (j = 0; j < target->num_lun; j++) {
1911110145Ssimokawa			sdev = &target->luns[j];
1912110336Ssimokawa			if (sdev->status >= SBP_DEV_TOATTACH &&
1913110336Ssimokawa					sdev->status <= SBP_DEV_ATTACHED)
1914111615Ssimokawa				sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
1915110145Ssimokawa		}
1916110145Ssimokawa	}
1917110145Ssimokawa	return 0;
1918110145Ssimokawa}
1919110145Ssimokawa
1920110145Ssimokawastatic int
1921110145Ssimokawasbp_shutdown(device_t dev)
1922110145Ssimokawa{
1923110145Ssimokawa	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1924110145Ssimokawa
1925110145Ssimokawa	sbp_logout_all(sbp);
1926110145Ssimokawa	return (0);
1927110145Ssimokawa}
1928110145Ssimokawa
1929110145Ssimokawastatic int
1930103285Sikobsbp_detach(device_t dev)
1931103285Sikob{
1932103285Sikob	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1933103285Sikob	struct firewire_comm *fc = sbp->fd.fc;
1934103285Sikob	int i;
1935103285Sikob
1936103285SikobSBP_DEBUG(0)
1937103285Sikob	printf("sbp_detach\n");
1938103285SikobEND_DEBUG
1939110145Ssimokawa#if 0
1940103285Sikob	/* bus reset for logout */
1941103285Sikob	sbp->fd.post_explore = NULL;
1942103285Sikob	fc->ibr(fc);
1943110145Ssimokawa#endif
1944103285Sikob
1945103285Sikob	for (i = 0; i < SBP_NUM_TARGETS; i ++)
1946110145Ssimokawa		sbp_cam_detach_target(&sbp->targets[i]);
1947111615Ssimokawa	xpt_free_path(sbp->path);
1948103285Sikob	xpt_bus_deregister(cam_sim_path(sbp->sim));
1949110145Ssimokawa
1950110145Ssimokawa	sbp_logout_all(sbp);
1951110145Ssimokawa	/* XXX wait for logout completion */
1952110145Ssimokawa	tsleep(&i, FWPRI, "sbpdtc", hz/2);
1953110145Ssimokawa
1954110145Ssimokawa	fw_bindremove(fc, &sbp->fwb);
1955110145Ssimokawa	contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB, M_SBP);
1956103285Sikob	bus_dma_tag_destroy(sbp->dmat);
1957110145Ssimokawa
1958110145Ssimokawa	for (i = 0; i < SBP_NUM_TARGETS; i ++)
1959110145Ssimokawa		if (sbp->targets[i].luns != NULL)
1960110145Ssimokawa			free(sbp->targets[i].luns, M_SBP);
1961110145Ssimokawa
1962103285Sikob	return (0);
1963103285Sikob}
1964103285Sikob
1965103285Sikobstatic void
1966110145Ssimokawasbp_cam_detach_target(struct sbp_target *target)
1967103285Sikob{
1968103285Sikob	int i;
1969103285Sikob	struct sbp_dev *sdev;
1970103285Sikob
1971103285Sikob	if (target->luns != NULL) {
1972108529SsimokawaSBP_DEBUG(0)
1973103285Sikob		printf("sbp_detach_target %d\n", target->target_id);
1974108529SsimokawaEND_DEBUG
1975111615Ssimokawa		callout_stop(&target->scan_callout);
1976111615Ssimokawa		callout_stop(&target->mgm_ocb_timeout);
1977110193Ssimokawa		for (i = 0; i < target->num_lun; i++) {
1978103285Sikob			sdev = &target->luns[i];
1979111615Ssimokawa			callout_stop(&sdev->login_callout);
1980103285Sikob			if (sdev->status == SBP_DEV_RESET ||
1981103285Sikob					sdev->status == SBP_DEV_DEAD)
1982103285Sikob				continue;
1983110336Ssimokawa			if (sdev->path) {
1984103285Sikob				xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
1985110336Ssimokawa				xpt_free_path(sdev->path);
1986110336Ssimokawa				sdev->path = NULL;
1987110336Ssimokawa			}
1988103285Sikob			sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
1989103285Sikob		}
1990103285Sikob	}
1991103285Sikob}
1992103285Sikob
1993103285Sikobstatic void
1994103285Sikobsbp_timeout(void *arg)
1995103285Sikob{
1996103285Sikob	struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
1997103285Sikob	struct sbp_dev *sdev = ocb->sdev;
1998103285Sikob
1999103285Sikob	sbp_show_sdev_info(sdev, 2);
2000110336Ssimokawa	printf("request timeout ... ");
2001103285Sikob
2002111615Ssimokawa	if (ocb->flags == OCB_ACT_MGM) {
2003111615Ssimokawa		printf("management ORB\n");
2004111615Ssimokawa		/* XXX just ignore for now */
2005111615Ssimokawa		sdev->target->mgm_ocb_cur = NULL;
2006111615Ssimokawa		sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2007111615Ssimokawa		return;
2008111615Ssimokawa	}
2009111615Ssimokawa
2010110336Ssimokawa	xpt_freeze_devq(sdev->path, 1);
2011111615Ssimokawa	sdev->freeze ++;
2012110336Ssimokawa	sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2013110336Ssimokawa	if (sdev->flags & SBP_DEV_TIMEOUT) {
2014110269Ssimokawa#if 0
2015110336Ssimokawa		struct firewire_comm *fc;
2016110336Ssimokawa
2017110336Ssimokawa		printf("bus reset\n");
2018110336Ssimokawa		fc = sdev->target->sbp->fd.fc;
2019110336Ssimokawa		fc->ibr(fc);
2020110336Ssimokawa		sdev->status == SBP_DEV_RETRY;
2021110269Ssimokawa#else
2022110336Ssimokawa		printf("target reset\n");
2023111615Ssimokawa		sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2024110269Ssimokawa#endif
2025110336Ssimokawa		sdev->flags &= ~SBP_DEV_TIMEOUT;
2026110336Ssimokawa	} else {
2027110336Ssimokawa		printf("agent reset\n");
2028110336Ssimokawa		sdev->flags |= SBP_DEV_TIMEOUT;
2029110336Ssimokawa		sbp_agent_reset(sdev);
2030110336Ssimokawa	}
2031103285Sikob	return;
2032103285Sikob}
2033103285Sikob
2034103285Sikobstatic void
2035103285Sikobsbp_action1(struct cam_sim *sim, union ccb *ccb)
2036103285Sikob{
2037103285Sikob
2038103285Sikob	struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2039103285Sikob	struct sbp_target *target = NULL;
2040103285Sikob	struct sbp_dev *sdev = NULL;
2041103285Sikob
2042103285Sikob	/* target:lun -> sdev mapping */
2043103285Sikob	if (sbp != NULL
2044103285Sikob			&& ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2045103285Sikob			&& ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2046103285Sikob		target = &sbp->targets[ccb->ccb_h.target_id];
2047103285Sikob		if (target->fwdev != NULL
2048103285Sikob				&& ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2049103285Sikob				&& ccb->ccb_h.target_lun < target->num_lun) {
2050103285Sikob			sdev = &target->luns[ccb->ccb_h.target_lun];
2051103285Sikob			if (sdev->status != SBP_DEV_ATTACHED &&
2052103285Sikob				sdev->status != SBP_DEV_PROBE)
2053103285Sikob				sdev = NULL;
2054103285Sikob		}
2055103285Sikob	}
2056103285Sikob
2057103285SikobSBP_DEBUG(1)
2058103285Sikob	if (sdev == NULL)
2059103285Sikob		printf("invalid target %d lun %d\n",
2060103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2061103285SikobEND_DEBUG
2062103285Sikob
2063103285Sikob	switch (ccb->ccb_h.func_code) {
2064103285Sikob	case XPT_SCSI_IO:
2065103285Sikob	case XPT_RESET_DEV:
2066103285Sikob	case XPT_GET_TRAN_SETTINGS:
2067103285Sikob	case XPT_SET_TRAN_SETTINGS:
2068103285Sikob	case XPT_CALC_GEOMETRY:
2069103285Sikob		if (sdev == NULL) {
2070103285SikobSBP_DEBUG(1)
2071103285Sikob			printf("%s:%d:%d:func_code 0x%04x: "
2072103285Sikob				"Invalid target (target needed)\n",
2073103285Sikob				device_get_nameunit(sbp->fd.dev),
2074103285Sikob				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2075103285Sikob				ccb->ccb_h.func_code);
2076103285SikobEND_DEBUG
2077103285Sikob
2078108276Ssimokawa			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2079103285Sikob			xpt_done(ccb);
2080103285Sikob			return;
2081103285Sikob		}
2082103285Sikob		break;
2083103285Sikob	case XPT_PATH_INQ:
2084103285Sikob	case XPT_NOOP:
2085103285Sikob		/* The opcodes sometimes aimed at a target (sc is valid),
2086103285Sikob		 * sometimes aimed at the SIM (sc is invalid and target is
2087103285Sikob		 * CAM_TARGET_WILDCARD)
2088103285Sikob		 */
2089103285Sikob		if (sbp == NULL &&
2090103285Sikob			ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2091103285SikobSBP_DEBUG(0)
2092103285Sikob			printf("%s:%d:%d func_code 0x%04x: "
2093103285Sikob				"Invalid target (no wildcard)\n",
2094103285Sikob				device_get_nameunit(sbp->fd.dev),
2095103285Sikob				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2096103285Sikob				ccb->ccb_h.func_code);
2097103285SikobEND_DEBUG
2098108276Ssimokawa			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2099103285Sikob			xpt_done(ccb);
2100103285Sikob			return;
2101103285Sikob		}
2102103285Sikob		break;
2103103285Sikob	default:
2104103285Sikob		/* XXX Hm, we should check the input parameters */
2105103285Sikob		break;
2106103285Sikob	}
2107103285Sikob
2108103285Sikob	switch (ccb->ccb_h.func_code) {
2109103285Sikob	case XPT_SCSI_IO:
2110103285Sikob	{
2111103285Sikob		struct ccb_scsiio *csio;
2112103285Sikob		struct sbp_ocb *ocb;
2113103285Sikob		int s, speed;
2114106506Ssimokawa		void *cdb;
2115103285Sikob
2116103285Sikob		csio = &ccb->csio;
2117103285Sikob
2118103285SikobSBP_DEBUG(1)
2119103285Sikob		printf("%s:%d:%d XPT_SCSI_IO: "
2120103285Sikob			"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2121103285Sikob			", flags: 0x%02x, "
2122103285Sikob			"%db cmd/%db data/%db sense\n",
2123103285Sikob			device_get_nameunit(sbp->fd.dev),
2124103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2125103285Sikob			csio->cdb_io.cdb_bytes[0],
2126103285Sikob			csio->cdb_io.cdb_bytes[1],
2127103285Sikob			csio->cdb_io.cdb_bytes[2],
2128103285Sikob			csio->cdb_io.cdb_bytes[3],
2129103285Sikob			csio->cdb_io.cdb_bytes[4],
2130103285Sikob			csio->cdb_io.cdb_bytes[5],
2131103285Sikob			csio->cdb_io.cdb_bytes[6],
2132103285Sikob			csio->cdb_io.cdb_bytes[7],
2133103285Sikob			csio->cdb_io.cdb_bytes[8],
2134103285Sikob			csio->cdb_io.cdb_bytes[9],
2135103285Sikob			ccb->ccb_h.flags & CAM_DIR_MASK,
2136103285Sikob			csio->cdb_len, csio->dxfer_len,
2137103285Sikob			csio->sense_len);
2138103285SikobEND_DEBUG
2139103285Sikob		if(sdev == NULL){
2140103285Sikob			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2141103285Sikob			xpt_done(ccb);
2142103285Sikob			return;
2143103285Sikob		}
2144103285Sikob#if 0
2145103285Sikob		/* if we are in probe stage, pass only probe commands */
2146103285Sikob		if (sdev->status == SBP_DEV_PROBE) {
2147103285Sikob			char *name;
2148103285Sikob			name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2149103285Sikob			printf("probe stage, periph name: %s\n", name);
2150103285Sikob			if (strcmp(name, "probe") != 0) {
2151103285Sikob				ccb->ccb_h.status = CAM_REQUEUE_REQ;
2152103285Sikob				xpt_done(ccb);
2153103285Sikob				return;
2154103285Sikob			}
2155103285Sikob		}
2156103285Sikob#endif
2157103285Sikob		if ((ocb = sbp_get_ocb(sbp)) == NULL) {
2158103285Sikob			s = splfw();
2159103285Sikob			sbp->flags |= SBP_RESOURCE_SHORTAGE;
2160103285Sikob			splx(s);
2161103285Sikob			return;
2162103285Sikob		}
2163103285Sikob		ocb->flags = OCB_ACT_CMD;
2164103285Sikob		ocb->sdev = sdev;
2165103285Sikob		ocb->ccb = ccb;
2166103285Sikob		ccb->ccb_h.ccb_sdev_ptr = sdev;
2167103285Sikob		ocb->orb[0] = htonl(1 << 31);
2168103285Sikob		ocb->orb[1] = 0;
2169103285Sikob		ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2170103285Sikob		ocb->orb[3] = htonl(vtophys(ocb->ind_ptr));
2171103285Sikob		speed = min(target->fwdev->speed, max_speed);
2172103285Sikob		ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2173103285Sikob						| ORB_CMD_MAXP(speed + 7));
2174103285Sikob		if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
2175103285Sikob			ocb->orb[4] |= htonl(ORB_CMD_IN);
2176103285Sikob		}
2177103285Sikob
2178103285Sikob		if (csio->ccb_h.flags & CAM_SCATTER_VALID)
2179103285Sikob			printf("sbp: CAM_SCATTER_VALID\n");
2180103285Sikob		if (csio->ccb_h.flags & CAM_DATA_PHYS)
2181103285Sikob			printf("sbp: CAM_DATA_PHYS\n");
2182103285Sikob
2183106506Ssimokawa		if (csio->ccb_h.flags & CAM_CDB_POINTER)
2184106506Ssimokawa			cdb = (void *)csio->cdb_io.cdb_ptr;
2185106506Ssimokawa		else
2186106506Ssimokawa			cdb = (void *)&csio->cdb_io.cdb_bytes;
2187106506Ssimokawa		bcopy(cdb,
2188103285Sikob			(void *)(uintptr_t)(volatile void *)&ocb->orb[5],
2189106506Ssimokawa				csio->cdb_len);
2190103285Sikob/*
2191103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2192103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2193103285Sikob*/
2194103285Sikob		if (ccb->csio.dxfer_len > 0) {
2195103285Sikob			int s;
2196103285Sikob
2197103285Sikob			if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
2198103285Sikob				printf("sbp_action1: cannot create dmamap\n");
2199103285Sikob				break;
2200103285Sikob			}
2201103285Sikob
2202103285Sikob			s = splsoftvm();
2203103285Sikob			bus_dmamap_load(/*dma tag*/sbp->dmat,
2204103285Sikob					/*dma map*/ocb->dmamap,
2205103285Sikob					ccb->csio.data_ptr,
2206103285Sikob					ccb->csio.dxfer_len,
2207103285Sikob					sbp_execute_ocb,
2208103285Sikob					ocb,
2209103285Sikob					/*flags*/0);
2210103285Sikob			splx(s);
2211103285Sikob		} else
2212103285Sikob			sbp_execute_ocb(ocb, NULL, 0, 0);
2213103285Sikob		break;
2214103285Sikob	}
2215103285Sikob	case XPT_CALC_GEOMETRY:
2216103285Sikob	{
2217103285Sikob		struct ccb_calc_geometry *ccg;
2218103285Sikob		u_int32_t size_mb;
2219103285Sikob		u_int32_t secs_per_cylinder;
2220103285Sikob		int extended = 1;
2221103285Sikob		ccg = &ccb->ccg;
2222103285Sikob
2223103285Sikob		if (ccg->block_size == 0) {
2224103285Sikob			printf("sbp_action1: block_size is 0.\n");
2225103285Sikob			ccb->ccb_h.status = CAM_REQ_INVALID;
2226103285Sikob			xpt_done(ccb);
2227103285Sikob			break;
2228103285Sikob		}
2229103285SikobSBP_DEBUG(1)
2230103285Sikob		printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2231103285Sikob			"Volume size = %d\n",
2232103285Sikob			device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim),
2233103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2234103285Sikob			ccg->volume_size);
2235103285SikobEND_DEBUG
2236103285Sikob
2237103285Sikob		size_mb = ccg->volume_size
2238103285Sikob			/ ((1024L * 1024L) / ccg->block_size);
2239103285Sikob
2240103285Sikob		if (size_mb >= 1024 && extended) {
2241103285Sikob			ccg->heads = 255;
2242103285Sikob			ccg->secs_per_track = 63;
2243103285Sikob		} else {
2244103285Sikob			ccg->heads = 64;
2245103285Sikob			ccg->secs_per_track = 32;
2246103285Sikob		}
2247103285Sikob		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2248103285Sikob		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2249103285Sikob		ccb->ccb_h.status = CAM_REQ_CMP;
2250103285Sikob		xpt_done(ccb);
2251103285Sikob		break;
2252103285Sikob	}
2253103285Sikob	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
2254103285Sikob	{
2255103285Sikob
2256103285SikobSBP_DEBUG(1)
2257103285Sikob		printf("%s:%d:XPT_RESET_BUS: \n",
2258103285Sikob			device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2259103285SikobEND_DEBUG
2260103285Sikob
2261103285Sikob		ccb->ccb_h.status = CAM_REQ_INVALID;
2262103285Sikob		xpt_done(ccb);
2263103285Sikob		break;
2264103285Sikob	}
2265103285Sikob	case XPT_PATH_INQ:		/* Path routing inquiry */
2266103285Sikob	{
2267103285Sikob		struct ccb_pathinq *cpi = &ccb->cpi;
2268103285Sikob
2269103285SikobSBP_DEBUG(1)
2270103285Sikob		printf("%s:%d:%d XPT_PATH_INQ:.\n",
2271103285Sikob			device_get_nameunit(sbp->fd.dev),
2272103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2273103285SikobEND_DEBUG
2274103285Sikob		cpi->version_num = 1; /* XXX??? */
2275111615Ssimokawa		cpi->hba_inquiry = PI_TAG_ABLE;
2276103285Sikob		cpi->target_sprt = 0;
2277111199Ssimokawa		cpi->hba_misc = PIM_NOBUSRESET;
2278103285Sikob		cpi->hba_eng_cnt = 0;
2279103285Sikob		cpi->max_target = SBP_NUM_TARGETS - 1;
2280103285Sikob		cpi->max_lun = SBP_NUM_LUNS - 1;
2281103285Sikob		cpi->initiator_id = SBP_INITIATOR;
2282103285Sikob		cpi->bus_id = sim->bus_id;
2283103285Sikob		cpi->base_transfer_speed = 400 * 1000 / 8;
2284103285Sikob		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2285103285Sikob		strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2286103285Sikob		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2287103285Sikob		cpi->unit_number = sim->unit_number;
2288103285Sikob
2289103285Sikob		cpi->ccb_h.status = CAM_REQ_CMP;
2290103285Sikob		xpt_done(ccb);
2291103285Sikob		break;
2292103285Sikob	}
2293103285Sikob	case XPT_GET_TRAN_SETTINGS:
2294103285Sikob	{
2295103285Sikob		struct ccb_trans_settings *cts = &ccb->cts;
2296103285SikobSBP_DEBUG(1)
2297103285Sikob		printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2298103285Sikob			device_get_nameunit(sbp->fd.dev),
2299103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2300103285SikobEND_DEBUG
2301111615Ssimokawa		/* Enable disconnect and tagged queuing */
2302103285Sikob		cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2303111615Ssimokawa		cts->flags = CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB;
2304103285Sikob
2305103285Sikob		cts->ccb_h.status = CAM_REQ_CMP;
2306103285Sikob		xpt_done(ccb);
2307103285Sikob		break;
2308103285Sikob	}
2309103285Sikob	case XPT_ABORT:
2310103285Sikob		ccb->ccb_h.status = CAM_UA_ABORT;
2311103285Sikob		xpt_done(ccb);
2312103285Sikob		break;
2313111615Ssimokawa	case XPT_SET_TRAN_SETTINGS:
2314111615Ssimokawa		/* XXX */
2315103285Sikob	default:
2316103285Sikob		ccb->ccb_h.status = CAM_REQ_INVALID;
2317103285Sikob		xpt_done(ccb);
2318103285Sikob		break;
2319103285Sikob	}
2320103285Sikob	return;
2321103285Sikob}
2322103285Sikob
2323103285Sikobstatic void
2324103285Sikobsbp_action(struct cam_sim *sim, union ccb *ccb)
2325103285Sikob{
2326103285Sikob	int s;
2327103285Sikob
2328103285Sikob	s = splfw();
2329103285Sikob	sbp_action1(sim, ccb);
2330103285Sikob	splx(s);
2331103285Sikob}
2332103285Sikob
2333103285Sikobstatic void
2334103285Sikobsbp_execute_ocb(void *arg,  bus_dma_segment_t *segments, int seg, int error)
2335103285Sikob{
2336103285Sikob	int i;
2337103285Sikob	struct sbp_ocb *ocb;
2338103285Sikob	struct sbp_ocb *prev;
2339103285Sikob	union ccb *ccb;
2340105633Ssimokawa	bus_dma_segment_t *s;
2341103285Sikob
2342103285Sikob	if (error)
2343103285Sikob		printf("sbp_execute_ocb: error=%d\n", error);
2344103285Sikob
2345103285Sikob	ocb = (struct sbp_ocb *)arg;
2346103285Sikob	if (seg == 1) {
2347103285Sikob		/* direct pointer */
2348103285Sikob		ocb->orb[3] = htonl(segments[0].ds_addr);
2349103285Sikob		ocb->orb[4] |= htonl(segments[0].ds_len);
2350103285Sikob	} else if(seg > 1) {
2351103285Sikob		/* page table */
2352103285SikobSBP_DEBUG(1)
2353103285Sikob		printf("sbp_execute_ocb: seg %d", seg);
2354103285Sikob		for (i = 0; i < seg; i++)
2355108712Ssimokawa#if __FreeBSD_version >= 500000
2356106543Ssimokawa			printf(", %tx:%zd", segments[i].ds_addr,
2357108712Ssimokawa#else
2358108877Ssimokawa			printf(", %x:%d", segments[i].ds_addr,
2359108712Ssimokawa#endif
2360103285Sikob						segments[i].ds_len);
2361103285Sikob		printf("\n");
2362103285SikobEND_DEBUG
2363103285Sikob		for (i = 0; i < seg; i++) {
2364105633Ssimokawa			s = &segments[i];
2365108877SsimokawaSBP_DEBUG(0)
2366108877Ssimokawa			/* XXX LSI Logic "< 16 byte" bug might be hit */
2367105633Ssimokawa			if (s->ds_len < 16)
2368105633Ssimokawa				printf("sbp_execute_ocb: warning, "
2369108877Ssimokawa#if __FreeBSD_version >= 500000
2370106543Ssimokawa					"segment length(%zd) is less than 16."
2371108877Ssimokawa#else
2372108877Ssimokawa					"segment length(%d) is less than 16."
2373108877Ssimokawa#endif
2374105633Ssimokawa					"(seg=%d/%d)\n", s->ds_len, i+1, seg);
2375108877SsimokawaEND_DEBUG
2376105633Ssimokawa			ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2377105633Ssimokawa			ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2378103285Sikob		}
2379103285Sikob		ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2380103285Sikob	}
2381103285Sikob
2382103285Sikob	ccb = ocb->ccb;
2383103285Sikob	prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2384103285Sikob	if (prev)
2385103285Sikob		sbp_doorbell(ocb->sdev);
2386103285Sikob	else
2387103285Sikob		sbp_orb_pointer(ocb->sdev, ocb);
2388103285Sikob}
2389103285Sikob
2390103285Sikobstatic void
2391103285Sikobsbp_poll(struct cam_sim *sim)
2392103285Sikob{
2393103285Sikob	/* should call fwohci_intr? */
2394103285Sikob	return;
2395103285Sikob}
2396103285Sikobstatic struct sbp_ocb *
2397111615Ssimokawasbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2398103285Sikob{
2399103285Sikob	struct sbp_ocb *ocb;
2400103285Sikob	struct sbp_ocb *next;
2401103285Sikob	int s = splfw(), order = 0;
2402103285Sikob	int flags;
2403103285Sikob
2404103285Sikob	for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2405103285Sikob		next = STAILQ_NEXT(ocb, ocb);
2406103285Sikob		flags = ocb->flags;
2407103285SikobSBP_DEBUG(1)
2408110336Ssimokawa		sbp_show_sdev_info(sdev, 2);
2409108712Ssimokawa#if __FreeBSD_version >= 500000
2410106543Ssimokawa		printf("orb: 0x%tx next: 0x%x, flags %x\n",
2411108712Ssimokawa#else
2412108712Ssimokawa		printf("orb: 0x%x next: 0x%lx, flags %x\n",
2413108712Ssimokawa#endif
2414103285Sikob			vtophys(&ocb->orb[0]), ntohl(ocb->orb[1]), flags);
2415103285SikobEND_DEBUG
2416111615Ssimokawa		if (OCB_MATCH(ocb, sbp_status)) {
2417103285Sikob			/* found */
2418103285Sikob			if (ocb->flags & OCB_RESERVED)
2419103285Sikob				ocb->flags |= OCB_DONE;
2420103285Sikob			else
2421103285Sikob				STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2422103285Sikob			if (ocb->ccb != NULL)
2423103285Sikob				untimeout(sbp_timeout, (caddr_t)ocb,
2424103285Sikob						ocb->ccb->ccb_h.timeout_ch);
2425103285Sikob			if (ocb->dmamap != NULL) {
2426103285Sikob				bus_dmamap_destroy(sdev->target->sbp->dmat,
2427103285Sikob							ocb->dmamap);
2428103285Sikob				ocb->dmamap = NULL;
2429103285Sikob			}
2430103285Sikob			break;
2431103285Sikob		} else {
2432103285Sikob			if ((ocb->flags & OCB_RESERVED) &&
2433103285Sikob					(ocb->flags & OCB_DONE)) {
2434103285Sikob				/* next orb must be fetched already */
2435103285Sikob				STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2436103285Sikob				sbp_free_ocb(sdev->target->sbp, ocb);
2437103285Sikob			} else
2438103285Sikob				order ++;
2439103285Sikob		}
2440103285Sikob	}
2441103285Sikob	splx(s);
2442103285SikobSBP_DEBUG(0)
2443103285Sikob	if (ocb && order > 0) {
2444103285Sikob		sbp_show_sdev_info(sdev, 2);
2445103285Sikob		printf("unordered execution order:%d\n", order);
2446103285Sikob	}
2447103285SikobEND_DEBUG
2448103285Sikob	return (ocb);
2449103285Sikob}
2450103285Sikob
2451103285Sikobstatic struct sbp_ocb *
2452103285Sikobsbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2453103285Sikob{
2454103285Sikob	int s = splfw();
2455103285Sikob	struct sbp_ocb *prev;
2456103285Sikob
2457103285SikobSBP_DEBUG(2)
2458103285Sikob	sbp_show_sdev_info(sdev, 2);
2459108712Ssimokawa#if __FreeBSD_version >= 500000
2460106543Ssimokawa	printf("sbp_enqueue_ocb orb=0x%tx in physical memory\n", vtophys(&ocb->orb[0]));
2461108712Ssimokawa#else
2462108712Ssimokawa	printf("sbp_enqueue_ocb orb=0x%x in physical memory\n", vtophys(&ocb->orb[0]));
2463108712Ssimokawa#endif
2464103285SikobEND_DEBUG
2465103285Sikob	prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2466103285Sikob	STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2467103285Sikob
2468103285Sikob	if (ocb->ccb != NULL)
2469103285Sikob		ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb,
2470103285Sikob					(ocb->ccb->ccb_h.timeout * hz) / 1000);
2471103285Sikob
2472110579Ssimokawa	if (prev != NULL ) {
2473103285SikobSBP_DEBUG(1)
2474108712Ssimokawa#if __FreeBSD_version >= 500000
2475106543Ssimokawa	printf("linking chain 0x%tx -> 0x%tx\n", vtophys(&prev->orb[0]),
2476108712Ssimokawa#else
2477108712Ssimokawa	printf("linking chain 0x%x -> 0x%x\n", vtophys(&prev->orb[0]),
2478108712Ssimokawa#endif
2479103285Sikob			vtophys(&ocb->orb[0]));
2480103285SikobEND_DEBUG
2481103285Sikob		prev->flags |= OCB_RESERVED;
2482103285Sikob		prev->orb[1] = htonl(vtophys(&ocb->orb[0]));
2483103285Sikob		prev->orb[0] = 0;
2484103285Sikob	}
2485103285Sikob	splx(s);
2486103285Sikob
2487103285Sikob	return prev;
2488103285Sikob}
2489103285Sikob
2490103285Sikobstatic struct sbp_ocb *
2491103285Sikobsbp_get_ocb(struct sbp_softc *sbp)
2492103285Sikob{
2493103285Sikob	struct sbp_ocb *ocb;
2494103285Sikob	int s = splfw();
2495103285Sikob	ocb = STAILQ_FIRST(&sbp->free_ocbs);
2496103285Sikob	if (ocb == NULL) {
2497103285Sikob		printf("ocb shortage!!!\n");
2498103285Sikob		return NULL;
2499103285Sikob	}
2500111615Ssimokawa	STAILQ_REMOVE_HEAD(&sbp->free_ocbs, ocb);
2501103285Sikob	splx(s);
2502103285Sikob	ocb->ccb = NULL;
2503103285Sikob	return (ocb);
2504103285Sikob}
2505103285Sikob
2506103285Sikobstatic void
2507103285Sikobsbp_free_ocb(struct sbp_softc *sbp, struct sbp_ocb *ocb)
2508103285Sikob{
2509103285Sikob#if 0 /* XXX make sure that ocb has ccb */
2510103285Sikob	if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0 &&
2511103285Sikob	    (ocb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
2512103285Sikob		ocb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2513103285Sikob		sbp->flags &= ~SBP_RESOURCE_SHORTAGE;
2514103285Sikob	}
2515103285Sikob#else
2516103285Sikob	if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0)
2517103285Sikob		sbp->flags &= ~SBP_RESOURCE_SHORTAGE;
2518103285Sikob#endif
2519103285Sikob	ocb->flags = 0;
2520103285Sikob	ocb->ccb = NULL;
2521103285Sikob	STAILQ_INSERT_TAIL(&sbp->free_ocbs, ocb, ocb);
2522103285Sikob}
2523103285Sikob
2524103285Sikobstatic void
2525103285Sikobsbp_abort_ocb(struct sbp_ocb *ocb, int status)
2526103285Sikob{
2527103285Sikob	struct sbp_dev *sdev;
2528103285Sikob
2529103285Sikob	sdev = ocb->sdev;
2530110269SsimokawaSBP_DEBUG(1)
2531103285Sikob	sbp_show_sdev_info(sdev, 2);
2532103285Sikob	printf("sbp_abort_ocb 0x%x\n", status);
2533105792Ssimokawa	if (ocb->ccb != NULL)
2534105792Ssimokawa		sbp_print_scsi_cmd(ocb);
2535103285SikobEND_DEBUG
2536103285Sikob	if (ocb->ccb != NULL && !(ocb->flags & OCB_DONE)) {
2537110336Ssimokawa		untimeout(sbp_timeout, (caddr_t)ocb,
2538110336Ssimokawa					ocb->ccb->ccb_h.timeout_ch);
2539103285Sikob		ocb->ccb->ccb_h.status = status;
2540103285Sikob		xpt_done(ocb->ccb);
2541103285Sikob	}
2542103285Sikob	if (ocb->dmamap != NULL) {
2543103285Sikob		bus_dmamap_destroy(sdev->target->sbp->dmat, ocb->dmamap);
2544103285Sikob		ocb->dmamap = NULL;
2545103285Sikob	}
2546103285Sikob	sbp_free_ocb(sdev->target->sbp, ocb);
2547103285Sikob}
2548103285Sikob
2549103285Sikobstatic void
2550103285Sikobsbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2551103285Sikob{
2552103285Sikob	int s;
2553105792Ssimokawa	struct sbp_ocb *ocb, *next;
2554105792Ssimokawa	STAILQ_HEAD(, sbp_ocb) temp;
2555103285Sikob
2556103285Sikob	s = splfw();
2557105792Ssimokawa
2558105792Ssimokawa	bcopy(&sdev->ocbs, &temp, sizeof(temp));
2559105792Ssimokawa	STAILQ_INIT(&sdev->ocbs);
2560105792Ssimokawa	for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2561105792Ssimokawa		next = STAILQ_NEXT(ocb, ocb);
2562103285Sikob		sbp_abort_ocb(ocb, status);
2563103285Sikob	}
2564105792Ssimokawa
2565103285Sikob	splx(s);
2566103285Sikob}
2567103285Sikob
2568103285Sikobstatic devclass_t sbp_devclass;
2569103285Sikob
2570103285Sikobstatic device_method_t sbp_methods[] = {
2571103285Sikob	/* device interface */
2572103285Sikob	DEVMETHOD(device_identify,	sbp_identify),
2573103285Sikob	DEVMETHOD(device_probe,		sbp_probe),
2574103285Sikob	DEVMETHOD(device_attach,	sbp_attach),
2575103285Sikob	DEVMETHOD(device_detach,	sbp_detach),
2576110145Ssimokawa	DEVMETHOD(device_shutdown,	sbp_shutdown),
2577103285Sikob
2578103285Sikob	{ 0, 0 }
2579103285Sikob};
2580103285Sikob
2581103285Sikobstatic driver_t sbp_driver = {
2582103285Sikob	"sbp",
2583103285Sikob	sbp_methods,
2584103285Sikob	sizeof(struct sbp_softc),
2585103285Sikob};
2586103285SikobDRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2587103285SikobMODULE_VERSION(sbp, 1);
2588103285SikobMODULE_DEPEND(sbp, firewire, 1, 1, 1);
2589103285SikobMODULE_DEPEND(sbp, cam, 1, 1, 1);
2590