sbp.c revision 247109
1139749Simp/*-
2122526Ssimokawa * Copyright (c) 2003 Hidetoshi Shimokawa
3122526Ssimokawa * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
4103285Sikob * All rights reserved.
5103285Sikob *
6103285Sikob * Redistribution and use in source and binary forms, with or without
7103285Sikob * modification, are permitted provided that the following conditions
8103285Sikob * are met:
9103285Sikob * 1. Redistributions of source code must retain the above copyright
10103285Sikob *    notice, this list of conditions and the following disclaimer.
11103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
12103285Sikob *    notice, this list of conditions and the following disclaimer in the
13103285Sikob *    documentation and/or other materials provided with the distribution.
14103285Sikob * 3. All advertising materials mentioning features or use of this software
15103285Sikob *    must display the acknowledgement as bellow:
16103285Sikob *
17103285Sikob *    This product includes software developed by K. Kobayashi and H. Shimokawa
18103285Sikob *
19103285Sikob * 4. The name of the author may not be used to endorse or promote products
20103285Sikob *    derived from this software without specific prior written permission.
21103285Sikob *
22103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23103285Sikob * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24103285Sikob * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25103285Sikob * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26103285Sikob * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27103285Sikob * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28103285Sikob * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30103285Sikob * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31103285Sikob * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32103285Sikob * POSSIBILITY OF SUCH DAMAGE.
33103285Sikob *
34103285Sikob * $FreeBSD: head/sys/dev/firewire/sbp.c 247109 2013-02-21 17:50:21Z imp $
35103285Sikob *
36103285Sikob */
37103285Sikob
38103285Sikob#include <sys/param.h>
39103285Sikob#include <sys/systm.h>
40103285Sikob#include <sys/module.h>
41103285Sikob#include <sys/bus.h>
42127468Ssimokawa#include <sys/kernel.h>
43103285Sikob#include <sys/sysctl.h>
44103285Sikob#include <machine/bus.h>
45103285Sikob#include <sys/malloc.h>
46127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 501102
47117126Sscottl#include <sys/lock.h>
48117126Sscottl#include <sys/mutex.h>
49117732Ssimokawa#endif
50103285Sikob
51127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500106
52112136Ssimokawa#include <sys/devicestat.h>	/* for struct devstat */
53112136Ssimokawa#endif
54103285Sikob
55127468Ssimokawa#ifdef __DragonFly__
56127468Ssimokawa#include <bus/cam/cam.h>
57127468Ssimokawa#include <bus/cam/cam_ccb.h>
58127468Ssimokawa#include <bus/cam/cam_sim.h>
59127468Ssimokawa#include <bus/cam/cam_xpt_sim.h>
60127468Ssimokawa#include <bus/cam/cam_debug.h>
61127468Ssimokawa#include <bus/cam/cam_periph.h>
62127468Ssimokawa#include <bus/cam/scsi/scsi_all.h>
63127468Ssimokawa
64127468Ssimokawa#include <bus/firewire/firewire.h>
65127468Ssimokawa#include <bus/firewire/firewirereg.h>
66127468Ssimokawa#include <bus/firewire/fwdma.h>
67127468Ssimokawa#include <bus/firewire/iec13213.h>
68127468Ssimokawa#include "sbp.h"
69127468Ssimokawa#else
70103285Sikob#include <cam/cam.h>
71103285Sikob#include <cam/cam_ccb.h>
72103285Sikob#include <cam/cam_sim.h>
73103285Sikob#include <cam/cam_xpt_sim.h>
74103285Sikob#include <cam/cam_debug.h>
75103285Sikob#include <cam/cam_periph.h>
76103285Sikob#include <cam/scsi/scsi_all.h>
77103285Sikob
78103285Sikob#include <dev/firewire/firewire.h>
79103285Sikob#include <dev/firewire/firewirereg.h>
80113584Ssimokawa#include <dev/firewire/fwdma.h>
81103285Sikob#include <dev/firewire/iec13213.h>
82120660Ssimokawa#include <dev/firewire/sbp.h>
83127468Ssimokawa#endif
84103285Sikob
85103285Sikob#define ccb_sdev_ptr	spriv_ptr0
86103285Sikob#define ccb_sbp_ptr	spriv_ptr1
87103285Sikob
88111615Ssimokawa#define SBP_NUM_TARGETS 8 /* MAX 64 */
89121185Ssimokawa/*
90121185Ssimokawa * Scan_bus doesn't work for more than 8 LUNs
91121185Ssimokawa * because of CAM_SCSI2_MAXLUN in cam_xpt.c
92121185Ssimokawa */
93121185Ssimokawa#define SBP_NUM_LUNS 64
94167622Ssimokawa#define SBP_MAXPHYS  MIN(MAXPHYS, (512*1024) /* 512KB */)
95113584Ssimokawa#define SBP_DMA_SIZE PAGE_SIZE
96113584Ssimokawa#define SBP_LOGIN_SIZE sizeof(struct sbp_login_res)
97113584Ssimokawa#define SBP_QUEUE_LEN ((SBP_DMA_SIZE - SBP_LOGIN_SIZE) / sizeof(struct sbp_ocb))
98103285Sikob#define SBP_NUM_OCB (SBP_QUEUE_LEN * SBP_NUM_TARGETS)
99113584Ssimokawa
100111615Ssimokawa/*
101111615Ssimokawa * STATUS FIFO addressing
102111615Ssimokawa *   bit
103111615Ssimokawa * -----------------------
104130532Sdfr *  0- 1( 2): 0 (alignment)
105111615Ssimokawa *  2- 7( 6): target
106111615Ssimokawa *  8-15( 8): lun
107120660Ssimokawa * 16-31( 8): reserved
108111615Ssimokawa * 32-47(16): SBP_BIND_HI
109111615Ssimokawa * 48-64(16): bus_id, node_id
110111615Ssimokawa */
111103285Sikob#define SBP_BIND_HI 0x1
112120660Ssimokawa#define SBP_DEV2ADDR(t, l) \
113120660Ssimokawa	(((u_int64_t)SBP_BIND_HI << 32) \
114120660Ssimokawa	| (((l) & 0xff) << 8) \
115120660Ssimokawa	| (((t) & 0x3f) << 2))
116103285Sikob#define SBP_ADDR2TRG(a)	(((a) >> 2) & 0x3f)
117103285Sikob#define SBP_ADDR2LUN(a)	(((a) >> 8) & 0xff)
118120660Ssimokawa#define SBP_INITIATOR 7
119103285Sikob
120103285Sikobstatic char *orb_fun_name[] = {
121120660Ssimokawa	ORB_FUN_NAMES
122103285Sikob};
123103285Sikob
124111203Ssimokawastatic int debug = 0;
125103285Sikobstatic int auto_login = 1;
126124251Ssimokawastatic int max_speed = -1;
127111199Ssimokawastatic int sbp_cold = 1;
128122387Ssimokawastatic int ex_login = 1;
129122387Ssimokawastatic int login_delay = 1000;	/* msec */
130122387Ssimokawastatic int scan_delay = 500;	/* msec */
131127468Ssimokawastatic int use_doorbell = 0;
132127468Ssimokawastatic int sbp_tags = 0;
133103285Sikob
134103285SikobSYSCTL_DECL(_hw_firewire);
135227309Sedstatic SYSCTL_NODE(_hw_firewire, OID_AUTO, sbp, CTLFLAG_RD, 0,
136227309Sed	"SBP-II Subsystem");
137103285SikobSYSCTL_INT(_debug, OID_AUTO, sbp_debug, CTLFLAG_RW, &debug, 0,
138103285Sikob	"SBP debug flag");
139103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, auto_login, CTLFLAG_RW, &auto_login, 0,
140103285Sikob	"SBP perform login automatically");
141103285SikobSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, max_speed, CTLFLAG_RW, &max_speed, 0,
142103285Sikob	"SBP transfer max speed");
143121792SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, exclusive_login, CTLFLAG_RW,
144130677Ssimokawa	&ex_login, 0, "SBP enable exclusive login");
145122387SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, login_delay, CTLFLAG_RW,
146122387Ssimokawa	&login_delay, 0, "SBP login delay in msec");
147122387SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, scan_delay, CTLFLAG_RW,
148122387Ssimokawa	&scan_delay, 0, "SBP scan delay in msec");
149127468SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, use_doorbell, CTLFLAG_RW,
150127468Ssimokawa	&use_doorbell, 0, "SBP use doorbell request");
151127468SsimokawaSYSCTL_INT(_hw_firewire_sbp, OID_AUTO, tags, CTLFLAG_RW, &sbp_tags, 0,
152127468Ssimokawa	"SBP tagged queuing support");
153103285Sikob
154122387SsimokawaTUNABLE_INT("hw.firewire.sbp.auto_login", &auto_login);
155122387SsimokawaTUNABLE_INT("hw.firewire.sbp.max_speed", &max_speed);
156122387SsimokawaTUNABLE_INT("hw.firewire.sbp.exclusive_login", &ex_login);
157122387SsimokawaTUNABLE_INT("hw.firewire.sbp.login_delay", &login_delay);
158122387SsimokawaTUNABLE_INT("hw.firewire.sbp.scan_delay", &scan_delay);
159127468SsimokawaTUNABLE_INT("hw.firewire.sbp.use_doorbell", &use_doorbell);
160127468SsimokawaTUNABLE_INT("hw.firewire.sbp.tags", &sbp_tags);
161122387Ssimokawa
162103285Sikob#define NEED_RESPONSE 0
163103285Sikob
164113584Ssimokawa#define SBP_SEG_MAX rounddown(0xffff, PAGE_SIZE)
165113584Ssimokawa#ifdef __sparc64__ /* iommu */
166167622Ssimokawa#define SBP_IND_MAX howmany(SBP_MAXPHYS, SBP_SEG_MAX)
167113584Ssimokawa#else
168167622Ssimokawa#define SBP_IND_MAX howmany(SBP_MAXPHYS, PAGE_SIZE)
169113584Ssimokawa#endif
170103285Sikobstruct sbp_ocb {
171103285Sikob	STAILQ_ENTRY(sbp_ocb)	ocb;
172103285Sikob	union ccb	*ccb;
173113584Ssimokawa	bus_addr_t	bus_addr;
174129585Sdfr	uint32_t	orb[8];
175129585Sdfr#define IND_PTR_OFFSET	(8*sizeof(uint32_t))
176120660Ssimokawa	struct ind_ptr  ind_ptr[SBP_IND_MAX];
177103285Sikob	struct sbp_dev	*sdev;
178113584Ssimokawa	int		flags; /* XXX should be removed */
179103285Sikob	bus_dmamap_t	dmamap;
180103285Sikob};
181113584Ssimokawa
182103285Sikob#define OCB_ACT_MGM 0
183103285Sikob#define OCB_ACT_CMD 1
184113584Ssimokawa#define OCB_MATCH(o,s)	((o)->bus_addr == ntohl((s)->orb_lo))
185103285Sikob
186103285Sikobstruct sbp_dev{
187103285Sikob#define SBP_DEV_RESET		0	/* accept login */
188114732Ssimokawa#define SBP_DEV_LOGIN		1	/* to login */
189110336Ssimokawa#if 0
190103285Sikob#define SBP_DEV_RECONN		2	/* to reconnect */
191110336Ssimokawa#endif
192103285Sikob#define SBP_DEV_TOATTACH	3	/* to attach */
193103285Sikob#define SBP_DEV_PROBE		4	/* scan lun */
194103285Sikob#define SBP_DEV_ATTACHED	5	/* in operation */
195103285Sikob#define SBP_DEV_DEAD		6	/* unavailable unit */
196103285Sikob#define SBP_DEV_RETRY		7	/* unavailable unit */
197129585Sdfr	uint8_t status:4,
198114732Ssimokawa		 timeout:4;
199129585Sdfr	uint8_t type;
200129585Sdfr	uint16_t lun_id;
201129585Sdfr	uint16_t freeze;
202121185Ssimokawa#define	ORB_LINK_DEAD		(1 << 0)
203121185Ssimokawa#define	VALID_LUN		(1 << 1)
204121185Ssimokawa#define	ORB_POINTER_ACTIVE	(1 << 2)
205121185Ssimokawa#define	ORB_POINTER_NEED	(1 << 3)
206127468Ssimokawa#define	ORB_DOORBELL_ACTIVE	(1 << 4)
207127468Ssimokawa#define	ORB_DOORBELL_NEED	(1 << 5)
208127468Ssimokawa#define	ORB_SHORTAGE		(1 << 6)
209129585Sdfr	uint16_t flags;
210103285Sikob	struct cam_path *path;
211103285Sikob	struct sbp_target *target;
212113584Ssimokawa	struct fwdma_alloc dma;
213113584Ssimokawa	struct sbp_login_res *login;
214111615Ssimokawa	struct callout login_callout;
215113584Ssimokawa	struct sbp_ocb *ocb;
216103285Sikob	STAILQ_HEAD(, sbp_ocb) ocbs;
217113584Ssimokawa	STAILQ_HEAD(, sbp_ocb) free_ocbs;
218127468Ssimokawa	struct sbp_ocb *last_ocb;
219103285Sikob	char vendor[32];
220103285Sikob	char product[32];
221103285Sikob	char revision[10];
222188756Ssbruno	char bustgtlun[32];
223103285Sikob};
224103285Sikob
225103285Sikobstruct sbp_target {
226103285Sikob	int target_id;
227103285Sikob	int num_lun;
228121185Ssimokawa	struct sbp_dev	**luns;
229103285Sikob	struct sbp_softc *sbp;
230103285Sikob	struct fw_device *fwdev;
231129585Sdfr	uint32_t mgm_hi, mgm_lo;
232111615Ssimokawa	struct sbp_ocb *mgm_ocb_cur;
233111615Ssimokawa	STAILQ_HEAD(, sbp_ocb) mgm_ocb_queue;
234111615Ssimokawa	struct callout mgm_ocb_timeout;
235111615Ssimokawa	struct callout scan_callout;
236113584Ssimokawa	STAILQ_HEAD(, fw_xfer) xferlist;
237113584Ssimokawa	int n_xfer;
238103285Sikob};
239103285Sikob
240103285Sikobstruct sbp_softc {
241103285Sikob	struct firewire_dev_comm fd;
242103285Sikob	struct cam_sim  *sim;
243111615Ssimokawa	struct cam_path  *path;
244103285Sikob	struct sbp_target targets[SBP_NUM_TARGETS];
245103285Sikob	struct fw_bind fwb;
246103285Sikob	bus_dma_tag_t	dmat;
247122387Ssimokawa	struct timeval last_busreset;
248124145Ssimokawa#define SIMQ_FREEZED 1
249124145Ssimokawa	int flags;
250170374Ssimokawa	struct mtx mtx;
251103285Sikob};
252170374Ssimokawa#define SBP_LOCK(sbp) mtx_lock(&(sbp)->mtx)
253170374Ssimokawa#define SBP_UNLOCK(sbp) mtx_unlock(&(sbp)->mtx)
254122387Ssimokawa
255124169Ssimokawastatic void sbp_post_explore (void *);
256124169Ssimokawastatic void sbp_recv (struct fw_xfer *);
257124169Ssimokawastatic void sbp_mgm_callback (struct fw_xfer *);
258121185Ssimokawa#if 0
259124169Ssimokawastatic void sbp_cmd_callback (struct fw_xfer *);
260121185Ssimokawa#endif
261124169Ssimokawastatic void sbp_orb_pointer (struct sbp_dev *, struct sbp_ocb *);
262127468Ssimokawastatic void sbp_doorbell(struct sbp_dev *);
263124169Ssimokawastatic void sbp_execute_ocb (void *,  bus_dma_segment_t *, int, int);
264124169Ssimokawastatic void sbp_free_ocb (struct sbp_dev *, struct sbp_ocb *);
265124169Ssimokawastatic void sbp_abort_ocb (struct sbp_ocb *, int);
266124169Ssimokawastatic void sbp_abort_all_ocbs (struct sbp_dev *, int);
267170807Ssimokawastatic struct fw_xfer * sbp_write_cmd_locked (struct sbp_dev *, int, int);
268124169Ssimokawastatic struct fw_xfer * sbp_write_cmd (struct sbp_dev *, int, int);
269124169Ssimokawastatic struct sbp_ocb * sbp_get_ocb (struct sbp_dev *);
270124169Ssimokawastatic struct sbp_ocb * sbp_enqueue_ocb (struct sbp_dev *, struct sbp_ocb *);
271124169Ssimokawastatic struct sbp_ocb * sbp_dequeue_ocb (struct sbp_dev *, struct sbp_status *);
272121185Ssimokawastatic void sbp_cam_detach_sdev(struct sbp_dev *);
273121185Ssimokawastatic void sbp_free_sdev(struct sbp_dev *);
274124169Ssimokawastatic void sbp_cam_detach_target (struct sbp_target *);
275124169Ssimokawastatic void sbp_free_target (struct sbp_target *);
276124169Ssimokawastatic void sbp_mgm_timeout (void *arg);
277124169Ssimokawastatic void sbp_timeout (void *arg);
278124169Ssimokawastatic void sbp_mgm_orb (struct sbp_dev *, int, struct sbp_ocb *);
279103285Sikob
280227293Sedstatic MALLOC_DEFINE(M_SBP, "sbp", "SBP-II/FireWire");
281103285Sikob
282103285Sikob/* cam related functions */
283103285Sikobstatic void	sbp_action(struct cam_sim *sim, union ccb *ccb);
284103285Sikobstatic void	sbp_poll(struct cam_sim *sim);
285111615Ssimokawastatic void	sbp_cam_scan_lun(struct cam_periph *, union ccb *);
286111615Ssimokawastatic void	sbp_cam_scan_target(void *arg);
287103285Sikob
288103285Sikobstatic char *orb_status0[] = {
289103285Sikob	/* 0 */ "No additional information to report",
290103285Sikob	/* 1 */ "Request type not supported",
291103285Sikob	/* 2 */ "Speed not supported",
292103285Sikob	/* 3 */ "Page size not supported",
293103285Sikob	/* 4 */ "Access denied",
294103285Sikob	/* 5 */ "Logical unit not supported",
295103285Sikob	/* 6 */ "Maximum payload too small",
296103285Sikob	/* 7 */ "Reserved for future standardization",
297103285Sikob	/* 8 */ "Resources unavailable",
298103285Sikob	/* 9 */ "Function rejected",
299103285Sikob	/* A */ "Login ID not recognized",
300103285Sikob	/* B */ "Dummy ORB completed",
301103285Sikob	/* C */ "Request aborted",
302103285Sikob	/* FF */ "Unspecified error"
303103285Sikob#define MAX_ORB_STATUS0 0xd
304103285Sikob};
305103285Sikob
306103285Sikobstatic char *orb_status1_object[] = {
307103285Sikob	/* 0 */ "Operation request block (ORB)",
308103285Sikob	/* 1 */ "Data buffer",
309103285Sikob	/* 2 */ "Page table",
310103285Sikob	/* 3 */ "Unable to specify"
311103285Sikob};
312103285Sikob
313103285Sikobstatic char *orb_status1_serial_bus_error[] = {
314103285Sikob	/* 0 */ "Missing acknowledge",
315103285Sikob	/* 1 */ "Reserved; not to be used",
316103285Sikob	/* 2 */ "Time-out error",
317103285Sikob	/* 3 */ "Reserved; not to be used",
318103285Sikob	/* 4 */ "Busy retry limit exceeded(X)",
319103285Sikob	/* 5 */ "Busy retry limit exceeded(A)",
320103285Sikob	/* 6 */ "Busy retry limit exceeded(B)",
321103285Sikob	/* 7 */ "Reserved for future standardization",
322103285Sikob	/* 8 */ "Reserved for future standardization",
323103285Sikob	/* 9 */ "Reserved for future standardization",
324103285Sikob	/* A */ "Reserved for future standardization",
325103285Sikob	/* B */ "Tardy retry limit exceeded",
326103285Sikob	/* C */ "Conflict error",
327103285Sikob	/* D */ "Data error",
328103285Sikob	/* E */ "Type error",
329103285Sikob	/* F */ "Address error"
330103285Sikob};
331103285Sikob
332103285Sikobstatic void
333103285Sikobsbp_identify(driver_t *driver, device_t parent)
334103285Sikob{
335103285SikobSBP_DEBUG(0)
336103285Sikob	printf("sbp_identify\n");
337103285SikobEND_DEBUG
338103285Sikob
339188393Sfjoe	BUS_ADD_CHILD(parent, 0, "sbp", device_get_unit(parent));
340103285Sikob}
341103285Sikob
342103285Sikob/*
343103285Sikob * sbp_probe()
344103285Sikob */
345103285Sikobstatic int
346103285Sikobsbp_probe(device_t dev)
347103285Sikob{
348103285Sikob	device_t pa;
349103285Sikob
350103285SikobSBP_DEBUG(0)
351103285Sikob	printf("sbp_probe\n");
352103285SikobEND_DEBUG
353103285Sikob
354103285Sikob	pa = device_get_parent(dev);
355103285Sikob	if(device_get_unit(dev) != device_get_unit(pa)){
356103285Sikob		return(ENXIO);
357103285Sikob	}
358103285Sikob
359120660Ssimokawa	device_set_desc(dev, "SBP-2/SCSI over FireWire");
360111615Ssimokawa
361132432Ssimokawa#if 0
362111615Ssimokawa	if (bootverbose)
363111615Ssimokawa		debug = bootverbose;
364132432Ssimokawa#endif
365132432Ssimokawa
366103285Sikob	return (0);
367103285Sikob}
368103285Sikob
369188756Ssbruno/*
370188756Ssbruno * Display device characteristics on the console
371188756Ssbruno */
372103285Sikobstatic void
373188756Ssbrunosbp_show_sdev_info(struct sbp_dev *sdev)
374103285Sikob{
375103285Sikob	struct fw_device *fwdev;
376103285Sikob
377103285Sikob	fwdev = sdev->target->fwdev;
378188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
379188756Ssbruno		"%s: %s: ordered:%d type:%d EUI:%08x%08x node:%d "
380188756Ssbruno		"speed:%d maxrec:%d\n",
381188756Ssbruno		__func__,
382188756Ssbruno		sdev->bustgtlun,
383108503Ssimokawa		(sdev->type & 0x40) >> 6,
384108503Ssimokawa		(sdev->type & 0x1f),
385103285Sikob		fwdev->eui.hi,
386103285Sikob		fwdev->eui.lo,
387103285Sikob		fwdev->dst,
388103285Sikob		fwdev->speed,
389188756Ssbruno		fwdev->maxrec);
390188756Ssbruno
391188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
392188756Ssbruno			"%s: %s '%s' '%s' '%s'\n",
393188756Ssbruno			__func__,
394188756Ssbruno			sdev->bustgtlun,
395188756Ssbruno			sdev->vendor,
396188756Ssbruno			sdev->product,
397188756Ssbruno			sdev->revision);
398103285Sikob}
399103285Sikob
400110184Ssimokawastatic struct {
401110184Ssimokawa	int bus;
402110184Ssimokawa	int target;
403110184Ssimokawa	struct fw_eui64 eui;
404110184Ssimokawa} wired[] = {
405110184Ssimokawa	/* Bus	Target	EUI64 */
406110184Ssimokawa#if 0
407110184Ssimokawa	{0,	2,	{0x00018ea0, 0x01fd0154}},	/* Logitec HDD */
408110184Ssimokawa	{0,	0,	{0x00018ea6, 0x00100682}},	/* Logitec DVD */
409110184Ssimokawa	{0,	1,	{0x00d03200, 0xa412006a}},	/* Yano HDD */
410110184Ssimokawa#endif
411110184Ssimokawa	{-1,	-1,	{0,0}}
412110184Ssimokawa};
413110184Ssimokawa
414110184Ssimokawastatic int
415110184Ssimokawasbp_new_target(struct sbp_softc *sbp, struct fw_device *fwdev)
416110184Ssimokawa{
417110184Ssimokawa	int bus, i, target=-1;
418110184Ssimokawa	char w[SBP_NUM_TARGETS];
419110184Ssimokawa
420110184Ssimokawa	bzero(w, sizeof(w));
421110184Ssimokawa	bus = device_get_unit(sbp->fd.dev);
422110184Ssimokawa
423110184Ssimokawa	/* XXX wired-down configuration should be gotten from
424110184Ssimokawa					tunable or device hint */
425110184Ssimokawa	for (i = 0; wired[i].bus >= 0; i ++) {
426110184Ssimokawa		if (wired[i].bus == bus) {
427110184Ssimokawa			w[wired[i].target] = 1;
428110184Ssimokawa			if (wired[i].eui.hi == fwdev->eui.hi &&
429110184Ssimokawa					wired[i].eui.lo == fwdev->eui.lo)
430110184Ssimokawa				target = wired[i].target;
431110184Ssimokawa		}
432110184Ssimokawa	}
433110184Ssimokawa	if (target >= 0) {
434110184Ssimokawa		if(target < SBP_NUM_TARGETS &&
435110184Ssimokawa				sbp->targets[target].fwdev == NULL)
436110184Ssimokawa			return(target);
437110184Ssimokawa		device_printf(sbp->fd.dev,
438110184Ssimokawa			"target %d is not free for %08x:%08x\n",
439110184Ssimokawa			target, fwdev->eui.hi, fwdev->eui.lo);
440110184Ssimokawa		target = -1;
441110184Ssimokawa	}
442110184Ssimokawa	/* non-wired target */
443110184Ssimokawa	for (i = 0; i < SBP_NUM_TARGETS; i ++)
444110184Ssimokawa		if (sbp->targets[i].fwdev == NULL && w[i] == 0) {
445110184Ssimokawa			target = i;
446110184Ssimokawa			break;
447110184Ssimokawa		}
448110184Ssimokawa
449110184Ssimokawa	return target;
450110184Ssimokawa}
451110184Ssimokawa
452121185Ssimokawastatic void
453121185Ssimokawasbp_alloc_lun(struct sbp_target *target)
454103285Sikob{
455108503Ssimokawa	struct crom_context cc;
456108503Ssimokawa	struct csrreg *reg;
457121185Ssimokawa	struct sbp_dev *sdev, **newluns;
458121185Ssimokawa	struct sbp_softc *sbp;
459121185Ssimokawa	int maxlun, lun, i;
460103285Sikob
461121185Ssimokawa	sbp = target->sbp;
462114069Ssimokawa	crom_init_context(&cc, target->fwdev->csrrom);
463108503Ssimokawa	/* XXX shoud parse appropriate unit directories only */
464108503Ssimokawa	maxlun = -1;
465108503Ssimokawa	while (cc.depth >= 0) {
466108503Ssimokawa		reg = crom_search_key(&cc, CROM_LUN);
467108503Ssimokawa		if (reg == NULL)
468108503Ssimokawa			break;
469110839Ssimokawa		lun = reg->val & 0xffff;
470108642SsimokawaSBP_DEBUG(0)
471108642Ssimokawa		printf("target %d lun %d found\n", target->target_id, lun);
472108642SsimokawaEND_DEBUG
473108503Ssimokawa		if (maxlun < lun)
474108503Ssimokawa			maxlun = lun;
475108503Ssimokawa		crom_next(&cc);
476108503Ssimokawa	}
477110839Ssimokawa	if (maxlun < 0)
478121185Ssimokawa		printf("%s:%d no LUN found\n",
479121185Ssimokawa		    device_get_nameunit(target->sbp->fd.dev),
480121185Ssimokawa		    target->target_id);
481121185Ssimokawa
482121185Ssimokawa	maxlun ++;
483110839Ssimokawa	if (maxlun >= SBP_NUM_LUNS)
484110839Ssimokawa		maxlun = SBP_NUM_LUNS;
485121185Ssimokawa
486121185Ssimokawa	/* Invalidiate stale devices */
487121185Ssimokawa	for (lun = 0; lun < target->num_lun; lun ++) {
488121185Ssimokawa		sdev = target->luns[lun];
489121185Ssimokawa		if (sdev == NULL)
490121185Ssimokawa			continue;
491121185Ssimokawa		sdev->flags &= ~VALID_LUN;
492121185Ssimokawa		if (lun >= maxlun) {
493121185Ssimokawa			/* lost device */
494121185Ssimokawa			sbp_cam_detach_sdev(sdev);
495121185Ssimokawa			sbp_free_sdev(sdev);
496187993Ssbruno			target->luns[lun] = NULL;
497121185Ssimokawa		}
498103285Sikob	}
499121185Ssimokawa
500121185Ssimokawa	/* Reallocate */
501121185Ssimokawa	if (maxlun != target->num_lun) {
502121185Ssimokawa		newluns = (struct sbp_dev **) realloc(target->luns,
503121185Ssimokawa		    sizeof(struct sbp_dev *) * maxlun,
504121185Ssimokawa		    M_SBP, M_NOWAIT | M_ZERO);
505121185Ssimokawa
506121185Ssimokawa		if (newluns == NULL) {
507127468Ssimokawa			printf("%s: realloc failed\n", __func__);
508121185Ssimokawa			newluns = target->luns;
509121185Ssimokawa			maxlun = target->num_lun;
510121185Ssimokawa		}
511121185Ssimokawa
512121185Ssimokawa		/*
513121185Ssimokawa		 * We must zero the extended region for the case
514121185Ssimokawa		 * realloc() doesn't allocate new buffer.
515121185Ssimokawa		 */
516121185Ssimokawa		if (maxlun > target->num_lun)
517121185Ssimokawa			bzero(&newluns[target->num_lun],
518121185Ssimokawa			    sizeof(struct sbp_dev *) *
519121185Ssimokawa			    (maxlun - target->num_lun));
520121185Ssimokawa
521121185Ssimokawa		target->luns = newluns;
522121185Ssimokawa		target->num_lun = maxlun;
523121185Ssimokawa	}
524121185Ssimokawa
525108503Ssimokawa	crom_init_context(&cc, target->fwdev->csrrom);
526108503Ssimokawa	while (cc.depth >= 0) {
527121185Ssimokawa		int new = 0;
528121185Ssimokawa
529108503Ssimokawa		reg = crom_search_key(&cc, CROM_LUN);
530108503Ssimokawa		if (reg == NULL)
531108503Ssimokawa			break;
532110839Ssimokawa		lun = reg->val & 0xffff;
533110839Ssimokawa		if (lun >= SBP_NUM_LUNS) {
534110839Ssimokawa			printf("too large lun %d\n", lun);
535121185Ssimokawa			goto next;
536110839Ssimokawa		}
537121185Ssimokawa
538121185Ssimokawa		sdev = target->luns[lun];
539121185Ssimokawa		if (sdev == NULL) {
540121185Ssimokawa			sdev = malloc(sizeof(struct sbp_dev),
541121185Ssimokawa			    M_SBP, M_NOWAIT | M_ZERO);
542121185Ssimokawa			if (sdev == NULL) {
543127468Ssimokawa				printf("%s: malloc failed\n", __func__);
544121185Ssimokawa				goto next;
545121185Ssimokawa			}
546121185Ssimokawa			target->luns[lun] = sdev;
547121185Ssimokawa			sdev->lun_id = lun;
548121185Ssimokawa			sdev->target = target;
549121185Ssimokawa			STAILQ_INIT(&sdev->ocbs);
550121185Ssimokawa			CALLOUT_INIT(&sdev->login_callout);
551121185Ssimokawa			sdev->status = SBP_DEV_RESET;
552121185Ssimokawa			new = 1;
553188756Ssbruno			snprintf(sdev->bustgtlun, 32, "%s:%d:%d",
554188756Ssbruno					device_get_nameunit(sdev->target->sbp->fd.dev),
555188756Ssbruno					sdev->target->target_id,
556188756Ssbruno					sdev->lun_id);
557121185Ssimokawa		}
558121185Ssimokawa		sdev->flags |= VALID_LUN;
559119196Ssimokawa		sdev->type = (reg->val & 0xff0000) >> 16;
560113584Ssimokawa
561121185Ssimokawa		if (new == 0)
562121185Ssimokawa			goto next;
563121185Ssimokawa
564113584Ssimokawa		fwdma_malloc(sbp->fd.fc,
565129585Sdfr			/* alignment */ sizeof(uint32_t),
566219543Smarius			SBP_DMA_SIZE, &sdev->dma, BUS_DMA_NOWAIT |
567219543Smarius			BUS_DMA_COHERENT);
568113584Ssimokawa		if (sdev->dma.v_addr == NULL) {
569113584Ssimokawa			printf("%s: dma space allocation failed\n",
570127468Ssimokawa							__func__);
571121185Ssimokawa			free(sdev, M_SBP);
572121185Ssimokawa			target->luns[lun] = NULL;
573121185Ssimokawa			goto next;
574113584Ssimokawa		}
575113584Ssimokawa		sdev->login = (struct sbp_login_res *) sdev->dma.v_addr;
576113584Ssimokawa		sdev->ocb = (struct sbp_ocb *)
577113584Ssimokawa				((char *)sdev->dma.v_addr + SBP_LOGIN_SIZE);
578113584Ssimokawa		bzero((char *)sdev->ocb,
579113584Ssimokawa			sizeof (struct sbp_ocb) * SBP_QUEUE_LEN);
580113584Ssimokawa
581113584Ssimokawa		STAILQ_INIT(&sdev->free_ocbs);
582113584Ssimokawa		for (i = 0; i < SBP_QUEUE_LEN; i++) {
583113584Ssimokawa			struct sbp_ocb *ocb;
584113584Ssimokawa			ocb = &sdev->ocb[i];
585113584Ssimokawa			ocb->bus_addr = sdev->dma.bus_addr
586113584Ssimokawa				+ SBP_LOGIN_SIZE
587113584Ssimokawa				+ sizeof(struct sbp_ocb) * i
588113584Ssimokawa				+ offsetof(struct sbp_ocb, orb[0]);
589113584Ssimokawa			if (bus_dmamap_create(sbp->dmat, 0, &ocb->dmamap)) {
590113584Ssimokawa				printf("sbp_attach: cannot create dmamap\n");
591121185Ssimokawa				/* XXX */
592121185Ssimokawa				goto next;
593113584Ssimokawa			}
594113584Ssimokawa			sbp_free_ocb(sdev, ocb);
595113584Ssimokawa		}
596121185Ssimokawanext:
597108503Ssimokawa		crom_next(&cc);
598103285Sikob	}
599121185Ssimokawa
600121185Ssimokawa	for (lun = 0; lun < target->num_lun; lun ++) {
601121185Ssimokawa		sdev = target->luns[lun];
602121185Ssimokawa		if (sdev != NULL && (sdev->flags & VALID_LUN) == 0) {
603121185Ssimokawa			sbp_cam_detach_sdev(sdev);
604121185Ssimokawa			sbp_free_sdev(sdev);
605121185Ssimokawa			target->luns[lun] = NULL;
606121185Ssimokawa		}
607121185Ssimokawa	}
608121185Ssimokawa}
609121185Ssimokawa
610121185Ssimokawastatic struct sbp_target *
611121185Ssimokawasbp_alloc_target(struct sbp_softc *sbp, struct fw_device *fwdev)
612121185Ssimokawa{
613121185Ssimokawa	int i;
614121185Ssimokawa	struct sbp_target *target;
615121185Ssimokawa	struct crom_context cc;
616121185Ssimokawa	struct csrreg *reg;
617121185Ssimokawa
618121185SsimokawaSBP_DEBUG(1)
619121185Ssimokawa	printf("sbp_alloc_target\n");
620121185SsimokawaEND_DEBUG
621121185Ssimokawa	i = sbp_new_target(sbp, fwdev);
622121185Ssimokawa	if (i < 0) {
623121185Ssimokawa		device_printf(sbp->fd.dev, "increase SBP_NUM_TARGETS!\n");
624121185Ssimokawa		return NULL;
625121185Ssimokawa	}
626121185Ssimokawa	/* new target */
627121185Ssimokawa	target = &sbp->targets[i];
628121185Ssimokawa	target->sbp = sbp;
629121185Ssimokawa	target->fwdev = fwdev;
630121185Ssimokawa	target->target_id = i;
631121185Ssimokawa	/* XXX we may want to reload mgm port after each bus reset */
632121185Ssimokawa	/* XXX there might be multiple management agents */
633121185Ssimokawa	crom_init_context(&cc, target->fwdev->csrrom);
634121185Ssimokawa	reg = crom_search_key(&cc, CROM_MGM);
635121185Ssimokawa	if (reg == NULL || reg->val == 0) {
636121185Ssimokawa		printf("NULL management address\n");
637121185Ssimokawa		target->fwdev = NULL;
638121185Ssimokawa		return NULL;
639121185Ssimokawa	}
640121185Ssimokawa	target->mgm_hi = 0xffff;
641121185Ssimokawa	target->mgm_lo = 0xf0000000 | (reg->val << 2);
642121185Ssimokawa	target->mgm_ocb_cur = NULL;
643121185SsimokawaSBP_DEBUG(1)
644121185Ssimokawa	printf("target:%d mgm_port: %x\n", i, target->mgm_lo);
645121185SsimokawaEND_DEBUG
646121185Ssimokawa	STAILQ_INIT(&target->xferlist);
647121185Ssimokawa	target->n_xfer = 0;
648121185Ssimokawa	STAILQ_INIT(&target->mgm_ocb_queue);
649121185Ssimokawa	CALLOUT_INIT(&target->mgm_ocb_timeout);
650121185Ssimokawa	CALLOUT_INIT(&target->scan_callout);
651121185Ssimokawa
652121185Ssimokawa	target->luns = NULL;
653121185Ssimokawa	target->num_lun = 0;
654114069Ssimokawa	return target;
655103285Sikob}
656103285Sikob
657103285Sikobstatic void
658103285Sikobsbp_probe_lun(struct sbp_dev *sdev)
659103285Sikob{
660103285Sikob	struct fw_device *fwdev;
661114069Ssimokawa	struct crom_context c, *cc = &c;
662114069Ssimokawa	struct csrreg *reg;
663103285Sikob
664103285Sikob	bzero(sdev->vendor, sizeof(sdev->vendor));
665103285Sikob	bzero(sdev->product, sizeof(sdev->product));
666114069Ssimokawa
667114069Ssimokawa	fwdev = sdev->target->fwdev;
668114069Ssimokawa	crom_init_context(cc, fwdev->csrrom);
669114069Ssimokawa	/* get vendor string */
670114069Ssimokawa	crom_search_key(cc, CSRKEY_VENDOR);
671114069Ssimokawa	crom_next(cc);
672114069Ssimokawa	crom_parse_text(cc, sdev->vendor, sizeof(sdev->vendor));
673114223Ssimokawa	/* skip to the unit directory for SBP-2 */
674114260Ssimokawa	while ((reg = crom_search_key(cc, CSRKEY_VER)) != NULL) {
675114223Ssimokawa		if (reg->val == CSRVAL_T10SBP2)
676114223Ssimokawa			break;
677114260Ssimokawa		crom_next(cc);
678114260Ssimokawa	}
679114069Ssimokawa	/* get firmware revision */
680114069Ssimokawa	reg = crom_search_key(cc, CSRKEY_FIRM_VER);
681114069Ssimokawa	if (reg != NULL)
682114069Ssimokawa		snprintf(sdev->revision, sizeof(sdev->revision),
683114069Ssimokawa						"%06x", reg->val);
684114069Ssimokawa	/* get product string */
685114069Ssimokawa	crom_search_key(cc, CSRKEY_MODEL);
686114069Ssimokawa	crom_next(cc);
687114069Ssimokawa	crom_parse_text(cc, sdev->product, sizeof(sdev->product));
688103285Sikob}
689111615Ssimokawa
690103285Sikobstatic void
691111615Ssimokawasbp_login_callout(void *arg)
692103285Sikob{
693111615Ssimokawa	struct sbp_dev *sdev = (struct sbp_dev *)arg;
694111615Ssimokawa	sbp_mgm_orb(sdev, ORB_FUN_LGI, NULL);
695111615Ssimokawa}
696111615Ssimokawa
697122387Ssimokawastatic void
698122387Ssimokawasbp_login(struct sbp_dev *sdev)
699122387Ssimokawa{
700122387Ssimokawa	struct timeval delta;
701122387Ssimokawa	struct timeval t;
702122387Ssimokawa	int ticks = 0;
703122387Ssimokawa
704122387Ssimokawa	microtime(&delta);
705122387Ssimokawa	timevalsub(&delta, &sdev->target->sbp->last_busreset);
706122387Ssimokawa	t.tv_sec = login_delay / 1000;
707122387Ssimokawa	t.tv_usec = (login_delay % 1000) * 1000;
708122387Ssimokawa	timevalsub(&t, &delta);
709122387Ssimokawa	if (t.tv_sec >= 0 && t.tv_usec > 0)
710122387Ssimokawa		ticks = (t.tv_sec * 1000 + t.tv_usec / 1000) * hz / 1000;
711122387SsimokawaSBP_DEBUG(0)
712153706Strhodes	printf("%s: sec = %jd usec = %ld ticks = %d\n", __func__,
713153706Strhodes	    (intmax_t)t.tv_sec, t.tv_usec, ticks);
714122387SsimokawaEND_DEBUG
715122387Ssimokawa	callout_reset(&sdev->login_callout, ticks,
716122387Ssimokawa			sbp_login_callout, (void *)(sdev));
717122387Ssimokawa}
718122387Ssimokawa
719114069Ssimokawa#define SBP_FWDEV_ALIVE(fwdev) (((fwdev)->status == FWDEVATTACHED) \
720114069Ssimokawa	&& crom_has_specver((fwdev)->csrrom, CSRVAL_ANSIT10, CSRVAL_T10SBP2))
721111615Ssimokawa
722111615Ssimokawastatic void
723111615Ssimokawasbp_probe_target(void *arg)
724111615Ssimokawa{
725111615Ssimokawa	struct sbp_target *target = (struct sbp_target *)arg;
726188756Ssbruno	struct sbp_softc *sbp = target->sbp;
727103285Sikob	struct sbp_dev *sdev;
728111615Ssimokawa	int i, alive;
729103285Sikob
730111615Ssimokawa	alive = SBP_FWDEV_ALIVE(target->fwdev);
731103285SikobSBP_DEBUG(1)
732188756Ssbruno	device_printf(sbp->fd.dev, "%s %d%salive\n",
733188756Ssbruno		 __func__, target->target_id,
734188756Ssbruno		(!alive) ? " not " : "");
735103285SikobEND_DEBUG
736103285Sikob
737103285Sikob	sbp = target->sbp;
738121185Ssimokawa	sbp_alloc_lun(target);
739121185Ssimokawa
740111615Ssimokawa	/* XXX untimeout mgm_ocb and dequeue */
741103285Sikob	for (i=0; i < target->num_lun; i++) {
742121185Ssimokawa		sdev = target->luns[i];
743121185Ssimokawa		if (sdev == NULL)
744121185Ssimokawa			continue;
745103285Sikob		if (alive && (sdev->status != SBP_DEV_DEAD)) {
746103285Sikob			if (sdev->path != NULL) {
747170374Ssimokawa				SBP_LOCK(sbp);
748103285Sikob				xpt_freeze_devq(sdev->path, 1);
749111615Ssimokawa				sdev->freeze ++;
750170374Ssimokawa				SBP_UNLOCK(sbp);
751103285Sikob			}
752111615Ssimokawa			sbp_probe_lun(sdev);
753188756Ssbruno			sbp_show_sdev_info(sdev);
754111615Ssimokawa
755111615Ssimokawa			sbp_abort_all_ocbs(sdev, CAM_SCSI_BUS_RESET);
756103285Sikob			switch (sdev->status) {
757110336Ssimokawa			case SBP_DEV_RESET:
758103285Sikob				/* new or revived target */
759113584Ssimokawa				if (auto_login)
760111615Ssimokawa					sbp_login(sdev);
761103285Sikob				break;
762111615Ssimokawa			case SBP_DEV_TOATTACH:
763111615Ssimokawa			case SBP_DEV_PROBE:
764111615Ssimokawa			case SBP_DEV_ATTACHED:
765110336Ssimokawa			case SBP_DEV_RETRY:
766110336Ssimokawa			default:
767111615Ssimokawa				sbp_mgm_orb(sdev, ORB_FUN_RCN, NULL);
768110336Ssimokawa				break;
769103285Sikob			}
770103285Sikob		} else {
771103285Sikob			switch (sdev->status) {
772103285Sikob			case SBP_DEV_ATTACHED:
773103285SikobSBP_DEBUG(0)
774103285Sikob				/* the device has gone */
775188756Ssbruno				device_printf(sbp->fd.dev, "%s: lost target\n",
776188756Ssbruno					__func__);
777103285SikobEND_DEBUG
778111615Ssimokawa				if (sdev->path) {
779170374Ssimokawa					SBP_LOCK(sbp);
780103285Sikob					xpt_freeze_devq(sdev->path, 1);
781111615Ssimokawa					sdev->freeze ++;
782170374Ssimokawa					SBP_UNLOCK(sbp);
783111615Ssimokawa				}
784103285Sikob				sdev->status = SBP_DEV_RETRY;
785187993Ssbruno				sbp_cam_detach_sdev(sdev);
786187993Ssbruno				sbp_free_sdev(sdev);
787187993Ssbruno				target->luns[i] = NULL;
788103285Sikob				break;
789103285Sikob			case SBP_DEV_PROBE:
790103285Sikob			case SBP_DEV_TOATTACH:
791103285Sikob				sdev->status = SBP_DEV_RESET;
792103285Sikob				break;
793103285Sikob			case SBP_DEV_RETRY:
794103285Sikob			case SBP_DEV_RESET:
795103285Sikob			case SBP_DEV_DEAD:
796103285Sikob				break;
797103285Sikob			}
798103285Sikob		}
799103285Sikob	}
800103285Sikob}
801103285Sikob
802103285Sikobstatic void
803113584Ssimokawasbp_post_busreset(void *arg)
804113584Ssimokawa{
805113584Ssimokawa	struct sbp_softc *sbp;
806113584Ssimokawa
807113584Ssimokawa	sbp = (struct sbp_softc *)arg;
808113584SsimokawaSBP_DEBUG(0)
809113584Ssimokawa	printf("sbp_post_busreset\n");
810113584SsimokawaEND_DEBUG
811124145Ssimokawa	if ((sbp->sim->flags & SIMQ_FREEZED) == 0) {
812170374Ssimokawa		SBP_LOCK(sbp);
813124145Ssimokawa		xpt_freeze_simq(sbp->sim, /*count*/1);
814124145Ssimokawa		sbp->sim->flags |= SIMQ_FREEZED;
815170374Ssimokawa		SBP_UNLOCK(sbp);
816124145Ssimokawa	}
817122387Ssimokawa	microtime(&sbp->last_busreset);
818113584Ssimokawa}
819113584Ssimokawa
820113584Ssimokawastatic void
821103285Sikobsbp_post_explore(void *arg)
822103285Sikob{
823103285Sikob	struct sbp_softc *sbp = (struct sbp_softc *)arg;
824103285Sikob	struct sbp_target *target;
825103285Sikob	struct fw_device *fwdev;
826103285Sikob	int i, alive;
827103285Sikob
828111199SsimokawaSBP_DEBUG(0)
829111199Ssimokawa	printf("sbp_post_explore (sbp_cold=%d)\n", sbp_cold);
830103285SikobEND_DEBUG
831170400Ssimokawa	/* We need physical access */
832170400Ssimokawa	if (!firewire_phydma_enable)
833170400Ssimokawa		return;
834170400Ssimokawa
835122387Ssimokawa	if (sbp_cold > 0)
836122387Ssimokawa		sbp_cold --;
837122387Ssimokawa
838122387Ssimokawa#if 0
839122387Ssimokawa	/*
840122387Ssimokawa	 * XXX don't let CAM the bus rest.
841122387Ssimokawa	 * CAM tries to do something with freezed (DEV_RETRY) devices.
842111615Ssimokawa	 */
843111615Ssimokawa	xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
844103285Sikob#endif
845111615Ssimokawa
846130532Sdfr	/* Garbage Collection */
847103285Sikob	for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
848103285Sikob		target = &sbp->targets[i];
849110193Ssimokawa		STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link)
850110193Ssimokawa			if (target->fwdev == NULL || target->fwdev == fwdev)
851110193Ssimokawa				break;
852121185Ssimokawa		if (fwdev == NULL) {
853103285Sikob			/* device has removed in lower driver */
854110145Ssimokawa			sbp_cam_detach_target(target);
855121185Ssimokawa			sbp_free_target(target);
856103285Sikob		}
857103285Sikob	}
858103285Sikob	/* traverse device list */
859110193Ssimokawa	STAILQ_FOREACH(fwdev, &sbp->fd.fc->devices, link) {
860103285SikobSBP_DEBUG(0)
861188756Ssbruno		device_printf(sbp->fd.dev,"%s:: EUI:%08x%08x %s attached, state=%d\n",
862188756Ssbruno				__func__, fwdev->eui.hi, fwdev->eui.lo,
863188756Ssbruno				(fwdev->status != FWDEVATTACHED) ? "not" : "",
864188756Ssbruno				fwdev->status);
865103285SikobEND_DEBUG
866111615Ssimokawa		alive = SBP_FWDEV_ALIVE(fwdev);
867103285Sikob		for(i = 0 ; i < SBP_NUM_TARGETS ; i ++){
868103285Sikob			target = &sbp->targets[i];
869103285Sikob			if(target->fwdev == fwdev ) {
870103285Sikob				/* known target */
871103285Sikob				break;
872103285Sikob			}
873103285Sikob		}
874103285Sikob		if(i == SBP_NUM_TARGETS){
875103285Sikob			if (alive) {
876103285Sikob				/* new target */
877103285Sikob				target = sbp_alloc_target(sbp, fwdev);
878103285Sikob				if (target == NULL)
879103285Sikob					continue;
880103285Sikob			} else {
881103285Sikob				continue;
882103285Sikob			}
883103285Sikob		}
884111615Ssimokawa		sbp_probe_target((void *)target);
885121185Ssimokawa		if (target->num_lun == 0)
886121185Ssimokawa			sbp_free_target(target);
887103285Sikob	}
888170374Ssimokawa	SBP_LOCK(sbp);
889124145Ssimokawa	xpt_release_simq(sbp->sim, /*run queue*/TRUE);
890124145Ssimokawa	sbp->sim->flags &= ~SIMQ_FREEZED;
891170374Ssimokawa	SBP_UNLOCK(sbp);
892103285Sikob}
893103285Sikob
894103285Sikob#if NEED_RESPONSE
895103285Sikobstatic void
896103285Sikobsbp_loginres_callback(struct fw_xfer *xfer){
897113584Ssimokawa	int s;
898103285Sikob	struct sbp_dev *sdev;
899103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
900113584SsimokawaSBP_DEBUG(1)
901188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,"%s\n", __func__);
902103285SikobEND_DEBUG
903113584Ssimokawa	/* recycle */
904113584Ssimokawa	s = splfw();
905113584Ssimokawa	STAILQ_INSERT_TAIL(&sdev->target->sbp->fwb.xferlist, xfer, link);
906113584Ssimokawa	splx(s);
907103285Sikob	return;
908103285Sikob}
909103285Sikob#endif
910103285Sikob
911113584Ssimokawastatic __inline void
912113584Ssimokawasbp_xfer_free(struct fw_xfer *xfer)
913113584Ssimokawa{
914113584Ssimokawa	struct sbp_dev *sdev;
915113584Ssimokawa	int s;
916113584Ssimokawa
917113584Ssimokawa	sdev = (struct sbp_dev *)xfer->sc;
918113584Ssimokawa	fw_xfer_unload(xfer);
919113584Ssimokawa	s = splfw();
920170374Ssimokawa	SBP_LOCK(sdev->target->sbp);
921113584Ssimokawa	STAILQ_INSERT_TAIL(&sdev->target->xferlist, xfer, link);
922170374Ssimokawa	SBP_UNLOCK(sdev->target->sbp);
923113584Ssimokawa	splx(s);
924113584Ssimokawa}
925113584Ssimokawa
926103285Sikobstatic void
927114732Ssimokawasbp_reset_start_callback(struct fw_xfer *xfer)
928103285Sikob{
929114732Ssimokawa	struct sbp_dev *tsdev, *sdev = (struct sbp_dev *)xfer->sc;
930114732Ssimokawa	struct sbp_target *target = sdev->target;
931114732Ssimokawa	int i;
932114732Ssimokawa
933114732Ssimokawa	if (xfer->resp != 0) {
934188756Ssbruno		device_printf(sdev->target->sbp->fd.dev,
935188756Ssbruno			"%s: %s failed: resp=%d\n", __func__, sdev->bustgtlun, xfer->resp);
936114732Ssimokawa	}
937114732Ssimokawa
938114732Ssimokawa	for (i = 0; i < target->num_lun; i++) {
939121185Ssimokawa		tsdev = target->luns[i];
940121185Ssimokawa		if (tsdev != NULL && tsdev->status == SBP_DEV_LOGIN)
941121185Ssimokawa			sbp_login(tsdev);
942114732Ssimokawa	}
943114732Ssimokawa}
944114732Ssimokawa
945114732Ssimokawastatic void
946114732Ssimokawasbp_reset_start(struct sbp_dev *sdev)
947114732Ssimokawa{
948114732Ssimokawa	struct fw_xfer *xfer;
949114732Ssimokawa	struct fw_pkt *fp;
950114732Ssimokawa
951114732SsimokawaSBP_DEBUG(0)
952188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
953188756Ssbruno			"%s:%s\n", __func__,sdev->bustgtlun);
954114732SsimokawaEND_DEBUG
955114732Ssimokawa
956114732Ssimokawa	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
957167632Ssimokawa	xfer->hand = sbp_reset_start_callback;
958120660Ssimokawa	fp = &xfer->send.hdr;
959114732Ssimokawa	fp->mode.wreqq.dest_hi = 0xffff;
960114732Ssimokawa	fp->mode.wreqq.dest_lo = 0xf0000000 | RESET_START;
961114732Ssimokawa	fp->mode.wreqq.data = htonl(0xf);
962114732Ssimokawa	fw_asyreq(xfer->fc, -1, xfer);
963114732Ssimokawa}
964114732Ssimokawa
965114732Ssimokawastatic void
966114732Ssimokawasbp_mgm_callback(struct fw_xfer *xfer)
967114732Ssimokawa{
968103285Sikob	struct sbp_dev *sdev;
969114732Ssimokawa	int resp;
970114732Ssimokawa
971103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
972114732Ssimokawa
973114732SsimokawaSBP_DEBUG(1)
974188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
975188756Ssbruno		"%s:%s\n", __func__, sdev->bustgtlun);
976103285SikobEND_DEBUG
977114732Ssimokawa	resp = xfer->resp;
978113584Ssimokawa	sbp_xfer_free(xfer);
979103285Sikob	return;
980103285Sikob}
981103285Sikob
982111615Ssimokawastatic struct sbp_dev *
983111615Ssimokawasbp_next_dev(struct sbp_target *target, int lun)
984111615Ssimokawa{
985121185Ssimokawa	struct sbp_dev **sdevp;
986111615Ssimokawa	int i;
987111615Ssimokawa
988121185Ssimokawa	for (i = lun, sdevp = &target->luns[lun]; i < target->num_lun;
989121185Ssimokawa	    i++, sdevp++)
990121185Ssimokawa		if (*sdevp != NULL && (*sdevp)->status == SBP_DEV_PROBE)
991121185Ssimokawa			return(*sdevp);
992121185Ssimokawa	return(NULL);
993111615Ssimokawa}
994111615Ssimokawa
995111615Ssimokawa#define SCAN_PRI 1
996103285Sikobstatic void
997111615Ssimokawasbp_cam_scan_lun(struct cam_periph *periph, union ccb *ccb)
998103285Sikob{
999111615Ssimokawa	struct sbp_target *target;
1000103285Sikob	struct sbp_dev *sdev;
1001111615Ssimokawa
1002103285Sikob	sdev = (struct sbp_dev *) ccb->ccb_h.ccb_sdev_ptr;
1003111615Ssimokawa	target = sdev->target;
1004110269SsimokawaSBP_DEBUG(0)
1005188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
1006188756Ssbruno		"%s:%s\n", __func__, sdev->bustgtlun);
1007103285SikobEND_DEBUG
1008111615Ssimokawa	if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1009111615Ssimokawa		sdev->status = SBP_DEV_ATTACHED;
1010111615Ssimokawa	} else {
1011188756Ssbruno		device_printf(sdev->target->sbp->fd.dev,
1012188756Ssbruno			"%s:%s failed\n", __func__, sdev->bustgtlun);
1013111615Ssimokawa	}
1014111615Ssimokawa	sdev = sbp_next_dev(target, sdev->lun_id + 1);
1015111615Ssimokawa	if (sdev == NULL) {
1016111615Ssimokawa		free(ccb, M_SBP);
1017111615Ssimokawa		return;
1018111615Ssimokawa	}
1019111615Ssimokawa	/* reuse ccb */
1020111615Ssimokawa	xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
1021111615Ssimokawa	ccb->ccb_h.ccb_sdev_ptr = sdev;
1022111615Ssimokawa	xpt_action(ccb);
1023111615Ssimokawa	xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1024111615Ssimokawa	sdev->freeze = 1;
1025103285Sikob}
1026103285Sikob
1027103285Sikobstatic void
1028111615Ssimokawasbp_cam_scan_target(void *arg)
1029103285Sikob{
1030111615Ssimokawa	struct sbp_target *target = (struct sbp_target *)arg;
1031111615Ssimokawa	struct sbp_dev *sdev;
1032110798Ssimokawa	union ccb *ccb;
1033103285Sikob
1034111615Ssimokawa	sdev = sbp_next_dev(target, 0);
1035111615Ssimokawa	if (sdev == NULL) {
1036111615Ssimokawa		printf("sbp_cam_scan_target: nothing to do for target%d\n",
1037111615Ssimokawa							target->target_id);
1038110798Ssimokawa		return;
1039110798Ssimokawa	}
1040103285SikobSBP_DEBUG(0)
1041188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
1042188756Ssbruno		"%s:%s\n", __func__, sdev->bustgtlun);
1043103285SikobEND_DEBUG
1044111615Ssimokawa	ccb = malloc(sizeof(union ccb), M_SBP, M_NOWAIT | M_ZERO);
1045111615Ssimokawa	if (ccb == NULL) {
1046111615Ssimokawa		printf("sbp_cam_scan_target: malloc failed\n");
1047111615Ssimokawa		return;
1048111615Ssimokawa	}
1049111615Ssimokawa	xpt_setup_ccb(&ccb->ccb_h, sdev->path, SCAN_PRI);
1050103285Sikob	ccb->ccb_h.func_code = XPT_SCAN_LUN;
1051111615Ssimokawa	ccb->ccb_h.cbfcnp = sbp_cam_scan_lun;
1052111615Ssimokawa	ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1053103285Sikob	ccb->crcn.flags = CAM_FLAG_NONE;
1054103285Sikob	ccb->ccb_h.ccb_sdev_ptr = sdev;
1055103285Sikob
1056103285Sikob	/* The scan is in progress now. */
1057170374Ssimokawa	SBP_LOCK(target->sbp);
1058111040Ssimokawa	xpt_action(ccb);
1059111615Ssimokawa	xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1060111615Ssimokawa	sdev->freeze = 1;
1061170374Ssimokawa	SBP_UNLOCK(target->sbp);
1062103285Sikob}
1063103285Sikob
1064111615Ssimokawastatic __inline void
1065111615Ssimokawasbp_scan_dev(struct sbp_dev *sdev)
1066111615Ssimokawa{
1067111040Ssimokawa	sdev->status = SBP_DEV_PROBE;
1068122387Ssimokawa	callout_reset(&sdev->target->scan_callout, scan_delay * hz / 1000,
1069111615Ssimokawa			sbp_cam_scan_target, (void *)sdev->target);
1070103285Sikob}
1071103285Sikob
1072103285Sikobstatic void
1073103285Sikobsbp_do_attach(struct fw_xfer *xfer)
1074103285Sikob{
1075103285Sikob	struct sbp_dev *sdev;
1076111615Ssimokawa	struct sbp_target *target;
1077111615Ssimokawa	struct sbp_softc *sbp;
1078103285Sikob
1079103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
1080111615Ssimokawa	target = sdev->target;
1081111615Ssimokawa	sbp = target->sbp;
1082103285SikobSBP_DEBUG(0)
1083188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
1084188756Ssbruno		"%s:%s\n", __func__, sdev->bustgtlun);
1085103285SikobEND_DEBUG
1086113584Ssimokawa	sbp_xfer_free(xfer);
1087111199Ssimokawa
1088103285Sikob	if (sdev->path == NULL)
1089103285Sikob		xpt_create_path(&sdev->path, xpt_periph,
1090111615Ssimokawa			cam_sim_path(target->sbp->sim),
1091111615Ssimokawa			target->target_id, sdev->lun_id);
1092103285Sikob
1093111199Ssimokawa	/*
1094111199Ssimokawa	 * Let CAM scan the bus if we are in the boot process.
1095111199Ssimokawa	 * XXX xpt_scan_bus cannot detect LUN larger than 0
1096111199Ssimokawa	 * if LUN 0 doesn't exists.
1097111199Ssimokawa	 */
1098111199Ssimokawa	if (sbp_cold > 0) {
1099111615Ssimokawa		sdev->status = SBP_DEV_ATTACHED;
1100111199Ssimokawa		return;
1101111199Ssimokawa	}
1102111199Ssimokawa
1103111615Ssimokawa	sbp_scan_dev(sdev);
1104103285Sikob	return;
1105103285Sikob}
1106103285Sikob
1107103285Sikobstatic void
1108103285Sikobsbp_agent_reset_callback(struct fw_xfer *xfer)
1109103285Sikob{
1110103285Sikob	struct sbp_dev *sdev;
1111103285Sikob
1112103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
1113103285SikobSBP_DEBUG(1)
1114188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
1115188756Ssbruno			"%s:%s\n", __func__, sdev->bustgtlun);
1116103285SikobEND_DEBUG
1117114732Ssimokawa	if (xfer->resp != 0) {
1118188756Ssbruno		device_printf(sdev->target->sbp->fd.dev,
1119188756Ssbruno			"%s:%s resp=%d\n", __func__, sdev->bustgtlun, xfer->resp);
1120114732Ssimokawa	}
1121114732Ssimokawa
1122113584Ssimokawa	sbp_xfer_free(xfer);
1123111615Ssimokawa	if (sdev->path) {
1124170374Ssimokawa		SBP_LOCK(sdev->target->sbp);
1125111615Ssimokawa		xpt_release_devq(sdev->path, sdev->freeze, TRUE);
1126111615Ssimokawa		sdev->freeze = 0;
1127170374Ssimokawa		SBP_UNLOCK(sdev->target->sbp);
1128111615Ssimokawa	}
1129103285Sikob}
1130103285Sikob
1131103285Sikobstatic void
1132110336Ssimokawasbp_agent_reset(struct sbp_dev *sdev)
1133103285Sikob{
1134103285Sikob	struct fw_xfer *xfer;
1135103285Sikob	struct fw_pkt *fp;
1136103285Sikob
1137103285SikobSBP_DEBUG(0)
1138188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
1139188756Ssbruno		"%s:%s\n", __func__, sdev->bustgtlun);
1140103285SikobEND_DEBUG
1141103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0x04);
1142103285Sikob	if (xfer == NULL)
1143103285Sikob		return;
1144115788Ssimokawa	if (sdev->status == SBP_DEV_ATTACHED || sdev->status == SBP_DEV_PROBE)
1145167632Ssimokawa		xfer->hand = sbp_agent_reset_callback;
1146110336Ssimokawa	else
1147167632Ssimokawa		xfer->hand = sbp_do_attach;
1148120660Ssimokawa	fp = &xfer->send.hdr;
1149103285Sikob	fp->mode.wreqq.data = htonl(0xf);
1150103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1151111615Ssimokawa	sbp_abort_all_ocbs(sdev, CAM_BDR_SENT);
1152103285Sikob}
1153103285Sikob
1154103285Sikobstatic void
1155103285Sikobsbp_busy_timeout_callback(struct fw_xfer *xfer)
1156103285Sikob{
1157103285Sikob	struct sbp_dev *sdev;
1158103285Sikob
1159103285Sikob	sdev = (struct sbp_dev *)xfer->sc;
1160103285SikobSBP_DEBUG(1)
1161188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
1162188756Ssbruno		"%s:%s\n", __func__, sdev->bustgtlun);
1163103285SikobEND_DEBUG
1164113584Ssimokawa	sbp_xfer_free(xfer);
1165110336Ssimokawa	sbp_agent_reset(sdev);
1166103285Sikob}
1167103285Sikob
1168103285Sikobstatic void
1169103285Sikobsbp_busy_timeout(struct sbp_dev *sdev)
1170103285Sikob{
1171103285Sikob	struct fw_pkt *fp;
1172103285Sikob	struct fw_xfer *xfer;
1173103285SikobSBP_DEBUG(0)
1174188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
1175188756Ssbruno		"%s:%s\n", __func__, sdev->bustgtlun);
1176103285SikobEND_DEBUG
1177103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQQ, 0);
1178103285Sikob
1179167632Ssimokawa	xfer->hand = sbp_busy_timeout_callback;
1180120660Ssimokawa	fp = &xfer->send.hdr;
1181113584Ssimokawa	fp->mode.wreqq.dest_hi = 0xffff;
1182113584Ssimokawa	fp->mode.wreqq.dest_lo = 0xf0000000 | BUSY_TIMEOUT;
1183110129Ssimokawa	fp->mode.wreqq.data = htonl((1 << (13+12)) | 0xf);
1184103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1185103285Sikob}
1186103285Sikob
1187103285Sikobstatic void
1188121185Ssimokawasbp_orb_pointer_callback(struct fw_xfer *xfer)
1189121185Ssimokawa{
1190121185Ssimokawa	struct sbp_dev *sdev;
1191121185Ssimokawa	sdev = (struct sbp_dev *)xfer->sc;
1192121185Ssimokawa
1193170807SsimokawaSBP_DEBUG(2)
1194188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
1195188756Ssbruno		"%s:%s\n", __func__, sdev->bustgtlun);
1196121185SsimokawaEND_DEBUG
1197121185Ssimokawa	if (xfer->resp != 0) {
1198121185Ssimokawa		/* XXX */
1199127468Ssimokawa		printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1200121185Ssimokawa	}
1201121185Ssimokawa	sbp_xfer_free(xfer);
1202170807Ssimokawa
1203170807Ssimokawa	SBP_LOCK(sdev->target->sbp);
1204121185Ssimokawa	sdev->flags &= ~ORB_POINTER_ACTIVE;
1205121185Ssimokawa
1206121185Ssimokawa	if ((sdev->flags & ORB_POINTER_NEED) != 0) {
1207121185Ssimokawa		struct sbp_ocb *ocb;
1208121185Ssimokawa
1209121185Ssimokawa		sdev->flags &= ~ORB_POINTER_NEED;
1210121185Ssimokawa		ocb = STAILQ_FIRST(&sdev->ocbs);
1211121185Ssimokawa		if (ocb != NULL)
1212121185Ssimokawa			sbp_orb_pointer(sdev, ocb);
1213121185Ssimokawa	}
1214170807Ssimokawa	SBP_UNLOCK(sdev->target->sbp);
1215121185Ssimokawa	return;
1216121185Ssimokawa}
1217121185Ssimokawa
1218121185Ssimokawastatic void
1219103285Sikobsbp_orb_pointer(struct sbp_dev *sdev, struct sbp_ocb *ocb)
1220103285Sikob{
1221103285Sikob	struct fw_xfer *xfer;
1222103285Sikob	struct fw_pkt *fp;
1223121185SsimokawaSBP_DEBUG(1)
1224188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
1225188756Ssbruno		"%s:%s 0x%08x\n",
1226188756Ssbruno		__func__, sdev->bustgtlun,
1227188756Ssbruno		(uint32_t)ocb->bus_addr);
1228103285SikobEND_DEBUG
1229103285Sikob
1230170807Ssimokawa	mtx_assert(&sdev->target->sbp->mtx, MA_OWNED);
1231170807Ssimokawa
1232121185Ssimokawa	if ((sdev->flags & ORB_POINTER_ACTIVE) != 0) {
1233122421SsimokawaSBP_DEBUG(0)
1234127468Ssimokawa		printf("%s: orb pointer active\n", __func__);
1235122421SsimokawaEND_DEBUG
1236121185Ssimokawa		sdev->flags |= ORB_POINTER_NEED;
1237121185Ssimokawa		return;
1238121185Ssimokawa	}
1239121185Ssimokawa
1240121185Ssimokawa	sdev->flags |= ORB_POINTER_ACTIVE;
1241170807Ssimokawa	xfer = sbp_write_cmd_locked(sdev, FWTCODE_WREQB, 0x08);
1242103285Sikob	if (xfer == NULL)
1243103285Sikob		return;
1244167632Ssimokawa	xfer->hand = sbp_orb_pointer_callback;
1245103285Sikob
1246120660Ssimokawa	fp = &xfer->send.hdr;
1247113584Ssimokawa	fp->mode.wreqb.len = 8;
1248103285Sikob	fp->mode.wreqb.extcode = 0;
1249120660Ssimokawa	xfer->send.payload[0] =
1250103285Sikob		htonl(((sdev->target->sbp->fd.fc->nodeid | FWLOCALBUS )<< 16));
1251129585Sdfr	xfer->send.payload[1] = htonl((uint32_t)ocb->bus_addr);
1252103285Sikob
1253187993Ssbruno	/*
1254187993Ssbruno	 * sbp_xfer_free() will attempt to acquire
1255187993Ssbruno	 * the SBP lock on entrance.  Also, this removes
1256187993Ssbruno	 * a LOR between the firewire layer and sbp
1257187993Ssbruno	 */
1258187993Ssbruno	SBP_UNLOCK(sdev->target->sbp);
1259103285Sikob	if(fw_asyreq(xfer->fc, -1, xfer) != 0){
1260113584Ssimokawa			sbp_xfer_free(xfer);
1261103285Sikob			ocb->ccb->ccb_h.status = CAM_REQ_INVALID;
1262103285Sikob			xpt_done(ocb->ccb);
1263103285Sikob	}
1264187993Ssbruno	SBP_LOCK(sdev->target->sbp);
1265103285Sikob}
1266103285Sikob
1267103285Sikobstatic void
1268127468Ssimokawasbp_doorbell_callback(struct fw_xfer *xfer)
1269121185Ssimokawa{
1270121185Ssimokawa	struct sbp_dev *sdev;
1271121185Ssimokawa	sdev = (struct sbp_dev *)xfer->sc;
1272127468Ssimokawa
1273127468SsimokawaSBP_DEBUG(1)
1274188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
1275188756Ssbruno		"%s:%s\n", __func__, sdev->bustgtlun);
1276121185SsimokawaEND_DEBUG
1277121185Ssimokawa	if (xfer->resp != 0) {
1278121185Ssimokawa		/* XXX */
1279188756Ssbruno		device_printf(sdev->target->sbp->fd.dev,
1280188756Ssbruno			"%s: xfer->resp = %d\n", __func__, xfer->resp);
1281121185Ssimokawa	}
1282121185Ssimokawa	sbp_xfer_free(xfer);
1283127468Ssimokawa	sdev->flags &= ~ORB_DOORBELL_ACTIVE;
1284127468Ssimokawa	if ((sdev->flags & ORB_DOORBELL_NEED) != 0) {
1285127468Ssimokawa		sdev->flags &= ~ORB_DOORBELL_NEED;
1286170807Ssimokawa		SBP_LOCK(sdev->target->sbp);
1287127468Ssimokawa		sbp_doorbell(sdev);
1288170807Ssimokawa		SBP_UNLOCK(sdev->target->sbp);
1289127468Ssimokawa	}
1290121185Ssimokawa	return;
1291121185Ssimokawa}
1292121185Ssimokawa
1293121185Ssimokawastatic void
1294103285Sikobsbp_doorbell(struct sbp_dev *sdev)
1295103285Sikob{
1296103285Sikob	struct fw_xfer *xfer;
1297103285Sikob	struct fw_pkt *fp;
1298103285SikobSBP_DEBUG(1)
1299188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
1300188756Ssbruno		"%s:%s\n", __func__, sdev->bustgtlun);
1301103285SikobEND_DEBUG
1302103285Sikob
1303127468Ssimokawa	if ((sdev->flags & ORB_DOORBELL_ACTIVE) != 0) {
1304127468Ssimokawa		sdev->flags |= ORB_DOORBELL_NEED;
1305127468Ssimokawa		return;
1306127468Ssimokawa	}
1307127468Ssimokawa	sdev->flags |= ORB_DOORBELL_ACTIVE;
1308170807Ssimokawa	xfer = sbp_write_cmd_locked(sdev, FWTCODE_WREQQ, 0x10);
1309103285Sikob	if (xfer == NULL)
1310103285Sikob		return;
1311167632Ssimokawa	xfer->hand = sbp_doorbell_callback;
1312127468Ssimokawa	fp = &xfer->send.hdr;
1313103285Sikob	fp->mode.wreqq.data = htonl(0xf);
1314103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1315103285Sikob}
1316103285Sikob
1317103285Sikobstatic struct fw_xfer *
1318170807Ssimokawasbp_write_cmd_locked(struct sbp_dev *sdev, int tcode, int offset)
1319103285Sikob{
1320103285Sikob	struct fw_xfer *xfer;
1321103285Sikob	struct fw_pkt *fp;
1322113584Ssimokawa	struct sbp_target *target;
1323113584Ssimokawa	int s, new = 0;
1324103285Sikob
1325170807Ssimokawa	mtx_assert(&sdev->target->sbp->mtx, MA_OWNED);
1326170807Ssimokawa
1327113584Ssimokawa	target = sdev->target;
1328113584Ssimokawa	s = splfw();
1329113584Ssimokawa	xfer = STAILQ_FIRST(&target->xferlist);
1330113584Ssimokawa	if (xfer == NULL) {
1331113584Ssimokawa		if (target->n_xfer > 5 /* XXX */) {
1332113584Ssimokawa			printf("sbp: no more xfer for this target\n");
1333113584Ssimokawa			splx(s);
1334113584Ssimokawa			return(NULL);
1335113584Ssimokawa		}
1336120660Ssimokawa		xfer = fw_xfer_alloc_buf(M_SBP, 8, 0);
1337113584Ssimokawa		if(xfer == NULL){
1338113584Ssimokawa			printf("sbp: fw_xfer_alloc_buf failed\n");
1339113584Ssimokawa			splx(s);
1340113584Ssimokawa			return NULL;
1341113584Ssimokawa		}
1342113584Ssimokawa		target->n_xfer ++;
1343113584Ssimokawa		if (debug)
1344113584Ssimokawa			printf("sbp: alloc %d xfer\n", target->n_xfer);
1345113584Ssimokawa		new = 1;
1346113584Ssimokawa	} else {
1347113584Ssimokawa		STAILQ_REMOVE_HEAD(&target->xferlist, link);
1348103285Sikob	}
1349113584Ssimokawa	splx(s);
1350113584Ssimokawa
1351113584Ssimokawa	if (new) {
1352120660Ssimokawa		xfer->recv.pay_len = 0;
1353120660Ssimokawa		xfer->send.spd = min(sdev->target->fwdev->speed, max_speed);
1354113584Ssimokawa		xfer->fc = sdev->target->sbp->fd.fc;
1355103285Sikob	}
1356120660Ssimokawa
1357120660Ssimokawa	if (tcode == FWTCODE_WREQB)
1358120660Ssimokawa		xfer->send.pay_len = 8;
1359120660Ssimokawa	else
1360120660Ssimokawa		xfer->send.pay_len = 0;
1361120660Ssimokawa
1362103285Sikob	xfer->sc = (caddr_t)sdev;
1363120660Ssimokawa	fp = &xfer->send.hdr;
1364113584Ssimokawa	fp->mode.wreqq.dest_hi = sdev->login->cmd_hi;
1365113584Ssimokawa	fp->mode.wreqq.dest_lo = sdev->login->cmd_lo + offset;
1366103285Sikob	fp->mode.wreqq.tlrt = 0;
1367103285Sikob	fp->mode.wreqq.tcode = tcode;
1368103285Sikob	fp->mode.wreqq.pri = 0;
1369120660Ssimokawa	fp->mode.wreqq.dst = FWLOCALBUS | sdev->target->fwdev->dst;
1370103285Sikob
1371103285Sikob	return xfer;
1372103285Sikob
1373103285Sikob}
1374103285Sikob
1375170807Ssimokawastatic struct fw_xfer *
1376170807Ssimokawasbp_write_cmd(struct sbp_dev *sdev, int tcode, int offset)
1377170807Ssimokawa{
1378170807Ssimokawa	struct sbp_softc *sbp = sdev->target->sbp;
1379170807Ssimokawa	struct fw_xfer *xfer;
1380170807Ssimokawa
1381170807Ssimokawa	SBP_LOCK(sbp);
1382170807Ssimokawa	xfer = sbp_write_cmd_locked(sdev, tcode, offset);
1383170807Ssimokawa	SBP_UNLOCK(sbp);
1384170807Ssimokawa
1385170807Ssimokawa	return (xfer);
1386170807Ssimokawa}
1387170807Ssimokawa
1388103285Sikobstatic void
1389111615Ssimokawasbp_mgm_orb(struct sbp_dev *sdev, int func, struct sbp_ocb *aocb)
1390103285Sikob{
1391103285Sikob	struct fw_xfer *xfer;
1392103285Sikob	struct fw_pkt *fp;
1393103285Sikob	struct sbp_ocb *ocb;
1394111615Ssimokawa	struct sbp_target *target;
1395103285Sikob	int s, nid;
1396103285Sikob
1397111615Ssimokawa	target = sdev->target;
1398111615Ssimokawa	nid = target->sbp->fd.fc->nodeid | FWLOCALBUS;
1399111615Ssimokawa
1400111615Ssimokawa	s = splfw();
1401170374Ssimokawa	SBP_LOCK(target->sbp);
1402111615Ssimokawa	if (func == ORB_FUN_RUNQUEUE) {
1403111615Ssimokawa		ocb = STAILQ_FIRST(&target->mgm_ocb_queue);
1404111615Ssimokawa		if (target->mgm_ocb_cur != NULL || ocb == NULL) {
1405170374Ssimokawa			SBP_UNLOCK(target->sbp);
1406111615Ssimokawa			splx(s);
1407111615Ssimokawa			return;
1408111615Ssimokawa		}
1409111615Ssimokawa		STAILQ_REMOVE_HEAD(&target->mgm_ocb_queue, ocb);
1410170374Ssimokawa		SBP_UNLOCK(target->sbp);
1411111615Ssimokawa		goto start;
1412111615Ssimokawa	}
1413113584Ssimokawa	if ((ocb = sbp_get_ocb(sdev)) == NULL) {
1414170374Ssimokawa		SBP_UNLOCK(target->sbp);
1415103285Sikob		splx(s);
1416122528Ssimokawa		/* XXX */
1417103285Sikob		return;
1418103285Sikob	}
1419170374Ssimokawa	SBP_UNLOCK(target->sbp);
1420103285Sikob	ocb->flags = OCB_ACT_MGM;
1421103285Sikob	ocb->sdev = sdev;
1422103285Sikob
1423120660Ssimokawa	bzero((void *)ocb->orb, sizeof(ocb->orb));
1424103285Sikob	ocb->orb[6] = htonl((nid << 16) | SBP_BIND_HI);
1425120660Ssimokawa	ocb->orb[7] = htonl(SBP_DEV2ADDR(target->target_id, sdev->lun_id));
1426103285Sikob
1427107653SsimokawaSBP_DEBUG(0)
1428188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
1429188756Ssbruno		 "%s:%s %s\n",
1430188756Ssbruno		 __func__,sdev->bustgtlun,
1431188756Ssbruno		 orb_fun_name[(func>>16)&0xf]);
1432107653SsimokawaEND_DEBUG
1433103285Sikob	switch (func) {
1434103285Sikob	case ORB_FUN_LGI:
1435120660Ssimokawa		ocb->orb[0] = ocb->orb[1] = 0; /* password */
1436103285Sikob		ocb->orb[2] = htonl(nid << 16);
1437113584Ssimokawa		ocb->orb[3] = htonl(sdev->dma.bus_addr);
1438121792Ssimokawa		ocb->orb[4] = htonl(ORB_NOTIFY | sdev->lun_id);
1439122387Ssimokawa		if (ex_login)
1440121792Ssimokawa			ocb->orb[4] |= htonl(ORB_EXV);
1441113584Ssimokawa		ocb->orb[5] = htonl(SBP_LOGIN_SIZE);
1442113584Ssimokawa		fwdma_sync(&sdev->dma, BUS_DMASYNC_PREREAD);
1443103285Sikob		break;
1444110336Ssimokawa	case ORB_FUN_ATA:
1445111615Ssimokawa		ocb->orb[0] = htonl((0 << 16) | 0);
1446113584Ssimokawa		ocb->orb[1] = htonl(aocb->bus_addr & 0xffffffff);
1447110336Ssimokawa		/* fall through */
1448103285Sikob	case ORB_FUN_RCN:
1449103285Sikob	case ORB_FUN_LGO:
1450103285Sikob	case ORB_FUN_LUR:
1451103285Sikob	case ORB_FUN_RST:
1452103285Sikob	case ORB_FUN_ATS:
1453113584Ssimokawa		ocb->orb[4] = htonl(ORB_NOTIFY | func | sdev->login->id);
1454103285Sikob		break;
1455103285Sikob	}
1456103285Sikob
1457111615Ssimokawa	if (target->mgm_ocb_cur != NULL) {
1458111615Ssimokawa		/* there is a standing ORB */
1459170374Ssimokawa		SBP_LOCK(target->sbp);
1460111615Ssimokawa		STAILQ_INSERT_TAIL(&sdev->target->mgm_ocb_queue, ocb, ocb);
1461170374Ssimokawa		SBP_UNLOCK(target->sbp);
1462111615Ssimokawa		splx(s);
1463111615Ssimokawa		return;
1464111615Ssimokawa	}
1465111615Ssimokawastart:
1466111615Ssimokawa	target->mgm_ocb_cur = ocb;
1467111615Ssimokawa	splx(s);
1468111615Ssimokawa
1469111615Ssimokawa	callout_reset(&target->mgm_ocb_timeout, 5*hz,
1470114732Ssimokawa				sbp_mgm_timeout, (caddr_t)ocb);
1471103285Sikob	xfer = sbp_write_cmd(sdev, FWTCODE_WREQB, 0);
1472103285Sikob	if(xfer == NULL){
1473103285Sikob		return;
1474103285Sikob	}
1475167632Ssimokawa	xfer->hand = sbp_mgm_callback;
1476103285Sikob
1477120660Ssimokawa	fp = &xfer->send.hdr;
1478113584Ssimokawa	fp->mode.wreqb.dest_hi = sdev->target->mgm_hi;
1479113584Ssimokawa	fp->mode.wreqb.dest_lo = sdev->target->mgm_lo;
1480113584Ssimokawa	fp->mode.wreqb.len = 8;
1481103285Sikob	fp->mode.wreqb.extcode = 0;
1482120660Ssimokawa	xfer->send.payload[0] = htonl(nid << 16);
1483120660Ssimokawa	xfer->send.payload[1] = htonl(ocb->bus_addr & 0xffffffff);
1484103285Sikob
1485103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1486103285Sikob}
1487103285Sikob
1488103285Sikobstatic void
1489105792Ssimokawasbp_print_scsi_cmd(struct sbp_ocb *ocb)
1490103285Sikob{
1491103285Sikob	struct ccb_scsiio *csio;
1492103285Sikob
1493103285Sikob	csio = &ocb->ccb->csio;
1494103285Sikob	printf("%s:%d:%d XPT_SCSI_IO: "
1495103285Sikob		"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
1496103285Sikob		", flags: 0x%02x, "
1497103285Sikob		"%db cmd/%db data/%db sense\n",
1498103285Sikob		device_get_nameunit(ocb->sdev->target->sbp->fd.dev),
1499103285Sikob		ocb->ccb->ccb_h.target_id, ocb->ccb->ccb_h.target_lun,
1500103285Sikob		csio->cdb_io.cdb_bytes[0],
1501103285Sikob		csio->cdb_io.cdb_bytes[1],
1502103285Sikob		csio->cdb_io.cdb_bytes[2],
1503103285Sikob		csio->cdb_io.cdb_bytes[3],
1504103285Sikob		csio->cdb_io.cdb_bytes[4],
1505103285Sikob		csio->cdb_io.cdb_bytes[5],
1506103285Sikob		csio->cdb_io.cdb_bytes[6],
1507103285Sikob		csio->cdb_io.cdb_bytes[7],
1508103285Sikob		csio->cdb_io.cdb_bytes[8],
1509103285Sikob		csio->cdb_io.cdb_bytes[9],
1510103285Sikob		ocb->ccb->ccb_h.flags & CAM_DIR_MASK,
1511103285Sikob		csio->cdb_len, csio->dxfer_len,
1512103285Sikob		csio->sense_len);
1513105792Ssimokawa}
1514105792Ssimokawa
1515105792Ssimokawastatic void
1516105792Ssimokawasbp_scsi_status(struct sbp_status *sbp_status, struct sbp_ocb *ocb)
1517105792Ssimokawa{
1518105792Ssimokawa	struct sbp_cmd_status *sbp_cmd_status;
1519225950Sken	struct scsi_sense_data_fixed *sense;
1520105792Ssimokawa
1521105792Ssimokawa	sbp_cmd_status = (struct sbp_cmd_status *)sbp_status->data;
1522225950Sken	sense = (struct scsi_sense_data_fixed *)&ocb->ccb->csio.sense_data;
1523105792Ssimokawa
1524105792SsimokawaSBP_DEBUG(0)
1525105792Ssimokawa	sbp_print_scsi_cmd(ocb);
1526103285Sikob	/* XXX need decode status */
1527188756Ssbruno	printf("%s: SCSI status %x sfmt %x valid %x key %x code %x qlfr %x len %d\n",
1528188756Ssbruno		ocb->sdev->bustgtlun,
1529103285Sikob		sbp_cmd_status->status,
1530103285Sikob		sbp_cmd_status->sfmt,
1531103285Sikob		sbp_cmd_status->valid,
1532103285Sikob		sbp_cmd_status->s_key,
1533103285Sikob		sbp_cmd_status->s_code,
1534103285Sikob		sbp_cmd_status->s_qlfr,
1535188756Ssbruno		sbp_status->len);
1536103285SikobEND_DEBUG
1537103285Sikob
1538110071Ssimokawa	switch (sbp_cmd_status->status) {
1539110071Ssimokawa	case SCSI_STATUS_CHECK_COND:
1540110071Ssimokawa	case SCSI_STATUS_BUSY:
1541110071Ssimokawa	case SCSI_STATUS_CMD_TERMINATED:
1542103285Sikob		if(sbp_cmd_status->sfmt == SBP_SFMT_CURR){
1543103285Sikob			sense->error_code = SSD_CURRENT_ERROR;
1544103285Sikob		}else{
1545103285Sikob			sense->error_code = SSD_DEFERRED_ERROR;
1546103285Sikob		}
1547103285Sikob		if(sbp_cmd_status->valid)
1548103285Sikob			sense->error_code |= SSD_ERRCODE_VALID;
1549103285Sikob		sense->flags = sbp_cmd_status->s_key;
1550103285Sikob		if(sbp_cmd_status->mark)
1551103285Sikob			sense->flags |= SSD_FILEMARK;
1552103285Sikob		if(sbp_cmd_status->eom)
1553103285Sikob			sense->flags |= SSD_EOM;
1554103285Sikob		if(sbp_cmd_status->ill_len)
1555103285Sikob			sense->flags |= SSD_ILI;
1556119556Ssimokawa
1557119556Ssimokawa		bcopy(&sbp_cmd_status->info, &sense->info[0], 4);
1558119556Ssimokawa
1559103285Sikob		if (sbp_status->len <= 1)
1560103285Sikob			/* XXX not scsi status. shouldn't be happened */
1561103285Sikob			sense->extra_len = 0;
1562103285Sikob		else if (sbp_status->len <= 4)
1563103285Sikob			/* add_sense_code(_qual), info, cmd_spec_info */
1564103285Sikob			sense->extra_len = 6;
1565103285Sikob		else
1566103285Sikob			/* fru, sense_key_spec */
1567103285Sikob			sense->extra_len = 10;
1568119556Ssimokawa
1569119556Ssimokawa		bcopy(&sbp_cmd_status->cdb, &sense->cmd_spec_info[0], 4);
1570119556Ssimokawa
1571103285Sikob		sense->add_sense_code = sbp_cmd_status->s_code;
1572103285Sikob		sense->add_sense_code_qual = sbp_cmd_status->s_qlfr;
1573103285Sikob		sense->fru = sbp_cmd_status->fru;
1574103285Sikob
1575119556Ssimokawa		bcopy(&sbp_cmd_status->s_keydep[0],
1576119556Ssimokawa		    &sense->sense_key_spec[0], 3);
1577119556Ssimokawa
1578201758Smbr		ocb->ccb->csio.scsi_status = sbp_cmd_status->status;
1579103285Sikob		ocb->ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
1580103285Sikob							| CAM_AUTOSNS_VALID;
1581103285Sikob/*
1582103285Sikob{
1583129585Sdfr		uint8_t j, *tmp;
1584103285Sikob		tmp = sense;
1585103285Sikob		for( j = 0 ; j < 32 ; j+=8){
1586103285Sikob			printf("sense %02x%02x %02x%02x %02x%02x %02x%02x\n",
1587103285Sikob				tmp[j], tmp[j+1], tmp[j+2], tmp[j+3],
1588103285Sikob				tmp[j+4], tmp[j+5], tmp[j+6], tmp[j+7]);
1589103285Sikob		}
1590103285Sikob
1591103285Sikob}
1592103285Sikob*/
1593110071Ssimokawa		break;
1594110071Ssimokawa	default:
1595188756Ssbruno		device_printf(ocb->sdev->target->sbp->fd.dev,
1596188756Ssbruno				"%s:%s unknown scsi status 0x%x\n",
1597188756Ssbruno				__func__, ocb->sdev->bustgtlun,
1598188756Ssbruno				sbp_cmd_status->status);
1599103285Sikob	}
1600103285Sikob}
1601103285Sikob
1602103285Sikobstatic void
1603103285Sikobsbp_fix_inq_data(struct sbp_ocb *ocb)
1604103285Sikob{
1605103285Sikob	union ccb *ccb;
1606103285Sikob	struct sbp_dev *sdev;
1607103285Sikob	struct scsi_inquiry_data *inq;
1608103285Sikob
1609103285Sikob	ccb = ocb->ccb;
1610103285Sikob	sdev = ocb->sdev;
1611103285Sikob
1612103285Sikob	if (ccb->csio.cdb_io.cdb_bytes[1] & SI_EVPD)
1613103285Sikob		return;
1614103285SikobSBP_DEBUG(1)
1615188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
1616188756Ssbruno		"%s:%s\n", __func__, sdev->bustgtlun);
1617103285SikobEND_DEBUG
1618103285Sikob	inq = (struct scsi_inquiry_data *) ccb->csio.data_ptr;
1619103285Sikob	switch (SID_TYPE(inq)) {
1620103285Sikob	case T_DIRECT:
1621118291Ssimokawa#if 0
1622103285Sikob		/*
1623103285Sikob		 * XXX Convert Direct Access device to RBC.
1624108281Ssimokawa		 * I've never seen FireWire DA devices which support READ_6.
1625103285Sikob		 */
1626103285Sikob		if (SID_TYPE(inq) == T_DIRECT)
1627103285Sikob			inq->device |= T_RBC; /*  T_DIRECT == 0 */
1628103285Sikob#endif
1629103285Sikob		/* fall through */
1630103285Sikob	case T_RBC:
1631103285Sikob		/*
1632103285Sikob		 * Override vendor/product/revision information.
1633103285Sikob		 * Some devices sometimes return strange strings.
1634103285Sikob		 */
1635111615Ssimokawa#if 1
1636103285Sikob		bcopy(sdev->vendor, inq->vendor, sizeof(inq->vendor));
1637103285Sikob		bcopy(sdev->product, inq->product, sizeof(inq->product));
1638103285Sikob		bcopy(sdev->revision+2, inq->revision, sizeof(inq->revision));
1639111615Ssimokawa#endif
1640103285Sikob		break;
1641103285Sikob	}
1642127468Ssimokawa	/*
1643127468Ssimokawa	 * Force to enable/disable tagged queuing.
1644127468Ssimokawa	 * XXX CAM also checks SCP_QUEUE_DQUE flag in the control mode page.
1645127468Ssimokawa	 */
1646127468Ssimokawa	if (sbp_tags > 0)
1647127468Ssimokawa		inq->flags |= SID_CmdQue;
1648127468Ssimokawa	else if (sbp_tags < 0)
1649127468Ssimokawa		inq->flags &= ~SID_CmdQue;
1650127468Ssimokawa
1651103285Sikob}
1652103285Sikob
1653103285Sikobstatic void
1654113584Ssimokawasbp_recv1(struct fw_xfer *xfer)
1655113584Ssimokawa{
1656103285Sikob	struct fw_pkt *rfp;
1657103285Sikob#if NEED_RESPONSE
1658103285Sikob	struct fw_pkt *sfp;
1659103285Sikob#endif
1660103285Sikob	struct sbp_softc *sbp;
1661103285Sikob	struct sbp_dev *sdev;
1662103285Sikob	struct sbp_ocb *ocb;
1663103285Sikob	struct sbp_login_res *login_res = NULL;
1664103285Sikob	struct sbp_status *sbp_status;
1665103285Sikob	struct sbp_target *target;
1666111704Ssimokawa	int	orb_fun, status_valid0, status_valid, t, l, reset_agent = 0;
1667129585Sdfr	uint32_t addr;
1668103285Sikob/*
1669129585Sdfr	uint32_t *ld;
1670103285Sikob	ld = xfer->recv.buf;
1671103285Sikobprintf("sbp %x %d %d %08x %08x %08x %08x\n",
1672103285Sikob			xfer->resp, xfer->recv.len, xfer->recv.off, ntohl(ld[0]), ntohl(ld[1]), ntohl(ld[2]), ntohl(ld[3]));
1673103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[4]), ntohl(ld[5]), ntohl(ld[6]), ntohl(ld[7]));
1674103285Sikobprintf("sbp %08x %08x %08x %08x\n", ntohl(ld[8]), ntohl(ld[9]), ntohl(ld[10]), ntohl(ld[11]));
1675103285Sikob*/
1676113584Ssimokawa	sbp = (struct sbp_softc *)xfer->sc;
1677120660Ssimokawa	if (xfer->resp != 0){
1678122529Ssimokawa		printf("sbp_recv: xfer->resp = %d\n", xfer->resp);
1679113584Ssimokawa		goto done0;
1680103285Sikob	}
1681120660Ssimokawa	if (xfer->recv.payload == NULL){
1682120660Ssimokawa		printf("sbp_recv: xfer->recv.payload == NULL\n");
1683113584Ssimokawa		goto done0;
1684103285Sikob	}
1685120660Ssimokawa	rfp = &xfer->recv.hdr;
1686103285Sikob	if(rfp->mode.wreqb.tcode != FWTCODE_WREQB){
1687103285Sikob		printf("sbp_recv: tcode = %d\n", rfp->mode.wreqb.tcode);
1688113584Ssimokawa		goto done0;
1689103285Sikob	}
1690120660Ssimokawa	sbp_status = (struct sbp_status *)xfer->recv.payload;
1691113584Ssimokawa	addr = rfp->mode.wreqb.dest_lo;
1692103285SikobSBP_DEBUG(2)
1693103285Sikob	printf("received address 0x%x\n", addr);
1694103285SikobEND_DEBUG
1695110189Ssimokawa	t = SBP_ADDR2TRG(addr);
1696110189Ssimokawa	if (t >= SBP_NUM_TARGETS) {
1697110189Ssimokawa		device_printf(sbp->fd.dev,
1698110189Ssimokawa			"sbp_recv1: invalid target %d\n", t);
1699113584Ssimokawa		goto done0;
1700110189Ssimokawa	}
1701110189Ssimokawa	target = &sbp->targets[t];
1702110189Ssimokawa	l = SBP_ADDR2LUN(addr);
1703121185Ssimokawa	if (l >= target->num_lun || target->luns[l] == NULL) {
1704110189Ssimokawa		device_printf(sbp->fd.dev,
1705110189Ssimokawa			"sbp_recv1: invalid lun %d (target=%d)\n", l, t);
1706113584Ssimokawa		goto done0;
1707110189Ssimokawa	}
1708121185Ssimokawa	sdev = target->luns[l];
1709103285Sikob
1710110189Ssimokawa	ocb = NULL;
1711110189Ssimokawa	switch (sbp_status->src) {
1712110189Ssimokawa	case 0:
1713110189Ssimokawa	case 1:
1714111615Ssimokawa		/* check mgm_ocb_cur first */
1715111615Ssimokawa		ocb  = target->mgm_ocb_cur;
1716111615Ssimokawa		if (ocb != NULL) {
1717111615Ssimokawa			if (OCB_MATCH(ocb, sbp_status)) {
1718111615Ssimokawa				callout_stop(&target->mgm_ocb_timeout);
1719111615Ssimokawa				target->mgm_ocb_cur = NULL;
1720111615Ssimokawa				break;
1721111615Ssimokawa			}
1722111615Ssimokawa		}
1723111615Ssimokawa		ocb = sbp_dequeue_ocb(sdev, sbp_status);
1724110189Ssimokawa		if (ocb == NULL) {
1725188756Ssbruno			device_printf(sdev->target->sbp->fd.dev,
1726127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
1727188756Ssbruno				"%s:%s No ocb(%lx) on the queue\n",
1728127468Ssimokawa#else
1729188756Ssbruno				"%s:%s No ocb(%x) on the queue\n",
1730111858Ssimokawa#endif
1731188756Ssbruno				__func__,sdev->bustgtlun,
1732188756Ssbruno				ntohl(sbp_status->orb_lo));
1733110189Ssimokawa		}
1734110189Ssimokawa		break;
1735110189Ssimokawa	case 2:
1736110189Ssimokawa		/* unsolicit */
1737188756Ssbruno		device_printf(sdev->target->sbp->fd.dev,
1738188756Ssbruno			"%s:%s unsolicit status received\n",
1739188756Ssbruno			__func__, sdev->bustgtlun);
1740110189Ssimokawa		break;
1741110189Ssimokawa	default:
1742188756Ssbruno		device_printf(sdev->target->sbp->fd.dev,
1743188756Ssbruno			"%s:%s unknown sbp_status->src\n",
1744188756Ssbruno			__func__, sdev->bustgtlun);
1745110189Ssimokawa	}
1746110189Ssimokawa
1747111615Ssimokawa	status_valid0 = (sbp_status->src < 2
1748110189Ssimokawa			&& sbp_status->resp == ORB_RES_CMPL
1749111615Ssimokawa			&& sbp_status->dead == 0);
1750111615Ssimokawa	status_valid = (status_valid0 && sbp_status->status == 0);
1751103285Sikob
1752121185Ssimokawa	if (!status_valid0 || debug > 2){
1753103285Sikob		int status;
1754110129SsimokawaSBP_DEBUG(0)
1755188756Ssbruno		device_printf(sdev->target->sbp->fd.dev,
1756188756Ssbruno			"%s:%s ORB status src:%x resp:%x dead:%x"
1757127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
1758127468Ssimokawa				" len:%x stat:%x orb:%x%08lx\n",
1759127468Ssimokawa#else
1760103285Sikob				" len:%x stat:%x orb:%x%08x\n",
1761108712Ssimokawa#endif
1762188756Ssbruno			__func__, sdev->bustgtlun,
1763103285Sikob			sbp_status->src, sbp_status->resp, sbp_status->dead,
1764103285Sikob			sbp_status->len, sbp_status->status,
1765108280Ssimokawa			ntohs(sbp_status->orb_hi), ntohl(sbp_status->orb_lo));
1766110129SsimokawaEND_DEBUG
1767188756Ssbruno		device_printf(sdev->target->sbp->fd.dev,
1768188756Ssbruno				"%s\n", sdev->bustgtlun);
1769103285Sikob		status = sbp_status->status;
1770103285Sikob		switch(sbp_status->resp) {
1771103285Sikob		case 0:
1772103285Sikob			if (status > MAX_ORB_STATUS0)
1773103285Sikob				printf("%s\n", orb_status0[MAX_ORB_STATUS0]);
1774110189Ssimokawa			else
1775110129Ssimokawa				printf("%s\n", orb_status0[status]);
1776103285Sikob			break;
1777103285Sikob		case 1:
1778110336Ssimokawa			printf("Obj: %s, Error: %s\n",
1779103285Sikob				orb_status1_object[(status>>6) & 3],
1780103285Sikob				orb_status1_serial_bus_error[status & 0xf]);
1781103285Sikob			break;
1782110129Ssimokawa		case 2:
1783110129Ssimokawa			printf("Illegal request\n");
1784110129Ssimokawa			break;
1785110129Ssimokawa		case 3:
1786110129Ssimokawa			printf("Vendor dependent\n");
1787110129Ssimokawa			break;
1788103285Sikob		default:
1789110129Ssimokawa			printf("unknown respose code %d\n", sbp_status->resp);
1790103285Sikob		}
1791103285Sikob	}
1792103285Sikob
1793103285Sikob	/* we have to reset the fetch agent if it's dead */
1794103285Sikob	if (sbp_status->dead) {
1795111615Ssimokawa		if (sdev->path) {
1796170374Ssimokawa			SBP_LOCK(sbp);
1797103285Sikob			xpt_freeze_devq(sdev->path, 1);
1798111615Ssimokawa			sdev->freeze ++;
1799170374Ssimokawa			SBP_UNLOCK(sbp);
1800111615Ssimokawa		}
1801111704Ssimokawa		reset_agent = 1;
1802103285Sikob	}
1803103285Sikob
1804111704Ssimokawa	if (ocb == NULL)
1805111704Ssimokawa		goto done;
1806103285Sikob
1807103285Sikob	switch(ntohl(ocb->orb[4]) & ORB_FMT_MSK){
1808103285Sikob	case ORB_FMT_NOP:
1809103285Sikob		break;
1810103285Sikob	case ORB_FMT_VED:
1811103285Sikob		break;
1812103285Sikob	case ORB_FMT_STD:
1813111819Ssimokawa		switch(ocb->flags) {
1814103285Sikob		case OCB_ACT_MGM:
1815103285Sikob			orb_fun = ntohl(ocb->orb[4]) & ORB_FUN_MSK;
1816120842Ssimokawa			reset_agent = 0;
1817103285Sikob			switch(orb_fun) {
1818103285Sikob			case ORB_FUN_LGI:
1819113584Ssimokawa				fwdma_sync(&sdev->dma, BUS_DMASYNC_POSTREAD);
1820113584Ssimokawa				login_res = sdev->login;
1821103285Sikob				login_res->len = ntohs(login_res->len);
1822103285Sikob				login_res->id = ntohs(login_res->id);
1823103285Sikob				login_res->cmd_hi = ntohs(login_res->cmd_hi);
1824103285Sikob				login_res->cmd_lo = ntohl(login_res->cmd_lo);
1825103285Sikob				if (status_valid) {
1826103285SikobSBP_DEBUG(0)
1827188756Ssbruno					device_printf(sdev->target->sbp->fd.dev,
1828188756Ssbruno						"%s:%s login: len %d, ID %d, cmd %08x%08x, recon_hold %d\n",
1829188756Ssbruno						__func__, sdev->bustgtlun,
1830188756Ssbruno						login_res->len, login_res->id,
1831188756Ssbruno						login_res->cmd_hi, login_res->cmd_lo,
1832188756Ssbruno						ntohs(login_res->recon_hold));
1833103285SikobEND_DEBUG
1834103285Sikob					sbp_busy_timeout(sdev);
1835103285Sikob				} else {
1836110336Ssimokawa					/* forgot logout? */
1837188756Ssbruno					device_printf(sdev->target->sbp->fd.dev,
1838188756Ssbruno						"%s:%s login failed\n",
1839188756Ssbruno						__func__, sdev->bustgtlun);
1840103285Sikob					sdev->status = SBP_DEV_RESET;
1841103285Sikob				}
1842103285Sikob				break;
1843103285Sikob			case ORB_FUN_RCN:
1844113584Ssimokawa				login_res = sdev->login;
1845103285Sikob				if (status_valid) {
1846103285SikobSBP_DEBUG(0)
1847188756Ssbruno					device_printf(sdev->target->sbp->fd.dev,
1848188756Ssbruno						"%s:%s reconnect: len %d, ID %d, cmd %08x%08x\n",
1849188756Ssbruno						__func__, sdev->bustgtlun,
1850188756Ssbruno						login_res->len, login_res->id,
1851188756Ssbruno						login_res->cmd_hi, login_res->cmd_lo);
1852103285SikobEND_DEBUG
1853111615Ssimokawa					if (sdev->status == SBP_DEV_ATTACHED)
1854111615Ssimokawa						sbp_scan_dev(sdev);
1855111615Ssimokawa					else
1856111615Ssimokawa						sbp_agent_reset(sdev);
1857103285Sikob				} else {
1858103285Sikob					/* reconnection hold time exceed? */
1859110336SsimokawaSBP_DEBUG(0)
1860188756Ssbruno					device_printf(sdev->target->sbp->fd.dev,
1861188756Ssbruno						"%s:%s reconnect failed\n",
1862188756Ssbruno						__func__, sdev->bustgtlun);
1863110336SsimokawaEND_DEBUG
1864111615Ssimokawa					sbp_login(sdev);
1865103285Sikob				}
1866103285Sikob				break;
1867103285Sikob			case ORB_FUN_LGO:
1868103285Sikob				sdev->status = SBP_DEV_RESET;
1869103285Sikob				break;
1870110336Ssimokawa			case ORB_FUN_RST:
1871110336Ssimokawa				sbp_busy_timeout(sdev);
1872110336Ssimokawa				break;
1873103285Sikob			case ORB_FUN_LUR:
1874103285Sikob			case ORB_FUN_ATA:
1875103285Sikob			case ORB_FUN_ATS:
1876110336Ssimokawa				sbp_agent_reset(sdev);
1877103285Sikob				break;
1878103285Sikob			default:
1879188756Ssbruno				device_printf(sdev->target->sbp->fd.dev,
1880188756Ssbruno					"%s:%s unknown function %d\n",
1881188756Ssbruno					__func__, sdev->bustgtlun, orb_fun);
1882103285Sikob				break;
1883103285Sikob			}
1884111615Ssimokawa			sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
1885103285Sikob			break;
1886103285Sikob		case OCB_ACT_CMD:
1887114732Ssimokawa			sdev->timeout = 0;
1888103285Sikob			if(ocb->ccb != NULL){
1889103285Sikob				union ccb *ccb;
1890188756Ssbruno
1891103285Sikob				ccb = ocb->ccb;
1892103285Sikob				if(sbp_status->len > 1){
1893103285Sikob					sbp_scsi_status(sbp_status, ocb);
1894103285Sikob				}else{
1895103285Sikob					if(sbp_status->resp != ORB_RES_CMPL){
1896103285Sikob						ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1897103285Sikob					}else{
1898103285Sikob						ccb->ccb_h.status = CAM_REQ_CMP;
1899103285Sikob					}
1900103285Sikob				}
1901103285Sikob				/* fix up inq data */
1902103285Sikob				if (ccb->csio.cdb_io.cdb_bytes[0] == INQUIRY)
1903103285Sikob					sbp_fix_inq_data(ocb);
1904170374Ssimokawa				SBP_LOCK(sbp);
1905103285Sikob				xpt_done(ccb);
1906170374Ssimokawa				SBP_UNLOCK(sbp);
1907103285Sikob			}
1908103285Sikob			break;
1909103285Sikob		default:
1910103285Sikob			break;
1911103285Sikob		}
1912103285Sikob	}
1913103285Sikob
1914127468Ssimokawa	if (!use_doorbell)
1915127468Ssimokawa		sbp_free_ocb(sdev, ocb);
1916111704Ssimokawadone:
1917111704Ssimokawa	if (reset_agent)
1918111704Ssimokawa		sbp_agent_reset(sdev);
1919103285Sikob
1920113584Ssimokawadone0:
1921120660Ssimokawa	xfer->recv.pay_len = SBP_RECV_LEN;
1922103285Sikob/* The received packet is usually small enough to be stored within
1923103285Sikob * the buffer. In that case, the controller return ack_complete and
1924103285Sikob * no respose is necessary.
1925103285Sikob *
1926103285Sikob * XXX fwohci.c and firewire.c should inform event_code such as
1927103285Sikob * ack_complete or ack_pending to upper driver.
1928103285Sikob */
1929103285Sikob#if NEED_RESPONSE
1930103285Sikob	xfer->send.off = 0;
1931103285Sikob	sfp = (struct fw_pkt *)xfer->send.buf;
1932103285Sikob	sfp->mode.wres.dst = rfp->mode.wreqb.src;
1933113584Ssimokawa	xfer->dst = sfp->mode.wres.dst;
1934103285Sikob	xfer->spd = min(sdev->target->fwdev->speed, max_speed);
1935167632Ssimokawa	xfer->hand = sbp_loginres_callback;
1936103285Sikob
1937103285Sikob	sfp->mode.wres.tlrt = rfp->mode.wreqb.tlrt;
1938103285Sikob	sfp->mode.wres.tcode = FWTCODE_WRES;
1939103285Sikob	sfp->mode.wres.rtcode = 0;
1940103285Sikob	sfp->mode.wres.pri = 0;
1941103285Sikob
1942103285Sikob	fw_asyreq(xfer->fc, -1, xfer);
1943103285Sikob#else
1944113584Ssimokawa	/* recycle */
1945170807Ssimokawa	/* we don't need a lock here because bottom half is serialized */
1946113584Ssimokawa	STAILQ_INSERT_TAIL(&sbp->fwb.xferlist, xfer, link);
1947103285Sikob#endif
1948103285Sikob
1949103285Sikob	return;
1950103285Sikob
1951103285Sikob}
1952103285Sikob
1953103285Sikobstatic void
1954103285Sikobsbp_recv(struct fw_xfer *xfer)
1955103285Sikob{
1956103285Sikob	int s;
1957103285Sikob
1958103285Sikob	s = splcam();
1959103285Sikob	sbp_recv1(xfer);
1960103285Sikob	splx(s);
1961103285Sikob}
1962103285Sikob/*
1963103285Sikob * sbp_attach()
1964103285Sikob */
1965103285Sikobstatic int
1966103285Sikobsbp_attach(device_t dev)
1967103285Sikob{
1968103285Sikob	struct sbp_softc *sbp;
1969103285Sikob	struct cam_devq *devq;
1970170374Ssimokawa	struct firewire_comm *fc;
1971103285Sikob	int i, s, error;
1972103285Sikob
1973167624Ssimokawa	if (DFLTPHYS > SBP_MAXPHYS)
1974167624Ssimokawa		device_printf(dev, "Warning, DFLTPHYS(%dKB) is larger than "
1975167624Ssimokawa			"SBP_MAXPHYS(%dKB).\n", DFLTPHYS / 1024,
1976167624Ssimokawa			SBP_MAXPHYS / 1024);
1977167624Ssimokawa
1978170400Ssimokawa	if (!firewire_phydma_enable)
1979170400Ssimokawa		device_printf(dev, "Warning, hw.firewire.phydma_enable must be 1 "
1980170400Ssimokawa			"for SBP over FireWire.\n");
1981103285SikobSBP_DEBUG(0)
1982111199Ssimokawa	printf("sbp_attach (cold=%d)\n", cold);
1983103285SikobEND_DEBUG
1984103285Sikob
1985111199Ssimokawa	if (cold)
1986111199Ssimokawa		sbp_cold ++;
1987103285Sikob	sbp = ((struct sbp_softc *)device_get_softc(dev));
1988103285Sikob	bzero(sbp, sizeof(struct sbp_softc));
1989103285Sikob	sbp->fd.dev = dev;
1990170374Ssimokawa	sbp->fd.fc = fc = device_get_ivars(dev);
1991170374Ssimokawa	mtx_init(&sbp->mtx, "sbp", NULL, MTX_DEF);
1992124251Ssimokawa
1993124251Ssimokawa	if (max_speed < 0)
1994170374Ssimokawa		max_speed = fc->speed;
1995124251Ssimokawa
1996170374Ssimokawa	error = bus_dma_tag_create(/*parent*/fc->dmat,
1997113584Ssimokawa				/* XXX shoud be 4 for sane backend? */
1998113584Ssimokawa				/*alignment*/1,
1999103285Sikob				/*boundary*/0,
2000103285Sikob				/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
2001103285Sikob				/*highaddr*/BUS_SPACE_MAXADDR,
2002103285Sikob				/*filter*/NULL, /*filterarg*/NULL,
2003103285Sikob				/*maxsize*/0x100000, /*nsegments*/SBP_IND_MAX,
2004113474Ssimokawa				/*maxsegsz*/SBP_SEG_MAX,
2005103285Sikob				/*flags*/BUS_DMA_ALLOCNOW,
2006127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 501102
2007117126Sscottl				/*lockfunc*/busdma_lock_mutex,
2008170374Ssimokawa				/*lockarg*/&sbp->mtx,
2009117228Ssimokawa#endif
2010117228Ssimokawa				&sbp->dmat);
2011103285Sikob	if (error != 0) {
2012103285Sikob		printf("sbp_attach: Could not allocate DMA tag "
2013103285Sikob			"- error %d\n", error);
2014103285Sikob			return (ENOMEM);
2015103285Sikob	}
2016103285Sikob
2017103285Sikob	devq = cam_simq_alloc(/*maxopenings*/SBP_NUM_OCB);
2018103285Sikob	if (devq == NULL)
2019103285Sikob		return (ENXIO);
2020103285Sikob
2021103285Sikob	for( i = 0 ; i < SBP_NUM_TARGETS ; i++){
2022103285Sikob		sbp->targets[i].fwdev = NULL;
2023103285Sikob		sbp->targets[i].luns = NULL;
2024103285Sikob	}
2025103285Sikob
2026103285Sikob	sbp->sim = cam_sim_alloc(sbp_action, sbp_poll, "sbp", sbp,
2027103285Sikob				 device_get_unit(dev),
2028170374Ssimokawa				 &sbp->mtx,
2029111615Ssimokawa				 /*untagged*/ 1,
2030122528Ssimokawa				 /*tagged*/ SBP_QUEUE_LEN - 1,
2031111615Ssimokawa				 devq);
2032103285Sikob
2033103285Sikob	if (sbp->sim == NULL) {
2034103285Sikob		cam_simq_free(devq);
2035103285Sikob		return (ENXIO);
2036103285Sikob	}
2037103285Sikob
2038170374Ssimokawa	SBP_LOCK(sbp);
2039170872Sscottl	if (xpt_bus_register(sbp->sim, dev, /*bus*/0) != CAM_SUCCESS)
2040111615Ssimokawa		goto fail;
2041103285Sikob
2042111615Ssimokawa	if (xpt_create_path(&sbp->path, xpt_periph, cam_sim_path(sbp->sim),
2043120660Ssimokawa	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2044120660Ssimokawa		xpt_bus_deregister(cam_sim_path(sbp->sim));
2045111615Ssimokawa		goto fail;
2046120660Ssimokawa	}
2047170374Ssimokawa	SBP_UNLOCK(sbp);
2048111615Ssimokawa
2049103285Sikob	/* We reserve 16 bit space (4 bytes X 64 targets X 256 luns) */
2050120660Ssimokawa	sbp->fwb.start = ((u_int64_t)SBP_BIND_HI << 32) | SBP_DEV2ADDR(0, 0);
2051120660Ssimokawa	sbp->fwb.end = sbp->fwb.start + 0xffff;
2052113584Ssimokawa	/* pre-allocate xfer */
2053113584Ssimokawa	STAILQ_INIT(&sbp->fwb.xferlist);
2054169130Ssimokawa	fw_xferlist_add(&sbp->fwb.xferlist, M_SBP,
2055169130Ssimokawa	    /*send*/ 0, /*recv*/ SBP_RECV_LEN, SBP_NUM_OCB/2,
2056170374Ssimokawa	    fc, (void *)sbp, sbp_recv);
2057103285Sikob
2058170374Ssimokawa	fw_bindadd(fc, &sbp->fwb);
2059170374Ssimokawa
2060113584Ssimokawa	sbp->fd.post_busreset = sbp_post_busreset;
2061103285Sikob	sbp->fd.post_explore = sbp_post_explore;
2062103285Sikob
2063170374Ssimokawa	if (fc->status != -1) {
2064111199Ssimokawa		s = splfw();
2065122387Ssimokawa		sbp_post_busreset((void *)sbp);
2066111199Ssimokawa		sbp_post_explore((void *)sbp);
2067111199Ssimokawa		splx(s);
2068111199Ssimokawa	}
2069170374Ssimokawa	SBP_LOCK(sbp);
2070122387Ssimokawa	xpt_async(AC_BUS_RESET, sbp->path, /*arg*/ NULL);
2071170374Ssimokawa	SBP_UNLOCK(sbp);
2072111199Ssimokawa
2073103285Sikob	return (0);
2074111615Ssimokawafail:
2075170374Ssimokawa	SBP_UNLOCK(sbp);
2076111615Ssimokawa	cam_sim_free(sbp->sim, /*free_devq*/TRUE);
2077111615Ssimokawa	return (ENXIO);
2078103285Sikob}
2079103285Sikob
2080103285Sikobstatic int
2081110145Ssimokawasbp_logout_all(struct sbp_softc *sbp)
2082110145Ssimokawa{
2083110145Ssimokawa	struct sbp_target *target;
2084110145Ssimokawa	struct sbp_dev *sdev;
2085110145Ssimokawa	int i, j;
2086110145Ssimokawa
2087110145SsimokawaSBP_DEBUG(0)
2088110145Ssimokawa	printf("sbp_logout_all\n");
2089110145SsimokawaEND_DEBUG
2090110145Ssimokawa	for (i = 0 ; i < SBP_NUM_TARGETS ; i ++) {
2091110145Ssimokawa		target = &sbp->targets[i];
2092110145Ssimokawa		if (target->luns == NULL)
2093110145Ssimokawa			continue;
2094110145Ssimokawa		for (j = 0; j < target->num_lun; j++) {
2095121185Ssimokawa			sdev = target->luns[j];
2096121185Ssimokawa			if (sdev == NULL)
2097121185Ssimokawa				continue;
2098113584Ssimokawa			callout_stop(&sdev->login_callout);
2099110336Ssimokawa			if (sdev->status >= SBP_DEV_TOATTACH &&
2100110336Ssimokawa					sdev->status <= SBP_DEV_ATTACHED)
2101111615Ssimokawa				sbp_mgm_orb(sdev, ORB_FUN_LGO, NULL);
2102110145Ssimokawa		}
2103110145Ssimokawa	}
2104113584Ssimokawa
2105110145Ssimokawa	return 0;
2106110145Ssimokawa}
2107110145Ssimokawa
2108110145Ssimokawastatic int
2109110145Ssimokawasbp_shutdown(device_t dev)
2110110145Ssimokawa{
2111110145Ssimokawa	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2112110145Ssimokawa
2113110145Ssimokawa	sbp_logout_all(sbp);
2114110145Ssimokawa	return (0);
2115110145Ssimokawa}
2116110145Ssimokawa
2117121185Ssimokawastatic void
2118121185Ssimokawasbp_free_sdev(struct sbp_dev *sdev)
2119121185Ssimokawa{
2120121185Ssimokawa	int i;
2121121185Ssimokawa
2122121185Ssimokawa	if (sdev == NULL)
2123121185Ssimokawa		return;
2124121185Ssimokawa	for (i = 0; i < SBP_QUEUE_LEN; i++)
2125121185Ssimokawa		bus_dmamap_destroy(sdev->target->sbp->dmat,
2126121185Ssimokawa		    sdev->ocb[i].dmamap);
2127121185Ssimokawa	fwdma_free(sdev->target->sbp->fd.fc, &sdev->dma);
2128121185Ssimokawa	free(sdev, M_SBP);
2129187993Ssbruno	sdev = NULL;
2130121185Ssimokawa}
2131121185Ssimokawa
2132121185Ssimokawastatic void
2133121185Ssimokawasbp_free_target(struct sbp_target *target)
2134121185Ssimokawa{
2135121185Ssimokawa	struct sbp_softc *sbp;
2136121185Ssimokawa	struct fw_xfer *xfer, *next;
2137121185Ssimokawa	int i;
2138121185Ssimokawa
2139121185Ssimokawa	if (target->luns == NULL)
2140121185Ssimokawa		return;
2141121185Ssimokawa	callout_stop(&target->mgm_ocb_timeout);
2142121185Ssimokawa	sbp = target->sbp;
2143121185Ssimokawa	for (i = 0; i < target->num_lun; i++)
2144121185Ssimokawa		sbp_free_sdev(target->luns[i]);
2145121185Ssimokawa
2146121185Ssimokawa	for (xfer = STAILQ_FIRST(&target->xferlist);
2147121185Ssimokawa			xfer != NULL; xfer = next) {
2148121185Ssimokawa		next = STAILQ_NEXT(xfer, link);
2149121185Ssimokawa		fw_xfer_free_buf(xfer);
2150121185Ssimokawa	}
2151121185Ssimokawa	STAILQ_INIT(&target->xferlist);
2152121185Ssimokawa	free(target->luns, M_SBP);
2153201758Smbr	target->num_lun = 0;
2154121185Ssimokawa	target->luns = NULL;
2155121185Ssimokawa	target->fwdev = NULL;
2156121185Ssimokawa}
2157121185Ssimokawa
2158110145Ssimokawastatic int
2159103285Sikobsbp_detach(device_t dev)
2160103285Sikob{
2161103285Sikob	struct sbp_softc *sbp = ((struct sbp_softc *)device_get_softc(dev));
2162103285Sikob	struct firewire_comm *fc = sbp->fd.fc;
2163121185Ssimokawa	int i;
2164103285Sikob
2165103285SikobSBP_DEBUG(0)
2166103285Sikob	printf("sbp_detach\n");
2167103285SikobEND_DEBUG
2168113584Ssimokawa
2169103285Sikob	for (i = 0; i < SBP_NUM_TARGETS; i ++)
2170110145Ssimokawa		sbp_cam_detach_target(&sbp->targets[i]);
2171170374Ssimokawa
2172170374Ssimokawa	SBP_LOCK(sbp);
2173120660Ssimokawa	xpt_async(AC_LOST_DEVICE, sbp->path, NULL);
2174111615Ssimokawa	xpt_free_path(sbp->path);
2175103285Sikob	xpt_bus_deregister(cam_sim_path(sbp->sim));
2176170378Sgallatin	cam_sim_free(sbp->sim, /*free_devq*/ TRUE);
2177170374Ssimokawa	SBP_UNLOCK(sbp);
2178110145Ssimokawa
2179110145Ssimokawa	sbp_logout_all(sbp);
2180113584Ssimokawa
2181110145Ssimokawa	/* XXX wait for logout completion */
2182167086Sjhb	pause("sbpdtc", hz/2);
2183110145Ssimokawa
2184121185Ssimokawa	for (i = 0 ; i < SBP_NUM_TARGETS ; i ++)
2185121185Ssimokawa		sbp_free_target(&sbp->targets[i]);
2186113584Ssimokawa
2187110145Ssimokawa	fw_bindremove(fc, &sbp->fwb);
2188169130Ssimokawa	fw_xferlist_remove(&sbp->fwb.xferlist);
2189113584Ssimokawa
2190103285Sikob	bus_dma_tag_destroy(sbp->dmat);
2191170374Ssimokawa	mtx_destroy(&sbp->mtx);
2192110145Ssimokawa
2193103285Sikob	return (0);
2194103285Sikob}
2195103285Sikob
2196103285Sikobstatic void
2197121185Ssimokawasbp_cam_detach_sdev(struct sbp_dev *sdev)
2198121185Ssimokawa{
2199121185Ssimokawa	if (sdev == NULL)
2200121185Ssimokawa		return;
2201121185Ssimokawa	if (sdev->status == SBP_DEV_DEAD)
2202121185Ssimokawa		return;
2203121185Ssimokawa	if (sdev->status == SBP_DEV_RESET)
2204121185Ssimokawa		return;
2205170374Ssimokawa	sbp_abort_all_ocbs(sdev, CAM_DEV_NOT_THERE);
2206121185Ssimokawa	if (sdev->path) {
2207170374Ssimokawa		SBP_LOCK(sdev->target->sbp);
2208121185Ssimokawa		xpt_release_devq(sdev->path,
2209121185Ssimokawa				 sdev->freeze, TRUE);
2210121185Ssimokawa		sdev->freeze = 0;
2211121185Ssimokawa		xpt_async(AC_LOST_DEVICE, sdev->path, NULL);
2212121185Ssimokawa		xpt_free_path(sdev->path);
2213121185Ssimokawa		sdev->path = NULL;
2214170374Ssimokawa		SBP_UNLOCK(sdev->target->sbp);
2215121185Ssimokawa	}
2216121185Ssimokawa}
2217121185Ssimokawa
2218121185Ssimokawastatic void
2219110145Ssimokawasbp_cam_detach_target(struct sbp_target *target)
2220103285Sikob{
2221103285Sikob	int i;
2222103285Sikob
2223103285Sikob	if (target->luns != NULL) {
2224108529SsimokawaSBP_DEBUG(0)
2225103285Sikob		printf("sbp_detach_target %d\n", target->target_id);
2226108529SsimokawaEND_DEBUG
2227111615Ssimokawa		callout_stop(&target->scan_callout);
2228121185Ssimokawa		for (i = 0; i < target->num_lun; i++)
2229121185Ssimokawa			sbp_cam_detach_sdev(target->luns[i]);
2230103285Sikob	}
2231103285Sikob}
2232103285Sikob
2233103285Sikobstatic void
2234114732Ssimokawasbp_target_reset(struct sbp_dev *sdev, int method)
2235114732Ssimokawa{
2236114732Ssimokawa	int i;
2237114732Ssimokawa	struct sbp_target *target = sdev->target;
2238114732Ssimokawa	struct sbp_dev *tsdev;
2239114732Ssimokawa
2240114732Ssimokawa	for (i = 0; i < target->num_lun; i++) {
2241121185Ssimokawa		tsdev = target->luns[i];
2242121185Ssimokawa		if (tsdev == NULL)
2243121185Ssimokawa			continue;
2244114732Ssimokawa		if (tsdev->status == SBP_DEV_DEAD)
2245114732Ssimokawa			continue;
2246114732Ssimokawa		if (tsdev->status == SBP_DEV_RESET)
2247114732Ssimokawa			continue;
2248170374Ssimokawa		SBP_LOCK(target->sbp);
2249114732Ssimokawa		xpt_freeze_devq(tsdev->path, 1);
2250114732Ssimokawa		tsdev->freeze ++;
2251170374Ssimokawa		SBP_UNLOCK(target->sbp);
2252114732Ssimokawa		sbp_abort_all_ocbs(tsdev, CAM_CMD_TIMEOUT);
2253114732Ssimokawa		if (method == 2)
2254114732Ssimokawa			tsdev->status = SBP_DEV_LOGIN;
2255114732Ssimokawa	}
2256114732Ssimokawa	switch(method) {
2257114732Ssimokawa	case 1:
2258114732Ssimokawa		printf("target reset\n");
2259114732Ssimokawa		sbp_mgm_orb(sdev, ORB_FUN_RST, NULL);
2260114732Ssimokawa		break;
2261114732Ssimokawa	case 2:
2262114732Ssimokawa		printf("reset start\n");
2263114732Ssimokawa		sbp_reset_start(sdev);
2264114732Ssimokawa		break;
2265114732Ssimokawa	}
2266114732Ssimokawa
2267114732Ssimokawa}
2268114732Ssimokawa
2269114732Ssimokawastatic void
2270114732Ssimokawasbp_mgm_timeout(void *arg)
2271114732Ssimokawa{
2272114732Ssimokawa	struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2273114732Ssimokawa	struct sbp_dev *sdev = ocb->sdev;
2274114732Ssimokawa	struct sbp_target *target = sdev->target;
2275114732Ssimokawa
2276188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
2277188756Ssbruno		"%s:%s request timeout(mgm orb:0x%08x)\n",
2278188756Ssbruno		__func__, sdev->bustgtlun, (uint32_t)ocb->bus_addr);
2279114732Ssimokawa	target->mgm_ocb_cur = NULL;
2280114732Ssimokawa	sbp_free_ocb(sdev, ocb);
2281114732Ssimokawa#if 0
2282114732Ssimokawa	/* XXX */
2283121185Ssimokawa	printf("run next request\n");
2284114732Ssimokawa	sbp_mgm_orb(sdev, ORB_FUN_RUNQUEUE, NULL);
2285114732Ssimokawa#endif
2286188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
2287188756Ssbruno		"%s:%s reset start\n",
2288188756Ssbruno		__func__, sdev->bustgtlun);
2289114732Ssimokawa	sbp_reset_start(sdev);
2290114732Ssimokawa}
2291114732Ssimokawa
2292114732Ssimokawastatic void
2293103285Sikobsbp_timeout(void *arg)
2294103285Sikob{
2295103285Sikob	struct sbp_ocb *ocb = (struct sbp_ocb *)arg;
2296103285Sikob	struct sbp_dev *sdev = ocb->sdev;
2297103285Sikob
2298188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
2299188756Ssbruno		"%s:%s request timeout(cmd orb:0x%08x) ... ",
2300188756Ssbruno		__func__, sdev->bustgtlun, (uint32_t)ocb->bus_addr);
2301103285Sikob
2302114732Ssimokawa	sdev->timeout ++;
2303114732Ssimokawa	switch(sdev->timeout) {
2304114732Ssimokawa	case 1:
2305110336Ssimokawa		printf("agent reset\n");
2306170374Ssimokawa		SBP_LOCK(sdev->target->sbp);
2307114732Ssimokawa		xpt_freeze_devq(sdev->path, 1);
2308114732Ssimokawa		sdev->freeze ++;
2309170374Ssimokawa		SBP_UNLOCK(sdev->target->sbp);
2310114732Ssimokawa		sbp_abort_all_ocbs(sdev, CAM_CMD_TIMEOUT);
2311110336Ssimokawa		sbp_agent_reset(sdev);
2312114732Ssimokawa		break;
2313114732Ssimokawa	case 2:
2314114732Ssimokawa	case 3:
2315114732Ssimokawa		sbp_target_reset(sdev, sdev->timeout - 1);
2316114732Ssimokawa		break;
2317114732Ssimokawa#if 0
2318114732Ssimokawa	default:
2319114732Ssimokawa		/* XXX give up */
2320114732Ssimokawa		sbp_cam_detach_target(target);
2321114732Ssimokawa		if (target->luns != NULL)
2322114732Ssimokawa			free(target->luns, M_SBP);
2323201758Smbr		target->num_lun = 0;
2324114732Ssimokawa		target->luns = NULL;
2325114732Ssimokawa		target->fwdev = NULL;
2326114732Ssimokawa#endif
2327110336Ssimokawa	}
2328103285Sikob}
2329103285Sikob
2330103285Sikobstatic void
2331103285Sikobsbp_action1(struct cam_sim *sim, union ccb *ccb)
2332103285Sikob{
2333103285Sikob
2334103285Sikob	struct sbp_softc *sbp = (struct sbp_softc *)sim->softc;
2335103285Sikob	struct sbp_target *target = NULL;
2336103285Sikob	struct sbp_dev *sdev = NULL;
2337103285Sikob
2338103285Sikob	/* target:lun -> sdev mapping */
2339103285Sikob	if (sbp != NULL
2340103285Sikob			&& ccb->ccb_h.target_id != CAM_TARGET_WILDCARD
2341103285Sikob			&& ccb->ccb_h.target_id < SBP_NUM_TARGETS) {
2342103285Sikob		target = &sbp->targets[ccb->ccb_h.target_id];
2343103285Sikob		if (target->fwdev != NULL
2344103285Sikob				&& ccb->ccb_h.target_lun != CAM_LUN_WILDCARD
2345103285Sikob				&& ccb->ccb_h.target_lun < target->num_lun) {
2346121185Ssimokawa			sdev = target->luns[ccb->ccb_h.target_lun];
2347121185Ssimokawa			if (sdev != NULL && sdev->status != SBP_DEV_ATTACHED &&
2348103285Sikob				sdev->status != SBP_DEV_PROBE)
2349103285Sikob				sdev = NULL;
2350103285Sikob		}
2351103285Sikob	}
2352103285Sikob
2353103285SikobSBP_DEBUG(1)
2354103285Sikob	if (sdev == NULL)
2355103285Sikob		printf("invalid target %d lun %d\n",
2356103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2357103285SikobEND_DEBUG
2358103285Sikob
2359103285Sikob	switch (ccb->ccb_h.func_code) {
2360103285Sikob	case XPT_SCSI_IO:
2361103285Sikob	case XPT_RESET_DEV:
2362103285Sikob	case XPT_GET_TRAN_SETTINGS:
2363103285Sikob	case XPT_SET_TRAN_SETTINGS:
2364103285Sikob	case XPT_CALC_GEOMETRY:
2365103285Sikob		if (sdev == NULL) {
2366103285SikobSBP_DEBUG(1)
2367103285Sikob			printf("%s:%d:%d:func_code 0x%04x: "
2368103285Sikob				"Invalid target (target needed)\n",
2369103285Sikob				device_get_nameunit(sbp->fd.dev),
2370103285Sikob				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2371103285Sikob				ccb->ccb_h.func_code);
2372103285SikobEND_DEBUG
2373103285Sikob
2374108276Ssimokawa			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2375103285Sikob			xpt_done(ccb);
2376103285Sikob			return;
2377103285Sikob		}
2378103285Sikob		break;
2379103285Sikob	case XPT_PATH_INQ:
2380103285Sikob	case XPT_NOOP:
2381103285Sikob		/* The opcodes sometimes aimed at a target (sc is valid),
2382103285Sikob		 * sometimes aimed at the SIM (sc is invalid and target is
2383103285Sikob		 * CAM_TARGET_WILDCARD)
2384103285Sikob		 */
2385103285Sikob		if (sbp == NULL &&
2386103285Sikob			ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
2387103285SikobSBP_DEBUG(0)
2388103285Sikob			printf("%s:%d:%d func_code 0x%04x: "
2389103285Sikob				"Invalid target (no wildcard)\n",
2390103285Sikob				device_get_nameunit(sbp->fd.dev),
2391103285Sikob				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2392103285Sikob				ccb->ccb_h.func_code);
2393103285SikobEND_DEBUG
2394108276Ssimokawa			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2395103285Sikob			xpt_done(ccb);
2396103285Sikob			return;
2397103285Sikob		}
2398103285Sikob		break;
2399103285Sikob	default:
2400103285Sikob		/* XXX Hm, we should check the input parameters */
2401103285Sikob		break;
2402103285Sikob	}
2403103285Sikob
2404103285Sikob	switch (ccb->ccb_h.func_code) {
2405103285Sikob	case XPT_SCSI_IO:
2406103285Sikob	{
2407103285Sikob		struct ccb_scsiio *csio;
2408103285Sikob		struct sbp_ocb *ocb;
2409113584Ssimokawa		int speed;
2410106506Ssimokawa		void *cdb;
2411103285Sikob
2412103285Sikob		csio = &ccb->csio;
2413170374Ssimokawa		mtx_assert(sim->mtx, MA_OWNED);
2414103285Sikob
2415121185SsimokawaSBP_DEBUG(2)
2416103285Sikob		printf("%s:%d:%d XPT_SCSI_IO: "
2417103285Sikob			"cmd: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x"
2418103285Sikob			", flags: 0x%02x, "
2419103285Sikob			"%db cmd/%db data/%db sense\n",
2420103285Sikob			device_get_nameunit(sbp->fd.dev),
2421103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2422103285Sikob			csio->cdb_io.cdb_bytes[0],
2423103285Sikob			csio->cdb_io.cdb_bytes[1],
2424103285Sikob			csio->cdb_io.cdb_bytes[2],
2425103285Sikob			csio->cdb_io.cdb_bytes[3],
2426103285Sikob			csio->cdb_io.cdb_bytes[4],
2427103285Sikob			csio->cdb_io.cdb_bytes[5],
2428103285Sikob			csio->cdb_io.cdb_bytes[6],
2429103285Sikob			csio->cdb_io.cdb_bytes[7],
2430103285Sikob			csio->cdb_io.cdb_bytes[8],
2431103285Sikob			csio->cdb_io.cdb_bytes[9],
2432103285Sikob			ccb->ccb_h.flags & CAM_DIR_MASK,
2433103285Sikob			csio->cdb_len, csio->dxfer_len,
2434103285Sikob			csio->sense_len);
2435103285SikobEND_DEBUG
2436103285Sikob		if(sdev == NULL){
2437103285Sikob			ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2438103285Sikob			xpt_done(ccb);
2439103285Sikob			return;
2440103285Sikob		}
2441103285Sikob#if 0
2442103285Sikob		/* if we are in probe stage, pass only probe commands */
2443103285Sikob		if (sdev->status == SBP_DEV_PROBE) {
2444103285Sikob			char *name;
2445103285Sikob			name = xpt_path_periph(ccb->ccb_h.path)->periph_name;
2446103285Sikob			printf("probe stage, periph name: %s\n", name);
2447103285Sikob			if (strcmp(name, "probe") != 0) {
2448103285Sikob				ccb->ccb_h.status = CAM_REQUEUE_REQ;
2449103285Sikob				xpt_done(ccb);
2450103285Sikob				return;
2451103285Sikob			}
2452103285Sikob		}
2453103285Sikob#endif
2454122528Ssimokawa		if ((ocb = sbp_get_ocb(sdev)) == NULL) {
2455170807Ssimokawa			ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2456127468Ssimokawa			if (sdev->freeze == 0) {
2457170374Ssimokawa				SBP_LOCK(sdev->target->sbp);
2458127468Ssimokawa				xpt_freeze_devq(sdev->path, 1);
2459127468Ssimokawa				sdev->freeze ++;
2460170374Ssimokawa				SBP_UNLOCK(sdev->target->sbp);
2461127468Ssimokawa			}
2462122528Ssimokawa			xpt_done(ccb);
2463103285Sikob			return;
2464122528Ssimokawa		}
2465113584Ssimokawa
2466103285Sikob		ocb->flags = OCB_ACT_CMD;
2467103285Sikob		ocb->sdev = sdev;
2468103285Sikob		ocb->ccb = ccb;
2469103285Sikob		ccb->ccb_h.ccb_sdev_ptr = sdev;
2470103285Sikob		ocb->orb[0] = htonl(1 << 31);
2471103285Sikob		ocb->orb[1] = 0;
2472103285Sikob		ocb->orb[2] = htonl(((sbp->fd.fc->nodeid | FWLOCALBUS )<< 16) );
2473113584Ssimokawa		ocb->orb[3] = htonl(ocb->bus_addr + IND_PTR_OFFSET);
2474103285Sikob		speed = min(target->fwdev->speed, max_speed);
2475103285Sikob		ocb->orb[4] = htonl(ORB_NOTIFY | ORB_CMD_SPD(speed)
2476103285Sikob						| ORB_CMD_MAXP(speed + 7));
2477103285Sikob		if((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN){
2478103285Sikob			ocb->orb[4] |= htonl(ORB_CMD_IN);
2479103285Sikob		}
2480103285Sikob
2481106506Ssimokawa		if (csio->ccb_h.flags & CAM_CDB_POINTER)
2482106506Ssimokawa			cdb = (void *)csio->cdb_io.cdb_ptr;
2483106506Ssimokawa		else
2484106506Ssimokawa			cdb = (void *)&csio->cdb_io.cdb_bytes;
2485120660Ssimokawa		bcopy(cdb, (void *)&ocb->orb[5], csio->cdb_len);
2486103285Sikob/*
2487103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[0]), ntohl(ocb->orb[1]), ntohl(ocb->orb[2]), ntohl(ocb->orb[3]));
2488103285Sikobprintf("ORB %08x %08x %08x %08x\n", ntohl(ocb->orb[4]), ntohl(ocb->orb[5]), ntohl(ocb->orb[6]), ntohl(ocb->orb[7]));
2489103285Sikob*/
2490103285Sikob		if (ccb->csio.dxfer_len > 0) {
2491247109Simp			int error;
2492103285Sikob
2493246713Skib			error = bus_dmamap_load_ccb(/*dma tag*/sbp->dmat,
2494103285Sikob					/*dma map*/ocb->dmamap,
2495246713Skib					ccb,
2496103285Sikob					sbp_execute_ocb,
2497103285Sikob					ocb,
2498103285Sikob					/*flags*/0);
2499113584Ssimokawa			if (error)
2500113584Ssimokawa				printf("sbp: bus_dmamap_load error %d\n", error);
2501103285Sikob		} else
2502103285Sikob			sbp_execute_ocb(ocb, NULL, 0, 0);
2503103285Sikob		break;
2504103285Sikob	}
2505103285Sikob	case XPT_CALC_GEOMETRY:
2506103285Sikob	{
2507103285Sikob		struct ccb_calc_geometry *ccg;
2508127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 501100
2509129585Sdfr		uint32_t size_mb;
2510129585Sdfr		uint32_t secs_per_cylinder;
2511116429Ssimokawa		int extended = 1;
2512116429Ssimokawa#endif
2513116351Snjl
2514103285Sikob		ccg = &ccb->ccg;
2515103285Sikob		if (ccg->block_size == 0) {
2516103285Sikob			printf("sbp_action1: block_size is 0.\n");
2517103285Sikob			ccb->ccb_h.status = CAM_REQ_INVALID;
2518103285Sikob			xpt_done(ccb);
2519103285Sikob			break;
2520103285Sikob		}
2521103285SikobSBP_DEBUG(1)
2522103285Sikob		printf("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
2523127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2524127468Ssimokawa			"Volume size = %d\n",
2525127468Ssimokawa#else
2526114540Sjake			"Volume size = %jd\n",
2527114927Ssimokawa#endif
2528114927Ssimokawa			device_get_nameunit(sbp->fd.dev),
2529114927Ssimokawa			cam_sim_path(sbp->sim),
2530103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2531127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
2532114927Ssimokawa			(uintmax_t)
2533114927Ssimokawa#endif
2534114927Ssimokawa				ccg->volume_size);
2535103285SikobEND_DEBUG
2536103285Sikob
2537127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 501100
2538116429Ssimokawa		size_mb = ccg->volume_size
2539116429Ssimokawa			/ ((1024L * 1024L) / ccg->block_size);
2540116429Ssimokawa
2541116429Ssimokawa		if (size_mb > 1024 && extended) {
2542116429Ssimokawa			ccg->heads = 255;
2543116429Ssimokawa			ccg->secs_per_track = 63;
2544116429Ssimokawa		} else {
2545116429Ssimokawa			ccg->heads = 64;
2546116429Ssimokawa			ccg->secs_per_track = 32;
2547116429Ssimokawa		}
2548116429Ssimokawa		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2549116429Ssimokawa		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2550116429Ssimokawa		ccb->ccb_h.status = CAM_REQ_CMP;
2551116429Ssimokawa#else
2552116351Snjl		cam_calc_geometry(ccg, /*extended*/1);
2553116429Ssimokawa#endif
2554103285Sikob		xpt_done(ccb);
2555103285Sikob		break;
2556103285Sikob	}
2557103285Sikob	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
2558103285Sikob	{
2559103285Sikob
2560103285SikobSBP_DEBUG(1)
2561103285Sikob		printf("%s:%d:XPT_RESET_BUS: \n",
2562103285Sikob			device_get_nameunit(sbp->fd.dev), cam_sim_path(sbp->sim));
2563103285SikobEND_DEBUG
2564103285Sikob
2565103285Sikob		ccb->ccb_h.status = CAM_REQ_INVALID;
2566103285Sikob		xpt_done(ccb);
2567103285Sikob		break;
2568103285Sikob	}
2569103285Sikob	case XPT_PATH_INQ:		/* Path routing inquiry */
2570103285Sikob	{
2571103285Sikob		struct ccb_pathinq *cpi = &ccb->cpi;
2572103285Sikob
2573103285SikobSBP_DEBUG(1)
2574103285Sikob		printf("%s:%d:%d XPT_PATH_INQ:.\n",
2575103285Sikob			device_get_nameunit(sbp->fd.dev),
2576103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2577103285SikobEND_DEBUG
2578103285Sikob		cpi->version_num = 1; /* XXX??? */
2579111615Ssimokawa		cpi->hba_inquiry = PI_TAG_ABLE;
2580103285Sikob		cpi->target_sprt = 0;
2581118105Snjl		cpi->hba_misc = PIM_NOBUSRESET | PIM_NO_6_BYTE;
2582103285Sikob		cpi->hba_eng_cnt = 0;
2583103285Sikob		cpi->max_target = SBP_NUM_TARGETS - 1;
2584103285Sikob		cpi->max_lun = SBP_NUM_LUNS - 1;
2585103285Sikob		cpi->initiator_id = SBP_INITIATOR;
2586103285Sikob		cpi->bus_id = sim->bus_id;
2587103285Sikob		cpi->base_transfer_speed = 400 * 1000 / 8;
2588103285Sikob		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2589103285Sikob		strncpy(cpi->hba_vid, "SBP", HBA_IDLEN);
2590103285Sikob		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
2591103285Sikob		cpi->unit_number = sim->unit_number;
2592163816Smjacob                cpi->transport = XPORT_SPI;	/* XX should have a FireWire */
2593163816Smjacob                cpi->transport_version = 2;
2594163816Smjacob                cpi->protocol = PROTO_SCSI;
2595163816Smjacob                cpi->protocol_version = SCSI_REV_2;
2596103285Sikob
2597103285Sikob		cpi->ccb_h.status = CAM_REQ_CMP;
2598103285Sikob		xpt_done(ccb);
2599103285Sikob		break;
2600103285Sikob	}
2601103285Sikob	case XPT_GET_TRAN_SETTINGS:
2602103285Sikob	{
2603103285Sikob		struct ccb_trans_settings *cts = &ccb->cts;
2604163816Smjacob		struct ccb_trans_settings_scsi *scsi =
2605163816Smjacob		    &cts->proto_specific.scsi;
2606163816Smjacob		struct ccb_trans_settings_spi *spi =
2607163816Smjacob		    &cts->xport_specific.spi;
2608163816Smjacob
2609163816Smjacob		cts->protocol = PROTO_SCSI;
2610163816Smjacob		cts->protocol_version = SCSI_REV_2;
2611163816Smjacob		cts->transport = XPORT_SPI;	/* should have a FireWire */
2612163816Smjacob		cts->transport_version = 2;
2613163816Smjacob		spi->valid = CTS_SPI_VALID_DISC;
2614163816Smjacob		spi->flags = CTS_SPI_FLAGS_DISC_ENB;
2615163816Smjacob		scsi->valid = CTS_SCSI_VALID_TQ;
2616163816Smjacob		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2617103285SikobSBP_DEBUG(1)
2618103285Sikob		printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:.\n",
2619103285Sikob			device_get_nameunit(sbp->fd.dev),
2620103285Sikob			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2621103285SikobEND_DEBUG
2622103285Sikob		cts->ccb_h.status = CAM_REQ_CMP;
2623103285Sikob		xpt_done(ccb);
2624103285Sikob		break;
2625103285Sikob	}
2626103285Sikob	case XPT_ABORT:
2627103285Sikob		ccb->ccb_h.status = CAM_UA_ABORT;
2628103285Sikob		xpt_done(ccb);
2629103285Sikob		break;
2630111615Ssimokawa	case XPT_SET_TRAN_SETTINGS:
2631111615Ssimokawa		/* XXX */
2632103285Sikob	default:
2633103285Sikob		ccb->ccb_h.status = CAM_REQ_INVALID;
2634103285Sikob		xpt_done(ccb);
2635103285Sikob		break;
2636103285Sikob	}
2637103285Sikob	return;
2638103285Sikob}
2639103285Sikob
2640103285Sikobstatic void
2641103285Sikobsbp_action(struct cam_sim *sim, union ccb *ccb)
2642103285Sikob{
2643103285Sikob	int s;
2644103285Sikob
2645103285Sikob	s = splfw();
2646103285Sikob	sbp_action1(sim, ccb);
2647103285Sikob	splx(s);
2648103285Sikob}
2649103285Sikob
2650103285Sikobstatic void
2651103285Sikobsbp_execute_ocb(void *arg,  bus_dma_segment_t *segments, int seg, int error)
2652103285Sikob{
2653103285Sikob	int i;
2654103285Sikob	struct sbp_ocb *ocb;
2655103285Sikob	struct sbp_ocb *prev;
2656105633Ssimokawa	bus_dma_segment_t *s;
2657103285Sikob
2658103285Sikob	if (error)
2659103285Sikob		printf("sbp_execute_ocb: error=%d\n", error);
2660103285Sikob
2661103285Sikob	ocb = (struct sbp_ocb *)arg;
2662113584Ssimokawa
2663121185SsimokawaSBP_DEBUG(2)
2664113584Ssimokawa	printf("sbp_execute_ocb: seg %d", seg);
2665113584Ssimokawa	for (i = 0; i < seg; i++)
2666127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2667127468Ssimokawa		printf(", %x:%d", segments[i].ds_addr, segments[i].ds_len);
2668127468Ssimokawa#else
2669113972Ssimokawa		printf(", %jx:%jd", (uintmax_t)segments[i].ds_addr,
2670113972Ssimokawa					(uintmax_t)segments[i].ds_len);
2671113584Ssimokawa#endif
2672113584Ssimokawa	printf("\n");
2673113584SsimokawaEND_DEBUG
2674113584Ssimokawa
2675103285Sikob	if (seg == 1) {
2676103285Sikob		/* direct pointer */
2677113474Ssimokawa		s = &segments[0];
2678113474Ssimokawa		if (s->ds_len > SBP_SEG_MAX)
2679113474Ssimokawa			panic("ds_len > SBP_SEG_MAX, fix busdma code");
2680113474Ssimokawa		ocb->orb[3] = htonl(s->ds_addr);
2681113474Ssimokawa		ocb->orb[4] |= htonl(s->ds_len);
2682103285Sikob	} else if(seg > 1) {
2683103285Sikob		/* page table */
2684103285Sikob		for (i = 0; i < seg; i++) {
2685105633Ssimokawa			s = &segments[i];
2686108877SsimokawaSBP_DEBUG(0)
2687108877Ssimokawa			/* XXX LSI Logic "< 16 byte" bug might be hit */
2688105633Ssimokawa			if (s->ds_len < 16)
2689105633Ssimokawa				printf("sbp_execute_ocb: warning, "
2690127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2691127468Ssimokawa					"segment length(%d) is less than 16."
2692127468Ssimokawa#else
2693106543Ssimokawa					"segment length(%zd) is less than 16."
2694108877Ssimokawa#endif
2695205842Simp					"(seg=%d/%d)\n", (size_t)s->ds_len, i+1, seg);
2696108877SsimokawaEND_DEBUG
2697113474Ssimokawa			if (s->ds_len > SBP_SEG_MAX)
2698113474Ssimokawa				panic("ds_len > SBP_SEG_MAX, fix busdma code");
2699105633Ssimokawa			ocb->ind_ptr[i].hi = htonl(s->ds_len << 16);
2700105633Ssimokawa			ocb->ind_ptr[i].lo = htonl(s->ds_addr);
2701103285Sikob		}
2702103285Sikob		ocb->orb[4] |= htonl(ORB_CMD_PTBL | seg);
2703103285Sikob	}
2704103285Sikob
2705113584Ssimokawa	if (seg > 0)
2706113584Ssimokawa		bus_dmamap_sync(ocb->sdev->target->sbp->dmat, ocb->dmamap,
2707113584Ssimokawa			(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2708113584Ssimokawa			BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2709103285Sikob	prev = sbp_enqueue_ocb(ocb->sdev, ocb);
2710113584Ssimokawa	fwdma_sync(&ocb->sdev->dma, BUS_DMASYNC_PREWRITE);
2711127468Ssimokawa	if (use_doorbell) {
2712127468Ssimokawa		if (prev == NULL) {
2713127468Ssimokawa			if (ocb->sdev->last_ocb != NULL)
2714127468Ssimokawa				sbp_doorbell(ocb->sdev);
2715127468Ssimokawa			else
2716127468Ssimokawa				sbp_orb_pointer(ocb->sdev, ocb);
2717127468Ssimokawa		}
2718127468Ssimokawa	} else {
2719127468Ssimokawa		if (prev == NULL || (ocb->sdev->flags & ORB_LINK_DEAD) != 0) {
2720127468Ssimokawa			ocb->sdev->flags &= ~ORB_LINK_DEAD;
2721127468Ssimokawa			sbp_orb_pointer(ocb->sdev, ocb);
2722127468Ssimokawa		}
2723120842Ssimokawa	}
2724103285Sikob}
2725103285Sikob
2726103285Sikobstatic void
2727103285Sikobsbp_poll(struct cam_sim *sim)
2728103285Sikob{
2729120847Ssimokawa	struct sbp_softc *sbp;
2730120847Ssimokawa	struct firewire_comm *fc;
2731120847Ssimokawa
2732120847Ssimokawa	sbp = (struct sbp_softc *)sim->softc;
2733120847Ssimokawa	fc = sbp->fd.fc;
2734120847Ssimokawa
2735120847Ssimokawa	fc->poll(fc, 0, -1);
2736120847Ssimokawa
2737103285Sikob	return;
2738103285Sikob}
2739120847Ssimokawa
2740103285Sikobstatic struct sbp_ocb *
2741111615Ssimokawasbp_dequeue_ocb(struct sbp_dev *sdev, struct sbp_status *sbp_status)
2742103285Sikob{
2743103285Sikob	struct sbp_ocb *ocb;
2744103285Sikob	struct sbp_ocb *next;
2745103285Sikob	int s = splfw(), order = 0;
2746103285Sikob	int flags;
2747103285Sikob
2748103285SikobSBP_DEBUG(1)
2749188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
2750127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2751188756Ssbruno	"%s:%s 0x%08lx src %d\n",
2752127468Ssimokawa#else
2753188756Ssbruno	"%s:%s 0x%08x src %d\n",
2754108712Ssimokawa#endif
2755188756Ssbruno	    __func__, sdev->bustgtlun, ntohl(sbp_status->orb_lo), sbp_status->src);
2756103285SikobEND_DEBUG
2757170374Ssimokawa	SBP_LOCK(sdev->target->sbp);
2758121185Ssimokawa	for (ocb = STAILQ_FIRST(&sdev->ocbs); ocb != NULL; ocb = next) {
2759121185Ssimokawa		next = STAILQ_NEXT(ocb, ocb);
2760121185Ssimokawa		flags = ocb->flags;
2761111615Ssimokawa		if (OCB_MATCH(ocb, sbp_status)) {
2762103285Sikob			/* found */
2763111819Ssimokawa			STAILQ_REMOVE(&sdev->ocbs, ocb, sbp_ocb, ocb);
2764103285Sikob			if (ocb->ccb != NULL)
2765103285Sikob				untimeout(sbp_timeout, (caddr_t)ocb,
2766103285Sikob						ocb->ccb->ccb_h.timeout_ch);
2767113584Ssimokawa			if (ntohl(ocb->orb[4]) & 0xffff) {
2768113584Ssimokawa				bus_dmamap_sync(sdev->target->sbp->dmat,
2769113584Ssimokawa					ocb->dmamap,
2770113584Ssimokawa					(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2771113584Ssimokawa					BUS_DMASYNC_POSTREAD :
2772113584Ssimokawa					BUS_DMASYNC_POSTWRITE);
2773113584Ssimokawa				bus_dmamap_unload(sdev->target->sbp->dmat,
2774113584Ssimokawa					ocb->dmamap);
2775103285Sikob			}
2776127468Ssimokawa			if (!use_doorbell) {
2777127468Ssimokawa				if (sbp_status->src == SRC_NO_NEXT) {
2778127468Ssimokawa					if (next != NULL)
2779127468Ssimokawa						sbp_orb_pointer(sdev, next);
2780127468Ssimokawa					else if (order > 0) {
2781127468Ssimokawa						/*
2782127468Ssimokawa						 * Unordered execution
2783127468Ssimokawa						 * We need to send pointer for
2784127468Ssimokawa						 * next ORB
2785127468Ssimokawa						 */
2786127468Ssimokawa						sdev->flags |= ORB_LINK_DEAD;
2787127468Ssimokawa					}
2788120842Ssimokawa				}
2789127468Ssimokawa			} else {
2790127468Ssimokawa				/*
2791127468Ssimokawa				 * XXX this is not correct for unordered
2792127468Ssimokawa				 * execution.
2793127468Ssimokawa				 */
2794170807Ssimokawa				if (sdev->last_ocb != NULL) {
2795170807Ssimokawa					SBP_UNLOCK(sdev->target->sbp);
2796127468Ssimokawa					sbp_free_ocb(sdev, sdev->last_ocb);
2797170807Ssimokawa					SBP_LOCK(sdev->target->sbp);
2798170807Ssimokawa				}
2799127468Ssimokawa				sdev->last_ocb = ocb;
2800127468Ssimokawa				if (next != NULL &&
2801127468Ssimokawa				    sbp_status->src == SRC_NO_NEXT)
2802127468Ssimokawa					sbp_doorbell(sdev);
2803120842Ssimokawa			}
2804103285Sikob			break;
2805111819Ssimokawa		} else
2806111819Ssimokawa			order ++;
2807103285Sikob	}
2808170374Ssimokawa	SBP_UNLOCK(sdev->target->sbp);
2809103285Sikob	splx(s);
2810103285SikobSBP_DEBUG(0)
2811103285Sikob	if (ocb && order > 0) {
2812188756Ssbruno		device_printf(sdev->target->sbp->fd.dev,
2813188756Ssbruno			"%s:%s unordered execution order:%d\n",
2814188756Ssbruno			__func__, sdev->bustgtlun, order);
2815103285Sikob	}
2816103285SikobEND_DEBUG
2817103285Sikob	return (ocb);
2818103285Sikob}
2819103285Sikob
2820103285Sikobstatic struct sbp_ocb *
2821103285Sikobsbp_enqueue_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2822103285Sikob{
2823103285Sikob	int s = splfw();
2824127468Ssimokawa	struct sbp_ocb *prev, *prev2;
2825103285Sikob
2826170807Ssimokawa	mtx_assert(&sdev->target->sbp->mtx, MA_OWNED);
2827121185SsimokawaSBP_DEBUG(1)
2828188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
2829127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2830188756Ssbruno	"%s:%s 0x%08x\n", __func__, sdev->bustgtlun, ocb->bus_addr);
2831108712Ssimokawa#else
2832188756Ssbruno	"%s:%s 0x%08jx\n", __func__, sdev->bustgtlun, (uintmax_t)ocb->bus_addr);
2833108712Ssimokawa#endif
2834103285SikobEND_DEBUG
2835127468Ssimokawa	prev2 = prev = STAILQ_LAST(&sdev->ocbs, sbp_ocb, ocb);
2836103285Sikob	STAILQ_INSERT_TAIL(&sdev->ocbs, ocb, ocb);
2837103285Sikob
2838103285Sikob	if (ocb->ccb != NULL)
2839103285Sikob		ocb->ccb->ccb_h.timeout_ch = timeout(sbp_timeout, (caddr_t)ocb,
2840103285Sikob					(ocb->ccb->ccb_h.timeout * hz) / 1000);
2841103285Sikob
2842127468Ssimokawa	if (use_doorbell && prev == NULL)
2843127468Ssimokawa		prev2 = sdev->last_ocb;
2844127468Ssimokawa
2845170807Ssimokawa	if (prev2 != NULL && (ocb->sdev->flags & ORB_LINK_DEAD) == 0) {
2846170807SsimokawaSBP_DEBUG(1)
2847127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2848127468Ssimokawa		printf("linking chain 0x%x -> 0x%x\n",
2849127468Ssimokawa		    prev2->bus_addr, ocb->bus_addr);
2850127468Ssimokawa#else
2851120842Ssimokawa		printf("linking chain 0x%jx -> 0x%jx\n",
2852127468Ssimokawa		    (uintmax_t)prev2->bus_addr, (uintmax_t)ocb->bus_addr);
2853108712Ssimokawa#endif
2854103285SikobEND_DEBUG
2855170724Ssimokawa		/*
2856170724Ssimokawa		 * Suppress compiler optimization so that orb[1] must be written first.
2857170724Ssimokawa		 * XXX We may need an explicit memory barrier for other architectures
2858170724Ssimokawa		 * other than i386/amd64.
2859170724Ssimokawa		 */
2860170724Ssimokawa		*(volatile uint32_t *)&prev2->orb[1] = htonl(ocb->bus_addr);
2861170724Ssimokawa		*(volatile uint32_t *)&prev2->orb[0] = 0;
2862103285Sikob	}
2863103285Sikob	splx(s);
2864103285Sikob
2865103285Sikob	return prev;
2866103285Sikob}
2867103285Sikob
2868103285Sikobstatic struct sbp_ocb *
2869113584Ssimokawasbp_get_ocb(struct sbp_dev *sdev)
2870103285Sikob{
2871103285Sikob	struct sbp_ocb *ocb;
2872103285Sikob	int s = splfw();
2873170374Ssimokawa
2874170374Ssimokawa	mtx_assert(&sdev->target->sbp->mtx, MA_OWNED);
2875113584Ssimokawa	ocb = STAILQ_FIRST(&sdev->free_ocbs);
2876103285Sikob	if (ocb == NULL) {
2877127468Ssimokawa		sdev->flags |= ORB_SHORTAGE;
2878103285Sikob		printf("ocb shortage!!!\n");
2879134868Ssimokawa		splx(s);
2880103285Sikob		return NULL;
2881103285Sikob	}
2882113584Ssimokawa	STAILQ_REMOVE_HEAD(&sdev->free_ocbs, ocb);
2883103285Sikob	splx(s);
2884103285Sikob	ocb->ccb = NULL;
2885103285Sikob	return (ocb);
2886103285Sikob}
2887103285Sikob
2888103285Sikobstatic void
2889113584Ssimokawasbp_free_ocb(struct sbp_dev *sdev, struct sbp_ocb *ocb)
2890103285Sikob{
2891103285Sikob	ocb->flags = 0;
2892103285Sikob	ocb->ccb = NULL;
2893170374Ssimokawa
2894170374Ssimokawa	SBP_LOCK(sdev->target->sbp);
2895113584Ssimokawa	STAILQ_INSERT_TAIL(&sdev->free_ocbs, ocb, ocb);
2896127468Ssimokawa	if ((sdev->flags & ORB_SHORTAGE) != 0) {
2897127468Ssimokawa		int count;
2898127468Ssimokawa
2899127468Ssimokawa		sdev->flags &= ~ORB_SHORTAGE;
2900127468Ssimokawa		count = sdev->freeze;
2901127468Ssimokawa		sdev->freeze = 0;
2902127468Ssimokawa		xpt_release_devq(sdev->path, count, TRUE);
2903127468Ssimokawa	}
2904170374Ssimokawa	SBP_UNLOCK(sdev->target->sbp);
2905103285Sikob}
2906103285Sikob
2907103285Sikobstatic void
2908103285Sikobsbp_abort_ocb(struct sbp_ocb *ocb, int status)
2909103285Sikob{
2910103285Sikob	struct sbp_dev *sdev;
2911103285Sikob
2912103285Sikob	sdev = ocb->sdev;
2913113584SsimokawaSBP_DEBUG(0)
2914188756Ssbruno	device_printf(sdev->target->sbp->fd.dev,
2915127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
2916188756Ssbruno	"%s:%s 0x%x\n", __func__, sdev->bustgtlun, ocb->bus_addr);
2917127468Ssimokawa#else
2918188756Ssbruno	"%s:%s 0x%jx\n", __func__, sdev->bustgtlun, (uintmax_t)ocb->bus_addr);
2919111819Ssimokawa#endif
2920113584SsimokawaEND_DEBUG
2921113584SsimokawaSBP_DEBUG(1)
2922105792Ssimokawa	if (ocb->ccb != NULL)
2923105792Ssimokawa		sbp_print_scsi_cmd(ocb);
2924103285SikobEND_DEBUG
2925113584Ssimokawa	if (ntohl(ocb->orb[4]) & 0xffff) {
2926113584Ssimokawa		bus_dmamap_sync(sdev->target->sbp->dmat, ocb->dmamap,
2927113584Ssimokawa			(ntohl(ocb->orb[4]) & ORB_CMD_IN) ?
2928113584Ssimokawa			BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2929113584Ssimokawa		bus_dmamap_unload(sdev->target->sbp->dmat, ocb->dmamap);
2930113584Ssimokawa	}
2931111819Ssimokawa	if (ocb->ccb != NULL) {
2932110336Ssimokawa		untimeout(sbp_timeout, (caddr_t)ocb,
2933110336Ssimokawa					ocb->ccb->ccb_h.timeout_ch);
2934103285Sikob		ocb->ccb->ccb_h.status = status;
2935170374Ssimokawa		SBP_LOCK(sdev->target->sbp);
2936103285Sikob		xpt_done(ocb->ccb);
2937170374Ssimokawa		SBP_UNLOCK(sdev->target->sbp);
2938103285Sikob	}
2939113584Ssimokawa	sbp_free_ocb(sdev, ocb);
2940103285Sikob}
2941103285Sikob
2942103285Sikobstatic void
2943103285Sikobsbp_abort_all_ocbs(struct sbp_dev *sdev, int status)
2944103285Sikob{
2945103285Sikob	int s;
2946105792Ssimokawa	struct sbp_ocb *ocb, *next;
2947105792Ssimokawa	STAILQ_HEAD(, sbp_ocb) temp;
2948103285Sikob
2949103285Sikob	s = splfw();
2950105792Ssimokawa
2951170807Ssimokawa	STAILQ_INIT(&temp);
2952170807Ssimokawa	SBP_LOCK(sdev->target->sbp);
2953170807Ssimokawa	STAILQ_CONCAT(&temp, &sdev->ocbs);
2954105792Ssimokawa	STAILQ_INIT(&sdev->ocbs);
2955170807Ssimokawa	SBP_UNLOCK(sdev->target->sbp);
2956170807Ssimokawa
2957105792Ssimokawa	for (ocb = STAILQ_FIRST(&temp); ocb != NULL; ocb = next) {
2958105792Ssimokawa		next = STAILQ_NEXT(ocb, ocb);
2959103285Sikob		sbp_abort_ocb(ocb, status);
2960103285Sikob	}
2961127468Ssimokawa	if (sdev->last_ocb != NULL) {
2962127468Ssimokawa		sbp_free_ocb(sdev, sdev->last_ocb);
2963127468Ssimokawa		sdev->last_ocb = NULL;
2964127468Ssimokawa	}
2965105792Ssimokawa
2966103285Sikob	splx(s);
2967103285Sikob}
2968103285Sikob
2969103285Sikobstatic devclass_t sbp_devclass;
2970103285Sikob
2971103285Sikobstatic device_method_t sbp_methods[] = {
2972103285Sikob	/* device interface */
2973103285Sikob	DEVMETHOD(device_identify,	sbp_identify),
2974103285Sikob	DEVMETHOD(device_probe,		sbp_probe),
2975103285Sikob	DEVMETHOD(device_attach,	sbp_attach),
2976103285Sikob	DEVMETHOD(device_detach,	sbp_detach),
2977110145Ssimokawa	DEVMETHOD(device_shutdown,	sbp_shutdown),
2978103285Sikob
2979103285Sikob	{ 0, 0 }
2980103285Sikob};
2981103285Sikob
2982103285Sikobstatic driver_t sbp_driver = {
2983103285Sikob	"sbp",
2984103285Sikob	sbp_methods,
2985103285Sikob	sizeof(struct sbp_softc),
2986103285Sikob};
2987127468Ssimokawa#ifdef __DragonFly__
2988127468SsimokawaDECLARE_DUMMY_MODULE(sbp);
2989127468Ssimokawa#endif
2990103285SikobDRIVER_MODULE(sbp, firewire, sbp_driver, sbp_devclass, 0, 0);
2991103285SikobMODULE_VERSION(sbp, 1);
2992103285SikobMODULE_DEPEND(sbp, firewire, 1, 1, 1);
2993103285SikobMODULE_DEPEND(sbp, cam, 1, 1, 1);
2994