1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SES_IMPL_H
28 #define	_SES_IMPL_H
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 #include <alloca.h>
35 #include <errno.h>
36 #include <assert.h>
37 #include <dirent.h>
38 #include <dlfcn.h>
39 #include <limits.h>
40 #include <pthread.h>
41 #include <stdarg.h>
42 #include <stddef.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <strings.h>
47 #include <libnvpair.h>
48 #include <unistd.h>
49 #include <sys/mman.h>
50 #include <sys/types.h>
51 #include <sys/sysmacros.h>
52 #include <sys/systeminfo.h>
53 
54 #include <scsi/libscsi.h>
55 #include <scsi/libses_plugin.h>
56 #include <scsi/plugins/ses/framework/ses2_impl.h>
57 
58 #define	LIBSES_ERRMSGLEN	512
59 
60 #define	LIBSES_DEFAULT_PLUGINDIR 	"/usr/lib/scsi/plugins/ses"
61 #define	LIBSES_PLUGIN_FRAMEWORK		"framework"
62 #define	LIBSES_PLUGIN_VENDOR		"vendor"
63 
64 #define	LIBSES_PLUGIN_EXT	".so"
65 
66 struct ses_plugin {
67 	struct ses_plugin *sp_next;	/* next plugin in list */
68 	struct ses_plugin *sp_prev;	/* previous plugin in list */
69 	uint64_t sp_priority;		/* plugin priority */
70 	struct ses_target *sp_target;	/* corresponding target */
71 	void *sp_object;		/* shared object */
72 	void *sp_data;			/* module-specific data */
73 	boolean_t sp_initialized;	/* successfully initialized */
74 	ses_pagedesc_t *sp_pages;	/* pages */
75 	int (*sp_init)(ses_plugin_t *);	/* plugin init */
76 	void (*sp_fini)(ses_plugin_t *); /* plugin fini */
77 	int (*sp_node_parse)(ses_plugin_t *, ses_node_t *); /* parse node */
78 	int (*sp_node_ctl)(ses_plugin_t *, ses_node_t *, const char *,
79 	    nvlist_t *);		/* node control */
80 };
81 
82 struct ses_target {
83 	libscsi_hdl_t *st_scsi_hdl;
84 	libscsi_target_t *st_target;
85 	struct ses_plugin *st_plugin_first;
86 	struct ses_plugin *st_plugin_last;
87 	struct ses_snap *st_snapshots;
88 	boolean_t st_closescsi;
89 	boolean_t st_truncate;
90 	pthread_mutex_t st_lock;
91 };
92 
93 /*
94  * Maximum number of snapshot retries triggered by generation count changes
95  */
96 #define	LIBSES_MAX_GC_RETRIES	10
97 
98 /*
99  * Maximum number of Enclosure Busy retries
100  */
101 #define	LIBSES_MAX_BUSY_RETRIES	3
102 
103 typedef struct ses_snap_page {
104 	ses2_diag_page_t ssp_num;
105 	boolean_t ssp_control;
106 	boolean_t ssp_initialized;
107 	size_t ssp_alloc;
108 	size_t ssp_len;
109 	void *ssp_page;
110 	char *ssp_mmap_base;
111 	size_t ssp_mmap_len;
112 	struct ses_snap_page *ssp_next;
113 	struct ses_snap_page *ssp_unique;
114 } ses_snap_page_t;
115 
116 struct ses_snap {
117 	struct ses_target *ss_target;
118 	uint32_t ss_generation;
119 	hrtime_t ss_time;
120 	struct ses_node *ss_root;
121 	size_t ss_n_elem;
122 	ses_snap_page_t *ss_pages;
123 	size_t ss_n_nodes;
124 	struct ses_node **ss_nodes;
125 	struct ses_snap *ss_next;
126 	struct ses_snap *ss_prev;
127 	uint32_t ss_refcnt;
128 };
129 
130 struct ses_node {
131 	ses_node_type_t sn_type;
132 	uint64_t sn_rootidx;	/* Relative index for enclosure/aggregate */
133 	size_t sn_id;		/* Unique global ID */
134 	uint64_t sn_enc_num;
135 	struct ses_snap *sn_snapshot;
136 	struct ses_node *sn_parent;
137 	struct ses_node *sn_next_sibling;
138 	struct ses_node *sn_prev_sibling;
139 	struct ses_node *sn_first_child;
140 	struct ses_node *sn_last_child;
141 	nvlist_t *sn_props;
142 };
143 
144 extern int ses_fill_snap(ses_snap_t *);
145 extern void ses_node_teardown(ses_node_t *);
146 extern ses_snap_page_t *ses_snap_find_page(ses_snap_t *, ses2_diag_page_t,
147     boolean_t);
148 extern ses_snap_page_t *ses_snap_ctl_page(ses_snap_t *,
149     ses2_diag_page_t, size_t, boolean_t);
150 extern int ses_snap_do_ctl(ses_snap_t *);
151 
152 extern int ses_libscsi_error(libscsi_hdl_t *, const char *, ...);
153 extern int ses_scsi_error(libscsi_action_t *, const char *, ...);
154 
155 extern int ses_plugin_load(ses_target_t *);
156 extern void ses_plugin_unload(ses_target_t *);
157 
158 extern ses_pagedesc_t *ses_get_pagedesc(ses_target_t *, int, ses_pagetype_t);
159 extern int ses_fill_node(ses_node_t *);
160 
161 extern int enc_parse_ed(ses2_ed_impl_t *, nvlist_t *);
162 extern int enc_parse_td(ses2_td_hdr_impl_t *, const char *, nvlist_t *);
163 
164 #ifdef	__cplusplus
165 }
166 #endif
167 
168 #endif	/* _SES_IMPL_H */
169