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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
|
/*
* 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 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* fcode helper driver -- provide priv. access and kernel communication
* to the userland fcode interpreter.
*/
#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/ddi_impldefs.h>
#include <sys/ndi_impldefs.h>
#include <sys/modctl.h>
#include <sys/stat.h>
#include <sys/fcode.h>
static int fc_max_opens = 32; /* Up to this many simultaneous opens */
/*
* Soft state associated with each instance of driver open.
*/
static struct fc_state {
int state; /* available flag or active state */
struct fc_request *req; /* Active Request */
} *fc_states;
#define FC_STATE_INACTIVE 0 /* Unopen, available for use */
#define FC_STATE_OPEN 1 /* Inital open */
#define FC_STATE_READ_DONE 2 /* blocking read done */
#define FC_STATE_IN_PROGRESS 3 /* FC_GET_PARAMETERS done, active */
#define FC_STATE_VALIDATED 4 /* FC_VALIDATE done, active */
#define FC_STATE_ERROR_SET 5 /* FC_SET_FCODE_ERROR done, active */
#define FC_STATE_ACTIVE(s) ((s) != 0)
#define FC_STATE_AVAILABLE(s) ((s) == FC_STATE_INACTIVE)
static kmutex_t fc_open_lock; /* serialize instance assignment */
static kcondvar_t fc_open_cv; /* wait for available open */
static int fc_open_count; /* number of current open instance */
static int fc_open(dev_t *, int, int, cred_t *);
static int fc_close(dev_t, int, int, cred_t *);
static int fc_read(dev_t, struct uio *, cred_t *);
static int fc_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
static int fc_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
static int fc_attach(dev_info_t *, ddi_attach_cmd_t cmd);
static int fc_detach(dev_info_t *, ddi_detach_cmd_t cmd);
static int fc_get_parameters(dev_t, intptr_t, int, cred_t *, int *);
static int fc_get_my_args(dev_t, intptr_t, int, cred_t *, int *);
static int fc_run_priv(dev_t, intptr_t, int, cred_t *, int *);
static int fc_validate(dev_t, intptr_t, int, cred_t *, int *);
static int fc_get_fcode(dev_t, intptr_t, int, cred_t *, int *);
static int fc_set_fcode_error(dev_t, intptr_t, int, cred_t *, int *);
static struct cb_ops fc_cb_ops = {
fc_open, /* open */
fc_close, /* close */
nodev, /* strategy */
nodev, /* print */
nodev, /* dump */
fc_read, /* read */
nodev, /* write */
fc_ioctl, /* ioctl */
nodev, /* devmap */
nodev, /* mmap */
nodev, /* segmap */
nochpoll, /* poll */
ddi_prop_op, /* prop_op */
NULL, /* streamtab */
D_NEW | D_MP /* Driver compatibility flag */
};
static struct dev_ops fcode_ops = {
DEVO_REV, /* devo_rev, */
0, /* refcnt */
fc_info, /* info */
nulldev, /* identify */
nulldev, /* probe */
fc_attach, /* attach */
fc_detach, /* detach */
nodev, /* reset */
&fc_cb_ops, /* driver operations */
NULL /* bus operations */
};
/*
* Module linkage information for the kernel.
*/
static struct modldrv modldrv = {
&mod_driverops,
"FCode driver %I%",
&fcode_ops
};
static struct modlinkage modlinkage = {
MODREV_1,
&modldrv,
NULL
};
#ifndef lint
static char _depends_on[] = "misc/fcodem";
#endif
int
_init(void)
{
int error;
mutex_init(&fc_open_lock, NULL, MUTEX_DRIVER, NULL);
cv_init(&fc_open_cv, NULL, CV_DRIVER, NULL);
error = mod_install(&modlinkage);
if (error != 0) {
mutex_destroy(&fc_open_lock);
cv_destroy(&fc_open_cv);
return (error);
}
return (0);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
int
_fini(void)
{
int error;
error = mod_remove(&modlinkage);
if (error != 0) {
return (error);
}
mutex_destroy(&fc_open_lock);
cv_destroy(&fc_open_cv);
return (0);
}
static dev_info_t *fc_dip;
/*ARGSUSED*/
static int
fc_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
{
int error = DDI_FAILURE;
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
*result = (void *)fc_dip;
error = DDI_SUCCESS;
break;
case DDI_INFO_DEVT2INSTANCE:
/* All dev_t's map to the same, single instance */
*result = (void *)0;
error = DDI_SUCCESS;
break;
default:
break;
}
return (error);
}
static int
fc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
int error = DDI_FAILURE;
switch (cmd) {
case DDI_ATTACH:
fc_open_count = 0;
fc_states = kmem_zalloc(
fc_max_opens * sizeof (struct fc_state), KM_SLEEP);
if (ddi_create_minor_node(dip, "fcode", S_IFCHR,
0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
kmem_free(fc_states,
fc_max_opens * sizeof (struct fc_state));
error = DDI_FAILURE;
} else {
fc_dip = dip;
ddi_report_dev(dip);
error = DDI_SUCCESS;
}
break;
default:
error = DDI_FAILURE;
break;
}
return (error);
}
static int
fc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
int error = DDI_FAILURE;
switch (cmd) {
case DDI_DETACH:
ddi_remove_minor_node(dip, NULL);
fc_dip = NULL;
kmem_free(fc_states, fc_max_opens * sizeof (struct fc_state));
error = DDI_SUCCESS;
break;
default:
error = DDI_FAILURE;
break;
}
return (error);
}
/*
* Allow multiple opens by tweaking the dev_t such that it looks like each
* open is getting a different minor device. Each minor gets a separate
* entry in the fc_states[] table.
*/
/*ARGSUSED*/
static int
fc_open(dev_t *devp, int flag, int otyp, cred_t *credp)
{
int m;
struct fc_state *st;
if (getminor(*devp) != 0)
return (EINVAL);
mutex_enter(&fc_open_lock);
while (fc_open_count >= fc_max_opens) {
/*
* maximum open instance reached, wait for a close
*/
FC_DEBUG0(1, CE_WARN,
"fcode: Maximum fcode open reached, waiting for exit\n");
if (cv_wait_sig(&fc_open_cv, &fc_open_lock) == 0) {
mutex_exit(&fc_open_lock);
return (EINTR);
/*NOTREACHED*/
}
}
fc_open_count++;
for (m = 0, st = fc_states; m < fc_max_opens; m++, st++) {
if (FC_STATE_ACTIVE(st->state))
continue;
st->state = FC_STATE_OPEN;
st->req = 0;
break; /* It's ours. */
}
mutex_exit(&fc_open_lock);
ASSERT(m < fc_max_opens);
*devp = makedevice(getmajor(*devp), (minor_t)(m + 1));
FC_DEBUG2(9, CE_CONT, "fc_open: open count = %d (%d)\n",
fc_open_count, m + 1);
return (0);
}
/*ARGSUSED*/
static int
fc_close(dev_t dev, int flag, int otype, cred_t *cred_p)
{
struct fc_state *st;
int m = (int)getminor(dev) - 1;
struct fc_request *fp;
struct fc_client_interface *cp;
st = fc_states + m;
ASSERT(m < fc_max_opens && FC_STATE_ACTIVE(st->state));
/*
* The close indicates we're done with this request.
* If we haven't validated this request, then something
* bad may have happened (ie: perhaps the user program was
* killed), so we should invalidate it, then close the session.
*/
if (st->state == FC_STATE_READ_DONE) {
fp = st->req;
fp->error = FC_ERROR;
}
if (st->state > FC_STATE_READ_DONE) {
cp = kmem_zalloc(sizeof (struct fc_client_interface), KM_SLEEP);
fp = st->req;
ASSERT(fp);
ASSERT(fp->ap_ops);
if (st->state != FC_STATE_VALIDATED) {
FC_DEBUG0(1, CE_CONT,
"fc_close: Send invalidate cmd\n");
cp->svc_name = fc_ptr2cell(FC_SVC_INVALIDATE);
(void) fp->ap_ops(fp->ap_dip, fp->handle, cp);
if ((st->state != FC_STATE_ERROR_SET) ||
(fp->error == FC_SUCCESS)) {
fp->error = FC_ERROR;
}
/*
* else - fp->error already set by userland interpreter
*/
}
bzero(cp, sizeof (struct fc_client_interface));
FC_DEBUG0(9, CE_CONT, "fc_close: Sending exit cmd\n");
cp->svc_name = fc_ptr2cell(FC_SVC_EXIT);
(void) fp->ap_ops(fp->ap_dip, fp->handle, cp);
kmem_free(cp, sizeof (struct fc_client_interface));
}
/*
* Mark the request as done ...
*/
if ((fp = st->req) != NULL)
fc_finish_request(fp);
/*
* rectify count and signal any waiters
*/
mutex_enter(&fc_open_lock);
st->state = FC_STATE_INACTIVE;
st->req = 0;
FC_DEBUG2(9, CE_CONT, "fc_close: open count = %d (%d)\n",
fc_open_count, m + 1);
if (fc_open_count >= fc_max_opens) {
cv_broadcast(&fc_open_cv);
}
fc_open_count--;
mutex_exit(&fc_open_lock);
return (0);
}
/*ARGSUSED*/
static int
fc_read(dev_t dev, struct uio *uio, cred_t *cred)
{
struct fc_state *st;
int m = (int)getminor(dev) - 1;
struct fc_request *fp;
st = fc_states + m;
ASSERT(m < fc_max_opens && FC_STATE_ACTIVE(st->state));
/*
* Wait for a internal request for the interpreter
* and sleep till one arrives. When one arrives,
* return from the read. (No data is actually returned).
*/
if (st->state != FC_STATE_OPEN) {
cmn_err(CE_CONT, "fc_read: Wrong state (%d) for read\n",
st->state);
return (EINVAL);
}
/*
* Wait for a request, allowing the wait to be interrupted.
*/
if ((fp = fc_get_request()) == NULL)
return (EINTR);
FC_DEBUG1(3, CE_CONT, "fc_read: request fp: %p\n", fp);
/*
* Update our state and store the request pointer.
*/
mutex_enter(&fc_open_lock);
st->req = fp;
st->state = FC_STATE_READ_DONE;
mutex_exit(&fc_open_lock);
return (0);
}
/*ARGSUSED*/
static int
fc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
{
struct fc_state *st;
int m = (int)getminor(dev) - 1;
if (m >= fc_max_opens) {
return (EINVAL);
}
st = fc_states + m;
ASSERT(FC_STATE_ACTIVE(st->state));
switch (cmd) {
case FC_GET_PARAMETERS:
/*
* This should be the first command and is used to
* return data about the request, including the
* the fcode address and size and the unit address
* of the new child. The fcode offset,size can later
* be used as an offset in an mmap request to allow
* the fcode to be mapped in.
*/
return (fc_get_parameters(dev, arg, mode, credp, rvalp));
case FC_GET_MY_ARGS:
/*
* Get the inital setting of my-args. This should be done
* after FC_GET_PARAMETERS.
*/
return (fc_get_my_args(dev, arg, mode, credp, rvalp));
case FC_RUN_PRIV:
/*
* Run a priveledged op on behalf of the interpreter,
* or download device tree data from the interpreter.
*/
return (fc_run_priv(dev, arg, mode, credp, rvalp));
case FC_VALIDATE:
/*
* The interpreter is done, mark state as done, validating
* the data downloaded into the kernel.
*/
return (fc_validate(dev, arg, mode, credp, rvalp));
case FC_GET_FCODE_DATA:
/*
* Copy out device fcode to user buffer.
*/
return (fc_get_fcode(dev, arg, mode, credp, rvalp));
case FC_SET_FCODE_ERROR:
/*
* Copy in interpreter error status
*/
return (fc_set_fcode_error(dev, arg, mode, credp, rvalp));
}
/*
* Invalid ioctl command
*/
return (ENOTTY);
}
/*
* fc_get_parameters: Get information about the current request.
* The input 'arg' is a pointer to 'struct fc_parameters' which
* we write back to the caller with the information from the req
* structure.
*/
/*ARGSUSED*/
static int
fc_get_parameters(dev_t dev, intptr_t arg, int mode, cred_t *credp, int *rvalp)
{
struct fc_state *st;
int m = (int)getminor(dev) - 1;
fco_handle_t rp;
struct fc_parameters *fcp;
st = fc_states + m;
ASSERT(m < fc_max_opens && FC_STATE_ACTIVE(st->state));
/*
* It's an error if we're not in state FC_STATE_READ_DONE
*/
if (st->state != FC_STATE_READ_DONE) {
cmn_err(CE_CONT, "fc_ioctl: fc_get_parameters: "
"wrong state (%d)\n", st->state);
return (EINVAL);
}
ASSERT(st->req != NULL);
rp = st->req->handle;
FC_DEBUG1(3, CE_CONT, "fc_ioctl: fc_get_parameters fp: %p\n", st->req);
/*
* Create and copyout the attachment point ihandle,
* the fcode kaddr,len and the unit address.
* Note how we treat ihandles and phandles (they are the same thing
* only accross this interface ... a dev_info_t *.)
*/
fcp = kmem_zalloc(sizeof (struct fc_parameters), KM_SLEEP);
fcp->fcode_size = rp->fcode_size;
(void) strncpy(fcp->unit_address, rp->unit_address,
sizeof (fcp->unit_address) - 1);
/*
* XXX - APA This needs to be made more bus independant.
*/
if (rp->bus_args) {
bcopy(rp->bus_args, &fcp->config_address, sizeof (int));
FC_DEBUG1(3, CE_CONT, "fc_ioctl: config_address=%x\n",
fcp->config_address);
} else {
FC_DEBUG0(3, CE_CONT, "fc_ioctl: fc_get_parameters "
"There are no bus specific arguments\n");
}
if (copyout(fcp, (void *)arg, sizeof (struct fc_parameters)) == -1) {
kmem_free(fcp, sizeof (struct fc_parameters));
return (EFAULT);
}
kmem_free(fcp, sizeof (struct fc_parameters));
/*
* Update our state
*/
mutex_enter(&fc_open_lock);
st->state = FC_STATE_IN_PROGRESS;
mutex_exit(&fc_open_lock);
return (0);
}
/*
* fc_get_my_args: Get the initial setting for my-args.
* The input 'arg' is a pointer where the my-arg string is written
* to. The string is NULL terminated.
*/
/*ARGSUSED*/
static int
fc_get_my_args(dev_t dev, intptr_t arg, int mode, cred_t *credp, int *rvalp)
{
struct fc_state *st;
int m = (int)getminor(dev) - 1;
fco_handle_t rp;
st = fc_states + m;
ASSERT(m < fc_max_opens && FC_STATE_ACTIVE(st->state));
/*
* It's an error if we're not in state FC_STATE_READ_DONE
*/
if (st->state != FC_STATE_IN_PROGRESS) {
cmn_err(CE_CONT, "fc_ioctl: fc_get_my_args: "
"wrong state (%d)\n", st->state);
return (EINVAL);
}
ASSERT(st->req != NULL);
rp = st->req->handle;
FC_DEBUG1(3, CE_CONT, "fc_ioctl: fc_get_my_args fp: %p\n", st->req);
if (rp->my_args == NULL) {
FC_DEBUG0(3, CE_CONT, "fc_ioctl: fc_get_my_args "
"There are no bus specific my-args\n");
return (EINVAL);
}
if (strlen(rp->my_args) > FC_GET_MY_ARGS_BUFLEN) {
FC_DEBUG1(3, CE_CONT, "fc_ioctl: fc_get_my_args "
"my-args is larger than %d\n", FC_GET_MY_ARGS_BUFLEN);
return (EINVAL);
}
if (copyout(rp->my_args, (void *)arg, strlen(rp->my_args) + 1) == -1) {
return (EFAULT);
}
return (0);
}
/*ARGSUSED*/
static int
fc_run_priv(dev_t dev, intptr_t arg, int mode, cred_t *credp, int *rvalp)
{
struct fc_state *st;
int m = (int)getminor(dev) - 1;
struct fc_request *fp;
struct fc_client_interface tc, *cp, *ap;
size_t csize;
int nresults, nargs, error;
char *name;
ap = (struct fc_client_interface *)arg;
st = fc_states + m;
ASSERT(m < fc_max_opens && FC_STATE_ACTIVE(st->state));
/*
* It's an error if we're not in state FC_STATE_IN_PROGRESS
*/
if (st->state != FC_STATE_IN_PROGRESS) {
cmn_err(CE_CONT, "fc_ioctl: fc_run_priv: wrong state (%d)\n",
st->state);
return (EINVAL);
}
/*
* Get the first three cells to figure out how large the buffer
* needs to be; allocate it and copy it in. The array is variable
* sized based on the fixed portion plus the given number of arg.
* cells and given number of result cells.
*/
if (copyin((void *)arg, &tc, 3 * sizeof (fc_cell_t))) {
FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_run_priv "
"fault copying in first 2 cells from %p\n", arg);
return (EFAULT);
}
/*
* XXX We should probably limit #args and #results to something
* reasonable without blindly copying it in.
*/
nresults = fc_cell2int(tc.nresults); /* save me for later */
nargs = fc_cell2int(tc.nargs);
csize = (FCC_FIXED_CELLS + nargs + nresults) * sizeof (fc_cell_t);
cp = kmem_zalloc(csize, KM_SLEEP);
/*
* Don't bother copying in the result cells
*/
if (copyin((void *)arg, cp, csize - (nresults * sizeof (fc_cell_t)))) {
FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_run_priv "
"fault copying in argument array from %p\n", arg);
kmem_free(cp, csize);
return (EFAULT);
}
/*
* reset the error fields.
*/
cp->error = fc_int2cell(0);
cp->priv_error = fc_int2cell(0);
/*
* Copy in the service name into our copy of the array.
* Later, be careful not to copy out the svc name pointer.
*/
name = kmem_zalloc(FC_SVC_NAME_LEN, KM_SLEEP);
if (copyinstr(fc_cell2ptr(cp->svc_name), name,
FC_SVC_NAME_LEN - 1, NULL)) {
FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_run_priv "
"fault copying in service name from %p\n",
fc_cell2ptr(cp->svc_name));
kmem_free(cp, csize);
kmem_free(name, FC_SVC_NAME_LEN);
return (EFAULT);
}
cp->svc_name = fc_ptr2cell(name);
FC_DEBUG3(7, CE_CONT, "fc_ioctl: fc_run_priv: "
"service name <%s> nargs %d nresults %d\n",
name, fc_cell2int(cp->nargs), fc_cell2int(cp->nresults));
/*
* Call the driver's ops function to provide the service
*/
fp = st->req;
ASSERT(fp->ap_ops);
error = fp->ap_ops(fp->ap_dip, fp->handle, cp);
/*
* If error is non-zero, we need to log the error and
* the service name, and write back the error to the
* callers argument array.
*/
if (error || cp->error) {
FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_run_priv: "
"service name <%s> was unserviced\n", name);
cp->error = FC_ERR_SVC_NAME;
cp->nresults = fc_int2cell(0);
error = copyout(&cp->error, &ap->error, sizeof (fc_cell_t));
error |= copyout(&cp->nresults, &ap->nresults,
sizeof (fc_cell_t));
kmem_free(cp, csize);
kmem_free(name, FC_SVC_NAME_LEN);
if (error) {
FC_DEBUG0(1, CE_CONT, "fc_ioctl: fc_run_priv "
"fault copying out error result\n");
return (EFAULT);
}
return (0);
}
if (cp->priv_error) {
FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_run_priv: "
"service name <%s> caused a priv violation\n", name);
cp->priv_error = FC_PRIV_ERROR;
cp->nresults = fc_int2cell(0);
error = copyout(&cp->error, &ap->error, sizeof (fc_cell_t));
error |= copyout(&cp->priv_error, &ap->priv_error,
sizeof (fc_cell_t));
error |= copyout(&cp->nresults, &ap->nresults,
sizeof (fc_cell_t));
kmem_free(cp, csize);
kmem_free(name, FC_SVC_NAME_LEN);
if (error) {
FC_DEBUG0(1, CE_CONT, "fc_ioctl: fc_run_priv "
"fault copying out priv error result\n");
return (EFAULT);
}
return (0);
}
/*
* We believe we have a successful result at this point, thus we
* have to copy out the actual number of result cells to be
* returned, the two error fields and each of the results.
*/
if (fc_cell2int(cp->nresults) > nresults)
cmn_err(CE_PANIC, "fc_ioctl: fc_run_priv: "
"results (from ops function) overflow\n");
error = copyout(&cp->nresults, &ap->nresults, sizeof (fc_cell_t));
error |= copyout(&cp->error, &ap->error, sizeof (fc_cell_t));
error |= copyout(&cp->priv_error, &ap->priv_error, sizeof (fc_cell_t));
if ((error == 0) && cp->nresults)
error |= copyout(&fc_result(cp, 0), &(ap->v[nargs]),
cp->nresults * sizeof (fc_cell_t));
kmem_free(cp, csize);
kmem_free(name, FC_SVC_NAME_LEN);
if (error) {
FC_DEBUG0(1, CE_CONT, "fc_ioctl: fc_run_priv "
"fault copying out (good) results\n");
return (EFAULT);
}
return (0);
}
/*ARGSUSED*/
static int
fc_validate(dev_t dev, intptr_t arg, int mode, cred_t *credp, int *rvalp)
{
struct fc_state *st;
int m = (int)getminor(dev) - 1;
struct fc_request *fp;
struct fc_client_interface *cp;
st = fc_states + m;
ASSERT(m < fc_max_opens && FC_STATE_ACTIVE(st->state));
/*
* It's an error if we're not in state FC_STATE_IN_PROGRESS
*/
if (st->state != FC_STATE_IN_PROGRESS) {
cmn_err(CE_CONT, "fc_ioctl: fc_validate: wrong state (%d)\n",
st->state);
return (EINVAL);
}
FC_DEBUG0(2, CE_CONT, "fc_ioctl: fc_validate: Sending validate cmd\n");
/*
* Send a "validate" command down the line.
* The command has no arguments and no results.
*/
cp = kmem_zalloc(sizeof (struct fc_client_interface), KM_SLEEP);
cp->svc_name = fc_ptr2cell(FC_SVC_VALIDATE);
fp = st->req;
ASSERT(fp->ap_ops);
(void) fp->ap_ops(fp->ap_dip, fp->handle, cp);
kmem_free(cp, sizeof (struct fc_client_interface));
/*
* Update our state.
*/
mutex_enter(&fc_open_lock);
st->state = FC_STATE_VALIDATED;
mutex_exit(&fc_open_lock);
return (0);
}
/*
* fc_get_fcode: Copy out device fcode to user buffer.
* The input 'arg' is a pointer to 'fc_fcode_info_t' which
* should have fcode_size field set. The fcode_ptr field is a
* pointer to a user buffer of fcode_size.
*/
/*ARGSUSED*/
static int
fc_get_fcode(dev_t dev, intptr_t arg, int mode, cred_t *credp, int *rvalp)
{
struct fc_state *st;
int m = (int)getminor(dev) - 1;
fco_handle_t rp;
struct fc_fcode_info fcode_info;
st = fc_states + m;
ASSERT(m < fc_max_opens && FC_STATE_ACTIVE(st->state));
ASSERT(st->req != NULL);
rp = st->req->handle;
FC_DEBUG1(3, CE_CONT, "fc_ioctl: fc_get_fcode fp: %p\n", st->req);
/*
* Get the fc_fcode_info structure from userland.
*/
if (copyin((void *)arg, &fcode_info, sizeof (fc_fcode_info_t))) {
FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_get_fcode "
"fault copying in fcode_info from %p\n", arg);
return (EFAULT);
}
/*
* Validate that buffer size is what we expect.
*/
if (fcode_info.fcode_size != rp->fcode_size) {
FC_DEBUG2(1, CE_CONT, "fc_ioctl: fc_get_fcode "
"requested size (0x%x) doesn't match real size (0x%x)\n",
fcode_info.fcode_size, rp->fcode_size);
return (EINVAL);
}
/*
* Copyout the fcode.
*/
if (copyout(rp->fcode, fcode_info.fcode_ptr, rp->fcode_size) == -1) {
FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_get_fcode "
"fault copying out fcode to %p\n", fcode_info.fcode_ptr);
return (EFAULT);
}
return (0);
}
/*
* fc_set_fcode_error: Copy in fcode error.
* The input 'arg' is a pointer to int which
* should have the appropriate error code set.
*/
/*ARGSUSED*/
static int
fc_set_fcode_error(dev_t dev, intptr_t arg, int mode, cred_t *credp, int *rvalp)
{
struct fc_state *st;
struct fc_request *fp;
int m = (int)getminor(dev) - 1;
int status;
st = fc_states + m;
ASSERT(m < fc_max_opens && FC_STATE_ACTIVE(st->state));
ASSERT(st->req != NULL);
fp = st->req;
FC_DEBUG1(3, CE_CONT, "fc_ioctl: fc_set_fcode_error fp: %p\n", fp);
/*
* Get the error code from userland.
* We expect these to be negative values to denote
* interpreter errors.
*/
if (copyin((void *)arg, &status, sizeof (int))) {
FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_set_fcode_error "
"fault copying in status from %p\n", arg);
return (EFAULT);
}
if (!FC_ERROR_VALID(status)) {
FC_DEBUG1(1, CE_CONT, "fc_ioctl: fc_set_fcode_error "
"invalid error code specified %i\n", status);
return (EINVAL);
}
fp->error = status;
mutex_enter(&fc_open_lock);
st->state = FC_STATE_ERROR_SET;
mutex_exit(&fc_open_lock);
return (0);
}
|