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