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