1c2a13d6fSAndriy Gapon /*- 2c2a13d6fSAndriy Gapon * SPDX-License-Identifier: BSD-2-Clause 3c2a13d6fSAndriy Gapon * 4c2a13d6fSAndriy Gapon * Copyright (c) 2019 Andriy Gapon <avg@FreeBSD.org> 5c2a13d6fSAndriy Gapon * 6c2a13d6fSAndriy Gapon * Redistribution and use in source and binary forms, with or without 7c2a13d6fSAndriy Gapon * modification, are permitted provided that the following conditions 8c2a13d6fSAndriy Gapon * are met: 9c2a13d6fSAndriy Gapon * 1. Redistributions of source code must retain the above copyright 10c2a13d6fSAndriy Gapon * notice, this list of conditions and the following disclaimer. 11c2a13d6fSAndriy Gapon * 2. Redistributions in binary form must reproduce the above copyright 12c2a13d6fSAndriy Gapon * notice, this list of conditions and the following disclaimer in the 13c2a13d6fSAndriy Gapon * documentation and/or other materials provided with the distribution. 14c2a13d6fSAndriy Gapon * 15c2a13d6fSAndriy Gapon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16c2a13d6fSAndriy Gapon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17c2a13d6fSAndriy Gapon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18c2a13d6fSAndriy Gapon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19c2a13d6fSAndriy Gapon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20c2a13d6fSAndriy Gapon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21c2a13d6fSAndriy Gapon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22c2a13d6fSAndriy Gapon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23c2a13d6fSAndriy Gapon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24c2a13d6fSAndriy Gapon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25c2a13d6fSAndriy Gapon * SUCH DAMAGE. 26c2a13d6fSAndriy Gapon */ 27c2a13d6fSAndriy Gapon 28c2a13d6fSAndriy Gapon #include <sys/cdefs.h> 29c2a13d6fSAndriy Gapon __RCSID("$FreeBSD$"); 30c2a13d6fSAndriy Gapon 31c2a13d6fSAndriy Gapon #include <sys/stat.h> 32c2a13d6fSAndriy Gapon #include <sys/param.h> 33c2a13d6fSAndriy Gapon #include <sys/mman.h> 34fc9780fdSAlfredo Dal'Ava Junior #include <sys/endian.h> 35c2a13d6fSAndriy Gapon 36c2a13d6fSAndriy Gapon #include <errno.h> 37c2a13d6fSAndriy Gapon #include <err.h> 38c2a13d6fSAndriy Gapon #include <fcntl.h> 39c2a13d6fSAndriy Gapon #include <stdint.h> 40c2a13d6fSAndriy Gapon #include <stdio.h> 41c2a13d6fSAndriy Gapon #include <stdlib.h> 42c2a13d6fSAndriy Gapon #include <string.h> 43c2a13d6fSAndriy Gapon #include <unistd.h> 44c2a13d6fSAndriy Gapon 45c2a13d6fSAndriy Gapon #include "mpsutil.h" 46c2a13d6fSAndriy Gapon 47c2a13d6fSAndriy Gapon MPS_TABLE(top, slot); 48c2a13d6fSAndriy Gapon 49c2a13d6fSAndriy Gapon static int 50c2a13d6fSAndriy Gapon slot_set(int argc, char **argv) 51c2a13d6fSAndriy Gapon { 52c2a13d6fSAndriy Gapon char *endptr; 53c2a13d6fSAndriy Gapon unsigned long ux; 54c2a13d6fSAndriy Gapon long x; 55c2a13d6fSAndriy Gapon int error; 56c2a13d6fSAndriy Gapon int fd; 57c2a13d6fSAndriy Gapon U32 status; 58c2a13d6fSAndriy Gapon U16 handle; 59c2a13d6fSAndriy Gapon U16 slot; 60c2a13d6fSAndriy Gapon 61c2a13d6fSAndriy Gapon if (argc != 5) { 62c2a13d6fSAndriy Gapon warnx("Incorrect number of arguments"); 63c2a13d6fSAndriy Gapon return (EINVAL); 64c2a13d6fSAndriy Gapon } 65c2a13d6fSAndriy Gapon 66c2a13d6fSAndriy Gapon if (strcmp(argv[1], "status") != 0) { 67c2a13d6fSAndriy Gapon warnx("Invalid argument '%s', expecting 'status'", 68c2a13d6fSAndriy Gapon argv[1]); 69c2a13d6fSAndriy Gapon return (EINVAL); 70c2a13d6fSAndriy Gapon } 71c2a13d6fSAndriy Gapon 72c2a13d6fSAndriy Gapon errno = 0; 73c2a13d6fSAndriy Gapon x = strtol(argv[2], &endptr, 0); 74c2a13d6fSAndriy Gapon if (*endptr != '\0' || errno != 0 || x < 0 || x > UINT16_MAX) { 75c2a13d6fSAndriy Gapon warnx("Invalid enclosure handle argument '%s'", argv[2]); 76c2a13d6fSAndriy Gapon return (EINVAL); 77c2a13d6fSAndriy Gapon } 78c2a13d6fSAndriy Gapon handle = x; 79c2a13d6fSAndriy Gapon 80c2a13d6fSAndriy Gapon errno = 0; 81c2a13d6fSAndriy Gapon x = strtol(argv[3], &endptr, 0); 82c2a13d6fSAndriy Gapon if (*endptr != '\0' || errno != 0 || x < 0 || x > UINT16_MAX) { 83c2a13d6fSAndriy Gapon warnx("Invalid slot argument '%s'", argv[3]); 84c2a13d6fSAndriy Gapon return (EINVAL); 85c2a13d6fSAndriy Gapon } 86c2a13d6fSAndriy Gapon slot = x; 87c2a13d6fSAndriy Gapon 88c2a13d6fSAndriy Gapon errno = 0; 89c2a13d6fSAndriy Gapon ux = strtoul(argv[4], &endptr, 0); 90c2a13d6fSAndriy Gapon if (*endptr != '\0' || errno != 0 || ux > UINT32_MAX) { 91c2a13d6fSAndriy Gapon warnx("Invalid status argument '%s'", argv[4]); 92c2a13d6fSAndriy Gapon return (EINVAL); 93c2a13d6fSAndriy Gapon } 94c2a13d6fSAndriy Gapon status = ux; 95c2a13d6fSAndriy Gapon 96c2a13d6fSAndriy Gapon fd = mps_open(mps_unit); 97c2a13d6fSAndriy Gapon if (fd < 0) { 98c2a13d6fSAndriy Gapon error = errno; 99c2a13d6fSAndriy Gapon warn("mps_open"); 100c2a13d6fSAndriy Gapon return (error); 101c2a13d6fSAndriy Gapon } 102c2a13d6fSAndriy Gapon 103fc9780fdSAlfredo Dal'Ava Junior if (mps_set_slot_status(fd, htole16(handle), htole16(slot), 104fc9780fdSAlfredo Dal'Ava Junior htole32(status)) != 0) { 105c2a13d6fSAndriy Gapon warnx("Failed to set status"); 106c2a13d6fSAndriy Gapon close(fd); 107c2a13d6fSAndriy Gapon return (1); 108c2a13d6fSAndriy Gapon } 109c2a13d6fSAndriy Gapon 110c2a13d6fSAndriy Gapon close(fd); 111c2a13d6fSAndriy Gapon printf("Successfully set slot status\n"); 112c2a13d6fSAndriy Gapon return (0); 113c2a13d6fSAndriy Gapon } 114c2a13d6fSAndriy Gapon 115c2a13d6fSAndriy Gapon MPS_COMMAND(slot, set, slot_set, "status <enclosure handle> <slot number> " 116*638e2a13SScott Long "<status>", "\n Set status of the slot in the directly attached enclosure"); 117