1 /* 2 * Copyright (c) 2015, AVAGO Tech. All rights reserved. Author: Marian Choy 3 * Copyright (c) 2014, LSI Corp. All rights reserved. Author: Marian Choy 4 * Support: freebsdraid@avagotech.com 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: 9 * 10 * 1. Redistributions of source code must retain the above copyright notice, 11 * this list of conditions and the following disclaimer. 2. Redistributions 12 * in binary form must reproduce the above copyright notice, this list of 13 * conditions and the following disclaimer in the documentation and/or other 14 * materials provided with the distribution. 3. Neither the name of the 15 * <ORGANIZATION> nor the names of its contributors may be used to endorse or 16 * promote products derived from this software without specific prior written 17 * permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 * 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include "dev/mrsas/mrsas.h" 37 38 #include <cam/cam.h> 39 #include <cam/cam_ccb.h> 40 #include <cam/cam_sim.h> 41 #include <cam/cam_xpt_sim.h> 42 #include <cam/cam_debug.h> 43 #include <cam/cam_periph.h> 44 #include <cam/cam_xpt_periph.h> 45 46 #include <cam/scsi/scsi_all.h> 47 #include <cam/scsi/scsi_message.h> 48 #include <sys/taskqueue.h> 49 #include <sys/kernel.h> 50 51 52 #include <sys/time.h> /* XXX for pcpu.h */ 53 #include <sys/pcpu.h> /* XXX for PCPU_GET */ 54 55 #define smp_processor_id() PCPU_GET(cpuid) 56 57 /* 58 * Function prototypes 59 */ 60 int mrsas_cam_attach(struct mrsas_softc *sc); 61 int mrsas_find_io_type(struct cam_sim *sim, union ccb *ccb); 62 int mrsas_bus_scan(struct mrsas_softc *sc); 63 int mrsas_bus_scan_sim(struct mrsas_softc *sc, struct cam_sim *sim); 64 int 65 mrsas_map_request(struct mrsas_softc *sc, 66 struct mrsas_mpt_cmd *cmd, union ccb *ccb); 67 int 68 mrsas_build_ldio_rw(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd, 69 union ccb *ccb); 70 int 71 mrsas_build_ldio_nonrw(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd, 72 union ccb *ccb); 73 int 74 mrsas_build_syspdio(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd, 75 union ccb *ccb, struct cam_sim *sim, u_int8_t fp_possible); 76 int 77 mrsas_setup_io(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd, 78 union ccb *ccb, u_int32_t device_id, 79 MRSAS_RAID_SCSI_IO_REQUEST * io_request); 80 void mrsas_xpt_freeze(struct mrsas_softc *sc); 81 void mrsas_xpt_release(struct mrsas_softc *sc); 82 void mrsas_cam_detach(struct mrsas_softc *sc); 83 void mrsas_release_mpt_cmd(struct mrsas_mpt_cmd *cmd); 84 void mrsas_unmap_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd); 85 void mrsas_cmd_done(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd); 86 void 87 mrsas_fire_cmd(struct mrsas_softc *sc, u_int32_t req_desc_lo, 88 u_int32_t req_desc_hi); 89 void 90 mrsas_set_pd_lba(MRSAS_RAID_SCSI_IO_REQUEST * io_request, 91 u_int8_t cdb_len, struct IO_REQUEST_INFO *io_info, union ccb *ccb, 92 MR_DRV_RAID_MAP_ALL * local_map_ptr, u_int32_t ref_tag, 93 u_int32_t ld_block_size); 94 static void mrsas_freeze_simq(struct mrsas_mpt_cmd *cmd, struct cam_sim *sim); 95 static void mrsas_cam_poll(struct cam_sim *sim); 96 static void mrsas_action(struct cam_sim *sim, union ccb *ccb); 97 static void mrsas_scsiio_timeout(void *data); 98 static int mrsas_track_scsiio(struct mrsas_softc *sc, target_id_t id, u_int32_t bus_id); 99 static void mrsas_tm_response_code(struct mrsas_softc *sc, 100 MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply); 101 static int mrsas_issue_tm(struct mrsas_softc *sc, 102 MRSAS_REQUEST_DESCRIPTOR_UNION *req_desc); 103 static void 104 mrsas_data_load_cb(void *arg, bus_dma_segment_t *segs, 105 int nseg, int error); 106 static int32_t 107 mrsas_startio(struct mrsas_softc *sc, struct cam_sim *sim, 108 union ccb *ccb); 109 struct mrsas_mpt_cmd *mrsas_get_mpt_cmd(struct mrsas_softc *sc); 110 MRSAS_REQUEST_DESCRIPTOR_UNION * 111 mrsas_get_request_desc(struct mrsas_softc *sc, u_int16_t index); 112 113 extern void 114 mrsas_map_mpt_cmd_status(struct mrsas_mpt_cmd *cmd, u_int8_t status, 115 u_int8_t extStatus); 116 extern int mrsas_reset_targets(struct mrsas_softc *sc); 117 extern u_int16_t MR_TargetIdToLdGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map); 118 extern u_int32_t 119 MR_LdBlockSizeGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map); 120 extern void mrsas_isr(void *arg); 121 extern void mrsas_aen_handler(struct mrsas_softc *sc); 122 extern u_int8_t 123 MR_BuildRaidContext(struct mrsas_softc *sc, 124 struct IO_REQUEST_INFO *io_info, RAID_CONTEXT * pRAID_Context, 125 MR_DRV_RAID_MAP_ALL * map); 126 extern u_int16_t 127 MR_LdSpanArrayGet(u_int32_t ld, u_int32_t span, 128 MR_DRV_RAID_MAP_ALL * map); 129 extern u_int16_t 130 mrsas_get_updated_dev_handle(struct mrsas_softc *sc, 131 PLD_LOAD_BALANCE_INFO lbInfo, struct IO_REQUEST_INFO *io_info); 132 extern u_int8_t 133 megasas_get_best_arm(PLD_LOAD_BALANCE_INFO lbInfo, u_int8_t arm, 134 u_int64_t block, u_int32_t count); 135 extern int mrsas_complete_cmd(struct mrsas_softc *sc, u_int32_t MSIxIndex); 136 extern MR_LD_RAID *MR_LdRaidGet(u_int32_t ld, MR_DRV_RAID_MAP_ALL * map); 137 extern void mrsas_disable_intr(struct mrsas_softc *sc); 138 extern void mrsas_enable_intr(struct mrsas_softc *sc); 139 140 141 /* 142 * mrsas_cam_attach: Main entry to CAM subsystem 143 * input: Adapter instance soft state 144 * 145 * This function is called from mrsas_attach() during initialization to perform 146 * SIM allocations and XPT bus registration. If the kernel version is 7.4 or 147 * earlier, it would also initiate a bus scan. 148 */ 149 int 150 mrsas_cam_attach(struct mrsas_softc *sc) 151 { 152 struct cam_devq *devq; 153 int mrsas_cam_depth; 154 155 mrsas_cam_depth = sc->max_fw_cmds - MRSAS_INTERNAL_CMDS; 156 157 if ((devq = cam_simq_alloc(mrsas_cam_depth)) == NULL) { 158 device_printf(sc->mrsas_dev, "Cannot allocate SIM queue\n"); 159 return (ENOMEM); 160 } 161 /* 162 * Create SIM for bus 0 and register, also create path 163 */ 164 sc->sim_0 = cam_sim_alloc(mrsas_action, mrsas_cam_poll, "mrsas", sc, 165 device_get_unit(sc->mrsas_dev), &sc->sim_lock, mrsas_cam_depth, 166 mrsas_cam_depth, devq); 167 if (sc->sim_0 == NULL) { 168 cam_simq_free(devq); 169 device_printf(sc->mrsas_dev, "Cannot register SIM\n"); 170 return (ENXIO); 171 } 172 /* Initialize taskqueue for Event Handling */ 173 TASK_INIT(&sc->ev_task, 0, (void *)mrsas_aen_handler, sc); 174 sc->ev_tq = taskqueue_create("mrsas_taskq", M_NOWAIT | M_ZERO, 175 taskqueue_thread_enqueue, &sc->ev_tq); 176 177 /* Run the task queue with lowest priority */ 178 taskqueue_start_threads(&sc->ev_tq, 1, 255, "%s taskq", 179 device_get_nameunit(sc->mrsas_dev)); 180 mtx_lock(&sc->sim_lock); 181 if (xpt_bus_register(sc->sim_0, sc->mrsas_dev, 0) != CAM_SUCCESS) { 182 cam_sim_free(sc->sim_0, TRUE); /* passing true frees the devq */ 183 mtx_unlock(&sc->sim_lock); 184 return (ENXIO); 185 } 186 if (xpt_create_path(&sc->path_0, NULL, cam_sim_path(sc->sim_0), 187 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 188 xpt_bus_deregister(cam_sim_path(sc->sim_0)); 189 cam_sim_free(sc->sim_0, TRUE); /* passing true will free the 190 * devq */ 191 mtx_unlock(&sc->sim_lock); 192 return (ENXIO); 193 } 194 mtx_unlock(&sc->sim_lock); 195 196 /* 197 * Create SIM for bus 1 and register, also create path 198 */ 199 sc->sim_1 = cam_sim_alloc(mrsas_action, mrsas_cam_poll, "mrsas", sc, 200 device_get_unit(sc->mrsas_dev), &sc->sim_lock, mrsas_cam_depth, 201 mrsas_cam_depth, devq); 202 if (sc->sim_1 == NULL) { 203 cam_simq_free(devq); 204 device_printf(sc->mrsas_dev, "Cannot register SIM\n"); 205 return (ENXIO); 206 } 207 mtx_lock(&sc->sim_lock); 208 if (xpt_bus_register(sc->sim_1, sc->mrsas_dev, 1) != CAM_SUCCESS) { 209 cam_sim_free(sc->sim_1, TRUE); /* passing true frees the devq */ 210 mtx_unlock(&sc->sim_lock); 211 return (ENXIO); 212 } 213 if (xpt_create_path(&sc->path_1, NULL, cam_sim_path(sc->sim_1), 214 CAM_TARGET_WILDCARD, 215 CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 216 xpt_bus_deregister(cam_sim_path(sc->sim_1)); 217 cam_sim_free(sc->sim_1, TRUE); 218 mtx_unlock(&sc->sim_lock); 219 return (ENXIO); 220 } 221 mtx_unlock(&sc->sim_lock); 222 223 #if (__FreeBSD_version <= 704000) 224 if (mrsas_bus_scan(sc)) { 225 device_printf(sc->mrsas_dev, "Error in bus scan.\n"); 226 return (1); 227 } 228 #endif 229 return (0); 230 } 231 232 /* 233 * mrsas_cam_detach: De-allocates and teardown CAM 234 * input: Adapter instance soft state 235 * 236 * De-registers and frees the paths and SIMs. 237 */ 238 void 239 mrsas_cam_detach(struct mrsas_softc *sc) 240 { 241 if (sc->ev_tq != NULL) 242 taskqueue_free(sc->ev_tq); 243 mtx_lock(&sc->sim_lock); 244 if (sc->path_0) 245 xpt_free_path(sc->path_0); 246 if (sc->sim_0) { 247 xpt_bus_deregister(cam_sim_path(sc->sim_0)); 248 cam_sim_free(sc->sim_0, FALSE); 249 } 250 if (sc->path_1) 251 xpt_free_path(sc->path_1); 252 if (sc->sim_1) { 253 xpt_bus_deregister(cam_sim_path(sc->sim_1)); 254 cam_sim_free(sc->sim_1, TRUE); 255 } 256 mtx_unlock(&sc->sim_lock); 257 } 258 259 /* 260 * mrsas_action: SIM callback entry point 261 * input: pointer to SIM pointer to CAM Control Block 262 * 263 * This function processes CAM subsystem requests. The type of request is stored 264 * in ccb->ccb_h.func_code. The preprocessor #ifdef is necessary because 265 * ccb->cpi.maxio is not supported for FreeBSD version 7.4 or earlier. 266 */ 267 static void 268 mrsas_action(struct cam_sim *sim, union ccb *ccb) 269 { 270 struct mrsas_softc *sc = (struct mrsas_softc *)cam_sim_softc(sim); 271 struct ccb_hdr *ccb_h = &(ccb->ccb_h); 272 u_int32_t device_id; 273 274 /* 275 * Check if the system going down 276 * or the adapter is in unrecoverable critical error 277 */ 278 if (sc->remove_in_progress || 279 (sc->adprecovery == MRSAS_HW_CRITICAL_ERROR)) { 280 ccb->ccb_h.status |= CAM_DEV_NOT_THERE; 281 xpt_done(ccb); 282 return; 283 } 284 285 switch (ccb->ccb_h.func_code) { 286 case XPT_SCSI_IO: 287 { 288 device_id = ccb_h->target_id; 289 290 /* 291 * bus 0 is LD, bus 1 is for system-PD 292 */ 293 if (cam_sim_bus(sim) == 1 && 294 sc->pd_list[device_id].driveState != MR_PD_STATE_SYSTEM) { 295 ccb->ccb_h.status |= CAM_DEV_NOT_THERE; 296 xpt_done(ccb); 297 } else { 298 if (mrsas_startio(sc, sim, ccb)) { 299 ccb->ccb_h.status |= CAM_REQ_INVALID; 300 xpt_done(ccb); 301 } 302 } 303 break; 304 } 305 case XPT_ABORT: 306 { 307 ccb->ccb_h.status = CAM_UA_ABORT; 308 xpt_done(ccb); 309 break; 310 } 311 case XPT_RESET_BUS: 312 { 313 xpt_done(ccb); 314 break; 315 } 316 case XPT_GET_TRAN_SETTINGS: 317 { 318 ccb->cts.protocol = PROTO_SCSI; 319 ccb->cts.protocol_version = SCSI_REV_2; 320 ccb->cts.transport = XPORT_SPI; 321 ccb->cts.transport_version = 2; 322 ccb->cts.xport_specific.spi.valid = CTS_SPI_VALID_DISC; 323 ccb->cts.xport_specific.spi.flags = CTS_SPI_FLAGS_DISC_ENB; 324 ccb->cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ; 325 ccb->cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB; 326 ccb->ccb_h.status = CAM_REQ_CMP; 327 xpt_done(ccb); 328 break; 329 } 330 case XPT_SET_TRAN_SETTINGS: 331 { 332 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 333 xpt_done(ccb); 334 break; 335 } 336 case XPT_CALC_GEOMETRY: 337 { 338 cam_calc_geometry(&ccb->ccg, 1); 339 xpt_done(ccb); 340 break; 341 } 342 case XPT_PATH_INQ: 343 { 344 ccb->cpi.version_num = 1; 345 ccb->cpi.hba_inquiry = 0; 346 ccb->cpi.target_sprt = 0; 347 #if (__FreeBSD_version >= 902001) 348 ccb->cpi.hba_misc = PIM_UNMAPPED; 349 #else 350 ccb->cpi.hba_misc = 0; 351 #endif 352 ccb->cpi.hba_eng_cnt = 0; 353 ccb->cpi.max_lun = MRSAS_SCSI_MAX_LUNS; 354 ccb->cpi.unit_number = cam_sim_unit(sim); 355 ccb->cpi.bus_id = cam_sim_bus(sim); 356 ccb->cpi.initiator_id = MRSAS_SCSI_INITIATOR_ID; 357 ccb->cpi.base_transfer_speed = 150000; 358 strlcpy(ccb->cpi.sim_vid, "FreeBSD", SIM_IDLEN); 359 strlcpy(ccb->cpi.hba_vid, "AVAGO", HBA_IDLEN); 360 strlcpy(ccb->cpi.dev_name, cam_sim_name(sim), DEV_IDLEN); 361 ccb->cpi.transport = XPORT_SPI; 362 ccb->cpi.transport_version = 2; 363 ccb->cpi.protocol = PROTO_SCSI; 364 ccb->cpi.protocol_version = SCSI_REV_2; 365 if (ccb->cpi.bus_id == 0) 366 ccb->cpi.max_target = MRSAS_MAX_PD - 1; 367 else 368 ccb->cpi.max_target = MRSAS_MAX_LD_IDS - 1; 369 #if (__FreeBSD_version > 704000) 370 ccb->cpi.maxio = sc->max_num_sge * MRSAS_PAGE_SIZE; 371 #endif 372 ccb->ccb_h.status = CAM_REQ_CMP; 373 xpt_done(ccb); 374 break; 375 } 376 default: 377 { 378 ccb->ccb_h.status = CAM_REQ_INVALID; 379 xpt_done(ccb); 380 break; 381 } 382 } 383 } 384 385 /* 386 * mrsas_scsiio_timeout: Callback function for IO timed out 387 * input: mpt command context 388 * 389 * This function will execute after timeout value provided by ccb header from 390 * CAM layer, if timer expires. Driver will run timer for all DCDM and LDIO 391 * coming from CAM layer. This function is callback function for IO timeout 392 * and it runs in no-sleep context. Set do_timedout_reset in Adapter context 393 * so that it will execute OCR/Kill adpter from ocr_thread context. 394 */ 395 static void 396 mrsas_scsiio_timeout(void *data) 397 { 398 struct mrsas_mpt_cmd *cmd; 399 struct mrsas_softc *sc; 400 u_int32_t target_id; 401 402 if (!data) 403 return; 404 405 cmd = (struct mrsas_mpt_cmd *)data; 406 sc = cmd->sc; 407 408 if (cmd->ccb_ptr == NULL) { 409 printf("command timeout with NULL ccb\n"); 410 return; 411 } 412 413 /* 414 * Below callout is dummy entry so that it will be cancelled from 415 * mrsas_cmd_done(). Now Controller will go to OCR/Kill Adapter based 416 * on OCR enable/disable property of Controller from ocr_thread 417 * context. 418 */ 419 #if (__FreeBSD_version >= 1000510) 420 callout_reset_sbt(&cmd->cm_callout, SBT_1S * 180, 0, 421 mrsas_scsiio_timeout, cmd, 0); 422 #else 423 callout_reset(&cmd->cm_callout, (180000 * hz) / 1000, 424 mrsas_scsiio_timeout, cmd); 425 #endif 426 427 if (cmd->ccb_ptr->cpi.bus_id == 0) 428 target_id = cmd->ccb_ptr->ccb_h.target_id; 429 else 430 target_id = (cmd->ccb_ptr->ccb_h.target_id + (MRSAS_MAX_PD - 1)); 431 432 /* Save the cmd to be processed for TM, if it is not there in the array */ 433 if (sc->target_reset_pool[target_id] == NULL) { 434 sc->target_reset_pool[target_id] = cmd; 435 mrsas_atomic_inc(&sc->target_reset_outstanding); 436 } 437 438 return; 439 } 440 441 /* 442 * mrsas_startio: SCSI IO entry point 443 * input: Adapter instance soft state 444 * pointer to CAM Control Block 445 * 446 * This function is the SCSI IO entry point and it initiates IO processing. It 447 * copies the IO and depending if the IO is read/write or inquiry, it would 448 * call mrsas_build_ldio() or mrsas_build_dcdb(), respectively. It returns 0 449 * if the command is sent to firmware successfully, otherwise it returns 1. 450 */ 451 static int32_t 452 mrsas_startio(struct mrsas_softc *sc, struct cam_sim *sim, 453 union ccb *ccb) 454 { 455 struct mrsas_mpt_cmd *cmd; 456 struct ccb_hdr *ccb_h = &(ccb->ccb_h); 457 struct ccb_scsiio *csio = &(ccb->csio); 458 MRSAS_REQUEST_DESCRIPTOR_UNION *req_desc; 459 u_int8_t cmd_type; 460 461 if ((csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE && 462 (!sc->fw_sync_cache_support)) { 463 ccb->ccb_h.status = CAM_REQ_CMP; 464 xpt_done(ccb); 465 return (0); 466 } 467 ccb_h->status |= CAM_SIM_QUEUED; 468 cmd = mrsas_get_mpt_cmd(sc); 469 470 if (!cmd) { 471 ccb_h->status |= CAM_REQUEUE_REQ; 472 xpt_done(ccb); 473 return (0); 474 } 475 if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) { 476 if (ccb_h->flags & CAM_DIR_IN) 477 cmd->flags |= MRSAS_DIR_IN; 478 if (ccb_h->flags & CAM_DIR_OUT) 479 cmd->flags |= MRSAS_DIR_OUT; 480 } else 481 cmd->flags = MRSAS_DIR_NONE; /* no data */ 482 483 /* For FreeBSD 9.2 and higher */ 484 #if (__FreeBSD_version >= 902001) 485 /* 486 * XXX We don't yet support physical addresses here. 487 */ 488 switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) { 489 case CAM_DATA_PADDR: 490 case CAM_DATA_SG_PADDR: 491 device_printf(sc->mrsas_dev, "%s: physical addresses not supported\n", 492 __func__); 493 mrsas_release_mpt_cmd(cmd); 494 ccb_h->status = CAM_REQ_INVALID; 495 ccb_h->status &= ~CAM_SIM_QUEUED; 496 goto done; 497 case CAM_DATA_SG: 498 device_printf(sc->mrsas_dev, "%s: scatter gather is not supported\n", 499 __func__); 500 mrsas_release_mpt_cmd(cmd); 501 ccb_h->status = CAM_REQ_INVALID; 502 goto done; 503 case CAM_DATA_VADDR: 504 if (csio->dxfer_len > (sc->max_num_sge * MRSAS_PAGE_SIZE)) { 505 mrsas_release_mpt_cmd(cmd); 506 ccb_h->status = CAM_REQ_TOO_BIG; 507 goto done; 508 } 509 cmd->length = csio->dxfer_len; 510 if (cmd->length) 511 cmd->data = csio->data_ptr; 512 break; 513 case CAM_DATA_BIO: 514 if (csio->dxfer_len > (sc->max_num_sge * MRSAS_PAGE_SIZE)) { 515 mrsas_release_mpt_cmd(cmd); 516 ccb_h->status = CAM_REQ_TOO_BIG; 517 goto done; 518 } 519 cmd->length = csio->dxfer_len; 520 if (cmd->length) 521 cmd->data = csio->data_ptr; 522 break; 523 default: 524 ccb->ccb_h.status = CAM_REQ_INVALID; 525 goto done; 526 } 527 #else 528 if (!(ccb_h->flags & CAM_DATA_PHYS)) { /* Virtual data address */ 529 if (!(ccb_h->flags & CAM_SCATTER_VALID)) { 530 if (csio->dxfer_len > (sc->max_num_sge * MRSAS_PAGE_SIZE)) { 531 mrsas_release_mpt_cmd(cmd); 532 ccb_h->status = CAM_REQ_TOO_BIG; 533 goto done; 534 } 535 cmd->length = csio->dxfer_len; 536 if (cmd->length) 537 cmd->data = csio->data_ptr; 538 } else { 539 mrsas_release_mpt_cmd(cmd); 540 ccb_h->status = CAM_REQ_INVALID; 541 goto done; 542 } 543 } else { /* Data addresses are physical. */ 544 mrsas_release_mpt_cmd(cmd); 545 ccb_h->status = CAM_REQ_INVALID; 546 ccb_h->status &= ~CAM_SIM_QUEUED; 547 goto done; 548 } 549 #endif 550 /* save ccb ptr */ 551 cmd->ccb_ptr = ccb; 552 553 req_desc = mrsas_get_request_desc(sc, (cmd->index) - 1); 554 if (!req_desc) { 555 device_printf(sc->mrsas_dev, "Cannot get request_descriptor.\n"); 556 return (FAIL); 557 } 558 memset(req_desc, 0, sizeof(MRSAS_REQUEST_DESCRIPTOR_UNION)); 559 cmd->request_desc = req_desc; 560 561 if (ccb_h->flags & CAM_CDB_POINTER) 562 bcopy(csio->cdb_io.cdb_ptr, cmd->io_request->CDB.CDB32, csio->cdb_len); 563 else 564 bcopy(csio->cdb_io.cdb_bytes, cmd->io_request->CDB.CDB32, csio->cdb_len); 565 mtx_lock(&sc->raidmap_lock); 566 567 /* Check for IO type READ-WRITE targeted for Logical Volume */ 568 cmd_type = mrsas_find_io_type(sim, ccb); 569 switch (cmd_type) { 570 case READ_WRITE_LDIO: 571 /* Build READ-WRITE IO for Logical Volume */ 572 if (mrsas_build_ldio_rw(sc, cmd, ccb)) { 573 device_printf(sc->mrsas_dev, "Build RW LDIO failed.\n"); 574 mtx_unlock(&sc->raidmap_lock); 575 return (1); 576 } 577 break; 578 case NON_READ_WRITE_LDIO: 579 /* Build NON READ-WRITE IO for Logical Volume */ 580 if (mrsas_build_ldio_nonrw(sc, cmd, ccb)) { 581 device_printf(sc->mrsas_dev, "Build NON-RW LDIO failed.\n"); 582 mtx_unlock(&sc->raidmap_lock); 583 return (1); 584 } 585 break; 586 case READ_WRITE_SYSPDIO: 587 case NON_READ_WRITE_SYSPDIO: 588 if (sc->secure_jbod_support && 589 (cmd_type == NON_READ_WRITE_SYSPDIO)) { 590 /* Build NON-RW IO for JBOD */ 591 if (mrsas_build_syspdio(sc, cmd, ccb, sim, 0)) { 592 device_printf(sc->mrsas_dev, 593 "Build SYSPDIO failed.\n"); 594 mtx_unlock(&sc->raidmap_lock); 595 return (1); 596 } 597 } else { 598 /* Build RW IO for JBOD */ 599 if (mrsas_build_syspdio(sc, cmd, ccb, sim, 1)) { 600 device_printf(sc->mrsas_dev, 601 "Build SYSPDIO failed.\n"); 602 mtx_unlock(&sc->raidmap_lock); 603 return (1); 604 } 605 } 606 } 607 mtx_unlock(&sc->raidmap_lock); 608 609 if (cmd->flags == MRSAS_DIR_IN) /* from device */ 610 cmd->io_request->Control |= MPI2_SCSIIO_CONTROL_READ; 611 else if (cmd->flags == MRSAS_DIR_OUT) /* to device */ 612 cmd->io_request->Control |= MPI2_SCSIIO_CONTROL_WRITE; 613 614 cmd->io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING; 615 cmd->io_request->SGLOffset0 = offsetof(MRSAS_RAID_SCSI_IO_REQUEST, SGL) / 4; 616 cmd->io_request->SenseBufferLowAddress = cmd->sense_phys_addr; 617 cmd->io_request->SenseBufferLength = MRSAS_SCSI_SENSE_BUFFERSIZE; 618 619 req_desc = cmd->request_desc; 620 req_desc->SCSIIO.SMID = cmd->index; 621 622 /* 623 * Start timer for IO timeout. Default timeout value is 90 second. 624 */ 625 #if (__FreeBSD_version >= 1000510) 626 callout_reset_sbt(&cmd->cm_callout, SBT_1S * 180, 0, 627 mrsas_scsiio_timeout, cmd, 0); 628 #else 629 callout_reset(&cmd->cm_callout, (180000 * hz) / 1000, 630 mrsas_scsiio_timeout, cmd); 631 #endif 632 mrsas_atomic_inc(&sc->fw_outstanding); 633 634 if (mrsas_atomic_read(&sc->fw_outstanding) > sc->io_cmds_highwater) 635 sc->io_cmds_highwater++; 636 637 mrsas_fire_cmd(sc, req_desc->addr.u.low, req_desc->addr.u.high); 638 return (0); 639 640 done: 641 xpt_done(ccb); 642 return (0); 643 } 644 645 /* 646 * mrsas_find_io_type: Determines if IO is read/write or inquiry 647 * input: pointer to CAM Control Block 648 * 649 * This function determines if the IO is read/write or inquiry. It returns a 1 650 * if the IO is read/write and 0 if it is inquiry. 651 */ 652 int 653 mrsas_find_io_type(struct cam_sim *sim, union ccb *ccb) 654 { 655 struct ccb_scsiio *csio = &(ccb->csio); 656 657 switch (csio->cdb_io.cdb_bytes[0]) { 658 case READ_10: 659 case WRITE_10: 660 case READ_12: 661 case WRITE_12: 662 case READ_6: 663 case WRITE_6: 664 case READ_16: 665 case WRITE_16: 666 return (cam_sim_bus(sim) ? 667 READ_WRITE_SYSPDIO : READ_WRITE_LDIO); 668 default: 669 return (cam_sim_bus(sim) ? 670 NON_READ_WRITE_SYSPDIO : NON_READ_WRITE_LDIO); 671 } 672 } 673 674 /* 675 * mrsas_get_mpt_cmd: Get a cmd from free command pool 676 * input: Adapter instance soft state 677 * 678 * This function removes an MPT command from the command free list and 679 * initializes it. 680 */ 681 struct mrsas_mpt_cmd * 682 mrsas_get_mpt_cmd(struct mrsas_softc *sc) 683 { 684 struct mrsas_mpt_cmd *cmd = NULL; 685 686 mtx_lock(&sc->mpt_cmd_pool_lock); 687 if (!TAILQ_EMPTY(&sc->mrsas_mpt_cmd_list_head)) { 688 cmd = TAILQ_FIRST(&sc->mrsas_mpt_cmd_list_head); 689 TAILQ_REMOVE(&sc->mrsas_mpt_cmd_list_head, cmd, next); 690 } else { 691 goto out; 692 } 693 694 memset((uint8_t *)cmd->io_request, 0, MRSAS_MPI2_RAID_DEFAULT_IO_FRAME_SIZE); 695 cmd->data = NULL; 696 cmd->length = 0; 697 cmd->flags = 0; 698 cmd->error_code = 0; 699 cmd->load_balance = 0; 700 cmd->ccb_ptr = NULL; 701 702 out: 703 mtx_unlock(&sc->mpt_cmd_pool_lock); 704 return cmd; 705 } 706 707 /* 708 * mrsas_release_mpt_cmd: Return a cmd to free command pool 709 * input: Command packet for return to free command pool 710 * 711 * This function returns an MPT command to the free command list. 712 */ 713 void 714 mrsas_release_mpt_cmd(struct mrsas_mpt_cmd *cmd) 715 { 716 struct mrsas_softc *sc = cmd->sc; 717 718 mtx_lock(&sc->mpt_cmd_pool_lock); 719 cmd->sync_cmd_idx = (u_int32_t)MRSAS_ULONG_MAX; 720 TAILQ_INSERT_HEAD(&(sc->mrsas_mpt_cmd_list_head), cmd, next); 721 mtx_unlock(&sc->mpt_cmd_pool_lock); 722 723 return; 724 } 725 726 /* 727 * mrsas_get_request_desc: Get request descriptor from array 728 * input: Adapter instance soft state 729 * SMID index 730 * 731 * This function returns a pointer to the request descriptor. 732 */ 733 MRSAS_REQUEST_DESCRIPTOR_UNION * 734 mrsas_get_request_desc(struct mrsas_softc *sc, u_int16_t index) 735 { 736 u_int8_t *p; 737 738 if (index >= sc->max_fw_cmds) { 739 device_printf(sc->mrsas_dev, "Invalid SMID (0x%x)request for desc\n", index); 740 return NULL; 741 } 742 p = sc->req_desc + sizeof(MRSAS_REQUEST_DESCRIPTOR_UNION) * index; 743 744 return (MRSAS_REQUEST_DESCRIPTOR_UNION *) p; 745 } 746 747 /* 748 * mrsas_build_ldio_rw: Builds an LDIO command 749 * input: Adapter instance soft state 750 * Pointer to command packet 751 * Pointer to CCB 752 * 753 * This function builds the LDIO command packet. It returns 0 if the command is 754 * built successfully, otherwise it returns a 1. 755 */ 756 int 757 mrsas_build_ldio_rw(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd, 758 union ccb *ccb) 759 { 760 struct ccb_hdr *ccb_h = &(ccb->ccb_h); 761 struct ccb_scsiio *csio = &(ccb->csio); 762 u_int32_t device_id; 763 MRSAS_RAID_SCSI_IO_REQUEST *io_request; 764 765 device_id = ccb_h->target_id; 766 767 io_request = cmd->io_request; 768 io_request->RaidContext.raid_context.VirtualDiskTgtId = device_id; 769 io_request->RaidContext.raid_context.status = 0; 770 io_request->RaidContext.raid_context.exStatus = 0; 771 772 /* just the cdb len, other flags zero, and ORed-in later for FP */ 773 io_request->IoFlags = csio->cdb_len; 774 775 if (mrsas_setup_io(sc, cmd, ccb, device_id, io_request) != SUCCESS) 776 device_printf(sc->mrsas_dev, "Build ldio or fpio error\n"); 777 778 io_request->DataLength = cmd->length; 779 780 if (mrsas_map_request(sc, cmd, ccb) == SUCCESS) { 781 if (cmd->sge_count > sc->max_num_sge) { 782 device_printf(sc->mrsas_dev, "Error: sge_count (0x%x) exceeds" 783 "max (0x%x) allowed\n", cmd->sge_count, sc->max_num_sge); 784 return (FAIL); 785 } 786 if (sc->is_ventura) 787 io_request->RaidContext.raid_context_g35.numSGE = cmd->sge_count; 788 else { 789 /* 790 * numSGE store lower 8 bit of sge_count. numSGEExt store 791 * higher 8 bit of sge_count 792 */ 793 io_request->RaidContext.raid_context.numSGE = cmd->sge_count; 794 io_request->RaidContext.raid_context.numSGEExt = (uint8_t)(cmd->sge_count >> 8); 795 } 796 797 } else { 798 device_printf(sc->mrsas_dev, "Data map/load failed.\n"); 799 return (FAIL); 800 } 801 return (0); 802 } 803 804 /* stream detection on read and and write IOs */ 805 static void 806 mrsas_stream_detect(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd, 807 struct IO_REQUEST_INFO *io_info) 808 { 809 u_int32_t device_id = io_info->ldTgtId; 810 LD_STREAM_DETECT *current_ld_SD = sc->streamDetectByLD[device_id]; 811 u_int32_t *track_stream = ¤t_ld_SD->mruBitMap; 812 u_int32_t streamNum, shiftedValues, unshiftedValues; 813 u_int32_t indexValueMask, shiftedValuesMask; 814 int i; 815 boolean_t isReadAhead = false; 816 STREAM_DETECT *current_SD; 817 818 /* find possible stream */ 819 for (i = 0; i < MAX_STREAMS_TRACKED; ++i) { 820 streamNum = (*track_stream >> (i * BITS_PER_INDEX_STREAM)) & 821 STREAM_MASK; 822 current_SD = ¤t_ld_SD->streamTrack[streamNum]; 823 /* 824 * if we found a stream, update the raid context and 825 * also update the mruBitMap 826 */ 827 if (current_SD->nextSeqLBA && 828 io_info->ldStartBlock >= current_SD->nextSeqLBA && 829 (io_info->ldStartBlock <= (current_SD->nextSeqLBA+32)) && 830 (current_SD->isRead == io_info->isRead)) { 831 if (io_info->ldStartBlock != current_SD->nextSeqLBA && 832 (!io_info->isRead || !isReadAhead)) { 833 /* 834 * Once the API availible we need to change this. 835 * At this point we are not allowing any gap 836 */ 837 continue; 838 } 839 cmd->io_request->RaidContext.raid_context_g35.streamDetected = TRUE; 840 current_SD->nextSeqLBA = io_info->ldStartBlock + io_info->numBlocks; 841 /* 842 * update the mruBitMap LRU 843 */ 844 shiftedValuesMask = (1 << i * BITS_PER_INDEX_STREAM) - 1 ; 845 shiftedValues = ((*track_stream & shiftedValuesMask) << 846 BITS_PER_INDEX_STREAM); 847 indexValueMask = STREAM_MASK << i * BITS_PER_INDEX_STREAM; 848 unshiftedValues = (*track_stream) & 849 (~(shiftedValuesMask | indexValueMask)); 850 *track_stream = 851 (unshiftedValues | shiftedValues | streamNum); 852 return; 853 } 854 } 855 /* 856 * if we did not find any stream, create a new one from the least recently used 857 */ 858 streamNum = (*track_stream >> 859 ((MAX_STREAMS_TRACKED - 1) * BITS_PER_INDEX_STREAM)) & STREAM_MASK; 860 current_SD = ¤t_ld_SD->streamTrack[streamNum]; 861 current_SD->isRead = io_info->isRead; 862 current_SD->nextSeqLBA = io_info->ldStartBlock + io_info->numBlocks; 863 *track_stream = (((*track_stream & ZERO_LAST_STREAM) << 4) | streamNum); 864 return; 865 } 866 867 868 /* 869 * mrsas_setup_io: Set up data including Fast Path I/O 870 * input: Adapter instance soft state 871 * Pointer to command packet 872 * Pointer to CCB 873 * 874 * This function builds the DCDB inquiry command. It returns 0 if the command 875 * is built successfully, otherwise it returns a 1. 876 */ 877 int 878 mrsas_setup_io(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd, 879 union ccb *ccb, u_int32_t device_id, 880 MRSAS_RAID_SCSI_IO_REQUEST * io_request) 881 { 882 struct ccb_hdr *ccb_h = &(ccb->ccb_h); 883 struct ccb_scsiio *csio = &(ccb->csio); 884 struct IO_REQUEST_INFO io_info; 885 MR_DRV_RAID_MAP_ALL *map_ptr; 886 MR_LD_RAID *raid; 887 u_int8_t fp_possible; 888 u_int32_t start_lba_hi, start_lba_lo, ld_block_size, ld; 889 u_int32_t datalength = 0; 890 891 start_lba_lo = 0; 892 start_lba_hi = 0; 893 fp_possible = 0; 894 895 /* 896 * READ_6 (0x08) or WRITE_6 (0x0A) cdb 897 */ 898 if (csio->cdb_len == 6) { 899 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[4]; 900 start_lba_lo = ((u_int32_t)csio->cdb_io.cdb_bytes[1] << 16) | 901 ((u_int32_t)csio->cdb_io.cdb_bytes[2] << 8) | 902 (u_int32_t)csio->cdb_io.cdb_bytes[3]; 903 start_lba_lo &= 0x1FFFFF; 904 } 905 /* 906 * READ_10 (0x28) or WRITE_6 (0x2A) cdb 907 */ 908 else if (csio->cdb_len == 10) { 909 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[8] | 910 ((u_int32_t)csio->cdb_io.cdb_bytes[7] << 8); 911 start_lba_lo = ((u_int32_t)csio->cdb_io.cdb_bytes[2] << 24) | 912 ((u_int32_t)csio->cdb_io.cdb_bytes[3] << 16) | 913 (u_int32_t)csio->cdb_io.cdb_bytes[4] << 8 | 914 ((u_int32_t)csio->cdb_io.cdb_bytes[5]); 915 } 916 /* 917 * READ_12 (0xA8) or WRITE_12 (0xAA) cdb 918 */ 919 else if (csio->cdb_len == 12) { 920 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[6] << 24 | 921 ((u_int32_t)csio->cdb_io.cdb_bytes[7] << 16) | 922 ((u_int32_t)csio->cdb_io.cdb_bytes[8] << 8) | 923 ((u_int32_t)csio->cdb_io.cdb_bytes[9]); 924 start_lba_lo = ((u_int32_t)csio->cdb_io.cdb_bytes[2] << 24) | 925 ((u_int32_t)csio->cdb_io.cdb_bytes[3] << 16) | 926 (u_int32_t)csio->cdb_io.cdb_bytes[4] << 8 | 927 ((u_int32_t)csio->cdb_io.cdb_bytes[5]); 928 } 929 /* 930 * READ_16 (0x88) or WRITE_16 (0xx8A) cdb 931 */ 932 else if (csio->cdb_len == 16) { 933 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[10] << 24 | 934 ((u_int32_t)csio->cdb_io.cdb_bytes[11] << 16) | 935 ((u_int32_t)csio->cdb_io.cdb_bytes[12] << 8) | 936 ((u_int32_t)csio->cdb_io.cdb_bytes[13]); 937 start_lba_lo = ((u_int32_t)csio->cdb_io.cdb_bytes[6] << 24) | 938 ((u_int32_t)csio->cdb_io.cdb_bytes[7] << 16) | 939 (u_int32_t)csio->cdb_io.cdb_bytes[8] << 8 | 940 ((u_int32_t)csio->cdb_io.cdb_bytes[9]); 941 start_lba_hi = ((u_int32_t)csio->cdb_io.cdb_bytes[2] << 24) | 942 ((u_int32_t)csio->cdb_io.cdb_bytes[3] << 16) | 943 (u_int32_t)csio->cdb_io.cdb_bytes[4] << 8 | 944 ((u_int32_t)csio->cdb_io.cdb_bytes[5]); 945 } 946 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO)); 947 io_info.ldStartBlock = ((u_int64_t)start_lba_hi << 32) | start_lba_lo; 948 io_info.numBlocks = datalength; 949 io_info.ldTgtId = device_id; 950 951 switch (ccb_h->flags & CAM_DIR_MASK) { 952 case CAM_DIR_IN: 953 io_info.isRead = 1; 954 break; 955 case CAM_DIR_OUT: 956 io_info.isRead = 0; 957 break; 958 case CAM_DIR_NONE: 959 default: 960 mrsas_dprint(sc, MRSAS_TRACE, "From %s : DMA Flag is %d \n", __func__, ccb_h->flags & CAM_DIR_MASK); 961 break; 962 } 963 964 map_ptr = sc->ld_drv_map[(sc->map_id & 1)]; 965 ld_block_size = MR_LdBlockSizeGet(device_id, map_ptr); 966 967 ld = MR_TargetIdToLdGet(device_id, map_ptr); 968 if ((ld >= MAX_LOGICAL_DRIVES_EXT) || (!sc->fast_path_io)) { 969 io_request->RaidContext.raid_context.regLockFlags = 0; 970 fp_possible = 0; 971 } else { 972 if (MR_BuildRaidContext(sc, &io_info, &io_request->RaidContext.raid_context, map_ptr)) 973 fp_possible = io_info.fpOkForIo; 974 } 975 976 raid = MR_LdRaidGet(ld, map_ptr); 977 /* Store the TM capability value in cmd */ 978 cmd->tmCapable = raid->capability.tmCapable; 979 980 cmd->request_desc->SCSIIO.MSIxIndex = 981 sc->msix_vectors ? smp_processor_id() % sc->msix_vectors : 0; 982 983 if (sc->is_ventura && sc->streamDetectByLD) { 984 mtx_lock(&sc->stream_lock); 985 mrsas_stream_detect(sc, cmd, &io_info); 986 mtx_unlock(&sc->stream_lock); 987 /* In ventura if stream detected for a read and it is read ahead capable make this IO as LDIO */ 988 if (io_request->RaidContext.raid_context_g35.streamDetected && 989 io_info.isRead && io_info.raCapable) 990 fp_possible = FALSE; 991 } 992 993 if (fp_possible) { 994 mrsas_set_pd_lba(io_request, csio->cdb_len, &io_info, ccb, map_ptr, 995 start_lba_lo, ld_block_size); 996 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST; 997 cmd->request_desc->SCSIIO.RequestFlags = 998 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO << 999 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 1000 if (sc->mrsas_gen3_ctrl) { 1001 if (io_request->RaidContext.raid_context.regLockFlags == REGION_TYPE_UNUSED) 1002 cmd->request_desc->SCSIIO.RequestFlags = 1003 (MRSAS_REQ_DESCRIPT_FLAGS_NO_LOCK << 1004 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 1005 io_request->RaidContext.raid_context.Type = MPI2_TYPE_CUDA; 1006 io_request->RaidContext.raid_context.nseg = 0x1; 1007 io_request->IoFlags |= MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH; 1008 io_request->RaidContext.raid_context.regLockFlags |= 1009 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA | 1010 MR_RL_FLAGS_SEQ_NUM_ENABLE); 1011 } else if (sc->is_ventura) { 1012 io_request->RaidContext.raid_context_g35.Type = MPI2_TYPE_CUDA; 1013 io_request->RaidContext.raid_context_g35.nseg = 0x1; 1014 io_request->RaidContext.raid_context_g35.routingFlags.bits.sqn = 1; 1015 io_request->IoFlags |= MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH; 1016 if (io_request->RaidContext.raid_context_g35.routingFlags.bits.sld) { 1017 io_request->RaidContext.raid_context_g35.RAIDFlags = 1018 (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS 1019 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT); 1020 } 1021 } 1022 if ((sc->load_balance_info[device_id].loadBalanceFlag) && 1023 (io_info.isRead)) { 1024 io_info.devHandle = 1025 mrsas_get_updated_dev_handle(sc, 1026 &sc->load_balance_info[device_id], &io_info); 1027 cmd->load_balance = MRSAS_LOAD_BALANCE_FLAG; 1028 cmd->pd_r1_lb = io_info.pd_after_lb; 1029 if (sc->is_ventura) 1030 io_request->RaidContext.raid_context_g35.spanArm = io_info.span_arm; 1031 else 1032 io_request->RaidContext.raid_context.spanArm = io_info.span_arm; 1033 } else 1034 cmd->load_balance = 0; 1035 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle; 1036 io_request->DevHandle = io_info.devHandle; 1037 } else { 1038 /* Not FP IO */ 1039 io_request->RaidContext.raid_context.timeoutValue = map_ptr->raidMap.fpPdIoTimeoutSec; 1040 cmd->request_desc->SCSIIO.RequestFlags = 1041 (MRSAS_REQ_DESCRIPT_FLAGS_LD_IO << 1042 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 1043 if (sc->mrsas_gen3_ctrl) { 1044 if (io_request->RaidContext.raid_context.regLockFlags == REGION_TYPE_UNUSED) 1045 cmd->request_desc->SCSIIO.RequestFlags = 1046 (MRSAS_REQ_DESCRIPT_FLAGS_NO_LOCK << 1047 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 1048 io_request->RaidContext.raid_context.Type = MPI2_TYPE_CUDA; 1049 io_request->RaidContext.raid_context.regLockFlags |= 1050 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 | 1051 MR_RL_FLAGS_SEQ_NUM_ENABLE); 1052 io_request->RaidContext.raid_context.nseg = 0x1; 1053 } else if (sc->is_ventura) { 1054 io_request->RaidContext.raid_context_g35.Type = MPI2_TYPE_CUDA; 1055 io_request->RaidContext.raid_context_g35.routingFlags.bits.sqn = 1; 1056 io_request->RaidContext.raid_context_g35.nseg = 0x1; 1057 } 1058 io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST; 1059 io_request->DevHandle = device_id; 1060 } 1061 return (0); 1062 } 1063 1064 /* 1065 * mrsas_build_ldio_nonrw: Builds an LDIO command 1066 * input: Adapter instance soft state 1067 * Pointer to command packet 1068 * Pointer to CCB 1069 * 1070 * This function builds the LDIO command packet. It returns 0 if the command is 1071 * built successfully, otherwise it returns a 1. 1072 */ 1073 int 1074 mrsas_build_ldio_nonrw(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd, 1075 union ccb *ccb) 1076 { 1077 struct ccb_hdr *ccb_h = &(ccb->ccb_h); 1078 u_int32_t device_id, ld; 1079 MR_DRV_RAID_MAP_ALL *map_ptr; 1080 MR_LD_RAID *raid; 1081 MRSAS_RAID_SCSI_IO_REQUEST *io_request; 1082 1083 io_request = cmd->io_request; 1084 device_id = ccb_h->target_id; 1085 1086 map_ptr = sc->ld_drv_map[(sc->map_id & 1)]; 1087 ld = MR_TargetIdToLdGet(device_id, map_ptr); 1088 raid = MR_LdRaidGet(ld, map_ptr); 1089 /* Store the TM capability value in cmd */ 1090 cmd->tmCapable = raid->capability.tmCapable; 1091 1092 /* FW path for LD Non-RW (SCSI management commands) */ 1093 io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST; 1094 io_request->DevHandle = device_id; 1095 cmd->request_desc->SCSIIO.RequestFlags = 1096 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO << 1097 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 1098 1099 io_request->RaidContext.raid_context.VirtualDiskTgtId = device_id; 1100 io_request->LUN[1] = ccb_h->target_lun & 0xF; 1101 io_request->DataLength = cmd->length; 1102 1103 if (mrsas_map_request(sc, cmd, ccb) == SUCCESS) { 1104 if (cmd->sge_count > sc->max_num_sge) { 1105 device_printf(sc->mrsas_dev, "Error: sge_count (0x%x) exceeds" 1106 "max (0x%x) allowed\n", cmd->sge_count, sc->max_num_sge); 1107 return (1); 1108 } 1109 if (sc->is_ventura) 1110 io_request->RaidContext.raid_context_g35.numSGE = cmd->sge_count; 1111 else { 1112 /* 1113 * numSGE store lower 8 bit of sge_count. numSGEExt store 1114 * higher 8 bit of sge_count 1115 */ 1116 io_request->RaidContext.raid_context.numSGE = cmd->sge_count; 1117 io_request->RaidContext.raid_context.numSGEExt = (uint8_t)(cmd->sge_count >> 8); 1118 } 1119 } else { 1120 device_printf(sc->mrsas_dev, "Data map/load failed.\n"); 1121 return (1); 1122 } 1123 return (0); 1124 } 1125 1126 /* 1127 * mrsas_build_syspdio: Builds an DCDB command 1128 * input: Adapter instance soft state 1129 * Pointer to command packet 1130 * Pointer to CCB 1131 * 1132 * This function builds the DCDB inquiry command. It returns 0 if the command 1133 * is built successfully, otherwise it returns a 1. 1134 */ 1135 int 1136 mrsas_build_syspdio(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd, 1137 union ccb *ccb, struct cam_sim *sim, u_int8_t fp_possible) 1138 { 1139 struct ccb_hdr *ccb_h = &(ccb->ccb_h); 1140 u_int32_t device_id; 1141 MR_DRV_RAID_MAP_ALL *local_map_ptr; 1142 MRSAS_RAID_SCSI_IO_REQUEST *io_request; 1143 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync; 1144 1145 io_request = cmd->io_request; 1146 device_id = ccb_h->target_id; 1147 local_map_ptr = sc->ld_drv_map[(sc->map_id & 1)]; 1148 io_request->RaidContext.raid_context.RAIDFlags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD 1149 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT; 1150 io_request->RaidContext.raid_context.regLockFlags = 0; 1151 io_request->RaidContext.raid_context.regLockRowLBA = 0; 1152 io_request->RaidContext.raid_context.regLockLength = 0; 1153 1154 /* If FW supports PD sequence number */ 1155 if (sc->use_seqnum_jbod_fp && 1156 sc->pd_list[device_id].driveType == 0x00) { 1157 //printf("Using Drv seq num\n"); 1158 pd_sync = (void *)sc->jbodmap_mem[(sc->pd_seq_map_id - 1) & 1]; 1159 cmd->tmCapable = pd_sync->seq[device_id].capability.tmCapable; 1160 /* More than 256 PD/JBOD support for Ventura */ 1161 if (sc->support_morethan256jbod) 1162 io_request->RaidContext.raid_context.VirtualDiskTgtId = 1163 pd_sync->seq[device_id].pdTargetId; 1164 else 1165 io_request->RaidContext.raid_context.VirtualDiskTgtId = 1166 device_id + 255; 1167 io_request->RaidContext.raid_context.configSeqNum = pd_sync->seq[device_id].seqNum; 1168 io_request->DevHandle = pd_sync->seq[device_id].devHandle; 1169 if (sc->is_ventura) 1170 io_request->RaidContext.raid_context_g35.routingFlags.bits.sqn = 1; 1171 else 1172 io_request->RaidContext.raid_context.regLockFlags |= 1173 (MR_RL_FLAGS_SEQ_NUM_ENABLE | MR_RL_FLAGS_GRANT_DESTINATION_CUDA); 1174 io_request->RaidContext.raid_context.Type = MPI2_TYPE_CUDA; 1175 io_request->RaidContext.raid_context.nseg = 0x1; 1176 } else if (sc->fast_path_io) { 1177 //printf("Using LD RAID map\n"); 1178 io_request->RaidContext.raid_context.VirtualDiskTgtId = device_id; 1179 io_request->RaidContext.raid_context.configSeqNum = 0; 1180 local_map_ptr = sc->ld_drv_map[(sc->map_id & 1)]; 1181 io_request->DevHandle = 1182 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl; 1183 } else { 1184 //printf("Using FW PATH\n"); 1185 /* Want to send all IO via FW path */ 1186 io_request->RaidContext.raid_context.VirtualDiskTgtId = device_id; 1187 io_request->RaidContext.raid_context.configSeqNum = 0; 1188 io_request->DevHandle = 0xFFFF; 1189 } 1190 1191 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle; 1192 cmd->request_desc->SCSIIO.MSIxIndex = 1193 sc->msix_vectors ? smp_processor_id() % sc->msix_vectors : 0; 1194 1195 if (!fp_possible) { 1196 /* system pd firmware path */ 1197 io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST; 1198 cmd->request_desc->SCSIIO.RequestFlags = 1199 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO << 1200 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 1201 io_request->RaidContext.raid_context.timeoutValue = 1202 local_map_ptr->raidMap.fpPdIoTimeoutSec; 1203 io_request->RaidContext.raid_context.VirtualDiskTgtId = device_id; 1204 } else { 1205 /* system pd fast path */ 1206 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST; 1207 io_request->RaidContext.raid_context.timeoutValue = local_map_ptr->raidMap.fpPdIoTimeoutSec; 1208 1209 /* 1210 * NOTE - For system pd RW cmds only IoFlags will be FAST_PATH 1211 * Because the NON RW cmds will now go via FW Queue 1212 * and not the Exception queue 1213 */ 1214 if (sc->mrsas_gen3_ctrl || sc->is_ventura) 1215 io_request->IoFlags |= MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH; 1216 1217 cmd->request_desc->SCSIIO.RequestFlags = 1218 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO << 1219 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 1220 } 1221 1222 io_request->LUN[1] = ccb_h->target_lun & 0xF; 1223 io_request->DataLength = cmd->length; 1224 1225 if (mrsas_map_request(sc, cmd, ccb) == SUCCESS) { 1226 if (cmd->sge_count > sc->max_num_sge) { 1227 device_printf(sc->mrsas_dev, "Error: sge_count (0x%x) exceeds" 1228 "max (0x%x) allowed\n", cmd->sge_count, sc->max_num_sge); 1229 return (1); 1230 } 1231 if (sc->is_ventura) 1232 io_request->RaidContext.raid_context_g35.numSGE = cmd->sge_count; 1233 else { 1234 /* 1235 * numSGE store lower 8 bit of sge_count. numSGEExt store 1236 * higher 8 bit of sge_count 1237 */ 1238 io_request->RaidContext.raid_context.numSGE = cmd->sge_count; 1239 io_request->RaidContext.raid_context.numSGEExt = (uint8_t)(cmd->sge_count >> 8); 1240 } 1241 } else { 1242 device_printf(sc->mrsas_dev, "Data map/load failed.\n"); 1243 return (1); 1244 } 1245 return (0); 1246 } 1247 1248 /* 1249 * mrsas_map_request: Map and load data 1250 * input: Adapter instance soft state 1251 * Pointer to command packet 1252 * 1253 * For data from OS, map and load the data buffer into bus space. The SG list 1254 * is built in the callback. If the bus dmamap load is not successful, 1255 * cmd->error_code will contain the error code and a 1 is returned. 1256 */ 1257 int 1258 mrsas_map_request(struct mrsas_softc *sc, 1259 struct mrsas_mpt_cmd *cmd, union ccb *ccb) 1260 { 1261 u_int32_t retcode = 0; 1262 struct cam_sim *sim; 1263 1264 sim = xpt_path_sim(cmd->ccb_ptr->ccb_h.path); 1265 1266 if (cmd->data != NULL) { 1267 /* Map data buffer into bus space */ 1268 mtx_lock(&sc->io_lock); 1269 #if (__FreeBSD_version >= 902001) 1270 retcode = bus_dmamap_load_ccb(sc->data_tag, cmd->data_dmamap, ccb, 1271 mrsas_data_load_cb, cmd, 0); 1272 #else 1273 retcode = bus_dmamap_load(sc->data_tag, cmd->data_dmamap, cmd->data, 1274 cmd->length, mrsas_data_load_cb, cmd, BUS_DMA_NOWAIT); 1275 #endif 1276 mtx_unlock(&sc->io_lock); 1277 if (retcode) 1278 device_printf(sc->mrsas_dev, "bus_dmamap_load(): retcode = %d\n", retcode); 1279 if (retcode == EINPROGRESS) { 1280 device_printf(sc->mrsas_dev, "request load in progress\n"); 1281 mrsas_freeze_simq(cmd, sim); 1282 } 1283 } 1284 if (cmd->error_code) 1285 return (1); 1286 return (retcode); 1287 } 1288 1289 /* 1290 * mrsas_unmap_request: Unmap and unload data 1291 * input: Adapter instance soft state 1292 * Pointer to command packet 1293 * 1294 * This function unmaps and unloads data from OS. 1295 */ 1296 void 1297 mrsas_unmap_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd) 1298 { 1299 if (cmd->data != NULL) { 1300 if (cmd->flags & MRSAS_DIR_IN) 1301 bus_dmamap_sync(sc->data_tag, cmd->data_dmamap, BUS_DMASYNC_POSTREAD); 1302 if (cmd->flags & MRSAS_DIR_OUT) 1303 bus_dmamap_sync(sc->data_tag, cmd->data_dmamap, BUS_DMASYNC_POSTWRITE); 1304 mtx_lock(&sc->io_lock); 1305 bus_dmamap_unload(sc->data_tag, cmd->data_dmamap); 1306 mtx_unlock(&sc->io_lock); 1307 } 1308 } 1309 1310 /* 1311 * mrsas_data_load_cb: Callback entry point 1312 * input: Pointer to command packet as argument 1313 * Pointer to segment 1314 * Number of segments Error 1315 * 1316 * This is the callback function of the bus dma map load. It builds the SG 1317 * list. 1318 */ 1319 static void 1320 mrsas_data_load_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 1321 { 1322 struct mrsas_mpt_cmd *cmd = (struct mrsas_mpt_cmd *)arg; 1323 struct mrsas_softc *sc = cmd->sc; 1324 MRSAS_RAID_SCSI_IO_REQUEST *io_request; 1325 pMpi25IeeeSgeChain64_t sgl_ptr; 1326 int i = 0, sg_processed = 0; 1327 1328 if (error) { 1329 cmd->error_code = error; 1330 device_printf(sc->mrsas_dev, "mrsas_data_load_cb: error=%d\n", error); 1331 if (error == EFBIG) { 1332 cmd->ccb_ptr->ccb_h.status = CAM_REQ_TOO_BIG; 1333 return; 1334 } 1335 } 1336 if (cmd->flags & MRSAS_DIR_IN) 1337 bus_dmamap_sync(cmd->sc->data_tag, cmd->data_dmamap, 1338 BUS_DMASYNC_PREREAD); 1339 if (cmd->flags & MRSAS_DIR_OUT) 1340 bus_dmamap_sync(cmd->sc->data_tag, cmd->data_dmamap, 1341 BUS_DMASYNC_PREWRITE); 1342 if (nseg > sc->max_num_sge) { 1343 device_printf(sc->mrsas_dev, "SGE count is too large or 0.\n"); 1344 return; 1345 } 1346 io_request = cmd->io_request; 1347 sgl_ptr = (pMpi25IeeeSgeChain64_t)&io_request->SGL; 1348 1349 if (sc->mrsas_gen3_ctrl || sc->is_ventura) { 1350 pMpi25IeeeSgeChain64_t sgl_ptr_end = sgl_ptr; 1351 1352 sgl_ptr_end += sc->max_sge_in_main_msg - 1; 1353 sgl_ptr_end->Flags = 0; 1354 } 1355 if (nseg != 0) { 1356 for (i = 0; i < nseg; i++) { 1357 sgl_ptr->Address = segs[i].ds_addr; 1358 sgl_ptr->Length = segs[i].ds_len; 1359 sgl_ptr->Flags = 0; 1360 if (sc->mrsas_gen3_ctrl || sc->is_ventura) { 1361 if (i == nseg - 1) 1362 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST; 1363 } 1364 sgl_ptr++; 1365 sg_processed = i + 1; 1366 if ((sg_processed == (sc->max_sge_in_main_msg - 1)) && 1367 (nseg > sc->max_sge_in_main_msg)) { 1368 pMpi25IeeeSgeChain64_t sg_chain; 1369 1370 if (sc->mrsas_gen3_ctrl || sc->is_ventura) { 1371 if ((cmd->io_request->IoFlags & MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) 1372 != MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) 1373 cmd->io_request->ChainOffset = sc->chain_offset_io_request; 1374 else 1375 cmd->io_request->ChainOffset = 0; 1376 } else 1377 cmd->io_request->ChainOffset = sc->chain_offset_io_request; 1378 sg_chain = sgl_ptr; 1379 if (sc->mrsas_gen3_ctrl || sc->is_ventura) 1380 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT; 1381 else 1382 sg_chain->Flags = (IEEE_SGE_FLAGS_CHAIN_ELEMENT | MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR); 1383 sg_chain->Length = (sizeof(MPI2_SGE_IO_UNION) * (nseg - sg_processed)); 1384 sg_chain->Address = cmd->chain_frame_phys_addr; 1385 sgl_ptr = (pMpi25IeeeSgeChain64_t)cmd->chain_frame; 1386 } 1387 } 1388 } 1389 cmd->sge_count = nseg; 1390 } 1391 1392 /* 1393 * mrsas_freeze_simq: Freeze SIM queue 1394 * input: Pointer to command packet 1395 * Pointer to SIM 1396 * 1397 * This function freezes the sim queue. 1398 */ 1399 static void 1400 mrsas_freeze_simq(struct mrsas_mpt_cmd *cmd, struct cam_sim *sim) 1401 { 1402 union ccb *ccb = (union ccb *)(cmd->ccb_ptr); 1403 1404 xpt_freeze_simq(sim, 1); 1405 ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 1406 ccb->ccb_h.status |= CAM_REQUEUE_REQ; 1407 } 1408 1409 void 1410 mrsas_xpt_freeze(struct mrsas_softc *sc) 1411 { 1412 xpt_freeze_simq(sc->sim_0, 1); 1413 xpt_freeze_simq(sc->sim_1, 1); 1414 } 1415 1416 void 1417 mrsas_xpt_release(struct mrsas_softc *sc) 1418 { 1419 xpt_release_simq(sc->sim_0, 1); 1420 xpt_release_simq(sc->sim_1, 1); 1421 } 1422 1423 /* 1424 * mrsas_cmd_done: Perform remaining command completion 1425 * input: Adapter instance soft state Pointer to command packet 1426 * 1427 * This function calls ummap request and releases the MPT command. 1428 */ 1429 void 1430 mrsas_cmd_done(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd) 1431 { 1432 mrsas_unmap_request(sc, cmd); 1433 1434 mtx_lock(&sc->sim_lock); 1435 callout_stop(&cmd->cm_callout); 1436 xpt_done(cmd->ccb_ptr); 1437 cmd->ccb_ptr = NULL; 1438 mtx_unlock(&sc->sim_lock); 1439 mrsas_release_mpt_cmd(cmd); 1440 } 1441 1442 /* 1443 * mrsas_cam_poll: Polling entry point 1444 * input: Pointer to SIM 1445 * 1446 * This is currently a stub function. 1447 */ 1448 static void 1449 mrsas_cam_poll(struct cam_sim *sim) 1450 { 1451 int i; 1452 struct mrsas_softc *sc = (struct mrsas_softc *)cam_sim_softc(sim); 1453 1454 if (sc->msix_vectors != 0){ 1455 for (i=0; i<sc->msix_vectors; i++){ 1456 mrsas_complete_cmd(sc, i); 1457 } 1458 } else { 1459 mrsas_complete_cmd(sc, 0); 1460 } 1461 } 1462 1463 /* 1464 * mrsas_bus_scan: Perform bus scan 1465 * input: Adapter instance soft state 1466 * 1467 * This mrsas_bus_scan function is needed for FreeBSD 7.x. Also, it should not 1468 * be called in FreeBSD 8.x and later versions, where the bus scan is 1469 * automatic. 1470 */ 1471 int 1472 mrsas_bus_scan(struct mrsas_softc *sc) 1473 { 1474 union ccb *ccb_0; 1475 union ccb *ccb_1; 1476 1477 if ((ccb_0 = xpt_alloc_ccb()) == NULL) { 1478 return (ENOMEM); 1479 } 1480 if ((ccb_1 = xpt_alloc_ccb()) == NULL) { 1481 xpt_free_ccb(ccb_0); 1482 return (ENOMEM); 1483 } 1484 mtx_lock(&sc->sim_lock); 1485 if (xpt_create_path(&ccb_0->ccb_h.path, xpt_periph, cam_sim_path(sc->sim_0), 1486 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 1487 xpt_free_ccb(ccb_0); 1488 xpt_free_ccb(ccb_1); 1489 mtx_unlock(&sc->sim_lock); 1490 return (EIO); 1491 } 1492 if (xpt_create_path(&ccb_1->ccb_h.path, xpt_periph, cam_sim_path(sc->sim_1), 1493 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 1494 xpt_free_ccb(ccb_0); 1495 xpt_free_ccb(ccb_1); 1496 mtx_unlock(&sc->sim_lock); 1497 return (EIO); 1498 } 1499 mtx_unlock(&sc->sim_lock); 1500 xpt_rescan(ccb_0); 1501 xpt_rescan(ccb_1); 1502 1503 return (0); 1504 } 1505 1506 /* 1507 * mrsas_bus_scan_sim: Perform bus scan per SIM 1508 * input: adapter instance soft state 1509 * 1510 * This function will be called from Event handler on LD creation/deletion, 1511 * JBOD on/off. 1512 */ 1513 int 1514 mrsas_bus_scan_sim(struct mrsas_softc *sc, struct cam_sim *sim) 1515 { 1516 union ccb *ccb; 1517 1518 if ((ccb = xpt_alloc_ccb()) == NULL) { 1519 return (ENOMEM); 1520 } 1521 mtx_lock(&sc->sim_lock); 1522 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(sim), 1523 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 1524 xpt_free_ccb(ccb); 1525 mtx_unlock(&sc->sim_lock); 1526 return (EIO); 1527 } 1528 mtx_unlock(&sc->sim_lock); 1529 xpt_rescan(ccb); 1530 1531 return (0); 1532 } 1533 1534 /* 1535 * mrsas_track_scsiio: Track IOs for a given target in the mpt_cmd_list 1536 * input: Adapter instance soft state 1537 * Target ID of target 1538 * Bus ID of the target 1539 * 1540 * This function checks for any pending IO in the whole mpt_cmd_list pool 1541 * with the bus_id and target_id passed in arguments. If some IO is found 1542 * that means target reset is not successfully completed. 1543 * 1544 * Returns FAIL if IOs pending to the target device, else return SUCCESS 1545 */ 1546 static int 1547 mrsas_track_scsiio(struct mrsas_softc *sc, target_id_t tgt_id, u_int32_t bus_id) 1548 { 1549 int i; 1550 struct mrsas_mpt_cmd *mpt_cmd = NULL; 1551 1552 for (i = 0 ; i < sc->max_fw_cmds; i++) { 1553 mpt_cmd = sc->mpt_cmd_list[i]; 1554 1555 /* 1556 * Check if the target_id and bus_id is same as the timeout IO 1557 */ 1558 if (mpt_cmd->ccb_ptr) { 1559 /* bus_id = 1 denotes a VD */ 1560 if (bus_id == 1) 1561 tgt_id = (mpt_cmd->ccb_ptr->ccb_h.target_id - (MRSAS_MAX_PD - 1)); 1562 1563 if (mpt_cmd->ccb_ptr->cpi.bus_id == bus_id && 1564 mpt_cmd->ccb_ptr->ccb_h.target_id == tgt_id) { 1565 device_printf(sc->mrsas_dev, 1566 "IO commands pending to target id %d\n", tgt_id); 1567 return FAIL; 1568 } 1569 } 1570 } 1571 1572 return SUCCESS; 1573 } 1574 1575 #if TM_DEBUG 1576 /* 1577 * mrsas_tm_response_code: Prints TM response code received from FW 1578 * input: Adapter instance soft state 1579 * MPI reply returned from firmware 1580 * 1581 * Returns nothing. 1582 */ 1583 static void 1584 mrsas_tm_response_code(struct mrsas_softc *sc, 1585 MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply) 1586 { 1587 char *desc; 1588 1589 switch (mpi_reply->ResponseCode) { 1590 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE: 1591 desc = "task management request completed"; 1592 break; 1593 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME: 1594 desc = "invalid frame"; 1595 break; 1596 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED: 1597 desc = "task management request not supported"; 1598 break; 1599 case MPI2_SCSITASKMGMT_RSP_TM_FAILED: 1600 desc = "task management request failed"; 1601 break; 1602 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED: 1603 desc = "task management request succeeded"; 1604 break; 1605 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN: 1606 desc = "invalid lun"; 1607 break; 1608 case 0xA: 1609 desc = "overlapped tag attempted"; 1610 break; 1611 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC: 1612 desc = "task queued, however not sent to target"; 1613 break; 1614 default: 1615 desc = "unknown"; 1616 break; 1617 } 1618 device_printf(sc->mrsas_dev, "response_code(%01x): %s\n", 1619 mpi_reply->ResponseCode, desc); 1620 device_printf(sc->mrsas_dev, 1621 "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo\n" 1622 "0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n", 1623 mpi_reply->TerminationCount, mpi_reply->DevHandle, 1624 mpi_reply->Function, mpi_reply->TaskType, 1625 mpi_reply->IOCStatus, mpi_reply->IOCLogInfo); 1626 } 1627 #endif 1628 1629 /* 1630 * mrsas_issue_tm: Fires the TM command to FW and waits for completion 1631 * input: Adapter instance soft state 1632 * reqest descriptor compiled by mrsas_reset_targets 1633 * 1634 * Returns FAIL if TM command TIMEDOUT from FW else SUCCESS. 1635 */ 1636 static int 1637 mrsas_issue_tm(struct mrsas_softc *sc, 1638 MRSAS_REQUEST_DESCRIPTOR_UNION *req_desc) 1639 { 1640 int sleep_stat; 1641 1642 mrsas_fire_cmd(sc, req_desc->addr.u.low, req_desc->addr.u.high); 1643 sleep_stat = msleep(&sc->ocr_chan, &sc->sim_lock, PRIBIO, "tm_sleep", 50*hz); 1644 1645 if (sleep_stat == EWOULDBLOCK) { 1646 device_printf(sc->mrsas_dev, "tm cmd TIMEDOUT\n"); 1647 return FAIL; 1648 } 1649 1650 return SUCCESS; 1651 } 1652 1653 /* 1654 * mrsas_reset_targets : Gathers info to fire a target reset command 1655 * input: Adapter instance soft state 1656 * 1657 * This function compiles data for a target reset command to be fired to the FW 1658 * and then traverse the target_reset_pool to see targets with TIMEDOUT IOs. 1659 * 1660 * Returns SUCCESS or FAIL 1661 */ 1662 int mrsas_reset_targets(struct mrsas_softc *sc) 1663 { 1664 struct mrsas_mpt_cmd *tm_mpt_cmd = NULL; 1665 struct mrsas_mpt_cmd *tgt_mpt_cmd = NULL; 1666 MR_TASK_MANAGE_REQUEST *mr_request; 1667 MPI2_SCSI_TASK_MANAGE_REQUEST *tm_mpi_request; 1668 MRSAS_REQUEST_DESCRIPTOR_UNION *req_desc; 1669 int retCode = FAIL, count, i, outstanding; 1670 u_int32_t MSIxIndex, bus_id; 1671 target_id_t tgt_id; 1672 #if TM_DEBUG 1673 MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply; 1674 #endif 1675 1676 outstanding = mrsas_atomic_read(&sc->fw_outstanding); 1677 1678 if (!outstanding) { 1679 device_printf(sc->mrsas_dev, "NO IOs pending...\n"); 1680 mrsas_atomic_set(&sc->target_reset_outstanding, 0); 1681 retCode = SUCCESS; 1682 goto return_status; 1683 } else if (sc->adprecovery != MRSAS_HBA_OPERATIONAL) { 1684 device_printf(sc->mrsas_dev, "Controller is not operational\n"); 1685 goto return_status; 1686 } else { 1687 /* Some more error checks will be added in future */ 1688 } 1689 1690 /* Get an mpt frame and an index to fire the TM cmd */ 1691 tm_mpt_cmd = mrsas_get_mpt_cmd(sc); 1692 if (!tm_mpt_cmd) { 1693 retCode = FAIL; 1694 goto return_status; 1695 } 1696 1697 req_desc = mrsas_get_request_desc(sc, (tm_mpt_cmd->index) - 1); 1698 if (!req_desc) { 1699 device_printf(sc->mrsas_dev, "Cannot get request_descriptor for tm.\n"); 1700 retCode = FAIL; 1701 goto release_mpt; 1702 } 1703 memset(req_desc, 0, sizeof(MRSAS_REQUEST_DESCRIPTOR_UNION)); 1704 1705 req_desc->HighPriority.SMID = tm_mpt_cmd->index; 1706 req_desc->HighPriority.RequestFlags = 1707 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY << 1708 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 1709 req_desc->HighPriority.MSIxIndex = 0; 1710 req_desc->HighPriority.LMID = 0; 1711 req_desc->HighPriority.Reserved1 = 0; 1712 tm_mpt_cmd->request_desc = req_desc; 1713 1714 mr_request = (MR_TASK_MANAGE_REQUEST *) tm_mpt_cmd->io_request; 1715 memset(mr_request, 0, sizeof(MR_TASK_MANAGE_REQUEST)); 1716 1717 tm_mpi_request = (MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest; 1718 tm_mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT; 1719 tm_mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET; 1720 tm_mpi_request->TaskMID = 0; /* smid task */ 1721 tm_mpi_request->LUN[1] = 0; 1722 1723 /* Traverse the tm_mpt pool to get valid entries */ 1724 for (i = 0 ; i < MRSAS_MAX_TM_TARGETS; i++) { 1725 if(!sc->target_reset_pool[i]) { 1726 continue; 1727 } else { 1728 tgt_mpt_cmd = sc->target_reset_pool[i]; 1729 } 1730 1731 tgt_id = i; 1732 1733 /* See if the target is tm capable or NOT */ 1734 if (!tgt_mpt_cmd->tmCapable) { 1735 device_printf(sc->mrsas_dev, "Task management NOT SUPPORTED for " 1736 "CAM target:%d\n", tgt_id); 1737 1738 retCode = FAIL; 1739 goto release_mpt; 1740 } 1741 1742 tm_mpi_request->DevHandle = tgt_mpt_cmd->io_request->DevHandle; 1743 1744 if (i < (MRSAS_MAX_PD - 1)) { 1745 mr_request->uTmReqReply.tmReqFlags.isTMForPD = 1; 1746 bus_id = 0; 1747 } else { 1748 mr_request->uTmReqReply.tmReqFlags.isTMForLD = 1; 1749 bus_id = 1; 1750 } 1751 1752 device_printf(sc->mrsas_dev, "TM will be fired for " 1753 "CAM target:%d and bus_id %d\n", tgt_id, bus_id); 1754 1755 sc->ocr_chan = (void *)&tm_mpt_cmd; 1756 retCode = mrsas_issue_tm(sc, req_desc); 1757 if (retCode == FAIL) 1758 goto release_mpt; 1759 1760 #if TM_DEBUG 1761 mpi_reply = 1762 (MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->uTmReqReply.TMReply; 1763 mrsas_tm_response_code(sc, mpi_reply); 1764 #endif 1765 mrsas_atomic_dec(&sc->target_reset_outstanding); 1766 sc->target_reset_pool[i] = NULL; 1767 1768 /* Check for pending cmds in the mpt_cmd_pool with the tgt_id */ 1769 mrsas_disable_intr(sc); 1770 /* Wait for 1 second to complete parallel ISR calling same 1771 * mrsas_complete_cmd() 1772 */ 1773 msleep(&sc->ocr_chan, &sc->sim_lock, PRIBIO, "mrsas_reset_wakeup", 1774 1 * hz); 1775 count = sc->msix_vectors > 0 ? sc->msix_vectors : 1; 1776 mtx_unlock(&sc->sim_lock); 1777 for (MSIxIndex = 0; MSIxIndex < count; MSIxIndex++) 1778 mrsas_complete_cmd(sc, MSIxIndex); 1779 mtx_lock(&sc->sim_lock); 1780 retCode = mrsas_track_scsiio(sc, tgt_id, bus_id); 1781 mrsas_enable_intr(sc); 1782 1783 if (retCode == FAIL) 1784 goto release_mpt; 1785 } 1786 1787 device_printf(sc->mrsas_dev, "Number of targets outstanding " 1788 "after reset: %d\n", mrsas_atomic_read(&sc->target_reset_outstanding)); 1789 1790 release_mpt: 1791 mrsas_release_mpt_cmd(tm_mpt_cmd); 1792 return_status: 1793 device_printf(sc->mrsas_dev, "target reset %s!!\n", 1794 (retCode == SUCCESS) ? "SUCCESS" : "FAIL"); 1795 1796 return retCode; 1797 } 1798 1799