1257820Ssbruno/*-
2257820Ssbruno * Copyright (c) 2013 Yahoo!, Inc.
3257820Ssbruno * All rights reserved.
4257820Ssbruno *
5257820Ssbruno * Redistribution and use in source and binary forms, with or without
6257820Ssbruno * modification, are permitted provided that the following conditions
7257820Ssbruno * are met:
8257820Ssbruno * 1. Redistributions of source code must retain the above copyright
9257820Ssbruno *    notice, this list of conditions and the following disclaimer.
10257820Ssbruno * 2. Redistributions in binary form must reproduce the above copyright
11257820Ssbruno *    notice, this list of conditions and the following disclaimer in the
12257820Ssbruno *    documentation and/or other materials provided with the distribution.
13257820Ssbruno *
14257820Ssbruno * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15257820Ssbruno * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16257820Ssbruno * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17257820Ssbruno * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18257820Ssbruno * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19257820Ssbruno * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20257820Ssbruno * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21257820Ssbruno * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22257820Ssbruno * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23257820Ssbruno * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24257820Ssbruno * SUCH DAMAGE.
25257820Ssbruno *
26257820Ssbruno *
27257820Ssbruno * $FreeBSD: releng/11.0/usr.sbin/mfiutil/mfi_properties.c 271882 2014-09-19 23:38:44Z smh $
28257820Ssbruno */
29257820Ssbruno
30257820Ssbruno#include <sys/errno.h>
31257820Ssbruno#include <sys/ioctl.h>
32257820Ssbruno#include <sys/param.h>
33257820Ssbruno#include <sys/sysctl.h>
34257820Ssbruno#include <sys/uio.h>
35257820Ssbruno
36257820Ssbruno#include <err.h>
37257820Ssbruno#include <fcntl.h>
38257820Ssbruno#include <stdio.h>
39257820Ssbruno#include <stdlib.h>
40257820Ssbruno#include <string.h>
41257820Ssbruno#include <unistd.h>
42257820Ssbruno
43257820Ssbruno#include "mfiutil.h"
44257820Ssbruno#include <dev/mfi/mfi_ioctl.h>
45257820Ssbruno
46257820SsbrunoMFI_TABLE(top, ctrlprop);
47257820Ssbruno
48257820Ssbrunostatic int
49257820Ssbrunomfi_ctrl_get_properties(int fd, struct mfi_ctrl_props *info)
50257820Ssbruno{
51257820Ssbruno
52257820Ssbruno	return (mfi_dcmd_command(fd, MFI_DCMD_CTRL_GET_PROPS, info,
53257820Ssbruno	    sizeof(struct mfi_ctrl_props), NULL, 0, NULL));
54257820Ssbruno}
55257820Ssbruno
56257820Ssbrunostatic int
57257820Ssbrunomfi_ctrl_set_properties(int fd, struct mfi_ctrl_props *info)
58257820Ssbruno{
59257820Ssbruno
60257820Ssbruno	return (mfi_dcmd_command(fd, MFI_DCMD_CTRL_SET_PROPS, info,
61257820Ssbruno	    sizeof(struct mfi_ctrl_props), NULL, 0, NULL));
62257820Ssbruno}
63257820Ssbruno
64257820Ssbruno/*
65257820Ssbruno * aquite the controller properties data structure modify the
66257820Ssbruno * rebuild rate if requested and then retun
67257820Ssbruno */
68257820Ssbrunostatic int
69257820Ssbrunomfi_ctrl_rebuild_rate(int ac, char **av)
70257820Ssbruno{
71271882Ssmh	int error, fd;
72257820Ssbruno	struct mfi_ctrl_props ctrl_props;
73257820Ssbruno
74257820Ssbruno	if (ac > 2) {
75257820Ssbruno		warn("mfi_ctrl_set_rebuild_rate");
76257820Ssbruno		return(-1);
77257820Ssbruno	}
78257820Ssbruno
79271882Ssmh	fd = mfi_open(mfi_unit, O_RDWR);
80271882Ssmh	if (fd < 0) {
81271882Ssmh		error = errno;
82271882Ssmh		warn("mfi_open");
83271882Ssmh		return (error);
84271882Ssmh	}
85257820Ssbruno
86257820Ssbruno	error = mfi_ctrl_get_properties(fd, &ctrl_props);
87271882Ssmh	if ( error < 0) {
88271882Ssmh		error = errno;
89271882Ssmh		warn("Failed to get controller properties");
90271882Ssmh		close(fd);
91271882Ssmh		return (error);
92271882Ssmh	}
93257820Ssbruno	/*
94257820Ssbruno	 * User requested a change to the rebuild rate
95257820Ssbruno	 */
96257820Ssbruno	if (ac > 1) {
97257820Ssbruno		ctrl_props.rebuild_rate = atoi(av[ac - 1]);
98257820Ssbruno		error = mfi_ctrl_set_properties(fd, &ctrl_props);
99271882Ssmh		if ( error < 0) {
100271882Ssmh			error = errno;
101271882Ssmh			warn("Failed to set controller properties");
102271882Ssmh			close(fd);
103271882Ssmh			return (error);
104271882Ssmh		}
105257820Ssbruno
106257820Ssbruno		error = mfi_ctrl_get_properties(fd, &ctrl_props);
107271882Ssmh		if ( error < 0) {
108271882Ssmh			error = errno;
109271882Ssmh			warn("Failed to get controller properties");
110271882Ssmh			close(fd);
111271882Ssmh			return (error);
112271882Ssmh		}
113257820Ssbruno	}
114257820Ssbruno	printf ("controller rebuild rate: %%%u \n",
115257820Ssbruno		ctrl_props.rebuild_rate);
116257820Ssbruno	return (0);
117257820Ssbruno}
118257820SsbrunoMFI_COMMAND(ctrlprop, rebuild, mfi_ctrl_rebuild_rate);
119257820Ssbruno
120257820Ssbrunostatic int
121257820Ssbrunomfi_ctrl_alarm_enable(int ac, char **av)
122257820Ssbruno{
123271882Ssmh	int error, fd;
124257820Ssbruno	struct mfi_ctrl_props ctrl_props;
125257820Ssbruno
126257820Ssbruno	if (ac > 2) {
127257820Ssbruno		warn("mfi_ctrl_alarm_enable");
128257820Ssbruno		return(-1);
129257820Ssbruno	}
130257820Ssbruno
131271882Ssmh	fd = mfi_open(mfi_unit, O_RDWR);
132271882Ssmh	if (fd < 0) {
133271882Ssmh		error = errno;
134271882Ssmh		warn("mfi_open");
135271882Ssmh		return (error);
136271882Ssmh	}
137257820Ssbruno
138257820Ssbruno	error = mfi_ctrl_get_properties(fd, &ctrl_props);
139271882Ssmh	if ( error < 0) {
140271882Ssmh		error = errno;
141271882Ssmh		warn("Failed to get controller properties");
142271882Ssmh		close(fd);
143271882Ssmh		return (error);
144271882Ssmh	}
145257820Ssbruno	printf ("controller alarm was : %s\n",
146257820Ssbruno		(ctrl_props.alarm_enable ? "enabled" : "disabled"));
147257820Ssbruno
148257820Ssbruno	if (ac > 1) {
149257820Ssbruno		ctrl_props.alarm_enable = atoi(av[ac - 1]);
150257820Ssbruno		error = mfi_ctrl_set_properties(fd, &ctrl_props);
151271882Ssmh		if ( error < 0) {
152271882Ssmh			error = errno;
153271882Ssmh			warn("Failed to set controller properties");
154271882Ssmh			close(fd);
155271882Ssmh			return (error);
156271882Ssmh		}
157257820Ssbruno
158257820Ssbruno		error = mfi_ctrl_get_properties(fd, &ctrl_props);
159271882Ssmh		if ( error < 0) {
160271882Ssmh			error = errno;
161271882Ssmh			warn("Failed to get controller properties");
162271882Ssmh			close(fd);
163271882Ssmh			return (error);
164271882Ssmh		}
165257820Ssbruno	}
166257820Ssbruno	printf ("controller alarm was : %s\n",
167257820Ssbruno		(ctrl_props.alarm_enable ? "enabled" : "disabled"));
168257820Ssbruno	return (0);
169257820Ssbruno}
170257820Ssbruno
171257820SsbrunoMFI_COMMAND(ctrlprop, alarm, mfi_ctrl_alarm_enable);
172