xref: /freebsd/sys/dev/mps/mps_user.c (revision 7aa65846327fe5bc7e5961c2f7fd0c61f2ec0b01)
1 /*-
2  * Copyright (c) 2008 Yahoo!, Inc.
3  * All rights reserved.
4  * Written by: John Baldwin <jhb@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the author nor the names of any co-contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * LSI MPT-Fusion Host Adapter FreeBSD userland interface
31  */
32 /*-
33  * Copyright (c) 2011 LSI Corp.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  *
57  * LSI MPT-Fusion Host Adapter FreeBSD
58  *
59  * $FreeBSD$
60  */
61 
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
64 
65 #include "opt_compat.h"
66 
67 /* TODO Move headers to mpsvar */
68 #include <sys/types.h>
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/selinfo.h>
73 #include <sys/module.h>
74 #include <sys/bus.h>
75 #include <sys/conf.h>
76 #include <sys/bio.h>
77 #include <sys/malloc.h>
78 #include <sys/uio.h>
79 #include <sys/sysctl.h>
80 #include <sys/ioccom.h>
81 #include <sys/endian.h>
82 #include <sys/queue.h>
83 #include <sys/kthread.h>
84 #include <sys/taskqueue.h>
85 #include <sys/proc.h>
86 #include <sys/sysent.h>
87 
88 #include <machine/bus.h>
89 #include <machine/resource.h>
90 #include <sys/rman.h>
91 
92 #include <cam/cam.h>
93 #include <cam/scsi/scsi_all.h>
94 
95 #include <dev/mps/mpi/mpi2_type.h>
96 #include <dev/mps/mpi/mpi2.h>
97 #include <dev/mps/mpi/mpi2_ioc.h>
98 #include <dev/mps/mpi/mpi2_cnfg.h>
99 #include <dev/mps/mpi/mpi2_init.h>
100 #include <dev/mps/mpi/mpi2_tool.h>
101 #include <dev/mps/mps_ioctl.h>
102 #include <dev/mps/mpsvar.h>
103 #include <dev/mps/mps_table.h>
104 #include <dev/mps/mps_sas.h>
105 #include <dev/pci/pcivar.h>
106 #include <dev/pci/pcireg.h>
107 
108 static d_open_t		mps_open;
109 static d_close_t	mps_close;
110 static d_ioctl_t	mps_ioctl_devsw;
111 
112 static struct cdevsw mps_cdevsw = {
113 	.d_version =	D_VERSION,
114 	.d_flags =	0,
115 	.d_open =	mps_open,
116 	.d_close =	mps_close,
117 	.d_ioctl =	mps_ioctl_devsw,
118 	.d_name =	"mps",
119 };
120 
121 typedef int (mps_user_f)(struct mps_command *, struct mps_usr_command *);
122 static mps_user_f	mpi_pre_ioc_facts;
123 static mps_user_f	mpi_pre_port_facts;
124 static mps_user_f	mpi_pre_fw_download;
125 static mps_user_f	mpi_pre_fw_upload;
126 static mps_user_f	mpi_pre_sata_passthrough;
127 static mps_user_f	mpi_pre_smp_passthrough;
128 static mps_user_f	mpi_pre_config;
129 static mps_user_f	mpi_pre_sas_io_unit_control;
130 
131 static int mps_user_read_cfg_header(struct mps_softc *,
132 				    struct mps_cfg_page_req *);
133 static int mps_user_read_cfg_page(struct mps_softc *,
134 				  struct mps_cfg_page_req *, void *);
135 static int mps_user_read_extcfg_header(struct mps_softc *,
136 				     struct mps_ext_cfg_page_req *);
137 static int mps_user_read_extcfg_page(struct mps_softc *,
138 				     struct mps_ext_cfg_page_req *, void *);
139 static int mps_user_write_cfg_page(struct mps_softc *,
140 				   struct mps_cfg_page_req *, void *);
141 static int mps_user_setup_request(struct mps_command *,
142 				  struct mps_usr_command *);
143 static int mps_user_command(struct mps_softc *, struct mps_usr_command *);
144 
145 static int mps_user_pass_thru(struct mps_softc *sc, mps_pass_thru_t *data);
146 static void mps_user_get_adapter_data(struct mps_softc *sc,
147     mps_adapter_data_t *data);
148 static void mps_user_read_pci_info(struct mps_softc *sc,
149     mps_pci_info_t *data);
150 static uint8_t mps_get_fw_diag_buffer_number(struct mps_softc *sc,
151     uint32_t unique_id);
152 static int mps_post_fw_diag_buffer(struct mps_softc *sc,
153     mps_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code);
154 static int mps_release_fw_diag_buffer(struct mps_softc *sc,
155     mps_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code,
156     uint32_t diag_type);
157 static int mps_diag_register(struct mps_softc *sc,
158     mps_fw_diag_register_t *diag_register, uint32_t *return_code);
159 static int mps_diag_unregister(struct mps_softc *sc,
160     mps_fw_diag_unregister_t *diag_unregister, uint32_t *return_code);
161 static int mps_diag_query(struct mps_softc *sc, mps_fw_diag_query_t *diag_query,
162     uint32_t *return_code);
163 static int mps_diag_read_buffer(struct mps_softc *sc,
164     mps_diag_read_buffer_t *diag_read_buffer, uint8_t *ioctl_buf,
165     uint32_t *return_code);
166 static int mps_diag_release(struct mps_softc *sc,
167     mps_fw_diag_release_t *diag_release, uint32_t *return_code);
168 static int mps_do_diag_action(struct mps_softc *sc, uint32_t action,
169     uint8_t *diag_action, uint32_t length, uint32_t *return_code);
170 static int mps_user_diag_action(struct mps_softc *sc, mps_diag_action_t *data);
171 static void mps_user_event_query(struct mps_softc *sc, mps_event_query_t *data);
172 static void mps_user_event_enable(struct mps_softc *sc,
173     mps_event_enable_t *data);
174 static int mps_user_event_report(struct mps_softc *sc,
175     mps_event_report_t *data);
176 static int mps_user_reg_access(struct mps_softc *sc, mps_reg_access_t *data);
177 static int mps_user_btdh(struct mps_softc *sc, mps_btdh_mapping_t *data);
178 
179 static MALLOC_DEFINE(M_MPSUSER, "mps_user", "Buffers for mps(4) ioctls");
180 
181 /* Macros from compat/freebsd32/freebsd32.h */
182 #define	PTRIN(v)	(void *)(uintptr_t)(v)
183 #define	PTROUT(v)	(uint32_t)(uintptr_t)(v)
184 
185 #define	CP(src,dst,fld) do { (dst).fld = (src).fld; } while (0)
186 #define	PTRIN_CP(src,dst,fld)				\
187 	do { (dst).fld = PTRIN((src).fld); } while (0)
188 #define	PTROUT_CP(src,dst,fld) \
189 	do { (dst).fld = PTROUT((src).fld); } while (0)
190 
191 int
192 mps_attach_user(struct mps_softc *sc)
193 {
194 	int unit;
195 
196 	unit = device_get_unit(sc->mps_dev);
197 	sc->mps_cdev = make_dev(&mps_cdevsw, unit, UID_ROOT, GID_OPERATOR, 0640,
198 	    "mps%d", unit);
199 	if (sc->mps_cdev == NULL) {
200 		return (ENOMEM);
201 	}
202 	sc->mps_cdev->si_drv1 = sc;
203 	return (0);
204 }
205 
206 void
207 mps_detach_user(struct mps_softc *sc)
208 {
209 
210 	/* XXX: do a purge of pending requests? */
211 	destroy_dev(sc->mps_cdev);
212 
213 }
214 
215 static int
216 mps_open(struct cdev *dev, int flags, int fmt, struct thread *td)
217 {
218 
219 	return (0);
220 }
221 
222 static int
223 mps_close(struct cdev *dev, int flags, int fmt, struct thread *td)
224 {
225 
226 	return (0);
227 }
228 
229 static int
230 mps_user_read_cfg_header(struct mps_softc *sc,
231     struct mps_cfg_page_req *page_req)
232 {
233 	MPI2_CONFIG_PAGE_HEADER *hdr;
234 	struct mps_config_params params;
235 	int	    error;
236 
237 	hdr = &params.hdr.Struct;
238 	params.action = MPI2_CONFIG_ACTION_PAGE_HEADER;
239 	params.page_address = le32toh(page_req->page_address);
240 	hdr->PageVersion = 0;
241 	hdr->PageLength = 0;
242 	hdr->PageNumber = page_req->header.PageNumber;
243 	hdr->PageType = page_req->header.PageType;
244 	params.buffer = NULL;
245 	params.length = 0;
246 	params.callback = NULL;
247 
248 	if ((error = mps_read_config_page(sc, &params)) != 0) {
249 		/*
250 		 * Leave the request. Without resetting the chip, it's
251 		 * still owned by it and we'll just get into trouble
252 		 * freeing it now. Mark it as abandoned so that if it
253 		 * shows up later it can be freed.
254 		 */
255 		mps_printf(sc, "read_cfg_header timed out\n");
256 		return (ETIMEDOUT);
257 	}
258 
259 	page_req->ioc_status = htole16(params.status);
260 	if ((page_req->ioc_status & MPI2_IOCSTATUS_MASK) ==
261 	    MPI2_IOCSTATUS_SUCCESS) {
262 		bcopy(hdr, &page_req->header, sizeof(page_req->header));
263 	}
264 
265 	return (0);
266 }
267 
268 static int
269 mps_user_read_cfg_page(struct mps_softc *sc, struct mps_cfg_page_req *page_req,
270     void *buf)
271 {
272 	MPI2_CONFIG_PAGE_HEADER *reqhdr, *hdr;
273 	struct mps_config_params params;
274 	int	      error;
275 
276 	reqhdr = buf;
277 	hdr = &params.hdr.Struct;
278 	hdr->PageVersion = reqhdr->PageVersion;
279 	hdr->PageLength = reqhdr->PageLength;
280 	hdr->PageNumber = reqhdr->PageNumber;
281 	hdr->PageType = reqhdr->PageType & MPI2_CONFIG_PAGETYPE_MASK;
282 	params.action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
283 	params.page_address = le32toh(page_req->page_address);
284 	params.buffer = buf;
285 	params.length = le32toh(page_req->len);
286 	params.callback = NULL;
287 
288 	if ((error = mps_read_config_page(sc, &params)) != 0) {
289 		mps_printf(sc, "mps_user_read_cfg_page timed out\n");
290 		return (ETIMEDOUT);
291 	}
292 
293 	page_req->ioc_status = htole16(params.status);
294 	return (0);
295 }
296 
297 static int
298 mps_user_read_extcfg_header(struct mps_softc *sc,
299     struct mps_ext_cfg_page_req *ext_page_req)
300 {
301 	MPI2_CONFIG_EXTENDED_PAGE_HEADER *hdr;
302 	struct mps_config_params params;
303 	int	    error;
304 
305 	hdr = &params.hdr.Ext;
306 	params.action = MPI2_CONFIG_ACTION_PAGE_HEADER;
307 	hdr->PageVersion = ext_page_req->header.PageVersion;
308 	hdr->ExtPageLength = 0;
309 	hdr->PageNumber = ext_page_req->header.PageNumber;
310 	hdr->ExtPageType = ext_page_req->header.ExtPageType;
311 	params.page_address = le32toh(ext_page_req->page_address);
312 	if ((error = mps_read_config_page(sc, &params)) != 0) {
313 		/*
314 		 * Leave the request. Without resetting the chip, it's
315 		 * still owned by it and we'll just get into trouble
316 		 * freeing it now. Mark it as abandoned so that if it
317 		 * shows up later it can be freed.
318 		 */
319 		mps_printf(sc, "mps_user_read_extcfg_header timed out\n");
320 		return (ETIMEDOUT);
321 	}
322 
323 	ext_page_req->ioc_status = htole16(params.status);
324 	if ((ext_page_req->ioc_status & MPI2_IOCSTATUS_MASK) ==
325 	    MPI2_IOCSTATUS_SUCCESS) {
326 		ext_page_req->header.PageVersion = hdr->PageVersion;
327 		ext_page_req->header.PageNumber = hdr->PageNumber;
328 		ext_page_req->header.PageType = hdr->PageType;
329 		ext_page_req->header.ExtPageLength = hdr->ExtPageLength;
330 		ext_page_req->header.ExtPageType = hdr->ExtPageType;
331 	}
332 
333 	return (0);
334 }
335 
336 static int
337 mps_user_read_extcfg_page(struct mps_softc *sc,
338     struct mps_ext_cfg_page_req *ext_page_req, void *buf)
339 {
340 	MPI2_CONFIG_EXTENDED_PAGE_HEADER *reqhdr, *hdr;
341 	struct mps_config_params params;
342 	int error;
343 
344 	reqhdr = buf;
345 	hdr = &params.hdr.Ext;
346 	params.action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
347 	params.page_address = le32toh(ext_page_req->page_address);
348 	hdr->PageVersion = reqhdr->PageVersion;
349 	hdr->PageNumber = reqhdr->PageNumber;
350 	hdr->ExtPageType = reqhdr->ExtPageType;
351 	hdr->ExtPageLength = reqhdr->ExtPageLength;
352 	params.buffer = buf;
353 	params.length = le32toh(ext_page_req->len);
354 	params.callback = NULL;
355 
356 	if ((error = mps_read_config_page(sc, &params)) != 0) {
357 		mps_printf(sc, "mps_user_read_extcfg_page timed out\n");
358 		return (ETIMEDOUT);
359 	}
360 
361 	ext_page_req->ioc_status = htole16(params.status);
362 	return (0);
363 }
364 
365 static int
366 mps_user_write_cfg_page(struct mps_softc *sc,
367     struct mps_cfg_page_req *page_req, void *buf)
368 {
369 	MPI2_CONFIG_PAGE_HEADER *reqhdr, *hdr;
370 	struct mps_config_params params;
371 	u_int	      hdr_attr;
372 	int	      error;
373 
374 	reqhdr = buf;
375 	hdr = &params.hdr.Struct;
376 	hdr_attr = reqhdr->PageType & MPI2_CONFIG_PAGEATTR_MASK;
377 	if (hdr_attr != MPI2_CONFIG_PAGEATTR_CHANGEABLE &&
378 	    hdr_attr != MPI2_CONFIG_PAGEATTR_PERSISTENT) {
379 		mps_printf(sc, "page type 0x%x not changeable\n",
380 			reqhdr->PageType & MPI2_CONFIG_PAGETYPE_MASK);
381 		return (EINVAL);
382 	}
383 
384 	/*
385 	 * There isn't any point in restoring stripped out attributes
386 	 * if you then mask them going down to issue the request.
387 	 */
388 
389 	hdr->PageVersion = reqhdr->PageVersion;
390 	hdr->PageLength = reqhdr->PageLength;
391 	hdr->PageNumber = reqhdr->PageNumber;
392 	hdr->PageType = reqhdr->PageType;
393 	params.action = MPI2_CONFIG_ACTION_PAGE_WRITE_CURRENT;
394 	params.page_address = le32toh(page_req->page_address);
395 	params.buffer = buf;
396 	params.length = le32toh(page_req->len);
397 	params.callback = NULL;
398 
399 	if ((error = mps_write_config_page(sc, &params)) != 0) {
400 		mps_printf(sc, "mps_write_cfg_page timed out\n");
401 		return (ETIMEDOUT);
402 	}
403 
404 	page_req->ioc_status = htole16(params.status);
405 	return (0);
406 }
407 
408 void
409 mpi_init_sge(struct mps_command *cm, void *req, void *sge)
410 {
411 	int off, space;
412 
413 	space = (int)cm->cm_sc->facts->IOCRequestFrameSize * 4;
414 	off = (uintptr_t)sge - (uintptr_t)req;
415 
416 	KASSERT(off < space, ("bad pointers %p %p, off %d, space %d",
417             req, sge, off, space));
418 
419 	cm->cm_sge = sge;
420 	cm->cm_sglsize = space - off;
421 }
422 
423 /*
424  * Prepare the mps_command for an IOC_FACTS request.
425  */
426 static int
427 mpi_pre_ioc_facts(struct mps_command *cm, struct mps_usr_command *cmd)
428 {
429 	MPI2_IOC_FACTS_REQUEST *req = (void *)cm->cm_req;
430 	MPI2_IOC_FACTS_REPLY *rpl;
431 
432 	if (cmd->req_len != sizeof *req)
433 		return (EINVAL);
434 	if (cmd->rpl_len != sizeof *rpl)
435 		return (EINVAL);
436 
437 	cm->cm_sge = NULL;
438 	cm->cm_sglsize = 0;
439 	return (0);
440 }
441 
442 /*
443  * Prepare the mps_command for a PORT_FACTS request.
444  */
445 static int
446 mpi_pre_port_facts(struct mps_command *cm, struct mps_usr_command *cmd)
447 {
448 	MPI2_PORT_FACTS_REQUEST *req = (void *)cm->cm_req;
449 	MPI2_PORT_FACTS_REPLY *rpl;
450 
451 	if (cmd->req_len != sizeof *req)
452 		return (EINVAL);
453 	if (cmd->rpl_len != sizeof *rpl)
454 		return (EINVAL);
455 
456 	cm->cm_sge = NULL;
457 	cm->cm_sglsize = 0;
458 	return (0);
459 }
460 
461 /*
462  * Prepare the mps_command for a FW_DOWNLOAD request.
463  */
464 static int
465 mpi_pre_fw_download(struct mps_command *cm, struct mps_usr_command *cmd)
466 {
467 	MPI2_FW_DOWNLOAD_REQUEST *req = (void *)cm->cm_req;
468 	MPI2_FW_DOWNLOAD_REPLY *rpl;
469 	MPI2_FW_DOWNLOAD_TCSGE tc;
470 	int error;
471 
472 	/*
473 	 * This code assumes there is room in the request's SGL for
474 	 * the TransactionContext plus at least a SGL chain element.
475 	 */
476 	CTASSERT(sizeof req->SGL >= sizeof tc + MPS_SGC_SIZE);
477 
478 	if (cmd->req_len != sizeof *req)
479 		return (EINVAL);
480 	if (cmd->rpl_len != sizeof *rpl)
481 		return (EINVAL);
482 
483 	if (cmd->len == 0)
484 		return (EINVAL);
485 
486 	error = copyin(cmd->buf, cm->cm_data, cmd->len);
487 	if (error != 0)
488 		return (error);
489 
490 	mpi_init_sge(cm, req, &req->SGL);
491 	bzero(&tc, sizeof tc);
492 
493 	/*
494 	 * For now, the F/W image must be provided in a single request.
495 	 */
496 	if ((req->MsgFlags & MPI2_FW_DOWNLOAD_MSGFLGS_LAST_SEGMENT) == 0)
497 		return (EINVAL);
498 	if (req->TotalImageSize != cmd->len)
499 		return (EINVAL);
500 
501 	/*
502 	 * The value of the first two elements is specified in the
503 	 * Fusion-MPT Message Passing Interface document.
504 	 */
505 	tc.ContextSize = 0;
506 	tc.DetailsLength = 12;
507 	tc.ImageOffset = 0;
508 	tc.ImageSize = cmd->len;
509 
510 	cm->cm_flags |= MPS_CM_FLAGS_DATAOUT;
511 
512 	return (mps_push_sge(cm, &tc, sizeof tc, 0));
513 }
514 
515 /*
516  * Prepare the mps_command for a FW_UPLOAD request.
517  */
518 static int
519 mpi_pre_fw_upload(struct mps_command *cm, struct mps_usr_command *cmd)
520 {
521 	MPI2_FW_UPLOAD_REQUEST *req = (void *)cm->cm_req;
522 	MPI2_FW_UPLOAD_REPLY *rpl;
523 	MPI2_FW_UPLOAD_TCSGE tc;
524 
525 	/*
526 	 * This code assumes there is room in the request's SGL for
527 	 * the TransactionContext plus at least a SGL chain element.
528 	 */
529 	CTASSERT(sizeof req->SGL >= sizeof tc + MPS_SGC_SIZE);
530 
531 	if (cmd->req_len != sizeof *req)
532 		return (EINVAL);
533 	if (cmd->rpl_len != sizeof *rpl)
534 		return (EINVAL);
535 
536 	mpi_init_sge(cm, req, &req->SGL);
537 	if (cmd->len == 0) {
538 		/* Perhaps just asking what the size of the fw is? */
539 		return (0);
540 	}
541 
542 	bzero(&tc, sizeof tc);
543 
544 	/*
545 	 * The value of the first two elements is specified in the
546 	 * Fusion-MPT Message Passing Interface document.
547 	 */
548 	tc.ContextSize = 0;
549 	tc.DetailsLength = 12;
550 	/*
551 	 * XXX Is there any reason to fetch a partial image?  I.e. to
552 	 * set ImageOffset to something other than 0?
553 	 */
554 	tc.ImageOffset = 0;
555 	tc.ImageSize = cmd->len;
556 
557 	return (mps_push_sge(cm, &tc, sizeof tc, 0));
558 }
559 
560 /*
561  * Prepare the mps_command for a SATA_PASSTHROUGH request.
562  */
563 static int
564 mpi_pre_sata_passthrough(struct mps_command *cm, struct mps_usr_command *cmd)
565 {
566 	MPI2_SATA_PASSTHROUGH_REQUEST *req = (void *)cm->cm_req;
567 	MPI2_SATA_PASSTHROUGH_REPLY *rpl;
568 
569 	if (cmd->req_len != sizeof *req)
570 		return (EINVAL);
571 	if (cmd->rpl_len != sizeof *rpl)
572 		return (EINVAL);
573 
574 	mpi_init_sge(cm, req, &req->SGL);
575 	return (0);
576 }
577 
578 /*
579  * Prepare the mps_command for a SMP_PASSTHROUGH request.
580  */
581 static int
582 mpi_pre_smp_passthrough(struct mps_command *cm, struct mps_usr_command *cmd)
583 {
584 	MPI2_SMP_PASSTHROUGH_REQUEST *req = (void *)cm->cm_req;
585 	MPI2_SMP_PASSTHROUGH_REPLY *rpl;
586 
587 	if (cmd->req_len != sizeof *req)
588 		return (EINVAL);
589 	if (cmd->rpl_len != sizeof *rpl)
590 		return (EINVAL);
591 
592 	mpi_init_sge(cm, req, &req->SGL);
593 	return (0);
594 }
595 
596 /*
597  * Prepare the mps_command for a CONFIG request.
598  */
599 static int
600 mpi_pre_config(struct mps_command *cm, struct mps_usr_command *cmd)
601 {
602 	MPI2_CONFIG_REQUEST *req = (void *)cm->cm_req;
603 	MPI2_CONFIG_REPLY *rpl;
604 
605 	if (cmd->req_len != sizeof *req)
606 		return (EINVAL);
607 	if (cmd->rpl_len != sizeof *rpl)
608 		return (EINVAL);
609 
610 	mpi_init_sge(cm, req, &req->PageBufferSGE);
611 	return (0);
612 }
613 
614 /*
615  * Prepare the mps_command for a SAS_IO_UNIT_CONTROL request.
616  */
617 static int
618 mpi_pre_sas_io_unit_control(struct mps_command *cm,
619 			     struct mps_usr_command *cmd)
620 {
621 
622 	cm->cm_sge = NULL;
623 	cm->cm_sglsize = 0;
624 	return (0);
625 }
626 
627 /*
628  * A set of functions to prepare an mps_command for the various
629  * supported requests.
630  */
631 struct mps_user_func {
632 	U8		Function;
633 	mps_user_f	*f_pre;
634 } mps_user_func_list[] = {
635 	{ MPI2_FUNCTION_IOC_FACTS,		mpi_pre_ioc_facts },
636 	{ MPI2_FUNCTION_PORT_FACTS,		mpi_pre_port_facts },
637 	{ MPI2_FUNCTION_FW_DOWNLOAD, 		mpi_pre_fw_download },
638 	{ MPI2_FUNCTION_FW_UPLOAD,		mpi_pre_fw_upload },
639 	{ MPI2_FUNCTION_SATA_PASSTHROUGH,	mpi_pre_sata_passthrough },
640 	{ MPI2_FUNCTION_SMP_PASSTHROUGH,	mpi_pre_smp_passthrough},
641 	{ MPI2_FUNCTION_CONFIG,			mpi_pre_config},
642 	{ MPI2_FUNCTION_SAS_IO_UNIT_CONTROL,	mpi_pre_sas_io_unit_control },
643 	{ 0xFF,					NULL } /* list end */
644 };
645 
646 static int
647 mps_user_setup_request(struct mps_command *cm, struct mps_usr_command *cmd)
648 {
649 	MPI2_REQUEST_HEADER *hdr = (MPI2_REQUEST_HEADER *)cm->cm_req;
650 	struct mps_user_func *f;
651 
652 	for (f = mps_user_func_list; f->f_pre != NULL; f++) {
653 		if (hdr->Function == f->Function)
654 			return (f->f_pre(cm, cmd));
655 	}
656 	return (EINVAL);
657 }
658 
659 static int
660 mps_user_command(struct mps_softc *sc, struct mps_usr_command *cmd)
661 {
662 	MPI2_REQUEST_HEADER *hdr;
663 	MPI2_DEFAULT_REPLY *rpl;
664 	void *buf = NULL;
665 	struct mps_command *cm = NULL;
666 	int err = 0;
667 	int sz;
668 
669 	mps_lock(sc);
670 	cm = mps_alloc_command(sc);
671 
672 	if (cm == NULL) {
673 		mps_printf(sc, "mps_user_command: no mps requests\n");
674 		err = ENOMEM;
675 		goto Ret;
676 	}
677 	mps_unlock(sc);
678 
679 	hdr = (MPI2_REQUEST_HEADER *)cm->cm_req;
680 
681 	mps_dprint(sc, MPS_INFO, "mps_user_command: req %p %d  rpl %p %d\n",
682 		    cmd->req, cmd->req_len, cmd->rpl, cmd->rpl_len );
683 
684 	if (cmd->req_len > (int)sc->facts->IOCRequestFrameSize * 4) {
685 		err = EINVAL;
686 		goto RetFreeUnlocked;
687 	}
688 	err = copyin(cmd->req, hdr, cmd->req_len);
689 	if (err != 0)
690 		goto RetFreeUnlocked;
691 
692 	mps_dprint(sc, MPS_INFO, "mps_user_command: Function %02X  "
693 	    "MsgFlags %02X\n", hdr->Function, hdr->MsgFlags );
694 
695 	err = mps_user_setup_request(cm, cmd);
696 	if (err != 0) {
697 		mps_printf(sc, "mps_user_command: unsupported function 0x%X\n",
698 		    hdr->Function );
699 		goto RetFreeUnlocked;
700 	}
701 
702 	if (cmd->len > 0) {
703 		buf = malloc(cmd->len, M_MPSUSER, M_WAITOK|M_ZERO);
704 		cm->cm_data = buf;
705 		cm->cm_length = cmd->len;
706 	} else {
707 		cm->cm_data = NULL;
708 		cm->cm_length = 0;
709 	}
710 
711 	cm->cm_flags = MPS_CM_FLAGS_SGE_SIMPLE;
712 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
713 
714 	mps_lock(sc);
715 	err = mps_wait_command(sc, cm, 30);
716 
717 	if (err) {
718 		mps_printf(sc, "%s: invalid request: error %d\n",
719 		    __func__, err);
720 		goto Ret;
721 	}
722 
723 	rpl = (MPI2_DEFAULT_REPLY *)cm->cm_reply;
724 	sz = rpl->MsgLength * 4;
725 
726 	if (sz > cmd->rpl_len) {
727 		mps_printf(sc,
728 		    "mps_user_command: reply buffer too small %d required %d\n",
729 		    cmd->rpl_len, sz );
730 		err = EINVAL;
731 		sz = cmd->rpl_len;
732 	}
733 
734 	mps_unlock(sc);
735 	copyout(rpl, cmd->rpl, sz);
736 	if (buf != NULL)
737 		copyout(buf, cmd->buf, cmd->len);
738 	mps_dprint(sc, MPS_INFO, "mps_user_command: reply size %d\n", sz );
739 
740 RetFreeUnlocked:
741 	mps_lock(sc);
742 	if (cm != NULL)
743 		mps_free_command(sc, cm);
744 Ret:
745 	mps_unlock(sc);
746 	if (buf != NULL)
747 		free(buf, M_MPSUSER);
748 	return (err);
749 }
750 
751 static int
752 mps_user_pass_thru(struct mps_softc *sc, mps_pass_thru_t *data)
753 {
754 	MPI2_REQUEST_HEADER	*hdr, tmphdr;
755 	MPI2_DEFAULT_REPLY	*rpl;
756 	struct mps_command	*cm = NULL;
757 	int			err = 0, dir = 0, sz;
758 	uint8_t			function = 0;
759 	u_int			sense_len;
760 
761 	/*
762 	 * Only allow one passthru command at a time.  Use the MPS_FLAGS_BUSY
763 	 * bit to denote that a passthru is being processed.
764 	 */
765 	mps_lock(sc);
766 	if (sc->mps_flags & MPS_FLAGS_BUSY) {
767 		mps_dprint(sc, MPS_INFO, "%s: Only one passthru command "
768 		    "allowed at a single time.", __func__);
769 		mps_unlock(sc);
770 		return (EBUSY);
771 	}
772 	sc->mps_flags |= MPS_FLAGS_BUSY;
773 	mps_unlock(sc);
774 
775 	/*
776 	 * Do some validation on data direction.  Valid cases are:
777 	 *    1) DataSize is 0 and direction is NONE
778 	 *    2) DataSize is non-zero and one of:
779 	 *        a) direction is READ or
780 	 *        b) direction is WRITE or
781 	 *        c) direction is BOTH and DataOutSize is non-zero
782 	 * If valid and the direction is BOTH, change the direction to READ.
783 	 * if valid and the direction is not BOTH, make sure DataOutSize is 0.
784 	 */
785 	if (((data->DataSize == 0) &&
786 	    (data->DataDirection == MPS_PASS_THRU_DIRECTION_NONE)) ||
787 	    ((data->DataSize != 0) &&
788 	    ((data->DataDirection == MPS_PASS_THRU_DIRECTION_READ) ||
789 	    (data->DataDirection == MPS_PASS_THRU_DIRECTION_WRITE) ||
790 	    ((data->DataDirection == MPS_PASS_THRU_DIRECTION_BOTH) &&
791 	    (data->DataOutSize != 0))))) {
792 		if (data->DataDirection == MPS_PASS_THRU_DIRECTION_BOTH)
793 			data->DataDirection = MPS_PASS_THRU_DIRECTION_READ;
794 		else
795 			data->DataOutSize = 0;
796 	} else
797 		return (EINVAL);
798 
799 	mps_dprint(sc, MPS_INFO, "%s: req 0x%jx %d  rpl 0x%jx %d "
800 	    "data in 0x%jx %d data out 0x%jx %d data dir %d\n", __func__,
801 	    data->PtrRequest, data->RequestSize, data->PtrReply,
802 	    data->ReplySize, data->PtrData, data->DataSize,
803 	    data->PtrDataOut, data->DataOutSize, data->DataDirection);
804 
805 	/*
806 	 * copy in the header so we know what we're dealing with before we
807 	 * commit to allocating a command for it.
808 	 */
809 	err = copyin(PTRIN(data->PtrRequest), &tmphdr, data->RequestSize);
810 	if (err != 0)
811 		goto RetFreeUnlocked;
812 
813 	if (data->RequestSize > (int)sc->facts->IOCRequestFrameSize * 4) {
814 		err = EINVAL;
815 		goto RetFreeUnlocked;
816 	}
817 
818 	function = tmphdr.Function;
819 	mps_dprint(sc, MPS_INFO, "%s: Function %02X MsgFlags %02X\n", __func__,
820 	    function, tmphdr.MsgFlags);
821 
822 	/*
823 	 * Handle a passthru TM request.
824 	 */
825 	if (function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
826 		MPI2_SCSI_TASK_MANAGE_REQUEST	*task;
827 
828 		mps_lock(sc);
829 		cm = mpssas_alloc_tm(sc);
830 		if (cm == NULL) {
831 			err = EINVAL;
832 			goto Ret;
833 		}
834 
835 		/* Copy the header in.  Only a small fixup is needed. */
836 		task = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
837 		bcopy(&tmphdr, task, data->RequestSize);
838 		task->TaskMID = cm->cm_desc.Default.SMID;
839 
840 		cm->cm_data = NULL;
841 		cm->cm_desc.HighPriority.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
842 		cm->cm_complete = NULL;
843 		cm->cm_complete_data = NULL;
844 
845 		err = mps_wait_command(sc, cm, 30);
846 
847 		if (err != 0) {
848 			err = EIO;
849 			mps_dprint(sc, MPS_FAULT, "%s: task management failed",
850 			    __func__);
851 		}
852 		/*
853 		 * Copy the reply data and sense data to user space.
854 		 */
855 		if (cm->cm_reply != NULL) {
856 			rpl = (MPI2_DEFAULT_REPLY *)cm->cm_reply;
857 			sz = rpl->MsgLength * 4;
858 
859 			if (sz > data->ReplySize) {
860 				mps_printf(sc, "%s: reply buffer too small: %d, "
861 				    "required: %d\n", __func__, data->ReplySize, sz);
862 				err = EINVAL;
863 			} else {
864 				mps_unlock(sc);
865 				copyout(cm->cm_reply, PTRIN(data->PtrReply),
866 				    data->ReplySize);
867 				mps_lock(sc);
868 			}
869 		}
870 		mpssas_free_tm(sc, cm);
871 		goto Ret;
872 	}
873 
874 	mps_lock(sc);
875 	cm = mps_alloc_command(sc);
876 
877 	if (cm == NULL) {
878 		mps_printf(sc, "%s: no mps requests\n", __func__);
879 		err = ENOMEM;
880 		goto Ret;
881 	}
882 	mps_unlock(sc);
883 
884 	hdr = (MPI2_REQUEST_HEADER *)cm->cm_req;
885 	bcopy(&tmphdr, hdr, data->RequestSize);
886 
887 	/*
888 	 * Do some checking to make sure the IOCTL request contains a valid
889 	 * request.  Then set the SGL info.
890 	 */
891 	mpi_init_sge(cm, hdr, (void *)((uint8_t *)hdr + data->RequestSize));
892 
893 	/*
894 	 * Set up for read, write or both.  From check above, DataOutSize will
895 	 * be 0 if direction is READ or WRITE, but it will have some non-zero
896 	 * value if the direction is BOTH.  So, just use the biggest size to get
897 	 * the cm_data buffer size.  If direction is BOTH, 2 SGLs need to be set
898 	 * up; the first is for the request and the second will contain the
899 	 * response data. cm_out_len needs to be set here and this will be used
900 	 * when the SGLs are set up.
901 	 */
902 	cm->cm_data = NULL;
903 	cm->cm_length = MAX(data->DataSize, data->DataOutSize);
904 	cm->cm_out_len = data->DataOutSize;
905 	cm->cm_flags = 0;
906 	if (cm->cm_length != 0) {
907 		cm->cm_data = malloc(cm->cm_length, M_MPSUSER, M_WAITOK |
908 		    M_ZERO);
909 		if (cm->cm_data == NULL) {
910 			mps_dprint(sc, MPS_FAULT, "%s: alloc failed for IOCTL "
911 			    "passthru length %d\n", __func__, cm->cm_length);
912 		} else {
913 			cm->cm_flags = MPS_CM_FLAGS_DATAIN;
914 			if (data->DataOutSize) {
915 				cm->cm_flags |= MPS_CM_FLAGS_DATAOUT;
916 				err = copyin(PTRIN(data->PtrDataOut),
917 				    cm->cm_data, data->DataOutSize);
918 			} else if (data->DataDirection ==
919 			    MPS_PASS_THRU_DIRECTION_WRITE) {
920 				cm->cm_flags = MPS_CM_FLAGS_DATAOUT;
921 				err = copyin(PTRIN(data->PtrData),
922 				    cm->cm_data, data->DataSize);
923 			}
924 			if (err != 0)
925 				mps_dprint(sc, MPS_FAULT, "%s: failed to copy "
926 				    "IOCTL data from user space\n", __func__);
927 		}
928 	}
929 	cm->cm_flags |= MPS_CM_FLAGS_SGE_SIMPLE;
930 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
931 
932 	/*
933 	 * Set up Sense buffer and SGL offset for IO passthru.  SCSI IO request
934 	 * uses SCSI IO descriptor.
935 	 */
936 	if ((function == MPI2_FUNCTION_SCSI_IO_REQUEST) ||
937 	    (function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
938 		MPI2_SCSI_IO_REQUEST	*scsi_io_req;
939 
940 		scsi_io_req = (MPI2_SCSI_IO_REQUEST *)hdr;
941 		/*
942 		 * Put SGE for data and data_out buffer at the end of
943 		 * scsi_io_request message header (64 bytes in total).
944 		 * Following above SGEs, the residual space will be used by
945 		 * sense data.
946 		 */
947 		scsi_io_req->SenseBufferLength = (uint8_t)(data->RequestSize -
948 		    64);
949 		scsi_io_req->SenseBufferLowAddress = cm->cm_sense_busaddr;
950 
951 		/*
952 		 * Set SGLOffset0 value.  This is the number of dwords that SGL
953 		 * is offset from the beginning of MPI2_SCSI_IO_REQUEST struct.
954 		 */
955 		scsi_io_req->SGLOffset0 = 24;
956 
957 		/*
958 		 * Setup descriptor info.  RAID passthrough must use the
959 		 * default request descriptor which is already set, so if this
960 		 * is a SCSI IO request, change the descriptor to SCSI IO.
961 		 * Also, if this is a SCSI IO request, handle the reply in the
962 		 * mpssas_scsio_complete function.
963 		 */
964 		if (function == MPI2_FUNCTION_SCSI_IO_REQUEST) {
965 			cm->cm_desc.SCSIIO.RequestFlags =
966 			    MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
967 			cm->cm_desc.SCSIIO.DevHandle = scsi_io_req->DevHandle;
968 
969 			/*
970 			 * Make sure the DevHandle is not 0 because this is a
971 			 * likely error.
972 			 */
973 			if (scsi_io_req->DevHandle == 0) {
974 				err = EINVAL;
975 				goto RetFreeUnlocked;
976 			}
977 		}
978 	}
979 
980 	mps_lock(sc);
981 
982 	err = mps_wait_command(sc, cm, 30);
983 
984 	if (err) {
985 		mps_printf(sc, "%s: invalid request: error %d\n", __func__,
986 		    err);
987 		mps_unlock(sc);
988 		goto RetFreeUnlocked;
989 	}
990 
991 	/*
992 	 * Sync the DMA data, if any.  Then copy the data to user space.
993 	 */
994 	if (cm->cm_data != NULL) {
995 		if (cm->cm_flags & MPS_CM_FLAGS_DATAIN)
996 			dir = BUS_DMASYNC_POSTREAD;
997 		else if (cm->cm_flags & MPS_CM_FLAGS_DATAOUT)
998 			dir = BUS_DMASYNC_POSTWRITE;;
999 		bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir);
1000 		bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
1001 
1002 		if (cm->cm_flags & MPS_CM_FLAGS_DATAIN) {
1003 			mps_unlock(sc);
1004 			err = copyout(cm->cm_data,
1005 			    PTRIN(data->PtrData), data->DataSize);
1006 			mps_lock(sc);
1007 			if (err != 0)
1008 				mps_dprint(sc, MPS_FAULT, "%s: failed to copy "
1009 				    "IOCTL data to user space\n", __func__);
1010 		}
1011 	}
1012 
1013 	/*
1014 	 * Copy the reply data and sense data to user space.
1015 	 */
1016 	if (cm->cm_reply != NULL) {
1017 		rpl = (MPI2_DEFAULT_REPLY *)cm->cm_reply;
1018 		sz = rpl->MsgLength * 4;
1019 
1020 		if (sz > data->ReplySize) {
1021 			mps_printf(sc, "%s: reply buffer too small: %d, "
1022 			    "required: %d\n", __func__, data->ReplySize, sz);
1023 			err = EINVAL;
1024 		} else {
1025 			mps_unlock(sc);
1026 			copyout(cm->cm_reply, PTRIN(data->PtrReply),
1027 			    data->ReplySize);
1028 			mps_lock(sc);
1029 		}
1030 
1031 		if ((function == MPI2_FUNCTION_SCSI_IO_REQUEST) ||
1032 		    (function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
1033 			if (((MPI2_SCSI_IO_REPLY *)rpl)->SCSIState &
1034 			    MPI2_SCSI_STATE_AUTOSENSE_VALID) {
1035 				sense_len =
1036 				    MIN(((MPI2_SCSI_IO_REPLY *)rpl)->SenseCount,
1037 				    sizeof(struct scsi_sense_data));
1038 				mps_unlock(sc);
1039 				copyout(cm->cm_sense, cm->cm_req + 64, sense_len);
1040 				mps_lock(sc);
1041 			}
1042 		}
1043 	}
1044 	mps_unlock(sc);
1045 
1046 RetFreeUnlocked:
1047 	mps_lock(sc);
1048 
1049 	if (cm != NULL) {
1050 		if (cm->cm_data)
1051 			free(cm->cm_data, M_MPSUSER);
1052 		mps_free_command(sc, cm);
1053 	}
1054 Ret:
1055 	sc->mps_flags &= ~MPS_FLAGS_BUSY;
1056 	mps_unlock(sc);
1057 
1058 	return (err);
1059 }
1060 
1061 static void
1062 mps_user_get_adapter_data(struct mps_softc *sc, mps_adapter_data_t *data)
1063 {
1064 	Mpi2ConfigReply_t	mpi_reply;
1065 	Mpi2BiosPage3_t		config_page;
1066 
1067 	/*
1068 	 * Use the PCI interface functions to get the Bus, Device, and Function
1069 	 * information.
1070 	 */
1071 	data->PciInformation.u.bits.BusNumber = pci_get_bus(sc->mps_dev);
1072 	data->PciInformation.u.bits.DeviceNumber = pci_get_slot(sc->mps_dev);
1073 	data->PciInformation.u.bits.FunctionNumber =
1074 	    pci_get_function(sc->mps_dev);
1075 
1076 	/*
1077 	 * Get the FW version that should already be saved in IOC Facts.
1078 	 */
1079 	data->MpiFirmwareVersion = sc->facts->FWVersion.Word;
1080 
1081 	/*
1082 	 * General device info.
1083 	 */
1084 	data->AdapterType = MPSIOCTL_ADAPTER_TYPE_SAS2;
1085 	if (sc->mps_flags & MPS_FLAGS_WD_AVAILABLE)
1086 		data->AdapterType = MPSIOCTL_ADAPTER_TYPE_SAS2_SSS6200;
1087 	data->PCIDeviceHwId = pci_get_device(sc->mps_dev);
1088 	data->PCIDeviceHwRev = pci_read_config(sc->mps_dev, PCIR_REVID, 1);
1089 	data->SubSystemId = pci_get_subdevice(sc->mps_dev);
1090 	data->SubsystemVendorId = pci_get_subvendor(sc->mps_dev);
1091 
1092 	/*
1093 	 * Get the driver version.
1094 	 */
1095 	strcpy((char *)&data->DriverVersion[0], MPS_DRIVER_VERSION);
1096 
1097 	/*
1098 	 * Need to get BIOS Config Page 3 for the BIOS Version.
1099 	 */
1100 	data->BiosVersion = 0;
1101 	mps_lock(sc);
1102 	if (mps_config_get_bios_pg3(sc, &mpi_reply, &config_page))
1103 		printf("%s: Error while retrieving BIOS Version\n", __func__);
1104 	else
1105 		data->BiosVersion = config_page.BiosVersion;
1106 	mps_unlock(sc);
1107 }
1108 
1109 static void
1110 mps_user_read_pci_info(struct mps_softc *sc, mps_pci_info_t *data)
1111 {
1112 	int	i;
1113 
1114 	/*
1115 	 * Use the PCI interface functions to get the Bus, Device, and Function
1116 	 * information.
1117 	 */
1118 	data->BusNumber = pci_get_bus(sc->mps_dev);
1119 	data->DeviceNumber = pci_get_slot(sc->mps_dev);
1120 	data->FunctionNumber = pci_get_function(sc->mps_dev);
1121 
1122 	/*
1123 	 * Now get the interrupt vector and the pci header.  The vector can
1124 	 * only be 0 right now.  The header is the first 256 bytes of config
1125 	 * space.
1126 	 */
1127 	data->InterruptVector = 0;
1128 	for (i = 0; i < sizeof (data->PciHeader); i++) {
1129 		data->PciHeader[i] = pci_read_config(sc->mps_dev, i, 1);
1130 	}
1131 }
1132 
1133 static uint8_t
1134 mps_get_fw_diag_buffer_number(struct mps_softc *sc, uint32_t unique_id)
1135 {
1136 	uint8_t	index;
1137 
1138 	for (index = 0; index < MPI2_DIAG_BUF_TYPE_COUNT; index++) {
1139 		if (sc->fw_diag_buffer_list[index].unique_id == unique_id) {
1140 			return (index);
1141 		}
1142 	}
1143 
1144 	return (MPS_FW_DIAGNOSTIC_UID_NOT_FOUND);
1145 }
1146 
1147 static int
1148 mps_post_fw_diag_buffer(struct mps_softc *sc,
1149     mps_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code)
1150 {
1151 	MPI2_DIAG_BUFFER_POST_REQUEST	*req;
1152 	MPI2_DIAG_BUFFER_POST_REPLY	*reply;
1153 	struct mps_command		*cm = NULL;
1154 	int				i, status;
1155 
1156 	/*
1157 	 * If buffer is not enabled, just leave.
1158 	 */
1159 	*return_code = MPS_FW_DIAG_ERROR_POST_FAILED;
1160 	if (!pBuffer->enabled) {
1161 		return (MPS_DIAG_FAILURE);
1162 	}
1163 
1164 	/*
1165 	 * Clear some flags initially.
1166 	 */
1167 	pBuffer->force_release = FALSE;
1168 	pBuffer->valid_data = FALSE;
1169 	pBuffer->owned_by_firmware = FALSE;
1170 
1171 	/*
1172 	 * Get a command.
1173 	 */
1174 	cm = mps_alloc_command(sc);
1175 	if (cm == NULL) {
1176 		mps_printf(sc, "%s: no mps requests\n", __func__);
1177 		return (MPS_DIAG_FAILURE);
1178 	}
1179 
1180 	/*
1181 	 * Build the request for releasing the FW Diag Buffer and send it.
1182 	 */
1183 	req = (MPI2_DIAG_BUFFER_POST_REQUEST *)cm->cm_req;
1184 	req->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1185 	req->BufferType = pBuffer->buffer_type;
1186 	req->ExtendedType = pBuffer->extended_type;
1187 	req->BufferLength = pBuffer->size;
1188 	for (i = 0; i < (sizeof(req->ProductSpecific) / 4); i++)
1189 		req->ProductSpecific[i] = pBuffer->product_specific[i];
1190 	mps_from_u64(sc->fw_diag_busaddr, &req->BufferAddress);
1191 	cm->cm_data = NULL;
1192 	cm->cm_length = 0;
1193 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1194 	cm->cm_complete_data = NULL;
1195 
1196 	/*
1197 	 * Send command synchronously.
1198 	 */
1199 	status = mps_wait_command(sc, cm, 30);
1200 	if (status) {
1201 		mps_printf(sc, "%s: invalid request: error %d\n", __func__,
1202 		    status);
1203 		status = MPS_DIAG_FAILURE;
1204 		goto done;
1205 	}
1206 
1207 	/*
1208 	 * Process POST reply.
1209 	 */
1210 	reply = (MPI2_DIAG_BUFFER_POST_REPLY *)cm->cm_reply;
1211 	if (reply->IOCStatus != MPI2_IOCSTATUS_SUCCESS) {
1212 		status = MPS_DIAG_FAILURE;
1213 		mps_dprint(sc, MPS_FAULT, "%s: post of FW  Diag Buffer failed "
1214 		    "with IOCStatus = 0x%x, IOCLogInfo = 0x%x and "
1215 		    "TransferLength = 0x%x\n", __func__, reply->IOCStatus,
1216 		    reply->IOCLogInfo, reply->TransferLength);
1217 		goto done;
1218 	}
1219 
1220 	/*
1221 	 * Post was successful.
1222 	 */
1223 	pBuffer->valid_data = TRUE;
1224 	pBuffer->owned_by_firmware = TRUE;
1225 	*return_code = MPS_FW_DIAG_ERROR_SUCCESS;
1226 	status = MPS_DIAG_SUCCESS;
1227 
1228 done:
1229 	mps_free_command(sc, cm);
1230 	return (status);
1231 }
1232 
1233 static int
1234 mps_release_fw_diag_buffer(struct mps_softc *sc,
1235     mps_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code,
1236     uint32_t diag_type)
1237 {
1238 	MPI2_DIAG_RELEASE_REQUEST	*req;
1239 	MPI2_DIAG_RELEASE_REPLY		*reply;
1240 	struct mps_command		*cm = NULL;
1241 	int				status;
1242 
1243 	/*
1244 	 * If buffer is not enabled, just leave.
1245 	 */
1246 	*return_code = MPS_FW_DIAG_ERROR_RELEASE_FAILED;
1247 	if (!pBuffer->enabled) {
1248 		mps_dprint(sc, MPS_INFO, "%s: This buffer type is not supported "
1249 		    "by the IOC", __func__);
1250 		return (MPS_DIAG_FAILURE);
1251 	}
1252 
1253 	/*
1254 	 * Clear some flags initially.
1255 	 */
1256 	pBuffer->force_release = FALSE;
1257 	pBuffer->valid_data = FALSE;
1258 	pBuffer->owned_by_firmware = FALSE;
1259 
1260 	/*
1261 	 * Get a command.
1262 	 */
1263 	cm = mps_alloc_command(sc);
1264 	if (cm == NULL) {
1265 		mps_printf(sc, "%s: no mps requests\n", __func__);
1266 		return (MPS_DIAG_FAILURE);
1267 	}
1268 
1269 	/*
1270 	 * Build the request for releasing the FW Diag Buffer and send it.
1271 	 */
1272 	req = (MPI2_DIAG_RELEASE_REQUEST *)cm->cm_req;
1273 	req->Function = MPI2_FUNCTION_DIAG_RELEASE;
1274 	req->BufferType = pBuffer->buffer_type;
1275 	cm->cm_data = NULL;
1276 	cm->cm_length = 0;
1277 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1278 	cm->cm_complete_data = NULL;
1279 
1280 	/*
1281 	 * Send command synchronously.
1282 	 */
1283 	status = mps_wait_command(sc, cm, 30);
1284 	if (status) {
1285 		mps_printf(sc, "%s: invalid request: error %d\n", __func__,
1286 		    status);
1287 		status = MPS_DIAG_FAILURE;
1288 		goto done;
1289 	}
1290 
1291 	/*
1292 	 * Process RELEASE reply.
1293 	 */
1294 	reply = (MPI2_DIAG_RELEASE_REPLY *)cm->cm_reply;
1295 	if ((reply->IOCStatus != MPI2_IOCSTATUS_SUCCESS) ||
1296 	    pBuffer->owned_by_firmware) {
1297 		status = MPS_DIAG_FAILURE;
1298 		mps_dprint(sc, MPS_FAULT, "%s: release of FW Diag Buffer "
1299 		    "failed with IOCStatus = 0x%x and IOCLogInfo = 0x%x\n",
1300 		    __func__, reply->IOCStatus, reply->IOCLogInfo);
1301 		goto done;
1302 	}
1303 
1304 	/*
1305 	 * Release was successful.
1306 	 */
1307 	*return_code = MPS_FW_DIAG_ERROR_SUCCESS;
1308 	status = MPS_DIAG_SUCCESS;
1309 
1310 	/*
1311 	 * If this was for an UNREGISTER diag type command, clear the unique ID.
1312 	 */
1313 	if (diag_type == MPS_FW_DIAG_TYPE_UNREGISTER) {
1314 		pBuffer->unique_id = MPS_FW_DIAG_INVALID_UID;
1315 	}
1316 
1317 done:
1318 	return (status);
1319 }
1320 
1321 static int
1322 mps_diag_register(struct mps_softc *sc, mps_fw_diag_register_t *diag_register,
1323     uint32_t *return_code)
1324 {
1325 	mps_fw_diagnostic_buffer_t	*pBuffer;
1326 	uint8_t				extended_type, buffer_type, i;
1327 	uint32_t			buffer_size;
1328 	uint32_t			unique_id;
1329 	int				status;
1330 
1331 	extended_type = diag_register->ExtendedType;
1332 	buffer_type = diag_register->BufferType;
1333 	buffer_size = diag_register->RequestedBufferSize;
1334 	unique_id = diag_register->UniqueId;
1335 
1336 	/*
1337 	 * Check for valid buffer type
1338 	 */
1339 	if (buffer_type >= MPI2_DIAG_BUF_TYPE_COUNT) {
1340 		*return_code = MPS_FW_DIAG_ERROR_INVALID_PARAMETER;
1341 		return (MPS_DIAG_FAILURE);
1342 	}
1343 
1344 	/*
1345 	 * Get the current buffer and look up the unique ID.  The unique ID
1346 	 * should not be found.  If it is, the ID is already in use.
1347 	 */
1348 	i = mps_get_fw_diag_buffer_number(sc, unique_id);
1349 	pBuffer = &sc->fw_diag_buffer_list[buffer_type];
1350 	if (i != MPS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1351 		*return_code = MPS_FW_DIAG_ERROR_INVALID_UID;
1352 		return (MPS_DIAG_FAILURE);
1353 	}
1354 
1355 	/*
1356 	 * The buffer's unique ID should not be registered yet, and the given
1357 	 * unique ID cannot be 0.
1358 	 */
1359 	if ((pBuffer->unique_id != MPS_FW_DIAG_INVALID_UID) ||
1360 	    (unique_id == MPS_FW_DIAG_INVALID_UID)) {
1361 		*return_code = MPS_FW_DIAG_ERROR_INVALID_UID;
1362 		return (MPS_DIAG_FAILURE);
1363 	}
1364 
1365 	/*
1366 	 * If this buffer is already posted as immediate, just change owner.
1367 	 */
1368 	if (pBuffer->immediate && pBuffer->owned_by_firmware &&
1369 	    (pBuffer->unique_id == MPS_FW_DIAG_INVALID_UID)) {
1370 		pBuffer->immediate = FALSE;
1371 		pBuffer->unique_id = unique_id;
1372 		return (MPS_DIAG_SUCCESS);
1373 	}
1374 
1375 	/*
1376 	 * Post a new buffer after checking if it's enabled.  The DMA buffer
1377 	 * that is allocated will be contiguous (nsegments = 1).
1378 	 */
1379 	if (!pBuffer->enabled) {
1380 		*return_code = MPS_FW_DIAG_ERROR_NO_BUFFER;
1381 		return (MPS_DIAG_FAILURE);
1382 	}
1383         if (bus_dma_tag_create( sc->mps_parent_dmat,    /* parent */
1384 				1, 0,			/* algnmnt, boundary */
1385 				BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1386 				BUS_SPACE_MAXADDR,	/* highaddr */
1387 				NULL, NULL,		/* filter, filterarg */
1388                                 buffer_size,		/* maxsize */
1389                                 1,			/* nsegments */
1390                                 buffer_size,		/* maxsegsize */
1391                                 0,			/* flags */
1392                                 NULL, NULL,		/* lockfunc, lockarg */
1393                                 &sc->fw_diag_dmat)) {
1394 		device_printf(sc->mps_dev, "Cannot allocate FW diag buffer DMA "
1395 		    "tag\n");
1396 		return (ENOMEM);
1397         }
1398         if (bus_dmamem_alloc(sc->fw_diag_dmat, (void **)&sc->fw_diag_buffer,
1399 	    BUS_DMA_NOWAIT, &sc->fw_diag_map)) {
1400 		device_printf(sc->mps_dev, "Cannot allocate FW diag buffer "
1401 		    "memory\n");
1402 		return (ENOMEM);
1403         }
1404         bzero(sc->fw_diag_buffer, buffer_size);
1405         bus_dmamap_load(sc->fw_diag_dmat, sc->fw_diag_map, sc->fw_diag_buffer,
1406 	    buffer_size, mps_memaddr_cb, &sc->fw_diag_busaddr, 0);
1407 	pBuffer->size = buffer_size;
1408 
1409 	/*
1410 	 * Copy the given info to the diag buffer and post the buffer.
1411 	 */
1412 	pBuffer->buffer_type = buffer_type;
1413 	pBuffer->immediate = FALSE;
1414 	if (buffer_type == MPI2_DIAG_BUF_TYPE_TRACE) {
1415 		for (i = 0; i < (sizeof (pBuffer->product_specific) / 4);
1416 		    i++) {
1417 			pBuffer->product_specific[i] =
1418 			    diag_register->ProductSpecific[i];
1419 		}
1420 	}
1421 	pBuffer->extended_type = extended_type;
1422 	pBuffer->unique_id = unique_id;
1423 	status = mps_post_fw_diag_buffer(sc, pBuffer, return_code);
1424 
1425 	/*
1426 	 * In case there was a failure, free the DMA buffer.
1427 	 */
1428 	if (status == MPS_DIAG_FAILURE) {
1429 		if (sc->fw_diag_busaddr != 0)
1430 			bus_dmamap_unload(sc->fw_diag_dmat, sc->fw_diag_map);
1431 		if (sc->fw_diag_buffer != NULL)
1432 			bus_dmamem_free(sc->fw_diag_dmat, sc->fw_diag_buffer,
1433 			    sc->fw_diag_map);
1434 		if (sc->fw_diag_dmat != NULL)
1435 			bus_dma_tag_destroy(sc->fw_diag_dmat);
1436 	}
1437 
1438 	return (status);
1439 }
1440 
1441 static int
1442 mps_diag_unregister(struct mps_softc *sc,
1443     mps_fw_diag_unregister_t *diag_unregister, uint32_t *return_code)
1444 {
1445 	mps_fw_diagnostic_buffer_t	*pBuffer;
1446 	uint8_t				i;
1447 	uint32_t			unique_id;
1448 	int				status;
1449 
1450 	unique_id = diag_unregister->UniqueId;
1451 
1452 	/*
1453 	 * Get the current buffer and look up the unique ID.  The unique ID
1454 	 * should be there.
1455 	 */
1456 	i = mps_get_fw_diag_buffer_number(sc, unique_id);
1457 	if (i == MPS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1458 		*return_code = MPS_FW_DIAG_ERROR_INVALID_UID;
1459 		return (MPS_DIAG_FAILURE);
1460 	}
1461 
1462 	pBuffer = &sc->fw_diag_buffer_list[i];
1463 
1464 	/*
1465 	 * Try to release the buffer from FW before freeing it.  If release
1466 	 * fails, don't free the DMA buffer in case FW tries to access it
1467 	 * later.  If buffer is not owned by firmware, can't release it.
1468 	 */
1469 	if (!pBuffer->owned_by_firmware) {
1470 		status = MPS_DIAG_SUCCESS;
1471 	} else {
1472 		status = mps_release_fw_diag_buffer(sc, pBuffer, return_code,
1473 		    MPS_FW_DIAG_TYPE_UNREGISTER);
1474 	}
1475 
1476 	/*
1477 	 * At this point, return the current status no matter what happens with
1478 	 * the DMA buffer.
1479 	 */
1480 	pBuffer->unique_id = MPS_FW_DIAG_INVALID_UID;
1481 	if (status == MPS_DIAG_SUCCESS) {
1482 		if (sc->fw_diag_busaddr != 0)
1483 			bus_dmamap_unload(sc->fw_diag_dmat, sc->fw_diag_map);
1484 		if (sc->fw_diag_buffer != NULL)
1485 			bus_dmamem_free(sc->fw_diag_dmat, sc->fw_diag_buffer,
1486 			    sc->fw_diag_map);
1487 		if (sc->fw_diag_dmat != NULL)
1488 			bus_dma_tag_destroy(sc->fw_diag_dmat);
1489 	}
1490 
1491 	return (status);
1492 }
1493 
1494 static int
1495 mps_diag_query(struct mps_softc *sc, mps_fw_diag_query_t *diag_query,
1496     uint32_t *return_code)
1497 {
1498 	mps_fw_diagnostic_buffer_t	*pBuffer;
1499 	uint8_t				i;
1500 	uint32_t			unique_id;
1501 
1502 	unique_id = diag_query->UniqueId;
1503 
1504 	/*
1505 	 * If ID is valid, query on ID.
1506 	 * If ID is invalid, query on buffer type.
1507 	 */
1508 	if (unique_id == MPS_FW_DIAG_INVALID_UID) {
1509 		i = diag_query->BufferType;
1510 		if (i >= MPI2_DIAG_BUF_TYPE_COUNT) {
1511 			*return_code = MPS_FW_DIAG_ERROR_INVALID_UID;
1512 			return (MPS_DIAG_FAILURE);
1513 		}
1514 	} else {
1515 		i = mps_get_fw_diag_buffer_number(sc, unique_id);
1516 		if (i == MPS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1517 			*return_code = MPS_FW_DIAG_ERROR_INVALID_UID;
1518 			return (MPS_DIAG_FAILURE);
1519 		}
1520 	}
1521 
1522 	/*
1523 	 * Fill query structure with the diag buffer info.
1524 	 */
1525 	pBuffer = &sc->fw_diag_buffer_list[i];
1526 	diag_query->BufferType = pBuffer->buffer_type;
1527 	diag_query->ExtendedType = pBuffer->extended_type;
1528 	if (diag_query->BufferType == MPI2_DIAG_BUF_TYPE_TRACE) {
1529 		for (i = 0; i < (sizeof(diag_query->ProductSpecific) / 4);
1530 		    i++) {
1531 			diag_query->ProductSpecific[i] =
1532 			    pBuffer->product_specific[i];
1533 		}
1534 	}
1535 	diag_query->TotalBufferSize = pBuffer->size;
1536 	diag_query->DriverAddedBufferSize = 0;
1537 	diag_query->UniqueId = pBuffer->unique_id;
1538 	diag_query->ApplicationFlags = 0;
1539 	diag_query->DiagnosticFlags = 0;
1540 
1541 	/*
1542 	 * Set/Clear application flags
1543 	 */
1544 	if (pBuffer->immediate) {
1545 		diag_query->ApplicationFlags &= ~MPS_FW_DIAG_FLAG_APP_OWNED;
1546 	} else {
1547 		diag_query->ApplicationFlags |= MPS_FW_DIAG_FLAG_APP_OWNED;
1548 	}
1549 	if (pBuffer->valid_data || pBuffer->owned_by_firmware) {
1550 		diag_query->ApplicationFlags |= MPS_FW_DIAG_FLAG_BUFFER_VALID;
1551 	} else {
1552 		diag_query->ApplicationFlags &= ~MPS_FW_DIAG_FLAG_BUFFER_VALID;
1553 	}
1554 	if (pBuffer->owned_by_firmware) {
1555 		diag_query->ApplicationFlags |=
1556 		    MPS_FW_DIAG_FLAG_FW_BUFFER_ACCESS;
1557 	} else {
1558 		diag_query->ApplicationFlags &=
1559 		    ~MPS_FW_DIAG_FLAG_FW_BUFFER_ACCESS;
1560 	}
1561 
1562 	return (MPS_DIAG_SUCCESS);
1563 }
1564 
1565 static int
1566 mps_diag_read_buffer(struct mps_softc *sc,
1567     mps_diag_read_buffer_t *diag_read_buffer, uint8_t *ioctl_buf,
1568     uint32_t *return_code)
1569 {
1570 	mps_fw_diagnostic_buffer_t	*pBuffer;
1571 	uint8_t				i, *pData;
1572 	uint32_t			unique_id;
1573 	int				status;
1574 
1575 	unique_id = diag_read_buffer->UniqueId;
1576 
1577 	/*
1578 	 * Get the current buffer and look up the unique ID.  The unique ID
1579 	 * should be there.
1580 	 */
1581 	i = mps_get_fw_diag_buffer_number(sc, unique_id);
1582 	if (i == MPS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1583 		*return_code = MPS_FW_DIAG_ERROR_INVALID_UID;
1584 		return (MPS_DIAG_FAILURE);
1585 	}
1586 
1587 	pBuffer = &sc->fw_diag_buffer_list[i];
1588 
1589 	/*
1590 	 * Make sure requested read is within limits
1591 	 */
1592 	if (diag_read_buffer->StartingOffset + diag_read_buffer->BytesToRead >
1593 	    pBuffer->size) {
1594 		*return_code = MPS_FW_DIAG_ERROR_INVALID_PARAMETER;
1595 		return (MPS_DIAG_FAILURE);
1596 	}
1597 
1598 	/*
1599 	 * Copy the requested data from DMA to the diag_read_buffer.  The DMA
1600 	 * buffer that was allocated is one contiguous buffer.
1601 	 */
1602 	pData = (uint8_t *)(sc->fw_diag_buffer +
1603 	    diag_read_buffer->StartingOffset);
1604 	if (copyout(pData, ioctl_buf, diag_read_buffer->BytesToRead) != 0)
1605 		return (MPS_DIAG_FAILURE);
1606 	diag_read_buffer->Status = 0;
1607 
1608 	/*
1609 	 * Set or clear the Force Release flag.
1610 	 */
1611 	if (pBuffer->force_release) {
1612 		diag_read_buffer->Flags |= MPS_FW_DIAG_FLAG_FORCE_RELEASE;
1613 	} else {
1614 		diag_read_buffer->Flags &= ~MPS_FW_DIAG_FLAG_FORCE_RELEASE;
1615 	}
1616 
1617 	/*
1618 	 * If buffer is to be reregistered, make sure it's not already owned by
1619 	 * firmware first.
1620 	 */
1621 	status = MPS_DIAG_SUCCESS;
1622 	if (!pBuffer->owned_by_firmware) {
1623 		if (diag_read_buffer->Flags & MPS_FW_DIAG_FLAG_REREGISTER) {
1624 			status = mps_post_fw_diag_buffer(sc, pBuffer,
1625 			    return_code);
1626 		}
1627 	}
1628 
1629 	return (status);
1630 }
1631 
1632 static int
1633 mps_diag_release(struct mps_softc *sc, mps_fw_diag_release_t *diag_release,
1634     uint32_t *return_code)
1635 {
1636 	mps_fw_diagnostic_buffer_t	*pBuffer;
1637 	uint8_t				i;
1638 	uint32_t			unique_id;
1639 	int				status;
1640 
1641 	unique_id = diag_release->UniqueId;
1642 
1643 	/*
1644 	 * Get the current buffer and look up the unique ID.  The unique ID
1645 	 * should be there.
1646 	 */
1647 	i = mps_get_fw_diag_buffer_number(sc, unique_id);
1648 	if (i == MPS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1649 		*return_code = MPS_FW_DIAG_ERROR_INVALID_UID;
1650 		return (MPS_DIAG_FAILURE);
1651 	}
1652 
1653 	pBuffer = &sc->fw_diag_buffer_list[i];
1654 
1655 	/*
1656 	 * If buffer is not owned by firmware, it's already been released.
1657 	 */
1658 	if (!pBuffer->owned_by_firmware) {
1659 		*return_code = MPS_FW_DIAG_ERROR_ALREADY_RELEASED;
1660 		return (MPS_DIAG_FAILURE);
1661 	}
1662 
1663 	/*
1664 	 * Release the buffer.
1665 	 */
1666 	status = mps_release_fw_diag_buffer(sc, pBuffer, return_code,
1667 	    MPS_FW_DIAG_TYPE_RELEASE);
1668 	return (status);
1669 }
1670 
1671 static int
1672 mps_do_diag_action(struct mps_softc *sc, uint32_t action, uint8_t *diag_action,
1673     uint32_t length, uint32_t *return_code)
1674 {
1675 	mps_fw_diag_register_t		diag_register;
1676 	mps_fw_diag_unregister_t	diag_unregister;
1677 	mps_fw_diag_query_t		diag_query;
1678 	mps_diag_read_buffer_t		diag_read_buffer;
1679 	mps_fw_diag_release_t		diag_release;
1680 	int				status = MPS_DIAG_SUCCESS;
1681 	uint32_t			original_return_code;
1682 
1683 	original_return_code = *return_code;
1684 	*return_code = MPS_FW_DIAG_ERROR_SUCCESS;
1685 
1686 	switch (action) {
1687 		case MPS_FW_DIAG_TYPE_REGISTER:
1688 			if (!length) {
1689 				*return_code =
1690 				    MPS_FW_DIAG_ERROR_INVALID_PARAMETER;
1691 				status = MPS_DIAG_FAILURE;
1692 				break;
1693 			}
1694 			if (copyin(diag_action, &diag_register,
1695 			    sizeof(diag_register)) != 0)
1696 				return (MPS_DIAG_FAILURE);
1697 			status = mps_diag_register(sc, &diag_register,
1698 			    return_code);
1699 			break;
1700 
1701 		case MPS_FW_DIAG_TYPE_UNREGISTER:
1702 			if (length < sizeof(diag_unregister)) {
1703 				*return_code =
1704 				    MPS_FW_DIAG_ERROR_INVALID_PARAMETER;
1705 				status = MPS_DIAG_FAILURE;
1706 				break;
1707 			}
1708 			if (copyin(diag_action, &diag_unregister,
1709 			    sizeof(diag_unregister)) != 0)
1710 				return (MPS_DIAG_FAILURE);
1711 			status = mps_diag_unregister(sc, &diag_unregister,
1712 			    return_code);
1713 			break;
1714 
1715 		case MPS_FW_DIAG_TYPE_QUERY:
1716 			if (length < sizeof (diag_query)) {
1717 				*return_code =
1718 				    MPS_FW_DIAG_ERROR_INVALID_PARAMETER;
1719 				status = MPS_DIAG_FAILURE;
1720 				break;
1721 			}
1722 			if (copyin(diag_action, &diag_query, sizeof(diag_query))
1723 			    != 0)
1724 				return (MPS_DIAG_FAILURE);
1725 			status = mps_diag_query(sc, &diag_query, return_code);
1726 			if (status == MPS_DIAG_SUCCESS)
1727 				if (copyout(&diag_query, diag_action,
1728 				    sizeof (diag_query)) != 0)
1729 					return (MPS_DIAG_FAILURE);
1730 			break;
1731 
1732 		case MPS_FW_DIAG_TYPE_READ_BUFFER:
1733 			if (copyin(diag_action, &diag_read_buffer,
1734 			    sizeof(diag_read_buffer)) != 0)
1735 				return (MPS_DIAG_FAILURE);
1736 			if (length < diag_read_buffer.BytesToRead) {
1737 				*return_code =
1738 				    MPS_FW_DIAG_ERROR_INVALID_PARAMETER;
1739 				status = MPS_DIAG_FAILURE;
1740 				break;
1741 			}
1742 			status = mps_diag_read_buffer(sc, &diag_read_buffer,
1743 			    PTRIN(diag_read_buffer.PtrDataBuffer),
1744 			    return_code);
1745 			if (status == MPS_DIAG_SUCCESS) {
1746 				if (copyout(&diag_read_buffer, diag_action,
1747 				    sizeof(diag_read_buffer) -
1748 				    sizeof(diag_read_buffer.PtrDataBuffer)) !=
1749 				    0)
1750 					return (MPS_DIAG_FAILURE);
1751 			}
1752 			break;
1753 
1754 		case MPS_FW_DIAG_TYPE_RELEASE:
1755 			if (length < sizeof(diag_release)) {
1756 				*return_code =
1757 				    MPS_FW_DIAG_ERROR_INVALID_PARAMETER;
1758 				status = MPS_DIAG_FAILURE;
1759 				break;
1760 			}
1761 			if (copyin(diag_action, &diag_release,
1762 			    sizeof(diag_release)) != 0)
1763 				return (MPS_DIAG_FAILURE);
1764 			status = mps_diag_release(sc, &diag_release,
1765 			    return_code);
1766 			break;
1767 
1768 		default:
1769 			*return_code = MPS_FW_DIAG_ERROR_INVALID_PARAMETER;
1770 			status = MPS_DIAG_FAILURE;
1771 			break;
1772 	}
1773 
1774 	if ((status == MPS_DIAG_FAILURE) &&
1775 	    (original_return_code == MPS_FW_DIAG_NEW) &&
1776 	    (*return_code != MPS_FW_DIAG_ERROR_SUCCESS))
1777 		status = MPS_DIAG_SUCCESS;
1778 
1779 	return (status);
1780 }
1781 
1782 static int
1783 mps_user_diag_action(struct mps_softc *sc, mps_diag_action_t *data)
1784 {
1785 	int			status;
1786 
1787 	/*
1788 	 * Only allow one diag action at one time.
1789 	 */
1790 	if (sc->mps_flags & MPS_FLAGS_BUSY) {
1791 		mps_dprint(sc, MPS_INFO, "%s: Only one FW diag command "
1792 		    "allowed at a single time.", __func__);
1793 		return (EBUSY);
1794 	}
1795 	sc->mps_flags |= MPS_FLAGS_BUSY;
1796 
1797 	/*
1798 	 * Send diag action request
1799 	 */
1800 	if (data->Action == MPS_FW_DIAG_TYPE_REGISTER ||
1801 	    data->Action == MPS_FW_DIAG_TYPE_UNREGISTER ||
1802 	    data->Action == MPS_FW_DIAG_TYPE_QUERY ||
1803 	    data->Action == MPS_FW_DIAG_TYPE_READ_BUFFER ||
1804 	    data->Action == MPS_FW_DIAG_TYPE_RELEASE) {
1805 		status = mps_do_diag_action(sc, data->Action,
1806 		    PTRIN(data->PtrDiagAction), data->Length,
1807 		    &data->ReturnCode);
1808 	} else
1809 		status = EINVAL;
1810 
1811 	sc->mps_flags &= ~MPS_FLAGS_BUSY;
1812 	return (status);
1813 }
1814 
1815 /*
1816  * Copy the event recording mask and the event queue size out.  For
1817  * clarification, the event recording mask (events_to_record) is not the same
1818  * thing as the event mask (event_mask).  events_to_record has a bit set for
1819  * every event type that is to be recorded by the driver, and event_mask has a
1820  * bit cleared for every event that is allowed into the driver from the IOC.
1821  * They really have nothing to do with each other.
1822  */
1823 static void
1824 mps_user_event_query(struct mps_softc *sc, mps_event_query_t *data)
1825 {
1826 	uint8_t	i;
1827 
1828 	mps_lock(sc);
1829 	data->Entries = MPS_EVENT_QUEUE_SIZE;
1830 
1831 	for (i = 0; i < 4; i++) {
1832 		data->Types[i] = sc->events_to_record[i];
1833 	}
1834 	mps_unlock(sc);
1835 }
1836 
1837 /*
1838  * Set the driver's event mask according to what's been given.  See
1839  * mps_user_event_query for explanation of the event recording mask and the IOC
1840  * event mask.  It's the app's responsibility to enable event logging by setting
1841  * the bits in events_to_record.  Initially, no events will be logged.
1842  */
1843 static void
1844 mps_user_event_enable(struct mps_softc *sc, mps_event_enable_t *data)
1845 {
1846 	uint8_t	i;
1847 
1848 	mps_lock(sc);
1849 	for (i = 0; i < 4; i++) {
1850 		sc->events_to_record[i] = data->Types[i];
1851 	}
1852 	mps_unlock(sc);
1853 }
1854 
1855 /*
1856  * Copy out the events that have been recorded, up to the max events allowed.
1857  */
1858 static int
1859 mps_user_event_report(struct mps_softc *sc, mps_event_report_t *data)
1860 {
1861 	int		status = 0;
1862 	uint32_t	size;
1863 
1864 	mps_lock(sc);
1865 	size = data->Size;
1866 	if ((size >= sizeof(sc->recorded_events)) && (status == 0)) {
1867 		mps_unlock(sc);
1868 		if (copyout((void *)sc->recorded_events,
1869 		    PTRIN(data->PtrEvents), size) != 0)
1870 			status = EFAULT;
1871 		mps_lock(sc);
1872 	} else {
1873 		/*
1874 		 * data->Size value is not large enough to copy event data.
1875 		 */
1876 		status = EFAULT;
1877 	}
1878 
1879 	/*
1880 	 * Change size value to match the number of bytes that were copied.
1881 	 */
1882 	if (status == 0)
1883 		data->Size = sizeof(sc->recorded_events);
1884 	mps_unlock(sc);
1885 
1886 	return (status);
1887 }
1888 
1889 /*
1890  * Record events into the driver from the IOC if they are not masked.
1891  */
1892 void
1893 mpssas_record_event(struct mps_softc *sc,
1894     MPI2_EVENT_NOTIFICATION_REPLY *event_reply)
1895 {
1896 	uint32_t	event;
1897 	int		i, j;
1898 	uint16_t	event_data_len;
1899 	boolean_t	sendAEN = FALSE;
1900 
1901 	event = event_reply->Event;
1902 
1903 	/*
1904 	 * Generate a system event to let anyone who cares know that a
1905 	 * LOG_ENTRY_ADDED event has occurred.  This is sent no matter what the
1906 	 * event mask is set to.
1907 	 */
1908 	if (event == MPI2_EVENT_LOG_ENTRY_ADDED) {
1909 		sendAEN = TRUE;
1910 	}
1911 
1912 	/*
1913 	 * Record the event only if its corresponding bit is set in
1914 	 * events_to_record.  event_index is the index into recorded_events and
1915 	 * event_number is the overall number of an event being recorded since
1916 	 * start-of-day.  event_index will roll over; event_number will never
1917 	 * roll over.
1918 	 */
1919 	i = (uint8_t)(event / 32);
1920 	j = (uint8_t)(event % 32);
1921 	if ((i < 4) && ((1 << j) & sc->events_to_record[i])) {
1922 		i = sc->event_index;
1923 		sc->recorded_events[i].Type = event;
1924 		sc->recorded_events[i].Number = ++sc->event_number;
1925 		bzero(sc->recorded_events[i].Data, MPS_MAX_EVENT_DATA_LENGTH *
1926 		    4);
1927 		event_data_len = event_reply->EventDataLength;
1928 
1929 		if (event_data_len > 0) {
1930 			/*
1931 			 * Limit data to size in m_event entry
1932 			 */
1933 			if (event_data_len > MPS_MAX_EVENT_DATA_LENGTH) {
1934 				event_data_len = MPS_MAX_EVENT_DATA_LENGTH;
1935 			}
1936 			for (j = 0; j < event_data_len; j++) {
1937 				sc->recorded_events[i].Data[j] =
1938 				    event_reply->EventData[j];
1939 			}
1940 
1941 			/*
1942 			 * check for index wrap-around
1943 			 */
1944 			if (++i == MPS_EVENT_QUEUE_SIZE) {
1945 				i = 0;
1946 			}
1947 			sc->event_index = (uint8_t)i;
1948 
1949 			/*
1950 			 * Set flag to send the event.
1951 			 */
1952 			sendAEN = TRUE;
1953 		}
1954 	}
1955 
1956 	/*
1957 	 * Generate a system event if flag is set to let anyone who cares know
1958 	 * that an event has occurred.
1959 	 */
1960 	if (sendAEN) {
1961 //SLM-how to send a system event (see kqueue, kevent)
1962 //		(void) ddi_log_sysevent(mpt->m_dip, DDI_VENDOR_LSI, "MPT_SAS",
1963 //		    "SAS", NULL, NULL, DDI_NOSLEEP);
1964 	}
1965 }
1966 
1967 static int
1968 mps_user_reg_access(struct mps_softc *sc, mps_reg_access_t *data)
1969 {
1970 	int	status = 0;
1971 
1972 	switch (data->Command) {
1973 		/*
1974 		 * IO access is not supported.
1975 		 */
1976 		case REG_IO_READ:
1977 		case REG_IO_WRITE:
1978 			mps_dprint(sc, MPS_INFO, "IO access is not supported. "
1979 			    "Use memory access.");
1980 			status = EINVAL;
1981 			break;
1982 
1983 		case REG_MEM_READ:
1984 			data->RegData = mps_regread(sc, data->RegOffset);
1985 			break;
1986 
1987 		case REG_MEM_WRITE:
1988 			mps_regwrite(sc, data->RegOffset, data->RegData);
1989 			break;
1990 
1991 		default:
1992 			status = EINVAL;
1993 			break;
1994 	}
1995 
1996 	return (status);
1997 }
1998 
1999 static int
2000 mps_user_btdh(struct mps_softc *sc, mps_btdh_mapping_t *data)
2001 {
2002 	uint8_t		bt2dh = FALSE;
2003 	uint8_t		dh2bt = FALSE;
2004 	uint16_t	dev_handle, bus, target;
2005 
2006 	bus = data->Bus;
2007 	target = data->TargetID;
2008 	dev_handle = data->DevHandle;
2009 
2010 	/*
2011 	 * When DevHandle is 0xFFFF and Bus/Target are not 0xFFFF, use Bus/
2012 	 * Target to get DevHandle.  When Bus/Target are 0xFFFF and DevHandle is
2013 	 * not 0xFFFF, use DevHandle to get Bus/Target.  Anything else is
2014 	 * invalid.
2015 	 */
2016 	if ((bus == 0xFFFF) && (target == 0xFFFF) && (dev_handle != 0xFFFF))
2017 		dh2bt = TRUE;
2018 	if ((dev_handle == 0xFFFF) && (bus != 0xFFFF) && (target != 0xFFFF))
2019 		bt2dh = TRUE;
2020 	if (!dh2bt && !bt2dh)
2021 		return (EINVAL);
2022 
2023 	/*
2024 	 * Only handle bus of 0.  Make sure target is within range.
2025 	 */
2026 	if (bt2dh) {
2027 		if (bus != 0)
2028 			return (EINVAL);
2029 
2030 		if (target > sc->max_devices) {
2031 			mps_dprint(sc, MPS_FAULT, "Target ID is out of range "
2032 			   "for Bus/Target to DevHandle mapping.");
2033 			return (EINVAL);
2034 		}
2035 		dev_handle = sc->mapping_table[target].dev_handle;
2036 		if (dev_handle)
2037 			data->DevHandle = dev_handle;
2038 	} else {
2039 		bus = 0;
2040 		target = mps_mapping_get_sas_id_from_handle(sc, dev_handle);
2041 		data->Bus = bus;
2042 		data->TargetID = target;
2043 	}
2044 
2045 	return (0);
2046 }
2047 
2048 static int
2049 mps_ioctl(struct cdev *dev, u_long cmd, void *arg, int flag,
2050     struct thread *td)
2051 {
2052 	struct mps_softc *sc;
2053 	struct mps_cfg_page_req *page_req;
2054 	struct mps_ext_cfg_page_req *ext_page_req;
2055 	void *mps_page;
2056 	int error, reset_loop;
2057 
2058 	mps_page = NULL;
2059 	sc = dev->si_drv1;
2060 	page_req = (void *)arg;
2061 	ext_page_req = (void *)arg;
2062 
2063 	switch (cmd) {
2064 	case MPSIO_READ_CFG_HEADER:
2065 		mps_lock(sc);
2066 		error = mps_user_read_cfg_header(sc, page_req);
2067 		mps_unlock(sc);
2068 		break;
2069 	case MPSIO_READ_CFG_PAGE:
2070 		mps_page = malloc(page_req->len, M_MPSUSER, M_WAITOK | M_ZERO);
2071 		error = copyin(page_req->buf, mps_page,
2072 		    sizeof(MPI2_CONFIG_PAGE_HEADER));
2073 		if (error)
2074 			break;
2075 		mps_lock(sc);
2076 		error = mps_user_read_cfg_page(sc, page_req, mps_page);
2077 		mps_unlock(sc);
2078 		if (error)
2079 			break;
2080 		error = copyout(mps_page, page_req->buf, page_req->len);
2081 		break;
2082 	case MPSIO_READ_EXT_CFG_HEADER:
2083 		mps_lock(sc);
2084 		error = mps_user_read_extcfg_header(sc, ext_page_req);
2085 		mps_unlock(sc);
2086 		break;
2087 	case MPSIO_READ_EXT_CFG_PAGE:
2088 		mps_page = malloc(ext_page_req->len, M_MPSUSER, M_WAITOK|M_ZERO);
2089 		error = copyin(ext_page_req->buf, mps_page,
2090 		    sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
2091 		if (error)
2092 			break;
2093 		mps_lock(sc);
2094 		error = mps_user_read_extcfg_page(sc, ext_page_req, mps_page);
2095 		mps_unlock(sc);
2096 		if (error)
2097 			break;
2098 		error = copyout(mps_page, ext_page_req->buf, ext_page_req->len);
2099 		break;
2100 	case MPSIO_WRITE_CFG_PAGE:
2101 		mps_page = malloc(page_req->len, M_MPSUSER, M_WAITOK|M_ZERO);
2102 		error = copyin(page_req->buf, mps_page, page_req->len);
2103 		if (error)
2104 			break;
2105 		mps_lock(sc);
2106 		error = mps_user_write_cfg_page(sc, page_req, mps_page);
2107 		mps_unlock(sc);
2108 		break;
2109 	case MPSIO_MPS_COMMAND:
2110 		error = mps_user_command(sc, (struct mps_usr_command *)arg);
2111 		break;
2112 	case MPTIOCTL_PASS_THRU:
2113 		/*
2114 		 * The user has requested to pass through a command to be
2115 		 * executed by the MPT firmware.  Call our routine which does
2116 		 * this.  Only allow one passthru IOCTL at one time.
2117 		 */
2118 		error = mps_user_pass_thru(sc, (mps_pass_thru_t *)arg);
2119 		break;
2120 	case MPTIOCTL_GET_ADAPTER_DATA:
2121 		/*
2122 		 * The user has requested to read adapter data.  Call our
2123 		 * routine which does this.
2124 		 */
2125 		error = 0;
2126 		mps_user_get_adapter_data(sc, (mps_adapter_data_t *)arg);
2127 		break;
2128 	case MPTIOCTL_GET_PCI_INFO:
2129 		/*
2130 		 * The user has requested to read pci info.  Call
2131 		 * our routine which does this.
2132 		 */
2133 		mps_lock(sc);
2134 		error = 0;
2135 		mps_user_read_pci_info(sc, (mps_pci_info_t *)arg);
2136 		mps_unlock(sc);
2137 		break;
2138 	case MPTIOCTL_RESET_ADAPTER:
2139 		mps_lock(sc);
2140 		sc->port_enable_complete = 0;
2141 		error = mps_reinit(sc);
2142 		mps_unlock(sc);
2143 		/*
2144 		 * Wait no more than 5 minutes for Port Enable to complete
2145 		 */
2146 		for (reset_loop = 0; (reset_loop < MPS_DIAG_RESET_TIMEOUT) &&
2147 		    (!sc->port_enable_complete); reset_loop++) {
2148 			DELAY(1000);
2149 		}
2150 		if (reset_loop == MPS_DIAG_RESET_TIMEOUT) {
2151 			printf("Port Enable did not complete after Diag "
2152 			    "Reset.\n");
2153 		}
2154 		break;
2155 	case MPTIOCTL_DIAG_ACTION:
2156 		/*
2157 		 * The user has done a diag buffer action.  Call our routine
2158 		 * which does this.  Only allow one diag action at one time.
2159 		 */
2160 		mps_lock(sc);
2161 		error = mps_user_diag_action(sc, (mps_diag_action_t *)arg);
2162 		mps_unlock(sc);
2163 		break;
2164 	case MPTIOCTL_EVENT_QUERY:
2165 		/*
2166 		 * The user has done an event query. Call our routine which does
2167 		 * this.
2168 		 */
2169 		error = 0;
2170 		mps_user_event_query(sc, (mps_event_query_t *)arg);
2171 		break;
2172 	case MPTIOCTL_EVENT_ENABLE:
2173 		/*
2174 		 * The user has done an event enable. Call our routine which
2175 		 * does this.
2176 		 */
2177 		error = 0;
2178 		mps_user_event_enable(sc, (mps_event_enable_t *)arg);
2179 		break;
2180 	case MPTIOCTL_EVENT_REPORT:
2181 		/*
2182 		 * The user has done an event report. Call our routine which
2183 		 * does this.
2184 		 */
2185 		error = mps_user_event_report(sc, (mps_event_report_t *)arg);
2186 		break;
2187 	case MPTIOCTL_REG_ACCESS:
2188 		/*
2189 		 * The user has requested register access.  Call our routine
2190 		 * which does this.
2191 		 */
2192 		mps_lock(sc);
2193 		error = mps_user_reg_access(sc, (mps_reg_access_t *)arg);
2194 		mps_unlock(sc);
2195 		break;
2196 	case MPTIOCTL_BTDH_MAPPING:
2197 		/*
2198 		 * The user has requested to translate a bus/target to a
2199 		 * DevHandle or a DevHandle to a bus/target.  Call our routine
2200 		 * which does this.
2201 		 */
2202 		error = mps_user_btdh(sc, (mps_btdh_mapping_t *)arg);
2203 		break;
2204 	default:
2205 		error = ENOIOCTL;
2206 		break;
2207 	}
2208 
2209 	if (mps_page != NULL)
2210 		free(mps_page, M_MPSUSER);
2211 
2212 	return (error);
2213 }
2214 
2215 #ifdef COMPAT_FREEBSD32
2216 
2217 struct mps_cfg_page_req32 {
2218 	MPI2_CONFIG_PAGE_HEADER header;
2219 	uint32_t page_address;
2220 	uint32_t buf;
2221 	int	len;
2222 	uint16_t ioc_status;
2223 };
2224 
2225 struct mps_ext_cfg_page_req32 {
2226 	MPI2_CONFIG_EXTENDED_PAGE_HEADER header;
2227 	uint32_t page_address;
2228 	uint32_t buf;
2229 	int	len;
2230 	uint16_t ioc_status;
2231 };
2232 
2233 struct mps_raid_action32 {
2234 	uint8_t action;
2235 	uint8_t volume_bus;
2236 	uint8_t volume_id;
2237 	uint8_t phys_disk_num;
2238 	uint32_t action_data_word;
2239 	uint32_t buf;
2240 	int len;
2241 	uint32_t volume_status;
2242 	uint32_t action_data[4];
2243 	uint16_t action_status;
2244 	uint16_t ioc_status;
2245 	uint8_t write;
2246 };
2247 
2248 struct mps_usr_command32 {
2249 	uint32_t req;
2250 	uint32_t req_len;
2251 	uint32_t rpl;
2252 	uint32_t rpl_len;
2253 	uint32_t buf;
2254 	int len;
2255 	uint32_t flags;
2256 };
2257 
2258 #define	MPSIO_READ_CFG_HEADER32	_IOWR('M', 200, struct mps_cfg_page_req32)
2259 #define	MPSIO_READ_CFG_PAGE32	_IOWR('M', 201, struct mps_cfg_page_req32)
2260 #define	MPSIO_READ_EXT_CFG_HEADER32 _IOWR('M', 202, struct mps_ext_cfg_page_req32)
2261 #define	MPSIO_READ_EXT_CFG_PAGE32 _IOWR('M', 203, struct mps_ext_cfg_page_req32)
2262 #define	MPSIO_WRITE_CFG_PAGE32	_IOWR('M', 204, struct mps_cfg_page_req32)
2263 #define	MPSIO_RAID_ACTION32	_IOWR('M', 205, struct mps_raid_action32)
2264 #define	MPSIO_MPS_COMMAND32	_IOWR('M', 210, struct mps_usr_command32)
2265 
2266 static int
2267 mps_ioctl32(struct cdev *dev, u_long cmd32, void *_arg, int flag,
2268     struct thread *td)
2269 {
2270 	struct mps_cfg_page_req32 *page32 = _arg;
2271 	struct mps_ext_cfg_page_req32 *ext32 = _arg;
2272 	struct mps_raid_action32 *raid32 = _arg;
2273 	struct mps_usr_command32 *user32 = _arg;
2274 	union {
2275 		struct mps_cfg_page_req page;
2276 		struct mps_ext_cfg_page_req ext;
2277 		struct mps_raid_action raid;
2278 		struct mps_usr_command user;
2279 	} arg;
2280 	u_long cmd;
2281 	int error;
2282 
2283 	switch (cmd32) {
2284 	case MPSIO_READ_CFG_HEADER32:
2285 	case MPSIO_READ_CFG_PAGE32:
2286 	case MPSIO_WRITE_CFG_PAGE32:
2287 		if (cmd32 == MPSIO_READ_CFG_HEADER32)
2288 			cmd = MPSIO_READ_CFG_HEADER;
2289 		else if (cmd32 == MPSIO_READ_CFG_PAGE32)
2290 			cmd = MPSIO_READ_CFG_PAGE;
2291 		else
2292 			cmd = MPSIO_WRITE_CFG_PAGE;
2293 		CP(*page32, arg.page, header);
2294 		CP(*page32, arg.page, page_address);
2295 		PTRIN_CP(*page32, arg.page, buf);
2296 		CP(*page32, arg.page, len);
2297 		CP(*page32, arg.page, ioc_status);
2298 		break;
2299 
2300 	case MPSIO_READ_EXT_CFG_HEADER32:
2301 	case MPSIO_READ_EXT_CFG_PAGE32:
2302 		if (cmd32 == MPSIO_READ_EXT_CFG_HEADER32)
2303 			cmd = MPSIO_READ_EXT_CFG_HEADER;
2304 		else
2305 			cmd = MPSIO_READ_EXT_CFG_PAGE;
2306 		CP(*ext32, arg.ext, header);
2307 		CP(*ext32, arg.ext, page_address);
2308 		PTRIN_CP(*ext32, arg.ext, buf);
2309 		CP(*ext32, arg.ext, len);
2310 		CP(*ext32, arg.ext, ioc_status);
2311 		break;
2312 
2313 	case MPSIO_RAID_ACTION32:
2314 		cmd = MPSIO_RAID_ACTION;
2315 		CP(*raid32, arg.raid, action);
2316 		CP(*raid32, arg.raid, volume_bus);
2317 		CP(*raid32, arg.raid, volume_id);
2318 		CP(*raid32, arg.raid, phys_disk_num);
2319 		CP(*raid32, arg.raid, action_data_word);
2320 		PTRIN_CP(*raid32, arg.raid, buf);
2321 		CP(*raid32, arg.raid, len);
2322 		CP(*raid32, arg.raid, volume_status);
2323 		bcopy(raid32->action_data, arg.raid.action_data,
2324 		    sizeof arg.raid.action_data);
2325 		CP(*raid32, arg.raid, ioc_status);
2326 		CP(*raid32, arg.raid, write);
2327 		break;
2328 
2329 	case MPSIO_MPS_COMMAND32:
2330 		cmd = MPSIO_MPS_COMMAND;
2331 		PTRIN_CP(*user32, arg.user, req);
2332 		CP(*user32, arg.user, req_len);
2333 		PTRIN_CP(*user32, arg.user, rpl);
2334 		CP(*user32, arg.user, rpl_len);
2335 		PTRIN_CP(*user32, arg.user, buf);
2336 		CP(*user32, arg.user, len);
2337 		CP(*user32, arg.user, flags);
2338 		break;
2339 	default:
2340 		return (ENOIOCTL);
2341 	}
2342 
2343 	error = mps_ioctl(dev, cmd, &arg, flag, td);
2344 	if (error == 0 && (cmd32 & IOC_OUT) != 0) {
2345 		switch (cmd32) {
2346 		case MPSIO_READ_CFG_HEADER32:
2347 		case MPSIO_READ_CFG_PAGE32:
2348 		case MPSIO_WRITE_CFG_PAGE32:
2349 			CP(arg.page, *page32, header);
2350 			CP(arg.page, *page32, page_address);
2351 			PTROUT_CP(arg.page, *page32, buf);
2352 			CP(arg.page, *page32, len);
2353 			CP(arg.page, *page32, ioc_status);
2354 			break;
2355 
2356 		case MPSIO_READ_EXT_CFG_HEADER32:
2357 		case MPSIO_READ_EXT_CFG_PAGE32:
2358 			CP(arg.ext, *ext32, header);
2359 			CP(arg.ext, *ext32, page_address);
2360 			PTROUT_CP(arg.ext, *ext32, buf);
2361 			CP(arg.ext, *ext32, len);
2362 			CP(arg.ext, *ext32, ioc_status);
2363 			break;
2364 
2365 		case MPSIO_RAID_ACTION32:
2366 			CP(arg.raid, *raid32, action);
2367 			CP(arg.raid, *raid32, volume_bus);
2368 			CP(arg.raid, *raid32, volume_id);
2369 			CP(arg.raid, *raid32, phys_disk_num);
2370 			CP(arg.raid, *raid32, action_data_word);
2371 			PTROUT_CP(arg.raid, *raid32, buf);
2372 			CP(arg.raid, *raid32, len);
2373 			CP(arg.raid, *raid32, volume_status);
2374 			bcopy(arg.raid.action_data, raid32->action_data,
2375 			    sizeof arg.raid.action_data);
2376 			CP(arg.raid, *raid32, ioc_status);
2377 			CP(arg.raid, *raid32, write);
2378 			break;
2379 
2380 		case MPSIO_MPS_COMMAND32:
2381 			PTROUT_CP(arg.user, *user32, req);
2382 			CP(arg.user, *user32, req_len);
2383 			PTROUT_CP(arg.user, *user32, rpl);
2384 			CP(arg.user, *user32, rpl_len);
2385 			PTROUT_CP(arg.user, *user32, buf);
2386 			CP(arg.user, *user32, len);
2387 			CP(arg.user, *user32, flags);
2388 			break;
2389 		}
2390 	}
2391 
2392 	return (error);
2393 }
2394 #endif /* COMPAT_FREEBSD32 */
2395 
2396 static int
2397 mps_ioctl_devsw(struct cdev *dev, u_long com, caddr_t arg, int flag,
2398     struct thread *td)
2399 {
2400 #ifdef COMPAT_FREEBSD32
2401 	if (SV_CURPROC_FLAG(SV_ILP32))
2402 		return (mps_ioctl32(dev, com, arg, flag, td));
2403 #endif
2404 	return (mps_ioctl(dev, com, arg, flag, td));
2405 }
2406