isp_netbsd.h revision 1.48 1 /* $NetBSD: isp_netbsd.h,v 1.48 2002/02/21 22:32:42 mjacob 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 (at) 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 (at) 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 0x400
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 STRNCAT strncat
150 #define USEC_DELAY DELAY
151 #define USEC_SLEEP(isp, x) \
152 if (!ISP_MUSTPOLL(isp)) \
153 ISP_UNLOCK(isp); \
154 DELAY(x); \
155 if (!ISP_MUSTPOLL(isp)) \
156 ISP_LOCK(isp)
157
158 #define NANOTIME_T struct timeval
159 #define GET_NANOTIME microtime
160 #define GET_NANOSEC(x) (((x)->tv_sec * 1000000 + (x)->tv_usec) * 1000)
161 #define NANOTIME_SUB isp_microtime_sub
162
163 #define MAXISPREQUEST(isp) 256
164
165
166 #define MEMORYBARRIER(isp, type, offset, size) \
167 switch (type) { \
168 case SYNC_REQUEST: \
169 { \
170 off_t off = (off_t) offset * QENTRY_LEN; \
171 bus_dmamap_sync(isp->isp_dmatag, isp->isp_rqdmap, \
172 off, size, BUS_DMASYNC_PREWRITE); \
173 break; \
174 } \
175 case SYNC_RESULT: \
176 { \
177 off_t off = (off_t) offset * QENTRY_LEN; \
178 off += ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)); \
179 bus_dmamap_sync(isp->isp_dmatag, isp->isp_rsdmap, \
180 off, size, BUS_DMASYNC_POSTREAD); \
181 break; \
182 } \
183 case SYNC_SFORDEV: \
184 { \
185 off_t off = \
186 ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)) + \
187 ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp)) + offset; \
188 bus_dmamap_sync(isp->isp_dmatag, isp->isp_scdmap, \
189 off, size, BUS_DMASYNC_PREWRITE); \
190 break; \
191 } \
192 case SYNC_SFORCPU: \
193 { \
194 off_t off = \
195 ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp)) + \
196 ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp)) + offset; \
197 bus_dmamap_sync(isp->isp_dmatag, isp->isp_scdmap, \
198 off, size, BUS_DMASYNC_POSTREAD); \
199 break; \
200 } \
201 case SYNC_REG: \
202 default: \
203 break; \
204 }
205
206 #define MBOX_ACQUIRE(isp) \
207 if (isp->isp_osinfo.onintstack) { \
208 if (isp->isp_osinfo.mbox_locked) { \
209 mbp->param[0] = MBOX_HOST_INTERFACE_ERROR; \
210 isp_prt(isp, ISP_LOGERR, "cannot acquire MBOX lock"); \
211 return; \
212 } \
213 } else { \
214 while (isp->isp_osinfo.mbox_locked) { \
215 isp->isp_osinfo.mbox_wanted = 1; \
216 (void) tsleep(&isp->isp_osinfo.mboxwaiting+1, \
217 PRIBIO, "mbox_acquire", 0); \
218 } \
219 } \
220 isp->isp_osinfo.mbox_locked = 1
221
222 #define MBOX_WAIT_COMPLETE isp_wait_complete
223
224 #define MBOX_NOTIFY_COMPLETE(isp) \
225 if (isp->isp_osinfo.mboxwaiting) { \
226 isp->isp_osinfo.mboxwaiting = 0; \
227 wakeup(&isp->isp_osinfo.mboxwaiting); \
228 } \
229 isp->isp_mboxbsy = 0
230
231 #define MBOX_RELEASE(isp) \
232 if (isp->isp_osinfo.mbox_wanted) { \
233 isp->isp_osinfo.mbox_wanted = 0; \
234 wakeup(&isp->isp_osinfo.mboxwaiting+1); \
235 } \
236 isp->isp_osinfo.mbox_locked = 0
237
238 #define FC_SCRATCH_ACQUIRE(isp)
239 #define FC_SCRATCH_RELEASE(isp)
240
241 #ifndef SCSI_GOOD
242 #define SCSI_GOOD 0x0
243 #endif
244 #ifndef SCSI_CHECK
245 #define SCSI_CHECK 0x2
246 #endif
247 #ifndef SCSI_BUSY
248 #define SCSI_BUSY 0x8
249 #endif
250 #ifndef SCSI_QFULL
251 #define SCSI_QFULL 0x28
252 #endif
253
254 #define XS_T struct scsipi_xfer
255 #define XS_CHANNEL(xs) ((int) (xs)->xs_periph->periph_channel->chan_channel)
256 #define XS_ISP(xs) \
257 ((void *)(xs)->xs_periph->periph_channel->chan_adapter->adapt_dev)
258 #define XS_LUN(xs) ((int) (xs)->xs_periph->periph_lun)
259 #define XS_TGT(xs) ((int) (xs)->xs_periph->periph_target)
260 #define XS_CDBP(xs) ((caddr_t) (xs)->cmd)
261 #define XS_CDBLEN(xs) (xs)->cmdlen
262 #define XS_XFRLEN(xs) (xs)->datalen
263 #define XS_TIME(xs) (xs)->timeout
264 #define XS_RESID(xs) (xs)->resid
265 #define XS_STSP(xs) (&(xs)->status)
266 #define XS_SNSP(xs) (&(xs)->sense.scsi_sense)
267 #define XS_SNSLEN(xs) (sizeof (xs)->sense)
268 #define XS_SNSKEY(xs) ((xs)->sense.scsi_sense.flags)
269 #define XS_TAG_P(ccb) (((xs)->xs_control & XS_CTL_POLL) != 0)
270 #define XS_TAG_TYPE(xs) \
271 (((xs)->xs_control & XS_CTL_URGENT) ? REQFLAG_HTAG : \
272 ((xs)->bp && xs->bp->b_flags & B_ASYNC) ? REQFLAG_STAG : REQFLAG_OTAG)
273
274 #define XS_SETERR(xs, v) (xs)->error = v
275
276 # define HBA_NOERROR XS_NOERROR
277 # define HBA_BOTCH XS_DRIVER_STUFFUP
278 # define HBA_CMDTIMEOUT XS_TIMEOUT
279 # define HBA_SELTIMEOUT XS_SELTIMEOUT
280 # define HBA_TGTBSY XS_BUSY
281 # define HBA_BUSRESET XS_RESET
282 # define HBA_ABORTED XS_DRIVER_STUFFUP
283 # define HBA_DATAOVR XS_DRIVER_STUFFUP
284 # define HBA_ARQFAIL XS_DRIVER_STUFFUP
285
286 #define XS_ERR(xs) (xs)->error
287
288 #define XS_NOERR(xs) (xs)->error == XS_NOERROR
289
290 #define XS_INITERR(xs) (xs)->error = 0, XS_CMD_S_CLEAR(xs)
291
292 #define XS_SAVE_SENSE(xs, sp) \
293 if (xs->error == XS_NOERROR) { \
294 xs->error = XS_SENSE; \
295 } \
296 memcpy(&(xs)->sense, sp->req_sense_data, \
297 imin(XS_SNSLEN(xs), sp->req_sense_len))
298
299 #define XS_SET_STATE_STAT(a, b, c)
300
301 #define DEFAULT_IID(x) 7
302 #define DEFAULT_LOOPID(x) 108
303 #define DEFAULT_NODEWWN(isp) (isp)->isp_osinfo.un._wwn
304 #define DEFAULT_PORTWWN(isp) (isp)->isp_osinfo.un._wwn
305 #define ISP_NODEWWN(isp) FCPARAM(isp)->isp_nodewwn
306 #define ISP_PORTWWN(isp) FCPARAM(isp)->isp_portwwn
307
308 #if _BYTE_ORDER == _BIG_ENDIAN
309 #ifdef ISP_SBUS_SUPPORTED
310 #define ISP_IOXPUT_8(isp, s, d) *(d) = s
311 #define ISP_IOXPUT_16(isp, s, d) \
312 *(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap16(s)
313 #define ISP_IOXPUT_32(isp, s, d) \
314 *(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap32(s)
315
316 #define ISP_IOXGET_8(isp, s, d) d = (*((u_int8_t *)s))
317 #define ISP_IOXGET_16(isp, s, d) \
318 d = (isp->isp_bustype == ISP_BT_SBUS)? \
319 *((u_int16_t *)s) : bswap16(*((u_int16_t *)s))
320 #define ISP_IOXGET_32(isp, s, d) \
321 d = (isp->isp_bustype == ISP_BT_SBUS)? \
322 *((u_int32_t *)s) : bswap32(*((u_int32_t *)s))
323 #else
324 #define ISP_IOXPUT_8(isp, s, d) *(d) = s
325 #define ISP_IOXPUT_16(isp, s, d) *(d) = bswap16(s)
326 #define ISP_IOXPUT_32(isp, s, d) *(d) = bswap32(s)
327 #define ISP_IOXGET_8(isp, s, d) d = (*((u_int8_t *)s))
328 #define ISP_IOXGET_16(isp, s, d) d = bswap16(*((u_int16_t *)s))
329 #define ISP_IOXGET_32(isp, s, d) d = bswap32(*((u_int32_t *)s))
330 #endif
331 #define ISP_SWIZZLE_NVRAM_WORD(isp, rp) *rp = bswap16(*rp)
332 #else
333 #define ISP_IOXPUT_8(isp, s, d) *(d) = s
334 #define ISP_IOXPUT_16(isp, s, d) *(d) = s
335 #define ISP_IOXPUT_32(isp, s, d) *(d) = s
336 #define ISP_IOXGET_8(isp, s, d) d = *(s)
337 #define ISP_IOXGET_16(isp, s, d) d = *(s)
338 #define ISP_IOXGET_32(isp, s, d) d = *(s)
339 #define ISP_SWIZZLE_NVRAM_WORD(isp, rp)
340 #endif
341
342 /*
343 * Includes of common header files
344 */
345
346 #include <dev/ic/ispreg.h>
347 #include <dev/ic/ispvar.h>
348 #include <dev/ic/ispmbox.h>
349 #include <dev/ic/isp_ioctl.h>
350
351 /*
352 * isp_osinfo definitions, extensions and shorthand.
353 */
354 #define isp_name isp_osinfo._dev.dv_xname
355 #define isp_unit isp_osinfo._dev.dv_unit
356 #define isp_chanA isp_osinfo._chan
357 #define isp_chanB isp_osinfo._chan_b
358
359
360 /*
361 * Driver prototypes..
362 */
363 void isp_attach(struct ispsoftc *);
364 void isp_uninit(struct ispsoftc *);
365
366 static INLINE void isp_lock(struct ispsoftc *);
367 static INLINE void isp_unlock(struct ispsoftc *);
368 static INLINE char *strncat(char *, const char *, size_t);
369 static INLINE u_int64_t
370 isp_microtime_sub(struct timeval *, struct timeval *);
371 static void isp_wait_complete(struct ispsoftc *);
372
373 /*
374 * Driver wide data...
375 */
376
377 /*
378 * Locking macros...
379 */
380 #define ISP_LOCK isp_lock
381 #define ISP_UNLOCK isp_unlock
382 #define ISP_ILOCK(x) isp_lock(x); isp->isp_osinfo.onintstack++
383 #define ISP_IUNLOCK(x) isp->isp_osinfo.onintstack--; isp_unlock(x)
384
385 /*
386 * Platform private flags
387 */
388
389 #define XS_PSTS_INWDOG 0x10000000
390 #define XS_PSTS_GRACE 0x20000000
391 #define XS_PSTS_ALL 0x30000000
392
393 #define XS_CMD_S_WDOG(xs) (xs)->xs_status |= XS_PSTS_INWDOG
394 #define XS_CMD_C_WDOG(xs) (xs)->xs_status &= ~XS_PSTS_INWDOG
395 #define XS_CMD_WDOG_P(xs) (((xs)->xs_status & XS_PSTS_INWDOG) != 0)
396
397 #define XS_CMD_S_GRACE(xs) (xs)->xs_status |= XS_PSTS_GRACE
398 #define XS_CMD_C_GRACE(xs) (xs)->xs_status &= ~XS_PSTS_GRACE
399 #define XS_CMD_GRACE_P(xs) (((xs)->xs_status & XS_PSTS_GRACE) != 0)
400
401 #define XS_CMD_S_DONE(xs) (xs)->xs_status |= XS_STS_DONE
402 #define XS_CMD_C_DONE(xs) (xs)->xs_status &= ~XS_STS_DONE
403 #define XS_CMD_DONE_P(xs) (((xs)->xs_status & XS_STS_DONE) != 0)
404
405 #define XS_CMD_S_CLEAR(xs) (xs)->xs_status &= ~XS_PSTS_ALL
406
407 /*
408 * Platform specific 'inline' or support functions
409 */
410 static INLINE void
411 isp_lock(struct ispsoftc *isp)
412 {
413 int s = splbio();
414 if (isp->isp_osinfo.islocked++ == 0) {
415 isp->isp_osinfo.splsaved = s;
416 } else {
417 splx(s);
418 }
419 }
420
421 static INLINE void
422 isp_unlock(struct ispsoftc *isp)
423 {
424 if (isp->isp_osinfo.islocked-- <= 1) {
425 isp->isp_osinfo.islocked = 0;
426 splx(isp->isp_osinfo.splsaved);
427 }
428 }
429
430 static INLINE char *
431 strncat(char *d, const char *s, size_t c)
432 {
433 char *t = d;
434
435 if (c) {
436 while (*d)
437 d++;
438 while ((*d++ = *s++)) {
439 if (--c == 0) {
440 *d = '\0';
441 break;
442 }
443 }
444 }
445 return (t);
446 }
447
448 static INLINE u_int64_t
449 isp_microtime_sub(struct timeval *b, struct timeval *a)
450 {
451 struct timeval x;
452 u_int64_t elapsed;
453 timersub(b, a, &x);
454 elapsed = GET_NANOSEC(&x);
455 if (elapsed == 0)
456 elapsed++;
457 return (elapsed);
458 }
459
460 static INLINE void
461 isp_wait_complete(struct ispsoftc *isp)
462 {
463 int lim;
464 if (isp->isp_mbxwrk0)
465 lim = 60;
466 else
467 lim = 5;
468 if (isp->isp_osinfo.onintstack || isp->isp_osinfo.no_mbox_ints) {
469 int useclim = 1000000 * lim, usecs = 0;
470 /*
471 * For sanity's sake, we don't delay longer
472 * than 5 seconds for polled commands.
473 */
474 while (usecs < useclim) {
475 u_int16_t isr, sema, mbox;
476 if (isp->isp_mboxbsy == 0) {
477 break;
478 }
479 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
480 isp_intr(isp, isr, sema, mbox);
481 if (isp->isp_mboxbsy == 0) {
482 break;
483 }
484 }
485 USEC_DELAY(500);
486 usecs += 500;
487 }
488 if (isp->isp_mboxbsy != 0) {
489 isp_prt(isp, ISP_LOGWARN,
490 "Polled Mailbox Command (0x%x) Timeout",
491 isp->isp_lastmbxcmd);
492 }
493 } else {
494 int rv = 0;
495 isp->isp_osinfo.mboxwaiting = 1;
496 while (isp->isp_osinfo.mboxwaiting && rv == 0) {
497 struct timeval dtime;
498 int timo;
499 struct timeval tv;
500
501 dtime.tv_sec = lim;
502 dtime.tv_usec = 0;
503 microtime(&tv);
504 timeradd(&tv, &dtime, &tv);
505 if ((timo = hzto(&tv)) == 0) {
506 timo = 1;
507 }
508 rv = tsleep(&isp->isp_osinfo.mboxwaiting,
509 PRIBIO, "isp_mboxcmd", timo);
510 }
511 if (rv == EWOULDBLOCK) {
512 isp->isp_mboxbsy = 0;
513 isp->isp_osinfo.mboxwaiting = 0;
514 isp_prt(isp, ISP_LOGWARN,
515 "Interrupting Mailbox Command (0x%x) Timeout",
516 isp->isp_lastmbxcmd);
517 }
518 }
519 }
520
521
522 /*
523 * Common inline functions
524 */
525 #include <dev/ic/isp_inline.h>
526
527 #if !defined(ISP_DISABLE_FW) && !defined(ISP_COMPILE_FW)
528 #define ISP_COMPILE_FW 1
529 #endif
530 #endif /* _ISP_NETBSD_H */
531