1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 /* Communications core for Avago Technologies (LSI) MPT2 */ 37 38 /* TODO Move headers to mpsvar */ 39 #include <sys/types.h> 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/selinfo.h> 44 #include <sys/lock.h> 45 #include <sys/mutex.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/smp.h> 54 #include <sys/queue.h> 55 #include <sys/kthread.h> 56 #include <sys/taskqueue.h> 57 #include <sys/endian.h> 58 #include <sys/eventhandler.h> 59 #include <sys/sbuf.h> 60 #include <sys/priv.h> 61 62 #include <machine/bus.h> 63 #include <machine/resource.h> 64 #include <sys/rman.h> 65 #include <sys/proc.h> 66 67 #include <dev/pci/pcivar.h> 68 69 #include <cam/cam.h> 70 #include <cam/scsi/scsi_all.h> 71 72 #include <dev/mps/mpi/mpi2_type.h> 73 #include <dev/mps/mpi/mpi2.h> 74 #include <dev/mps/mpi/mpi2_ioc.h> 75 #include <dev/mps/mpi/mpi2_sas.h> 76 #include <dev/mps/mpi/mpi2_cnfg.h> 77 #include <dev/mps/mpi/mpi2_init.h> 78 #include <dev/mps/mpi/mpi2_tool.h> 79 #include <dev/mps/mps_ioctl.h> 80 #include <dev/mps/mpsvar.h> 81 #include <dev/mps/mps_table.h> 82 83 static int mps_diag_reset(struct mps_softc *sc, int sleep_flag); 84 static int mps_init_queues(struct mps_softc *sc); 85 static void mps_resize_queues(struct mps_softc *sc); 86 static int mps_message_unit_reset(struct mps_softc *sc, int sleep_flag); 87 static int mps_transition_operational(struct mps_softc *sc); 88 static int mps_iocfacts_allocate(struct mps_softc *sc, uint8_t attaching); 89 static void mps_iocfacts_free(struct mps_softc *sc); 90 static void mps_startup(void *arg); 91 static int mps_send_iocinit(struct mps_softc *sc); 92 static int mps_alloc_queues(struct mps_softc *sc); 93 static int mps_alloc_hw_queues(struct mps_softc *sc); 94 static int mps_alloc_replies(struct mps_softc *sc); 95 static int mps_alloc_requests(struct mps_softc *sc); 96 static int mps_attach_log(struct mps_softc *sc); 97 static __inline void mps_complete_command(struct mps_softc *sc, 98 struct mps_command *cm); 99 static void mps_dispatch_event(struct mps_softc *sc, uintptr_t data, 100 MPI2_EVENT_NOTIFICATION_REPLY *reply); 101 static void mps_config_complete(struct mps_softc *sc, struct mps_command *cm); 102 static void mps_periodic(void *); 103 static int mps_reregister_events(struct mps_softc *sc); 104 static void mps_enqueue_request(struct mps_softc *sc, struct mps_command *cm); 105 static int mps_get_iocfacts(struct mps_softc *sc, MPI2_IOC_FACTS_REPLY *facts); 106 static int mps_wait_db_ack(struct mps_softc *sc, int timeout, int sleep_flag); 107 static int mps_debug_sysctl(SYSCTL_HANDLER_ARGS); 108 static int mps_dump_reqs(SYSCTL_HANDLER_ARGS); 109 static void mps_parse_debug(struct mps_softc *sc, char *list); 110 111 SYSCTL_NODE(_hw, OID_AUTO, mps, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 112 "MPS Driver Parameters"); 113 114 MALLOC_DEFINE(M_MPT2, "mps", "mpt2 driver memory"); 115 MALLOC_DECLARE(M_MPSUSER); 116 117 /* 118 * Do a "Diagnostic Reset" aka a hard reset. This should get the chip out of 119 * any state and back to its initialization state machine. 120 */ 121 static char mpt2_reset_magic[] = { 0x00, 0x0f, 0x04, 0x0b, 0x02, 0x07, 0x0d }; 122 123 /* Added this union to smoothly convert le64toh cm->cm_desc.Words. 124 * Compiler only support unint64_t to be passed as argument. 125 * Otherwise it will throw below error 126 * "aggregate value used where an integer was expected" 127 */ 128 129 typedef union { 130 u64 word; 131 struct { 132 u32 low; 133 u32 high; 134 } u; 135 } request_descriptor_t; 136 137 /* Rate limit chain-fail messages to 1 per minute */ 138 static struct timeval mps_chainfail_interval = { 60, 0 }; 139 140 /* 141 * sleep_flag can be either CAN_SLEEP or NO_SLEEP. 142 * If this function is called from process context, it can sleep 143 * and there is no harm to sleep, in case if this fuction is called 144 * from Interrupt handler, we can not sleep and need NO_SLEEP flag set. 145 * based on sleep flags driver will call either msleep, pause or DELAY. 146 * msleep and pause are of same variant, but pause is used when mps_mtx 147 * is not hold by driver. 148 * 149 */ 150 static int 151 mps_diag_reset(struct mps_softc *sc,int sleep_flag) 152 { 153 uint32_t reg; 154 int i, error, tries = 0; 155 uint8_t first_wait_done = FALSE; 156 157 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__); 158 159 /* Clear any pending interrupts */ 160 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0); 161 162 /* 163 * Force NO_SLEEP for threads prohibited to sleep 164 * e.a Thread from interrupt handler are prohibited to sleep. 165 */ 166 if (curthread->td_no_sleeping != 0) 167 sleep_flag = NO_SLEEP; 168 169 mps_dprint(sc, MPS_INIT, "sequence start, sleep_flag= %d\n", sleep_flag); 170 171 /* Push the magic sequence */ 172 error = ETIMEDOUT; 173 while (tries++ < 20) { 174 for (i = 0; i < sizeof(mpt2_reset_magic); i++) 175 mps_regwrite(sc, MPI2_WRITE_SEQUENCE_OFFSET, 176 mpt2_reset_magic[i]); 177 /* wait 100 msec */ 178 if (mtx_owned(&sc->mps_mtx) && sleep_flag == CAN_SLEEP) 179 msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0, 180 "mpsdiag", hz/10); 181 else if (sleep_flag == CAN_SLEEP) 182 pause("mpsdiag", hz/10); 183 else 184 DELAY(100 * 1000); 185 186 reg = mps_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET); 187 if (reg & MPI2_DIAG_DIAG_WRITE_ENABLE) { 188 error = 0; 189 break; 190 } 191 } 192 if (error) { 193 mps_dprint(sc, MPS_INIT, "sequence failed, error=%d, exit\n", 194 error); 195 return (error); 196 } 197 198 /* Send the actual reset. XXX need to refresh the reg? */ 199 reg |= MPI2_DIAG_RESET_ADAPTER; 200 mps_dprint(sc, MPS_INIT, "sequence success, sending reset, reg= 0x%x\n", 201 reg); 202 mps_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET, reg); 203 204 /* Wait up to 300 seconds in 50ms intervals */ 205 error = ETIMEDOUT; 206 for (i = 0; i < 6000; i++) { 207 /* 208 * Wait 50 msec. If this is the first time through, wait 256 209 * msec to satisfy Diag Reset timing requirements. 210 */ 211 if (first_wait_done) { 212 if (mtx_owned(&sc->mps_mtx) && sleep_flag == CAN_SLEEP) 213 msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0, 214 "mpsdiag", hz/20); 215 else if (sleep_flag == CAN_SLEEP) 216 pause("mpsdiag", hz/20); 217 else 218 DELAY(50 * 1000); 219 } else { 220 DELAY(256 * 1000); 221 first_wait_done = TRUE; 222 } 223 /* 224 * Check for the RESET_ADAPTER bit to be cleared first, then 225 * wait for the RESET state to be cleared, which takes a little 226 * longer. 227 */ 228 reg = mps_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET); 229 if (reg & MPI2_DIAG_RESET_ADAPTER) { 230 continue; 231 } 232 reg = mps_regread(sc, MPI2_DOORBELL_OFFSET); 233 if ((reg & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_RESET) { 234 error = 0; 235 break; 236 } 237 } 238 if (error) { 239 mps_dprint(sc, MPS_INIT, "reset failed, error= %d, exit\n", 240 error); 241 return (error); 242 } 243 244 mps_regwrite(sc, MPI2_WRITE_SEQUENCE_OFFSET, 0x0); 245 mps_dprint(sc, MPS_INIT, "diag reset success, exit\n"); 246 247 return (0); 248 } 249 250 static int 251 mps_message_unit_reset(struct mps_softc *sc, int sleep_flag) 252 { 253 int error; 254 255 MPS_FUNCTRACE(sc); 256 257 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__); 258 259 error = 0; 260 mps_regwrite(sc, MPI2_DOORBELL_OFFSET, 261 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET << 262 MPI2_DOORBELL_FUNCTION_SHIFT); 263 264 if (mps_wait_db_ack(sc, 5, sleep_flag) != 0) { 265 mps_dprint(sc, MPS_INIT|MPS_FAULT, 266 "Doorbell handshake failed\n"); 267 error = ETIMEDOUT; 268 } 269 270 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__); 271 return (error); 272 } 273 274 static int 275 mps_transition_ready(struct mps_softc *sc) 276 { 277 uint32_t reg, state; 278 int error, tries = 0; 279 int sleep_flags; 280 281 MPS_FUNCTRACE(sc); 282 /* If we are in attach call, do not sleep */ 283 sleep_flags = (sc->mps_flags & MPS_FLAGS_ATTACH_DONE) 284 ? CAN_SLEEP:NO_SLEEP; 285 error = 0; 286 287 mps_dprint(sc, MPS_INIT, "%s entered, sleep_flags= %d\n", 288 __func__, sleep_flags); 289 290 while (tries++ < 1200) { 291 reg = mps_regread(sc, MPI2_DOORBELL_OFFSET); 292 mps_dprint(sc, MPS_INIT, " Doorbell= 0x%x\n", reg); 293 294 /* 295 * Ensure the IOC is ready to talk. If it's not, try 296 * resetting it. 297 */ 298 if (reg & MPI2_DOORBELL_USED) { 299 mps_dprint(sc, MPS_INIT, " Not ready, sending diag " 300 "reset\n"); 301 mps_diag_reset(sc, sleep_flags); 302 DELAY(50000); 303 continue; 304 } 305 306 /* Is the adapter owned by another peer? */ 307 if ((reg & MPI2_DOORBELL_WHO_INIT_MASK) == 308 (MPI2_WHOINIT_PCI_PEER << MPI2_DOORBELL_WHO_INIT_SHIFT)) { 309 mps_dprint(sc, MPS_INIT|MPS_FAULT, "IOC is under the " 310 "control of another peer host, aborting " 311 "initialization.\n"); 312 error = ENXIO; 313 break; 314 } 315 316 state = reg & MPI2_IOC_STATE_MASK; 317 if (state == MPI2_IOC_STATE_READY) { 318 /* Ready to go! */ 319 error = 0; 320 break; 321 } else if (state == MPI2_IOC_STATE_FAULT) { 322 mps_dprint(sc, MPS_INIT|MPS_FAULT, "IOC in fault " 323 "state 0x%x, resetting\n", 324 state & MPI2_DOORBELL_FAULT_CODE_MASK); 325 mps_diag_reset(sc, sleep_flags); 326 } else if (state == MPI2_IOC_STATE_OPERATIONAL) { 327 /* Need to take ownership */ 328 mps_message_unit_reset(sc, sleep_flags); 329 } else if (state == MPI2_IOC_STATE_RESET) { 330 /* Wait a bit, IOC might be in transition */ 331 mps_dprint(sc, MPS_INIT|MPS_FAULT, 332 "IOC in unexpected reset state\n"); 333 } else { 334 mps_dprint(sc, MPS_INIT|MPS_FAULT, 335 "IOC in unknown state 0x%x\n", state); 336 error = EINVAL; 337 break; 338 } 339 340 /* Wait 50ms for things to settle down. */ 341 DELAY(50000); 342 } 343 344 if (error) 345 mps_dprint(sc, MPS_INIT|MPS_FAULT, 346 "Cannot transition IOC to ready\n"); 347 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__); 348 349 return (error); 350 } 351 352 static int 353 mps_transition_operational(struct mps_softc *sc) 354 { 355 uint32_t reg, state; 356 int error; 357 358 MPS_FUNCTRACE(sc); 359 360 error = 0; 361 reg = mps_regread(sc, MPI2_DOORBELL_OFFSET); 362 mps_dprint(sc, MPS_INIT, "%s entered, Doorbell= 0x%x\n", __func__, reg); 363 364 state = reg & MPI2_IOC_STATE_MASK; 365 if (state != MPI2_IOC_STATE_READY) { 366 mps_dprint(sc, MPS_INIT, "IOC not ready\n"); 367 if ((error = mps_transition_ready(sc)) != 0) { 368 mps_dprint(sc, MPS_INIT|MPS_FAULT, 369 "failed to transition ready, exit\n"); 370 return (error); 371 } 372 } 373 374 error = mps_send_iocinit(sc); 375 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__); 376 377 return (error); 378 } 379 380 static void 381 mps_resize_queues(struct mps_softc *sc) 382 { 383 u_int reqcr, prireqcr, maxio, sges_per_frame; 384 385 /* 386 * Size the queues. Since the reply queues always need one free 387 * entry, we'll deduct one reply message here. The LSI documents 388 * suggest instead to add a count to the request queue, but I think 389 * that it's better to deduct from reply queue. 390 */ 391 prireqcr = MAX(1, sc->max_prireqframes); 392 prireqcr = MIN(prireqcr, sc->facts->HighPriorityCredit); 393 394 reqcr = MAX(2, sc->max_reqframes); 395 reqcr = MIN(reqcr, sc->facts->RequestCredit); 396 397 sc->num_reqs = prireqcr + reqcr; 398 sc->num_prireqs = prireqcr; 399 sc->num_replies = MIN(sc->max_replyframes + sc->max_evtframes, 400 sc->facts->MaxReplyDescriptorPostQueueDepth) - 1; 401 402 /* Store the request frame size in bytes rather than as 32bit words */ 403 sc->reqframesz = sc->facts->IOCRequestFrameSize * 4; 404 405 /* 406 * Max IO Size is Page Size * the following: 407 * ((SGEs per frame - 1 for chain element) * Max Chain Depth) 408 * + 1 for no chain needed in last frame 409 * 410 * If user suggests a Max IO size to use, use the smaller of the 411 * user's value and the calculated value as long as the user's 412 * value is larger than 0. The user's value is in pages. 413 */ 414 sges_per_frame = sc->reqframesz / sizeof(MPI2_SGE_SIMPLE64) - 1; 415 maxio = (sges_per_frame * sc->facts->MaxChainDepth + 1) * PAGE_SIZE; 416 417 /* 418 * If I/O size limitation requested, then use it and pass up to CAM. 419 * If not, use maxphys as an optimization hint, but report HW limit. 420 */ 421 if (sc->max_io_pages > 0) { 422 maxio = min(maxio, sc->max_io_pages * PAGE_SIZE); 423 sc->maxio = maxio; 424 } else { 425 sc->maxio = maxio; 426 maxio = min(maxio, maxphys); 427 } 428 429 sc->num_chains = (maxio / PAGE_SIZE + sges_per_frame - 2) / 430 sges_per_frame * reqcr; 431 if (sc->max_chains > 0 && sc->max_chains < sc->num_chains) 432 sc->num_chains = sc->max_chains; 433 434 /* 435 * Figure out the number of MSIx-based queues. If the firmware or 436 * user has done something crazy and not allowed enough credit for 437 * the queues to be useful then don't enable multi-queue. 438 */ 439 if (sc->facts->MaxMSIxVectors < 2) 440 sc->msi_msgs = 1; 441 442 if (sc->msi_msgs > 1) { 443 sc->msi_msgs = MIN(sc->msi_msgs, mp_ncpus); 444 sc->msi_msgs = MIN(sc->msi_msgs, sc->facts->MaxMSIxVectors); 445 if (sc->num_reqs / sc->msi_msgs < 2) 446 sc->msi_msgs = 1; 447 } 448 449 mps_dprint(sc, MPS_INIT, "Sized queues to q=%d reqs=%d replies=%d\n", 450 sc->msi_msgs, sc->num_reqs, sc->num_replies); 451 } 452 453 /* 454 * This is called during attach and when re-initializing due to a Diag Reset. 455 * IOC Facts is used to allocate many of the structures needed by the driver. 456 * If called from attach, de-allocation is not required because the driver has 457 * not allocated any structures yet, but if called from a Diag Reset, previously 458 * allocated structures based on IOC Facts will need to be freed and re- 459 * allocated bases on the latest IOC Facts. 460 */ 461 static int 462 mps_iocfacts_allocate(struct mps_softc *sc, uint8_t attaching) 463 { 464 int error; 465 Mpi2IOCFactsReply_t saved_facts; 466 uint8_t saved_mode, reallocating; 467 468 mps_dprint(sc, MPS_INIT|MPS_TRACE, "%s entered\n", __func__); 469 470 /* Save old IOC Facts and then only reallocate if Facts have changed */ 471 if (!attaching) { 472 bcopy(sc->facts, &saved_facts, sizeof(MPI2_IOC_FACTS_REPLY)); 473 } 474 475 /* 476 * Get IOC Facts. In all cases throughout this function, panic if doing 477 * a re-initialization and only return the error if attaching so the OS 478 * can handle it. 479 */ 480 if ((error = mps_get_iocfacts(sc, sc->facts)) != 0) { 481 if (attaching) { 482 mps_dprint(sc, MPS_INIT|MPS_FAULT, "Failed to get " 483 "IOC Facts with error %d, exit\n", error); 484 return (error); 485 } else { 486 panic("%s failed to get IOC Facts with error %d\n", 487 __func__, error); 488 } 489 } 490 491 MPS_DPRINT_PAGE(sc, MPS_XINFO, iocfacts, sc->facts); 492 493 snprintf(sc->fw_version, sizeof(sc->fw_version), 494 "%02d.%02d.%02d.%02d", 495 sc->facts->FWVersion.Struct.Major, 496 sc->facts->FWVersion.Struct.Minor, 497 sc->facts->FWVersion.Struct.Unit, 498 sc->facts->FWVersion.Struct.Dev); 499 500 snprintf(sc->msg_version, sizeof(sc->msg_version), "%d.%d", 501 (sc->facts->MsgVersion & MPI2_IOCFACTS_MSGVERSION_MAJOR_MASK) >> 502 MPI2_IOCFACTS_MSGVERSION_MAJOR_SHIFT, 503 (sc->facts->MsgVersion & MPI2_IOCFACTS_MSGVERSION_MINOR_MASK) >> 504 MPI2_IOCFACTS_MSGVERSION_MINOR_SHIFT); 505 506 mps_dprint(sc, MPS_INFO, "Firmware: %s, Driver: %s\n", sc->fw_version, 507 MPS_DRIVER_VERSION); 508 mps_dprint(sc, MPS_INFO, "IOCCapabilities: %b\n", 509 sc->facts->IOCCapabilities, 510 "\20" "\3ScsiTaskFull" "\4DiagTrace" "\5SnapBuf" "\6ExtBuf" 511 "\7EEDP" "\10BiDirTarg" "\11Multicast" "\14TransRetry" "\15IR" 512 "\16EventReplay" "\17RaidAccel" "\20MSIXIndex" "\21HostDisc"); 513 514 /* 515 * If the chip doesn't support event replay then a hard reset will be 516 * required to trigger a full discovery. Do the reset here then 517 * retransition to Ready. A hard reset might have already been done, 518 * but it doesn't hurt to do it again. Only do this if attaching, not 519 * for a Diag Reset. 520 */ 521 if (attaching && ((sc->facts->IOCCapabilities & 522 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY) == 0)) { 523 mps_dprint(sc, MPS_INIT, "No event replay, reseting\n"); 524 mps_diag_reset(sc, NO_SLEEP); 525 if ((error = mps_transition_ready(sc)) != 0) { 526 mps_dprint(sc, MPS_INIT|MPS_FAULT, "Failed to " 527 "transition to ready with error %d, exit\n", 528 error); 529 return (error); 530 } 531 } 532 533 /* 534 * Set flag if IR Firmware is loaded. If the RAID Capability has 535 * changed from the previous IOC Facts, log a warning, but only if 536 * checking this after a Diag Reset and not during attach. 537 */ 538 saved_mode = sc->ir_firmware; 539 if (sc->facts->IOCCapabilities & 540 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) 541 sc->ir_firmware = 1; 542 if (!attaching) { 543 if (sc->ir_firmware != saved_mode) { 544 mps_dprint(sc, MPS_INIT|MPS_FAULT, "new IR/IT mode " 545 "in IOC Facts does not match previous mode\n"); 546 } 547 } 548 549 /* Only deallocate and reallocate if relevant IOC Facts have changed */ 550 reallocating = FALSE; 551 sc->mps_flags &= ~MPS_FLAGS_REALLOCATED; 552 553 if ((!attaching) && 554 ((saved_facts.MsgVersion != sc->facts->MsgVersion) || 555 (saved_facts.HeaderVersion != sc->facts->HeaderVersion) || 556 (saved_facts.MaxChainDepth != sc->facts->MaxChainDepth) || 557 (saved_facts.RequestCredit != sc->facts->RequestCredit) || 558 (saved_facts.ProductID != sc->facts->ProductID) || 559 (saved_facts.IOCCapabilities != sc->facts->IOCCapabilities) || 560 (saved_facts.IOCRequestFrameSize != 561 sc->facts->IOCRequestFrameSize) || 562 (saved_facts.MaxTargets != sc->facts->MaxTargets) || 563 (saved_facts.MaxSasExpanders != sc->facts->MaxSasExpanders) || 564 (saved_facts.MaxEnclosures != sc->facts->MaxEnclosures) || 565 (saved_facts.HighPriorityCredit != sc->facts->HighPriorityCredit) || 566 (saved_facts.MaxReplyDescriptorPostQueueDepth != 567 sc->facts->MaxReplyDescriptorPostQueueDepth) || 568 (saved_facts.ReplyFrameSize != sc->facts->ReplyFrameSize) || 569 (saved_facts.MaxVolumes != sc->facts->MaxVolumes) || 570 (saved_facts.MaxPersistentEntries != 571 sc->facts->MaxPersistentEntries))) { 572 reallocating = TRUE; 573 574 /* Record that we reallocated everything */ 575 sc->mps_flags |= MPS_FLAGS_REALLOCATED; 576 } 577 578 /* 579 * Some things should be done if attaching or re-allocating after a Diag 580 * Reset, but are not needed after a Diag Reset if the FW has not 581 * changed. 582 */ 583 if (attaching || reallocating) { 584 /* 585 * Check if controller supports FW diag buffers and set flag to 586 * enable each type. 587 */ 588 if (sc->facts->IOCCapabilities & 589 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) 590 sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_TRACE]. 591 enabled = TRUE; 592 if (sc->facts->IOCCapabilities & 593 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) 594 sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_SNAPSHOT]. 595 enabled = TRUE; 596 if (sc->facts->IOCCapabilities & 597 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) 598 sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_EXTENDED]. 599 enabled = TRUE; 600 601 /* 602 * Set flag if EEDP is supported and if TLR is supported. 603 */ 604 if (sc->facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) 605 sc->eedp_enabled = TRUE; 606 if (sc->facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) 607 sc->control_TLR = TRUE; 608 609 mps_resize_queues(sc); 610 611 /* 612 * Initialize all Tail Queues 613 */ 614 TAILQ_INIT(&sc->req_list); 615 TAILQ_INIT(&sc->high_priority_req_list); 616 TAILQ_INIT(&sc->chain_list); 617 TAILQ_INIT(&sc->tm_list); 618 } 619 620 /* 621 * If doing a Diag Reset and the FW is significantly different 622 * (reallocating will be set above in IOC Facts comparison), then all 623 * buffers based on the IOC Facts will need to be freed before they are 624 * reallocated. 625 */ 626 if (reallocating) { 627 mps_iocfacts_free(sc); 628 mpssas_realloc_targets(sc, saved_facts.MaxTargets + 629 saved_facts.MaxVolumes); 630 } 631 632 /* 633 * Any deallocation has been completed. Now start reallocating 634 * if needed. Will only need to reallocate if attaching or if the new 635 * IOC Facts are different from the previous IOC Facts after a Diag 636 * Reset. Targets have already been allocated above if needed. 637 */ 638 error = 0; 639 while (attaching || reallocating) { 640 if ((error = mps_alloc_hw_queues(sc)) != 0) 641 break; 642 if ((error = mps_alloc_replies(sc)) != 0) 643 break; 644 if ((error = mps_alloc_requests(sc)) != 0) 645 break; 646 if ((error = mps_alloc_queues(sc)) != 0) 647 break; 648 649 break; 650 } 651 if (error) { 652 mps_dprint(sc, MPS_INIT|MPS_FAULT, 653 "Failed to alloc queues with error %d\n", error); 654 mps_free(sc); 655 return (error); 656 } 657 658 /* Always initialize the queues */ 659 bzero(sc->free_queue, sc->fqdepth * 4); 660 mps_init_queues(sc); 661 662 /* 663 * Always get the chip out of the reset state, but only panic if not 664 * attaching. If attaching and there is an error, that is handled by 665 * the OS. 666 */ 667 error = mps_transition_operational(sc); 668 if (error != 0) { 669 mps_dprint(sc, MPS_INIT|MPS_FAULT, "Failed to " 670 "transition to operational with error %d\n", error); 671 mps_free(sc); 672 return (error); 673 } 674 675 /* 676 * Finish the queue initialization. 677 * These are set here instead of in mps_init_queues() because the 678 * IOC resets these values during the state transition in 679 * mps_transition_operational(). The free index is set to 1 680 * because the corresponding index in the IOC is set to 0, and the 681 * IOC treats the queues as full if both are set to the same value. 682 * Hence the reason that the queue can't hold all of the possible 683 * replies. 684 */ 685 sc->replypostindex = 0; 686 mps_regwrite(sc, MPI2_REPLY_FREE_HOST_INDEX_OFFSET, sc->replyfreeindex); 687 mps_regwrite(sc, MPI2_REPLY_POST_HOST_INDEX_OFFSET, 0); 688 689 /* 690 * Attach the subsystems so they can prepare their event masks. 691 * XXX Should be dynamic so that IM/IR and user modules can attach 692 */ 693 error = 0; 694 while (attaching) { 695 mps_dprint(sc, MPS_INIT, "Attaching subsystems\n"); 696 if ((error = mps_attach_log(sc)) != 0) 697 break; 698 if ((error = mps_attach_sas(sc)) != 0) 699 break; 700 if ((error = mps_attach_user(sc)) != 0) 701 break; 702 break; 703 } 704 if (error) { 705 mps_dprint(sc, MPS_INIT|MPS_FAULT, "Failed to attach all " 706 "subsystems: error %d\n", error); 707 mps_free(sc); 708 return (error); 709 } 710 711 /* 712 * XXX If the number of MSI-X vectors changes during re-init, this 713 * won't see it and adjust. 714 */ 715 if (attaching && (error = mps_pci_setup_interrupts(sc)) != 0) { 716 mps_dprint(sc, MPS_INIT|MPS_FAULT, "Failed to setup " 717 "interrupts\n"); 718 mps_free(sc); 719 return (error); 720 } 721 722 /* 723 * Set flag if this is a WD controller. This shouldn't ever change, but 724 * reset it after a Diag Reset, just in case. 725 */ 726 sc->WD_available = FALSE; 727 if (pci_get_device(sc->mps_dev) == MPI2_MFGPAGE_DEVID_SSS6200) 728 sc->WD_available = TRUE; 729 730 return (error); 731 } 732 733 /* 734 * This is called if memory is being free (during detach for example) and when 735 * buffers need to be reallocated due to a Diag Reset. 736 */ 737 static void 738 mps_iocfacts_free(struct mps_softc *sc) 739 { 740 struct mps_command *cm; 741 int i; 742 743 mps_dprint(sc, MPS_TRACE, "%s\n", __func__); 744 745 if (sc->free_busaddr != 0) 746 bus_dmamap_unload(sc->queues_dmat, sc->queues_map); 747 if (sc->free_queue != NULL) 748 bus_dmamem_free(sc->queues_dmat, sc->free_queue, 749 sc->queues_map); 750 if (sc->queues_dmat != NULL) 751 bus_dma_tag_destroy(sc->queues_dmat); 752 753 if (sc->chain_frames != NULL) { 754 bus_dmamap_unload(sc->chain_dmat, sc->chain_map); 755 bus_dmamem_free(sc->chain_dmat, sc->chain_frames, 756 sc->chain_map); 757 } 758 if (sc->chain_dmat != NULL) 759 bus_dma_tag_destroy(sc->chain_dmat); 760 761 if (sc->sense_busaddr != 0) 762 bus_dmamap_unload(sc->sense_dmat, sc->sense_map); 763 if (sc->sense_frames != NULL) 764 bus_dmamem_free(sc->sense_dmat, sc->sense_frames, 765 sc->sense_map); 766 if (sc->sense_dmat != NULL) 767 bus_dma_tag_destroy(sc->sense_dmat); 768 769 if (sc->reply_busaddr != 0) 770 bus_dmamap_unload(sc->reply_dmat, sc->reply_map); 771 if (sc->reply_frames != NULL) 772 bus_dmamem_free(sc->reply_dmat, sc->reply_frames, 773 sc->reply_map); 774 if (sc->reply_dmat != NULL) 775 bus_dma_tag_destroy(sc->reply_dmat); 776 777 if (sc->req_busaddr != 0) 778 bus_dmamap_unload(sc->req_dmat, sc->req_map); 779 if (sc->req_frames != NULL) 780 bus_dmamem_free(sc->req_dmat, sc->req_frames, sc->req_map); 781 if (sc->req_dmat != NULL) 782 bus_dma_tag_destroy(sc->req_dmat); 783 784 if (sc->chains != NULL) 785 free(sc->chains, M_MPT2); 786 if (sc->commands != NULL) { 787 for (i = 1; i < sc->num_reqs; i++) { 788 cm = &sc->commands[i]; 789 bus_dmamap_destroy(sc->buffer_dmat, cm->cm_dmamap); 790 } 791 free(sc->commands, M_MPT2); 792 } 793 if (sc->buffer_dmat != NULL) 794 bus_dma_tag_destroy(sc->buffer_dmat); 795 796 mps_pci_free_interrupts(sc); 797 free(sc->queues, M_MPT2); 798 sc->queues = NULL; 799 } 800 801 /* 802 * The terms diag reset and hard reset are used interchangeably in the MPI 803 * docs to mean resetting the controller chip. In this code diag reset 804 * cleans everything up, and the hard reset function just sends the reset 805 * sequence to the chip. This should probably be refactored so that every 806 * subsystem gets a reset notification of some sort, and can clean up 807 * appropriately. 808 */ 809 int 810 mps_reinit(struct mps_softc *sc) 811 { 812 int error; 813 struct mpssas_softc *sassc; 814 815 sassc = sc->sassc; 816 817 MPS_FUNCTRACE(sc); 818 819 mtx_assert(&sc->mps_mtx, MA_OWNED); 820 821 mps_dprint(sc, MPS_INIT|MPS_INFO, "Reinitializing controller\n"); 822 if (sc->mps_flags & MPS_FLAGS_DIAGRESET) { 823 mps_dprint(sc, MPS_INIT, "Reset already in progress\n"); 824 return 0; 825 } 826 827 /* make sure the completion callbacks can recognize they're getting 828 * a NULL cm_reply due to a reset. 829 */ 830 sc->mps_flags |= MPS_FLAGS_DIAGRESET; 831 832 /* 833 * Mask interrupts here. 834 */ 835 mps_dprint(sc, MPS_INIT, "masking interrupts and resetting\n"); 836 mps_mask_intr(sc); 837 838 error = mps_diag_reset(sc, CAN_SLEEP); 839 if (error != 0) { 840 /* XXXSL No need to panic here */ 841 panic("%s hard reset failed with error %d\n", 842 __func__, error); 843 } 844 845 /* Restore the PCI state, including the MSI-X registers */ 846 mps_pci_restore(sc); 847 848 /* Give the I/O subsystem special priority to get itself prepared */ 849 mpssas_handle_reinit(sc); 850 851 /* 852 * Get IOC Facts and allocate all structures based on this information. 853 * The attach function will also call mps_iocfacts_allocate at startup. 854 * If relevant values have changed in IOC Facts, this function will free 855 * all of the memory based on IOC Facts and reallocate that memory. 856 */ 857 if ((error = mps_iocfacts_allocate(sc, FALSE)) != 0) { 858 panic("%s IOC Facts based allocation failed with error %d\n", 859 __func__, error); 860 } 861 862 /* 863 * Mapping structures will be re-allocated after getting IOC Page8, so 864 * free these structures here. 865 */ 866 mps_mapping_exit(sc); 867 868 /* 869 * The static page function currently read is IOC Page8. Others can be 870 * added in future. It's possible that the values in IOC Page8 have 871 * changed after a Diag Reset due to user modification, so always read 872 * these. Interrupts are masked, so unmask them before getting config 873 * pages. 874 */ 875 mps_unmask_intr(sc); 876 sc->mps_flags &= ~MPS_FLAGS_DIAGRESET; 877 mps_base_static_config_pages(sc); 878 879 /* 880 * Some mapping info is based in IOC Page8 data, so re-initialize the 881 * mapping tables. 882 */ 883 mps_mapping_initialize(sc); 884 885 /* 886 * Restart will reload the event masks clobbered by the reset, and 887 * then enable the port. 888 */ 889 mps_reregister_events(sc); 890 891 /* the end of discovery will release the simq, so we're done. */ 892 mps_dprint(sc, MPS_INIT|MPS_XINFO, "Finished sc %p post %u free %u\n", 893 sc, sc->replypostindex, sc->replyfreeindex); 894 895 mpssas_release_simq_reinit(sassc); 896 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__); 897 898 return 0; 899 } 900 901 /* Wait for the chip to ACK a word that we've put into its FIFO 902 * Wait for <timeout> seconds. In single loop wait for busy loop 903 * for 500 microseconds. 904 * Total is [ 0.5 * (2000 * <timeout>) ] in miliseconds. 905 * */ 906 static int 907 mps_wait_db_ack(struct mps_softc *sc, int timeout, int sleep_flag) 908 { 909 910 u32 cntdn, count; 911 u32 int_status; 912 u32 doorbell; 913 914 count = 0; 915 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout; 916 do { 917 int_status = mps_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET); 918 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) { 919 mps_dprint(sc, MPS_TRACE, 920 "%s: successful count(%d), timeout(%d)\n", 921 __func__, count, timeout); 922 return 0; 923 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) { 924 doorbell = mps_regread(sc, MPI2_DOORBELL_OFFSET); 925 if ((doorbell & MPI2_IOC_STATE_MASK) == 926 MPI2_IOC_STATE_FAULT) { 927 mps_dprint(sc, MPS_FAULT, 928 "fault_state(0x%04x)!\n", doorbell); 929 return (EFAULT); 930 } 931 } else if (int_status == 0xFFFFFFFF) 932 goto out; 933 934 /* If it can sleep, sleep for 1 milisecond, else busy loop for 935 * 0.5 milisecond */ 936 if (mtx_owned(&sc->mps_mtx) && sleep_flag == CAN_SLEEP) 937 msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0, 938 "mpsdba", hz/1000); 939 else if (sleep_flag == CAN_SLEEP) 940 pause("mpsdba", hz/1000); 941 else 942 DELAY(500); 943 count++; 944 } while (--cntdn); 945 946 out: 947 mps_dprint(sc, MPS_FAULT, "%s: failed due to timeout count(%d), " 948 "int_status(%x)!\n", __func__, count, int_status); 949 return (ETIMEDOUT); 950 951 } 952 953 /* Wait for the chip to signal that the next word in its FIFO can be fetched */ 954 static int 955 mps_wait_db_int(struct mps_softc *sc) 956 { 957 int retry; 958 959 for (retry = 0; retry < MPS_DB_MAX_WAIT; retry++) { 960 if ((mps_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET) & 961 MPI2_HIS_IOC2SYS_DB_STATUS) != 0) 962 return (0); 963 DELAY(2000); 964 } 965 return (ETIMEDOUT); 966 } 967 968 /* Step through the synchronous command state machine, i.e. "Doorbell mode" */ 969 static int 970 mps_request_sync(struct mps_softc *sc, void *req, MPI2_DEFAULT_REPLY *reply, 971 int req_sz, int reply_sz, int timeout) 972 { 973 uint32_t *data32; 974 uint16_t *data16; 975 int i, count, ioc_sz, residual; 976 int sleep_flags = CAN_SLEEP; 977 978 if (curthread->td_no_sleeping != 0) 979 sleep_flags = NO_SLEEP; 980 981 /* Step 1 */ 982 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0); 983 984 /* Step 2 */ 985 if (mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED) 986 return (EBUSY); 987 988 /* Step 3 989 * Announce that a message is coming through the doorbell. Messages 990 * are pushed at 32bit words, so round up if needed. 991 */ 992 count = (req_sz + 3) / 4; 993 mps_regwrite(sc, MPI2_DOORBELL_OFFSET, 994 (MPI2_FUNCTION_HANDSHAKE << MPI2_DOORBELL_FUNCTION_SHIFT) | 995 (count << MPI2_DOORBELL_ADD_DWORDS_SHIFT)); 996 997 /* Step 4 */ 998 if (mps_wait_db_int(sc) || 999 (mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED) == 0) { 1000 mps_dprint(sc, MPS_FAULT, "Doorbell failed to activate\n"); 1001 return (ENXIO); 1002 } 1003 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0); 1004 if (mps_wait_db_ack(sc, 5, sleep_flags) != 0) { 1005 mps_dprint(sc, MPS_FAULT, "Doorbell handshake failed\n"); 1006 return (ENXIO); 1007 } 1008 1009 /* Step 5 */ 1010 /* Clock out the message data synchronously in 32-bit dwords*/ 1011 data32 = (uint32_t *)req; 1012 for (i = 0; i < count; i++) { 1013 mps_regwrite(sc, MPI2_DOORBELL_OFFSET, htole32(data32[i])); 1014 if (mps_wait_db_ack(sc, 5, sleep_flags) != 0) { 1015 mps_dprint(sc, MPS_FAULT, 1016 "Timeout while writing doorbell\n"); 1017 return (ENXIO); 1018 } 1019 } 1020 1021 /* Step 6 */ 1022 /* Clock in the reply in 16-bit words. The total length of the 1023 * message is always in the 4th byte, so clock out the first 2 words 1024 * manually, then loop the rest. 1025 */ 1026 data16 = (uint16_t *)reply; 1027 if (mps_wait_db_int(sc) != 0) { 1028 mps_dprint(sc, MPS_FAULT, "Timeout reading doorbell 0\n"); 1029 return (ENXIO); 1030 } 1031 data16[0] = 1032 mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_DATA_MASK; 1033 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0); 1034 if (mps_wait_db_int(sc) != 0) { 1035 mps_dprint(sc, MPS_FAULT, "Timeout reading doorbell 1\n"); 1036 return (ENXIO); 1037 } 1038 data16[1] = 1039 mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_DATA_MASK; 1040 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0); 1041 1042 /* Number of 32bit words in the message */ 1043 ioc_sz = reply->MsgLength; 1044 1045 /* 1046 * Figure out how many 16bit words to clock in without overrunning. 1047 * The precision loss with dividing reply_sz can safely be 1048 * ignored because the messages can only be multiples of 32bits. 1049 */ 1050 residual = 0; 1051 count = MIN((reply_sz / 4), ioc_sz) * 2; 1052 if (count < ioc_sz * 2) { 1053 residual = ioc_sz * 2 - count; 1054 mps_dprint(sc, MPS_ERROR, "Driver error, throwing away %d " 1055 "residual message words\n", residual); 1056 } 1057 1058 for (i = 2; i < count; i++) { 1059 if (mps_wait_db_int(sc) != 0) { 1060 mps_dprint(sc, MPS_FAULT, 1061 "Timeout reading doorbell %d\n", i); 1062 return (ENXIO); 1063 } 1064 data16[i] = mps_regread(sc, MPI2_DOORBELL_OFFSET) & 1065 MPI2_DOORBELL_DATA_MASK; 1066 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0); 1067 } 1068 1069 /* 1070 * Pull out residual words that won't fit into the provided buffer. 1071 * This keeps the chip from hanging due to a driver programming 1072 * error. 1073 */ 1074 while (residual--) { 1075 if (mps_wait_db_int(sc) != 0) { 1076 mps_dprint(sc, MPS_FAULT, 1077 "Timeout reading doorbell\n"); 1078 return (ENXIO); 1079 } 1080 (void)mps_regread(sc, MPI2_DOORBELL_OFFSET); 1081 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0); 1082 } 1083 1084 /* Step 7 */ 1085 if (mps_wait_db_int(sc) != 0) { 1086 mps_dprint(sc, MPS_FAULT, "Timeout waiting to exit doorbell\n"); 1087 return (ENXIO); 1088 } 1089 if (mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED) 1090 mps_dprint(sc, MPS_FAULT, "Warning, doorbell still active\n"); 1091 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0); 1092 1093 return (0); 1094 } 1095 1096 static void 1097 mps_enqueue_request(struct mps_softc *sc, struct mps_command *cm) 1098 { 1099 request_descriptor_t rd; 1100 MPS_FUNCTRACE(sc); 1101 mps_dprint(sc, MPS_TRACE, "SMID %u cm %p ccb %p\n", 1102 cm->cm_desc.Default.SMID, cm, cm->cm_ccb); 1103 1104 if (sc->mps_flags & MPS_FLAGS_ATTACH_DONE && !(sc->mps_flags & MPS_FLAGS_SHUTDOWN)) 1105 mtx_assert(&sc->mps_mtx, MA_OWNED); 1106 1107 if (++sc->io_cmds_active > sc->io_cmds_highwater) 1108 sc->io_cmds_highwater++; 1109 rd.u.low = cm->cm_desc.Words.Low; 1110 rd.u.high = cm->cm_desc.Words.High; 1111 rd.word = htole64(rd.word); 1112 1113 KASSERT(cm->cm_state == MPS_CM_STATE_BUSY, 1114 ("command not busy, state = %u\n", cm->cm_state)); 1115 cm->cm_state = MPS_CM_STATE_INQUEUE; 1116 1117 /* TODO-We may need to make below regwrite atomic */ 1118 mps_regwrite(sc, MPI2_REQUEST_DESCRIPTOR_POST_LOW_OFFSET, 1119 rd.u.low); 1120 mps_regwrite(sc, MPI2_REQUEST_DESCRIPTOR_POST_HIGH_OFFSET, 1121 rd.u.high); 1122 } 1123 1124 /* 1125 * Just the FACTS, ma'am. 1126 */ 1127 static int 1128 mps_get_iocfacts(struct mps_softc *sc, MPI2_IOC_FACTS_REPLY *facts) 1129 { 1130 MPI2_DEFAULT_REPLY *reply; 1131 MPI2_IOC_FACTS_REQUEST request; 1132 int error, req_sz, reply_sz; 1133 1134 MPS_FUNCTRACE(sc); 1135 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__); 1136 1137 req_sz = sizeof(MPI2_IOC_FACTS_REQUEST); 1138 reply_sz = sizeof(MPI2_IOC_FACTS_REPLY); 1139 reply = (MPI2_DEFAULT_REPLY *)facts; 1140 1141 bzero(&request, req_sz); 1142 request.Function = MPI2_FUNCTION_IOC_FACTS; 1143 error = mps_request_sync(sc, &request, reply, req_sz, reply_sz, 5); 1144 mps_dprint(sc, MPS_INIT, "%s exit error= %d\n", __func__, error); 1145 1146 return (error); 1147 } 1148 1149 static int 1150 mps_send_iocinit(struct mps_softc *sc) 1151 { 1152 MPI2_IOC_INIT_REQUEST init; 1153 MPI2_DEFAULT_REPLY reply; 1154 int req_sz, reply_sz, error; 1155 struct timeval now; 1156 uint64_t time_in_msec; 1157 1158 MPS_FUNCTRACE(sc); 1159 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__); 1160 1161 /* Do a quick sanity check on proper initialization */ 1162 if ((sc->pqdepth == 0) || (sc->fqdepth == 0) || (sc->reqframesz == 0) 1163 || (sc->replyframesz == 0)) { 1164 mps_dprint(sc, MPS_INIT|MPS_ERROR, 1165 "Driver not fully initialized for IOCInit\n"); 1166 return (EINVAL); 1167 } 1168 1169 req_sz = sizeof(MPI2_IOC_INIT_REQUEST); 1170 reply_sz = sizeof(MPI2_IOC_INIT_REPLY); 1171 bzero(&init, req_sz); 1172 bzero(&reply, reply_sz); 1173 1174 /* 1175 * Fill in the init block. Note that most addresses are 1176 * deliberately in the lower 32bits of memory. This is a micro- 1177 * optimzation for PCI/PCIX, though it's not clear if it helps PCIe. 1178 */ 1179 init.Function = MPI2_FUNCTION_IOC_INIT; 1180 init.WhoInit = MPI2_WHOINIT_HOST_DRIVER; 1181 init.MsgVersion = htole16(MPI2_VERSION); 1182 init.HeaderVersion = htole16(MPI2_HEADER_VERSION); 1183 init.SystemRequestFrameSize = htole16((uint16_t)(sc->reqframesz / 4)); 1184 init.ReplyDescriptorPostQueueDepth = htole16(sc->pqdepth); 1185 init.ReplyFreeQueueDepth = htole16(sc->fqdepth); 1186 init.SenseBufferAddressHigh = 0; 1187 init.SystemReplyAddressHigh = 0; 1188 init.SystemRequestFrameBaseAddress.High = 0; 1189 init.SystemRequestFrameBaseAddress.Low = htole32((uint32_t)sc->req_busaddr); 1190 init.ReplyDescriptorPostQueueAddress.High = 0; 1191 init.ReplyDescriptorPostQueueAddress.Low = htole32((uint32_t)sc->post_busaddr); 1192 init.ReplyFreeQueueAddress.High = 0; 1193 init.ReplyFreeQueueAddress.Low = htole32((uint32_t)sc->free_busaddr); 1194 getmicrotime(&now); 1195 time_in_msec = (now.tv_sec * 1000 + now.tv_usec/1000); 1196 init.TimeStamp.High = htole32((time_in_msec >> 32) & 0xFFFFFFFF); 1197 init.TimeStamp.Low = htole32(time_in_msec & 0xFFFFFFFF); 1198 1199 error = mps_request_sync(sc, &init, &reply, req_sz, reply_sz, 5); 1200 if ((reply.IOCStatus & MPI2_IOCSTATUS_MASK) != MPI2_IOCSTATUS_SUCCESS) 1201 error = ENXIO; 1202 1203 mps_dprint(sc, MPS_INIT, "IOCInit status= 0x%x\n", reply.IOCStatus); 1204 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__); 1205 return (error); 1206 } 1207 1208 void 1209 mps_memaddr_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 1210 { 1211 bus_addr_t *addr; 1212 1213 addr = arg; 1214 *addr = segs[0].ds_addr; 1215 } 1216 1217 void 1218 mps_memaddr_wait_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 1219 { 1220 struct mps_busdma_context *ctx; 1221 int need_unload, need_free; 1222 1223 ctx = (struct mps_busdma_context *)arg; 1224 need_unload = 0; 1225 need_free = 0; 1226 1227 mps_lock(ctx->softc); 1228 ctx->error = error; 1229 ctx->completed = 1; 1230 if ((error == 0) && (ctx->abandoned == 0)) { 1231 *ctx->addr = segs[0].ds_addr; 1232 } else { 1233 if (nsegs != 0) 1234 need_unload = 1; 1235 if (ctx->abandoned != 0) 1236 need_free = 1; 1237 } 1238 if (need_free == 0) 1239 wakeup(ctx); 1240 1241 mps_unlock(ctx->softc); 1242 1243 if (need_unload != 0) { 1244 bus_dmamap_unload(ctx->buffer_dmat, 1245 ctx->buffer_dmamap); 1246 *ctx->addr = 0; 1247 } 1248 1249 if (need_free != 0) 1250 free(ctx, M_MPSUSER); 1251 } 1252 1253 static int 1254 mps_alloc_queues(struct mps_softc *sc) 1255 { 1256 struct mps_queue *q; 1257 u_int nq, i; 1258 1259 nq = sc->msi_msgs; 1260 mps_dprint(sc, MPS_INIT|MPS_XINFO, "Allocating %d I/O queues\n", nq); 1261 1262 sc->queues = malloc(sizeof(struct mps_queue) * nq, M_MPT2, 1263 M_NOWAIT|M_ZERO); 1264 if (sc->queues == NULL) 1265 return (ENOMEM); 1266 1267 for (i = 0; i < nq; i++) { 1268 q = &sc->queues[i]; 1269 mps_dprint(sc, MPS_INIT, "Configuring queue %d %p\n", i, q); 1270 q->sc = sc; 1271 q->qnum = i; 1272 } 1273 1274 return (0); 1275 } 1276 1277 static int 1278 mps_alloc_hw_queues(struct mps_softc *sc) 1279 { 1280 bus_dma_template_t t; 1281 bus_addr_t queues_busaddr; 1282 uint8_t *queues; 1283 int qsize, fqsize, pqsize; 1284 1285 /* 1286 * The reply free queue contains 4 byte entries in multiples of 16 and 1287 * aligned on a 16 byte boundary. There must always be an unused entry. 1288 * This queue supplies fresh reply frames for the firmware to use. 1289 * 1290 * The reply descriptor post queue contains 8 byte entries in 1291 * multiples of 16 and aligned on a 16 byte boundary. This queue 1292 * contains filled-in reply frames sent from the firmware to the host. 1293 * 1294 * These two queues are allocated together for simplicity. 1295 */ 1296 sc->fqdepth = roundup2(sc->num_replies + 1, 16); 1297 sc->pqdepth = roundup2(sc->num_replies + 1, 16); 1298 fqsize= sc->fqdepth * 4; 1299 pqsize = sc->pqdepth * 8; 1300 qsize = fqsize + pqsize; 1301 1302 bus_dma_template_init(&t, sc->mps_parent_dmat); 1303 BUS_DMA_TEMPLATE_FILL(&t, BD_ALIGNMENT(16), BD_MAXSIZE(qsize), 1304 BD_MAXSEGSIZE(qsize), BD_NSEGMENTS(1), 1305 BD_LOWADDR(BUS_SPACE_MAXADDR_32BIT)); 1306 if (bus_dma_template_tag(&t, &sc->queues_dmat)) { 1307 mps_dprint(sc, MPS_ERROR, "Cannot allocate queues DMA tag\n"); 1308 return (ENOMEM); 1309 } 1310 if (bus_dmamem_alloc(sc->queues_dmat, (void **)&queues, BUS_DMA_NOWAIT, 1311 &sc->queues_map)) { 1312 mps_dprint(sc, MPS_ERROR, "Cannot allocate queues memory\n"); 1313 return (ENOMEM); 1314 } 1315 bzero(queues, qsize); 1316 bus_dmamap_load(sc->queues_dmat, sc->queues_map, queues, qsize, 1317 mps_memaddr_cb, &queues_busaddr, 0); 1318 1319 sc->free_queue = (uint32_t *)queues; 1320 sc->free_busaddr = queues_busaddr; 1321 sc->post_queue = (MPI2_REPLY_DESCRIPTORS_UNION *)(queues + fqsize); 1322 sc->post_busaddr = queues_busaddr + fqsize; 1323 mps_dprint(sc, MPS_INIT, "free queue busaddr= %#016jx size= %d\n", 1324 (uintmax_t)sc->free_busaddr, fqsize); 1325 mps_dprint(sc, MPS_INIT, "reply queue busaddr= %#016jx size= %d\n", 1326 (uintmax_t)sc->post_busaddr, pqsize); 1327 1328 return (0); 1329 } 1330 1331 static int 1332 mps_alloc_replies(struct mps_softc *sc) 1333 { 1334 bus_dma_template_t t; 1335 int rsize, num_replies; 1336 1337 /* Store the reply frame size in bytes rather than as 32bit words */ 1338 sc->replyframesz = sc->facts->ReplyFrameSize * 4; 1339 1340 /* 1341 * sc->num_replies should be one less than sc->fqdepth. We need to 1342 * allocate space for sc->fqdepth replies, but only sc->num_replies 1343 * replies can be used at once. 1344 */ 1345 num_replies = max(sc->fqdepth, sc->num_replies); 1346 1347 rsize = sc->replyframesz * num_replies; 1348 bus_dma_template_init(&t, sc->mps_parent_dmat); 1349 BUS_DMA_TEMPLATE_FILL(&t, BD_ALIGNMENT(4), BD_MAXSIZE(rsize), 1350 BD_MAXSEGSIZE(rsize), BD_NSEGMENTS(1), 1351 BD_LOWADDR(BUS_SPACE_MAXADDR_32BIT)); 1352 if (bus_dma_template_tag(&t, &sc->reply_dmat)) { 1353 mps_dprint(sc, MPS_ERROR, "Cannot allocate replies DMA tag\n"); 1354 return (ENOMEM); 1355 } 1356 if (bus_dmamem_alloc(sc->reply_dmat, (void **)&sc->reply_frames, 1357 BUS_DMA_NOWAIT, &sc->reply_map)) { 1358 mps_dprint(sc, MPS_ERROR, "Cannot allocate replies memory\n"); 1359 return (ENOMEM); 1360 } 1361 bzero(sc->reply_frames, rsize); 1362 bus_dmamap_load(sc->reply_dmat, sc->reply_map, sc->reply_frames, rsize, 1363 mps_memaddr_cb, &sc->reply_busaddr, 0); 1364 1365 mps_dprint(sc, MPS_INIT, "reply frames busaddr= %#016jx size= %d\n", 1366 (uintmax_t)sc->reply_busaddr, rsize); 1367 1368 return (0); 1369 } 1370 1371 static void 1372 mps_load_chains_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 1373 { 1374 struct mps_softc *sc = arg; 1375 struct mps_chain *chain; 1376 bus_size_t bo; 1377 int i, o, s; 1378 1379 if (error != 0) 1380 return; 1381 1382 for (i = 0, o = 0, s = 0; s < nsegs; s++) { 1383 KASSERT(segs[s].ds_addr + segs[s].ds_len - 1 <= BUS_SPACE_MAXADDR_32BIT, 1384 ("mps: Bad segment address %#jx len %#jx\n", (uintmax_t)segs[s].ds_addr, 1385 (uintmax_t)segs[s].ds_len)); 1386 for (bo = 0; bo + sc->reqframesz <= segs[s].ds_len; 1387 bo += sc->reqframesz) { 1388 chain = &sc->chains[i++]; 1389 chain->chain =(MPI2_SGE_IO_UNION *)(sc->chain_frames+o); 1390 chain->chain_busaddr = segs[s].ds_addr + bo; 1391 o += sc->reqframesz; 1392 mps_free_chain(sc, chain); 1393 } 1394 if (bo != segs[s].ds_len) 1395 o += segs[s].ds_len - bo; 1396 } 1397 sc->chain_free_lowwater = i; 1398 } 1399 1400 static int 1401 mps_alloc_requests(struct mps_softc *sc) 1402 { 1403 bus_dma_template_t t; 1404 struct mps_command *cm; 1405 int i, rsize, nsegs; 1406 1407 rsize = sc->reqframesz * sc->num_reqs; 1408 bus_dma_template_init(&t, sc->mps_parent_dmat); 1409 BUS_DMA_TEMPLATE_FILL(&t, BD_ALIGNMENT(16), BD_MAXSIZE(rsize), 1410 BD_MAXSEGSIZE(rsize), BD_NSEGMENTS(1), 1411 BD_LOWADDR(BUS_SPACE_MAXADDR_32BIT)); 1412 if (bus_dma_template_tag(&t, &sc->req_dmat)) { 1413 mps_dprint(sc, MPS_ERROR, "Cannot allocate request DMA tag\n"); 1414 return (ENOMEM); 1415 } 1416 if (bus_dmamem_alloc(sc->req_dmat, (void **)&sc->req_frames, 1417 BUS_DMA_NOWAIT, &sc->req_map)) { 1418 mps_dprint(sc, MPS_ERROR, "Cannot allocate request memory\n"); 1419 return (ENOMEM); 1420 } 1421 bzero(sc->req_frames, rsize); 1422 bus_dmamap_load(sc->req_dmat, sc->req_map, sc->req_frames, rsize, 1423 mps_memaddr_cb, &sc->req_busaddr, 0); 1424 mps_dprint(sc, MPS_INIT, "request frames busaddr= %#016jx size= %d\n", 1425 (uintmax_t)sc->req_busaddr, rsize); 1426 1427 sc->chains = malloc(sizeof(struct mps_chain) * sc->num_chains, M_MPT2, 1428 M_NOWAIT | M_ZERO); 1429 if (!sc->chains) { 1430 mps_dprint(sc, MPS_ERROR, "Cannot allocate chain memory\n"); 1431 return (ENOMEM); 1432 } 1433 rsize = sc->reqframesz * sc->num_chains; 1434 bus_dma_template_clone(&t, sc->req_dmat); 1435 BUS_DMA_TEMPLATE_FILL(&t, BD_MAXSIZE(rsize), BD_MAXSEGSIZE(rsize), 1436 BD_NSEGMENTS(howmany(rsize, PAGE_SIZE))); 1437 if (bus_dma_template_tag(&t, &sc->chain_dmat)) { 1438 mps_dprint(sc, MPS_ERROR, "Cannot allocate chain DMA tag\n"); 1439 return (ENOMEM); 1440 } 1441 if (bus_dmamem_alloc(sc->chain_dmat, (void **)&sc->chain_frames, 1442 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->chain_map)) { 1443 mps_dprint(sc, MPS_ERROR, "Cannot allocate chain memory\n"); 1444 return (ENOMEM); 1445 } 1446 if (bus_dmamap_load(sc->chain_dmat, sc->chain_map, sc->chain_frames, 1447 rsize, mps_load_chains_cb, sc, BUS_DMA_NOWAIT)) { 1448 mps_dprint(sc, MPS_ERROR, "Cannot load chain memory\n"); 1449 bus_dmamem_free(sc->chain_dmat, sc->chain_frames, 1450 sc->chain_map); 1451 return (ENOMEM); 1452 } 1453 1454 rsize = MPS_SENSE_LEN * sc->num_reqs; 1455 bus_dma_template_clone(&t, sc->req_dmat); 1456 BUS_DMA_TEMPLATE_FILL(&t, BD_ALIGNMENT(1), BD_MAXSIZE(rsize), 1457 BD_MAXSEGSIZE(rsize)); 1458 if (bus_dma_template_tag(&t, &sc->sense_dmat)) { 1459 mps_dprint(sc, MPS_ERROR, "Cannot allocate sense DMA tag\n"); 1460 return (ENOMEM); 1461 } 1462 if (bus_dmamem_alloc(sc->sense_dmat, (void **)&sc->sense_frames, 1463 BUS_DMA_NOWAIT, &sc->sense_map)) { 1464 mps_dprint(sc, MPS_ERROR, "Cannot allocate sense memory\n"); 1465 return (ENOMEM); 1466 } 1467 bzero(sc->sense_frames, rsize); 1468 bus_dmamap_load(sc->sense_dmat, sc->sense_map, sc->sense_frames, rsize, 1469 mps_memaddr_cb, &sc->sense_busaddr, 0); 1470 mps_dprint(sc, MPS_INIT, "sense frames busaddr= %#016jx size= %d\n", 1471 (uintmax_t)sc->sense_busaddr, rsize); 1472 1473 nsegs = (sc->maxio / PAGE_SIZE) + 1; 1474 bus_dma_template_init(&t, sc->mps_parent_dmat); 1475 BUS_DMA_TEMPLATE_FILL(&t, BD_MAXSIZE(BUS_SPACE_MAXSIZE_32BIT), 1476 BD_NSEGMENTS(nsegs), BD_MAXSEGSIZE(BUS_SPACE_MAXSIZE_24BIT), 1477 BD_FLAGS(BUS_DMA_ALLOCNOW), BD_LOCKFUNC(busdma_lock_mutex), 1478 BD_LOCKFUNCARG(&sc->mps_mtx)); 1479 if (bus_dma_template_tag(&t, &sc->buffer_dmat)) { 1480 mps_dprint(sc, MPS_ERROR, "Cannot allocate buffer DMA tag\n"); 1481 return (ENOMEM); 1482 } 1483 1484 /* 1485 * SMID 0 cannot be used as a free command per the firmware spec. 1486 * Just drop that command instead of risking accounting bugs. 1487 */ 1488 sc->commands = malloc(sizeof(struct mps_command) * sc->num_reqs, 1489 M_MPT2, M_WAITOK | M_ZERO); 1490 for (i = 1; i < sc->num_reqs; i++) { 1491 cm = &sc->commands[i]; 1492 cm->cm_req = sc->req_frames + i * sc->reqframesz; 1493 cm->cm_req_busaddr = sc->req_busaddr + i * sc->reqframesz; 1494 cm->cm_sense = &sc->sense_frames[i]; 1495 cm->cm_sense_busaddr = sc->sense_busaddr + i * MPS_SENSE_LEN; 1496 cm->cm_desc.Default.SMID = i; 1497 cm->cm_sc = sc; 1498 cm->cm_state = MPS_CM_STATE_BUSY; 1499 TAILQ_INIT(&cm->cm_chain_list); 1500 callout_init_mtx(&cm->cm_callout, &sc->mps_mtx, 0); 1501 1502 /* XXX Is a failure here a critical problem? */ 1503 if (bus_dmamap_create(sc->buffer_dmat, 0, &cm->cm_dmamap) == 0) 1504 if (i <= sc->num_prireqs) 1505 mps_free_high_priority_command(sc, cm); 1506 else 1507 mps_free_command(sc, cm); 1508 else { 1509 panic("failed to allocate command %d\n", i); 1510 sc->num_reqs = i; 1511 break; 1512 } 1513 } 1514 1515 return (0); 1516 } 1517 1518 static int 1519 mps_init_queues(struct mps_softc *sc) 1520 { 1521 int i; 1522 1523 memset((uint8_t *)sc->post_queue, 0xff, sc->pqdepth * 8); 1524 1525 /* 1526 * According to the spec, we need to use one less reply than we 1527 * have space for on the queue. So sc->num_replies (the number we 1528 * use) should be less than sc->fqdepth (allocated size). 1529 */ 1530 if (sc->num_replies >= sc->fqdepth) 1531 return (EINVAL); 1532 1533 /* 1534 * Initialize all of the free queue entries. 1535 */ 1536 for (i = 0; i < sc->fqdepth; i++) 1537 sc->free_queue[i] = sc->reply_busaddr + (i * sc->replyframesz); 1538 sc->replyfreeindex = sc->num_replies; 1539 1540 return (0); 1541 } 1542 1543 /* Get the driver parameter tunables. Lowest priority are the driver defaults. 1544 * Next are the global settings, if they exist. Highest are the per-unit 1545 * settings, if they exist. 1546 */ 1547 void 1548 mps_get_tunables(struct mps_softc *sc) 1549 { 1550 char tmpstr[80], mps_debug[80]; 1551 1552 /* XXX default to some debugging for now */ 1553 sc->mps_debug = MPS_INFO|MPS_FAULT; 1554 sc->disable_msix = 0; 1555 sc->disable_msi = 0; 1556 sc->max_msix = MPS_MSIX_MAX; 1557 sc->max_chains = MPS_CHAIN_FRAMES; 1558 sc->max_io_pages = MPS_MAXIO_PAGES; 1559 sc->enable_ssu = MPS_SSU_ENABLE_SSD_DISABLE_HDD; 1560 sc->spinup_wait_time = DEFAULT_SPINUP_WAIT; 1561 sc->use_phynum = 1; 1562 sc->max_reqframes = MPS_REQ_FRAMES; 1563 sc->max_prireqframes = MPS_PRI_REQ_FRAMES; 1564 sc->max_replyframes = MPS_REPLY_FRAMES; 1565 sc->max_evtframes = MPS_EVT_REPLY_FRAMES; 1566 1567 /* 1568 * Grab the global variables. 1569 */ 1570 bzero(mps_debug, 80); 1571 if (TUNABLE_STR_FETCH("hw.mps.debug_level", mps_debug, 80) != 0) 1572 mps_parse_debug(sc, mps_debug); 1573 TUNABLE_INT_FETCH("hw.mps.disable_msix", &sc->disable_msix); 1574 TUNABLE_INT_FETCH("hw.mps.disable_msi", &sc->disable_msi); 1575 TUNABLE_INT_FETCH("hw.mps.max_msix", &sc->max_msix); 1576 TUNABLE_INT_FETCH("hw.mps.max_chains", &sc->max_chains); 1577 TUNABLE_INT_FETCH("hw.mps.max_io_pages", &sc->max_io_pages); 1578 TUNABLE_INT_FETCH("hw.mps.enable_ssu", &sc->enable_ssu); 1579 TUNABLE_INT_FETCH("hw.mps.spinup_wait_time", &sc->spinup_wait_time); 1580 TUNABLE_INT_FETCH("hw.mps.use_phy_num", &sc->use_phynum); 1581 TUNABLE_INT_FETCH("hw.mps.max_reqframes", &sc->max_reqframes); 1582 TUNABLE_INT_FETCH("hw.mps.max_prireqframes", &sc->max_prireqframes); 1583 TUNABLE_INT_FETCH("hw.mps.max_replyframes", &sc->max_replyframes); 1584 TUNABLE_INT_FETCH("hw.mps.max_evtframes", &sc->max_evtframes); 1585 1586 /* Grab the unit-instance variables */ 1587 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.debug_level", 1588 device_get_unit(sc->mps_dev)); 1589 bzero(mps_debug, 80); 1590 if (TUNABLE_STR_FETCH(tmpstr, mps_debug, 80) != 0) 1591 mps_parse_debug(sc, mps_debug); 1592 1593 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.disable_msix", 1594 device_get_unit(sc->mps_dev)); 1595 TUNABLE_INT_FETCH(tmpstr, &sc->disable_msix); 1596 1597 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.disable_msi", 1598 device_get_unit(sc->mps_dev)); 1599 TUNABLE_INT_FETCH(tmpstr, &sc->disable_msi); 1600 1601 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_msix", 1602 device_get_unit(sc->mps_dev)); 1603 TUNABLE_INT_FETCH(tmpstr, &sc->max_msix); 1604 1605 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_chains", 1606 device_get_unit(sc->mps_dev)); 1607 TUNABLE_INT_FETCH(tmpstr, &sc->max_chains); 1608 1609 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_io_pages", 1610 device_get_unit(sc->mps_dev)); 1611 TUNABLE_INT_FETCH(tmpstr, &sc->max_io_pages); 1612 1613 bzero(sc->exclude_ids, sizeof(sc->exclude_ids)); 1614 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.exclude_ids", 1615 device_get_unit(sc->mps_dev)); 1616 TUNABLE_STR_FETCH(tmpstr, sc->exclude_ids, sizeof(sc->exclude_ids)); 1617 1618 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.enable_ssu", 1619 device_get_unit(sc->mps_dev)); 1620 TUNABLE_INT_FETCH(tmpstr, &sc->enable_ssu); 1621 1622 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.spinup_wait_time", 1623 device_get_unit(sc->mps_dev)); 1624 TUNABLE_INT_FETCH(tmpstr, &sc->spinup_wait_time); 1625 1626 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.use_phy_num", 1627 device_get_unit(sc->mps_dev)); 1628 TUNABLE_INT_FETCH(tmpstr, &sc->use_phynum); 1629 1630 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_reqframes", 1631 device_get_unit(sc->mps_dev)); 1632 TUNABLE_INT_FETCH(tmpstr, &sc->max_reqframes); 1633 1634 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_prireqframes", 1635 device_get_unit(sc->mps_dev)); 1636 TUNABLE_INT_FETCH(tmpstr, &sc->max_prireqframes); 1637 1638 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_replyframes", 1639 device_get_unit(sc->mps_dev)); 1640 TUNABLE_INT_FETCH(tmpstr, &sc->max_replyframes); 1641 1642 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_evtframes", 1643 device_get_unit(sc->mps_dev)); 1644 TUNABLE_INT_FETCH(tmpstr, &sc->max_evtframes); 1645 1646 } 1647 1648 static void 1649 mps_setup_sysctl(struct mps_softc *sc) 1650 { 1651 struct sysctl_ctx_list *sysctl_ctx = NULL; 1652 struct sysctl_oid *sysctl_tree = NULL; 1653 char tmpstr[80], tmpstr2[80]; 1654 1655 /* 1656 * Setup the sysctl variable so the user can change the debug level 1657 * on the fly. 1658 */ 1659 snprintf(tmpstr, sizeof(tmpstr), "MPS controller %d", 1660 device_get_unit(sc->mps_dev)); 1661 snprintf(tmpstr2, sizeof(tmpstr2), "%d", device_get_unit(sc->mps_dev)); 1662 1663 sysctl_ctx = device_get_sysctl_ctx(sc->mps_dev); 1664 if (sysctl_ctx != NULL) 1665 sysctl_tree = device_get_sysctl_tree(sc->mps_dev); 1666 1667 if (sysctl_tree == NULL) { 1668 sysctl_ctx_init(&sc->sysctl_ctx); 1669 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx, 1670 SYSCTL_STATIC_CHILDREN(_hw_mps), OID_AUTO, tmpstr2, 1671 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, tmpstr); 1672 if (sc->sysctl_tree == NULL) 1673 return; 1674 sysctl_ctx = &sc->sysctl_ctx; 1675 sysctl_tree = sc->sysctl_tree; 1676 } 1677 1678 SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1679 OID_AUTO, "debug_level", CTLTYPE_STRING | CTLFLAG_RW |CTLFLAG_MPSAFE, 1680 sc, 0, mps_debug_sysctl, "A", "mps debug level"); 1681 1682 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1683 OID_AUTO, "disable_msix", CTLFLAG_RD, &sc->disable_msix, 0, 1684 "Disable the use of MSI-X interrupts"); 1685 1686 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1687 OID_AUTO, "disable_msi", CTLFLAG_RD, &sc->disable_msi, 0, 1688 "Disable the use of MSI interrupts"); 1689 1690 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1691 OID_AUTO, "max_msix", CTLFLAG_RD, &sc->max_msix, 0, 1692 "User-defined maximum number of MSIX queues"); 1693 1694 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1695 OID_AUTO, "msix_msgs", CTLFLAG_RD, &sc->msi_msgs, 0, 1696 "Negotiated number of MSIX queues"); 1697 1698 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1699 OID_AUTO, "max_reqframes", CTLFLAG_RD, &sc->max_reqframes, 0, 1700 "Total number of allocated request frames"); 1701 1702 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1703 OID_AUTO, "max_prireqframes", CTLFLAG_RD, &sc->max_prireqframes, 0, 1704 "Total number of allocated high priority request frames"); 1705 1706 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1707 OID_AUTO, "max_replyframes", CTLFLAG_RD, &sc->max_replyframes, 0, 1708 "Total number of allocated reply frames"); 1709 1710 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1711 OID_AUTO, "max_evtframes", CTLFLAG_RD, &sc->max_evtframes, 0, 1712 "Total number of event frames allocated"); 1713 1714 SYSCTL_ADD_STRING(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1715 OID_AUTO, "firmware_version", CTLFLAG_RD, sc->fw_version, 1716 strlen(sc->fw_version), "firmware version"); 1717 1718 SYSCTL_ADD_STRING(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1719 OID_AUTO, "driver_version", CTLFLAG_RD, MPS_DRIVER_VERSION, 1720 strlen(MPS_DRIVER_VERSION), "driver version"); 1721 1722 SYSCTL_ADD_STRING(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1723 OID_AUTO, "msg_version", CTLFLAG_RD, sc->msg_version, 1724 strlen(sc->msg_version), "message interface version"); 1725 1726 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1727 OID_AUTO, "io_cmds_active", CTLFLAG_RD, 1728 &sc->io_cmds_active, 0, "number of currently active commands"); 1729 1730 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1731 OID_AUTO, "io_cmds_highwater", CTLFLAG_RD, 1732 &sc->io_cmds_highwater, 0, "maximum active commands seen"); 1733 1734 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1735 OID_AUTO, "chain_free", CTLFLAG_RD, 1736 &sc->chain_free, 0, "number of free chain elements"); 1737 1738 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1739 OID_AUTO, "chain_free_lowwater", CTLFLAG_RD, 1740 &sc->chain_free_lowwater, 0,"lowest number of free chain elements"); 1741 1742 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1743 OID_AUTO, "max_chains", CTLFLAG_RD, 1744 &sc->max_chains, 0,"maximum chain frames that will be allocated"); 1745 1746 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1747 OID_AUTO, "max_io_pages", CTLFLAG_RD, 1748 &sc->max_io_pages, 0,"maximum pages to allow per I/O (if <1 use " 1749 "IOCFacts)"); 1750 1751 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1752 OID_AUTO, "enable_ssu", CTLFLAG_RW, &sc->enable_ssu, 0, 1753 "enable SSU to SATA SSD/HDD at shutdown"); 1754 1755 SYSCTL_ADD_UQUAD(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1756 OID_AUTO, "chain_alloc_fail", CTLFLAG_RD, 1757 &sc->chain_alloc_fail, "chain allocation failures"); 1758 1759 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1760 OID_AUTO, "spinup_wait_time", CTLFLAG_RD, 1761 &sc->spinup_wait_time, DEFAULT_SPINUP_WAIT, "seconds to wait for " 1762 "spinup after SATA ID error"); 1763 1764 SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1765 OID_AUTO, "mapping_table_dump", 1766 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0, 1767 mps_mapping_dump, "A", "Mapping Table Dump"); 1768 1769 SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1770 OID_AUTO, "encl_table_dump", 1771 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0, 1772 mps_mapping_encl_dump, "A", "Enclosure Table Dump"); 1773 1774 SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1775 OID_AUTO, "dump_reqs", 1776 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, 1777 sc, 0, mps_dump_reqs, "I", "Dump Active Requests"); 1778 1779 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1780 OID_AUTO, "dump_reqs_alltypes", CTLFLAG_RW, 1781 &sc->dump_reqs_alltypes, 0, 1782 "dump all request types not just inqueue"); 1783 1784 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree), 1785 OID_AUTO, "use_phy_num", CTLFLAG_RD, &sc->use_phynum, 0, 1786 "Use the phy number for enumeration"); 1787 } 1788 1789 static struct mps_debug_string { 1790 char *name; 1791 int flag; 1792 } mps_debug_strings[] = { 1793 {"info", MPS_INFO}, 1794 {"fault", MPS_FAULT}, 1795 {"event", MPS_EVENT}, 1796 {"log", MPS_LOG}, 1797 {"recovery", MPS_RECOVERY}, 1798 {"error", MPS_ERROR}, 1799 {"init", MPS_INIT}, 1800 {"xinfo", MPS_XINFO}, 1801 {"user", MPS_USER}, 1802 {"mapping", MPS_MAPPING}, 1803 {"trace", MPS_TRACE} 1804 }; 1805 1806 enum mps_debug_level_combiner { 1807 COMB_NONE, 1808 COMB_ADD, 1809 COMB_SUB 1810 }; 1811 1812 static int 1813 mps_debug_sysctl(SYSCTL_HANDLER_ARGS) 1814 { 1815 struct mps_softc *sc; 1816 struct mps_debug_string *string; 1817 struct sbuf *sbuf; 1818 char *buffer; 1819 size_t sz; 1820 int i, len, debug, error; 1821 1822 sc = (struct mps_softc *)arg1; 1823 1824 error = sysctl_wire_old_buffer(req, 0); 1825 if (error != 0) 1826 return (error); 1827 1828 sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req); 1829 debug = sc->mps_debug; 1830 1831 sbuf_printf(sbuf, "%#x", debug); 1832 1833 sz = sizeof(mps_debug_strings) / sizeof(mps_debug_strings[0]); 1834 for (i = 0; i < sz; i++) { 1835 string = &mps_debug_strings[i]; 1836 if (debug & string->flag) 1837 sbuf_printf(sbuf, ",%s", string->name); 1838 } 1839 1840 error = sbuf_finish(sbuf); 1841 sbuf_delete(sbuf); 1842 1843 if (error || req->newptr == NULL) 1844 return (error); 1845 1846 len = req->newlen - req->newidx; 1847 if (len == 0) 1848 return (0); 1849 1850 buffer = malloc(len, M_MPT2, M_ZERO|M_WAITOK); 1851 error = SYSCTL_IN(req, buffer, len); 1852 1853 mps_parse_debug(sc, buffer); 1854 1855 free(buffer, M_MPT2); 1856 return (error); 1857 } 1858 1859 static void 1860 mps_parse_debug(struct mps_softc *sc, char *list) 1861 { 1862 struct mps_debug_string *string; 1863 enum mps_debug_level_combiner op; 1864 char *token, *endtoken; 1865 size_t sz; 1866 int flags, i; 1867 1868 if (list == NULL || *list == '\0') 1869 return; 1870 1871 if (*list == '+') { 1872 op = COMB_ADD; 1873 list++; 1874 } else if (*list == '-') { 1875 op = COMB_SUB; 1876 list++; 1877 } else 1878 op = COMB_NONE; 1879 if (*list == '\0') 1880 return; 1881 1882 flags = 0; 1883 sz = sizeof(mps_debug_strings) / sizeof(mps_debug_strings[0]); 1884 while ((token = strsep(&list, ":,")) != NULL) { 1885 /* Handle integer flags */ 1886 flags |= strtol(token, &endtoken, 0); 1887 if (token != endtoken) 1888 continue; 1889 1890 /* Handle text flags */ 1891 for (i = 0; i < sz; i++) { 1892 string = &mps_debug_strings[i]; 1893 if (strcasecmp(token, string->name) == 0) { 1894 flags |= string->flag; 1895 break; 1896 } 1897 } 1898 } 1899 1900 switch (op) { 1901 case COMB_NONE: 1902 sc->mps_debug = flags; 1903 break; 1904 case COMB_ADD: 1905 sc->mps_debug |= flags; 1906 break; 1907 case COMB_SUB: 1908 sc->mps_debug &= (~flags); 1909 break; 1910 } 1911 1912 return; 1913 } 1914 1915 struct mps_dumpreq_hdr { 1916 uint32_t smid; 1917 uint32_t state; 1918 uint32_t numframes; 1919 uint32_t deschi; 1920 uint32_t desclo; 1921 }; 1922 1923 static int 1924 mps_dump_reqs(SYSCTL_HANDLER_ARGS) 1925 { 1926 struct mps_softc *sc; 1927 struct mps_chain *chain, *chain1; 1928 struct mps_command *cm; 1929 struct mps_dumpreq_hdr hdr; 1930 struct sbuf *sb; 1931 uint32_t smid, state; 1932 int i, numreqs, error = 0; 1933 1934 sc = (struct mps_softc *)arg1; 1935 1936 if ((error = priv_check(curthread, PRIV_DRIVER)) != 0) { 1937 printf("priv check error %d\n", error); 1938 return (error); 1939 } 1940 1941 state = MPS_CM_STATE_INQUEUE; 1942 smid = 1; 1943 numreqs = sc->num_reqs; 1944 1945 if (req->newptr != NULL) 1946 return (EINVAL); 1947 1948 if (smid == 0 || smid > sc->num_reqs) 1949 return (EINVAL); 1950 if (numreqs <= 0 || (numreqs + smid > sc->num_reqs)) 1951 numreqs = sc->num_reqs; 1952 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req); 1953 1954 /* Best effort, no locking */ 1955 for (i = smid; i < numreqs; i++) { 1956 cm = &sc->commands[i]; 1957 if ((sc->dump_reqs_alltypes == 0) && (cm->cm_state != state)) 1958 continue; 1959 hdr.smid = i; 1960 hdr.state = cm->cm_state; 1961 hdr.numframes = 1; 1962 hdr.deschi = cm->cm_desc.Words.High; 1963 hdr.desclo = cm->cm_desc.Words.Low; 1964 TAILQ_FOREACH_SAFE(chain, &cm->cm_chain_list, chain_link, 1965 chain1) 1966 hdr.numframes++; 1967 sbuf_bcat(sb, &hdr, sizeof(hdr)); 1968 sbuf_bcat(sb, cm->cm_req, 128); 1969 TAILQ_FOREACH_SAFE(chain, &cm->cm_chain_list, chain_link, 1970 chain1) 1971 sbuf_bcat(sb, chain->chain, 128); 1972 } 1973 1974 error = sbuf_finish(sb); 1975 sbuf_delete(sb); 1976 return (error); 1977 } 1978 1979 int 1980 mps_attach(struct mps_softc *sc) 1981 { 1982 int error; 1983 1984 MPS_FUNCTRACE(sc); 1985 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__); 1986 1987 mtx_init(&sc->mps_mtx, "MPT2SAS lock", NULL, MTX_DEF); 1988 callout_init_mtx(&sc->periodic, &sc->mps_mtx, 0); 1989 callout_init_mtx(&sc->device_check_callout, &sc->mps_mtx, 0); 1990 TAILQ_INIT(&sc->event_list); 1991 timevalclear(&sc->lastfail); 1992 1993 if ((error = mps_transition_ready(sc)) != 0) { 1994 mps_dprint(sc, MPS_INIT|MPS_FAULT, "failed to transition " 1995 "ready\n"); 1996 return (error); 1997 } 1998 1999 sc->facts = malloc(sizeof(MPI2_IOC_FACTS_REPLY), M_MPT2, 2000 M_ZERO|M_NOWAIT); 2001 if(!sc->facts) { 2002 mps_dprint(sc, MPS_INIT|MPS_FAULT, "Cannot allocate memory, " 2003 "exit\n"); 2004 return (ENOMEM); 2005 } 2006 2007 /* 2008 * Get IOC Facts and allocate all structures based on this information. 2009 * A Diag Reset will also call mps_iocfacts_allocate and re-read the IOC 2010 * Facts. If relevant values have changed in IOC Facts, this function 2011 * will free all of the memory based on IOC Facts and reallocate that 2012 * memory. If this fails, any allocated memory should already be freed. 2013 */ 2014 if ((error = mps_iocfacts_allocate(sc, TRUE)) != 0) { 2015 mps_dprint(sc, MPS_INIT|MPS_FAULT, "IOC Facts based allocation " 2016 "failed with error %d, exit\n", error); 2017 return (error); 2018 } 2019 2020 /* Start the periodic watchdog check on the IOC Doorbell */ 2021 mps_periodic(sc); 2022 2023 /* 2024 * The portenable will kick off discovery events that will drive the 2025 * rest of the initialization process. The CAM/SAS module will 2026 * hold up the boot sequence until discovery is complete. 2027 */ 2028 sc->mps_ich.ich_func = mps_startup; 2029 sc->mps_ich.ich_arg = sc; 2030 if (config_intrhook_establish(&sc->mps_ich) != 0) { 2031 mps_dprint(sc, MPS_INIT|MPS_ERROR, 2032 "Cannot establish MPS config hook\n"); 2033 error = EINVAL; 2034 } 2035 2036 /* 2037 * Allow IR to shutdown gracefully when shutdown occurs. 2038 */ 2039 sc->shutdown_eh = EVENTHANDLER_REGISTER(shutdown_final, 2040 mpssas_ir_shutdown, sc, SHUTDOWN_PRI_DEFAULT); 2041 2042 if (sc->shutdown_eh == NULL) 2043 mps_dprint(sc, MPS_INIT|MPS_ERROR, 2044 "shutdown event registration failed\n"); 2045 2046 mps_setup_sysctl(sc); 2047 2048 sc->mps_flags |= MPS_FLAGS_ATTACH_DONE; 2049 mps_dprint(sc, MPS_INIT, "%s exit error= %d\n", __func__, error); 2050 2051 return (error); 2052 } 2053 2054 /* Run through any late-start handlers. */ 2055 static void 2056 mps_startup(void *arg) 2057 { 2058 struct mps_softc *sc; 2059 2060 sc = (struct mps_softc *)arg; 2061 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__); 2062 2063 mps_lock(sc); 2064 mps_unmask_intr(sc); 2065 2066 /* initialize device mapping tables */ 2067 mps_base_static_config_pages(sc); 2068 mps_mapping_initialize(sc); 2069 mpssas_startup(sc); 2070 mps_unlock(sc); 2071 2072 mps_dprint(sc, MPS_INIT, "disestablish config intrhook\n"); 2073 config_intrhook_disestablish(&sc->mps_ich); 2074 sc->mps_ich.ich_arg = NULL; 2075 2076 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__); 2077 } 2078 2079 /* Periodic watchdog. Is called with the driver lock already held. */ 2080 static void 2081 mps_periodic(void *arg) 2082 { 2083 struct mps_softc *sc; 2084 uint32_t db; 2085 2086 sc = (struct mps_softc *)arg; 2087 if (sc->mps_flags & MPS_FLAGS_SHUTDOWN) 2088 return; 2089 2090 db = mps_regread(sc, MPI2_DOORBELL_OFFSET); 2091 if ((db & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) { 2092 mps_dprint(sc, MPS_FAULT, "IOC Fault 0x%08x, Resetting\n", db); 2093 mps_reinit(sc); 2094 } 2095 2096 callout_reset_sbt(&sc->periodic, MPS_PERIODIC_DELAY * SBT_1S, 0, 2097 mps_periodic, sc, C_PREL(1)); 2098 } 2099 2100 static void 2101 mps_log_evt_handler(struct mps_softc *sc, uintptr_t data, 2102 MPI2_EVENT_NOTIFICATION_REPLY *event) 2103 { 2104 MPI2_EVENT_DATA_LOG_ENTRY_ADDED *entry; 2105 2106 MPS_DPRINT_EVENT(sc, generic, event); 2107 2108 switch (event->Event) { 2109 case MPI2_EVENT_LOG_DATA: 2110 mps_dprint(sc, MPS_EVENT, "MPI2_EVENT_LOG_DATA:\n"); 2111 if (sc->mps_debug & MPS_EVENT) 2112 hexdump(event->EventData, event->EventDataLength, NULL, 0); 2113 break; 2114 case MPI2_EVENT_LOG_ENTRY_ADDED: 2115 entry = (MPI2_EVENT_DATA_LOG_ENTRY_ADDED *)event->EventData; 2116 mps_dprint(sc, MPS_EVENT, "MPI2_EVENT_LOG_ENTRY_ADDED event " 2117 "0x%x Sequence %d:\n", entry->LogEntryQualifier, 2118 entry->LogSequence); 2119 break; 2120 default: 2121 break; 2122 } 2123 return; 2124 } 2125 2126 static int 2127 mps_attach_log(struct mps_softc *sc) 2128 { 2129 u32 events[MPI2_EVENT_NOTIFY_EVENTMASK_WORDS]; 2130 2131 bzero(events, 16); 2132 setbit(events, MPI2_EVENT_LOG_DATA); 2133 setbit(events, MPI2_EVENT_LOG_ENTRY_ADDED); 2134 2135 mps_register_events(sc, events, mps_log_evt_handler, NULL, 2136 &sc->mps_log_eh); 2137 2138 return (0); 2139 } 2140 2141 static int 2142 mps_detach_log(struct mps_softc *sc) 2143 { 2144 2145 if (sc->mps_log_eh != NULL) 2146 mps_deregister_events(sc, sc->mps_log_eh); 2147 return (0); 2148 } 2149 2150 /* 2151 * Free all of the driver resources and detach submodules. Should be called 2152 * without the lock held. 2153 */ 2154 int 2155 mps_free(struct mps_softc *sc) 2156 { 2157 int error; 2158 2159 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__); 2160 /* Turn off the watchdog */ 2161 mps_lock(sc); 2162 sc->mps_flags |= MPS_FLAGS_SHUTDOWN; 2163 mps_unlock(sc); 2164 /* Lock must not be held for this */ 2165 callout_drain(&sc->periodic); 2166 callout_drain(&sc->device_check_callout); 2167 2168 if (((error = mps_detach_log(sc)) != 0) || 2169 ((error = mps_detach_sas(sc)) != 0)) { 2170 mps_dprint(sc, MPS_INIT|MPS_FAULT, "failed to detach " 2171 "subsystems, exit\n"); 2172 return (error); 2173 } 2174 2175 mps_detach_user(sc); 2176 2177 /* Put the IOC back in the READY state. */ 2178 mps_lock(sc); 2179 if ((error = mps_transition_ready(sc)) != 0) { 2180 mps_unlock(sc); 2181 return (error); 2182 } 2183 mps_unlock(sc); 2184 2185 if (sc->facts != NULL) 2186 free(sc->facts, M_MPT2); 2187 2188 /* 2189 * Free all buffers that are based on IOC Facts. A Diag Reset may need 2190 * to free these buffers too. 2191 */ 2192 mps_iocfacts_free(sc); 2193 2194 if (sc->sysctl_tree != NULL) 2195 sysctl_ctx_free(&sc->sysctl_ctx); 2196 2197 /* Deregister the shutdown function */ 2198 if (sc->shutdown_eh != NULL) 2199 EVENTHANDLER_DEREGISTER(shutdown_final, sc->shutdown_eh); 2200 2201 mtx_destroy(&sc->mps_mtx); 2202 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__); 2203 2204 return (0); 2205 } 2206 2207 static __inline void 2208 mps_complete_command(struct mps_softc *sc, struct mps_command *cm) 2209 { 2210 MPS_FUNCTRACE(sc); 2211 2212 if (cm == NULL) { 2213 mps_dprint(sc, MPS_ERROR, "Completing NULL command\n"); 2214 return; 2215 } 2216 2217 KASSERT(cm->cm_state == MPS_CM_STATE_INQUEUE, 2218 ("command not inqueue, state = %u\n", cm->cm_state)); 2219 cm->cm_state = MPS_CM_STATE_BUSY; 2220 if (cm->cm_flags & MPS_CM_FLAGS_POLLED) 2221 cm->cm_flags |= MPS_CM_FLAGS_COMPLETE; 2222 2223 if (cm->cm_complete != NULL) { 2224 mps_dprint(sc, MPS_TRACE, 2225 "%s cm %p calling cm_complete %p data %p reply %p\n", 2226 __func__, cm, cm->cm_complete, cm->cm_complete_data, 2227 cm->cm_reply); 2228 cm->cm_complete(sc, cm); 2229 } 2230 2231 if (cm->cm_flags & MPS_CM_FLAGS_WAKEUP) { 2232 mps_dprint(sc, MPS_TRACE, "waking up %p\n", cm); 2233 wakeup(cm); 2234 } 2235 2236 if (cm->cm_sc->io_cmds_active != 0) { 2237 cm->cm_sc->io_cmds_active--; 2238 } else { 2239 mps_dprint(sc, MPS_ERROR, "Warning: io_cmds_active is " 2240 "out of sync - resynching to 0\n"); 2241 } 2242 } 2243 2244 static void 2245 mps_sas_log_info(struct mps_softc *sc , u32 log_info) 2246 { 2247 union loginfo_type { 2248 u32 loginfo; 2249 struct { 2250 u32 subcode:16; 2251 u32 code:8; 2252 u32 originator:4; 2253 u32 bus_type:4; 2254 } dw; 2255 }; 2256 union loginfo_type sas_loginfo; 2257 char *originator_str = NULL; 2258 2259 sas_loginfo.loginfo = log_info; 2260 if (sas_loginfo.dw.bus_type != 3 /*SAS*/) 2261 return; 2262 2263 /* each nexus loss loginfo */ 2264 if (log_info == 0x31170000) 2265 return; 2266 2267 /* eat the loginfos associated with task aborts */ 2268 if ((log_info == 30050000 || log_info == 2269 0x31140000 || log_info == 0x31130000)) 2270 return; 2271 2272 switch (sas_loginfo.dw.originator) { 2273 case 0: 2274 originator_str = "IOP"; 2275 break; 2276 case 1: 2277 originator_str = "PL"; 2278 break; 2279 case 2: 2280 originator_str = "IR"; 2281 break; 2282 } 2283 2284 mps_dprint(sc, MPS_LOG, "log_info(0x%08x): originator(%s), " 2285 "code(0x%02x), sub_code(0x%04x)\n", log_info, 2286 originator_str, sas_loginfo.dw.code, 2287 sas_loginfo.dw.subcode); 2288 } 2289 2290 static void 2291 mps_display_reply_info(struct mps_softc *sc, uint8_t *reply) 2292 { 2293 MPI2DefaultReply_t *mpi_reply; 2294 u16 sc_status; 2295 2296 mpi_reply = (MPI2DefaultReply_t*)reply; 2297 sc_status = le16toh(mpi_reply->IOCStatus); 2298 if (sc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) 2299 mps_sas_log_info(sc, le32toh(mpi_reply->IOCLogInfo)); 2300 } 2301 void 2302 mps_intr(void *data) 2303 { 2304 struct mps_softc *sc; 2305 uint32_t status; 2306 2307 sc = (struct mps_softc *)data; 2308 mps_dprint(sc, MPS_TRACE, "%s\n", __func__); 2309 2310 /* 2311 * Check interrupt status register to flush the bus. This is 2312 * needed for both INTx interrupts and driver-driven polling 2313 */ 2314 status = mps_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET); 2315 if ((status & MPI2_HIS_REPLY_DESCRIPTOR_INTERRUPT) == 0) 2316 return; 2317 2318 mps_lock(sc); 2319 mps_intr_locked(data); 2320 mps_unlock(sc); 2321 return; 2322 } 2323 2324 /* 2325 * In theory, MSI/MSIX interrupts shouldn't need to read any registers on the 2326 * chip. Hopefully this theory is correct. 2327 */ 2328 void 2329 mps_intr_msi(void *data) 2330 { 2331 struct mps_softc *sc; 2332 2333 sc = (struct mps_softc *)data; 2334 mps_dprint(sc, MPS_TRACE, "%s\n", __func__); 2335 mps_lock(sc); 2336 mps_intr_locked(data); 2337 mps_unlock(sc); 2338 return; 2339 } 2340 2341 /* 2342 * The locking is overly broad and simplistic, but easy to deal with for now. 2343 */ 2344 void 2345 mps_intr_locked(void *data) 2346 { 2347 MPI2_REPLY_DESCRIPTORS_UNION *desc; 2348 MPI2_DIAG_RELEASE_REPLY *rel_rep; 2349 mps_fw_diagnostic_buffer_t *pBuffer; 2350 struct mps_softc *sc; 2351 struct mps_command *cm = NULL; 2352 uint64_t tdesc; 2353 uint8_t flags; 2354 u_int pq; 2355 2356 sc = (struct mps_softc *)data; 2357 2358 pq = sc->replypostindex; 2359 mps_dprint(sc, MPS_TRACE, 2360 "%s sc %p starting with replypostindex %u\n", 2361 __func__, sc, sc->replypostindex); 2362 2363 for ( ;; ) { 2364 cm = NULL; 2365 desc = &sc->post_queue[sc->replypostindex]; 2366 2367 /* 2368 * Copy and clear out the descriptor so that any reentry will 2369 * immediately know that this descriptor has already been 2370 * looked at. There is unfortunate casting magic because the 2371 * MPI API doesn't have a cardinal 64bit type. 2372 */ 2373 tdesc = 0xffffffffffffffff; 2374 tdesc = atomic_swap_64((uint64_t *)desc, tdesc); 2375 desc = (MPI2_REPLY_DESCRIPTORS_UNION *)&tdesc; 2376 2377 flags = desc->Default.ReplyFlags & 2378 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK; 2379 if ((flags == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) 2380 || (le32toh(desc->Words.High) == 0xffffffff)) 2381 break; 2382 2383 /* increment the replypostindex now, so that event handlers 2384 * and cm completion handlers which decide to do a diag 2385 * reset can zero it without it getting incremented again 2386 * afterwards, and we break out of this loop on the next 2387 * iteration since the reply post queue has been cleared to 2388 * 0xFF and all descriptors look unused (which they are). 2389 */ 2390 if (++sc->replypostindex >= sc->pqdepth) 2391 sc->replypostindex = 0; 2392 2393 switch (flags) { 2394 case MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS: 2395 cm = &sc->commands[le16toh(desc->SCSIIOSuccess.SMID)]; 2396 cm->cm_reply = NULL; 2397 break; 2398 case MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY: 2399 { 2400 uint32_t baddr; 2401 uint8_t *reply; 2402 2403 /* 2404 * Re-compose the reply address from the address 2405 * sent back from the chip. The ReplyFrameAddress 2406 * is the lower 32 bits of the physical address of 2407 * particular reply frame. Convert that address to 2408 * host format, and then use that to provide the 2409 * offset against the virtual address base 2410 * (sc->reply_frames). 2411 */ 2412 baddr = le32toh(desc->AddressReply.ReplyFrameAddress); 2413 reply = sc->reply_frames + 2414 (baddr - ((uint32_t)sc->reply_busaddr)); 2415 /* 2416 * Make sure the reply we got back is in a valid 2417 * range. If not, go ahead and panic here, since 2418 * we'll probably panic as soon as we deference the 2419 * reply pointer anyway. 2420 */ 2421 if ((reply < sc->reply_frames) 2422 || (reply > (sc->reply_frames + 2423 (sc->fqdepth * sc->replyframesz)))) { 2424 printf("%s: WARNING: reply %p out of range!\n", 2425 __func__, reply); 2426 printf("%s: reply_frames %p, fqdepth %d, " 2427 "frame size %d\n", __func__, 2428 sc->reply_frames, sc->fqdepth, 2429 sc->replyframesz); 2430 printf("%s: baddr %#x,\n", __func__, baddr); 2431 /* LSI-TODO. See Linux Code for Graceful exit */ 2432 panic("Reply address out of range"); 2433 } 2434 if (le16toh(desc->AddressReply.SMID) == 0) { 2435 if (((MPI2_DEFAULT_REPLY *)reply)->Function == 2436 MPI2_FUNCTION_DIAG_BUFFER_POST) { 2437 /* 2438 * If SMID is 0 for Diag Buffer Post, 2439 * this implies that the reply is due to 2440 * a release function with a status that 2441 * the buffer has been released. Set 2442 * the buffer flags accordingly. 2443 */ 2444 rel_rep = 2445 (MPI2_DIAG_RELEASE_REPLY *)reply; 2446 if ((le16toh(rel_rep->IOCStatus) & 2447 MPI2_IOCSTATUS_MASK) == 2448 MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED) 2449 { 2450 pBuffer = 2451 &sc->fw_diag_buffer_list[ 2452 rel_rep->BufferType]; 2453 pBuffer->valid_data = TRUE; 2454 pBuffer->owned_by_firmware = 2455 FALSE; 2456 pBuffer->immediate = FALSE; 2457 } 2458 } else 2459 mps_dispatch_event(sc, baddr, 2460 (MPI2_EVENT_NOTIFICATION_REPLY *) 2461 reply); 2462 } else { 2463 /* 2464 * Ignore commands not in INQUEUE state 2465 * since they've already been completed 2466 * via another path. 2467 */ 2468 cm = &sc->commands[ 2469 le16toh(desc->AddressReply.SMID)]; 2470 if (cm->cm_state == MPS_CM_STATE_INQUEUE) { 2471 cm->cm_reply = reply; 2472 cm->cm_reply_data = le32toh( 2473 desc->AddressReply.ReplyFrameAddress); 2474 } else { 2475 mps_dprint(sc, MPS_RECOVERY, 2476 "Bad state for ADDRESS_REPLY status," 2477 " ignoring state %d cm %p\n", 2478 cm->cm_state, cm); 2479 } 2480 } 2481 break; 2482 } 2483 case MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS: 2484 case MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER: 2485 case MPI2_RPY_DESCRIPT_FLAGS_RAID_ACCELERATOR_SUCCESS: 2486 default: 2487 /* Unhandled */ 2488 mps_dprint(sc, MPS_ERROR, "Unhandled reply 0x%x\n", 2489 desc->Default.ReplyFlags); 2490 cm = NULL; 2491 break; 2492 } 2493 2494 2495 if (cm != NULL) { 2496 // Print Error reply frame 2497 if (cm->cm_reply) 2498 mps_display_reply_info(sc,cm->cm_reply); 2499 mps_complete_command(sc, cm); 2500 } 2501 } 2502 2503 if (pq != sc->replypostindex) { 2504 mps_dprint(sc, MPS_TRACE, "%s sc %p writing postindex %d\n", 2505 __func__, sc, sc->replypostindex); 2506 mps_regwrite(sc, MPI2_REPLY_POST_HOST_INDEX_OFFSET, 2507 sc->replypostindex); 2508 } 2509 2510 return; 2511 } 2512 2513 static void 2514 mps_dispatch_event(struct mps_softc *sc, uintptr_t data, 2515 MPI2_EVENT_NOTIFICATION_REPLY *reply) 2516 { 2517 struct mps_event_handle *eh; 2518 int event, handled = 0; 2519 2520 event = le16toh(reply->Event); 2521 TAILQ_FOREACH(eh, &sc->event_list, eh_list) { 2522 if (isset(eh->mask, event)) { 2523 eh->callback(sc, data, reply); 2524 handled++; 2525 } 2526 } 2527 2528 if (handled == 0) 2529 mps_dprint(sc, MPS_EVENT, "Unhandled event 0x%x\n", le16toh(event)); 2530 2531 /* 2532 * This is the only place that the event/reply should be freed. 2533 * Anything wanting to hold onto the event data should have 2534 * already copied it into their own storage. 2535 */ 2536 mps_free_reply(sc, data); 2537 } 2538 2539 static void 2540 mps_reregister_events_complete(struct mps_softc *sc, struct mps_command *cm) 2541 { 2542 mps_dprint(sc, MPS_TRACE, "%s\n", __func__); 2543 2544 if (cm->cm_reply) 2545 MPS_DPRINT_EVENT(sc, generic, 2546 (MPI2_EVENT_NOTIFICATION_REPLY *)cm->cm_reply); 2547 2548 mps_free_command(sc, cm); 2549 2550 /* next, send a port enable */ 2551 mpssas_startup(sc); 2552 } 2553 2554 /* 2555 * For both register_events and update_events, the caller supplies a bitmap 2556 * of events that it _wants_. These functions then turn that into a bitmask 2557 * suitable for the controller. 2558 */ 2559 int 2560 mps_register_events(struct mps_softc *sc, u32 *mask, 2561 mps_evt_callback_t *cb, void *data, struct mps_event_handle **handle) 2562 { 2563 struct mps_event_handle *eh; 2564 int error = 0; 2565 2566 eh = malloc(sizeof(struct mps_event_handle), M_MPT2, M_WAITOK|M_ZERO); 2567 eh->callback = cb; 2568 eh->data = data; 2569 TAILQ_INSERT_TAIL(&sc->event_list, eh, eh_list); 2570 if (mask != NULL) 2571 error = mps_update_events(sc, eh, mask); 2572 *handle = eh; 2573 2574 return (error); 2575 } 2576 2577 int 2578 mps_update_events(struct mps_softc *sc, struct mps_event_handle *handle, 2579 u32 *mask) 2580 { 2581 MPI2_EVENT_NOTIFICATION_REQUEST *evtreq; 2582 MPI2_EVENT_NOTIFICATION_REPLY *reply = NULL; 2583 struct mps_command *cm; 2584 int error, i; 2585 2586 mps_dprint(sc, MPS_TRACE, "%s\n", __func__); 2587 2588 if ((mask != NULL) && (handle != NULL)) 2589 bcopy(mask, &handle->mask[0], sizeof(u32) * 2590 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS); 2591 2592 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) 2593 sc->event_mask[i] = -1; 2594 2595 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) 2596 sc->event_mask[i] &= ~handle->mask[i]; 2597 2598 if ((cm = mps_alloc_command(sc)) == NULL) 2599 return (EBUSY); 2600 evtreq = (MPI2_EVENT_NOTIFICATION_REQUEST *)cm->cm_req; 2601 evtreq->Function = MPI2_FUNCTION_EVENT_NOTIFICATION; 2602 evtreq->MsgFlags = 0; 2603 evtreq->SASBroadcastPrimitiveMasks = 0; 2604 #ifdef MPS_DEBUG_ALL_EVENTS 2605 { 2606 u_char fullmask[16]; 2607 memset(fullmask, 0x00, 16); 2608 bcopy(fullmask, &evtreq->EventMasks[0], sizeof(u32) * 2609 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS); 2610 } 2611 #else 2612 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) 2613 evtreq->EventMasks[i] = 2614 htole32(sc->event_mask[i]); 2615 #endif 2616 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE; 2617 cm->cm_data = NULL; 2618 2619 error = mps_wait_command(sc, &cm, 60, 0); 2620 if (cm != NULL) 2621 reply = (MPI2_EVENT_NOTIFICATION_REPLY *)cm->cm_reply; 2622 if ((reply == NULL) || 2623 (reply->IOCStatus & MPI2_IOCSTATUS_MASK) != MPI2_IOCSTATUS_SUCCESS) 2624 error = ENXIO; 2625 2626 if (reply) 2627 MPS_DPRINT_EVENT(sc, generic, reply); 2628 2629 mps_dprint(sc, MPS_TRACE, "%s finished error %d\n", __func__, error); 2630 2631 if (cm != NULL) 2632 mps_free_command(sc, cm); 2633 return (error); 2634 } 2635 2636 static int 2637 mps_reregister_events(struct mps_softc *sc) 2638 { 2639 MPI2_EVENT_NOTIFICATION_REQUEST *evtreq; 2640 struct mps_command *cm; 2641 struct mps_event_handle *eh; 2642 int error, i; 2643 2644 mps_dprint(sc, MPS_TRACE, "%s\n", __func__); 2645 2646 /* first, reregister events */ 2647 2648 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) 2649 sc->event_mask[i] = -1; 2650 2651 TAILQ_FOREACH(eh, &sc->event_list, eh_list) { 2652 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) 2653 sc->event_mask[i] &= ~eh->mask[i]; 2654 } 2655 2656 if ((cm = mps_alloc_command(sc)) == NULL) 2657 return (EBUSY); 2658 evtreq = (MPI2_EVENT_NOTIFICATION_REQUEST *)cm->cm_req; 2659 evtreq->Function = MPI2_FUNCTION_EVENT_NOTIFICATION; 2660 evtreq->MsgFlags = 0; 2661 evtreq->SASBroadcastPrimitiveMasks = 0; 2662 #ifdef MPS_DEBUG_ALL_EVENTS 2663 { 2664 u_char fullmask[16]; 2665 memset(fullmask, 0x00, 16); 2666 bcopy(fullmask, &evtreq->EventMasks[0], sizeof(u32) * 2667 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS); 2668 } 2669 #else 2670 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) 2671 evtreq->EventMasks[i] = 2672 htole32(sc->event_mask[i]); 2673 #endif 2674 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE; 2675 cm->cm_data = NULL; 2676 cm->cm_complete = mps_reregister_events_complete; 2677 2678 error = mps_map_command(sc, cm); 2679 2680 mps_dprint(sc, MPS_TRACE, "%s finished with error %d\n", __func__, 2681 error); 2682 return (error); 2683 } 2684 2685 void 2686 mps_deregister_events(struct mps_softc *sc, struct mps_event_handle *handle) 2687 { 2688 2689 TAILQ_REMOVE(&sc->event_list, handle, eh_list); 2690 free(handle, M_MPT2); 2691 } 2692 2693 /* 2694 * Add a chain element as the next SGE for the specified command. 2695 * Reset cm_sge and cm_sgesize to indicate all the available space. 2696 */ 2697 static int 2698 mps_add_chain(struct mps_command *cm) 2699 { 2700 MPI2_SGE_CHAIN64 *sgc; 2701 struct mps_chain *chain; 2702 u_int space; 2703 2704 if (cm->cm_sglsize < MPS_SGC_SIZE) 2705 panic("MPS: Need SGE Error Code\n"); 2706 2707 chain = mps_alloc_chain(cm->cm_sc); 2708 if (chain == NULL) 2709 return (ENOBUFS); 2710 2711 space = cm->cm_sc->reqframesz; 2712 2713 /* 2714 * Note: a double-linked list is used to make it easier to 2715 * walk for debugging. 2716 */ 2717 TAILQ_INSERT_TAIL(&cm->cm_chain_list, chain, chain_link); 2718 2719 sgc = (MPI2_SGE_CHAIN64 *)&cm->cm_sge->MpiChain; 2720 sgc->Length = htole16(space); 2721 sgc->NextChainOffset = 0; 2722 /* TODO Looks like bug in Setting sgc->Flags. 2723 * sgc->Flags = ( MPI2_SGE_FLAGS_CHAIN_ELEMENT | MPI2_SGE_FLAGS_64_BIT_ADDRESSING | 2724 * MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT 2725 * This is fine.. because we are not using simple element. In case of 2726 * MPI2_SGE_CHAIN64, we have separate Length and Flags field. 2727 */ 2728 sgc->Flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT | MPI2_SGE_FLAGS_64_BIT_ADDRESSING; 2729 sgc->Address.High = htole32(chain->chain_busaddr >> 32); 2730 sgc->Address.Low = htole32(chain->chain_busaddr); 2731 2732 cm->cm_sge = (MPI2_SGE_IO_UNION *)&chain->chain->MpiSimple; 2733 cm->cm_sglsize = space; 2734 return (0); 2735 } 2736 2737 /* 2738 * Add one scatter-gather element (chain, simple, transaction context) 2739 * to the scatter-gather list for a command. Maintain cm_sglsize and 2740 * cm_sge as the remaining size and pointer to the next SGE to fill 2741 * in, respectively. 2742 */ 2743 int 2744 mps_push_sge(struct mps_command *cm, void *sgep, size_t len, int segsleft) 2745 { 2746 MPI2_SGE_TRANSACTION_UNION *tc = sgep; 2747 MPI2_SGE_SIMPLE64 *sge = sgep; 2748 int error, type; 2749 uint32_t saved_buf_len, saved_address_low, saved_address_high; 2750 2751 type = (tc->Flags & MPI2_SGE_FLAGS_ELEMENT_MASK); 2752 2753 #ifdef INVARIANTS 2754 switch (type) { 2755 case MPI2_SGE_FLAGS_TRANSACTION_ELEMENT: { 2756 if (len != tc->DetailsLength + 4) 2757 panic("TC %p length %u or %zu?", tc, 2758 tc->DetailsLength + 4, len); 2759 } 2760 break; 2761 case MPI2_SGE_FLAGS_CHAIN_ELEMENT: 2762 /* Driver only uses 64-bit chain elements */ 2763 if (len != MPS_SGC_SIZE) 2764 panic("CHAIN %p length %u or %zu?", sgep, 2765 MPS_SGC_SIZE, len); 2766 break; 2767 case MPI2_SGE_FLAGS_SIMPLE_ELEMENT: 2768 /* Driver only uses 64-bit SGE simple elements */ 2769 if (len != MPS_SGE64_SIZE) 2770 panic("SGE simple %p length %u or %zu?", sge, 2771 MPS_SGE64_SIZE, len); 2772 if (((le32toh(sge->FlagsLength) >> MPI2_SGE_FLAGS_SHIFT) & 2773 MPI2_SGE_FLAGS_ADDRESS_SIZE) == 0) 2774 panic("SGE simple %p not marked 64-bit?", sge); 2775 2776 break; 2777 default: 2778 panic("Unexpected SGE %p, flags %02x", tc, tc->Flags); 2779 } 2780 #endif 2781 2782 /* 2783 * case 1: 1 more segment, enough room for it 2784 * case 2: 2 more segments, enough room for both 2785 * case 3: >=2 more segments, only enough room for 1 and a chain 2786 * case 4: >=1 more segment, enough room for only a chain 2787 * case 5: >=1 more segment, no room for anything (error) 2788 */ 2789 2790 /* 2791 * There should be room for at least a chain element, or this 2792 * code is buggy. Case (5). 2793 */ 2794 if (cm->cm_sglsize < MPS_SGC_SIZE) 2795 panic("MPS: Need SGE Error Code\n"); 2796 2797 if (segsleft >= 1 && cm->cm_sglsize < len + MPS_SGC_SIZE) { 2798 /* 2799 * 1 or more segment, enough room for only a chain. 2800 * Hope the previous element wasn't a Simple entry 2801 * that needed to be marked with 2802 * MPI2_SGE_FLAGS_LAST_ELEMENT. Case (4). 2803 */ 2804 if ((error = mps_add_chain(cm)) != 0) 2805 return (error); 2806 } 2807 2808 if (segsleft >= 2 && 2809 cm->cm_sglsize < len + MPS_SGC_SIZE + MPS_SGE64_SIZE) { 2810 /* 2811 * There are 2 or more segments left to add, and only 2812 * enough room for 1 and a chain. Case (3). 2813 * 2814 * Mark as last element in this chain if necessary. 2815 */ 2816 if (type == MPI2_SGE_FLAGS_SIMPLE_ELEMENT) { 2817 sge->FlagsLength |= htole32( 2818 MPI2_SGE_FLAGS_LAST_ELEMENT << MPI2_SGE_FLAGS_SHIFT); 2819 } 2820 2821 /* 2822 * Add the item then a chain. Do the chain now, 2823 * rather than on the next iteration, to simplify 2824 * understanding the code. 2825 */ 2826 cm->cm_sglsize -= len; 2827 bcopy(sgep, cm->cm_sge, len); 2828 cm->cm_sge = (MPI2_SGE_IO_UNION *)((uintptr_t)cm->cm_sge + len); 2829 return (mps_add_chain(cm)); 2830 } 2831 2832 #ifdef INVARIANTS 2833 /* Case 1: 1 more segment, enough room for it. */ 2834 if (segsleft == 1 && cm->cm_sglsize < len) 2835 panic("1 seg left and no room? %u versus %zu", 2836 cm->cm_sglsize, len); 2837 2838 /* Case 2: 2 more segments, enough room for both */ 2839 if (segsleft == 2 && cm->cm_sglsize < len + MPS_SGE64_SIZE) 2840 panic("2 segs left and no room? %u versus %zu", 2841 cm->cm_sglsize, len); 2842 #endif 2843 2844 if (segsleft == 1 && type == MPI2_SGE_FLAGS_SIMPLE_ELEMENT) { 2845 /* 2846 * If this is a bi-directional request, need to account for that 2847 * here. Save the pre-filled sge values. These will be used 2848 * either for the 2nd SGL or for a single direction SGL. If 2849 * cm_out_len is non-zero, this is a bi-directional request, so 2850 * fill in the OUT SGL first, then the IN SGL, otherwise just 2851 * fill in the IN SGL. Note that at this time, when filling in 2852 * 2 SGL's for a bi-directional request, they both use the same 2853 * DMA buffer (same cm command). 2854 */ 2855 saved_buf_len = le32toh(sge->FlagsLength) & 0x00FFFFFF; 2856 saved_address_low = sge->Address.Low; 2857 saved_address_high = sge->Address.High; 2858 if (cm->cm_out_len) { 2859 sge->FlagsLength = htole32(cm->cm_out_len | 2860 ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT | 2861 MPI2_SGE_FLAGS_END_OF_BUFFER | 2862 MPI2_SGE_FLAGS_HOST_TO_IOC | 2863 MPI2_SGE_FLAGS_64_BIT_ADDRESSING) << 2864 MPI2_SGE_FLAGS_SHIFT)); 2865 cm->cm_sglsize -= len; 2866 bcopy(sgep, cm->cm_sge, len); 2867 cm->cm_sge = (MPI2_SGE_IO_UNION *)((uintptr_t)cm->cm_sge 2868 + len); 2869 } 2870 saved_buf_len |= 2871 ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT | 2872 MPI2_SGE_FLAGS_END_OF_BUFFER | 2873 MPI2_SGE_FLAGS_LAST_ELEMENT | 2874 MPI2_SGE_FLAGS_END_OF_LIST | 2875 MPI2_SGE_FLAGS_64_BIT_ADDRESSING) << 2876 MPI2_SGE_FLAGS_SHIFT); 2877 if (cm->cm_flags & MPS_CM_FLAGS_DATAIN) { 2878 saved_buf_len |= 2879 ((uint32_t)(MPI2_SGE_FLAGS_IOC_TO_HOST) << 2880 MPI2_SGE_FLAGS_SHIFT); 2881 } else { 2882 saved_buf_len |= 2883 ((uint32_t)(MPI2_SGE_FLAGS_HOST_TO_IOC) << 2884 MPI2_SGE_FLAGS_SHIFT); 2885 } 2886 sge->FlagsLength = htole32(saved_buf_len); 2887 sge->Address.Low = saved_address_low; 2888 sge->Address.High = saved_address_high; 2889 } 2890 2891 cm->cm_sglsize -= len; 2892 bcopy(sgep, cm->cm_sge, len); 2893 cm->cm_sge = (MPI2_SGE_IO_UNION *)((uintptr_t)cm->cm_sge + len); 2894 return (0); 2895 } 2896 2897 /* 2898 * Add one dma segment to the scatter-gather list for a command. 2899 */ 2900 int 2901 mps_add_dmaseg(struct mps_command *cm, vm_paddr_t pa, size_t len, u_int flags, 2902 int segsleft) 2903 { 2904 MPI2_SGE_SIMPLE64 sge; 2905 2906 /* 2907 * This driver always uses 64-bit address elements for simplicity. 2908 */ 2909 bzero(&sge, sizeof(sge)); 2910 flags |= MPI2_SGE_FLAGS_SIMPLE_ELEMENT | 2911 MPI2_SGE_FLAGS_64_BIT_ADDRESSING; 2912 sge.FlagsLength = htole32(len | (flags << MPI2_SGE_FLAGS_SHIFT)); 2913 mps_from_u64(pa, &sge.Address); 2914 2915 return (mps_push_sge(cm, &sge, sizeof sge, segsleft)); 2916 } 2917 2918 static void 2919 mps_data_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 2920 { 2921 struct mps_softc *sc; 2922 struct mps_command *cm; 2923 u_int i, dir, sflags; 2924 2925 cm = (struct mps_command *)arg; 2926 sc = cm->cm_sc; 2927 2928 /* 2929 * In this case, just print out a warning and let the chip tell the 2930 * user they did the wrong thing. 2931 */ 2932 if ((cm->cm_max_segs != 0) && (nsegs > cm->cm_max_segs)) { 2933 mps_dprint(sc, MPS_ERROR, 2934 "%s: warning: busdma returned %d segments, " 2935 "more than the %d allowed\n", __func__, nsegs, 2936 cm->cm_max_segs); 2937 } 2938 2939 /* 2940 * Set up DMA direction flags. Bi-directional requests are also handled 2941 * here. In that case, both direction flags will be set. 2942 */ 2943 sflags = 0; 2944 if (cm->cm_flags & MPS_CM_FLAGS_SMP_PASS) { 2945 /* 2946 * We have to add a special case for SMP passthrough, there 2947 * is no easy way to generically handle it. The first 2948 * S/G element is used for the command (therefore the 2949 * direction bit needs to be set). The second one is used 2950 * for the reply. We'll leave it to the caller to make 2951 * sure we only have two buffers. 2952 */ 2953 /* 2954 * Even though the busdma man page says it doesn't make 2955 * sense to have both direction flags, it does in this case. 2956 * We have one s/g element being accessed in each direction. 2957 */ 2958 dir = BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD; 2959 2960 /* 2961 * Set the direction flag on the first buffer in the SMP 2962 * passthrough request. We'll clear it for the second one. 2963 */ 2964 sflags |= MPI2_SGE_FLAGS_DIRECTION | 2965 MPI2_SGE_FLAGS_END_OF_BUFFER; 2966 } else if (cm->cm_flags & MPS_CM_FLAGS_DATAOUT) { 2967 sflags |= MPI2_SGE_FLAGS_HOST_TO_IOC; 2968 dir = BUS_DMASYNC_PREWRITE; 2969 } else 2970 dir = BUS_DMASYNC_PREREAD; 2971 2972 for (i = 0; i < nsegs; i++) { 2973 if ((cm->cm_flags & MPS_CM_FLAGS_SMP_PASS) && (i != 0)) { 2974 sflags &= ~MPI2_SGE_FLAGS_DIRECTION; 2975 } 2976 error = mps_add_dmaseg(cm, segs[i].ds_addr, segs[i].ds_len, 2977 sflags, nsegs - i); 2978 if (error != 0) { 2979 /* Resource shortage, roll back! */ 2980 if (ratecheck(&sc->lastfail, &mps_chainfail_interval)) 2981 mps_dprint(sc, MPS_INFO, "Out of chain frames, " 2982 "consider increasing hw.mps.max_chains.\n"); 2983 cm->cm_flags |= MPS_CM_FLAGS_CHAIN_FAILED; 2984 /* 2985 * mpr_complete_command can only be called on commands 2986 * that are in the queue. Since this is an error path 2987 * which gets called before we enqueue, update the state 2988 * to meet this requirement before we complete it. 2989 */ 2990 cm->cm_state = MPS_CM_STATE_INQUEUE; 2991 mps_complete_command(sc, cm); 2992 return; 2993 } 2994 } 2995 2996 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir); 2997 mps_enqueue_request(sc, cm); 2998 2999 return; 3000 } 3001 3002 static void 3003 mps_data_cb2(void *arg, bus_dma_segment_t *segs, int nsegs, bus_size_t mapsize, 3004 int error) 3005 { 3006 mps_data_cb(arg, segs, nsegs, error); 3007 } 3008 3009 /* 3010 * This is the routine to enqueue commands ansynchronously. 3011 * Note that the only error path here is from bus_dmamap_load(), which can 3012 * return EINPROGRESS if it is waiting for resources. Other than this, it's 3013 * assumed that if you have a command in-hand, then you have enough credits 3014 * to use it. 3015 */ 3016 int 3017 mps_map_command(struct mps_softc *sc, struct mps_command *cm) 3018 { 3019 int error = 0; 3020 3021 if (cm->cm_flags & MPS_CM_FLAGS_USE_UIO) { 3022 error = bus_dmamap_load_uio(sc->buffer_dmat, cm->cm_dmamap, 3023 &cm->cm_uio, mps_data_cb2, cm, 0); 3024 } else if (cm->cm_flags & MPS_CM_FLAGS_USE_CCB) { 3025 error = bus_dmamap_load_ccb(sc->buffer_dmat, cm->cm_dmamap, 3026 cm->cm_data, mps_data_cb, cm, 0); 3027 } else if ((cm->cm_data != NULL) && (cm->cm_length != 0)) { 3028 error = bus_dmamap_load(sc->buffer_dmat, cm->cm_dmamap, 3029 cm->cm_data, cm->cm_length, mps_data_cb, cm, 0); 3030 } else { 3031 /* Add a zero-length element as needed */ 3032 if (cm->cm_sge != NULL) 3033 mps_add_dmaseg(cm, 0, 0, 0, 1); 3034 mps_enqueue_request(sc, cm); 3035 } 3036 3037 return (error); 3038 } 3039 3040 /* 3041 * This is the routine to enqueue commands synchronously. An error of 3042 * EINPROGRESS from mps_map_command() is ignored since the command will 3043 * be executed and enqueued automatically. Other errors come from msleep(). 3044 */ 3045 int 3046 mps_wait_command(struct mps_softc *sc, struct mps_command **cmp, int timeout, 3047 int sleep_flag) 3048 { 3049 int error, rc; 3050 struct timeval cur_time, start_time; 3051 struct mps_command *cm = *cmp; 3052 3053 if (sc->mps_flags & MPS_FLAGS_DIAGRESET) 3054 return EBUSY; 3055 3056 cm->cm_complete = NULL; 3057 cm->cm_flags |= MPS_CM_FLAGS_POLLED; 3058 error = mps_map_command(sc, cm); 3059 if ((error != 0) && (error != EINPROGRESS)) 3060 return (error); 3061 3062 /* 3063 * Check for context and wait for 50 mSec at a time until time has 3064 * expired or the command has finished. If msleep can't be used, need 3065 * to poll. 3066 */ 3067 if (curthread->td_no_sleeping != 0) 3068 sleep_flag = NO_SLEEP; 3069 getmicrouptime(&start_time); 3070 if (mtx_owned(&sc->mps_mtx) && sleep_flag == CAN_SLEEP) { 3071 cm->cm_flags |= MPS_CM_FLAGS_WAKEUP; 3072 error = msleep(cm, &sc->mps_mtx, 0, "mpswait", timeout*hz); 3073 if (error == EWOULDBLOCK) { 3074 /* 3075 * Record the actual elapsed time in the case of a 3076 * timeout for the message below. 3077 */ 3078 getmicrouptime(&cur_time); 3079 timevalsub(&cur_time, &start_time); 3080 } 3081 } else { 3082 while ((cm->cm_flags & MPS_CM_FLAGS_COMPLETE) == 0) { 3083 mps_intr_locked(sc); 3084 if (sleep_flag == CAN_SLEEP) 3085 pause("mpswait", hz/20); 3086 else 3087 DELAY(50000); 3088 3089 getmicrouptime(&cur_time); 3090 timevalsub(&cur_time, &start_time); 3091 if (cur_time.tv_sec > timeout) { 3092 error = EWOULDBLOCK; 3093 break; 3094 } 3095 } 3096 } 3097 3098 if (error == EWOULDBLOCK) { 3099 if (cm->cm_timeout_handler == NULL) { 3100 mps_dprint(sc, MPS_FAULT, "Calling Reinit from %s, timeout=%d," 3101 " elapsed=%jd\n", __func__, timeout, 3102 (intmax_t)cur_time.tv_sec); 3103 rc = mps_reinit(sc); 3104 mps_dprint(sc, MPS_FAULT, "Reinit %s\n", (rc == 0) ? "success" : 3105 "failed"); 3106 } else 3107 cm->cm_timeout_handler(sc, cm); 3108 if (sc->mps_flags & MPS_FLAGS_REALLOCATED) { 3109 /* 3110 * Tell the caller that we freed the command in a 3111 * reinit. 3112 */ 3113 *cmp = NULL; 3114 } 3115 error = ETIMEDOUT; 3116 } 3117 return (error); 3118 } 3119 3120 /* 3121 * The MPT driver had a verbose interface for config pages. In this driver, 3122 * reduce it to much simpler terms, similar to the Linux driver. 3123 */ 3124 int 3125 mps_read_config_page(struct mps_softc *sc, struct mps_config_params *params) 3126 { 3127 MPI2_CONFIG_REQUEST *req; 3128 struct mps_command *cm; 3129 int error; 3130 3131 if (sc->mps_flags & MPS_FLAGS_BUSY) { 3132 return (EBUSY); 3133 } 3134 3135 cm = mps_alloc_command(sc); 3136 if (cm == NULL) { 3137 return (EBUSY); 3138 } 3139 3140 req = (MPI2_CONFIG_REQUEST *)cm->cm_req; 3141 req->Function = MPI2_FUNCTION_CONFIG; 3142 req->Action = params->action; 3143 req->SGLFlags = 0; 3144 req->ChainOffset = 0; 3145 req->PageAddress = params->page_address; 3146 if (params->hdr.Struct.PageType == MPI2_CONFIG_PAGETYPE_EXTENDED) { 3147 MPI2_CONFIG_EXTENDED_PAGE_HEADER *hdr; 3148 3149 hdr = ¶ms->hdr.Ext; 3150 req->ExtPageType = hdr->ExtPageType; 3151 req->ExtPageLength = hdr->ExtPageLength; 3152 req->Header.PageType = MPI2_CONFIG_PAGETYPE_EXTENDED; 3153 req->Header.PageLength = 0; /* Must be set to zero */ 3154 req->Header.PageNumber = hdr->PageNumber; 3155 req->Header.PageVersion = hdr->PageVersion; 3156 } else { 3157 MPI2_CONFIG_PAGE_HEADER *hdr; 3158 3159 hdr = ¶ms->hdr.Struct; 3160 req->Header.PageType = hdr->PageType; 3161 req->Header.PageNumber = hdr->PageNumber; 3162 req->Header.PageLength = hdr->PageLength; 3163 req->Header.PageVersion = hdr->PageVersion; 3164 } 3165 3166 cm->cm_data = params->buffer; 3167 cm->cm_length = params->length; 3168 if (cm->cm_data != NULL) { 3169 cm->cm_sge = &req->PageBufferSGE; 3170 cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION); 3171 cm->cm_flags = MPS_CM_FLAGS_SGE_SIMPLE | MPS_CM_FLAGS_DATAIN; 3172 } else 3173 cm->cm_sge = NULL; 3174 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE; 3175 3176 cm->cm_complete_data = params; 3177 if (params->callback != NULL) { 3178 cm->cm_complete = mps_config_complete; 3179 return (mps_map_command(sc, cm)); 3180 } else { 3181 error = mps_wait_command(sc, &cm, 0, CAN_SLEEP); 3182 if (error) { 3183 mps_dprint(sc, MPS_FAULT, 3184 "Error %d reading config page\n", error); 3185 if (cm != NULL) 3186 mps_free_command(sc, cm); 3187 return (error); 3188 } 3189 mps_config_complete(sc, cm); 3190 } 3191 3192 return (0); 3193 } 3194 3195 int 3196 mps_write_config_page(struct mps_softc *sc, struct mps_config_params *params) 3197 { 3198 return (EINVAL); 3199 } 3200 3201 static void 3202 mps_config_complete(struct mps_softc *sc, struct mps_command *cm) 3203 { 3204 MPI2_CONFIG_REPLY *reply; 3205 struct mps_config_params *params; 3206 3207 MPS_FUNCTRACE(sc); 3208 params = cm->cm_complete_data; 3209 3210 if (cm->cm_data != NULL) { 3211 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, 3212 BUS_DMASYNC_POSTREAD); 3213 bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap); 3214 } 3215 3216 /* 3217 * XXX KDM need to do more error recovery? This results in the 3218 * device in question not getting probed. 3219 */ 3220 if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) { 3221 params->status = MPI2_IOCSTATUS_BUSY; 3222 goto done; 3223 } 3224 3225 reply = (MPI2_CONFIG_REPLY *)cm->cm_reply; 3226 if (reply == NULL) { 3227 params->status = MPI2_IOCSTATUS_BUSY; 3228 goto done; 3229 } 3230 params->status = reply->IOCStatus; 3231 if (params->hdr.Struct.PageType == MPI2_CONFIG_PAGETYPE_EXTENDED) { 3232 params->hdr.Ext.ExtPageType = reply->ExtPageType; 3233 params->hdr.Ext.ExtPageLength = reply->ExtPageLength; 3234 params->hdr.Ext.PageType = reply->Header.PageType; 3235 params->hdr.Ext.PageNumber = reply->Header.PageNumber; 3236 params->hdr.Ext.PageVersion = reply->Header.PageVersion; 3237 } else { 3238 params->hdr.Struct.PageType = reply->Header.PageType; 3239 params->hdr.Struct.PageNumber = reply->Header.PageNumber; 3240 params->hdr.Struct.PageLength = reply->Header.PageLength; 3241 params->hdr.Struct.PageVersion = reply->Header.PageVersion; 3242 } 3243 3244 done: 3245 mps_free_command(sc, cm); 3246 if (params->callback != NULL) 3247 params->callback(sc, params); 3248 3249 return; 3250 } 3251