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