sbp.c revision 111199
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 111199 2003-02-21 02:09:04Z 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
83111040Ssimokawa#define MAX_FREEZE	10
84111040Ssimokawa
85103285Sikob#define ORB_NOTIFY	(1 << 31)
86103285Sikob#define	ORB_FMT_STD	(0 << 29)
87103285Sikob#define	ORB_FMT_VED	(2 << 29)
88103285Sikob#define	ORB_FMT_NOP	(3 << 29)
89103285Sikob#define	ORB_FMT_MSK	(3 << 29)
90103285Sikob#define	ORB_EXV		(1 << 28)
91103285Sikob/* */
92103285Sikob#define	ORB_CMD_IN	(1 << 27)
93103285Sikob/* */
94103285Sikob#define	ORB_CMD_SPD(x)	((x) << 24)
95103285Sikob#define	ORB_CMD_MAXP(x)	((x) << 20)
96103285Sikob#define	ORB_RCN_TMO(x)	((x) << 20)
97103285Sikob#define	ORB_CMD_PTBL	(1 << 19)
98103285Sikob#define	ORB_CMD_PSZ(x)	((x) << 16)
99103285Sikob
100103285Sikob#define	ORB_FUN_LGI	(0 << 16)
101103285Sikob#define	ORB_FUN_QLG	(1 << 16)
102103285Sikob#define	ORB_FUN_RCN	(3 << 16)
103103285Sikob#define	ORB_FUN_LGO	(7 << 16)
104103285Sikob#define	ORB_FUN_ATA	(0xb << 16)
105103285Sikob#define	ORB_FUN_ATS	(0xc << 16)
106103285Sikob#define	ORB_FUN_LUR	(0xe << 16)
107103285Sikob#define	ORB_FUN_RST	(0xf << 16)
108103285Sikob#define	ORB_FUN_MSK	(0xf << 16)
109103285Sikob
110103285Sikobstatic char *orb_fun_name[] = {
111103285Sikob	/* 0 */ "LOGIN",
112103285Sikob	/* 1 */ "QUERY LOGINS",
113103285Sikob	/* 2 */ "Reserved",
114103285Sikob	/* 3 */ "RECONNECT",
115103285Sikob	/* 4 */ "SET PASSWORD",
116103285Sikob	/* 5 */ "Reserved",
117103285Sikob	/* 6 */ "Reserved",
118103285Sikob	/* 7 */ "LOGOUT",
119103285Sikob	/* 8 */ "Reserved",
120103285Sikob	/* 9 */ "Reserved",
121103285Sikob	/* A */ "Reserved",
122103285Sikob	/* B */ "ABORT TASK",
123103285Sikob	/* C */ "ABORT TASK SET",
124103285Sikob	/* D */ "Reserved",
125103285Sikob	/* E */ "LOGICAL UNIT RESET",
126103285Sikob	/* F */ "TARGET RESET"
127103285Sikob};
128103285Sikob
129103285Sikob#define ORB_RES_CMPL 0
130103285Sikob#define ORB_RES_FAIL 1
131103285Sikob#define ORB_RES_ILLE 2
132103285Sikob#define ORB_RES_VEND 3
133103285Sikob
134111199Ssimokawastatic int debug = 1;
135103285Sikobstatic int auto_login = 1;
136103285Sikobstatic int max_speed = 2;
137111199Ssimokawastatic int sbp_cold = 1;
138103285Sikob
139103285SikobSYSCTL_DECL(_hw_firewire);
140103285SikobSYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0, "SBP-II Subsystem");
141103285SikobSYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0,
142103285Sikob	"SBP debug flag");
143103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0,
144103285Sikob	"SBP perform login automatically");
145103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0,
146103285Sikob	"SBP transfer max speed");
147103285Sikob
148103285Sikob#define SBP_DEBUG(x)	if (debug > x) {
149103285Sikob#define END_DEBUG	}
150103285Sikob
151103285Sikob#define NEED_RESPONSE 0
152103285Sikob
153103285Sikobstruct ind_ptr {
154103285Sikob	u_int32_t hi,lo;
155103285Sikob};
156103285Sikob#define SBP_IND_MAX 0x20
157103285Sikobstruct sbp_ocb {
158103285Sikob	STAILQ_ENTRY(sbp_ocb)	ocb;
159103285Sikob	union ccb	*ccb;
160103285Sikob	volatile u_int32_t	orb[8];
161103285Sikob	volatile struct ind_ptr  ind_ptr[SBP_IND_MAX];
162103285Sikob	struct sbp_dev	*sdev;
163103285Sikob	int		flags;
164103285Sikob	bus_dmamap_t	dmamap;
165103285Sikob};
166103285Sikob#define OCB_ACT_MGM 0
167103285Sikob#define OCB_ACT_CMD 1
168103285Sikob#define OCB_ACT_MASK 3
169103285Sikob#define OCB_RESERVED 0x10
170103285Sikob#define OCB_DONE 0x20
171103285Sikob
172103285Sikob#define SBP_RESOURCE_SHORTAGE 0x10
173103285Sikob
174103285Sikobstruct sbp_login_res{
175103285Sikob	u_int16_t	len;
176103285Sikob	u_int16_t	id;
177103285Sikob	u_int16_t	res0;
178103285Sikob	u_int16_t	cmd_hi;
179103285Sikob	u_int32_t	cmd_lo;
180103285Sikob	u_int16_t	res1;
181103285Sikob	u_int16_t	recon_hold;
182103285Sikob};
183103285Sikobstruct sbp_status{
184103285Sikob	u_int8_t	len:3,
185103285Sikob			dead:1,
186103285Sikob			resp:2,
187103285Sikob			src:2;
188103285Sikob	u_int8_t	status:8;
189103285Sikob	u_int16_t	orb_hi;
190103285Sikob	u_int32_t	orb_lo;
191103285Sikob	u_int32_t	data[6];
192103285Sikob};
193103285Sikobstruct sbp_cmd_status{
194103285Sikob#define SBP_SFMT_CURR 0
195103285Sikob#define SBP_SFMT_DEFER 1
196103285Sikob	u_int8_t	status:6,
197103285Sikob			sfmt:2;
198103285Sikob	u_int8_t	s_key:4,
199103285Sikob			ill_len:1,
200103285Sikob			eom:1,
201103285Sikob			mark:1,
202103285Sikob			valid:1;
203103285Sikob	u_int8_t	s_code;
204103285Sikob	u_int8_t	s_qlfr;
205103285Sikob	u_int32_t	info;
206103285Sikob	u_int32_t	cdb;
207103285Sikob	u_int32_t	fru:8,
208103285Sikob			s_keydep:24;
209103285Sikob	u_int32_t	vend[2];
210103285Sikob};
211103285Sikob
212103285Sikobstruct sbp_dev{
213103285Sikob#define SBP_DEV_RESET		0	/* accept login */
214110336Ssimokawa#if 0
215103285Sikob#define SBP_DEV_LOGIN		1	/* to login */
216103285Sikob#define SBP_DEV_RECONN		2	/* to reconnect */
217110336Ssimokawa#endif
218103285Sikob#define SBP_DEV_TOATTACH	3	/* to attach */
219103285Sikob#define SBP_DEV_PROBE		4	/* scan lun */
220103285Sikob#define SBP_DEV_ATTACHED	5	/* in operation */
221103285Sikob#define SBP_DEV_DEAD		6	/* unavailable unit */
222103285Sikob#define SBP_DEV_RETRY		7	/* unavailable unit */
223110336Ssimokawa	u_int8_t status:4,
224110336Ssimokawa#define SBP_DEV_TIMEOUT		1
225110336Ssimokawa		 flags:4;
226108503Ssimokawa	u_int8_t type;
227108503Ssimokawa	u_int16_t lun_id;
228103285Sikob	struct cam_path *path;
229103285Sikob	struct sbp_target *target;
230103285Sikob	struct sbp_login_res login;
231103285Sikob	STAILQ_HEAD(, sbp_ocb) ocbs;
232103285Sikob	char vendor[32];
233103285Sikob	char product[32];
234103285Sikob	char revision[10];
235103285Sikob};
236103285Sikob
237103285Sikobstruct sbp_target {
238103285Sikob	int target_id;
239103285Sikob	int num_lun;
240103285Sikob	struct sbp_dev	*luns;
241103285Sikob	struct sbp_softc *sbp;
242103285Sikob	struct fw_device *fwdev;
243103285Sikob	u_int32_t mgm_hi, mgm_lo;
244103285Sikob};
245103285Sikob
246103285Sikobstruct sbp_softc {
247103285Sikob	struct firewire_dev_comm fd;
248103285Sikob	unsigned char flags;
249103285Sikob	struct cam_sim  *sim;
250103285Sikob	struct sbp_target targets[SBP_NUM_TARGETS];
251103285Sikob	struct fw_bind fwb;
252103285Sikob	STAILQ_HEAD(, sbp_ocb) free_ocbs;
253103285Sikob	struct sbp_ocb *ocb;
254103285Sikob	bus_dma_tag_t	dmat;
255103285Sikob};
256103285Sikobstatic void sbp_post_explore __P((void *));
257103285Sikobstatic void sbp_recv __P((struct fw_xfer *));
258103285Sikobstatic void sbp_login_callback __P((struct fw_xfer *));
259103285Sikobstatic void sbp_cmd_callback __P((struct fw_xfer *));
260103285Sikobstatic void sbp_orb_pointer __P((struct sbp_dev *, struct sbp_ocb *));
261103285Sikobstatic void sbp_execute_ocb __P((void *,  bus_dma_segment_t *, int, int));
262103285Sikobstatic void sbp_free_ocb __P((struct sbp_softc *, struct sbp_ocb *));
263103285Sikobstatic void sbp_abort_ocb __P((struct sbp_ocb *, int));
264103285Sikobstatic void sbp_abort_all_ocbs __P((struct sbp_dev *, int));
265103285Sikobstatic struct fw_xfer * sbp_write_cmd __P((struct sbp_dev *, int, int));
266103285Sikobstatic struct sbp_ocb * sbp_get_ocb __P((struct sbp_softc *));
267103285Sikobstatic struct sbp_ocb * sbp_enqueue_ocb __P((struct sbp_dev *, struct sbp_ocb *));
268103285Sikobstatic struct sbp_ocb * sbp_dequeue_ocb __P((struct sbp_dev *, u_int32_t));
269110145Ssimokawastatic void sbp_cam_detach_target __P((struct sbp_target *));
270103285Sikobstatic void sbp_timeout __P((void *arg));
271110336Ssimokawastatic void sbp_mgm_orb __P((struct sbp_dev *, int, u_int16_t, u_int32_t));
272103285Sikob
273108281SsimokawaMALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire");
274103285Sikob
275103285Sikob/* cam related functions */
276103285Sikobstatic void	sbp_action(struct cam_sim *sim, union ccb *ccb);
277103285Sikobstatic void	sbp_poll(struct cam_sim *sim);
278103285Sikobstatic void	sbp_cam_callback(struct cam_periph *periph,
279103285Sikob					union ccb *ccb);
280103285Sikobstatic void	sbp_cam_scan_lun(struct sbp_dev *sdev);
281103285Sikob
282103285Sikobstatic char *orb_status0[] = {
283103285Sikob	/* 0 */ "No additional information to report",
284103285Sikob	/* 1 */ "Request type not supported",
285103285Sikob	/* 2 */ "Speed not supported",
286103285Sikob	/* 3 */ "Page size not supported",
287103285Sikob	/* 4 */ "Access denied",
288103285Sikob	/* 5 */ "Logical unit not supported",
289103285Sikob	/* 6 */ "Maximum payload too small",
290103285Sikob	/* 7 */ "Reserved for future standardization",
291103285Sikob	/* 8 */ "Resources unavailable",
292103285Sikob	/* 9 */ "Function rejected",
293103285Sikob	/* A */ "Login ID not recognized",
294103285Sikob	/* B */ "Dummy ORB completed",
295103285Sikob	/* C */ "Request aborted",
296103285Sikob	/* FF */ "Unspecified error"
297103285Sikob#define MAX_ORB_STATUS0 0xd
298103285Sikob};
299103285Sikob
300103285Sikobstatic char *orb_status1_object[] = {
301103285Sikob	/* 0 */ "Operation request block (ORB)",
302103285Sikob	/* 1 */ "Data buffer",
303103285Sikob	/* 2 */ "Page table",
304103285Sikob	/* 3 */ "Unable to specify"
305103285Sikob};
306103285Sikob
307103285Sikobstatic char *orb_status1_serial_bus_error[] = {
308103285Sikob	/* 0 */ "Missing acknowledge",
309103285Sikob	/* 1 */ "Reserved; not to be used",
310103285Sikob	/* 2 */ "Time-out error",
311103285Sikob	/* 3 */ "Reserved; not to be used",
312103285Sikob	/* 4 */ "Busy retry limit exceeded(X)",
313103285Sikob	/* 5 */ "Busy retry limit exceeded(A)",
314103285Sikob	/* 6 */ "Busy retry limit exceeded(B)",
315103285Sikob	/* 7 */ "Reserved for future standardization",
316103285Sikob	/* 8 */ "Reserved for future standardization",
317103285Sikob	/* 9 */ "Reserved for future standardization",
318103285Sikob	/* A */ "Reserved for future standardization",
319103285Sikob	/* B */ "Tardy retry limit exceeded",
320103285Sikob	/* C */ "Conflict error",
321103285Sikob	/* D */ "Data error",
322103285Sikob	/* E */ "Type error",
323103285Sikob	/* F */ "Address error"
324103285Sikob};
325103285Sikob
326103285Sikobstatic void
327103285Sikobsbp_identify(driver_t *driver, device_t parent)
328103285Sikob{
329103285Sikob	device_t child;
330103285SikobSBP_DEBUG(0)
331103285Sikob	printf("sbp_identify\n");
332103285SikobEND_DEBUG
333103285Sikob
334103285Sikob	child = BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent));
335103285Sikob}
336103285Sikob
337103285Sikob/*
338103285Sikob * sbp_probe()
339103285Sikob */
340103285Sikobstatic int
341103285Sikobsbp_probe(device_t dev)
342103285Sikob{
343103285Sikob	device_t pa;
344103285Sikob
345103285SikobSBP_DEBUG(0)
346103285Sikob	printf("sbp_probe\n");
347103285SikobEND_DEBUG
348103285Sikob
349103285Sikob	pa = device_get_parent(dev);
350103285Sikob	if(device_get_unit(dev) != device_get_unit(pa)){
351103285Sikob		return(ENXIO);
352103285Sikob	}
353103285Sikob
354103285Sikob	device_set_desc(dev, "SBP2/SCSI over firewire");
355103285Sikob	return (0);
356103285Sikob}
357103285Sikob
358103285Sikobstatic void
359103285Sikobsbp_show_sdev_info(struct sbp_dev *sdev, int new)
360103285Sikob{
361103285Sikob	struct fw_device *fwdev;
362103285Sikob
363103285Sikob	printf("%s:%d:%d ",
364103285Sikob		device_get_nameunit(sdev->target->sbp->fd.dev),
365103285Sikob		sdev->target->target_id,
366103285Sikob		sdev->lun_id
367103285Sikob	);
368103285Sikob	if (new == 2) {
369103285Sikob		return;
370103285Sikob	}
371103285Sikob	fwdev = sdev->target->fwdev;
372103285Sikob	printf("ordered:%d type:%d EUI:%08x%08x node:%d "
373103285Sikob		"speed:%d maxrec:%d",
374108503Ssimokawa		(sdev->type & 0x40) >> 6,
375108503Ssimokawa		(sdev->type & 0x1f),
376103285Sikob		fwdev->eui.hi,
377103285Sikob		fwdev->eui.lo,
378103285Sikob		fwdev->dst,
379103285Sikob		fwdev->speed,
380103285Sikob		fwdev->maxrec
381103285Sikob	);
382103285Sikob	if (new)
383103285Sikob		printf(" new!\n");
384103285Sikob	else
385103285Sikob		printf("\n");
386103285Sikob	sbp_show_sdev_info(sdev, 2);
387103285Sikob	printf("'%s' '%s' '%s'\n", sdev->vendor, sdev->product, sdev->revision);
388103285Sikob}
389103285Sikob
390110184Ssimokawastatic struct {
391110184Ssimokawa	int bus;
392110184Ssimokawa	int target;
393110184Ssimokawa	struct fw_eui64 eui;
394110184Ssimokawa} wired[] = {
395110184Ssimokawa	/* Bus	Target	EUI64 */
396110184Ssimokawa#if 0
397110184Ssimokawa	{0,	2,	{0x00018ea0, 0x01fd0154}},	/* Logitec HDD */
398110184Ssimokawa	{0,	0,	{0x00018ea6, 0x00100682}},	/* Logitec DVD */
399110184Ssimokawa	{0,	1,	{0x00d03200, 0xa412006a}},	/* Yano HDD */
400110184Ssimokawa#endif
401110184Ssimokawa	{-1,	-1,	{0,0}}
402110184Ssimokawa};
403110184Ssimokawa
404110184Ssimokawastatic int
405110184Ssimokawasbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev)
406110184Ssimokawa{
407110184Ssimokawa	int bus, i, target=-1;
408110184Ssimokawa	char w[SBP_NUM_TARGETS];
409110184Ssimokawa
410110184Ssimokawa	bzero(w, sizeof(w));
411110184Ssimokawa	bus = device_get_unit(sbp->fd.dev);
412110184Ssimokawa
413110184Ssimokawa	/* XXX wired-down configuration should be gotten from
414110184Ssimokawa					tunable or device hint */
415110184Ssimokawa	for (i = 0; wired[i].bus >= 0; i ++) {
416110184Ssimokawa		if (wired[i].bus == bus) {
417110184Ssimokawa			w[wired[i].target] = 1;
418110184Ssimokawa			if (wired[i].eui.hi == fwdev->eui.hi &&
419110184Ssimokawa					wired[i].eui.lo == fwdev->eui.lo)
420110184Ssimokawa				target = wired[i].target;
421110184Ssimokawa		}
422110184Ssimokawa	}
423110184Ssimokawa	if (target >= 0) {
424110184Ssimokawa		if(target < SBP_NUM_TARGETS &&
425110184Ssimokawa				sbp->targets[target].fwdev == NULL)
426110184Ssimokawa			return(target);
427110184Ssimokawa		device_printf(sbp->fd.dev,
428110184Ssimokawa			"target %d is not free for %08x:%08x\n",
429110184Ssimokawa			target, fwdev->eui.hi, fwdev->eui.lo);
430110184Ssimokawa		target = -1;
431110184Ssimokawa	}
432110184Ssimokawa	/* non-wired target */
433110184Ssimokawa	for (i = 0; i < SBP_NUM_TARGETS; i ++)
434110184Ssimokawa		if (sbp->targets[i].fwdev == NULL && w[i] == 0) {
435110184Ssimokawa			target = i;
436110184Ssimokawa			break;
437110184Ssimokawa		}
438110184Ssimokawa
439110184Ssimokawa	return target;
440110184Ssimokawa}
441110184Ssimokawa
442103285Sikobstatic struct sbp_target *
443103285Sikobsbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
444103285Sikob{
445108503Ssimokawa	int i, maxlun, lun;
446103285Sikob	struct sbp_target *target;
447103285Sikob	struct sbp_dev *sdev;
448108503Ssimokawa	struct crom_context cc;
449108503Ssimokawa	struct csrreg *reg;
450103285Sikob
451103285SikobSBP_DEBUG(1)
452103285Sikob	printf("sbp_alloc_target\n");
453103285SikobEND_DEBUG
454110184Ssimokawa	i = sbp_new_target(sbp, fwdev);
455110184Ssimokawa	if (i < 0) {
456110184Ssimokawa		device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n");
457103285Sikob		return NULL;
458103285Sikob	}
459103285Sikob	/* new target */
460103285Sikob	target = &sbp->targets[i];
461103285Sikob	target->sbp = sbp;
462103285Sikob	target->fwdev = fwdev;
463103285Sikob	target->target_id = i;
464103285Sikob	if((target->mgm_lo = getcsrdata(fwdev, 0x54)) == 0 ){
465103285Sikob		/* bad target */
466103285Sikob		printf("NULL management address\n");
467103285Sikob		target->fwdev = NULL;
468103285Sikob		return NULL;
469103285Sikob	}
470103285Sikob	target->mgm_hi = 0xffff;
471103285Sikob	target->mgm_lo = 0xf0000000 | target->mgm_lo << 2;
472103285Sikob	/* XXX num_lun may be changed. realloc luns? */
473108503Ssimokawa	crom_init_context(&cc, target->fwdev->csrrom);
474108503Ssimokawa	/* XXX shoud parse appropriate unit directories only */
475108503Ssimokawa	maxlun = -1;
476108503Ssimokawa	while (cc.depth >= 0) {
477108503Ssimokawa		reg = crom_search_key(&cc, CROM_LUN);
478108503Ssimokawa		if (reg == NULL)
479108503Ssimokawa			break;
480110839Ssimokawa		lun = reg->val & 0xffff;
481108642SsimokawaSBP_DEBUG(0)
482108642Ssimokawa		printf("target %d lun %d found\n", target->target_id, lun);
483108642SsimokawaEND_DEBUG
484108503Ssimokawa		if (maxlun < lun)
485108503Ssimokawa			maxlun = lun;
486108503Ssimokawa		crom_next(&cc);
487108503Ssimokawa	}
488110839Ssimokawa	if (maxlun < 0)
489110839Ssimokawa		printf("no lun found!\n");
490110839Ssimokawa	if (maxlun >= SBP_NUM_LUNS)
491110839Ssimokawa		maxlun = SBP_NUM_LUNS;
492108503Ssimokawa	target->num_lun = maxlun + 1;
493103285Sikob	target->luns = (struct sbp_dev *) malloc(
494103285Sikob				sizeof(struct sbp_dev) * target->num_lun,
495103285Sikob				M_SBP, M_NOWAIT | M_ZERO);
496103285Sikob	for (i = 0; i < target->num_lun; i++) {
497103285Sikob		sdev = &target->luns[i];
498103285Sikob		sdev->lun_id = i;
499103285Sikob		sdev->target = target;
500103285Sikob		STAILQ_INIT(&sdev->ocbs);
501108503Ssimokawa		sdev->status = SBP_DEV_DEAD;
502103285Sikob	}
503108503Ssimokawa	crom_init_context(&cc, target->fwdev->csrrom);
504108503Ssimokawa	while (cc.depth >= 0) {
505108503Ssimokawa		reg = crom_search_key(&cc, CROM_LUN);
506108503Ssimokawa		if (reg == NULL)
507108503Ssimokawa			break;
508110839Ssimokawa		lun = reg->val & 0xffff;
509110839Ssimokawa		if (lun >= SBP_NUM_LUNS) {
510110839Ssimokawa			printf("too large lun %d\n", lun);
511110839Ssimokawa			continue;
512110839Ssimokawa		}
513108503Ssimokawa		target->luns[lun].status = SBP_DEV_RESET;
514110839Ssimokawa		target->luns[lun].type = (reg->val & 0xf0000) >> 16;
515108503Ssimokawa		crom_next(&cc);
516110839Ssimokawa	    }
517110839Ssimokawa	    return target;
518110839Ssimokawa    }
519103285Sikob
520103285Sikobstatic void
521103285Sikobsbp_get_text_leaf(struct fw_device *fwdev, int key, char *buf, int len)
522103285Sikob{
523103285Sikob	static char *nullstr = "(null)";
524103285Sikob	int i, clen, found=0;
525103285Sikob	struct csrhdr *chdr;
526103285Sikob	struct csrreg *creg;
527103285Sikob	u_int32_t *src, *dst;
528103285Sikob
529103285Sikob	chdr = (struct csrhdr *)&fwdev->csrrom[0];
530110839Ssimokawa	/* skip crom header, bus info and root directory */
531110839Ssimokawa	creg = (struct csrreg *)chdr + chdr->info_len + 2;
532110839Ssimokawa	/* search unitl the one before the last. */
533110839Ssimokawa	for (i = chdr->info_len + 2; i < fwdev->rommax / 4; i++) {
534103285Sikob		if((creg++)->key == key){
535103285Sikob			found = 1;
536103285Sikob			break;
537103285Sikob		}
538103285Sikob	}
539110798Ssimokawa	if (!found || creg->key != CROM_TEXTLEAF) {
540103285Sikob		strncpy(buf, nullstr, len);
541103285Sikob		return;
542103285Sikob	}
543103285Sikob	src = (u_int32_t *) creg + creg->val;
544103285Sikob	clen = ((*src >> 16) - 2) * 4;
545103285Sikob	src += 3;
546103285Sikob	dst = (u_int32_t *) buf;
547103285Sikob	if (len < clen)
548103285Sikob		clen = len;
549103285Sikob	for (i = 0; i < clen/4; i++)
550103285Sikob		*dst++ = htonl(*src++);
551103285Sikob	buf[clen] = 0;
552103285Sikob}
553103285Sikob
554103285Sikobstatic void
555103285Sikobsbp_probe_lun(struct sbp_dev *sdev)
556103285Sikob{
557103285Sikob	struct fw_device *fwdev;
558103285Sikob	int rev;
559103285Sikob
560103285Sikob	fwdev = sdev->target->fwdev;
561103285Sikob	bzero(sdev->vendor, sizeof(sdev->vendor));
562103285Sikob	bzero(sdev->product, sizeof(sdev->product));
563103285Sikob	sbp_get_text_leaf(fwdev, 0x03, sdev->vendor, sizeof(sdev->vendor));
564103285Sikob	sbp_get_text_leaf(fwdev, 0x17, sdev->product, sizeof(sdev->product));
565103285Sikob	rev = getcsrdata(sdev->target->fwdev, 0x3c);
566103285Sikob	snprintf(sdev->revision, sizeof(sdev->revision), "%06x", rev);
567103285Sikob}
568103285Sikobstatic void
569103285Sikobsbp_probe_target(struct sbp_target *target, int alive)
570103285Sikob{
571103285Sikob	struct sbp_softc *sbp;
572103285Sikob	struct sbp_dev *sdev;
573103285Sikob	struct firewire_comm *fc;
574103285Sikob	int i;
575103285Sikob
576103285SikobSBP_DEBUG(1)
577103285Sikob	printf("sbp_probe_target %d\n", target->target_id);
578103285Sikob	if (!alive)
579103285Sikob		printf("not alive\n");
580103285SikobEND_DEBUG
581103285Sikob
582103285Sikob	sbp = target->sbp;
583103285Sikob	fc = target->sbp->fd.fc;
584103285Sikob	for (i=0; i < target->num_lun; i++) {
585103285Sikob		sdev = &target->luns[i];
586103285Sikob		if (alive && (sdev->status != SBP_DEV_DEAD)) {
587103285Sikob			if (sdev->path != NULL) {
588103285Sikob				xpt_freeze_devq(sdev->path, 1);
589103285Sikob			}
590103285Sikob			sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ);
591103285Sikob			switch (sdev->status) {
592110336Ssimokawa			case SBP_DEV_RESET:
593103285Sikob				/* new or revived target */
594103285Sikob				sbp_probe_lun(sdev);
595103285Sikob				if (auto_login) {
596103285Sikob					sdev->status = SBP_DEV_TOATTACH;
597110336Ssimokawa					sbp_mgm_orb(sdev, ORB_FUN_LGI, 0, 0);
598103285Sikob				}
599103285Sikob				break;
600110336Ssimokawa			case SBP_DEV_RETRY:
601110336Ssimokawa				sbp_probe_lun(sdev);
602110336Ssimokawa			default:
603110336Ssimokawa				sbp_mgm_orb(sdev, ORB_FUN_RCN, 0, 0);
604110336Ssimokawa				break;
605103285Sikob			}
606107653SsimokawaSBP_DEBUG(0)
607103285Sikob			sbp_show_sdev_info(sdev,
608103285Sikob					(sdev->status == SBP_DEV_TOATTACH));
609107653SsimokawaEND_DEBUG
610103285Sikob		} else {
611103285Sikob			switch (sdev->status) {
612103285Sikob			case SBP_DEV_ATTACHED:
613103285SikobSBP_DEBUG(0)
614103285Sikob				/* the device has gone */
615103285Sikob				sbp_show_sdev_info(sdev, 2);
616103285Sikob				printf("lost target\n");
617103285SikobEND_DEBUG
618103285Sikob				if (sdev->path)
619103285Sikob					xpt_freeze_devq(sdev->path, 1);
620103285Sikob				sdev->status = SBP_DEV_RETRY;
621103285Sikob				sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ);
622103285Sikob				break;
623103285Sikob			case SBP_DEV_PROBE:
624103285Sikob			case SBP_DEV_TOATTACH:
625103285Sikob				sdev->status = SBP_DEV_RESET;
626103285Sikob				break;
627103285Sikob			case SBP_DEV_RETRY:
628103285Sikob			case SBP_DEV_RESET:
629103285Sikob			case SBP_DEV_DEAD:
630103285Sikob				break;
631103285Sikob			}
632103285Sikob		}
633103285Sikob	}
634103285Sikob}
635103285Sikob
636103285Sikobstatic void
637103285Sikobsbp_post_explore(void *arg)
638103285Sikob{
639103285Sikob	struct sbp_softc *sbp = (struct sbp_softc *)arg;
640103285Sikob	struct sbp_target *target;
641103285Sikob	struct fw_device *fwdev;
642103285Sikob	int i, alive;
643103285Sikob
644111199SsimokawaSBP_DEBUG(0)
645111199Ssimokawa	printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
646103285SikobEND_DEBUG
647103285Sikob#if 0
648103285Sikob	xpt_freeze_simq(sbp->sim, /*count*/ 1);
649103285Sikob#endif
650111199Ssimokawa	if (sbp_cold > 0)
651111199Ssimokawa		sbp_cold --;
652103285Sikob	/* Gabage Collection */
653103285Sikob	for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
654103285Sikob		target = &sbp->targets[i];
655110193Ssimokawa		STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
656110193Ssimokawa			if (target->fwdev == NULL || target->fwdev == fwdev)
657110193Ssimokawa				break;
658103285Sikob		if(fwdev == NULL){
659103285Sikob			/* device has removed in lower driver */
660110145Ssimokawa			sbp_cam_detach_target(target);
661110145Ssimokawa			if (target->luns != NULL)
662110145Ssimokawa				free(target->luns, M_SBP);
663110145Ssimokawa			target->num_lun = 0;;
664110145Ssimokawa			target->luns = NULL;
665110145Ssimokawa			target->fwdev = NULL;
666103285Sikob		}
667103285Sikob	}
668103285Sikob	/* traverse device list */
669110193Ssimokawa	STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
670103285SikobSBP_DEBUG(0)
671103285Sikob		printf("sbp_post_explore: EUI:%08x%08x ",
672103285Sikob				fwdev->eui.hi, fwdev->eui.lo);
673103285Sikob		if (fwdev->status == FWDEVATTACHED) {
674103285Sikob			printf("spec=%d key=%d.\n",
675103285Sikob			getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10,
676103285Sikob			getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2);
677103285Sikob		} else {
678103285Sikob			printf("not attached, state=%d.\n", fwdev->status);
679103285Sikob		}
680103285SikobEND_DEBUG
681103285Sikob		alive = (fwdev->status == FWDEVATTACHED)
682103285Sikob			&& (getcsrdata(fwdev, CSRKEY_SPEC) == CSRVAL_ANSIT10)
683103285Sikob			&& (getcsrdata(fwdev, CSRKEY_VER) == CSRVAL_T10SBP2);
684103285Sikob		for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
685103285Sikob			target = &sbp->targets[i];
686103285Sikob			if(target->fwdev == fwdev ) {
687103285Sikob				/* known target */
688103285Sikob				break;
689103285Sikob			}
690103285Sikob		}
691103285Sikob		if(i == SBP_NUM_TARGETS){
692103285Sikob			if (alive) {
693103285Sikob				/* new target */
694103285Sikob				target = sbp_alloc_target(sbp, fwdev);
695103285Sikob				if (target == NULL)
696103285Sikob					continue;
697103285Sikob			} else {
698103285Sikob				continue;
699103285Sikob			}
700103285Sikob		}
701103285Sikob		sbp_probe_target(target, alive);
702103285Sikob	}
703103285Sikob#if 0
704103285Sikob	timeout(sbp_release_queue, (caddr_t)sbp, bus_reset_rest * hz / 1000);
705103285Sikob#endif
706103285Sikob}
707103285Sikob
708103285Sikob#if NEED_RESPONSE
709103285Sikobstatic void
710103285Sikobsbp_loginres_callback(struct fw_xfer *xfer){
711103285SikobSBP_DEBUG(1)
712103285Sikob	struct sbp_dev *sdev;
713103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
714103285Sikob	sbp_show_sdev_info(sdev, 2);
715103285Sikob	printf("sbp_loginres_callback\n");
716103285SikobEND_DEBUG
717103285Sikob	fw_xfer_free(xfer);
718103285Sikob	return;
719103285Sikob}
720103285Sikob#endif
721103285Sikob
722103285Sikobstatic void
723103285Sikobsbp_login_callback(struct fw_xfer *xfer)
724103285Sikob{
725103285SikobSBP_DEBUG(1)
726103285Sikob	struct sbp_dev *sdev;
727103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
728103285Sikob	sbp_show_sdev_info(sdev, 2);
729103285Sikob	printf("sbp_login_callback\n");
730103285SikobEND_DEBUG
731103285Sikob	fw_xfer_free(xfer);
732103285Sikob	return;
733103285Sikob}
734103285Sikob
735103285Sikobstatic void
736103285Sikobsbp_cmd_callback(struct fw_xfer *xfer)
737103285Sikob{
738103285SikobSBP_DEBUG(2)
739103285Sikob	struct sbp_dev *sdev;
740103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
741103285Sikob	sbp_show_sdev_info(sdev, 2);
742103285Sikob	printf("sbp_cmd_callback\n");
743103285SikobEND_DEBUG
744103285Sikob	fw_xfer_free(xfer);
745103285Sikob	return;
746103285Sikob}
747103285Sikob
748103285Sikobstatic void
749103285Sikobsbp_cam_callback(struct cam_periph *periph, union ccb *ccb)
750103285Sikob{
751103285Sikob	struct sbp_dev *sdev;
752103285Sikob	sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
753110269SsimokawaSBP_DEBUG(0)
754103285Sikob	sbp_show_sdev_info(sdev, 2);
755103285Sikob	printf("sbp_cam_callback\n");
756103285SikobEND_DEBUG
757103285Sikob	sdev->status = SBP_DEV_ATTACHED;
758103285Sikob	free(ccb, M_SBP);
759103285Sikob}
760103285Sikob
761103285Sikobstatic void
762103285Sikobsbp_cam_scan_lun(struct sbp_dev *sdev)
763103285Sikob{
764110798Ssimokawa	union ccb *ccb;
765103285Sikob
766110798Ssimokawa	ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO);
767110798Ssimokawa	if (ccb == NULL) {
768110798Ssimokawa		printf("sbp_cam_scan_lun: malloc failed\n");
769110798Ssimokawa		return;
770110798Ssimokawa	}
771110798Ssimokawa
772103285SikobSBP_DEBUG(0)
773103285Sikob	sbp_show_sdev_info(sdev, 2);
774103285Sikob	printf("sbp_cam_scan_lun\n");
775103285SikobEND_DEBUG
776103285Sikob	xpt_setup_ccb(&ccb->ccb_h, sdev->path, 5/*priority (low)*/);
777103285Sikob	ccb->ccb_h.func_code = XPT_SCAN_LUN;
778103285Sikob	ccb->ccb_h.cbfcnp = sbp_cam_callback;
779103285Sikob	ccb->crcn.flags = CAM_FLAG_NONE;
780103285Sikob	ccb->ccb_h.ccb_sdev_ptr = sdev;
781103285Sikob
782103285Sikob	/* The scan is in progress now. */
783111040Ssimokawa	sdev->status = SBP_DEV_PROBE;
784111040Ssimokawa	xpt_action(ccb);
785103285Sikob}
786103285Sikob
787103285Sikob
788103285Sikobstatic void
789103285Sikobsbp_ping_unit_callback(struct cam_periph *periph, union ccb *ccb)
790103285Sikob{
791103285Sikob	struct sbp_dev *sdev;
792103285Sikob	sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
793110269SsimokawaSBP_DEBUG(0)
794103285Sikob	sbp_show_sdev_info(sdev, 2);
795103285Sikob	printf("sbp_ping_unit_callback\n");
796103285SikobEND_DEBUG
797103285Sikob	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
798103285Sikob		if (--ccb->ccb_h.retry_count == 0) {
799103285Sikob			sbp_show_sdev_info(sdev, 2);
800110336Ssimokawa			printf("sbp_ping_unit_callback: "
801110336Ssimokawa				"retry count exceeded\n");
802103285Sikob			sdev->status = SBP_DEV_RETRY;
803103285Sikob			free(ccb, M_SBP);
804103285Sikob		} else {
805103285Sikob			/* requeue */
806103285Sikob			xpt_action(ccb);
807111040Ssimokawa			xpt_release_devq(sdev->path, MAX_FREEZE, TRUE);
808103285Sikob		}
809103285Sikob	} else {
810103285Sikob		free(ccb->csio.data_ptr, M_SBP);
811103285Sikob		free(ccb, M_SBP);
812103285Sikob		sdev->status = SBP_DEV_ATTACHED;
813111040Ssimokawa		xpt_release_devq(sdev->path, MAX_FREEZE, TRUE);
814103285Sikob	}
815103285Sikob}
816103285Sikob
817103285Sikob/*
818103285Sikob * XXX Some devices need to execute inquiry or read_capacity
819103285Sikob * after bus_rest during busy transfer.
820103285Sikob * Otherwise they return incorrect result for READ(and WRITE?)
821103285Sikob * command without any SBP-II/SCSI error.
822103285Sikob *
823103285Sikob * e.g. Maxtor 3000XT, Yano A-dish.
824103285Sikob */
825103285Sikobstatic void
826103285Sikobsbp_ping_unit(struct sbp_dev *sdev)
827103285Sikob{
828103285Sikob	union ccb *ccb;
829103285Sikob	struct scsi_inquiry_data *inq_buf;
830103285Sikob
831110798Ssimokawa
832110798Ssimokawa	ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO);
833110798Ssimokawa	if (ccb == NULL) {
834110798Ssimokawa		printf("sbp_ping_unit: malloc failed\n");
835110798Ssimokawa		return;
836110798Ssimokawa	}
837110798Ssimokawa
838103285Sikob	inq_buf = (struct scsi_inquiry_data *)
839110798Ssimokawa			malloc(sizeof(*inq_buf), M_SBP, M_NOWAIT);
840110798Ssimokawa	if (inq_buf == NULL) {
841110798Ssimokawa		free(ccb, M_SBP);
842110798Ssimokawa		printf("sbp_ping_unit: malloc failed\n");
843110798Ssimokawa		return;
844110798Ssimokawa	}
845103285Sikob
846110269SsimokawaSBP_DEBUG(0)
847103285Sikob	sbp_show_sdev_info(sdev, 2);
848103285Sikob	printf("sbp_ping_unit\n");
849103285SikobEND_DEBUG
850103285Sikob
851103285Sikob	/*
852103285Sikob	 * We need to execute this command before any other queued command.
853103285Sikob	 * Make priority 0 and freeze queue after execution for retry.
854103285Sikob	 * cam's scan_lun command doesn't provide this feature.
855103285Sikob	 */
856103285Sikob	xpt_setup_ccb(&ccb->ccb_h, sdev->path, 0/*priority (high)*/);
857103285Sikob	scsi_inquiry(
858103285Sikob		&ccb->csio,
859103285Sikob		/*retries*/ 5,
860103285Sikob		sbp_ping_unit_callback,
861103285Sikob		MSG_SIMPLE_Q_TAG,
862103285Sikob		(u_int8_t *)inq_buf,
863103285Sikob		SHORT_INQUIRY_LENGTH,
864103285Sikob		/*evpd*/FALSE,
865103285Sikob		/*page_code*/0,
866103285Sikob		SSD_MIN_SIZE,
867103285Sikob		/*timeout*/60000
868103285Sikob	);
869103285Sikob	ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
870103285Sikob	xpt_action(ccb);
871111040Ssimokawa	sdev->status = SBP_DEV_PROBE;
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);
885111199Ssimokawa
886103285Sikob	if (sdev->path == NULL)
887103285Sikob		xpt_create_path(&sdev->path, xpt_periph,
888103285Sikob			cam_sim_path(sdev->target->sbp->sim),
889103285Sikob			sdev->target->target_id, sdev->lun_id);
890103285Sikob
891111199Ssimokawa	/*
892111199Ssimokawa	 * Let CAM scan the bus if we are in the boot process.
893111199Ssimokawa	 * XXX xpt_scan_bus cannot detect LUN larger than 0
894111199Ssimokawa	 * if LUN 0 doesn't exists.
895111199Ssimokawa	 */
896111199Ssimokawa	if (sbp_cold > 0) {
897111199Ssimokawa		sdev->status = SBP_DEV_PROBE;
898111199Ssimokawa		return;
899111199Ssimokawa	}
900111199Ssimokawa
901111040Ssimokawa	if (sdev->status == SBP_DEV_RETRY)
902110336Ssimokawa		sbp_ping_unit(sdev);
903111040Ssimokawa	else
904103285Sikob		sbp_cam_scan_lun(sdev);
905111040Ssimokawa	xpt_release_devq(sdev->path, MAX_FREEZE, TRUE);
906103285Sikob	return;
907103285Sikob}
908103285Sikob
909103285Sikobstatic void
910103285Sikobsbp_agent_reset_callback(struct fw_xfer *xfer)
911103285Sikob{
912103285Sikob	struct sbp_dev *sdev;
913103285Sikob
914103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
915103285SikobSBP_DEBUG(1)
916103285Sikob	sbp_show_sdev_info(sdev, 2);
917103285Sikob	printf("sbp_cmd_callback\n");
918103285SikobEND_DEBUG
919103285Sikob	fw_xfer_free(xfer);
920103285Sikob	sbp_abort_all_ocbs(sdev, CAM_REQUEUE_REQ);
921103285Sikob	if (sdev->path)
922111040Ssimokawa		xpt_release_devq(sdev->path, MAX_FREEZE, TRUE);
923103285Sikob}
924103285Sikob
925103285Sikobstatic void
926110336Ssimokawasbp_agent_reset(struct sbp_dev *sdev)
927103285Sikob{
928103285Sikob	struct fw_xfer *xfer;
929103285Sikob	struct fw_pkt *fp;
930103285Sikob
931103285SikobSBP_DEBUG(0)
932103285Sikob	sbp_show_sdev_info(sdev, 2);
933103285Sikob	printf("sbp_agent_reset\n");
934103285SikobEND_DEBUG
935103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
936103285Sikob	if (xfer == NULL)
937103285Sikob		return;
938110336Ssimokawa	if (sdev->status == SBP_DEV_ATTACHED)
939110336Ssimokawa		xfer->act.hand = sbp_agent_reset_callback;
940110336Ssimokawa	else
941103285Sikob		xfer->act.hand = sbp_do_attach;
942103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
943103285Sikob	fp->mode.wreqq.data = htonl(0xf);
944103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
945103285Sikob}
946103285Sikob
947103285Sikobstatic void
948103285Sikobsbp_busy_timeout_callback(struct fw_xfer *xfer)
949103285Sikob{
950103285Sikob	struct sbp_dev *sdev;
951103285Sikob
952103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
953103285SikobSBP_DEBUG(1)
954103285Sikob	sbp_show_sdev_info(sdev, 2);
955110336Ssimokawa	printf("sbp_busy_timeout_callback\n");
956103285SikobEND_DEBUG
957103285Sikob	fw_xfer_free(xfer);
958110336Ssimokawa	sbp_agent_reset(sdev);
959103285Sikob}
960103285Sikob
961103285Sikobstatic void
962103285Sikobsbp_busy_timeout(struct sbp_dev *sdev)
963103285Sikob{
964103285Sikob	struct fw_pkt *fp;
965103285Sikob	struct fw_xfer *xfer;
966103285SikobSBP_DEBUG(0)
967103285Sikob	sbp_show_sdev_info(sdev, 2);
968103285Sikob	printf("sbp_busy_timeout\n");
969103285SikobEND_DEBUG
970103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
971103285Sikob
972103285Sikob	xfer->act.hand = sbp_busy_timeout_callback;
973103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
974103285Sikob	fp->mode.wreqq.dest_hi = htons(0xffff);
975110129Ssimokawa	fp->mode.wreqq.dest_lo = htonl(0xf0000000 | BUSY_TIMEOUT);
976110129Ssimokawa	fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
977103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
978103285Sikob}
979103285Sikob
980103285Sikob#if 0
981103285Sikobstatic void
982103285Sikobsbp_reset_start(struct sbp_dev *sdev)
983103285Sikob{
984103285Sikob	struct fw_xfer *xfer;
985103285Sikob	struct fw_pkt *fp;
986103285Sikob
987103285SikobSBP_DEBUG(0)
988103285Sikob	sbp_show_sdev_info(sdev, 2);
989103285Sikob	printf("sbp_reset_start\n");
990103285SikobEND_DEBUG
991103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
992103285Sikob
993103285Sikob	xfer->act.hand = sbp_busy_timeout;
994103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
995103285Sikob	fp->mode.wreqq.dest_hi = htons(0xffff);
996103285Sikob	fp->mode.wreqq.dest_lo = htonl(0xf0000000 | RESET_START);
997103285Sikob	fp->mode.wreqq.data = htonl(0xf);
998103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
999103285Sikob}
1000103285Sikob#endif
1001103285Sikob
1002103285Sikobstatic void
1003103285Sikobsbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1004103285Sikob{
1005103285Sikob	struct fw_xfer *xfer;
1006103285Sikob	struct fw_pkt *fp;
1007103285SikobSBP_DEBUG(2)
1008103285Sikob	sbp_show_sdev_info(sdev, 2);
1009103285Sikob	printf("sbp_orb_pointer\n");
1010103285SikobEND_DEBUG
1011103285Sikob
1012103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0x08);
1013103285Sikob	if (xfer == NULL)
1014103285Sikob		return;
1015103285Sikob	xfer->act.hand = sbp_cmd_callback;
1016103285Sikob
1017103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1018103285Sikob	fp->mode.wreqb.len = htons(8);
1019103285Sikob	fp->mode.wreqb.extcode = 0;
1020103285Sikob	fp->mode.wreqb.payload[0] =
1021103285Sikob		htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1022103285Sikob	fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0]));
1023103285Sikob
1024103285Sikob	if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1025103285Sikob			fw_xfer_free(xfer);
1026103285Sikob			ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1027103285Sikob			xpt_done(ocb->ccb);
1028103285Sikob	}
1029103285Sikob}
1030103285Sikob
1031103285Sikobstatic void
1032103285Sikobsbp_doorbell(struct sbp_dev *sdev)
1033103285Sikob{
1034103285Sikob	struct fw_xfer *xfer;
1035103285Sikob	struct fw_pkt *fp;
1036103285SikobSBP_DEBUG(1)
1037103285Sikob	sbp_show_sdev_info(sdev, 2);
1038103285Sikob	printf("sbp_doorbell\n");
1039103285SikobEND_DEBUG
1040103285Sikob
1041103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x10);
1042103285Sikob	if (xfer == NULL)
1043103285Sikob		return;
1044103285Sikob	xfer->act.hand = sbp_cmd_callback;
1045103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1046103285Sikob	fp->mode.wreqq.data = htonl(0xf);
1047103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1048103285Sikob}
1049103285Sikob
1050103285Sikobstatic struct fw_xfer *
1051103285Sikobsbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1052103285Sikob{
1053103285Sikob	struct fw_xfer *xfer;
1054103285Sikob	struct fw_pkt *fp;
1055103285Sikob
1056110269Ssimokawa	xfer = fw_xfer_alloc(M_SBP);
1057103285Sikob	if(xfer == NULL){
1058103285Sikob		return NULL;
1059103285Sikob	}
1060103285Sikob	if (tcode == FWTCODE_WREQQ)
1061103285Sikob		xfer->send.len = 16;
1062103285Sikob	else
1063103285Sikob		xfer->send.len = 24;
1064103285Sikob
1065110195Ssimokawa	xfer->send.buf = malloc(xfer->send.len, M_FW, M_NOWAIT);
1066103285Sikob	if(xfer->send.buf == NULL){
1067110195Ssimokawa		fw_xfer_free(xfer);
1068103285Sikob		return NULL;
1069103285Sikob	}
1070103285Sikob
1071103285Sikob	xfer->send.off = 0;
1072103285Sikob	xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1073103285Sikob	xfer->sc = (caddr_t)sdev;
1074103285Sikob	xfer->fc = sdev->target->sbp->fd.fc;
1075103285Sikob	xfer->retry_req = fw_asybusy;
1076103285Sikob
1077103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1078103285Sikob	fp->mode.wreqq.dest_hi = htons(sdev->login.cmd_hi);
1079103285Sikob	fp->mode.wreqq.dest_lo = htonl(sdev->login.cmd_lo + offset);
1080103285Sikob	fp->mode.wreqq.tlrt = 0;
1081103285Sikob	fp->mode.wreqq.tcode = tcode;
1082103285Sikob	fp->mode.wreqq.pri = 0;
1083103285Sikob	xfer->dst = FWLOCALBUS | sdev->target->fwdev->dst;
1084103285Sikob	fp->mode.wreqq.dst = htons(xfer->dst);
1085103285Sikob
1086103285Sikob	return xfer;
1087103285Sikob
1088103285Sikob}
1089103285Sikob
1090103285Sikobstatic void
1091110336Ssimokawasbp_mgm_orb(struct sbp_dev *sdev, int func, u_int16_t orb_hi, u_int32_t orb_lo)
1092103285Sikob{
1093103285Sikob	struct fw_xfer *xfer;
1094103285Sikob	struct fw_pkt *fp;
1095103285Sikob	struct sbp_ocb *ocb;
1096103285Sikob	int s, nid;
1097103285Sikob
1098103285Sikob	if ((ocb = sbp_get_ocb(sdev->target->sbp)) == NULL) {
1099103285Sikob		s = splfw();
1100103285Sikob		sdev->target->sbp->flags |= SBP_RESOURCE_SHORTAGE;
1101103285Sikob		splx(s);
1102103285Sikob		return;
1103103285Sikob	}
1104103285Sikob	ocb->flags = OCB_ACT_MGM;
1105103285Sikob	ocb->sdev = sdev;
1106103285Sikob	ocb->ccb = NULL;
1107103285Sikob
1108103285Sikob	nid = sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS;
1109103285Sikob	bzero((void *)(uintptr_t)(volatile void *)ocb->orb, sizeof(ocb->orb));
1110103285Sikob	ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1111103285Sikob	ocb->orb[7] = htonl(SBP_DEV2ADDR(
1112103285Sikob		device_get_unit(sdev->target->sbp->fd.dev),
1113103285Sikob		sdev->target->target_id,
1114103285Sikob		sdev->lun_id));
1115103285Sikob
1116107653SsimokawaSBP_DEBUG(0)
1117103285Sikob	sbp_show_sdev_info(sdev, 2);
1118103285Sikob	printf("%s\n", orb_fun_name[(func>>16)&0xf]);
1119107653SsimokawaEND_DEBUG
1120103285Sikob	switch (func) {
1121103285Sikob	case ORB_FUN_LGI:
1122103285Sikob		ocb->orb[2] = htonl(nid << 16);
1123103285Sikob		ocb->orb[3] = htonl(vtophys(&sdev->login));
1124103285Sikob		ocb->orb[4] = htonl(ORB_NOTIFY | ORB_EXV | sdev->lun_id);
1125103285Sikob		ocb->orb[5] = htonl(sizeof(struct sbp_login_res));
1126103285Sikob		break;
1127110336Ssimokawa	case ORB_FUN_ATA:
1128110336Ssimokawa		ocb->orb[0] = htonl((0 << 16) | orb_hi);
1129110336Ssimokawa		ocb->orb[1] = htonl(orb_lo);
1130110336Ssimokawa		/* fall through */
1131103285Sikob	case ORB_FUN_RCN:
1132103285Sikob	case ORB_FUN_LGO:
1133103285Sikob	case ORB_FUN_LUR:
1134103285Sikob	case ORB_FUN_RST:
1135103285Sikob	case ORB_FUN_ATS:
1136103285Sikob		ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login.id);
1137103285Sikob		break;
1138103285Sikob	}
1139103285Sikob
1140103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1141103285Sikob	if(xfer == NULL){
1142103285Sikob		return;
1143103285Sikob	}
1144103285Sikob	xfer->act.hand = sbp_login_callback;
1145103285Sikob
1146103285Sikob	fp = (struct fw_pkt *)xfer->send.buf;
1147103285Sikob	fp->mode.wreqb.dest_hi = htons(sdev->target->mgm_hi);
1148103285Sikob	fp->mode.wreqb.dest_lo = htonl(sdev->target->mgm_lo);
1149103285Sikob	fp->mode.wreqb.len = htons(8);
1150103285Sikob	fp->mode.wreqb.extcode = 0;
1151108503Ssimokawa	fp->mode.wreqb.payload[0] = htonl(nid << 16);
1152103285Sikob	fp->mode.wreqb.payload[1] = htonl(vtophys(&ocb->orb[0]));
1153103285Sikob	sbp_enqueue_ocb(sdev, ocb);
1154103285Sikob
1155103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1156103285Sikob}
1157103285Sikob
1158103285Sikobstatic void
1159105792Ssimokawasbp_print_scsi_cmd(struct sbp_ocb *ocb)
1160103285Sikob{
1161103285Sikob	struct ccb_scsiio *csio;
1162103285Sikob
1163103285Sikob	csio = &ocb->ccb->csio;
1164103285Sikob	printf("%s:%d:%d XPT_SCSI_IO: "
1165103285Sikob		"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1166103285Sikob		", flags: 0x%02x, "
1167103285Sikob		"%db cmd/%db data/%db sense\n",
1168103285Sikob		device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1169103285Sikob		ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun,
1170103285Sikob		csio->cdb_io.cdb_bytes[0],
1171103285Sikob		csio->cdb_io.cdb_bytes[1],
1172103285Sikob		csio->cdb_io.cdb_bytes[2],
1173103285Sikob		csio->cdb_io.cdb_bytes[3],
1174103285Sikob		csio->cdb_io.cdb_bytes[4],
1175103285Sikob		csio->cdb_io.cdb_bytes[5],
1176103285Sikob		csio->cdb_io.cdb_bytes[6],
1177103285Sikob		csio->cdb_io.cdb_bytes[7],
1178103285Sikob		csio->cdb_io.cdb_bytes[8],
1179103285Sikob		csio->cdb_io.cdb_bytes[9],
1180103285Sikob		ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1181103285Sikob		csio->cdb_len, csio->dxfer_len,
1182103285Sikob		csio->sense_len);
1183105792Ssimokawa}
1184105792Ssimokawa
1185105792Ssimokawastatic void
1186105792Ssimokawasbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1187105792Ssimokawa{
1188105792Ssimokawa	struct sbp_cmd_status *sbp_cmd_status;
1189105792Ssimokawa	struct scsi_sense_data *sense;
1190105792Ssimokawa
1191105792Ssimokawa	sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1192105792Ssimokawa	sense = &ocb->ccb->csio.sense_data;
1193105792Ssimokawa
1194105792SsimokawaSBP_DEBUG(0)
1195105792Ssimokawa	sbp_print_scsi_cmd(ocb);
1196103285Sikob	/* XXX need decode status */
1197103285Sikob	sbp_show_sdev_info(ocb->sdev, 2);
1198103285Sikob	printf("SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d",
1199103285Sikob		sbp_cmd_status->status,
1200103285Sikob		sbp_cmd_status->sfmt,
1201103285Sikob		sbp_cmd_status->valid,
1202103285Sikob		sbp_cmd_status->s_key,
1203103285Sikob		sbp_cmd_status->s_code,
1204103285Sikob		sbp_cmd_status->s_qlfr,
1205103285Sikob		sbp_status->len
1206103285Sikob	);
1207103285Sikob#if 0	 /* XXX */
1208103285Sikob	if (sbp_cmd_status->status == SCSI_STATUS_CHECK_COND) {
1209103285Sikob		printf(" %s\n", scsi_sense_key_text[sbp_cmd_status->s_key]);
1210103285Sikob			scsi_sense_desc(
1211103285Sikob				sbp_cmd_status->s_code,
1212103285Sikob				sbp_cmd_status->s_qlfr,
1213103285Sikob				ocb->ccb->ccb_h.path->device->inq_data
1214103285Sikob			)
1215103285Sikob	} else {
1216103285Sikob		printf("\n");
1217103285Sikob	}
1218103285Sikob#else
1219103285Sikob	printf("\n");
1220103285Sikob#endif
1221103285SikobEND_DEBUG
1222103285Sikob
1223110071Ssimokawa	switch (sbp_cmd_status->status) {
1224110071Ssimokawa	case SCSI_STATUS_CHECK_COND:
1225110071Ssimokawa	case SCSI_STATUS_BUSY:
1226110071Ssimokawa	case SCSI_STATUS_CMD_TERMINATED:
1227103285Sikob		if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1228103285Sikob			sense->error_code = SSD_CURRENT_ERROR;
1229103285Sikob		}else{
1230103285Sikob			sense->error_code = SSD_DEFERRED_ERROR;
1231103285Sikob		}
1232103285Sikob		if(sbp_cmd_status->valid)
1233103285Sikob			sense->error_code |= SSD_ERRCODE_VALID;
1234103285Sikob		sense->flags = sbp_cmd_status->s_key;
1235103285Sikob		if(sbp_cmd_status->mark)
1236103285Sikob			sense->flags |= SSD_FILEMARK;
1237103285Sikob		if(sbp_cmd_status->eom)
1238103285Sikob			sense->flags |= SSD_EOM;
1239103285Sikob		if(sbp_cmd_status->ill_len)
1240103285Sikob			sense->flags |= SSD_ILI;
1241103285Sikob		sense->info[0] = ntohl(sbp_cmd_status->info) & 0xff;
1242103285Sikob		sense->info[1] =(ntohl(sbp_cmd_status->info) >> 8) & 0xff;
1243103285Sikob		sense->info[2] =(ntohl(sbp_cmd_status->info) >> 16) & 0xff;
1244103285Sikob		sense->info[3] =(ntohl(sbp_cmd_status->info) >> 24) & 0xff;
1245103285Sikob		if (sbp_status->len <= 1)
1246103285Sikob			/* XXX not scsi status. shouldn't be happened */
1247103285Sikob			sense->extra_len = 0;
1248103285Sikob		else if (sbp_status->len <= 4)
1249103285Sikob			/* add_sense_code(_qual), info, cmd_spec_info */
1250103285Sikob			sense->extra_len = 6;
1251103285Sikob		else
1252103285Sikob			/* fru, sense_key_spec */
1253103285Sikob			sense->extra_len = 10;
1254103285Sikob		sense->cmd_spec_info[0] = ntohl(sbp_cmd_status->cdb) & 0xff;
1255103285Sikob		sense->cmd_spec_info[1] = (ntohl(sbp_cmd_status->cdb) >> 8) & 0xff;
1256103285Sikob		sense->cmd_spec_info[2] = (ntohl(sbp_cmd_status->cdb) >> 16) & 0xff;
1257103285Sikob		sense->cmd_spec_info[3] = (ntohl(sbp_cmd_status->cdb) >> 24) & 0xff;
1258103285Sikob		sense->add_sense_code = sbp_cmd_status->s_code;
1259103285Sikob		sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1260103285Sikob		sense->fru = sbp_cmd_status->fru;
1261103285Sikob		sense->sense_key_spec[0] = ntohl(sbp_cmd_status->s_keydep) & 0xff;
1262103285Sikob		sense->sense_key_spec[1] = (ntohl(sbp_cmd_status->s_keydep) >>8) & 0xff;
1263103285Sikob		sense->sense_key_spec[2] = (ntohl(sbp_cmd_status->s_keydep) >>16) & 0xff;
1264103285Sikob
1265103285Sikob		ocb->ccb->csio.scsi_status = sbp_cmd_status->status;;
1266103285Sikob		ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1267103285Sikob							| CAM_AUTOSNS_VALID;
1268103285Sikob/*
1269103285Sikob{
1270103285Sikob		u_int8_t j, *tmp;
1271103285Sikob		tmp = sense;
1272103285Sikob		for( j = 0 ; j < 32 ; j+=8){
1273103285Sikob			printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1274103285Sikob				tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1275103285Sikob				tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1276103285Sikob		}
1277103285Sikob
1278103285Sikob}
1279103285Sikob*/
1280110071Ssimokawa		break;
1281110071Ssimokawa	default:
1282110071Ssimokawa		sbp_show_sdev_info(ocb->sdev, 2);
1283110071Ssimokawa		printf("sbp_scsi_status: unknown scsi status 0x%x\n",
1284110071Ssimokawa						sbp_cmd_status->status);
1285103285Sikob	}
1286103285Sikob}
1287103285Sikob
1288103285Sikobstatic void
1289103285Sikobsbp_fix_inq_data(struct sbp_ocb *ocb)
1290103285Sikob{
1291103285Sikob	union ccb *ccb;
1292103285Sikob	struct sbp_dev *sdev;
1293103285Sikob	struct scsi_inquiry_data *inq;
1294103285Sikob
1295103285Sikob	ccb = ocb->ccb;
1296103285Sikob	sdev = ocb->sdev;
1297103285Sikob
1298103285Sikob	if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1299103285Sikob		return;
1300103285SikobSBP_DEBUG(1)
1301103285Sikob	sbp_show_sdev_info(sdev, 2);
1302103285Sikob	printf("sbp_fix_inq_data\n");
1303103285SikobEND_DEBUG
1304103285Sikob	inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1305103285Sikob	switch (SID_TYPE(inq)) {
1306103285Sikob	case T_DIRECT:
1307103285Sikob		/*
1308103285Sikob		 * XXX Convert Direct Access device to RBC.
1309108281Ssimokawa		 * I've never seen FireWire DA devices which support READ_6.
1310103285Sikob		 */
1311103285Sikob#if 1
1312103285Sikob		if (SID_TYPE(inq) == T_DIRECT)
1313103285Sikob			inq->device |= T_RBC; /*  T_DIRECT == 0 */
1314103285Sikob#endif
1315103285Sikob		/* fall through */
1316103285Sikob	case T_RBC:
1317103285Sikob		/* disable tag queuing */
1318103285Sikob		inq->flags &= ~SID_CmdQue;
1319103285Sikob		/*
1320103285Sikob		 * Override vendor/product/revision information.
1321103285Sikob		 * Some devices sometimes return strange strings.
1322103285Sikob		 */
1323103285Sikob		bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1324103285Sikob		bcopy(sdev->product, inq->product, sizeof(inq->product));
1325103285Sikob		bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1326103285Sikob		break;
1327103285Sikob	}
1328103285Sikob}
1329103285Sikob
1330103285Sikobstatic void
1331103285Sikobsbp_recv1(struct fw_xfer *xfer){
1332103285Sikob	struct fw_pkt *rfp;
1333103285Sikob#if NEED_RESPONSE
1334103285Sikob	struct fw_pkt *sfp;
1335103285Sikob#endif
1336103285Sikob	struct sbp_softc *sbp;
1337103285Sikob	struct sbp_dev *sdev;
1338103285Sikob	struct sbp_ocb *ocb;
1339103285Sikob	struct sbp_login_res *login_res = NULL;
1340103285Sikob	struct sbp_status *sbp_status;
1341103285Sikob	struct sbp_target *target;
1342110189Ssimokawa	int	orb_fun, status_valid, t, l;
1343103285Sikob	u_int32_t addr;
1344103285Sikob/*
1345103285Sikob	u_int32_t *ld;
1346103285Sikob	ld = xfer->recv.buf;
1347103285Sikobprintf("sbp %x %d %d %08x %08x %08x %08x\n",
1348103285Sikob			xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1349103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1350103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1351103285Sikob*/
1352103285Sikob	if(xfer->resp != 0){
1353103285Sikob		printf("sbp_recv: xfer->resp != 0\n");
1354103285Sikob		fw_xfer_free( xfer);
1355103285Sikob		return;
1356103285Sikob	}
1357103285Sikob	if(xfer->recv.buf == NULL){
1358103285Sikob		printf("sbp_recv: xfer->recv.buf == NULL\n");
1359103285Sikob		fw_xfer_free( xfer);
1360103285Sikob		return;
1361103285Sikob	}
1362103285Sikob	sbp = (struct sbp_softc *)xfer->sc;
1363103285Sikob	rfp = (struct fw_pkt *)xfer->recv.buf;
1364103285Sikob	if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1365103285Sikob		printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1366103285Sikob		fw_xfer_free( xfer);
1367103285Sikob		return;
1368103285Sikob	}
1369103285Sikob	sbp_status = (struct sbp_status *)rfp->mode.wreqb.payload;
1370103285Sikob	addr = ntohl(rfp->mode.wreqb.dest_lo);
1371103285SikobSBP_DEBUG(2)
1372103285Sikob	printf("received address 0x%x\n", addr);
1373103285SikobEND_DEBUG
1374110189Ssimokawa	t = SBP_ADDR2TRG(addr);
1375110189Ssimokawa	if (t >= SBP_NUM_TARGETS) {
1376110189Ssimokawa		device_printf(sbp->fd.dev,
1377110189Ssimokawa			"sbp_recv1: invalid target %d\n", t);
1378110189Ssimokawa		fw_xfer_free(xfer);
1379110189Ssimokawa		return;
1380110189Ssimokawa	}
1381110189Ssimokawa	target = &sbp->targets[t];
1382110189Ssimokawa	l = SBP_ADDR2LUN(addr);
1383110189Ssimokawa	if (l >= target->num_lun) {
1384110189Ssimokawa		device_printf(sbp->fd.dev,
1385110189Ssimokawa			"sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1386110189Ssimokawa		fw_xfer_free(xfer);
1387110189Ssimokawa		return;
1388110189Ssimokawa	}
1389110189Ssimokawa	sdev = &target->luns[l];
1390103285Sikob
1391110189Ssimokawa	ocb = NULL;
1392110189Ssimokawa	switch (sbp_status->src) {
1393110189Ssimokawa	case 0:
1394110189Ssimokawa	case 1:
1395110189Ssimokawa		ocb = sbp_dequeue_ocb(sdev, ntohl(sbp_status->orb_lo));
1396110189Ssimokawa		if (ocb == NULL) {
1397110189Ssimokawa			sbp_show_sdev_info(sdev, 2);
1398110189Ssimokawa			printf("No ocb on the queue\n");
1399110189Ssimokawa		}
1400110189Ssimokawa		break;
1401110189Ssimokawa	case 2:
1402110189Ssimokawa		/* unsolicit */
1403110189Ssimokawa		sbp_show_sdev_info(sdev, 2);
1404110189Ssimokawa		printf("unsolicit status received\n");
1405110189Ssimokawa		break;
1406110189Ssimokawa	default:
1407110189Ssimokawa		sbp_show_sdev_info(sdev, 2);
1408110189Ssimokawa		printf("unknown sbp_status->src\n");
1409110189Ssimokawa	}
1410110189Ssimokawa
1411110189Ssimokawa	status_valid = (sbp_status->src < 2
1412110189Ssimokawa			&& sbp_status->resp == ORB_RES_CMPL
1413103285Sikob			&& sbp_status->dead == 0
1414103285Sikob			&& sbp_status->status == 0);
1415103285Sikob
1416103285Sikob	if (!status_valid || debug > 1){
1417103285Sikob		int status;
1418110129SsimokawaSBP_DEBUG(0)
1419103285Sikob		sbp_show_sdev_info(sdev, 2);
1420103285Sikob		printf("ORB status src:%x resp:%x dead:%x"
1421108712Ssimokawa#if __FreeBSD_version >= 500000
1422103285Sikob				" len:%x stat:%x orb:%x%08x\n",
1423108712Ssimokawa#else
1424108712Ssimokawa				" len:%x stat:%x orb:%x%08lx\n",
1425108712Ssimokawa#endif
1426103285Sikob			sbp_status->src, sbp_status->resp, sbp_status->dead,
1427103285Sikob			sbp_status->len, sbp_status->status,
1428108280Ssimokawa			ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1429110129SsimokawaEND_DEBUG
1430103285Sikob		sbp_show_sdev_info(sdev, 2);
1431103285Sikob		status = sbp_status->status;
1432103285Sikob		switch(sbp_status->resp) {
1433103285Sikob		case 0:
1434103285Sikob			if (status > MAX_ORB_STATUS0)
1435103285Sikob				printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1436110189Ssimokawa			else
1437110129Ssimokawa				printf("%s\n", orb_status0[status]);
1438103285Sikob			break;
1439103285Sikob		case 1:
1440110336Ssimokawa			printf("Obj: %s, Error: %s\n",
1441103285Sikob				orb_status1_object[(status>>6) & 3],
1442103285Sikob				orb_status1_serial_bus_error[status & 0xf]);
1443103285Sikob			break;
1444110129Ssimokawa		case 2:
1445110129Ssimokawa			printf("Illegal request\n");
1446110129Ssimokawa			break;
1447110129Ssimokawa		case 3:
1448110129Ssimokawa			printf("Vendor dependent\n");
1449110129Ssimokawa			break;
1450103285Sikob		default:
1451110129Ssimokawa			printf("unknown respose code %d\n", sbp_status->resp);
1452103285Sikob		}
1453103285Sikob	}
1454103285Sikob
1455103285Sikob	/* we have to reset the fetch agent if it's dead */
1456103285Sikob	if (sbp_status->dead) {
1457103285Sikob		if (sdev->path)
1458103285Sikob			xpt_freeze_devq(sdev->path, 1);
1459110336Ssimokawa		sbp_agent_reset(sdev);
1460103285Sikob	}
1461103285Sikob
1462103285Sikob	if (ocb == NULL) {
1463110189Ssimokawa		fw_xfer_free(xfer);
1464103285Sikob		return;
1465103285Sikob	}
1466103285Sikob
1467110336Ssimokawa	sdev->flags &= ~SBP_DEV_TIMEOUT;
1468110336Ssimokawa
1469103285Sikob	switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1470103285Sikob	case ORB_FMT_NOP:
1471103285Sikob		break;
1472103285Sikob	case ORB_FMT_VED:
1473103285Sikob		break;
1474103285Sikob	case ORB_FMT_STD:
1475103285Sikob		switch(ocb->flags & OCB_ACT_MASK){
1476103285Sikob		case OCB_ACT_MGM:
1477103285Sikob			orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1478103285Sikob			switch(orb_fun) {
1479103285Sikob			case ORB_FUN_LGI:
1480103285Sikob				login_res = &sdev->login;
1481103285Sikob				login_res->len = ntohs(login_res->len);
1482103285Sikob				login_res->id = ntohs(login_res->id);
1483103285Sikob				login_res->cmd_hi = ntohs(login_res->cmd_hi);
1484103285Sikob				login_res->cmd_lo = ntohl(login_res->cmd_lo);
1485103285Sikob				if (status_valid) {
1486103285SikobSBP_DEBUG(0)
1487103285Sikobsbp_show_sdev_info(sdev, 2);
1488103285Sikobprintf("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));
1489103285SikobEND_DEBUG
1490103285Sikob#if 1
1491103285Sikob					sbp_busy_timeout(sdev);
1492103285Sikob#else
1493110336Ssimokawa					sbp_mgm_orb(sdev, ORB_FUN_ATS, 0, 0);
1494103285Sikob#endif
1495103285Sikob				} else {
1496110336Ssimokawa					/* forgot logout? */
1497110336Ssimokawa					sbp_show_sdev_info(sdev, 2);
1498103285Sikob					printf("login failed\n");
1499103285Sikob					sdev->status = SBP_DEV_RESET;
1500103285Sikob				}
1501103285Sikob				break;
1502103285Sikob			case ORB_FUN_RCN:
1503103285Sikob				login_res = &sdev->login;
1504103285Sikob				if (status_valid) {
1505103285SikobSBP_DEBUG(0)
1506103285Sikobsbp_show_sdev_info(sdev, 2);
1507103285Sikobprintf("reconnect: len %d, ID %d, cmd %08x%08x\n", login_res->len, login_res->id, login_res->cmd_hi, login_res->cmd_lo);
1508103285SikobEND_DEBUG
1509103285Sikob#if 1
1510103285Sikob					sbp_ping_unit(sdev);
1511111040Ssimokawa					xpt_release_devq(sdev->path,
1512111040Ssimokawa							MAX_FREEZE, TRUE);
1513103285Sikob#else
1514110336Ssimokawa					sdev->status = SBP_DEV_ATTACHED;
1515110336Ssimokawa					sbp_mgm_orb(sdev, ORB_FUN_ATS, 0, 0);
1516103285Sikob#endif
1517103285Sikob				} else {
1518103285Sikob					/* reconnection hold time exceed? */
1519110336SsimokawaSBP_DEBUG(0)
1520110336Ssimokawa					sbp_show_sdev_info(sdev, 2);
1521103285Sikob					printf("reconnect failed\n");
1522110336SsimokawaEND_DEBUG
1523110336Ssimokawa					sbp_mgm_orb(sdev, ORB_FUN_LGI, 0, 0);
1524103285Sikob				}
1525103285Sikob				break;
1526103285Sikob			case ORB_FUN_LGO:
1527103285Sikob				sdev->status = SBP_DEV_RESET;
1528103285Sikob				break;
1529110336Ssimokawa			case ORB_FUN_RST:
1530110336Ssimokawa				sbp_busy_timeout(sdev);
1531110336Ssimokawa				break;
1532103285Sikob			case ORB_FUN_LUR:
1533103285Sikob			case ORB_FUN_ATA:
1534103285Sikob			case ORB_FUN_ATS:
1535110336Ssimokawa				sbp_agent_reset(sdev);
1536103285Sikob				break;
1537103285Sikob			default:
1538110336Ssimokawa				sbp_show_sdev_info(sdev, 2);
1539110336Ssimokawa				printf("unknown function %d\n", orb_fun);
1540103285Sikob				break;
1541103285Sikob			}
1542103285Sikob			break;
1543103285Sikob		case OCB_ACT_CMD:
1544103285Sikob			if(ocb->ccb != NULL){
1545103285Sikob				union ccb *ccb;
1546103285Sikob/*
1547103285Sikob				u_int32_t *ld;
1548103285Sikob				ld = ocb->ccb->csio.data_ptr;
1549103285Sikob				if(ld != NULL && ocb->ccb->csio.dxfer_len != 0)
1550103285Sikob					printf("ptr %08x %08x %08x %08x\n", ld[0], ld[1], ld[2], ld[3]);
1551103285Sikob				else
1552103285Sikob					printf("ptr NULL\n");
1553103285Sikobprintf("len %d\n", sbp_status->len);
1554103285Sikob*/
1555103285Sikob				ccb = ocb->ccb;
1556103285Sikob				if(sbp_status->len > 1){
1557103285Sikob					sbp_scsi_status(sbp_status, ocb);
1558103285Sikob				}else{
1559103285Sikob					if(sbp_status->resp != ORB_RES_CMPL){
1560103285Sikob						ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1561103285Sikob					}else{
1562103285Sikob						ccb->ccb_h.status = CAM_REQ_CMP;
1563103285Sikob					}
1564103285Sikob				}
1565103285Sikob				/* fix up inq data */
1566103285Sikob				if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1567103285Sikob					sbp_fix_inq_data(ocb);
1568103285Sikob				xpt_done(ccb);
1569103285Sikob			}
1570103285Sikob			break;
1571103285Sikob		default:
1572103285Sikob			break;
1573103285Sikob		}
1574103285Sikob	}
1575103285Sikob
1576103285Sikob	if (!(ocb->flags & OCB_RESERVED))
1577103285Sikob		sbp_free_ocb(sbp, ocb);
1578103285Sikob
1579103285Sikob/* The received packet is usually small enough to be stored within
1580103285Sikob * the buffer. In that case, the controller return ack_complete and
1581103285Sikob * no respose is necessary.
1582103285Sikob *
1583103285Sikob * XXX fwohci.c and firewire.c should inform event_code such as
1584103285Sikob * ack_complete or ack_pending to upper driver.
1585103285Sikob */
1586103285Sikob#if NEED_RESPONSE
1587103285Sikob	xfer->send.buf = malloc(12, M_SBP, M_NOWAIT | M_ZERO);
1588103285Sikob	xfer->send.len = 12;
1589103285Sikob	xfer->send.off = 0;
1590103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
1591103285Sikob	sfp->mode.wres.dst = rfp->mode.wreqb.src;
1592103285Sikob	xfer->dst = ntohs(sfp->mode.wres.dst);
1593103285Sikob	xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1594103285Sikob	xfer->act.hand = sbp_loginres_callback;
1595103285Sikob	xfer->retry_req = fw_asybusy;
1596103285Sikob
1597103285Sikob	sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1598103285Sikob	sfp->mode.wres.tcode = FWTCODE_WRES;
1599103285Sikob	sfp->mode.wres.rtcode = 0;
1600103285Sikob	sfp->mode.wres.pri = 0;
1601103285Sikob
1602103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1603103285Sikob#else
1604103285Sikob	fw_xfer_free(xfer);
1605103285Sikob#endif
1606103285Sikob
1607103285Sikob	return;
1608103285Sikob
1609103285Sikob}
1610103285Sikob
1611103285Sikobstatic void
1612103285Sikobsbp_recv(struct fw_xfer *xfer)
1613103285Sikob{
1614103285Sikob	int s;
1615103285Sikob
1616103285Sikob	s = splcam();
1617103285Sikob	sbp_recv1(xfer);
1618103285Sikob	splx(s);
1619103285Sikob}
1620103285Sikob/*
1621103285Sikob * sbp_attach()
1622103285Sikob */
1623103285Sikobstatic int
1624103285Sikobsbp_attach(device_t dev)
1625103285Sikob{
1626103285Sikob	struct sbp_softc *sbp;
1627103285Sikob	struct cam_devq *devq;
1628103285Sikob	struct fw_xfer *xfer;
1629103285Sikob	int i, s, error;
1630103285Sikob
1631103285SikobSBP_DEBUG(0)
1632111199Ssimokawa	printf("sbp_attach (cold=%d)\n", cold);
1633103285SikobEND_DEBUG
1634103285Sikob
1635111199Ssimokawa	if (cold)
1636111199Ssimokawa		sbp_cold ++;
1637103285Sikob	sbp = ((struct sbp_softc *)device_get_softc(dev));
1638103285Sikob	bzero(sbp, sizeof(struct sbp_softc));
1639103285Sikob	sbp->fd.dev = dev;
1640103285Sikob	sbp->fd.fc = device_get_ivars(dev);
1641103285Sikob	error = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
1642103285Sikob				/*boundary*/0,
1643103285Sikob				/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
1644103285Sikob				/*highaddr*/BUS_SPACE_MAXADDR,
1645103285Sikob				/*filter*/NULL, /*filterarg*/NULL,
1646103285Sikob				/*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
1647103285Sikob				/*maxsegsz*/0x8000,
1648103285Sikob				/*flags*/BUS_DMA_ALLOCNOW,
1649103285Sikob				&sbp->dmat);
1650103285Sikob	if (error != 0) {
1651103285Sikob		printf("sbp_attach: Could not allocate DMA tag "
1652103285Sikob			"- error %d\n", error);
1653103285Sikob			return (ENOMEM);
1654103285Sikob	}
1655103285Sikob
1656103285Sikob	devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
1657103285Sikob	if (devq == NULL)
1658103285Sikob		return (ENXIO);
1659103285Sikob
1660103285Sikob	for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
1661103285Sikob		sbp->targets[i].fwdev = NULL;
1662103285Sikob		sbp->targets[i].luns = NULL;
1663103285Sikob	}
1664103285Sikob
1665103285Sikob	sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
1666103285Sikob				 device_get_unit(dev),
1667103285Sikob				 /*untagged*/ SBP_QUEUE_LEN,
1668103285Sikob				 /*tagged*/0, devq);
1669103285Sikob
1670103285Sikob	if (sbp->sim == NULL) {
1671103285Sikob		cam_simq_free(devq);
1672103285Sikob		return (ENXIO);
1673103285Sikob	}
1674103285Sikob
1675103285Sikob	sbp->ocb = (struct sbp_ocb *) contigmalloc(
1676103285Sikob		sizeof (struct sbp_ocb) * SBP_NUM_OCB,
1677109424Ssimokawa		M_SBP, M_NOWAIT, 0x10000, 0xffffffff, PAGE_SIZE, 0ul);
1678103285Sikob	bzero(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB);
1679103285Sikob
1680103285Sikob	if (sbp->ocb == NULL) {
1681103285Sikob		printf("sbp0: ocb alloction failure\n");
1682103285Sikob		return (ENOMEM);
1683103285Sikob	}
1684103285Sikob
1685103285Sikob	STAILQ_INIT(&sbp->free_ocbs);
1686103285Sikob	for (i = 0; i < SBP_NUM_OCB; i++) {
1687103285Sikob		sbp_free_ocb(sbp, &sbp->ocb[i]);
1688103285Sikob	}
1689103285Sikob
1690103285Sikob	if (xpt_bus_register(sbp->sim, /*bus*/0) != CAM_SUCCESS) {
1691103285Sikob		cam_sim_free(sbp->sim, /*free_devq*/TRUE);
1692103285Sikob		contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB,
1693103285Sikob									M_SBP);
1694103285Sikob		return (ENXIO);
1695103285Sikob	}
1696103285Sikob
1697110269Ssimokawa	xfer = fw_xfer_alloc(M_SBP);
1698103285Sikob	xfer->act.hand = sbp_recv;
1699103285Sikob	xfer->act_type = FWACT_XFER;
1700103285Sikob#if NEED_RESPONSE
1701103285Sikob	xfer->fc = sbp->fd.fc;
1702103285Sikob#endif
1703103285Sikob	xfer->sc = (caddr_t)sbp;
1704103285Sikob
1705103285Sikob	sbp->fwb.start_hi = SBP_BIND_HI;
1706103285Sikob	sbp->fwb.start_lo = SBP_DEV2ADDR(device_get_unit(sbp->fd.dev), 0, 0);
1707103285Sikob	/* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
1708103285Sikob	sbp->fwb.addrlen = 0xffff;
1709103285Sikob	sbp->fwb.xfer = xfer;
1710103285Sikob	fw_bindadd(sbp->fd.fc, &sbp->fwb);
1711103285Sikob
1712103285Sikob	sbp->fd.post_explore = sbp_post_explore;
1713103285Sikob
1714111199Ssimokawa	if (sbp->fd.fc->status != -1) {
1715111199Ssimokawa		s = splfw();
1716111199Ssimokawa		sbp_post_explore((void *)sbp);
1717111199Ssimokawa		splx(s);
1718111199Ssimokawa	}
1719111199Ssimokawa
1720103285Sikob	return (0);
1721103285Sikob}
1722103285Sikob
1723103285Sikobstatic int
1724110145Ssimokawasbp_logout_all(struct sbp_softc *sbp)
1725110145Ssimokawa{
1726110145Ssimokawa	struct sbp_target *target;
1727110145Ssimokawa	struct sbp_dev *sdev;
1728110145Ssimokawa	int i, j;
1729110145Ssimokawa
1730110145SsimokawaSBP_DEBUG(0)
1731110145Ssimokawa	printf("sbp_logout_all\n");
1732110145SsimokawaEND_DEBUG
1733110145Ssimokawa	for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
1734110145Ssimokawa		target = &sbp->targets[i];
1735110145Ssimokawa		if (target->luns == NULL)
1736110145Ssimokawa			continue;
1737110145Ssimokawa		for (j = 0; j < target->num_lun; j++) {
1738110145Ssimokawa			sdev = &target->luns[j];
1739110336Ssimokawa			if (sdev->status >= SBP_DEV_TOATTACH &&
1740110336Ssimokawa					sdev->status <= SBP_DEV_ATTACHED)
1741110336Ssimokawa				sbp_mgm_orb(sdev, ORB_FUN_LGO, 0, 0);
1742110145Ssimokawa		}
1743110145Ssimokawa	}
1744110145Ssimokawa	return 0;
1745110145Ssimokawa}
1746110145Ssimokawa
1747110145Ssimokawastatic int
1748110145Ssimokawasbp_shutdown(device_t dev)
1749110145Ssimokawa{
1750110145Ssimokawa	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1751110145Ssimokawa
1752110145Ssimokawa	sbp_logout_all(sbp);
1753110145Ssimokawa	return (0);
1754110145Ssimokawa}
1755110145Ssimokawa
1756110145Ssimokawastatic int
1757103285Sikobsbp_detach(device_t dev)
1758103285Sikob{
1759103285Sikob	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
1760103285Sikob	struct firewire_comm *fc = sbp->fd.fc;
1761103285Sikob	int i;
1762103285Sikob
1763103285SikobSBP_DEBUG(0)
1764103285Sikob	printf("sbp_detach\n");
1765103285SikobEND_DEBUG
1766110145Ssimokawa#if 0
1767103285Sikob	/* bus reset for logout */
1768103285Sikob	sbp->fd.post_explore = NULL;
1769103285Sikob	fc->ibr(fc);
1770110145Ssimokawa#endif
1771103285Sikob
1772103285Sikob	for (i = 0; i < SBP_NUM_TARGETS; i ++)
1773110145Ssimokawa		sbp_cam_detach_target(&sbp->targets[i]);
1774103285Sikob	xpt_bus_deregister(cam_sim_path(sbp->sim));
1775110145Ssimokawa
1776110145Ssimokawa	sbp_logout_all(sbp);
1777110145Ssimokawa	/* XXX wait for logout completion */
1778110145Ssimokawa	tsleep(&i, FWPRI, "sbpdtc", hz/2);
1779110145Ssimokawa
1780110145Ssimokawa	fw_bindremove(fc, &sbp->fwb);
1781110145Ssimokawa	contigfree(sbp->ocb, sizeof (struct sbp_ocb) * SBP_NUM_OCB, M_SBP);
1782103285Sikob	bus_dma_tag_destroy(sbp->dmat);
1783110145Ssimokawa
1784110145Ssimokawa	for (i = 0; i < SBP_NUM_TARGETS; i ++)
1785110145Ssimokawa		if (sbp->targets[i].luns != NULL)
1786110145Ssimokawa			free(sbp->targets[i].luns, M_SBP);
1787110145Ssimokawa
1788103285Sikob	return (0);
1789103285Sikob}
1790103285Sikob
1791103285Sikobstatic void
1792110145Ssimokawasbp_cam_detach_target(struct sbp_target *target)
1793103285Sikob{
1794103285Sikob	int i;
1795103285Sikob	struct sbp_dev *sdev;
1796103285Sikob
1797103285Sikob	if (target->luns != NULL) {
1798108529SsimokawaSBP_DEBUG(0)
1799103285Sikob		printf("sbp_detach_target %d\n", target->target_id);
1800108529SsimokawaEND_DEBUG
1801110193Ssimokawa		for (i = 0; i < target->num_lun; i++) {
1802103285Sikob			sdev = &target->luns[i];
1803103285Sikob			if (sdev->status == SBP_DEV_RESET ||
1804103285Sikob					sdev->status == SBP_DEV_DEAD)
1805103285Sikob				continue;
1806110336Ssimokawa			if (sdev->path) {
1807103285Sikob				xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
1808110336Ssimokawa				xpt_free_path(sdev->path);
1809110336Ssimokawa				sdev->path = NULL;
1810110336Ssimokawa			}
1811103285Sikob			sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
1812103285Sikob		}
1813103285Sikob	}
1814103285Sikob}
1815103285Sikob
1816103285Sikobstatic void
1817103285Sikobsbp_timeout(void *arg)
1818103285Sikob{
1819103285Sikob	struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
1820103285Sikob	struct sbp_dev *sdev = ocb->sdev;
1821103285Sikob
1822103285Sikob	sbp_show_sdev_info(sdev, 2);
1823110336Ssimokawa	printf("request timeout ... ");
1824103285Sikob
1825110336Ssimokawa	xpt_freeze_devq(sdev->path, 1);
1826110336Ssimokawa	sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
1827110336Ssimokawa	if (sdev->flags & SBP_DEV_TIMEOUT) {
1828110269Ssimokawa#if 0
1829110336Ssimokawa		struct firewire_comm *fc;
1830110336Ssimokawa
1831110336Ssimokawa		printf("bus reset\n");
1832110336Ssimokawa		fc = sdev->target->sbp->fd.fc;
1833110336Ssimokawa		fc->ibr(fc);
1834110336Ssimokawa		sdev->status == SBP_DEV_RETRY;
1835110269Ssimokawa#else
1836110336Ssimokawa		printf("target reset\n");
1837110336Ssimokawa		sbp_mgm_orb(sdev, ORB_FUN_RST, 0, 0);
1838110269Ssimokawa#endif
1839110336Ssimokawa		sdev->flags &= ~SBP_DEV_TIMEOUT;
1840110336Ssimokawa	} else {
1841110336Ssimokawa		printf("agent reset\n");
1842110336Ssimokawa		sdev->flags |= SBP_DEV_TIMEOUT;
1843110336Ssimokawa		sbp_agent_reset(sdev);
1844110336Ssimokawa	}
1845103285Sikob	return;
1846103285Sikob}
1847103285Sikob
1848103285Sikobstatic void
1849103285Sikobsbp_action1(struct cam_sim *sim, union ccb *ccb)
1850103285Sikob{
1851103285Sikob
1852103285Sikob	struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
1853103285Sikob	struct sbp_target *target = NULL;
1854103285Sikob	struct sbp_dev *sdev = NULL;
1855103285Sikob
1856103285Sikob	/* target:lun -> sdev mapping */
1857103285Sikob	if (sbp != NULL
1858103285Sikob			&& ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
1859103285Sikob			&& ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
1860103285Sikob		target = &sbp->targets[ccb->ccb_h.target_id];
1861103285Sikob		if (target->fwdev != NULL
1862103285Sikob				&& ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
1863103285Sikob				&& ccb->ccb_h.target_lun < target->num_lun) {
1864103285Sikob			sdev = &target->luns[ccb->ccb_h.target_lun];
1865103285Sikob			if (sdev->status != SBP_DEV_ATTACHED &&
1866103285Sikob				sdev->status != SBP_DEV_PROBE)
1867103285Sikob				sdev = NULL;
1868103285Sikob		}
1869103285Sikob	}
1870103285Sikob
1871103285SikobSBP_DEBUG(1)
1872103285Sikob	if (sdev == NULL)
1873103285Sikob		printf("invalid target %d lun %d\n",
1874103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
1875103285SikobEND_DEBUG
1876103285Sikob
1877103285Sikob	switch (ccb->ccb_h.func_code) {
1878103285Sikob	case XPT_SCSI_IO:
1879103285Sikob	case XPT_RESET_DEV:
1880103285Sikob	case XPT_GET_TRAN_SETTINGS:
1881103285Sikob	case XPT_SET_TRAN_SETTINGS:
1882103285Sikob	case XPT_CALC_GEOMETRY:
1883103285Sikob		if (sdev == NULL) {
1884103285SikobSBP_DEBUG(1)
1885103285Sikob			printf("%s:%d:%d:func_code 0x%04x: "
1886103285Sikob				"Invalid target (target needed)\n",
1887103285Sikob				device_get_nameunit(sbp->fd.dev),
1888103285Sikob				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
1889103285Sikob				ccb->ccb_h.func_code);
1890103285SikobEND_DEBUG
1891103285Sikob
1892108276Ssimokawa			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1893103285Sikob			xpt_done(ccb);
1894103285Sikob			return;
1895103285Sikob		}
1896103285Sikob		break;
1897103285Sikob	case XPT_PATH_INQ:
1898103285Sikob	case XPT_NOOP:
1899103285Sikob		/* The opcodes sometimes aimed at a target (sc is valid),
1900103285Sikob		 * sometimes aimed at the SIM (sc is invalid and target is
1901103285Sikob		 * CAM_TARGET_WILDCARD)
1902103285Sikob		 */
1903103285Sikob		if (sbp == NULL &&
1904103285Sikob			ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
1905103285SikobSBP_DEBUG(0)
1906103285Sikob			printf("%s:%d:%d func_code 0x%04x: "
1907103285Sikob				"Invalid target (no wildcard)\n",
1908103285Sikob				device_get_nameunit(sbp->fd.dev),
1909103285Sikob				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
1910103285Sikob				ccb->ccb_h.func_code);
1911103285SikobEND_DEBUG
1912108276Ssimokawa			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1913103285Sikob			xpt_done(ccb);
1914103285Sikob			return;
1915103285Sikob		}
1916103285Sikob		break;
1917103285Sikob	default:
1918103285Sikob		/* XXX Hm, we should check the input parameters */
1919103285Sikob		break;
1920103285Sikob	}
1921103285Sikob
1922103285Sikob	switch (ccb->ccb_h.func_code) {
1923103285Sikob	case XPT_SCSI_IO:
1924103285Sikob	{
1925103285Sikob		struct ccb_scsiio *csio;
1926103285Sikob		struct sbp_ocb *ocb;
1927103285Sikob		int s, speed;
1928106506Ssimokawa		void *cdb;
1929103285Sikob
1930103285Sikob		csio = &ccb->csio;
1931103285Sikob
1932103285SikobSBP_DEBUG(1)
1933103285Sikob		printf("%s:%d:%d XPT_SCSI_IO: "
1934103285Sikob			"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1935103285Sikob			", flags: 0x%02x, "
1936103285Sikob			"%db cmd/%db data/%db sense\n",
1937103285Sikob			device_get_nameunit(sbp->fd.dev),
1938103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
1939103285Sikob			csio->cdb_io.cdb_bytes[0],
1940103285Sikob			csio->cdb_io.cdb_bytes[1],
1941103285Sikob			csio->cdb_io.cdb_bytes[2],
1942103285Sikob			csio->cdb_io.cdb_bytes[3],
1943103285Sikob			csio->cdb_io.cdb_bytes[4],
1944103285Sikob			csio->cdb_io.cdb_bytes[5],
1945103285Sikob			csio->cdb_io.cdb_bytes[6],
1946103285Sikob			csio->cdb_io.cdb_bytes[7],
1947103285Sikob			csio->cdb_io.cdb_bytes[8],
1948103285Sikob			csio->cdb_io.cdb_bytes[9],
1949103285Sikob			ccb->ccb_h.flags & CAM_DIR_MASK,
1950103285Sikob			csio->cdb_len, csio->dxfer_len,
1951103285Sikob			csio->sense_len);
1952103285SikobEND_DEBUG
1953103285Sikob		if(sdev == NULL){
1954103285Sikob			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1955103285Sikob			xpt_done(ccb);
1956103285Sikob			return;
1957103285Sikob		}
1958103285Sikob#if 0
1959103285Sikob		/* if we are in probe stage, pass only probe commands */
1960103285Sikob		if (sdev->status == SBP_DEV_PROBE) {
1961103285Sikob			char *name;
1962103285Sikob			name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
1963103285Sikob			printf("probe stage, periph name: %s\n", name);
1964103285Sikob			if (strcmp(name, "probe") != 0) {
1965103285Sikob				ccb->ccb_h.status = CAM_REQUEUE_REQ;
1966103285Sikob				xpt_done(ccb);
1967103285Sikob				return;
1968103285Sikob			}
1969103285Sikob		}
1970103285Sikob#endif
1971103285Sikob		if ((ocb = sbp_get_ocb(sbp)) == NULL) {
1972103285Sikob			s = splfw();
1973103285Sikob			sbp->flags |= SBP_RESOURCE_SHORTAGE;
1974103285Sikob			splx(s);
1975103285Sikob			return;
1976103285Sikob		}
1977103285Sikob		ocb->flags = OCB_ACT_CMD;
1978103285Sikob		ocb->sdev = sdev;
1979103285Sikob		ocb->ccb = ccb;
1980103285Sikob		ccb->ccb_h.ccb_sdev_ptr = sdev;
1981103285Sikob		ocb->orb[0] = htonl(1 << 31);
1982103285Sikob		ocb->orb[1] = 0;
1983103285Sikob		ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
1984103285Sikob		ocb->orb[3] = htonl(vtophys(ocb->ind_ptr));
1985103285Sikob		speed = min(target->fwdev->speed, max_speed);
1986103285Sikob		ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
1987103285Sikob						| ORB_CMD_MAXP(speed + 7));
1988103285Sikob		if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
1989103285Sikob			ocb->orb[4] |= htonl(ORB_CMD_IN);
1990103285Sikob		}
1991103285Sikob
1992103285Sikob		if (csio->ccb_h.flags & CAM_SCATTER_VALID)
1993103285Sikob			printf("sbp: CAM_SCATTER_VALID\n");
1994103285Sikob		if (csio->ccb_h.flags & CAM_DATA_PHYS)
1995103285Sikob			printf("sbp: CAM_DATA_PHYS\n");
1996103285Sikob
1997106506Ssimokawa		if (csio->ccb_h.flags & CAM_CDB_POINTER)
1998106506Ssimokawa			cdb = (void *)csio->cdb_io.cdb_ptr;
1999106506Ssimokawa		else
2000106506Ssimokawa			cdb = (void *)&csio->cdb_io.cdb_bytes;
2001106506Ssimokawa		bcopy(cdb,
2002103285Sikob			(void *)(uintptr_t)(volatile void *)&ocb->orb[5],
2003106506Ssimokawa				csio->cdb_len);
2004103285Sikob/*
2005103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2006103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2007103285Sikob*/
2008103285Sikob		if (ccb->csio.dxfer_len > 0) {
2009103285Sikob			int s;
2010103285Sikob
2011103285Sikob			if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
2012103285Sikob				printf("sbp_action1: cannot create dmamap\n");
2013103285Sikob				break;
2014103285Sikob			}
2015103285Sikob
2016103285Sikob			s = splsoftvm();
2017103285Sikob			bus_dmamap_load(/*dma tag*/sbp->dmat,
2018103285Sikob					/*dma map*/ocb->dmamap,
2019103285Sikob					ccb->csio.data_ptr,
2020103285Sikob					ccb->csio.dxfer_len,
2021103285Sikob					sbp_execute_ocb,
2022103285Sikob					ocb,
2023103285Sikob					/*flags*/0);
2024103285Sikob			splx(s);
2025103285Sikob		} else
2026103285Sikob			sbp_execute_ocb(ocb, NULL, 0, 0);
2027103285Sikob		break;
2028103285Sikob	}
2029103285Sikob	case XPT_CALC_GEOMETRY:
2030103285Sikob	{
2031103285Sikob		struct ccb_calc_geometry *ccg;
2032103285Sikob		u_int32_t size_mb;
2033103285Sikob		u_int32_t secs_per_cylinder;
2034103285Sikob		int extended = 1;
2035103285Sikob		ccg = &ccb->ccg;
2036103285Sikob
2037103285Sikob		if (ccg->block_size == 0) {
2038103285Sikob			printf("sbp_action1: block_size is 0.\n");
2039103285Sikob			ccb->ccb_h.status = CAM_REQ_INVALID;
2040103285Sikob			xpt_done(ccb);
2041103285Sikob			break;
2042103285Sikob		}
2043103285SikobSBP_DEBUG(1)
2044103285Sikob		printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2045103285Sikob			"Volume size = %d\n",
2046103285Sikob			device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim),
2047103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2048103285Sikob			ccg->volume_size);
2049103285SikobEND_DEBUG
2050103285Sikob
2051103285Sikob		size_mb = ccg->volume_size
2052103285Sikob			/ ((1024L * 1024L) / ccg->block_size);
2053103285Sikob
2054103285Sikob		if (size_mb >= 1024 && extended) {
2055103285Sikob			ccg->heads = 255;
2056103285Sikob			ccg->secs_per_track = 63;
2057103285Sikob		} else {
2058103285Sikob			ccg->heads = 64;
2059103285Sikob			ccg->secs_per_track = 32;
2060103285Sikob		}
2061103285Sikob		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2062103285Sikob		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2063103285Sikob		ccb->ccb_h.status = CAM_REQ_CMP;
2064103285Sikob		xpt_done(ccb);
2065103285Sikob		break;
2066103285Sikob	}
2067103285Sikob	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
2068103285Sikob	{
2069103285Sikob
2070103285SikobSBP_DEBUG(1)
2071103285Sikob		printf("%s:%d:XPT_RESET_BUS: \n",
2072103285Sikob			device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2073103285SikobEND_DEBUG
2074103285Sikob
2075103285Sikob		ccb->ccb_h.status = CAM_REQ_INVALID;
2076103285Sikob		xpt_done(ccb);
2077103285Sikob		break;
2078103285Sikob	}
2079103285Sikob	case XPT_PATH_INQ:		/* Path routing inquiry */
2080103285Sikob	{
2081103285Sikob		struct ccb_pathinq *cpi = &ccb->cpi;
2082103285Sikob
2083103285SikobSBP_DEBUG(1)
2084103285Sikob		printf("%s:%d:%d XPT_PATH_INQ:.\n",
2085103285Sikob			device_get_nameunit(sbp->fd.dev),
2086103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2087103285SikobEND_DEBUG
2088103285Sikob		cpi->version_num = 1; /* XXX??? */
2089103285Sikob		cpi->hba_inquiry = 0;
2090103285Sikob		cpi->target_sprt = 0;
2091111199Ssimokawa		cpi->hba_misc = PIM_NOBUSRESET;
2092103285Sikob		cpi->hba_eng_cnt = 0;
2093103285Sikob		cpi->max_target = SBP_NUM_TARGETS - 1;
2094103285Sikob		cpi->max_lun = SBP_NUM_LUNS - 1;
2095103285Sikob		cpi->initiator_id = SBP_INITIATOR;
2096103285Sikob		cpi->bus_id = sim->bus_id;
2097103285Sikob		cpi->base_transfer_speed = 400 * 1000 / 8;
2098103285Sikob		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2099103285Sikob		strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2100103285Sikob		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2101103285Sikob		cpi->unit_number = sim->unit_number;
2102103285Sikob
2103103285Sikob		cpi->ccb_h.status = CAM_REQ_CMP;
2104103285Sikob		xpt_done(ccb);
2105103285Sikob		break;
2106103285Sikob	}
2107103285Sikob	case XPT_GET_TRAN_SETTINGS:
2108103285Sikob	{
2109103285Sikob		struct ccb_trans_settings *cts = &ccb->cts;
2110103285SikobSBP_DEBUG(1)
2111103285Sikob		printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2112103285Sikob			device_get_nameunit(sbp->fd.dev),
2113103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2114103285SikobEND_DEBUG
2115103285Sikob		/* Disable disconnect and tagged queuing */
2116103285Sikob		cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
2117103285Sikob		cts->flags = 0;
2118103285Sikob
2119103285Sikob		cts->ccb_h.status = CAM_REQ_CMP;
2120103285Sikob		xpt_done(ccb);
2121103285Sikob		break;
2122103285Sikob	}
2123103285Sikob	case XPT_ABORT:
2124103285Sikob		ccb->ccb_h.status = CAM_UA_ABORT;
2125103285Sikob		xpt_done(ccb);
2126103285Sikob		break;
2127103285Sikob	default:
2128103285Sikob		ccb->ccb_h.status = CAM_REQ_INVALID;
2129103285Sikob		xpt_done(ccb);
2130103285Sikob		break;
2131103285Sikob	}
2132103285Sikob	return;
2133103285Sikob}
2134103285Sikob
2135103285Sikobstatic void
2136103285Sikobsbp_action(struct cam_sim *sim, union ccb *ccb)
2137103285Sikob{
2138103285Sikob	int s;
2139103285Sikob
2140103285Sikob	s = splfw();
2141103285Sikob	sbp_action1(sim, ccb);
2142103285Sikob	splx(s);
2143103285Sikob}
2144103285Sikob
2145103285Sikobstatic void
2146103285Sikobsbp_execute_ocb(void *arg,  bus_dma_segment_t *segments, int seg, int error)
2147103285Sikob{
2148103285Sikob	int i;
2149103285Sikob	struct sbp_ocb *ocb;
2150103285Sikob	struct sbp_ocb *prev;
2151103285Sikob	union ccb *ccb;
2152105633Ssimokawa	bus_dma_segment_t *s;
2153103285Sikob
2154103285Sikob	if (error)
2155103285Sikob		printf("sbp_execute_ocb: error=%d\n", error);
2156103285Sikob
2157103285Sikob	ocb = (struct sbp_ocb *)arg;
2158103285Sikob	if (seg == 1) {
2159103285Sikob		/* direct pointer */
2160103285Sikob		ocb->orb[3] = htonl(segments[0].ds_addr);
2161103285Sikob		ocb->orb[4] |= htonl(segments[0].ds_len);
2162103285Sikob	} else if(seg > 1) {
2163103285Sikob		/* page table */
2164103285SikobSBP_DEBUG(1)
2165103285Sikob		printf("sbp_execute_ocb: seg %d", seg);
2166103285Sikob		for (i = 0; i < seg; i++)
2167108712Ssimokawa#if __FreeBSD_version >= 500000
2168106543Ssimokawa			printf(", %tx:%zd", segments[i].ds_addr,
2169108712Ssimokawa#else
2170108877Ssimokawa			printf(", %x:%d", segments[i].ds_addr,
2171108712Ssimokawa#endif
2172103285Sikob						segments[i].ds_len);
2173103285Sikob		printf("\n");
2174103285SikobEND_DEBUG
2175103285Sikob		for (i = 0; i < seg; i++) {
2176105633Ssimokawa			s = &segments[i];
2177108877SsimokawaSBP_DEBUG(0)
2178108877Ssimokawa			/* XXX LSI Logic "< 16 byte" bug might be hit */
2179105633Ssimokawa			if (s->ds_len < 16)
2180105633Ssimokawa				printf("sbp_execute_ocb: warning, "
2181108877Ssimokawa#if __FreeBSD_version >= 500000
2182106543Ssimokawa					"segment length(%zd) is less than 16."
2183108877Ssimokawa#else
2184108877Ssimokawa					"segment length(%d) is less than 16."
2185108877Ssimokawa#endif
2186105633Ssimokawa					"(seg=%d/%d)\n", s->ds_len, i+1, seg);
2187108877SsimokawaEND_DEBUG
2188105633Ssimokawa			ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2189105633Ssimokawa			ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2190103285Sikob		}
2191103285Sikob		ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2192103285Sikob	}
2193103285Sikob
2194103285Sikob	ccb = ocb->ccb;
2195103285Sikob	prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2196103285Sikob	if (prev)
2197103285Sikob		sbp_doorbell(ocb->sdev);
2198103285Sikob	else
2199103285Sikob		sbp_orb_pointer(ocb->sdev, ocb);
2200103285Sikob}
2201103285Sikob
2202103285Sikobstatic void
2203103285Sikobsbp_poll(struct cam_sim *sim)
2204103285Sikob{
2205103285Sikob	/* should call fwohci_intr? */
2206103285Sikob	return;
2207103285Sikob}
2208103285Sikobstatic struct sbp_ocb *
2209103285Sikobsbp_dequeue_ocb(struct sbp_dev *sdev, u_int32_t orb_lo)
2210103285Sikob{
2211103285Sikob	struct sbp_ocb *ocb;
2212103285Sikob	struct sbp_ocb *next;
2213103285Sikob	int s = splfw(), order = 0;
2214103285Sikob	int flags;
2215103285Sikob
2216103285Sikob	for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2217103285Sikob		next = STAILQ_NEXT(ocb, ocb);
2218103285Sikob		flags = ocb->flags;
2219103285SikobSBP_DEBUG(1)
2220110336Ssimokawa		sbp_show_sdev_info(sdev, 2);
2221108712Ssimokawa#if __FreeBSD_version >= 500000
2222106543Ssimokawa		printf("orb: 0x%tx next: 0x%x, flags %x\n",
2223108712Ssimokawa#else
2224108712Ssimokawa		printf("orb: 0x%x next: 0x%lx, flags %x\n",
2225108712Ssimokawa#endif
2226103285Sikob			vtophys(&ocb->orb[0]), ntohl(ocb->orb[1]), flags);
2227103285SikobEND_DEBUG
2228103285Sikob		if (vtophys(&ocb->orb[0]) == orb_lo) {
2229103285Sikob			/* found */
2230103285Sikob			if (ocb->flags & OCB_RESERVED)
2231103285Sikob				ocb->flags |= OCB_DONE;
2232103285Sikob			else
2233103285Sikob				STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2234103285Sikob			if (ocb->ccb != NULL)
2235103285Sikob				untimeout(sbp_timeout, (caddr_t)ocb,
2236103285Sikob						ocb->ccb->ccb_h.timeout_ch);
2237103285Sikob			if (ocb->dmamap != NULL) {
2238103285Sikob				bus_dmamap_destroy(sdev->target->sbp->dmat,
2239103285Sikob							ocb->dmamap);
2240103285Sikob				ocb->dmamap = NULL;
2241103285Sikob			}
2242103285Sikob			break;
2243103285Sikob		} else {
2244103285Sikob			if ((ocb->flags & OCB_RESERVED) &&
2245103285Sikob					(ocb->flags & OCB_DONE)) {
2246103285Sikob				/* next orb must be fetched already */
2247103285Sikob				STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2248103285Sikob				sbp_free_ocb(sdev->target->sbp, ocb);
2249103285Sikob			} else
2250103285Sikob				order ++;
2251103285Sikob		}
2252103285Sikob	}
2253103285Sikob	splx(s);
2254103285SikobSBP_DEBUG(0)
2255103285Sikob	if (ocb && order > 0) {
2256103285Sikob		sbp_show_sdev_info(sdev, 2);
2257103285Sikob		printf("unordered execution order:%d\n", order);
2258103285Sikob	}
2259103285SikobEND_DEBUG
2260103285Sikob	return (ocb);
2261103285Sikob}
2262103285Sikob
2263103285Sikobstatic struct sbp_ocb *
2264103285Sikobsbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2265103285Sikob{
2266103285Sikob	int s = splfw();
2267103285Sikob	struct sbp_ocb *prev;
2268103285Sikob
2269103285SikobSBP_DEBUG(2)
2270103285Sikob	sbp_show_sdev_info(sdev, 2);
2271108712Ssimokawa#if __FreeBSD_version >= 500000
2272106543Ssimokawa	printf("sbp_enqueue_ocb orb=0x%tx in physical memory\n", vtophys(&ocb->orb[0]));
2273108712Ssimokawa#else
2274108712Ssimokawa	printf("sbp_enqueue_ocb orb=0x%x in physical memory\n", vtophys(&ocb->orb[0]));
2275108712Ssimokawa#endif
2276103285SikobEND_DEBUG
2277103285Sikob	prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2278103285Sikob	STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2279103285Sikob
2280103285Sikob	if (ocb->ccb != NULL)
2281103285Sikob		ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb,
2282103285Sikob					(ocb->ccb->ccb_h.timeout * hz) / 1000);
2283103285Sikob
2284110579Ssimokawa	if (prev != NULL ) {
2285103285SikobSBP_DEBUG(1)
2286108712Ssimokawa#if __FreeBSD_version >= 500000
2287106543Ssimokawa	printf("linking chain 0x%tx -> 0x%tx\n", vtophys(&prev->orb[0]),
2288108712Ssimokawa#else
2289108712Ssimokawa	printf("linking chain 0x%x -> 0x%x\n", vtophys(&prev->orb[0]),
2290108712Ssimokawa#endif
2291103285Sikob			vtophys(&ocb->orb[0]));
2292103285SikobEND_DEBUG
2293103285Sikob		prev->flags |= OCB_RESERVED;
2294103285Sikob		prev->orb[1] = htonl(vtophys(&ocb->orb[0]));
2295103285Sikob		prev->orb[0] = 0;
2296103285Sikob	}
2297103285Sikob	splx(s);
2298103285Sikob
2299103285Sikob	return prev;
2300103285Sikob}
2301103285Sikob
2302103285Sikobstatic struct sbp_ocb *
2303103285Sikobsbp_get_ocb(struct sbp_softc *sbp)
2304103285Sikob{
2305103285Sikob	struct sbp_ocb *ocb;
2306103285Sikob	int s = splfw();
2307103285Sikob	ocb = STAILQ_FIRST(&sbp->free_ocbs);
2308103285Sikob	if (ocb == NULL) {
2309103285Sikob		printf("ocb shortage!!!\n");
2310103285Sikob		return NULL;
2311103285Sikob	}
2312103285Sikob	STAILQ_REMOVE(&sbp->free_ocbs, ocb, sbp_ocb, ocb);
2313103285Sikob	splx(s);
2314103285Sikob	ocb->ccb = NULL;
2315103285Sikob	return (ocb);
2316103285Sikob}
2317103285Sikob
2318103285Sikobstatic void
2319103285Sikobsbp_free_ocb(struct sbp_softc *sbp, struct sbp_ocb *ocb)
2320103285Sikob{
2321103285Sikob#if 0 /* XXX make sure that ocb has ccb */
2322103285Sikob	if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0 &&
2323103285Sikob	    (ocb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
2324103285Sikob		ocb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2325103285Sikob		sbp->flags &= ~SBP_RESOURCE_SHORTAGE;
2326103285Sikob	}
2327103285Sikob#else
2328103285Sikob	if ((sbp->flags & SBP_RESOURCE_SHORTAGE) != 0)
2329103285Sikob		sbp->flags &= ~SBP_RESOURCE_SHORTAGE;
2330103285Sikob#endif
2331103285Sikob	ocb->flags = 0;
2332103285Sikob	ocb->ccb = NULL;
2333103285Sikob	STAILQ_INSERT_TAIL(&sbp->free_ocbs, ocb, ocb);
2334103285Sikob}
2335103285Sikob
2336103285Sikobstatic void
2337103285Sikobsbp_abort_ocb(struct sbp_ocb *ocb, int status)
2338103285Sikob{
2339103285Sikob	struct sbp_dev *sdev;
2340103285Sikob
2341103285Sikob	sdev = ocb->sdev;
2342110269SsimokawaSBP_DEBUG(1)
2343103285Sikob	sbp_show_sdev_info(sdev, 2);
2344103285Sikob	printf("sbp_abort_ocb 0x%x\n", status);
2345105792Ssimokawa	if (ocb->ccb != NULL)
2346105792Ssimokawa		sbp_print_scsi_cmd(ocb);
2347103285SikobEND_DEBUG
2348103285Sikob	if (ocb->ccb != NULL && !(ocb->flags & OCB_DONE)) {
2349110336Ssimokawa		untimeout(sbp_timeout, (caddr_t)ocb,
2350110336Ssimokawa					ocb->ccb->ccb_h.timeout_ch);
2351103285Sikob		ocb->ccb->ccb_h.status = status;
2352103285Sikob		xpt_done(ocb->ccb);
2353103285Sikob	}
2354103285Sikob	if (ocb->dmamap != NULL) {
2355103285Sikob		bus_dmamap_destroy(sdev->target->sbp->dmat, ocb->dmamap);
2356103285Sikob		ocb->dmamap = NULL;
2357103285Sikob	}
2358103285Sikob	sbp_free_ocb(sdev->target->sbp, ocb);
2359103285Sikob}
2360103285Sikob
2361103285Sikobstatic void
2362103285Sikobsbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2363103285Sikob{
2364103285Sikob	int s;
2365105792Ssimokawa	struct sbp_ocb *ocb, *next;
2366105792Ssimokawa	STAILQ_HEAD(, sbp_ocb) temp;
2367103285Sikob
2368103285Sikob	s = splfw();
2369105792Ssimokawa
2370105792Ssimokawa	bcopy(&sdev->ocbs, &temp, sizeof(temp));
2371105792Ssimokawa	STAILQ_INIT(&sdev->ocbs);
2372105792Ssimokawa	for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2373105792Ssimokawa		next = STAILQ_NEXT(ocb, ocb);
2374103285Sikob		sbp_abort_ocb(ocb, status);
2375103285Sikob	}
2376105792Ssimokawa
2377103285Sikob	splx(s);
2378103285Sikob}
2379103285Sikob
2380103285Sikobstatic devclass_t sbp_devclass;
2381103285Sikob
2382103285Sikobstatic device_method_t sbp_methods[] = {
2383103285Sikob	/* device interface */
2384103285Sikob	DEVMETHOD(device_identify,	sbp_identify),
2385103285Sikob	DEVMETHOD(device_probe,		sbp_probe),
2386103285Sikob	DEVMETHOD(device_attach,	sbp_attach),
2387103285Sikob	DEVMETHOD(device_detach,	sbp_detach),
2388110145Ssimokawa	DEVMETHOD(device_shutdown,	sbp_shutdown),
2389103285Sikob
2390103285Sikob	{ 0, 0 }
2391103285Sikob};
2392103285Sikob
2393103285Sikobstatic driver_t sbp_driver = {
2394103285Sikob	"sbp",
2395103285Sikob	sbp_methods,
2396103285Sikob	sizeof(struct sbp_softc),
2397103285Sikob};
2398103285SikobDRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2399103285SikobMODULE_VERSION(sbp, 1);
2400103285SikobMODULE_DEPEND(sbp, firewire, 1, 1, 1);
2401103285SikobMODULE_DEPEND(sbp, cam, 1, 1, 1);
2402