150477Speter/* $FreeBSD$ */
2139749Simp/*-
380313Smjacob * Qlogic ISP SCSI Host Adapter FreeBSD Wrapper Definitions
435388Smjacob *
5196008Smjacob * Copyright (c) 1997-2008 by Matthew Jacob
6154704Smjacob * All rights reserved.
7154704Smjacob *
835388Smjacob * Redistribution and use in source and binary forms, with or without
935388Smjacob * modification, are permitted provided that the following conditions
1035388Smjacob * are met:
1135388Smjacob * 1. Redistributions of source code must retain the above copyright
1235388Smjacob *    notice immediately at the beginning of the file, without modification,
1335388Smjacob *    this list of conditions, and the following disclaimer.
1466189Smjacob * 2. The name of the author may not be used to endorse or promote products
1566189Smjacob *    derived from this software without specific prior written permission.
1635388Smjacob *
1735388Smjacob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1835388Smjacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1935388Smjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2035388Smjacob * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
2135388Smjacob * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2235388Smjacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2335388Smjacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2435388Smjacob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2535388Smjacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2635388Smjacob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2735388Smjacob * SUCH DAMAGE.
2835388Smjacob */
2935388Smjacob#ifndef	_ISP_FREEBSD_H
3035388Smjacob#define	_ISP_FREEBSD_H
3135388Smjacob
3239445Smjacob#include <sys/param.h>
3348487Smjacob#include <sys/systm.h>
3495533Smike#include <sys/endian.h>
3574914Sjhb#include <sys/lock.h>
36157943Smjacob#include <sys/kernel.h>
37157943Smjacob#include <sys/queue.h>
3859452Smjacob#include <sys/malloc.h>
3967365Sjhb#include <sys/mutex.h>
4077365Smjacob#include <sys/condvar.h>
41227126Smjacob#include <sys/sysctl.h>
42157943Smjacob
4362496Smjacob#include <sys/proc.h>
4473246Smjacob#include <sys/bus.h>
45224804Smjacob#include <sys/taskqueue.h>
4639445Smjacob
4748487Smjacob#include <machine/bus.h>
4862496Smjacob#include <machine/cpu.h>
49196008Smjacob#include <machine/stdarg.h>
5039445Smjacob
5148487Smjacob#include <cam/cam.h>
5248487Smjacob#include <cam/cam_debug.h>
5348487Smjacob#include <cam/cam_ccb.h>
5448487Smjacob#include <cam/cam_sim.h>
5548487Smjacob#include <cam/cam_xpt.h>
5648487Smjacob#include <cam/cam_xpt_sim.h>
5748487Smjacob#include <cam/cam_debug.h>
5848487Smjacob#include <cam/scsi/scsi_all.h>
5948487Smjacob#include <cam/scsi/scsi_message.h>
6048487Smjacob
6152348Smjacob#include "opt_ddb.h"
6248487Smjacob#include "opt_isp.h"
63102272Smjacob
64196008Smjacob#define	ISP_PLATFORM_VERSION_MAJOR	7
65237537Smjacob#define	ISP_PLATFORM_VERSION_MINOR	10
66158656Smjacob
6787635Smjacob/*
6887635Smjacob * Efficiency- get rid of SBus code && tests unless we need them.
6987635Smjacob */
70153072Sru#ifdef __sparc64__
7187635Smjacob#define	ISP_SBUS_SUPPORTED	1
7287635Smjacob#else
7387635Smjacob#define	ISP_SBUS_SUPPORTED	0
7487635Smjacob#endif
7539235Sgibbs
76100680Smjacob#define	ISP_IFLAGS	INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
77100680Smjacob
78238869Smjacob#define	N_XCMDS		64
79238869Smjacob#define	XCMD_SIZE	512
80238869Smjacobstruct ispsoftc;
81238869Smjacobtypedef union isp_ecmd {
82238869Smjacob	union isp_ecmd *	next;
83238869Smjacob	uint8_t			data[XCMD_SIZE];
84238869Smjacob} isp_ecmd_t;
85238869Smjacobisp_ecmd_t *	isp_get_ecmd(struct ispsoftc *);
86238869Smjacobvoid		isp_put_ecmd(struct ispsoftc *, isp_ecmd_t *);
87238869Smjacob
8855366Smjacob#ifdef	ISP_TARGET_MODE
89228914Smjacob/* Not quite right, but there was no bump for this change */
90228914Smjacob#if __FreeBSD_version < 225469
91228914Smjacob#define	SDFIXED(x)	(&x)
92228914Smjacob#else
93228914Smjacob#define	SDFIXED(x)	((struct scsi_sense_data_fixed *)(&x))
94228914Smjacob#endif
95228914Smjacob
9687635Smjacob#define	ISP_TARGET_FUNCTIONS	1
97196008Smjacob#define	ATPDPSIZE	4096
98196008Smjacob
99196008Smjacob#include <dev/isp/isp_target.h>
100196008Smjacob
10184242Smjacobtypedef struct {
102196008Smjacob	void *		next;
103155704Smjacob	uint32_t	orig_datalen;
104155704Smjacob	uint32_t	bytes_xfered;
105239143Smjacob	uint32_t	bytes_in_transit;
106237537Smjacob	uint32_t	tag;		/* typically f/w RX_ID */
107196008Smjacob	uint32_t	lun;
108196008Smjacob	uint32_t	nphdl;
109196008Smjacob	uint32_t	sid;
110196008Smjacob	uint32_t	portid;
111237537Smjacob	uint16_t	rxid;	/* wire rxid */
112237537Smjacob	uint16_t	oxid;	/* wire oxid */
113238869Smjacob	uint16_t	word3;	/* PRLI word3 params */
114238869Smjacob	uint16_t	ctcnt;	/* number of CTIOs currently active */
115239143Smjacob	uint8_t		seqno;	/* CTIO sequence number */
116196008Smjacob	uint32_t
117238869Smjacob			srr_notify_rcvd	: 1,
118238869Smjacob			cdb0		: 8,
119238869Smjacob			sendst		: 1,
120238869Smjacob			dead		: 1,
121238869Smjacob			tattr		: 3,
122238869Smjacob			state		: 3;
123238869Smjacob	void *		ests;
124238869Smjacob	/*
125238869Smjacob	 * The current SRR notify copy
126238869Smjacob	 */
127238869Smjacob	uint8_t		srr[64];	/*  sb QENTRY_LEN, but order of definitions is wrong */
128238869Smjacob	void *		srr_ccb;
129238869Smjacob	uint32_t	nsrr;
13084242Smjacob} atio_private_data_t;
13198288Smjacob#define	ATPD_STATE_FREE			0
13298288Smjacob#define	ATPD_STATE_ATIO			1
13398288Smjacob#define	ATPD_STATE_CAM			2
13498288Smjacob#define	ATPD_STATE_CTIO			3
13598288Smjacob#define	ATPD_STATE_LAST_CTIO		4
13698288Smjacob#define	ATPD_STATE_PDON			5
13784242Smjacob
138239143Smjacob#define	ATPD_CCB_OUTSTANDING		16
139239143Smjacob
140239143Smjacob#define	ATPD_SEQ_MASK			0x7f
141239143Smjacob#define	ATPD_SEQ_NOTIFY_CAM		0x80
142239143Smjacob#define	ATPD_SET_SEQNO(hdrp, atp)	((isphdr_t *)hdrp)->rqs_seqno &= ~ATPD_SEQ_MASK, ((isphdr_t *)hdrp)->rqs_seqno |= (atp)->seqno
143239143Smjacob#define	ATPD_GET_SEQNO(hdrp)		(((isphdr_t *)hdrp)->rqs_seqno & ATPD_SEQ_MASK)
144239143Smjacob#define	ATPD_GET_NCAM(hdrp)		((((isphdr_t *)hdrp)->rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0)
145239143Smjacob
146196008Smjacobtypedef union inot_private_data inot_private_data_t;
147196008Smjacobunion inot_private_data {
148196008Smjacob	inot_private_data_t *next;
149196008Smjacob	struct {
150196008Smjacob		isp_notify_t nt;	/* must be first! */
151196008Smjacob		uint8_t data[64];	/* sb QENTRY_LEN, but order of definitions is wrong */
152196008Smjacob		uint32_t tag_id, seq_id;
153196008Smjacob	} rd;
154196008Smjacob};
155238869Smjacobtypedef struct isp_timed_notify_ack {
156238869Smjacob	void *isp;
157238869Smjacob	void *not;
158238869Smjacob	uint8_t data[64];	 /* sb QENTRY_LEN, but order of definitions is wrong */
159238869Smjacob} isp_tna_t;
160196008Smjacob
161239143SmjacobTAILQ_HEAD(isp_ccbq, ccb_hdr);
16255366Smjacobtypedef struct tstate {
163196008Smjacob	SLIST_ENTRY(tstate) next;
16455366Smjacob	struct cam_path *owner;
165239143Smjacob	struct isp_ccbq waitq;		/* waiting CCBs */
16655366Smjacob	struct ccb_hdr_slist atios;
16755366Smjacob	struct ccb_hdr_slist inots;
168155704Smjacob	uint32_t hold;
169236427Smjacob	uint32_t
170236427Smjacob		enabled		: 1,
171236427Smjacob		atio_count	: 15,
172236427Smjacob		inot_count	: 15;
173196008Smjacob	inot_private_data_t *	restart_queue;
174196008Smjacob	inot_private_data_t *	ntfree;
175196008Smjacob	inot_private_data_t	ntpool[ATPDPSIZE];
176196008Smjacob	atio_private_data_t *	atfree;
177196008Smjacob	atio_private_data_t	atpool[ATPDPSIZE];
17855366Smjacob} tstate_t;
17955366Smjacob
180196008Smjacob#define	LUN_HASH_SIZE		32
181196008Smjacob#define	LUN_HASH_FUNC(lun)	((lun) & (LUN_HASH_SIZE - 1))
182196008Smjacob
18355366Smjacob#endif
18455366Smjacob
185169292Smjacob/*
186169292Smjacob * Per command info.
187169292Smjacob */
188169292Smjacobstruct isp_pcmd {
189169292Smjacob	struct isp_pcmd *	next;
190238869Smjacob	bus_dmamap_t 		dmap;		/* dma map for this command */
191238869Smjacob	struct ispsoftc *	isp;		/* containing isp */
192238869Smjacob	struct callout		wdog;		/* watchdog timer */
193239143Smjacob	uint32_t		datalen;	/* data length for this command (target mode only) */
194238869Smjacob	uint8_t			totslen;	/* sense length on status response */
195238869Smjacob	uint8_t			cumslen;	/* sense length on status response */
196238869Smjacob	uint8_t 		crn;		/* command reference number */
197169292Smjacob};
198169292Smjacob#define	ISP_PCMD(ccb)		(ccb)->ccb_h.spriv_ptr1
199169292Smjacob#define	PISP_PCMD(ccb)		((struct isp_pcmd *)ISP_PCMD(ccb))
200169292Smjacob
201196008Smjacob/*
202238869Smjacob * Per nexus info.
203238869Smjacob */
204238869Smjacobstruct isp_nexus {
205238869Smjacob	struct isp_nexus *	next;
206238869Smjacob	uint32_t
207238869Smjacob		crnseed	:	8;	/* next command reference number */
208238869Smjacob	uint32_t
209238869Smjacob		tgt	:	16,	/* TGT for target */
210238869Smjacob		lun	:	16;	/* LUN for target */
211238869Smjacob};
212238869Smjacob#define	NEXUS_HASH_WIDTH	32
213238869Smjacob#define	INITIAL_NEXUS_COUNT	MAX_FC_TARG
214238869Smjacob#define	NEXUS_HASH(tgt, lun)	((tgt + lun) % NEXUS_HASH_WIDTH)
215238869Smjacob
216238869Smjacob/*
217196008Smjacob * Per channel information
218196008Smjacob */
219196008SmjacobSLIST_HEAD(tslist, tstate);
220196008Smjacob
221196008Smjacobstruct isp_fc {
222196008Smjacob	struct cam_sim *sim;
223196008Smjacob	struct cam_path *path;
224196008Smjacob	struct ispsoftc *isp;
225196008Smjacob	struct proc *kproc;
226196008Smjacob	bus_dma_tag_t tdmat;
227196008Smjacob	bus_dmamap_t tdmap;
228196008Smjacob	uint64_t def_wwpn;
229196008Smjacob	uint64_t def_wwnn;
230196008Smjacob	uint32_t loop_down_time;
231196008Smjacob	uint32_t loop_down_limit;
232196008Smjacob	uint32_t gone_device_time;
233238869Smjacob	/*
234238869Smjacob	 * Per target/lun info- just to keep a per-ITL nexus crn count
235238869Smjacob	 */
236238869Smjacob	struct isp_nexus *nexus_hash[NEXUS_HASH_WIDTH];
237238869Smjacob	struct isp_nexus *nexus_free_list;
238196008Smjacob	uint32_t
239196008Smjacob#ifdef	ISP_TARGET_MODE
240196008Smjacob#ifdef	ISP_INTERNAL_TARGET
241196008Smjacob		proc_active	: 1,
242196008Smjacob#endif
243196008Smjacob		tm_luns_enabled	: 1,
244196008Smjacob		tm_enable_defer	: 1,
245196008Smjacob		tm_enabled	: 1,
246196008Smjacob#endif
247163899Smjacob		simqfrozen	: 3,
248196008Smjacob		default_id	: 8,
249163899Smjacob		hysteresis	: 8,
250205236Smjacob		def_role	: 2,	/* default role */
251164272Smjacob		gdt_running	: 1,
252196008Smjacob		loop_dead	: 1,
253200089Smjacob		fcbsy		: 1,
254200089Smjacob		ready		: 1;
255196008Smjacob	struct callout ldt;	/* loop down timer */
256196008Smjacob	struct callout gdt;	/* gone device timer */
257224804Smjacob	struct task ltask;
258224804Smjacob	struct task gtask;
259196008Smjacob#ifdef	ISP_TARGET_MODE
260196008Smjacob	struct tslist lun_hash[LUN_HASH_SIZE];
261196008Smjacob#ifdef	ISP_INTERNAL_TARGET
262196008Smjacob	struct proc *		target_proc;
263196008Smjacob#endif
264238869Smjacob#if defined(DEBUG)
265238869Smjacob	unsigned int inject_lost_data_frame;
266196008Smjacob#endif
267238869Smjacob#endif
268196008Smjacob};
269196008Smjacob
270196008Smjacobstruct isp_spi {
271196008Smjacob	struct cam_sim *sim;
272196008Smjacob	struct cam_path *path;
273196008Smjacob	uint32_t
274196008Smjacob#ifdef	ISP_TARGET_MODE
275196008Smjacob#ifdef	ISP_INTERNAL_TARGET
276196008Smjacob		proc_active	: 1,
277196008Smjacob#endif
278196008Smjacob		tm_luns_enabled	: 1,
279196008Smjacob		tm_enable_defer	: 1,
280196008Smjacob		tm_enabled	: 1,
281196008Smjacob#endif
282196008Smjacob		simqfrozen	: 3,
283205236Smjacob		def_role	: 2,
284196008Smjacob		iid		: 4;
285196008Smjacob#ifdef	ISP_TARGET_MODE
286196008Smjacob	struct tslist lun_hash[LUN_HASH_SIZE];
287196008Smjacob#ifdef	ISP_INTERNAL_TARGET
288196008Smjacob	struct proc *		target_proc;
289196008Smjacob#endif
290196008Smjacob#endif
291196008Smjacob};
292196008Smjacob
293196008Smjacobstruct isposinfo {
294196008Smjacob	/*
295196008Smjacob	 * Linkage, locking, and identity
296196008Smjacob	 */
297169292Smjacob	struct mtx		lock;
298196008Smjacob	device_t		dev;
299196008Smjacob	struct cdev *		cdev;
300196008Smjacob	struct intr_config_hook	ehook;
301196008Smjacob	struct cam_devq *	devq;
302196008Smjacob
303196008Smjacob	/*
304196008Smjacob	 * Firmware pointer
305196008Smjacob	 */
306166756Sluigi	const struct firmware *	fw;
307196008Smjacob
308196008Smjacob	/*
309196008Smjacob	 * DMA related sdtuff
310196008Smjacob	 */
311196008Smjacob	bus_space_tag_t		bus_tag;
312196008Smjacob	bus_dma_tag_t		dmat;
313196008Smjacob	bus_space_handle_t	bus_handle;
31493706Smjacob	bus_dma_tag_t		cdmat;
31593706Smjacob	bus_dmamap_t		cdmap;
316196008Smjacob
317169292Smjacob	/*
318196008Smjacob	 * Command and transaction related related stuff
319169292Smjacob	 */
320169292Smjacob	struct isp_pcmd *	pcmd_pool;
321169292Smjacob	struct isp_pcmd *	pcmd_free;
322169292Smjacob
323196008Smjacob	uint32_t
32455366Smjacob#ifdef	ISP_TARGET_MODE
325196008Smjacob		tmwanted	: 1,
326196008Smjacob		tmbusy		: 1,
327196008Smjacob#else
328196008Smjacob				: 2,
32955366Smjacob#endif
330238869Smjacob		sixtyfourbit	: 1,	/* sixtyfour bit platform */
331196008Smjacob		timer_active	: 1,
332196008Smjacob		autoconf	: 1,
333196008Smjacob		ehook_active	: 1,
334196008Smjacob		disabled	: 1,
335196008Smjacob		mbox_sleeping	: 1,
336196008Smjacob		mbox_sleep_ok	: 1,
337196008Smjacob		mboxcmd_done	: 1,
338196008Smjacob		mboxbsy		: 1;
339196008Smjacob
340196008Smjacob	struct callout		tmo;	/* general timer */
341196008Smjacob
342196008Smjacob	/*
343196008Smjacob	 * misc- needs to be sorted better XXXXXX
344196008Smjacob	 */
345196008Smjacob	int			framesize;
346196008Smjacob	int			exec_throttle;
347196008Smjacob	int			cont_max;
348196008Smjacob
349196008Smjacob#ifdef	ISP_TARGET_MODE
350196008Smjacob	cam_status *		rptr;
351196008Smjacob#endif
352196008Smjacob
353238869Smjacob	bus_addr_t		ecmd_dma;
354238869Smjacob	isp_ecmd_t *		ecmd_base;
355238869Smjacob	isp_ecmd_t *		ecmd_free;
356238869Smjacob
357196008Smjacob	/*
358196008Smjacob	 * Per-type private storage...
359196008Smjacob	 */
360196008Smjacob	union {
361196008Smjacob		struct isp_fc *fc;
362196008Smjacob		struct isp_spi *spi;
363196008Smjacob		void *ptr;
364196008Smjacob	} pc;
36535388Smjacob};
366196008Smjacob#define	ISP_FC_PC(isp, chan)	(&(isp)->isp_osinfo.pc.fc[(chan)])
367196008Smjacob#define	ISP_SPI_PC(isp, chan)	(&(isp)->isp_osinfo.pc.spi[(chan)])
368196008Smjacob#define	ISP_GET_PC(isp, chan, tag, rslt)		\
369196008Smjacob	if (IS_SCSI(isp)) {				\
370196008Smjacob		rslt = ISP_SPI_PC(isp, chan)-> tag;	\
371196008Smjacob	} else {					\
372196008Smjacob		rslt = ISP_FC_PC(isp, chan)-> tag;	\
373196008Smjacob	}
374196008Smjacob#define	ISP_GET_PC_ADDR(isp, chan, tag, rp)		\
375196008Smjacob	if (IS_SCSI(isp)) {				\
376196008Smjacob		rp = &ISP_SPI_PC(isp, chan)-> tag;	\
377196008Smjacob	} else {					\
378196008Smjacob		rp = &ISP_FC_PC(isp, chan)-> tag;	\
379196008Smjacob	}
380196008Smjacob#define	ISP_SET_PC(isp, chan, tag, val)			\
381196008Smjacob	if (IS_SCSI(isp)) {				\
382196008Smjacob		ISP_SPI_PC(isp, chan)-> tag = val;	\
383196008Smjacob	} else {					\
384196008Smjacob		ISP_FC_PC(isp, chan)-> tag = val;	\
385196008Smjacob	}
38639235Sgibbs
387238869Smjacob#define	FCP_NEXT_CRN	isp_fcp_next_crn
38877365Smjacob#define	isp_lock	isp_osinfo.lock
389167501Smjacob#define	isp_bus_tag	isp_osinfo.bus_tag
390167501Smjacob#define	isp_bus_handle	isp_osinfo.bus_handle
39177365Smjacob
39264092Smjacob/*
39369525Smjacob * Locking macros...
39469525Smjacob */
395169292Smjacob#define	ISP_LOCK(isp)	mtx_lock(&isp->isp_osinfo.lock)
396169292Smjacob#define	ISP_UNLOCK(isp)	mtx_unlock(&isp->isp_osinfo.lock)
39769525Smjacob
39869525Smjacob/*
39964092Smjacob * Required Macros/Defines
40064092Smjacob */
401196008Smjacob#define	ISP_FC_SCRLEN		0x1000
40245284Smjacob
403196008Smjacob#define	ISP_MEMZERO(a, b)	memset(a, 0, b)
404196008Smjacob#define	ISP_MEMCPY		memcpy
405196008Smjacob#define	ISP_SNPRINTF		snprintf
406196008Smjacob#define	ISP_DELAY		DELAY
407196008Smjacob#define	ISP_SLEEP(isp, x)	DELAY(x)
40835388Smjacob
409219098Smjacob#define	ISP_MIN			imin
410219098Smjacob
411196008Smjacob#ifndef	DIAGNOSTIC
412196008Smjacob#define	ISP_INLINE		__inline
413196008Smjacob#else
414196008Smjacob#define	ISP_INLINE
415196008Smjacob#endif
416196008Smjacob
41764092Smjacob#define	NANOTIME_T		struct timespec
41864092Smjacob#define	GET_NANOTIME		nanotime
41964092Smjacob#define	GET_NANOSEC(x)		((x)->tv_sec * 1000000000 + (x)->tv_nsec)
420164272Smjacob#define	NANOTIME_SUB		isp_nanotime_sub
42162496Smjacob
42299598Smjacob#define	MAXISPREQUEST(isp)	((IS_FC(isp) || IS_ULTRA2(isp))? 1024 : 256)
42362496Smjacob
424218691Smarius#define	MEMORYBARRIER(isp, type, offset, size, chan)		\
42593706Smjacobswitch (type) {							\
42693706Smjacobcase SYNC_SFORDEV:						\
427218691Smarius{								\
428218691Smarius	struct isp_fc *fc = ISP_FC_PC(isp, chan);		\
429218691Smarius	bus_dmamap_sync(fc->tdmat, fc->tdmap,			\
430218691Smarius	   BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);		\
431218691Smarius	break;							\
432218691Smarius}								\
43393706Smjacobcase SYNC_REQUEST:						\
434196008Smjacob	bus_dmamap_sync(isp->isp_osinfo.cdmat,			\
435196008Smjacob	   isp->isp_osinfo.cdmap, 				\
43693706Smjacob	   BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);		\
43793706Smjacob	break;							\
43893706Smjacobcase SYNC_SFORCPU:						\
439218691Smarius{								\
440218691Smarius	struct isp_fc *fc = ISP_FC_PC(isp, chan);		\
441218691Smarius	bus_dmamap_sync(fc->tdmat, fc->tdmap,			\
442218691Smarius	   BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);	\
443218691Smarius	break;							\
444218691Smarius}								\
44593706Smjacobcase SYNC_RESULT:						\
446196008Smjacob	bus_dmamap_sync(isp->isp_osinfo.cdmat, 			\
447196008Smjacob	   isp->isp_osinfo.cdmap,				\
44893706Smjacob	   BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);	\
44993706Smjacob	break;							\
450167501Smjacobcase SYNC_REG:							\
451196008Smjacob	bus_space_barrier(isp->isp_osinfo.bus_tag,		\
452196008Smjacob	    isp->isp_osinfo.bus_handle, offset, size,		\
453219282Smjacob	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);	\
454167501Smjacob	break;							\
45593706Smjacobdefault:							\
45693706Smjacob	break;							\
45793706Smjacob}
45862496Smjacob
459163899Smjacob#define	MBOX_ACQUIRE			isp_mbox_acquire
46064092Smjacob#define	MBOX_WAIT_COMPLETE		isp_mbox_wait_complete
461164272Smjacob#define	MBOX_NOTIFY_COMPLETE		isp_mbox_notify_done
462163899Smjacob#define	MBOX_RELEASE			isp_mbox_release
46362496Smjacob
464196008Smjacob#define	FC_SCRATCH_ACQUIRE		isp_fc_scratch_acquire
465196008Smjacob#define	FC_SCRATCH_RELEASE(isp, chan)	isp->isp_osinfo.pc.fc[chan].fcbsy = 0
46690224Smjacob
46764092Smjacob#ifndef	SCSI_GOOD
46864092Smjacob#define	SCSI_GOOD	SCSI_STATUS_OK
46964092Smjacob#endif
47064092Smjacob#ifndef	SCSI_CHECK
47164092Smjacob#define	SCSI_CHECK	SCSI_STATUS_CHECK_COND
47264092Smjacob#endif
47364092Smjacob#ifndef	SCSI_BUSY
47464092Smjacob#define	SCSI_BUSY	SCSI_STATUS_BUSY
47564092Smjacob#endif
47664092Smjacob#ifndef	SCSI_QFULL
47764092Smjacob#define	SCSI_QFULL	SCSI_STATUS_QUEUE_FULL
47864092Smjacob#endif
47964092Smjacob
48064092Smjacob#define	XS_T			struct ccb_scsiio
481155704Smjacob#define	XS_DMA_ADDR_T		bus_addr_t
482196008Smjacob#define XS_GET_DMA64_SEG(a, b, c)		\
483196008Smjacob{						\
484196008Smjacob	ispds64_t *d = a;			\
485196008Smjacob	bus_dma_segment_t *e = b;		\
486196008Smjacob	uint32_t f = c;				\
487196008Smjacob	e += f;					\
488196008Smjacob        d->ds_base = DMA_LO32(e->ds_addr);	\
489196008Smjacob        d->ds_basehi = DMA_HI32(e->ds_addr);	\
490196008Smjacob        d->ds_count = e->ds_len;		\
491196008Smjacob}
492196008Smjacob#define XS_GET_DMA_SEG(a, b, c)			\
493196008Smjacob{						\
494196008Smjacob	ispds_t *d = a;				\
495196008Smjacob	bus_dma_segment_t *e = b;		\
496196008Smjacob	uint32_t f = c;				\
497196008Smjacob	e += f;					\
498196008Smjacob        d->ds_base = DMA_LO32(e->ds_addr);	\
499196008Smjacob        d->ds_count = e->ds_len;		\
500196008Smjacob}
501169292Smjacob#define	XS_ISP(ccb)		cam_sim_softc(xpt_path_sim((ccb)->ccb_h.path))
50264092Smjacob#define	XS_CHANNEL(ccb)		cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path))
50364092Smjacob#define	XS_TGT(ccb)		(ccb)->ccb_h.target_id
50464092Smjacob#define	XS_LUN(ccb)		(ccb)->ccb_h.target_lun
50535388Smjacob
50664092Smjacob#define	XS_CDBP(ccb)	\
50764092Smjacob	(((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \
50864092Smjacob	 (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes)
50964092Smjacob
51064092Smjacob#define	XS_CDBLEN(ccb)		(ccb)->cdb_len
51148487Smjacob#define	XS_XFRLEN(ccb)		(ccb)->dxfer_len
51248487Smjacob#define	XS_TIME(ccb)		(ccb)->ccb_h.timeout
513196008Smjacob#define	XS_GET_RESID(ccb)	(ccb)->resid
514196008Smjacob#define	XS_SET_RESID(ccb, r)	(ccb)->resid = r
51564092Smjacob#define	XS_STSP(ccb)		(&(ccb)->scsi_status)
51648487Smjacob#define	XS_SNSP(ccb)		(&(ccb)->sense_data)
51764092Smjacob
518238869Smjacob#define	XS_TOT_SNSLEN(ccb)	ccb->sense_len
519238869Smjacob#define	XS_CUR_SNSLEN(ccb)	(ccb->sense_len - ccb->sense_resid)
52064092Smjacob
521225950Sken#define	XS_SNSKEY(ccb)		(scsi_get_sense_key(&(ccb)->sense_data, \
522238869Smjacob				 ccb->sense_len - ccb->sense_resid, 1))
523225950Sken
524225950Sken#define	XS_SNSASC(ccb)		(scsi_get_asc(&(ccb)->sense_data,	\
525238869Smjacob				 ccb->sense_len - ccb->sense_resid, 1))
526225950Sken
527225950Sken#define	XS_SNSASCQ(ccb)		(scsi_get_ascq(&(ccb)->sense_data,	\
528238869Smjacob				 ccb->sense_len - ccb->sense_resid, 1))
52964092Smjacob#define	XS_TAG_P(ccb)	\
53064092Smjacob	(((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \
53164092Smjacob	 (ccb)->tag_action != CAM_TAG_ACTION_NONE)
53235388Smjacob
53364092Smjacob#define	XS_TAG_TYPE(ccb)	\
53464092Smjacob	((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \
53564092Smjacob	 ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG))
53664092Smjacob
53764092Smjacob
53864092Smjacob#define	XS_SETERR(ccb, v)	(ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \
539238869Smjacob				(ccb)->ccb_h.status |= v
54064092Smjacob
54164092Smjacob#	define	HBA_NOERROR		CAM_REQ_INPROG
54264092Smjacob#	define	HBA_BOTCH		CAM_UNREC_HBA_ERROR
54364092Smjacob#	define	HBA_CMDTIMEOUT		CAM_CMD_TIMEOUT
54464092Smjacob#	define	HBA_SELTIMEOUT		CAM_SEL_TIMEOUT
54564092Smjacob#	define	HBA_TGTBSY		CAM_SCSI_STATUS_ERROR
54664092Smjacob#	define	HBA_BUSRESET		CAM_SCSI_BUS_RESET
54764092Smjacob#	define	HBA_ABORTED		CAM_REQ_ABORTED
54864092Smjacob#	define	HBA_DATAOVR		CAM_DATA_RUN_ERR
54964092Smjacob#	define	HBA_ARQFAIL		CAM_AUTOSENSE_FAIL
55064092Smjacob
55164092Smjacob
55264092Smjacob#define	XS_ERR(ccb)		((ccb)->ccb_h.status & CAM_STATUS_MASK)
55364092Smjacob
554238869Smjacob#define	XS_NOERR(ccb)		(((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
55564092Smjacob
556238869Smjacob#define	XS_INITERR(ccb)		XS_SETERR(ccb, CAM_REQ_INPROG), ccb->sense_resid = ccb->sense_len
55764092Smjacob
558238869Smjacob#define	XS_SAVE_SENSE(xs, sense_ptr, totslen, slen)	do {			\
559238869Smjacob		uint32_t tlen = slen;						\
560238869Smjacob		if (tlen > (xs)->sense_len)					\
561238869Smjacob			tlen = (xs)->sense_len;					\
562238869Smjacob		PISP_PCMD(xs)->totslen = imin((xs)->sense_len, totslen);	\
563238869Smjacob		PISP_PCMD(xs)->cumslen = tlen;					\
564238869Smjacob		memcpy(&(xs)->sense_data, sense_ptr, tlen);			\
565238869Smjacob		(xs)->sense_resid = (xs)->sense_len - tlen;			\
566238869Smjacob		(xs)->ccb_h.status |= CAM_AUTOSNS_VALID;			\
567238869Smjacob	} while (0)
56864092Smjacob
569238869Smjacob#define	XS_SENSE_APPEND(xs, xsnsp, xsnsl)	do {				\
570238869Smjacob		uint32_t off = PISP_PCMD(xs)->cumslen;				\
571238869Smjacob		uint8_t *ptr = &((uint8_t *)(&(xs)->sense_data))[off];		\
572238869Smjacob		uint32_t amt = imin(xsnsl, PISP_PCMD(xs)->totslen - off);	\
573238869Smjacob		if (amt) {							\
574238869Smjacob			memcpy(ptr, xsnsp, amt);				\
575238869Smjacob			(xs)->sense_resid -= amt;				\
576238869Smjacob			PISP_PCMD(xs)->cumslen += amt;				\
577238869Smjacob		}								\
578238869Smjacob	} while (0)
579238869Smjacob
580205698Smjacob#define	XS_SENSE_VALID(xs)	(((xs)->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
58164092Smjacob
582196008Smjacob#define	DEFAULT_FRAMESIZE(isp)		isp->isp_osinfo.framesize
583196008Smjacob#define	DEFAULT_EXEC_THROTTLE(isp)	isp->isp_osinfo.exec_throttle
58464092Smjacob
585196008Smjacob#define	GET_DEFAULT_ROLE(isp, chan)	\
586205236Smjacob	(IS_FC(isp)? ISP_FC_PC(isp, chan)->def_role : ISP_SPI_PC(isp, chan)->def_role)
587196008Smjacob#define	SET_DEFAULT_ROLE(isp, chan, val)		\
588196008Smjacob	if (IS_FC(isp)) { 				\
589205236Smjacob		ISP_FC_PC(isp, chan)->def_role = val;	\
590196008Smjacob	} else {					\
591205236Smjacob		ISP_SPI_PC(isp, chan)->def_role = val;	\
592196008Smjacob	}
593167473Smjacob
594196008Smjacob#define	DEFAULT_IID(isp, chan)		isp->isp_osinfo.pc.spi[chan].iid
595167473Smjacob
596196008Smjacob#define	DEFAULT_LOOPID(x, chan)		isp->isp_osinfo.pc.fc[chan].default_id
597196008Smjacob
598196008Smjacob#define DEFAULT_NODEWWN(isp, chan)  	isp_default_wwn(isp, chan, 0, 1)
599196008Smjacob#define DEFAULT_PORTWWN(isp, chan)  	isp_default_wwn(isp, chan, 0, 0)
600196008Smjacob#define ACTIVE_NODEWWN(isp, chan)   	isp_default_wwn(isp, chan, 1, 1)
601196008Smjacob#define ACTIVE_PORTWWN(isp, chan)   	isp_default_wwn(isp, chan, 1, 0)
602196008Smjacob
603196008Smjacob
60487635Smjacob#if	BYTE_ORDER == BIG_ENDIAN
60587635Smjacob#ifdef	ISP_SBUS_SUPPORTED
60687635Smjacob#define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
60787635Smjacob#define	ISP_IOXPUT_16(isp, s, d)				\
60887635Smjacob	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap16(s)
60987635Smjacob#define	ISP_IOXPUT_32(isp, s, d)				\
61087635Smjacob	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap32(s)
611155704Smjacob#define	ISP_IOXGET_8(isp, s, d)		d = (*((uint8_t *)s))
61287635Smjacob#define	ISP_IOXGET_16(isp, s, d)				\
61387635Smjacob	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
614155704Smjacob	*((uint16_t *)s) : bswap16(*((uint16_t *)s))
61587635Smjacob#define	ISP_IOXGET_32(isp, s, d)				\
61687635Smjacob	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
617155704Smjacob	*((uint32_t *)s) : bswap32(*((uint32_t *)s))
618163899Smjacob
619163899Smjacob#else	/* ISP_SBUS_SUPPORTED */
62087635Smjacob#define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
62187635Smjacob#define	ISP_IOXPUT_16(isp, s, d)	*(d) = bswap16(s)
62287635Smjacob#define	ISP_IOXPUT_32(isp, s, d)	*(d) = bswap32(s)
623155704Smjacob#define	ISP_IOXGET_8(isp, s, d)		d = (*((uint8_t *)s))
624155704Smjacob#define	ISP_IOXGET_16(isp, s, d)	d = bswap16(*((uint16_t *)s))
625155704Smjacob#define	ISP_IOXGET_32(isp, s, d)	d = bswap32(*((uint32_t *)s))
62687635Smjacob#endif
62787635Smjacob#define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)	*rp = bswap16(*rp)
628171159Smjacob#define	ISP_SWIZZLE_NVRAM_LONG(isp, rp)	*rp = bswap32(*rp)
629163899Smjacob
630163899Smjacob#define	ISP_IOZGET_8(isp, s, d)		d = (*((uint8_t *)s))
631163899Smjacob#define	ISP_IOZGET_16(isp, s, d)	d = (*((uint16_t *)s))
632163899Smjacob#define	ISP_IOZGET_32(isp, s, d)	d = (*((uint32_t *)s))
633163899Smjacob#define	ISP_IOZPUT_8(isp, s, d)		*(d) = s
634163899Smjacob#define	ISP_IOZPUT_16(isp, s, d)	*(d) = s
635163899Smjacob#define	ISP_IOZPUT_32(isp, s, d)	*(d) = s
636163899Smjacob
637163899Smjacob
63887635Smjacob#else
63987635Smjacob#define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
64087635Smjacob#define	ISP_IOXPUT_16(isp, s, d)	*(d) = s
64187635Smjacob#define	ISP_IOXPUT_32(isp, s, d)	*(d) = s
64287635Smjacob#define	ISP_IOXGET_8(isp, s, d)		d = *(s)
64387635Smjacob#define	ISP_IOXGET_16(isp, s, d)	d = *(s)
64487635Smjacob#define	ISP_IOXGET_32(isp, s, d)	d = *(s)
64587635Smjacob#define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)
646171159Smjacob#define	ISP_SWIZZLE_NVRAM_LONG(isp, rp)
647163899Smjacob
648163899Smjacob#define	ISP_IOZPUT_8(isp, s, d)		*(d) = s
649163899Smjacob#define	ISP_IOZPUT_16(isp, s, d)	*(d) = bswap16(s)
650163899Smjacob#define	ISP_IOZPUT_32(isp, s, d)	*(d) = bswap32(s)
651163899Smjacob
652163899Smjacob#define	ISP_IOZGET_8(isp, s, d)		d = (*((uint8_t *)(s)))
653163899Smjacob#define	ISP_IOZGET_16(isp, s, d)	d = bswap16(*((uint16_t *)(s)))
654163899Smjacob#define	ISP_IOZGET_32(isp, s, d)	d = bswap32(*((uint32_t *)(s)))
655163899Smjacob
65687635Smjacob#endif
65764092Smjacob
658171159Smjacob#define	ISP_SWAP16(isp, s)	bswap16(s)
659171159Smjacob#define	ISP_SWAP32(isp, s)	bswap32(s)
660171159Smjacob
66148487Smjacob/*
66264092Smjacob * Includes of common header files
66348487Smjacob */
66435388Smjacob
66564092Smjacob#include <dev/isp/ispreg.h>
66664092Smjacob#include <dev/isp/ispvar.h>
66764092Smjacob#include <dev/isp/ispmbox.h>
66835388Smjacob
66964092Smjacob/*
67064092Smjacob * isp_osinfo definiitions && shorthand
67164092Smjacob */
67264092Smjacob#define	SIMQFRZ_RESOURCE	0x1
67364092Smjacob#define	SIMQFRZ_LOOPDOWN	0x2
67464092Smjacob#define	SIMQFRZ_TIMED		0x4
67564092Smjacob
67673246Smjacob#define	isp_dev		isp_osinfo.dev
67764092Smjacob
67864092Smjacob/*
67964092Smjacob * prototypes for isp_pci && isp_freebsd to share
68064092Smjacob */
681196008Smjacobextern int isp_attach(ispsoftc_t *);
682224856Smjacobextern int isp_detach(ispsoftc_t *);
683157943Smjacobextern void isp_uninit(ispsoftc_t *);
684196008Smjacobextern uint64_t isp_default_wwn(ispsoftc_t *, int, int, int);
68564092Smjacob
68664092Smjacob/*
68799756Smjacob * driver global data
68899756Smjacob */
68999756Smjacobextern int isp_announced;
690163899Smjacobextern int isp_fabric_hysteresis;
691163899Smjacobextern int isp_loop_down_limit;
692164272Smjacobextern int isp_gone_device_time;
693163899Smjacobextern int isp_quickboot_time;
694196008Smjacobextern int isp_autoconfig;
69599756Smjacob
69699756Smjacob/*
69764092Smjacob * Platform private flags
69864092Smjacob */
69962172Smjacob
70039235Sgibbs/*
701164272Smjacob * Platform Library Functions
702164272Smjacob */
703164272Smjacobvoid isp_prt(ispsoftc_t *, int level, const char *, ...) __printflike(3, 4);
704205698Smjacobvoid isp_xs_prt(ispsoftc_t *, XS_T *, int level, const char *, ...) __printflike(4, 5);
705164272Smjacobuint64_t isp_nanotime_sub(struct timespec *, struct timespec *);
706164272Smjacobint isp_mbox_acquire(ispsoftc_t *);
707164272Smjacobvoid isp_mbox_wait_complete(ispsoftc_t *, mbreg_t *);
708164272Smjacobvoid isp_mbox_notify_done(ispsoftc_t *);
709164272Smjacobvoid isp_mbox_release(ispsoftc_t *);
710196008Smjacobint isp_fc_scratch_acquire(ispsoftc_t *, int);
711164272Smjacobint isp_mstohz(int);
712169292Smjacobvoid isp_platform_intr(void *);
713169292Smjacobvoid isp_common_dmateardown(ispsoftc_t *, struct ccb_scsiio *, uint32_t);
714238869Smjacobint isp_fcp_next_crn(ispsoftc_t *, uint8_t *, XS_T *);
715164272Smjacob
716164272Smjacob/*
717169292Smjacob * Platform Version specific defines
718166177Smjacob */
719169292Smjacob#define	BUS_DMA_ROOTARG(x)	bus_get_dma_tag(x)
720169292Smjacob#define	isp_dma_tag_create(a, b, c, d, e, f, g, h, i, j, k, z)	\
721169292Smjacob	bus_dma_tag_create(a, b, c, d, e, f, g, h, i, j, k, \
722169292Smjacob	busdma_lock_mutex, &isp->isp_osinfo.lock, z)
723169292Smjacob
724166935Smjacob#define	isp_setup_intr	bus_setup_intr
725166177Smjacob
726169292Smjacob#define	isp_sim_alloc(a, b, c, d, e, f, g, h)	\
727169292Smjacob	cam_sim_alloc(a, b, c, d, e, &(d)->isp_osinfo.lock, f, g, h)
728169292Smjacob
729196008Smjacob#define	ISP_PATH_PRT(i, l, p, ...)					\
730196008Smjacob	if ((l) == ISP_LOGALL || ((l)& (i)->isp_dblev) != 0) {		\
731196008Smjacob                xpt_print(p, __VA_ARGS__);				\
732196008Smjacob        }
733196008Smjacob
734166177Smjacob/*
73564092Smjacob * Platform specific inline functions
73639235Sgibbs */
73744819Smjacob
73864092Smjacob/*
739164272Smjacob * ISP General Library functions
74064092Smjacob */
74144819Smjacob
742155228Smjacob#include <dev/isp/isp_library.h>
743155228Smjacob
74435388Smjacob#endif	/* _ISP_FREEBSD_H */
745