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