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