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