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