2 * Generic routines for LSI Fusion adapters.
5 * Copyright (c) 2000, 2001 by Greg Ansley
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 immediately at the beginning of the file, without modification,
12 * this list of conditions, and the following disclaimer.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
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 FOR
20 * 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
29 * Copyright (c) 2002, 2006 by Matthew Jacob
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions are
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
38 * substantially similar to the "NO WARRANTY" disclaimer below
39 * ("Disclaimer") and any redistribution must be conditioned upon including
40 * a substantially similar Disclaimer requirement for further binary
42 * 3. Neither the names of the above listed copyright holders nor the names
43 * of any contributors may be used to endorse or promote products derived
44 * from this software without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
47 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
50 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
56 * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 * Support from Chris Ellsworth in order to make SAS adapters work
59 * is gratefully acknowledged.
62 * Support from LSI-Logic has also gone a great deal toward making this a
63 * workable subsystem and is gratefully acknowledged.
66 * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
67 * Copyright (c) 2005, WHEEL Sp. z o.o.
68 * Copyright (c) 2004, 2005 Justin T. Gibbs
69 * All rights reserved.
71 * Redistribution and use in source and binary forms, with or without
72 * modification, are permitted provided that the following conditions are
74 * 1. Redistributions of source code must retain the above copyright
75 * notice, this list of conditions and the following disclaimer.
76 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
77 * substantially similar to the "NO WARRANTY" disclaimer below
78 * ("Disclaimer") and any redistribution must be conditioned upon including
79 * a substantially similar Disclaimer requirement for further binary
81 * 3. Neither the names of the above listed copyright holders nor the names
82 * of any contributors may be used to endorse or promote products derived
83 * from this software without specific prior written permission.
85 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
86 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
89 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
90 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
91 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
92 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
93 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
94 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
95 * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
98 #include <sys/cdefs.h>
99 __FBSDID("$FreeBSD$");
101 #include <dev/mpt/mpt.h>
102 #include <dev/mpt/mpt_cam.h> /* XXX For static handler registration */
103 #include <dev/mpt/mpt_raid.h> /* XXX For static handler registration */
105 #include <dev/mpt/mpilib/mpi.h>
106 #include <dev/mpt/mpilib/mpi_ioc.h>
107 #include <dev/mpt/mpilib/mpi_fc.h>
108 #include <dev/mpt/mpilib/mpi_targ.h>
110 #include <sys/sysctl.h>
112 #define MPT_MAX_TRYS 3
113 #define MPT_MAX_WAIT 300000
115 static int maxwait_ack = 0;
116 static int maxwait_int = 0;
117 static int maxwait_state = 0;
119 static TAILQ_HEAD(, mpt_softc) mpt_tailq = TAILQ_HEAD_INITIALIZER(mpt_tailq);
120 mpt_reply_handler_t *mpt_reply_handlers[MPT_NUM_REPLY_HANDLERS];
122 static mpt_reply_handler_t mpt_default_reply_handler;
123 static mpt_reply_handler_t mpt_config_reply_handler;
124 static mpt_reply_handler_t mpt_handshake_reply_handler;
125 static mpt_reply_handler_t mpt_event_reply_handler;
126 static void mpt_send_event_ack(struct mpt_softc *mpt, request_t *ack_req,
127 MSG_EVENT_NOTIFY_REPLY *msg, uint32_t context);
128 static int mpt_send_event_request(struct mpt_softc *mpt, int onoff);
129 static int mpt_soft_reset(struct mpt_softc *mpt);
130 static void mpt_hard_reset(struct mpt_softc *mpt);
131 static int mpt_dma_buf_alloc(struct mpt_softc *mpt);
132 static void mpt_dma_buf_free(struct mpt_softc *mpt);
133 static int mpt_configure_ioc(struct mpt_softc *mpt, int, int);
134 static int mpt_enable_ioc(struct mpt_softc *mpt, int);
136 /************************* Personality Module Support *************************/
138 * We include one extra entry that is guaranteed to be NULL
139 * to simplify our itterator.
141 static struct mpt_personality *mpt_personalities[MPT_MAX_PERSONALITIES + 1];
142 static __inline struct mpt_personality*
143 mpt_pers_find(struct mpt_softc *, u_int);
144 static __inline struct mpt_personality*
145 mpt_pers_find_reverse(struct mpt_softc *, u_int);
147 static __inline struct mpt_personality *
148 mpt_pers_find(struct mpt_softc *mpt, u_int start_at)
150 KASSERT(start_at <= MPT_MAX_PERSONALITIES,
151 ("mpt_pers_find: starting position out of range"));
153 while (start_at < MPT_MAX_PERSONALITIES
154 && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) {
157 return (mpt_personalities[start_at]);
161 * Used infrequently, so no need to optimize like a forward
162 * traversal where we use the MAX+1 is guaranteed to be NULL
165 static __inline struct mpt_personality *
166 mpt_pers_find_reverse(struct mpt_softc *mpt, u_int start_at)
168 while (start_at < MPT_MAX_PERSONALITIES
169 && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) {
172 if (start_at < MPT_MAX_PERSONALITIES)
173 return (mpt_personalities[start_at]);
177 #define MPT_PERS_FOREACH(mpt, pers) \
178 for (pers = mpt_pers_find(mpt, /*start_at*/0); \
180 pers = mpt_pers_find(mpt, /*start_at*/pers->id+1))
182 #define MPT_PERS_FOREACH_REVERSE(mpt, pers) \
183 for (pers = mpt_pers_find_reverse(mpt, MPT_MAX_PERSONALITIES-1);\
185 pers = mpt_pers_find_reverse(mpt, /*start_at*/pers->id-1))
187 static mpt_load_handler_t mpt_stdload;
188 static mpt_probe_handler_t mpt_stdprobe;
189 static mpt_attach_handler_t mpt_stdattach;
190 static mpt_enable_handler_t mpt_stdenable;
191 static mpt_ready_handler_t mpt_stdready;
192 static mpt_event_handler_t mpt_stdevent;
193 static mpt_reset_handler_t mpt_stdreset;
194 static mpt_shutdown_handler_t mpt_stdshutdown;
195 static mpt_detach_handler_t mpt_stddetach;
196 static mpt_unload_handler_t mpt_stdunload;
197 static struct mpt_personality mpt_default_personality =
200 .probe = mpt_stdprobe,
201 .attach = mpt_stdattach,
202 .enable = mpt_stdenable,
203 .ready = mpt_stdready,
204 .event = mpt_stdevent,
205 .reset = mpt_stdreset,
206 .shutdown = mpt_stdshutdown,
207 .detach = mpt_stddetach,
208 .unload = mpt_stdunload
211 static mpt_load_handler_t mpt_core_load;
212 static mpt_attach_handler_t mpt_core_attach;
213 static mpt_enable_handler_t mpt_core_enable;
214 static mpt_reset_handler_t mpt_core_ioc_reset;
215 static mpt_event_handler_t mpt_core_event;
216 static mpt_shutdown_handler_t mpt_core_shutdown;
217 static mpt_shutdown_handler_t mpt_core_detach;
218 static mpt_unload_handler_t mpt_core_unload;
219 static struct mpt_personality mpt_core_personality =
222 .load = mpt_core_load,
223 // .attach = mpt_core_attach,
224 // .enable = mpt_core_enable,
225 .event = mpt_core_event,
226 .reset = mpt_core_ioc_reset,
227 .shutdown = mpt_core_shutdown,
228 .detach = mpt_core_detach,
229 .unload = mpt_core_unload,
233 * Manual declaration so that DECLARE_MPT_PERSONALITY doesn't need
234 * ordering information. We want the core to always register FIRST.
235 * other modules are set to SI_ORDER_SECOND.
237 static moduledata_t mpt_core_mod = {
238 "mpt_core", mpt_modevent, &mpt_core_personality
240 DECLARE_MODULE(mpt_core, mpt_core_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
241 MODULE_VERSION(mpt_core, 1);
243 #define MPT_PERS_ATTACHED(pers, mpt) ((mpt)->mpt_pers_mask & (0x1 << pers->id))
246 mpt_modevent(module_t mod, int type, void *data)
248 struct mpt_personality *pers;
251 pers = (struct mpt_personality *)data;
257 mpt_load_handler_t **def_handler;
258 mpt_load_handler_t **pers_handler;
261 for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
262 if (mpt_personalities[i] == NULL)
265 if (i >= MPT_MAX_PERSONALITIES) {
270 mpt_personalities[i] = pers;
272 /* Install standard/noop handlers for any NULL entries. */
273 def_handler = MPT_PERS_FIRST_HANDLER(&mpt_default_personality);
274 pers_handler = MPT_PERS_FIRST_HANDLER(pers);
275 while (pers_handler <= MPT_PERS_LAST_HANDLER(pers)) {
276 if (*pers_handler == NULL)
277 *pers_handler = *def_handler;
282 error = (pers->load(pers));
284 mpt_personalities[i] = NULL;
289 #if __FreeBSD_version >= 500000
294 error = pers->unload(pers);
295 mpt_personalities[pers->id] = NULL;
305 mpt_stdload(struct mpt_personality *pers)
308 /* Load is always successful. */
313 mpt_stdprobe(struct mpt_softc *mpt)
316 /* Probe is always successful. */
321 mpt_stdattach(struct mpt_softc *mpt)
324 /* Attach is always successful. */
329 mpt_stdenable(struct mpt_softc *mpt)
332 /* Enable is always successful. */
337 mpt_stdready(struct mpt_softc *mpt)
343 mpt_stdevent(struct mpt_softc *mpt, request_t *req, MSG_EVENT_NOTIFY_REPLY *msg)
346 mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_stdevent: 0x%x\n", msg->Event & 0xFF);
347 /* Event was not for us. */
352 mpt_stdreset(struct mpt_softc *mpt, int type)
358 mpt_stdshutdown(struct mpt_softc *mpt)
364 mpt_stddetach(struct mpt_softc *mpt)
370 mpt_stdunload(struct mpt_personality *pers)
373 /* Unload is always successful. */
378 * Post driver attachment, we may want to perform some global actions.
379 * Here is the hook to do so.
383 mpt_postattach(void *unused)
385 struct mpt_softc *mpt;
386 struct mpt_personality *pers;
388 TAILQ_FOREACH(mpt, &mpt_tailq, links) {
389 MPT_PERS_FOREACH(mpt, pers)
393 SYSINIT(mptdev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, mpt_postattach, NULL);
395 /******************************* Bus DMA Support ******************************/
397 mpt_map_rquest(void *arg, bus_dma_segment_t *segs, int nseg, int error)
399 struct mpt_map_info *map_info;
401 map_info = (struct mpt_map_info *)arg;
402 map_info->error = error;
403 map_info->phys = segs->ds_addr;
406 /**************************** Reply/Event Handling ****************************/
408 mpt_register_handler(struct mpt_softc *mpt, mpt_handler_type type,
409 mpt_handler_t handler, uint32_t *phandler_id)
413 case MPT_HANDLER_REPLY:
418 if (phandler_id == NULL)
421 free_cbi = MPT_HANDLER_ID_NONE;
422 for (cbi = 0; cbi < MPT_NUM_REPLY_HANDLERS; cbi++) {
424 * If the same handler is registered multiple
425 * times, don't error out. Just return the
426 * index of the original registration.
428 if (mpt_reply_handlers[cbi] == handler.reply_handler) {
429 *phandler_id = MPT_CBI_TO_HID(cbi);
434 * Fill from the front in the hope that
435 * all registered handlers consume only a
438 * We don't break on the first empty slot so
439 * that the full table is checked to see if
440 * this handler was previously registered.
442 if (free_cbi == MPT_HANDLER_ID_NONE &&
443 (mpt_reply_handlers[cbi]
444 == mpt_default_reply_handler))
447 if (free_cbi == MPT_HANDLER_ID_NONE) {
450 mpt_reply_handlers[free_cbi] = handler.reply_handler;
451 *phandler_id = MPT_CBI_TO_HID(free_cbi);
455 mpt_prt(mpt, "mpt_register_handler unknown type %d\n", type);
462 mpt_deregister_handler(struct mpt_softc *mpt, mpt_handler_type type,
463 mpt_handler_t handler, uint32_t handler_id)
467 case MPT_HANDLER_REPLY:
471 cbi = MPT_CBI(handler_id);
472 if (cbi >= MPT_NUM_REPLY_HANDLERS
473 || mpt_reply_handlers[cbi] != handler.reply_handler)
475 mpt_reply_handlers[cbi] = mpt_default_reply_handler;
479 mpt_prt(mpt, "mpt_deregister_handler unknown type %d\n", type);
486 mpt_default_reply_handler(struct mpt_softc *mpt, request_t *req,
487 uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
491 "Default Handler Called: req=%p:%u reply_descriptor=%x frame=%p\n",
492 req, req->serno, reply_desc, reply_frame);
494 if (reply_frame != NULL)
495 mpt_dump_reply_frame(mpt, reply_frame);
497 mpt_prt(mpt, "Reply Frame Ignored\n");
499 return (/*free_reply*/TRUE);
503 mpt_config_reply_handler(struct mpt_softc *mpt, request_t *req,
504 uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
508 if (reply_frame != NULL) {
510 MSG_CONFIG_REPLY *reply;
512 cfgp = (MSG_CONFIG *)req->req_vbuf;
513 reply = (MSG_CONFIG_REPLY *)reply_frame;
514 req->IOCStatus = le16toh(reply_frame->IOCStatus);
515 bcopy(&reply->Header, &cfgp->Header,
516 sizeof(cfgp->Header));
517 cfgp->ExtPageLength = reply->ExtPageLength;
518 cfgp->ExtPageType = reply->ExtPageType;
520 req->state &= ~REQ_STATE_QUEUED;
521 req->state |= REQ_STATE_DONE;
522 TAILQ_REMOVE(&mpt->request_pending_list, req, links);
523 if ((req->state & REQ_STATE_NEED_WAKEUP) != 0) {
525 } else if ((req->state & REQ_STATE_TIMEDOUT) != 0) {
527 * Whew- we can free this request (late completion)
529 mpt_free_request(mpt, req);
537 mpt_handshake_reply_handler(struct mpt_softc *mpt, request_t *req,
538 uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
541 /* Nothing to be done. */
546 mpt_event_reply_handler(struct mpt_softc *mpt, request_t *req,
547 uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
551 KASSERT(reply_frame != NULL, ("null reply in mpt_event_reply_handler"));
552 KASSERT(req != NULL, ("null request in mpt_event_reply_handler"));
555 switch (reply_frame->Function) {
556 case MPI_FUNCTION_EVENT_NOTIFICATION:
558 MSG_EVENT_NOTIFY_REPLY *msg;
559 struct mpt_personality *pers;
563 msg = (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
564 msg->EventDataLength = le16toh(msg->EventDataLength);
565 msg->IOCStatus = le16toh(msg->IOCStatus);
566 msg->IOCLogInfo = le32toh(msg->IOCLogInfo);
567 msg->Event = le32toh(msg->Event);
568 MPT_PERS_FOREACH(mpt, pers)
569 handled += pers->event(mpt, req, msg);
571 if (handled == 0 && mpt->mpt_pers_mask == 0) {
572 mpt_lprt(mpt, MPT_PRT_INFO,
573 "No Handlers For Any Event Notify Frames. "
574 "Event %#x (ACK %sequired).\n",
575 msg->Event, msg->AckRequired? "r" : "not r");
576 } else if (handled == 0) {
578 msg->AckRequired? MPT_PRT_WARN : MPT_PRT_INFO,
579 "Unhandled Event Notify Frame. Event %#x "
580 "(ACK %sequired).\n",
581 msg->Event, msg->AckRequired? "r" : "not r");
584 if (msg->AckRequired) {
588 context = req->index | MPT_REPLY_HANDLER_EVENTS;
589 ack_req = mpt_get_request(mpt, FALSE);
590 if (ack_req == NULL) {
591 struct mpt_evtf_record *evtf;
593 evtf = (struct mpt_evtf_record *)reply_frame;
594 evtf->context = context;
595 LIST_INSERT_HEAD(&mpt->ack_frames, evtf, links);
599 mpt_send_event_ack(mpt, ack_req, msg, context);
601 * Don't check for CONTINUATION_REPLY here
607 case MPI_FUNCTION_PORT_ENABLE:
608 mpt_lprt(mpt, MPT_PRT_DEBUG , "enable port reply\n");
610 case MPI_FUNCTION_EVENT_ACK:
613 mpt_prt(mpt, "unknown event function: %x\n",
614 reply_frame->Function);
619 * I'm not sure that this continuation stuff works as it should.
621 * I've had FC async events occur that free the frame up because
622 * the continuation bit isn't set, and then additional async events
623 * then occur using the same context. As you might imagine, this
624 * leads to Very Bad Thing.
626 * Let's just be safe for now and not free them up until we figure
627 * out what's actually happening here.
630 if ((reply_frame->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY) == 0) {
631 TAILQ_REMOVE(&mpt->request_pending_list, req, links);
632 mpt_free_request(mpt, req);
633 mpt_prt(mpt, "event_reply %x for req %p:%u NOT a continuation",
634 reply_frame->Function, req, req->serno);
635 if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) {
636 MSG_EVENT_NOTIFY_REPLY *msg =
637 (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
638 mpt_prtc(mpt, " Event=0x%x AckReq=%d",
639 msg->Event, msg->AckRequired);
642 mpt_prt(mpt, "event_reply %x for %p:%u IS a continuation",
643 reply_frame->Function, req, req->serno);
644 if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) {
645 MSG_EVENT_NOTIFY_REPLY *msg =
646 (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
647 mpt_prtc(mpt, " Event=0x%x AckReq=%d",
648 msg->Event, msg->AckRequired);
657 * Process an asynchronous event from the IOC.
660 mpt_core_event(struct mpt_softc *mpt, request_t *req,
661 MSG_EVENT_NOTIFY_REPLY *msg)
664 mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_core_event: 0x%x\n",
666 switch(msg->Event & 0xFF) {
669 case MPI_EVENT_LOG_DATA:
673 /* Some error occurred that LSI wants logged */
674 mpt_prt(mpt, "EvtLogData: IOCLogInfo: 0x%08x\n",
676 mpt_prt(mpt, "\tEvtLogData: Event Data:");
677 for (i = 0; i < msg->EventDataLength; i++)
678 mpt_prtc(mpt, " %08x", msg->Data[i]);
682 case MPI_EVENT_EVENT_CHANGE:
684 * This is just an acknowledgement
685 * of our mpt_send_event_request.
688 case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
698 mpt_send_event_ack(struct mpt_softc *mpt, request_t *ack_req,
699 MSG_EVENT_NOTIFY_REPLY *msg, uint32_t context)
703 ackp = (MSG_EVENT_ACK *)ack_req->req_vbuf;
704 memset(ackp, 0, sizeof (*ackp));
705 ackp->Function = MPI_FUNCTION_EVENT_ACK;
706 ackp->Event = htole32(msg->Event);
707 ackp->EventContext = htole32(msg->EventContext);
708 ackp->MsgContext = htole32(context);
709 mpt_check_doorbell(mpt);
710 mpt_send_cmd(mpt, ack_req);
713 /***************************** Interrupt Handling *****************************/
717 struct mpt_softc *mpt;
721 mpt = (struct mpt_softc *)arg;
722 mpt_lprt(mpt, MPT_PRT_DEBUG2, "enter mpt_intr\n");
723 MPT_LOCK_ASSERT(mpt);
725 while ((reply_desc = mpt_pop_reply_queue(mpt)) != MPT_REPLY_EMPTY) {
727 MSG_DEFAULT_REPLY *reply_frame;
728 uint32_t reply_baddr;
739 if ((reply_desc & MPI_ADDRESS_REPLY_A_BIT) != 0) {
741 * Ensure that the reply frame is coherent.
743 reply_baddr = MPT_REPLY_BADDR(reply_desc);
744 offset = reply_baddr - (mpt->reply_phys & 0xFFFFFFFF);
745 bus_dmamap_sync_range(mpt->reply_dmat,
746 mpt->reply_dmap, offset, MPT_REPLY_SIZE,
747 BUS_DMASYNC_POSTREAD);
748 reply_frame = MPT_REPLY_OTOV(mpt, offset);
749 ctxt_idx = le32toh(reply_frame->MsgContext);
753 type = MPI_GET_CONTEXT_REPLY_TYPE(reply_desc);
754 ctxt_idx = reply_desc;
755 mpt_lprt(mpt, MPT_PRT_DEBUG1, "Context Reply: 0x%08x\n",
759 case MPI_CONTEXT_REPLY_TYPE_SCSI_INIT:
760 ctxt_idx &= MPI_CONTEXT_REPLY_CONTEXT_MASK;
762 case MPI_CONTEXT_REPLY_TYPE_SCSI_TARGET:
763 ctxt_idx = GET_IO_INDEX(reply_desc);
764 if (mpt->tgt_cmd_ptrs == NULL) {
766 "mpt_intr: no target cmd ptrs\n");
767 reply_desc = MPT_REPLY_EMPTY;
770 if (ctxt_idx >= mpt->tgt_cmds_allocated) {
772 "mpt_intr: bad tgt cmd ctxt %u\n",
774 reply_desc = MPT_REPLY_EMPTY;
778 req = mpt->tgt_cmd_ptrs[ctxt_idx];
780 mpt_prt(mpt, "no request backpointer "
781 "at index %u", ctxt_idx);
782 reply_desc = MPT_REPLY_EMPTY;
787 * Reformulate ctxt_idx to be just as if
788 * it were another type of context reply
789 * so the code below will find the request
790 * via indexing into the pool.
793 req->index | mpt->scsi_tgt_handler_id;
796 case MPI_CONTEXT_REPLY_TYPE_LAN:
797 mpt_prt(mpt, "LAN CONTEXT REPLY: 0x%08x\n",
799 reply_desc = MPT_REPLY_EMPTY;
802 mpt_prt(mpt, "Context Reply 0x%08x?\n", type);
803 reply_desc = MPT_REPLY_EMPTY;
806 if (reply_desc == MPT_REPLY_EMPTY) {
807 if (ntrips++ > 1000) {
814 cb_index = MPT_CONTEXT_TO_CBI(ctxt_idx);
815 req_index = MPT_CONTEXT_TO_REQI(ctxt_idx);
816 if (req_index < MPT_MAX_REQUESTS(mpt)) {
817 req = &mpt->request_pool[req_index];
819 mpt_prt(mpt, "WARN: mpt_intr index == %d (reply_desc =="
820 " 0x%x)\n", req_index, reply_desc);
823 bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
824 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
825 free_rf = mpt_reply_handlers[cb_index](mpt, req,
826 reply_desc, reply_frame);
828 if (reply_frame != NULL && free_rf) {
829 bus_dmamap_sync_range(mpt->reply_dmat,
830 mpt->reply_dmap, offset, MPT_REPLY_SIZE,
831 BUS_DMASYNC_PREREAD);
832 mpt_free_reply(mpt, reply_baddr);
836 * If we got ourselves disabled, don't get stuck in a loop
839 mpt_disable_ints(mpt);
842 if (ntrips++ > 1000) {
846 mpt_lprt(mpt, MPT_PRT_DEBUG2, "exit mpt_intr\n");
849 /******************************* Error Recovery *******************************/
851 mpt_complete_request_chain(struct mpt_softc *mpt, struct req_queue *chain,
854 MSG_DEFAULT_REPLY ioc_status_frame;
857 memset(&ioc_status_frame, 0, sizeof(ioc_status_frame));
858 ioc_status_frame.MsgLength = roundup2(sizeof(ioc_status_frame), 4);
859 ioc_status_frame.IOCStatus = iocstatus;
860 while((req = TAILQ_FIRST(chain)) != NULL) {
861 MSG_REQUEST_HEADER *msg_hdr;
864 bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
865 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
866 msg_hdr = (MSG_REQUEST_HEADER *)req->req_vbuf;
867 ioc_status_frame.Function = msg_hdr->Function;
868 ioc_status_frame.MsgContext = msg_hdr->MsgContext;
869 cb_index = MPT_CONTEXT_TO_CBI(le32toh(msg_hdr->MsgContext));
870 mpt_reply_handlers[cb_index](mpt, req, msg_hdr->MsgContext,
872 if (mpt_req_on_pending_list(mpt, req) != 0)
873 TAILQ_REMOVE(chain, req, links);
877 /********************************* Diagnostics ********************************/
879 * Perform a diagnostic dump of a reply frame.
882 mpt_dump_reply_frame(struct mpt_softc *mpt, MSG_DEFAULT_REPLY *reply_frame)
885 mpt_prt(mpt, "Address Reply:\n");
886 mpt_print_reply(reply_frame);
889 /******************************* Doorbell Access ******************************/
890 static __inline uint32_t mpt_rd_db(struct mpt_softc *mpt);
891 static __inline uint32_t mpt_rd_intr(struct mpt_softc *mpt);
893 static __inline uint32_t
894 mpt_rd_db(struct mpt_softc *mpt)
897 return mpt_read(mpt, MPT_OFFSET_DOORBELL);
900 static __inline uint32_t
901 mpt_rd_intr(struct mpt_softc *mpt)
904 return mpt_read(mpt, MPT_OFFSET_INTR_STATUS);
907 /* Busy wait for a door bell to be read by IOC */
909 mpt_wait_db_ack(struct mpt_softc *mpt)
913 for (i=0; i < MPT_MAX_WAIT; i++) {
914 if (!MPT_DB_IS_BUSY(mpt_rd_intr(mpt))) {
915 maxwait_ack = i > maxwait_ack ? i : maxwait_ack;
923 /* Busy wait for a door bell interrupt */
925 mpt_wait_db_int(struct mpt_softc *mpt)
929 for (i = 0; i < MPT_MAX_WAIT; i++) {
930 if (MPT_DB_INTR(mpt_rd_intr(mpt))) {
931 maxwait_int = i > maxwait_int ? i : maxwait_int;
939 /* Wait for IOC to transition to a give state */
941 mpt_check_doorbell(struct mpt_softc *mpt)
943 uint32_t db = mpt_rd_db(mpt);
945 if (MPT_STATE(db) != MPT_DB_STATE_RUNNING) {
946 mpt_prt(mpt, "Device not running\n");
951 /* Wait for IOC to transition to a give state */
953 mpt_wait_state(struct mpt_softc *mpt, enum DB_STATE_BITS state)
957 for (i = 0; i < MPT_MAX_WAIT; i++) {
958 uint32_t db = mpt_rd_db(mpt);
959 if (MPT_STATE(db) == state) {
960 maxwait_state = i > maxwait_state ? i : maxwait_state;
969 /************************* Intialization/Configuration ************************/
970 static int mpt_download_fw(struct mpt_softc *mpt);
972 /* Issue the reset COMMAND to the IOC */
974 mpt_soft_reset(struct mpt_softc *mpt)
977 mpt_lprt(mpt, MPT_PRT_DEBUG, "soft reset\n");
979 /* Have to use hard reset if we are not in Running state */
980 if (MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_RUNNING) {
981 mpt_prt(mpt, "soft reset failed: device not running\n");
985 /* If door bell is in use we don't have a chance of getting
986 * a word in since the IOC probably crashed in message
987 * processing. So don't waste our time.
989 if (MPT_DB_IS_IN_USE(mpt_rd_db(mpt))) {
990 mpt_prt(mpt, "soft reset failed: doorbell wedged\n");
994 /* Send the reset request to the IOC */
995 mpt_write(mpt, MPT_OFFSET_DOORBELL,
996 MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET << MPI_DOORBELL_FUNCTION_SHIFT);
997 if (mpt_wait_db_ack(mpt) != MPT_OK) {
998 mpt_prt(mpt, "soft reset failed: ack timeout\n");
1002 /* Wait for the IOC to reload and come out of reset state */
1003 if (mpt_wait_state(mpt, MPT_DB_STATE_READY) != MPT_OK) {
1004 mpt_prt(mpt, "soft reset failed: device did not restart\n");
1012 mpt_enable_diag_mode(struct mpt_softc *mpt)
1019 if ((mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC) & MPI_DIAG_DRWE) != 0)
1022 /* Enable diagnostic registers */
1023 mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFF);
1024 mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_1ST_KEY_VALUE);
1025 mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_2ND_KEY_VALUE);
1026 mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_3RD_KEY_VALUE);
1027 mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_4TH_KEY_VALUE);
1028 mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_5TH_KEY_VALUE);
1038 mpt_disable_diag_mode(struct mpt_softc *mpt)
1041 mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFFFFFFFF);
1044 /* This is a magic diagnostic reset that resets all the ARM
1045 * processors in the chip.
1048 mpt_hard_reset(struct mpt_softc *mpt)
1054 mpt_lprt(mpt, MPT_PRT_DEBUG, "hard reset\n");
1057 mpt_write(mpt, MPT_OFFSET_RESET_1078, 0x07);
1062 error = mpt_enable_diag_mode(mpt);
1064 mpt_prt(mpt, "WARNING - Could not enter diagnostic mode !\n");
1065 mpt_prt(mpt, "Trying to reset anyway.\n");
1068 diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
1071 * This appears to be a workaround required for some
1072 * firmware or hardware revs.
1074 mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_DISABLE_ARM);
1077 /* Diag. port is now active so we can now hit the reset bit */
1078 mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_RESET_ADAPTER);
1081 * Ensure that the reset has finished. We delay 1ms
1082 * prior to reading the register to make sure the chip
1083 * has sufficiently completed its reset to handle register
1089 diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
1090 } while (--wait && (diagreg & MPI_DIAG_RESET_ADAPTER) == 0);
1093 mpt_prt(mpt, "WARNING - Failed hard reset! "
1094 "Trying to initialize anyway.\n");
1098 * If we have firmware to download, it must be loaded before
1099 * the controller will become operational. Do so now.
1101 if (mpt->fw_image != NULL) {
1103 error = mpt_download_fw(mpt);
1106 mpt_prt(mpt, "WARNING - Firmware Download Failed!\n");
1107 mpt_prt(mpt, "Trying to initialize anyway.\n");
1112 * Reseting the controller should have disabled write
1113 * access to the diagnostic registers, but disable
1114 * manually to be sure.
1116 mpt_disable_diag_mode(mpt);
1120 mpt_core_ioc_reset(struct mpt_softc *mpt, int type)
1124 * Complete all pending requests with a status
1125 * appropriate for an IOC reset.
1127 mpt_complete_request_chain(mpt, &mpt->request_pending_list,
1128 MPI_IOCSTATUS_INVALID_STATE);
1132 * Reset the IOC when needed. Try software command first then if needed
1133 * poke at the magic diagnostic reset. Note that a hard reset resets
1134 * *both* IOCs on dual function chips (FC929 && LSI1030) as well as
1135 * fouls up the PCI configuration registers.
1138 mpt_reset(struct mpt_softc *mpt, int reinit)
1140 struct mpt_personality *pers;
1145 * Try a soft reset. If that fails, get out the big hammer.
1148 if ((ret = mpt_soft_reset(mpt)) != MPT_OK) {
1150 for (cnt = 0; cnt < 5; cnt++) {
1151 /* Failed; do a hard reset */
1152 mpt_hard_reset(mpt);
1155 * Wait for the IOC to reload
1156 * and come out of reset state
1158 ret = mpt_wait_state(mpt, MPT_DB_STATE_READY);
1159 if (ret == MPT_OK) {
1163 * Okay- try to check again...
1165 ret = mpt_wait_state(mpt, MPT_DB_STATE_READY);
1166 if (ret == MPT_OK) {
1169 mpt_prt(mpt, "mpt_reset: failed hard reset (%d:%d)\n",
1174 if (retry_cnt == 0) {
1176 * Invoke reset handlers. We bump the reset count so
1177 * that mpt_wait_req() understands that regardless of
1178 * the specified wait condition, it should stop its wait.
1181 MPT_PERS_FOREACH(mpt, pers)
1182 pers->reset(mpt, ret);
1186 ret = mpt_enable_ioc(mpt, 1);
1187 if (ret == MPT_OK) {
1188 mpt_enable_ints(mpt);
1191 if (ret != MPT_OK && retry_cnt++ < 2) {
1197 /* Return a command buffer to the free queue */
1199 mpt_free_request(struct mpt_softc *mpt, request_t *req)
1202 struct mpt_evtf_record *record;
1203 uint32_t offset, reply_baddr;
1205 if (req == NULL || req != &mpt->request_pool[req->index]) {
1206 panic("mpt_free_request: bad req ptr");
1208 if ((nxt = req->chain) != NULL) {
1210 mpt_free_request(mpt, nxt); /* NB: recursion */
1212 KASSERT(req->state != REQ_STATE_FREE, ("freeing free request"));
1213 KASSERT(!(req->state & REQ_STATE_LOCKED), ("freeing locked request"));
1214 MPT_LOCK_ASSERT(mpt);
1215 KASSERT(mpt_req_on_free_list(mpt, req) == 0,
1216 ("mpt_free_request: req %p:%u func %x already on freelist",
1217 req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1218 KASSERT(mpt_req_on_pending_list(mpt, req) == 0,
1219 ("mpt_free_request: req %p:%u func %x on pending list",
1220 req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1222 mpt_req_not_spcl(mpt, req, "mpt_free_request", __LINE__);
1226 if (LIST_EMPTY(&mpt->ack_frames)) {
1228 * Insert free ones at the tail
1231 req->state = REQ_STATE_FREE;
1233 memset(req->req_vbuf, 0xff, sizeof (MSG_REQUEST_HEADER));
1235 TAILQ_INSERT_TAIL(&mpt->request_free_list, req, links);
1236 if (mpt->getreqwaiter != 0) {
1237 mpt->getreqwaiter = 0;
1238 wakeup(&mpt->request_free_list);
1244 * Process an ack frame deferred due to resource shortage.
1246 record = LIST_FIRST(&mpt->ack_frames);
1247 LIST_REMOVE(record, links);
1248 req->state = REQ_STATE_ALLOCATED;
1249 mpt_assign_serno(mpt, req);
1250 mpt_send_event_ack(mpt, req, &record->reply, record->context);
1251 offset = (uint32_t)((uint8_t *)record - mpt->reply);
1252 reply_baddr = offset + (mpt->reply_phys & 0xFFFFFFFF);
1253 bus_dmamap_sync_range(mpt->reply_dmat, mpt->reply_dmap, offset,
1254 MPT_REPLY_SIZE, BUS_DMASYNC_PREREAD);
1255 mpt_free_reply(mpt, reply_baddr);
1258 /* Get a command buffer from the free queue */
1260 mpt_get_request(struct mpt_softc *mpt, int sleep_ok)
1265 MPT_LOCK_ASSERT(mpt);
1266 req = TAILQ_FIRST(&mpt->request_free_list);
1268 KASSERT(req == &mpt->request_pool[req->index],
1269 ("mpt_get_request: corrupted request free list"));
1270 KASSERT(req->state == REQ_STATE_FREE,
1271 ("req %p:%u not free on free list %x index %d function %x",
1272 req, req->serno, req->state, req->index,
1273 ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1274 TAILQ_REMOVE(&mpt->request_free_list, req, links);
1275 req->state = REQ_STATE_ALLOCATED;
1277 mpt_assign_serno(mpt, req);
1278 } else if (sleep_ok != 0) {
1279 mpt->getreqwaiter = 1;
1280 mpt_sleep(mpt, &mpt->request_free_list, PUSER, "mptgreq", 0);
1286 /* Pass the command to the IOC */
1288 mpt_send_cmd(struct mpt_softc *mpt, request_t *req)
1291 if (mpt->verbose > MPT_PRT_DEBUG2) {
1292 mpt_dump_request(mpt, req);
1294 bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
1295 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1296 req->state |= REQ_STATE_QUEUED;
1297 KASSERT(mpt_req_on_free_list(mpt, req) == 0,
1298 ("req %p:%u func %x on freelist list in mpt_send_cmd",
1299 req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1300 KASSERT(mpt_req_on_pending_list(mpt, req) == 0,
1301 ("req %p:%u func %x already on pending list in mpt_send_cmd",
1302 req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1303 TAILQ_INSERT_HEAD(&mpt->request_pending_list, req, links);
1304 mpt_write(mpt, MPT_OFFSET_REQUEST_Q, (uint32_t) req->req_pbuf);
1308 * Wait for a request to complete.
1311 * mpt softc of controller executing request
1312 * req request to wait for
1313 * sleep_ok nonzero implies may sleep in this context
1314 * time_ms timeout in ms. 0 implies no timeout.
1317 * 0 Request completed
1318 * non-0 Timeout fired before request completion.
1321 mpt_wait_req(struct mpt_softc *mpt, request_t *req,
1322 mpt_req_state_t state, mpt_req_state_t mask,
1323 int sleep_ok, int time_ms)
1330 * timeout is in ms. 0 indicates infinite wait.
1331 * Convert to ticks or 500us units depending on
1334 if (sleep_ok != 0) {
1335 timeout = (time_ms * hz) / 1000;
1337 timeout = time_ms * 2;
1339 req->state |= REQ_STATE_NEED_WAKEUP;
1340 mask &= ~REQ_STATE_NEED_WAKEUP;
1341 saved_cnt = mpt->reset_cnt;
1342 while ((req->state & mask) != state && mpt->reset_cnt == saved_cnt) {
1343 if (sleep_ok != 0) {
1344 error = mpt_sleep(mpt, req, PUSER, "mptreq", timeout);
1345 if (error == EWOULDBLOCK) {
1350 if (time_ms != 0 && --timeout == 0) {
1357 req->state &= ~REQ_STATE_NEED_WAKEUP;
1358 if (mpt->reset_cnt != saved_cnt) {
1361 if (time_ms && timeout <= 0) {
1362 MSG_REQUEST_HEADER *msg_hdr = req->req_vbuf;
1363 req->state |= REQ_STATE_TIMEDOUT;
1364 mpt_prt(mpt, "mpt_wait_req(%x) timed out\n", msg_hdr->Function);
1371 * Send a command to the IOC via the handshake register.
1373 * Only done at initialization time and for certain unusual
1374 * commands such as device/bus reset as specified by LSI.
1377 mpt_send_handshake_cmd(struct mpt_softc *mpt, size_t len, void *cmd)
1380 uint32_t data, *data32;
1382 /* Check condition of the IOC */
1383 data = mpt_rd_db(mpt);
1384 if ((MPT_STATE(data) != MPT_DB_STATE_READY
1385 && MPT_STATE(data) != MPT_DB_STATE_RUNNING
1386 && MPT_STATE(data) != MPT_DB_STATE_FAULT)
1387 || MPT_DB_IS_IN_USE(data)) {
1388 mpt_prt(mpt, "handshake aborted - invalid doorbell state\n");
1393 /* We move things in 32 bit chunks */
1394 len = (len + 3) >> 2;
1397 /* Clear any left over pending doorbell interrupts */
1398 if (MPT_DB_INTR(mpt_rd_intr(mpt)))
1399 mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1402 * Tell the handshake reg. we are going to send a command
1403 * and how long it is going to be.
1405 data = (MPI_FUNCTION_HANDSHAKE << MPI_DOORBELL_FUNCTION_SHIFT) |
1406 (len << MPI_DOORBELL_ADD_DWORDS_SHIFT);
1407 mpt_write(mpt, MPT_OFFSET_DOORBELL, data);
1409 /* Wait for the chip to notice */
1410 if (mpt_wait_db_int(mpt) != MPT_OK) {
1411 mpt_prt(mpt, "mpt_send_handshake_cmd: db ignored\n");
1415 /* Clear the interrupt */
1416 mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1418 if (mpt_wait_db_ack(mpt) != MPT_OK) {
1419 mpt_prt(mpt, "mpt_send_handshake_cmd: db ack timed out\n");
1423 /* Send the command */
1424 for (i = 0; i < len; i++) {
1425 mpt_write(mpt, MPT_OFFSET_DOORBELL, htole32(*data32++));
1426 if (mpt_wait_db_ack(mpt) != MPT_OK) {
1428 "mpt_send_handshake_cmd: timeout @ index %d\n", i);
1435 /* Get the response from the handshake register */
1437 mpt_recv_handshake_reply(struct mpt_softc *mpt, size_t reply_len, void *reply)
1439 int left, reply_left;
1442 MSG_DEFAULT_REPLY *hdr;
1444 /* We move things out in 16 bit chunks */
1446 data16 = (u_int16_t *)reply;
1448 hdr = (MSG_DEFAULT_REPLY *)reply;
1450 /* Get first word */
1451 if (mpt_wait_db_int(mpt) != MPT_OK) {
1452 mpt_prt(mpt, "mpt_recv_handshake_cmd timeout1\n");
1455 data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
1456 *data16++ = le16toh(data & MPT_DB_DATA_MASK);
1457 mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1459 /* Get Second Word */
1460 if (mpt_wait_db_int(mpt) != MPT_OK) {
1461 mpt_prt(mpt, "mpt_recv_handshake_cmd timeout2\n");
1464 data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
1465 *data16++ = le16toh(data & MPT_DB_DATA_MASK);
1466 mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1469 * With the second word, we can now look at the length.
1470 * Warn about a reply that's too short (except for IOC FACTS REPLY)
1472 if ((reply_len >> 1) != hdr->MsgLength &&
1473 (hdr->Function != MPI_FUNCTION_IOC_FACTS)){
1474 #if __FreeBSD_version >= 500000
1475 mpt_prt(mpt, "reply length does not match message length: "
1476 "got %x; expected %zx for function %x\n",
1477 hdr->MsgLength << 2, reply_len << 1, hdr->Function);
1479 mpt_prt(mpt, "reply length does not match message length: "
1480 "got %x; expected %x for function %x\n",
1481 hdr->MsgLength << 2, reply_len << 1, hdr->Function);
1485 /* Get rest of the reply; but don't overflow the provided buffer */
1486 left = (hdr->MsgLength << 1) - 2;
1487 reply_left = reply_len - 2;
1491 if (mpt_wait_db_int(mpt) != MPT_OK) {
1492 mpt_prt(mpt, "mpt_recv_handshake_cmd timeout3\n");
1495 data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
1496 datum = le16toh(data & MPT_DB_DATA_MASK);
1498 if (reply_left-- > 0)
1501 mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1504 /* One more wait & clear at the end */
1505 if (mpt_wait_db_int(mpt) != MPT_OK) {
1506 mpt_prt(mpt, "mpt_recv_handshake_cmd timeout4\n");
1509 mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1511 if ((hdr->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1512 if (mpt->verbose >= MPT_PRT_TRACE)
1513 mpt_print_reply(hdr);
1514 return (MPT_FAIL | hdr->IOCStatus);
1521 mpt_get_iocfacts(struct mpt_softc *mpt, MSG_IOC_FACTS_REPLY *freplp)
1523 MSG_IOC_FACTS f_req;
1526 memset(&f_req, 0, sizeof f_req);
1527 f_req.Function = MPI_FUNCTION_IOC_FACTS;
1528 f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
1529 error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req);
1533 error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp);
1538 mpt_get_portfacts(struct mpt_softc *mpt, U8 port, MSG_PORT_FACTS_REPLY *freplp)
1540 MSG_PORT_FACTS f_req;
1543 memset(&f_req, 0, sizeof f_req);
1544 f_req.Function = MPI_FUNCTION_PORT_FACTS;
1545 f_req.PortNumber = port;
1546 f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
1547 error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req);
1551 error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp);
1556 * Send the initialization request. This is where we specify how many
1557 * SCSI busses and how many devices per bus we wish to emulate.
1558 * This is also the command that specifies the max size of the reply
1559 * frames from the IOC that we will be allocating.
1562 mpt_send_ioc_init(struct mpt_softc *mpt, uint32_t who)
1566 MSG_IOC_INIT_REPLY reply;
1568 memset(&init, 0, sizeof init);
1570 init.Function = MPI_FUNCTION_IOC_INIT;
1571 init.MaxDevices = 0; /* at least 256 devices per bus */
1572 init.MaxBuses = 16; /* at least 16 busses */
1574 init.MsgVersion = htole16(MPI_VERSION);
1575 init.HeaderVersion = htole16(MPI_HEADER_VERSION);
1576 init.ReplyFrameSize = htole16(MPT_REPLY_SIZE);
1577 init.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
1579 if ((error = mpt_send_handshake_cmd(mpt, sizeof init, &init)) != 0) {
1583 error = mpt_recv_handshake_reply(mpt, sizeof reply, &reply);
1589 * Utiltity routine to read configuration headers and pages
1592 mpt_issue_cfg_req(struct mpt_softc *mpt, request_t *req, cfgparms_t *params,
1593 bus_addr_t addr, bus_size_t len, int sleep_ok, int timeout_ms)
1598 cfgp = req->req_vbuf;
1599 memset(cfgp, 0, sizeof *cfgp);
1600 cfgp->Action = params->Action;
1601 cfgp->Function = MPI_FUNCTION_CONFIG;
1602 cfgp->Header.PageVersion = params->PageVersion;
1603 cfgp->Header.PageNumber = params->PageNumber;
1604 cfgp->PageAddress = htole32(params->PageAddress);
1605 if ((params->PageType & MPI_CONFIG_PAGETYPE_MASK) ==
1606 MPI_CONFIG_PAGETYPE_EXTENDED) {
1607 cfgp->Header.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1608 cfgp->Header.PageLength = 0;
1609 cfgp->ExtPageLength = htole16(params->ExtPageLength);
1610 cfgp->ExtPageType = params->ExtPageType;
1612 cfgp->Header.PageType = params->PageType;
1613 cfgp->Header.PageLength = params->PageLength;
1615 se = (SGE_SIMPLE32 *)&cfgp->PageBufferSGE;
1616 se->Address = htole32(addr);
1617 MPI_pSGE_SET_LENGTH(se, len);
1618 MPI_pSGE_SET_FLAGS(se, (MPI_SGE_FLAGS_SIMPLE_ELEMENT |
1619 MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER |
1620 MPI_SGE_FLAGS_END_OF_LIST |
1621 ((params->Action == MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT
1622 || params->Action == MPI_CONFIG_ACTION_PAGE_WRITE_NVRAM)
1623 ? MPI_SGE_FLAGS_HOST_TO_IOC : MPI_SGE_FLAGS_IOC_TO_HOST)));
1624 se->FlagsLength = htole32(se->FlagsLength);
1625 cfgp->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG);
1627 mpt_check_doorbell(mpt);
1628 mpt_send_cmd(mpt, req);
1629 return (mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE,
1630 sleep_ok, timeout_ms));
1634 mpt_read_extcfg_header(struct mpt_softc *mpt, int PageVersion, int PageNumber,
1635 uint32_t PageAddress, int ExtPageType,
1636 CONFIG_EXTENDED_PAGE_HEADER *rslt,
1637 int sleep_ok, int timeout_ms)
1641 MSG_CONFIG_REPLY *cfgp;
1644 req = mpt_get_request(mpt, sleep_ok);
1646 mpt_prt(mpt, "mpt_extread_cfg_header: Get request failed!\n");
1650 params.Action = MPI_CONFIG_ACTION_PAGE_HEADER;
1651 params.PageVersion = PageVersion;
1652 params.PageLength = 0;
1653 params.PageNumber = PageNumber;
1654 params.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1655 params.PageAddress = PageAddress;
1656 params.ExtPageType = ExtPageType;
1657 params.ExtPageLength = 0;
1658 error = mpt_issue_cfg_req(mpt, req, ¶ms, /*addr*/0, /*len*/0,
1659 sleep_ok, timeout_ms);
1662 * Leave the request. Without resetting the chip, it's
1663 * still owned by it and we'll just get into trouble
1664 * freeing it now. Mark it as abandoned so that if it
1665 * shows up later it can be freed.
1667 mpt_prt(mpt, "read_extcfg_header timed out\n");
1671 switch (req->IOCStatus & MPI_IOCSTATUS_MASK) {
1672 case MPI_IOCSTATUS_SUCCESS:
1673 cfgp = req->req_vbuf;
1674 rslt->PageVersion = cfgp->Header.PageVersion;
1675 rslt->PageNumber = cfgp->Header.PageNumber;
1676 rslt->PageType = cfgp->Header.PageType;
1677 rslt->ExtPageLength = le16toh(cfgp->ExtPageLength);
1678 rslt->ExtPageType = cfgp->ExtPageType;
1681 case MPI_IOCSTATUS_CONFIG_INVALID_PAGE:
1682 mpt_lprt(mpt, MPT_PRT_DEBUG,
1683 "Invalid Page Type %d Number %d Addr 0x%0x\n",
1684 MPI_CONFIG_PAGETYPE_EXTENDED, PageNumber, PageAddress);
1688 mpt_prt(mpt, "mpt_read_extcfg_header: Config Info Status %x\n",
1693 mpt_free_request(mpt, req);
1698 mpt_read_extcfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
1699 CONFIG_EXTENDED_PAGE_HEADER *hdr, void *buf, size_t len,
1700 int sleep_ok, int timeout_ms)
1706 req = mpt_get_request(mpt, sleep_ok);
1708 mpt_prt(mpt, "mpt_read_extcfg_page: Get request failed!\n");
1712 params.Action = Action;
1713 params.PageVersion = hdr->PageVersion;
1714 params.PageLength = 0;
1715 params.PageNumber = hdr->PageNumber;
1716 params.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1717 params.PageAddress = PageAddress;
1718 params.ExtPageType = hdr->ExtPageType;
1719 params.ExtPageLength = hdr->ExtPageLength;
1720 error = mpt_issue_cfg_req(mpt, req, ¶ms,
1721 req->req_pbuf + MPT_RQSL(mpt),
1722 len, sleep_ok, timeout_ms);
1724 mpt_prt(mpt, "read_extcfg_page(%d) timed out\n", Action);
1728 if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1729 mpt_prt(mpt, "mpt_read_extcfg_page: Config Info Status %x\n",
1731 mpt_free_request(mpt, req);
1734 memcpy(buf, ((uint8_t *)req->req_vbuf)+MPT_RQSL(mpt), len);
1735 mpt_free_request(mpt, req);
1740 mpt_read_cfg_header(struct mpt_softc *mpt, int PageType, int PageNumber,
1741 uint32_t PageAddress, CONFIG_PAGE_HEADER *rslt,
1742 int sleep_ok, int timeout_ms)
1749 req = mpt_get_request(mpt, sleep_ok);
1751 mpt_prt(mpt, "mpt_read_cfg_header: Get request failed!\n");
1755 params.Action = MPI_CONFIG_ACTION_PAGE_HEADER;
1756 params.PageVersion = 0;
1757 params.PageLength = 0;
1758 params.PageNumber = PageNumber;
1759 params.PageType = PageType;
1760 params.PageAddress = PageAddress;
1761 error = mpt_issue_cfg_req(mpt, req, ¶ms, /*addr*/0, /*len*/0,
1762 sleep_ok, timeout_ms);
1765 * Leave the request. Without resetting the chip, it's
1766 * still owned by it and we'll just get into trouble
1767 * freeing it now. Mark it as abandoned so that if it
1768 * shows up later it can be freed.
1770 mpt_prt(mpt, "read_cfg_header timed out\n");
1774 switch (req->IOCStatus & MPI_IOCSTATUS_MASK) {
1775 case MPI_IOCSTATUS_SUCCESS:
1776 cfgp = req->req_vbuf;
1777 bcopy(&cfgp->Header, rslt, sizeof(*rslt));
1780 case MPI_IOCSTATUS_CONFIG_INVALID_PAGE:
1781 mpt_lprt(mpt, MPT_PRT_DEBUG,
1782 "Invalid Page Type %d Number %d Addr 0x%0x\n",
1783 PageType, PageNumber, PageAddress);
1787 mpt_prt(mpt, "mpt_read_cfg_header: Config Info Status %x\n",
1792 mpt_free_request(mpt, req);
1797 mpt_read_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
1798 CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1805 req = mpt_get_request(mpt, sleep_ok);
1807 mpt_prt(mpt, "mpt_read_cfg_page: Get request failed!\n");
1811 params.Action = Action;
1812 params.PageVersion = hdr->PageVersion;
1813 params.PageLength = hdr->PageLength;
1814 params.PageNumber = hdr->PageNumber;
1815 params.PageType = hdr->PageType & MPI_CONFIG_PAGETYPE_MASK;
1816 params.PageAddress = PageAddress;
1817 error = mpt_issue_cfg_req(mpt, req, ¶ms,
1818 req->req_pbuf + MPT_RQSL(mpt),
1819 len, sleep_ok, timeout_ms);
1821 mpt_prt(mpt, "read_cfg_page(%d) timed out\n", Action);
1825 if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1826 mpt_prt(mpt, "mpt_read_cfg_page: Config Info Status %x\n",
1828 mpt_free_request(mpt, req);
1831 memcpy(hdr, ((uint8_t *)req->req_vbuf)+MPT_RQSL(mpt), len);
1832 mpt_free_request(mpt, req);
1837 mpt_write_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
1838 CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1846 hdr_attr = hdr->PageType & MPI_CONFIG_PAGEATTR_MASK;
1847 if (hdr_attr != MPI_CONFIG_PAGEATTR_CHANGEABLE &&
1848 hdr_attr != MPI_CONFIG_PAGEATTR_PERSISTENT) {
1849 mpt_prt(mpt, "page type 0x%x not changeable\n",
1850 hdr->PageType & MPI_CONFIG_PAGETYPE_MASK);
1856 * We shouldn't mask off other bits here.
1858 hdr->PageType &= MPI_CONFIG_PAGETYPE_MASK;
1861 req = mpt_get_request(mpt, sleep_ok);
1865 memcpy(((caddr_t)req->req_vbuf) + MPT_RQSL(mpt), hdr, len);
1868 * There isn't any point in restoring stripped out attributes
1869 * if you then mask them going down to issue the request.
1872 params.Action = Action;
1873 params.PageVersion = hdr->PageVersion;
1874 params.PageLength = hdr->PageLength;
1875 params.PageNumber = hdr->PageNumber;
1876 params.PageAddress = PageAddress;
1878 /* Restore stripped out attributes */
1879 hdr->PageType |= hdr_attr;
1880 params.PageType = hdr->PageType & MPI_CONFIG_PAGETYPE_MASK;
1882 params.PageType = hdr->PageType;
1884 error = mpt_issue_cfg_req(mpt, req, ¶ms,
1885 req->req_pbuf + MPT_RQSL(mpt),
1886 len, sleep_ok, timeout_ms);
1888 mpt_prt(mpt, "mpt_write_cfg_page timed out\n");
1892 if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1893 mpt_prt(mpt, "mpt_write_cfg_page: Config Info Status %x\n",
1895 mpt_free_request(mpt, req);
1898 mpt_free_request(mpt, req);
1903 * Read IOC configuration information
1906 mpt_read_config_info_ioc(struct mpt_softc *mpt)
1908 CONFIG_PAGE_HEADER hdr;
1909 struct mpt_raid_volume *mpt_raid;
1914 rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC,
1915 2, 0, &hdr, FALSE, 5000);
1917 * If it's an invalid page, so what? Not a supported function....
1926 mpt_lprt(mpt, MPT_PRT_DEBUG,
1927 "IOC Page 2 Header: Version %x len %x PageNumber %x PageType %x\n",
1928 hdr.PageVersion, hdr.PageLength << 2,
1929 hdr.PageNumber, hdr.PageType);
1931 len = hdr.PageLength * sizeof(uint32_t);
1932 mpt->ioc_page2 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1933 if (mpt->ioc_page2 == NULL) {
1934 mpt_prt(mpt, "unable to allocate memory for IOC page 2\n");
1935 mpt_raid_free_mem(mpt);
1938 memcpy(&mpt->ioc_page2->Header, &hdr, sizeof(hdr));
1939 rv = mpt_read_cur_cfg_page(mpt, 0,
1940 &mpt->ioc_page2->Header, len, FALSE, 5000);
1942 mpt_prt(mpt, "failed to read IOC Page 2\n");
1943 mpt_raid_free_mem(mpt);
1946 mpt2host_config_page_ioc2(mpt->ioc_page2);
1948 if (mpt->ioc_page2->CapabilitiesFlags != 0) {
1951 mpt_prt(mpt, "Capabilities: (");
1952 for (mask = 1; mask != 0; mask <<= 1) {
1953 if ((mpt->ioc_page2->CapabilitiesFlags & mask) == 0) {
1957 case MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT:
1958 mpt_prtc(mpt, " RAID-0");
1960 case MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT:
1961 mpt_prtc(mpt, " RAID-1E");
1963 case MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT:
1964 mpt_prtc(mpt, " RAID-1");
1966 case MPI_IOCPAGE2_CAP_FLAGS_SES_SUPPORT:
1967 mpt_prtc(mpt, " SES");
1969 case MPI_IOCPAGE2_CAP_FLAGS_SAFTE_SUPPORT:
1970 mpt_prtc(mpt, " SAFTE");
1972 case MPI_IOCPAGE2_CAP_FLAGS_CROSS_CHANNEL_SUPPORT:
1973 mpt_prtc(mpt, " Multi-Channel-Arrays");
1978 mpt_prtc(mpt, " )\n");
1979 if ((mpt->ioc_page2->CapabilitiesFlags
1980 & (MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT
1981 | MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT
1982 | MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT)) != 0) {
1983 mpt_prt(mpt, "%d Active Volume%s(%d Max)\n",
1984 mpt->ioc_page2->NumActiveVolumes,
1985 mpt->ioc_page2->NumActiveVolumes != 1
1987 mpt->ioc_page2->MaxVolumes);
1988 mpt_prt(mpt, "%d Hidden Drive Member%s(%d Max)\n",
1989 mpt->ioc_page2->NumActivePhysDisks,
1990 mpt->ioc_page2->NumActivePhysDisks != 1
1992 mpt->ioc_page2->MaxPhysDisks);
1996 len = mpt->ioc_page2->MaxVolumes * sizeof(struct mpt_raid_volume);
1997 mpt->raid_volumes = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1998 if (mpt->raid_volumes == NULL) {
1999 mpt_prt(mpt, "Could not allocate RAID volume data\n");
2000 mpt_raid_free_mem(mpt);
2005 * Copy critical data out of ioc_page2 so that we can
2006 * safely refresh the page without windows of unreliable
2009 mpt->raid_max_volumes = mpt->ioc_page2->MaxVolumes;
2011 len = sizeof(*mpt->raid_volumes->config_page) +
2012 (sizeof (RAID_VOL0_PHYS_DISK) * (mpt->ioc_page2->MaxPhysDisks - 1));
2013 for (i = 0; i < mpt->ioc_page2->MaxVolumes; i++) {
2014 mpt_raid = &mpt->raid_volumes[i];
2015 mpt_raid->config_page =
2016 malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
2017 if (mpt_raid->config_page == NULL) {
2018 mpt_prt(mpt, "Could not allocate RAID page data\n");
2019 mpt_raid_free_mem(mpt);
2023 mpt->raid_page0_len = len;
2025 len = mpt->ioc_page2->MaxPhysDisks * sizeof(struct mpt_raid_disk);
2026 mpt->raid_disks = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
2027 if (mpt->raid_disks == NULL) {
2028 mpt_prt(mpt, "Could not allocate RAID disk data\n");
2029 mpt_raid_free_mem(mpt);
2032 mpt->raid_max_disks = mpt->ioc_page2->MaxPhysDisks;
2037 rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC,
2038 3, 0, &hdr, FALSE, 5000);
2040 mpt_raid_free_mem(mpt);
2044 mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC Page 3 Header: %x %x %x %x\n",
2045 hdr.PageVersion, hdr.PageLength, hdr.PageNumber, hdr.PageType);
2047 len = hdr.PageLength * sizeof(uint32_t);
2048 mpt->ioc_page3 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
2049 if (mpt->ioc_page3 == NULL) {
2050 mpt_prt(mpt, "unable to allocate memory for IOC page 3\n");
2051 mpt_raid_free_mem(mpt);
2054 memcpy(&mpt->ioc_page3->Header, &hdr, sizeof(hdr));
2055 rv = mpt_read_cur_cfg_page(mpt, 0,
2056 &mpt->ioc_page3->Header, len, FALSE, 5000);
2058 mpt_raid_free_mem(mpt);
2061 mpt2host_config_page_ioc3(mpt->ioc_page3);
2062 mpt_raid_wakeup(mpt);
2070 mpt_send_port_enable(struct mpt_softc *mpt, int port)
2073 MSG_PORT_ENABLE *enable_req;
2076 req = mpt_get_request(mpt, /*sleep_ok*/FALSE);
2080 enable_req = req->req_vbuf;
2081 memset(enable_req, 0, MPT_RQSL(mpt));
2083 enable_req->Function = MPI_FUNCTION_PORT_ENABLE;
2084 enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG);
2085 enable_req->PortNumber = port;
2087 mpt_check_doorbell(mpt);
2088 mpt_lprt(mpt, MPT_PRT_DEBUG, "enabling port %d\n", port);
2090 mpt_send_cmd(mpt, req);
2091 error = mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE,
2092 FALSE, (mpt->is_sas || mpt->is_fc)? 300000 : 30000);
2094 mpt_prt(mpt, "port %d enable timed out\n", port);
2097 mpt_free_request(mpt, req);
2098 mpt_lprt(mpt, MPT_PRT_DEBUG, "enabled port %d\n", port);
2103 * Enable/Disable asynchronous event reporting.
2106 mpt_send_event_request(struct mpt_softc *mpt, int onoff)
2109 MSG_EVENT_NOTIFY *enable_req;
2111 req = mpt_get_request(mpt, FALSE);
2115 enable_req = req->req_vbuf;
2116 memset(enable_req, 0, sizeof *enable_req);
2118 enable_req->Function = MPI_FUNCTION_EVENT_NOTIFICATION;
2119 enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_EVENTS);
2120 enable_req->Switch = onoff;
2122 mpt_check_doorbell(mpt);
2123 mpt_lprt(mpt, MPT_PRT_DEBUG, "%sabling async events\n",
2124 onoff ? "en" : "dis");
2126 * Send the command off, but don't wait for it.
2128 mpt_send_cmd(mpt, req);
2133 * Un-mask the interrupts on the chip.
2136 mpt_enable_ints(struct mpt_softc *mpt)
2139 /* Unmask every thing except door bell int */
2140 mpt_write(mpt, MPT_OFFSET_INTR_MASK, MPT_INTR_DB_MASK);
2144 * Mask the interrupts on the chip.
2147 mpt_disable_ints(struct mpt_softc *mpt)
2150 /* Mask all interrupts */
2151 mpt_write(mpt, MPT_OFFSET_INTR_MASK,
2152 MPT_INTR_REPLY_MASK | MPT_INTR_DB_MASK);
2156 mpt_sysctl_attach(struct mpt_softc *mpt)
2158 #if __FreeBSD_version >= 500000
2159 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(mpt->dev);
2160 struct sysctl_oid *tree = device_get_sysctl_tree(mpt->dev);
2162 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
2163 "debug", CTLFLAG_RW, &mpt->verbose, 0,
2164 "Debugging/Verbose level");
2165 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
2166 "role", CTLFLAG_RD, &mpt->role, 0,
2168 #ifdef MPT_TEST_MULTIPATH
2169 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
2170 "failure_id", CTLFLAG_RW, &mpt->failure_id, -1,
2171 "Next Target to Fail");
2177 mpt_attach(struct mpt_softc *mpt)
2179 struct mpt_personality *pers;
2183 mpt_core_attach(mpt);
2184 mpt_core_enable(mpt);
2186 TAILQ_INSERT_TAIL(&mpt_tailq, mpt, links);
2187 for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
2188 pers = mpt_personalities[i];
2192 if (pers->probe(mpt) == 0) {
2193 error = pers->attach(mpt);
2198 mpt->mpt_pers_mask |= (0x1 << pers->id);
2204 * Now that we've attached everything, do the enable function
2205 * for all of the personalities. This allows the personalities
2206 * to do setups that are appropriate for them prior to enabling
2209 for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
2210 pers = mpt_personalities[i];
2211 if (pers != NULL && MPT_PERS_ATTACHED(pers, mpt) != 0) {
2212 error = pers->enable(mpt);
2214 mpt_prt(mpt, "personality %s attached but would"
2215 " not enable (%d)\n", pers->name, error);
2225 mpt_shutdown(struct mpt_softc *mpt)
2227 struct mpt_personality *pers;
2229 MPT_PERS_FOREACH_REVERSE(mpt, pers) {
2230 pers->shutdown(mpt);
2236 mpt_detach(struct mpt_softc *mpt)
2238 struct mpt_personality *pers;
2240 MPT_PERS_FOREACH_REVERSE(mpt, pers) {
2242 mpt->mpt_pers_mask &= ~(0x1 << pers->id);
2245 TAILQ_REMOVE(&mpt_tailq, mpt, links);
2250 mpt_core_load(struct mpt_personality *pers)
2255 * Setup core handlers and insert the default handler
2256 * into all "empty slots".
2258 for (i = 0; i < MPT_NUM_REPLY_HANDLERS; i++) {
2259 mpt_reply_handlers[i] = mpt_default_reply_handler;
2262 mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_EVENTS)] =
2263 mpt_event_reply_handler;
2264 mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_CONFIG)] =
2265 mpt_config_reply_handler;
2266 mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_HANDSHAKE)] =
2267 mpt_handshake_reply_handler;
2272 * Initialize per-instance driver data and perform
2273 * initial controller configuration.
2276 mpt_core_attach(struct mpt_softc *mpt)
2280 LIST_INIT(&mpt->ack_frames);
2281 /* Put all request buffers on the free list */
2282 TAILQ_INIT(&mpt->request_pending_list);
2283 TAILQ_INIT(&mpt->request_free_list);
2284 TAILQ_INIT(&mpt->request_timeout_list);
2285 for (val = 0; val < MPT_MAX_LUNS; val++) {
2286 STAILQ_INIT(&mpt->trt[val].atios);
2287 STAILQ_INIT(&mpt->trt[val].inots);
2289 STAILQ_INIT(&mpt->trt_wildcard.atios);
2290 STAILQ_INIT(&mpt->trt_wildcard.inots);
2291 #ifdef MPT_TEST_MULTIPATH
2292 mpt->failure_id = -1;
2294 mpt->scsi_tgt_handler_id = MPT_HANDLER_ID_NONE;
2295 mpt_sysctl_attach(mpt);
2296 mpt_lprt(mpt, MPT_PRT_DEBUG, "doorbell req = %s\n",
2297 mpt_ioc_diag(mpt_read(mpt, MPT_OFFSET_DOORBELL)));
2300 error = mpt_configure_ioc(mpt, 0, 0);
2307 mpt_core_enable(struct mpt_softc *mpt)
2311 * We enter with the IOC enabled, but async events
2312 * not enabled, ports not enabled and interrupts
2318 * Enable asynchronous event reporting- all personalities
2319 * have attached so that they should be able to now field
2322 mpt_send_event_request(mpt, 1);
2325 * Catch any pending interrupts
2327 * This seems to be crucial- otherwise
2328 * the portenable below times out.
2335 mpt_enable_ints(mpt);
2338 * Catch any pending interrupts
2340 * This seems to be crucial- otherwise
2341 * the portenable below times out.
2348 if (mpt_send_port_enable(mpt, 0) != MPT_OK) {
2349 mpt_prt(mpt, "failed to enable port 0\n");
2358 mpt_core_shutdown(struct mpt_softc *mpt)
2361 mpt_disable_ints(mpt);
2365 mpt_core_detach(struct mpt_softc *mpt)
2372 mpt_disable_ints(mpt);
2374 /* Make sure no request has pending timeouts. */
2375 for (val = 0; val < MPT_MAX_REQUESTS(mpt); val++) {
2376 request_t *req = &mpt->request_pool[val];
2377 mpt_callout_drain(mpt, &req->callout);
2380 mpt_dma_buf_free(mpt);
2384 mpt_core_unload(struct mpt_personality *pers)
2387 /* Unload is always successful. */
2391 #define FW_UPLOAD_REQ_SIZE \
2392 (sizeof(MSG_FW_UPLOAD) - sizeof(SGE_MPI_UNION) \
2393 + sizeof(FW_UPLOAD_TCSGE) + sizeof(SGE_SIMPLE32))
2396 mpt_upload_fw(struct mpt_softc *mpt)
2398 uint8_t fw_req_buf[FW_UPLOAD_REQ_SIZE];
2399 MSG_FW_UPLOAD_REPLY fw_reply;
2400 MSG_FW_UPLOAD *fw_req;
2401 FW_UPLOAD_TCSGE *tsge;
2406 memset(&fw_req_buf, 0, sizeof(fw_req_buf));
2407 fw_req = (MSG_FW_UPLOAD *)fw_req_buf;
2408 fw_req->ImageType = MPI_FW_UPLOAD_ITYPE_FW_IOC_MEM;
2409 fw_req->Function = MPI_FUNCTION_FW_UPLOAD;
2410 fw_req->MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
2411 tsge = (FW_UPLOAD_TCSGE *)&fw_req->SGL;
2412 tsge->DetailsLength = 12;
2413 tsge->Flags = MPI_SGE_FLAGS_TRANSACTION_ELEMENT;
2414 tsge->ImageSize = htole32(mpt->fw_image_size);
2415 sge = (SGE_SIMPLE32 *)(tsge + 1);
2416 flags = (MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER
2417 | MPI_SGE_FLAGS_END_OF_LIST | MPI_SGE_FLAGS_SIMPLE_ELEMENT
2418 | MPI_SGE_FLAGS_32_BIT_ADDRESSING | MPI_SGE_FLAGS_IOC_TO_HOST);
2419 flags <<= MPI_SGE_FLAGS_SHIFT;
2420 sge->FlagsLength = htole32(flags | mpt->fw_image_size);
2421 sge->Address = htole32(mpt->fw_phys);
2422 bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap, BUS_DMASYNC_PREREAD);
2423 error = mpt_send_handshake_cmd(mpt, sizeof(fw_req_buf), &fw_req_buf);
2426 error = mpt_recv_handshake_reply(mpt, sizeof(fw_reply), &fw_reply);
2427 bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap, BUS_DMASYNC_POSTREAD);
2432 mpt_diag_outsl(struct mpt_softc *mpt, uint32_t addr,
2433 uint32_t *data, bus_size_t len)
2437 data_end = data + (roundup2(len, sizeof(uint32_t)) / 4);
2439 pci_enable_io(mpt->dev, SYS_RES_IOPORT);
2441 mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, addr);
2442 while (data != data_end) {
2443 mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, *data);
2447 pci_disable_io(mpt->dev, SYS_RES_IOPORT);
2452 mpt_download_fw(struct mpt_softc *mpt)
2454 MpiFwHeader_t *fw_hdr;
2456 uint32_t ext_offset;
2459 if (mpt->pci_pio_reg == NULL) {
2460 mpt_prt(mpt, "No PIO resource!\n");
2464 mpt_prt(mpt, "Downloading Firmware - Image Size %d\n",
2465 mpt->fw_image_size);
2467 error = mpt_enable_diag_mode(mpt);
2469 mpt_prt(mpt, "Could not enter diagnostic mode!\n");
2473 mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC,
2474 MPI_DIAG_RW_ENABLE|MPI_DIAG_DISABLE_ARM);
2476 fw_hdr = (MpiFwHeader_t *)mpt->fw_image;
2477 bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap, BUS_DMASYNC_PREWRITE);
2478 mpt_diag_outsl(mpt, fw_hdr->LoadStartAddress, (uint32_t*)fw_hdr,
2480 bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap, BUS_DMASYNC_POSTWRITE);
2482 ext_offset = fw_hdr->NextImageHeaderOffset;
2483 while (ext_offset != 0) {
2484 MpiExtImageHeader_t *ext;
2486 ext = (MpiExtImageHeader_t *)((uintptr_t)fw_hdr + ext_offset);
2487 ext_offset = ext->NextImageHeaderOffset;
2488 bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap,
2489 BUS_DMASYNC_PREWRITE);
2490 mpt_diag_outsl(mpt, ext->LoadStartAddress, (uint32_t*)ext,
2492 bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap,
2493 BUS_DMASYNC_POSTWRITE);
2497 pci_enable_io(mpt->dev, SYS_RES_IOPORT);
2499 /* Setup the address to jump to on reset. */
2500 mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, fw_hdr->IopResetRegAddr);
2501 mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, fw_hdr->IopResetVectorValue);
2504 * The controller sets the "flash bad" status after attempting
2505 * to auto-boot from flash. Clear the status so that the controller
2506 * will continue the boot process with our newly installed firmware.
2508 mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE);
2509 data = mpt_pio_read(mpt, MPT_OFFSET_DIAG_DATA) | MPT_DIAG_MEM_CFG_BADFL;
2510 mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE);
2511 mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, data);
2514 pci_disable_io(mpt->dev, SYS_RES_IOPORT);
2518 * Re-enable the processor and clear the boot halt flag.
2520 data = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
2521 data &= ~(MPI_DIAG_PREVENT_IOC_BOOT|MPI_DIAG_DISABLE_ARM);
2522 mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, data);
2524 mpt_disable_diag_mode(mpt);
2529 mpt_dma_buf_alloc(struct mpt_softc *mpt)
2531 struct mpt_map_info mi;
2536 /* Create a child tag for data buffers */
2537 if (mpt_dma_tag_create(mpt, mpt->parent_dmat, 1,
2538 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
2539 NULL, NULL, (mpt->max_cam_seg_cnt - 1) * PAGE_SIZE,
2540 mpt->max_cam_seg_cnt, BUS_SPACE_MAXSIZE_32BIT, 0,
2541 &mpt->buffer_dmat) != 0) {
2542 mpt_prt(mpt, "cannot create a dma tag for data buffers\n");
2546 /* Create a child tag for request buffers */
2547 if (mpt_dma_tag_create(mpt, mpt->parent_dmat, PAGE_SIZE, 0,
2548 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
2549 NULL, NULL, MPT_REQ_MEM_SIZE(mpt), 1, BUS_SPACE_MAXSIZE_32BIT, 0,
2550 &mpt->request_dmat) != 0) {
2551 mpt_prt(mpt, "cannot create a dma tag for requests\n");
2555 /* Allocate some DMA accessible memory for requests */
2556 if (bus_dmamem_alloc(mpt->request_dmat, (void **)&mpt->request,
2557 BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &mpt->request_dmap) != 0) {
2558 mpt_prt(mpt, "cannot allocate %d bytes of request memory\n",
2559 MPT_REQ_MEM_SIZE(mpt));
2566 /* Load and lock it into "bus space" */
2567 bus_dmamap_load(mpt->request_dmat, mpt->request_dmap, mpt->request,
2568 MPT_REQ_MEM_SIZE(mpt), mpt_map_rquest, &mi, 0);
2571 mpt_prt(mpt, "error %d loading dma map for DMA request queue\n",
2575 mpt->request_phys = mi.phys;
2578 * Now create per-request dma maps
2581 pptr = mpt->request_phys;
2582 vptr = mpt->request;
2583 end = pptr + MPT_REQ_MEM_SIZE(mpt);
2585 request_t *req = &mpt->request_pool[i];
2588 /* Store location of Request Data */
2589 req->req_pbuf = pptr;
2590 req->req_vbuf = vptr;
2592 pptr += MPT_REQUEST_AREA;
2593 vptr += MPT_REQUEST_AREA;
2595 req->sense_pbuf = (pptr - MPT_SENSE_SIZE);
2596 req->sense_vbuf = (vptr - MPT_SENSE_SIZE);
2598 error = bus_dmamap_create(mpt->buffer_dmat, 0, &req->dmap);
2600 mpt_prt(mpt, "error %d creating per-cmd DMA maps\n",
2610 mpt_dma_buf_free(struct mpt_softc *mpt)
2614 if (mpt->request_dmat == 0) {
2615 mpt_lprt(mpt, MPT_PRT_DEBUG, "already released dma memory\n");
2618 for (i = 0; i < MPT_MAX_REQUESTS(mpt); i++) {
2619 bus_dmamap_destroy(mpt->buffer_dmat, mpt->request_pool[i].dmap);
2621 bus_dmamap_unload(mpt->request_dmat, mpt->request_dmap);
2622 bus_dmamem_free(mpt->request_dmat, mpt->request, mpt->request_dmap);
2623 bus_dma_tag_destroy(mpt->request_dmat);
2624 mpt->request_dmat = 0;
2625 bus_dma_tag_destroy(mpt->buffer_dmat);
2629 * Allocate/Initialize data structures for the controller. Called
2630 * once at instance startup.
2633 mpt_configure_ioc(struct mpt_softc *mpt, int tn, int needreset)
2635 PTR_MSG_PORT_FACTS_REPLY pfp;
2636 int error, port, val;
2639 if (tn == MPT_MAX_TRYS) {
2644 * No need to reset if the IOC is already in the READY state.
2646 * Force reset if initialization failed previously.
2647 * Note that a hard_reset of the second channel of a '929
2648 * will stop operation of the first channel. Hopefully, if the
2649 * first channel is ok, the second will not require a hard
2652 if (needreset || MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_READY) {
2653 if (mpt_reset(mpt, FALSE) != MPT_OK) {
2654 return (mpt_configure_ioc(mpt, tn++, 1));
2659 if (mpt_get_iocfacts(mpt, &mpt->ioc_facts) != MPT_OK) {
2660 mpt_prt(mpt, "mpt_get_iocfacts failed\n");
2661 return (mpt_configure_ioc(mpt, tn++, 1));
2663 mpt2host_iocfacts_reply(&mpt->ioc_facts);
2665 mpt_prt(mpt, "MPI Version=%d.%d.%d.%d\n",
2666 mpt->ioc_facts.MsgVersion >> 8,
2667 mpt->ioc_facts.MsgVersion & 0xFF,
2668 mpt->ioc_facts.HeaderVersion >> 8,
2669 mpt->ioc_facts.HeaderVersion & 0xFF);
2672 * Now that we know request frame size, we can calculate
2673 * the actual (reasonable) segment limit for read/write I/O.
2675 * This limit is constrained by:
2677 * + The size of each area we allocate per command (and how
2678 * many chain segments we can fit into it).
2679 * + The total number of areas we've set up.
2680 * + The actual chain depth the card will allow.
2682 * The first area's segment count is limited by the I/O request
2683 * at the head of it. We cannot allocate realistically more
2684 * than MPT_MAX_REQUESTS areas. Therefore, to account for both
2685 * conditions, we'll just start out with MPT_MAX_REQUESTS-2.
2688 /* total number of request areas we (can) allocate */
2689 mpt->max_seg_cnt = MPT_MAX_REQUESTS(mpt) - 2;
2691 /* converted to the number of chain areas possible */
2692 mpt->max_seg_cnt *= MPT_NRFM(mpt);
2694 /* limited by the number of chain areas the card will support */
2695 if (mpt->max_seg_cnt > mpt->ioc_facts.MaxChainDepth) {
2696 mpt_lprt(mpt, MPT_PRT_INFO,
2697 "chain depth limited to %u (from %u)\n",
2698 mpt->ioc_facts.MaxChainDepth, mpt->max_seg_cnt);
2699 mpt->max_seg_cnt = mpt->ioc_facts.MaxChainDepth;
2702 /* converted to the number of simple sges in chain segments. */
2703 mpt->max_seg_cnt *= (MPT_NSGL(mpt) - 1);
2706 * Use this as the basis for reporting the maximum I/O size to CAM.
2708 mpt->max_cam_seg_cnt = min(mpt->max_seg_cnt, (MAXPHYS / PAGE_SIZE) + 1);
2710 error = mpt_dma_buf_alloc(mpt);
2712 mpt_prt(mpt, "mpt_dma_buf_alloc() failed!\n");
2716 for (val = 0; val < MPT_MAX_REQUESTS(mpt); val++) {
2717 request_t *req = &mpt->request_pool[val];
2718 req->state = REQ_STATE_ALLOCATED;
2719 mpt_callout_init(mpt, &req->callout);
2720 mpt_free_request(mpt, req);
2723 mpt_lprt(mpt, MPT_PRT_INFO, "Maximum Segment Count: %u, Maximum "
2724 "CAM Segment Count: %u\n", mpt->max_seg_cnt,
2725 mpt->max_cam_seg_cnt);
2727 mpt_lprt(mpt, MPT_PRT_INFO, "MsgLength=%u IOCNumber = %d\n",
2728 mpt->ioc_facts.MsgLength, mpt->ioc_facts.IOCNumber);
2729 mpt_lprt(mpt, MPT_PRT_INFO,
2730 "IOCFACTS: GlobalCredits=%d BlockSize=%u bytes "
2731 "Request Frame Size %u bytes Max Chain Depth %u\n",
2732 mpt->ioc_facts.GlobalCredits, mpt->ioc_facts.BlockSize,
2733 mpt->ioc_facts.RequestFrameSize << 2,
2734 mpt->ioc_facts.MaxChainDepth);
2735 mpt_lprt(mpt, MPT_PRT_INFO, "IOCFACTS: Num Ports %d, FWImageSize %d, "
2736 "Flags=%#x\n", mpt->ioc_facts.NumberOfPorts,
2737 mpt->ioc_facts.FWImageSize, mpt->ioc_facts.Flags);
2739 len = mpt->ioc_facts.NumberOfPorts * sizeof (MSG_PORT_FACTS_REPLY);
2740 mpt->port_facts = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
2741 if (mpt->port_facts == NULL) {
2742 mpt_prt(mpt, "unable to allocate memory for port facts\n");
2747 if ((mpt->ioc_facts.Flags & MPI_IOCFACTS_FLAGS_FW_DOWNLOAD_BOOT) &&
2748 (mpt->fw_uploaded == 0)) {
2749 struct mpt_map_info mi;
2752 * In some configurations, the IOC's firmware is
2753 * stored in a shared piece of system NVRAM that
2754 * is only accessible via the BIOS. In this
2755 * case, the firmware keeps a copy of firmware in
2756 * RAM until the OS driver retrieves it. Once
2757 * retrieved, we are responsible for re-downloading
2758 * the firmware after any hard-reset.
2760 mpt->fw_image_size = mpt->ioc_facts.FWImageSize;
2761 error = mpt_dma_tag_create(mpt, mpt->parent_dmat, 1, 0,
2762 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
2763 mpt->fw_image_size, 1, mpt->fw_image_size, 0,
2766 mpt_prt(mpt, "cannot create firmware dma tag\n");
2769 error = bus_dmamem_alloc(mpt->fw_dmat,
2770 (void **)&mpt->fw_image, BUS_DMA_NOWAIT |
2771 BUS_DMA_COHERENT, &mpt->fw_dmap);
2773 mpt_prt(mpt, "cannot allocate firmware memory\n");
2774 bus_dma_tag_destroy(mpt->fw_dmat);
2779 bus_dmamap_load(mpt->fw_dmat, mpt->fw_dmap,
2780 mpt->fw_image, mpt->fw_image_size, mpt_map_rquest, &mi, 0);
2781 mpt->fw_phys = mi.phys;
2783 error = mpt_upload_fw(mpt);
2785 mpt_prt(mpt, "firmware upload failed.\n");
2786 bus_dmamap_unload(mpt->fw_dmat, mpt->fw_dmap);
2787 bus_dmamem_free(mpt->fw_dmat, mpt->fw_image,
2789 bus_dma_tag_destroy(mpt->fw_dmat);
2790 mpt->fw_image = NULL;
2793 mpt->fw_uploaded = 1;
2796 for (port = 0; port < mpt->ioc_facts.NumberOfPorts; port++) {
2797 pfp = &mpt->port_facts[port];
2798 error = mpt_get_portfacts(mpt, 0, pfp);
2799 if (error != MPT_OK) {
2801 "mpt_get_portfacts on port %d failed\n", port);
2802 free(mpt->port_facts, M_DEVBUF);
2803 mpt->port_facts = NULL;
2804 return (mpt_configure_ioc(mpt, tn++, 1));
2806 mpt2host_portfacts_reply(pfp);
2809 error = MPT_PRT_INFO;
2811 error = MPT_PRT_DEBUG;
2813 mpt_lprt(mpt, error,
2814 "PORTFACTS[%d]: Type %x PFlags %x IID %d MaxDev %d\n",
2815 port, pfp->PortType, pfp->ProtocolFlags, pfp->PortSCSIID,
2821 * XXX: Not yet supporting more than port 0
2823 pfp = &mpt->port_facts[0];
2824 if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_FC) {
2828 } else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_SAS) {
2832 } else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_SCSI) {
2836 if (mpt->mpt_ini_id == MPT_INI_ID_NONE)
2837 mpt->mpt_ini_id = pfp->PortSCSIID;
2838 } else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_ISCSI) {
2839 mpt_prt(mpt, "iSCSI not supported yet\n");
2841 } else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_INACTIVE) {
2842 mpt_prt(mpt, "Inactive Port\n");
2845 mpt_prt(mpt, "unknown Port Type %#x\n", pfp->PortType);
2850 * Set our role with what this port supports.
2852 * Note this might be changed later in different modules
2853 * if this is different from what is wanted.
2855 mpt->role = MPT_ROLE_NONE;
2856 if (pfp->ProtocolFlags & MPI_PORTFACTS_PROTOCOL_INITIATOR) {
2857 mpt->role |= MPT_ROLE_INITIATOR;
2859 if (pfp->ProtocolFlags & MPI_PORTFACTS_PROTOCOL_TARGET) {
2860 mpt->role |= MPT_ROLE_TARGET;
2866 if (mpt_enable_ioc(mpt, 1) != MPT_OK) {
2867 mpt_prt(mpt, "unable to initialize IOC\n");
2872 * Read IOC configuration information.
2874 * We need this to determine whether or not we have certain
2875 * settings for Integrated Mirroring (e.g.).
2877 mpt_read_config_info_ioc(mpt);
2883 mpt_enable_ioc(struct mpt_softc *mpt, int portenable)
2888 if (mpt_send_ioc_init(mpt, MPI_WHOINIT_HOST_DRIVER) != MPT_OK) {
2889 mpt_prt(mpt, "mpt_send_ioc_init failed\n");
2893 mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_send_ioc_init ok\n");
2895 if (mpt_wait_state(mpt, MPT_DB_STATE_RUNNING) != MPT_OK) {
2896 mpt_prt(mpt, "IOC failed to go to run state\n");
2899 mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC now at RUNSTATE\n");
2902 * Give it reply buffers
2904 * Do *not* exceed global credits.
2906 for (val = 0, pptr = mpt->reply_phys;
2907 (pptr + MPT_REPLY_SIZE) < (mpt->reply_phys + PAGE_SIZE);
2908 pptr += MPT_REPLY_SIZE) {
2909 mpt_free_reply(mpt, pptr);
2910 if (++val == mpt->ioc_facts.GlobalCredits - 1)
2916 * Enable the port if asked. This is only done if we're resetting
2917 * the IOC after initial startup.
2921 * Enable asynchronous event reporting
2923 mpt_send_event_request(mpt, 1);
2925 if (mpt_send_port_enable(mpt, 0) != MPT_OK) {
2926 mpt_prt(mpt, "%s: failed to enable port 0\n", __func__);
2934 * Endian Conversion Functions- only used on Big Endian machines
2936 #if _BYTE_ORDER == _BIG_ENDIAN
2938 mpt2host_sge_simple_union(SGE_SIMPLE_UNION *sge)
2941 MPT_2_HOST32(sge, FlagsLength);
2942 MPT_2_HOST32(sge, u.Address64.Low);
2943 MPT_2_HOST32(sge, u.Address64.High);
2947 mpt2host_iocfacts_reply(MSG_IOC_FACTS_REPLY *rp)
2950 MPT_2_HOST16(rp, MsgVersion);
2951 MPT_2_HOST16(rp, HeaderVersion);
2952 MPT_2_HOST32(rp, MsgContext);
2953 MPT_2_HOST16(rp, IOCExceptions);
2954 MPT_2_HOST16(rp, IOCStatus);
2955 MPT_2_HOST32(rp, IOCLogInfo);
2956 MPT_2_HOST16(rp, ReplyQueueDepth);
2957 MPT_2_HOST16(rp, RequestFrameSize);
2958 MPT_2_HOST16(rp, Reserved_0101_FWVersion);
2959 MPT_2_HOST16(rp, ProductID);
2960 MPT_2_HOST32(rp, CurrentHostMfaHighAddr);
2961 MPT_2_HOST16(rp, GlobalCredits);
2962 MPT_2_HOST32(rp, CurrentSenseBufferHighAddr);
2963 MPT_2_HOST16(rp, CurReplyFrameSize);
2964 MPT_2_HOST32(rp, FWImageSize);
2965 MPT_2_HOST32(rp, IOCCapabilities);
2966 MPT_2_HOST32(rp, FWVersion.Word);
2967 MPT_2_HOST16(rp, HighPriorityQueueDepth);
2968 MPT_2_HOST16(rp, Reserved2);
2969 mpt2host_sge_simple_union(&rp->HostPageBufferSGE);
2970 MPT_2_HOST32(rp, ReplyFifoHostSignalingAddr);
2974 mpt2host_portfacts_reply(MSG_PORT_FACTS_REPLY *pfp)
2977 MPT_2_HOST16(pfp, Reserved);
2978 MPT_2_HOST16(pfp, Reserved1);
2979 MPT_2_HOST32(pfp, MsgContext);
2980 MPT_2_HOST16(pfp, Reserved2);
2981 MPT_2_HOST16(pfp, IOCStatus);
2982 MPT_2_HOST32(pfp, IOCLogInfo);
2983 MPT_2_HOST16(pfp, MaxDevices);
2984 MPT_2_HOST16(pfp, PortSCSIID);
2985 MPT_2_HOST16(pfp, ProtocolFlags);
2986 MPT_2_HOST16(pfp, MaxPostedCmdBuffers);
2987 MPT_2_HOST16(pfp, MaxPersistentIDs);
2988 MPT_2_HOST16(pfp, MaxLanBuckets);
2989 MPT_2_HOST16(pfp, Reserved4);
2990 MPT_2_HOST32(pfp, Reserved5);
2994 mpt2host_config_page_ioc2(CONFIG_PAGE_IOC_2 *ioc2)
2998 MPT_2_HOST32(ioc2, CapabilitiesFlags);
2999 for (i = 0; i < MPI_IOC_PAGE_2_RAID_VOLUME_MAX; i++) {
3000 MPT_2_HOST16(ioc2, RaidVolume[i].Reserved3);
3005 mpt2host_config_page_ioc3(CONFIG_PAGE_IOC_3 *ioc3)
3008 MPT_2_HOST16(ioc3, Reserved2);
3012 mpt2host_config_page_scsi_port_0(CONFIG_PAGE_SCSI_PORT_0 *sp0)
3015 MPT_2_HOST32(sp0, Capabilities);
3016 MPT_2_HOST32(sp0, PhysicalInterface);
3020 mpt2host_config_page_scsi_port_1(CONFIG_PAGE_SCSI_PORT_1 *sp1)
3023 MPT_2_HOST32(sp1, Configuration);
3024 MPT_2_HOST32(sp1, OnBusTimerValue);
3025 MPT_2_HOST16(sp1, IDConfig);
3029 host2mpt_config_page_scsi_port_1(CONFIG_PAGE_SCSI_PORT_1 *sp1)
3032 HOST_2_MPT32(sp1, Configuration);
3033 HOST_2_MPT32(sp1, OnBusTimerValue);
3034 HOST_2_MPT16(sp1, IDConfig);
3038 mpt2host_config_page_scsi_port_2(CONFIG_PAGE_SCSI_PORT_2 *sp2)
3042 MPT_2_HOST32(sp2, PortFlags);
3043 MPT_2_HOST32(sp2, PortSettings);
3044 for (i = 0; i < sizeof(sp2->DeviceSettings) /
3045 sizeof(*sp2->DeviceSettings); i++) {
3046 MPT_2_HOST16(sp2, DeviceSettings[i].DeviceFlags);
3051 mpt2host_config_page_scsi_device_0(CONFIG_PAGE_SCSI_DEVICE_0 *sd0)
3054 MPT_2_HOST32(sd0, NegotiatedParameters);
3055 MPT_2_HOST32(sd0, Information);
3059 mpt2host_config_page_scsi_device_1(CONFIG_PAGE_SCSI_DEVICE_1 *sd1)
3062 MPT_2_HOST32(sd1, RequestedParameters);
3063 MPT_2_HOST32(sd1, Reserved);
3064 MPT_2_HOST32(sd1, Configuration);
3068 host2mpt_config_page_scsi_device_1(CONFIG_PAGE_SCSI_DEVICE_1 *sd1)
3071 HOST_2_MPT32(sd1, RequestedParameters);
3072 HOST_2_MPT32(sd1, Reserved);
3073 HOST_2_MPT32(sd1, Configuration);
3077 mpt2host_config_page_fc_port_0(CONFIG_PAGE_FC_PORT_0 *fp0)
3080 MPT_2_HOST32(fp0, Flags);
3081 MPT_2_HOST32(fp0, PortIdentifier);
3082 MPT_2_HOST32(fp0, WWNN.Low);
3083 MPT_2_HOST32(fp0, WWNN.High);
3084 MPT_2_HOST32(fp0, WWPN.Low);
3085 MPT_2_HOST32(fp0, WWPN.High);
3086 MPT_2_HOST32(fp0, SupportedServiceClass);
3087 MPT_2_HOST32(fp0, SupportedSpeeds);
3088 MPT_2_HOST32(fp0, CurrentSpeed);
3089 MPT_2_HOST32(fp0, MaxFrameSize);
3090 MPT_2_HOST32(fp0, FabricWWNN.Low);
3091 MPT_2_HOST32(fp0, FabricWWNN.High);
3092 MPT_2_HOST32(fp0, FabricWWPN.Low);
3093 MPT_2_HOST32(fp0, FabricWWPN.High);
3094 MPT_2_HOST32(fp0, DiscoveredPortsCount);
3095 MPT_2_HOST32(fp0, MaxInitiators);
3099 mpt2host_config_page_fc_port_1(CONFIG_PAGE_FC_PORT_1 *fp1)
3102 MPT_2_HOST32(fp1, Flags);
3103 MPT_2_HOST32(fp1, NoSEEPROMWWNN.Low);
3104 MPT_2_HOST32(fp1, NoSEEPROMWWNN.High);
3105 MPT_2_HOST32(fp1, NoSEEPROMWWPN.Low);
3106 MPT_2_HOST32(fp1, NoSEEPROMWWPN.High);
3110 host2mpt_config_page_fc_port_1(CONFIG_PAGE_FC_PORT_1 *fp1)
3113 HOST_2_MPT32(fp1, Flags);
3114 HOST_2_MPT32(fp1, NoSEEPROMWWNN.Low);
3115 HOST_2_MPT32(fp1, NoSEEPROMWWNN.High);
3116 HOST_2_MPT32(fp1, NoSEEPROMWWPN.Low);
3117 HOST_2_MPT32(fp1, NoSEEPROMWWPN.High);
3121 mpt2host_config_page_raid_vol_0(CONFIG_PAGE_RAID_VOL_0 *volp)
3125 MPT_2_HOST16(volp, VolumeStatus.Reserved);
3126 MPT_2_HOST16(volp, VolumeSettings.Settings);
3127 MPT_2_HOST32(volp, MaxLBA);
3128 MPT_2_HOST32(volp, MaxLBAHigh);
3129 MPT_2_HOST32(volp, StripeSize);
3130 MPT_2_HOST32(volp, Reserved2);
3131 MPT_2_HOST32(volp, Reserved3);
3132 for (i = 0; i < MPI_RAID_VOL_PAGE_0_PHYSDISK_MAX; i++) {
3133 MPT_2_HOST16(volp, PhysDisk[i].Reserved);
3138 mpt2host_config_page_raid_phys_disk_0(CONFIG_PAGE_RAID_PHYS_DISK_0 *rpd0)
3141 MPT_2_HOST32(rpd0, Reserved1);
3142 MPT_2_HOST16(rpd0, PhysDiskStatus.Reserved);
3143 MPT_2_HOST32(rpd0, MaxLBA);
3144 MPT_2_HOST16(rpd0, ErrorData.Reserved);
3145 MPT_2_HOST16(rpd0, ErrorData.ErrorCount);
3146 MPT_2_HOST16(rpd0, ErrorData.SmartCount);
3150 mpt2host_mpi_raid_vol_indicator(MPI_RAID_VOL_INDICATOR *vi)
3153 MPT_2_HOST16(vi, TotalBlocks.High);
3154 MPT_2_HOST16(vi, TotalBlocks.Low);
3155 MPT_2_HOST16(vi, BlocksRemaining.High);
3156 MPT_2_HOST16(vi, BlocksRemaining.Low);