xref: /freebsd/sys/dev/isp/isp_freebsd.h (revision 5069714534cba67f1985e6dfe23b145178372b5f)
1 /* $FreeBSD$ */
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. 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	4
32 
33 #if	((ISP_PLATFORM_VERSION_MAJOR * 10)  + ISP_PLATFORM_VERSION_MINOR) >= 54
34 #define	ISP_SMPLOCK	1
35 #endif
36 
37 
38 #include <sys/param.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/queue.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
45 #include <sys/proc.h>
46 
47 #include <machine/bus_memio.h>
48 #include <machine/bus_pio.h>
49 #include <machine/bus.h>
50 #include <machine/clock.h>
51 #include <machine/cpu.h>
52 
53 #include <cam/cam.h>
54 #include <cam/cam_debug.h>
55 #include <cam/cam_ccb.h>
56 #include <cam/cam_sim.h>
57 #include <cam/cam_xpt.h>
58 #include <cam/cam_xpt_sim.h>
59 #include <cam/cam_debug.h>
60 #include <cam/scsi/scsi_all.h>
61 #include <cam/scsi/scsi_message.h>
62 
63 #include "opt_ddb.h"
64 #include "opt_isp.h"
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 	u_int32_t hold;
76 } tstate_t;
77 
78 /*
79  * This should work very well for 100% of parallel SCSI cases, 100%
80  * of non-SCCLUN FC cases, and hopefully some larger fraction of the
81  * SCCLUN FC cases. Basically, we index by the low 5 bits of lun and
82  * then linear search. This has to be reasonably zippy, but not crucially
83  * so.
84  */
85 #define	LUN_HASH_SIZE		32
86 #define	LUN_HASH_FUNC(lun)	((lun) & 0x1f)
87 
88 #endif
89 
90 struct isposinfo {
91 	struct ispsoftc *	next;
92 	u_int64_t		default_wwn;
93 	char			name[8];
94 	int			unit;
95 	struct cam_sim		*sim;
96 	struct cam_path		*path;
97 	struct cam_sim		*sim2;
98 	struct cam_path		*path2;
99 	struct intr_config_hook	ehook;
100 	u_int8_t		mboxwaiting;
101 	u_int8_t		simqfrozen;
102 	u_int8_t		drain;
103 	u_int8_t		intsok;
104 #ifdef	ISP_SMPLOCK
105 	struct mtx		lock;
106 #else
107 	volatile u_int32_t	islocked;
108 	int			splsaved;
109 #endif
110 #ifdef	ISP_TARGET_MODE
111 #define	TM_WANTED		0x01
112 #define	TM_BUSY			0x02
113 #define	TM_TMODE_ENABLED	0x80
114 	u_int8_t		tmflags;
115 	u_int8_t		rstatus;
116 	u_int16_t		rollinfo;
117 	tstate_t		tsdflt;
118 	tstate_t		*lun_hash[LUN_HASH_SIZE];
119 #endif
120 };
121 
122 /*
123  * Locking macros...
124  */
125 
126 #ifdef	ISP_SMPLOCK
127 #define	ISP_LOCK(x)		mtx_enter(&(x)->isp_osinfo.lock, MTX_DEF)
128 #define	ISP_UNLOCK(x)		mtx_exit(&(x)->isp_osinfo.lock, MTX_DEF)
129 #else
130 #define	ISP_LOCK		isp_lock
131 #define	ISP_UNLOCK		isp_unlock
132 #endif
133 
134 
135 /*
136  * Required Macros/Defines
137  */
138 
139 #define	INLINE			__inline
140 
141 #define	ISP2100_FABRIC		1
142 #define	ISP2100_SCRLEN		0x400
143 
144 #define	MEMZERO			bzero
145 #define	MEMCPY(dst, src, amt)	bcopy((src), (dst), (amt))
146 #define	SNPRINTF		snprintf
147 #define	STRNCAT			strncat
148 #define	USEC_DELAY		DELAY
149 #define	USEC_SLEEP(isp, x)		\
150 	if (isp->isp_osinfo.intsok)	\
151 		ISP_UNLOCK(isp);	\
152 	DELAY(x);			\
153 	if (isp->isp_osinfo.intsok)	\
154 		ISP_LOCK(isp)
155 
156 #define	NANOTIME_T		struct timespec
157 #define	GET_NANOTIME		nanotime
158 #define	GET_NANOSEC(x)		((x)->tv_sec * 1000000000 + (x)->tv_nsec)
159 #define	NANOTIME_SUB		nanotime_sub
160 
161 #define	MAXISPREQUEST(isp)	256
162 
163 #ifdef	__alpha__
164 #define	MEMORYBARRIER(isp, type, offset, size)	alpha_mb()
165 #else
166 #define	MEMORYBARRIER(isp, type, offset, size)
167 #endif
168 
169 #define	MBOX_ACQUIRE(isp)
170 #define	MBOX_WAIT_COMPLETE		isp_mbox_wait_complete
171 #define	MBOX_NOTIFY_COMPLETE(isp)	\
172 	if (isp->isp_osinfo.mboxwaiting) { \
173 		isp->isp_osinfo.mboxwaiting = 0; \
174 		wakeup(&isp->isp_osinfo.mboxwaiting); \
175 	} \
176 	isp->isp_mboxbsy = 0
177 #define	MBOX_RELEASE(isp)
178 
179 #ifndef	SCSI_GOOD
180 #define	SCSI_GOOD	SCSI_STATUS_OK
181 #endif
182 #ifndef	SCSI_CHECK
183 #define	SCSI_CHECK	SCSI_STATUS_CHECK_COND
184 #endif
185 #ifndef	SCSI_BUSY
186 #define	SCSI_BUSY	SCSI_STATUS_BUSY
187 #endif
188 #ifndef	SCSI_QFULL
189 #define	SCSI_QFULL	SCSI_STATUS_QUEUE_FULL
190 #endif
191 
192 #define	XS_T			struct ccb_scsiio
193 #define	XS_ISP(ccb)		((struct ispsoftc *) (ccb)->ccb_h.spriv_ptr1)
194 #define	XS_CHANNEL(ccb)		cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path))
195 #define	XS_TGT(ccb)		(ccb)->ccb_h.target_id
196 #define	XS_LUN(ccb)		(ccb)->ccb_h.target_lun
197 
198 #define	XS_CDBP(ccb)	\
199 	(((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \
200 	 (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes)
201 
202 #define	XS_CDBLEN(ccb)		(ccb)->cdb_len
203 #define	XS_XFRLEN(ccb)		(ccb)->dxfer_len
204 #define	XS_TIME(ccb)		(ccb)->ccb_h.timeout
205 #define	XS_RESID(ccb)		(ccb)->resid
206 #define	XS_STSP(ccb)		(&(ccb)->scsi_status)
207 #define	XS_SNSP(ccb)		(&(ccb)->sense_data)
208 
209 #define	XS_SNSLEN(ccb)		\
210 	imin((sizeof((ccb)->sense_data)), ccb->sense_len)
211 
212 #define	XS_SNSKEY(ccb)		((ccb)->sense_data.flags & 0xf)
213 #define	XS_TAG_P(ccb)	\
214 	(((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \
215 	 (ccb)->tag_action != CAM_TAG_ACTION_NONE)
216 
217 #define	XS_TAG_TYPE(ccb)	\
218 	((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \
219 	 ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG))
220 
221 
222 #define	XS_SETERR(ccb, v)	(ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \
223 				(ccb)->ccb_h.status |= v, \
224 				(ccb)->ccb_h.spriv_field0 |= ISP_SPRIV_ERRSET
225 
226 #	define	HBA_NOERROR		CAM_REQ_INPROG
227 #	define	HBA_BOTCH		CAM_UNREC_HBA_ERROR
228 #	define	HBA_CMDTIMEOUT		CAM_CMD_TIMEOUT
229 #	define	HBA_SELTIMEOUT		CAM_SEL_TIMEOUT
230 #	define	HBA_TGTBSY		CAM_SCSI_STATUS_ERROR
231 #	define	HBA_BUSRESET		CAM_SCSI_BUS_RESET
232 #	define	HBA_ABORTED		CAM_REQ_ABORTED
233 #	define	HBA_DATAOVR		CAM_DATA_RUN_ERR
234 #	define	HBA_ARQFAIL		CAM_AUTOSENSE_FAIL
235 
236 
237 #define	XS_ERR(ccb)		((ccb)->ccb_h.status & CAM_STATUS_MASK)
238 
239 #define	XS_NOERR(ccb)		\
240 	(((ccb)->ccb_h.spriv_field0 & ISP_SPRIV_ERRSET) == 0 || \
241 	 ((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
242 
243 #define	XS_INITERR(ccb)		\
244 	XS_SETERR(ccb, CAM_REQ_INPROG), (ccb)->ccb_h.spriv_field0 = 0
245 
246 #define	XS_SAVE_SENSE(xs, sp)				\
247 	(xs)->ccb_h.status |= CAM_AUTOSNS_VALID,	\
248 	bcopy(sp->req_sense_data, &(xs)->sense_data,	\
249 	    imin(XS_SNSLEN(xs), sp->req_sense_len))
250 
251 #define	XS_SET_STATE_STAT(a, b, c)
252 
253 #define	DEFAULT_IID(x)		7
254 #define	DEFAULT_LOOPID(x)	109
255 #define	DEFAULT_NODEWWN(isp)	(isp)->isp_osinfo.default_wwn
256 #define	DEFAULT_PORTWWN(isp)	(isp)->isp_osinfo.default_wwn
257 #define	ISP_NODEWWN(isp)	FCPARAM(isp)->isp_nodewwn
258 #define	ISP_PORTWWN(isp)	FCPARAM(isp)->isp_portwwn
259 
260 #define	ISP_UNSWIZZLE_AND_COPY_PDBP(isp, dest, src)	\
261 	if((void *)src != (void *)dest) bcopy(src, dest, sizeof (isp_pdb_t))
262 #define	ISP_SWIZZLE_ICB(a, b)
263 #define	ISP_SWIZZLE_REQUEST(a, b)
264 #define	ISP_UNSWIZZLE_RESPONSE(a, b, c)
265 #define	ISP_SWIZZLE_SNS_REQ(a, b)
266 #define	ISP_UNSWIZZLE_SNS_RSP(a, b, c)
267 #define	ISP_SWIZZLE_NVRAM_WORD(isp, x)
268 
269 /*
270  * Includes of common header files
271  */
272 
273 #include <dev/isp/ispreg.h>
274 #include <dev/isp/ispvar.h>
275 #include <dev/isp/ispmbox.h>
276 
277 /*
278  * isp_osinfo definiitions && shorthand
279  */
280 #define	SIMQFRZ_RESOURCE	0x1
281 #define	SIMQFRZ_LOOPDOWN	0x2
282 #define	SIMQFRZ_TIMED		0x4
283 
284 #define	isp_sim		isp_osinfo.sim
285 #define	isp_path	isp_osinfo.path
286 #define	isp_sim2	isp_osinfo.sim2
287 #define	isp_path2	isp_osinfo.path2
288 #define	isp_unit	isp_osinfo.unit
289 #define	isp_name	isp_osinfo.name
290 
291 /*
292  * prototypes for isp_pci && isp_freebsd to share
293  */
294 extern void isp_attach(struct ispsoftc *);
295 extern void isp_uninit(struct ispsoftc *);
296 
297 /*
298  * Platform private flags
299  */
300 #define	ISP_SPRIV_ERRSET	0x1
301 #define	ISP_SPRIV_INWDOG	0x2
302 #define	ISP_SPRIV_GRACE		0x4
303 #define	ISP_SPRIV_DONE		0x8
304 
305 #define	XS_CMD_S_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_INWDOG
306 #define	XS_CMD_C_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_INWDOG
307 #define	XS_CMD_WDOG_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_INWDOG)
308 
309 #define	XS_CMD_S_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_GRACE
310 #define	XS_CMD_C_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_GRACE
311 #define	XS_CMD_GRACE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_GRACE)
312 
313 #define	XS_CMD_S_DONE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_DONE
314 #define	XS_CMD_C_DONE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_DONE
315 #define	XS_CMD_DONE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_DONE)
316 
317 #define	XS_CMD_S_CLEAR(sccb)	(sccb)->ccb_h.spriv_field0 = 0
318 /*
319  * Platform specific inline functions
320  */
321 #ifndef	ISP_SMPLOCK
322 static INLINE void isp_lock(struct ispsoftc *);
323 static INLINE void
324 isp_lock(struct ispsoftc *isp)
325 {
326 	int s = splcam();
327 	if (isp->isp_osinfo.islocked++ == 0) {
328 		isp->isp_osinfo.splsaved = s;
329 	} else {
330 		splx(s);
331 	}
332 }
333 
334 static INLINE void isp_unlock(struct ispsoftc *);
335 static INLINE void
336 isp_unlock(struct ispsoftc *isp)
337 {
338 	if (isp->isp_osinfo.islocked) {
339 		if (--isp->isp_osinfo.islocked == 0) {
340 			splx(isp->isp_osinfo.splsaved);
341 		}
342 	}
343 }
344 #endif
345 
346 static INLINE void isp_mbox_wait_complete(struct ispsoftc *);
347 static INLINE void
348 isp_mbox_wait_complete(struct ispsoftc *isp)
349 {
350 	if (isp->isp_osinfo.intsok) {
351 		isp->isp_osinfo.mboxwaiting = 1;
352 #ifdef	ISP_SMPLOCK
353 		(void) msleep(&isp->isp_osinfo.mboxwaiting,
354 		    &isp->isp_osinfo.lock, PRIBIO, "isp_mboxwaiting", 10 * hz);
355 #else
356 		(void) tsleep(&isp->isp_osinfo.mboxwaiting, PRIBIO,
357 		    "isp_mboxwaiting", 10 * hz);
358 #endif
359 		if (isp->isp_mboxbsy != 0) {
360 			isp_prt(isp, ISP_LOGWARN,
361 			    "Interrupting Mailbox Command (0x%x) Timeout",
362 			    isp->isp_lastmbxcmd);
363 			isp->isp_mboxbsy = 0;
364 		}
365 		isp->isp_osinfo.mboxwaiting = 0;
366 	} else {
367 		int j;
368 		for (j = 0; j < 60 * 10000; j++) {
369 			if (isp_intr(isp) == 0) {
370 				USEC_DELAY(500);
371 			}
372 			if (isp->isp_mboxbsy == 0) {
373 				break;
374 			}
375 		}
376 		if (isp->isp_mboxbsy != 0) {
377 			isp_prt(isp, ISP_LOGWARN,
378 			    "Polled Mailbox Command (0x%x) Timeout",
379 			    isp->isp_lastmbxcmd);
380 		}
381 	}
382 }
383 
384 static INLINE u_int64_t nanotime_sub(struct timespec *, struct timespec *);
385 static INLINE u_int64_t
386 nanotime_sub(struct timespec *b, struct timespec *a)
387 {
388 	u_int64_t elapsed;
389 	struct timespec x = *b;
390 	timespecsub(&x, a);
391 	elapsed = GET_NANOSEC(&x);
392 	if (elapsed == 0)
393 		elapsed++;
394 	return (elapsed);
395 }
396 
397 static INLINE char *strncat(char *, const char *, size_t);
398 static INLINE char *
399 strncat(char *d, const char *s, size_t c)
400 {
401         char *t = d;
402 
403         if (c) {
404                 while (*d)
405                         d++;
406                 while ((*d++ = *s++)) {
407                         if (--c == 0) {
408                                 *d = '\0';
409                                 break;
410                         }
411                 }
412         }
413         return (t);
414 }
415 
416 /*
417  * Common inline functions
418  */
419 
420 #include <dev/isp/isp_inline.h>
421 #endif	/* _ISP_FREEBSD_H */
422