1196212Sscottl/*-
2196212Sscottl * Copyright (c) 2008 Yahoo!, Inc.
3196212Sscottl * All rights reserved.
4196212Sscottl * Written by: John Baldwin <jhb@FreeBSD.org>
5196212Sscottl *
6196212Sscottl * Redistribution and use in source and binary forms, with or without
7196212Sscottl * modification, are permitted provided that the following conditions
8196212Sscottl * are met:
9196212Sscottl * 1. Redistributions of source code must retain the above copyright
10196212Sscottl *    notice, this list of conditions and the following disclaimer.
11196212Sscottl * 2. Redistributions in binary form must reproduce the above copyright
12196212Sscottl *    notice, this list of conditions and the following disclaimer in the
13196212Sscottl *    documentation and/or other materials provided with the distribution.
14196212Sscottl * 3. Neither the name of the author nor the names of any co-contributors
15196212Sscottl *    may be used to endorse or promote products derived from this software
16196212Sscottl *    without specific prior written permission.
17196212Sscottl *
18196212Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19196212Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20196212Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21196212Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22196212Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23196212Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24196212Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25196212Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26196212Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27196212Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28196212Sscottl * SUCH DAMAGE.
29196212Sscottl *
30196212Sscottl * $FreeBSD$
31196212Sscottl */
32196212Sscottl
33196212Sscottl#ifndef __MPTUTIL_H__
34196212Sscottl#define	__MPTUTIL_H__
35196212Sscottl
36196212Sscottl#include <sys/cdefs.h>
37196212Sscottl#include <sys/linker_set.h>
38196212Sscottl
39196212Sscottl#include <dev/mpt/mpilib/mpi_type.h>
40196212Sscottl#include <dev/mpt/mpilib/mpi.h>
41196212Sscottl#include <dev/mpt/mpilib/mpi_cnfg.h>
42196212Sscottl#include <dev/mpt/mpilib/mpi_raid.h>
43196212Sscottl
44196212Sscottl#define	IOC_STATUS_SUCCESS(status)					\
45196212Sscottl	(((status) & MPI_IOCSTATUS_MASK) == MPI_IOCSTATUS_SUCCESS)
46196212Sscottl
47196212Sscottlstruct mpt_query_disk {
48196212Sscottl	char	devname[SPECNAMELEN + 1];
49196212Sscottl};
50196212Sscottl
51196212Sscottlstruct mpt_standalone_disk {
52196212Sscottl	uint64_t maxlba;
53196212Sscottl	char	inqstring[64];
54196212Sscottl	char	devname[SPECNAMELEN + 1];
55196212Sscottl	u_int	bus;
56196212Sscottl	u_int	target;
57196212Sscottl};
58196212Sscottl
59196212Sscottlstruct mpt_drive_list {
60196212Sscottl	int	ndrives;
61196212Sscottl	CONFIG_PAGE_RAID_PHYS_DISK_0 *drives[0];
62196212Sscottl};
63196212Sscottl
64196212Sscottlstruct mptutil_command {
65196212Sscottl	const char *name;
66196212Sscottl	int (*handler)(int ac, char **av);
67196212Sscottl};
68196212Sscottl
69196212Sscottl#define	MPT_DATASET(name)	mptutil_ ## name ## _table
70196212Sscottl
71196212Sscottl#define	MPT_COMMAND(set, name, function)				\
72196212Sscottl	static struct mptutil_command function ## _mptutil_command =	\
73196212Sscottl	{ #name, function };						\
74196212Sscottl	DATA_SET(MPT_DATASET(set), function ## _mptutil_command)
75196212Sscottl
76196212Sscottl#define	MPT_TABLE(set, name)						\
77196212Sscottl	SET_DECLARE(MPT_DATASET(name), struct mptutil_command);		\
78196212Sscottl									\
79196212Sscottl	static int							\
80196212Sscottl	mptutil_ ## name ## _table_handler(int ac, char **av)		\
81196212Sscottl	{								\
82196212Sscottl		return (mpt_table_handler(SET_BEGIN(MPT_DATASET(name)), \
83196212Sscottl		    SET_LIMIT(MPT_DATASET(name)), ac, av));		\
84196212Sscottl	}								\
85196212Sscottl	MPT_COMMAND(set, name, mptutil_ ## name ## _table_handler)
86196212Sscottl
87196212Sscottlextern int mpt_unit;
88196212Sscottl
89196212Sscottl#ifdef DEBUG
90196212Sscottlvoid	hexdump(const void *ptr, int length, const char *hdr, int flags);
91196212Sscottl#define	HD_COLUMN_MASK	0xff
92196212Sscottl#define	HD_DELIM_MASK	0xff00
93196212Sscottl#define	HD_OMIT_COUNT	(1 << 16)
94196212Sscottl#define	HD_OMIT_HEX	(1 << 17)
95196212Sscottl#define	HD_OMIT_CHARS	(1 << 18)
96196212Sscottl#endif
97196212Sscottl
98196212Sscottlint	mpt_table_handler(struct mptutil_command **start,
99196212Sscottl    struct mptutil_command **end, int ac, char **av);
100196212Sscottlint	mpt_read_config_page_header(int fd, U8 PageType, U8 PageNumber,
101196212Sscottl    U32 PageAddress, CONFIG_PAGE_HEADER *header, U16 *IOCStatus);
102196212Sscottlvoid	*mpt_read_config_page(int fd, U8 PageType, U8 PageNumber,
103196212Sscottl    U32 PageAddress, U16 *IOCStatus);
104196212Sscottlvoid	*mpt_read_extended_config_page(int fd, U8 ExtPageType, U8 PageVersion,
105196212Sscottl    U8 PageNumber, U32 PageAddress, U16 *IOCStatus);
106196212Sscottlint	mpt_write_config_page(int fd, void *buf, U16 *IOCStatus);
107196212Sscottlconst char *mpt_ioc_status(U16 IOCStatus);
108196212Sscottlint	mpt_raid_action(int fd, U8 Action, U8 VolumeBus, U8 VolumeID,
109196212Sscottl    U8 PhysDiskNum, U32 ActionDataWord, void *buf, int len,
110196212Sscottl    RAID_VOL0_STATUS *VolumeStatus, U32 *ActionData, int datalen,
111196212Sscottl    U16 *IOCStatus, U16 *ActionStatus, int write);
112196212Sscottlconst char *mpt_raid_status(U16 ActionStatus);
113196212Sscottlint	mpt_open(int unit);
114196212Sscottlconst char *mpt_raid_level(U8 VolumeType);
115196212Sscottlconst char *mpt_volstate(U8 State);
116196212Sscottlconst char *mpt_pdstate(CONFIG_PAGE_RAID_PHYS_DISK_0 *info);
117196212Sscottlconst char *mpt_pd_inq_string(CONFIG_PAGE_RAID_PHYS_DISK_0 *pd_info);
118196212Sscottlstruct mpt_drive_list *mpt_pd_list(int fd);
119196212Sscottlvoid	mpt_free_pd_list(struct mpt_drive_list *list);
120196212Sscottlint	mpt_query_disk(U8 VolumeBus, U8 VolumeID, struct mpt_query_disk *qd);
121196212Sscottlconst char *mpt_volume_name(U8 VolumeBus, U8 VolumeID);
122196212Sscottlint	mpt_fetch_disks(int fd, int *ndisks,
123196212Sscottl    struct mpt_standalone_disk **disksp);
124196212Sscottlint	mpt_lock_volume(U8 VolumeBus, U8 VolumeID);
125196212Sscottlint	mpt_lookup_drive(struct mpt_drive_list *list, const char *drive,
126196212Sscottl    U8 *PhysDiskNum);
127196212Sscottlint	mpt_lookup_volume(int fd, const char *name, U8 *VolumeBus,
128196212Sscottl    U8 *VolumeID);
129196212Sscottlint	mpt_rescan_bus(int bus, int id);
130196212Sscottl
131196212Sscottlstatic __inline void *
132196212Sscottlmpt_read_man_page(int fd, U8 PageNumber, U16 *IOCStatus)
133196212Sscottl{
134196212Sscottl
135196212Sscottl	return (mpt_read_config_page(fd, MPI_CONFIG_PAGETYPE_MANUFACTURING,
136196212Sscottl	    PageNumber, 0, IOCStatus));
137196212Sscottl}
138196212Sscottl
139196212Sscottlstatic __inline void *
140196212Sscottlmpt_read_ioc_page(int fd, U8 PageNumber, U16 *IOCStatus)
141196212Sscottl{
142196212Sscottl
143196212Sscottl	return (mpt_read_config_page(fd, MPI_CONFIG_PAGETYPE_IOC, PageNumber,
144196212Sscottl	    0, IOCStatus));
145196212Sscottl}
146196212Sscottl
147196212Sscottlstatic __inline U32
148196212Sscottlmpt_vol_pageaddr(U8 VolumeBus, U8 VolumeID)
149196212Sscottl{
150196212Sscottl
151196212Sscottl	return (VolumeBus << 8 | VolumeID);
152196212Sscottl}
153196212Sscottl
154196212Sscottlstatic __inline CONFIG_PAGE_RAID_VOL_0 *
155196212Sscottlmpt_vol_info(int fd, U8 VolumeBus, U8 VolumeID, U16 *IOCStatus)
156196212Sscottl{
157196212Sscottl
158196212Sscottl	return (mpt_read_config_page(fd, MPI_CONFIG_PAGETYPE_RAID_VOLUME, 0,
159196212Sscottl	    mpt_vol_pageaddr(VolumeBus, VolumeID), IOCStatus));
160196212Sscottl}
161196212Sscottl
162196212Sscottlstatic __inline CONFIG_PAGE_RAID_VOL_1 *
163196212Sscottlmpt_vol_names(int fd, U8 VolumeBus, U8 VolumeID, U16 *IOCStatus)
164196212Sscottl{
165196212Sscottl
166196212Sscottl	return (mpt_read_config_page(fd, MPI_CONFIG_PAGETYPE_RAID_VOLUME, 1,
167196212Sscottl	    mpt_vol_pageaddr(VolumeBus, VolumeID), IOCStatus));
168196212Sscottl}
169196212Sscottl
170196212Sscottlstatic __inline CONFIG_PAGE_RAID_PHYS_DISK_0 *
171196212Sscottlmpt_pd_info(int fd, U8 PhysDiskNum, U16 *IOCStatus)
172196212Sscottl{
173196212Sscottl
174196212Sscottl	return (mpt_read_config_page(fd, MPI_CONFIG_PAGETYPE_RAID_PHYSDISK, 0,
175196212Sscottl	    PhysDiskNum, IOCStatus));
176196212Sscottl}
177196212Sscottl
178196212Sscottl#endif /* !__MPTUTIL_H__ */
179