xref: /freebsd/usr.sbin/mpsutil/mpsutil.h (revision 4543ef516683042d46f3bd3bb8a4f3f746e00499)
1 /*-
2  * Copyright (c) 2008 Yahoo!, Inc.
3  * All rights reserved.
4  * Written by: John Baldwin <jhb@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  * 3. Neither the name of the author nor the names of any co-contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #ifndef __MPSUTIL_H__
32 #define	__MPSUTIL_H__
33 
34 #include <sys/cdefs.h>
35 #include <sys/linker_set.h>
36 #include <stdbool.h>
37 
38 #include <dev/mps/mpi/mpi2_type.h>
39 #include <dev/mps/mpi/mpi2.h>
40 #include <dev/mps/mpi/mpi2_cnfg.h>
41 #include <dev/mps/mpi/mpi2_raid.h>
42 #include <dev/mps/mpi/mpi2_ioc.h>
43 #include <dev/mps/mpi/mpi2_init.h>
44 #include <dev/mps/mpi/mpi2_tool.h>
45 
46 #define MPSUTIL_VERSION	"1.0.0"
47 
48 #define	IOC_STATUS_SUCCESS(status)					\
49 	(((status) & MPI2_IOCSTATUS_MASK) == MPI2_IOCSTATUS_SUCCESS)
50 
51 struct mpsutil_command {
52 	const char *name;
53 	int (*handler)(int ac, char **av);
54 };
55 struct mpsutil_usage {
56 	const char *set;
57 	const char *name;
58 	void (*handler)(const char **, const char**);
59 };
60 
61 #define	MPS_DATASET(name)	mpsutil_ ## name ## _table
62 
63 #define	MPS_COMMAND(set, name, function, args, desc)			\
64 	static struct mpsutil_command function ## _mpsutil_command =	\
65 	{ #name, function };						\
66 	DATA_SET(MPS_DATASET(set), function ## _mpsutil_command);	\
67 	static void							\
68 	function ## _usage(const char **a3, const char **a4)		\
69 	{								\
70 		*a3 = args;						\
71 		*a4 = desc;						\
72 		return;							\
73 	};								\
74 	static struct mpsutil_usage function ## _mpsutil_usage =	\
75 	{ #set, #name, function ## _usage };				\
76 	DATA_SET(MPS_DATASET(usage), function ## _mpsutil_usage);
77 
78 #define	_MPS_COMMAND(set, name, function)				\
79 	static struct mpsutil_command function ## _mpsutil_command =	\
80 	{ #name, function };						\
81 	DATA_SET(MPS_DATASET(set), function ## _mpsutil_command);
82 
83 #define	MPS_TABLE(set, name)						\
84 	SET_DECLARE(MPS_DATASET(name), struct mpsutil_command);		\
85 									\
86 	static int							\
87 	mpsutil_ ## name ## _table_handler(int ac, char **av)		\
88 	{								\
89 		return (mps_table_handler(SET_BEGIN(MPS_DATASET(name)), \
90 		    SET_LIMIT(MPS_DATASET(name)), ac, av));		\
91 	}								\
92 	_MPS_COMMAND(set, name, mpsutil_ ## name ## _table_handler)
93 
94 extern int mps_unit;
95 extern int is_mps;
96 #define MPS_MAX_UNIT 10
97 
98 void	hexdump(const void *ptr, int length, const char *hdr, int flags);
99 #define	HD_COLUMN_MASK	0xff
100 #define	HD_DELIM_MASK	0xff00
101 #define	HD_OMIT_COUNT	(1 << 16)
102 #define	HD_OMIT_HEX	(1 << 17)
103 #define	HD_OMIT_CHARS	(1 << 18)
104 #define HD_REVERSED	(1 << 19)
105 int	mps_parse_flags(uintmax_t, const char *, char *, int);
106 
107 int	mps_open(int unit);
108 int	mps_table_handler(struct mpsutil_command **start,
109     struct mpsutil_command **end, int ac, char **av);
110 int	mps_user_command(int fd, void *req, uint32_t req_len, void *reply,
111 	uint32_t reply_len, void *buffer, int len, uint32_t flags);
112 int	mps_pass_command(int fd, void *req, uint32_t req_len, void *reply,
113 	uint32_t reply_len, void *data_in, uint32_t datain_len, void *data_out,
114 	uint32_t dataout_len, uint32_t timeout);
115 int	mps_read_config_page_header(int fd, U8 PageType, U8 PageNumber,
116     U32 PageAddress, MPI2_CONFIG_PAGE_HEADER *header, U16 *IOCStatus);
117 int	mps_read_ext_config_page_header(int fd, U8 ExtPageType, U8 PageNumber,
118     U32 PageAddress, MPI2_CONFIG_PAGE_HEADER *header,
119     U16 *ExtPageLen, U16 *IOCStatus);
120 void	*mps_read_config_page(int fd, U8 PageType, U8 PageNumber,
121     U32 PageAddress, U16 *IOCStatus);
122 void	*mps_read_extended_config_page(int fd, U8 ExtPageType, U8 PageVersion,
123     U8 PageNumber, U32 PageAddress, U16 *IOCStatus);
124 int	mps_map_btdh(int fd, uint16_t *devhandle, uint16_t *bus,
125     uint16_t *target);
126 const char *mps_ioc_status(U16 IOCStatus);
127 int	mps_firmware_send(int fd, unsigned char *buf, uint32_t len, bool bios);
128 int	mps_firmware_get(int fd, unsigned char **buf, bool bios);
129 int	mps_set_slot_status(int fd, U16 handle, U16 slot, U32 status);
130 
131 static __inline void *
132 mps_read_man_page(int fd, U8 PageNumber, U16 *IOCStatus)
133 {
134 
135 	return (mps_read_config_page(fd, MPI2_CONFIG_PAGETYPE_MANUFACTURING,
136 	    PageNumber, 0, IOCStatus));
137 }
138 
139 static __inline void *
140 mps_read_ioc_page(int fd, U8 PageNumber, U16 *IOCStatus)
141 {
142 
143 	return (mps_read_config_page(fd, MPI2_CONFIG_PAGETYPE_IOC, PageNumber,
144 	    0, IOCStatus));
145 }
146 
147 static __inline uint64_t
148 mps_to_u64(U64 *data)
149 {
150 
151 	return (((uint64_t)(data->High) << 32 ) | data->Low);
152 }
153 
154 MPI2_IOC_FACTS_REPLY * mps_get_iocfacts(int fd);
155 
156 #endif /* !__MPSUTIL_H__ */
157