xref: /freebsd/sys/dev/mpr/mpr.c (revision 3c5ac992c76786af01081721995ae377131b3edc)
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
5991554f2SKenneth D. Merry  * All rights reserved.
6991554f2SKenneth D. Merry  *
7991554f2SKenneth D. Merry  * Redistribution and use in source and binary forms, with or without
8991554f2SKenneth D. Merry  * modification, are permitted provided that the following conditions
9991554f2SKenneth D. Merry  * are met:
10991554f2SKenneth D. Merry  * 1. Redistributions of source code must retain the above copyright
11991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer.
12991554f2SKenneth D. Merry  * 2. Redistributions in binary form must reproduce the above copyright
13991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer in the
14991554f2SKenneth D. Merry  *    documentation and/or other materials provided with the distribution.
15991554f2SKenneth D. Merry  *
16991554f2SKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17991554f2SKenneth D. Merry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18991554f2SKenneth D. Merry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19991554f2SKenneth D. Merry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20991554f2SKenneth D. Merry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21991554f2SKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22991554f2SKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23991554f2SKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24991554f2SKenneth D. Merry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25991554f2SKenneth D. Merry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26991554f2SKenneth D. Merry  * SUCH DAMAGE.
27991554f2SKenneth D. Merry  *
28a2c14879SStephen McConnell  * Avago Technologies (LSI) MPT-Fusion Host Adapter FreeBSD
29a2c14879SStephen McConnell  *
30991554f2SKenneth D. Merry  */
31991554f2SKenneth D. Merry 
32991554f2SKenneth D. Merry #include <sys/cdefs.h>
33991554f2SKenneth D. Merry __FBSDID("$FreeBSD$");
34991554f2SKenneth D. Merry 
35a2c14879SStephen McConnell /* Communications core for Avago Technologies (LSI) MPT3 */
36991554f2SKenneth D. Merry 
37991554f2SKenneth D. Merry /* TODO Move headers to mprvar */
38991554f2SKenneth D. Merry #include <sys/types.h>
39991554f2SKenneth D. Merry #include <sys/param.h>
40991554f2SKenneth D. Merry #include <sys/systm.h>
41991554f2SKenneth D. Merry #include <sys/kernel.h>
42991554f2SKenneth D. Merry #include <sys/selinfo.h>
43991554f2SKenneth D. Merry #include <sys/lock.h>
44991554f2SKenneth D. Merry #include <sys/mutex.h>
45991554f2SKenneth D. Merry #include <sys/module.h>
46991554f2SKenneth D. Merry #include <sys/bus.h>
47991554f2SKenneth D. Merry #include <sys/conf.h>
48991554f2SKenneth D. Merry #include <sys/bio.h>
49991554f2SKenneth D. Merry #include <sys/malloc.h>
50991554f2SKenneth D. Merry #include <sys/uio.h>
51991554f2SKenneth D. Merry #include <sys/sysctl.h>
52bec09074SScott Long #include <sys/smp.h>
53991554f2SKenneth D. Merry #include <sys/queue.h>
54991554f2SKenneth D. Merry #include <sys/kthread.h>
55991554f2SKenneth D. Merry #include <sys/taskqueue.h>
56991554f2SKenneth D. Merry #include <sys/endian.h>
57991554f2SKenneth D. Merry #include <sys/eventhandler.h>
58991554f2SKenneth D. Merry 
59991554f2SKenneth D. Merry #include <machine/bus.h>
60991554f2SKenneth D. Merry #include <machine/resource.h>
61991554f2SKenneth D. Merry #include <sys/rman.h>
62991554f2SKenneth D. Merry #include <sys/proc.h>
63991554f2SKenneth D. Merry 
64991554f2SKenneth D. Merry #include <dev/pci/pcivar.h>
65991554f2SKenneth D. Merry 
66991554f2SKenneth D. Merry #include <cam/cam.h>
6767feec50SStephen McConnell #include <cam/cam_ccb.h>
68991554f2SKenneth D. Merry #include <cam/scsi/scsi_all.h>
69991554f2SKenneth D. Merry 
70991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_type.h>
71991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2.h>
72991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_ioc.h>
73991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_sas.h>
7467feec50SStephen McConnell #include <dev/mpr/mpi/mpi2_pci.h>
75991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_cnfg.h>
76991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_init.h>
77991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_tool.h>
78991554f2SKenneth D. Merry #include <dev/mpr/mpr_ioctl.h>
79991554f2SKenneth D. Merry #include <dev/mpr/mprvar.h>
80991554f2SKenneth D. Merry #include <dev/mpr/mpr_table.h>
8167feec50SStephen McConnell #include <dev/mpr/mpr_sas.h>
82991554f2SKenneth D. Merry 
83991554f2SKenneth D. Merry static int mpr_diag_reset(struct mpr_softc *sc, int sleep_flag);
84991554f2SKenneth D. Merry static int mpr_init_queues(struct mpr_softc *sc);
85*3c5ac992SScott Long static void mpr_resize_queues(struct mpr_softc *sc);
86991554f2SKenneth D. Merry static int mpr_message_unit_reset(struct mpr_softc *sc, int sleep_flag);
87991554f2SKenneth D. Merry static int mpr_transition_operational(struct mpr_softc *sc);
88991554f2SKenneth D. Merry static int mpr_iocfacts_allocate(struct mpr_softc *sc, uint8_t attaching);
89991554f2SKenneth D. Merry static void mpr_iocfacts_free(struct mpr_softc *sc);
90991554f2SKenneth D. Merry static void mpr_startup(void *arg);
91991554f2SKenneth D. Merry static int mpr_send_iocinit(struct mpr_softc *sc);
92991554f2SKenneth D. Merry static int mpr_alloc_queues(struct mpr_softc *sc);
931415db6cSScott Long static int mpr_alloc_hw_queues(struct mpr_softc *sc);
94991554f2SKenneth D. Merry static int mpr_alloc_replies(struct mpr_softc *sc);
95991554f2SKenneth D. Merry static int mpr_alloc_requests(struct mpr_softc *sc);
9667feec50SStephen McConnell static int mpr_alloc_nvme_prp_pages(struct mpr_softc *sc);
97991554f2SKenneth D. Merry static int mpr_attach_log(struct mpr_softc *sc);
98991554f2SKenneth D. Merry static __inline void mpr_complete_command(struct mpr_softc *sc,
99991554f2SKenneth D. Merry     struct mpr_command *cm);
100991554f2SKenneth D. Merry static void mpr_dispatch_event(struct mpr_softc *sc, uintptr_t data,
101991554f2SKenneth D. Merry     MPI2_EVENT_NOTIFICATION_REPLY *reply);
1027a2a6a1aSStephen McConnell static void mpr_config_complete(struct mpr_softc *sc, struct mpr_command *cm);
103991554f2SKenneth D. Merry static void mpr_periodic(void *);
104991554f2SKenneth D. Merry static int mpr_reregister_events(struct mpr_softc *sc);
1057a2a6a1aSStephen McConnell static void mpr_enqueue_request(struct mpr_softc *sc, struct mpr_command *cm);
1067a2a6a1aSStephen McConnell static int mpr_get_iocfacts(struct mpr_softc *sc, MPI2_IOC_FACTS_REPLY *facts);
107991554f2SKenneth D. Merry static int mpr_wait_db_ack(struct mpr_softc *sc, int timeout, int sleep_flag);
108991554f2SKenneth D. Merry SYSCTL_NODE(_hw, OID_AUTO, mpr, CTLFLAG_RD, 0, "MPR Driver Parameters");
109991554f2SKenneth D. Merry 
110991554f2SKenneth D. Merry MALLOC_DEFINE(M_MPR, "mpr", "mpr driver memory");
111991554f2SKenneth D. Merry 
112991554f2SKenneth D. Merry /*
113991554f2SKenneth D. Merry  * Do a "Diagnostic Reset" aka a hard reset.  This should get the chip out of
114991554f2SKenneth D. Merry  * any state and back to its initialization state machine.
115991554f2SKenneth D. Merry  */
116991554f2SKenneth D. Merry static char mpt2_reset_magic[] = { 0x00, 0x0f, 0x04, 0x0b, 0x02, 0x07, 0x0d };
117991554f2SKenneth D. Merry 
118991554f2SKenneth D. Merry /*
119991554f2SKenneth D. Merry  * Added this union to smoothly convert le64toh cm->cm_desc.Words.
12067feec50SStephen McConnell  * Compiler only supports uint64_t to be passed as an argument.
121757ff642SScott Long  * Otherwise it will throw this error:
122991554f2SKenneth D. Merry  * "aggregate value used where an integer was expected"
123991554f2SKenneth D. Merry  */
124991554f2SKenneth D. Merry typedef union _reply_descriptor {
125991554f2SKenneth D. Merry         u64 word;
126991554f2SKenneth D. Merry         struct {
127991554f2SKenneth D. Merry                 u32 low;
128991554f2SKenneth D. Merry                 u32 high;
129991554f2SKenneth D. Merry         } u;
13067feec50SStephen McConnell } reply_descriptor, request_descriptor;
131991554f2SKenneth D. Merry 
132991554f2SKenneth D. Merry /* Rate limit chain-fail messages to 1 per minute */
133991554f2SKenneth D. Merry static struct timeval mpr_chainfail_interval = { 60, 0 };
134991554f2SKenneth D. Merry 
135991554f2SKenneth D. Merry /*
136991554f2SKenneth D. Merry  * sleep_flag can be either CAN_SLEEP or NO_SLEEP.
137991554f2SKenneth D. Merry  * If this function is called from process context, it can sleep
138991554f2SKenneth D. Merry  * and there is no harm to sleep, in case if this fuction is called
139991554f2SKenneth D. Merry  * from Interrupt handler, we can not sleep and need NO_SLEEP flag set.
140991554f2SKenneth D. Merry  * based on sleep flags driver will call either msleep, pause or DELAY.
141991554f2SKenneth D. Merry  * msleep and pause are of same variant, but pause is used when mpr_mtx
142991554f2SKenneth D. Merry  * is not hold by driver.
143991554f2SKenneth D. Merry  */
144991554f2SKenneth D. Merry static int
145991554f2SKenneth D. Merry mpr_diag_reset(struct mpr_softc *sc,int sleep_flag)
146991554f2SKenneth D. Merry {
147991554f2SKenneth D. Merry 	uint32_t reg;
148991554f2SKenneth D. Merry 	int i, error, tries = 0;
149991554f2SKenneth D. Merry 	uint8_t first_wait_done = FALSE;
150991554f2SKenneth D. Merry 
151757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
152991554f2SKenneth D. Merry 
153991554f2SKenneth D. Merry 	/* Clear any pending interrupts */
154991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
155991554f2SKenneth D. Merry 
156991554f2SKenneth D. Merry 	/*
157991554f2SKenneth D. Merry 	 * Force NO_SLEEP for threads prohibited to sleep
158991554f2SKenneth D. Merry  	 * e.a Thread from interrupt handler are prohibited to sleep.
159991554f2SKenneth D. Merry  	 */
160991554f2SKenneth D. Merry #if __FreeBSD_version >= 1000029
161991554f2SKenneth D. Merry 	if (curthread->td_no_sleeping)
162991554f2SKenneth D. Merry #else //__FreeBSD_version < 1000029
163991554f2SKenneth D. Merry 	if (curthread->td_pflags & TDP_NOSLEEPING)
164991554f2SKenneth D. Merry #endif //__FreeBSD_version >= 1000029
165991554f2SKenneth D. Merry 		sleep_flag = NO_SLEEP;
166991554f2SKenneth D. Merry 
167757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "sequence start, sleep_flag=%d\n", sleep_flag);
168991554f2SKenneth D. Merry 	/* Push the magic sequence */
169991554f2SKenneth D. Merry 	error = ETIMEDOUT;
170991554f2SKenneth D. Merry 	while (tries++ < 20) {
171991554f2SKenneth D. Merry 		for (i = 0; i < sizeof(mpt2_reset_magic); i++)
172991554f2SKenneth D. Merry 			mpr_regwrite(sc, MPI2_WRITE_SEQUENCE_OFFSET,
173991554f2SKenneth D. Merry 			    mpt2_reset_magic[i]);
174991554f2SKenneth D. Merry 
175991554f2SKenneth D. Merry 		/* wait 100 msec */
176991554f2SKenneth D. Merry 		if (mtx_owned(&sc->mpr_mtx) && sleep_flag == CAN_SLEEP)
177991554f2SKenneth D. Merry 			msleep(&sc->msleep_fake_chan, &sc->mpr_mtx, 0,
178991554f2SKenneth D. Merry 			    "mprdiag", hz/10);
179991554f2SKenneth D. Merry 		else if (sleep_flag == CAN_SLEEP)
180991554f2SKenneth D. Merry 			pause("mprdiag", hz/10);
181991554f2SKenneth D. Merry 		else
182991554f2SKenneth D. Merry 			DELAY(100 * 1000);
183991554f2SKenneth D. Merry 
184991554f2SKenneth D. Merry 		reg = mpr_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
185991554f2SKenneth D. Merry 		if (reg & MPI2_DIAG_DIAG_WRITE_ENABLE) {
186991554f2SKenneth D. Merry 			error = 0;
187991554f2SKenneth D. Merry 			break;
188991554f2SKenneth D. Merry 		}
189991554f2SKenneth D. Merry 	}
190757ff642SScott Long 	if (error) {
191757ff642SScott Long 		mpr_dprint(sc, MPR_INIT, "sequence failed, error=%d, exit\n",
192757ff642SScott Long 		    error);
193991554f2SKenneth D. Merry 		return (error);
194757ff642SScott Long 	}
195991554f2SKenneth D. Merry 
196991554f2SKenneth D. Merry 	/* Send the actual reset.  XXX need to refresh the reg? */
197757ff642SScott Long 	reg |= MPI2_DIAG_RESET_ADAPTER;
198757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "sequence success, sending reset, reg= 0x%x\n",
199757ff642SScott Long 	    reg);
200757ff642SScott Long 	mpr_regwrite(sc, MPI2_HOST_DIAGNOSTIC_OFFSET, reg);
201991554f2SKenneth D. Merry 
202991554f2SKenneth D. Merry 	/* Wait up to 300 seconds in 50ms intervals */
203991554f2SKenneth D. Merry 	error = ETIMEDOUT;
204991554f2SKenneth D. Merry 	for (i = 0; i < 6000; i++) {
205991554f2SKenneth D. Merry 		/*
206991554f2SKenneth D. Merry 		 * Wait 50 msec. If this is the first time through, wait 256
207991554f2SKenneth D. Merry 		 * msec to satisfy Diag Reset timing requirements.
208991554f2SKenneth D. Merry 		 */
209991554f2SKenneth D. Merry 		if (first_wait_done) {
210991554f2SKenneth D. Merry 			if (mtx_owned(&sc->mpr_mtx) && sleep_flag == CAN_SLEEP)
211991554f2SKenneth D. Merry 				msleep(&sc->msleep_fake_chan, &sc->mpr_mtx, 0,
212991554f2SKenneth D. Merry 				    "mprdiag", hz/20);
213991554f2SKenneth D. Merry 			else if (sleep_flag == CAN_SLEEP)
214991554f2SKenneth D. Merry 				pause("mprdiag", hz/20);
215991554f2SKenneth D. Merry 			else
216991554f2SKenneth D. Merry 				DELAY(50 * 1000);
217991554f2SKenneth D. Merry 		} else {
218991554f2SKenneth D. Merry 			DELAY(256 * 1000);
219991554f2SKenneth D. Merry 			first_wait_done = TRUE;
220991554f2SKenneth D. Merry 		}
221991554f2SKenneth D. Merry 		/*
222991554f2SKenneth D. Merry 		 * Check for the RESET_ADAPTER bit to be cleared first, then
223991554f2SKenneth D. Merry 		 * wait for the RESET state to be cleared, which takes a little
224991554f2SKenneth D. Merry 		 * longer.
225991554f2SKenneth D. Merry 		 */
226991554f2SKenneth D. Merry 		reg = mpr_regread(sc, MPI2_HOST_DIAGNOSTIC_OFFSET);
227991554f2SKenneth D. Merry 		if (reg & MPI2_DIAG_RESET_ADAPTER) {
228991554f2SKenneth D. Merry 			continue;
229991554f2SKenneth D. Merry 		}
230991554f2SKenneth D. Merry 		reg = mpr_regread(sc, MPI2_DOORBELL_OFFSET);
231991554f2SKenneth D. Merry 		if ((reg & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_RESET) {
232991554f2SKenneth D. Merry 			error = 0;
233991554f2SKenneth D. Merry 			break;
234991554f2SKenneth D. Merry 		}
235991554f2SKenneth D. Merry 	}
236757ff642SScott Long 	if (error) {
237757ff642SScott Long 		mpr_dprint(sc, MPR_INIT, "reset failed, error= %d, exit\n",
238757ff642SScott Long 		    error);
239991554f2SKenneth D. Merry 		return (error);
240757ff642SScott Long 	}
241991554f2SKenneth D. Merry 
242991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_WRITE_SEQUENCE_OFFSET, 0x0);
243757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "diag reset success, exit\n");
244991554f2SKenneth D. Merry 
245991554f2SKenneth D. Merry 	return (0);
246991554f2SKenneth D. Merry }
247991554f2SKenneth D. Merry 
248991554f2SKenneth D. Merry static int
249991554f2SKenneth D. Merry mpr_message_unit_reset(struct mpr_softc *sc, int sleep_flag)
250991554f2SKenneth D. Merry {
251757ff642SScott Long 	int error;
252991554f2SKenneth D. Merry 
253991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
254991554f2SKenneth D. Merry 
255757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
256757ff642SScott Long 
257757ff642SScott Long 	error = 0;
258991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_DOORBELL_OFFSET,
259991554f2SKenneth D. Merry 	    MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET <<
260991554f2SKenneth D. Merry 	    MPI2_DOORBELL_FUNCTION_SHIFT);
261991554f2SKenneth D. Merry 
262991554f2SKenneth D. Merry 	if (mpr_wait_db_ack(sc, 5, sleep_flag) != 0) {
263757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_FAULT,
264757ff642SScott Long 		    "Doorbell handshake failed\n");
265757ff642SScott Long 		error = ETIMEDOUT;
266991554f2SKenneth D. Merry 	}
267991554f2SKenneth D. Merry 
268757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit\n", __func__);
269757ff642SScott Long 	return (error);
270991554f2SKenneth D. Merry }
271991554f2SKenneth D. Merry 
272991554f2SKenneth D. Merry static int
273991554f2SKenneth D. Merry mpr_transition_ready(struct mpr_softc *sc)
274991554f2SKenneth D. Merry {
275991554f2SKenneth D. Merry 	uint32_t reg, state;
276991554f2SKenneth D. Merry 	int error, tries = 0;
277991554f2SKenneth D. Merry 	int sleep_flags;
278991554f2SKenneth D. Merry 
279991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
280991554f2SKenneth D. Merry 	/* If we are in attach call, do not sleep */
281991554f2SKenneth D. Merry 	sleep_flags = (sc->mpr_flags & MPR_FLAGS_ATTACH_DONE)
282991554f2SKenneth D. Merry 	    ? CAN_SLEEP : NO_SLEEP;
283991554f2SKenneth D. Merry 
284991554f2SKenneth D. Merry 	error = 0;
285757ff642SScott Long 
286757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered, sleep_flags= %d\n",
287757ff642SScott Long 	    __func__, sleep_flags);
288757ff642SScott Long 
289991554f2SKenneth D. Merry 	while (tries++ < 1200) {
290991554f2SKenneth D. Merry 		reg = mpr_regread(sc, MPI2_DOORBELL_OFFSET);
291991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_INIT, "  Doorbell= 0x%x\n", reg);
292991554f2SKenneth D. Merry 
293991554f2SKenneth D. Merry 		/*
294991554f2SKenneth D. Merry 		 * Ensure the IOC is ready to talk.  If it's not, try
295991554f2SKenneth D. Merry 		 * resetting it.
296991554f2SKenneth D. Merry 		 */
297991554f2SKenneth D. Merry 		if (reg & MPI2_DOORBELL_USED) {
298757ff642SScott Long 			mpr_dprint(sc, MPR_INIT, "  Not ready, sending diag "
299757ff642SScott Long 			    "reset\n");
300991554f2SKenneth D. Merry 			mpr_diag_reset(sc, sleep_flags);
301991554f2SKenneth D. Merry 			DELAY(50000);
302991554f2SKenneth D. Merry 			continue;
303991554f2SKenneth D. Merry 		}
304991554f2SKenneth D. Merry 
305991554f2SKenneth D. Merry 		/* Is the adapter owned by another peer? */
306991554f2SKenneth D. Merry 		if ((reg & MPI2_DOORBELL_WHO_INIT_MASK) ==
307991554f2SKenneth D. Merry 		    (MPI2_WHOINIT_PCI_PEER << MPI2_DOORBELL_WHO_INIT_SHIFT)) {
308757ff642SScott Long 			mpr_dprint(sc, MPR_INIT|MPR_FAULT, "IOC is under the "
309757ff642SScott Long 			    "control of another peer host, aborting "
310757ff642SScott Long 			    "initialization.\n");
311757ff642SScott Long 			error = ENXIO;
312757ff642SScott Long 			break;
313991554f2SKenneth D. Merry 		}
314991554f2SKenneth D. Merry 
315991554f2SKenneth D. Merry 		state = reg & MPI2_IOC_STATE_MASK;
316991554f2SKenneth D. Merry 		if (state == MPI2_IOC_STATE_READY) {
317991554f2SKenneth D. Merry 			/* Ready to go! */
318991554f2SKenneth D. Merry 			error = 0;
319991554f2SKenneth D. Merry 			break;
320991554f2SKenneth D. Merry 		} else if (state == MPI2_IOC_STATE_FAULT) {
321757ff642SScott Long 			mpr_dprint(sc, MPR_INIT|MPR_FAULT, "IOC in fault "
322757ff642SScott Long 			    "state 0x%x, resetting\n",
323991554f2SKenneth D. Merry 			    state & MPI2_DOORBELL_FAULT_CODE_MASK);
324991554f2SKenneth D. Merry 			mpr_diag_reset(sc, sleep_flags);
325991554f2SKenneth D. Merry 		} else if (state == MPI2_IOC_STATE_OPERATIONAL) {
326991554f2SKenneth D. Merry 			/* Need to take ownership */
327991554f2SKenneth D. Merry 			mpr_message_unit_reset(sc, sleep_flags);
328991554f2SKenneth D. Merry 		} else if (state == MPI2_IOC_STATE_RESET) {
329991554f2SKenneth D. Merry 			/* Wait a bit, IOC might be in transition */
330757ff642SScott Long 			mpr_dprint(sc, MPR_INIT|MPR_FAULT,
331991554f2SKenneth D. Merry 			    "IOC in unexpected reset state\n");
332991554f2SKenneth D. Merry 		} else {
333757ff642SScott Long 			mpr_dprint(sc, MPR_INIT|MPR_FAULT,
334991554f2SKenneth D. Merry 			    "IOC in unknown state 0x%x\n", state);
335991554f2SKenneth D. Merry 			error = EINVAL;
336991554f2SKenneth D. Merry 			break;
337991554f2SKenneth D. Merry 		}
338991554f2SKenneth D. Merry 
339991554f2SKenneth D. Merry 		/* Wait 50ms for things to settle down. */
340991554f2SKenneth D. Merry 		DELAY(50000);
341991554f2SKenneth D. Merry 	}
342991554f2SKenneth D. Merry 
343991554f2SKenneth D. Merry 	if (error)
344757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_FAULT,
345757ff642SScott Long 		    "Cannot transition IOC to ready\n");
346757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit\n", __func__);
347991554f2SKenneth D. Merry 	return (error);
348991554f2SKenneth D. Merry }
349991554f2SKenneth D. Merry 
350991554f2SKenneth D. Merry static int
351991554f2SKenneth D. Merry mpr_transition_operational(struct mpr_softc *sc)
352991554f2SKenneth D. Merry {
353991554f2SKenneth D. Merry 	uint32_t reg, state;
354991554f2SKenneth D. Merry 	int error;
355991554f2SKenneth D. Merry 
356991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
357991554f2SKenneth D. Merry 
358991554f2SKenneth D. Merry 	error = 0;
359991554f2SKenneth D. Merry 	reg = mpr_regread(sc, MPI2_DOORBELL_OFFSET);
360757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered, Doorbell= 0x%x\n", __func__, reg);
361991554f2SKenneth D. Merry 
362991554f2SKenneth D. Merry 	state = reg & MPI2_IOC_STATE_MASK;
363991554f2SKenneth D. Merry 	if (state != MPI2_IOC_STATE_READY) {
364757ff642SScott Long 		mpr_dprint(sc, MPR_INIT, "IOC not ready\n");
365991554f2SKenneth D. Merry 		if ((error = mpr_transition_ready(sc)) != 0) {
366757ff642SScott Long 			mpr_dprint(sc, MPR_INIT|MPR_FAULT,
367757ff642SScott Long 			    "failed to transition ready, exit\n");
368991554f2SKenneth D. Merry 			return (error);
369991554f2SKenneth D. Merry 		}
370991554f2SKenneth D. Merry 	}
371991554f2SKenneth D. Merry 
372991554f2SKenneth D. Merry 	error = mpr_send_iocinit(sc);
373757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit\n", __func__);
374757ff642SScott Long 
375991554f2SKenneth D. Merry 	return (error);
376991554f2SKenneth D. Merry }
377991554f2SKenneth D. Merry 
378*3c5ac992SScott Long static void
379*3c5ac992SScott Long mpr_resize_queues(struct mpr_softc *sc)
380*3c5ac992SScott Long {
381*3c5ac992SScott Long 	int reqcr, prireqcr;
382*3c5ac992SScott Long 
383*3c5ac992SScott Long 	/*
384*3c5ac992SScott Long 	 * Size the queues. Since the reply queues always need one free
385*3c5ac992SScott Long 	 * entry, we'll deduct one reply message here.  The LSI documents
386*3c5ac992SScott Long 	 * suggest instead to add a count to the request queue, but I think
387*3c5ac992SScott Long 	 * that it's better to deduct from reply queue.
388*3c5ac992SScott Long 	 */
389*3c5ac992SScott Long 	prireqcr = MAX(1, sc->max_prireqframes);
390*3c5ac992SScott Long 	prireqcr = MIN(prireqcr, sc->facts->HighPriorityCredit);
391*3c5ac992SScott Long 
392*3c5ac992SScott Long 	reqcr = MAX(2, sc->max_reqframes);
393*3c5ac992SScott Long 	reqcr = MIN(reqcr, sc->facts->RequestCredit);
394*3c5ac992SScott Long 
395*3c5ac992SScott Long 	sc->num_reqs = prireqcr + reqcr;
396*3c5ac992SScott Long 	sc->num_replies = MIN(sc->max_replyframes + sc->max_evtframes,
397*3c5ac992SScott Long 	    sc->facts->MaxReplyDescriptorPostQueueDepth) - 1;
398*3c5ac992SScott Long 
399*3c5ac992SScott Long 	/*
400*3c5ac992SScott Long 	 * Figure out the number of MSIx-based queues.  If the firmware or
401*3c5ac992SScott Long 	 * user has done something crazy and not allowed enough credit for
402*3c5ac992SScott Long 	 * the queues to be useful then don't enable multi-queue.
403*3c5ac992SScott Long 	 */
404*3c5ac992SScott Long 	if (sc->facts->MaxMSIxVectors < 2)
405*3c5ac992SScott Long 		sc->msi_msgs = 1;
406*3c5ac992SScott Long 
407*3c5ac992SScott Long 	if (sc->msi_msgs > 1) {
408*3c5ac992SScott Long 		sc->msi_msgs = MIN(sc->msi_msgs, mp_ncpus);
409*3c5ac992SScott Long 		sc->msi_msgs = MIN(sc->msi_msgs, sc->facts->MaxMSIxVectors);
410*3c5ac992SScott Long 		if (sc->num_reqs / sc->msi_msgs < 2)
411*3c5ac992SScott Long 			sc->msi_msgs = 1;
412*3c5ac992SScott Long 	}
413*3c5ac992SScott Long 
414*3c5ac992SScott Long 	mpr_dprint(sc, MPR_INIT, "Sized queues to q=%d reqs=%d replies=%d\n",
415*3c5ac992SScott Long 	    sc->msi_msgs, sc->num_reqs, sc->num_replies);
416*3c5ac992SScott Long }
417*3c5ac992SScott Long 
418991554f2SKenneth D. Merry /*
419991554f2SKenneth D. Merry  * This is called during attach and when re-initializing due to a Diag Reset.
420991554f2SKenneth D. Merry  * IOC Facts is used to allocate many of the structures needed by the driver.
421991554f2SKenneth D. Merry  * If called from attach, de-allocation is not required because the driver has
422991554f2SKenneth D. Merry  * not allocated any structures yet, but if called from a Diag Reset, previously
423991554f2SKenneth D. Merry  * allocated structures based on IOC Facts will need to be freed and re-
424991554f2SKenneth D. Merry  * allocated bases on the latest IOC Facts.
425991554f2SKenneth D. Merry  */
426991554f2SKenneth D. Merry static int
427991554f2SKenneth D. Merry mpr_iocfacts_allocate(struct mpr_softc *sc, uint8_t attaching)
428991554f2SKenneth D. Merry {
429a2c14879SStephen McConnell 	int error;
430991554f2SKenneth D. Merry 	Mpi2IOCFactsReply_t saved_facts;
431991554f2SKenneth D. Merry 	uint8_t saved_mode, reallocating;
432991554f2SKenneth D. Merry 
433757ff642SScott Long 	mpr_dprint(sc, MPR_INIT|MPR_TRACE, "%s entered\n", __func__);
434991554f2SKenneth D. Merry 
435991554f2SKenneth D. Merry 	/* Save old IOC Facts and then only reallocate if Facts have changed */
436991554f2SKenneth D. Merry 	if (!attaching) {
437991554f2SKenneth D. Merry 		bcopy(sc->facts, &saved_facts, sizeof(MPI2_IOC_FACTS_REPLY));
438991554f2SKenneth D. Merry 	}
439991554f2SKenneth D. Merry 
440991554f2SKenneth D. Merry 	/*
441991554f2SKenneth D. Merry 	 * Get IOC Facts.  In all cases throughout this function, panic if doing
442991554f2SKenneth D. Merry 	 * a re-initialization and only return the error if attaching so the OS
443991554f2SKenneth D. Merry 	 * can handle it.
444991554f2SKenneth D. Merry 	 */
445991554f2SKenneth D. Merry 	if ((error = mpr_get_iocfacts(sc, sc->facts)) != 0) {
446991554f2SKenneth D. Merry 		if (attaching) {
447757ff642SScott Long 			mpr_dprint(sc, MPR_INIT|MPR_FAULT, "Failed to get "
448757ff642SScott Long 			    "IOC Facts with error %d, exit\n", error);
449991554f2SKenneth D. Merry 			return (error);
450991554f2SKenneth D. Merry 		} else {
451991554f2SKenneth D. Merry 			panic("%s failed to get IOC Facts with error %d\n",
452991554f2SKenneth D. Merry 			    __func__, error);
453991554f2SKenneth D. Merry 		}
454991554f2SKenneth D. Merry 	}
455991554f2SKenneth D. Merry 
456055e2653SScott Long 	MPR_DPRINT_PAGE(sc, MPR_XINFO, iocfacts, sc->facts);
457991554f2SKenneth D. Merry 
458991554f2SKenneth D. Merry 	snprintf(sc->fw_version, sizeof(sc->fw_version),
459991554f2SKenneth D. Merry 	    "%02d.%02d.%02d.%02d",
460991554f2SKenneth D. Merry 	    sc->facts->FWVersion.Struct.Major,
461991554f2SKenneth D. Merry 	    sc->facts->FWVersion.Struct.Minor,
462991554f2SKenneth D. Merry 	    sc->facts->FWVersion.Struct.Unit,
463991554f2SKenneth D. Merry 	    sc->facts->FWVersion.Struct.Dev);
464991554f2SKenneth D. Merry 
465757ff642SScott Long 	mpr_dprint(sc, MPR_INFO, "Firmware: %s, Driver: %s\n", sc->fw_version,
466991554f2SKenneth D. Merry 	    MPR_DRIVER_VERSION);
467757ff642SScott Long 	mpr_dprint(sc, MPR_INFO,
468757ff642SScott Long 	    "IOCCapabilities: %b\n", sc->facts->IOCCapabilities,
469991554f2SKenneth D. Merry 	    "\20" "\3ScsiTaskFull" "\4DiagTrace" "\5SnapBuf" "\6ExtBuf"
470991554f2SKenneth D. Merry 	    "\7EEDP" "\10BiDirTarg" "\11Multicast" "\14TransRetry" "\15IR"
47167feec50SStephen McConnell 	    "\16EventReplay" "\17RaidAccel" "\20MSIXIndex" "\21HostDisc"
47267feec50SStephen McConnell 	    "\22FastPath" "\23RDPQArray" "\24AtomicReqDesc" "\25PCIeSRIOV");
473991554f2SKenneth D. Merry 
474991554f2SKenneth D. Merry 	/*
475991554f2SKenneth D. Merry 	 * If the chip doesn't support event replay then a hard reset will be
476991554f2SKenneth D. Merry 	 * required to trigger a full discovery.  Do the reset here then
477991554f2SKenneth D. Merry 	 * retransition to Ready.  A hard reset might have already been done,
478991554f2SKenneth D. Merry 	 * but it doesn't hurt to do it again.  Only do this if attaching, not
479991554f2SKenneth D. Merry 	 * for a Diag Reset.
480991554f2SKenneth D. Merry 	 */
481757ff642SScott Long 	if (attaching && ((sc->facts->IOCCapabilities &
482757ff642SScott Long 	    MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY) == 0)) {
483757ff642SScott Long 		mpr_dprint(sc, MPR_INIT, "No event replay, resetting\n");
484991554f2SKenneth D. Merry 		mpr_diag_reset(sc, NO_SLEEP);
485991554f2SKenneth D. Merry 		if ((error = mpr_transition_ready(sc)) != 0) {
486757ff642SScott Long 			mpr_dprint(sc, MPR_INIT|MPR_FAULT, "Failed to "
487757ff642SScott Long 			    "transition to ready with error %d, exit\n",
488757ff642SScott Long 			    error);
489991554f2SKenneth D. Merry 			return (error);
490991554f2SKenneth D. Merry 		}
491991554f2SKenneth D. Merry 	}
492991554f2SKenneth D. Merry 
493991554f2SKenneth D. Merry 	/*
494991554f2SKenneth D. Merry 	 * Set flag if IR Firmware is loaded.  If the RAID Capability has
495991554f2SKenneth D. Merry 	 * changed from the previous IOC Facts, log a warning, but only if
496991554f2SKenneth D. Merry 	 * checking this after a Diag Reset and not during attach.
497991554f2SKenneth D. Merry 	 */
498991554f2SKenneth D. Merry 	saved_mode = sc->ir_firmware;
499991554f2SKenneth D. Merry 	if (sc->facts->IOCCapabilities &
500991554f2SKenneth D. Merry 	    MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID)
501991554f2SKenneth D. Merry 		sc->ir_firmware = 1;
502991554f2SKenneth D. Merry 	if (!attaching) {
503991554f2SKenneth D. Merry 		if (sc->ir_firmware != saved_mode) {
504757ff642SScott Long 			mpr_dprint(sc, MPR_INIT|MPR_FAULT, "new IR/IT mode "
505757ff642SScott Long 			    "in IOC Facts does not match previous mode\n");
506991554f2SKenneth D. Merry 		}
507991554f2SKenneth D. Merry 	}
508991554f2SKenneth D. Merry 
509991554f2SKenneth D. Merry 	/* Only deallocate and reallocate if relevant IOC Facts have changed */
510991554f2SKenneth D. Merry 	reallocating = FALSE;
5116d4ffcb4SKenneth D. Merry 	sc->mpr_flags &= ~MPR_FLAGS_REALLOCATED;
5126d4ffcb4SKenneth D. Merry 
513991554f2SKenneth D. Merry 	if ((!attaching) &&
514991554f2SKenneth D. Merry 	    ((saved_facts.MsgVersion != sc->facts->MsgVersion) ||
515991554f2SKenneth D. Merry 	    (saved_facts.HeaderVersion != sc->facts->HeaderVersion) ||
516991554f2SKenneth D. Merry 	    (saved_facts.MaxChainDepth != sc->facts->MaxChainDepth) ||
517991554f2SKenneth D. Merry 	    (saved_facts.RequestCredit != sc->facts->RequestCredit) ||
518991554f2SKenneth D. Merry 	    (saved_facts.ProductID != sc->facts->ProductID) ||
519991554f2SKenneth D. Merry 	    (saved_facts.IOCCapabilities != sc->facts->IOCCapabilities) ||
520991554f2SKenneth D. Merry 	    (saved_facts.IOCRequestFrameSize !=
521991554f2SKenneth D. Merry 	    sc->facts->IOCRequestFrameSize) ||
5222bbc5fcbSStephen McConnell 	    (saved_facts.IOCMaxChainSegmentSize !=
5232bbc5fcbSStephen McConnell 	    sc->facts->IOCMaxChainSegmentSize) ||
524991554f2SKenneth D. Merry 	    (saved_facts.MaxTargets != sc->facts->MaxTargets) ||
525991554f2SKenneth D. Merry 	    (saved_facts.MaxSasExpanders != sc->facts->MaxSasExpanders) ||
526991554f2SKenneth D. Merry 	    (saved_facts.MaxEnclosures != sc->facts->MaxEnclosures) ||
527991554f2SKenneth D. Merry 	    (saved_facts.HighPriorityCredit != sc->facts->HighPriorityCredit) ||
528991554f2SKenneth D. Merry 	    (saved_facts.MaxReplyDescriptorPostQueueDepth !=
529991554f2SKenneth D. Merry 	    sc->facts->MaxReplyDescriptorPostQueueDepth) ||
530991554f2SKenneth D. Merry 	    (saved_facts.ReplyFrameSize != sc->facts->ReplyFrameSize) ||
531991554f2SKenneth D. Merry 	    (saved_facts.MaxVolumes != sc->facts->MaxVolumes) ||
532991554f2SKenneth D. Merry 	    (saved_facts.MaxPersistentEntries !=
533991554f2SKenneth D. Merry 	    sc->facts->MaxPersistentEntries))) {
534991554f2SKenneth D. Merry 		reallocating = TRUE;
5356d4ffcb4SKenneth D. Merry 
5366d4ffcb4SKenneth D. Merry 		/* Record that we reallocated everything */
5376d4ffcb4SKenneth D. Merry 		sc->mpr_flags |= MPR_FLAGS_REALLOCATED;
538991554f2SKenneth D. Merry 	}
539991554f2SKenneth D. Merry 
540991554f2SKenneth D. Merry 	/*
541991554f2SKenneth D. Merry 	 * Some things should be done if attaching or re-allocating after a Diag
542991554f2SKenneth D. Merry 	 * Reset, but are not needed after a Diag Reset if the FW has not
543991554f2SKenneth D. Merry 	 * changed.
544991554f2SKenneth D. Merry 	 */
545991554f2SKenneth D. Merry 	if (attaching || reallocating) {
546991554f2SKenneth D. Merry 		/*
547991554f2SKenneth D. Merry 		 * Check if controller supports FW diag buffers and set flag to
548991554f2SKenneth D. Merry 		 * enable each type.
549991554f2SKenneth D. Merry 		 */
550991554f2SKenneth D. Merry 		if (sc->facts->IOCCapabilities &
551991554f2SKenneth D. Merry 		    MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
552991554f2SKenneth D. Merry 			sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_TRACE].
553991554f2SKenneth D. Merry 			    enabled = TRUE;
554991554f2SKenneth D. Merry 		if (sc->facts->IOCCapabilities &
555991554f2SKenneth D. Merry 		    MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
556991554f2SKenneth D. Merry 			sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_SNAPSHOT].
557991554f2SKenneth D. Merry 			    enabled = TRUE;
558991554f2SKenneth D. Merry 		if (sc->facts->IOCCapabilities &
559991554f2SKenneth D. Merry 		    MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
560991554f2SKenneth D. Merry 			sc->fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_EXTENDED].
561991554f2SKenneth D. Merry 			    enabled = TRUE;
562991554f2SKenneth D. Merry 
563991554f2SKenneth D. Merry 		/*
56467feec50SStephen McConnell 		 * Set flags for some supported items.
565991554f2SKenneth D. Merry 		 */
566991554f2SKenneth D. Merry 		if (sc->facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP)
567991554f2SKenneth D. Merry 			sc->eedp_enabled = TRUE;
568991554f2SKenneth D. Merry 		if (sc->facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR)
569991554f2SKenneth D. Merry 			sc->control_TLR = TRUE;
57067feec50SStephen McConnell 		if (sc->facts->IOCCapabilities &
57167feec50SStephen McConnell 		    MPI26_IOCFACTS_CAPABILITY_ATOMIC_REQ)
57267feec50SStephen McConnell 			sc->atomic_desc_capable = TRUE;
573991554f2SKenneth D. Merry 
574*3c5ac992SScott Long 		mpr_resize_queues(sc);
575991554f2SKenneth D. Merry 
576991554f2SKenneth D. Merry 		/*
577991554f2SKenneth D. Merry 		 * Initialize all Tail Queues
578991554f2SKenneth D. Merry 		 */
579991554f2SKenneth D. Merry 		TAILQ_INIT(&sc->req_list);
580991554f2SKenneth D. Merry 		TAILQ_INIT(&sc->high_priority_req_list);
581991554f2SKenneth D. Merry 		TAILQ_INIT(&sc->chain_list);
58267feec50SStephen McConnell 		TAILQ_INIT(&sc->prp_page_list);
583991554f2SKenneth D. Merry 		TAILQ_INIT(&sc->tm_list);
584991554f2SKenneth D. Merry 	}
585991554f2SKenneth D. Merry 
586991554f2SKenneth D. Merry 	/*
587991554f2SKenneth D. Merry 	 * If doing a Diag Reset and the FW is significantly different
588991554f2SKenneth D. Merry 	 * (reallocating will be set above in IOC Facts comparison), then all
589991554f2SKenneth D. Merry 	 * buffers based on the IOC Facts will need to be freed before they are
590991554f2SKenneth D. Merry 	 * reallocated.
591991554f2SKenneth D. Merry 	 */
592991554f2SKenneth D. Merry 	if (reallocating) {
593991554f2SKenneth D. Merry 		mpr_iocfacts_free(sc);
594327f2e6cSStephen McConnell 		mprsas_realloc_targets(sc, saved_facts.MaxTargets +
595327f2e6cSStephen McConnell 		    saved_facts.MaxVolumes);
596991554f2SKenneth D. Merry 	}
597991554f2SKenneth D. Merry 
598991554f2SKenneth D. Merry 	/*
599991554f2SKenneth D. Merry 	 * Any deallocation has been completed.  Now start reallocating
600991554f2SKenneth D. Merry 	 * if needed.  Will only need to reallocate if attaching or if the new
601991554f2SKenneth D. Merry 	 * IOC Facts are different from the previous IOC Facts after a Diag
602991554f2SKenneth D. Merry 	 * Reset. Targets have already been allocated above if needed.
603991554f2SKenneth D. Merry 	 */
6041415db6cSScott Long 	error = 0;
6051415db6cSScott Long 	while (attaching || reallocating) {
6061415db6cSScott Long 		if ((error = mpr_alloc_hw_queues(sc)) != 0)
6071415db6cSScott Long 			break;
6081415db6cSScott Long 		if ((error = mpr_alloc_replies(sc)) != 0)
6091415db6cSScott Long 			break;
6101415db6cSScott Long 		if ((error = mpr_alloc_requests(sc)) != 0)
6111415db6cSScott Long 			break;
6121415db6cSScott Long 		if ((error = mpr_alloc_queues(sc)) != 0)
6131415db6cSScott Long 			break;
6141415db6cSScott Long 		break;
6151415db6cSScott Long 	}
6161415db6cSScott Long 	if (error) {
617757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR,
6181415db6cSScott Long 		    "Failed to alloc queues with error %d\n", error);
619991554f2SKenneth D. Merry 		mpr_free(sc);
620991554f2SKenneth D. Merry 		return (error);
621991554f2SKenneth D. Merry 	}
622991554f2SKenneth D. Merry 
623991554f2SKenneth D. Merry 	/* Always initialize the queues */
624991554f2SKenneth D. Merry 	bzero(sc->free_queue, sc->fqdepth * 4);
625991554f2SKenneth D. Merry 	mpr_init_queues(sc);
626991554f2SKenneth D. Merry 
627991554f2SKenneth D. Merry 	/*
628991554f2SKenneth D. Merry 	 * Always get the chip out of the reset state, but only panic if not
629991554f2SKenneth D. Merry 	 * attaching.  If attaching and there is an error, that is handled by
630991554f2SKenneth D. Merry 	 * the OS.
631991554f2SKenneth D. Merry 	 */
632991554f2SKenneth D. Merry 	error = mpr_transition_operational(sc);
633991554f2SKenneth D. Merry 	if (error != 0) {
634757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_FAULT, "Failed to "
635757ff642SScott Long 		    "transition to operational with error %d\n", error);
636991554f2SKenneth D. Merry 		mpr_free(sc);
637991554f2SKenneth D. Merry 		return (error);
638991554f2SKenneth D. Merry 	}
639991554f2SKenneth D. Merry 
640991554f2SKenneth D. Merry 	/*
641991554f2SKenneth D. Merry 	 * Finish the queue initialization.
642991554f2SKenneth D. Merry 	 * These are set here instead of in mpr_init_queues() because the
643991554f2SKenneth D. Merry 	 * IOC resets these values during the state transition in
644991554f2SKenneth D. Merry 	 * mpr_transition_operational().  The free index is set to 1
645991554f2SKenneth D. Merry 	 * because the corresponding index in the IOC is set to 0, and the
646991554f2SKenneth D. Merry 	 * IOC treats the queues as full if both are set to the same value.
647991554f2SKenneth D. Merry 	 * Hence the reason that the queue can't hold all of the possible
648991554f2SKenneth D. Merry 	 * replies.
649991554f2SKenneth D. Merry 	 */
650991554f2SKenneth D. Merry 	sc->replypostindex = 0;
651991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_REPLY_FREE_HOST_INDEX_OFFSET, sc->replyfreeindex);
652991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_REPLY_POST_HOST_INDEX_OFFSET, 0);
653991554f2SKenneth D. Merry 
654991554f2SKenneth D. Merry 	/*
655991554f2SKenneth D. Merry 	 * Attach the subsystems so they can prepare their event masks.
6561415db6cSScott Long 	 * XXX Should be dynamic so that IM/IR and user modules can attach
657991554f2SKenneth D. Merry 	 */
6581415db6cSScott Long 	error = 0;
6591415db6cSScott Long 	while (attaching) {
660757ff642SScott Long 		mpr_dprint(sc, MPR_INIT, "Attaching subsystems\n");
6611415db6cSScott Long 		if ((error = mpr_attach_log(sc)) != 0)
6621415db6cSScott Long 			break;
6631415db6cSScott Long 		if ((error = mpr_attach_sas(sc)) != 0)
6641415db6cSScott Long 			break;
6651415db6cSScott Long 		if ((error = mpr_attach_user(sc)) != 0)
6661415db6cSScott Long 			break;
6671415db6cSScott Long 		break;
6681415db6cSScott Long 	}
6691415db6cSScott Long 	if (error) {
670757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR,
6711415db6cSScott Long 		    "Failed to attach all subsystems: error %d\n", error);
672991554f2SKenneth D. Merry 		mpr_free(sc);
673991554f2SKenneth D. Merry 		return (error);
674991554f2SKenneth D. Merry 	}
675991554f2SKenneth D. Merry 
676991554f2SKenneth D. Merry 	if ((error = mpr_pci_setup_interrupts(sc)) != 0) {
677757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR,
678757ff642SScott Long 		    "Failed to setup interrupts\n");
679991554f2SKenneth D. Merry 		mpr_free(sc);
680991554f2SKenneth D. Merry 		return (error);
681991554f2SKenneth D. Merry 	}
682991554f2SKenneth D. Merry 
683991554f2SKenneth D. Merry 	return (error);
684991554f2SKenneth D. Merry }
685991554f2SKenneth D. Merry 
686991554f2SKenneth D. Merry /*
687991554f2SKenneth D. Merry  * This is called if memory is being free (during detach for example) and when
688991554f2SKenneth D. Merry  * buffers need to be reallocated due to a Diag Reset.
689991554f2SKenneth D. Merry  */
690991554f2SKenneth D. Merry static void
691991554f2SKenneth D. Merry mpr_iocfacts_free(struct mpr_softc *sc)
692991554f2SKenneth D. Merry {
693991554f2SKenneth D. Merry 	struct mpr_command *cm;
694991554f2SKenneth D. Merry 	int i;
695991554f2SKenneth D. Merry 
696991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
697991554f2SKenneth D. Merry 
698991554f2SKenneth D. Merry 	if (sc->free_busaddr != 0)
699991554f2SKenneth D. Merry 		bus_dmamap_unload(sc->queues_dmat, sc->queues_map);
700991554f2SKenneth D. Merry 	if (sc->free_queue != NULL)
701991554f2SKenneth D. Merry 		bus_dmamem_free(sc->queues_dmat, sc->free_queue,
702991554f2SKenneth D. Merry 		    sc->queues_map);
703991554f2SKenneth D. Merry 	if (sc->queues_dmat != NULL)
704991554f2SKenneth D. Merry 		bus_dma_tag_destroy(sc->queues_dmat);
705991554f2SKenneth D. Merry 
706991554f2SKenneth D. Merry 	if (sc->chain_busaddr != 0)
707991554f2SKenneth D. Merry 		bus_dmamap_unload(sc->chain_dmat, sc->chain_map);
708991554f2SKenneth D. Merry 	if (sc->chain_frames != NULL)
709991554f2SKenneth D. Merry 		bus_dmamem_free(sc->chain_dmat, sc->chain_frames,
710991554f2SKenneth D. Merry 		    sc->chain_map);
711991554f2SKenneth D. Merry 	if (sc->chain_dmat != NULL)
712991554f2SKenneth D. Merry 		bus_dma_tag_destroy(sc->chain_dmat);
713991554f2SKenneth D. Merry 
714991554f2SKenneth D. Merry 	if (sc->sense_busaddr != 0)
715991554f2SKenneth D. Merry 		bus_dmamap_unload(sc->sense_dmat, sc->sense_map);
716991554f2SKenneth D. Merry 	if (sc->sense_frames != NULL)
717991554f2SKenneth D. Merry 		bus_dmamem_free(sc->sense_dmat, sc->sense_frames,
718991554f2SKenneth D. Merry 		    sc->sense_map);
719991554f2SKenneth D. Merry 	if (sc->sense_dmat != NULL)
720991554f2SKenneth D. Merry 		bus_dma_tag_destroy(sc->sense_dmat);
721991554f2SKenneth D. Merry 
72267feec50SStephen McConnell 	if (sc->prp_page_busaddr != 0)
72367feec50SStephen McConnell 		bus_dmamap_unload(sc->prp_page_dmat, sc->prp_page_map);
72467feec50SStephen McConnell 	if (sc->prp_pages != NULL)
72567feec50SStephen McConnell 		bus_dmamem_free(sc->prp_page_dmat, sc->prp_pages,
72667feec50SStephen McConnell 		    sc->prp_page_map);
72767feec50SStephen McConnell 	if (sc->prp_page_dmat != NULL)
72867feec50SStephen McConnell 		bus_dma_tag_destroy(sc->prp_page_dmat);
72967feec50SStephen McConnell 
730991554f2SKenneth D. Merry 	if (sc->reply_busaddr != 0)
731991554f2SKenneth D. Merry 		bus_dmamap_unload(sc->reply_dmat, sc->reply_map);
732991554f2SKenneth D. Merry 	if (sc->reply_frames != NULL)
733991554f2SKenneth D. Merry 		bus_dmamem_free(sc->reply_dmat, sc->reply_frames,
734991554f2SKenneth D. Merry 		    sc->reply_map);
735991554f2SKenneth D. Merry 	if (sc->reply_dmat != NULL)
736991554f2SKenneth D. Merry 		bus_dma_tag_destroy(sc->reply_dmat);
737991554f2SKenneth D. Merry 
738991554f2SKenneth D. Merry 	if (sc->req_busaddr != 0)
739991554f2SKenneth D. Merry 		bus_dmamap_unload(sc->req_dmat, sc->req_map);
740991554f2SKenneth D. Merry 	if (sc->req_frames != NULL)
741991554f2SKenneth D. Merry 		bus_dmamem_free(sc->req_dmat, sc->req_frames, sc->req_map);
742991554f2SKenneth D. Merry 	if (sc->req_dmat != NULL)
743991554f2SKenneth D. Merry 		bus_dma_tag_destroy(sc->req_dmat);
744991554f2SKenneth D. Merry 
745991554f2SKenneth D. Merry 	if (sc->chains != NULL)
746991554f2SKenneth D. Merry 		free(sc->chains, M_MPR);
74767feec50SStephen McConnell 	if (sc->prps != NULL)
74867feec50SStephen McConnell 		free(sc->prps, M_MPR);
749991554f2SKenneth D. Merry 	if (sc->commands != NULL) {
750991554f2SKenneth D. Merry 		for (i = 1; i < sc->num_reqs; i++) {
751991554f2SKenneth D. Merry 			cm = &sc->commands[i];
752991554f2SKenneth D. Merry 			bus_dmamap_destroy(sc->buffer_dmat, cm->cm_dmamap);
753991554f2SKenneth D. Merry 		}
754991554f2SKenneth D. Merry 		free(sc->commands, M_MPR);
755991554f2SKenneth D. Merry 	}
756991554f2SKenneth D. Merry 	if (sc->buffer_dmat != NULL)
757991554f2SKenneth D. Merry 		bus_dma_tag_destroy(sc->buffer_dmat);
758bec09074SScott Long 
759bec09074SScott Long 	mpr_pci_free_interrupts(sc);
760bec09074SScott Long 	free(sc->queues, M_MPR);
761bec09074SScott Long 	sc->queues = NULL;
762991554f2SKenneth D. Merry }
763991554f2SKenneth D. Merry 
764991554f2SKenneth D. Merry /*
765991554f2SKenneth D. Merry  * The terms diag reset and hard reset are used interchangeably in the MPI
766991554f2SKenneth D. Merry  * docs to mean resetting the controller chip.  In this code diag reset
767991554f2SKenneth D. Merry  * cleans everything up, and the hard reset function just sends the reset
768991554f2SKenneth D. Merry  * sequence to the chip.  This should probably be refactored so that every
769991554f2SKenneth D. Merry  * subsystem gets a reset notification of some sort, and can clean up
770991554f2SKenneth D. Merry  * appropriately.
771991554f2SKenneth D. Merry  */
772991554f2SKenneth D. Merry int
773991554f2SKenneth D. Merry mpr_reinit(struct mpr_softc *sc)
774991554f2SKenneth D. Merry {
775991554f2SKenneth D. Merry 	int error;
776991554f2SKenneth D. Merry 	struct mprsas_softc *sassc;
777991554f2SKenneth D. Merry 
778991554f2SKenneth D. Merry 	sassc = sc->sassc;
779991554f2SKenneth D. Merry 
780991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
781991554f2SKenneth D. Merry 
782991554f2SKenneth D. Merry 	mtx_assert(&sc->mpr_mtx, MA_OWNED);
783991554f2SKenneth D. Merry 
784757ff642SScott Long 	mpr_dprint(sc, MPR_INIT|MPR_INFO, "Reinitializing controller\n");
785991554f2SKenneth D. Merry 	if (sc->mpr_flags & MPR_FLAGS_DIAGRESET) {
786757ff642SScott Long 		mpr_dprint(sc, MPR_INIT, "Reset already in progress\n");
787991554f2SKenneth D. Merry 		return 0;
788991554f2SKenneth D. Merry 	}
789991554f2SKenneth D. Merry 
790757ff642SScott Long 	/*
791757ff642SScott Long 	 * Make sure the completion callbacks can recognize they're getting
792991554f2SKenneth D. Merry 	 * a NULL cm_reply due to a reset.
793991554f2SKenneth D. Merry 	 */
794991554f2SKenneth D. Merry 	sc->mpr_flags |= MPR_FLAGS_DIAGRESET;
795991554f2SKenneth D. Merry 
796991554f2SKenneth D. Merry 	/*
797991554f2SKenneth D. Merry 	 * Mask interrupts here.
798991554f2SKenneth D. Merry 	 */
799757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "Masking interrupts and resetting\n");
800991554f2SKenneth D. Merry 	mpr_mask_intr(sc);
801991554f2SKenneth D. Merry 
802991554f2SKenneth D. Merry 	error = mpr_diag_reset(sc, CAN_SLEEP);
803991554f2SKenneth D. Merry 	if (error != 0) {
804991554f2SKenneth D. Merry 		panic("%s hard reset failed with error %d\n", __func__, error);
805991554f2SKenneth D. Merry 	}
806991554f2SKenneth D. Merry 
807991554f2SKenneth D. Merry 	/* Restore the PCI state, including the MSI-X registers */
808991554f2SKenneth D. Merry 	mpr_pci_restore(sc);
809991554f2SKenneth D. Merry 
810991554f2SKenneth D. Merry 	/* Give the I/O subsystem special priority to get itself prepared */
811991554f2SKenneth D. Merry 	mprsas_handle_reinit(sc);
812991554f2SKenneth D. Merry 
813991554f2SKenneth D. Merry 	/*
814991554f2SKenneth D. Merry 	 * Get IOC Facts and allocate all structures based on this information.
815991554f2SKenneth D. Merry 	 * The attach function will also call mpr_iocfacts_allocate at startup.
816991554f2SKenneth D. Merry 	 * If relevant values have changed in IOC Facts, this function will free
817991554f2SKenneth D. Merry 	 * all of the memory based on IOC Facts and reallocate that memory.
818991554f2SKenneth D. Merry 	 */
819991554f2SKenneth D. Merry 	if ((error = mpr_iocfacts_allocate(sc, FALSE)) != 0) {
820991554f2SKenneth D. Merry 		panic("%s IOC Facts based allocation failed with error %d\n",
821991554f2SKenneth D. Merry 		    __func__, error);
822991554f2SKenneth D. Merry 	}
823991554f2SKenneth D. Merry 
824991554f2SKenneth D. Merry 	/*
825991554f2SKenneth D. Merry 	 * Mapping structures will be re-allocated after getting IOC Page8, so
826991554f2SKenneth D. Merry 	 * free these structures here.
827991554f2SKenneth D. Merry 	 */
828991554f2SKenneth D. Merry 	mpr_mapping_exit(sc);
829991554f2SKenneth D. Merry 
830991554f2SKenneth D. Merry 	/*
831991554f2SKenneth D. Merry 	 * The static page function currently read is IOC Page8.  Others can be
832991554f2SKenneth D. Merry 	 * added in future.  It's possible that the values in IOC Page8 have
833991554f2SKenneth D. Merry 	 * changed after a Diag Reset due to user modification, so always read
834991554f2SKenneth D. Merry 	 * these.  Interrupts are masked, so unmask them before getting config
835991554f2SKenneth D. Merry 	 * pages.
836991554f2SKenneth D. Merry 	 */
837991554f2SKenneth D. Merry 	mpr_unmask_intr(sc);
838991554f2SKenneth D. Merry 	sc->mpr_flags &= ~MPR_FLAGS_DIAGRESET;
839991554f2SKenneth D. Merry 	mpr_base_static_config_pages(sc);
840991554f2SKenneth D. Merry 
841991554f2SKenneth D. Merry 	/*
842991554f2SKenneth D. Merry 	 * Some mapping info is based in IOC Page8 data, so re-initialize the
843991554f2SKenneth D. Merry 	 * mapping tables.
844991554f2SKenneth D. Merry 	 */
845991554f2SKenneth D. Merry 	mpr_mapping_initialize(sc);
846991554f2SKenneth D. Merry 
847991554f2SKenneth D. Merry 	/*
848991554f2SKenneth D. Merry 	 * Restart will reload the event masks clobbered by the reset, and
849991554f2SKenneth D. Merry 	 * then enable the port.
850991554f2SKenneth D. Merry 	 */
851991554f2SKenneth D. Merry 	mpr_reregister_events(sc);
852991554f2SKenneth D. Merry 
853991554f2SKenneth D. Merry 	/* the end of discovery will release the simq, so we're done. */
854757ff642SScott Long 	mpr_dprint(sc, MPR_INIT|MPR_XINFO, "Finished sc %p post %u free %u\n",
855757ff642SScott Long 	    sc, sc->replypostindex, sc->replyfreeindex);
856991554f2SKenneth D. Merry 	mprsas_release_simq_reinit(sassc);
857757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit error= %d\n", __func__, error);
858991554f2SKenneth D. Merry 
859991554f2SKenneth D. Merry 	return 0;
860991554f2SKenneth D. Merry }
861991554f2SKenneth D. Merry 
862991554f2SKenneth D. Merry /* Wait for the chip to ACK a word that we've put into its FIFO
863991554f2SKenneth D. Merry  * Wait for <timeout> seconds. In single loop wait for busy loop
864991554f2SKenneth D. Merry  * for 500 microseconds.
865991554f2SKenneth D. Merry  * Total is [ 0.5 * (2000 * <timeout>) ] in miliseconds.
866991554f2SKenneth D. Merry  * */
867991554f2SKenneth D. Merry static int
868991554f2SKenneth D. Merry mpr_wait_db_ack(struct mpr_softc *sc, int timeout, int sleep_flag)
869991554f2SKenneth D. Merry {
870991554f2SKenneth D. Merry 	u32 cntdn, count;
871991554f2SKenneth D. Merry 	u32 int_status;
872991554f2SKenneth D. Merry 	u32 doorbell;
873991554f2SKenneth D. Merry 
874991554f2SKenneth D. Merry 	count = 0;
875991554f2SKenneth D. Merry 	cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
876991554f2SKenneth D. Merry 	do {
877991554f2SKenneth D. Merry 		int_status = mpr_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET);
878991554f2SKenneth D. Merry 		if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
879757ff642SScott Long 			mpr_dprint(sc, MPR_TRACE, "%s: successful count(%d), "
880991554f2SKenneth D. Merry 			    "timeout(%d)\n", __func__, count, timeout);
881991554f2SKenneth D. Merry 			return 0;
882991554f2SKenneth D. Merry 		} else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
883991554f2SKenneth D. Merry 			doorbell = mpr_regread(sc, MPI2_DOORBELL_OFFSET);
884991554f2SKenneth D. Merry 			if ((doorbell & MPI2_IOC_STATE_MASK) ==
885991554f2SKenneth D. Merry 			    MPI2_IOC_STATE_FAULT) {
886991554f2SKenneth D. Merry 				mpr_dprint(sc, MPR_FAULT,
887991554f2SKenneth D. Merry 				    "fault_state(0x%04x)!\n", doorbell);
888991554f2SKenneth D. Merry 				return (EFAULT);
889991554f2SKenneth D. Merry 			}
890991554f2SKenneth D. Merry 		} else if (int_status == 0xFFFFFFFF)
891991554f2SKenneth D. Merry 			goto out;
892991554f2SKenneth D. Merry 
893991554f2SKenneth D. Merry 		/*
894991554f2SKenneth D. Merry 		 * If it can sleep, sleep for 1 milisecond, else busy loop for
895991554f2SKenneth D. Merry  		 * 0.5 milisecond
896991554f2SKenneth D. Merry 		 */
897991554f2SKenneth D. Merry 		if (mtx_owned(&sc->mpr_mtx) && sleep_flag == CAN_SLEEP)
898a2c14879SStephen McConnell 			msleep(&sc->msleep_fake_chan, &sc->mpr_mtx, 0, "mprdba",
899a2c14879SStephen McConnell 			    hz/1000);
900991554f2SKenneth D. Merry 		else if (sleep_flag == CAN_SLEEP)
901991554f2SKenneth D. Merry 			pause("mprdba", hz/1000);
902991554f2SKenneth D. Merry 		else
903991554f2SKenneth D. Merry 			DELAY(500);
904991554f2SKenneth D. Merry 		count++;
905991554f2SKenneth D. Merry 	} while (--cntdn);
906991554f2SKenneth D. Merry 
907991554f2SKenneth D. Merry out:
908991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_FAULT, "%s: failed due to timeout count(%d), "
909991554f2SKenneth D. Merry 		"int_status(%x)!\n", __func__, count, int_status);
910991554f2SKenneth D. Merry 	return (ETIMEDOUT);
911991554f2SKenneth D. Merry }
912991554f2SKenneth D. Merry 
913991554f2SKenneth D. Merry /* Wait for the chip to signal that the next word in its FIFO can be fetched */
914991554f2SKenneth D. Merry static int
915991554f2SKenneth D. Merry mpr_wait_db_int(struct mpr_softc *sc)
916991554f2SKenneth D. Merry {
917991554f2SKenneth D. Merry 	int retry;
918991554f2SKenneth D. Merry 
919991554f2SKenneth D. Merry 	for (retry = 0; retry < MPR_DB_MAX_WAIT; retry++) {
920991554f2SKenneth D. Merry 		if ((mpr_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET) &
921991554f2SKenneth D. Merry 		    MPI2_HIS_IOC2SYS_DB_STATUS) != 0)
922991554f2SKenneth D. Merry 			return (0);
923991554f2SKenneth D. Merry 		DELAY(2000);
924991554f2SKenneth D. Merry 	}
925991554f2SKenneth D. Merry 	return (ETIMEDOUT);
926991554f2SKenneth D. Merry }
927991554f2SKenneth D. Merry 
928991554f2SKenneth D. Merry /* Step through the synchronous command state machine, i.e. "Doorbell mode" */
929991554f2SKenneth D. Merry static int
930991554f2SKenneth D. Merry mpr_request_sync(struct mpr_softc *sc, void *req, MPI2_DEFAULT_REPLY *reply,
931991554f2SKenneth D. Merry     int req_sz, int reply_sz, int timeout)
932991554f2SKenneth D. Merry {
933991554f2SKenneth D. Merry 	uint32_t *data32;
934991554f2SKenneth D. Merry 	uint16_t *data16;
935991554f2SKenneth D. Merry 	int i, count, ioc_sz, residual;
936991554f2SKenneth D. Merry 	int sleep_flags = CAN_SLEEP;
937991554f2SKenneth D. Merry 
938991554f2SKenneth D. Merry #if __FreeBSD_version >= 1000029
939991554f2SKenneth D. Merry 	if (curthread->td_no_sleeping)
940991554f2SKenneth D. Merry #else //__FreeBSD_version < 1000029
941991554f2SKenneth D. Merry 	if (curthread->td_pflags & TDP_NOSLEEPING)
942991554f2SKenneth D. Merry #endif //__FreeBSD_version >= 1000029
943991554f2SKenneth D. Merry 		sleep_flags = NO_SLEEP;
944991554f2SKenneth D. Merry 
945991554f2SKenneth D. Merry 	/* Step 1 */
946991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
947991554f2SKenneth D. Merry 
948991554f2SKenneth D. Merry 	/* Step 2 */
949991554f2SKenneth D. Merry 	if (mpr_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED)
950991554f2SKenneth D. Merry 		return (EBUSY);
951991554f2SKenneth D. Merry 
952991554f2SKenneth D. Merry 	/* Step 3
953991554f2SKenneth D. Merry 	 * Announce that a message is coming through the doorbell.  Messages
954991554f2SKenneth D. Merry 	 * are pushed at 32bit words, so round up if needed.
955991554f2SKenneth D. Merry 	 */
956991554f2SKenneth D. Merry 	count = (req_sz + 3) / 4;
957991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_DOORBELL_OFFSET,
958991554f2SKenneth D. Merry 	    (MPI2_FUNCTION_HANDSHAKE << MPI2_DOORBELL_FUNCTION_SHIFT) |
959991554f2SKenneth D. Merry 	    (count << MPI2_DOORBELL_ADD_DWORDS_SHIFT));
960991554f2SKenneth D. Merry 
961991554f2SKenneth D. Merry 	/* Step 4 */
962991554f2SKenneth D. Merry 	if (mpr_wait_db_int(sc) ||
963991554f2SKenneth D. Merry 	    (mpr_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED) == 0) {
964991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Doorbell failed to activate\n");
965991554f2SKenneth D. Merry 		return (ENXIO);
966991554f2SKenneth D. Merry 	}
967991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
968991554f2SKenneth D. Merry 	if (mpr_wait_db_ack(sc, 5, sleep_flags) != 0) {
969991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Doorbell handshake failed\n");
970991554f2SKenneth D. Merry 		return (ENXIO);
971991554f2SKenneth D. Merry 	}
972991554f2SKenneth D. Merry 
973991554f2SKenneth D. Merry 	/* Step 5 */
974991554f2SKenneth D. Merry 	/* Clock out the message data synchronously in 32-bit dwords*/
975991554f2SKenneth D. Merry 	data32 = (uint32_t *)req;
976991554f2SKenneth D. Merry 	for (i = 0; i < count; i++) {
977991554f2SKenneth D. Merry 		mpr_regwrite(sc, MPI2_DOORBELL_OFFSET, htole32(data32[i]));
978991554f2SKenneth D. Merry 		if (mpr_wait_db_ack(sc, 5, sleep_flags) != 0) {
979991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT,
980991554f2SKenneth D. Merry 			    "Timeout while writing doorbell\n");
981991554f2SKenneth D. Merry 			return (ENXIO);
982991554f2SKenneth D. Merry 		}
983991554f2SKenneth D. Merry 	}
984991554f2SKenneth D. Merry 
985991554f2SKenneth D. Merry 	/* Step 6 */
986991554f2SKenneth D. Merry 	/* Clock in the reply in 16-bit words.  The total length of the
987991554f2SKenneth D. Merry 	 * message is always in the 4th byte, so clock out the first 2 words
988991554f2SKenneth D. Merry 	 * manually, then loop the rest.
989991554f2SKenneth D. Merry 	 */
990991554f2SKenneth D. Merry 	data16 = (uint16_t *)reply;
991991554f2SKenneth D. Merry 	if (mpr_wait_db_int(sc) != 0) {
992991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Timeout reading doorbell 0\n");
993991554f2SKenneth D. Merry 		return (ENXIO);
994991554f2SKenneth D. Merry 	}
995991554f2SKenneth D. Merry 	data16[0] =
996991554f2SKenneth D. Merry 	    mpr_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_DATA_MASK;
997991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
998991554f2SKenneth D. Merry 	if (mpr_wait_db_int(sc) != 0) {
999991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Timeout reading doorbell 1\n");
1000991554f2SKenneth D. Merry 		return (ENXIO);
1001991554f2SKenneth D. Merry 	}
1002991554f2SKenneth D. Merry 	data16[1] =
1003991554f2SKenneth D. Merry 	    mpr_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_DATA_MASK;
1004991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1005991554f2SKenneth D. Merry 
1006991554f2SKenneth D. Merry 	/* Number of 32bit words in the message */
1007991554f2SKenneth D. Merry 	ioc_sz = reply->MsgLength;
1008991554f2SKenneth D. Merry 
1009991554f2SKenneth D. Merry 	/*
1010991554f2SKenneth D. Merry 	 * Figure out how many 16bit words to clock in without overrunning.
1011991554f2SKenneth D. Merry 	 * The precision loss with dividing reply_sz can safely be
1012991554f2SKenneth D. Merry 	 * ignored because the messages can only be multiples of 32bits.
1013991554f2SKenneth D. Merry 	 */
1014991554f2SKenneth D. Merry 	residual = 0;
1015991554f2SKenneth D. Merry 	count = MIN((reply_sz / 4), ioc_sz) * 2;
1016991554f2SKenneth D. Merry 	if (count < ioc_sz * 2) {
1017991554f2SKenneth D. Merry 		residual = ioc_sz * 2 - count;
1018991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "Driver error, throwing away %d "
1019991554f2SKenneth D. Merry 		    "residual message words\n", residual);
1020991554f2SKenneth D. Merry 	}
1021991554f2SKenneth D. Merry 
1022991554f2SKenneth D. Merry 	for (i = 2; i < count; i++) {
1023991554f2SKenneth D. Merry 		if (mpr_wait_db_int(sc) != 0) {
1024991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT,
1025991554f2SKenneth D. Merry 			    "Timeout reading doorbell %d\n", i);
1026991554f2SKenneth D. Merry 			return (ENXIO);
1027991554f2SKenneth D. Merry 		}
1028991554f2SKenneth D. Merry 		data16[i] = mpr_regread(sc, MPI2_DOORBELL_OFFSET) &
1029991554f2SKenneth D. Merry 		    MPI2_DOORBELL_DATA_MASK;
1030991554f2SKenneth D. Merry 		mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1031991554f2SKenneth D. Merry 	}
1032991554f2SKenneth D. Merry 
1033991554f2SKenneth D. Merry 	/*
1034991554f2SKenneth D. Merry 	 * Pull out residual words that won't fit into the provided buffer.
1035991554f2SKenneth D. Merry 	 * This keeps the chip from hanging due to a driver programming
1036991554f2SKenneth D. Merry 	 * error.
1037991554f2SKenneth D. Merry 	 */
1038991554f2SKenneth D. Merry 	while (residual--) {
1039991554f2SKenneth D. Merry 		if (mpr_wait_db_int(sc) != 0) {
1040991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT, "Timeout reading doorbell\n");
1041991554f2SKenneth D. Merry 			return (ENXIO);
1042991554f2SKenneth D. Merry 		}
1043991554f2SKenneth D. Merry 		(void)mpr_regread(sc, MPI2_DOORBELL_OFFSET);
1044991554f2SKenneth D. Merry 		mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1045991554f2SKenneth D. Merry 	}
1046991554f2SKenneth D. Merry 
1047991554f2SKenneth D. Merry 	/* Step 7 */
1048991554f2SKenneth D. Merry 	if (mpr_wait_db_int(sc) != 0) {
1049991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Timeout waiting to exit doorbell\n");
1050991554f2SKenneth D. Merry 		return (ENXIO);
1051991554f2SKenneth D. Merry 	}
1052991554f2SKenneth D. Merry 	if (mpr_regread(sc, MPI2_DOORBELL_OFFSET) & MPI2_DOORBELL_USED)
1053991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Warning, doorbell still active\n");
1054991554f2SKenneth D. Merry 	mpr_regwrite(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET, 0x0);
1055991554f2SKenneth D. Merry 
1056991554f2SKenneth D. Merry 	return (0);
1057991554f2SKenneth D. Merry }
1058991554f2SKenneth D. Merry 
1059991554f2SKenneth D. Merry static void
1060991554f2SKenneth D. Merry mpr_enqueue_request(struct mpr_softc *sc, struct mpr_command *cm)
1061991554f2SKenneth D. Merry {
106267feec50SStephen McConnell 	request_descriptor rd;
1063991554f2SKenneth D. Merry 
1064991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
1065a2c14879SStephen McConnell 	mpr_dprint(sc, MPR_TRACE, "SMID %u cm %p ccb %p\n",
1066991554f2SKenneth D. Merry 	    cm->cm_desc.Default.SMID, cm, cm->cm_ccb);
1067991554f2SKenneth D. Merry 
1068991554f2SKenneth D. Merry 	if (sc->mpr_flags & MPR_FLAGS_ATTACH_DONE && !(sc->mpr_flags &
1069991554f2SKenneth D. Merry 	    MPR_FLAGS_SHUTDOWN))
1070991554f2SKenneth D. Merry 		mtx_assert(&sc->mpr_mtx, MA_OWNED);
1071991554f2SKenneth D. Merry 
1072991554f2SKenneth D. Merry 	if (++sc->io_cmds_active > sc->io_cmds_highwater)
1073991554f2SKenneth D. Merry 		sc->io_cmds_highwater++;
1074991554f2SKenneth D. Merry 
107567feec50SStephen McConnell 	if (sc->atomic_desc_capable) {
107667feec50SStephen McConnell 		rd.u.low = cm->cm_desc.Words.Low;
107767feec50SStephen McConnell 		mpr_regwrite(sc, MPI26_ATOMIC_REQUEST_DESCRIPTOR_POST_OFFSET,
107867feec50SStephen McConnell 		    rd.u.low);
107967feec50SStephen McConnell 	} else {
1080991554f2SKenneth D. Merry 		rd.u.low = cm->cm_desc.Words.Low;
1081991554f2SKenneth D. Merry 		rd.u.high = cm->cm_desc.Words.High;
1082991554f2SKenneth D. Merry 		rd.word = htole64(rd.word);
1083991554f2SKenneth D. Merry 		mpr_regwrite(sc, MPI2_REQUEST_DESCRIPTOR_POST_LOW_OFFSET,
1084991554f2SKenneth D. Merry 		    rd.u.low);
1085991554f2SKenneth D. Merry 		mpr_regwrite(sc, MPI2_REQUEST_DESCRIPTOR_POST_HIGH_OFFSET,
1086991554f2SKenneth D. Merry 		    rd.u.high);
1087991554f2SKenneth D. Merry 	}
108867feec50SStephen McConnell }
1089991554f2SKenneth D. Merry 
1090991554f2SKenneth D. Merry /*
1091991554f2SKenneth D. Merry  * Just the FACTS, ma'am.
1092991554f2SKenneth D. Merry  */
1093991554f2SKenneth D. Merry static int
1094991554f2SKenneth D. Merry mpr_get_iocfacts(struct mpr_softc *sc, MPI2_IOC_FACTS_REPLY *facts)
1095991554f2SKenneth D. Merry {
1096991554f2SKenneth D. Merry 	MPI2_DEFAULT_REPLY *reply;
1097991554f2SKenneth D. Merry 	MPI2_IOC_FACTS_REQUEST request;
1098991554f2SKenneth D. Merry 	int error, req_sz, reply_sz;
1099991554f2SKenneth D. Merry 
1100991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
1101757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
1102991554f2SKenneth D. Merry 
1103991554f2SKenneth D. Merry 	req_sz = sizeof(MPI2_IOC_FACTS_REQUEST);
1104991554f2SKenneth D. Merry 	reply_sz = sizeof(MPI2_IOC_FACTS_REPLY);
1105991554f2SKenneth D. Merry 	reply = (MPI2_DEFAULT_REPLY *)facts;
1106991554f2SKenneth D. Merry 
1107991554f2SKenneth D. Merry 	bzero(&request, req_sz);
1108991554f2SKenneth D. Merry 	request.Function = MPI2_FUNCTION_IOC_FACTS;
1109991554f2SKenneth D. Merry 	error = mpr_request_sync(sc, &request, reply, req_sz, reply_sz, 5);
1110991554f2SKenneth D. Merry 
1111757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit, error= %d\n", __func__, error);
1112991554f2SKenneth D. Merry 	return (error);
1113991554f2SKenneth D. Merry }
1114991554f2SKenneth D. Merry 
1115991554f2SKenneth D. Merry static int
1116991554f2SKenneth D. Merry mpr_send_iocinit(struct mpr_softc *sc)
1117991554f2SKenneth D. Merry {
1118991554f2SKenneth D. Merry 	MPI2_IOC_INIT_REQUEST	init;
1119991554f2SKenneth D. Merry 	MPI2_DEFAULT_REPLY	reply;
1120991554f2SKenneth D. Merry 	int req_sz, reply_sz, error;
1121991554f2SKenneth D. Merry 	struct timeval now;
1122991554f2SKenneth D. Merry 	uint64_t time_in_msec;
1123991554f2SKenneth D. Merry 
1124991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
1125757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
1126991554f2SKenneth D. Merry 
1127991554f2SKenneth D. Merry 	req_sz = sizeof(MPI2_IOC_INIT_REQUEST);
1128991554f2SKenneth D. Merry 	reply_sz = sizeof(MPI2_IOC_INIT_REPLY);
1129991554f2SKenneth D. Merry 	bzero(&init, req_sz);
1130991554f2SKenneth D. Merry 	bzero(&reply, reply_sz);
1131991554f2SKenneth D. Merry 
1132991554f2SKenneth D. Merry 	/*
1133991554f2SKenneth D. Merry 	 * Fill in the init block.  Note that most addresses are
1134991554f2SKenneth D. Merry 	 * deliberately in the lower 32bits of memory.  This is a micro-
1135991554f2SKenneth D. Merry 	 * optimzation for PCI/PCIX, though it's not clear if it helps PCIe.
1136991554f2SKenneth D. Merry 	 */
1137991554f2SKenneth D. Merry 	init.Function = MPI2_FUNCTION_IOC_INIT;
1138991554f2SKenneth D. Merry 	init.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
1139991554f2SKenneth D. Merry 	init.MsgVersion = htole16(MPI2_VERSION);
1140991554f2SKenneth D. Merry 	init.HeaderVersion = htole16(MPI2_HEADER_VERSION);
1141991554f2SKenneth D. Merry 	init.SystemRequestFrameSize = htole16(sc->facts->IOCRequestFrameSize);
1142991554f2SKenneth D. Merry 	init.ReplyDescriptorPostQueueDepth = htole16(sc->pqdepth);
1143991554f2SKenneth D. Merry 	init.ReplyFreeQueueDepth = htole16(sc->fqdepth);
1144991554f2SKenneth D. Merry 	init.SenseBufferAddressHigh = 0;
1145991554f2SKenneth D. Merry 	init.SystemReplyAddressHigh = 0;
1146991554f2SKenneth D. Merry 	init.SystemRequestFrameBaseAddress.High = 0;
1147991554f2SKenneth D. Merry 	init.SystemRequestFrameBaseAddress.Low =
1148991554f2SKenneth D. Merry 	    htole32((uint32_t)sc->req_busaddr);
1149991554f2SKenneth D. Merry 	init.ReplyDescriptorPostQueueAddress.High = 0;
1150991554f2SKenneth D. Merry 	init.ReplyDescriptorPostQueueAddress.Low =
1151991554f2SKenneth D. Merry 	    htole32((uint32_t)sc->post_busaddr);
1152991554f2SKenneth D. Merry 	init.ReplyFreeQueueAddress.High = 0;
1153991554f2SKenneth D. Merry 	init.ReplyFreeQueueAddress.Low = htole32((uint32_t)sc->free_busaddr);
1154991554f2SKenneth D. Merry 	getmicrotime(&now);
1155991554f2SKenneth D. Merry 	time_in_msec = (now.tv_sec * 1000 + now.tv_usec/1000);
1156991554f2SKenneth D. Merry 	init.TimeStamp.High = htole32((time_in_msec >> 32) & 0xFFFFFFFF);
1157991554f2SKenneth D. Merry 	init.TimeStamp.Low = htole32(time_in_msec & 0xFFFFFFFF);
115867feec50SStephen McConnell 	init.HostPageSize = HOST_PAGE_SIZE_4K;
1159991554f2SKenneth D. Merry 
1160991554f2SKenneth D. Merry 	error = mpr_request_sync(sc, &init, &reply, req_sz, reply_sz, 5);
1161991554f2SKenneth D. Merry 	if ((reply.IOCStatus & MPI2_IOCSTATUS_MASK) != MPI2_IOCSTATUS_SUCCESS)
1162991554f2SKenneth D. Merry 		error = ENXIO;
1163991554f2SKenneth D. Merry 
1164991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_INIT, "IOCInit status= 0x%x\n", reply.IOCStatus);
1165757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit\n", __func__);
1166991554f2SKenneth D. Merry 	return (error);
1167991554f2SKenneth D. Merry }
1168991554f2SKenneth D. Merry 
1169991554f2SKenneth D. Merry void
1170991554f2SKenneth D. Merry mpr_memaddr_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1171991554f2SKenneth D. Merry {
1172991554f2SKenneth D. Merry 	bus_addr_t *addr;
1173991554f2SKenneth D. Merry 
1174991554f2SKenneth D. Merry 	addr = arg;
1175991554f2SKenneth D. Merry 	*addr = segs[0].ds_addr;
1176991554f2SKenneth D. Merry }
1177991554f2SKenneth D. Merry 
1178991554f2SKenneth D. Merry static int
1179991554f2SKenneth D. Merry mpr_alloc_queues(struct mpr_softc *sc)
1180991554f2SKenneth D. Merry {
1181bec09074SScott Long 	struct mpr_queue *q;
11821415db6cSScott Long 	int nq, i;
1183bec09074SScott Long 
1184*3c5ac992SScott Long 	nq = sc->msi_msgs;
1185bec09074SScott Long 	mpr_dprint(sc, MPR_INIT|MPR_XINFO, "Allocating %d I/O queues\n", nq);
1186bec09074SScott Long 
1187*3c5ac992SScott Long 	sc->queues = malloc(sizeof(struct mpr_queue) * nq, M_MPR,
1188*3c5ac992SScott Long 	     M_NOWAIT|M_ZERO);
1189bec09074SScott Long 	if (sc->queues == NULL)
1190bec09074SScott Long 		return (ENOMEM);
1191bec09074SScott Long 
1192bec09074SScott Long 	for (i = 0; i < nq; i++) {
1193bec09074SScott Long 		q = &sc->queues[i];
1194bec09074SScott Long 		mpr_dprint(sc, MPR_INIT, "Configuring queue %d %p\n", i, q);
1195bec09074SScott Long 		q->sc = sc;
1196bec09074SScott Long 		q->qnum = i;
1197bec09074SScott Long 	}
11981415db6cSScott Long 	return (0);
11991415db6cSScott Long }
12001415db6cSScott Long 
12011415db6cSScott Long static int
12021415db6cSScott Long mpr_alloc_hw_queues(struct mpr_softc *sc)
12031415db6cSScott Long {
12041415db6cSScott Long 	bus_addr_t queues_busaddr;
12051415db6cSScott Long 	uint8_t *queues;
12061415db6cSScott Long 	int qsize, fqsize, pqsize;
1207991554f2SKenneth D. Merry 
1208991554f2SKenneth D. Merry 	/*
1209991554f2SKenneth D. Merry 	 * The reply free queue contains 4 byte entries in multiples of 16 and
1210991554f2SKenneth D. Merry 	 * aligned on a 16 byte boundary. There must always be an unused entry.
1211991554f2SKenneth D. Merry 	 * This queue supplies fresh reply frames for the firmware to use.
1212991554f2SKenneth D. Merry 	 *
1213991554f2SKenneth D. Merry 	 * The reply descriptor post queue contains 8 byte entries in
1214991554f2SKenneth D. Merry 	 * multiples of 16 and aligned on a 16 byte boundary.  This queue
1215991554f2SKenneth D. Merry 	 * contains filled-in reply frames sent from the firmware to the host.
1216991554f2SKenneth D. Merry 	 *
1217991554f2SKenneth D. Merry 	 * These two queues are allocated together for simplicity.
1218991554f2SKenneth D. Merry 	 */
1219d9c9c81cSPedro F. Giffuni 	sc->fqdepth = roundup2(sc->num_replies + 1, 16);
1220d9c9c81cSPedro F. Giffuni 	sc->pqdepth = roundup2(sc->num_replies + 1, 16);
1221991554f2SKenneth D. Merry 	fqsize= sc->fqdepth * 4;
1222991554f2SKenneth D. Merry 	pqsize = sc->pqdepth * 8;
1223991554f2SKenneth D. Merry 	qsize = fqsize + pqsize;
1224991554f2SKenneth D. Merry 
1225991554f2SKenneth D. Merry         if (bus_dma_tag_create( sc->mpr_parent_dmat,    /* parent */
1226991554f2SKenneth D. Merry 				16, 0,			/* algnmnt, boundary */
1227991554f2SKenneth D. Merry 				BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1228991554f2SKenneth D. Merry 				BUS_SPACE_MAXADDR,	/* highaddr */
1229991554f2SKenneth D. Merry 				NULL, NULL,		/* filter, filterarg */
1230991554f2SKenneth D. Merry                                 qsize,			/* maxsize */
1231991554f2SKenneth D. Merry                                 1,			/* nsegments */
1232991554f2SKenneth D. Merry                                 qsize,			/* maxsegsize */
1233991554f2SKenneth D. Merry                                 0,			/* flags */
1234991554f2SKenneth D. Merry                                 NULL, NULL,		/* lockfunc, lockarg */
1235991554f2SKenneth D. Merry                                 &sc->queues_dmat)) {
1236757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate queues DMA tag\n");
1237991554f2SKenneth D. Merry 		return (ENOMEM);
1238991554f2SKenneth D. Merry         }
1239991554f2SKenneth D. Merry         if (bus_dmamem_alloc(sc->queues_dmat, (void **)&queues, BUS_DMA_NOWAIT,
1240991554f2SKenneth D. Merry 	    &sc->queues_map)) {
1241757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate queues memory\n");
1242991554f2SKenneth D. Merry 		return (ENOMEM);
1243991554f2SKenneth D. Merry         }
1244991554f2SKenneth D. Merry         bzero(queues, qsize);
1245991554f2SKenneth D. Merry         bus_dmamap_load(sc->queues_dmat, sc->queues_map, queues, qsize,
1246991554f2SKenneth D. Merry 	    mpr_memaddr_cb, &queues_busaddr, 0);
1247991554f2SKenneth D. Merry 
1248991554f2SKenneth D. Merry 	sc->free_queue = (uint32_t *)queues;
1249991554f2SKenneth D. Merry 	sc->free_busaddr = queues_busaddr;
1250991554f2SKenneth D. Merry 	sc->post_queue = (MPI2_REPLY_DESCRIPTORS_UNION *)(queues + fqsize);
1251991554f2SKenneth D. Merry 	sc->post_busaddr = queues_busaddr + fqsize;
1252991554f2SKenneth D. Merry 
1253991554f2SKenneth D. Merry 	return (0);
1254991554f2SKenneth D. Merry }
1255991554f2SKenneth D. Merry 
1256991554f2SKenneth D. Merry static int
1257991554f2SKenneth D. Merry mpr_alloc_replies(struct mpr_softc *sc)
1258991554f2SKenneth D. Merry {
1259991554f2SKenneth D. Merry 	int rsize, num_replies;
1260991554f2SKenneth D. Merry 
1261991554f2SKenneth D. Merry 	/*
1262991554f2SKenneth D. Merry 	 * sc->num_replies should be one less than sc->fqdepth.  We need to
1263991554f2SKenneth D. Merry 	 * allocate space for sc->fqdepth replies, but only sc->num_replies
1264991554f2SKenneth D. Merry 	 * replies can be used at once.
1265991554f2SKenneth D. Merry 	 */
1266991554f2SKenneth D. Merry 	num_replies = max(sc->fqdepth, sc->num_replies);
1267991554f2SKenneth D. Merry 
1268991554f2SKenneth D. Merry 	rsize = sc->facts->ReplyFrameSize * num_replies * 4;
1269991554f2SKenneth D. Merry         if (bus_dma_tag_create( sc->mpr_parent_dmat,    /* parent */
1270991554f2SKenneth D. Merry 				4, 0,			/* algnmnt, boundary */
1271991554f2SKenneth D. Merry 				BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1272991554f2SKenneth D. Merry 				BUS_SPACE_MAXADDR,	/* highaddr */
1273991554f2SKenneth D. Merry 				NULL, NULL,		/* filter, filterarg */
1274991554f2SKenneth D. Merry                                 rsize,			/* maxsize */
1275991554f2SKenneth D. Merry                                 1,			/* nsegments */
1276991554f2SKenneth D. Merry                                 rsize,			/* maxsegsize */
1277991554f2SKenneth D. Merry                                 0,			/* flags */
1278991554f2SKenneth D. Merry                                 NULL, NULL,		/* lockfunc, lockarg */
1279991554f2SKenneth D. Merry                                 &sc->reply_dmat)) {
1280757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate replies DMA tag\n");
1281991554f2SKenneth D. Merry 		return (ENOMEM);
1282991554f2SKenneth D. Merry         }
1283991554f2SKenneth D. Merry         if (bus_dmamem_alloc(sc->reply_dmat, (void **)&sc->reply_frames,
1284991554f2SKenneth D. Merry 	    BUS_DMA_NOWAIT, &sc->reply_map)) {
1285757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate replies memory\n");
1286991554f2SKenneth D. Merry 		return (ENOMEM);
1287991554f2SKenneth D. Merry         }
1288991554f2SKenneth D. Merry         bzero(sc->reply_frames, rsize);
1289991554f2SKenneth D. Merry         bus_dmamap_load(sc->reply_dmat, sc->reply_map, sc->reply_frames, rsize,
1290991554f2SKenneth D. Merry 	    mpr_memaddr_cb, &sc->reply_busaddr, 0);
1291991554f2SKenneth D. Merry 
1292991554f2SKenneth D. Merry 	return (0);
1293991554f2SKenneth D. Merry }
1294991554f2SKenneth D. Merry 
1295991554f2SKenneth D. Merry static int
1296991554f2SKenneth D. Merry mpr_alloc_requests(struct mpr_softc *sc)
1297991554f2SKenneth D. Merry {
1298991554f2SKenneth D. Merry 	struct mpr_command *cm;
1299991554f2SKenneth D. Merry 	struct mpr_chain *chain;
1300991554f2SKenneth D. Merry 	int i, rsize, nsegs;
1301991554f2SKenneth D. Merry 
1302991554f2SKenneth D. Merry 	rsize = sc->facts->IOCRequestFrameSize * sc->num_reqs * 4;
1303991554f2SKenneth D. Merry         if (bus_dma_tag_create( sc->mpr_parent_dmat,    /* parent */
1304991554f2SKenneth D. Merry 				16, 0,			/* algnmnt, boundary */
1305991554f2SKenneth D. Merry 				BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1306991554f2SKenneth D. Merry 				BUS_SPACE_MAXADDR,	/* highaddr */
1307991554f2SKenneth D. Merry 				NULL, NULL,		/* filter, filterarg */
1308991554f2SKenneth D. Merry                                 rsize,			/* maxsize */
1309991554f2SKenneth D. Merry                                 1,			/* nsegments */
1310991554f2SKenneth D. Merry                                 rsize,			/* maxsegsize */
1311991554f2SKenneth D. Merry                                 0,			/* flags */
1312991554f2SKenneth D. Merry                                 NULL, NULL,		/* lockfunc, lockarg */
1313991554f2SKenneth D. Merry                                 &sc->req_dmat)) {
1314757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate request DMA tag\n");
1315991554f2SKenneth D. Merry 		return (ENOMEM);
1316991554f2SKenneth D. Merry         }
1317991554f2SKenneth D. Merry         if (bus_dmamem_alloc(sc->req_dmat, (void **)&sc->req_frames,
1318991554f2SKenneth D. Merry 	    BUS_DMA_NOWAIT, &sc->req_map)) {
1319757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate request memory\n");
1320991554f2SKenneth D. Merry 		return (ENOMEM);
1321991554f2SKenneth D. Merry         }
1322991554f2SKenneth D. Merry         bzero(sc->req_frames, rsize);
1323991554f2SKenneth D. Merry         bus_dmamap_load(sc->req_dmat, sc->req_map, sc->req_frames, rsize,
1324991554f2SKenneth D. Merry 	    mpr_memaddr_cb, &sc->req_busaddr, 0);
1325991554f2SKenneth D. Merry 
13262bbc5fcbSStephen McConnell 	/*
13272bbc5fcbSStephen McConnell 	 * Gen3 and beyond uses the IOCMaxChainSegmentSize from IOC Facts to
13282bbc5fcbSStephen McConnell 	 * get the size of a Chain Frame.  Previous versions use the size as a
13292bbc5fcbSStephen McConnell 	 * Request Frame for the Chain Frame size.  If IOCMaxChainSegmentSize
13302bbc5fcbSStephen McConnell 	 * is 0, use the default value.  The IOCMaxChainSegmentSize is the
13312bbc5fcbSStephen McConnell 	 * number of 16-byte elelements that can fit in a Chain Frame, which is
13322bbc5fcbSStephen McConnell 	 * the size of an IEEE Simple SGE.
13332bbc5fcbSStephen McConnell 	 */
13342bbc5fcbSStephen McConnell 	if (sc->facts->MsgVersion >= MPI2_VERSION_02_05) {
13352bbc5fcbSStephen McConnell 		sc->chain_seg_size =
13362bbc5fcbSStephen McConnell 		    htole16(sc->facts->IOCMaxChainSegmentSize);
13372bbc5fcbSStephen McConnell 		if (sc->chain_seg_size == 0) {
13382bbc5fcbSStephen McConnell 			sc->chain_frame_size = MPR_DEFAULT_CHAIN_SEG_SIZE *
13392bbc5fcbSStephen McConnell 			    MPR_MAX_CHAIN_ELEMENT_SIZE;
13402bbc5fcbSStephen McConnell 		} else {
13412bbc5fcbSStephen McConnell 			sc->chain_frame_size = sc->chain_seg_size *
13422bbc5fcbSStephen McConnell 			    MPR_MAX_CHAIN_ELEMENT_SIZE;
13432bbc5fcbSStephen McConnell 		}
13442bbc5fcbSStephen McConnell 	} else {
13452bbc5fcbSStephen McConnell 		sc->chain_frame_size = sc->facts->IOCRequestFrameSize * 4;
13462bbc5fcbSStephen McConnell 	}
13472bbc5fcbSStephen McConnell 	rsize = sc->chain_frame_size * sc->max_chains;
1348991554f2SKenneth D. Merry         if (bus_dma_tag_create( sc->mpr_parent_dmat,    /* parent */
1349991554f2SKenneth D. Merry 				16, 0,			/* algnmnt, boundary */
1350991554f2SKenneth D. Merry 				BUS_SPACE_MAXADDR,	/* lowaddr */
1351991554f2SKenneth D. Merry 				BUS_SPACE_MAXADDR,	/* highaddr */
1352991554f2SKenneth D. Merry 				NULL, NULL,		/* filter, filterarg */
1353991554f2SKenneth D. Merry                                 rsize,			/* maxsize */
1354991554f2SKenneth D. Merry                                 1,			/* nsegments */
1355991554f2SKenneth D. Merry                                 rsize,			/* maxsegsize */
1356991554f2SKenneth D. Merry                                 0,			/* flags */
1357991554f2SKenneth D. Merry                                 NULL, NULL,		/* lockfunc, lockarg */
1358991554f2SKenneth D. Merry                                 &sc->chain_dmat)) {
1359757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate chain DMA tag\n");
1360991554f2SKenneth D. Merry 		return (ENOMEM);
1361991554f2SKenneth D. Merry         }
1362991554f2SKenneth D. Merry         if (bus_dmamem_alloc(sc->chain_dmat, (void **)&sc->chain_frames,
1363991554f2SKenneth D. Merry 	    BUS_DMA_NOWAIT, &sc->chain_map)) {
1364757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate chain memory\n");
1365991554f2SKenneth D. Merry 		return (ENOMEM);
1366991554f2SKenneth D. Merry         }
1367991554f2SKenneth D. Merry         bzero(sc->chain_frames, rsize);
1368991554f2SKenneth D. Merry         bus_dmamap_load(sc->chain_dmat, sc->chain_map, sc->chain_frames, rsize,
1369991554f2SKenneth D. Merry 	    mpr_memaddr_cb, &sc->chain_busaddr, 0);
1370991554f2SKenneth D. Merry 
1371991554f2SKenneth D. Merry 	rsize = MPR_SENSE_LEN * sc->num_reqs;
1372991554f2SKenneth D. Merry 	if (bus_dma_tag_create( sc->mpr_parent_dmat,    /* parent */
1373991554f2SKenneth D. Merry 				1, 0,			/* algnmnt, boundary */
1374991554f2SKenneth D. Merry 				BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1375991554f2SKenneth D. Merry 				BUS_SPACE_MAXADDR,	/* highaddr */
1376991554f2SKenneth D. Merry 				NULL, NULL,		/* filter, filterarg */
1377991554f2SKenneth D. Merry                                 rsize,			/* maxsize */
1378991554f2SKenneth D. Merry                                 1,			/* nsegments */
1379991554f2SKenneth D. Merry                                 rsize,			/* maxsegsize */
1380991554f2SKenneth D. Merry                                 0,			/* flags */
1381991554f2SKenneth D. Merry                                 NULL, NULL,		/* lockfunc, lockarg */
1382991554f2SKenneth D. Merry                                 &sc->sense_dmat)) {
1383757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate sense DMA tag\n");
1384991554f2SKenneth D. Merry 		return (ENOMEM);
1385991554f2SKenneth D. Merry         }
1386991554f2SKenneth D. Merry         if (bus_dmamem_alloc(sc->sense_dmat, (void **)&sc->sense_frames,
1387991554f2SKenneth D. Merry 	    BUS_DMA_NOWAIT, &sc->sense_map)) {
1388757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate sense memory\n");
1389991554f2SKenneth D. Merry 		return (ENOMEM);
1390991554f2SKenneth D. Merry         }
1391991554f2SKenneth D. Merry         bzero(sc->sense_frames, rsize);
1392991554f2SKenneth D. Merry         bus_dmamap_load(sc->sense_dmat, sc->sense_map, sc->sense_frames, rsize,
1393991554f2SKenneth D. Merry 	    mpr_memaddr_cb, &sc->sense_busaddr, 0);
1394991554f2SKenneth D. Merry 
1395991554f2SKenneth D. Merry 	sc->chains = malloc(sizeof(struct mpr_chain) * sc->max_chains, M_MPR,
1396991554f2SKenneth D. Merry 	    M_WAITOK | M_ZERO);
1397991554f2SKenneth D. Merry 	if (!sc->chains) {
1398757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate chain memory\n");
1399991554f2SKenneth D. Merry 		return (ENOMEM);
1400991554f2SKenneth D. Merry 	}
1401991554f2SKenneth D. Merry 	for (i = 0; i < sc->max_chains; i++) {
1402991554f2SKenneth D. Merry 		chain = &sc->chains[i];
1403991554f2SKenneth D. Merry 		chain->chain = (MPI2_SGE_IO_UNION *)(sc->chain_frames +
14042bbc5fcbSStephen McConnell 		    i * sc->chain_frame_size);
1405991554f2SKenneth D. Merry 		chain->chain_busaddr = sc->chain_busaddr +
14062bbc5fcbSStephen McConnell 		    i * sc->chain_frame_size;
1407991554f2SKenneth D. Merry 		mpr_free_chain(sc, chain);
1408991554f2SKenneth D. Merry 		sc->chain_free_lowwater++;
1409991554f2SKenneth D. Merry 	}
1410991554f2SKenneth D. Merry 
141167feec50SStephen McConnell 	/*
141267feec50SStephen McConnell 	 * Allocate NVMe PRP Pages for NVMe SGL support only if the FW supports
141367feec50SStephen McConnell 	 * these devices.
141467feec50SStephen McConnell 	 */
141567feec50SStephen McConnell 	if ((sc->facts->MsgVersion >= MPI2_VERSION_02_06) &&
141667feec50SStephen McConnell 	    (sc->facts->ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES)) {
141767feec50SStephen McConnell 		if (mpr_alloc_nvme_prp_pages(sc) == ENOMEM)
141867feec50SStephen McConnell 			return (ENOMEM);
141967feec50SStephen McConnell 	}
142067feec50SStephen McConnell 
1421991554f2SKenneth D. Merry 	/* XXX Need to pick a more precise value */
1422991554f2SKenneth D. Merry 	nsegs = (MAXPHYS / PAGE_SIZE) + 1;
1423991554f2SKenneth D. Merry         if (bus_dma_tag_create( sc->mpr_parent_dmat,    /* parent */
1424991554f2SKenneth D. Merry 				1, 0,			/* algnmnt, boundary */
1425991554f2SKenneth D. Merry 				BUS_SPACE_MAXADDR,	/* lowaddr */
1426991554f2SKenneth D. Merry 				BUS_SPACE_MAXADDR,	/* highaddr */
1427991554f2SKenneth D. Merry 				NULL, NULL,		/* filter, filterarg */
1428991554f2SKenneth D. Merry                                 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
1429991554f2SKenneth D. Merry                                 nsegs,			/* nsegments */
1430991554f2SKenneth D. Merry                                 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
1431991554f2SKenneth D. Merry                                 BUS_DMA_ALLOCNOW,	/* flags */
1432991554f2SKenneth D. Merry                                 busdma_lock_mutex,	/* lockfunc */
1433991554f2SKenneth D. Merry 				&sc->mpr_mtx,		/* lockarg */
1434991554f2SKenneth D. Merry                                 &sc->buffer_dmat)) {
1435757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate buffer DMA tag\n");
1436991554f2SKenneth D. Merry 		return (ENOMEM);
1437991554f2SKenneth D. Merry         }
1438991554f2SKenneth D. Merry 
1439991554f2SKenneth D. Merry 	/*
1440991554f2SKenneth D. Merry 	 * SMID 0 cannot be used as a free command per the firmware spec.
1441991554f2SKenneth D. Merry 	 * Just drop that command instead of risking accounting bugs.
1442991554f2SKenneth D. Merry 	 */
1443991554f2SKenneth D. Merry 	sc->commands = malloc(sizeof(struct mpr_command) * sc->num_reqs,
1444991554f2SKenneth D. Merry 	    M_MPR, M_WAITOK | M_ZERO);
1445991554f2SKenneth D. Merry 	if (!sc->commands) {
1446757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate command memory\n");
1447991554f2SKenneth D. Merry 		return (ENOMEM);
1448991554f2SKenneth D. Merry 	}
1449991554f2SKenneth D. Merry 	for (i = 1; i < sc->num_reqs; i++) {
1450991554f2SKenneth D. Merry 		cm = &sc->commands[i];
1451991554f2SKenneth D. Merry 		cm->cm_req = sc->req_frames +
1452991554f2SKenneth D. Merry 		    i * sc->facts->IOCRequestFrameSize * 4;
1453991554f2SKenneth D. Merry 		cm->cm_req_busaddr = sc->req_busaddr +
1454991554f2SKenneth D. Merry 		    i * sc->facts->IOCRequestFrameSize * 4;
1455991554f2SKenneth D. Merry 		cm->cm_sense = &sc->sense_frames[i];
1456991554f2SKenneth D. Merry 		cm->cm_sense_busaddr = sc->sense_busaddr + i * MPR_SENSE_LEN;
1457991554f2SKenneth D. Merry 		cm->cm_desc.Default.SMID = i;
1458991554f2SKenneth D. Merry 		cm->cm_sc = sc;
1459991554f2SKenneth D. Merry 		TAILQ_INIT(&cm->cm_chain_list);
146067feec50SStephen McConnell 		TAILQ_INIT(&cm->cm_prp_page_list);
1461991554f2SKenneth D. Merry 		callout_init_mtx(&cm->cm_callout, &sc->mpr_mtx, 0);
1462991554f2SKenneth D. Merry 
1463991554f2SKenneth D. Merry 		/* XXX Is a failure here a critical problem? */
146467feec50SStephen McConnell 		if (bus_dmamap_create(sc->buffer_dmat, 0, &cm->cm_dmamap)
146567feec50SStephen McConnell 		    == 0) {
1466991554f2SKenneth D. Merry 			if (i <= sc->facts->HighPriorityCredit)
1467991554f2SKenneth D. Merry 				mpr_free_high_priority_command(sc, cm);
1468991554f2SKenneth D. Merry 			else
1469991554f2SKenneth D. Merry 				mpr_free_command(sc, cm);
147067feec50SStephen McConnell 		} else {
1471991554f2SKenneth D. Merry 			panic("failed to allocate command %d\n", i);
1472991554f2SKenneth D. Merry 			sc->num_reqs = i;
1473991554f2SKenneth D. Merry 			break;
1474991554f2SKenneth D. Merry 		}
1475991554f2SKenneth D. Merry 	}
1476991554f2SKenneth D. Merry 
1477991554f2SKenneth D. Merry 	return (0);
1478991554f2SKenneth D. Merry }
1479991554f2SKenneth D. Merry 
148067feec50SStephen McConnell /*
148167feec50SStephen McConnell  * Allocate contiguous buffers for PCIe NVMe devices for building native PRPs,
148267feec50SStephen McConnell  * which are scatter/gather lists for NVMe devices.
148367feec50SStephen McConnell  *
148467feec50SStephen McConnell  * This buffer must be contiguous due to the nature of how NVMe PRPs are built
148567feec50SStephen McConnell  * and translated by FW.
148667feec50SStephen McConnell  *
148767feec50SStephen McConnell  * returns ENOMEM if memory could not be allocated, otherwise returns 0.
148867feec50SStephen McConnell  */
148967feec50SStephen McConnell static int
149067feec50SStephen McConnell mpr_alloc_nvme_prp_pages(struct mpr_softc *sc)
149167feec50SStephen McConnell {
149267feec50SStephen McConnell 	int PRPs_per_page, PRPs_required, pages_required;
149367feec50SStephen McConnell 	int rsize, i;
149467feec50SStephen McConnell 	struct mpr_prp_page *prp_page;
149567feec50SStephen McConnell 
149667feec50SStephen McConnell 	/*
149767feec50SStephen McConnell 	 * Assuming a MAX_IO_SIZE of 1MB and a PAGE_SIZE of 4k, the max number
149867feec50SStephen McConnell 	 * of PRPs (NVMe's Scatter/Gather Element) needed per I/O is:
149967feec50SStephen McConnell 	 * MAX_IO_SIZE / PAGE_SIZE = 256
150067feec50SStephen McConnell 	 *
150167feec50SStephen McConnell 	 * 1 PRP entry in main frame for PRP list pointer still leaves 255 PRPs
150267feec50SStephen McConnell 	 * required for the remainder of the 1MB I/O. 512 PRPs can fit into one
150367feec50SStephen McConnell 	 * page (4096 / 8 = 512), so only one page is required for each I/O.
150467feec50SStephen McConnell 	 *
150567feec50SStephen McConnell 	 * Each of these buffers will need to be contiguous. For simplicity,
150667feec50SStephen McConnell 	 * only one buffer is allocated here, which has all of the space
150767feec50SStephen McConnell 	 * required for the NVMe Queue Depth. If there are problems allocating
150867feec50SStephen McConnell 	 * this one buffer, this function will need to change to allocate
150967feec50SStephen McConnell 	 * individual, contiguous NVME_QDEPTH buffers.
151067feec50SStephen McConnell 	 *
151167feec50SStephen McConnell 	 * The real calculation will use the real max io size. Above is just an
151267feec50SStephen McConnell 	 * example.
151367feec50SStephen McConnell 	 *
151467feec50SStephen McConnell 	 */
151567feec50SStephen McConnell 	PRPs_required = sc->maxio / PAGE_SIZE;
151667feec50SStephen McConnell 	PRPs_per_page = (PAGE_SIZE / PRP_ENTRY_SIZE) - 1;
151767feec50SStephen McConnell 	pages_required = (PRPs_required / PRPs_per_page) + 1;
151867feec50SStephen McConnell 
151967feec50SStephen McConnell 	sc->prp_buffer_size = PAGE_SIZE * pages_required;
152067feec50SStephen McConnell 	rsize = sc->prp_buffer_size * NVME_QDEPTH;
152167feec50SStephen McConnell 	if (bus_dma_tag_create( sc->mpr_parent_dmat,	/* parent */
152267feec50SStephen McConnell 				4, 0,			/* algnmnt, boundary */
152367feec50SStephen McConnell 				BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
152467feec50SStephen McConnell 				BUS_SPACE_MAXADDR,	/* highaddr */
152567feec50SStephen McConnell 				NULL, NULL,		/* filter, filterarg */
152667feec50SStephen McConnell 				rsize,			/* maxsize */
152767feec50SStephen McConnell 				1,			/* nsegments */
152867feec50SStephen McConnell 				rsize,			/* maxsegsize */
152967feec50SStephen McConnell 				0,			/* flags */
153067feec50SStephen McConnell 				NULL, NULL,		/* lockfunc, lockarg */
153167feec50SStephen McConnell 				&sc->prp_page_dmat)) {
1532757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate NVMe PRP DMA "
153367feec50SStephen McConnell 		    "tag\n");
153467feec50SStephen McConnell 		return (ENOMEM);
153567feec50SStephen McConnell 	}
153667feec50SStephen McConnell 	if (bus_dmamem_alloc(sc->prp_page_dmat, (void **)&sc->prp_pages,
153767feec50SStephen McConnell 	    BUS_DMA_NOWAIT, &sc->prp_page_map)) {
1538757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR, "Cannot allocate NVMe PRP memory\n");
153967feec50SStephen McConnell 		return (ENOMEM);
154067feec50SStephen McConnell 	}
154167feec50SStephen McConnell 	bzero(sc->prp_pages, rsize);
154267feec50SStephen McConnell 	bus_dmamap_load(sc->prp_page_dmat, sc->prp_page_map, sc->prp_pages,
154367feec50SStephen McConnell 	    rsize, mpr_memaddr_cb, &sc->prp_page_busaddr, 0);
154467feec50SStephen McConnell 
154567feec50SStephen McConnell 	sc->prps = malloc(sizeof(struct mpr_prp_page) * NVME_QDEPTH, M_MPR,
154667feec50SStephen McConnell 	    M_WAITOK | M_ZERO);
154767feec50SStephen McConnell 	for (i = 0; i < NVME_QDEPTH; i++) {
154867feec50SStephen McConnell 		prp_page = &sc->prps[i];
154967feec50SStephen McConnell 		prp_page->prp_page = (uint64_t *)(sc->prp_pages +
155067feec50SStephen McConnell 		    i * sc->prp_buffer_size);
155167feec50SStephen McConnell 		prp_page->prp_page_busaddr = (uint64_t)(sc->prp_page_busaddr +
155267feec50SStephen McConnell 		    i * sc->prp_buffer_size);
155367feec50SStephen McConnell 		mpr_free_prp_page(sc, prp_page);
155467feec50SStephen McConnell 		sc->prp_pages_free_lowwater++;
155567feec50SStephen McConnell 	}
155667feec50SStephen McConnell 
155767feec50SStephen McConnell 	return (0);
155867feec50SStephen McConnell }
155967feec50SStephen McConnell 
1560991554f2SKenneth D. Merry static int
1561991554f2SKenneth D. Merry mpr_init_queues(struct mpr_softc *sc)
1562991554f2SKenneth D. Merry {
1563991554f2SKenneth D. Merry 	int i;
1564991554f2SKenneth D. Merry 
1565991554f2SKenneth D. Merry 	memset((uint8_t *)sc->post_queue, 0xff, sc->pqdepth * 8);
1566991554f2SKenneth D. Merry 
1567991554f2SKenneth D. Merry 	/*
1568991554f2SKenneth D. Merry 	 * According to the spec, we need to use one less reply than we
1569991554f2SKenneth D. Merry 	 * have space for on the queue.  So sc->num_replies (the number we
1570991554f2SKenneth D. Merry 	 * use) should be less than sc->fqdepth (allocated size).
1571991554f2SKenneth D. Merry 	 */
1572991554f2SKenneth D. Merry 	if (sc->num_replies >= sc->fqdepth)
1573991554f2SKenneth D. Merry 		return (EINVAL);
1574991554f2SKenneth D. Merry 
1575991554f2SKenneth D. Merry 	/*
1576991554f2SKenneth D. Merry 	 * Initialize all of the free queue entries.
1577991554f2SKenneth D. Merry 	 */
157867feec50SStephen McConnell 	for (i = 0; i < sc->fqdepth; i++) {
157967feec50SStephen McConnell 		sc->free_queue[i] = sc->reply_busaddr +
158067feec50SStephen McConnell 		    (i * sc->facts->ReplyFrameSize * 4);
158167feec50SStephen McConnell 	}
1582991554f2SKenneth D. Merry 	sc->replyfreeindex = sc->num_replies;
1583991554f2SKenneth D. Merry 
1584991554f2SKenneth D. Merry 	return (0);
1585991554f2SKenneth D. Merry }
1586991554f2SKenneth D. Merry 
1587991554f2SKenneth D. Merry /* Get the driver parameter tunables.  Lowest priority are the driver defaults.
1588991554f2SKenneth D. Merry  * Next are the global settings, if they exist.  Highest are the per-unit
1589991554f2SKenneth D. Merry  * settings, if they exist.
1590991554f2SKenneth D. Merry  */
1591252b2b4fSScott Long void
1592991554f2SKenneth D. Merry mpr_get_tunables(struct mpr_softc *sc)
1593991554f2SKenneth D. Merry {
1594991554f2SKenneth D. Merry 	char tmpstr[80];
1595991554f2SKenneth D. Merry 
1596991554f2SKenneth D. Merry 	/* XXX default to some debugging for now */
1597991554f2SKenneth D. Merry 	sc->mpr_debug = MPR_INFO | MPR_FAULT;
1598991554f2SKenneth D. Merry 	sc->disable_msix = 0;
1599991554f2SKenneth D. Merry 	sc->disable_msi = 0;
1600*3c5ac992SScott Long 	sc->max_msix = MPR_MSIX_MAX;
1601991554f2SKenneth D. Merry 	sc->max_chains = MPR_CHAIN_FRAMES;
160232b0a21eSStephen McConnell 	sc->max_io_pages = MPR_MAXIO_PAGES;
1603a2c14879SStephen McConnell 	sc->enable_ssu = MPR_SSU_ENABLE_SSD_DISABLE_HDD;
1604a2c14879SStephen McConnell 	sc->spinup_wait_time = DEFAULT_SPINUP_WAIT;
16054ab1cdc5SScott Long 	sc->use_phynum = 1;
1606*3c5ac992SScott Long 	sc->max_reqframes = MPR_REQ_FRAMES;
1607*3c5ac992SScott Long 	sc->max_prireqframes = MPR_PRI_REQ_FRAMES;
1608*3c5ac992SScott Long 	sc->max_replyframes = MPR_REPLY_FRAMES;
1609*3c5ac992SScott Long 	sc->max_evtframes = MPR_EVT_REPLY_FRAMES;
1610991554f2SKenneth D. Merry 
1611991554f2SKenneth D. Merry 	/*
1612991554f2SKenneth D. Merry 	 * Grab the global variables.
1613991554f2SKenneth D. Merry 	 */
1614991554f2SKenneth D. Merry 	TUNABLE_INT_FETCH("hw.mpr.debug_level", &sc->mpr_debug);
1615991554f2SKenneth D. Merry 	TUNABLE_INT_FETCH("hw.mpr.disable_msix", &sc->disable_msix);
1616991554f2SKenneth D. Merry 	TUNABLE_INT_FETCH("hw.mpr.disable_msi", &sc->disable_msi);
1617*3c5ac992SScott Long 	TUNABLE_INT_FETCH("hw.mpr.max_msix", &sc->max_msix);
1618991554f2SKenneth D. Merry 	TUNABLE_INT_FETCH("hw.mpr.max_chains", &sc->max_chains);
161932b0a21eSStephen McConnell 	TUNABLE_INT_FETCH("hw.mpr.max_io_pages", &sc->max_io_pages);
1620a2c14879SStephen McConnell 	TUNABLE_INT_FETCH("hw.mpr.enable_ssu", &sc->enable_ssu);
1621a2c14879SStephen McConnell 	TUNABLE_INT_FETCH("hw.mpr.spinup_wait_time", &sc->spinup_wait_time);
16224ab1cdc5SScott Long 	TUNABLE_INT_FETCH("hw.mpr.use_phy_num", &sc->use_phynum);
1623*3c5ac992SScott Long 	TUNABLE_INT_FETCH("hw.mpr.max_reqframes", &sc->max_reqframes);
1624*3c5ac992SScott Long 	TUNABLE_INT_FETCH("hw.mpr.max_prireqframes", &sc->max_prireqframes);
1625*3c5ac992SScott Long 	TUNABLE_INT_FETCH("hw.mpr.max_replyframes", &sc->max_replyframes);
1626*3c5ac992SScott Long 	TUNABLE_INT_FETCH("hw.mpr.max_evtframes", &sc->max_evtframes);
1627991554f2SKenneth D. Merry 
1628991554f2SKenneth D. Merry 	/* Grab the unit-instance variables */
1629991554f2SKenneth D. Merry 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.debug_level",
1630991554f2SKenneth D. Merry 	    device_get_unit(sc->mpr_dev));
1631991554f2SKenneth D. Merry 	TUNABLE_INT_FETCH(tmpstr, &sc->mpr_debug);
1632991554f2SKenneth D. Merry 
1633991554f2SKenneth D. Merry 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.disable_msix",
1634991554f2SKenneth D. Merry 	    device_get_unit(sc->mpr_dev));
1635991554f2SKenneth D. Merry 	TUNABLE_INT_FETCH(tmpstr, &sc->disable_msix);
1636991554f2SKenneth D. Merry 
1637991554f2SKenneth D. Merry 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.disable_msi",
1638991554f2SKenneth D. Merry 	    device_get_unit(sc->mpr_dev));
1639991554f2SKenneth D. Merry 	TUNABLE_INT_FETCH(tmpstr, &sc->disable_msi);
1640991554f2SKenneth D. Merry 
1641*3c5ac992SScott Long 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.max_msix",
1642*3c5ac992SScott Long 	    device_get_unit(sc->mpr_dev));
1643*3c5ac992SScott Long 	TUNABLE_INT_FETCH(tmpstr, &sc->max_msix);
1644*3c5ac992SScott Long 
1645991554f2SKenneth D. Merry 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.max_chains",
1646991554f2SKenneth D. Merry 	    device_get_unit(sc->mpr_dev));
1647991554f2SKenneth D. Merry 	TUNABLE_INT_FETCH(tmpstr, &sc->max_chains);
1648991554f2SKenneth D. Merry 
164932b0a21eSStephen McConnell 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.max_io_pages",
165032b0a21eSStephen McConnell 	    device_get_unit(sc->mpr_dev));
165132b0a21eSStephen McConnell 	TUNABLE_INT_FETCH(tmpstr, &sc->max_io_pages);
165232b0a21eSStephen McConnell 
1653991554f2SKenneth D. Merry 	bzero(sc->exclude_ids, sizeof(sc->exclude_ids));
1654991554f2SKenneth D. Merry 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.exclude_ids",
1655991554f2SKenneth D. Merry 	    device_get_unit(sc->mpr_dev));
1656991554f2SKenneth D. Merry 	TUNABLE_STR_FETCH(tmpstr, sc->exclude_ids, sizeof(sc->exclude_ids));
1657a2c14879SStephen McConnell 
1658a2c14879SStephen McConnell 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.enable_ssu",
1659a2c14879SStephen McConnell 	    device_get_unit(sc->mpr_dev));
1660a2c14879SStephen McConnell 	TUNABLE_INT_FETCH(tmpstr, &sc->enable_ssu);
1661a2c14879SStephen McConnell 
1662a2c14879SStephen McConnell 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.spinup_wait_time",
1663a2c14879SStephen McConnell 	    device_get_unit(sc->mpr_dev));
1664a2c14879SStephen McConnell 	TUNABLE_INT_FETCH(tmpstr, &sc->spinup_wait_time);
16654ab1cdc5SScott Long 
16664ab1cdc5SScott Long 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.use_phy_num",
16674ab1cdc5SScott Long 	    device_get_unit(sc->mpr_dev));
16684ab1cdc5SScott Long 	TUNABLE_INT_FETCH(tmpstr, &sc->use_phynum);
1669*3c5ac992SScott Long 
1670*3c5ac992SScott Long 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.max_reqframes",
1671*3c5ac992SScott Long 	    device_get_unit(sc->mpr_dev));
1672*3c5ac992SScott Long 	TUNABLE_INT_FETCH(tmpstr, &sc->max_reqframes);
1673*3c5ac992SScott Long 
1674*3c5ac992SScott Long 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.max_prireqframes",
1675*3c5ac992SScott Long 	    device_get_unit(sc->mpr_dev));
1676*3c5ac992SScott Long 	TUNABLE_INT_FETCH(tmpstr, &sc->max_prireqframes);
1677*3c5ac992SScott Long 
1678*3c5ac992SScott Long 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.max_replyframes",
1679*3c5ac992SScott Long 	    device_get_unit(sc->mpr_dev));
1680*3c5ac992SScott Long 	TUNABLE_INT_FETCH(tmpstr, &sc->max_replyframes);
1681*3c5ac992SScott Long 
1682*3c5ac992SScott Long 	snprintf(tmpstr, sizeof(tmpstr), "dev.mpr.%d.max_evtframes",
1683*3c5ac992SScott Long 	    device_get_unit(sc->mpr_dev));
1684*3c5ac992SScott Long 	TUNABLE_INT_FETCH(tmpstr, &sc->max_evtframes);
1685991554f2SKenneth D. Merry }
1686991554f2SKenneth D. Merry 
1687991554f2SKenneth D. Merry static void
1688991554f2SKenneth D. Merry mpr_setup_sysctl(struct mpr_softc *sc)
1689991554f2SKenneth D. Merry {
1690991554f2SKenneth D. Merry 	struct sysctl_ctx_list	*sysctl_ctx = NULL;
1691991554f2SKenneth D. Merry 	struct sysctl_oid	*sysctl_tree = NULL;
1692991554f2SKenneth D. Merry 	char tmpstr[80], tmpstr2[80];
1693991554f2SKenneth D. Merry 
1694991554f2SKenneth D. Merry 	/*
1695991554f2SKenneth D. Merry 	 * Setup the sysctl variable so the user can change the debug level
1696991554f2SKenneth D. Merry 	 * on the fly.
1697991554f2SKenneth D. Merry 	 */
1698991554f2SKenneth D. Merry 	snprintf(tmpstr, sizeof(tmpstr), "MPR controller %d",
1699991554f2SKenneth D. Merry 	    device_get_unit(sc->mpr_dev));
1700991554f2SKenneth D. Merry 	snprintf(tmpstr2, sizeof(tmpstr2), "%d", device_get_unit(sc->mpr_dev));
1701991554f2SKenneth D. Merry 
1702991554f2SKenneth D. Merry 	sysctl_ctx = device_get_sysctl_ctx(sc->mpr_dev);
1703991554f2SKenneth D. Merry 	if (sysctl_ctx != NULL)
1704991554f2SKenneth D. Merry 		sysctl_tree = device_get_sysctl_tree(sc->mpr_dev);
1705991554f2SKenneth D. Merry 
1706991554f2SKenneth D. Merry 	if (sysctl_tree == NULL) {
1707991554f2SKenneth D. Merry 		sysctl_ctx_init(&sc->sysctl_ctx);
1708991554f2SKenneth D. Merry 		sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
1709991554f2SKenneth D. Merry 		    SYSCTL_STATIC_CHILDREN(_hw_mpr), OID_AUTO, tmpstr2,
1710991554f2SKenneth D. Merry 		    CTLFLAG_RD, 0, tmpstr);
1711991554f2SKenneth D. Merry 		if (sc->sysctl_tree == NULL)
1712991554f2SKenneth D. Merry 			return;
1713991554f2SKenneth D. Merry 		sysctl_ctx = &sc->sysctl_ctx;
1714991554f2SKenneth D. Merry 		sysctl_tree = sc->sysctl_tree;
1715991554f2SKenneth D. Merry 	}
1716991554f2SKenneth D. Merry 
1717991554f2SKenneth D. Merry 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1718991554f2SKenneth D. Merry 	    OID_AUTO, "debug_level", CTLFLAG_RW, &sc->mpr_debug, 0,
1719991554f2SKenneth D. Merry 	    "mpr debug level");
1720991554f2SKenneth D. Merry 
1721991554f2SKenneth D. Merry 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1722991554f2SKenneth D. Merry 	    OID_AUTO, "disable_msix", CTLFLAG_RD, &sc->disable_msix, 0,
1723991554f2SKenneth D. Merry 	    "Disable the use of MSI-X interrupts");
1724991554f2SKenneth D. Merry 
1725991554f2SKenneth D. Merry 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1726*3c5ac992SScott Long 	    OID_AUTO, "max_msix", CTLFLAG_RD, &sc->max_msix, 0,
1727*3c5ac992SScott Long 	    "User-defined maximum number of MSIX queues");
1728*3c5ac992SScott Long 
1729*3c5ac992SScott Long 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1730*3c5ac992SScott Long 	    OID_AUTO, "msix_msgs", CTLFLAG_RD, &sc->msi_msgs, 0,
1731*3c5ac992SScott Long 	    "Negotiated number of MSIX queues");
1732*3c5ac992SScott Long 
1733*3c5ac992SScott Long 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1734*3c5ac992SScott Long 	    OID_AUTO, "max_reqframes", CTLFLAG_RD, &sc->max_reqframes, 0,
1735*3c5ac992SScott Long 	    "Total number of allocated request frames");
1736*3c5ac992SScott Long 
1737*3c5ac992SScott Long 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1738*3c5ac992SScott Long 	    OID_AUTO, "max_prireqframes", CTLFLAG_RD, &sc->max_prireqframes, 0,
1739*3c5ac992SScott Long 	    "Total number of allocated high priority request frames");
1740*3c5ac992SScott Long 
1741*3c5ac992SScott Long 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1742*3c5ac992SScott Long 	    OID_AUTO, "max_replyframes", CTLFLAG_RD, &sc->max_replyframes, 0,
1743*3c5ac992SScott Long 	    "Total number of allocated reply frames");
1744*3c5ac992SScott Long 
1745*3c5ac992SScott Long 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1746*3c5ac992SScott Long 	    OID_AUTO, "max_evtframes", CTLFLAG_RD, &sc->max_evtframes, 0,
1747*3c5ac992SScott Long 	    "Total number of event frames allocated");
1748991554f2SKenneth D. Merry 
1749991554f2SKenneth D. Merry 	SYSCTL_ADD_STRING(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1750f0188618SHans Petter Selasky 	    OID_AUTO, "firmware_version", CTLFLAG_RW, sc->fw_version,
1751991554f2SKenneth D. Merry 	    strlen(sc->fw_version), "firmware version");
1752991554f2SKenneth D. Merry 
1753991554f2SKenneth D. Merry 	SYSCTL_ADD_STRING(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1754991554f2SKenneth D. Merry 	    OID_AUTO, "driver_version", CTLFLAG_RW, MPR_DRIVER_VERSION,
1755991554f2SKenneth D. Merry 	    strlen(MPR_DRIVER_VERSION), "driver version");
1756991554f2SKenneth D. Merry 
1757991554f2SKenneth D. Merry 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1758991554f2SKenneth D. Merry 	    OID_AUTO, "io_cmds_active", CTLFLAG_RD,
1759991554f2SKenneth D. Merry 	    &sc->io_cmds_active, 0, "number of currently active commands");
1760991554f2SKenneth D. Merry 
1761991554f2SKenneth D. Merry 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1762991554f2SKenneth D. Merry 	    OID_AUTO, "io_cmds_highwater", CTLFLAG_RD,
1763991554f2SKenneth D. Merry 	    &sc->io_cmds_highwater, 0, "maximum active commands seen");
1764991554f2SKenneth D. Merry 
1765991554f2SKenneth D. Merry 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1766991554f2SKenneth D. Merry 	    OID_AUTO, "chain_free", CTLFLAG_RD,
1767991554f2SKenneth D. Merry 	    &sc->chain_free, 0, "number of free chain elements");
1768991554f2SKenneth D. Merry 
1769991554f2SKenneth D. Merry 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1770991554f2SKenneth D. Merry 	    OID_AUTO, "chain_free_lowwater", CTLFLAG_RD,
1771991554f2SKenneth D. Merry 	    &sc->chain_free_lowwater, 0,"lowest number of free chain elements");
1772991554f2SKenneth D. Merry 
1773991554f2SKenneth D. Merry 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1774991554f2SKenneth D. Merry 	    OID_AUTO, "max_chains", CTLFLAG_RD,
1775991554f2SKenneth D. Merry 	    &sc->max_chains, 0,"maximum chain frames that will be allocated");
1776991554f2SKenneth D. Merry 
1777a2c14879SStephen McConnell 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
177832b0a21eSStephen McConnell 	    OID_AUTO, "max_io_pages", CTLFLAG_RD,
177932b0a21eSStephen McConnell 	    &sc->max_io_pages, 0,"maximum pages to allow per I/O (if <1 use "
178032b0a21eSStephen McConnell 	    "IOCFacts)");
178132b0a21eSStephen McConnell 
178232b0a21eSStephen McConnell 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1783a2c14879SStephen McConnell 	    OID_AUTO, "enable_ssu", CTLFLAG_RW, &sc->enable_ssu, 0,
1784a2c14879SStephen McConnell 	    "enable SSU to SATA SSD/HDD at shutdown");
1785a2c14879SStephen McConnell 
1786991554f2SKenneth D. Merry 	SYSCTL_ADD_UQUAD(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1787991554f2SKenneth D. Merry 	    OID_AUTO, "chain_alloc_fail", CTLFLAG_RD,
1788991554f2SKenneth D. Merry 	    &sc->chain_alloc_fail, "chain allocation failures");
1789a2c14879SStephen McConnell 
1790a2c14879SStephen McConnell 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1791a2c14879SStephen McConnell 	    OID_AUTO, "spinup_wait_time", CTLFLAG_RD,
1792a2c14879SStephen McConnell 	    &sc->spinup_wait_time, DEFAULT_SPINUP_WAIT, "seconds to wait for "
1793a2c14879SStephen McConnell 	    "spinup after SATA ID error");
17944ab1cdc5SScott Long 
17954ab1cdc5SScott Long 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
17964ab1cdc5SScott Long 	    OID_AUTO, "use_phy_num", CTLFLAG_RD, &sc->use_phynum, 0,
17974ab1cdc5SScott Long 	    "Use the phy number for enumeration");
179867feec50SStephen McConnell 
179967feec50SStephen McConnell 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
180067feec50SStephen McConnell 	    OID_AUTO, "prp_pages_free", CTLFLAG_RD,
180167feec50SStephen McConnell 	    &sc->prp_pages_free, 0, "number of free PRP pages");
180267feec50SStephen McConnell 
180367feec50SStephen McConnell 	SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
180467feec50SStephen McConnell 	    OID_AUTO, "prp_pages_free_lowwater", CTLFLAG_RD,
180567feec50SStephen McConnell 	    &sc->prp_pages_free_lowwater, 0,"lowest number of free PRP pages");
180667feec50SStephen McConnell 
180767feec50SStephen McConnell 	SYSCTL_ADD_UQUAD(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
180867feec50SStephen McConnell 	    OID_AUTO, "prp_page_alloc_fail", CTLFLAG_RD,
180967feec50SStephen McConnell 	    &sc->prp_page_alloc_fail, "PRP page allocation failures");
1810991554f2SKenneth D. Merry }
1811991554f2SKenneth D. Merry 
1812991554f2SKenneth D. Merry int
1813991554f2SKenneth D. Merry mpr_attach(struct mpr_softc *sc)
1814991554f2SKenneth D. Merry {
1815991554f2SKenneth D. Merry 	int error;
1816991554f2SKenneth D. Merry 
1817991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
1818757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
1819991554f2SKenneth D. Merry 
1820991554f2SKenneth D. Merry 	mtx_init(&sc->mpr_mtx, "MPR lock", NULL, MTX_DEF);
1821991554f2SKenneth D. Merry 	callout_init_mtx(&sc->periodic, &sc->mpr_mtx, 0);
1822327f2e6cSStephen McConnell 	callout_init_mtx(&sc->device_check_callout, &sc->mpr_mtx, 0);
1823991554f2SKenneth D. Merry 	TAILQ_INIT(&sc->event_list);
1824991554f2SKenneth D. Merry 	timevalclear(&sc->lastfail);
1825991554f2SKenneth D. Merry 
1826991554f2SKenneth D. Merry 	if ((error = mpr_transition_ready(sc)) != 0) {
1827757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_FAULT,
1828757ff642SScott Long 		    "Failed to transition ready\n");
1829991554f2SKenneth D. Merry 		return (error);
1830991554f2SKenneth D. Merry 	}
1831991554f2SKenneth D. Merry 
1832991554f2SKenneth D. Merry 	sc->facts = malloc(sizeof(MPI2_IOC_FACTS_REPLY), M_MPR,
1833991554f2SKenneth D. Merry 	    M_ZERO|M_NOWAIT);
1834991554f2SKenneth D. Merry 	if (!sc->facts) {
1835757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_FAULT,
1836757ff642SScott Long 		    "Cannot allocate memory, exit\n");
1837991554f2SKenneth D. Merry 		return (ENOMEM);
1838991554f2SKenneth D. Merry 	}
1839991554f2SKenneth D. Merry 
1840991554f2SKenneth D. Merry 	/*
1841991554f2SKenneth D. Merry 	 * Get IOC Facts and allocate all structures based on this information.
1842991554f2SKenneth D. Merry 	 * A Diag Reset will also call mpr_iocfacts_allocate and re-read the IOC
1843991554f2SKenneth D. Merry 	 * Facts. If relevant values have changed in IOC Facts, this function
1844991554f2SKenneth D. Merry 	 * will free all of the memory based on IOC Facts and reallocate that
1845991554f2SKenneth D. Merry 	 * memory.  If this fails, any allocated memory should already be freed.
1846991554f2SKenneth D. Merry 	 */
1847991554f2SKenneth D. Merry 	if ((error = mpr_iocfacts_allocate(sc, TRUE)) != 0) {
1848757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_FAULT, "IOC Facts allocation "
1849757ff642SScott Long 		    "failed with error %d\n", error);
1850991554f2SKenneth D. Merry 		return (error);
1851991554f2SKenneth D. Merry 	}
1852991554f2SKenneth D. Merry 
1853991554f2SKenneth D. Merry 	/* Start the periodic watchdog check on the IOC Doorbell */
1854991554f2SKenneth D. Merry 	mpr_periodic(sc);
1855991554f2SKenneth D. Merry 
1856991554f2SKenneth D. Merry 	/*
1857991554f2SKenneth D. Merry 	 * The portenable will kick off discovery events that will drive the
1858991554f2SKenneth D. Merry 	 * rest of the initialization process.  The CAM/SAS module will
1859991554f2SKenneth D. Merry 	 * hold up the boot sequence until discovery is complete.
1860991554f2SKenneth D. Merry 	 */
1861991554f2SKenneth D. Merry 	sc->mpr_ich.ich_func = mpr_startup;
1862991554f2SKenneth D. Merry 	sc->mpr_ich.ich_arg = sc;
1863991554f2SKenneth D. Merry 	if (config_intrhook_establish(&sc->mpr_ich) != 0) {
1864757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR,
1865757ff642SScott Long 		    "Cannot establish MPR config hook\n");
1866991554f2SKenneth D. Merry 		error = EINVAL;
1867991554f2SKenneth D. Merry 	}
1868991554f2SKenneth D. Merry 
1869991554f2SKenneth D. Merry 	/*
1870991554f2SKenneth D. Merry 	 * Allow IR to shutdown gracefully when shutdown occurs.
1871991554f2SKenneth D. Merry 	 */
1872991554f2SKenneth D. Merry 	sc->shutdown_eh = EVENTHANDLER_REGISTER(shutdown_final,
1873991554f2SKenneth D. Merry 	    mprsas_ir_shutdown, sc, SHUTDOWN_PRI_DEFAULT);
1874991554f2SKenneth D. Merry 
1875991554f2SKenneth D. Merry 	if (sc->shutdown_eh == NULL)
1876757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_ERROR,
1877757ff642SScott Long 		    "shutdown event registration failed\n");
1878991554f2SKenneth D. Merry 
1879991554f2SKenneth D. Merry 	mpr_setup_sysctl(sc);
1880991554f2SKenneth D. Merry 
1881991554f2SKenneth D. Merry 	sc->mpr_flags |= MPR_FLAGS_ATTACH_DONE;
1882757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit error= %d\n", __func__, error);
1883991554f2SKenneth D. Merry 
1884991554f2SKenneth D. Merry 	return (error);
1885991554f2SKenneth D. Merry }
1886991554f2SKenneth D. Merry 
1887991554f2SKenneth D. Merry /* Run through any late-start handlers. */
1888991554f2SKenneth D. Merry static void
1889991554f2SKenneth D. Merry mpr_startup(void *arg)
1890991554f2SKenneth D. Merry {
1891991554f2SKenneth D. Merry 	struct mpr_softc *sc;
1892991554f2SKenneth D. Merry 
1893991554f2SKenneth D. Merry 	sc = (struct mpr_softc *)arg;
1894757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
1895991554f2SKenneth D. Merry 
1896991554f2SKenneth D. Merry 	mpr_lock(sc);
1897991554f2SKenneth D. Merry 	mpr_unmask_intr(sc);
1898991554f2SKenneth D. Merry 
1899991554f2SKenneth D. Merry 	/* initialize device mapping tables */
1900991554f2SKenneth D. Merry 	mpr_base_static_config_pages(sc);
1901991554f2SKenneth D. Merry 	mpr_mapping_initialize(sc);
1902991554f2SKenneth D. Merry 	mprsas_startup(sc);
1903991554f2SKenneth D. Merry 	mpr_unlock(sc);
1904a4bb51a4SScott Long 
1905a4bb51a4SScott Long 	mpr_dprint(sc, MPR_INIT, "disestablish config intrhook\n");
1906a4bb51a4SScott Long 	config_intrhook_disestablish(&sc->mpr_ich);
1907a4bb51a4SScott Long 	sc->mpr_ich.ich_arg = NULL;
1908a4bb51a4SScott Long 
1909757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit\n", __func__);
1910991554f2SKenneth D. Merry }
1911991554f2SKenneth D. Merry 
1912991554f2SKenneth D. Merry /* Periodic watchdog.  Is called with the driver lock already held. */
1913991554f2SKenneth D. Merry static void
1914991554f2SKenneth D. Merry mpr_periodic(void *arg)
1915991554f2SKenneth D. Merry {
1916991554f2SKenneth D. Merry 	struct mpr_softc *sc;
1917991554f2SKenneth D. Merry 	uint32_t db;
1918991554f2SKenneth D. Merry 
1919991554f2SKenneth D. Merry 	sc = (struct mpr_softc *)arg;
1920991554f2SKenneth D. Merry 	if (sc->mpr_flags & MPR_FLAGS_SHUTDOWN)
1921991554f2SKenneth D. Merry 		return;
1922991554f2SKenneth D. Merry 
1923991554f2SKenneth D. Merry 	db = mpr_regread(sc, MPI2_DOORBELL_OFFSET);
1924991554f2SKenneth D. Merry 	if ((db & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
1925991554f2SKenneth D. Merry 		if ((db & MPI2_DOORBELL_FAULT_CODE_MASK) ==
1926991554f2SKenneth D. Merry 		    IFAULT_IOP_OVER_TEMP_THRESHOLD_EXCEEDED) {
1927991554f2SKenneth D. Merry 			panic("TEMPERATURE FAULT: STOPPING.");
1928991554f2SKenneth D. Merry 		}
1929991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "IOC Fault 0x%08x, Resetting\n", db);
1930991554f2SKenneth D. Merry 		mpr_reinit(sc);
1931991554f2SKenneth D. Merry 	}
1932991554f2SKenneth D. Merry 
1933991554f2SKenneth D. Merry 	callout_reset(&sc->periodic, MPR_PERIODIC_DELAY * hz, mpr_periodic, sc);
1934991554f2SKenneth D. Merry }
1935991554f2SKenneth D. Merry 
1936991554f2SKenneth D. Merry static void
1937991554f2SKenneth D. Merry mpr_log_evt_handler(struct mpr_softc *sc, uintptr_t data,
1938991554f2SKenneth D. Merry     MPI2_EVENT_NOTIFICATION_REPLY *event)
1939991554f2SKenneth D. Merry {
1940991554f2SKenneth D. Merry 	MPI2_EVENT_DATA_LOG_ENTRY_ADDED *entry;
1941991554f2SKenneth D. Merry 
1942055e2653SScott Long 	MPR_DPRINT_EVENT(sc, generic, event);
1943991554f2SKenneth D. Merry 
1944991554f2SKenneth D. Merry 	switch (event->Event) {
1945991554f2SKenneth D. Merry 	case MPI2_EVENT_LOG_DATA:
1946991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "MPI2_EVENT_LOG_DATA:\n");
1947991554f2SKenneth D. Merry 		if (sc->mpr_debug & MPR_EVENT)
1948991554f2SKenneth D. Merry 			hexdump(event->EventData, event->EventDataLength, NULL,
1949991554f2SKenneth D. Merry 			    0);
1950991554f2SKenneth D. Merry 		break;
1951991554f2SKenneth D. Merry 	case MPI2_EVENT_LOG_ENTRY_ADDED:
1952991554f2SKenneth D. Merry 		entry = (MPI2_EVENT_DATA_LOG_ENTRY_ADDED *)event->EventData;
1953991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "MPI2_EVENT_LOG_ENTRY_ADDED event "
1954991554f2SKenneth D. Merry 		    "0x%x Sequence %d:\n", entry->LogEntryQualifier,
1955991554f2SKenneth D. Merry 		     entry->LogSequence);
1956991554f2SKenneth D. Merry 		break;
1957991554f2SKenneth D. Merry 	default:
1958991554f2SKenneth D. Merry 		break;
1959991554f2SKenneth D. Merry 	}
1960991554f2SKenneth D. Merry 	return;
1961991554f2SKenneth D. Merry }
1962991554f2SKenneth D. Merry 
1963991554f2SKenneth D. Merry static int
1964991554f2SKenneth D. Merry mpr_attach_log(struct mpr_softc *sc)
1965991554f2SKenneth D. Merry {
1966991554f2SKenneth D. Merry 	uint8_t events[16];
1967991554f2SKenneth D. Merry 
1968991554f2SKenneth D. Merry 	bzero(events, 16);
1969991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_LOG_DATA);
1970991554f2SKenneth D. Merry 	setbit(events, MPI2_EVENT_LOG_ENTRY_ADDED);
1971991554f2SKenneth D. Merry 
1972991554f2SKenneth D. Merry 	mpr_register_events(sc, events, mpr_log_evt_handler, NULL,
1973991554f2SKenneth D. Merry 	    &sc->mpr_log_eh);
1974991554f2SKenneth D. Merry 
1975991554f2SKenneth D. Merry 	return (0);
1976991554f2SKenneth D. Merry }
1977991554f2SKenneth D. Merry 
1978991554f2SKenneth D. Merry static int
1979991554f2SKenneth D. Merry mpr_detach_log(struct mpr_softc *sc)
1980991554f2SKenneth D. Merry {
1981991554f2SKenneth D. Merry 
1982991554f2SKenneth D. Merry 	if (sc->mpr_log_eh != NULL)
1983991554f2SKenneth D. Merry 		mpr_deregister_events(sc, sc->mpr_log_eh);
1984991554f2SKenneth D. Merry 	return (0);
1985991554f2SKenneth D. Merry }
1986991554f2SKenneth D. Merry 
1987991554f2SKenneth D. Merry /*
1988991554f2SKenneth D. Merry  * Free all of the driver resources and detach submodules.  Should be called
1989991554f2SKenneth D. Merry  * without the lock held.
1990991554f2SKenneth D. Merry  */
1991991554f2SKenneth D. Merry int
1992991554f2SKenneth D. Merry mpr_free(struct mpr_softc *sc)
1993991554f2SKenneth D. Merry {
1994991554f2SKenneth D. Merry 	int error;
1995991554f2SKenneth D. Merry 
1996757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s entered\n", __func__);
1997991554f2SKenneth D. Merry 	/* Turn off the watchdog */
1998991554f2SKenneth D. Merry 	mpr_lock(sc);
1999991554f2SKenneth D. Merry 	sc->mpr_flags |= MPR_FLAGS_SHUTDOWN;
2000991554f2SKenneth D. Merry 	mpr_unlock(sc);
2001991554f2SKenneth D. Merry 	/* Lock must not be held for this */
2002991554f2SKenneth D. Merry 	callout_drain(&sc->periodic);
2003327f2e6cSStephen McConnell 	callout_drain(&sc->device_check_callout);
2004991554f2SKenneth D. Merry 
2005991554f2SKenneth D. Merry 	if (((error = mpr_detach_log(sc)) != 0) ||
2006757ff642SScott Long 	    ((error = mpr_detach_sas(sc)) != 0)) {
2007757ff642SScott Long 		mpr_dprint(sc, MPR_INIT|MPR_FAULT, "failed to detach "
2008757ff642SScott Long 		    "subsystems, error= %d, exit\n", error);
2009991554f2SKenneth D. Merry 		return (error);
2010757ff642SScott Long 	}
2011991554f2SKenneth D. Merry 
2012991554f2SKenneth D. Merry 	mpr_detach_user(sc);
2013991554f2SKenneth D. Merry 
2014991554f2SKenneth D. Merry 	/* Put the IOC back in the READY state. */
2015991554f2SKenneth D. Merry 	mpr_lock(sc);
2016991554f2SKenneth D. Merry 	if ((error = mpr_transition_ready(sc)) != 0) {
2017991554f2SKenneth D. Merry 		mpr_unlock(sc);
2018991554f2SKenneth D. Merry 		return (error);
2019991554f2SKenneth D. Merry 	}
2020991554f2SKenneth D. Merry 	mpr_unlock(sc);
2021991554f2SKenneth D. Merry 
2022991554f2SKenneth D. Merry 	if (sc->facts != NULL)
2023991554f2SKenneth D. Merry 		free(sc->facts, M_MPR);
2024991554f2SKenneth D. Merry 
2025991554f2SKenneth D. Merry 	/*
2026991554f2SKenneth D. Merry 	 * Free all buffers that are based on IOC Facts.  A Diag Reset may need
2027991554f2SKenneth D. Merry 	 * to free these buffers too.
2028991554f2SKenneth D. Merry 	 */
2029991554f2SKenneth D. Merry 	mpr_iocfacts_free(sc);
2030991554f2SKenneth D. Merry 
2031991554f2SKenneth D. Merry 	if (sc->sysctl_tree != NULL)
2032991554f2SKenneth D. Merry 		sysctl_ctx_free(&sc->sysctl_ctx);
2033991554f2SKenneth D. Merry 
2034991554f2SKenneth D. Merry 	/* Deregister the shutdown function */
2035991554f2SKenneth D. Merry 	if (sc->shutdown_eh != NULL)
2036991554f2SKenneth D. Merry 		EVENTHANDLER_DEREGISTER(shutdown_final, sc->shutdown_eh);
2037991554f2SKenneth D. Merry 
2038991554f2SKenneth D. Merry 	mtx_destroy(&sc->mpr_mtx);
2039757ff642SScott Long 	mpr_dprint(sc, MPR_INIT, "%s exit\n", __func__);
2040991554f2SKenneth D. Merry 
2041991554f2SKenneth D. Merry 	return (0);
2042991554f2SKenneth D. Merry }
2043991554f2SKenneth D. Merry 
2044991554f2SKenneth D. Merry static __inline void
2045991554f2SKenneth D. Merry mpr_complete_command(struct mpr_softc *sc, struct mpr_command *cm)
2046991554f2SKenneth D. Merry {
2047991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
2048991554f2SKenneth D. Merry 
2049991554f2SKenneth D. Merry 	if (cm == NULL) {
2050991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "Completing NULL command\n");
2051991554f2SKenneth D. Merry 		return;
2052991554f2SKenneth D. Merry 	}
2053991554f2SKenneth D. Merry 
2054991554f2SKenneth D. Merry 	if (cm->cm_flags & MPR_CM_FLAGS_POLLED)
2055991554f2SKenneth D. Merry 		cm->cm_flags |= MPR_CM_FLAGS_COMPLETE;
2056991554f2SKenneth D. Merry 
2057991554f2SKenneth D. Merry 	if (cm->cm_complete != NULL) {
2058991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_TRACE,
2059991554f2SKenneth D. Merry 		    "%s cm %p calling cm_complete %p data %p reply %p\n",
2060991554f2SKenneth D. Merry 		    __func__, cm, cm->cm_complete, cm->cm_complete_data,
2061991554f2SKenneth D. Merry 		    cm->cm_reply);
2062991554f2SKenneth D. Merry 		cm->cm_complete(sc, cm);
2063991554f2SKenneth D. Merry 	}
2064991554f2SKenneth D. Merry 
2065991554f2SKenneth D. Merry 	if (cm->cm_flags & MPR_CM_FLAGS_WAKEUP) {
2066991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_TRACE, "waking up %p\n", cm);
2067991554f2SKenneth D. Merry 		wakeup(cm);
2068991554f2SKenneth D. Merry 	}
2069991554f2SKenneth D. Merry 
2070991554f2SKenneth D. Merry 	if (sc->io_cmds_active != 0) {
2071991554f2SKenneth D. Merry 		sc->io_cmds_active--;
2072991554f2SKenneth D. Merry 	} else {
2073991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "Warning: io_cmds_active is "
2074991554f2SKenneth D. Merry 		    "out of sync - resynching to 0\n");
2075991554f2SKenneth D. Merry 	}
2076991554f2SKenneth D. Merry }
2077991554f2SKenneth D. Merry 
2078991554f2SKenneth D. Merry static void
2079991554f2SKenneth D. Merry mpr_sas_log_info(struct mpr_softc *sc , u32 log_info)
2080991554f2SKenneth D. Merry {
2081991554f2SKenneth D. Merry 	union loginfo_type {
2082991554f2SKenneth D. Merry 		u32	loginfo;
2083991554f2SKenneth D. Merry 		struct {
2084991554f2SKenneth D. Merry 			u32	subcode:16;
2085991554f2SKenneth D. Merry 			u32	code:8;
2086991554f2SKenneth D. Merry 			u32	originator:4;
2087991554f2SKenneth D. Merry 			u32	bus_type:4;
2088991554f2SKenneth D. Merry 		} dw;
2089991554f2SKenneth D. Merry 	};
2090991554f2SKenneth D. Merry 	union loginfo_type sas_loginfo;
2091991554f2SKenneth D. Merry 	char *originator_str = NULL;
2092991554f2SKenneth D. Merry 
2093991554f2SKenneth D. Merry 	sas_loginfo.loginfo = log_info;
2094991554f2SKenneth D. Merry 	if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
2095991554f2SKenneth D. Merry 		return;
2096991554f2SKenneth D. Merry 
2097991554f2SKenneth D. Merry 	/* each nexus loss loginfo */
2098991554f2SKenneth D. Merry 	if (log_info == 0x31170000)
2099991554f2SKenneth D. Merry 		return;
2100991554f2SKenneth D. Merry 
2101991554f2SKenneth D. Merry 	/* eat the loginfos associated with task aborts */
2102991554f2SKenneth D. Merry 	if ((log_info == 30050000) || (log_info == 0x31140000) ||
2103991554f2SKenneth D. Merry 	    (log_info == 0x31130000))
2104991554f2SKenneth D. Merry 		return;
2105991554f2SKenneth D. Merry 
2106991554f2SKenneth D. Merry 	switch (sas_loginfo.dw.originator) {
2107991554f2SKenneth D. Merry 	case 0:
2108991554f2SKenneth D. Merry 		originator_str = "IOP";
2109991554f2SKenneth D. Merry 		break;
2110991554f2SKenneth D. Merry 	case 1:
2111991554f2SKenneth D. Merry 		originator_str = "PL";
2112991554f2SKenneth D. Merry 		break;
2113991554f2SKenneth D. Merry 	case 2:
2114991554f2SKenneth D. Merry 		originator_str = "IR";
2115991554f2SKenneth D. Merry 		break;
2116991554f2SKenneth D. Merry 	}
2117991554f2SKenneth D. Merry 
2118b41c6ff9SStephen McConnell 	mpr_dprint(sc, MPR_LOG, "log_info(0x%08x): originator(%s), "
21197a2a6a1aSStephen McConnell 	    "code(0x%02x), sub_code(0x%04x)\n", log_info, originator_str,
21207a2a6a1aSStephen McConnell 	    sas_loginfo.dw.code, sas_loginfo.dw.subcode);
2121991554f2SKenneth D. Merry }
2122991554f2SKenneth D. Merry 
2123991554f2SKenneth D. Merry static void
2124991554f2SKenneth D. Merry mpr_display_reply_info(struct mpr_softc *sc, uint8_t *reply)
2125991554f2SKenneth D. Merry {
2126991554f2SKenneth D. Merry 	MPI2DefaultReply_t *mpi_reply;
2127991554f2SKenneth D. Merry 	u16 sc_status;
2128991554f2SKenneth D. Merry 
2129991554f2SKenneth D. Merry 	mpi_reply = (MPI2DefaultReply_t*)reply;
2130991554f2SKenneth D. Merry 	sc_status = le16toh(mpi_reply->IOCStatus);
2131991554f2SKenneth D. Merry 	if (sc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
2132991554f2SKenneth D. Merry 		mpr_sas_log_info(sc, le32toh(mpi_reply->IOCLogInfo));
2133991554f2SKenneth D. Merry }
2134991554f2SKenneth D. Merry 
2135991554f2SKenneth D. Merry void
2136991554f2SKenneth D. Merry mpr_intr(void *data)
2137991554f2SKenneth D. Merry {
2138991554f2SKenneth D. Merry 	struct mpr_softc *sc;
2139991554f2SKenneth D. Merry 	uint32_t status;
2140991554f2SKenneth D. Merry 
2141991554f2SKenneth D. Merry 	sc = (struct mpr_softc *)data;
2142991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
2143991554f2SKenneth D. Merry 
2144991554f2SKenneth D. Merry 	/*
2145991554f2SKenneth D. Merry 	 * Check interrupt status register to flush the bus.  This is
2146991554f2SKenneth D. Merry 	 * needed for both INTx interrupts and driver-driven polling
2147991554f2SKenneth D. Merry 	 */
2148991554f2SKenneth D. Merry 	status = mpr_regread(sc, MPI2_HOST_INTERRUPT_STATUS_OFFSET);
2149991554f2SKenneth D. Merry 	if ((status & MPI2_HIS_REPLY_DESCRIPTOR_INTERRUPT) == 0)
2150991554f2SKenneth D. Merry 		return;
2151991554f2SKenneth D. Merry 
2152991554f2SKenneth D. Merry 	mpr_lock(sc);
2153991554f2SKenneth D. Merry 	mpr_intr_locked(data);
2154991554f2SKenneth D. Merry 	mpr_unlock(sc);
2155991554f2SKenneth D. Merry 	return;
2156991554f2SKenneth D. Merry }
2157991554f2SKenneth D. Merry 
2158991554f2SKenneth D. Merry /*
2159991554f2SKenneth D. Merry  * In theory, MSI/MSIX interrupts shouldn't need to read any registers on the
2160991554f2SKenneth D. Merry  * chip.  Hopefully this theory is correct.
2161991554f2SKenneth D. Merry  */
2162991554f2SKenneth D. Merry void
2163991554f2SKenneth D. Merry mpr_intr_msi(void *data)
2164991554f2SKenneth D. Merry {
2165991554f2SKenneth D. Merry 	struct mpr_softc *sc;
2166991554f2SKenneth D. Merry 
2167991554f2SKenneth D. Merry 	sc = (struct mpr_softc *)data;
2168991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
2169991554f2SKenneth D. Merry 	mpr_lock(sc);
2170991554f2SKenneth D. Merry 	mpr_intr_locked(data);
2171991554f2SKenneth D. Merry 	mpr_unlock(sc);
2172991554f2SKenneth D. Merry 	return;
2173991554f2SKenneth D. Merry }
2174991554f2SKenneth D. Merry 
2175991554f2SKenneth D. Merry /*
2176991554f2SKenneth D. Merry  * The locking is overly broad and simplistic, but easy to deal with for now.
2177991554f2SKenneth D. Merry  */
2178991554f2SKenneth D. Merry void
2179991554f2SKenneth D. Merry mpr_intr_locked(void *data)
2180991554f2SKenneth D. Merry {
2181991554f2SKenneth D. Merry 	MPI2_REPLY_DESCRIPTORS_UNION *desc;
2182991554f2SKenneth D. Merry 	struct mpr_softc *sc;
2183991554f2SKenneth D. Merry 	struct mpr_command *cm = NULL;
2184991554f2SKenneth D. Merry 	uint8_t flags;
2185991554f2SKenneth D. Merry 	u_int pq;
2186991554f2SKenneth D. Merry 	MPI2_DIAG_RELEASE_REPLY *rel_rep;
2187991554f2SKenneth D. Merry 	mpr_fw_diagnostic_buffer_t *pBuffer;
2188991554f2SKenneth D. Merry 
2189991554f2SKenneth D. Merry 	sc = (struct mpr_softc *)data;
2190991554f2SKenneth D. Merry 
2191991554f2SKenneth D. Merry 	pq = sc->replypostindex;
2192991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE,
2193991554f2SKenneth D. Merry 	    "%s sc %p starting with replypostindex %u\n",
2194991554f2SKenneth D. Merry 	    __func__, sc, sc->replypostindex);
2195991554f2SKenneth D. Merry 
2196991554f2SKenneth D. Merry 	for ( ;; ) {
2197991554f2SKenneth D. Merry 		cm = NULL;
2198991554f2SKenneth D. Merry 		desc = &sc->post_queue[sc->replypostindex];
2199991554f2SKenneth D. Merry 		flags = desc->Default.ReplyFlags &
2200991554f2SKenneth D. Merry 		    MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
2201991554f2SKenneth D. Merry 		if ((flags == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) ||
2202991554f2SKenneth D. Merry 		    (le32toh(desc->Words.High) == 0xffffffff))
2203991554f2SKenneth D. Merry 			break;
2204991554f2SKenneth D. Merry 
2205991554f2SKenneth D. Merry 		/* increment the replypostindex now, so that event handlers
2206991554f2SKenneth D. Merry 		 * and cm completion handlers which decide to do a diag
2207991554f2SKenneth D. Merry 		 * reset can zero it without it getting incremented again
2208991554f2SKenneth D. Merry 		 * afterwards, and we break out of this loop on the next
2209991554f2SKenneth D. Merry 		 * iteration since the reply post queue has been cleared to
2210991554f2SKenneth D. Merry 		 * 0xFF and all descriptors look unused (which they are).
2211991554f2SKenneth D. Merry 		 */
2212991554f2SKenneth D. Merry 		if (++sc->replypostindex >= sc->pqdepth)
2213991554f2SKenneth D. Merry 			sc->replypostindex = 0;
2214991554f2SKenneth D. Merry 
2215991554f2SKenneth D. Merry 		switch (flags) {
2216991554f2SKenneth D. Merry 		case MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS:
2217991554f2SKenneth D. Merry 		case MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS:
221867feec50SStephen McConnell 		case MPI26_RPY_DESCRIPT_FLAGS_PCIE_ENCAPSULATED_SUCCESS:
2219991554f2SKenneth D. Merry 			cm = &sc->commands[le16toh(desc->SCSIIOSuccess.SMID)];
2220991554f2SKenneth D. Merry 			cm->cm_reply = NULL;
2221991554f2SKenneth D. Merry 			break;
2222991554f2SKenneth D. Merry 		case MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY:
2223991554f2SKenneth D. Merry 		{
2224991554f2SKenneth D. Merry 			uint32_t baddr;
2225991554f2SKenneth D. Merry 			uint8_t *reply;
2226991554f2SKenneth D. Merry 
2227991554f2SKenneth D. Merry 			/*
2228991554f2SKenneth D. Merry 			 * Re-compose the reply address from the address
2229991554f2SKenneth D. Merry 			 * sent back from the chip.  The ReplyFrameAddress
2230991554f2SKenneth D. Merry 			 * is the lower 32 bits of the physical address of
2231991554f2SKenneth D. Merry 			 * particular reply frame.  Convert that address to
2232991554f2SKenneth D. Merry 			 * host format, and then use that to provide the
2233991554f2SKenneth D. Merry 			 * offset against the virtual address base
2234991554f2SKenneth D. Merry 			 * (sc->reply_frames).
2235991554f2SKenneth D. Merry 			 */
2236991554f2SKenneth D. Merry 			baddr = le32toh(desc->AddressReply.ReplyFrameAddress);
2237991554f2SKenneth D. Merry 			reply = sc->reply_frames +
2238991554f2SKenneth D. Merry 				(baddr - ((uint32_t)sc->reply_busaddr));
2239991554f2SKenneth D. Merry 			/*
2240991554f2SKenneth D. Merry 			 * Make sure the reply we got back is in a valid
2241991554f2SKenneth D. Merry 			 * range.  If not, go ahead and panic here, since
2242991554f2SKenneth D. Merry 			 * we'll probably panic as soon as we deference the
2243991554f2SKenneth D. Merry 			 * reply pointer anyway.
2244991554f2SKenneth D. Merry 			 */
2245991554f2SKenneth D. Merry 			if ((reply < sc->reply_frames)
2246991554f2SKenneth D. Merry 			 || (reply > (sc->reply_frames +
2247991554f2SKenneth D. Merry 			     (sc->fqdepth * sc->facts->ReplyFrameSize * 4)))) {
2248991554f2SKenneth D. Merry 				printf("%s: WARNING: reply %p out of range!\n",
2249991554f2SKenneth D. Merry 				       __func__, reply);
2250991554f2SKenneth D. Merry 				printf("%s: reply_frames %p, fqdepth %d, "
2251991554f2SKenneth D. Merry 				       "frame size %d\n", __func__,
2252991554f2SKenneth D. Merry 				       sc->reply_frames, sc->fqdepth,
2253991554f2SKenneth D. Merry 				       sc->facts->ReplyFrameSize * 4);
2254991554f2SKenneth D. Merry 				printf("%s: baddr %#x,\n", __func__, baddr);
2255991554f2SKenneth D. Merry 				/* LSI-TODO. See Linux Code for Graceful exit */
2256991554f2SKenneth D. Merry 				panic("Reply address out of range");
2257991554f2SKenneth D. Merry 			}
2258991554f2SKenneth D. Merry 			if (le16toh(desc->AddressReply.SMID) == 0) {
2259991554f2SKenneth D. Merry 				if (((MPI2_DEFAULT_REPLY *)reply)->Function ==
2260991554f2SKenneth D. Merry 				    MPI2_FUNCTION_DIAG_BUFFER_POST) {
2261991554f2SKenneth D. Merry 					/*
2262991554f2SKenneth D. Merry 					 * If SMID is 0 for Diag Buffer Post,
2263991554f2SKenneth D. Merry 					 * this implies that the reply is due to
2264991554f2SKenneth D. Merry 					 * a release function with a status that
2265991554f2SKenneth D. Merry 					 * the buffer has been released.  Set
2266991554f2SKenneth D. Merry 					 * the buffer flags accordingly.
2267991554f2SKenneth D. Merry 					 */
2268991554f2SKenneth D. Merry 					rel_rep =
2269991554f2SKenneth D. Merry 					    (MPI2_DIAG_RELEASE_REPLY *)reply;
2270d3f6eabfSStephen McConnell 					if ((le16toh(rel_rep->IOCStatus) &
2271d3f6eabfSStephen McConnell 					    MPI2_IOCSTATUS_MASK) ==
2272991554f2SKenneth D. Merry 					    MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED)
2273991554f2SKenneth D. Merry 					{
2274991554f2SKenneth D. Merry 						pBuffer =
2275991554f2SKenneth D. Merry 						    &sc->fw_diag_buffer_list[
2276991554f2SKenneth D. Merry 						    rel_rep->BufferType];
2277991554f2SKenneth D. Merry 						pBuffer->valid_data = TRUE;
2278991554f2SKenneth D. Merry 						pBuffer->owned_by_firmware =
2279991554f2SKenneth D. Merry 						    FALSE;
2280991554f2SKenneth D. Merry 						pBuffer->immediate = FALSE;
2281991554f2SKenneth D. Merry 					}
2282991554f2SKenneth D. Merry 				} else
2283991554f2SKenneth D. Merry 					mpr_dispatch_event(sc, baddr,
2284991554f2SKenneth D. Merry 					    (MPI2_EVENT_NOTIFICATION_REPLY *)
2285991554f2SKenneth D. Merry 					    reply);
2286991554f2SKenneth D. Merry 			} else {
2287991554f2SKenneth D. Merry 				cm = &sc->commands[
2288991554f2SKenneth D. Merry 				    le16toh(desc->AddressReply.SMID)];
2289991554f2SKenneth D. Merry 				cm->cm_reply = reply;
2290991554f2SKenneth D. Merry 				cm->cm_reply_data =
2291991554f2SKenneth D. Merry 				    le32toh(desc->AddressReply.
2292991554f2SKenneth D. Merry 				    ReplyFrameAddress);
2293991554f2SKenneth D. Merry 			}
2294991554f2SKenneth D. Merry 			break;
2295991554f2SKenneth D. Merry 		}
2296991554f2SKenneth D. Merry 		case MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS:
2297991554f2SKenneth D. Merry 		case MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER:
2298991554f2SKenneth D. Merry 		case MPI2_RPY_DESCRIPT_FLAGS_RAID_ACCELERATOR_SUCCESS:
2299991554f2SKenneth D. Merry 		default:
2300991554f2SKenneth D. Merry 			/* Unhandled */
2301991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_ERROR, "Unhandled reply 0x%x\n",
2302991554f2SKenneth D. Merry 			    desc->Default.ReplyFlags);
2303991554f2SKenneth D. Merry 			cm = NULL;
2304991554f2SKenneth D. Merry 			break;
2305991554f2SKenneth D. Merry 		}
2306991554f2SKenneth D. Merry 
2307991554f2SKenneth D. Merry 		if (cm != NULL) {
2308991554f2SKenneth D. Merry 			// Print Error reply frame
2309991554f2SKenneth D. Merry 			if (cm->cm_reply)
2310991554f2SKenneth D. Merry 				mpr_display_reply_info(sc,cm->cm_reply);
2311991554f2SKenneth D. Merry 			mpr_complete_command(sc, cm);
2312991554f2SKenneth D. Merry 		}
2313991554f2SKenneth D. Merry 
2314991554f2SKenneth D. Merry 		desc->Words.Low = 0xffffffff;
2315991554f2SKenneth D. Merry 		desc->Words.High = 0xffffffff;
2316991554f2SKenneth D. Merry 	}
2317991554f2SKenneth D. Merry 
2318991554f2SKenneth D. Merry 	if (pq != sc->replypostindex) {
2319991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_TRACE,
2320991554f2SKenneth D. Merry 		    "%s sc %p writing postindex %d\n",
2321991554f2SKenneth D. Merry 		    __func__, sc, sc->replypostindex);
2322991554f2SKenneth D. Merry 		mpr_regwrite(sc, MPI2_REPLY_POST_HOST_INDEX_OFFSET,
2323991554f2SKenneth D. Merry 		    sc->replypostindex);
2324991554f2SKenneth D. Merry 	}
2325991554f2SKenneth D. Merry 
2326991554f2SKenneth D. Merry 	return;
2327991554f2SKenneth D. Merry }
2328991554f2SKenneth D. Merry 
2329991554f2SKenneth D. Merry static void
2330991554f2SKenneth D. Merry mpr_dispatch_event(struct mpr_softc *sc, uintptr_t data,
2331991554f2SKenneth D. Merry     MPI2_EVENT_NOTIFICATION_REPLY *reply)
2332991554f2SKenneth D. Merry {
2333991554f2SKenneth D. Merry 	struct mpr_event_handle *eh;
2334991554f2SKenneth D. Merry 	int event, handled = 0;
2335991554f2SKenneth D. Merry 
2336991554f2SKenneth D. Merry 	event = le16toh(reply->Event);
2337991554f2SKenneth D. Merry 	TAILQ_FOREACH(eh, &sc->event_list, eh_list) {
2338991554f2SKenneth D. Merry 		if (isset(eh->mask, event)) {
2339991554f2SKenneth D. Merry 			eh->callback(sc, data, reply);
2340991554f2SKenneth D. Merry 			handled++;
2341991554f2SKenneth D. Merry 		}
2342991554f2SKenneth D. Merry 	}
2343991554f2SKenneth D. Merry 
2344991554f2SKenneth D. Merry 	if (handled == 0)
2345991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_EVENT, "Unhandled event 0x%x\n",
2346991554f2SKenneth D. Merry 		    le16toh(event));
2347991554f2SKenneth D. Merry 
2348991554f2SKenneth D. Merry 	/*
2349991554f2SKenneth D. Merry 	 * This is the only place that the event/reply should be freed.
2350991554f2SKenneth D. Merry 	 * Anything wanting to hold onto the event data should have
2351991554f2SKenneth D. Merry 	 * already copied it into their own storage.
2352991554f2SKenneth D. Merry 	 */
2353991554f2SKenneth D. Merry 	mpr_free_reply(sc, data);
2354991554f2SKenneth D. Merry }
2355991554f2SKenneth D. Merry 
2356991554f2SKenneth D. Merry static void
2357991554f2SKenneth D. Merry mpr_reregister_events_complete(struct mpr_softc *sc, struct mpr_command *cm)
2358991554f2SKenneth D. Merry {
2359991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
2360991554f2SKenneth D. Merry 
2361991554f2SKenneth D. Merry 	if (cm->cm_reply)
2362055e2653SScott Long 		MPR_DPRINT_EVENT(sc, generic,
2363991554f2SKenneth D. Merry 			(MPI2_EVENT_NOTIFICATION_REPLY *)cm->cm_reply);
2364991554f2SKenneth D. Merry 
2365991554f2SKenneth D. Merry 	mpr_free_command(sc, cm);
2366991554f2SKenneth D. Merry 
2367991554f2SKenneth D. Merry 	/* next, send a port enable */
2368991554f2SKenneth D. Merry 	mprsas_startup(sc);
2369991554f2SKenneth D. Merry }
2370991554f2SKenneth D. Merry 
2371991554f2SKenneth D. Merry /*
2372991554f2SKenneth D. Merry  * For both register_events and update_events, the caller supplies a bitmap
2373991554f2SKenneth D. Merry  * of events that it _wants_.  These functions then turn that into a bitmask
2374991554f2SKenneth D. Merry  * suitable for the controller.
2375991554f2SKenneth D. Merry  */
2376991554f2SKenneth D. Merry int
2377991554f2SKenneth D. Merry mpr_register_events(struct mpr_softc *sc, uint8_t *mask,
2378991554f2SKenneth D. Merry     mpr_evt_callback_t *cb, void *data, struct mpr_event_handle **handle)
2379991554f2SKenneth D. Merry {
2380991554f2SKenneth D. Merry 	struct mpr_event_handle *eh;
2381991554f2SKenneth D. Merry 	int error = 0;
2382991554f2SKenneth D. Merry 
2383991554f2SKenneth D. Merry 	eh = malloc(sizeof(struct mpr_event_handle), M_MPR, M_WAITOK|M_ZERO);
2384991554f2SKenneth D. Merry 	if (!eh) {
2385757ff642SScott Long 		mpr_dprint(sc, MPR_EVENT|MPR_ERROR,
2386757ff642SScott Long 		    "Cannot allocate event memory\n");
2387991554f2SKenneth D. Merry 		return (ENOMEM);
2388991554f2SKenneth D. Merry 	}
2389991554f2SKenneth D. Merry 	eh->callback = cb;
2390991554f2SKenneth D. Merry 	eh->data = data;
2391991554f2SKenneth D. Merry 	TAILQ_INSERT_TAIL(&sc->event_list, eh, eh_list);
2392991554f2SKenneth D. Merry 	if (mask != NULL)
2393991554f2SKenneth D. Merry 		error = mpr_update_events(sc, eh, mask);
2394991554f2SKenneth D. Merry 	*handle = eh;
2395991554f2SKenneth D. Merry 
2396991554f2SKenneth D. Merry 	return (error);
2397991554f2SKenneth D. Merry }
2398991554f2SKenneth D. Merry 
2399991554f2SKenneth D. Merry int
2400991554f2SKenneth D. Merry mpr_update_events(struct mpr_softc *sc, struct mpr_event_handle *handle,
2401991554f2SKenneth D. Merry     uint8_t *mask)
2402991554f2SKenneth D. Merry {
2403991554f2SKenneth D. Merry 	MPI2_EVENT_NOTIFICATION_REQUEST *evtreq;
24046d4ffcb4SKenneth D. Merry 	MPI2_EVENT_NOTIFICATION_REPLY *reply = NULL;
24056d4ffcb4SKenneth D. Merry 	struct mpr_command *cm = NULL;
2406991554f2SKenneth D. Merry 	struct mpr_event_handle *eh;
2407991554f2SKenneth D. Merry 	int error, i;
2408991554f2SKenneth D. Merry 
2409991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
2410991554f2SKenneth D. Merry 
2411991554f2SKenneth D. Merry 	if ((mask != NULL) && (handle != NULL))
2412991554f2SKenneth D. Merry 		bcopy(mask, &handle->mask[0], 16);
2413991554f2SKenneth D. Merry 	memset(sc->event_mask, 0xff, 16);
2414991554f2SKenneth D. Merry 
2415991554f2SKenneth D. Merry 	TAILQ_FOREACH(eh, &sc->event_list, eh_list) {
2416991554f2SKenneth D. Merry 		for (i = 0; i < 16; i++)
2417991554f2SKenneth D. Merry 			sc->event_mask[i] &= ~eh->mask[i];
2418991554f2SKenneth D. Merry 	}
2419991554f2SKenneth D. Merry 
2420991554f2SKenneth D. Merry 	if ((cm = mpr_alloc_command(sc)) == NULL)
2421991554f2SKenneth D. Merry 		return (EBUSY);
2422991554f2SKenneth D. Merry 	evtreq = (MPI2_EVENT_NOTIFICATION_REQUEST *)cm->cm_req;
2423991554f2SKenneth D. Merry 	evtreq->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
2424991554f2SKenneth D. Merry 	evtreq->MsgFlags = 0;
2425991554f2SKenneth D. Merry 	evtreq->SASBroadcastPrimitiveMasks = 0;
2426991554f2SKenneth D. Merry #ifdef MPR_DEBUG_ALL_EVENTS
2427991554f2SKenneth D. Merry 	{
2428991554f2SKenneth D. Merry 		u_char fullmask[16];
2429991554f2SKenneth D. Merry 		memset(fullmask, 0x00, 16);
2430991554f2SKenneth D. Merry 		bcopy(fullmask, (uint8_t *)&evtreq->EventMasks, 16);
2431991554f2SKenneth D. Merry 	}
2432991554f2SKenneth D. Merry #else
2433991554f2SKenneth D. Merry 		bcopy(sc->event_mask, (uint8_t *)&evtreq->EventMasks, 16);
2434991554f2SKenneth D. Merry #endif
2435991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2436991554f2SKenneth D. Merry 	cm->cm_data = NULL;
2437991554f2SKenneth D. Merry 
24386d4ffcb4SKenneth D. Merry 	error = mpr_request_polled(sc, &cm);
24396d4ffcb4SKenneth D. Merry 	if (cm != NULL)
2440991554f2SKenneth D. Merry 		reply = (MPI2_EVENT_NOTIFICATION_REPLY *)cm->cm_reply;
2441991554f2SKenneth D. Merry 	if ((reply == NULL) ||
2442991554f2SKenneth D. Merry 	    (reply->IOCStatus & MPI2_IOCSTATUS_MASK) != MPI2_IOCSTATUS_SUCCESS)
2443991554f2SKenneth D. Merry 		error = ENXIO;
2444991554f2SKenneth D. Merry 
2445991554f2SKenneth D. Merry 	if (reply)
2446055e2653SScott Long 		MPR_DPRINT_EVENT(sc, generic, reply);
2447991554f2SKenneth D. Merry 
2448991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s finished error %d\n", __func__, error);
2449991554f2SKenneth D. Merry 
24506d4ffcb4SKenneth D. Merry 	if (cm != NULL)
2451991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
2452991554f2SKenneth D. Merry 	return (error);
2453991554f2SKenneth D. Merry }
2454991554f2SKenneth D. Merry 
2455991554f2SKenneth D. Merry static int
2456991554f2SKenneth D. Merry mpr_reregister_events(struct mpr_softc *sc)
2457991554f2SKenneth D. Merry {
2458991554f2SKenneth D. Merry 	MPI2_EVENT_NOTIFICATION_REQUEST *evtreq;
2459991554f2SKenneth D. Merry 	struct mpr_command *cm;
2460991554f2SKenneth D. Merry 	struct mpr_event_handle *eh;
2461991554f2SKenneth D. Merry 	int error, i;
2462991554f2SKenneth D. Merry 
2463991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
2464991554f2SKenneth D. Merry 
2465991554f2SKenneth D. Merry 	/* first, reregister events */
2466991554f2SKenneth D. Merry 
2467991554f2SKenneth D. Merry 	memset(sc->event_mask, 0xff, 16);
2468991554f2SKenneth D. Merry 
2469991554f2SKenneth D. Merry 	TAILQ_FOREACH(eh, &sc->event_list, eh_list) {
2470991554f2SKenneth D. Merry 		for (i = 0; i < 16; i++)
2471991554f2SKenneth D. Merry 			sc->event_mask[i] &= ~eh->mask[i];
2472991554f2SKenneth D. Merry 	}
2473991554f2SKenneth D. Merry 
2474991554f2SKenneth D. Merry 	if ((cm = mpr_alloc_command(sc)) == NULL)
2475991554f2SKenneth D. Merry 		return (EBUSY);
2476991554f2SKenneth D. Merry 	evtreq = (MPI2_EVENT_NOTIFICATION_REQUEST *)cm->cm_req;
2477991554f2SKenneth D. Merry 	evtreq->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
2478991554f2SKenneth D. Merry 	evtreq->MsgFlags = 0;
2479991554f2SKenneth D. Merry 	evtreq->SASBroadcastPrimitiveMasks = 0;
2480991554f2SKenneth D. Merry #ifdef MPR_DEBUG_ALL_EVENTS
2481991554f2SKenneth D. Merry 	{
2482991554f2SKenneth D. Merry 		u_char fullmask[16];
2483991554f2SKenneth D. Merry 		memset(fullmask, 0x00, 16);
2484991554f2SKenneth D. Merry 		bcopy(fullmask, (uint8_t *)&evtreq->EventMasks, 16);
2485991554f2SKenneth D. Merry 	}
2486991554f2SKenneth D. Merry #else
2487991554f2SKenneth D. Merry 		bcopy(sc->event_mask, (uint8_t *)&evtreq->EventMasks, 16);
2488991554f2SKenneth D. Merry #endif
2489991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2490991554f2SKenneth D. Merry 	cm->cm_data = NULL;
2491991554f2SKenneth D. Merry 	cm->cm_complete = mpr_reregister_events_complete;
2492991554f2SKenneth D. Merry 
2493991554f2SKenneth D. Merry 	error = mpr_map_command(sc, cm);
2494991554f2SKenneth D. Merry 
2495991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_TRACE, "%s finished with error %d\n", __func__,
2496991554f2SKenneth D. Merry 	    error);
2497991554f2SKenneth D. Merry 	return (error);
2498991554f2SKenneth D. Merry }
2499991554f2SKenneth D. Merry 
2500991554f2SKenneth D. Merry int
2501991554f2SKenneth D. Merry mpr_deregister_events(struct mpr_softc *sc, struct mpr_event_handle *handle)
2502991554f2SKenneth D. Merry {
2503991554f2SKenneth D. Merry 
2504991554f2SKenneth D. Merry 	TAILQ_REMOVE(&sc->event_list, handle, eh_list);
2505991554f2SKenneth D. Merry 	free(handle, M_MPR);
2506991554f2SKenneth D. Merry 	return (mpr_update_events(sc, NULL, NULL));
2507991554f2SKenneth D. Merry }
2508991554f2SKenneth D. Merry 
250967feec50SStephen McConnell /**
251067feec50SStephen McConnell * mpr_build_nvme_prp - This function is called for NVMe end devices to build a
251167feec50SStephen McConnell * native SGL (NVMe PRP). The native SGL is built starting in the first PRP entry
251267feec50SStephen McConnell * of the NVMe message (PRP1). If the data buffer is small enough to be described
251367feec50SStephen McConnell * entirely using PRP1, then PRP2 is not used. If needed, PRP2 is used to
251467feec50SStephen McConnell * describe a larger data buffer. If the data buffer is too large to describe
251567feec50SStephen McConnell * using the two PRP entriess inside the NVMe message, then PRP1 describes the
251667feec50SStephen McConnell * first data memory segment, and PRP2 contains a pointer to a PRP list located
251767feec50SStephen McConnell * elsewhere in memory to describe the remaining data memory segments. The PRP
251867feec50SStephen McConnell * list will be contiguous.
251967feec50SStephen McConnell 
252067feec50SStephen McConnell * The native SGL for NVMe devices is a Physical Region Page (PRP). A PRP
252167feec50SStephen McConnell * consists of a list of PRP entries to describe a number of noncontigous
252267feec50SStephen McConnell * physical memory segments as a single memory buffer, just as a SGL does. Note
252367feec50SStephen McConnell * however, that this function is only used by the IOCTL call, so the memory
252467feec50SStephen McConnell * given will be guaranteed to be contiguous. There is no need to translate
252567feec50SStephen McConnell * non-contiguous SGL into a PRP in this case. All PRPs will describe contiguous
252667feec50SStephen McConnell * space that is one page size each.
252767feec50SStephen McConnell *
252867feec50SStephen McConnell * Each NVMe message contains two PRP entries. The first (PRP1) either contains
252967feec50SStephen McConnell * a PRP list pointer or a PRP element, depending upon the command. PRP2 contains
253067feec50SStephen McConnell * the second PRP element if the memory being described fits within 2 PRP
253167feec50SStephen McConnell * entries, or a PRP list pointer if the PRP spans more than two entries.
253267feec50SStephen McConnell *
253367feec50SStephen McConnell * A PRP list pointer contains the address of a PRP list, structured as a linear
253467feec50SStephen McConnell * array of PRP entries. Each PRP entry in this list describes a segment of
253567feec50SStephen McConnell * physical memory.
253667feec50SStephen McConnell *
253767feec50SStephen McConnell * Each 64-bit PRP entry comprises an address and an offset field. The address
253867feec50SStephen McConnell * always points to the beginning of a PAGE_SIZE physical memory page, and the
253967feec50SStephen McConnell * offset describes where within that page the memory segment begins. Only the
254067feec50SStephen McConnell * first element in a PRP list may contain a non-zero offest, implying that all
254167feec50SStephen McConnell * memory segments following the first begin at the start of a PAGE_SIZE page.
254267feec50SStephen McConnell *
254367feec50SStephen McConnell * Each PRP element normally describes a chunck of PAGE_SIZE physical memory,
254467feec50SStephen McConnell * with exceptions for the first and last elements in the list. If the memory
254567feec50SStephen McConnell * being described by the list begins at a non-zero offset within the first page,
254667feec50SStephen McConnell * then the first PRP element will contain a non-zero offset indicating where the
254767feec50SStephen McConnell * region begins within the page. The last memory segment may end before the end
254867feec50SStephen McConnell * of the PAGE_SIZE segment, depending upon the overall size of the memory being
254967feec50SStephen McConnell * described by the PRP list.
255067feec50SStephen McConnell *
255167feec50SStephen McConnell * Since PRP entries lack any indication of size, the overall data buffer length
255267feec50SStephen McConnell * is used to determine where the end of the data memory buffer is located, and
255367feec50SStephen McConnell * how many PRP entries are required to describe it.
255467feec50SStephen McConnell *
255567feec50SStephen McConnell * Returns nothing.
255667feec50SStephen McConnell */
255767feec50SStephen McConnell void
255867feec50SStephen McConnell mpr_build_nvme_prp(struct mpr_softc *sc, struct mpr_command *cm,
255967feec50SStephen McConnell     Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request, void *data,
256067feec50SStephen McConnell     uint32_t data_in_sz, uint32_t data_out_sz)
256167feec50SStephen McConnell {
256267feec50SStephen McConnell 	int			prp_size = PRP_ENTRY_SIZE;
256367feec50SStephen McConnell 	uint64_t		*prp_entry, *prp1_entry, *prp2_entry;
256467feec50SStephen McConnell 	uint64_t		*prp_entry_phys, *prp_page, *prp_page_phys;
256567feec50SStephen McConnell 	uint32_t		offset, entry_len, page_mask_result, page_mask;
256667feec50SStephen McConnell 	bus_addr_t		paddr;
256767feec50SStephen McConnell 	size_t			length;
256867feec50SStephen McConnell 	struct mpr_prp_page	*prp_page_info = NULL;
256967feec50SStephen McConnell 
257067feec50SStephen McConnell 	/*
257167feec50SStephen McConnell 	 * Not all commands require a data transfer. If no data, just return
257267feec50SStephen McConnell 	 * without constructing any PRP.
257367feec50SStephen McConnell 	 */
257467feec50SStephen McConnell 	if (!data_in_sz && !data_out_sz)
257567feec50SStephen McConnell 		return;
257667feec50SStephen McConnell 
257767feec50SStephen McConnell 	/*
257867feec50SStephen McConnell 	 * Set pointers to PRP1 and PRP2, which are in the NVMe command. PRP1 is
257967feec50SStephen McConnell 	 * located at a 24 byte offset from the start of the NVMe command. Then
258067feec50SStephen McConnell 	 * set the current PRP entry pointer to PRP1.
258167feec50SStephen McConnell 	 */
258267feec50SStephen McConnell 	prp1_entry = (uint64_t *)(nvme_encap_request->NVMe_Command +
258367feec50SStephen McConnell 	    NVME_CMD_PRP1_OFFSET);
258467feec50SStephen McConnell 	prp2_entry = (uint64_t *)(nvme_encap_request->NVMe_Command +
258567feec50SStephen McConnell 	    NVME_CMD_PRP2_OFFSET);
258667feec50SStephen McConnell 	prp_entry = prp1_entry;
258767feec50SStephen McConnell 
258867feec50SStephen McConnell 	/*
258967feec50SStephen McConnell 	 * For the PRP entries, use the specially allocated buffer of
259067feec50SStephen McConnell 	 * contiguous memory. PRP Page allocation failures should not happen
259167feec50SStephen McConnell 	 * because there should be enough PRP page buffers to account for the
259267feec50SStephen McConnell 	 * possible NVMe QDepth.
259367feec50SStephen McConnell 	 */
259467feec50SStephen McConnell 	prp_page_info = mpr_alloc_prp_page(sc);
259567feec50SStephen McConnell 	KASSERT(prp_page_info != NULL, ("%s: There are no PRP Pages left to be "
259667feec50SStephen McConnell 	    "used for building a native NVMe SGL.\n", __func__));
259767feec50SStephen McConnell 	prp_page = (uint64_t *)prp_page_info->prp_page;
259867feec50SStephen McConnell 	prp_page_phys = (uint64_t *)(uintptr_t)prp_page_info->prp_page_busaddr;
259967feec50SStephen McConnell 
260067feec50SStephen McConnell 	/*
260167feec50SStephen McConnell 	 * Insert the allocated PRP page into the command's PRP page list. This
260267feec50SStephen McConnell 	 * will be freed when the command is freed.
260367feec50SStephen McConnell 	 */
260467feec50SStephen McConnell 	TAILQ_INSERT_TAIL(&cm->cm_prp_page_list, prp_page_info, prp_page_link);
260567feec50SStephen McConnell 
260667feec50SStephen McConnell 	/*
260767feec50SStephen McConnell 	 * Check if we are within 1 entry of a page boundary we don't want our
260867feec50SStephen McConnell 	 * first entry to be a PRP List entry.
260967feec50SStephen McConnell 	 */
261067feec50SStephen McConnell 	page_mask = PAGE_SIZE - 1;
261167feec50SStephen McConnell 	page_mask_result = (uintptr_t)((uint8_t *)prp_page + prp_size) &
261267feec50SStephen McConnell 	    page_mask;
261367feec50SStephen McConnell 	if (!page_mask_result)
261467feec50SStephen McConnell 	{
261567feec50SStephen McConnell 		/* Bump up to next page boundary. */
261667feec50SStephen McConnell 		prp_page = (uint64_t *)((uint8_t *)prp_page + prp_size);
261767feec50SStephen McConnell 		prp_page_phys = (uint64_t *)((uint8_t *)prp_page_phys +
261867feec50SStephen McConnell 		    prp_size);
261967feec50SStephen McConnell 	}
262067feec50SStephen McConnell 
262167feec50SStephen McConnell 	/*
262267feec50SStephen McConnell 	 * Set PRP physical pointer, which initially points to the current PRP
262367feec50SStephen McConnell 	 * DMA memory page.
262467feec50SStephen McConnell 	 */
262567feec50SStephen McConnell 	prp_entry_phys = prp_page_phys;
262667feec50SStephen McConnell 
262767feec50SStephen McConnell 	/* Get physical address and length of the data buffer. */
262867feec50SStephen McConnell 	paddr = (bus_addr_t)data;
262967feec50SStephen McConnell 	if (data_in_sz)
263067feec50SStephen McConnell 		length = data_in_sz;
263167feec50SStephen McConnell 	else
263267feec50SStephen McConnell 		length = data_out_sz;
263367feec50SStephen McConnell 
263467feec50SStephen McConnell 	/* Loop while the length is not zero. */
263567feec50SStephen McConnell 	while (length)
263667feec50SStephen McConnell 	{
263767feec50SStephen McConnell 		/*
263867feec50SStephen McConnell 		 * Check if we need to put a list pointer here if we are at page
263967feec50SStephen McConnell 		 * boundary - prp_size (8 bytes).
264067feec50SStephen McConnell 		 */
264167feec50SStephen McConnell 		page_mask_result = (uintptr_t)((uint8_t *)prp_entry_phys +
264267feec50SStephen McConnell 		    prp_size) & page_mask;
264367feec50SStephen McConnell 		if (!page_mask_result)
264467feec50SStephen McConnell 		{
264567feec50SStephen McConnell 			/*
264667feec50SStephen McConnell 			 * This is the last entry in a PRP List, so we need to
264767feec50SStephen McConnell 			 * put a PRP list pointer here. What this does is:
264867feec50SStephen McConnell 			 *   - bump the current memory pointer to the next
264967feec50SStephen McConnell 			 *     address, which will be the next full page.
265067feec50SStephen McConnell 			 *   - set the PRP Entry to point to that page. This is
265167feec50SStephen McConnell 			 *     now the PRP List pointer.
265267feec50SStephen McConnell 			 *   - bump the PRP Entry pointer the start of the next
265367feec50SStephen McConnell 			 *     page. Since all of this PRP memory is contiguous,
265467feec50SStephen McConnell 			 *     no need to get a new page - it's just the next
265567feec50SStephen McConnell 			 *     address.
265667feec50SStephen McConnell 			 */
265767feec50SStephen McConnell 			prp_entry_phys++;
265867feec50SStephen McConnell 			*prp_entry =
265967feec50SStephen McConnell 			    htole64((uint64_t)(uintptr_t)prp_entry_phys);
266067feec50SStephen McConnell 			prp_entry++;
266167feec50SStephen McConnell 		}
266267feec50SStephen McConnell 
266367feec50SStephen McConnell 		/* Need to handle if entry will be part of a page. */
266467feec50SStephen McConnell 		offset = (uint32_t)paddr & page_mask;
266567feec50SStephen McConnell 		entry_len = PAGE_SIZE - offset;
266667feec50SStephen McConnell 
266767feec50SStephen McConnell 		if (prp_entry == prp1_entry)
266867feec50SStephen McConnell 		{
266967feec50SStephen McConnell 			/*
267067feec50SStephen McConnell 			 * Must fill in the first PRP pointer (PRP1) before
267167feec50SStephen McConnell 			 * moving on.
267267feec50SStephen McConnell 			 */
267367feec50SStephen McConnell 			*prp1_entry = htole64((uint64_t)paddr);
267467feec50SStephen McConnell 
267567feec50SStephen McConnell 			/*
267667feec50SStephen McConnell 			 * Now point to the second PRP entry within the
267767feec50SStephen McConnell 			 * command (PRP2).
267867feec50SStephen McConnell 			 */
267967feec50SStephen McConnell 			prp_entry = prp2_entry;
268067feec50SStephen McConnell 		}
268167feec50SStephen McConnell 		else if (prp_entry == prp2_entry)
268267feec50SStephen McConnell 		{
268367feec50SStephen McConnell 			/*
268467feec50SStephen McConnell 			 * Should the PRP2 entry be a PRP List pointer or just a
268567feec50SStephen McConnell 			 * regular PRP pointer? If there is more than one more
268667feec50SStephen McConnell 			 * page of data, must use a PRP List pointer.
268767feec50SStephen McConnell 			 */
268867feec50SStephen McConnell 			if (length > PAGE_SIZE)
268967feec50SStephen McConnell 			{
269067feec50SStephen McConnell 				/*
269167feec50SStephen McConnell 				 * PRP2 will contain a PRP List pointer because
269267feec50SStephen McConnell 				 * more PRP's are needed with this command. The
269367feec50SStephen McConnell 				 * list will start at the beginning of the
269467feec50SStephen McConnell 				 * contiguous buffer.
269567feec50SStephen McConnell 				 */
269667feec50SStephen McConnell 				*prp2_entry =
269767feec50SStephen McConnell 				    htole64(
269867feec50SStephen McConnell 				    (uint64_t)(uintptr_t)prp_entry_phys);
269967feec50SStephen McConnell 
270067feec50SStephen McConnell 				/*
270167feec50SStephen McConnell 				 * The next PRP Entry will be the start of the
270267feec50SStephen McConnell 				 * first PRP List.
270367feec50SStephen McConnell 				 */
270467feec50SStephen McConnell 				prp_entry = prp_page;
270567feec50SStephen McConnell 			}
270667feec50SStephen McConnell 			else
270767feec50SStephen McConnell 			{
270867feec50SStephen McConnell 				/*
270967feec50SStephen McConnell 				 * After this, the PRP Entries are complete.
271067feec50SStephen McConnell 				 * This command uses 2 PRP's and no PRP list.
271167feec50SStephen McConnell 				 */
271267feec50SStephen McConnell 				*prp2_entry = htole64((uint64_t)paddr);
271367feec50SStephen McConnell 			}
271467feec50SStephen McConnell 		}
271567feec50SStephen McConnell 		else
271667feec50SStephen McConnell 		{
271767feec50SStephen McConnell 			/*
271867feec50SStephen McConnell 			 * Put entry in list and bump the addresses.
271967feec50SStephen McConnell 			 *
272067feec50SStephen McConnell 			 * After PRP1 and PRP2 are filled in, this will fill in
272167feec50SStephen McConnell 			 * all remaining PRP entries in a PRP List, one per each
272267feec50SStephen McConnell 			 * time through the loop.
272367feec50SStephen McConnell 			 */
272467feec50SStephen McConnell 			*prp_entry = htole64((uint64_t)paddr);
272567feec50SStephen McConnell 			prp_entry++;
272667feec50SStephen McConnell 			prp_entry_phys++;
272767feec50SStephen McConnell 		}
272867feec50SStephen McConnell 
272967feec50SStephen McConnell 		/*
273067feec50SStephen McConnell 		 * Bump the phys address of the command's data buffer by the
273167feec50SStephen McConnell 		 * entry_len.
273267feec50SStephen McConnell 		 */
273367feec50SStephen McConnell 		paddr += entry_len;
273467feec50SStephen McConnell 
273567feec50SStephen McConnell 		/* Decrement length accounting for last partial page. */
273667feec50SStephen McConnell 		if (entry_len > length)
273767feec50SStephen McConnell 			length = 0;
273867feec50SStephen McConnell 		else
273967feec50SStephen McConnell 			length -= entry_len;
274067feec50SStephen McConnell 	}
274167feec50SStephen McConnell }
274267feec50SStephen McConnell 
274367feec50SStephen McConnell /*
274467feec50SStephen McConnell  * mpr_check_pcie_native_sgl - This function is called for PCIe end devices to
274567feec50SStephen McConnell  * determine if the driver needs to build a native SGL. If so, that native SGL
274667feec50SStephen McConnell  * is built in the contiguous buffers allocated especially for PCIe SGL
274767feec50SStephen McConnell  * creation. If the driver will not build a native SGL, return TRUE and a
274867feec50SStephen McConnell  * normal IEEE SGL will be built. Currently this routine supports NVMe devices
274967feec50SStephen McConnell  * only.
275067feec50SStephen McConnell  *
275167feec50SStephen McConnell  * Returns FALSE (0) if native SGL was built, TRUE (1) if no SGL was built.
275267feec50SStephen McConnell  */
275367feec50SStephen McConnell static int
275467feec50SStephen McConnell mpr_check_pcie_native_sgl(struct mpr_softc *sc, struct mpr_command *cm,
275567feec50SStephen McConnell     bus_dma_segment_t *segs, int segs_left)
275667feec50SStephen McConnell {
275767feec50SStephen McConnell 	uint32_t		i, sge_dwords, length, offset, entry_len;
275867feec50SStephen McConnell 	uint32_t		num_entries, buff_len = 0, sges_in_segment;
275967feec50SStephen McConnell 	uint32_t		page_mask, page_mask_result, *curr_buff;
276067feec50SStephen McConnell 	uint32_t		*ptr_sgl, *ptr_first_sgl, first_page_offset;
276167feec50SStephen McConnell 	uint32_t		first_page_data_size, end_residual;
276267feec50SStephen McConnell 	uint64_t		*msg_phys;
276367feec50SStephen McConnell 	bus_addr_t		paddr;
276467feec50SStephen McConnell 	int			build_native_sgl = 0, first_prp_entry;
276567feec50SStephen McConnell 	int			prp_size = PRP_ENTRY_SIZE;
276667feec50SStephen McConnell 	Mpi25IeeeSgeChain64_t	*main_chain_element = NULL;
276767feec50SStephen McConnell 	struct mpr_prp_page	*prp_page_info = NULL;
276867feec50SStephen McConnell 
276967feec50SStephen McConnell 	mpr_dprint(sc, MPR_TRACE, "%s\n", __func__);
277067feec50SStephen McConnell 
277167feec50SStephen McConnell 	/*
277267feec50SStephen McConnell 	 * Add up the sizes of each segment length to get the total transfer
277367feec50SStephen McConnell 	 * size, which will be checked against the Maximum Data Transfer Size.
277467feec50SStephen McConnell 	 * If the data transfer length exceeds the MDTS for this device, just
277567feec50SStephen McConnell 	 * return 1 so a normal IEEE SGL will be built. F/W will break the I/O
277667feec50SStephen McConnell 	 * up into multiple I/O's. [nvme_mdts = 0 means unlimited]
277767feec50SStephen McConnell 	 */
277867feec50SStephen McConnell 	for (i = 0; i < segs_left; i++)
277967feec50SStephen McConnell 		buff_len += htole32(segs[i].ds_len);
278067feec50SStephen McConnell 	if ((cm->cm_targ->MDTS > 0) && (buff_len > cm->cm_targ->MDTS))
278167feec50SStephen McConnell 		return 1;
278267feec50SStephen McConnell 
278367feec50SStephen McConnell 	/* Create page_mask (to get offset within page) */
278467feec50SStephen McConnell 	page_mask = PAGE_SIZE - 1;
278567feec50SStephen McConnell 
278667feec50SStephen McConnell 	/*
278767feec50SStephen McConnell 	 * Check if the number of elements exceeds the max number that can be
278867feec50SStephen McConnell 	 * put in the main message frame (H/W can only translate an SGL that
278967feec50SStephen McConnell 	 * is contained entirely in the main message frame).
279067feec50SStephen McConnell 	 */
279167feec50SStephen McConnell 	sges_in_segment = (sc->facts->IOCRequestFrameSize -
279267feec50SStephen McConnell 	    offsetof(Mpi25SCSIIORequest_t, SGL)) / sizeof(MPI25_SGE_IO_UNION);
279367feec50SStephen McConnell 	if (segs_left > sges_in_segment)
279467feec50SStephen McConnell 		build_native_sgl = 1;
279567feec50SStephen McConnell 	else
279667feec50SStephen McConnell 	{
279767feec50SStephen McConnell 		/*
279867feec50SStephen McConnell 		 * NVMe uses one PRP for each physical page (or part of physical
279967feec50SStephen McConnell 		 * page).
280067feec50SStephen McConnell 		 *    if 4 pages or less then IEEE is OK
280167feec50SStephen McConnell 		 *    if > 5 pages then we need to build a native SGL
280267feec50SStephen McConnell 		 *    if > 4 and <= 5 pages, then check the physical address of
280367feec50SStephen McConnell 		 *      the first SG entry, then if this first size in the page
280467feec50SStephen McConnell 		 *      is >= the residual beyond 4 pages then use IEEE,
280567feec50SStephen McConnell 		 *      otherwise use native SGL
280667feec50SStephen McConnell 		 */
280767feec50SStephen McConnell 		if (buff_len > (PAGE_SIZE * 5))
280867feec50SStephen McConnell 			build_native_sgl = 1;
280967feec50SStephen McConnell 		else if ((buff_len > (PAGE_SIZE * 4)) &&
281067feec50SStephen McConnell 		    (buff_len <= (PAGE_SIZE * 5)) )
281167feec50SStephen McConnell 		{
281267feec50SStephen McConnell 			msg_phys = (uint64_t *)segs[0].ds_addr;
281367feec50SStephen McConnell 			first_page_offset =
281467feec50SStephen McConnell 			    ((uint32_t)(uint64_t)(uintptr_t)msg_phys &
281567feec50SStephen McConnell 			    page_mask);
281667feec50SStephen McConnell 			first_page_data_size = PAGE_SIZE - first_page_offset;
281767feec50SStephen McConnell 			end_residual = buff_len % PAGE_SIZE;
281867feec50SStephen McConnell 
281967feec50SStephen McConnell 			/*
282067feec50SStephen McConnell 			 * If offset into first page pushes the end of the data
282167feec50SStephen McConnell 			 * beyond end of the 5th page, we need the extra PRP
282267feec50SStephen McConnell 			 * list.
282367feec50SStephen McConnell 			 */
282467feec50SStephen McConnell 			if (first_page_data_size < end_residual)
282567feec50SStephen McConnell 				build_native_sgl = 1;
282667feec50SStephen McConnell 
282767feec50SStephen McConnell 			/*
282867feec50SStephen McConnell 			 * Check if first SG entry size is < residual beyond 4
282967feec50SStephen McConnell 			 * pages.
283067feec50SStephen McConnell 			 */
283167feec50SStephen McConnell 			if (htole32(segs[0].ds_len) <
283267feec50SStephen McConnell 			    (buff_len - (PAGE_SIZE * 4)))
283367feec50SStephen McConnell 				build_native_sgl = 1;
283467feec50SStephen McConnell 		}
283567feec50SStephen McConnell 	}
283667feec50SStephen McConnell 
283767feec50SStephen McConnell 	/* check if native SGL is needed */
283867feec50SStephen McConnell 	if (!build_native_sgl)
283967feec50SStephen McConnell 		return 1;
284067feec50SStephen McConnell 
284167feec50SStephen McConnell 	/*
284267feec50SStephen McConnell 	 * Native SGL is needed.
284367feec50SStephen McConnell 	 * Put a chain element in main message frame that points to the first
284467feec50SStephen McConnell 	 * chain buffer.
284567feec50SStephen McConnell 	 *
284667feec50SStephen McConnell 	 * NOTE:  The ChainOffset field must be 0 when using a chain pointer to
284767feec50SStephen McConnell 	 *        a native SGL.
284867feec50SStephen McConnell 	 */
284967feec50SStephen McConnell 
285067feec50SStephen McConnell 	/* Set main message chain element pointer */
285167feec50SStephen McConnell 	main_chain_element = (pMpi25IeeeSgeChain64_t)cm->cm_sge;
285267feec50SStephen McConnell 
285367feec50SStephen McConnell 	/*
285467feec50SStephen McConnell 	 * For NVMe the chain element needs to be the 2nd SGL entry in the main
285567feec50SStephen McConnell 	 * message.
285667feec50SStephen McConnell 	 */
285767feec50SStephen McConnell 	main_chain_element = (Mpi25IeeeSgeChain64_t *)
285867feec50SStephen McConnell 	    ((uint8_t *)main_chain_element + sizeof(MPI25_IEEE_SGE_CHAIN64));
285967feec50SStephen McConnell 
286067feec50SStephen McConnell 	/*
286167feec50SStephen McConnell 	 * For the PRP entries, use the specially allocated buffer of
286267feec50SStephen McConnell 	 * contiguous memory. PRP Page allocation failures should not happen
286367feec50SStephen McConnell 	 * because there should be enough PRP page buffers to account for the
286467feec50SStephen McConnell 	 * possible NVMe QDepth.
286567feec50SStephen McConnell 	 */
286667feec50SStephen McConnell 	prp_page_info = mpr_alloc_prp_page(sc);
286767feec50SStephen McConnell 	KASSERT(prp_page_info != NULL, ("%s: There are no PRP Pages left to be "
286867feec50SStephen McConnell 	    "used for building a native NVMe SGL.\n", __func__));
286967feec50SStephen McConnell 	curr_buff = (uint32_t *)prp_page_info->prp_page;
287067feec50SStephen McConnell 	msg_phys = (uint64_t *)(uintptr_t)prp_page_info->prp_page_busaddr;
287167feec50SStephen McConnell 
287267feec50SStephen McConnell 	/*
287367feec50SStephen McConnell 	 * Insert the allocated PRP page into the command's PRP page list. This
287467feec50SStephen McConnell 	 * will be freed when the command is freed.
287567feec50SStephen McConnell 	 */
287667feec50SStephen McConnell 	TAILQ_INSERT_TAIL(&cm->cm_prp_page_list, prp_page_info, prp_page_link);
287767feec50SStephen McConnell 
287867feec50SStephen McConnell 	/*
287967feec50SStephen McConnell 	 * Check if we are within 1 entry of a page boundary we don't want our
288067feec50SStephen McConnell 	 * first entry to be a PRP List entry.
288167feec50SStephen McConnell 	 */
288267feec50SStephen McConnell 	page_mask_result = (uintptr_t)((uint8_t *)curr_buff + prp_size) &
288367feec50SStephen McConnell 	    page_mask;
288467feec50SStephen McConnell 	if (!page_mask_result) {
288567feec50SStephen McConnell 		/* Bump up to next page boundary. */
288667feec50SStephen McConnell 		curr_buff = (uint32_t *)((uint8_t *)curr_buff + prp_size);
288767feec50SStephen McConnell 		msg_phys = (uint64_t *)((uint8_t *)msg_phys + prp_size);
288867feec50SStephen McConnell 	}
288967feec50SStephen McConnell 
289067feec50SStephen McConnell 	/* Fill in the chain element and make it an NVMe segment type. */
289167feec50SStephen McConnell 	main_chain_element->Address.High =
289267feec50SStephen McConnell 	    htole32((uint32_t)((uint64_t)(uintptr_t)msg_phys >> 32));
289367feec50SStephen McConnell 	main_chain_element->Address.Low =
289467feec50SStephen McConnell 	    htole32((uint32_t)(uintptr_t)msg_phys);
289567feec50SStephen McConnell 	main_chain_element->NextChainOffset = 0;
289667feec50SStephen McConnell 	main_chain_element->Flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
289767feec50SStephen McConnell 	    MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
289867feec50SStephen McConnell 	    MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
289967feec50SStephen McConnell 
290067feec50SStephen McConnell 	/* Set SGL pointer to start of contiguous PCIe buffer. */
290167feec50SStephen McConnell 	ptr_sgl = curr_buff;
290267feec50SStephen McConnell 	sge_dwords = 2;
290367feec50SStephen McConnell 	num_entries = 0;
290467feec50SStephen McConnell 
290567feec50SStephen McConnell 	/*
290667feec50SStephen McConnell 	 * NVMe has a very convoluted PRP format. One PRP is required for each
290767feec50SStephen McConnell 	 * page or partial page. We need to split up OS SG entries if they are
290867feec50SStephen McConnell 	 * longer than one page or cross a page boundary. We also have to insert
290967feec50SStephen McConnell 	 * a PRP list pointer entry as the last entry in each physical page of
291067feec50SStephen McConnell 	 * the PRP list.
291167feec50SStephen McConnell 	 *
291267feec50SStephen McConnell 	 * NOTE: The first PRP "entry" is actually placed in the first SGL entry
291367feec50SStephen McConnell 	 * in the main message in IEEE 64 format. The 2nd entry in the main
291467feec50SStephen McConnell 	 * message is the chain element, and the rest of the PRP entries are
291567feec50SStephen McConnell 	 * built in the contiguous PCIe buffer.
291667feec50SStephen McConnell 	 */
291767feec50SStephen McConnell 	first_prp_entry = 1;
291867feec50SStephen McConnell 	ptr_first_sgl = (uint32_t *)cm->cm_sge;
291967feec50SStephen McConnell 
292067feec50SStephen McConnell 	for (i = 0; i < segs_left; i++) {
292167feec50SStephen McConnell 		/* Get physical address and length of this SG entry. */
292267feec50SStephen McConnell 		paddr = segs[i].ds_addr;
292367feec50SStephen McConnell 		length = segs[i].ds_len;
292467feec50SStephen McConnell 
292567feec50SStephen McConnell 		/*
292667feec50SStephen McConnell 		 * Check whether a given SGE buffer lies on a non-PAGED
292767feec50SStephen McConnell 		 * boundary if this is not the first page. If so, this is not
292867feec50SStephen McConnell 		 * expected so have FW build the SGL.
292967feec50SStephen McConnell 		 */
2930757ff642SScott Long 		if ((i != 0) && (((uint32_t)paddr & page_mask) != 0)) {
293167feec50SStephen McConnell 			mpr_dprint(sc, MPR_ERROR, "Unaligned SGE while "
293267feec50SStephen McConnell 			    "building NVMe PRPs, low address is 0x%x\n",
293367feec50SStephen McConnell 			    (uint32_t)paddr);
293467feec50SStephen McConnell 			return 1;
293567feec50SStephen McConnell 		}
293667feec50SStephen McConnell 
293767feec50SStephen McConnell 		/* Apart from last SGE, if any other SGE boundary is not page
293867feec50SStephen McConnell 		 * aligned then it means that hole exists. Existence of hole
293967feec50SStephen McConnell 		 * leads to data corruption. So fallback to IEEE SGEs.
294067feec50SStephen McConnell 		 */
294167feec50SStephen McConnell 		if (i != (segs_left - 1)) {
294267feec50SStephen McConnell 			if (((uint32_t)paddr + length) & page_mask) {
294367feec50SStephen McConnell 				mpr_dprint(sc, MPR_ERROR, "Unaligned SGE "
294467feec50SStephen McConnell 				    "boundary while building NVMe PRPs, low "
294567feec50SStephen McConnell 				    "address: 0x%x and length: %u\n",
294667feec50SStephen McConnell 				    (uint32_t)paddr, length);
294767feec50SStephen McConnell 				return 1;
294867feec50SStephen McConnell 			}
294967feec50SStephen McConnell 		}
295067feec50SStephen McConnell 
295167feec50SStephen McConnell 		/* Loop while the length is not zero. */
295267feec50SStephen McConnell 		while (length) {
295367feec50SStephen McConnell 			/*
295467feec50SStephen McConnell 			 * Check if we need to put a list pointer here if we are
295567feec50SStephen McConnell 			 * at page boundary - prp_size.
295667feec50SStephen McConnell 			 */
295767feec50SStephen McConnell 			page_mask_result = (uintptr_t)((uint8_t *)ptr_sgl +
295867feec50SStephen McConnell 			    prp_size) & page_mask;
295967feec50SStephen McConnell 			if (!page_mask_result) {
296067feec50SStephen McConnell 				/*
296167feec50SStephen McConnell 				 * Need to put a PRP list pointer here.
296267feec50SStephen McConnell 				 */
296367feec50SStephen McConnell 				msg_phys = (uint64_t *)((uint8_t *)msg_phys +
296467feec50SStephen McConnell 				    prp_size);
296567feec50SStephen McConnell 				*ptr_sgl = htole32((uintptr_t)msg_phys);
296667feec50SStephen McConnell 				*(ptr_sgl+1) = htole32((uint64_t)(uintptr_t)
296767feec50SStephen McConnell 				    msg_phys >> 32);
296867feec50SStephen McConnell 				ptr_sgl += sge_dwords;
296967feec50SStephen McConnell 				num_entries++;
297067feec50SStephen McConnell 			}
297167feec50SStephen McConnell 
297267feec50SStephen McConnell 			/* Need to handle if entry will be part of a page. */
297367feec50SStephen McConnell 			offset = (uint32_t)paddr & page_mask;
297467feec50SStephen McConnell 			entry_len = PAGE_SIZE - offset;
297567feec50SStephen McConnell 			if (first_prp_entry) {
297667feec50SStephen McConnell 				/*
297767feec50SStephen McConnell 				 * Put IEEE entry in first SGE in main message.
297867feec50SStephen McConnell 				 * (Simple element, System addr, not end of
297967feec50SStephen McConnell 				 * list.)
298067feec50SStephen McConnell 				 */
298167feec50SStephen McConnell 				*ptr_first_sgl = htole32((uint32_t)paddr);
298267feec50SStephen McConnell 				*(ptr_first_sgl + 1) =
298367feec50SStephen McConnell 				    htole32((uint32_t)((uint64_t)paddr >> 32));
298467feec50SStephen McConnell 				*(ptr_first_sgl + 2) = htole32(entry_len);
298567feec50SStephen McConnell 				*(ptr_first_sgl + 3) = 0;
298667feec50SStephen McConnell 
298767feec50SStephen McConnell 				/* No longer the first PRP entry. */
298867feec50SStephen McConnell 				first_prp_entry = 0;
298967feec50SStephen McConnell 			} else {
299067feec50SStephen McConnell 				/* Put entry in list. */
299167feec50SStephen McConnell 				*ptr_sgl = htole32((uint32_t)paddr);
299267feec50SStephen McConnell 				*(ptr_sgl + 1) =
299367feec50SStephen McConnell 				    htole32((uint32_t)((uint64_t)paddr >> 32));
299467feec50SStephen McConnell 
299567feec50SStephen McConnell 				/* Bump ptr_sgl, msg_phys, and num_entries. */
299667feec50SStephen McConnell 				ptr_sgl += sge_dwords;
299767feec50SStephen McConnell 				msg_phys = (uint64_t *)((uint8_t *)msg_phys +
299867feec50SStephen McConnell 				    prp_size);
299967feec50SStephen McConnell 				num_entries++;
300067feec50SStephen McConnell 			}
300167feec50SStephen McConnell 
300267feec50SStephen McConnell 			/* Bump the phys address by the entry_len. */
300367feec50SStephen McConnell 			paddr += entry_len;
300467feec50SStephen McConnell 
300567feec50SStephen McConnell 			/* Decrement length accounting for last partial page. */
300667feec50SStephen McConnell 			if (entry_len > length)
300767feec50SStephen McConnell 				length = 0;
300867feec50SStephen McConnell 			else
300967feec50SStephen McConnell 				length -= entry_len;
301067feec50SStephen McConnell 		}
301167feec50SStephen McConnell 	}
301267feec50SStephen McConnell 
301367feec50SStephen McConnell 	/* Set chain element Length. */
301467feec50SStephen McConnell 	main_chain_element->Length = htole32(num_entries * prp_size);
301567feec50SStephen McConnell 
301667feec50SStephen McConnell 	/* Return 0, indicating we built a native SGL. */
301767feec50SStephen McConnell 	return 0;
301867feec50SStephen McConnell }
301967feec50SStephen McConnell 
3020991554f2SKenneth D. Merry /*
3021991554f2SKenneth D. Merry  * Add a chain element as the next SGE for the specified command.
3022991554f2SKenneth D. Merry  * Reset cm_sge and cm_sgesize to indicate all the available space. Chains are
3023991554f2SKenneth D. Merry  * only required for IEEE commands.  Therefore there is no code for commands
3024a2c14879SStephen McConnell  * that have the MPR_CM_FLAGS_SGE_SIMPLE flag set (and those commands
3025a2c14879SStephen McConnell  * shouldn't be requesting chains).
3026991554f2SKenneth D. Merry  */
3027991554f2SKenneth D. Merry static int
3028991554f2SKenneth D. Merry mpr_add_chain(struct mpr_command *cm, int segsleft)
3029991554f2SKenneth D. Merry {
3030991554f2SKenneth D. Merry 	struct mpr_softc *sc = cm->cm_sc;
3031991554f2SKenneth D. Merry 	MPI2_REQUEST_HEADER *req;
3032991554f2SKenneth D. Merry 	MPI25_IEEE_SGE_CHAIN64 *ieee_sgc;
3033991554f2SKenneth D. Merry 	struct mpr_chain *chain;
30342bbc5fcbSStephen McConnell 	int sgc_size, current_segs, rem_segs, segs_per_frame;
3035991554f2SKenneth D. Merry 	uint8_t next_chain_offset = 0;
3036991554f2SKenneth D. Merry 
3037991554f2SKenneth D. Merry 	/*
3038991554f2SKenneth D. Merry 	 * Fail if a command is requesting a chain for SIMPLE SGE's.  For SAS3
3039991554f2SKenneth D. Merry 	 * only IEEE commands should be requesting chains.  Return some error
3040991554f2SKenneth D. Merry 	 * code other than 0.
3041991554f2SKenneth D. Merry 	 */
3042991554f2SKenneth D. Merry 	if (cm->cm_flags & MPR_CM_FLAGS_SGE_SIMPLE) {
3043991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_ERROR, "A chain element cannot be added to "
3044991554f2SKenneth D. Merry 		    "an MPI SGL.\n");
3045991554f2SKenneth D. Merry 		return(ENOBUFS);
3046991554f2SKenneth D. Merry 	}
3047991554f2SKenneth D. Merry 
3048991554f2SKenneth D. Merry 	sgc_size = sizeof(MPI25_IEEE_SGE_CHAIN64);
3049991554f2SKenneth D. Merry 	if (cm->cm_sglsize < sgc_size)
3050991554f2SKenneth D. Merry 		panic("MPR: Need SGE Error Code\n");
3051991554f2SKenneth D. Merry 
3052991554f2SKenneth D. Merry 	chain = mpr_alloc_chain(cm->cm_sc);
3053991554f2SKenneth D. Merry 	if (chain == NULL)
3054991554f2SKenneth D. Merry 		return (ENOBUFS);
3055991554f2SKenneth D. Merry 
3056991554f2SKenneth D. Merry 	/*
3057991554f2SKenneth D. Merry 	 * Note: a double-linked list is used to make it easier to walk for
3058991554f2SKenneth D. Merry 	 * debugging.
3059991554f2SKenneth D. Merry 	 */
3060991554f2SKenneth D. Merry 	TAILQ_INSERT_TAIL(&cm->cm_chain_list, chain, chain_link);
3061991554f2SKenneth D. Merry 
3062991554f2SKenneth D. Merry 	/*
3063991554f2SKenneth D. Merry 	 * Need to know if the number of frames left is more than 1 or not.  If
3064991554f2SKenneth D. Merry 	 * more than 1 frame is required, NextChainOffset will need to be set,
3065991554f2SKenneth D. Merry 	 * which will just be the last segment of the frame.
3066991554f2SKenneth D. Merry 	 */
3067991554f2SKenneth D. Merry 	rem_segs = 0;
3068991554f2SKenneth D. Merry 	if (cm->cm_sglsize < (sgc_size * segsleft)) {
3069991554f2SKenneth D. Merry 		/*
3070991554f2SKenneth D. Merry 		 * rem_segs is the number of segements remaining after the
3071991554f2SKenneth D. Merry 		 * segments that will go into the current frame.  Since it is
3072991554f2SKenneth D. Merry 		 * known that at least one more frame is required, account for
3073991554f2SKenneth D. Merry 		 * the chain element.  To know if more than one more frame is
3074991554f2SKenneth D. Merry 		 * required, just check if there will be a remainder after using
3075991554f2SKenneth D. Merry 		 * the current frame (with this chain) and the next frame.  If
3076991554f2SKenneth D. Merry 		 * so the NextChainOffset must be the last element of the next
3077991554f2SKenneth D. Merry 		 * frame.
3078991554f2SKenneth D. Merry 		 */
3079991554f2SKenneth D. Merry 		current_segs = (cm->cm_sglsize / sgc_size) - 1;
3080991554f2SKenneth D. Merry 		rem_segs = segsleft - current_segs;
30812bbc5fcbSStephen McConnell 		segs_per_frame = sc->chain_frame_size / sgc_size;
3082991554f2SKenneth D. Merry 		if (rem_segs > segs_per_frame) {
3083991554f2SKenneth D. Merry 			next_chain_offset = segs_per_frame - 1;
3084991554f2SKenneth D. Merry 		}
3085991554f2SKenneth D. Merry 	}
3086991554f2SKenneth D. Merry 	ieee_sgc = &((MPI25_SGE_IO_UNION *)cm->cm_sge)->IeeeChain;
30872bbc5fcbSStephen McConnell 	ieee_sgc->Length = next_chain_offset ?
30882bbc5fcbSStephen McConnell 	    htole32((uint32_t)sc->chain_frame_size) :
3089991554f2SKenneth D. Merry 	    htole32((uint32_t)rem_segs * (uint32_t)sgc_size);
3090991554f2SKenneth D. Merry 	ieee_sgc->NextChainOffset = next_chain_offset;
3091991554f2SKenneth D. Merry 	ieee_sgc->Flags = (MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
3092991554f2SKenneth D. Merry 	    MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR);
3093991554f2SKenneth D. Merry 	ieee_sgc->Address.Low = htole32(chain->chain_busaddr);
3094991554f2SKenneth D. Merry 	ieee_sgc->Address.High = htole32(chain->chain_busaddr >> 32);
3095991554f2SKenneth D. Merry 	cm->cm_sge = &((MPI25_SGE_IO_UNION *)chain->chain)->IeeeSimple;
3096991554f2SKenneth D. Merry 	req = (MPI2_REQUEST_HEADER *)cm->cm_req;
30972bbc5fcbSStephen McConnell 	req->ChainOffset = (sc->chain_frame_size - sgc_size) >> 4;
3098991554f2SKenneth D. Merry 
30992bbc5fcbSStephen McConnell 	cm->cm_sglsize = sc->chain_frame_size;
3100991554f2SKenneth D. Merry 	return (0);
3101991554f2SKenneth D. Merry }
3102991554f2SKenneth D. Merry 
3103991554f2SKenneth D. Merry /*
3104991554f2SKenneth D. Merry  * Add one scatter-gather element to the scatter-gather list for a command.
3105a2c14879SStephen McConnell  * Maintain cm_sglsize and cm_sge as the remaining size and pointer to the
3106a2c14879SStephen McConnell  * next SGE to fill in, respectively.  In Gen3, the MPI SGL does not have a
3107a2c14879SStephen McConnell  * chain, so don't consider any chain additions.
3108991554f2SKenneth D. Merry  */
3109991554f2SKenneth D. Merry int
3110991554f2SKenneth D. Merry mpr_push_sge(struct mpr_command *cm, MPI2_SGE_SIMPLE64 *sge, size_t len,
3111991554f2SKenneth D. Merry     int segsleft)
3112991554f2SKenneth D. Merry {
3113991554f2SKenneth D. Merry 	uint32_t saved_buf_len, saved_address_low, saved_address_high;
3114991554f2SKenneth D. Merry 	u32 sge_flags;
3115991554f2SKenneth D. Merry 
3116991554f2SKenneth D. Merry 	/*
3117991554f2SKenneth D. Merry 	 * case 1: >=1 more segment, no room for anything (error)
3118991554f2SKenneth D. Merry 	 * case 2: 1 more segment and enough room for it
3119991554f2SKenneth D. Merry          */
3120991554f2SKenneth D. Merry 
3121991554f2SKenneth D. Merry 	if (cm->cm_sglsize < (segsleft * sizeof(MPI2_SGE_SIMPLE64))) {
3122991554f2SKenneth D. Merry 		mpr_dprint(cm->cm_sc, MPR_ERROR,
3123991554f2SKenneth D. Merry 		    "%s: warning: Not enough room for MPI SGL in frame.\n",
3124991554f2SKenneth D. Merry 		    __func__);
3125991554f2SKenneth D. Merry 		return(ENOBUFS);
3126991554f2SKenneth D. Merry 	}
3127991554f2SKenneth D. Merry 
3128991554f2SKenneth D. Merry 	KASSERT(segsleft == 1,
3129991554f2SKenneth D. Merry 	    ("segsleft cannot be more than 1 for an MPI SGL; segsleft = %d\n",
3130991554f2SKenneth D. Merry 	    segsleft));
3131991554f2SKenneth D. Merry 
3132991554f2SKenneth D. Merry 	/*
3133991554f2SKenneth D. Merry 	 * There is one more segment left to add for the MPI SGL and there is
3134991554f2SKenneth D. Merry 	 * enough room in the frame to add it.  This is the normal case because
3135991554f2SKenneth D. Merry 	 * MPI SGL's don't have chains, otherwise something is wrong.
3136991554f2SKenneth D. Merry 	 *
3137991554f2SKenneth D. Merry 	 * If this is a bi-directional request, need to account for that
3138991554f2SKenneth D. Merry 	 * here.  Save the pre-filled sge values.  These will be used
3139991554f2SKenneth D. Merry 	 * either for the 2nd SGL or for a single direction SGL.  If
3140991554f2SKenneth D. Merry 	 * cm_out_len is non-zero, this is a bi-directional request, so
3141991554f2SKenneth D. Merry 	 * fill in the OUT SGL first, then the IN SGL, otherwise just
3142991554f2SKenneth D. Merry 	 * fill in the IN SGL.  Note that at this time, when filling in
3143991554f2SKenneth D. Merry 	 * 2 SGL's for a bi-directional request, they both use the same
3144991554f2SKenneth D. Merry 	 * DMA buffer (same cm command).
3145991554f2SKenneth D. Merry 	 */
3146991554f2SKenneth D. Merry 	saved_buf_len = sge->FlagsLength & 0x00FFFFFF;
3147991554f2SKenneth D. Merry 	saved_address_low = sge->Address.Low;
3148991554f2SKenneth D. Merry 	saved_address_high = sge->Address.High;
3149991554f2SKenneth D. Merry 	if (cm->cm_out_len) {
3150991554f2SKenneth D. Merry 		sge->FlagsLength = cm->cm_out_len |
3151991554f2SKenneth D. Merry 		    ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
3152991554f2SKenneth D. Merry 		    MPI2_SGE_FLAGS_END_OF_BUFFER |
3153991554f2SKenneth D. Merry 		    MPI2_SGE_FLAGS_HOST_TO_IOC |
3154991554f2SKenneth D. Merry 		    MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
3155991554f2SKenneth D. Merry 		    MPI2_SGE_FLAGS_SHIFT);
3156991554f2SKenneth D. Merry 		cm->cm_sglsize -= len;
3157991554f2SKenneth D. Merry 		/* Endian Safe code */
3158991554f2SKenneth D. Merry 		sge_flags = sge->FlagsLength;
3159991554f2SKenneth D. Merry 		sge->FlagsLength = htole32(sge_flags);
3160991554f2SKenneth D. Merry 		sge->Address.High = htole32(sge->Address.High);
3161991554f2SKenneth D. Merry 		sge->Address.Low = htole32(sge->Address.Low);
3162991554f2SKenneth D. Merry 		bcopy(sge, cm->cm_sge, len);
3163991554f2SKenneth D. Merry 		cm->cm_sge = (MPI2_SGE_IO_UNION *)((uintptr_t)cm->cm_sge + len);
3164991554f2SKenneth D. Merry 	}
3165991554f2SKenneth D. Merry 	sge->FlagsLength = saved_buf_len |
3166991554f2SKenneth D. Merry 	    ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
3167991554f2SKenneth D. Merry 	    MPI2_SGE_FLAGS_END_OF_BUFFER |
3168991554f2SKenneth D. Merry 	    MPI2_SGE_FLAGS_LAST_ELEMENT |
3169991554f2SKenneth D. Merry 	    MPI2_SGE_FLAGS_END_OF_LIST |
3170991554f2SKenneth D. Merry 	    MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
3171991554f2SKenneth D. Merry 	    MPI2_SGE_FLAGS_SHIFT);
3172991554f2SKenneth D. Merry 	if (cm->cm_flags & MPR_CM_FLAGS_DATAIN) {
3173991554f2SKenneth D. Merry 		sge->FlagsLength |=
3174991554f2SKenneth D. Merry 		    ((uint32_t)(MPI2_SGE_FLAGS_IOC_TO_HOST) <<
3175991554f2SKenneth D. Merry 		    MPI2_SGE_FLAGS_SHIFT);
3176991554f2SKenneth D. Merry 	} else {
3177991554f2SKenneth D. Merry 		sge->FlagsLength |=
3178991554f2SKenneth D. Merry 		    ((uint32_t)(MPI2_SGE_FLAGS_HOST_TO_IOC) <<
3179991554f2SKenneth D. Merry 		    MPI2_SGE_FLAGS_SHIFT);
3180991554f2SKenneth D. Merry 	}
3181991554f2SKenneth D. Merry 	sge->Address.Low = saved_address_low;
3182991554f2SKenneth D. Merry 	sge->Address.High = saved_address_high;
3183991554f2SKenneth D. Merry 
3184991554f2SKenneth D. Merry 	cm->cm_sglsize -= len;
3185991554f2SKenneth D. Merry 	/* Endian Safe code */
3186991554f2SKenneth D. Merry 	sge_flags = sge->FlagsLength;
3187991554f2SKenneth D. Merry 	sge->FlagsLength = htole32(sge_flags);
3188991554f2SKenneth D. Merry 	sge->Address.High = htole32(sge->Address.High);
3189991554f2SKenneth D. Merry 	sge->Address.Low = htole32(sge->Address.Low);
3190991554f2SKenneth D. Merry 	bcopy(sge, cm->cm_sge, len);
3191991554f2SKenneth D. Merry 	cm->cm_sge = (MPI2_SGE_IO_UNION *)((uintptr_t)cm->cm_sge + len);
3192991554f2SKenneth D. Merry 	return (0);
3193991554f2SKenneth D. Merry }
3194991554f2SKenneth D. Merry 
3195991554f2SKenneth D. Merry /*
3196991554f2SKenneth D. Merry  * Add one IEEE scatter-gather element (chain or simple) to the IEEE scatter-
3197991554f2SKenneth D. Merry  * gather list for a command.  Maintain cm_sglsize and cm_sge as the
3198991554f2SKenneth D. Merry  * remaining size and pointer to the next SGE to fill in, respectively.
3199991554f2SKenneth D. Merry  */
3200991554f2SKenneth D. Merry int
3201991554f2SKenneth D. Merry mpr_push_ieee_sge(struct mpr_command *cm, void *sgep, int segsleft)
3202991554f2SKenneth D. Merry {
3203991554f2SKenneth D. Merry 	MPI2_IEEE_SGE_SIMPLE64 *sge = sgep;
3204991554f2SKenneth D. Merry 	int error, ieee_sge_size = sizeof(MPI25_SGE_IO_UNION);
3205991554f2SKenneth D. Merry 	uint32_t saved_buf_len, saved_address_low, saved_address_high;
3206991554f2SKenneth D. Merry 	uint32_t sge_length;
3207991554f2SKenneth D. Merry 
3208991554f2SKenneth D. Merry 	/*
3209991554f2SKenneth D. Merry 	 * case 1: No room for chain or segment (error).
3210991554f2SKenneth D. Merry 	 * case 2: Two or more segments left but only room for chain.
3211991554f2SKenneth D. Merry 	 * case 3: Last segment and room for it, so set flags.
3212991554f2SKenneth D. Merry 	 */
3213991554f2SKenneth D. Merry 
3214991554f2SKenneth D. Merry 	/*
3215991554f2SKenneth D. Merry 	 * There should be room for at least one element, or there is a big
3216991554f2SKenneth D. Merry 	 * problem.
3217991554f2SKenneth D. Merry 	 */
3218991554f2SKenneth D. Merry 	if (cm->cm_sglsize < ieee_sge_size)
3219991554f2SKenneth D. Merry 		panic("MPR: Need SGE Error Code\n");
3220991554f2SKenneth D. Merry 
3221991554f2SKenneth D. Merry 	if ((segsleft >= 2) && (cm->cm_sglsize < (ieee_sge_size * 2))) {
3222991554f2SKenneth D. Merry 		if ((error = mpr_add_chain(cm, segsleft)) != 0)
3223991554f2SKenneth D. Merry 			return (error);
3224991554f2SKenneth D. Merry 	}
3225991554f2SKenneth D. Merry 
3226991554f2SKenneth D. Merry 	if (segsleft == 1) {
3227991554f2SKenneth D. Merry 		/*
3228991554f2SKenneth D. Merry 		 * If this is a bi-directional request, need to account for that
3229991554f2SKenneth D. Merry 		 * here.  Save the pre-filled sge values.  These will be used
3230991554f2SKenneth D. Merry 		 * either for the 2nd SGL or for a single direction SGL.  If
3231991554f2SKenneth D. Merry 		 * cm_out_len is non-zero, this is a bi-directional request, so
3232991554f2SKenneth D. Merry 		 * fill in the OUT SGL first, then the IN SGL, otherwise just
3233991554f2SKenneth D. Merry 		 * fill in the IN SGL.  Note that at this time, when filling in
3234991554f2SKenneth D. Merry 		 * 2 SGL's for a bi-directional request, they both use the same
3235991554f2SKenneth D. Merry 		 * DMA buffer (same cm command).
3236991554f2SKenneth D. Merry 		 */
3237991554f2SKenneth D. Merry 		saved_buf_len = sge->Length;
3238991554f2SKenneth D. Merry 		saved_address_low = sge->Address.Low;
3239991554f2SKenneth D. Merry 		saved_address_high = sge->Address.High;
3240991554f2SKenneth D. Merry 		if (cm->cm_out_len) {
3241991554f2SKenneth D. Merry 			sge->Length = cm->cm_out_len;
3242991554f2SKenneth D. Merry 			sge->Flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
3243991554f2SKenneth D. Merry 			    MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR);
3244991554f2SKenneth D. Merry 			cm->cm_sglsize -= ieee_sge_size;
3245991554f2SKenneth D. Merry 			/* Endian Safe code */
3246991554f2SKenneth D. Merry 			sge_length = sge->Length;
3247991554f2SKenneth D. Merry 			sge->Length = htole32(sge_length);
3248991554f2SKenneth D. Merry 			sge->Address.High = htole32(sge->Address.High);
3249991554f2SKenneth D. Merry 			sge->Address.Low = htole32(sge->Address.Low);
3250991554f2SKenneth D. Merry 			bcopy(sgep, cm->cm_sge, ieee_sge_size);
3251991554f2SKenneth D. Merry 			cm->cm_sge =
3252991554f2SKenneth D. Merry 			    (MPI25_SGE_IO_UNION *)((uintptr_t)cm->cm_sge +
3253991554f2SKenneth D. Merry 			    ieee_sge_size);
3254991554f2SKenneth D. Merry 		}
3255991554f2SKenneth D. Merry 		sge->Length = saved_buf_len;
3256991554f2SKenneth D. Merry 		sge->Flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
3257991554f2SKenneth D. Merry 		    MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
3258991554f2SKenneth D. Merry 		    MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
3259991554f2SKenneth D. Merry 		sge->Address.Low = saved_address_low;
3260991554f2SKenneth D. Merry 		sge->Address.High = saved_address_high;
3261991554f2SKenneth D. Merry 	}
3262991554f2SKenneth D. Merry 
3263991554f2SKenneth D. Merry 	cm->cm_sglsize -= ieee_sge_size;
3264991554f2SKenneth D. Merry 	/* Endian Safe code */
3265991554f2SKenneth D. Merry 	sge_length = sge->Length;
3266991554f2SKenneth D. Merry 	sge->Length = htole32(sge_length);
3267991554f2SKenneth D. Merry 	sge->Address.High = htole32(sge->Address.High);
3268991554f2SKenneth D. Merry 	sge->Address.Low = htole32(sge->Address.Low);
3269991554f2SKenneth D. Merry 	bcopy(sgep, cm->cm_sge, ieee_sge_size);
3270991554f2SKenneth D. Merry 	cm->cm_sge = (MPI25_SGE_IO_UNION *)((uintptr_t)cm->cm_sge +
3271991554f2SKenneth D. Merry 	    ieee_sge_size);
3272991554f2SKenneth D. Merry 	return (0);
3273991554f2SKenneth D. Merry }
3274991554f2SKenneth D. Merry 
3275991554f2SKenneth D. Merry /*
3276991554f2SKenneth D. Merry  * Add one dma segment to the scatter-gather list for a command.
3277991554f2SKenneth D. Merry  */
3278991554f2SKenneth D. Merry int
3279991554f2SKenneth D. Merry mpr_add_dmaseg(struct mpr_command *cm, vm_paddr_t pa, size_t len, u_int flags,
3280991554f2SKenneth D. Merry     int segsleft)
3281991554f2SKenneth D. Merry {
3282991554f2SKenneth D. Merry 	MPI2_SGE_SIMPLE64 sge;
3283991554f2SKenneth D. Merry 	MPI2_IEEE_SGE_SIMPLE64 ieee_sge;
3284991554f2SKenneth D. Merry 
3285991554f2SKenneth D. Merry 	if (!(cm->cm_flags & MPR_CM_FLAGS_SGE_SIMPLE)) {
3286991554f2SKenneth D. Merry 		ieee_sge.Flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
3287991554f2SKenneth D. Merry 		    MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR);
3288991554f2SKenneth D. Merry 		ieee_sge.Length = len;
3289991554f2SKenneth D. Merry 		mpr_from_u64(pa, &ieee_sge.Address);
3290991554f2SKenneth D. Merry 
3291991554f2SKenneth D. Merry 		return (mpr_push_ieee_sge(cm, &ieee_sge, segsleft));
3292991554f2SKenneth D. Merry 	} else {
3293991554f2SKenneth D. Merry 		/*
3294991554f2SKenneth D. Merry 		 * This driver always uses 64-bit address elements for
3295991554f2SKenneth D. Merry 		 * simplicity.
3296991554f2SKenneth D. Merry 		 */
3297991554f2SKenneth D. Merry 		flags |= MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
3298991554f2SKenneth D. Merry 		    MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
3299991554f2SKenneth D. Merry 		/* Set Endian safe macro in mpr_push_sge */
3300991554f2SKenneth D. Merry 		sge.FlagsLength = len | (flags << MPI2_SGE_FLAGS_SHIFT);
3301991554f2SKenneth D. Merry 		mpr_from_u64(pa, &sge.Address);
3302991554f2SKenneth D. Merry 
3303991554f2SKenneth D. Merry 		return (mpr_push_sge(cm, &sge, sizeof sge, segsleft));
3304991554f2SKenneth D. Merry 	}
3305991554f2SKenneth D. Merry }
3306991554f2SKenneth D. Merry 
3307991554f2SKenneth D. Merry static void
3308991554f2SKenneth D. Merry mpr_data_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
3309991554f2SKenneth D. Merry {
3310991554f2SKenneth D. Merry 	struct mpr_softc *sc;
3311991554f2SKenneth D. Merry 	struct mpr_command *cm;
3312991554f2SKenneth D. Merry 	u_int i, dir, sflags;
3313991554f2SKenneth D. Merry 
3314991554f2SKenneth D. Merry 	cm = (struct mpr_command *)arg;
3315991554f2SKenneth D. Merry 	sc = cm->cm_sc;
3316991554f2SKenneth D. Merry 
3317991554f2SKenneth D. Merry 	/*
3318991554f2SKenneth D. Merry 	 * In this case, just print out a warning and let the chip tell the
3319991554f2SKenneth D. Merry 	 * user they did the wrong thing.
3320991554f2SKenneth D. Merry 	 */
3321991554f2SKenneth D. Merry 	if ((cm->cm_max_segs != 0) && (nsegs > cm->cm_max_segs)) {
33227a2a6a1aSStephen McConnell 		mpr_dprint(sc, MPR_ERROR, "%s: warning: busdma returned %d "
33237a2a6a1aSStephen McConnell 		    "segments, more than the %d allowed\n", __func__, nsegs,
3324991554f2SKenneth D. Merry 		    cm->cm_max_segs);
3325991554f2SKenneth D. Merry 	}
3326991554f2SKenneth D. Merry 
3327991554f2SKenneth D. Merry 	/*
3328991554f2SKenneth D. Merry 	 * Set up DMA direction flags.  Bi-directional requests are also handled
3329991554f2SKenneth D. Merry 	 * here.  In that case, both direction flags will be set.
3330991554f2SKenneth D. Merry 	 */
3331991554f2SKenneth D. Merry 	sflags = 0;
3332991554f2SKenneth D. Merry 	if (cm->cm_flags & MPR_CM_FLAGS_SMP_PASS) {
3333991554f2SKenneth D. Merry 		/*
3334991554f2SKenneth D. Merry 		 * We have to add a special case for SMP passthrough, there
3335991554f2SKenneth D. Merry 		 * is no easy way to generically handle it.  The first
3336991554f2SKenneth D. Merry 		 * S/G element is used for the command (therefore the
3337991554f2SKenneth D. Merry 		 * direction bit needs to be set).  The second one is used
3338991554f2SKenneth D. Merry 		 * for the reply.  We'll leave it to the caller to make
3339991554f2SKenneth D. Merry 		 * sure we only have two buffers.
3340991554f2SKenneth D. Merry 		 */
3341991554f2SKenneth D. Merry 		/*
3342991554f2SKenneth D. Merry 		 * Even though the busdma man page says it doesn't make
3343991554f2SKenneth D. Merry 		 * sense to have both direction flags, it does in this case.
3344991554f2SKenneth D. Merry 		 * We have one s/g element being accessed in each direction.
3345991554f2SKenneth D. Merry 		 */
3346991554f2SKenneth D. Merry 		dir = BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD;
3347991554f2SKenneth D. Merry 
3348991554f2SKenneth D. Merry 		/*
3349991554f2SKenneth D. Merry 		 * Set the direction flag on the first buffer in the SMP
3350991554f2SKenneth D. Merry 		 * passthrough request.  We'll clear it for the second one.
3351991554f2SKenneth D. Merry 		 */
3352991554f2SKenneth D. Merry 		sflags |= MPI2_SGE_FLAGS_DIRECTION |
3353991554f2SKenneth D. Merry 			  MPI2_SGE_FLAGS_END_OF_BUFFER;
3354991554f2SKenneth D. Merry 	} else if (cm->cm_flags & MPR_CM_FLAGS_DATAOUT) {
3355991554f2SKenneth D. Merry 		sflags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
3356991554f2SKenneth D. Merry 		dir = BUS_DMASYNC_PREWRITE;
3357991554f2SKenneth D. Merry 	} else
3358991554f2SKenneth D. Merry 		dir = BUS_DMASYNC_PREREAD;
3359991554f2SKenneth D. Merry 
336067feec50SStephen McConnell 	/* Check if a native SG list is needed for an NVMe PCIe device. */
336167feec50SStephen McConnell 	if (cm->cm_targ && cm->cm_targ->is_nvme &&
336267feec50SStephen McConnell 	    mpr_check_pcie_native_sgl(sc, cm, segs, nsegs) == 0) {
336367feec50SStephen McConnell 		/* A native SG list was built, skip to end. */
336467feec50SStephen McConnell 		goto out;
336567feec50SStephen McConnell 	}
336667feec50SStephen McConnell 
3367991554f2SKenneth D. Merry 	for (i = 0; i < nsegs; i++) {
3368991554f2SKenneth D. Merry 		if ((cm->cm_flags & MPR_CM_FLAGS_SMP_PASS) && (i != 0)) {
3369991554f2SKenneth D. Merry 			sflags &= ~MPI2_SGE_FLAGS_DIRECTION;
3370991554f2SKenneth D. Merry 		}
3371991554f2SKenneth D. Merry 		error = mpr_add_dmaseg(cm, segs[i].ds_addr, segs[i].ds_len,
3372991554f2SKenneth D. Merry 		    sflags, nsegs - i);
3373991554f2SKenneth D. Merry 		if (error != 0) {
3374991554f2SKenneth D. Merry 			/* Resource shortage, roll back! */
3375991554f2SKenneth D. Merry 			if (ratecheck(&sc->lastfail, &mpr_chainfail_interval))
3376991554f2SKenneth D. Merry 				mpr_dprint(sc, MPR_INFO, "Out of chain frames, "
3377991554f2SKenneth D. Merry 				    "consider increasing hw.mpr.max_chains.\n");
3378991554f2SKenneth D. Merry 			cm->cm_flags |= MPR_CM_FLAGS_CHAIN_FAILED;
3379991554f2SKenneth D. Merry 			mpr_complete_command(sc, cm);
3380991554f2SKenneth D. Merry 			return;
3381991554f2SKenneth D. Merry 		}
3382991554f2SKenneth D. Merry 	}
3383991554f2SKenneth D. Merry 
338467feec50SStephen McConnell out:
3385991554f2SKenneth D. Merry 	bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir);
3386991554f2SKenneth D. Merry 	mpr_enqueue_request(sc, cm);
3387991554f2SKenneth D. Merry 
3388991554f2SKenneth D. Merry 	return;
3389991554f2SKenneth D. Merry }
3390991554f2SKenneth D. Merry 
3391991554f2SKenneth D. Merry static void
3392991554f2SKenneth D. Merry mpr_data_cb2(void *arg, bus_dma_segment_t *segs, int nsegs, bus_size_t mapsize,
3393991554f2SKenneth D. Merry 	     int error)
3394991554f2SKenneth D. Merry {
3395991554f2SKenneth D. Merry 	mpr_data_cb(arg, segs, nsegs, error);
3396991554f2SKenneth D. Merry }
3397991554f2SKenneth D. Merry 
3398991554f2SKenneth D. Merry /*
3399991554f2SKenneth D. Merry  * This is the routine to enqueue commands ansynchronously.
3400991554f2SKenneth D. Merry  * Note that the only error path here is from bus_dmamap_load(), which can
3401991554f2SKenneth D. Merry  * return EINPROGRESS if it is waiting for resources.  Other than this, it's
3402991554f2SKenneth D. Merry  * assumed that if you have a command in-hand, then you have enough credits
3403991554f2SKenneth D. Merry  * to use it.
3404991554f2SKenneth D. Merry  */
3405991554f2SKenneth D. Merry int
3406991554f2SKenneth D. Merry mpr_map_command(struct mpr_softc *sc, struct mpr_command *cm)
3407991554f2SKenneth D. Merry {
3408991554f2SKenneth D. Merry 	int error = 0;
3409991554f2SKenneth D. Merry 
3410991554f2SKenneth D. Merry 	if (cm->cm_flags & MPR_CM_FLAGS_USE_UIO) {
3411991554f2SKenneth D. Merry 		error = bus_dmamap_load_uio(sc->buffer_dmat, cm->cm_dmamap,
3412991554f2SKenneth D. Merry 		    &cm->cm_uio, mpr_data_cb2, cm, 0);
3413991554f2SKenneth D. Merry 	} else if (cm->cm_flags & MPR_CM_FLAGS_USE_CCB) {
3414991554f2SKenneth D. Merry 		error = bus_dmamap_load_ccb(sc->buffer_dmat, cm->cm_dmamap,
3415991554f2SKenneth D. Merry 		    cm->cm_data, mpr_data_cb, cm, 0);
3416991554f2SKenneth D. Merry 	} else if ((cm->cm_data != NULL) && (cm->cm_length != 0)) {
3417991554f2SKenneth D. Merry 		error = bus_dmamap_load(sc->buffer_dmat, cm->cm_dmamap,
3418991554f2SKenneth D. Merry 		    cm->cm_data, cm->cm_length, mpr_data_cb, cm, 0);
3419991554f2SKenneth D. Merry 	} else {
3420991554f2SKenneth D. Merry 		/* Add a zero-length element as needed */
3421991554f2SKenneth D. Merry 		if (cm->cm_sge != NULL)
3422991554f2SKenneth D. Merry 			mpr_add_dmaseg(cm, 0, 0, 0, 1);
3423991554f2SKenneth D. Merry 		mpr_enqueue_request(sc, cm);
3424991554f2SKenneth D. Merry 	}
3425991554f2SKenneth D. Merry 
3426991554f2SKenneth D. Merry 	return (error);
3427991554f2SKenneth D. Merry }
3428991554f2SKenneth D. Merry 
3429991554f2SKenneth D. Merry /*
3430991554f2SKenneth D. Merry  * This is the routine to enqueue commands synchronously.  An error of
3431991554f2SKenneth D. Merry  * EINPROGRESS from mpr_map_command() is ignored since the command will
3432991554f2SKenneth D. Merry  * be executed and enqueued automatically.  Other errors come from msleep().
3433991554f2SKenneth D. Merry  */
3434991554f2SKenneth D. Merry int
34356d4ffcb4SKenneth D. Merry mpr_wait_command(struct mpr_softc *sc, struct mpr_command **cmp, int timeout,
3436991554f2SKenneth D. Merry     int sleep_flag)
3437991554f2SKenneth D. Merry {
3438991554f2SKenneth D. Merry 	int error, rc;
3439991554f2SKenneth D. Merry 	struct timeval cur_time, start_time;
34406d4ffcb4SKenneth D. Merry 	struct mpr_command *cm = *cmp;
3441991554f2SKenneth D. Merry 
3442991554f2SKenneth D. Merry 	if (sc->mpr_flags & MPR_FLAGS_DIAGRESET)
3443991554f2SKenneth D. Merry 		return  EBUSY;
3444991554f2SKenneth D. Merry 
3445991554f2SKenneth D. Merry 	cm->cm_complete = NULL;
3446991554f2SKenneth D. Merry 	cm->cm_flags |= (MPR_CM_FLAGS_WAKEUP + MPR_CM_FLAGS_POLLED);
3447991554f2SKenneth D. Merry 	error = mpr_map_command(sc, cm);
3448991554f2SKenneth D. Merry 	if ((error != 0) && (error != EINPROGRESS))
3449991554f2SKenneth D. Merry 		return (error);
3450991554f2SKenneth D. Merry 
3451991554f2SKenneth D. Merry 	// Check for context and wait for 50 mSec at a time until time has
3452991554f2SKenneth D. Merry 	// expired or the command has finished.  If msleep can't be used, need
3453991554f2SKenneth D. Merry 	// to poll.
3454991554f2SKenneth D. Merry #if __FreeBSD_version >= 1000029
3455991554f2SKenneth D. Merry 	if (curthread->td_no_sleeping)
3456991554f2SKenneth D. Merry #else //__FreeBSD_version < 1000029
3457991554f2SKenneth D. Merry 	if (curthread->td_pflags & TDP_NOSLEEPING)
3458991554f2SKenneth D. Merry #endif //__FreeBSD_version >= 1000029
3459991554f2SKenneth D. Merry 		sleep_flag = NO_SLEEP;
3460417aa6b8SKenneth D. Merry 	getmicrouptime(&start_time);
3461991554f2SKenneth D. Merry 	if (mtx_owned(&sc->mpr_mtx) && sleep_flag == CAN_SLEEP) {
3462991554f2SKenneth D. Merry 		error = msleep(cm, &sc->mpr_mtx, 0, "mprwait", timeout*hz);
3463417aa6b8SKenneth D. Merry 		if (error == EWOULDBLOCK) {
3464417aa6b8SKenneth D. Merry 			/*
3465417aa6b8SKenneth D. Merry 			 * Record the actual elapsed time in the case of a
3466417aa6b8SKenneth D. Merry 			 * timeout for the message below.
3467417aa6b8SKenneth D. Merry 			 */
3468417aa6b8SKenneth D. Merry 			getmicrouptime(&cur_time);
3469417aa6b8SKenneth D. Merry 			timevalsub(&cur_time, &start_time);
3470417aa6b8SKenneth D. Merry 		}
3471991554f2SKenneth D. Merry 	} else {
3472991554f2SKenneth D. Merry 		while ((cm->cm_flags & MPR_CM_FLAGS_COMPLETE) == 0) {
3473991554f2SKenneth D. Merry 			mpr_intr_locked(sc);
3474991554f2SKenneth D. Merry 			if (sleep_flag == CAN_SLEEP)
3475991554f2SKenneth D. Merry 				pause("mprwait", hz/20);
3476991554f2SKenneth D. Merry 			else
3477991554f2SKenneth D. Merry 				DELAY(50000);
3478991554f2SKenneth D. Merry 
3479417aa6b8SKenneth D. Merry 			getmicrouptime(&cur_time);
3480417aa6b8SKenneth D. Merry 			timevalsub(&cur_time, &start_time);
3481417aa6b8SKenneth D. Merry 			if (cur_time.tv_sec > timeout) {
3482991554f2SKenneth D. Merry 				error = EWOULDBLOCK;
3483991554f2SKenneth D. Merry 				break;
3484991554f2SKenneth D. Merry 			}
3485991554f2SKenneth D. Merry 		}
3486991554f2SKenneth D. Merry 	}
3487991554f2SKenneth D. Merry 
3488991554f2SKenneth D. Merry 	if (error == EWOULDBLOCK) {
3489417aa6b8SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Calling Reinit from %s, timeout=%d,"
3490417aa6b8SKenneth D. Merry 		    " elapsed=%jd\n", __func__, timeout,
3491417aa6b8SKenneth D. Merry 		    (intmax_t)cur_time.tv_sec);
3492991554f2SKenneth D. Merry 		rc = mpr_reinit(sc);
3493991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Reinit %s\n", (rc == 0) ? "success" :
3494991554f2SKenneth D. Merry 		    "failed");
34956d4ffcb4SKenneth D. Merry 		if (sc->mpr_flags & MPR_FLAGS_REALLOCATED) {
34966d4ffcb4SKenneth D. Merry 			/*
34976d4ffcb4SKenneth D. Merry 			 * Tell the caller that we freed the command in a
34986d4ffcb4SKenneth D. Merry 			 * reinit.
34996d4ffcb4SKenneth D. Merry 			 */
35006d4ffcb4SKenneth D. Merry 			*cmp = NULL;
35016d4ffcb4SKenneth D. Merry 		}
3502991554f2SKenneth D. Merry 		error = ETIMEDOUT;
3503991554f2SKenneth D. Merry 	}
3504991554f2SKenneth D. Merry 	return (error);
3505991554f2SKenneth D. Merry }
3506991554f2SKenneth D. Merry 
3507991554f2SKenneth D. Merry /*
3508991554f2SKenneth D. Merry  * This is the routine to enqueue a command synchonously and poll for
3509991554f2SKenneth D. Merry  * completion.  Its use should be rare.
3510991554f2SKenneth D. Merry  */
3511991554f2SKenneth D. Merry int
35126d4ffcb4SKenneth D. Merry mpr_request_polled(struct mpr_softc *sc, struct mpr_command **cmp)
3513991554f2SKenneth D. Merry {
35146d4ffcb4SKenneth D. Merry 	int error, rc;
3515991554f2SKenneth D. Merry 	struct timeval cur_time, start_time;
35166d4ffcb4SKenneth D. Merry 	struct mpr_command *cm = *cmp;
3517991554f2SKenneth D. Merry 
3518991554f2SKenneth D. Merry 	error = 0;
3519991554f2SKenneth D. Merry 
3520991554f2SKenneth D. Merry 	cm->cm_flags |= MPR_CM_FLAGS_POLLED;
3521991554f2SKenneth D. Merry 	cm->cm_complete = NULL;
3522991554f2SKenneth D. Merry 	mpr_map_command(sc, cm);
3523991554f2SKenneth D. Merry 
35246d4ffcb4SKenneth D. Merry 	getmicrouptime(&start_time);
3525991554f2SKenneth D. Merry 	while ((cm->cm_flags & MPR_CM_FLAGS_COMPLETE) == 0) {
3526991554f2SKenneth D. Merry 		mpr_intr_locked(sc);
3527991554f2SKenneth D. Merry 
3528991554f2SKenneth D. Merry 		if (mtx_owned(&sc->mpr_mtx))
3529991554f2SKenneth D. Merry 			msleep(&sc->msleep_fake_chan, &sc->mpr_mtx, 0,
3530991554f2SKenneth D. Merry 			    "mprpoll", hz/20);
3531991554f2SKenneth D. Merry 		else
3532991554f2SKenneth D. Merry 			pause("mprpoll", hz/20);
3533991554f2SKenneth D. Merry 
3534991554f2SKenneth D. Merry 		/*
3535991554f2SKenneth D. Merry 		 * Check for real-time timeout and fail if more than 60 seconds.
3536991554f2SKenneth D. Merry 		 */
35376d4ffcb4SKenneth D. Merry 		getmicrouptime(&cur_time);
35386d4ffcb4SKenneth D. Merry 		timevalsub(&cur_time, &start_time);
35396d4ffcb4SKenneth D. Merry 		if (cur_time.tv_sec > 60) {
3540991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT, "polling failed\n");
3541991554f2SKenneth D. Merry 			error = ETIMEDOUT;
3542991554f2SKenneth D. Merry 			break;
3543991554f2SKenneth D. Merry 		}
3544991554f2SKenneth D. Merry 	}
3545991554f2SKenneth D. Merry 
3546991554f2SKenneth D. Merry 	if (error) {
3547991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "Calling Reinit from %s\n", __func__);
3548991554f2SKenneth D. Merry 		rc = mpr_reinit(sc);
35497a2a6a1aSStephen McConnell 		mpr_dprint(sc, MPR_FAULT, "Reinit %s\n", (rc == 0) ? "success" :
35507a2a6a1aSStephen McConnell 		    "failed");
35516d4ffcb4SKenneth D. Merry 
35526d4ffcb4SKenneth D. Merry 		if (sc->mpr_flags & MPR_FLAGS_REALLOCATED) {
35536d4ffcb4SKenneth D. Merry 			/*
35546d4ffcb4SKenneth D. Merry 			 * Tell the caller that we freed the command in a
35556d4ffcb4SKenneth D. Merry 			 * reinit.
35566d4ffcb4SKenneth D. Merry 			 */
35576d4ffcb4SKenneth D. Merry 			*cmp = NULL;
35586d4ffcb4SKenneth D. Merry 		}
3559991554f2SKenneth D. Merry 	}
3560991554f2SKenneth D. Merry 	return (error);
3561991554f2SKenneth D. Merry }
3562991554f2SKenneth D. Merry 
3563991554f2SKenneth D. Merry /*
3564991554f2SKenneth D. Merry  * The MPT driver had a verbose interface for config pages.  In this driver,
3565453130d9SPedro F. Giffuni  * reduce it to much simpler terms, similar to the Linux driver.
3566991554f2SKenneth D. Merry  */
3567991554f2SKenneth D. Merry int
3568991554f2SKenneth D. Merry mpr_read_config_page(struct mpr_softc *sc, struct mpr_config_params *params)
3569991554f2SKenneth D. Merry {
3570991554f2SKenneth D. Merry 	MPI2_CONFIG_REQUEST *req;
3571991554f2SKenneth D. Merry 	struct mpr_command *cm;
3572991554f2SKenneth D. Merry 	int error;
3573991554f2SKenneth D. Merry 
3574991554f2SKenneth D. Merry 	if (sc->mpr_flags & MPR_FLAGS_BUSY) {
3575991554f2SKenneth D. Merry 		return (EBUSY);
3576991554f2SKenneth D. Merry 	}
3577991554f2SKenneth D. Merry 
3578991554f2SKenneth D. Merry 	cm = mpr_alloc_command(sc);
3579991554f2SKenneth D. Merry 	if (cm == NULL) {
3580991554f2SKenneth D. Merry 		return (EBUSY);
3581991554f2SKenneth D. Merry 	}
3582991554f2SKenneth D. Merry 
3583991554f2SKenneth D. Merry 	req = (MPI2_CONFIG_REQUEST *)cm->cm_req;
3584991554f2SKenneth D. Merry 	req->Function = MPI2_FUNCTION_CONFIG;
3585991554f2SKenneth D. Merry 	req->Action = params->action;
3586991554f2SKenneth D. Merry 	req->SGLFlags = 0;
3587991554f2SKenneth D. Merry 	req->ChainOffset = 0;
3588991554f2SKenneth D. Merry 	req->PageAddress = params->page_address;
3589991554f2SKenneth D. Merry 	if (params->hdr.Struct.PageType == MPI2_CONFIG_PAGETYPE_EXTENDED) {
3590991554f2SKenneth D. Merry 		MPI2_CONFIG_EXTENDED_PAGE_HEADER *hdr;
3591991554f2SKenneth D. Merry 
3592991554f2SKenneth D. Merry 		hdr = &params->hdr.Ext;
3593991554f2SKenneth D. Merry 		req->ExtPageType = hdr->ExtPageType;
3594991554f2SKenneth D. Merry 		req->ExtPageLength = hdr->ExtPageLength;
3595991554f2SKenneth D. Merry 		req->Header.PageType = MPI2_CONFIG_PAGETYPE_EXTENDED;
3596991554f2SKenneth D. Merry 		req->Header.PageLength = 0; /* Must be set to zero */
3597991554f2SKenneth D. Merry 		req->Header.PageNumber = hdr->PageNumber;
3598991554f2SKenneth D. Merry 		req->Header.PageVersion = hdr->PageVersion;
3599991554f2SKenneth D. Merry 	} else {
3600991554f2SKenneth D. Merry 		MPI2_CONFIG_PAGE_HEADER *hdr;
3601991554f2SKenneth D. Merry 
3602991554f2SKenneth D. Merry 		hdr = &params->hdr.Struct;
3603991554f2SKenneth D. Merry 		req->Header.PageType = hdr->PageType;
3604991554f2SKenneth D. Merry 		req->Header.PageNumber = hdr->PageNumber;
3605991554f2SKenneth D. Merry 		req->Header.PageLength = hdr->PageLength;
3606991554f2SKenneth D. Merry 		req->Header.PageVersion = hdr->PageVersion;
3607991554f2SKenneth D. Merry 	}
3608991554f2SKenneth D. Merry 
3609991554f2SKenneth D. Merry 	cm->cm_data = params->buffer;
3610991554f2SKenneth D. Merry 	cm->cm_length = params->length;
3611a2c14879SStephen McConnell 	if (cm->cm_data != NULL) {
3612991554f2SKenneth D. Merry 		cm->cm_sge = &req->PageBufferSGE;
3613991554f2SKenneth D. Merry 		cm->cm_sglsize = sizeof(MPI2_SGE_IO_UNION);
3614991554f2SKenneth D. Merry 		cm->cm_flags = MPR_CM_FLAGS_SGE_SIMPLE | MPR_CM_FLAGS_DATAIN;
3615a2c14879SStephen McConnell 	} else
3616a2c14879SStephen McConnell 		cm->cm_sge = NULL;
3617991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3618991554f2SKenneth D. Merry 
3619991554f2SKenneth D. Merry 	cm->cm_complete_data = params;
3620991554f2SKenneth D. Merry 	if (params->callback != NULL) {
3621991554f2SKenneth D. Merry 		cm->cm_complete = mpr_config_complete;
3622991554f2SKenneth D. Merry 		return (mpr_map_command(sc, cm));
3623991554f2SKenneth D. Merry 	} else {
36246d4ffcb4SKenneth D. Merry 		error = mpr_wait_command(sc, &cm, 0, CAN_SLEEP);
3625991554f2SKenneth D. Merry 		if (error) {
3626991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT,
3627991554f2SKenneth D. Merry 			    "Error %d reading config page\n", error);
36286d4ffcb4SKenneth D. Merry 			if (cm != NULL)
3629991554f2SKenneth D. Merry 				mpr_free_command(sc, cm);
3630991554f2SKenneth D. Merry 			return (error);
3631991554f2SKenneth D. Merry 		}
3632991554f2SKenneth D. Merry 		mpr_config_complete(sc, cm);
3633991554f2SKenneth D. Merry 	}
3634991554f2SKenneth D. Merry 
3635991554f2SKenneth D. Merry 	return (0);
3636991554f2SKenneth D. Merry }
3637991554f2SKenneth D. Merry 
3638991554f2SKenneth D. Merry int
3639991554f2SKenneth D. Merry mpr_write_config_page(struct mpr_softc *sc, struct mpr_config_params *params)
3640991554f2SKenneth D. Merry {
3641991554f2SKenneth D. Merry 	return (EINVAL);
3642991554f2SKenneth D. Merry }
3643991554f2SKenneth D. Merry 
3644991554f2SKenneth D. Merry static void
3645991554f2SKenneth D. Merry mpr_config_complete(struct mpr_softc *sc, struct mpr_command *cm)
3646991554f2SKenneth D. Merry {
3647991554f2SKenneth D. Merry 	MPI2_CONFIG_REPLY *reply;
3648991554f2SKenneth D. Merry 	struct mpr_config_params *params;
3649991554f2SKenneth D. Merry 
3650991554f2SKenneth D. Merry 	MPR_FUNCTRACE(sc);
3651991554f2SKenneth D. Merry 	params = cm->cm_complete_data;
3652991554f2SKenneth D. Merry 
3653991554f2SKenneth D. Merry 	if (cm->cm_data != NULL) {
3654991554f2SKenneth D. Merry 		bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap,
3655991554f2SKenneth D. Merry 		    BUS_DMASYNC_POSTREAD);
3656991554f2SKenneth D. Merry 		bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
3657991554f2SKenneth D. Merry 	}
3658991554f2SKenneth D. Merry 
3659991554f2SKenneth D. Merry 	/*
3660991554f2SKenneth D. Merry 	 * XXX KDM need to do more error recovery?  This results in the
3661991554f2SKenneth D. Merry 	 * device in question not getting probed.
3662991554f2SKenneth D. Merry 	 */
3663991554f2SKenneth D. Merry 	if ((cm->cm_flags & MPR_CM_FLAGS_ERROR_MASK) != 0) {
3664991554f2SKenneth D. Merry 		params->status = MPI2_IOCSTATUS_BUSY;
3665991554f2SKenneth D. Merry 		goto done;
3666991554f2SKenneth D. Merry 	}
3667991554f2SKenneth D. Merry 
3668991554f2SKenneth D. Merry 	reply = (MPI2_CONFIG_REPLY *)cm->cm_reply;
3669991554f2SKenneth D. Merry 	if (reply == NULL) {
3670991554f2SKenneth D. Merry 		params->status = MPI2_IOCSTATUS_BUSY;
3671991554f2SKenneth D. Merry 		goto done;
3672991554f2SKenneth D. Merry 	}
3673991554f2SKenneth D. Merry 	params->status = reply->IOCStatus;
3674a2c14879SStephen McConnell 	if (params->hdr.Struct.PageType == MPI2_CONFIG_PAGETYPE_EXTENDED) {
3675991554f2SKenneth D. Merry 		params->hdr.Ext.ExtPageType = reply->ExtPageType;
3676991554f2SKenneth D. Merry 		params->hdr.Ext.ExtPageLength = reply->ExtPageLength;
3677a2c14879SStephen McConnell 		params->hdr.Ext.PageType = reply->Header.PageType;
3678a2c14879SStephen McConnell 		params->hdr.Ext.PageNumber = reply->Header.PageNumber;
3679a2c14879SStephen McConnell 		params->hdr.Ext.PageVersion = reply->Header.PageVersion;
3680991554f2SKenneth D. Merry 	} else {
3681991554f2SKenneth D. Merry 		params->hdr.Struct.PageType = reply->Header.PageType;
3682991554f2SKenneth D. Merry 		params->hdr.Struct.PageNumber = reply->Header.PageNumber;
3683991554f2SKenneth D. Merry 		params->hdr.Struct.PageLength = reply->Header.PageLength;
3684991554f2SKenneth D. Merry 		params->hdr.Struct.PageVersion = reply->Header.PageVersion;
3685991554f2SKenneth D. Merry 	}
3686991554f2SKenneth D. Merry 
3687991554f2SKenneth D. Merry done:
3688991554f2SKenneth D. Merry 	mpr_free_command(sc, cm);
3689991554f2SKenneth D. Merry 	if (params->callback != NULL)
3690991554f2SKenneth D. Merry 		params->callback(sc, params);
3691991554f2SKenneth D. Merry 
3692991554f2SKenneth D. Merry 	return;
3693991554f2SKenneth D. Merry }
3694