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/param.h>
37 #include <sys/errno.h>
38 #include <sys/ioctl.h>
39 #include <sys/sysctl.h>
41 #include <sys/endian.h>
51 #include <dev/mps/mps_ioctl.h>
52 #include <dev/mpr/mpr_ioctl.h>
54 #ifndef USE_MPT_IOCTLS
55 #define USE_MPT_IOCTLS
58 static const char *mps_ioc_status_codes[] = {
59 "Success", /* 0x0000 */
62 "Invalid scatter-gather list",
65 "Insufficient resources",
67 "Invalid state", /* 0x0008 */
68 "Operation state not supported",
91 "Invalid configuration action", /* 0x0020 */
92 "Invalid configuration type",
93 "Invalid configuration page",
94 "Invalid configuration data",
95 "No configuration defaults",
96 "Unable to commit configuration change",
123 "Recovered SCSI error", /* 0x0040 */
125 "Invalid SCSI target ID",
126 "SCSI device not there",
128 "SCSI data underrun",
130 "SCSI protocol error",
131 "SCSI task terminated", /* 0x0048 */
132 "SCSI residual mismatch",
133 "SCSI task management failed",
134 "SCSI I/O controller terminated",
135 "SCSI external controller terminated",
137 "EEDP reference tag error",
138 "EEDP application tag error",
155 "SCSI target priority I/O", /* 0x0060 */
156 "Invalid SCSI target port",
157 "Invalid SCSI target I/O index",
158 "SCSI target aborted",
159 "No connection retryable",
162 "Invalid FC receive ID",
163 "FC did invalid", /* 0x0068 */
164 "FC node logged out",
165 "Transfer count mismatch",
167 "FC exchange canceled",
169 "Too much write data",
171 "ACK NAK timeout", /* 0x0070 */
187 "LAN device not found", /* 0x0080 */
188 "LAN device failure",
189 "LAN transmit error",
190 "LAN transmit aborted",
192 "LAN receive aborted",
193 "LAN partial packet",
203 "SAS SMP request failed", /* 0x0090 */
204 "SAS SMP data overrun",
211 "Inband aborted", /* 0x0098 */
212 "No inband connection",
219 "Diagnostic released", /* 0x00A0 */
222 struct mprs_pass_thru {
226 uint32_t RequestSize;
229 uint32_t DataDirection;
231 uint32_t DataOutSize;
235 struct mprs_btdh_mapping {
242 static void adjust_iocfacts_endianness(MPI2_IOC_FACTS_REPLY *facts);
245 mps_ioc_status(U16 IOCStatus)
247 static char buffer[16];
249 IOCStatus &= MPI2_IOCSTATUS_MASK;
250 if (IOCStatus < sizeof(mps_ioc_status_codes) / sizeof(char *) &&
251 mps_ioc_status_codes[IOCStatus] != NULL)
252 return (mps_ioc_status_codes[IOCStatus]);
253 snprintf(buffer, sizeof(buffer), "Status: 0x%04x", IOCStatus);
257 #ifdef USE_MPT_IOCTLS
259 mps_map_btdh(int fd, uint16_t *devhandle, uint16_t *bus, uint16_t *target)
262 struct mprs_btdh_mapping map;
265 map.TargetID = *target;
266 map.DevHandle = *devhandle;
268 if ((error = ioctl(fd, MPTIOCTL_BTDH_MAPPING, &map)) != 0) {
270 warn("Failed to map bus/target/device");
275 *target = map.TargetID;
276 *devhandle = map.DevHandle;
282 mps_set_slot_status(int fd, U16 handle, U16 slot, U32 status)
284 MPI2_SEP_REQUEST req;
285 MPI2_SEP_REPLY reply;
287 bzero(&req, sizeof(req));
288 req.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
289 req.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
290 req.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
291 req.EnclosureHandle = handle;
293 req.SlotStatus = status;
295 if (mps_pass_command(fd, &req, sizeof(req), &reply, sizeof(reply),
296 NULL, 0, NULL, 0, 30) != 0)
299 if (!IOC_STATUS_SUCCESS(le16toh(reply.IOCStatus)))
305 mps_read_config_page_header(int fd, U8 PageType, U8 PageNumber, U32 PageAddress,
306 MPI2_CONFIG_PAGE_HEADER *header, U16 *IOCStatus)
308 MPI2_CONFIG_REQUEST req;
309 MPI2_CONFIG_REPLY reply;
311 bzero(&req, sizeof(req));
312 req.Function = MPI2_FUNCTION_CONFIG;
313 req.Action = MPI2_CONFIG_ACTION_PAGE_HEADER;
314 req.Header.PageType = PageType;
315 req.Header.PageNumber = PageNumber;
316 req.PageAddress = PageAddress;
318 if (mps_pass_command(fd, &req, sizeof(req), &reply, sizeof(reply),
319 NULL, 0, NULL, 0, 30))
322 if (!IOC_STATUS_SUCCESS(le16toh(reply.IOCStatus))) {
323 if (IOCStatus != NULL)
324 *IOCStatus = reply.IOCStatus;
329 *header = reply.Header;
334 mps_read_ext_config_page_header(int fd, U8 ExtPageType, U8 PageNumber, U32 PageAddress, MPI2_CONFIG_PAGE_HEADER *header, U16 *ExtPageLength, U16 *IOCStatus)
336 MPI2_CONFIG_REQUEST req;
337 MPI2_CONFIG_REPLY reply;
339 bzero(&req, sizeof(req));
340 req.Function = MPI2_FUNCTION_CONFIG;
341 req.Action = MPI2_CONFIG_ACTION_PAGE_HEADER;
342 req.Header.PageType = MPI2_CONFIG_PAGETYPE_EXTENDED;
343 req.ExtPageType = ExtPageType;
344 req.Header.PageNumber = PageNumber;
345 req.PageAddress = htole32(PageAddress);
347 if (mps_pass_command(fd, &req, sizeof(req), &reply, sizeof(reply),
348 NULL, 0, NULL, 0, 30))
351 if (!IOC_STATUS_SUCCESS(le16toh(reply.IOCStatus))) {
352 if (IOCStatus != NULL)
353 *IOCStatus = le16toh(reply.IOCStatus);
356 if ((header == NULL) || (ExtPageLength == NULL))
358 *header = reply.Header;
359 *ExtPageLength = reply.ExtPageLength;
364 mps_read_config_page(int fd, U8 PageType, U8 PageNumber, U32 PageAddress,
367 MPI2_CONFIG_REQUEST req;
368 MPI2_CONFIG_PAGE_HEADER header;
369 MPI2_CONFIG_REPLY reply;
373 bzero(&header, sizeof(header));
374 error = mps_read_config_page_header(fd, PageType, PageNumber,
375 PageAddress, &header, IOCStatus);
381 bzero(&req, sizeof(req));
382 req.Function = MPI2_FUNCTION_CONFIG;
383 req.Action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
384 req.PageAddress = htole32(PageAddress);
386 if (req.Header.PageLength == 0)
387 req.Header.PageLength = 4;
389 len = req.Header.PageLength * 4;
391 if (mps_pass_command(fd, &req, sizeof(req), &reply, sizeof(reply),
392 buf, len, NULL, 0, 30)) {
398 reply.IOCStatus = le16toh(reply.IOCStatus);
399 if (!IOC_STATUS_SUCCESS(reply.IOCStatus)) {
400 if (IOCStatus != NULL)
401 *IOCStatus = reply.IOCStatus;
403 warnx("Reading config page failed: 0x%x %s",
404 reply.IOCStatus, mps_ioc_status(reply.IOCStatus));
413 mps_read_extended_config_page(int fd, U8 ExtPageType, U8 PageVersion,
414 U8 PageNumber, U32 PageAddress, U16 *IOCStatus)
416 MPI2_CONFIG_REQUEST req;
417 MPI2_CONFIG_PAGE_HEADER header;
418 MPI2_CONFIG_REPLY reply;
423 if (IOCStatus != NULL)
424 *IOCStatus = MPI2_IOCSTATUS_SUCCESS;
425 bzero(&header, sizeof(header));
426 error = mps_read_ext_config_page_header(fd, ExtPageType, PageNumber,
427 PageAddress, &header, &pagelen, IOCStatus);
433 bzero(&req, sizeof(req));
434 req.Function = MPI2_FUNCTION_CONFIG;
435 req.Action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
436 req.PageAddress = htole32(PageAddress);
439 pagelen = htole16(4);
440 req.ExtPageLength = pagelen;
441 req.ExtPageType = ExtPageType;
443 len = le16toh(pagelen) * 4;
445 if (mps_pass_command(fd, &req, sizeof(req), &reply, sizeof(reply),
446 buf, len, NULL, 0, 30)) {
452 reply.IOCStatus = le16toh(reply.IOCStatus);
453 if (!IOC_STATUS_SUCCESS(reply.IOCStatus)) {
454 if (IOCStatus != NULL)
455 *IOCStatus = reply.IOCStatus;
457 warnx("Reading extended config page failed: %s",
458 mps_ioc_status(reply.IOCStatus));
467 mps_firmware_send(int fd, unsigned char *fw, uint32_t len, bool bios)
469 MPI2_FW_DOWNLOAD_REQUEST req;
470 MPI2_FW_DOWNLOAD_REPLY reply;
472 bzero(&req, sizeof(req));
473 bzero(&reply, sizeof(reply));
474 req.Function = MPI2_FUNCTION_FW_DOWNLOAD;
475 req.ImageType = bios ? MPI2_FW_DOWNLOAD_ITYPE_BIOS : MPI2_FW_DOWNLOAD_ITYPE_FW;
476 req.TotalImageSize = htole32(len);
477 req.MsgFlags = MPI2_FW_DOWNLOAD_MSGFLGS_LAST_SEGMENT;
479 if (mps_user_command(fd, &req, sizeof(req), &reply, sizeof(reply),
487 mps_firmware_get(int fd, unsigned char **firmware, bool bios)
489 MPI2_FW_UPLOAD_REQUEST req;
490 MPI2_FW_UPLOAD_REPLY reply;
494 bzero(&req, sizeof(req));
495 bzero(&reply, sizeof(reply));
496 req.Function = MPI2_FUNCTION_FW_UPLOAD;
497 req.ImageType = bios ? MPI2_FW_DOWNLOAD_ITYPE_BIOS : MPI2_FW_DOWNLOAD_ITYPE_FW;
499 if (mps_user_command(fd, &req, sizeof(req), &reply, sizeof(reply),
503 if (reply.ActualImageSize == 0) {
507 size = le32toh(reply.ActualImageSize);
508 *firmware = calloc(size, sizeof(unsigned char));
509 if (*firmware == NULL) {
513 if (mps_user_command(fd, &req, sizeof(req), &reply, sizeof(reply),
514 *firmware, size, 0)) {
525 mps_read_config_page_header(int fd, U8 PageType, U8 PageNumber, U32 PageAddress,
526 MPI2_CONFIG_PAGE_HEADER *header, U16 *IOCStatus)
528 struct mps_cfg_page_req req;
530 if (IOCStatus != NULL)
531 *IOCStatus = MPI2_IOCSTATUS_SUCCESS;
534 bzero(&req, sizeof(req));
535 req.header.PageType = PageType;
536 req.header.PageNumber = PageNumber;
537 req.page_address = PageAddress;
538 if (ioctl(fd, MPSIO_READ_CFG_HEADER, &req) < 0)
540 if (!IOC_STATUS_SUCCESS(req.ioc_status)) {
541 if (IOCStatus != NULL)
542 *IOCStatus = req.ioc_status;
545 bcopy(&req.header, header, sizeof(*header));
550 mps_read_config_page(int fd, U8 PageType, U8 PageNumber, U32 PageAddress,
553 struct mps_cfg_page_req req;
557 error = mps_read_config_page_header(fd, PageType, PageNumber,
558 PageAddress, &req.header, IOCStatus);
564 if (req.header.PageLength == 0)
565 req.header.PageLength = 4;
566 req.len = req.header.PageLength * 4;
567 buf = malloc(req.len);
569 bcopy(&req.header, buf, sizeof(req.header));
570 if (ioctl(fd, MPSIO_READ_CFG_PAGE, &req) < 0) {
576 req.ioc_status = le16toh(req.ioc_status);
577 if (!IOC_STATUS_SUCCESS(req.ioc_status)) {
578 if (IOCStatus != NULL)
579 *IOCStatus = req.ioc_status;
581 warnx("Reading config page failed: 0x%x %s",
582 req.ioc_status, mps_ioc_status(req.ioc_status));
591 mps_read_extended_config_page(int fd, U8 ExtPageType, U8 PageVersion,
592 U8 PageNumber, U32 PageAddress, U16 *IOCStatus)
594 struct mps_ext_cfg_page_req req;
598 if (IOCStatus != NULL)
599 *IOCStatus = MPI2_IOCSTATUS_SUCCESS;
600 bzero(&req, sizeof(req));
601 req.header.PageVersion = PageVersion;
602 req.header.PageNumber = PageNumber;
603 req.header.ExtPageType = ExtPageType;
604 req.page_address = htole32(PageAddress);
605 if (ioctl(fd, MPSIO_READ_EXT_CFG_HEADER, &req) < 0)
607 req.ioc_status = le16toh(req.ioc_status);
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 req.ioc_status = le16toh(req.ioc_status);
628 if (!IOC_STATUS_SUCCESS(req.ioc_status)) {
629 if (IOCStatus != NULL)
630 *IOCStatus = req.ioc_status;
632 warnx("Reading extended config page failed: %s",
633 mps_ioc_status(req.ioc_status));
645 char path[MAXPATHLEN];
647 snprintf(path, sizeof(path), "/dev/mp%s%d", is_mps ? "s": "r", unit);
648 return (open(path, O_RDWR));
652 mps_user_command(int fd, void *req, uint32_t req_len, void *reply,
653 uint32_t reply_len, void *buffer, int len, uint32_t flags)
655 struct mps_usr_command cmd;
657 bzero(&cmd, sizeof(struct mps_usr_command));
659 cmd.req_len = req_len;
661 cmd.rpl_len = reply_len;
666 if (ioctl(fd, is_mps ? MPSIO_MPS_COMMAND : MPRIO_MPR_COMMAND, &cmd) < 0)
672 mps_pass_command(int fd, void *req, uint32_t req_len, void *reply,
673 uint32_t reply_len, void *data_in, uint32_t datain_len, void *data_out,
674 uint32_t dataout_len, uint32_t timeout)
676 struct mprs_pass_thru pass;
678 bzero(&pass, sizeof(pass));
679 pass.PtrRequest = (uint64_t)(uintptr_t)req;
680 pass.PtrReply = (uint64_t)(uintptr_t)reply;
681 pass.RequestSize = req_len;
682 pass.ReplySize = reply_len;
683 if (datain_len && dataout_len) {
684 pass.PtrData = (uint64_t)(uintptr_t)data_in;
685 pass.PtrDataOut = (uint64_t)(uintptr_t)data_out;
686 pass.DataSize = datain_len;
687 pass.DataOutSize = dataout_len;
689 pass.DataDirection = MPS_PASS_THRU_DIRECTION_BOTH;
691 pass.DataDirection = MPR_PASS_THRU_DIRECTION_BOTH;
693 } else if (datain_len) {
694 pass.PtrData = (uint64_t)(uintptr_t)data_in;
695 pass.DataSize = datain_len;
697 pass.DataDirection = MPS_PASS_THRU_DIRECTION_READ;
699 pass.DataDirection = MPR_PASS_THRU_DIRECTION_READ;
701 } else if (dataout_len) {
702 pass.PtrData = (uint64_t)(uintptr_t)data_out;
703 pass.DataSize = dataout_len;
705 pass.DataDirection = MPS_PASS_THRU_DIRECTION_WRITE;
707 pass.DataDirection = MPR_PASS_THRU_DIRECTION_WRITE;
711 pass.DataDirection = MPS_PASS_THRU_DIRECTION_NONE;
713 pass.DataDirection = MPR_PASS_THRU_DIRECTION_NONE;
716 pass.Timeout = timeout;
718 if (ioctl(fd, MPTIOCTL_PASS_THRU, &pass) < 0)
723 /* Return the length in bytes of the device's MPI2_IOC_FACTS reply */
725 mps_get_ioc_factslen(int fd)
727 MPI2_IOC_FACTS_REQUEST req;
728 const size_t factslen = 4;
729 char factsbuf[4] = {0};
730 MPI2_IOC_FACTS_REPLY *facts = (MPI2_IOC_FACTS_REPLY*)factsbuf;
733 bzero(&req, sizeof(req));
734 req.Function = MPI2_FUNCTION_IOC_FACTS;
735 error = mps_pass_command(fd, &req, sizeof(MPI2_IOC_FACTS_REQUEST),
736 factsbuf, factslen, NULL, 0, NULL, 0, 10);
741 /* The card's response is measured in dwords */
742 return (facts->MsgLength * 4);
745 MPI2_IOC_FACTS_REPLY *
746 mps_get_iocfacts(int fd)
748 MPI2_IOC_FACTS_REPLY *facts;
749 MPI2_IOC_FACTS_REQUEST req;
753 factslen = mps_get_ioc_factslen(fd);
757 facts = malloc(factslen);
763 bzero(&req, sizeof(req));
764 req.Function = MPI2_FUNCTION_IOC_FACTS;
767 error = mps_pass_command(fd, &req, sizeof(MPI2_IOC_FACTS_REQUEST),
768 facts, factslen, NULL, 0, NULL, 0, 10);
770 error = mps_user_command(fd, &req, sizeof(MPI2_IOC_FACTS_REQUEST),
771 facts, factslen, NULL, 0, 0);
778 if (!IOC_STATUS_SUCCESS(facts->IOCStatus)) {
783 adjust_iocfacts_endianness(facts);
788 adjust_iocfacts_endianness(MPI2_IOC_FACTS_REPLY *facts)
790 facts->MsgVersion = le16toh(facts->MsgVersion);
791 facts->HeaderVersion = le16toh(facts->HeaderVersion);
792 facts->Reserved1 = le16toh(facts->Reserved1);
793 facts->IOCExceptions = le16toh(facts->IOCExceptions);
794 facts->IOCStatus = le16toh(facts->IOCStatus);
795 facts->IOCLogInfo = le32toh(facts->IOCLogInfo);
796 facts->RequestCredit = le16toh(facts->RequestCredit);
797 facts->ProductID = le16toh(facts->ProductID);
798 facts->IOCCapabilities = le32toh(facts->IOCCapabilities);
799 facts->IOCRequestFrameSize =
800 le16toh(facts->IOCRequestFrameSize);
801 facts->FWVersion.Word = le32toh(facts->FWVersion.Word);
802 facts->MaxInitiators = le16toh(facts->MaxInitiators);
803 facts->MaxTargets = le16toh(facts->MaxTargets);
804 facts->MaxSasExpanders = le16toh(facts->MaxSasExpanders);
805 facts->MaxEnclosures = le16toh(facts->MaxEnclosures);
806 facts->ProtocolFlags = le16toh(facts->ProtocolFlags);
807 facts->HighPriorityCredit = le16toh(facts->HighPriorityCredit);
808 facts->MaxReplyDescriptorPostQueueDepth =
809 le16toh(facts->MaxReplyDescriptorPostQueueDepth);
810 facts->MaxDevHandle = le16toh(facts->MaxDevHandle);
811 facts->MaxPersistentEntries =
812 le16toh(facts->MaxPersistentEntries);
813 facts->MinDevHandle = le16toh(facts->MinDevHandle);