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