2 * Copyright (c) 2009 Yahoo! Inc.
3 * Copyright (c) 2011-2015 LSI Corp.
4 * Copyright (c) 2013-2015 Avago Technologies
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
28 * Avago Technologies (LSI) MPT-Fusion Host Adapter FreeBSD
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 /* Communications core for Avago Technologies (LSI) MPT2 */
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>
45 #include <sys/mutex.h>
46 #include <sys/module.h>
50 #include <sys/malloc.h>
52 #include <sys/sysctl.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>
61 #include <machine/bus.h>
62 #include <machine/resource.h>
66 #include <dev/pci/pcivar.h>
69 #include <cam/scsi/scsi_all.h>
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>
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);
109 SYSCTL_NODE(_hw, OID_AUTO, mps, CTLFLAG_RD, 0, "MPS Driver Parameters");
111 MALLOC_DEFINE(M_MPT2, "mps", "mpt2 driver memory");
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.
117 static char mpt2_reset_magic[] = { 0x00, 0x0f, 0x04, 0x0b, 0x02, 0x07, 0x0d };
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"
125 typedef union _reply_descriptor {
131 }reply_descriptor,address_descriptor;
133 /* Rate limit chain-fail messages to 1 per minute */
134 static struct timeval mps_chainfail_interval = { 60, 0 };
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.
147 mps_diag_reset(struct mps_softc *sc,int sleep_flag)
150 int i, error, tries = 0;
151 uint8_t first_wait_done = FALSE;
153 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
155 /* Clear any pending interrupts */
156 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
159 * Force NO_SLEEP for threads prohibited to sleep
160 * e.a Thread from interrupt handler are prohibited to sleep.
162 if (curthread->td_no_sleeping != 0)
163 sleep_flag = NO_SLEEP;
165 mps_dprint(sc, MPS_INIT, "sequence start, sleep_flag= %d\n", sleep_flag);
167 /* Push the magic sequence */
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]);
174 if (mtx_owned(&sc->mps_mtx) && sleep_flag == CAN_SLEEP)
175 msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0,
177 else if (sleep_flag == CAN_SLEEP)
178 pause("mpsdiag", hz/10);
182 reg = mps_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
183 if (reg & MPI2_DIAG_DIAG_WRITE_ENABLE) {
189 mps_dprint(sc, MPS_INIT, "sequence failed, error=%d, exit\n",
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",
198 mps_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET, reg);
200 /* Wait up to 300 seconds in 50ms intervals */
202 for (i = 0; i < 6000; i++) {
204 * Wait 50 msec. If this is the first time through, wait 256
205 * msec to satisfy Diag Reset timing requirements.
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,
211 else if (sleep_flag == CAN_SLEEP)
212 pause("mpsdiag", hz/20);
217 first_wait_done = TRUE;
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
224 reg = mps_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
225 if (reg & MPI2_DIAG_RESET_ADAPTER) {
228 reg = mps_regread(sc, MPI2_DOORBELL_OFFSET);
229 if ((reg & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_RESET) {
235 mps_dprint(sc, MPS_INIT, "reset failed, error= %d, exit\n",
240 mps_regwrite(sc, MPI2_WRITE_SEQUENCE_OFFSET, 0x0);
241 mps_dprint(sc, MPS_INIT, "diag reset success, exit\n");
247 mps_message_unit_reset(struct mps_softc *sc, int sleep_flag)
253 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
256 mps_regwrite(sc, MPI2_DOORBELL_OFFSET,
257 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET <<
258 MPI2_DOORBELL_FUNCTION_SHIFT);
260 if (mps_wait_db_ack(sc, 5, sleep_flag) != 0) {
261 mps_dprint(sc, MPS_INIT|MPS_FAULT,
262 "Doorbell handshake failed\n");
266 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__);
271 mps_transition_ready(struct mps_softc *sc)
274 int error, tries = 0;
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;
283 mps_dprint(sc, MPS_INIT, "%s entered, sleep_flags= %d\n",
284 __func__, sleep_flags);
286 while (tries++ < 1200) {
287 reg = mps_regread(sc, MPI2_DOORBELL_OFFSET);
288 mps_dprint(sc, MPS_INIT, " Doorbell= 0x%x\n", reg);
291 * Ensure the IOC is ready to talk. If it's not, try
294 if (reg & MPI2_DOORBELL_USED) {
295 mps_dprint(sc, MPS_INIT, " Not ready, sending diag "
297 mps_diag_reset(sc, sleep_flags);
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");
312 state = reg & MPI2_IOC_STATE_MASK;
313 if (state == MPI2_IOC_STATE_READY) {
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");
330 mps_dprint(sc, MPS_INIT|MPS_FAULT,
331 "IOC in unknown state 0x%x\n", state);
336 /* Wait 50ms for things to settle down. */
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__);
349 mps_transition_operational(struct mps_softc *sc)
357 reg = mps_regread(sc, MPI2_DOORBELL_OFFSET);
358 mps_dprint(sc, MPS_INIT, "%s entered, Doorbell= 0x%x\n", __func__, reg);
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");
370 error = mps_send_iocinit(sc);
371 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__);
377 mps_resize_queues(struct mps_softc *sc)
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.
387 prireqcr = MAX(1, sc->max_prireqframes);
388 prireqcr = MIN(prireqcr, sc->facts->HighPriorityCredit);
390 reqcr = MAX(2, sc->max_reqframes);
391 reqcr = MIN(reqcr, sc->facts->RequestCredit);
393 sc->num_reqs = prireqcr + reqcr;
394 sc->num_replies = MIN(sc->max_replyframes + sc->max_evtframes,
395 sc->facts->MaxReplyDescriptorPostQueueDepth) - 1;
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.
402 if (sc->facts->MaxMSIxVectors < 2)
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)
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);
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.
425 mps_iocfacts_allocate(struct mps_softc *sc, uint8_t attaching)
428 Mpi2IOCFactsReply_t saved_facts;
429 uint8_t saved_mode, reallocating;
431 mps_dprint(sc, MPS_INIT|MPS_TRACE, "%s entered\n", __func__);
433 /* Save old IOC Facts and then only reallocate if Facts have changed */
435 bcopy(sc->facts, &saved_facts, sizeof(MPI2_IOC_FACTS_REPLY));
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
443 if ((error = mps_get_iocfacts(sc, sc->facts)) != 0) {
445 mps_dprint(sc, MPS_INIT|MPS_FAULT, "Failed to get "
446 "IOC Facts with error %d, exit\n", error);
449 panic("%s failed to get IOC Facts with error %d\n",
454 MPS_DPRINT_PAGE(sc, MPS_XINFO, iocfacts, sc->facts);
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);
463 mps_dprint(sc, MPS_INFO, "Firmware: %s, Driver: %s\n", sc->fw_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");
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
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",
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.
495 saved_mode = sc->ir_firmware;
496 if (sc->facts->IOCCapabilities &
497 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID)
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");
506 /* Only deallocate and reallocate if relevant IOC Facts have changed */
507 reallocating = FALSE;
508 sc->mps_flags &= ~MPS_FLAGS_REALLOCATED;
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))) {
531 /* Record that we reallocated everything */
532 sc->mps_flags |= MPS_FLAGS_REALLOCATED;
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
540 if (attaching || reallocating) {
542 * Check if controller supports FW diag buffers and set flag to
545 if (sc->facts->IOCCapabilities &
546 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
547 sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_TRACE].
549 if (sc->facts->IOCCapabilities &
550 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
551 sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_SNAPSHOT].
553 if (sc->facts->IOCCapabilities &
554 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
555 sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_EXTENDED].
559 * Set flag if EEDP is supported and if TLR is supported.
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;
566 mps_resize_queues(sc);
569 * Initialize all Tail Queues
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);
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
584 mps_iocfacts_free(sc);
585 mpssas_realloc_targets(sc, saved_facts.MaxTargets +
586 saved_facts.MaxVolumes);
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.
596 while (attaching || reallocating) {
597 if ((error = mps_alloc_hw_queues(sc)) != 0)
599 if ((error = mps_alloc_replies(sc)) != 0)
601 if ((error = mps_alloc_requests(sc)) != 0)
603 if ((error = mps_alloc_queues(sc)) != 0)
609 mps_dprint(sc, MPS_INIT|MPS_FAULT,
610 "Failed to alloc queues with error %d\n", error);
615 /* Always initialize the queues */
616 bzero(sc->free_queue, sc->fqdepth * 4);
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
624 error = mps_transition_operational(sc);
626 mps_dprint(sc, MPS_INIT|MPS_FAULT, "Failed to "
627 "transition to operational with error %d\n", error);
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
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);
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
652 mps_dprint(sc, MPS_INIT, "Attaching subsystems\n");
653 if ((error = mps_attach_log(sc)) != 0)
655 if ((error = mps_attach_sas(sc)) != 0)
657 if ((error = mps_attach_user(sc)) != 0)
662 mps_dprint(sc, MPS_INIT|MPS_FAULT, "Failed to attach all "
663 "subsystems: error %d\n", error);
668 if ((error = mps_pci_setup_interrupts(sc)) != 0) {
669 mps_dprint(sc, MPS_INIT|MPS_FAULT, "Failed to setup "
676 * Set flag if this is a WD controller. This shouldn't ever change, but
677 * reset it after a Diag Reset, just in case.
679 sc->WD_available = FALSE;
680 if (pci_get_device(sc->mps_dev) == MPI2_MFGPAGE_DEVID_SSS6200)
681 sc->WD_available = TRUE;
687 * This is called if memory is being free (during detach for example) and when
688 * buffers need to be reallocated due to a Diag Reset.
691 mps_iocfacts_free(struct mps_softc *sc)
693 struct mps_command *cm;
696 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
698 if (sc->free_busaddr != 0)
699 bus_dmamap_unload(sc->queues_dmat, sc->queues_map);
700 if (sc->free_queue != NULL)
701 bus_dmamem_free(sc->queues_dmat, sc->free_queue,
703 if (sc->queues_dmat != NULL)
704 bus_dma_tag_destroy(sc->queues_dmat);
706 if (sc->chain_busaddr != 0)
707 bus_dmamap_unload(sc->chain_dmat, sc->chain_map);
708 if (sc->chain_frames != NULL)
709 bus_dmamem_free(sc->chain_dmat, sc->chain_frames,
711 if (sc->chain_dmat != NULL)
712 bus_dma_tag_destroy(sc->chain_dmat);
714 if (sc->sense_busaddr != 0)
715 bus_dmamap_unload(sc->sense_dmat, sc->sense_map);
716 if (sc->sense_frames != NULL)
717 bus_dmamem_free(sc->sense_dmat, sc->sense_frames,
719 if (sc->sense_dmat != NULL)
720 bus_dma_tag_destroy(sc->sense_dmat);
722 if (sc->reply_busaddr != 0)
723 bus_dmamap_unload(sc->reply_dmat, sc->reply_map);
724 if (sc->reply_frames != NULL)
725 bus_dmamem_free(sc->reply_dmat, sc->reply_frames,
727 if (sc->reply_dmat != NULL)
728 bus_dma_tag_destroy(sc->reply_dmat);
730 if (sc->req_busaddr != 0)
731 bus_dmamap_unload(sc->req_dmat, sc->req_map);
732 if (sc->req_frames != NULL)
733 bus_dmamem_free(sc->req_dmat, sc->req_frames, sc->req_map);
734 if (sc->req_dmat != NULL)
735 bus_dma_tag_destroy(sc->req_dmat);
737 if (sc->chains != NULL)
738 free(sc->chains, M_MPT2);
739 if (sc->commands != NULL) {
740 for (i = 1; i < sc->num_reqs; i++) {
741 cm = &sc->commands[i];
742 bus_dmamap_destroy(sc->buffer_dmat, cm->cm_dmamap);
744 free(sc->commands, M_MPT2);
746 if (sc->buffer_dmat != NULL)
747 bus_dma_tag_destroy(sc->buffer_dmat);
749 mps_pci_free_interrupts(sc);
750 free(sc->queues, M_MPT2);
755 * The terms diag reset and hard reset are used interchangeably in the MPI
756 * docs to mean resetting the controller chip. In this code diag reset
757 * cleans everything up, and the hard reset function just sends the reset
758 * sequence to the chip. This should probably be refactored so that every
759 * subsystem gets a reset notification of some sort, and can clean up
763 mps_reinit(struct mps_softc *sc)
766 struct mpssas_softc *sassc;
772 mtx_assert(&sc->mps_mtx, MA_OWNED);
774 mps_dprint(sc, MPS_INIT|MPS_INFO, "Reinitializing controller\n");
775 if (sc->mps_flags & MPS_FLAGS_DIAGRESET) {
776 mps_dprint(sc, MPS_INIT, "Reset already in progress\n");
780 /* make sure the completion callbacks can recognize they're getting
781 * a NULL cm_reply due to a reset.
783 sc->mps_flags |= MPS_FLAGS_DIAGRESET;
786 * Mask interrupts here.
788 mps_dprint(sc, MPS_INIT, "masking interrupts and resetting\n");
791 error = mps_diag_reset(sc, CAN_SLEEP);
793 /* XXXSL No need to panic here */
794 panic("%s hard reset failed with error %d\n",
798 /* Restore the PCI state, including the MSI-X registers */
801 /* Give the I/O subsystem special priority to get itself prepared */
802 mpssas_handle_reinit(sc);
805 * Get IOC Facts and allocate all structures based on this information.
806 * The attach function will also call mps_iocfacts_allocate at startup.
807 * If relevant values have changed in IOC Facts, this function will free
808 * all of the memory based on IOC Facts and reallocate that memory.
810 if ((error = mps_iocfacts_allocate(sc, FALSE)) != 0) {
811 panic("%s IOC Facts based allocation failed with error %d\n",
816 * Mapping structures will be re-allocated after getting IOC Page8, so
817 * free these structures here.
819 mps_mapping_exit(sc);
822 * The static page function currently read is IOC Page8. Others can be
823 * added in future. It's possible that the values in IOC Page8 have
824 * changed after a Diag Reset due to user modification, so always read
825 * these. Interrupts are masked, so unmask them before getting config
829 sc->mps_flags &= ~MPS_FLAGS_DIAGRESET;
830 mps_base_static_config_pages(sc);
833 * Some mapping info is based in IOC Page8 data, so re-initialize the
836 mps_mapping_initialize(sc);
839 * Restart will reload the event masks clobbered by the reset, and
840 * then enable the port.
842 mps_reregister_events(sc);
844 /* the end of discovery will release the simq, so we're done. */
845 mps_dprint(sc, MPS_INIT|MPS_XINFO, "Finished sc %p post %u free %u\n",
846 sc, sc->replypostindex, sc->replyfreeindex);
848 mpssas_release_simq_reinit(sassc);
849 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__);
854 /* Wait for the chip to ACK a word that we've put into its FIFO
855 * Wait for <timeout> seconds. In single loop wait for busy loop
856 * for 500 microseconds.
857 * Total is [ 0.5 * (2000 * <timeout>) ] in miliseconds.
860 mps_wait_db_ack(struct mps_softc *sc, int timeout, int sleep_flag)
868 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
870 int_status = mps_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET);
871 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
872 mps_dprint(sc, MPS_TRACE,
873 "%s: successful count(%d), timeout(%d)\n",
874 __func__, count, timeout);
876 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
877 doorbell = mps_regread(sc, MPI2_DOORBELL_OFFSET);
878 if ((doorbell & MPI2_IOC_STATE_MASK) ==
879 MPI2_IOC_STATE_FAULT) {
880 mps_dprint(sc, MPS_FAULT,
881 "fault_state(0x%04x)!\n", doorbell);
884 } else if (int_status == 0xFFFFFFFF)
887 /* If it can sleep, sleep for 1 milisecond, else busy loop for
889 if (mtx_owned(&sc->mps_mtx) && sleep_flag == CAN_SLEEP)
890 msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0,
892 else if (sleep_flag == CAN_SLEEP)
893 pause("mpsdba", hz/1000);
900 mps_dprint(sc, MPS_FAULT, "%s: failed due to timeout count(%d), "
901 "int_status(%x)!\n", __func__, count, int_status);
906 /* Wait for the chip to signal that the next word in its FIFO can be fetched */
908 mps_wait_db_int(struct mps_softc *sc)
912 for (retry = 0; retry < MPS_DB_MAX_WAIT; retry++) {
913 if ((mps_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET) &
914 MPI2_HIS_IOC2SYS_DB_STATUS) != 0)
921 /* Step through the synchronous command state machine, i.e. "Doorbell mode" */
923 mps_request_sync(struct mps_softc *sc, void *req, MPI2_DEFAULT_REPLY *reply,
924 int req_sz, int reply_sz, int timeout)
928 int i, count, ioc_sz, residual;
929 int sleep_flags = CAN_SLEEP;
931 if (curthread->td_no_sleeping != 0)
932 sleep_flags = NO_SLEEP;
935 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
938 if (mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED)
942 * Announce that a message is coming through the doorbell. Messages
943 * are pushed at 32bit words, so round up if needed.
945 count = (req_sz + 3) / 4;
946 mps_regwrite(sc, MPI2_DOORBELL_OFFSET,
947 (MPI2_FUNCTION_HANDSHAKE << MPI2_DOORBELL_FUNCTION_SHIFT) |
948 (count << MPI2_DOORBELL_ADD_DWORDS_SHIFT));
951 if (mps_wait_db_int(sc) ||
952 (mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED) == 0) {
953 mps_dprint(sc, MPS_FAULT, "Doorbell failed to activate\n");
956 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
957 if (mps_wait_db_ack(sc, 5, sleep_flags) != 0) {
958 mps_dprint(sc, MPS_FAULT, "Doorbell handshake failed\n");
963 /* Clock out the message data synchronously in 32-bit dwords*/
964 data32 = (uint32_t *)req;
965 for (i = 0; i < count; i++) {
966 mps_regwrite(sc, MPI2_DOORBELL_OFFSET, htole32(data32[i]));
967 if (mps_wait_db_ack(sc, 5, sleep_flags) != 0) {
968 mps_dprint(sc, MPS_FAULT,
969 "Timeout while writing doorbell\n");
975 /* Clock in the reply in 16-bit words. The total length of the
976 * message is always in the 4th byte, so clock out the first 2 words
977 * manually, then loop the rest.
979 data16 = (uint16_t *)reply;
980 if (mps_wait_db_int(sc) != 0) {
981 mps_dprint(sc, MPS_FAULT, "Timeout reading doorbell 0\n");
985 mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_DATA_MASK;
986 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
987 if (mps_wait_db_int(sc) != 0) {
988 mps_dprint(sc, MPS_FAULT, "Timeout reading doorbell 1\n");
992 mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_DATA_MASK;
993 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
995 /* Number of 32bit words in the message */
996 ioc_sz = reply->MsgLength;
999 * Figure out how many 16bit words to clock in without overrunning.
1000 * The precision loss with dividing reply_sz can safely be
1001 * ignored because the messages can only be multiples of 32bits.
1004 count = MIN((reply_sz / 4), ioc_sz) * 2;
1005 if (count < ioc_sz * 2) {
1006 residual = ioc_sz * 2 - count;
1007 mps_dprint(sc, MPS_ERROR, "Driver error, throwing away %d "
1008 "residual message words\n", residual);
1011 for (i = 2; i < count; i++) {
1012 if (mps_wait_db_int(sc) != 0) {
1013 mps_dprint(sc, MPS_FAULT,
1014 "Timeout reading doorbell %d\n", i);
1017 data16[i] = mps_regread(sc, MPI2_DOORBELL_OFFSET) &
1018 MPI2_DOORBELL_DATA_MASK;
1019 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1023 * Pull out residual words that won't fit into the provided buffer.
1024 * This keeps the chip from hanging due to a driver programming
1027 while (residual--) {
1028 if (mps_wait_db_int(sc) != 0) {
1029 mps_dprint(sc, MPS_FAULT,
1030 "Timeout reading doorbell\n");
1033 (void)mps_regread(sc, MPI2_DOORBELL_OFFSET);
1034 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1038 if (mps_wait_db_int(sc) != 0) {
1039 mps_dprint(sc, MPS_FAULT, "Timeout waiting to exit doorbell\n");
1042 if (mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED)
1043 mps_dprint(sc, MPS_FAULT, "Warning, doorbell still active\n");
1044 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1050 mps_enqueue_request(struct mps_softc *sc, struct mps_command *cm)
1052 reply_descriptor rd;
1054 mps_dprint(sc, MPS_TRACE, "SMID %u cm %p ccb %p\n",
1055 cm->cm_desc.Default.SMID, cm, cm->cm_ccb);
1057 if (sc->mps_flags & MPS_FLAGS_ATTACH_DONE && !(sc->mps_flags & MPS_FLAGS_SHUTDOWN))
1058 mtx_assert(&sc->mps_mtx, MA_OWNED);
1060 if (++sc->io_cmds_active > sc->io_cmds_highwater)
1061 sc->io_cmds_highwater++;
1062 rd.u.low = cm->cm_desc.Words.Low;
1063 rd.u.high = cm->cm_desc.Words.High;
1064 rd.word = htole64(rd.word);
1065 /* TODO-We may need to make below regwrite atomic */
1066 mps_regwrite(sc, MPI2_REQUEST_DESCRIPTOR_POST_LOW_OFFSET,
1068 mps_regwrite(sc, MPI2_REQUEST_DESCRIPTOR_POST_HIGH_OFFSET,
1073 * Just the FACTS, ma'am.
1076 mps_get_iocfacts(struct mps_softc *sc, MPI2_IOC_FACTS_REPLY *facts)
1078 MPI2_DEFAULT_REPLY *reply;
1079 MPI2_IOC_FACTS_REQUEST request;
1080 int error, req_sz, reply_sz;
1083 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
1085 req_sz = sizeof(MPI2_IOC_FACTS_REQUEST);
1086 reply_sz = sizeof(MPI2_IOC_FACTS_REPLY);
1087 reply = (MPI2_DEFAULT_REPLY *)facts;
1089 bzero(&request, req_sz);
1090 request.Function = MPI2_FUNCTION_IOC_FACTS;
1091 error = mps_request_sync(sc, &request, reply, req_sz, reply_sz, 5);
1092 mps_dprint(sc, MPS_INIT, "%s exit error= %d\n", __func__, error);
1098 mps_send_iocinit(struct mps_softc *sc)
1100 MPI2_IOC_INIT_REQUEST init;
1101 MPI2_DEFAULT_REPLY reply;
1102 int req_sz, reply_sz, error;
1104 uint64_t time_in_msec;
1107 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
1109 req_sz = sizeof(MPI2_IOC_INIT_REQUEST);
1110 reply_sz = sizeof(MPI2_IOC_INIT_REPLY);
1111 bzero(&init, req_sz);
1112 bzero(&reply, reply_sz);
1115 * Fill in the init block. Note that most addresses are
1116 * deliberately in the lower 32bits of memory. This is a micro-
1117 * optimzation for PCI/PCIX, though it's not clear if it helps PCIe.
1119 init.Function = MPI2_FUNCTION_IOC_INIT;
1120 init.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
1121 init.MsgVersion = htole16(MPI2_VERSION);
1122 init.HeaderVersion = htole16(MPI2_HEADER_VERSION);
1123 init.SystemRequestFrameSize = htole16(sc->facts->IOCRequestFrameSize);
1124 init.ReplyDescriptorPostQueueDepth = htole16(sc->pqdepth);
1125 init.ReplyFreeQueueDepth = htole16(sc->fqdepth);
1126 init.SenseBufferAddressHigh = 0;
1127 init.SystemReplyAddressHigh = 0;
1128 init.SystemRequestFrameBaseAddress.High = 0;
1129 init.SystemRequestFrameBaseAddress.Low = htole32((uint32_t)sc->req_busaddr);
1130 init.ReplyDescriptorPostQueueAddress.High = 0;
1131 init.ReplyDescriptorPostQueueAddress.Low = htole32((uint32_t)sc->post_busaddr);
1132 init.ReplyFreeQueueAddress.High = 0;
1133 init.ReplyFreeQueueAddress.Low = htole32((uint32_t)sc->free_busaddr);
1135 time_in_msec = (now.tv_sec * 1000 + now.tv_usec/1000);
1136 init.TimeStamp.High = htole32((time_in_msec >> 32) & 0xFFFFFFFF);
1137 init.TimeStamp.Low = htole32(time_in_msec & 0xFFFFFFFF);
1139 error = mps_request_sync(sc, &init, &reply, req_sz, reply_sz, 5);
1140 if ((reply.IOCStatus & MPI2_IOCSTATUS_MASK) != MPI2_IOCSTATUS_SUCCESS)
1143 mps_dprint(sc, MPS_INIT, "IOCInit status= 0x%x\n", reply.IOCStatus);
1144 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__);
1149 mps_memaddr_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1154 *addr = segs[0].ds_addr;
1158 mps_alloc_queues(struct mps_softc *sc)
1160 struct mps_queue *q;
1164 mps_dprint(sc, MPS_INIT|MPS_XINFO, "Allocating %d I/O queues\n", nq);
1166 sc->queues = malloc(sizeof(struct mps_queue) * nq, M_MPT2,
1168 if (sc->queues == NULL)
1171 for (i = 0; i < nq; i++) {
1173 mps_dprint(sc, MPS_INIT, "Configuring queue %d %p\n", i, q);
1182 mps_alloc_hw_queues(struct mps_softc *sc)
1184 bus_addr_t queues_busaddr;
1186 int qsize, fqsize, pqsize;
1189 * The reply free queue contains 4 byte entries in multiples of 16 and
1190 * aligned on a 16 byte boundary. There must always be an unused entry.
1191 * This queue supplies fresh reply frames for the firmware to use.
1193 * The reply descriptor post queue contains 8 byte entries in
1194 * multiples of 16 and aligned on a 16 byte boundary. This queue
1195 * contains filled-in reply frames sent from the firmware to the host.
1197 * These two queues are allocated together for simplicity.
1199 sc->fqdepth = roundup2(sc->num_replies + 1, 16);
1200 sc->pqdepth = roundup2(sc->num_replies + 1, 16);
1201 fqsize= sc->fqdepth * 4;
1202 pqsize = sc->pqdepth * 8;
1203 qsize = fqsize + pqsize;
1205 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
1206 16, 0, /* algnmnt, boundary */
1207 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1208 BUS_SPACE_MAXADDR, /* highaddr */
1209 NULL, NULL, /* filter, filterarg */
1210 qsize, /* maxsize */
1212 qsize, /* maxsegsize */
1214 NULL, NULL, /* lockfunc, lockarg */
1215 &sc->queues_dmat)) {
1216 mps_dprint(sc, MPS_ERROR, "Cannot allocate queues DMA tag\n");
1219 if (bus_dmamem_alloc(sc->queues_dmat, (void **)&queues, BUS_DMA_NOWAIT,
1221 mps_dprint(sc, MPS_ERROR, "Cannot allocate queues memory\n");
1224 bzero(queues, qsize);
1225 bus_dmamap_load(sc->queues_dmat, sc->queues_map, queues, qsize,
1226 mps_memaddr_cb, &queues_busaddr, 0);
1228 sc->free_queue = (uint32_t *)queues;
1229 sc->free_busaddr = queues_busaddr;
1230 sc->post_queue = (MPI2_REPLY_DESCRIPTORS_UNION *)(queues + fqsize);
1231 sc->post_busaddr = queues_busaddr + fqsize;
1237 mps_alloc_replies(struct mps_softc *sc)
1239 int rsize, num_replies;
1242 * sc->num_replies should be one less than sc->fqdepth. We need to
1243 * allocate space for sc->fqdepth replies, but only sc->num_replies
1244 * replies can be used at once.
1246 num_replies = max(sc->fqdepth, sc->num_replies);
1248 rsize = sc->facts->ReplyFrameSize * num_replies * 4;
1249 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
1250 4, 0, /* algnmnt, boundary */
1251 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1252 BUS_SPACE_MAXADDR, /* highaddr */
1253 NULL, NULL, /* filter, filterarg */
1254 rsize, /* maxsize */
1256 rsize, /* maxsegsize */
1258 NULL, NULL, /* lockfunc, lockarg */
1260 mps_dprint(sc, MPS_ERROR, "Cannot allocate replies DMA tag\n");
1263 if (bus_dmamem_alloc(sc->reply_dmat, (void **)&sc->reply_frames,
1264 BUS_DMA_NOWAIT, &sc->reply_map)) {
1265 mps_dprint(sc, MPS_ERROR, "Cannot allocate replies memory\n");
1268 bzero(sc->reply_frames, rsize);
1269 bus_dmamap_load(sc->reply_dmat, sc->reply_map, sc->reply_frames, rsize,
1270 mps_memaddr_cb, &sc->reply_busaddr, 0);
1276 mps_alloc_requests(struct mps_softc *sc)
1278 struct mps_command *cm;
1279 struct mps_chain *chain;
1280 int i, rsize, nsegs;
1282 rsize = sc->facts->IOCRequestFrameSize * sc->num_reqs * 4;
1283 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
1284 16, 0, /* algnmnt, boundary */
1285 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1286 BUS_SPACE_MAXADDR, /* highaddr */
1287 NULL, NULL, /* filter, filterarg */
1288 rsize, /* maxsize */
1290 rsize, /* maxsegsize */
1292 NULL, NULL, /* lockfunc, lockarg */
1294 mps_dprint(sc, MPS_ERROR, "Cannot allocate request DMA tag\n");
1297 if (bus_dmamem_alloc(sc->req_dmat, (void **)&sc->req_frames,
1298 BUS_DMA_NOWAIT, &sc->req_map)) {
1299 mps_dprint(sc, MPS_ERROR, "Cannot allocate request memory\n");
1302 bzero(sc->req_frames, rsize);
1303 bus_dmamap_load(sc->req_dmat, sc->req_map, sc->req_frames, rsize,
1304 mps_memaddr_cb, &sc->req_busaddr, 0);
1306 rsize = sc->facts->IOCRequestFrameSize * sc->max_chains * 4;
1307 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
1308 16, 0, /* algnmnt, boundary */
1309 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1310 BUS_SPACE_MAXADDR, /* highaddr */
1311 NULL, NULL, /* filter, filterarg */
1312 rsize, /* maxsize */
1314 rsize, /* maxsegsize */
1316 NULL, NULL, /* lockfunc, lockarg */
1318 mps_dprint(sc, MPS_ERROR, "Cannot allocate chain DMA tag\n");
1321 if (bus_dmamem_alloc(sc->chain_dmat, (void **)&sc->chain_frames,
1322 BUS_DMA_NOWAIT, &sc->chain_map)) {
1323 mps_dprint(sc, MPS_ERROR, "Cannot allocate chain memory\n");
1326 bzero(sc->chain_frames, rsize);
1327 bus_dmamap_load(sc->chain_dmat, sc->chain_map, sc->chain_frames, rsize,
1328 mps_memaddr_cb, &sc->chain_busaddr, 0);
1330 rsize = MPS_SENSE_LEN * sc->num_reqs;
1331 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
1332 1, 0, /* algnmnt, boundary */
1333 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1334 BUS_SPACE_MAXADDR, /* highaddr */
1335 NULL, NULL, /* filter, filterarg */
1336 rsize, /* maxsize */
1338 rsize, /* maxsegsize */
1340 NULL, NULL, /* lockfunc, lockarg */
1342 mps_dprint(sc, MPS_ERROR, "Cannot allocate sense DMA tag\n");
1345 if (bus_dmamem_alloc(sc->sense_dmat, (void **)&sc->sense_frames,
1346 BUS_DMA_NOWAIT, &sc->sense_map)) {
1347 mps_dprint(sc, MPS_ERROR, "Cannot allocate sense memory\n");
1350 bzero(sc->sense_frames, rsize);
1351 bus_dmamap_load(sc->sense_dmat, sc->sense_map, sc->sense_frames, rsize,
1352 mps_memaddr_cb, &sc->sense_busaddr, 0);
1354 sc->chains = malloc(sizeof(struct mps_chain) * sc->max_chains, M_MPT2,
1357 mps_dprint(sc, MPS_ERROR, "Cannot allocate chains memory\n");
1360 for (i = 0; i < sc->max_chains; i++) {
1361 chain = &sc->chains[i];
1362 chain->chain = (MPI2_SGE_IO_UNION *)(sc->chain_frames +
1363 i * sc->facts->IOCRequestFrameSize * 4);
1364 chain->chain_busaddr = sc->chain_busaddr +
1365 i * sc->facts->IOCRequestFrameSize * 4;
1366 mps_free_chain(sc, chain);
1367 sc->chain_free_lowwater++;
1370 /* XXX Need to pick a more precise value */
1371 nsegs = (MAXPHYS / PAGE_SIZE) + 1;
1372 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
1373 1, 0, /* algnmnt, boundary */
1374 BUS_SPACE_MAXADDR, /* lowaddr */
1375 BUS_SPACE_MAXADDR, /* highaddr */
1376 NULL, NULL, /* filter, filterarg */
1377 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
1378 nsegs, /* nsegments */
1379 BUS_SPACE_MAXSIZE_24BIT,/* maxsegsize */
1380 BUS_DMA_ALLOCNOW, /* flags */
1381 busdma_lock_mutex, /* lockfunc */
1382 &sc->mps_mtx, /* lockarg */
1383 &sc->buffer_dmat)) {
1384 mps_dprint(sc, MPS_ERROR, "Cannot allocate buffer DMA tag\n");
1389 * SMID 0 cannot be used as a free command per the firmware spec.
1390 * Just drop that command instead of risking accounting bugs.
1392 sc->commands = malloc(sizeof(struct mps_command) * sc->num_reqs,
1393 M_MPT2, M_WAITOK | M_ZERO);
1395 mps_dprint(sc, MPS_ERROR, "Cannot allocate command memory\n");
1398 for (i = 1; i < sc->num_reqs; i++) {
1399 cm = &sc->commands[i];
1400 cm->cm_req = sc->req_frames +
1401 i * sc->facts->IOCRequestFrameSize * 4;
1402 cm->cm_req_busaddr = sc->req_busaddr +
1403 i * sc->facts->IOCRequestFrameSize * 4;
1404 cm->cm_sense = &sc->sense_frames[i];
1405 cm->cm_sense_busaddr = sc->sense_busaddr + i * MPS_SENSE_LEN;
1406 cm->cm_desc.Default.SMID = i;
1408 TAILQ_INIT(&cm->cm_chain_list);
1409 callout_init_mtx(&cm->cm_callout, &sc->mps_mtx, 0);
1411 /* XXX Is a failure here a critical problem? */
1412 if (bus_dmamap_create(sc->buffer_dmat, 0, &cm->cm_dmamap) == 0)
1413 if (i <= sc->facts->HighPriorityCredit)
1414 mps_free_high_priority_command(sc, cm);
1416 mps_free_command(sc, cm);
1418 panic("failed to allocate command %d\n", i);
1428 mps_init_queues(struct mps_softc *sc)
1432 memset((uint8_t *)sc->post_queue, 0xff, sc->pqdepth * 8);
1435 * According to the spec, we need to use one less reply than we
1436 * have space for on the queue. So sc->num_replies (the number we
1437 * use) should be less than sc->fqdepth (allocated size).
1439 if (sc->num_replies >= sc->fqdepth)
1443 * Initialize all of the free queue entries.
1445 for (i = 0; i < sc->fqdepth; i++)
1446 sc->free_queue[i] = sc->reply_busaddr + (i * sc->facts->ReplyFrameSize * 4);
1447 sc->replyfreeindex = sc->num_replies;
1452 /* Get the driver parameter tunables. Lowest priority are the driver defaults.
1453 * Next are the global settings, if they exist. Highest are the per-unit
1454 * settings, if they exist.
1457 mps_get_tunables(struct mps_softc *sc)
1459 char tmpstr[80], mps_debug[80];
1461 /* XXX default to some debugging for now */
1462 sc->mps_debug = MPS_INFO|MPS_FAULT;
1463 sc->disable_msix = 0;
1464 sc->disable_msi = 0;
1465 sc->max_msix = MPS_MSIX_MAX;
1466 sc->max_chains = MPS_CHAIN_FRAMES;
1467 sc->max_io_pages = MPS_MAXIO_PAGES;
1468 sc->enable_ssu = MPS_SSU_ENABLE_SSD_DISABLE_HDD;
1469 sc->spinup_wait_time = DEFAULT_SPINUP_WAIT;
1471 sc->max_reqframes = MPS_REQ_FRAMES;
1472 sc->max_prireqframes = MPS_PRI_REQ_FRAMES;
1473 sc->max_replyframes = MPS_REPLY_FRAMES;
1474 sc->max_evtframes = MPS_EVT_REPLY_FRAMES;
1477 * Grab the global variables.
1479 bzero(mps_debug, 80);
1480 if (TUNABLE_STR_FETCH("hw.mps.debug_level", mps_debug, 80) != 0)
1481 mps_parse_debug(sc, mps_debug);
1482 TUNABLE_INT_FETCH("hw.mps.disable_msix", &sc->disable_msix);
1483 TUNABLE_INT_FETCH("hw.mps.disable_msi", &sc->disable_msi);
1484 TUNABLE_INT_FETCH("hw.mps.max_msix", &sc->max_msix);
1485 TUNABLE_INT_FETCH("hw.mps.max_chains", &sc->max_chains);
1486 TUNABLE_INT_FETCH("hw.mps.max_io_pages", &sc->max_io_pages);
1487 TUNABLE_INT_FETCH("hw.mps.enable_ssu", &sc->enable_ssu);
1488 TUNABLE_INT_FETCH("hw.mps.spinup_wait_time", &sc->spinup_wait_time);
1489 TUNABLE_INT_FETCH("hw.mps.use_phy_num", &sc->use_phynum);
1490 TUNABLE_INT_FETCH("hw.mps.max_reqframes", &sc->max_reqframes);
1491 TUNABLE_INT_FETCH("hw.mps.max_prireqframes", &sc->max_prireqframes);
1492 TUNABLE_INT_FETCH("hw.mps.max_replyframes", &sc->max_replyframes);
1493 TUNABLE_INT_FETCH("hw.mps.max_evtframes", &sc->max_evtframes);
1495 /* Grab the unit-instance variables */
1496 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.debug_level",
1497 device_get_unit(sc->mps_dev));
1498 bzero(mps_debug, 80);
1499 if (TUNABLE_STR_FETCH(tmpstr, mps_debug, 80) != 0)
1500 mps_parse_debug(sc, mps_debug);
1502 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.disable_msix",
1503 device_get_unit(sc->mps_dev));
1504 TUNABLE_INT_FETCH(tmpstr, &sc->disable_msix);
1506 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.disable_msi",
1507 device_get_unit(sc->mps_dev));
1508 TUNABLE_INT_FETCH(tmpstr, &sc->disable_msi);
1510 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_msix",
1511 device_get_unit(sc->mps_dev));
1512 TUNABLE_INT_FETCH(tmpstr, &sc->max_msix);
1514 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_chains",
1515 device_get_unit(sc->mps_dev));
1516 TUNABLE_INT_FETCH(tmpstr, &sc->max_chains);
1518 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_io_pages",
1519 device_get_unit(sc->mps_dev));
1520 TUNABLE_INT_FETCH(tmpstr, &sc->max_io_pages);
1522 bzero(sc->exclude_ids, sizeof(sc->exclude_ids));
1523 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.exclude_ids",
1524 device_get_unit(sc->mps_dev));
1525 TUNABLE_STR_FETCH(tmpstr, sc->exclude_ids, sizeof(sc->exclude_ids));
1527 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.enable_ssu",
1528 device_get_unit(sc->mps_dev));
1529 TUNABLE_INT_FETCH(tmpstr, &sc->enable_ssu);
1531 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.spinup_wait_time",
1532 device_get_unit(sc->mps_dev));
1533 TUNABLE_INT_FETCH(tmpstr, &sc->spinup_wait_time);
1535 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.use_phy_num",
1536 device_get_unit(sc->mps_dev));
1537 TUNABLE_INT_FETCH(tmpstr, &sc->use_phynum);
1539 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_reqframes",
1540 device_get_unit(sc->mps_dev));
1541 TUNABLE_INT_FETCH(tmpstr, &sc->max_reqframes);
1543 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_prireqframes",
1544 device_get_unit(sc->mps_dev));
1545 TUNABLE_INT_FETCH(tmpstr, &sc->max_prireqframes);
1547 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_replyframes",
1548 device_get_unit(sc->mps_dev));
1549 TUNABLE_INT_FETCH(tmpstr, &sc->max_replyframes);
1551 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_evtframes",
1552 device_get_unit(sc->mps_dev));
1553 TUNABLE_INT_FETCH(tmpstr, &sc->max_evtframes);
1558 mps_setup_sysctl(struct mps_softc *sc)
1560 struct sysctl_ctx_list *sysctl_ctx = NULL;
1561 struct sysctl_oid *sysctl_tree = NULL;
1562 char tmpstr[80], tmpstr2[80];
1565 * Setup the sysctl variable so the user can change the debug level
1568 snprintf(tmpstr, sizeof(tmpstr), "MPS controller %d",
1569 device_get_unit(sc->mps_dev));
1570 snprintf(tmpstr2, sizeof(tmpstr2), "%d", device_get_unit(sc->mps_dev));
1572 sysctl_ctx = device_get_sysctl_ctx(sc->mps_dev);
1573 if (sysctl_ctx != NULL)
1574 sysctl_tree = device_get_sysctl_tree(sc->mps_dev);
1576 if (sysctl_tree == NULL) {
1577 sysctl_ctx_init(&sc->sysctl_ctx);
1578 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
1579 SYSCTL_STATIC_CHILDREN(_hw_mps), OID_AUTO, tmpstr2,
1580 CTLFLAG_RD, 0, tmpstr);
1581 if (sc->sysctl_tree == NULL)
1583 sysctl_ctx = &sc->sysctl_ctx;
1584 sysctl_tree = sc->sysctl_tree;
1587 SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1588 OID_AUTO, "debug_level", CTLTYPE_STRING | CTLFLAG_RW |CTLFLAG_MPSAFE,
1589 sc, 0, mps_debug_sysctl, "A", "mps debug level");
1591 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1592 OID_AUTO, "disable_msix", CTLFLAG_RD, &sc->disable_msix, 0,
1593 "Disable the use of MSI-X interrupts");
1595 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1596 OID_AUTO, "disable_msi", CTLFLAG_RD, &sc->disable_msi, 0,
1597 "Disable the use of MSI interrupts");
1599 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1600 OID_AUTO, "max_msix", CTLFLAG_RD, &sc->max_msix, 0,
1601 "User-defined maximum number of MSIX queues");
1603 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1604 OID_AUTO, "msix_msgs", CTLFLAG_RD, &sc->msi_msgs, 0,
1605 "Negotiated number of MSIX queues");
1607 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1608 OID_AUTO, "max_reqframes", CTLFLAG_RD, &sc->max_reqframes, 0,
1609 "Total number of allocated request frames");
1611 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1612 OID_AUTO, "max_prireqframes", CTLFLAG_RD, &sc->max_prireqframes, 0,
1613 "Total number of allocated high priority request frames");
1615 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1616 OID_AUTO, "max_replyframes", CTLFLAG_RD, &sc->max_replyframes, 0,
1617 "Total number of allocated reply frames");
1619 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1620 OID_AUTO, "max_evtframes", CTLFLAG_RD, &sc->max_evtframes, 0,
1621 "Total number of event frames allocated");
1623 SYSCTL_ADD_STRING(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1624 OID_AUTO, "firmware_version", CTLFLAG_RW, sc->fw_version,
1625 strlen(sc->fw_version), "firmware version");
1627 SYSCTL_ADD_STRING(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1628 OID_AUTO, "driver_version", CTLFLAG_RW, MPS_DRIVER_VERSION,
1629 strlen(MPS_DRIVER_VERSION), "driver version");
1631 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1632 OID_AUTO, "io_cmds_active", CTLFLAG_RD,
1633 &sc->io_cmds_active, 0, "number of currently active commands");
1635 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1636 OID_AUTO, "io_cmds_highwater", CTLFLAG_RD,
1637 &sc->io_cmds_highwater, 0, "maximum active commands seen");
1639 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1640 OID_AUTO, "chain_free", CTLFLAG_RD,
1641 &sc->chain_free, 0, "number of free chain elements");
1643 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1644 OID_AUTO, "chain_free_lowwater", CTLFLAG_RD,
1645 &sc->chain_free_lowwater, 0,"lowest number of free chain elements");
1647 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1648 OID_AUTO, "max_chains", CTLFLAG_RD,
1649 &sc->max_chains, 0,"maximum chain frames that will be allocated");
1651 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1652 OID_AUTO, "max_io_pages", CTLFLAG_RD,
1653 &sc->max_io_pages, 0,"maximum pages to allow per I/O (if <1 use "
1656 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1657 OID_AUTO, "enable_ssu", CTLFLAG_RW, &sc->enable_ssu, 0,
1658 "enable SSU to SATA SSD/HDD at shutdown");
1660 SYSCTL_ADD_UQUAD(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1661 OID_AUTO, "chain_alloc_fail", CTLFLAG_RD,
1662 &sc->chain_alloc_fail, "chain allocation failures");
1664 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1665 OID_AUTO, "spinup_wait_time", CTLFLAG_RD,
1666 &sc->spinup_wait_time, DEFAULT_SPINUP_WAIT, "seconds to wait for "
1667 "spinup after SATA ID error");
1669 SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1670 OID_AUTO, "mapping_table_dump", CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
1671 mps_mapping_dump, "A", "Mapping Table Dump");
1673 SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1674 OID_AUTO, "encl_table_dump", CTLTYPE_STRING | CTLFLAG_RD, sc, 0,
1675 mps_mapping_encl_dump, "A", "Enclosure Table Dump");
1677 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1678 OID_AUTO, "use_phy_num", CTLFLAG_RD, &sc->use_phynum, 0,
1679 "Use the phy number for enumeration");
1682 static struct mps_debug_string {
1685 } mps_debug_strings[] = {
1687 {"fault", MPS_FAULT},
1688 {"event", MPS_EVENT},
1690 {"recovery", MPS_RECOVERY},
1691 {"error", MPS_ERROR},
1693 {"xinfo", MPS_XINFO},
1695 {"mapping", MPS_MAPPING},
1696 {"trace", MPS_TRACE}
1699 enum mps_debug_level_combiner {
1706 mps_debug_sysctl(SYSCTL_HANDLER_ARGS)
1708 struct mps_softc *sc;
1709 struct mps_debug_string *string;
1713 int i, len, debug, error;
1715 sc = (struct mps_softc *)arg1;
1717 error = sysctl_wire_old_buffer(req, 0);
1721 sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
1722 debug = sc->mps_debug;
1724 sbuf_printf(sbuf, "%#x", debug);
1726 sz = sizeof(mps_debug_strings) / sizeof(mps_debug_strings[0]);
1727 for (i = 0; i < sz; i++) {
1728 string = &mps_debug_strings[i];
1729 if (debug & string->flag)
1730 sbuf_printf(sbuf, ",%s", string->name);
1733 error = sbuf_finish(sbuf);
1736 if (error || req->newptr == NULL)
1739 len = req->newlen - req->newidx;
1743 buffer = malloc(len, M_MPT2, M_ZERO|M_WAITOK);
1744 error = SYSCTL_IN(req, buffer, len);
1746 mps_parse_debug(sc, buffer);
1748 free(buffer, M_MPT2);
1753 mps_parse_debug(struct mps_softc *sc, char *list)
1755 struct mps_debug_string *string;
1756 enum mps_debug_level_combiner op;
1757 char *token, *endtoken;
1761 if (list == NULL || *list == '\0')
1767 } else if (*list == '-') {
1776 sz = sizeof(mps_debug_strings) / sizeof(mps_debug_strings[0]);
1777 while ((token = strsep(&list, ":,")) != NULL) {
1779 /* Handle integer flags */
1780 flags |= strtol(token, &endtoken, 0);
1781 if (token != endtoken)
1784 /* Handle text flags */
1785 for (i = 0; i < sz; i++) {
1786 string = &mps_debug_strings[i];
1787 if (strcasecmp(token, string->name) == 0) {
1788 flags |= string->flag;
1796 sc->mps_debug = flags;
1799 sc->mps_debug |= flags;
1802 sc->mps_debug &= (~flags);
1810 mps_attach(struct mps_softc *sc)
1815 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
1817 mtx_init(&sc->mps_mtx, "MPT2SAS lock", NULL, MTX_DEF);
1818 callout_init_mtx(&sc->periodic, &sc->mps_mtx, 0);
1819 callout_init_mtx(&sc->device_check_callout, &sc->mps_mtx, 0);
1820 TAILQ_INIT(&sc->event_list);
1821 timevalclear(&sc->lastfail);
1823 if ((error = mps_transition_ready(sc)) != 0) {
1824 mps_dprint(sc, MPS_INIT|MPS_FAULT, "failed to transition "
1829 sc->facts = malloc(sizeof(MPI2_IOC_FACTS_REPLY), M_MPT2,
1832 mps_dprint(sc, MPS_INIT|MPS_FAULT, "Cannot allocate memory, "
1838 * Get IOC Facts and allocate all structures based on this information.
1839 * A Diag Reset will also call mps_iocfacts_allocate and re-read the IOC
1840 * Facts. If relevant values have changed in IOC Facts, this function
1841 * will free all of the memory based on IOC Facts and reallocate that
1842 * memory. If this fails, any allocated memory should already be freed.
1844 if ((error = mps_iocfacts_allocate(sc, TRUE)) != 0) {
1845 mps_dprint(sc, MPS_INIT|MPS_FAULT, "IOC Facts based allocation "
1846 "failed with error %d, exit\n", error);
1850 /* Start the periodic watchdog check on the IOC Doorbell */
1854 * The portenable will kick off discovery events that will drive the
1855 * rest of the initialization process. The CAM/SAS module will
1856 * hold up the boot sequence until discovery is complete.
1858 sc->mps_ich.ich_func = mps_startup;
1859 sc->mps_ich.ich_arg = sc;
1860 if (config_intrhook_establish(&sc->mps_ich) != 0) {
1861 mps_dprint(sc, MPS_INIT|MPS_ERROR,
1862 "Cannot establish MPS config hook\n");
1867 * Allow IR to shutdown gracefully when shutdown occurs.
1869 sc->shutdown_eh = EVENTHANDLER_REGISTER(shutdown_final,
1870 mpssas_ir_shutdown, sc, SHUTDOWN_PRI_DEFAULT);
1872 if (sc->shutdown_eh == NULL)
1873 mps_dprint(sc, MPS_INIT|MPS_ERROR,
1874 "shutdown event registration failed\n");
1876 mps_setup_sysctl(sc);
1878 sc->mps_flags |= MPS_FLAGS_ATTACH_DONE;
1879 mps_dprint(sc, MPS_INIT, "%s exit error= %d\n", __func__, error);
1884 /* Run through any late-start handlers. */
1886 mps_startup(void *arg)
1888 struct mps_softc *sc;
1890 sc = (struct mps_softc *)arg;
1891 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
1894 mps_unmask_intr(sc);
1896 /* initialize device mapping tables */
1897 mps_base_static_config_pages(sc);
1898 mps_mapping_initialize(sc);
1902 mps_dprint(sc, MPS_INIT, "disestablish config intrhook\n");
1903 config_intrhook_disestablish(&sc->mps_ich);
1904 sc->mps_ich.ich_arg = NULL;
1906 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__);
1909 /* Periodic watchdog. Is called with the driver lock already held. */
1911 mps_periodic(void *arg)
1913 struct mps_softc *sc;
1916 sc = (struct mps_softc *)arg;
1917 if (sc->mps_flags & MPS_FLAGS_SHUTDOWN)
1920 db = mps_regread(sc, MPI2_DOORBELL_OFFSET);
1921 if ((db & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
1922 mps_dprint(sc, MPS_FAULT, "IOC Fault 0x%08x, Resetting\n", db);
1926 callout_reset(&sc->periodic, MPS_PERIODIC_DELAY * hz, mps_periodic, sc);
1930 mps_log_evt_handler(struct mps_softc *sc, uintptr_t data,
1931 MPI2_EVENT_NOTIFICATION_REPLY *event)
1933 MPI2_EVENT_DATA_LOG_ENTRY_ADDED *entry;
1935 MPS_DPRINT_EVENT(sc, generic, event);
1937 switch (event->Event) {
1938 case MPI2_EVENT_LOG_DATA:
1939 mps_dprint(sc, MPS_EVENT, "MPI2_EVENT_LOG_DATA:\n");
1940 if (sc->mps_debug & MPS_EVENT)
1941 hexdump(event->EventData, event->EventDataLength, NULL, 0);
1943 case MPI2_EVENT_LOG_ENTRY_ADDED:
1944 entry = (MPI2_EVENT_DATA_LOG_ENTRY_ADDED *)event->EventData;
1945 mps_dprint(sc, MPS_EVENT, "MPI2_EVENT_LOG_ENTRY_ADDED event "
1946 "0x%x Sequence %d:\n", entry->LogEntryQualifier,
1947 entry->LogSequence);
1956 mps_attach_log(struct mps_softc *sc)
1958 u32 events[MPI2_EVENT_NOTIFY_EVENTMASK_WORDS];
1961 setbit(events, MPI2_EVENT_LOG_DATA);
1962 setbit(events, MPI2_EVENT_LOG_ENTRY_ADDED);
1964 mps_register_events(sc, events, mps_log_evt_handler, NULL,
1971 mps_detach_log(struct mps_softc *sc)
1974 if (sc->mps_log_eh != NULL)
1975 mps_deregister_events(sc, sc->mps_log_eh);
1980 * Free all of the driver resources and detach submodules. Should be called
1981 * without the lock held.
1984 mps_free(struct mps_softc *sc)
1988 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
1989 /* Turn off the watchdog */
1991 sc->mps_flags |= MPS_FLAGS_SHUTDOWN;
1993 /* Lock must not be held for this */
1994 callout_drain(&sc->periodic);
1995 callout_drain(&sc->device_check_callout);
1997 if (((error = mps_detach_log(sc)) != 0) ||
1998 ((error = mps_detach_sas(sc)) != 0)) {
1999 mps_dprint(sc, MPS_INIT|MPS_FAULT, "failed to detach "
2000 "subsystems, exit\n");
2004 mps_detach_user(sc);
2006 /* Put the IOC back in the READY state. */
2008 if ((error = mps_transition_ready(sc)) != 0) {
2014 if (sc->facts != NULL)
2015 free(sc->facts, M_MPT2);
2018 * Free all buffers that are based on IOC Facts. A Diag Reset may need
2019 * to free these buffers too.
2021 mps_iocfacts_free(sc);
2023 if (sc->sysctl_tree != NULL)
2024 sysctl_ctx_free(&sc->sysctl_ctx);
2026 /* Deregister the shutdown function */
2027 if (sc->shutdown_eh != NULL)
2028 EVENTHANDLER_DEREGISTER(shutdown_final, sc->shutdown_eh);
2030 mtx_destroy(&sc->mps_mtx);
2031 mps_dprint(sc, MPS_INIT, "%s exit\n", __func__);
2036 static __inline void
2037 mps_complete_command(struct mps_softc *sc, struct mps_command *cm)
2042 mps_dprint(sc, MPS_ERROR, "Completing NULL command\n");
2046 if (cm->cm_flags & MPS_CM_FLAGS_POLLED)
2047 cm->cm_flags |= MPS_CM_FLAGS_COMPLETE;
2049 if (cm->cm_complete != NULL) {
2050 mps_dprint(sc, MPS_TRACE,
2051 "%s cm %p calling cm_complete %p data %p reply %p\n",
2052 __func__, cm, cm->cm_complete, cm->cm_complete_data,
2054 cm->cm_complete(sc, cm);
2057 if (cm->cm_flags & MPS_CM_FLAGS_WAKEUP) {
2058 mps_dprint(sc, MPS_TRACE, "waking up %p\n", cm);
2062 if (cm->cm_sc->io_cmds_active != 0) {
2063 cm->cm_sc->io_cmds_active--;
2065 mps_dprint(sc, MPS_ERROR, "Warning: io_cmds_active is "
2066 "out of sync - resynching to 0\n");
2072 mps_sas_log_info(struct mps_softc *sc , u32 log_info)
2074 union loginfo_type {
2083 union loginfo_type sas_loginfo;
2084 char *originator_str = NULL;
2086 sas_loginfo.loginfo = log_info;
2087 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
2090 /* each nexus loss loginfo */
2091 if (log_info == 0x31170000)
2094 /* eat the loginfos associated with task aborts */
2095 if ((log_info == 30050000 || log_info ==
2096 0x31140000 || log_info == 0x31130000))
2099 switch (sas_loginfo.dw.originator) {
2101 originator_str = "IOP";
2104 originator_str = "PL";
2107 originator_str = "IR";
2111 mps_dprint(sc, MPS_LOG, "log_info(0x%08x): originator(%s), "
2112 "code(0x%02x), sub_code(0x%04x)\n", log_info,
2113 originator_str, sas_loginfo.dw.code,
2114 sas_loginfo.dw.subcode);
2118 mps_display_reply_info(struct mps_softc *sc, uint8_t *reply)
2120 MPI2DefaultReply_t *mpi_reply;
2123 mpi_reply = (MPI2DefaultReply_t*)reply;
2124 sc_status = le16toh(mpi_reply->IOCStatus);
2125 if (sc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
2126 mps_sas_log_info(sc, le32toh(mpi_reply->IOCLogInfo));
2129 mps_intr(void *data)
2131 struct mps_softc *sc;
2134 sc = (struct mps_softc *)data;
2135 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
2138 * Check interrupt status register to flush the bus. This is
2139 * needed for both INTx interrupts and driver-driven polling
2141 status = mps_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET);
2142 if ((status & MPI2_HIS_REPLY_DESCRIPTOR_INTERRUPT) == 0)
2146 mps_intr_locked(data);
2152 * In theory, MSI/MSIX interrupts shouldn't need to read any registers on the
2153 * chip. Hopefully this theory is correct.
2156 mps_intr_msi(void *data)
2158 struct mps_softc *sc;
2160 sc = (struct mps_softc *)data;
2161 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
2163 mps_intr_locked(data);
2169 * The locking is overly broad and simplistic, but easy to deal with for now.
2172 mps_intr_locked(void *data)
2174 MPI2_REPLY_DESCRIPTORS_UNION *desc;
2175 struct mps_softc *sc;
2176 struct mps_command *cm = NULL;
2179 MPI2_DIAG_RELEASE_REPLY *rel_rep;
2180 mps_fw_diagnostic_buffer_t *pBuffer;
2182 sc = (struct mps_softc *)data;
2184 pq = sc->replypostindex;
2185 mps_dprint(sc, MPS_TRACE,
2186 "%s sc %p starting with replypostindex %u\n",
2187 __func__, sc, sc->replypostindex);
2191 desc = &sc->post_queue[sc->replypostindex];
2192 flags = desc->Default.ReplyFlags &
2193 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
2194 if ((flags == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
2195 || (le32toh(desc->Words.High) == 0xffffffff))
2198 /* increment the replypostindex now, so that event handlers
2199 * and cm completion handlers which decide to do a diag
2200 * reset can zero it without it getting incremented again
2201 * afterwards, and we break out of this loop on the next
2202 * iteration since the reply post queue has been cleared to
2203 * 0xFF and all descriptors look unused (which they are).
2205 if (++sc->replypostindex >= sc->pqdepth)
2206 sc->replypostindex = 0;
2209 case MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS:
2210 cm = &sc->commands[le16toh(desc->SCSIIOSuccess.SMID)];
2211 cm->cm_reply = NULL;
2213 case MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY:
2219 * Re-compose the reply address from the address
2220 * sent back from the chip. The ReplyFrameAddress
2221 * is the lower 32 bits of the physical address of
2222 * particular reply frame. Convert that address to
2223 * host format, and then use that to provide the
2224 * offset against the virtual address base
2225 * (sc->reply_frames).
2227 baddr = le32toh(desc->AddressReply.ReplyFrameAddress);
2228 reply = sc->reply_frames +
2229 (baddr - ((uint32_t)sc->reply_busaddr));
2231 * Make sure the reply we got back is in a valid
2232 * range. If not, go ahead and panic here, since
2233 * we'll probably panic as soon as we deference the
2234 * reply pointer anyway.
2236 if ((reply < sc->reply_frames)
2237 || (reply > (sc->reply_frames +
2238 (sc->fqdepth * sc->facts->ReplyFrameSize * 4)))) {
2239 printf("%s: WARNING: reply %p out of range!\n",
2241 printf("%s: reply_frames %p, fqdepth %d, "
2242 "frame size %d\n", __func__,
2243 sc->reply_frames, sc->fqdepth,
2244 sc->facts->ReplyFrameSize * 4);
2245 printf("%s: baddr %#x,\n", __func__, baddr);
2246 /* LSI-TODO. See Linux Code. Need Graceful exit*/
2247 panic("Reply address out of range");
2249 if (le16toh(desc->AddressReply.SMID) == 0) {
2250 if (((MPI2_DEFAULT_REPLY *)reply)->Function ==
2251 MPI2_FUNCTION_DIAG_BUFFER_POST) {
2253 * If SMID is 0 for Diag Buffer Post,
2254 * this implies that the reply is due to
2255 * a release function with a status that
2256 * the buffer has been released. Set
2257 * the buffer flags accordingly.
2260 (MPI2_DIAG_RELEASE_REPLY *)reply;
2261 if ((le16toh(rel_rep->IOCStatus) &
2262 MPI2_IOCSTATUS_MASK) ==
2263 MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED)
2266 &sc->fw_diag_buffer_list[
2267 rel_rep->BufferType];
2268 pBuffer->valid_data = TRUE;
2269 pBuffer->owned_by_firmware =
2271 pBuffer->immediate = FALSE;
2274 mps_dispatch_event(sc, baddr,
2275 (MPI2_EVENT_NOTIFICATION_REPLY *)
2278 cm = &sc->commands[le16toh(desc->AddressReply.SMID)];
2279 cm->cm_reply = reply;
2281 le32toh(desc->AddressReply.ReplyFrameAddress);
2285 case MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS:
2286 case MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER:
2287 case MPI2_RPY_DESCRIPT_FLAGS_RAID_ACCELERATOR_SUCCESS:
2290 mps_dprint(sc, MPS_ERROR, "Unhandled reply 0x%x\n",
2291 desc->Default.ReplyFlags);
2298 // Print Error reply frame
2300 mps_display_reply_info(sc,cm->cm_reply);
2301 mps_complete_command(sc, cm);
2304 desc->Words.Low = 0xffffffff;
2305 desc->Words.High = 0xffffffff;
2308 if (pq != sc->replypostindex) {
2309 mps_dprint(sc, MPS_TRACE,
2310 "%s sc %p writing postindex %d\n",
2311 __func__, sc, sc->replypostindex);
2312 mps_regwrite(sc, MPI2_REPLY_POST_HOST_INDEX_OFFSET, sc->replypostindex);
2319 mps_dispatch_event(struct mps_softc *sc, uintptr_t data,
2320 MPI2_EVENT_NOTIFICATION_REPLY *reply)
2322 struct mps_event_handle *eh;
2323 int event, handled = 0;
2325 event = le16toh(reply->Event);
2326 TAILQ_FOREACH(eh, &sc->event_list, eh_list) {
2327 if (isset(eh->mask, event)) {
2328 eh->callback(sc, data, reply);
2334 mps_dprint(sc, MPS_EVENT, "Unhandled event 0x%x\n", le16toh(event));
2337 * This is the only place that the event/reply should be freed.
2338 * Anything wanting to hold onto the event data should have
2339 * already copied it into their own storage.
2341 mps_free_reply(sc, data);
2345 mps_reregister_events_complete(struct mps_softc *sc, struct mps_command *cm)
2347 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
2350 MPS_DPRINT_EVENT(sc, generic,
2351 (MPI2_EVENT_NOTIFICATION_REPLY *)cm->cm_reply);
2353 mps_free_command(sc, cm);
2355 /* next, send a port enable */
2360 * For both register_events and update_events, the caller supplies a bitmap
2361 * of events that it _wants_. These functions then turn that into a bitmask
2362 * suitable for the controller.
2365 mps_register_events(struct mps_softc *sc, u32 *mask,
2366 mps_evt_callback_t *cb, void *data, struct mps_event_handle **handle)
2368 struct mps_event_handle *eh;
2371 eh = malloc(sizeof(struct mps_event_handle), M_MPT2, M_WAITOK|M_ZERO);
2373 mps_dprint(sc, MPS_ERROR, "Cannot allocate event memory\n");
2378 TAILQ_INSERT_TAIL(&sc->event_list, eh, eh_list);
2380 error = mps_update_events(sc, eh, mask);
2387 mps_update_events(struct mps_softc *sc, struct mps_event_handle *handle,
2390 MPI2_EVENT_NOTIFICATION_REQUEST *evtreq;
2391 MPI2_EVENT_NOTIFICATION_REPLY *reply = NULL;
2392 struct mps_command *cm;
2395 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
2397 if ((mask != NULL) && (handle != NULL))
2398 bcopy(mask, &handle->mask[0], sizeof(u32) *
2399 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS);
2401 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2402 sc->event_mask[i] = -1;
2404 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2405 sc->event_mask[i] &= ~handle->mask[i];
2408 if ((cm = mps_alloc_command(sc)) == NULL)
2410 evtreq = (MPI2_EVENT_NOTIFICATION_REQUEST *)cm->cm_req;
2411 evtreq->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
2412 evtreq->MsgFlags = 0;
2413 evtreq->SASBroadcastPrimitiveMasks = 0;
2414 #ifdef MPS_DEBUG_ALL_EVENTS
2416 u_char fullmask[16];
2417 memset(fullmask, 0x00, 16);
2418 bcopy(fullmask, &evtreq->EventMasks[0], sizeof(u32) *
2419 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS);
2422 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2423 evtreq->EventMasks[i] =
2424 htole32(sc->event_mask[i]);
2426 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2429 error = mps_wait_command(sc, &cm, 60, 0);
2431 reply = (MPI2_EVENT_NOTIFICATION_REPLY *)cm->cm_reply;
2432 if ((reply == NULL) ||
2433 (reply->IOCStatus & MPI2_IOCSTATUS_MASK) != MPI2_IOCSTATUS_SUCCESS)
2437 MPS_DPRINT_EVENT(sc, generic, reply);
2439 mps_dprint(sc, MPS_TRACE, "%s finished error %d\n", __func__, error);
2442 mps_free_command(sc, cm);
2447 mps_reregister_events(struct mps_softc *sc)
2449 MPI2_EVENT_NOTIFICATION_REQUEST *evtreq;
2450 struct mps_command *cm;
2451 struct mps_event_handle *eh;
2454 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
2456 /* first, reregister events */
2458 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2459 sc->event_mask[i] = -1;
2461 TAILQ_FOREACH(eh, &sc->event_list, eh_list) {
2462 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2463 sc->event_mask[i] &= ~eh->mask[i];
2466 if ((cm = mps_alloc_command(sc)) == NULL)
2468 evtreq = (MPI2_EVENT_NOTIFICATION_REQUEST *)cm->cm_req;
2469 evtreq->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
2470 evtreq->MsgFlags = 0;
2471 evtreq->SASBroadcastPrimitiveMasks = 0;
2472 #ifdef MPS_DEBUG_ALL_EVENTS
2474 u_char fullmask[16];
2475 memset(fullmask, 0x00, 16);
2476 bcopy(fullmask, &evtreq->EventMasks[0], sizeof(u32) *
2477 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS);
2480 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2481 evtreq->EventMasks[i] =
2482 htole32(sc->event_mask[i]);
2484 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2486 cm->cm_complete = mps_reregister_events_complete;
2488 error = mps_map_command(sc, cm);
2490 mps_dprint(sc, MPS_TRACE, "%s finished with error %d\n", __func__,
2496 mps_deregister_events(struct mps_softc *sc, struct mps_event_handle *handle)
2499 TAILQ_REMOVE(&sc->event_list, handle, eh_list);
2500 free(handle, M_MPT2);
2504 * Add a chain element as the next SGE for the specified command.
2505 * Reset cm_sge and cm_sgesize to indicate all the available space.
2508 mps_add_chain(struct mps_command *cm)
2510 MPI2_SGE_CHAIN32 *sgc;
2511 struct mps_chain *chain;
2514 if (cm->cm_sglsize < MPS_SGC_SIZE)
2515 panic("MPS: Need SGE Error Code\n");
2517 chain = mps_alloc_chain(cm->cm_sc);
2521 space = (int)cm->cm_sc->facts->IOCRequestFrameSize * 4;
2524 * Note: a double-linked list is used to make it easier to
2525 * walk for debugging.
2527 TAILQ_INSERT_TAIL(&cm->cm_chain_list, chain, chain_link);
2529 sgc = (MPI2_SGE_CHAIN32 *)&cm->cm_sge->MpiChain;
2530 sgc->Length = htole16(space);
2531 sgc->NextChainOffset = 0;
2532 /* TODO Looks like bug in Setting sgc->Flags.
2533 * sgc->Flags = ( MPI2_SGE_FLAGS_CHAIN_ELEMENT | MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
2534 * MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT
2535 * This is fine.. because we are not using simple element. In case of
2536 * MPI2_SGE_CHAIN32, we have separate Length and Flags feild.
2538 sgc->Flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT;
2539 sgc->Address = htole32(chain->chain_busaddr);
2541 cm->cm_sge = (MPI2_SGE_IO_UNION *)&chain->chain->MpiSimple;
2542 cm->cm_sglsize = space;
2547 * Add one scatter-gather element (chain, simple, transaction context)
2548 * to the scatter-gather list for a command. Maintain cm_sglsize and
2549 * cm_sge as the remaining size and pointer to the next SGE to fill
2553 mps_push_sge(struct mps_command *cm, void *sgep, size_t len, int segsleft)
2555 MPI2_SGE_TRANSACTION_UNION *tc = sgep;
2556 MPI2_SGE_SIMPLE64 *sge = sgep;
2558 uint32_t saved_buf_len, saved_address_low, saved_address_high;
2560 type = (tc->Flags & MPI2_SGE_FLAGS_ELEMENT_MASK);
2564 case MPI2_SGE_FLAGS_TRANSACTION_ELEMENT: {
2565 if (len != tc->DetailsLength + 4)
2566 panic("TC %p length %u or %zu?", tc,
2567 tc->DetailsLength + 4, len);
2570 case MPI2_SGE_FLAGS_CHAIN_ELEMENT:
2571 /* Driver only uses 32-bit chain elements */
2572 if (len != MPS_SGC_SIZE)
2573 panic("CHAIN %p length %u or %zu?", sgep,
2576 case MPI2_SGE_FLAGS_SIMPLE_ELEMENT:
2577 /* Driver only uses 64-bit SGE simple elements */
2578 if (len != MPS_SGE64_SIZE)
2579 panic("SGE simple %p length %u or %zu?", sge,
2580 MPS_SGE64_SIZE, len);
2581 if (((le32toh(sge->FlagsLength) >> MPI2_SGE_FLAGS_SHIFT) &
2582 MPI2_SGE_FLAGS_ADDRESS_SIZE) == 0)
2583 panic("SGE simple %p not marked 64-bit?", sge);
2587 panic("Unexpected SGE %p, flags %02x", tc, tc->Flags);
2592 * case 1: 1 more segment, enough room for it
2593 * case 2: 2 more segments, enough room for both
2594 * case 3: >=2 more segments, only enough room for 1 and a chain
2595 * case 4: >=1 more segment, enough room for only a chain
2596 * case 5: >=1 more segment, no room for anything (error)
2600 * There should be room for at least a chain element, or this
2601 * code is buggy. Case (5).
2603 if (cm->cm_sglsize < MPS_SGC_SIZE)
2604 panic("MPS: Need SGE Error Code\n");
2606 if (segsleft >= 2 &&
2607 cm->cm_sglsize < len + MPS_SGC_SIZE + MPS_SGE64_SIZE) {
2609 * There are 2 or more segments left to add, and only
2610 * enough room for 1 and a chain. Case (3).
2612 * Mark as last element in this chain if necessary.
2614 if (type == MPI2_SGE_FLAGS_SIMPLE_ELEMENT) {
2615 sge->FlagsLength |= htole32(
2616 MPI2_SGE_FLAGS_LAST_ELEMENT << MPI2_SGE_FLAGS_SHIFT);
2620 * Add the item then a chain. Do the chain now,
2621 * rather than on the next iteration, to simplify
2622 * understanding the code.
2624 cm->cm_sglsize -= len;
2625 bcopy(sgep, cm->cm_sge, len);
2626 cm->cm_sge = (MPI2_SGE_IO_UNION *)((uintptr_t)cm->cm_sge + len);
2627 return (mps_add_chain(cm));
2630 if (segsleft >= 1 && cm->cm_sglsize < len + MPS_SGC_SIZE) {
2632 * 1 or more segment, enough room for only a chain.
2633 * Hope the previous element wasn't a Simple entry
2634 * that needed to be marked with
2635 * MPI2_SGE_FLAGS_LAST_ELEMENT. Case (4).
2637 if ((error = mps_add_chain(cm)) != 0)
2642 /* Case 1: 1 more segment, enough room for it. */
2643 if (segsleft == 1 && cm->cm_sglsize < len)
2644 panic("1 seg left and no room? %u versus %zu",
2645 cm->cm_sglsize, len);
2647 /* Case 2: 2 more segments, enough room for both */
2648 if (segsleft == 2 && cm->cm_sglsize < len + MPS_SGE64_SIZE)
2649 panic("2 segs left and no room? %u versus %zu",
2650 cm->cm_sglsize, len);
2653 if (segsleft == 1 && type == MPI2_SGE_FLAGS_SIMPLE_ELEMENT) {
2655 * If this is a bi-directional request, need to account for that
2656 * here. Save the pre-filled sge values. These will be used
2657 * either for the 2nd SGL or for a single direction SGL. If
2658 * cm_out_len is non-zero, this is a bi-directional request, so
2659 * fill in the OUT SGL first, then the IN SGL, otherwise just
2660 * fill in the IN SGL. Note that at this time, when filling in
2661 * 2 SGL's for a bi-directional request, they both use the same
2662 * DMA buffer (same cm command).
2664 saved_buf_len = le32toh(sge->FlagsLength) & 0x00FFFFFF;
2665 saved_address_low = sge->Address.Low;
2666 saved_address_high = sge->Address.High;
2667 if (cm->cm_out_len) {
2668 sge->FlagsLength = htole32(cm->cm_out_len |
2669 ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2670 MPI2_SGE_FLAGS_END_OF_BUFFER |
2671 MPI2_SGE_FLAGS_HOST_TO_IOC |
2672 MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
2673 MPI2_SGE_FLAGS_SHIFT));
2674 cm->cm_sglsize -= len;
2675 bcopy(sgep, cm->cm_sge, len);
2676 cm->cm_sge = (MPI2_SGE_IO_UNION *)((uintptr_t)cm->cm_sge
2680 ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2681 MPI2_SGE_FLAGS_END_OF_BUFFER |
2682 MPI2_SGE_FLAGS_LAST_ELEMENT |
2683 MPI2_SGE_FLAGS_END_OF_LIST |
2684 MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
2685 MPI2_SGE_FLAGS_SHIFT);
2686 if (cm->cm_flags & MPS_CM_FLAGS_DATAIN) {
2688 ((uint32_t)(MPI2_SGE_FLAGS_IOC_TO_HOST) <<
2689 MPI2_SGE_FLAGS_SHIFT);
2692 ((uint32_t)(MPI2_SGE_FLAGS_HOST_TO_IOC) <<
2693 MPI2_SGE_FLAGS_SHIFT);
2695 sge->FlagsLength = htole32(saved_buf_len);
2696 sge->Address.Low = saved_address_low;
2697 sge->Address.High = saved_address_high;
2700 cm->cm_sglsize -= len;
2701 bcopy(sgep, cm->cm_sge, len);
2702 cm->cm_sge = (MPI2_SGE_IO_UNION *)((uintptr_t)cm->cm_sge + len);
2707 * Add one dma segment to the scatter-gather list for a command.
2710 mps_add_dmaseg(struct mps_command *cm, vm_paddr_t pa, size_t len, u_int flags,
2713 MPI2_SGE_SIMPLE64 sge;
2716 * This driver always uses 64-bit address elements for simplicity.
2718 bzero(&sge, sizeof(sge));
2719 flags |= MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2720 MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
2721 sge.FlagsLength = htole32(len | (flags << MPI2_SGE_FLAGS_SHIFT));
2722 mps_from_u64(pa, &sge.Address);
2724 return (mps_push_sge(cm, &sge, sizeof sge, segsleft));
2728 mps_data_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
2730 struct mps_softc *sc;
2731 struct mps_command *cm;
2732 u_int i, dir, sflags;
2734 cm = (struct mps_command *)arg;
2738 * In this case, just print out a warning and let the chip tell the
2739 * user they did the wrong thing.
2741 if ((cm->cm_max_segs != 0) && (nsegs > cm->cm_max_segs)) {
2742 mps_dprint(sc, MPS_ERROR,
2743 "%s: warning: busdma returned %d segments, "
2744 "more than the %d allowed\n", __func__, nsegs,
2749 * Set up DMA direction flags. Bi-directional requests are also handled
2750 * here. In that case, both direction flags will be set.
2753 if (cm->cm_flags & MPS_CM_FLAGS_SMP_PASS) {
2755 * We have to add a special case for SMP passthrough, there
2756 * is no easy way to generically handle it. The first
2757 * S/G element is used for the command (therefore the
2758 * direction bit needs to be set). The second one is used
2759 * for the reply. We'll leave it to the caller to make
2760 * sure we only have two buffers.
2763 * Even though the busdma man page says it doesn't make
2764 * sense to have both direction flags, it does in this case.
2765 * We have one s/g element being accessed in each direction.
2767 dir = BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD;
2770 * Set the direction flag on the first buffer in the SMP
2771 * passthrough request. We'll clear it for the second one.
2773 sflags |= MPI2_SGE_FLAGS_DIRECTION |
2774 MPI2_SGE_FLAGS_END_OF_BUFFER;
2775 } else if (cm->cm_flags & MPS_CM_FLAGS_DATAOUT) {
2776 sflags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
2777 dir = BUS_DMASYNC_PREWRITE;
2779 dir = BUS_DMASYNC_PREREAD;
2781 for (i = 0; i < nsegs; i++) {
2782 if ((cm->cm_flags & MPS_CM_FLAGS_SMP_PASS) && (i != 0)) {
2783 sflags &= ~MPI2_SGE_FLAGS_DIRECTION;
2785 error = mps_add_dmaseg(cm, segs[i].ds_addr, segs[i].ds_len,
2788 /* Resource shortage, roll back! */
2789 if (ratecheck(&sc->lastfail, &mps_chainfail_interval))
2790 mps_dprint(sc, MPS_INFO, "Out of chain frames, "
2791 "consider increasing hw.mps.max_chains.\n");
2792 cm->cm_flags |= MPS_CM_FLAGS_CHAIN_FAILED;
2793 mps_complete_command(sc, cm);
2798 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir);
2799 mps_enqueue_request(sc, cm);
2805 mps_data_cb2(void *arg, bus_dma_segment_t *segs, int nsegs, bus_size_t mapsize,
2808 mps_data_cb(arg, segs, nsegs, error);
2812 * This is the routine to enqueue commands ansynchronously.
2813 * Note that the only error path here is from bus_dmamap_load(), which can
2814 * return EINPROGRESS if it is waiting for resources. Other than this, it's
2815 * assumed that if you have a command in-hand, then you have enough credits
2819 mps_map_command(struct mps_softc *sc, struct mps_command *cm)
2823 if (cm->cm_flags & MPS_CM_FLAGS_USE_UIO) {
2824 error = bus_dmamap_load_uio(sc->buffer_dmat, cm->cm_dmamap,
2825 &cm->cm_uio, mps_data_cb2, cm, 0);
2826 } else if (cm->cm_flags & MPS_CM_FLAGS_USE_CCB) {
2827 error = bus_dmamap_load_ccb(sc->buffer_dmat, cm->cm_dmamap,
2828 cm->cm_data, mps_data_cb, cm, 0);
2829 } else if ((cm->cm_data != NULL) && (cm->cm_length != 0)) {
2830 error = bus_dmamap_load(sc->buffer_dmat, cm->cm_dmamap,
2831 cm->cm_data, cm->cm_length, mps_data_cb, cm, 0);
2833 /* Add a zero-length element as needed */
2834 if (cm->cm_sge != NULL)
2835 mps_add_dmaseg(cm, 0, 0, 0, 1);
2836 mps_enqueue_request(sc, cm);
2843 * This is the routine to enqueue commands synchronously. An error of
2844 * EINPROGRESS from mps_map_command() is ignored since the command will
2845 * be executed and enqueued automatically. Other errors come from msleep().
2848 mps_wait_command(struct mps_softc *sc, struct mps_command **cmp, int timeout,
2852 struct timeval cur_time, start_time;
2853 struct mps_command *cm = *cmp;
2855 if (sc->mps_flags & MPS_FLAGS_DIAGRESET)
2858 cm->cm_complete = NULL;
2859 cm->cm_flags |= MPS_CM_FLAGS_POLLED;
2860 error = mps_map_command(sc, cm);
2861 if ((error != 0) && (error != EINPROGRESS))
2865 * Check for context and wait for 50 mSec at a time until time has
2866 * expired or the command has finished. If msleep can't be used, need
2869 if (curthread->td_no_sleeping != 0)
2870 sleep_flag = NO_SLEEP;
2871 getmicrouptime(&start_time);
2872 if (mtx_owned(&sc->mps_mtx) && sleep_flag == CAN_SLEEP) {
2873 cm->cm_flags |= MPS_CM_FLAGS_WAKEUP;
2874 error = msleep(cm, &sc->mps_mtx, 0, "mpswait", timeout*hz);
2875 if (error == EWOULDBLOCK) {
2877 * Record the actual elapsed time in the case of a
2878 * timeout for the message below.
2880 getmicrouptime(&cur_time);
2881 timevalsub(&cur_time, &start_time);
2884 while ((cm->cm_flags & MPS_CM_FLAGS_COMPLETE) == 0) {
2885 mps_intr_locked(sc);
2886 if (sleep_flag == CAN_SLEEP)
2887 pause("mpswait", hz/20);
2891 getmicrouptime(&cur_time);
2892 timevalsub(&cur_time, &start_time);
2893 if (cur_time.tv_sec > timeout) {
2894 error = EWOULDBLOCK;
2900 if (error == EWOULDBLOCK) {
2901 mps_dprint(sc, MPS_FAULT, "Calling Reinit from %s, timeout=%d,"
2902 " elapsed=%jd\n", __func__, timeout,
2903 (intmax_t)cur_time.tv_sec);
2904 rc = mps_reinit(sc);
2905 mps_dprint(sc, MPS_FAULT, "Reinit %s\n", (rc == 0) ? "success" :
2907 if (sc->mps_flags & MPS_FLAGS_REALLOCATED) {
2909 * Tell the caller that we freed the command in a
2920 * The MPT driver had a verbose interface for config pages. In this driver,
2921 * reduce it to much simpler terms, similar to the Linux driver.
2924 mps_read_config_page(struct mps_softc *sc, struct mps_config_params *params)
2926 MPI2_CONFIG_REQUEST *req;
2927 struct mps_command *cm;
2930 if (sc->mps_flags & MPS_FLAGS_BUSY) {
2934 cm = mps_alloc_command(sc);
2939 req = (MPI2_CONFIG_REQUEST *)cm->cm_req;
2940 req->Function = MPI2_FUNCTION_CONFIG;
2941 req->Action = params->action;
2943 req->ChainOffset = 0;
2944 req->PageAddress = params->page_address;
2945 if (params->hdr.Struct.PageType == MPI2_CONFIG_PAGETYPE_EXTENDED) {
2946 MPI2_CONFIG_EXTENDED_PAGE_HEADER *hdr;
2948 hdr = ¶ms->hdr.Ext;
2949 req->ExtPageType = hdr->ExtPageType;
2950 req->ExtPageLength = hdr->ExtPageLength;
2951 req->Header.PageType = MPI2_CONFIG_PAGETYPE_EXTENDED;
2952 req->Header.PageLength = 0; /* Must be set to zero */
2953 req->Header.PageNumber = hdr->PageNumber;
2954 req->Header.PageVersion = hdr->PageVersion;
2956 MPI2_CONFIG_PAGE_HEADER *hdr;
2958 hdr = ¶ms->hdr.Struct;
2959 req->Header.PageType = hdr->PageType;
2960 req->Header.PageNumber = hdr->PageNumber;
2961 req->Header.PageLength = hdr->PageLength;
2962 req->Header.PageVersion = hdr->PageVersion;
2965 cm->cm_data = params->buffer;
2966 cm->cm_length = params->length;
2967 if (cm->cm_data != NULL) {
2968 cm->cm_sge = &req->PageBufferSGE;
2969 cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
2970 cm->cm_flags = MPS_CM_FLAGS_SGE_SIMPLE | MPS_CM_FLAGS_DATAIN;
2973 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2975 cm->cm_complete_data = params;
2976 if (params->callback != NULL) {
2977 cm->cm_complete = mps_config_complete;
2978 return (mps_map_command(sc, cm));
2980 error = mps_wait_command(sc, &cm, 0, CAN_SLEEP);
2982 mps_dprint(sc, MPS_FAULT,
2983 "Error %d reading config page\n", error);
2985 mps_free_command(sc, cm);
2988 mps_config_complete(sc, cm);
2995 mps_write_config_page(struct mps_softc *sc, struct mps_config_params *params)
3001 mps_config_complete(struct mps_softc *sc, struct mps_command *cm)
3003 MPI2_CONFIG_REPLY *reply;
3004 struct mps_config_params *params;
3007 params = cm->cm_complete_data;
3009 if (cm->cm_data != NULL) {
3010 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap,
3011 BUS_DMASYNC_POSTREAD);
3012 bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
3016 * XXX KDM need to do more error recovery? This results in the
3017 * device in question not getting probed.
3019 if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
3020 params->status = MPI2_IOCSTATUS_BUSY;
3024 reply = (MPI2_CONFIG_REPLY *)cm->cm_reply;
3025 if (reply == NULL) {
3026 params->status = MPI2_IOCSTATUS_BUSY;
3029 params->status = reply->IOCStatus;
3030 if (params->hdr.Struct.PageType == MPI2_CONFIG_PAGETYPE_EXTENDED) {
3031 params->hdr.Ext.ExtPageType = reply->ExtPageType;
3032 params->hdr.Ext.ExtPageLength = reply->ExtPageLength;
3033 params->hdr.Ext.PageType = reply->Header.PageType;
3034 params->hdr.Ext.PageNumber = reply->Header.PageNumber;
3035 params->hdr.Ext.PageVersion = reply->Header.PageVersion;
3037 params->hdr.Struct.PageType = reply->Header.PageType;
3038 params->hdr.Struct.PageNumber = reply->Header.PageNumber;
3039 params->hdr.Struct.PageLength = reply->Header.PageLength;
3040 params->hdr.Struct.PageVersion = reply->Header.PageVersion;
3044 mps_free_command(sc, cm);
3045 if (params->callback != NULL)
3046 params->callback(sc, params);