2 * Copyright (c) 2009 Yahoo! Inc.
3 * Copyright (c) 2012 LSI Corp.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * LSI MPT-Fusion Host Adapter FreeBSD
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 /* Communications core for LSI MPT2 */
37 /* TODO Move headers to mpsvar */
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/selinfo.h>
44 #include <sys/mutex.h>
45 #include <sys/module.h>
49 #include <sys/malloc.h>
51 #include <sys/sysctl.h>
52 #include <sys/queue.h>
53 #include <sys/kthread.h>
54 #include <sys/endian.h>
55 #include <sys/eventhandler.h>
57 #include <machine/bus.h>
58 #include <machine/resource.h>
62 #include <dev/pci/pcivar.h>
64 #include <cam/scsi/scsi_all.h>
66 #include <dev/mps/mpi/mpi2_type.h>
67 #include <dev/mps/mpi/mpi2.h>
68 #include <dev/mps/mpi/mpi2_ioc.h>
69 #include <dev/mps/mpi/mpi2_sas.h>
70 #include <dev/mps/mpi/mpi2_cnfg.h>
71 #include <dev/mps/mpi/mpi2_init.h>
72 #include <dev/mps/mpi/mpi2_tool.h>
73 #include <dev/mps/mps_ioctl.h>
74 #include <dev/mps/mpsvar.h>
75 #include <dev/mps/mps_table.h>
77 static int mps_diag_reset(struct mps_softc *sc, int sleep_flag);
78 static int mps_init_queues(struct mps_softc *sc);
79 static int mps_message_unit_reset(struct mps_softc *sc, int sleep_flag);
80 static int mps_transition_operational(struct mps_softc *sc);
81 static void mps_startup(void *arg);
82 static int mps_send_iocinit(struct mps_softc *sc);
83 static int mps_attach_log(struct mps_softc *sc);
84 static __inline void mps_complete_command(struct mps_command *cm);
85 static void mps_dispatch_event(struct mps_softc *sc, uintptr_t data,
86 MPI2_EVENT_NOTIFICATION_REPLY *reply);
87 static void mps_config_complete(struct mps_softc *sc, struct mps_command *cm);
88 static void mps_periodic(void *);
89 static int mps_reregister_events(struct mps_softc *sc);
90 static void mps_enqueue_request(struct mps_softc *sc, struct mps_command *cm);
91 static int mps_wait_db_ack(struct mps_softc *sc, int timeout, int sleep_flag);
92 SYSCTL_NODE(_hw, OID_AUTO, mps, CTLFLAG_RD, 0, "MPS Driver Parameters");
94 MALLOC_DEFINE(M_MPT2, "mps", "mpt2 driver memory");
97 * Do a "Diagnostic Reset" aka a hard reset. This should get the chip out of
98 * any state and back to its initialization state machine.
100 static char mpt2_reset_magic[] = { 0x00, 0x0f, 0x04, 0x0b, 0x02, 0x07, 0x0d };
102 /* Added this union to smoothly convert le64toh cm->cm_desc.Words.
103 * Compiler only support unint64_t to be passed as argument.
104 * Otherwise it will through below error
105 * "aggregate value used where an integer was expected"
108 typedef union _reply_descriptor {
114 }reply_descriptor,address_descriptor;
117 * sleep_flag can be either CAN_SLEEP or NO_SLEEP.
118 * If this function is called from process context, it can sleep
119 * and there is no harm to sleep, in case if this fuction is called
120 * from Interrupt handler, we can not sleep and need NO_SLEEP flag set.
121 * based on sleep flags driver will call either msleep, pause or DELAY.
122 * msleep and pause are of same variant, but pause is used when mps_mtx
123 * is not hold by driver.
127 mps_diag_reset(struct mps_softc *sc,int sleep_flag)
130 int i, error, tries = 0;
132 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
134 /* Clear any pending interrupts */
135 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
137 /*Force NO_SLEEP for threads prohibited to sleep
138 * e.a Thread from interrupt handler are prohibited to sleep.
140 if(curthread->td_pflags & TDP_NOSLEEPING)
141 sleep_flag = NO_SLEEP;
143 /* Push the magic sequence */
145 while (tries++ < 20) {
146 for (i = 0; i < sizeof(mpt2_reset_magic); i++)
147 mps_regwrite(sc, MPI2_WRITE_SEQUENCE_OFFSET,
148 mpt2_reset_magic[i]);
150 if (mtx_owned(&sc->mps_mtx) && sleep_flag == CAN_SLEEP)
151 msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0, "mpsdiag", hz/10);
152 else if (sleep_flag == CAN_SLEEP)
153 pause("mpsdiag", hz/10);
157 reg = mps_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
158 if (reg & MPI2_DIAG_DIAG_WRITE_ENABLE) {
166 /* Send the actual reset. XXX need to refresh the reg? */
167 mps_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET,
168 reg | MPI2_DIAG_RESET_ADAPTER);
170 /* Wait up to 300 seconds in 50ms intervals */
172 for (i = 0; i < 60000; i++) {
174 if (mtx_owned(&sc->mps_mtx) && sleep_flag == CAN_SLEEP)
175 msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0, "mpsdiag", hz/20);
176 else if (sleep_flag == CAN_SLEEP)
177 pause("mpsdiag", hz/20);
180 reg = mps_regread(sc, MPI2_DOORBELL_OFFSET);
181 if ((reg & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_RESET) {
189 mps_regwrite(sc, MPI2_WRITE_SEQUENCE_OFFSET, 0x0);
195 mps_message_unit_reset(struct mps_softc *sc, int sleep_flag)
198 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
200 mps_regwrite(sc, MPI2_DOORBELL_OFFSET,
201 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET <<
202 MPI2_DOORBELL_FUNCTION_SHIFT);
204 if (mps_wait_db_ack(sc, 5, sleep_flag) != 0) {
205 mps_dprint(sc, MPS_FAULT, "Doorbell handshake failed : <%s>\n",
214 mps_transition_ready(struct mps_softc *sc)
217 int error, tries = 0;
220 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
221 /* If we are in attach call, do not sleep */
222 sleep_flags = (sc->mps_flags & MPS_FLAGS_ATTACH_DONE)
223 ? CAN_SLEEP:NO_SLEEP;
225 while (tries++ < 5) {
226 reg = mps_regread(sc, MPI2_DOORBELL_OFFSET);
227 mps_dprint(sc, MPS_INFO, "Doorbell= 0x%x\n", reg);
230 * Ensure the IOC is ready to talk. If it's not, try
233 if (reg & MPI2_DOORBELL_USED) {
234 mps_diag_reset(sc, sleep_flags);
239 /* Is the adapter owned by another peer? */
240 if ((reg & MPI2_DOORBELL_WHO_INIT_MASK) ==
241 (MPI2_WHOINIT_PCI_PEER << MPI2_DOORBELL_WHO_INIT_SHIFT)) {
242 device_printf(sc->mps_dev, "IOC is under the control "
243 "of another peer host, aborting initialization.\n");
247 state = reg & MPI2_IOC_STATE_MASK;
248 if (state == MPI2_IOC_STATE_READY) {
252 } else if (state == MPI2_IOC_STATE_FAULT) {
253 mps_dprint(sc, MPS_INFO, "IOC in fault state 0x%x\n",
254 state & MPI2_DOORBELL_FAULT_CODE_MASK);
255 mps_diag_reset(sc, sleep_flags);
256 } else if (state == MPI2_IOC_STATE_OPERATIONAL) {
257 /* Need to take ownership */
258 mps_message_unit_reset(sc, sleep_flags);
259 } else if (state == MPI2_IOC_STATE_RESET) {
260 /* Wait a bit, IOC might be in transition */
261 mps_dprint(sc, MPS_FAULT,
262 "IOC in unexpected reset state\n");
264 mps_dprint(sc, MPS_FAULT,
265 "IOC in unknown state 0x%x\n", state);
270 /* Wait 50ms for things to settle down. */
275 device_printf(sc->mps_dev, "Cannot transition IOC to ready\n");
281 mps_transition_operational(struct mps_softc *sc)
286 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
289 reg = mps_regread(sc, MPI2_DOORBELL_OFFSET);
290 mps_dprint(sc, MPS_INFO, "Doorbell= 0x%x\n", reg);
292 state = reg & MPI2_IOC_STATE_MASK;
293 if (state != MPI2_IOC_STATE_READY) {
294 if ((error = mps_transition_ready(sc)) != 0) {
295 mps_dprint(sc, MPS_FAULT,
296 "%s failed to transition ready\n", __func__);
301 error = mps_send_iocinit(sc);
306 * XXX Some of this should probably move to mps.c
308 * The terms diag reset and hard reset are used interchangeably in the MPI
309 * docs to mean resetting the controller chip. In this code diag reset
310 * cleans everything up, and the hard reset function just sends the reset
311 * sequence to the chip. This should probably be refactored so that every
312 * subsystem gets a reset notification of some sort, and can clean up
316 mps_reinit(struct mps_softc *sc)
321 mps_printf(sc, "%s sc %p\n", __func__, sc);
323 mtx_assert(&sc->mps_mtx, MA_OWNED);
325 if (sc->mps_flags & MPS_FLAGS_DIAGRESET) {
326 mps_printf(sc, "%s reset already in progress\n", __func__);
330 /* make sure the completion callbacks can recognize they're getting
331 * a NULL cm_reply due to a reset.
333 sc->mps_flags |= MPS_FLAGS_DIAGRESET;
335 mps_printf(sc, "%s mask interrupts\n", __func__);
338 error = mps_diag_reset(sc, CAN_SLEEP);
340 panic("%s hard reset failed with error %d\n",
344 /* Restore the PCI state, including the MSI-X registers */
347 /* Give the I/O subsystem special priority to get itself prepared */
348 mpssas_handle_reinit(sc);
350 /* reinitialize queues after the reset */
351 bzero(sc->free_queue, sc->fqdepth * 4);
354 /* get the chip out of the reset state */
355 error = mps_transition_operational(sc);
357 panic("%s transition operational failed with error %d\n",
360 /* Reinitialize the reply queue. This is delicate because this
361 * function is typically invoked by task mgmt completion callbacks,
362 * which are called by the interrupt thread. We need to make sure
363 * the interrupt handler loop will exit when we return to it, and
364 * that it will recognize the indexes we've changed.
366 sc->replypostindex = 0;
367 mps_regwrite(sc, MPI2_REPLY_FREE_HOST_INDEX_OFFSET, sc->replyfreeindex);
368 mps_regwrite(sc, MPI2_REPLY_POST_HOST_INDEX_OFFSET, sc->replypostindex);
370 db = mps_regread(sc, MPI2_DOORBELL_OFFSET);
371 mps_printf(sc, "%s doorbell 0x%08x\n", __func__, db);
373 mps_printf(sc, "%s unmask interrupts post %u free %u\n", __func__,
374 sc->replypostindex, sc->replyfreeindex);
378 mps_printf(sc, "%s restarting post %u free %u\n", __func__,
379 sc->replypostindex, sc->replyfreeindex);
381 /* restart will reload the event masks clobbered by the reset, and
382 * then enable the port.
384 mps_reregister_events(sc);
386 /* the end of discovery will release the simq, so we're done. */
387 mps_printf(sc, "%s finished sc %p post %u free %u\n",
389 sc->replypostindex, sc->replyfreeindex);
391 sc->mps_flags &= ~MPS_FLAGS_DIAGRESET;
396 /* Wait for the chip to ACK a word that we've put into its FIFO
397 * Wait for <timeout> seconds. In single loop wait for busy loop
398 * for 500 microseconds.
399 * Total is [ 0.5 * (2000 * <timeout>) ] in miliseconds.
402 mps_wait_db_ack(struct mps_softc *sc, int timeout, int sleep_flag)
410 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
412 int_status = mps_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET);
413 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
414 mps_dprint(sc, MPS_INFO,
415 "%s: successfull count(%d), timeout(%d)\n",
416 __func__, count, timeout);
418 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
419 doorbell = mps_regread(sc, MPI2_DOORBELL_OFFSET);
420 if ((doorbell & MPI2_IOC_STATE_MASK) ==
421 MPI2_IOC_STATE_FAULT) {
422 mps_dprint(sc, MPS_FAULT,
423 "fault_state(0x%04x)!\n", doorbell);
426 } else if (int_status == 0xFFFFFFFF)
429 /* If it can sleep, sleep for 1 milisecond, else busy loop for
431 if (mtx_owned(&sc->mps_mtx) && sleep_flag == CAN_SLEEP)
432 msleep(&sc->msleep_fake_chan, &sc->mps_mtx, 0,
434 else if (sleep_flag == CAN_SLEEP)
435 pause("mpsdba", hz/1000);
442 mps_dprint(sc, MPS_FAULT, "%s: failed due to timeout count(%d), "
443 "int_status(%x)!\n", __func__, count, int_status);
448 /* Wait for the chip to signal that the next word in its FIFO can be fetched */
450 mps_wait_db_int(struct mps_softc *sc)
454 for (retry = 0; retry < MPS_DB_MAX_WAIT; retry++) {
455 if ((mps_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET) &
456 MPI2_HIS_IOC2SYS_DB_STATUS) != 0)
463 /* Step through the synchronous command state machine, i.e. "Doorbell mode" */
465 mps_request_sync(struct mps_softc *sc, void *req, MPI2_DEFAULT_REPLY *reply,
466 int req_sz, int reply_sz, int timeout)
470 int i, count, ioc_sz, residual;
471 int sleep_flags = CAN_SLEEP;
473 if(curthread->td_pflags & TDP_NOSLEEPING)
474 sleep_flags = NO_SLEEP;
477 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
480 if (mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED)
484 * Announce that a message is coming through the doorbell. Messages
485 * are pushed at 32bit words, so round up if needed.
487 count = (req_sz + 3) / 4;
488 mps_regwrite(sc, MPI2_DOORBELL_OFFSET,
489 (MPI2_FUNCTION_HANDSHAKE << MPI2_DOORBELL_FUNCTION_SHIFT) |
490 (count << MPI2_DOORBELL_ADD_DWORDS_SHIFT));
493 if (mps_wait_db_int(sc) ||
494 (mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED) == 0) {
495 mps_dprint(sc, MPS_FAULT, "Doorbell failed to activate\n");
498 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
499 if (mps_wait_db_ack(sc, 5, sleep_flags) != 0) {
500 mps_dprint(sc, MPS_FAULT, "Doorbell handshake failed\n");
505 /* Clock out the message data synchronously in 32-bit dwords*/
506 data32 = (uint32_t *)req;
507 for (i = 0; i < count; i++) {
508 mps_regwrite(sc, MPI2_DOORBELL_OFFSET, htole32(data32[i]));
509 if (mps_wait_db_ack(sc, 5, sleep_flags) != 0) {
510 mps_dprint(sc, MPS_FAULT,
511 "Timeout while writing doorbell\n");
517 /* Clock in the reply in 16-bit words. The total length of the
518 * message is always in the 4th byte, so clock out the first 2 words
519 * manually, then loop the rest.
521 data16 = (uint16_t *)reply;
522 if (mps_wait_db_int(sc) != 0) {
523 mps_dprint(sc, MPS_FAULT, "Timeout reading doorbell 0\n");
527 mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_DATA_MASK;
528 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
529 if (mps_wait_db_int(sc) != 0) {
530 mps_dprint(sc, MPS_FAULT, "Timeout reading doorbell 1\n");
534 mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_DATA_MASK;
535 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
537 /* Number of 32bit words in the message */
538 ioc_sz = reply->MsgLength;
541 * Figure out how many 16bit words to clock in without overrunning.
542 * The precision loss with dividing reply_sz can safely be
543 * ignored because the messages can only be multiples of 32bits.
546 count = MIN((reply_sz / 4), ioc_sz) * 2;
547 if (count < ioc_sz * 2) {
548 residual = ioc_sz * 2 - count;
549 mps_dprint(sc, MPS_FAULT, "Driver error, throwing away %d "
550 "residual message words\n", residual);
553 for (i = 2; i < count; i++) {
554 if (mps_wait_db_int(sc) != 0) {
555 mps_dprint(sc, MPS_FAULT,
556 "Timeout reading doorbell %d\n", i);
559 data16[i] = mps_regread(sc, MPI2_DOORBELL_OFFSET) &
560 MPI2_DOORBELL_DATA_MASK;
561 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
565 * Pull out residual words that won't fit into the provided buffer.
566 * This keeps the chip from hanging due to a driver programming
570 if (mps_wait_db_int(sc) != 0) {
571 mps_dprint(sc, MPS_FAULT,
572 "Timeout reading doorbell\n");
575 (void)mps_regread(sc, MPI2_DOORBELL_OFFSET);
576 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
580 if (mps_wait_db_int(sc) != 0) {
581 mps_dprint(sc, MPS_FAULT, "Timeout waiting to exit doorbell\n");
584 if (mps_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED)
585 mps_dprint(sc, MPS_FAULT, "Warning, doorbell still active\n");
586 mps_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
592 mps_enqueue_request(struct mps_softc *sc, struct mps_command *cm)
595 mps_dprint(sc, MPS_TRACE, "%s SMID %u cm %p ccb %p\n", __func__,
596 cm->cm_desc.Default.SMID, cm, cm->cm_ccb);
598 if (sc->mps_flags & MPS_FLAGS_ATTACH_DONE && !(sc->mps_flags & MPS_FLAGS_SHUTDOWN))
599 mtx_assert(&sc->mps_mtx, MA_OWNED);
601 if (++sc->io_cmds_active > sc->io_cmds_highwater)
602 sc->io_cmds_highwater++;
603 rd.u.low = cm->cm_desc.Words.Low;
604 rd.u.high = cm->cm_desc.Words.High;
605 rd.word = htole64(rd.word);
606 /* TODO-We may need to make below regwrite atomic */
607 mps_regwrite(sc, MPI2_REQUEST_DESCRIPTOR_POST_LOW_OFFSET,
609 mps_regwrite(sc, MPI2_REQUEST_DESCRIPTOR_POST_HIGH_OFFSET,
614 * Just the FACTS, ma'am.
617 mps_get_iocfacts(struct mps_softc *sc, MPI2_IOC_FACTS_REPLY *facts)
619 MPI2_DEFAULT_REPLY *reply;
620 MPI2_IOC_FACTS_REQUEST request;
621 int error, req_sz, reply_sz;
623 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
625 req_sz = sizeof(MPI2_IOC_FACTS_REQUEST);
626 reply_sz = sizeof(MPI2_IOC_FACTS_REPLY);
627 reply = (MPI2_DEFAULT_REPLY *)facts;
629 bzero(&request, req_sz);
630 request.Function = MPI2_FUNCTION_IOC_FACTS;
631 error = mps_request_sync(sc, &request, reply, req_sz, reply_sz, 5);
637 mps_get_portfacts(struct mps_softc *sc, MPI2_PORT_FACTS_REPLY *facts, int port)
639 MPI2_PORT_FACTS_REQUEST *request;
640 MPI2_PORT_FACTS_REPLY *reply;
641 struct mps_command *cm;
644 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
646 if ((cm = mps_alloc_command(sc)) == NULL)
648 request = (MPI2_PORT_FACTS_REQUEST *)cm->cm_req;
649 request->Function = MPI2_FUNCTION_PORT_FACTS;
650 request->PortNumber = port;
651 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
653 error = mps_request_polled(sc, cm);
654 reply = (MPI2_PORT_FACTS_REPLY *)cm->cm_reply;
656 mps_printf(sc, "%s NULL reply\n", __func__);
659 if ((reply->IOCStatus & MPI2_IOCSTATUS_MASK) != MPI2_IOCSTATUS_SUCCESS) {
661 "%s error %d iocstatus 0x%x iocloginfo 0x%x type 0x%x\n",
662 __func__, error, reply->IOCStatus, reply->IOCLogInfo,
666 bcopy(reply, facts, sizeof(MPI2_PORT_FACTS_REPLY));
668 mps_free_command(sc, cm);
674 mps_send_iocinit(struct mps_softc *sc)
676 MPI2_IOC_INIT_REQUEST init;
677 MPI2_DEFAULT_REPLY reply;
678 int req_sz, reply_sz, error;
680 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
682 req_sz = sizeof(MPI2_IOC_INIT_REQUEST);
683 reply_sz = sizeof(MPI2_IOC_INIT_REPLY);
684 bzero(&init, req_sz);
685 bzero(&reply, reply_sz);
688 * Fill in the init block. Note that most addresses are
689 * deliberately in the lower 32bits of memory. This is a micro-
690 * optimzation for PCI/PCIX, though it's not clear if it helps PCIe.
692 init.Function = MPI2_FUNCTION_IOC_INIT;
693 init.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
694 init.MsgVersion = htole16(MPI2_VERSION);
695 init.HeaderVersion = htole16(MPI2_HEADER_VERSION);
696 init.SystemRequestFrameSize = htole16(sc->facts->IOCRequestFrameSize);
697 init.ReplyDescriptorPostQueueDepth = htole16(sc->pqdepth);
698 init.ReplyFreeQueueDepth = htole16(sc->fqdepth);
699 init.SenseBufferAddressHigh = 0;
700 init.SystemReplyAddressHigh = 0;
701 init.SystemRequestFrameBaseAddress.High = 0;
702 init.SystemRequestFrameBaseAddress.Low = htole32((uint32_t)sc->req_busaddr);
703 init.ReplyDescriptorPostQueueAddress.High = 0;
704 init.ReplyDescriptorPostQueueAddress.Low = htole32((uint32_t)sc->post_busaddr);
705 init.ReplyFreeQueueAddress.High = 0;
706 init.ReplyFreeQueueAddress.Low = htole32((uint32_t)sc->free_busaddr);
707 init.TimeStamp.High = 0;
708 init.TimeStamp.Low = htole32((uint32_t)time_uptime);
710 error = mps_request_sync(sc, &init, &reply, req_sz, reply_sz, 5);
711 if ((reply.IOCStatus & MPI2_IOCSTATUS_MASK) != MPI2_IOCSTATUS_SUCCESS)
714 mps_dprint(sc, MPS_INFO, "IOCInit status= 0x%x\n", reply.IOCStatus);
719 mps_memaddr_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
724 *addr = segs[0].ds_addr;
728 mps_alloc_queues(struct mps_softc *sc)
730 bus_addr_t queues_busaddr;
732 int qsize, fqsize, pqsize;
735 * The reply free queue contains 4 byte entries in multiples of 16 and
736 * aligned on a 16 byte boundary. There must always be an unused entry.
737 * This queue supplies fresh reply frames for the firmware to use.
739 * The reply descriptor post queue contains 8 byte entries in
740 * multiples of 16 and aligned on a 16 byte boundary. This queue
741 * contains filled-in reply frames sent from the firmware to the host.
743 * These two queues are allocated together for simplicity.
745 sc->fqdepth = roundup2((sc->num_replies + 1), 16);
746 sc->pqdepth = roundup2((sc->num_replies + 1), 16);
747 fqsize= sc->fqdepth * 4;
748 pqsize = sc->pqdepth * 8;
749 qsize = fqsize + pqsize;
751 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
752 16, 0, /* algnmnt, boundary */
753 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
754 BUS_SPACE_MAXADDR, /* highaddr */
755 NULL, NULL, /* filter, filterarg */
758 qsize, /* maxsegsize */
760 NULL, NULL, /* lockfunc, lockarg */
762 device_printf(sc->mps_dev, "Cannot allocate queues DMA tag\n");
765 if (bus_dmamem_alloc(sc->queues_dmat, (void **)&queues, BUS_DMA_NOWAIT,
767 device_printf(sc->mps_dev, "Cannot allocate queues memory\n");
770 bzero(queues, qsize);
771 bus_dmamap_load(sc->queues_dmat, sc->queues_map, queues, qsize,
772 mps_memaddr_cb, &queues_busaddr, 0);
774 sc->free_queue = (uint32_t *)queues;
775 sc->free_busaddr = queues_busaddr;
776 sc->post_queue = (MPI2_REPLY_DESCRIPTORS_UNION *)(queues + fqsize);
777 sc->post_busaddr = queues_busaddr + fqsize;
783 mps_alloc_replies(struct mps_softc *sc)
785 int rsize, num_replies;
788 * sc->num_replies should be one less than sc->fqdepth. We need to
789 * allocate space for sc->fqdepth replies, but only sc->num_replies
790 * replies can be used at once.
792 num_replies = max(sc->fqdepth, sc->num_replies);
794 rsize = sc->facts->ReplyFrameSize * num_replies * 4;
795 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
796 4, 0, /* algnmnt, boundary */
797 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
798 BUS_SPACE_MAXADDR, /* highaddr */
799 NULL, NULL, /* filter, filterarg */
802 rsize, /* maxsegsize */
804 NULL, NULL, /* lockfunc, lockarg */
806 device_printf(sc->mps_dev, "Cannot allocate replies DMA tag\n");
809 if (bus_dmamem_alloc(sc->reply_dmat, (void **)&sc->reply_frames,
810 BUS_DMA_NOWAIT, &sc->reply_map)) {
811 device_printf(sc->mps_dev, "Cannot allocate replies memory\n");
814 bzero(sc->reply_frames, rsize);
815 bus_dmamap_load(sc->reply_dmat, sc->reply_map, sc->reply_frames, rsize,
816 mps_memaddr_cb, &sc->reply_busaddr, 0);
822 mps_alloc_requests(struct mps_softc *sc)
824 struct mps_command *cm;
825 struct mps_chain *chain;
828 rsize = sc->facts->IOCRequestFrameSize * sc->num_reqs * 4;
829 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
830 16, 0, /* algnmnt, boundary */
831 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
832 BUS_SPACE_MAXADDR, /* highaddr */
833 NULL, NULL, /* filter, filterarg */
836 rsize, /* maxsegsize */
838 NULL, NULL, /* lockfunc, lockarg */
840 device_printf(sc->mps_dev, "Cannot allocate request DMA tag\n");
843 if (bus_dmamem_alloc(sc->req_dmat, (void **)&sc->req_frames,
844 BUS_DMA_NOWAIT, &sc->req_map)) {
845 device_printf(sc->mps_dev, "Cannot allocate request memory\n");
848 bzero(sc->req_frames, rsize);
849 bus_dmamap_load(sc->req_dmat, sc->req_map, sc->req_frames, rsize,
850 mps_memaddr_cb, &sc->req_busaddr, 0);
852 rsize = sc->facts->IOCRequestFrameSize * sc->max_chains * 4;
853 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
854 16, 0, /* algnmnt, boundary */
855 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
856 BUS_SPACE_MAXADDR, /* highaddr */
857 NULL, NULL, /* filter, filterarg */
860 rsize, /* maxsegsize */
862 NULL, NULL, /* lockfunc, lockarg */
864 device_printf(sc->mps_dev, "Cannot allocate chain DMA tag\n");
867 if (bus_dmamem_alloc(sc->chain_dmat, (void **)&sc->chain_frames,
868 BUS_DMA_NOWAIT, &sc->chain_map)) {
869 device_printf(sc->mps_dev, "Cannot allocate chain memory\n");
872 bzero(sc->chain_frames, rsize);
873 bus_dmamap_load(sc->chain_dmat, sc->chain_map, sc->chain_frames, rsize,
874 mps_memaddr_cb, &sc->chain_busaddr, 0);
876 rsize = MPS_SENSE_LEN * sc->num_reqs;
877 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
878 1, 0, /* algnmnt, boundary */
879 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
880 BUS_SPACE_MAXADDR, /* highaddr */
881 NULL, NULL, /* filter, filterarg */
884 rsize, /* maxsegsize */
886 NULL, NULL, /* lockfunc, lockarg */
888 device_printf(sc->mps_dev, "Cannot allocate sense DMA tag\n");
891 if (bus_dmamem_alloc(sc->sense_dmat, (void **)&sc->sense_frames,
892 BUS_DMA_NOWAIT, &sc->sense_map)) {
893 device_printf(sc->mps_dev, "Cannot allocate sense memory\n");
896 bzero(sc->sense_frames, rsize);
897 bus_dmamap_load(sc->sense_dmat, sc->sense_map, sc->sense_frames, rsize,
898 mps_memaddr_cb, &sc->sense_busaddr, 0);
900 sc->chains = malloc(sizeof(struct mps_chain) * sc->max_chains, M_MPT2,
903 device_printf(sc->mps_dev,
904 "Cannot allocate chains memory %s %d\n",
908 for (i = 0; i < sc->max_chains; i++) {
909 chain = &sc->chains[i];
910 chain->chain = (MPI2_SGE_IO_UNION *)(sc->chain_frames +
911 i * sc->facts->IOCRequestFrameSize * 4);
912 chain->chain_busaddr = sc->chain_busaddr +
913 i * sc->facts->IOCRequestFrameSize * 4;
914 mps_free_chain(sc, chain);
915 sc->chain_free_lowwater++;
918 /* XXX Need to pick a more precise value */
919 nsegs = (MAXPHYS / PAGE_SIZE) + 1;
920 if (bus_dma_tag_create( sc->mps_parent_dmat, /* parent */
921 1, 0, /* algnmnt, boundary */
922 BUS_SPACE_MAXADDR, /* lowaddr */
923 BUS_SPACE_MAXADDR, /* highaddr */
924 NULL, NULL, /* filter, filterarg */
925 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
926 nsegs, /* nsegments */
927 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
928 BUS_DMA_ALLOCNOW, /* flags */
929 busdma_lock_mutex, /* lockfunc */
930 &sc->mps_mtx, /* lockarg */
932 device_printf(sc->mps_dev, "Cannot allocate buffer DMA tag\n");
937 * SMID 0 cannot be used as a free command per the firmware spec.
938 * Just drop that command instead of risking accounting bugs.
940 sc->commands = malloc(sizeof(struct mps_command) * sc->num_reqs,
941 M_MPT2, M_WAITOK | M_ZERO);
943 device_printf(sc->mps_dev, "Cannot allocate memory %s %d\n",
947 for (i = 1; i < sc->num_reqs; i++) {
948 cm = &sc->commands[i];
949 cm->cm_req = sc->req_frames +
950 i * sc->facts->IOCRequestFrameSize * 4;
951 cm->cm_req_busaddr = sc->req_busaddr +
952 i * sc->facts->IOCRequestFrameSize * 4;
953 cm->cm_sense = &sc->sense_frames[i];
954 cm->cm_sense_busaddr = sc->sense_busaddr + i * MPS_SENSE_LEN;
955 cm->cm_desc.Default.SMID = i;
957 TAILQ_INIT(&cm->cm_chain_list);
958 callout_init_mtx(&cm->cm_callout, &sc->mps_mtx, 0);
960 /* XXX Is a failure here a critical problem? */
961 if (bus_dmamap_create(sc->buffer_dmat, 0, &cm->cm_dmamap) == 0)
962 if (i <= sc->facts->HighPriorityCredit)
963 mps_free_high_priority_command(sc, cm);
965 mps_free_command(sc, cm);
967 panic("failed to allocate command %d\n", i);
977 mps_init_queues(struct mps_softc *sc)
981 memset((uint8_t *)sc->post_queue, 0xff, sc->pqdepth * 8);
984 * According to the spec, we need to use one less reply than we
985 * have space for on the queue. So sc->num_replies (the number we
986 * use) should be less than sc->fqdepth (allocated size).
988 if (sc->num_replies >= sc->fqdepth)
992 * Initialize all of the free queue entries.
994 for (i = 0; i < sc->fqdepth; i++)
995 sc->free_queue[i] = sc->reply_busaddr + (i * sc->facts->ReplyFrameSize * 4);
996 sc->replyfreeindex = sc->num_replies;
1001 /* Get the driver parameter tunables. Lowest priority are the driver defaults.
1002 * Next are the global settings, if they exist. Highest are the per-unit
1003 * settings, if they exist.
1006 mps_get_tunables(struct mps_softc *sc)
1010 /* XXX default to some debugging for now */
1011 sc->mps_debug = MPS_FAULT;
1012 sc->disable_msix = 0;
1013 sc->disable_msi = 0;
1014 sc->max_chains = MPS_CHAIN_FRAMES;
1017 * Grab the global variables.
1019 TUNABLE_INT_FETCH("hw.mps.debug_level", &sc->mps_debug);
1020 TUNABLE_INT_FETCH("hw.mps.disable_msix", &sc->disable_msix);
1021 TUNABLE_INT_FETCH("hw.mps.disable_msi", &sc->disable_msi);
1022 TUNABLE_INT_FETCH("hw.mps.max_chains", &sc->max_chains);
1024 /* Grab the unit-instance variables */
1025 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.debug_level",
1026 device_get_unit(sc->mps_dev));
1027 TUNABLE_INT_FETCH(tmpstr, &sc->mps_debug);
1029 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.disable_msix",
1030 device_get_unit(sc->mps_dev));
1031 TUNABLE_INT_FETCH(tmpstr, &sc->disable_msix);
1033 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.disable_msi",
1034 device_get_unit(sc->mps_dev));
1035 TUNABLE_INT_FETCH(tmpstr, &sc->disable_msi);
1037 snprintf(tmpstr, sizeof(tmpstr), "dev.mps.%d.max_chains",
1038 device_get_unit(sc->mps_dev));
1039 TUNABLE_INT_FETCH(tmpstr, &sc->max_chains);
1043 mps_setup_sysctl(struct mps_softc *sc)
1045 struct sysctl_ctx_list *sysctl_ctx = NULL;
1046 struct sysctl_oid *sysctl_tree = NULL;
1047 char tmpstr[80], tmpstr2[80];
1050 * Setup the sysctl variable so the user can change the debug level
1053 snprintf(tmpstr, sizeof(tmpstr), "MPS controller %d",
1054 device_get_unit(sc->mps_dev));
1055 snprintf(tmpstr2, sizeof(tmpstr2), "%d", device_get_unit(sc->mps_dev));
1057 sysctl_ctx = device_get_sysctl_ctx(sc->mps_dev);
1058 if (sysctl_ctx != NULL)
1059 sysctl_tree = device_get_sysctl_tree(sc->mps_dev);
1061 if (sysctl_tree == NULL) {
1062 sysctl_ctx_init(&sc->sysctl_ctx);
1063 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
1064 SYSCTL_STATIC_CHILDREN(_hw_mps), OID_AUTO, tmpstr2,
1065 CTLFLAG_RD, 0, tmpstr);
1066 if (sc->sysctl_tree == NULL)
1068 sysctl_ctx = &sc->sysctl_ctx;
1069 sysctl_tree = sc->sysctl_tree;
1072 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1073 OID_AUTO, "debug_level", CTLFLAG_RW, &sc->mps_debug, 0,
1076 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1077 OID_AUTO, "disable_msix", CTLFLAG_RD, &sc->disable_msix, 0,
1078 "Disable the use of MSI-X interrupts");
1080 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1081 OID_AUTO, "disable_msi", CTLFLAG_RD, &sc->disable_msi, 0,
1082 "Disable the use of MSI interrupts");
1084 SYSCTL_ADD_STRING(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1085 OID_AUTO, "firmware_version", CTLFLAG_RW, &sc->fw_version,
1086 strlen(sc->fw_version), "firmware version");
1088 SYSCTL_ADD_STRING(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1089 OID_AUTO, "driver_version", CTLFLAG_RW, MPS_DRIVER_VERSION,
1090 strlen(MPS_DRIVER_VERSION), "driver version");
1092 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1093 OID_AUTO, "io_cmds_active", CTLFLAG_RD,
1094 &sc->io_cmds_active, 0, "number of currently active commands");
1096 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1097 OID_AUTO, "io_cmds_highwater", CTLFLAG_RD,
1098 &sc->io_cmds_highwater, 0, "maximum active commands seen");
1100 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1101 OID_AUTO, "chain_free", CTLFLAG_RD,
1102 &sc->chain_free, 0, "number of free chain elements");
1104 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1105 OID_AUTO, "chain_free_lowwater", CTLFLAG_RD,
1106 &sc->chain_free_lowwater, 0,"lowest number of free chain elements");
1108 SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1109 OID_AUTO, "max_chains", CTLFLAG_RD,
1110 &sc->max_chains, 0,"maximum chain frames that will be allocated");
1112 #if __FreeBSD_version >= 900030
1113 SYSCTL_ADD_UQUAD(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1114 OID_AUTO, "chain_alloc_fail", CTLFLAG_RD,
1115 &sc->chain_alloc_fail, "chain allocation failures");
1116 #endif //FreeBSD_version >= 900030
1120 mps_attach(struct mps_softc *sc)
1124 mps_get_tunables(sc);
1126 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
1128 mtx_init(&sc->mps_mtx, "MPT2SAS lock", NULL, MTX_DEF);
1129 callout_init_mtx(&sc->periodic, &sc->mps_mtx, 0);
1130 TAILQ_INIT(&sc->event_list);
1132 if ((error = mps_transition_ready(sc)) != 0) {
1133 mps_printf(sc, "%s failed to transition ready\n", __func__);
1137 sc->facts = malloc(sizeof(MPI2_IOC_FACTS_REPLY), M_MPT2,
1140 device_printf(sc->mps_dev, "Cannot allocate memory %s %d\n",
1141 __func__, __LINE__);
1144 if ((error = mps_get_iocfacts(sc, sc->facts)) != 0)
1147 mps_print_iocfacts(sc, sc->facts);
1149 snprintf(sc->fw_version, sizeof(sc->fw_version),
1150 "%02d.%02d.%02d.%02d",
1151 sc->facts->FWVersion.Struct.Major,
1152 sc->facts->FWVersion.Struct.Minor,
1153 sc->facts->FWVersion.Struct.Unit,
1154 sc->facts->FWVersion.Struct.Dev);
1156 mps_printf(sc, "Firmware: %s, Driver: %s\n", sc->fw_version,
1157 MPS_DRIVER_VERSION);
1158 mps_printf(sc, "IOCCapabilities: %b\n", sc->facts->IOCCapabilities,
1159 "\20" "\3ScsiTaskFull" "\4DiagTrace" "\5SnapBuf" "\6ExtBuf"
1160 "\7EEDP" "\10BiDirTarg" "\11Multicast" "\14TransRetry" "\15IR"
1161 "\16EventReplay" "\17RaidAccel" "\20MSIXIndex" "\21HostDisc");
1164 * If the chip doesn't support event replay then a hard reset will be
1165 * required to trigger a full discovery. Do the reset here then
1166 * retransition to Ready. A hard reset might have already been done,
1167 * but it doesn't hurt to do it again.
1169 if ((sc->facts->IOCCapabilities &
1170 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY) == 0) {
1171 mps_diag_reset(sc, NO_SLEEP);
1172 if ((error = mps_transition_ready(sc)) != 0)
1177 * Set flag if IR Firmware is loaded.
1179 if (sc->facts->IOCCapabilities &
1180 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID)
1181 sc->ir_firmware = 1;
1184 * Check if controller supports FW diag buffers and set flag to enable
1187 if (sc->facts->IOCCapabilities &
1188 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1189 sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_TRACE].enabled =
1191 if (sc->facts->IOCCapabilities &
1192 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1193 sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_SNAPSHOT].enabled =
1195 if (sc->facts->IOCCapabilities &
1196 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1197 sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_EXTENDED].enabled =
1201 * Set flag if EEDP is supported and if TLR is supported.
1203 if (sc->facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP)
1204 sc->eedp_enabled = TRUE;
1205 if (sc->facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR)
1206 sc->control_TLR = TRUE;
1209 * Size the queues. Since the reply queues always need one free entry,
1210 * we'll just deduct one reply message here.
1212 sc->num_reqs = MIN(MPS_REQ_FRAMES, sc->facts->RequestCredit);
1213 sc->num_replies = MIN(MPS_REPLY_FRAMES + MPS_EVT_REPLY_FRAMES,
1214 sc->facts->MaxReplyDescriptorPostQueueDepth) - 1;
1215 TAILQ_INIT(&sc->req_list);
1216 TAILQ_INIT(&sc->high_priority_req_list);
1217 TAILQ_INIT(&sc->chain_list);
1218 TAILQ_INIT(&sc->tm_list);
1220 if (((error = mps_alloc_queues(sc)) != 0) ||
1221 ((error = mps_alloc_replies(sc)) != 0) ||
1222 ((error = mps_alloc_requests(sc)) != 0)) {
1223 mps_printf(sc, "%s failed to alloc\n", __func__);
1228 if (((error = mps_init_queues(sc)) != 0) ||
1229 ((error = mps_transition_operational(sc)) != 0)) {
1230 mps_printf(sc, "%s failed to transition operational\n", __func__);
1236 * Finish the queue initialization.
1237 * These are set here instead of in mps_init_queues() because the
1238 * IOC resets these values during the state transition in
1239 * mps_transition_operational(). The free index is set to 1
1240 * because the corresponding index in the IOC is set to 0, and the
1241 * IOC treats the queues as full if both are set to the same value.
1242 * Hence the reason that the queue can't hold all of the possible
1245 sc->replypostindex = 0;
1246 mps_regwrite(sc, MPI2_REPLY_FREE_HOST_INDEX_OFFSET, sc->replyfreeindex);
1247 mps_regwrite(sc, MPI2_REPLY_POST_HOST_INDEX_OFFSET, 0);
1249 sc->pfacts = malloc(sizeof(MPI2_PORT_FACTS_REPLY) *
1250 sc->facts->NumberOfPorts, M_MPT2, M_ZERO|M_WAITOK);
1252 device_printf(sc->mps_dev, "Cannot allocate memory %s %d\n",
1253 __func__, __LINE__);
1256 for (i = 0; i < sc->facts->NumberOfPorts; i++) {
1257 if ((error = mps_get_portfacts(sc, &sc->pfacts[i], i)) != 0) {
1258 mps_printf(sc, "%s failed to get portfacts for port %d\n",
1263 mps_print_portfacts(sc, &sc->pfacts[i]);
1266 /* Attach the subsystems so they can prepare their event masks. */
1267 /* XXX Should be dynamic so that IM/IR and user modules can attach */
1268 if (((error = mps_attach_log(sc)) != 0) ||
1269 ((error = mps_attach_sas(sc)) != 0) ||
1270 ((error = mps_attach_user(sc)) != 0)) {
1271 mps_printf(sc, "%s failed to attach all subsystems: error %d\n",
1277 if ((error = mps_pci_setup_interrupts(sc)) != 0) {
1278 mps_printf(sc, "%s failed to setup interrupts\n", __func__);
1284 * The static page function currently read is ioc page8. Others can be
1287 mps_base_static_config_pages(sc);
1289 /* Start the periodic watchdog check on the IOC Doorbell */
1293 * The portenable will kick off discovery events that will drive the
1294 * rest of the initialization process. The CAM/SAS module will
1295 * hold up the boot sequence until discovery is complete.
1297 sc->mps_ich.ich_func = mps_startup;
1298 sc->mps_ich.ich_arg = sc;
1299 if (config_intrhook_establish(&sc->mps_ich) != 0) {
1300 mps_dprint(sc, MPS_FAULT, "Cannot establish MPS config hook\n");
1305 * Allow IR to shutdown gracefully when shutdown occurs.
1307 sc->shutdown_eh = EVENTHANDLER_REGISTER(shutdown_final,
1308 mpssas_ir_shutdown, sc, SHUTDOWN_PRI_DEFAULT);
1310 if (sc->shutdown_eh == NULL)
1311 mps_dprint(sc, MPS_FAULT, "shutdown event registration "
1314 mps_setup_sysctl(sc);
1316 sc->mps_flags |= MPS_FLAGS_ATTACH_DONE;
1321 /* Run through any late-start handlers. */
1323 mps_startup(void *arg)
1325 struct mps_softc *sc;
1327 sc = (struct mps_softc *)arg;
1330 mps_unmask_intr(sc);
1331 /* initialize device mapping tables */
1332 mps_mapping_initialize(sc);
1337 /* Periodic watchdog. Is called with the driver lock already held. */
1339 mps_periodic(void *arg)
1341 struct mps_softc *sc;
1344 sc = (struct mps_softc *)arg;
1345 if (sc->mps_flags & MPS_FLAGS_SHUTDOWN)
1348 db = mps_regread(sc, MPI2_DOORBELL_OFFSET);
1349 if ((db & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
1350 device_printf(sc->mps_dev, "IOC Fault 0x%08x, Resetting\n", db);
1355 callout_reset(&sc->periodic, MPS_PERIODIC_DELAY * hz, mps_periodic, sc);
1359 mps_log_evt_handler(struct mps_softc *sc, uintptr_t data,
1360 MPI2_EVENT_NOTIFICATION_REPLY *event)
1362 MPI2_EVENT_DATA_LOG_ENTRY_ADDED *entry;
1364 mps_print_event(sc, event);
1366 switch (event->Event) {
1367 case MPI2_EVENT_LOG_DATA:
1368 device_printf(sc->mps_dev, "MPI2_EVENT_LOG_DATA:\n");
1369 hexdump(event->EventData, event->EventDataLength, NULL, 0);
1371 case MPI2_EVENT_LOG_ENTRY_ADDED:
1372 entry = (MPI2_EVENT_DATA_LOG_ENTRY_ADDED *)event->EventData;
1373 mps_dprint(sc, MPS_INFO, "MPI2_EVENT_LOG_ENTRY_ADDED event "
1374 "0x%x Sequence %d:\n", entry->LogEntryQualifier,
1375 entry->LogSequence);
1384 mps_attach_log(struct mps_softc *sc)
1386 u32 events[MPI2_EVENT_NOTIFY_EVENTMASK_WORDS];
1389 setbit(events, MPI2_EVENT_LOG_DATA);
1390 setbit(events, MPI2_EVENT_LOG_ENTRY_ADDED);
1392 mps_register_events(sc, events, mps_log_evt_handler, NULL,
1399 mps_detach_log(struct mps_softc *sc)
1402 if (sc->mps_log_eh != NULL)
1403 mps_deregister_events(sc, sc->mps_log_eh);
1408 * Free all of the driver resources and detach submodules. Should be called
1409 * without the lock held.
1412 mps_free(struct mps_softc *sc)
1414 struct mps_command *cm;
1417 /* Turn off the watchdog */
1419 sc->mps_flags |= MPS_FLAGS_SHUTDOWN;
1421 /* Lock must not be held for this */
1422 callout_drain(&sc->periodic);
1424 if (((error = mps_detach_log(sc)) != 0) ||
1425 ((error = mps_detach_sas(sc)) != 0))
1428 mps_detach_user(sc);
1430 /* Put the IOC back in the READY state. */
1432 if ((error = mps_transition_ready(sc)) != 0) {
1438 if (sc->facts != NULL)
1439 free(sc->facts, M_MPT2);
1441 if (sc->pfacts != NULL)
1442 free(sc->pfacts, M_MPT2);
1444 if (sc->post_busaddr != 0)
1445 bus_dmamap_unload(sc->queues_dmat, sc->queues_map);
1446 if (sc->post_queue != NULL)
1447 bus_dmamem_free(sc->queues_dmat, sc->post_queue,
1449 if (sc->queues_dmat != NULL)
1450 bus_dma_tag_destroy(sc->queues_dmat);
1452 if (sc->chain_busaddr != 0)
1453 bus_dmamap_unload(sc->chain_dmat, sc->chain_map);
1454 if (sc->chain_frames != NULL)
1455 bus_dmamem_free(sc->chain_dmat, sc->chain_frames,sc->chain_map);
1456 if (sc->chain_dmat != NULL)
1457 bus_dma_tag_destroy(sc->chain_dmat);
1459 if (sc->sense_busaddr != 0)
1460 bus_dmamap_unload(sc->sense_dmat, sc->sense_map);
1461 if (sc->sense_frames != NULL)
1462 bus_dmamem_free(sc->sense_dmat, sc->sense_frames,sc->sense_map);
1463 if (sc->sense_dmat != NULL)
1464 bus_dma_tag_destroy(sc->sense_dmat);
1466 if (sc->reply_busaddr != 0)
1467 bus_dmamap_unload(sc->reply_dmat, sc->reply_map);
1468 if (sc->reply_frames != NULL)
1469 bus_dmamem_free(sc->reply_dmat, sc->reply_frames,sc->reply_map);
1470 if (sc->reply_dmat != NULL)
1471 bus_dma_tag_destroy(sc->reply_dmat);
1473 if (sc->req_busaddr != 0)
1474 bus_dmamap_unload(sc->req_dmat, sc->req_map);
1475 if (sc->req_frames != NULL)
1476 bus_dmamem_free(sc->req_dmat, sc->req_frames, sc->req_map);
1477 if (sc->req_dmat != NULL)
1478 bus_dma_tag_destroy(sc->req_dmat);
1480 if (sc->chains != NULL)
1481 free(sc->chains, M_MPT2);
1482 if (sc->commands != NULL) {
1483 for (i = 1; i < sc->num_reqs; i++) {
1484 cm = &sc->commands[i];
1485 bus_dmamap_destroy(sc->buffer_dmat, cm->cm_dmamap);
1487 free(sc->commands, M_MPT2);
1489 if (sc->buffer_dmat != NULL)
1490 bus_dma_tag_destroy(sc->buffer_dmat);
1492 if (sc->sysctl_tree != NULL)
1493 sysctl_ctx_free(&sc->sysctl_ctx);
1495 mps_mapping_free_memory(sc);
1497 /* Deregister the shutdown function */
1498 if (sc->shutdown_eh != NULL)
1499 EVENTHANDLER_DEREGISTER(shutdown_final, sc->shutdown_eh);
1501 mtx_destroy(&sc->mps_mtx);
1506 static __inline void
1507 mps_complete_command(struct mps_command *cm)
1509 if (cm->cm_flags & MPS_CM_FLAGS_POLLED)
1510 cm->cm_flags |= MPS_CM_FLAGS_COMPLETE;
1512 if (cm->cm_complete != NULL) {
1513 mps_dprint(cm->cm_sc, MPS_TRACE,
1514 "%s cm %p calling cm_complete %p data %p reply %p\n",
1515 __func__, cm, cm->cm_complete, cm->cm_complete_data,
1517 cm->cm_complete(cm->cm_sc, cm);
1520 if (cm->cm_flags & MPS_CM_FLAGS_WAKEUP) {
1521 mps_dprint(cm->cm_sc, MPS_TRACE, "%s: waking up %p\n",
1526 if (cm->cm_sc->io_cmds_active != 0) {
1527 cm->cm_sc->io_cmds_active--;
1529 mps_dprint(cm->cm_sc, MPS_INFO, "Warning: io_cmds_active is "
1530 "out of sync - resynching to 0\n");
1536 mps_sas_log_info(struct mps_softc *sc , u32 log_info)
1538 union loginfo_type {
1547 union loginfo_type sas_loginfo;
1548 char *originator_str = NULL;
1550 sas_loginfo.loginfo = log_info;
1551 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
1554 /* each nexus loss loginfo */
1555 if (log_info == 0x31170000)
1558 /* eat the loginfos associated with task aborts */
1559 if ((log_info == 30050000 || log_info ==
1560 0x31140000 || log_info == 0x31130000))
1563 switch (sas_loginfo.dw.originator) {
1565 originator_str = "IOP";
1568 originator_str = "PL";
1571 originator_str = "IR";
1575 mps_dprint(sc, MPS_INFO, "log_info(0x%08x): originator(%s), "
1576 "code(0x%02x), sub_code(0x%04x)\n", log_info,
1577 originator_str, sas_loginfo.dw.code,
1578 sas_loginfo.dw.subcode);
1582 mps_display_reply_info(struct mps_softc *sc, uint8_t *reply)
1584 MPI2DefaultReply_t *mpi_reply;
1587 mpi_reply = (MPI2DefaultReply_t*)reply;
1588 sc_status = le16toh(mpi_reply->IOCStatus);
1589 if (sc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
1590 mps_sas_log_info(sc, le32toh(mpi_reply->IOCLogInfo));
1593 mps_intr(void *data)
1595 struct mps_softc *sc;
1598 sc = (struct mps_softc *)data;
1599 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
1602 * Check interrupt status register to flush the bus. This is
1603 * needed for both INTx interrupts and driver-driven polling
1605 status = mps_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET);
1606 if ((status & MPI2_HIS_REPLY_DESCRIPTOR_INTERRUPT) == 0)
1610 mps_intr_locked(data);
1616 * In theory, MSI/MSIX interrupts shouldn't need to read any registers on the
1617 * chip. Hopefully this theory is correct.
1620 mps_intr_msi(void *data)
1622 struct mps_softc *sc;
1624 sc = (struct mps_softc *)data;
1625 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
1627 mps_intr_locked(data);
1633 * The locking is overly broad and simplistic, but easy to deal with for now.
1636 mps_intr_locked(void *data)
1638 MPI2_REPLY_DESCRIPTORS_UNION *desc;
1639 struct mps_softc *sc;
1640 struct mps_command *cm = NULL;
1643 MPI2_DIAG_RELEASE_REPLY *rel_rep;
1644 mps_fw_diagnostic_buffer_t *pBuffer;
1646 sc = (struct mps_softc *)data;
1648 pq = sc->replypostindex;
1649 mps_dprint(sc, MPS_TRACE,
1650 "%s sc %p starting with replypostindex %u\n",
1651 __func__, sc, sc->replypostindex);
1655 desc = &sc->post_queue[sc->replypostindex];
1656 flags = desc->Default.ReplyFlags &
1657 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1658 if ((flags == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1659 || (le32toh(desc->Words.High) == 0xffffffff))
1662 /* increment the replypostindex now, so that event handlers
1663 * and cm completion handlers which decide to do a diag
1664 * reset can zero it without it getting incremented again
1665 * afterwards, and we break out of this loop on the next
1666 * iteration since the reply post queue has been cleared to
1667 * 0xFF and all descriptors look unused (which they are).
1669 if (++sc->replypostindex >= sc->pqdepth)
1670 sc->replypostindex = 0;
1673 case MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS:
1674 cm = &sc->commands[le16toh(desc->SCSIIOSuccess.SMID)];
1675 cm->cm_reply = NULL;
1677 case MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY:
1683 * Re-compose the reply address from the address
1684 * sent back from the chip. The ReplyFrameAddress
1685 * is the lower 32 bits of the physical address of
1686 * particular reply frame. Convert that address to
1687 * host format, and then use that to provide the
1688 * offset against the virtual address base
1689 * (sc->reply_frames).
1691 baddr = le32toh(desc->AddressReply.ReplyFrameAddress);
1692 reply = sc->reply_frames +
1693 (baddr - ((uint32_t)sc->reply_busaddr));
1695 * Make sure the reply we got back is in a valid
1696 * range. If not, go ahead and panic here, since
1697 * we'll probably panic as soon as we deference the
1698 * reply pointer anyway.
1700 if ((reply < sc->reply_frames)
1701 || (reply > (sc->reply_frames +
1702 (sc->fqdepth * sc->facts->ReplyFrameSize * 4)))) {
1703 printf("%s: WARNING: reply %p out of range!\n",
1705 printf("%s: reply_frames %p, fqdepth %d, "
1706 "frame size %d\n", __func__,
1707 sc->reply_frames, sc->fqdepth,
1708 sc->facts->ReplyFrameSize * 4);
1709 printf("%s: baddr %#x,\n", __func__, baddr);
1710 /* LSI-TODO. See Linux Code. Need Gracefull exit*/
1711 panic("Reply address out of range");
1713 if (le16toh(desc->AddressReply.SMID) == 0) {
1714 if (((MPI2_DEFAULT_REPLY *)reply)->Function ==
1715 MPI2_FUNCTION_DIAG_BUFFER_POST) {
1717 * If SMID is 0 for Diag Buffer Post,
1718 * this implies that the reply is due to
1719 * a release function with a status that
1720 * the buffer has been released. Set
1721 * the buffer flags accordingly.
1724 (MPI2_DIAG_RELEASE_REPLY *)reply;
1725 if (le16toh(rel_rep->IOCStatus) ==
1726 MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED)
1729 &sc->fw_diag_buffer_list[
1730 rel_rep->BufferType];
1731 pBuffer->valid_data = TRUE;
1732 pBuffer->owned_by_firmware =
1734 pBuffer->immediate = FALSE;
1737 mps_dispatch_event(sc, baddr,
1738 (MPI2_EVENT_NOTIFICATION_REPLY *)
1741 cm = &sc->commands[le16toh(desc->AddressReply.SMID)];
1742 cm->cm_reply = reply;
1744 le32toh(desc->AddressReply.ReplyFrameAddress);
1748 case MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS:
1749 case MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER:
1750 case MPI2_RPY_DESCRIPT_FLAGS_RAID_ACCELERATOR_SUCCESS:
1753 device_printf(sc->mps_dev, "Unhandled reply 0x%x\n",
1754 desc->Default.ReplyFlags);
1761 // Print Error reply frame
1763 mps_display_reply_info(sc,cm->cm_reply);
1764 mps_complete_command(cm);
1767 desc->Words.Low = 0xffffffff;
1768 desc->Words.High = 0xffffffff;
1771 if (pq != sc->replypostindex) {
1772 mps_dprint(sc, MPS_TRACE,
1773 "%s sc %p writing postindex %d\n",
1774 __func__, sc, sc->replypostindex);
1775 mps_regwrite(sc, MPI2_REPLY_POST_HOST_INDEX_OFFSET, sc->replypostindex);
1782 mps_dispatch_event(struct mps_softc *sc, uintptr_t data,
1783 MPI2_EVENT_NOTIFICATION_REPLY *reply)
1785 struct mps_event_handle *eh;
1786 int event, handled = 0;
1788 event = le16toh(reply->Event);
1789 TAILQ_FOREACH(eh, &sc->event_list, eh_list) {
1790 if (isset(eh->mask, event)) {
1791 eh->callback(sc, data, reply);
1797 device_printf(sc->mps_dev, "Unhandled event 0x%x\n", le16toh(event));
1800 * This is the only place that the event/reply should be freed.
1801 * Anything wanting to hold onto the event data should have
1802 * already copied it into their own storage.
1804 mps_free_reply(sc, data);
1808 mps_reregister_events_complete(struct mps_softc *sc, struct mps_command *cm)
1810 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
1814 (MPI2_EVENT_NOTIFICATION_REPLY *)cm->cm_reply);
1816 mps_free_command(sc, cm);
1818 /* next, send a port enable */
1823 * For both register_events and update_events, the caller supplies a bitmap
1824 * of events that it _wants_. These functions then turn that into a bitmask
1825 * suitable for the controller.
1828 mps_register_events(struct mps_softc *sc, u32 *mask,
1829 mps_evt_callback_t *cb, void *data, struct mps_event_handle **handle)
1831 struct mps_event_handle *eh;
1834 eh = malloc(sizeof(struct mps_event_handle), M_MPT2, M_WAITOK|M_ZERO);
1836 device_printf(sc->mps_dev, "Cannot allocate memory %s %d\n",
1837 __func__, __LINE__);
1842 TAILQ_INSERT_TAIL(&sc->event_list, eh, eh_list);
1844 error = mps_update_events(sc, eh, mask);
1851 mps_update_events(struct mps_softc *sc, struct mps_event_handle *handle,
1854 MPI2_EVENT_NOTIFICATION_REQUEST *evtreq;
1855 MPI2_EVENT_NOTIFICATION_REPLY *reply;
1856 struct mps_command *cm;
1859 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
1861 if ((mask != NULL) && (handle != NULL))
1862 bcopy(mask, &handle->mask[0], sizeof(u32) *
1863 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS);
1865 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
1866 sc->event_mask[i] = -1;
1868 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
1869 sc->event_mask[i] &= ~handle->mask[i];
1872 if ((cm = mps_alloc_command(sc)) == NULL)
1874 evtreq = (MPI2_EVENT_NOTIFICATION_REQUEST *)cm->cm_req;
1875 evtreq->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
1876 evtreq->MsgFlags = 0;
1877 evtreq->SASBroadcastPrimitiveMasks = 0;
1878 #ifdef MPS_DEBUG_ALL_EVENTS
1880 u_char fullmask[16];
1881 memset(fullmask, 0x00, 16);
1882 bcopy(fullmask, &evtreq->EventMasks[0], sizeof(u32) *
1883 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS);
1886 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
1887 evtreq->EventMasks[i] =
1888 htole32(sc->event_mask[i]);
1890 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1893 error = mps_request_polled(sc, cm);
1894 reply = (MPI2_EVENT_NOTIFICATION_REPLY *)cm->cm_reply;
1895 if ((reply == NULL) ||
1896 (reply->IOCStatus & MPI2_IOCSTATUS_MASK) != MPI2_IOCSTATUS_SUCCESS)
1898 mps_print_event(sc, reply);
1899 mps_dprint(sc, MPS_TRACE, "%s finished error %d\n", __func__, error);
1901 mps_free_command(sc, cm);
1906 mps_reregister_events(struct mps_softc *sc)
1908 MPI2_EVENT_NOTIFICATION_REQUEST *evtreq;
1909 struct mps_command *cm;
1910 struct mps_event_handle *eh;
1913 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
1915 /* first, reregister events */
1917 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
1918 sc->event_mask[i] = -1;
1920 TAILQ_FOREACH(eh, &sc->event_list, eh_list) {
1921 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
1922 sc->event_mask[i] &= ~eh->mask[i];
1925 if ((cm = mps_alloc_command(sc)) == NULL)
1927 evtreq = (MPI2_EVENT_NOTIFICATION_REQUEST *)cm->cm_req;
1928 evtreq->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
1929 evtreq->MsgFlags = 0;
1930 evtreq->SASBroadcastPrimitiveMasks = 0;
1931 #ifdef MPS_DEBUG_ALL_EVENTS
1933 u_char fullmask[16];
1934 memset(fullmask, 0x00, 16);
1935 bcopy(fullmask, &evtreq->EventMasks[0], sizeof(u32) *
1936 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS);
1939 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
1940 evtreq->EventMasks[i] =
1941 htole32(sc->event_mask[i]);
1943 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1945 cm->cm_complete = mps_reregister_events_complete;
1947 error = mps_map_command(sc, cm);
1949 mps_dprint(sc, MPS_TRACE, "%s finished with error %d\n", __func__, error);
1954 mps_deregister_events(struct mps_softc *sc, struct mps_event_handle *handle)
1957 TAILQ_REMOVE(&sc->event_list, handle, eh_list);
1958 free(handle, M_MPT2);
1962 * Add a chain element as the next SGE for the specified command.
1963 * Reset cm_sge and cm_sgesize to indicate all the available space.
1966 mps_add_chain(struct mps_command *cm)
1968 MPI2_SGE_CHAIN32 *sgc;
1969 struct mps_chain *chain;
1972 if (cm->cm_sglsize < MPS_SGC_SIZE)
1973 panic("MPS: Need SGE Error Code\n");
1975 chain = mps_alloc_chain(cm->cm_sc);
1979 space = (int)cm->cm_sc->facts->IOCRequestFrameSize * 4;
1982 * Note: a double-linked list is used to make it easier to
1983 * walk for debugging.
1985 TAILQ_INSERT_TAIL(&cm->cm_chain_list, chain, chain_link);
1987 sgc = (MPI2_SGE_CHAIN32 *)&cm->cm_sge->MpiChain;
1988 sgc->Length = htole16(space);
1989 sgc->NextChainOffset = 0;
1990 /* TODO Looks like bug in Setting sgc->Flags.
1991 * sgc->Flags = ( MPI2_SGE_FLAGS_CHAIN_ELEMENT | MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1992 * MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT
1993 * This is fine.. because we are not using simple element. In case of
1994 * MPI2_SGE_CHAIN32, we have seperate Length and Flags feild.
1996 sgc->Flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT;
1997 sgc->Address = htole32(chain->chain_busaddr);
1999 cm->cm_sge = (MPI2_SGE_IO_UNION *)&chain->chain->MpiSimple;
2000 cm->cm_sglsize = space;
2005 * Add one scatter-gather element (chain, simple, transaction context)
2006 * to the scatter-gather list for a command. Maintain cm_sglsize and
2007 * cm_sge as the remaining size and pointer to the next SGE to fill
2011 mps_push_sge(struct mps_command *cm, void *sgep, size_t len, int segsleft)
2013 MPI2_SGE_TRANSACTION_UNION *tc = sgep;
2014 MPI2_SGE_SIMPLE64 *sge = sgep;
2016 uint32_t saved_buf_len, saved_address_low, saved_address_high;
2019 type = (tc->Flags & MPI2_SGE_FLAGS_ELEMENT_MASK);
2023 case MPI2_SGE_FLAGS_TRANSACTION_ELEMENT: {
2024 if (len != tc->DetailsLength + 4)
2025 panic("TC %p length %u or %zu?", tc,
2026 tc->DetailsLength + 4, len);
2029 case MPI2_SGE_FLAGS_CHAIN_ELEMENT:
2030 /* Driver only uses 32-bit chain elements */
2031 if (len != MPS_SGC_SIZE)
2032 panic("CHAIN %p length %u or %zu?", sgep,
2035 case MPI2_SGE_FLAGS_SIMPLE_ELEMENT:
2036 /* Driver only uses 64-bit SGE simple elements */
2038 if (len != MPS_SGE64_SIZE)
2039 panic("SGE simple %p length %u or %zu?", sge,
2040 MPS_SGE64_SIZE, len);
2041 if (((sge->FlagsLength >> MPI2_SGE_FLAGS_SHIFT) &
2042 MPI2_SGE_FLAGS_ADDRESS_SIZE) == 0)
2043 panic("SGE simple %p flags %02x not marked 64-bit?",
2044 sge, sge->FlagsLength >> MPI2_SGE_FLAGS_SHIFT);
2048 panic("Unexpected SGE %p, flags %02x", tc, tc->Flags);
2053 * case 1: 1 more segment, enough room for it
2054 * case 2: 2 more segments, enough room for both
2055 * case 3: >=2 more segments, only enough room for 1 and a chain
2056 * case 4: >=1 more segment, enough room for only a chain
2057 * case 5: >=1 more segment, no room for anything (error)
2061 * There should be room for at least a chain element, or this
2062 * code is buggy. Case (5).
2064 if (cm->cm_sglsize < MPS_SGC_SIZE)
2065 panic("MPS: Need SGE Error Code\n");
2067 if (segsleft >= 2 &&
2068 cm->cm_sglsize < len + MPS_SGC_SIZE + MPS_SGE64_SIZE) {
2070 * There are 2 or more segments left to add, and only
2071 * enough room for 1 and a chain. Case (3).
2073 * Mark as last element in this chain if necessary.
2075 if (type == MPI2_SGE_FLAGS_SIMPLE_ELEMENT) {
2077 (MPI2_SGE_FLAGS_LAST_ELEMENT << MPI2_SGE_FLAGS_SHIFT);
2081 * Add the item then a chain. Do the chain now,
2082 * rather than on the next iteration, to simplify
2083 * understanding the code.
2085 cm->cm_sglsize -= len;
2086 /* Endian Safe code */
2087 sge_flags = sge->FlagsLength;
2088 sge->FlagsLength = htole32(sge_flags);
2089 sge->Address.High = htole32(sge->Address.High);
2090 sge->Address.Low = htole32(sge->Address.Low);
2091 bcopy(sgep, cm->cm_sge, len);
2092 cm->cm_sge = (MPI2_SGE_IO_UNION *)((uintptr_t)cm->cm_sge + len);
2093 return (mps_add_chain(cm));
2096 if (segsleft >= 1 && cm->cm_sglsize < len + MPS_SGC_SIZE) {
2098 * 1 or more segment, enough room for only a chain.
2099 * Hope the previous element wasn't a Simple entry
2100 * that needed to be marked with
2101 * MPI2_SGE_FLAGS_LAST_ELEMENT. Case (4).
2103 if ((error = mps_add_chain(cm)) != 0)
2108 /* Case 1: 1 more segment, enough room for it. */
2109 if (segsleft == 1 && cm->cm_sglsize < len)
2110 panic("1 seg left and no room? %u versus %zu",
2111 cm->cm_sglsize, len);
2113 /* Case 2: 2 more segments, enough room for both */
2114 if (segsleft == 2 && cm->cm_sglsize < len + MPS_SGE64_SIZE)
2115 panic("2 segs left and no room? %u versus %zu",
2116 cm->cm_sglsize, len);
2119 if (segsleft == 1 && type == MPI2_SGE_FLAGS_SIMPLE_ELEMENT) {
2121 * If this is a bi-directional request, need to account for that
2122 * here. Save the pre-filled sge values. These will be used
2123 * either for the 2nd SGL or for a single direction SGL. If
2124 * cm_out_len is non-zero, this is a bi-directional request, so
2125 * fill in the OUT SGL first, then the IN SGL, otherwise just
2126 * fill in the IN SGL. Note that at this time, when filling in
2127 * 2 SGL's for a bi-directional request, they both use the same
2128 * DMA buffer (same cm command).
2130 saved_buf_len = sge->FlagsLength & 0x00FFFFFF;
2131 saved_address_low = sge->Address.Low;
2132 saved_address_high = sge->Address.High;
2133 if (cm->cm_out_len) {
2134 sge->FlagsLength = cm->cm_out_len |
2135 ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2136 MPI2_SGE_FLAGS_END_OF_BUFFER |
2137 MPI2_SGE_FLAGS_HOST_TO_IOC |
2138 MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
2139 MPI2_SGE_FLAGS_SHIFT);
2140 cm->cm_sglsize -= len;
2141 /* Endian Safe code */
2142 sge_flags = sge->FlagsLength;
2143 sge->FlagsLength = htole32(sge_flags);
2144 sge->Address.High = htole32(sge->Address.High);
2145 sge->Address.Low = htole32(sge->Address.Low);
2146 bcopy(sgep, cm->cm_sge, len);
2147 cm->cm_sge = (MPI2_SGE_IO_UNION *)((uintptr_t)cm->cm_sge
2150 sge->FlagsLength = saved_buf_len |
2151 ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2152 MPI2_SGE_FLAGS_END_OF_BUFFER |
2153 MPI2_SGE_FLAGS_LAST_ELEMENT |
2154 MPI2_SGE_FLAGS_END_OF_LIST |
2155 MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
2156 MPI2_SGE_FLAGS_SHIFT);
2157 if (cm->cm_flags & MPS_CM_FLAGS_DATAIN) {
2159 ((uint32_t)(MPI2_SGE_FLAGS_IOC_TO_HOST) <<
2160 MPI2_SGE_FLAGS_SHIFT);
2163 ((uint32_t)(MPI2_SGE_FLAGS_HOST_TO_IOC) <<
2164 MPI2_SGE_FLAGS_SHIFT);
2166 sge->Address.Low = saved_address_low;
2167 sge->Address.High = saved_address_high;
2170 cm->cm_sglsize -= len;
2171 /* Endian Safe code */
2172 sge_flags = sge->FlagsLength;
2173 sge->FlagsLength = htole32(sge_flags);
2174 sge->Address.High = htole32(sge->Address.High);
2175 sge->Address.Low = htole32(sge->Address.Low);
2176 bcopy(sgep, cm->cm_sge, len);
2177 cm->cm_sge = (MPI2_SGE_IO_UNION *)((uintptr_t)cm->cm_sge + len);
2182 * Add one dma segment to the scatter-gather list for a command.
2185 mps_add_dmaseg(struct mps_command *cm, vm_paddr_t pa, size_t len, u_int flags,
2188 MPI2_SGE_SIMPLE64 sge;
2191 * This driver always uses 64-bit address elements for simplicity.
2193 flags |= MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
2194 MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
2195 /* Set Endian safe macro in mps_push_sge */
2196 sge.FlagsLength = len | (flags << MPI2_SGE_FLAGS_SHIFT);
2197 mps_from_u64(pa, &sge.Address);
2199 return (mps_push_sge(cm, &sge, sizeof sge, segsleft));
2203 mps_data_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
2205 struct mps_softc *sc;
2206 struct mps_command *cm;
2207 u_int i, dir, sflags;
2209 cm = (struct mps_command *)arg;
2213 * In this case, just print out a warning and let the chip tell the
2214 * user they did the wrong thing.
2216 if ((cm->cm_max_segs != 0) && (nsegs > cm->cm_max_segs)) {
2217 mps_printf(sc, "%s: warning: busdma returned %d segments, "
2218 "more than the %d allowed\n", __func__, nsegs,
2223 * Set up DMA direction flags. Bi-directional requests are also handled
2224 * here. In that case, both direction flags will be set.
2227 if (cm->cm_flags & MPS_CM_FLAGS_SMP_PASS) {
2229 * We have to add a special case for SMP passthrough, there
2230 * is no easy way to generically handle it. The first
2231 * S/G element is used for the command (therefore the
2232 * direction bit needs to be set). The second one is used
2233 * for the reply. We'll leave it to the caller to make
2234 * sure we only have two buffers.
2237 * Even though the busdma man page says it doesn't make
2238 * sense to have both direction flags, it does in this case.
2239 * We have one s/g element being accessed in each direction.
2241 dir = BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD;
2244 * Set the direction flag on the first buffer in the SMP
2245 * passthrough request. We'll clear it for the second one.
2247 sflags |= MPI2_SGE_FLAGS_DIRECTION |
2248 MPI2_SGE_FLAGS_END_OF_BUFFER;
2249 } else if (cm->cm_flags & MPS_CM_FLAGS_DATAOUT) {
2250 sflags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
2251 dir = BUS_DMASYNC_PREWRITE;
2253 dir = BUS_DMASYNC_PREREAD;
2255 for (i = 0; i < nsegs; i++) {
2256 if ((cm->cm_flags & MPS_CM_FLAGS_SMP_PASS) && (i != 0)) {
2257 sflags &= ~MPI2_SGE_FLAGS_DIRECTION;
2259 error = mps_add_dmaseg(cm, segs[i].ds_addr, segs[i].ds_len,
2262 /* Resource shortage, roll back! */
2263 mps_dprint(sc, MPS_INFO, "out of chain frames\n");
2264 cm->cm_flags |= MPS_CM_FLAGS_CHAIN_FAILED;
2265 mps_complete_command(cm);
2270 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir);
2271 mps_enqueue_request(sc, cm);
2277 mps_data_cb2(void *arg, bus_dma_segment_t *segs, int nsegs, bus_size_t mapsize,
2280 mps_data_cb(arg, segs, nsegs, error);
2284 * This is the routine to enqueue commands ansynchronously.
2285 * Note that the only error path here is from bus_dmamap_load(), which can
2286 * return EINPROGRESS if it is waiting for resources. Other than this, it's
2287 * assumed that if you have a command in-hand, then you have enough credits
2291 mps_map_command(struct mps_softc *sc, struct mps_command *cm)
2293 MPI2_SGE_SIMPLE32 *sge;
2296 if (cm->cm_flags & MPS_CM_FLAGS_USE_UIO) {
2297 error = bus_dmamap_load_uio(sc->buffer_dmat, cm->cm_dmamap,
2298 &cm->cm_uio, mps_data_cb2, cm, 0);
2299 } else if ((cm->cm_data != NULL) && (cm->cm_length != 0)) {
2300 error = bus_dmamap_load(sc->buffer_dmat, cm->cm_dmamap,
2301 cm->cm_data, cm->cm_length, mps_data_cb, cm, 0);
2303 /* Add a zero-length element as needed */
2304 if (cm->cm_sge != NULL) {
2305 sge = (MPI2_SGE_SIMPLE32 *)cm->cm_sge;
2306 sge->FlagsLength = htole32((MPI2_SGE_FLAGS_LAST_ELEMENT |
2307 MPI2_SGE_FLAGS_END_OF_BUFFER |
2308 MPI2_SGE_FLAGS_END_OF_LIST |
2309 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
2310 MPI2_SGE_FLAGS_SHIFT);
2313 mps_enqueue_request(sc, cm);
2320 * This is the routine to enqueue commands synchronously. An error of
2321 * EINPROGRESS from mps_map_command() is ignored since the command will
2322 * be executed and enqueued automatically. Other errors come from msleep().
2325 mps_wait_command(struct mps_softc *sc, struct mps_command *cm, int timeout)
2329 mtx_assert(&sc->mps_mtx, MA_OWNED);
2331 if(sc->mps_flags & MPS_FLAGS_DIAGRESET)
2334 cm->cm_complete = NULL;
2335 cm->cm_flags |= MPS_CM_FLAGS_WAKEUP;
2336 error = mps_map_command(sc, cm);
2337 if ((error != 0) && (error != EINPROGRESS))
2339 error = msleep(cm, &sc->mps_mtx, 0, "mpswait", timeout*hz);
2340 if (error == EWOULDBLOCK) {
2341 mps_dprint(sc, MPS_FAULT, "Calling Reinit from %s\n", __func__);
2342 rc = mps_reinit(sc);
2343 mps_dprint(sc, MPS_FAULT, "Reinit %s\n",
2344 (rc == 0) ? "success" : "failed");
2351 * This is the routine to enqueue a command synchonously and poll for
2352 * completion. Its use should be rare.
2355 mps_request_polled(struct mps_softc *sc, struct mps_command *cm)
2357 int error, timeout = 0, rc;
2361 cm->cm_flags |= MPS_CM_FLAGS_POLLED;
2362 cm->cm_complete = NULL;
2363 mps_map_command(sc, cm);
2365 while ((cm->cm_flags & MPS_CM_FLAGS_COMPLETE) == 0) {
2366 mps_intr_locked(sc);
2369 if (timeout++ > 1000) {
2370 mps_dprint(sc, MPS_FAULT, "polling failed\n");
2377 mps_dprint(sc, MPS_FAULT, "Calling Reinit from %s\n", __func__);
2378 rc = mps_reinit(sc);
2379 mps_dprint(sc, MPS_FAULT, "Reinit %s\n",
2380 (rc == 0) ? "success" : "failed");
2387 * The MPT driver had a verbose interface for config pages. In this driver,
2388 * reduce it to much simplier terms, similar to the Linux driver.
2391 mps_read_config_page(struct mps_softc *sc, struct mps_config_params *params)
2393 MPI2_CONFIG_REQUEST *req;
2394 struct mps_command *cm;
2397 if (sc->mps_flags & MPS_FLAGS_BUSY) {
2401 cm = mps_alloc_command(sc);
2406 req = (MPI2_CONFIG_REQUEST *)cm->cm_req;
2407 req->Function = MPI2_FUNCTION_CONFIG;
2408 req->Action = params->action;
2410 req->ChainOffset = 0;
2411 req->PageAddress = params->page_address;
2412 if (params->hdr.Ext.ExtPageType != 0) {
2413 MPI2_CONFIG_EXTENDED_PAGE_HEADER *hdr;
2415 hdr = ¶ms->hdr.Ext;
2416 req->ExtPageType = hdr->ExtPageType;
2417 req->ExtPageLength = hdr->ExtPageLength;
2418 req->Header.PageType = MPI2_CONFIG_PAGETYPE_EXTENDED;
2419 req->Header.PageLength = 0; /* Must be set to zero */
2420 req->Header.PageNumber = hdr->PageNumber;
2421 req->Header.PageVersion = hdr->PageVersion;
2423 MPI2_CONFIG_PAGE_HEADER *hdr;
2425 hdr = ¶ms->hdr.Struct;
2426 req->Header.PageType = hdr->PageType;
2427 req->Header.PageNumber = hdr->PageNumber;
2428 req->Header.PageLength = hdr->PageLength;
2429 req->Header.PageVersion = hdr->PageVersion;
2432 cm->cm_data = params->buffer;
2433 cm->cm_length = params->length;
2434 cm->cm_sge = &req->PageBufferSGE;
2435 cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
2436 cm->cm_flags = MPS_CM_FLAGS_SGE_SIMPLE | MPS_CM_FLAGS_DATAIN;
2437 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2439 cm->cm_complete_data = params;
2440 if (params->callback != NULL) {
2441 cm->cm_complete = mps_config_complete;
2442 return (mps_map_command(sc, cm));
2444 error = mps_wait_command(sc, cm, 0);
2446 mps_dprint(sc, MPS_FAULT,
2447 "Error %d reading config page\n", error);
2448 mps_free_command(sc, cm);
2451 mps_config_complete(sc, cm);
2458 mps_write_config_page(struct mps_softc *sc, struct mps_config_params *params)
2464 mps_config_complete(struct mps_softc *sc, struct mps_command *cm)
2466 MPI2_CONFIG_REPLY *reply;
2467 struct mps_config_params *params;
2469 params = cm->cm_complete_data;
2471 if (cm->cm_data != NULL) {
2472 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap,
2473 BUS_DMASYNC_POSTREAD);
2474 bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
2478 * XXX KDM need to do more error recovery? This results in the
2479 * device in question not getting probed.
2481 if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
2482 params->status = MPI2_IOCSTATUS_BUSY;
2486 reply = (MPI2_CONFIG_REPLY *)cm->cm_reply;
2487 if (reply == NULL) {
2488 params->status = MPI2_IOCSTATUS_BUSY;
2491 params->status = reply->IOCStatus;
2492 if (params->hdr.Ext.ExtPageType != 0) {
2493 params->hdr.Ext.ExtPageType = reply->ExtPageType;
2494 params->hdr.Ext.ExtPageLength = reply->ExtPageLength;
2496 params->hdr.Struct.PageType = reply->Header.PageType;
2497 params->hdr.Struct.PageNumber = reply->Header.PageNumber;
2498 params->hdr.Struct.PageLength = reply->Header.PageLength;
2499 params->hdr.Struct.PageVersion = reply->Header.PageVersion;
2503 mps_free_command(sc, cm);
2504 if (params->callback != NULL)
2505 params->callback(sc, params);