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