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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <syslog.h>
#include <libdevice.h>
#include <sys/fibre-channel/fcio.h>
#include "common.h"
static int parse_line(char *line, char *path, char *wwn, char *filename);
static int create_ap_instance(char *ap_id, char *wwn_string,
char *filename, char *line);
static void log_error(char *msg_id, char *input_tmplt, ...);
static char ctoi(char c);
/*
* Simple wrapper for syslog error messages.
* Allows easy addition of syserr output if desired.
*/
static void
log_error(char *msg_id, char *input_tmplt, ...)
{
va_list ap;
char input_merged_msg[200];
char *msg_template = "ID[luxadm.create_fabric_device.%s] %s";
/*
* First %s for msg_id in merged msg.
* Second %s is for input merged_msg
*/
char *merged_msg;
va_start(ap, input_tmplt);
/* insert caller's args */
(void) vsprintf(input_merged_msg, input_tmplt, ap);
va_end(ap);
merged_msg = (char *)malloc(strlen(msg_template) +
strlen(input_merged_msg) +
strlen(msg_id) + 1);
if (merged_msg == NULL) {
syslog(LOG_ERR, "ID[luxadm.create_fabric_device.2317] "
"malloc failure, %s", strerror(errno));
} else {
sprintf(merged_msg, msg_template, msg_id, input_merged_msg);
/* first insert msg_id */
syslog(LOG_ERR, merged_msg, "");
(void) puts(merged_msg); /* also print message */
free(merged_msg);
}
}
/*
* Routines for reading tapestry repository file
*/
#define COMMENT_CHAR '#'
int
read_repos_file(char *repos_filename)
{
int fd;
char *line;
char *tmp_ptr, *mmap_ptr;
char path[MAXPATHLEN];
int ret;
char wwn[FC_WWN_SIZE*2+1];
struct stat stbuf;
unsigned int filesize;
unsigned int bytes_read;
if (repos_filename == NULL || *repos_filename == '\0') {
log_error("2310",
"filename missing for -f option of "
"luxadm -e create_fabric_device");
return (-1);
}
fd = open(repos_filename, O_RDONLY);
if (fd == -1) {
log_error("2311",
"fopen failed: cannot open repository file %s. %d",
repos_filename, strerror(errno));
return (-1);
}
if (fstat(fd, &stbuf) == -1) {
close(fd);
log_error("2312", "stat failed on file %s. %s",
repos_filename, strerror(errno));
return (-1);
}
filesize = stbuf.st_size;
tmp_ptr = mmap_ptr = mmap((caddr_t)0, filesize,
(PROT_READ | PROT_WRITE), MAP_PRIVATE, fd, 0);
if (mmap_ptr == MAP_FAILED) {
log_error("2315", "Failed to mmap file %s. %s",
repos_filename, strerror(errno));
return (-1);
}
bytes_read = 0;
while (bytes_read < filesize) {
line = tmp_ptr;
while (bytes_read < filesize && *tmp_ptr != '\n') {
bytes_read++;
tmp_ptr++;
}
if (*tmp_ptr == '\n') {
*tmp_ptr = '\0';
tmp_ptr++;
bytes_read++;
}
/* If the line is a comment, read another line */
if (*line == COMMENT_CHAR) {
continue;
}
ret = parse_line(line, path, wwn, repos_filename);
if (ret == 0) {
ret = create_ap_instance(path,
wwn, repos_filename, line);
}
}
ret = close(fd);
ret = munmap(mmap_ptr, filesize);
return (ret);
}
/*
* Input is paramater 1 - a line from repository
* Output is other parameters, the path to the attachment point,
* and the port wwn are parsed from the repository
* Format is
* "/devices/pci..../fp@1,0:fc::wwn"
* If controller name is missing, that's okay. Other fields
* must be present
*
* Return 0 on success or -1 on failure; all failures logged to syslog.
*/
#define WWN_DELIM "::"
static int
parse_line(char *line, char *path, char *wwn, char *filename)
{
char *p_path, *p_wwn, *p_delim;
char *line_copy;
line_copy = strdup(line);
if (line_copy == NULL) {
log_error("2317", "malloc failure, %s", strerror(errno));
}
p_path = line_copy;
p_delim = strstr(p_path, WWN_DELIM);
if (p_delim == NULL) {
log_error("2313",
"Invalid line (%s) in file %s.", line, filename);
free(line_copy);
return (-1);
}
*p_delim = '\0'; /* NULL terminate path */
if (strlcpy(path, p_path, MAXPATHLEN) >= MAXPATHLEN) {
log_error("2318",
"Path too long (%s) in file %s.", p_path, filename);
free(line_copy);
return (-1);
}
p_wwn = p_delim + strlen(WWN_DELIM);
/*
* Now look for the blank delimiter before the controller
*
* This is just the case when there may be a controller #
* after the attachment point and WWN. For example -
* /devices/pci@b,2000/pci@2/SUNW,qlc@4/fp@0,0:fc::220000203707f4f1 c4
*/
p_delim = strchr(p_wwn, ' ');
if (p_delim != NULL) {
/* now p_delim points to blank */
*p_delim = '\0'; /* terminate wwn at delim */
} else {
char *p_last_char;
p_last_char = p_wwn+strlen(p_wwn)-1;
if (*p_last_char == '\n') {
*p_last_char = '\0';
}
}
strcpy(wwn, p_wwn);
free(line_copy);
return (0);
}
static char
ctoi(char c)
{
if ((c >= '0') && (c <= '9'))
c -= '0';
else if ((c >= 'A') && (c <= 'F'))
c = c - 'A' + 10;
else if ((c >= 'a') && (c <= 'f'))
c = c - 'a' + 10;
else
c = -1;
return (c);
}
/*
* "string" is Input and "port_wwn" has the output
*
* This function converts a string to WWN.
* For example a string like
* "220000203707F4F1" gets converted to 0x220000203707F4F1 ...
* where
* port_wwn[0] = 0x22,
* port_wwn[1] = 0x00,
* port_wwn[2] = 0x00,
* port_wwn[3] = 0x20,
* port_wwn[4] = 0x37,
* port_wwn[5] = 0x07,
* port_wwn[6] = 0xF4, and
* port_wwn[7] = 0xF1
*/
static int
string_to_wwn(const uchar_t *string, uchar_t *port_wwn)
{
int i;
char c, c1;
uchar_t *wwnp;
wwnp = port_wwn;
for (i = 0; i < WWN_SIZE; i++, wwnp++) {
c = ctoi(*string++);
c1 = ctoi(*string++);
if (c == -1 || c1 == -1)
return (-1);
*wwnp = ((c << 4) + c1);
}
return (0);
}
static int
create_ap_instance(char *ap_id, char *wwn_string,
char *filename, char *line)
{
devctl_hdl_t bus_handle, dev_handle;
devctl_ddef_t ddef_handle;
int ret;
uchar_t wwn_array[FC_WWN_SIZE];
ddef_handle = devctl_ddef_alloc("dummy", 0);
if (ddef_handle == NULL) {
log_error("2314",
"Internal error to process line (%s) in file: %s. %s",
line, filename, strerror(errno));
return (-1);
}
/*
* g_string_to_wwn() has not been used here because it
* prepends 2 NULLs.
*/
if (string_to_wwn((uchar_t *)wwn_string, wwn_array) != 0) {
log_error("2314",
"Internal error to process line (%s) in file: %s. %s",
line, filename, strerror(errno));
devctl_ddef_free(ddef_handle);
return (-1);
}
(void) devctl_ddef_byte_array(ddef_handle,
"port-wwn", FC_WWN_SIZE, wwn_array);
if ((bus_handle = devctl_bus_acquire(ap_id, 0)) == NULL) {
devctl_ddef_free(ddef_handle);
log_error("2314",
"Internal error to process line (%s) in file: %s. %s",
line, filename, strerror(errno));
return (-1);
}
if (ret =
devctl_bus_dev_create(bus_handle, ddef_handle, 0, &dev_handle)) {
devctl_ddef_free(ddef_handle);
devctl_release(bus_handle);
log_error("2316",
"configuration failed for line (%s) in file: %s. %s",
line, filename, strerror(errno));
return (-1);
}
devctl_release(dev_handle);
devctl_ddef_free(ddef_handle);
devctl_release(bus_handle);
return (ret);
}
|