Initial import from FreeBSD RELENG_4:
[games.git] / sys / dev / disk / isp / isp_freebsd.h
1 /* $FreeBSD: src/sys/dev/isp/isp_freebsd.h,v 1.26.2.17 2002/10/11 17:35:11 mjacob Exp $ */
2 /*
3  * Qlogic ISP SCSI Host Adapter FreeBSD Wrapper Definitions
4  * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002 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      4
31 #define ISP_PLATFORM_VERSION_MINOR      16
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/queue.h>
37 #include <sys/malloc.h>
38 #include <sys/proc.h>
39 #include <sys/bus.h>
40
41 #include <machine/bus_memio.h>
42 #include <machine/bus_pio.h>
43 #include <machine/bus.h>
44 #include <machine/clock.h>
45 #include <machine/cpu.h>
46
47 #include <cam/cam.h>
48 #include <cam/cam_debug.h>
49 #include <cam/cam_ccb.h>
50 #include <cam/cam_sim.h>
51 #include <cam/cam_xpt.h>
52 #include <cam/cam_xpt_sim.h>
53 #include <cam/cam_debug.h>
54 #include <cam/scsi/scsi_all.h>
55 #include <cam/scsi/scsi_message.h>
56
57 #include "opt_ddb.h"
58 #include "opt_isp.h"
59
60 /*
61  * Not quite yet
62  */
63 /* #define      ISP_DAC_SUPPORTED       1 */
64
65 /*
66  * Efficiency- get rid of SBus code && tests unless we need them.
67  */
68 #if     _MACHINE_ARCH == sparc64
69 #define ISP_SBUS_SUPPORTED      1
70 #else
71 #define ISP_SBUS_SUPPORTED      0
72 #endif
73
74 #define HANDLE_LOOPSTATE_IN_OUTER_LAYERS        1
75
76 typedef void ispfwfunc(int, int, int, u_int16_t **);
77
78 #ifdef  ISP_TARGET_MODE
79 #define ISP_TARGET_FUNCTIONS    1
80 #define ATPDPSIZE       256
81 typedef struct {
82         u_int32_t       orig_datalen;
83         u_int32_t       bytes_xfered;
84         u_int32_t       last_xframt;
85         u_int32_t       tag     : 16,
86                         lun     : 13,   /* not enough */
87                         state   : 3;
88 } atio_private_data_t;
89 #define ATPD_STATE_FREE                 0
90 #define ATPD_STATE_ATIO                 1
91 #define ATPD_STATE_CAM                  2
92 #define ATPD_STATE_CTIO                 3
93 #define ATPD_STATE_LAST_CTIO            4
94 #define ATPD_STATE_PDON                 5
95
96 typedef struct tstate {
97         struct tstate *next;
98         struct cam_path *owner;
99         struct ccb_hdr_slist atios;
100         struct ccb_hdr_slist inots;
101         lun_id_t lun;
102         int bus;
103         u_int32_t hold;
104         int atio_count;
105 } tstate_t;
106
107 #define LUN_HASH_SIZE                   32
108 #define LUN_HASH_FUNC(isp, port, lun)                                   \
109         ((IS_DUALBUS(isp)) ?                                            \
110                 (((lun) & ((LUN_HASH_SIZE >> 1) - 1)) << (port)) :      \
111                 ((lun) & (LUN_HASH_SIZE - 1)))
112 #endif
113
114 struct isposinfo {
115         struct ispsoftc *       next;
116         u_int64_t               default_port_wwn;
117         u_int64_t               default_node_wwn;
118         u_int32_t               default_id;
119         device_t                dev;
120         struct cam_sim          *sim;
121         struct cam_path         *path;
122         struct cam_sim          *sim2;
123         struct cam_path         *path2;
124         struct intr_config_hook ehook;
125         u_int8_t                : 1,
126                 fcbsy           : 1,
127                 ktmature        : 1,
128                 mboxwaiting     : 1,
129                 intsok          : 1,
130                 simqfrozen      : 3;
131         int                     islocked;
132         int                     splsaved;
133         struct proc             *kproc;
134         bus_dma_tag_t           cdmat;
135         bus_dmamap_t            cdmap;
136 #define isp_cdmat               isp_osinfo.cdmat
137 #define isp_cdmap               isp_osinfo.cdmap
138 #ifdef  ISP_TARGET_MODE
139 #define TM_WANTED               0x80
140 #define TM_BUSY                 0x40
141 #define TM_WILDCARD_ENABLED     0x02
142 #define TM_TMODE_ENABLED        0x01
143         u_int8_t                tmflags[2];     /* two busses */
144         u_int8_t                rstatus[2];     /* two bussed */
145         u_int16_t               rollinfo;
146         tstate_t                tsdflt[2];      /* two busses */
147         tstate_t                *lun_hash[LUN_HASH_SIZE];
148         atio_private_data_t     atpdp[ATPDPSIZE];
149 #endif
150 };
151
152 #define isp_lock        isp_osinfo.lock
153
154 /*
155  * Locking macros...
156  */
157
158 #define ISP_LOCK                isp_lockspl
159 #define ISP_UNLOCK              isp_unlockspl
160 #define ISPLOCK_2_CAMLOCK(x)
161 #define CAMLOCK_2_ISPLOCK(x)
162
163 /*
164  * Required Macros/Defines
165  */
166
167 #define INLINE                  __inline
168
169 #define ISP2100_SCRLEN          0x800
170
171 #define MEMZERO                 bzero
172 #define MEMCPY(dst, src, amt)   bcopy((src), (dst), (amt))
173 #define SNPRINTF                snprintf
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)      ((IS_FC(isp) || IS_ULTRA2(isp))? 1024 : 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_mbxworkp); \
211         } \
212         isp->isp_mboxbsy = 0
213 #define MBOX_RELEASE(isp)
214
215 #define FC_SCRATCH_ACQUIRE(isp)                                         \
216         if (isp->isp_osinfo.fcbsy) {                                    \
217                 isp_prt(isp, ISP_LOGWARN,                               \
218                     "FC scratch area busy (line %d)!", __LINE__);       \
219         } else                                                          \
220                 isp->isp_osinfo.fcbsy = 1
221 #define FC_SCRATCH_RELEASE(isp)          isp->isp_osinfo.fcbsy = 0
222
223 #ifndef SCSI_GOOD
224 #define SCSI_GOOD       SCSI_STATUS_OK
225 #endif
226 #ifndef SCSI_CHECK
227 #define SCSI_CHECK      SCSI_STATUS_CHECK_COND
228 #endif
229 #ifndef SCSI_BUSY
230 #define SCSI_BUSY       SCSI_STATUS_BUSY
231 #endif
232 #ifndef SCSI_QFULL
233 #define SCSI_QFULL      SCSI_STATUS_QUEUE_FULL
234 #endif
235
236 #define XS_T                    struct ccb_scsiio
237 #define XS_ISP(ccb)             ((struct ispsoftc *) (ccb)->ccb_h.spriv_ptr1)
238 #define XS_CHANNEL(ccb)         cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path))
239 #define XS_TGT(ccb)             (ccb)->ccb_h.target_id
240 #define XS_LUN(ccb)             (ccb)->ccb_h.target_lun
241
242 #define XS_CDBP(ccb)    \
243         (((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \
244          (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes)
245
246 #define XS_CDBLEN(ccb)          (ccb)->cdb_len
247 #define XS_XFRLEN(ccb)          (ccb)->dxfer_len
248 #define XS_TIME(ccb)            (ccb)->ccb_h.timeout
249 #define XS_RESID(ccb)           (ccb)->resid
250 #define XS_STSP(ccb)            (&(ccb)->scsi_status)
251 #define XS_SNSP(ccb)            (&(ccb)->sense_data)
252
253 #define XS_SNSLEN(ccb)          \
254         imin((sizeof((ccb)->sense_data)), ccb->sense_len)
255
256 #define XS_SNSKEY(ccb)          ((ccb)->sense_data.flags & 0xf)
257 #define XS_TAG_P(ccb)   \
258         (((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \
259          (ccb)->tag_action != CAM_TAG_ACTION_NONE)
260
261 #define XS_TAG_TYPE(ccb)        \
262         ((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \
263          ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG))
264                 
265
266 #define XS_SETERR(ccb, v)       (ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \
267                                 (ccb)->ccb_h.status |= v, \
268                                 (ccb)->ccb_h.spriv_field0 |= ISP_SPRIV_ERRSET
269
270 #       define  HBA_NOERROR             CAM_REQ_INPROG
271 #       define  HBA_BOTCH               CAM_UNREC_HBA_ERROR
272 #       define  HBA_CMDTIMEOUT          CAM_CMD_TIMEOUT
273 #       define  HBA_SELTIMEOUT          CAM_SEL_TIMEOUT
274 #       define  HBA_TGTBSY              CAM_SCSI_STATUS_ERROR
275 #       define  HBA_BUSRESET            CAM_SCSI_BUS_RESET
276 #       define  HBA_ABORTED             CAM_REQ_ABORTED
277 #       define  HBA_DATAOVR             CAM_DATA_RUN_ERR
278 #       define  HBA_ARQFAIL             CAM_AUTOSENSE_FAIL
279
280
281 #define XS_ERR(ccb)             ((ccb)->ccb_h.status & CAM_STATUS_MASK)
282
283 #define XS_NOERR(ccb)           \
284         (((ccb)->ccb_h.spriv_field0 & ISP_SPRIV_ERRSET) == 0 || \
285          ((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
286
287 #define XS_INITERR(ccb)         \
288         XS_SETERR(ccb, CAM_REQ_INPROG), (ccb)->ccb_h.spriv_field0 = 0
289
290 #define XS_SAVE_SENSE(xs, sp)                           \
291         (xs)->ccb_h.status |= CAM_AUTOSNS_VALID,        \
292         bcopy(sp->req_sense_data, &(xs)->sense_data,    \
293             imin(XS_SNSLEN(xs), sp->req_sense_len))
294
295 #define XS_SET_STATE_STAT(a, b, c)
296
297 #define DEFAULT_IID(x)          (isp)->isp_osinfo.default_id
298 #define DEFAULT_LOOPID(x)       (isp)->isp_osinfo.default_id
299 #define DEFAULT_NODEWWN(isp)    (isp)->isp_osinfo.default_node_wwn
300 #define DEFAULT_PORTWWN(isp)    (isp)->isp_osinfo.default_port_wwn
301 #define ISP_NODEWWN(isp)        FCPARAM(isp)->isp_nodewwn
302 #define ISP_PORTWWN(isp)        FCPARAM(isp)->isp_portwwn
303
304 #if     BYTE_ORDER == BIG_ENDIAN
305 #ifdef  ISP_SBUS_SUPPORTED
306 #define ISP_IOXPUT_8(isp, s, d)         *(d) = s
307 #define ISP_IOXPUT_16(isp, s, d)                                \
308         *(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap16(s)
309 #define ISP_IOXPUT_32(isp, s, d)                                \
310         *(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap32(s)
311 #define ISP_IOXGET_8(isp, s, d)         d = (*((u_int8_t *)s))
312 #define ISP_IOXGET_16(isp, s, d)                                \
313         d = (isp->isp_bustype == ISP_BT_SBUS)?                  \
314         *((u_int16_t *)s) : bswap16(*((u_int16_t *)s))
315 #define ISP_IOXGET_32(isp, s, d)                                \
316         d = (isp->isp_bustype == ISP_BT_SBUS)?                  \
317         *((u_int32_t *)s) : bswap32(*((u_int32_t *)s))
318 #else
319 #define ISP_IOXPUT_8(isp, s, d)         *(d) = s
320 #define ISP_IOXPUT_16(isp, s, d)        *(d) = bswap16(s)
321 #define ISP_IOXPUT_32(isp, s, d)        *(d) = bswap32(s)
322 #define ISP_IOXGET_8(isp, s, d)         d = (*((u_int8_t *)s))
323 #define ISP_IOXGET_16(isp, s, d)        d = bswap16(*((u_int16_t *)s))
324 #define ISP_IOXGET_32(isp, s, d)        d = bswap32(*((u_int32_t *)s))
325 #endif
326 #define ISP_SWIZZLE_NVRAM_WORD(isp, rp) *rp = bswap16(*rp)
327 #else
328 #define ISP_IOXPUT_8(isp, s, d)         *(d) = s
329 #define ISP_IOXPUT_16(isp, s, d)        *(d) = s
330 #define ISP_IOXPUT_32(isp, s, d)        *(d) = s
331 #define ISP_IOXGET_8(isp, s, d)         d = *(s)
332 #define ISP_IOXGET_16(isp, s, d)        d = *(s)
333 #define ISP_IOXGET_32(isp, s, d)        d = *(s)
334 #define ISP_SWIZZLE_NVRAM_WORD(isp, rp)
335 #endif
336
337 /*
338  * Includes of common header files
339  */
340
341 #include <dev/isp/ispreg.h>
342 #include <dev/isp/ispvar.h>
343 #include <dev/isp/ispmbox.h>
344
345 /*
346  * isp_osinfo definiitions && shorthand
347  */
348 #define SIMQFRZ_RESOURCE        0x1
349 #define SIMQFRZ_LOOPDOWN        0x2
350 #define SIMQFRZ_TIMED           0x4
351
352 #define isp_sim         isp_osinfo.sim
353 #define isp_path        isp_osinfo.path
354 #define isp_sim2        isp_osinfo.sim2
355 #define isp_path2       isp_osinfo.path2
356 #define isp_dev         isp_osinfo.dev
357
358 /*
359  * prototypes for isp_pci && isp_freebsd to share
360  */
361 extern void isp_attach(struct ispsoftc *);
362 extern void isp_uninit(struct ispsoftc *);
363
364 /*
365  * Platform private flags
366  */
367 #define ISP_SPRIV_ERRSET        0x1
368 #define ISP_SPRIV_INWDOG        0x2
369 #define ISP_SPRIV_GRACE         0x4
370 #define ISP_SPRIV_DONE          0x8
371
372 #define XS_CMD_S_WDOG(sccb)     (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_INWDOG
373 #define XS_CMD_C_WDOG(sccb)     (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_INWDOG
374 #define XS_CMD_WDOG_P(sccb)     ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_INWDOG)
375
376 #define XS_CMD_S_GRACE(sccb)    (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_GRACE
377 #define XS_CMD_C_GRACE(sccb)    (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_GRACE
378 #define XS_CMD_GRACE_P(sccb)    ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_GRACE)
379
380 #define XS_CMD_S_DONE(sccb)     (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_DONE
381 #define XS_CMD_C_DONE(sccb)     (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_DONE
382 #define XS_CMD_DONE_P(sccb)     ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_DONE)
383
384 #define XS_CMD_S_CLEAR(sccb)    (sccb)->ccb_h.spriv_field0 = 0
385 /*
386  * Platform specific inline functions
387  */
388 static INLINE void isp_lockspl(struct ispsoftc *);
389 static INLINE void
390 isp_lockspl(struct ispsoftc *isp)
391 {
392        int s = splcam();
393        if (isp->isp_osinfo.islocked++ == 0) {  
394                isp->isp_osinfo.splsaved = s;
395        } else {
396                splx(s);
397        }
398 }
399
400 static INLINE void isp_unlockspl(struct ispsoftc *);
401 static INLINE void
402 isp_unlockspl(struct ispsoftc *isp)
403 {
404        if (isp->isp_osinfo.islocked) {
405                if (--isp->isp_osinfo.islocked == 0) {
406                        splx(isp->isp_osinfo.splsaved);
407                }
408        }
409 }
410
411 static INLINE void isp_mbox_wait_complete(struct ispsoftc *);
412 static INLINE void
413 isp_mbox_wait_complete(struct ispsoftc *isp)
414 {
415         if (isp->isp_osinfo.intsok) {
416                 int lim = ((isp->isp_mbxwrk0)? 120 : 20) * hz;
417                 isp->isp_osinfo.mboxwaiting = 1;
418                 (void) tsleep(&isp->isp_mbxworkp, PRIBIO,
419                     "isp_mboxwaiting", lim);
420                 if (isp->isp_mboxbsy != 0) {
421                         isp_prt(isp, ISP_LOGWARN,
422                             "Interrupting Mailbox Command (0x%x) Timeout",
423                             isp->isp_lastmbxcmd);
424                         isp->isp_mboxbsy = 0;
425                 }
426                 isp->isp_osinfo.mboxwaiting = 0;
427         } else {
428                 int lim = ((isp->isp_mbxwrk0)? 240 : 60) * 10000;
429                 int j;
430                 for (j = 0; j < lim; j++) {
431                         u_int16_t isr, sema, mbox;
432                         if (isp->isp_mboxbsy == 0) {
433                                 break;
434                         }
435                         if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
436                                 isp_intr(isp, isr, sema, mbox);
437                                 if (isp->isp_mboxbsy == 0) {
438                                         break;
439                                 }
440                         }
441                         USEC_DELAY(500);
442                 }
443                 if (isp->isp_mboxbsy != 0) {
444                         isp_prt(isp, ISP_LOGWARN,
445                             "Polled Mailbox Command (0x%x) Timeout",
446                             isp->isp_lastmbxcmd);
447                 }
448         }
449 }
450
451 static INLINE u_int64_t nanotime_sub(struct timespec *, struct timespec *);
452 static INLINE u_int64_t
453 nanotime_sub(struct timespec *b, struct timespec *a)
454 {
455         u_int64_t elapsed;
456         struct timespec x = *b;
457         timespecsub(&x, a);
458         elapsed = GET_NANOSEC(&x);
459         if (elapsed == 0)
460                 elapsed++;
461         return (elapsed);
462 }
463
464 static INLINE char *strncat(char *, const char *, size_t);
465 static INLINE char *
466 strncat(char *d, const char *s, size_t c)
467 {
468         char *t = d;
469
470         if (c) {
471                 while (*d)
472                         d++;
473                 while ((*d++ = *s++)) {
474                         if (--c == 0) {
475                                 *d = '\0';
476                                 break;
477                         }
478                 }
479         }
480         return (t);
481 }
482
483 /*
484  * Common inline functions
485  */
486
487 #include <dev/isp/isp_inline.h>
488 #endif  /* _ISP_FREEBSD_H */