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