xref: /freebsd/sys/dev/isp/isp_freebsd.h (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
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  * Required Macros/Defines
124  */
125 
126 #define	INLINE			__inline
127 
128 #define	ISP2100_FABRIC		1
129 #define	ISP2100_SCRLEN		0x400
130 
131 #define	MEMZERO			bzero
132 #define	MEMCPY(dst, src, amt)	bcopy((src), (dst), (amt))
133 #define	SNPRINTF		snprintf
134 #define	STRNCAT			strncat
135 #define	USEC_DELAY		DELAY
136 
137 #define	NANOTIME_T		struct timespec
138 #define	GET_NANOTIME		nanotime
139 #define	GET_NANOSEC(x)		((x)->tv_sec * 1000000000 + (x)->tv_nsec)
140 #define	NANOTIME_SUB		nanotime_sub
141 
142 #define	MAXISPREQUEST(isp)	256
143 
144 #ifdef	__alpha__
145 #define	MEMORYBARRIER(isp, type, offset, size)	alpha_mb()
146 #else
147 #define	MEMORYBARRIER(isp, type, offset, size)
148 #endif
149 
150 #define	MBOX_ACQUIRE(isp)
151 #define	MBOX_WAIT_COMPLETE		isp_mbox_wait_complete
152 #define	MBOX_NOTIFY_COMPLETE(isp)	\
153 	if (isp->isp_osinfo.mboxwaiting) { \
154 		isp->isp_osinfo.mboxwaiting = 0; \
155 		wakeup(&isp->isp_osinfo.mboxwaiting); \
156 	} \
157 	isp->isp_mboxbsy = 0
158 #define	MBOX_RELEASE(isp)
159 
160 #ifndef	SCSI_GOOD
161 #define	SCSI_GOOD	SCSI_STATUS_OK
162 #endif
163 #ifndef	SCSI_CHECK
164 #define	SCSI_CHECK	SCSI_STATUS_CHECK_COND
165 #endif
166 #ifndef	SCSI_BUSY
167 #define	SCSI_BUSY	SCSI_STATUS_BUSY
168 #endif
169 #ifndef	SCSI_QFULL
170 #define	SCSI_QFULL	SCSI_STATUS_QUEUE_FULL
171 #endif
172 
173 #define	XS_T			struct ccb_scsiio
174 #define	XS_ISP(ccb)		((struct ispsoftc *) (ccb)->ccb_h.spriv_ptr1)
175 #define	XS_CHANNEL(ccb)		cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path))
176 #define	XS_TGT(ccb)		(ccb)->ccb_h.target_id
177 #define	XS_LUN(ccb)		(ccb)->ccb_h.target_lun
178 
179 #define	XS_CDBP(ccb)	\
180 	(((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \
181 	 (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes)
182 
183 #define	XS_CDBLEN(ccb)		(ccb)->cdb_len
184 #define	XS_XFRLEN(ccb)		(ccb)->dxfer_len
185 #define	XS_TIME(ccb)		(ccb)->ccb_h.timeout
186 #define	XS_RESID(ccb)		(ccb)->resid
187 #define	XS_STSP(ccb)		(&(ccb)->scsi_status)
188 #define	XS_SNSP(ccb)		(&(ccb)->sense_data)
189 
190 #define	XS_SNSLEN(ccb)		\
191 	imin((sizeof((ccb)->sense_data)), ccb->sense_len)
192 
193 #define	XS_SNSKEY(ccb)		((ccb)->sense_data.flags & 0xf)
194 #define	XS_TAG_P(ccb)	\
195 	(((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \
196 	 (ccb)->tag_action != CAM_TAG_ACTION_NONE)
197 
198 #define	XS_TAG_TYPE(ccb)	\
199 	((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \
200 	 ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG))
201 
202 
203 #define	XS_SETERR(ccb, v)	(ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \
204 				(ccb)->ccb_h.status |= v, \
205 				(ccb)->ccb_h.spriv_field0 |= ISP_SPRIV_ERRSET
206 
207 #	define	HBA_NOERROR		CAM_REQ_INPROG
208 #	define	HBA_BOTCH		CAM_UNREC_HBA_ERROR
209 #	define	HBA_CMDTIMEOUT		CAM_CMD_TIMEOUT
210 #	define	HBA_SELTIMEOUT		CAM_SEL_TIMEOUT
211 #	define	HBA_TGTBSY		CAM_SCSI_STATUS_ERROR
212 #	define	HBA_BUSRESET		CAM_SCSI_BUS_RESET
213 #	define	HBA_ABORTED		CAM_REQ_ABORTED
214 #	define	HBA_DATAOVR		CAM_DATA_RUN_ERR
215 #	define	HBA_ARQFAIL		CAM_AUTOSENSE_FAIL
216 
217 
218 #define	XS_ERR(ccb)		((ccb)->ccb_h.status & CAM_STATUS_MASK)
219 
220 #define	XS_NOERR(ccb)		\
221 	(((ccb)->ccb_h.spriv_field0 & ISP_SPRIV_ERRSET) == 0 || \
222 	 ((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
223 
224 #define	XS_INITERR(ccb)		\
225 	XS_SETERR(ccb, CAM_REQ_INPROG), (ccb)->ccb_h.spriv_field0 = 0
226 
227 #define	XS_SAVE_SENSE(xs, sp)				\
228 	bcopy(sp->req_sense_data, &(xs)->sense_data,	\
229 	    imin(XS_SNSLEN(xs), sp->req_sense_len))
230 
231 #define	XS_SET_STATE_STAT(a, b, c)
232 
233 #define	DEFAULT_IID(x)		7
234 #define	DEFAULT_LOOPID(x)	109
235 #define	DEFAULT_NODEWWN(isp)	(isp)->isp_osinfo.default_wwn
236 #define	DEFAULT_PORTWWN(isp)	(isp)->isp_osinfo.default_wwn
237 #define	ISP_NODEWWN(isp)	FCPARAM(isp)->isp_nodewwn
238 #define	ISP_PORTWWN(isp)	FCPARAM(isp)->isp_portwwn
239 
240 #define	ISP_UNSWIZZLE_AND_COPY_PDBP(isp, dest, src)	\
241 	if((void *)src != (void *)dest) bcopy(src, dest, sizeof (isp_pdb_t))
242 #define	ISP_SWIZZLE_ICB(a, b)
243 #define	ISP_SWIZZLE_REQUEST(a, b)
244 #define	ISP_UNSWIZZLE_RESPONSE(a, b, c)
245 #define	ISP_SWIZZLE_SNS_REQ(a, b)
246 #define	ISP_UNSWIZZLE_SNS_RSP(a, b, c)
247 #define	ISP_SWIZZLE_NVRAM_WORD(isp, x)
248 
249 /*
250  * Includes of common header files
251  */
252 
253 #include <dev/isp/ispreg.h>
254 #include <dev/isp/ispvar.h>
255 #include <dev/isp/ispmbox.h>
256 
257 /*
258  * isp_osinfo definiitions && shorthand
259  */
260 #define	SIMQFRZ_RESOURCE	0x1
261 #define	SIMQFRZ_LOOPDOWN	0x2
262 #define	SIMQFRZ_TIMED		0x4
263 
264 #define	isp_sim		isp_osinfo.sim
265 #define	isp_path	isp_osinfo.path
266 #define	isp_sim2	isp_osinfo.sim2
267 #define	isp_path2	isp_osinfo.path2
268 #define	isp_unit	isp_osinfo.unit
269 #define	isp_name	isp_osinfo.name
270 
271 /*
272  * prototypes for isp_pci && isp_freebsd to share
273  */
274 extern void isp_attach(struct ispsoftc *);
275 extern void isp_uninit(struct ispsoftc *);
276 
277 /*
278  * Locking macros...
279  */
280 
281 #ifdef	ISP_SMPLOCK
282 #define	ISP_LOCK(x)		mtx_enter(&(x)->isp_osinfo.lock, MTX_DEF)
283 #define	ISP_UNLOCK(x)		mtx_exit(&(x)->isp_osinfo.lock, MTX_DEF)
284 #else
285 #define	ISP_LOCK		isp_lock
286 #define	ISP_UNLOCK		isp_unlock
287 #endif
288 
289 /*
290  * Platform private flags
291  */
292 #define	ISP_SPRIV_ERRSET	0x1
293 #define	ISP_SPRIV_INWDOG	0x2
294 #define	ISP_SPRIV_GRACE		0x4
295 #define	ISP_SPRIV_DONE		0x8
296 
297 #define	XS_CMD_S_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_INWDOG
298 #define	XS_CMD_C_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_INWDOG
299 #define	XS_CMD_WDOG_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_INWDOG)
300 
301 #define	XS_CMD_S_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_GRACE
302 #define	XS_CMD_C_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_GRACE
303 #define	XS_CMD_GRACE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_GRACE)
304 
305 #define	XS_CMD_S_DONE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_DONE
306 #define	XS_CMD_C_DONE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_DONE
307 #define	XS_CMD_DONE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_DONE)
308 
309 #define	XS_CMD_S_CLEAR(sccb)	(sccb)->ccb_h.spriv_field0 = 0
310 /*
311  * Platform specific inline functions
312  */
313 #ifndef	ISP_SMPLOCK
314 static INLINE void isp_lock(struct ispsoftc *);
315 static INLINE void
316 isp_lock(struct ispsoftc *isp)
317 {
318 	int s = splcam();
319 	if (isp->isp_osinfo.islocked++ == 0) {
320 		isp->isp_osinfo.splsaved = s;
321 	} else {
322 		splx(s);
323 	}
324 }
325 
326 static INLINE void isp_unlock(struct ispsoftc *);
327 static INLINE void
328 isp_unlock(struct ispsoftc *isp)
329 {
330 	if (--isp->isp_osinfo.islocked == 0) {
331 		splx(isp->isp_osinfo.splsaved);
332 	}
333 }
334 #endif
335 
336 static INLINE void isp_mbox_wait_complete(struct ispsoftc *);
337 static INLINE void
338 isp_mbox_wait_complete(struct ispsoftc *isp)
339 {
340 	if (isp->isp_osinfo.intsok) {
341 		isp->isp_osinfo.mboxwaiting = 1;
342 #ifdef	ISP_SMPLOCK
343 		(void) msleep(&isp->isp_osinfo.mboxwaiting,
344 		    &isp->isp_osinfo.lock, PRIBIO, "isp_mboxwaiting", 5 * hz);
345 #else
346 		(void) tsleep(&isp->isp_osinfo.mboxwaiting, PRIBIO,
347 		    "isp_mboxwaiting", 5 * hz);
348 #endif
349 		if (isp->isp_mboxbsy != 0) {
350 			isp_prt(isp, ISP_LOGWARN, "interrupting mbox timeout");
351 			isp->isp_mboxbsy = 0;
352 		}
353 		isp->isp_osinfo.mboxwaiting = 0;
354 	} else {
355 		int j;
356 		for (j = 0; j < 60 * 2000; j++) {
357 			if (isp_intr(isp) == 0) {
358 				USEC_DELAY(500);
359 			}
360 			if (isp->isp_mboxbsy == 0) {
361 				break;
362 			}
363 		}
364 		if (isp->isp_mboxbsy != 0) {
365 			isp_prt(isp, ISP_LOGWARN, "polled mbox timeout");
366 		}
367 	}
368 }
369 
370 static INLINE u_int64_t nanotime_sub(struct timespec *, struct timespec *);
371 static INLINE u_int64_t
372 nanotime_sub(struct timespec *b, struct timespec *a)
373 {
374 	u_int64_t elapsed;
375 	struct timespec x = *b;
376 	timespecsub(&x, a);
377 	elapsed = GET_NANOSEC(&x);
378 	if (elapsed == 0)
379 		elapsed++;
380 	return (elapsed);
381 }
382 
383 static INLINE char *strncat(char *, const char *, size_t);
384 static INLINE char *
385 strncat(char *d, const char *s, size_t c)
386 {
387         char *t = d;
388 
389         if (c) {
390                 while (*d)
391                         d++;
392                 while ((*d++ = *s++)) {
393                         if (--c == 0) {
394                                 *d = '\0';
395                                 break;
396                         }
397                 }
398         }
399         return (t);
400 }
401 
402 /*
403  * Common inline functions
404  */
405 
406 #include <dev/isp/isp_inline.h>
407 #endif	/* _ISP_FREEBSD_H */
408