xref: /freebsd/sys/dev/isp/isp_freebsd.h (revision 5bba2114d0aeb3adc36f08ea8c5a0712f29c2e76)
1 /* $FreeBSD$ */
2 /*-
3  * Qlogic ISP SCSI Host Adapter FreeBSD Wrapper Definitions
4  *
5  * Copyright (c) 1997-2006 by Matthew Jacob
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice immediately at the beginning of the file, without modification,
13  *    this list of conditions, and the following disclaimer.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 #ifndef	_ISP_FREEBSD_H
30 #define	_ISP_FREEBSD_H
31 
32 #define	ISP_PLATFORM_VERSION_MAJOR	5
33 #define	ISP_PLATFORM_VERSION_MINOR	9
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/endian.h>
38 #include <sys/kernel.h>
39 #include <sys/queue.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/proc.h>
45 #include <sys/bus.h>
46 
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 /* disabled until done correctly */
65 /* #define	ISP_DAC_SUPPORTED	1 */
66 
67 /*
68  * Efficiency- get rid of SBus code && tests unless we need them.
69  */
70 #ifdef __sparc64__
71 #define	ISP_SBUS_SUPPORTED	1
72 #else
73 #define	ISP_SBUS_SUPPORTED	0
74 #endif
75 
76 #define	HANDLE_LOOPSTATE_IN_OUTER_LAYERS	1
77 /* #define	ISP_SMPLOCK			1 */
78 
79 #ifdef	ISP_SMPLOCK
80 #define	ISP_IFLAGS	INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
81 #else
82 #define	ISP_IFLAGS	INTR_TYPE_CAM | INTR_ENTROPY
83 #endif
84 
85 typedef void ispfwfunc(int, int, int, u_int16_t **);
86 
87 #ifdef	ISP_TARGET_MODE
88 #define	ISP_TARGET_FUNCTIONS	1
89 #define	ATPDPSIZE	256
90 typedef struct {
91 	u_int32_t	orig_datalen;
92 	u_int32_t	bytes_xfered;
93 	u_int32_t	last_xframt;
94 	u_int32_t	tag	: 16,
95 			lun	: 13,	/* not enough */
96 			state	: 3;
97 } atio_private_data_t;
98 #define	ATPD_STATE_FREE			0
99 #define	ATPD_STATE_ATIO			1
100 #define	ATPD_STATE_CAM			2
101 #define	ATPD_STATE_CTIO			3
102 #define	ATPD_STATE_LAST_CTIO		4
103 #define	ATPD_STATE_PDON			5
104 
105 typedef struct tstate {
106 	struct tstate *next;
107 	struct cam_path *owner;
108 	struct ccb_hdr_slist atios;
109 	struct ccb_hdr_slist inots;
110 	lun_id_t lun;
111 	int bus;
112 	u_int32_t hold;
113 	int atio_count;
114 	int inot_count;
115 } tstate_t;
116 
117 #define	LUN_HASH_SIZE			32
118 #define	LUN_HASH_FUNC(isp, port, lun)					\
119 	((IS_DUALBUS(isp)) ?						\
120 		(((lun) & ((LUN_HASH_SIZE >> 1) - 1)) << (port)) :	\
121 		((lun) & (LUN_HASH_SIZE - 1)))
122 #endif
123 
124 struct isposinfo {
125 	struct ispsoftc *	next;
126 	u_int64_t		default_port_wwn;
127 	u_int64_t		default_node_wwn;
128 	u_int32_t		default_id;
129 	device_t		dev;
130 	struct cam_sim		*sim;
131 	struct cam_path		*path;
132 	struct cam_sim		*sim2;
133 	struct cam_path		*path2;
134 	struct intr_config_hook	ehook;
135 	u_int8_t		: 1,
136 		fcbsy		: 1,
137 		ktmature	: 1,
138 		mboxwaiting	: 1,
139 		intsok		: 1,
140 		simqfrozen	: 3;
141 	struct mtx		lock;
142 	struct cv		kthread_cv;
143 	struct proc		*kproc;
144 	bus_dma_tag_t		cdmat;
145 	bus_dmamap_t		cdmap;
146 #define	isp_cdmat		isp_osinfo.cdmat
147 #define	isp_cdmap		isp_osinfo.cdmap
148 #ifdef	ISP_TARGET_MODE
149 #define	TM_WILDCARD_ENABLED	0x02
150 #define	TM_TMODE_ENABLED	0x01
151 	u_int8_t		tmflags[2];	/* two busses */
152 #define	NLEACT	4
153 	union ccb *		leact[NLEACT];
154 	tstate_t		tsdflt[2];	/* two busses */
155 	tstate_t		*lun_hash[LUN_HASH_SIZE];
156 	atio_private_data_t	atpdp[ATPDPSIZE];
157 #endif
158 };
159 
160 #define	isp_lock	isp_osinfo.lock
161 
162 /*
163  * Locking macros...
164  */
165 
166 #ifdef	ISP_SMPLOCK
167 #define	ISP_LOCK(x)		mtx_lock(&(x)->isp_lock)
168 #define	ISP_UNLOCK(x)		mtx_unlock(&(x)->isp_lock)
169 #define	ISPLOCK_2_CAMLOCK(isp)	\
170 	mtx_unlock(&(isp)->isp_lock); mtx_lock(&Giant)
171 #define	CAMLOCK_2_ISPLOCK(isp)	\
172 	mtx_unlock(&Giant); mtx_lock(&(isp)->isp_lock)
173 #else
174 #define	ISP_LOCK(x)		do { } while (0)
175 #define	ISP_UNLOCK(x)		do { } while (0)
176 #define	ISPLOCK_2_CAMLOCK(isp)	do { } while (0)
177 #define	CAMLOCK_2_ISPLOCK(isp)	do { } while (0)
178 #endif
179 
180 /*
181  * Required Macros/Defines
182  */
183 
184 #define	ISP2100_SCRLEN		0x800
185 
186 #define	MEMZERO			bzero
187 #define	MEMCPY(dst, src, amt)	bcopy((src), (dst), (amt))
188 #define	SNPRINTF		snprintf
189 #define	USEC_DELAY		DELAY
190 #define	USEC_SLEEP(isp, x)		\
191 	if (isp->isp_osinfo.intsok)	\
192 		ISP_UNLOCK(isp);	\
193 	DELAY(x);			\
194 	if (isp->isp_osinfo.intsok)	\
195 		ISP_LOCK(isp)
196 
197 #define	NANOTIME_T		struct timespec
198 #define	GET_NANOTIME		nanotime
199 #define	GET_NANOSEC(x)		((x)->tv_sec * 1000000000 + (x)->tv_nsec)
200 #define	NANOTIME_SUB		nanotime_sub
201 
202 #define	MAXISPREQUEST(isp)	((IS_FC(isp) || IS_ULTRA2(isp))? 1024 : 256)
203 
204 #define	MEMORYBARRIER(isp, type, offset, size)			\
205 switch (type) {							\
206 case SYNC_SFORDEV:						\
207 case SYNC_REQUEST:						\
208 	bus_dmamap_sync(isp->isp_cdmat, isp->isp_cdmap, 	\
209 	   BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);		\
210 	break;							\
211 case SYNC_SFORCPU:						\
212 case SYNC_RESULT:						\
213 	bus_dmamap_sync(isp->isp_cdmat, isp->isp_cdmap,		\
214 	   BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);	\
215 	break;							\
216 default:							\
217 	break;							\
218 }
219 
220 #define	MBOX_ACQUIRE(isp)
221 #define	MBOX_WAIT_COMPLETE		isp_mbox_wait_complete
222 #define	MBOX_NOTIFY_COMPLETE(isp)	\
223 	if (isp->isp_osinfo.mboxwaiting) { \
224 		isp->isp_osinfo.mboxwaiting = 0; \
225 		wakeup(&isp->isp_mbxworkp); \
226 	} \
227 	isp->isp_mboxbsy = 0
228 #define	MBOX_RELEASE(isp)
229 
230 #define	FC_SCRATCH_ACQUIRE(isp)						\
231 	if (isp->isp_osinfo.fcbsy) {					\
232 		isp_prt(isp, ISP_LOGWARN,				\
233 		    "FC scratch area busy (line %d)!", __LINE__);	\
234 	} else								\
235 		isp->isp_osinfo.fcbsy = 1
236 #define	FC_SCRATCH_RELEASE(isp)		 isp->isp_osinfo.fcbsy = 0
237 
238 #ifndef	SCSI_GOOD
239 #define	SCSI_GOOD	SCSI_STATUS_OK
240 #endif
241 #ifndef	SCSI_CHECK
242 #define	SCSI_CHECK	SCSI_STATUS_CHECK_COND
243 #endif
244 #ifndef	SCSI_BUSY
245 #define	SCSI_BUSY	SCSI_STATUS_BUSY
246 #endif
247 #ifndef	SCSI_QFULL
248 #define	SCSI_QFULL	SCSI_STATUS_QUEUE_FULL
249 #endif
250 
251 #define	XS_T			struct ccb_scsiio
252 #define	XS_ISP(ccb)		((struct ispsoftc *) (ccb)->ccb_h.spriv_ptr1)
253 #define	XS_CHANNEL(ccb)		cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path))
254 #define	XS_TGT(ccb)		(ccb)->ccb_h.target_id
255 #define	XS_LUN(ccb)		(ccb)->ccb_h.target_lun
256 
257 #define	XS_CDBP(ccb)	\
258 	(((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \
259 	 (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes)
260 
261 #define	XS_CDBLEN(ccb)		(ccb)->cdb_len
262 #define	XS_XFRLEN(ccb)		(ccb)->dxfer_len
263 #define	XS_TIME(ccb)		(ccb)->ccb_h.timeout
264 #define	XS_RESID(ccb)		(ccb)->resid
265 #define	XS_STSP(ccb)		(&(ccb)->scsi_status)
266 #define	XS_SNSP(ccb)		(&(ccb)->sense_data)
267 
268 #define	XS_SNSLEN(ccb)		\
269 	imin((sizeof((ccb)->sense_data)), ccb->sense_len)
270 
271 #define	XS_SNSKEY(ccb)		((ccb)->sense_data.flags & 0xf)
272 #define	XS_TAG_P(ccb)	\
273 	(((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \
274 	 (ccb)->tag_action != CAM_TAG_ACTION_NONE)
275 
276 #define	XS_TAG_TYPE(ccb)	\
277 	((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \
278 	 ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG))
279 
280 
281 #define	XS_SETERR(ccb, v)	(ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \
282 				(ccb)->ccb_h.status |= v, \
283 				(ccb)->ccb_h.spriv_field0 |= ISP_SPRIV_ERRSET
284 
285 #	define	HBA_NOERROR		CAM_REQ_INPROG
286 #	define	HBA_BOTCH		CAM_UNREC_HBA_ERROR
287 #	define	HBA_CMDTIMEOUT		CAM_CMD_TIMEOUT
288 #	define	HBA_SELTIMEOUT		CAM_SEL_TIMEOUT
289 #	define	HBA_TGTBSY		CAM_SCSI_STATUS_ERROR
290 #	define	HBA_BUSRESET		CAM_SCSI_BUS_RESET
291 #	define	HBA_ABORTED		CAM_REQ_ABORTED
292 #	define	HBA_DATAOVR		CAM_DATA_RUN_ERR
293 #	define	HBA_ARQFAIL		CAM_AUTOSENSE_FAIL
294 
295 
296 #define	XS_ERR(ccb)		((ccb)->ccb_h.status & CAM_STATUS_MASK)
297 
298 #define	XS_NOERR(ccb)		\
299 	(((ccb)->ccb_h.spriv_field0 & ISP_SPRIV_ERRSET) == 0 || \
300 	 ((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
301 
302 #define	XS_INITERR(ccb)		\
303 	XS_SETERR(ccb, CAM_REQ_INPROG), (ccb)->ccb_h.spriv_field0 = 0
304 
305 #define	XS_SAVE_SENSE(xs, sp)				\
306 	(xs)->ccb_h.status |= CAM_AUTOSNS_VALID,	\
307 	bcopy(sp->req_sense_data, &(xs)->sense_data,	\
308 	    imin(XS_SNSLEN(xs), sp->req_sense_len))
309 
310 #define	XS_SET_STATE_STAT(a, b, c)
311 
312 #define	DEFAULT_IID(x)		(isp)->isp_osinfo.default_id
313 #define	DEFAULT_LOOPID(x)	(isp)->isp_osinfo.default_id
314 #define	DEFAULT_NODEWWN(isp)	(isp)->isp_osinfo.default_node_wwn
315 #define	DEFAULT_PORTWWN(isp)	(isp)->isp_osinfo.default_port_wwn
316 #define	ISP_NODEWWN(isp)	FCPARAM(isp)->isp_nodewwn
317 #define	ISP_PORTWWN(isp)	FCPARAM(isp)->isp_portwwn
318 
319 #if	BYTE_ORDER == BIG_ENDIAN
320 #ifdef	ISP_SBUS_SUPPORTED
321 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
322 #define	ISP_IOXPUT_16(isp, s, d)				\
323 	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap16(s)
324 #define	ISP_IOXPUT_32(isp, s, d)				\
325 	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap32(s)
326 #define	ISP_IOXGET_8(isp, s, d)		d = (*((u_int8_t *)s))
327 #define	ISP_IOXGET_16(isp, s, d)				\
328 	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
329 	*((u_int16_t *)s) : bswap16(*((u_int16_t *)s))
330 #define	ISP_IOXGET_32(isp, s, d)				\
331 	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
332 	*((u_int32_t *)s) : bswap32(*((u_int32_t *)s))
333 #else
334 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
335 #define	ISP_IOXPUT_16(isp, s, d)	*(d) = bswap16(s)
336 #define	ISP_IOXPUT_32(isp, s, d)	*(d) = bswap32(s)
337 #define	ISP_IOXGET_8(isp, s, d)		d = (*((u_int8_t *)s))
338 #define	ISP_IOXGET_16(isp, s, d)	d = bswap16(*((u_int16_t *)s))
339 #define	ISP_IOXGET_32(isp, s, d)	d = bswap32(*((u_int32_t *)s))
340 #endif
341 #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)	*rp = bswap16(*rp)
342 #else
343 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
344 #define	ISP_IOXPUT_16(isp, s, d)	*(d) = s
345 #define	ISP_IOXPUT_32(isp, s, d)	*(d) = s
346 #define	ISP_IOXGET_8(isp, s, d)		d = *(s)
347 #define	ISP_IOXGET_16(isp, s, d)	d = *(s)
348 #define	ISP_IOXGET_32(isp, s, d)	d = *(s)
349 #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)
350 #endif
351 
352 /*
353  * Includes of common header files
354  */
355 
356 #include <dev/isp/ispreg.h>
357 #include <dev/isp/ispvar.h>
358 #include <dev/isp/ispmbox.h>
359 
360 void isp_prt(struct ispsoftc *, int level, const char *, ...)
361 	__printflike(3, 4);
362 /*
363  * isp_osinfo definiitions && shorthand
364  */
365 #define	SIMQFRZ_RESOURCE	0x1
366 #define	SIMQFRZ_LOOPDOWN	0x2
367 #define	SIMQFRZ_TIMED		0x4
368 
369 #define	isp_sim		isp_osinfo.sim
370 #define	isp_path	isp_osinfo.path
371 #define	isp_sim2	isp_osinfo.sim2
372 #define	isp_path2	isp_osinfo.path2
373 #define	isp_dev		isp_osinfo.dev
374 
375 /*
376  * prototypes for isp_pci && isp_freebsd to share
377  */
378 extern void isp_attach(struct ispsoftc *);
379 extern void isp_uninit(struct ispsoftc *);
380 
381 /*
382  * driver global data
383  */
384 extern int isp_announced;
385 
386 /*
387  * Platform private flags
388  */
389 #define	ISP_SPRIV_ERRSET	0x1
390 #define	ISP_SPRIV_INWDOG	0x2
391 #define	ISP_SPRIV_GRACE		0x4
392 #define	ISP_SPRIV_DONE		0x8
393 
394 #define	XS_CMD_S_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_INWDOG
395 #define	XS_CMD_C_WDOG(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_INWDOG
396 #define	XS_CMD_WDOG_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_INWDOG)
397 
398 #define	XS_CMD_S_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_GRACE
399 #define	XS_CMD_C_GRACE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_GRACE
400 #define	XS_CMD_GRACE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_GRACE)
401 
402 #define	XS_CMD_S_DONE(sccb)	(sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_DONE
403 #define	XS_CMD_C_DONE(sccb)	(sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_DONE
404 #define	XS_CMD_DONE_P(sccb)	((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_DONE)
405 
406 #define	XS_CMD_S_CLEAR(sccb)	(sccb)->ccb_h.spriv_field0 = 0
407 
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  * ISP Library functions
491  */
492 
493 #include <dev/isp/isp_library.h>
494 
495 #endif	/* _ISP_FREEBSD_H */
496