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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
|
/*
* Copyright (c) 2003
* Bill Paul <wpaul@windriver.com>. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Bill Paul.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/unistd.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/callout.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <sys/sysctl.h>
#include <sys/proc.h>
#include <sys/malloc.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <vm/uma.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/ethernet.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_ioctl.h>
#include <compat/ndis/pe_var.h>
#include <compat/ndis/resource_var.h>
#include <compat/ndis/ntoskrnl_var.h>
#include <compat/ndis/ndis_var.h>
#include <compat/ndis/hal_var.h>
#include <compat/ndis/cfg_var.h>
#include <dev/if_ndis/if_ndisvar.h>
#define NDIS_DUMMY_PATH "\\\\some\\bogus\\path"
__stdcall static void ndis_status_func(ndis_handle, ndis_status,
void *, uint32_t);
__stdcall static void ndis_statusdone_func(ndis_handle);
__stdcall static void ndis_setdone_func(ndis_handle, ndis_status);
__stdcall static void ndis_getdone_func(ndis_handle, ndis_status);
__stdcall static void ndis_resetdone_func(ndis_handle, ndis_status, uint8_t);
__stdcall static void ndis_sendrsrcavail_func(ndis_handle);
struct nd_head ndis_devhead;
struct ndis_req {
void (*nr_func)(void *);
void *nr_arg;
int nr_exit;
STAILQ_ENTRY(ndis_req) link;
};
struct ndisproc {
struct ndisqhead *np_q;
struct proc *np_p;
int np_state;
};
static int ndis_create_kthreads(void);
static void ndis_destroy_kthreads(void);
static void ndis_stop_thread(int);
static int ndis_enlarge_thrqueue(int);
static int ndis_shrink_thrqueue(int);
static void ndis_runq(void *);
extern struct mtx_pool *ndis_mtxpool;
static uma_zone_t ndis_packet_zone, ndis_buffer_zone;
struct mtx *ndis_thr_mtx;
static STAILQ_HEAD(ndisqhead, ndis_req) ndis_ttodo;
struct ndisqhead ndis_itodo;
struct ndisqhead ndis_free;
static int ndis_jobs = 32;
static struct ndisproc ndis_tproc;
static struct ndisproc ndis_iproc;
/*
* This allows us to export our symbols to other modules.
* Note that we call ourselves 'ndisapi' to avoid a namespace
* collision with if_ndis.ko, which internally calls itself
* 'ndis.'
*/
static int
ndis_modevent(module_t mod, int cmd, void *arg)
{
int error = 0;
switch (cmd) {
case MOD_LOAD:
/* Initialize subsystems */
ndis_libinit();
ntoskrnl_libinit();
/* Initialize TX buffer UMA zone. */
ndis_packet_zone = uma_zcreate("NDIS packet",
sizeof(ndis_packet), NULL, NULL, NULL,
NULL, UMA_ALIGN_PTR, 0);
ndis_buffer_zone = uma_zcreate("NDIS buffer",
sizeof(ndis_buffer), NULL, NULL, NULL,
NULL, UMA_ALIGN_PTR, 0);
ndis_create_kthreads();
TAILQ_INIT(&ndis_devhead);
break;
case MOD_SHUTDOWN:
/* stop kthreads */
ndis_destroy_kthreads();
if (TAILQ_FIRST(&ndis_devhead) == NULL) {
/* Shut down subsystems */
ndis_libfini();
ntoskrnl_libfini();
/* Remove zones */
uma_zdestroy(ndis_packet_zone);
uma_zdestroy(ndis_buffer_zone);
}
break;
case MOD_UNLOAD:
/* stop kthreads */
ndis_destroy_kthreads();
/* Shut down subsystems */
ndis_libfini();
ntoskrnl_libfini();
/* Remove zones */
uma_zdestroy(ndis_packet_zone);
uma_zdestroy(ndis_buffer_zone);
break;
default:
error = EINVAL;
break;
}
return(error);
}
DEV_MODULE(ndisapi, ndis_modevent, NULL);
MODULE_VERSION(ndisapi, 1);
/*
* We create two kthreads for the NDIS subsystem. One of them is a task
* queue for performing various odd jobs. The other is an swi thread
* reserved exclusively for running interrupt handlers. The reason we
* have our own task queue is that there are some cases where we may
* need to sleep for a significant amount of time, and if we were to
* use one of the taskqueue threads, we might delay the processing
* of other pending tasks which might need to run right away. We have
* a separate swi thread because we don't want our interrupt handling
* to be delayed either.
*
* By default there are 32 jobs available to start, and another 8
* are added to the free list each time a new device is created.
*/
static void
ndis_runq(arg)
void *arg;
{
struct ndis_req *r = NULL, *die = NULL;
struct ndisproc *p;
p = arg;
while (1) {
kthread_suspend(p->np_p, 0);
/* Look for any jobs on the work queue. */
mtx_pool_lock(ndis_mtxpool, ndis_thr_mtx);
p->np_state = NDIS_PSTATE_RUNNING;
while(STAILQ_FIRST(p->np_q) != NULL) {
r = STAILQ_FIRST(p->np_q);
STAILQ_REMOVE_HEAD(p->np_q, link);
mtx_pool_unlock(ndis_mtxpool, ndis_thr_mtx);
/* Do the work. */
if (r->nr_func != NULL)
(*r->nr_func)(r->nr_arg);
mtx_pool_lock(ndis_mtxpool, ndis_thr_mtx);
STAILQ_INSERT_HEAD(&ndis_free, r, link);
/* Check for a shutdown request */
if (r->nr_exit == TRUE)
die = r;
}
p->np_state = NDIS_PSTATE_SLEEPING;
mtx_pool_unlock(ndis_mtxpool, ndis_thr_mtx);
/* Bail if we were told to shut down. */
if (die != NULL)
break;
}
wakeup(die);
kthread_exit(0);
}
static int
ndis_create_kthreads()
{
struct ndis_req *r;
int i, error = 0;
ndis_thr_mtx = mtx_pool_alloc(ndis_mtxpool);
STAILQ_INIT(&ndis_ttodo);
STAILQ_INIT(&ndis_itodo);
STAILQ_INIT(&ndis_free);
for (i = 0; i < ndis_jobs; i++) {
r = malloc(sizeof(struct ndis_req), M_DEVBUF, M_WAITOK);
if (r == NULL) {
error = ENOMEM;
break;
}
STAILQ_INSERT_HEAD(&ndis_free, r, link);
}
if (error == 0) {
ndis_tproc.np_q = &ndis_ttodo;
ndis_tproc.np_state = NDIS_PSTATE_SLEEPING;
error = kthread_create(ndis_runq, &ndis_tproc,
&ndis_tproc.np_p, RFHIGHPID,
NDIS_KSTACK_PAGES, "ndis taskqueue");
}
if (error == 0) {
ndis_iproc.np_q = &ndis_itodo;
ndis_iproc.np_state = NDIS_PSTATE_SLEEPING;
error = kthread_create(ndis_runq, &ndis_iproc,
&ndis_iproc.np_p, RFHIGHPID,
NDIS_KSTACK_PAGES, "ndis swi");
}
if (error) {
while ((r = STAILQ_FIRST(&ndis_free)) != NULL) {
STAILQ_REMOVE_HEAD(&ndis_free, link);
free(r, M_DEVBUF);
}
return(error);
}
return(0);
}
static void
ndis_destroy_kthreads()
{
struct ndis_req *r;
/* Stop the threads. */
ndis_stop_thread(NDIS_TASKQUEUE);
ndis_stop_thread(NDIS_SWI);
/* Destroy request structures. */
while ((r = STAILQ_FIRST(&ndis_free)) != NULL) {
STAILQ_REMOVE_HEAD(&ndis_free, link);
free(r, M_DEVBUF);
}
return;
}
static void
ndis_stop_thread(t)
int t;
{
struct ndis_req *r;
struct ndisqhead *q;
struct proc *p;
if (t == NDIS_TASKQUEUE) {
q = &ndis_ttodo;
p = ndis_tproc.np_p;
} else {
q = &ndis_itodo;
p = ndis_iproc.np_p;
}
/* Create and post a special 'exit' job. */
mtx_pool_lock(ndis_mtxpool, ndis_thr_mtx);
r = STAILQ_FIRST(&ndis_free);
STAILQ_REMOVE_HEAD(&ndis_free, link);
r->nr_func = NULL;
r->nr_arg = NULL;
r->nr_exit = TRUE;
STAILQ_INSERT_TAIL(q, r, link);
mtx_pool_unlock(ndis_mtxpool, ndis_thr_mtx);
kthread_resume(p);
/* wait for thread exit */
tsleep(r, PPAUSE|PCATCH, "ndisthrexit", hz * 60);
/* Now empty the job list. */
mtx_pool_lock(ndis_mtxpool, ndis_thr_mtx);
while ((r = STAILQ_FIRST(q)) != NULL) {
STAILQ_REMOVE_HEAD(q, link);
STAILQ_INSERT_HEAD(&ndis_free, r, link);
}
mtx_pool_unlock(ndis_mtxpool, ndis_thr_mtx);
return;
}
static int
ndis_enlarge_thrqueue(cnt)
int cnt;
{
struct ndis_req *r;
int i;
for (i = 0; i < cnt; i++) {
r = malloc(sizeof(struct ndis_req), M_DEVBUF, M_WAITOK);
if (r == NULL)
return(ENOMEM);
mtx_pool_lock(ndis_mtxpool, ndis_thr_mtx);
STAILQ_INSERT_HEAD(&ndis_free, r, link);
ndis_jobs++;
mtx_pool_unlock(ndis_mtxpool, ndis_thr_mtx);
}
return(0);
}
static int
ndis_shrink_thrqueue(cnt)
int cnt;
{
struct ndis_req *r;
int i;
for (i = 0; i < cnt; i++) {
mtx_pool_lock(ndis_mtxpool, ndis_thr_mtx);
r = STAILQ_FIRST(&ndis_free);
if (r == NULL) {
mtx_pool_unlock(ndis_mtxpool, ndis_thr_mtx);
return(ENOMEM);
}
STAILQ_REMOVE_HEAD(&ndis_free, link);
ndis_jobs--;
mtx_pool_unlock(ndis_mtxpool, ndis_thr_mtx);
free(r, M_DEVBUF);
}
return(0);
}
int
ndis_unsched(func, arg, t)
void (*func)(void *);
void *arg;
int t;
{
struct ndis_req *r;
struct ndisqhead *q;
struct proc *p;
if (t == NDIS_TASKQUEUE) {
q = &ndis_ttodo;
p = ndis_tproc.np_p;
} else {
q = &ndis_itodo;
p = ndis_iproc.np_p;
}
mtx_pool_lock(ndis_mtxpool, ndis_thr_mtx);
STAILQ_FOREACH(r, q, link) {
if (r->nr_func == func && r->nr_arg == arg) {
STAILQ_REMOVE(q, r, ndis_req, link);
STAILQ_INSERT_HEAD(&ndis_free, r, link);
mtx_pool_unlock(ndis_mtxpool, ndis_thr_mtx);
return(0);
}
}
mtx_pool_unlock(ndis_mtxpool, ndis_thr_mtx);
return(ENOENT);
}
int
ndis_sched(func, arg, t)
void (*func)(void *);
void *arg;
int t;
{
struct ndis_req *r;
struct ndisqhead *q;
struct proc *p;
int s;
if (t == NDIS_TASKQUEUE) {
q = &ndis_ttodo;
p = ndis_tproc.np_p;
} else {
q = &ndis_itodo;
p = ndis_iproc.np_p;
}
mtx_pool_lock(ndis_mtxpool, ndis_thr_mtx);
/*
* Check to see if an instance of this job is already
* pending. If so, don't bother queuing it again.
*/
STAILQ_FOREACH(r, q, link) {
if (r->nr_func == func && r->nr_arg == arg) {
mtx_pool_unlock(ndis_mtxpool, ndis_thr_mtx);
return(0);
}
}
r = STAILQ_FIRST(&ndis_free);
if (r == NULL) {
mtx_pool_unlock(ndis_mtxpool, ndis_thr_mtx);
return(EAGAIN);
}
STAILQ_REMOVE_HEAD(&ndis_free, link);
r->nr_func = func;
r->nr_arg = arg;
r->nr_exit = FALSE;
STAILQ_INSERT_TAIL(q, r, link);
if (t == NDIS_TASKQUEUE)
s = ndis_tproc.np_state;
else
s = ndis_iproc.np_state;
mtx_pool_unlock(ndis_mtxpool, ndis_thr_mtx);
/*
* Post the job, but only if the thread is actually blocked
* on its own suspend call. If a driver queues up a job with
* NdisScheduleWorkItem() which happens to do a KeWaitForObject(),
* it may suspend there, and in that case we don't want to wake
* it up until KeWaitForObject() gets woken up on its own.
*/
if (s == NDIS_PSTATE_SLEEPING)
kthread_resume(p);
return(0);
}
__stdcall static void
ndis_sendrsrcavail_func(adapter)
ndis_handle adapter;
{
return;
}
__stdcall static void
ndis_status_func(adapter, status, sbuf, slen)
ndis_handle adapter;
ndis_status status;
void *sbuf;
uint32_t slen;
{
ndis_miniport_block *block;
block = adapter;
if (block->nmb_ifp->if_flags & IFF_DEBUG)
device_printf (block->nmb_dev, "status: %x\n", status);
return;
}
__stdcall static void
ndis_statusdone_func(adapter)
ndis_handle adapter;
{
ndis_miniport_block *block;
block = adapter;
if (block->nmb_ifp->if_flags & IFF_DEBUG)
device_printf (block->nmb_dev, "status complete\n");
return;
}
__stdcall static void
ndis_setdone_func(adapter, status)
ndis_handle adapter;
ndis_status status;
{
ndis_miniport_block *block;
block = adapter;
block->nmb_setstat = status;
wakeup(&block->nmb_wkupdpctimer);
return;
}
__stdcall static void
ndis_getdone_func(adapter, status)
ndis_handle adapter;
ndis_status status;
{
ndis_miniport_block *block;
block = adapter;
block->nmb_getstat = status;
wakeup(&block->nmb_wkupdpctimer);
return;
}
__stdcall static void
ndis_resetdone_func(adapter, status, addressingreset)
ndis_handle adapter;
ndis_status status;
uint8_t addressingreset;
{
ndis_miniport_block *block;
block = adapter;
if (block->nmb_ifp->if_flags & IFF_DEBUG)
device_printf (block->nmb_dev, "reset done...\n");
wakeup(block->nmb_ifp);
return;
}
int
ndis_create_sysctls(arg)
void *arg;
{
struct ndis_softc *sc;
ndis_cfg *vals;
char buf[256];
if (arg == NULL)
return(EINVAL);
sc = arg;
vals = sc->ndis_regvals;
TAILQ_INIT(&sc->ndis_cfglist_head);
/* Create the sysctl tree. */
sc->ndis_tree = SYSCTL_ADD_NODE(&sc->ndis_ctx,
SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
device_get_nameunit(sc->ndis_dev), CTLFLAG_RD, 0,
device_get_desc(sc->ndis_dev));
/* Add the driver-specific registry keys. */
vals = sc->ndis_regvals;
while(1) {
if (vals->nc_cfgkey == NULL)
break;
if (vals->nc_idx != sc->ndis_devidx) {
vals++;
continue;
}
SYSCTL_ADD_STRING(&sc->ndis_ctx,
SYSCTL_CHILDREN(sc->ndis_tree),
OID_AUTO, vals->nc_cfgkey,
CTLFLAG_RW, vals->nc_val,
sizeof(vals->nc_val),
vals->nc_cfgdesc);
vals++;
}
/* Now add a couple of builtin keys. */
/*
* Environment can be either Windows (0) or WindowsNT (1).
* We qualify as the latter.
*/
ndis_add_sysctl(sc, "Environment",
"Windows environment", "1", CTLFLAG_RD);
/* NDIS version should be 5.1. */
ndis_add_sysctl(sc, "NdisVersion",
"NDIS API Version", "0x00050001", CTLFLAG_RD);
/* Bus type (PCI, PCMCIA, etc...) */
sprintf(buf, "%d", (int)sc->ndis_iftype);
ndis_add_sysctl(sc, "BusType", "Bus Type", buf, CTLFLAG_RD);
if (sc->ndis_res_io != NULL) {
sprintf(buf, "0x%lx", rman_get_start(sc->ndis_res_io));
ndis_add_sysctl(sc, "IOBaseAddress",
"Base I/O Address", buf, CTLFLAG_RD);
}
if (sc->ndis_irq != NULL) {
sprintf(buf, "%lu", rman_get_start(sc->ndis_irq));
ndis_add_sysctl(sc, "InterruptNumber",
"Interrupt Number", buf, CTLFLAG_RD);
}
return(0);
}
int
ndis_add_sysctl(arg, key, desc, val, flag)
void *arg;
char *key;
char *desc;
char *val;
int flag;
{
struct ndis_softc *sc;
struct ndis_cfglist *cfg;
char descstr[256];
sc = arg;
cfg = malloc(sizeof(struct ndis_cfglist), M_DEVBUF, M_NOWAIT|M_ZERO);
if (cfg == NULL)
return(ENOMEM);
cfg->ndis_cfg.nc_cfgkey = strdup(key, M_DEVBUF);
if (desc == NULL) {
snprintf(descstr, sizeof(descstr), "%s (dynamic)", key);
cfg->ndis_cfg.nc_cfgdesc = strdup(descstr, M_DEVBUF);
} else
cfg->ndis_cfg.nc_cfgdesc = strdup(desc, M_DEVBUF);
strcpy(cfg->ndis_cfg.nc_val, val);
TAILQ_INSERT_TAIL(&sc->ndis_cfglist_head, cfg, link);
SYSCTL_ADD_STRING(&sc->ndis_ctx, SYSCTL_CHILDREN(sc->ndis_tree),
OID_AUTO, cfg->ndis_cfg.nc_cfgkey, flag,
cfg->ndis_cfg.nc_val, sizeof(cfg->ndis_cfg.nc_val),
cfg->ndis_cfg.nc_cfgdesc);
return(0);
}
int
ndis_flush_sysctls(arg)
void *arg;
{
struct ndis_softc *sc;
struct ndis_cfglist *cfg;
sc = arg;
while (!TAILQ_EMPTY(&sc->ndis_cfglist_head)) {
cfg = TAILQ_FIRST(&sc->ndis_cfglist_head);
TAILQ_REMOVE(&sc->ndis_cfglist_head, cfg, link);
free(cfg->ndis_cfg.nc_cfgkey, M_DEVBUF);
free(cfg->ndis_cfg.nc_cfgdesc, M_DEVBUF);
free(cfg, M_DEVBUF);
}
return(0);
}
void
ndis_return_packet(buf, arg)
void *buf; /* not used */
void *arg;
{
struct ndis_softc *sc;
ndis_handle adapter;
ndis_packet *p;
__stdcall ndis_return_handler returnfunc;
if (arg == NULL)
return;
p = arg;
/* Decrement refcount. */
p->np_refcnt--;
/* Release packet when refcount hits zero, otherwise return. */
if (p->np_refcnt)
return;
sc = p->np_softc;
returnfunc = sc->ndis_chars.nmc_return_packet_func;
adapter = sc->ndis_block.nmb_miniportadapterctx;
if (adapter != NULL)
returnfunc(adapter, p);
return;
}
void
ndis_free_bufs(b0)
ndis_buffer *b0;
{
ndis_buffer *next;
if (b0 == NULL)
return;
while(b0 != NULL) {
next = b0->nb_next;
uma_zfree (ndis_buffer_zone, b0);
b0 = next;
}
return;
}
void
ndis_free_packet(p)
ndis_packet *p;
{
if (p == NULL)
return;
ndis_free_bufs(p->np_private.npp_head);
uma_zfree(ndis_packet_zone, p);
return;
}
int
ndis_convert_res(arg)
void *arg;
{
struct ndis_softc *sc;
ndis_resource_list *rl = NULL;
cm_partial_resource_desc *prd = NULL;
ndis_miniport_block *block;
device_t dev;
struct resource_list *brl;
struct resource_list brl_rev;
struct resource_list_entry *brle, *n;
int error = 0;
sc = arg;
block = &sc->ndis_block;
dev = sc->ndis_dev;
SLIST_INIT(&brl_rev);
rl = malloc(sizeof(ndis_resource_list) +
(sizeof(cm_partial_resource_desc) * (sc->ndis_rescnt - 1)),
M_DEVBUF, M_NOWAIT|M_ZERO);
if (rl == NULL)
return(ENOMEM);
rl->cprl_version = 5;
rl->cprl_version = 1;
rl->cprl_count = sc->ndis_rescnt;
prd = rl->cprl_partial_descs;
brl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
if (brl != NULL) {
/*
* We have a small problem. Some PCI devices have
* multiple I/O ranges. Windows orders them starting
* from lowest numbered BAR to highest. We discover
* them in that order too, but insert them into a singly
* linked list head first, which means when time comes
* to traverse the list, we enumerate them in reverse
* order. This screws up some drivers which expect the
* BARs to be in ascending order so that they can choose
* the "first" one as their register space. Unfortunately,
* in order to fix this, we have to create our own
* temporary list with the entries in reverse order.
*/
SLIST_FOREACH(brle, brl, link) {
n = malloc(sizeof(struct resource_list_entry),
M_TEMP, M_NOWAIT);
if (n == NULL) {
error = ENOMEM;
goto bad;
}
bcopy((char *)brle, (char *)n,
sizeof(struct resource_list_entry));
SLIST_INSERT_HEAD(&brl_rev, n, link);
}
SLIST_FOREACH(brle, &brl_rev, link) {
switch (brle->type) {
case SYS_RES_IOPORT:
prd->cprd_type = CmResourceTypePort;
prd->cprd_flags = CM_RESOURCE_PORT_IO;
prd->cprd_sharedisp =
CmResourceShareDeviceExclusive;
prd->u.cprd_port.cprd_start.np_quad =
brle->start;
prd->u.cprd_port.cprd_len = brle->count;
break;
case SYS_RES_MEMORY:
prd->cprd_type = CmResourceTypeMemory;
prd->cprd_flags =
CM_RESOURCE_MEMORY_READ_WRITE;
prd->cprd_sharedisp =
CmResourceShareDeviceExclusive;
prd->u.cprd_port.cprd_start.np_quad =
brle->start;
prd->u.cprd_port.cprd_len = brle->count;
break;
case SYS_RES_IRQ:
prd->cprd_type = CmResourceTypeInterrupt;
prd->cprd_flags = 0;
prd->cprd_sharedisp =
CmResourceShareDeviceExclusive;
prd->u.cprd_intr.cprd_level = brle->start;
prd->u.cprd_intr.cprd_vector = brle->start;
prd->u.cprd_intr.cprd_affinity = 0;
break;
default:
break;
}
prd++;
}
}
block->nmb_rlist = rl;
bad:
while (!SLIST_EMPTY(&brl_rev)) {
n = SLIST_FIRST(&brl_rev);
SLIST_REMOVE_HEAD(&brl_rev, link);
free (n, M_TEMP);
}
return(error);
}
/*
* Map an NDIS packet to an mbuf list. When an NDIS driver receives a
* packet, it will hand it to us in the form of an ndis_packet,
* which we need to convert to an mbuf that is then handed off
* to the stack. Note: we configure the mbuf list so that it uses
* the memory regions specified by the ndis_buffer structures in
* the ndis_packet as external storage. In most cases, this will
* point to a memory region allocated by the driver (either by
* ndis_malloc_withtag() or ndis_alloc_sharedmem()). We expect
* the driver to handle free()ing this region for is, so we set up
* a dummy no-op free handler for it.
*/
int
ndis_ptom(m0, p)
struct mbuf **m0;
ndis_packet *p;
{
struct mbuf *m, *prev = NULL;
ndis_buffer *buf;
ndis_packet_private *priv;
uint32_t totlen = 0;
if (p == NULL || m0 == NULL)
return(EINVAL);
priv = &p->np_private;
buf = priv->npp_head;
p->np_refcnt = 0;
for (buf = priv->npp_head; buf != NULL; buf = buf->nb_next) {
if (buf == priv->npp_head)
MGETHDR(m, M_DONTWAIT, MT_HEADER);
else
MGET(m, M_DONTWAIT, MT_DATA);
if (m == NULL) {
m_freem(*m0);
*m0 = NULL;
return(ENOBUFS);
}
m->m_len = buf->nb_bytecount;
m->m_data = MDL_VA(buf);
MEXTADD(m, m->m_data, m->m_len, ndis_return_packet,
p, 0, EXT_NDIS);
p->np_refcnt++;
totlen += m->m_len;
if (m->m_flags & MT_HEADER)
*m0 = m;
else
prev->m_next = m;
prev = m;
}
(*m0)->m_pkthdr.len = totlen;
return(0);
}
/*
* Create an mbuf chain from an NDIS packet chain.
* This is used mainly when transmitting packets, where we need
* to turn an mbuf off an interface's send queue and transform it
* into an NDIS packet which will be fed into the NDIS driver's
* send routine.
*
* NDIS packets consist of two parts: an ndis_packet structure,
* which is vaguely analagous to the pkthdr portion of an mbuf,
* and one or more ndis_buffer structures, which define the
* actual memory segments in which the packet data resides.
* We need to allocate one ndis_buffer for each mbuf in a chain,
* plus one ndis_packet as the header.
*/
int
ndis_mtop(m0, p)
struct mbuf *m0;
ndis_packet **p;
{
struct mbuf *m;
ndis_buffer *buf = NULL, *prev = NULL;
ndis_packet_private *priv;
if (p == NULL || m0 == NULL)
return(EINVAL);
/* If caller didn't supply a packet, make one. */
if (*p == NULL) {
*p = uma_zalloc(ndis_packet_zone, M_NOWAIT|M_ZERO);
if (*p == NULL)
return(ENOMEM);
}
priv = &(*p)->np_private;
priv->npp_totlen = m0->m_pkthdr.len;
priv->npp_packetooboffset = offsetof(ndis_packet, np_oob);
priv->npp_ndispktflags = NDIS_PACKET_ALLOCATED_BY_NDIS;
for (m = m0; m != NULL; m = m->m_next) {
if (m->m_len == 0)
continue;
buf = uma_zalloc(ndis_buffer_zone, M_NOWAIT | M_ZERO);
if (buf == NULL) {
ndis_free_packet(*p);
*p = NULL;
return(ENOMEM);
}
MDL_INIT(buf, m->m_data, m->m_len);
if (priv->npp_head == NULL)
priv->npp_head = buf;
else
prev->nb_next = buf;
prev = buf;
}
priv->npp_tail = buf;
priv->npp_totlen = m0->m_pkthdr.len;
return(0);
}
int
ndis_get_supported_oids(arg, oids, oidcnt)
void *arg;
ndis_oid **oids;
int *oidcnt;
{
int len, rval;
ndis_oid *o;
if (arg == NULL || oids == NULL || oidcnt == NULL)
return(EINVAL);
len = 0;
ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, NULL, &len);
o = malloc(len, M_DEVBUF, M_NOWAIT);
if (o == NULL)
return(ENOMEM);
rval = ndis_get_info(arg, OID_GEN_SUPPORTED_LIST, o, &len);
if (rval) {
free(o, M_DEVBUF);
return(rval);
}
*oids = o;
*oidcnt = len / 4;
return(0);
}
int
ndis_set_info(arg, oid, buf, buflen)
void *arg;
ndis_oid oid;
void *buf;
int *buflen;
{
struct ndis_softc *sc;
ndis_status rval;
ndis_handle adapter;
__stdcall ndis_setinfo_handler setfunc;
uint32_t byteswritten = 0, bytesneeded = 0;
int error;
sc = arg;
NDIS_LOCK(sc);
setfunc = sc->ndis_chars.nmc_setinfo_func;
adapter = sc->ndis_block.nmb_miniportadapterctx;
NDIS_UNLOCK(sc);
if (adapter == NULL || setfunc == NULL)
return(ENXIO);
rval = setfunc(adapter, oid, buf, *buflen,
&byteswritten, &bytesneeded);
if (rval == NDIS_STATUS_PENDING) {
PROC_LOCK(curthread->td_proc);
error = msleep(&sc->ndis_block.nmb_wkupdpctimer,
&curthread->td_proc->p_mtx, PPAUSE|PDROP,
"ndisset", 5 * hz);
rval = sc->ndis_block.nmb_setstat;
}
if (byteswritten)
*buflen = byteswritten;
if (bytesneeded)
*buflen = bytesneeded;
if (rval == NDIS_STATUS_INVALID_LENGTH)
return(ENOSPC);
if (rval == NDIS_STATUS_INVALID_OID)
return(EINVAL);
if (rval == NDIS_STATUS_NOT_SUPPORTED ||
rval == NDIS_STATUS_NOT_ACCEPTED)
return(ENOTSUP);
if (rval != NDIS_STATUS_SUCCESS)
return(ENODEV);
return(0);
}
typedef void (*ndis_senddone_func)(ndis_handle, ndis_packet *, ndis_status);
int
ndis_send_packets(arg, packets, cnt)
void *arg;
ndis_packet **packets;
int cnt;
{
struct ndis_softc *sc;
ndis_handle adapter;
__stdcall ndis_sendmulti_handler sendfunc;
__stdcall ndis_senddone_func senddonefunc;
int i;
ndis_packet *p;
sc = arg;
adapter = sc->ndis_block.nmb_miniportadapterctx;
if (adapter == NULL)
return(ENXIO);
sendfunc = sc->ndis_chars.nmc_sendmulti_func;
senddonefunc = sc->ndis_block.nmb_senddone_func;
sendfunc(adapter, packets, cnt);
for (i = 0; i < cnt; i++) {
p = packets[i];
/*
* Either the driver already handed the packet to
* ndis_txeof() due to a failure, or it wants to keep
* it and release it asynchronously later. Skip to the
* next one.
*/
if (p == NULL || p->np_oob.npo_status == NDIS_STATUS_PENDING)
continue;
senddonefunc(&sc->ndis_block, p, p->np_oob.npo_status);
}
return(0);
}
int
ndis_send_packet(arg, packet)
void *arg;
ndis_packet *packet;
{
struct ndis_softc *sc;
ndis_handle adapter;
ndis_status status;
__stdcall ndis_sendsingle_handler sendfunc;
__stdcall ndis_senddone_func senddonefunc;
sc = arg;
adapter = sc->ndis_block.nmb_miniportadapterctx;
if (adapter == NULL)
return(ENXIO);
sendfunc = sc->ndis_chars.nmc_sendsingle_func;
senddonefunc = sc->ndis_block.nmb_senddone_func;
status = sendfunc(adapter, packet, packet->np_private.npp_flags);
if (status == NDIS_STATUS_PENDING)
return(0);
senddonefunc(&sc->ndis_block, packet, status);
return(0);
}
int
ndis_init_dma(arg)
void *arg;
{
struct ndis_softc *sc;
int i, error;
sc = arg;
sc->ndis_tmaps = malloc(sizeof(bus_dmamap_t) * sc->ndis_maxpkts,
M_DEVBUF, M_NOWAIT|M_ZERO);
if (sc->ndis_tmaps == NULL)
return(ENOMEM);
for (i = 0; i < sc->ndis_maxpkts; i++) {
error = bus_dmamap_create(sc->ndis_ttag, 0,
&sc->ndis_tmaps[i]);
if (error) {
free(sc->ndis_tmaps, M_DEVBUF);
return(ENODEV);
}
}
return(0);
}
int
ndis_destroy_dma(arg)
void *arg;
{
struct ndis_softc *sc;
struct mbuf *m;
ndis_packet *p = NULL;
int i;
sc = arg;
for (i = 0; i < sc->ndis_maxpkts; i++) {
if (sc->ndis_txarray[i] != NULL) {
p = sc->ndis_txarray[i];
m = (struct mbuf *)p->np_rsvd[1];
if (m != NULL)
m_freem(m);
ndis_free_packet(sc->ndis_txarray[i]);
}
bus_dmamap_destroy(sc->ndis_ttag, sc->ndis_tmaps[i]);
}
free(sc->ndis_tmaps, M_DEVBUF);
bus_dma_tag_destroy(sc->ndis_ttag);
return(0);
}
int
ndis_reset_nic(arg)
void *arg;
{
struct ndis_softc *sc;
ndis_handle adapter;
__stdcall ndis_reset_handler resetfunc;
uint8_t addressing_reset;
struct ifnet *ifp;
int rval;
sc = arg;
ifp = &sc->arpcom.ac_if;
NDIS_LOCK(sc);
adapter = sc->ndis_block.nmb_miniportadapterctx;
resetfunc = sc->ndis_chars.nmc_reset_func;
NDIS_UNLOCK(sc);
if (adapter == NULL || resetfunc == NULL)
return(EIO);
rval = resetfunc(&addressing_reset, adapter);
if (rval == NDIS_STATUS_PENDING) {
PROC_LOCK(curthread->td_proc);
msleep(sc, &curthread->td_proc->p_mtx,
PPAUSE|PDROP, "ndisrst", 0);
}
return(0);
}
int
ndis_halt_nic(arg)
void *arg;
{
struct ndis_softc *sc;
ndis_handle adapter;
__stdcall ndis_halt_handler haltfunc;
struct ifnet *ifp;
sc = arg;
ifp = &sc->arpcom.ac_if;
NDIS_LOCK(sc);
adapter = sc->ndis_block.nmb_miniportadapterctx;
if (adapter == NULL) {
NDIS_UNLOCK(sc);
return(EIO);
}
/*
* The adapter context is only valid after the init
* handler has been called, and is invalid once the
* halt handler has been called.
*/
haltfunc = sc->ndis_chars.nmc_halt_func;
NDIS_UNLOCK(sc);
haltfunc(adapter);
NDIS_LOCK(sc);
sc->ndis_block.nmb_miniportadapterctx = NULL;
NDIS_UNLOCK(sc);
return(0);
}
int
ndis_shutdown_nic(arg)
void *arg;
{
struct ndis_softc *sc;
ndis_handle adapter;
__stdcall ndis_shutdown_handler shutdownfunc;
sc = arg;
NDIS_LOCK(sc);
adapter = sc->ndis_block.nmb_miniportadapterctx;
shutdownfunc = sc->ndis_chars.nmc_shutdown_handler;
NDIS_UNLOCK(sc);
if (adapter == NULL || shutdownfunc == NULL)
return(EIO);
if (sc->ndis_chars.nmc_rsvd0 == NULL)
shutdownfunc(adapter);
else
shutdownfunc(sc->ndis_chars.nmc_rsvd0);
ndis_shrink_thrqueue(8);
TAILQ_REMOVE(&ndis_devhead, &sc->ndis_block, link);
return(0);
}
int
ndis_init_nic(arg)
void *arg;
{
struct ndis_softc *sc;
ndis_miniport_block *block;
__stdcall ndis_init_handler initfunc;
ndis_status status, openstatus = 0;
ndis_medium mediumarray[NdisMediumMax];
uint32_t chosenmedium, i;
if (arg == NULL)
return(EINVAL);
sc = arg;
NDIS_LOCK(sc);
block = &sc->ndis_block;
initfunc = sc->ndis_chars.nmc_init_func;
NDIS_UNLOCK(sc);
TAILQ_INIT(&block->nmb_timerlist);
for (i = 0; i < NdisMediumMax; i++)
mediumarray[i] = i;
status = initfunc(&openstatus, &chosenmedium,
mediumarray, NdisMediumMax, block, block);
/*
* If the init fails, blow away the other exported routines
* we obtained from the driver so we can't call them later.
* If the init failed, none of these will work.
*/
if (status != NDIS_STATUS_SUCCESS) {
NDIS_LOCK(sc);
sc->ndis_block.nmb_miniportadapterctx = NULL;
NDIS_UNLOCK(sc);
return(ENXIO);
}
return(0);
}
void
ndis_enable_intr(arg)
void *arg;
{
struct ndis_softc *sc;
ndis_handle adapter;
__stdcall ndis_enable_interrupts_handler intrenbfunc;
sc = arg;
NDIS_LOCK(sc);
adapter = sc->ndis_block.nmb_miniportadapterctx;
intrenbfunc = sc->ndis_chars.nmc_enable_interrupts_func;
NDIS_UNLOCK(sc);
if (adapter == NULL || intrenbfunc == NULL)
return;
intrenbfunc(adapter);
return;
}
void
ndis_disable_intr(arg)
void *arg;
{
struct ndis_softc *sc;
ndis_handle adapter;
__stdcall ndis_disable_interrupts_handler intrdisfunc;
sc = arg;
NDIS_LOCK(sc);
adapter = sc->ndis_block.nmb_miniportadapterctx;
intrdisfunc = sc->ndis_chars.nmc_disable_interrupts_func;
NDIS_UNLOCK(sc);
if (adapter == NULL || intrdisfunc == NULL)
return;
intrdisfunc(adapter);
return;
}
int
ndis_isr(arg, ourintr, callhandler)
void *arg;
int *ourintr;
int *callhandler;
{
struct ndis_softc *sc;
ndis_handle adapter;
__stdcall ndis_isr_handler isrfunc;
uint8_t accepted, queue;
if (arg == NULL || ourintr == NULL || callhandler == NULL)
return(EINVAL);
sc = arg;
NDIS_LOCK(sc);
adapter = sc->ndis_block.nmb_miniportadapterctx;
isrfunc = sc->ndis_chars.nmc_isr_func;
NDIS_UNLOCK(sc);
if (adapter == NULL || isrfunc == NULL)
return(ENXIO);
isrfunc(&accepted, &queue, adapter);
*ourintr = accepted;
*callhandler = queue;
return(0);
}
int
ndis_intrhand(arg)
void *arg;
{
struct ndis_softc *sc;
ndis_handle adapter;
__stdcall ndis_interrupt_handler intrfunc;
if (arg == NULL)
return(EINVAL);
sc = arg;
NDIS_LOCK(sc);
adapter = sc->ndis_block.nmb_miniportadapterctx;
intrfunc = sc->ndis_chars.nmc_interrupt_func;
NDIS_UNLOCK(sc);
if (adapter == NULL || intrfunc == NULL)
return(EINVAL);
intrfunc(adapter);
return(0);
}
int
ndis_get_info(arg, oid, buf, buflen)
void *arg;
ndis_oid oid;
void *buf;
int *buflen;
{
struct ndis_softc *sc;
ndis_status rval;
ndis_handle adapter;
__stdcall ndis_queryinfo_handler queryfunc;
uint32_t byteswritten = 0, bytesneeded = 0;
int error;
sc = arg;
NDIS_LOCK(sc);
queryfunc = sc->ndis_chars.nmc_queryinfo_func;
adapter = sc->ndis_block.nmb_miniportadapterctx;
NDIS_UNLOCK(sc);
if (adapter == NULL || queryfunc == NULL)
return(ENXIO);
rval = queryfunc(adapter, oid, buf, *buflen,
&byteswritten, &bytesneeded);
/* Wait for requests that block. */
if (rval == NDIS_STATUS_PENDING) {
PROC_LOCK(curthread->td_proc);
error = msleep(&sc->ndis_block.nmb_wkupdpctimer,
&curthread->td_proc->p_mtx, PPAUSE|PDROP,
"ndisget", 5 * hz);
rval = sc->ndis_block.nmb_getstat;
}
if (byteswritten)
*buflen = byteswritten;
if (bytesneeded)
*buflen = bytesneeded;
if (rval == NDIS_STATUS_INVALID_LENGTH ||
rval == NDIS_STATUS_BUFFER_TOO_SHORT)
return(ENOSPC);
if (rval == NDIS_STATUS_INVALID_OID)
return(EINVAL);
if (rval == NDIS_STATUS_NOT_SUPPORTED ||
rval == NDIS_STATUS_NOT_ACCEPTED)
return(ENOTSUP);
if (rval != NDIS_STATUS_SUCCESS)
return(ENODEV);
return(0);
}
int
ndis_unload_driver(arg)
void *arg;
{
struct ndis_softc *sc;
sc = arg;
free(sc->ndis_block.nmb_rlist, M_DEVBUF);
ndis_flush_sysctls(sc);
ndis_shrink_thrqueue(8);
TAILQ_REMOVE(&ndis_devhead, &sc->ndis_block, link);
return(0);
}
#define NDIS_LOADED htonl(0x42534F44)
int
ndis_load_driver(img, arg)
vm_offset_t img;
void *arg;
{
__stdcall driver_entry entry;
image_optional_header opt_hdr;
image_import_descriptor imp_desc;
ndis_unicode_string dummystr;
ndis_miniport_block *block;
ndis_status status;
int idx;
uint32_t *ptr;
struct ndis_softc *sc;
sc = arg;
/*
* Only perform the relocation/linking phase once
* since the binary image may be shared among multiple
* device instances.
*/
ptr = (uint32_t *)(img + 8);
if (*ptr != NDIS_LOADED) {
/* Perform text relocation */
if (pe_relocate(img))
return(ENOEXEC);
/* Dynamically link the NDIS.SYS routines -- required. */
if (pe_patch_imports(img, "NDIS", ndis_functbl))
return(ENOEXEC);
/* Dynamically link the HAL.dll routines -- also required. */
if (pe_patch_imports(img, "HAL", hal_functbl))
return(ENOEXEC);
/* Dynamically link ntoskrnl.exe -- optional. */
if (pe_get_import_descriptor(img,
&imp_desc, "ntoskrnl") == 0) {
if (pe_patch_imports(img,
"ntoskrnl", ntoskrnl_functbl))
return(ENOEXEC);
}
*ptr = NDIS_LOADED;
}
/* Locate the driver entry point */
pe_get_optional_header(img, &opt_hdr);
entry = (driver_entry)pe_translate_addr(img, opt_hdr.ioh_entryaddr);
dummystr.nus_len = strlen(NDIS_DUMMY_PATH) * 2;
dummystr.nus_maxlen = strlen(NDIS_DUMMY_PATH) * 2;
dummystr.nus_buf = NULL;
ndis_ascii_to_unicode(NDIS_DUMMY_PATH, &dummystr.nus_buf);
/*
* Now that we have the miniport driver characteristics,
* create an NDIS block and call the init handler.
* This will cause the driver to try to probe for
* a device.
*/
block = &sc->ndis_block;
ptr = (uint32_t *)block;
for (idx = 0; idx < sizeof(ndis_miniport_block) / 4; idx++) {
*ptr = idx | 0xdead0000;
ptr++;
}
block->nmb_signature = (void *)0xcafebabe;
block->nmb_setdone_func = ndis_setdone_func;
block->nmb_querydone_func = ndis_getdone_func;
block->nmb_status_func = ndis_status_func;
block->nmb_statusdone_func = ndis_statusdone_func;
block->nmb_resetdone_func = ndis_resetdone_func;
block->nmb_sendrsrc_func = ndis_sendrsrcavail_func;
block->nmb_ifp = &sc->arpcom.ac_if;
block->nmb_dev = sc->ndis_dev;
block->nmb_img = img;
block->nmb_devobj.do_rsvd = block;
/*
* Now call the DriverEntry() routine. This will cause
* a callout to the NdisInitializeWrapper() and
* NdisMRegisterMiniport() routines.
*/
status = entry(&block->nmb_devobj, &dummystr);
free (dummystr.nus_buf, M_DEVBUF);
if (status != NDIS_STATUS_SUCCESS)
return(ENODEV);
ndis_enlarge_thrqueue(8);
TAILQ_INSERT_TAIL(&ndis_devhead, block, link);
return(0);
}
|