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