Merge branch 'vendor/GCC'
[dragonfly.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 /* $DragonFly: src/sys/dev/disk/isp/isp_freebsd.h,v 1.8 2006/12/20 18:14:38 dillon Exp $ */
3 /*
4  * Qlogic ISP SCSI Host Adapter FreeBSD Wrapper Definitions
5  * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002 by Matthew Jacob
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice immediately at the beginning of the file, without modification,
12  *    this list of conditions, and the following disclaimer.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 #ifndef _ISP_FREEBSD_H
29 #define _ISP_FREEBSD_H
30
31 #define ISP_PLATFORM_VERSION_MAJOR      4
32 #define ISP_PLATFORM_VERSION_MINOR      16
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/queue.h>
38 #include <sys/malloc.h>
39 #include <sys/proc.h>
40 #include <sys/bus.h>
41 #include <sys/thread2.h>
42
43 #include <machine/clock.h>
44 #include <machine/cpu.h>
45
46 #include <bus/cam/cam.h>
47 #include <bus/cam/cam_debug.h>
48 #include <bus/cam/cam_ccb.h>
49 #include <bus/cam/cam_sim.h>
50 #include <bus/cam/cam_xpt.h>
51 #include <bus/cam/cam_xpt_sim.h>
52 #include <bus/cam/cam_debug.h>
53 #include <bus/cam/scsi/scsi_all.h>
54 #include <bus/cam/scsi/scsi_message.h>
55
56 #include "opt_ddb.h"
57 #include "opt_isp.h"
58
59 /*
60  * Not quite yet
61  */
62 /* #define      ISP_DAC_SUPPORTED       1 */
63
64 /*
65  * Efficiency- get rid of SBus code && tests unless we need them.
66  */
67 #if     _MACHINE_ARCH == sparc64
68 #define ISP_SBUS_SUPPORTED      1
69 #else
70 #define ISP_SBUS_SUPPORTED      0
71 #endif
72
73 #define HANDLE_LOOPSTATE_IN_OUTER_LAYERS        1
74
75 typedef void ispfwfunc(int, int, int, u_int16_t **);
76
77 #ifdef  ISP_TARGET_MODE
78 #define ISP_TARGET_FUNCTIONS    1
79 #define ATPDPSIZE       256
80 typedef struct {
81         u_int32_t       orig_datalen;
82         u_int32_t       bytes_xfered;
83         u_int32_t       last_xframt;
84         u_int32_t       tag     : 16,
85                         lun     : 13,   /* not enough */
86                         state   : 3;
87 } atio_private_data_t;
88 #define ATPD_STATE_FREE                 0
89 #define ATPD_STATE_ATIO                 1
90 #define ATPD_STATE_CAM                  2
91 #define ATPD_STATE_CTIO                 3
92 #define ATPD_STATE_LAST_CTIO            4
93 #define ATPD_STATE_PDON                 5
94
95 typedef struct tstate {
96         struct tstate *next;
97         struct cam_path *owner;
98         struct ccb_hdr_slist atios;
99         struct ccb_hdr_slist inots;
100         lun_id_t lun;
101         int bus;
102         u_int32_t hold;
103         int atio_count;
104 } tstate_t;
105
106 #define LUN_HASH_SIZE                   32
107 #define LUN_HASH_FUNC(isp, port, lun)                                   \
108         ((IS_DUALBUS(isp)) ?                                            \
109                 (((lun) & ((LUN_HASH_SIZE >> 1) - 1)) << (port)) :      \
110                 ((lun) & (LUN_HASH_SIZE - 1)))
111 #endif
112
113 struct isposinfo {
114         struct ispsoftc *       next;
115         u_int64_t               default_port_wwn;
116         u_int64_t               default_node_wwn;
117         u_int32_t               default_id;
118         device_t                dev;
119         struct cam_sim          *sim;
120         struct cam_path         *path;
121         struct cam_sim          *sim2;
122         struct cam_path         *path2;
123         struct intr_config_hook ehook;
124         u_int8_t                : 1,
125                 fcbsy           : 1,
126                 ktmature        : 1,
127                 mboxwaiting     : 1,
128                 intsok          : 1,
129                 simqfrozen      : 3;
130         struct thread           *kthread;
131         bus_dma_tag_t           cdmat;
132         bus_dmamap_t            cdmap;
133 #define isp_cdmat               isp_osinfo.cdmat
134 #define isp_cdmap               isp_osinfo.cdmap
135 #ifdef  ISP_TARGET_MODE
136 #define TM_WANTED               0x80
137 #define TM_BUSY                 0x40
138 #define TM_WILDCARD_ENABLED     0x02
139 #define TM_TMODE_ENABLED        0x01
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                isp_lockspl
156 #define ISP_UNLOCK              isp_unlockspl
157 #define ISPLOCK_2_CAMLOCK(x)
158 #define CAMLOCK_2_ISPLOCK(x)
159
160 /*
161  * Required Macros/Defines
162  */
163
164 #define INLINE                  __inline
165
166 #define ISP2100_SCRLEN          0x800
167
168 #define MEMZERO                 bzero
169 #define MEMCPY(dst, src, amt)   bcopy((src), (dst), (amt))
170 #define SNPRINTF                ksnprintf
171 #define USEC_DELAY              DELAY
172 #define USEC_SLEEP(isp, x)              \
173         if (isp->isp_osinfo.intsok)     \
174                 ISP_UNLOCK(isp);        \
175         DELAY(x);                       \
176         if (isp->isp_osinfo.intsok)     \
177                 ISP_LOCK(isp)
178
179 #define NANOTIME_T              struct timespec
180 #define GET_NANOTIME            nanotime
181 #define GET_NANOSEC(x)          ((x)->tv_sec * 1000000000 + (x)->tv_nsec)
182 #define NANOTIME_SUB            nanotime_sub
183
184 #define MAXISPREQUEST(isp)      ((IS_FC(isp) || IS_ULTRA2(isp))? 1024 : 256)
185
186 #define MEMORYBARRIER(isp, type, offset, size)                  \
187 switch (type) {                                                 \
188 case SYNC_SFORDEV:                                              \
189 case SYNC_REQUEST:                                              \
190         bus_dmamap_sync(isp->isp_cdmat, isp->isp_cdmap,         \
191            BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);         \
192         break;                                                  \
193 case SYNC_SFORCPU:                                              \
194 case SYNC_RESULT:                                               \
195         bus_dmamap_sync(isp->isp_cdmat, isp->isp_cdmap,         \
196            BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);       \
197         break;                                                  \
198 default:                                                        \
199         break;                                                  \
200 }
201
202 #define MBOX_ACQUIRE(isp)
203 #define MBOX_WAIT_COMPLETE              isp_mbox_wait_complete
204 #define MBOX_NOTIFY_COMPLETE(isp)       \
205         if (isp->isp_osinfo.mboxwaiting) { \
206                 isp->isp_osinfo.mboxwaiting = 0; \
207                 wakeup(&isp->isp_mbxworkp); \
208         } \
209         isp->isp_mboxbsy = 0
210 #define MBOX_RELEASE(isp)
211
212 #define FC_SCRATCH_ACQUIRE(isp)                                         \
213         if (isp->isp_osinfo.fcbsy) {                                    \
214                 isp_prt(isp, ISP_LOGWARN,                               \
215                     "FC scratch area busy (line %d)!", __LINE__);       \
216         } else                                                          \
217                 isp->isp_osinfo.fcbsy = 1
218 #define FC_SCRATCH_RELEASE(isp)          isp->isp_osinfo.fcbsy = 0
219
220 #ifndef SCSI_GOOD
221 #define SCSI_GOOD       SCSI_STATUS_OK
222 #endif
223 #ifndef SCSI_CHECK
224 #define SCSI_CHECK      SCSI_STATUS_CHECK_COND
225 #endif
226 #ifndef SCSI_BUSY
227 #define SCSI_BUSY       SCSI_STATUS_BUSY
228 #endif
229 #ifndef SCSI_QFULL
230 #define SCSI_QFULL      SCSI_STATUS_QUEUE_FULL
231 #endif
232
233 #define XS_T                    struct ccb_scsiio
234 #define XS_ISP(ccb)             ((struct ispsoftc *) (ccb)->ccb_h.spriv_ptr1)
235 #define XS_CHANNEL(ccb)         cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path))
236 #define XS_TGT(ccb)             (ccb)->ccb_h.target_id
237 #define XS_LUN(ccb)             (ccb)->ccb_h.target_lun
238
239 #define XS_CDBP(ccb)    \
240         (((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \
241          (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes)
242
243 #define XS_CDBLEN(ccb)          (ccb)->cdb_len
244 #define XS_XFRLEN(ccb)          (ccb)->dxfer_len
245 #define XS_TIME(ccb)            (ccb)->ccb_h.timeout
246 #define XS_RESID(ccb)           (ccb)->resid
247 #define XS_STSP(ccb)            (&(ccb)->scsi_status)
248 #define XS_SNSP(ccb)            (&(ccb)->sense_data)
249
250 #define XS_SNSLEN(ccb)          \
251         imin((sizeof((ccb)->sense_data)), ccb->sense_len)
252
253 #define XS_SNSKEY(ccb)          ((ccb)->sense_data.flags & 0xf)
254 #define XS_TAG_P(ccb)   \
255         (((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \
256          (ccb)->tag_action != CAM_TAG_ACTION_NONE)
257
258 #define XS_TAG_TYPE(ccb)        \
259         ((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \
260          ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG))
261                 
262
263 #define XS_SETERR(ccb, v)       (ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \
264                                 (ccb)->ccb_h.status |= v, \
265                                 (ccb)->ccb_h.spriv_field0 |= ISP_SPRIV_ERRSET
266
267 #       define  HBA_NOERROR             CAM_REQ_INPROG
268 #       define  HBA_BOTCH               CAM_UNREC_HBA_ERROR
269 #       define  HBA_CMDTIMEOUT          CAM_CMD_TIMEOUT
270 #       define  HBA_SELTIMEOUT          CAM_SEL_TIMEOUT
271 #       define  HBA_TGTBSY              CAM_SCSI_STATUS_ERROR
272 #       define  HBA_BUSRESET            CAM_SCSI_BUS_RESET
273 #       define  HBA_ABORTED             CAM_REQ_ABORTED
274 #       define  HBA_DATAOVR             CAM_DATA_RUN_ERR
275 #       define  HBA_ARQFAIL             CAM_AUTOSENSE_FAIL
276
277
278 #define XS_ERR(ccb)             ((ccb)->ccb_h.status & CAM_STATUS_MASK)
279
280 #define XS_NOERR(ccb)           \
281         (((ccb)->ccb_h.spriv_field0 & ISP_SPRIV_ERRSET) == 0 || \
282          ((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
283
284 #define XS_INITERR(ccb)         \
285         XS_SETERR(ccb, CAM_REQ_INPROG), (ccb)->ccb_h.spriv_field0 = 0
286
287 #define XS_SAVE_SENSE(xs, sp)                           \
288         (xs)->ccb_h.status |= CAM_AUTOSNS_VALID,        \
289         bcopy(sp->req_sense_data, &(xs)->sense_data,    \
290             imin(XS_SNSLEN(xs), sp->req_sense_len))
291
292 #define XS_SET_STATE_STAT(a, b, c)
293
294 #define DEFAULT_IID(x)          (isp)->isp_osinfo.default_id
295 #define DEFAULT_LOOPID(x)       (isp)->isp_osinfo.default_id
296 #define DEFAULT_NODEWWN(isp)    (isp)->isp_osinfo.default_node_wwn
297 #define DEFAULT_PORTWWN(isp)    (isp)->isp_osinfo.default_port_wwn
298 #define ISP_NODEWWN(isp)        FCPARAM(isp)->isp_nodewwn
299 #define ISP_PORTWWN(isp)        FCPARAM(isp)->isp_portwwn
300
301 #if     BYTE_ORDER == BIG_ENDIAN
302 #ifdef  ISP_SBUS_SUPPORTED
303 #define ISP_IOXPUT_8(isp, s, d)         *(d) = s
304 #define ISP_IOXPUT_16(isp, s, d)                                \
305         *(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap16(s)
306 #define ISP_IOXPUT_32(isp, s, d)                                \
307         *(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap32(s)
308 #define ISP_IOXGET_8(isp, s, d)         d = (*((u_int8_t *)s))
309 #define ISP_IOXGET_16(isp, s, d)                                \
310         d = (isp->isp_bustype == ISP_BT_SBUS)?                  \
311         *((u_int16_t *)s) : bswap16(*((u_int16_t *)s))
312 #define ISP_IOXGET_32(isp, s, d)                                \
313         d = (isp->isp_bustype == ISP_BT_SBUS)?                  \
314         *((u_int32_t *)s) : bswap32(*((u_int32_t *)s))
315 #else
316 #define ISP_IOXPUT_8(isp, s, d)         *(d) = s
317 #define ISP_IOXPUT_16(isp, s, d)        *(d) = bswap16(s)
318 #define ISP_IOXPUT_32(isp, s, d)        *(d) = bswap32(s)
319 #define ISP_IOXGET_8(isp, s, d)         d = (*((u_int8_t *)s))
320 #define ISP_IOXGET_16(isp, s, d)        d = bswap16(*((u_int16_t *)s))
321 #define ISP_IOXGET_32(isp, s, d)        d = bswap32(*((u_int32_t *)s))
322 #endif
323 #define ISP_SWIZZLE_NVRAM_WORD(isp, rp) *rp = bswap16(*rp)
324 #else
325 #define ISP_IOXPUT_8(isp, s, d)         *(d) = s
326 #define ISP_IOXPUT_16(isp, s, d)        *(d) = s
327 #define ISP_IOXPUT_32(isp, s, d)        *(d) = s
328 #define ISP_IOXGET_8(isp, s, d)         d = *(s)
329 #define ISP_IOXGET_16(isp, s, d)        d = *(s)
330 #define ISP_IOXGET_32(isp, s, d)        d = *(s)
331 #define ISP_SWIZZLE_NVRAM_WORD(isp, rp)
332 #endif
333
334 /*
335  * Includes of common header files
336  */
337
338 #include "ispreg.h"
339 #include "ispvar.h"
340 #include "ispmbox.h"
341
342 /*
343  * isp_osinfo definiitions && shorthand
344  */
345 #define SIMQFRZ_RESOURCE        0x1
346 #define SIMQFRZ_LOOPDOWN        0x2
347 #define SIMQFRZ_TIMED           0x4
348
349 #define isp_sim         isp_osinfo.sim
350 #define isp_path        isp_osinfo.path
351 #define isp_sim2        isp_osinfo.sim2
352 #define isp_path2       isp_osinfo.path2
353 #define isp_dev         isp_osinfo.dev
354
355 /*
356  * prototypes for isp_pci && isp_freebsd to share
357  */
358 extern void isp_attach(struct ispsoftc *);
359 extern void isp_uninit(struct ispsoftc *);
360
361 /*
362  * Platform private flags
363  */
364 #define ISP_SPRIV_ERRSET        0x1
365 #define ISP_SPRIV_INWDOG        0x2
366 #define ISP_SPRIV_GRACE         0x4
367 #define ISP_SPRIV_DONE          0x8
368
369 #define XS_CMD_S_WDOG(sccb)     (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_INWDOG
370 #define XS_CMD_C_WDOG(sccb)     (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_INWDOG
371 #define XS_CMD_WDOG_P(sccb)     ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_INWDOG)
372
373 #define XS_CMD_S_GRACE(sccb)    (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_GRACE
374 #define XS_CMD_C_GRACE(sccb)    (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_GRACE
375 #define XS_CMD_GRACE_P(sccb)    ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_GRACE)
376
377 #define XS_CMD_S_DONE(sccb)     (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_DONE
378 #define XS_CMD_C_DONE(sccb)     (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_DONE
379 #define XS_CMD_DONE_P(sccb)     ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_DONE)
380
381 #define XS_CMD_S_CLEAR(sccb)    (sccb)->ccb_h.spriv_field0 = 0
382 /*
383  * Platform specific inline functions
384  */
385 static INLINE void isp_lockspl(struct ispsoftc *);
386 static INLINE void
387 isp_lockspl(struct ispsoftc *isp)
388 {
389        crit_enter();
390 }
391
392 static INLINE void isp_unlockspl(struct ispsoftc *);
393 static INLINE void
394 isp_unlockspl(struct ispsoftc *isp)
395 {
396         crit_exit();
397 }
398
399 static INLINE void isp_mbox_wait_complete(struct ispsoftc *);
400 static INLINE void
401 isp_mbox_wait_complete(struct ispsoftc *isp)
402 {
403         if (isp->isp_osinfo.intsok) {
404                 int lim = ((isp->isp_mbxwrk0)? 120 : 20) * hz;
405                 isp->isp_osinfo.mboxwaiting = 1;
406                 (void) tsleep(&isp->isp_mbxworkp, 0, "isp_mboxwaiting", lim);
407                 if (isp->isp_mboxbsy != 0) {
408                         isp_prt(isp, ISP_LOGWARN,
409                             "Interrupting Mailbox Command (0x%x) Timeout",
410                             isp->isp_lastmbxcmd);
411                         isp->isp_mboxbsy = 0;
412                 }
413                 isp->isp_osinfo.mboxwaiting = 0;
414         } else {
415                 int lim = ((isp->isp_mbxwrk0)? 240 : 60) * 10000;
416                 int j;
417                 for (j = 0; j < lim; j++) {
418                         u_int16_t isr, sema, mbox;
419                         if (isp->isp_mboxbsy == 0) {
420                                 break;
421                         }
422                         if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
423                                 isp_intr(isp, isr, sema, mbox);
424                                 if (isp->isp_mboxbsy == 0) {
425                                         break;
426                                 }
427                         }
428                         USEC_DELAY(500);
429                 }
430                 if (isp->isp_mboxbsy != 0) {
431                         isp_prt(isp, ISP_LOGWARN,
432                             "Polled Mailbox Command (0x%x) Timeout",
433                             isp->isp_lastmbxcmd);
434                 }
435         }
436 }
437
438 static INLINE u_int64_t nanotime_sub(struct timespec *, struct timespec *);
439 static INLINE u_int64_t
440 nanotime_sub(struct timespec *b, struct timespec *a)
441 {
442         u_int64_t elapsed;
443         struct timespec x = *b;
444         timespecsub(&x, a);
445         elapsed = GET_NANOSEC(&x);
446         if (elapsed == 0)
447                 elapsed++;
448         return (elapsed);
449 }
450
451 static INLINE char *strncat(char *, const char *, size_t);
452 static INLINE char *
453 strncat(char *d, const char *s, size_t c)
454 {
455         char *t = d;
456
457         if (c) {
458                 while (*d)
459                         d++;
460                 while ((*d++ = *s++)) {
461                         if (--c == 0) {
462                                 *d = '\0';
463                                 break;
464                         }
465                 }
466         }
467         return (t);
468 }
469
470 /*
471  * Common inline functions
472  */
473
474 #include "isp_inline.h"
475 #endif  /* _ISP_FREEBSD_H */