2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2021 Daniel Austin <freebsd-ports@dan.me.uk>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/errno.h>
41 static int set_ncq(int ac, char **av);
46 set_ncq(int ac, char **av)
48 MPI2_CONFIG_PAGE_HEADER header;
49 MPI2_CONFIG_PAGE_IO_UNIT_1 *iounit1;
50 MPI2_CONFIG_REQUEST req;
51 MPI2_CONFIG_REPLY reply;
54 bzero(&req, sizeof(req));
55 bzero(&header, sizeof(header));
56 bzero(&reply, sizeof(reply));
58 fd = mps_open(mps_unit);
65 error = mps_read_config_page_header(fd, MPI2_CONFIG_PAGETYPE_IO_UNIT, 1, 0,
69 warn("Failed to get IOUNIT page 1 header");
73 iounit1 = mps_read_config_page(fd, MPI2_CONFIG_PAGETYPE_IO_UNIT, 1, 0, NULL);
74 if (iounit1 == NULL) {
76 warn("Failed to get IOUNIT page 1 info");
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");
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;
93 warn("set ncq: Only 'enable' and 'disable' allowed.");
96 req.Function = MPI2_FUNCTION_CONFIG;
97 req.Action = MPI2_CONFIG_ACTION_PAGE_WRITE_CURRENT;
98 req.ExtPageLength = 0;
102 if (mps_pass_command(fd, &req, sizeof(req) - sizeof(req.PageBufferSGE), &reply, sizeof(reply),
103 NULL, 0, iounit1, sizeof(iounit1), 30) != 0) {
106 warn("Failed to update config page");
109 if (!IOC_STATUS_SUCCESS(reply.IOCStatus)) {
112 warn("%s", mps_ioc_status(reply.IOCStatus));
115 printf("NCQ setting accepted. It may not take effect until the controller is reset.\n");
119 warn("set ncq: too many arguments");
128 MPS_COMMAND(set, ncq, set_ncq, "[enable|disable]", "set SATA NCQ function")