1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2009 Yahoo! Inc. 5 * Copyright (c) 2011-2015 LSI Corp. 6 * Copyright (c) 2013-2015 Avago Technologies 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * Avago Technologies (LSI) MPT-Fusion Host Adapter FreeBSD 31 * 32 * $FreeBSD$ 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 /* Communications core for Avago Technologies (LSI) MPT2 */ 39 40 /* TODO Move headers to mpsvar */ 41 #include <sys/types.h> 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/selinfo.h> 46 #include <sys/module.h> 47 #include <sys/bus.h> 48 #include <sys/conf.h> 49 #include <sys/bio.h> 50 #include <sys/malloc.h> 51 #include <sys/uio.h> 52 #include <sys/sysctl.h> 53 #include <sys/endian.h> 54 #include <sys/queue.h> 55 #include <sys/kthread.h> 56 #include <sys/taskqueue.h> 57 #include <sys/sbuf.h> 58 59 #include <machine/bus.h> 60 #include <machine/resource.h> 61 #include <sys/rman.h> 62 63 #include <machine/stdarg.h> 64 65 #include <cam/cam.h> 66 #include <cam/cam_ccb.h> 67 #include <cam/cam_xpt.h> 68 #include <cam/cam_debug.h> 69 #include <cam/cam_sim.h> 70 #include <cam/cam_xpt_sim.h> 71 #include <cam/cam_xpt_periph.h> 72 #include <cam/cam_periph.h> 73 #include <cam/scsi/scsi_all.h> 74 #include <cam/scsi/scsi_message.h> 75 #if __FreeBSD_version >= 900026 76 #include <cam/scsi/smp_all.h> 77 #endif 78 79 #include <dev/mps/mpi/mpi2_type.h> 80 #include <dev/mps/mpi/mpi2.h> 81 #include <dev/mps/mpi/mpi2_ioc.h> 82 #include <dev/mps/mpi/mpi2_sas.h> 83 #include <dev/mps/mpi/mpi2_cnfg.h> 84 #include <dev/mps/mpi/mpi2_init.h> 85 #include <dev/mps/mpi/mpi2_tool.h> 86 #include <dev/mps/mps_ioctl.h> 87 #include <dev/mps/mpsvar.h> 88 #include <dev/mps/mps_table.h> 89 #include <dev/mps/mps_sas.h> 90 91 #define MPSSAS_DISCOVERY_TIMEOUT 20 92 #define MPSSAS_MAX_DISCOVERY_TIMEOUTS 10 /* 200 seconds */ 93 94 /* 95 * static array to check SCSI OpCode for EEDP protection bits 96 */ 97 #define PRO_R MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP 98 #define PRO_W MPI2_SCSIIO_EEDPFLAGS_INSERT_OP 99 #define PRO_V MPI2_SCSIIO_EEDPFLAGS_INSERT_OP 100 static uint8_t op_code_prot[256] = { 101 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 103 0, 0, 0, 0, 0, 0, 0, 0, PRO_R, 0, PRO_W, 0, 0, 0, PRO_W, PRO_V, 104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 105 0, PRO_W, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 106 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 107 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 108 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 109 0, 0, 0, 0, 0, 0, 0, 0, PRO_R, 0, PRO_W, 0, 0, 0, PRO_W, PRO_V, 110 0, 0, 0, PRO_W, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 111 0, 0, 0, 0, 0, 0, 0, 0, PRO_R, 0, PRO_W, 0, 0, 0, PRO_W, PRO_V, 112 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 113 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 116 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 117 }; 118 119 MALLOC_DEFINE(M_MPSSAS, "MPSSAS", "MPS SAS memory"); 120 121 static void mpssas_remove_device(struct mps_softc *, struct mps_command *); 122 static void mpssas_remove_complete(struct mps_softc *, struct mps_command *); 123 static void mpssas_action(struct cam_sim *sim, union ccb *ccb); 124 static void mpssas_poll(struct cam_sim *sim); 125 static int mpssas_send_abort(struct mps_softc *sc, struct mps_command *tm, 126 struct mps_command *cm); 127 static void mpssas_scsiio_timeout(void *data); 128 static void mpssas_abort_complete(struct mps_softc *sc, struct mps_command *cm); 129 static void mpssas_direct_drive_io(struct mpssas_softc *sassc, 130 struct mps_command *cm, union ccb *ccb); 131 static void mpssas_action_scsiio(struct mpssas_softc *, union ccb *); 132 static void mpssas_scsiio_complete(struct mps_softc *, struct mps_command *); 133 static void mpssas_action_resetdev(struct mpssas_softc *, union ccb *); 134 #if __FreeBSD_version >= 900026 135 static void mpssas_smpio_complete(struct mps_softc *sc, struct mps_command *cm); 136 static void mpssas_send_smpcmd(struct mpssas_softc *sassc, union ccb *ccb, 137 uint64_t sasaddr); 138 static void mpssas_action_smpio(struct mpssas_softc *sassc, union ccb *ccb); 139 #endif //FreeBSD_version >= 900026 140 static void mpssas_resetdev_complete(struct mps_softc *, struct mps_command *); 141 static void mpssas_async(void *callback_arg, uint32_t code, 142 struct cam_path *path, void *arg); 143 #if (__FreeBSD_version < 901503) || \ 144 ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000006)) 145 static void mpssas_check_eedp(struct mps_softc *sc, struct cam_path *path, 146 struct ccb_getdev *cgd); 147 static void mpssas_read_cap_done(struct cam_periph *periph, union ccb *done_ccb); 148 #endif 149 static int mpssas_send_portenable(struct mps_softc *sc); 150 static void mpssas_portenable_complete(struct mps_softc *sc, 151 struct mps_command *cm); 152 153 struct mpssas_target * 154 mpssas_find_target_by_handle(struct mpssas_softc *sassc, int start, uint16_t handle) 155 { 156 struct mpssas_target *target; 157 int i; 158 159 for (i = start; i < sassc->maxtargets; i++) { 160 target = &sassc->targets[i]; 161 if (target->handle == handle) 162 return (target); 163 } 164 165 return (NULL); 166 } 167 168 /* we need to freeze the simq during attach and diag reset, to avoid failing 169 * commands before device handles have been found by discovery. Since 170 * discovery involves reading config pages and possibly sending commands, 171 * discovery actions may continue even after we receive the end of discovery 172 * event, so refcount discovery actions instead of assuming we can unfreeze 173 * the simq when we get the event. 174 */ 175 void 176 mpssas_startup_increment(struct mpssas_softc *sassc) 177 { 178 MPS_FUNCTRACE(sassc->sc); 179 180 if ((sassc->flags & MPSSAS_IN_STARTUP) != 0) { 181 if (sassc->startup_refcount++ == 0) { 182 /* just starting, freeze the simq */ 183 mps_dprint(sassc->sc, MPS_INIT, 184 "%s freezing simq\n", __func__); 185 #if __FreeBSD_version >= 1000039 186 xpt_hold_boot(); 187 #endif 188 xpt_freeze_simq(sassc->sim, 1); 189 } 190 mps_dprint(sassc->sc, MPS_INIT, "%s refcount %u\n", __func__, 191 sassc->startup_refcount); 192 } 193 } 194 195 void 196 mpssas_release_simq_reinit(struct mpssas_softc *sassc) 197 { 198 if (sassc->flags & MPSSAS_QUEUE_FROZEN) { 199 sassc->flags &= ~MPSSAS_QUEUE_FROZEN; 200 xpt_release_simq(sassc->sim, 1); 201 mps_dprint(sassc->sc, MPS_INFO, "Unfreezing SIM queue\n"); 202 } 203 } 204 205 void 206 mpssas_startup_decrement(struct mpssas_softc *sassc) 207 { 208 MPS_FUNCTRACE(sassc->sc); 209 210 if ((sassc->flags & MPSSAS_IN_STARTUP) != 0) { 211 if (--sassc->startup_refcount == 0) { 212 /* finished all discovery-related actions, release 213 * the simq and rescan for the latest topology. 214 */ 215 mps_dprint(sassc->sc, MPS_INIT, 216 "%s releasing simq\n", __func__); 217 sassc->flags &= ~MPSSAS_IN_STARTUP; 218 xpt_release_simq(sassc->sim, 1); 219 #if __FreeBSD_version >= 1000039 220 xpt_release_boot(); 221 #else 222 mpssas_rescan_target(sassc->sc, NULL); 223 #endif 224 } 225 mps_dprint(sassc->sc, MPS_INIT, "%s refcount %u\n", __func__, 226 sassc->startup_refcount); 227 } 228 } 229 230 /* The firmware requires us to stop sending commands when we're doing task 231 * management, so refcount the TMs and keep the simq frozen when any are in 232 * use. 233 */ 234 struct mps_command * 235 mpssas_alloc_tm(struct mps_softc *sc) 236 { 237 struct mps_command *tm; 238 239 tm = mps_alloc_high_priority_command(sc); 240 return tm; 241 } 242 243 void 244 mpssas_free_tm(struct mps_softc *sc, struct mps_command *tm) 245 { 246 int target_id = 0xFFFFFFFF; 247 248 if (tm == NULL) 249 return; 250 251 /* 252 * For TM's the devq is frozen for the device. Unfreeze it here and 253 * free the resources used for freezing the devq. Must clear the 254 * INRESET flag as well or scsi I/O will not work. 255 */ 256 if (tm->cm_targ != NULL) { 257 tm->cm_targ->flags &= ~MPSSAS_TARGET_INRESET; 258 target_id = tm->cm_targ->tid; 259 } 260 if (tm->cm_ccb) { 261 mps_dprint(sc, MPS_INFO, "Unfreezing devq for target ID %d\n", 262 target_id); 263 xpt_release_devq(tm->cm_ccb->ccb_h.path, 1, TRUE); 264 xpt_free_path(tm->cm_ccb->ccb_h.path); 265 xpt_free_ccb(tm->cm_ccb); 266 } 267 268 mps_free_high_priority_command(sc, tm); 269 } 270 271 void 272 mpssas_rescan_target(struct mps_softc *sc, struct mpssas_target *targ) 273 { 274 struct mpssas_softc *sassc = sc->sassc; 275 path_id_t pathid; 276 target_id_t targetid; 277 union ccb *ccb; 278 279 MPS_FUNCTRACE(sc); 280 pathid = cam_sim_path(sassc->sim); 281 if (targ == NULL) 282 targetid = CAM_TARGET_WILDCARD; 283 else 284 targetid = targ - sassc->targets; 285 286 /* 287 * Allocate a CCB and schedule a rescan. 288 */ 289 ccb = xpt_alloc_ccb_nowait(); 290 if (ccb == NULL) { 291 mps_dprint(sc, MPS_ERROR, "unable to alloc CCB for rescan\n"); 292 return; 293 } 294 295 if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, 296 targetid, CAM_LUN_WILDCARD) != CAM_REQ_CMP) { 297 mps_dprint(sc, MPS_ERROR, "unable to create path for rescan\n"); 298 xpt_free_ccb(ccb); 299 return; 300 } 301 302 if (targetid == CAM_TARGET_WILDCARD) 303 ccb->ccb_h.func_code = XPT_SCAN_BUS; 304 else 305 ccb->ccb_h.func_code = XPT_SCAN_TGT; 306 307 mps_dprint(sc, MPS_TRACE, "%s targetid %u\n", __func__, targetid); 308 xpt_rescan(ccb); 309 } 310 311 static void 312 mpssas_log_command(struct mps_command *cm, u_int level, const char *fmt, ...) 313 { 314 struct sbuf sb; 315 va_list ap; 316 char str[192]; 317 char path_str[64]; 318 319 if (cm == NULL) 320 return; 321 322 /* No need to be in here if debugging isn't enabled */ 323 if ((cm->cm_sc->mps_debug & level) == 0) 324 return; 325 326 sbuf_new(&sb, str, sizeof(str), 0); 327 328 va_start(ap, fmt); 329 330 if (cm->cm_ccb != NULL) { 331 xpt_path_string(cm->cm_ccb->csio.ccb_h.path, path_str, 332 sizeof(path_str)); 333 sbuf_cat(&sb, path_str); 334 if (cm->cm_ccb->ccb_h.func_code == XPT_SCSI_IO) { 335 scsi_command_string(&cm->cm_ccb->csio, &sb); 336 sbuf_printf(&sb, "length %d ", 337 cm->cm_ccb->csio.dxfer_len); 338 } 339 } 340 else { 341 sbuf_printf(&sb, "(noperiph:%s%d:%u:%u:%u): ", 342 cam_sim_name(cm->cm_sc->sassc->sim), 343 cam_sim_unit(cm->cm_sc->sassc->sim), 344 cam_sim_bus(cm->cm_sc->sassc->sim), 345 cm->cm_targ ? cm->cm_targ->tid : 0xFFFFFFFF, 346 cm->cm_lun); 347 } 348 349 sbuf_printf(&sb, "SMID %u ", cm->cm_desc.Default.SMID); 350 sbuf_vprintf(&sb, fmt, ap); 351 sbuf_finish(&sb); 352 mps_print_field(cm->cm_sc, "%s", sbuf_data(&sb)); 353 354 va_end(ap); 355 } 356 357 358 static void 359 mpssas_remove_volume(struct mps_softc *sc, struct mps_command *tm) 360 { 361 MPI2_SCSI_TASK_MANAGE_REPLY *reply; 362 struct mpssas_target *targ; 363 uint16_t handle; 364 365 MPS_FUNCTRACE(sc); 366 367 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply; 368 handle = (uint16_t)(uintptr_t)tm->cm_complete_data; 369 targ = tm->cm_targ; 370 371 if (reply == NULL) { 372 /* XXX retry the remove after the diag reset completes? */ 373 mps_dprint(sc, MPS_FAULT, 374 "%s NULL reply resetting device 0x%04x\n", __func__, 375 handle); 376 mpssas_free_tm(sc, tm); 377 return; 378 } 379 380 if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) != 381 MPI2_IOCSTATUS_SUCCESS) { 382 mps_dprint(sc, MPS_ERROR, 383 "IOCStatus = 0x%x while resetting device 0x%x\n", 384 le16toh(reply->IOCStatus), handle); 385 } 386 387 mps_dprint(sc, MPS_XINFO, 388 "Reset aborted %u commands\n", reply->TerminationCount); 389 mps_free_reply(sc, tm->cm_reply_data); 390 tm->cm_reply = NULL; /* Ensures the reply won't get re-freed */ 391 392 mps_dprint(sc, MPS_XINFO, 393 "clearing target %u handle 0x%04x\n", targ->tid, handle); 394 395 /* 396 * Don't clear target if remove fails because things will get confusing. 397 * Leave the devname and sasaddr intact so that we know to avoid reusing 398 * this target id if possible, and so we can assign the same target id 399 * to this device if it comes back in the future. 400 */ 401 if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) == 402 MPI2_IOCSTATUS_SUCCESS) { 403 targ = tm->cm_targ; 404 targ->handle = 0x0; 405 targ->encl_handle = 0x0; 406 targ->encl_slot = 0x0; 407 targ->exp_dev_handle = 0x0; 408 targ->phy_num = 0x0; 409 targ->linkrate = 0x0; 410 targ->devinfo = 0x0; 411 targ->flags = 0x0; 412 } 413 414 mpssas_free_tm(sc, tm); 415 } 416 417 418 /* 419 * No Need to call "MPI2_SAS_OP_REMOVE_DEVICE" For Volume removal. 420 * Otherwise Volume Delete is same as Bare Drive Removal. 421 */ 422 void 423 mpssas_prepare_volume_remove(struct mpssas_softc *sassc, uint16_t handle) 424 { 425 MPI2_SCSI_TASK_MANAGE_REQUEST *req; 426 struct mps_softc *sc; 427 struct mps_command *cm; 428 struct mpssas_target *targ = NULL; 429 430 MPS_FUNCTRACE(sassc->sc); 431 sc = sassc->sc; 432 433 #ifdef WD_SUPPORT 434 /* 435 * If this is a WD controller, determine if the disk should be exposed 436 * to the OS or not. If disk should be exposed, return from this 437 * function without doing anything. 438 */ 439 if (sc->WD_available && (sc->WD_hide_expose == 440 MPS_WD_EXPOSE_ALWAYS)) { 441 return; 442 } 443 #endif //WD_SUPPORT 444 445 targ = mpssas_find_target_by_handle(sassc, 0, handle); 446 if (targ == NULL) { 447 /* FIXME: what is the action? */ 448 /* We don't know about this device? */ 449 mps_dprint(sc, MPS_ERROR, 450 "%s %d : invalid handle 0x%x \n", __func__,__LINE__, handle); 451 return; 452 } 453 454 targ->flags |= MPSSAS_TARGET_INREMOVAL; 455 456 cm = mpssas_alloc_tm(sc); 457 if (cm == NULL) { 458 mps_dprint(sc, MPS_ERROR, 459 "%s: command alloc failure\n", __func__); 460 return; 461 } 462 463 mpssas_rescan_target(sc, targ); 464 465 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req; 466 req->DevHandle = targ->handle; 467 req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT; 468 req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET; 469 470 /* SAS Hard Link Reset / SATA Link Reset */ 471 req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET; 472 473 cm->cm_targ = targ; 474 cm->cm_data = NULL; 475 cm->cm_desc.HighPriority.RequestFlags = 476 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY; 477 cm->cm_complete = mpssas_remove_volume; 478 cm->cm_complete_data = (void *)(uintptr_t)handle; 479 480 mps_dprint(sc, MPS_INFO, "%s: Sending reset for target ID %d\n", 481 __func__, targ->tid); 482 mpssas_prepare_for_tm(sc, cm, targ, CAM_LUN_WILDCARD); 483 484 mps_map_command(sc, cm); 485 } 486 487 /* 488 * The MPT2 firmware performs debounce on the link to avoid transient link 489 * errors and false removals. When it does decide that link has been lost 490 * and a device need to go away, it expects that the host will perform a 491 * target reset and then an op remove. The reset has the side-effect of 492 * aborting any outstanding requests for the device, which is required for 493 * the op-remove to succeed. It's not clear if the host should check for 494 * the device coming back alive after the reset. 495 */ 496 void 497 mpssas_prepare_remove(struct mpssas_softc *sassc, uint16_t handle) 498 { 499 MPI2_SCSI_TASK_MANAGE_REQUEST *req; 500 struct mps_softc *sc; 501 struct mps_command *cm; 502 struct mpssas_target *targ = NULL; 503 504 MPS_FUNCTRACE(sassc->sc); 505 506 sc = sassc->sc; 507 508 targ = mpssas_find_target_by_handle(sassc, 0, handle); 509 if (targ == NULL) { 510 /* FIXME: what is the action? */ 511 /* We don't know about this device? */ 512 mps_dprint(sc, MPS_ERROR, 513 "%s : invalid handle 0x%x \n", __func__, handle); 514 return; 515 } 516 517 targ->flags |= MPSSAS_TARGET_INREMOVAL; 518 519 cm = mpssas_alloc_tm(sc); 520 if (cm == NULL) { 521 mps_dprint(sc, MPS_ERROR, 522 "%s: command alloc failure\n", __func__); 523 return; 524 } 525 526 mpssas_rescan_target(sc, targ); 527 528 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req; 529 memset(req, 0, sizeof(*req)); 530 req->DevHandle = htole16(targ->handle); 531 req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT; 532 req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET; 533 534 /* SAS Hard Link Reset / SATA Link Reset */ 535 req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET; 536 537 cm->cm_targ = targ; 538 cm->cm_data = NULL; 539 cm->cm_desc.HighPriority.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY; 540 cm->cm_complete = mpssas_remove_device; 541 cm->cm_complete_data = (void *)(uintptr_t)handle; 542 543 mps_dprint(sc, MPS_INFO, "%s: Sending reset for target ID %d\n", 544 __func__, targ->tid); 545 mpssas_prepare_for_tm(sc, cm, targ, CAM_LUN_WILDCARD); 546 547 mps_map_command(sc, cm); 548 } 549 550 static void 551 mpssas_remove_device(struct mps_softc *sc, struct mps_command *tm) 552 { 553 MPI2_SCSI_TASK_MANAGE_REPLY *reply; 554 MPI2_SAS_IOUNIT_CONTROL_REQUEST *req; 555 struct mpssas_target *targ; 556 struct mps_command *next_cm; 557 uint16_t handle; 558 559 MPS_FUNCTRACE(sc); 560 561 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply; 562 handle = (uint16_t)(uintptr_t)tm->cm_complete_data; 563 targ = tm->cm_targ; 564 565 /* 566 * Currently there should be no way we can hit this case. It only 567 * happens when we have a failure to allocate chain frames, and 568 * task management commands don't have S/G lists. 569 */ 570 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) { 571 mps_dprint(sc, MPS_ERROR, 572 "%s: cm_flags = %#x for remove of handle %#04x! " 573 "This should not happen!\n", __func__, tm->cm_flags, 574 handle); 575 } 576 577 if (reply == NULL) { 578 /* XXX retry the remove after the diag reset completes? */ 579 mps_dprint(sc, MPS_FAULT, 580 "%s NULL reply resetting device 0x%04x\n", __func__, 581 handle); 582 mpssas_free_tm(sc, tm); 583 return; 584 } 585 586 if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) != 587 MPI2_IOCSTATUS_SUCCESS) { 588 mps_dprint(sc, MPS_ERROR, 589 "IOCStatus = 0x%x while resetting device 0x%x\n", 590 le16toh(reply->IOCStatus), handle); 591 } 592 593 mps_dprint(sc, MPS_XINFO, "Reset aborted %u commands\n", 594 le32toh(reply->TerminationCount)); 595 mps_free_reply(sc, tm->cm_reply_data); 596 tm->cm_reply = NULL; /* Ensures the reply won't get re-freed */ 597 598 /* Reuse the existing command */ 599 req = (MPI2_SAS_IOUNIT_CONTROL_REQUEST *)tm->cm_req; 600 memset(req, 0, sizeof(*req)); 601 req->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL; 602 req->Operation = MPI2_SAS_OP_REMOVE_DEVICE; 603 req->DevHandle = htole16(handle); 604 tm->cm_data = NULL; 605 tm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE; 606 tm->cm_complete = mpssas_remove_complete; 607 tm->cm_complete_data = (void *)(uintptr_t)handle; 608 609 mps_map_command(sc, tm); 610 611 mps_dprint(sc, MPS_XINFO, "clearing target %u handle 0x%04x\n", 612 targ->tid, handle); 613 TAILQ_FOREACH_SAFE(tm, &targ->commands, cm_link, next_cm) { 614 union ccb *ccb; 615 616 mps_dprint(sc, MPS_XINFO, "Completing missed command %p\n", tm); 617 ccb = tm->cm_complete_data; 618 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE); 619 mpssas_scsiio_complete(sc, tm); 620 } 621 } 622 623 static void 624 mpssas_remove_complete(struct mps_softc *sc, struct mps_command *tm) 625 { 626 MPI2_SAS_IOUNIT_CONTROL_REPLY *reply; 627 uint16_t handle; 628 struct mpssas_target *targ; 629 struct mpssas_lun *lun; 630 631 MPS_FUNCTRACE(sc); 632 633 reply = (MPI2_SAS_IOUNIT_CONTROL_REPLY *)tm->cm_reply; 634 handle = (uint16_t)(uintptr_t)tm->cm_complete_data; 635 636 /* 637 * Currently there should be no way we can hit this case. It only 638 * happens when we have a failure to allocate chain frames, and 639 * task management commands don't have S/G lists. 640 */ 641 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) { 642 mps_dprint(sc, MPS_XINFO, 643 "%s: cm_flags = %#x for remove of handle %#04x! " 644 "This should not happen!\n", __func__, tm->cm_flags, 645 handle); 646 mpssas_free_tm(sc, tm); 647 return; 648 } 649 650 if (reply == NULL) { 651 /* most likely a chip reset */ 652 mps_dprint(sc, MPS_FAULT, 653 "%s NULL reply removing device 0x%04x\n", __func__, handle); 654 mpssas_free_tm(sc, tm); 655 return; 656 } 657 658 mps_dprint(sc, MPS_XINFO, 659 "%s on handle 0x%04x, IOCStatus= 0x%x\n", __func__, 660 handle, le16toh(reply->IOCStatus)); 661 662 /* 663 * Don't clear target if remove fails because things will get confusing. 664 * Leave the devname and sasaddr intact so that we know to avoid reusing 665 * this target id if possible, and so we can assign the same target id 666 * to this device if it comes back in the future. 667 */ 668 if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) == 669 MPI2_IOCSTATUS_SUCCESS) { 670 targ = tm->cm_targ; 671 targ->handle = 0x0; 672 targ->encl_handle = 0x0; 673 targ->encl_slot = 0x0; 674 targ->exp_dev_handle = 0x0; 675 targ->phy_num = 0x0; 676 targ->linkrate = 0x0; 677 targ->devinfo = 0x0; 678 targ->flags = 0x0; 679 680 while(!SLIST_EMPTY(&targ->luns)) { 681 lun = SLIST_FIRST(&targ->luns); 682 SLIST_REMOVE_HEAD(&targ->luns, lun_link); 683 free(lun, M_MPT2); 684 } 685 } 686 687 688 mpssas_free_tm(sc, tm); 689 } 690 691 static int 692 mpssas_register_events(struct mps_softc *sc) 693 { 694 u32 events[MPI2_EVENT_NOTIFY_EVENTMASK_WORDS]; 695 696 bzero(events, 16); 697 setbit(events, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE); 698 setbit(events, MPI2_EVENT_SAS_DISCOVERY); 699 setbit(events, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE); 700 setbit(events, MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE); 701 setbit(events, MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW); 702 setbit(events, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST); 703 setbit(events, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE); 704 setbit(events, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST); 705 setbit(events, MPI2_EVENT_IR_VOLUME); 706 setbit(events, MPI2_EVENT_IR_PHYSICAL_DISK); 707 setbit(events, MPI2_EVENT_IR_OPERATION_STATUS); 708 setbit(events, MPI2_EVENT_LOG_ENTRY_ADDED); 709 710 mps_register_events(sc, events, mpssas_evt_handler, NULL, 711 &sc->sassc->mpssas_eh); 712 713 return (0); 714 } 715 716 int 717 mps_attach_sas(struct mps_softc *sc) 718 { 719 struct mpssas_softc *sassc; 720 cam_status status; 721 int unit, error = 0, reqs; 722 723 MPS_FUNCTRACE(sc); 724 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__); 725 726 sassc = malloc(sizeof(struct mpssas_softc), M_MPT2, M_WAITOK|M_ZERO); 727 if(!sassc) { 728 mps_dprint(sc, MPS_INIT|MPS_ERROR, 729 "Cannot allocate SAS controller memory\n"); 730 return (ENOMEM); 731 } 732 733 /* 734 * XXX MaxTargets could change during a reinit. Since we don't 735 * resize the targets[] array during such an event, cache the value 736 * of MaxTargets here so that we don't get into trouble later. This 737 * should move into the reinit logic. 738 */ 739 sassc->maxtargets = sc->facts->MaxTargets + sc->facts->MaxVolumes; 740 sassc->targets = malloc(sizeof(struct mpssas_target) * 741 sassc->maxtargets, M_MPT2, M_WAITOK|M_ZERO); 742 if(!sassc->targets) { 743 mps_dprint(sc, MPS_INIT|MPS_ERROR, 744 "Cannot allocate SAS target memory\n"); 745 free(sassc, M_MPT2); 746 return (ENOMEM); 747 } 748 sc->sassc = sassc; 749 sassc->sc = sc; 750 751 reqs = sc->num_reqs - sc->num_prireqs - 1; 752 if ((sassc->devq = cam_simq_alloc(reqs)) == NULL) { 753 mps_dprint(sc, MPS_ERROR, "Cannot allocate SIMQ\n"); 754 error = ENOMEM; 755 goto out; 756 } 757 758 unit = device_get_unit(sc->mps_dev); 759 sassc->sim = cam_sim_alloc(mpssas_action, mpssas_poll, "mps", sassc, 760 unit, &sc->mps_mtx, reqs, reqs, sassc->devq); 761 if (sassc->sim == NULL) { 762 mps_dprint(sc, MPS_INIT|MPS_ERROR, "Cannot allocate SIM\n"); 763 error = EINVAL; 764 goto out; 765 } 766 767 TAILQ_INIT(&sassc->ev_queue); 768 769 /* Initialize taskqueue for Event Handling */ 770 TASK_INIT(&sassc->ev_task, 0, mpssas_firmware_event_work, sc); 771 sassc->ev_tq = taskqueue_create("mps_taskq", M_NOWAIT | M_ZERO, 772 taskqueue_thread_enqueue, &sassc->ev_tq); 773 taskqueue_start_threads(&sassc->ev_tq, 1, PRIBIO, "%s taskq", 774 device_get_nameunit(sc->mps_dev)); 775 776 mps_lock(sc); 777 778 /* 779 * XXX There should be a bus for every port on the adapter, but since 780 * we're just going to fake the topology for now, we'll pretend that 781 * everything is just a target on a single bus. 782 */ 783 if ((error = xpt_bus_register(sassc->sim, sc->mps_dev, 0)) != 0) { 784 mps_dprint(sc, MPS_INIT|MPS_ERROR, 785 "Error %d registering SCSI bus\n", error); 786 mps_unlock(sc); 787 goto out; 788 } 789 790 /* 791 * Assume that discovery events will start right away. 792 * 793 * Hold off boot until discovery is complete. 794 */ 795 sassc->flags |= MPSSAS_IN_STARTUP | MPSSAS_IN_DISCOVERY; 796 sc->sassc->startup_refcount = 0; 797 mpssas_startup_increment(sassc); 798 799 callout_init(&sassc->discovery_callout, 1 /*mpsafe*/); 800 801 /* 802 * Register for async events so we can determine the EEDP 803 * capabilities of devices. 804 */ 805 status = xpt_create_path(&sassc->path, /*periph*/NULL, 806 cam_sim_path(sc->sassc->sim), CAM_TARGET_WILDCARD, 807 CAM_LUN_WILDCARD); 808 if (status != CAM_REQ_CMP) { 809 mps_dprint(sc, MPS_ERROR|MPS_INIT, 810 "Error %#x creating sim path\n", status); 811 sassc->path = NULL; 812 } else { 813 int event; 814 815 #if (__FreeBSD_version >= 1000006) || \ 816 ((__FreeBSD_version >= 901503) && (__FreeBSD_version < 1000000)) 817 event = AC_ADVINFO_CHANGED; 818 #else 819 event = AC_FOUND_DEVICE; 820 #endif 821 status = xpt_register_async(event, mpssas_async, sc, 822 sassc->path); 823 if (status != CAM_REQ_CMP) { 824 mps_dprint(sc, MPS_ERROR, 825 "Error %#x registering async handler for " 826 "AC_ADVINFO_CHANGED events\n", status); 827 xpt_free_path(sassc->path); 828 sassc->path = NULL; 829 } 830 } 831 if (status != CAM_REQ_CMP) { 832 /* 833 * EEDP use is the exception, not the rule. 834 * Warn the user, but do not fail to attach. 835 */ 836 mps_printf(sc, "EEDP capabilities disabled.\n"); 837 } 838 839 mps_unlock(sc); 840 841 mpssas_register_events(sc); 842 out: 843 if (error) 844 mps_detach_sas(sc); 845 846 mps_dprint(sc, MPS_INIT, "%s exit error= %d\n", __func__, error); 847 return (error); 848 } 849 850 int 851 mps_detach_sas(struct mps_softc *sc) 852 { 853 struct mpssas_softc *sassc; 854 struct mpssas_lun *lun, *lun_tmp; 855 struct mpssas_target *targ; 856 int i; 857 858 MPS_FUNCTRACE(sc); 859 860 if (sc->sassc == NULL) 861 return (0); 862 863 sassc = sc->sassc; 864 mps_deregister_events(sc, sassc->mpssas_eh); 865 866 /* 867 * Drain and free the event handling taskqueue with the lock 868 * unheld so that any parallel processing tasks drain properly 869 * without deadlocking. 870 */ 871 if (sassc->ev_tq != NULL) 872 taskqueue_free(sassc->ev_tq); 873 874 /* Make sure CAM doesn't wedge if we had to bail out early. */ 875 mps_lock(sc); 876 877 while (sassc->startup_refcount != 0) 878 mpssas_startup_decrement(sassc); 879 880 /* Deregister our async handler */ 881 if (sassc->path != NULL) { 882 xpt_register_async(0, mpssas_async, sc, sassc->path); 883 xpt_free_path(sassc->path); 884 sassc->path = NULL; 885 } 886 887 if (sassc->flags & MPSSAS_IN_STARTUP) 888 xpt_release_simq(sassc->sim, 1); 889 890 if (sassc->sim != NULL) { 891 xpt_bus_deregister(cam_sim_path(sassc->sim)); 892 cam_sim_free(sassc->sim, FALSE); 893 } 894 895 mps_unlock(sc); 896 897 if (sassc->devq != NULL) 898 cam_simq_free(sassc->devq); 899 900 for(i=0; i< sassc->maxtargets ;i++) { 901 targ = &sassc->targets[i]; 902 SLIST_FOREACH_SAFE(lun, &targ->luns, lun_link, lun_tmp) { 903 free(lun, M_MPT2); 904 } 905 } 906 free(sassc->targets, M_MPT2); 907 free(sassc, M_MPT2); 908 sc->sassc = NULL; 909 910 return (0); 911 } 912 913 void 914 mpssas_discovery_end(struct mpssas_softc *sassc) 915 { 916 struct mps_softc *sc = sassc->sc; 917 918 MPS_FUNCTRACE(sc); 919 920 if (sassc->flags & MPSSAS_DISCOVERY_TIMEOUT_PENDING) 921 callout_stop(&sassc->discovery_callout); 922 923 /* 924 * After discovery has completed, check the mapping table for any 925 * missing devices and update their missing counts. Only do this once 926 * whenever the driver is initialized so that missing counts aren't 927 * updated unnecessarily. Note that just because discovery has 928 * completed doesn't mean that events have been processed yet. The 929 * check_devices function is a callout timer that checks if ALL devices 930 * are missing. If so, it will wait a little longer for events to 931 * complete and keep resetting itself until some device in the mapping 932 * table is not missing, meaning that event processing has started. 933 */ 934 if (sc->track_mapping_events) { 935 mps_dprint(sc, MPS_XINFO | MPS_MAPPING, "Discovery has " 936 "completed. Check for missing devices in the mapping " 937 "table.\n"); 938 callout_reset(&sc->device_check_callout, 939 MPS_MISSING_CHECK_DELAY * hz, mps_mapping_check_devices, 940 sc); 941 } 942 } 943 944 static void 945 mpssas_action(struct cam_sim *sim, union ccb *ccb) 946 { 947 struct mpssas_softc *sassc; 948 949 sassc = cam_sim_softc(sim); 950 951 MPS_FUNCTRACE(sassc->sc); 952 mps_dprint(sassc->sc, MPS_TRACE, "ccb func_code 0x%x\n", 953 ccb->ccb_h.func_code); 954 mtx_assert(&sassc->sc->mps_mtx, MA_OWNED); 955 956 switch (ccb->ccb_h.func_code) { 957 case XPT_PATH_INQ: 958 { 959 struct ccb_pathinq *cpi = &ccb->cpi; 960 struct mps_softc *sc = sassc->sc; 961 uint8_t sges_per_frame; 962 963 cpi->version_num = 1; 964 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16; 965 cpi->target_sprt = 0; 966 #if __FreeBSD_version >= 1000039 967 cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED | PIM_NOSCAN; 968 #else 969 cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED; 970 #endif 971 cpi->hba_eng_cnt = 0; 972 cpi->max_target = sassc->maxtargets - 1; 973 cpi->max_lun = 255; 974 975 /* 976 * initiator_id is set here to an ID outside the set of valid 977 * target IDs (including volumes). 978 */ 979 cpi->initiator_id = sassc->maxtargets; 980 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 981 strlcpy(cpi->hba_vid, "Avago Tech", HBA_IDLEN); 982 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 983 cpi->unit_number = cam_sim_unit(sim); 984 cpi->bus_id = cam_sim_bus(sim); 985 cpi->base_transfer_speed = 150000; 986 cpi->transport = XPORT_SAS; 987 cpi->transport_version = 0; 988 cpi->protocol = PROTO_SCSI; 989 cpi->protocol_version = SCSI_REV_SPC; 990 991 /* 992 * Max IO Size is Page Size * the following: 993 * ((SGEs per frame - 1 for chain element) * 994 * Max Chain Depth) + 1 for no chain needed in last frame 995 * 996 * If user suggests a Max IO size to use, use the smaller of the 997 * user's value and the calculated value as long as the user's 998 * value is larger than 0. The user's value is in pages. 999 */ 1000 sges_per_frame = ((sc->reqframesz) / 1001 sizeof(MPI2_SGE_SIMPLE64)) - 1; 1002 cpi->maxio = (sges_per_frame * sc->facts->MaxChainDepth) + 1; 1003 cpi->maxio *= PAGE_SIZE; 1004 if ((sc->max_io_pages > 0) && (sc->max_io_pages * PAGE_SIZE < 1005 cpi->maxio)) 1006 cpi->maxio = sc->max_io_pages * PAGE_SIZE; 1007 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP); 1008 break; 1009 } 1010 case XPT_GET_TRAN_SETTINGS: 1011 { 1012 struct ccb_trans_settings *cts; 1013 struct ccb_trans_settings_sas *sas; 1014 struct ccb_trans_settings_scsi *scsi; 1015 struct mpssas_target *targ; 1016 1017 cts = &ccb->cts; 1018 sas = &cts->xport_specific.sas; 1019 scsi = &cts->proto_specific.scsi; 1020 1021 KASSERT(cts->ccb_h.target_id < sassc->maxtargets, 1022 ("Target %d out of bounds in XPT_GET_TRANS_SETTINGS\n", 1023 cts->ccb_h.target_id)); 1024 targ = &sassc->targets[cts->ccb_h.target_id]; 1025 if (targ->handle == 0x0) { 1026 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE); 1027 break; 1028 } 1029 1030 cts->protocol_version = SCSI_REV_SPC2; 1031 cts->transport = XPORT_SAS; 1032 cts->transport_version = 0; 1033 1034 sas->valid = CTS_SAS_VALID_SPEED; 1035 switch (targ->linkrate) { 1036 case 0x08: 1037 sas->bitrate = 150000; 1038 break; 1039 case 0x09: 1040 sas->bitrate = 300000; 1041 break; 1042 case 0x0a: 1043 sas->bitrate = 600000; 1044 break; 1045 default: 1046 sas->valid = 0; 1047 } 1048 1049 cts->protocol = PROTO_SCSI; 1050 scsi->valid = CTS_SCSI_VALID_TQ; 1051 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB; 1052 1053 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP); 1054 break; 1055 } 1056 case XPT_CALC_GEOMETRY: 1057 cam_calc_geometry(&ccb->ccg, /*extended*/1); 1058 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP); 1059 break; 1060 case XPT_RESET_DEV: 1061 mps_dprint(sassc->sc, MPS_XINFO, "mpssas_action XPT_RESET_DEV\n"); 1062 mpssas_action_resetdev(sassc, ccb); 1063 return; 1064 case XPT_RESET_BUS: 1065 case XPT_ABORT: 1066 case XPT_TERM_IO: 1067 mps_dprint(sassc->sc, MPS_XINFO, 1068 "mpssas_action faking success for abort or reset\n"); 1069 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP); 1070 break; 1071 case XPT_SCSI_IO: 1072 mpssas_action_scsiio(sassc, ccb); 1073 return; 1074 #if __FreeBSD_version >= 900026 1075 case XPT_SMP_IO: 1076 mpssas_action_smpio(sassc, ccb); 1077 return; 1078 #endif 1079 default: 1080 mpssas_set_ccbstatus(ccb, CAM_FUNC_NOTAVAIL); 1081 break; 1082 } 1083 xpt_done(ccb); 1084 1085 } 1086 1087 static void 1088 mpssas_announce_reset(struct mps_softc *sc, uint32_t ac_code, 1089 target_id_t target_id, lun_id_t lun_id) 1090 { 1091 path_id_t path_id = cam_sim_path(sc->sassc->sim); 1092 struct cam_path *path; 1093 1094 mps_dprint(sc, MPS_XINFO, "%s code %x target %d lun %jx\n", __func__, 1095 ac_code, target_id, (uintmax_t)lun_id); 1096 1097 if (xpt_create_path(&path, NULL, 1098 path_id, target_id, lun_id) != CAM_REQ_CMP) { 1099 mps_dprint(sc, MPS_ERROR, "unable to create path for reset " 1100 "notification\n"); 1101 return; 1102 } 1103 1104 xpt_async(ac_code, path, NULL); 1105 xpt_free_path(path); 1106 } 1107 1108 static void 1109 mpssas_complete_all_commands(struct mps_softc *sc) 1110 { 1111 struct mps_command *cm; 1112 int i; 1113 int completed; 1114 1115 MPS_FUNCTRACE(sc); 1116 mtx_assert(&sc->mps_mtx, MA_OWNED); 1117 1118 /* complete all commands with a NULL reply */ 1119 for (i = 1; i < sc->num_reqs; i++) { 1120 cm = &sc->commands[i]; 1121 cm->cm_reply = NULL; 1122 completed = 0; 1123 1124 if (cm->cm_flags & MPS_CM_FLAGS_POLLED) 1125 cm->cm_flags |= MPS_CM_FLAGS_COMPLETE; 1126 1127 if (cm->cm_complete != NULL) { 1128 mpssas_log_command(cm, MPS_RECOVERY, 1129 "completing cm %p state %x ccb %p for diag reset\n", 1130 cm, cm->cm_state, cm->cm_ccb); 1131 1132 cm->cm_complete(sc, cm); 1133 completed = 1; 1134 } 1135 1136 if (cm->cm_flags & MPS_CM_FLAGS_WAKEUP) { 1137 mpssas_log_command(cm, MPS_RECOVERY, 1138 "waking up cm %p state %x ccb %p for diag reset\n", 1139 cm, cm->cm_state, cm->cm_ccb); 1140 wakeup(cm); 1141 completed = 1; 1142 } 1143 1144 if (cm->cm_sc->io_cmds_active != 0) 1145 cm->cm_sc->io_cmds_active--; 1146 1147 if ((completed == 0) && (cm->cm_state != MPS_CM_STATE_FREE)) { 1148 /* this should never happen, but if it does, log */ 1149 mpssas_log_command(cm, MPS_RECOVERY, 1150 "cm %p state %x flags 0x%x ccb %p during diag " 1151 "reset\n", cm, cm->cm_state, cm->cm_flags, 1152 cm->cm_ccb); 1153 } 1154 } 1155 } 1156 1157 void 1158 mpssas_handle_reinit(struct mps_softc *sc) 1159 { 1160 int i; 1161 1162 /* Go back into startup mode and freeze the simq, so that CAM 1163 * doesn't send any commands until after we've rediscovered all 1164 * targets and found the proper device handles for them. 1165 * 1166 * After the reset, portenable will trigger discovery, and after all 1167 * discovery-related activities have finished, the simq will be 1168 * released. 1169 */ 1170 mps_dprint(sc, MPS_INIT, "%s startup\n", __func__); 1171 sc->sassc->flags |= MPSSAS_IN_STARTUP; 1172 sc->sassc->flags |= MPSSAS_IN_DISCOVERY; 1173 mpssas_startup_increment(sc->sassc); 1174 1175 /* notify CAM of a bus reset */ 1176 mpssas_announce_reset(sc, AC_BUS_RESET, CAM_TARGET_WILDCARD, 1177 CAM_LUN_WILDCARD); 1178 1179 /* complete and cleanup after all outstanding commands */ 1180 mpssas_complete_all_commands(sc); 1181 1182 mps_dprint(sc, MPS_INIT, 1183 "%s startup %u after command completion\n", __func__, 1184 sc->sassc->startup_refcount); 1185 1186 /* zero all the target handles, since they may change after the 1187 * reset, and we have to rediscover all the targets and use the new 1188 * handles. 1189 */ 1190 for (i = 0; i < sc->sassc->maxtargets; i++) { 1191 if (sc->sassc->targets[i].outstanding != 0) 1192 mps_dprint(sc, MPS_INIT, "target %u outstanding %u\n", 1193 i, sc->sassc->targets[i].outstanding); 1194 sc->sassc->targets[i].handle = 0x0; 1195 sc->sassc->targets[i].exp_dev_handle = 0x0; 1196 sc->sassc->targets[i].outstanding = 0; 1197 sc->sassc->targets[i].flags = MPSSAS_TARGET_INDIAGRESET; 1198 } 1199 } 1200 1201 static void 1202 mpssas_tm_timeout(void *data) 1203 { 1204 struct mps_command *tm = data; 1205 struct mps_softc *sc = tm->cm_sc; 1206 1207 mtx_assert(&sc->mps_mtx, MA_OWNED); 1208 1209 mpssas_log_command(tm, MPS_INFO|MPS_RECOVERY, 1210 "task mgmt %p timed out\n", tm); 1211 mps_reinit(sc); 1212 } 1213 1214 static void 1215 mpssas_logical_unit_reset_complete(struct mps_softc *sc, struct mps_command *tm) 1216 { 1217 MPI2_SCSI_TASK_MANAGE_REPLY *reply; 1218 MPI2_SCSI_TASK_MANAGE_REQUEST *req; 1219 unsigned int cm_count = 0; 1220 struct mps_command *cm; 1221 struct mpssas_target *targ; 1222 1223 callout_stop(&tm->cm_callout); 1224 1225 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req; 1226 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply; 1227 targ = tm->cm_targ; 1228 1229 /* 1230 * Currently there should be no way we can hit this case. It only 1231 * happens when we have a failure to allocate chain frames, and 1232 * task management commands don't have S/G lists. 1233 * XXXSL So should it be an assertion? 1234 */ 1235 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) { 1236 mps_dprint(sc, MPS_RECOVERY|MPS_ERROR, 1237 "%s: cm_flags = %#x for LUN reset! " 1238 "This should not happen!\n", __func__, tm->cm_flags); 1239 mpssas_free_tm(sc, tm); 1240 return; 1241 } 1242 1243 if (reply == NULL) { 1244 mps_dprint(sc, MPS_RECOVERY, "NULL reset reply for tm %p\n", 1245 tm); 1246 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) { 1247 /* this completion was due to a reset, just cleanup */ 1248 mps_dprint(sc, MPS_RECOVERY, "Hardware undergoing " 1249 "reset, ignoring NULL LUN reset reply\n"); 1250 targ->tm = NULL; 1251 mpssas_free_tm(sc, tm); 1252 } 1253 else { 1254 /* we should have gotten a reply. */ 1255 mps_dprint(sc, MPS_INFO|MPS_RECOVERY, "NULL reply on " 1256 "LUN reset attempt, resetting controller\n"); 1257 mps_reinit(sc); 1258 } 1259 return; 1260 } 1261 1262 mps_dprint(sc, MPS_RECOVERY, 1263 "logical unit reset status 0x%x code 0x%x count %u\n", 1264 le16toh(reply->IOCStatus), le32toh(reply->ResponseCode), 1265 le32toh(reply->TerminationCount)); 1266 1267 /* 1268 * See if there are any outstanding commands for this LUN. 1269 * This could be made more efficient by using a per-LU data 1270 * structure of some sort. 1271 */ 1272 TAILQ_FOREACH(cm, &targ->commands, cm_link) { 1273 if (cm->cm_lun == tm->cm_lun) 1274 cm_count++; 1275 } 1276 1277 if (cm_count == 0) { 1278 mps_dprint(sc, MPS_RECOVERY|MPS_INFO, 1279 "Finished recovery after LUN reset for target %u\n", 1280 targ->tid); 1281 1282 mpssas_announce_reset(sc, AC_SENT_BDR, targ->tid, tm->cm_lun); 1283 1284 /* 1285 * We've finished recovery for this logical unit. check and 1286 * see if some other logical unit has a timedout command 1287 * that needs to be processed. 1288 */ 1289 cm = TAILQ_FIRST(&targ->timedout_commands); 1290 if (cm) { 1291 mps_dprint(sc, MPS_INFO|MPS_RECOVERY, 1292 "More commands to abort for target %u\n", 1293 targ->tid); 1294 mpssas_send_abort(sc, tm, cm); 1295 } else { 1296 targ->tm = NULL; 1297 mpssas_free_tm(sc, tm); 1298 } 1299 } else { 1300 /* 1301 * If we still have commands for this LUN, the reset 1302 * effectively failed, regardless of the status reported. 1303 * Escalate to a target reset. 1304 */ 1305 mps_dprint(sc, MPS_INFO|MPS_RECOVERY, 1306 "logical unit reset complete for target %u, but still " 1307 "have %u command(s), sending target reset\n", targ->tid, 1308 cm_count); 1309 mpssas_send_reset(sc, tm, 1310 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET); 1311 } 1312 } 1313 1314 static void 1315 mpssas_target_reset_complete(struct mps_softc *sc, struct mps_command *tm) 1316 { 1317 MPI2_SCSI_TASK_MANAGE_REPLY *reply; 1318 MPI2_SCSI_TASK_MANAGE_REQUEST *req; 1319 struct mpssas_target *targ; 1320 1321 callout_stop(&tm->cm_callout); 1322 1323 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req; 1324 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply; 1325 targ = tm->cm_targ; 1326 1327 /* 1328 * Currently there should be no way we can hit this case. It only 1329 * happens when we have a failure to allocate chain frames, and 1330 * task management commands don't have S/G lists. 1331 */ 1332 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) { 1333 mps_dprint(sc, MPS_ERROR,"%s: cm_flags = %#x for target reset! " 1334 "This should not happen!\n", __func__, tm->cm_flags); 1335 mpssas_free_tm(sc, tm); 1336 return; 1337 } 1338 1339 if (reply == NULL) { 1340 mps_dprint(sc, MPS_RECOVERY, 1341 "NULL target reset reply for tm %pi TaskMID %u\n", 1342 tm, le16toh(req->TaskMID)); 1343 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) { 1344 /* this completion was due to a reset, just cleanup */ 1345 mps_dprint(sc, MPS_RECOVERY, "Hardware undergoing " 1346 "reset, ignoring NULL target reset reply\n"); 1347 targ->tm = NULL; 1348 mpssas_free_tm(sc, tm); 1349 } else { 1350 /* we should have gotten a reply. */ 1351 mps_dprint(sc, MPS_INFO|MPS_RECOVERY, "NULL reply on " 1352 "target reset attempt, resetting controller\n"); 1353 mps_reinit(sc); 1354 } 1355 return; 1356 } 1357 1358 mps_dprint(sc, MPS_RECOVERY, 1359 "target reset status 0x%x code 0x%x count %u\n", 1360 le16toh(reply->IOCStatus), le32toh(reply->ResponseCode), 1361 le32toh(reply->TerminationCount)); 1362 1363 if (targ->outstanding == 0) { 1364 /* we've finished recovery for this target and all 1365 * of its logical units. 1366 */ 1367 mps_dprint(sc, MPS_RECOVERY|MPS_INFO, 1368 "Finished reset recovery for target %u\n", targ->tid); 1369 1370 mpssas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid, 1371 CAM_LUN_WILDCARD); 1372 1373 targ->tm = NULL; 1374 mpssas_free_tm(sc, tm); 1375 } else { 1376 /* 1377 * After a target reset, if this target still has 1378 * outstanding commands, the reset effectively failed, 1379 * regardless of the status reported. escalate. 1380 */ 1381 mps_dprint(sc, MPS_INFO|MPS_RECOVERY, 1382 "Target reset complete for target %u, but still have %u " 1383 "command(s), resetting controller\n", targ->tid, 1384 targ->outstanding); 1385 mps_reinit(sc); 1386 } 1387 } 1388 1389 #define MPS_RESET_TIMEOUT 30 1390 1391 int 1392 mpssas_send_reset(struct mps_softc *sc, struct mps_command *tm, uint8_t type) 1393 { 1394 MPI2_SCSI_TASK_MANAGE_REQUEST *req; 1395 struct mpssas_target *target; 1396 int err; 1397 1398 target = tm->cm_targ; 1399 if (target->handle == 0) { 1400 mps_dprint(sc, MPS_ERROR,"%s null devhandle for target_id %d\n", 1401 __func__, target->tid); 1402 return -1; 1403 } 1404 1405 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req; 1406 req->DevHandle = htole16(target->handle); 1407 req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT; 1408 req->TaskType = type; 1409 1410 if (type == MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET) { 1411 /* XXX Need to handle invalid LUNs */ 1412 MPS_SET_LUN(req->LUN, tm->cm_lun); 1413 tm->cm_targ->logical_unit_resets++; 1414 mps_dprint(sc, MPS_RECOVERY|MPS_INFO, 1415 "Sending logical unit reset to target %u lun %d\n", 1416 target->tid, tm->cm_lun); 1417 tm->cm_complete = mpssas_logical_unit_reset_complete; 1418 mpssas_prepare_for_tm(sc, tm, target, tm->cm_lun); 1419 } else if (type == MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET) { 1420 /* 1421 * Target reset method = 1422 * SAS Hard Link Reset / SATA Link Reset 1423 */ 1424 req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET; 1425 tm->cm_targ->target_resets++; 1426 mps_dprint(sc, MPS_RECOVERY|MPS_INFO, 1427 "Sending target reset to target %u\n", target->tid); 1428 tm->cm_complete = mpssas_target_reset_complete; 1429 mpssas_prepare_for_tm(sc, tm, target, CAM_LUN_WILDCARD); 1430 } else { 1431 mps_dprint(sc, MPS_ERROR, "unexpected reset type 0x%x\n", type); 1432 return -1; 1433 } 1434 1435 tm->cm_data = NULL; 1436 tm->cm_desc.HighPriority.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY; 1437 tm->cm_complete_data = (void *)tm; 1438 1439 callout_reset(&tm->cm_callout, MPS_RESET_TIMEOUT * hz, 1440 mpssas_tm_timeout, tm); 1441 1442 err = mps_map_command(sc, tm); 1443 if (err) 1444 mps_dprint(sc, MPS_ERROR|MPS_RECOVERY, 1445 "error %d sending reset type %u\n", 1446 err, type); 1447 1448 return err; 1449 } 1450 1451 1452 static void 1453 mpssas_abort_complete(struct mps_softc *sc, struct mps_command *tm) 1454 { 1455 struct mps_command *cm; 1456 MPI2_SCSI_TASK_MANAGE_REPLY *reply; 1457 MPI2_SCSI_TASK_MANAGE_REQUEST *req; 1458 struct mpssas_target *targ; 1459 1460 callout_stop(&tm->cm_callout); 1461 1462 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req; 1463 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply; 1464 targ = tm->cm_targ; 1465 1466 /* 1467 * Currently there should be no way we can hit this case. It only 1468 * happens when we have a failure to allocate chain frames, and 1469 * task management commands don't have S/G lists. 1470 */ 1471 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) { 1472 mps_dprint(sc, MPS_RECOVERY, 1473 "cm_flags = %#x for abort %p TaskMID %u!\n", 1474 tm->cm_flags, tm, le16toh(req->TaskMID)); 1475 mpssas_free_tm(sc, tm); 1476 return; 1477 } 1478 1479 if (reply == NULL) { 1480 mps_dprint(sc, MPS_RECOVERY, 1481 "NULL abort reply for tm %p TaskMID %u\n", 1482 tm, le16toh(req->TaskMID)); 1483 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) { 1484 /* this completion was due to a reset, just cleanup */ 1485 mps_dprint(sc, MPS_RECOVERY, "Hardware undergoing " 1486 "reset, ignoring NULL abort reply\n"); 1487 targ->tm = NULL; 1488 mpssas_free_tm(sc, tm); 1489 } else { 1490 /* we should have gotten a reply. */ 1491 mps_dprint(sc, MPS_INFO|MPS_RECOVERY, "NULL reply on " 1492 "abort attempt, resetting controller\n"); 1493 mps_reinit(sc); 1494 } 1495 return; 1496 } 1497 1498 mps_dprint(sc, MPS_RECOVERY, 1499 "abort TaskMID %u status 0x%x code 0x%x count %u\n", 1500 le16toh(req->TaskMID), 1501 le16toh(reply->IOCStatus), le32toh(reply->ResponseCode), 1502 le32toh(reply->TerminationCount)); 1503 1504 cm = TAILQ_FIRST(&tm->cm_targ->timedout_commands); 1505 if (cm == NULL) { 1506 /* 1507 * If there are no more timedout commands, we're done with 1508 * error recovery for this target. 1509 */ 1510 mps_dprint(sc, MPS_INFO|MPS_RECOVERY, 1511 "Finished abort recovery for target %u\n", targ->tid); 1512 1513 targ->tm = NULL; 1514 mpssas_free_tm(sc, tm); 1515 } else if (le16toh(req->TaskMID) != cm->cm_desc.Default.SMID) { 1516 /* abort success, but we have more timedout commands to abort */ 1517 mps_dprint(sc, MPS_INFO|MPS_RECOVERY, 1518 "Continuing abort recovery for target %u\n", targ->tid); 1519 1520 mpssas_send_abort(sc, tm, cm); 1521 } else { 1522 /* we didn't get a command completion, so the abort 1523 * failed as far as we're concerned. escalate. 1524 */ 1525 mps_dprint(sc, MPS_RECOVERY, 1526 "Abort failed for target %u, sending logical unit reset\n", 1527 targ->tid); 1528 1529 mpssas_send_reset(sc, tm, 1530 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET); 1531 } 1532 } 1533 1534 #define MPS_ABORT_TIMEOUT 5 1535 1536 static int 1537 mpssas_send_abort(struct mps_softc *sc, struct mps_command *tm, struct mps_command *cm) 1538 { 1539 MPI2_SCSI_TASK_MANAGE_REQUEST *req; 1540 struct mpssas_target *targ; 1541 int err; 1542 1543 targ = cm->cm_targ; 1544 if (targ->handle == 0) { 1545 mps_dprint(sc, MPS_ERROR|MPS_RECOVERY, 1546 "%s null devhandle for target_id %d\n", 1547 __func__, cm->cm_ccb->ccb_h.target_id); 1548 return -1; 1549 } 1550 1551 mpssas_log_command(cm, MPS_RECOVERY|MPS_INFO, 1552 "Aborting command %p\n", cm); 1553 1554 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req; 1555 req->DevHandle = htole16(targ->handle); 1556 req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT; 1557 req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK; 1558 1559 /* XXX Need to handle invalid LUNs */ 1560 MPS_SET_LUN(req->LUN, cm->cm_ccb->ccb_h.target_lun); 1561 1562 req->TaskMID = htole16(cm->cm_desc.Default.SMID); 1563 1564 tm->cm_data = NULL; 1565 tm->cm_desc.HighPriority.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY; 1566 tm->cm_complete = mpssas_abort_complete; 1567 tm->cm_complete_data = (void *)tm; 1568 tm->cm_targ = cm->cm_targ; 1569 tm->cm_lun = cm->cm_lun; 1570 1571 callout_reset(&tm->cm_callout, MPS_ABORT_TIMEOUT * hz, 1572 mpssas_tm_timeout, tm); 1573 1574 targ->aborts++; 1575 1576 mpssas_prepare_for_tm(sc, tm, targ, tm->cm_lun); 1577 1578 err = mps_map_command(sc, tm); 1579 if (err) 1580 mps_dprint(sc, MPS_ERROR|MPS_RECOVERY, 1581 "error %d sending abort for cm %p SMID %u\n", 1582 err, cm, req->TaskMID); 1583 return err; 1584 } 1585 1586 static void 1587 mpssas_scsiio_timeout(void *data) 1588 { 1589 sbintime_t elapsed, now; 1590 union ccb *ccb; 1591 struct mps_softc *sc; 1592 struct mps_command *cm; 1593 struct mpssas_target *targ; 1594 1595 cm = (struct mps_command *)data; 1596 sc = cm->cm_sc; 1597 ccb = cm->cm_ccb; 1598 now = sbinuptime(); 1599 1600 MPS_FUNCTRACE(sc); 1601 mtx_assert(&sc->mps_mtx, MA_OWNED); 1602 1603 mps_dprint(sc, MPS_XINFO|MPS_RECOVERY, "Timeout checking cm %p\n", sc); 1604 1605 /* 1606 * Run the interrupt handler to make sure it's not pending. This 1607 * isn't perfect because the command could have already completed 1608 * and been re-used, though this is unlikely. 1609 */ 1610 mps_intr_locked(sc); 1611 if (cm->cm_state == MPS_CM_STATE_FREE) { 1612 mpssas_log_command(cm, MPS_XINFO, 1613 "SCSI command %p almost timed out\n", cm); 1614 return; 1615 } 1616 1617 if (cm->cm_ccb == NULL) { 1618 mps_dprint(sc, MPS_ERROR, "command timeout with NULL ccb\n"); 1619 return; 1620 } 1621 1622 targ = cm->cm_targ; 1623 targ->timeouts++; 1624 1625 elapsed = now - ccb->ccb_h.qos.sim_data; 1626 mpssas_log_command(cm, MPS_INFO|MPS_RECOVERY, 1627 "Command timeout on target %u(0x%04x) %d set, %d.%d elapsed\n", 1628 targ->tid, targ->handle, ccb->ccb_h.timeout, 1629 sbintime_getsec(elapsed), elapsed & 0xffffffff); 1630 1631 /* XXX first, check the firmware state, to see if it's still 1632 * operational. if not, do a diag reset. 1633 */ 1634 mpssas_set_ccbstatus(cm->cm_ccb, CAM_CMD_TIMEOUT); 1635 cm->cm_state = MPS_CM_STATE_TIMEDOUT; 1636 TAILQ_INSERT_TAIL(&targ->timedout_commands, cm, cm_recovery); 1637 1638 if (targ->tm != NULL) { 1639 /* target already in recovery, just queue up another 1640 * timedout command to be processed later. 1641 */ 1642 mps_dprint(sc, MPS_RECOVERY, 1643 "queued timedout cm %p for processing by tm %p\n", 1644 cm, targ->tm); 1645 } else if ((targ->tm = mpssas_alloc_tm(sc)) != NULL) { 1646 mps_dprint(sc, MPS_RECOVERY|MPS_INFO, 1647 "Sending abort to target %u for SMID %d\n", targ->tid, 1648 cm->cm_desc.Default.SMID); 1649 mps_dprint(sc, MPS_RECOVERY, "timedout cm %p allocated tm %p\n", 1650 cm, targ->tm); 1651 1652 /* start recovery by aborting the first timedout command */ 1653 mpssas_send_abort(sc, targ->tm, cm); 1654 } else { 1655 /* XXX queue this target up for recovery once a TM becomes 1656 * available. The firmware only has a limited number of 1657 * HighPriority credits for the high priority requests used 1658 * for task management, and we ran out. 1659 * 1660 * Isilon: don't worry about this for now, since we have 1661 * more credits than disks in an enclosure, and limit 1662 * ourselves to one TM per target for recovery. 1663 */ 1664 mps_dprint(sc, MPS_ERROR|MPS_RECOVERY, 1665 "timedout cm %p failed to allocate a tm\n", cm); 1666 } 1667 1668 } 1669 1670 static void 1671 mpssas_action_scsiio(struct mpssas_softc *sassc, union ccb *ccb) 1672 { 1673 MPI2_SCSI_IO_REQUEST *req; 1674 struct ccb_scsiio *csio; 1675 struct mps_softc *sc; 1676 struct mpssas_target *targ; 1677 struct mpssas_lun *lun; 1678 struct mps_command *cm; 1679 uint8_t i, lba_byte, *ref_tag_addr; 1680 uint16_t eedp_flags; 1681 uint32_t mpi_control; 1682 1683 sc = sassc->sc; 1684 MPS_FUNCTRACE(sc); 1685 mtx_assert(&sc->mps_mtx, MA_OWNED); 1686 1687 csio = &ccb->csio; 1688 KASSERT(csio->ccb_h.target_id < sassc->maxtargets, 1689 ("Target %d out of bounds in XPT_SCSI_IO\n", 1690 csio->ccb_h.target_id)); 1691 targ = &sassc->targets[csio->ccb_h.target_id]; 1692 mps_dprint(sc, MPS_TRACE, "ccb %p target flag %x\n", ccb, targ->flags); 1693 if (targ->handle == 0x0) { 1694 mps_dprint(sc, MPS_ERROR, "%s NULL handle for target %u\n", 1695 __func__, csio->ccb_h.target_id); 1696 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE); 1697 xpt_done(ccb); 1698 return; 1699 } 1700 if (targ->flags & MPS_TARGET_FLAGS_RAID_COMPONENT) { 1701 mps_dprint(sc, MPS_ERROR, "%s Raid component no SCSI IO " 1702 "supported %u\n", __func__, csio->ccb_h.target_id); 1703 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE); 1704 xpt_done(ccb); 1705 return; 1706 } 1707 /* 1708 * Sometimes, it is possible to get a command that is not "In 1709 * Progress" and was actually aborted by the upper layer. Check for 1710 * this here and complete the command without error. 1711 */ 1712 if (mpssas_get_ccbstatus(ccb) != CAM_REQ_INPROG) { 1713 mps_dprint(sc, MPS_TRACE, "%s Command is not in progress for " 1714 "target %u\n", __func__, csio->ccb_h.target_id); 1715 xpt_done(ccb); 1716 return; 1717 } 1718 /* 1719 * If devinfo is 0 this will be a volume. In that case don't tell CAM 1720 * that the volume has timed out. We want volumes to be enumerated 1721 * until they are deleted/removed, not just failed. 1722 */ 1723 if (targ->flags & MPSSAS_TARGET_INREMOVAL) { 1724 if (targ->devinfo == 0) 1725 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP); 1726 else 1727 mpssas_set_ccbstatus(ccb, CAM_SEL_TIMEOUT); 1728 xpt_done(ccb); 1729 return; 1730 } 1731 1732 if ((sc->mps_flags & MPS_FLAGS_SHUTDOWN) != 0) { 1733 mps_dprint(sc, MPS_INFO, "%s shutting down\n", __func__); 1734 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE); 1735 xpt_done(ccb); 1736 return; 1737 } 1738 1739 /* 1740 * If target has a reset in progress, freeze the devq and return. The 1741 * devq will be released when the TM reset is finished. 1742 */ 1743 if (targ->flags & MPSSAS_TARGET_INRESET) { 1744 ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN; 1745 mps_dprint(sc, MPS_INFO, "%s: Freezing devq for target ID %d\n", 1746 __func__, targ->tid); 1747 xpt_freeze_devq(ccb->ccb_h.path, 1); 1748 xpt_done(ccb); 1749 return; 1750 } 1751 1752 cm = mps_alloc_command(sc); 1753 if (cm == NULL || (sc->mps_flags & MPS_FLAGS_DIAGRESET)) { 1754 if (cm != NULL) { 1755 mps_free_command(sc, cm); 1756 } 1757 if ((sassc->flags & MPSSAS_QUEUE_FROZEN) == 0) { 1758 xpt_freeze_simq(sassc->sim, 1); 1759 sassc->flags |= MPSSAS_QUEUE_FROZEN; 1760 } 1761 ccb->ccb_h.status &= ~CAM_SIM_QUEUED; 1762 ccb->ccb_h.status |= CAM_REQUEUE_REQ; 1763 xpt_done(ccb); 1764 return; 1765 } 1766 1767 req = (MPI2_SCSI_IO_REQUEST *)cm->cm_req; 1768 bzero(req, sizeof(*req)); 1769 req->DevHandle = htole16(targ->handle); 1770 req->Function = MPI2_FUNCTION_SCSI_IO_REQUEST; 1771 req->MsgFlags = 0; 1772 req->SenseBufferLowAddress = htole32(cm->cm_sense_busaddr); 1773 req->SenseBufferLength = MPS_SENSE_LEN; 1774 req->SGLFlags = 0; 1775 req->ChainOffset = 0; 1776 req->SGLOffset0 = 24; /* 32bit word offset to the SGL */ 1777 req->SGLOffset1= 0; 1778 req->SGLOffset2= 0; 1779 req->SGLOffset3= 0; 1780 req->SkipCount = 0; 1781 req->DataLength = htole32(csio->dxfer_len); 1782 req->BidirectionalDataLength = 0; 1783 req->IoFlags = htole16(csio->cdb_len); 1784 req->EEDPFlags = 0; 1785 1786 /* Note: BiDirectional transfers are not supported */ 1787 switch (csio->ccb_h.flags & CAM_DIR_MASK) { 1788 case CAM_DIR_IN: 1789 mpi_control = MPI2_SCSIIO_CONTROL_READ; 1790 cm->cm_flags |= MPS_CM_FLAGS_DATAIN; 1791 break; 1792 case CAM_DIR_OUT: 1793 mpi_control = MPI2_SCSIIO_CONTROL_WRITE; 1794 cm->cm_flags |= MPS_CM_FLAGS_DATAOUT; 1795 break; 1796 case CAM_DIR_NONE: 1797 default: 1798 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER; 1799 break; 1800 } 1801 1802 if (csio->cdb_len == 32) 1803 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT; 1804 /* 1805 * It looks like the hardware doesn't require an explicit tag 1806 * number for each transaction. SAM Task Management not supported 1807 * at the moment. 1808 */ 1809 switch (csio->tag_action) { 1810 case MSG_HEAD_OF_Q_TAG: 1811 mpi_control |= MPI2_SCSIIO_CONTROL_HEADOFQ; 1812 break; 1813 case MSG_ORDERED_Q_TAG: 1814 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ; 1815 break; 1816 case MSG_ACA_TASK: 1817 mpi_control |= MPI2_SCSIIO_CONTROL_ACAQ; 1818 break; 1819 case CAM_TAG_ACTION_NONE: 1820 case MSG_SIMPLE_Q_TAG: 1821 default: 1822 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ; 1823 break; 1824 } 1825 mpi_control |= sc->mapping_table[csio->ccb_h.target_id].TLR_bits; 1826 req->Control = htole32(mpi_control); 1827 if (MPS_SET_LUN(req->LUN, csio->ccb_h.target_lun) != 0) { 1828 mps_free_command(sc, cm); 1829 mpssas_set_ccbstatus(ccb, CAM_LUN_INVALID); 1830 xpt_done(ccb); 1831 return; 1832 } 1833 1834 if (csio->ccb_h.flags & CAM_CDB_POINTER) 1835 bcopy(csio->cdb_io.cdb_ptr, &req->CDB.CDB32[0], csio->cdb_len); 1836 else 1837 bcopy(csio->cdb_io.cdb_bytes, &req->CDB.CDB32[0],csio->cdb_len); 1838 req->IoFlags = htole16(csio->cdb_len); 1839 1840 /* 1841 * Check if EEDP is supported and enabled. If it is then check if the 1842 * SCSI opcode could be using EEDP. If so, make sure the LUN exists and 1843 * is formatted for EEDP support. If all of this is true, set CDB up 1844 * for EEDP transfer. 1845 */ 1846 eedp_flags = op_code_prot[req->CDB.CDB32[0]]; 1847 if (sc->eedp_enabled && eedp_flags) { 1848 SLIST_FOREACH(lun, &targ->luns, lun_link) { 1849 if (lun->lun_id == csio->ccb_h.target_lun) { 1850 break; 1851 } 1852 } 1853 1854 if ((lun != NULL) && (lun->eedp_formatted)) { 1855 req->EEDPBlockSize = htole16(lun->eedp_block_size); 1856 eedp_flags |= (MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG | 1857 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG | 1858 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD); 1859 req->EEDPFlags = htole16(eedp_flags); 1860 1861 /* 1862 * If CDB less than 32, fill in Primary Ref Tag with 1863 * low 4 bytes of LBA. If CDB is 32, tag stuff is 1864 * already there. Also, set protection bit. FreeBSD 1865 * currently does not support CDBs bigger than 16, but 1866 * the code doesn't hurt, and will be here for the 1867 * future. 1868 */ 1869 if (csio->cdb_len != 32) { 1870 lba_byte = (csio->cdb_len == 16) ? 6 : 2; 1871 ref_tag_addr = (uint8_t *)&req->CDB.EEDP32. 1872 PrimaryReferenceTag; 1873 for (i = 0; i < 4; i++) { 1874 *ref_tag_addr = 1875 req->CDB.CDB32[lba_byte + i]; 1876 ref_tag_addr++; 1877 } 1878 req->CDB.EEDP32.PrimaryReferenceTag = 1879 htole32(req->CDB.EEDP32.PrimaryReferenceTag); 1880 req->CDB.EEDP32.PrimaryApplicationTagMask = 1881 0xFFFF; 1882 req->CDB.CDB32[1] = (req->CDB.CDB32[1] & 0x1F) | 1883 0x20; 1884 } else { 1885 eedp_flags |= 1886 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_APPTAG; 1887 req->EEDPFlags = htole16(eedp_flags); 1888 req->CDB.CDB32[10] = (req->CDB.CDB32[10] & 1889 0x1F) | 0x20; 1890 } 1891 } 1892 } 1893 1894 cm->cm_length = csio->dxfer_len; 1895 if (cm->cm_length != 0) { 1896 cm->cm_data = ccb; 1897 cm->cm_flags |= MPS_CM_FLAGS_USE_CCB; 1898 } else { 1899 cm->cm_data = NULL; 1900 } 1901 cm->cm_sge = &req->SGL; 1902 cm->cm_sglsize = (32 - 24) * 4; 1903 cm->cm_desc.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO; 1904 cm->cm_desc.SCSIIO.DevHandle = htole16(targ->handle); 1905 cm->cm_complete = mpssas_scsiio_complete; 1906 cm->cm_complete_data = ccb; 1907 cm->cm_targ = targ; 1908 cm->cm_lun = csio->ccb_h.target_lun; 1909 cm->cm_ccb = ccb; 1910 1911 /* 1912 * If HBA is a WD and the command is not for a retry, try to build a 1913 * direct I/O message. If failed, or the command is for a retry, send 1914 * the I/O to the IR volume itself. 1915 */ 1916 if (sc->WD_valid_config) { 1917 if (ccb->ccb_h.sim_priv.entries[0].field == MPS_WD_RETRY) { 1918 mpssas_direct_drive_io(sassc, cm, ccb); 1919 } else { 1920 mpssas_set_ccbstatus(ccb, CAM_REQ_INPROG); 1921 } 1922 } 1923 1924 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING) 1925 if (csio->bio != NULL) 1926 biotrack(csio->bio, __func__); 1927 #endif 1928 csio->ccb_h.qos.sim_data = sbinuptime(); 1929 callout_reset_sbt(&cm->cm_callout, SBT_1MS * ccb->ccb_h.timeout, 0, 1930 mpssas_scsiio_timeout, cm, 0); 1931 1932 targ->issued++; 1933 targ->outstanding++; 1934 TAILQ_INSERT_TAIL(&targ->commands, cm, cm_link); 1935 ccb->ccb_h.status |= CAM_SIM_QUEUED; 1936 1937 mpssas_log_command(cm, MPS_XINFO, "%s cm %p ccb %p outstanding %u\n", 1938 __func__, cm, ccb, targ->outstanding); 1939 1940 mps_map_command(sc, cm); 1941 return; 1942 } 1943 1944 /** 1945 * mps_sc_failed_io_info - translated non-succesfull SCSI_IO request 1946 */ 1947 static void 1948 mps_sc_failed_io_info(struct mps_softc *sc, struct ccb_scsiio *csio, 1949 Mpi2SCSIIOReply_t *mpi_reply) 1950 { 1951 u32 response_info; 1952 u8 *response_bytes; 1953 u16 ioc_status = le16toh(mpi_reply->IOCStatus) & 1954 MPI2_IOCSTATUS_MASK; 1955 u8 scsi_state = mpi_reply->SCSIState; 1956 u8 scsi_status = mpi_reply->SCSIStatus; 1957 u32 log_info = le32toh(mpi_reply->IOCLogInfo); 1958 const char *desc_ioc_state, *desc_scsi_status; 1959 1960 if (log_info == 0x31170000) 1961 return; 1962 1963 desc_ioc_state = mps_describe_table(mps_iocstatus_string, 1964 ioc_status); 1965 desc_scsi_status = mps_describe_table(mps_scsi_status_string, 1966 scsi_status); 1967 1968 mps_dprint(sc, MPS_XINFO, "\thandle(0x%04x), ioc_status(%s)(0x%04x)\n", 1969 le16toh(mpi_reply->DevHandle), desc_ioc_state, ioc_status); 1970 1971 /* 1972 *We can add more detail about underflow data here 1973 * TO-DO 1974 */ 1975 mps_dprint(sc, MPS_XINFO, "\tscsi_status(%s)(0x%02x), " 1976 "scsi_state %b\n", desc_scsi_status, scsi_status, 1977 scsi_state, "\20" "\1AutosenseValid" "\2AutosenseFailed" 1978 "\3NoScsiStatus" "\4Terminated" "\5Response InfoValid"); 1979 1980 if (sc->mps_debug & MPS_XINFO && 1981 scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) { 1982 mps_dprint(sc, MPS_XINFO, "-> Sense Buffer Data : Start :\n"); 1983 scsi_sense_print(csio); 1984 mps_dprint(sc, MPS_XINFO, "-> Sense Buffer Data : End :\n"); 1985 } 1986 1987 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) { 1988 response_info = le32toh(mpi_reply->ResponseInfo); 1989 response_bytes = (u8 *)&response_info; 1990 mps_dprint(sc, MPS_XINFO, "response code(0x%1x): %s\n", 1991 response_bytes[0], 1992 mps_describe_table(mps_scsi_taskmgmt_string, 1993 response_bytes[0])); 1994 } 1995 } 1996 1997 static void 1998 mpssas_scsiio_complete(struct mps_softc *sc, struct mps_command *cm) 1999 { 2000 MPI2_SCSI_IO_REPLY *rep; 2001 union ccb *ccb; 2002 struct ccb_scsiio *csio; 2003 struct mpssas_softc *sassc; 2004 struct scsi_vpd_supported_page_list *vpd_list = NULL; 2005 u8 *TLR_bits, TLR_on; 2006 int dir = 0, i; 2007 u16 alloc_len; 2008 struct mpssas_target *target; 2009 target_id_t target_id; 2010 2011 MPS_FUNCTRACE(sc); 2012 mps_dprint(sc, MPS_TRACE, 2013 "cm %p SMID %u ccb %p reply %p outstanding %u\n", cm, 2014 cm->cm_desc.Default.SMID, cm->cm_ccb, cm->cm_reply, 2015 cm->cm_targ->outstanding); 2016 2017 callout_stop(&cm->cm_callout); 2018 mtx_assert(&sc->mps_mtx, MA_OWNED); 2019 2020 sassc = sc->sassc; 2021 ccb = cm->cm_complete_data; 2022 csio = &ccb->csio; 2023 target_id = csio->ccb_h.target_id; 2024 rep = (MPI2_SCSI_IO_REPLY *)cm->cm_reply; 2025 /* 2026 * XXX KDM if the chain allocation fails, does it matter if we do 2027 * the sync and unload here? It is simpler to do it in every case, 2028 * assuming it doesn't cause problems. 2029 */ 2030 if (cm->cm_data != NULL) { 2031 if (cm->cm_flags & MPS_CM_FLAGS_DATAIN) 2032 dir = BUS_DMASYNC_POSTREAD; 2033 else if (cm->cm_flags & MPS_CM_FLAGS_DATAOUT) 2034 dir = BUS_DMASYNC_POSTWRITE; 2035 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir); 2036 bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap); 2037 } 2038 2039 cm->cm_targ->completed++; 2040 cm->cm_targ->outstanding--; 2041 TAILQ_REMOVE(&cm->cm_targ->commands, cm, cm_link); 2042 ccb->ccb_h.status &= ~(CAM_STATUS_MASK | CAM_SIM_QUEUED); 2043 2044 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING) 2045 if (ccb->csio.bio != NULL) 2046 biotrack(ccb->csio.bio, __func__); 2047 #endif 2048 2049 if (cm->cm_state == MPS_CM_STATE_TIMEDOUT) { 2050 TAILQ_REMOVE(&cm->cm_targ->timedout_commands, cm, cm_recovery); 2051 if (cm->cm_reply != NULL) 2052 mpssas_log_command(cm, MPS_RECOVERY, 2053 "completed timedout cm %p ccb %p during recovery " 2054 "ioc %x scsi %x state %x xfer %u\n", 2055 cm, cm->cm_ccb, 2056 le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState, 2057 le32toh(rep->TransferCount)); 2058 else 2059 mpssas_log_command(cm, MPS_RECOVERY, 2060 "completed timedout cm %p ccb %p during recovery\n", 2061 cm, cm->cm_ccb); 2062 } else if (cm->cm_targ->tm != NULL) { 2063 if (cm->cm_reply != NULL) 2064 mpssas_log_command(cm, MPS_RECOVERY, 2065 "completed cm %p ccb %p during recovery " 2066 "ioc %x scsi %x state %x xfer %u\n", 2067 cm, cm->cm_ccb, 2068 le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState, 2069 le32toh(rep->TransferCount)); 2070 else 2071 mpssas_log_command(cm, MPS_RECOVERY, 2072 "completed cm %p ccb %p during recovery\n", 2073 cm, cm->cm_ccb); 2074 } else if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) { 2075 mpssas_log_command(cm, MPS_RECOVERY, 2076 "reset completed cm %p ccb %p\n", 2077 cm, cm->cm_ccb); 2078 } 2079 2080 if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) { 2081 /* 2082 * We ran into an error after we tried to map the command, 2083 * so we're getting a callback without queueing the command 2084 * to the hardware. So we set the status here, and it will 2085 * be retained below. We'll go through the "fast path", 2086 * because there can be no reply when we haven't actually 2087 * gone out to the hardware. 2088 */ 2089 mpssas_set_ccbstatus(ccb, CAM_REQUEUE_REQ); 2090 2091 /* 2092 * Currently the only error included in the mask is 2093 * MPS_CM_FLAGS_CHAIN_FAILED, which means we're out of 2094 * chain frames. We need to freeze the queue until we get 2095 * a command that completed without this error, which will 2096 * hopefully have some chain frames attached that we can 2097 * use. If we wanted to get smarter about it, we would 2098 * only unfreeze the queue in this condition when we're 2099 * sure that we're getting some chain frames back. That's 2100 * probably unnecessary. 2101 */ 2102 if ((sassc->flags & MPSSAS_QUEUE_FROZEN) == 0) { 2103 xpt_freeze_simq(sassc->sim, 1); 2104 sassc->flags |= MPSSAS_QUEUE_FROZEN; 2105 mps_dprint(sc, MPS_XINFO, "Error sending command, " 2106 "freezing SIM queue\n"); 2107 } 2108 } 2109 2110 /* 2111 * If this is a Start Stop Unit command and it was issued by the driver 2112 * during shutdown, decrement the refcount to account for all of the 2113 * commands that were sent. All SSU commands should be completed before 2114 * shutdown completes, meaning SSU_refcount will be 0 after SSU_started 2115 * is TRUE. 2116 */ 2117 if (sc->SSU_started && (csio->cdb_io.cdb_bytes[0] == START_STOP_UNIT)) { 2118 mps_dprint(sc, MPS_INFO, "Decrementing SSU count.\n"); 2119 sc->SSU_refcount--; 2120 } 2121 2122 /* Take the fast path to completion */ 2123 if (cm->cm_reply == NULL) { 2124 if (mpssas_get_ccbstatus(ccb) == CAM_REQ_INPROG) { 2125 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) 2126 mpssas_set_ccbstatus(ccb, CAM_SCSI_BUS_RESET); 2127 else { 2128 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP); 2129 ccb->csio.scsi_status = SCSI_STATUS_OK; 2130 } 2131 if (sassc->flags & MPSSAS_QUEUE_FROZEN) { 2132 ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 2133 sassc->flags &= ~MPSSAS_QUEUE_FROZEN; 2134 mps_dprint(sc, MPS_XINFO, 2135 "Unfreezing SIM queue\n"); 2136 } 2137 } 2138 2139 /* 2140 * There are two scenarios where the status won't be 2141 * CAM_REQ_CMP. The first is if MPS_CM_FLAGS_ERROR_MASK is 2142 * set, the second is in the MPS_FLAGS_DIAGRESET above. 2143 */ 2144 if (mpssas_get_ccbstatus(ccb) != CAM_REQ_CMP) { 2145 /* 2146 * Freeze the dev queue so that commands are 2147 * executed in the correct order after error 2148 * recovery. 2149 */ 2150 ccb->ccb_h.status |= CAM_DEV_QFRZN; 2151 xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1); 2152 } 2153 mps_free_command(sc, cm); 2154 xpt_done(ccb); 2155 return; 2156 } 2157 2158 mpssas_log_command(cm, MPS_XINFO, 2159 "ioc %x scsi %x state %x xfer %u\n", 2160 le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState, 2161 le32toh(rep->TransferCount)); 2162 2163 /* 2164 * If this is a Direct Drive I/O, reissue the I/O to the original IR 2165 * Volume if an error occurred (normal I/O retry). Use the original 2166 * CCB, but set a flag that this will be a retry so that it's sent to 2167 * the original volume. Free the command but reuse the CCB. 2168 */ 2169 if (cm->cm_flags & MPS_CM_FLAGS_DD_IO) { 2170 mps_free_command(sc, cm); 2171 ccb->ccb_h.sim_priv.entries[0].field = MPS_WD_RETRY; 2172 mpssas_action_scsiio(sassc, ccb); 2173 return; 2174 } else 2175 ccb->ccb_h.sim_priv.entries[0].field = 0; 2176 2177 switch (le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK) { 2178 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN: 2179 csio->resid = cm->cm_length - le32toh(rep->TransferCount); 2180 /* FALLTHROUGH */ 2181 case MPI2_IOCSTATUS_SUCCESS: 2182 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR: 2183 2184 if ((le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK) == 2185 MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR) 2186 mpssas_log_command(cm, MPS_XINFO, "recovered error\n"); 2187 2188 /* Completion failed at the transport level. */ 2189 if (rep->SCSIState & (MPI2_SCSI_STATE_NO_SCSI_STATUS | 2190 MPI2_SCSI_STATE_TERMINATED)) { 2191 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR); 2192 break; 2193 } 2194 2195 /* In a modern packetized environment, an autosense failure 2196 * implies that there's not much else that can be done to 2197 * recover the command. 2198 */ 2199 if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_FAILED) { 2200 mpssas_set_ccbstatus(ccb, CAM_AUTOSENSE_FAIL); 2201 break; 2202 } 2203 2204 /* 2205 * CAM doesn't care about SAS Response Info data, but if this is 2206 * the state check if TLR should be done. If not, clear the 2207 * TLR_bits for the target. 2208 */ 2209 if ((rep->SCSIState & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) && 2210 ((le32toh(rep->ResponseInfo) & 2211 MPI2_SCSI_RI_MASK_REASONCODE) == 2212 MPS_SCSI_RI_INVALID_FRAME)) { 2213 sc->mapping_table[target_id].TLR_bits = 2214 (u8)MPI2_SCSIIO_CONTROL_NO_TLR; 2215 } 2216 2217 /* 2218 * Intentionally override the normal SCSI status reporting 2219 * for these two cases. These are likely to happen in a 2220 * multi-initiator environment, and we want to make sure that 2221 * CAM retries these commands rather than fail them. 2222 */ 2223 if ((rep->SCSIStatus == MPI2_SCSI_STATUS_COMMAND_TERMINATED) || 2224 (rep->SCSIStatus == MPI2_SCSI_STATUS_TASK_ABORTED)) { 2225 mpssas_set_ccbstatus(ccb, CAM_REQ_ABORTED); 2226 break; 2227 } 2228 2229 /* Handle normal status and sense */ 2230 csio->scsi_status = rep->SCSIStatus; 2231 if (rep->SCSIStatus == MPI2_SCSI_STATUS_GOOD) 2232 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP); 2233 else 2234 mpssas_set_ccbstatus(ccb, CAM_SCSI_STATUS_ERROR); 2235 2236 if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_VALID) { 2237 int sense_len, returned_sense_len; 2238 2239 returned_sense_len = min(le32toh(rep->SenseCount), 2240 sizeof(struct scsi_sense_data)); 2241 if (returned_sense_len < ccb->csio.sense_len) 2242 ccb->csio.sense_resid = ccb->csio.sense_len - 2243 returned_sense_len; 2244 else 2245 ccb->csio.sense_resid = 0; 2246 2247 sense_len = min(returned_sense_len, 2248 ccb->csio.sense_len - ccb->csio.sense_resid); 2249 bzero(&ccb->csio.sense_data, 2250 sizeof(ccb->csio.sense_data)); 2251 bcopy(cm->cm_sense, &ccb->csio.sense_data, sense_len); 2252 ccb->ccb_h.status |= CAM_AUTOSNS_VALID; 2253 } 2254 2255 /* 2256 * Check if this is an INQUIRY command. If it's a VPD inquiry, 2257 * and it's page code 0 (Supported Page List), and there is 2258 * inquiry data, and this is for a sequential access device, and 2259 * the device is an SSP target, and TLR is supported by the 2260 * controller, turn the TLR_bits value ON if page 0x90 is 2261 * supported. 2262 */ 2263 if ((csio->cdb_io.cdb_bytes[0] == INQUIRY) && 2264 (csio->cdb_io.cdb_bytes[1] & SI_EVPD) && 2265 (csio->cdb_io.cdb_bytes[2] == SVPD_SUPPORTED_PAGE_LIST) && 2266 ((csio->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR) && 2267 (csio->data_ptr != NULL) && 2268 ((csio->data_ptr[0] & 0x1f) == T_SEQUENTIAL) && 2269 (sc->control_TLR) && 2270 (sc->mapping_table[target_id].device_info & 2271 MPI2_SAS_DEVICE_INFO_SSP_TARGET)) { 2272 vpd_list = (struct scsi_vpd_supported_page_list *) 2273 csio->data_ptr; 2274 TLR_bits = &sc->mapping_table[target_id].TLR_bits; 2275 *TLR_bits = (u8)MPI2_SCSIIO_CONTROL_NO_TLR; 2276 TLR_on = (u8)MPI2_SCSIIO_CONTROL_TLR_ON; 2277 alloc_len = ((u16)csio->cdb_io.cdb_bytes[3] << 8) + 2278 csio->cdb_io.cdb_bytes[4]; 2279 alloc_len -= csio->resid; 2280 for (i = 0; i < MIN(vpd_list->length, alloc_len); i++) { 2281 if (vpd_list->list[i] == 0x90) { 2282 *TLR_bits = TLR_on; 2283 break; 2284 } 2285 } 2286 } 2287 2288 /* 2289 * If this is a SATA direct-access end device, mark it so that 2290 * a SCSI StartStopUnit command will be sent to it when the 2291 * driver is being shutdown. 2292 */ 2293 if ((csio->cdb_io.cdb_bytes[0] == INQUIRY) && 2294 ((csio->data_ptr[0] & 0x1f) == T_DIRECT) && 2295 (sc->mapping_table[target_id].device_info & 2296 MPI2_SAS_DEVICE_INFO_SATA_DEVICE) && 2297 ((sc->mapping_table[target_id].device_info & 2298 MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) == 2299 MPI2_SAS_DEVICE_INFO_END_DEVICE)) { 2300 target = &sassc->targets[target_id]; 2301 target->supports_SSU = TRUE; 2302 mps_dprint(sc, MPS_XINFO, "Target %d supports SSU\n", 2303 target_id); 2304 } 2305 break; 2306 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE: 2307 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE: 2308 /* 2309 * If devinfo is 0 this will be a volume. In that case don't 2310 * tell CAM that the volume is not there. We want volumes to 2311 * be enumerated until they are deleted/removed, not just 2312 * failed. 2313 */ 2314 if (cm->cm_targ->devinfo == 0) 2315 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP); 2316 else 2317 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE); 2318 break; 2319 case MPI2_IOCSTATUS_INVALID_SGL: 2320 mps_print_scsiio_cmd(sc, cm); 2321 mpssas_set_ccbstatus(ccb, CAM_UNREC_HBA_ERROR); 2322 break; 2323 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED: 2324 /* 2325 * This is one of the responses that comes back when an I/O 2326 * has been aborted. If it is because of a timeout that we 2327 * initiated, just set the status to CAM_CMD_TIMEOUT. 2328 * Otherwise set it to CAM_REQ_ABORTED. The effect on the 2329 * command is the same (it gets retried, subject to the 2330 * retry counter), the only difference is what gets printed 2331 * on the console. 2332 */ 2333 if (cm->cm_state == MPS_CM_STATE_TIMEDOUT) 2334 mpssas_set_ccbstatus(ccb, CAM_CMD_TIMEOUT); 2335 else 2336 mpssas_set_ccbstatus(ccb, CAM_REQ_ABORTED); 2337 break; 2338 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN: 2339 /* resid is ignored for this condition */ 2340 csio->resid = 0; 2341 mpssas_set_ccbstatus(ccb, CAM_DATA_RUN_ERR); 2342 break; 2343 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED: 2344 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED: 2345 /* 2346 * These can sometimes be transient transport-related 2347 * errors, and sometimes persistent drive-related errors. 2348 * We used to retry these without decrementing the retry 2349 * count by returning CAM_REQUEUE_REQ. Unfortunately, if 2350 * we hit a persistent drive problem that returns one of 2351 * these error codes, we would retry indefinitely. So, 2352 * return CAM_REQ_CMP_ERROR so that we decrement the retry 2353 * count and avoid infinite retries. We're taking the 2354 * potential risk of flagging false failures in the event 2355 * of a topology-related error (e.g. a SAS expander problem 2356 * causes a command addressed to a drive to fail), but 2357 * avoiding getting into an infinite retry loop. 2358 */ 2359 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR); 2360 mps_dprint(sc, MPS_INFO, 2361 "Controller reported %s tgt %u SMID %u loginfo %x\n", 2362 mps_describe_table(mps_iocstatus_string, 2363 le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK), 2364 target_id, cm->cm_desc.Default.SMID, 2365 le32toh(rep->IOCLogInfo)); 2366 mps_dprint(sc, MPS_XINFO, 2367 "SCSIStatus %x SCSIState %x xfercount %u\n", 2368 rep->SCSIStatus, rep->SCSIState, 2369 le32toh(rep->TransferCount)); 2370 break; 2371 case MPI2_IOCSTATUS_INVALID_FUNCTION: 2372 case MPI2_IOCSTATUS_INTERNAL_ERROR: 2373 case MPI2_IOCSTATUS_INVALID_VPID: 2374 case MPI2_IOCSTATUS_INVALID_FIELD: 2375 case MPI2_IOCSTATUS_INVALID_STATE: 2376 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED: 2377 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR: 2378 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR: 2379 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH: 2380 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED: 2381 default: 2382 mpssas_log_command(cm, MPS_XINFO, 2383 "completed ioc %x loginfo %x scsi %x state %x xfer %u\n", 2384 le16toh(rep->IOCStatus), le32toh(rep->IOCLogInfo), 2385 rep->SCSIStatus, rep->SCSIState, 2386 le32toh(rep->TransferCount)); 2387 csio->resid = cm->cm_length; 2388 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR); 2389 break; 2390 } 2391 2392 mps_sc_failed_io_info(sc,csio,rep); 2393 2394 if (sassc->flags & MPSSAS_QUEUE_FROZEN) { 2395 ccb->ccb_h.status |= CAM_RELEASE_SIMQ; 2396 sassc->flags &= ~MPSSAS_QUEUE_FROZEN; 2397 mps_dprint(sc, MPS_XINFO, "Command completed, " 2398 "unfreezing SIM queue\n"); 2399 } 2400 2401 if (mpssas_get_ccbstatus(ccb) != CAM_REQ_CMP) { 2402 ccb->ccb_h.status |= CAM_DEV_QFRZN; 2403 xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1); 2404 } 2405 2406 mps_free_command(sc, cm); 2407 xpt_done(ccb); 2408 } 2409 2410 /* All Request reached here are Endian safe */ 2411 static void 2412 mpssas_direct_drive_io(struct mpssas_softc *sassc, struct mps_command *cm, 2413 union ccb *ccb) { 2414 pMpi2SCSIIORequest_t pIO_req; 2415 struct mps_softc *sc = sassc->sc; 2416 uint64_t virtLBA; 2417 uint32_t physLBA, stripe_offset, stripe_unit; 2418 uint32_t io_size, column; 2419 uint8_t *ptrLBA, lba_idx, physLBA_byte, *CDB; 2420 2421 /* 2422 * If this is a valid SCSI command (Read6, Read10, Read16, Write6, 2423 * Write10, or Write16), build a direct I/O message. Otherwise, the I/O 2424 * will be sent to the IR volume itself. Since Read6 and Write6 are a 2425 * bit different than the 10/16 CDBs, handle them separately. 2426 */ 2427 pIO_req = (pMpi2SCSIIORequest_t)cm->cm_req; 2428 CDB = pIO_req->CDB.CDB32; 2429 2430 /* 2431 * Handle 6 byte CDBs. 2432 */ 2433 if ((pIO_req->DevHandle == sc->DD_dev_handle) && ((CDB[0] == READ_6) || 2434 (CDB[0] == WRITE_6))) { 2435 /* 2436 * Get the transfer size in blocks. 2437 */ 2438 io_size = (cm->cm_length >> sc->DD_block_exponent); 2439 2440 /* 2441 * Get virtual LBA given in the CDB. 2442 */ 2443 virtLBA = ((uint64_t)(CDB[1] & 0x1F) << 16) | 2444 ((uint64_t)CDB[2] << 8) | (uint64_t)CDB[3]; 2445 2446 /* 2447 * Check that LBA range for I/O does not exceed volume's 2448 * MaxLBA. 2449 */ 2450 if ((virtLBA + (uint64_t)io_size - 1) <= 2451 sc->DD_max_lba) { 2452 /* 2453 * Check if the I/O crosses a stripe boundary. If not, 2454 * translate the virtual LBA to a physical LBA and set 2455 * the DevHandle for the PhysDisk to be used. If it 2456 * does cross a boundary, do normal I/O. To get the 2457 * right DevHandle to use, get the map number for the 2458 * column, then use that map number to look up the 2459 * DevHandle of the PhysDisk. 2460 */ 2461 stripe_offset = (uint32_t)virtLBA & 2462 (sc->DD_stripe_size - 1); 2463 if ((stripe_offset + io_size) <= sc->DD_stripe_size) { 2464 physLBA = (uint32_t)virtLBA >> 2465 sc->DD_stripe_exponent; 2466 stripe_unit = physLBA / sc->DD_num_phys_disks; 2467 column = physLBA % sc->DD_num_phys_disks; 2468 pIO_req->DevHandle = 2469 htole16(sc->DD_column_map[column].dev_handle); 2470 /* ???? Is this endian safe*/ 2471 cm->cm_desc.SCSIIO.DevHandle = 2472 pIO_req->DevHandle; 2473 2474 physLBA = (stripe_unit << 2475 sc->DD_stripe_exponent) + stripe_offset; 2476 ptrLBA = &pIO_req->CDB.CDB32[1]; 2477 physLBA_byte = (uint8_t)(physLBA >> 16); 2478 *ptrLBA = physLBA_byte; 2479 ptrLBA = &pIO_req->CDB.CDB32[2]; 2480 physLBA_byte = (uint8_t)(physLBA >> 8); 2481 *ptrLBA = physLBA_byte; 2482 ptrLBA = &pIO_req->CDB.CDB32[3]; 2483 physLBA_byte = (uint8_t)physLBA; 2484 *ptrLBA = physLBA_byte; 2485 2486 /* 2487 * Set flag that Direct Drive I/O is 2488 * being done. 2489 */ 2490 cm->cm_flags |= MPS_CM_FLAGS_DD_IO; 2491 } 2492 } 2493 return; 2494 } 2495 2496 /* 2497 * Handle 10, 12 or 16 byte CDBs. 2498 */ 2499 if ((pIO_req->DevHandle == sc->DD_dev_handle) && ((CDB[0] == READ_10) || 2500 (CDB[0] == WRITE_10) || (CDB[0] == READ_16) || 2501 (CDB[0] == WRITE_16) || (CDB[0] == READ_12) || 2502 (CDB[0] == WRITE_12))) { 2503 /* 2504 * For 16-byte CDB's, verify that the upper 4 bytes of the CDB 2505 * are 0. If not, this is accessing beyond 2TB so handle it in 2506 * the else section. 10-byte and 12-byte CDB's are OK. 2507 * FreeBSD sends very rare 12 byte READ/WRITE, but driver is 2508 * ready to accept 12byte CDB for Direct IOs. 2509 */ 2510 if ((CDB[0] == READ_10 || CDB[0] == WRITE_10) || 2511 (CDB[0] == READ_12 || CDB[0] == WRITE_12) || 2512 !(CDB[2] | CDB[3] | CDB[4] | CDB[5])) { 2513 /* 2514 * Get the transfer size in blocks. 2515 */ 2516 io_size = (cm->cm_length >> sc->DD_block_exponent); 2517 2518 /* 2519 * Get virtual LBA. Point to correct lower 4 bytes of 2520 * LBA in the CDB depending on command. 2521 */ 2522 lba_idx = ((CDB[0] == READ_12) || 2523 (CDB[0] == WRITE_12) || 2524 (CDB[0] == READ_10) || 2525 (CDB[0] == WRITE_10))? 2 : 6; 2526 virtLBA = ((uint64_t)CDB[lba_idx] << 24) | 2527 ((uint64_t)CDB[lba_idx + 1] << 16) | 2528 ((uint64_t)CDB[lba_idx + 2] << 8) | 2529 (uint64_t)CDB[lba_idx + 3]; 2530 2531 /* 2532 * Check that LBA range for I/O does not exceed volume's 2533 * MaxLBA. 2534 */ 2535 if ((virtLBA + (uint64_t)io_size - 1) <= 2536 sc->DD_max_lba) { 2537 /* 2538 * Check if the I/O crosses a stripe boundary. 2539 * If not, translate the virtual LBA to a 2540 * physical LBA and set the DevHandle for the 2541 * PhysDisk to be used. If it does cross a 2542 * boundary, do normal I/O. To get the right 2543 * DevHandle to use, get the map number for the 2544 * column, then use that map number to look up 2545 * the DevHandle of the PhysDisk. 2546 */ 2547 stripe_offset = (uint32_t)virtLBA & 2548 (sc->DD_stripe_size - 1); 2549 if ((stripe_offset + io_size) <= 2550 sc->DD_stripe_size) { 2551 physLBA = (uint32_t)virtLBA >> 2552 sc->DD_stripe_exponent; 2553 stripe_unit = physLBA / 2554 sc->DD_num_phys_disks; 2555 column = physLBA % 2556 sc->DD_num_phys_disks; 2557 pIO_req->DevHandle = 2558 htole16(sc->DD_column_map[column]. 2559 dev_handle); 2560 cm->cm_desc.SCSIIO.DevHandle = 2561 pIO_req->DevHandle; 2562 2563 physLBA = (stripe_unit << 2564 sc->DD_stripe_exponent) + 2565 stripe_offset; 2566 ptrLBA = 2567 &pIO_req->CDB.CDB32[lba_idx]; 2568 physLBA_byte = (uint8_t)(physLBA >> 24); 2569 *ptrLBA = physLBA_byte; 2570 ptrLBA = 2571 &pIO_req->CDB.CDB32[lba_idx + 1]; 2572 physLBA_byte = (uint8_t)(physLBA >> 16); 2573 *ptrLBA = physLBA_byte; 2574 ptrLBA = 2575 &pIO_req->CDB.CDB32[lba_idx + 2]; 2576 physLBA_byte = (uint8_t)(physLBA >> 8); 2577 *ptrLBA = physLBA_byte; 2578 ptrLBA = 2579 &pIO_req->CDB.CDB32[lba_idx + 3]; 2580 physLBA_byte = (uint8_t)physLBA; 2581 *ptrLBA = physLBA_byte; 2582 2583 /* 2584 * Set flag that Direct Drive I/O is 2585 * being done. 2586 */ 2587 cm->cm_flags |= MPS_CM_FLAGS_DD_IO; 2588 } 2589 } 2590 } else { 2591 /* 2592 * 16-byte CDB and the upper 4 bytes of the CDB are not 2593 * 0. Get the transfer size in blocks. 2594 */ 2595 io_size = (cm->cm_length >> sc->DD_block_exponent); 2596 2597 /* 2598 * Get virtual LBA. 2599 */ 2600 virtLBA = ((uint64_t)CDB[2] << 54) | 2601 ((uint64_t)CDB[3] << 48) | 2602 ((uint64_t)CDB[4] << 40) | 2603 ((uint64_t)CDB[5] << 32) | 2604 ((uint64_t)CDB[6] << 24) | 2605 ((uint64_t)CDB[7] << 16) | 2606 ((uint64_t)CDB[8] << 8) | 2607 (uint64_t)CDB[9]; 2608 2609 /* 2610 * Check that LBA range for I/O does not exceed volume's 2611 * MaxLBA. 2612 */ 2613 if ((virtLBA + (uint64_t)io_size - 1) <= 2614 sc->DD_max_lba) { 2615 /* 2616 * Check if the I/O crosses a stripe boundary. 2617 * If not, translate the virtual LBA to a 2618 * physical LBA and set the DevHandle for the 2619 * PhysDisk to be used. If it does cross a 2620 * boundary, do normal I/O. To get the right 2621 * DevHandle to use, get the map number for the 2622 * column, then use that map number to look up 2623 * the DevHandle of the PhysDisk. 2624 */ 2625 stripe_offset = (uint32_t)virtLBA & 2626 (sc->DD_stripe_size - 1); 2627 if ((stripe_offset + io_size) <= 2628 sc->DD_stripe_size) { 2629 physLBA = (uint32_t)(virtLBA >> 2630 sc->DD_stripe_exponent); 2631 stripe_unit = physLBA / 2632 sc->DD_num_phys_disks; 2633 column = physLBA % 2634 sc->DD_num_phys_disks; 2635 pIO_req->DevHandle = 2636 htole16(sc->DD_column_map[column]. 2637 dev_handle); 2638 cm->cm_desc.SCSIIO.DevHandle = 2639 pIO_req->DevHandle; 2640 2641 physLBA = (stripe_unit << 2642 sc->DD_stripe_exponent) + 2643 stripe_offset; 2644 2645 /* 2646 * Set upper 4 bytes of LBA to 0. We 2647 * assume that the phys disks are less 2648 * than 2 TB's in size. Then, set the 2649 * lower 4 bytes. 2650 */ 2651 pIO_req->CDB.CDB32[2] = 0; 2652 pIO_req->CDB.CDB32[3] = 0; 2653 pIO_req->CDB.CDB32[4] = 0; 2654 pIO_req->CDB.CDB32[5] = 0; 2655 ptrLBA = &pIO_req->CDB.CDB32[6]; 2656 physLBA_byte = (uint8_t)(physLBA >> 24); 2657 *ptrLBA = physLBA_byte; 2658 ptrLBA = &pIO_req->CDB.CDB32[7]; 2659 physLBA_byte = (uint8_t)(physLBA >> 16); 2660 *ptrLBA = physLBA_byte; 2661 ptrLBA = &pIO_req->CDB.CDB32[8]; 2662 physLBA_byte = (uint8_t)(physLBA >> 8); 2663 *ptrLBA = physLBA_byte; 2664 ptrLBA = &pIO_req->CDB.CDB32[9]; 2665 physLBA_byte = (uint8_t)physLBA; 2666 *ptrLBA = physLBA_byte; 2667 2668 /* 2669 * Set flag that Direct Drive I/O is 2670 * being done. 2671 */ 2672 cm->cm_flags |= MPS_CM_FLAGS_DD_IO; 2673 } 2674 } 2675 } 2676 } 2677 } 2678 2679 #if __FreeBSD_version >= 900026 2680 static void 2681 mpssas_smpio_complete(struct mps_softc *sc, struct mps_command *cm) 2682 { 2683 MPI2_SMP_PASSTHROUGH_REPLY *rpl; 2684 MPI2_SMP_PASSTHROUGH_REQUEST *req; 2685 uint64_t sasaddr; 2686 union ccb *ccb; 2687 2688 ccb = cm->cm_complete_data; 2689 2690 /* 2691 * Currently there should be no way we can hit this case. It only 2692 * happens when we have a failure to allocate chain frames, and SMP 2693 * commands require two S/G elements only. That should be handled 2694 * in the standard request size. 2695 */ 2696 if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) { 2697 mps_dprint(sc, MPS_ERROR,"%s: cm_flags = %#x on SMP request!\n", 2698 __func__, cm->cm_flags); 2699 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR); 2700 goto bailout; 2701 } 2702 2703 rpl = (MPI2_SMP_PASSTHROUGH_REPLY *)cm->cm_reply; 2704 if (rpl == NULL) { 2705 mps_dprint(sc, MPS_ERROR, "%s: NULL cm_reply!\n", __func__); 2706 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR); 2707 goto bailout; 2708 } 2709 2710 req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req; 2711 sasaddr = le32toh(req->SASAddress.Low); 2712 sasaddr |= ((uint64_t)(le32toh(req->SASAddress.High))) << 32; 2713 2714 if ((le16toh(rpl->IOCStatus) & MPI2_IOCSTATUS_MASK) != 2715 MPI2_IOCSTATUS_SUCCESS || 2716 rpl->SASStatus != MPI2_SASSTATUS_SUCCESS) { 2717 mps_dprint(sc, MPS_XINFO, "%s: IOCStatus %04x SASStatus %02x\n", 2718 __func__, le16toh(rpl->IOCStatus), rpl->SASStatus); 2719 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR); 2720 goto bailout; 2721 } 2722 2723 mps_dprint(sc, MPS_XINFO, "%s: SMP request to SAS address " 2724 "%#jx completed successfully\n", __func__, 2725 (uintmax_t)sasaddr); 2726 2727 if (ccb->smpio.smp_response[2] == SMP_FR_ACCEPTED) 2728 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP); 2729 else 2730 mpssas_set_ccbstatus(ccb, CAM_SMP_STATUS_ERROR); 2731 2732 bailout: 2733 /* 2734 * We sync in both directions because we had DMAs in the S/G list 2735 * in both directions. 2736 */ 2737 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, 2738 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 2739 bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap); 2740 mps_free_command(sc, cm); 2741 xpt_done(ccb); 2742 } 2743 2744 static void 2745 mpssas_send_smpcmd(struct mpssas_softc *sassc, union ccb *ccb, uint64_t sasaddr) 2746 { 2747 struct mps_command *cm; 2748 uint8_t *request, *response; 2749 MPI2_SMP_PASSTHROUGH_REQUEST *req; 2750 struct mps_softc *sc; 2751 int error; 2752 2753 sc = sassc->sc; 2754 error = 0; 2755 2756 /* 2757 * XXX We don't yet support physical addresses here. 2758 */ 2759 switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) { 2760 case CAM_DATA_PADDR: 2761 case CAM_DATA_SG_PADDR: 2762 mps_dprint(sc, MPS_ERROR, 2763 "%s: physical addresses not supported\n", __func__); 2764 mpssas_set_ccbstatus(ccb, CAM_REQ_INVALID); 2765 xpt_done(ccb); 2766 return; 2767 case CAM_DATA_SG: 2768 /* 2769 * The chip does not support more than one buffer for the 2770 * request or response. 2771 */ 2772 if ((ccb->smpio.smp_request_sglist_cnt > 1) 2773 || (ccb->smpio.smp_response_sglist_cnt > 1)) { 2774 mps_dprint(sc, MPS_ERROR, 2775 "%s: multiple request or response " 2776 "buffer segments not supported for SMP\n", 2777 __func__); 2778 mpssas_set_ccbstatus(ccb, CAM_REQ_INVALID); 2779 xpt_done(ccb); 2780 return; 2781 } 2782 2783 /* 2784 * The CAM_SCATTER_VALID flag was originally implemented 2785 * for the XPT_SCSI_IO CCB, which only has one data pointer. 2786 * We have two. So, just take that flag to mean that we 2787 * might have S/G lists, and look at the S/G segment count 2788 * to figure out whether that is the case for each individual 2789 * buffer. 2790 */ 2791 if (ccb->smpio.smp_request_sglist_cnt != 0) { 2792 bus_dma_segment_t *req_sg; 2793 2794 req_sg = (bus_dma_segment_t *)ccb->smpio.smp_request; 2795 request = (uint8_t *)(uintptr_t)req_sg[0].ds_addr; 2796 } else 2797 request = ccb->smpio.smp_request; 2798 2799 if (ccb->smpio.smp_response_sglist_cnt != 0) { 2800 bus_dma_segment_t *rsp_sg; 2801 2802 rsp_sg = (bus_dma_segment_t *)ccb->smpio.smp_response; 2803 response = (uint8_t *)(uintptr_t)rsp_sg[0].ds_addr; 2804 } else 2805 response = ccb->smpio.smp_response; 2806 break; 2807 case CAM_DATA_VADDR: 2808 request = ccb->smpio.smp_request; 2809 response = ccb->smpio.smp_response; 2810 break; 2811 default: 2812 mpssas_set_ccbstatus(ccb, CAM_REQ_INVALID); 2813 xpt_done(ccb); 2814 return; 2815 } 2816 2817 cm = mps_alloc_command(sc); 2818 if (cm == NULL) { 2819 mps_dprint(sc, MPS_ERROR, 2820 "%s: cannot allocate command\n", __func__); 2821 mpssas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL); 2822 xpt_done(ccb); 2823 return; 2824 } 2825 2826 req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req; 2827 bzero(req, sizeof(*req)); 2828 req->Function = MPI2_FUNCTION_SMP_PASSTHROUGH; 2829 2830 /* Allow the chip to use any route to this SAS address. */ 2831 req->PhysicalPort = 0xff; 2832 2833 req->RequestDataLength = htole16(ccb->smpio.smp_request_len); 2834 req->SGLFlags = 2835 MPI2_SGLFLAGS_SYSTEM_ADDRESS_SPACE | MPI2_SGLFLAGS_SGL_TYPE_MPI; 2836 2837 mps_dprint(sc, MPS_XINFO, "%s: sending SMP request to SAS " 2838 "address %#jx\n", __func__, (uintmax_t)sasaddr); 2839 2840 mpi_init_sge(cm, req, &req->SGL); 2841 2842 /* 2843 * Set up a uio to pass into mps_map_command(). This allows us to 2844 * do one map command, and one busdma call in there. 2845 */ 2846 cm->cm_uio.uio_iov = cm->cm_iovec; 2847 cm->cm_uio.uio_iovcnt = 2; 2848 cm->cm_uio.uio_segflg = UIO_SYSSPACE; 2849 2850 /* 2851 * The read/write flag isn't used by busdma, but set it just in 2852 * case. This isn't exactly accurate, either, since we're going in 2853 * both directions. 2854 */ 2855 cm->cm_uio.uio_rw = UIO_WRITE; 2856 2857 cm->cm_iovec[0].iov_base = request; 2858 cm->cm_iovec[0].iov_len = le16toh(req->RequestDataLength); 2859 cm->cm_iovec[1].iov_base = response; 2860 cm->cm_iovec[1].iov_len = ccb->smpio.smp_response_len; 2861 2862 cm->cm_uio.uio_resid = cm->cm_iovec[0].iov_len + 2863 cm->cm_iovec[1].iov_len; 2864 2865 /* 2866 * Trigger a warning message in mps_data_cb() for the user if we 2867 * wind up exceeding two S/G segments. The chip expects one 2868 * segment for the request and another for the response. 2869 */ 2870 cm->cm_max_segs = 2; 2871 2872 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE; 2873 cm->cm_complete = mpssas_smpio_complete; 2874 cm->cm_complete_data = ccb; 2875 2876 /* 2877 * Tell the mapping code that we're using a uio, and that this is 2878 * an SMP passthrough request. There is a little special-case 2879 * logic there (in mps_data_cb()) to handle the bidirectional 2880 * transfer. 2881 */ 2882 cm->cm_flags |= MPS_CM_FLAGS_USE_UIO | MPS_CM_FLAGS_SMP_PASS | 2883 MPS_CM_FLAGS_DATAIN | MPS_CM_FLAGS_DATAOUT; 2884 2885 /* The chip data format is little endian. */ 2886 req->SASAddress.High = htole32(sasaddr >> 32); 2887 req->SASAddress.Low = htole32(sasaddr); 2888 2889 /* 2890 * XXX Note that we don't have a timeout/abort mechanism here. 2891 * From the manual, it looks like task management requests only 2892 * work for SCSI IO and SATA passthrough requests. We may need to 2893 * have a mechanism to retry requests in the event of a chip reset 2894 * at least. Hopefully the chip will insure that any errors short 2895 * of that are relayed back to the driver. 2896 */ 2897 error = mps_map_command(sc, cm); 2898 if ((error != 0) && (error != EINPROGRESS)) { 2899 mps_dprint(sc, MPS_ERROR, 2900 "%s: error %d returned from mps_map_command()\n", 2901 __func__, error); 2902 goto bailout_error; 2903 } 2904 2905 return; 2906 2907 bailout_error: 2908 mps_free_command(sc, cm); 2909 mpssas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL); 2910 xpt_done(ccb); 2911 return; 2912 2913 } 2914 2915 static void 2916 mpssas_action_smpio(struct mpssas_softc *sassc, union ccb *ccb) 2917 { 2918 struct mps_softc *sc; 2919 struct mpssas_target *targ; 2920 uint64_t sasaddr = 0; 2921 2922 sc = sassc->sc; 2923 2924 /* 2925 * Make sure the target exists. 2926 */ 2927 KASSERT(ccb->ccb_h.target_id < sassc->maxtargets, 2928 ("Target %d out of bounds in XPT_SMP_IO\n", ccb->ccb_h.target_id)); 2929 targ = &sassc->targets[ccb->ccb_h.target_id]; 2930 if (targ->handle == 0x0) { 2931 mps_dprint(sc, MPS_ERROR, 2932 "%s: target %d does not exist!\n", __func__, 2933 ccb->ccb_h.target_id); 2934 mpssas_set_ccbstatus(ccb, CAM_SEL_TIMEOUT); 2935 xpt_done(ccb); 2936 return; 2937 } 2938 2939 /* 2940 * If this device has an embedded SMP target, we'll talk to it 2941 * directly. 2942 * figure out what the expander's address is. 2943 */ 2944 if ((targ->devinfo & MPI2_SAS_DEVICE_INFO_SMP_TARGET) != 0) 2945 sasaddr = targ->sasaddr; 2946 2947 /* 2948 * If we don't have a SAS address for the expander yet, try 2949 * grabbing it from the page 0x83 information cached in the 2950 * transport layer for this target. LSI expanders report the 2951 * expander SAS address as the port-associated SAS address in 2952 * Inquiry VPD page 0x83. Maxim expanders don't report it in page 2953 * 0x83. 2954 * 2955 * XXX KDM disable this for now, but leave it commented out so that 2956 * it is obvious that this is another possible way to get the SAS 2957 * address. 2958 * 2959 * The parent handle method below is a little more reliable, and 2960 * the other benefit is that it works for devices other than SES 2961 * devices. So you can send a SMP request to a da(4) device and it 2962 * will get routed to the expander that device is attached to. 2963 * (Assuming the da(4) device doesn't contain an SMP target...) 2964 */ 2965 #if 0 2966 if (sasaddr == 0) 2967 sasaddr = xpt_path_sas_addr(ccb->ccb_h.path); 2968 #endif 2969 2970 /* 2971 * If we still don't have a SAS address for the expander, look for 2972 * the parent device of this device, which is probably the expander. 2973 */ 2974 if (sasaddr == 0) { 2975 #ifdef OLD_MPS_PROBE 2976 struct mpssas_target *parent_target; 2977 #endif 2978 2979 if (targ->parent_handle == 0x0) { 2980 mps_dprint(sc, MPS_ERROR, 2981 "%s: handle %d does not have a valid " 2982 "parent handle!\n", __func__, targ->handle); 2983 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE); 2984 goto bailout; 2985 } 2986 #ifdef OLD_MPS_PROBE 2987 parent_target = mpssas_find_target_by_handle(sassc, 0, 2988 targ->parent_handle); 2989 2990 if (parent_target == NULL) { 2991 mps_dprint(sc, MPS_ERROR, 2992 "%s: handle %d does not have a valid " 2993 "parent target!\n", __func__, targ->handle); 2994 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE); 2995 goto bailout; 2996 } 2997 2998 if ((parent_target->devinfo & 2999 MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0) { 3000 mps_dprint(sc, MPS_ERROR, 3001 "%s: handle %d parent %d does not " 3002 "have an SMP target!\n", __func__, 3003 targ->handle, parent_target->handle); 3004 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE); 3005 goto bailout; 3006 3007 } 3008 3009 sasaddr = parent_target->sasaddr; 3010 #else /* OLD_MPS_PROBE */ 3011 if ((targ->parent_devinfo & 3012 MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0) { 3013 mps_dprint(sc, MPS_ERROR, 3014 "%s: handle %d parent %d does not " 3015 "have an SMP target!\n", __func__, 3016 targ->handle, targ->parent_handle); 3017 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE); 3018 goto bailout; 3019 3020 } 3021 if (targ->parent_sasaddr == 0x0) { 3022 mps_dprint(sc, MPS_ERROR, 3023 "%s: handle %d parent handle %d does " 3024 "not have a valid SAS address!\n", 3025 __func__, targ->handle, targ->parent_handle); 3026 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE); 3027 goto bailout; 3028 } 3029 3030 sasaddr = targ->parent_sasaddr; 3031 #endif /* OLD_MPS_PROBE */ 3032 3033 } 3034 3035 if (sasaddr == 0) { 3036 mps_dprint(sc, MPS_INFO, 3037 "%s: unable to find SAS address for handle %d\n", 3038 __func__, targ->handle); 3039 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE); 3040 goto bailout; 3041 } 3042 mpssas_send_smpcmd(sassc, ccb, sasaddr); 3043 3044 return; 3045 3046 bailout: 3047 xpt_done(ccb); 3048 3049 } 3050 #endif //__FreeBSD_version >= 900026 3051 3052 static void 3053 mpssas_action_resetdev(struct mpssas_softc *sassc, union ccb *ccb) 3054 { 3055 MPI2_SCSI_TASK_MANAGE_REQUEST *req; 3056 struct mps_softc *sc; 3057 struct mps_command *tm; 3058 struct mpssas_target *targ; 3059 3060 MPS_FUNCTRACE(sassc->sc); 3061 mtx_assert(&sassc->sc->mps_mtx, MA_OWNED); 3062 3063 KASSERT(ccb->ccb_h.target_id < sassc->maxtargets, 3064 ("Target %d out of bounds in XPT_RESET_DEV\n", 3065 ccb->ccb_h.target_id)); 3066 sc = sassc->sc; 3067 tm = mps_alloc_command(sc); 3068 if (tm == NULL) { 3069 mps_dprint(sc, MPS_ERROR, 3070 "command alloc failure in mpssas_action_resetdev\n"); 3071 mpssas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL); 3072 xpt_done(ccb); 3073 return; 3074 } 3075 3076 targ = &sassc->targets[ccb->ccb_h.target_id]; 3077 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req; 3078 req->DevHandle = htole16(targ->handle); 3079 req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT; 3080 req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET; 3081 3082 /* SAS Hard Link Reset / SATA Link Reset */ 3083 req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET; 3084 3085 tm->cm_data = NULL; 3086 tm->cm_desc.HighPriority.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY; 3087 tm->cm_complete = mpssas_resetdev_complete; 3088 tm->cm_complete_data = ccb; 3089 tm->cm_targ = targ; 3090 targ->flags |= MPSSAS_TARGET_INRESET; 3091 3092 mps_map_command(sc, tm); 3093 } 3094 3095 static void 3096 mpssas_resetdev_complete(struct mps_softc *sc, struct mps_command *tm) 3097 { 3098 MPI2_SCSI_TASK_MANAGE_REPLY *resp; 3099 union ccb *ccb; 3100 3101 MPS_FUNCTRACE(sc); 3102 mtx_assert(&sc->mps_mtx, MA_OWNED); 3103 3104 resp = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply; 3105 ccb = tm->cm_complete_data; 3106 3107 /* 3108 * Currently there should be no way we can hit this case. It only 3109 * happens when we have a failure to allocate chain frames, and 3110 * task management commands don't have S/G lists. 3111 */ 3112 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) { 3113 MPI2_SCSI_TASK_MANAGE_REQUEST *req; 3114 3115 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req; 3116 3117 mps_dprint(sc, MPS_ERROR, 3118 "%s: cm_flags = %#x for reset of handle %#04x! " 3119 "This should not happen!\n", __func__, tm->cm_flags, 3120 req->DevHandle); 3121 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR); 3122 goto bailout; 3123 } 3124 3125 mps_dprint(sc, MPS_XINFO, 3126 "%s: IOCStatus = 0x%x ResponseCode = 0x%x\n", __func__, 3127 le16toh(resp->IOCStatus), le32toh(resp->ResponseCode)); 3128 3129 if (le32toh(resp->ResponseCode) == MPI2_SCSITASKMGMT_RSP_TM_COMPLETE) { 3130 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP); 3131 mpssas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid, 3132 CAM_LUN_WILDCARD); 3133 } 3134 else 3135 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR); 3136 3137 bailout: 3138 3139 mpssas_free_tm(sc, tm); 3140 xpt_done(ccb); 3141 } 3142 3143 static void 3144 mpssas_poll(struct cam_sim *sim) 3145 { 3146 struct mpssas_softc *sassc; 3147 3148 sassc = cam_sim_softc(sim); 3149 3150 if (sassc->sc->mps_debug & MPS_TRACE) { 3151 /* frequent debug messages during a panic just slow 3152 * everything down too much. 3153 */ 3154 mps_printf(sassc->sc, "%s clearing MPS_TRACE\n", __func__); 3155 sassc->sc->mps_debug &= ~MPS_TRACE; 3156 } 3157 3158 mps_intr_locked(sassc->sc); 3159 } 3160 3161 static void 3162 mpssas_async(void *callback_arg, uint32_t code, struct cam_path *path, 3163 void *arg) 3164 { 3165 struct mps_softc *sc; 3166 3167 sc = (struct mps_softc *)callback_arg; 3168 3169 switch (code) { 3170 #if (__FreeBSD_version >= 1000006) || \ 3171 ((__FreeBSD_version >= 901503) && (__FreeBSD_version < 1000000)) 3172 case AC_ADVINFO_CHANGED: { 3173 struct mpssas_target *target; 3174 struct mpssas_softc *sassc; 3175 struct scsi_read_capacity_data_long rcap_buf; 3176 struct ccb_dev_advinfo cdai; 3177 struct mpssas_lun *lun; 3178 lun_id_t lunid; 3179 int found_lun; 3180 uintptr_t buftype; 3181 3182 buftype = (uintptr_t)arg; 3183 3184 found_lun = 0; 3185 sassc = sc->sassc; 3186 3187 /* 3188 * We're only interested in read capacity data changes. 3189 */ 3190 if (buftype != CDAI_TYPE_RCAPLONG) 3191 break; 3192 3193 /* 3194 * We should have a handle for this, but check to make sure. 3195 */ 3196 KASSERT(xpt_path_target_id(path) < sassc->maxtargets, 3197 ("Target %d out of bounds in mpssas_async\n", 3198 xpt_path_target_id(path))); 3199 target = &sassc->targets[xpt_path_target_id(path)]; 3200 if (target->handle == 0) 3201 break; 3202 3203 lunid = xpt_path_lun_id(path); 3204 3205 SLIST_FOREACH(lun, &target->luns, lun_link) { 3206 if (lun->lun_id == lunid) { 3207 found_lun = 1; 3208 break; 3209 } 3210 } 3211 3212 if (found_lun == 0) { 3213 lun = malloc(sizeof(struct mpssas_lun), M_MPT2, 3214 M_NOWAIT | M_ZERO); 3215 if (lun == NULL) { 3216 mps_dprint(sc, MPS_ERROR, "Unable to alloc " 3217 "LUN for EEDP support.\n"); 3218 break; 3219 } 3220 lun->lun_id = lunid; 3221 SLIST_INSERT_HEAD(&target->luns, lun, lun_link); 3222 } 3223 3224 bzero(&rcap_buf, sizeof(rcap_buf)); 3225 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL); 3226 cdai.ccb_h.func_code = XPT_DEV_ADVINFO; 3227 cdai.ccb_h.flags = CAM_DIR_IN; 3228 cdai.buftype = CDAI_TYPE_RCAPLONG; 3229 #if (__FreeBSD_version >= 1100061) || \ 3230 ((__FreeBSD_version >= 1001510) && (__FreeBSD_version < 1100000)) 3231 cdai.flags = CDAI_FLAG_NONE; 3232 #else 3233 cdai.flags = 0; 3234 #endif 3235 cdai.bufsiz = sizeof(rcap_buf); 3236 cdai.buf = (uint8_t *)&rcap_buf; 3237 xpt_action((union ccb *)&cdai); 3238 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0) 3239 cam_release_devq(cdai.ccb_h.path, 3240 0, 0, 0, FALSE); 3241 3242 if ((mpssas_get_ccbstatus((union ccb *)&cdai) == CAM_REQ_CMP) 3243 && (rcap_buf.prot & SRC16_PROT_EN)) { 3244 lun->eedp_formatted = TRUE; 3245 lun->eedp_block_size = scsi_4btoul(rcap_buf.length); 3246 } else { 3247 lun->eedp_formatted = FALSE; 3248 lun->eedp_block_size = 0; 3249 } 3250 break; 3251 } 3252 #else 3253 case AC_FOUND_DEVICE: { 3254 struct ccb_getdev *cgd; 3255 3256 cgd = arg; 3257 mpssas_check_eedp(sc, path, cgd); 3258 break; 3259 } 3260 #endif 3261 default: 3262 break; 3263 } 3264 } 3265 3266 #if (__FreeBSD_version < 901503) || \ 3267 ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000006)) 3268 static void 3269 mpssas_check_eedp(struct mps_softc *sc, struct cam_path *path, 3270 struct ccb_getdev *cgd) 3271 { 3272 struct mpssas_softc *sassc = sc->sassc; 3273 struct ccb_scsiio *csio; 3274 struct scsi_read_capacity_16 *scsi_cmd; 3275 struct scsi_read_capacity_eedp *rcap_buf; 3276 path_id_t pathid; 3277 target_id_t targetid; 3278 lun_id_t lunid; 3279 union ccb *ccb; 3280 struct cam_path *local_path; 3281 struct mpssas_target *target; 3282 struct mpssas_lun *lun; 3283 uint8_t found_lun; 3284 char path_str[64]; 3285 3286 sassc = sc->sassc; 3287 pathid = cam_sim_path(sassc->sim); 3288 targetid = xpt_path_target_id(path); 3289 lunid = xpt_path_lun_id(path); 3290 3291 KASSERT(targetid < sassc->maxtargets, 3292 ("Target %d out of bounds in mpssas_check_eedp\n", 3293 targetid)); 3294 target = &sassc->targets[targetid]; 3295 if (target->handle == 0x0) 3296 return; 3297 3298 /* 3299 * Determine if the device is EEDP capable. 3300 * 3301 * If this flag is set in the inquiry data, 3302 * the device supports protection information, 3303 * and must support the 16 byte read 3304 * capacity command, otherwise continue without 3305 * sending read cap 16 3306 */ 3307 if ((cgd->inq_data.spc3_flags & SPC3_SID_PROTECT) == 0) 3308 return; 3309 3310 /* 3311 * Issue a READ CAPACITY 16 command. This info 3312 * is used to determine if the LUN is formatted 3313 * for EEDP support. 3314 */ 3315 ccb = xpt_alloc_ccb_nowait(); 3316 if (ccb == NULL) { 3317 mps_dprint(sc, MPS_ERROR, "Unable to alloc CCB " 3318 "for EEDP support.\n"); 3319 return; 3320 } 3321 3322 if (xpt_create_path(&local_path, xpt_periph, 3323 pathid, targetid, lunid) != CAM_REQ_CMP) { 3324 mps_dprint(sc, MPS_ERROR, "Unable to create " 3325 "path for EEDP support\n"); 3326 xpt_free_ccb(ccb); 3327 return; 3328 } 3329 3330 /* 3331 * If LUN is already in list, don't create a new 3332 * one. 3333 */ 3334 found_lun = FALSE; 3335 SLIST_FOREACH(lun, &target->luns, lun_link) { 3336 if (lun->lun_id == lunid) { 3337 found_lun = TRUE; 3338 break; 3339 } 3340 } 3341 if (!found_lun) { 3342 lun = malloc(sizeof(struct mpssas_lun), M_MPT2, 3343 M_NOWAIT | M_ZERO); 3344 if (lun == NULL) { 3345 mps_dprint(sc, MPS_ERROR, 3346 "Unable to alloc LUN for EEDP support.\n"); 3347 xpt_free_path(local_path); 3348 xpt_free_ccb(ccb); 3349 return; 3350 } 3351 lun->lun_id = lunid; 3352 SLIST_INSERT_HEAD(&target->luns, lun, 3353 lun_link); 3354 } 3355 3356 xpt_path_string(local_path, path_str, sizeof(path_str)); 3357 3358 mps_dprint(sc, MPS_INFO, "Sending read cap: path %s handle %d\n", 3359 path_str, target->handle); 3360 3361 /* 3362 * Issue a READ CAPACITY 16 command for the LUN. 3363 * The mpssas_read_cap_done function will load 3364 * the read cap info into the LUN struct. 3365 */ 3366 rcap_buf = malloc(sizeof(struct scsi_read_capacity_eedp), 3367 M_MPT2, M_NOWAIT | M_ZERO); 3368 if (rcap_buf == NULL) { 3369 mps_dprint(sc, MPS_FAULT, 3370 "Unable to alloc read capacity buffer for EEDP support.\n"); 3371 xpt_free_path(ccb->ccb_h.path); 3372 xpt_free_ccb(ccb); 3373 return; 3374 } 3375 xpt_setup_ccb(&ccb->ccb_h, local_path, CAM_PRIORITY_XPT); 3376 csio = &ccb->csio; 3377 csio->ccb_h.func_code = XPT_SCSI_IO; 3378 csio->ccb_h.flags = CAM_DIR_IN; 3379 csio->ccb_h.retry_count = 4; 3380 csio->ccb_h.cbfcnp = mpssas_read_cap_done; 3381 csio->ccb_h.timeout = 60000; 3382 csio->data_ptr = (uint8_t *)rcap_buf; 3383 csio->dxfer_len = sizeof(struct scsi_read_capacity_eedp); 3384 csio->sense_len = MPS_SENSE_LEN; 3385 csio->cdb_len = sizeof(*scsi_cmd); 3386 csio->tag_action = MSG_SIMPLE_Q_TAG; 3387 3388 scsi_cmd = (struct scsi_read_capacity_16 *)&csio->cdb_io.cdb_bytes; 3389 bzero(scsi_cmd, sizeof(*scsi_cmd)); 3390 scsi_cmd->opcode = 0x9E; 3391 scsi_cmd->service_action = SRC16_SERVICE_ACTION; 3392 ((uint8_t *)scsi_cmd)[13] = sizeof(struct scsi_read_capacity_eedp); 3393 3394 ccb->ccb_h.ppriv_ptr1 = sassc; 3395 xpt_action(ccb); 3396 } 3397 3398 static void 3399 mpssas_read_cap_done(struct cam_periph *periph, union ccb *done_ccb) 3400 { 3401 struct mpssas_softc *sassc; 3402 struct mpssas_target *target; 3403 struct mpssas_lun *lun; 3404 struct scsi_read_capacity_eedp *rcap_buf; 3405 3406 if (done_ccb == NULL) 3407 return; 3408 3409 /* Driver need to release devq, it Scsi command is 3410 * generated by driver internally. 3411 * Currently there is a single place where driver 3412 * calls scsi command internally. In future if driver 3413 * calls more scsi command internally, it needs to release 3414 * devq internally, since those command will not go back to 3415 * cam_periph. 3416 */ 3417 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) ) { 3418 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN; 3419 xpt_release_devq(done_ccb->ccb_h.path, 3420 /*count*/ 1, /*run_queue*/TRUE); 3421 } 3422 3423 rcap_buf = (struct scsi_read_capacity_eedp *)done_ccb->csio.data_ptr; 3424 3425 /* 3426 * Get the LUN ID for the path and look it up in the LUN list for the 3427 * target. 3428 */ 3429 sassc = (struct mpssas_softc *)done_ccb->ccb_h.ppriv_ptr1; 3430 KASSERT(done_ccb->ccb_h.target_id < sassc->maxtargets, 3431 ("Target %d out of bounds in mpssas_read_cap_done\n", 3432 done_ccb->ccb_h.target_id)); 3433 target = &sassc->targets[done_ccb->ccb_h.target_id]; 3434 SLIST_FOREACH(lun, &target->luns, lun_link) { 3435 if (lun->lun_id != done_ccb->ccb_h.target_lun) 3436 continue; 3437 3438 /* 3439 * Got the LUN in the target's LUN list. Fill it in 3440 * with EEDP info. If the READ CAP 16 command had some 3441 * SCSI error (common if command is not supported), mark 3442 * the lun as not supporting EEDP and set the block size 3443 * to 0. 3444 */ 3445 if ((mpssas_get_ccbstatus(done_ccb) != CAM_REQ_CMP) 3446 || (done_ccb->csio.scsi_status != SCSI_STATUS_OK)) { 3447 lun->eedp_formatted = FALSE; 3448 lun->eedp_block_size = 0; 3449 break; 3450 } 3451 3452 if (rcap_buf->protect & 0x01) { 3453 mps_dprint(sassc->sc, MPS_INFO, "LUN %d for " 3454 "target ID %d is formatted for EEDP " 3455 "support.\n", done_ccb->ccb_h.target_lun, 3456 done_ccb->ccb_h.target_id); 3457 lun->eedp_formatted = TRUE; 3458 lun->eedp_block_size = scsi_4btoul(rcap_buf->length); 3459 } 3460 break; 3461 } 3462 3463 // Finished with this CCB and path. 3464 free(rcap_buf, M_MPT2); 3465 xpt_free_path(done_ccb->ccb_h.path); 3466 xpt_free_ccb(done_ccb); 3467 } 3468 #endif /* (__FreeBSD_version < 901503) || \ 3469 ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000006)) */ 3470 3471 void 3472 mpssas_prepare_for_tm(struct mps_softc *sc, struct mps_command *tm, 3473 struct mpssas_target *target, lun_id_t lun_id) 3474 { 3475 union ccb *ccb; 3476 path_id_t path_id; 3477 3478 /* 3479 * Set the INRESET flag for this target so that no I/O will be sent to 3480 * the target until the reset has completed. If an I/O request does 3481 * happen, the devq will be frozen. The CCB holds the path which is 3482 * used to release the devq. The devq is released and the CCB is freed 3483 * when the TM completes. 3484 */ 3485 ccb = xpt_alloc_ccb_nowait(); 3486 if (ccb) { 3487 path_id = cam_sim_path(sc->sassc->sim); 3488 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, path_id, 3489 target->tid, lun_id) != CAM_REQ_CMP) { 3490 xpt_free_ccb(ccb); 3491 } else { 3492 tm->cm_ccb = ccb; 3493 tm->cm_targ = target; 3494 target->flags |= MPSSAS_TARGET_INRESET; 3495 } 3496 } 3497 } 3498 3499 int 3500 mpssas_startup(struct mps_softc *sc) 3501 { 3502 3503 /* 3504 * Send the port enable message and set the wait_for_port_enable flag. 3505 * This flag helps to keep the simq frozen until all discovery events 3506 * are processed. 3507 */ 3508 sc->wait_for_port_enable = 1; 3509 mpssas_send_portenable(sc); 3510 return (0); 3511 } 3512 3513 static int 3514 mpssas_send_portenable(struct mps_softc *sc) 3515 { 3516 MPI2_PORT_ENABLE_REQUEST *request; 3517 struct mps_command *cm; 3518 3519 MPS_FUNCTRACE(sc); 3520 3521 if ((cm = mps_alloc_command(sc)) == NULL) 3522 return (EBUSY); 3523 request = (MPI2_PORT_ENABLE_REQUEST *)cm->cm_req; 3524 request->Function = MPI2_FUNCTION_PORT_ENABLE; 3525 request->MsgFlags = 0; 3526 request->VP_ID = 0; 3527 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE; 3528 cm->cm_complete = mpssas_portenable_complete; 3529 cm->cm_data = NULL; 3530 cm->cm_sge = NULL; 3531 3532 mps_map_command(sc, cm); 3533 mps_dprint(sc, MPS_XINFO, 3534 "mps_send_portenable finished cm %p req %p complete %p\n", 3535 cm, cm->cm_req, cm->cm_complete); 3536 return (0); 3537 } 3538 3539 static void 3540 mpssas_portenable_complete(struct mps_softc *sc, struct mps_command *cm) 3541 { 3542 MPI2_PORT_ENABLE_REPLY *reply; 3543 struct mpssas_softc *sassc; 3544 3545 MPS_FUNCTRACE(sc); 3546 sassc = sc->sassc; 3547 3548 /* 3549 * Currently there should be no way we can hit this case. It only 3550 * happens when we have a failure to allocate chain frames, and 3551 * port enable commands don't have S/G lists. 3552 */ 3553 if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) { 3554 mps_dprint(sc, MPS_ERROR, "%s: cm_flags = %#x for port enable! " 3555 "This should not happen!\n", __func__, cm->cm_flags); 3556 } 3557 3558 reply = (MPI2_PORT_ENABLE_REPLY *)cm->cm_reply; 3559 if (reply == NULL) 3560 mps_dprint(sc, MPS_FAULT, "Portenable NULL reply\n"); 3561 else if (le16toh(reply->IOCStatus & MPI2_IOCSTATUS_MASK) != 3562 MPI2_IOCSTATUS_SUCCESS) 3563 mps_dprint(sc, MPS_FAULT, "Portenable failed\n"); 3564 3565 mps_free_command(sc, cm); 3566 3567 /* 3568 * Get WarpDrive info after discovery is complete but before the scan 3569 * starts. At this point, all devices are ready to be exposed to the 3570 * OS. If devices should be hidden instead, take them out of the 3571 * 'targets' array before the scan. The devinfo for a disk will have 3572 * some info and a volume's will be 0. Use that to remove disks. 3573 */ 3574 mps_wd_config_pages(sc); 3575 3576 /* 3577 * Done waiting for port enable to complete. Decrement the refcount. 3578 * If refcount is 0, discovery is complete and a rescan of the bus can 3579 * take place. Since the simq was explicitly frozen before port 3580 * enable, it must be explicitly released here to keep the 3581 * freeze/release count in sync. 3582 */ 3583 sc->wait_for_port_enable = 0; 3584 sc->port_enable_complete = 1; 3585 wakeup(&sc->port_enable_complete); 3586 mpssas_startup_decrement(sassc); 3587 } 3588 3589 int 3590 mpssas_check_id(struct mpssas_softc *sassc, int id) 3591 { 3592 struct mps_softc *sc = sassc->sc; 3593 char *ids; 3594 char *name; 3595 3596 ids = &sc->exclude_ids[0]; 3597 while((name = strsep(&ids, ",")) != NULL) { 3598 if (name[0] == '\0') 3599 continue; 3600 if (strtol(name, NULL, 0) == (long)id) 3601 return (1); 3602 } 3603 3604 return (0); 3605 } 3606 3607 void 3608 mpssas_realloc_targets(struct mps_softc *sc, int maxtargets) 3609 { 3610 struct mpssas_softc *sassc; 3611 struct mpssas_lun *lun, *lun_tmp; 3612 struct mpssas_target *targ; 3613 int i; 3614 3615 sassc = sc->sassc; 3616 /* 3617 * The number of targets is based on IOC Facts, so free all of 3618 * the allocated LUNs for each target and then the target buffer 3619 * itself. 3620 */ 3621 for (i=0; i< maxtargets; i++) { 3622 targ = &sassc->targets[i]; 3623 SLIST_FOREACH_SAFE(lun, &targ->luns, lun_link, lun_tmp) { 3624 free(lun, M_MPT2); 3625 } 3626 } 3627 free(sassc->targets, M_MPT2); 3628 3629 sassc->targets = malloc(sizeof(struct mpssas_target) * maxtargets, 3630 M_MPT2, M_WAITOK|M_ZERO); 3631 if (!sassc->targets) { 3632 panic("%s failed to alloc targets with error %d\n", 3633 __func__, ENOMEM); 3634 } 3635 } 3636