1257820Ssbruno/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
4257820Ssbruno * Copyright (c) 2013 Yahoo!, Inc.
5257820Ssbruno * All rights reserved.
6257820Ssbruno *
7257820Ssbruno * Redistribution and use in source and binary forms, with or without
8257820Ssbruno * modification, are permitted provided that the following conditions
9257820Ssbruno * are met:
10257820Ssbruno * 1. Redistributions of source code must retain the above copyright
11257820Ssbruno *    notice, this list of conditions and the following disclaimer.
12257820Ssbruno * 2. Redistributions in binary form must reproduce the above copyright
13257820Ssbruno *    notice, this list of conditions and the following disclaimer in the
14257820Ssbruno *    documentation and/or other materials provided with the distribution.
15257820Ssbruno *
16257820Ssbruno * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17257820Ssbruno * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18257820Ssbruno * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19257820Ssbruno * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20257820Ssbruno * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21257820Ssbruno * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22257820Ssbruno * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23257820Ssbruno * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24257820Ssbruno * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25257820Ssbruno * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26257820Ssbruno * SUCH DAMAGE.
27257820Ssbruno *
28257820Ssbruno *
29257820Ssbruno * $FreeBSD: stable/11/usr.sbin/mfiutil/mfi_properties.c 330449 2018-03-05 07:26:05Z eadler $
30257820Ssbruno */
31257820Ssbruno
32257820Ssbruno#include <sys/errno.h>
33257820Ssbruno#include <sys/ioctl.h>
34257820Ssbruno#include <sys/param.h>
35257820Ssbruno#include <sys/sysctl.h>
36257820Ssbruno#include <sys/uio.h>
37257820Ssbruno
38257820Ssbruno#include <err.h>
39257820Ssbruno#include <fcntl.h>
40257820Ssbruno#include <stdio.h>
41257820Ssbruno#include <stdlib.h>
42257820Ssbruno#include <string.h>
43257820Ssbruno#include <unistd.h>
44257820Ssbruno
45257820Ssbruno#include "mfiutil.h"
46257820Ssbruno#include <dev/mfi/mfi_ioctl.h>
47257820Ssbruno
48257820SsbrunoMFI_TABLE(top, ctrlprop);
49257820Ssbruno
50257820Ssbrunostatic int
51257820Ssbrunomfi_ctrl_get_properties(int fd, struct mfi_ctrl_props *info)
52257820Ssbruno{
53257820Ssbruno
54257820Ssbruno	return (mfi_dcmd_command(fd, MFI_DCMD_CTRL_GET_PROPS, info,
55257820Ssbruno	    sizeof(struct mfi_ctrl_props), NULL, 0, NULL));
56257820Ssbruno}
57257820Ssbruno
58257820Ssbrunostatic int
59257820Ssbrunomfi_ctrl_set_properties(int fd, struct mfi_ctrl_props *info)
60257820Ssbruno{
61257820Ssbruno
62257820Ssbruno	return (mfi_dcmd_command(fd, MFI_DCMD_CTRL_SET_PROPS, info,
63257820Ssbruno	    sizeof(struct mfi_ctrl_props), NULL, 0, NULL));
64257820Ssbruno}
65257820Ssbruno
66257820Ssbruno/*
67257820Ssbruno * aquite the controller properties data structure modify the
68257820Ssbruno * rebuild rate if requested and then retun
69257820Ssbruno */
70257820Ssbrunostatic int
71257820Ssbrunomfi_ctrl_rebuild_rate(int ac, char **av)
72257820Ssbruno{
73271882Ssmh	int error, fd;
74257820Ssbruno	struct mfi_ctrl_props ctrl_props;
75257820Ssbruno
76257820Ssbruno	if (ac > 2) {
77257820Ssbruno		warn("mfi_ctrl_set_rebuild_rate");
78257820Ssbruno		return(-1);
79257820Ssbruno	}
80257820Ssbruno
81271882Ssmh	fd = mfi_open(mfi_unit, O_RDWR);
82271882Ssmh	if (fd < 0) {
83271882Ssmh		error = errno;
84271882Ssmh		warn("mfi_open");
85271882Ssmh		return (error);
86271882Ssmh	}
87257820Ssbruno
88257820Ssbruno	error = mfi_ctrl_get_properties(fd, &ctrl_props);
89271882Ssmh	if ( error < 0) {
90271882Ssmh		error = errno;
91271882Ssmh		warn("Failed to get controller properties");
92271882Ssmh		close(fd);
93271882Ssmh		return (error);
94271882Ssmh	}
95257820Ssbruno	/*
96257820Ssbruno	 * User requested a change to the rebuild rate
97257820Ssbruno	 */
98257820Ssbruno	if (ac > 1) {
99257820Ssbruno		ctrl_props.rebuild_rate = atoi(av[ac - 1]);
100257820Ssbruno		error = mfi_ctrl_set_properties(fd, &ctrl_props);
101271882Ssmh		if ( error < 0) {
102271882Ssmh			error = errno;
103271882Ssmh			warn("Failed to set controller properties");
104271882Ssmh			close(fd);
105271882Ssmh			return (error);
106271882Ssmh		}
107257820Ssbruno
108257820Ssbruno		error = mfi_ctrl_get_properties(fd, &ctrl_props);
109271882Ssmh		if ( error < 0) {
110271882Ssmh			error = errno;
111271882Ssmh			warn("Failed to get controller properties");
112271882Ssmh			close(fd);
113271882Ssmh			return (error);
114271882Ssmh		}
115257820Ssbruno	}
116257820Ssbruno	printf ("controller rebuild rate: %%%u \n",
117257820Ssbruno		ctrl_props.rebuild_rate);
118257820Ssbruno	return (0);
119257820Ssbruno}
120257820SsbrunoMFI_COMMAND(ctrlprop, rebuild, mfi_ctrl_rebuild_rate);
121257820Ssbruno
122257820Ssbrunostatic int
123257820Ssbrunomfi_ctrl_alarm_enable(int ac, char **av)
124257820Ssbruno{
125271882Ssmh	int error, fd;
126257820Ssbruno	struct mfi_ctrl_props ctrl_props;
127257820Ssbruno
128257820Ssbruno	if (ac > 2) {
129257820Ssbruno		warn("mfi_ctrl_alarm_enable");
130257820Ssbruno		return(-1);
131257820Ssbruno	}
132257820Ssbruno
133271882Ssmh	fd = mfi_open(mfi_unit, O_RDWR);
134271882Ssmh	if (fd < 0) {
135271882Ssmh		error = errno;
136271882Ssmh		warn("mfi_open");
137271882Ssmh		return (error);
138271882Ssmh	}
139257820Ssbruno
140257820Ssbruno	error = mfi_ctrl_get_properties(fd, &ctrl_props);
141271882Ssmh	if ( error < 0) {
142271882Ssmh		error = errno;
143271882Ssmh		warn("Failed to get controller properties");
144271882Ssmh		close(fd);
145271882Ssmh		return (error);
146271882Ssmh	}
147257820Ssbruno	printf ("controller alarm was : %s\n",
148257820Ssbruno		(ctrl_props.alarm_enable ? "enabled" : "disabled"));
149257820Ssbruno
150257820Ssbruno	if (ac > 1) {
151257820Ssbruno		ctrl_props.alarm_enable = atoi(av[ac - 1]);
152257820Ssbruno		error = mfi_ctrl_set_properties(fd, &ctrl_props);
153271882Ssmh		if ( error < 0) {
154271882Ssmh			error = errno;
155271882Ssmh			warn("Failed to set controller properties");
156271882Ssmh			close(fd);
157271882Ssmh			return (error);
158271882Ssmh		}
159257820Ssbruno
160257820Ssbruno		error = mfi_ctrl_get_properties(fd, &ctrl_props);
161271882Ssmh		if ( error < 0) {
162271882Ssmh			error = errno;
163271882Ssmh			warn("Failed to get controller properties");
164271882Ssmh			close(fd);
165271882Ssmh			return (error);
166271882Ssmh		}
167257820Ssbruno	}
168257820Ssbruno	printf ("controller alarm was : %s\n",
169257820Ssbruno		(ctrl_props.alarm_enable ? "enabled" : "disabled"));
170257820Ssbruno	return (0);
171257820Ssbruno}
172257820Ssbruno
173257820SsbrunoMFI_COMMAND(ctrlprop, alarm, mfi_ctrl_alarm_enable);
174