xref: /freebsd/sys/dev/isp/isp_freebsd.h (revision 6adf353a56a161443406b44a45d00c688ca7b857)
1 /* $FreeBSD$ */
2 /*
3  * Qlogic ISP SCSI Host Adapter FreeBSD Wrapper Definitions
4  * Copyright (c) 1997, 1998, 1999, 2000, 2001 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/param.h>
35 #include <sys/systm.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 #define	HANDLE_LOOPSTATE_IN_OUTER_LAYERS	1
65 
66 typedef void ispfwfunc __P((int, int, int, const u_int16_t **));
67 
68 #ifdef	ISP_TARGET_MODE
69 typedef struct tstate {
70 	struct tstate *next;
71 	struct cam_path *owner;
72 	struct ccb_hdr_slist atios;
73 	struct ccb_hdr_slist inots;
74 	lun_id_t lun;
75 	int bus;
76 	u_int32_t hold;
77 } tstate_t;
78 
79 #define	LUN_HASH_SIZE			32
80 #define	LUN_HASH_FUNC(isp, port, lun)					\
81 	((IS_DUALBUS(isp)) ?						\
82 		(((lun) & ((LUN_HASH_SIZE >> 1) - 1)) << (port)) :	\
83 		((lun) & (LUN_HASH_SIZE - 1)))
84 #endif
85 
86 struct isposinfo {
87 	struct ispsoftc *	next;
88 	u_int64_t		default_port_wwn;
89 	u_int64_t		default_node_wwn;
90 	device_t		dev;
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 	u_int8_t		intsok;
100 	struct mtx		lock;
101 	struct cv		kthread_cv;
102 	struct proc		*kproc;
103 #ifdef	ISP_TARGET_MODE
104 #define	TM_WANTED		0x80
105 #define	TM_BUSY			0x40
106 #define	TM_TMODE_ENABLED	0x03
107 	u_int8_t		tmflags;
108 	u_int8_t		rstatus;
109 	u_int16_t		rollinfo;
110 	tstate_t		tsdflt[2];	/* two busses */
111 	tstate_t		*lun_hash[LUN_HASH_SIZE];
112 #endif
113 };
114 
115 #define	isp_lock	isp_osinfo.lock
116 
117 /*
118  * Locking macros...
119  */
120 
121 #define	ISP_LOCK(x)		mtx_lock(&(x)->isp_lock)
122 #define	ISP_UNLOCK(x)		mtx_unlock(&(x)->isp_lock)
123 #define	ISPLOCK_2_CAMLOCK(isp)	\
124 	mtx_unlock(&(isp)->isp_lock); mtx_lock(&Giant)
125 #define	CAMLOCK_2_ISPLOCK(isp)	\
126 	mtx_unlock(&Giant); mtx_lock(&(isp)->isp_lock)
127 
128 /*
129  * Required Macros/Defines
130  */
131 
132 #define	INLINE			__inline
133 
134 #define	ISP2100_SCRLEN		0x400
135 
136 #define	MEMZERO			bzero
137 #define	MEMCPY(dst, src, amt)	bcopy((src), (dst), (amt))
138 #define	SNPRINTF		snprintf
139 #define	STRNCAT			strncat
140 #define	USEC_DELAY		DELAY
141 #define	USEC_SLEEP(isp, x)		\
142 	if (isp->isp_osinfo.intsok)	\
143 		ISP_UNLOCK(isp);	\
144 	DELAY(x);			\
145 	if (isp->isp_osinfo.intsok)	\
146 		ISP_LOCK(isp)
147 
148 #define	NANOTIME_T		struct timespec
149 #define	GET_NANOTIME		nanotime
150 #define	GET_NANOSEC(x)		((x)->tv_sec * 1000000000 + (x)->tv_nsec)
151 #define	NANOTIME_SUB		nanotime_sub
152 
153 #define	MAXISPREQUEST(isp)	256
154 
155 #ifdef	__alpha__
156 #define	MEMORYBARRIER(isp, type, offset, size)	alpha_mb()
157 #else
158 #define	MEMORYBARRIER(isp, type, offset, size)
159 #endif
160 
161 #define	MBOX_ACQUIRE(isp)
162 #define	MBOX_WAIT_COMPLETE		isp_mbox_wait_complete
163 #define	MBOX_NOTIFY_COMPLETE(isp)	\
164 	if (isp->isp_osinfo.mboxwaiting) { \
165 		isp->isp_osinfo.mboxwaiting = 0; \
166 		wakeup(&isp->isp_osinfo.mboxwaiting); \
167 	} \
168 	isp->isp_mboxbsy = 0
169 #define	MBOX_RELEASE(isp)
170 
171 #ifndef	SCSI_GOOD
172 #define	SCSI_GOOD	SCSI_STATUS_OK
173 #endif
174 #ifndef	SCSI_CHECK
175 #define	SCSI_CHECK	SCSI_STATUS_CHECK_COND
176 #endif
177 #ifndef	SCSI_BUSY
178 #define	SCSI_BUSY	SCSI_STATUS_BUSY
179 #endif
180 #ifndef	SCSI_QFULL
181 #define	SCSI_QFULL	SCSI_STATUS_QUEUE_FULL
182 #endif
183 
184 #define	XS_T			struct ccb_scsiio
185 #define	XS_ISP(ccb)		((struct ispsoftc *) (ccb)->ccb_h.spriv_ptr1)
186 #define	XS_CHANNEL(ccb)		cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path))
187 #define	XS_TGT(ccb)		(ccb)->ccb_h.target_id
188 #define	XS_LUN(ccb)		(ccb)->ccb_h.target_lun
189 
190 #define	XS_CDBP(ccb)	\
191 	(((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \
192 	 (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes)
193 
194 #define	XS_CDBLEN(ccb)		(ccb)->cdb_len
195 #define	XS_XFRLEN(ccb)		(ccb)->dxfer_len
196 #define	XS_TIME(ccb)		(ccb)->ccb_h.timeout
197 #define	XS_RESID(ccb)		(ccb)->resid
198 #define	XS_STSP(ccb)		(&(ccb)->scsi_status)
199 #define	XS_SNSP(ccb)		(&(ccb)->sense_data)
200 
201 #define	XS_SNSLEN(ccb)		\
202 	imin((sizeof((ccb)->sense_data)), ccb->sense_len)
203 
204 #define	XS_SNSKEY(ccb)		((ccb)->sense_data.flags & 0xf)
205 #define	XS_TAG_P(ccb)	\
206 	(((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \
207 	 (ccb)->tag_action != CAM_TAG_ACTION_NONE)
208 
209 #define	XS_TAG_TYPE(ccb)	\
210 	((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \
211 	 ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG))
212 
213 
214 #define	XS_SETERR(ccb, v)	(ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \
215 				(ccb)->ccb_h.status |= v, \
216 				(ccb)->ccb_h.spriv_field0 |= ISP_SPRIV_ERRSET
217 
218 #	define	HBA_NOERROR		CAM_REQ_INPROG
219 #	define	HBA_BOTCH		CAM_UNREC_HBA_ERROR
220 #	define	HBA_CMDTIMEOUT		CAM_CMD_TIMEOUT
221 #	define	HBA_SELTIMEOUT		CAM_SEL_TIMEOUT
222 #	define	HBA_TGTBSY		CAM_SCSI_STATUS_ERROR
223 #	define	HBA_BUSRESET		CAM_SCSI_BUS_RESET
224 #	define	HBA_ABORTED		CAM_REQ_ABORTED
225 #	define	HBA_DATAOVR		CAM_DATA_RUN_ERR
226 #	define	HBA_ARQFAIL		CAM_AUTOSENSE_FAIL
227 
228 
229 #define	XS_ERR(ccb)		((ccb)->ccb_h.status & CAM_STATUS_MASK)
230 
231 #define	XS_NOERR(ccb)		\
232 	(((ccb)->ccb_h.spriv_field0 & ISP_SPRIV_ERRSET) == 0 || \
233 	 ((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
234 
235 #define	XS_INITERR(ccb)		\
236 	XS_SETERR(ccb, CAM_REQ_INPROG), (ccb)->ccb_h.spriv_field0 = 0
237 
238 #define	XS_SAVE_SENSE(xs, sp)				\
239 	(xs)->ccb_h.status |= CAM_AUTOSNS_VALID,	\
240 	bcopy(sp->req_sense_data, &(xs)->sense_data,	\
241 	    imin(XS_SNSLEN(xs), sp->req_sense_len))
242 
243 #define	XS_SET_STATE_STAT(a, b, c)
244 
245 #define	DEFAULT_IID(x)		7
246 #define	DEFAULT_LOOPID(x)	109
247 #define	DEFAULT_NODEWWN(isp)	(isp)->isp_osinfo.default_node_wwn
248 #define	DEFAULT_PORTWWN(isp)	(isp)->isp_osinfo.default_port_wwn
249 #define	ISP_NODEWWN(isp)	FCPARAM(isp)->isp_nodewwn
250 #define	ISP_PORTWWN(isp)	FCPARAM(isp)->isp_portwwn
251 
252 #define	ISP_UNSWIZZLE_AND_COPY_PDBP(isp, dest, src)	\
253 	if((void *)src != (void *)dest) bcopy(src, dest, sizeof (isp_pdb_t))
254 #define	ISP_SWIZZLE_ICB(a, b)
255 #define	ISP_SWIZZLE_REQUEST(a, b)
256 #define	ISP_UNSWIZZLE_RESPONSE(a, b, c)
257 #define	ISP_SWIZZLE_SNS_REQ(a, b)
258 #define	ISP_UNSWIZZLE_SNS_RSP(a, b, c)
259 #define	ISP_SWIZZLE_NVRAM_WORD(isp, x)
260 
261 /*
262  * Includes of common header files
263  */
264 
265 #include <dev/isp/ispreg.h>
266 #include <dev/isp/ispvar.h>
267 #include <dev/isp/ispmbox.h>
268 
269 /*
270  * isp_osinfo definiitions && shorthand
271  */
272 #define	SIMQFRZ_RESOURCE	0x1
273 #define	SIMQFRZ_LOOPDOWN	0x2
274 #define	SIMQFRZ_TIMED		0x4
275 
276 #define	isp_sim		isp_osinfo.sim
277 #define	isp_path	isp_osinfo.path
278 #define	isp_sim2	isp_osinfo.sim2
279 #define	isp_path2	isp_osinfo.path2
280 #define	isp_dev		isp_osinfo.dev
281 
282 /*
283  * prototypes for isp_pci && isp_freebsd to share
284  */
285 extern void isp_attach(struct ispsoftc *);
286 extern void isp_uninit(struct ispsoftc *);
287 
288 /*
289  * Platform private flags
290  */
291 #define	ISP_SPRIV_ERRSET	0x1
292 #define	ISP_SPRIV_INWDOG	0x2
293 #define	ISP_SPRIV_GRACE		0x4
294 #define	ISP_SPRIV_DONE		0x8
295 
296 #define	XS_CMD_S_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_INWDOG
297 #define	XS_CMD_C_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_INWDOG
298 #define	XS_CMD_WDOG_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_INWDOG)
299 
300 #define	XS_CMD_S_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_GRACE
301 #define	XS_CMD_C_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_GRACE
302 #define	XS_CMD_GRACE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_GRACE)
303 
304 #define	XS_CMD_S_DONE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_DONE
305 #define	XS_CMD_C_DONE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_DONE
306 #define	XS_CMD_DONE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_DONE)
307 
308 #define	XS_CMD_S_CLEAR(sccb)	(sccb)->ccb_h.spriv_field0 = 0
309 /*
310  * Platform specific inline functions
311  */
312 
313 static INLINE void isp_mbox_wait_complete(struct ispsoftc *);
314 static INLINE void
315 isp_mbox_wait_complete(struct ispsoftc *isp)
316 {
317 	if (isp->isp_osinfo.intsok) {
318 		isp->isp_osinfo.mboxwaiting = 1;
319 		(void) msleep(&isp->isp_osinfo.mboxwaiting,
320 		    &isp->isp_lock, PRIBIO, "isp_mboxwaiting", 10 * hz);
321 		if (isp->isp_mboxbsy != 0) {
322 			isp_prt(isp, ISP_LOGWARN,
323 			    "Interrupting Mailbox Command (0x%x) Timeout",
324 			    isp->isp_lastmbxcmd);
325 			isp->isp_mboxbsy = 0;
326 		}
327 		isp->isp_osinfo.mboxwaiting = 0;
328 	} else {
329 		int j;
330 		for (j = 0; j < 60 * 10000; j++) {
331 			if (isp_intr(isp) == 0) {
332 				USEC_DELAY(500);
333 			}
334 			if (isp->isp_mboxbsy == 0) {
335 				break;
336 			}
337 		}
338 		if (isp->isp_mboxbsy != 0) {
339 			isp_prt(isp, ISP_LOGWARN,
340 			    "Polled Mailbox Command (0x%x) Timeout",
341 			    isp->isp_lastmbxcmd);
342 		}
343 	}
344 }
345 
346 static INLINE u_int64_t nanotime_sub(struct timespec *, struct timespec *);
347 static INLINE u_int64_t
348 nanotime_sub(struct timespec *b, struct timespec *a)
349 {
350 	u_int64_t elapsed;
351 	struct timespec x = *b;
352 	timespecsub(&x, a);
353 	elapsed = GET_NANOSEC(&x);
354 	if (elapsed == 0)
355 		elapsed++;
356 	return (elapsed);
357 }
358 
359 static INLINE char *strncat(char *, const char *, size_t);
360 static INLINE char *
361 strncat(char *d, const char *s, size_t c)
362 {
363         char *t = d;
364 
365         if (c) {
366                 while (*d)
367                         d++;
368                 while ((*d++ = *s++)) {
369                         if (--c == 0) {
370                                 *d = '\0';
371                                 break;
372                         }
373                 }
374         }
375         return (t);
376 }
377 
378 /*
379  * Common inline functions
380  */
381 
382 #include <dev/isp/isp_inline.h>
383 #endif	/* _ISP_FREEBSD_H */
384