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