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
98260341Smav#define	ATPDPHASHSIZE	16
99260341Smav#define	ATPDPHASH(x)	((((x) >> 24) ^ ((x) >> 16) ^ ((x) >> 8) ^ (x)) &  \
100260341Smav			    ((ATPDPHASHSIZE) - 1))
101196008Smjacob
102196008Smjacob#include <dev/isp/isp_target.h>
103260341Smavtypedef struct atio_private_data {
104260341Smav	LIST_ENTRY(atio_private_data)	next;
105155704Smjacob	uint32_t	orig_datalen;
106155704Smjacob	uint32_t	bytes_xfered;
107239143Smjacob	uint32_t	bytes_in_transit;
108237537Smjacob	uint32_t	tag;		/* typically f/w RX_ID */
109196008Smjacob	uint32_t	lun;
110196008Smjacob	uint32_t	nphdl;
111196008Smjacob	uint32_t	sid;
112196008Smjacob	uint32_t	portid;
113237537Smjacob	uint16_t	rxid;	/* wire rxid */
114237537Smjacob	uint16_t	oxid;	/* wire oxid */
115238869Smjacob	uint16_t	word3;	/* PRLI word3 params */
116238869Smjacob	uint16_t	ctcnt;	/* number of CTIOs currently active */
117239143Smjacob	uint8_t		seqno;	/* CTIO sequence number */
118196008Smjacob	uint32_t
119238869Smjacob			srr_notify_rcvd	: 1,
120238869Smjacob			cdb0		: 8,
121238869Smjacob			sendst		: 1,
122238869Smjacob			dead		: 1,
123238869Smjacob			tattr		: 3,
124238869Smjacob			state		: 3;
125238869Smjacob	void *		ests;
126238869Smjacob	/*
127238869Smjacob	 * The current SRR notify copy
128238869Smjacob	 */
129238869Smjacob	uint8_t		srr[64];	/*  sb QENTRY_LEN, but order of definitions is wrong */
130238869Smjacob	void *		srr_ccb;
131238869Smjacob	uint32_t	nsrr;
13284242Smjacob} atio_private_data_t;
13398288Smjacob#define	ATPD_STATE_FREE			0
13498288Smjacob#define	ATPD_STATE_ATIO			1
13598288Smjacob#define	ATPD_STATE_CAM			2
13698288Smjacob#define	ATPD_STATE_CTIO			3
13798288Smjacob#define	ATPD_STATE_LAST_CTIO		4
13898288Smjacob#define	ATPD_STATE_PDON			5
13984242Smjacob
140239143Smjacob#define	ATPD_CCB_OUTSTANDING		16
141239143Smjacob
142239143Smjacob#define	ATPD_SEQ_MASK			0x7f
143239143Smjacob#define	ATPD_SEQ_NOTIFY_CAM		0x80
144239143Smjacob#define	ATPD_SET_SEQNO(hdrp, atp)	((isphdr_t *)hdrp)->rqs_seqno &= ~ATPD_SEQ_MASK, ((isphdr_t *)hdrp)->rqs_seqno |= (atp)->seqno
145239143Smjacob#define	ATPD_GET_SEQNO(hdrp)		(((isphdr_t *)hdrp)->rqs_seqno & ATPD_SEQ_MASK)
146239143Smjacob#define	ATPD_GET_NCAM(hdrp)		((((isphdr_t *)hdrp)->rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0)
147239143Smjacob
148196008Smjacobtypedef union inot_private_data inot_private_data_t;
149196008Smjacobunion inot_private_data {
150196008Smjacob	inot_private_data_t *next;
151196008Smjacob	struct {
152196008Smjacob		isp_notify_t nt;	/* must be first! */
153196008Smjacob		uint8_t data[64];	/* sb QENTRY_LEN, but order of definitions is wrong */
154196008Smjacob		uint32_t tag_id, seq_id;
155196008Smjacob	} rd;
156196008Smjacob};
157238869Smjacobtypedef struct isp_timed_notify_ack {
158238869Smjacob	void *isp;
159238869Smjacob	void *not;
160238869Smjacob	uint8_t data[64];	 /* sb QENTRY_LEN, but order of definitions is wrong */
161238869Smjacob} isp_tna_t;
162196008Smjacob
163239143SmjacobTAILQ_HEAD(isp_ccbq, ccb_hdr);
16455366Smjacobtypedef struct tstate {
165196008Smjacob	SLIST_ENTRY(tstate) next;
16655366Smjacob	struct cam_path *owner;
167239143Smjacob	struct isp_ccbq waitq;		/* waiting CCBs */
16855366Smjacob	struct ccb_hdr_slist atios;
16955366Smjacob	struct ccb_hdr_slist inots;
170155704Smjacob	uint32_t hold;
171236427Smjacob	uint32_t
172236427Smjacob		enabled		: 1,
173236427Smjacob		atio_count	: 15,
174236427Smjacob		inot_count	: 15;
175196008Smjacob	inot_private_data_t *	restart_queue;
176196008Smjacob	inot_private_data_t *	ntfree;
177196008Smjacob	inot_private_data_t	ntpool[ATPDPSIZE];
178260341Smav	LIST_HEAD(, atio_private_data)	atfree;
179260341Smav	LIST_HEAD(, atio_private_data)	atused[ATPDPHASHSIZE];
180196008Smjacob	atio_private_data_t	atpool[ATPDPSIZE];
18155366Smjacob} tstate_t;
18255366Smjacob
183196008Smjacob#define	LUN_HASH_SIZE		32
184196008Smjacob#define	LUN_HASH_FUNC(lun)	((lun) & (LUN_HASH_SIZE - 1))
185196008Smjacob
18655366Smjacob#endif
18755366Smjacob
188169292Smjacob/*
189169292Smjacob * Per command info.
190169292Smjacob */
191169292Smjacobstruct isp_pcmd {
192169292Smjacob	struct isp_pcmd *	next;
193238869Smjacob	bus_dmamap_t 		dmap;		/* dma map for this command */
194238869Smjacob	struct ispsoftc *	isp;		/* containing isp */
195238869Smjacob	struct callout		wdog;		/* watchdog timer */
196239143Smjacob	uint32_t		datalen;	/* data length for this command (target mode only) */
197238869Smjacob	uint8_t			totslen;	/* sense length on status response */
198238869Smjacob	uint8_t			cumslen;	/* sense length on status response */
199238869Smjacob	uint8_t 		crn;		/* command reference number */
200169292Smjacob};
201169292Smjacob#define	ISP_PCMD(ccb)		(ccb)->ccb_h.spriv_ptr1
202169292Smjacob#define	PISP_PCMD(ccb)		((struct isp_pcmd *)ISP_PCMD(ccb))
203169292Smjacob
204196008Smjacob/*
205238869Smjacob * Per nexus info.
206238869Smjacob */
207238869Smjacobstruct isp_nexus {
208238869Smjacob	struct isp_nexus *	next;
209238869Smjacob	uint32_t
210238869Smjacob		crnseed	:	8;	/* next command reference number */
211238869Smjacob	uint32_t
212238869Smjacob		tgt	:	16,	/* TGT for target */
213238869Smjacob		lun	:	16;	/* LUN for target */
214238869Smjacob};
215238869Smjacob#define	NEXUS_HASH_WIDTH	32
216238869Smjacob#define	INITIAL_NEXUS_COUNT	MAX_FC_TARG
217238869Smjacob#define	NEXUS_HASH(tgt, lun)	((tgt + lun) % NEXUS_HASH_WIDTH)
218238869Smjacob
219238869Smjacob/*
220196008Smjacob * Per channel information
221196008Smjacob */
222196008SmjacobSLIST_HEAD(tslist, tstate);
223196008Smjacob
224196008Smjacobstruct isp_fc {
225196008Smjacob	struct cam_sim *sim;
226196008Smjacob	struct cam_path *path;
227196008Smjacob	struct ispsoftc *isp;
228196008Smjacob	struct proc *kproc;
229196008Smjacob	bus_dma_tag_t tdmat;
230196008Smjacob	bus_dmamap_t tdmap;
231196008Smjacob	uint64_t def_wwpn;
232196008Smjacob	uint64_t def_wwnn;
233196008Smjacob	uint32_t loop_down_time;
234196008Smjacob	uint32_t loop_down_limit;
235196008Smjacob	uint32_t gone_device_time;
236238869Smjacob	/*
237238869Smjacob	 * Per target/lun info- just to keep a per-ITL nexus crn count
238238869Smjacob	 */
239238869Smjacob	struct isp_nexus *nexus_hash[NEXUS_HASH_WIDTH];
240238869Smjacob	struct isp_nexus *nexus_free_list;
241196008Smjacob	uint32_t
242196008Smjacob#ifdef	ISP_TARGET_MODE
243196008Smjacob#ifdef	ISP_INTERNAL_TARGET
244196008Smjacob		proc_active	: 1,
245196008Smjacob#endif
246196008Smjacob		tm_luns_enabled	: 1,
247196008Smjacob		tm_enable_defer	: 1,
248196008Smjacob		tm_enabled	: 1,
249196008Smjacob#endif
250163899Smjacob		simqfrozen	: 3,
251196008Smjacob		default_id	: 8,
252163899Smjacob		hysteresis	: 8,
253205236Smjacob		def_role	: 2,	/* default role */
254164272Smjacob		gdt_running	: 1,
255196008Smjacob		loop_dead	: 1,
256200089Smjacob		fcbsy		: 1,
257200089Smjacob		ready		: 1;
258196008Smjacob	struct callout ldt;	/* loop down timer */
259196008Smjacob	struct callout gdt;	/* gone device timer */
260224804Smjacob	struct task ltask;
261224804Smjacob	struct task gtask;
262196008Smjacob#ifdef	ISP_TARGET_MODE
263196008Smjacob	struct tslist lun_hash[LUN_HASH_SIZE];
264196008Smjacob#ifdef	ISP_INTERNAL_TARGET
265196008Smjacob	struct proc *		target_proc;
266196008Smjacob#endif
267238869Smjacob#if defined(DEBUG)
268238869Smjacob	unsigned int inject_lost_data_frame;
269196008Smjacob#endif
270238869Smjacob#endif
271196008Smjacob};
272196008Smjacob
273196008Smjacobstruct isp_spi {
274196008Smjacob	struct cam_sim *sim;
275196008Smjacob	struct cam_path *path;
276196008Smjacob	uint32_t
277196008Smjacob#ifdef	ISP_TARGET_MODE
278196008Smjacob#ifdef	ISP_INTERNAL_TARGET
279196008Smjacob		proc_active	: 1,
280196008Smjacob#endif
281196008Smjacob		tm_luns_enabled	: 1,
282196008Smjacob		tm_enable_defer	: 1,
283196008Smjacob		tm_enabled	: 1,
284196008Smjacob#endif
285196008Smjacob		simqfrozen	: 3,
286205236Smjacob		def_role	: 2,
287196008Smjacob		iid		: 4;
288196008Smjacob#ifdef	ISP_TARGET_MODE
289196008Smjacob	struct tslist lun_hash[LUN_HASH_SIZE];
290196008Smjacob#ifdef	ISP_INTERNAL_TARGET
291196008Smjacob	struct proc *		target_proc;
292196008Smjacob#endif
293196008Smjacob#endif
294196008Smjacob};
295196008Smjacob
296196008Smjacobstruct isposinfo {
297196008Smjacob	/*
298196008Smjacob	 * Linkage, locking, and identity
299196008Smjacob	 */
300169292Smjacob	struct mtx		lock;
301196008Smjacob	device_t		dev;
302196008Smjacob	struct cdev *		cdev;
303196008Smjacob	struct intr_config_hook	ehook;
304196008Smjacob	struct cam_devq *	devq;
305196008Smjacob
306196008Smjacob	/*
307196008Smjacob	 * Firmware pointer
308196008Smjacob	 */
309166756Sluigi	const struct firmware *	fw;
310196008Smjacob
311196008Smjacob	/*
312196008Smjacob	 * DMA related sdtuff
313196008Smjacob	 */
314196008Smjacob	bus_space_tag_t		bus_tag;
315196008Smjacob	bus_dma_tag_t		dmat;
316196008Smjacob	bus_space_handle_t	bus_handle;
31793706Smjacob	bus_dma_tag_t		cdmat;
31893706Smjacob	bus_dmamap_t		cdmap;
319196008Smjacob
320169292Smjacob	/*
321196008Smjacob	 * Command and transaction related related stuff
322169292Smjacob	 */
323169292Smjacob	struct isp_pcmd *	pcmd_pool;
324169292Smjacob	struct isp_pcmd *	pcmd_free;
325169292Smjacob
326196008Smjacob	uint32_t
32755366Smjacob#ifdef	ISP_TARGET_MODE
328196008Smjacob		tmwanted	: 1,
329196008Smjacob		tmbusy		: 1,
330196008Smjacob#else
331196008Smjacob				: 2,
33255366Smjacob#endif
333238869Smjacob		sixtyfourbit	: 1,	/* sixtyfour bit platform */
334196008Smjacob		timer_active	: 1,
335196008Smjacob		autoconf	: 1,
336196008Smjacob		ehook_active	: 1,
337196008Smjacob		disabled	: 1,
338196008Smjacob		mbox_sleeping	: 1,
339196008Smjacob		mbox_sleep_ok	: 1,
340196008Smjacob		mboxcmd_done	: 1,
341196008Smjacob		mboxbsy		: 1;
342196008Smjacob
343196008Smjacob	struct callout		tmo;	/* general timer */
344196008Smjacob
345196008Smjacob	/*
346196008Smjacob	 * misc- needs to be sorted better XXXXXX
347196008Smjacob	 */
348196008Smjacob	int			framesize;
349196008Smjacob	int			exec_throttle;
350196008Smjacob	int			cont_max;
351196008Smjacob
352196008Smjacob#ifdef	ISP_TARGET_MODE
353196008Smjacob	cam_status *		rptr;
354196008Smjacob#endif
355196008Smjacob
356238869Smjacob	bus_addr_t		ecmd_dma;
357238869Smjacob	isp_ecmd_t *		ecmd_base;
358238869Smjacob	isp_ecmd_t *		ecmd_free;
359238869Smjacob
360196008Smjacob	/*
361196008Smjacob	 * Per-type private storage...
362196008Smjacob	 */
363196008Smjacob	union {
364196008Smjacob		struct isp_fc *fc;
365196008Smjacob		struct isp_spi *spi;
366196008Smjacob		void *ptr;
367196008Smjacob	} pc;
36835388Smjacob};
369196008Smjacob#define	ISP_FC_PC(isp, chan)	(&(isp)->isp_osinfo.pc.fc[(chan)])
370196008Smjacob#define	ISP_SPI_PC(isp, chan)	(&(isp)->isp_osinfo.pc.spi[(chan)])
371196008Smjacob#define	ISP_GET_PC(isp, chan, tag, rslt)		\
372196008Smjacob	if (IS_SCSI(isp)) {				\
373196008Smjacob		rslt = ISP_SPI_PC(isp, chan)-> tag;	\
374196008Smjacob	} else {					\
375196008Smjacob		rslt = ISP_FC_PC(isp, chan)-> tag;	\
376196008Smjacob	}
377196008Smjacob#define	ISP_GET_PC_ADDR(isp, chan, tag, rp)		\
378196008Smjacob	if (IS_SCSI(isp)) {				\
379196008Smjacob		rp = &ISP_SPI_PC(isp, chan)-> tag;	\
380196008Smjacob	} else {					\
381196008Smjacob		rp = &ISP_FC_PC(isp, chan)-> tag;	\
382196008Smjacob	}
383196008Smjacob#define	ISP_SET_PC(isp, chan, tag, val)			\
384196008Smjacob	if (IS_SCSI(isp)) {				\
385196008Smjacob		ISP_SPI_PC(isp, chan)-> tag = val;	\
386196008Smjacob	} else {					\
387196008Smjacob		ISP_FC_PC(isp, chan)-> tag = val;	\
388196008Smjacob	}
38939235Sgibbs
390238869Smjacob#define	FCP_NEXT_CRN	isp_fcp_next_crn
39177365Smjacob#define	isp_lock	isp_osinfo.lock
392167501Smjacob#define	isp_bus_tag	isp_osinfo.bus_tag
393167501Smjacob#define	isp_bus_handle	isp_osinfo.bus_handle
39477365Smjacob
39564092Smjacob/*
39669525Smjacob * Locking macros...
39769525Smjacob */
398169292Smjacob#define	ISP_LOCK(isp)	mtx_lock(&isp->isp_osinfo.lock)
399169292Smjacob#define	ISP_UNLOCK(isp)	mtx_unlock(&isp->isp_osinfo.lock)
40069525Smjacob
40169525Smjacob/*
40264092Smjacob * Required Macros/Defines
40364092Smjacob */
404196008Smjacob#define	ISP_FC_SCRLEN		0x1000
40545284Smjacob
406196008Smjacob#define	ISP_MEMZERO(a, b)	memset(a, 0, b)
407196008Smjacob#define	ISP_MEMCPY		memcpy
408196008Smjacob#define	ISP_SNPRINTF		snprintf
409196008Smjacob#define	ISP_DELAY		DELAY
410196008Smjacob#define	ISP_SLEEP(isp, x)	DELAY(x)
41135388Smjacob
412219098Smjacob#define	ISP_MIN			imin
413219098Smjacob
414196008Smjacob#ifndef	DIAGNOSTIC
415196008Smjacob#define	ISP_INLINE		__inline
416196008Smjacob#else
417196008Smjacob#define	ISP_INLINE
418196008Smjacob#endif
419196008Smjacob
42064092Smjacob#define	NANOTIME_T		struct timespec
42164092Smjacob#define	GET_NANOTIME		nanotime
42264092Smjacob#define	GET_NANOSEC(x)		((x)->tv_sec * 1000000000 + (x)->tv_nsec)
423164272Smjacob#define	NANOTIME_SUB		isp_nanotime_sub
42462496Smjacob
42599598Smjacob#define	MAXISPREQUEST(isp)	((IS_FC(isp) || IS_ULTRA2(isp))? 1024 : 256)
42662496Smjacob
427218691Smarius#define	MEMORYBARRIER(isp, type, offset, size, chan)		\
42893706Smjacobswitch (type) {							\
42993706Smjacobcase SYNC_SFORDEV:						\
430218691Smarius{								\
431218691Smarius	struct isp_fc *fc = ISP_FC_PC(isp, chan);		\
432218691Smarius	bus_dmamap_sync(fc->tdmat, fc->tdmap,			\
433218691Smarius	   BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);		\
434218691Smarius	break;							\
435218691Smarius}								\
43693706Smjacobcase SYNC_REQUEST:						\
437196008Smjacob	bus_dmamap_sync(isp->isp_osinfo.cdmat,			\
438196008Smjacob	   isp->isp_osinfo.cdmap, 				\
43993706Smjacob	   BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);		\
44093706Smjacob	break;							\
44193706Smjacobcase SYNC_SFORCPU:						\
442218691Smarius{								\
443218691Smarius	struct isp_fc *fc = ISP_FC_PC(isp, chan);		\
444218691Smarius	bus_dmamap_sync(fc->tdmat, fc->tdmap,			\
445218691Smarius	   BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);	\
446218691Smarius	break;							\
447218691Smarius}								\
44893706Smjacobcase SYNC_RESULT:						\
449196008Smjacob	bus_dmamap_sync(isp->isp_osinfo.cdmat, 			\
450196008Smjacob	   isp->isp_osinfo.cdmap,				\
45193706Smjacob	   BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);	\
45293706Smjacob	break;							\
453167501Smjacobcase SYNC_REG:							\
454196008Smjacob	bus_space_barrier(isp->isp_osinfo.bus_tag,		\
455196008Smjacob	    isp->isp_osinfo.bus_handle, offset, size,		\
456219282Smjacob	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);	\
457167501Smjacob	break;							\
45893706Smjacobdefault:							\
45993706Smjacob	break;							\
46093706Smjacob}
46162496Smjacob
462260347Smav#define	MEMORYBARRIERW(isp, type, offset, size, chan)		\
463260347Smavswitch (type) {							\
464260347Smavcase SYNC_SFORDEV:						\
465260347Smav{								\
466260347Smav	struct isp_fc *fc = ISP_FC_PC(isp, chan);		\
467260347Smav	bus_dmamap_sync(fc->tdmat, fc->tdmap,			\
468260347Smav	   BUS_DMASYNC_PREWRITE);				\
469260347Smav	break;							\
470260347Smav}								\
471260347Smavcase SYNC_REQUEST:						\
472260347Smav	bus_dmamap_sync(isp->isp_osinfo.cdmat,			\
473260347Smav	   isp->isp_osinfo.cdmap, BUS_DMASYNC_PREWRITE);	\
474260347Smav	break;							\
475260347Smavcase SYNC_SFORCPU:						\
476260347Smav{								\
477260347Smav	struct isp_fc *fc = ISP_FC_PC(isp, chan);		\
478260347Smav	bus_dmamap_sync(fc->tdmat, fc->tdmap,			\
479260347Smav	   BUS_DMASYNC_POSTWRITE);				\
480260347Smav	break;							\
481260347Smav}								\
482260347Smavcase SYNC_RESULT:						\
483260347Smav	bus_dmamap_sync(isp->isp_osinfo.cdmat, 			\
484260347Smav	   isp->isp_osinfo.cdmap, BUS_DMASYNC_POSTWRITE);	\
485260347Smav	break;							\
486260347Smavcase SYNC_REG:							\
487260347Smav	bus_space_barrier(isp->isp_osinfo.bus_tag,		\
488260347Smav	    isp->isp_osinfo.bus_handle, offset, size,		\
489260347Smav	    BUS_SPACE_BARRIER_WRITE);				\
490260347Smav	break;							\
491260347Smavdefault:							\
492260347Smav	break;							\
493260347Smav}
494260347Smav
495163899Smjacob#define	MBOX_ACQUIRE			isp_mbox_acquire
49664092Smjacob#define	MBOX_WAIT_COMPLETE		isp_mbox_wait_complete
497164272Smjacob#define	MBOX_NOTIFY_COMPLETE		isp_mbox_notify_done
498163899Smjacob#define	MBOX_RELEASE			isp_mbox_release
49962496Smjacob
500196008Smjacob#define	FC_SCRATCH_ACQUIRE		isp_fc_scratch_acquire
501196008Smjacob#define	FC_SCRATCH_RELEASE(isp, chan)	isp->isp_osinfo.pc.fc[chan].fcbsy = 0
50290224Smjacob
50364092Smjacob#ifndef	SCSI_GOOD
50464092Smjacob#define	SCSI_GOOD	SCSI_STATUS_OK
50564092Smjacob#endif
50664092Smjacob#ifndef	SCSI_CHECK
50764092Smjacob#define	SCSI_CHECK	SCSI_STATUS_CHECK_COND
50864092Smjacob#endif
50964092Smjacob#ifndef	SCSI_BUSY
51064092Smjacob#define	SCSI_BUSY	SCSI_STATUS_BUSY
51164092Smjacob#endif
51264092Smjacob#ifndef	SCSI_QFULL
51364092Smjacob#define	SCSI_QFULL	SCSI_STATUS_QUEUE_FULL
51464092Smjacob#endif
51564092Smjacob
51664092Smjacob#define	XS_T			struct ccb_scsiio
517155704Smjacob#define	XS_DMA_ADDR_T		bus_addr_t
518196008Smjacob#define XS_GET_DMA64_SEG(a, b, c)		\
519196008Smjacob{						\
520196008Smjacob	ispds64_t *d = a;			\
521196008Smjacob	bus_dma_segment_t *e = b;		\
522196008Smjacob	uint32_t f = c;				\
523196008Smjacob	e += f;					\
524196008Smjacob        d->ds_base = DMA_LO32(e->ds_addr);	\
525196008Smjacob        d->ds_basehi = DMA_HI32(e->ds_addr);	\
526196008Smjacob        d->ds_count = e->ds_len;		\
527196008Smjacob}
528196008Smjacob#define XS_GET_DMA_SEG(a, b, c)			\
529196008Smjacob{						\
530196008Smjacob	ispds_t *d = a;				\
531196008Smjacob	bus_dma_segment_t *e = b;		\
532196008Smjacob	uint32_t f = c;				\
533196008Smjacob	e += f;					\
534196008Smjacob        d->ds_base = DMA_LO32(e->ds_addr);	\
535196008Smjacob        d->ds_count = e->ds_len;		\
536196008Smjacob}
537169292Smjacob#define	XS_ISP(ccb)		cam_sim_softc(xpt_path_sim((ccb)->ccb_h.path))
53864092Smjacob#define	XS_CHANNEL(ccb)		cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path))
53964092Smjacob#define	XS_TGT(ccb)		(ccb)->ccb_h.target_id
54064092Smjacob#define	XS_LUN(ccb)		(ccb)->ccb_h.target_lun
54135388Smjacob
54264092Smjacob#define	XS_CDBP(ccb)	\
54364092Smjacob	(((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \
54464092Smjacob	 (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes)
54564092Smjacob
54664092Smjacob#define	XS_CDBLEN(ccb)		(ccb)->cdb_len
54748487Smjacob#define	XS_XFRLEN(ccb)		(ccb)->dxfer_len
54848487Smjacob#define	XS_TIME(ccb)		(ccb)->ccb_h.timeout
549196008Smjacob#define	XS_GET_RESID(ccb)	(ccb)->resid
550196008Smjacob#define	XS_SET_RESID(ccb, r)	(ccb)->resid = r
55164092Smjacob#define	XS_STSP(ccb)		(&(ccb)->scsi_status)
55248487Smjacob#define	XS_SNSP(ccb)		(&(ccb)->sense_data)
55364092Smjacob
554238869Smjacob#define	XS_TOT_SNSLEN(ccb)	ccb->sense_len
555238869Smjacob#define	XS_CUR_SNSLEN(ccb)	(ccb->sense_len - ccb->sense_resid)
55664092Smjacob
557225950Sken#define	XS_SNSKEY(ccb)		(scsi_get_sense_key(&(ccb)->sense_data, \
558238869Smjacob				 ccb->sense_len - ccb->sense_resid, 1))
559225950Sken
560225950Sken#define	XS_SNSASC(ccb)		(scsi_get_asc(&(ccb)->sense_data,	\
561238869Smjacob				 ccb->sense_len - ccb->sense_resid, 1))
562225950Sken
563225950Sken#define	XS_SNSASCQ(ccb)		(scsi_get_ascq(&(ccb)->sense_data,	\
564238869Smjacob				 ccb->sense_len - ccb->sense_resid, 1))
56564092Smjacob#define	XS_TAG_P(ccb)	\
56664092Smjacob	(((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \
56764092Smjacob	 (ccb)->tag_action != CAM_TAG_ACTION_NONE)
56835388Smjacob
56964092Smjacob#define	XS_TAG_TYPE(ccb)	\
57064092Smjacob	((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \
57164092Smjacob	 ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG))
57264092Smjacob
57364092Smjacob
57464092Smjacob#define	XS_SETERR(ccb, v)	(ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \
575238869Smjacob				(ccb)->ccb_h.status |= v
57664092Smjacob
57764092Smjacob#	define	HBA_NOERROR		CAM_REQ_INPROG
57864092Smjacob#	define	HBA_BOTCH		CAM_UNREC_HBA_ERROR
57964092Smjacob#	define	HBA_CMDTIMEOUT		CAM_CMD_TIMEOUT
58064092Smjacob#	define	HBA_SELTIMEOUT		CAM_SEL_TIMEOUT
58164092Smjacob#	define	HBA_TGTBSY		CAM_SCSI_STATUS_ERROR
58264092Smjacob#	define	HBA_BUSRESET		CAM_SCSI_BUS_RESET
58364092Smjacob#	define	HBA_ABORTED		CAM_REQ_ABORTED
58464092Smjacob#	define	HBA_DATAOVR		CAM_DATA_RUN_ERR
58564092Smjacob#	define	HBA_ARQFAIL		CAM_AUTOSENSE_FAIL
58664092Smjacob
58764092Smjacob
58864092Smjacob#define	XS_ERR(ccb)		((ccb)->ccb_h.status & CAM_STATUS_MASK)
58964092Smjacob
590238869Smjacob#define	XS_NOERR(ccb)		(((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
59164092Smjacob
592238869Smjacob#define	XS_INITERR(ccb)		XS_SETERR(ccb, CAM_REQ_INPROG), ccb->sense_resid = ccb->sense_len
59364092Smjacob
594238869Smjacob#define	XS_SAVE_SENSE(xs, sense_ptr, totslen, slen)	do {			\
595238869Smjacob		uint32_t tlen = slen;						\
596238869Smjacob		if (tlen > (xs)->sense_len)					\
597238869Smjacob			tlen = (xs)->sense_len;					\
598238869Smjacob		PISP_PCMD(xs)->totslen = imin((xs)->sense_len, totslen);	\
599238869Smjacob		PISP_PCMD(xs)->cumslen = tlen;					\
600238869Smjacob		memcpy(&(xs)->sense_data, sense_ptr, tlen);			\
601238869Smjacob		(xs)->sense_resid = (xs)->sense_len - tlen;			\
602238869Smjacob		(xs)->ccb_h.status |= CAM_AUTOSNS_VALID;			\
603238869Smjacob	} while (0)
60464092Smjacob
605238869Smjacob#define	XS_SENSE_APPEND(xs, xsnsp, xsnsl)	do {				\
606238869Smjacob		uint32_t off = PISP_PCMD(xs)->cumslen;				\
607238869Smjacob		uint8_t *ptr = &((uint8_t *)(&(xs)->sense_data))[off];		\
608238869Smjacob		uint32_t amt = imin(xsnsl, PISP_PCMD(xs)->totslen - off);	\
609238869Smjacob		if (amt) {							\
610238869Smjacob			memcpy(ptr, xsnsp, amt);				\
611238869Smjacob			(xs)->sense_resid -= amt;				\
612238869Smjacob			PISP_PCMD(xs)->cumslen += amt;				\
613238869Smjacob		}								\
614238869Smjacob	} while (0)
615238869Smjacob
616205698Smjacob#define	XS_SENSE_VALID(xs)	(((xs)->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
61764092Smjacob
618196008Smjacob#define	DEFAULT_FRAMESIZE(isp)		isp->isp_osinfo.framesize
619196008Smjacob#define	DEFAULT_EXEC_THROTTLE(isp)	isp->isp_osinfo.exec_throttle
62064092Smjacob
621196008Smjacob#define	GET_DEFAULT_ROLE(isp, chan)	\
622205236Smjacob	(IS_FC(isp)? ISP_FC_PC(isp, chan)->def_role : ISP_SPI_PC(isp, chan)->def_role)
623196008Smjacob#define	SET_DEFAULT_ROLE(isp, chan, val)		\
624196008Smjacob	if (IS_FC(isp)) { 				\
625205236Smjacob		ISP_FC_PC(isp, chan)->def_role = val;	\
626196008Smjacob	} else {					\
627205236Smjacob		ISP_SPI_PC(isp, chan)->def_role = val;	\
628196008Smjacob	}
629167473Smjacob
630196008Smjacob#define	DEFAULT_IID(isp, chan)		isp->isp_osinfo.pc.spi[chan].iid
631167473Smjacob
632196008Smjacob#define	DEFAULT_LOOPID(x, chan)		isp->isp_osinfo.pc.fc[chan].default_id
633196008Smjacob
634196008Smjacob#define DEFAULT_NODEWWN(isp, chan)  	isp_default_wwn(isp, chan, 0, 1)
635196008Smjacob#define DEFAULT_PORTWWN(isp, chan)  	isp_default_wwn(isp, chan, 0, 0)
636196008Smjacob#define ACTIVE_NODEWWN(isp, chan)   	isp_default_wwn(isp, chan, 1, 1)
637196008Smjacob#define ACTIVE_PORTWWN(isp, chan)   	isp_default_wwn(isp, chan, 1, 0)
638196008Smjacob
639196008Smjacob
64087635Smjacob#if	BYTE_ORDER == BIG_ENDIAN
64187635Smjacob#ifdef	ISP_SBUS_SUPPORTED
64287635Smjacob#define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
64387635Smjacob#define	ISP_IOXPUT_16(isp, s, d)				\
64487635Smjacob	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap16(s)
64587635Smjacob#define	ISP_IOXPUT_32(isp, s, d)				\
64687635Smjacob	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap32(s)
647155704Smjacob#define	ISP_IOXGET_8(isp, s, d)		d = (*((uint8_t *)s))
64887635Smjacob#define	ISP_IOXGET_16(isp, s, d)				\
64987635Smjacob	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
650155704Smjacob	*((uint16_t *)s) : bswap16(*((uint16_t *)s))
65187635Smjacob#define	ISP_IOXGET_32(isp, s, d)				\
65287635Smjacob	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
653155704Smjacob	*((uint32_t *)s) : bswap32(*((uint32_t *)s))
654163899Smjacob
655163899Smjacob#else	/* ISP_SBUS_SUPPORTED */
65687635Smjacob#define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
65787635Smjacob#define	ISP_IOXPUT_16(isp, s, d)	*(d) = bswap16(s)
65887635Smjacob#define	ISP_IOXPUT_32(isp, s, d)	*(d) = bswap32(s)
659155704Smjacob#define	ISP_IOXGET_8(isp, s, d)		d = (*((uint8_t *)s))
660155704Smjacob#define	ISP_IOXGET_16(isp, s, d)	d = bswap16(*((uint16_t *)s))
661155704Smjacob#define	ISP_IOXGET_32(isp, s, d)	d = bswap32(*((uint32_t *)s))
66287635Smjacob#endif
66387635Smjacob#define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)	*rp = bswap16(*rp)
664171159Smjacob#define	ISP_SWIZZLE_NVRAM_LONG(isp, rp)	*rp = bswap32(*rp)
665163899Smjacob
666163899Smjacob#define	ISP_IOZGET_8(isp, s, d)		d = (*((uint8_t *)s))
667163899Smjacob#define	ISP_IOZGET_16(isp, s, d)	d = (*((uint16_t *)s))
668163899Smjacob#define	ISP_IOZGET_32(isp, s, d)	d = (*((uint32_t *)s))
669163899Smjacob#define	ISP_IOZPUT_8(isp, s, d)		*(d) = s
670163899Smjacob#define	ISP_IOZPUT_16(isp, s, d)	*(d) = s
671163899Smjacob#define	ISP_IOZPUT_32(isp, s, d)	*(d) = s
672163899Smjacob
673163899Smjacob
67487635Smjacob#else
67587635Smjacob#define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
67687635Smjacob#define	ISP_IOXPUT_16(isp, s, d)	*(d) = s
67787635Smjacob#define	ISP_IOXPUT_32(isp, s, d)	*(d) = s
67887635Smjacob#define	ISP_IOXGET_8(isp, s, d)		d = *(s)
67987635Smjacob#define	ISP_IOXGET_16(isp, s, d)	d = *(s)
68087635Smjacob#define	ISP_IOXGET_32(isp, s, d)	d = *(s)
68187635Smjacob#define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)
682171159Smjacob#define	ISP_SWIZZLE_NVRAM_LONG(isp, rp)
683163899Smjacob
684163899Smjacob#define	ISP_IOZPUT_8(isp, s, d)		*(d) = s
685163899Smjacob#define	ISP_IOZPUT_16(isp, s, d)	*(d) = bswap16(s)
686163899Smjacob#define	ISP_IOZPUT_32(isp, s, d)	*(d) = bswap32(s)
687163899Smjacob
688163899Smjacob#define	ISP_IOZGET_8(isp, s, d)		d = (*((uint8_t *)(s)))
689163899Smjacob#define	ISP_IOZGET_16(isp, s, d)	d = bswap16(*((uint16_t *)(s)))
690163899Smjacob#define	ISP_IOZGET_32(isp, s, d)	d = bswap32(*((uint32_t *)(s)))
691163899Smjacob
69287635Smjacob#endif
69364092Smjacob
694171159Smjacob#define	ISP_SWAP16(isp, s)	bswap16(s)
695171159Smjacob#define	ISP_SWAP32(isp, s)	bswap32(s)
696171159Smjacob
69748487Smjacob/*
69864092Smjacob * Includes of common header files
69948487Smjacob */
70035388Smjacob
70164092Smjacob#include <dev/isp/ispreg.h>
70264092Smjacob#include <dev/isp/ispvar.h>
70364092Smjacob#include <dev/isp/ispmbox.h>
70435388Smjacob
70564092Smjacob/*
70664092Smjacob * isp_osinfo definiitions && shorthand
70764092Smjacob */
70864092Smjacob#define	SIMQFRZ_RESOURCE	0x1
70964092Smjacob#define	SIMQFRZ_LOOPDOWN	0x2
71064092Smjacob#define	SIMQFRZ_TIMED		0x4
71164092Smjacob
71273246Smjacob#define	isp_dev		isp_osinfo.dev
71364092Smjacob
71464092Smjacob/*
71564092Smjacob * prototypes for isp_pci && isp_freebsd to share
71664092Smjacob */
717196008Smjacobextern int isp_attach(ispsoftc_t *);
718224856Smjacobextern int isp_detach(ispsoftc_t *);
719157943Smjacobextern void isp_uninit(ispsoftc_t *);
720196008Smjacobextern uint64_t isp_default_wwn(ispsoftc_t *, int, int, int);
72164092Smjacob
72264092Smjacob/*
72399756Smjacob * driver global data
72499756Smjacob */
72599756Smjacobextern int isp_announced;
726163899Smjacobextern int isp_fabric_hysteresis;
727163899Smjacobextern int isp_loop_down_limit;
728164272Smjacobextern int isp_gone_device_time;
729163899Smjacobextern int isp_quickboot_time;
730196008Smjacobextern int isp_autoconfig;
73199756Smjacob
73299756Smjacob/*
73364092Smjacob * Platform private flags
73464092Smjacob */
73562172Smjacob
73639235Sgibbs/*
737164272Smjacob * Platform Library Functions
738164272Smjacob */
739164272Smjacobvoid isp_prt(ispsoftc_t *, int level, const char *, ...) __printflike(3, 4);
740205698Smjacobvoid isp_xs_prt(ispsoftc_t *, XS_T *, int level, const char *, ...) __printflike(4, 5);
741164272Smjacobuint64_t isp_nanotime_sub(struct timespec *, struct timespec *);
742164272Smjacobint isp_mbox_acquire(ispsoftc_t *);
743164272Smjacobvoid isp_mbox_wait_complete(ispsoftc_t *, mbreg_t *);
744164272Smjacobvoid isp_mbox_notify_done(ispsoftc_t *);
745164272Smjacobvoid isp_mbox_release(ispsoftc_t *);
746196008Smjacobint isp_fc_scratch_acquire(ispsoftc_t *, int);
747164272Smjacobint isp_mstohz(int);
748169292Smjacobvoid isp_platform_intr(void *);
749169292Smjacobvoid isp_common_dmateardown(ispsoftc_t *, struct ccb_scsiio *, uint32_t);
750238869Smjacobint isp_fcp_next_crn(ispsoftc_t *, uint8_t *, XS_T *);
751164272Smjacob
752164272Smjacob/*
753169292Smjacob * Platform Version specific defines
754166177Smjacob */
755169292Smjacob#define	BUS_DMA_ROOTARG(x)	bus_get_dma_tag(x)
756169292Smjacob#define	isp_dma_tag_create(a, b, c, d, e, f, g, h, i, j, k, z)	\
757169292Smjacob	bus_dma_tag_create(a, b, c, d, e, f, g, h, i, j, k, \
758169292Smjacob	busdma_lock_mutex, &isp->isp_osinfo.lock, z)
759169292Smjacob
760166935Smjacob#define	isp_setup_intr	bus_setup_intr
761166177Smjacob
762169292Smjacob#define	isp_sim_alloc(a, b, c, d, e, f, g, h)	\
763169292Smjacob	cam_sim_alloc(a, b, c, d, e, &(d)->isp_osinfo.lock, f, g, h)
764169292Smjacob
765196008Smjacob#define	ISP_PATH_PRT(i, l, p, ...)					\
766196008Smjacob	if ((l) == ISP_LOGALL || ((l)& (i)->isp_dblev) != 0) {		\
767196008Smjacob                xpt_print(p, __VA_ARGS__);				\
768196008Smjacob        }
769196008Smjacob
770166177Smjacob/*
77164092Smjacob * Platform specific inline functions
77239235Sgibbs */
77344819Smjacob
77464092Smjacob/*
775164272Smjacob * ISP General Library functions
77664092Smjacob */
77744819Smjacob
778155228Smjacob#include <dev/isp/isp_library.h>
779155228Smjacob
78035388Smjacob#endif	/* _ISP_FREEBSD_H */
781