2 * Copyright (c) 2008 Yahoo!, Inc.
4 * Written by: John Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
30 * Broadcom Inc. (LSI) MPT-Fusion Host Adapter FreeBSD userland interface
33 * Copyright (c) 2011-2015 LSI Corp.
34 * Copyright (c) 2013-2016 Avago Technologies
35 * Copyright 2000-2020 Broadcom Inc.
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * Broadcom Inc. (LSI) MPT-Fusion Host Adapter FreeBSD
64 #include <sys/cdefs.h>
65 __FBSDID("$FreeBSD$");
67 /* TODO Move headers to mprvar */
68 #include <sys/types.h>
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/selinfo.h>
73 #include <sys/module.h>
77 #include <sys/abi_compat.h>
78 #include <sys/malloc.h>
80 #include <sys/sysctl.h>
81 #include <sys/ioccom.h>
82 #include <sys/endian.h>
83 #include <sys/queue.h>
84 #include <sys/kthread.h>
85 #include <sys/taskqueue.h>
87 #include <sys/sysent.h>
89 #include <machine/bus.h>
90 #include <machine/resource.h>
94 #include <cam/cam_ccb.h>
96 #include <dev/mpr/mpi/mpi2_type.h>
97 #include <dev/mpr/mpi/mpi2.h>
98 #include <dev/mpr/mpi/mpi2_ioc.h>
99 #include <dev/mpr/mpi/mpi2_cnfg.h>
100 #include <dev/mpr/mpi/mpi2_init.h>
101 #include <dev/mpr/mpi/mpi2_tool.h>
102 #include <dev/mpr/mpi/mpi2_pci.h>
103 #include <dev/mpr/mpr_ioctl.h>
104 #include <dev/mpr/mprvar.h>
105 #include <dev/mpr/mpr_table.h>
106 #include <dev/mpr/mpr_sas.h>
107 #include <dev/pci/pcivar.h>
108 #include <dev/pci/pcireg.h>
110 static d_open_t mpr_open;
111 static d_close_t mpr_close;
112 static d_ioctl_t mpr_ioctl_devsw;
114 static struct cdevsw mpr_cdevsw = {
115 .d_version = D_VERSION,
118 .d_close = mpr_close,
119 .d_ioctl = mpr_ioctl_devsw,
123 typedef int (mpr_user_f)(struct mpr_command *, struct mpr_usr_command *);
124 static mpr_user_f mpi_pre_ioc_facts;
125 static mpr_user_f mpi_pre_port_facts;
126 static mpr_user_f mpi_pre_fw_download;
127 static mpr_user_f mpi_pre_fw_upload;
128 static mpr_user_f mpi_pre_sata_passthrough;
129 static mpr_user_f mpi_pre_smp_passthrough;
130 static mpr_user_f mpi_pre_config;
131 static mpr_user_f mpi_pre_sas_io_unit_control;
133 static int mpr_user_read_cfg_header(struct mpr_softc *,
134 struct mpr_cfg_page_req *);
135 static int mpr_user_read_cfg_page(struct mpr_softc *,
136 struct mpr_cfg_page_req *, void *);
137 static int mpr_user_read_extcfg_header(struct mpr_softc *,
138 struct mpr_ext_cfg_page_req *);
139 static int mpr_user_read_extcfg_page(struct mpr_softc *,
140 struct mpr_ext_cfg_page_req *, void *);
141 static int mpr_user_write_cfg_page(struct mpr_softc *,
142 struct mpr_cfg_page_req *, void *);
143 static int mpr_user_setup_request(struct mpr_command *,
144 struct mpr_usr_command *);
145 static int mpr_user_command(struct mpr_softc *, struct mpr_usr_command *);
147 static int mpr_user_pass_thru(struct mpr_softc *sc, mpr_pass_thru_t *data);
148 static void mpr_user_get_adapter_data(struct mpr_softc *sc,
149 mpr_adapter_data_t *data);
150 static void mpr_user_read_pci_info(struct mpr_softc *sc, mpr_pci_info_t *data);
151 static uint8_t mpr_get_fw_diag_buffer_number(struct mpr_softc *sc,
153 static int mpr_post_fw_diag_buffer(struct mpr_softc *sc,
154 mpr_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code);
155 static int mpr_release_fw_diag_buffer(struct mpr_softc *sc,
156 mpr_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code,
158 static int mpr_diag_register(struct mpr_softc *sc,
159 mpr_fw_diag_register_t *diag_register, uint32_t *return_code);
160 static int mpr_diag_unregister(struct mpr_softc *sc,
161 mpr_fw_diag_unregister_t *diag_unregister, uint32_t *return_code);
162 static int mpr_diag_query(struct mpr_softc *sc, mpr_fw_diag_query_t *diag_query,
163 uint32_t *return_code);
164 static int mpr_diag_read_buffer(struct mpr_softc *sc,
165 mpr_diag_read_buffer_t *diag_read_buffer, uint8_t *ioctl_buf,
166 uint32_t *return_code);
167 static int mpr_diag_release(struct mpr_softc *sc,
168 mpr_fw_diag_release_t *diag_release, uint32_t *return_code);
169 static int mpr_do_diag_action(struct mpr_softc *sc, uint32_t action,
170 uint8_t *diag_action, uint32_t length, uint32_t *return_code);
171 static int mpr_user_diag_action(struct mpr_softc *sc, mpr_diag_action_t *data);
172 static void mpr_user_event_query(struct mpr_softc *sc, mpr_event_query_t *data);
173 static void mpr_user_event_enable(struct mpr_softc *sc,
174 mpr_event_enable_t *data);
175 static int mpr_user_event_report(struct mpr_softc *sc,
176 mpr_event_report_t *data);
177 static int mpr_user_reg_access(struct mpr_softc *sc, mpr_reg_access_t *data);
178 static int mpr_user_btdh(struct mpr_softc *sc, mpr_btdh_mapping_t *data);
180 static MALLOC_DEFINE(M_MPRUSER, "mpr_user", "Buffers for mpr(4) ioctls");
183 * MPI functions that support IEEE SGLs for SAS3.
185 static uint8_t ieee_sgl_func_list[] = {
186 MPI2_FUNCTION_SCSI_IO_REQUEST,
187 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH,
188 MPI2_FUNCTION_SMP_PASSTHROUGH,
189 MPI2_FUNCTION_SATA_PASSTHROUGH,
190 MPI2_FUNCTION_FW_UPLOAD,
191 MPI2_FUNCTION_FW_DOWNLOAD,
192 MPI2_FUNCTION_TARGET_ASSIST,
193 MPI2_FUNCTION_TARGET_STATUS_SEND,
194 MPI2_FUNCTION_TOOLBOX
198 mpr_attach_user(struct mpr_softc *sc)
202 unit = device_get_unit(sc->mpr_dev);
203 sc->mpr_cdev = make_dev(&mpr_cdevsw, unit, UID_ROOT, GID_OPERATOR, 0640,
206 if (sc->mpr_cdev == NULL)
209 sc->mpr_cdev->si_drv1 = sc;
214 mpr_detach_user(struct mpr_softc *sc)
217 /* XXX: do a purge of pending requests? */
218 if (sc->mpr_cdev != NULL)
219 destroy_dev(sc->mpr_cdev);
223 mpr_open(struct cdev *dev, int flags, int fmt, struct thread *td)
230 mpr_close(struct cdev *dev, int flags, int fmt, struct thread *td)
237 mpr_user_read_cfg_header(struct mpr_softc *sc,
238 struct mpr_cfg_page_req *page_req)
240 MPI2_CONFIG_PAGE_HEADER *hdr;
241 struct mpr_config_params params;
244 hdr = ¶ms.hdr.Struct;
245 params.action = MPI2_CONFIG_ACTION_PAGE_HEADER;
246 params.page_address = le32toh(page_req->page_address);
247 hdr->PageVersion = 0;
249 hdr->PageNumber = page_req->header.PageNumber;
250 hdr->PageType = page_req->header.PageType;
251 params.buffer = NULL;
253 params.callback = NULL;
255 if ((error = mpr_read_config_page(sc, ¶ms)) != 0) {
257 * Leave the request. Without resetting the chip, it's
258 * still owned by it and we'll just get into trouble
259 * freeing it now. Mark it as abandoned so that if it
260 * shows up later it can be freed.
262 mpr_printf(sc, "read_cfg_header timed out\n");
266 page_req->ioc_status = htole16(params.status);
267 if ((page_req->ioc_status & MPI2_IOCSTATUS_MASK) ==
268 MPI2_IOCSTATUS_SUCCESS) {
269 bcopy(hdr, &page_req->header, sizeof(page_req->header));
276 mpr_user_read_cfg_page(struct mpr_softc *sc, struct mpr_cfg_page_req *page_req,
279 MPI2_CONFIG_PAGE_HEADER *reqhdr, *hdr;
280 struct mpr_config_params params;
284 hdr = ¶ms.hdr.Struct;
285 hdr->PageVersion = reqhdr->PageVersion;
286 hdr->PageLength = reqhdr->PageLength;
287 hdr->PageNumber = reqhdr->PageNumber;
288 hdr->PageType = reqhdr->PageType & MPI2_CONFIG_PAGETYPE_MASK;
289 params.action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
290 params.page_address = le32toh(page_req->page_address);
292 params.length = le32toh(page_req->len);
293 params.callback = NULL;
295 if ((error = mpr_read_config_page(sc, ¶ms)) != 0) {
296 mpr_printf(sc, "mpr_user_read_cfg_page timed out\n");
300 page_req->ioc_status = htole16(params.status);
305 mpr_user_read_extcfg_header(struct mpr_softc *sc,
306 struct mpr_ext_cfg_page_req *ext_page_req)
308 MPI2_CONFIG_EXTENDED_PAGE_HEADER *hdr;
309 struct mpr_config_params params;
312 hdr = ¶ms.hdr.Ext;
313 params.action = MPI2_CONFIG_ACTION_PAGE_HEADER;
314 hdr->PageVersion = ext_page_req->header.PageVersion;
315 hdr->PageType = MPI2_CONFIG_PAGETYPE_EXTENDED;
316 hdr->ExtPageLength = 0;
317 hdr->PageNumber = ext_page_req->header.PageNumber;
318 hdr->ExtPageType = ext_page_req->header.ExtPageType;
319 params.page_address = le32toh(ext_page_req->page_address);
320 params.buffer = NULL;
322 params.callback = NULL;
324 if ((error = mpr_read_config_page(sc, ¶ms)) != 0) {
326 * Leave the request. Without resetting the chip, it's
327 * still owned by it and we'll just get into trouble
328 * freeing it now. Mark it as abandoned so that if it
329 * shows up later it can be freed.
331 mpr_printf(sc, "mpr_user_read_extcfg_header timed out\n");
335 ext_page_req->ioc_status = htole16(params.status);
336 if ((ext_page_req->ioc_status & MPI2_IOCSTATUS_MASK) ==
337 MPI2_IOCSTATUS_SUCCESS) {
338 ext_page_req->header.PageVersion = hdr->PageVersion;
339 ext_page_req->header.PageNumber = hdr->PageNumber;
340 ext_page_req->header.PageType = hdr->PageType;
341 ext_page_req->header.ExtPageLength = hdr->ExtPageLength;
342 ext_page_req->header.ExtPageType = hdr->ExtPageType;
349 mpr_user_read_extcfg_page(struct mpr_softc *sc,
350 struct mpr_ext_cfg_page_req *ext_page_req, void *buf)
352 MPI2_CONFIG_EXTENDED_PAGE_HEADER *reqhdr, *hdr;
353 struct mpr_config_params params;
357 hdr = ¶ms.hdr.Ext;
358 params.action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
359 params.page_address = le32toh(ext_page_req->page_address);
360 hdr->PageVersion = reqhdr->PageVersion;
361 hdr->PageType = MPI2_CONFIG_PAGETYPE_EXTENDED;
362 hdr->PageNumber = reqhdr->PageNumber;
363 hdr->ExtPageType = reqhdr->ExtPageType;
364 hdr->ExtPageLength = reqhdr->ExtPageLength;
366 params.length = le32toh(ext_page_req->len);
367 params.callback = NULL;
369 if ((error = mpr_read_config_page(sc, ¶ms)) != 0) {
370 mpr_printf(sc, "mpr_user_read_extcfg_page timed out\n");
374 ext_page_req->ioc_status = htole16(params.status);
379 mpr_user_write_cfg_page(struct mpr_softc *sc,
380 struct mpr_cfg_page_req *page_req, void *buf)
382 MPI2_CONFIG_PAGE_HEADER *reqhdr, *hdr;
383 struct mpr_config_params params;
388 hdr = ¶ms.hdr.Struct;
389 hdr_attr = reqhdr->PageType & MPI2_CONFIG_PAGEATTR_MASK;
390 if (hdr_attr != MPI2_CONFIG_PAGEATTR_CHANGEABLE &&
391 hdr_attr != MPI2_CONFIG_PAGEATTR_PERSISTENT) {
392 mpr_printf(sc, "page type 0x%x not changeable\n",
393 reqhdr->PageType & MPI2_CONFIG_PAGETYPE_MASK);
398 * There isn't any point in restoring stripped out attributes
399 * if you then mask them going down to issue the request.
402 hdr->PageVersion = reqhdr->PageVersion;
403 hdr->PageLength = reqhdr->PageLength;
404 hdr->PageNumber = reqhdr->PageNumber;
405 hdr->PageType = reqhdr->PageType;
406 params.action = MPI2_CONFIG_ACTION_PAGE_WRITE_CURRENT;
407 params.page_address = le32toh(page_req->page_address);
409 params.length = le32toh(page_req->len);
410 params.callback = NULL;
412 if ((error = mpr_write_config_page(sc, ¶ms)) != 0) {
413 mpr_printf(sc, "mpr_write_cfg_page timed out\n");
417 page_req->ioc_status = htole16(params.status);
422 mpr_init_sge(struct mpr_command *cm, void *req, void *sge)
426 space = (int)cm->cm_sc->reqframesz;
427 off = (uintptr_t)sge - (uintptr_t)req;
429 KASSERT(off < space, ("bad pointers %p %p, off %d, space %d",
430 req, sge, off, space));
433 cm->cm_sglsize = space - off;
437 * Prepare the mpr_command for an IOC_FACTS request.
440 mpi_pre_ioc_facts(struct mpr_command *cm, struct mpr_usr_command *cmd)
442 MPI2_IOC_FACTS_REQUEST *req = (void *)cm->cm_req;
443 MPI2_IOC_FACTS_REPLY *rpl;
445 if (cmd->req_len != sizeof *req)
447 if (cmd->rpl_len != sizeof *rpl)
456 * Prepare the mpr_command for a PORT_FACTS request.
459 mpi_pre_port_facts(struct mpr_command *cm, struct mpr_usr_command *cmd)
461 MPI2_PORT_FACTS_REQUEST *req = (void *)cm->cm_req;
462 MPI2_PORT_FACTS_REPLY *rpl;
464 if (cmd->req_len != sizeof *req)
466 if (cmd->rpl_len != sizeof *rpl)
475 * Prepare the mpr_command for a FW_DOWNLOAD request.
478 mpi_pre_fw_download(struct mpr_command *cm, struct mpr_usr_command *cmd)
480 MPI25_FW_DOWNLOAD_REQUEST *req = (void *)cm->cm_req;
481 MPI2_FW_DOWNLOAD_REPLY *rpl;
484 if (cmd->req_len != sizeof *req)
486 if (cmd->rpl_len != sizeof *rpl)
492 error = copyin(cmd->buf, cm->cm_data, cmd->len);
496 mpr_init_sge(cm, req, &req->SGL);
499 * For now, the F/W image must be provided in a single request.
501 if ((req->MsgFlags & MPI2_FW_DOWNLOAD_MSGFLGS_LAST_SEGMENT) == 0)
503 if (req->TotalImageSize != cmd->len)
506 req->ImageOffset = 0;
507 req->ImageSize = cmd->len;
509 cm->cm_flags |= MPR_CM_FLAGS_DATAOUT;
511 return (mpr_push_ieee_sge(cm, &req->SGL, 0));
515 * Prepare the mpr_command for a FW_UPLOAD request.
518 mpi_pre_fw_upload(struct mpr_command *cm, struct mpr_usr_command *cmd)
520 MPI25_FW_UPLOAD_REQUEST *req = (void *)cm->cm_req;
521 MPI2_FW_UPLOAD_REPLY *rpl;
523 if (cmd->req_len != sizeof *req)
525 if (cmd->rpl_len != sizeof *rpl)
528 mpr_init_sge(cm, req, &req->SGL);
530 /* Perhaps just asking what the size of the fw is? */
534 req->ImageOffset = 0;
535 req->ImageSize = cmd->len;
537 cm->cm_flags |= MPR_CM_FLAGS_DATAIN;
539 return (mpr_push_ieee_sge(cm, &req->SGL, 0));
543 * Prepare the mpr_command for a SATA_PASSTHROUGH request.
546 mpi_pre_sata_passthrough(struct mpr_command *cm, struct mpr_usr_command *cmd)
548 MPI2_SATA_PASSTHROUGH_REQUEST *req = (void *)cm->cm_req;
549 MPI2_SATA_PASSTHROUGH_REPLY *rpl;
551 if (cmd->req_len != sizeof *req)
553 if (cmd->rpl_len != sizeof *rpl)
556 mpr_init_sge(cm, req, &req->SGL);
561 * Prepare the mpr_command for a SMP_PASSTHROUGH request.
564 mpi_pre_smp_passthrough(struct mpr_command *cm, struct mpr_usr_command *cmd)
566 MPI2_SMP_PASSTHROUGH_REQUEST *req = (void *)cm->cm_req;
567 MPI2_SMP_PASSTHROUGH_REPLY *rpl;
569 if (cmd->req_len != sizeof *req)
571 if (cmd->rpl_len != sizeof *rpl)
574 mpr_init_sge(cm, req, &req->SGL);
579 * Prepare the mpr_command for a CONFIG request.
582 mpi_pre_config(struct mpr_command *cm, struct mpr_usr_command *cmd)
584 MPI2_CONFIG_REQUEST *req = (void *)cm->cm_req;
585 MPI2_CONFIG_REPLY *rpl;
587 if (cmd->req_len != sizeof *req)
589 if (cmd->rpl_len != sizeof *rpl)
592 mpr_init_sge(cm, req, &req->PageBufferSGE);
597 * Prepare the mpr_command for a SAS_IO_UNIT_CONTROL request.
600 mpi_pre_sas_io_unit_control(struct mpr_command *cm,
601 struct mpr_usr_command *cmd)
610 * A set of functions to prepare an mpr_command for the various
611 * supported requests.
613 struct mpr_user_func {
616 } mpr_user_func_list[] = {
617 { MPI2_FUNCTION_IOC_FACTS, mpi_pre_ioc_facts },
618 { MPI2_FUNCTION_PORT_FACTS, mpi_pre_port_facts },
619 { MPI2_FUNCTION_FW_DOWNLOAD, mpi_pre_fw_download },
620 { MPI2_FUNCTION_FW_UPLOAD, mpi_pre_fw_upload },
621 { MPI2_FUNCTION_SATA_PASSTHROUGH, mpi_pre_sata_passthrough },
622 { MPI2_FUNCTION_SMP_PASSTHROUGH, mpi_pre_smp_passthrough},
623 { MPI2_FUNCTION_CONFIG, mpi_pre_config},
624 { MPI2_FUNCTION_SAS_IO_UNIT_CONTROL, mpi_pre_sas_io_unit_control },
625 { 0xFF, NULL } /* list end */
629 mpr_user_setup_request(struct mpr_command *cm, struct mpr_usr_command *cmd)
631 MPI2_REQUEST_HEADER *hdr = (MPI2_REQUEST_HEADER *)cm->cm_req;
632 struct mpr_user_func *f;
634 for (f = mpr_user_func_list; f->f_pre != NULL; f++) {
635 if (hdr->Function == f->Function)
636 return (f->f_pre(cm, cmd));
642 mpr_user_command(struct mpr_softc *sc, struct mpr_usr_command *cmd)
644 MPI2_REQUEST_HEADER *hdr;
645 MPI2_DEFAULT_REPLY *rpl = NULL;
647 struct mpr_command *cm = NULL;
652 cm = mpr_alloc_command(sc);
655 mpr_printf(sc, "%s: no mpr requests\n", __func__);
661 hdr = (MPI2_REQUEST_HEADER *)cm->cm_req;
663 mpr_dprint(sc, MPR_USER, "%s: req %p %d rpl %p %d\n", __func__,
664 cmd->req, cmd->req_len, cmd->rpl, cmd->rpl_len);
666 if (cmd->req_len > (int)sc->reqframesz) {
668 goto RetFreeUnlocked;
670 err = copyin(cmd->req, hdr, cmd->req_len);
672 goto RetFreeUnlocked;
674 mpr_dprint(sc, MPR_USER, "%s: Function %02X MsgFlags %02X\n", __func__,
675 hdr->Function, hdr->MsgFlags);
678 buf = malloc(cmd->len, M_MPRUSER, M_WAITOK|M_ZERO);
680 cm->cm_length = cmd->len;
686 cm->cm_flags = MPR_CM_FLAGS_SGE_SIMPLE;
687 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
689 err = mpr_user_setup_request(cm, cmd);
691 mpr_printf(sc, "%s: unsupported parameter or unsupported "
692 "function in request (function = 0x%X)\n", __func__,
696 goto RetFreeUnlocked;
699 err = mpr_wait_command(sc, &cm, 30, CAN_SLEEP);
701 if (err || (cm == NULL)) {
702 mpr_printf(sc, "%s: invalid request: error %d\n",
708 rpl = (MPI2_DEFAULT_REPLY *)cm->cm_reply;
710 sz = rpl->MsgLength * 4;
714 if (sz > cmd->rpl_len) {
715 mpr_printf(sc, "%s: user reply buffer (%d) smaller than "
716 "returned buffer (%d)\n", __func__, cmd->rpl_len, sz);
721 copyout(rpl, cmd->rpl, sz);
723 copyout(buf, cmd->buf, cmd->len);
724 mpr_dprint(sc, MPR_USER, "%s: reply size %d\n", __func__, sz);
730 mpr_free_command(sc, cm);
733 free(buf, M_MPRUSER);
738 mpr_user_pass_thru(struct mpr_softc *sc, mpr_pass_thru_t *data)
740 MPI2_REQUEST_HEADER *hdr, tmphdr;
741 MPI2_DEFAULT_REPLY *rpl;
742 Mpi26NVMeEncapsulatedErrorReply_t *nvme_error_reply = NULL;
743 Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
744 struct mpr_command *cm = NULL;
745 int i, err = 0, dir = 0, sz;
746 uint8_t tool, function = 0;
748 struct mprsas_target *targ = NULL;
751 * Only allow one passthru command at a time. Use the MPR_FLAGS_BUSY
752 * bit to denote that a passthru is being processed.
755 if (sc->mpr_flags & MPR_FLAGS_BUSY) {
756 mpr_dprint(sc, MPR_USER, "%s: Only one passthru command "
757 "allowed at a single time.", __func__);
761 sc->mpr_flags |= MPR_FLAGS_BUSY;
765 * Do some validation on data direction. Valid cases are:
766 * 1) DataSize is 0 and direction is NONE
767 * 2) DataSize is non-zero and one of:
768 * a) direction is READ or
769 * b) direction is WRITE or
770 * c) direction is BOTH and DataOutSize is non-zero
771 * If valid and the direction is BOTH, change the direction to READ.
772 * if valid and the direction is not BOTH, make sure DataOutSize is 0.
774 if (((data->DataSize == 0) &&
775 (data->DataDirection == MPR_PASS_THRU_DIRECTION_NONE)) ||
776 ((data->DataSize != 0) &&
777 ((data->DataDirection == MPR_PASS_THRU_DIRECTION_READ) ||
778 (data->DataDirection == MPR_PASS_THRU_DIRECTION_WRITE) ||
779 ((data->DataDirection == MPR_PASS_THRU_DIRECTION_BOTH) &&
780 (data->DataOutSize != 0))))) {
781 if (data->DataDirection == MPR_PASS_THRU_DIRECTION_BOTH)
782 data->DataDirection = MPR_PASS_THRU_DIRECTION_READ;
784 data->DataOutSize = 0;
787 goto RetFreeUnlocked;
790 mpr_dprint(sc, MPR_USER, "%s: req 0x%jx %d rpl 0x%jx %d "
791 "data in 0x%jx %d data out 0x%jx %d data dir %d\n", __func__,
792 data->PtrRequest, data->RequestSize, data->PtrReply,
793 data->ReplySize, data->PtrData, data->DataSize,
794 data->PtrDataOut, data->DataOutSize, data->DataDirection);
797 * copy in the header so we know what we're dealing with before we
798 * commit to allocating a command for it.
800 err = copyin(PTRIN(data->PtrRequest), &tmphdr, data->RequestSize);
802 goto RetFreeUnlocked;
804 if (data->RequestSize > (int)sc->reqframesz) {
806 goto RetFreeUnlocked;
809 function = tmphdr.Function;
810 mpr_dprint(sc, MPR_USER, "%s: Function %02X MsgFlags %02X\n", __func__,
811 function, tmphdr.MsgFlags);
814 * Handle a passthru TM request.
816 if (function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
817 MPI2_SCSI_TASK_MANAGE_REQUEST *task;
820 cm = mprsas_alloc_tm(sc);
826 /* Copy the header in. Only a small fixup is needed. */
827 task = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
828 bcopy(&tmphdr, task, data->RequestSize);
829 task->TaskMID = cm->cm_desc.Default.SMID;
832 cm->cm_complete = NULL;
833 cm->cm_complete_data = NULL;
835 targ = mprsas_find_target_by_handle(sc->sassc, 0,
838 mpr_dprint(sc, MPR_INFO,
839 "%s %d : invalid handle for requested TM 0x%x \n",
840 __func__, __LINE__, task->DevHandle);
843 mprsas_prepare_for_tm(sc, cm, targ, CAM_LUN_WILDCARD);
844 err = mpr_wait_command(sc, &cm, 30, CAN_SLEEP);
849 mpr_dprint(sc, MPR_FAULT, "%s: task management failed",
853 * Copy the reply data and sense data to user space.
855 if ((cm != NULL) && (cm->cm_reply != NULL)) {
856 rpl = (MPI2_DEFAULT_REPLY *)cm->cm_reply;
857 sz = rpl->MsgLength * 4;
859 if (sz > data->ReplySize) {
860 mpr_printf(sc, "%s: user reply buffer (%d) "
861 "smaller than returned buffer (%d)\n",
862 __func__, data->ReplySize, sz);
865 copyout(cm->cm_reply, PTRIN(data->PtrReply),
869 mprsas_free_tm(sc, cm);
874 cm = mpr_alloc_command(sc);
877 mpr_printf(sc, "%s: no mpr requests\n", __func__);
883 hdr = (MPI2_REQUEST_HEADER *)cm->cm_req;
884 bcopy(&tmphdr, hdr, data->RequestSize);
887 * Do some checking to make sure the IOCTL request contains a valid
888 * request. Then set the SGL info.
890 mpr_init_sge(cm, hdr, (void *)((uint8_t *)hdr + data->RequestSize));
893 * Set up for read, write or both. From check above, DataOutSize will
894 * be 0 if direction is READ or WRITE, but it will have some non-zero
895 * value if the direction is BOTH. So, just use the biggest size to get
896 * the cm_data buffer size. If direction is BOTH, 2 SGLs need to be set
897 * up; the first is for the request and the second will contain the
898 * response data. cm_out_len needs to be set here and this will be used
899 * when the SGLs are set up.
902 cm->cm_length = MAX(data->DataSize, data->DataOutSize);
903 cm->cm_out_len = data->DataOutSize;
905 if (cm->cm_length != 0) {
906 cm->cm_data = malloc(cm->cm_length, M_MPRUSER, M_WAITOK |
908 cm->cm_flags = MPR_CM_FLAGS_DATAIN;
909 if (data->DataOutSize) {
910 cm->cm_flags |= MPR_CM_FLAGS_DATAOUT;
911 err = copyin(PTRIN(data->PtrDataOut),
912 cm->cm_data, data->DataOutSize);
913 } else if (data->DataDirection ==
914 MPR_PASS_THRU_DIRECTION_WRITE) {
915 cm->cm_flags = MPR_CM_FLAGS_DATAOUT;
916 err = copyin(PTRIN(data->PtrData),
917 cm->cm_data, data->DataSize);
920 mpr_dprint(sc, MPR_FAULT, "%s: failed to copy IOCTL "
921 "data from user space\n", __func__);
924 * Set this flag only if processing a command that does not need an
925 * IEEE SGL. The CLI Tool within the Toolbox uses IEEE SGLs, so clear
926 * the flag only for that tool if processing a Toolbox function.
928 cm->cm_flags |= MPR_CM_FLAGS_SGE_SIMPLE;
929 for (i = 0; i < sizeof (ieee_sgl_func_list); i++) {
930 if (function == ieee_sgl_func_list[i]) {
931 if (function == MPI2_FUNCTION_TOOLBOX)
933 tool = (uint8_t)hdr->FunctionDependent1;
934 if (tool != MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL)
937 cm->cm_flags &= ~MPR_CM_FLAGS_SGE_SIMPLE;
941 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
943 if (function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
945 (Mpi26NVMeEncapsulatedRequest_t *)cm->cm_req;
946 cm->cm_desc.Default.RequestFlags =
947 MPI26_REQ_DESCRIPT_FLAGS_PCIE_ENCAPSULATED;
950 * Get the Physical Address of the sense buffer.
951 * Save the user's Error Response buffer address and use that
952 * field to hold the sense buffer address.
953 * Clear the internal sense buffer, which will potentially hold
954 * the Completion Queue Entry on return, or 0 if no Entry.
955 * Build the PRPs and set direction bits.
958 cm->nvme_error_response =
959 (uint64_t *)(uintptr_t)(((uint64_t)nvme_encap_request->
960 ErrorResponseBaseAddress.High << 32) |
961 (uint64_t)nvme_encap_request->
962 ErrorResponseBaseAddress.Low);
963 nvme_encap_request->ErrorResponseBaseAddress.High =
964 htole32((uint32_t)((uint64_t)cm->cm_sense_busaddr >> 32));
965 nvme_encap_request->ErrorResponseBaseAddress.Low =
966 htole32(cm->cm_sense_busaddr);
967 memset(cm->cm_sense, 0, NVME_ERROR_RESPONSE_SIZE);
968 mpr_build_nvme_prp(sc, cm, nvme_encap_request, cm->cm_data,
969 data->DataSize, data->DataOutSize);
973 * Set up Sense buffer and SGL offset for IO passthru. SCSI IO request
974 * uses SCSI IO or Fast Path SCSI IO descriptor.
976 if ((function == MPI2_FUNCTION_SCSI_IO_REQUEST) ||
977 (function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
978 MPI2_SCSI_IO_REQUEST *scsi_io_req;
980 scsi_io_req = (MPI2_SCSI_IO_REQUEST *)hdr;
982 * Put SGE for data and data_out buffer at the end of
983 * scsi_io_request message header (64 bytes in total).
984 * Following above SGEs, the residual space will be used by
987 scsi_io_req->SenseBufferLength = (uint8_t)(data->RequestSize -
989 scsi_io_req->SenseBufferLowAddress =
990 htole32(cm->cm_sense_busaddr);
993 * Set SGLOffset0 value. This is the number of dwords that SGL
994 * is offset from the beginning of MPI2_SCSI_IO_REQUEST struct.
996 scsi_io_req->SGLOffset0 = 24;
999 * Setup descriptor info. RAID passthrough must use the
1000 * default request descriptor which is already set, so if this
1001 * is a SCSI IO request, change the descriptor to SCSI IO or
1002 * Fast Path SCSI IO. Also, if this is a SCSI IO request,
1003 * handle the reply in the mprsas_scsio_complete function.
1005 if (function == MPI2_FUNCTION_SCSI_IO_REQUEST) {
1006 targ = mprsas_find_target_by_handle(sc->sassc, 0,
1007 scsi_io_req->DevHandle);
1010 printf("No Target found for handle %d\n",
1011 scsi_io_req->DevHandle);
1013 goto RetFreeUnlocked;
1016 if (targ->scsi_req_desc_type ==
1017 MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO) {
1018 cm->cm_desc.FastPathSCSIIO.RequestFlags =
1019 MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
1020 if (!sc->atomic_desc_capable) {
1021 cm->cm_desc.FastPathSCSIIO.DevHandle =
1022 scsi_io_req->DevHandle;
1024 scsi_io_req->IoFlags |=
1025 MPI25_SCSIIO_IOFLAGS_FAST_PATH;
1027 cm->cm_desc.SCSIIO.RequestFlags =
1028 MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
1029 if (!sc->atomic_desc_capable) {
1030 cm->cm_desc.SCSIIO.DevHandle =
1031 scsi_io_req->DevHandle;
1036 * Make sure the DevHandle is not 0 because this is a
1039 if (scsi_io_req->DevHandle == 0) {
1041 goto RetFreeUnlocked;
1048 err = mpr_wait_command(sc, &cm, 30, CAN_SLEEP);
1050 if (err || (cm == NULL)) {
1051 mpr_printf(sc, "%s: invalid request: error %d\n", __func__,
1057 * Sync the DMA data, if any. Then copy the data to user space.
1059 if (cm->cm_data != NULL) {
1060 if (cm->cm_flags & MPR_CM_FLAGS_DATAIN)
1061 dir = BUS_DMASYNC_POSTREAD;
1062 else if (cm->cm_flags & MPR_CM_FLAGS_DATAOUT)
1063 dir = BUS_DMASYNC_POSTWRITE;
1064 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir);
1065 bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
1067 if (cm->cm_flags & MPR_CM_FLAGS_DATAIN) {
1069 err = copyout(cm->cm_data,
1070 PTRIN(data->PtrData), data->DataSize);
1073 mpr_dprint(sc, MPR_FAULT, "%s: failed to copy "
1074 "IOCTL data to user space\n", __func__);
1079 * Copy the reply data and sense data to user space.
1081 if (cm->cm_reply != NULL) {
1082 rpl = (MPI2_DEFAULT_REPLY *)cm->cm_reply;
1083 sz = rpl->MsgLength * 4;
1085 if (sz > data->ReplySize) {
1086 mpr_printf(sc, "%s: user reply buffer (%d) smaller "
1087 "than returned buffer (%d)\n", __func__,
1088 data->ReplySize, sz);
1091 copyout(cm->cm_reply, PTRIN(data->PtrReply), data->ReplySize);
1094 if ((function == MPI2_FUNCTION_SCSI_IO_REQUEST) ||
1095 (function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
1096 if (((MPI2_SCSI_IO_REPLY *)rpl)->SCSIState &
1097 MPI2_SCSI_STATE_AUTOSENSE_VALID) {
1099 MIN((le32toh(((MPI2_SCSI_IO_REPLY *)rpl)->
1100 SenseCount)), sizeof(struct
1103 copyout(cm->cm_sense, (PTRIN(data->PtrReply +
1104 sizeof(MPI2_SCSI_IO_REPLY))), sense_len);
1110 * Copy out the NVMe Error Reponse to user. The Error Response
1111 * buffer is given by the user, but a sense buffer is used to
1112 * get that data from the IOC. The user's
1113 * ErrorResponseBaseAddress is saved in the
1114 * 'nvme_error_response' field before the command because that
1115 * field is set to a sense buffer. When the command is
1116 * complete, the Error Response data from the IOC is copied to
1117 * that user address after it is checked for validity.
1118 * Also note that 'sense' buffers are not defined for
1119 * NVMe commands. Sense terminalogy is only used here so that
1120 * the same IOCTL structure and sense buffers can be used for
1123 if (function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
1124 if (cm->nvme_error_response == NULL) {
1125 mpr_dprint(sc, MPR_INFO, "NVMe Error Response "
1126 "buffer is NULL. Response data will not be "
1129 goto RetFreeUnlocked;
1133 (Mpi26NVMeEncapsulatedErrorReply_t *)cm->cm_reply;
1134 sz = MIN(le32toh(nvme_error_reply->ErrorResponseCount),
1135 NVME_ERROR_RESPONSE_SIZE);
1137 copyout(cm->cm_sense,
1138 (PTRIN(data->PtrReply +
1139 sizeof(MPI2_SCSI_IO_REPLY))), sz);
1151 free(cm->cm_data, M_MPRUSER);
1152 mpr_free_command(sc, cm);
1155 sc->mpr_flags &= ~MPR_FLAGS_BUSY;
1162 mpr_user_get_adapter_data(struct mpr_softc *sc, mpr_adapter_data_t *data)
1164 Mpi2ConfigReply_t mpi_reply;
1165 Mpi2BiosPage3_t config_page;
1168 * Use the PCI interface functions to get the Bus, Device, and Function
1171 data->PciInformation.u.bits.BusNumber = pci_get_bus(sc->mpr_dev);
1172 data->PciInformation.u.bits.DeviceNumber = pci_get_slot(sc->mpr_dev);
1173 data->PciInformation.u.bits.FunctionNumber =
1174 pci_get_function(sc->mpr_dev);
1177 * Get the FW version that should already be saved in IOC Facts.
1179 data->MpiFirmwareVersion = sc->facts->FWVersion.Word;
1182 * General device info.
1184 if (sc->mpr_flags & MPR_FLAGS_GEN35_IOC)
1185 data->AdapterType = MPRIOCTL_ADAPTER_TYPE_SAS35;
1187 data->AdapterType = MPRIOCTL_ADAPTER_TYPE_SAS3;
1188 data->PCIDeviceHwId = pci_get_device(sc->mpr_dev);
1189 data->PCIDeviceHwRev = pci_read_config(sc->mpr_dev, PCIR_REVID, 1);
1190 data->SubSystemId = pci_get_subdevice(sc->mpr_dev);
1191 data->SubsystemVendorId = pci_get_subvendor(sc->mpr_dev);
1194 * Get the driver version.
1196 strcpy((char *)&data->DriverVersion[0], MPR_DRIVER_VERSION);
1199 * Need to get BIOS Config Page 3 for the BIOS Version.
1201 data->BiosVersion = 0;
1203 if (mpr_config_get_bios_pg3(sc, &mpi_reply, &config_page))
1204 printf("%s: Error while retrieving BIOS Version\n", __func__);
1206 data->BiosVersion = config_page.BiosVersion;
1211 mpr_user_read_pci_info(struct mpr_softc *sc, mpr_pci_info_t *data)
1216 * Use the PCI interface functions to get the Bus, Device, and Function
1219 data->BusNumber = pci_get_bus(sc->mpr_dev);
1220 data->DeviceNumber = pci_get_slot(sc->mpr_dev);
1221 data->FunctionNumber = pci_get_function(sc->mpr_dev);
1224 * Now get the interrupt vector and the pci header. The vector can
1225 * only be 0 right now. The header is the first 256 bytes of config
1228 data->InterruptVector = 0;
1229 for (i = 0; i < sizeof (data->PciHeader); i++) {
1230 data->PciHeader[i] = pci_read_config(sc->mpr_dev, i, 1);
1235 mpr_get_fw_diag_buffer_number(struct mpr_softc *sc, uint32_t unique_id)
1239 for (index = 0; index < MPI2_DIAG_BUF_TYPE_COUNT; index++) {
1240 if (sc->fw_diag_buffer_list[index].unique_id == unique_id) {
1245 return (MPR_FW_DIAGNOSTIC_UID_NOT_FOUND);
1249 mpr_post_fw_diag_buffer(struct mpr_softc *sc,
1250 mpr_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code)
1252 MPI2_DIAG_BUFFER_POST_REQUEST *req;
1253 MPI2_DIAG_BUFFER_POST_REPLY *reply;
1254 struct mpr_command *cm = NULL;
1258 * If buffer is not enabled, just leave.
1260 *return_code = MPR_FW_DIAG_ERROR_POST_FAILED;
1261 if (!pBuffer->enabled) {
1262 return (MPR_DIAG_FAILURE);
1266 * Clear some flags initially.
1268 pBuffer->force_release = FALSE;
1269 pBuffer->valid_data = FALSE;
1270 pBuffer->owned_by_firmware = FALSE;
1275 cm = mpr_alloc_command(sc);
1277 mpr_printf(sc, "%s: no mpr requests\n", __func__);
1278 return (MPR_DIAG_FAILURE);
1282 * Build the request for releasing the FW Diag Buffer and send it.
1284 req = (MPI2_DIAG_BUFFER_POST_REQUEST *)cm->cm_req;
1285 req->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1286 req->BufferType = pBuffer->buffer_type;
1287 req->ExtendedType = pBuffer->extended_type;
1288 req->BufferLength = pBuffer->size;
1289 for (i = 0; i < (sizeof(req->ProductSpecific) / 4); i++)
1290 req->ProductSpecific[i] = pBuffer->product_specific[i];
1291 mpr_from_u64(sc->fw_diag_busaddr, &req->BufferAddress);
1294 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1295 cm->cm_complete_data = NULL;
1298 * Send command synchronously.
1300 status = mpr_wait_command(sc, &cm, 30, CAN_SLEEP);
1301 if (status || (cm == NULL)) {
1302 mpr_printf(sc, "%s: invalid request: error %d\n", __func__,
1304 status = MPR_DIAG_FAILURE;
1309 * Process POST reply.
1311 reply = (MPI2_DIAG_BUFFER_POST_REPLY *)cm->cm_reply;
1312 if (reply == NULL) {
1313 mpr_printf(sc, "%s: reply is NULL, probably due to "
1314 "reinitialization", __func__);
1315 status = MPR_DIAG_FAILURE;
1319 if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
1320 MPI2_IOCSTATUS_SUCCESS) {
1321 status = MPR_DIAG_FAILURE;
1322 mpr_dprint(sc, MPR_FAULT, "%s: post of FW Diag Buffer failed "
1323 "with IOCStatus = 0x%x, IOCLogInfo = 0x%x and "
1324 "TransferLength = 0x%x\n", __func__,
1325 le16toh(reply->IOCStatus), le32toh(reply->IOCLogInfo),
1326 le32toh(reply->TransferLength));
1331 * Post was successful.
1333 pBuffer->valid_data = TRUE;
1334 pBuffer->owned_by_firmware = TRUE;
1335 *return_code = MPR_FW_DIAG_ERROR_SUCCESS;
1336 status = MPR_DIAG_SUCCESS;
1340 mpr_free_command(sc, cm);
1345 mpr_release_fw_diag_buffer(struct mpr_softc *sc,
1346 mpr_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code,
1349 MPI2_DIAG_RELEASE_REQUEST *req;
1350 MPI2_DIAG_RELEASE_REPLY *reply;
1351 struct mpr_command *cm = NULL;
1355 * If buffer is not enabled, just leave.
1357 *return_code = MPR_FW_DIAG_ERROR_RELEASE_FAILED;
1358 if (!pBuffer->enabled) {
1359 mpr_dprint(sc, MPR_USER, "%s: This buffer type is not "
1360 "supported by the IOC", __func__);
1361 return (MPR_DIAG_FAILURE);
1365 * Clear some flags initially.
1367 pBuffer->force_release = FALSE;
1368 pBuffer->valid_data = FALSE;
1369 pBuffer->owned_by_firmware = FALSE;
1374 cm = mpr_alloc_command(sc);
1376 mpr_printf(sc, "%s: no mpr requests\n", __func__);
1377 return (MPR_DIAG_FAILURE);
1381 * Build the request for releasing the FW Diag Buffer and send it.
1383 req = (MPI2_DIAG_RELEASE_REQUEST *)cm->cm_req;
1384 req->Function = MPI2_FUNCTION_DIAG_RELEASE;
1385 req->BufferType = pBuffer->buffer_type;
1388 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1389 cm->cm_complete_data = NULL;
1392 * Send command synchronously.
1394 status = mpr_wait_command(sc, &cm, 30, CAN_SLEEP);
1395 if (status || (cm == NULL)) {
1396 mpr_printf(sc, "%s: invalid request: error %d\n", __func__,
1398 status = MPR_DIAG_FAILURE;
1403 * Process RELEASE reply.
1405 reply = (MPI2_DIAG_RELEASE_REPLY *)cm->cm_reply;
1406 if (reply == NULL) {
1407 mpr_printf(sc, "%s: reply is NULL, probably due to "
1408 "reinitialization", __func__);
1409 status = MPR_DIAG_FAILURE;
1412 if (((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
1413 MPI2_IOCSTATUS_SUCCESS) || pBuffer->owned_by_firmware) {
1414 status = MPR_DIAG_FAILURE;
1415 mpr_dprint(sc, MPR_FAULT, "%s: release of FW Diag Buffer "
1416 "failed with IOCStatus = 0x%x and IOCLogInfo = 0x%x\n",
1417 __func__, le16toh(reply->IOCStatus),
1418 le32toh(reply->IOCLogInfo));
1423 * Release was successful.
1425 *return_code = MPR_FW_DIAG_ERROR_SUCCESS;
1426 status = MPR_DIAG_SUCCESS;
1429 * If this was for an UNREGISTER diag type command, clear the unique ID.
1431 if (diag_type == MPR_FW_DIAG_TYPE_UNREGISTER) {
1432 pBuffer->unique_id = MPR_FW_DIAG_INVALID_UID;
1437 mpr_free_command(sc, cm);
1443 mpr_diag_register(struct mpr_softc *sc, mpr_fw_diag_register_t *diag_register,
1444 uint32_t *return_code)
1446 bus_dma_tag_template_t t;
1447 mpr_fw_diagnostic_buffer_t *pBuffer;
1448 struct mpr_busdma_context *ctx;
1449 uint8_t extended_type, buffer_type, i;
1450 uint32_t buffer_size;
1455 extended_type = diag_register->ExtendedType;
1456 buffer_type = diag_register->BufferType;
1457 buffer_size = diag_register->RequestedBufferSize;
1458 unique_id = diag_register->UniqueId;
1463 * Check for valid buffer type
1465 if (buffer_type >= MPI2_DIAG_BUF_TYPE_COUNT) {
1466 *return_code = MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1467 return (MPR_DIAG_FAILURE);
1471 * Get the current buffer and look up the unique ID. The unique ID
1472 * should not be found. If it is, the ID is already in use.
1474 i = mpr_get_fw_diag_buffer_number(sc, unique_id);
1475 pBuffer = &sc->fw_diag_buffer_list[buffer_type];
1476 if (i != MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1477 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1478 return (MPR_DIAG_FAILURE);
1482 * The buffer's unique ID should not be registered yet, and the given
1483 * unique ID cannot be 0.
1485 if ((pBuffer->unique_id != MPR_FW_DIAG_INVALID_UID) ||
1486 (unique_id == MPR_FW_DIAG_INVALID_UID)) {
1487 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1488 return (MPR_DIAG_FAILURE);
1492 * If this buffer is already posted as immediate, just change owner.
1494 if (pBuffer->immediate && pBuffer->owned_by_firmware &&
1495 (pBuffer->unique_id == MPR_FW_DIAG_INVALID_UID)) {
1496 pBuffer->immediate = FALSE;
1497 pBuffer->unique_id = unique_id;
1498 return (MPR_DIAG_SUCCESS);
1502 * Post a new buffer after checking if it's enabled. The DMA buffer
1503 * that is allocated will be contiguous (nsegments = 1).
1505 if (!pBuffer->enabled) {
1506 *return_code = MPR_FW_DIAG_ERROR_NO_BUFFER;
1507 return (MPR_DIAG_FAILURE);
1509 bus_dma_template_init(&t, sc->mpr_parent_dmat);
1510 t.lowaddr = BUS_SPACE_MAXADDR_32BIT;
1511 t.maxsize = t.maxsegsize = buffer_size;
1513 if (bus_dma_template_tag(&t, &sc->fw_diag_dmat)) {
1514 mpr_dprint(sc, MPR_ERROR,
1515 "Cannot allocate FW diag buffer DMA tag\n");
1516 *return_code = MPR_FW_DIAG_ERROR_NO_BUFFER;
1517 status = MPR_DIAG_FAILURE;
1520 if (bus_dmamem_alloc(sc->fw_diag_dmat, (void **)&sc->fw_diag_buffer,
1521 BUS_DMA_NOWAIT, &sc->fw_diag_map)) {
1522 mpr_dprint(sc, MPR_ERROR,
1523 "Cannot allocate FW diag buffer memory\n");
1524 *return_code = MPR_FW_DIAG_ERROR_NO_BUFFER;
1525 status = MPR_DIAG_FAILURE;
1528 bzero(sc->fw_diag_buffer, buffer_size);
1530 ctx = malloc(sizeof(*ctx), M_MPR, M_WAITOK | M_ZERO);
1532 device_printf(sc->mpr_dev, "%s: context malloc failed\n",
1534 *return_code = MPR_FW_DIAG_ERROR_NO_BUFFER;
1535 status = MPR_DIAG_FAILURE;
1538 ctx->addr = &sc->fw_diag_busaddr;
1539 ctx->buffer_dmat = sc->fw_diag_dmat;
1540 ctx->buffer_dmamap = sc->fw_diag_map;
1542 error = bus_dmamap_load(sc->fw_diag_dmat, sc->fw_diag_map,
1543 sc->fw_diag_buffer, buffer_size, mpr_memaddr_wait_cb,
1545 if (error == EINPROGRESS) {
1548 device_printf(sc->mpr_dev, "%s: Deferred bus_dmamap_load\n",
1551 * Wait for the load to complete. If we're interrupted,
1555 if (ctx->completed == 0) {
1556 error = msleep(ctx, &sc->mpr_mtx, PCATCH, "mprwait", 0);
1559 * We got an error from msleep(9). This is
1560 * most likely due to a signal. Tell
1561 * mpr_memaddr_wait_cb() that we've abandoned
1562 * the context, so it needs to clean up when
1567 /* The callback will free this memory */
1571 device_printf(sc->mpr_dev, "Cannot "
1572 "bus_dmamap_load FW diag buffer, error = "
1573 "%d returned from msleep\n", error);
1574 *return_code = MPR_FW_DIAG_ERROR_NO_BUFFER;
1575 status = MPR_DIAG_FAILURE;
1582 if ((error != 0) || (ctx->error != 0)) {
1583 device_printf(sc->mpr_dev, "Cannot bus_dmamap_load FW diag "
1584 "buffer, %serror = %d\n", error ? "" : "callback ",
1585 error ? error : ctx->error);
1586 *return_code = MPR_FW_DIAG_ERROR_NO_BUFFER;
1587 status = MPR_DIAG_FAILURE;
1591 bus_dmamap_sync(sc->fw_diag_dmat, sc->fw_diag_map, BUS_DMASYNC_PREREAD);
1593 pBuffer->size = buffer_size;
1596 * Copy the given info to the diag buffer and post the buffer.
1598 pBuffer->buffer_type = buffer_type;
1599 pBuffer->immediate = FALSE;
1600 if (buffer_type == MPI2_DIAG_BUF_TYPE_TRACE) {
1601 for (i = 0; i < (sizeof (pBuffer->product_specific) / 4);
1603 pBuffer->product_specific[i] =
1604 diag_register->ProductSpecific[i];
1607 pBuffer->extended_type = extended_type;
1608 pBuffer->unique_id = unique_id;
1609 status = mpr_post_fw_diag_buffer(sc, pBuffer, return_code);
1614 * In case there was a failure, free the DMA buffer.
1616 if (status == MPR_DIAG_FAILURE) {
1617 if (sc->fw_diag_busaddr != 0) {
1618 bus_dmamap_unload(sc->fw_diag_dmat, sc->fw_diag_map);
1619 sc->fw_diag_busaddr = 0;
1621 if (sc->fw_diag_buffer != NULL) {
1622 bus_dmamem_free(sc->fw_diag_dmat, sc->fw_diag_buffer,
1624 sc->fw_diag_buffer = NULL;
1626 if (sc->fw_diag_dmat != NULL) {
1627 bus_dma_tag_destroy(sc->fw_diag_dmat);
1628 sc->fw_diag_dmat = NULL;
1639 mpr_diag_unregister(struct mpr_softc *sc,
1640 mpr_fw_diag_unregister_t *diag_unregister, uint32_t *return_code)
1642 mpr_fw_diagnostic_buffer_t *pBuffer;
1647 unique_id = diag_unregister->UniqueId;
1650 * Get the current buffer and look up the unique ID. The unique ID
1653 i = mpr_get_fw_diag_buffer_number(sc, unique_id);
1654 if (i == MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1655 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1656 return (MPR_DIAG_FAILURE);
1659 pBuffer = &sc->fw_diag_buffer_list[i];
1662 * Try to release the buffer from FW before freeing it. If release
1663 * fails, don't free the DMA buffer in case FW tries to access it
1664 * later. If buffer is not owned by firmware, can't release it.
1666 if (!pBuffer->owned_by_firmware) {
1667 status = MPR_DIAG_SUCCESS;
1669 status = mpr_release_fw_diag_buffer(sc, pBuffer, return_code,
1670 MPR_FW_DIAG_TYPE_UNREGISTER);
1674 * At this point, return the current status no matter what happens with
1677 pBuffer->unique_id = MPR_FW_DIAG_INVALID_UID;
1678 if (status == MPR_DIAG_SUCCESS) {
1679 if (sc->fw_diag_busaddr != 0) {
1680 bus_dmamap_unload(sc->fw_diag_dmat, sc->fw_diag_map);
1681 sc->fw_diag_busaddr = 0;
1683 if (sc->fw_diag_buffer != NULL) {
1684 bus_dmamem_free(sc->fw_diag_dmat, sc->fw_diag_buffer,
1686 sc->fw_diag_buffer = NULL;
1688 if (sc->fw_diag_dmat != NULL) {
1689 bus_dma_tag_destroy(sc->fw_diag_dmat);
1690 sc->fw_diag_dmat = NULL;
1698 mpr_diag_query(struct mpr_softc *sc, mpr_fw_diag_query_t *diag_query,
1699 uint32_t *return_code)
1701 mpr_fw_diagnostic_buffer_t *pBuffer;
1705 unique_id = diag_query->UniqueId;
1708 * If ID is valid, query on ID.
1709 * If ID is invalid, query on buffer type.
1711 if (unique_id == MPR_FW_DIAG_INVALID_UID) {
1712 i = diag_query->BufferType;
1713 if (i >= MPI2_DIAG_BUF_TYPE_COUNT) {
1714 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1715 return (MPR_DIAG_FAILURE);
1718 i = mpr_get_fw_diag_buffer_number(sc, unique_id);
1719 if (i == MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1720 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1721 return (MPR_DIAG_FAILURE);
1726 * Fill query structure with the diag buffer info.
1728 pBuffer = &sc->fw_diag_buffer_list[i];
1729 diag_query->BufferType = pBuffer->buffer_type;
1730 diag_query->ExtendedType = pBuffer->extended_type;
1731 if (diag_query->BufferType == MPI2_DIAG_BUF_TYPE_TRACE) {
1732 for (i = 0; i < (sizeof(diag_query->ProductSpecific) / 4);
1734 diag_query->ProductSpecific[i] =
1735 pBuffer->product_specific[i];
1738 diag_query->TotalBufferSize = pBuffer->size;
1739 diag_query->DriverAddedBufferSize = 0;
1740 diag_query->UniqueId = pBuffer->unique_id;
1741 diag_query->ApplicationFlags = 0;
1742 diag_query->DiagnosticFlags = 0;
1745 * Set/Clear application flags
1747 if (pBuffer->immediate) {
1748 diag_query->ApplicationFlags &= ~MPR_FW_DIAG_FLAG_APP_OWNED;
1750 diag_query->ApplicationFlags |= MPR_FW_DIAG_FLAG_APP_OWNED;
1752 if (pBuffer->valid_data || pBuffer->owned_by_firmware) {
1753 diag_query->ApplicationFlags |= MPR_FW_DIAG_FLAG_BUFFER_VALID;
1755 diag_query->ApplicationFlags &= ~MPR_FW_DIAG_FLAG_BUFFER_VALID;
1757 if (pBuffer->owned_by_firmware) {
1758 diag_query->ApplicationFlags |=
1759 MPR_FW_DIAG_FLAG_FW_BUFFER_ACCESS;
1761 diag_query->ApplicationFlags &=
1762 ~MPR_FW_DIAG_FLAG_FW_BUFFER_ACCESS;
1765 return (MPR_DIAG_SUCCESS);
1769 mpr_diag_read_buffer(struct mpr_softc *sc,
1770 mpr_diag_read_buffer_t *diag_read_buffer, uint8_t *ioctl_buf,
1771 uint32_t *return_code)
1773 mpr_fw_diagnostic_buffer_t *pBuffer;
1778 unique_id = diag_read_buffer->UniqueId;
1781 * Get the current buffer and look up the unique ID. The unique ID
1784 i = mpr_get_fw_diag_buffer_number(sc, unique_id);
1785 if (i == MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1786 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1787 return (MPR_DIAG_FAILURE);
1790 pBuffer = &sc->fw_diag_buffer_list[i];
1793 * Make sure requested read is within limits
1795 if (diag_read_buffer->StartingOffset + diag_read_buffer->BytesToRead >
1797 *return_code = MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1798 return (MPR_DIAG_FAILURE);
1801 /* Sync the DMA map before we copy to userland. */
1802 bus_dmamap_sync(sc->fw_diag_dmat, sc->fw_diag_map,
1803 BUS_DMASYNC_POSTREAD);
1806 * Copy the requested data from DMA to the diag_read_buffer. The DMA
1807 * buffer that was allocated is one contiguous buffer.
1809 pData = (uint8_t *)(sc->fw_diag_buffer +
1810 diag_read_buffer->StartingOffset);
1811 if (copyout(pData, ioctl_buf, diag_read_buffer->BytesToRead) != 0)
1812 return (MPR_DIAG_FAILURE);
1813 diag_read_buffer->Status = 0;
1816 * Set or clear the Force Release flag.
1818 if (pBuffer->force_release) {
1819 diag_read_buffer->Flags |= MPR_FW_DIAG_FLAG_FORCE_RELEASE;
1821 diag_read_buffer->Flags &= ~MPR_FW_DIAG_FLAG_FORCE_RELEASE;
1825 * If buffer is to be reregistered, make sure it's not already owned by
1828 status = MPR_DIAG_SUCCESS;
1829 if (!pBuffer->owned_by_firmware) {
1830 if (diag_read_buffer->Flags & MPR_FW_DIAG_FLAG_REREGISTER) {
1831 status = mpr_post_fw_diag_buffer(sc, pBuffer,
1840 mpr_diag_release(struct mpr_softc *sc, mpr_fw_diag_release_t *diag_release,
1841 uint32_t *return_code)
1843 mpr_fw_diagnostic_buffer_t *pBuffer;
1848 unique_id = diag_release->UniqueId;
1851 * Get the current buffer and look up the unique ID. The unique ID
1854 i = mpr_get_fw_diag_buffer_number(sc, unique_id);
1855 if (i == MPR_FW_DIAGNOSTIC_UID_NOT_FOUND) {
1856 *return_code = MPR_FW_DIAG_ERROR_INVALID_UID;
1857 return (MPR_DIAG_FAILURE);
1860 pBuffer = &sc->fw_diag_buffer_list[i];
1863 * If buffer is not owned by firmware, it's already been released.
1865 if (!pBuffer->owned_by_firmware) {
1866 *return_code = MPR_FW_DIAG_ERROR_ALREADY_RELEASED;
1867 return (MPR_DIAG_FAILURE);
1871 * Release the buffer.
1873 status = mpr_release_fw_diag_buffer(sc, pBuffer, return_code,
1874 MPR_FW_DIAG_TYPE_RELEASE);
1879 mpr_do_diag_action(struct mpr_softc *sc, uint32_t action, uint8_t *diag_action,
1880 uint32_t length, uint32_t *return_code)
1882 mpr_fw_diag_register_t diag_register;
1883 mpr_fw_diag_unregister_t diag_unregister;
1884 mpr_fw_diag_query_t diag_query;
1885 mpr_diag_read_buffer_t diag_read_buffer;
1886 mpr_fw_diag_release_t diag_release;
1887 int status = MPR_DIAG_SUCCESS;
1888 uint32_t original_return_code;
1890 original_return_code = *return_code;
1891 *return_code = MPR_FW_DIAG_ERROR_SUCCESS;
1894 case MPR_FW_DIAG_TYPE_REGISTER:
1897 MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1898 status = MPR_DIAG_FAILURE;
1901 if (copyin(diag_action, &diag_register,
1902 sizeof(diag_register)) != 0)
1903 return (MPR_DIAG_FAILURE);
1904 status = mpr_diag_register(sc, &diag_register,
1908 case MPR_FW_DIAG_TYPE_UNREGISTER:
1909 if (length < sizeof(diag_unregister)) {
1911 MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1912 status = MPR_DIAG_FAILURE;
1915 if (copyin(diag_action, &diag_unregister,
1916 sizeof(diag_unregister)) != 0)
1917 return (MPR_DIAG_FAILURE);
1918 status = mpr_diag_unregister(sc, &diag_unregister,
1922 case MPR_FW_DIAG_TYPE_QUERY:
1923 if (length < sizeof (diag_query)) {
1925 MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1926 status = MPR_DIAG_FAILURE;
1929 if (copyin(diag_action, &diag_query, sizeof(diag_query))
1931 return (MPR_DIAG_FAILURE);
1932 status = mpr_diag_query(sc, &diag_query, return_code);
1933 if (status == MPR_DIAG_SUCCESS)
1934 if (copyout(&diag_query, diag_action,
1935 sizeof (diag_query)) != 0)
1936 return (MPR_DIAG_FAILURE);
1939 case MPR_FW_DIAG_TYPE_READ_BUFFER:
1940 if (copyin(diag_action, &diag_read_buffer,
1941 sizeof(diag_read_buffer)) != 0)
1942 return (MPR_DIAG_FAILURE);
1943 if (length < diag_read_buffer.BytesToRead) {
1945 MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1946 status = MPR_DIAG_FAILURE;
1949 status = mpr_diag_read_buffer(sc, &diag_read_buffer,
1950 PTRIN(diag_read_buffer.PtrDataBuffer),
1952 if (status == MPR_DIAG_SUCCESS) {
1953 if (copyout(&diag_read_buffer, diag_action,
1954 sizeof(diag_read_buffer) -
1955 sizeof(diag_read_buffer.PtrDataBuffer)) !=
1957 return (MPR_DIAG_FAILURE);
1961 case MPR_FW_DIAG_TYPE_RELEASE:
1962 if (length < sizeof(diag_release)) {
1964 MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1965 status = MPR_DIAG_FAILURE;
1968 if (copyin(diag_action, &diag_release,
1969 sizeof(diag_release)) != 0)
1970 return (MPR_DIAG_FAILURE);
1971 status = mpr_diag_release(sc, &diag_release,
1976 *return_code = MPR_FW_DIAG_ERROR_INVALID_PARAMETER;
1977 status = MPR_DIAG_FAILURE;
1981 if ((status == MPR_DIAG_FAILURE) &&
1982 (original_return_code == MPR_FW_DIAG_NEW) &&
1983 (*return_code != MPR_FW_DIAG_ERROR_SUCCESS))
1984 status = MPR_DIAG_SUCCESS;
1990 mpr_user_diag_action(struct mpr_softc *sc, mpr_diag_action_t *data)
1995 * Only allow one diag action at one time.
1997 if (sc->mpr_flags & MPR_FLAGS_BUSY) {
1998 mpr_dprint(sc, MPR_USER, "%s: Only one FW diag command "
1999 "allowed at a single time.", __func__);
2002 sc->mpr_flags |= MPR_FLAGS_BUSY;
2005 * Send diag action request
2007 if (data->Action == MPR_FW_DIAG_TYPE_REGISTER ||
2008 data->Action == MPR_FW_DIAG_TYPE_UNREGISTER ||
2009 data->Action == MPR_FW_DIAG_TYPE_QUERY ||
2010 data->Action == MPR_FW_DIAG_TYPE_READ_BUFFER ||
2011 data->Action == MPR_FW_DIAG_TYPE_RELEASE) {
2012 status = mpr_do_diag_action(sc, data->Action,
2013 PTRIN(data->PtrDiagAction), data->Length,
2018 sc->mpr_flags &= ~MPR_FLAGS_BUSY;
2023 * Copy the event recording mask and the event queue size out. For
2024 * clarification, the event recording mask (events_to_record) is not the same
2025 * thing as the event mask (event_mask). events_to_record has a bit set for
2026 * every event type that is to be recorded by the driver, and event_mask has a
2027 * bit cleared for every event that is allowed into the driver from the IOC.
2028 * They really have nothing to do with each other.
2031 mpr_user_event_query(struct mpr_softc *sc, mpr_event_query_t *data)
2036 data->Entries = MPR_EVENT_QUEUE_SIZE;
2038 for (i = 0; i < 4; i++) {
2039 data->Types[i] = sc->events_to_record[i];
2045 * Set the driver's event mask according to what's been given. See
2046 * mpr_user_event_query for explanation of the event recording mask and the IOC
2047 * event mask. It's the app's responsibility to enable event logging by setting
2048 * the bits in events_to_record. Initially, no events will be logged.
2051 mpr_user_event_enable(struct mpr_softc *sc, mpr_event_enable_t *data)
2056 for (i = 0; i < 4; i++) {
2057 sc->events_to_record[i] = data->Types[i];
2063 * Copy out the events that have been recorded, up to the max events allowed.
2066 mpr_user_event_report(struct mpr_softc *sc, mpr_event_report_t *data)
2073 if ((size >= sizeof(sc->recorded_events)) && (status == 0)) {
2075 if (copyout((void *)sc->recorded_events,
2076 PTRIN(data->PtrEvents), size) != 0)
2081 * data->Size value is not large enough to copy event data.
2087 * Change size value to match the number of bytes that were copied.
2090 data->Size = sizeof(sc->recorded_events);
2097 * Record events into the driver from the IOC if they are not masked.
2100 mprsas_record_event(struct mpr_softc *sc,
2101 MPI2_EVENT_NOTIFICATION_REPLY *event_reply)
2105 uint16_t event_data_len;
2106 boolean_t sendAEN = FALSE;
2108 event = event_reply->Event;
2111 * Generate a system event to let anyone who cares know that a
2112 * LOG_ENTRY_ADDED event has occurred. This is sent no matter what the
2113 * event mask is set to.
2115 if (event == MPI2_EVENT_LOG_ENTRY_ADDED) {
2120 * Record the event only if its corresponding bit is set in
2121 * events_to_record. event_index is the index into recorded_events and
2122 * event_number is the overall number of an event being recorded since
2123 * start-of-day. event_index will roll over; event_number will never
2126 i = (uint8_t)(event / 32);
2127 j = (uint8_t)(event % 32);
2128 if ((i < 4) && ((1 << j) & sc->events_to_record[i])) {
2129 i = sc->event_index;
2130 sc->recorded_events[i].Type = event;
2131 sc->recorded_events[i].Number = ++sc->event_number;
2132 bzero(sc->recorded_events[i].Data, MPR_MAX_EVENT_DATA_LENGTH *
2134 event_data_len = event_reply->EventDataLength;
2136 if (event_data_len > 0) {
2138 * Limit data to size in m_event entry
2140 if (event_data_len > MPR_MAX_EVENT_DATA_LENGTH) {
2141 event_data_len = MPR_MAX_EVENT_DATA_LENGTH;
2143 for (j = 0; j < event_data_len; j++) {
2144 sc->recorded_events[i].Data[j] =
2145 event_reply->EventData[j];
2149 * check for index wrap-around
2151 if (++i == MPR_EVENT_QUEUE_SIZE) {
2154 sc->event_index = (uint8_t)i;
2157 * Set flag to send the event.
2164 * Generate a system event if flag is set to let anyone who cares know
2165 * that an event has occurred.
2168 //SLM-how to send a system event (see kqueue, kevent)
2169 // (void) ddi_log_sysevent(mpt->m_dip, DDI_VENDOR_LSI, "MPT_SAS",
2170 // "SAS", NULL, NULL, DDI_NOSLEEP);
2175 mpr_user_reg_access(struct mpr_softc *sc, mpr_reg_access_t *data)
2179 switch (data->Command) {
2181 * IO access is not supported.
2185 mpr_dprint(sc, MPR_USER, "IO access is not supported. "
2186 "Use memory access.");
2191 data->RegData = mpr_regread(sc, data->RegOffset);
2195 mpr_regwrite(sc, data->RegOffset, data->RegData);
2207 mpr_user_btdh(struct mpr_softc *sc, mpr_btdh_mapping_t *data)
2209 uint8_t bt2dh = FALSE;
2210 uint8_t dh2bt = FALSE;
2211 uint16_t dev_handle, bus, target;
2214 target = data->TargetID;
2215 dev_handle = data->DevHandle;
2218 * When DevHandle is 0xFFFF and Bus/Target are not 0xFFFF, use Bus/
2219 * Target to get DevHandle. When Bus/Target are 0xFFFF and DevHandle is
2220 * not 0xFFFF, use DevHandle to get Bus/Target. Anything else is
2223 if ((bus == 0xFFFF) && (target == 0xFFFF) && (dev_handle != 0xFFFF))
2225 if ((dev_handle == 0xFFFF) && (bus != 0xFFFF) && (target != 0xFFFF))
2227 if (!dh2bt && !bt2dh)
2231 * Only handle bus of 0. Make sure target is within range.
2237 if (target > sc->max_devices) {
2238 mpr_dprint(sc, MPR_XINFO, "Target ID is out of range "
2239 "for Bus/Target to DevHandle mapping.");
2242 dev_handle = sc->mapping_table[target].dev_handle;
2244 data->DevHandle = dev_handle;
2247 target = mpr_mapping_get_tid_from_handle(sc, dev_handle);
2249 data->TargetID = target;
2256 mpr_ioctl(struct cdev *dev, u_long cmd, void *arg, int flag,
2259 struct mpr_softc *sc;
2260 struct mpr_cfg_page_req *page_req;
2261 struct mpr_ext_cfg_page_req *ext_page_req;
2263 int error, msleep_ret;
2267 page_req = (void *)arg;
2268 ext_page_req = (void *)arg;
2271 case MPRIO_READ_CFG_HEADER:
2273 error = mpr_user_read_cfg_header(sc, page_req);
2276 case MPRIO_READ_CFG_PAGE:
2277 mpr_page = malloc(page_req->len, M_MPRUSER, M_WAITOK | M_ZERO);
2278 error = copyin(page_req->buf, mpr_page,
2279 sizeof(MPI2_CONFIG_PAGE_HEADER));
2283 error = mpr_user_read_cfg_page(sc, page_req, mpr_page);
2287 error = copyout(mpr_page, page_req->buf, page_req->len);
2289 case MPRIO_READ_EXT_CFG_HEADER:
2291 error = mpr_user_read_extcfg_header(sc, ext_page_req);
2294 case MPRIO_READ_EXT_CFG_PAGE:
2295 mpr_page = malloc(ext_page_req->len, M_MPRUSER,
2297 error = copyin(ext_page_req->buf, mpr_page,
2298 sizeof(MPI2_CONFIG_EXTENDED_PAGE_HEADER));
2302 error = mpr_user_read_extcfg_page(sc, ext_page_req, mpr_page);
2306 error = copyout(mpr_page, ext_page_req->buf, ext_page_req->len);
2308 case MPRIO_WRITE_CFG_PAGE:
2309 mpr_page = malloc(page_req->len, M_MPRUSER, M_WAITOK|M_ZERO);
2310 error = copyin(page_req->buf, mpr_page, page_req->len);
2314 error = mpr_user_write_cfg_page(sc, page_req, mpr_page);
2317 case MPRIO_MPR_COMMAND:
2318 error = mpr_user_command(sc, (struct mpr_usr_command *)arg);
2320 case MPTIOCTL_PASS_THRU:
2322 * The user has requested to pass through a command to be
2323 * executed by the MPT firmware. Call our routine which does
2324 * this. Only allow one passthru IOCTL at one time.
2326 error = mpr_user_pass_thru(sc, (mpr_pass_thru_t *)arg);
2328 case MPTIOCTL_GET_ADAPTER_DATA:
2330 * The user has requested to read adapter data. Call our
2331 * routine which does this.
2334 mpr_user_get_adapter_data(sc, (mpr_adapter_data_t *)arg);
2336 case MPTIOCTL_GET_PCI_INFO:
2338 * The user has requested to read pci info. Call
2339 * our routine which does this.
2343 mpr_user_read_pci_info(sc, (mpr_pci_info_t *)arg);
2346 case MPTIOCTL_RESET_ADAPTER:
2348 sc->port_enable_complete = 0;
2349 uint32_t reinit_start = time_uptime;
2350 error = mpr_reinit(sc);
2351 /* Sleep for 300 second. */
2352 msleep_ret = msleep(&sc->port_enable_complete, &sc->mpr_mtx,
2353 PRIBIO, "mpr_porten", 300 * hz);
2356 printf("Port Enable did not complete after Diag "
2357 "Reset msleep error %d.\n", msleep_ret);
2359 mpr_dprint(sc, MPR_USER, "Hard Reset with Port Enable "
2360 "completed in %d seconds.\n",
2361 (uint32_t)(time_uptime - reinit_start));
2363 case MPTIOCTL_DIAG_ACTION:
2365 * The user has done a diag buffer action. Call our routine
2366 * which does this. Only allow one diag action at one time.
2369 error = mpr_user_diag_action(sc, (mpr_diag_action_t *)arg);
2372 case MPTIOCTL_EVENT_QUERY:
2374 * The user has done an event query. Call our routine which does
2378 mpr_user_event_query(sc, (mpr_event_query_t *)arg);
2380 case MPTIOCTL_EVENT_ENABLE:
2382 * The user has done an event enable. Call our routine which
2386 mpr_user_event_enable(sc, (mpr_event_enable_t *)arg);
2388 case MPTIOCTL_EVENT_REPORT:
2390 * The user has done an event report. Call our routine which
2393 error = mpr_user_event_report(sc, (mpr_event_report_t *)arg);
2395 case MPTIOCTL_REG_ACCESS:
2397 * The user has requested register access. Call our routine
2401 error = mpr_user_reg_access(sc, (mpr_reg_access_t *)arg);
2404 case MPTIOCTL_BTDH_MAPPING:
2406 * The user has requested to translate a bus/target to a
2407 * DevHandle or a DevHandle to a bus/target. Call our routine
2410 error = mpr_user_btdh(sc, (mpr_btdh_mapping_t *)arg);
2417 if (mpr_page != NULL)
2418 free(mpr_page, M_MPRUSER);
2423 #ifdef COMPAT_FREEBSD32
2425 struct mpr_cfg_page_req32 {
2426 MPI2_CONFIG_PAGE_HEADER header;
2427 uint32_t page_address;
2430 uint16_t ioc_status;
2433 struct mpr_ext_cfg_page_req32 {
2434 MPI2_CONFIG_EXTENDED_PAGE_HEADER header;
2435 uint32_t page_address;
2438 uint16_t ioc_status;
2441 struct mpr_raid_action32 {
2445 uint8_t phys_disk_num;
2446 uint32_t action_data_word;
2449 uint32_t volume_status;
2450 uint32_t action_data[4];
2451 uint16_t action_status;
2452 uint16_t ioc_status;
2456 struct mpr_usr_command32 {
2466 #define MPRIO_READ_CFG_HEADER32 _IOWR('M', 200, struct mpr_cfg_page_req32)
2467 #define MPRIO_READ_CFG_PAGE32 _IOWR('M', 201, struct mpr_cfg_page_req32)
2468 #define MPRIO_READ_EXT_CFG_HEADER32 _IOWR('M', 202, struct mpr_ext_cfg_page_req32)
2469 #define MPRIO_READ_EXT_CFG_PAGE32 _IOWR('M', 203, struct mpr_ext_cfg_page_req32)
2470 #define MPRIO_WRITE_CFG_PAGE32 _IOWR('M', 204, struct mpr_cfg_page_req32)
2471 #define MPRIO_RAID_ACTION32 _IOWR('M', 205, struct mpr_raid_action32)
2472 #define MPRIO_MPR_COMMAND32 _IOWR('M', 210, struct mpr_usr_command32)
2475 mpr_ioctl32(struct cdev *dev, u_long cmd32, void *_arg, int flag,
2478 struct mpr_cfg_page_req32 *page32 = _arg;
2479 struct mpr_ext_cfg_page_req32 *ext32 = _arg;
2480 struct mpr_raid_action32 *raid32 = _arg;
2481 struct mpr_usr_command32 *user32 = _arg;
2483 struct mpr_cfg_page_req page;
2484 struct mpr_ext_cfg_page_req ext;
2485 struct mpr_raid_action raid;
2486 struct mpr_usr_command user;
2492 case MPRIO_READ_CFG_HEADER32:
2493 case MPRIO_READ_CFG_PAGE32:
2494 case MPRIO_WRITE_CFG_PAGE32:
2495 if (cmd32 == MPRIO_READ_CFG_HEADER32)
2496 cmd = MPRIO_READ_CFG_HEADER;
2497 else if (cmd32 == MPRIO_READ_CFG_PAGE32)
2498 cmd = MPRIO_READ_CFG_PAGE;
2500 cmd = MPRIO_WRITE_CFG_PAGE;
2501 CP(*page32, arg.page, header);
2502 CP(*page32, arg.page, page_address);
2503 PTRIN_CP(*page32, arg.page, buf);
2504 CP(*page32, arg.page, len);
2505 CP(*page32, arg.page, ioc_status);
2508 case MPRIO_READ_EXT_CFG_HEADER32:
2509 case MPRIO_READ_EXT_CFG_PAGE32:
2510 if (cmd32 == MPRIO_READ_EXT_CFG_HEADER32)
2511 cmd = MPRIO_READ_EXT_CFG_HEADER;
2513 cmd = MPRIO_READ_EXT_CFG_PAGE;
2514 CP(*ext32, arg.ext, header);
2515 CP(*ext32, arg.ext, page_address);
2516 PTRIN_CP(*ext32, arg.ext, buf);
2517 CP(*ext32, arg.ext, len);
2518 CP(*ext32, arg.ext, ioc_status);
2521 case MPRIO_RAID_ACTION32:
2522 cmd = MPRIO_RAID_ACTION;
2523 CP(*raid32, arg.raid, action);
2524 CP(*raid32, arg.raid, volume_bus);
2525 CP(*raid32, arg.raid, volume_id);
2526 CP(*raid32, arg.raid, phys_disk_num);
2527 CP(*raid32, arg.raid, action_data_word);
2528 PTRIN_CP(*raid32, arg.raid, buf);
2529 CP(*raid32, arg.raid, len);
2530 CP(*raid32, arg.raid, volume_status);
2531 bcopy(raid32->action_data, arg.raid.action_data,
2532 sizeof arg.raid.action_data);
2533 CP(*raid32, arg.raid, ioc_status);
2534 CP(*raid32, arg.raid, write);
2537 case MPRIO_MPR_COMMAND32:
2538 cmd = MPRIO_MPR_COMMAND;
2539 PTRIN_CP(*user32, arg.user, req);
2540 CP(*user32, arg.user, req_len);
2541 PTRIN_CP(*user32, arg.user, rpl);
2542 CP(*user32, arg.user, rpl_len);
2543 PTRIN_CP(*user32, arg.user, buf);
2544 CP(*user32, arg.user, len);
2545 CP(*user32, arg.user, flags);
2551 error = mpr_ioctl(dev, cmd, &arg, flag, td);
2552 if (error == 0 && (cmd32 & IOC_OUT) != 0) {
2554 case MPRIO_READ_CFG_HEADER32:
2555 case MPRIO_READ_CFG_PAGE32:
2556 case MPRIO_WRITE_CFG_PAGE32:
2557 CP(arg.page, *page32, header);
2558 CP(arg.page, *page32, page_address);
2559 PTROUT_CP(arg.page, *page32, buf);
2560 CP(arg.page, *page32, len);
2561 CP(arg.page, *page32, ioc_status);
2564 case MPRIO_READ_EXT_CFG_HEADER32:
2565 case MPRIO_READ_EXT_CFG_PAGE32:
2566 CP(arg.ext, *ext32, header);
2567 CP(arg.ext, *ext32, page_address);
2568 PTROUT_CP(arg.ext, *ext32, buf);
2569 CP(arg.ext, *ext32, len);
2570 CP(arg.ext, *ext32, ioc_status);
2573 case MPRIO_RAID_ACTION32:
2574 CP(arg.raid, *raid32, action);
2575 CP(arg.raid, *raid32, volume_bus);
2576 CP(arg.raid, *raid32, volume_id);
2577 CP(arg.raid, *raid32, phys_disk_num);
2578 CP(arg.raid, *raid32, action_data_word);
2579 PTROUT_CP(arg.raid, *raid32, buf);
2580 CP(arg.raid, *raid32, len);
2581 CP(arg.raid, *raid32, volume_status);
2582 bcopy(arg.raid.action_data, raid32->action_data,
2583 sizeof arg.raid.action_data);
2584 CP(arg.raid, *raid32, ioc_status);
2585 CP(arg.raid, *raid32, write);
2588 case MPRIO_MPR_COMMAND32:
2589 PTROUT_CP(arg.user, *user32, req);
2590 CP(arg.user, *user32, req_len);
2591 PTROUT_CP(arg.user, *user32, rpl);
2592 CP(arg.user, *user32, rpl_len);
2593 PTROUT_CP(arg.user, *user32, buf);
2594 CP(arg.user, *user32, len);
2595 CP(arg.user, *user32, flags);
2602 #endif /* COMPAT_FREEBSD32 */
2605 mpr_ioctl_devsw(struct cdev *dev, u_long com, caddr_t arg, int flag,
2608 #ifdef COMPAT_FREEBSD32
2609 if (SV_CURPROC_FLAG(SV_ILP32))
2610 return (mpr_ioctl32(dev, com, arg, flag, td));
2612 return (mpr_ioctl(dev, com, arg, flag, td));