xref: /freebsd/usr.sbin/mpsutil/mps_set.c (revision 1843dfb05ed80149f5a412180af882e3cb8f451b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021 Daniel Austin <freebsd-ports@dan.me.uk>
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 #include <sys/param.h>
30 #include <sys/errno.h>
31 #include <err.h>
32 #include <libutil.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include "mpsutil.h"
38 
39 static int set_ncq(int ac, char **av);
40 
41 MPS_TABLE(top, set);
42 
43 static int
44 set_ncq(int ac, char **av)
45 {
46 	MPI2_CONFIG_PAGE_HEADER header;
47 	MPI2_CONFIG_PAGE_IO_UNIT_1 *iounit1;
48 	MPI2_CONFIG_REQUEST req;
49 	MPI2_CONFIG_REPLY reply;
50 	int error, fd;
51 
52 	bzero(&req, sizeof(req));
53 	bzero(&header, sizeof(header));
54 	bzero(&reply, sizeof(reply));
55 
56 	fd = mps_open(mps_unit);
57 	if (fd < 0) {
58 		error = errno;
59 		warn("mps_open");
60 		return (error);
61 	}
62 
63 	error = mps_read_config_page_header(fd, MPI2_CONFIG_PAGETYPE_IO_UNIT, 1, 0,
64 		&header, NULL);
65 	if (error) {
66 			error = errno;
67 			warn("Failed to get IOUNIT page 1 header");
68 			return (error);
69 	}
70 
71 	iounit1 = mps_read_config_page(fd, MPI2_CONFIG_PAGETYPE_IO_UNIT, 1, 0, NULL);
72 	if (iounit1 == NULL) {
73 		error = errno;
74 		warn("Failed to get IOUNIT page 1 info");
75 		return (error);
76 	}
77 
78 	if (ac == 1) {
79 		/* just show current setting */
80 		printf("SATA Native Command Queueing is currently: %s\n",
81 			((iounit1->Flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE) == 0) ?
82 			"ENABLED" : "DISABLED");
83 	} else if (ac == 2) {
84 		if (!strcasecmp(av[1], "enable") || !strcmp(av[1], "1")) {
85 			iounit1->Flags &= ~MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE;
86 		} else if (!strcasecmp(av[1], "disable") || !strcmp(av[1], "0")) {
87 			iounit1->Flags |= MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE;
88 		} else {
89 			free(iounit1);
90 			error = EINVAL;
91 			warn("set ncq: Only 'enable' and 'disable' allowed.");
92 			return (EINVAL);
93 		}
94 		req.Function = MPI2_FUNCTION_CONFIG;
95 		req.Action = MPI2_CONFIG_ACTION_PAGE_WRITE_CURRENT;
96 		req.ExtPageLength = 0;
97 		req.ExtPageType = 0;
98 		req.Header = header;
99 		req.PageAddress = 0;
100 		if (mps_pass_command(fd, &req, sizeof(req) - sizeof(req.PageBufferSGE), &reply, sizeof(reply),
101 			NULL, 0, iounit1, sizeof(iounit1), 30) != 0) {
102 				free(iounit1);
103 				error = errno;
104 				warn("Failed to update config page");
105 		                return (error);
106 		}
107 		if (!IOC_STATUS_SUCCESS(reply.IOCStatus)) {
108 			free(iounit1);
109 			error = errno;
110 			warn("%s", mps_ioc_status(reply.IOCStatus));
111 			return (error);
112 		}
113 		printf("NCQ setting accepted.  It may not take effect until the controller is reset.\n");
114 	} else {
115 		free(iounit1);
116 		errno = EINVAL;
117 		warn("set ncq: too many arguments");
118 		return (EINVAL);
119 	}
120 	free(iounit1);
121 
122 	close(fd);
123 	return (0);
124 }
125 
126 MPS_COMMAND(set, ncq, set_ncq, "[enable|disable]", "set SATA NCQ function")
127 
128