xref: /freebsd/sys/dev/mpr/mpr.c (revision c0e0e530ced057502f51d7a6086857305e08fae0)
1991554f2SKenneth D. Merry /*-
2991554f2SKenneth D. Merry  * Copyright (c) 2009 Yahoo! Inc.
3a2c14879SStephen McConnell  * Copyright (c) 2011-2015 LSI Corp.
47a2a6a1aSStephen McConnell  * Copyright (c) 2013-2016 Avago Technologies
546b23587SKashyap D Desai  * Copyright 2000-2020 Broadcom Inc.
6991554f2SKenneth D. Merry  * All rights reserved.
7991554f2SKenneth D. Merry  *
8991554f2SKenneth D. Merry  * Redistribution and use in source and binary forms, with or without
9991554f2SKenneth D. Merry  * modification, are permitted provided that the following conditions
10991554f2SKenneth D. Merry  * are met:
11991554f2SKenneth D. Merry  * 1. Redistributions of source code must retain the above copyright
12991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer.
13991554f2SKenneth D. Merry  * 2. Redistributions in binary form must reproduce the above copyright
14991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer in the
15991554f2SKenneth D. Merry  *    documentation and/or other materials provided with the distribution.
16991554f2SKenneth D. Merry  *
17991554f2SKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18991554f2SKenneth D. Merry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19991554f2SKenneth D. Merry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20991554f2SKenneth D. Merry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21991554f2SKenneth D. Merry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22991554f2SKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23991554f2SKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24991554f2SKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25991554f2SKenneth D. Merry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26991554f2SKenneth D. Merry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27991554f2SKenneth D. Merry  * SUCH DAMAGE.
28991554f2SKenneth D. Merry  *
2946b23587SKashyap D Desai  * Broadcom Inc. (LSI) MPT-Fusion Host Adapter FreeBSD
30a2c14879SStephen McConnell  *
31991554f2SKenneth D. Merry  */
32991554f2SKenneth D. Merry 
33991554f2SKenneth D. Merry #include <sys/cdefs.h>
34a2c14879SStephen McConnell /* Communications core for Avago Technologies (LSI) MPT3 */
35991554f2SKenneth D. Merry 
36991554f2SKenneth D. Merry /* TODO Move headers to mprvar */
37991554f2SKenneth D. Merry #include <sys/types.h>
38991554f2SKenneth D. Merry #include <sys/param.h>
39991554f2SKenneth D. Merry #include <sys/systm.h>
40991554f2SKenneth D. Merry #include <sys/kernel.h>
41991554f2SKenneth D. Merry #include <sys/selinfo.h>
42991554f2SKenneth D. Merry #include <sys/lock.h>
43991554f2SKenneth D. Merry #include <sys/mutex.h>
44991554f2SKenneth D. Merry #include <sys/module.h>
45991554f2SKenneth D. Merry #include <sys/bus.h>
46991554f2SKenneth D. Merry #include <sys/conf.h>
47991554f2SKenneth D. Merry #include <sys/bio.h>
48991554f2SKenneth D. Merry #include <sys/malloc.h>
49991554f2SKenneth D. Merry #include <sys/uio.h>
50991554f2SKenneth D. Merry #include <sys/sysctl.h>
51bec09074SScott Long #include <sys/smp.h>
52991554f2SKenneth D. Merry #include <sys/queue.h>
53991554f2SKenneth D. Merry #include <sys/kthread.h>
54991554f2SKenneth D. Merry #include <sys/taskqueue.h>
55991554f2SKenneth D. Merry #include <sys/endian.h>
56991554f2SKenneth D. Merry #include <sys/eventhandler.h>
57867aa8cdSScott Long #include <sys/sbuf.h>
58cf6ea6f2SScott Long #include <sys/priv.h>
59991554f2SKenneth D. Merry 
60991554f2SKenneth D. Merry #include <machine/bus.h>
61991554f2SKenneth D. Merry #include <machine/resource.h>
62991554f2SKenneth D. Merry #include <sys/rman.h>
63991554f2SKenneth D. Merry #include <sys/proc.h>
64991554f2SKenneth D. Merry 
65991554f2SKenneth D. Merry #include <dev/pci/pcivar.h>
66991554f2SKenneth D. Merry 
67991554f2SKenneth D. Merry #include <cam/cam.h>
6867feec50SStephen McConnell #include <cam/cam_ccb.h>
69991554f2SKenneth D. Merry #include <cam/scsi/scsi_all.h>
70991554f2SKenneth D. Merry 
71991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_type.h>
72991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2.h>
73991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_ioc.h>
74991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_sas.h>
7567feec50SStephen McConnell #include <dev/mpr/mpi/mpi2_pci.h>
76991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_cnfg.h>
77991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_init.h>
78991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_tool.h>
79991554f2SKenneth D. Merry #include <dev/mpr/mpr_ioctl.h>
80991554f2SKenneth D. Merry #include <dev/mpr/mprvar.h>
81991554f2SKenneth D. Merry #include <dev/mpr/mpr_table.h>
8267feec50SStephen McConnell #include <dev/mpr/mpr_sas.h>
83991554f2SKenneth D. Merry 
84991554f2SKenneth D. Merry static int mpr_diag_reset(struct mpr_softc *sc, int sleep_flag);
85991554f2SKenneth D. Merry static int mpr_init_queues(struct mpr_softc *sc);
863c5ac992SScott Long static void mpr_resize_queues(struct mpr_softc *sc);
87991554f2SKenneth D. Merry static int mpr_message_unit_reset(struct mpr_softc *sc, int sleep_flag);
88991554f2SKenneth D. Merry static int mpr_transition_operational(struct mpr_softc *sc);
89991554f2SKenneth D. Merry static int mpr_iocfacts_allocate(struct mpr_softc *sc, uint8_t attaching);
90991554f2SKenneth D. Merry static void mpr_iocfacts_free(struct mpr_softc *sc);
91991554f2SKenneth D. Merry static void mpr_startup(void *arg);
92991554f2SKenneth D. Merry static int mpr_send_iocinit(struct mpr_softc *sc);
93991554f2SKenneth D. Merry static int mpr_alloc_queues(struct mpr_softc *sc);
941415db6cSScott Long static int mpr_alloc_hw_queues(struct mpr_softc *sc);
95991554f2SKenneth D. Merry static int mpr_alloc_replies(struct mpr_softc *sc);
96991554f2SKenneth D. Merry static int mpr_alloc_requests(struct mpr_softc *sc);
9767feec50SStephen McConnell static int mpr_alloc_nvme_prp_pages(struct mpr_softc *sc);
98991554f2SKenneth D. Merry static int mpr_attach_log(struct mpr_softc *sc);
99991554f2SKenneth D. Merry static __inline void mpr_complete_command(struct mpr_softc *sc,
100991554f2SKenneth D. Merry     struct mpr_command *cm);
101991554f2SKenneth D. Merry static void mpr_dispatch_event(struct mpr_softc *sc, uintptr_t data,
102991554f2SKenneth D. Merry     MPI2_EVENT_NOTIFICATION_REPLY *reply);
1037a2a6a1aSStephen McConnell static void mpr_config_complete(struct mpr_softc *sc, struct mpr_command *cm);
104991554f2SKenneth D. Merry static void mpr_periodic(void *);
105991554f2SKenneth D. Merry static int mpr_reregister_events(struct mpr_softc *sc);
1067a2a6a1aSStephen McConnell static void mpr_enqueue_request(struct mpr_softc *sc, struct mpr_command *cm);
1077a2a6a1aSStephen McConnell static int mpr_get_iocfacts(struct mpr_softc *sc, MPI2_IOC_FACTS_REPLY *facts);
108991554f2SKenneth D. Merry static int mpr_wait_db_ack(struct mpr_softc *sc, int timeout, int sleep_flag);
109867aa8cdSScott Long static int mpr_debug_sysctl(SYSCTL_HANDLER_ARGS);
110cf6ea6f2SScott Long static int mpr_dump_reqs(SYSCTL_HANDLER_ARGS);
111867aa8cdSScott Long static void mpr_parse_debug(struct mpr_softc *sc, char *list);
11271900a79SAlfredo Dal'Ava Junior static void adjust_iocfacts_endianness(MPI2_IOC_FACTS_REPLY *facts);
113867aa8cdSScott Long 
1147029da5cSPawel Biernacki SYSCTL_NODE(_hw, OID_AUTO, mpr, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
1157029da5cSPawel Biernacki     "MPR Driver Parameters");
116991554f2SKenneth D. Merry 
117991554f2SKenneth D. Merry MALLOC_DEFINE(M_MPR, "mpr", "mpr driver memory");
118991554f2SKenneth D. Merry 
119991554f2SKenneth D. Merry /*
120991554f2SKenneth D. Merry  * Do a "Diagnostic Reset" aka a hard reset.  This should get the chip out of
121991554f2SKenneth D. Merry  * any state and back to its initialization state machine.
122991554f2SKenneth D. Merry  */
123991554f2SKenneth D. Merry static char mpt2_reset_magic[] = { 0x00, 0x0f, 0x04, 0x0b, 0x02, 0x07, 0x0d };
124991554f2SKenneth D. Merry 
125991554f2SKenneth D. Merry /*
126991554f2SKenneth D. Merry  * Added this union to smoothly convert le64toh cm->cm_desc.Words.
12767feec50SStephen McConnell  * Compiler only supports uint64_t to be passed as an argument.
128757ff642SScott Long  * Otherwise it will throw this error:
129991554f2SKenneth D. Merry  * "aggregate value used where an integer was expected"
130991554f2SKenneth D. Merry  */
1314bc604dcSScott Long typedef union {
132991554f2SKenneth D. Merry         u64 word;
133991554f2SKenneth D. Merry         struct {
134991554f2SKenneth D. Merry                 u32 low;
135991554f2SKenneth D. Merry                 u32 high;
136991554f2SKenneth D. Merry         } u;
1374bc604dcSScott Long } request_descriptor_t;
138991554f2SKenneth D. Merry 
139991554f2SKenneth D. Merry /* Rate limit chain-fail messages to 1 per minute */
140991554f2SKenneth D. Merry static struct timeval mpr_chainfail_interval = { 60, 0 };
141991554f2SKenneth D. Merry 
142991554f2SKenneth D. Merry /*
143991554f2SKenneth D. Merry  * sleep_flag can be either CAN_SLEEP or NO_SLEEP.
144991554f2SKenneth D. Merry  * If this function is called from process context, it can sleep
145991554f2SKenneth D. Merry  * and there is no harm to sleep, in case if this fuction is called
146991554f2SKenneth D. Merry  * from Interrupt handler, we can not sleep and need NO_SLEEP flag set.
147991554f2SKenneth D. Merry  * based on sleep flags driver will call either msleep, pause or DELAY.
148991554f2SKenneth D. Merry  * msleep and pause are of same variant, but pause is used when mpr_mtx
149991554f2SKenneth D. Merry  * is not hold by driver.
150991554f2SKenneth D. Merry  */
151991554f2SKenneth D. Merry static int
mpr_diag_reset(struct mpr_softc * sc,int sleep_flag)152991554f2SKenneth D. Merry mpr_diag_reset(struct mpr_softc *sc,int sleep_flag)
153991554f2SKenneth D. Merry {
154991554f2SKenneth D. Merry 	uint32_t reg;
155991554f2SKenneth D. Merry 	int i, error, tries = 0;
156991554f2SKenneth D. Merry 	uint8_t first_wait_done = FALSE;
157991554f2SKenneth D. Merry 
158757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
159991554f2SKenneth D. Merry 
160991554f2SKenneth D. Merry 	/* Clear any pending interrupts */
161991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
162991554f2SKenneth D. Merry 
163991554f2SKenneth D. Merry 	/*
164991554f2SKenneth D. Merry 	 * Force NO_SLEEP for threads prohibited to sleep
165991554f2SKenneth D. Merry  	 * e.a Thread from interrupt handler are prohibited to sleep.
166991554f2SKenneth D. Merry  	 */
167991554f2SKenneth D. Merry 	if (curthread->td_no_sleeping)
168991554f2SKenneth D. Merry 		sleep_flag = NO_SLEEP;
169991554f2SKenneth D. Merry 
170757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "sequence start, sleep_flag=%d\n", sleep_flag);
171991554f2SKenneth D. Merry 	/* Push the magic sequence */
172991554f2SKenneth D. Merry 	error = ETIMEDOUT;
173991554f2SKenneth D. Merry 	while (tries++ < 20) {
174991554f2SKenneth D. Merry 		for (i = 0; i < sizeof(mpt2_reset_magic); i++)
175991554f2SKenneth D. Merry 			mpr_regwrite(sc, MPI2_WRITE_SEQUENCE_OFFSET,
176991554f2SKenneth D. Merry 			    mpt2_reset_magic[i]);
177991554f2SKenneth D. Merry 
178991554f2SKenneth D. Merry 		/* wait 100 msec */
179991554f2SKenneth D. Merry 		if (mtx_owned(&sc->mpr_mtx) && sleep_flag == CAN_SLEEP)
180991554f2SKenneth D. Merry 			msleep(&sc->msleep_fake_chan, &sc->mpr_mtx, 0,
181991554f2SKenneth D. Merry 			    "mprdiag", hz/10);
182991554f2SKenneth D. Merry 		else if (sleep_flag == CAN_SLEEP)
183991554f2SKenneth D. Merry 			pause("mprdiag", hz/10);
184991554f2SKenneth D. Merry 		else
185991554f2SKenneth D. Merry 			DELAY(100 * 1000);
186991554f2SKenneth D. Merry 
187991554f2SKenneth D. Merry 		reg = mpr_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
188991554f2SKenneth D. Merry 		if (reg & MPI2_DIAG_DIAG_WRITE_ENABLE) {
189991554f2SKenneth D. Merry 			error = 0;
190991554f2SKenneth D. Merry 			break;
191991554f2SKenneth D. Merry 		}
192991554f2SKenneth D. Merry 	}
193757ff642SScott Long 	if (error) {
194757ff642SScott Long 		mpr_dprint(sc, MPR_INIT, "sequence failed, error=%d, exit\n",
195757ff642SScott Long 		    error);
196991554f2SKenneth D. Merry 		return (error);
197757ff642SScott Long 	}
198991554f2SKenneth D. Merry 
199991554f2SKenneth D. Merry 	/* Send the actual reset.  XXX need to refresh the reg? */
200757ff642SScott Long 	reg |= MPI2_DIAG_RESET_ADAPTER;
201757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "sequence success, sending reset, reg= 0x%x\n",
202757ff642SScott Long 	    reg);
203757ff642SScott Long 	mpr_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET, reg);
204991554f2SKenneth D. Merry 
205991554f2SKenneth D. Merry 	/* Wait up to 300 seconds in 50ms intervals */
206991554f2SKenneth D. Merry 	error = ETIMEDOUT;
207991554f2SKenneth D. Merry 	for (i = 0; i < 6000; i++) {
208991554f2SKenneth D. Merry 		/*
209991554f2SKenneth D. Merry 		 * Wait 50 msec. If this is the first time through, wait 256
210991554f2SKenneth D. Merry 		 * msec to satisfy Diag Reset timing requirements.
211991554f2SKenneth D. Merry 		 */
212991554f2SKenneth D. Merry 		if (first_wait_done) {
213991554f2SKenneth D. Merry 			if (mtx_owned(&sc->mpr_mtx) && sleep_flag == CAN_SLEEP)
214991554f2SKenneth D. Merry 				msleep(&sc->msleep_fake_chan, &sc->mpr_mtx, 0,
215991554f2SKenneth D. Merry 				    "mprdiag", hz/20);
216991554f2SKenneth D. Merry 			else if (sleep_flag == CAN_SLEEP)
217991554f2SKenneth D. Merry 				pause("mprdiag", hz/20);
218991554f2SKenneth D. Merry 			else
219991554f2SKenneth D. Merry 				DELAY(50 * 1000);
220991554f2SKenneth D. Merry 		} else {
221991554f2SKenneth D. Merry 			DELAY(256 * 1000);
222991554f2SKenneth D. Merry 			first_wait_done = TRUE;
223991554f2SKenneth D. Merry 		}
224991554f2SKenneth D. Merry 		/*
225991554f2SKenneth D. Merry 		 * Check for the RESET_ADAPTER bit to be cleared first, then
226991554f2SKenneth D. Merry 		 * wait for the RESET state to be cleared, which takes a little
227991554f2SKenneth D. Merry 		 * longer.
228991554f2SKenneth D. Merry 		 */
229991554f2SKenneth D. Merry 		reg = mpr_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
230991554f2SKenneth D. Merry 		if (reg & MPI2_DIAG_RESET_ADAPTER) {
231991554f2SKenneth D. Merry 			continue;
232991554f2SKenneth D. Merry 		}
233991554f2SKenneth D. Merry 		reg = mpr_regread(sc, MPI2_DOORBELL_OFFSET);
234991554f2SKenneth D. Merry 		if ((reg & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_RESET) {
235991554f2SKenneth D. Merry 			error = 0;
236991554f2SKenneth D. Merry 			break;
237991554f2SKenneth D. Merry 		}
238991554f2SKenneth D. Merry 	}
239757ff642SScott Long 	if (error) {
240757ff642SScott Long 		mpr_dprint(sc, MPR_INIT, "reset failed, error= %d, exit\n",
241757ff642SScott Long 		    error);
242991554f2SKenneth D. Merry 		return (error);
243757ff642SScott Long 	}
244991554f2SKenneth D. Merry 
245991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_WRITE_SEQUENCE_OFFSET, 0x0);
246757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "diag reset success, exit\n");
247991554f2SKenneth D. Merry 
248991554f2SKenneth D. Merry 	return (0);
249991554f2SKenneth D. Merry }
250991554f2SKenneth D. Merry 
251991554f2SKenneth D. Merry static int
mpr_message_unit_reset(struct mpr_softc * sc,int sleep_flag)252991554f2SKenneth D. Merry mpr_message_unit_reset(struct mpr_softc *sc, int sleep_flag)
253991554f2SKenneth D. Merry {
254757ff642SScott Long 	int error;
255991554f2SKenneth D. Merry 
256991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
257991554f2SKenneth D. Merry 
258757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
259757ff642SScott Long 
260757ff642SScott Long 	error = 0;
261991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_DOORBELL_OFFSET,
262991554f2SKenneth D. Merry 	    MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET <<
263991554f2SKenneth D. Merry 	    MPI2_DOORBELL_FUNCTION_SHIFT);
264991554f2SKenneth D. Merry 
265991554f2SKenneth D. Merry 	if (mpr_wait_db_ack(sc, 5, sleep_flag) != 0) {
266757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_FAULT,
267757ff642SScott Long 		    "Doorbell handshake failed\n");
268757ff642SScott Long 		error = ETIMEDOUT;
269991554f2SKenneth D. Merry 	}
270991554f2SKenneth D. Merry 
271757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit\n", __func__);
272757ff642SScott Long 	return (error);
273991554f2SKenneth D. Merry }
274991554f2SKenneth D. Merry 
275991554f2SKenneth D. Merry static int
mpr_transition_ready(struct mpr_softc * sc)276991554f2SKenneth D. Merry mpr_transition_ready(struct mpr_softc *sc)
277991554f2SKenneth D. Merry {
278991554f2SKenneth D. Merry 	uint32_t reg, state;
279991554f2SKenneth D. Merry 	int error, tries = 0;
280991554f2SKenneth D. Merry 	int sleep_flags;
281991554f2SKenneth D. Merry 
282991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
283991554f2SKenneth D. Merry 	/* If we are in attach call, do not sleep */
284991554f2SKenneth D. Merry 	sleep_flags = (sc->mpr_flags & MPR_FLAGS_ATTACH_DONE)
285991554f2SKenneth D. Merry 	    ? CAN_SLEEP : NO_SLEEP;
286991554f2SKenneth D. Merry 
287991554f2SKenneth D. Merry 	error = 0;
288757ff642SScott Long 
289757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered, sleep_flags= %d\n",
290757ff642SScott Long 	    __func__, sleep_flags);
291757ff642SScott Long 
292991554f2SKenneth D. Merry 	while (tries++ < 1200) {
293991554f2SKenneth D. Merry 		reg = mpr_regread(sc, MPI2_DOORBELL_OFFSET);
294991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_INIT, "  Doorbell= 0x%x\n", reg);
295991554f2SKenneth D. Merry 
296991554f2SKenneth D. Merry 		/*
297991554f2SKenneth D. Merry 		 * Ensure the IOC is ready to talk.  If it's not, try
298991554f2SKenneth D. Merry 		 * resetting it.
299991554f2SKenneth D. Merry 		 */
300991554f2SKenneth D. Merry 		if (reg & MPI2_DOORBELL_USED) {
301757ff642SScott Long 			mpr_dprint(sc, MPR_INIT, "  Not ready, sending diag "
302757ff642SScott Long 			    "reset\n");
303991554f2SKenneth D. Merry 			mpr_diag_reset(sc, sleep_flags);
304991554f2SKenneth D. Merry 			DELAY(50000);
305991554f2SKenneth D. Merry 			continue;
306991554f2SKenneth D. Merry 		}
307991554f2SKenneth D. Merry 
308991554f2SKenneth D. Merry 		/* Is the adapter owned by another peer? */
309991554f2SKenneth D. Merry 		if ((reg & MPI2_DOORBELL_WHO_INIT_MASK) ==
310991554f2SKenneth D. Merry 		    (MPI2_WHOINIT_PCI_PEER << MPI2_DOORBELL_WHO_INIT_SHIFT)) {
311757ff642SScott Long 			mpr_dprint(sc, MPR_INIT|MPR_FAULT, "IOC is under the "
312757ff642SScott Long 			    "control of another peer host, aborting "
313757ff642SScott Long 			    "initialization.\n");
314757ff642SScott Long 			error = ENXIO;
315757ff642SScott Long 			break;
316991554f2SKenneth D. Merry 		}
317991554f2SKenneth D. Merry 
318991554f2SKenneth D. Merry 		state = reg & MPI2_IOC_STATE_MASK;
319991554f2SKenneth D. Merry 		if (state == MPI2_IOC_STATE_READY) {
320991554f2SKenneth D. Merry 			/* Ready to go! */
321991554f2SKenneth D. Merry 			error = 0;
322991554f2SKenneth D. Merry 			break;
323991554f2SKenneth D. Merry 		} else if (state == MPI2_IOC_STATE_FAULT) {
324757ff642SScott Long 			mpr_dprint(sc, MPR_INIT|MPR_FAULT, "IOC in fault "
325757ff642SScott Long 			    "state 0x%x, resetting\n",
326991554f2SKenneth D. Merry 			    state & MPI2_DOORBELL_FAULT_CODE_MASK);
327991554f2SKenneth D. Merry 			mpr_diag_reset(sc, sleep_flags);
328991554f2SKenneth D. Merry 		} else if (state == MPI2_IOC_STATE_OPERATIONAL) {
329991554f2SKenneth D. Merry 			/* Need to take ownership */
330991554f2SKenneth D. Merry 			mpr_message_unit_reset(sc, sleep_flags);
331991554f2SKenneth D. Merry 		} else if (state == MPI2_IOC_STATE_RESET) {
332991554f2SKenneth D. Merry 			/* Wait a bit, IOC might be in transition */
333757ff642SScott Long 			mpr_dprint(sc, MPR_INIT|MPR_FAULT,
334991554f2SKenneth D. Merry 			    "IOC in unexpected reset state\n");
335991554f2SKenneth D. Merry 		} else {
336757ff642SScott Long 			mpr_dprint(sc, MPR_INIT|MPR_FAULT,
337991554f2SKenneth D. Merry 			    "IOC in unknown state 0x%x\n", state);
338991554f2SKenneth D. Merry 			error = EINVAL;
339991554f2SKenneth D. Merry 			break;
340991554f2SKenneth D. Merry 		}
341991554f2SKenneth D. Merry 
342991554f2SKenneth D. Merry 		/* Wait 50ms for things to settle down. */
343991554f2SKenneth D. Merry 		DELAY(50000);
344991554f2SKenneth D. Merry 	}
345991554f2SKenneth D. Merry 
346991554f2SKenneth D. Merry 	if (error)
347757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_FAULT,
348757ff642SScott Long 		    "Cannot transition IOC to ready\n");
349757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit\n", __func__);
350991554f2SKenneth D. Merry 	return (error);
351991554f2SKenneth D. Merry }
352991554f2SKenneth D. Merry 
353991554f2SKenneth D. Merry static int
mpr_transition_operational(struct mpr_softc * sc)354991554f2SKenneth D. Merry mpr_transition_operational(struct mpr_softc *sc)
355991554f2SKenneth D. Merry {
356991554f2SKenneth D. Merry 	uint32_t reg, state;
357991554f2SKenneth D. Merry 	int error;
358991554f2SKenneth D. Merry 
359991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
360991554f2SKenneth D. Merry 
361991554f2SKenneth D. Merry 	error = 0;
362991554f2SKenneth D. Merry 	reg = mpr_regread(sc, MPI2_DOORBELL_OFFSET);
363757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered, Doorbell= 0x%x\n", __func__, reg);
364991554f2SKenneth D. Merry 
365991554f2SKenneth D. Merry 	state = reg & MPI2_IOC_STATE_MASK;
366991554f2SKenneth D. Merry 	if (state != MPI2_IOC_STATE_READY) {
367757ff642SScott Long 		mpr_dprint(sc, MPR_INIT, "IOC not ready\n");
368991554f2SKenneth D. Merry 		if ((error = mpr_transition_ready(sc)) != 0) {
369757ff642SScott Long 			mpr_dprint(sc, MPR_INIT|MPR_FAULT,
370757ff642SScott Long 			    "failed to transition ready, exit\n");
371991554f2SKenneth D. Merry 			return (error);
372991554f2SKenneth D. Merry 		}
373991554f2SKenneth D. Merry 	}
374991554f2SKenneth D. Merry 
375991554f2SKenneth D. Merry 	error = mpr_send_iocinit(sc);
376757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit\n", __func__);
377757ff642SScott Long 
378991554f2SKenneth D. Merry 	return (error);
379991554f2SKenneth D. Merry }
380991554f2SKenneth D. Merry 
3813c5ac992SScott Long static void
mpr_resize_queues(struct mpr_softc * sc)3823c5ac992SScott Long mpr_resize_queues(struct mpr_softc *sc)
3833c5ac992SScott Long {
384731308d0SAlexander Motin 	u_int reqcr, prireqcr, maxio, sges_per_frame, chain_seg_size;
3853c5ac992SScott Long 
3863c5ac992SScott Long 	/*
3873c5ac992SScott Long 	 * Size the queues. Since the reply queues always need one free
3883c5ac992SScott Long 	 * entry, we'll deduct one reply message here.  The LSI documents
3893c5ac992SScott Long 	 * suggest instead to add a count to the request queue, but I think
3903c5ac992SScott Long 	 * that it's better to deduct from reply queue.
3913c5ac992SScott Long 	 */
3923c5ac992SScott Long 	prireqcr = MAX(1, sc->max_prireqframes);
3933c5ac992SScott Long 	prireqcr = MIN(prireqcr, sc->facts->HighPriorityCredit);
3943c5ac992SScott Long 
3953c5ac992SScott Long 	reqcr = MAX(2, sc->max_reqframes);
3963c5ac992SScott Long 	reqcr = MIN(reqcr, sc->facts->RequestCredit);
3973c5ac992SScott Long 
3983c5ac992SScott Long 	sc->num_reqs = prireqcr + reqcr;
39962a09ee9SAlexander Motin 	sc->num_prireqs = prireqcr;
4003c5ac992SScott Long 	sc->num_replies = MIN(sc->max_replyframes + sc->max_evtframes,
4013c5ac992SScott Long 	    sc->facts->MaxReplyDescriptorPostQueueDepth) - 1;
4023c5ac992SScott Long 
4034f5d6573SAlexander Motin 	/* Store the request frame size in bytes rather than as 32bit words */
4044f5d6573SAlexander Motin 	sc->reqframesz = sc->facts->IOCRequestFrameSize * 4;
4054f5d6573SAlexander Motin 
4064f5d6573SAlexander Motin 	/*
4074f5d6573SAlexander Motin 	 * Gen3 and beyond uses the IOCMaxChainSegmentSize from IOC Facts to
4084f5d6573SAlexander Motin 	 * get the size of a Chain Frame.  Previous versions use the size as a
4094f5d6573SAlexander Motin 	 * Request Frame for the Chain Frame size.  If IOCMaxChainSegmentSize
4104f5d6573SAlexander Motin 	 * is 0, use the default value.  The IOCMaxChainSegmentSize is the
4114f5d6573SAlexander Motin 	 * number of 16-byte elelements that can fit in a Chain Frame, which is
4124f5d6573SAlexander Motin 	 * the size of an IEEE Simple SGE.
4134f5d6573SAlexander Motin 	 */
4144f5d6573SAlexander Motin 	if (sc->facts->MsgVersion >= MPI2_VERSION_02_05) {
41571900a79SAlfredo Dal'Ava Junior 		chain_seg_size = sc->facts->IOCMaxChainSegmentSize;
416731308d0SAlexander Motin 		if (chain_seg_size == 0)
417731308d0SAlexander Motin 			chain_seg_size = MPR_DEFAULT_CHAIN_SEG_SIZE;
418731308d0SAlexander Motin 		sc->chain_frame_size = chain_seg_size *
4194f5d6573SAlexander Motin 		    MPR_MAX_CHAIN_ELEMENT_SIZE;
4204f5d6573SAlexander Motin 	} else {
4214f5d6573SAlexander Motin 		sc->chain_frame_size = sc->reqframesz;
4224f5d6573SAlexander Motin 	}
4234f5d6573SAlexander Motin 
4244f5d6573SAlexander Motin 	/*
4254f5d6573SAlexander Motin 	 * Max IO Size is Page Size * the following:
4264f5d6573SAlexander Motin 	 * ((SGEs per frame - 1 for chain element) * Max Chain Depth)
4274f5d6573SAlexander Motin 	 * + 1 for no chain needed in last frame
4284f5d6573SAlexander Motin 	 *
4294f5d6573SAlexander Motin 	 * If user suggests a Max IO size to use, use the smaller of the
4304f5d6573SAlexander Motin 	 * user's value and the calculated value as long as the user's
4314f5d6573SAlexander Motin 	 * value is larger than 0. The user's value is in pages.
4324f5d6573SAlexander Motin 	 */
4334f5d6573SAlexander Motin 	sges_per_frame = sc->chain_frame_size/sizeof(MPI2_IEEE_SGE_SIMPLE64)-1;
4344f5d6573SAlexander Motin 	maxio = (sges_per_frame * sc->facts->MaxChainDepth + 1) * PAGE_SIZE;
4354f5d6573SAlexander Motin 
4364f5d6573SAlexander Motin 	/*
4374f5d6573SAlexander Motin 	 * If I/O size limitation requested then use it and pass up to CAM.
438cd853791SKonstantin Belousov 	 * If not, use maxphys as an optimization hint, but report HW limit.
4394f5d6573SAlexander Motin 	 */
4404f5d6573SAlexander Motin 	if (sc->max_io_pages > 0) {
4414f5d6573SAlexander Motin 		maxio = min(maxio, sc->max_io_pages * PAGE_SIZE);
4424f5d6573SAlexander Motin 		sc->maxio = maxio;
4434f5d6573SAlexander Motin 	} else {
4444f5d6573SAlexander Motin 		sc->maxio = maxio;
445cd853791SKonstantin Belousov 		maxio = min(maxio, maxphys);
4464f5d6573SAlexander Motin 	}
4474f5d6573SAlexander Motin 
4484f5d6573SAlexander Motin 	sc->num_chains = (maxio / PAGE_SIZE + sges_per_frame - 2) /
4494f5d6573SAlexander Motin 	    sges_per_frame * reqcr;
4504f5d6573SAlexander Motin 	if (sc->max_chains > 0 && sc->max_chains < sc->num_chains)
4514f5d6573SAlexander Motin 		sc->num_chains = sc->max_chains;
4524f5d6573SAlexander Motin 
4533c5ac992SScott Long 	/*
4543c5ac992SScott Long 	 * Figure out the number of MSIx-based queues.  If the firmware or
4553c5ac992SScott Long 	 * user has done something crazy and not allowed enough credit for
4563c5ac992SScott Long 	 * the queues to be useful then don't enable multi-queue.
4573c5ac992SScott Long 	 */
4583c5ac992SScott Long 	if (sc->facts->MaxMSIxVectors < 2)
4593c5ac992SScott Long 		sc->msi_msgs = 1;
4603c5ac992SScott Long 
4613c5ac992SScott Long 	if (sc->msi_msgs > 1) {
4623c5ac992SScott Long 		sc->msi_msgs = MIN(sc->msi_msgs, mp_ncpus);
4633c5ac992SScott Long 		sc->msi_msgs = MIN(sc->msi_msgs, sc->facts->MaxMSIxVectors);
4643c5ac992SScott Long 		if (sc->num_reqs / sc->msi_msgs < 2)
4653c5ac992SScott Long 			sc->msi_msgs = 1;
4663c5ac992SScott Long 	}
4673c5ac992SScott Long 
4683c5ac992SScott Long 	mpr_dprint(sc, MPR_INIT, "Sized queues to q=%d reqs=%d replies=%d\n",
4693c5ac992SScott Long 	    sc->msi_msgs, sc->num_reqs, sc->num_replies);
4703c5ac992SScott Long }
4713c5ac992SScott Long 
472991554f2SKenneth D. Merry /*
473991554f2SKenneth D. Merry  * This is called during attach and when re-initializing due to a Diag Reset.
474991554f2SKenneth D. Merry  * IOC Facts is used to allocate many of the structures needed by the driver.
475991554f2SKenneth D. Merry  * If called from attach, de-allocation is not required because the driver has
476991554f2SKenneth D. Merry  * not allocated any structures yet, but if called from a Diag Reset, previously
477991554f2SKenneth D. Merry  * allocated structures based on IOC Facts will need to be freed and re-
478991554f2SKenneth D. Merry  * allocated bases on the latest IOC Facts.
479991554f2SKenneth D. Merry  */
480991554f2SKenneth D. Merry static int
mpr_iocfacts_allocate(struct mpr_softc * sc,uint8_t attaching)481991554f2SKenneth D. Merry mpr_iocfacts_allocate(struct mpr_softc *sc, uint8_t attaching)
482991554f2SKenneth D. Merry {
483a2c14879SStephen McConnell 	int error;
484991554f2SKenneth D. Merry 	Mpi2IOCFactsReply_t saved_facts;
485991554f2SKenneth D. Merry 	uint8_t saved_mode, reallocating;
486991554f2SKenneth D. Merry 
487757ff642SScott Long 	mpr_dprint(sc, MPR_INIT|MPR_TRACE, "%s entered\n", __func__);
488991554f2SKenneth D. Merry 
489991554f2SKenneth D. Merry 	/* Save old IOC Facts and then only reallocate if Facts have changed */
490991554f2SKenneth D. Merry 	if (!attaching) {
491991554f2SKenneth D. Merry 		bcopy(sc->facts, &saved_facts, sizeof(MPI2_IOC_FACTS_REPLY));
492991554f2SKenneth D. Merry 	}
493991554f2SKenneth D. Merry 
494991554f2SKenneth D. Merry 	/*
495991554f2SKenneth D. Merry 	 * Get IOC Facts.  In all cases throughout this function, panic if doing
496991554f2SKenneth D. Merry 	 * a re-initialization and only return the error if attaching so the OS
497991554f2SKenneth D. Merry 	 * can handle it.
498991554f2SKenneth D. Merry 	 */
499991554f2SKenneth D. Merry 	if ((error = mpr_get_iocfacts(sc, sc->facts)) != 0) {
500991554f2SKenneth D. Merry 		if (attaching) {
501757ff642SScott Long 			mpr_dprint(sc, MPR_INIT|MPR_FAULT, "Failed to get "
502757ff642SScott Long 			    "IOC Facts with error %d, exit\n", error);
503991554f2SKenneth D. Merry 			return (error);
504991554f2SKenneth D. Merry 		} else {
505991554f2SKenneth D. Merry 			panic("%s failed to get IOC Facts with error %d\n",
506991554f2SKenneth D. Merry 			    __func__, error);
507991554f2SKenneth D. Merry 		}
508991554f2SKenneth D. Merry 	}
509991554f2SKenneth D. Merry 
510055e2653SScott Long 	MPR_DPRINT_PAGE(sc, MPR_XINFO, iocfacts, sc->facts);
511991554f2SKenneth D. Merry 
512991554f2SKenneth D. Merry 	snprintf(sc->fw_version, sizeof(sc->fw_version),
513991554f2SKenneth D. Merry 	    "%02d.%02d.%02d.%02d",
514991554f2SKenneth D. Merry 	    sc->facts->FWVersion.Struct.Major,
515991554f2SKenneth D. Merry 	    sc->facts->FWVersion.Struct.Minor,
516991554f2SKenneth D. Merry 	    sc->facts->FWVersion.Struct.Unit,
517991554f2SKenneth D. Merry 	    sc->facts->FWVersion.Struct.Dev);
518991554f2SKenneth D. Merry 
51969e85eb8SScott Long 	snprintf(sc->msg_version, sizeof(sc->msg_version), "%d.%d",
52069e85eb8SScott Long 	    (sc->facts->MsgVersion & MPI2_IOCFACTS_MSGVERSION_MAJOR_MASK) >>
52169e85eb8SScott Long 	    MPI2_IOCFACTS_MSGVERSION_MAJOR_SHIFT,
52269e85eb8SScott Long 	    (sc->facts->MsgVersion & MPI2_IOCFACTS_MSGVERSION_MINOR_MASK) >>
52369e85eb8SScott Long 	    MPI2_IOCFACTS_MSGVERSION_MINOR_SHIFT);
52469e85eb8SScott Long 
525757ff642SScott Long 	mpr_dprint(sc, MPR_INFO, "Firmware: %s, Driver: %s\n", sc->fw_version,
526991554f2SKenneth D. Merry 	    MPR_DRIVER_VERSION);
527757ff642SScott Long 	mpr_dprint(sc, MPR_INFO,
528757ff642SScott Long 	    "IOCCapabilities: %b\n", sc->facts->IOCCapabilities,
529991554f2SKenneth D. Merry 	    "\20" "\3ScsiTaskFull" "\4DiagTrace" "\5SnapBuf" "\6ExtBuf"
530991554f2SKenneth D. Merry 	    "\7EEDP" "\10BiDirTarg" "\11Multicast" "\14TransRetry" "\15IR"
53167feec50SStephen McConnell 	    "\16EventReplay" "\17RaidAccel" "\20MSIXIndex" "\21HostDisc"
53267feec50SStephen McConnell 	    "\22FastPath" "\23RDPQArray" "\24AtomicReqDesc" "\25PCIeSRIOV");
533991554f2SKenneth D. Merry 
534991554f2SKenneth D. Merry 	/*
535991554f2SKenneth D. Merry 	 * If the chip doesn't support event replay then a hard reset will be
536991554f2SKenneth D. Merry 	 * required to trigger a full discovery.  Do the reset here then
537991554f2SKenneth D. Merry 	 * retransition to Ready.  A hard reset might have already been done,
538991554f2SKenneth D. Merry 	 * but it doesn't hurt to do it again.  Only do this if attaching, not
539991554f2SKenneth D. Merry 	 * for a Diag Reset.
540991554f2SKenneth D. Merry 	 */
541757ff642SScott Long 	if (attaching && ((sc->facts->IOCCapabilities &
542757ff642SScott Long 	    MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY) == 0)) {
543757ff642SScott Long 		mpr_dprint(sc, MPR_INIT, "No event replay, resetting\n");
544991554f2SKenneth D. Merry 		mpr_diag_reset(sc, NO_SLEEP);
545991554f2SKenneth D. Merry 		if ((error = mpr_transition_ready(sc)) != 0) {
546757ff642SScott Long 			mpr_dprint(sc, MPR_INIT|MPR_FAULT, "Failed to "
547757ff642SScott Long 			    "transition to ready with error %d, exit\n",
548757ff642SScott Long 			    error);
549991554f2SKenneth D. Merry 			return (error);
550991554f2SKenneth D. Merry 		}
551991554f2SKenneth D. Merry 	}
552991554f2SKenneth D. Merry 
553991554f2SKenneth D. Merry 	/*
554991554f2SKenneth D. Merry 	 * Set flag if IR Firmware is loaded.  If the RAID Capability has
555991554f2SKenneth D. Merry 	 * changed from the previous IOC Facts, log a warning, but only if
556991554f2SKenneth D. Merry 	 * checking this after a Diag Reset and not during attach.
557991554f2SKenneth D. Merry 	 */
558991554f2SKenneth D. Merry 	saved_mode = sc->ir_firmware;
559991554f2SKenneth D. Merry 	if (sc->facts->IOCCapabilities &
560991554f2SKenneth D. Merry 	    MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID)
561991554f2SKenneth D. Merry 		sc->ir_firmware = 1;
562991554f2SKenneth D. Merry 	if (!attaching) {
563991554f2SKenneth D. Merry 		if (sc->ir_firmware != saved_mode) {
564757ff642SScott Long 			mpr_dprint(sc, MPR_INIT|MPR_FAULT, "new IR/IT mode "
565757ff642SScott Long 			    "in IOC Facts does not match previous mode\n");
566991554f2SKenneth D. Merry 		}
567991554f2SKenneth D. Merry 	}
568991554f2SKenneth D. Merry 
569991554f2SKenneth D. Merry 	/* Only deallocate and reallocate if relevant IOC Facts have changed */
570991554f2SKenneth D. Merry 	reallocating = FALSE;
5716d4ffcb4SKenneth D. Merry 	sc->mpr_flags &= ~MPR_FLAGS_REALLOCATED;
5726d4ffcb4SKenneth D. Merry 
573991554f2SKenneth D. Merry 	if ((!attaching) &&
574991554f2SKenneth D. Merry 	    ((saved_facts.MsgVersion != sc->facts->MsgVersion) ||
575991554f2SKenneth D. Merry 	    (saved_facts.HeaderVersion != sc->facts->HeaderVersion) ||
576991554f2SKenneth D. Merry 	    (saved_facts.MaxChainDepth != sc->facts->MaxChainDepth) ||
577991554f2SKenneth D. Merry 	    (saved_facts.RequestCredit != sc->facts->RequestCredit) ||
578991554f2SKenneth D. Merry 	    (saved_facts.ProductID != sc->facts->ProductID) ||
579991554f2SKenneth D. Merry 	    (saved_facts.IOCCapabilities != sc->facts->IOCCapabilities) ||
580991554f2SKenneth D. Merry 	    (saved_facts.IOCRequestFrameSize !=
581991554f2SKenneth D. Merry 	    sc->facts->IOCRequestFrameSize) ||
5822bbc5fcbSStephen McConnell 	    (saved_facts.IOCMaxChainSegmentSize !=
5832bbc5fcbSStephen McConnell 	    sc->facts->IOCMaxChainSegmentSize) ||
584991554f2SKenneth D. Merry 	    (saved_facts.MaxTargets != sc->facts->MaxTargets) ||
585991554f2SKenneth D. Merry 	    (saved_facts.MaxSasExpanders != sc->facts->MaxSasExpanders) ||
586991554f2SKenneth D. Merry 	    (saved_facts.MaxEnclosures != sc->facts->MaxEnclosures) ||
587991554f2SKenneth D. Merry 	    (saved_facts.HighPriorityCredit != sc->facts->HighPriorityCredit) ||
588991554f2SKenneth D. Merry 	    (saved_facts.MaxReplyDescriptorPostQueueDepth !=
589991554f2SKenneth D. Merry 	    sc->facts->MaxReplyDescriptorPostQueueDepth) ||
590991554f2SKenneth D. Merry 	    (saved_facts.ReplyFrameSize != sc->facts->ReplyFrameSize) ||
591991554f2SKenneth D. Merry 	    (saved_facts.MaxVolumes != sc->facts->MaxVolumes) ||
592991554f2SKenneth D. Merry 	    (saved_facts.MaxPersistentEntries !=
593991554f2SKenneth D. Merry 	    sc->facts->MaxPersistentEntries))) {
594991554f2SKenneth D. Merry 		reallocating = TRUE;
5956d4ffcb4SKenneth D. Merry 
5966d4ffcb4SKenneth D. Merry 		/* Record that we reallocated everything */
5976d4ffcb4SKenneth D. Merry 		sc->mpr_flags |= MPR_FLAGS_REALLOCATED;
598991554f2SKenneth D. Merry 	}
599991554f2SKenneth D. Merry 
600991554f2SKenneth D. Merry 	/*
601991554f2SKenneth D. Merry 	 * Some things should be done if attaching or re-allocating after a Diag
602991554f2SKenneth D. Merry 	 * Reset, but are not needed after a Diag Reset if the FW has not
603991554f2SKenneth D. Merry 	 * changed.
604991554f2SKenneth D. Merry 	 */
605991554f2SKenneth D. Merry 	if (attaching || reallocating) {
606991554f2SKenneth D. Merry 		/*
607991554f2SKenneth D. Merry 		 * Check if controller supports FW diag buffers and set flag to
608991554f2SKenneth D. Merry 		 * enable each type.
609991554f2SKenneth D. Merry 		 */
610991554f2SKenneth D. Merry 		if (sc->facts->IOCCapabilities &
611991554f2SKenneth D. Merry 		    MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
612991554f2SKenneth D. Merry 			sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_TRACE].
613991554f2SKenneth D. Merry 			    enabled = TRUE;
614991554f2SKenneth D. Merry 		if (sc->facts->IOCCapabilities &
615991554f2SKenneth D. Merry 		    MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
616991554f2SKenneth D. Merry 			sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_SNAPSHOT].
617991554f2SKenneth D. Merry 			    enabled = TRUE;
618991554f2SKenneth D. Merry 		if (sc->facts->IOCCapabilities &
619991554f2SKenneth D. Merry 		    MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
620991554f2SKenneth D. Merry 			sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_EXTENDED].
621991554f2SKenneth D. Merry 			    enabled = TRUE;
622991554f2SKenneth D. Merry 
623991554f2SKenneth D. Merry 		/*
62467feec50SStephen McConnell 		 * Set flags for some supported items.
625991554f2SKenneth D. Merry 		 */
626991554f2SKenneth D. Merry 		if (sc->facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP)
627991554f2SKenneth D. Merry 			sc->eedp_enabled = TRUE;
628991554f2SKenneth D. Merry 		if (sc->facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR)
629991554f2SKenneth D. Merry 			sc->control_TLR = TRUE;
63034c5490dSKashyap D Desai 		if ((sc->facts->IOCCapabilities &
63134c5490dSKashyap D Desai 		    MPI26_IOCFACTS_CAPABILITY_ATOMIC_REQ) &&
63234c5490dSKashyap D Desai 		    (sc->mpr_flags & MPR_FLAGS_SEA_IOC))
63367feec50SStephen McConnell 			sc->atomic_desc_capable = TRUE;
634991554f2SKenneth D. Merry 
6353c5ac992SScott Long 		mpr_resize_queues(sc);
636991554f2SKenneth D. Merry 
637991554f2SKenneth D. Merry 		/*
638991554f2SKenneth D. Merry 		 * Initialize all Tail Queues
639991554f2SKenneth D. Merry 		 */
640991554f2SKenneth D. Merry 		TAILQ_INIT(&sc->req_list);
641991554f2SKenneth D. Merry 		TAILQ_INIT(&sc->high_priority_req_list);
642991554f2SKenneth D. Merry 		TAILQ_INIT(&sc->chain_list);
64367feec50SStephen McConnell 		TAILQ_INIT(&sc->prp_page_list);
644991554f2SKenneth D. Merry 		TAILQ_INIT(&sc->tm_list);
645991554f2SKenneth D. Merry 	}
646991554f2SKenneth D. Merry 
647991554f2SKenneth D. Merry 	/*
648991554f2SKenneth D. Merry 	 * If doing a Diag Reset and the FW is significantly different
649991554f2SKenneth D. Merry 	 * (reallocating will be set above in IOC Facts comparison), then all
650991554f2SKenneth D. Merry 	 * buffers based on the IOC Facts will need to be freed before they are
651991554f2SKenneth D. Merry 	 * reallocated.
652991554f2SKenneth D. Merry 	 */
653991554f2SKenneth D. Merry 	if (reallocating) {
654991554f2SKenneth D. Merry 		mpr_iocfacts_free(sc);
655327f2e6cSStephen McConnell 		mprsas_realloc_targets(sc, saved_facts.MaxTargets +
656327f2e6cSStephen McConnell 		    saved_facts.MaxVolumes);
657991554f2SKenneth D. Merry 	}
658991554f2SKenneth D. Merry 
659991554f2SKenneth D. Merry 	/*
660991554f2SKenneth D. Merry 	 * Any deallocation has been completed.  Now start reallocating
661991554f2SKenneth D. Merry 	 * if needed.  Will only need to reallocate if attaching or if the new
662991554f2SKenneth D. Merry 	 * IOC Facts are different from the previous IOC Facts after a Diag
663991554f2SKenneth D. Merry 	 * Reset. Targets have already been allocated above if needed.
664991554f2SKenneth D. Merry 	 */
6651415db6cSScott Long 	error = 0;
6661415db6cSScott Long 	while (attaching || reallocating) {
6671415db6cSScott Long 		if ((error = mpr_alloc_hw_queues(sc)) != 0)
6681415db6cSScott Long 			break;
6691415db6cSScott Long 		if ((error = mpr_alloc_replies(sc)) != 0)
6701415db6cSScott Long 			break;
6711415db6cSScott Long 		if ((error = mpr_alloc_requests(sc)) != 0)
6721415db6cSScott Long 			break;
6731415db6cSScott Long 		if ((error = mpr_alloc_queues(sc)) != 0)
6741415db6cSScott Long 			break;
6751415db6cSScott Long 		break;
6761415db6cSScott Long 	}
6771415db6cSScott Long 	if (error) {
678757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR,
6791415db6cSScott Long 		    "Failed to alloc queues with error %d\n", error);
680991554f2SKenneth D. Merry 		mpr_free(sc);
681991554f2SKenneth D. Merry 		return (error);
682991554f2SKenneth D. Merry 	}
683991554f2SKenneth D. Merry 
684991554f2SKenneth D. Merry 	/* Always initialize the queues */
685991554f2SKenneth D. Merry 	bzero(sc->free_queue, sc->fqdepth * 4);
686991554f2SKenneth D. Merry 	mpr_init_queues(sc);
687991554f2SKenneth D. Merry 
688991554f2SKenneth D. Merry 	/*
689991554f2SKenneth D. Merry 	 * Always get the chip out of the reset state, but only panic if not
690991554f2SKenneth D. Merry 	 * attaching.  If attaching and there is an error, that is handled by
691991554f2SKenneth D. Merry 	 * the OS.
692991554f2SKenneth D. Merry 	 */
693991554f2SKenneth D. Merry 	error = mpr_transition_operational(sc);
694991554f2SKenneth D. Merry 	if (error != 0) {
695757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_FAULT, "Failed to "
696757ff642SScott Long 		    "transition to operational with error %d\n", error);
697991554f2SKenneth D. Merry 		mpr_free(sc);
698991554f2SKenneth D. Merry 		return (error);
699991554f2SKenneth D. Merry 	}
700991554f2SKenneth D. Merry 
701991554f2SKenneth D. Merry 	/*
702991554f2SKenneth D. Merry 	 * Finish the queue initialization.
703991554f2SKenneth D. Merry 	 * These are set here instead of in mpr_init_queues() because the
704991554f2SKenneth D. Merry 	 * IOC resets these values during the state transition in
705991554f2SKenneth D. Merry 	 * mpr_transition_operational().  The free index is set to 1
706991554f2SKenneth D. Merry 	 * because the corresponding index in the IOC is set to 0, and the
707991554f2SKenneth D. Merry 	 * IOC treats the queues as full if both are set to the same value.
708991554f2SKenneth D. Merry 	 * Hence the reason that the queue can't hold all of the possible
709991554f2SKenneth D. Merry 	 * replies.
710991554f2SKenneth D. Merry 	 */
711991554f2SKenneth D. Merry 	sc->replypostindex = 0;
712991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_REPLY_FREE_HOST_INDEX_OFFSET, sc->replyfreeindex);
713991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_REPLY_POST_HOST_INDEX_OFFSET, 0);
714991554f2SKenneth D. Merry 
715991554f2SKenneth D. Merry 	/*
716991554f2SKenneth D. Merry 	 * Attach the subsystems so they can prepare their event masks.
7171415db6cSScott Long 	 * XXX Should be dynamic so that IM/IR and user modules can attach
718991554f2SKenneth D. Merry 	 */
7191415db6cSScott Long 	error = 0;
7201415db6cSScott Long 	while (attaching) {
721757ff642SScott Long 		mpr_dprint(sc, MPR_INIT, "Attaching subsystems\n");
7221415db6cSScott Long 		if ((error = mpr_attach_log(sc)) != 0)
7231415db6cSScott Long 			break;
7241415db6cSScott Long 		if ((error = mpr_attach_sas(sc)) != 0)
7251415db6cSScott Long 			break;
7261415db6cSScott Long 		if ((error = mpr_attach_user(sc)) != 0)
7271415db6cSScott Long 			break;
7281415db6cSScott Long 		break;
7291415db6cSScott Long 	}
7301415db6cSScott Long 	if (error) {
731757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR,
7321415db6cSScott Long 		    "Failed to attach all subsystems: error %d\n", error);
733991554f2SKenneth D. Merry 		mpr_free(sc);
734991554f2SKenneth D. Merry 		return (error);
735991554f2SKenneth D. Merry 	}
736991554f2SKenneth D. Merry 
73710695417SScott Long 	/*
73810695417SScott Long 	 * XXX If the number of MSI-X vectors changes during re-init, this
73910695417SScott Long 	 * won't see it and adjust.
74010695417SScott Long 	 */
74111778fcaSKenneth D. Merry 	if ((attaching || reallocating) && (error = mpr_pci_setup_interrupts(sc)) != 0) {
742757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR,
743757ff642SScott Long 		    "Failed to setup interrupts\n");
744991554f2SKenneth D. Merry 		mpr_free(sc);
745991554f2SKenneth D. Merry 		return (error);
746991554f2SKenneth D. Merry 	}
747991554f2SKenneth D. Merry 
748991554f2SKenneth D. Merry 	return (error);
749991554f2SKenneth D. Merry }
750991554f2SKenneth D. Merry 
751991554f2SKenneth D. Merry /*
752991554f2SKenneth D. Merry  * This is called if memory is being free (during detach for example) and when
753991554f2SKenneth D. Merry  * buffers need to be reallocated due to a Diag Reset.
754991554f2SKenneth D. Merry  */
755991554f2SKenneth D. Merry static void
mpr_iocfacts_free(struct mpr_softc * sc)756991554f2SKenneth D. Merry mpr_iocfacts_free(struct mpr_softc *sc)
757991554f2SKenneth D. Merry {
758991554f2SKenneth D. Merry 	struct mpr_command *cm;
759991554f2SKenneth D. Merry 	int i;
760991554f2SKenneth D. Merry 
761991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
762991554f2SKenneth D. Merry 
763991554f2SKenneth D. Merry 	if (sc->free_busaddr != 0)
764991554f2SKenneth D. Merry 		bus_dmamap_unload(sc->queues_dmat, sc->queues_map);
765991554f2SKenneth D. Merry 	if (sc->free_queue != NULL)
766991554f2SKenneth D. Merry 		bus_dmamem_free(sc->queues_dmat, sc->free_queue,
767991554f2SKenneth D. Merry 		    sc->queues_map);
768991554f2SKenneth D. Merry 	if (sc->queues_dmat != NULL)
769991554f2SKenneth D. Merry 		bus_dma_tag_destroy(sc->queues_dmat);
770991554f2SKenneth D. Merry 
771731308d0SAlexander Motin 	if (sc->chain_frames != NULL) {
772991554f2SKenneth D. Merry 		bus_dmamap_unload(sc->chain_dmat, sc->chain_map);
773991554f2SKenneth D. Merry 		bus_dmamem_free(sc->chain_dmat, sc->chain_frames,
774991554f2SKenneth D. Merry 		    sc->chain_map);
775731308d0SAlexander Motin 	}
776991554f2SKenneth D. Merry 	if (sc->chain_dmat != NULL)
777991554f2SKenneth D. Merry 		bus_dma_tag_destroy(sc->chain_dmat);
778991554f2SKenneth D. Merry 
779991554f2SKenneth D. Merry 	if (sc->sense_busaddr != 0)
780991554f2SKenneth D. Merry 		bus_dmamap_unload(sc->sense_dmat, sc->sense_map);
781991554f2SKenneth D. Merry 	if (sc->sense_frames != NULL)
782991554f2SKenneth D. Merry 		bus_dmamem_free(sc->sense_dmat, sc->sense_frames,
783991554f2SKenneth D. Merry 		    sc->sense_map);
784991554f2SKenneth D. Merry 	if (sc->sense_dmat != NULL)
785991554f2SKenneth D. Merry 		bus_dma_tag_destroy(sc->sense_dmat);
786991554f2SKenneth D. Merry 
78767feec50SStephen McConnell 	if (sc->prp_page_busaddr != 0)
78867feec50SStephen McConnell 		bus_dmamap_unload(sc->prp_page_dmat, sc->prp_page_map);
78967feec50SStephen McConnell 	if (sc->prp_pages != NULL)
79067feec50SStephen McConnell 		bus_dmamem_free(sc->prp_page_dmat, sc->prp_pages,
79167feec50SStephen McConnell 		    sc->prp_page_map);
79267feec50SStephen McConnell 	if (sc->prp_page_dmat != NULL)
79367feec50SStephen McConnell 		bus_dma_tag_destroy(sc->prp_page_dmat);
79467feec50SStephen McConnell 
795991554f2SKenneth D. Merry 	if (sc->reply_busaddr != 0)
796991554f2SKenneth D. Merry 		bus_dmamap_unload(sc->reply_dmat, sc->reply_map);
797991554f2SKenneth D. Merry 	if (sc->reply_frames != NULL)
798991554f2SKenneth D. Merry 		bus_dmamem_free(sc->reply_dmat, sc->reply_frames,
799991554f2SKenneth D. Merry 		    sc->reply_map);
800991554f2SKenneth D. Merry 	if (sc->reply_dmat != NULL)
801991554f2SKenneth D. Merry 		bus_dma_tag_destroy(sc->reply_dmat);
802991554f2SKenneth D. Merry 
803991554f2SKenneth D. Merry 	if (sc->req_busaddr != 0)
804991554f2SKenneth D. Merry 		bus_dmamap_unload(sc->req_dmat, sc->req_map);
805991554f2SKenneth D. Merry 	if (sc->req_frames != NULL)
806991554f2SKenneth D. Merry 		bus_dmamem_free(sc->req_dmat, sc->req_frames, sc->req_map);
807991554f2SKenneth D. Merry 	if (sc->req_dmat != NULL)
808991554f2SKenneth D. Merry 		bus_dma_tag_destroy(sc->req_dmat);
809991554f2SKenneth D. Merry 
810991554f2SKenneth D. Merry 	if (sc->chains != NULL)
811991554f2SKenneth D. Merry 		free(sc->chains, M_MPR);
81267feec50SStephen McConnell 	if (sc->prps != NULL)
81367feec50SStephen McConnell 		free(sc->prps, M_MPR);
814991554f2SKenneth D. Merry 	if (sc->commands != NULL) {
815991554f2SKenneth D. Merry 		for (i = 1; i < sc->num_reqs; i++) {
816991554f2SKenneth D. Merry 			cm = &sc->commands[i];
817991554f2SKenneth D. Merry 			bus_dmamap_destroy(sc->buffer_dmat, cm->cm_dmamap);
818991554f2SKenneth D. Merry 		}
819991554f2SKenneth D. Merry 		free(sc->commands, M_MPR);
820991554f2SKenneth D. Merry 	}
821991554f2SKenneth D. Merry 	if (sc->buffer_dmat != NULL)
822991554f2SKenneth D. Merry 		bus_dma_tag_destroy(sc->buffer_dmat);
823bec09074SScott Long 
824bec09074SScott Long 	mpr_pci_free_interrupts(sc);
825bec09074SScott Long 	free(sc->queues, M_MPR);
826bec09074SScott Long 	sc->queues = NULL;
827991554f2SKenneth D. Merry }
828991554f2SKenneth D. Merry 
829991554f2SKenneth D. Merry /*
830991554f2SKenneth D. Merry  * The terms diag reset and hard reset are used interchangeably in the MPI
831991554f2SKenneth D. Merry  * docs to mean resetting the controller chip.  In this code diag reset
832991554f2SKenneth D. Merry  * cleans everything up, and the hard reset function just sends the reset
833991554f2SKenneth D. Merry  * sequence to the chip.  This should probably be refactored so that every
834991554f2SKenneth D. Merry  * subsystem gets a reset notification of some sort, and can clean up
835991554f2SKenneth D. Merry  * appropriately.
836991554f2SKenneth D. Merry  */
837991554f2SKenneth D. Merry int
mpr_reinit(struct mpr_softc * sc)838991554f2SKenneth D. Merry mpr_reinit(struct mpr_softc *sc)
839991554f2SKenneth D. Merry {
840991554f2SKenneth D. Merry 	int error;
841991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
842991554f2SKenneth D. Merry 
843991554f2SKenneth D. Merry 	sassc = sc->sassc;
844991554f2SKenneth D. Merry 
845991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
846991554f2SKenneth D. Merry 
847991554f2SKenneth D. Merry 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
848991554f2SKenneth D. Merry 
849757ff642SScott Long 	mpr_dprint(sc, MPR_INIT|MPR_INFO, "Reinitializing controller\n");
850991554f2SKenneth D. Merry 	if (sc->mpr_flags & MPR_FLAGS_DIAGRESET) {
851757ff642SScott Long 		mpr_dprint(sc, MPR_INIT, "Reset already in progress\n");
852991554f2SKenneth D. Merry 		return 0;
853991554f2SKenneth D. Merry 	}
854991554f2SKenneth D. Merry 
855757ff642SScott Long 	/*
856757ff642SScott Long 	 * Make sure the completion callbacks can recognize they're getting
857991554f2SKenneth D. Merry 	 * a NULL cm_reply due to a reset.
858991554f2SKenneth D. Merry 	 */
859991554f2SKenneth D. Merry 	sc->mpr_flags |= MPR_FLAGS_DIAGRESET;
860991554f2SKenneth D. Merry 
861991554f2SKenneth D. Merry 	/*
862991554f2SKenneth D. Merry 	 * Mask interrupts here.
863991554f2SKenneth D. Merry 	 */
864757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "Masking interrupts and resetting\n");
865991554f2SKenneth D. Merry 	mpr_mask_intr(sc);
866991554f2SKenneth D. Merry 
867991554f2SKenneth D. Merry 	error = mpr_diag_reset(sc, CAN_SLEEP);
868991554f2SKenneth D. Merry 	if (error != 0) {
869991554f2SKenneth D. Merry 		panic("%s hard reset failed with error %d\n", __func__, error);
870991554f2SKenneth D. Merry 	}
871991554f2SKenneth D. Merry 
872991554f2SKenneth D. Merry 	/* Restore the PCI state, including the MSI-X registers */
873991554f2SKenneth D. Merry 	mpr_pci_restore(sc);
874991554f2SKenneth D. Merry 
875991554f2SKenneth D. Merry 	/* Give the I/O subsystem special priority to get itself prepared */
876991554f2SKenneth D. Merry 	mprsas_handle_reinit(sc);
877991554f2SKenneth D. Merry 
878991554f2SKenneth D. Merry 	/*
879991554f2SKenneth D. Merry 	 * Get IOC Facts and allocate all structures based on this information.
880991554f2SKenneth D. Merry 	 * The attach function will also call mpr_iocfacts_allocate at startup.
881991554f2SKenneth D. Merry 	 * If relevant values have changed in IOC Facts, this function will free
882991554f2SKenneth D. Merry 	 * all of the memory based on IOC Facts and reallocate that memory.
883991554f2SKenneth D. Merry 	 */
884991554f2SKenneth D. Merry 	if ((error = mpr_iocfacts_allocate(sc, FALSE)) != 0) {
885991554f2SKenneth D. Merry 		panic("%s IOC Facts based allocation failed with error %d\n",
886991554f2SKenneth D. Merry 		    __func__, error);
887991554f2SKenneth D. Merry 	}
888991554f2SKenneth D. Merry 
889991554f2SKenneth D. Merry 	/*
890991554f2SKenneth D. Merry 	 * Mapping structures will be re-allocated after getting IOC Page8, so
891991554f2SKenneth D. Merry 	 * free these structures here.
892991554f2SKenneth D. Merry 	 */
893991554f2SKenneth D. Merry 	mpr_mapping_exit(sc);
894991554f2SKenneth D. Merry 
895991554f2SKenneth D. Merry 	/*
896991554f2SKenneth D. Merry 	 * The static page function currently read is IOC Page8.  Others can be
897991554f2SKenneth D. Merry 	 * added in future.  It's possible that the values in IOC Page8 have
898991554f2SKenneth D. Merry 	 * changed after a Diag Reset due to user modification, so always read
899991554f2SKenneth D. Merry 	 * these.  Interrupts are masked, so unmask them before getting config
900991554f2SKenneth D. Merry 	 * pages.
901991554f2SKenneth D. Merry 	 */
902991554f2SKenneth D. Merry 	mpr_unmask_intr(sc);
903991554f2SKenneth D. Merry 	sc->mpr_flags &= ~MPR_FLAGS_DIAGRESET;
904991554f2SKenneth D. Merry 	mpr_base_static_config_pages(sc);
905991554f2SKenneth D. Merry 
906991554f2SKenneth D. Merry 	/*
907991554f2SKenneth D. Merry 	 * Some mapping info is based in IOC Page8 data, so re-initialize the
908991554f2SKenneth D. Merry 	 * mapping tables.
909991554f2SKenneth D. Merry 	 */
910991554f2SKenneth D. Merry 	mpr_mapping_initialize(sc);
911991554f2SKenneth D. Merry 
912991554f2SKenneth D. Merry 	/*
913991554f2SKenneth D. Merry 	 * Restart will reload the event masks clobbered by the reset, and
914991554f2SKenneth D. Merry 	 * then enable the port.
915991554f2SKenneth D. Merry 	 */
916991554f2SKenneth D. Merry 	mpr_reregister_events(sc);
917991554f2SKenneth D. Merry 
918991554f2SKenneth D. Merry 	/* the end of discovery will release the simq, so we're done. */
919757ff642SScott Long 	mpr_dprint(sc, MPR_INIT|MPR_XINFO, "Finished sc %p post %u free %u\n",
920757ff642SScott Long 	    sc, sc->replypostindex, sc->replyfreeindex);
921991554f2SKenneth D. Merry 	mprsas_release_simq_reinit(sassc);
922757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit error= %d\n", __func__, error);
923991554f2SKenneth D. Merry 
924991554f2SKenneth D. Merry 	return 0;
925991554f2SKenneth D. Merry }
926991554f2SKenneth D. Merry 
927991554f2SKenneth D. Merry /* Wait for the chip to ACK a word that we've put into its FIFO
928991554f2SKenneth D. Merry  * Wait for <timeout> seconds. In single loop wait for busy loop
929991554f2SKenneth D. Merry  * for 500 microseconds.
930991554f2SKenneth D. Merry  * Total is [ 0.5 * (2000 * <timeout>) ] in miliseconds.
931991554f2SKenneth D. Merry  * */
932991554f2SKenneth D. Merry static int
mpr_wait_db_ack(struct mpr_softc * sc,int timeout,int sleep_flag)933991554f2SKenneth D. Merry mpr_wait_db_ack(struct mpr_softc *sc, int timeout, int sleep_flag)
934991554f2SKenneth D. Merry {
935991554f2SKenneth D. Merry 	u32 cntdn, count;
936991554f2SKenneth D. Merry 	u32 int_status;
937991554f2SKenneth D. Merry 	u32 doorbell;
938991554f2SKenneth D. Merry 
939991554f2SKenneth D. Merry 	count = 0;
940991554f2SKenneth D. Merry 	cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
941991554f2SKenneth D. Merry 	do {
942991554f2SKenneth D. Merry 		int_status = mpr_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET);
943991554f2SKenneth D. Merry 		if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
944757ff642SScott Long 			mpr_dprint(sc, MPR_TRACE, "%s: successful count(%d), "
945991554f2SKenneth D. Merry 			    "timeout(%d)\n", __func__, count, timeout);
946991554f2SKenneth D. Merry 			return 0;
947991554f2SKenneth D. Merry 		} else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
948991554f2SKenneth D. Merry 			doorbell = mpr_regread(sc, MPI2_DOORBELL_OFFSET);
949991554f2SKenneth D. Merry 			if ((doorbell & MPI2_IOC_STATE_MASK) ==
950991554f2SKenneth D. Merry 			    MPI2_IOC_STATE_FAULT) {
951991554f2SKenneth D. Merry 				mpr_dprint(sc, MPR_FAULT,
952991554f2SKenneth D. Merry 				    "fault_state(0x%04x)!\n", doorbell);
953991554f2SKenneth D. Merry 				return (EFAULT);
954991554f2SKenneth D. Merry 			}
955991554f2SKenneth D. Merry 		} else if (int_status == 0xFFFFFFFF)
956991554f2SKenneth D. Merry 			goto out;
957991554f2SKenneth D. Merry 
958991554f2SKenneth D. Merry 		/*
959991554f2SKenneth D. Merry 		 * If it can sleep, sleep for 1 milisecond, else busy loop for
960991554f2SKenneth D. Merry  		 * 0.5 milisecond
961991554f2SKenneth D. Merry 		 */
962991554f2SKenneth D. Merry 		if (mtx_owned(&sc->mpr_mtx) && sleep_flag == CAN_SLEEP)
963a2c14879SStephen McConnell 			msleep(&sc->msleep_fake_chan, &sc->mpr_mtx, 0, "mprdba",
964a2c14879SStephen McConnell 			    hz/1000);
965991554f2SKenneth D. Merry 		else if (sleep_flag == CAN_SLEEP)
966991554f2SKenneth D. Merry 			pause("mprdba", hz/1000);
967991554f2SKenneth D. Merry 		else
968991554f2SKenneth D. Merry 			DELAY(500);
969991554f2SKenneth D. Merry 		count++;
970991554f2SKenneth D. Merry 	} while (--cntdn);
971991554f2SKenneth D. Merry 
972991554f2SKenneth D. Merry out:
973991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_FAULT, "%s: failed due to timeout count(%d), "
974991554f2SKenneth D. Merry 		"int_status(%x)!\n", __func__, count, int_status);
975991554f2SKenneth D. Merry 	return (ETIMEDOUT);
976991554f2SKenneth D. Merry }
977991554f2SKenneth D. Merry 
978991554f2SKenneth D. Merry /* Wait for the chip to signal that the next word in its FIFO can be fetched */
979991554f2SKenneth D. Merry static int
mpr_wait_db_int(struct mpr_softc * sc)980991554f2SKenneth D. Merry mpr_wait_db_int(struct mpr_softc *sc)
981991554f2SKenneth D. Merry {
982991554f2SKenneth D. Merry 	int retry;
983991554f2SKenneth D. Merry 
984991554f2SKenneth D. Merry 	for (retry = 0; retry < MPR_DB_MAX_WAIT; retry++) {
985991554f2SKenneth D. Merry 		if ((mpr_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET) &
986991554f2SKenneth D. Merry 		    MPI2_HIS_IOC2SYS_DB_STATUS) != 0)
987991554f2SKenneth D. Merry 			return (0);
988991554f2SKenneth D. Merry 		DELAY(2000);
989991554f2SKenneth D. Merry 	}
990991554f2SKenneth D. Merry 	return (ETIMEDOUT);
991991554f2SKenneth D. Merry }
992991554f2SKenneth D. Merry 
993991554f2SKenneth D. Merry /* Step through the synchronous command state machine, i.e. "Doorbell mode" */
994991554f2SKenneth D. Merry static int
mpr_request_sync(struct mpr_softc * sc,void * req,MPI2_DEFAULT_REPLY * reply,int req_sz,int reply_sz,int timeout)995991554f2SKenneth D. Merry mpr_request_sync(struct mpr_softc *sc, void *req, MPI2_DEFAULT_REPLY *reply,
996991554f2SKenneth D. Merry     int req_sz, int reply_sz, int timeout)
997991554f2SKenneth D. Merry {
998991554f2SKenneth D. Merry 	uint32_t *data32;
999991554f2SKenneth D. Merry 	uint16_t *data16;
1000991554f2SKenneth D. Merry 	int i, count, ioc_sz, residual;
1001991554f2SKenneth D. Merry 	int sleep_flags = CAN_SLEEP;
1002991554f2SKenneth D. Merry 
1003991554f2SKenneth D. Merry 	if (curthread->td_no_sleeping)
1004991554f2SKenneth D. Merry 		sleep_flags = NO_SLEEP;
1005991554f2SKenneth D. Merry 
1006991554f2SKenneth D. Merry 	/* Step 1 */
1007991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1008991554f2SKenneth D. Merry 
1009991554f2SKenneth D. Merry 	/* Step 2 */
1010991554f2SKenneth D. Merry 	if (mpr_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED)
1011991554f2SKenneth D. Merry 		return (EBUSY);
1012991554f2SKenneth D. Merry 
1013991554f2SKenneth D. Merry 	/* Step 3
1014991554f2SKenneth D. Merry 	 * Announce that a message is coming through the doorbell.  Messages
1015991554f2SKenneth D. Merry 	 * are pushed at 32bit words, so round up if needed.
1016991554f2SKenneth D. Merry 	 */
1017991554f2SKenneth D. Merry 	count = (req_sz + 3) / 4;
1018991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_DOORBELL_OFFSET,
1019991554f2SKenneth D. Merry 	    (MPI2_FUNCTION_HANDSHAKE << MPI2_DOORBELL_FUNCTION_SHIFT) |
1020991554f2SKenneth D. Merry 	    (count << MPI2_DOORBELL_ADD_DWORDS_SHIFT));
1021991554f2SKenneth D. Merry 
1022991554f2SKenneth D. Merry 	/* Step 4 */
1023991554f2SKenneth D. Merry 	if (mpr_wait_db_int(sc) ||
1024991554f2SKenneth D. Merry 	    (mpr_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED) == 0) {
1025991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Doorbell failed to activate\n");
1026991554f2SKenneth D. Merry 		return (ENXIO);
1027991554f2SKenneth D. Merry 	}
1028991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1029991554f2SKenneth D. Merry 	if (mpr_wait_db_ack(sc, 5, sleep_flags) != 0) {
1030991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Doorbell handshake failed\n");
1031991554f2SKenneth D. Merry 		return (ENXIO);
1032991554f2SKenneth D. Merry 	}
1033991554f2SKenneth D. Merry 
1034991554f2SKenneth D. Merry 	/* Step 5 */
1035991554f2SKenneth D. Merry 	/* Clock out the message data synchronously in 32-bit dwords*/
1036991554f2SKenneth D. Merry 	data32 = (uint32_t *)req;
1037991554f2SKenneth D. Merry 	for (i = 0; i < count; i++) {
1038991554f2SKenneth D. Merry 		mpr_regwrite(sc, MPI2_DOORBELL_OFFSET, htole32(data32[i]));
1039991554f2SKenneth D. Merry 		if (mpr_wait_db_ack(sc, 5, sleep_flags) != 0) {
1040991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT,
1041991554f2SKenneth D. Merry 			    "Timeout while writing doorbell\n");
1042991554f2SKenneth D. Merry 			return (ENXIO);
1043991554f2SKenneth D. Merry 		}
1044991554f2SKenneth D. Merry 	}
1045991554f2SKenneth D. Merry 
1046991554f2SKenneth D. Merry 	/* Step 6 */
1047991554f2SKenneth D. Merry 	/* Clock in the reply in 16-bit words.  The total length of the
1048991554f2SKenneth D. Merry 	 * message is always in the 4th byte, so clock out the first 2 words
1049991554f2SKenneth D. Merry 	 * manually, then loop the rest.
1050991554f2SKenneth D. Merry 	 */
1051991554f2SKenneth D. Merry 	data16 = (uint16_t *)reply;
1052991554f2SKenneth D. Merry 	if (mpr_wait_db_int(sc) != 0) {
1053991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Timeout reading doorbell 0\n");
1054991554f2SKenneth D. Merry 		return (ENXIO);
1055991554f2SKenneth D. Merry 	}
105671900a79SAlfredo Dal'Ava Junior 
105771900a79SAlfredo Dal'Ava Junior 	/*
105871900a79SAlfredo Dal'Ava Junior 	 * If in a BE platform, swap bytes using le16toh to not
105971900a79SAlfredo Dal'Ava Junior 	 * disturb 8 bit field neighbors in destination structure
106071900a79SAlfredo Dal'Ava Junior 	 * pointed by data16.
106171900a79SAlfredo Dal'Ava Junior 	 */
1062991554f2SKenneth D. Merry 	data16[0] =
106371900a79SAlfredo Dal'Ava Junior 	    le16toh(mpr_regread(sc, MPI2_DOORBELL_OFFSET)) & MPI2_DOORBELL_DATA_MASK;
1064991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1065991554f2SKenneth D. Merry 	if (mpr_wait_db_int(sc) != 0) {
1066991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Timeout reading doorbell 1\n");
1067991554f2SKenneth D. Merry 		return (ENXIO);
1068991554f2SKenneth D. Merry 	}
1069991554f2SKenneth D. Merry 	data16[1] =
107071900a79SAlfredo Dal'Ava Junior 	    le16toh(mpr_regread(sc, MPI2_DOORBELL_OFFSET)) & MPI2_DOORBELL_DATA_MASK;
1071991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1072991554f2SKenneth D. Merry 
1073991554f2SKenneth D. Merry 	/* Number of 32bit words in the message */
1074991554f2SKenneth D. Merry 	ioc_sz = reply->MsgLength;
1075991554f2SKenneth D. Merry 
1076991554f2SKenneth D. Merry 	/*
1077991554f2SKenneth D. Merry 	 * Figure out how many 16bit words to clock in without overrunning.
1078991554f2SKenneth D. Merry 	 * The precision loss with dividing reply_sz can safely be
1079991554f2SKenneth D. Merry 	 * ignored because the messages can only be multiples of 32bits.
1080991554f2SKenneth D. Merry 	 */
1081991554f2SKenneth D. Merry 	residual = 0;
1082991554f2SKenneth D. Merry 	count = MIN((reply_sz / 4), ioc_sz) * 2;
1083991554f2SKenneth D. Merry 	if (count < ioc_sz * 2) {
1084991554f2SKenneth D. Merry 		residual = ioc_sz * 2 - count;
1085991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "Driver error, throwing away %d "
1086991554f2SKenneth D. Merry 		    "residual message words\n", residual);
1087991554f2SKenneth D. Merry 	}
1088991554f2SKenneth D. Merry 
1089991554f2SKenneth D. Merry 	for (i = 2; i < count; i++) {
1090991554f2SKenneth D. Merry 		if (mpr_wait_db_int(sc) != 0) {
1091991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT,
1092991554f2SKenneth D. Merry 			    "Timeout reading doorbell %d\n", i);
1093991554f2SKenneth D. Merry 			return (ENXIO);
1094991554f2SKenneth D. Merry 		}
109571900a79SAlfredo Dal'Ava Junior 		data16[i] = le16toh(mpr_regread(sc, MPI2_DOORBELL_OFFSET)) &
1096991554f2SKenneth D. Merry 		    MPI2_DOORBELL_DATA_MASK;
1097991554f2SKenneth D. Merry 		mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1098991554f2SKenneth D. Merry 	}
1099991554f2SKenneth D. Merry 
1100991554f2SKenneth D. Merry 	/*
1101991554f2SKenneth D. Merry 	 * Pull out residual words that won't fit into the provided buffer.
1102991554f2SKenneth D. Merry 	 * This keeps the chip from hanging due to a driver programming
1103991554f2SKenneth D. Merry 	 * error.
1104991554f2SKenneth D. Merry 	 */
1105991554f2SKenneth D. Merry 	while (residual--) {
1106991554f2SKenneth D. Merry 		if (mpr_wait_db_int(sc) != 0) {
1107991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT, "Timeout reading doorbell\n");
1108991554f2SKenneth D. Merry 			return (ENXIO);
1109991554f2SKenneth D. Merry 		}
1110991554f2SKenneth D. Merry 		(void)mpr_regread(sc, MPI2_DOORBELL_OFFSET);
1111991554f2SKenneth D. Merry 		mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1112991554f2SKenneth D. Merry 	}
1113991554f2SKenneth D. Merry 
1114991554f2SKenneth D. Merry 	/* Step 7 */
1115991554f2SKenneth D. Merry 	if (mpr_wait_db_int(sc) != 0) {
1116991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Timeout waiting to exit doorbell\n");
1117991554f2SKenneth D. Merry 		return (ENXIO);
1118991554f2SKenneth D. Merry 	}
1119991554f2SKenneth D. Merry 	if (mpr_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED)
1120991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Warning, doorbell still active\n");
1121991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1122991554f2SKenneth D. Merry 
1123991554f2SKenneth D. Merry 	return (0);
1124991554f2SKenneth D. Merry }
1125991554f2SKenneth D. Merry 
1126991554f2SKenneth D. Merry static void
mpr_enqueue_request(struct mpr_softc * sc,struct mpr_command * cm)1127991554f2SKenneth D. Merry mpr_enqueue_request(struct mpr_softc *sc, struct mpr_command *cm)
1128991554f2SKenneth D. Merry {
11294bc604dcSScott Long 	request_descriptor_t rd;
1130991554f2SKenneth D. Merry 
1131991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
1132a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_TRACE, "SMID %u cm %p ccb %p\n",
1133991554f2SKenneth D. Merry 	    cm->cm_desc.Default.SMID, cm, cm->cm_ccb);
1134991554f2SKenneth D. Merry 
1135991554f2SKenneth D. Merry 	if (sc->mpr_flags & MPR_FLAGS_ATTACH_DONE && !(sc->mpr_flags &
1136991554f2SKenneth D. Merry 	    MPR_FLAGS_SHUTDOWN))
1137991554f2SKenneth D. Merry 		mtx_assert(&sc->mpr_mtx, MA_OWNED);
1138991554f2SKenneth D. Merry 
1139991554f2SKenneth D. Merry 	if (++sc->io_cmds_active > sc->io_cmds_highwater)
1140991554f2SKenneth D. Merry 		sc->io_cmds_highwater++;
1141991554f2SKenneth D. Merry 
1142175ad3d0SKenneth D. Merry 	KASSERT(cm->cm_state == MPR_CM_STATE_BUSY,
1143175ad3d0SKenneth D. Merry 	    ("command not busy, state = %u\n", cm->cm_state));
1144f0779b04SScott Long 	cm->cm_state = MPR_CM_STATE_INQUEUE;
1145f0779b04SScott Long 
114667feec50SStephen McConnell 	if (sc->atomic_desc_capable) {
114767feec50SStephen McConnell 		rd.u.low = cm->cm_desc.Words.Low;
114867feec50SStephen McConnell 		mpr_regwrite(sc, MPI26_ATOMIC_REQUEST_DESCRIPTOR_POST_OFFSET,
114967feec50SStephen McConnell 		    rd.u.low);
115067feec50SStephen McConnell 	} else {
115171900a79SAlfredo Dal'Ava Junior 		rd.u.low = htole32(cm->cm_desc.Words.Low);
115271900a79SAlfredo Dal'Ava Junior 		rd.u.high = htole32(cm->cm_desc.Words.High);
1153991554f2SKenneth D. Merry 		mpr_regwrite(sc, MPI2_REQUEST_DESCRIPTOR_POST_LOW_OFFSET,
1154991554f2SKenneth D. Merry 		    rd.u.low);
1155991554f2SKenneth D. Merry 		mpr_regwrite(sc, MPI2_REQUEST_DESCRIPTOR_POST_HIGH_OFFSET,
1156991554f2SKenneth D. Merry 		    rd.u.high);
1157991554f2SKenneth D. Merry 	}
115867feec50SStephen McConnell }
1159991554f2SKenneth D. Merry 
1160991554f2SKenneth D. Merry /*
116171900a79SAlfredo Dal'Ava Junior  * Ioc facts are read in 16 bit words and and stored with le16toh,
116271900a79SAlfredo Dal'Ava Junior  * this takes care of proper U8 fields endianness in
116371900a79SAlfredo Dal'Ava Junior  * MPI2_IOC_FACTS_REPLY, but we still need to swap back U16 fields.
116471900a79SAlfredo Dal'Ava Junior  */
116571900a79SAlfredo Dal'Ava Junior static void
adjust_iocfacts_endianness(MPI2_IOC_FACTS_REPLY * facts)116671900a79SAlfredo Dal'Ava Junior adjust_iocfacts_endianness(MPI2_IOC_FACTS_REPLY *facts)
116771900a79SAlfredo Dal'Ava Junior {
116871900a79SAlfredo Dal'Ava Junior 	facts->HeaderVersion = le16toh(facts->HeaderVersion);
116971900a79SAlfredo Dal'Ava Junior 	facts->Reserved1 = le16toh(facts->Reserved1);
117071900a79SAlfredo Dal'Ava Junior 	facts->IOCExceptions = le16toh(facts->IOCExceptions);
117171900a79SAlfredo Dal'Ava Junior 	facts->IOCStatus = le16toh(facts->IOCStatus);
117271900a79SAlfredo Dal'Ava Junior 	facts->IOCLogInfo = le32toh(facts->IOCLogInfo);
117371900a79SAlfredo Dal'Ava Junior 	facts->RequestCredit = le16toh(facts->RequestCredit);
117471900a79SAlfredo Dal'Ava Junior 	facts->ProductID = le16toh(facts->ProductID);
117571900a79SAlfredo Dal'Ava Junior 	facts->IOCCapabilities = le32toh(facts->IOCCapabilities);
117671900a79SAlfredo Dal'Ava Junior 	facts->IOCRequestFrameSize = le16toh(facts->IOCRequestFrameSize);
117771900a79SAlfredo Dal'Ava Junior 	facts->IOCMaxChainSegmentSize = le16toh(facts->IOCMaxChainSegmentSize);
117871900a79SAlfredo Dal'Ava Junior 	facts->MaxInitiators = le16toh(facts->MaxInitiators);
117971900a79SAlfredo Dal'Ava Junior 	facts->MaxTargets = le16toh(facts->MaxTargets);
118071900a79SAlfredo Dal'Ava Junior 	facts->MaxSasExpanders = le16toh(facts->MaxSasExpanders);
118171900a79SAlfredo Dal'Ava Junior 	facts->MaxEnclosures = le16toh(facts->MaxEnclosures);
118271900a79SAlfredo Dal'Ava Junior 	facts->ProtocolFlags = le16toh(facts->ProtocolFlags);
118371900a79SAlfredo Dal'Ava Junior 	facts->HighPriorityCredit = le16toh(facts->HighPriorityCredit);
118471900a79SAlfredo Dal'Ava Junior 	facts->MaxReplyDescriptorPostQueueDepth = le16toh(facts->MaxReplyDescriptorPostQueueDepth);
118571900a79SAlfredo Dal'Ava Junior 	facts->MaxDevHandle = le16toh(facts->MaxDevHandle);
118671900a79SAlfredo Dal'Ava Junior 	facts->MaxPersistentEntries = le16toh(facts->MaxPersistentEntries);
118771900a79SAlfredo Dal'Ava Junior 	facts->MinDevHandle = le16toh(facts->MinDevHandle);
118871900a79SAlfredo Dal'Ava Junior }
118971900a79SAlfredo Dal'Ava Junior 
119071900a79SAlfredo Dal'Ava Junior /*
1191991554f2SKenneth D. Merry  * Just the FACTS, ma'am.
1192991554f2SKenneth D. Merry  */
1193991554f2SKenneth D. Merry static int
mpr_get_iocfacts(struct mpr_softc * sc,MPI2_IOC_FACTS_REPLY * facts)1194991554f2SKenneth D. Merry mpr_get_iocfacts(struct mpr_softc *sc, MPI2_IOC_FACTS_REPLY *facts)
1195991554f2SKenneth D. Merry {
1196991554f2SKenneth D. Merry 	MPI2_DEFAULT_REPLY *reply;
1197991554f2SKenneth D. Merry 	MPI2_IOC_FACTS_REQUEST request;
1198*c0e0e530Sprateek sethi 	int error, req_sz, reply_sz, retry = 0;
1199991554f2SKenneth D. Merry 
1200991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
1201757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
1202991554f2SKenneth D. Merry 
1203991554f2SKenneth D. Merry 	req_sz = sizeof(MPI2_IOC_FACTS_REQUEST);
1204991554f2SKenneth D. Merry 	reply_sz = sizeof(MPI2_IOC_FACTS_REPLY);
1205991554f2SKenneth D. Merry 	reply = (MPI2_DEFAULT_REPLY *)facts;
1206991554f2SKenneth D. Merry 
1207*c0e0e530Sprateek sethi 	/*
1208*c0e0e530Sprateek sethi 	 * Retry sending the initialization sequence. Sometimes, especially with
1209*c0e0e530Sprateek sethi 	 * older firmware, the initialization process fails. Retrying allows the
1210*c0e0e530Sprateek sethi 	 * error to clear in the firmware.
1211*c0e0e530Sprateek sethi 	 */
1212991554f2SKenneth D. Merry 	bzero(&request, req_sz);
1213991554f2SKenneth D. Merry 	request.Function = MPI2_FUNCTION_IOC_FACTS;
1214*c0e0e530Sprateek sethi 	while (retry < 5) {
1215991554f2SKenneth D. Merry 		error = mpr_request_sync(sc, &request, reply, req_sz, reply_sz, 5);
1216*c0e0e530Sprateek sethi 		if (error == 0)
1217*c0e0e530Sprateek sethi 			break;
1218*c0e0e530Sprateek sethi 		mpr_dprint(sc, MPR_FAULT, "%s failed retry %d\n", __func__, retry);
1219*c0e0e530Sprateek sethi 		DELAY(1000);
1220*c0e0e530Sprateek sethi                 retry++;
1221*c0e0e530Sprateek sethi 	}
1222991554f2SKenneth D. Merry 
1223*c0e0e530Sprateek sethi 	if (error == 0) {
122471900a79SAlfredo Dal'Ava Junior 		adjust_iocfacts_endianness(facts);
122571900a79SAlfredo Dal'Ava Junior 		mpr_dprint(sc, MPR_TRACE, "facts->IOCCapabilities 0x%x\n", facts->IOCCapabilities);
1226*c0e0e530Sprateek sethi 	}
1227757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit, error= %d\n", __func__, error);
1228991554f2SKenneth D. Merry 	return (error);
1229991554f2SKenneth D. Merry }
1230991554f2SKenneth D. Merry 
1231991554f2SKenneth D. Merry static int
mpr_send_iocinit(struct mpr_softc * sc)1232991554f2SKenneth D. Merry mpr_send_iocinit(struct mpr_softc *sc)
1233991554f2SKenneth D. Merry {
1234991554f2SKenneth D. Merry 	MPI2_IOC_INIT_REQUEST	init;
1235991554f2SKenneth D. Merry 	MPI2_DEFAULT_REPLY	reply;
1236991554f2SKenneth D. Merry 	int req_sz, reply_sz, error;
1237991554f2SKenneth D. Merry 	struct timeval now;
1238991554f2SKenneth D. Merry 	uint64_t time_in_msec;
1239991554f2SKenneth D. Merry 
1240991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
1241757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
1242991554f2SKenneth D. Merry 
124396410703SScott Long 	/* Do a quick sanity check on proper initialization */
124496410703SScott Long 	if ((sc->pqdepth == 0) || (sc->fqdepth == 0) || (sc->reqframesz == 0)
124596410703SScott Long 	    || (sc->replyframesz == 0)) {
124696410703SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR,
124796410703SScott Long 		    "Driver not fully initialized for IOCInit\n");
124896410703SScott Long 		return (EINVAL);
124996410703SScott Long 	}
125096410703SScott Long 
1251991554f2SKenneth D. Merry 	req_sz = sizeof(MPI2_IOC_INIT_REQUEST);
1252991554f2SKenneth D. Merry 	reply_sz = sizeof(MPI2_IOC_INIT_REPLY);
1253991554f2SKenneth D. Merry 	bzero(&init, req_sz);
1254991554f2SKenneth D. Merry 	bzero(&reply, reply_sz);
1255991554f2SKenneth D. Merry 
1256991554f2SKenneth D. Merry 	/*
1257991554f2SKenneth D. Merry 	 * Fill in the init block.  Note that most addresses are
1258991554f2SKenneth D. Merry 	 * deliberately in the lower 32bits of memory.  This is a micro-
1259991554f2SKenneth D. Merry 	 * optimzation for PCI/PCIX, though it's not clear if it helps PCIe.
1260991554f2SKenneth D. Merry 	 */
1261991554f2SKenneth D. Merry 	init.Function = MPI2_FUNCTION_IOC_INIT;
1262991554f2SKenneth D. Merry 	init.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
1263991554f2SKenneth D. Merry 	init.MsgVersion = htole16(MPI2_VERSION);
1264991554f2SKenneth D. Merry 	init.HeaderVersion = htole16(MPI2_HEADER_VERSION);
126596410703SScott Long 	init.SystemRequestFrameSize = htole16((uint16_t)(sc->reqframesz / 4));
1266991554f2SKenneth D. Merry 	init.ReplyDescriptorPostQueueDepth = htole16(sc->pqdepth);
1267991554f2SKenneth D. Merry 	init.ReplyFreeQueueDepth = htole16(sc->fqdepth);
1268991554f2SKenneth D. Merry 	init.SenseBufferAddressHigh = 0;
1269991554f2SKenneth D. Merry 	init.SystemReplyAddressHigh = 0;
1270991554f2SKenneth D. Merry 	init.SystemRequestFrameBaseAddress.High = 0;
1271991554f2SKenneth D. Merry 	init.SystemRequestFrameBaseAddress.Low =
1272991554f2SKenneth D. Merry 	    htole32((uint32_t)sc->req_busaddr);
1273991554f2SKenneth D. Merry 	init.ReplyDescriptorPostQueueAddress.High = 0;
1274991554f2SKenneth D. Merry 	init.ReplyDescriptorPostQueueAddress.Low =
1275991554f2SKenneth D. Merry 	    htole32((uint32_t)sc->post_busaddr);
1276991554f2SKenneth D. Merry 	init.ReplyFreeQueueAddress.High = 0;
1277991554f2SKenneth D. Merry 	init.ReplyFreeQueueAddress.Low = htole32((uint32_t)sc->free_busaddr);
1278991554f2SKenneth D. Merry 	getmicrotime(&now);
1279991554f2SKenneth D. Merry 	time_in_msec = (now.tv_sec * 1000 + now.tv_usec/1000);
1280991554f2SKenneth D. Merry 	init.TimeStamp.High = htole32((time_in_msec >> 32) & 0xFFFFFFFF);
1281991554f2SKenneth D. Merry 	init.TimeStamp.Low = htole32(time_in_msec & 0xFFFFFFFF);
128267feec50SStephen McConnell 	init.HostPageSize = HOST_PAGE_SIZE_4K;
1283991554f2SKenneth D. Merry 
1284991554f2SKenneth D. Merry 	error = mpr_request_sync(sc, &init, &reply, req_sz, reply_sz, 5);
128571900a79SAlfredo Dal'Ava Junior 	if ((le16toh(reply.IOCStatus) & MPI2_IOCSTATUS_MASK) != MPI2_IOCSTATUS_SUCCESS)
1286991554f2SKenneth D. Merry 		error = ENXIO;
1287991554f2SKenneth D. Merry 
128871900a79SAlfredo Dal'Ava Junior 	mpr_dprint(sc, MPR_INIT, "IOCInit status= 0x%x\n", le16toh(reply.IOCStatus));
1289757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit\n", __func__);
1290991554f2SKenneth D. Merry 	return (error);
1291991554f2SKenneth D. Merry }
1292991554f2SKenneth D. Merry 
1293991554f2SKenneth D. Merry void
mpr_memaddr_cb(void * arg,bus_dma_segment_t * segs,int nsegs,int error)1294991554f2SKenneth D. Merry mpr_memaddr_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1295991554f2SKenneth D. Merry {
1296991554f2SKenneth D. Merry 	bus_addr_t *addr;
1297991554f2SKenneth D. Merry 
1298991554f2SKenneth D. Merry 	addr = arg;
1299991554f2SKenneth D. Merry 	*addr = segs[0].ds_addr;
1300991554f2SKenneth D. Merry }
1301991554f2SKenneth D. Merry 
1302e2997a03SKenneth D. Merry void
mpr_memaddr_wait_cb(void * arg,bus_dma_segment_t * segs,int nsegs,int error)1303e2997a03SKenneth D. Merry mpr_memaddr_wait_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1304e2997a03SKenneth D. Merry {
1305e2997a03SKenneth D. Merry 	struct mpr_busdma_context *ctx;
1306e2997a03SKenneth D. Merry 	int need_unload, need_free;
1307e2997a03SKenneth D. Merry 
1308e2997a03SKenneth D. Merry 	ctx = (struct mpr_busdma_context *)arg;
1309e2997a03SKenneth D. Merry 	need_unload = 0;
1310e2997a03SKenneth D. Merry 	need_free = 0;
1311e2997a03SKenneth D. Merry 
1312e2997a03SKenneth D. Merry 	mpr_lock(ctx->softc);
1313e2997a03SKenneth D. Merry 	ctx->error = error;
1314e2997a03SKenneth D. Merry 	ctx->completed = 1;
1315e2997a03SKenneth D. Merry 	if ((error == 0) && (ctx->abandoned == 0)) {
1316e2997a03SKenneth D. Merry 		*ctx->addr = segs[0].ds_addr;
1317e2997a03SKenneth D. Merry 	} else {
1318e2997a03SKenneth D. Merry 		if (nsegs != 0)
1319e2997a03SKenneth D. Merry 			need_unload = 1;
1320e2997a03SKenneth D. Merry 		if (ctx->abandoned != 0)
1321e2997a03SKenneth D. Merry 			need_free = 1;
1322e2997a03SKenneth D. Merry 	}
1323e2997a03SKenneth D. Merry 	if (need_free == 0)
1324e2997a03SKenneth D. Merry 		wakeup(ctx);
1325e2997a03SKenneth D. Merry 
1326e2997a03SKenneth D. Merry 	mpr_unlock(ctx->softc);
1327e2997a03SKenneth D. Merry 
1328e2997a03SKenneth D. Merry 	if (need_unload != 0) {
1329e2997a03SKenneth D. Merry 		bus_dmamap_unload(ctx->buffer_dmat,
1330e2997a03SKenneth D. Merry 				  ctx->buffer_dmamap);
1331e2997a03SKenneth D. Merry 		*ctx->addr = 0;
1332e2997a03SKenneth D. Merry 	}
1333e2997a03SKenneth D. Merry 
1334e2997a03SKenneth D. Merry 	if (need_free != 0)
1335e2997a03SKenneth D. Merry 		free(ctx, M_MPR);
1336e2997a03SKenneth D. Merry }
1337e2997a03SKenneth D. Merry 
1338991554f2SKenneth D. Merry static int
mpr_alloc_queues(struct mpr_softc * sc)1339991554f2SKenneth D. Merry mpr_alloc_queues(struct mpr_softc *sc)
1340991554f2SKenneth D. Merry {
1341bec09074SScott Long 	struct mpr_queue *q;
13421415db6cSScott Long 	int nq, i;
1343bec09074SScott Long 
13443c5ac992SScott Long 	nq = sc->msi_msgs;
1345bec09074SScott Long 	mpr_dprint(sc, MPR_INIT|MPR_XINFO, "Allocating %d I/O queues\n", nq);
1346bec09074SScott Long 
1347ac2fffa4SPedro F. Giffuni 	sc->queues = malloc(sizeof(struct mpr_queue) * nq, M_MPR,
13483c5ac992SScott Long 	     M_NOWAIT|M_ZERO);
1349bec09074SScott Long 	if (sc->queues == NULL)
1350bec09074SScott Long 		return (ENOMEM);
1351bec09074SScott Long 
1352bec09074SScott Long 	for (i = 0; i < nq; i++) {
1353bec09074SScott Long 		q = &sc->queues[i];
1354bec09074SScott Long 		mpr_dprint(sc, MPR_INIT, "Configuring queue %d %p\n", i, q);
1355bec09074SScott Long 		q->sc = sc;
1356bec09074SScott Long 		q->qnum = i;
1357bec09074SScott Long 	}
13581415db6cSScott Long 	return (0);
13591415db6cSScott Long }
13601415db6cSScott Long 
13611415db6cSScott Long static int
mpr_alloc_hw_queues(struct mpr_softc * sc)13621415db6cSScott Long mpr_alloc_hw_queues(struct mpr_softc *sc)
13631415db6cSScott Long {
136474c781edSScott Long 	bus_dma_template_t t;
13651415db6cSScott Long 	bus_addr_t queues_busaddr;
13661415db6cSScott Long 	uint8_t *queues;
13671415db6cSScott Long 	int qsize, fqsize, pqsize;
1368991554f2SKenneth D. Merry 
1369991554f2SKenneth D. Merry 	/*
1370991554f2SKenneth D. Merry 	 * The reply free queue contains 4 byte entries in multiples of 16 and
1371991554f2SKenneth D. Merry 	 * aligned on a 16 byte boundary. There must always be an unused entry.
1372991554f2SKenneth D. Merry 	 * This queue supplies fresh reply frames for the firmware to use.
1373991554f2SKenneth D. Merry 	 *
1374991554f2SKenneth D. Merry 	 * The reply descriptor post queue contains 8 byte entries in
1375991554f2SKenneth D. Merry 	 * multiples of 16 and aligned on a 16 byte boundary.  This queue
1376991554f2SKenneth D. Merry 	 * contains filled-in reply frames sent from the firmware to the host.
1377991554f2SKenneth D. Merry 	 *
1378991554f2SKenneth D. Merry 	 * These two queues are allocated together for simplicity.
1379991554f2SKenneth D. Merry 	 */
1380d9c9c81cSPedro F. Giffuni 	sc->fqdepth = roundup2(sc->num_replies + 1, 16);
1381d9c9c81cSPedro F. Giffuni 	sc->pqdepth = roundup2(sc->num_replies + 1, 16);
1382991554f2SKenneth D. Merry 	fqsize= sc->fqdepth * 4;
1383991554f2SKenneth D. Merry 	pqsize = sc->pqdepth * 8;
1384991554f2SKenneth D. Merry 	qsize = fqsize + pqsize;
1385991554f2SKenneth D. Merry 
1386f5ead205SScott Long 	bus_dma_template_init(&t, sc->mpr_parent_dmat);
138774c781edSScott Long 	BUS_DMA_TEMPLATE_FILL(&t, BD_ALIGNMENT(16), BD_MAXSIZE(qsize),
138874c781edSScott Long 	    BD_MAXSEGSIZE(qsize), BD_NSEGMENTS(1),
138974c781edSScott Long 	    BD_LOWADDR(BUS_SPACE_MAXADDR_32BIT));
1390f5ead205SScott Long 	if (bus_dma_template_tag(&t, &sc->queues_dmat)) {
1391757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate queues DMA tag\n");
1392991554f2SKenneth D. Merry 		return (ENOMEM);
1393991554f2SKenneth D. Merry         }
1394991554f2SKenneth D. Merry         if (bus_dmamem_alloc(sc->queues_dmat, (void **)&queues, BUS_DMA_NOWAIT,
1395991554f2SKenneth D. Merry 	    &sc->queues_map)) {
1396757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate queues memory\n");
1397991554f2SKenneth D. Merry 		return (ENOMEM);
1398991554f2SKenneth D. Merry         }
1399991554f2SKenneth D. Merry         bzero(queues, qsize);
1400991554f2SKenneth D. Merry         bus_dmamap_load(sc->queues_dmat, sc->queues_map, queues, qsize,
1401991554f2SKenneth D. Merry 	    mpr_memaddr_cb, &queues_busaddr, 0);
1402991554f2SKenneth D. Merry 
1403991554f2SKenneth D. Merry 	sc->free_queue = (uint32_t *)queues;
1404991554f2SKenneth D. Merry 	sc->free_busaddr = queues_busaddr;
1405991554f2SKenneth D. Merry 	sc->post_queue = (MPI2_REPLY_DESCRIPTORS_UNION *)(queues + fqsize);
1406991554f2SKenneth D. Merry 	sc->post_busaddr = queues_busaddr + fqsize;
140792ddc7b8SLi-Wen Hsu 	mpr_dprint(sc, MPR_INIT, "free queue busaddr= %#016jx size= %d\n",
140892ddc7b8SLi-Wen Hsu 	    (uintmax_t)sc->free_busaddr, fqsize);
140992ddc7b8SLi-Wen Hsu 	mpr_dprint(sc, MPR_INIT, "reply queue busaddr= %#016jx size= %d\n",
141092ddc7b8SLi-Wen Hsu 	    (uintmax_t)sc->post_busaddr, pqsize);
1411991554f2SKenneth D. Merry 
1412991554f2SKenneth D. Merry 	return (0);
1413991554f2SKenneth D. Merry }
1414991554f2SKenneth D. Merry 
1415991554f2SKenneth D. Merry static int
mpr_alloc_replies(struct mpr_softc * sc)1416991554f2SKenneth D. Merry mpr_alloc_replies(struct mpr_softc *sc)
1417991554f2SKenneth D. Merry {
141874c781edSScott Long 	bus_dma_template_t t;
1419991554f2SKenneth D. Merry 	int rsize, num_replies;
1420991554f2SKenneth D. Merry 
142196410703SScott Long 	/* Store the reply frame size in bytes rather than as 32bit words */
142296410703SScott Long 	sc->replyframesz = sc->facts->ReplyFrameSize * 4;
142396410703SScott Long 
1424991554f2SKenneth D. Merry 	/*
1425991554f2SKenneth D. Merry 	 * sc->num_replies should be one less than sc->fqdepth.  We need to
1426991554f2SKenneth D. Merry 	 * allocate space for sc->fqdepth replies, but only sc->num_replies
1427991554f2SKenneth D. Merry 	 * replies can be used at once.
1428991554f2SKenneth D. Merry 	 */
1429991554f2SKenneth D. Merry 	num_replies = max(sc->fqdepth, sc->num_replies);
1430991554f2SKenneth D. Merry 
143196410703SScott Long 	rsize = sc->replyframesz * num_replies;
1432f5ead205SScott Long 	bus_dma_template_init(&t, sc->mpr_parent_dmat);
143374c781edSScott Long 	BUS_DMA_TEMPLATE_FILL(&t, BD_ALIGNMENT(4), BD_MAXSIZE(rsize),
143474c781edSScott Long 	    BD_MAXSEGSIZE(rsize), BD_NSEGMENTS(1),
143574c781edSScott Long 	    BD_LOWADDR(BUS_SPACE_MAXADDR_32BIT));
1436f5ead205SScott Long 	if (bus_dma_template_tag(&t, &sc->reply_dmat)) {
1437757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate replies DMA tag\n");
1438991554f2SKenneth D. Merry 		return (ENOMEM);
1439991554f2SKenneth D. Merry         }
1440991554f2SKenneth D. Merry         if (bus_dmamem_alloc(sc->reply_dmat, (void **)&sc->reply_frames,
1441991554f2SKenneth D. Merry 	    BUS_DMA_NOWAIT, &sc->reply_map)) {
1442757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate replies memory\n");
1443991554f2SKenneth D. Merry 		return (ENOMEM);
1444991554f2SKenneth D. Merry         }
1445991554f2SKenneth D. Merry         bzero(sc->reply_frames, rsize);
1446991554f2SKenneth D. Merry         bus_dmamap_load(sc->reply_dmat, sc->reply_map, sc->reply_frames, rsize,
1447991554f2SKenneth D. Merry 	    mpr_memaddr_cb, &sc->reply_busaddr, 0);
144892ddc7b8SLi-Wen Hsu 	mpr_dprint(sc, MPR_INIT, "reply frames busaddr= %#016jx size= %d\n",
144992ddc7b8SLi-Wen Hsu 	    (uintmax_t)sc->reply_busaddr, rsize);
1450991554f2SKenneth D. Merry 
1451991554f2SKenneth D. Merry 	return (0);
1452991554f2SKenneth D. Merry }
1453991554f2SKenneth D. Merry 
1454731308d0SAlexander Motin static void
mpr_load_chains_cb(void * arg,bus_dma_segment_t * segs,int nsegs,int error)1455731308d0SAlexander Motin mpr_load_chains_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1456731308d0SAlexander Motin {
1457731308d0SAlexander Motin 	struct mpr_softc *sc = arg;
1458731308d0SAlexander Motin 	struct mpr_chain *chain;
1459731308d0SAlexander Motin 	bus_size_t bo;
1460731308d0SAlexander Motin 	int i, o, s;
1461731308d0SAlexander Motin 
1462731308d0SAlexander Motin 	if (error != 0)
1463731308d0SAlexander Motin 		return;
1464731308d0SAlexander Motin 
1465731308d0SAlexander Motin 	for (i = 0, o = 0, s = 0; s < nsegs; s++) {
1466731308d0SAlexander Motin 		for (bo = 0; bo + sc->chain_frame_size <= segs[s].ds_len;
1467731308d0SAlexander Motin 		    bo += sc->chain_frame_size) {
1468731308d0SAlexander Motin 			chain = &sc->chains[i++];
1469731308d0SAlexander Motin 			chain->chain =(MPI2_SGE_IO_UNION *)(sc->chain_frames+o);
1470731308d0SAlexander Motin 			chain->chain_busaddr = segs[s].ds_addr + bo;
1471731308d0SAlexander Motin 			o += sc->chain_frame_size;
1472731308d0SAlexander Motin 			mpr_free_chain(sc, chain);
1473731308d0SAlexander Motin 		}
1474731308d0SAlexander Motin 		if (bo != segs[s].ds_len)
1475731308d0SAlexander Motin 			o += segs[s].ds_len - bo;
1476731308d0SAlexander Motin 	}
1477731308d0SAlexander Motin 	sc->chain_free_lowwater = i;
1478731308d0SAlexander Motin }
1479731308d0SAlexander Motin 
1480991554f2SKenneth D. Merry static int
mpr_alloc_requests(struct mpr_softc * sc)1481991554f2SKenneth D. Merry mpr_alloc_requests(struct mpr_softc *sc)
1482991554f2SKenneth D. Merry {
148374c781edSScott Long 	bus_dma_template_t t;
1484991554f2SKenneth D. Merry 	struct mpr_command *cm;
1485991554f2SKenneth D. Merry 	int i, rsize, nsegs;
1486991554f2SKenneth D. Merry 
148796410703SScott Long 	rsize = sc->reqframesz * sc->num_reqs;
1488f5ead205SScott Long 	bus_dma_template_init(&t, sc->mpr_parent_dmat);
148974c781edSScott Long 	BUS_DMA_TEMPLATE_FILL(&t, BD_ALIGNMENT(16), BD_MAXSIZE(rsize),
149074c781edSScott Long 	    BD_MAXSEGSIZE(rsize), BD_NSEGMENTS(1),
149174c781edSScott Long 	    BD_LOWADDR(BUS_SPACE_MAXADDR_32BIT));
1492f5ead205SScott Long 	if (bus_dma_template_tag(&t, &sc->req_dmat)) {
1493757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate request DMA tag\n");
1494991554f2SKenneth D. Merry 		return (ENOMEM);
1495991554f2SKenneth D. Merry         }
1496991554f2SKenneth D. Merry         if (bus_dmamem_alloc(sc->req_dmat, (void **)&sc->req_frames,
1497991554f2SKenneth D. Merry 	    BUS_DMA_NOWAIT, &sc->req_map)) {
1498757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate request memory\n");
1499991554f2SKenneth D. Merry 		return (ENOMEM);
1500991554f2SKenneth D. Merry         }
1501991554f2SKenneth D. Merry         bzero(sc->req_frames, rsize);
1502991554f2SKenneth D. Merry         bus_dmamap_load(sc->req_dmat, sc->req_map, sc->req_frames, rsize,
1503991554f2SKenneth D. Merry 	    mpr_memaddr_cb, &sc->req_busaddr, 0);
150492ddc7b8SLi-Wen Hsu 	mpr_dprint(sc, MPR_INIT, "request frames busaddr= %#016jx size= %d\n",
150592ddc7b8SLi-Wen Hsu 	    (uintmax_t)sc->req_busaddr, rsize);
1506991554f2SKenneth D. Merry 
1507731308d0SAlexander Motin 	sc->chains = malloc(sizeof(struct mpr_chain) * sc->num_chains, M_MPR,
1508731308d0SAlexander Motin 	    M_NOWAIT | M_ZERO);
1509731308d0SAlexander Motin 	if (!sc->chains) {
1510731308d0SAlexander Motin 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate chain memory\n");
1511731308d0SAlexander Motin 		return (ENOMEM);
1512731308d0SAlexander Motin 	}
15134f5d6573SAlexander Motin 	rsize = sc->chain_frame_size * sc->num_chains;
1514f5ead205SScott Long 	bus_dma_template_init(&t, sc->mpr_parent_dmat);
151574c781edSScott Long 	BUS_DMA_TEMPLATE_FILL(&t, BD_ALIGNMENT(16), BD_MAXSIZE(rsize),
1516264610a8SKenneth D. Merry 	    BD_MAXSEGSIZE(rsize), BD_NSEGMENTS((howmany(rsize, PAGE_SIZE))),
1517264610a8SKenneth D. Merry 	    BD_BOUNDARY(BUS_SPACE_MAXSIZE_32BIT+1));
1518f5ead205SScott Long 	if (bus_dma_template_tag(&t, &sc->chain_dmat)) {
1519757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate chain DMA tag\n");
1520991554f2SKenneth D. Merry 		return (ENOMEM);
1521991554f2SKenneth D. Merry 	}
1522991554f2SKenneth D. Merry 	if (bus_dmamem_alloc(sc->chain_dmat, (void **)&sc->chain_frames,
1523731308d0SAlexander Motin 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->chain_map)) {
1524757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate chain memory\n");
1525991554f2SKenneth D. Merry 		return (ENOMEM);
1526991554f2SKenneth D. Merry 	}
1527731308d0SAlexander Motin 	if (bus_dmamap_load(sc->chain_dmat, sc->chain_map, sc->chain_frames,
1528731308d0SAlexander Motin 	    rsize, mpr_load_chains_cb, sc, BUS_DMA_NOWAIT)) {
1529731308d0SAlexander Motin 		mpr_dprint(sc, MPR_ERROR, "Cannot load chain memory\n");
1530731308d0SAlexander Motin 		bus_dmamem_free(sc->chain_dmat, sc->chain_frames,
1531731308d0SAlexander Motin 		    sc->chain_map);
1532731308d0SAlexander Motin 		return (ENOMEM);
1533731308d0SAlexander Motin 	}
1534991554f2SKenneth D. Merry 
1535991554f2SKenneth D. Merry 	rsize = MPR_SENSE_LEN * sc->num_reqs;
1536f5ead205SScott Long 	bus_dma_template_clone(&t, sc->req_dmat);
153774c781edSScott Long 	BUS_DMA_TEMPLATE_FILL(&t, BD_ALIGNMENT(1), BD_MAXSIZE(rsize),
153874c781edSScott Long 	    BD_MAXSEGSIZE(rsize));
1539f5ead205SScott Long 	if (bus_dma_template_tag(&t, &sc->sense_dmat)) {
1540757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate sense DMA tag\n");
1541991554f2SKenneth D. Merry 		return (ENOMEM);
1542991554f2SKenneth D. Merry         }
1543991554f2SKenneth D. Merry         if (bus_dmamem_alloc(sc->sense_dmat, (void **)&sc->sense_frames,
1544991554f2SKenneth D. Merry 	    BUS_DMA_NOWAIT, &sc->sense_map)) {
1545757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate sense memory\n");
1546991554f2SKenneth D. Merry 		return (ENOMEM);
1547991554f2SKenneth D. Merry         }
1548991554f2SKenneth D. Merry         bzero(sc->sense_frames, rsize);
1549991554f2SKenneth D. Merry         bus_dmamap_load(sc->sense_dmat, sc->sense_map, sc->sense_frames, rsize,
1550991554f2SKenneth D. Merry 	    mpr_memaddr_cb, &sc->sense_busaddr, 0);
155192ddc7b8SLi-Wen Hsu 	mpr_dprint(sc, MPR_INIT, "sense frames busaddr= %#016jx size= %d\n",
155292ddc7b8SLi-Wen Hsu 	    (uintmax_t)sc->sense_busaddr, rsize);
1553991554f2SKenneth D. Merry 
155467feec50SStephen McConnell 	/*
155567feec50SStephen McConnell 	 * Allocate NVMe PRP Pages for NVMe SGL support only if the FW supports
155667feec50SStephen McConnell 	 * these devices.
155767feec50SStephen McConnell 	 */
155867feec50SStephen McConnell 	if ((sc->facts->MsgVersion >= MPI2_VERSION_02_06) &&
155967feec50SStephen McConnell 	    (sc->facts->ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES)) {
156067feec50SStephen McConnell 		if (mpr_alloc_nvme_prp_pages(sc) == ENOMEM)
156167feec50SStephen McConnell 			return (ENOMEM);
156267feec50SStephen McConnell 	}
156367feec50SStephen McConnell 
15644f5d6573SAlexander Motin 	nsegs = (sc->maxio / PAGE_SIZE) + 1;
1565f5ead205SScott Long 	bus_dma_template_init(&t, sc->mpr_parent_dmat);
156674c781edSScott Long 	BUS_DMA_TEMPLATE_FILL(&t, BD_MAXSIZE(BUS_SPACE_MAXSIZE_32BIT),
156774c781edSScott Long 	    BD_NSEGMENTS(nsegs), BD_MAXSEGSIZE(BUS_SPACE_MAXSIZE_32BIT),
156874c781edSScott Long 	    BD_FLAGS(BUS_DMA_ALLOCNOW), BD_LOCKFUNC(busdma_lock_mutex),
1569264610a8SKenneth D. Merry 	    BD_LOCKFUNCARG(&sc->mpr_mtx),
1570264610a8SKenneth D. Merry 	    BD_BOUNDARY(BUS_SPACE_MAXSIZE_32BIT+1));
1571f5ead205SScott Long 	if (bus_dma_template_tag(&t, &sc->buffer_dmat)) {
1572757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate buffer DMA tag\n");
1573991554f2SKenneth D. Merry 		return (ENOMEM);
1574991554f2SKenneth D. Merry         }
1575991554f2SKenneth D. Merry 
1576991554f2SKenneth D. Merry 	/*
1577991554f2SKenneth D. Merry 	 * SMID 0 cannot be used as a free command per the firmware spec.
1578991554f2SKenneth D. Merry 	 * Just drop that command instead of risking accounting bugs.
1579991554f2SKenneth D. Merry 	 */
1580991554f2SKenneth D. Merry 	sc->commands = malloc(sizeof(struct mpr_command) * sc->num_reqs,
1581991554f2SKenneth D. Merry 	    M_MPR, M_WAITOK | M_ZERO);
1582991554f2SKenneth D. Merry 	for (i = 1; i < sc->num_reqs; i++) {
1583991554f2SKenneth D. Merry 		cm = &sc->commands[i];
158496410703SScott Long 		cm->cm_req = sc->req_frames + i * sc->reqframesz;
158596410703SScott Long 		cm->cm_req_busaddr = sc->req_busaddr + i * sc->reqframesz;
1586991554f2SKenneth D. Merry 		cm->cm_sense = &sc->sense_frames[i];
1587991554f2SKenneth D. Merry 		cm->cm_sense_busaddr = sc->sense_busaddr + i * MPR_SENSE_LEN;
158871900a79SAlfredo Dal'Ava Junior 		cm->cm_desc.Default.SMID = htole16(i);
1589991554f2SKenneth D. Merry 		cm->cm_sc = sc;
1590f0779b04SScott Long 		cm->cm_state = MPR_CM_STATE_BUSY;
1591991554f2SKenneth D. Merry 		TAILQ_INIT(&cm->cm_chain_list);
159267feec50SStephen McConnell 		TAILQ_INIT(&cm->cm_prp_page_list);
1593991554f2SKenneth D. Merry 		callout_init_mtx(&cm->cm_callout, &sc->mpr_mtx, 0);
1594991554f2SKenneth D. Merry 
1595991554f2SKenneth D. Merry 		/* XXX Is a failure here a critical problem? */
159667feec50SStephen McConnell 		if (bus_dmamap_create(sc->buffer_dmat, 0, &cm->cm_dmamap)
159767feec50SStephen McConnell 		    == 0) {
159862a09ee9SAlexander Motin 			if (i <= sc->num_prireqs)
1599991554f2SKenneth D. Merry 				mpr_free_high_priority_command(sc, cm);
1600991554f2SKenneth D. Merry 			else
1601991554f2SKenneth D. Merry 				mpr_free_command(sc, cm);
160267feec50SStephen McConnell 		} else {
1603991554f2SKenneth D. Merry 			panic("failed to allocate command %d\n", i);
1604991554f2SKenneth D. Merry 			sc->num_reqs = i;
1605991554f2SKenneth D. Merry 			break;
1606991554f2SKenneth D. Merry 		}
1607991554f2SKenneth D. Merry 	}
1608991554f2SKenneth D. Merry 
1609991554f2SKenneth D. Merry 	return (0);
1610991554f2SKenneth D. Merry }
1611991554f2SKenneth D. Merry 
161267feec50SStephen McConnell /*
161367feec50SStephen McConnell  * Allocate contiguous buffers for PCIe NVMe devices for building native PRPs,
161467feec50SStephen McConnell  * which are scatter/gather lists for NVMe devices.
161567feec50SStephen McConnell  *
161667feec50SStephen McConnell  * This buffer must be contiguous due to the nature of how NVMe PRPs are built
161767feec50SStephen McConnell  * and translated by FW.
161867feec50SStephen McConnell  *
161967feec50SStephen McConnell  * returns ENOMEM if memory could not be allocated, otherwise returns 0.
162067feec50SStephen McConnell  */
162167feec50SStephen McConnell static int
mpr_alloc_nvme_prp_pages(struct mpr_softc * sc)162267feec50SStephen McConnell mpr_alloc_nvme_prp_pages(struct mpr_softc *sc)
162367feec50SStephen McConnell {
162474c781edSScott Long 	bus_dma_template_t t;
1625f5ead205SScott Long 	struct mpr_prp_page *prp_page;
162667feec50SStephen McConnell 	int PRPs_per_page, PRPs_required, pages_required;
162767feec50SStephen McConnell 	int rsize, i;
162867feec50SStephen McConnell 
162967feec50SStephen McConnell 	/*
163067feec50SStephen McConnell 	 * Assuming a MAX_IO_SIZE of 1MB and a PAGE_SIZE of 4k, the max number
163167feec50SStephen McConnell 	 * of PRPs (NVMe's Scatter/Gather Element) needed per I/O is:
163267feec50SStephen McConnell 	 * MAX_IO_SIZE / PAGE_SIZE = 256
163367feec50SStephen McConnell 	 *
163467feec50SStephen McConnell 	 * 1 PRP entry in main frame for PRP list pointer still leaves 255 PRPs
163567feec50SStephen McConnell 	 * required for the remainder of the 1MB I/O. 512 PRPs can fit into one
163667feec50SStephen McConnell 	 * page (4096 / 8 = 512), so only one page is required for each I/O.
163767feec50SStephen McConnell 	 *
163867feec50SStephen McConnell 	 * Each of these buffers will need to be contiguous. For simplicity,
163967feec50SStephen McConnell 	 * only one buffer is allocated here, which has all of the space
164067feec50SStephen McConnell 	 * required for the NVMe Queue Depth. If there are problems allocating
164167feec50SStephen McConnell 	 * this one buffer, this function will need to change to allocate
164267feec50SStephen McConnell 	 * individual, contiguous NVME_QDEPTH buffers.
164367feec50SStephen McConnell 	 *
164467feec50SStephen McConnell 	 * The real calculation will use the real max io size. Above is just an
164567feec50SStephen McConnell 	 * example.
164667feec50SStephen McConnell 	 *
164767feec50SStephen McConnell 	 */
164867feec50SStephen McConnell 	PRPs_required = sc->maxio / PAGE_SIZE;
164967feec50SStephen McConnell 	PRPs_per_page = (PAGE_SIZE / PRP_ENTRY_SIZE) - 1;
165067feec50SStephen McConnell 	pages_required = (PRPs_required / PRPs_per_page) + 1;
165167feec50SStephen McConnell 
165267feec50SStephen McConnell 	sc->prp_buffer_size = PAGE_SIZE * pages_required;
165367feec50SStephen McConnell 	rsize = sc->prp_buffer_size * NVME_QDEPTH;
1654f5ead205SScott Long 	bus_dma_template_init(&t, sc->mpr_parent_dmat);
165574c781edSScott Long 	BUS_DMA_TEMPLATE_FILL(&t, BD_ALIGNMENT(4), BD_MAXSIZE(rsize),
165674c781edSScott Long 	    BD_MAXSEGSIZE(rsize), BD_NSEGMENTS(1),
165774c781edSScott Long 	    BD_LOWADDR(BUS_SPACE_MAXADDR_32BIT));
1658f5ead205SScott Long 	if (bus_dma_template_tag(&t, &sc->prp_page_dmat)) {
1659757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate NVMe PRP DMA "
166067feec50SStephen McConnell 		    "tag\n");
166167feec50SStephen McConnell 		return (ENOMEM);
166267feec50SStephen McConnell 	}
166367feec50SStephen McConnell 	if (bus_dmamem_alloc(sc->prp_page_dmat, (void **)&sc->prp_pages,
166467feec50SStephen McConnell 	    BUS_DMA_NOWAIT, &sc->prp_page_map)) {
1665757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate NVMe PRP memory\n");
166667feec50SStephen McConnell 		return (ENOMEM);
166767feec50SStephen McConnell 	}
166867feec50SStephen McConnell 	bzero(sc->prp_pages, rsize);
166967feec50SStephen McConnell 	bus_dmamap_load(sc->prp_page_dmat, sc->prp_page_map, sc->prp_pages,
167067feec50SStephen McConnell 	    rsize, mpr_memaddr_cb, &sc->prp_page_busaddr, 0);
167167feec50SStephen McConnell 
167267feec50SStephen McConnell 	sc->prps = malloc(sizeof(struct mpr_prp_page) * NVME_QDEPTH, M_MPR,
167367feec50SStephen McConnell 	    M_WAITOK | M_ZERO);
167467feec50SStephen McConnell 	for (i = 0; i < NVME_QDEPTH; i++) {
167567feec50SStephen McConnell 		prp_page = &sc->prps[i];
167667feec50SStephen McConnell 		prp_page->prp_page = (uint64_t *)(sc->prp_pages +
167767feec50SStephen McConnell 		    i * sc->prp_buffer_size);
167867feec50SStephen McConnell 		prp_page->prp_page_busaddr = (uint64_t)(sc->prp_page_busaddr +
167967feec50SStephen McConnell 		    i * sc->prp_buffer_size);
168067feec50SStephen McConnell 		mpr_free_prp_page(sc, prp_page);
168167feec50SStephen McConnell 		sc->prp_pages_free_lowwater++;
168267feec50SStephen McConnell 	}
168367feec50SStephen McConnell 
168467feec50SStephen McConnell 	return (0);
168567feec50SStephen McConnell }
168667feec50SStephen McConnell 
1687991554f2SKenneth D. Merry static int
mpr_init_queues(struct mpr_softc * sc)1688991554f2SKenneth D. Merry mpr_init_queues(struct mpr_softc *sc)
1689991554f2SKenneth D. Merry {
1690991554f2SKenneth D. Merry 	int i;
1691991554f2SKenneth D. Merry 
1692991554f2SKenneth D. Merry 	memset((uint8_t *)sc->post_queue, 0xff, sc->pqdepth * 8);
1693991554f2SKenneth D. Merry 
1694991554f2SKenneth D. Merry 	/*
1695991554f2SKenneth D. Merry 	 * According to the spec, we need to use one less reply than we
1696991554f2SKenneth D. Merry 	 * have space for on the queue.  So sc->num_replies (the number we
1697991554f2SKenneth D. Merry 	 * use) should be less than sc->fqdepth (allocated size).
1698991554f2SKenneth D. Merry 	 */
1699991554f2SKenneth D. Merry 	if (sc->num_replies >= sc->fqdepth)
1700991554f2SKenneth D. Merry 		return (EINVAL);
1701991554f2SKenneth D. Merry 
1702991554f2SKenneth D. Merry 	/*
1703991554f2SKenneth D. Merry 	 * Initialize all of the free queue entries.
1704991554f2SKenneth D. Merry 	 */
170567feec50SStephen McConnell 	for (i = 0; i < sc->fqdepth; i++) {
170671900a79SAlfredo Dal'Ava Junior 		sc->free_queue[i] = htole32(sc->reply_busaddr + (i * sc->replyframesz));
170767feec50SStephen McConnell 	}
1708991554f2SKenneth D. Merry 	sc->replyfreeindex = sc->num_replies;
1709991554f2SKenneth D. Merry 
1710991554f2SKenneth D. Merry 	return (0);
1711991554f2SKenneth D. Merry }
1712991554f2SKenneth D. Merry 
1713991554f2SKenneth D. Merry /* Get the driver parameter tunables.  Lowest priority are the driver defaults.
1714991554f2SKenneth D. Merry  * Next are the global settings, if they exist.  Highest are the per-unit
1715991554f2SKenneth D. Merry  * settings, if they exist.
1716991554f2SKenneth D. Merry  */
1717252b2b4fSScott Long void
mpr_get_tunables(struct mpr_softc * sc)1718991554f2SKenneth D. Merry mpr_get_tunables(struct mpr_softc *sc)
1719991554f2SKenneth D. Merry {
1720867aa8cdSScott Long 	char tmpstr[80], mpr_debug[80];
1721991554f2SKenneth D. Merry 
1722991554f2SKenneth D. Merry 	/* XXX default to some debugging for now */
1723991554f2SKenneth D. Merry 	sc->mpr_debug = MPR_INFO | MPR_FAULT;
1724991554f2SKenneth D. Merry 	sc->disable_msix = 0;
1725991554f2SKenneth D. Merry 	sc->disable_msi = 0;
17263c5ac992SScott Long 	sc->max_msix = MPR_MSIX_MAX;
1727991554f2SKenneth D. Merry 	sc->max_chains = MPR_CHAIN_FRAMES;
172832b0a21eSStephen McConnell 	sc->max_io_pages = MPR_MAXIO_PAGES;
1729a2c14879SStephen McConnell 	sc->enable_ssu = MPR_SSU_ENABLE_SSD_DISABLE_HDD;
1730a2c14879SStephen McConnell 	sc->spinup_wait_time = DEFAULT_SPINUP_WAIT;
17314ab1cdc5SScott Long 	sc->use_phynum = 1;
17323c5ac992SScott Long 	sc->max_reqframes = MPR_REQ_FRAMES;
17333c5ac992SScott Long 	sc->max_prireqframes = MPR_PRI_REQ_FRAMES;
17343c5ac992SScott Long 	sc->max_replyframes = MPR_REPLY_FRAMES;
17353c5ac992SScott Long 	sc->max_evtframes = MPR_EVT_REPLY_FRAMES;
1736991554f2SKenneth D. Merry 
1737991554f2SKenneth D. Merry 	/*
1738991554f2SKenneth D. Merry 	 * Grab the global variables.
1739991554f2SKenneth D. Merry 	 */
1740867aa8cdSScott Long 	bzero(mpr_debug, 80);
1741867aa8cdSScott Long 	if (TUNABLE_STR_FETCH("hw.mpr.debug_level", mpr_debug, 80) != 0)
1742867aa8cdSScott Long 		mpr_parse_debug(sc, mpr_debug);
1743991554f2SKenneth D. Merry 	TUNABLE_INT_FETCH("hw.mpr.disable_msix", &sc->disable_msix);
1744991554f2SKenneth D. Merry 	TUNABLE_INT_FETCH("hw.mpr.disable_msi", &sc->disable_msi);
17453c5ac992SScott Long 	TUNABLE_INT_FETCH("hw.mpr.max_msix", &sc->max_msix);
1746991554f2SKenneth D. Merry 	TUNABLE_INT_FETCH("hw.mpr.max_chains", &sc->max_chains);
174732b0a21eSStephen McConnell 	TUNABLE_INT_FETCH("hw.mpr.max_io_pages", &sc->max_io_pages);
1748a2c14879SStephen McConnell 	TUNABLE_INT_FETCH("hw.mpr.enable_ssu", &sc->enable_ssu);
1749a2c14879SStephen McConnell 	TUNABLE_INT_FETCH("hw.mpr.spinup_wait_time", &sc->spinup_wait_time);
17504ab1cdc5SScott Long 	TUNABLE_INT_FETCH("hw.mpr.use_phy_num", &sc->use_phynum);
17513c5ac992SScott Long 	TUNABLE_INT_FETCH("hw.mpr.max_reqframes", &sc->max_reqframes);
17523c5ac992SScott Long 	TUNABLE_INT_FETCH("hw.mpr.max_prireqframes", &sc->max_prireqframes);
17533c5ac992SScott Long 	TUNABLE_INT_FETCH("hw.mpr.max_replyframes", &sc->max_replyframes);
17543c5ac992SScott Long 	TUNABLE_INT_FETCH("hw.mpr.max_evtframes", &sc->max_evtframes);
1755991554f2SKenneth D. Merry 
1756991554f2SKenneth D. Merry 	/* Grab the unit-instance variables */
1757991554f2SKenneth D. Merry 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.debug_level",
1758991554f2SKenneth D. Merry 	    device_get_unit(sc->mpr_dev));
1759867aa8cdSScott Long 	bzero(mpr_debug, 80);
1760867aa8cdSScott Long 	if (TUNABLE_STR_FETCH(tmpstr, mpr_debug, 80) != 0)
1761867aa8cdSScott Long 		mpr_parse_debug(sc, mpr_debug);
1762991554f2SKenneth D. Merry 
1763991554f2SKenneth D. Merry 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.disable_msix",
1764991554f2SKenneth D. Merry 	    device_get_unit(sc->mpr_dev));
1765991554f2SKenneth D. Merry 	TUNABLE_INT_FETCH(tmpstr, &sc->disable_msix);
1766991554f2SKenneth D. Merry 
1767991554f2SKenneth D. Merry 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.disable_msi",
1768991554f2SKenneth D. Merry 	    device_get_unit(sc->mpr_dev));
1769991554f2SKenneth D. Merry 	TUNABLE_INT_FETCH(tmpstr, &sc->disable_msi);
1770991554f2SKenneth D. Merry 
17713c5ac992SScott Long 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.max_msix",
17723c5ac992SScott Long 	    device_get_unit(sc->mpr_dev));
17733c5ac992SScott Long 	TUNABLE_INT_FETCH(tmpstr, &sc->max_msix);
17743c5ac992SScott Long 
1775991554f2SKenneth D. Merry 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.max_chains",
1776991554f2SKenneth D. Merry 	    device_get_unit(sc->mpr_dev));
1777991554f2SKenneth D. Merry 	TUNABLE_INT_FETCH(tmpstr, &sc->max_chains);
1778991554f2SKenneth D. Merry 
177932b0a21eSStephen McConnell 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.max_io_pages",
178032b0a21eSStephen McConnell 	    device_get_unit(sc->mpr_dev));
178132b0a21eSStephen McConnell 	TUNABLE_INT_FETCH(tmpstr, &sc->max_io_pages);
178232b0a21eSStephen McConnell 
1783991554f2SKenneth D. Merry 	bzero(sc->exclude_ids, sizeof(sc->exclude_ids));
1784991554f2SKenneth D. Merry 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.exclude_ids",
1785991554f2SKenneth D. Merry 	    device_get_unit(sc->mpr_dev));
1786991554f2SKenneth D. Merry 	TUNABLE_STR_FETCH(tmpstr, sc->exclude_ids, sizeof(sc->exclude_ids));
1787a2c14879SStephen McConnell 
1788a2c14879SStephen McConnell 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.enable_ssu",
1789a2c14879SStephen McConnell 	    device_get_unit(sc->mpr_dev));
1790a2c14879SStephen McConnell 	TUNABLE_INT_FETCH(tmpstr, &sc->enable_ssu);
1791a2c14879SStephen McConnell 
1792a2c14879SStephen McConnell 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.spinup_wait_time",
1793a2c14879SStephen McConnell 	    device_get_unit(sc->mpr_dev));
1794a2c14879SStephen McConnell 	TUNABLE_INT_FETCH(tmpstr, &sc->spinup_wait_time);
17954ab1cdc5SScott Long 
17964ab1cdc5SScott Long 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.use_phy_num",
17974ab1cdc5SScott Long 	    device_get_unit(sc->mpr_dev));
17984ab1cdc5SScott Long 	TUNABLE_INT_FETCH(tmpstr, &sc->use_phynum);
17993c5ac992SScott Long 
18003c5ac992SScott Long 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.max_reqframes",
18013c5ac992SScott Long 	    device_get_unit(sc->mpr_dev));
18023c5ac992SScott Long 	TUNABLE_INT_FETCH(tmpstr, &sc->max_reqframes);
18033c5ac992SScott Long 
18043c5ac992SScott Long 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.max_prireqframes",
18053c5ac992SScott Long 	    device_get_unit(sc->mpr_dev));
18063c5ac992SScott Long 	TUNABLE_INT_FETCH(tmpstr, &sc->max_prireqframes);
18073c5ac992SScott Long 
18083c5ac992SScott Long 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.max_replyframes",
18093c5ac992SScott Long 	    device_get_unit(sc->mpr_dev));
18103c5ac992SScott Long 	TUNABLE_INT_FETCH(tmpstr, &sc->max_replyframes);
18113c5ac992SScott Long 
18123c5ac992SScott Long 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.max_evtframes",
18133c5ac992SScott Long 	    device_get_unit(sc->mpr_dev));
18143c5ac992SScott Long 	TUNABLE_INT_FETCH(tmpstr, &sc->max_evtframes);
1815991554f2SKenneth D. Merry }
1816991554f2SKenneth D. Merry 
1817991554f2SKenneth D. Merry static void
mpr_setup_sysctl(struct mpr_softc * sc)1818991554f2SKenneth D. Merry mpr_setup_sysctl(struct mpr_softc *sc)
1819991554f2SKenneth D. Merry {
1820991554f2SKenneth D. Merry 	struct sysctl_ctx_list	*sysctl_ctx = NULL;
1821991554f2SKenneth D. Merry 	struct sysctl_oid	*sysctl_tree = NULL;
1822991554f2SKenneth D. Merry 	char tmpstr[80], tmpstr2[80];
1823991554f2SKenneth D. Merry 
1824991554f2SKenneth D. Merry 	/*
1825991554f2SKenneth D. Merry 	 * Setup the sysctl variable so the user can change the debug level
1826991554f2SKenneth D. Merry 	 * on the fly.
1827991554f2SKenneth D. Merry 	 */
1828991554f2SKenneth D. Merry 	snprintf(tmpstr, sizeof(tmpstr), "MPR controller %d",
1829991554f2SKenneth D. Merry 	    device_get_unit(sc->mpr_dev));
1830991554f2SKenneth D. Merry 	snprintf(tmpstr2, sizeof(tmpstr2), "%d", device_get_unit(sc->mpr_dev));
1831991554f2SKenneth D. Merry 
1832991554f2SKenneth D. Merry 	sysctl_ctx = device_get_sysctl_ctx(sc->mpr_dev);
1833991554f2SKenneth D. Merry 	if (sysctl_ctx != NULL)
1834991554f2SKenneth D. Merry 		sysctl_tree = device_get_sysctl_tree(sc->mpr_dev);
1835991554f2SKenneth D. Merry 
1836991554f2SKenneth D. Merry 	if (sysctl_tree == NULL) {
1837991554f2SKenneth D. Merry 		sysctl_ctx_init(&sc->sysctl_ctx);
1838991554f2SKenneth D. Merry 		sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
1839991554f2SKenneth D. Merry 		    SYSCTL_STATIC_CHILDREN(_hw_mpr), OID_AUTO, tmpstr2,
18407029da5cSPawel Biernacki 		    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, tmpstr);
1841991554f2SKenneth D. Merry 		if (sc->sysctl_tree == NULL)
1842991554f2SKenneth D. Merry 			return;
1843991554f2SKenneth D. Merry 		sysctl_ctx = &sc->sysctl_ctx;
1844991554f2SKenneth D. Merry 		sysctl_tree = sc->sysctl_tree;
1845991554f2SKenneth D. Merry 	}
1846991554f2SKenneth D. Merry 
1847867aa8cdSScott Long 	SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1848cb242d7cSScott Long 	    OID_AUTO, "debug_level", CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1849cb242d7cSScott Long 	    sc, 0, mpr_debug_sysctl, "A", "mpr debug level");
1850991554f2SKenneth D. Merry 
1851991554f2SKenneth D. Merry 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1852991554f2SKenneth D. Merry 	    OID_AUTO, "disable_msix", CTLFLAG_RD, &sc->disable_msix, 0,
1853991554f2SKenneth D. Merry 	    "Disable the use of MSI-X interrupts");
1854991554f2SKenneth D. Merry 
1855991554f2SKenneth D. Merry 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
18563c5ac992SScott Long 	    OID_AUTO, "max_msix", CTLFLAG_RD, &sc->max_msix, 0,
18573c5ac992SScott Long 	    "User-defined maximum number of MSIX queues");
18583c5ac992SScott Long 
18593c5ac992SScott Long 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
18603c5ac992SScott Long 	    OID_AUTO, "msix_msgs", CTLFLAG_RD, &sc->msi_msgs, 0,
18613c5ac992SScott Long 	    "Negotiated number of MSIX queues");
18623c5ac992SScott Long 
18633c5ac992SScott Long 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
18643c5ac992SScott Long 	    OID_AUTO, "max_reqframes", CTLFLAG_RD, &sc->max_reqframes, 0,
18653c5ac992SScott Long 	    "Total number of allocated request frames");
18663c5ac992SScott Long 
18673c5ac992SScott Long 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
18683c5ac992SScott Long 	    OID_AUTO, "max_prireqframes", CTLFLAG_RD, &sc->max_prireqframes, 0,
18693c5ac992SScott Long 	    "Total number of allocated high priority request frames");
18703c5ac992SScott Long 
18713c5ac992SScott Long 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
18723c5ac992SScott Long 	    OID_AUTO, "max_replyframes", CTLFLAG_RD, &sc->max_replyframes, 0,
18733c5ac992SScott Long 	    "Total number of allocated reply frames");
18743c5ac992SScott Long 
18753c5ac992SScott Long 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
18763c5ac992SScott Long 	    OID_AUTO, "max_evtframes", CTLFLAG_RD, &sc->max_evtframes, 0,
18773c5ac992SScott Long 	    "Total number of event frames allocated");
1878991554f2SKenneth D. Merry 
1879991554f2SKenneth D. Merry 	SYSCTL_ADD_STRING(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
188069e85eb8SScott Long 	    OID_AUTO, "firmware_version", CTLFLAG_RD, sc->fw_version,
1881991554f2SKenneth D. Merry 	    strlen(sc->fw_version), "firmware version");
1882991554f2SKenneth D. Merry 
1883991554f2SKenneth D. Merry 	SYSCTL_ADD_STRING(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
188469e85eb8SScott Long 	    OID_AUTO, "driver_version", CTLFLAG_RD, MPR_DRIVER_VERSION,
1885991554f2SKenneth D. Merry 	    strlen(MPR_DRIVER_VERSION), "driver version");
1886991554f2SKenneth D. Merry 
188769e85eb8SScott Long 	SYSCTL_ADD_STRING(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
188869e85eb8SScott Long 	    OID_AUTO, "msg_version", CTLFLAG_RD, sc->msg_version,
18897d154c4dSAlan Somers 	    strlen(sc->msg_version), "message interface version (deprecated)");
189069e85eb8SScott Long 
1891991554f2SKenneth D. Merry 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1892991554f2SKenneth D. Merry 	    OID_AUTO, "io_cmds_active", CTLFLAG_RD,
1893991554f2SKenneth D. Merry 	    &sc->io_cmds_active, 0, "number of currently active commands");
1894991554f2SKenneth D. Merry 
1895991554f2SKenneth D. Merry 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1896991554f2SKenneth D. Merry 	    OID_AUTO, "io_cmds_highwater", CTLFLAG_RD,
1897991554f2SKenneth D. Merry 	    &sc->io_cmds_highwater, 0, "maximum active commands seen");
1898991554f2SKenneth D. Merry 
1899991554f2SKenneth D. Merry 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1900991554f2SKenneth D. Merry 	    OID_AUTO, "chain_free", CTLFLAG_RD,
1901991554f2SKenneth D. Merry 	    &sc->chain_free, 0, "number of free chain elements");
1902991554f2SKenneth D. Merry 
1903991554f2SKenneth D. Merry 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1904991554f2SKenneth D. Merry 	    OID_AUTO, "chain_free_lowwater", CTLFLAG_RD,
1905991554f2SKenneth D. Merry 	    &sc->chain_free_lowwater, 0,"lowest number of free chain elements");
1906991554f2SKenneth D. Merry 
1907991554f2SKenneth D. Merry 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1908991554f2SKenneth D. Merry 	    OID_AUTO, "max_chains", CTLFLAG_RD,
1909991554f2SKenneth D. Merry 	    &sc->max_chains, 0,"maximum chain frames that will be allocated");
1910991554f2SKenneth D. Merry 
1911a2c14879SStephen McConnell 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
191232b0a21eSStephen McConnell 	    OID_AUTO, "max_io_pages", CTLFLAG_RD,
191332b0a21eSStephen McConnell 	    &sc->max_io_pages, 0,"maximum pages to allow per I/O (if <1 use "
191432b0a21eSStephen McConnell 	    "IOCFacts)");
191532b0a21eSStephen McConnell 
191632b0a21eSStephen McConnell 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1917a2c14879SStephen McConnell 	    OID_AUTO, "enable_ssu", CTLFLAG_RW, &sc->enable_ssu, 0,
1918a2c14879SStephen McConnell 	    "enable SSU to SATA SSD/HDD at shutdown");
1919a2c14879SStephen McConnell 
1920991554f2SKenneth D. Merry 	SYSCTL_ADD_UQUAD(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1921991554f2SKenneth D. Merry 	    OID_AUTO, "chain_alloc_fail", CTLFLAG_RD,
1922991554f2SKenneth D. Merry 	    &sc->chain_alloc_fail, "chain allocation failures");
1923a2c14879SStephen McConnell 
1924a2c14879SStephen McConnell 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1925a2c14879SStephen McConnell 	    OID_AUTO, "spinup_wait_time", CTLFLAG_RD,
1926a2c14879SStephen McConnell 	    &sc->spinup_wait_time, DEFAULT_SPINUP_WAIT, "seconds to wait for "
1927a2c14879SStephen McConnell 	    "spinup after SATA ID error");
19284ab1cdc5SScott Long 
1929cf6ea6f2SScott Long 	SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
19307029da5cSPawel Biernacki 	    OID_AUTO, "dump_reqs",
1931b776de67SAlexander Motin 	    CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE,
19327029da5cSPawel Biernacki 	    sc, 0, mpr_dump_reqs, "I", "Dump Active Requests");
1933cf6ea6f2SScott Long 
19344ab1cdc5SScott Long 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1935175ad3d0SKenneth D. Merry 	    OID_AUTO, "dump_reqs_alltypes", CTLFLAG_RW,
1936175ad3d0SKenneth D. Merry 	    &sc->dump_reqs_alltypes, 0,
1937175ad3d0SKenneth D. Merry 	    "dump all request types not just inqueue");
1938175ad3d0SKenneth D. Merry 
1939175ad3d0SKenneth D. Merry 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
19404ab1cdc5SScott Long 	    OID_AUTO, "use_phy_num", CTLFLAG_RD, &sc->use_phynum, 0,
19414ab1cdc5SScott Long 	    "Use the phy number for enumeration");
194267feec50SStephen McConnell 
194367feec50SStephen McConnell 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
194467feec50SStephen McConnell 	    OID_AUTO, "prp_pages_free", CTLFLAG_RD,
194567feec50SStephen McConnell 	    &sc->prp_pages_free, 0, "number of free PRP pages");
194667feec50SStephen McConnell 
194767feec50SStephen McConnell 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
194867feec50SStephen McConnell 	    OID_AUTO, "prp_pages_free_lowwater", CTLFLAG_RD,
194967feec50SStephen McConnell 	    &sc->prp_pages_free_lowwater, 0,"lowest number of free PRP pages");
195067feec50SStephen McConnell 
195167feec50SStephen McConnell 	SYSCTL_ADD_UQUAD(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
195267feec50SStephen McConnell 	    OID_AUTO, "prp_page_alloc_fail", CTLFLAG_RD,
195367feec50SStephen McConnell 	    &sc->prp_page_alloc_fail, "PRP page allocation failures");
1954991554f2SKenneth D. Merry }
1955991554f2SKenneth D. Merry 
1956867aa8cdSScott Long static struct mpr_debug_string {
1957867aa8cdSScott Long 	char *name;
1958867aa8cdSScott Long 	int flag;
1959867aa8cdSScott Long } mpr_debug_strings[] = {
1960867aa8cdSScott Long 	{"info", MPR_INFO},
1961867aa8cdSScott Long 	{"fault", MPR_FAULT},
1962867aa8cdSScott Long 	{"event", MPR_EVENT},
1963867aa8cdSScott Long 	{"log", MPR_LOG},
1964867aa8cdSScott Long 	{"recovery", MPR_RECOVERY},
1965867aa8cdSScott Long 	{"error", MPR_ERROR},
1966867aa8cdSScott Long 	{"init", MPR_INIT},
1967867aa8cdSScott Long 	{"xinfo", MPR_XINFO},
1968867aa8cdSScott Long 	{"user", MPR_USER},
1969867aa8cdSScott Long 	{"mapping", MPR_MAPPING},
1970867aa8cdSScott Long 	{"trace", MPR_TRACE}
1971867aa8cdSScott Long };
1972867aa8cdSScott Long 
1973cfd6fd5aSScott Long enum mpr_debug_level_combiner {
1974cfd6fd5aSScott Long 	COMB_NONE,
1975cfd6fd5aSScott Long 	COMB_ADD,
1976cfd6fd5aSScott Long 	COMB_SUB
1977cfd6fd5aSScott Long };
1978cfd6fd5aSScott Long 
1979867aa8cdSScott Long static int
mpr_debug_sysctl(SYSCTL_HANDLER_ARGS)1980867aa8cdSScott Long mpr_debug_sysctl(SYSCTL_HANDLER_ARGS)
1981867aa8cdSScott Long {
1982867aa8cdSScott Long 	struct mpr_softc *sc;
1983867aa8cdSScott Long 	struct mpr_debug_string *string;
1984cb242d7cSScott Long 	struct sbuf *sbuf;
1985867aa8cdSScott Long 	char *buffer;
1986867aa8cdSScott Long 	size_t sz;
1987867aa8cdSScott Long 	int i, len, debug, error;
1988867aa8cdSScott Long 
1989867aa8cdSScott Long 	sc = (struct mpr_softc *)arg1;
1990867aa8cdSScott Long 
1991867aa8cdSScott Long 	error = sysctl_wire_old_buffer(req, 0);
1992867aa8cdSScott Long 	if (error != 0)
1993867aa8cdSScott Long 		return (error);
1994867aa8cdSScott Long 
1995cb242d7cSScott Long 	sbuf = sbuf_new_for_sysctl(NULL, NULL, 128, req);
1996867aa8cdSScott Long 	debug = sc->mpr_debug;
1997867aa8cdSScott Long 
1998cb242d7cSScott Long 	sbuf_printf(sbuf, "%#x", debug);
1999867aa8cdSScott Long 
2000867aa8cdSScott Long 	sz = sizeof(mpr_debug_strings) / sizeof(mpr_debug_strings[0]);
2001867aa8cdSScott Long 	for (i = 0; i < sz; i++) {
2002867aa8cdSScott Long 		string = &mpr_debug_strings[i];
2003867aa8cdSScott Long 		if (debug & string->flag)
2004cb242d7cSScott Long 			sbuf_printf(sbuf, ",%s", string->name);
2005867aa8cdSScott Long 	}
2006867aa8cdSScott Long 
2007cb242d7cSScott Long 	error = sbuf_finish(sbuf);
2008cb242d7cSScott Long 	sbuf_delete(sbuf);
2009867aa8cdSScott Long 
2010867aa8cdSScott Long 	if (error || req->newptr == NULL)
2011867aa8cdSScott Long 		return (error);
2012867aa8cdSScott Long 
2013867aa8cdSScott Long 	len = req->newlen - req->newidx;
2014867aa8cdSScott Long 	if (len == 0)
2015867aa8cdSScott Long 		return (0);
2016867aa8cdSScott Long 
2017867aa8cdSScott Long 	buffer = malloc(len, M_MPR, M_ZERO|M_WAITOK);
2018867aa8cdSScott Long 	error = SYSCTL_IN(req, buffer, len);
2019867aa8cdSScott Long 
2020867aa8cdSScott Long 	mpr_parse_debug(sc, buffer);
2021867aa8cdSScott Long 
2022867aa8cdSScott Long 	free(buffer, M_MPR);
2023867aa8cdSScott Long 	return (error);
2024867aa8cdSScott Long }
2025867aa8cdSScott Long 
2026867aa8cdSScott Long static void
mpr_parse_debug(struct mpr_softc * sc,char * list)2027867aa8cdSScott Long mpr_parse_debug(struct mpr_softc *sc, char *list)
2028867aa8cdSScott Long {
2029867aa8cdSScott Long 	struct mpr_debug_string *string;
2030cfd6fd5aSScott Long 	enum mpr_debug_level_combiner op;
2031867aa8cdSScott Long 	char *token, *endtoken;
2032867aa8cdSScott Long 	size_t sz;
2033867aa8cdSScott Long 	int flags, i;
2034867aa8cdSScott Long 
2035867aa8cdSScott Long 	if (list == NULL || *list == '\0')
2036867aa8cdSScott Long 		return;
2037867aa8cdSScott Long 
2038cfd6fd5aSScott Long 	if (*list == '+') {
2039cfd6fd5aSScott Long 		op = COMB_ADD;
2040cfd6fd5aSScott Long 		list++;
2041cfd6fd5aSScott Long 	} else if (*list == '-') {
2042cfd6fd5aSScott Long 		op = COMB_SUB;
2043cfd6fd5aSScott Long 		list++;
2044cfd6fd5aSScott Long 	} else
2045cfd6fd5aSScott Long 		op = COMB_NONE;
2046cfd6fd5aSScott Long 	if (*list == '\0')
2047cfd6fd5aSScott Long 		return;
2048cfd6fd5aSScott Long 
2049867aa8cdSScott Long 	flags = 0;
2050867aa8cdSScott Long 	sz = sizeof(mpr_debug_strings) / sizeof(mpr_debug_strings[0]);
2051867aa8cdSScott Long 	while ((token = strsep(&list, ":,")) != NULL) {
2052867aa8cdSScott Long 		/* Handle integer flags */
2053867aa8cdSScott Long 		flags |= strtol(token, &endtoken, 0);
2054867aa8cdSScott Long 		if (token != endtoken)
2055867aa8cdSScott Long 			continue;
2056867aa8cdSScott Long 
2057867aa8cdSScott Long 		/* Handle text flags */
2058867aa8cdSScott Long 		for (i = 0; i < sz; i++) {
2059867aa8cdSScott Long 			string = &mpr_debug_strings[i];
2060867aa8cdSScott Long 			if (strcasecmp(token, string->name) == 0) {
2061867aa8cdSScott Long 				flags |= string->flag;
2062867aa8cdSScott Long 				break;
2063867aa8cdSScott Long 			}
2064867aa8cdSScott Long 		}
2065867aa8cdSScott Long 	}
2066867aa8cdSScott Long 
2067cfd6fd5aSScott Long 	switch (op) {
2068cfd6fd5aSScott Long 	case COMB_NONE:
2069867aa8cdSScott Long 		sc->mpr_debug = flags;
2070cfd6fd5aSScott Long 		break;
2071cfd6fd5aSScott Long 	case COMB_ADD:
2072cfd6fd5aSScott Long 		sc->mpr_debug |= flags;
2073cfd6fd5aSScott Long 		break;
2074cfd6fd5aSScott Long 	case COMB_SUB:
2075cfd6fd5aSScott Long 		sc->mpr_debug &= (~flags);
2076cfd6fd5aSScott Long 		break;
2077cfd6fd5aSScott Long 	}
2078867aa8cdSScott Long 	return;
2079867aa8cdSScott Long }
2080867aa8cdSScott Long 
2081cf6ea6f2SScott Long struct mpr_dumpreq_hdr {
2082cf6ea6f2SScott Long 	uint32_t	smid;
2083cf6ea6f2SScott Long 	uint32_t	state;
2084cf6ea6f2SScott Long 	uint32_t	numframes;
2085cf6ea6f2SScott Long 	uint32_t	deschi;
2086cf6ea6f2SScott Long 	uint32_t	desclo;
2087cf6ea6f2SScott Long };
2088cf6ea6f2SScott Long 
2089cf6ea6f2SScott Long static int
mpr_dump_reqs(SYSCTL_HANDLER_ARGS)2090cf6ea6f2SScott Long mpr_dump_reqs(SYSCTL_HANDLER_ARGS)
2091cf6ea6f2SScott Long {
2092cf6ea6f2SScott Long 	struct mpr_softc *sc;
2093cf6ea6f2SScott Long 	struct mpr_chain *chain, *chain1;
2094cf6ea6f2SScott Long 	struct mpr_command *cm;
2095cf6ea6f2SScott Long 	struct mpr_dumpreq_hdr hdr;
2096cf6ea6f2SScott Long 	struct sbuf *sb;
2097cf6ea6f2SScott Long 	uint32_t smid, state;
2098cf6ea6f2SScott Long 	int i, numreqs, error = 0;
2099cf6ea6f2SScott Long 
2100cf6ea6f2SScott Long 	sc = (struct mpr_softc *)arg1;
2101cf6ea6f2SScott Long 
2102cf6ea6f2SScott Long 	if ((error = priv_check(curthread, PRIV_DRIVER)) != 0) {
2103cf6ea6f2SScott Long 		printf("priv check error %d\n", error);
2104cf6ea6f2SScott Long 		return (error);
2105cf6ea6f2SScott Long 	}
2106cf6ea6f2SScott Long 
2107cf6ea6f2SScott Long 	state = MPR_CM_STATE_INQUEUE;
2108cf6ea6f2SScott Long 	smid = 1;
2109cf6ea6f2SScott Long 	numreqs = sc->num_reqs;
2110cf6ea6f2SScott Long 
2111cf6ea6f2SScott Long 	if (req->newptr != NULL)
2112cf6ea6f2SScott Long 		return (EINVAL);
2113cf6ea6f2SScott Long 
2114cf6ea6f2SScott Long 	if (smid == 0 || smid > sc->num_reqs)
2115cf6ea6f2SScott Long 		return (EINVAL);
2116cf6ea6f2SScott Long 	if (numreqs <= 0 || (numreqs + smid > sc->num_reqs))
2117cf6ea6f2SScott Long 		numreqs = sc->num_reqs;
2118cf6ea6f2SScott Long 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
2119cf6ea6f2SScott Long 
2120cf6ea6f2SScott Long 	/* Best effort, no locking */
2121cf6ea6f2SScott Long 	for (i = smid; i < numreqs; i++) {
2122cf6ea6f2SScott Long 		cm = &sc->commands[i];
2123175ad3d0SKenneth D. Merry 		if ((sc->dump_reqs_alltypes == 0) && (cm->cm_state != state))
2124cf6ea6f2SScott Long 			continue;
2125cf6ea6f2SScott Long 		hdr.smid = i;
2126cf6ea6f2SScott Long 		hdr.state = cm->cm_state;
2127cf6ea6f2SScott Long 		hdr.numframes = 1;
2128cf6ea6f2SScott Long 		hdr.deschi = cm->cm_desc.Words.High;
2129cf6ea6f2SScott Long 		hdr.desclo = cm->cm_desc.Words.Low;
2130cf6ea6f2SScott Long 		TAILQ_FOREACH_SAFE(chain, &cm->cm_chain_list, chain_link,
2131cf6ea6f2SScott Long 		   chain1)
2132cf6ea6f2SScott Long 			hdr.numframes++;
2133cf6ea6f2SScott Long 		sbuf_bcat(sb, &hdr, sizeof(hdr));
2134cf6ea6f2SScott Long 		sbuf_bcat(sb, cm->cm_req, 128);
2135cf6ea6f2SScott Long 		TAILQ_FOREACH_SAFE(chain, &cm->cm_chain_list, chain_link,
2136cf6ea6f2SScott Long 		    chain1)
2137cf6ea6f2SScott Long 			sbuf_bcat(sb, chain->chain, 128);
2138cf6ea6f2SScott Long 	}
2139cf6ea6f2SScott Long 
2140cf6ea6f2SScott Long 	error = sbuf_finish(sb);
2141cf6ea6f2SScott Long 	sbuf_delete(sb);
2142cf6ea6f2SScott Long 	return (error);
2143cf6ea6f2SScott Long }
2144cf6ea6f2SScott Long 
2145991554f2SKenneth D. Merry int
mpr_attach(struct mpr_softc * sc)2146991554f2SKenneth D. Merry mpr_attach(struct mpr_softc *sc)
2147991554f2SKenneth D. Merry {
2148991554f2SKenneth D. Merry 	int error;
2149991554f2SKenneth D. Merry 
2150991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
2151757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
2152991554f2SKenneth D. Merry 
2153991554f2SKenneth D. Merry 	mtx_init(&sc->mpr_mtx, "MPR lock", NULL, MTX_DEF);
2154991554f2SKenneth D. Merry 	callout_init_mtx(&sc->periodic, &sc->mpr_mtx, 0);
2155327f2e6cSStephen McConnell 	callout_init_mtx(&sc->device_check_callout, &sc->mpr_mtx, 0);
2156991554f2SKenneth D. Merry 	TAILQ_INIT(&sc->event_list);
2157991554f2SKenneth D. Merry 	timevalclear(&sc->lastfail);
2158991554f2SKenneth D. Merry 
2159991554f2SKenneth D. Merry 	if ((error = mpr_transition_ready(sc)) != 0) {
2160757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_FAULT,
2161757ff642SScott Long 		    "Failed to transition ready\n");
2162991554f2SKenneth D. Merry 		return (error);
2163991554f2SKenneth D. Merry 	}
2164991554f2SKenneth D. Merry 
2165991554f2SKenneth D. Merry 	sc->facts = malloc(sizeof(MPI2_IOC_FACTS_REPLY), M_MPR,
2166991554f2SKenneth D. Merry 	    M_ZERO|M_NOWAIT);
2167991554f2SKenneth D. Merry 	if (!sc->facts) {
2168757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_FAULT,
2169757ff642SScott Long 		    "Cannot allocate memory, exit\n");
2170991554f2SKenneth D. Merry 		return (ENOMEM);
2171991554f2SKenneth D. Merry 	}
2172991554f2SKenneth D. Merry 
2173991554f2SKenneth D. Merry 	/*
2174991554f2SKenneth D. Merry 	 * Get IOC Facts and allocate all structures based on this information.
2175991554f2SKenneth D. Merry 	 * A Diag Reset will also call mpr_iocfacts_allocate and re-read the IOC
2176991554f2SKenneth D. Merry 	 * Facts. If relevant values have changed in IOC Facts, this function
2177991554f2SKenneth D. Merry 	 * will free all of the memory based on IOC Facts and reallocate that
2178991554f2SKenneth D. Merry 	 * memory.  If this fails, any allocated memory should already be freed.
2179991554f2SKenneth D. Merry 	 */
2180991554f2SKenneth D. Merry 	if ((error = mpr_iocfacts_allocate(sc, TRUE)) != 0) {
2181757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_FAULT, "IOC Facts allocation "
2182757ff642SScott Long 		    "failed with error %d\n", error);
2183991554f2SKenneth D. Merry 		return (error);
2184991554f2SKenneth D. Merry 	}
2185991554f2SKenneth D. Merry 
2186991554f2SKenneth D. Merry 	/* Start the periodic watchdog check on the IOC Doorbell */
2187991554f2SKenneth D. Merry 	mpr_periodic(sc);
2188991554f2SKenneth D. Merry 
2189991554f2SKenneth D. Merry 	/*
2190991554f2SKenneth D. Merry 	 * The portenable will kick off discovery events that will drive the
2191991554f2SKenneth D. Merry 	 * rest of the initialization process.  The CAM/SAS module will
2192991554f2SKenneth D. Merry 	 * hold up the boot sequence until discovery is complete.
2193991554f2SKenneth D. Merry 	 */
2194991554f2SKenneth D. Merry 	sc->mpr_ich.ich_func = mpr_startup;
2195991554f2SKenneth D. Merry 	sc->mpr_ich.ich_arg = sc;
2196991554f2SKenneth D. Merry 	if (config_intrhook_establish(&sc->mpr_ich) != 0) {
2197757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR,
2198757ff642SScott Long 		    "Cannot establish MPR config hook\n");
2199991554f2SKenneth D. Merry 		error = EINVAL;
2200991554f2SKenneth D. Merry 	}
2201991554f2SKenneth D. Merry 
2202991554f2SKenneth D. Merry 	/*
2203991554f2SKenneth D. Merry 	 * Allow IR to shutdown gracefully when shutdown occurs.
2204991554f2SKenneth D. Merry 	 */
2205991554f2SKenneth D. Merry 	sc->shutdown_eh = EVENTHANDLER_REGISTER(shutdown_final,
2206991554f2SKenneth D. Merry 	    mprsas_ir_shutdown, sc, SHUTDOWN_PRI_DEFAULT);
2207991554f2SKenneth D. Merry 
2208991554f2SKenneth D. Merry 	if (sc->shutdown_eh == NULL)
2209757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR,
2210757ff642SScott Long 		    "shutdown event registration failed\n");
2211991554f2SKenneth D. Merry 
2212991554f2SKenneth D. Merry 	mpr_setup_sysctl(sc);
2213991554f2SKenneth D. Merry 
2214991554f2SKenneth D. Merry 	sc->mpr_flags |= MPR_FLAGS_ATTACH_DONE;
2215757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit error= %d\n", __func__, error);
2216991554f2SKenneth D. Merry 
2217991554f2SKenneth D. Merry 	return (error);
2218991554f2SKenneth D. Merry }
2219991554f2SKenneth D. Merry 
2220991554f2SKenneth D. Merry /* Run through any late-start handlers. */
2221991554f2SKenneth D. Merry static void
mpr_startup(void * arg)2222991554f2SKenneth D. Merry mpr_startup(void *arg)
2223991554f2SKenneth D. Merry {
2224991554f2SKenneth D. Merry 	struct mpr_softc *sc;
2225991554f2SKenneth D. Merry 
2226991554f2SKenneth D. Merry 	sc = (struct mpr_softc *)arg;
2227757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
2228991554f2SKenneth D. Merry 
2229991554f2SKenneth D. Merry 	mpr_lock(sc);
2230991554f2SKenneth D. Merry 	mpr_unmask_intr(sc);
2231991554f2SKenneth D. Merry 
2232991554f2SKenneth D. Merry 	/* initialize device mapping tables */
2233991554f2SKenneth D. Merry 	mpr_base_static_config_pages(sc);
2234991554f2SKenneth D. Merry 	mpr_mapping_initialize(sc);
2235991554f2SKenneth D. Merry 	mprsas_startup(sc);
2236991554f2SKenneth D. Merry 	mpr_unlock(sc);
2237a4bb51a4SScott Long 
2238a4bb51a4SScott Long 	mpr_dprint(sc, MPR_INIT, "disestablish config intrhook\n");
2239a4bb51a4SScott Long 	config_intrhook_disestablish(&sc->mpr_ich);
2240a4bb51a4SScott Long 	sc->mpr_ich.ich_arg = NULL;
2241a4bb51a4SScott Long 
2242757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit\n", __func__);
2243991554f2SKenneth D. Merry }
2244991554f2SKenneth D. Merry 
2245991554f2SKenneth D. Merry /* Periodic watchdog.  Is called with the driver lock already held. */
2246991554f2SKenneth D. Merry static void
mpr_periodic(void * arg)2247991554f2SKenneth D. Merry mpr_periodic(void *arg)
2248991554f2SKenneth D. Merry {
2249991554f2SKenneth D. Merry 	struct mpr_softc *sc;
2250991554f2SKenneth D. Merry 	uint32_t db;
2251991554f2SKenneth D. Merry 
2252991554f2SKenneth D. Merry 	sc = (struct mpr_softc *)arg;
2253991554f2SKenneth D. Merry 	if (sc->mpr_flags & MPR_FLAGS_SHUTDOWN)
2254991554f2SKenneth D. Merry 		return;
2255991554f2SKenneth D. Merry 
2256991554f2SKenneth D. Merry 	db = mpr_regread(sc, MPI2_DOORBELL_OFFSET);
2257991554f2SKenneth D. Merry 	if ((db & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2258991554f2SKenneth D. Merry 		if ((db & MPI2_DOORBELL_FAULT_CODE_MASK) ==
2259991554f2SKenneth D. Merry 		    IFAULT_IOP_OVER_TEMP_THRESHOLD_EXCEEDED) {
2260991554f2SKenneth D. Merry 			panic("TEMPERATURE FAULT: STOPPING.");
2261991554f2SKenneth D. Merry 		}
2262991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "IOC Fault 0x%08x, Resetting\n", db);
2263991554f2SKenneth D. Merry 		mpr_reinit(sc);
2264991554f2SKenneth D. Merry 	}
2265991554f2SKenneth D. Merry 
22661849bc5fSAlexander Motin 	callout_reset_sbt(&sc->periodic, MPR_PERIODIC_DELAY * SBT_1S, 0,
22671849bc5fSAlexander Motin 	    mpr_periodic, sc, C_PREL(1));
2268991554f2SKenneth D. Merry }
2269991554f2SKenneth D. Merry 
2270991554f2SKenneth D. Merry static void
mpr_log_evt_handler(struct mpr_softc * sc,uintptr_t data,MPI2_EVENT_NOTIFICATION_REPLY * event)2271991554f2SKenneth D. Merry mpr_log_evt_handler(struct mpr_softc *sc, uintptr_t data,
2272991554f2SKenneth D. Merry     MPI2_EVENT_NOTIFICATION_REPLY *event)
2273991554f2SKenneth D. Merry {
2274991554f2SKenneth D. Merry 	MPI2_EVENT_DATA_LOG_ENTRY_ADDED *entry;
2275991554f2SKenneth D. Merry 
2276055e2653SScott Long 	MPR_DPRINT_EVENT(sc, generic, event);
2277991554f2SKenneth D. Merry 
2278991554f2SKenneth D. Merry 	switch (event->Event) {
2279991554f2SKenneth D. Merry 	case MPI2_EVENT_LOG_DATA:
2280991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "MPI2_EVENT_LOG_DATA:\n");
2281991554f2SKenneth D. Merry 		if (sc->mpr_debug & MPR_EVENT)
2282991554f2SKenneth D. Merry 			hexdump(event->EventData, event->EventDataLength, NULL,
2283991554f2SKenneth D. Merry 			    0);
2284991554f2SKenneth D. Merry 		break;
2285991554f2SKenneth D. Merry 	case MPI2_EVENT_LOG_ENTRY_ADDED:
2286991554f2SKenneth D. Merry 		entry = (MPI2_EVENT_DATA_LOG_ENTRY_ADDED *)event->EventData;
2287991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "MPI2_EVENT_LOG_ENTRY_ADDED event "
2288991554f2SKenneth D. Merry 		    "0x%x Sequence %d:\n", entry->LogEntryQualifier,
2289991554f2SKenneth D. Merry 		     entry->LogSequence);
2290991554f2SKenneth D. Merry 		break;
2291991554f2SKenneth D. Merry 	default:
2292991554f2SKenneth D. Merry 		break;
2293991554f2SKenneth D. Merry 	}
2294991554f2SKenneth D. Merry 	return;
2295991554f2SKenneth D. Merry }
2296991554f2SKenneth D. Merry 
2297991554f2SKenneth D. Merry static int
mpr_attach_log(struct mpr_softc * sc)2298991554f2SKenneth D. Merry mpr_attach_log(struct mpr_softc *sc)
2299991554f2SKenneth D. Merry {
2300991554f2SKenneth D. Merry 	uint8_t events[16];
2301991554f2SKenneth D. Merry 
2302991554f2SKenneth D. Merry 	bzero(events, 16);
2303991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_LOG_DATA);
2304991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_LOG_ENTRY_ADDED);
2305991554f2SKenneth D. Merry 
2306991554f2SKenneth D. Merry 	mpr_register_events(sc, events, mpr_log_evt_handler, NULL,
2307991554f2SKenneth D. Merry 	    &sc->mpr_log_eh);
2308991554f2SKenneth D. Merry 
2309991554f2SKenneth D. Merry 	return (0);
2310991554f2SKenneth D. Merry }
2311991554f2SKenneth D. Merry 
2312991554f2SKenneth D. Merry static int
mpr_detach_log(struct mpr_softc * sc)2313991554f2SKenneth D. Merry mpr_detach_log(struct mpr_softc *sc)
2314991554f2SKenneth D. Merry {
2315991554f2SKenneth D. Merry 
2316991554f2SKenneth D. Merry 	if (sc->mpr_log_eh != NULL)
2317991554f2SKenneth D. Merry 		mpr_deregister_events(sc, sc->mpr_log_eh);
2318991554f2SKenneth D. Merry 	return (0);
2319991554f2SKenneth D. Merry }
2320991554f2SKenneth D. Merry 
2321991554f2SKenneth D. Merry /*
2322991554f2SKenneth D. Merry  * Free all of the driver resources and detach submodules.  Should be called
2323991554f2SKenneth D. Merry  * without the lock held.
2324991554f2SKenneth D. Merry  */
2325991554f2SKenneth D. Merry int
mpr_free(struct mpr_softc * sc)2326991554f2SKenneth D. Merry mpr_free(struct mpr_softc *sc)
2327991554f2SKenneth D. Merry {
2328991554f2SKenneth D. Merry 	int error;
2329991554f2SKenneth D. Merry 
2330757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
2331991554f2SKenneth D. Merry 	/* Turn off the watchdog */
2332991554f2SKenneth D. Merry 	mpr_lock(sc);
2333991554f2SKenneth D. Merry 	sc->mpr_flags |= MPR_FLAGS_SHUTDOWN;
2334991554f2SKenneth D. Merry 	mpr_unlock(sc);
2335991554f2SKenneth D. Merry 	/* Lock must not be held for this */
2336991554f2SKenneth D. Merry 	callout_drain(&sc->periodic);
2337327f2e6cSStephen McConnell 	callout_drain(&sc->device_check_callout);
2338991554f2SKenneth D. Merry 
2339991554f2SKenneth D. Merry 	if (((error = mpr_detach_log(sc)) != 0) ||
2340757ff642SScott Long 	    ((error = mpr_detach_sas(sc)) != 0)) {
2341757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_FAULT, "failed to detach "
2342757ff642SScott Long 		    "subsystems, error= %d, exit\n", error);
2343991554f2SKenneth D. Merry 		return (error);
2344757ff642SScott Long 	}
2345991554f2SKenneth D. Merry 
2346991554f2SKenneth D. Merry 	mpr_detach_user(sc);
2347991554f2SKenneth D. Merry 
2348991554f2SKenneth D. Merry 	/* Put the IOC back in the READY state. */
2349991554f2SKenneth D. Merry 	mpr_lock(sc);
2350991554f2SKenneth D. Merry 	if ((error = mpr_transition_ready(sc)) != 0) {
2351991554f2SKenneth D. Merry 		mpr_unlock(sc);
2352991554f2SKenneth D. Merry 		return (error);
2353991554f2SKenneth D. Merry 	}
2354991554f2SKenneth D. Merry 	mpr_unlock(sc);
2355991554f2SKenneth D. Merry 
2356991554f2SKenneth D. Merry 	if (sc->facts != NULL)
2357991554f2SKenneth D. Merry 		free(sc->facts, M_MPR);
2358991554f2SKenneth D. Merry 
2359991554f2SKenneth D. Merry 	/*
2360991554f2SKenneth D. Merry 	 * Free all buffers that are based on IOC Facts.  A Diag Reset may need
2361991554f2SKenneth D. Merry 	 * to free these buffers too.
2362991554f2SKenneth D. Merry 	 */
2363991554f2SKenneth D. Merry 	mpr_iocfacts_free(sc);
2364991554f2SKenneth D. Merry 
2365991554f2SKenneth D. Merry 	if (sc->sysctl_tree != NULL)
2366991554f2SKenneth D. Merry 		sysctl_ctx_free(&sc->sysctl_ctx);
2367991554f2SKenneth D. Merry 
2368991554f2SKenneth D. Merry 	/* Deregister the shutdown function */
2369991554f2SKenneth D. Merry 	if (sc->shutdown_eh != NULL)
2370991554f2SKenneth D. Merry 		EVENTHANDLER_DEREGISTER(shutdown_final, sc->shutdown_eh);
2371991554f2SKenneth D. Merry 
2372991554f2SKenneth D. Merry 	mtx_destroy(&sc->mpr_mtx);
2373757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit\n", __func__);
2374991554f2SKenneth D. Merry 
2375991554f2SKenneth D. Merry 	return (0);
2376991554f2SKenneth D. Merry }
2377991554f2SKenneth D. Merry 
2378991554f2SKenneth D. Merry static __inline void
mpr_complete_command(struct mpr_softc * sc,struct mpr_command * cm)2379991554f2SKenneth D. Merry mpr_complete_command(struct mpr_softc *sc, struct mpr_command *cm)
2380991554f2SKenneth D. Merry {
2381991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
2382991554f2SKenneth D. Merry 
2383991554f2SKenneth D. Merry 	if (cm == NULL) {
2384991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "Completing NULL command\n");
2385991554f2SKenneth D. Merry 		return;
2386991554f2SKenneth D. Merry 	}
2387991554f2SKenneth D. Merry 
2388175ad3d0SKenneth D. Merry 	KASSERT(cm->cm_state == MPR_CM_STATE_INQUEUE,
2389175ad3d0SKenneth D. Merry 	    ("command not inqueue, state = %u\n", cm->cm_state));
23904b1ac5c2SWarner Losh 	cm->cm_state = MPR_CM_STATE_BUSY;
2391991554f2SKenneth D. Merry 	if (cm->cm_flags & MPR_CM_FLAGS_POLLED)
2392991554f2SKenneth D. Merry 		cm->cm_flags |= MPR_CM_FLAGS_COMPLETE;
2393991554f2SKenneth D. Merry 
2394991554f2SKenneth D. Merry 	if (cm->cm_complete != NULL) {
2395991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_TRACE,
2396991554f2SKenneth D. Merry 		    "%s cm %p calling cm_complete %p data %p reply %p\n",
2397991554f2SKenneth D. Merry 		    __func__, cm, cm->cm_complete, cm->cm_complete_data,
2398991554f2SKenneth D. Merry 		    cm->cm_reply);
2399991554f2SKenneth D. Merry 		cm->cm_complete(sc, cm);
2400991554f2SKenneth D. Merry 	}
2401991554f2SKenneth D. Merry 
2402991554f2SKenneth D. Merry 	if (cm->cm_flags & MPR_CM_FLAGS_WAKEUP) {
2403991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_TRACE, "waking up %p\n", cm);
2404991554f2SKenneth D. Merry 		wakeup(cm);
2405991554f2SKenneth D. Merry 	}
2406991554f2SKenneth D. Merry 
2407991554f2SKenneth D. Merry 	if (sc->io_cmds_active != 0) {
2408991554f2SKenneth D. Merry 		sc->io_cmds_active--;
2409991554f2SKenneth D. Merry 	} else {
2410991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "Warning: io_cmds_active is "
2411991554f2SKenneth D. Merry 		    "out of sync - resynching to 0\n");
2412991554f2SKenneth D. Merry 	}
2413991554f2SKenneth D. Merry }
2414991554f2SKenneth D. Merry 
2415991554f2SKenneth D. Merry static void
mpr_sas_log_info(struct mpr_softc * sc,u32 log_info)2416991554f2SKenneth D. Merry mpr_sas_log_info(struct mpr_softc *sc , u32 log_info)
2417991554f2SKenneth D. Merry {
2418991554f2SKenneth D. Merry 	union loginfo_type {
2419991554f2SKenneth D. Merry 		u32	loginfo;
2420991554f2SKenneth D. Merry 		struct {
2421991554f2SKenneth D. Merry 			u32	subcode:16;
2422991554f2SKenneth D. Merry 			u32	code:8;
2423991554f2SKenneth D. Merry 			u32	originator:4;
2424991554f2SKenneth D. Merry 			u32	bus_type:4;
2425991554f2SKenneth D. Merry 		} dw;
2426991554f2SKenneth D. Merry 	};
2427991554f2SKenneth D. Merry 	union loginfo_type sas_loginfo;
2428991554f2SKenneth D. Merry 	char *originator_str = NULL;
2429991554f2SKenneth D. Merry 
2430991554f2SKenneth D. Merry 	sas_loginfo.loginfo = log_info;
2431991554f2SKenneth D. Merry 	if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
2432991554f2SKenneth D. Merry 		return;
2433991554f2SKenneth D. Merry 
2434991554f2SKenneth D. Merry 	/* each nexus loss loginfo */
2435991554f2SKenneth D. Merry 	if (log_info == 0x31170000)
2436991554f2SKenneth D. Merry 		return;
2437991554f2SKenneth D. Merry 
2438991554f2SKenneth D. Merry 	/* eat the loginfos associated with task aborts */
2439991554f2SKenneth D. Merry 	if ((log_info == 30050000) || (log_info == 0x31140000) ||
2440991554f2SKenneth D. Merry 	    (log_info == 0x31130000))
2441991554f2SKenneth D. Merry 		return;
2442991554f2SKenneth D. Merry 
2443991554f2SKenneth D. Merry 	switch (sas_loginfo.dw.originator) {
2444991554f2SKenneth D. Merry 	case 0:
2445991554f2SKenneth D. Merry 		originator_str = "IOP";
2446991554f2SKenneth D. Merry 		break;
2447991554f2SKenneth D. Merry 	case 1:
2448991554f2SKenneth D. Merry 		originator_str = "PL";
2449991554f2SKenneth D. Merry 		break;
2450991554f2SKenneth D. Merry 	case 2:
2451991554f2SKenneth D. Merry 		originator_str = "IR";
2452991554f2SKenneth D. Merry 		break;
2453991554f2SKenneth D. Merry 	}
2454991554f2SKenneth D. Merry 
2455b41c6ff9SStephen McConnell 	mpr_dprint(sc, MPR_LOG, "log_info(0x%08x): originator(%s), "
24567a2a6a1aSStephen McConnell 	    "code(0x%02x), sub_code(0x%04x)\n", log_info, originator_str,
24577a2a6a1aSStephen McConnell 	    sas_loginfo.dw.code, sas_loginfo.dw.subcode);
2458991554f2SKenneth D. Merry }
2459991554f2SKenneth D. Merry 
2460991554f2SKenneth D. Merry static void
mpr_display_reply_info(struct mpr_softc * sc,uint8_t * reply)2461991554f2SKenneth D. Merry mpr_display_reply_info(struct mpr_softc *sc, uint8_t *reply)
2462991554f2SKenneth D. Merry {
2463991554f2SKenneth D. Merry 	MPI2DefaultReply_t *mpi_reply;
2464991554f2SKenneth D. Merry 	u16 sc_status;
2465991554f2SKenneth D. Merry 
2466991554f2SKenneth D. Merry 	mpi_reply = (MPI2DefaultReply_t*)reply;
2467991554f2SKenneth D. Merry 	sc_status = le16toh(mpi_reply->IOCStatus);
2468991554f2SKenneth D. Merry 	if (sc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
2469991554f2SKenneth D. Merry 		mpr_sas_log_info(sc, le32toh(mpi_reply->IOCLogInfo));
2470991554f2SKenneth D. Merry }
2471991554f2SKenneth D. Merry 
2472991554f2SKenneth D. Merry void
mpr_intr(void * data)2473991554f2SKenneth D. Merry mpr_intr(void *data)
2474991554f2SKenneth D. Merry {
2475991554f2SKenneth D. Merry 	struct mpr_softc *sc;
2476991554f2SKenneth D. Merry 	uint32_t status;
2477991554f2SKenneth D. Merry 
2478991554f2SKenneth D. Merry 	sc = (struct mpr_softc *)data;
2479991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
2480991554f2SKenneth D. Merry 
2481991554f2SKenneth D. Merry 	/*
2482991554f2SKenneth D. Merry 	 * Check interrupt status register to flush the bus.  This is
2483991554f2SKenneth D. Merry 	 * needed for both INTx interrupts and driver-driven polling
2484991554f2SKenneth D. Merry 	 */
2485991554f2SKenneth D. Merry 	status = mpr_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET);
2486991554f2SKenneth D. Merry 	if ((status & MPI2_HIS_REPLY_DESCRIPTOR_INTERRUPT) == 0)
2487991554f2SKenneth D. Merry 		return;
2488991554f2SKenneth D. Merry 
2489991554f2SKenneth D. Merry 	mpr_lock(sc);
2490991554f2SKenneth D. Merry 	mpr_intr_locked(data);
2491991554f2SKenneth D. Merry 	mpr_unlock(sc);
2492991554f2SKenneth D. Merry 	return;
2493991554f2SKenneth D. Merry }
2494991554f2SKenneth D. Merry 
2495991554f2SKenneth D. Merry /*
2496991554f2SKenneth D. Merry  * In theory, MSI/MSIX interrupts shouldn't need to read any registers on the
2497991554f2SKenneth D. Merry  * chip.  Hopefully this theory is correct.
2498991554f2SKenneth D. Merry  */
2499991554f2SKenneth D. Merry void
mpr_intr_msi(void * data)2500991554f2SKenneth D. Merry mpr_intr_msi(void *data)
2501991554f2SKenneth D. Merry {
2502991554f2SKenneth D. Merry 	struct mpr_softc *sc;
2503991554f2SKenneth D. Merry 
2504991554f2SKenneth D. Merry 	sc = (struct mpr_softc *)data;
2505991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
2506991554f2SKenneth D. Merry 	mpr_lock(sc);
2507991554f2SKenneth D. Merry 	mpr_intr_locked(data);
2508991554f2SKenneth D. Merry 	mpr_unlock(sc);
2509991554f2SKenneth D. Merry 	return;
2510991554f2SKenneth D. Merry }
2511991554f2SKenneth D. Merry 
2512991554f2SKenneth D. Merry /*
2513991554f2SKenneth D. Merry  * The locking is overly broad and simplistic, but easy to deal with for now.
2514991554f2SKenneth D. Merry  */
2515991554f2SKenneth D. Merry void
mpr_intr_locked(void * data)2516991554f2SKenneth D. Merry mpr_intr_locked(void *data)
2517991554f2SKenneth D. Merry {
2518991554f2SKenneth D. Merry 	MPI2_REPLY_DESCRIPTORS_UNION *desc;
2519617e85f3SScott Long 	MPI2_DIAG_RELEASE_REPLY *rel_rep;
2520617e85f3SScott Long 	mpr_fw_diagnostic_buffer_t *pBuffer;
2521991554f2SKenneth D. Merry 	struct mpr_softc *sc;
2522617e85f3SScott Long 	uint64_t tdesc;
2523991554f2SKenneth D. Merry 	struct mpr_command *cm = NULL;
2524991554f2SKenneth D. Merry 	uint8_t flags;
2525991554f2SKenneth D. Merry 	u_int pq;
2526991554f2SKenneth D. Merry 
2527991554f2SKenneth D. Merry 	sc = (struct mpr_softc *)data;
2528991554f2SKenneth D. Merry 
2529991554f2SKenneth D. Merry 	pq = sc->replypostindex;
2530991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE,
2531991554f2SKenneth D. Merry 	    "%s sc %p starting with replypostindex %u\n",
2532991554f2SKenneth D. Merry 	    __func__, sc, sc->replypostindex);
2533991554f2SKenneth D. Merry 
2534991554f2SKenneth D. Merry 	for ( ;; ) {
2535991554f2SKenneth D. Merry 		cm = NULL;
2536991554f2SKenneth D. Merry 		desc = &sc->post_queue[sc->replypostindex];
2537617e85f3SScott Long 
2538617e85f3SScott Long 		/*
2539617e85f3SScott Long 		 * Copy and clear out the descriptor so that any reentry will
2540617e85f3SScott Long 		 * immediately know that this descriptor has already been
2541617e85f3SScott Long 		 * looked at.  There is unfortunate casting magic because the
2542617e85f3SScott Long 		 * MPI API doesn't have a cardinal 64bit type.
2543617e85f3SScott Long 		 */
2544617e85f3SScott Long 		tdesc = 0xffffffffffffffff;
2545617e85f3SScott Long 		tdesc = atomic_swap_64((uint64_t *)desc, tdesc);
2546617e85f3SScott Long 		desc = (MPI2_REPLY_DESCRIPTORS_UNION *)&tdesc;
2547617e85f3SScott Long 
2548991554f2SKenneth D. Merry 		flags = desc->Default.ReplyFlags &
2549991554f2SKenneth D. Merry 		    MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
2550991554f2SKenneth D. Merry 		if ((flags == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) ||
2551991554f2SKenneth D. Merry 		    (le32toh(desc->Words.High) == 0xffffffff))
2552991554f2SKenneth D. Merry 			break;
2553991554f2SKenneth D. Merry 
2554991554f2SKenneth D. Merry 		/* increment the replypostindex now, so that event handlers
2555991554f2SKenneth D. Merry 		 * and cm completion handlers which decide to do a diag
2556991554f2SKenneth D. Merry 		 * reset can zero it without it getting incremented again
2557991554f2SKenneth D. Merry 		 * afterwards, and we break out of this loop on the next
2558991554f2SKenneth D. Merry 		 * iteration since the reply post queue has been cleared to
2559991554f2SKenneth D. Merry 		 * 0xFF and all descriptors look unused (which they are).
2560991554f2SKenneth D. Merry 		 */
2561991554f2SKenneth D. Merry 		if (++sc->replypostindex >= sc->pqdepth)
2562991554f2SKenneth D. Merry 			sc->replypostindex = 0;
2563991554f2SKenneth D. Merry 
2564991554f2SKenneth D. Merry 		switch (flags) {
2565991554f2SKenneth D. Merry 		case MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS:
2566991554f2SKenneth D. Merry 		case MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS:
256767feec50SStephen McConnell 		case MPI26_RPY_DESCRIPT_FLAGS_PCIE_ENCAPSULATED_SUCCESS:
2568991554f2SKenneth D. Merry 			cm = &sc->commands[le16toh(desc->SCSIIOSuccess.SMID)];
2569991554f2SKenneth D. Merry 			cm->cm_reply = NULL;
2570991554f2SKenneth D. Merry 			break;
2571991554f2SKenneth D. Merry 		case MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY:
2572991554f2SKenneth D. Merry 		{
2573991554f2SKenneth D. Merry 			uint32_t baddr;
2574991554f2SKenneth D. Merry 			uint8_t *reply;
2575991554f2SKenneth D. Merry 
2576991554f2SKenneth D. Merry 			/*
2577991554f2SKenneth D. Merry 			 * Re-compose the reply address from the address
2578991554f2SKenneth D. Merry 			 * sent back from the chip.  The ReplyFrameAddress
2579991554f2SKenneth D. Merry 			 * is the lower 32 bits of the physical address of
2580991554f2SKenneth D. Merry 			 * particular reply frame.  Convert that address to
2581991554f2SKenneth D. Merry 			 * host format, and then use that to provide the
2582991554f2SKenneth D. Merry 			 * offset against the virtual address base
2583991554f2SKenneth D. Merry 			 * (sc->reply_frames).
2584991554f2SKenneth D. Merry 			 */
2585991554f2SKenneth D. Merry 			baddr = le32toh(desc->AddressReply.ReplyFrameAddress);
2586991554f2SKenneth D. Merry 			reply = sc->reply_frames +
2587991554f2SKenneth D. Merry 				(baddr - ((uint32_t)sc->reply_busaddr));
2588991554f2SKenneth D. Merry 			/*
2589991554f2SKenneth D. Merry 			 * Make sure the reply we got back is in a valid
2590991554f2SKenneth D. Merry 			 * range.  If not, go ahead and panic here, since
2591991554f2SKenneth D. Merry 			 * we'll probably panic as soon as we deference the
2592991554f2SKenneth D. Merry 			 * reply pointer anyway.
2593991554f2SKenneth D. Merry 			 */
2594991554f2SKenneth D. Merry 			if ((reply < sc->reply_frames)
2595991554f2SKenneth D. Merry 			 || (reply > (sc->reply_frames +
259696410703SScott Long 			     (sc->fqdepth * sc->replyframesz)))) {
2597991554f2SKenneth D. Merry 				printf("%s: WARNING: reply %p out of range!\n",
2598991554f2SKenneth D. Merry 				       __func__, reply);
2599991554f2SKenneth D. Merry 				printf("%s: reply_frames %p, fqdepth %d, "
2600991554f2SKenneth D. Merry 				       "frame size %d\n", __func__,
2601991554f2SKenneth D. Merry 				       sc->reply_frames, sc->fqdepth,
260296410703SScott Long 				       sc->replyframesz);
2603991554f2SKenneth D. Merry 				printf("%s: baddr %#x,\n", __func__, baddr);
2604991554f2SKenneth D. Merry 				/* LSI-TODO. See Linux Code for Graceful exit */
2605991554f2SKenneth D. Merry 				panic("Reply address out of range");
2606991554f2SKenneth D. Merry 			}
2607991554f2SKenneth D. Merry 			if (le16toh(desc->AddressReply.SMID) == 0) {
2608991554f2SKenneth D. Merry 				if (((MPI2_DEFAULT_REPLY *)reply)->Function ==
2609991554f2SKenneth D. Merry 				    MPI2_FUNCTION_DIAG_BUFFER_POST) {
2610991554f2SKenneth D. Merry 					/*
2611991554f2SKenneth D. Merry 					 * If SMID is 0 for Diag Buffer Post,
2612991554f2SKenneth D. Merry 					 * this implies that the reply is due to
2613991554f2SKenneth D. Merry 					 * a release function with a status that
2614991554f2SKenneth D. Merry 					 * the buffer has been released.  Set
2615991554f2SKenneth D. Merry 					 * the buffer flags accordingly.
2616991554f2SKenneth D. Merry 					 */
2617991554f2SKenneth D. Merry 					rel_rep =
2618991554f2SKenneth D. Merry 					    (MPI2_DIAG_RELEASE_REPLY *)reply;
2619d3f6eabfSStephen McConnell 					if ((le16toh(rel_rep->IOCStatus) &
2620d3f6eabfSStephen McConnell 					    MPI2_IOCSTATUS_MASK) ==
2621991554f2SKenneth D. Merry 					    MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED)
2622991554f2SKenneth D. Merry 					{
2623991554f2SKenneth D. Merry 						pBuffer =
2624991554f2SKenneth D. Merry 						    &sc->fw_diag_buffer_list[
2625991554f2SKenneth D. Merry 						    rel_rep->BufferType];
2626991554f2SKenneth D. Merry 						pBuffer->valid_data = TRUE;
2627991554f2SKenneth D. Merry 						pBuffer->owned_by_firmware =
2628991554f2SKenneth D. Merry 						    FALSE;
2629991554f2SKenneth D. Merry 						pBuffer->immediate = FALSE;
2630991554f2SKenneth D. Merry 					}
2631991554f2SKenneth D. Merry 				} else
2632991554f2SKenneth D. Merry 					mpr_dispatch_event(sc, baddr,
2633991554f2SKenneth D. Merry 					    (MPI2_EVENT_NOTIFICATION_REPLY *)
2634991554f2SKenneth D. Merry 					    reply);
2635991554f2SKenneth D. Merry 			} else {
2636991554f2SKenneth D. Merry 				cm = &sc->commands[
2637991554f2SKenneth D. Merry 				    le16toh(desc->AddressReply.SMID)];
26388fe7bf06SWarner Losh 				if (cm->cm_state == MPR_CM_STATE_INQUEUE) {
2639991554f2SKenneth D. Merry 					cm->cm_reply = reply;
2640991554f2SKenneth D. Merry 					cm->cm_reply_data =
2641991554f2SKenneth D. Merry 					    le32toh(desc->AddressReply.
2642991554f2SKenneth D. Merry 						ReplyFrameAddress);
26438fe7bf06SWarner Losh 				} else {
26448fe7bf06SWarner Losh 					mpr_dprint(sc, MPR_RECOVERY,
26458fe7bf06SWarner Losh 					    "Bad state for ADDRESS_REPLY status,"
26468fe7bf06SWarner Losh 					    " ignoring state %d cm %p\n",
26478fe7bf06SWarner Losh 					    cm->cm_state, cm);
26488fe7bf06SWarner Losh 				}
2649991554f2SKenneth D. Merry 			}
2650991554f2SKenneth D. Merry 			break;
2651991554f2SKenneth D. Merry 		}
2652991554f2SKenneth D. Merry 		case MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS:
2653991554f2SKenneth D. Merry 		case MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER:
2654991554f2SKenneth D. Merry 		case MPI2_RPY_DESCRIPT_FLAGS_RAID_ACCELERATOR_SUCCESS:
2655991554f2SKenneth D. Merry 		default:
2656991554f2SKenneth D. Merry 			/* Unhandled */
2657991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_ERROR, "Unhandled reply 0x%x\n",
2658991554f2SKenneth D. Merry 			    desc->Default.ReplyFlags);
2659991554f2SKenneth D. Merry 			cm = NULL;
2660991554f2SKenneth D. Merry 			break;
2661991554f2SKenneth D. Merry 		}
2662991554f2SKenneth D. Merry 
2663991554f2SKenneth D. Merry 		if (cm != NULL) {
2664991554f2SKenneth D. Merry 			// Print Error reply frame
2665991554f2SKenneth D. Merry 			if (cm->cm_reply)
2666991554f2SKenneth D. Merry 				mpr_display_reply_info(sc,cm->cm_reply);
2667991554f2SKenneth D. Merry 			mpr_complete_command(sc, cm);
2668991554f2SKenneth D. Merry 		}
2669991554f2SKenneth D. Merry 	}
2670991554f2SKenneth D. Merry 
2671991554f2SKenneth D. Merry 	if (pq != sc->replypostindex) {
2672f0779b04SScott Long 		mpr_dprint(sc, MPR_TRACE, "%s sc %p writing postindex %d\n",
2673991554f2SKenneth D. Merry 		    __func__, sc, sc->replypostindex);
2674991554f2SKenneth D. Merry 		mpr_regwrite(sc, MPI2_REPLY_POST_HOST_INDEX_OFFSET,
2675991554f2SKenneth D. Merry 		    sc->replypostindex);
2676991554f2SKenneth D. Merry 	}
2677991554f2SKenneth D. Merry 
2678991554f2SKenneth D. Merry 	return;
2679991554f2SKenneth D. Merry }
2680991554f2SKenneth D. Merry 
2681991554f2SKenneth D. Merry static void
mpr_dispatch_event(struct mpr_softc * sc,uintptr_t data,MPI2_EVENT_NOTIFICATION_REPLY * reply)2682991554f2SKenneth D. Merry mpr_dispatch_event(struct mpr_softc *sc, uintptr_t data,
2683991554f2SKenneth D. Merry     MPI2_EVENT_NOTIFICATION_REPLY *reply)
2684991554f2SKenneth D. Merry {
2685991554f2SKenneth D. Merry 	struct mpr_event_handle *eh;
2686991554f2SKenneth D. Merry 	int event, handled = 0;
2687991554f2SKenneth D. Merry 
2688991554f2SKenneth D. Merry 	event = le16toh(reply->Event);
2689991554f2SKenneth D. Merry 	TAILQ_FOREACH(eh, &sc->event_list, eh_list) {
2690991554f2SKenneth D. Merry 		if (isset(eh->mask, event)) {
2691991554f2SKenneth D. Merry 			eh->callback(sc, data, reply);
2692991554f2SKenneth D. Merry 			handled++;
2693991554f2SKenneth D. Merry 		}
2694991554f2SKenneth D. Merry 	}
2695991554f2SKenneth D. Merry 
2696991554f2SKenneth D. Merry 	if (handled == 0)
2697991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "Unhandled event 0x%x\n",
2698991554f2SKenneth D. Merry 		    le16toh(event));
2699991554f2SKenneth D. Merry 
2700991554f2SKenneth D. Merry 	/*
2701991554f2SKenneth D. Merry 	 * This is the only place that the event/reply should be freed.
2702991554f2SKenneth D. Merry 	 * Anything wanting to hold onto the event data should have
2703991554f2SKenneth D. Merry 	 * already copied it into their own storage.
2704991554f2SKenneth D. Merry 	 */
2705991554f2SKenneth D. Merry 	mpr_free_reply(sc, data);
2706991554f2SKenneth D. Merry }
2707991554f2SKenneth D. Merry 
2708991554f2SKenneth D. Merry static void
mpr_reregister_events_complete(struct mpr_softc * sc,struct mpr_command * cm)2709991554f2SKenneth D. Merry mpr_reregister_events_complete(struct mpr_softc *sc, struct mpr_command *cm)
2710991554f2SKenneth D. Merry {
2711991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
2712991554f2SKenneth D. Merry 
2713991554f2SKenneth D. Merry 	if (cm->cm_reply)
2714055e2653SScott Long 		MPR_DPRINT_EVENT(sc, generic,
2715991554f2SKenneth D. Merry 			(MPI2_EVENT_NOTIFICATION_REPLY *)cm->cm_reply);
2716991554f2SKenneth D. Merry 
2717991554f2SKenneth D. Merry 	mpr_free_command(sc, cm);
2718991554f2SKenneth D. Merry 
2719991554f2SKenneth D. Merry 	/* next, send a port enable */
2720991554f2SKenneth D. Merry 	mprsas_startup(sc);
2721991554f2SKenneth D. Merry }
2722991554f2SKenneth D. Merry 
2723991554f2SKenneth D. Merry /*
2724991554f2SKenneth D. Merry  * For both register_events and update_events, the caller supplies a bitmap
2725991554f2SKenneth D. Merry  * of events that it _wants_.  These functions then turn that into a bitmask
2726991554f2SKenneth D. Merry  * suitable for the controller.
2727991554f2SKenneth D. Merry  */
2728991554f2SKenneth D. Merry int
mpr_register_events(struct mpr_softc * sc,uint8_t * mask,mpr_evt_callback_t * cb,void * data,struct mpr_event_handle ** handle)2729991554f2SKenneth D. Merry mpr_register_events(struct mpr_softc *sc, uint8_t *mask,
2730991554f2SKenneth D. Merry     mpr_evt_callback_t *cb, void *data, struct mpr_event_handle **handle)
2731991554f2SKenneth D. Merry {
2732991554f2SKenneth D. Merry 	struct mpr_event_handle *eh;
2733991554f2SKenneth D. Merry 	int error = 0;
2734991554f2SKenneth D. Merry 
2735991554f2SKenneth D. Merry 	eh = malloc(sizeof(struct mpr_event_handle), M_MPR, M_WAITOK|M_ZERO);
2736991554f2SKenneth D. Merry 	eh->callback = cb;
2737991554f2SKenneth D. Merry 	eh->data = data;
2738991554f2SKenneth D. Merry 	TAILQ_INSERT_TAIL(&sc->event_list, eh, eh_list);
2739991554f2SKenneth D. Merry 	if (mask != NULL)
2740991554f2SKenneth D. Merry 		error = mpr_update_events(sc, eh, mask);
2741991554f2SKenneth D. Merry 	*handle = eh;
2742991554f2SKenneth D. Merry 
2743991554f2SKenneth D. Merry 	return (error);
2744991554f2SKenneth D. Merry }
2745991554f2SKenneth D. Merry 
2746991554f2SKenneth D. Merry int
mpr_update_events(struct mpr_softc * sc,struct mpr_event_handle * handle,uint8_t * mask)2747991554f2SKenneth D. Merry mpr_update_events(struct mpr_softc *sc, struct mpr_event_handle *handle,
2748991554f2SKenneth D. Merry     uint8_t *mask)
2749991554f2SKenneth D. Merry {
2750991554f2SKenneth D. Merry 	MPI2_EVENT_NOTIFICATION_REQUEST *evtreq;
27516d4ffcb4SKenneth D. Merry 	MPI2_EVENT_NOTIFICATION_REPLY *reply = NULL;
27526d4ffcb4SKenneth D. Merry 	struct mpr_command *cm = NULL;
2753991554f2SKenneth D. Merry 	struct mpr_event_handle *eh;
2754991554f2SKenneth D. Merry 	int error, i;
2755991554f2SKenneth D. Merry 
2756991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
2757991554f2SKenneth D. Merry 
2758991554f2SKenneth D. Merry 	if ((mask != NULL) && (handle != NULL))
2759991554f2SKenneth D. Merry 		bcopy(mask, &handle->mask[0], 16);
2760991554f2SKenneth D. Merry 	memset(sc->event_mask, 0xff, 16);
2761991554f2SKenneth D. Merry 
2762991554f2SKenneth D. Merry 	TAILQ_FOREACH(eh, &sc->event_list, eh_list) {
2763991554f2SKenneth D. Merry 		for (i = 0; i < 16; i++)
2764991554f2SKenneth D. Merry 			sc->event_mask[i] &= ~eh->mask[i];
2765991554f2SKenneth D. Merry 	}
2766991554f2SKenneth D. Merry 
2767991554f2SKenneth D. Merry 	if ((cm = mpr_alloc_command(sc)) == NULL)
2768991554f2SKenneth D. Merry 		return (EBUSY);
2769991554f2SKenneth D. Merry 	evtreq = (MPI2_EVENT_NOTIFICATION_REQUEST *)cm->cm_req;
2770991554f2SKenneth D. Merry 	evtreq->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
2771991554f2SKenneth D. Merry 	evtreq->MsgFlags = 0;
2772991554f2SKenneth D. Merry 	evtreq->SASBroadcastPrimitiveMasks = 0;
2773991554f2SKenneth D. Merry #ifdef MPR_DEBUG_ALL_EVENTS
2774991554f2SKenneth D. Merry 	{
2775444c6615SMariusz Zaborski 		u_char fullmask[sizeof(evtreq->EventMasks)];
2776444c6615SMariusz Zaborski 		memset(fullmask, 0x00, sizeof(fullmask));
2777444c6615SMariusz Zaborski 		bcopy(fullmask, (uint8_t *)&evtreq->EventMasks, sizeof(fullmask));
2778991554f2SKenneth D. Merry 	}
2779991554f2SKenneth D. Merry #else
2780ea6597c3SMariusz Zaborski 	bcopy(sc->event_mask, (uint8_t *)&evtreq->EventMasks, sizeof(sc->event_mask));
278171900a79SAlfredo Dal'Ava Junior 	for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2782ea6597c3SMariusz Zaborski 		evtreq->EventMasks[i] = htole32(evtreq->EventMasks[i]);
2783991554f2SKenneth D. Merry #endif
2784991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2785991554f2SKenneth D. Merry 	cm->cm_data = NULL;
2786991554f2SKenneth D. Merry 
27876d4ffcb4SKenneth D. Merry 	error = mpr_request_polled(sc, &cm);
27886d4ffcb4SKenneth D. Merry 	if (cm != NULL)
2789991554f2SKenneth D. Merry 		reply = (MPI2_EVENT_NOTIFICATION_REPLY *)cm->cm_reply;
2790991554f2SKenneth D. Merry 	if ((reply == NULL) ||
2791991554f2SKenneth D. Merry 	    (reply->IOCStatus & MPI2_IOCSTATUS_MASK) != MPI2_IOCSTATUS_SUCCESS)
2792991554f2SKenneth D. Merry 		error = ENXIO;
2793991554f2SKenneth D. Merry 
2794991554f2SKenneth D. Merry 	if (reply)
2795055e2653SScott Long 		MPR_DPRINT_EVENT(sc, generic, reply);
2796991554f2SKenneth D. Merry 
2797991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s finished error %d\n", __func__, error);
2798991554f2SKenneth D. Merry 
27996d4ffcb4SKenneth D. Merry 	if (cm != NULL)
2800991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
2801991554f2SKenneth D. Merry 	return (error);
2802991554f2SKenneth D. Merry }
2803991554f2SKenneth D. Merry 
2804991554f2SKenneth D. Merry static int
mpr_reregister_events(struct mpr_softc * sc)2805991554f2SKenneth D. Merry mpr_reregister_events(struct mpr_softc *sc)
2806991554f2SKenneth D. Merry {
2807991554f2SKenneth D. Merry 	MPI2_EVENT_NOTIFICATION_REQUEST *evtreq;
2808991554f2SKenneth D. Merry 	struct mpr_command *cm;
2809991554f2SKenneth D. Merry 	struct mpr_event_handle *eh;
2810991554f2SKenneth D. Merry 	int error, i;
2811991554f2SKenneth D. Merry 
2812991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
2813991554f2SKenneth D. Merry 
2814991554f2SKenneth D. Merry 	/* first, reregister events */
2815991554f2SKenneth D. Merry 
2816991554f2SKenneth D. Merry 	memset(sc->event_mask, 0xff, 16);
2817991554f2SKenneth D. Merry 
2818991554f2SKenneth D. Merry 	TAILQ_FOREACH(eh, &sc->event_list, eh_list) {
2819991554f2SKenneth D. Merry 		for (i = 0; i < 16; i++)
2820991554f2SKenneth D. Merry 			sc->event_mask[i] &= ~eh->mask[i];
2821991554f2SKenneth D. Merry 	}
2822991554f2SKenneth D. Merry 
2823991554f2SKenneth D. Merry 	if ((cm = mpr_alloc_command(sc)) == NULL)
2824991554f2SKenneth D. Merry 		return (EBUSY);
2825991554f2SKenneth D. Merry 	evtreq = (MPI2_EVENT_NOTIFICATION_REQUEST *)cm->cm_req;
2826991554f2SKenneth D. Merry 	evtreq->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
2827991554f2SKenneth D. Merry 	evtreq->MsgFlags = 0;
2828991554f2SKenneth D. Merry 	evtreq->SASBroadcastPrimitiveMasks = 0;
2829991554f2SKenneth D. Merry #ifdef MPR_DEBUG_ALL_EVENTS
2830991554f2SKenneth D. Merry 	{
2831444c6615SMariusz Zaborski 		u_char fullmask[sizeof(evtreq->EventMasks)];
2832444c6615SMariusz Zaborski 		memset(fullmask, 0x00, sizeof(fullmask));
2833444c6615SMariusz Zaborski 		bcopy(fullmask, (uint8_t *)&evtreq->EventMasks, sizeof(fullmask));
2834991554f2SKenneth D. Merry 	}
2835991554f2SKenneth D. Merry #else
2836ea6597c3SMariusz Zaborski 	bcopy(sc->event_mask, (uint8_t *)&evtreq->EventMasks, sizeof(sc->event_mask));
283771900a79SAlfredo Dal'Ava Junior 	for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2838ea6597c3SMariusz Zaborski 		evtreq->EventMasks[i] = htole32(evtreq->EventMasks[i]);
2839991554f2SKenneth D. Merry #endif
2840991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2841991554f2SKenneth D. Merry 	cm->cm_data = NULL;
2842991554f2SKenneth D. Merry 	cm->cm_complete = mpr_reregister_events_complete;
2843991554f2SKenneth D. Merry 
2844991554f2SKenneth D. Merry 	error = mpr_map_command(sc, cm);
2845991554f2SKenneth D. Merry 
2846991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s finished with error %d\n", __func__,
2847991554f2SKenneth D. Merry 	    error);
2848991554f2SKenneth D. Merry 	return (error);
2849991554f2SKenneth D. Merry }
2850991554f2SKenneth D. Merry 
2851991554f2SKenneth D. Merry int
mpr_deregister_events(struct mpr_softc * sc,struct mpr_event_handle * handle)2852991554f2SKenneth D. Merry mpr_deregister_events(struct mpr_softc *sc, struct mpr_event_handle *handle)
2853991554f2SKenneth D. Merry {
2854991554f2SKenneth D. Merry 
2855991554f2SKenneth D. Merry 	TAILQ_REMOVE(&sc->event_list, handle, eh_list);
2856991554f2SKenneth D. Merry 	free(handle, M_MPR);
2857991554f2SKenneth D. Merry 	return (mpr_update_events(sc, NULL, NULL));
2858991554f2SKenneth D. Merry }
2859991554f2SKenneth D. Merry 
286067feec50SStephen McConnell /**
286167feec50SStephen McConnell * mpr_build_nvme_prp - This function is called for NVMe end devices to build a
286267feec50SStephen McConnell * native SGL (NVMe PRP). The native SGL is built starting in the first PRP entry
286367feec50SStephen McConnell * of the NVMe message (PRP1). If the data buffer is small enough to be described
286467feec50SStephen McConnell * entirely using PRP1, then PRP2 is not used. If needed, PRP2 is used to
286567feec50SStephen McConnell * describe a larger data buffer. If the data buffer is too large to describe
286667feec50SStephen McConnell * using the two PRP entriess inside the NVMe message, then PRP1 describes the
286767feec50SStephen McConnell * first data memory segment, and PRP2 contains a pointer to a PRP list located
286867feec50SStephen McConnell * elsewhere in memory to describe the remaining data memory segments. The PRP
286967feec50SStephen McConnell * list will be contiguous.
287067feec50SStephen McConnell 
287167feec50SStephen McConnell * The native SGL for NVMe devices is a Physical Region Page (PRP). A PRP
287267feec50SStephen McConnell * consists of a list of PRP entries to describe a number of noncontigous
287367feec50SStephen McConnell * physical memory segments as a single memory buffer, just as a SGL does. Note
287467feec50SStephen McConnell * however, that this function is only used by the IOCTL call, so the memory
287567feec50SStephen McConnell * given will be guaranteed to be contiguous. There is no need to translate
287667feec50SStephen McConnell * non-contiguous SGL into a PRP in this case. All PRPs will describe contiguous
287767feec50SStephen McConnell * space that is one page size each.
287867feec50SStephen McConnell *
287967feec50SStephen McConnell * Each NVMe message contains two PRP entries. The first (PRP1) either contains
288067feec50SStephen McConnell * a PRP list pointer or a PRP element, depending upon the command. PRP2 contains
288167feec50SStephen McConnell * the second PRP element if the memory being described fits within 2 PRP
288267feec50SStephen McConnell * entries, or a PRP list pointer if the PRP spans more than two entries.
288367feec50SStephen McConnell *
288467feec50SStephen McConnell * A PRP list pointer contains the address of a PRP list, structured as a linear
288567feec50SStephen McConnell * array of PRP entries. Each PRP entry in this list describes a segment of
288667feec50SStephen McConnell * physical memory.
288767feec50SStephen McConnell *
288867feec50SStephen McConnell * Each 64-bit PRP entry comprises an address and an offset field. The address
288967feec50SStephen McConnell * always points to the beginning of a PAGE_SIZE physical memory page, and the
289067feec50SStephen McConnell * offset describes where within that page the memory segment begins. Only the
289167feec50SStephen McConnell * first element in a PRP list may contain a non-zero offest, implying that all
289267feec50SStephen McConnell * memory segments following the first begin at the start of a PAGE_SIZE page.
289367feec50SStephen McConnell *
289467feec50SStephen McConnell * Each PRP element normally describes a chunck of PAGE_SIZE physical memory,
289567feec50SStephen McConnell * with exceptions for the first and last elements in the list. If the memory
289667feec50SStephen McConnell * being described by the list begins at a non-zero offset within the first page,
289767feec50SStephen McConnell * then the first PRP element will contain a non-zero offset indicating where the
289867feec50SStephen McConnell * region begins within the page. The last memory segment may end before the end
289967feec50SStephen McConnell * of the PAGE_SIZE segment, depending upon the overall size of the memory being
290067feec50SStephen McConnell * described by the PRP list.
290167feec50SStephen McConnell *
290267feec50SStephen McConnell * Since PRP entries lack any indication of size, the overall data buffer length
290367feec50SStephen McConnell * is used to determine where the end of the data memory buffer is located, and
290467feec50SStephen McConnell * how many PRP entries are required to describe it.
290567feec50SStephen McConnell *
290667feec50SStephen McConnell * Returns nothing.
290767feec50SStephen McConnell */
290867feec50SStephen McConnell void
mpr_build_nvme_prp(struct mpr_softc * sc,struct mpr_command * cm,Mpi26NVMeEncapsulatedRequest_t * nvme_encap_request,void * data,uint32_t data_in_sz,uint32_t data_out_sz)290967feec50SStephen McConnell mpr_build_nvme_prp(struct mpr_softc *sc, struct mpr_command *cm,
291067feec50SStephen McConnell     Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request, void *data,
291167feec50SStephen McConnell     uint32_t data_in_sz, uint32_t data_out_sz)
291267feec50SStephen McConnell {
291367feec50SStephen McConnell 	int			prp_size = PRP_ENTRY_SIZE;
291467feec50SStephen McConnell 	uint64_t		*prp_entry, *prp1_entry, *prp2_entry;
291567feec50SStephen McConnell 	uint64_t		*prp_entry_phys, *prp_page, *prp_page_phys;
291667feec50SStephen McConnell 	uint32_t		offset, entry_len, page_mask_result, page_mask;
291767feec50SStephen McConnell 	bus_addr_t		paddr;
291867feec50SStephen McConnell 	size_t			length;
291967feec50SStephen McConnell 	struct mpr_prp_page	*prp_page_info = NULL;
292067feec50SStephen McConnell 
292167feec50SStephen McConnell 	/*
292267feec50SStephen McConnell 	 * Not all commands require a data transfer. If no data, just return
292367feec50SStephen McConnell 	 * without constructing any PRP.
292467feec50SStephen McConnell 	 */
292567feec50SStephen McConnell 	if (!data_in_sz && !data_out_sz)
292667feec50SStephen McConnell 		return;
292767feec50SStephen McConnell 
292867feec50SStephen McConnell 	/*
292967feec50SStephen McConnell 	 * Set pointers to PRP1 and PRP2, which are in the NVMe command. PRP1 is
293067feec50SStephen McConnell 	 * located at a 24 byte offset from the start of the NVMe command. Then
293167feec50SStephen McConnell 	 * set the current PRP entry pointer to PRP1.
293267feec50SStephen McConnell 	 */
293367feec50SStephen McConnell 	prp1_entry = (uint64_t *)(nvme_encap_request->NVMe_Command +
293467feec50SStephen McConnell 	    NVME_CMD_PRP1_OFFSET);
293567feec50SStephen McConnell 	prp2_entry = (uint64_t *)(nvme_encap_request->NVMe_Command +
293667feec50SStephen McConnell 	    NVME_CMD_PRP2_OFFSET);
293767feec50SStephen McConnell 	prp_entry = prp1_entry;
293867feec50SStephen McConnell 
293967feec50SStephen McConnell 	/*
294067feec50SStephen McConnell 	 * For the PRP entries, use the specially allocated buffer of
294167feec50SStephen McConnell 	 * contiguous memory. PRP Page allocation failures should not happen
294267feec50SStephen McConnell 	 * because there should be enough PRP page buffers to account for the
294367feec50SStephen McConnell 	 * possible NVMe QDepth.
294467feec50SStephen McConnell 	 */
294567feec50SStephen McConnell 	prp_page_info = mpr_alloc_prp_page(sc);
294667feec50SStephen McConnell 	KASSERT(prp_page_info != NULL, ("%s: There are no PRP Pages left to be "
294767feec50SStephen McConnell 	    "used for building a native NVMe SGL.\n", __func__));
294867feec50SStephen McConnell 	prp_page = (uint64_t *)prp_page_info->prp_page;
294967feec50SStephen McConnell 	prp_page_phys = (uint64_t *)(uintptr_t)prp_page_info->prp_page_busaddr;
295067feec50SStephen McConnell 
295167feec50SStephen McConnell 	/*
295267feec50SStephen McConnell 	 * Insert the allocated PRP page into the command's PRP page list. This
295367feec50SStephen McConnell 	 * will be freed when the command is freed.
295467feec50SStephen McConnell 	 */
295567feec50SStephen McConnell 	TAILQ_INSERT_TAIL(&cm->cm_prp_page_list, prp_page_info, prp_page_link);
295667feec50SStephen McConnell 
295767feec50SStephen McConnell 	/*
295867feec50SStephen McConnell 	 * Check if we are within 1 entry of a page boundary we don't want our
295967feec50SStephen McConnell 	 * first entry to be a PRP List entry.
296067feec50SStephen McConnell 	 */
296167feec50SStephen McConnell 	page_mask = PAGE_SIZE - 1;
296267feec50SStephen McConnell 	page_mask_result = (uintptr_t)((uint8_t *)prp_page + prp_size) &
296367feec50SStephen McConnell 	    page_mask;
296467feec50SStephen McConnell 	if (!page_mask_result)
296567feec50SStephen McConnell 	{
296667feec50SStephen McConnell 		/* Bump up to next page boundary. */
296767feec50SStephen McConnell 		prp_page = (uint64_t *)((uint8_t *)prp_page + prp_size);
296867feec50SStephen McConnell 		prp_page_phys = (uint64_t *)((uint8_t *)prp_page_phys +
296967feec50SStephen McConnell 		    prp_size);
297067feec50SStephen McConnell 	}
297167feec50SStephen McConnell 
297267feec50SStephen McConnell 	/*
297367feec50SStephen McConnell 	 * Set PRP physical pointer, which initially points to the current PRP
297467feec50SStephen McConnell 	 * DMA memory page.
297567feec50SStephen McConnell 	 */
297667feec50SStephen McConnell 	prp_entry_phys = prp_page_phys;
297767feec50SStephen McConnell 
297867feec50SStephen McConnell 	/* Get physical address and length of the data buffer. */
297977baa225SJustin Hibbits 	paddr = (bus_addr_t)(uintptr_t)data;
298067feec50SStephen McConnell 	if (data_in_sz)
298167feec50SStephen McConnell 		length = data_in_sz;
298267feec50SStephen McConnell 	else
298367feec50SStephen McConnell 		length = data_out_sz;
298467feec50SStephen McConnell 
298567feec50SStephen McConnell 	/* Loop while the length is not zero. */
298667feec50SStephen McConnell 	while (length)
298767feec50SStephen McConnell 	{
298867feec50SStephen McConnell 		/*
298967feec50SStephen McConnell 		 * Check if we need to put a list pointer here if we are at page
299067feec50SStephen McConnell 		 * boundary - prp_size (8 bytes).
299167feec50SStephen McConnell 		 */
299267feec50SStephen McConnell 		page_mask_result = (uintptr_t)((uint8_t *)prp_entry_phys +
299367feec50SStephen McConnell 		    prp_size) & page_mask;
299467feec50SStephen McConnell 		if (!page_mask_result)
299567feec50SStephen McConnell 		{
299667feec50SStephen McConnell 			/*
299767feec50SStephen McConnell 			 * This is the last entry in a PRP List, so we need to
299867feec50SStephen McConnell 			 * put a PRP list pointer here. What this does is:
299967feec50SStephen McConnell 			 *   - bump the current memory pointer to the next
300067feec50SStephen McConnell 			 *     address, which will be the next full page.
300167feec50SStephen McConnell 			 *   - set the PRP Entry to point to that page. This is
300267feec50SStephen McConnell 			 *     now the PRP List pointer.
300367feec50SStephen McConnell 			 *   - bump the PRP Entry pointer the start of the next
300467feec50SStephen McConnell 			 *     page. Since all of this PRP memory is contiguous,
300567feec50SStephen McConnell 			 *     no need to get a new page - it's just the next
300667feec50SStephen McConnell 			 *     address.
300767feec50SStephen McConnell 			 */
300867feec50SStephen McConnell 			prp_entry_phys++;
300967feec50SStephen McConnell 			*prp_entry =
301067feec50SStephen McConnell 			    htole64((uint64_t)(uintptr_t)prp_entry_phys);
301167feec50SStephen McConnell 			prp_entry++;
301267feec50SStephen McConnell 		}
301367feec50SStephen McConnell 
301467feec50SStephen McConnell 		/* Need to handle if entry will be part of a page. */
301567feec50SStephen McConnell 		offset = (uint32_t)paddr & page_mask;
301667feec50SStephen McConnell 		entry_len = PAGE_SIZE - offset;
301767feec50SStephen McConnell 
301867feec50SStephen McConnell 		if (prp_entry == prp1_entry)
301967feec50SStephen McConnell 		{
302067feec50SStephen McConnell 			/*
302167feec50SStephen McConnell 			 * Must fill in the first PRP pointer (PRP1) before
302267feec50SStephen McConnell 			 * moving on.
302367feec50SStephen McConnell 			 */
302467feec50SStephen McConnell 			*prp1_entry = htole64((uint64_t)paddr);
302567feec50SStephen McConnell 
302667feec50SStephen McConnell 			/*
302767feec50SStephen McConnell 			 * Now point to the second PRP entry within the
302867feec50SStephen McConnell 			 * command (PRP2).
302967feec50SStephen McConnell 			 */
303067feec50SStephen McConnell 			prp_entry = prp2_entry;
303167feec50SStephen McConnell 		}
303267feec50SStephen McConnell 		else if (prp_entry == prp2_entry)
303367feec50SStephen McConnell 		{
303467feec50SStephen McConnell 			/*
303567feec50SStephen McConnell 			 * Should the PRP2 entry be a PRP List pointer or just a
303667feec50SStephen McConnell 			 * regular PRP pointer? If there is more than one more
303767feec50SStephen McConnell 			 * page of data, must use a PRP List pointer.
303867feec50SStephen McConnell 			 */
303967feec50SStephen McConnell 			if (length > PAGE_SIZE)
304067feec50SStephen McConnell 			{
304167feec50SStephen McConnell 				/*
304267feec50SStephen McConnell 				 * PRP2 will contain a PRP List pointer because
304367feec50SStephen McConnell 				 * more PRP's are needed with this command. The
304467feec50SStephen McConnell 				 * list will start at the beginning of the
304567feec50SStephen McConnell 				 * contiguous buffer.
304667feec50SStephen McConnell 				 */
304767feec50SStephen McConnell 				*prp2_entry =
304867feec50SStephen McConnell 				    htole64(
304967feec50SStephen McConnell 				    (uint64_t)(uintptr_t)prp_entry_phys);
305067feec50SStephen McConnell 
305167feec50SStephen McConnell 				/*
305267feec50SStephen McConnell 				 * The next PRP Entry will be the start of the
305367feec50SStephen McConnell 				 * first PRP List.
305467feec50SStephen McConnell 				 */
305567feec50SStephen McConnell 				prp_entry = prp_page;
305667feec50SStephen McConnell 			}
305767feec50SStephen McConnell 			else
305867feec50SStephen McConnell 			{
305967feec50SStephen McConnell 				/*
306067feec50SStephen McConnell 				 * After this, the PRP Entries are complete.
306167feec50SStephen McConnell 				 * This command uses 2 PRP's and no PRP list.
306267feec50SStephen McConnell 				 */
306367feec50SStephen McConnell 				*prp2_entry = htole64((uint64_t)paddr);
306467feec50SStephen McConnell 			}
306567feec50SStephen McConnell 		}
306667feec50SStephen McConnell 		else
306767feec50SStephen McConnell 		{
306867feec50SStephen McConnell 			/*
306967feec50SStephen McConnell 			 * Put entry in list and bump the addresses.
307067feec50SStephen McConnell 			 *
307167feec50SStephen McConnell 			 * After PRP1 and PRP2 are filled in, this will fill in
307267feec50SStephen McConnell 			 * all remaining PRP entries in a PRP List, one per each
307367feec50SStephen McConnell 			 * time through the loop.
307467feec50SStephen McConnell 			 */
307567feec50SStephen McConnell 			*prp_entry = htole64((uint64_t)paddr);
307667feec50SStephen McConnell 			prp_entry++;
307767feec50SStephen McConnell 			prp_entry_phys++;
307867feec50SStephen McConnell 		}
307967feec50SStephen McConnell 
308067feec50SStephen McConnell 		/*
308167feec50SStephen McConnell 		 * Bump the phys address of the command's data buffer by the
308267feec50SStephen McConnell 		 * entry_len.
308367feec50SStephen McConnell 		 */
308467feec50SStephen McConnell 		paddr += entry_len;
308567feec50SStephen McConnell 
308667feec50SStephen McConnell 		/* Decrement length accounting for last partial page. */
308767feec50SStephen McConnell 		if (entry_len > length)
308867feec50SStephen McConnell 			length = 0;
308967feec50SStephen McConnell 		else
309067feec50SStephen McConnell 			length -= entry_len;
309167feec50SStephen McConnell 	}
309267feec50SStephen McConnell }
309367feec50SStephen McConnell 
309467feec50SStephen McConnell /*
309567feec50SStephen McConnell  * mpr_check_pcie_native_sgl - This function is called for PCIe end devices to
309667feec50SStephen McConnell  * determine if the driver needs to build a native SGL. If so, that native SGL
309767feec50SStephen McConnell  * is built in the contiguous buffers allocated especially for PCIe SGL
309867feec50SStephen McConnell  * creation. If the driver will not build a native SGL, return TRUE and a
309967feec50SStephen McConnell  * normal IEEE SGL will be built. Currently this routine supports NVMe devices
310067feec50SStephen McConnell  * only.
310167feec50SStephen McConnell  *
310267feec50SStephen McConnell  * Returns FALSE (0) if native SGL was built, TRUE (1) if no SGL was built.
310367feec50SStephen McConnell  */
310467feec50SStephen McConnell static int
mpr_check_pcie_native_sgl(struct mpr_softc * sc,struct mpr_command * cm,bus_dma_segment_t * segs,int segs_left)310567feec50SStephen McConnell mpr_check_pcie_native_sgl(struct mpr_softc *sc, struct mpr_command *cm,
310667feec50SStephen McConnell     bus_dma_segment_t *segs, int segs_left)
310767feec50SStephen McConnell {
310867feec50SStephen McConnell 	uint32_t		i, sge_dwords, length, offset, entry_len;
310967feec50SStephen McConnell 	uint32_t		num_entries, buff_len = 0, sges_in_segment;
311067feec50SStephen McConnell 	uint32_t		page_mask, page_mask_result, *curr_buff;
311167feec50SStephen McConnell 	uint32_t		*ptr_sgl, *ptr_first_sgl, first_page_offset;
311267feec50SStephen McConnell 	uint32_t		first_page_data_size, end_residual;
311367feec50SStephen McConnell 	uint64_t		*msg_phys;
311467feec50SStephen McConnell 	bus_addr_t		paddr;
311567feec50SStephen McConnell 	int			build_native_sgl = 0, first_prp_entry;
311667feec50SStephen McConnell 	int			prp_size = PRP_ENTRY_SIZE;
311767feec50SStephen McConnell 	Mpi25IeeeSgeChain64_t	*main_chain_element = NULL;
311867feec50SStephen McConnell 	struct mpr_prp_page	*prp_page_info = NULL;
311967feec50SStephen McConnell 
312067feec50SStephen McConnell 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
312167feec50SStephen McConnell 
312267feec50SStephen McConnell 	/*
312367feec50SStephen McConnell 	 * Add up the sizes of each segment length to get the total transfer
312467feec50SStephen McConnell 	 * size, which will be checked against the Maximum Data Transfer Size.
312567feec50SStephen McConnell 	 * If the data transfer length exceeds the MDTS for this device, just
312667feec50SStephen McConnell 	 * return 1 so a normal IEEE SGL will be built. F/W will break the I/O
312767feec50SStephen McConnell 	 * up into multiple I/O's. [nvme_mdts = 0 means unlimited]
312867feec50SStephen McConnell 	 */
312967feec50SStephen McConnell 	for (i = 0; i < segs_left; i++)
313067feec50SStephen McConnell 		buff_len += htole32(segs[i].ds_len);
313167feec50SStephen McConnell 	if ((cm->cm_targ->MDTS > 0) && (buff_len > cm->cm_targ->MDTS))
313267feec50SStephen McConnell 		return 1;
313367feec50SStephen McConnell 
313467feec50SStephen McConnell 	/* Create page_mask (to get offset within page) */
313567feec50SStephen McConnell 	page_mask = PAGE_SIZE - 1;
313667feec50SStephen McConnell 
313767feec50SStephen McConnell 	/*
313867feec50SStephen McConnell 	 * Check if the number of elements exceeds the max number that can be
313967feec50SStephen McConnell 	 * put in the main message frame (H/W can only translate an SGL that
314067feec50SStephen McConnell 	 * is contained entirely in the main message frame).
314167feec50SStephen McConnell 	 */
314296410703SScott Long 	sges_in_segment = (sc->reqframesz -
314367feec50SStephen McConnell 	    offsetof(Mpi25SCSIIORequest_t, SGL)) / sizeof(MPI25_SGE_IO_UNION);
314467feec50SStephen McConnell 	if (segs_left > sges_in_segment)
314567feec50SStephen McConnell 		build_native_sgl = 1;
314667feec50SStephen McConnell 	else
314767feec50SStephen McConnell 	{
314867feec50SStephen McConnell 		/*
314967feec50SStephen McConnell 		 * NVMe uses one PRP for each physical page (or part of physical
315067feec50SStephen McConnell 		 * page).
315167feec50SStephen McConnell 		 *    if 4 pages or less then IEEE is OK
315267feec50SStephen McConnell 		 *    if > 5 pages then we need to build a native SGL
315367feec50SStephen McConnell 		 *    if > 4 and <= 5 pages, then check the physical address of
315467feec50SStephen McConnell 		 *      the first SG entry, then if this first size in the page
315567feec50SStephen McConnell 		 *      is >= the residual beyond 4 pages then use IEEE,
315667feec50SStephen McConnell 		 *      otherwise use native SGL
315767feec50SStephen McConnell 		 */
315867feec50SStephen McConnell 		if (buff_len > (PAGE_SIZE * 5))
315967feec50SStephen McConnell 			build_native_sgl = 1;
316067feec50SStephen McConnell 		else if ((buff_len > (PAGE_SIZE * 4)) &&
316167feec50SStephen McConnell 		    (buff_len <= (PAGE_SIZE * 5)) )
316267feec50SStephen McConnell 		{
316377baa225SJustin Hibbits 			msg_phys = (uint64_t *)(uintptr_t)segs[0].ds_addr;
316467feec50SStephen McConnell 			first_page_offset =
316567feec50SStephen McConnell 			    ((uint32_t)(uint64_t)(uintptr_t)msg_phys &
316667feec50SStephen McConnell 			    page_mask);
316767feec50SStephen McConnell 			first_page_data_size = PAGE_SIZE - first_page_offset;
316867feec50SStephen McConnell 			end_residual = buff_len % PAGE_SIZE;
316967feec50SStephen McConnell 
317067feec50SStephen McConnell 			/*
317167feec50SStephen McConnell 			 * If offset into first page pushes the end of the data
317267feec50SStephen McConnell 			 * beyond end of the 5th page, we need the extra PRP
317367feec50SStephen McConnell 			 * list.
317467feec50SStephen McConnell 			 */
317567feec50SStephen McConnell 			if (first_page_data_size < end_residual)
317667feec50SStephen McConnell 				build_native_sgl = 1;
317767feec50SStephen McConnell 
317867feec50SStephen McConnell 			/*
317967feec50SStephen McConnell 			 * Check if first SG entry size is < residual beyond 4
318067feec50SStephen McConnell 			 * pages.
318167feec50SStephen McConnell 			 */
318267feec50SStephen McConnell 			if (htole32(segs[0].ds_len) <
318367feec50SStephen McConnell 			    (buff_len - (PAGE_SIZE * 4)))
318467feec50SStephen McConnell 				build_native_sgl = 1;
318567feec50SStephen McConnell 		}
318667feec50SStephen McConnell 	}
318767feec50SStephen McConnell 
318867feec50SStephen McConnell 	/* check if native SGL is needed */
318967feec50SStephen McConnell 	if (!build_native_sgl)
319067feec50SStephen McConnell 		return 1;
319167feec50SStephen McConnell 
319267feec50SStephen McConnell 	/*
319367feec50SStephen McConnell 	 * Native SGL is needed.
319467feec50SStephen McConnell 	 * Put a chain element in main message frame that points to the first
319567feec50SStephen McConnell 	 * chain buffer.
319667feec50SStephen McConnell 	 *
319767feec50SStephen McConnell 	 * NOTE:  The ChainOffset field must be 0 when using a chain pointer to
319867feec50SStephen McConnell 	 *        a native SGL.
319967feec50SStephen McConnell 	 */
320067feec50SStephen McConnell 
320167feec50SStephen McConnell 	/* Set main message chain element pointer */
320267feec50SStephen McConnell 	main_chain_element = (pMpi25IeeeSgeChain64_t)cm->cm_sge;
320367feec50SStephen McConnell 
320467feec50SStephen McConnell 	/*
320567feec50SStephen McConnell 	 * For NVMe the chain element needs to be the 2nd SGL entry in the main
320667feec50SStephen McConnell 	 * message.
320767feec50SStephen McConnell 	 */
320867feec50SStephen McConnell 	main_chain_element = (Mpi25IeeeSgeChain64_t *)
320967feec50SStephen McConnell 	    ((uint8_t *)main_chain_element + sizeof(MPI25_IEEE_SGE_CHAIN64));
321067feec50SStephen McConnell 
321167feec50SStephen McConnell 	/*
321267feec50SStephen McConnell 	 * For the PRP entries, use the specially allocated buffer of
321367feec50SStephen McConnell 	 * contiguous memory. PRP Page allocation failures should not happen
321467feec50SStephen McConnell 	 * because there should be enough PRP page buffers to account for the
321567feec50SStephen McConnell 	 * possible NVMe QDepth.
321667feec50SStephen McConnell 	 */
321767feec50SStephen McConnell 	prp_page_info = mpr_alloc_prp_page(sc);
321867feec50SStephen McConnell 	KASSERT(prp_page_info != NULL, ("%s: There are no PRP Pages left to be "
321967feec50SStephen McConnell 	    "used for building a native NVMe SGL.\n", __func__));
322067feec50SStephen McConnell 	curr_buff = (uint32_t *)prp_page_info->prp_page;
322167feec50SStephen McConnell 	msg_phys = (uint64_t *)(uintptr_t)prp_page_info->prp_page_busaddr;
322267feec50SStephen McConnell 
322367feec50SStephen McConnell 	/*
322467feec50SStephen McConnell 	 * Insert the allocated PRP page into the command's PRP page list. This
322567feec50SStephen McConnell 	 * will be freed when the command is freed.
322667feec50SStephen McConnell 	 */
322767feec50SStephen McConnell 	TAILQ_INSERT_TAIL(&cm->cm_prp_page_list, prp_page_info, prp_page_link);
322867feec50SStephen McConnell 
322967feec50SStephen McConnell 	/*
323067feec50SStephen McConnell 	 * Check if we are within 1 entry of a page boundary we don't want our
323167feec50SStephen McConnell 	 * first entry to be a PRP List entry.
323267feec50SStephen McConnell 	 */
323367feec50SStephen McConnell 	page_mask_result = (uintptr_t)((uint8_t *)curr_buff + prp_size) &
323467feec50SStephen McConnell 	    page_mask;
323567feec50SStephen McConnell 	if (!page_mask_result) {
323667feec50SStephen McConnell 		/* Bump up to next page boundary. */
323767feec50SStephen McConnell 		curr_buff = (uint32_t *)((uint8_t *)curr_buff + prp_size);
323867feec50SStephen McConnell 		msg_phys = (uint64_t *)((uint8_t *)msg_phys + prp_size);
323967feec50SStephen McConnell 	}
324067feec50SStephen McConnell 
324167feec50SStephen McConnell 	/* Fill in the chain element and make it an NVMe segment type. */
324267feec50SStephen McConnell 	main_chain_element->Address.High =
324367feec50SStephen McConnell 	    htole32((uint32_t)((uint64_t)(uintptr_t)msg_phys >> 32));
324467feec50SStephen McConnell 	main_chain_element->Address.Low =
324567feec50SStephen McConnell 	    htole32((uint32_t)(uintptr_t)msg_phys);
324667feec50SStephen McConnell 	main_chain_element->NextChainOffset = 0;
324767feec50SStephen McConnell 	main_chain_element->Flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
324867feec50SStephen McConnell 	    MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
324967feec50SStephen McConnell 	    MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
325067feec50SStephen McConnell 
325167feec50SStephen McConnell 	/* Set SGL pointer to start of contiguous PCIe buffer. */
325267feec50SStephen McConnell 	ptr_sgl = curr_buff;
325367feec50SStephen McConnell 	sge_dwords = 2;
325467feec50SStephen McConnell 	num_entries = 0;
325567feec50SStephen McConnell 
325667feec50SStephen McConnell 	/*
325767feec50SStephen McConnell 	 * NVMe has a very convoluted PRP format. One PRP is required for each
325867feec50SStephen McConnell 	 * page or partial page. We need to split up OS SG entries if they are
325967feec50SStephen McConnell 	 * longer than one page or cross a page boundary. We also have to insert
326067feec50SStephen McConnell 	 * a PRP list pointer entry as the last entry in each physical page of
326167feec50SStephen McConnell 	 * the PRP list.
326267feec50SStephen McConnell 	 *
326367feec50SStephen McConnell 	 * NOTE: The first PRP "entry" is actually placed in the first SGL entry
326467feec50SStephen McConnell 	 * in the main message in IEEE 64 format. The 2nd entry in the main
326567feec50SStephen McConnell 	 * message is the chain element, and the rest of the PRP entries are
326667feec50SStephen McConnell 	 * built in the contiguous PCIe buffer.
326767feec50SStephen McConnell 	 */
326867feec50SStephen McConnell 	first_prp_entry = 1;
326967feec50SStephen McConnell 	ptr_first_sgl = (uint32_t *)cm->cm_sge;
327067feec50SStephen McConnell 
327167feec50SStephen McConnell 	for (i = 0; i < segs_left; i++) {
327267feec50SStephen McConnell 		/* Get physical address and length of this SG entry. */
327367feec50SStephen McConnell 		paddr = segs[i].ds_addr;
327467feec50SStephen McConnell 		length = segs[i].ds_len;
327567feec50SStephen McConnell 
327667feec50SStephen McConnell 		/*
327767feec50SStephen McConnell 		 * Check whether a given SGE buffer lies on a non-PAGED
327867feec50SStephen McConnell 		 * boundary if this is not the first page. If so, this is not
327967feec50SStephen McConnell 		 * expected so have FW build the SGL.
328067feec50SStephen McConnell 		 */
3281757ff642SScott Long 		if ((i != 0) && (((uint32_t)paddr & page_mask) != 0)) {
328267feec50SStephen McConnell 			mpr_dprint(sc, MPR_ERROR, "Unaligned SGE while "
328367feec50SStephen McConnell 			    "building NVMe PRPs, low address is 0x%x\n",
328467feec50SStephen McConnell 			    (uint32_t)paddr);
328567feec50SStephen McConnell 			return 1;
328667feec50SStephen McConnell 		}
328767feec50SStephen McConnell 
328867feec50SStephen McConnell 		/* Apart from last SGE, if any other SGE boundary is not page
328967feec50SStephen McConnell 		 * aligned then it means that hole exists. Existence of hole
329067feec50SStephen McConnell 		 * leads to data corruption. So fallback to IEEE SGEs.
329167feec50SStephen McConnell 		 */
329267feec50SStephen McConnell 		if (i != (segs_left - 1)) {
329367feec50SStephen McConnell 			if (((uint32_t)paddr + length) & page_mask) {
329467feec50SStephen McConnell 				mpr_dprint(sc, MPR_ERROR, "Unaligned SGE "
329567feec50SStephen McConnell 				    "boundary while building NVMe PRPs, low "
329667feec50SStephen McConnell 				    "address: 0x%x and length: %u\n",
329767feec50SStephen McConnell 				    (uint32_t)paddr, length);
329867feec50SStephen McConnell 				return 1;
329967feec50SStephen McConnell 			}
330067feec50SStephen McConnell 		}
330167feec50SStephen McConnell 
330267feec50SStephen McConnell 		/* Loop while the length is not zero. */
330367feec50SStephen McConnell 		while (length) {
330467feec50SStephen McConnell 			/*
330567feec50SStephen McConnell 			 * Check if we need to put a list pointer here if we are
330667feec50SStephen McConnell 			 * at page boundary - prp_size.
330767feec50SStephen McConnell 			 */
330867feec50SStephen McConnell 			page_mask_result = (uintptr_t)((uint8_t *)ptr_sgl +
330967feec50SStephen McConnell 			    prp_size) & page_mask;
331067feec50SStephen McConnell 			if (!page_mask_result) {
331167feec50SStephen McConnell 				/*
331267feec50SStephen McConnell 				 * Need to put a PRP list pointer here.
331367feec50SStephen McConnell 				 */
331467feec50SStephen McConnell 				msg_phys = (uint64_t *)((uint8_t *)msg_phys +
331567feec50SStephen McConnell 				    prp_size);
331667feec50SStephen McConnell 				*ptr_sgl = htole32((uintptr_t)msg_phys);
331767feec50SStephen McConnell 				*(ptr_sgl+1) = htole32((uint64_t)(uintptr_t)
331867feec50SStephen McConnell 				    msg_phys >> 32);
331967feec50SStephen McConnell 				ptr_sgl += sge_dwords;
332067feec50SStephen McConnell 				num_entries++;
332167feec50SStephen McConnell 			}
332267feec50SStephen McConnell 
332367feec50SStephen McConnell 			/* Need to handle if entry will be part of a page. */
332467feec50SStephen McConnell 			offset = (uint32_t)paddr & page_mask;
332567feec50SStephen McConnell 			entry_len = PAGE_SIZE - offset;
332667feec50SStephen McConnell 			if (first_prp_entry) {
332767feec50SStephen McConnell 				/*
332867feec50SStephen McConnell 				 * Put IEEE entry in first SGE in main message.
332967feec50SStephen McConnell 				 * (Simple element, System addr, not end of
333067feec50SStephen McConnell 				 * list.)
333167feec50SStephen McConnell 				 */
333267feec50SStephen McConnell 				*ptr_first_sgl = htole32((uint32_t)paddr);
333367feec50SStephen McConnell 				*(ptr_first_sgl + 1) =
333467feec50SStephen McConnell 				    htole32((uint32_t)((uint64_t)paddr >> 32));
333567feec50SStephen McConnell 				*(ptr_first_sgl + 2) = htole32(entry_len);
333667feec50SStephen McConnell 				*(ptr_first_sgl + 3) = 0;
333767feec50SStephen McConnell 
333867feec50SStephen McConnell 				/* No longer the first PRP entry. */
333967feec50SStephen McConnell 				first_prp_entry = 0;
334067feec50SStephen McConnell 			} else {
334167feec50SStephen McConnell 				/* Put entry in list. */
334267feec50SStephen McConnell 				*ptr_sgl = htole32((uint32_t)paddr);
334367feec50SStephen McConnell 				*(ptr_sgl + 1) =
334467feec50SStephen McConnell 				    htole32((uint32_t)((uint64_t)paddr >> 32));
334567feec50SStephen McConnell 
334667feec50SStephen McConnell 				/* Bump ptr_sgl, msg_phys, and num_entries. */
334767feec50SStephen McConnell 				ptr_sgl += sge_dwords;
334867feec50SStephen McConnell 				msg_phys = (uint64_t *)((uint8_t *)msg_phys +
334967feec50SStephen McConnell 				    prp_size);
335067feec50SStephen McConnell 				num_entries++;
335167feec50SStephen McConnell 			}
335267feec50SStephen McConnell 
335367feec50SStephen McConnell 			/* Bump the phys address by the entry_len. */
335467feec50SStephen McConnell 			paddr += entry_len;
335567feec50SStephen McConnell 
335667feec50SStephen McConnell 			/* Decrement length accounting for last partial page. */
335767feec50SStephen McConnell 			if (entry_len > length)
335867feec50SStephen McConnell 				length = 0;
335967feec50SStephen McConnell 			else
336067feec50SStephen McConnell 				length -= entry_len;
336167feec50SStephen McConnell 		}
336267feec50SStephen McConnell 	}
336367feec50SStephen McConnell 
336467feec50SStephen McConnell 	/* Set chain element Length. */
336567feec50SStephen McConnell 	main_chain_element->Length = htole32(num_entries * prp_size);
336667feec50SStephen McConnell 
336767feec50SStephen McConnell 	/* Return 0, indicating we built a native SGL. */
336867feec50SStephen McConnell 	return 0;
336967feec50SStephen McConnell }
337067feec50SStephen McConnell 
3371991554f2SKenneth D. Merry /*
3372991554f2SKenneth D. Merry  * Add a chain element as the next SGE for the specified command.
3373991554f2SKenneth D. Merry  * Reset cm_sge and cm_sgesize to indicate all the available space. Chains are
3374991554f2SKenneth D. Merry  * only required for IEEE commands.  Therefore there is no code for commands
3375a2c14879SStephen McConnell  * that have the MPR_CM_FLAGS_SGE_SIMPLE flag set (and those commands
3376a2c14879SStephen McConnell  * shouldn't be requesting chains).
3377991554f2SKenneth D. Merry  */
3378991554f2SKenneth D. Merry static int
mpr_add_chain(struct mpr_command * cm,int segsleft)3379991554f2SKenneth D. Merry mpr_add_chain(struct mpr_command *cm, int segsleft)
3380991554f2SKenneth D. Merry {
3381991554f2SKenneth D. Merry 	struct mpr_softc *sc = cm->cm_sc;
3382991554f2SKenneth D. Merry 	MPI2_REQUEST_HEADER *req;
3383991554f2SKenneth D. Merry 	MPI25_IEEE_SGE_CHAIN64 *ieee_sgc;
3384991554f2SKenneth D. Merry 	struct mpr_chain *chain;
33852bbc5fcbSStephen McConnell 	int sgc_size, current_segs, rem_segs, segs_per_frame;
3386991554f2SKenneth D. Merry 	uint8_t next_chain_offset = 0;
3387991554f2SKenneth D. Merry 
3388991554f2SKenneth D. Merry 	/*
3389991554f2SKenneth D. Merry 	 * Fail if a command is requesting a chain for SIMPLE SGE's.  For SAS3
3390991554f2SKenneth D. Merry 	 * only IEEE commands should be requesting chains.  Return some error
3391991554f2SKenneth D. Merry 	 * code other than 0.
3392991554f2SKenneth D. Merry 	 */
3393991554f2SKenneth D. Merry 	if (cm->cm_flags & MPR_CM_FLAGS_SGE_SIMPLE) {
3394991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "A chain element cannot be added to "
3395991554f2SKenneth D. Merry 		    "an MPI SGL.\n");
3396991554f2SKenneth D. Merry 		return(ENOBUFS);
3397991554f2SKenneth D. Merry 	}
3398991554f2SKenneth D. Merry 
3399991554f2SKenneth D. Merry 	sgc_size = sizeof(MPI25_IEEE_SGE_CHAIN64);
3400991554f2SKenneth D. Merry 	if (cm->cm_sglsize < sgc_size)
3401991554f2SKenneth D. Merry 		panic("MPR: Need SGE Error Code\n");
3402991554f2SKenneth D. Merry 
3403991554f2SKenneth D. Merry 	chain = mpr_alloc_chain(cm->cm_sc);
3404991554f2SKenneth D. Merry 	if (chain == NULL)
3405991554f2SKenneth D. Merry 		return (ENOBUFS);
3406991554f2SKenneth D. Merry 
3407991554f2SKenneth D. Merry 	/*
3408991554f2SKenneth D. Merry 	 * Note: a double-linked list is used to make it easier to walk for
3409991554f2SKenneth D. Merry 	 * debugging.
3410991554f2SKenneth D. Merry 	 */
3411991554f2SKenneth D. Merry 	TAILQ_INSERT_TAIL(&cm->cm_chain_list, chain, chain_link);
3412991554f2SKenneth D. Merry 
3413991554f2SKenneth D. Merry 	/*
3414991554f2SKenneth D. Merry 	 * Need to know if the number of frames left is more than 1 or not.  If
3415991554f2SKenneth D. Merry 	 * more than 1 frame is required, NextChainOffset will need to be set,
3416991554f2SKenneth D. Merry 	 * which will just be the last segment of the frame.
3417991554f2SKenneth D. Merry 	 */
3418991554f2SKenneth D. Merry 	rem_segs = 0;
3419991554f2SKenneth D. Merry 	if (cm->cm_sglsize < (sgc_size * segsleft)) {
3420991554f2SKenneth D. Merry 		/*
3421ddeb702fSGordon Bergling 		 * rem_segs is the number of segment remaining after the
3422991554f2SKenneth D. Merry 		 * segments that will go into the current frame.  Since it is
3423991554f2SKenneth D. Merry 		 * known that at least one more frame is required, account for
3424991554f2SKenneth D. Merry 		 * the chain element.  To know if more than one more frame is
3425991554f2SKenneth D. Merry 		 * required, just check if there will be a remainder after using
3426991554f2SKenneth D. Merry 		 * the current frame (with this chain) and the next frame.  If
3427991554f2SKenneth D. Merry 		 * so the NextChainOffset must be the last element of the next
3428991554f2SKenneth D. Merry 		 * frame.
3429991554f2SKenneth D. Merry 		 */
3430991554f2SKenneth D. Merry 		current_segs = (cm->cm_sglsize / sgc_size) - 1;
3431991554f2SKenneth D. Merry 		rem_segs = segsleft - current_segs;
34322bbc5fcbSStephen McConnell 		segs_per_frame = sc->chain_frame_size / sgc_size;
3433991554f2SKenneth D. Merry 		if (rem_segs > segs_per_frame) {
3434991554f2SKenneth D. Merry 			next_chain_offset = segs_per_frame - 1;
3435991554f2SKenneth D. Merry 		}
3436991554f2SKenneth D. Merry 	}
3437991554f2SKenneth D. Merry 	ieee_sgc = &((MPI25_SGE_IO_UNION *)cm->cm_sge)->IeeeChain;
34382bbc5fcbSStephen McConnell 	ieee_sgc->Length = next_chain_offset ?
34392bbc5fcbSStephen McConnell 	    htole32((uint32_t)sc->chain_frame_size) :
3440991554f2SKenneth D. Merry 	    htole32((uint32_t)rem_segs * (uint32_t)sgc_size);
3441991554f2SKenneth D. Merry 	ieee_sgc->NextChainOffset = next_chain_offset;
3442991554f2SKenneth D. Merry 	ieee_sgc->Flags = (MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
3443991554f2SKenneth D. Merry 	    MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR);
3444991554f2SKenneth D. Merry 	ieee_sgc->Address.Low = htole32(chain->chain_busaddr);
3445991554f2SKenneth D. Merry 	ieee_sgc->Address.High = htole32(chain->chain_busaddr >> 32);
3446991554f2SKenneth D. Merry 	cm->cm_sge = &((MPI25_SGE_IO_UNION *)chain->chain)->IeeeSimple;
3447991554f2SKenneth D. Merry 	req = (MPI2_REQUEST_HEADER *)cm->cm_req;
34482bbc5fcbSStephen McConnell 	req->ChainOffset = (sc->chain_frame_size - sgc_size) >> 4;
3449991554f2SKenneth D. Merry 
34502bbc5fcbSStephen McConnell 	cm->cm_sglsize = sc->chain_frame_size;
3451991554f2SKenneth D. Merry 	return (0);
3452991554f2SKenneth D. Merry }
3453991554f2SKenneth D. Merry 
3454991554f2SKenneth D. Merry /*
3455991554f2SKenneth D. Merry  * Add one scatter-gather element to the scatter-gather list for a command.
3456a2c14879SStephen McConnell  * Maintain cm_sglsize and cm_sge as the remaining size and pointer to the
3457a2c14879SStephen McConnell  * next SGE to fill in, respectively.  In Gen3, the MPI SGL does not have a
3458a2c14879SStephen McConnell  * chain, so don't consider any chain additions.
3459991554f2SKenneth D. Merry  */
3460991554f2SKenneth D. Merry int
mpr_push_sge(struct mpr_command * cm,MPI2_SGE_SIMPLE64 * sge,size_t len,int segsleft)3461991554f2SKenneth D. Merry mpr_push_sge(struct mpr_command *cm, MPI2_SGE_SIMPLE64 *sge, size_t len,
3462991554f2SKenneth D. Merry     int segsleft)
3463991554f2SKenneth D. Merry {
3464991554f2SKenneth D. Merry 	uint32_t saved_buf_len, saved_address_low, saved_address_high;
3465991554f2SKenneth D. Merry 	u32 sge_flags;
3466991554f2SKenneth D. Merry 
3467991554f2SKenneth D. Merry 	/*
3468991554f2SKenneth D. Merry 	 * case 1: >=1 more segment, no room for anything (error)
3469991554f2SKenneth D. Merry 	 * case 2: 1 more segment and enough room for it
3470991554f2SKenneth D. Merry          */
3471991554f2SKenneth D. Merry 
3472991554f2SKenneth D. Merry 	if (cm->cm_sglsize < (segsleft * sizeof(MPI2_SGE_SIMPLE64))) {
3473991554f2SKenneth D. Merry 		mpr_dprint(cm->cm_sc, MPR_ERROR,
3474991554f2SKenneth D. Merry 		    "%s: warning: Not enough room for MPI SGL in frame.\n",
3475991554f2SKenneth D. Merry 		    __func__);
3476991554f2SKenneth D. Merry 		return(ENOBUFS);
3477991554f2SKenneth D. Merry 	}
3478991554f2SKenneth D. Merry 
3479991554f2SKenneth D. Merry 	KASSERT(segsleft == 1,
3480991554f2SKenneth D. Merry 	    ("segsleft cannot be more than 1 for an MPI SGL; segsleft = %d\n",
3481991554f2SKenneth D. Merry 	    segsleft));
3482991554f2SKenneth D. Merry 
3483991554f2SKenneth D. Merry 	/*
3484991554f2SKenneth D. Merry 	 * There is one more segment left to add for the MPI SGL and there is
3485991554f2SKenneth D. Merry 	 * enough room in the frame to add it.  This is the normal case because
3486991554f2SKenneth D. Merry 	 * MPI SGL's don't have chains, otherwise something is wrong.
3487991554f2SKenneth D. Merry 	 *
3488991554f2SKenneth D. Merry 	 * If this is a bi-directional request, need to account for that
3489991554f2SKenneth D. Merry 	 * here.  Save the pre-filled sge values.  These will be used
3490991554f2SKenneth D. Merry 	 * either for the 2nd SGL or for a single direction SGL.  If
3491991554f2SKenneth D. Merry 	 * cm_out_len is non-zero, this is a bi-directional request, so
3492991554f2SKenneth D. Merry 	 * fill in the OUT SGL first, then the IN SGL, otherwise just
3493991554f2SKenneth D. Merry 	 * fill in the IN SGL.  Note that at this time, when filling in
3494991554f2SKenneth D. Merry 	 * 2 SGL's for a bi-directional request, they both use the same
3495991554f2SKenneth D. Merry 	 * DMA buffer (same cm command).
3496991554f2SKenneth D. Merry 	 */
3497991554f2SKenneth D. Merry 	saved_buf_len = sge->FlagsLength & 0x00FFFFFF;
3498991554f2SKenneth D. Merry 	saved_address_low = sge->Address.Low;
3499991554f2SKenneth D. Merry 	saved_address_high = sge->Address.High;
3500991554f2SKenneth D. Merry 	if (cm->cm_out_len) {
3501991554f2SKenneth D. Merry 		sge->FlagsLength = cm->cm_out_len |
3502991554f2SKenneth D. Merry 		    ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
3503991554f2SKenneth D. Merry 		    MPI2_SGE_FLAGS_END_OF_BUFFER |
3504991554f2SKenneth D. Merry 		    MPI2_SGE_FLAGS_HOST_TO_IOC |
3505991554f2SKenneth D. Merry 		    MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
3506991554f2SKenneth D. Merry 		    MPI2_SGE_FLAGS_SHIFT);
3507991554f2SKenneth D. Merry 		cm->cm_sglsize -= len;
3508991554f2SKenneth D. Merry 		/* Endian Safe code */
3509991554f2SKenneth D. Merry 		sge_flags = sge->FlagsLength;
3510991554f2SKenneth D. Merry 		sge->FlagsLength = htole32(sge_flags);
3511991554f2SKenneth D. Merry 		bcopy(sge, cm->cm_sge, len);
3512991554f2SKenneth D. Merry 		cm->cm_sge = (MPI2_SGE_IO_UNION *)((uintptr_t)cm->cm_sge + len);
3513991554f2SKenneth D. Merry 	}
3514991554f2SKenneth D. Merry 	sge->FlagsLength = saved_buf_len |
3515991554f2SKenneth D. Merry 	    ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
3516991554f2SKenneth D. Merry 	    MPI2_SGE_FLAGS_END_OF_BUFFER |
3517991554f2SKenneth D. Merry 	    MPI2_SGE_FLAGS_LAST_ELEMENT |
3518991554f2SKenneth D. Merry 	    MPI2_SGE_FLAGS_END_OF_LIST |
3519991554f2SKenneth D. Merry 	    MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
3520991554f2SKenneth D. Merry 	    MPI2_SGE_FLAGS_SHIFT);
3521991554f2SKenneth D. Merry 	if (cm->cm_flags & MPR_CM_FLAGS_DATAIN) {
3522991554f2SKenneth D. Merry 		sge->FlagsLength |=
3523991554f2SKenneth D. Merry 		    ((uint32_t)(MPI2_SGE_FLAGS_IOC_TO_HOST) <<
3524991554f2SKenneth D. Merry 		    MPI2_SGE_FLAGS_SHIFT);
3525991554f2SKenneth D. Merry 	} else {
3526991554f2SKenneth D. Merry 		sge->FlagsLength |=
3527991554f2SKenneth D. Merry 		    ((uint32_t)(MPI2_SGE_FLAGS_HOST_TO_IOC) <<
3528991554f2SKenneth D. Merry 		    MPI2_SGE_FLAGS_SHIFT);
3529991554f2SKenneth D. Merry 	}
3530991554f2SKenneth D. Merry 	sge->Address.Low = saved_address_low;
3531991554f2SKenneth D. Merry 	sge->Address.High = saved_address_high;
3532991554f2SKenneth D. Merry 
3533991554f2SKenneth D. Merry 	cm->cm_sglsize -= len;
3534991554f2SKenneth D. Merry 	/* Endian Safe code */
3535991554f2SKenneth D. Merry 	sge_flags = sge->FlagsLength;
3536991554f2SKenneth D. Merry 	sge->FlagsLength = htole32(sge_flags);
3537991554f2SKenneth D. Merry 	bcopy(sge, cm->cm_sge, len);
3538991554f2SKenneth D. Merry 	cm->cm_sge = (MPI2_SGE_IO_UNION *)((uintptr_t)cm->cm_sge + len);
3539991554f2SKenneth D. Merry 	return (0);
3540991554f2SKenneth D. Merry }
3541991554f2SKenneth D. Merry 
3542991554f2SKenneth D. Merry /*
3543991554f2SKenneth D. Merry  * Add one IEEE scatter-gather element (chain or simple) to the IEEE scatter-
3544991554f2SKenneth D. Merry  * gather list for a command.  Maintain cm_sglsize and cm_sge as the
3545991554f2SKenneth D. Merry  * remaining size and pointer to the next SGE to fill in, respectively.
3546991554f2SKenneth D. Merry  */
3547991554f2SKenneth D. Merry int
mpr_push_ieee_sge(struct mpr_command * cm,void * sgep,int segsleft)3548991554f2SKenneth D. Merry mpr_push_ieee_sge(struct mpr_command *cm, void *sgep, int segsleft)
3549991554f2SKenneth D. Merry {
3550991554f2SKenneth D. Merry 	MPI2_IEEE_SGE_SIMPLE64 *sge = sgep;
3551991554f2SKenneth D. Merry 	int error, ieee_sge_size = sizeof(MPI25_SGE_IO_UNION);
3552991554f2SKenneth D. Merry 	uint32_t saved_buf_len, saved_address_low, saved_address_high;
3553991554f2SKenneth D. Merry 	uint32_t sge_length;
3554991554f2SKenneth D. Merry 
3555991554f2SKenneth D. Merry 	/*
3556991554f2SKenneth D. Merry 	 * case 1: No room for chain or segment (error).
3557991554f2SKenneth D. Merry 	 * case 2: Two or more segments left but only room for chain.
3558991554f2SKenneth D. Merry 	 * case 3: Last segment and room for it, so set flags.
3559991554f2SKenneth D. Merry 	 */
3560991554f2SKenneth D. Merry 
3561991554f2SKenneth D. Merry 	/*
3562991554f2SKenneth D. Merry 	 * There should be room for at least one element, or there is a big
3563991554f2SKenneth D. Merry 	 * problem.
3564991554f2SKenneth D. Merry 	 */
3565991554f2SKenneth D. Merry 	if (cm->cm_sglsize < ieee_sge_size)
3566991554f2SKenneth D. Merry 		panic("MPR: Need SGE Error Code\n");
3567991554f2SKenneth D. Merry 
3568991554f2SKenneth D. Merry 	if ((segsleft >= 2) && (cm->cm_sglsize < (ieee_sge_size * 2))) {
3569991554f2SKenneth D. Merry 		if ((error = mpr_add_chain(cm, segsleft)) != 0)
3570991554f2SKenneth D. Merry 			return (error);
3571991554f2SKenneth D. Merry 	}
3572991554f2SKenneth D. Merry 
3573991554f2SKenneth D. Merry 	if (segsleft == 1) {
3574991554f2SKenneth D. Merry 		/*
3575991554f2SKenneth D. Merry 		 * If this is a bi-directional request, need to account for that
3576991554f2SKenneth D. Merry 		 * here.  Save the pre-filled sge values.  These will be used
3577991554f2SKenneth D. Merry 		 * either for the 2nd SGL or for a single direction SGL.  If
3578991554f2SKenneth D. Merry 		 * cm_out_len is non-zero, this is a bi-directional request, so
3579991554f2SKenneth D. Merry 		 * fill in the OUT SGL first, then the IN SGL, otherwise just
3580991554f2SKenneth D. Merry 		 * fill in the IN SGL.  Note that at this time, when filling in
3581991554f2SKenneth D. Merry 		 * 2 SGL's for a bi-directional request, they both use the same
3582991554f2SKenneth D. Merry 		 * DMA buffer (same cm command).
3583991554f2SKenneth D. Merry 		 */
3584991554f2SKenneth D. Merry 		saved_buf_len = sge->Length;
3585991554f2SKenneth D. Merry 		saved_address_low = sge->Address.Low;
3586991554f2SKenneth D. Merry 		saved_address_high = sge->Address.High;
3587991554f2SKenneth D. Merry 		if (cm->cm_out_len) {
3588991554f2SKenneth D. Merry 			sge->Length = cm->cm_out_len;
3589991554f2SKenneth D. Merry 			sge->Flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
3590991554f2SKenneth D. Merry 			    MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR);
3591991554f2SKenneth D. Merry 			cm->cm_sglsize -= ieee_sge_size;
3592991554f2SKenneth D. Merry 			/* Endian Safe code */
3593991554f2SKenneth D. Merry 			sge_length = sge->Length;
3594991554f2SKenneth D. Merry 			sge->Length = htole32(sge_length);
3595991554f2SKenneth D. Merry 			bcopy(sgep, cm->cm_sge, ieee_sge_size);
3596991554f2SKenneth D. Merry 			cm->cm_sge =
3597991554f2SKenneth D. Merry 			    (MPI25_SGE_IO_UNION *)((uintptr_t)cm->cm_sge +
3598991554f2SKenneth D. Merry 			    ieee_sge_size);
3599991554f2SKenneth D. Merry 		}
3600991554f2SKenneth D. Merry 		sge->Length = saved_buf_len;
3601991554f2SKenneth D. Merry 		sge->Flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
3602991554f2SKenneth D. Merry 		    MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
3603991554f2SKenneth D. Merry 		    MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
3604991554f2SKenneth D. Merry 		sge->Address.Low = saved_address_low;
3605991554f2SKenneth D. Merry 		sge->Address.High = saved_address_high;
3606991554f2SKenneth D. Merry 	}
3607991554f2SKenneth D. Merry 
3608991554f2SKenneth D. Merry 	cm->cm_sglsize -= ieee_sge_size;
3609991554f2SKenneth D. Merry 	/* Endian Safe code */
3610991554f2SKenneth D. Merry 	sge_length = sge->Length;
3611991554f2SKenneth D. Merry 	sge->Length = htole32(sge_length);
3612991554f2SKenneth D. Merry 	bcopy(sgep, cm->cm_sge, ieee_sge_size);
3613991554f2SKenneth D. Merry 	cm->cm_sge = (MPI25_SGE_IO_UNION *)((uintptr_t)cm->cm_sge +
3614991554f2SKenneth D. Merry 	    ieee_sge_size);
3615991554f2SKenneth D. Merry 	return (0);
3616991554f2SKenneth D. Merry }
3617991554f2SKenneth D. Merry 
3618991554f2SKenneth D. Merry /*
3619991554f2SKenneth D. Merry  * Add one dma segment to the scatter-gather list for a command.
3620991554f2SKenneth D. Merry  */
3621991554f2SKenneth D. Merry int
mpr_add_dmaseg(struct mpr_command * cm,vm_paddr_t pa,size_t len,u_int flags,int segsleft)3622991554f2SKenneth D. Merry mpr_add_dmaseg(struct mpr_command *cm, vm_paddr_t pa, size_t len, u_int flags,
3623991554f2SKenneth D. Merry     int segsleft)
3624991554f2SKenneth D. Merry {
3625991554f2SKenneth D. Merry 	MPI2_SGE_SIMPLE64 sge;
3626991554f2SKenneth D. Merry 	MPI2_IEEE_SGE_SIMPLE64 ieee_sge;
3627991554f2SKenneth D. Merry 
3628991554f2SKenneth D. Merry 	if (!(cm->cm_flags & MPR_CM_FLAGS_SGE_SIMPLE)) {
3629991554f2SKenneth D. Merry 		ieee_sge.Flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
3630991554f2SKenneth D. Merry 		    MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR);
3631991554f2SKenneth D. Merry 		ieee_sge.Length = len;
3632991554f2SKenneth D. Merry 		mpr_from_u64(pa, &ieee_sge.Address);
3633991554f2SKenneth D. Merry 
3634991554f2SKenneth D. Merry 		return (mpr_push_ieee_sge(cm, &ieee_sge, segsleft));
3635991554f2SKenneth D. Merry 	} else {
3636991554f2SKenneth D. Merry 		/*
3637991554f2SKenneth D. Merry 		 * This driver always uses 64-bit address elements for
3638991554f2SKenneth D. Merry 		 * simplicity.
3639991554f2SKenneth D. Merry 		 */
3640991554f2SKenneth D. Merry 		flags |= MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
3641991554f2SKenneth D. Merry 		    MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
3642991554f2SKenneth D. Merry 		/* Set Endian safe macro in mpr_push_sge */
3643991554f2SKenneth D. Merry 		sge.FlagsLength = len | (flags << MPI2_SGE_FLAGS_SHIFT);
3644991554f2SKenneth D. Merry 		mpr_from_u64(pa, &sge.Address);
3645991554f2SKenneth D. Merry 
3646991554f2SKenneth D. Merry 		return (mpr_push_sge(cm, &sge, sizeof sge, segsleft));
3647991554f2SKenneth D. Merry 	}
3648991554f2SKenneth D. Merry }
3649991554f2SKenneth D. Merry 
3650991554f2SKenneth D. Merry static void
mpr_data_cb(void * arg,bus_dma_segment_t * segs,int nsegs,int error)3651991554f2SKenneth D. Merry mpr_data_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
3652991554f2SKenneth D. Merry {
3653991554f2SKenneth D. Merry 	struct mpr_softc *sc;
3654991554f2SKenneth D. Merry 	struct mpr_command *cm;
3655991554f2SKenneth D. Merry 	u_int i, dir, sflags;
3656991554f2SKenneth D. Merry 
3657991554f2SKenneth D. Merry 	cm = (struct mpr_command *)arg;
3658991554f2SKenneth D. Merry 	sc = cm->cm_sc;
3659991554f2SKenneth D. Merry 
3660991554f2SKenneth D. Merry 	/*
3661991554f2SKenneth D. Merry 	 * In this case, just print out a warning and let the chip tell the
3662991554f2SKenneth D. Merry 	 * user they did the wrong thing.
3663991554f2SKenneth D. Merry 	 */
3664991554f2SKenneth D. Merry 	if ((cm->cm_max_segs != 0) && (nsegs > cm->cm_max_segs)) {
36657a2a6a1aSStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "%s: warning: busdma returned %d "
36667a2a6a1aSStephen McConnell 		    "segments, more than the %d allowed\n", __func__, nsegs,
3667991554f2SKenneth D. Merry 		    cm->cm_max_segs);
3668991554f2SKenneth D. Merry 	}
3669991554f2SKenneth D. Merry 
3670991554f2SKenneth D. Merry 	/*
3671991554f2SKenneth D. Merry 	 * Set up DMA direction flags.  Bi-directional requests are also handled
3672991554f2SKenneth D. Merry 	 * here.  In that case, both direction flags will be set.
3673991554f2SKenneth D. Merry 	 */
3674991554f2SKenneth D. Merry 	sflags = 0;
3675991554f2SKenneth D. Merry 	if (cm->cm_flags & MPR_CM_FLAGS_SMP_PASS) {
3676991554f2SKenneth D. Merry 		/*
3677991554f2SKenneth D. Merry 		 * We have to add a special case for SMP passthrough, there
3678991554f2SKenneth D. Merry 		 * is no easy way to generically handle it.  The first
3679991554f2SKenneth D. Merry 		 * S/G element is used for the command (therefore the
3680991554f2SKenneth D. Merry 		 * direction bit needs to be set).  The second one is used
3681991554f2SKenneth D. Merry 		 * for the reply.  We'll leave it to the caller to make
3682991554f2SKenneth D. Merry 		 * sure we only have two buffers.
3683991554f2SKenneth D. Merry 		 */
3684991554f2SKenneth D. Merry 		/*
3685991554f2SKenneth D. Merry 		 * Even though the busdma man page says it doesn't make
3686991554f2SKenneth D. Merry 		 * sense to have both direction flags, it does in this case.
3687991554f2SKenneth D. Merry 		 * We have one s/g element being accessed in each direction.
3688991554f2SKenneth D. Merry 		 */
3689991554f2SKenneth D. Merry 		dir = BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD;
3690991554f2SKenneth D. Merry 
3691991554f2SKenneth D. Merry 		/*
3692991554f2SKenneth D. Merry 		 * Set the direction flag on the first buffer in the SMP
3693991554f2SKenneth D. Merry 		 * passthrough request.  We'll clear it for the second one.
3694991554f2SKenneth D. Merry 		 */
3695991554f2SKenneth D. Merry 		sflags |= MPI2_SGE_FLAGS_DIRECTION |
3696991554f2SKenneth D. Merry 			  MPI2_SGE_FLAGS_END_OF_BUFFER;
3697991554f2SKenneth D. Merry 	} else if (cm->cm_flags & MPR_CM_FLAGS_DATAOUT) {
3698991554f2SKenneth D. Merry 		sflags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
3699991554f2SKenneth D. Merry 		dir = BUS_DMASYNC_PREWRITE;
3700991554f2SKenneth D. Merry 	} else
3701991554f2SKenneth D. Merry 		dir = BUS_DMASYNC_PREREAD;
3702991554f2SKenneth D. Merry 
370367feec50SStephen McConnell 	/* Check if a native SG list is needed for an NVMe PCIe device. */
370467feec50SStephen McConnell 	if (cm->cm_targ && cm->cm_targ->is_nvme &&
370567feec50SStephen McConnell 	    mpr_check_pcie_native_sgl(sc, cm, segs, nsegs) == 0) {
370667feec50SStephen McConnell 		/* A native SG list was built, skip to end. */
370767feec50SStephen McConnell 		goto out;
370867feec50SStephen McConnell 	}
370967feec50SStephen McConnell 
3710991554f2SKenneth D. Merry 	for (i = 0; i < nsegs; i++) {
3711991554f2SKenneth D. Merry 		if ((cm->cm_flags & MPR_CM_FLAGS_SMP_PASS) && (i != 0)) {
3712991554f2SKenneth D. Merry 			sflags &= ~MPI2_SGE_FLAGS_DIRECTION;
3713991554f2SKenneth D. Merry 		}
3714991554f2SKenneth D. Merry 		error = mpr_add_dmaseg(cm, segs[i].ds_addr, segs[i].ds_len,
3715991554f2SKenneth D. Merry 		    sflags, nsegs - i);
3716991554f2SKenneth D. Merry 		if (error != 0) {
3717991554f2SKenneth D. Merry 			/* Resource shortage, roll back! */
3718991554f2SKenneth D. Merry 			if (ratecheck(&sc->lastfail, &mpr_chainfail_interval))
3719991554f2SKenneth D. Merry 				mpr_dprint(sc, MPR_INFO, "Out of chain frames, "
3720991554f2SKenneth D. Merry 				    "consider increasing hw.mpr.max_chains.\n");
3721991554f2SKenneth D. Merry 			cm->cm_flags |= MPR_CM_FLAGS_CHAIN_FAILED;
3722c5041b4eSWarner Losh 			/*
3723c5041b4eSWarner Losh 			 * mpr_complete_command can only be called on commands
3724c5041b4eSWarner Losh 			 * that are in the queue. Since this is an error path
3725c5041b4eSWarner Losh 			 * which gets called before we enqueue, update the state
3726c5041b4eSWarner Losh 			 * to meet this requirement before we complete it.
3727c5041b4eSWarner Losh 			 */
372833755dbbSWarner Losh 			cm->cm_state = MPR_CM_STATE_INQUEUE;
3729991554f2SKenneth D. Merry 			mpr_complete_command(sc, cm);
3730991554f2SKenneth D. Merry 			return;
3731991554f2SKenneth D. Merry 		}
3732991554f2SKenneth D. Merry 	}
3733991554f2SKenneth D. Merry 
373467feec50SStephen McConnell out:
3735991554f2SKenneth D. Merry 	bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir);
3736991554f2SKenneth D. Merry 	mpr_enqueue_request(sc, cm);
3737991554f2SKenneth D. Merry 
3738991554f2SKenneth D. Merry 	return;
3739991554f2SKenneth D. Merry }
3740991554f2SKenneth D. Merry 
3741991554f2SKenneth D. Merry static void
mpr_data_cb2(void * arg,bus_dma_segment_t * segs,int nsegs,bus_size_t mapsize,int error)3742991554f2SKenneth D. Merry mpr_data_cb2(void *arg, bus_dma_segment_t *segs, int nsegs, bus_size_t mapsize,
3743991554f2SKenneth D. Merry 	     int error)
3744991554f2SKenneth D. Merry {
3745991554f2SKenneth D. Merry 	mpr_data_cb(arg, segs, nsegs, error);
3746991554f2SKenneth D. Merry }
3747991554f2SKenneth D. Merry 
3748991554f2SKenneth D. Merry /*
3749991554f2SKenneth D. Merry  * This is the routine to enqueue commands ansynchronously.
3750991554f2SKenneth D. Merry  * Note that the only error path here is from bus_dmamap_load(), which can
3751991554f2SKenneth D. Merry  * return EINPROGRESS if it is waiting for resources.  Other than this, it's
3752991554f2SKenneth D. Merry  * assumed that if you have a command in-hand, then you have enough credits
3753991554f2SKenneth D. Merry  * to use it.
3754991554f2SKenneth D. Merry  */
3755991554f2SKenneth D. Merry int
mpr_map_command(struct mpr_softc * sc,struct mpr_command * cm)3756991554f2SKenneth D. Merry mpr_map_command(struct mpr_softc *sc, struct mpr_command *cm)
3757991554f2SKenneth D. Merry {
3758991554f2SKenneth D. Merry 	int error = 0;
3759991554f2SKenneth D. Merry 
3760991554f2SKenneth D. Merry 	if (cm->cm_flags & MPR_CM_FLAGS_USE_UIO) {
3761991554f2SKenneth D. Merry 		error = bus_dmamap_load_uio(sc->buffer_dmat, cm->cm_dmamap,
3762991554f2SKenneth D. Merry 		    &cm->cm_uio, mpr_data_cb2, cm, 0);
3763991554f2SKenneth D. Merry 	} else if (cm->cm_flags & MPR_CM_FLAGS_USE_CCB) {
3764991554f2SKenneth D. Merry 		error = bus_dmamap_load_ccb(sc->buffer_dmat, cm->cm_dmamap,
3765991554f2SKenneth D. Merry 		    cm->cm_data, mpr_data_cb, cm, 0);
3766991554f2SKenneth D. Merry 	} else if ((cm->cm_data != NULL) && (cm->cm_length != 0)) {
3767991554f2SKenneth D. Merry 		error = bus_dmamap_load(sc->buffer_dmat, cm->cm_dmamap,
3768991554f2SKenneth D. Merry 		    cm->cm_data, cm->cm_length, mpr_data_cb, cm, 0);
3769991554f2SKenneth D. Merry 	} else {
3770991554f2SKenneth D. Merry 		/* Add a zero-length element as needed */
3771991554f2SKenneth D. Merry 		if (cm->cm_sge != NULL)
3772991554f2SKenneth D. Merry 			mpr_add_dmaseg(cm, 0, 0, 0, 1);
3773991554f2SKenneth D. Merry 		mpr_enqueue_request(sc, cm);
3774991554f2SKenneth D. Merry 	}
3775991554f2SKenneth D. Merry 
3776991554f2SKenneth D. Merry 	return (error);
3777991554f2SKenneth D. Merry }
3778991554f2SKenneth D. Merry 
3779991554f2SKenneth D. Merry /*
3780991554f2SKenneth D. Merry  * This is the routine to enqueue commands synchronously.  An error of
3781991554f2SKenneth D. Merry  * EINPROGRESS from mpr_map_command() is ignored since the command will
3782991554f2SKenneth D. Merry  * be executed and enqueued automatically.  Other errors come from msleep().
3783991554f2SKenneth D. Merry  */
3784991554f2SKenneth D. Merry int
mpr_wait_command(struct mpr_softc * sc,struct mpr_command ** cmp,int timeout,int sleep_flag)37856d4ffcb4SKenneth D. Merry mpr_wait_command(struct mpr_softc *sc, struct mpr_command **cmp, int timeout,
3786991554f2SKenneth D. Merry     int sleep_flag)
3787991554f2SKenneth D. Merry {
3788991554f2SKenneth D. Merry 	int error, rc;
3789991554f2SKenneth D. Merry 	struct timeval cur_time, start_time;
37906d4ffcb4SKenneth D. Merry 	struct mpr_command *cm = *cmp;
3791991554f2SKenneth D. Merry 
3792991554f2SKenneth D. Merry 	if (sc->mpr_flags & MPR_FLAGS_DIAGRESET)
3793991554f2SKenneth D. Merry 		return  EBUSY;
3794991554f2SKenneth D. Merry 
3795991554f2SKenneth D. Merry 	cm->cm_complete = NULL;
3796991554f2SKenneth D. Merry 	cm->cm_flags |= (MPR_CM_FLAGS_WAKEUP + MPR_CM_FLAGS_POLLED);
3797991554f2SKenneth D. Merry 	error = mpr_map_command(sc, cm);
3798991554f2SKenneth D. Merry 	if ((error != 0) && (error != EINPROGRESS))
3799991554f2SKenneth D. Merry 		return (error);
3800991554f2SKenneth D. Merry 
3801991554f2SKenneth D. Merry 	// Check for context and wait for 50 mSec at a time until time has
3802991554f2SKenneth D. Merry 	// expired or the command has finished.  If msleep can't be used, need
3803991554f2SKenneth D. Merry 	// to poll.
3804991554f2SKenneth D. Merry 	if (curthread->td_no_sleeping)
3805991554f2SKenneth D. Merry 		sleep_flag = NO_SLEEP;
3806417aa6b8SKenneth D. Merry 	getmicrouptime(&start_time);
3807991554f2SKenneth D. Merry 	if (mtx_owned(&sc->mpr_mtx) && sleep_flag == CAN_SLEEP) {
3808991554f2SKenneth D. Merry 		error = msleep(cm, &sc->mpr_mtx, 0, "mprwait", timeout*hz);
3809417aa6b8SKenneth D. Merry 		if (error == EWOULDBLOCK) {
3810417aa6b8SKenneth D. Merry 			/*
3811417aa6b8SKenneth D. Merry 			 * Record the actual elapsed time in the case of a
3812417aa6b8SKenneth D. Merry 			 * timeout for the message below.
3813417aa6b8SKenneth D. Merry 			 */
3814417aa6b8SKenneth D. Merry 			getmicrouptime(&cur_time);
3815417aa6b8SKenneth D. Merry 			timevalsub(&cur_time, &start_time);
3816417aa6b8SKenneth D. Merry 		}
3817991554f2SKenneth D. Merry 	} else {
3818991554f2SKenneth D. Merry 		while ((cm->cm_flags & MPR_CM_FLAGS_COMPLETE) == 0) {
3819991554f2SKenneth D. Merry 			mpr_intr_locked(sc);
3820991554f2SKenneth D. Merry 			if (sleep_flag == CAN_SLEEP)
3821991554f2SKenneth D. Merry 				pause("mprwait", hz/20);
3822991554f2SKenneth D. Merry 			else
3823991554f2SKenneth D. Merry 				DELAY(50000);
3824991554f2SKenneth D. Merry 
3825417aa6b8SKenneth D. Merry 			getmicrouptime(&cur_time);
3826417aa6b8SKenneth D. Merry 			timevalsub(&cur_time, &start_time);
3827417aa6b8SKenneth D. Merry 			if (cur_time.tv_sec > timeout) {
3828991554f2SKenneth D. Merry 				error = EWOULDBLOCK;
3829991554f2SKenneth D. Merry 				break;
3830991554f2SKenneth D. Merry 			}
3831991554f2SKenneth D. Merry 		}
3832991554f2SKenneth D. Merry 	}
3833991554f2SKenneth D. Merry 
3834991554f2SKenneth D. Merry 	if (error == EWOULDBLOCK) {
383586312e46SConrad Meyer 		if (cm->cm_timeout_handler == NULL) {
3836417aa6b8SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT, "Calling Reinit from %s, timeout=%d,"
3837417aa6b8SKenneth D. Merry 			    " elapsed=%jd\n", __func__, timeout,
3838417aa6b8SKenneth D. Merry 			    (intmax_t)cur_time.tv_sec);
3839991554f2SKenneth D. Merry 			rc = mpr_reinit(sc);
3840991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT, "Reinit %s\n", (rc == 0) ? "success" :
3841991554f2SKenneth D. Merry 			    "failed");
384286312e46SConrad Meyer 		} else
384386312e46SConrad Meyer 			cm->cm_timeout_handler(sc, cm);
38446d4ffcb4SKenneth D. Merry 		if (sc->mpr_flags & MPR_FLAGS_REALLOCATED) {
38456d4ffcb4SKenneth D. Merry 			/*
38466d4ffcb4SKenneth D. Merry 			 * Tell the caller that we freed the command in a
38476d4ffcb4SKenneth D. Merry 			 * reinit.
38486d4ffcb4SKenneth D. Merry 			 */
38496d4ffcb4SKenneth D. Merry 			*cmp = NULL;
38506d4ffcb4SKenneth D. Merry 		}
3851991554f2SKenneth D. Merry 		error = ETIMEDOUT;
3852991554f2SKenneth D. Merry 	}
3853991554f2SKenneth D. Merry 	return (error);
3854991554f2SKenneth D. Merry }
3855991554f2SKenneth D. Merry 
3856991554f2SKenneth D. Merry /*
3857991554f2SKenneth D. Merry  * This is the routine to enqueue a command synchonously and poll for
3858991554f2SKenneth D. Merry  * completion.  Its use should be rare.
3859991554f2SKenneth D. Merry  */
3860991554f2SKenneth D. Merry int
mpr_request_polled(struct mpr_softc * sc,struct mpr_command ** cmp)38616d4ffcb4SKenneth D. Merry mpr_request_polled(struct mpr_softc *sc, struct mpr_command **cmp)
3862991554f2SKenneth D. Merry {
38636d4ffcb4SKenneth D. Merry 	int error, rc;
3864991554f2SKenneth D. Merry 	struct timeval cur_time, start_time;
38656d4ffcb4SKenneth D. Merry 	struct mpr_command *cm = *cmp;
3866991554f2SKenneth D. Merry 
3867991554f2SKenneth D. Merry 	error = 0;
3868991554f2SKenneth D. Merry 
3869991554f2SKenneth D. Merry 	cm->cm_flags |= MPR_CM_FLAGS_POLLED;
3870991554f2SKenneth D. Merry 	cm->cm_complete = NULL;
3871991554f2SKenneth D. Merry 	mpr_map_command(sc, cm);
3872991554f2SKenneth D. Merry 
38736d4ffcb4SKenneth D. Merry 	getmicrouptime(&start_time);
3874991554f2SKenneth D. Merry 	while ((cm->cm_flags & MPR_CM_FLAGS_COMPLETE) == 0) {
3875991554f2SKenneth D. Merry 		mpr_intr_locked(sc);
3876991554f2SKenneth D. Merry 
3877991554f2SKenneth D. Merry 		if (mtx_owned(&sc->mpr_mtx))
3878991554f2SKenneth D. Merry 			msleep(&sc->msleep_fake_chan, &sc->mpr_mtx, 0,
3879991554f2SKenneth D. Merry 			    "mprpoll", hz/20);
3880991554f2SKenneth D. Merry 		else
3881991554f2SKenneth D. Merry 			pause("mprpoll", hz/20);
3882991554f2SKenneth D. Merry 
3883991554f2SKenneth D. Merry 		/*
3884991554f2SKenneth D. Merry 		 * Check for real-time timeout and fail if more than 60 seconds.
3885991554f2SKenneth D. Merry 		 */
38866d4ffcb4SKenneth D. Merry 		getmicrouptime(&cur_time);
38876d4ffcb4SKenneth D. Merry 		timevalsub(&cur_time, &start_time);
38886d4ffcb4SKenneth D. Merry 		if (cur_time.tv_sec > 60) {
3889991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT, "polling failed\n");
3890991554f2SKenneth D. Merry 			error = ETIMEDOUT;
3891991554f2SKenneth D. Merry 			break;
3892991554f2SKenneth D. Merry 		}
3893991554f2SKenneth D. Merry 	}
38944b1ac5c2SWarner Losh 	cm->cm_state = MPR_CM_STATE_BUSY;
3895991554f2SKenneth D. Merry 	if (error) {
3896991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Calling Reinit from %s\n", __func__);
3897991554f2SKenneth D. Merry 		rc = mpr_reinit(sc);
38987a2a6a1aSStephen McConnell 		mpr_dprint(sc, MPR_FAULT, "Reinit %s\n", (rc == 0) ? "success" :
38997a2a6a1aSStephen McConnell 		    "failed");
39006d4ffcb4SKenneth D. Merry 
39016d4ffcb4SKenneth D. Merry 		if (sc->mpr_flags & MPR_FLAGS_REALLOCATED) {
39026d4ffcb4SKenneth D. Merry 			/*
39036d4ffcb4SKenneth D. Merry 			 * Tell the caller that we freed the command in a
39046d4ffcb4SKenneth D. Merry 			 * reinit.
39056d4ffcb4SKenneth D. Merry 			 */
39066d4ffcb4SKenneth D. Merry 			*cmp = NULL;
39076d4ffcb4SKenneth D. Merry 		}
3908991554f2SKenneth D. Merry 	}
3909991554f2SKenneth D. Merry 	return (error);
3910991554f2SKenneth D. Merry }
3911991554f2SKenneth D. Merry 
3912991554f2SKenneth D. Merry /*
3913991554f2SKenneth D. Merry  * The MPT driver had a verbose interface for config pages.  In this driver,
3914453130d9SPedro F. Giffuni  * reduce it to much simpler terms, similar to the Linux driver.
3915991554f2SKenneth D. Merry  */
3916991554f2SKenneth D. Merry int
mpr_read_config_page(struct mpr_softc * sc,struct mpr_config_params * params)3917991554f2SKenneth D. Merry mpr_read_config_page(struct mpr_softc *sc, struct mpr_config_params *params)
3918991554f2SKenneth D. Merry {
3919991554f2SKenneth D. Merry 	MPI2_CONFIG_REQUEST *req;
3920991554f2SKenneth D. Merry 	struct mpr_command *cm;
3921991554f2SKenneth D. Merry 	int error;
3922991554f2SKenneth D. Merry 
3923991554f2SKenneth D. Merry 	if (sc->mpr_flags & MPR_FLAGS_BUSY) {
3924991554f2SKenneth D. Merry 		return (EBUSY);
3925991554f2SKenneth D. Merry 	}
3926991554f2SKenneth D. Merry 
3927991554f2SKenneth D. Merry 	cm = mpr_alloc_command(sc);
3928991554f2SKenneth D. Merry 	if (cm == NULL) {
3929991554f2SKenneth D. Merry 		return (EBUSY);
3930991554f2SKenneth D. Merry 	}
3931991554f2SKenneth D. Merry 
3932991554f2SKenneth D. Merry 	req = (MPI2_CONFIG_REQUEST *)cm->cm_req;
3933991554f2SKenneth D. Merry 	req->Function = MPI2_FUNCTION_CONFIG;
3934991554f2SKenneth D. Merry 	req->Action = params->action;
3935991554f2SKenneth D. Merry 	req->SGLFlags = 0;
3936991554f2SKenneth D. Merry 	req->ChainOffset = 0;
3937991554f2SKenneth D. Merry 	req->PageAddress = params->page_address;
3938991554f2SKenneth D. Merry 	if (params->hdr.Struct.PageType == MPI2_CONFIG_PAGETYPE_EXTENDED) {
3939991554f2SKenneth D. Merry 		MPI2_CONFIG_EXTENDED_PAGE_HEADER *hdr;
3940991554f2SKenneth D. Merry 
3941991554f2SKenneth D. Merry 		hdr = &params->hdr.Ext;
3942991554f2SKenneth D. Merry 		req->ExtPageType = hdr->ExtPageType;
3943991554f2SKenneth D. Merry 		req->ExtPageLength = hdr->ExtPageLength;
3944991554f2SKenneth D. Merry 		req->Header.PageType = MPI2_CONFIG_PAGETYPE_EXTENDED;
3945991554f2SKenneth D. Merry 		req->Header.PageLength = 0; /* Must be set to zero */
3946991554f2SKenneth D. Merry 		req->Header.PageNumber = hdr->PageNumber;
3947991554f2SKenneth D. Merry 		req->Header.PageVersion = hdr->PageVersion;
3948991554f2SKenneth D. Merry 	} else {
3949991554f2SKenneth D. Merry 		MPI2_CONFIG_PAGE_HEADER *hdr;
3950991554f2SKenneth D. Merry 
3951991554f2SKenneth D. Merry 		hdr = &params->hdr.Struct;
3952991554f2SKenneth D. Merry 		req->Header.PageType = hdr->PageType;
3953991554f2SKenneth D. Merry 		req->Header.PageNumber = hdr->PageNumber;
3954991554f2SKenneth D. Merry 		req->Header.PageLength = hdr->PageLength;
3955991554f2SKenneth D. Merry 		req->Header.PageVersion = hdr->PageVersion;
3956991554f2SKenneth D. Merry 	}
3957991554f2SKenneth D. Merry 
3958991554f2SKenneth D. Merry 	cm->cm_data = params->buffer;
3959991554f2SKenneth D. Merry 	cm->cm_length = params->length;
3960a2c14879SStephen McConnell 	if (cm->cm_data != NULL) {
3961991554f2SKenneth D. Merry 		cm->cm_sge = &req->PageBufferSGE;
3962991554f2SKenneth D. Merry 		cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
3963991554f2SKenneth D. Merry 		cm->cm_flags = MPR_CM_FLAGS_SGE_SIMPLE | MPR_CM_FLAGS_DATAIN;
3964a2c14879SStephen McConnell 	} else
3965a2c14879SStephen McConnell 		cm->cm_sge = NULL;
3966991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3967991554f2SKenneth D. Merry 
3968991554f2SKenneth D. Merry 	cm->cm_complete_data = params;
3969991554f2SKenneth D. Merry 	if (params->callback != NULL) {
3970991554f2SKenneth D. Merry 		cm->cm_complete = mpr_config_complete;
3971991554f2SKenneth D. Merry 		return (mpr_map_command(sc, cm));
3972991554f2SKenneth D. Merry 	} else {
39736d4ffcb4SKenneth D. Merry 		error = mpr_wait_command(sc, &cm, 0, CAN_SLEEP);
3974991554f2SKenneth D. Merry 		if (error) {
3975991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT,
3976991554f2SKenneth D. Merry 			    "Error %d reading config page\n", error);
39776d4ffcb4SKenneth D. Merry 			if (cm != NULL)
3978991554f2SKenneth D. Merry 				mpr_free_command(sc, cm);
3979991554f2SKenneth D. Merry 			return (error);
3980991554f2SKenneth D. Merry 		}
3981991554f2SKenneth D. Merry 		mpr_config_complete(sc, cm);
3982991554f2SKenneth D. Merry 	}
3983991554f2SKenneth D. Merry 
3984991554f2SKenneth D. Merry 	return (0);
3985991554f2SKenneth D. Merry }
3986991554f2SKenneth D. Merry 
3987991554f2SKenneth D. Merry int
mpr_write_config_page(struct mpr_softc * sc,struct mpr_config_params * params)3988991554f2SKenneth D. Merry mpr_write_config_page(struct mpr_softc *sc, struct mpr_config_params *params)
3989991554f2SKenneth D. Merry {
3990991554f2SKenneth D. Merry 	return (EINVAL);
3991991554f2SKenneth D. Merry }
3992991554f2SKenneth D. Merry 
3993991554f2SKenneth D. Merry static void
mpr_config_complete(struct mpr_softc * sc,struct mpr_command * cm)3994991554f2SKenneth D. Merry mpr_config_complete(struct mpr_softc *sc, struct mpr_command *cm)
3995991554f2SKenneth D. Merry {
3996991554f2SKenneth D. Merry 	MPI2_CONFIG_REPLY *reply;
3997991554f2SKenneth D. Merry 	struct mpr_config_params *params;
3998991554f2SKenneth D. Merry 
3999991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
4000991554f2SKenneth D. Merry 	params = cm->cm_complete_data;
4001991554f2SKenneth D. Merry 
4002991554f2SKenneth D. Merry 	if (cm->cm_data != NULL) {
4003991554f2SKenneth D. Merry 		bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap,
4004991554f2SKenneth D. Merry 		    BUS_DMASYNC_POSTREAD);
4005991554f2SKenneth D. Merry 		bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
4006991554f2SKenneth D. Merry 	}
4007991554f2SKenneth D. Merry 
4008991554f2SKenneth D. Merry 	/*
4009991554f2SKenneth D. Merry 	 * XXX KDM need to do more error recovery?  This results in the
4010991554f2SKenneth D. Merry 	 * device in question not getting probed.
4011991554f2SKenneth D. Merry 	 */
4012991554f2SKenneth D. Merry 	if ((cm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
4013991554f2SKenneth D. Merry 		params->status = MPI2_IOCSTATUS_BUSY;
4014991554f2SKenneth D. Merry 		goto done;
4015991554f2SKenneth D. Merry 	}
4016991554f2SKenneth D. Merry 
4017991554f2SKenneth D. Merry 	reply = (MPI2_CONFIG_REPLY *)cm->cm_reply;
4018991554f2SKenneth D. Merry 	if (reply == NULL) {
4019991554f2SKenneth D. Merry 		params->status = MPI2_IOCSTATUS_BUSY;
4020991554f2SKenneth D. Merry 		goto done;
4021991554f2SKenneth D. Merry 	}
4022991554f2SKenneth D. Merry 	params->status = reply->IOCStatus;
4023a2c14879SStephen McConnell 	if (params->hdr.Struct.PageType == MPI2_CONFIG_PAGETYPE_EXTENDED) {
4024991554f2SKenneth D. Merry 		params->hdr.Ext.ExtPageType = reply->ExtPageType;
4025991554f2SKenneth D. Merry 		params->hdr.Ext.ExtPageLength = reply->ExtPageLength;
4026a2c14879SStephen McConnell 		params->hdr.Ext.PageType = reply->Header.PageType;
4027a2c14879SStephen McConnell 		params->hdr.Ext.PageNumber = reply->Header.PageNumber;
4028a2c14879SStephen McConnell 		params->hdr.Ext.PageVersion = reply->Header.PageVersion;
4029991554f2SKenneth D. Merry 	} else {
4030991554f2SKenneth D. Merry 		params->hdr.Struct.PageType = reply->Header.PageType;
4031991554f2SKenneth D. Merry 		params->hdr.Struct.PageNumber = reply->Header.PageNumber;
4032991554f2SKenneth D. Merry 		params->hdr.Struct.PageLength = reply->Header.PageLength;
4033991554f2SKenneth D. Merry 		params->hdr.Struct.PageVersion = reply->Header.PageVersion;
4034991554f2SKenneth D. Merry 	}
4035991554f2SKenneth D. Merry 
4036991554f2SKenneth D. Merry done:
4037991554f2SKenneth D. Merry 	mpr_free_command(sc, cm);
4038991554f2SKenneth D. Merry 	if (params->callback != NULL)
4039991554f2SKenneth D. Merry 		params->callback(sc, params);
4040991554f2SKenneth D. Merry 
4041991554f2SKenneth D. Merry 	return;
4042991554f2SKenneth D. Merry }
4043