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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/*
* 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 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <alloca.h>
#include <sys/stat.h>
#include <malloc.h>
#include <fcntl.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
#include <sys/mdesc.h>
#include <sys/mdesc_impl.h>
#include <libdevinfo.h>
#include "ldma.h"
#include "mdesc_mutable.h"
static int get_devinfo(uint8_t **mdpp, size_t *size);
static boolean_t is_root_complex(di_prom_handle_t ph, di_node_t di);
static md_node_t *link_device_node(mmd_t *mdp,
di_prom_handle_t ph, di_node_t di, md_node_t *node, char *path);
static int create_children(mmd_t *mdp,
di_prom_handle_t ph, md_node_t *node, di_node_t parent);
static int create_peers(mmd_t *mdp,
di_prom_handle_t ph, md_node_t *node, di_node_t dev);
static int device_tree_to_md(mmd_t *mdp, md_node_t *top);
#define PCIEX "pciex"
#define LDMA_MODULE LDMA_NAME_DIO
/* System Info version supported (only version 1.0) */
static ds_ver_t ldma_dio_vers[] = { {1, 0} };
#define LDMA_DIO_NVERS (sizeof (ldma_dio_vers) / sizeof (ds_ver_t))
#define LDMA_DIO_NHANDLERS (sizeof (ldma_dio_handlers) / \
sizeof (ldma_msg_handler_t))
static ldm_msg_func_t ldma_dio_pcidev_info_handler;
static ldma_msg_handler_t ldma_dio_handlers[] = {
{MSGDIO_PCIDEV_INFO, LDMA_MSGFLG_ACCESS_CONTROL,
ldma_dio_pcidev_info_handler },
};
ldma_agent_info_t ldma_dio_info = {
LDMA_NAME_DIO,
ldma_dio_vers, LDMA_DIO_NVERS,
ldma_dio_handlers, LDMA_DIO_NHANDLERS
};
/* ARGSUSED */
static ldma_request_status_t
ldma_dio_pcidev_info_handler(ds_ver_t *ver, ldma_message_header_t *request,
size_t request_dlen, ldma_message_header_t **replyp, size_t *reply_dlenp)
{
ldma_message_header_t *reply;
char *data;
uint8_t *md_bufp = NULL;
size_t md_size;
int rv;
LDMA_DBG("%s: PCI device info request", __func__);
rv = get_devinfo(&md_bufp, &md_size);
if (rv != 0) {
LDMA_ERR("Failed to generate devinfo MD");
return (LDMA_REQ_FAILED);
}
reply = ldma_alloc_result_msg(request, md_size);
if (reply == NULL) {
LDMA_ERR("Memory allocation failure");
free(md_bufp);
return (LDMA_REQ_FAILED);
}
reply->msg_info = md_size;
data = LDMA_HDR2DATA(reply);
(void) memcpy(data, md_bufp, md_size);
*replyp = reply;
*reply_dlenp = md_size;
free(md_bufp);
LDMA_DBG("%s: sending PCI device info", __func__);
return (LDMA_REQ_COMPLETED);
}
static boolean_t
is_root_complex(di_prom_handle_t ph, di_node_t di)
{
int len;
char *type;
len = di_prom_prop_lookup_strings(ph, di, "device_type", &type);
if ((len == 0) || (type == NULL))
return (B_FALSE);
if (strcmp(type, PCIEX) != 0)
return (B_FALSE);
/*
* A root complex node is directly under the root node. So, if
* 'di' is not the root node, and its parent has no parent,
* then 'di' represents a root complex node.
*/
return ((di_parent_node(di) != DI_NODE_NIL) &&
(di_parent_node(di_parent_node(di)) == DI_NODE_NIL));
}
/*
* String properties in the prom can contain multiple null-terminated
* strings which are concatenated together. We must represent them in
* an MD as a data property. This function retrieves such a property
* and adds it to the MD. If the 'alt_name' PROM property exists then
* the MD property is created with the value of the PROM 'alt_name'
* property, otherwise it is created with the value of the PROM 'name'
* property.
*/
static int
add_prom_string_prop(di_prom_handle_t ph,
mmd_t *mdp, md_node_t *np, di_node_t di, char *name, char *alt_name)
{
int count;
char *pp_data = NULL;
char *str;
int rv = 0;
if (alt_name != NULL) {
count = di_prom_prop_lookup_strings(ph, di, alt_name, &pp_data);
}
if (pp_data == NULL) {
count = di_prom_prop_lookup_strings(ph, di, name, &pp_data);
}
if (count > 0 && pp_data != NULL) {
for (str = pp_data; count > 0; str += strlen(str) + 1)
count--;
rv = md_add_data_property(mdp,
np, name, str - pp_data, (uint8_t *)pp_data);
}
return (rv);
}
/*
* Add an int property 'name' to an MD from an existing PROM property. If
* the 'alt_name' PROM property exists then the MD property is created with
* the value of the PROM 'alt_name' property, otherwise it is created with
* the value of the PROM 'name' property.
*/
static int
add_prom_int_prop(di_prom_handle_t ph,
mmd_t *mdp, md_node_t *np, di_node_t di, char *name, char *alt_name)
{
int count;
int rv = 0;
int *pp_data = NULL;
if (alt_name != NULL) {
count = di_prom_prop_lookup_ints(ph, di, alt_name, &pp_data);
}
if (pp_data == NULL) {
count = di_prom_prop_lookup_ints(ph, di, name, &pp_data);
}
/*
* Note: We know that the properties of interest contain a
* a single int.
*/
if (count > 0 && pp_data != NULL) {
ASSERT(count == 1);
rv = md_add_value_property(mdp, np, name, *pp_data);
}
return (rv);
}
static md_node_t *
link_device_node(mmd_t *mdp,
di_prom_handle_t ph, di_node_t di, md_node_t *node, char *path)
{
md_node_t *np;
np = md_link_new_node(mdp, "iodevice", node, "fwd", "back");
if (np == NULL)
return (NULL);
/* Add the properties from the devinfo node. */
if (md_add_string_property(mdp, np, "dev_path", path) != 0)
goto fail;
/* Add the required properties for this node. */
if (add_prom_string_prop(ph, mdp, np, di, "device_type", NULL) != 0)
goto fail;
if (add_prom_string_prop(ph, mdp, np, di, "compatible", NULL) != 0)
goto fail;
if (add_prom_int_prop(ph,
mdp, np, di, "device-id", "real-device-id") != 0)
goto fail;
if (add_prom_int_prop(ph,
mdp, np, di, "vendor-id", "real-vendor-id") != 0)
goto fail;
if (add_prom_int_prop(ph,
mdp, np, di, "class-code", "real-class-code") != 0)
goto fail;
return (np);
fail:
md_free_node(mdp, np);
return (NULL);
}
static int
create_children(mmd_t *mdp,
di_prom_handle_t ph, md_node_t *md_parent, di_node_t di_parent)
{
md_node_t *md_node;
md_node_t *md_child;
di_node_t di_child;
char *path;
int rv;
path = di_devfs_path(di_parent);
if (path == NULL)
return (EIO);
md_node = link_device_node(mdp, ph, di_parent, md_parent, path);
di_devfs_path_free(path);
if (md_node == NULL) {
return (ENOMEM);
}
while ((di_child = di_child_node(di_parent)) != DI_NODE_NIL) {
path = di_devfs_path(di_child);
if (path != NULL) {
md_child = link_device_node(mdp,
ph, di_child, md_node, path);
di_devfs_path_free(path);
if (md_child == NULL) {
return (ENOMEM);
}
}
rv = create_peers(mdp, ph, md_node, di_child);
if (rv != 0)
return (rv);
md_node = md_child;
di_parent = di_child;
}
return (0);
}
static int
create_peers(mmd_t *mdp, di_prom_handle_t ph, md_node_t *node, di_node_t dev)
{
di_node_t di_peer;
int rv;
while ((di_peer = di_sibling_node(dev)) != DI_NODE_NIL) {
rv = create_children(mdp, ph, node, di_peer);
if (rv != 0)
return (rv);
dev = di_peer;
}
return (0);
}
static int
device_tree_to_md(mmd_t *mdp, md_node_t *top)
{
di_node_t node;
di_node_t root;
di_prom_handle_t ph;
int rv = 0;
root = di_init("/", DINFOSUBTREE | DINFOPROP);
if (root == DI_NODE_NIL) {
LDMA_ERR("di_init cannot find device tree root node.");
return (errno);
}
ph = di_prom_init();
if (ph == DI_PROM_HANDLE_NIL) {
LDMA_ERR("di_prom_init failed.");
di_fini(root);
return (errno);
}
node = di_child_node(root);
while (node != NULL) {
if (is_root_complex(ph, node)) {
rv = create_children(mdp, ph, top, node);
if (rv != 0)
break;
}
node = di_sibling_node(node);
}
di_prom_fini(ph);
di_fini(root);
return (rv);
}
static int
get_devinfo(uint8_t **mdpp, size_t *size)
{
mmd_t *mdp;
md_node_t *rootp;
size_t md_size;
uint8_t *md_bufp;
mdp = md_new_md();
if (mdp == NULL) {
return (ENOMEM);
}
rootp = md_new_node(mdp, "root");
if (rootp == NULL) {
md_destroy(mdp);
return (ENOMEM);
}
if (device_tree_to_md(mdp, rootp) != 0) {
md_destroy(mdp);
return (ENOMEM);
}
md_size = (int)md_gen_bin(mdp, &md_bufp);
if (md_size == 0) {
md_destroy(mdp);
return (EIO);
}
*mdpp = md_bufp;
*size = md_size;
md_destroy(mdp);
return (0);
}
|