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>
42 #include <sys/sysctl.h>
44 #include <sys/endian.h>
54 #include <dev/mps/mps_ioctl.h>
55 #include <dev/mpr/mpr_ioctl.h>
57 #ifndef USE_MPT_IOCTLS
58 #define USE_MPT_IOCTLS
61 static const char *mps_ioc_status_codes[] = {
62 "Success", /* 0x0000 */
65 "Invalid scatter-gather list",
68 "Insufficient resources",
70 "Invalid state", /* 0x0008 */
71 "Operation state not supported",
94 "Invalid configuration action", /* 0x0020 */
95 "Invalid configuration type",
96 "Invalid configuration page",
97 "Invalid configuration data",
98 "No configuration defaults",
99 "Unable to commit configuration change",
126 "Recovered SCSI error", /* 0x0040 */
128 "Invalid SCSI target ID",
129 "SCSI device not there",
131 "SCSI data underrun",
133 "SCSI protocol error",
134 "SCSI task terminated", /* 0x0048 */
135 "SCSI residual mismatch",
136 "SCSI task management failed",
137 "SCSI I/O controller terminated",
138 "SCSI external controller terminated",
140 "EEDP reference tag error",
141 "EEDP application tag error",
158 "SCSI target priority I/O", /* 0x0060 */
159 "Invalid SCSI target port",
160 "Invalid SCSI target I/O index",
161 "SCSI target aborted",
162 "No connection retryable",
165 "Invalid FC receive ID",
166 "FC did invalid", /* 0x0068 */
167 "FC node logged out",
168 "Transfer count mismatch",
170 "FC exchange canceled",
172 "Too much write data",
174 "ACK NAK timeout", /* 0x0070 */
190 "LAN device not found", /* 0x0080 */
191 "LAN device failure",
192 "LAN transmit error",
193 "LAN transmit aborted",
195 "LAN receive aborted",
196 "LAN partial packet",
206 "SAS SMP request failed", /* 0x0090 */
207 "SAS SMP data overrun",
214 "Inband aborted", /* 0x0098 */
215 "No inband connection",
222 "Diagnostic released", /* 0x00A0 */
225 struct mprs_pass_thru {
229 uint32_t RequestSize;
232 uint32_t DataDirection;
234 uint32_t DataOutSize;
238 struct mprs_btdh_mapping {
245 static void adjust_iocfacts_endianness(MPI2_IOC_FACTS_REPLY *facts);
248 mps_ioc_status(U16 IOCStatus)
250 static char buffer[16];
252 IOCStatus &= MPI2_IOCSTATUS_MASK;
253 if (IOCStatus < sizeof(mps_ioc_status_codes) / sizeof(char *) &&
254 mps_ioc_status_codes[IOCStatus] != NULL)
255 return (mps_ioc_status_codes[IOCStatus]);
256 snprintf(buffer, sizeof(buffer), "Status: 0x%04x", IOCStatus);
260 #ifdef USE_MPT_IOCTLS
262 mps_map_btdh(int fd, uint16_t *devhandle, uint16_t *bus, uint16_t *target)
265 struct mprs_btdh_mapping map;
268 map.TargetID = *target;
269 map.DevHandle = *devhandle;
271 if ((error = ioctl(fd, MPTIOCTL_BTDH_MAPPING, &map)) != 0) {
273 warn("Failed to map bus/target/device");
278 *target = map.TargetID;
279 *devhandle = map.DevHandle;
285 mps_set_slot_status(int fd, U16 handle, U16 slot, U32 status)
287 MPI2_SEP_REQUEST req;
288 MPI2_SEP_REPLY reply;
290 bzero(&req, sizeof(req));
291 req.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
292 req.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
293 req.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
294 req.EnclosureHandle = handle;
296 req.SlotStatus = status;
298 if (mps_pass_command(fd, &req, sizeof(req), &reply, sizeof(reply),
299 NULL, 0, NULL, 0, 30) != 0)
302 if (!IOC_STATUS_SUCCESS(le16toh(reply.IOCStatus)))
308 mps_read_config_page_header(int fd, U8 PageType, U8 PageNumber, U32 PageAddress,
309 MPI2_CONFIG_PAGE_HEADER *header, U16 *IOCStatus)
311 MPI2_CONFIG_REQUEST req;
312 MPI2_CONFIG_REPLY reply;
314 bzero(&req, sizeof(req));
315 req.Function = MPI2_FUNCTION_CONFIG;
316 req.Action = MPI2_CONFIG_ACTION_PAGE_HEADER;
317 req.Header.PageType = PageType;
318 req.Header.PageNumber = PageNumber;
319 req.PageAddress = PageAddress;
321 if (mps_pass_command(fd, &req, sizeof(req), &reply, sizeof(reply),
322 NULL, 0, NULL, 0, 30))
325 if (!IOC_STATUS_SUCCESS(le16toh(reply.IOCStatus))) {
326 if (IOCStatus != NULL)
327 *IOCStatus = reply.IOCStatus;
332 *header = reply.Header;
337 mps_read_ext_config_page_header(int fd, U8 ExtPageType, U8 PageNumber, U32 PageAddress, MPI2_CONFIG_PAGE_HEADER *header, U16 *ExtPageLength, U16 *IOCStatus)
339 MPI2_CONFIG_REQUEST req;
340 MPI2_CONFIG_REPLY reply;
342 bzero(&req, sizeof(req));
343 req.Function = MPI2_FUNCTION_CONFIG;
344 req.Action = MPI2_CONFIG_ACTION_PAGE_HEADER;
345 req.Header.PageType = MPI2_CONFIG_PAGETYPE_EXTENDED;
346 req.ExtPageType = ExtPageType;
347 req.Header.PageNumber = PageNumber;
348 req.PageAddress = htole32(PageAddress);
350 if (mps_pass_command(fd, &req, sizeof(req), &reply, sizeof(reply),
351 NULL, 0, NULL, 0, 30))
354 if (!IOC_STATUS_SUCCESS(le16toh(reply.IOCStatus))) {
355 if (IOCStatus != NULL)
356 *IOCStatus = le16toh(reply.IOCStatus);
359 if ((header == NULL) || (ExtPageLength == NULL))
361 *header = reply.Header;
362 *ExtPageLength = reply.ExtPageLength;
367 mps_read_config_page(int fd, U8 PageType, U8 PageNumber, U32 PageAddress,
370 MPI2_CONFIG_REQUEST req;
371 MPI2_CONFIG_PAGE_HEADER header;
372 MPI2_CONFIG_REPLY reply;
376 bzero(&header, sizeof(header));
377 error = mps_read_config_page_header(fd, PageType, PageNumber,
378 PageAddress, &header, IOCStatus);
384 bzero(&req, sizeof(req));
385 req.Function = MPI2_FUNCTION_CONFIG;
386 req.Action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
387 req.PageAddress = htole32(PageAddress);
389 if (req.Header.PageLength == 0)
390 req.Header.PageLength = 4;
392 len = req.Header.PageLength * 4;
394 if (mps_pass_command(fd, &req, sizeof(req), &reply, sizeof(reply),
395 buf, len, NULL, 0, 30)) {
401 reply.IOCStatus = le16toh(reply.IOCStatus);
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 = htole32(PageAddress);
442 pagelen = htole16(4);
443 req.ExtPageLength = pagelen;
444 req.ExtPageType = ExtPageType;
446 len = le16toh(pagelen) * 4;
448 if (mps_pass_command(fd, &req, sizeof(req), &reply, sizeof(reply),
449 buf, len, NULL, 0, 30)) {
455 reply.IOCStatus = le16toh(reply.IOCStatus);
456 if (!IOC_STATUS_SUCCESS(reply.IOCStatus)) {
457 if (IOCStatus != NULL)
458 *IOCStatus = reply.IOCStatus;
460 warnx("Reading extended config page failed: %s",
461 mps_ioc_status(reply.IOCStatus));
470 mps_firmware_send(int fd, unsigned char *fw, uint32_t len, bool bios)
472 MPI2_FW_DOWNLOAD_REQUEST req;
473 MPI2_FW_DOWNLOAD_REPLY reply;
475 bzero(&req, sizeof(req));
476 bzero(&reply, sizeof(reply));
477 req.Function = MPI2_FUNCTION_FW_DOWNLOAD;
478 req.ImageType = bios ? MPI2_FW_DOWNLOAD_ITYPE_BIOS : MPI2_FW_DOWNLOAD_ITYPE_FW;
479 req.TotalImageSize = htole32(len);
480 req.MsgFlags = MPI2_FW_DOWNLOAD_MSGFLGS_LAST_SEGMENT;
482 if (mps_user_command(fd, &req, sizeof(req), &reply, sizeof(reply),
490 mps_firmware_get(int fd, unsigned char **firmware, bool bios)
492 MPI2_FW_UPLOAD_REQUEST req;
493 MPI2_FW_UPLOAD_REPLY reply;
497 bzero(&req, sizeof(req));
498 bzero(&reply, sizeof(reply));
499 req.Function = MPI2_FUNCTION_FW_UPLOAD;
500 req.ImageType = bios ? MPI2_FW_DOWNLOAD_ITYPE_BIOS : MPI2_FW_DOWNLOAD_ITYPE_FW;
502 if (mps_user_command(fd, &req, sizeof(req), &reply, sizeof(reply),
506 if (reply.ActualImageSize == 0) {
510 size = le32toh(reply.ActualImageSize);
511 *firmware = calloc(size, sizeof(unsigned char));
512 if (*firmware == NULL) {
516 if (mps_user_command(fd, &req, sizeof(req), &reply, sizeof(reply),
517 *firmware, size, 0)) {
528 mps_read_config_page_header(int fd, U8 PageType, U8 PageNumber, U32 PageAddress,
529 MPI2_CONFIG_PAGE_HEADER *header, U16 *IOCStatus)
531 struct mps_cfg_page_req req;
533 if (IOCStatus != NULL)
534 *IOCStatus = MPI2_IOCSTATUS_SUCCESS;
537 bzero(&req, sizeof(req));
538 req.header.PageType = PageType;
539 req.header.PageNumber = PageNumber;
540 req.page_address = PageAddress;
541 if (ioctl(fd, MPSIO_READ_CFG_HEADER, &req) < 0)
543 if (!IOC_STATUS_SUCCESS(req.ioc_status)) {
544 if (IOCStatus != NULL)
545 *IOCStatus = req.ioc_status;
548 bcopy(&req.header, header, sizeof(*header));
553 mps_read_config_page(int fd, U8 PageType, U8 PageNumber, U32 PageAddress,
556 struct mps_cfg_page_req req;
560 error = mps_read_config_page_header(fd, PageType, PageNumber,
561 PageAddress, &req.header, IOCStatus);
567 if (req.header.PageLength == 0)
568 req.header.PageLength = 4;
569 req.len = req.header.PageLength * 4;
570 buf = malloc(req.len);
572 bcopy(&req.header, buf, sizeof(req.header));
573 if (ioctl(fd, MPSIO_READ_CFG_PAGE, &req) < 0) {
579 req.ioc_status = le16toh(req.ioc_status);
580 if (!IOC_STATUS_SUCCESS(req.ioc_status)) {
581 if (IOCStatus != NULL)
582 *IOCStatus = req.ioc_status;
584 warnx("Reading config page failed: 0x%x %s",
585 req.ioc_status, mps_ioc_status(req.ioc_status));
594 mps_read_extended_config_page(int fd, U8 ExtPageType, U8 PageVersion,
595 U8 PageNumber, U32 PageAddress, U16 *IOCStatus)
597 struct mps_ext_cfg_page_req req;
601 if (IOCStatus != NULL)
602 *IOCStatus = MPI2_IOCSTATUS_SUCCESS;
603 bzero(&req, sizeof(req));
604 req.header.PageVersion = PageVersion;
605 req.header.PageNumber = PageNumber;
606 req.header.ExtPageType = ExtPageType;
607 req.page_address = htole32(PageAddress);
608 if (ioctl(fd, MPSIO_READ_EXT_CFG_HEADER, &req) < 0)
610 req.ioc_status = le16toh(req.ioc_status);
611 if (!IOC_STATUS_SUCCESS(req.ioc_status)) {
612 if (IOCStatus != NULL)
613 *IOCStatus = req.ioc_status;
615 warnx("Reading extended config page header failed: %s",
616 mps_ioc_status(req.ioc_status));
620 req.len = req.header.ExtPageLength * 4;
621 buf = malloc(req.len);
623 bcopy(&req.header, buf, sizeof(req.header));
624 if (ioctl(fd, MPSIO_READ_EXT_CFG_PAGE, &req) < 0) {
630 req.ioc_status = le16toh(req.ioc_status);
631 if (!IOC_STATUS_SUCCESS(req.ioc_status)) {
632 if (IOCStatus != NULL)
633 *IOCStatus = req.ioc_status;
635 warnx("Reading extended config page failed: %s",
636 mps_ioc_status(req.ioc_status));
648 char path[MAXPATHLEN];
650 snprintf(path, sizeof(path), "/dev/mp%s%d", is_mps ? "s": "r", unit);
651 return (open(path, O_RDWR));
655 mps_user_command(int fd, void *req, uint32_t req_len, void *reply,
656 uint32_t reply_len, void *buffer, int len, uint32_t flags)
658 struct mps_usr_command cmd;
660 bzero(&cmd, sizeof(struct mps_usr_command));
662 cmd.req_len = req_len;
664 cmd.rpl_len = reply_len;
669 if (ioctl(fd, is_mps ? MPSIO_MPS_COMMAND : MPRIO_MPR_COMMAND, &cmd) < 0)
675 mps_pass_command(int fd, void *req, uint32_t req_len, void *reply,
676 uint32_t reply_len, void *data_in, uint32_t datain_len, void *data_out,
677 uint32_t dataout_len, uint32_t timeout)
679 struct mprs_pass_thru pass;
681 bzero(&pass, sizeof(pass));
682 pass.PtrRequest = (uint64_t)(uintptr_t)req;
683 pass.PtrReply = (uint64_t)(uintptr_t)reply;
684 pass.RequestSize = req_len;
685 pass.ReplySize = reply_len;
686 if (datain_len && dataout_len) {
687 pass.PtrData = (uint64_t)(uintptr_t)data_in;
688 pass.PtrDataOut = (uint64_t)(uintptr_t)data_out;
689 pass.DataSize = datain_len;
690 pass.DataOutSize = dataout_len;
692 pass.DataDirection = MPS_PASS_THRU_DIRECTION_BOTH;
694 pass.DataDirection = MPR_PASS_THRU_DIRECTION_BOTH;
696 } else if (datain_len) {
697 pass.PtrData = (uint64_t)(uintptr_t)data_in;
698 pass.DataSize = datain_len;
700 pass.DataDirection = MPS_PASS_THRU_DIRECTION_READ;
702 pass.DataDirection = MPR_PASS_THRU_DIRECTION_READ;
704 } else if (dataout_len) {
705 pass.PtrData = (uint64_t)(uintptr_t)data_out;
706 pass.DataSize = dataout_len;
708 pass.DataDirection = MPS_PASS_THRU_DIRECTION_WRITE;
710 pass.DataDirection = MPR_PASS_THRU_DIRECTION_WRITE;
714 pass.DataDirection = MPS_PASS_THRU_DIRECTION_NONE;
716 pass.DataDirection = MPR_PASS_THRU_DIRECTION_NONE;
719 pass.Timeout = timeout;
721 if (ioctl(fd, MPTIOCTL_PASS_THRU, &pass) < 0)
726 MPI2_IOC_FACTS_REPLY *
727 mps_get_iocfacts(int fd)
729 MPI2_IOC_FACTS_REPLY *facts;
730 MPI2_IOC_FACTS_REQUEST req;
731 char msgver[8], sysctlname[128];
732 size_t len, factslen;
735 snprintf(sysctlname, sizeof(sysctlname), "dev.%s.%d.msg_version",
736 is_mps ? "mps" : "mpr", mps_unit);
738 factslen = sizeof(MPI2_IOC_FACTS_REPLY);
739 len = sizeof(msgver);
740 error = sysctlbyname(sysctlname, msgver, &len, NULL, 0);
742 if (strncmp(msgver, "2.6", sizeof(msgver)) == 0)
746 facts = malloc(factslen);
752 bzero(&req, factslen);
753 req.Function = MPI2_FUNCTION_IOC_FACTS;
756 error = mps_pass_command(fd, &req, sizeof(MPI2_IOC_FACTS_REQUEST),
757 facts, factslen, NULL, 0, NULL, 0, 10);
759 error = mps_user_command(fd, &req, sizeof(MPI2_IOC_FACTS_REQUEST),
760 facts, factslen, NULL, 0, 0);
767 if (!IOC_STATUS_SUCCESS(facts->IOCStatus)) {
772 adjust_iocfacts_endianness(facts);
777 adjust_iocfacts_endianness(MPI2_IOC_FACTS_REPLY *facts)
779 facts->MsgVersion = le16toh(facts->MsgVersion);
780 facts->HeaderVersion = le16toh(facts->HeaderVersion);
781 facts->Reserved1 = le16toh(facts->Reserved1);
782 facts->IOCExceptions = le16toh(facts->IOCExceptions);
783 facts->IOCStatus = le16toh(facts->IOCStatus);
784 facts->IOCLogInfo = le32toh(facts->IOCLogInfo);
785 facts->RequestCredit = le16toh(facts->RequestCredit);
786 facts->ProductID = le16toh(facts->ProductID);
787 facts->IOCCapabilities = le32toh(facts->IOCCapabilities);
788 facts->IOCRequestFrameSize =
789 le16toh(facts->IOCRequestFrameSize);
790 facts->FWVersion.Word = le32toh(facts->FWVersion.Word);
791 facts->MaxInitiators = le16toh(facts->MaxInitiators);
792 facts->MaxTargets = le16toh(facts->MaxTargets);
793 facts->MaxSasExpanders = le16toh(facts->MaxSasExpanders);
794 facts->MaxEnclosures = le16toh(facts->MaxEnclosures);
795 facts->ProtocolFlags = le16toh(facts->ProtocolFlags);
796 facts->HighPriorityCredit = le16toh(facts->HighPriorityCredit);
797 facts->MaxReplyDescriptorPostQueueDepth =
798 le16toh(facts->MaxReplyDescriptorPostQueueDepth);
799 facts->MaxDevHandle = le16toh(facts->MaxDevHandle);
800 facts->MaxPersistentEntries =
801 le16toh(facts->MaxPersistentEntries);
802 facts->MinDevHandle = le16toh(facts->MinDevHandle);