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