xref: /freebsd-src/usr.sbin/mpsutil/mps_set.c (revision d56ce5915c41517ccc7de2510daec200e100ef68)
1*e2ea6942SDaniel Austin /*-
2*e2ea6942SDaniel Austin  * SPDX-License-Identifier: BSD-2-Clause
3*e2ea6942SDaniel Austin  *
4*e2ea6942SDaniel Austin  * Copyright (c) 2021 Daniel Austin <freebsd-ports@dan.me.uk>
5*e2ea6942SDaniel Austin  *
6*e2ea6942SDaniel Austin  * Redistribution and use in source and binary forms, with or without
7*e2ea6942SDaniel Austin  * modification, are permitted provided that the following conditions
8*e2ea6942SDaniel Austin  * are met:
9*e2ea6942SDaniel Austin  * 1. Redistributions of source code must retain the above copyright
10*e2ea6942SDaniel Austin  *    notice, this list of conditions and the following disclaimer.
11*e2ea6942SDaniel Austin  * 2. Redistributions in binary form must reproduce the above copyright
12*e2ea6942SDaniel Austin  *    notice, this list of conditions and the following disclaimer in the
13*e2ea6942SDaniel Austin  *    documentation and/or other materials provided with the distribution.
14*e2ea6942SDaniel Austin  *
15*e2ea6942SDaniel Austin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*e2ea6942SDaniel Austin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*e2ea6942SDaniel Austin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*e2ea6942SDaniel Austin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*e2ea6942SDaniel Austin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*e2ea6942SDaniel Austin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*e2ea6942SDaniel Austin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*e2ea6942SDaniel Austin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*e2ea6942SDaniel Austin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*e2ea6942SDaniel Austin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*e2ea6942SDaniel Austin  * SUCH DAMAGE.
26*e2ea6942SDaniel Austin  */
27*e2ea6942SDaniel Austin 
28*e2ea6942SDaniel Austin #include <sys/param.h>
29*e2ea6942SDaniel Austin #include <sys/errno.h>
30*e2ea6942SDaniel Austin #include <err.h>
31*e2ea6942SDaniel Austin #include <stdio.h>
32*e2ea6942SDaniel Austin #include <stdlib.h>
33*e2ea6942SDaniel Austin #include <string.h>
34*e2ea6942SDaniel Austin #include <unistd.h>
35*e2ea6942SDaniel Austin #include "mpsutil.h"
36*e2ea6942SDaniel Austin 
37*e2ea6942SDaniel Austin static int set_ncq(int ac, char **av);
38*e2ea6942SDaniel Austin 
39*e2ea6942SDaniel Austin MPS_TABLE(top, set);
40*e2ea6942SDaniel Austin 
41*e2ea6942SDaniel Austin static int
set_ncq(int ac,char ** av)42*e2ea6942SDaniel Austin set_ncq(int ac, char **av)
43*e2ea6942SDaniel Austin {
44*e2ea6942SDaniel Austin 	MPI2_CONFIG_PAGE_HEADER header;
45*e2ea6942SDaniel Austin 	MPI2_CONFIG_PAGE_IO_UNIT_1 *iounit1;
46*e2ea6942SDaniel Austin 	MPI2_CONFIG_REQUEST req;
47*e2ea6942SDaniel Austin 	MPI2_CONFIG_REPLY reply;
48*e2ea6942SDaniel Austin 	int error, fd;
49*e2ea6942SDaniel Austin 
50*e2ea6942SDaniel Austin 	bzero(&req, sizeof(req));
51*e2ea6942SDaniel Austin 	bzero(&header, sizeof(header));
52*e2ea6942SDaniel Austin 	bzero(&reply, sizeof(reply));
53*e2ea6942SDaniel Austin 
54*e2ea6942SDaniel Austin 	fd = mps_open(mps_unit);
55*e2ea6942SDaniel Austin 	if (fd < 0) {
56*e2ea6942SDaniel Austin 		error = errno;
57*e2ea6942SDaniel Austin 		warn("mps_open");
58*e2ea6942SDaniel Austin 		return (error);
59*e2ea6942SDaniel Austin 	}
60*e2ea6942SDaniel Austin 
61*e2ea6942SDaniel Austin 	error = mps_read_config_page_header(fd, MPI2_CONFIG_PAGETYPE_IO_UNIT, 1, 0,
62*e2ea6942SDaniel Austin 		&header, NULL);
63*e2ea6942SDaniel Austin 	if (error) {
64*e2ea6942SDaniel Austin 			error = errno;
65*e2ea6942SDaniel Austin 			warn("Failed to get IOUNIT page 1 header");
66*e2ea6942SDaniel Austin 			return (error);
67*e2ea6942SDaniel Austin 	}
68*e2ea6942SDaniel Austin 
69*e2ea6942SDaniel Austin 	iounit1 = mps_read_config_page(fd, MPI2_CONFIG_PAGETYPE_IO_UNIT, 1, 0, NULL);
70*e2ea6942SDaniel Austin 	if (iounit1 == NULL) {
71*e2ea6942SDaniel Austin 		error = errno;
72*e2ea6942SDaniel Austin 		warn("Failed to get IOUNIT page 1 info");
73*e2ea6942SDaniel Austin 		return (error);
74*e2ea6942SDaniel Austin 	}
75*e2ea6942SDaniel Austin 
76*e2ea6942SDaniel Austin 	if (ac == 1) {
77*e2ea6942SDaniel Austin 		/* just show current setting */
78*e2ea6942SDaniel Austin 		printf("SATA Native Command Queueing is currently: %s\n",
79*e2ea6942SDaniel Austin 			((iounit1->Flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE) == 0) ?
80*e2ea6942SDaniel Austin 			"ENABLED" : "DISABLED");
81*e2ea6942SDaniel Austin 	} else if (ac == 2) {
82*e2ea6942SDaniel Austin 		if (!strcasecmp(av[1], "enable") || !strcmp(av[1], "1")) {
83*e2ea6942SDaniel Austin 			iounit1->Flags &= ~MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE;
84*e2ea6942SDaniel Austin 		} else if (!strcasecmp(av[1], "disable") || !strcmp(av[1], "0")) {
85*e2ea6942SDaniel Austin 			iounit1->Flags |= MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE;
86*e2ea6942SDaniel Austin 		} else {
87*e2ea6942SDaniel Austin 			free(iounit1);
88*e2ea6942SDaniel Austin 			error = EINVAL;
89*e2ea6942SDaniel Austin 			warn("set ncq: Only 'enable' and 'disable' allowed.");
90*e2ea6942SDaniel Austin 			return (EINVAL);
91*e2ea6942SDaniel Austin 		}
92*e2ea6942SDaniel Austin 		req.Function = MPI2_FUNCTION_CONFIG;
93*e2ea6942SDaniel Austin 		req.Action = MPI2_CONFIG_ACTION_PAGE_WRITE_CURRENT;
94*e2ea6942SDaniel Austin 		req.ExtPageLength = 0;
95*e2ea6942SDaniel Austin 		req.ExtPageType = 0;
96*e2ea6942SDaniel Austin 		req.Header = header;
97*e2ea6942SDaniel Austin 		req.PageAddress = 0;
98*e2ea6942SDaniel Austin 		if (mps_pass_command(fd, &req, sizeof(req) - sizeof(req.PageBufferSGE), &reply, sizeof(reply),
99*e2ea6942SDaniel Austin 			NULL, 0, iounit1, sizeof(iounit1), 30) != 0) {
100*e2ea6942SDaniel Austin 				free(iounit1);
101*e2ea6942SDaniel Austin 				error = errno;
102*e2ea6942SDaniel Austin 				warn("Failed to update config page");
103*e2ea6942SDaniel Austin 		                return (error);
104*e2ea6942SDaniel Austin 		}
105*e2ea6942SDaniel Austin 		if (!IOC_STATUS_SUCCESS(reply.IOCStatus)) {
106*e2ea6942SDaniel Austin 			free(iounit1);
107*e2ea6942SDaniel Austin 			error = errno;
108*e2ea6942SDaniel Austin 			warn("%s", mps_ioc_status(reply.IOCStatus));
109*e2ea6942SDaniel Austin 			return (error);
110*e2ea6942SDaniel Austin 		}
111*e2ea6942SDaniel Austin 		printf("NCQ setting accepted.  It may not take effect until the controller is reset.\n");
112*e2ea6942SDaniel Austin 	} else {
113*e2ea6942SDaniel Austin 		free(iounit1);
114*e2ea6942SDaniel Austin 		errno = EINVAL;
115*e2ea6942SDaniel Austin 		warn("set ncq: too many arguments");
116*e2ea6942SDaniel Austin 		return (EINVAL);
117*e2ea6942SDaniel Austin 	}
118*e2ea6942SDaniel Austin 	free(iounit1);
119*e2ea6942SDaniel Austin 
120*e2ea6942SDaniel Austin 	close(fd);
121*e2ea6942SDaniel Austin 	return (0);
122*e2ea6942SDaniel Austin }
123*e2ea6942SDaniel Austin 
124*e2ea6942SDaniel Austin MPS_COMMAND(set, ncq, set_ncq, "[enable|disable]", "set SATA NCQ function")
125*e2ea6942SDaniel Austin 
126