Merge from vendor branch NTPD:
[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.5 2003/08/07 21:16:53 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
42 #include <machine/bus_memio.h>
43 #include <machine/bus_pio.h>
44 #include <machine/bus.h>
45 #include <machine/clock.h>
46 #include <machine/cpu.h>
47
48 #include <bus/cam/cam.h>
49 #include <bus/cam/cam_debug.h>
50 #include <bus/cam/cam_ccb.h>
51 #include <bus/cam/cam_sim.h>
52 #include <bus/cam/cam_xpt.h>
53 #include <bus/cam/cam_xpt_sim.h>
54 #include <bus/cam/cam_debug.h>
55 #include <bus/cam/scsi/scsi_all.h>
56 #include <bus/cam/scsi/scsi_message.h>
57
58 #include "opt_ddb.h"
59 #include "opt_isp.h"
60
61 /*
62  * Not quite yet
63  */
64 /* #define      ISP_DAC_SUPPORTED       1 */
65
66 /*
67  * Efficiency- get rid of SBus code && tests unless we need them.
68  */
69 #if     _MACHINE_ARCH == sparc64
70 #define ISP_SBUS_SUPPORTED      1
71 #else
72 #define ISP_SBUS_SUPPORTED      0
73 #endif
74
75 #define HANDLE_LOOPSTATE_IN_OUTER_LAYERS        1
76
77 typedef void ispfwfunc(int, int, int, u_int16_t **);
78
79 #ifdef  ISP_TARGET_MODE
80 #define ISP_TARGET_FUNCTIONS    1
81 #define ATPDPSIZE       256
82 typedef struct {
83         u_int32_t       orig_datalen;
84         u_int32_t       bytes_xfered;
85         u_int32_t       last_xframt;
86         u_int32_t       tag     : 16,
87                         lun     : 13,   /* not enough */
88                         state   : 3;
89 } atio_private_data_t;
90 #define ATPD_STATE_FREE                 0
91 #define ATPD_STATE_ATIO                 1
92 #define ATPD_STATE_CAM                  2
93 #define ATPD_STATE_CTIO                 3
94 #define ATPD_STATE_LAST_CTIO            4
95 #define ATPD_STATE_PDON                 5
96
97 typedef struct tstate {
98         struct tstate *next;
99         struct cam_path *owner;
100         struct ccb_hdr_slist atios;
101         struct ccb_hdr_slist inots;
102         lun_id_t lun;
103         int bus;
104         u_int32_t hold;
105         int atio_count;
106 } tstate_t;
107
108 #define LUN_HASH_SIZE                   32
109 #define LUN_HASH_FUNC(isp, port, lun)                                   \
110         ((IS_DUALBUS(isp)) ?                                            \
111                 (((lun) & ((LUN_HASH_SIZE >> 1) - 1)) << (port)) :      \
112                 ((lun) & (LUN_HASH_SIZE - 1)))
113 #endif
114
115 struct isposinfo {
116         struct ispsoftc *       next;
117         u_int64_t               default_port_wwn;
118         u_int64_t               default_node_wwn;
119         u_int32_t               default_id;
120         device_t                dev;
121         struct cam_sim          *sim;
122         struct cam_path         *path;
123         struct cam_sim          *sim2;
124         struct cam_path         *path2;
125         struct intr_config_hook ehook;
126         u_int8_t                : 1,
127                 fcbsy           : 1,
128                 ktmature        : 1,
129                 mboxwaiting     : 1,
130                 intsok          : 1,
131                 simqfrozen      : 3;
132         int                     islocked;
133         int                     splsaved;
134         struct thread           *kthread;
135         bus_dma_tag_t           cdmat;
136         bus_dmamap_t            cdmap;
137 #define isp_cdmat               isp_osinfo.cdmat
138 #define isp_cdmap               isp_osinfo.cdmap
139 #ifdef  ISP_TARGET_MODE
140 #define TM_WANTED               0x80
141 #define TM_BUSY                 0x40
142 #define TM_WILDCARD_ENABLED     0x02
143 #define TM_TMODE_ENABLED        0x01
144         u_int8_t                tmflags[2];     /* two busses */
145         u_int8_t                rstatus[2];     /* two bussed */
146         u_int16_t               rollinfo;
147         tstate_t                tsdflt[2];      /* two busses */
148         tstate_t                *lun_hash[LUN_HASH_SIZE];
149         atio_private_data_t     atpdp[ATPDPSIZE];
150 #endif
151 };
152
153 #define isp_lock        isp_osinfo.lock
154
155 /*
156  * Locking macros...
157  */
158
159 #define ISP_LOCK                isp_lockspl
160 #define ISP_UNLOCK              isp_unlockspl
161 #define ISPLOCK_2_CAMLOCK(x)
162 #define CAMLOCK_2_ISPLOCK(x)
163
164 /*
165  * Required Macros/Defines
166  */
167
168 #define INLINE                  __inline
169
170 #define ISP2100_SCRLEN          0x800
171
172 #define MEMZERO                 bzero
173 #define MEMCPY(dst, src, amt)   bcopy((src), (dst), (amt))
174 #define SNPRINTF                snprintf
175 #define USEC_DELAY              DELAY
176 #define USEC_SLEEP(isp, x)              \
177         if (isp->isp_osinfo.intsok)     \
178                 ISP_UNLOCK(isp);        \
179         DELAY(x);                       \
180         if (isp->isp_osinfo.intsok)     \
181                 ISP_LOCK(isp)
182
183 #define NANOTIME_T              struct timespec
184 #define GET_NANOTIME            nanotime
185 #define GET_NANOSEC(x)          ((x)->tv_sec * 1000000000 + (x)->tv_nsec)
186 #define NANOTIME_SUB            nanotime_sub
187
188 #define MAXISPREQUEST(isp)      ((IS_FC(isp) || IS_ULTRA2(isp))? 1024 : 256)
189
190 #define MEMORYBARRIER(isp, type, offset, size)                  \
191 switch (type) {                                                 \
192 case SYNC_SFORDEV:                                              \
193 case SYNC_REQUEST:                                              \
194         bus_dmamap_sync(isp->isp_cdmat, isp->isp_cdmap,         \
195            BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);         \
196         break;                                                  \
197 case SYNC_SFORCPU:                                              \
198 case SYNC_RESULT:                                               \
199         bus_dmamap_sync(isp->isp_cdmat, isp->isp_cdmap,         \
200            BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);       \
201         break;                                                  \
202 default:                                                        \
203         break;                                                  \
204 }
205
206 #define MBOX_ACQUIRE(isp)
207 #define MBOX_WAIT_COMPLETE              isp_mbox_wait_complete
208 #define MBOX_NOTIFY_COMPLETE(isp)       \
209         if (isp->isp_osinfo.mboxwaiting) { \
210                 isp->isp_osinfo.mboxwaiting = 0; \
211                 wakeup(&isp->isp_mbxworkp); \
212         } \
213         isp->isp_mboxbsy = 0
214 #define MBOX_RELEASE(isp)
215
216 #define FC_SCRATCH_ACQUIRE(isp)                                         \
217         if (isp->isp_osinfo.fcbsy) {                                    \
218                 isp_prt(isp, ISP_LOGWARN,                               \
219                     "FC scratch area busy (line %d)!", __LINE__);       \
220         } else                                                          \
221                 isp->isp_osinfo.fcbsy = 1
222 #define FC_SCRATCH_RELEASE(isp)          isp->isp_osinfo.fcbsy = 0
223
224 #ifndef SCSI_GOOD
225 #define SCSI_GOOD       SCSI_STATUS_OK
226 #endif
227 #ifndef SCSI_CHECK
228 #define SCSI_CHECK      SCSI_STATUS_CHECK_COND
229 #endif
230 #ifndef SCSI_BUSY
231 #define SCSI_BUSY       SCSI_STATUS_BUSY
232 #endif
233 #ifndef SCSI_QFULL
234 #define SCSI_QFULL      SCSI_STATUS_QUEUE_FULL
235 #endif
236
237 #define XS_T                    struct ccb_scsiio
238 #define XS_ISP(ccb)             ((struct ispsoftc *) (ccb)->ccb_h.spriv_ptr1)
239 #define XS_CHANNEL(ccb)         cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path))
240 #define XS_TGT(ccb)             (ccb)->ccb_h.target_id
241 #define XS_LUN(ccb)             (ccb)->ccb_h.target_lun
242
243 #define XS_CDBP(ccb)    \
244         (((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \
245          (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes)
246
247 #define XS_CDBLEN(ccb)          (ccb)->cdb_len
248 #define XS_XFRLEN(ccb)          (ccb)->dxfer_len
249 #define XS_TIME(ccb)            (ccb)->ccb_h.timeout
250 #define XS_RESID(ccb)           (ccb)->resid
251 #define XS_STSP(ccb)            (&(ccb)->scsi_status)
252 #define XS_SNSP(ccb)            (&(ccb)->sense_data)
253
254 #define XS_SNSLEN(ccb)          \
255         imin((sizeof((ccb)->sense_data)), ccb->sense_len)
256
257 #define XS_SNSKEY(ccb)          ((ccb)->sense_data.flags & 0xf)
258 #define XS_TAG_P(ccb)   \
259         (((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \
260          (ccb)->tag_action != CAM_TAG_ACTION_NONE)
261
262 #define XS_TAG_TYPE(ccb)        \
263         ((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \
264          ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG))
265                 
266
267 #define XS_SETERR(ccb, v)       (ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \
268                                 (ccb)->ccb_h.status |= v, \
269                                 (ccb)->ccb_h.spriv_field0 |= ISP_SPRIV_ERRSET
270
271 #       define  HBA_NOERROR             CAM_REQ_INPROG
272 #       define  HBA_BOTCH               CAM_UNREC_HBA_ERROR
273 #       define  HBA_CMDTIMEOUT          CAM_CMD_TIMEOUT
274 #       define  HBA_SELTIMEOUT          CAM_SEL_TIMEOUT
275 #       define  HBA_TGTBSY              CAM_SCSI_STATUS_ERROR
276 #       define  HBA_BUSRESET            CAM_SCSI_BUS_RESET
277 #       define  HBA_ABORTED             CAM_REQ_ABORTED
278 #       define  HBA_DATAOVR             CAM_DATA_RUN_ERR
279 #       define  HBA_ARQFAIL             CAM_AUTOSENSE_FAIL
280
281
282 #define XS_ERR(ccb)             ((ccb)->ccb_h.status & CAM_STATUS_MASK)
283
284 #define XS_NOERR(ccb)           \
285         (((ccb)->ccb_h.spriv_field0 & ISP_SPRIV_ERRSET) == 0 || \
286          ((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)
287
288 #define XS_INITERR(ccb)         \
289         XS_SETERR(ccb, CAM_REQ_INPROG), (ccb)->ccb_h.spriv_field0 = 0
290
291 #define XS_SAVE_SENSE(xs, sp)                           \
292         (xs)->ccb_h.status |= CAM_AUTOSNS_VALID,        \
293         bcopy(sp->req_sense_data, &(xs)->sense_data,    \
294             imin(XS_SNSLEN(xs), sp->req_sense_len))
295
296 #define XS_SET_STATE_STAT(a, b, c)
297
298 #define DEFAULT_IID(x)          (isp)->isp_osinfo.default_id
299 #define DEFAULT_LOOPID(x)       (isp)->isp_osinfo.default_id
300 #define DEFAULT_NODEWWN(isp)    (isp)->isp_osinfo.default_node_wwn
301 #define DEFAULT_PORTWWN(isp)    (isp)->isp_osinfo.default_port_wwn
302 #define ISP_NODEWWN(isp)        FCPARAM(isp)->isp_nodewwn
303 #define ISP_PORTWWN(isp)        FCPARAM(isp)->isp_portwwn
304
305 #if     BYTE_ORDER == BIG_ENDIAN
306 #ifdef  ISP_SBUS_SUPPORTED
307 #define ISP_IOXPUT_8(isp, s, d)         *(d) = s
308 #define ISP_IOXPUT_16(isp, s, d)                                \
309         *(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap16(s)
310 #define ISP_IOXPUT_32(isp, s, d)                                \
311         *(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap32(s)
312 #define ISP_IOXGET_8(isp, s, d)         d = (*((u_int8_t *)s))
313 #define ISP_IOXGET_16(isp, s, d)                                \
314         d = (isp->isp_bustype == ISP_BT_SBUS)?                  \
315         *((u_int16_t *)s) : bswap16(*((u_int16_t *)s))
316 #define ISP_IOXGET_32(isp, s, d)                                \
317         d = (isp->isp_bustype == ISP_BT_SBUS)?                  \
318         *((u_int32_t *)s) : bswap32(*((u_int32_t *)s))
319 #else
320 #define ISP_IOXPUT_8(isp, s, d)         *(d) = s
321 #define ISP_IOXPUT_16(isp, s, d)        *(d) = bswap16(s)
322 #define ISP_IOXPUT_32(isp, s, d)        *(d) = bswap32(s)
323 #define ISP_IOXGET_8(isp, s, d)         d = (*((u_int8_t *)s))
324 #define ISP_IOXGET_16(isp, s, d)        d = bswap16(*((u_int16_t *)s))
325 #define ISP_IOXGET_32(isp, s, d)        d = bswap32(*((u_int32_t *)s))
326 #endif
327 #define ISP_SWIZZLE_NVRAM_WORD(isp, rp) *rp = bswap16(*rp)
328 #else
329 #define ISP_IOXPUT_8(isp, s, d)         *(d) = s
330 #define ISP_IOXPUT_16(isp, s, d)        *(d) = s
331 #define ISP_IOXPUT_32(isp, s, d)        *(d) = s
332 #define ISP_IOXGET_8(isp, s, d)         d = *(s)
333 #define ISP_IOXGET_16(isp, s, d)        d = *(s)
334 #define ISP_IOXGET_32(isp, s, d)        d = *(s)
335 #define ISP_SWIZZLE_NVRAM_WORD(isp, rp)
336 #endif
337
338 /*
339  * Includes of common header files
340  */
341
342 #include "ispreg.h"
343 #include "ispvar.h"
344 #include "ispmbox.h"
345
346 /*
347  * isp_osinfo definiitions && shorthand
348  */
349 #define SIMQFRZ_RESOURCE        0x1
350 #define SIMQFRZ_LOOPDOWN        0x2
351 #define SIMQFRZ_TIMED           0x4
352
353 #define isp_sim         isp_osinfo.sim
354 #define isp_path        isp_osinfo.path
355 #define isp_sim2        isp_osinfo.sim2
356 #define isp_path2       isp_osinfo.path2
357 #define isp_dev         isp_osinfo.dev
358
359 /*
360  * prototypes for isp_pci && isp_freebsd to share
361  */
362 extern void isp_attach(struct ispsoftc *);
363 extern void isp_uninit(struct ispsoftc *);
364
365 /*
366  * Platform private flags
367  */
368 #define ISP_SPRIV_ERRSET        0x1
369 #define ISP_SPRIV_INWDOG        0x2
370 #define ISP_SPRIV_GRACE         0x4
371 #define ISP_SPRIV_DONE          0x8
372
373 #define XS_CMD_S_WDOG(sccb)     (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_INWDOG
374 #define XS_CMD_C_WDOG(sccb)     (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_INWDOG
375 #define XS_CMD_WDOG_P(sccb)     ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_INWDOG)
376
377 #define XS_CMD_S_GRACE(sccb)    (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_GRACE
378 #define XS_CMD_C_GRACE(sccb)    (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_GRACE
379 #define XS_CMD_GRACE_P(sccb)    ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_GRACE)
380
381 #define XS_CMD_S_DONE(sccb)     (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_DONE
382 #define XS_CMD_C_DONE(sccb)     (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_DONE
383 #define XS_CMD_DONE_P(sccb)     ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_DONE)
384
385 #define XS_CMD_S_CLEAR(sccb)    (sccb)->ccb_h.spriv_field0 = 0
386 /*
387  * Platform specific inline functions
388  */
389 static INLINE void isp_lockspl(struct ispsoftc *);
390 static INLINE void
391 isp_lockspl(struct ispsoftc *isp)
392 {
393        int s = splcam();
394        if (isp->isp_osinfo.islocked++ == 0) {  
395                isp->isp_osinfo.splsaved = s;
396        } else {
397                splx(s);
398        }
399 }
400
401 static INLINE void isp_unlockspl(struct ispsoftc *);
402 static INLINE void
403 isp_unlockspl(struct ispsoftc *isp)
404 {
405        if (isp->isp_osinfo.islocked) {
406                if (--isp->isp_osinfo.islocked == 0) {
407                        splx(isp->isp_osinfo.splsaved);
408                }
409        }
410 }
411
412 static INLINE void isp_mbox_wait_complete(struct ispsoftc *);
413 static INLINE void
414 isp_mbox_wait_complete(struct ispsoftc *isp)
415 {
416         if (isp->isp_osinfo.intsok) {
417                 int lim = ((isp->isp_mbxwrk0)? 120 : 20) * hz;
418                 isp->isp_osinfo.mboxwaiting = 1;
419                 (void) tsleep(&isp->isp_mbxworkp, 0, "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 "isp_inline.h"
488 #endif  /* _ISP_FREEBSD_H */