summaryrefslogtreecommitdiff
path: root/usr/src/lib/scsi/plugins/smp/usmp/common/usmp.c
blob: fa3ea089214bf188abeb3628a1ef4d6fad516ce7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/scsi/scsi_address.h>
#include <sys/scsi/impl/usmp.h>
#include <sys/libdevid.h>

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <limits.h>

#include <scsi/libsmp.h>
#include <scsi/libsmp_plugin.h>

#include <libdevinfo.h>

struct usmp_dev {
	int ud_fd;
	char *ud_dev;
	uint64_t ud_addr;
};

struct di_walk_arg {
	dev_t dev;
	uint64_t addr;
};

static int
di_walk(di_node_t node, di_minor_t minor, void *arg)
{
	struct di_walk_arg *wp = arg;
	char *wwn;

	if (di_minor_spectype(minor) != S_IFCHR)
		return (DI_WALK_CONTINUE);

	if (di_minor_devt(minor) == wp->dev) {
		if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
		    SCSI_ADDR_PROP_TARGET_PORT, &wwn) != 1 &&
		    di_prop_lookup_strings(DDI_DEV_T_ANY, node,
		    "smp-wwn", &wwn) != 1)
			return (DI_WALK_CONTINUE);

		if (scsi_wwnstr_to_wwn(wwn, &wp->addr) != DDI_SUCCESS)
			return (DI_WALK_CONTINUE);

		return (DI_WALK_TERMINATE);
	}

	return (DI_WALK_CONTINUE);
}

static void *
usmp_open(const void *target)
{
	struct usmp_dev *dp;
	const char *target_name = (const char *)target;

	struct stat64 st;
	di_node_t root, smp;
	struct di_walk_arg walk;

	if ((dp = smp_zalloc(sizeof (struct usmp_dev))) == NULL)
		return (NULL);

	if ((dp->ud_dev = smp_strdup(target_name)) == NULL) {
		smp_free(dp);
		return (NULL);
	}

	if ((dp->ud_fd = open(target_name, O_RDONLY)) < 0) {
		(void) smp_error(ESMP_BADTARGET,
		    "failed to open %s for reading: %s",
		    target_name, strerror(errno));
		smp_free(dp->ud_dev);
		smp_free(dp);
		return (NULL);
	}

	if (fstat64(dp->ud_fd, &st) != 0) {
		(void) smp_error(ESMP_BADTARGET,
		    "failed to stat %s: %s", target_name, strerror(errno));
		(void) close(dp->ud_fd);
		smp_free(dp->ud_dev);
		smp_free(dp);
		return (NULL);
	}

	if ((root = di_init("/", DINFOCACHE)) != DI_NODE_NIL) {
		for (smp = di_drv_first_node("smp", root); smp != DI_NODE_NIL;
		    smp = di_drv_next_node(smp)) {
			bzero(&walk, sizeof (walk));
			walk.dev = st.st_rdev;
			(void) di_walk_minor(smp, NULL, 0, &walk, di_walk);
			if (walk.addr != 0) {
				dp->ud_addr = walk.addr;
				break;
			}
		}
		di_fini(root);
	}

	return (dp);
}

static void
usmp_close(void *private)
{
	struct usmp_dev *dp = (struct usmp_dev *)private;

	if (dp == NULL)
		return;

	if (dp->ud_fd > 0)
		(void) close(dp->ud_fd);

	smp_free(dp->ud_dev);
	smp_free(dp);
}

static int
usmp_exec(void *private, smp_action_t *ap)
{
	struct usmp_dev *dp = (struct usmp_dev *)private;
	struct usmp_cmd cmd;
	void *req, *resp;
	size_t reqlen, resplen;

	bzero(&cmd, sizeof (cmd));

	smp_action_get_request_frame(ap, &req, &reqlen);
	smp_action_get_response_frame(ap, &resp, &resplen);

	ASSERT(req != NULL);
	ASSERT(resp != NULL);
	ASSERT(reqlen != 0);
	ASSERT(resplen != 0);

	cmd.usmp_req = req;
	cmd.usmp_reqsize = reqlen;
	cmd.usmp_rsp = resp;
	cmd.usmp_rspsize = resplen;
	cmd.usmp_timeout = (int)smp_action_get_timeout(ap);

	if (ioctl(dp->ud_fd, USMPFUNC, &cmd) < 0) {
		ASSERT(errno != EFAULT);
		switch (errno) {
		case EINVAL:
			return (smp_error(ESMP_BADFUNC, "internal usmp error"));
		case EPERM:
			return (smp_error(ESMP_PERM,
			    "insufficient privileges"));
		case EIO:
			return (smp_error(ESMP_IO, "I/O error"));
		default:
			return (smp_error(ESMP_SYS, "usmp ioctl failed: %s",
			    strerror(errno)));
		}
	}

	/*
	 * There is no way to determine the amount of data actually transferred
	 * so we will just place the upper bound at the allocated size.
	 */
	smp_action_set_response_len(ap, resplen);

	return (0);
}

static void
usmp_target_name(void *private, char *buf, size_t len)
{
	struct usmp_dev *dp = (struct usmp_dev *)private;

	(void) strlcpy(buf, dp->ud_dev, len);
}

static uint64_t
usmp_target_addr(void *private)
{
	struct usmp_dev *dp = (struct usmp_dev *)private;

	return (dp->ud_addr);
}

static const smp_engine_ops_t usmp_ops = {
	.seo_open = usmp_open,
	.seo_close = usmp_close,
	.seo_exec = usmp_exec,
	.seo_target_name = usmp_target_name,
	.seo_target_addr = usmp_target_addr
};

int
_smp_init(smp_engine_t *ep)
{
	smp_engine_config_t config = {
		.sec_name = "usmp",
		.sec_ops = &usmp_ops
	};

	return (smp_engine_register(ep, LIBSMP_ENGINE_VERSION, &config));
}