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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Schizo/PCI Functions to the Safari Configurator
*
*/
#include <sys/types.h>
#include <sys/cred.h>
#include <sys/mman.h>
#include <sys/kmem.h>
#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/sunndi.h>
#include <sys/modctl.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/autoconf.h>
#include <sys/ksynch.h>
#include <sys/promif.h>
#include <sys/ndi_impldefs.h>
#include <sys/ddi_impldefs.h>
#include <sys/machsystm.h>
#include <sys/gp2cfg.h>
#include <sys/gptwo_pci.h>
#ifdef DEBUG
int gptwo_pci_debug = 0;
static void debug(char *, uintptr_t, uintptr_t,
uintptr_t, uintptr_t, uintptr_t);
#define GPTWO_DEBUG0(level, flag, s) if (gptwo_pci_debug >= level) \
cmn_err(flag, s)
#define GPTWO_DEBUG1(level, flag, fmt, a1) if (gptwo_pci_debug >= level) \
debug(fmt, (uintptr_t)(a1), 0, 0, 0, 0);
#define GPTWO_DEBUG2(level, flag, fmt, a1, a2) if (gptwo_pci_debug >= level) \
debug(fmt, (uintptr_t)(a1), (uintptr_t)(a2), 0, 0, 0);
#define GPTWO_DEBUG3(level, flag, fmt, a1, a2, a3) \
if (gptwo_pci_debug >= level) \
debug(fmt, (uintptr_t)(a1), (uintptr_t)(a2), (uintptr_t)(a3), 0, 0);
#else
#define GPTWO_DEBUG0(level, flag, s)
#define GPTWO_DEBUG1(level, flag, fmt, a1)
#define GPTWO_DEBUG2(level, flag, fmt, a1, a2)
#define GPTWO_DEBUG3(level, flag, fmt, a1, a2, a3)
#endif
void gptwocfg_devi_attach_to_parent(dev_info_t *);
static char *gptwo_get_probe_string(spcd_t *, int);
static void gptwo_find_nodes(dev_info_t *, int, gptwo_new_nodes_t *);
extern caddr_t efcode_vaddr;
extern int efcode_size;
/*
* Module linkage information for the kernel.
*/
extern struct mod_ops mod_miscops;
static struct modlmisc modlmisc = {
&mod_miscops, /* Type of module */
"gptwo->pci configurator",
};
static struct modlinkage modlinkage = {
MODREV_1, (void *)&modlmisc, NULL
};
int
_init(void)
{
int err = 0;
/*
* Create a resource map for the contigous memory allocated
* at start-of-day in startup.c
*/
if (ndi_ra_map_setup(ddi_root_node(), "gptwo-contigousmem")
== NDI_FAILURE) {
GPTWO_DEBUG0(1, CE_WARN,
"Can not setup resource map - gptwo-contigousmem\n");
return (1);
}
/*
* Put the allocated memory into the pool.
*/
(void) ndi_ra_free(ddi_root_node(), (uint64_t)efcode_vaddr,
(uint64_t)efcode_size, "gptwo-contigousmem", 0);
/* register devices with the configurator */
gptwocfg_register_ops(SAFPTYPE_sPCI, gptwo_configure_pci,
gptwo_unconfigure_pci);
gptwocfg_register_ops(SAFPTYPE_cPCI, gptwo_configure_pci,
gptwo_unconfigure_pci);
gptwocfg_register_ops(SAFPTYPE_PCIX, gptwo_configure_pci,
gptwo_unconfigure_pci);
if ((err = mod_install(&modlinkage)) != 0) {
GPTWO_DEBUG1(1, CE_WARN, "gptwo_pci (PCI Functions) "
"failed to load, error=%d\n", err);
gptwocfg_unregister_ops(SAFPTYPE_sPCI);
gptwocfg_unregister_ops(SAFPTYPE_cPCI);
gptwocfg_unregister_ops(SAFPTYPE_PCIX);
} else {
GPTWO_DEBUG0(1, CE_WARN, "gptwo_pci (PCI Functions) "
"has been loaded.\n");
}
return (err);
}
int
_fini(void)
{
gptwocfg_unregister_ops(SAFPTYPE_sPCI);
gptwocfg_unregister_ops(SAFPTYPE_cPCI);
gptwocfg_unregister_ops(SAFPTYPE_PCIX);
return (mod_remove(&modlinkage));
}
int
_info(modinfop)
struct modinfo *modinfop;
{
return (mod_info(&modlinkage, modinfop));
}
/*ARGSUSED*/
static int
set_name_prop(dev_info_t *dip, void *arg, uint_t flags)
{
if (ndi_prop_update_string(DDI_DEV_T_NONE, dip,
"name", "pci") != DDI_SUCCESS) {
return (DDI_WALK_ERROR);
}
return (DDI_WALK_TERMINATE);
}
/*ARGSUSED*/
static void
get_new_child(dev_info_t *rdip, void *arg, uint_t flags)
{
dev_info_t **dipp = (dev_info_t **)arg;
ASSERT(dipp && (*dipp == NULL));
*dipp = rdip;
}
gptwo_new_nodes_t *
gptwo_configure_pci(dev_info_t *ap, spcd_t *pcd, uint_t id)
{
fco_handle_t fco_handle;
int error, i, circ, freq;
dev_info_t *new_child;
char unit_address[64];
gptwo_new_nodes_t *new_nodes;
char *probe_string;
devi_branch_t b = {0};
GPTWO_DEBUG2(1, CE_CONT, "gptwo_configure_pci: id=%x pcd=%lx\n",
id, pcd);
new_nodes = gptwocfg_allocate_node_list(IOBUS_PER_PORT);
i = IOBUS_PER_PORT;
while (i) {
i--;
if (pcd->spcd_iobus_rsv[i] != SPCD_RSV_PASS) {
cmn_err(CE_WARN, "gptwo_configure_pci: saf id=0x%x "
"leaf %d - Can not be probed\n", id, i);
continue;
}
/*
* Ideally, fcode would be run from the "sid_branch_create"
* callback (that is the primary purpose of that callback).
* However, the fcode interpreter was written with the
* assumption that the "new_child" was linked into the
* device tree. The callback is invoked with the devinfo node
* in the DS_PROTO state. More investigation is needed before
* we can invoke the interpreter from the callback. For now,
* we create the "new_child" in the BOUND state, invoke the
* fcode interpreter and then rebind the dip to use any
* compatible properties created by fcode.
*/
new_child = NULL;
b.arg = &new_child;
b.type = DEVI_BRANCH_SID;
b.create.sid_branch_create = set_name_prop;
b.devi_branch_callback = get_new_child;
/*
* Prevent any changes to new_child
* until we have bound it to the correct driver.
*/
ndi_devi_enter(ap, &circ);
if (e_ddi_branch_create(ap, &b, NULL, 0)) {
ASSERT(new_child == NULL);
if (new_nodes->gptwo_nodes[0] == NULL) {
GPTWO_DEBUG0(1, CE_CONT, "gptwo_configure_pci: "
"No nodes configured - "
"removing new_nodes\n");
gptwocfg_free_node_list(new_nodes);
new_nodes = NULL;
}
ndi_devi_exit(ap, circ);
return (new_nodes);
}
/*
* The platform DR interfaces created the dip in
* bound state. Bring devinfo node down to linked
* state and hold it there until compatible
* properties are created.
*/
e_ddi_branch_rele(new_child);
(void) i_ndi_unconfig_node(new_child, DS_LINKED, 0);
ASSERT(i_ddi_node_state(new_child) == DS_LINKED);
e_ddi_branch_hold(new_child);
mutex_enter(&DEVI(new_child)->devi_lock);
DEVI(new_child)->devi_flags |= DEVI_NO_BIND;
mutex_exit(&DEVI(new_child)->devi_lock);
/*
* Drop the busy-hold on parent before calling
* fcode_interpreter to prevent potential deadlocks
*/
ndi_devi_exit(ap, circ);
(void) sprintf(unit_address, "%x", id);
/*
* Build the probe string from the PCD that will be passed
* in to the interpreter as my-args. This will tell the
* fcode what pci devices to probe after the pci node has
* been probed.
*/
probe_string = gptwo_get_probe_string(pcd, i);
GPTWO_DEBUG3(1, CE_CONT, "gptwo_configure_pci: args to "
"interpreter ap=%lx new_child=%lx unit_address=%s\n",
ap, new_child, unit_address);
if (probe_string)
GPTWO_DEBUG1(1, CE_CONT, "gptwo_configure_pci: "
"probe string=%s\n", probe_string);
fco_handle = gp2_fc_ops_alloc_handle(ap, new_child, NULL, NULL,
unit_address, probe_string);
GPTWO_DEBUG0(1, CE_CONT,
"gptwocfg: Calling Fcode Interpeter...\n");
error = fcode_interpreter(ap, &gp2_fc_ops, fco_handle);
GPTWO_DEBUG1(1, CE_CONT,
"gptwo_configure_pci: fcode_interpreter "
" returned %x\n", error);
if (error) {
cmn_err(CE_WARN, "gptwo_pci: Unable to probe pci leaf "
"%s\n", unit_address);
gp2_fc_ops_free_handle(fco_handle);
(void) e_ddi_branch_destroy(new_child, NULL, 0);
} else {
gptwocfg_save_handle(new_child, fco_handle);
/*
* Compatible properties (if any) have been created,
* so bind driver.
*/
ndi_devi_enter(ap, &circ);
ASSERT(i_ddi_node_state(new_child) <= DS_LINKED);
mutex_enter(&DEVI(new_child)->devi_lock);
DEVI(new_child)->devi_flags &= ~DEVI_NO_BIND;
mutex_exit(&DEVI(new_child)->devi_lock);
ndi_devi_exit(ap, circ);
if (ndi_devi_bind_driver(new_child, 0) !=
DDI_SUCCESS) {
cmn_err(CE_WARN, "gptwo_pci: Unable to bind"
" new pci child at dip=0x%p\n",
(void *)new_child);
}
/*
* If POST provided a frequency, the clock-frequency
* property needs to be updated.
*/
if (pcd->spcd_afreq) {
/*
* The upper byte is for leaf B and the lower
* byte is for leaf A.
*/
if (i)
freq = pcd->spcd_afreq >> 8;
else
freq = pcd->spcd_afreq & 0x00ff;
(void) ndi_prop_update_int(DDI_DEV_T_NONE,
new_child, "clock-frequency",
(freq * 1000 * 1000));
}
}
}
gptwo_find_nodes(ap, id, new_nodes);
if (new_nodes->gptwo_nodes[0] == NULL) {
GPTWO_DEBUG0(1, CE_CONT, "gptwo_configure_pci: "
"No nodes configured - removing new_nodes\n");
gptwocfg_free_node_list(new_nodes);
new_nodes = NULL;
}
GPTWO_DEBUG1(1, CE_CONT, "gptwo_configure_pci: "
"Returning new_nodes=%p\n", new_nodes);
return (new_nodes);
}
dev_info_t *
gptwo_unconfigure_pci(dev_info_t *dip)
{
fco_handle_t fco_handle;
fco_handle = gptwocfg_get_handle(dip);
if (fco_handle != NULL) {
/*
* If there is a handle, there may be resources
* that need to be freed from when the
* devices's fcode ran.
*/
GPTWO_DEBUG1(1, CE_CONT, "fco_handle=%lx\n", fco_handle);
gp2_fc_ops_free_handle(fco_handle);
}
return (NULL);
}
static void
gptwo_find_nodes(dev_info_t *ap, int id, gptwo_new_nodes_t *new_nodes)
{
dev_info_t *saf_dev;
int found, j, circ;
int i = 0;
GPTWO_DEBUG1(1, CE_CONT, "gptwo_find_nodes - id=%x\n", id);
/*
* We are walking child list of ap, so hold it busy
*/
ndi_devi_enter(ap, &circ);
saf_dev = ddi_get_child(ap);
while (saf_dev != NULL) {
if (ddi_getprop(DDI_DEV_T_ANY, saf_dev,
DDI_PROP_DONTPASS, "portid", -1) == id) {
if (i < IOBUS_PER_PORT) {
GPTWO_DEBUG2(1, CE_CONT,
"gptwo_find_nodes - "
"Found %d %p\n", i, saf_dev);
found = 0;
for (j = 0; j < IOBUS_PER_PORT; j++) {
if (new_nodes->gptwo_nodes[j] ==
saf_dev) {
found = 1;
}
}
if (!found) {
/*
* Branch rooted at saf-dev was
* held earlier.
*/
ASSERT(e_ddi_branch_held(saf_dev));
new_nodes->gptwo_nodes[i] = saf_dev;
i++;
}
} else {
GPTWO_DEBUG0(1, CE_CONT,
"gptwo_find_nodes - "
"No room in new_nodes\n");
}
}
saf_dev = ddi_get_next_sibling(saf_dev);
}
ndi_devi_exit(ap, circ);
}
static char *
gptwo_get_probe_string(spcd_t *pcd, int bus_number)
{
int i, str_size;
char temp[64];
char num[8];
char *probe;
GPTWO_DEBUG2(1, CE_CONT, "gptwo_get_probe_string - %p %x\n", pcd,
bus_number);
temp[0] = NULL;
for (i = 0; i < IOCARD_PER_BUS; i++) {
GPTWO_DEBUG2(1, CE_CONT, "gptwo_get_probe_string - "
"card status %x %x\n",
i, pcd->spcd_iocard_rsv[bus_number][i]);
if (pcd->spcd_iocard_rsv[bus_number][i] == SPCD_RSV_PASS) {
numtos(i, num);
if (temp[0] == NULL)
(void) sprintf(temp, "%s", num);
else
(void) sprintf(temp, "%s,%s", temp, num);
}
}
if (bus_number == 0)
(void) sprintf(temp, "%sa", temp); /* Append a 'a' for leaf A */
else
(void) sprintf(temp, "%sb", temp); /* Append a 'b' for leaf B */
str_size = strlen(temp);
if (str_size == 0)
return (NULL);
probe = kmem_zalloc(str_size + 1, KM_SLEEP);
(void) strcpy(probe, temp);
GPTWO_DEBUG1(1, CE_CONT, "gptwo_get_probe_string - Returning %s\n",
probe);
return (probe);
}
#ifdef DEBUG
static void
debug(char *fmt, uintptr_t a1, uintptr_t a2, uintptr_t a3,
uintptr_t a4, uintptr_t a5)
{
cmn_err(CE_CONT, fmt, a1, a2, a3, a4, a5);
}
#endif
|