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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2000, 2002 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright (c) 2016 by Delphix. All rights reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* fc_ops.c: Framework generic fcode ops
*/
#include <sys/types.h>
#include <sys/kmem.h>
#include <sys/systm.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/sunndi.h>
#include <sys/modctl.h>
#include <sys/fcode.h>
#include <sys/ddi_implfuncs.h>
#include <sys/ndi_impldefs.h>
#include <sys/ethernet.h>
static int fco_new_device(dev_info_t *, fco_handle_t, fc_ci_t *);
static int fco_finish_device(dev_info_t *, fco_handle_t, fc_ci_t *);
static int fco_create_property(dev_info_t *, fco_handle_t, fc_ci_t *);
static int fco_validate(dev_info_t *, fco_handle_t, fc_ci_t *);
static int fco_invalidate(dev_info_t *, fco_handle_t, fc_ci_t *);
static int fco_exit(dev_info_t *, fco_handle_t, fc_ci_t *);
static int fco_getproplen(dev_info_t *, fco_handle_t, fc_ci_t *);
static int fco_getprop(dev_info_t *, fco_handle_t, fc_ci_t *);
static int fco_ap_phandle(dev_info_t *, fco_handle_t, fc_ci_t *);
static int fco_child(dev_info_t *, fco_handle_t, fc_ci_t *);
static int fco_peer(dev_info_t *, fco_handle_t, fc_ci_t *);
static int fco_parent(dev_info_t *, fco_handle_t, fc_ci_t *);
static int fco_alloc_phandle(dev_info_t *, fco_handle_t, fc_ci_t *);
static int fco_local_ether_addr(dev_info_t *, fco_handle_t, fc_ci_t *);
struct fc_ops_v {
char *svc_name;
fc_ops_t *f;
};
static struct fc_ops_v fov[] = {
{ "open", fc_fail_op},
{ "close", fc_fail_op},
{ "$find", fc_fail_op},
{ "encode-unit", fc_fail_op},
{ "decode-unit", fc_fail_op},
{ FC_GET_MY_PROPLEN, fco_getproplen},
{ FC_GET_MY_PROP, fco_getprop},
{ FC_GET_PKG_PROPLEN, fco_getproplen},
{ FC_GET_PKG_PROP, fco_getprop},
{ FC_GET_IN_PROPLEN, fco_getproplen},
{ FC_GET_IN_PROP, fco_getprop},
{ FC_NEW_DEVICE, fco_new_device},
{ FC_FINISH_DEVICE, fco_finish_device},
{ FC_CREATE_PROPERTY, fco_create_property},
{ FC_AP_PHANDLE, fco_ap_phandle},
{ "child", fco_child},
{ "peer", fco_peer},
{ FC_PARENT, fco_parent},
{ FC_ALLOC_PHANDLE, fco_alloc_phandle},
{ FC_SVC_VALIDATE, fco_validate},
{ FC_SVC_INVALIDATE, fco_invalidate},
{ FC_SVC_EXIT, fco_exit},
{ "local-ether-addr", fco_local_ether_addr},
{ NULL, NULL}
};
/*
* Allocate a handle for the ops function .. our handle is a resource list
* Return the handle to our caller, so it can call us with it when we need it.
*/
/*ARGSUSED*/
fco_handle_t
fc_ops_alloc_handle(dev_info_t *ap, dev_info_t *child,
void *fcode, size_t fcode_size, char *unit_address, void *bus_args)
{
fco_handle_t rp;
char *up;
rp = kmem_zalloc(sizeof (struct fc_resource_list), KM_SLEEP);
rp->next_handle = NULL; /* nobody is downstream */
rp->ap = ap;
rp->child = child;
rp->fcode = fcode;
rp->fcode_size = fcode_size;
if (unit_address) {
up = kmem_zalloc(strlen(unit_address) + 1, KM_SLEEP);
(void) strcpy(up, unit_address);
rp->unit_address = up;
}
rp->bus_args = NULL; /* generic module has no bus args */
fc_phandle_table_alloc(fc_handle_to_phandle_head(rp));
(void) fc_dip_to_phandle(fc_handle_to_phandle_head(rp), ap);
/*
* Create our copy of the device tree.
*/
fc_create_device_tree(ap, &rp->dtree);
return (rp);
}
/*
* Free any resources associated with this handle.
*/
void
fc_ops_free_handle(fco_handle_t rp)
{
struct fc_resource *ip, *np;
if (rp->unit_address)
kmem_free(rp->unit_address, strlen(rp->unit_address) + 1);
if (rp->dtree)
fc_remove_device_tree(&rp->dtree);
fc_phandle_table_free(fc_handle_to_phandle_head(rp));
for (ip = rp->head; ip != NULL; ip = np) {
np = ip->next;
switch (ip->type) {
case RT_NODEID:
impl_ddi_free_nodeid(ip->fc_nodeid_r);
break;
default:
cmn_err(CE_CONT, "pci_fc_ops_free: "
"unknown resource type %d\n", ip->type);
break;
}
fc_rem_resource(rp, ip);
kmem_free(ip, sizeof (struct fc_resource));
}
kmem_free(rp, sizeof (struct fc_resource_list));
}
int
fc_ops(dev_info_t *ap, fco_handle_t handle, fc_ci_t *cp)
{
struct fc_ops_v *pv;
char *name = fc_cell2ptr(cp->svc_name);
for (pv = fov; pv->svc_name != NULL; ++pv)
if (strcmp(pv->svc_name, name) == 0)
return (pv->f(ap, handle, cp));
return (-1);
}
/*
* The interpreter can't do get-inherited-property directly,
* because we don't want to return a kernel address, so it
* has to break up the request into a get-proplen and get-prop
* call so it can allocate memory for the property and pass that
* buffer in to get-prop. The buffer should be 'suitably aligned'.
*
* XXX: We don't know the property type, so we can't return
* prop-encoded arrays, which fortunately, isn't a problem
* on big-endian machines.
*
* get-proplen has one result: proplen
* proplen is returned as -1 if the propname doesn't exist and
* as zero if the property is a boolean property.
*
* get-prop has one result: proplen, returned as -1 if propname doesn't exist.
*/
/*
* fco_getproplen ( propname phandle -- proplen )
*/
/*ARGSUSED*/
static int
fco_getproplen(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
int proplen;
int flags = 0;
fc_phandle_t h;
dev_info_t *dip;
char *pnp;
char propname[OBP_MAXPROPNAME];
if (strstr(fc_cell2ptr(cp->svc_name), "inherited") == NULL)
flags |= DDI_PROP_DONTPASS;
if (fc_cell2int(cp->nargs) != 2)
return (fc_syntax_error(cp, "nargs must be 2"));
if (fc_cell2int(cp->nresults) < 1)
return (fc_syntax_error(cp, "nresults must be > 0"));
/*
* Make sure this is a handle we gave out ...
*/
h = fc_cell2phandle(fc_arg(cp, 0));
if ((dip = fc_phandle_to_dip(fc_handle_to_phandle_head(rp), h)) == NULL)
return (fc_priv_error(cp, "unknown handle"));
/*
* XXX: We should care if the string is longer than OBP_MAXPROPNAME
*/
pnp = fc_cell2ptr(fc_arg(cp, 1));
bzero(propname, OBP_MAXPROPNAME);
if (copyinstr(pnp, propname, OBP_MAXPROPNAME - 1, NULL))
return (fc_priv_error(cp, "EFAULT copying in propname"));
if (ddi_getproplen(DDI_DEV_T_ANY, dip, flags, propname, &proplen))
proplen = -1;
fc_result(cp, 0) = fc_int2cell(proplen);
cp->nresults = fc_int2cell(1);
return (fc_success_op(ap, rp, cp));
}
/*
* fco_getprop ( propname buffer phandle -- proplen )
*/
/*ARGSUSED*/
static int
fco_getprop(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
int proplen = -1;
int flags = DDI_PROP_CANSLEEP;
char *pnp, *bp;
fc_phandle_t h;
dev_info_t *dip;
char propname[OBP_MAXPROPNAME];
if (strstr(fc_cell2ptr(cp->svc_name), "inherited") == NULL)
flags |= DDI_PROP_DONTPASS;
if (fc_cell2int(cp->nargs) != 3)
return (fc_syntax_error(cp, "nargs must be 3"));
if (fc_cell2int(cp->nresults) < 1)
return (fc_syntax_error(cp, "nresults must be > 0"));
/*
* Make sure this is a handle we gave out ...
*/
h = fc_cell2phandle(fc_arg(cp, 0));
if ((dip = fc_phandle_to_dip(fc_handle_to_phandle_head(rp), h)) == NULL)
return (fc_priv_error(cp, "unknown handle"));
/*
* XXX: We should care if the string is longer than OBP_MAXPROPNAME
*/
pnp = fc_cell2ptr(fc_arg(cp, 2));
bzero(propname, OBP_MAXPROPNAME);
if (copyinstr(pnp, propname, OBP_MAXPROPNAME - 1, NULL))
return (fc_priv_error(cp, "EFAULT copying in propname"));
if (ddi_getlongprop(DDI_DEV_T_ANY, dip, flags,
propname, (caddr_t)&bp, &proplen))
proplen = -1;
if (proplen > 0) {
char *up = fc_cell2ptr(fc_arg(cp, 1));
int error;
error = copyout(bp, up, proplen);
kmem_free(bp, proplen);
if (error)
return (fc_priv_error(cp, "EFAULT copying data out"));
}
cp->nresults = fc_int2cell(1);
fc_result(cp, 0) = fc_int2cell(proplen);
return (fc_success_op(ap, rp, cp));
}
static int
fco_ap_phandle(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
fc_phandle_t h;
if (fc_cell2int(cp->nargs) != 0)
return (fc_syntax_error(cp, "nargs must be 0"));
if (fc_cell2int(cp->nresults) < 1)
return (fc_syntax_error(cp, "nresults must be > 0"));
FC_DEBUG1(9, CE_CONT, "fco_ap_phandle: Looking up ap dip %p\n", ap);
h = fc_dip_to_phandle(fc_handle_to_phandle_head(rp), ap);
cp->nresults = fc_int2cell(1);
fc_result(cp, 0) = fc_phandle2cell(h);
return (fc_success_op(ap, rp, cp));
}
static int
fco_child(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
fc_phandle_t h;
dev_info_t *dip;
if (fc_cell2int(cp->nargs) != 1)
return (fc_syntax_error(cp, "nargs must be 1"));
if (fc_cell2int(cp->nresults) < 1)
return (fc_syntax_error(cp, "nresults must be > 0"));
/*
* Make sure this is a handle we gave out ...
*/
h = fc_cell2phandle(fc_arg(cp, 0));
if ((dip = fc_phandle_to_dip(fc_handle_to_phandle_head(rp), h)) == NULL)
return (fc_priv_error(cp, "unknown handle"));
/*
* Find the child and if there is one, return it ...
*/
dip = ddi_get_child(dip);
h = 0;
if (dip != NULL)
h = fc_dip_to_phandle(fc_handle_to_phandle_head(rp), dip);
cp->nresults = fc_int2cell(1);
fc_result(cp, 0) = fc_phandle2cell(h);
return (fc_success_op(ap, rp, cp));
}
static int
fco_peer(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
fc_phandle_t h;
dev_info_t *dip;
if (fc_cell2int(cp->nargs) != 1)
return (fc_syntax_error(cp, "nargs must be 1"));
if (fc_cell2int(cp->nresults) < 1)
return (fc_syntax_error(cp, "nresults must be > 0"));
/*
* Make sure this is a handle we gave out ...
*/
h = fc_cell2phandle(fc_arg(cp, 0));
if ((dip = fc_phandle_to_dip(fc_handle_to_phandle_head(rp), h)) == NULL)
return (fc_priv_error(cp, "unknown handle"));
/*
* Find the child and if there is one, return it ...
*/
dip = ddi_get_next_sibling(dip);
h = 0;
if (dip != NULL)
h = fc_dip_to_phandle(fc_handle_to_phandle_head(rp), dip);
cp->nresults = fc_int2cell(1);
fc_result(cp, 0) = fc_phandle2cell(h);
return (fc_success_op(ap, rp, cp));
}
static int
fco_parent(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
fc_phandle_t h;
dev_info_t *dip;
if (fc_cell2int(cp->nargs) != 1)
return (fc_syntax_error(cp, "nargs must be 1"));
if (fc_cell2int(cp->nresults) < 1)
return (fc_syntax_error(cp, "nresults must be > 0"));
/*
* Make sure this is a handle we gave out ...
*/
h = fc_cell2phandle(fc_arg(cp, 0));
if ((dip = fc_phandle_to_dip(fc_handle_to_phandle_head(rp), h)) == NULL)
return (fc_priv_error(cp, "unknown handle"));
/*
* Find the parent and if there is one, return it ...
*/
dip = ddi_get_parent(dip);
h = 0;
if (dip != NULL)
h = fc_dip_to_phandle(fc_handle_to_phandle_head(rp), dip);
cp->nresults = fc_int2cell(1);
fc_result(cp, 0) = fc_phandle2cell(h);
return (fc_success_op(ap, rp, cp));
}
/*
* Allocate a phandle ... we don't currently track the phandle.
*/
static int
fco_alloc_phandle(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
fc_phandle_t h;
int n;
struct fc_resource *ip;
if (fc_cell2int(cp->nargs) != 0)
return (fc_syntax_error(cp, "nargs must be 0"));
if (fc_cell2int(cp->nresults) < 1)
return (fc_syntax_error(cp, "nresults must be > 0"));
if (impl_ddi_alloc_nodeid(&n))
return (fc_priv_error(cp, "Can't allocate a nodeid"));
/*
* Log the nodeid resource so we can release it later if we need to.
*/
ip = kmem_zalloc(sizeof (struct fc_resource), KM_SLEEP);
ip->type = RT_NODEID;
ip->fc_nodeid_r = n;
fc_add_resource(rp, ip);
h = (fc_phandle_t)n;
cp->nresults = fc_int2cell(1);
fc_result(cp, 0) = fc_phandle2cell(h);
return (fc_success_op(ap, rp, cp));
}
static struct fc_resource *
find_nodeid_resource(fco_handle_t rp, int n)
{
struct fc_resource *ip;
fc_lock_resource_list(rp);
for (ip = rp->head; ip != NULL; ip = ip->next) {
if (ip->type != RT_NODEID)
continue;
if (ip->fc_nodeid_r == n)
break;
}
fc_unlock_resource_list(rp);
return (ip);
}
/*
* fco_new_device ( name-cstr unit-addr-cstr parent.phandle phandle -- )
*/
static int
fco_new_device(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
fc_phandle_t ph, ch;
dev_info_t *pdev, *cdev;
char *s;
int createmode = 0;
char *unit_address = NULL;
char nodename[OBP_MAXPROPNAME];
if (fc_cell2int(cp->nargs) != 4)
return (fc_syntax_error(cp, "nargs must be 4"));
/*
* Make sure these are handles we gave out ... and we have
* a corresponding parent devinfo node.
*/
ph = fc_cell2phandle(fc_arg(cp, 1));
pdev = fc_phandle_to_dip(fc_handle_to_phandle_head(rp), ph);
if (pdev == NULL)
return (fc_priv_error(cp, "unknown parent phandle"));
ch = fc_cell2phandle(fc_arg(cp, 0));
cdev = fc_phandle_to_dip(fc_handle_to_phandle_head(rp), ch);
switch (rp->cdip_state) {
case FC_CDIP_NOSTATE:
/*
* The first child must be a child of the attachment point.
*/
if (pdev != ap)
return (fc_priv_error(cp, "first child must be a "
"child of the attachment point"));
/*
* If this bus has a config child, the first child must
* be the configuration child. Otherwise, the child must
* be a new (unknown) node.
*/
if (cdev != NULL) {
if (rp->child != NULL) {
if (cdev != rp->child)
return (fc_priv_error(cp, "first "
"child must be the "
"configuration child"));
} else {
return (fc_priv_error(cp, "known child -- "
"unknown child expected"));
}
}
break;
case FC_CDIP_DONE:
/*
* If we've already created the first child, this
* child must be unknown and the parent must be a known
* child of the attachment point.
*/
if (cdev)
return (fc_priv_error(cp, "known child -- "
"unknown child expected"));
if (fc_find_node(pdev, fc_handle_to_dtree(rp)) == NULL)
return (fc_priv_error(cp, "parent is an unknown "
"child of the attachment point"));
break;
default:
/*
* If we're in some other state, we shouldn't be here.
*/
return (fc_priv_error(cp, "bad node-creation state"));
/* NOTREACHED */
}
/*
* Get the nodename and the unit address.
*/
s = fc_cell2ptr(fc_arg(cp, 3));
bzero(nodename, OBP_MAXPROPNAME);
if (copyinstr(s, nodename, OBP_MAXPROPNAME - 1, NULL))
return (fc_priv_error(cp, "EFAULT copying in nodename"));
s = fc_cell2ptr(fc_arg(cp, 2));
unit_address = kmem_zalloc(OBP_MAXPATHLEN, KM_SLEEP);
if (copyinstr(s, unit_address, OBP_MAXPATHLEN - 1, NULL)) {
kmem_free(unit_address, OBP_MAXPATHLEN);
return (fc_priv_error(cp, "EFAULT copying in unit address"));
}
/*
* If cdev is NULL, we have to create the child, otherwise, the
* child already exists and we're just merging properties into
* the existing node. The node must be unbound.
*/
if (cdev == NULL)
createmode = 1;
if (createmode) {
struct fc_resource *ip;
int nodeid;
/*
* Make sure 'ch' is a nodeid we gave the interpreter.
* It must be on our resource list.
*/
if ((ip = find_nodeid_resource(rp, (int)ch)) == NULL) {
kmem_free(unit_address, OBP_MAXPATHLEN);
return (fc_priv_error(cp, "Unknown phandle"));
}
/*
* Allocate a self-identifying, persistent node with
* the auto-free attribute.
*/
if (ndi_devi_alloc(pdev, nodename, DEVI_SID_NODEID, &cdev)) {
kmem_free(unit_address, OBP_MAXPATHLEN);
return (fc_priv_error(cp, "Can't create node"));
}
/*
* Free the nodeid we just allocated here, and use
* the one we handed in. Retain the attributes of
* the original SID nodetype.
*/
nodeid = ddi_get_nodeid(cdev);
i_ndi_set_nodeid(cdev, (int)ch);
impl_ddi_free_nodeid(nodeid);
/*
* Remove nodeid 'ch' from our resource list, now that it
* will be managed by the ddi framework.
*/
fc_rem_resource(rp, ip);
kmem_free(ip, sizeof (struct fc_resource));
} else if (strcmp(ddi_node_name(cdev), nodename) != 0) {
FC_DEBUG2(1, CE_CONT, "Changing <%s> nodename to <%s>\n",
ddi_node_name(cdev), nodename);
if (ndi_devi_set_nodename(cdev, nodename, 0)) {
kmem_free(unit_address, OBP_MAXPATHLEN);
return (fc_priv_error(cp, "Can't set ndi nodename"));
}
}
if (fc_ndi_prop_update(DDI_DEV_T_NONE, cdev, "name",
(uchar_t *)nodename, strlen(nodename) + 1)) {
kmem_free(unit_address, OBP_MAXPATHLEN);
if (createmode)
(void) ndi_devi_free(cdev);
return (fc_priv_error(cp, "Can't create name property"));
}
/*
* Add the dip->phandle translation to our list of known phandles.
*/
fc_add_dip_to_phandle(fc_handle_to_phandle_head(rp), cdev, ch);
/*
* Add the new node to our copy of the subtree.
*/
fc_add_child(cdev, pdev, fc_handle_to_dtree(rp));
rp->cdip = cdev;
rp->cdip_state = FC_CDIP_STARTED;
kmem_free(unit_address, OBP_MAXPATHLEN);
cp->nresults = fc_int2cell(0);
return (fc_success_op(ap, rp, cp));
}
/*
* fco_finish_device ( phandle -- )
*/
static int
fco_finish_device(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
fc_phandle_t h;
dev_info_t *cdev;
if (fc_cell2int(cp->nargs) != 1)
return (fc_syntax_error(cp, "nargs must be 1"));
if (rp->cdip_state != FC_CDIP_STARTED)
return (fc_priv_error(cp, "bad node-creation state"));
h = fc_cell2phandle(fc_arg(cp, 0));
cdev = fc_phandle_to_dip(fc_handle_to_phandle_head(rp), h);
if (cdev != rp->cdip)
return (fc_priv_error(cp, "bad phandle"));
/*
* We don't want to online children of the attachment point.
* We'll 'config' them online later.
*
* XXX - APA - I've changed this a bit. The only time we don't
* want to bind the device is if the parent is the attachment point
* and the device is the same as the device that was passed to
* the interpreter. We assume the configurator will do the binding.
*/
if ((ddi_get_parent(cdev) == ap) && (cdev == rp->child)) {
FC_DEBUG2(5, CE_CONT, "fc_finish_device: "
"*not* binding <%s> dip %p\n", ddi_node_name(cdev), cdev);
} else {
FC_DEBUG2(5, CE_CONT, "fc_finish_device: binding <%s> dip %p\n",
ddi_node_name(cdev), cdev);
(void) ndi_devi_bind_driver(cdev, 0);
}
rp->cdip_state = FC_CDIP_DONE;
cp->nresults = fc_int2cell(0);
return (fc_success_op(ap, rp, cp));
}
/*
* fco_create_property ( propname-cstr buf len phandle -- )
*/
static int
fco_create_property(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
char *buf, *bp, *pnp;
size_t len;
fc_phandle_t h;
dev_info_t *dev;
int error;
char propname[OBP_MAXPROPNAME];
if (fc_cell2int(cp->nargs) != 4)
return (fc_syntax_error(cp, "nargs must be 4"));
h = fc_cell2phandle(fc_arg(cp, 0));
len = fc_cell2size(fc_arg(cp, 1));
bp = fc_cell2ptr(fc_arg(cp, 2));
pnp = fc_cell2ptr(fc_arg(cp, 3));
dev = fc_phandle_to_dip(fc_handle_to_phandle_head(rp), h);
if (dev == NULL)
return (fc_priv_error(cp, "bad phandle"));
bzero(propname, OBP_MAXPROPNAME);
if (copyinstr(pnp, propname, OBP_MAXPROPNAME - 1, NULL))
return (fc_priv_error(cp, "EFAULT copying in propname"));
buf = NULL;
if (len != 0) {
buf = kmem_zalloc(len, KM_SLEEP);
if (copyin(bp, buf, len)) {
kmem_free(buf, len);
return (fc_priv_error(cp, "EFAULT copying in propval"));
}
}
/*
* check for propname: 'name' ... we don't allow it
* by changed here. It has to be specified when the node
* is created.
*/
if (strcmp(propname, "name") == 0) {
char *n = ddi_node_name(dev);
if (len == 0)
return (fc_priv_error(cp, "setting <name> to NULL"));
if ((len < (strlen(n) + 1)) || (strcmp(n, buf) != 0)) {
kmem_free(buf, len);
return (fc_priv_error(cp, "changing <name> property"));
}
/*
* Since we're not changing the value, and we already created
* the 'name' property when we created the node ...
*/
kmem_free(buf, len);
cp->nresults = fc_int2cell(0);
return (fc_success_op(ap, rp, cp));
}
error = fc_ndi_prop_update(DDI_DEV_T_NONE, dev, propname,
(uchar_t *)buf, len);
if (len != 0)
kmem_free(buf, len);
if (error)
return (fc_priv_error(cp, "Can't create property"));
cp->nresults = fc_int2cell(0);
return (fc_success_op(ap, rp, cp));
}
/*
* Make sure any in-progress activity is completed,
* and for now, online the subtree.
* XXX: Presumably the configurator will online the subtree
* XXX: by doing an ndi_devi_online with NDI_CONFIG on the child
* XXX: if there is one. For now, we're doing it here.
* XXX: For buses without a configurator (and thus no config child),
* XXX: we have to do it here.
*
*/
static int
fco_validate(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
rp->cdip_state = FC_CDIP_CONFIG;
cp->nresults = fc_int2cell(0);
return (fc_success_op(ap, rp, cp));
}
static void
remove_subtree(dev_info_t *root, struct fc_device_tree *subtree)
{
dev_info_t *child;
/*
* Remove the subtree, depth first. Each iterative
* call gets another child at each level of the tree
* until there are no more children.
*/
while ((child = fc_child_node(root, subtree)) != NULL)
remove_subtree(child, subtree);
/*
* Delete the subtree root and remove its record from our
* copy of the subtree.
*/
fc_remove_child(root, subtree);
(void) ndi_devi_offline(root, NDI_UNCONFIG | NDI_DEVI_REMOVE);
}
static int
fco_invalidate(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
dev_info_t *root, *child;
struct fc_device_tree *subtree = fc_handle_to_dtree(rp);
int configured = (rp->cdip_state == FC_CDIP_CONFIG);
/*
* If we created any children, delete them. The root node is the
* config child, if one exists for this bus, otherwise it's the
* attachment point.
*
* Our copy of the subtree only contains records of nodes we created
* under the subtree root and contains the parent->child linkage
* that isn't yet established in the real device tree.
*
* XXX: What we don't do is restore the config child node to it's
* pre-interpretive state. (We may have added properties to
* that node. It's not clear if its necessary to clean them up.)
*/
root = rp->child ? rp->child : ap;
while ((child = fc_child_node(root, subtree)) != NULL) {
FC_DEBUG2(1, CE_CONT, "fco_invalidate: remove subtree "
"<%s> dip %p\n", ddi_node_name(child), child);
remove_subtree(child, subtree);
}
if (configured)
(void) ndi_devi_offline(root, NDI_UNCONFIG);
cp->nresults = fc_int2cell(0);
return (fc_success_op(ap, rp, cp));
}
static int
fco_exit(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
FC_DEBUG0(1, CE_CONT, "exit op not implemented .. succeeding\n");
cp->nresults = fc_int2cell(0);
return (fc_success_op(ap, rp, cp));
}
/*
* Needed to implement 'mac-address' Fcode, no obvious place to pick this
* info up from user-land.
*/
static int
fco_local_ether_addr(dev_info_t *ap, fco_handle_t rp, fc_ci_t *cp)
{
if (fc_cell2int(cp->nargs) != 0)
return (fc_syntax_error(cp, "nargs must be 0"));
if (fc_cell2int(cp->nresults) != 2)
return (fc_syntax_error(cp, "nresults must be 2"));
cp->nresults = fc_int2cell(2);
(void) localetheraddr(NULL, (struct ether_addr *)(&fc_result(cp, 0)));
return (fc_success_op(ap, rp, cp));
}
#ifdef DEBUG
void
fc_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
|