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