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