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