2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Yahoo! Inc.
5 * Copyright (c) 2011-2015 LSI Corp.
6 * Copyright (c) 2013-2015 Avago Technologies
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Avago Technologies (LSI) MPT-Fusion Host Adapter FreeBSD
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 /* Communications core for Avago Technologies (LSI) MPT2 */
40 /* TODO Move headers to mpsvar */
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/selinfo.h>
47 #include <sys/mutex.h>
48 #include <sys/module.h>
52 #include <sys/malloc.h>
54 #include <sys/sysctl.h>
56 #include <sys/queue.h>
57 #include <sys/kthread.h>
58 #include <sys/taskqueue.h>
59 #include <sys/endian.h>
60 #include <sys/eventhandler.h>
64 #include <machine/bus.h>
65 #include <machine/resource.h>
69 #include <dev/pci/pcivar.h>
72 #include <cam/scsi/scsi_all.h>
74 #include <dev/mps/mpi/mpi2_type.h>
75 #include <dev/mps/mpi/mpi2.h>
76 #include <dev/mps/mpi/mpi2_ioc.h>
77 #include <dev/mps/mpi/mpi2_sas.h>
78 #include <dev/mps/mpi/mpi2_cnfg.h>
79 #include <dev/mps/mpi/mpi2_init.h>
80 #include <dev/mps/mpi/mpi2_tool.h>
81 #include <dev/mps/mps_ioctl.h>
82 #include <dev/mps/mpsvar.h>
83 #include <dev/mps/mps_table.h>
85 static int mps_diag_reset(struct mps_softc *sc, int sleep_flag);
86 static int mps_init_queues(struct mps_softc *sc);
87 static void mps_resize_queues(struct mps_softc *sc);
88 static int mps_message_unit_reset(struct mps_softc *sc, int sleep_flag);
89 static int mps_transition_operational(struct mps_softc *sc);
90 static int mps_iocfacts_allocate(struct mps_softc *sc, uint8_t attaching);
91 static void mps_iocfacts_free(struct mps_softc *sc);
92 static void mps_startup(void *arg);
93 static int mps_send_iocinit(struct mps_softc *sc);
94 static int mps_alloc_queues(struct mps_softc *sc);
95 static int mps_alloc_hw_queues(struct mps_softc *sc);
96 static int mps_alloc_replies(struct mps_softc *sc);
97 static int mps_alloc_requests(struct mps_softc *sc);
98 static int mps_attach_log(struct mps_softc *sc);
99 static __inline void mps_complete_command(struct mps_softc *sc,
100 struct mps_command *cm);
101 static void mps_dispatch_event(struct mps_softc *sc, uintptr_t data,
102 MPI2_EVENT_NOTIFICATION_REPLY *reply);
103 static void mps_config_complete(struct mps_softc *sc, struct mps_command *cm);
104 static void mps_periodic(void *);
105 static int mps_reregister_events(struct mps_softc *sc);
106 static void mps_enqueue_request(struct mps_softc *sc, struct mps_command *cm);
107 static int mps_get_iocfacts(struct mps_softc *sc, MPI2_IOC_FACTS_REPLY *facts);
108 static int mps_wait_db_ack(struct mps_softc *sc, int timeout, int sleep_flag);
109 static int mps_debug_sysctl(SYSCTL_HANDLER_ARGS);
110 static int mps_dump_reqs(SYSCTL_HANDLER_ARGS);
111 static void mps_parse_debug(struct mps_softc *sc, char *list);
113 SYSCTL_NODE(_hw, OID_AUTO, mps, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
114 "MPS Driver Parameters");
116 MALLOC_DEFINE(M_MPT2, "mps", "mpt2 driver memory");
117 MALLOC_DECLARE(M_MPSUSER);
120 * Do a "Diagnostic Reset" aka a hard reset. This should get the chip out of
121 * any state and back to its initialization state machine.
123 static char mpt2_reset_magic[] = { 0x00, 0x0f, 0x04, 0x0b, 0x02, 0x07, 0x0d };
125 /* Added this union to smoothly convert le64toh cm->cm_desc.Words.
126 * Compiler only support unint64_t to be passed as argument.
127 * Otherwise it will throw below error
128 * "aggregate value used where an integer was expected"
131 typedef union _reply_descriptor {
137 }reply_descriptor,address_descriptor;
139 /* Rate limit chain-fail messages to 1 per minute */
140 static struct timeval mps_chainfail_interval = { 60, 0 };
143 * sleep_flag can be either CAN_SLEEP or NO_SLEEP.
144 * If this function is called from process context, it can sleep
145 * and there is no harm to sleep, in case if this fuction is called
146 * from Interrupt handler, we can not sleep and need NO_SLEEP flag set.
147 * based on sleep flags driver will call either msleep, pause or DELAY.
148 * msleep and pause are of same variant, but pause is used when mps_mtx
149 * is not hold by driver.
153 mps_diag_reset(struct mps_softc *sc,int sleep_flag)
156 int i, error, tries = 0;
157 uint8_t first_wait_done = FALSE;
159 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
161 /* Clear any pending interrupts */
162 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
165 * Force NO_SLEEP for threads prohibited to sleep
166 * e.a Thread from interrupt handler are prohibited to sleep.
168 if (curthread->td_no_sleeping != 0)
169 sleep_flag = NO_SLEEP;
171 mps_dprint(sc, MPS_INIT, "sequence start, sleep_flag= %d\n", sleep_flag);
173 /* Push the magic sequence */
175 while (tries++ < 20) {
176 for (i = 0; i < sizeof(mpt2_reset_magic); i++)
177 mps_regwrite(sc, MPI2_WRITE_SEQUENCE_OFFSET,
178 mpt2_reset_magic[i]);
180 if (mtx_owned(&sc->mps_mtx) && sleep_flag == CAN_SLEEP)
181 msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0,
183 else if (sleep_flag == CAN_SLEEP)
184 pause("mpsdiag", hz/10);
188 reg = mps_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
189 if (reg & MPI2_DIAG_DIAG_WRITE_ENABLE) {
195 mps_dprint(sc, MPS_INIT, "sequence failed, error=%d, exit\n",
200 /* Send the actual reset. XXX need to refresh the reg? */
201 reg |= MPI2_DIAG_RESET_ADAPTER;
202 mps_dprint(sc, MPS_INIT, "sequence success, sending reset, reg= 0x%x\n",
204 mps_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET, reg);
206 /* Wait up to 300 seconds in 50ms intervals */
208 for (i = 0; i < 6000; i++) {
210 * Wait 50 msec. If this is the first time through, wait 256
211 * msec to satisfy Diag Reset timing requirements.
213 if (first_wait_done) {
214 if (mtx_owned(&sc->mps_mtx) && sleep_flag == CAN_SLEEP)
215 msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0,
217 else if (sleep_flag == CAN_SLEEP)
218 pause("mpsdiag", hz/20);
223 first_wait_done = TRUE;
226 * Check for the RESET_ADAPTER bit to be cleared first, then
227 * wait for the RESET state to be cleared, which takes a little
230 reg = mps_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
231 if (reg & MPI2_DIAG_RESET_ADAPTER) {
234 reg = mps_regread(sc, MPI2_DOORBELL_OFFSET);
235 if ((reg & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_RESET) {
241 mps_dprint(sc, MPS_INIT, "reset failed, error= %d, exit\n",
246 mps_regwrite(sc, MPI2_WRITE_SEQUENCE_OFFSET, 0x0);
247 mps_dprint(sc, MPS_INIT, "diag reset success, exit\n");
253 mps_message_unit_reset(struct mps_softc *sc, int sleep_flag)
259 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
262 mps_regwrite(sc, MPI2_DOORBELL_OFFSET,
263 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET <<
264 MPI2_DOORBELL_FUNCTION_SHIFT);
266 if (mps_wait_db_ack(sc, 5, sleep_flag) != 0) {
267 mps_dprint(sc, MPS_INIT|MPS_FAULT,
268 "Doorbell handshake failed\n");
272 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__);
277 mps_transition_ready(struct mps_softc *sc)
280 int error, tries = 0;
284 /* If we are in attach call, do not sleep */
285 sleep_flags = (sc->mps_flags & MPS_FLAGS_ATTACH_DONE)
286 ? CAN_SLEEP:NO_SLEEP;
289 mps_dprint(sc, MPS_INIT, "%s entered, sleep_flags= %d\n",
290 __func__, sleep_flags);
292 while (tries++ < 1200) {
293 reg = mps_regread(sc, MPI2_DOORBELL_OFFSET);
294 mps_dprint(sc, MPS_INIT, " Doorbell= 0x%x\n", reg);
297 * Ensure the IOC is ready to talk. If it's not, try
300 if (reg & MPI2_DOORBELL_USED) {
301 mps_dprint(sc, MPS_INIT, " Not ready, sending diag "
303 mps_diag_reset(sc, sleep_flags);
308 /* Is the adapter owned by another peer? */
309 if ((reg & MPI2_DOORBELL_WHO_INIT_MASK) ==
310 (MPI2_WHOINIT_PCI_PEER << MPI2_DOORBELL_WHO_INIT_SHIFT)) {
311 mps_dprint(sc, MPS_INIT|MPS_FAULT, "IOC is under the "
312 "control of another peer host, aborting "
313 "initialization.\n");
318 state = reg & MPI2_IOC_STATE_MASK;
319 if (state == MPI2_IOC_STATE_READY) {
323 } else if (state == MPI2_IOC_STATE_FAULT) {
324 mps_dprint(sc, MPS_INIT|MPS_FAULT, "IOC in fault "
325 "state 0x%x, resetting\n",
326 state & MPI2_DOORBELL_FAULT_CODE_MASK);
327 mps_diag_reset(sc, sleep_flags);
328 } else if (state == MPI2_IOC_STATE_OPERATIONAL) {
329 /* Need to take ownership */
330 mps_message_unit_reset(sc, sleep_flags);
331 } else if (state == MPI2_IOC_STATE_RESET) {
332 /* Wait a bit, IOC might be in transition */
333 mps_dprint(sc, MPS_INIT|MPS_FAULT,
334 "IOC in unexpected reset state\n");
336 mps_dprint(sc, MPS_INIT|MPS_FAULT,
337 "IOC in unknown state 0x%x\n", state);
342 /* Wait 50ms for things to settle down. */
347 mps_dprint(sc, MPS_INIT|MPS_FAULT,
348 "Cannot transition IOC to ready\n");
349 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__);
355 mps_transition_operational(struct mps_softc *sc)
363 reg = mps_regread(sc, MPI2_DOORBELL_OFFSET);
364 mps_dprint(sc, MPS_INIT, "%s entered, Doorbell= 0x%x\n", __func__, reg);
366 state = reg & MPI2_IOC_STATE_MASK;
367 if (state != MPI2_IOC_STATE_READY) {
368 mps_dprint(sc, MPS_INIT, "IOC not ready\n");
369 if ((error = mps_transition_ready(sc)) != 0) {
370 mps_dprint(sc, MPS_INIT|MPS_FAULT,
371 "failed to transition ready, exit\n");
376 error = mps_send_iocinit(sc);
377 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__);
383 mps_resize_queues(struct mps_softc *sc)
385 u_int reqcr, prireqcr, maxio, sges_per_frame;
388 * Size the queues. Since the reply queues always need one free
389 * entry, we'll deduct one reply message here. The LSI documents
390 * suggest instead to add a count to the request queue, but I think
391 * that it's better to deduct from reply queue.
393 prireqcr = MAX(1, sc->max_prireqframes);
394 prireqcr = MIN(prireqcr, sc->facts->HighPriorityCredit);
396 reqcr = MAX(2, sc->max_reqframes);
397 reqcr = MIN(reqcr, sc->facts->RequestCredit);
399 sc->num_reqs = prireqcr + reqcr;
400 sc->num_prireqs = prireqcr;
401 sc->num_replies = MIN(sc->max_replyframes + sc->max_evtframes,
402 sc->facts->MaxReplyDescriptorPostQueueDepth) - 1;
404 /* Store the request frame size in bytes rather than as 32bit words */
405 sc->reqframesz = sc->facts->IOCRequestFrameSize * 4;
408 * Max IO Size is Page Size * the following:
409 * ((SGEs per frame - 1 for chain element) * Max Chain Depth)
410 * + 1 for no chain needed in last frame
412 * If user suggests a Max IO size to use, use the smaller of the
413 * user's value and the calculated value as long as the user's
414 * value is larger than 0. The user's value is in pages.
416 sges_per_frame = sc->reqframesz / sizeof(MPI2_SGE_SIMPLE64) - 1;
417 maxio = (sges_per_frame * sc->facts->MaxChainDepth + 1) * PAGE_SIZE;
420 * If I/O size limitation requested, then use it and pass up to CAM.
421 * If not, use MAXPHYS as an optimization hint, but report HW limit.
423 if (sc->max_io_pages > 0) {
424 maxio = min(maxio, sc->max_io_pages * PAGE_SIZE);
428 maxio = min(maxio, MAXPHYS);
431 sc->num_chains = (maxio / PAGE_SIZE + sges_per_frame - 2) /
432 sges_per_frame * reqcr;
433 if (sc->max_chains > 0 && sc->max_chains < sc->num_chains)
434 sc->num_chains = sc->max_chains;
437 * Figure out the number of MSIx-based queues. If the firmware or
438 * user has done something crazy and not allowed enough credit for
439 * the queues to be useful then don't enable multi-queue.
441 if (sc->facts->MaxMSIxVectors < 2)
444 if (sc->msi_msgs > 1) {
445 sc->msi_msgs = MIN(sc->msi_msgs, mp_ncpus);
446 sc->msi_msgs = MIN(sc->msi_msgs, sc->facts->MaxMSIxVectors);
447 if (sc->num_reqs / sc->msi_msgs < 2)
451 mps_dprint(sc, MPS_INIT, "Sized queues to q=%d reqs=%d replies=%d\n",
452 sc->msi_msgs, sc->num_reqs, sc->num_replies);
456 * This is called during attach and when re-initializing due to a Diag Reset.
457 * IOC Facts is used to allocate many of the structures needed by the driver.
458 * If called from attach, de-allocation is not required because the driver has
459 * not allocated any structures yet, but if called from a Diag Reset, previously
460 * allocated structures based on IOC Facts will need to be freed and re-
461 * allocated bases on the latest IOC Facts.
464 mps_iocfacts_allocate(struct mps_softc *sc, uint8_t attaching)
467 Mpi2IOCFactsReply_t saved_facts;
468 uint8_t saved_mode, reallocating;
470 mps_dprint(sc, MPS_INIT|MPS_TRACE, "%s entered\n", __func__);
472 /* Save old IOC Facts and then only reallocate if Facts have changed */
474 bcopy(sc->facts, &saved_facts, sizeof(MPI2_IOC_FACTS_REPLY));
478 * Get IOC Facts. In all cases throughout this function, panic if doing
479 * a re-initialization and only return the error if attaching so the OS
482 if ((error = mps_get_iocfacts(sc, sc->facts)) != 0) {
484 mps_dprint(sc, MPS_INIT|MPS_FAULT, "Failed to get "
485 "IOC Facts with error %d, exit\n", error);
488 panic("%s failed to get IOC Facts with error %d\n",
493 MPS_DPRINT_PAGE(sc, MPS_XINFO, iocfacts, sc->facts);
495 snprintf(sc->fw_version, sizeof(sc->fw_version),
496 "%02d.%02d.%02d.%02d",
497 sc->facts->FWVersion.Struct.Major,
498 sc->facts->FWVersion.Struct.Minor,
499 sc->facts->FWVersion.Struct.Unit,
500 sc->facts->FWVersion.Struct.Dev);
502 snprintf(sc->msg_version, sizeof(sc->msg_version), "%d.%d",
503 (sc->facts->MsgVersion & MPI2_IOCFACTS_MSGVERSION_MAJOR_MASK) >>
504 MPI2_IOCFACTS_MSGVERSION_MAJOR_SHIFT,
505 (sc->facts->MsgVersion & MPI2_IOCFACTS_MSGVERSION_MINOR_MASK) >>
506 MPI2_IOCFACTS_MSGVERSION_MINOR_SHIFT);
508 mps_dprint(sc, MPS_INFO, "Firmware: %s, Driver: %s\n", sc->fw_version,
510 mps_dprint(sc, MPS_INFO, "IOCCapabilities: %b\n",
511 sc->facts->IOCCapabilities,
512 "\20" "\3ScsiTaskFull" "\4DiagTrace" "\5SnapBuf" "\6ExtBuf"
513 "\7EEDP" "\10BiDirTarg" "\11Multicast" "\14TransRetry" "\15IR"
514 "\16EventReplay" "\17RaidAccel" "\20MSIXIndex" "\21HostDisc");
517 * If the chip doesn't support event replay then a hard reset will be
518 * required to trigger a full discovery. Do the reset here then
519 * retransition to Ready. A hard reset might have already been done,
520 * but it doesn't hurt to do it again. Only do this if attaching, not
523 if (attaching && ((sc->facts->IOCCapabilities &
524 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY) == 0)) {
525 mps_dprint(sc, MPS_INIT, "No event replay, reseting\n");
526 mps_diag_reset(sc, NO_SLEEP);
527 if ((error = mps_transition_ready(sc)) != 0) {
528 mps_dprint(sc, MPS_INIT|MPS_FAULT, "Failed to "
529 "transition to ready with error %d, exit\n",
536 * Set flag if IR Firmware is loaded. If the RAID Capability has
537 * changed from the previous IOC Facts, log a warning, but only if
538 * checking this after a Diag Reset and not during attach.
540 saved_mode = sc->ir_firmware;
541 if (sc->facts->IOCCapabilities &
542 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID)
545 if (sc->ir_firmware != saved_mode) {
546 mps_dprint(sc, MPS_INIT|MPS_FAULT, "new IR/IT mode "
547 "in IOC Facts does not match previous mode\n");
551 /* Only deallocate and reallocate if relevant IOC Facts have changed */
552 reallocating = FALSE;
553 sc->mps_flags &= ~MPS_FLAGS_REALLOCATED;
556 ((saved_facts.MsgVersion != sc->facts->MsgVersion) ||
557 (saved_facts.HeaderVersion != sc->facts->HeaderVersion) ||
558 (saved_facts.MaxChainDepth != sc->facts->MaxChainDepth) ||
559 (saved_facts.RequestCredit != sc->facts->RequestCredit) ||
560 (saved_facts.ProductID != sc->facts->ProductID) ||
561 (saved_facts.IOCCapabilities != sc->facts->IOCCapabilities) ||
562 (saved_facts.IOCRequestFrameSize !=
563 sc->facts->IOCRequestFrameSize) ||
564 (saved_facts.MaxTargets != sc->facts->MaxTargets) ||
565 (saved_facts.MaxSasExpanders != sc->facts->MaxSasExpanders) ||
566 (saved_facts.MaxEnclosures != sc->facts->MaxEnclosures) ||
567 (saved_facts.HighPriorityCredit != sc->facts->HighPriorityCredit) ||
568 (saved_facts.MaxReplyDescriptorPostQueueDepth !=
569 sc->facts->MaxReplyDescriptorPostQueueDepth) ||
570 (saved_facts.ReplyFrameSize != sc->facts->ReplyFrameSize) ||
571 (saved_facts.MaxVolumes != sc->facts->MaxVolumes) ||
572 (saved_facts.MaxPersistentEntries !=
573 sc->facts->MaxPersistentEntries))) {
576 /* Record that we reallocated everything */
577 sc->mps_flags |= MPS_FLAGS_REALLOCATED;
581 * Some things should be done if attaching or re-allocating after a Diag
582 * Reset, but are not needed after a Diag Reset if the FW has not
585 if (attaching || reallocating) {
587 * Check if controller supports FW diag buffers and set flag to
590 if (sc->facts->IOCCapabilities &
591 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
592 sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_TRACE].
594 if (sc->facts->IOCCapabilities &
595 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
596 sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_SNAPSHOT].
598 if (sc->facts->IOCCapabilities &
599 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
600 sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_EXTENDED].
604 * Set flag if EEDP is supported and if TLR is supported.
606 if (sc->facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP)
607 sc->eedp_enabled = TRUE;
608 if (sc->facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR)
609 sc->control_TLR = TRUE;
611 mps_resize_queues(sc);
614 * Initialize all Tail Queues
616 TAILQ_INIT(&sc->req_list);
617 TAILQ_INIT(&sc->high_priority_req_list);
618 TAILQ_INIT(&sc->chain_list);
619 TAILQ_INIT(&sc->tm_list);
623 * If doing a Diag Reset and the FW is significantly different
624 * (reallocating will be set above in IOC Facts comparison), then all
625 * buffers based on the IOC Facts will need to be freed before they are
629 mps_iocfacts_free(sc);
630 mpssas_realloc_targets(sc, saved_facts.MaxTargets +
631 saved_facts.MaxVolumes);
635 * Any deallocation has been completed. Now start reallocating
636 * if needed. Will only need to reallocate if attaching or if the new
637 * IOC Facts are different from the previous IOC Facts after a Diag
638 * Reset. Targets have already been allocated above if needed.
641 while (attaching || reallocating) {
642 if ((error = mps_alloc_hw_queues(sc)) != 0)
644 if ((error = mps_alloc_replies(sc)) != 0)
646 if ((error = mps_alloc_requests(sc)) != 0)
648 if ((error = mps_alloc_queues(sc)) != 0)
654 mps_dprint(sc, MPS_INIT|MPS_FAULT,
655 "Failed to alloc queues with error %d\n", error);
660 /* Always initialize the queues */
661 bzero(sc->free_queue, sc->fqdepth * 4);
665 * Always get the chip out of the reset state, but only panic if not
666 * attaching. If attaching and there is an error, that is handled by
669 error = mps_transition_operational(sc);
671 mps_dprint(sc, MPS_INIT|MPS_FAULT, "Failed to "
672 "transition to operational with error %d\n", error);
678 * Finish the queue initialization.
679 * These are set here instead of in mps_init_queues() because the
680 * IOC resets these values during the state transition in
681 * mps_transition_operational(). The free index is set to 1
682 * because the corresponding index in the IOC is set to 0, and the
683 * IOC treats the queues as full if both are set to the same value.
684 * Hence the reason that the queue can't hold all of the possible
687 sc->replypostindex = 0;
688 mps_regwrite(sc, MPI2_REPLY_FREE_HOST_INDEX_OFFSET, sc->replyfreeindex);
689 mps_regwrite(sc, MPI2_REPLY_POST_HOST_INDEX_OFFSET, 0);
692 * Attach the subsystems so they can prepare their event masks.
693 * XXX Should be dynamic so that IM/IR and user modules can attach
697 mps_dprint(sc, MPS_INIT, "Attaching subsystems\n");
698 if ((error = mps_attach_log(sc)) != 0)
700 if ((error = mps_attach_sas(sc)) != 0)
702 if ((error = mps_attach_user(sc)) != 0)
707 mps_dprint(sc, MPS_INIT|MPS_FAULT, "Failed to attach all "
708 "subsystems: error %d\n", error);
714 * XXX If the number of MSI-X vectors changes during re-init, this
715 * won't see it and adjust.
717 if (attaching && (error = mps_pci_setup_interrupts(sc)) != 0) {
718 mps_dprint(sc, MPS_INIT|MPS_FAULT, "Failed to setup "
725 * Set flag if this is a WD controller. This shouldn't ever change, but
726 * reset it after a Diag Reset, just in case.
728 sc->WD_available = FALSE;
729 if (pci_get_device(sc->mps_dev) == MPI2_MFGPAGE_DEVID_SSS6200)
730 sc->WD_available = TRUE;
736 * This is called if memory is being free (during detach for example) and when
737 * buffers need to be reallocated due to a Diag Reset.
740 mps_iocfacts_free(struct mps_softc *sc)
742 struct mps_command *cm;
745 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
747 if (sc->free_busaddr != 0)
748 bus_dmamap_unload(sc->queues_dmat, sc->queues_map);
749 if (sc->free_queue != NULL)
750 bus_dmamem_free(sc->queues_dmat, sc->free_queue,
752 if (sc->queues_dmat != NULL)
753 bus_dma_tag_destroy(sc->queues_dmat);
755 if (sc->chain_frames != NULL) {
756 bus_dmamap_unload(sc->chain_dmat, sc->chain_map);
757 bus_dmamem_free(sc->chain_dmat, sc->chain_frames,
760 if (sc->chain_dmat != NULL)
761 bus_dma_tag_destroy(sc->chain_dmat);
763 if (sc->sense_busaddr != 0)
764 bus_dmamap_unload(sc->sense_dmat, sc->sense_map);
765 if (sc->sense_frames != NULL)
766 bus_dmamem_free(sc->sense_dmat, sc->sense_frames,
768 if (sc->sense_dmat != NULL)
769 bus_dma_tag_destroy(sc->sense_dmat);
771 if (sc->reply_busaddr != 0)
772 bus_dmamap_unload(sc->reply_dmat, sc->reply_map);
773 if (sc->reply_frames != NULL)
774 bus_dmamem_free(sc->reply_dmat, sc->reply_frames,
776 if (sc->reply_dmat != NULL)
777 bus_dma_tag_destroy(sc->reply_dmat);
779 if (sc->req_busaddr != 0)
780 bus_dmamap_unload(sc->req_dmat, sc->req_map);
781 if (sc->req_frames != NULL)
782 bus_dmamem_free(sc->req_dmat, sc->req_frames, sc->req_map);
783 if (sc->req_dmat != NULL)
784 bus_dma_tag_destroy(sc->req_dmat);
786 if (sc->chains != NULL)
787 free(sc->chains, M_MPT2);
788 if (sc->commands != NULL) {
789 for (i = 1; i < sc->num_reqs; i++) {
790 cm = &sc->commands[i];
791 bus_dmamap_destroy(sc->buffer_dmat, cm->cm_dmamap);
793 free(sc->commands, M_MPT2);
795 if (sc->buffer_dmat != NULL)
796 bus_dma_tag_destroy(sc->buffer_dmat);
798 mps_pci_free_interrupts(sc);
799 free(sc->queues, M_MPT2);
804 * The terms diag reset and hard reset are used interchangeably in the MPI
805 * docs to mean resetting the controller chip. In this code diag reset
806 * cleans everything up, and the hard reset function just sends the reset
807 * sequence to the chip. This should probably be refactored so that every
808 * subsystem gets a reset notification of some sort, and can clean up
812 mps_reinit(struct mps_softc *sc)
815 struct mpssas_softc *sassc;
821 mtx_assert(&sc->mps_mtx, MA_OWNED);
823 mps_dprint(sc, MPS_INIT|MPS_INFO, "Reinitializing controller\n");
824 if (sc->mps_flags & MPS_FLAGS_DIAGRESET) {
825 mps_dprint(sc, MPS_INIT, "Reset already in progress\n");
829 /* make sure the completion callbacks can recognize they're getting
830 * a NULL cm_reply due to a reset.
832 sc->mps_flags |= MPS_FLAGS_DIAGRESET;
835 * Mask interrupts here.
837 mps_dprint(sc, MPS_INIT, "masking interrupts and resetting\n");
840 error = mps_diag_reset(sc, CAN_SLEEP);
842 /* XXXSL No need to panic here */
843 panic("%s hard reset failed with error %d\n",
847 /* Restore the PCI state, including the MSI-X registers */
850 /* Give the I/O subsystem special priority to get itself prepared */
851 mpssas_handle_reinit(sc);
854 * Get IOC Facts and allocate all structures based on this information.
855 * The attach function will also call mps_iocfacts_allocate at startup.
856 * If relevant values have changed in IOC Facts, this function will free
857 * all of the memory based on IOC Facts and reallocate that memory.
859 if ((error = mps_iocfacts_allocate(sc, FALSE)) != 0) {
860 panic("%s IOC Facts based allocation failed with error %d\n",
865 * Mapping structures will be re-allocated after getting IOC Page8, so
866 * free these structures here.
868 mps_mapping_exit(sc);
871 * The static page function currently read is IOC Page8. Others can be
872 * added in future. It's possible that the values in IOC Page8 have
873 * changed after a Diag Reset due to user modification, so always read
874 * these. Interrupts are masked, so unmask them before getting config
878 sc->mps_flags &= ~MPS_FLAGS_DIAGRESET;
879 mps_base_static_config_pages(sc);
882 * Some mapping info is based in IOC Page8 data, so re-initialize the
885 mps_mapping_initialize(sc);
888 * Restart will reload the event masks clobbered by the reset, and
889 * then enable the port.
891 mps_reregister_events(sc);
893 /* the end of discovery will release the simq, so we're done. */
894 mps_dprint(sc, MPS_INIT|MPS_XINFO, "Finished sc %p post %u free %u\n",
895 sc, sc->replypostindex, sc->replyfreeindex);
897 mpssas_release_simq_reinit(sassc);
898 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__);
903 /* Wait for the chip to ACK a word that we've put into its FIFO
904 * Wait for <timeout> seconds. In single loop wait for busy loop
905 * for 500 microseconds.
906 * Total is [ 0.5 * (2000 * <timeout>) ] in miliseconds.
909 mps_wait_db_ack(struct mps_softc *sc, int timeout, int sleep_flag)
917 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
919 int_status = mps_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET);
920 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
921 mps_dprint(sc, MPS_TRACE,
922 "%s: successful count(%d), timeout(%d)\n",
923 __func__, count, timeout);
925 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
926 doorbell = mps_regread(sc, MPI2_DOORBELL_OFFSET);
927 if ((doorbell & MPI2_IOC_STATE_MASK) ==
928 MPI2_IOC_STATE_FAULT) {
929 mps_dprint(sc, MPS_FAULT,
930 "fault_state(0x%04x)!\n", doorbell);
933 } else if (int_status == 0xFFFFFFFF)
936 /* If it can sleep, sleep for 1 milisecond, else busy loop for
938 if (mtx_owned(&sc->mps_mtx) && sleep_flag == CAN_SLEEP)
939 msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0,
941 else if (sleep_flag == CAN_SLEEP)
942 pause("mpsdba", hz/1000);
949 mps_dprint(sc, MPS_FAULT, "%s: failed due to timeout count(%d), "
950 "int_status(%x)!\n", __func__, count, int_status);
955 /* Wait for the chip to signal that the next word in its FIFO can be fetched */
957 mps_wait_db_int(struct mps_softc *sc)
961 for (retry = 0; retry < MPS_DB_MAX_WAIT; retry++) {
962 if ((mps_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET) &
963 MPI2_HIS_IOC2SYS_DB_STATUS) != 0)
970 /* Step through the synchronous command state machine, i.e. "Doorbell mode" */
972 mps_request_sync(struct mps_softc *sc, void *req, MPI2_DEFAULT_REPLY *reply,
973 int req_sz, int reply_sz, int timeout)
977 int i, count, ioc_sz, residual;
978 int sleep_flags = CAN_SLEEP;
980 if (curthread->td_no_sleeping != 0)
981 sleep_flags = NO_SLEEP;
984 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
987 if (mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED)
991 * Announce that a message is coming through the doorbell. Messages
992 * are pushed at 32bit words, so round up if needed.
994 count = (req_sz + 3) / 4;
995 mps_regwrite(sc, MPI2_DOORBELL_OFFSET,
996 (MPI2_FUNCTION_HANDSHAKE << MPI2_DOORBELL_FUNCTION_SHIFT) |
997 (count << MPI2_DOORBELL_ADD_DWORDS_SHIFT));
1000 if (mps_wait_db_int(sc) ||
1001 (mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED) == 0) {
1002 mps_dprint(sc, MPS_FAULT, "Doorbell failed to activate\n");
1005 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1006 if (mps_wait_db_ack(sc, 5, sleep_flags) != 0) {
1007 mps_dprint(sc, MPS_FAULT, "Doorbell handshake failed\n");
1012 /* Clock out the message data synchronously in 32-bit dwords*/
1013 data32 = (uint32_t *)req;
1014 for (i = 0; i < count; i++) {
1015 mps_regwrite(sc, MPI2_DOORBELL_OFFSET, htole32(data32[i]));
1016 if (mps_wait_db_ack(sc, 5, sleep_flags) != 0) {
1017 mps_dprint(sc, MPS_FAULT,
1018 "Timeout while writing doorbell\n");
1024 /* Clock in the reply in 16-bit words. The total length of the
1025 * message is always in the 4th byte, so clock out the first 2 words
1026 * manually, then loop the rest.
1028 data16 = (uint16_t *)reply;
1029 if (mps_wait_db_int(sc) != 0) {
1030 mps_dprint(sc, MPS_FAULT, "Timeout reading doorbell 0\n");
1034 mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_DATA_MASK;
1035 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1036 if (mps_wait_db_int(sc) != 0) {
1037 mps_dprint(sc, MPS_FAULT, "Timeout reading doorbell 1\n");
1041 mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_DATA_MASK;
1042 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1044 /* Number of 32bit words in the message */
1045 ioc_sz = reply->MsgLength;
1048 * Figure out how many 16bit words to clock in without overrunning.
1049 * The precision loss with dividing reply_sz can safely be
1050 * ignored because the messages can only be multiples of 32bits.
1053 count = MIN((reply_sz / 4), ioc_sz) * 2;
1054 if (count < ioc_sz * 2) {
1055 residual = ioc_sz * 2 - count;
1056 mps_dprint(sc, MPS_ERROR, "Driver error, throwing away %d "
1057 "residual message words\n", residual);
1060 for (i = 2; i < count; i++) {
1061 if (mps_wait_db_int(sc) != 0) {
1062 mps_dprint(sc, MPS_FAULT,
1063 "Timeout reading doorbell %d\n", i);
1066 data16[i] = mps_regread(sc, MPI2_DOORBELL_OFFSET) &
1067 MPI2_DOORBELL_DATA_MASK;
1068 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1072 * Pull out residual words that won't fit into the provided buffer.
1073 * This keeps the chip from hanging due to a driver programming
1076 while (residual--) {
1077 if (mps_wait_db_int(sc) != 0) {
1078 mps_dprint(sc, MPS_FAULT,
1079 "Timeout reading doorbell\n");
1082 (void)mps_regread(sc, MPI2_DOORBELL_OFFSET);
1083 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1087 if (mps_wait_db_int(sc) != 0) {
1088 mps_dprint(sc, MPS_FAULT, "Timeout waiting to exit doorbell\n");
1091 if (mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED)
1092 mps_dprint(sc, MPS_FAULT, "Warning, doorbell still active\n");
1093 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1099 mps_enqueue_request(struct mps_softc *sc, struct mps_command *cm)
1101 reply_descriptor rd;
1103 mps_dprint(sc, MPS_TRACE, "SMID %u cm %p ccb %p\n",
1104 cm->cm_desc.Default.SMID, cm, cm->cm_ccb);
1106 if (sc->mps_flags & MPS_FLAGS_ATTACH_DONE && !(sc->mps_flags & MPS_FLAGS_SHUTDOWN))
1107 mtx_assert(&sc->mps_mtx, MA_OWNED);
1109 if (++sc->io_cmds_active > sc->io_cmds_highwater)
1110 sc->io_cmds_highwater++;
1111 rd.u.low = cm->cm_desc.Words.Low;
1112 rd.u.high = cm->cm_desc.Words.High;
1113 rd.word = htole64(rd.word);
1115 KASSERT(cm->cm_state == MPS_CM_STATE_BUSY, ("command not busy\n"));
1116 cm->cm_state = MPS_CM_STATE_INQUEUE;
1118 /* TODO-We may need to make below regwrite atomic */
1119 mps_regwrite(sc, MPI2_REQUEST_DESCRIPTOR_POST_LOW_OFFSET,
1121 mps_regwrite(sc, MPI2_REQUEST_DESCRIPTOR_POST_HIGH_OFFSET,
1126 * Just the FACTS, ma'am.
1129 mps_get_iocfacts(struct mps_softc *sc, MPI2_IOC_FACTS_REPLY *facts)
1131 MPI2_DEFAULT_REPLY *reply;
1132 MPI2_IOC_FACTS_REQUEST request;
1133 int error, req_sz, reply_sz;
1136 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
1138 req_sz = sizeof(MPI2_IOC_FACTS_REQUEST);
1139 reply_sz = sizeof(MPI2_IOC_FACTS_REPLY);
1140 reply = (MPI2_DEFAULT_REPLY *)facts;
1142 bzero(&request, req_sz);
1143 request.Function = MPI2_FUNCTION_IOC_FACTS;
1144 error = mps_request_sync(sc, &request, reply, req_sz, reply_sz, 5);
1145 mps_dprint(sc, MPS_INIT, "%s exit error= %d\n", __func__, error);
1151 mps_send_iocinit(struct mps_softc *sc)
1153 MPI2_IOC_INIT_REQUEST init;
1154 MPI2_DEFAULT_REPLY reply;
1155 int req_sz, reply_sz, error;
1157 uint64_t time_in_msec;
1160 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
1162 /* Do a quick sanity check on proper initialization */
1163 if ((sc->pqdepth == 0) || (sc->fqdepth == 0) || (sc->reqframesz == 0)
1164 || (sc->replyframesz == 0)) {
1165 mps_dprint(sc, MPS_INIT|MPS_ERROR,
1166 "Driver not fully initialized for IOCInit\n");
1170 req_sz = sizeof(MPI2_IOC_INIT_REQUEST);
1171 reply_sz = sizeof(MPI2_IOC_INIT_REPLY);
1172 bzero(&init, req_sz);
1173 bzero(&reply, reply_sz);
1176 * Fill in the init block. Note that most addresses are
1177 * deliberately in the lower 32bits of memory. This is a micro-
1178 * optimzation for PCI/PCIX, though it's not clear if it helps PCIe.
1180 init.Function = MPI2_FUNCTION_IOC_INIT;
1181 init.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
1182 init.MsgVersion = htole16(MPI2_VERSION);
1183 init.HeaderVersion = htole16(MPI2_HEADER_VERSION);
1184 init.SystemRequestFrameSize = htole16((uint16_t)(sc->reqframesz / 4));
1185 init.ReplyDescriptorPostQueueDepth = htole16(sc->pqdepth);
1186 init.ReplyFreeQueueDepth = htole16(sc->fqdepth);
1187 init.SenseBufferAddressHigh = 0;
1188 init.SystemReplyAddressHigh = 0;
1189 init.SystemRequestFrameBaseAddress.High = 0;
1190 init.SystemRequestFrameBaseAddress.Low = htole32((uint32_t)sc->req_busaddr);
1191 init.ReplyDescriptorPostQueueAddress.High = 0;
1192 init.ReplyDescriptorPostQueueAddress.Low = htole32((uint32_t)sc->post_busaddr);
1193 init.ReplyFreeQueueAddress.High = 0;
1194 init.ReplyFreeQueueAddress.Low = htole32((uint32_t)sc->free_busaddr);
1196 time_in_msec = (now.tv_sec * 1000 + now.tv_usec/1000);
1197 init.TimeStamp.High = htole32((time_in_msec >> 32) & 0xFFFFFFFF);
1198 init.TimeStamp.Low = htole32(time_in_msec & 0xFFFFFFFF);
1200 error = mps_request_sync(sc, &init, &reply, req_sz, reply_sz, 5);
1201 if ((reply.IOCStatus & MPI2_IOCSTATUS_MASK) != MPI2_IOCSTATUS_SUCCESS)
1204 mps_dprint(sc, MPS_INIT, "IOCInit status= 0x%x\n", reply.IOCStatus);
1205 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__);
1210 mps_memaddr_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1215 *addr = segs[0].ds_addr;
1219 mps_memaddr_wait_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1221 struct mps_busdma_context *ctx;
1222 int need_unload, need_free;
1224 ctx = (struct mps_busdma_context *)arg;
1228 mps_lock(ctx->softc);
1231 if ((error == 0) && (ctx->abandoned == 0)) {
1232 *ctx->addr = segs[0].ds_addr;
1236 if (ctx->abandoned != 0)
1242 mps_unlock(ctx->softc);
1244 if (need_unload != 0) {
1245 bus_dmamap_unload(ctx->buffer_dmat,
1246 ctx->buffer_dmamap);
1251 free(ctx, M_MPSUSER);
1255 mps_alloc_queues(struct mps_softc *sc)
1257 struct mps_queue *q;
1261 mps_dprint(sc, MPS_INIT|MPS_XINFO, "Allocating %d I/O queues\n", nq);
1263 sc->queues = malloc(sizeof(struct mps_queue) * nq, M_MPT2,
1265 if (sc->queues == NULL)
1268 for (i = 0; i < nq; i++) {
1270 mps_dprint(sc, MPS_INIT, "Configuring queue %d %p\n", i, q);
1279 mps_alloc_hw_queues(struct mps_softc *sc)
1281 bus_addr_t queues_busaddr;
1283 int qsize, fqsize, pqsize;
1286 * The reply free queue contains 4 byte entries in multiples of 16 and
1287 * aligned on a 16 byte boundary. There must always be an unused entry.
1288 * This queue supplies fresh reply frames for the firmware to use.
1290 * The reply descriptor post queue contains 8 byte entries in
1291 * multiples of 16 and aligned on a 16 byte boundary. This queue
1292 * contains filled-in reply frames sent from the firmware to the host.
1294 * These two queues are allocated together for simplicity.
1296 sc->fqdepth = roundup2(sc->num_replies + 1, 16);
1297 sc->pqdepth = roundup2(sc->num_replies + 1, 16);
1298 fqsize= sc->fqdepth * 4;
1299 pqsize = sc->pqdepth * 8;
1300 qsize = fqsize + pqsize;
1302 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
1303 16, 0, /* algnmnt, boundary */
1304 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1305 BUS_SPACE_MAXADDR, /* highaddr */
1306 NULL, NULL, /* filter, filterarg */
1307 qsize, /* maxsize */
1309 qsize, /* maxsegsize */
1311 NULL, NULL, /* lockfunc, lockarg */
1312 &sc->queues_dmat)) {
1313 mps_dprint(sc, MPS_ERROR, "Cannot allocate queues DMA tag\n");
1316 if (bus_dmamem_alloc(sc->queues_dmat, (void **)&queues, BUS_DMA_NOWAIT,
1318 mps_dprint(sc, MPS_ERROR, "Cannot allocate queues memory\n");
1321 bzero(queues, qsize);
1322 bus_dmamap_load(sc->queues_dmat, sc->queues_map, queues, qsize,
1323 mps_memaddr_cb, &queues_busaddr, 0);
1325 sc->free_queue = (uint32_t *)queues;
1326 sc->free_busaddr = queues_busaddr;
1327 sc->post_queue = (MPI2_REPLY_DESCRIPTORS_UNION *)(queues + fqsize);
1328 sc->post_busaddr = queues_busaddr + fqsize;
1329 mps_dprint(sc, MPS_INIT, "free queue busaddr= %#016jx size= %d\n",
1330 (uintmax_t)sc->free_busaddr, fqsize);
1331 mps_dprint(sc, MPS_INIT, "reply queue busaddr= %#016jx size= %d\n",
1332 (uintmax_t)sc->post_busaddr, pqsize);
1338 mps_alloc_replies(struct mps_softc *sc)
1340 int rsize, num_replies;
1342 /* Store the reply frame size in bytes rather than as 32bit words */
1343 sc->replyframesz = sc->facts->ReplyFrameSize * 4;
1346 * sc->num_replies should be one less than sc->fqdepth. We need to
1347 * allocate space for sc->fqdepth replies, but only sc->num_replies
1348 * replies can be used at once.
1350 num_replies = max(sc->fqdepth, sc->num_replies);
1352 rsize = sc->replyframesz * num_replies;
1353 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
1354 4, 0, /* algnmnt, boundary */
1355 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1356 BUS_SPACE_MAXADDR, /* highaddr */
1357 NULL, NULL, /* filter, filterarg */
1358 rsize, /* maxsize */
1360 rsize, /* maxsegsize */
1362 NULL, NULL, /* lockfunc, lockarg */
1364 mps_dprint(sc, MPS_ERROR, "Cannot allocate replies DMA tag\n");
1367 if (bus_dmamem_alloc(sc->reply_dmat, (void **)&sc->reply_frames,
1368 BUS_DMA_NOWAIT, &sc->reply_map)) {
1369 mps_dprint(sc, MPS_ERROR, "Cannot allocate replies memory\n");
1372 bzero(sc->reply_frames, rsize);
1373 bus_dmamap_load(sc->reply_dmat, sc->reply_map, sc->reply_frames, rsize,
1374 mps_memaddr_cb, &sc->reply_busaddr, 0);
1376 mps_dprint(sc, MPS_INIT, "reply frames busaddr= %#016jx size= %d\n",
1377 (uintmax_t)sc->reply_busaddr, rsize);
1383 mps_load_chains_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1385 struct mps_softc *sc = arg;
1386 struct mps_chain *chain;
1393 for (i = 0, o = 0, s = 0; s < nsegs; s++) {
1394 for (bo = 0; bo + sc->reqframesz <= segs[s].ds_len;
1395 bo += sc->reqframesz) {
1396 chain = &sc->chains[i++];
1397 chain->chain =(MPI2_SGE_IO_UNION *)(sc->chain_frames+o);
1398 chain->chain_busaddr = segs[s].ds_addr + bo;
1399 o += sc->reqframesz;
1400 mps_free_chain(sc, chain);
1402 if (bo != segs[s].ds_len)
1403 o += segs[s].ds_len - bo;
1405 sc->chain_free_lowwater = i;
1409 mps_alloc_requests(struct mps_softc *sc)
1411 struct mps_command *cm;
1412 int i, rsize, nsegs;
1414 rsize = sc->reqframesz * sc->num_reqs;
1415 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
1416 16, 0, /* algnmnt, boundary */
1417 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1418 BUS_SPACE_MAXADDR, /* highaddr */
1419 NULL, NULL, /* filter, filterarg */
1420 rsize, /* maxsize */
1422 rsize, /* maxsegsize */
1424 NULL, NULL, /* lockfunc, lockarg */
1426 mps_dprint(sc, MPS_ERROR, "Cannot allocate request DMA tag\n");
1429 if (bus_dmamem_alloc(sc->req_dmat, (void **)&sc->req_frames,
1430 BUS_DMA_NOWAIT, &sc->req_map)) {
1431 mps_dprint(sc, MPS_ERROR, "Cannot allocate request memory\n");
1434 bzero(sc->req_frames, rsize);
1435 bus_dmamap_load(sc->req_dmat, sc->req_map, sc->req_frames, rsize,
1436 mps_memaddr_cb, &sc->req_busaddr, 0);
1437 mps_dprint(sc, MPS_INIT, "request frames busaddr= %#016jx size= %d\n",
1438 (uintmax_t)sc->req_busaddr, rsize);
1440 sc->chains = malloc(sizeof(struct mps_chain) * sc->num_chains, M_MPT2,
1443 mps_dprint(sc, MPS_ERROR, "Cannot allocate chain memory\n");
1446 rsize = sc->reqframesz * sc->num_chains;
1447 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
1448 16, 0, /* algnmnt, boundary */
1449 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1450 BUS_SPACE_MAXADDR, /* highaddr */
1451 NULL, NULL, /* filter, filterarg */
1452 rsize, /* maxsize */
1453 howmany(rsize, PAGE_SIZE), /* nsegments */
1454 rsize, /* maxsegsize */
1456 NULL, NULL, /* lockfunc, lockarg */
1458 mps_dprint(sc, MPS_ERROR, "Cannot allocate chain DMA tag\n");
1461 if (bus_dmamem_alloc(sc->chain_dmat, (void **)&sc->chain_frames,
1462 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->chain_map)) {
1463 mps_dprint(sc, MPS_ERROR, "Cannot allocate chain memory\n");
1466 if (bus_dmamap_load(sc->chain_dmat, sc->chain_map, sc->chain_frames,
1467 rsize, mps_load_chains_cb, sc, BUS_DMA_NOWAIT)) {
1468 mps_dprint(sc, MPS_ERROR, "Cannot load chain memory\n");
1469 bus_dmamem_free(sc->chain_dmat, sc->chain_frames,
1474 rsize = MPS_SENSE_LEN * sc->num_reqs;
1475 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
1476 1, 0, /* algnmnt, boundary */
1477 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1478 BUS_SPACE_MAXADDR, /* highaddr */
1479 NULL, NULL, /* filter, filterarg */
1480 rsize, /* maxsize */
1482 rsize, /* maxsegsize */
1484 NULL, NULL, /* lockfunc, lockarg */
1486 mps_dprint(sc, MPS_ERROR, "Cannot allocate sense DMA tag\n");
1489 if (bus_dmamem_alloc(sc->sense_dmat, (void **)&sc->sense_frames,
1490 BUS_DMA_NOWAIT, &sc->sense_map)) {
1491 mps_dprint(sc, MPS_ERROR, "Cannot allocate sense memory\n");
1494 bzero(sc->sense_frames, rsize);
1495 bus_dmamap_load(sc->sense_dmat, sc->sense_map, sc->sense_frames, rsize,
1496 mps_memaddr_cb, &sc->sense_busaddr, 0);
1497 mps_dprint(sc, MPS_INIT, "sense frames busaddr= %#016jx size= %d\n",
1498 (uintmax_t)sc->sense_busaddr, rsize);
1500 nsegs = (sc->maxio / PAGE_SIZE) + 1;
1501 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
1502 1, 0, /* algnmnt, boundary */
1503 BUS_SPACE_MAXADDR, /* lowaddr */
1504 BUS_SPACE_MAXADDR, /* highaddr */
1505 NULL, NULL, /* filter, filterarg */
1506 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
1507 nsegs, /* nsegments */
1508 BUS_SPACE_MAXSIZE_24BIT,/* maxsegsize */
1509 BUS_DMA_ALLOCNOW, /* flags */
1510 busdma_lock_mutex, /* lockfunc */
1511 &sc->mps_mtx, /* lockarg */
1512 &sc->buffer_dmat)) {
1513 mps_dprint(sc, MPS_ERROR, "Cannot allocate buffer DMA tag\n");
1518 * SMID 0 cannot be used as a free command per the firmware spec.
1519 * Just drop that command instead of risking accounting bugs.
1521 sc->commands = malloc(sizeof(struct mps_command) * sc->num_reqs,
1522 M_MPT2, M_WAITOK | M_ZERO);
1524 mps_dprint(sc, MPS_ERROR, "Cannot allocate command memory\n");
1527 for (i = 1; i < sc->num_reqs; i++) {
1528 cm = &sc->commands[i];
1529 cm->cm_req = sc->req_frames + i * sc->reqframesz;
1530 cm->cm_req_busaddr = sc->req_busaddr + i * sc->reqframesz;
1531 cm->cm_sense = &sc->sense_frames[i];
1532 cm->cm_sense_busaddr = sc->sense_busaddr + i * MPS_SENSE_LEN;
1533 cm->cm_desc.Default.SMID = i;
1535 cm->cm_state = MPS_CM_STATE_BUSY;
1536 TAILQ_INIT(&cm->cm_chain_list);
1537 callout_init_mtx(&cm->cm_callout, &sc->mps_mtx, 0);
1539 /* XXX Is a failure here a critical problem? */
1540 if (bus_dmamap_create(sc->buffer_dmat, 0, &cm->cm_dmamap) == 0)
1541 if (i <= sc->num_prireqs)
1542 mps_free_high_priority_command(sc, cm);
1544 mps_free_command(sc, cm);
1546 panic("failed to allocate command %d\n", i);
1556 mps_init_queues(struct mps_softc *sc)
1560 memset((uint8_t *)sc->post_queue, 0xff, sc->pqdepth * 8);
1563 * According to the spec, we need to use one less reply than we
1564 * have space for on the queue. So sc->num_replies (the number we
1565 * use) should be less than sc->fqdepth (allocated size).
1567 if (sc->num_replies >= sc->fqdepth)
1571 * Initialize all of the free queue entries.
1573 for (i = 0; i < sc->fqdepth; i++)
1574 sc->free_queue[i] = sc->reply_busaddr + (i * sc->replyframesz);
1575 sc->replyfreeindex = sc->num_replies;
1580 /* Get the driver parameter tunables. Lowest priority are the driver defaults.
1581 * Next are the global settings, if they exist. Highest are the per-unit
1582 * settings, if they exist.
1585 mps_get_tunables(struct mps_softc *sc)
1587 char tmpstr[80], mps_debug[80];
1589 /* XXX default to some debugging for now */
1590 sc->mps_debug = MPS_INFO|MPS_FAULT;
1591 sc->disable_msix = 0;
1592 sc->disable_msi = 0;
1593 sc->max_msix = MPS_MSIX_MAX;
1594 sc->max_chains = MPS_CHAIN_FRAMES;
1595 sc->max_io_pages = MPS_MAXIO_PAGES;
1596 sc->enable_ssu = MPS_SSU_ENABLE_SSD_DISABLE_HDD;
1597 sc->spinup_wait_time = DEFAULT_SPINUP_WAIT;
1599 sc->max_reqframes = MPS_REQ_FRAMES;
1600 sc->max_prireqframes = MPS_PRI_REQ_FRAMES;
1601 sc->max_replyframes = MPS_REPLY_FRAMES;
1602 sc->max_evtframes = MPS_EVT_REPLY_FRAMES;
1605 * Grab the global variables.
1607 bzero(mps_debug, 80);
1608 if (TUNABLE_STR_FETCH("hw.mps.debug_level", mps_debug, 80) != 0)
1609 mps_parse_debug(sc, mps_debug);
1610 TUNABLE_INT_FETCH("hw.mps.disable_msix", &sc->disable_msix);
1611 TUNABLE_INT_FETCH("hw.mps.disable_msi", &sc->disable_msi);
1612 TUNABLE_INT_FETCH("hw.mps.max_msix", &sc->max_msix);
1613 TUNABLE_INT_FETCH("hw.mps.max_chains", &sc->max_chains);
1614 TUNABLE_INT_FETCH("hw.mps.max_io_pages", &sc->max_io_pages);
1615 TUNABLE_INT_FETCH("hw.mps.enable_ssu", &sc->enable_ssu);
1616 TUNABLE_INT_FETCH("hw.mps.spinup_wait_time", &sc->spinup_wait_time);
1617 TUNABLE_INT_FETCH("hw.mps.use_phy_num", &sc->use_phynum);
1618 TUNABLE_INT_FETCH("hw.mps.max_reqframes", &sc->max_reqframes);
1619 TUNABLE_INT_FETCH("hw.mps.max_prireqframes", &sc->max_prireqframes);
1620 TUNABLE_INT_FETCH("hw.mps.max_replyframes", &sc->max_replyframes);
1621 TUNABLE_INT_FETCH("hw.mps.max_evtframes", &sc->max_evtframes);
1623 /* Grab the unit-instance variables */
1624 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.debug_level",
1625 device_get_unit(sc->mps_dev));
1626 bzero(mps_debug, 80);
1627 if (TUNABLE_STR_FETCH(tmpstr, mps_debug, 80) != 0)
1628 mps_parse_debug(sc, mps_debug);
1630 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.disable_msix",
1631 device_get_unit(sc->mps_dev));
1632 TUNABLE_INT_FETCH(tmpstr, &sc->disable_msix);
1634 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.disable_msi",
1635 device_get_unit(sc->mps_dev));
1636 TUNABLE_INT_FETCH(tmpstr, &sc->disable_msi);
1638 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_msix",
1639 device_get_unit(sc->mps_dev));
1640 TUNABLE_INT_FETCH(tmpstr, &sc->max_msix);
1642 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_chains",
1643 device_get_unit(sc->mps_dev));
1644 TUNABLE_INT_FETCH(tmpstr, &sc->max_chains);
1646 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_io_pages",
1647 device_get_unit(sc->mps_dev));
1648 TUNABLE_INT_FETCH(tmpstr, &sc->max_io_pages);
1650 bzero(sc->exclude_ids, sizeof(sc->exclude_ids));
1651 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.exclude_ids",
1652 device_get_unit(sc->mps_dev));
1653 TUNABLE_STR_FETCH(tmpstr, sc->exclude_ids, sizeof(sc->exclude_ids));
1655 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.enable_ssu",
1656 device_get_unit(sc->mps_dev));
1657 TUNABLE_INT_FETCH(tmpstr, &sc->enable_ssu);
1659 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.spinup_wait_time",
1660 device_get_unit(sc->mps_dev));
1661 TUNABLE_INT_FETCH(tmpstr, &sc->spinup_wait_time);
1663 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.use_phy_num",
1664 device_get_unit(sc->mps_dev));
1665 TUNABLE_INT_FETCH(tmpstr, &sc->use_phynum);
1667 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_reqframes",
1668 device_get_unit(sc->mps_dev));
1669 TUNABLE_INT_FETCH(tmpstr, &sc->max_reqframes);
1671 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_prireqframes",
1672 device_get_unit(sc->mps_dev));
1673 TUNABLE_INT_FETCH(tmpstr, &sc->max_prireqframes);
1675 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_replyframes",
1676 device_get_unit(sc->mps_dev));
1677 TUNABLE_INT_FETCH(tmpstr, &sc->max_replyframes);
1679 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_evtframes",
1680 device_get_unit(sc->mps_dev));
1681 TUNABLE_INT_FETCH(tmpstr, &sc->max_evtframes);
1686 mps_setup_sysctl(struct mps_softc *sc)
1688 struct sysctl_ctx_list *sysctl_ctx = NULL;
1689 struct sysctl_oid *sysctl_tree = NULL;
1690 char tmpstr[80], tmpstr2[80];
1693 * Setup the sysctl variable so the user can change the debug level
1696 snprintf(tmpstr, sizeof(tmpstr), "MPS controller %d",
1697 device_get_unit(sc->mps_dev));
1698 snprintf(tmpstr2, sizeof(tmpstr2), "%d", device_get_unit(sc->mps_dev));
1700 sysctl_ctx = device_get_sysctl_ctx(sc->mps_dev);
1701 if (sysctl_ctx != NULL)
1702 sysctl_tree = device_get_sysctl_tree(sc->mps_dev);
1704 if (sysctl_tree == NULL) {
1705 sysctl_ctx_init(&sc->sysctl_ctx);
1706 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
1707 SYSCTL_STATIC_CHILDREN(_hw_mps), OID_AUTO, tmpstr2,
1708 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, tmpstr);
1709 if (sc->sysctl_tree == NULL)
1711 sysctl_ctx = &sc->sysctl_ctx;
1712 sysctl_tree = sc->sysctl_tree;
1715 SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1716 OID_AUTO, "debug_level", CTLTYPE_STRING | CTLFLAG_RW |CTLFLAG_MPSAFE,
1717 sc, 0, mps_debug_sysctl, "A", "mps debug level");
1719 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1720 OID_AUTO, "disable_msix", CTLFLAG_RD, &sc->disable_msix, 0,
1721 "Disable the use of MSI-X interrupts");
1723 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1724 OID_AUTO, "disable_msi", CTLFLAG_RD, &sc->disable_msi, 0,
1725 "Disable the use of MSI interrupts");
1727 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1728 OID_AUTO, "max_msix", CTLFLAG_RD, &sc->max_msix, 0,
1729 "User-defined maximum number of MSIX queues");
1731 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1732 OID_AUTO, "msix_msgs", CTLFLAG_RD, &sc->msi_msgs, 0,
1733 "Negotiated number of MSIX queues");
1735 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1736 OID_AUTO, "max_reqframes", CTLFLAG_RD, &sc->max_reqframes, 0,
1737 "Total number of allocated request frames");
1739 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1740 OID_AUTO, "max_prireqframes", CTLFLAG_RD, &sc->max_prireqframes, 0,
1741 "Total number of allocated high priority request frames");
1743 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1744 OID_AUTO, "max_replyframes", CTLFLAG_RD, &sc->max_replyframes, 0,
1745 "Total number of allocated reply frames");
1747 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1748 OID_AUTO, "max_evtframes", CTLFLAG_RD, &sc->max_evtframes, 0,
1749 "Total number of event frames allocated");
1751 SYSCTL_ADD_STRING(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1752 OID_AUTO, "firmware_version", CTLFLAG_RD, sc->fw_version,
1753 strlen(sc->fw_version), "firmware version");
1755 SYSCTL_ADD_STRING(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1756 OID_AUTO, "driver_version", CTLFLAG_RD, MPS_DRIVER_VERSION,
1757 strlen(MPS_DRIVER_VERSION), "driver version");
1759 SYSCTL_ADD_STRING(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1760 OID_AUTO, "msg_version", CTLFLAG_RD, sc->msg_version,
1761 strlen(sc->msg_version), "message interface version");
1763 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1764 OID_AUTO, "io_cmds_active", CTLFLAG_RD,
1765 &sc->io_cmds_active, 0, "number of currently active commands");
1767 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1768 OID_AUTO, "io_cmds_highwater", CTLFLAG_RD,
1769 &sc->io_cmds_highwater, 0, "maximum active commands seen");
1771 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1772 OID_AUTO, "chain_free", CTLFLAG_RD,
1773 &sc->chain_free, 0, "number of free chain elements");
1775 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1776 OID_AUTO, "chain_free_lowwater", CTLFLAG_RD,
1777 &sc->chain_free_lowwater, 0,"lowest number of free chain elements");
1779 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1780 OID_AUTO, "max_chains", CTLFLAG_RD,
1781 &sc->max_chains, 0,"maximum chain frames that will be allocated");
1783 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1784 OID_AUTO, "max_io_pages", CTLFLAG_RD,
1785 &sc->max_io_pages, 0,"maximum pages to allow per I/O (if <1 use "
1788 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1789 OID_AUTO, "enable_ssu", CTLFLAG_RW, &sc->enable_ssu, 0,
1790 "enable SSU to SATA SSD/HDD at shutdown");
1792 SYSCTL_ADD_UQUAD(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1793 OID_AUTO, "chain_alloc_fail", CTLFLAG_RD,
1794 &sc->chain_alloc_fail, "chain allocation failures");
1796 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1797 OID_AUTO, "spinup_wait_time", CTLFLAG_RD,
1798 &sc->spinup_wait_time, DEFAULT_SPINUP_WAIT, "seconds to wait for "
1799 "spinup after SATA ID error");
1801 SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1802 OID_AUTO, "mapping_table_dump",
1803 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
1804 mps_mapping_dump, "A", "Mapping Table Dump");
1806 SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1807 OID_AUTO, "encl_table_dump",
1808 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
1809 mps_mapping_encl_dump, "A", "Enclosure Table Dump");
1811 SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1812 OID_AUTO, "dump_reqs",
1813 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_NEEDGIANT,
1814 sc, 0, mps_dump_reqs, "I", "Dump Active Requests");
1816 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1817 OID_AUTO, "use_phy_num", CTLFLAG_RD, &sc->use_phynum, 0,
1818 "Use the phy number for enumeration");
1821 static struct mps_debug_string {
1824 } mps_debug_strings[] = {
1826 {"fault", MPS_FAULT},
1827 {"event", MPS_EVENT},
1829 {"recovery", MPS_RECOVERY},
1830 {"error", MPS_ERROR},
1832 {"xinfo", MPS_XINFO},
1834 {"mapping", MPS_MAPPING},
1835 {"trace", MPS_TRACE}
1838 enum mps_debug_level_combiner {
1845 mps_debug_sysctl(SYSCTL_HANDLER_ARGS)
1847 struct mps_softc *sc;
1848 struct mps_debug_string *string;
1852 int i, len, debug, error;
1854 sc = (struct mps_softc *)arg1;
1856 error = sysctl_wire_old_buffer(req, 0);
1860 sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
1861 debug = sc->mps_debug;
1863 sbuf_printf(sbuf, "%#x", debug);
1865 sz = sizeof(mps_debug_strings) / sizeof(mps_debug_strings[0]);
1866 for (i = 0; i < sz; i++) {
1867 string = &mps_debug_strings[i];
1868 if (debug & string->flag)
1869 sbuf_printf(sbuf, ",%s", string->name);
1872 error = sbuf_finish(sbuf);
1875 if (error || req->newptr == NULL)
1878 len = req->newlen - req->newidx;
1882 buffer = malloc(len, M_MPT2, M_ZERO|M_WAITOK);
1883 error = SYSCTL_IN(req, buffer, len);
1885 mps_parse_debug(sc, buffer);
1887 free(buffer, M_MPT2);
1892 mps_parse_debug(struct mps_softc *sc, char *list)
1894 struct mps_debug_string *string;
1895 enum mps_debug_level_combiner op;
1896 char *token, *endtoken;
1900 if (list == NULL || *list == '\0')
1906 } else if (*list == '-') {
1915 sz = sizeof(mps_debug_strings) / sizeof(mps_debug_strings[0]);
1916 while ((token = strsep(&list, ":,")) != NULL) {
1918 /* Handle integer flags */
1919 flags |= strtol(token, &endtoken, 0);
1920 if (token != endtoken)
1923 /* Handle text flags */
1924 for (i = 0; i < sz; i++) {
1925 string = &mps_debug_strings[i];
1926 if (strcasecmp(token, string->name) == 0) {
1927 flags |= string->flag;
1935 sc->mps_debug = flags;
1938 sc->mps_debug |= flags;
1941 sc->mps_debug &= (~flags);
1948 struct mps_dumpreq_hdr {
1957 mps_dump_reqs(SYSCTL_HANDLER_ARGS)
1959 struct mps_softc *sc;
1960 struct mps_chain *chain, *chain1;
1961 struct mps_command *cm;
1962 struct mps_dumpreq_hdr hdr;
1964 uint32_t smid, state;
1965 int i, numreqs, error = 0;
1967 sc = (struct mps_softc *)arg1;
1969 if ((error = priv_check(curthread, PRIV_DRIVER)) != 0) {
1970 printf("priv check error %d\n", error);
1974 state = MPS_CM_STATE_INQUEUE;
1976 numreqs = sc->num_reqs;
1978 if (req->newptr != NULL)
1981 if (smid == 0 || smid > sc->num_reqs)
1983 if (numreqs <= 0 || (numreqs + smid > sc->num_reqs))
1984 numreqs = sc->num_reqs;
1985 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
1987 /* Best effort, no locking */
1988 for (i = smid; i < numreqs; i++) {
1989 cm = &sc->commands[i];
1990 if (cm->cm_state != state)
1993 hdr.state = cm->cm_state;
1995 hdr.deschi = cm->cm_desc.Words.High;
1996 hdr.desclo = cm->cm_desc.Words.Low;
1997 TAILQ_FOREACH_SAFE(chain, &cm->cm_chain_list, chain_link,
2000 sbuf_bcat(sb, &hdr, sizeof(hdr));
2001 sbuf_bcat(sb, cm->cm_req, 128);
2002 TAILQ_FOREACH_SAFE(chain, &cm->cm_chain_list, chain_link,
2004 sbuf_bcat(sb, chain->chain, 128);
2007 error = sbuf_finish(sb);
2013 mps_attach(struct mps_softc *sc)
2018 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
2020 mtx_init(&sc->mps_mtx, "MPT2SAS lock", NULL, MTX_DEF);
2021 callout_init_mtx(&sc->periodic, &sc->mps_mtx, 0);
2022 callout_init_mtx(&sc->device_check_callout, &sc->mps_mtx, 0);
2023 TAILQ_INIT(&sc->event_list);
2024 timevalclear(&sc->lastfail);
2026 if ((error = mps_transition_ready(sc)) != 0) {
2027 mps_dprint(sc, MPS_INIT|MPS_FAULT, "failed to transition "
2032 sc->facts = malloc(sizeof(MPI2_IOC_FACTS_REPLY), M_MPT2,
2035 mps_dprint(sc, MPS_INIT|MPS_FAULT, "Cannot allocate memory, "
2041 * Get IOC Facts and allocate all structures based on this information.
2042 * A Diag Reset will also call mps_iocfacts_allocate and re-read the IOC
2043 * Facts. If relevant values have changed in IOC Facts, this function
2044 * will free all of the memory based on IOC Facts and reallocate that
2045 * memory. If this fails, any allocated memory should already be freed.
2047 if ((error = mps_iocfacts_allocate(sc, TRUE)) != 0) {
2048 mps_dprint(sc, MPS_INIT|MPS_FAULT, "IOC Facts based allocation "
2049 "failed with error %d, exit\n", error);
2053 /* Start the periodic watchdog check on the IOC Doorbell */
2057 * The portenable will kick off discovery events that will drive the
2058 * rest of the initialization process. The CAM/SAS module will
2059 * hold up the boot sequence until discovery is complete.
2061 sc->mps_ich.ich_func = mps_startup;
2062 sc->mps_ich.ich_arg = sc;
2063 if (config_intrhook_establish(&sc->mps_ich) != 0) {
2064 mps_dprint(sc, MPS_INIT|MPS_ERROR,
2065 "Cannot establish MPS config hook\n");
2070 * Allow IR to shutdown gracefully when shutdown occurs.
2072 sc->shutdown_eh = EVENTHANDLER_REGISTER(shutdown_final,
2073 mpssas_ir_shutdown, sc, SHUTDOWN_PRI_DEFAULT);
2075 if (sc->shutdown_eh == NULL)
2076 mps_dprint(sc, MPS_INIT|MPS_ERROR,
2077 "shutdown event registration failed\n");
2079 mps_setup_sysctl(sc);
2081 sc->mps_flags |= MPS_FLAGS_ATTACH_DONE;
2082 mps_dprint(sc, MPS_INIT, "%s exit error= %d\n", __func__, error);
2087 /* Run through any late-start handlers. */
2089 mps_startup(void *arg)
2091 struct mps_softc *sc;
2093 sc = (struct mps_softc *)arg;
2094 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
2097 mps_unmask_intr(sc);
2099 /* initialize device mapping tables */
2100 mps_base_static_config_pages(sc);
2101 mps_mapping_initialize(sc);
2105 mps_dprint(sc, MPS_INIT, "disestablish config intrhook\n");
2106 config_intrhook_disestablish(&sc->mps_ich);
2107 sc->mps_ich.ich_arg = NULL;
2109 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__);
2112 /* Periodic watchdog. Is called with the driver lock already held. */
2114 mps_periodic(void *arg)
2116 struct mps_softc *sc;
2119 sc = (struct mps_softc *)arg;
2120 if (sc->mps_flags & MPS_FLAGS_SHUTDOWN)
2123 db = mps_regread(sc, MPI2_DOORBELL_OFFSET);
2124 if ((db & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2125 mps_dprint(sc, MPS_FAULT, "IOC Fault 0x%08x, Resetting\n", db);
2129 callout_reset(&sc->periodic, MPS_PERIODIC_DELAY * hz, mps_periodic, sc);
2133 mps_log_evt_handler(struct mps_softc *sc, uintptr_t data,
2134 MPI2_EVENT_NOTIFICATION_REPLY *event)
2136 MPI2_EVENT_DATA_LOG_ENTRY_ADDED *entry;
2138 MPS_DPRINT_EVENT(sc, generic, event);
2140 switch (event->Event) {
2141 case MPI2_EVENT_LOG_DATA:
2142 mps_dprint(sc, MPS_EVENT, "MPI2_EVENT_LOG_DATA:\n");
2143 if (sc->mps_debug & MPS_EVENT)
2144 hexdump(event->EventData, event->EventDataLength, NULL, 0);
2146 case MPI2_EVENT_LOG_ENTRY_ADDED:
2147 entry = (MPI2_EVENT_DATA_LOG_ENTRY_ADDED *)event->EventData;
2148 mps_dprint(sc, MPS_EVENT, "MPI2_EVENT_LOG_ENTRY_ADDED event "
2149 "0x%x Sequence %d:\n", entry->LogEntryQualifier,
2150 entry->LogSequence);
2159 mps_attach_log(struct mps_softc *sc)
2161 u32 events[MPI2_EVENT_NOTIFY_EVENTMASK_WORDS];
2164 setbit(events, MPI2_EVENT_LOG_DATA);
2165 setbit(events, MPI2_EVENT_LOG_ENTRY_ADDED);
2167 mps_register_events(sc, events, mps_log_evt_handler, NULL,
2174 mps_detach_log(struct mps_softc *sc)
2177 if (sc->mps_log_eh != NULL)
2178 mps_deregister_events(sc, sc->mps_log_eh);
2183 * Free all of the driver resources and detach submodules. Should be called
2184 * without the lock held.
2187 mps_free(struct mps_softc *sc)
2191 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
2192 /* Turn off the watchdog */
2194 sc->mps_flags |= MPS_FLAGS_SHUTDOWN;
2196 /* Lock must not be held for this */
2197 callout_drain(&sc->periodic);
2198 callout_drain(&sc->device_check_callout);
2200 if (((error = mps_detach_log(sc)) != 0) ||
2201 ((error = mps_detach_sas(sc)) != 0)) {
2202 mps_dprint(sc, MPS_INIT|MPS_FAULT, "failed to detach "
2203 "subsystems, exit\n");
2207 mps_detach_user(sc);
2209 /* Put the IOC back in the READY state. */
2211 if ((error = mps_transition_ready(sc)) != 0) {
2217 if (sc->facts != NULL)
2218 free(sc->facts, M_MPT2);
2221 * Free all buffers that are based on IOC Facts. A Diag Reset may need
2222 * to free these buffers too.
2224 mps_iocfacts_free(sc);
2226 if (sc->sysctl_tree != NULL)
2227 sysctl_ctx_free(&sc->sysctl_ctx);
2229 /* Deregister the shutdown function */
2230 if (sc->shutdown_eh != NULL)
2231 EVENTHANDLER_DEREGISTER(shutdown_final, sc->shutdown_eh);
2233 mtx_destroy(&sc->mps_mtx);
2234 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__);
2239 static __inline void
2240 mps_complete_command(struct mps_softc *sc, struct mps_command *cm)
2245 mps_dprint(sc, MPS_ERROR, "Completing NULL command\n");
2249 if (cm->cm_flags & MPS_CM_FLAGS_POLLED)
2250 cm->cm_flags |= MPS_CM_FLAGS_COMPLETE;
2252 if (cm->cm_complete != NULL) {
2253 mps_dprint(sc, MPS_TRACE,
2254 "%s cm %p calling cm_complete %p data %p reply %p\n",
2255 __func__, cm, cm->cm_complete, cm->cm_complete_data,
2257 cm->cm_complete(sc, cm);
2260 if (cm->cm_flags & MPS_CM_FLAGS_WAKEUP) {
2261 mps_dprint(sc, MPS_TRACE, "waking up %p\n", cm);
2265 if (cm->cm_sc->io_cmds_active != 0) {
2266 cm->cm_sc->io_cmds_active--;
2268 mps_dprint(sc, MPS_ERROR, "Warning: io_cmds_active is "
2269 "out of sync - resynching to 0\n");
2275 mps_sas_log_info(struct mps_softc *sc , u32 log_info)
2277 union loginfo_type {
2286 union loginfo_type sas_loginfo;
2287 char *originator_str = NULL;
2289 sas_loginfo.loginfo = log_info;
2290 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
2293 /* each nexus loss loginfo */
2294 if (log_info == 0x31170000)
2297 /* eat the loginfos associated with task aborts */
2298 if ((log_info == 30050000 || log_info ==
2299 0x31140000 || log_info == 0x31130000))
2302 switch (sas_loginfo.dw.originator) {
2304 originator_str = "IOP";
2307 originator_str = "PL";
2310 originator_str = "IR";
2314 mps_dprint(sc, MPS_LOG, "log_info(0x%08x): originator(%s), "
2315 "code(0x%02x), sub_code(0x%04x)\n", log_info,
2316 originator_str, sas_loginfo.dw.code,
2317 sas_loginfo.dw.subcode);
2321 mps_display_reply_info(struct mps_softc *sc, uint8_t *reply)
2323 MPI2DefaultReply_t *mpi_reply;
2326 mpi_reply = (MPI2DefaultReply_t*)reply;
2327 sc_status = le16toh(mpi_reply->IOCStatus);
2328 if (sc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
2329 mps_sas_log_info(sc, le32toh(mpi_reply->IOCLogInfo));
2332 mps_intr(void *data)
2334 struct mps_softc *sc;
2337 sc = (struct mps_softc *)data;
2338 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
2341 * Check interrupt status register to flush the bus. This is
2342 * needed for both INTx interrupts and driver-driven polling
2344 status = mps_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET);
2345 if ((status & MPI2_HIS_REPLY_DESCRIPTOR_INTERRUPT) == 0)
2349 mps_intr_locked(data);
2355 * In theory, MSI/MSIX interrupts shouldn't need to read any registers on the
2356 * chip. Hopefully this theory is correct.
2359 mps_intr_msi(void *data)
2361 struct mps_softc *sc;
2363 sc = (struct mps_softc *)data;
2364 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
2366 mps_intr_locked(data);
2372 * The locking is overly broad and simplistic, but easy to deal with for now.
2375 mps_intr_locked(void *data)
2377 MPI2_REPLY_DESCRIPTORS_UNION *desc;
2378 MPI2_DIAG_RELEASE_REPLY *rel_rep;
2379 mps_fw_diagnostic_buffer_t *pBuffer;
2380 struct mps_softc *sc;
2381 struct mps_command *cm = NULL;
2386 sc = (struct mps_softc *)data;
2388 pq = sc->replypostindex;
2389 mps_dprint(sc, MPS_TRACE,
2390 "%s sc %p starting with replypostindex %u\n",
2391 __func__, sc, sc->replypostindex);
2395 desc = &sc->post_queue[sc->replypostindex];
2398 * Copy and clear out the descriptor so that any reentry will
2399 * immediately know that this descriptor has already been
2400 * looked at. There is unfortunate casting magic because the
2401 * MPI API doesn't have a cardinal 64bit type.
2403 tdesc = 0xffffffffffffffff;
2404 tdesc = atomic_swap_64((uint64_t *)desc, tdesc);
2405 desc = (MPI2_REPLY_DESCRIPTORS_UNION *)&tdesc;
2407 flags = desc->Default.ReplyFlags &
2408 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
2409 if ((flags == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
2410 || (le32toh(desc->Words.High) == 0xffffffff))
2413 /* increment the replypostindex now, so that event handlers
2414 * and cm completion handlers which decide to do a diag
2415 * reset can zero it without it getting incremented again
2416 * afterwards, and we break out of this loop on the next
2417 * iteration since the reply post queue has been cleared to
2418 * 0xFF and all descriptors look unused (which they are).
2420 if (++sc->replypostindex >= sc->pqdepth)
2421 sc->replypostindex = 0;
2424 case MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS:
2425 cm = &sc->commands[le16toh(desc->SCSIIOSuccess.SMID)];
2426 KASSERT(cm->cm_state == MPS_CM_STATE_INQUEUE,
2427 ("command not inqueue\n"));
2428 cm->cm_state = MPS_CM_STATE_BUSY;
2429 cm->cm_reply = NULL;
2431 case MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY:
2437 * Re-compose the reply address from the address
2438 * sent back from the chip. The ReplyFrameAddress
2439 * is the lower 32 bits of the physical address of
2440 * particular reply frame. Convert that address to
2441 * host format, and then use that to provide the
2442 * offset against the virtual address base
2443 * (sc->reply_frames).
2445 baddr = le32toh(desc->AddressReply.ReplyFrameAddress);
2446 reply = sc->reply_frames +
2447 (baddr - ((uint32_t)sc->reply_busaddr));
2449 * Make sure the reply we got back is in a valid
2450 * range. If not, go ahead and panic here, since
2451 * we'll probably panic as soon as we deference the
2452 * reply pointer anyway.
2454 if ((reply < sc->reply_frames)
2455 || (reply > (sc->reply_frames +
2456 (sc->fqdepth * sc->replyframesz)))) {
2457 printf("%s: WARNING: reply %p out of range!\n",
2459 printf("%s: reply_frames %p, fqdepth %d, "
2460 "frame size %d\n", __func__,
2461 sc->reply_frames, sc->fqdepth,
2463 printf("%s: baddr %#x,\n", __func__, baddr);
2464 /* LSI-TODO. See Linux Code for Graceful exit */
2465 panic("Reply address out of range");
2467 if (le16toh(desc->AddressReply.SMID) == 0) {
2468 if (((MPI2_DEFAULT_REPLY *)reply)->Function ==
2469 MPI2_FUNCTION_DIAG_BUFFER_POST) {
2471 * If SMID is 0 for Diag Buffer Post,
2472 * this implies that the reply is due to
2473 * a release function with a status that
2474 * the buffer has been released. Set
2475 * the buffer flags accordingly.
2478 (MPI2_DIAG_RELEASE_REPLY *)reply;
2479 if ((le16toh(rel_rep->IOCStatus) &
2480 MPI2_IOCSTATUS_MASK) ==
2481 MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED)
2484 &sc->fw_diag_buffer_list[
2485 rel_rep->BufferType];
2486 pBuffer->valid_data = TRUE;
2487 pBuffer->owned_by_firmware =
2489 pBuffer->immediate = FALSE;
2492 mps_dispatch_event(sc, baddr,
2493 (MPI2_EVENT_NOTIFICATION_REPLY *)
2497 * Ignore commands not in INQUEUE state
2498 * since they've already been completed
2502 le16toh(desc->AddressReply.SMID)];
2503 if (cm->cm_state == MPS_CM_STATE_INQUEUE) {
2504 cm->cm_state = MPS_CM_STATE_BUSY;
2505 cm->cm_reply = reply;
2506 cm->cm_reply_data = le32toh(
2507 desc->AddressReply.ReplyFrameAddress);
2509 mps_dprint(sc, MPS_RECOVERY,
2510 "Bad state for ADDRESS_REPLY status,"
2511 " ignoring state %d cm %p\n",
2517 case MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS:
2518 case MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER:
2519 case MPI2_RPY_DESCRIPT_FLAGS_RAID_ACCELERATOR_SUCCESS:
2522 mps_dprint(sc, MPS_ERROR, "Unhandled reply 0x%x\n",
2523 desc->Default.ReplyFlags);
2530 // Print Error reply frame
2532 mps_display_reply_info(sc,cm->cm_reply);
2533 mps_complete_command(sc, cm);
2537 if (pq != sc->replypostindex) {
2538 mps_dprint(sc, MPS_TRACE, "%s sc %p writing postindex %d\n",
2539 __func__, sc, sc->replypostindex);
2540 mps_regwrite(sc, MPI2_REPLY_POST_HOST_INDEX_OFFSET,
2541 sc->replypostindex);
2548 mps_dispatch_event(struct mps_softc *sc, uintptr_t data,
2549 MPI2_EVENT_NOTIFICATION_REPLY *reply)
2551 struct mps_event_handle *eh;
2552 int event, handled = 0;
2554 event = le16toh(reply->Event);
2555 TAILQ_FOREACH(eh, &sc->event_list, eh_list) {
2556 if (isset(eh->mask, event)) {
2557 eh->callback(sc, data, reply);
2563 mps_dprint(sc, MPS_EVENT, "Unhandled event 0x%x\n", le16toh(event));
2566 * This is the only place that the event/reply should be freed.
2567 * Anything wanting to hold onto the event data should have
2568 * already copied it into their own storage.
2570 mps_free_reply(sc, data);
2574 mps_reregister_events_complete(struct mps_softc *sc, struct mps_command *cm)
2576 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
2579 MPS_DPRINT_EVENT(sc, generic,
2580 (MPI2_EVENT_NOTIFICATION_REPLY *)cm->cm_reply);
2582 mps_free_command(sc, cm);
2584 /* next, send a port enable */
2589 * For both register_events and update_events, the caller supplies a bitmap
2590 * of events that it _wants_. These functions then turn that into a bitmask
2591 * suitable for the controller.
2594 mps_register_events(struct mps_softc *sc, u32 *mask,
2595 mps_evt_callback_t *cb, void *data, struct mps_event_handle **handle)
2597 struct mps_event_handle *eh;
2600 eh = malloc(sizeof(struct mps_event_handle), M_MPT2, M_WAITOK|M_ZERO);
2602 mps_dprint(sc, MPS_ERROR, "Cannot allocate event memory\n");
2607 TAILQ_INSERT_TAIL(&sc->event_list, eh, eh_list);
2609 error = mps_update_events(sc, eh, mask);
2616 mps_update_events(struct mps_softc *sc, struct mps_event_handle *handle,
2619 MPI2_EVENT_NOTIFICATION_REQUEST *evtreq;
2620 MPI2_EVENT_NOTIFICATION_REPLY *reply = NULL;
2621 struct mps_command *cm;
2624 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
2626 if ((mask != NULL) && (handle != NULL))
2627 bcopy(mask, &handle->mask[0], sizeof(u32) *
2628 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS);
2630 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2631 sc->event_mask[i] = -1;
2633 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2634 sc->event_mask[i] &= ~handle->mask[i];
2637 if ((cm = mps_alloc_command(sc)) == NULL)
2639 evtreq = (MPI2_EVENT_NOTIFICATION_REQUEST *)cm->cm_req;
2640 evtreq->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
2641 evtreq->MsgFlags = 0;
2642 evtreq->SASBroadcastPrimitiveMasks = 0;
2643 #ifdef MPS_DEBUG_ALL_EVENTS
2645 u_char fullmask[16];
2646 memset(fullmask, 0x00, 16);
2647 bcopy(fullmask, &evtreq->EventMasks[0], sizeof(u32) *
2648 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS);
2651 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2652 evtreq->EventMasks[i] =
2653 htole32(sc->event_mask[i]);
2655 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2658 error = mps_wait_command(sc, &cm, 60, 0);
2660 reply = (MPI2_EVENT_NOTIFICATION_REPLY *)cm->cm_reply;
2661 if ((reply == NULL) ||
2662 (reply->IOCStatus & MPI2_IOCSTATUS_MASK) != MPI2_IOCSTATUS_SUCCESS)
2666 MPS_DPRINT_EVENT(sc, generic, reply);
2668 mps_dprint(sc, MPS_TRACE, "%s finished error %d\n", __func__, error);
2671 mps_free_command(sc, cm);
2676 mps_reregister_events(struct mps_softc *sc)
2678 MPI2_EVENT_NOTIFICATION_REQUEST *evtreq;
2679 struct mps_command *cm;
2680 struct mps_event_handle *eh;
2683 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
2685 /* first, reregister events */
2687 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2688 sc->event_mask[i] = -1;
2690 TAILQ_FOREACH(eh, &sc->event_list, eh_list) {
2691 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2692 sc->event_mask[i] &= ~eh->mask[i];
2695 if ((cm = mps_alloc_command(sc)) == NULL)
2697 evtreq = (MPI2_EVENT_NOTIFICATION_REQUEST *)cm->cm_req;
2698 evtreq->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
2699 evtreq->MsgFlags = 0;
2700 evtreq->SASBroadcastPrimitiveMasks = 0;
2701 #ifdef MPS_DEBUG_ALL_EVENTS
2703 u_char fullmask[16];
2704 memset(fullmask, 0x00, 16);
2705 bcopy(fullmask, &evtreq->EventMasks[0], sizeof(u32) *
2706 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS);
2709 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2710 evtreq->EventMasks[i] =
2711 htole32(sc->event_mask[i]);
2713 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2715 cm->cm_complete = mps_reregister_events_complete;
2717 error = mps_map_command(sc, cm);
2719 mps_dprint(sc, MPS_TRACE, "%s finished with error %d\n", __func__,
2725 mps_deregister_events(struct mps_softc *sc, struct mps_event_handle *handle)
2728 TAILQ_REMOVE(&sc->event_list, handle, eh_list);
2729 free(handle, M_MPT2);
2733 * Add a chain element as the next SGE for the specified command.
2734 * Reset cm_sge and cm_sgesize to indicate all the available space.
2737 mps_add_chain(struct mps_command *cm)
2739 MPI2_SGE_CHAIN32 *sgc;
2740 struct mps_chain *chain;
2743 if (cm->cm_sglsize < MPS_SGC_SIZE)
2744 panic("MPS: Need SGE Error Code\n");
2746 chain = mps_alloc_chain(cm->cm_sc);
2750 space = cm->cm_sc->reqframesz;
2753 * Note: a double-linked list is used to make it easier to
2754 * walk for debugging.
2756 TAILQ_INSERT_TAIL(&cm->cm_chain_list, chain, chain_link);
2758 sgc = (MPI2_SGE_CHAIN32 *)&cm->cm_sge->MpiChain;
2759 sgc->Length = htole16(space);
2760 sgc->NextChainOffset = 0;
2761 /* TODO Looks like bug in Setting sgc->Flags.
2762 * sgc->Flags = ( MPI2_SGE_FLAGS_CHAIN_ELEMENT | MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
2763 * MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT
2764 * This is fine.. because we are not using simple element. In case of
2765 * MPI2_SGE_CHAIN32, we have separate Length and Flags feild.
2767 sgc->Flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT;
2768 sgc->Address = htole32(chain->chain_busaddr);
2770 cm->cm_sge = (MPI2_SGE_IO_UNION *)&chain->chain->MpiSimple;
2771 cm->cm_sglsize = space;
2776 * Add one scatter-gather element (chain, simple, transaction context)
2777 * to the scatter-gather list for a command. Maintain cm_sglsize and
2778 * cm_sge as the remaining size and pointer to the next SGE to fill
2782 mps_push_sge(struct mps_command *cm, void *sgep, size_t len, int segsleft)
2784 MPI2_SGE_TRANSACTION_UNION *tc = sgep;
2785 MPI2_SGE_SIMPLE64 *sge = sgep;
2787 uint32_t saved_buf_len, saved_address_low, saved_address_high;
2789 type = (tc->Flags & MPI2_SGE_FLAGS_ELEMENT_MASK);
2793 case MPI2_SGE_FLAGS_TRANSACTION_ELEMENT: {
2794 if (len != tc->DetailsLength + 4)
2795 panic("TC %p length %u or %zu?", tc,
2796 tc->DetailsLength + 4, len);
2799 case MPI2_SGE_FLAGS_CHAIN_ELEMENT:
2800 /* Driver only uses 32-bit chain elements */
2801 if (len != MPS_SGC_SIZE)
2802 panic("CHAIN %p length %u or %zu?", sgep,
2805 case MPI2_SGE_FLAGS_SIMPLE_ELEMENT:
2806 /* Driver only uses 64-bit SGE simple elements */
2807 if (len != MPS_SGE64_SIZE)
2808 panic("SGE simple %p length %u or %zu?", sge,
2809 MPS_SGE64_SIZE, len);
2810 if (((le32toh(sge->FlagsLength) >> MPI2_SGE_FLAGS_SHIFT) &
2811 MPI2_SGE_FLAGS_ADDRESS_SIZE) == 0)
2812 panic("SGE simple %p not marked 64-bit?", sge);
2816 panic("Unexpected SGE %p, flags %02x", tc, tc->Flags);
2821 * case 1: 1 more segment, enough room for it
2822 * case 2: 2 more segments, enough room for both
2823 * case 3: >=2 more segments, only enough room for 1 and a chain
2824 * case 4: >=1 more segment, enough room for only a chain
2825 * case 5: >=1 more segment, no room for anything (error)
2829 * There should be room for at least a chain element, or this
2830 * code is buggy. Case (5).
2832 if (cm->cm_sglsize < MPS_SGC_SIZE)
2833 panic("MPS: Need SGE Error Code\n");
2835 if (segsleft >= 1 && cm->cm_sglsize < len + MPS_SGC_SIZE) {
2837 * 1 or more segment, enough room for only a chain.
2838 * Hope the previous element wasn't a Simple entry
2839 * that needed to be marked with
2840 * MPI2_SGE_FLAGS_LAST_ELEMENT. Case (4).
2842 if ((error = mps_add_chain(cm)) != 0)
2846 if (segsleft >= 2 &&
2847 cm->cm_sglsize < len + MPS_SGC_SIZE + MPS_SGE64_SIZE) {
2849 * There are 2 or more segments left to add, and only
2850 * enough room for 1 and a chain. Case (3).
2852 * Mark as last element in this chain if necessary.
2854 if (type == MPI2_SGE_FLAGS_SIMPLE_ELEMENT) {
2855 sge->FlagsLength |= htole32(
2856 MPI2_SGE_FLAGS_LAST_ELEMENT << MPI2_SGE_FLAGS_SHIFT);
2860 * Add the item then a chain. Do the chain now,
2861 * rather than on the next iteration, to simplify
2862 * understanding the code.
2864 cm->cm_sglsize -= len;
2865 bcopy(sgep, cm->cm_sge, len);
2866 cm->cm_sge = (MPI2_SGE_IO_UNION *)((uintptr_t)cm->cm_sge + len);
2867 return (mps_add_chain(cm));
2871 /* Case 1: 1 more segment, enough room for it. */
2872 if (segsleft == 1 && cm->cm_sglsize < len)
2873 panic("1 seg left and no room? %u versus %zu",
2874 cm->cm_sglsize, len);
2876 /* Case 2: 2 more segments, enough room for both */
2877 if (segsleft == 2 && cm->cm_sglsize < len + MPS_SGE64_SIZE)
2878 panic("2 segs left and no room? %u versus %zu",
2879 cm->cm_sglsize, len);
2882 if (segsleft == 1 && type == MPI2_SGE_FLAGS_SIMPLE_ELEMENT) {
2884 * If this is a bi-directional request, need to account for that
2885 * here. Save the pre-filled sge values. These will be used
2886 * either for the 2nd SGL or for a single direction SGL. If
2887 * cm_out_len is non-zero, this is a bi-directional request, so
2888 * fill in the OUT SGL first, then the IN SGL, otherwise just
2889 * fill in the IN SGL. Note that at this time, when filling in
2890 * 2 SGL's for a bi-directional request, they both use the same
2891 * DMA buffer (same cm command).
2893 saved_buf_len = le32toh(sge->FlagsLength) & 0x00FFFFFF;
2894 saved_address_low = sge->Address.Low;
2895 saved_address_high = sge->Address.High;
2896 if (cm->cm_out_len) {
2897 sge->FlagsLength = htole32(cm->cm_out_len |
2898 ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2899 MPI2_SGE_FLAGS_END_OF_BUFFER |
2900 MPI2_SGE_FLAGS_HOST_TO_IOC |
2901 MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
2902 MPI2_SGE_FLAGS_SHIFT));
2903 cm->cm_sglsize -= len;
2904 bcopy(sgep, cm->cm_sge, len);
2905 cm->cm_sge = (MPI2_SGE_IO_UNION *)((uintptr_t)cm->cm_sge
2909 ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2910 MPI2_SGE_FLAGS_END_OF_BUFFER |
2911 MPI2_SGE_FLAGS_LAST_ELEMENT |
2912 MPI2_SGE_FLAGS_END_OF_LIST |
2913 MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
2914 MPI2_SGE_FLAGS_SHIFT);
2915 if (cm->cm_flags & MPS_CM_FLAGS_DATAIN) {
2917 ((uint32_t)(MPI2_SGE_FLAGS_IOC_TO_HOST) <<
2918 MPI2_SGE_FLAGS_SHIFT);
2921 ((uint32_t)(MPI2_SGE_FLAGS_HOST_TO_IOC) <<
2922 MPI2_SGE_FLAGS_SHIFT);
2924 sge->FlagsLength = htole32(saved_buf_len);
2925 sge->Address.Low = saved_address_low;
2926 sge->Address.High = saved_address_high;
2929 cm->cm_sglsize -= len;
2930 bcopy(sgep, cm->cm_sge, len);
2931 cm->cm_sge = (MPI2_SGE_IO_UNION *)((uintptr_t)cm->cm_sge + len);
2936 * Add one dma segment to the scatter-gather list for a command.
2939 mps_add_dmaseg(struct mps_command *cm, vm_paddr_t pa, size_t len, u_int flags,
2942 MPI2_SGE_SIMPLE64 sge;
2945 * This driver always uses 64-bit address elements for simplicity.
2947 bzero(&sge, sizeof(sge));
2948 flags |= MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2949 MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
2950 sge.FlagsLength = htole32(len | (flags << MPI2_SGE_FLAGS_SHIFT));
2951 mps_from_u64(pa, &sge.Address);
2953 return (mps_push_sge(cm, &sge, sizeof sge, segsleft));
2957 mps_data_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
2959 struct mps_softc *sc;
2960 struct mps_command *cm;
2961 u_int i, dir, sflags;
2963 cm = (struct mps_command *)arg;
2967 * In this case, just print out a warning and let the chip tell the
2968 * user they did the wrong thing.
2970 if ((cm->cm_max_segs != 0) && (nsegs > cm->cm_max_segs)) {
2971 mps_dprint(sc, MPS_ERROR,
2972 "%s: warning: busdma returned %d segments, "
2973 "more than the %d allowed\n", __func__, nsegs,
2978 * Set up DMA direction flags. Bi-directional requests are also handled
2979 * here. In that case, both direction flags will be set.
2982 if (cm->cm_flags & MPS_CM_FLAGS_SMP_PASS) {
2984 * We have to add a special case for SMP passthrough, there
2985 * is no easy way to generically handle it. The first
2986 * S/G element is used for the command (therefore the
2987 * direction bit needs to be set). The second one is used
2988 * for the reply. We'll leave it to the caller to make
2989 * sure we only have two buffers.
2992 * Even though the busdma man page says it doesn't make
2993 * sense to have both direction flags, it does in this case.
2994 * We have one s/g element being accessed in each direction.
2996 dir = BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD;
2999 * Set the direction flag on the first buffer in the SMP
3000 * passthrough request. We'll clear it for the second one.
3002 sflags |= MPI2_SGE_FLAGS_DIRECTION |
3003 MPI2_SGE_FLAGS_END_OF_BUFFER;
3004 } else if (cm->cm_flags & MPS_CM_FLAGS_DATAOUT) {
3005 sflags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
3006 dir = BUS_DMASYNC_PREWRITE;
3008 dir = BUS_DMASYNC_PREREAD;
3010 for (i = 0; i < nsegs; i++) {
3011 if ((cm->cm_flags & MPS_CM_FLAGS_SMP_PASS) && (i != 0)) {
3012 sflags &= ~MPI2_SGE_FLAGS_DIRECTION;
3014 error = mps_add_dmaseg(cm, segs[i].ds_addr, segs[i].ds_len,
3017 /* Resource shortage, roll back! */
3018 if (ratecheck(&sc->lastfail, &mps_chainfail_interval))
3019 mps_dprint(sc, MPS_INFO, "Out of chain frames, "
3020 "consider increasing hw.mps.max_chains.\n");
3021 cm->cm_flags |= MPS_CM_FLAGS_CHAIN_FAILED;
3022 mps_complete_command(sc, cm);
3027 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir);
3028 mps_enqueue_request(sc, cm);
3034 mps_data_cb2(void *arg, bus_dma_segment_t *segs, int nsegs, bus_size_t mapsize,
3037 mps_data_cb(arg, segs, nsegs, error);
3041 * This is the routine to enqueue commands ansynchronously.
3042 * Note that the only error path here is from bus_dmamap_load(), which can
3043 * return EINPROGRESS if it is waiting for resources. Other than this, it's
3044 * assumed that if you have a command in-hand, then you have enough credits
3048 mps_map_command(struct mps_softc *sc, struct mps_command *cm)
3052 if (cm->cm_flags & MPS_CM_FLAGS_USE_UIO) {
3053 error = bus_dmamap_load_uio(sc->buffer_dmat, cm->cm_dmamap,
3054 &cm->cm_uio, mps_data_cb2, cm, 0);
3055 } else if (cm->cm_flags & MPS_CM_FLAGS_USE_CCB) {
3056 error = bus_dmamap_load_ccb(sc->buffer_dmat, cm->cm_dmamap,
3057 cm->cm_data, mps_data_cb, cm, 0);
3058 } else if ((cm->cm_data != NULL) && (cm->cm_length != 0)) {
3059 error = bus_dmamap_load(sc->buffer_dmat, cm->cm_dmamap,
3060 cm->cm_data, cm->cm_length, mps_data_cb, cm, 0);
3062 /* Add a zero-length element as needed */
3063 if (cm->cm_sge != NULL)
3064 mps_add_dmaseg(cm, 0, 0, 0, 1);
3065 mps_enqueue_request(sc, cm);
3072 * This is the routine to enqueue commands synchronously. An error of
3073 * EINPROGRESS from mps_map_command() is ignored since the command will
3074 * be executed and enqueued automatically. Other errors come from msleep().
3077 mps_wait_command(struct mps_softc *sc, struct mps_command **cmp, int timeout,
3081 struct timeval cur_time, start_time;
3082 struct mps_command *cm = *cmp;
3084 if (sc->mps_flags & MPS_FLAGS_DIAGRESET)
3087 cm->cm_complete = NULL;
3088 cm->cm_flags |= MPS_CM_FLAGS_POLLED;
3089 error = mps_map_command(sc, cm);
3090 if ((error != 0) && (error != EINPROGRESS))
3094 * Check for context and wait for 50 mSec at a time until time has
3095 * expired or the command has finished. If msleep can't be used, need
3098 if (curthread->td_no_sleeping != 0)
3099 sleep_flag = NO_SLEEP;
3100 getmicrouptime(&start_time);
3101 if (mtx_owned(&sc->mps_mtx) && sleep_flag == CAN_SLEEP) {
3102 cm->cm_flags |= MPS_CM_FLAGS_WAKEUP;
3103 error = msleep(cm, &sc->mps_mtx, 0, "mpswait", timeout*hz);
3104 if (error == EWOULDBLOCK) {
3106 * Record the actual elapsed time in the case of a
3107 * timeout for the message below.
3109 getmicrouptime(&cur_time);
3110 timevalsub(&cur_time, &start_time);
3113 while ((cm->cm_flags & MPS_CM_FLAGS_COMPLETE) == 0) {
3114 mps_intr_locked(sc);
3115 if (sleep_flag == CAN_SLEEP)
3116 pause("mpswait", hz/20);
3120 getmicrouptime(&cur_time);
3121 timevalsub(&cur_time, &start_time);
3122 if (cur_time.tv_sec > timeout) {
3123 error = EWOULDBLOCK;
3129 if (error == EWOULDBLOCK) {
3130 if (cm->cm_timeout_handler == NULL) {
3131 mps_dprint(sc, MPS_FAULT, "Calling Reinit from %s, timeout=%d,"
3132 " elapsed=%jd\n", __func__, timeout,
3133 (intmax_t)cur_time.tv_sec);
3134 rc = mps_reinit(sc);
3135 mps_dprint(sc, MPS_FAULT, "Reinit %s\n", (rc == 0) ? "success" :
3138 cm->cm_timeout_handler(sc, cm);
3139 if (sc->mps_flags & MPS_FLAGS_REALLOCATED) {
3141 * Tell the caller that we freed the command in a
3152 * The MPT driver had a verbose interface for config pages. In this driver,
3153 * reduce it to much simpler terms, similar to the Linux driver.
3156 mps_read_config_page(struct mps_softc *sc, struct mps_config_params *params)
3158 MPI2_CONFIG_REQUEST *req;
3159 struct mps_command *cm;
3162 if (sc->mps_flags & MPS_FLAGS_BUSY) {
3166 cm = mps_alloc_command(sc);
3171 req = (MPI2_CONFIG_REQUEST *)cm->cm_req;
3172 req->Function = MPI2_FUNCTION_CONFIG;
3173 req->Action = params->action;
3175 req->ChainOffset = 0;
3176 req->PageAddress = params->page_address;
3177 if (params->hdr.Struct.PageType == MPI2_CONFIG_PAGETYPE_EXTENDED) {
3178 MPI2_CONFIG_EXTENDED_PAGE_HEADER *hdr;
3180 hdr = ¶ms->hdr.Ext;
3181 req->ExtPageType = hdr->ExtPageType;
3182 req->ExtPageLength = hdr->ExtPageLength;
3183 req->Header.PageType = MPI2_CONFIG_PAGETYPE_EXTENDED;
3184 req->Header.PageLength = 0; /* Must be set to zero */
3185 req->Header.PageNumber = hdr->PageNumber;
3186 req->Header.PageVersion = hdr->PageVersion;
3188 MPI2_CONFIG_PAGE_HEADER *hdr;
3190 hdr = ¶ms->hdr.Struct;
3191 req->Header.PageType = hdr->PageType;
3192 req->Header.PageNumber = hdr->PageNumber;
3193 req->Header.PageLength = hdr->PageLength;
3194 req->Header.PageVersion = hdr->PageVersion;
3197 cm->cm_data = params->buffer;
3198 cm->cm_length = params->length;
3199 if (cm->cm_data != NULL) {
3200 cm->cm_sge = &req->PageBufferSGE;
3201 cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
3202 cm->cm_flags = MPS_CM_FLAGS_SGE_SIMPLE | MPS_CM_FLAGS_DATAIN;
3205 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3207 cm->cm_complete_data = params;
3208 if (params->callback != NULL) {
3209 cm->cm_complete = mps_config_complete;
3210 return (mps_map_command(sc, cm));
3212 error = mps_wait_command(sc, &cm, 0, CAN_SLEEP);
3214 mps_dprint(sc, MPS_FAULT,
3215 "Error %d reading config page\n", error);
3217 mps_free_command(sc, cm);
3220 mps_config_complete(sc, cm);
3227 mps_write_config_page(struct mps_softc *sc, struct mps_config_params *params)
3233 mps_config_complete(struct mps_softc *sc, struct mps_command *cm)
3235 MPI2_CONFIG_REPLY *reply;
3236 struct mps_config_params *params;
3239 params = cm->cm_complete_data;
3241 if (cm->cm_data != NULL) {
3242 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap,
3243 BUS_DMASYNC_POSTREAD);
3244 bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
3248 * XXX KDM need to do more error recovery? This results in the
3249 * device in question not getting probed.
3251 if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
3252 params->status = MPI2_IOCSTATUS_BUSY;
3256 reply = (MPI2_CONFIG_REPLY *)cm->cm_reply;
3257 if (reply == NULL) {
3258 params->status = MPI2_IOCSTATUS_BUSY;
3261 params->status = reply->IOCStatus;
3262 if (params->hdr.Struct.PageType == MPI2_CONFIG_PAGETYPE_EXTENDED) {
3263 params->hdr.Ext.ExtPageType = reply->ExtPageType;
3264 params->hdr.Ext.ExtPageLength = reply->ExtPageLength;
3265 params->hdr.Ext.PageType = reply->Header.PageType;
3266 params->hdr.Ext.PageNumber = reply->Header.PageNumber;
3267 params->hdr.Ext.PageVersion = reply->Header.PageVersion;
3269 params->hdr.Struct.PageType = reply->Header.PageType;
3270 params->hdr.Struct.PageNumber = reply->Header.PageNumber;
3271 params->hdr.Struct.PageLength = reply->Header.PageLength;
3272 params->hdr.Struct.PageVersion = reply->Header.PageVersion;
3276 mps_free_command(sc, cm);
3277 if (params->callback != NULL)
3278 params->callback(sc, params);