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