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