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