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