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