sbp.c revision 110184
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 110184 2003-02-01 08:55:33Z 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
71103285Sikob#define SBP_NUM_TARGETS 8
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
76103285Sikob#define SBP_ESELECT_TIMEOUT 1
77103285Sikob#define SBP_BIND_HI 0x1
78103285Sikob#define SBP_DEV2ADDR(u, t, l)	\
79103285Sikob	((((u) & 0xff) << 16) | (((l) & 0xff) << 8) | (((t) & 0x3f) << 2))
80103285Sikob#define SBP_ADDR2TRG(a)	(((a) >> 2) & 0x3f)
81103285Sikob#define SBP_ADDR2LUN(a)	(((a) >> 8) & 0xff)
82103285Sikob
83103285Sikob#define ORB_NOTIFY	(1 << 31)
84103285Sikob#define	ORB_FMT_STD	(0 << 29)
85103285Sikob#define	ORB_FMT_VED	(2 << 29)
86103285Sikob#define	ORB_FMT_NOP	(3 << 29)
87103285Sikob#define	ORB_FMT_MSK	(3 << 29)
88103285Sikob#define	ORB_EXV		(1 << 28)
89103285Sikob/* */
90103285Sikob#define	ORB_CMD_IN	(1 << 27)
91103285Sikob/* */
92103285Sikob#define	ORB_CMD_SPD(x)	((x) << 24)
93103285Sikob#define	ORB_CMD_MAXP(x)	((x) << 20)
94103285Sikob#define	ORB_RCN_TMO(x)	((x) << 20)
95103285Sikob#define	ORB_CMD_PTBL	(1 << 19)
96103285Sikob#define	ORB_CMD_PSZ(x)	((x) << 16)
97103285Sikob
98103285Sikob#define	ORB_FUN_LGI	(0 << 16)
99103285Sikob#define	ORB_FUN_QLG	(1 << 16)
100103285Sikob#define	ORB_FUN_RCN	(3 << 16)
101103285Sikob#define	ORB_FUN_LGO	(7 << 16)
102103285Sikob#define	ORB_FUN_ATA	(0xb << 16)
103103285Sikob#define	ORB_FUN_ATS	(0xc << 16)
104103285Sikob#define	ORB_FUN_LUR	(0xe << 16)
105103285Sikob#define	ORB_FUN_RST	(0xf << 16)
106103285Sikob#define	ORB_FUN_MSK	(0xf << 16)
107103285Sikob
108103285Sikobstatic char *orb_fun_name[] = {
109103285Sikob	/* 0 */ "LOGIN",
110103285Sikob	/* 1 */ "QUERY LOGINS",
111103285Sikob	/* 2 */ "Reserved",
112103285Sikob	/* 3 */ "RECONNECT",
113103285Sikob	/* 4 */ "SET PASSWORD",
114103285Sikob	/* 5 */ "Reserved",
115103285Sikob	/* 6 */ "Reserved",
116103285Sikob	/* 7 */ "LOGOUT",
117103285Sikob	/* 8 */ "Reserved",
118103285Sikob	/* 9 */ "Reserved",
119103285Sikob	/* A */ "Reserved",
120103285Sikob	/* B */ "ABORT TASK",
121103285Sikob	/* C */ "ABORT TASK SET",
122103285Sikob	/* D */ "Reserved",
123103285Sikob	/* E */ "LOGICAL UNIT RESET",
124103285Sikob	/* F */ "TARGET RESET"
125103285Sikob};
126103285Sikob
127103285Sikob#define ORB_RES_CMPL 0
128103285Sikob#define ORB_RES_FAIL 1
129103285Sikob#define ORB_RES_ILLE 2
130103285Sikob#define ORB_RES_VEND 3
131103285Sikob
132107653Ssimokawastatic int debug = 0;
133103285Sikobstatic int auto_login = 1;
134103285Sikobstatic int max_speed = 2;
135103285Sikob
136103285SikobSYSCTL_DECL(_hw_firewire);
137103285SikobSYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem");
138103285SikobSYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0,
139103285Sikob	"SBP debug flag");
140103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0,
141103285Sikob	"SBP perform login automatically");
142103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0,
143103285Sikob	"SBP transfer max speed");
144103285Sikob
145103285Sikob#define SBP_DEBUG(x)	if (debug > x) {
146103285Sikob#define END_DEBUG	}
147103285Sikob
148103285Sikob#define NEED_RESPONSE 0
149103285Sikob
150103285Sikobstruct ind_ptr {
151103285Sikob	u_int32_t hi,lo;
152103285Sikob};
153103285Sikob#define SBP_IND_MAX 0x20
154103285Sikobstruct sbp_ocb {
155103285Sikob	STAILQ_ENTRY(sbp_ocb)	ocb;
156103285Sikob	union ccb	*ccb;
157103285Sikob	volatile u_int32_t	orb[8];
158103285Sikob	volatile struct ind_ptr  ind_ptr[SBP_IND_MAX];
159103285Sikob	struct sbp_dev	*sdev;
160103285Sikob	int		flags;
161103285Sikob	bus_dmamap_t	dmamap;
162103285Sikob};
163103285Sikob#define OCB_ACT_MGM 0
164103285Sikob#define OCB_ACT_CMD 1
165103285Sikob#define OCB_ACT_MASK 3
166103285Sikob#define OCB_RESERVED 0x10
167103285Sikob#define OCB_DONE 0x20
168103285Sikob
169103285Sikob#define SBP_RESOURCE_SHORTAGE 0x10
170103285Sikob
171103285Sikobstruct sbp_login_res{
172103285Sikob	u_int16_t	len;
173103285Sikob	u_int16_t	id;
174103285Sikob	u_int16_t	res0;
175103285Sikob	u_int16_t	cmd_hi;
176103285Sikob	u_int32_t	cmd_lo;
177103285Sikob	u_int16_t	res1;
178103285Sikob	u_int16_t	recon_hold;
179103285Sikob};
180103285Sikobstruct sbp_status{
181103285Sikob	u_int8_t	len:3,
182103285Sikob			dead:1,
183103285Sikob			resp:2,
184103285Sikob			src:2;
185103285Sikob	u_int8_t	status:8;
186103285Sikob	u_int16_t	orb_hi;
187103285Sikob	u_int32_t	orb_lo;
188103285Sikob	u_int32_t	data[6];
189103285Sikob};
190103285Sikobstruct sbp_cmd_status{
191103285Sikob#define SBP_SFMT_CURR 0
192103285Sikob#define SBP_SFMT_DEFER 1
193103285Sikob	u_int8_t	status:6,
194103285Sikob			sfmt:2;
195103285Sikob	u_int8_t	s_key:4,
196103285Sikob			ill_len:1,
197103285Sikob			eom:1,
198103285Sikob			mark:1,
199103285Sikob			valid:1;
200103285Sikob	u_int8_t	s_code;
201103285Sikob	u_int8_t	s_qlfr;
202103285Sikob	u_int32_t	info;
203103285Sikob	u_int32_t	cdb;
204103285Sikob	u_int32_t	fru:8,
205103285Sikob			s_keydep:24;
206103285Sikob	u_int32_t	vend[2];
207103285Sikob};
208103285Sikob
209103285Sikobstruct sbp_dev{
210103285Sikob#define SBP_DEV_RESET		0	/* accept login */
211103285Sikob#define SBP_DEV_LOGIN		1	/* to login */
212103285Sikob#define SBP_DEV_RECONN		2	/* to reconnect */
213103285Sikob#define SBP_DEV_TOATTACH	3	/* to attach */
214103285Sikob#define SBP_DEV_PROBE		4	/* scan lun */
215103285Sikob#define SBP_DEV_ATTACHED	5	/* in operation */
216103285Sikob#define SBP_DEV_DEAD		6	/* unavailable unit */
217103285Sikob#define SBP_DEV_RETRY		7	/* unavailable unit */
218108503Ssimokawa	u_int8_t status;
219108503Ssimokawa	u_int8_t type;
220108503Ssimokawa	u_int16_t lun_id;
221103285Sikob	struct cam_path *path;
222103285Sikob	struct sbp_target *target;
223103285Sikob	struct sbp_login_res login;
224103285Sikob	STAILQ_HEAD(, sbp_ocb) ocbs;
225103285Sikob	char vendor[32];
226103285Sikob	char product[32];
227103285Sikob	char revision[10];
228103285Sikob};
229103285Sikob
230103285Sikobstruct sbp_target {
231103285Sikob	int target_id;
232103285Sikob	int num_lun;
233103285Sikob	struct sbp_dev	*luns;
234103285Sikob	struct sbp_softc *sbp;
235103285Sikob	struct fw_device *fwdev;
236103285Sikob	u_int32_t mgm_hi, mgm_lo;
237103285Sikob};
238103285Sikob
239103285Sikobstruct sbp_softc {
240103285Sikob	struct firewire_dev_comm fd;
241103285Sikob	unsigned char flags;
242103285Sikob	struct cam_sim  *sim;
243103285Sikob	struct sbp_target targets[SBP_NUM_TARGETS];
244103285Sikob	struct fw_bind fwb;
245103285Sikob	STAILQ_HEAD(, sbp_ocb) free_ocbs;
246103285Sikob	struct sbp_ocb *ocb;
247103285Sikob	bus_dma_tag_t	dmat;
248103285Sikob};
249103285Sikobstatic void sbp_post_explore __P((void *));
250103285Sikobstatic void sbp_recv __P((struct fw_xfer *));
251103285Sikobstatic void sbp_login_callback __P((struct fw_xfer *));
252103285Sikobstatic void sbp_cmd_callback __P((struct fw_xfer *));
253103285Sikobstatic void sbp_orb_pointer __P((struct sbp_dev *, struct sbp_ocb *));
254103285Sikobstatic void sbp_execute_ocb __P((void *,  bus_dma_segment_t *, int, int));
255103285Sikobstatic void sbp_free_ocb __P((struct sbp_softc *, struct sbp_ocb *));
256103285Sikobstatic void sbp_abort_ocb __P((struct sbp_ocb *, int));
257103285Sikobstatic void sbp_abort_all_ocbs __P((struct sbp_dev *, int));
258103285Sikobstatic struct fw_xfer * sbp_write_cmd __P((struct sbp_dev *, int, int));
259103285Sikobstatic struct sbp_ocb * sbp_get_ocb __P((struct sbp_softc *));
260103285Sikobstatic struct sbp_ocb * sbp_enqueue_ocb __P((struct sbp_dev *, struct sbp_ocb *));
261103285Sikobstatic struct sbp_ocb * sbp_dequeue_ocb __P((struct sbp_dev *, u_int32_t));
262110145Ssimokawastatic void sbp_cam_detach_target __P((struct sbp_target *));
263103285Sikobstatic void sbp_timeout __P((void *arg));
264103285Sikobstatic void sbp_mgm_orb __P((struct sbp_dev *, int));
265103285Sikob
266108281SsimokawaMALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire");
267103285Sikob
268103285Sikob/* cam related functions */
269103285Sikobstatic void	sbp_action(struct cam_sim *sim, union ccb *ccb);
270103285Sikobstatic void	sbp_poll(struct cam_sim *sim);
271103285Sikobstatic void	sbp_cam_callback(struct cam_periph *periph,
272103285Sikob					union ccb *ccb);
273103285Sikobstatic void	sbp_cam_scan_lun(struct sbp_dev *sdev);
274103285Sikob
275103285Sikobstatic char *orb_status0[] = {
276103285Sikob	/* 0 */ "No additional information to report",
277103285Sikob	/* 1 */ "Request type not supported",
278103285Sikob	/* 2 */ "Speed not supported",
279103285Sikob	/* 3 */ "Page size not supported",
280103285Sikob	/* 4 */ "Access denied",
281103285Sikob	/* 5 */ "Logical unit not supported",
282103285Sikob	/* 6 */ "Maximum payload too small",
283103285Sikob	/* 7 */ "Reserved for future standardization",
284103285Sikob	/* 8 */ "Resources unavailable",
285103285Sikob	/* 9 */ "Function rejected",
286103285Sikob	/* A */ "Login ID not recognized",
287103285Sikob	/* B */ "Dummy ORB completed",
288103285Sikob	/* C */ "Request aborted",
289103285Sikob	/* FF */ "Unspecified error"
290103285Sikob#define MAX_ORB_STATUS0 0xd
291103285Sikob};
292103285Sikob
293103285Sikobstatic char *orb_status1_object[] = {
294103285Sikob	/* 0 */ "Operation request block (ORB)",
295103285Sikob	/* 1 */ "Data buffer",
296103285Sikob	/* 2 */ "Page table",
297103285Sikob	/* 3 */ "Unable to specify"
298103285Sikob};
299103285Sikob
300103285Sikobstatic char *orb_status1_serial_bus_error[] = {
301103285Sikob	/* 0 */ "Missing acknowledge",
302103285Sikob	/* 1 */ "Reserved; not to be used",
303103285Sikob	/* 2 */ "Time-out error",
304103285Sikob	/* 3 */ "Reserved; not to be used",
305103285Sikob	/* 4 */ "Busy retry limit exceeded(X)",
306103285Sikob	/* 5 */ "Busy retry limit exceeded(A)",
307103285Sikob	/* 6 */ "Busy retry limit exceeded(B)",
308103285Sikob	/* 7 */ "Reserved for future standardization",
309103285Sikob	/* 8 */ "Reserved for future standardization",
310103285Sikob	/* 9 */ "Reserved for future standardization",
311103285Sikob	/* A */ "Reserved for future standardization",
312103285Sikob	/* B */ "Tardy retry limit exceeded",
313103285Sikob	/* C */ "Conflict error",
314103285Sikob	/* D */ "Data error",
315103285Sikob	/* E */ "Type error",
316103285Sikob	/* F */ "Address error"
317103285Sikob};
318103285Sikob
319103285Sikobstatic void
320103285Sikobsbp_identify(driver_t *driver, device_t parent)
321103285Sikob{
322103285Sikob	device_t child;
323103285SikobSBP_DEBUG(0)
324103285Sikob	printf("sbp_identify\n");
325103285SikobEND_DEBUG
326103285Sikob
327103285Sikob	child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent));
328103285Sikob}
329103285Sikob
330103285Sikob/*
331103285Sikob * sbp_probe()
332103285Sikob */
333103285Sikobstatic int
334103285Sikobsbp_probe(device_t dev)
335103285Sikob{
336103285Sikob	device_t pa;
337103285Sikob
338103285SikobSBP_DEBUG(0)
339103285Sikob	printf("sbp_probe\n");
340103285SikobEND_DEBUG
341103285Sikob
342103285Sikob	pa = device_get_parent(dev);
343103285Sikob	if(device_get_unit(dev) != device_get_unit(pa)){
344103285Sikob		return(ENXIO);
345103285Sikob	}
346103285Sikob
347103285Sikob	device_set_desc(dev, "SBP2/SCSI over firewire");
348103285Sikob	return (0);
349103285Sikob}
350103285Sikob
351103285Sikobstatic void
352103285Sikobsbp_show_sdev_info(struct sbp_dev *sdev, int new)
353103285Sikob{
354103285Sikob	struct fw_device *fwdev;
355103285Sikob
356103285Sikob	printf("%s:%d:%d ",
357103285Sikob		device_get_nameunit(sdev->target->sbp->fd.dev),
358103285Sikob		sdev->target->target_id,
359103285Sikob		sdev->lun_id
360103285Sikob	);
361103285Sikob	if (new == 2) {
362103285Sikob		return;
363103285Sikob	}
364103285Sikob	fwdev = sdev->target->fwdev;
365103285Sikob	printf("ordered:%d type:%d EUI:%08x%08x node:%d "
366103285Sikob		"speed:%d maxrec:%d",
367108503Ssimokawa		(sdev->type & 0x40) >> 6,
368108503Ssimokawa		(sdev->type & 0x1f),
369103285Sikob		fwdev->eui.hi,
370103285Sikob		fwdev->eui.lo,
371103285Sikob		fwdev->dst,
372103285Sikob		fwdev->speed,
373103285Sikob		fwdev->maxrec
374103285Sikob	);
375103285Sikob	if (new)
376103285Sikob		printf(" new!\n");
377103285Sikob	else
378103285Sikob		printf("\n");
379103285Sikob	sbp_show_sdev_info(sdev, 2);
380103285Sikob	printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision);
381103285Sikob}
382103285Sikob
383110184Ssimokawastatic struct {
384110184Ssimokawa	int bus;
385110184Ssimokawa	int target;
386110184Ssimokawa	struct fw_eui64 eui;
387110184Ssimokawa} wired[] = {
388110184Ssimokawa	/* Bus	Target	EUI64 */
389110184Ssimokawa#if 0
390110184Ssimokawa	{0,	2,	{0x00018ea0, 0x01fd0154}},	/* Logitec HDD */
391110184Ssimokawa	{0,	0,	{0x00018ea6, 0x00100682}},	/* Logitec DVD */
392110184Ssimokawa	{0,	1,	{0x00d03200, 0xa412006a}},	/* Yano HDD */
393110184Ssimokawa#endif
394110184Ssimokawa	{-1,	-1,	{0,0}}
395110184Ssimokawa};
396110184Ssimokawa
397110184Ssimokawastatic int
398110184Ssimokawasbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev)
399110184Ssimokawa{
400110184Ssimokawa	int bus, i, target=-1;
401110184Ssimokawa	char w[SBP_NUM_TARGETS];
402110184Ssimokawa
403110184Ssimokawa	bzero(w, sizeof(w));
404110184Ssimokawa	bus = device_get_unit(sbp->fd.dev);
405110184Ssimokawa
406110184Ssimokawa	/* XXX wired-down configuration should be gotten from
407110184Ssimokawa					tunable or device hint */
408110184Ssimokawa	for (i = 0; wired[i].bus >= 0; i ++) {
409110184Ssimokawa		if (wired[i].bus == bus) {
410110184Ssimokawa			w[wired[i].target] = 1;
411110184Ssimokawa			if (wired[i].eui.hi == fwdev->eui.hi &&
412110184Ssimokawa					wired[i].eui.lo == fwdev->eui.lo)
413110184Ssimokawa				target = wired[i].target;
414110184Ssimokawa		}
415110184Ssimokawa	}
416110184Ssimokawa	if (target >= 0) {
417110184Ssimokawa		if(target < SBP_NUM_TARGETS &&
418110184Ssimokawa				sbp->targets[target].fwdev == NULL)
419110184Ssimokawa			return(target);
420110184Ssimokawa		device_printf(sbp->fd.dev,
421110184Ssimokawa			"target %d is not free for %08x:%08x\n",
422110184Ssimokawa			target, fwdev->eui.hi, fwdev->eui.lo);
423110184Ssimokawa		target = -1;
424110184Ssimokawa	}
425110184Ssimokawa	/* non-wired target */
426110184Ssimokawa	for (i = 0; i < SBP_NUM_TARGETS; i ++)
427110184Ssimokawa		if (sbp->targets[i].fwdev == NULL && w[i] == 0) {
428110184Ssimokawa			target = i;
429110184Ssimokawa			break;
430110184Ssimokawa		}
431110184Ssimokawa
432110184Ssimokawa	return target;
433110184Ssimokawa}
434110184Ssimokawa
435103285Sikobstatic struct sbp_target *
436103285Sikobsbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
437103285Sikob{
438108503Ssimokawa	int i, maxlun, lun;
439103285Sikob	struct sbp_target *target;
440103285Sikob	struct sbp_dev *sdev;
441108503Ssimokawa	struct crom_context cc;
442108503Ssimokawa	struct csrreg *reg;
443103285Sikob
444103285SikobSBP_DEBUG(1)
445103285Sikob	printf("sbp_alloc_target\n");
446103285SikobEND_DEBUG
447110184Ssimokawa	i = sbp_new_target(sbp, fwdev);
448110184Ssimokawa	if (i < 0) {
449110184Ssimokawa		device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n");
450103285Sikob		return NULL;
451103285Sikob	}
452103285Sikob	/* new target */
453103285Sikob	target = &sbp->targets[i];
454103285Sikob	target->sbp = sbp;
455103285Sikob	target->fwdev = fwdev;
456103285Sikob	target->target_id = i;
457103285Sikob	if((target->mgm_lo = getcsrdata(fwdev, 0x54)) == 0 ){
458103285Sikob		/* bad target */
459103285Sikob		printf("NULL management address\n");
460103285Sikob		target->fwdev = NULL;
461103285Sikob		return NULL;
462103285Sikob	}
463103285Sikob	target->mgm_hi = 0xffff;
464103285Sikob	target->mgm_lo = 0xf0000000 | target->mgm_lo << 2;
465103285Sikob	/* XXX num_lun may be changed. realloc luns? */
466108503Ssimokawa	crom_init_context(&cc, target->fwdev->csrrom);
467108503Ssimokawa	/* XXX shoud parse appropriate unit directories only */
468108503Ssimokawa	maxlun = -1;
469108503Ssimokawa	while (cc.depth >= 0) {
470108503Ssimokawa		reg = crom_search_key(&cc, CROM_LUN);
471108503Ssimokawa		if (reg == NULL)
472108503Ssimokawa			break;
473108503Ssimokawa		lun = reg->val & 0xff;
474108642SsimokawaSBP_DEBUG(0)
475108642Ssimokawa		printf("target %d lun %d found\n", target->target_id, lun);
476108642SsimokawaEND_DEBUG
477108503Ssimokawa		if (maxlun < lun)
478108503Ssimokawa			maxlun = lun;
479108503Ssimokawa		crom_next(&cc);
480108503Ssimokawa	}
481108503Ssimokawa	target->num_lun = maxlun + 1;
482108503Ssimokawa	if (maxlun < 0) {
483108503Ssimokawa		printf("no lun found!\n");
484108503Ssimokawa	}
485103285Sikob	target->luns = (struct sbp_dev *) malloc(
486103285Sikob				sizeof(struct sbp_dev) * target->num_lun,
487103285Sikob				M_SBP, M_NOWAIT | M_ZERO);
488103285Sikob	for (i = 0; i < target->num_lun; i++) {
489103285Sikob		sdev = &target->luns[i];
490103285Sikob		sdev->lun_id = i;
491103285Sikob		sdev->target = target;
492103285Sikob		STAILQ_INIT(&sdev->ocbs);
493108503Ssimokawa		sdev->status = SBP_DEV_DEAD;
494103285Sikob	}
495108503Ssimokawa	crom_init_context(&cc, target->fwdev->csrrom);
496108503Ssimokawa	while (cc.depth >= 0) {
497108503Ssimokawa		reg = crom_search_key(&cc, CROM_LUN);
498108503Ssimokawa		if (reg == NULL)
499108503Ssimokawa			break;
500108503Ssimokawa		lun = reg->val & 0xff;
501108503Ssimokawa		target->luns[lun].status = SBP_DEV_RESET;
502108503Ssimokawa		target->luns[lun].type = (reg->val & 0x0f00) >> 16;
503108503Ssimokawa		crom_next(&cc);
504108503Ssimokawa	}
505103285Sikob	return target;
506103285Sikob}
507103285Sikob
508103285Sikobstatic void
509103285Sikobsbp_get_text_leaf(struct fw_device *fwdev, int key, char *buf, int len)
510103285Sikob{
511103285Sikob	static char *nullstr = "(null)";
512103285Sikob	int i, clen, found=0;
513103285Sikob	struct csrhdr *chdr;
514103285Sikob	struct csrreg *creg;
515103285Sikob	u_int32_t *src, *dst;
516103285Sikob
517103285Sikob	chdr = (struct csrhdr *)&fwdev->csrrom[0];
518103285Sikob	creg = (struct csrreg *)chdr;
519103285Sikob	creg += chdr->info_len;
520103285Sikob	for( i = chdr->info_len + 4; i <= fwdev->rommax; i+=4){
521103285Sikob		if((creg++)->key == key){
522103285Sikob			found = 1;
523103285Sikob			break;
524103285Sikob		}
525103285Sikob	}
526103285Sikob	if (!found) {
527103285Sikob		strncpy(buf, nullstr, len);
528103285Sikob		return;
529103285Sikob	}
530103285Sikob	src = (u_int32_t *) creg + creg->val;
531103285Sikob	clen = ((*src >> 16) - 2) * 4;
532103285Sikob	src += 3;
533103285Sikob	dst = (u_int32_t *) buf;
534103285Sikob	if (len < clen)
535103285Sikob		clen = len;
536103285Sikob	for (i = 0; i < clen/4; i++)
537103285Sikob		*dst++ = htonl(*src++);
538103285Sikob	buf[clen] = 0;
539103285Sikob}
540103285Sikob
541103285Sikobstatic void
542103285Sikobsbp_probe_lun(struct sbp_dev *sdev)
543103285Sikob{
544103285Sikob	struct fw_device *fwdev;
545103285Sikob	int rev;
546103285Sikob
547103285Sikob	fwdev = sdev->target->fwdev;
548103285Sikob	bzero(sdev->vendor, sizeof(sdev->vendor));
549103285Sikob	bzero(sdev->product, sizeof(sdev->product));
550103285Sikob	sbp_get_text_leaf(fwdev, 0x03, sdev->vendor, sizeof(sdev->vendor));
551103285Sikob	sbp_get_text_leaf(fwdev, 0x17, sdev->product, sizeof(sdev->product));
552103285Sikob	rev = getcsrdata(sdev->target->fwdev, 0x3c);
553103285Sikob	snprintf(sdev->revision, sizeof(sdev->revision), "%06x", rev);
554103285Sikob}
555103285Sikobstatic void
556103285Sikobsbp_probe_target(struct sbp_target *target, int alive)
557103285Sikob{
558103285Sikob	struct sbp_softc *sbp;
559103285Sikob	struct sbp_dev *sdev;
560103285Sikob	struct firewire_comm *fc;
561103285Sikob	int i;
562103285Sikob
563103285SikobSBP_DEBUG(1)
564103285Sikob	printf("sbp_probe_target %d\n", target->target_id);
565103285Sikob	if (!alive)
566103285Sikob		printf("not alive\n");
567103285SikobEND_DEBUG
568103285Sikob
569103285Sikob	sbp = target->sbp;
570103285Sikob	fc = target->sbp->fd.fc;
571103285Sikob	for (i=0; i < target->num_lun; i++) {
572103285Sikob		sdev = &target->luns[i];
573103285Sikob		if (alive && (sdev->status != SBP_DEV_DEAD)) {
574103285Sikob			if (sdev->path != NULL) {
575103285Sikob				xpt_freeze_devq(sdev->path, 1);
576103285Sikob			}
577103285Sikob			sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ);
578103285Sikob			switch (sdev->status) {
579103285Sikob			case SBP_DEV_ATTACHED:
580103285Sikob				sbp_mgm_orb(sdev, ORB_FUN_RCN);
581103285Sikob				break;
582103285Sikob			case SBP_DEV_RETRY:
583103285Sikob				sbp_probe_lun(sdev);
584103285Sikob				sbp_mgm_orb(sdev, ORB_FUN_LGI);
585103285Sikob				break;
586103285Sikob			default:
587103285Sikob				/* new or revived target */
588103285Sikob				sbp_probe_lun(sdev);
589103285Sikob				if (auto_login) {
590103285Sikob					sdev->status = SBP_DEV_TOATTACH;
591103285Sikob					sbp_mgm_orb(sdev, ORB_FUN_LGI);
592103285Sikob				}
593103285Sikob				break;
594103285Sikob			}
595107653SsimokawaSBP_DEBUG(0)
596103285Sikob			sbp_show_sdev_info(sdev,
597103285Sikob					(sdev->status == SBP_DEV_TOATTACH));
598107653SsimokawaEND_DEBUG
599103285Sikob		} else {
600103285Sikob			switch (sdev->status) {
601103285Sikob			case SBP_DEV_ATTACHED:
602103285SikobSBP_DEBUG(0)
603103285Sikob				/* the device has gone */
604103285Sikob				sbp_show_sdev_info(sdev, 2);
605103285Sikob				printf("lost target\n");
606103285SikobEND_DEBUG
607103285Sikob				if (sdev->path)
608103285Sikob					xpt_freeze_devq(sdev->path, 1);
609103285Sikob				sdev->status = SBP_DEV_RETRY;
610103285Sikob				sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ);
611103285Sikob				break;
612103285Sikob			case SBP_DEV_PROBE:
613103285Sikob			case SBP_DEV_TOATTACH:
614103285Sikob				sdev->status = SBP_DEV_RESET;
615103285Sikob				break;
616103285Sikob			case SBP_DEV_RETRY:
617103285Sikob			case SBP_DEV_RESET:
618103285Sikob			case SBP_DEV_DEAD:
619103285Sikob				break;
620103285Sikob			}
621103285Sikob		}
622103285Sikob	}
623103285Sikob}
624103285Sikob
625103285Sikob#if 0
626103285Sikobstatic void
627103285Sikobsbp_release_queue(void *arg)
628103285Sikob{
629103285Sikob	struct sbp_softc *sbp;
630103285Sikob
631103285SikobSBP_DEBUG(0)
632103285Sikob	printf("sbp_release_queue\n");
633103285SikobEND_DEBUG
634103285Sikob	sbp = (struct sbp_softc *)arg;
635103285Sikob	xpt_release_simq(sbp->sim, 1);
636103285Sikob}
637103285Sikob
638103285Sikobstatic void
639103285Sikobsbp_release_devq(void *arg)
640103285Sikob{
641103285Sikob	struct sbp_dev *sdev;
642103285Sikob	int s;
643103285Sikob
644103285Sikob	sdev = (struct sbp_dev *)arg;
645103285SikobSBP_DEBUG(0)
646103285Sikob	sbp_show_sdev_info(sdev, 2);
647103285Sikob	printf("sbp_release_devq\n");
648103285SikobEND_DEBUG
649103285Sikob	s = splcam();
650103285Sikob	xpt_release_devq(sdev->path, 1, TRUE);
651103285Sikob	splx(s);
652103285Sikob}
653103285Sikob#endif
654103285Sikob
655103285Sikobstatic void
656103285Sikobsbp_post_explore(void *arg)
657103285Sikob{
658103285Sikob	struct sbp_softc *sbp = (struct sbp_softc *)arg;
659103285Sikob	struct sbp_target *target;
660103285Sikob	struct fw_device *fwdev;
661103285Sikob	int i, alive;
662103285Sikob
663103285SikobSBP_DEBUG(1)
664103285Sikob	printf("sbp_post_explore\n");
665103285SikobEND_DEBUG
666103285Sikob#if 0
667103285Sikob	xpt_freeze_simq(sbp->sim, /*count*/ 1);
668103285Sikob#endif
669103285Sikob	/* Gabage Collection */
670103285Sikob	for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
671103285Sikob		target = &sbp->targets[i];
672103285Sikob		for( fwdev  = TAILQ_FIRST(&sbp->fd.fc->devices);
673103285Sikob			fwdev != NULL; fwdev = TAILQ_NEXT(fwdev, link)){
674103285Sikob			if(target->fwdev == NULL) break;
675103285Sikob			if(target->fwdev == fwdev) break;
676103285Sikob		}
677103285Sikob		if(fwdev == NULL){
678103285Sikob			/* device has removed in lower driver */
679110145Ssimokawa			sbp_cam_detach_target(target);
680110145Ssimokawa			if (target->luns != NULL)
681110145Ssimokawa				free(target->luns, M_SBP);
682110145Ssimokawa			target->num_lun = 0;;
683110145Ssimokawa			target->luns = NULL;
684110145Ssimokawa			target->fwdev = NULL;
685103285Sikob		}
686103285Sikob	}
687103285Sikob	/* traverse device list */
688103285Sikob	for( fwdev  = TAILQ_FIRST(&sbp->fd.fc->devices);
689103285Sikob		fwdev != NULL; fwdev = TAILQ_NEXT(fwdev, link)){
690103285SikobSBP_DEBUG(0)
691103285Sikob		printf("sbp_post_explore: EUI:%08x%08x ",
692103285Sikob				fwdev->eui.hi, fwdev->eui.lo);
693103285Sikob		if (fwdev->status == FWDEVATTACHED) {
694103285Sikob			printf("spec=%d key=%d.\n",
695103285Sikob			getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10,
696103285Sikob			getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2);
697103285Sikob		} else {
698103285Sikob			printf("not attached, state=%d.\n", fwdev->status);
699103285Sikob		}
700103285SikobEND_DEBUG
701103285Sikob		alive = (fwdev->status == FWDEVATTACHED)
702103285Sikob			&& (getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10)
703103285Sikob			&& (getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2);
704103285Sikob		for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
705103285Sikob			target = &sbp->targets[i];
706103285Sikob			if(target->fwdev == fwdev ) {
707103285Sikob				/* known target */
708103285Sikob				break;
709103285Sikob			}
710103285Sikob		}
711103285Sikob		if(i == SBP_NUM_TARGETS){
712103285Sikob			if (alive) {
713103285Sikob				/* new target */
714103285Sikob				target = sbp_alloc_target(sbp, fwdev);
715103285Sikob				if (target == NULL)
716103285Sikob					continue;
717103285Sikob			} else {
718103285Sikob				continue;
719103285Sikob			}
720103285Sikob		}
721103285Sikob		sbp_probe_target(target, alive);
722103285Sikob	}
723103285Sikob#if 0
724103285Sikob	timeout(sbp_release_queue, (caddr_t)sbp, bus_reset_rest * hz / 1000);
725103285Sikob#endif
726103285Sikob}
727103285Sikob
728103285Sikob#if NEED_RESPONSE
729103285Sikobstatic void
730103285Sikobsbp_loginres_callback(struct fw_xfer *xfer){
731103285SikobSBP_DEBUG(1)
732103285Sikob	struct sbp_dev *sdev;
733103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
734103285Sikob	sbp_show_sdev_info(sdev, 2);
735103285Sikob	printf("sbp_loginres_callback\n");
736103285SikobEND_DEBUG
737103285Sikob	fw_xfer_free(xfer);
738103285Sikob	return;
739103285Sikob}
740103285Sikob#endif
741103285Sikob
742103285Sikobstatic void
743103285Sikobsbp_login_callback(struct fw_xfer *xfer)
744103285Sikob{
745103285SikobSBP_DEBUG(1)
746103285Sikob	struct sbp_dev *sdev;
747103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
748103285Sikob	sbp_show_sdev_info(sdev, 2);
749103285Sikob	printf("sbp_login_callback\n");
750103285SikobEND_DEBUG
751103285Sikob	fw_xfer_free(xfer);
752103285Sikob	return;
753103285Sikob}
754103285Sikob
755103285Sikobstatic void
756103285Sikobsbp_cmd_callback(struct fw_xfer *xfer)
757103285Sikob{
758103285SikobSBP_DEBUG(2)
759103285Sikob	struct sbp_dev *sdev;
760103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
761103285Sikob	sbp_show_sdev_info(sdev, 2);
762103285Sikob	printf("sbp_cmd_callback\n");
763103285SikobEND_DEBUG
764103285Sikob	fw_xfer_free(xfer);
765103285Sikob	return;
766103285Sikob}
767103285Sikob
768103285Sikobstatic void
769103285Sikobsbp_cam_callback(struct cam_periph *periph, union ccb *ccb)
770103285Sikob{
771103285Sikob	struct sbp_dev *sdev;
772103285Sikob	sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
773103285SikobSBP_DEBUG(1)
774103285Sikob	sbp_show_sdev_info(sdev, 2);
775103285Sikob	printf("sbp_cam_callback\n");
776103285SikobEND_DEBUG
777103285Sikob	sdev->status = SBP_DEV_ATTACHED;
778103285Sikob	free(ccb, M_SBP);
779103285Sikob}
780103285Sikob
781103285Sikobstatic void
782103285Sikobsbp_cam_scan_lun(struct sbp_dev *sdev)
783103285Sikob{
784109623Salfred	union ccb *ccb = malloc(sizeof(union ccb), M_SBP, M_ZERO);
785103285Sikob
786103285SikobSBP_DEBUG(0)
787103285Sikob	sbp_show_sdev_info(sdev, 2);
788103285Sikob	printf("sbp_cam_scan_lun\n");
789103285SikobEND_DEBUG
790103285Sikob	xpt_setup_ccb(&ccb->ccb_h, sdev->path, 5/*priority (low)*/);
791103285Sikob	ccb->ccb_h.func_code = XPT_SCAN_LUN;
792103285Sikob	ccb->ccb_h.cbfcnp = sbp_cam_callback;
793103285Sikob	ccb->crcn.flags = CAM_FLAG_NONE;
794103285Sikob	ccb->ccb_h.ccb_sdev_ptr = sdev;
795103285Sikob	xpt_action(ccb);
796103285Sikob
797103285Sikob	/* The scan is in progress now. */
798103285Sikob}
799103285Sikob
800103285Sikob
801103285Sikobstatic void
802103285Sikobsbp_ping_unit_callback(struct cam_periph *periph, union ccb *ccb)
803103285Sikob{
804103285Sikob	struct sbp_dev *sdev;
805103285Sikob	sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
806103285SikobSBP_DEBUG(1)
807103285Sikob	sbp_show_sdev_info(sdev, 2);
808103285Sikob	printf("sbp_ping_unit_callback\n");
809103285SikobEND_DEBUG
810103285Sikob	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
811103285Sikob		if (--ccb->ccb_h.retry_count == 0) {
812103285Sikob			sbp_show_sdev_info(sdev, 2);
813103285Sikob			printf("sbp_tur_callback: retry count exceeded\n");
814103285Sikob			sdev->status = SBP_DEV_RETRY;
815103285Sikob			free(ccb, M_SBP);
816103285Sikob		} else {
817103285Sikob			/* requeue */
818103285Sikob			xpt_action(ccb);
819103285Sikob			xpt_release_devq(sdev->path, 1, TRUE);
820103285Sikob		}
821103285Sikob	} else {
822103285Sikob		free(ccb->csio.data_ptr, M_SBP);
823103285Sikob		free(ccb, M_SBP);
824103285Sikob		sdev->status = SBP_DEV_ATTACHED;
825103285Sikob		xpt_release_devq(sdev->path, 1, TRUE);
826103285Sikob	}
827103285Sikob}
828103285Sikob
829103285Sikob/*
830103285Sikob * XXX Some devices need to execute inquiry or read_capacity
831103285Sikob * after bus_rest during busy transfer.
832103285Sikob * Otherwise they return incorrect result for READ(and WRITE?)
833103285Sikob * command without any SBP-II/SCSI error.
834103285Sikob *
835103285Sikob * e.g. Maxtor 3000XT, Yano A-dish.
836103285Sikob */
837103285Sikobstatic void
838103285Sikobsbp_ping_unit(struct sbp_dev *sdev)
839103285Sikob{
840103285Sikob	union ccb *ccb;
841103285Sikob	struct scsi_inquiry_data *inq_buf;
842103285Sikob
843109623Salfred	ccb = malloc(sizeof(union ccb), M_SBP, M_ZERO);
844103285Sikob	inq_buf = (struct scsi_inquiry_data *)
845109623Salfred			malloc(sizeof(*inq_buf), M_SBP, 0);
846103285Sikob
847103285SikobSBP_DEBUG(1)
848103285Sikob	sbp_show_sdev_info(sdev, 2);
849103285Sikob	printf("sbp_ping_unit\n");
850103285SikobEND_DEBUG
851103285Sikob
852103285Sikob	/*
853103285Sikob	 * We need to execute this command before any other queued command.
854103285Sikob	 * Make priority 0 and freeze queue after execution for retry.
855103285Sikob	 * cam's scan_lun command doesn't provide this feature.
856103285Sikob	 */
857103285Sikob	xpt_setup_ccb(&ccb->ccb_h, sdev->path, 0/*priority (high)*/);
858103285Sikob	scsi_inquiry(
859103285Sikob		&ccb->csio,
860103285Sikob		/*retries*/ 5,
861103285Sikob		sbp_ping_unit_callback,
862103285Sikob		MSG_SIMPLE_Q_TAG,
863103285Sikob		(u_int8_t *)inq_buf,
864103285Sikob		SHORT_INQUIRY_LENGTH,
865103285Sikob		/*evpd*/FALSE,
866103285Sikob		/*page_code*/0,
867103285Sikob		SSD_MIN_SIZE,
868103285Sikob		/*timeout*/60000
869103285Sikob	);
870103285Sikob	ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
871103285Sikob	xpt_action(ccb);
872103285Sikob}
873103285Sikob
874103285Sikobstatic void
875103285Sikobsbp_do_attach(struct fw_xfer *xfer)
876103285Sikob{
877103285Sikob	struct sbp_dev *sdev;
878103285Sikob
879103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
880103285SikobSBP_DEBUG(0)
881103285Sikob	sbp_show_sdev_info(sdev, 2);
882103285Sikob	printf("sbp_do_attach\n");
883103285SikobEND_DEBUG
884103285Sikob	fw_xfer_free(xfer);
885103285Sikob	if (sdev->path == NULL)
886103285Sikob		xpt_create_path(&sdev->path, xpt_periph,
887103285Sikob			cam_sim_path(sdev->target->sbp->sim),
888103285Sikob			sdev->target->target_id, sdev->lun_id);
889103285Sikob
890103285Sikob	if (sdev->status == SBP_DEV_RETRY) {
891103285Sikob		sdev->status = SBP_DEV_PROBE;
892103285Sikob		sbp_ping_unit(sdev);
893103285Sikob		/* freezed twice */
894103285Sikob		xpt_release_devq(sdev->path, 1, TRUE);
895103285Sikob	} else {
896103285Sikob		sdev->status = SBP_DEV_PROBE;
897103285Sikob		sbp_cam_scan_lun(sdev);
898103285Sikob	}
899103285Sikob	xpt_release_devq(sdev->path, 1, TRUE);
900103285Sikob	return;
901103285Sikob}
902103285Sikob
903103285Sikobstatic void
904103285Sikobsbp_agent_reset_callback(struct fw_xfer *xfer)
905103285Sikob{
906103285Sikob	struct sbp_dev *sdev;
907103285Sikob
908103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
909103285SikobSBP_DEBUG(1)
910103285Sikob	sbp_show_sdev_info(sdev, 2);
911103285Sikob	printf("sbp_cmd_callback\n");
912103285SikobEND_DEBUG
913103285Sikob	fw_xfer_free(xfer);
914103285Sikob	sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ);
915103285Sikob	if (sdev->path)
916103285Sikob		xpt_release_devq(sdev->path, 1, TRUE);
917103285Sikob}
918103285Sikob
919103285Sikobstatic void
920103285Sikobsbp_agent_reset(struct sbp_dev *sdev, int attach)
921103285Sikob{
922103285Sikob	struct fw_xfer *xfer;
923103285Sikob	struct fw_pkt *fp;
924103285Sikob
925103285SikobSBP_DEBUG(0)
926103285Sikob	sbp_show_sdev_info(sdev, 2);
927103285Sikob	printf("sbp_agent_reset\n");
928103285SikobEND_DEBUG
929103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
930103285Sikob	if (xfer == NULL)
931103285Sikob		return;
932103285Sikob	if (attach)
933103285Sikob		xfer->act.hand = sbp_do_attach;
934103285Sikob	else
935103285Sikob		xfer->act.hand = sbp_agent_reset_callback;
936103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
937103285Sikob	fp->mode.wreqq.data = htonl(0xf);
938103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
939103285Sikob}
940103285Sikob
941103285Sikobstatic void
942103285Sikobsbp_busy_timeout_callback(struct fw_xfer *xfer)
943103285Sikob{
944103285Sikob	struct sbp_dev *sdev;
945103285Sikob
946103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
947103285SikobSBP_DEBUG(1)
948103285Sikob	sbp_show_sdev_info(sdev, 2);
949103285Sikob	printf("sbp_but_timeout_callback\n");
950103285SikobEND_DEBUG
951103285Sikob	fw_xfer_free(xfer);
952103285Sikob	sbp_agent_reset(sdev, 1);
953103285Sikob}
954103285Sikob
955103285Sikobstatic void
956103285Sikobsbp_busy_timeout(struct sbp_dev *sdev)
957103285Sikob{
958103285Sikob	struct fw_pkt *fp;
959103285Sikob	struct fw_xfer *xfer;
960103285SikobSBP_DEBUG(0)
961103285Sikob	sbp_show_sdev_info(sdev, 2);
962103285Sikob	printf("sbp_busy_timeout\n");
963103285SikobEND_DEBUG
964103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
965103285Sikob
966103285Sikob	xfer->act.hand = sbp_busy_timeout_callback;
967103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
968103285Sikob	fp->mode.wreqq.dest_hi = htons(0xffff);
969110129Ssimokawa	fp->mode.wreqq.dest_lo = htonl(0xf0000000 | BUSY_TIMEOUT);
970110129Ssimokawa	fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
971103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
972103285Sikob}
973103285Sikob
974103285Sikob#if 0
975103285Sikobstatic void
976103285Sikobsbp_reset_start(struct sbp_dev *sdev)
977103285Sikob{
978103285Sikob	struct fw_xfer *xfer;
979103285Sikob	struct fw_pkt *fp;
980103285Sikob
981103285SikobSBP_DEBUG(0)
982103285Sikob	sbp_show_sdev_info(sdev, 2);
983103285Sikob	printf("sbp_reset_start\n");
984103285SikobEND_DEBUG
985103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
986103285Sikob
987103285Sikob	xfer->act.hand = sbp_busy_timeout;
988103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
989103285Sikob	fp->mode.wreqq.dest_hi = htons(0xffff);
990103285Sikob	fp->mode.wreqq.dest_lo = htonl(0xf0000000 | RESET_START);
991103285Sikob	fp->mode.wreqq.data = htonl(0xf);
992103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
993103285Sikob}
994103285Sikob#endif
995103285Sikob
996103285Sikobstatic void
997103285Sikobsbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
998103285Sikob{
999103285Sikob	struct fw_xfer *xfer;
1000103285Sikob	struct fw_pkt *fp;
1001103285SikobSBP_DEBUG(2)
1002103285Sikob	sbp_show_sdev_info(sdev, 2);
1003103285Sikob	printf("sbp_orb_pointer\n");
1004103285SikobEND_DEBUG
1005103285Sikob
1006103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1007103285Sikob	if (xfer == NULL)
1008103285Sikob		return;
1009103285Sikob	xfer->act.hand = sbp_cmd_callback;
1010103285Sikob
1011103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1012103285Sikob	fp->mode.wreqb.len = htons(8);
1013103285Sikob	fp->mode.wreqb.extcode = 0;
1014103285Sikob	fp->mode.wreqb.payload[0] =
1015103285Sikob		htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1016103285Sikob	fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0]));
1017103285Sikob
1018103285Sikob	if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1019103285Sikob			fw_xfer_free(xfer);
1020103285Sikob			ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1021103285Sikob			xpt_done(ocb->ccb);
1022103285Sikob	}
1023103285Sikob}
1024103285Sikob
1025103285Sikobstatic void
1026103285Sikobsbp_doorbell(struct sbp_dev *sdev)
1027103285Sikob{
1028103285Sikob	struct fw_xfer *xfer;
1029103285Sikob	struct fw_pkt *fp;
1030103285SikobSBP_DEBUG(1)
1031103285Sikob	sbp_show_sdev_info(sdev, 2);
1032103285Sikob	printf("sbp_doorbell\n");
1033103285SikobEND_DEBUG
1034103285Sikob
1035103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1036103285Sikob	if (xfer == NULL)
1037103285Sikob		return;
1038103285Sikob	xfer->act.hand = sbp_cmd_callback;
1039103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1040103285Sikob	fp->mode.wreqq.data = htonl(0xf);
1041103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1042103285Sikob}
1043103285Sikob
1044103285Sikobstatic struct fw_xfer *
1045103285Sikobsbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1046103285Sikob{
1047103285Sikob	struct fw_xfer *xfer;
1048103285Sikob	struct fw_pkt *fp;
1049103285Sikob
1050103285Sikob	xfer = fw_xfer_alloc();
1051103285Sikob	if(xfer == NULL){
1052103285Sikob		return NULL;
1053103285Sikob	}
1054103285Sikob	if (tcode == FWTCODE_WREQQ)
1055103285Sikob		xfer->send.len = 16;
1056103285Sikob	else
1057103285Sikob		xfer->send.len = 24;
1058103285Sikob
1059103285Sikob	xfer->send.buf = malloc(xfer->send.len, M_DEVBUF, M_NOWAIT);
1060103285Sikob	if(xfer->send.buf == NULL){
1061103285Sikob		fw_xfer_free( xfer);
1062103285Sikob		return NULL;
1063103285Sikob	}
1064103285Sikob
1065103285Sikob	xfer->send.off = 0;
1066103285Sikob	xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1067103285Sikob	xfer->sc = (caddr_t)sdev;
1068103285Sikob	xfer->fc = sdev->target->sbp->fd.fc;
1069103285Sikob	xfer->retry_req = fw_asybusy;
1070103285Sikob
1071103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1072103285Sikob	fp->mode.wreqq.dest_hi = htons(sdev->login.cmd_hi);
1073103285Sikob	fp->mode.wreqq.dest_lo = htonl(sdev->login.cmd_lo + offset);
1074103285Sikob	fp->mode.wreqq.tlrt = 0;
1075103285Sikob	fp->mode.wreqq.tcode = tcode;
1076103285Sikob	fp->mode.wreqq.pri = 0;
1077103285Sikob	xfer->dst = FWLOCALBUS | sdev->target->fwdev->dst;
1078103285Sikob	fp->mode.wreqq.dst = htons(xfer->dst);
1079103285Sikob
1080103285Sikob	return xfer;
1081103285Sikob
1082103285Sikob}
1083103285Sikob
1084103285Sikobstatic void
1085103285Sikobsbp_mgm_orb(struct sbp_dev *sdev, int func)
1086103285Sikob{
1087103285Sikob	struct fw_xfer *xfer;
1088103285Sikob	struct fw_pkt *fp;
1089103285Sikob	struct sbp_ocb *ocb;
1090103285Sikob	int s, nid;
1091103285Sikob
1092103285Sikob	if ((ocb = sbp_get_ocb(sdev->target->sbp)) == NULL) {
1093103285Sikob		s = splfw();
1094103285Sikob		sdev->target->sbp->flags |= SBP_RESOURCE_SHORTAGE;
1095103285Sikob		splx(s);
1096103285Sikob		return;
1097103285Sikob	}
1098103285Sikob	ocb->flags = OCB_ACT_MGM;
1099103285Sikob	ocb->sdev = sdev;
1100103285Sikob	ocb->ccb = NULL;
1101103285Sikob
1102103285Sikob	nid = sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS;
1103103285Sikob	bzero((void *)(uintptr_t)(volatile void *)ocb->orb, sizeof(ocb->orb));
1104103285Sikob	ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1105103285Sikob	ocb->orb[7] = htonl(SBP_DEV2ADDR(
1106103285Sikob		device_get_unit(sdev->target->sbp->fd.dev),
1107103285Sikob		sdev->target->target_id,
1108103285Sikob		sdev->lun_id));
1109103285Sikob
1110107653SsimokawaSBP_DEBUG(0)
1111103285Sikob	sbp_show_sdev_info(sdev, 2);
1112103285Sikob	printf("%s\n", orb_fun_name[(func>>16)&0xf]);
1113107653SsimokawaEND_DEBUG
1114103285Sikob	switch (func) {
1115103285Sikob	case ORB_FUN_LGI:
1116103285Sikob		ocb->orb[2] = htonl(nid << 16);
1117103285Sikob		ocb->orb[3] = htonl(vtophys(&sdev->login));
1118103285Sikob		ocb->orb[4] = htonl(ORB_NOTIFY | ORB_EXV | sdev->lun_id);
1119103285Sikob		ocb->orb[5] = htonl(sizeof(struct sbp_login_res));
1120103285Sikob		break;
1121103285Sikob	case ORB_FUN_RCN:
1122103285Sikob	case ORB_FUN_LGO:
1123103285Sikob	case ORB_FUN_LUR:
1124103285Sikob	case ORB_FUN_RST:
1125103285Sikob	case ORB_FUN_ATA:
1126103285Sikob	case ORB_FUN_ATS:
1127103285Sikob		ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login.id);
1128103285Sikob		break;
1129103285Sikob	}
1130103285Sikob
1131103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1132103285Sikob	if(xfer == NULL){
1133103285Sikob		return;
1134103285Sikob	}
1135103285Sikob	xfer->act.hand = sbp_login_callback;
1136103285Sikob
1137103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1138103285Sikob	fp->mode.wreqb.dest_hi = htons(sdev->target->mgm_hi);
1139103285Sikob	fp->mode.wreqb.dest_lo = htonl(sdev->target->mgm_lo);
1140103285Sikob	fp->mode.wreqb.len = htons(8);
1141103285Sikob	fp->mode.wreqb.extcode = 0;
1142108503Ssimokawa	fp->mode.wreqb.payload[0] = htonl(nid << 16);
1143103285Sikob	fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0]));
1144103285Sikob	sbp_enqueue_ocb(sdev, ocb);
1145103285Sikob
1146103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1147103285Sikob}
1148103285Sikob
1149103285Sikobstatic void
1150105792Ssimokawasbp_print_scsi_cmd(struct sbp_ocb *ocb)
1151103285Sikob{
1152103285Sikob	struct ccb_scsiio *csio;
1153103285Sikob
1154103285Sikob	csio = &ocb->ccb->csio;
1155103285Sikob	printf("%s:%d:%d XPT_SCSI_IO: "
1156103285Sikob		"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1157103285Sikob		", flags: 0x%02x, "
1158103285Sikob		"%db cmd/%db data/%db sense\n",
1159103285Sikob		device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1160103285Sikob		ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun,
1161103285Sikob		csio->cdb_io.cdb_bytes[0],
1162103285Sikob		csio->cdb_io.cdb_bytes[1],
1163103285Sikob		csio->cdb_io.cdb_bytes[2],
1164103285Sikob		csio->cdb_io.cdb_bytes[3],
1165103285Sikob		csio->cdb_io.cdb_bytes[4],
1166103285Sikob		csio->cdb_io.cdb_bytes[5],
1167103285Sikob		csio->cdb_io.cdb_bytes[6],
1168103285Sikob		csio->cdb_io.cdb_bytes[7],
1169103285Sikob		csio->cdb_io.cdb_bytes[8],
1170103285Sikob		csio->cdb_io.cdb_bytes[9],
1171103285Sikob		ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1172103285Sikob		csio->cdb_len, csio->dxfer_len,
1173103285Sikob		csio->sense_len);
1174105792Ssimokawa}
1175105792Ssimokawa
1176105792Ssimokawastatic void
1177105792Ssimokawasbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1178105792Ssimokawa{
1179105792Ssimokawa	struct sbp_cmd_status *sbp_cmd_status;
1180105792Ssimokawa	struct scsi_sense_data *sense;
1181105792Ssimokawa
1182105792Ssimokawa	sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1183105792Ssimokawa	sense = &ocb->ccb->csio.sense_data;
1184105792Ssimokawa
1185105792SsimokawaSBP_DEBUG(0)
1186105792Ssimokawa	sbp_print_scsi_cmd(ocb);
1187103285Sikob	/* XXX need decode status */
1188103285Sikob	sbp_show_sdev_info(ocb->sdev, 2);
1189103285Sikob	printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d",
1190103285Sikob		sbp_cmd_status->status,
1191103285Sikob		sbp_cmd_status->sfmt,
1192103285Sikob		sbp_cmd_status->valid,
1193103285Sikob		sbp_cmd_status->s_key,
1194103285Sikob		sbp_cmd_status->s_code,
1195103285Sikob		sbp_cmd_status->s_qlfr,
1196103285Sikob		sbp_status->len
1197103285Sikob	);
1198103285Sikob#if 0	 /* XXX */
1199103285Sikob	if (sbp_cmd_status->status == SCSI_STATUS_CHECK_COND) {
1200103285Sikob		printf(" %s\n", scsi_sense_key_text[sbp_cmd_status->s_key]);
1201103285Sikob			scsi_sense_desc(
1202103285Sikob				sbp_cmd_status->s_code,
1203103285Sikob				sbp_cmd_status->s_qlfr,
1204103285Sikob				ocb->ccb->ccb_h.path->device->inq_data
1205103285Sikob			)
1206103285Sikob	} else {
1207103285Sikob		printf("\n");
1208103285Sikob	}
1209103285Sikob#else
1210103285Sikob	printf("\n");
1211103285Sikob#endif
1212103285SikobEND_DEBUG
1213103285Sikob
1214110071Ssimokawa	switch (sbp_cmd_status->status) {
1215110071Ssimokawa	case SCSI_STATUS_CHECK_COND:
1216110071Ssimokawa	case SCSI_STATUS_BUSY:
1217110071Ssimokawa	case SCSI_STATUS_CMD_TERMINATED:
1218103285Sikob		if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1219103285Sikob			sense->error_code = SSD_CURRENT_ERROR;
1220103285Sikob		}else{
1221103285Sikob			sense->error_code = SSD_DEFERRED_ERROR;
1222103285Sikob		}
1223103285Sikob		if(sbp_cmd_status->valid)
1224103285Sikob			sense->error_code |= SSD_ERRCODE_VALID;
1225103285Sikob		sense->flags = sbp_cmd_status->s_key;
1226103285Sikob		if(sbp_cmd_status->mark)
1227103285Sikob			sense->flags |= SSD_FILEMARK;
1228103285Sikob		if(sbp_cmd_status->eom)
1229103285Sikob			sense->flags |= SSD_EOM;
1230103285Sikob		if(sbp_cmd_status->ill_len)
1231103285Sikob			sense->flags |= SSD_ILI;
1232103285Sikob		sense->info[0] = ntohl(sbp_cmd_status->info) & 0xff;
1233103285Sikob		sense->info[1] =(ntohl(sbp_cmd_status->info) >> 8) & 0xff;
1234103285Sikob		sense->info[2] =(ntohl(sbp_cmd_status->info) >> 16) & 0xff;
1235103285Sikob		sense->info[3] =(ntohl(sbp_cmd_status->info) >> 24) & 0xff;
1236103285Sikob		if (sbp_status->len <= 1)
1237103285Sikob			/* XXX not scsi status. shouldn't be happened */
1238103285Sikob			sense->extra_len = 0;
1239103285Sikob		else if (sbp_status->len <= 4)
1240103285Sikob			/* add_sense_code(_qual), info, cmd_spec_info */
1241103285Sikob			sense->extra_len = 6;
1242103285Sikob		else
1243103285Sikob			/* fru, sense_key_spec */
1244103285Sikob			sense->extra_len = 10;
1245103285Sikob		sense->cmd_spec_info[0] = ntohl(sbp_cmd_status->cdb) & 0xff;
1246103285Sikob		sense->cmd_spec_info[1] = (ntohl(sbp_cmd_status->cdb) >> 8) & 0xff;
1247103285Sikob		sense->cmd_spec_info[2] = (ntohl(sbp_cmd_status->cdb) >> 16) & 0xff;
1248103285Sikob		sense->cmd_spec_info[3] = (ntohl(sbp_cmd_status->cdb) >> 24) & 0xff;
1249103285Sikob		sense->add_sense_code = sbp_cmd_status->s_code;
1250103285Sikob		sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1251103285Sikob		sense->fru = sbp_cmd_status->fru;
1252103285Sikob		sense->sense_key_spec[0] = ntohl(sbp_cmd_status->s_keydep) & 0xff;
1253103285Sikob		sense->sense_key_spec[1] = (ntohl(sbp_cmd_status->s_keydep) >>8) & 0xff;
1254103285Sikob		sense->sense_key_spec[2] = (ntohl(sbp_cmd_status->s_keydep) >>16) & 0xff;
1255103285Sikob
1256103285Sikob		ocb->ccb->csio.scsi_status = sbp_cmd_status->status;;
1257103285Sikob		ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1258103285Sikob							| CAM_AUTOSNS_VALID;
1259103285Sikob/*
1260103285Sikob{
1261103285Sikob		u_int8_t j, *tmp;
1262103285Sikob		tmp = sense;
1263103285Sikob		for( j = 0 ; j < 32 ; j+=8){
1264103285Sikob			printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1265103285Sikob				tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1266103285Sikob				tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1267103285Sikob		}
1268103285Sikob
1269103285Sikob}
1270103285Sikob*/
1271110071Ssimokawa		break;
1272110071Ssimokawa	default:
1273110071Ssimokawa		sbp_show_sdev_info(ocb->sdev, 2);
1274110071Ssimokawa		printf("sbp_scsi_status: unknown scsi status 0x%x\n",
1275110071Ssimokawa						sbp_cmd_status->status);
1276103285Sikob	}
1277103285Sikob}
1278103285Sikob
1279103285Sikobstatic void
1280103285Sikobsbp_fix_inq_data(struct sbp_ocb *ocb)
1281103285Sikob{
1282103285Sikob	union ccb *ccb;
1283103285Sikob	struct sbp_dev *sdev;
1284103285Sikob	struct scsi_inquiry_data *inq;
1285103285Sikob
1286103285Sikob	ccb = ocb->ccb;
1287103285Sikob	sdev = ocb->sdev;
1288103285Sikob
1289103285Sikob	if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1290103285Sikob		return;
1291103285SikobSBP_DEBUG(1)
1292103285Sikob	sbp_show_sdev_info(sdev, 2);
1293103285Sikob	printf("sbp_fix_inq_data\n");
1294103285SikobEND_DEBUG
1295103285Sikob	inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1296103285Sikob	switch (SID_TYPE(inq)) {
1297103285Sikob	case T_DIRECT:
1298103285Sikob		/*
1299103285Sikob		 * XXX Convert Direct Access device to RBC.
1300108281Ssimokawa		 * I've never seen FireWire DA devices which support READ_6.
1301103285Sikob		 */
1302103285Sikob#if 1
1303103285Sikob		if (SID_TYPE(inq) == T_DIRECT)
1304103285Sikob			inq->device |= T_RBC; /*  T_DIRECT == 0 */
1305103285Sikob#endif
1306103285Sikob		/* fall through */
1307103285Sikob	case T_RBC:
1308103285Sikob		/* disable tag queuing */
1309103285Sikob		inq->flags &= ~SID_CmdQue;
1310103285Sikob		/*
1311103285Sikob		 * Override vendor/product/revision information.
1312103285Sikob		 * Some devices sometimes return strange strings.
1313103285Sikob		 */
1314103285Sikob		bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1315103285Sikob		bcopy(sdev->product, inq->product, sizeof(inq->product));
1316103285Sikob		bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1317103285Sikob		break;
1318103285Sikob	}
1319103285Sikob}
1320103285Sikob
1321103285Sikobstatic void
1322103285Sikobsbp_recv1(struct fw_xfer *xfer){
1323103285Sikob	struct fw_pkt *rfp;
1324103285Sikob#if NEED_RESPONSE
1325103285Sikob	struct fw_pkt *sfp;
1326103285Sikob#endif
1327103285Sikob	struct sbp_softc *sbp;
1328103285Sikob	struct sbp_dev *sdev;
1329103285Sikob	struct sbp_ocb *ocb;
1330103285Sikob	struct sbp_login_res *login_res = NULL;
1331103285Sikob	struct sbp_status *sbp_status;
1332103285Sikob	struct sbp_target *target;
1333103285Sikob	int	orb_fun, status_valid;
1334103285Sikob	u_int32_t addr;
1335103285Sikob/*
1336103285Sikob	u_int32_t *ld;
1337103285Sikob	ld = xfer->recv.buf;
1338103285Sikobprintf("sbp %x %d %d %08x %08x %08x %08x\n",
1339103285Sikob			xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1340103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1341103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1342103285Sikob*/
1343103285Sikob	if(xfer->resp != 0){
1344103285Sikob		printf("sbp_recv: xfer->resp != 0\n");
1345103285Sikob		fw_xfer_free( xfer);
1346103285Sikob		return;
1347103285Sikob	}
1348103285Sikob	if(xfer->recv.buf == NULL){
1349103285Sikob		printf("sbp_recv: xfer->recv.buf == NULL\n");
1350103285Sikob		fw_xfer_free( xfer);
1351103285Sikob		return;
1352103285Sikob	}
1353103285Sikob	sbp = (struct sbp_softc *)xfer->sc;
1354103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
1355103285Sikob	if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1356103285Sikob		printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1357103285Sikob		fw_xfer_free( xfer);
1358103285Sikob		return;
1359103285Sikob	}
1360103285Sikob	sbp_status = (struct sbp_status *)rfp->mode.wreqb.payload;
1361103285Sikob	addr = ntohl(rfp->mode.wreqb.dest_lo);
1362103285SikobSBP_DEBUG(2)
1363103285Sikob	printf("received address 0x%x\n", addr);
1364103285SikobEND_DEBUG
1365103285Sikob	target = &sbp->targets[SBP_ADDR2TRG(addr)];
1366103285Sikob	sdev = &target->luns[SBP_ADDR2LUN(addr)];
1367103285Sikob
1368103285Sikob	status_valid = (sbp_status->resp == ORB_RES_CMPL
1369103285Sikob			&& sbp_status->dead == 0
1370103285Sikob			&& sbp_status->status == 0);
1371103285Sikob
1372103285Sikob	if (!status_valid || debug > 1){
1373103285Sikob		int status;
1374110129SsimokawaSBP_DEBUG(0)
1375103285Sikob		sbp_show_sdev_info(sdev, 2);
1376103285Sikob		printf("ORB status src:%x resp:%x dead:%x"
1377108712Ssimokawa#if __FreeBSD_version >= 500000
1378103285Sikob				" len:%x stat:%x orb:%x%08x\n",
1379108712Ssimokawa#else
1380108712Ssimokawa				" len:%x stat:%x orb:%x%08lx\n",
1381108712Ssimokawa#endif
1382103285Sikob			sbp_status->src, sbp_status->resp, sbp_status->dead,
1383103285Sikob			sbp_status->len, sbp_status->status,
1384108280Ssimokawa			ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1385110129SsimokawaEND_DEBUG
1386103285Sikob		sbp_show_sdev_info(sdev, 2);
1387103285Sikob		status = sbp_status->status;
1388103285Sikob		switch(sbp_status->resp) {
1389103285Sikob		case 0:
1390103285Sikob			if (status > MAX_ORB_STATUS0)
1391103285Sikob				printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1392110129Ssimokawa			else if (status > 0)
1393110129Ssimokawa				printf("%s\n", orb_status0[status]);
1394103285Sikob			else
1395110129Ssimokawa				printf("\n");
1396103285Sikob			break;
1397103285Sikob		case 1:
1398103285Sikob			printf("Object: %s, Serial Bus Error: %s\n",
1399103285Sikob				orb_status1_object[(status>>6) & 3],
1400103285Sikob				orb_status1_serial_bus_error[status & 0xf]);
1401103285Sikob			break;
1402110129Ssimokawa		case 2:
1403110129Ssimokawa			printf("Illegal request\n");
1404110129Ssimokawa			break;
1405110129Ssimokawa		case 3:
1406110129Ssimokawa			printf("Vendor dependent\n");
1407110129Ssimokawa			break;
1408103285Sikob		default:
1409110129Ssimokawa			printf("unknown respose code %d\n", sbp_status->resp);
1410103285Sikob		}
1411103285Sikob	}
1412103285Sikob	ocb = sbp_dequeue_ocb(sdev, ntohl(sbp_status->orb_lo));
1413103285Sikob
1414103285Sikob	/* we have to reset the fetch agent if it's dead */
1415103285Sikob	if (sbp_status->dead) {
1416103285Sikob		if (sdev->path)
1417103285Sikob			xpt_freeze_devq(sdev->path, 1);
1418103285Sikob		sbp_agent_reset(sdev, 0);
1419103285Sikob	}
1420103285Sikob
1421103285Sikob
1422103285Sikob	if (ocb == NULL) {
1423103285Sikob		printf("No ocb on the queue for target %d.\n", sdev->target->target_id);
1424103285Sikob		fw_xfer_free( xfer);
1425103285Sikob		return;
1426103285Sikob	}
1427103285Sikob
1428103285Sikob	switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1429103285Sikob	case ORB_FMT_NOP:
1430103285Sikob		break;
1431103285Sikob	case ORB_FMT_VED:
1432103285Sikob		break;
1433103285Sikob	case ORB_FMT_STD:
1434103285Sikob		switch(ocb->flags & OCB_ACT_MASK){
1435103285Sikob		case OCB_ACT_MGM:
1436103285Sikob			orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1437103285Sikob			switch(orb_fun) {
1438103285Sikob			case ORB_FUN_LGI:
1439103285Sikob				login_res = &sdev->login;
1440103285Sikob				login_res->len = ntohs(login_res->len);
1441103285Sikob				login_res->id = ntohs(login_res->id);
1442103285Sikob				login_res->cmd_hi = ntohs(login_res->cmd_hi);
1443103285Sikob				login_res->cmd_lo = ntohl(login_res->cmd_lo);
1444103285Sikob				if (status_valid) {
1445103285SikobSBP_DEBUG(0)
1446103285Sikobsbp_show_sdev_info(sdev, 2);
1447103285Sikobprintf("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));
1448103285SikobEND_DEBUG
1449103285Sikob#if 1
1450103285Sikob					sbp_busy_timeout(sdev);
1451103285Sikob#else
1452103285Sikob					sbp_mgm_orb(sdev, ORB_FUN_ATS);
1453103285Sikob#endif
1454103285Sikob				} else {
1455103285Sikob					/* forgot logout ? */
1456103285Sikob					printf("login failed\n");
1457103285Sikob					sdev->status = SBP_DEV_RESET;
1458103285Sikob				}
1459103285Sikob				break;
1460103285Sikob			case ORB_FUN_RCN:
1461103285Sikob				login_res = &sdev->login;
1462103285Sikob				if (status_valid) {
1463103285Sikob					sdev->status = SBP_DEV_ATTACHED;
1464103285SikobSBP_DEBUG(0)
1465103285Sikobsbp_show_sdev_info(sdev, 2);
1466103285Sikobprintf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo);
1467103285SikobEND_DEBUG
1468103285Sikob#if 1
1469103285Sikob					sbp_ping_unit(sdev);
1470103285Sikob					xpt_release_devq(sdev->path, 1, TRUE);
1471103285Sikob#else
1472103285Sikob					sbp_mgm_orb(sdev, ORB_FUN_ATS);
1473103285Sikob#endif
1474103285Sikob				} else {
1475103285Sikob					/* reconnection hold time exceed? */
1476103285Sikob					printf("reconnect failed\n");
1477103285Sikob					sbp_mgm_orb(sdev, ORB_FUN_LGI);
1478103285Sikob				}
1479103285Sikob				break;
1480103285Sikob			case ORB_FUN_LGO:
1481103285Sikob				sdev->status = SBP_DEV_RESET;
1482103285Sikob				break;
1483103285Sikob			case ORB_FUN_LUR:
1484103285Sikob			case ORB_FUN_RST:
1485103285Sikob			case ORB_FUN_ATA:
1486103285Sikob			case ORB_FUN_ATS:
1487103285Sikob				if (sdev->status == SBP_DEV_ATTACHED) {
1488103285Sikob					xpt_release_devq(sdev->path, 1, TRUE);
1489103285Sikob				} else {
1490103285Sikob					sbp_busy_timeout(sdev);
1491103285Sikob				}
1492103285Sikob				break;
1493103285Sikob			default:
1494103285Sikob				break;
1495103285Sikob			}
1496103285Sikob			break;
1497103285Sikob		case OCB_ACT_CMD:
1498103285Sikob			if(ocb->ccb != NULL){
1499103285Sikob				union ccb *ccb;
1500103285Sikob/*
1501103285Sikob				u_int32_t *ld;
1502103285Sikob				ld = ocb->ccb->csio.data_ptr;
1503103285Sikob				if(ld != NULL && ocb->ccb->csio.dxfer_len != 0)
1504103285Sikob					printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]);
1505103285Sikob				else
1506103285Sikob					printf("ptr NULL\n");
1507103285Sikobprintf("len %d\n", sbp_status->len);
1508103285Sikob*/
1509103285Sikob				ccb = ocb->ccb;
1510103285Sikob				if(sbp_status->len > 1){
1511103285Sikob					sbp_scsi_status(sbp_status, ocb);
1512103285Sikob				}else{
1513103285Sikob					if(sbp_status->resp != ORB_RES_CMPL){
1514103285Sikob						ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1515103285Sikob					}else{
1516103285Sikob						ccb->ccb_h.status = CAM_REQ_CMP;
1517103285Sikob					}
1518103285Sikob				}
1519103285Sikob				/* fix up inq data */
1520103285Sikob				if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1521103285Sikob					sbp_fix_inq_data(ocb);
1522103285Sikob				xpt_done(ccb);
1523103285Sikob			}
1524103285Sikob			break;
1525103285Sikob		default:
1526103285Sikob			break;
1527103285Sikob		}
1528103285Sikob	}
1529103285Sikob
1530103285Sikob	if (!(ocb->flags & OCB_RESERVED))
1531103285Sikob		sbp_free_ocb(sbp, ocb);
1532103285Sikob
1533103285Sikob/* The received packet is usually small enough to be stored within
1534103285Sikob * the buffer. In that case, the controller return ack_complete and
1535103285Sikob * no respose is necessary.
1536103285Sikob *
1537103285Sikob * XXX fwohci.c and firewire.c should inform event_code such as
1538103285Sikob * ack_complete or ack_pending to upper driver.
1539103285Sikob */
1540103285Sikob#if NEED_RESPONSE
1541103285Sikob	xfer->send.buf = malloc(12, M_SBP, M_NOWAIT | M_ZERO);
1542103285Sikob	xfer->send.len = 12;
1543103285Sikob	xfer->send.off = 0;
1544103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
1545103285Sikob	sfp->mode.wres.dst = rfp->mode.wreqb.src;
1546103285Sikob	xfer->dst = ntohs(sfp->mode.wres.dst);
1547103285Sikob	xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1548103285Sikob	xfer->act.hand = sbp_loginres_callback;
1549103285Sikob	xfer->retry_req = fw_asybusy;
1550103285Sikob
1551103285Sikob	sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1552103285Sikob	sfp->mode.wres.tcode = FWTCODE_WRES;
1553103285Sikob	sfp->mode.wres.rtcode = 0;
1554103285Sikob	sfp->mode.wres.pri = 0;
1555103285Sikob
1556103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1557103285Sikob#else
1558103285Sikob	fw_xfer_free(xfer);
1559103285Sikob#endif
1560103285Sikob
1561103285Sikob	return;
1562103285Sikob
1563103285Sikob}
1564103285Sikob
1565103285Sikobstatic void
1566103285Sikobsbp_recv(struct fw_xfer *xfer)
1567103285Sikob{
1568103285Sikob	int s;
1569103285Sikob
1570103285Sikob	s = splcam();
1571103285Sikob	sbp_recv1(xfer);
1572103285Sikob	splx(s);
1573103285Sikob}
1574103285Sikob/*
1575103285Sikob * sbp_attach()
1576103285Sikob */
1577103285Sikobstatic int
1578103285Sikobsbp_attach(device_t dev)
1579103285Sikob{
1580103285Sikob	struct sbp_softc *sbp;
1581103285Sikob	struct cam_devq *devq;
1582103285Sikob	struct fw_xfer *xfer;
1583103285Sikob	int i, s, error;
1584103285Sikob
1585103285SikobSBP_DEBUG(0)
1586103285Sikob	printf("sbp_attach\n");
1587103285SikobEND_DEBUG
1588103285Sikob
1589103285Sikob	sbp = ((struct sbp_softc *)device_get_softc(dev));
1590103285Sikob	bzero(sbp, sizeof(struct sbp_softc));
1591103285Sikob	sbp->fd.dev = dev;
1592103285Sikob	sbp->fd.fc = device_get_ivars(dev);
1593103285Sikob	error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
1594103285Sikob				/*boundary*/0,
1595103285Sikob				/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1596103285Sikob				/*highaddr*/BUS_SPACE_MAXADDR,
1597103285Sikob				/*filter*/NULL, /*filterarg*/NULL,
1598103285Sikob				/*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1599103285Sikob				/*maxsegsz*/0x8000,
1600103285Sikob				/*flags*/BUS_DMA_ALLOCNOW,
1601103285Sikob				&sbp->dmat);
1602103285Sikob	if (error != 0) {
1603103285Sikob		printf("sbp_attach: Could not allocate DMA tag "
1604103285Sikob			"- error %d\n", error);
1605103285Sikob			return (ENOMEM);
1606103285Sikob	}
1607103285Sikob
1608103285Sikob	devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1609103285Sikob	if (devq == NULL)
1610103285Sikob		return (ENXIO);
1611103285Sikob
1612103285Sikob	for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1613103285Sikob		sbp->targets[i].fwdev = NULL;
1614103285Sikob		sbp->targets[i].luns = NULL;
1615103285Sikob	}
1616103285Sikob
1617103285Sikob	sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1618103285Sikob				 device_get_unit(dev),
1619103285Sikob				 /*untagged*/ SBP_QUEUE_LEN,
1620103285Sikob				 /*tagged*/0, devq);
1621103285Sikob
1622103285Sikob	if (sbp->sim == NULL) {
1623103285Sikob		cam_simq_free(devq);
1624103285Sikob		return (ENXIO);
1625103285Sikob	}
1626103285Sikob
1627103285Sikob	sbp->ocb = (struct sbp_ocb *) contigmalloc(
1628103285Sikob		sizeof (struct sbp_ocb) * SBP_NUM_OCB,
1629109424Ssimokawa		M_SBP, M_NOWAIT, 0x10000, 0xffffffff, PAGE_SIZE, 0ul);
1630103285Sikob	bzero(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB);
1631103285Sikob
1632103285Sikob	if (sbp->ocb == NULL) {
1633103285Sikob		printf("sbp0: ocb alloction failure\n");
1634103285Sikob		return (ENOMEM);
1635103285Sikob	}
1636103285Sikob
1637103285Sikob	STAILQ_INIT(&sbp->free_ocbs);
1638103285Sikob	for (i = 0; i < SBP_NUM_OCB; i++) {
1639103285Sikob		sbp_free_ocb(sbp, &sbp->ocb[i]);
1640103285Sikob	}
1641103285Sikob
1642103285Sikob	if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS) {
1643103285Sikob		cam_sim_free(sbp->sim, /*free_devq*/TRUE);
1644103285Sikob		contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB,
1645103285Sikob									M_SBP);
1646103285Sikob		return (ENXIO);
1647103285Sikob	}
1648103285Sikob
1649103285Sikob	xfer = fw_xfer_alloc();
1650103285Sikob	xfer->act.hand = sbp_recv;
1651103285Sikob	xfer->act_type = FWACT_XFER;
1652103285Sikob#if NEED_RESPONSE
1653103285Sikob	xfer->fc = sbp->fd.fc;
1654103285Sikob#endif
1655103285Sikob	xfer->sc = (caddr_t)sbp;
1656103285Sikob
1657103285Sikob	sbp->fwb.start_hi = SBP_BIND_HI;
1658103285Sikob	sbp->fwb.start_lo = SBP_DEV2ADDR(device_get_unit(sbp->fd.dev), 0, 0);
1659103285Sikob	/* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1660103285Sikob	sbp->fwb.addrlen = 0xffff;
1661103285Sikob	sbp->fwb.xfer = xfer;
1662103285Sikob	fw_bindadd(sbp->fd.fc, &sbp->fwb);
1663103285Sikob
1664103285Sikob	sbp->fd.post_explore = sbp_post_explore;
1665103285Sikob	s = splfw();
1666103285Sikob	sbp_post_explore((void *)sbp);
1667103285Sikob	splx(s);
1668103285Sikob
1669103285Sikob	return (0);
1670103285Sikob}
1671103285Sikob
1672103285Sikobstatic int
1673110145Ssimokawasbp_logout_all(struct sbp_softc *sbp)
1674110145Ssimokawa{
1675110145Ssimokawa	struct sbp_target *target;
1676110145Ssimokawa	struct sbp_dev *sdev;
1677110145Ssimokawa	int i, j;
1678110145Ssimokawa
1679110145SsimokawaSBP_DEBUG(0)
1680110145Ssimokawa	printf("sbp_logout_all\n");
1681110145SsimokawaEND_DEBUG
1682110145Ssimokawa	for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
1683110145Ssimokawa		target = &sbp->targets[i];
1684110145Ssimokawa		if (target->luns == NULL)
1685110145Ssimokawa			continue;
1686110145Ssimokawa		for (j = 0; j < target->num_lun; j++) {
1687110145Ssimokawa			sdev = &target->luns[j];
1688110145Ssimokawa			if (sdev->status == SBP_DEV_ATTACHED) {
1689110145Ssimokawa				sbp_show_sdev_info(sdev, 2);
1690110145Ssimokawa				printf("logout\n");
1691110145Ssimokawa				sbp_mgm_orb(sdev, ORB_FUN_LGO);
1692110145Ssimokawa			}
1693110145Ssimokawa		}
1694110145Ssimokawa	}
1695110145Ssimokawa	return 0;
1696110145Ssimokawa}
1697110145Ssimokawa
1698110145Ssimokawastatic int
1699110145Ssimokawasbp_shutdown(device_t dev)
1700110145Ssimokawa{
1701110145Ssimokawa	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1702110145Ssimokawa
1703110145Ssimokawa	sbp_logout_all(sbp);
1704110145Ssimokawa	return (0);
1705110145Ssimokawa}
1706110145Ssimokawa
1707110145Ssimokawastatic int
1708103285Sikobsbp_detach(device_t dev)
1709103285Sikob{
1710103285Sikob	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1711103285Sikob	struct firewire_comm *fc = sbp->fd.fc;
1712103285Sikob	int i;
1713103285Sikob
1714103285SikobSBP_DEBUG(0)
1715103285Sikob	printf("sbp_detach\n");
1716103285SikobEND_DEBUG
1717110145Ssimokawa#if 0
1718103285Sikob	/* bus reset for logout */
1719103285Sikob	sbp->fd.post_explore = NULL;
1720103285Sikob	fc->ibr(fc);
1721110145Ssimokawa#endif
1722103285Sikob
1723103285Sikob	for (i = 0; i < SBP_NUM_TARGETS; i ++)
1724110145Ssimokawa		sbp_cam_detach_target(&sbp->targets[i]);
1725103285Sikob	xpt_bus_deregister(cam_sim_path(sbp->sim));
1726110145Ssimokawa
1727110145Ssimokawa	sbp_logout_all(sbp);
1728110145Ssimokawa	/* XXX wait for logout completion */
1729110145Ssimokawa	tsleep(&i, FWPRI, "sbpdtc", hz/2);
1730110145Ssimokawa
1731110145Ssimokawa	fw_bindremove(fc, &sbp->fwb);
1732110145Ssimokawa	contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB, M_SBP);
1733103285Sikob	bus_dma_tag_destroy(sbp->dmat);
1734110145Ssimokawa
1735110145Ssimokawa	for (i = 0; i < SBP_NUM_TARGETS; i ++)
1736110145Ssimokawa		if (sbp->targets[i].luns != NULL)
1737110145Ssimokawa			free(sbp->targets[i].luns, M_SBP);
1738110145Ssimokawa
1739103285Sikob	return (0);
1740103285Sikob}
1741103285Sikob
1742103285Sikobstatic void
1743110145Ssimokawasbp_cam_detach_target(struct sbp_target *target)
1744103285Sikob{
1745103285Sikob	int i;
1746103285Sikob	struct sbp_dev *sdev;
1747103285Sikob
1748103285Sikob	if (target->luns != NULL) {
1749108529SsimokawaSBP_DEBUG(0)
1750103285Sikob		printf("sbp_detach_target %d\n", target->target_id);
1751108529SsimokawaEND_DEBUG
1752103285Sikob		for (i=0; i < target->num_lun; i++) {
1753103285Sikob			sdev = &target->luns[i];
1754103285Sikob			if (sdev->status == SBP_DEV_RESET ||
1755103285Sikob					sdev->status == SBP_DEV_DEAD)
1756103285Sikob				continue;
1757103285Sikob			if (sdev->path)
1758103285Sikob				xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
1759103285Sikob			xpt_free_path(sdev->path);
1760103285Sikob			sdev->path = NULL;
1761103285Sikob			sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
1762103285Sikob		}
1763103285Sikob	}
1764103285Sikob}
1765103285Sikob
1766103285Sikobstatic void
1767103285Sikobsbp_timeout(void *arg)
1768103285Sikob{
1769103285Sikob	struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
1770103285Sikob	struct sbp_dev *sdev = ocb->sdev;
1771103285Sikob	int s;
1772103285Sikob
1773103285Sikob	sbp_show_sdev_info(sdev, 2);
1774103285Sikob	printf("request timeout ... requeue\n");
1775103285Sikob
1776103285Sikob	/* XXX need reset? */
1777103285Sikob
1778103285Sikob	s = splfw();
1779105633Ssimokawa	sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
1780103285Sikob	splx(s);
1781103285Sikob	return;
1782103285Sikob}
1783103285Sikob
1784103285Sikobstatic void
1785103285Sikobsbp_action1(struct cam_sim *sim, union ccb *ccb)
1786103285Sikob{
1787103285Sikob
1788103285Sikob	struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
1789103285Sikob	struct sbp_target *target = NULL;
1790103285Sikob	struct sbp_dev *sdev = NULL;
1791103285Sikob
1792103285Sikob	/* target:lun -> sdev mapping */
1793103285Sikob	if (sbp != NULL
1794103285Sikob			&& ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
1795103285Sikob			&& ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
1796103285Sikob		target = &sbp->targets[ccb->ccb_h.target_id];
1797103285Sikob		if (target->fwdev != NULL
1798103285Sikob				&& ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
1799103285Sikob				&& ccb->ccb_h.target_lun < target->num_lun) {
1800103285Sikob			sdev = &target->luns[ccb->ccb_h.target_lun];
1801103285Sikob			if (sdev->status != SBP_DEV_ATTACHED &&
1802103285Sikob				sdev->status != SBP_DEV_PROBE)
1803103285Sikob				sdev = NULL;
1804103285Sikob		}
1805103285Sikob	}
1806103285Sikob
1807103285SikobSBP_DEBUG(1)
1808103285Sikob	if (sdev == NULL)
1809103285Sikob		printf("invalid target %d lun %d\n",
1810103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
1811103285SikobEND_DEBUG
1812103285Sikob
1813103285Sikob	switch (ccb->ccb_h.func_code) {
1814103285Sikob	case XPT_SCSI_IO:
1815103285Sikob	case XPT_RESET_DEV:
1816103285Sikob	case XPT_GET_TRAN_SETTINGS:
1817103285Sikob	case XPT_SET_TRAN_SETTINGS:
1818103285Sikob	case XPT_CALC_GEOMETRY:
1819103285Sikob		if (sdev == NULL) {
1820103285SikobSBP_DEBUG(1)
1821103285Sikob			printf("%s:%d:%d:func_code 0x%04x: "
1822103285Sikob				"Invalid target (target needed)\n",
1823103285Sikob				device_get_nameunit(sbp->fd.dev),
1824103285Sikob				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
1825103285Sikob				ccb->ccb_h.func_code);
1826103285SikobEND_DEBUG
1827103285Sikob
1828108276Ssimokawa			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1829103285Sikob			xpt_done(ccb);
1830103285Sikob			return;
1831103285Sikob		}
1832103285Sikob		break;
1833103285Sikob	case XPT_PATH_INQ:
1834103285Sikob	case XPT_NOOP:
1835103285Sikob		/* The opcodes sometimes aimed at a target (sc is valid),
1836103285Sikob		 * sometimes aimed at the SIM (sc is invalid and target is
1837103285Sikob		 * CAM_TARGET_WILDCARD)
1838103285Sikob		 */
1839103285Sikob		if (sbp == NULL &&
1840103285Sikob			ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
1841103285SikobSBP_DEBUG(0)
1842103285Sikob			printf("%s:%d:%d func_code 0x%04x: "
1843103285Sikob				"Invalid target (no wildcard)\n",
1844103285Sikob				device_get_nameunit(sbp->fd.dev),
1845103285Sikob				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
1846103285Sikob				ccb->ccb_h.func_code);
1847103285SikobEND_DEBUG
1848108276Ssimokawa			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1849103285Sikob			xpt_done(ccb);
1850103285Sikob			return;
1851103285Sikob		}
1852103285Sikob		break;
1853103285Sikob	default:
1854103285Sikob		/* XXX Hm, we should check the input parameters */
1855103285Sikob		break;
1856103285Sikob	}
1857103285Sikob
1858103285Sikob	switch (ccb->ccb_h.func_code) {
1859103285Sikob	case XPT_SCSI_IO:
1860103285Sikob	{
1861103285Sikob		struct ccb_scsiio *csio;
1862103285Sikob		struct sbp_ocb *ocb;
1863103285Sikob		int s, speed;
1864106506Ssimokawa		void *cdb;
1865103285Sikob
1866103285Sikob		csio = &ccb->csio;
1867103285Sikob
1868103285SikobSBP_DEBUG(1)
1869103285Sikob		printf("%s:%d:%d XPT_SCSI_IO: "
1870103285Sikob			"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1871103285Sikob			", flags: 0x%02x, "
1872103285Sikob			"%db cmd/%db data/%db sense\n",
1873103285Sikob			device_get_nameunit(sbp->fd.dev),
1874103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
1875103285Sikob			csio->cdb_io.cdb_bytes[0],
1876103285Sikob			csio->cdb_io.cdb_bytes[1],
1877103285Sikob			csio->cdb_io.cdb_bytes[2],
1878103285Sikob			csio->cdb_io.cdb_bytes[3],
1879103285Sikob			csio->cdb_io.cdb_bytes[4],
1880103285Sikob			csio->cdb_io.cdb_bytes[5],
1881103285Sikob			csio->cdb_io.cdb_bytes[6],
1882103285Sikob			csio->cdb_io.cdb_bytes[7],
1883103285Sikob			csio->cdb_io.cdb_bytes[8],
1884103285Sikob			csio->cdb_io.cdb_bytes[9],
1885103285Sikob			ccb->ccb_h.flags & CAM_DIR_MASK,
1886103285Sikob			csio->cdb_len, csio->dxfer_len,
1887103285Sikob			csio->sense_len);
1888103285SikobEND_DEBUG
1889103285Sikob		if(sdev == NULL){
1890103285Sikob			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1891103285Sikob			xpt_done(ccb);
1892103285Sikob			return;
1893103285Sikob		}
1894103285Sikob#if 0
1895103285Sikob		/* if we are in probe stage, pass only probe commands */
1896103285Sikob		if (sdev->status == SBP_DEV_PROBE) {
1897103285Sikob			char *name;
1898103285Sikob			name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
1899103285Sikob			printf("probe stage, periph name: %s\n", name);
1900103285Sikob			if (strcmp(name, "probe") != 0) {
1901103285Sikob				ccb->ccb_h.status = CAM_REQUEUE_REQ;
1902103285Sikob				xpt_done(ccb);
1903103285Sikob				return;
1904103285Sikob			}
1905103285Sikob		}
1906103285Sikob#endif
1907103285Sikob		if ((ocb = sbp_get_ocb(sbp)) == NULL) {
1908103285Sikob			s = splfw();
1909103285Sikob			sbp->flags |= SBP_RESOURCE_SHORTAGE;
1910103285Sikob			splx(s);
1911103285Sikob			return;
1912103285Sikob		}
1913103285Sikob		ocb->flags = OCB_ACT_CMD;
1914103285Sikob		ocb->sdev = sdev;
1915103285Sikob		ocb->ccb = ccb;
1916103285Sikob		ccb->ccb_h.ccb_sdev_ptr = sdev;
1917103285Sikob		ocb->orb[0] = htonl(1 << 31);
1918103285Sikob		ocb->orb[1] = 0;
1919103285Sikob		ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
1920103285Sikob		ocb->orb[3] = htonl(vtophys(ocb->ind_ptr));
1921103285Sikob		speed = min(target->fwdev->speed, max_speed);
1922103285Sikob		ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
1923103285Sikob						| ORB_CMD_MAXP(speed + 7));
1924103285Sikob		if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
1925103285Sikob			ocb->orb[4] |= htonl(ORB_CMD_IN);
1926103285Sikob		}
1927103285Sikob
1928103285Sikob		if (csio->ccb_h.flags & CAM_SCATTER_VALID)
1929103285Sikob			printf("sbp: CAM_SCATTER_VALID\n");
1930103285Sikob		if (csio->ccb_h.flags & CAM_DATA_PHYS)
1931103285Sikob			printf("sbp: CAM_DATA_PHYS\n");
1932103285Sikob
1933106506Ssimokawa		if (csio->ccb_h.flags & CAM_CDB_POINTER)
1934106506Ssimokawa			cdb = (void *)csio->cdb_io.cdb_ptr;
1935106506Ssimokawa		else
1936106506Ssimokawa			cdb = (void *)&csio->cdb_io.cdb_bytes;
1937106506Ssimokawa		bcopy(cdb,
1938103285Sikob			(void *)(uintptr_t)(volatile void *)&ocb->orb[5],
1939106506Ssimokawa				csio->cdb_len);
1940103285Sikob/*
1941103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
1942103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
1943103285Sikob*/
1944103285Sikob		if (ccb->csio.dxfer_len > 0) {
1945103285Sikob			int s;
1946103285Sikob
1947103285Sikob			if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
1948103285Sikob				printf("sbp_action1: cannot create dmamap\n");
1949103285Sikob				break;
1950103285Sikob			}
1951103285Sikob
1952103285Sikob			s = splsoftvm();
1953103285Sikob			bus_dmamap_load(/*dma tag*/sbp->dmat,
1954103285Sikob					/*dma map*/ocb->dmamap,
1955103285Sikob					ccb->csio.data_ptr,
1956103285Sikob					ccb->csio.dxfer_len,
1957103285Sikob					sbp_execute_ocb,
1958103285Sikob					ocb,
1959103285Sikob					/*flags*/0);
1960103285Sikob			splx(s);
1961103285Sikob		} else
1962103285Sikob			sbp_execute_ocb(ocb, NULL, 0, 0);
1963103285Sikob		break;
1964103285Sikob	}
1965103285Sikob	case XPT_CALC_GEOMETRY:
1966103285Sikob	{
1967103285Sikob		struct ccb_calc_geometry *ccg;
1968103285Sikob		u_int32_t size_mb;
1969103285Sikob		u_int32_t secs_per_cylinder;
1970103285Sikob		int extended = 1;
1971103285Sikob		ccg = &ccb->ccg;
1972103285Sikob
1973103285Sikob		if (ccg->block_size == 0) {
1974103285Sikob			printf("sbp_action1: block_size is 0.\n");
1975103285Sikob			ccb->ccb_h.status = CAM_REQ_INVALID;
1976103285Sikob			xpt_done(ccb);
1977103285Sikob			break;
1978103285Sikob		}
1979103285SikobSBP_DEBUG(1)
1980103285Sikob		printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
1981103285Sikob			"Volume size = %d\n",
1982103285Sikob			device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim),
1983103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
1984103285Sikob			ccg->volume_size);
1985103285SikobEND_DEBUG
1986103285Sikob
1987103285Sikob		size_mb = ccg->volume_size
1988103285Sikob			/ ((1024L * 1024L) / ccg->block_size);
1989103285Sikob
1990103285Sikob		if (size_mb >= 1024 && extended) {
1991103285Sikob			ccg->heads = 255;
1992103285Sikob			ccg->secs_per_track = 63;
1993103285Sikob		} else {
1994103285Sikob			ccg->heads = 64;
1995103285Sikob			ccg->secs_per_track = 32;
1996103285Sikob		}
1997103285Sikob		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1998103285Sikob		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1999103285Sikob		ccb->ccb_h.status = CAM_REQ_CMP;
2000103285Sikob		xpt_done(ccb);
2001103285Sikob		break;
2002103285Sikob	}
2003103285Sikob	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
2004103285Sikob	{
2005103285Sikob
2006103285SikobSBP_DEBUG(1)
2007103285Sikob		printf("%s:%d:XPT_RESET_BUS: \n",
2008103285Sikob			device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2009103285SikobEND_DEBUG
2010103285Sikob
2011103285Sikob		ccb->ccb_h.status = CAM_REQ_INVALID;
2012103285Sikob		xpt_done(ccb);
2013103285Sikob		break;
2014103285Sikob	}
2015103285Sikob	case XPT_PATH_INQ:		/* Path routing inquiry */
2016103285Sikob	{
2017103285Sikob		struct ccb_pathinq *cpi = &ccb->cpi;
2018103285Sikob
2019103285SikobSBP_DEBUG(1)
2020103285Sikob		printf("%s:%d:%d XPT_PATH_INQ:.\n",
2021103285Sikob			device_get_nameunit(sbp->fd.dev),
2022103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2023103285SikobEND_DEBUG
2024103285Sikob		cpi->version_num = 1; /* XXX??? */
2025103285Sikob		cpi->hba_inquiry = 0;
2026103285Sikob		cpi->target_sprt = 0;
2027103285Sikob		cpi->hba_misc = 0;
2028103285Sikob		cpi->hba_eng_cnt = 0;
2029103285Sikob		cpi->max_target = SBP_NUM_TARGETS - 1;
2030103285Sikob		cpi->max_lun = SBP_NUM_LUNS - 1;
2031103285Sikob		cpi->initiator_id = SBP_INITIATOR;
2032103285Sikob		cpi->bus_id = sim->bus_id;
2033103285Sikob		cpi->base_transfer_speed = 400 * 1000 / 8;
2034103285Sikob		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2035103285Sikob		strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2036103285Sikob		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2037103285Sikob		cpi->unit_number = sim->unit_number;
2038103285Sikob
2039103285Sikob		cpi->ccb_h.status = CAM_REQ_CMP;
2040103285Sikob		xpt_done(ccb);
2041103285Sikob		break;
2042103285Sikob	}
2043103285Sikob	case XPT_GET_TRAN_SETTINGS:
2044103285Sikob	{
2045103285Sikob		struct ccb_trans_settings *cts = &ccb->cts;
2046103285SikobSBP_DEBUG(1)
2047103285Sikob		printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2048103285Sikob			device_get_nameunit(sbp->fd.dev),
2049103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2050103285SikobEND_DEBUG
2051103285Sikob		/* Disable disconnect and tagged queuing */
2052103285Sikob		cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2053103285Sikob		cts->flags = 0;
2054103285Sikob
2055103285Sikob		cts->ccb_h.status = CAM_REQ_CMP;
2056103285Sikob		xpt_done(ccb);
2057103285Sikob		break;
2058103285Sikob	}
2059103285Sikob	case XPT_ABORT:
2060103285Sikob		ccb->ccb_h.status = CAM_UA_ABORT;
2061103285Sikob		xpt_done(ccb);
2062103285Sikob		break;
2063103285Sikob	default:
2064103285Sikob		ccb->ccb_h.status = CAM_REQ_INVALID;
2065103285Sikob		xpt_done(ccb);
2066103285Sikob		break;
2067103285Sikob	}
2068103285Sikob	return;
2069103285Sikob}
2070103285Sikob
2071103285Sikobstatic void
2072103285Sikobsbp_action(struct cam_sim *sim, union ccb *ccb)
2073103285Sikob{
2074103285Sikob	int s;
2075103285Sikob
2076103285Sikob	s = splfw();
2077103285Sikob	sbp_action1(sim, ccb);
2078103285Sikob	splx(s);
2079103285Sikob}
2080103285Sikob
2081103285Sikobstatic void
2082103285Sikobsbp_execute_ocb(void *arg,  bus_dma_segment_t *segments, int seg, int error)
2083103285Sikob{
2084103285Sikob	int i;
2085103285Sikob	struct sbp_ocb *ocb;
2086103285Sikob	struct sbp_ocb *prev;
2087103285Sikob	union ccb *ccb;
2088105633Ssimokawa	bus_dma_segment_t *s;
2089103285Sikob
2090103285Sikob	if (error)
2091103285Sikob		printf("sbp_execute_ocb: error=%d\n", error);
2092103285Sikob
2093103285Sikob	ocb = (struct sbp_ocb *)arg;
2094103285Sikob	if (seg == 1) {
2095103285Sikob		/* direct pointer */
2096103285Sikob		ocb->orb[3] = htonl(segments[0].ds_addr);
2097103285Sikob		ocb->orb[4] |= htonl(segments[0].ds_len);
2098103285Sikob	} else if(seg > 1) {
2099103285Sikob		/* page table */
2100103285SikobSBP_DEBUG(1)
2101103285Sikob		printf("sbp_execute_ocb: seg %d", seg);
2102103285Sikob		for (i = 0; i < seg; i++)
2103108712Ssimokawa#if __FreeBSD_version >= 500000
2104106543Ssimokawa			printf(", %tx:%zd", segments[i].ds_addr,
2105108712Ssimokawa#else
2106108877Ssimokawa			printf(", %x:%d", segments[i].ds_addr,
2107108712Ssimokawa#endif
2108103285Sikob						segments[i].ds_len);
2109103285Sikob		printf("\n");
2110103285SikobEND_DEBUG
2111103285Sikob		for (i = 0; i < seg; i++) {
2112105633Ssimokawa			s = &segments[i];
2113108877SsimokawaSBP_DEBUG(0)
2114108877Ssimokawa			/* XXX LSI Logic "< 16 byte" bug might be hit */
2115105633Ssimokawa			if (s->ds_len < 16)
2116105633Ssimokawa				printf("sbp_execute_ocb: warning, "
2117108877Ssimokawa#if __FreeBSD_version >= 500000
2118106543Ssimokawa					"segment length(%zd) is less than 16."
2119108877Ssimokawa#else
2120108877Ssimokawa					"segment length(%d) is less than 16."
2121108877Ssimokawa#endif
2122105633Ssimokawa					"(seg=%d/%d)\n", s->ds_len, i+1, seg);
2123108877SsimokawaEND_DEBUG
2124105633Ssimokawa			ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2125105633Ssimokawa			ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2126103285Sikob		}
2127103285Sikob		ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2128103285Sikob	}
2129103285Sikob
2130103285Sikob	ccb = ocb->ccb;
2131103285Sikob	prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2132103285Sikob	if (prev)
2133103285Sikob		sbp_doorbell(ocb->sdev);
2134103285Sikob	else
2135103285Sikob		sbp_orb_pointer(ocb->sdev, ocb);
2136103285Sikob}
2137103285Sikob
2138103285Sikobstatic void
2139103285Sikobsbp_poll(struct cam_sim *sim)
2140103285Sikob{
2141103285Sikob	/* should call fwohci_intr? */
2142103285Sikob	return;
2143103285Sikob}
2144103285Sikobstatic struct sbp_ocb *
2145103285Sikobsbp_dequeue_ocb(struct sbp_dev *sdev, u_int32_t orb_lo)
2146103285Sikob{
2147103285Sikob	struct sbp_ocb *ocb;
2148103285Sikob	struct sbp_ocb *next;
2149103285Sikob	int s = splfw(), order = 0;
2150103285Sikob	int flags;
2151103285Sikob
2152103285Sikob	for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2153103285Sikob		next = STAILQ_NEXT(ocb, ocb);
2154103285Sikob		flags = ocb->flags;
2155103285SikobSBP_DEBUG(1)
2156108712Ssimokawa#if __FreeBSD_version >= 500000
2157106543Ssimokawa		printf("orb: 0x%tx next: 0x%x, flags %x\n",
2158108712Ssimokawa#else
2159108712Ssimokawa		printf("orb: 0x%x next: 0x%lx, flags %x\n",
2160108712Ssimokawa#endif
2161103285Sikob			vtophys(&ocb->orb[0]), ntohl(ocb->orb[1]), flags);
2162103285SikobEND_DEBUG
2163103285Sikob		if (vtophys(&ocb->orb[0]) == orb_lo) {
2164103285Sikob			/* found */
2165103285Sikob			if (ocb->flags & OCB_RESERVED)
2166103285Sikob				ocb->flags |= OCB_DONE;
2167103285Sikob			else
2168103285Sikob				STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2169103285Sikob			if (ocb->ccb != NULL)
2170103285Sikob				untimeout(sbp_timeout, (caddr_t)ocb,
2171103285Sikob						ocb->ccb->ccb_h.timeout_ch);
2172103285Sikob			if (ocb->dmamap != NULL) {
2173103285Sikob				bus_dmamap_destroy(sdev->target->sbp->dmat,
2174103285Sikob							ocb->dmamap);
2175103285Sikob				ocb->dmamap = NULL;
2176103285Sikob			}
2177103285Sikob			break;
2178103285Sikob		} else {
2179103285Sikob			if ((ocb->flags & OCB_RESERVED) &&
2180103285Sikob					(ocb->flags & OCB_DONE)) {
2181103285Sikob				/* next orb must be fetched already */
2182103285Sikob				STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2183103285Sikob				sbp_free_ocb(sdev->target->sbp, ocb);
2184103285Sikob			} else
2185103285Sikob				order ++;
2186103285Sikob		}
2187103285Sikob	}
2188103285Sikob	splx(s);
2189103285SikobSBP_DEBUG(0)
2190103285Sikob	if (ocb && order > 0) {
2191103285Sikob		sbp_show_sdev_info(sdev, 2);
2192103285Sikob		printf("unordered execution order:%d\n", order);
2193103285Sikob	}
2194103285SikobEND_DEBUG
2195103285Sikob	return (ocb);
2196103285Sikob}
2197103285Sikob
2198103285Sikobstatic struct sbp_ocb *
2199103285Sikobsbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2200103285Sikob{
2201103285Sikob	int s = splfw();
2202103285Sikob	struct sbp_ocb *prev;
2203103285Sikob
2204103285SikobSBP_DEBUG(2)
2205103285Sikob	sbp_show_sdev_info(sdev, 2);
2206108712Ssimokawa#if __FreeBSD_version >= 500000
2207106543Ssimokawa	printf("sbp_enqueue_ocb orb=0x%tx in physical memory\n", vtophys(&ocb->orb[0]));
2208108712Ssimokawa#else
2209108712Ssimokawa	printf("sbp_enqueue_ocb orb=0x%x in physical memory\n", vtophys(&ocb->orb[0]));
2210108712Ssimokawa#endif
2211103285SikobEND_DEBUG
2212103285Sikob	prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2213103285Sikob	STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2214103285Sikob
2215103285Sikob	if (ocb->ccb != NULL)
2216103285Sikob		ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb,
2217103285Sikob					(ocb->ccb->ccb_h.timeout * hz) / 1000);
2218103285Sikob
2219103285Sikob	if (prev != NULL
2220103285Sikob		&& ((prev->flags & OCB_ACT_MASK) == OCB_ACT_CMD)
2221103285Sikob		&& ((ocb->flags & OCB_ACT_MASK) == OCB_ACT_CMD)) {
2222103285SikobSBP_DEBUG(1)
2223108712Ssimokawa#if __FreeBSD_version >= 500000
2224106543Ssimokawa	printf("linking chain 0x%tx -> 0x%tx\n", vtophys(&prev->orb[0]),
2225108712Ssimokawa#else
2226108712Ssimokawa	printf("linking chain 0x%x -> 0x%x\n", vtophys(&prev->orb[0]),
2227108712Ssimokawa#endif
2228103285Sikob			vtophys(&ocb->orb[0]));
2229103285SikobEND_DEBUG
2230103285Sikob		prev->flags |= OCB_RESERVED;
2231103285Sikob		prev->orb[1] = htonl(vtophys(&ocb->orb[0]));
2232103285Sikob		prev->orb[0] = 0;
2233103285Sikob	} else {
2234103285Sikob		prev = NULL;
2235103285Sikob	}
2236103285Sikob	splx(s);
2237103285Sikob
2238103285Sikob	return prev;
2239103285Sikob}
2240103285Sikob
2241103285Sikobstatic struct sbp_ocb *
2242103285Sikobsbp_get_ocb(struct sbp_softc *sbp)
2243103285Sikob{
2244103285Sikob	struct sbp_ocb *ocb;
2245103285Sikob	int s = splfw();
2246103285Sikob	ocb = STAILQ_FIRST(&sbp->free_ocbs);
2247103285Sikob	if (ocb == NULL) {
2248103285Sikob		printf("ocb shortage!!!\n");
2249103285Sikob		return NULL;
2250103285Sikob	}
2251103285Sikob	STAILQ_REMOVE(&sbp->free_ocbs, ocb, sbp_ocb, ocb);
2252103285Sikob	splx(s);
2253103285Sikob	ocb->ccb = NULL;
2254103285Sikob	return (ocb);
2255103285Sikob}
2256103285Sikob
2257103285Sikobstatic void
2258103285Sikobsbp_free_ocb(struct sbp_softc *sbp, struct sbp_ocb *ocb)
2259103285Sikob{
2260103285Sikob#if 0 /* XXX make sure that ocb has ccb */
2261103285Sikob	if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0 &&
2262103285Sikob	    (ocb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
2263103285Sikob		ocb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2264103285Sikob		sbp->flags &= ~SBP_RESOURCE_SHORTAGE;
2265103285Sikob	}
2266103285Sikob#else
2267103285Sikob	if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0)
2268103285Sikob		sbp->flags &= ~SBP_RESOURCE_SHORTAGE;
2269103285Sikob#endif
2270103285Sikob	ocb->flags = 0;
2271103285Sikob	ocb->ccb = NULL;
2272103285Sikob	STAILQ_INSERT_TAIL(&sbp->free_ocbs, ocb, ocb);
2273103285Sikob}
2274103285Sikob
2275103285Sikobstatic void
2276103285Sikobsbp_abort_ocb(struct sbp_ocb *ocb, int status)
2277103285Sikob{
2278103285Sikob	struct sbp_dev *sdev;
2279103285Sikob
2280103285Sikob	sdev = ocb->sdev;
2281103285SikobSBP_DEBUG(0)
2282103285Sikob	sbp_show_sdev_info(sdev, 2);
2283103285Sikob	printf("sbp_abort_ocb 0x%x\n", status);
2284105792Ssimokawa	if (ocb->ccb != NULL)
2285105792Ssimokawa		sbp_print_scsi_cmd(ocb);
2286103285SikobEND_DEBUG
2287103285Sikob	if (ocb->ccb != NULL && !(ocb->flags & OCB_DONE)) {
2288103285Sikob		if (status != CAM_CMD_TIMEOUT)
2289103285Sikob			untimeout(sbp_timeout, (caddr_t)ocb,
2290103285Sikob						ocb->ccb->ccb_h.timeout_ch);
2291103285Sikob		ocb->ccb->ccb_h.status = status;
2292103285Sikob		xpt_done(ocb->ccb);
2293103285Sikob	}
2294103285Sikob	if (ocb->dmamap != NULL) {
2295103285Sikob		bus_dmamap_destroy(sdev->target->sbp->dmat, ocb->dmamap);
2296103285Sikob		ocb->dmamap = NULL;
2297103285Sikob	}
2298103285Sikob	sbp_free_ocb(sdev->target->sbp, ocb);
2299103285Sikob}
2300103285Sikob
2301103285Sikobstatic void
2302103285Sikobsbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2303103285Sikob{
2304103285Sikob	int s;
2305105792Ssimokawa	struct sbp_ocb *ocb, *next;
2306105792Ssimokawa	STAILQ_HEAD(, sbp_ocb) temp;
2307103285Sikob
2308103285Sikob	s = splfw();
2309105792Ssimokawa
2310105792Ssimokawa	bcopy(&sdev->ocbs, &temp, sizeof(temp));
2311105792Ssimokawa	STAILQ_INIT(&sdev->ocbs);
2312105792Ssimokawa	for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2313105792Ssimokawa		next = STAILQ_NEXT(ocb, ocb);
2314103285Sikob		sbp_abort_ocb(ocb, status);
2315103285Sikob	}
2316105792Ssimokawa
2317103285Sikob	splx(s);
2318103285Sikob}
2319103285Sikob
2320103285Sikobstatic devclass_t sbp_devclass;
2321103285Sikob
2322103285Sikobstatic device_method_t sbp_methods[] = {
2323103285Sikob	/* device interface */
2324103285Sikob	DEVMETHOD(device_identify,	sbp_identify),
2325103285Sikob	DEVMETHOD(device_probe,		sbp_probe),
2326103285Sikob	DEVMETHOD(device_attach,	sbp_attach),
2327103285Sikob	DEVMETHOD(device_detach,	sbp_detach),
2328110145Ssimokawa	DEVMETHOD(device_shutdown,	sbp_shutdown),
2329103285Sikob
2330103285Sikob	{ 0, 0 }
2331103285Sikob};
2332103285Sikob
2333103285Sikobstatic driver_t sbp_driver = {
2334103285Sikob	"sbp",
2335103285Sikob	sbp_methods,
2336103285Sikob	sizeof(struct sbp_softc),
2337103285Sikob};
2338103285SikobDRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2339103285SikobMODULE_VERSION(sbp, 1);
2340103285SikobMODULE_DEPEND(sbp, firewire, 1, 1, 1);
2341103285SikobMODULE_DEPEND(sbp, cam, 1, 1, 1);
2342