2 * Copyright (c) 2015 Baptiste Daroussin <bapt@FreeBSD.org>
4 * Copyright (c) 2015 Netflix, Inc.
5 * Written by: Scott Long <scottl@freebsd.org>
7 * Copyright (c) 2008 Yahoo!, Inc.
9 * Written by: John Baldwin <jhb@FreeBSD.org>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the author nor the names of any co-contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
39 #include <sys/param.h>
40 #include <sys/errno.h>
41 #include <sys/ioctl.h>
43 #include <sys/mps_ioctl.h>
45 #include "mps_ioctl.h"
46 #include "mpr_ioctl.h"
48 #include <sys/sysctl.h>
60 #ifndef USE_MPT_IOCTLS
61 #define USE_MPT_IOCTLS
64 static const char *mps_ioc_status_codes[] = {
65 "Success", /* 0x0000 */
68 "Invalid scatter-gather list",
71 "Insufficient resources",
73 "Invalid state", /* 0x0008 */
74 "Operation state not supported",
97 "Invalid configuration action", /* 0x0020 */
98 "Invalid configuration type",
99 "Invalid configuration page",
100 "Invalid configuration data",
101 "No configuration defaults",
102 "Unable to commit configuration change",
129 "Recovered SCSI error", /* 0x0040 */
131 "Invalid SCSI target ID",
132 "SCSI device not there",
134 "SCSI data underrun",
136 "SCSI protocol error",
137 "SCSI task terminated", /* 0x0048 */
138 "SCSI residual mismatch",
139 "SCSI task management failed",
140 "SCSI I/O controller terminated",
141 "SCSI external controller terminated",
143 "EEDP reference tag error",
144 "EEDP application tag error",
161 "SCSI target priority I/O", /* 0x0060 */
162 "Invalid SCSI target port",
163 "Invalid SCSI target I/O index",
164 "SCSI target aborted",
165 "No connection retryable",
168 "Invalid FC receive ID",
169 "FC did invalid", /* 0x0068 */
170 "FC node logged out",
171 "Transfer count mismatch",
173 "FC exchange canceled",
175 "Too much write data",
177 "ACK NAK timeout", /* 0x0070 */
193 "LAN device not found", /* 0x0080 */
194 "LAN device failure",
195 "LAN transmit error",
196 "LAN transmit aborted",
198 "LAN receive aborted",
199 "LAN partial packet",
209 "SAS SMP request failed", /* 0x0090 */
210 "SAS SMP data overrun",
217 "Inband aborted", /* 0x0098 */
218 "No inband connection",
225 "Diagnostic released", /* 0x00A0 */
228 struct mprs_pass_thru {
232 uint32_t RequestSize;
235 uint32_t DataDirection;
237 uint32_t DataOutSize;
241 struct mprs_btdh_mapping {
249 mps_ioc_status(U16 IOCStatus)
251 static char buffer[16];
253 IOCStatus &= MPI2_IOCSTATUS_MASK;
254 if (IOCStatus < sizeof(mps_ioc_status_codes) / sizeof(char *) &&
255 mps_ioc_status_codes[IOCStatus] != NULL)
256 return (mps_ioc_status_codes[IOCStatus]);
257 snprintf(buffer, sizeof(buffer), "Status: 0x%04x", IOCStatus);
261 #ifdef USE_MPT_IOCTLS
263 mps_map_btdh(int fd, uint16_t *devhandle, uint16_t *bus, uint16_t *target)
266 struct mprs_btdh_mapping map;
269 map.TargetID = *target;
270 map.DevHandle = *devhandle;
272 if ((error = ioctl(fd, MPTIOCTL_BTDH_MAPPING, &map)) != 0) {
274 warn("Failed to map bus/target/device");
279 *target = map.TargetID;
280 *devhandle = map.DevHandle;
286 mps_set_slot_status(int fd, U16 handle, U16 slot, U32 status)
288 MPI2_SEP_REQUEST req;
289 MPI2_SEP_REPLY reply;
291 bzero(&req, sizeof(req));
292 req.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
293 req.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
294 req.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
295 req.EnclosureHandle = handle;
297 req.SlotStatus = status;
299 if (mps_pass_command(fd, &req, sizeof(req), &reply, sizeof(reply),
300 NULL, 0, NULL, 0, 30) != 0)
303 if (!IOC_STATUS_SUCCESS(reply.IOCStatus))
309 mps_read_config_page_header(int fd, U8 PageType, U8 PageNumber, U32 PageAddress,
310 MPI2_CONFIG_PAGE_HEADER *header, U16 *IOCStatus)
312 MPI2_CONFIG_REQUEST req;
313 MPI2_CONFIG_REPLY reply;
315 bzero(&req, sizeof(req));
316 req.Function = MPI2_FUNCTION_CONFIG;
317 req.Action = MPI2_CONFIG_ACTION_PAGE_HEADER;
318 req.Header.PageType = PageType;
319 req.Header.PageNumber = PageNumber;
320 req.PageAddress = PageAddress;
322 if (mps_pass_command(fd, &req, sizeof(req), &reply, sizeof(reply),
323 NULL, 0, NULL, 0, 30))
326 if (!IOC_STATUS_SUCCESS(reply.IOCStatus)) {
327 if (IOCStatus != NULL)
328 *IOCStatus = reply.IOCStatus;
333 *header = reply.Header;
338 mps_read_ext_config_page_header(int fd, U8 ExtPageType, U8 PageNumber, U32 PageAddress, MPI2_CONFIG_PAGE_HEADER *header, U16 *ExtPageLength, U16 *IOCStatus)
340 MPI2_CONFIG_REQUEST req;
341 MPI2_CONFIG_REPLY reply;
343 bzero(&req, sizeof(req));
344 req.Function = MPI2_FUNCTION_CONFIG;
345 req.Action = MPI2_CONFIG_ACTION_PAGE_HEADER;
346 req.Header.PageType = MPI2_CONFIG_PAGETYPE_EXTENDED;
347 req.ExtPageType = ExtPageType;
348 req.Header.PageNumber = PageNumber;
349 req.PageAddress = PageAddress;
351 if (mps_pass_command(fd, &req, sizeof(req), &reply, sizeof(reply),
352 NULL, 0, NULL, 0, 30))
355 if (!IOC_STATUS_SUCCESS(reply.IOCStatus)) {
356 if (IOCStatus != NULL)
357 *IOCStatus = reply.IOCStatus;
360 if ((header == NULL) || (ExtPageLength == NULL))
362 *header = reply.Header;
363 *ExtPageLength = reply.ExtPageLength;
368 mps_read_config_page(int fd, U8 PageType, U8 PageNumber, U32 PageAddress,
371 MPI2_CONFIG_REQUEST req;
372 MPI2_CONFIG_PAGE_HEADER header;
373 MPI2_CONFIG_REPLY reply;
377 bzero(&header, sizeof(header));
378 error = mps_read_config_page_header(fd, PageType, PageNumber,
379 PageAddress, &header, IOCStatus);
385 bzero(&req, sizeof(req));
386 req.Function = MPI2_FUNCTION_CONFIG;
387 req.Action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
388 req.PageAddress = PageAddress;
390 if (req.Header.PageLength == 0)
391 req.Header.PageLength = 4;
393 len = req.Header.PageLength * 4;
395 if (mps_pass_command(fd, &req, sizeof(req), &reply, sizeof(reply),
396 buf, len, NULL, 0, 30)) {
402 if (!IOC_STATUS_SUCCESS(reply.IOCStatus)) {
403 if (IOCStatus != NULL)
404 *IOCStatus = reply.IOCStatus;
406 warnx("Reading config page failed: 0x%x %s",
407 reply.IOCStatus, mps_ioc_status(reply.IOCStatus));
416 mps_read_extended_config_page(int fd, U8 ExtPageType, U8 PageVersion,
417 U8 PageNumber, U32 PageAddress, U16 *IOCStatus)
419 MPI2_CONFIG_REQUEST req;
420 MPI2_CONFIG_PAGE_HEADER header;
421 MPI2_CONFIG_REPLY reply;
426 if (IOCStatus != NULL)
427 *IOCStatus = MPI2_IOCSTATUS_SUCCESS;
428 bzero(&header, sizeof(header));
429 error = mps_read_ext_config_page_header(fd, ExtPageType, PageNumber,
430 PageAddress, &header, &pagelen, IOCStatus);
436 bzero(&req, sizeof(req));
437 req.Function = MPI2_FUNCTION_CONFIG;
438 req.Action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
439 req.PageAddress = PageAddress;
443 req.ExtPageLength = pagelen;
444 req.ExtPageType = ExtPageType;
448 if (mps_pass_command(fd, &req, sizeof(req), &reply, sizeof(reply),
449 buf, len, NULL, 0, 30)) {
455 if (!IOC_STATUS_SUCCESS(reply.IOCStatus)) {
456 if (IOCStatus != NULL)
457 *IOCStatus = reply.IOCStatus;
459 warnx("Reading extended config page failed: %s",
460 mps_ioc_status(reply.IOCStatus));
469 mps_firmware_send(int fd, unsigned char *fw, uint32_t len, bool bios)
471 MPI2_FW_DOWNLOAD_REQUEST req;
472 MPI2_FW_DOWNLOAD_REPLY reply;
474 bzero(&req, sizeof(req));
475 bzero(&reply, sizeof(reply));
476 req.Function = MPI2_FUNCTION_FW_DOWNLOAD;
477 req.ImageType = bios ? MPI2_FW_DOWNLOAD_ITYPE_BIOS : MPI2_FW_DOWNLOAD_ITYPE_FW;
478 req.TotalImageSize = len;
479 req.MsgFlags = MPI2_FW_DOWNLOAD_MSGFLGS_LAST_SEGMENT;
481 if (mps_user_command(fd, &req, sizeof(req), &reply, sizeof(reply),
489 mps_firmware_get(int fd, unsigned char **firmware, bool bios)
491 MPI2_FW_UPLOAD_REQUEST req;
492 MPI2_FW_UPLOAD_REPLY reply;
496 bzero(&req, sizeof(req));
497 bzero(&reply, sizeof(reply));
498 req.Function = MPI2_FUNCTION_FW_UPLOAD;
499 req.ImageType = bios ? MPI2_FW_DOWNLOAD_ITYPE_BIOS : MPI2_FW_DOWNLOAD_ITYPE_FW;
501 if (mps_user_command(fd, &req, sizeof(req), &reply, sizeof(reply),
505 if (reply.ActualImageSize == 0) {
509 size = reply.ActualImageSize;
510 *firmware = calloc(size, sizeof(unsigned char));
511 if (*firmware == NULL) {
515 if (mps_user_command(fd, &req, sizeof(req), &reply, sizeof(reply),
516 *firmware, size, 0)) {
527 mps_read_config_page_header(int fd, U8 PageType, U8 PageNumber, U32 PageAddress,
528 MPI2_CONFIG_PAGE_HEADER *header, U16 *IOCStatus)
530 struct mps_cfg_page_req req;
532 if (IOCStatus != NULL)
533 *IOCStatus = MPI2_IOCSTATUS_SUCCESS;
536 bzero(&req, sizeof(req));
537 req.header.PageType = PageType;
538 req.header.PageNumber = PageNumber;
539 req.page_address = PageAddress;
540 if (ioctl(fd, MPSIO_READ_CFG_HEADER, &req) < 0)
542 if (!IOC_STATUS_SUCCESS(req.ioc_status)) {
543 if (IOCStatus != NULL)
544 *IOCStatus = req.ioc_status;
547 bcopy(&req.header, header, sizeof(*header));
552 mps_read_config_page(int fd, U8 PageType, U8 PageNumber, U32 PageAddress,
555 struct mps_cfg_page_req req;
559 error = mps_read_config_page_header(fd, PageType, PageNumber,
560 PageAddress, &req.header, IOCStatus);
566 if (req.header.PageLength == 0)
567 req.header.PageLength = 4;
568 req.len = req.header.PageLength * 4;
569 buf = malloc(req.len);
571 bcopy(&req.header, buf, sizeof(req.header));
572 if (ioctl(fd, MPSIO_READ_CFG_PAGE, &req) < 0) {
578 if (!IOC_STATUS_SUCCESS(req.ioc_status)) {
579 if (IOCStatus != NULL)
580 *IOCStatus = req.ioc_status;
582 warnx("Reading config page failed: 0x%x %s",
583 req.ioc_status, mps_ioc_status(req.ioc_status));
592 mps_read_extended_config_page(int fd, U8 ExtPageType, U8 PageVersion,
593 U8 PageNumber, U32 PageAddress, U16 *IOCStatus)
595 struct mps_ext_cfg_page_req req;
599 if (IOCStatus != NULL)
600 *IOCStatus = MPI2_IOCSTATUS_SUCCESS;
601 bzero(&req, sizeof(req));
602 req.header.PageVersion = PageVersion;
603 req.header.PageNumber = PageNumber;
604 req.header.ExtPageType = ExtPageType;
605 req.page_address = PageAddress;
606 if (ioctl(fd, MPSIO_READ_EXT_CFG_HEADER, &req) < 0)
608 if (!IOC_STATUS_SUCCESS(req.ioc_status)) {
609 if (IOCStatus != NULL)
610 *IOCStatus = req.ioc_status;
612 warnx("Reading extended config page header failed: %s",
613 mps_ioc_status(req.ioc_status));
617 req.len = req.header.ExtPageLength * 4;
618 buf = malloc(req.len);
620 bcopy(&req.header, buf, sizeof(req.header));
621 if (ioctl(fd, MPSIO_READ_EXT_CFG_PAGE, &req) < 0) {
627 if (!IOC_STATUS_SUCCESS(req.ioc_status)) {
628 if (IOCStatus != NULL)
629 *IOCStatus = req.ioc_status;
631 warnx("Reading extended config page failed: %s",
632 mps_ioc_status(req.ioc_status));
644 char path[MAXPATHLEN];
646 snprintf(path, sizeof(path), "/dev/mp%s%d", is_mps ? "s": "r", unit);
647 return (open(path, O_RDWR));
651 mps_user_command(int fd, void *req, uint32_t req_len, void *reply,
652 uint32_t reply_len, void *buffer, int len, uint32_t flags)
654 struct mps_usr_command cmd;
656 bzero(&cmd, sizeof(struct mps_usr_command));
658 cmd.req_len = req_len;
660 cmd.rpl_len = reply_len;
665 if (ioctl(fd, is_mps ? MPSIO_MPS_COMMAND : MPRIO_MPR_COMMAND, &cmd) < 0)
671 mps_pass_command(int fd, void *req, uint32_t req_len, void *reply,
672 uint32_t reply_len, void *data_in, uint32_t datain_len, void *data_out,
673 uint32_t dataout_len, uint32_t timeout)
675 struct mprs_pass_thru pass;
677 bzero(&pass, sizeof(pass));
678 pass.PtrRequest = (uint64_t)(uintptr_t)req;
679 pass.PtrReply = (uint64_t)(uintptr_t)reply;
680 pass.RequestSize = req_len;
681 pass.ReplySize = reply_len;
682 if (datain_len && dataout_len) {
683 pass.PtrData = (uint64_t)(uintptr_t)data_in;
684 pass.PtrDataOut = (uint64_t)(uintptr_t)data_out;
685 pass.DataSize = datain_len;
686 pass.DataOutSize = dataout_len;
688 pass.DataDirection = MPS_PASS_THRU_DIRECTION_BOTH;
690 pass.DataDirection = MPR_PASS_THRU_DIRECTION_BOTH;
692 } else if (datain_len) {
693 pass.PtrData = (uint64_t)(uintptr_t)data_in;
694 pass.DataSize = datain_len;
696 pass.DataDirection = MPS_PASS_THRU_DIRECTION_READ;
698 pass.DataDirection = MPR_PASS_THRU_DIRECTION_READ;
700 } else if (dataout_len) {
701 pass.PtrData = (uint64_t)(uintptr_t)data_out;
702 pass.DataSize = dataout_len;
704 pass.DataDirection = MPS_PASS_THRU_DIRECTION_WRITE;
706 pass.DataDirection = MPR_PASS_THRU_DIRECTION_WRITE;
710 pass.DataDirection = MPS_PASS_THRU_DIRECTION_NONE;
712 pass.DataDirection = MPR_PASS_THRU_DIRECTION_NONE;
715 pass.Timeout = timeout;
717 if (ioctl(fd, MPTIOCTL_PASS_THRU, &pass) < 0)
722 MPI2_IOC_FACTS_REPLY *
723 mps_get_iocfacts(int fd)
725 MPI2_IOC_FACTS_REPLY *facts;
726 MPI2_IOC_FACTS_REQUEST req;
727 char msgver[8], sysctlname[128];
728 size_t len, factslen;
731 snprintf(sysctlname, sizeof(sysctlname), "dev.%s.%d.msg_version",
732 is_mps ? "mps" : "mpr", mps_unit);
734 factslen = sizeof(MPI2_IOC_FACTS_REPLY);
735 len = sizeof(msgver);
736 error = sysctlbyname(sysctlname, msgver, &len, NULL, 0);
738 if (strncmp(msgver, "2.6", sizeof(msgver)) == 0)
742 facts = malloc(factslen);
748 bzero(&req, factslen);
749 req.Function = MPI2_FUNCTION_IOC_FACTS;
752 error = mps_pass_command(fd, &req, sizeof(MPI2_IOC_FACTS_REQUEST),
753 facts, factslen, NULL, 0, NULL, 0, 10);
755 error = mps_user_command(fd, &req, sizeof(MPI2_IOC_FACTS_REQUEST),
756 facts, factslen, NULL, 0, 0);
763 if (!IOC_STATUS_SUCCESS(facts->IOCStatus)) {