isp_freebsd.h revision 74914
133975Sjdp/* $FreeBSD: head/sys/dev/isp/isp_freebsd.h 74914 2001-03-28 09:17:56Z jhb $ */
234495Sjdp/*
333975Sjdp * Qlogic ISP SCSI Host Adapter FreeBSD Wrapper Definitions (CAM version)
433975Sjdp * Copyright (c) 1997, 1998, 1999, 2000 by Matthew Jacob
533975Sjdp *
633975Sjdp * Redistribution and use in source and binary forms, with or without
733975Sjdp * modification, are permitted provided that the following conditions
833975Sjdp * are met:
933975Sjdp * 1. Redistributions of source code must retain the above copyright
1033975Sjdp *    notice immediately at the beginning of the file, without modification,
1133975Sjdp *    this list of conditions, and the following disclaimer.
1233975Sjdp * 2. The name of the author may not be used to endorse or promote products
1333975Sjdp *    derived from this software without specific prior written permission.
1433975Sjdp *
1533975Sjdp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1633975Sjdp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1733975Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1833975Sjdp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
1933975Sjdp * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2033975Sjdp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2133975Sjdp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2233975Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2333975Sjdp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2433975Sjdp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2534495Sjdp * SUCH DAMAGE.
2634495Sjdp */
2734495Sjdp#ifndef	_ISP_FREEBSD_H
2834495Sjdp#define	_ISP_FREEBSD_H
2934495Sjdp
3033975Sjdp#define	ISP_PLATFORM_VERSION_MAJOR	5
3133975Sjdp#define	ISP_PLATFORM_VERSION_MINOR	6
3234495Sjdp
3334495Sjdp/*
3434495Sjdp * We're not ready for primetime yet
3534495Sjdp */
3634495Sjdp#if	0
3734495Sjdp#if	((ISP_PLATFORM_VERSION_MAJOR * 10)  + ISP_PLATFORM_VERSION_MINOR) >= 54
3834495Sjdp#define	ISP_SMPLOCK	1
3934495Sjdp#endif
40#endif
41
42
43#include <sys/param.h>
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/kernel.h>
47#include <sys/queue.h>
48#include <sys/lock.h>
49#include <sys/malloc.h>
50#include <sys/mutex.h>
51#include <sys/proc.h>
52#include <sys/bus.h>
53
54#include <machine/bus_memio.h>
55#include <machine/bus_pio.h>
56#include <machine/bus.h>
57#include <machine/clock.h>
58#include <machine/cpu.h>
59
60#include <cam/cam.h>
61#include <cam/cam_debug.h>
62#include <cam/cam_ccb.h>
63#include <cam/cam_sim.h>
64#include <cam/cam_xpt.h>
65#include <cam/cam_xpt_sim.h>
66#include <cam/cam_debug.h>
67#include <cam/scsi/scsi_all.h>
68#include <cam/scsi/scsi_message.h>
69
70#include "opt_ddb.h"
71#include "opt_isp.h"
72
73typedef void ispfwfunc __P((int, int, int, const u_int16_t **));
74
75#ifdef	ISP_TARGET_MODE
76typedef struct tstate {
77	struct tstate *next;
78	struct cam_path *owner;
79	struct ccb_hdr_slist atios;
80	struct ccb_hdr_slist inots;
81	lun_id_t lun;
82	u_int32_t hold;
83} tstate_t;
84
85/*
86 * This should work very well for 100% of parallel SCSI cases, 100%
87 * of non-SCCLUN FC cases, and hopefully some larger fraction of the
88 * SCCLUN FC cases. Basically, we index by the low 5 bits of lun and
89 * then linear search. This has to be reasonably zippy, but not crucially
90 * so.
91 */
92#define	LUN_HASH_SIZE		32
93#define	LUN_HASH_FUNC(lun)	((lun) & 0x1f)
94
95#endif
96
97struct isposinfo {
98	struct ispsoftc *	next;
99	u_int64_t		default_port_wwn;
100	u_int64_t		default_node_wwn;
101	device_t		dev;
102	struct cam_sim		*sim;
103	struct cam_path		*path;
104	struct cam_sim		*sim2;
105	struct cam_path		*path2;
106	struct intr_config_hook	ehook;
107	u_int8_t		mboxwaiting;
108	u_int8_t		simqfrozen;
109	u_int8_t		drain;
110	u_int8_t		intsok;
111#ifdef	ISP_SMPLOCK
112	struct mtx		lock;
113#else
114	volatile u_int32_t	islocked;
115	int			splsaved;
116#endif
117#ifdef	ISP_TARGET_MODE
118#define	TM_WANTED		0x01
119#define	TM_BUSY			0x02
120#define	TM_TMODE_ENABLED	0x80
121	u_int8_t		tmflags;
122	u_int8_t		rstatus;
123	u_int16_t		rollinfo;
124	tstate_t		tsdflt;
125	tstate_t		*lun_hash[LUN_HASH_SIZE];
126#endif
127};
128
129/*
130 * Locking macros...
131 */
132
133#ifdef	ISP_SMPLOCK
134#define	ISP_LOCK(x)		mtx_lock(&(x)->isp_osinfo.lock)
135#define	ISP_UNLOCK(x)		mtx_unlock(&(x)->isp_osinfo.lock)
136#else
137#define	ISP_LOCK		isp_lock
138#define	ISP_UNLOCK		isp_unlock
139#endif
140
141
142/*
143 * Required Macros/Defines
144 */
145
146#define	INLINE			__inline
147
148#define	ISP2100_SCRLEN		0x400
149
150#define	MEMZERO			bzero
151#define	MEMCPY(dst, src, amt)	bcopy((src), (dst), (amt))
152#define	SNPRINTF		snprintf
153#define	STRNCAT			strncat
154#define	USEC_DELAY		DELAY
155#define	USEC_SLEEP(isp, x)		\
156	if (isp->isp_osinfo.intsok)	\
157		ISP_UNLOCK(isp);	\
158	DELAY(x);			\
159	if (isp->isp_osinfo.intsok)	\
160		ISP_LOCK(isp)
161
162#define	NANOTIME_T		struct timespec
163#define	GET_NANOTIME		nanotime
164#define	GET_NANOSEC(x)		((x)->tv_sec * 1000000000 + (x)->tv_nsec)
165#define	NANOTIME_SUB		nanotime_sub
166
167#define	MAXISPREQUEST(isp)	256
168
169#ifdef	__alpha__
170#define	MEMORYBARRIER(isp, type, offset, size)	alpha_mb()
171#else
172#define	MEMORYBARRIER(isp, type, offset, size)
173#endif
174
175#define	MBOX_ACQUIRE(isp)
176#define	MBOX_WAIT_COMPLETE		isp_mbox_wait_complete
177#define	MBOX_NOTIFY_COMPLETE(isp)	\
178	if (isp->isp_osinfo.mboxwaiting) { \
179		isp->isp_osinfo.mboxwaiting = 0; \
180		wakeup(&isp->isp_osinfo.mboxwaiting); \
181	} \
182	isp->isp_mboxbsy = 0
183#define	MBOX_RELEASE(isp)
184
185#ifndef	SCSI_GOOD
186#define	SCSI_GOOD	SCSI_STATUS_OK
187#endif
188#ifndef	SCSI_CHECK
189#define	SCSI_CHECK	SCSI_STATUS_CHECK_COND
190#endif
191#ifndef	SCSI_BUSY
192#define	SCSI_BUSY	SCSI_STATUS_BUSY
193#endif
194#ifndef	SCSI_QFULL
195#define	SCSI_QFULL	SCSI_STATUS_QUEUE_FULL
196#endif
197
198#define	XS_T			struct ccb_scsiio
199#define	XS_ISP(ccb)		((struct ispsoftc *) (ccb)->ccb_h.spriv_ptr1)
200#define	XS_CHANNEL(ccb)		cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path))
201#define	XS_TGT(ccb)		(ccb)->ccb_h.target_id
202#define	XS_LUN(ccb)		(ccb)->ccb_h.target_lun
203
204#define	XS_CDBP(ccb)	\
205	(((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \
206	 (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes)
207
208#define	XS_CDBLEN(ccb)		(ccb)->cdb_len
209#define	XS_XFRLEN(ccb)		(ccb)->dxfer_len
210#define	XS_TIME(ccb)		(ccb)->ccb_h.timeout
211#define	XS_RESID(ccb)		(ccb)->resid
212#define	XS_STSP(ccb)		(&(ccb)->scsi_status)
213#define	XS_SNSP(ccb)		(&(ccb)->sense_data)
214
215#define	XS_SNSLEN(ccb)		\
216	imin((sizeof((ccb)->sense_data)), ccb->sense_len)
217
218#define	XS_SNSKEY(ccb)		((ccb)->sense_data.flags & 0xf)
219#define	XS_TAG_P(ccb)	\
220	(((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \
221	 (ccb)->tag_action != CAM_TAG_ACTION_NONE)
222
223#define	XS_TAG_TYPE(ccb)	\
224	((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \
225	 ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG))
226
227
228#define	XS_SETERR(ccb, v)	(ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \
229				(ccb)->ccb_h.status |= v, \
230				(ccb)->ccb_h.spriv_field0 |= ISP_SPRIV_ERRSET
231
232#	define	HBA_NOERROR		CAM_REQ_INPROG
233#	define	HBA_BOTCH		CAM_UNREC_HBA_ERROR
234#	define	HBA_CMDTIMEOUT		CAM_CMD_TIMEOUT
235#	define	HBA_SELTIMEOUT		CAM_SEL_TIMEOUT
236#	define	HBA_TGTBSY		CAM_SCSI_STATUS_ERROR
237#	define	HBA_BUSRESET		CAM_SCSI_BUS_RESET
238#	define	HBA_ABORTED		CAM_REQ_ABORTED
239#	define	HBA_DATAOVR		CAM_DATA_RUN_ERR
240#	define	HBA_ARQFAIL		CAM_AUTOSENSE_FAIL
241
242
243#define	XS_ERR(ccb)		((ccb)->ccb_h.status & CAM_STATUS_MASK)
244
245#define	XS_NOERR(ccb)		\
246	(((ccb)->ccb_h.spriv_field0 & ISP_SPRIV_ERRSET) == 0 || \
247	 ((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
248
249#define	XS_INITERR(ccb)		\
250	XS_SETERR(ccb, CAM_REQ_INPROG), (ccb)->ccb_h.spriv_field0 = 0
251
252#define	XS_SAVE_SENSE(xs, sp)				\
253	(xs)->ccb_h.status |= CAM_AUTOSNS_VALID,	\
254	bcopy(sp->req_sense_data, &(xs)->sense_data,	\
255	    imin(XS_SNSLEN(xs), sp->req_sense_len))
256
257#define	XS_SET_STATE_STAT(a, b, c)
258
259#define	DEFAULT_IID(x)		7
260#define	DEFAULT_LOOPID(x)	109
261#define	DEFAULT_NODEWWN(isp)	(isp)->isp_osinfo.default_node_wwn
262#define	DEFAULT_PORTWWN(isp)	(isp)->isp_osinfo.default_port_wwn
263#define	ISP_NODEWWN(isp)	FCPARAM(isp)->isp_nodewwn
264#define	ISP_PORTWWN(isp)	FCPARAM(isp)->isp_portwwn
265
266#define	ISP_UNSWIZZLE_AND_COPY_PDBP(isp, dest, src)	\
267	if((void *)src != (void *)dest) bcopy(src, dest, sizeof (isp_pdb_t))
268#define	ISP_SWIZZLE_ICB(a, b)
269#define	ISP_SWIZZLE_REQUEST(a, b)
270#define	ISP_UNSWIZZLE_RESPONSE(a, b, c)
271#define	ISP_SWIZZLE_SNS_REQ(a, b)
272#define	ISP_UNSWIZZLE_SNS_RSP(a, b, c)
273#define	ISP_SWIZZLE_NVRAM_WORD(isp, x)
274
275/*
276 * Includes of common header files
277 */
278
279#include <dev/isp/ispreg.h>
280#include <dev/isp/ispvar.h>
281#include <dev/isp/ispmbox.h>
282
283/*
284 * isp_osinfo definiitions && shorthand
285 */
286#define	SIMQFRZ_RESOURCE	0x1
287#define	SIMQFRZ_LOOPDOWN	0x2
288#define	SIMQFRZ_TIMED		0x4
289
290#define	isp_sim		isp_osinfo.sim
291#define	isp_path	isp_osinfo.path
292#define	isp_sim2	isp_osinfo.sim2
293#define	isp_path2	isp_osinfo.path2
294#define	isp_dev		isp_osinfo.dev
295
296/*
297 * prototypes for isp_pci && isp_freebsd to share
298 */
299extern void isp_attach(struct ispsoftc *);
300extern void isp_uninit(struct ispsoftc *);
301
302/*
303 * Platform private flags
304 */
305#define	ISP_SPRIV_ERRSET	0x1
306#define	ISP_SPRIV_INWDOG	0x2
307#define	ISP_SPRIV_GRACE		0x4
308#define	ISP_SPRIV_DONE		0x8
309
310#define	XS_CMD_S_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_INWDOG
311#define	XS_CMD_C_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_INWDOG
312#define	XS_CMD_WDOG_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_INWDOG)
313
314#define	XS_CMD_S_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_GRACE
315#define	XS_CMD_C_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_GRACE
316#define	XS_CMD_GRACE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_GRACE)
317
318#define	XS_CMD_S_DONE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_DONE
319#define	XS_CMD_C_DONE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_DONE
320#define	XS_CMD_DONE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_DONE)
321
322#define	XS_CMD_S_CLEAR(sccb)	(sccb)->ccb_h.spriv_field0 = 0
323/*
324 * Platform specific inline functions
325 */
326#ifndef	ISP_SMPLOCK
327static INLINE void isp_lock(struct ispsoftc *);
328static INLINE void
329isp_lock(struct ispsoftc *isp)
330{
331	int s = splcam();
332	if (isp->isp_osinfo.islocked++ == 0) {
333		isp->isp_osinfo.splsaved = s;
334	} else {
335		splx(s);
336	}
337}
338
339static INLINE void isp_unlock(struct ispsoftc *);
340static INLINE void
341isp_unlock(struct ispsoftc *isp)
342{
343	if (isp->isp_osinfo.islocked) {
344		if (--isp->isp_osinfo.islocked == 0) {
345			splx(isp->isp_osinfo.splsaved);
346		}
347	}
348}
349#endif
350
351static INLINE void isp_mbox_wait_complete(struct ispsoftc *);
352static INLINE void
353isp_mbox_wait_complete(struct ispsoftc *isp)
354{
355	if (isp->isp_osinfo.intsok) {
356		isp->isp_osinfo.mboxwaiting = 1;
357#ifdef	ISP_SMPLOCK
358		(void) msleep(&isp->isp_osinfo.mboxwaiting,
359		    &isp->isp_osinfo.lock, PRIBIO, "isp_mboxwaiting", 10 * hz);
360#else
361		(void) tsleep(&isp->isp_osinfo.mboxwaiting, PRIBIO,
362		    "isp_mboxwaiting", 10 * hz);
363#endif
364		if (isp->isp_mboxbsy != 0) {
365			isp_prt(isp, ISP_LOGWARN,
366			    "Interrupting Mailbox Command (0x%x) Timeout",
367			    isp->isp_lastmbxcmd);
368			isp->isp_mboxbsy = 0;
369		}
370		isp->isp_osinfo.mboxwaiting = 0;
371	} else {
372		int j;
373		for (j = 0; j < 60 * 10000; j++) {
374			if (isp_intr(isp) == 0) {
375				USEC_DELAY(500);
376			}
377			if (isp->isp_mboxbsy == 0) {
378				break;
379			}
380		}
381		if (isp->isp_mboxbsy != 0) {
382			isp_prt(isp, ISP_LOGWARN,
383			    "Polled Mailbox Command (0x%x) Timeout",
384			    isp->isp_lastmbxcmd);
385		}
386	}
387}
388
389static INLINE u_int64_t nanotime_sub(struct timespec *, struct timespec *);
390static INLINE u_int64_t
391nanotime_sub(struct timespec *b, struct timespec *a)
392{
393	u_int64_t elapsed;
394	struct timespec x = *b;
395	timespecsub(&x, a);
396	elapsed = GET_NANOSEC(&x);
397	if (elapsed == 0)
398		elapsed++;
399	return (elapsed);
400}
401
402static INLINE char *strncat(char *, const char *, size_t);
403static INLINE char *
404strncat(char *d, const char *s, size_t c)
405{
406        char *t = d;
407
408        if (c) {
409                while (*d)
410                        d++;
411                while ((*d++ = *s++)) {
412                        if (--c == 0) {
413                                *d = '\0';
414                                break;
415                        }
416                }
417        }
418        return (t);
419}
420
421/*
422 * Common inline functions
423 */
424
425#include <dev/isp/isp_inline.h>
426#endif	/* _ISP_FREEBSD_H */
427