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