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