1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2008 Yahoo!, Inc.
5 * All rights reserved.
6 * Written by: John Baldwin <jhb@FreeBSD.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $FreeBSD$
33 */
34
35#ifndef __MPTUTIL_H__
36#define	__MPTUTIL_H__
37
38#include <sys/cdefs.h>
39#include <sys/linker_set.h>
40
41#include <dev/mpt/mpilib/mpi_type.h>
42#include <dev/mpt/mpilib/mpi.h>
43#include <dev/mpt/mpilib/mpi_cnfg.h>
44#include <dev/mpt/mpilib/mpi_raid.h>
45
46#define	IOC_STATUS_SUCCESS(status)					\
47	(((status) & MPI_IOCSTATUS_MASK) == MPI_IOCSTATUS_SUCCESS)
48
49struct mpt_query_disk {
50	char	devname[SPECNAMELEN + 1];
51};
52
53struct mpt_standalone_disk {
54	uint64_t maxlba;
55	char	inqstring[64];
56	char	devname[SPECNAMELEN + 1];
57	u_int	bus;
58	u_int	target;
59};
60
61struct mpt_drive_list {
62	int	ndrives;
63	CONFIG_PAGE_RAID_PHYS_DISK_0 *drives[0];
64};
65
66struct mptutil_command {
67	const char *name;
68	int (*handler)(int ac, char **av);
69};
70
71#define	MPT_DATASET(name)	mptutil_ ## name ## _table
72
73#define	MPT_COMMAND(set, name, function)				\
74	static struct mptutil_command function ## _mptutil_command =	\
75	{ #name, function };						\
76	DATA_SET(MPT_DATASET(set), function ## _mptutil_command)
77
78#define	MPT_TABLE(set, name)						\
79	SET_DECLARE(MPT_DATASET(name), struct mptutil_command);		\
80									\
81	static int							\
82	mptutil_ ## name ## _table_handler(int ac, char **av)		\
83	{								\
84		return (mpt_table_handler(SET_BEGIN(MPT_DATASET(name)), \
85		    SET_LIMIT(MPT_DATASET(name)), ac, av));		\
86	}								\
87	MPT_COMMAND(set, name, mptutil_ ## name ## _table_handler)
88
89extern int mpt_unit;
90
91#ifdef DEBUG
92void	hexdump(const void *ptr, int length, const char *hdr, int flags);
93#define	HD_COLUMN_MASK	0xff
94#define	HD_DELIM_MASK	0xff00
95#define	HD_OMIT_COUNT	(1 << 16)
96#define	HD_OMIT_HEX	(1 << 17)
97#define	HD_OMIT_CHARS	(1 << 18)
98#endif
99
100int	mpt_table_handler(struct mptutil_command **start,
101    struct mptutil_command **end, int ac, char **av);
102int	mpt_read_config_page_header(int fd, U8 PageType, U8 PageNumber,
103    U32 PageAddress, CONFIG_PAGE_HEADER *header, U16 *IOCStatus);
104void	*mpt_read_config_page(int fd, U8 PageType, U8 PageNumber,
105    U32 PageAddress, U16 *IOCStatus);
106void	*mpt_read_extended_config_page(int fd, U8 ExtPageType, U8 PageVersion,
107    U8 PageNumber, U32 PageAddress, U16 *IOCStatus);
108int	mpt_write_config_page(int fd, void *buf, U16 *IOCStatus);
109const char *mpt_ioc_status(U16 IOCStatus);
110int	mpt_raid_action(int fd, U8 Action, U8 VolumeBus, U8 VolumeID,
111    U8 PhysDiskNum, U32 ActionDataWord, void *buf, int len,
112    RAID_VOL0_STATUS *VolumeStatus, U32 *ActionData, int datalen,
113    U16 *IOCStatus, U16 *ActionStatus, int write);
114const char *mpt_raid_status(U16 ActionStatus);
115int	mpt_open(int unit);
116const char *mpt_raid_level(U8 VolumeType);
117const char *mpt_volstate(U8 State);
118const char *mpt_pdstate(CONFIG_PAGE_RAID_PHYS_DISK_0 *info);
119const char *mpt_pd_inq_string(CONFIG_PAGE_RAID_PHYS_DISK_0 *pd_info);
120struct mpt_drive_list *mpt_pd_list(int fd);
121void	mpt_free_pd_list(struct mpt_drive_list *list);
122int	mpt_query_disk(U8 VolumeBus, U8 VolumeID, struct mpt_query_disk *qd);
123const char *mpt_volume_name(U8 VolumeBus, U8 VolumeID);
124int	mpt_fetch_disks(int fd, int *ndisks,
125    struct mpt_standalone_disk **disksp);
126int	mpt_lock_volume(U8 VolumeBus, U8 VolumeID);
127int	mpt_lookup_drive(struct mpt_drive_list *list, const char *drive,
128    U8 *PhysDiskNum);
129int	mpt_lookup_volume(int fd, const char *name, U8 *VolumeBus,
130    U8 *VolumeID);
131int	mpt_rescan_bus(int bus, int id);
132
133static __inline void *
134mpt_read_man_page(int fd, U8 PageNumber, U16 *IOCStatus)
135{
136
137	return (mpt_read_config_page(fd, MPI_CONFIG_PAGETYPE_MANUFACTURING,
138	    PageNumber, 0, IOCStatus));
139}
140
141static __inline void *
142mpt_read_ioc_page(int fd, U8 PageNumber, U16 *IOCStatus)
143{
144
145	return (mpt_read_config_page(fd, MPI_CONFIG_PAGETYPE_IOC, PageNumber,
146	    0, IOCStatus));
147}
148
149static __inline U32
150mpt_vol_pageaddr(U8 VolumeBus, U8 VolumeID)
151{
152
153	return (VolumeBus << 8 | VolumeID);
154}
155
156static __inline CONFIG_PAGE_RAID_VOL_0 *
157mpt_vol_info(int fd, U8 VolumeBus, U8 VolumeID, U16 *IOCStatus)
158{
159
160	return (mpt_read_config_page(fd, MPI_CONFIG_PAGETYPE_RAID_VOLUME, 0,
161	    mpt_vol_pageaddr(VolumeBus, VolumeID), IOCStatus));
162}
163
164static __inline CONFIG_PAGE_RAID_VOL_1 *
165mpt_vol_names(int fd, U8 VolumeBus, U8 VolumeID, U16 *IOCStatus)
166{
167
168	return (mpt_read_config_page(fd, MPI_CONFIG_PAGETYPE_RAID_VOLUME, 1,
169	    mpt_vol_pageaddr(VolumeBus, VolumeID), IOCStatus));
170}
171
172static __inline CONFIG_PAGE_RAID_PHYS_DISK_0 *
173mpt_pd_info(int fd, U8 PhysDiskNum, U16 *IOCStatus)
174{
175
176	return (mpt_read_config_page(fd, MPI_CONFIG_PAGETYPE_RAID_PHYSDISK, 0,
177	    PhysDiskNum, IOCStatus));
178}
179
180#endif /* !__MPTUTIL_H__ */
181