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