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 #ifdef ISP_TARGET_MODE 101 #define ISP_TARGET_FUNCTIONS 1 102 #define ATPDPSIZE 256 103 typedef struct { 104 uint32_t orig_datalen; 105 uint32_t bytes_xfered; 106 uint32_t last_xframt; 107 uint32_t tag : 16, 108 lun : 13, /* not enough */ 109 state : 3; 110 } atio_private_data_t; 111 #define ATPD_STATE_FREE 0 112 #define ATPD_STATE_ATIO 1 113 #define ATPD_STATE_CAM 2 114 #define ATPD_STATE_CTIO 3 115 #define ATPD_STATE_LAST_CTIO 4 116 #define ATPD_STATE_PDON 5 117 118 typedef struct tstate { 119 struct tstate *next; 120 struct cam_path *owner; 121 struct ccb_hdr_slist atios; 122 struct ccb_hdr_slist inots; 123 lun_id_t lun; 124 int bus; 125 uint32_t hold; 126 int atio_count; 127 int inot_count; 128 } tstate_t; 129 130 #define LUN_HASH_SIZE 32 131 #define LUN_HASH_FUNC(isp, port, lun) \ 132 ((IS_DUALBUS(isp)) ? \ 133 (((lun) & ((LUN_HASH_SIZE >> 1) - 1)) << (port)) : \ 134 ((lun) & (LUN_HASH_SIZE - 1))) 135 #endif 136 137 struct isposinfo { 138 struct ispsoftc * next; 139 uint64_t default_port_wwn; 140 uint64_t default_node_wwn; 141 uint32_t default_id; 142 device_t dev; 143 struct cam_sim *sim; 144 struct cam_path *path; 145 struct cam_sim *sim2; 146 struct cam_path *path2; 147 struct intr_config_hook ehook; 148 uint8_t 149 disabled : 1, 150 fcbsy : 1, 151 ktmature : 1, 152 mboxwaiting : 1, 153 intsok : 1, 154 simqfrozen : 3; 155 #if __FreeBSD_version >= 500000 156 struct firmware * fw; 157 struct mtx lock; 158 struct cv kthread_cv; 159 #endif 160 struct proc *kproc; 161 bus_dma_tag_t cdmat; 162 bus_dmamap_t cdmap; 163 #define isp_cdmat isp_osinfo.cdmat 164 #define isp_cdmap isp_osinfo.cdmap 165 #ifdef ISP_TARGET_MODE 166 #define TM_WILDCARD_ENABLED 0x02 167 #define TM_TMODE_ENABLED 0x01 168 uint8_t tmflags[2]; /* two busses */ 169 #define NLEACT 4 170 union ccb * leact[NLEACT]; 171 tstate_t tsdflt[2]; /* two busses */ 172 tstate_t *lun_hash[LUN_HASH_SIZE]; 173 atio_private_data_t atpdp[ATPDPSIZE]; 174 #endif 175 }; 176 177 #define isp_lock isp_osinfo.lock 178 179 /* 180 * Locking macros... 181 */ 182 183 #ifdef ISP_SMPLOCK 184 #define ISP_LOCK(x) mtx_lock(&(x)->isp_lock) 185 #define ISP_UNLOCK(x) mtx_unlock(&(x)->isp_lock) 186 #define ISPLOCK_2_CAMLOCK(isp) \ 187 mtx_unlock(&(isp)->isp_lock); mtx_lock(&Giant) 188 #define CAMLOCK_2_ISPLOCK(isp) \ 189 mtx_unlock(&Giant); mtx_lock(&(isp)->isp_lock) 190 #else 191 #define ISP_LOCK(x) do { } while (0) 192 #define ISP_UNLOCK(x) do { } while (0) 193 #define ISPLOCK_2_CAMLOCK(isp) do { } while (0) 194 #define CAMLOCK_2_ISPLOCK(isp) do { } while (0) 195 #endif 196 197 /* 198 * Required Macros/Defines 199 */ 200 201 #define ISP2100_SCRLEN 0x800 202 203 #define MEMZERO(a, b) memset(a, 0, b) 204 #define MEMCPY memcpy 205 #define SNPRINTF snprintf 206 #define USEC_DELAY DELAY 207 #define USEC_SLEEP(isp, x) \ 208 if (isp->isp_osinfo.intsok) \ 209 ISP_UNLOCK(isp); \ 210 DELAY(x); \ 211 if (isp->isp_osinfo.intsok) \ 212 ISP_LOCK(isp) 213 214 #define NANOTIME_T struct timespec 215 #define GET_NANOTIME nanotime 216 #define GET_NANOSEC(x) ((x)->tv_sec * 1000000000 + (x)->tv_nsec) 217 #define NANOTIME_SUB nanotime_sub 218 219 #define MAXISPREQUEST(isp) ((IS_FC(isp) || IS_ULTRA2(isp))? 1024 : 256) 220 221 #define MEMORYBARRIER(isp, type, offset, size) \ 222 switch (type) { \ 223 case SYNC_SFORDEV: \ 224 case SYNC_REQUEST: \ 225 bus_dmamap_sync(isp->isp_cdmat, isp->isp_cdmap, \ 226 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); \ 227 break; \ 228 case SYNC_SFORCPU: \ 229 case SYNC_RESULT: \ 230 bus_dmamap_sync(isp->isp_cdmat, isp->isp_cdmap, \ 231 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); \ 232 break; \ 233 default: \ 234 break; \ 235 } 236 237 #define MBOX_ACQUIRE(isp) 238 #define MBOX_WAIT_COMPLETE isp_mbox_wait_complete 239 #define MBOX_NOTIFY_COMPLETE(isp) \ 240 if (isp->isp_osinfo.mboxwaiting) { \ 241 isp->isp_osinfo.mboxwaiting = 0; \ 242 wakeup(&isp->isp_mbxworkp); \ 243 } \ 244 isp->isp_mboxbsy = 0 245 #define MBOX_RELEASE(isp) 246 247 #define FC_SCRATCH_ACQUIRE(isp) \ 248 if (isp->isp_osinfo.fcbsy) { \ 249 isp_prt(isp, ISP_LOGWARN, \ 250 "FC scratch area busy (line %d)!", __LINE__); \ 251 } else \ 252 isp->isp_osinfo.fcbsy = 1 253 #define FC_SCRATCH_RELEASE(isp) isp->isp_osinfo.fcbsy = 0 254 255 #ifndef SCSI_GOOD 256 #define SCSI_GOOD SCSI_STATUS_OK 257 #endif 258 #ifndef SCSI_CHECK 259 #define SCSI_CHECK SCSI_STATUS_CHECK_COND 260 #endif 261 #ifndef SCSI_BUSY 262 #define SCSI_BUSY SCSI_STATUS_BUSY 263 #endif 264 #ifndef SCSI_QFULL 265 #define SCSI_QFULL SCSI_STATUS_QUEUE_FULL 266 #endif 267 268 #define XS_T struct ccb_scsiio 269 #define XS_DMA_ADDR_T bus_addr_t 270 #define XS_ISP(ccb) ((ispsoftc_t *) (ccb)->ccb_h.spriv_ptr1) 271 #define XS_CHANNEL(ccb) cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path)) 272 #define XS_TGT(ccb) (ccb)->ccb_h.target_id 273 #define XS_LUN(ccb) (ccb)->ccb_h.target_lun 274 275 #define XS_CDBP(ccb) \ 276 (((ccb)->ccb_h.flags & CAM_CDB_POINTER)? \ 277 (ccb)->cdb_io.cdb_ptr : (ccb)->cdb_io.cdb_bytes) 278 279 #define XS_CDBLEN(ccb) (ccb)->cdb_len 280 #define XS_XFRLEN(ccb) (ccb)->dxfer_len 281 #define XS_TIME(ccb) (ccb)->ccb_h.timeout 282 #define XS_RESID(ccb) (ccb)->resid 283 #define XS_STSP(ccb) (&(ccb)->scsi_status) 284 #define XS_SNSP(ccb) (&(ccb)->sense_data) 285 286 #define XS_SNSLEN(ccb) \ 287 imin((sizeof((ccb)->sense_data)), ccb->sense_len) 288 289 #define XS_SNSKEY(ccb) ((ccb)->sense_data.flags & 0xf) 290 #define XS_TAG_P(ccb) \ 291 (((ccb)->ccb_h.flags & CAM_TAG_ACTION_VALID) && \ 292 (ccb)->tag_action != CAM_TAG_ACTION_NONE) 293 294 #define XS_TAG_TYPE(ccb) \ 295 ((ccb->tag_action == MSG_SIMPLE_Q_TAG)? REQFLAG_STAG : \ 296 ((ccb->tag_action == MSG_HEAD_OF_Q_TAG)? REQFLAG_HTAG : REQFLAG_OTAG)) 297 298 299 #define XS_SETERR(ccb, v) (ccb)->ccb_h.status &= ~CAM_STATUS_MASK, \ 300 (ccb)->ccb_h.status |= v, \ 301 (ccb)->ccb_h.spriv_field0 |= ISP_SPRIV_ERRSET 302 303 # define HBA_NOERROR CAM_REQ_INPROG 304 # define HBA_BOTCH CAM_UNREC_HBA_ERROR 305 # define HBA_CMDTIMEOUT CAM_CMD_TIMEOUT 306 # define HBA_SELTIMEOUT CAM_SEL_TIMEOUT 307 # define HBA_TGTBSY CAM_SCSI_STATUS_ERROR 308 # define HBA_BUSRESET CAM_SCSI_BUS_RESET 309 # define HBA_ABORTED CAM_REQ_ABORTED 310 # define HBA_DATAOVR CAM_DATA_RUN_ERR 311 # define HBA_ARQFAIL CAM_AUTOSENSE_FAIL 312 313 314 #define XS_ERR(ccb) ((ccb)->ccb_h.status & CAM_STATUS_MASK) 315 316 #define XS_NOERR(ccb) \ 317 (((ccb)->ccb_h.spriv_field0 & ISP_SPRIV_ERRSET) == 0 || \ 318 ((ccb)->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) 319 320 #define XS_INITERR(ccb) \ 321 XS_SETERR(ccb, CAM_REQ_INPROG), (ccb)->ccb_h.spriv_field0 = 0 322 323 #define XS_SAVE_SENSE(xs, sp) \ 324 (xs)->ccb_h.status |= CAM_AUTOSNS_VALID, \ 325 memcpy(&(xs)->sense_data, sp->req_sense_data, \ 326 imin(XS_SNSLEN(xs), sp->req_sense_len)) 327 328 #define XS_SET_STATE_STAT(a, b, c) 329 330 #define DEFAULT_IID(x) (isp)->isp_osinfo.default_id 331 #define DEFAULT_LOOPID(x) (isp)->isp_osinfo.default_id 332 #define DEFAULT_NODEWWN(isp) (isp)->isp_osinfo.default_node_wwn 333 #define DEFAULT_PORTWWN(isp) (isp)->isp_osinfo.default_port_wwn 334 #define ISP_NODEWWN(isp) FCPARAM(isp)->isp_nodewwn 335 #define ISP_PORTWWN(isp) FCPARAM(isp)->isp_portwwn 336 337 #if BYTE_ORDER == BIG_ENDIAN 338 #ifdef ISP_SBUS_SUPPORTED 339 #define ISP_IOXPUT_8(isp, s, d) *(d) = s 340 #define ISP_IOXPUT_16(isp, s, d) \ 341 *(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap16(s) 342 #define ISP_IOXPUT_32(isp, s, d) \ 343 *(d) = (isp->isp_bustype == ISP_BT_SBUS)? s : bswap32(s) 344 #define ISP_IOXGET_8(isp, s, d) d = (*((uint8_t *)s)) 345 #define ISP_IOXGET_16(isp, s, d) \ 346 d = (isp->isp_bustype == ISP_BT_SBUS)? \ 347 *((uint16_t *)s) : bswap16(*((uint16_t *)s)) 348 #define ISP_IOXGET_32(isp, s, d) \ 349 d = (isp->isp_bustype == ISP_BT_SBUS)? \ 350 *((uint32_t *)s) : bswap32(*((uint32_t *)s)) 351 #else 352 #define ISP_IOXPUT_8(isp, s, d) *(d) = s 353 #define ISP_IOXPUT_16(isp, s, d) *(d) = bswap16(s) 354 #define ISP_IOXPUT_32(isp, s, d) *(d) = bswap32(s) 355 #define ISP_IOXGET_8(isp, s, d) d = (*((uint8_t *)s)) 356 #define ISP_IOXGET_16(isp, s, d) d = bswap16(*((uint16_t *)s)) 357 #define ISP_IOXGET_32(isp, s, d) d = bswap32(*((uint32_t *)s)) 358 #endif 359 #define ISP_SWIZZLE_NVRAM_WORD(isp, rp) *rp = bswap16(*rp) 360 #else 361 #define ISP_IOXPUT_8(isp, s, d) *(d) = s 362 #define ISP_IOXPUT_16(isp, s, d) *(d) = s 363 #define ISP_IOXPUT_32(isp, s, d) *(d) = s 364 #define ISP_IOXGET_8(isp, s, d) d = *(s) 365 #define ISP_IOXGET_16(isp, s, d) d = *(s) 366 #define ISP_IOXGET_32(isp, s, d) d = *(s) 367 #define ISP_SWIZZLE_NVRAM_WORD(isp, rp) 368 #endif 369 370 /* 371 * Includes of common header files 372 */ 373 374 #include <dev/isp/ispreg.h> 375 #include <dev/isp/ispvar.h> 376 #include <dev/isp/ispmbox.h> 377 378 #ifdef ISP_TARGET_MODE 379 #include <dev/isp/isp_tpublic.h> 380 #endif 381 382 void isp_prt(ispsoftc_t *, int level, const char *, ...) 383 __printflike(3, 4); 384 /* 385 * isp_osinfo definiitions && shorthand 386 */ 387 #define SIMQFRZ_RESOURCE 0x1 388 #define SIMQFRZ_LOOPDOWN 0x2 389 #define SIMQFRZ_TIMED 0x4 390 391 #define isp_sim isp_osinfo.sim 392 #define isp_path isp_osinfo.path 393 #define isp_sim2 isp_osinfo.sim2 394 #define isp_path2 isp_osinfo.path2 395 #define isp_dev isp_osinfo.dev 396 397 /* 398 * prototypes for isp_pci && isp_freebsd to share 399 */ 400 extern void isp_attach(ispsoftc_t *); 401 extern void isp_uninit(ispsoftc_t *); 402 403 /* 404 * driver global data 405 */ 406 extern int isp_announced; 407 408 /* 409 * Platform private flags 410 */ 411 #define ISP_SPRIV_ERRSET 0x1 412 #define ISP_SPRIV_INWDOG 0x2 413 #define ISP_SPRIV_GRACE 0x4 414 #define ISP_SPRIV_DONE 0x8 415 416 #define XS_CMD_S_WDOG(sccb) (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_INWDOG 417 #define XS_CMD_C_WDOG(sccb) (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_INWDOG 418 #define XS_CMD_WDOG_P(sccb) ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_INWDOG) 419 420 #define XS_CMD_S_GRACE(sccb) (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_GRACE 421 #define XS_CMD_C_GRACE(sccb) (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_GRACE 422 #define XS_CMD_GRACE_P(sccb) ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_GRACE) 423 424 #define XS_CMD_S_DONE(sccb) (sccb)->ccb_h.spriv_field0 |= ISP_SPRIV_DONE 425 #define XS_CMD_C_DONE(sccb) (sccb)->ccb_h.spriv_field0 &= ~ISP_SPRIV_DONE 426 #define XS_CMD_DONE_P(sccb) ((sccb)->ccb_h.spriv_field0 & ISP_SPRIV_DONE) 427 428 #define XS_CMD_S_CLEAR(sccb) (sccb)->ccb_h.spriv_field0 = 0 429 430 /* 431 * Platform specific inline functions 432 */ 433 434 static __inline void isp_mbox_wait_complete(ispsoftc_t *); 435 static __inline void 436 isp_mbox_wait_complete(ispsoftc_t *isp) 437 { 438 if (isp->isp_osinfo.intsok) { 439 int lim = ((isp->isp_mbxwrk0)? 120 : 20) * hz; 440 isp->isp_osinfo.mboxwaiting = 1; 441 #ifdef ISP_SMPLOCK 442 (void) msleep(&isp->isp_mbxworkp, 443 &isp->isp_lock, PRIBIO, "isp_mboxwaiting", lim); 444 #else 445 (void) tsleep(&isp->isp_mbxworkp, 446 PRIBIO, "isp_mboxwaiting", lim); 447 #endif 448 if (isp->isp_mboxbsy != 0) { 449 isp_prt(isp, ISP_LOGWARN, 450 "Interrupting Mailbox Command (0x%x) Timeout", 451 isp->isp_lastmbxcmd); 452 isp->isp_mboxbsy = 0; 453 } 454 isp->isp_osinfo.mboxwaiting = 0; 455 } else { 456 int lim = ((isp->isp_mbxwrk0)? 240 : 60) * 10000; 457 int j; 458 for (j = 0; j < lim; j++) { 459 uint16_t isr, sema, mbox; 460 if (isp->isp_mboxbsy == 0) { 461 break; 462 } 463 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) { 464 isp_intr(isp, isr, sema, mbox); 465 if (isp->isp_mboxbsy == 0) { 466 break; 467 } 468 } 469 USEC_DELAY(500); 470 } 471 if (isp->isp_mboxbsy != 0) { 472 isp_prt(isp, ISP_LOGWARN, 473 "Polled Mailbox Command (0x%x) Timeout", 474 isp->isp_lastmbxcmd); 475 } 476 } 477 } 478 479 static __inline uint64_t nanotime_sub(struct timespec *, struct timespec *); 480 static __inline uint64_t 481 nanotime_sub(struct timespec *b, struct timespec *a) 482 { 483 uint64_t elapsed; 484 struct timespec x = *b; 485 timespecsub(&x, a); 486 elapsed = GET_NANOSEC(&x); 487 if (elapsed == 0) 488 elapsed++; 489 return (elapsed); 490 } 491 492 static __inline char *strncat(char *, const char *, size_t); 493 static __inline char * 494 strncat(char *d, const char *s, size_t c) 495 { 496 char *t = d; 497 498 if (c) { 499 while (*d) 500 d++; 501 while ((*d++ = *s++)) { 502 if (--c == 0) { 503 *d = '\0'; 504 break; 505 } 506 } 507 } 508 return (t); 509 } 510 511 /* 512 * ISP Library functions 513 */ 514 515 #include <dev/isp/isp_library.h> 516 517 #endif /* _ISP_FREEBSD_H */ 518