1ee7a2acaSSean Bruno /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
4ee7a2acaSSean Bruno * Copyright (c) 2013 Yahoo!, Inc.
5ee7a2acaSSean Bruno * All rights reserved.
6ee7a2acaSSean Bruno *
7ee7a2acaSSean Bruno * Redistribution and use in source and binary forms, with or without
8ee7a2acaSSean Bruno * modification, are permitted provided that the following conditions
9ee7a2acaSSean Bruno * are met:
10ee7a2acaSSean Bruno * 1. Redistributions of source code must retain the above copyright
11ee7a2acaSSean Bruno * notice, this list of conditions and the following disclaimer.
12ee7a2acaSSean Bruno * 2. Redistributions in binary form must reproduce the above copyright
13ee7a2acaSSean Bruno * notice, this list of conditions and the following disclaimer in the
14ee7a2acaSSean Bruno * documentation and/or other materials provided with the distribution.
15ee7a2acaSSean Bruno *
16ee7a2acaSSean Bruno * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17ee7a2acaSSean Bruno * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18ee7a2acaSSean Bruno * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ee7a2acaSSean Bruno * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20ee7a2acaSSean Bruno * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21ee7a2acaSSean Bruno * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22ee7a2acaSSean Bruno * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23ee7a2acaSSean Bruno * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24ee7a2acaSSean Bruno * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25ee7a2acaSSean Bruno * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26ee7a2acaSSean Bruno * SUCH DAMAGE.
27ee7a2acaSSean Bruno *
28ee7a2acaSSean Bruno */
29ee7a2acaSSean Bruno
30ee7a2acaSSean Bruno #include <sys/errno.h>
31ee7a2acaSSean Bruno #include <sys/ioctl.h>
32ee7a2acaSSean Bruno #include <sys/param.h>
33ee7a2acaSSean Bruno #include <sys/sysctl.h>
34ee7a2acaSSean Bruno #include <sys/uio.h>
35ee7a2acaSSean Bruno
36ee7a2acaSSean Bruno #include <err.h>
37ee7a2acaSSean Bruno #include <fcntl.h>
38ee7a2acaSSean Bruno #include <stdio.h>
39ee7a2acaSSean Bruno #include <stdlib.h>
40ee7a2acaSSean Bruno #include <string.h>
41ee7a2acaSSean Bruno #include <unistd.h>
42ee7a2acaSSean Bruno
43ee7a2acaSSean Bruno #include "mfiutil.h"
44ee7a2acaSSean Bruno #include <dev/mfi/mfi_ioctl.h>
45ee7a2acaSSean Bruno
46ee7a2acaSSean Bruno MFI_TABLE(top, ctrlprop);
47ee7a2acaSSean Bruno
48ee7a2acaSSean Bruno static int
mfi_ctrl_get_properties(int fd,struct mfi_ctrl_props * info)49ee7a2acaSSean Bruno mfi_ctrl_get_properties(int fd, struct mfi_ctrl_props *info)
50ee7a2acaSSean Bruno {
51ee7a2acaSSean Bruno
52ee7a2acaSSean Bruno return (mfi_dcmd_command(fd, MFI_DCMD_CTRL_GET_PROPS, info,
53ee7a2acaSSean Bruno sizeof(struct mfi_ctrl_props), NULL, 0, NULL));
54ee7a2acaSSean Bruno }
55ee7a2acaSSean Bruno
56ee7a2acaSSean Bruno static int
mfi_ctrl_set_properties(int fd,struct mfi_ctrl_props * info)57ee7a2acaSSean Bruno mfi_ctrl_set_properties(int fd, struct mfi_ctrl_props *info)
58ee7a2acaSSean Bruno {
59ee7a2acaSSean Bruno
60ee7a2acaSSean Bruno return (mfi_dcmd_command(fd, MFI_DCMD_CTRL_SET_PROPS, info,
61ee7a2acaSSean Bruno sizeof(struct mfi_ctrl_props), NULL, 0, NULL));
62ee7a2acaSSean Bruno }
63ee7a2acaSSean Bruno
64ee7a2acaSSean Bruno /*
65*ed96987bSWarner Losh * Acquire the controller properties data structure, modify the
6656ca9ef0SElyes Haouas * rebuild rate if requested and then return
67ee7a2acaSSean Bruno */
68ee7a2acaSSean Bruno static int
mfi_ctrl_rebuild_rate(int ac,char ** av)69ee7a2acaSSean Bruno mfi_ctrl_rebuild_rate(int ac, char **av)
70ee7a2acaSSean Bruno {
71ee7a2acaSSean Bruno int error, fd;
72ee7a2acaSSean Bruno struct mfi_ctrl_props ctrl_props;
73ee7a2acaSSean Bruno
74ee7a2acaSSean Bruno if (ac > 2) {
75ee7a2acaSSean Bruno warn("mfi_ctrl_set_rebuild_rate");
76ee7a2acaSSean Bruno return(-1);
77ee7a2acaSSean Bruno }
78ee7a2acaSSean Bruno
797e0f8b79SDoug Ambrisko fd = mfi_open(mfi_device, O_RDWR);
80ee7a2acaSSean Bruno if (fd < 0) {
81ee7a2acaSSean Bruno error = errno;
82ee7a2acaSSean Bruno warn("mfi_open");
83ee7a2acaSSean Bruno return (error);
84ee7a2acaSSean Bruno }
85ee7a2acaSSean Bruno
86ee7a2acaSSean Bruno error = mfi_ctrl_get_properties(fd, &ctrl_props);
87ee7a2acaSSean Bruno if ( error < 0) {
88ee7a2acaSSean Bruno error = errno;
89ee7a2acaSSean Bruno warn("Failed to get controller properties");
90ee7a2acaSSean Bruno close(fd);
91ee7a2acaSSean Bruno return (error);
92ee7a2acaSSean Bruno }
93ee7a2acaSSean Bruno /*
94ee7a2acaSSean Bruno * User requested a change to the rebuild rate
95ee7a2acaSSean Bruno */
96ee7a2acaSSean Bruno if (ac > 1) {
97ee7a2acaSSean Bruno ctrl_props.rebuild_rate = atoi(av[ac - 1]);
98ee7a2acaSSean Bruno error = mfi_ctrl_set_properties(fd, &ctrl_props);
99ee7a2acaSSean Bruno if ( error < 0) {
100ee7a2acaSSean Bruno error = errno;
101ee7a2acaSSean Bruno warn("Failed to set controller properties");
102ee7a2acaSSean Bruno close(fd);
103ee7a2acaSSean Bruno return (error);
104ee7a2acaSSean Bruno }
105ee7a2acaSSean Bruno
106ee7a2acaSSean Bruno error = mfi_ctrl_get_properties(fd, &ctrl_props);
107ee7a2acaSSean Bruno if ( error < 0) {
108ee7a2acaSSean Bruno error = errno;
109ee7a2acaSSean Bruno warn("Failed to get controller properties");
110ee7a2acaSSean Bruno close(fd);
111ee7a2acaSSean Bruno return (error);
112ee7a2acaSSean Bruno }
113ee7a2acaSSean Bruno }
114ee7a2acaSSean Bruno printf ("controller rebuild rate: %%%u \n",
115ee7a2acaSSean Bruno ctrl_props.rebuild_rate);
116ee7a2acaSSean Bruno return (0);
117ee7a2acaSSean Bruno }
118ee7a2acaSSean Bruno MFI_COMMAND(ctrlprop, rebuild, mfi_ctrl_rebuild_rate);
119ee7a2acaSSean Bruno
120ee7a2acaSSean Bruno static int
mfi_ctrl_alarm_enable(int ac,char ** av)121ee7a2acaSSean Bruno mfi_ctrl_alarm_enable(int ac, char **av)
122ee7a2acaSSean Bruno {
123ee7a2acaSSean Bruno int error, fd;
124ee7a2acaSSean Bruno struct mfi_ctrl_props ctrl_props;
125ee7a2acaSSean Bruno
126ee7a2acaSSean Bruno if (ac > 2) {
127ee7a2acaSSean Bruno warn("mfi_ctrl_alarm_enable");
128ee7a2acaSSean Bruno return(-1);
129ee7a2acaSSean Bruno }
130ee7a2acaSSean Bruno
1317e0f8b79SDoug Ambrisko fd = mfi_open(mfi_device, O_RDWR);
132ee7a2acaSSean Bruno if (fd < 0) {
133ee7a2acaSSean Bruno error = errno;
134ee7a2acaSSean Bruno warn("mfi_open");
135ee7a2acaSSean Bruno return (error);
136ee7a2acaSSean Bruno }
137ee7a2acaSSean Bruno
138ee7a2acaSSean Bruno error = mfi_ctrl_get_properties(fd, &ctrl_props);
139ee7a2acaSSean Bruno if ( error < 0) {
140ee7a2acaSSean Bruno error = errno;
141ee7a2acaSSean Bruno warn("Failed to get controller properties");
142ee7a2acaSSean Bruno close(fd);
143ee7a2acaSSean Bruno return (error);
144ee7a2acaSSean Bruno }
145ee7a2acaSSean Bruno printf ("controller alarm was : %s\n",
146ee7a2acaSSean Bruno (ctrl_props.alarm_enable ? "enabled" : "disabled"));
147ee7a2acaSSean Bruno
148ee7a2acaSSean Bruno if (ac > 1) {
149ee7a2acaSSean Bruno ctrl_props.alarm_enable = atoi(av[ac - 1]);
150ee7a2acaSSean Bruno error = mfi_ctrl_set_properties(fd, &ctrl_props);
151ee7a2acaSSean Bruno if ( error < 0) {
152ee7a2acaSSean Bruno error = errno;
153ee7a2acaSSean Bruno warn("Failed to set controller properties");
154ee7a2acaSSean Bruno close(fd);
155ee7a2acaSSean Bruno return (error);
156ee7a2acaSSean Bruno }
157ee7a2acaSSean Bruno
158ee7a2acaSSean Bruno error = mfi_ctrl_get_properties(fd, &ctrl_props);
159ee7a2acaSSean Bruno if ( error < 0) {
160ee7a2acaSSean Bruno error = errno;
161ee7a2acaSSean Bruno warn("Failed to get controller properties");
162ee7a2acaSSean Bruno close(fd);
163ee7a2acaSSean Bruno return (error);
164ee7a2acaSSean Bruno }
165ee7a2acaSSean Bruno }
166ee7a2acaSSean Bruno printf ("controller alarm was : %s\n",
167ee7a2acaSSean Bruno (ctrl_props.alarm_enable ? "enabled" : "disabled"));
168ee7a2acaSSean Bruno return (0);
169ee7a2acaSSean Bruno }
170ee7a2acaSSean Bruno
171ee7a2acaSSean Bruno MFI_COMMAND(ctrlprop, alarm, mfi_ctrl_alarm_enable);
172