xref: /netbsd-src/sys/dev/ic/isp_netbsd.h (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /* $NetBSD: isp_netbsd.h,v 1.57 2006/03/29 04:43:56 thorpej Exp $ */
2 /*
3  * This driver, which is contained in NetBSD in the files:
4  *
5  *	sys/dev/ic/isp.c
6  *	sys/dev/ic/isp_inline.h
7  *	sys/dev/ic/isp_netbsd.c
8  *	sys/dev/ic/isp_netbsd.h
9  *	sys/dev/ic/isp_target.c
10  *	sys/dev/ic/isp_target.h
11  *	sys/dev/ic/isp_tpublic.h
12  *	sys/dev/ic/ispmbox.h
13  *	sys/dev/ic/ispreg.h
14  *	sys/dev/ic/ispvar.h
15  *	sys/microcode/isp/asm_sbus.h
16  *	sys/microcode/isp/asm_1040.h
17  *	sys/microcode/isp/asm_1080.h
18  *	sys/microcode/isp/asm_12160.h
19  *	sys/microcode/isp/asm_2100.h
20  *	sys/microcode/isp/asm_2200.h
21  *	sys/pci/isp_pci.c
22  *	sys/sbus/isp_sbus.c
23  *
24  * Is being actively maintained by Matthew Jacob (mjacob@NetBSD.org).
25  * This driver also is shared source with FreeBSD, OpenBSD, Linux, Solaris,
26  * Linux versions. This tends to be an interesting maintenance problem.
27  *
28  * Please coordinate with Matthew Jacob on changes you wish to make here.
29  */
30 /*
31  * NetBSD Specific definitions for the Qlogic ISP Host Adapter
32  * Matthew Jacob <mjacob@nas.nasa.gov>
33  */
34 /*
35  * Copyright (C) 1997, 1998, 1999 National Aeronautics & Space Administration
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. The name of the author may not be used to endorse or promote products
47  *    derived from this software without specific prior written permission
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59  */
60 #ifndef	_ISP_NETBSD_H
61 #define	_ISP_NETBSD_H
62 
63 #include <sys/types.h>
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/errno.h>
68 #include <sys/ioctl.h>
69 #include <sys/device.h>
70 #include <sys/malloc.h>
71 #include <sys/buf.h>
72 #include <sys/proc.h>
73 #include <sys/user.h>
74 #include <sys/kthread.h>
75 
76 #include <machine/bus.h>
77 
78 #include <dev/scsipi/scsi_all.h>
79 #include <dev/scsipi/scsipi_all.h>
80 #include <dev/scsipi/scsiconf.h>
81 
82 #include <dev/scsipi/scsi_message.h>
83 #include <dev/scsipi/scsipi_debug.h>
84 
85 #include "opt_isp.h"
86 
87 /*
88  * Efficiency- get rid of SBus code && tests unless we need them.
89  */
90 #if	defined(__sparcv9__ ) || defined(__sparc__)
91 #define	ISP_SBUS_SUPPORTED	1
92 #else
93 #define	ISP_SBUS_SUPPORTED	0
94 #endif
95 
96 #define	ISP_PLATFORM_VERSION_MAJOR	2
97 #define	ISP_PLATFORM_VERSION_MINOR	1
98 
99 struct isposinfo {
100 	struct device		_dev;
101 	struct scsipi_channel	_chan;
102 	struct scsipi_channel	_chan_b;
103 	struct scsipi_adapter   _adapter;
104 	bus_dma_tag_t		dmatag;
105 	bus_dmamap_t		rqdmap;
106 	bus_dmamap_t		rsdmap;
107 	bus_dmamap_t		scdmap;	/* FC only */
108 	int			splsaved;
109 	int			mboxwaiting;
110 	u_int32_t		islocked;
111 	u_int32_t		onintstack;
112 	unsigned int		: 26,
113 		loop_checked	: 1,
114 		mbox_wanted	: 1,
115 		mbox_locked	: 1,
116 		no_mbox_ints	: 1,
117 		paused		: 1,
118 		blocked		: 1;
119 	union {
120 		u_int64_t	_wwn;
121 		u_int16_t	_discovered[2];
122 #define	wwn_seed	un._wwn
123 #define	discovered	un._discovered
124 	} un;
125 	u_int32_t		threadwork;
126 	struct proc *		thread;
127 };
128 #define	isp_dmatag		isp_osinfo.dmatag
129 #define	isp_rqdmap		isp_osinfo.rqdmap
130 #define	isp_rsdmap		isp_osinfo.rsdmap
131 #define	isp_scdmap		isp_osinfo.scdmap
132 
133 #define	ISP_MUSTPOLL(isp)	\
134  	(isp->isp_osinfo.onintstack || isp->isp_osinfo.no_mbox_ints)
135 
136 #define	HANDLE_LOOPSTATE_IN_OUTER_LAYERS	1
137 
138 /*
139  * Required Macros/Defines
140  */
141 
142 #define	INLINE			__inline
143 
144 #define	ISP2100_SCRLEN		0x800
145 
146 #define	MEMZERO(dst, amt)	memset((dst), 0, (amt))
147 #define	MEMCPY(dst, src, amt)	memcpy((dst), (src), (amt))
148 #define	SNPRINTF		snprintf
149 #define	USEC_DELAY		DELAY
150 #define	USEC_SLEEP(isp, x)		\
151 	if (!ISP_MUSTPOLL(isp))		\
152 		ISP_UNLOCK(isp);	\
153 	DELAY(x);			\
154 	if (!ISP_MUSTPOLL(isp))		\
155 		ISP_LOCK(isp)
156 
157 #define	NANOTIME_T		struct timeval
158 #define	GET_NANOTIME		microtime
159 #define	GET_NANOSEC(x)		(((x)->tv_sec * 1000000 + (x)->tv_usec) * 1000)
160 #define	NANOTIME_SUB		isp_microtime_sub
161 
162 #define	MAXISPREQUEST(isp)	256
163 
164 
165 #define	MEMORYBARRIER(isp, type, offset, size)			\
166 switch (type) {							\
167 case SYNC_REQUEST:						\
168 {								\
169 	off_t off = (off_t) offset * QENTRY_LEN;		\
170 	bus_dmamap_sync(isp->isp_dmatag, isp->isp_rqdmap,	\
171 	    off, size, BUS_DMASYNC_PREWRITE);			\
172 	break;							\
173 }								\
174 case SYNC_RESULT:						\
175 {								\
176 	off_t off = (off_t) offset * QENTRY_LEN;		\
177 	bus_dmamap_sync(isp->isp_dmatag, isp->isp_rsdmap,	\
178 	    off, size, BUS_DMASYNC_POSTREAD);			\
179 	break;							\
180 }								\
181 case SYNC_SFORDEV:						\
182 {								\
183 	off_t off = (off_t) offset;				\
184 	bus_dmamap_sync(isp->isp_dmatag, isp->isp_scdmap,	\
185 	    off, size, BUS_DMASYNC_PREWRITE);			\
186 	break;							\
187 }								\
188 case SYNC_SFORCPU:						\
189 {								\
190 	off_t off = (off_t) offset;				\
191 	bus_dmamap_sync(isp->isp_dmatag, isp->isp_scdmap,	\
192 	    off, size, BUS_DMASYNC_POSTREAD);			\
193 	break;							\
194 }								\
195 case SYNC_REG:							\
196 default:							\
197 	break;							\
198 }
199 
200 #define	MBOX_ACQUIRE(isp)						\
201 	if (isp->isp_osinfo.onintstack) {				\
202 		if (isp->isp_osinfo.mbox_locked) {			\
203 			mbp->param[0] = MBOX_HOST_INTERFACE_ERROR;	\
204 			isp_prt(isp, ISP_LOGERR, "cannot acquire MBOX lock"); \
205 			return;						\
206 		}							\
207 	} else {							\
208 		while (isp->isp_osinfo.mbox_locked) {			\
209 			isp->isp_osinfo.mbox_wanted = 1;		\
210 			(void) tsleep(&isp->isp_osinfo.mboxwaiting+1,	\
211 			    PRIBIO, "mbox_acquire", 0);			\
212 		}							\
213 	}								\
214 	isp->isp_osinfo.mbox_locked = 1
215 
216 #define	MBOX_WAIT_COMPLETE	isp_wait_complete
217 
218 #define	MBOX_NOTIFY_COMPLETE(isp)					\
219 	if (isp->isp_osinfo.mboxwaiting) {				\
220                 isp->isp_osinfo.mboxwaiting = 0;			\
221                 wakeup(&isp->isp_osinfo.mboxwaiting);			\
222         }								\
223 	isp->isp_mboxbsy = 0
224 
225 #define	MBOX_RELEASE(isp)						\
226 	if (isp->isp_osinfo.mbox_wanted) {				\
227 		isp->isp_osinfo.mbox_wanted = 0;			\
228 		wakeup(&isp->isp_osinfo.mboxwaiting+1);			\
229 	}								\
230 	isp->isp_osinfo.mbox_locked = 0
231 
232 #define	FC_SCRATCH_ACQUIRE(isp)
233 #define	FC_SCRATCH_RELEASE(isp)
234 
235 #ifndef	SCSI_GOOD
236 #define	SCSI_GOOD	0x0
237 #endif
238 #ifndef	SCSI_CHECK
239 #define	SCSI_CHECK	0x2
240 #endif
241 #ifndef	SCSI_BUSY
242 #define	SCSI_BUSY	0x8
243 #endif
244 #ifndef	SCSI_QFULL
245 #define	SCSI_QFULL	0x28
246 #endif
247 
248 #define	XS_T			struct scsipi_xfer
249 #define	XS_CHANNEL(xs)		((int) (xs)->xs_periph->periph_channel->chan_channel)
250 #define	XS_ISP(xs)		\
251 	((void *)(xs)->xs_periph->periph_channel->chan_adapter->adapt_dev)
252 #define	XS_LUN(xs)		((int) (xs)->xs_periph->periph_lun)
253 #define	XS_TGT(xs)		((int) (xs)->xs_periph->periph_target)
254 #define	XS_CDBP(xs)		((caddr_t) (xs)->cmd)
255 #define	XS_CDBLEN(xs)		(xs)->cmdlen
256 #define	XS_XFRLEN(xs)		(xs)->datalen
257 #define	XS_TIME(xs)		(xs)->timeout
258 #define	XS_RESID(xs)		(xs)->resid
259 #define	XS_STSP(xs)		(&(xs)->status)
260 #define	XS_SNSP(xs)		(&(xs)->sense.scsi_sense)
261 #define	XS_SNSLEN(xs)		(sizeof (xs)->sense)
262 #define	XS_SNSKEY(xs)		((xs)->sense.scsi_sense.flags)
263 #define	XS_TAG_P(ccb)		(((xs)->xs_control & XS_CTL_POLL) != 0)
264 #define	XS_TAG_TYPE(xs)	\
265 	(((xs)->xs_control & XS_CTL_URGENT) ? REQFLAG_HTAG : \
266 	((xs)->bp && xs->bp->b_flags & B_ASYNC) ? REQFLAG_STAG : REQFLAG_OTAG)
267 
268 #define	XS_SETERR(xs, v)	(xs)->error = v
269 
270 #	define	HBA_NOERROR		XS_NOERROR
271 #	define	HBA_BOTCH		XS_DRIVER_STUFFUP
272 #	define	HBA_CMDTIMEOUT		XS_TIMEOUT
273 #	define	HBA_SELTIMEOUT		XS_SELTIMEOUT
274 #	define	HBA_TGTBSY		XS_BUSY
275 #	define	HBA_BUSRESET		XS_RESET
276 #	define	HBA_ABORTED		XS_DRIVER_STUFFUP
277 #	define	HBA_DATAOVR		XS_DRIVER_STUFFUP
278 #	define	HBA_ARQFAIL		XS_DRIVER_STUFFUP
279 
280 #define	XS_ERR(xs)		(xs)->error
281 
282 #define	XS_NOERR(xs)		(xs)->error == XS_NOERROR
283 
284 #define	XS_INITERR(xs)		(xs)->error = 0, XS_CMD_S_CLEAR(xs)
285 
286 #define	XS_SAVE_SENSE(xs, sp)				\
287 	if (xs->error == XS_NOERROR) {			\
288 		xs->error = XS_SENSE;			\
289 	}						\
290 	memcpy(&(xs)->sense, sp->req_sense_data,	\
291 	    imin(XS_SNSLEN(xs), sp->req_sense_len))
292 
293 #define	XS_SET_STATE_STAT(a, b, c)
294 
295 #define	DEFAULT_IID(x)		7
296 #define	DEFAULT_LOOPID(x)	108
297 #define	DEFAULT_NODEWWN(isp)	(isp)->isp_osinfo.un._wwn
298 #define	DEFAULT_PORTWWN(isp)	(isp)->isp_osinfo.un._wwn
299 #define	ISP_NODEWWN(isp)	FCPARAM(isp)->isp_nodewwn
300 #define	ISP_PORTWWN(isp)	FCPARAM(isp)->isp_portwwn
301 
302 #if	_BYTE_ORDER == _BIG_ENDIAN
303 #ifdef	ISP_SBUS_SUPPORTED
304 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
305 #define	ISP_IOXPUT_16(isp, s, d)				\
306 	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap16(s)
307 #define	ISP_IOXPUT_32(isp, s, d)				\
308 	*(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap32(s)
309 
310 #define	ISP_IOXGET_8(isp, s, d)		d = (*((u_int8_t *)s))
311 #define	ISP_IOXGET_16(isp, s, d)				\
312 	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
313 	*((u_int16_t *)s) : bswap16(*((u_int16_t *)s))
314 #define	ISP_IOXGET_32(isp, s, d)				\
315 	d = (isp->isp_bustype == ISP_BT_SBUS)?			\
316 	*((u_int32_t *)s) : bswap32(*((u_int32_t *)s))
317 #else
318 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
319 #define	ISP_IOXPUT_16(isp, s, d)	*(d) = bswap16(s)
320 #define	ISP_IOXPUT_32(isp, s, d)	*(d) = bswap32(s)
321 #define	ISP_IOXGET_8(isp, s, d)		d = (*((u_int8_t *)s))
322 #define	ISP_IOXGET_16(isp, s, d)	d = bswap16(*((u_int16_t *)s))
323 #define	ISP_IOXGET_32(isp, s, d)	d = bswap32(*((u_int32_t *)s))
324 #endif
325 #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)	*rp = bswap16(*rp)
326 #else
327 #define	ISP_IOXPUT_8(isp, s, d)		*(d) = s
328 #define	ISP_IOXPUT_16(isp, s, d)	*(d) = s
329 #define	ISP_IOXPUT_32(isp, s, d)	*(d) = s
330 #define	ISP_IOXGET_8(isp, s, d)		d = *(s)
331 #define	ISP_IOXGET_16(isp, s, d)	d = *(s)
332 #define	ISP_IOXGET_32(isp, s, d)	d = *(s)
333 #define	ISP_SWIZZLE_NVRAM_WORD(isp, rp)
334 #endif
335 
336 /*
337  * Includes of common header files
338  */
339 
340 #include <dev/ic/ispreg.h>
341 #include <dev/ic/ispvar.h>
342 #include <dev/ic/ispmbox.h>
343 #include <dev/ic/isp_ioctl.h>
344 
345 /*
346  * isp_osinfo definitions, extensions and shorthand.
347  */
348 #define	isp_name	isp_osinfo._dev.dv_xname
349 #define	isp_chanA	isp_osinfo._chan
350 #define	isp_chanB	isp_osinfo._chan_b
351 
352 
353 /*
354  * Driver prototypes..
355  */
356 void isp_attach(struct ispsoftc *);
357 void isp_uninit(struct ispsoftc *);
358 
359 static INLINE void isp_lock(struct ispsoftc *);
360 static INLINE void isp_unlock(struct ispsoftc *);
361 static INLINE char *strncat(char *, const char *, size_t);
362 static INLINE u_int64_t
363 isp_microtime_sub(struct timeval *, struct timeval *);
364 static void isp_wait_complete(struct ispsoftc *);
365 
366 /*
367  * Driver wide data...
368  */
369 
370 /*
371  * Locking macros...
372  */
373 #define	ISP_LOCK		isp_lock
374 #define	ISP_UNLOCK		isp_unlock
375 #define	ISP_ILOCK(x)		isp_lock(x); isp->isp_osinfo.onintstack++
376 #define	ISP_IUNLOCK(x)		isp->isp_osinfo.onintstack--; isp_unlock(x)
377 
378 /*
379  * Platform private flags
380  */
381 
382 #define	XS_PSTS_INWDOG		0x10000000
383 #define	XS_PSTS_GRACE		0x20000000
384 #define	XS_PSTS_ALL		0x30000000
385 
386 #define	XS_CMD_S_WDOG(xs)	(xs)->xs_status |= XS_PSTS_INWDOG
387 #define	XS_CMD_C_WDOG(xs)	(xs)->xs_status &= ~XS_PSTS_INWDOG
388 #define	XS_CMD_WDOG_P(xs)	(((xs)->xs_status & XS_PSTS_INWDOG) != 0)
389 
390 #define	XS_CMD_S_GRACE(xs)	(xs)->xs_status |= XS_PSTS_GRACE
391 #define	XS_CMD_C_GRACE(xs)	(xs)->xs_status &= ~XS_PSTS_GRACE
392 #define	XS_CMD_GRACE_P(xs)	(((xs)->xs_status & XS_PSTS_GRACE) != 0)
393 
394 #define	XS_CMD_S_DONE(xs)	(xs)->xs_status |= XS_STS_DONE
395 #define	XS_CMD_C_DONE(xs)	(xs)->xs_status &= ~XS_STS_DONE
396 #define	XS_CMD_DONE_P(xs)	(((xs)->xs_status & XS_STS_DONE) != 0)
397 
398 #define	XS_CMD_S_CLEAR(xs)	(xs)->xs_status &= ~XS_PSTS_ALL
399 
400 /*
401  * Platform specific 'inline' or support functions
402  */
403 static INLINE void
404 isp_lock(struct ispsoftc *isp)
405 {
406 	int s = splbio();
407 	if (isp->isp_osinfo.islocked++ == 0) {
408 		isp->isp_osinfo.splsaved = s;
409 	} else {
410 		splx(s);
411 	}
412 }
413 
414 static INLINE void
415 isp_unlock(struct ispsoftc *isp)
416 {
417 	if (isp->isp_osinfo.islocked-- <= 1) {
418 		isp->isp_osinfo.islocked = 0;
419 		splx(isp->isp_osinfo.splsaved);
420 	}
421 }
422 
423 static INLINE char *
424 strncat(char *d, const char *s, size_t c)
425 {
426         char *t = d;
427 
428         if (c) {
429                 while (*d)
430                         d++;
431                 while ((*d++ = *s++)) {
432                         if (--c == 0) {
433                                 *d = '\0';
434                                 break;
435                         }
436                 }
437         }
438         return (t);
439 }
440 
441 static INLINE u_int64_t
442 isp_microtime_sub(struct timeval *b, struct timeval *a)
443 {
444 	struct timeval x;
445 	u_int64_t elapsed;
446 	timersub(b, a, &x);
447 	elapsed = GET_NANOSEC(&x);
448 	if (elapsed == 0)
449 		elapsed++;
450 	return (elapsed);
451 }
452 
453 static INLINE void
454 isp_wait_complete(struct ispsoftc *isp)
455 {
456 	int lim;
457 	if (isp->isp_mbxwrk0)
458 		lim = 60;
459 	else
460 		lim = 5;
461 	if (ISP_MUSTPOLL(isp)) {
462 		int useclim = 1000000 * lim, usecs = 0;
463 		/*
464 		 * For sanity's sake, we don't delay longer
465 		 * than 5 seconds for polled commands.
466 		 */
467 		while (usecs < useclim) {
468 			u_int16_t isr, sema, mbox;
469 			if (isp->isp_mboxbsy == 0) {
470 				break;
471 			}
472 			if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
473 				isp_intr(isp, isr, sema, mbox);
474 				if (isp->isp_mboxbsy == 0) {
475 					break;
476 				}
477 			}
478 			USEC_DELAY(500);
479 			usecs += 500;
480 		}
481 		if (isp->isp_mboxbsy != 0) {
482 			isp_prt(isp, ISP_LOGWARN,
483 			    "Polled Mailbox Command (0x%x) Timeout",
484 			    isp->isp_lastmbxcmd);
485 		}
486 	} else {
487 		int rv = 0;
488                 isp->isp_osinfo.mboxwaiting = 1;
489                 while (isp->isp_osinfo.mboxwaiting && rv == 0) {
490 			struct timeval dtime;
491 			int timo;
492 			struct timeval tv;
493 
494 			dtime.tv_sec = lim;
495 			dtime.tv_usec = 0;
496 			microtime(&tv);
497 			timeradd(&tv, &dtime, &tv);
498 			if ((timo = hzto(&tv)) == 0) {
499 				timo = 1;
500 			}
501 			rv = tsleep(&isp->isp_osinfo.mboxwaiting,
502 			    PRIBIO, "isp_mboxcmd", timo);
503 		}
504 		if (rv == EWOULDBLOCK) {
505 			isp->isp_mboxbsy = 0;
506 			isp->isp_osinfo.mboxwaiting = 0;
507 			isp_prt(isp, ISP_LOGWARN,
508 			    "Interrupting Mailbox Command (0x%x) Timeout",
509 			    isp->isp_lastmbxcmd);
510 		}
511 	}
512 }
513 
514 
515 /*
516  * Common inline functions
517  */
518 #include <dev/ic/isp_inline.h>
519 
520 #if	!defined(ISP_DISABLE_FW) && !defined(ISP_COMPILE_FW)
521 #define	ISP_COMPILE_FW	1
522 #endif
523 #endif	/* _ISP_NETBSD_H */
524