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