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