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