isp_freebsd.h revision 146073
1/* $FreeBSD: head/sys/dev/isp/isp_freebsd.h 146073 2005-05-11 00:22:17Z mjacob $ */
2/*-
3 * Qlogic ISP SCSI Host Adapter FreeBSD Wrapper Definitions
4 * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002 by Matthew Jacob
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice immediately at the beginning of the file, without modification,
11 *    this list of conditions, and the following disclaimer.
12 * 2. The name of the author may not be used to endorse or promote products
13 *    derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27#ifndef	_ISP_FREEBSD_H
28#define	_ISP_FREEBSD_H
29
30#define	ISP_PLATFORM_VERSION_MAJOR	5
31#define	ISP_PLATFORM_VERSION_MINOR	9
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/endian.h>
36#include <sys/kernel.h>
37#include <sys/queue.h>
38#include <sys/lock.h>
39#include <sys/malloc.h>
40#include <sys/mutex.h>
41#include <sys/condvar.h>
42#include <sys/proc.h>
43#include <sys/bus.h>
44
45#include <machine/bus_memio.h>
46#include <machine/bus_pio.h>
47#include <machine/bus.h>
48#include <machine/clock.h>
49#include <machine/cpu.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#ifdef	PAE
65#define	ISP_DAC_SUPPORTED	1
66#endif
67
68/*
69 * Efficiency- get rid of SBus code && tests unless we need them.
70 */
71#if	_MACHINE_ARCH == sparc64
72#define	ISP_SBUS_SUPPORTED	1
73#else
74#define	ISP_SBUS_SUPPORTED	0
75#endif
76
77#define	HANDLE_LOOPSTATE_IN_OUTER_LAYERS	1
78/* #define	ISP_SMPLOCK			1 */
79
80#ifdef	ISP_SMPLOCK
81#define	ISP_IFLAGS	INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
82#else
83#define	ISP_IFLAGS	INTR_TYPE_CAM | INTR_ENTROPY
84#endif
85
86typedef void ispfwfunc(int, int, int, u_int16_t **);
87
88#ifdef	ISP_TARGET_MODE
89#define	ISP_TARGET_FUNCTIONS	1
90#define	ATPDPSIZE	256
91typedef struct {
92	u_int32_t	orig_datalen;
93	u_int32_t	bytes_xfered;
94	u_int32_t	last_xframt;
95	u_int32_t	tag	: 16,
96			lun	: 13,	/* not enough */
97			state	: 3;
98} atio_private_data_t;
99#define	ATPD_STATE_FREE			0
100#define	ATPD_STATE_ATIO			1
101#define	ATPD_STATE_CAM			2
102#define	ATPD_STATE_CTIO			3
103#define	ATPD_STATE_LAST_CTIO		4
104#define	ATPD_STATE_PDON			5
105
106typedef struct tstate {
107	struct tstate *next;
108	struct cam_path *owner;
109	struct ccb_hdr_slist atios;
110	struct ccb_hdr_slist inots;
111	lun_id_t lun;
112	int bus;
113	u_int32_t hold;
114	int atio_count;
115	int inot_count;
116} tstate_t;
117
118#define	LUN_HASH_SIZE			32
119#define	LUN_HASH_FUNC(isp, port, lun)					\
120	((IS_DUALBUS(isp)) ?						\
121		(((lun) & ((LUN_HASH_SIZE >> 1) - 1)) << (port)) :	\
122		((lun) & (LUN_HASH_SIZE - 1)))
123#endif
124
125struct isposinfo {
126	struct ispsoftc *	next;
127	u_int64_t		default_port_wwn;
128	u_int64_t		default_node_wwn;
129	u_int32_t		default_id;
130	device_t		dev;
131	struct cam_sim		*sim;
132	struct cam_path		*path;
133	struct cam_sim		*sim2;
134	struct cam_path		*path2;
135	struct intr_config_hook	ehook;
136	u_int8_t		: 1,
137		fcbsy		: 1,
138		ktmature	: 1,
139		mboxwaiting	: 1,
140		intsok		: 1,
141		simqfrozen	: 3;
142	struct mtx		lock;
143	struct cv		kthread_cv;
144	struct proc		*kproc;
145	bus_dma_tag_t		cdmat;
146	bus_dmamap_t		cdmap;
147#define	isp_cdmat		isp_osinfo.cdmat
148#define	isp_cdmap		isp_osinfo.cdmap
149#ifdef	ISP_TARGET_MODE
150#define	TM_WILDCARD_ENABLED	0x02
151#define	TM_TMODE_ENABLED	0x01
152	u_int8_t		tmflags[2];	/* two busses */
153#define	NLEACT	4
154	union ccb *		leact[NLEACT];
155	tstate_t		tsdflt[2];	/* two busses */
156	tstate_t		*lun_hash[LUN_HASH_SIZE];
157	atio_private_data_t	atpdp[ATPDPSIZE];
158#endif
159};
160
161#define	isp_lock	isp_osinfo.lock
162
163/*
164 * Locking macros...
165 */
166
167#ifdef	ISP_SMPLOCK
168#define	ISP_LOCK(x)		mtx_lock(&(x)->isp_lock)
169#define	ISP_UNLOCK(x)		mtx_unlock(&(x)->isp_lock)
170#define	ISPLOCK_2_CAMLOCK(isp)	\
171	mtx_unlock(&(isp)->isp_lock); mtx_lock(&Giant)
172#define	CAMLOCK_2_ISPLOCK(isp)	\
173	mtx_unlock(&Giant); mtx_lock(&(isp)->isp_lock)
174#else
175#define	ISP_LOCK(x)		do { } while (0)
176#define	ISP_UNLOCK(x)		do { } while (0)
177#define	ISPLOCK_2_CAMLOCK(isp)	do { } while (0)
178#define	CAMLOCK_2_ISPLOCK(isp)	do { } while (0)
179#endif
180
181/*
182 * Required Macros/Defines
183 */
184
185#define	INLINE			__inline
186
187#define	ISP2100_SCRLEN		0x800
188
189#define	MEMZERO			bzero
190#define	MEMCPY(dst, src, amt)	bcopy((src), (dst), (amt))
191#define	SNPRINTF		snprintf
192#define	USEC_DELAY		DELAY
193#define	USEC_SLEEP(isp, x)		\
194	if (isp->isp_osinfo.intsok)	\
195		ISP_UNLOCK(isp);	\
196	DELAY(x);			\
197	if (isp->isp_osinfo.intsok)	\
198		ISP_LOCK(isp)
199
200#define	NANOTIME_T		struct timespec
201#define	GET_NANOTIME		nanotime
202#define	GET_NANOSEC(x)		((x)->tv_sec * 1000000000 + (x)->tv_nsec)
203#define	NANOTIME_SUB		nanotime_sub
204
205#define	MAXISPREQUEST(isp)	((IS_FC(isp) || IS_ULTRA2(isp))? 1024 : 256)
206
207#define	MEMORYBARRIER(isp, type, offset, size)			\
208switch (type) {							\
209case SYNC_SFORDEV:						\
210case SYNC_REQUEST:						\
211	bus_dmamap_sync(isp->isp_cdmat, isp->isp_cdmap, 	\
212	   BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);		\
213	break;							\
214case SYNC_SFORCPU:						\
215case SYNC_RESULT:						\
216	bus_dmamap_sync(isp->isp_cdmat, isp->isp_cdmap,		\
217	   BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);	\
218	break;							\
219default:							\
220	break;							\
221}
222
223#define	MBOX_ACQUIRE(isp)
224#define	MBOX_WAIT_COMPLETE		isp_mbox_wait_complete
225#define	MBOX_NOTIFY_COMPLETE(isp)	\
226	if (isp->isp_osinfo.mboxwaiting) { \
227		isp->isp_osinfo.mboxwaiting = 0; \
228		wakeup(&isp->isp_mbxworkp); \
229	} \
230	isp->isp_mboxbsy = 0
231#define	MBOX_RELEASE(isp)
232
233#define	FC_SCRATCH_ACQUIRE(isp)						\
234	if (isp->isp_osinfo.fcbsy) {					\
235		isp_prt(isp, ISP_LOGWARN,				\
236		    "FC scratch area busy (line %d)!", __LINE__);	\
237	} else								\
238		isp->isp_osinfo.fcbsy = 1
239#define	FC_SCRATCH_RELEASE(isp)		 isp->isp_osinfo.fcbsy = 0
240
241#ifndef	SCSI_GOOD
242#define	SCSI_GOOD	SCSI_STATUS_OK
243#endif
244#ifndef	SCSI_CHECK
245#define	SCSI_CHECK	SCSI_STATUS_CHECK_COND
246#endif
247#ifndef	SCSI_BUSY
248#define	SCSI_BUSY	SCSI_STATUS_BUSY
249#endif
250#ifndef	SCSI_QFULL
251#define	SCSI_QFULL	SCSI_STATUS_QUEUE_FULL
252#endif
253
254#define	XS_T			struct ccb_scsiio
255#define	XS_ISP(ccb)		((struct ispsoftc *) (ccb)->ccb_h.spriv_ptr1)
256#define	XS_CHANNEL(ccb)		cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path))
257#define	XS_TGT(ccb)		(ccb)->ccb_h.target_id
258#define	XS_LUN(ccb)		(ccb)->ccb_h.target_lun
259
260#define	XS_CDBP(ccb)	\
261	(((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \
262	 (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes)
263
264#define	XS_CDBLEN(ccb)		(ccb)->cdb_len
265#define	XS_XFRLEN(ccb)		(ccb)->dxfer_len
266#define	XS_TIME(ccb)		(ccb)->ccb_h.timeout
267#define	XS_RESID(ccb)		(ccb)->resid
268#define	XS_STSP(ccb)		(&(ccb)->scsi_status)
269#define	XS_SNSP(ccb)		(&(ccb)->sense_data)
270
271#define	XS_SNSLEN(ccb)		\
272	imin((sizeof((ccb)->sense_data)), ccb->sense_len)
273
274#define	XS_SNSKEY(ccb)		((ccb)->sense_data.flags & 0xf)
275#define	XS_TAG_P(ccb)	\
276	(((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \
277	 (ccb)->tag_action != CAM_TAG_ACTION_NONE)
278
279#define	XS_TAG_TYPE(ccb)	\
280	((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \
281	 ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG))
282
283
284#define	XS_SETERR(ccb, v)	(ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \
285				(ccb)->ccb_h.status |= v, \
286				(ccb)->ccb_h.spriv_field0 |= ISP_SPRIV_ERRSET
287
288#	define	HBA_NOERROR		CAM_REQ_INPROG
289#	define	HBA_BOTCH		CAM_UNREC_HBA_ERROR
290#	define	HBA_CMDTIMEOUT		CAM_CMD_TIMEOUT
291#	define	HBA_SELTIMEOUT		CAM_SEL_TIMEOUT
292#	define	HBA_TGTBSY		CAM_SCSI_STATUS_ERROR
293#	define	HBA_BUSRESET		CAM_SCSI_BUS_RESET
294#	define	HBA_ABORTED		CAM_REQ_ABORTED
295#	define	HBA_DATAOVR		CAM_DATA_RUN_ERR
296#	define	HBA_ARQFAIL		CAM_AUTOSENSE_FAIL
297
298
299#define	XS_ERR(ccb)		((ccb)->ccb_h.status & CAM_STATUS_MASK)
300
301#define	XS_NOERR(ccb)		\
302	(((ccb)->ccb_h.spriv_field0 & ISP_SPRIV_ERRSET) == 0 || \
303	 ((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
304
305#define	XS_INITERR(ccb)		\
306	XS_SETERR(ccb, CAM_REQ_INPROG), (ccb)->ccb_h.spriv_field0 = 0
307
308#define	XS_SAVE_SENSE(xs, sp)				\
309	(xs)->ccb_h.status |= CAM_AUTOSNS_VALID,	\
310	bcopy(sp->req_sense_data, &(xs)->sense_data,	\
311	    imin(XS_SNSLEN(xs), sp->req_sense_len))
312
313#define	XS_SET_STATE_STAT(a, b, c)
314
315#define	DEFAULT_IID(x)		(isp)->isp_osinfo.default_id
316#define	DEFAULT_LOOPID(x)	(isp)->isp_osinfo.default_id
317#define	DEFAULT_NODEWWN(isp)	(isp)->isp_osinfo.default_node_wwn
318#define	DEFAULT_PORTWWN(isp)	(isp)->isp_osinfo.default_port_wwn
319#define	ISP_NODEWWN(isp)	FCPARAM(isp)->isp_nodewwn
320#define	ISP_PORTWWN(isp)	FCPARAM(isp)->isp_portwwn
321
322#if	BYTE_ORDER == BIG_ENDIAN
323#ifdef	ISP_SBUS_SUPPORTED
324#define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
325#define	ISP_IOXPUT_16(isp, s, d)				\
326	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap16(s)
327#define	ISP_IOXPUT_32(isp, s, d)				\
328	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap32(s)
329#define	ISP_IOXGET_8(isp, s, d)		d = (*((u_int8_t *)s))
330#define	ISP_IOXGET_16(isp, s, d)				\
331	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
332	*((u_int16_t *)s) : bswap16(*((u_int16_t *)s))
333#define	ISP_IOXGET_32(isp, s, d)				\
334	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
335	*((u_int32_t *)s) : bswap32(*((u_int32_t *)s))
336#else
337#define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
338#define	ISP_IOXPUT_16(isp, s, d)	*(d) = bswap16(s)
339#define	ISP_IOXPUT_32(isp, s, d)	*(d) = bswap32(s)
340#define	ISP_IOXGET_8(isp, s, d)		d = (*((u_int8_t *)s))
341#define	ISP_IOXGET_16(isp, s, d)	d = bswap16(*((u_int16_t *)s))
342#define	ISP_IOXGET_32(isp, s, d)	d = bswap32(*((u_int32_t *)s))
343#endif
344#define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)	*rp = bswap16(*rp)
345#else
346#define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
347#define	ISP_IOXPUT_16(isp, s, d)	*(d) = s
348#define	ISP_IOXPUT_32(isp, s, d)	*(d) = s
349#define	ISP_IOXGET_8(isp, s, d)		d = *(s)
350#define	ISP_IOXGET_16(isp, s, d)	d = *(s)
351#define	ISP_IOXGET_32(isp, s, d)	d = *(s)
352#define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)
353#endif
354
355/*
356 * Includes of common header files
357 */
358
359#include <dev/isp/ispreg.h>
360#include <dev/isp/ispvar.h>
361#include <dev/isp/ispmbox.h>
362
363void isp_prt(struct ispsoftc *, int level, const char *, ...)
364	__printflike(3, 4);
365/*
366 * isp_osinfo definiitions && shorthand
367 */
368#define	SIMQFRZ_RESOURCE	0x1
369#define	SIMQFRZ_LOOPDOWN	0x2
370#define	SIMQFRZ_TIMED		0x4
371
372#define	isp_sim		isp_osinfo.sim
373#define	isp_path	isp_osinfo.path
374#define	isp_sim2	isp_osinfo.sim2
375#define	isp_path2	isp_osinfo.path2
376#define	isp_dev		isp_osinfo.dev
377
378/*
379 * prototypes for isp_pci && isp_freebsd to share
380 */
381extern void isp_attach(struct ispsoftc *);
382extern void isp_uninit(struct ispsoftc *);
383
384/*
385 * driver global data
386 */
387extern int isp_announced;
388
389/*
390 * Platform private flags
391 */
392#define	ISP_SPRIV_ERRSET	0x1
393#define	ISP_SPRIV_INWDOG	0x2
394#define	ISP_SPRIV_GRACE		0x4
395#define	ISP_SPRIV_DONE		0x8
396
397#define	XS_CMD_S_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_INWDOG
398#define	XS_CMD_C_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_INWDOG
399#define	XS_CMD_WDOG_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_INWDOG)
400
401#define	XS_CMD_S_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_GRACE
402#define	XS_CMD_C_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_GRACE
403#define	XS_CMD_GRACE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_GRACE)
404
405#define	XS_CMD_S_DONE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_DONE
406#define	XS_CMD_C_DONE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_DONE
407#define	XS_CMD_DONE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_DONE)
408
409#define	XS_CMD_S_CLEAR(sccb)	(sccb)->ccb_h.spriv_field0 = 0
410/*
411 * Platform specific inline functions
412 */
413
414static INLINE void isp_mbox_wait_complete(struct ispsoftc *);
415static INLINE void
416isp_mbox_wait_complete(struct ispsoftc *isp)
417{
418	if (isp->isp_osinfo.intsok) {
419		int lim = ((isp->isp_mbxwrk0)? 120 : 20) * hz;
420		isp->isp_osinfo.mboxwaiting = 1;
421#ifdef	ISP_SMPLOCK
422		(void) msleep(&isp->isp_mbxworkp,
423		    &isp->isp_lock, PRIBIO, "isp_mboxwaiting", lim);
424#else
425		(void) tsleep(&isp->isp_mbxworkp,
426		    PRIBIO, "isp_mboxwaiting", lim);
427#endif
428		if (isp->isp_mboxbsy != 0) {
429			isp_prt(isp, ISP_LOGWARN,
430			    "Interrupting Mailbox Command (0x%x) Timeout",
431			    isp->isp_lastmbxcmd);
432			isp->isp_mboxbsy = 0;
433		}
434		isp->isp_osinfo.mboxwaiting = 0;
435	} else {
436		int lim = ((isp->isp_mbxwrk0)? 240 : 60) * 10000;
437		int j;
438		for (j = 0; j < lim; j++) {
439			u_int16_t isr, sema, mbox;
440			if (isp->isp_mboxbsy == 0) {
441				break;
442			}
443			if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
444				isp_intr(isp, isr, sema, mbox);
445				if (isp->isp_mboxbsy == 0) {
446					break;
447				}
448			}
449			USEC_DELAY(500);
450		}
451		if (isp->isp_mboxbsy != 0) {
452			isp_prt(isp, ISP_LOGWARN,
453			    "Polled Mailbox Command (0x%x) Timeout",
454			    isp->isp_lastmbxcmd);
455		}
456	}
457}
458
459static INLINE u_int64_t nanotime_sub(struct timespec *, struct timespec *);
460static INLINE u_int64_t
461nanotime_sub(struct timespec *b, struct timespec *a)
462{
463	u_int64_t elapsed;
464	struct timespec x = *b;
465	timespecsub(&x, a);
466	elapsed = GET_NANOSEC(&x);
467	if (elapsed == 0)
468		elapsed++;
469	return (elapsed);
470}
471
472static INLINE char *strncat(char *, const char *, size_t);
473static INLINE char *
474strncat(char *d, const char *s, size_t c)
475{
476        char *t = d;
477
478        if (c) {
479                while (*d)
480                        d++;
481                while ((*d++ = *s++)) {
482                        if (--c == 0) {
483                                *d = '\0';
484                                break;
485                        }
486                }
487        }
488        return (t);
489}
490
491/*
492 * Common inline functions
493 */
494
495#include <dev/isp/isp_inline.h>
496#endif	/* _ISP_FREEBSD_H */
497