xref: /freebsd/sys/dev/isp/isp_freebsd.h (revision 262e143bd46171a6415a5b28af260a5efa2a3db8)
1 /* $FreeBSD$ */
2 /*-
3  * Qlogic ISP SCSI Host Adapter FreeBSD Wrapper Definitions
4  * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002 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/systm.h>
35 #include <sys/endian.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.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 
62 #ifdef	PAE
63 #define	ISP_DAC_SUPPORTED	1
64 #endif
65 
66 /*
67  * Efficiency- get rid of SBus code && tests unless we need them.
68  */
69 #ifdef __sparc64__
70 #define	ISP_SBUS_SUPPORTED	1
71 #else
72 #define	ISP_SBUS_SUPPORTED	0
73 #endif
74 
75 #define	HANDLE_LOOPSTATE_IN_OUTER_LAYERS	1
76 /* #define	ISP_SMPLOCK			1 */
77 
78 #ifdef	ISP_SMPLOCK
79 #define	ISP_IFLAGS	INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
80 #else
81 #define	ISP_IFLAGS	INTR_TYPE_CAM | INTR_ENTROPY
82 #endif
83 
84 typedef void ispfwfunc(int, int, int, u_int16_t **);
85 
86 #ifdef	ISP_TARGET_MODE
87 #define	ISP_TARGET_FUNCTIONS	1
88 #define	ATPDPSIZE	256
89 typedef struct {
90 	u_int32_t	orig_datalen;
91 	u_int32_t	bytes_xfered;
92 	u_int32_t	last_xframt;
93 	u_int32_t	tag	: 16,
94 			lun	: 13,	/* not enough */
95 			state	: 3;
96 } atio_private_data_t;
97 #define	ATPD_STATE_FREE			0
98 #define	ATPD_STATE_ATIO			1
99 #define	ATPD_STATE_CAM			2
100 #define	ATPD_STATE_CTIO			3
101 #define	ATPD_STATE_LAST_CTIO		4
102 #define	ATPD_STATE_PDON			5
103 
104 typedef struct tstate {
105 	struct tstate *next;
106 	struct cam_path *owner;
107 	struct ccb_hdr_slist atios;
108 	struct ccb_hdr_slist inots;
109 	lun_id_t lun;
110 	int bus;
111 	u_int32_t hold;
112 	int atio_count;
113 	int inot_count;
114 } tstate_t;
115 
116 #define	LUN_HASH_SIZE			32
117 #define	LUN_HASH_FUNC(isp, port, lun)					\
118 	((IS_DUALBUS(isp)) ?						\
119 		(((lun) & ((LUN_HASH_SIZE >> 1) - 1)) << (port)) :	\
120 		((lun) & (LUN_HASH_SIZE - 1)))
121 #endif
122 
123 struct isposinfo {
124 	struct ispsoftc *	next;
125 	u_int64_t		default_port_wwn;
126 	u_int64_t		default_node_wwn;
127 	u_int32_t		default_id;
128 	device_t		dev;
129 	struct cam_sim		*sim;
130 	struct cam_path		*path;
131 	struct cam_sim		*sim2;
132 	struct cam_path		*path2;
133 	struct intr_config_hook	ehook;
134 	u_int8_t		: 1,
135 		fcbsy		: 1,
136 		ktmature	: 1,
137 		mboxwaiting	: 1,
138 		intsok		: 1,
139 		simqfrozen	: 3;
140 	struct mtx		lock;
141 	struct cv		kthread_cv;
142 	struct proc		*kproc;
143 	bus_dma_tag_t		cdmat;
144 	bus_dmamap_t		cdmap;
145 #define	isp_cdmat		isp_osinfo.cdmat
146 #define	isp_cdmap		isp_osinfo.cdmap
147 #ifdef	ISP_TARGET_MODE
148 #define	TM_WILDCARD_ENABLED	0x02
149 #define	TM_TMODE_ENABLED	0x01
150 	u_int8_t		tmflags[2];	/* two busses */
151 #define	NLEACT	4
152 	union ccb *		leact[NLEACT];
153 	tstate_t		tsdflt[2];	/* two busses */
154 	tstate_t		*lun_hash[LUN_HASH_SIZE];
155 	atio_private_data_t	atpdp[ATPDPSIZE];
156 #endif
157 };
158 
159 #define	isp_lock	isp_osinfo.lock
160 
161 /*
162  * Locking macros...
163  */
164 
165 #ifdef	ISP_SMPLOCK
166 #define	ISP_LOCK(x)		mtx_lock(&(x)->isp_lock)
167 #define	ISP_UNLOCK(x)		mtx_unlock(&(x)->isp_lock)
168 #define	ISPLOCK_2_CAMLOCK(isp)	\
169 	mtx_unlock(&(isp)->isp_lock); mtx_lock(&Giant)
170 #define	CAMLOCK_2_ISPLOCK(isp)	\
171 	mtx_unlock(&Giant); mtx_lock(&(isp)->isp_lock)
172 #else
173 #define	ISP_LOCK(x)		do { } while (0)
174 #define	ISP_UNLOCK(x)		do { } while (0)
175 #define	ISPLOCK_2_CAMLOCK(isp)	do { } while (0)
176 #define	CAMLOCK_2_ISPLOCK(isp)	do { } while (0)
177 #endif
178 
179 /*
180  * Required Macros/Defines
181  */
182 
183 #define	INLINE			__inline
184 
185 #define	ISP2100_SCRLEN		0x800
186 
187 #define	MEMZERO			bzero
188 #define	MEMCPY(dst, src, amt)	bcopy((src), (dst), (amt))
189 #define	SNPRINTF		snprintf
190 #define	USEC_DELAY		DELAY
191 #define	USEC_SLEEP(isp, x)		\
192 	if (isp->isp_osinfo.intsok)	\
193 		ISP_UNLOCK(isp);	\
194 	DELAY(x);			\
195 	if (isp->isp_osinfo.intsok)	\
196 		ISP_LOCK(isp)
197 
198 #define	NANOTIME_T		struct timespec
199 #define	GET_NANOTIME		nanotime
200 #define	GET_NANOSEC(x)		((x)->tv_sec * 1000000000 + (x)->tv_nsec)
201 #define	NANOTIME_SUB		nanotime_sub
202 
203 #define	MAXISPREQUEST(isp)	((IS_FC(isp) || IS_ULTRA2(isp))? 1024 : 256)
204 
205 #define	MEMORYBARRIER(isp, type, offset, size)			\
206 switch (type) {							\
207 case SYNC_SFORDEV:						\
208 case SYNC_REQUEST:						\
209 	bus_dmamap_sync(isp->isp_cdmat, isp->isp_cdmap, 	\
210 	   BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);		\
211 	break;							\
212 case SYNC_SFORCPU:						\
213 case SYNC_RESULT:						\
214 	bus_dmamap_sync(isp->isp_cdmat, isp->isp_cdmap,		\
215 	   BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);	\
216 	break;							\
217 default:							\
218 	break;							\
219 }
220 
221 #define	MBOX_ACQUIRE(isp)
222 #define	MBOX_WAIT_COMPLETE		isp_mbox_wait_complete
223 #define	MBOX_NOTIFY_COMPLETE(isp)	\
224 	if (isp->isp_osinfo.mboxwaiting) { \
225 		isp->isp_osinfo.mboxwaiting = 0; \
226 		wakeup(&isp->isp_mbxworkp); \
227 	} \
228 	isp->isp_mboxbsy = 0
229 #define	MBOX_RELEASE(isp)
230 
231 #define	FC_SCRATCH_ACQUIRE(isp)						\
232 	if (isp->isp_osinfo.fcbsy) {					\
233 		isp_prt(isp, ISP_LOGWARN,				\
234 		    "FC scratch area busy (line %d)!", __LINE__);	\
235 	} else								\
236 		isp->isp_osinfo.fcbsy = 1
237 #define	FC_SCRATCH_RELEASE(isp)		 isp->isp_osinfo.fcbsy = 0
238 
239 #ifndef	SCSI_GOOD
240 #define	SCSI_GOOD	SCSI_STATUS_OK
241 #endif
242 #ifndef	SCSI_CHECK
243 #define	SCSI_CHECK	SCSI_STATUS_CHECK_COND
244 #endif
245 #ifndef	SCSI_BUSY
246 #define	SCSI_BUSY	SCSI_STATUS_BUSY
247 #endif
248 #ifndef	SCSI_QFULL
249 #define	SCSI_QFULL	SCSI_STATUS_QUEUE_FULL
250 #endif
251 
252 #define	XS_T			struct ccb_scsiio
253 #define	XS_ISP(ccb)		((struct ispsoftc *) (ccb)->ccb_h.spriv_ptr1)
254 #define	XS_CHANNEL(ccb)		cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path))
255 #define	XS_TGT(ccb)		(ccb)->ccb_h.target_id
256 #define	XS_LUN(ccb)		(ccb)->ccb_h.target_lun
257 
258 #define	XS_CDBP(ccb)	\
259 	(((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \
260 	 (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes)
261 
262 #define	XS_CDBLEN(ccb)		(ccb)->cdb_len
263 #define	XS_XFRLEN(ccb)		(ccb)->dxfer_len
264 #define	XS_TIME(ccb)		(ccb)->ccb_h.timeout
265 #define	XS_RESID(ccb)		(ccb)->resid
266 #define	XS_STSP(ccb)		(&(ccb)->scsi_status)
267 #define	XS_SNSP(ccb)		(&(ccb)->sense_data)
268 
269 #define	XS_SNSLEN(ccb)		\
270 	imin((sizeof((ccb)->sense_data)), ccb->sense_len)
271 
272 #define	XS_SNSKEY(ccb)		((ccb)->sense_data.flags & 0xf)
273 #define	XS_TAG_P(ccb)	\
274 	(((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \
275 	 (ccb)->tag_action != CAM_TAG_ACTION_NONE)
276 
277 #define	XS_TAG_TYPE(ccb)	\
278 	((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \
279 	 ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG))
280 
281 
282 #define	XS_SETERR(ccb, v)	(ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \
283 				(ccb)->ccb_h.status |= v, \
284 				(ccb)->ccb_h.spriv_field0 |= ISP_SPRIV_ERRSET
285 
286 #	define	HBA_NOERROR		CAM_REQ_INPROG
287 #	define	HBA_BOTCH		CAM_UNREC_HBA_ERROR
288 #	define	HBA_CMDTIMEOUT		CAM_CMD_TIMEOUT
289 #	define	HBA_SELTIMEOUT		CAM_SEL_TIMEOUT
290 #	define	HBA_TGTBSY		CAM_SCSI_STATUS_ERROR
291 #	define	HBA_BUSRESET		CAM_SCSI_BUS_RESET
292 #	define	HBA_ABORTED		CAM_REQ_ABORTED
293 #	define	HBA_DATAOVR		CAM_DATA_RUN_ERR
294 #	define	HBA_ARQFAIL		CAM_AUTOSENSE_FAIL
295 
296 
297 #define	XS_ERR(ccb)		((ccb)->ccb_h.status & CAM_STATUS_MASK)
298 
299 #define	XS_NOERR(ccb)		\
300 	(((ccb)->ccb_h.spriv_field0 & ISP_SPRIV_ERRSET) == 0 || \
301 	 ((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
302 
303 #define	XS_INITERR(ccb)		\
304 	XS_SETERR(ccb, CAM_REQ_INPROG), (ccb)->ccb_h.spriv_field0 = 0
305 
306 #define	XS_SAVE_SENSE(xs, sp)				\
307 	(xs)->ccb_h.status |= CAM_AUTOSNS_VALID,	\
308 	bcopy(sp->req_sense_data, &(xs)->sense_data,	\
309 	    imin(XS_SNSLEN(xs), sp->req_sense_len))
310 
311 #define	XS_SET_STATE_STAT(a, b, c)
312 
313 #define	DEFAULT_IID(x)		(isp)->isp_osinfo.default_id
314 #define	DEFAULT_LOOPID(x)	(isp)->isp_osinfo.default_id
315 #define	DEFAULT_NODEWWN(isp)	(isp)->isp_osinfo.default_node_wwn
316 #define	DEFAULT_PORTWWN(isp)	(isp)->isp_osinfo.default_port_wwn
317 #define	ISP_NODEWWN(isp)	FCPARAM(isp)->isp_nodewwn
318 #define	ISP_PORTWWN(isp)	FCPARAM(isp)->isp_portwwn
319 
320 #if	BYTE_ORDER == BIG_ENDIAN
321 #ifdef	ISP_SBUS_SUPPORTED
322 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
323 #define	ISP_IOXPUT_16(isp, s, d)				\
324 	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap16(s)
325 #define	ISP_IOXPUT_32(isp, s, d)				\
326 	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap32(s)
327 #define	ISP_IOXGET_8(isp, s, d)		d = (*((u_int8_t *)s))
328 #define	ISP_IOXGET_16(isp, s, d)				\
329 	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
330 	*((u_int16_t *)s) : bswap16(*((u_int16_t *)s))
331 #define	ISP_IOXGET_32(isp, s, d)				\
332 	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
333 	*((u_int32_t *)s) : bswap32(*((u_int32_t *)s))
334 #else
335 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
336 #define	ISP_IOXPUT_16(isp, s, d)	*(d) = bswap16(s)
337 #define	ISP_IOXPUT_32(isp, s, d)	*(d) = bswap32(s)
338 #define	ISP_IOXGET_8(isp, s, d)		d = (*((u_int8_t *)s))
339 #define	ISP_IOXGET_16(isp, s, d)	d = bswap16(*((u_int16_t *)s))
340 #define	ISP_IOXGET_32(isp, s, d)	d = bswap32(*((u_int32_t *)s))
341 #endif
342 #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)	*rp = bswap16(*rp)
343 #else
344 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
345 #define	ISP_IOXPUT_16(isp, s, d)	*(d) = s
346 #define	ISP_IOXPUT_32(isp, s, d)	*(d) = s
347 #define	ISP_IOXGET_8(isp, s, d)		d = *(s)
348 #define	ISP_IOXGET_16(isp, s, d)	d = *(s)
349 #define	ISP_IOXGET_32(isp, s, d)	d = *(s)
350 #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)
351 #endif
352 
353 /*
354  * Includes of common header files
355  */
356 
357 #include <dev/isp/ispreg.h>
358 #include <dev/isp/ispvar.h>
359 #include <dev/isp/ispmbox.h>
360 
361 void isp_prt(struct ispsoftc *, int level, const char *, ...)
362 	__printflike(3, 4);
363 /*
364  * isp_osinfo definiitions && shorthand
365  */
366 #define	SIMQFRZ_RESOURCE	0x1
367 #define	SIMQFRZ_LOOPDOWN	0x2
368 #define	SIMQFRZ_TIMED		0x4
369 
370 #define	isp_sim		isp_osinfo.sim
371 #define	isp_path	isp_osinfo.path
372 #define	isp_sim2	isp_osinfo.sim2
373 #define	isp_path2	isp_osinfo.path2
374 #define	isp_dev		isp_osinfo.dev
375 
376 /*
377  * prototypes for isp_pci && isp_freebsd to share
378  */
379 extern void isp_attach(struct ispsoftc *);
380 extern void isp_uninit(struct ispsoftc *);
381 
382 /*
383  * driver global data
384  */
385 extern int isp_announced;
386 
387 /*
388  * Platform private flags
389  */
390 #define	ISP_SPRIV_ERRSET	0x1
391 #define	ISP_SPRIV_INWDOG	0x2
392 #define	ISP_SPRIV_GRACE		0x4
393 #define	ISP_SPRIV_DONE		0x8
394 
395 #define	XS_CMD_S_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_INWDOG
396 #define	XS_CMD_C_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_INWDOG
397 #define	XS_CMD_WDOG_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_INWDOG)
398 
399 #define	XS_CMD_S_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_GRACE
400 #define	XS_CMD_C_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_GRACE
401 #define	XS_CMD_GRACE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_GRACE)
402 
403 #define	XS_CMD_S_DONE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_DONE
404 #define	XS_CMD_C_DONE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_DONE
405 #define	XS_CMD_DONE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_DONE)
406 
407 #define	XS_CMD_S_CLEAR(sccb)	(sccb)->ccb_h.spriv_field0 = 0
408 /*
409  * Platform specific inline functions
410  */
411 
412 static INLINE void isp_mbox_wait_complete(struct ispsoftc *);
413 static INLINE void
414 isp_mbox_wait_complete(struct ispsoftc *isp)
415 {
416 	if (isp->isp_osinfo.intsok) {
417 		int lim = ((isp->isp_mbxwrk0)? 120 : 20) * hz;
418 		isp->isp_osinfo.mboxwaiting = 1;
419 #ifdef	ISP_SMPLOCK
420 		(void) msleep(&isp->isp_mbxworkp,
421 		    &isp->isp_lock, PRIBIO, "isp_mboxwaiting", lim);
422 #else
423 		(void) tsleep(&isp->isp_mbxworkp,
424 		    PRIBIO, "isp_mboxwaiting", lim);
425 #endif
426 		if (isp->isp_mboxbsy != 0) {
427 			isp_prt(isp, ISP_LOGWARN,
428 			    "Interrupting Mailbox Command (0x%x) Timeout",
429 			    isp->isp_lastmbxcmd);
430 			isp->isp_mboxbsy = 0;
431 		}
432 		isp->isp_osinfo.mboxwaiting = 0;
433 	} else {
434 		int lim = ((isp->isp_mbxwrk0)? 240 : 60) * 10000;
435 		int j;
436 		for (j = 0; j < lim; j++) {
437 			u_int16_t isr, sema, mbox;
438 			if (isp->isp_mboxbsy == 0) {
439 				break;
440 			}
441 			if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
442 				isp_intr(isp, isr, sema, mbox);
443 				if (isp->isp_mboxbsy == 0) {
444 					break;
445 				}
446 			}
447 			USEC_DELAY(500);
448 		}
449 		if (isp->isp_mboxbsy != 0) {
450 			isp_prt(isp, ISP_LOGWARN,
451 			    "Polled Mailbox Command (0x%x) Timeout",
452 			    isp->isp_lastmbxcmd);
453 		}
454 	}
455 }
456 
457 static INLINE u_int64_t nanotime_sub(struct timespec *, struct timespec *);
458 static INLINE u_int64_t
459 nanotime_sub(struct timespec *b, struct timespec *a)
460 {
461 	u_int64_t elapsed;
462 	struct timespec x = *b;
463 	timespecsub(&x, a);
464 	elapsed = GET_NANOSEC(&x);
465 	if (elapsed == 0)
466 		elapsed++;
467 	return (elapsed);
468 }
469 
470 static INLINE char *strncat(char *, const char *, size_t);
471 static INLINE char *
472 strncat(char *d, const char *s, size_t c)
473 {
474         char *t = d;
475 
476         if (c) {
477                 while (*d)
478                         d++;
479                 while ((*d++ = *s++)) {
480                         if (--c == 0) {
481                                 *d = '\0';
482                                 break;
483                         }
484                 }
485         }
486         return (t);
487 }
488 
489 /*
490  * Common inline functions
491  */
492 
493 #include <dev/isp/isp_inline.h>
494 #endif	/* _ISP_FREEBSD_H */
495