xref: /freebsd/sys/dev/isp/isp_freebsd.h (revision 93e779a26c651610ac6e7986d67ecc9ed2cadcbf)
1 /* $FreeBSD$ */
2 /*-
3  * Qlogic ISP SCSI Host Adapter FreeBSD Wrapper Definitions
4  *
5  * Copyright (c) 1997-2008 by Matthew Jacob
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice immediately at the beginning of the file, without modification,
13  *    this list of conditions, and the following disclaimer.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 #ifndef	_ISP_FREEBSD_H
30 #define	_ISP_FREEBSD_H
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/endian.h>
35 #include <sys/lock.h>
36 #include <sys/kernel.h>
37 #include <sys/queue.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
41 #include <sys/sysctl.h>
42 
43 #include <sys/proc.h>
44 #include <sys/bus.h>
45 #include <sys/taskqueue.h>
46 
47 #include <machine/bus.h>
48 #include <machine/cpu.h>
49 #include <machine/stdarg.h>
50 
51 #include <cam/cam.h>
52 #include <cam/cam_debug.h>
53 #include <cam/cam_ccb.h>
54 #include <cam/cam_sim.h>
55 #include <cam/cam_xpt.h>
56 #include <cam/cam_xpt_sim.h>
57 #include <cam/cam_debug.h>
58 #include <cam/scsi/scsi_all.h>
59 #include <cam/scsi/scsi_message.h>
60 
61 #include "opt_ddb.h"
62 #include "opt_isp.h"
63 
64 #define	ISP_PLATFORM_VERSION_MAJOR	7
65 #define	ISP_PLATFORM_VERSION_MINOR	10
66 
67 /*
68  * Efficiency- get rid of SBus code && tests unless we need them.
69  */
70 #ifdef __sparc64__
71 #define	ISP_SBUS_SUPPORTED	1
72 #else
73 #define	ISP_SBUS_SUPPORTED	0
74 #endif
75 
76 #define	ISP_IFLAGS	INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
77 
78 #define	N_XCMDS		64
79 #define	XCMD_SIZE	512
80 struct ispsoftc;
81 typedef union isp_ecmd {
82 	union isp_ecmd *	next;
83 	uint8_t			data[XCMD_SIZE];
84 } isp_ecmd_t;
85 isp_ecmd_t *	isp_get_ecmd(struct ispsoftc *);
86 void		isp_put_ecmd(struct ispsoftc *, isp_ecmd_t *);
87 
88 #ifdef	ISP_TARGET_MODE
89 /* Not quite right, but there was no bump for this change */
90 #if __FreeBSD_version < 225469
91 #define	SDFIXED(x)	(&x)
92 #else
93 #define	SDFIXED(x)	((struct scsi_sense_data_fixed *)(&x))
94 #endif
95 
96 #define	ISP_TARGET_FUNCTIONS	1
97 #define	ATPDPSIZE	4096
98 #define	ATPDPHASHSIZE	32
99 #define	ATPDPHASH(x)	((((x) >> 24) ^ ((x) >> 16) ^ ((x) >> 8) ^ (x)) &  \
100 			    ((ATPDPHASHSIZE) - 1))
101 
102 #include <dev/isp/isp_target.h>
103 typedef struct atio_private_data {
104 	LIST_ENTRY(atio_private_data)	next;
105 	uint32_t	orig_datalen;
106 	uint32_t	bytes_xfered;
107 	uint32_t	bytes_in_transit;
108 	uint32_t	tag;		/* typically f/w RX_ID */
109 	uint32_t	lun;
110 	uint32_t	nphdl;
111 	uint32_t	sid;
112 	uint32_t	portid;
113 	uint16_t	rxid;	/* wire rxid */
114 	uint16_t	oxid;	/* wire oxid */
115 	uint16_t	word3;	/* PRLI word3 params */
116 	uint16_t	ctcnt;	/* number of CTIOs currently active */
117 	uint8_t		seqno;	/* CTIO sequence number */
118 	uint32_t
119 			srr_notify_rcvd	: 1,
120 			cdb0		: 8,
121 			sendst		: 1,
122 			dead		: 1,
123 			tattr		: 3,
124 			state		: 3;
125 	void *		ests;
126 	/*
127 	 * The current SRR notify copy
128 	 */
129 	uint8_t		srr[64];	/*  sb QENTRY_LEN, but order of definitions is wrong */
130 	void *		srr_ccb;
131 	uint32_t	nsrr;
132 } atio_private_data_t;
133 #define	ATPD_STATE_FREE			0
134 #define	ATPD_STATE_ATIO			1
135 #define	ATPD_STATE_CAM			2
136 #define	ATPD_STATE_CTIO			3
137 #define	ATPD_STATE_LAST_CTIO		4
138 #define	ATPD_STATE_PDON			5
139 
140 #define	ATPD_CCB_OUTSTANDING		16
141 
142 #define	ATPD_SEQ_MASK			0x7f
143 #define	ATPD_SEQ_NOTIFY_CAM		0x80
144 #define	ATPD_SET_SEQNO(hdrp, atp)	((isphdr_t *)hdrp)->rqs_seqno &= ~ATPD_SEQ_MASK, ((isphdr_t *)hdrp)->rqs_seqno |= (atp)->seqno
145 #define	ATPD_GET_SEQNO(hdrp)		(((isphdr_t *)hdrp)->rqs_seqno & ATPD_SEQ_MASK)
146 #define	ATPD_GET_NCAM(hdrp)		((((isphdr_t *)hdrp)->rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0)
147 
148 typedef union inot_private_data inot_private_data_t;
149 union inot_private_data {
150 	inot_private_data_t *next;
151 	struct {
152 		isp_notify_t nt;	/* must be first! */
153 		uint8_t data[64];	/* sb QENTRY_LEN, but order of definitions is wrong */
154 		uint32_t tag_id, seq_id;
155 	} rd;
156 };
157 typedef struct isp_timed_notify_ack {
158 	void *isp;
159 	void *not;
160 	uint8_t data[64];	 /* sb QENTRY_LEN, but order of definitions is wrong */
161 	struct callout timer;
162 } isp_tna_t;
163 
164 TAILQ_HEAD(isp_ccbq, ccb_hdr);
165 typedef struct tstate {
166 	SLIST_ENTRY(tstate) next;
167 	lun_id_t ts_lun;
168 	struct cam_path *owner;
169 	struct isp_ccbq waitq;		/* waiting CCBs */
170 	struct ccb_hdr_slist atios;
171 	struct ccb_hdr_slist inots;
172 	uint32_t		hold;
173 	uint16_t		atio_count;
174 	uint16_t		inot_count;
175 	inot_private_data_t *	restart_queue;
176 	inot_private_data_t *	ntfree;
177 	inot_private_data_t	ntpool[ATPDPSIZE];
178 	LIST_HEAD(, atio_private_data)	atfree;
179 	LIST_HEAD(, atio_private_data)	atused[ATPDPHASHSIZE];
180 	atio_private_data_t	atpool[ATPDPSIZE];
181 } tstate_t;
182 
183 #define	LUN_HASH_SIZE		32
184 #define	LUN_HASH_FUNC(lun)	((lun) & (LUN_HASH_SIZE - 1))
185 
186 #endif
187 
188 /*
189  * Per command info.
190  */
191 struct isp_pcmd {
192 	struct isp_pcmd *	next;
193 	bus_dmamap_t 		dmap;		/* dma map for this command */
194 	struct ispsoftc *	isp;		/* containing isp */
195 	struct callout		wdog;		/* watchdog timer */
196 	uint32_t		datalen;	/* data length for this command (target mode only) */
197 	uint8_t			totslen;	/* sense length on status response */
198 	uint8_t			cumslen;	/* sense length on status response */
199 	uint8_t 		crn;		/* command reference number */
200 };
201 #define	ISP_PCMD(ccb)		(ccb)->ccb_h.spriv_ptr1
202 #define	PISP_PCMD(ccb)		((struct isp_pcmd *)ISP_PCMD(ccb))
203 
204 /*
205  * Per nexus info.
206  */
207 struct isp_nexus {
208 	uint64_t lun;			/* LUN for target */
209 	uint32_t tgt;			/* TGT for target */
210 	uint8_t crnseed;		/* next command reference number */
211 	struct isp_nexus *next;
212 };
213 #define	NEXUS_HASH_WIDTH	32
214 #define	INITIAL_NEXUS_COUNT	MAX_FC_TARG
215 #define	NEXUS_HASH(tgt, lun)	((tgt + lun) % NEXUS_HASH_WIDTH)
216 
217 /*
218  * Per channel information
219  */
220 SLIST_HEAD(tslist, tstate);
221 
222 struct isp_fc {
223 	struct cam_sim *sim;
224 	struct cam_path *path;
225 	struct ispsoftc *isp;
226 	struct proc *kproc;
227 	bus_dma_tag_t tdmat;
228 	bus_dmamap_t tdmap;
229 	uint64_t def_wwpn;
230 	uint64_t def_wwnn;
231 	uint32_t loop_down_time;
232 	uint32_t loop_down_limit;
233 	uint32_t gone_device_time;
234 	/*
235 	 * Per target/lun info- just to keep a per-ITL nexus crn count
236 	 */
237 	struct isp_nexus *nexus_hash[NEXUS_HASH_WIDTH];
238 	struct isp_nexus *nexus_free_list;
239 	uint32_t
240 		simqfrozen	: 3,
241 		default_id	: 8,
242 		hysteresis	: 8,
243 		def_role	: 2,	/* default role */
244 		gdt_running	: 1,
245 		loop_dead	: 1,
246 		fcbsy		: 1,
247 		ready		: 1;
248 	struct callout ldt;	/* loop down timer */
249 	struct callout gdt;	/* gone device timer */
250 	struct task ltask;
251 	struct task gtask;
252 #ifdef	ISP_TARGET_MODE
253 	struct tslist lun_hash[LUN_HASH_SIZE];
254 #if defined(DEBUG)
255 	unsigned int inject_lost_data_frame;
256 #endif
257 #endif
258 	int			num_threads;
259 };
260 
261 struct isp_spi {
262 	struct cam_sim *sim;
263 	struct cam_path *path;
264 	uint32_t
265 		simqfrozen	: 3,
266 		iid		: 4;
267 #ifdef	ISP_TARGET_MODE
268 	struct tslist lun_hash[LUN_HASH_SIZE];
269 #endif
270 	int			num_threads;
271 };
272 
273 struct isposinfo {
274 	/*
275 	 * Linkage, locking, and identity
276 	 */
277 	struct mtx		lock;
278 	device_t		dev;
279 	struct cdev *		cdev;
280 	struct intr_config_hook	ehook;
281 	struct cam_devq *	devq;
282 
283 	/*
284 	 * Firmware pointer
285 	 */
286 	const struct firmware *	fw;
287 
288 	/*
289 	 * DMA related sdtuff
290 	 */
291 	bus_space_tag_t		bus_tag;
292 	bus_dma_tag_t		dmat;
293 	bus_space_handle_t	bus_handle;
294 	bus_dma_tag_t		cdmat;
295 	bus_dmamap_t		cdmap;
296 
297 	/*
298 	 * Command and transaction related related stuff
299 	 */
300 	struct isp_pcmd *	pcmd_pool;
301 	struct isp_pcmd *	pcmd_free;
302 
303 	uint32_t
304 #ifdef	ISP_TARGET_MODE
305 		tmwanted	: 1,
306 		tmbusy		: 1,
307 #else
308 				: 2,
309 #endif
310 		sixtyfourbit	: 1,	/* sixtyfour bit platform */
311 		timer_active	: 1,
312 		autoconf	: 1,
313 		ehook_active	: 1,
314 		mbox_sleeping	: 1,
315 		mbox_sleep_ok	: 1,
316 		mboxcmd_done	: 1,
317 		mboxbsy		: 1;
318 
319 	struct callout		tmo;	/* general timer */
320 
321 	/*
322 	 * misc- needs to be sorted better XXXXXX
323 	 */
324 	int			framesize;
325 	int			exec_throttle;
326 	int			cont_max;
327 
328 	bus_addr_t		ecmd_dma;
329 	isp_ecmd_t *		ecmd_base;
330 	isp_ecmd_t *		ecmd_free;
331 
332 	/*
333 	 * Per-type private storage...
334 	 */
335 	union {
336 		struct isp_fc *fc;
337 		struct isp_spi *spi;
338 		void *ptr;
339 	} pc;
340 
341 	int			is_exiting;
342 };
343 #define	ISP_FC_PC(isp, chan)	(&(isp)->isp_osinfo.pc.fc[(chan)])
344 #define	ISP_SPI_PC(isp, chan)	(&(isp)->isp_osinfo.pc.spi[(chan)])
345 #define	ISP_GET_PC(isp, chan, tag, rslt)		\
346 	if (IS_SCSI(isp)) {				\
347 		rslt = ISP_SPI_PC(isp, chan)-> tag;	\
348 	} else {					\
349 		rslt = ISP_FC_PC(isp, chan)-> tag;	\
350 	}
351 #define	ISP_GET_PC_ADDR(isp, chan, tag, rp)		\
352 	if (IS_SCSI(isp)) {				\
353 		rp = &ISP_SPI_PC(isp, chan)-> tag;	\
354 	} else {					\
355 		rp = &ISP_FC_PC(isp, chan)-> tag;	\
356 	}
357 #define	ISP_SET_PC(isp, chan, tag, val)			\
358 	if (IS_SCSI(isp)) {				\
359 		ISP_SPI_PC(isp, chan)-> tag = val;	\
360 	} else {					\
361 		ISP_FC_PC(isp, chan)-> tag = val;	\
362 	}
363 
364 #define	FCP_NEXT_CRN	isp_fcp_next_crn
365 #define	isp_lock	isp_osinfo.lock
366 #define	isp_bus_tag	isp_osinfo.bus_tag
367 #define	isp_bus_handle	isp_osinfo.bus_handle
368 
369 /*
370  * Locking macros...
371  */
372 #define	ISP_LOCK(isp)	mtx_lock(&(isp)->isp_osinfo.lock)
373 #define	ISP_UNLOCK(isp)	mtx_unlock(&(isp)->isp_osinfo.lock)
374 #define	ISP_ASSERT_LOCKED(isp)	mtx_assert(&(isp)->isp_osinfo.lock, MA_OWNED)
375 
376 /*
377  * Required Macros/Defines
378  */
379 #define	ISP_FC_SCRLEN		0x1000
380 
381 #define	ISP_MEMZERO(a, b)	memset(a, 0, b)
382 #define	ISP_MEMCPY		memcpy
383 #define	ISP_SNPRINTF		snprintf
384 #define	ISP_DELAY(x)		DELAY(x)
385 #if __FreeBSD_version < 1000029
386 #define	ISP_SLEEP(isp, x)	msleep(&(isp)->isp_osinfo.is_exiting, \
387     &(isp)->isp_osinfo.lock, 0, "isp_sleep", ((x) + tick - 1) / tick)
388 #else
389 #define	ISP_SLEEP(isp, x)	msleep_sbt(&(isp)->isp_osinfo.is_exiting, \
390     &(isp)->isp_osinfo.lock, 0, "isp_sleep", (x) * SBT_1US, 0, 0)
391 #endif
392 
393 #define	ISP_MIN			imin
394 
395 #ifndef	DIAGNOSTIC
396 #define	ISP_INLINE		__inline
397 #else
398 #define	ISP_INLINE
399 #endif
400 
401 #define	NANOTIME_T		struct timespec
402 #define	GET_NANOTIME		nanotime
403 #define	GET_NANOSEC(x)		((x)->tv_sec * 1000000000 + (x)->tv_nsec)
404 #define	NANOTIME_SUB		isp_nanotime_sub
405 
406 #define	MAXISPREQUEST(isp)	((IS_FC(isp) || IS_ULTRA2(isp))? 1024 : 256)
407 
408 #define	MEMORYBARRIER(isp, type, offset, size, chan)		\
409 switch (type) {							\
410 case SYNC_SFORDEV:						\
411 {								\
412 	struct isp_fc *fc = ISP_FC_PC(isp, chan);		\
413 	bus_dmamap_sync(fc->tdmat, fc->tdmap,			\
414 	   BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);		\
415 	break;							\
416 }								\
417 case SYNC_REQUEST:						\
418 	bus_dmamap_sync(isp->isp_osinfo.cdmat,			\
419 	   isp->isp_osinfo.cdmap, 				\
420 	   BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);		\
421 	break;							\
422 case SYNC_SFORCPU:						\
423 {								\
424 	struct isp_fc *fc = ISP_FC_PC(isp, chan);		\
425 	bus_dmamap_sync(fc->tdmat, fc->tdmap,			\
426 	   BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);	\
427 	break;							\
428 }								\
429 case SYNC_RESULT:						\
430 	bus_dmamap_sync(isp->isp_osinfo.cdmat, 			\
431 	   isp->isp_osinfo.cdmap,				\
432 	   BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);	\
433 	break;							\
434 case SYNC_REG:							\
435 	bus_space_barrier(isp->isp_osinfo.bus_tag,		\
436 	    isp->isp_osinfo.bus_handle, offset, size,		\
437 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);	\
438 	break;							\
439 default:							\
440 	break;							\
441 }
442 
443 #define	MEMORYBARRIERW(isp, type, offset, size, chan)		\
444 switch (type) {							\
445 case SYNC_SFORDEV:						\
446 {								\
447 	struct isp_fc *fc = ISP_FC_PC(isp, chan);		\
448 	bus_dmamap_sync(fc->tdmat, fc->tdmap,			\
449 	   BUS_DMASYNC_PREWRITE);				\
450 	break;							\
451 }								\
452 case SYNC_REQUEST:						\
453 	bus_dmamap_sync(isp->isp_osinfo.cdmat,			\
454 	   isp->isp_osinfo.cdmap, BUS_DMASYNC_PREWRITE);	\
455 	break;							\
456 case SYNC_SFORCPU:						\
457 {								\
458 	struct isp_fc *fc = ISP_FC_PC(isp, chan);		\
459 	bus_dmamap_sync(fc->tdmat, fc->tdmap,			\
460 	   BUS_DMASYNC_POSTWRITE);				\
461 	break;							\
462 }								\
463 case SYNC_RESULT:						\
464 	bus_dmamap_sync(isp->isp_osinfo.cdmat, 			\
465 	   isp->isp_osinfo.cdmap, BUS_DMASYNC_POSTWRITE);	\
466 	break;							\
467 case SYNC_REG:							\
468 	bus_space_barrier(isp->isp_osinfo.bus_tag,		\
469 	    isp->isp_osinfo.bus_handle, offset, size,		\
470 	    BUS_SPACE_BARRIER_WRITE);				\
471 	break;							\
472 default:							\
473 	break;							\
474 }
475 
476 #define	MBOX_ACQUIRE			isp_mbox_acquire
477 #define	MBOX_WAIT_COMPLETE		isp_mbox_wait_complete
478 #define	MBOX_NOTIFY_COMPLETE		isp_mbox_notify_done
479 #define	MBOX_RELEASE			isp_mbox_release
480 
481 #define	FC_SCRATCH_ACQUIRE		isp_fc_scratch_acquire
482 #define	FC_SCRATCH_RELEASE(isp, chan)	isp->isp_osinfo.pc.fc[chan].fcbsy = 0
483 
484 #ifndef	SCSI_GOOD
485 #define	SCSI_GOOD	SCSI_STATUS_OK
486 #endif
487 #ifndef	SCSI_CHECK
488 #define	SCSI_CHECK	SCSI_STATUS_CHECK_COND
489 #endif
490 #ifndef	SCSI_BUSY
491 #define	SCSI_BUSY	SCSI_STATUS_BUSY
492 #endif
493 #ifndef	SCSI_QFULL
494 #define	SCSI_QFULL	SCSI_STATUS_QUEUE_FULL
495 #endif
496 
497 #define	XS_T			struct ccb_scsiio
498 #define	XS_DMA_ADDR_T		bus_addr_t
499 #define XS_GET_DMA64_SEG(a, b, c)		\
500 {						\
501 	ispds64_t *d = a;			\
502 	bus_dma_segment_t *e = b;		\
503 	uint32_t f = c;				\
504 	e += f;					\
505         d->ds_base = DMA_LO32(e->ds_addr);	\
506         d->ds_basehi = DMA_HI32(e->ds_addr);	\
507         d->ds_count = e->ds_len;		\
508 }
509 #define XS_GET_DMA_SEG(a, b, c)			\
510 {						\
511 	ispds_t *d = a;				\
512 	bus_dma_segment_t *e = b;		\
513 	uint32_t f = c;				\
514 	e += f;					\
515         d->ds_base = DMA_LO32(e->ds_addr);	\
516         d->ds_count = e->ds_len;		\
517 }
518 #define	XS_ISP(ccb)		cam_sim_softc(xpt_path_sim((ccb)->ccb_h.path))
519 #define	XS_CHANNEL(ccb)		cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path))
520 #define	XS_TGT(ccb)		(ccb)->ccb_h.target_id
521 #define	XS_LUN(ccb)		(ccb)->ccb_h.target_lun
522 
523 #define	XS_CDBP(ccb)	\
524 	(((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \
525 	 (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes)
526 
527 #define	XS_CDBLEN(ccb)		(ccb)->cdb_len
528 #define	XS_XFRLEN(ccb)		(ccb)->dxfer_len
529 #define	XS_TIME(ccb)		(ccb)->ccb_h.timeout
530 #define	XS_GET_RESID(ccb)	(ccb)->resid
531 #define	XS_SET_RESID(ccb, r)	(ccb)->resid = r
532 #define	XS_STSP(ccb)		(&(ccb)->scsi_status)
533 #define	XS_SNSP(ccb)		(&(ccb)->sense_data)
534 
535 #define	XS_TOT_SNSLEN(ccb)	ccb->sense_len
536 #define	XS_CUR_SNSLEN(ccb)	(ccb->sense_len - ccb->sense_resid)
537 
538 #define	XS_SNSKEY(ccb)		(scsi_get_sense_key(&(ccb)->sense_data, \
539 				 ccb->sense_len - ccb->sense_resid, 1))
540 
541 #define	XS_SNSASC(ccb)		(scsi_get_asc(&(ccb)->sense_data,	\
542 				 ccb->sense_len - ccb->sense_resid, 1))
543 
544 #define	XS_SNSASCQ(ccb)		(scsi_get_ascq(&(ccb)->sense_data,	\
545 				 ccb->sense_len - ccb->sense_resid, 1))
546 #define	XS_TAG_P(ccb)	\
547 	(((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \
548 	 (ccb)->tag_action != CAM_TAG_ACTION_NONE)
549 
550 #define	XS_TAG_TYPE(ccb)	\
551 	((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \
552 	 ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG))
553 
554 
555 #define	XS_SETERR(ccb, v)	(ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \
556 				(ccb)->ccb_h.status |= v
557 
558 #	define	HBA_NOERROR		CAM_REQ_INPROG
559 #	define	HBA_BOTCH		CAM_UNREC_HBA_ERROR
560 #	define	HBA_CMDTIMEOUT		CAM_CMD_TIMEOUT
561 #	define	HBA_SELTIMEOUT		CAM_SEL_TIMEOUT
562 #	define	HBA_TGTBSY		CAM_SCSI_STATUS_ERROR
563 #	define	HBA_BUSRESET		CAM_SCSI_BUS_RESET
564 #	define	HBA_ABORTED		CAM_REQ_ABORTED
565 #	define	HBA_DATAOVR		CAM_DATA_RUN_ERR
566 #	define	HBA_ARQFAIL		CAM_AUTOSENSE_FAIL
567 
568 
569 #define	XS_ERR(ccb)		((ccb)->ccb_h.status & CAM_STATUS_MASK)
570 
571 #define	XS_NOERR(ccb)		(((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
572 
573 #define	XS_INITERR(ccb)		XS_SETERR(ccb, CAM_REQ_INPROG), ccb->sense_resid = ccb->sense_len
574 
575 #define	XS_SAVE_SENSE(xs, sense_ptr, totslen, slen)	do {			\
576 		uint32_t tlen = slen;						\
577 		if (tlen > (xs)->sense_len)					\
578 			tlen = (xs)->sense_len;					\
579 		PISP_PCMD(xs)->totslen = imin((xs)->sense_len, totslen);	\
580 		PISP_PCMD(xs)->cumslen = tlen;					\
581 		memcpy(&(xs)->sense_data, sense_ptr, tlen);			\
582 		(xs)->sense_resid = (xs)->sense_len - tlen;			\
583 		(xs)->ccb_h.status |= CAM_AUTOSNS_VALID;			\
584 	} while (0)
585 
586 #define	XS_SENSE_APPEND(xs, xsnsp, xsnsl)	do {				\
587 		uint32_t off = PISP_PCMD(xs)->cumslen;				\
588 		uint8_t *ptr = &((uint8_t *)(&(xs)->sense_data))[off];		\
589 		uint32_t amt = imin(xsnsl, PISP_PCMD(xs)->totslen - off);	\
590 		if (amt) {							\
591 			memcpy(ptr, xsnsp, amt);				\
592 			(xs)->sense_resid -= amt;				\
593 			PISP_PCMD(xs)->cumslen += amt;				\
594 		}								\
595 	} while (0)
596 
597 #define	XS_SENSE_VALID(xs)	(((xs)->ccb_h.status & CAM_AUTOSNS_VALID) != 0)
598 
599 #define	DEFAULT_FRAMESIZE(isp)		isp->isp_osinfo.framesize
600 #define	DEFAULT_EXEC_THROTTLE(isp)	isp->isp_osinfo.exec_throttle
601 
602 #define	DEFAULT_ROLE(isp, chan)	\
603 	(IS_FC(isp)? ISP_FC_PC(isp, chan)->def_role : ISP_ROLE_INITIATOR)
604 
605 #define	DEFAULT_IID(isp, chan)		isp->isp_osinfo.pc.spi[chan].iid
606 
607 #define	DEFAULT_LOOPID(x, chan)		isp->isp_osinfo.pc.fc[chan].default_id
608 
609 #define DEFAULT_NODEWWN(isp, chan)  	isp_default_wwn(isp, chan, 0, 1)
610 #define DEFAULT_PORTWWN(isp, chan)  	isp_default_wwn(isp, chan, 0, 0)
611 #define ACTIVE_NODEWWN(isp, chan)   	isp_default_wwn(isp, chan, 1, 1)
612 #define ACTIVE_PORTWWN(isp, chan)   	isp_default_wwn(isp, chan, 1, 0)
613 
614 
615 #if	BYTE_ORDER == BIG_ENDIAN
616 #ifdef	ISP_SBUS_SUPPORTED
617 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
618 #define	ISP_IOXPUT_16(isp, s, d)				\
619 	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap16(s)
620 #define	ISP_IOXPUT_32(isp, s, d)				\
621 	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap32(s)
622 #define	ISP_IOXGET_8(isp, s, d)		d = (*((uint8_t *)s))
623 #define	ISP_IOXGET_16(isp, s, d)				\
624 	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
625 	*((uint16_t *)s) : bswap16(*((uint16_t *)s))
626 #define	ISP_IOXGET_32(isp, s, d)				\
627 	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
628 	*((uint32_t *)s) : bswap32(*((uint32_t *)s))
629 
630 #else	/* ISP_SBUS_SUPPORTED */
631 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
632 #define	ISP_IOXPUT_16(isp, s, d)	*(d) = bswap16(s)
633 #define	ISP_IOXPUT_32(isp, s, d)	*(d) = bswap32(s)
634 #define	ISP_IOXGET_8(isp, s, d)		d = (*((uint8_t *)s))
635 #define	ISP_IOXGET_16(isp, s, d)	d = bswap16(*((uint16_t *)s))
636 #define	ISP_IOXGET_32(isp, s, d)	d = bswap32(*((uint32_t *)s))
637 #endif
638 #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)	*rp = bswap16(*rp)
639 #define	ISP_SWIZZLE_NVRAM_LONG(isp, rp)	*rp = bswap32(*rp)
640 
641 #define	ISP_IOZGET_8(isp, s, d)		d = (*((uint8_t *)s))
642 #define	ISP_IOZGET_16(isp, s, d)	d = (*((uint16_t *)s))
643 #define	ISP_IOZGET_32(isp, s, d)	d = (*((uint32_t *)s))
644 #define	ISP_IOZPUT_8(isp, s, d)		*(d) = s
645 #define	ISP_IOZPUT_16(isp, s, d)	*(d) = s
646 #define	ISP_IOZPUT_32(isp, s, d)	*(d) = s
647 
648 
649 #else
650 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
651 #define	ISP_IOXPUT_16(isp, s, d)	*(d) = s
652 #define	ISP_IOXPUT_32(isp, s, d)	*(d) = s
653 #define	ISP_IOXGET_8(isp, s, d)		d = *(s)
654 #define	ISP_IOXGET_16(isp, s, d)	d = *(s)
655 #define	ISP_IOXGET_32(isp, s, d)	d = *(s)
656 #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)
657 #define	ISP_SWIZZLE_NVRAM_LONG(isp, rp)
658 
659 #define	ISP_IOZPUT_8(isp, s, d)		*(d) = s
660 #define	ISP_IOZPUT_16(isp, s, d)	*(d) = bswap16(s)
661 #define	ISP_IOZPUT_32(isp, s, d)	*(d) = bswap32(s)
662 
663 #define	ISP_IOZGET_8(isp, s, d)		d = (*((uint8_t *)(s)))
664 #define	ISP_IOZGET_16(isp, s, d)	d = bswap16(*((uint16_t *)(s)))
665 #define	ISP_IOZGET_32(isp, s, d)	d = bswap32(*((uint32_t *)(s)))
666 
667 #endif
668 
669 #define	ISP_SWAP16(isp, s)	bswap16(s)
670 #define	ISP_SWAP32(isp, s)	bswap32(s)
671 
672 /*
673  * Includes of common header files
674  */
675 
676 #include <dev/isp/ispreg.h>
677 #include <dev/isp/ispvar.h>
678 #include <dev/isp/ispmbox.h>
679 
680 /*
681  * isp_osinfo definiitions && shorthand
682  */
683 #define	SIMQFRZ_RESOURCE	0x1
684 #define	SIMQFRZ_LOOPDOWN	0x2
685 #define	SIMQFRZ_TIMED		0x4
686 
687 #define	isp_dev		isp_osinfo.dev
688 
689 /*
690  * prototypes for isp_pci && isp_freebsd to share
691  */
692 extern int isp_attach(ispsoftc_t *);
693 extern int isp_detach(ispsoftc_t *);
694 extern void isp_uninit(ispsoftc_t *);
695 extern uint64_t isp_default_wwn(ispsoftc_t *, int, int, int);
696 
697 /*
698  * driver global data
699  */
700 extern int isp_announced;
701 extern int isp_fabric_hysteresis;
702 extern int isp_loop_down_limit;
703 extern int isp_gone_device_time;
704 extern int isp_quickboot_time;
705 
706 /*
707  * Platform private flags
708  */
709 
710 /*
711  * Platform Library Functions
712  */
713 void isp_prt(ispsoftc_t *, int level, const char *, ...) __printflike(3, 4);
714 void isp_xs_prt(ispsoftc_t *, XS_T *, int level, const char *, ...) __printflike(4, 5);
715 uint64_t isp_nanotime_sub(struct timespec *, struct timespec *);
716 int isp_mbox_acquire(ispsoftc_t *);
717 void isp_mbox_wait_complete(ispsoftc_t *, mbreg_t *);
718 void isp_mbox_notify_done(ispsoftc_t *);
719 void isp_mbox_release(ispsoftc_t *);
720 int isp_fc_scratch_acquire(ispsoftc_t *, int);
721 int isp_mstohz(int);
722 void isp_platform_intr(void *);
723 void isp_common_dmateardown(ispsoftc_t *, struct ccb_scsiio *, uint32_t);
724 void isp_fcp_reset_crn(ispsoftc_t *, int, uint32_t, int);
725 int isp_fcp_next_crn(ispsoftc_t *, uint8_t *, XS_T *);
726 
727 /*
728  * Platform Version specific defines
729  */
730 #define	BUS_DMA_ROOTARG(x)	bus_get_dma_tag(x)
731 #define	isp_dma_tag_create(a, b, c, d, e, f, g, h, i, j, k, z)	\
732 	bus_dma_tag_create(a, b, c, d, e, f, g, h, i, j, k, \
733 	busdma_lock_mutex, &isp->isp_osinfo.lock, z)
734 
735 #define	isp_setup_intr	bus_setup_intr
736 
737 #define	isp_sim_alloc(a, b, c, d, e, f, g, h)	\
738 	cam_sim_alloc(a, b, c, d, e, &(d)->isp_osinfo.lock, f, g, h)
739 
740 #define	ISP_PATH_PRT(i, l, p, ...)					\
741 	if ((l) == ISP_LOGALL || ((l)& (i)->isp_dblev) != 0) {		\
742                 xpt_print(p, __VA_ARGS__);				\
743         }
744 
745 /*
746  * Platform specific inline functions
747  */
748 
749 /*
750  * ISP General Library functions
751  */
752 
753 #include <dev/isp/isp_library.h>
754 
755 #endif	/* _ISP_FREEBSD_H */
756