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