xref: /freebsd/sys/dev/mpr/mpr_user.c (revision 68cc77a3b73ffda1e8ac891b9852faca833e11b7)
1991554f2SKenneth D. Merry /*-
2991554f2SKenneth D. Merry  * Copyright (c) 2008 Yahoo!, Inc.
3991554f2SKenneth D. Merry  * All rights reserved.
4991554f2SKenneth D. Merry  * Written by: John Baldwin <jhb@FreeBSD.org>
5991554f2SKenneth D. Merry  *
6991554f2SKenneth D. Merry  * Redistribution and use in source and binary forms, with or without
7991554f2SKenneth D. Merry  * modification, are permitted provided that the following conditions
8991554f2SKenneth D. Merry  * are met:
9991554f2SKenneth D. Merry  * 1. Redistributions of source code must retain the above copyright
10991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer.
11991554f2SKenneth D. Merry  * 2. Redistributions in binary form must reproduce the above copyright
12991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer in the
13991554f2SKenneth D. Merry  *    documentation and/or other materials provided with the distribution.
14991554f2SKenneth D. Merry  * 3. Neither the name of the author nor the names of any co-contributors
15991554f2SKenneth D. Merry  *    may be used to endorse or promote products derived from this software
16991554f2SKenneth D. Merry  *    without specific prior written permission.
17991554f2SKenneth D. Merry  *
18991554f2SKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19991554f2SKenneth D. Merry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20991554f2SKenneth D. Merry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21991554f2SKenneth D. Merry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22991554f2SKenneth D. Merry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23991554f2SKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24991554f2SKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25991554f2SKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26991554f2SKenneth D. Merry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27991554f2SKenneth D. Merry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28991554f2SKenneth D. Merry  * SUCH DAMAGE.
29991554f2SKenneth D. Merry  *
3046b23587SKashyap D Desai  * Broadcom Inc. (LSI) MPT-Fusion Host Adapter FreeBSD userland interface
31991554f2SKenneth D. Merry  */
32991554f2SKenneth D. Merry /*-
33a2c14879SStephen McConnell  * Copyright (c) 2011-2015 LSI Corp.
347a2a6a1aSStephen McConnell  * Copyright (c) 2013-2016 Avago Technologies
3546b23587SKashyap D Desai  * Copyright 2000-2020 Broadcom Inc.
36991554f2SKenneth D. Merry  * All rights reserved.
37991554f2SKenneth D. Merry  *
38991554f2SKenneth D. Merry  * Redistribution and use in source and binary forms, with or without
39991554f2SKenneth D. Merry  * modification, are permitted provided that the following conditions
40991554f2SKenneth D. Merry  * are met:
41991554f2SKenneth D. Merry  * 1. Redistributions of source code must retain the above copyright
42991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer.
43991554f2SKenneth D. Merry  * 2. Redistributions in binary form must reproduce the above copyright
44991554f2SKenneth D. Merry  *    notice, this list of conditions and the following disclaimer in the
45991554f2SKenneth D. Merry  *    documentation and/or other materials provided with the distribution.
46991554f2SKenneth D. Merry  *
47991554f2SKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
48991554f2SKenneth D. Merry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49991554f2SKenneth D. Merry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50991554f2SKenneth D. Merry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
51991554f2SKenneth D. Merry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52991554f2SKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53991554f2SKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54991554f2SKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55991554f2SKenneth D. Merry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56991554f2SKenneth D. Merry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57991554f2SKenneth D. Merry  * SUCH DAMAGE.
58991554f2SKenneth D. Merry  *
5946b23587SKashyap D Desai  * Broadcom Inc. (LSI) MPT-Fusion Host Adapter FreeBSD
60991554f2SKenneth D. Merry  */
61991554f2SKenneth D. Merry 
62991554f2SKenneth D. Merry #include <sys/cdefs.h>
63991554f2SKenneth D. Merry /* TODO Move headers to mprvar */
64991554f2SKenneth D. Merry #include <sys/types.h>
65991554f2SKenneth D. Merry #include <sys/param.h>
66991554f2SKenneth D. Merry #include <sys/systm.h>
67991554f2SKenneth D. Merry #include <sys/kernel.h>
68991554f2SKenneth D. Merry #include <sys/selinfo.h>
69991554f2SKenneth D. Merry #include <sys/module.h>
70991554f2SKenneth D. Merry #include <sys/bus.h>
71991554f2SKenneth D. Merry #include <sys/conf.h>
72991554f2SKenneth D. Merry #include <sys/bio.h>
73562894f0SBrooks Davis #include <sys/abi_compat.h>
74991554f2SKenneth D. Merry #include <sys/malloc.h>
75991554f2SKenneth D. Merry #include <sys/uio.h>
76991554f2SKenneth D. Merry #include <sys/sysctl.h>
77991554f2SKenneth D. Merry #include <sys/ioccom.h>
78991554f2SKenneth D. Merry #include <sys/endian.h>
79991554f2SKenneth D. Merry #include <sys/queue.h>
80991554f2SKenneth D. Merry #include <sys/kthread.h>
81991554f2SKenneth D. Merry #include <sys/taskqueue.h>
82991554f2SKenneth D. Merry #include <sys/proc.h>
83991554f2SKenneth D. Merry #include <sys/sysent.h>
84991554f2SKenneth D. Merry 
85991554f2SKenneth D. Merry #include <machine/bus.h>
86991554f2SKenneth D. Merry #include <machine/resource.h>
87991554f2SKenneth D. Merry #include <sys/rman.h>
88991554f2SKenneth D. Merry 
89991554f2SKenneth D. Merry #include <cam/cam.h>
90a2c14879SStephen McConnell #include <cam/cam_ccb.h>
91991554f2SKenneth D. Merry 
92991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_type.h>
93991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2.h>
94991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_ioc.h>
95991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_cnfg.h>
96991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_init.h>
97991554f2SKenneth D. Merry #include <dev/mpr/mpi/mpi2_tool.h>
9867feec50SStephen McConnell #include <dev/mpr/mpi/mpi2_pci.h>
99991554f2SKenneth D. Merry #include <dev/mpr/mpr_ioctl.h>
100991554f2SKenneth D. Merry #include <dev/mpr/mprvar.h>
101991554f2SKenneth D. Merry #include <dev/mpr/mpr_table.h>
102991554f2SKenneth D. Merry #include <dev/mpr/mpr_sas.h>
103991554f2SKenneth D. Merry #include <dev/pci/pcivar.h>
104991554f2SKenneth D. Merry #include <dev/pci/pcireg.h>
105991554f2SKenneth D. Merry 
106991554f2SKenneth D. Merry static d_open_t		mpr_open;
107991554f2SKenneth D. Merry static d_close_t	mpr_close;
108991554f2SKenneth D. Merry static d_ioctl_t	mpr_ioctl_devsw;
109991554f2SKenneth D. Merry 
110991554f2SKenneth D. Merry static struct cdevsw mpr_cdevsw = {
111991554f2SKenneth D. Merry 	.d_version =	D_VERSION,
112991554f2SKenneth D. Merry 	.d_flags =	0,
113991554f2SKenneth D. Merry 	.d_open =	mpr_open,
114991554f2SKenneth D. Merry 	.d_close =	mpr_close,
115991554f2SKenneth D. Merry 	.d_ioctl =	mpr_ioctl_devsw,
116991554f2SKenneth D. Merry 	.d_name =	"mpr",
117991554f2SKenneth D. Merry };
118991554f2SKenneth D. Merry 
119991554f2SKenneth D. Merry typedef int (mpr_user_f)(struct mpr_command *, struct mpr_usr_command *);
120991554f2SKenneth D. Merry static mpr_user_f	mpi_pre_ioc_facts;
121991554f2SKenneth D. Merry static mpr_user_f	mpi_pre_port_facts;
122991554f2SKenneth D. Merry static mpr_user_f	mpi_pre_fw_download;
123991554f2SKenneth D. Merry static mpr_user_f	mpi_pre_fw_upload;
124991554f2SKenneth D. Merry static mpr_user_f	mpi_pre_sata_passthrough;
125991554f2SKenneth D. Merry static mpr_user_f	mpi_pre_smp_passthrough;
126991554f2SKenneth D. Merry static mpr_user_f	mpi_pre_config;
127991554f2SKenneth D. Merry static mpr_user_f	mpi_pre_sas_io_unit_control;
128991554f2SKenneth D. Merry 
129991554f2SKenneth D. Merry static int mpr_user_read_cfg_header(struct mpr_softc *,
130991554f2SKenneth D. Merry     struct mpr_cfg_page_req *);
131991554f2SKenneth D. Merry static int mpr_user_read_cfg_page(struct mpr_softc *,
132991554f2SKenneth D. Merry     struct mpr_cfg_page_req *, void *);
133991554f2SKenneth D. Merry static int mpr_user_read_extcfg_header(struct mpr_softc *,
134991554f2SKenneth D. Merry     struct mpr_ext_cfg_page_req *);
135991554f2SKenneth D. Merry static int mpr_user_read_extcfg_page(struct mpr_softc *,
136991554f2SKenneth D. Merry     struct mpr_ext_cfg_page_req *, void *);
137991554f2SKenneth D. Merry static int mpr_user_write_cfg_page(struct mpr_softc *,
138991554f2SKenneth D. Merry     struct mpr_cfg_page_req *, void *);
139991554f2SKenneth D. Merry static int mpr_user_setup_request(struct mpr_command *,
140991554f2SKenneth D. Merry     struct mpr_usr_command *);
141991554f2SKenneth D. Merry static int mpr_user_command(struct mpr_softc *, struct mpr_usr_command *);
142991554f2SKenneth D. Merry 
143991554f2SKenneth D. Merry static int mpr_user_pass_thru(struct mpr_softc *sc, mpr_pass_thru_t *data);
144991554f2SKenneth D. Merry static void mpr_user_get_adapter_data(struct mpr_softc *sc,
145991554f2SKenneth D. Merry     mpr_adapter_data_t *data);
1467a2a6a1aSStephen McConnell static void mpr_user_read_pci_info(struct mpr_softc *sc, mpr_pci_info_t *data);
147991554f2SKenneth D. Merry static uint8_t mpr_get_fw_diag_buffer_number(struct mpr_softc *sc,
148991554f2SKenneth D. Merry     uint32_t unique_id);
149991554f2SKenneth D. Merry static int mpr_post_fw_diag_buffer(struct mpr_softc *sc,
150991554f2SKenneth D. Merry     mpr_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code);
151991554f2SKenneth D. Merry static int mpr_release_fw_diag_buffer(struct mpr_softc *sc,
152991554f2SKenneth D. Merry     mpr_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code,
153991554f2SKenneth D. Merry     uint32_t diag_type);
154991554f2SKenneth D. Merry static int mpr_diag_register(struct mpr_softc *sc,
155991554f2SKenneth D. Merry     mpr_fw_diag_register_t *diag_register, uint32_t *return_code);
156991554f2SKenneth D. Merry static int mpr_diag_unregister(struct mpr_softc *sc,
157991554f2SKenneth D. Merry     mpr_fw_diag_unregister_t *diag_unregister, uint32_t *return_code);
1587a2a6a1aSStephen McConnell static int mpr_diag_query(struct mpr_softc *sc, mpr_fw_diag_query_t *diag_query,
1597a2a6a1aSStephen McConnell     uint32_t *return_code);
160991554f2SKenneth D. Merry static int mpr_diag_read_buffer(struct mpr_softc *sc,
161991554f2SKenneth D. Merry     mpr_diag_read_buffer_t *diag_read_buffer, uint8_t *ioctl_buf,
162991554f2SKenneth D. Merry     uint32_t *return_code);
163991554f2SKenneth D. Merry static int mpr_diag_release(struct mpr_softc *sc,
164991554f2SKenneth D. Merry     mpr_fw_diag_release_t *diag_release, uint32_t *return_code);
165991554f2SKenneth D. Merry static int mpr_do_diag_action(struct mpr_softc *sc, uint32_t action,
166991554f2SKenneth D. Merry     uint8_t *diag_action, uint32_t length, uint32_t *return_code);
1677a2a6a1aSStephen McConnell static int mpr_user_diag_action(struct mpr_softc *sc, mpr_diag_action_t *data);
1687a2a6a1aSStephen McConnell static void mpr_user_event_query(struct mpr_softc *sc, mpr_event_query_t *data);
169991554f2SKenneth D. Merry static void mpr_user_event_enable(struct mpr_softc *sc,
170991554f2SKenneth D. Merry     mpr_event_enable_t *data);
171991554f2SKenneth D. Merry static int mpr_user_event_report(struct mpr_softc *sc,
172991554f2SKenneth D. Merry     mpr_event_report_t *data);
173991554f2SKenneth D. Merry static int mpr_user_reg_access(struct mpr_softc *sc, mpr_reg_access_t *data);
174991554f2SKenneth D. Merry static int mpr_user_btdh(struct mpr_softc *sc, mpr_btdh_mapping_t *data);
175991554f2SKenneth D. Merry 
176991554f2SKenneth D. Merry static MALLOC_DEFINE(M_MPRUSER, "mpr_user", "Buffers for mpr(4) ioctls");
177991554f2SKenneth D. Merry 
178991554f2SKenneth D. Merry /*
179991554f2SKenneth D. Merry  * MPI functions that support IEEE SGLs for SAS3.
180991554f2SKenneth D. Merry  */
181991554f2SKenneth D. Merry static uint8_t ieee_sgl_func_list[] = {
182991554f2SKenneth D. Merry 	MPI2_FUNCTION_SCSI_IO_REQUEST,
183991554f2SKenneth D. Merry 	MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH,
184991554f2SKenneth D. Merry 	MPI2_FUNCTION_SMP_PASSTHROUGH,
185991554f2SKenneth D. Merry 	MPI2_FUNCTION_SATA_PASSTHROUGH,
186991554f2SKenneth D. Merry 	MPI2_FUNCTION_FW_UPLOAD,
187991554f2SKenneth D. Merry 	MPI2_FUNCTION_FW_DOWNLOAD,
188991554f2SKenneth D. Merry 	MPI2_FUNCTION_TARGET_ASSIST,
189991554f2SKenneth D. Merry 	MPI2_FUNCTION_TARGET_STATUS_SEND,
190991554f2SKenneth D. Merry 	MPI2_FUNCTION_TOOLBOX
191991554f2SKenneth D. Merry };
192991554f2SKenneth D. Merry 
193991554f2SKenneth D. Merry int
mpr_attach_user(struct mpr_softc * sc)194991554f2SKenneth D. Merry mpr_attach_user(struct mpr_softc *sc)
195991554f2SKenneth D. Merry {
196991554f2SKenneth D. Merry 	int unit;
197991554f2SKenneth D. Merry 
198991554f2SKenneth D. Merry 	unit = device_get_unit(sc->mpr_dev);
1997a2a6a1aSStephen McConnell 	sc->mpr_cdev = make_dev(&mpr_cdevsw, unit, UID_ROOT, GID_OPERATOR, 0640,
2007a2a6a1aSStephen McConnell 	    "mpr%d", unit);
2017a2a6a1aSStephen McConnell 
2027a2a6a1aSStephen McConnell 	if (sc->mpr_cdev == NULL)
203991554f2SKenneth D. Merry 		return (ENOMEM);
2047a2a6a1aSStephen McConnell 
205991554f2SKenneth D. Merry 	sc->mpr_cdev->si_drv1 = sc;
206991554f2SKenneth D. Merry 	return (0);
207991554f2SKenneth D. Merry }
208991554f2SKenneth D. Merry 
209991554f2SKenneth D. Merry void
mpr_detach_user(struct mpr_softc * sc)210991554f2SKenneth D. Merry mpr_detach_user(struct mpr_softc *sc)
211991554f2SKenneth D. Merry {
212991554f2SKenneth D. Merry 
213991554f2SKenneth D. Merry 	/* XXX: do a purge of pending requests? */
214991554f2SKenneth D. Merry 	if (sc->mpr_cdev != NULL)
215991554f2SKenneth D. Merry 		destroy_dev(sc->mpr_cdev);
216991554f2SKenneth D. Merry }
217991554f2SKenneth D. Merry 
218991554f2SKenneth D. Merry static int
mpr_open(struct cdev * dev,int flags,int fmt,struct thread * td)219991554f2SKenneth D. Merry mpr_open(struct cdev *dev, int flags, int fmt, struct thread *td)
220991554f2SKenneth D. Merry {
221991554f2SKenneth D. Merry 
222991554f2SKenneth D. Merry 	return (0);
223991554f2SKenneth D. Merry }
224991554f2SKenneth D. Merry 
225991554f2SKenneth D. Merry static int
mpr_close(struct cdev * dev,int flags,int fmt,struct thread * td)226991554f2SKenneth D. Merry mpr_close(struct cdev *dev, int flags, int fmt, struct thread *td)
227991554f2SKenneth D. Merry {
228991554f2SKenneth D. Merry 
229991554f2SKenneth D. Merry 	return (0);
230991554f2SKenneth D. Merry }
231991554f2SKenneth D. Merry 
232991554f2SKenneth D. Merry static int
mpr_user_read_cfg_header(struct mpr_softc * sc,struct mpr_cfg_page_req * page_req)233991554f2SKenneth D. Merry mpr_user_read_cfg_header(struct mpr_softc *sc,
234991554f2SKenneth D. Merry     struct mpr_cfg_page_req *page_req)
235991554f2SKenneth D. Merry {
236991554f2SKenneth D. Merry 	MPI2_CONFIG_PAGE_HEADER *hdr;
237991554f2SKenneth D. Merry 	struct mpr_config_params params;
238991554f2SKenneth D. Merry 	int	    error;
239991554f2SKenneth D. Merry 
240991554f2SKenneth D. Merry 	hdr = &params.hdr.Struct;
241991554f2SKenneth D. Merry 	params.action = MPI2_CONFIG_ACTION_PAGE_HEADER;
242991554f2SKenneth D. Merry 	params.page_address = le32toh(page_req->page_address);
243991554f2SKenneth D. Merry 	hdr->PageVersion = 0;
244991554f2SKenneth D. Merry 	hdr->PageLength = 0;
245991554f2SKenneth D. Merry 	hdr->PageNumber = page_req->header.PageNumber;
246991554f2SKenneth D. Merry 	hdr->PageType = page_req->header.PageType;
247991554f2SKenneth D. Merry 	params.buffer = NULL;
248991554f2SKenneth D. Merry 	params.length = 0;
249991554f2SKenneth D. Merry 	params.callback = NULL;
250991554f2SKenneth D. Merry 
251991554f2SKenneth D. Merry 	if ((error = mpr_read_config_page(sc, &params)) != 0) {
252991554f2SKenneth D. Merry 		/*
253991554f2SKenneth D. Merry 		 * Leave the request. Without resetting the chip, it's
254991554f2SKenneth D. Merry 		 * still owned by it and we'll just get into trouble
255991554f2SKenneth D. Merry 		 * freeing it now. Mark it as abandoned so that if it
256991554f2SKenneth D. Merry 		 * shows up later it can be freed.
257991554f2SKenneth D. Merry 		 */
258991554f2SKenneth D. Merry 		mpr_printf(sc, "read_cfg_header timed out\n");
259991554f2SKenneth D. Merry 		return (ETIMEDOUT);
260991554f2SKenneth D. Merry 	}
261991554f2SKenneth D. Merry 
262991554f2SKenneth D. Merry 	page_req->ioc_status = htole16(params.status);
263991554f2SKenneth D. Merry 	if ((page_req->ioc_status & MPI2_IOCSTATUS_MASK) ==
264991554f2SKenneth D. Merry 	    MPI2_IOCSTATUS_SUCCESS) {
265991554f2SKenneth D. Merry 		bcopy(hdr, &page_req->header, sizeof(page_req->header));
266991554f2SKenneth D. Merry 	}
267991554f2SKenneth D. Merry 
268991554f2SKenneth D. Merry 	return (0);
269991554f2SKenneth D. Merry }
270991554f2SKenneth D. Merry 
271991554f2SKenneth D. Merry static int
mpr_user_read_cfg_page(struct mpr_softc * sc,struct mpr_cfg_page_req * page_req,void * buf)2727a2a6a1aSStephen McConnell mpr_user_read_cfg_page(struct mpr_softc *sc, struct mpr_cfg_page_req *page_req,
2737a2a6a1aSStephen McConnell     void *buf)
274991554f2SKenneth D. Merry {
275991554f2SKenneth D. Merry 	MPI2_CONFIG_PAGE_HEADER *reqhdr, *hdr;
276991554f2SKenneth D. Merry 	struct mpr_config_params params;
277991554f2SKenneth D. Merry 	int	      error;
278991554f2SKenneth D. Merry 
279991554f2SKenneth D. Merry 	reqhdr = buf;
280991554f2SKenneth D. Merry 	hdr = &params.hdr.Struct;
281991554f2SKenneth D. Merry 	hdr->PageVersion = reqhdr->PageVersion;
282991554f2SKenneth D. Merry 	hdr->PageLength = reqhdr->PageLength;
283991554f2SKenneth D. Merry 	hdr->PageNumber = reqhdr->PageNumber;
284991554f2SKenneth D. Merry 	hdr->PageType = reqhdr->PageType & MPI2_CONFIG_PAGETYPE_MASK;
285991554f2SKenneth D. Merry 	params.action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
286991554f2SKenneth D. Merry 	params.page_address = le32toh(page_req->page_address);
287991554f2SKenneth D. Merry 	params.buffer = buf;
288991554f2SKenneth D. Merry 	params.length = le32toh(page_req->len);
289991554f2SKenneth D. Merry 	params.callback = NULL;
290991554f2SKenneth D. Merry 
291991554f2SKenneth D. Merry 	if ((error = mpr_read_config_page(sc, &params)) != 0) {
292991554f2SKenneth D. Merry 		mpr_printf(sc, "mpr_user_read_cfg_page timed out\n");
293991554f2SKenneth D. Merry 		return (ETIMEDOUT);
294991554f2SKenneth D. Merry 	}
295991554f2SKenneth D. Merry 
296991554f2SKenneth D. Merry 	page_req->ioc_status = htole16(params.status);
297991554f2SKenneth D. Merry 	return (0);
298991554f2SKenneth D. Merry }
299991554f2SKenneth D. Merry 
300991554f2SKenneth D. Merry static int
mpr_user_read_extcfg_header(struct mpr_softc * sc,struct mpr_ext_cfg_page_req * ext_page_req)301991554f2SKenneth D. Merry mpr_user_read_extcfg_header(struct mpr_softc *sc,
302991554f2SKenneth D. Merry     struct mpr_ext_cfg_page_req *ext_page_req)
303991554f2SKenneth D. Merry {
304991554f2SKenneth D. Merry 	MPI2_CONFIG_EXTENDED_PAGE_HEADER *hdr;
305991554f2SKenneth D. Merry 	struct mpr_config_params params;
306991554f2SKenneth D. Merry 	int	    error;
307991554f2SKenneth D. Merry 
308991554f2SKenneth D. Merry 	hdr = &params.hdr.Ext;
309991554f2SKenneth D. Merry 	params.action = MPI2_CONFIG_ACTION_PAGE_HEADER;
310991554f2SKenneth D. Merry 	hdr->PageVersion = ext_page_req->header.PageVersion;
311991554f2SKenneth D. Merry 	hdr->PageType = MPI2_CONFIG_PAGETYPE_EXTENDED;
312991554f2SKenneth D. Merry 	hdr->ExtPageLength = 0;
313991554f2SKenneth D. Merry 	hdr->PageNumber = ext_page_req->header.PageNumber;
314991554f2SKenneth D. Merry 	hdr->ExtPageType = ext_page_req->header.ExtPageType;
315991554f2SKenneth D. Merry 	params.page_address = le32toh(ext_page_req->page_address);
316a2c14879SStephen McConnell 	params.buffer = NULL;
317a2c14879SStephen McConnell 	params.length = 0;
318a2c14879SStephen McConnell 	params.callback = NULL;
319a2c14879SStephen McConnell 
320991554f2SKenneth D. Merry 	if ((error = mpr_read_config_page(sc, &params)) != 0) {
321991554f2SKenneth D. Merry 		/*
322991554f2SKenneth D. Merry 		 * Leave the request. Without resetting the chip, it's
323991554f2SKenneth D. Merry 		 * still owned by it and we'll just get into trouble
324991554f2SKenneth D. Merry 		 * freeing it now. Mark it as abandoned so that if it
325991554f2SKenneth D. Merry 		 * shows up later it can be freed.
326991554f2SKenneth D. Merry 		 */
327991554f2SKenneth D. Merry 		mpr_printf(sc, "mpr_user_read_extcfg_header timed out\n");
328991554f2SKenneth D. Merry 		return (ETIMEDOUT);
329991554f2SKenneth D. Merry 	}
330991554f2SKenneth D. Merry 
331991554f2SKenneth D. Merry 	ext_page_req->ioc_status = htole16(params.status);
332991554f2SKenneth D. Merry 	if ((ext_page_req->ioc_status & MPI2_IOCSTATUS_MASK) ==
333991554f2SKenneth D. Merry 	    MPI2_IOCSTATUS_SUCCESS) {
334991554f2SKenneth D. Merry 		ext_page_req->header.PageVersion = hdr->PageVersion;
335991554f2SKenneth D. Merry 		ext_page_req->header.PageNumber = hdr->PageNumber;
336991554f2SKenneth D. Merry 		ext_page_req->header.PageType = hdr->PageType;
337991554f2SKenneth D. Merry 		ext_page_req->header.ExtPageLength = hdr->ExtPageLength;
338991554f2SKenneth D. Merry 		ext_page_req->header.ExtPageType = hdr->ExtPageType;
339991554f2SKenneth D. Merry 	}
340991554f2SKenneth D. Merry 
341991554f2SKenneth D. Merry 	return (0);
342991554f2SKenneth D. Merry }
343991554f2SKenneth D. Merry 
344991554f2SKenneth D. Merry static int
mpr_user_read_extcfg_page(struct mpr_softc * sc,struct mpr_ext_cfg_page_req * ext_page_req,void * buf)345991554f2SKenneth D. Merry mpr_user_read_extcfg_page(struct mpr_softc *sc,
346991554f2SKenneth D. Merry     struct mpr_ext_cfg_page_req *ext_page_req, void *buf)
347991554f2SKenneth D. Merry {
348991554f2SKenneth D. Merry 	MPI2_CONFIG_EXTENDED_PAGE_HEADER *reqhdr, *hdr;
349991554f2SKenneth D. Merry 	struct mpr_config_params params;
350991554f2SKenneth D. Merry 	int error;
351991554f2SKenneth D. Merry 
352991554f2SKenneth D. Merry 	reqhdr = buf;
353991554f2SKenneth D. Merry 	hdr = &params.hdr.Ext;
354991554f2SKenneth D. Merry 	params.action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
355991554f2SKenneth D. Merry 	params.page_address = le32toh(ext_page_req->page_address);
356991554f2SKenneth D. Merry 	hdr->PageVersion = reqhdr->PageVersion;
357c91d307fSStephen McConnell 	hdr->PageType = MPI2_CONFIG_PAGETYPE_EXTENDED;
358a2c14879SStephen McConnell 	hdr->PageNumber = reqhdr->PageNumber;
359991554f2SKenneth D. Merry 	hdr->ExtPageType = reqhdr->ExtPageType;
360991554f2SKenneth D. Merry 	hdr->ExtPageLength = reqhdr->ExtPageLength;
361991554f2SKenneth D. Merry 	params.buffer = buf;
362991554f2SKenneth D. Merry 	params.length = le32toh(ext_page_req->len);
363991554f2SKenneth D. Merry 	params.callback = NULL;
364991554f2SKenneth D. Merry 
365991554f2SKenneth D. Merry 	if ((error = mpr_read_config_page(sc, &params)) != 0) {
366991554f2SKenneth D. Merry 		mpr_printf(sc, "mpr_user_read_extcfg_page timed out\n");
367991554f2SKenneth D. Merry 		return (ETIMEDOUT);
368991554f2SKenneth D. Merry 	}
369991554f2SKenneth D. Merry 
370991554f2SKenneth D. Merry 	ext_page_req->ioc_status = htole16(params.status);
371991554f2SKenneth D. Merry 	return (0);
372991554f2SKenneth D. Merry }
373991554f2SKenneth D. Merry 
374991554f2SKenneth D. Merry static int
mpr_user_write_cfg_page(struct mpr_softc * sc,struct mpr_cfg_page_req * page_req,void * buf)375991554f2SKenneth D. Merry mpr_user_write_cfg_page(struct mpr_softc *sc,
376991554f2SKenneth D. Merry     struct mpr_cfg_page_req *page_req, void *buf)
377991554f2SKenneth D. Merry {
378991554f2SKenneth D. Merry 	MPI2_CONFIG_PAGE_HEADER *reqhdr, *hdr;
379991554f2SKenneth D. Merry 	struct mpr_config_params params;
380991554f2SKenneth D. Merry 	u_int	      hdr_attr;
381991554f2SKenneth D. Merry 	int	      error;
382991554f2SKenneth D. Merry 
383991554f2SKenneth D. Merry 	reqhdr = buf;
384991554f2SKenneth D. Merry 	hdr = &params.hdr.Struct;
385991554f2SKenneth D. Merry 	hdr_attr = reqhdr->PageType & MPI2_CONFIG_PAGEATTR_MASK;
386991554f2SKenneth D. Merry 	if (hdr_attr != MPI2_CONFIG_PAGEATTR_CHANGEABLE &&
387991554f2SKenneth D. Merry 	    hdr_attr != MPI2_CONFIG_PAGEATTR_PERSISTENT) {
388991554f2SKenneth D. Merry 		mpr_printf(sc, "page type 0x%x not changeable\n",
389991554f2SKenneth D. Merry 			reqhdr->PageType & MPI2_CONFIG_PAGETYPE_MASK);
390991554f2SKenneth D. Merry 		return (EINVAL);
391991554f2SKenneth D. Merry 	}
392991554f2SKenneth D. Merry 
393991554f2SKenneth D. Merry 	/*
394991554f2SKenneth D. Merry 	 * There isn't any point in restoring stripped out attributes
395991554f2SKenneth D. Merry 	 * if you then mask them going down to issue the request.
396991554f2SKenneth D. Merry 	 */
397991554f2SKenneth D. Merry 
398991554f2SKenneth D. Merry 	hdr->PageVersion = reqhdr->PageVersion;
399991554f2SKenneth D. Merry 	hdr->PageLength = reqhdr->PageLength;
400991554f2SKenneth D. Merry 	hdr->PageNumber = reqhdr->PageNumber;
401991554f2SKenneth D. Merry 	hdr->PageType = reqhdr->PageType;
402991554f2SKenneth D. Merry 	params.action = MPI2_CONFIG_ACTION_PAGE_WRITE_CURRENT;
403991554f2SKenneth D. Merry 	params.page_address = le32toh(page_req->page_address);
404991554f2SKenneth D. Merry 	params.buffer = buf;
405991554f2SKenneth D. Merry 	params.length = le32toh(page_req->len);
406991554f2SKenneth D. Merry 	params.callback = NULL;
407991554f2SKenneth D. Merry 
408991554f2SKenneth D. Merry 	if ((error = mpr_write_config_page(sc, &params)) != 0) {
409991554f2SKenneth D. Merry 		mpr_printf(sc, "mpr_write_cfg_page timed out\n");
410991554f2SKenneth D. Merry 		return (ETIMEDOUT);
411991554f2SKenneth D. Merry 	}
412991554f2SKenneth D. Merry 
413991554f2SKenneth D. Merry 	page_req->ioc_status = htole16(params.status);
414991554f2SKenneth D. Merry 	return (0);
415991554f2SKenneth D. Merry }
416991554f2SKenneth D. Merry 
417991554f2SKenneth D. Merry void
mpr_init_sge(struct mpr_command * cm,void * req,void * sge)418991554f2SKenneth D. Merry mpr_init_sge(struct mpr_command *cm, void *req, void *sge)
419991554f2SKenneth D. Merry {
420991554f2SKenneth D. Merry 	int off, space;
421991554f2SKenneth D. Merry 
42296410703SScott Long 	space = (int)cm->cm_sc->reqframesz;
423991554f2SKenneth D. Merry 	off = (uintptr_t)sge - (uintptr_t)req;
424991554f2SKenneth D. Merry 
425991554f2SKenneth D. Merry 	KASSERT(off < space, ("bad pointers %p %p, off %d, space %d",
426991554f2SKenneth D. Merry             req, sge, off, space));
427991554f2SKenneth D. Merry 
428991554f2SKenneth D. Merry 	cm->cm_sge = sge;
429991554f2SKenneth D. Merry 	cm->cm_sglsize = space - off;
430991554f2SKenneth D. Merry }
431991554f2SKenneth D. Merry 
432991554f2SKenneth D. Merry /*
433991554f2SKenneth D. Merry  * Prepare the mpr_command for an IOC_FACTS request.
434991554f2SKenneth D. Merry  */
435991554f2SKenneth D. Merry static int
mpi_pre_ioc_facts(struct mpr_command * cm,struct mpr_usr_command * cmd)436991554f2SKenneth D. Merry mpi_pre_ioc_facts(struct mpr_command *cm, struct mpr_usr_command *cmd)
437991554f2SKenneth D. Merry {
438991554f2SKenneth D. Merry 	MPI2_IOC_FACTS_REQUEST *req = (void *)cm->cm_req;
439991554f2SKenneth D. Merry 	MPI2_IOC_FACTS_REPLY *rpl;
440991554f2SKenneth D. Merry 
441991554f2SKenneth D. Merry 	if (cmd->req_len != sizeof *req)
442991554f2SKenneth D. Merry 		return (EINVAL);
443991554f2SKenneth D. Merry 	if (cmd->rpl_len != sizeof *rpl)
444991554f2SKenneth D. Merry 		return (EINVAL);
445991554f2SKenneth D. Merry 
446991554f2SKenneth D. Merry 	cm->cm_sge = NULL;
447991554f2SKenneth D. Merry 	cm->cm_sglsize = 0;
448991554f2SKenneth D. Merry 	return (0);
449991554f2SKenneth D. Merry }
450991554f2SKenneth D. Merry 
451991554f2SKenneth D. Merry /*
452991554f2SKenneth D. Merry  * Prepare the mpr_command for a PORT_FACTS request.
453991554f2SKenneth D. Merry  */
454991554f2SKenneth D. Merry static int
mpi_pre_port_facts(struct mpr_command * cm,struct mpr_usr_command * cmd)455991554f2SKenneth D. Merry mpi_pre_port_facts(struct mpr_command *cm, struct mpr_usr_command *cmd)
456991554f2SKenneth D. Merry {
457991554f2SKenneth D. Merry 	MPI2_PORT_FACTS_REQUEST *req = (void *)cm->cm_req;
458991554f2SKenneth D. Merry 	MPI2_PORT_FACTS_REPLY *rpl;
459991554f2SKenneth D. Merry 
460991554f2SKenneth D. Merry 	if (cmd->req_len != sizeof *req)
461991554f2SKenneth D. Merry 		return (EINVAL);
462991554f2SKenneth D. Merry 	if (cmd->rpl_len != sizeof *rpl)
463991554f2SKenneth D. Merry 		return (EINVAL);
464991554f2SKenneth D. Merry 
465991554f2SKenneth D. Merry 	cm->cm_sge = NULL;
466991554f2SKenneth D. Merry 	cm->cm_sglsize = 0;
467991554f2SKenneth D. Merry 	return (0);
468991554f2SKenneth D. Merry }
469991554f2SKenneth D. Merry 
470991554f2SKenneth D. Merry /*
471991554f2SKenneth D. Merry  * Prepare the mpr_command for a FW_DOWNLOAD request.
472991554f2SKenneth D. Merry  */
473991554f2SKenneth D. Merry static int
mpi_pre_fw_download(struct mpr_command * cm,struct mpr_usr_command * cmd)474991554f2SKenneth D. Merry mpi_pre_fw_download(struct mpr_command *cm, struct mpr_usr_command *cmd)
475991554f2SKenneth D. Merry {
476991554f2SKenneth D. Merry 	MPI25_FW_DOWNLOAD_REQUEST *req = (void *)cm->cm_req;
477991554f2SKenneth D. Merry 	MPI2_FW_DOWNLOAD_REPLY *rpl;
478991554f2SKenneth D. Merry 	int error;
479991554f2SKenneth D. Merry 
480991554f2SKenneth D. Merry 	if (cmd->req_len != sizeof *req)
481991554f2SKenneth D. Merry 		return (EINVAL);
482991554f2SKenneth D. Merry 	if (cmd->rpl_len != sizeof *rpl)
483991554f2SKenneth D. Merry 		return (EINVAL);
484991554f2SKenneth D. Merry 
485991554f2SKenneth D. Merry 	if (cmd->len == 0)
486991554f2SKenneth D. Merry 		return (EINVAL);
487991554f2SKenneth D. Merry 
488991554f2SKenneth D. Merry 	error = copyin(cmd->buf, cm->cm_data, cmd->len);
489991554f2SKenneth D. Merry 	if (error != 0)
490991554f2SKenneth D. Merry 		return (error);
491991554f2SKenneth D. Merry 
492991554f2SKenneth D. Merry 	mpr_init_sge(cm, req, &req->SGL);
493991554f2SKenneth D. Merry 
494991554f2SKenneth D. Merry 	/*
495991554f2SKenneth D. Merry 	 * For now, the F/W image must be provided in a single request.
496991554f2SKenneth D. Merry 	 */
497991554f2SKenneth D. Merry 	if ((req->MsgFlags & MPI2_FW_DOWNLOAD_MSGFLGS_LAST_SEGMENT) == 0)
498991554f2SKenneth D. Merry 		return (EINVAL);
499991554f2SKenneth D. Merry 	if (req->TotalImageSize != cmd->len)
500991554f2SKenneth D. Merry 		return (EINVAL);
501991554f2SKenneth D. Merry 
502991554f2SKenneth D. Merry 	req->ImageOffset = 0;
503991554f2SKenneth D. Merry 	req->ImageSize = cmd->len;
504991554f2SKenneth D. Merry 
505991554f2SKenneth D. Merry 	cm->cm_flags |= MPR_CM_FLAGS_DATAOUT;
506991554f2SKenneth D. Merry 
507991554f2SKenneth D. Merry 	return (mpr_push_ieee_sge(cm, &req->SGL, 0));
508991554f2SKenneth D. Merry }
509991554f2SKenneth D. Merry 
510991554f2SKenneth D. Merry /*
511991554f2SKenneth D. Merry  * Prepare the mpr_command for a FW_UPLOAD request.
512991554f2SKenneth D. Merry  */
513991554f2SKenneth D. Merry static int
mpi_pre_fw_upload(struct mpr_command * cm,struct mpr_usr_command * cmd)514991554f2SKenneth D. Merry mpi_pre_fw_upload(struct mpr_command *cm, struct mpr_usr_command *cmd)
515991554f2SKenneth D. Merry {
516991554f2SKenneth D. Merry 	MPI25_FW_UPLOAD_REQUEST *req = (void *)cm->cm_req;
517991554f2SKenneth D. Merry 	MPI2_FW_UPLOAD_REPLY *rpl;
518991554f2SKenneth D. Merry 
519991554f2SKenneth D. Merry 	if (cmd->req_len != sizeof *req)
520991554f2SKenneth D. Merry 		return (EINVAL);
521991554f2SKenneth D. Merry 	if (cmd->rpl_len != sizeof *rpl)
522991554f2SKenneth D. Merry 		return (EINVAL);
523991554f2SKenneth D. Merry 
524991554f2SKenneth D. Merry 	mpr_init_sge(cm, req, &req->SGL);
525991554f2SKenneth D. Merry 	if (cmd->len == 0) {
526991554f2SKenneth D. Merry 		/* Perhaps just asking what the size of the fw is? */
527991554f2SKenneth D. Merry 		return (0);
528991554f2SKenneth D. Merry 	}
529991554f2SKenneth D. Merry 
530991554f2SKenneth D. Merry 	req->ImageOffset = 0;
531991554f2SKenneth D. Merry 	req->ImageSize = cmd->len;
532991554f2SKenneth D. Merry 
533a2c14879SStephen McConnell 	cm->cm_flags |= MPR_CM_FLAGS_DATAIN;
534a2c14879SStephen McConnell 
535991554f2SKenneth D. Merry 	return (mpr_push_ieee_sge(cm, &req->SGL, 0));
536991554f2SKenneth D. Merry }
537991554f2SKenneth D. Merry 
538991554f2SKenneth D. Merry /*
539991554f2SKenneth D. Merry  * Prepare the mpr_command for a SATA_PASSTHROUGH request.
540991554f2SKenneth D. Merry  */
541991554f2SKenneth D. Merry static int
mpi_pre_sata_passthrough(struct mpr_command * cm,struct mpr_usr_command * cmd)542991554f2SKenneth D. Merry mpi_pre_sata_passthrough(struct mpr_command *cm, struct mpr_usr_command *cmd)
543991554f2SKenneth D. Merry {
544991554f2SKenneth D. Merry 	MPI2_SATA_PASSTHROUGH_REQUEST *req = (void *)cm->cm_req;
545991554f2SKenneth D. Merry 	MPI2_SATA_PASSTHROUGH_REPLY *rpl;
546991554f2SKenneth D. Merry 
547991554f2SKenneth D. Merry 	if (cmd->req_len != sizeof *req)
548991554f2SKenneth D. Merry 		return (EINVAL);
549991554f2SKenneth D. Merry 	if (cmd->rpl_len != sizeof *rpl)
550991554f2SKenneth D. Merry 		return (EINVAL);
551991554f2SKenneth D. Merry 
552991554f2SKenneth D. Merry 	mpr_init_sge(cm, req, &req->SGL);
553991554f2SKenneth D. Merry 	return (0);
554991554f2SKenneth D. Merry }
555991554f2SKenneth D. Merry 
556991554f2SKenneth D. Merry /*
557991554f2SKenneth D. Merry  * Prepare the mpr_command for a SMP_PASSTHROUGH request.
558991554f2SKenneth D. Merry  */
559991554f2SKenneth D. Merry static int
mpi_pre_smp_passthrough(struct mpr_command * cm,struct mpr_usr_command * cmd)560991554f2SKenneth D. Merry mpi_pre_smp_passthrough(struct mpr_command *cm, struct mpr_usr_command *cmd)
561991554f2SKenneth D. Merry {
562991554f2SKenneth D. Merry 	MPI2_SMP_PASSTHROUGH_REQUEST *req = (void *)cm->cm_req;
563991554f2SKenneth D. Merry 	MPI2_SMP_PASSTHROUGH_REPLY *rpl;
564991554f2SKenneth D. Merry 
565991554f2SKenneth D. Merry 	if (cmd->req_len != sizeof *req)
566991554f2SKenneth D. Merry 		return (EINVAL);
567991554f2SKenneth D. Merry 	if (cmd->rpl_len != sizeof *rpl)
568991554f2SKenneth D. Merry 		return (EINVAL);
569991554f2SKenneth D. Merry 
570991554f2SKenneth D. Merry 	mpr_init_sge(cm, req, &req->SGL);
571991554f2SKenneth D. Merry 	return (0);
572991554f2SKenneth D. Merry }
573991554f2SKenneth D. Merry 
574991554f2SKenneth D. Merry /*
575991554f2SKenneth D. Merry  * Prepare the mpr_command for a CONFIG request.
576991554f2SKenneth D. Merry  */
577991554f2SKenneth D. Merry static int
mpi_pre_config(struct mpr_command * cm,struct mpr_usr_command * cmd)578991554f2SKenneth D. Merry mpi_pre_config(struct mpr_command *cm, struct mpr_usr_command *cmd)
579991554f2SKenneth D. Merry {
580991554f2SKenneth D. Merry 	MPI2_CONFIG_REQUEST *req = (void *)cm->cm_req;
581991554f2SKenneth D. Merry 	MPI2_CONFIG_REPLY *rpl;
582991554f2SKenneth D. Merry 
583991554f2SKenneth D. Merry 	if (cmd->req_len != sizeof *req)
584991554f2SKenneth D. Merry 		return (EINVAL);
585991554f2SKenneth D. Merry 	if (cmd->rpl_len != sizeof *rpl)
586991554f2SKenneth D. Merry 		return (EINVAL);
587991554f2SKenneth D. Merry 
588991554f2SKenneth D. Merry 	mpr_init_sge(cm, req, &req->PageBufferSGE);
589991554f2SKenneth D. Merry 	return (0);
590991554f2SKenneth D. Merry }
591991554f2SKenneth D. Merry 
592991554f2SKenneth D. Merry /*
593991554f2SKenneth D. Merry  * Prepare the mpr_command for a SAS_IO_UNIT_CONTROL request.
594991554f2SKenneth D. Merry  */
595991554f2SKenneth D. Merry static int
mpi_pre_sas_io_unit_control(struct mpr_command * cm,struct mpr_usr_command * cmd)596991554f2SKenneth D. Merry mpi_pre_sas_io_unit_control(struct mpr_command *cm,
597991554f2SKenneth D. Merry 			     struct mpr_usr_command *cmd)
598991554f2SKenneth D. Merry {
599991554f2SKenneth D. Merry 
600991554f2SKenneth D. Merry 	cm->cm_sge = NULL;
601991554f2SKenneth D. Merry 	cm->cm_sglsize = 0;
602991554f2SKenneth D. Merry 	return (0);
603991554f2SKenneth D. Merry }
604991554f2SKenneth D. Merry 
605991554f2SKenneth D. Merry /*
606991554f2SKenneth D. Merry  * A set of functions to prepare an mpr_command for the various
607991554f2SKenneth D. Merry  * supported requests.
608991554f2SKenneth D. Merry  */
609991554f2SKenneth D. Merry struct mpr_user_func {
610991554f2SKenneth D. Merry 	U8		Function;
611991554f2SKenneth D. Merry 	mpr_user_f	*f_pre;
612991554f2SKenneth D. Merry } mpr_user_func_list[] = {
613991554f2SKenneth D. Merry 	{ MPI2_FUNCTION_IOC_FACTS,		mpi_pre_ioc_facts },
614991554f2SKenneth D. Merry 	{ MPI2_FUNCTION_PORT_FACTS,		mpi_pre_port_facts },
615991554f2SKenneth D. Merry 	{ MPI2_FUNCTION_FW_DOWNLOAD, 		mpi_pre_fw_download },
616991554f2SKenneth D. Merry 	{ MPI2_FUNCTION_FW_UPLOAD,		mpi_pre_fw_upload },
617991554f2SKenneth D. Merry 	{ MPI2_FUNCTION_SATA_PASSTHROUGH,	mpi_pre_sata_passthrough },
618991554f2SKenneth D. Merry 	{ MPI2_FUNCTION_SMP_PASSTHROUGH,	mpi_pre_smp_passthrough},
619991554f2SKenneth D. Merry 	{ MPI2_FUNCTION_CONFIG,			mpi_pre_config},
620991554f2SKenneth D. Merry 	{ MPI2_FUNCTION_SAS_IO_UNIT_CONTROL,	mpi_pre_sas_io_unit_control },
621991554f2SKenneth D. Merry 	{ 0xFF,					NULL } /* list end */
622991554f2SKenneth D. Merry };
623991554f2SKenneth D. Merry 
624991554f2SKenneth D. Merry static int
mpr_user_setup_request(struct mpr_command * cm,struct mpr_usr_command * cmd)625991554f2SKenneth D. Merry mpr_user_setup_request(struct mpr_command *cm, struct mpr_usr_command *cmd)
626991554f2SKenneth D. Merry {
627991554f2SKenneth D. Merry 	MPI2_REQUEST_HEADER *hdr = (MPI2_REQUEST_HEADER *)cm->cm_req;
628991554f2SKenneth D. Merry 	struct mpr_user_func *f;
629991554f2SKenneth D. Merry 
630991554f2SKenneth D. Merry 	for (f = mpr_user_func_list; f->f_pre != NULL; f++) {
631991554f2SKenneth D. Merry 		if (hdr->Function == f->Function)
632991554f2SKenneth D. Merry 			return (f->f_pre(cm, cmd));
633991554f2SKenneth D. Merry 	}
634991554f2SKenneth D. Merry 	return (EINVAL);
635991554f2SKenneth D. Merry }
636991554f2SKenneth D. Merry 
637991554f2SKenneth D. Merry static int
mpr_user_command(struct mpr_softc * sc,struct mpr_usr_command * cmd)638991554f2SKenneth D. Merry mpr_user_command(struct mpr_softc *sc, struct mpr_usr_command *cmd)
639991554f2SKenneth D. Merry {
640991554f2SKenneth D. Merry 	MPI2_REQUEST_HEADER *hdr;
6416d4ffcb4SKenneth D. Merry 	MPI2_DEFAULT_REPLY *rpl = NULL;
642991554f2SKenneth D. Merry 	void *buf = NULL;
643991554f2SKenneth D. Merry 	struct mpr_command *cm = NULL;
644991554f2SKenneth D. Merry 	int err = 0;
645991554f2SKenneth D. Merry 	int sz;
646991554f2SKenneth D. Merry 
647991554f2SKenneth D. Merry 	mpr_lock(sc);
648991554f2SKenneth D. Merry 	cm = mpr_alloc_command(sc);
649991554f2SKenneth D. Merry 
650991554f2SKenneth D. Merry 	if (cm == NULL) {
651991554f2SKenneth D. Merry 		mpr_printf(sc, "%s: no mpr requests\n", __func__);
652991554f2SKenneth D. Merry 		err = ENOMEM;
6536d4ffcb4SKenneth D. Merry 		goto RetFree;
654991554f2SKenneth D. Merry 	}
655991554f2SKenneth D. Merry 	mpr_unlock(sc);
656991554f2SKenneth D. Merry 
657991554f2SKenneth D. Merry 	hdr = (MPI2_REQUEST_HEADER *)cm->cm_req;
658991554f2SKenneth D. Merry 
659991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_USER, "%s: req %p %d  rpl %p %d\n", __func__,
660991554f2SKenneth D. Merry 	    cmd->req, cmd->req_len, cmd->rpl, cmd->rpl_len);
661991554f2SKenneth D. Merry 
66296410703SScott Long 	if (cmd->req_len > (int)sc->reqframesz) {
663991554f2SKenneth D. Merry 		err = EINVAL;
664991554f2SKenneth D. Merry 		goto RetFreeUnlocked;
665991554f2SKenneth D. Merry 	}
666991554f2SKenneth D. Merry 	err = copyin(cmd->req, hdr, cmd->req_len);
667991554f2SKenneth D. Merry 	if (err != 0)
668991554f2SKenneth D. Merry 		goto RetFreeUnlocked;
669991554f2SKenneth D. Merry 
670991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_USER, "%s: Function %02X MsgFlags %02X\n", __func__,
671991554f2SKenneth D. Merry 	    hdr->Function, hdr->MsgFlags);
672991554f2SKenneth D. Merry 
673991554f2SKenneth D. Merry 	if (cmd->len > 0) {
674991554f2SKenneth D. Merry 		buf = malloc(cmd->len, M_MPRUSER, M_WAITOK|M_ZERO);
675991554f2SKenneth D. Merry 		cm->cm_data = buf;
676991554f2SKenneth D. Merry 		cm->cm_length = cmd->len;
677991554f2SKenneth D. Merry 	} else {
678991554f2SKenneth D. Merry 		cm->cm_data = NULL;
679991554f2SKenneth D. Merry 		cm->cm_length = 0;
680991554f2SKenneth D. Merry 	}
681991554f2SKenneth D. Merry 
682991554f2SKenneth D. Merry 	cm->cm_flags = MPR_CM_FLAGS_SGE_SIMPLE;
683991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
684991554f2SKenneth D. Merry 
685991554f2SKenneth D. Merry 	err = mpr_user_setup_request(cm, cmd);
686991554f2SKenneth D. Merry 	if (err == EINVAL) {
687991554f2SKenneth D. Merry 		mpr_printf(sc, "%s: unsupported parameter or unsupported "
688991554f2SKenneth D. Merry 		    "function in request (function = 0x%X)\n", __func__,
689991554f2SKenneth D. Merry 		    hdr->Function);
690991554f2SKenneth D. Merry 	}
691991554f2SKenneth D. Merry 	if (err != 0)
692991554f2SKenneth D. Merry 		goto RetFreeUnlocked;
693991554f2SKenneth D. Merry 
694991554f2SKenneth D. Merry 	mpr_lock(sc);
6956d4ffcb4SKenneth D. Merry 	err = mpr_wait_command(sc, &cm, 30, CAN_SLEEP);
696991554f2SKenneth D. Merry 
6976d4ffcb4SKenneth D. Merry 	if (err || (cm == NULL)) {
698991554f2SKenneth D. Merry 		mpr_printf(sc, "%s: invalid request: error %d\n",
699991554f2SKenneth D. Merry 		    __func__, err);
7006d4ffcb4SKenneth D. Merry 		goto RetFree;
701991554f2SKenneth D. Merry 	}
702991554f2SKenneth D. Merry 
7036d4ffcb4SKenneth D. Merry 	if (cm != NULL)
704991554f2SKenneth D. Merry 		rpl = (MPI2_DEFAULT_REPLY *)cm->cm_reply;
705991554f2SKenneth D. Merry 	if (rpl != NULL)
706991554f2SKenneth D. Merry 		sz = rpl->MsgLength * 4;
707991554f2SKenneth D. Merry 	else
708991554f2SKenneth D. Merry 		sz = 0;
709991554f2SKenneth D. Merry 
710991554f2SKenneth D. Merry 	if (sz > cmd->rpl_len) {
711991554f2SKenneth D. Merry 		mpr_printf(sc, "%s: user reply buffer (%d) smaller than "
712991554f2SKenneth D. Merry 		    "returned buffer (%d)\n", __func__, cmd->rpl_len, sz);
713991554f2SKenneth D. Merry 		sz = cmd->rpl_len;
714991554f2SKenneth D. Merry 	}
715991554f2SKenneth D. Merry 
716991554f2SKenneth D. Merry 	mpr_unlock(sc);
717*68cc77a3SMark Johnston 	err = copyout(rpl, cmd->rpl, sz);
718*68cc77a3SMark Johnston 	if (buf != NULL && err == 0)
719*68cc77a3SMark Johnston 		err = copyout(buf, cmd->buf, cmd->len);
720991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_USER, "%s: reply size %d\n", __func__, sz);
721991554f2SKenneth D. Merry 
722991554f2SKenneth D. Merry RetFreeUnlocked:
723991554f2SKenneth D. Merry 	mpr_lock(sc);
7246d4ffcb4SKenneth D. Merry RetFree:
725991554f2SKenneth D. Merry 	if (cm != NULL)
726991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
727991554f2SKenneth D. Merry 	mpr_unlock(sc);
728991554f2SKenneth D. Merry 	if (buf != NULL)
729991554f2SKenneth D. Merry 		free(buf, M_MPRUSER);
730991554f2SKenneth D. Merry 	return (err);
731991554f2SKenneth D. Merry }
732991554f2SKenneth D. Merry 
733991554f2SKenneth D. Merry static int
mpr_user_pass_thru(struct mpr_softc * sc,mpr_pass_thru_t * data)734991554f2SKenneth D. Merry mpr_user_pass_thru(struct mpr_softc *sc, mpr_pass_thru_t *data)
735991554f2SKenneth D. Merry {
736de828a91SMark Johnston 	MPI2_REQUEST_HEADER	*hdr, *tmphdr;
737991554f2SKenneth D. Merry 	MPI2_DEFAULT_REPLY	*rpl;
73867feec50SStephen McConnell 	Mpi26NVMeEncapsulatedErrorReply_t *nvme_error_reply = NULL;
73967feec50SStephen McConnell 	Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
740991554f2SKenneth D. Merry 	struct mpr_command	*cm = NULL;
741de828a91SMark Johnston 	void			*req = NULL;
742991554f2SKenneth D. Merry 	int			i, err = 0, dir = 0, sz;
743991554f2SKenneth D. Merry 	uint8_t			tool, function = 0;
744991554f2SKenneth D. Merry 	u_int			sense_len;
745991554f2SKenneth D. Merry 	struct mprsas_target	*targ = NULL;
746991554f2SKenneth D. Merry 
747991554f2SKenneth D. Merry 	/*
748991554f2SKenneth D. Merry 	 * Only allow one passthru command at a time.  Use the MPR_FLAGS_BUSY
749991554f2SKenneth D. Merry 	 * bit to denote that a passthru is being processed.
750991554f2SKenneth D. Merry 	 */
751991554f2SKenneth D. Merry 	mpr_lock(sc);
752991554f2SKenneth D. Merry 	if (sc->mpr_flags & MPR_FLAGS_BUSY) {
753991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_USER, "%s: Only one passthru command "
754991554f2SKenneth D. Merry 		    "allowed at a single time.", __func__);
755991554f2SKenneth D. Merry 		mpr_unlock(sc);
756991554f2SKenneth D. Merry 		return (EBUSY);
757991554f2SKenneth D. Merry 	}
758991554f2SKenneth D. Merry 	sc->mpr_flags |= MPR_FLAGS_BUSY;
759991554f2SKenneth D. Merry 	mpr_unlock(sc);
760991554f2SKenneth D. Merry 
761991554f2SKenneth D. Merry 	/*
762991554f2SKenneth D. Merry 	 * Do some validation on data direction.  Valid cases are:
763991554f2SKenneth D. Merry 	 *    1) DataSize is 0 and direction is NONE
764991554f2SKenneth D. Merry 	 *    2) DataSize is non-zero and one of:
765991554f2SKenneth D. Merry 	 *        a) direction is READ or
766991554f2SKenneth D. Merry 	 *        b) direction is WRITE or
767991554f2SKenneth D. Merry 	 *        c) direction is BOTH and DataOutSize is non-zero
768991554f2SKenneth D. Merry 	 * If valid and the direction is BOTH, change the direction to READ.
769991554f2SKenneth D. Merry 	 * if valid and the direction is not BOTH, make sure DataOutSize is 0.
770991554f2SKenneth D. Merry 	 */
771991554f2SKenneth D. Merry 	if (((data->DataSize == 0) &&
772991554f2SKenneth D. Merry 	    (data->DataDirection == MPR_PASS_THRU_DIRECTION_NONE)) ||
773991554f2SKenneth D. Merry 	    ((data->DataSize != 0) &&
774991554f2SKenneth D. Merry 	    ((data->DataDirection == MPR_PASS_THRU_DIRECTION_READ) ||
775991554f2SKenneth D. Merry 	    (data->DataDirection == MPR_PASS_THRU_DIRECTION_WRITE) ||
776991554f2SKenneth D. Merry 	    ((data->DataDirection == MPR_PASS_THRU_DIRECTION_BOTH) &&
777991554f2SKenneth D. Merry 	    (data->DataOutSize != 0))))) {
778991554f2SKenneth D. Merry 		if (data->DataDirection == MPR_PASS_THRU_DIRECTION_BOTH)
779991554f2SKenneth D. Merry 			data->DataDirection = MPR_PASS_THRU_DIRECTION_READ;
780991554f2SKenneth D. Merry 		else
781991554f2SKenneth D. Merry 			data->DataOutSize = 0;
7821a15d60dSAlexander Motin 	} else {
7831a15d60dSAlexander Motin 		err = EINVAL;
7841a15d60dSAlexander Motin 		goto RetFreeUnlocked;
7851a15d60dSAlexander Motin 	}
786991554f2SKenneth D. Merry 
787991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_USER, "%s: req 0x%jx %d  rpl 0x%jx %d "
788991554f2SKenneth D. Merry 	    "data in 0x%jx %d data out 0x%jx %d data dir %d\n", __func__,
789991554f2SKenneth D. Merry 	    data->PtrRequest, data->RequestSize, data->PtrReply,
790991554f2SKenneth D. Merry 	    data->ReplySize, data->PtrData, data->DataSize,
791991554f2SKenneth D. Merry 	    data->PtrDataOut, data->DataOutSize, data->DataDirection);
792991554f2SKenneth D. Merry 
793de828a91SMark Johnston 	if (data->RequestSize > sc->reqframesz) {
794991554f2SKenneth D. Merry 		err = EINVAL;
795991554f2SKenneth D. Merry 		goto RetFreeUnlocked;
796991554f2SKenneth D. Merry 	}
797991554f2SKenneth D. Merry 
798de828a91SMark Johnston 	req = malloc(data->RequestSize, M_MPRUSER, M_WAITOK | M_ZERO);
799de828a91SMark Johnston 	tmphdr = (MPI2_REQUEST_HEADER *)req;
800de828a91SMark Johnston 
801de828a91SMark Johnston 	err = copyin(PTRIN(data->PtrRequest), req, data->RequestSize);
802de828a91SMark Johnston 	if (err != 0)
803de828a91SMark Johnston 		goto RetFreeUnlocked;
804de828a91SMark Johnston 
805de828a91SMark Johnston 	function = tmphdr->Function;
806991554f2SKenneth D. Merry 	mpr_dprint(sc, MPR_USER, "%s: Function %02X MsgFlags %02X\n", __func__,
807de828a91SMark Johnston 	    function, tmphdr->MsgFlags);
808991554f2SKenneth D. Merry 
809991554f2SKenneth D. Merry 	/*
810991554f2SKenneth D. Merry 	 * Handle a passthru TM request.
811991554f2SKenneth D. Merry 	 */
812991554f2SKenneth D. Merry 	if (function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
813991554f2SKenneth D. Merry 		MPI2_SCSI_TASK_MANAGE_REQUEST	*task;
814991554f2SKenneth D. Merry 
815991554f2SKenneth D. Merry 		mpr_lock(sc);
816991554f2SKenneth D. Merry 		cm = mprsas_alloc_tm(sc);
817991554f2SKenneth D. Merry 		if (cm == NULL) {
818991554f2SKenneth D. Merry 			err = EINVAL;
819991554f2SKenneth D. Merry 			goto Ret;
820991554f2SKenneth D. Merry 		}
821991554f2SKenneth D. Merry 
822991554f2SKenneth D. Merry 		/* Copy the header in.  Only a small fixup is needed. */
823991554f2SKenneth D. Merry 		task = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
824de828a91SMark Johnston 		memcpy(task, req, data->RequestSize);
825991554f2SKenneth D. Merry 		task->TaskMID = cm->cm_desc.Default.SMID;
826991554f2SKenneth D. Merry 
827991554f2SKenneth D. Merry 		cm->cm_data = NULL;
828991554f2SKenneth D. Merry 		cm->cm_complete = NULL;
829991554f2SKenneth D. Merry 		cm->cm_complete_data = NULL;
830991554f2SKenneth D. Merry 
831a2c14879SStephen McConnell 		targ = mprsas_find_target_by_handle(sc->sassc, 0,
832a2c14879SStephen McConnell 		    task->DevHandle);
833a2c14879SStephen McConnell 		if (targ == NULL) {
834a2c14879SStephen McConnell 			mpr_dprint(sc, MPR_INFO,
835a2c14879SStephen McConnell 			   "%s %d : invalid handle for requested TM 0x%x \n",
836a2c14879SStephen McConnell 			   __func__, __LINE__, task->DevHandle);
837a2c14879SStephen McConnell 			err = 1;
838a2c14879SStephen McConnell 		} else {
839a2c14879SStephen McConnell 			mprsas_prepare_for_tm(sc, cm, targ, CAM_LUN_WILDCARD);
8406d4ffcb4SKenneth D. Merry 			err = mpr_wait_command(sc, &cm, 30, CAN_SLEEP);
841a2c14879SStephen McConnell 		}
842991554f2SKenneth D. Merry 
843991554f2SKenneth D. Merry 		if (err != 0) {
844991554f2SKenneth D. Merry 			err = EIO;
845991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_FAULT, "%s: task management failed",
846991554f2SKenneth D. Merry 			    __func__);
847991554f2SKenneth D. Merry 		}
848991554f2SKenneth D. Merry 		/*
849991554f2SKenneth D. Merry 		 * Copy the reply data and sense data to user space.
850991554f2SKenneth D. Merry 		 */
851*68cc77a3SMark Johnston 		if (err == 0 && cm != NULL && cm->cm_reply != NULL) {
852991554f2SKenneth D. Merry 			rpl = (MPI2_DEFAULT_REPLY *)cm->cm_reply;
853991554f2SKenneth D. Merry 			sz = rpl->MsgLength * 4;
854991554f2SKenneth D. Merry 
8557d154c4dSAlan Somers 			if (bootverbose && sz > data->ReplySize) {
856991554f2SKenneth D. Merry 				mpr_printf(sc, "%s: user reply buffer (%d) "
857991554f2SKenneth D. Merry 				    "smaller than returned buffer (%d)\n",
858991554f2SKenneth D. Merry 				    __func__, data->ReplySize, sz);
859991554f2SKenneth D. Merry 			}
860991554f2SKenneth D. Merry 			mpr_unlock(sc);
861*68cc77a3SMark Johnston 			err = copyout(cm->cm_reply, PTRIN(data->PtrReply),
86272aad3f9SAlan Somers 			    MIN(sz, data->ReplySize));
863991554f2SKenneth D. Merry 			mpr_lock(sc);
864991554f2SKenneth D. Merry 		}
865991554f2SKenneth D. Merry 		mprsas_free_tm(sc, cm);
866991554f2SKenneth D. Merry 		goto Ret;
867991554f2SKenneth D. Merry 	}
868991554f2SKenneth D. Merry 
869991554f2SKenneth D. Merry 	mpr_lock(sc);
870991554f2SKenneth D. Merry 	cm = mpr_alloc_command(sc);
871991554f2SKenneth D. Merry 	if (cm == NULL) {
872991554f2SKenneth D. Merry 		mpr_printf(sc, "%s: no mpr requests\n", __func__);
873991554f2SKenneth D. Merry 		err = ENOMEM;
874991554f2SKenneth D. Merry 		goto Ret;
875991554f2SKenneth D. Merry 	}
876991554f2SKenneth D. Merry 	mpr_unlock(sc);
877991554f2SKenneth D. Merry 
878991554f2SKenneth D. Merry 	hdr = (MPI2_REQUEST_HEADER *)cm->cm_req;
879de828a91SMark Johnston 	memcpy(hdr, req, data->RequestSize);
880991554f2SKenneth D. Merry 
881991554f2SKenneth D. Merry 	/*
882991554f2SKenneth D. Merry 	 * Do some checking to make sure the IOCTL request contains a valid
883991554f2SKenneth D. Merry 	 * request.  Then set the SGL info.
884991554f2SKenneth D. Merry 	 */
885991554f2SKenneth D. Merry 	mpr_init_sge(cm, hdr, (void *)((uint8_t *)hdr + data->RequestSize));
886991554f2SKenneth D. Merry 
887991554f2SKenneth D. Merry 	/*
888991554f2SKenneth D. Merry 	 * Set up for read, write or both.  From check above, DataOutSize will
889991554f2SKenneth D. Merry 	 * be 0 if direction is READ or WRITE, but it will have some non-zero
890991554f2SKenneth D. Merry 	 * value if the direction is BOTH.  So, just use the biggest size to get
891991554f2SKenneth D. Merry 	 * the cm_data buffer size.  If direction is BOTH, 2 SGLs need to be set
892991554f2SKenneth D. Merry 	 * up; the first is for the request and the second will contain the
893991554f2SKenneth D. Merry 	 * response data. cm_out_len needs to be set here and this will be used
894991554f2SKenneth D. Merry 	 * when the SGLs are set up.
895991554f2SKenneth D. Merry 	 */
896991554f2SKenneth D. Merry 	cm->cm_data = NULL;
897991554f2SKenneth D. Merry 	cm->cm_length = MAX(data->DataSize, data->DataOutSize);
898991554f2SKenneth D. Merry 	cm->cm_out_len = data->DataOutSize;
899991554f2SKenneth D. Merry 	cm->cm_flags = 0;
900991554f2SKenneth D. Merry 	if (cm->cm_length != 0) {
901991554f2SKenneth D. Merry 		cm->cm_data = malloc(cm->cm_length, M_MPRUSER, M_WAITOK |
902991554f2SKenneth D. Merry 		    M_ZERO);
903991554f2SKenneth D. Merry 		cm->cm_flags = MPR_CM_FLAGS_DATAIN;
904991554f2SKenneth D. Merry 		if (data->DataOutSize) {
905991554f2SKenneth D. Merry 			cm->cm_flags |= MPR_CM_FLAGS_DATAOUT;
906991554f2SKenneth D. Merry 			err = copyin(PTRIN(data->PtrDataOut),
907991554f2SKenneth D. Merry 			    cm->cm_data, data->DataOutSize);
908991554f2SKenneth D. Merry 		} else if (data->DataDirection ==
909991554f2SKenneth D. Merry 		    MPR_PASS_THRU_DIRECTION_WRITE) {
910991554f2SKenneth D. Merry 			cm->cm_flags = MPR_CM_FLAGS_DATAOUT;
911991554f2SKenneth D. Merry 			err = copyin(PTRIN(data->PtrData),
912991554f2SKenneth D. Merry 			    cm->cm_data, data->DataSize);
913991554f2SKenneth D. Merry 		}
914991554f2SKenneth D. Merry 		if (err != 0)
91567feec50SStephen McConnell 			mpr_dprint(sc, MPR_FAULT, "%s: failed to copy IOCTL "
91667feec50SStephen McConnell 			    "data from user space\n", __func__);
917991554f2SKenneth D. Merry 	}
918991554f2SKenneth D. Merry 	/*
919991554f2SKenneth D. Merry 	 * Set this flag only if processing a command that does not need an
920991554f2SKenneth D. Merry 	 * IEEE SGL.  The CLI Tool within the Toolbox uses IEEE SGLs, so clear
921991554f2SKenneth D. Merry 	 * the flag only for that tool if processing a Toolbox function.
922991554f2SKenneth D. Merry 	 */
923991554f2SKenneth D. Merry 	cm->cm_flags |= MPR_CM_FLAGS_SGE_SIMPLE;
924991554f2SKenneth D. Merry 	for (i = 0; i < sizeof (ieee_sgl_func_list); i++) {
925991554f2SKenneth D. Merry 		if (function == ieee_sgl_func_list[i]) {
926991554f2SKenneth D. Merry 			if (function == MPI2_FUNCTION_TOOLBOX)
927991554f2SKenneth D. Merry 			{
928991554f2SKenneth D. Merry 				tool = (uint8_t)hdr->FunctionDependent1;
929991554f2SKenneth D. Merry 				if (tool != MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL)
930991554f2SKenneth D. Merry 					break;
931991554f2SKenneth D. Merry 			}
932991554f2SKenneth D. Merry 			cm->cm_flags &= ~MPR_CM_FLAGS_SGE_SIMPLE;
933991554f2SKenneth D. Merry 			break;
934991554f2SKenneth D. Merry 		}
935991554f2SKenneth D. Merry 	}
936991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
937991554f2SKenneth D. Merry 
93867feec50SStephen McConnell 	if (function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
93967feec50SStephen McConnell 		nvme_encap_request =
94067feec50SStephen McConnell 		    (Mpi26NVMeEncapsulatedRequest_t *)cm->cm_req;
94167feec50SStephen McConnell 		cm->cm_desc.Default.RequestFlags =
94267feec50SStephen McConnell 		    MPI26_REQ_DESCRIPT_FLAGS_PCIE_ENCAPSULATED;
94367feec50SStephen McConnell 
94467feec50SStephen McConnell 		/*
94567feec50SStephen McConnell 		 * Get the Physical Address of the sense buffer.
94667feec50SStephen McConnell 		 * Save the user's Error Response buffer address and use that
94767feec50SStephen McConnell 		 *   field to hold the sense buffer address.
94867feec50SStephen McConnell 		 * Clear the internal sense buffer, which will potentially hold
94967feec50SStephen McConnell 		 *   the Completion Queue Entry on return, or 0 if no Entry.
95067feec50SStephen McConnell 		 * Build the PRPs and set direction bits.
95167feec50SStephen McConnell 		 * Send the request.
95267feec50SStephen McConnell 		 */
95367feec50SStephen McConnell 		cm->nvme_error_response =
95467feec50SStephen McConnell 		    (uint64_t *)(uintptr_t)(((uint64_t)nvme_encap_request->
95567feec50SStephen McConnell 		    ErrorResponseBaseAddress.High << 32) |
95667feec50SStephen McConnell 		    (uint64_t)nvme_encap_request->
95767feec50SStephen McConnell 		    ErrorResponseBaseAddress.Low);
95867feec50SStephen McConnell 		nvme_encap_request->ErrorResponseBaseAddress.High =
95967feec50SStephen McConnell 		    htole32((uint32_t)((uint64_t)cm->cm_sense_busaddr >> 32));
96067feec50SStephen McConnell 		nvme_encap_request->ErrorResponseBaseAddress.Low =
96167feec50SStephen McConnell 		    htole32(cm->cm_sense_busaddr);
96267feec50SStephen McConnell 		memset(cm->cm_sense, 0, NVME_ERROR_RESPONSE_SIZE);
96367feec50SStephen McConnell 		mpr_build_nvme_prp(sc, cm, nvme_encap_request, cm->cm_data,
96467feec50SStephen McConnell 		    data->DataSize, data->DataOutSize);
96567feec50SStephen McConnell 	}
96667feec50SStephen McConnell 
967991554f2SKenneth D. Merry 	/*
968991554f2SKenneth D. Merry 	 * Set up Sense buffer and SGL offset for IO passthru.  SCSI IO request
969991554f2SKenneth D. Merry 	 * uses SCSI IO or Fast Path SCSI IO descriptor.
970991554f2SKenneth D. Merry 	 */
971991554f2SKenneth D. Merry 	if ((function == MPI2_FUNCTION_SCSI_IO_REQUEST) ||
972991554f2SKenneth D. Merry 	    (function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
973991554f2SKenneth D. Merry 		MPI2_SCSI_IO_REQUEST	*scsi_io_req;
974991554f2SKenneth D. Merry 
975991554f2SKenneth D. Merry 		scsi_io_req = (MPI2_SCSI_IO_REQUEST *)hdr;
976991554f2SKenneth D. Merry 		/*
977991554f2SKenneth D. Merry 		 * Put SGE for data and data_out buffer at the end of
978991554f2SKenneth D. Merry 		 * scsi_io_request message header (64 bytes in total).
979991554f2SKenneth D. Merry 		 * Following above SGEs, the residual space will be used by
980991554f2SKenneth D. Merry 		 * sense data.
981991554f2SKenneth D. Merry 		 */
982991554f2SKenneth D. Merry 		scsi_io_req->SenseBufferLength = (uint8_t)(data->RequestSize -
983991554f2SKenneth D. Merry 		    64);
984991554f2SKenneth D. Merry 		scsi_io_req->SenseBufferLowAddress =
985991554f2SKenneth D. Merry 		    htole32(cm->cm_sense_busaddr);
986991554f2SKenneth D. Merry 
987991554f2SKenneth D. Merry 		/*
988991554f2SKenneth D. Merry 		 * Set SGLOffset0 value.  This is the number of dwords that SGL
989991554f2SKenneth D. Merry 		 * is offset from the beginning of MPI2_SCSI_IO_REQUEST struct.
990991554f2SKenneth D. Merry 		 */
991991554f2SKenneth D. Merry 		scsi_io_req->SGLOffset0 = 24;
992991554f2SKenneth D. Merry 
993991554f2SKenneth D. Merry 		/*
994991554f2SKenneth D. Merry 		 * Setup descriptor info.  RAID passthrough must use the
995991554f2SKenneth D. Merry 		 * default request descriptor which is already set, so if this
996991554f2SKenneth D. Merry 		 * is a SCSI IO request, change the descriptor to SCSI IO or
997991554f2SKenneth D. Merry 		 * Fast Path SCSI IO.  Also, if this is a SCSI IO request,
998991554f2SKenneth D. Merry 		 * handle the reply in the mprsas_scsio_complete function.
999991554f2SKenneth D. Merry 		 */
1000991554f2SKenneth D. Merry 		if (function == MPI2_FUNCTION_SCSI_IO_REQUEST) {
1001991554f2SKenneth D. Merry 			targ = mprsas_find_target_by_handle(sc->sassc, 0,
1002991554f2SKenneth D. Merry 			    scsi_io_req->DevHandle);
1003991554f2SKenneth D. Merry 
1004991554f2SKenneth D. Merry 			if (!targ) {
1005991554f2SKenneth D. Merry 				printf("No Target found for handle %d\n",
1006991554f2SKenneth D. Merry 				    scsi_io_req->DevHandle);
1007991554f2SKenneth D. Merry 				err = EINVAL;
1008991554f2SKenneth D. Merry 				goto RetFreeUnlocked;
1009991554f2SKenneth D. Merry 			}
1010991554f2SKenneth D. Merry 
1011991554f2SKenneth D. Merry 			if (targ->scsi_req_desc_type ==
1012991554f2SKenneth D. Merry 			    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO) {
1013991554f2SKenneth D. Merry 				cm->cm_desc.FastPathSCSIIO.RequestFlags =
1014991554f2SKenneth D. Merry 				    MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
101567feec50SStephen McConnell 				if (!sc->atomic_desc_capable) {
1016991554f2SKenneth D. Merry 					cm->cm_desc.FastPathSCSIIO.DevHandle =
1017991554f2SKenneth D. Merry 					    scsi_io_req->DevHandle;
101867feec50SStephen McConnell 				}
1019991554f2SKenneth D. Merry 				scsi_io_req->IoFlags |=
1020991554f2SKenneth D. Merry 				    MPI25_SCSIIO_IOFLAGS_FAST_PATH;
1021991554f2SKenneth D. Merry 			} else {
1022991554f2SKenneth D. Merry 				cm->cm_desc.SCSIIO.RequestFlags =
1023991554f2SKenneth D. Merry 				    MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
102467feec50SStephen McConnell 				if (!sc->atomic_desc_capable) {
1025991554f2SKenneth D. Merry 					cm->cm_desc.SCSIIO.DevHandle =
1026991554f2SKenneth D. Merry 					    scsi_io_req->DevHandle;
1027991554f2SKenneth D. Merry 				}
102867feec50SStephen McConnell 			}
1029991554f2SKenneth D. Merry 
1030991554f2SKenneth D. Merry 			/*
1031991554f2SKenneth D. Merry 			 * Make sure the DevHandle is not 0 because this is a
1032991554f2SKenneth D. Merry 			 * likely error.
1033991554f2SKenneth D. Merry 			 */
1034991554f2SKenneth D. Merry 			if (scsi_io_req->DevHandle == 0) {
1035991554f2SKenneth D. Merry 				err = EINVAL;
1036991554f2SKenneth D. Merry 				goto RetFreeUnlocked;
1037991554f2SKenneth D. Merry 			}
1038991554f2SKenneth D. Merry 		}
1039991554f2SKenneth D. Merry 	}
1040991554f2SKenneth D. Merry 
1041991554f2SKenneth D. Merry 	mpr_lock(sc);
1042991554f2SKenneth D. Merry 
10436d4ffcb4SKenneth D. Merry 	err = mpr_wait_command(sc, &cm, 30, CAN_SLEEP);
1044991554f2SKenneth D. Merry 
10456d4ffcb4SKenneth D. Merry 	if (err || (cm == NULL)) {
1046991554f2SKenneth D. Merry 		mpr_printf(sc, "%s: invalid request: error %d\n", __func__,
1047991554f2SKenneth D. Merry 		    err);
10486d4ffcb4SKenneth D. Merry 		goto RetFree;
1049991554f2SKenneth D. Merry 	}
1050991554f2SKenneth D. Merry 
1051991554f2SKenneth D. Merry 	/*
1052991554f2SKenneth D. Merry 	 * Sync the DMA data, if any.  Then copy the data to user space.
1053991554f2SKenneth D. Merry 	 */
1054991554f2SKenneth D. Merry 	if (cm->cm_data != NULL) {
1055991554f2SKenneth D. Merry 		if (cm->cm_flags & MPR_CM_FLAGS_DATAIN)
1056991554f2SKenneth D. Merry 			dir = BUS_DMASYNC_POSTREAD;
1057991554f2SKenneth D. Merry 		else if (cm->cm_flags & MPR_CM_FLAGS_DATAOUT)
1058a2c14879SStephen McConnell 			dir = BUS_DMASYNC_POSTWRITE;
1059991554f2SKenneth D. Merry 		bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir);
1060991554f2SKenneth D. Merry 		bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
1061991554f2SKenneth D. Merry 
1062991554f2SKenneth D. Merry 		if (cm->cm_flags & MPR_CM_FLAGS_DATAIN) {
1063991554f2SKenneth D. Merry 			mpr_unlock(sc);
1064991554f2SKenneth D. Merry 			err = copyout(cm->cm_data,
1065991554f2SKenneth D. Merry 			    PTRIN(data->PtrData), data->DataSize);
1066991554f2SKenneth D. Merry 			mpr_lock(sc);
1067991554f2SKenneth D. Merry 			if (err != 0)
1068991554f2SKenneth D. Merry 				mpr_dprint(sc, MPR_FAULT, "%s: failed to copy "
1069991554f2SKenneth D. Merry 				    "IOCTL data to user space\n", __func__);
1070991554f2SKenneth D. Merry 		}
1071991554f2SKenneth D. Merry 	}
1072991554f2SKenneth D. Merry 
1073991554f2SKenneth D. Merry 	/*
1074991554f2SKenneth D. Merry 	 * Copy the reply data and sense data to user space.
1075991554f2SKenneth D. Merry 	 */
1076*68cc77a3SMark Johnston 	if (err == 0 && cm->cm_reply != NULL) {
1077991554f2SKenneth D. Merry 		rpl = (MPI2_DEFAULT_REPLY *)cm->cm_reply;
1078991554f2SKenneth D. Merry 		sz = rpl->MsgLength * 4;
1079991554f2SKenneth D. Merry 
10807d154c4dSAlan Somers 		if (bootverbose && sz > data->ReplySize) {
1081991554f2SKenneth D. Merry 			mpr_printf(sc, "%s: user reply buffer (%d) smaller "
1082991554f2SKenneth D. Merry 			    "than returned buffer (%d)\n", __func__,
1083991554f2SKenneth D. Merry 			    data->ReplySize, sz);
1084991554f2SKenneth D. Merry 		}
1085991554f2SKenneth D. Merry 		mpr_unlock(sc);
1086*68cc77a3SMark Johnston 		err = copyout(cm->cm_reply, PTRIN(data->PtrReply),
108772aad3f9SAlan Somers 		    MIN(sz, data->ReplySize));
1088*68cc77a3SMark Johnston 		if (err != 0)
1089*68cc77a3SMark Johnston 			mpr_dprint(sc, MPR_FAULT, "%s: failed to copy "
1090*68cc77a3SMark Johnston 			    "IOCTL data to user space\n", __func__);
1091991554f2SKenneth D. Merry 		mpr_lock(sc);
1092991554f2SKenneth D. Merry 
1093*68cc77a3SMark Johnston 		if (err == 0 &&
1094*68cc77a3SMark Johnston 		    (function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
1095*68cc77a3SMark Johnston 		    function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
1096991554f2SKenneth D. Merry 			if (((MPI2_SCSI_IO_REPLY *)rpl)->SCSIState &
1097991554f2SKenneth D. Merry 			    MPI2_SCSI_STATE_AUTOSENSE_VALID) {
1098991554f2SKenneth D. Merry 				sense_len =
1099991554f2SKenneth D. Merry 				    MIN((le32toh(((MPI2_SCSI_IO_REPLY *)rpl)->
1100991554f2SKenneth D. Merry 				    SenseCount)), sizeof(struct
1101991554f2SKenneth D. Merry 				    scsi_sense_data));
1102991554f2SKenneth D. Merry 				mpr_unlock(sc);
1103*68cc77a3SMark Johnston 				err = copyout(cm->cm_sense,
1104*68cc77a3SMark Johnston 				    PTRIN(data->PtrReply +
1105*68cc77a3SMark Johnston 				    sizeof(MPI2_SCSI_IO_REPLY)), sense_len);
1106*68cc77a3SMark Johnston 				if (err != 0)
1107*68cc77a3SMark Johnston 					mpr_dprint(sc, MPR_FAULT,
1108*68cc77a3SMark Johnston 					    "%s: failed to copy IOCTL data to "
1109*68cc77a3SMark Johnston 					    "user space\n", __func__);
1110991554f2SKenneth D. Merry 				mpr_lock(sc);
1111991554f2SKenneth D. Merry 			}
1112991554f2SKenneth D. Merry 		}
111367feec50SStephen McConnell 
111467feec50SStephen McConnell 		/*
111567feec50SStephen McConnell 		 * Copy out the NVMe Error Reponse to user. The Error Response
111667feec50SStephen McConnell 		 * buffer is given by the user, but a sense buffer is used to
111767feec50SStephen McConnell 		 * get that data from the IOC. The user's
111867feec50SStephen McConnell 		 * ErrorResponseBaseAddress is saved in the
111967feec50SStephen McConnell 		 * 'nvme_error_response' field before the command because that
112067feec50SStephen McConnell 		 * field is set to a sense buffer. When the command is
112167feec50SStephen McConnell 		 * complete, the Error Response data from the IOC is copied to
112267feec50SStephen McConnell 		 * that user address after it is checked for validity.
112367feec50SStephen McConnell 		 * Also note that 'sense' buffers are not defined for
112467feec50SStephen McConnell 		 * NVMe commands. Sense terminalogy is only used here so that
112567feec50SStephen McConnell 		 * the same IOCTL structure and sense buffers can be used for
112667feec50SStephen McConnell 		 * NVMe.
112767feec50SStephen McConnell 		 */
1128*68cc77a3SMark Johnston 		if (err == 0 && function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
112967feec50SStephen McConnell 			if (cm->nvme_error_response == NULL) {
113067feec50SStephen McConnell 				mpr_dprint(sc, MPR_INFO, "NVMe Error Response "
113167feec50SStephen McConnell 				    "buffer is NULL. Response data will not be "
113267feec50SStephen McConnell 				    "returned.\n");
113367feec50SStephen McConnell 				mpr_unlock(sc);
113467feec50SStephen McConnell 				goto RetFreeUnlocked;
113567feec50SStephen McConnell 			}
113667feec50SStephen McConnell 
113767feec50SStephen McConnell 			nvme_error_reply =
113867feec50SStephen McConnell 			    (Mpi26NVMeEncapsulatedErrorReply_t *)cm->cm_reply;
113967feec50SStephen McConnell 			sz = MIN(le32toh(nvme_error_reply->ErrorResponseCount),
114067feec50SStephen McConnell 			    NVME_ERROR_RESPONSE_SIZE);
114167feec50SStephen McConnell 			mpr_unlock(sc);
1142*68cc77a3SMark Johnston 			err = copyout(cm->cm_sense,
114323daf8f4SKashyap D Desai 			    (PTRIN(data->PtrReply +
114423daf8f4SKashyap D Desai 			    sizeof(MPI2_SCSI_IO_REPLY))), sz);
1145*68cc77a3SMark Johnston 			if (err != 0)
1146*68cc77a3SMark Johnston 				mpr_dprint(sc, MPR_FAULT,
1147*68cc77a3SMark Johnston 				    "%s: failed to copy IOCTL data to "
1148*68cc77a3SMark Johnston 				    "user space\n", __func__);
114967feec50SStephen McConnell 			mpr_lock(sc);
115067feec50SStephen McConnell 		}
1151991554f2SKenneth D. Merry 	}
1152991554f2SKenneth D. Merry 	mpr_unlock(sc);
1153991554f2SKenneth D. Merry 
1154991554f2SKenneth D. Merry RetFreeUnlocked:
1155991554f2SKenneth D. Merry 	mpr_lock(sc);
1156991554f2SKenneth D. Merry 
11576d4ffcb4SKenneth D. Merry RetFree:
1158991554f2SKenneth D. Merry 	if (cm != NULL) {
1159991554f2SKenneth D. Merry 		if (cm->cm_data)
1160991554f2SKenneth D. Merry 			free(cm->cm_data, M_MPRUSER);
1161991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
1162991554f2SKenneth D. Merry 	}
1163991554f2SKenneth D. Merry Ret:
1164991554f2SKenneth D. Merry 	sc->mpr_flags &= ~MPR_FLAGS_BUSY;
1165991554f2SKenneth D. Merry 	mpr_unlock(sc);
1166de828a91SMark Johnston 	free(req, M_MPRUSER);
1167991554f2SKenneth D. Merry 
1168991554f2SKenneth D. Merry 	return (err);
1169991554f2SKenneth D. Merry }
1170991554f2SKenneth D. Merry 
1171991554f2SKenneth D. Merry static void
mpr_user_get_adapter_data(struct mpr_softc * sc,mpr_adapter_data_t * data)1172991554f2SKenneth D. Merry mpr_user_get_adapter_data(struct mpr_softc *sc, mpr_adapter_data_t *data)
1173991554f2SKenneth D. Merry {
1174991554f2SKenneth D. Merry 	Mpi2ConfigReply_t	mpi_reply;
1175991554f2SKenneth D. Merry 	Mpi2BiosPage3_t		config_page;
1176991554f2SKenneth D. Merry 
1177991554f2SKenneth D. Merry 	/*
1178991554f2SKenneth D. Merry 	 * Use the PCI interface functions to get the Bus, Device, and Function
1179991554f2SKenneth D. Merry 	 * information.
1180991554f2SKenneth D. Merry 	 */
1181991554f2SKenneth D. Merry 	data->PciInformation.u.bits.BusNumber = pci_get_bus(sc->mpr_dev);
1182991554f2SKenneth D. Merry 	data->PciInformation.u.bits.DeviceNumber = pci_get_slot(sc->mpr_dev);
1183991554f2SKenneth D. Merry 	data->PciInformation.u.bits.FunctionNumber =
1184991554f2SKenneth D. Merry 	    pci_get_function(sc->mpr_dev);
1185991554f2SKenneth D. Merry 
1186991554f2SKenneth D. Merry 	/*
1187991554f2SKenneth D. Merry 	 * Get the FW version that should already be saved in IOC Facts.
1188991554f2SKenneth D. Merry 	 */
1189991554f2SKenneth D. Merry 	data->MpiFirmwareVersion = sc->facts->FWVersion.Word;
1190991554f2SKenneth D. Merry 
1191991554f2SKenneth D. Merry 	/*
1192991554f2SKenneth D. Merry 	 * General device info.
1193991554f2SKenneth D. Merry 	 */
11945f5baf0eSAlexander Motin 	if (sc->mpr_flags & MPR_FLAGS_GEN35_IOC)
11955f5baf0eSAlexander Motin 		data->AdapterType = MPRIOCTL_ADAPTER_TYPE_SAS35;
11965f5baf0eSAlexander Motin 	else
1197991554f2SKenneth D. Merry 		data->AdapterType = MPRIOCTL_ADAPTER_TYPE_SAS3;
1198991554f2SKenneth D. Merry 	data->PCIDeviceHwId = pci_get_device(sc->mpr_dev);
1199991554f2SKenneth D. Merry 	data->PCIDeviceHwRev = pci_read_config(sc->mpr_dev, PCIR_REVID, 1);
1200991554f2SKenneth D. Merry 	data->SubSystemId = pci_get_subdevice(sc->mpr_dev);
1201991554f2SKenneth D. Merry 	data->SubsystemVendorId = pci_get_subvendor(sc->mpr_dev);
1202991554f2SKenneth D. Merry 
1203991554f2SKenneth D. Merry 	/*
1204991554f2SKenneth D. Merry 	 * Get the driver version.
1205991554f2SKenneth D. Merry 	 */
1206991554f2SKenneth D. Merry 	strcpy((char *)&data->DriverVersion[0], MPR_DRIVER_VERSION);
1207991554f2SKenneth D. Merry 
1208991554f2SKenneth D. Merry 	/*
1209991554f2SKenneth D. Merry 	 * Need to get BIOS Config Page 3 for the BIOS Version.
1210991554f2SKenneth D. Merry 	 */
1211991554f2SKenneth D. Merry 	data->BiosVersion = 0;
1212991554f2SKenneth D. Merry 	mpr_lock(sc);
1213991554f2SKenneth D. Merry 	if (mpr_config_get_bios_pg3(sc, &mpi_reply, &config_page))
1214991554f2SKenneth D. Merry 		printf("%s: Error while retrieving BIOS Version\n", __func__);
1215991554f2SKenneth D. Merry 	else
1216991554f2SKenneth D. Merry 		data->BiosVersion = config_page.BiosVersion;
1217991554f2SKenneth D. Merry 	mpr_unlock(sc);
1218991554f2SKenneth D. Merry }
1219991554f2SKenneth D. Merry 
1220991554f2SKenneth D. Merry static void
mpr_user_read_pci_info(struct mpr_softc * sc,mpr_pci_info_t * data)1221991554f2SKenneth D. Merry mpr_user_read_pci_info(struct mpr_softc *sc, mpr_pci_info_t *data)
1222991554f2SKenneth D. Merry {
1223991554f2SKenneth D. Merry 	int	i;
1224991554f2SKenneth D. Merry 
1225991554f2SKenneth D. Merry 	/*
1226991554f2SKenneth D. Merry 	 * Use the PCI interface functions to get the Bus, Device, and Function
1227991554f2SKenneth D. Merry 	 * information.
1228991554f2SKenneth D. Merry 	 */
1229991554f2SKenneth D. Merry 	data->BusNumber = pci_get_bus(sc->mpr_dev);
1230991554f2SKenneth D. Merry 	data->DeviceNumber = pci_get_slot(sc->mpr_dev);
1231991554f2SKenneth D. Merry 	data->FunctionNumber = pci_get_function(sc->mpr_dev);
1232991554f2SKenneth D. Merry 
1233991554f2SKenneth D. Merry 	/*
1234991554f2SKenneth D. Merry 	 * Now get the interrupt vector and the pci header.  The vector can
1235991554f2SKenneth D. Merry 	 * only be 0 right now.  The header is the first 256 bytes of config
1236991554f2SKenneth D. Merry 	 * space.
1237991554f2SKenneth D. Merry 	 */
1238991554f2SKenneth D. Merry 	data->InterruptVector = 0;
1239991554f2SKenneth D. Merry 	for (i = 0; i < sizeof (data->PciHeader); i++) {
1240991554f2SKenneth D. Merry 		data->PciHeader[i] = pci_read_config(sc->mpr_dev, i, 1);
1241991554f2SKenneth D. Merry 	}
1242991554f2SKenneth D. Merry }
1243991554f2SKenneth D. Merry 
1244991554f2SKenneth D. Merry static uint8_t
mpr_get_fw_diag_buffer_number(struct mpr_softc * sc,uint32_t unique_id)1245991554f2SKenneth D. Merry mpr_get_fw_diag_buffer_number(struct mpr_softc *sc, uint32_t unique_id)
1246991554f2SKenneth D. Merry {
1247991554f2SKenneth D. Merry 	uint8_t	index;
1248991554f2SKenneth D. Merry 
1249991554f2SKenneth D. Merry 	for (index = 0; index < MPI2_DIAG_BUF_TYPE_COUNT; index++) {
1250991554f2SKenneth D. Merry 		if (sc->fw_diag_buffer_list[index].unique_id == unique_id) {
1251991554f2SKenneth D. Merry 			return (index);
1252991554f2SKenneth D. Merry 		}
1253991554f2SKenneth D. Merry 	}
1254991554f2SKenneth D. Merry 
1255991554f2SKenneth D. Merry 	return (MPR_FW_DIAGNOSTIC_UID_NOT_FOUND);
1256991554f2SKenneth D. Merry }
1257991554f2SKenneth D. Merry 
1258991554f2SKenneth D. Merry static int
mpr_post_fw_diag_buffer(struct mpr_softc * sc,mpr_fw_diagnostic_buffer_t * pBuffer,uint32_t * return_code)1259991554f2SKenneth D. Merry mpr_post_fw_diag_buffer(struct mpr_softc *sc,
1260991554f2SKenneth D. Merry     mpr_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code)
1261991554f2SKenneth D. Merry {
1262991554f2SKenneth D. Merry 	MPI2_DIAG_BUFFER_POST_REQUEST	*req;
1263991554f2SKenneth D. Merry 	MPI2_DIAG_BUFFER_POST_REPLY	*reply;
1264991554f2SKenneth D. Merry 	struct mpr_command		*cm = NULL;
1265991554f2SKenneth D. Merry 	int				i, status;
1266991554f2SKenneth D. Merry 
1267991554f2SKenneth D. Merry 	/*
1268991554f2SKenneth D. Merry 	 * If buffer is not enabled, just leave.
1269991554f2SKenneth D. Merry 	 */
1270991554f2SKenneth D. Merry 	*return_code = MPR_FW_DIAG_ERROR_POST_FAILED;
1271991554f2SKenneth D. Merry 	if (!pBuffer->enabled) {
1272991554f2SKenneth D. Merry 		return (MPR_DIAG_FAILURE);
1273991554f2SKenneth D. Merry 	}
1274991554f2SKenneth D. Merry 
1275991554f2SKenneth D. Merry 	/*
1276991554f2SKenneth D. Merry 	 * Clear some flags initially.
1277991554f2SKenneth D. Merry 	 */
1278991554f2SKenneth D. Merry 	pBuffer->force_release = FALSE;
1279991554f2SKenneth D. Merry 	pBuffer->valid_data = FALSE;
1280991554f2SKenneth D. Merry 	pBuffer->owned_by_firmware = FALSE;
1281991554f2SKenneth D. Merry 
1282991554f2SKenneth D. Merry 	/*
1283991554f2SKenneth D. Merry 	 * Get a command.
1284991554f2SKenneth D. Merry 	 */
1285991554f2SKenneth D. Merry 	cm = mpr_alloc_command(sc);
1286991554f2SKenneth D. Merry 	if (cm == NULL) {
1287991554f2SKenneth D. Merry 		mpr_printf(sc, "%s: no mpr requests\n", __func__);
1288991554f2SKenneth D. Merry 		return (MPR_DIAG_FAILURE);
1289991554f2SKenneth D. Merry 	}
1290991554f2SKenneth D. Merry 
1291991554f2SKenneth D. Merry 	/*
1292991554f2SKenneth D. Merry 	 * Build the request for releasing the FW Diag Buffer and send it.
1293991554f2SKenneth D. Merry 	 */
1294991554f2SKenneth D. Merry 	req = (MPI2_DIAG_BUFFER_POST_REQUEST *)cm->cm_req;
1295991554f2SKenneth D. Merry 	req->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1296991554f2SKenneth D. Merry 	req->BufferType = pBuffer->buffer_type;
1297991554f2SKenneth D. Merry 	req->ExtendedType = pBuffer->extended_type;
1298991554f2SKenneth D. Merry 	req->BufferLength = pBuffer->size;
1299991554f2SKenneth D. Merry 	for (i = 0; i < (sizeof(req->ProductSpecific) / 4); i++)
1300991554f2SKenneth D. Merry 		req->ProductSpecific[i] = pBuffer->product_specific[i];
1301991554f2SKenneth D. Merry 	mpr_from_u64(sc->fw_diag_busaddr, &req->BufferAddress);
1302991554f2SKenneth D. Merry 	cm->cm_data = NULL;
1303991554f2SKenneth D. Merry 	cm->cm_length = 0;
1304991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1305991554f2SKenneth D. Merry 	cm->cm_complete_data = NULL;
1306991554f2SKenneth D. Merry 
1307991554f2SKenneth D. Merry 	/*
1308991554f2SKenneth D. Merry 	 * Send command synchronously.
1309991554f2SKenneth D. Merry 	 */
13106d4ffcb4SKenneth D. Merry 	status = mpr_wait_command(sc, &cm, 30, CAN_SLEEP);
13116d4ffcb4SKenneth D. Merry 	if (status || (cm == NULL)) {
1312991554f2SKenneth D. Merry 		mpr_printf(sc, "%s: invalid request: error %d\n", __func__,
1313991554f2SKenneth D. Merry 		    status);
1314991554f2SKenneth D. Merry 		status = MPR_DIAG_FAILURE;
1315991554f2SKenneth D. Merry 		goto done;
1316991554f2SKenneth D. Merry 	}
1317991554f2SKenneth D. Merry 
1318991554f2SKenneth D. Merry 	/*
1319991554f2SKenneth D. Merry 	 * Process POST reply.
1320991554f2SKenneth D. Merry 	 */
1321991554f2SKenneth D. Merry 	reply = (MPI2_DIAG_BUFFER_POST_REPLY *)cm->cm_reply;
1322e2997a03SKenneth D. Merry 	if (reply == NULL) {
1323e2997a03SKenneth D. Merry 		mpr_printf(sc, "%s: reply is NULL, probably due to "
132427ac4281SEd Maste 		    "reinitialization\n", __func__);
1325e2997a03SKenneth D. Merry 		status = MPR_DIAG_FAILURE;
1326e2997a03SKenneth D. Merry 		goto done;
1327e2997a03SKenneth D. Merry 	}
1328e2997a03SKenneth D. Merry 
1329d3f6eabfSStephen McConnell 	if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
1330d3f6eabfSStephen McConnell 	    MPI2_IOCSTATUS_SUCCESS) {
1331991554f2SKenneth D. Merry 		status = MPR_DIAG_FAILURE;
1332991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "%s: post of FW  Diag Buffer failed "
1333991554f2SKenneth D. Merry 		    "with IOCStatus = 0x%x, IOCLogInfo = 0x%x and "
1334d3f6eabfSStephen McConnell 		    "TransferLength = 0x%x\n", __func__,
1335d3f6eabfSStephen McConnell 		    le16toh(reply->IOCStatus), le32toh(reply->IOCLogInfo),
1336d3f6eabfSStephen McConnell 		    le32toh(reply->TransferLength));
1337991554f2SKenneth D. Merry 		goto done;
1338991554f2SKenneth D. Merry 	}
1339991554f2SKenneth D. Merry 
1340991554f2SKenneth D. Merry 	/*
1341991554f2SKenneth D. Merry 	 * Post was successful.
1342991554f2SKenneth D. Merry 	 */
1343991554f2SKenneth D. Merry 	pBuffer->valid_data = TRUE;
1344991554f2SKenneth D. Merry 	pBuffer->owned_by_firmware = TRUE;
1345991554f2SKenneth D. Merry 	*return_code = MPR_FW_DIAG_ERROR_SUCCESS;
1346991554f2SKenneth D. Merry 	status = MPR_DIAG_SUCCESS;
1347991554f2SKenneth D. Merry 
1348991554f2SKenneth D. Merry done:
13496d4ffcb4SKenneth D. Merry 	if (cm != NULL)
1350991554f2SKenneth D. Merry 		mpr_free_command(sc, cm);
1351991554f2SKenneth D. Merry 	return (status);
1352991554f2SKenneth D. Merry }
1353991554f2SKenneth D. Merry 
1354991554f2SKenneth D. Merry static int
mpr_release_fw_diag_buffer(struct mpr_softc * sc,mpr_fw_diagnostic_buffer_t * pBuffer,uint32_t * return_code,uint32_t diag_type)1355991554f2SKenneth D. Merry mpr_release_fw_diag_buffer(struct mpr_softc *sc,
1356991554f2SKenneth D. Merry     mpr_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code,
1357991554f2SKenneth D. Merry     uint32_t diag_type)
1358991554f2SKenneth D. Merry {
1359991554f2SKenneth D. Merry 	MPI2_DIAG_RELEASE_REQUEST	*req;
1360991554f2SKenneth D. Merry 	MPI2_DIAG_RELEASE_REPLY		*reply;
1361991554f2SKenneth D. Merry 	struct mpr_command		*cm = NULL;
1362991554f2SKenneth D. Merry 	int				status;
1363991554f2SKenneth D. Merry 
1364991554f2SKenneth D. Merry 	/*
1365991554f2SKenneth D. Merry 	 * If buffer is not enabled, just leave.
1366991554f2SKenneth D. Merry 	 */
1367991554f2SKenneth D. Merry 	*return_code = MPR_FW_DIAG_ERROR_RELEASE_FAILED;
1368991554f2SKenneth D. Merry 	if (!pBuffer->enabled) {
1369991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_USER, "%s: This buffer type is not "
1370991554f2SKenneth D. Merry 		    "supported by the IOC", __func__);
1371991554f2SKenneth D. Merry 		return (MPR_DIAG_FAILURE);
1372991554f2SKenneth D. Merry 	}
1373991554f2SKenneth D. Merry 
1374991554f2SKenneth D. Merry 	/*
1375991554f2SKenneth D. Merry 	 * Clear some flags initially.
1376991554f2SKenneth D. Merry 	 */
1377991554f2SKenneth D. Merry 	pBuffer->force_release = FALSE;
1378991554f2SKenneth D. Merry 	pBuffer->valid_data = FALSE;
1379991554f2SKenneth D. Merry 	pBuffer->owned_by_firmware = FALSE;
1380991554f2SKenneth D. Merry 
1381991554f2SKenneth D. Merry 	/*
1382991554f2SKenneth D. Merry 	 * Get a command.
1383991554f2SKenneth D. Merry 	 */
1384991554f2SKenneth D. Merry 	cm = mpr_alloc_command(sc);
1385991554f2SKenneth D. Merry 	if (cm == NULL) {
1386991554f2SKenneth D. Merry 		mpr_printf(sc, "%s: no mpr requests\n", __func__);
1387991554f2SKenneth D. Merry 		return (MPR_DIAG_FAILURE);
1388991554f2SKenneth D. Merry 	}
1389991554f2SKenneth D. Merry 
1390991554f2SKenneth D. Merry 	/*
1391991554f2SKenneth D. Merry 	 * Build the request for releasing the FW Diag Buffer and send it.
1392991554f2SKenneth D. Merry 	 */
1393991554f2SKenneth D. Merry 	req = (MPI2_DIAG_RELEASE_REQUEST *)cm->cm_req;
1394991554f2SKenneth D. Merry 	req->Function = MPI2_FUNCTION_DIAG_RELEASE;
1395991554f2SKenneth D. Merry 	req->BufferType = pBuffer->buffer_type;
1396991554f2SKenneth D. Merry 	cm->cm_data = NULL;
1397991554f2SKenneth D. Merry 	cm->cm_length = 0;
1398991554f2SKenneth D. Merry 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1399991554f2SKenneth D. Merry 	cm->cm_complete_data = NULL;
1400991554f2SKenneth D. Merry 
1401991554f2SKenneth D. Merry 	/*
1402991554f2SKenneth D. Merry 	 * Send command synchronously.
1403991554f2SKenneth D. Merry 	 */
14046d4ffcb4SKenneth D. Merry 	status = mpr_wait_command(sc, &cm, 30, CAN_SLEEP);
14056d4ffcb4SKenneth D. Merry 	if (status || (cm == NULL)) {
1406991554f2SKenneth D. Merry 		mpr_printf(sc, "%s: invalid request: error %d\n", __func__,
1407991554f2SKenneth D. Merry 		    status);
1408991554f2SKenneth D. Merry 		status = MPR_DIAG_FAILURE;
1409991554f2SKenneth D. Merry 		goto done;
1410991554f2SKenneth D. Merry 	}
1411991554f2SKenneth D. Merry 
1412991554f2SKenneth D. Merry 	/*
1413991554f2SKenneth D. Merry 	 * Process RELEASE reply.
1414991554f2SKenneth D. Merry 	 */
1415991554f2SKenneth D. Merry 	reply = (MPI2_DIAG_RELEASE_REPLY *)cm->cm_reply;
1416e2997a03SKenneth D. Merry 	if (reply == NULL) {
1417e2997a03SKenneth D. Merry 		mpr_printf(sc, "%s: reply is NULL, probably due to "
141827ac4281SEd Maste 		    "reinitialization\n", __func__);
1419e2997a03SKenneth D. Merry 		status = MPR_DIAG_FAILURE;
1420e2997a03SKenneth D. Merry 		goto done;
1421e2997a03SKenneth D. Merry 	}
1422d3f6eabfSStephen McConnell 	if (((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
1423d3f6eabfSStephen McConnell 	    MPI2_IOCSTATUS_SUCCESS) || pBuffer->owned_by_firmware) {
1424991554f2SKenneth D. Merry 		status = MPR_DIAG_FAILURE;
1425991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_FAULT, "%s: release of FW Diag Buffer "
1426991554f2SKenneth D. Merry 		    "failed with IOCStatus = 0x%x and IOCLogInfo = 0x%x\n",
1427d3f6eabfSStephen McConnell 		    __func__, le16toh(reply->IOCStatus),
1428d3f6eabfSStephen McConnell 		    le32toh(reply->IOCLogInfo));
1429991554f2SKenneth D. Merry 		goto done;
1430991554f2SKenneth D. Merry 	}
1431991554f2SKenneth D. Merry 
1432991554f2SKenneth D. Merry 	/*
1433991554f2SKenneth D. Merry 	 * Release was successful.
1434991554f2SKenneth D. Merry 	 */
1435991554f2SKenneth D. Merry 	*return_code = MPR_FW_DIAG_ERROR_SUCCESS;
1436991554f2SKenneth D. Merry 	status = MPR_DIAG_SUCCESS;
1437991554f2SKenneth D. Merry 
1438991554f2SKenneth D. Merry 	/*
1439991554f2SKenneth D. Merry 	 * If this was for an UNREGISTER diag type command, clear the unique ID.
1440991554f2SKenneth D. Merry 	 */
1441991554f2SKenneth D. Merry 	if (diag_type == MPR_FW_DIAG_TYPE_UNREGISTER) {
1442991554f2SKenneth D. Merry 		pBuffer->unique_id = MPR_FW_DIAG_INVALID_UID;
1443991554f2SKenneth D. Merry 	}
1444991554f2SKenneth D. Merry 
1445991554f2SKenneth D. Merry done:
14466d4ffcb4SKenneth D. Merry 	if (cm != NULL)
14476d4ffcb4SKenneth D. Merry 		mpr_free_command(sc, cm);
14486d4ffcb4SKenneth D. Merry 
1449991554f2SKenneth D. Merry 	return (status);
1450991554f2SKenneth D. Merry }
1451991554f2SKenneth D. Merry 
1452991554f2SKenneth D. Merry static int
mpr_diag_register(struct mpr_softc * sc,mpr_fw_diag_register_t * diag_register,uint32_t * return_code)1453a2c14879SStephen McConnell mpr_diag_register(struct mpr_softc *sc, mpr_fw_diag_register_t *diag_register,
1454a2c14879SStephen McConnell     uint32_t *return_code)
1455991554f2SKenneth D. Merry {
145674c781edSScott Long 	bus_dma_template_t		t;
1457991554f2SKenneth D. Merry 	mpr_fw_diagnostic_buffer_t	*pBuffer;
1458e2997a03SKenneth D. Merry 	struct mpr_busdma_context	*ctx;
1459991554f2SKenneth D. Merry 	uint8_t				extended_type, buffer_type, i;
1460991554f2SKenneth D. Merry 	uint32_t			buffer_size;
1461991554f2SKenneth D. Merry 	uint32_t			unique_id;
1462991554f2SKenneth D. Merry 	int				status;
1463e2997a03SKenneth D. Merry 	int				error;
1464991554f2SKenneth D. Merry 
1465991554f2SKenneth D. Merry 	extended_type = diag_register->ExtendedType;
1466991554f2SKenneth D. Merry 	buffer_type = diag_register->BufferType;
1467991554f2SKenneth D. Merry 	buffer_size = diag_register->RequestedBufferSize;
1468991554f2SKenneth D. Merry 	unique_id = diag_register->UniqueId;
1469e2997a03SKenneth D. Merry 	ctx = NULL;
1470e2997a03SKenneth D. Merry 	error = 0;
1471991554f2SKenneth D. Merry 
1472991554f2SKenneth D. Merry 	/*
1473991554f2SKenneth D. Merry 	 * Check for valid buffer type
1474991554f2SKenneth D. Merry 	 */
1475991554f2SKenneth D. Merry 	if (buffer_type >= MPI2_DIAG_BUF_TYPE_COUNT) {
1476991554f2SKenneth D. Merry 		*return_code = MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1477991554f2SKenneth D. Merry 		return (MPR_DIAG_FAILURE);
1478991554f2SKenneth D. Merry 	}
1479991554f2SKenneth D. Merry 
1480991554f2SKenneth D. Merry 	/*
1481991554f2SKenneth D. Merry 	 * Get the current buffer and look up the unique ID.  The unique ID
1482991554f2SKenneth D. Merry 	 * should not be found.  If it is, the ID is already in use.
1483991554f2SKenneth D. Merry 	 */
1484991554f2SKenneth D. Merry 	i = mpr_get_fw_diag_buffer_number(sc, unique_id);
1485991554f2SKenneth D. Merry 	pBuffer = &sc->fw_diag_buffer_list[buffer_type];
1486991554f2SKenneth D. Merry 	if (i != MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1487991554f2SKenneth D. Merry 		*return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1488991554f2SKenneth D. Merry 		return (MPR_DIAG_FAILURE);
1489991554f2SKenneth D. Merry 	}
1490991554f2SKenneth D. Merry 
1491991554f2SKenneth D. Merry 	/*
1492991554f2SKenneth D. Merry 	 * The buffer's unique ID should not be registered yet, and the given
1493991554f2SKenneth D. Merry 	 * unique ID cannot be 0.
1494991554f2SKenneth D. Merry 	 */
1495991554f2SKenneth D. Merry 	if ((pBuffer->unique_id != MPR_FW_DIAG_INVALID_UID) ||
1496991554f2SKenneth D. Merry 	    (unique_id == MPR_FW_DIAG_INVALID_UID)) {
1497991554f2SKenneth D. Merry 		*return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1498991554f2SKenneth D. Merry 		return (MPR_DIAG_FAILURE);
1499991554f2SKenneth D. Merry 	}
1500991554f2SKenneth D. Merry 
1501991554f2SKenneth D. Merry 	/*
1502991554f2SKenneth D. Merry 	 * If this buffer is already posted as immediate, just change owner.
1503991554f2SKenneth D. Merry 	 */
1504991554f2SKenneth D. Merry 	if (pBuffer->immediate && pBuffer->owned_by_firmware &&
1505991554f2SKenneth D. Merry 	    (pBuffer->unique_id == MPR_FW_DIAG_INVALID_UID)) {
1506991554f2SKenneth D. Merry 		pBuffer->immediate = FALSE;
1507991554f2SKenneth D. Merry 		pBuffer->unique_id = unique_id;
1508991554f2SKenneth D. Merry 		return (MPR_DIAG_SUCCESS);
1509991554f2SKenneth D. Merry 	}
1510991554f2SKenneth D. Merry 
1511991554f2SKenneth D. Merry 	/*
1512991554f2SKenneth D. Merry 	 * Post a new buffer after checking if it's enabled.  The DMA buffer
1513991554f2SKenneth D. Merry 	 * that is allocated will be contiguous (nsegments = 1).
1514991554f2SKenneth D. Merry 	 */
1515991554f2SKenneth D. Merry 	if (!pBuffer->enabled) {
1516991554f2SKenneth D. Merry 		*return_code = MPR_FW_DIAG_ERROR_NO_BUFFER;
1517991554f2SKenneth D. Merry 		return (MPR_DIAG_FAILURE);
1518991554f2SKenneth D. Merry 	}
1519f5ead205SScott Long 	bus_dma_template_init(&t, sc->mpr_parent_dmat);
152074c781edSScott Long 	BUS_DMA_TEMPLATE_FILL(&t, BD_LOWADDR(BUS_SPACE_MAXADDR_32BIT),
152174c781edSScott Long 	    BD_MAXSIZE(buffer_size), BD_MAXSEGSIZE(buffer_size),
152274c781edSScott Long 	    BD_NSEGMENTS(1));
1523f5ead205SScott Long 	if (bus_dma_template_tag(&t, &sc->fw_diag_dmat)) {
1524757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR,
1525757ff642SScott Long 		    "Cannot allocate FW diag buffer DMA tag\n");
1526e2997a03SKenneth D. Merry 		*return_code = MPR_FW_DIAG_ERROR_NO_BUFFER;
1527e2997a03SKenneth D. Merry 		status = MPR_DIAG_FAILURE;
1528e2997a03SKenneth D. Merry 		goto bailout;
1529991554f2SKenneth D. Merry 	}
1530991554f2SKenneth D. Merry         if (bus_dmamem_alloc(sc->fw_diag_dmat, (void **)&sc->fw_diag_buffer,
1531991554f2SKenneth D. Merry 	    BUS_DMA_NOWAIT, &sc->fw_diag_map)) {
1532757ff642SScott Long 		mpr_dprint(sc, MPR_ERROR,
1533757ff642SScott Long 		    "Cannot allocate FW diag buffer memory\n");
1534e2997a03SKenneth D. Merry 		*return_code = MPR_FW_DIAG_ERROR_NO_BUFFER;
1535e2997a03SKenneth D. Merry 		status = MPR_DIAG_FAILURE;
1536e2997a03SKenneth D. Merry 		goto bailout;
1537991554f2SKenneth D. Merry 	}
1538991554f2SKenneth D. Merry 	bzero(sc->fw_diag_buffer, buffer_size);
1539e2997a03SKenneth D. Merry 
1540e2997a03SKenneth D. Merry 	ctx = malloc(sizeof(*ctx), M_MPR, M_WAITOK | M_ZERO);
1541e2997a03SKenneth D. Merry 	ctx->addr = &sc->fw_diag_busaddr;
1542e2997a03SKenneth D. Merry 	ctx->buffer_dmat = sc->fw_diag_dmat;
1543e2997a03SKenneth D. Merry 	ctx->buffer_dmamap = sc->fw_diag_map;
1544e2997a03SKenneth D. Merry 	ctx->softc = sc;
1545e2997a03SKenneth D. Merry 	error = bus_dmamap_load(sc->fw_diag_dmat, sc->fw_diag_map,
1546e2997a03SKenneth D. Merry 	    sc->fw_diag_buffer, buffer_size, mpr_memaddr_wait_cb,
1547e2997a03SKenneth D. Merry 	    ctx, 0);
1548e2997a03SKenneth D. Merry 	if (error == EINPROGRESS) {
1549e2997a03SKenneth D. Merry 		/* XXX KDM */
1550e2997a03SKenneth D. Merry 		device_printf(sc->mpr_dev, "%s: Deferred bus_dmamap_load\n",
1551e2997a03SKenneth D. Merry 		    __func__);
1552e2997a03SKenneth D. Merry 		/*
1553e2997a03SKenneth D. Merry 		 * Wait for the load to complete.  If we're interrupted,
1554e2997a03SKenneth D. Merry 		 * bail out.
1555e2997a03SKenneth D. Merry 		 */
1556e2997a03SKenneth D. Merry 		mpr_lock(sc);
1557e2997a03SKenneth D. Merry 		if (ctx->completed == 0) {
1558e2997a03SKenneth D. Merry 			error = msleep(ctx, &sc->mpr_mtx, PCATCH, "mprwait", 0);
1559e2997a03SKenneth D. Merry 			if (error != 0) {
1560e2997a03SKenneth D. Merry 				/*
1561e2997a03SKenneth D. Merry 				 * We got an error from msleep(9).  This is
1562e2997a03SKenneth D. Merry 				 * most likely due to a signal.  Tell
1563e2997a03SKenneth D. Merry 				 * mpr_memaddr_wait_cb() that we've abandoned
1564e2997a03SKenneth D. Merry 				 * the context, so it needs to clean up when
1565e2997a03SKenneth D. Merry 				 * it is called.
1566e2997a03SKenneth D. Merry 				 */
1567e2997a03SKenneth D. Merry 				ctx->abandoned = 1;
1568e2997a03SKenneth D. Merry 
1569e2997a03SKenneth D. Merry 				/* The callback will free this memory */
1570e2997a03SKenneth D. Merry 				ctx = NULL;
1571e2997a03SKenneth D. Merry 				mpr_unlock(sc);
1572e2997a03SKenneth D. Merry 
1573e2997a03SKenneth D. Merry 				device_printf(sc->mpr_dev, "Cannot "
1574e2997a03SKenneth D. Merry 				    "bus_dmamap_load FW diag buffer, error = "
1575e2997a03SKenneth D. Merry 				    "%d returned from msleep\n", error);
1576e2997a03SKenneth D. Merry 				*return_code = MPR_FW_DIAG_ERROR_NO_BUFFER;
1577e2997a03SKenneth D. Merry 				status = MPR_DIAG_FAILURE;
1578e2997a03SKenneth D. Merry 				goto bailout;
1579e2997a03SKenneth D. Merry 			}
1580e2997a03SKenneth D. Merry 		}
1581e2997a03SKenneth D. Merry 		mpr_unlock(sc);
1582e2997a03SKenneth D. Merry 	}
1583e2997a03SKenneth D. Merry 
1584e2997a03SKenneth D. Merry 	if ((error != 0) || (ctx->error != 0)) {
1585e2997a03SKenneth D. Merry 		device_printf(sc->mpr_dev, "Cannot bus_dmamap_load FW diag "
1586e2997a03SKenneth D. Merry 		    "buffer, %serror = %d\n", error ? "" : "callback ",
1587e2997a03SKenneth D. Merry 		    error ? error : ctx->error);
1588e2997a03SKenneth D. Merry 		*return_code = MPR_FW_DIAG_ERROR_NO_BUFFER;
1589e2997a03SKenneth D. Merry 		status = MPR_DIAG_FAILURE;
1590e2997a03SKenneth D. Merry 		goto bailout;
1591e2997a03SKenneth D. Merry 	}
1592e2997a03SKenneth D. Merry 
1593e2997a03SKenneth D. Merry 	bus_dmamap_sync(sc->fw_diag_dmat, sc->fw_diag_map, BUS_DMASYNC_PREREAD);
1594e2997a03SKenneth D. Merry 
1595991554f2SKenneth D. Merry 	pBuffer->size = buffer_size;
1596991554f2SKenneth D. Merry 
1597991554f2SKenneth D. Merry 	/*
1598991554f2SKenneth D. Merry 	 * Copy the given info to the diag buffer and post the buffer.
1599991554f2SKenneth D. Merry 	 */
1600991554f2SKenneth D. Merry 	pBuffer->buffer_type = buffer_type;
1601991554f2SKenneth D. Merry 	pBuffer->immediate = FALSE;
1602991554f2SKenneth D. Merry 	if (buffer_type == MPI2_DIAG_BUF_TYPE_TRACE) {
1603991554f2SKenneth D. Merry 		for (i = 0; i < (sizeof (pBuffer->product_specific) / 4);
1604991554f2SKenneth D. Merry 		    i++) {
1605991554f2SKenneth D. Merry 			pBuffer->product_specific[i] =
1606991554f2SKenneth D. Merry 			    diag_register->ProductSpecific[i];
1607991554f2SKenneth D. Merry 		}
1608991554f2SKenneth D. Merry 	}
1609991554f2SKenneth D. Merry 	pBuffer->extended_type = extended_type;
1610991554f2SKenneth D. Merry 	pBuffer->unique_id = unique_id;
1611991554f2SKenneth D. Merry 	status = mpr_post_fw_diag_buffer(sc, pBuffer, return_code);
1612991554f2SKenneth D. Merry 
1613e2997a03SKenneth D. Merry bailout:
1614e2997a03SKenneth D. Merry 
1615991554f2SKenneth D. Merry 	/*
1616991554f2SKenneth D. Merry 	 * In case there was a failure, free the DMA buffer.
1617991554f2SKenneth D. Merry 	 */
1618991554f2SKenneth D. Merry 	if (status == MPR_DIAG_FAILURE) {
1619e2997a03SKenneth D. Merry 		if (sc->fw_diag_busaddr != 0) {
1620991554f2SKenneth D. Merry 			bus_dmamap_unload(sc->fw_diag_dmat, sc->fw_diag_map);
1621e2997a03SKenneth D. Merry 			sc->fw_diag_busaddr = 0;
1622e2997a03SKenneth D. Merry 		}
1623e2997a03SKenneth D. Merry 		if (sc->fw_diag_buffer != NULL) {
1624991554f2SKenneth D. Merry 			bus_dmamem_free(sc->fw_diag_dmat, sc->fw_diag_buffer,
1625991554f2SKenneth D. Merry 			    sc->fw_diag_map);
1626e2997a03SKenneth D. Merry 			sc->fw_diag_buffer = NULL;
1627991554f2SKenneth D. Merry 		}
1628e2997a03SKenneth D. Merry 		if (sc->fw_diag_dmat != NULL) {
1629e2997a03SKenneth D. Merry 			bus_dma_tag_destroy(sc->fw_diag_dmat);
1630e2997a03SKenneth D. Merry 			sc->fw_diag_dmat = NULL;
1631e2997a03SKenneth D. Merry 		}
1632e2997a03SKenneth D. Merry 	}
1633e2997a03SKenneth D. Merry 
1634e2997a03SKenneth D. Merry 	if (ctx != NULL)
1635e2997a03SKenneth D. Merry 		free(ctx, M_MPR);
1636991554f2SKenneth D. Merry 
1637991554f2SKenneth D. Merry 	return (status);
1638991554f2SKenneth D. Merry }
1639991554f2SKenneth D. Merry 
1640991554f2SKenneth D. Merry static int
mpr_diag_unregister(struct mpr_softc * sc,mpr_fw_diag_unregister_t * diag_unregister,uint32_t * return_code)1641991554f2SKenneth D. Merry mpr_diag_unregister(struct mpr_softc *sc,
1642991554f2SKenneth D. Merry     mpr_fw_diag_unregister_t *diag_unregister, uint32_t *return_code)
1643991554f2SKenneth D. Merry {
1644991554f2SKenneth D. Merry 	mpr_fw_diagnostic_buffer_t	*pBuffer;
1645991554f2SKenneth D. Merry 	uint8_t				i;
1646991554f2SKenneth D. Merry 	uint32_t			unique_id;
1647991554f2SKenneth D. Merry 	int				status;
1648991554f2SKenneth D. Merry 
1649991554f2SKenneth D. Merry 	unique_id = diag_unregister->UniqueId;
1650991554f2SKenneth D. Merry 
1651991554f2SKenneth D. Merry 	/*
1652991554f2SKenneth D. Merry 	 * Get the current buffer and look up the unique ID.  The unique ID
1653991554f2SKenneth D. Merry 	 * should be there.
1654991554f2SKenneth D. Merry 	 */
1655991554f2SKenneth D. Merry 	i = mpr_get_fw_diag_buffer_number(sc, unique_id);
1656991554f2SKenneth D. Merry 	if (i == MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1657991554f2SKenneth D. Merry 		*return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1658991554f2SKenneth D. Merry 		return (MPR_DIAG_FAILURE);
1659991554f2SKenneth D. Merry 	}
1660991554f2SKenneth D. Merry 
1661991554f2SKenneth D. Merry 	pBuffer = &sc->fw_diag_buffer_list[i];
1662991554f2SKenneth D. Merry 
1663991554f2SKenneth D. Merry 	/*
1664991554f2SKenneth D. Merry 	 * Try to release the buffer from FW before freeing it.  If release
1665991554f2SKenneth D. Merry 	 * fails, don't free the DMA buffer in case FW tries to access it
1666991554f2SKenneth D. Merry 	 * later.  If buffer is not owned by firmware, can't release it.
1667991554f2SKenneth D. Merry 	 */
1668991554f2SKenneth D. Merry 	if (!pBuffer->owned_by_firmware) {
1669991554f2SKenneth D. Merry 		status = MPR_DIAG_SUCCESS;
1670991554f2SKenneth D. Merry 	} else {
1671991554f2SKenneth D. Merry 		status = mpr_release_fw_diag_buffer(sc, pBuffer, return_code,
1672991554f2SKenneth D. Merry 		    MPR_FW_DIAG_TYPE_UNREGISTER);
1673991554f2SKenneth D. Merry 	}
1674991554f2SKenneth D. Merry 
1675991554f2SKenneth D. Merry 	/*
1676991554f2SKenneth D. Merry 	 * At this point, return the current status no matter what happens with
1677991554f2SKenneth D. Merry 	 * the DMA buffer.
1678991554f2SKenneth D. Merry 	 */
1679991554f2SKenneth D. Merry 	pBuffer->unique_id = MPR_FW_DIAG_INVALID_UID;
1680991554f2SKenneth D. Merry 	if (status == MPR_DIAG_SUCCESS) {
1681e2997a03SKenneth D. Merry 		if (sc->fw_diag_busaddr != 0) {
1682991554f2SKenneth D. Merry 			bus_dmamap_unload(sc->fw_diag_dmat, sc->fw_diag_map);
1683e2997a03SKenneth D. Merry 			sc->fw_diag_busaddr = 0;
1684e2997a03SKenneth D. Merry 		}
1685e2997a03SKenneth D. Merry 		if (sc->fw_diag_buffer != NULL) {
1686991554f2SKenneth D. Merry 			bus_dmamem_free(sc->fw_diag_dmat, sc->fw_diag_buffer,
1687991554f2SKenneth D. Merry 			    sc->fw_diag_map);
1688e2997a03SKenneth D. Merry 			sc->fw_diag_buffer = NULL;
1689e2997a03SKenneth D. Merry 		}
1690e2997a03SKenneth D. Merry 		if (sc->fw_diag_dmat != NULL) {
1691991554f2SKenneth D. Merry 			bus_dma_tag_destroy(sc->fw_diag_dmat);
1692e2997a03SKenneth D. Merry 			sc->fw_diag_dmat = NULL;
1693e2997a03SKenneth D. Merry 		}
1694991554f2SKenneth D. Merry 	}
1695991554f2SKenneth D. Merry 
1696991554f2SKenneth D. Merry 	return (status);
1697991554f2SKenneth D. Merry }
1698991554f2SKenneth D. Merry 
1699991554f2SKenneth D. Merry static int
mpr_diag_query(struct mpr_softc * sc,mpr_fw_diag_query_t * diag_query,uint32_t * return_code)1700991554f2SKenneth D. Merry mpr_diag_query(struct mpr_softc *sc, mpr_fw_diag_query_t *diag_query,
1701991554f2SKenneth D. Merry     uint32_t *return_code)
1702991554f2SKenneth D. Merry {
1703991554f2SKenneth D. Merry 	mpr_fw_diagnostic_buffer_t	*pBuffer;
1704991554f2SKenneth D. Merry 	uint8_t				i;
1705991554f2SKenneth D. Merry 	uint32_t			unique_id;
1706991554f2SKenneth D. Merry 
1707991554f2SKenneth D. Merry 	unique_id = diag_query->UniqueId;
1708991554f2SKenneth D. Merry 
1709991554f2SKenneth D. Merry 	/*
1710991554f2SKenneth D. Merry 	 * If ID is valid, query on ID.
1711991554f2SKenneth D. Merry 	 * If ID is invalid, query on buffer type.
1712991554f2SKenneth D. Merry 	 */
1713991554f2SKenneth D. Merry 	if (unique_id == MPR_FW_DIAG_INVALID_UID) {
1714991554f2SKenneth D. Merry 		i = diag_query->BufferType;
1715991554f2SKenneth D. Merry 		if (i >= MPI2_DIAG_BUF_TYPE_COUNT) {
1716991554f2SKenneth D. Merry 			*return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1717991554f2SKenneth D. Merry 			return (MPR_DIAG_FAILURE);
1718991554f2SKenneth D. Merry 		}
1719991554f2SKenneth D. Merry 	} else {
1720991554f2SKenneth D. Merry 		i = mpr_get_fw_diag_buffer_number(sc, unique_id);
1721991554f2SKenneth D. Merry 		if (i == MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1722991554f2SKenneth D. Merry 			*return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1723991554f2SKenneth D. Merry 			return (MPR_DIAG_FAILURE);
1724991554f2SKenneth D. Merry 		}
1725991554f2SKenneth D. Merry 	}
1726991554f2SKenneth D. Merry 
1727991554f2SKenneth D. Merry 	/*
1728991554f2SKenneth D. Merry 	 * Fill query structure with the diag buffer info.
1729991554f2SKenneth D. Merry 	 */
1730991554f2SKenneth D. Merry 	pBuffer = &sc->fw_diag_buffer_list[i];
1731991554f2SKenneth D. Merry 	diag_query->BufferType = pBuffer->buffer_type;
1732991554f2SKenneth D. Merry 	diag_query->ExtendedType = pBuffer->extended_type;
1733991554f2SKenneth D. Merry 	if (diag_query->BufferType == MPI2_DIAG_BUF_TYPE_TRACE) {
1734991554f2SKenneth D. Merry 		for (i = 0; i < (sizeof(diag_query->ProductSpecific) / 4);
1735991554f2SKenneth D. Merry 		    i++) {
1736991554f2SKenneth D. Merry 			diag_query->ProductSpecific[i] =
1737991554f2SKenneth D. Merry 			    pBuffer->product_specific[i];
1738991554f2SKenneth D. Merry 		}
1739991554f2SKenneth D. Merry 	}
1740991554f2SKenneth D. Merry 	diag_query->TotalBufferSize = pBuffer->size;
1741991554f2SKenneth D. Merry 	diag_query->DriverAddedBufferSize = 0;
1742991554f2SKenneth D. Merry 	diag_query->UniqueId = pBuffer->unique_id;
1743991554f2SKenneth D. Merry 	diag_query->ApplicationFlags = 0;
1744991554f2SKenneth D. Merry 	diag_query->DiagnosticFlags = 0;
1745991554f2SKenneth D. Merry 
1746991554f2SKenneth D. Merry 	/*
1747991554f2SKenneth D. Merry 	 * Set/Clear application flags
1748991554f2SKenneth D. Merry 	 */
1749991554f2SKenneth D. Merry 	if (pBuffer->immediate) {
1750991554f2SKenneth D. Merry 		diag_query->ApplicationFlags &= ~MPR_FW_DIAG_FLAG_APP_OWNED;
1751991554f2SKenneth D. Merry 	} else {
1752991554f2SKenneth D. Merry 		diag_query->ApplicationFlags |= MPR_FW_DIAG_FLAG_APP_OWNED;
1753991554f2SKenneth D. Merry 	}
1754991554f2SKenneth D. Merry 	if (pBuffer->valid_data || pBuffer->owned_by_firmware) {
1755991554f2SKenneth D. Merry 		diag_query->ApplicationFlags |= MPR_FW_DIAG_FLAG_BUFFER_VALID;
1756991554f2SKenneth D. Merry 	} else {
1757991554f2SKenneth D. Merry 		diag_query->ApplicationFlags &= ~MPR_FW_DIAG_FLAG_BUFFER_VALID;
1758991554f2SKenneth D. Merry 	}
1759991554f2SKenneth D. Merry 	if (pBuffer->owned_by_firmware) {
1760991554f2SKenneth D. Merry 		diag_query->ApplicationFlags |=
1761991554f2SKenneth D. Merry 		    MPR_FW_DIAG_FLAG_FW_BUFFER_ACCESS;
1762991554f2SKenneth D. Merry 	} else {
1763991554f2SKenneth D. Merry 		diag_query->ApplicationFlags &=
1764991554f2SKenneth D. Merry 		    ~MPR_FW_DIAG_FLAG_FW_BUFFER_ACCESS;
1765991554f2SKenneth D. Merry 	}
1766991554f2SKenneth D. Merry 
1767991554f2SKenneth D. Merry 	return (MPR_DIAG_SUCCESS);
1768991554f2SKenneth D. Merry }
1769991554f2SKenneth D. Merry 
1770991554f2SKenneth D. Merry static int
mpr_diag_read_buffer(struct mpr_softc * sc,mpr_diag_read_buffer_t * diag_read_buffer,uint8_t * ioctl_buf,uint32_t * return_code)1771991554f2SKenneth D. Merry mpr_diag_read_buffer(struct mpr_softc *sc,
1772991554f2SKenneth D. Merry     mpr_diag_read_buffer_t *diag_read_buffer, uint8_t *ioctl_buf,
1773991554f2SKenneth D. Merry     uint32_t *return_code)
1774991554f2SKenneth D. Merry {
1775991554f2SKenneth D. Merry 	mpr_fw_diagnostic_buffer_t	*pBuffer;
1776991554f2SKenneth D. Merry 	uint8_t				i, *pData;
1777991554f2SKenneth D. Merry 	uint32_t			unique_id;
1778991554f2SKenneth D. Merry 	int				status;
1779991554f2SKenneth D. Merry 
1780991554f2SKenneth D. Merry 	unique_id = diag_read_buffer->UniqueId;
1781991554f2SKenneth D. Merry 
1782991554f2SKenneth D. Merry 	/*
1783991554f2SKenneth D. Merry 	 * Get the current buffer and look up the unique ID.  The unique ID
1784991554f2SKenneth D. Merry 	 * should be there.
1785991554f2SKenneth D. Merry 	 */
1786991554f2SKenneth D. Merry 	i = mpr_get_fw_diag_buffer_number(sc, unique_id);
1787991554f2SKenneth D. Merry 	if (i == MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1788991554f2SKenneth D. Merry 		*return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1789991554f2SKenneth D. Merry 		return (MPR_DIAG_FAILURE);
1790991554f2SKenneth D. Merry 	}
1791991554f2SKenneth D. Merry 
1792991554f2SKenneth D. Merry 	pBuffer = &sc->fw_diag_buffer_list[i];
1793991554f2SKenneth D. Merry 
1794991554f2SKenneth D. Merry 	/*
1795991554f2SKenneth D. Merry 	 * Make sure requested read is within limits
1796991554f2SKenneth D. Merry 	 */
1797991554f2SKenneth D. Merry 	if (diag_read_buffer->StartingOffset + diag_read_buffer->BytesToRead >
1798991554f2SKenneth D. Merry 	    pBuffer->size) {
1799991554f2SKenneth D. Merry 		*return_code = MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1800991554f2SKenneth D. Merry 		return (MPR_DIAG_FAILURE);
1801991554f2SKenneth D. Merry 	}
1802991554f2SKenneth D. Merry 
1803e2997a03SKenneth D. Merry 	/* Sync the DMA map before we copy to userland. */
1804e2997a03SKenneth D. Merry 	bus_dmamap_sync(sc->fw_diag_dmat, sc->fw_diag_map,
1805e2997a03SKenneth D. Merry 	    BUS_DMASYNC_POSTREAD);
1806e2997a03SKenneth D. Merry 
1807991554f2SKenneth D. Merry 	/*
1808991554f2SKenneth D. Merry 	 * Copy the requested data from DMA to the diag_read_buffer.  The DMA
1809991554f2SKenneth D. Merry 	 * buffer that was allocated is one contiguous buffer.
1810991554f2SKenneth D. Merry 	 */
1811991554f2SKenneth D. Merry 	pData = (uint8_t *)(sc->fw_diag_buffer +
1812991554f2SKenneth D. Merry 	    diag_read_buffer->StartingOffset);
1813991554f2SKenneth D. Merry 	if (copyout(pData, ioctl_buf, diag_read_buffer->BytesToRead) != 0)
1814991554f2SKenneth D. Merry 		return (MPR_DIAG_FAILURE);
1815991554f2SKenneth D. Merry 	diag_read_buffer->Status = 0;
1816991554f2SKenneth D. Merry 
1817991554f2SKenneth D. Merry 	/*
1818991554f2SKenneth D. Merry 	 * Set or clear the Force Release flag.
1819991554f2SKenneth D. Merry 	 */
1820991554f2SKenneth D. Merry 	if (pBuffer->force_release) {
1821991554f2SKenneth D. Merry 		diag_read_buffer->Flags |= MPR_FW_DIAG_FLAG_FORCE_RELEASE;
1822991554f2SKenneth D. Merry 	} else {
1823991554f2SKenneth D. Merry 		diag_read_buffer->Flags &= ~MPR_FW_DIAG_FLAG_FORCE_RELEASE;
1824991554f2SKenneth D. Merry 	}
1825991554f2SKenneth D. Merry 
1826991554f2SKenneth D. Merry 	/*
1827991554f2SKenneth D. Merry 	 * If buffer is to be reregistered, make sure it's not already owned by
1828991554f2SKenneth D. Merry 	 * firmware first.
1829991554f2SKenneth D. Merry 	 */
1830991554f2SKenneth D. Merry 	status = MPR_DIAG_SUCCESS;
1831991554f2SKenneth D. Merry 	if (!pBuffer->owned_by_firmware) {
1832991554f2SKenneth D. Merry 		if (diag_read_buffer->Flags & MPR_FW_DIAG_FLAG_REREGISTER) {
1833991554f2SKenneth D. Merry 			status = mpr_post_fw_diag_buffer(sc, pBuffer,
1834991554f2SKenneth D. Merry 			    return_code);
1835991554f2SKenneth D. Merry 		}
1836991554f2SKenneth D. Merry 	}
1837991554f2SKenneth D. Merry 
1838991554f2SKenneth D. Merry 	return (status);
1839991554f2SKenneth D. Merry }
1840991554f2SKenneth D. Merry 
1841991554f2SKenneth D. Merry static int
mpr_diag_release(struct mpr_softc * sc,mpr_fw_diag_release_t * diag_release,uint32_t * return_code)1842991554f2SKenneth D. Merry mpr_diag_release(struct mpr_softc *sc, mpr_fw_diag_release_t *diag_release,
1843991554f2SKenneth D. Merry     uint32_t *return_code)
1844991554f2SKenneth D. Merry {
1845991554f2SKenneth D. Merry 	mpr_fw_diagnostic_buffer_t	*pBuffer;
1846991554f2SKenneth D. Merry 	uint8_t				i;
1847991554f2SKenneth D. Merry 	uint32_t			unique_id;
1848991554f2SKenneth D. Merry 	int				status;
1849991554f2SKenneth D. Merry 
1850991554f2SKenneth D. Merry 	unique_id = diag_release->UniqueId;
1851991554f2SKenneth D. Merry 
1852991554f2SKenneth D. Merry 	/*
1853991554f2SKenneth D. Merry 	 * Get the current buffer and look up the unique ID.  The unique ID
1854991554f2SKenneth D. Merry 	 * should be there.
1855991554f2SKenneth D. Merry 	 */
1856991554f2SKenneth D. Merry 	i = mpr_get_fw_diag_buffer_number(sc, unique_id);
1857991554f2SKenneth D. Merry 	if (i == MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1858991554f2SKenneth D. Merry 		*return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1859991554f2SKenneth D. Merry 		return (MPR_DIAG_FAILURE);
1860991554f2SKenneth D. Merry 	}
1861991554f2SKenneth D. Merry 
1862991554f2SKenneth D. Merry 	pBuffer = &sc->fw_diag_buffer_list[i];
1863991554f2SKenneth D. Merry 
1864991554f2SKenneth D. Merry 	/*
1865991554f2SKenneth D. Merry 	 * If buffer is not owned by firmware, it's already been released.
1866991554f2SKenneth D. Merry 	 */
1867991554f2SKenneth D. Merry 	if (!pBuffer->owned_by_firmware) {
1868991554f2SKenneth D. Merry 		*return_code = MPR_FW_DIAG_ERROR_ALREADY_RELEASED;
1869991554f2SKenneth D. Merry 		return (MPR_DIAG_FAILURE);
1870991554f2SKenneth D. Merry 	}
1871991554f2SKenneth D. Merry 
1872991554f2SKenneth D. Merry 	/*
1873991554f2SKenneth D. Merry 	 * Release the buffer.
1874991554f2SKenneth D. Merry 	 */
1875991554f2SKenneth D. Merry 	status = mpr_release_fw_diag_buffer(sc, pBuffer, return_code,
1876991554f2SKenneth D. Merry 	    MPR_FW_DIAG_TYPE_RELEASE);
1877991554f2SKenneth D. Merry 	return (status);
1878991554f2SKenneth D. Merry }
1879991554f2SKenneth D. Merry 
1880991554f2SKenneth D. Merry static int
mpr_do_diag_action(struct mpr_softc * sc,uint32_t action,uint8_t * diag_action,uint32_t length,uint32_t * return_code)18817a2a6a1aSStephen McConnell mpr_do_diag_action(struct mpr_softc *sc, uint32_t action, uint8_t *diag_action,
18827a2a6a1aSStephen McConnell     uint32_t length, uint32_t *return_code)
1883991554f2SKenneth D. Merry {
1884991554f2SKenneth D. Merry 	mpr_fw_diag_register_t		diag_register;
1885991554f2SKenneth D. Merry 	mpr_fw_diag_unregister_t	diag_unregister;
1886991554f2SKenneth D. Merry 	mpr_fw_diag_query_t		diag_query;
1887991554f2SKenneth D. Merry 	mpr_diag_read_buffer_t		diag_read_buffer;
1888991554f2SKenneth D. Merry 	mpr_fw_diag_release_t		diag_release;
1889991554f2SKenneth D. Merry 	int				status = MPR_DIAG_SUCCESS;
1890991554f2SKenneth D. Merry 	uint32_t			original_return_code;
1891991554f2SKenneth D. Merry 
1892991554f2SKenneth D. Merry 	original_return_code = *return_code;
1893991554f2SKenneth D. Merry 	*return_code = MPR_FW_DIAG_ERROR_SUCCESS;
1894991554f2SKenneth D. Merry 
1895991554f2SKenneth D. Merry 	switch (action) {
1896991554f2SKenneth D. Merry 		case MPR_FW_DIAG_TYPE_REGISTER:
1897991554f2SKenneth D. Merry 			if (!length) {
1898991554f2SKenneth D. Merry 				*return_code =
1899991554f2SKenneth D. Merry 				    MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1900991554f2SKenneth D. Merry 				status = MPR_DIAG_FAILURE;
1901991554f2SKenneth D. Merry 				break;
1902991554f2SKenneth D. Merry 			}
1903991554f2SKenneth D. Merry 			if (copyin(diag_action, &diag_register,
1904991554f2SKenneth D. Merry 			    sizeof(diag_register)) != 0)
1905991554f2SKenneth D. Merry 				return (MPR_DIAG_FAILURE);
1906991554f2SKenneth D. Merry 			status = mpr_diag_register(sc, &diag_register,
1907991554f2SKenneth D. Merry 			    return_code);
1908991554f2SKenneth D. Merry 			break;
1909991554f2SKenneth D. Merry 
1910991554f2SKenneth D. Merry 		case MPR_FW_DIAG_TYPE_UNREGISTER:
1911991554f2SKenneth D. Merry 			if (length < sizeof(diag_unregister)) {
1912991554f2SKenneth D. Merry 				*return_code =
1913991554f2SKenneth D. Merry 				    MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1914991554f2SKenneth D. Merry 				status = MPR_DIAG_FAILURE;
1915991554f2SKenneth D. Merry 				break;
1916991554f2SKenneth D. Merry 			}
1917991554f2SKenneth D. Merry 			if (copyin(diag_action, &diag_unregister,
1918991554f2SKenneth D. Merry 			    sizeof(diag_unregister)) != 0)
1919991554f2SKenneth D. Merry 				return (MPR_DIAG_FAILURE);
1920991554f2SKenneth D. Merry 			status = mpr_diag_unregister(sc, &diag_unregister,
1921991554f2SKenneth D. Merry 			    return_code);
1922991554f2SKenneth D. Merry 			break;
1923991554f2SKenneth D. Merry 
1924991554f2SKenneth D. Merry 		case MPR_FW_DIAG_TYPE_QUERY:
1925991554f2SKenneth D. Merry 			if (length < sizeof (diag_query)) {
1926991554f2SKenneth D. Merry 				*return_code =
1927991554f2SKenneth D. Merry 				    MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1928991554f2SKenneth D. Merry 				status = MPR_DIAG_FAILURE;
1929991554f2SKenneth D. Merry 				break;
1930991554f2SKenneth D. Merry 			}
1931991554f2SKenneth D. Merry 			if (copyin(diag_action, &diag_query, sizeof(diag_query))
1932991554f2SKenneth D. Merry 			    != 0)
1933991554f2SKenneth D. Merry 				return (MPR_DIAG_FAILURE);
1934991554f2SKenneth D. Merry 			status = mpr_diag_query(sc, &diag_query, return_code);
1935991554f2SKenneth D. Merry 			if (status == MPR_DIAG_SUCCESS)
1936991554f2SKenneth D. Merry 				if (copyout(&diag_query, diag_action,
1937991554f2SKenneth D. Merry 				    sizeof (diag_query)) != 0)
1938991554f2SKenneth D. Merry 					return (MPR_DIAG_FAILURE);
1939991554f2SKenneth D. Merry 			break;
1940991554f2SKenneth D. Merry 
1941991554f2SKenneth D. Merry 		case MPR_FW_DIAG_TYPE_READ_BUFFER:
1942991554f2SKenneth D. Merry 			if (copyin(diag_action, &diag_read_buffer,
1943991554f2SKenneth D. Merry 			    sizeof(diag_read_buffer)) != 0)
1944991554f2SKenneth D. Merry 				return (MPR_DIAG_FAILURE);
1945991554f2SKenneth D. Merry 			if (length < diag_read_buffer.BytesToRead) {
1946991554f2SKenneth D. Merry 				*return_code =
1947991554f2SKenneth D. Merry 				    MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1948991554f2SKenneth D. Merry 				status = MPR_DIAG_FAILURE;
1949991554f2SKenneth D. Merry 				break;
1950991554f2SKenneth D. Merry 			}
1951991554f2SKenneth D. Merry 			status = mpr_diag_read_buffer(sc, &diag_read_buffer,
1952991554f2SKenneth D. Merry 			    PTRIN(diag_read_buffer.PtrDataBuffer),
1953991554f2SKenneth D. Merry 			    return_code);
1954991554f2SKenneth D. Merry 			if (status == MPR_DIAG_SUCCESS) {
1955991554f2SKenneth D. Merry 				if (copyout(&diag_read_buffer, diag_action,
1956991554f2SKenneth D. Merry 				    sizeof(diag_read_buffer) -
1957991554f2SKenneth D. Merry 				    sizeof(diag_read_buffer.PtrDataBuffer)) !=
1958991554f2SKenneth D. Merry 				    0)
1959991554f2SKenneth D. Merry 					return (MPR_DIAG_FAILURE);
1960991554f2SKenneth D. Merry 			}
1961991554f2SKenneth D. Merry 			break;
1962991554f2SKenneth D. Merry 
1963991554f2SKenneth D. Merry 		case MPR_FW_DIAG_TYPE_RELEASE:
1964991554f2SKenneth D. Merry 			if (length < sizeof(diag_release)) {
1965991554f2SKenneth D. Merry 				*return_code =
1966991554f2SKenneth D. Merry 				    MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1967991554f2SKenneth D. Merry 				status = MPR_DIAG_FAILURE;
1968991554f2SKenneth D. Merry 				break;
1969991554f2SKenneth D. Merry 			}
1970991554f2SKenneth D. Merry 			if (copyin(diag_action, &diag_release,
1971991554f2SKenneth D. Merry 			    sizeof(diag_release)) != 0)
1972991554f2SKenneth D. Merry 				return (MPR_DIAG_FAILURE);
1973991554f2SKenneth D. Merry 			status = mpr_diag_release(sc, &diag_release,
1974991554f2SKenneth D. Merry 			    return_code);
1975991554f2SKenneth D. Merry 			break;
1976991554f2SKenneth D. Merry 
1977991554f2SKenneth D. Merry 		default:
1978991554f2SKenneth D. Merry 			*return_code = MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1979991554f2SKenneth D. Merry 			status = MPR_DIAG_FAILURE;
1980991554f2SKenneth D. Merry 			break;
1981991554f2SKenneth D. Merry 	}
1982991554f2SKenneth D. Merry 
1983991554f2SKenneth D. Merry 	if ((status == MPR_DIAG_FAILURE) &&
1984991554f2SKenneth D. Merry 	    (original_return_code == MPR_FW_DIAG_NEW) &&
1985991554f2SKenneth D. Merry 	    (*return_code != MPR_FW_DIAG_ERROR_SUCCESS))
1986991554f2SKenneth D. Merry 		status = MPR_DIAG_SUCCESS;
1987991554f2SKenneth D. Merry 
1988991554f2SKenneth D. Merry 	return (status);
1989991554f2SKenneth D. Merry }
1990991554f2SKenneth D. Merry 
1991991554f2SKenneth D. Merry static int
mpr_user_diag_action(struct mpr_softc * sc,mpr_diag_action_t * data)1992991554f2SKenneth D. Merry mpr_user_diag_action(struct mpr_softc *sc, mpr_diag_action_t *data)
1993991554f2SKenneth D. Merry {
1994991554f2SKenneth D. Merry 	int			status;
1995991554f2SKenneth D. Merry 
1996991554f2SKenneth D. Merry 	/*
1997991554f2SKenneth D. Merry 	 * Only allow one diag action at one time.
1998991554f2SKenneth D. Merry 	 */
1999991554f2SKenneth D. Merry 	if (sc->mpr_flags & MPR_FLAGS_BUSY) {
2000991554f2SKenneth D. Merry 		mpr_dprint(sc, MPR_USER, "%s: Only one FW diag command "
2001991554f2SKenneth D. Merry 		    "allowed at a single time.", __func__);
2002991554f2SKenneth D. Merry 		return (EBUSY);
2003991554f2SKenneth D. Merry 	}
2004991554f2SKenneth D. Merry 	sc->mpr_flags |= MPR_FLAGS_BUSY;
2005991554f2SKenneth D. Merry 
2006991554f2SKenneth D. Merry 	/*
2007991554f2SKenneth D. Merry 	 * Send diag action request
2008991554f2SKenneth D. Merry 	 */
2009991554f2SKenneth D. Merry 	if (data->Action == MPR_FW_DIAG_TYPE_REGISTER ||
2010991554f2SKenneth D. Merry 	    data->Action == MPR_FW_DIAG_TYPE_UNREGISTER ||
2011991554f2SKenneth D. Merry 	    data->Action == MPR_FW_DIAG_TYPE_QUERY ||
2012991554f2SKenneth D. Merry 	    data->Action == MPR_FW_DIAG_TYPE_READ_BUFFER ||
2013991554f2SKenneth D. Merry 	    data->Action == MPR_FW_DIAG_TYPE_RELEASE) {
2014991554f2SKenneth D. Merry 		status = mpr_do_diag_action(sc, data->Action,
2015991554f2SKenneth D. Merry 		    PTRIN(data->PtrDiagAction), data->Length,
2016991554f2SKenneth D. Merry 		    &data->ReturnCode);
2017991554f2SKenneth D. Merry 	} else
2018991554f2SKenneth D. Merry 		status = EINVAL;
2019991554f2SKenneth D. Merry 
2020991554f2SKenneth D. Merry 	sc->mpr_flags &= ~MPR_FLAGS_BUSY;
2021991554f2SKenneth D. Merry 	return (status);
2022991554f2SKenneth D. Merry }
2023991554f2SKenneth D. Merry 
2024991554f2SKenneth D. Merry /*
2025991554f2SKenneth D. Merry  * Copy the event recording mask and the event queue size out.  For
2026991554f2SKenneth D. Merry  * clarification, the event recording mask (events_to_record) is not the same
2027991554f2SKenneth D. Merry  * thing as the event mask (event_mask).  events_to_record has a bit set for
2028991554f2SKenneth D. Merry  * every event type that is to be recorded by the driver, and event_mask has a
2029991554f2SKenneth D. Merry  * bit cleared for every event that is allowed into the driver from the IOC.
2030991554f2SKenneth D. Merry  * They really have nothing to do with each other.
2031991554f2SKenneth D. Merry  */
2032991554f2SKenneth D. Merry static void
mpr_user_event_query(struct mpr_softc * sc,mpr_event_query_t * data)2033991554f2SKenneth D. Merry mpr_user_event_query(struct mpr_softc *sc, mpr_event_query_t *data)
2034991554f2SKenneth D. Merry {
2035991554f2SKenneth D. Merry 	uint8_t	i;
2036991554f2SKenneth D. Merry 
2037991554f2SKenneth D. Merry 	mpr_lock(sc);
2038991554f2SKenneth D. Merry 	data->Entries = MPR_EVENT_QUEUE_SIZE;
2039991554f2SKenneth D. Merry 
2040991554f2SKenneth D. Merry 	for (i = 0; i < 4; i++) {
2041991554f2SKenneth D. Merry 		data->Types[i] = sc->events_to_record[i];
2042991554f2SKenneth D. Merry 	}
2043991554f2SKenneth D. Merry 	mpr_unlock(sc);
2044991554f2SKenneth D. Merry }
2045991554f2SKenneth D. Merry 
2046991554f2SKenneth D. Merry /*
2047991554f2SKenneth D. Merry  * Set the driver's event mask according to what's been given.  See
2048991554f2SKenneth D. Merry  * mpr_user_event_query for explanation of the event recording mask and the IOC
2049991554f2SKenneth D. Merry  * event mask.  It's the app's responsibility to enable event logging by setting
2050991554f2SKenneth D. Merry  * the bits in events_to_record.  Initially, no events will be logged.
2051991554f2SKenneth D. Merry  */
2052991554f2SKenneth D. Merry static void
mpr_user_event_enable(struct mpr_softc * sc,mpr_event_enable_t * data)2053991554f2SKenneth D. Merry mpr_user_event_enable(struct mpr_softc *sc, mpr_event_enable_t *data)
2054991554f2SKenneth D. Merry {
2055991554f2SKenneth D. Merry 	uint8_t	i;
2056991554f2SKenneth D. Merry 
2057991554f2SKenneth D. Merry 	mpr_lock(sc);
2058991554f2SKenneth D. Merry 	for (i = 0; i < 4; i++) {
2059991554f2SKenneth D. Merry 		sc->events_to_record[i] = data->Types[i];
2060991554f2SKenneth D. Merry 	}
2061991554f2SKenneth D. Merry 	mpr_unlock(sc);
2062991554f2SKenneth D. Merry }
2063991554f2SKenneth D. Merry 
2064991554f2SKenneth D. Merry /*
2065991554f2SKenneth D. Merry  * Copy out the events that have been recorded, up to the max events allowed.
2066991554f2SKenneth D. Merry  */
2067991554f2SKenneth D. Merry static int
mpr_user_event_report(struct mpr_softc * sc,mpr_event_report_t * data)2068991554f2SKenneth D. Merry mpr_user_event_report(struct mpr_softc *sc, mpr_event_report_t *data)
2069991554f2SKenneth D. Merry {
2070991554f2SKenneth D. Merry 	int		status = 0;
2071991554f2SKenneth D. Merry 	uint32_t	size;
2072991554f2SKenneth D. Merry 
2073991554f2SKenneth D. Merry 	mpr_lock(sc);
2074991554f2SKenneth D. Merry 	size = data->Size;
2075991554f2SKenneth D. Merry 	if ((size >= sizeof(sc->recorded_events)) && (status == 0)) {
2076991554f2SKenneth D. Merry 		mpr_unlock(sc);
2077991554f2SKenneth D. Merry 		if (copyout((void *)sc->recorded_events,
207872aad3f9SAlan Somers 		    PTRIN(data->PtrEvents), sizeof(sc->recorded_events)) != 0)
2079991554f2SKenneth D. Merry 			status = EFAULT;
2080991554f2SKenneth D. Merry 		mpr_lock(sc);
2081991554f2SKenneth D. Merry 	} else {
2082991554f2SKenneth D. Merry 		/*
2083991554f2SKenneth D. Merry 		 * data->Size value is not large enough to copy event data.
2084991554f2SKenneth D. Merry 		 */
2085991554f2SKenneth D. Merry 		status = EFAULT;
2086991554f2SKenneth D. Merry 	}
2087991554f2SKenneth D. Merry 
2088991554f2SKenneth D. Merry 	/*
2089991554f2SKenneth D. Merry 	 * Change size value to match the number of bytes that were copied.
2090991554f2SKenneth D. Merry 	 */
2091991554f2SKenneth D. Merry 	if (status == 0)
2092991554f2SKenneth D. Merry 		data->Size = sizeof(sc->recorded_events);
2093991554f2SKenneth D. Merry 	mpr_unlock(sc);
2094991554f2SKenneth D. Merry 
2095991554f2SKenneth D. Merry 	return (status);
2096991554f2SKenneth D. Merry }
2097991554f2SKenneth D. Merry 
2098991554f2SKenneth D. Merry /*
2099991554f2SKenneth D. Merry  * Record events into the driver from the IOC if they are not masked.
2100991554f2SKenneth D. Merry  */
2101991554f2SKenneth D. Merry void
mprsas_record_event(struct mpr_softc * sc,MPI2_EVENT_NOTIFICATION_REPLY * event_reply)2102991554f2SKenneth D. Merry mprsas_record_event(struct mpr_softc *sc,
2103991554f2SKenneth D. Merry     MPI2_EVENT_NOTIFICATION_REPLY *event_reply)
2104991554f2SKenneth D. Merry {
2105991554f2SKenneth D. Merry 	uint32_t	event;
2106991554f2SKenneth D. Merry 	int		i, j;
2107991554f2SKenneth D. Merry 	uint16_t	event_data_len;
2108991554f2SKenneth D. Merry 	boolean_t	sendAEN = FALSE;
2109991554f2SKenneth D. Merry 
2110991554f2SKenneth D. Merry 	event = event_reply->Event;
2111991554f2SKenneth D. Merry 
2112991554f2SKenneth D. Merry 	/*
2113991554f2SKenneth D. Merry 	 * Generate a system event to let anyone who cares know that a
2114991554f2SKenneth D. Merry 	 * LOG_ENTRY_ADDED event has occurred.  This is sent no matter what the
2115991554f2SKenneth D. Merry 	 * event mask is set to.
2116991554f2SKenneth D. Merry 	 */
2117991554f2SKenneth D. Merry 	if (event == MPI2_EVENT_LOG_ENTRY_ADDED) {
2118991554f2SKenneth D. Merry 		sendAEN = TRUE;
2119991554f2SKenneth D. Merry 	}
2120991554f2SKenneth D. Merry 
2121991554f2SKenneth D. Merry 	/*
2122991554f2SKenneth D. Merry 	 * Record the event only if its corresponding bit is set in
2123991554f2SKenneth D. Merry 	 * events_to_record.  event_index is the index into recorded_events and
2124991554f2SKenneth D. Merry 	 * event_number is the overall number of an event being recorded since
2125991554f2SKenneth D. Merry 	 * start-of-day.  event_index will roll over; event_number will never
2126991554f2SKenneth D. Merry 	 * roll over.
2127991554f2SKenneth D. Merry 	 */
2128991554f2SKenneth D. Merry 	i = (uint8_t)(event / 32);
2129991554f2SKenneth D. Merry 	j = (uint8_t)(event % 32);
2130991554f2SKenneth D. Merry 	if ((i < 4) && ((1 << j) & sc->events_to_record[i])) {
2131991554f2SKenneth D. Merry 		i = sc->event_index;
2132991554f2SKenneth D. Merry 		sc->recorded_events[i].Type = event;
2133991554f2SKenneth D. Merry 		sc->recorded_events[i].Number = ++sc->event_number;
2134991554f2SKenneth D. Merry 		bzero(sc->recorded_events[i].Data, MPR_MAX_EVENT_DATA_LENGTH *
2135991554f2SKenneth D. Merry 		    4);
2136991554f2SKenneth D. Merry 		event_data_len = event_reply->EventDataLength;
2137991554f2SKenneth D. Merry 
2138991554f2SKenneth D. Merry 		if (event_data_len > 0) {
2139991554f2SKenneth D. Merry 			/*
2140991554f2SKenneth D. Merry 			 * Limit data to size in m_event entry
2141991554f2SKenneth D. Merry 			 */
2142991554f2SKenneth D. Merry 			if (event_data_len > MPR_MAX_EVENT_DATA_LENGTH) {
2143991554f2SKenneth D. Merry 				event_data_len = MPR_MAX_EVENT_DATA_LENGTH;
2144991554f2SKenneth D. Merry 			}
2145991554f2SKenneth D. Merry 			for (j = 0; j < event_data_len; j++) {
2146991554f2SKenneth D. Merry 				sc->recorded_events[i].Data[j] =
2147991554f2SKenneth D. Merry 				    event_reply->EventData[j];
2148991554f2SKenneth D. Merry 			}
2149991554f2SKenneth D. Merry 
2150991554f2SKenneth D. Merry 			/*
2151991554f2SKenneth D. Merry 			 * check for index wrap-around
2152991554f2SKenneth D. Merry 			 */
2153991554f2SKenneth D. Merry 			if (++i == MPR_EVENT_QUEUE_SIZE) {
2154991554f2SKenneth D. Merry 				i = 0;
2155991554f2SKenneth D. Merry 			}
2156991554f2SKenneth D. Merry 			sc->event_index = (uint8_t)i;
2157991554f2SKenneth D. Merry 
2158991554f2SKenneth D. Merry 			/*
2159991554f2SKenneth D. Merry 			 * Set flag to send the event.
2160991554f2SKenneth D. Merry 			 */
2161991554f2SKenneth D. Merry 			sendAEN = TRUE;
2162991554f2SKenneth D. Merry 		}
2163991554f2SKenneth D. Merry 	}
2164991554f2SKenneth D. Merry 
2165991554f2SKenneth D. Merry 	/*
2166991554f2SKenneth D. Merry 	 * Generate a system event if flag is set to let anyone who cares know
2167991554f2SKenneth D. Merry 	 * that an event has occurred.
2168991554f2SKenneth D. Merry 	 */
2169991554f2SKenneth D. Merry 	if (sendAEN) {
2170991554f2SKenneth D. Merry //SLM-how to send a system event (see kqueue, kevent)
2171991554f2SKenneth D. Merry //		(void) ddi_log_sysevent(mpt->m_dip, DDI_VENDOR_LSI, "MPT_SAS",
2172991554f2SKenneth D. Merry //		    "SAS", NULL, NULL, DDI_NOSLEEP);
2173991554f2SKenneth D. Merry 	}
2174991554f2SKenneth D. Merry }
2175991554f2SKenneth D. Merry 
2176991554f2SKenneth D. Merry static int
mpr_user_reg_access(struct mpr_softc * sc,mpr_reg_access_t * data)2177991554f2SKenneth D. Merry mpr_user_reg_access(struct mpr_softc *sc, mpr_reg_access_t *data)
2178991554f2SKenneth D. Merry {
2179991554f2SKenneth D. Merry 	int	status = 0;
2180991554f2SKenneth D. Merry 
2181991554f2SKenneth D. Merry 	switch (data->Command) {
2182991554f2SKenneth D. Merry 		/*
2183991554f2SKenneth D. Merry 		 * IO access is not supported.
2184991554f2SKenneth D. Merry 		 */
2185991554f2SKenneth D. Merry 		case REG_IO_READ:
2186991554f2SKenneth D. Merry 		case REG_IO_WRITE:
2187991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_USER, "IO access is not supported. "
2188991554f2SKenneth D. Merry 			    "Use memory access.");
2189991554f2SKenneth D. Merry 			status = EINVAL;
2190991554f2SKenneth D. Merry 			break;
2191991554f2SKenneth D. Merry 
2192991554f2SKenneth D. Merry 		case REG_MEM_READ:
2193991554f2SKenneth D. Merry 			data->RegData = mpr_regread(sc, data->RegOffset);
2194991554f2SKenneth D. Merry 			break;
2195991554f2SKenneth D. Merry 
2196991554f2SKenneth D. Merry 		case REG_MEM_WRITE:
2197991554f2SKenneth D. Merry 			mpr_regwrite(sc, data->RegOffset, data->RegData);
2198991554f2SKenneth D. Merry 			break;
2199991554f2SKenneth D. Merry 
2200991554f2SKenneth D. Merry 		default:
2201991554f2SKenneth D. Merry 			status = EINVAL;
2202991554f2SKenneth D. Merry 			break;
2203991554f2SKenneth D. Merry 	}
2204991554f2SKenneth D. Merry 
2205991554f2SKenneth D. Merry 	return (status);
2206991554f2SKenneth D. Merry }
2207991554f2SKenneth D. Merry 
2208991554f2SKenneth D. Merry static int
mpr_user_btdh(struct mpr_softc * sc,mpr_btdh_mapping_t * data)2209991554f2SKenneth D. Merry mpr_user_btdh(struct mpr_softc *sc, mpr_btdh_mapping_t *data)
2210991554f2SKenneth D. Merry {
2211991554f2SKenneth D. Merry 	uint8_t		bt2dh = FALSE;
2212991554f2SKenneth D. Merry 	uint8_t		dh2bt = FALSE;
2213991554f2SKenneth D. Merry 	uint16_t	dev_handle, bus, target;
2214991554f2SKenneth D. Merry 
2215991554f2SKenneth D. Merry 	bus = data->Bus;
2216991554f2SKenneth D. Merry 	target = data->TargetID;
2217991554f2SKenneth D. Merry 	dev_handle = data->DevHandle;
2218991554f2SKenneth D. Merry 
2219991554f2SKenneth D. Merry 	/*
2220991554f2SKenneth D. Merry 	 * When DevHandle is 0xFFFF and Bus/Target are not 0xFFFF, use Bus/
2221991554f2SKenneth D. Merry 	 * Target to get DevHandle.  When Bus/Target are 0xFFFF and DevHandle is
2222991554f2SKenneth D. Merry 	 * not 0xFFFF, use DevHandle to get Bus/Target.  Anything else is
2223991554f2SKenneth D. Merry 	 * invalid.
2224991554f2SKenneth D. Merry 	 */
2225991554f2SKenneth D. Merry 	if ((bus == 0xFFFF) && (target == 0xFFFF) && (dev_handle != 0xFFFF))
2226991554f2SKenneth D. Merry 		dh2bt = TRUE;
2227991554f2SKenneth D. Merry 	if ((dev_handle == 0xFFFF) && (bus != 0xFFFF) && (target != 0xFFFF))
2228991554f2SKenneth D. Merry 		bt2dh = TRUE;
2229991554f2SKenneth D. Merry 	if (!dh2bt && !bt2dh)
2230991554f2SKenneth D. Merry 		return (EINVAL);
2231991554f2SKenneth D. Merry 
2232991554f2SKenneth D. Merry 	/*
2233991554f2SKenneth D. Merry 	 * Only handle bus of 0.  Make sure target is within range.
2234991554f2SKenneth D. Merry 	 */
2235991554f2SKenneth D. Merry 	if (bt2dh) {
2236991554f2SKenneth D. Merry 		if (bus != 0)
2237991554f2SKenneth D. Merry 			return (EINVAL);
2238991554f2SKenneth D. Merry 
2239adc0dcc3SMark Johnston 		if (target >= sc->max_devices) {
224067feec50SStephen McConnell 			mpr_dprint(sc, MPR_XINFO, "Target ID is out of range "
2241991554f2SKenneth D. Merry 			   "for Bus/Target to DevHandle mapping.");
2242991554f2SKenneth D. Merry 			return (EINVAL);
2243991554f2SKenneth D. Merry 		}
2244991554f2SKenneth D. Merry 		dev_handle = sc->mapping_table[target].dev_handle;
2245991554f2SKenneth D. Merry 		if (dev_handle)
2246991554f2SKenneth D. Merry 			data->DevHandle = dev_handle;
2247991554f2SKenneth D. Merry 	} else {
2248991554f2SKenneth D. Merry 		bus = 0;
2249327f2e6cSStephen McConnell 		target = mpr_mapping_get_tid_from_handle(sc, dev_handle);
2250991554f2SKenneth D. Merry 		data->Bus = bus;
2251991554f2SKenneth D. Merry 		data->TargetID = target;
2252991554f2SKenneth D. Merry 	}
2253991554f2SKenneth D. Merry 
2254991554f2SKenneth D. Merry 	return (0);
2255991554f2SKenneth D. Merry }
2256991554f2SKenneth D. Merry 
2257991554f2SKenneth D. Merry static int
mpr_ioctl(struct cdev * dev,u_long cmd,void * arg,int flag,struct thread * td)2258991554f2SKenneth D. Merry mpr_ioctl(struct cdev *dev, u_long cmd, void *arg, int flag,
2259991554f2SKenneth D. Merry     struct thread *td)
2260991554f2SKenneth D. Merry {
2261991554f2SKenneth D. Merry 	struct mpr_softc *sc;
2262991554f2SKenneth D. Merry 	struct mpr_cfg_page_req *page_req;
2263991554f2SKenneth D. Merry 	struct mpr_ext_cfg_page_req *ext_page_req;
2264991554f2SKenneth D. Merry 	void *mpr_page;
2265991554f2SKenneth D. Merry 	int error, msleep_ret;
2266991554f2SKenneth D. Merry 
2267991554f2SKenneth D. Merry 	mpr_page = NULL;
2268991554f2SKenneth D. Merry 	sc = dev->si_drv1;
2269991554f2SKenneth D. Merry 	page_req = (void *)arg;
2270991554f2SKenneth D. Merry 	ext_page_req = (void *)arg;
2271991554f2SKenneth D. Merry 
2272991554f2SKenneth D. Merry 	switch (cmd) {
2273991554f2SKenneth D. Merry 	case MPRIO_READ_CFG_HEADER:
2274991554f2SKenneth D. Merry 		mpr_lock(sc);
2275991554f2SKenneth D. Merry 		error = mpr_user_read_cfg_header(sc, page_req);
2276991554f2SKenneth D. Merry 		mpr_unlock(sc);
2277991554f2SKenneth D. Merry 		break;
2278991554f2SKenneth D. Merry 	case MPRIO_READ_CFG_PAGE:
22798276c414SEd Maste 		if (page_req->len < (int)sizeof(MPI2_CONFIG_PAGE_HEADER)) {
22808276c414SEd Maste 			error = EINVAL;
22818276c414SEd Maste 			break;
22828276c414SEd Maste 		}
2283991554f2SKenneth D. Merry 		mpr_page = malloc(page_req->len, M_MPRUSER, M_WAITOK | M_ZERO);
2284991554f2SKenneth D. Merry 		error = copyin(page_req->buf, mpr_page,
2285991554f2SKenneth D. Merry 		    sizeof(MPI2_CONFIG_PAGE_HEADER));
2286991554f2SKenneth D. Merry 		if (error)
2287991554f2SKenneth D. Merry 			break;
2288991554f2SKenneth D. Merry 		mpr_lock(sc);
2289991554f2SKenneth D. Merry 		error = mpr_user_read_cfg_page(sc, page_req, mpr_page);
2290991554f2SKenneth D. Merry 		mpr_unlock(sc);
2291991554f2SKenneth D. Merry 		if (error)
2292991554f2SKenneth D. Merry 			break;
2293991554f2SKenneth D. Merry 		error = copyout(mpr_page, page_req->buf, page_req->len);
2294991554f2SKenneth D. Merry 		break;
2295991554f2SKenneth D. Merry 	case MPRIO_READ_EXT_CFG_HEADER:
2296991554f2SKenneth D. Merry 		mpr_lock(sc);
2297991554f2SKenneth D. Merry 		error = mpr_user_read_extcfg_header(sc, ext_page_req);
2298991554f2SKenneth D. Merry 		mpr_unlock(sc);
2299991554f2SKenneth D. Merry 		break;
2300991554f2SKenneth D. Merry 	case MPRIO_READ_EXT_CFG_PAGE:
23018276c414SEd Maste 		if (ext_page_req->len <
23028276c414SEd Maste 		    (int)sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER)) {
23038276c414SEd Maste 			error = EINVAL;
23048276c414SEd Maste 			break;
23058276c414SEd Maste 		}
2306991554f2SKenneth D. Merry 		mpr_page = malloc(ext_page_req->len, M_MPRUSER,
2307991554f2SKenneth D. Merry 		    M_WAITOK | M_ZERO);
2308991554f2SKenneth D. Merry 		error = copyin(ext_page_req->buf, mpr_page,
2309991554f2SKenneth D. Merry 		    sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
2310991554f2SKenneth D. Merry 		if (error)
2311991554f2SKenneth D. Merry 			break;
2312991554f2SKenneth D. Merry 		mpr_lock(sc);
2313991554f2SKenneth D. Merry 		error = mpr_user_read_extcfg_page(sc, ext_page_req, mpr_page);
2314991554f2SKenneth D. Merry 		mpr_unlock(sc);
2315991554f2SKenneth D. Merry 		if (error)
2316991554f2SKenneth D. Merry 			break;
2317991554f2SKenneth D. Merry 		error = copyout(mpr_page, ext_page_req->buf, ext_page_req->len);
2318991554f2SKenneth D. Merry 		break;
2319991554f2SKenneth D. Merry 	case MPRIO_WRITE_CFG_PAGE:
23208276c414SEd Maste 		if (page_req->len < (int)sizeof(MPI2_CONFIG_PAGE_HEADER)) {
23218276c414SEd Maste 			error = EINVAL;
23228276c414SEd Maste 			break;
23238276c414SEd Maste 		}
2324991554f2SKenneth D. Merry 		mpr_page = malloc(page_req->len, M_MPRUSER, M_WAITOK|M_ZERO);
2325991554f2SKenneth D. Merry 		error = copyin(page_req->buf, mpr_page, page_req->len);
2326991554f2SKenneth D. Merry 		if (error)
2327991554f2SKenneth D. Merry 			break;
2328991554f2SKenneth D. Merry 		mpr_lock(sc);
2329991554f2SKenneth D. Merry 		error = mpr_user_write_cfg_page(sc, page_req, mpr_page);
2330991554f2SKenneth D. Merry 		mpr_unlock(sc);
2331991554f2SKenneth D. Merry 		break;
2332991554f2SKenneth D. Merry 	case MPRIO_MPR_COMMAND:
2333991554f2SKenneth D. Merry 		error = mpr_user_command(sc, (struct mpr_usr_command *)arg);
2334991554f2SKenneth D. Merry 		break;
2335991554f2SKenneth D. Merry 	case MPTIOCTL_PASS_THRU:
2336991554f2SKenneth D. Merry 		/*
2337991554f2SKenneth D. Merry 		 * The user has requested to pass through a command to be
2338991554f2SKenneth D. Merry 		 * executed by the MPT firmware.  Call our routine which does
2339991554f2SKenneth D. Merry 		 * this.  Only allow one passthru IOCTL at one time.
2340991554f2SKenneth D. Merry 		 */
2341991554f2SKenneth D. Merry 		error = mpr_user_pass_thru(sc, (mpr_pass_thru_t *)arg);
2342991554f2SKenneth D. Merry 		break;
2343991554f2SKenneth D. Merry 	case MPTIOCTL_GET_ADAPTER_DATA:
2344991554f2SKenneth D. Merry 		/*
2345991554f2SKenneth D. Merry 		 * The user has requested to read adapter data.  Call our
2346991554f2SKenneth D. Merry 		 * routine which does this.
2347991554f2SKenneth D. Merry 		 */
2348991554f2SKenneth D. Merry 		error = 0;
2349991554f2SKenneth D. Merry 		mpr_user_get_adapter_data(sc, (mpr_adapter_data_t *)arg);
2350991554f2SKenneth D. Merry 		break;
2351991554f2SKenneth D. Merry 	case MPTIOCTL_GET_PCI_INFO:
2352991554f2SKenneth D. Merry 		/*
2353991554f2SKenneth D. Merry 		 * The user has requested to read pci info.  Call
2354991554f2SKenneth D. Merry 		 * our routine which does this.
2355991554f2SKenneth D. Merry 		 */
2356991554f2SKenneth D. Merry 		mpr_lock(sc);
2357991554f2SKenneth D. Merry 		error = 0;
2358991554f2SKenneth D. Merry 		mpr_user_read_pci_info(sc, (mpr_pci_info_t *)arg);
2359991554f2SKenneth D. Merry 		mpr_unlock(sc);
2360991554f2SKenneth D. Merry 		break;
2361991554f2SKenneth D. Merry 	case MPTIOCTL_RESET_ADAPTER:
2362991554f2SKenneth D. Merry 		mpr_lock(sc);
2363991554f2SKenneth D. Merry 		sc->port_enable_complete = 0;
2364991554f2SKenneth D. Merry 		uint32_t reinit_start = time_uptime;
2365991554f2SKenneth D. Merry 		error = mpr_reinit(sc);
2366991554f2SKenneth D. Merry 		/* Sleep for 300 second. */
2367991554f2SKenneth D. Merry 		msleep_ret = msleep(&sc->port_enable_complete, &sc->mpr_mtx,
2368991554f2SKenneth D. Merry 		    PRIBIO, "mpr_porten", 300 * hz);
2369991554f2SKenneth D. Merry 		mpr_unlock(sc);
2370991554f2SKenneth D. Merry 		if (msleep_ret)
2371991554f2SKenneth D. Merry 			printf("Port Enable did not complete after Diag "
2372991554f2SKenneth D. Merry 			    "Reset msleep error %d.\n", msleep_ret);
2373991554f2SKenneth D. Merry 		else
2374991554f2SKenneth D. Merry 			mpr_dprint(sc, MPR_USER, "Hard Reset with Port Enable "
2375991554f2SKenneth D. Merry 			    "completed in %d seconds.\n",
2376991554f2SKenneth D. Merry 			    (uint32_t)(time_uptime - reinit_start));
2377991554f2SKenneth D. Merry 		break;
2378991554f2SKenneth D. Merry 	case MPTIOCTL_DIAG_ACTION:
2379991554f2SKenneth D. Merry 		/*
2380991554f2SKenneth D. Merry 		 * The user has done a diag buffer action.  Call our routine
2381991554f2SKenneth D. Merry 		 * which does this.  Only allow one diag action at one time.
2382991554f2SKenneth D. Merry 		 */
2383991554f2SKenneth D. Merry 		mpr_lock(sc);
2384991554f2SKenneth D. Merry 		error = mpr_user_diag_action(sc, (mpr_diag_action_t *)arg);
2385991554f2SKenneth D. Merry 		mpr_unlock(sc);
2386991554f2SKenneth D. Merry 		break;
2387991554f2SKenneth D. Merry 	case MPTIOCTL_EVENT_QUERY:
2388991554f2SKenneth D. Merry 		/*
2389991554f2SKenneth D. Merry 		 * The user has done an event query. Call our routine which does
2390991554f2SKenneth D. Merry 		 * this.
2391991554f2SKenneth D. Merry 		 */
2392991554f2SKenneth D. Merry 		error = 0;
2393991554f2SKenneth D. Merry 		mpr_user_event_query(sc, (mpr_event_query_t *)arg);
2394991554f2SKenneth D. Merry 		break;
2395991554f2SKenneth D. Merry 	case MPTIOCTL_EVENT_ENABLE:
2396991554f2SKenneth D. Merry 		/*
2397991554f2SKenneth D. Merry 		 * The user has done an event enable. Call our routine which
2398991554f2SKenneth D. Merry 		 * does this.
2399991554f2SKenneth D. Merry 		 */
2400991554f2SKenneth D. Merry 		error = 0;
2401991554f2SKenneth D. Merry 		mpr_user_event_enable(sc, (mpr_event_enable_t *)arg);
2402991554f2SKenneth D. Merry 		break;
2403991554f2SKenneth D. Merry 	case MPTIOCTL_EVENT_REPORT:
2404991554f2SKenneth D. Merry 		/*
2405991554f2SKenneth D. Merry 		 * The user has done an event report. Call our routine which
2406991554f2SKenneth D. Merry 		 * does this.
2407991554f2SKenneth D. Merry 		 */
2408991554f2SKenneth D. Merry 		error = mpr_user_event_report(sc, (mpr_event_report_t *)arg);
2409991554f2SKenneth D. Merry 		break;
2410991554f2SKenneth D. Merry 	case MPTIOCTL_REG_ACCESS:
2411991554f2SKenneth D. Merry 		/*
2412991554f2SKenneth D. Merry 		 * The user has requested register access.  Call our routine
2413991554f2SKenneth D. Merry 		 * which does this.
2414991554f2SKenneth D. Merry 		 */
2415991554f2SKenneth D. Merry 		mpr_lock(sc);
2416991554f2SKenneth D. Merry 		error = mpr_user_reg_access(sc, (mpr_reg_access_t *)arg);
2417991554f2SKenneth D. Merry 		mpr_unlock(sc);
2418991554f2SKenneth D. Merry 		break;
2419991554f2SKenneth D. Merry 	case MPTIOCTL_BTDH_MAPPING:
2420991554f2SKenneth D. Merry 		/*
2421991554f2SKenneth D. Merry 		 * The user has requested to translate a bus/target to a
2422991554f2SKenneth D. Merry 		 * DevHandle or a DevHandle to a bus/target.  Call our routine
2423991554f2SKenneth D. Merry 		 * which does this.
2424991554f2SKenneth D. Merry 		 */
2425991554f2SKenneth D. Merry 		error = mpr_user_btdh(sc, (mpr_btdh_mapping_t *)arg);
2426991554f2SKenneth D. Merry 		break;
2427991554f2SKenneth D. Merry 	default:
2428991554f2SKenneth D. Merry 		error = ENOIOCTL;
2429991554f2SKenneth D. Merry 		break;
2430991554f2SKenneth D. Merry 	}
2431991554f2SKenneth D. Merry 
2432991554f2SKenneth D. Merry 	if (mpr_page != NULL)
2433991554f2SKenneth D. Merry 		free(mpr_page, M_MPRUSER);
2434991554f2SKenneth D. Merry 
2435991554f2SKenneth D. Merry 	return (error);
2436991554f2SKenneth D. Merry }
2437991554f2SKenneth D. Merry 
2438991554f2SKenneth D. Merry #ifdef COMPAT_FREEBSD32
2439991554f2SKenneth D. Merry 
2440991554f2SKenneth D. Merry struct mpr_cfg_page_req32 {
2441991554f2SKenneth D. Merry 	MPI2_CONFIG_PAGE_HEADER header;
2442991554f2SKenneth D. Merry 	uint32_t page_address;
2443991554f2SKenneth D. Merry 	uint32_t buf;
2444991554f2SKenneth D. Merry 	int	len;
2445991554f2SKenneth D. Merry 	uint16_t ioc_status;
2446991554f2SKenneth D. Merry };
2447991554f2SKenneth D. Merry 
2448991554f2SKenneth D. Merry struct mpr_ext_cfg_page_req32 {
2449991554f2SKenneth D. Merry 	MPI2_CONFIG_EXTENDED_PAGE_HEADER header;
2450991554f2SKenneth D. Merry 	uint32_t page_address;
2451991554f2SKenneth D. Merry 	uint32_t buf;
2452991554f2SKenneth D. Merry 	int	len;
2453991554f2SKenneth D. Merry 	uint16_t ioc_status;
2454991554f2SKenneth D. Merry };
2455991554f2SKenneth D. Merry 
2456991554f2SKenneth D. Merry struct mpr_raid_action32 {
2457991554f2SKenneth D. Merry 	uint8_t action;
2458991554f2SKenneth D. Merry 	uint8_t volume_bus;
2459991554f2SKenneth D. Merry 	uint8_t volume_id;
2460991554f2SKenneth D. Merry 	uint8_t phys_disk_num;
2461991554f2SKenneth D. Merry 	uint32_t action_data_word;
2462991554f2SKenneth D. Merry 	uint32_t buf;
2463991554f2SKenneth D. Merry 	int len;
2464991554f2SKenneth D. Merry 	uint32_t volume_status;
2465991554f2SKenneth D. Merry 	uint32_t action_data[4];
2466991554f2SKenneth D. Merry 	uint16_t action_status;
2467991554f2SKenneth D. Merry 	uint16_t ioc_status;
2468991554f2SKenneth D. Merry 	uint8_t write;
2469991554f2SKenneth D. Merry };
2470991554f2SKenneth D. Merry 
2471991554f2SKenneth D. Merry struct mpr_usr_command32 {
2472991554f2SKenneth D. Merry 	uint32_t req;
2473991554f2SKenneth D. Merry 	uint32_t req_len;
2474991554f2SKenneth D. Merry 	uint32_t rpl;
2475991554f2SKenneth D. Merry 	uint32_t rpl_len;
2476991554f2SKenneth D. Merry 	uint32_t buf;
2477991554f2SKenneth D. Merry 	int len;
2478991554f2SKenneth D. Merry 	uint32_t flags;
2479991554f2SKenneth D. Merry };
2480991554f2SKenneth D. Merry 
2481991554f2SKenneth D. Merry #define	MPRIO_READ_CFG_HEADER32	_IOWR('M', 200, struct mpr_cfg_page_req32)
2482991554f2SKenneth D. Merry #define	MPRIO_READ_CFG_PAGE32	_IOWR('M', 201, struct mpr_cfg_page_req32)
2483991554f2SKenneth D. Merry #define	MPRIO_READ_EXT_CFG_HEADER32 _IOWR('M', 202, struct mpr_ext_cfg_page_req32)
2484991554f2SKenneth D. Merry #define	MPRIO_READ_EXT_CFG_PAGE32 _IOWR('M', 203, struct mpr_ext_cfg_page_req32)
2485991554f2SKenneth D. Merry #define	MPRIO_WRITE_CFG_PAGE32	_IOWR('M', 204, struct mpr_cfg_page_req32)
2486991554f2SKenneth D. Merry #define	MPRIO_RAID_ACTION32	_IOWR('M', 205, struct mpr_raid_action32)
2487991554f2SKenneth D. Merry #define	MPRIO_MPR_COMMAND32	_IOWR('M', 210, struct mpr_usr_command32)
2488991554f2SKenneth D. Merry 
2489991554f2SKenneth D. Merry static int
mpr_ioctl32(struct cdev * dev,u_long cmd32,void * _arg,int flag,struct thread * td)2490991554f2SKenneth D. Merry mpr_ioctl32(struct cdev *dev, u_long cmd32, void *_arg, int flag,
2491991554f2SKenneth D. Merry     struct thread *td)
2492991554f2SKenneth D. Merry {
2493991554f2SKenneth D. Merry 	struct mpr_cfg_page_req32 *page32 = _arg;
2494991554f2SKenneth D. Merry 	struct mpr_ext_cfg_page_req32 *ext32 = _arg;
2495991554f2SKenneth D. Merry 	struct mpr_raid_action32 *raid32 = _arg;
2496991554f2SKenneth D. Merry 	struct mpr_usr_command32 *user32 = _arg;
2497991554f2SKenneth D. Merry 	union {
2498991554f2SKenneth D. Merry 		struct mpr_cfg_page_req page;
2499991554f2SKenneth D. Merry 		struct mpr_ext_cfg_page_req ext;
2500991554f2SKenneth D. Merry 		struct mpr_raid_action raid;
2501991554f2SKenneth D. Merry 		struct mpr_usr_command user;
2502991554f2SKenneth D. Merry 	} arg;
2503991554f2SKenneth D. Merry 	u_long cmd;
2504991554f2SKenneth D. Merry 	int error;
2505991554f2SKenneth D. Merry 
2506991554f2SKenneth D. Merry 	switch (cmd32) {
2507991554f2SKenneth D. Merry 	case MPRIO_READ_CFG_HEADER32:
2508991554f2SKenneth D. Merry 	case MPRIO_READ_CFG_PAGE32:
2509991554f2SKenneth D. Merry 	case MPRIO_WRITE_CFG_PAGE32:
2510991554f2SKenneth D. Merry 		if (cmd32 == MPRIO_READ_CFG_HEADER32)
2511991554f2SKenneth D. Merry 			cmd = MPRIO_READ_CFG_HEADER;
2512991554f2SKenneth D. Merry 		else if (cmd32 == MPRIO_READ_CFG_PAGE32)
2513991554f2SKenneth D. Merry 			cmd = MPRIO_READ_CFG_PAGE;
2514991554f2SKenneth D. Merry 		else
2515991554f2SKenneth D. Merry 			cmd = MPRIO_WRITE_CFG_PAGE;
2516991554f2SKenneth D. Merry 		CP(*page32, arg.page, header);
2517991554f2SKenneth D. Merry 		CP(*page32, arg.page, page_address);
2518991554f2SKenneth D. Merry 		PTRIN_CP(*page32, arg.page, buf);
2519991554f2SKenneth D. Merry 		CP(*page32, arg.page, len);
2520991554f2SKenneth D. Merry 		CP(*page32, arg.page, ioc_status);
2521991554f2SKenneth D. Merry 		break;
2522991554f2SKenneth D. Merry 
2523991554f2SKenneth D. Merry 	case MPRIO_READ_EXT_CFG_HEADER32:
2524991554f2SKenneth D. Merry 	case MPRIO_READ_EXT_CFG_PAGE32:
2525991554f2SKenneth D. Merry 		if (cmd32 == MPRIO_READ_EXT_CFG_HEADER32)
2526991554f2SKenneth D. Merry 			cmd = MPRIO_READ_EXT_CFG_HEADER;
2527991554f2SKenneth D. Merry 		else
2528991554f2SKenneth D. Merry 			cmd = MPRIO_READ_EXT_CFG_PAGE;
2529991554f2SKenneth D. Merry 		CP(*ext32, arg.ext, header);
2530991554f2SKenneth D. Merry 		CP(*ext32, arg.ext, page_address);
2531991554f2SKenneth D. Merry 		PTRIN_CP(*ext32, arg.ext, buf);
2532991554f2SKenneth D. Merry 		CP(*ext32, arg.ext, len);
2533991554f2SKenneth D. Merry 		CP(*ext32, arg.ext, ioc_status);
2534991554f2SKenneth D. Merry 		break;
2535991554f2SKenneth D. Merry 
2536991554f2SKenneth D. Merry 	case MPRIO_RAID_ACTION32:
2537991554f2SKenneth D. Merry 		cmd = MPRIO_RAID_ACTION;
2538991554f2SKenneth D. Merry 		CP(*raid32, arg.raid, action);
2539991554f2SKenneth D. Merry 		CP(*raid32, arg.raid, volume_bus);
2540991554f2SKenneth D. Merry 		CP(*raid32, arg.raid, volume_id);
2541991554f2SKenneth D. Merry 		CP(*raid32, arg.raid, phys_disk_num);
2542991554f2SKenneth D. Merry 		CP(*raid32, arg.raid, action_data_word);
2543991554f2SKenneth D. Merry 		PTRIN_CP(*raid32, arg.raid, buf);
2544991554f2SKenneth D. Merry 		CP(*raid32, arg.raid, len);
2545991554f2SKenneth D. Merry 		CP(*raid32, arg.raid, volume_status);
2546991554f2SKenneth D. Merry 		bcopy(raid32->action_data, arg.raid.action_data,
2547991554f2SKenneth D. Merry 		    sizeof arg.raid.action_data);
2548991554f2SKenneth D. Merry 		CP(*raid32, arg.raid, ioc_status);
2549991554f2SKenneth D. Merry 		CP(*raid32, arg.raid, write);
2550991554f2SKenneth D. Merry 		break;
2551991554f2SKenneth D. Merry 
2552991554f2SKenneth D. Merry 	case MPRIO_MPR_COMMAND32:
2553991554f2SKenneth D. Merry 		cmd = MPRIO_MPR_COMMAND;
2554991554f2SKenneth D. Merry 		PTRIN_CP(*user32, arg.user, req);
2555991554f2SKenneth D. Merry 		CP(*user32, arg.user, req_len);
2556991554f2SKenneth D. Merry 		PTRIN_CP(*user32, arg.user, rpl);
2557991554f2SKenneth D. Merry 		CP(*user32, arg.user, rpl_len);
2558991554f2SKenneth D. Merry 		PTRIN_CP(*user32, arg.user, buf);
2559991554f2SKenneth D. Merry 		CP(*user32, arg.user, len);
2560991554f2SKenneth D. Merry 		CP(*user32, arg.user, flags);
2561991554f2SKenneth D. Merry 		break;
2562991554f2SKenneth D. Merry 	default:
2563991554f2SKenneth D. Merry 		return (ENOIOCTL);
2564991554f2SKenneth D. Merry 	}
2565991554f2SKenneth D. Merry 
2566991554f2SKenneth D. Merry 	error = mpr_ioctl(dev, cmd, &arg, flag, td);
2567991554f2SKenneth D. Merry 	if (error == 0 && (cmd32 & IOC_OUT) != 0) {
2568991554f2SKenneth D. Merry 		switch (cmd32) {
2569991554f2SKenneth D. Merry 		case MPRIO_READ_CFG_HEADER32:
2570991554f2SKenneth D. Merry 		case MPRIO_READ_CFG_PAGE32:
2571991554f2SKenneth D. Merry 		case MPRIO_WRITE_CFG_PAGE32:
2572991554f2SKenneth D. Merry 			CP(arg.page, *page32, header);
2573991554f2SKenneth D. Merry 			CP(arg.page, *page32, page_address);
2574991554f2SKenneth D. Merry 			PTROUT_CP(arg.page, *page32, buf);
2575991554f2SKenneth D. Merry 			CP(arg.page, *page32, len);
2576991554f2SKenneth D. Merry 			CP(arg.page, *page32, ioc_status);
2577991554f2SKenneth D. Merry 			break;
2578991554f2SKenneth D. Merry 
2579991554f2SKenneth D. Merry 		case MPRIO_READ_EXT_CFG_HEADER32:
2580991554f2SKenneth D. Merry 		case MPRIO_READ_EXT_CFG_PAGE32:
2581991554f2SKenneth D. Merry 			CP(arg.ext, *ext32, header);
2582991554f2SKenneth D. Merry 			CP(arg.ext, *ext32, page_address);
2583991554f2SKenneth D. Merry 			PTROUT_CP(arg.ext, *ext32, buf);
2584991554f2SKenneth D. Merry 			CP(arg.ext, *ext32, len);
2585991554f2SKenneth D. Merry 			CP(arg.ext, *ext32, ioc_status);
2586991554f2SKenneth D. Merry 			break;
2587991554f2SKenneth D. Merry 
2588991554f2SKenneth D. Merry 		case MPRIO_RAID_ACTION32:
2589991554f2SKenneth D. Merry 			CP(arg.raid, *raid32, action);
2590991554f2SKenneth D. Merry 			CP(arg.raid, *raid32, volume_bus);
2591991554f2SKenneth D. Merry 			CP(arg.raid, *raid32, volume_id);
2592991554f2SKenneth D. Merry 			CP(arg.raid, *raid32, phys_disk_num);
2593991554f2SKenneth D. Merry 			CP(arg.raid, *raid32, action_data_word);
2594991554f2SKenneth D. Merry 			PTROUT_CP(arg.raid, *raid32, buf);
2595991554f2SKenneth D. Merry 			CP(arg.raid, *raid32, len);
2596991554f2SKenneth D. Merry 			CP(arg.raid, *raid32, volume_status);
2597991554f2SKenneth D. Merry 			bcopy(arg.raid.action_data, raid32->action_data,
2598991554f2SKenneth D. Merry 			    sizeof arg.raid.action_data);
2599991554f2SKenneth D. Merry 			CP(arg.raid, *raid32, ioc_status);
2600991554f2SKenneth D. Merry 			CP(arg.raid, *raid32, write);
2601991554f2SKenneth D. Merry 			break;
2602991554f2SKenneth D. Merry 
2603991554f2SKenneth D. Merry 		case MPRIO_MPR_COMMAND32:
2604991554f2SKenneth D. Merry 			PTROUT_CP(arg.user, *user32, req);
2605991554f2SKenneth D. Merry 			CP(arg.user, *user32, req_len);
2606991554f2SKenneth D. Merry 			PTROUT_CP(arg.user, *user32, rpl);
2607991554f2SKenneth D. Merry 			CP(arg.user, *user32, rpl_len);
2608991554f2SKenneth D. Merry 			PTROUT_CP(arg.user, *user32, buf);
2609991554f2SKenneth D. Merry 			CP(arg.user, *user32, len);
2610991554f2SKenneth D. Merry 			CP(arg.user, *user32, flags);
2611991554f2SKenneth D. Merry 			break;
2612991554f2SKenneth D. Merry 		}
2613991554f2SKenneth D. Merry 	}
2614991554f2SKenneth D. Merry 
2615991554f2SKenneth D. Merry 	return (error);
2616991554f2SKenneth D. Merry }
2617991554f2SKenneth D. Merry #endif /* COMPAT_FREEBSD32 */
2618991554f2SKenneth D. Merry 
2619991554f2SKenneth D. Merry static int
mpr_ioctl_devsw(struct cdev * dev,u_long com,caddr_t arg,int flag,struct thread * td)2620991554f2SKenneth D. Merry mpr_ioctl_devsw(struct cdev *dev, u_long com, caddr_t arg, int flag,
2621991554f2SKenneth D. Merry     struct thread *td)
2622991554f2SKenneth D. Merry {
2623991554f2SKenneth D. Merry #ifdef COMPAT_FREEBSD32
2624991554f2SKenneth D. Merry 	if (SV_CURPROC_FLAG(SV_ILP32))
2625991554f2SKenneth D. Merry 		return (mpr_ioctl32(dev, com, arg, flag, td));
2626991554f2SKenneth D. Merry #endif
2627991554f2SKenneth D. Merry 	return (mpr_ioctl(dev, com, arg, flag, td));
2628991554f2SKenneth D. Merry }
2629