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
|
/*-
* This file is provided under a dual BSD/GPLv2 license. When using or
* redistributing this file, you may do so under either license.
*
* GPL LICENSE SUMMARY
*
* Copyright (c) 2019 Advanced Micro Devices, Inc. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* BSD LICENSE
*
* Copyright (c) 2019 Advanced Micro Devices, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copy
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Advanced Micro Devices, Inc nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 THE COPYRIGHT
* OWNER OR CONTRIBUTORS 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.
*
* PCIe NTB Debugging Tool FreeBSD driver
*/
/*
* How to use this tool, by example.
*
* List of sysctl for ntb_tool driver.
* root@local# sysctl -a | grep ntb_tool
* dev.ntb_tool.0.peer0.spad7: 0x0
* dev.ntb_tool.0.peer0.spad6: 0x0
* dev.ntb_tool.0.peer0.spad5: 0x0
* dev.ntb_tool.0.peer0.spad4: 0x0
* dev.ntb_tool.0.peer0.spad3: 0x0
* dev.ntb_tool.0.peer0.spad2: 0x0
* dev.ntb_tool.0.peer0.spad1: 0x0
* dev.ntb_tool.0.peer0.spad0: 0x0
* dev.ntb_tool.0.peer0.mw_trans2:
* dev.ntb_tool.0.peer0.mw_trans1:
* dev.ntb_tool.0.peer0.mw_trans0:
* dev.ntb_tool.0.peer0.peer_mw2:
* dev.ntb_tool.0.peer0.peer_mw1:
* dev.ntb_tool.0.peer0.peer_mw0:
* dev.ntb_tool.0.peer0.mw2:
* dev.ntb_tool.0.peer0.mw1:
* dev.ntb_tool.0.peer0.mw0:
* dev.ntb_tool.0.peer0.link_event: 0x0
* dev.ntb_tool.0.peer0.link: Y
* dev.ntb_tool.0.peer0.port: 1
* dev.ntb_tool.0.spad7: 0x0
* dev.ntb_tool.0.spad6: 0x0
* dev.ntb_tool.0.spad5: 0x0
* dev.ntb_tool.0.spad4: 0x0
* dev.ntb_tool.0.spad3: 0x0
* dev.ntb_tool.0.spad2: 0x0
* dev.ntb_tool.0.spad1: 0x0
* dev.ntb_tool.0.spad0: 0x0
* dev.ntb_tool.0.db: 0x0
* dev.ntb_tool.0.db_event: 0x0
* dev.ntb_tool.0.db_mask: 0xffff
* dev.ntb_tool.0.db_valid_mask: 0xffff
* dev.ntb_tool.0.peer_db: 0x0
* dev.ntb_tool.0.peer_db_mask: 0xffff
* dev.ntb_tool.0.link: Y
* dev.ntb_tool.0.port: 0
*
* The above example list shows
* 1) three memory windows,
* 1) eight scratchpad registers.
* 3) doorbell config.
* 4) link config.
* 2) One peer.
*
* Based on the underlined ntb_hw driver config & connection topology, these
* things might differ.
*-----------------------------------------------------------------------------
* Eg: check local/peer port information.
*
* # Get local device port number
* root@local# sysctl dev.ntb_tool.0.port
*
* # Check peer device port number
* root@local# sysctl dev.ntb_tool.0.peer0.port
*-----------------------------------------------------------------------------
* Eg: NTB link tests
*
* # Set local link up/down
* root@local# sysctl dev.ntb_tool.0.link=Y
* root@local# sysctl dev.ntb_tool.0.link=N
*
* # Check if link with peer device is up/down:
* root@local# sysctl dev.ntb_tool.0.peer0.link
*
* # Poll until the link specified as up/down. For up, value needs to be set
* depends on peer index, i.e., for peer0 it is 0x1 and for down, value needs
* to be set as 0x0.
* root@local# sysctl dev.ntb_tool.0.peer0.link_event=0x1
* root@local# sysctl dev.ntb_tool.0.peer0.link_event=0x0
*-----------------------------------------------------------------------------
* Eg: Doorbell registers tests
*
* # clear/get local doorbell
* root@local# sysctl dev.ntb_tool.0.db="c 0x1"
* root@local# sysctl dev.ntb_tool.0.db
*
* # Set/clear/get local doorbell mask
* root@local# sysctl dev.ntb_tool.0.db_mask="s 0x1"
* root@local# sysctl dev.ntb_tool.0.db_mask="c 0x1"
* root@local# sysctl dev.ntb_tool.0.db_mask
*
* # Ring/clear/get peer doorbell
* root@local# sysctl dev.ntb_tool.0.peer_db="s 0x1"
* root@local# sysctl dev.ntb_tool.0.peer_db="c 0x1"
* root@local# sysctl dev.ntb_tool.0.peer_db
*
* # Set/clear/get peer doorbell mask (functionality is absent)
* root@local# sysctl dev.ntb_tool.0.peer_db_mask="s 0x1"
* root@local# sysctl dev.ntb_tool.0.peer_db_mask="c 0x1"
* root@local# sysctl dev.ntb_tool.0.peer_db_mask
*
* # Poll until local doorbell is set with the specified db bits
* root@local# dev.ntb_tool.0.db_event=0x1
*-----------------------------------------------------------------------------
* Eg: Scratchpad registers tests
*
* # Write/read to/from local scratchpad register #0
* root@local# sysctl dev.ntb_tool.0.spad0=0x1023457
* root@local# sysctl dev.ntb_tool.0.spad0
*
* # Write/read to/from peer scratchpad register #0
* root@local# sysctl dev.ntb_tool.0.peer0.spad0=0x01020304
* root@local# sysctl dev.ntb_tool.0.peer0.spad0
*-----------------------------------------------------------------------------
* Eg: Memory windows tests (need to configure local mw_trans on both sides)
*
* # Create inbound memory window buffer of specified size/get its dma address
* root@local# sysctl dev.ntb_tool.0.peer0.mw_trans0=16384
* root@local# sysctl dev.ntb_tool.0.peer0.mw_trans0
*
* # Write/read data to/from inbound memory window with specific pattern/random
* data.
* root@local# sysctl dev.ntb_tool.0.peer0.mw0="W offset 0 nbytes 100 pattern ab"
* root@local# sysctl dev.ntb_tool.0.peer0.mw0="R offset 0 nbytes 100"
*
* # Write/read data to/from outbound memory window on the local device with
* specific pattern/random (on peer device)
* root@local# sysctl dev.ntb_tool.0.peer0.peer_mw0="W offset 0 nbytes 100 pattern ab"
* root@local# sysctl dev.ntb_tool.0.peer0.peer_mw0="R offset 0 nbytes 100"
*-----------------------------------------------------------------------------
* NOTE: *Message registers are not supported*
*-----------------------------------------------------------------------------
*
* contact information:
* Arpan Palit <arpan.palit@amd.com>
*
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/mbuf.h>
#include <sys/sysctl.h>
#include <sys/sbuf.h>
#include <machine/bus.h>
#include <vm/vm.h>
#include "../ntb.h"
/* Buffer length for User input */
#define TOOL_BUF_LEN 48
/* Memory window default command read and write offset. */
#define DEFAULT_MW_OFF 0
/* Memory window default size and also max command read size. */
#define DEFAULT_MW_SIZE 1024
MALLOC_DEFINE(M_NTB_TOOL, "ntb_tool", "ntb_tool driver memory allocation");
/*
* Memory windows descriptor structure
*/
struct tool_mw {
struct tool_ctx *tc;
int widx;
int pidx;
/* Rx buff is off virt_addr / dma_base */
bus_addr_t dma_base;
caddr_t virt_addr;
bus_dmamap_t dma_map;
bus_dma_tag_t dma_tag;
/* Tx buff is off vbase / phys_addr */
caddr_t mm_base;
vm_paddr_t phys_addr;
bus_addr_t addr_limit;
size_t phys_size;
size_t xlat_align;
size_t xlat_align_size;
/* Memory window configured size and limits */
size_t size;
ssize_t mw_buf_size;
ssize_t mw_buf_offset;
ssize_t mw_peer_buf_size;
ssize_t mw_peer_buf_offset;
/* options to handle sysctl out */
int mw_cmd_rw;
int mw_peer_cmd_rw;
};
struct tool_spad {
int sidx;
int pidx;
struct tool_ctx *tc;
};
struct tool_peer {
int pidx;
struct tool_ctx *tc;
int inmw_cnt;
struct tool_mw *inmws;
int outspad_cnt;
struct tool_spad *outspads;
unsigned int port_no;
};
struct tool_ctx {
device_t dev;
struct callout link_event_timer;
struct callout db_event_timer;
int peer_cnt;
struct tool_peer *peers;
int inmsg_cnt;
struct tool_msg *inmsgs;
int inspad_cnt;
struct tool_spad *inspads;
unsigned int unsafe;
/* sysctl read out variables */
char link_status;
uint64_t link_bits;
uint64_t link_mask;
uint64_t db_valid_mask;
uint64_t db_mask_val;
uint64_t db_event_val;
uint64_t peer_db_val;
uint64_t peer_db_mask_val;
unsigned int port_no;
};
/* structure to save dma_addr after dma load */
struct ntb_tool_load_cb_args {
bus_addr_t addr;
int error;
};
/*
* NTB events handlers
*/
static void
tool_link_event(void *ctx)
{
struct tool_ctx *tc = ctx;
enum ntb_speed speed = 0;
enum ntb_width width = 0;
int up = 0;
up = ntb_link_is_up(tc->dev, &speed, &width);
if (up)
tc->link_status = 'Y';
else
tc->link_status = 'N';
device_printf(tc->dev, "link is %s speed %d width %d\n",
up ? "up" : "down", speed, width);
}
static void
tool_db_event(void *ctx, uint32_t vec)
{
struct tool_ctx *tc = ctx;
uint64_t db_bits, db_mask;
db_mask = ntb_db_vector_mask(tc->dev, vec);
db_bits = ntb_db_read(tc->dev);
device_printf(tc->dev, "doorbell vec %d mask %#llx bits %#llx\n",
vec, (unsigned long long)db_mask, (unsigned long long)db_bits);
}
static const struct ntb_ctx_ops tool_ops = {
.link_event = tool_link_event,
.db_event = tool_db_event,
};
/*
* Callout event methods
*/
static void
tool_link_event_handler(void *arg)
{
struct tool_ctx *tc = (struct tool_ctx *)arg;
uint64_t val;
val = ntb_link_is_up(tc->dev, NULL, NULL) & tc->link_mask;
if (val == tc->link_bits) {
device_printf(tc->dev, "link_event successful for link val="
"0x%jx\n", tc->link_bits);
tc->link_bits = 0x0;
tc->link_mask = 0x0;
} else
callout_reset(&tc->link_event_timer, 1, tool_link_event_handler, tc);
}
static void
tool_db_event_handler(void *arg)
{
struct tool_ctx *tc = (struct tool_ctx *)arg;
uint64_t db_bits;
db_bits = ntb_db_read(tc->dev);
if (db_bits == tc->db_event_val) {
device_printf(tc->dev, "db_event successful for db val=0x%jx\n",
tc->db_event_val);
tc->db_event_val = 0x0;
} else
callout_reset(&tc->db_event_timer, 1, tool_db_event_handler, tc);
}
/*
* Common read/write methods
*/
static inline int
get_ubuf(struct sysctl_req *req, char *ubuf)
{
int rc;
if (req->newlen >= TOOL_BUF_LEN)
return (EINVAL);
rc = SYSCTL_IN(req, ubuf, req->newlen);
if (rc)
return (rc);
ubuf[req->newlen] = '\0';
return (0);
}
static int
read_out(struct sysctl_req *req, uint64_t val)
{
char ubuf[16];
memset((void *)ubuf, 0, sizeof(ubuf));
snprintf(ubuf, sizeof(ubuf), "0x%jx", val);
return SYSCTL_OUT(req, ubuf, sizeof(ubuf));
}
static int
tool_fn_read(struct tool_ctx *tc, struct sysctl_req *req,
uint64_t (*fn_read)(device_t ), uint64_t val)
{
if (fn_read == NULL)
return read_out(req, val);
else if (fn_read)
return read_out(req, (uint64_t)fn_read(tc->dev));
else
return (EINVAL);
}
static int
tool_fn_write(struct tool_ctx *tc, struct sysctl_oid *oidp,
struct sysctl_req *req, char *ubuf, uint64_t *val, bool db_mask_sflag,
void (*fn_set)(device_t , uint64_t), void (*fn_clear)(device_t , uint64_t))
{
uint64_t db_valid_mask = tc->db_valid_mask;
uint64_t bits;
char cmd;
if (fn_set == NULL && fn_clear == NULL) {
device_printf(tc->dev, "ERR: Set & Clear both are not supported\n");
return (EINVAL);
}
if (tc->db_valid_mask == 0)
db_valid_mask = tc->db_valid_mask = ntb_db_valid_mask(tc->dev);
bits = 0;
sscanf(ubuf, "%c %jx", &cmd, &bits);
if (cmd == 's') {
if ((bits | db_valid_mask) > db_valid_mask) {
device_printf(tc->dev, "0x%jx value is not supported\n", bits);
return (EINVAL);
}
if (fn_set)
fn_set(tc->dev, bits);
else
return (EINVAL);
if (val)
*val |= bits;
} else if (cmd == 'c') {
if ((bits | db_valid_mask) > db_valid_mask) {
device_printf(tc->dev, "0x%jx value is not supported\n", bits);
return (EINVAL);
}
if (fn_clear)
fn_clear(tc->dev, bits);
if (val)
*val &= ~bits;
} else {
device_printf(tc->dev, "Wrong Write\n");
return (EINVAL);
}
return (0);
}
static int
parse_mw_buf(char *buf, char *cmd, ssize_t *offset, ssize_t *buf_size,
uint64_t *pattern, bool *s_pflag)
{
char op1[8], op2[8], op3[8];
uint64_t val1, val2, val3;
bool vs1, vs2, vs3;
int rc = 0;
vs1 = vs2 = vs3 = false;
sscanf(buf, "%c %s %jx %s %jx %s %jx",
cmd, op1, &val1, op2, &val2, op3, &val3);
if (*cmd != 'W' && *cmd != 'R')
return (EINVAL);
if (!strcmp(op1, "offset")) {
*offset = val1 ? val1 : DEFAULT_MW_OFF;
vs1 = true;
} else if (!strcmp(op1, "nbytes")) {
*buf_size = val1 ? val1: DEFAULT_MW_SIZE;
vs2 = true;
} else if (!strcmp(op1, "pattern")) {
*pattern = val1;
vs3 = true;
}
if (!vs1 && !strcmp(op2, "offset")) {
*offset = val2 ? val2 : DEFAULT_MW_OFF;
vs1 = true;
} else if (!vs2 && !strcmp(op2, "nbytes")) {
*buf_size = val2 ? val2: DEFAULT_MW_SIZE;
vs2 = true;
} else if (!vs3 && !strcmp(op2, "pattern")) {
*pattern = val2;
vs3 = true;
}
if (!vs1 && !strcmp(op3, "offset")) {
*offset = val3 ? val3 : DEFAULT_MW_OFF;
} else if (!vs2 && !strcmp(op3, "nbytes")) {
*buf_size = val3 ? val3: DEFAULT_MW_SIZE;
} else if (!vs3 && !strcmp(op3, "pattern")) {
*pattern = val3;
vs3 = true;
}
*s_pflag = vs3;
if (vs3 && *cmd == 'R')
printf("NTB_TOOL_WARN: pattern is not supported with read "
"command\n");
return (rc);
}
static int
tool_mw_read_fn(struct sysctl_req *req, struct tool_mw *inmw, char *read_addr,
int *cmd_op, ssize_t buf_off, ssize_t buf_size, char *type)
{
ssize_t index, size;
struct sbuf *sb;
int i, loop, rc;
char *tmp;
/* The below check is made to ignore sysctl read call. */
if (*cmd_op == 0)
return (0);
/* Proceeds only when command R/W is requested using sysctl. */
index = buf_off;
tmp = read_addr;
tmp += index;
loop = ((buf_size == 0) || (buf_size > DEFAULT_MW_SIZE)) ?
DEFAULT_MW_SIZE : buf_size;
/*
* 256 bytes of extra buffer has been allocated to print details like
* summary, size, notes, i.e., excluding data part.
*/
size = loop + 256;
sb = sbuf_new_for_sysctl(NULL, NULL, size, req);
if (sb == NULL) {
rc = sb->s_error;
return (rc);
}
if (!strcmp(type, "mw"))
sbuf_printf(sb, "\nConfigured MW size\t: %zu\n", inmw->size);
else if (!strcmp(type, "peer_mw"))
sbuf_printf(sb, "\nConfigured Peer MW size\t: %zu\n",
inmw->size);
sbuf_printf(sb, "R/W size\t\t: %zi\nR/W Offset\t\t: %zi\n\nData\n----"
"->", buf_size, buf_off);
/*
* Data will be read based on MW size provided by the user using nbytes,
* which is limited to 1024 bytes if user req bigger size to read, check
* above loop calculation which is limiting or setting the MW read size.
* Below for loop prints data where in each line contains 32 bytes data
* and after each 8 bytes of data we used four spaces which ensures one
* data block.
*/
for (i = 0 ; i < loop; i++) {
if ((i % 32) == 0) {
sbuf_printf(sb, "\n%08zx:", index);
index += 32;
}
if ((i % 8) == 0)
sbuf_printf(sb, " ");
sbuf_printf(sb, "%02hhx", *(tmp+i));
}
if (buf_size > DEFAULT_MW_SIZE)
sbuf_printf(sb, "\n\nNOTE: Truncating read size %zi->1024 "
"bytes\n", buf_size);
/* cmd_op is set to zero after completion of each R/W command. */
*cmd_op -= 1;
rc = sbuf_finish(sb);
sbuf_delete(sb);
return (rc);
}
static int
tool_mw_write_fn(struct sysctl_oid *oidp, struct sysctl_req *req,
struct tool_mw *inmw, char *ubuf, caddr_t write_buf, int *cmd_op,
ssize_t *buf_offset, ssize_t *buf_size)
{
ssize_t data_buf_size;
uint64_t pattern = 0;
bool s_pflag = false;
void *data_buf;
char cmd;
int rc;
if (!write_buf)
return (ENXIO);
/* buf_offset and buf_size set to default in case user does not req */
*buf_offset = DEFAULT_MW_OFF;
*buf_size = DEFAULT_MW_SIZE;
rc = parse_mw_buf(ubuf, &cmd, buf_offset, buf_size, &pattern, &s_pflag);
if (rc) {
device_printf(inmw->tc->dev, "Wrong Command \"%c\" provided\n",
cmd);
return (rc);
}
/* Check for req size and buffer limit */
if ((*buf_offset + *buf_size) > inmw->size) {
device_printf(inmw->tc->dev, "%s: configured mw size :%zi and "
"requested size :%zi.\n", __func__, inmw->size,
(*buf_offset + *buf_size));
*buf_offset = DEFAULT_MW_OFF;
*buf_size = DEFAULT_MW_SIZE;
rc = EINVAL;
goto out;
}
if (cmd == 'R')
goto read_out;
else if (cmd == 'W')
goto write;
else
goto out;
write:
data_buf_size = *buf_size;
data_buf = malloc(data_buf_size, M_NTB_TOOL, M_WAITOK | M_ZERO);
if (s_pflag)
memset(data_buf, pattern, data_buf_size);
else
arc4rand(data_buf, data_buf_size, 1);
memcpy(write_buf + *buf_offset, data_buf, data_buf_size);
free(data_buf, M_NTB_TOOL);
read_out:
/* cmd_op value is set to two as sysctl read call executes twice */
*cmd_op = 2;
out:
return (rc);
}
/*
* Port sysctl read/write methods
*/
static int
sysctl_peer_port_number(SYSCTL_HANDLER_ARGS)
{
struct tool_ctx *tc = (struct tool_ctx *)arg1;
int rc, pidx = arg2, peer_port;
peer_port = ntb_peer_port_number(tc->dev, pidx);
rc = sysctl_handle_int(oidp, &peer_port, 0, req);
if (rc)
device_printf(tc->dev, "Peer port sysctl set failed with err="
"(%d).\n", rc);
else
tc->peers[pidx].port_no = peer_port;
return (rc);
}
static int
sysctl_local_port_number(SYSCTL_HANDLER_ARGS)
{
struct tool_ctx *tc = (struct tool_ctx *)arg1;
int rc, local_port;
local_port = ntb_port_number(tc->dev);
rc = sysctl_handle_int(oidp, &local_port, 0, req);
if (rc)
device_printf(tc->dev, "Local port sysctl set failed with err="
"(%d).\n", rc);
else
tc->port_no = local_port;
return (rc);
}
static void
tool_init_peers(struct tool_ctx *tc)
{
int pidx;
tc->peer_cnt = ntb_peer_port_count(tc->dev);
tc->peers = malloc(tc->peer_cnt * sizeof(*tc->peers), M_NTB_TOOL,
M_WAITOK | M_ZERO);
for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
tc->peers[pidx].pidx = pidx;
tc->peers[pidx].tc = tc;
}
}
static void
tool_clear_peers(struct tool_ctx *tc)
{
free(tc->peers, M_NTB_TOOL);
}
/*
* Link state sysctl read/write methods
*/
static int
sysctl_link_handle(SYSCTL_HANDLER_ARGS)
{
struct tool_ctx *tc = (struct tool_ctx *)arg1;
char buf[TOOL_BUF_LEN];
int rc;
if (req->newptr == NULL) {
snprintf(buf, 2, "%c", tc->link_status);
return SYSCTL_OUT(req, buf, 2);
}
rc = get_ubuf(req, buf);
if (rc)
return (rc);
if (buf[0] == 'Y')
rc = ntb_link_enable(tc->dev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
else if (buf[0] == 'N')
rc = ntb_link_disable(tc->dev);
else
rc = EINVAL;
sscanf(buf, "%c", &tc->link_status);
return (0);
}
static int
sysctl_peer_link_handle(SYSCTL_HANDLER_ARGS)
{
struct tool_ctx *tc = (struct tool_ctx *)arg1;
int up = 0, pidx = arg2;
char buf[TOOL_BUF_LEN];
if (req->newptr)
return (0);
up = ntb_link_is_up(tc->dev, NULL, NULL);
memset((void *)buf, 0, TOOL_BUF_LEN);
if (up & (1UL << pidx))
buf[0] = 'Y';
else
buf[0] = 'N';
return SYSCTL_OUT(req, buf, sizeof(buf));
}
static int
sysctl_peer_link_event_handle(SYSCTL_HANDLER_ARGS)
{
struct tool_ctx *tc = (struct tool_ctx *)arg1;
char buf[TOOL_BUF_LEN];
int rc, pidx = arg2;
uint64_t bits;
if (req->newptr == NULL)
return read_out(req, tc->link_bits);
rc = get_ubuf(req, buf);
if (rc)
return (rc);
sscanf(buf, "0x%jx", &bits);
tc->link_bits = bits;
tc->link_mask = (1ULL << ((pidx) % 64));
callout_reset(&tc->link_event_timer, 1, tool_link_event_handler, tc);
return (0);
}
/*
* Memory windows read/write/setting methods
*/
static void
ntb_tool_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
{
struct ntb_tool_load_cb_args *cba = (struct ntb_tool_load_cb_args *)arg;
if (!(cba->error = error))
cba->addr = segs[0].ds_addr;
}
static int
sysctl_mw_handle(SYSCTL_HANDLER_ARGS)
{
struct tool_mw *inmw = (struct tool_mw *)arg1;
char buf[TOOL_BUF_LEN];
int rc;
if (req->newptr == NULL)
return tool_mw_read_fn(req, inmw, (char *)inmw->mm_base,
&inmw->mw_cmd_rw, inmw->mw_buf_offset, inmw->mw_buf_size,
"mw");
rc = get_ubuf(req, buf);
if (!rc)
return tool_mw_write_fn(oidp, req, inmw, buf, inmw->mm_base,
&inmw->mw_cmd_rw, &inmw->mw_buf_offset, &inmw->mw_buf_size);
return (rc);
}
static int
tool_setup_mw(struct tool_ctx *tc, unsigned int pidx, unsigned int widx,
size_t req_size)
{
struct tool_mw *inmw = &tc->peers[pidx].inmws[widx];
struct ntb_tool_load_cb_args cba;
int rc;
if (req_size == 0)
inmw->size = roundup(inmw->phys_size, inmw->xlat_align_size);
else
inmw->size = roundup(req_size, inmw->xlat_align_size);
device_printf(tc->dev, "mw_size %zi req_size %zi buff %zi\n",
inmw->phys_size, req_size, inmw->size);
if (bus_dma_tag_create(bus_get_dma_tag(tc->dev), inmw->xlat_align, 0,
inmw->addr_limit, BUS_SPACE_MAXADDR, NULL, NULL, inmw->size, 1,
inmw->size, 0, NULL, NULL, &inmw->dma_tag)) {
device_printf(tc->dev, "Unable to create MW tag of size "
"%zu/%zu\n", inmw->phys_size, inmw->size);
rc = ENOMEM;
goto err_free_dma_var;
}
if (bus_dmamem_alloc(inmw->dma_tag, (void **)&inmw->virt_addr,
BUS_DMA_WAITOK | BUS_DMA_ZERO, &inmw->dma_map)) {
device_printf(tc->dev, "Unable to allocate MW buffer of size "
"%zu/%zu\n", inmw->phys_size, inmw->size);
rc = ENOMEM;
goto err_free_tag_rem;
}
if (bus_dmamap_load(inmw->dma_tag, inmw->dma_map, inmw->virt_addr,
inmw->size, ntb_tool_load_cb, &cba, BUS_DMA_NOWAIT) || cba.error) {
device_printf(tc->dev, "Unable to load MW buffer of size "
"%zu/%zu\n", inmw->phys_size, inmw->size);
rc = ENOMEM;
goto err_free_dma;
}
inmw->dma_base = cba.addr;
rc = ntb_mw_set_trans(tc->dev, widx, inmw->dma_base, inmw->size);
if (rc)
goto err_free_mw;
return (0);
err_free_mw:
bus_dmamap_unload(inmw->dma_tag, inmw->dma_map);
err_free_dma:
bus_dmamem_free(inmw->dma_tag, inmw->virt_addr, inmw->dma_map);
err_free_tag_rem:
bus_dma_tag_destroy(inmw->dma_tag);
err_free_dma_var:
inmw->size = 0;
inmw->virt_addr = 0;
inmw->dma_base = 0;
inmw->dma_tag = 0;
inmw->dma_map = 0;
return (rc);
}
static void
tool_free_mw(struct tool_ctx *tc, int pidx, int widx)
{
struct tool_mw *inmw = &tc->peers[pidx].inmws[widx];
if (inmw->dma_base)
ntb_mw_clear_trans(tc->dev, widx);
if (inmw->virt_addr && inmw->dma_tag) {
bus_dmamap_unload(inmw->dma_tag, inmw->dma_map);
bus_dmamem_free(inmw->dma_tag, inmw->virt_addr, inmw->dma_map);
bus_dma_tag_destroy(inmw->dma_tag);
}
inmw->virt_addr = 0;
inmw->dma_base = 0;
inmw->dma_tag = 0;
inmw->dma_map = 0;
inmw->mm_base = 0;
inmw->size = 0;
}
static int
tool_mw_trans_read(struct tool_mw *inmw, struct sysctl_req *req)
{
ssize_t buf_size = 512;
struct sbuf *sb;
int rc = 0;
sb = sbuf_new_for_sysctl(NULL, NULL, buf_size, req);
if (sb == NULL) {
rc = sb->s_error;
return (rc);
}
sbuf_printf(sb, "\nInbound MW \t%d\n", inmw->widx);
sbuf_printf(sb, "Port \t%d (%d)\n",
ntb_peer_port_number(inmw->tc->dev, inmw->pidx), inmw->pidx);
sbuf_printf(sb, "Window Address \t%p\n", inmw->mm_base);
sbuf_printf(sb, "DMA Address \t0x%016llx\n", (long long)inmw->dma_base);
sbuf_printf(sb, "Window Size \t0x%016zx[p]\n", inmw->size);
sbuf_printf(sb, "Alignment \t0x%016zx[p]\n", inmw->xlat_align);
sbuf_printf(sb, "Size Alignment \t0x%016zx[p]\n",
inmw->xlat_align_size);
sbuf_printf(sb, "Size Max \t0x%016zx[p]\n", inmw->phys_size);
rc = sbuf_finish(sb);
sbuf_delete(sb);
return (rc);
}
static int
tool_mw_trans_write(struct sysctl_oid *oidp, struct sysctl_req *req,
struct tool_mw *inmw, size_t wsize)
{
struct tool_ctx *tc = inmw->tc;
int rc = 0;
if (wsize == 0)
return (EINVAL);
/* No need to re-setup mw */
if (inmw->size == wsize)
return (0);
/* free mw dma buffer */
if (inmw->size)
tool_free_mw(tc, inmw->pidx, inmw->widx);
rc = tool_setup_mw(tc, inmw->pidx, inmw->widx, wsize);
return (rc);
}
static int
sysctl_mw_trans_handler(SYSCTL_HANDLER_ARGS)
{
struct tool_mw *inmw = (struct tool_mw *)arg1;
char buf[TOOL_BUF_LEN];
ssize_t wsize;
int rc;
if (req->newptr == NULL)
return tool_mw_trans_read(inmw, req);
rc = get_ubuf(req, buf);
if (rc == 0) {
sscanf(buf, "%zi", &wsize);
return tool_mw_trans_write(oidp, req, inmw, wsize);
}
return (rc);
}
static int
sysctl_peer_mw_handle(SYSCTL_HANDLER_ARGS)
{
struct tool_mw *inmw = (struct tool_mw *)arg1;
char buf[TOOL_BUF_LEN];
int rc;
if (req->newptr == NULL)
return tool_mw_read_fn(req, inmw, (char *)inmw->virt_addr,
&inmw->mw_peer_cmd_rw, inmw->mw_peer_buf_offset,
inmw->mw_peer_buf_size, "mw");
rc = get_ubuf(req, buf);
if (rc == 0)
return tool_mw_write_fn(oidp, req, inmw, buf, inmw->virt_addr,
&inmw->mw_peer_cmd_rw, &inmw->mw_peer_buf_offset,
&inmw->mw_peer_buf_size);
return (rc);
}
static void tool_clear_mws(struct tool_ctx *tc)
{
int widx, pidx;
/* Free outbound memory windows */
for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++)
tool_free_mw(tc, pidx, widx);
free(tc->peers[pidx].inmws, M_NTB_TOOL);
}
}
static int
tool_init_mws(struct tool_ctx *tc)
{
struct tool_mw *mw;
int widx, pidx, rc;
/* Initialize inbound memory windows and outbound MWs wrapper */
for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
tc->peers[pidx].inmw_cnt = ntb_mw_count(tc->dev);
tc->peers[pidx].inmws = malloc(tc->peers[pidx].inmw_cnt *
sizeof(*tc->peers[pidx].inmws), M_NTB_TOOL,
M_WAITOK | M_ZERO);
for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++) {
mw = &tc->peers[pidx].inmws[widx];
memset((void *)mw, 0, sizeof(*mw));
mw->tc = tc;
mw->widx = widx;
mw->pidx = pidx;
mw->mw_buf_offset = DEFAULT_MW_OFF;
mw->mw_buf_size = DEFAULT_MW_SIZE;
/* get the tx buff details for each mw attached with each peer */
rc = ntb_mw_get_range(tc->dev, widx, &mw->phys_addr,
&mw->mm_base, &mw->phys_size, &mw->xlat_align,
&mw->xlat_align_size, &mw->addr_limit);
if (rc)
goto free_mws;
}
}
return (0);
free_mws:
tool_clear_mws(tc);
return (rc);
}
/*
* Doorbell handler for read/write
*/
static int
sysctl_db_handle(SYSCTL_HANDLER_ARGS)
{
struct tool_ctx *tc = (struct tool_ctx *)arg1;
char buf[TOOL_BUF_LEN];
uint64_t db_bits;
int rc;
if (req->newptr == NULL) {
db_bits = ntb_db_read(tc->dev);
return read_out(req, db_bits);
}
rc = get_ubuf(req, buf);
if (rc == 0)
return tool_fn_write(tc, oidp, req, buf, NULL, false, NULL,
ntb_db_clear);
return (rc);
}
static int
sysctl_db_valid_mask_handle(SYSCTL_HANDLER_ARGS)
{
struct tool_ctx *tc = (struct tool_ctx *)arg1;
tc->db_valid_mask = ntb_db_valid_mask(tc->dev);
if (!tc->db_valid_mask) {
device_printf(tc->dev, "Error getting db_valid_mask from "
"hw driver\n");
return (EINVAL);
} else {
return read_out(req, tc->db_valid_mask);
}
}
static int
sysctl_db_mask_handle(SYSCTL_HANDLER_ARGS)
{
struct tool_ctx *tc = (struct tool_ctx *)arg1;
char buf[TOOL_BUF_LEN];
int rc;
if (req->newptr == NULL) {
if (tc->db_mask_val == 0)
ntb_db_valid_mask(tc->dev);
return tool_fn_read(tc, req, NULL, tc->db_mask_val);
}
rc = get_ubuf(req, buf);
if (rc == 0)
return tool_fn_write(tc, oidp, req, buf, &tc->db_mask_val, true,
ntb_db_set_mask, ntb_db_clear_mask);
return (rc);
}
static int
sysctl_peer_db_handle(SYSCTL_HANDLER_ARGS)
{
struct tool_ctx *tc = (struct tool_ctx *)arg1;
char buf[TOOL_BUF_LEN];
int rc;
if (req->newptr == NULL)
return tool_fn_read(tc, req, NULL, tc->peer_db_val);
rc = get_ubuf(req, buf);
if (rc == 0)
return tool_fn_write(tc, oidp, req, buf, &tc->peer_db_val,
false, ntb_peer_db_set, NULL);
return (rc);
}
static int
sysctl_peer_db_mask_handle(SYSCTL_HANDLER_ARGS)
{
struct tool_ctx *tc = (struct tool_ctx *)arg1;
char buf[TOOL_BUF_LEN];
int rc;
if (req->newptr == NULL){
if (tc->peer_db_mask_val == 0)
ntb_db_valid_mask(tc->dev);
return tool_fn_read(tc, req, NULL, tc->peer_db_mask_val);
}
rc = get_ubuf(req, buf);
if (rc == 0)
return tool_fn_write(tc, oidp, req, buf, &tc->peer_db_mask_val,
true, NULL, NULL);
return (rc);
}
static int
sysctl_db_event_handle(SYSCTL_HANDLER_ARGS)
{
struct tool_ctx *tc = (struct tool_ctx *)arg1;
char buf[TOOL_BUF_LEN];
uint64_t bits;
int rc;
if (req->newptr == NULL)
return read_out(req, tc->db_event_val);
rc = get_ubuf(req, buf);
if (rc)
return (rc);
sscanf(buf, "%ju", &bits);
tc->db_event_val = bits;
callout_reset(&tc->db_event_timer, 1, tool_db_event_handler, tc);
return (0);
}
/*
* Scratchpads read/write methods
*/
static int
sysctl_spad_handle(SYSCTL_HANDLER_ARGS)
{
struct tool_ctx *tc = (struct tool_ctx *)arg1;
unsigned int sidx = arg2;
char buf[TOOL_BUF_LEN];
uint32_t bits;
int rc;
if (req->newptr == NULL) {
rc = ntb_spad_read(tc->dev, sidx, &bits);
if (rc)
return (rc);
else
return read_out(req, (uint64_t )bits);
}
rc = get_ubuf(req, buf);
if (rc == 0) {
sscanf(buf, "%i", &bits);
return ntb_spad_write(tc->dev, sidx, bits);
}
return (rc);
}
static int
sysctl_peer_spad_handle(SYSCTL_HANDLER_ARGS)
{
struct tool_ctx *tc = (struct tool_ctx *)arg1;
unsigned int sidx = arg2;
char buf[TOOL_BUF_LEN];
uint32_t bits;
int rc;
if (req->newptr == NULL) {
rc = ntb_peer_spad_read(tc->dev, sidx, &bits);
if (rc)
return (rc);
else
return read_out(req, (uint64_t )bits);
}
rc = get_ubuf(req, buf);
if (rc == 0) {
sscanf(buf, "%i", &bits);
return ntb_peer_spad_write(tc->dev, sidx, bits);
}
return (rc);
}
static void
tool_init_spads(struct tool_ctx *tc)
{
int sidx, pidx;
/* Initialize inbound scratchpad structures */
tc->inspad_cnt = ntb_spad_count(tc->dev);
tc->inspads = malloc(tc->inspad_cnt * sizeof(*tc->inspads), M_NTB_TOOL,
M_WAITOK | M_ZERO);
for (sidx = 0; sidx < tc->inspad_cnt; sidx++) {
tc->inspads[sidx].sidx = sidx;
tc->inspads[sidx].pidx = -1;
tc->inspads[sidx].tc = tc;
}
/* Initialize outbound scratchpad structures */
for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
tc->peers[pidx].outspad_cnt = ntb_spad_count(tc->dev);
tc->peers[pidx].outspads = malloc(tc->peers[pidx].outspad_cnt *
sizeof(*tc->peers[pidx].outspads), M_NTB_TOOL, M_WAITOK |
M_ZERO);
for (sidx = 0; sidx < tc->peers[pidx].outspad_cnt; sidx++) {
tc->peers[pidx].outspads[sidx].sidx = sidx;
tc->peers[pidx].outspads[sidx].pidx = pidx;
tc->peers[pidx].outspads[sidx].tc = tc;
}
}
}
static void
tool_clear_spads(struct tool_ctx *tc)
{
int pidx;
/* Free local inspads. */
free(tc->inspads, M_NTB_TOOL);
/* Free outspads for each peer. */
for (pidx = 0; pidx < tc->peer_cnt; pidx++)
free(tc->peers[pidx].outspads, M_NTB_TOOL);
}
/*
* Initialization methods
*/
static int
tool_check_ntb(struct tool_ctx *tc)
{
/* create and initialize link callout handler */
callout_init(&tc->link_event_timer, 1);
/* create and initialize db callout handler */
callout_init(&tc->db_event_timer, 1);
/* Initialize sysctl read out values to default */
tc->link_status = 'U';
tc->db_mask_val = 0;
tc->peer_db_val = 0;
tc->peer_db_mask_val = 0;
tc->db_event_val = 0;
tc->link_bits = 0;
return (0);
}
static void
tool_clear_data(struct tool_ctx *tc)
{
callout_drain(&tc->link_event_timer);
callout_drain(&tc->db_event_timer);
}
static int
tool_init_ntb(struct tool_ctx *tc)
{
return ntb_set_ctx(tc->dev, tc, &tool_ops);
}
static void
tool_clear_ntb(struct tool_ctx *tc)
{
ntb_clear_ctx(tc->dev);
ntb_link_disable(tc->dev);
}
/*
* Current sysctl implementation is made such that it gets attached to the
* device and while detach it gets cleared automatically.
*/
static void
tool_setup_sysctl(struct tool_ctx *tc)
{
char buf[TOOL_BUF_LEN], desc[TOOL_BUF_LEN];
struct sysctl_oid_list *top, *peer_top;
struct sysctl_oid *parent, *peer;
struct sysctl_ctx_list *clist;
unsigned int pidx, sidx, widx;
clist = device_get_sysctl_ctx(tc->dev);
parent = device_get_sysctl_tree(tc->dev);
top = SYSCTL_CHILDREN(parent);
SYSCTL_ADD_PROC(clist, top, OID_AUTO, "port", CTLTYPE_UINT |
CTLFLAG_RDTUN | CTLFLAG_MPSAFE, tc, 0, sysctl_local_port_number,
"IU", "local port number");
SYSCTL_ADD_PROC(clist, top, OID_AUTO, "link", CTLTYPE_STRING |
CTLFLAG_RWTUN | CTLFLAG_MPSAFE, tc, 0, sysctl_link_handle,
"IU", "link info");
SYSCTL_ADD_PROC(clist, top, OID_AUTO, "db", CTLTYPE_STRING |
CTLFLAG_RWTUN | CTLFLAG_MPSAFE, tc, 0, sysctl_db_handle,
"A", "db info");
SYSCTL_ADD_PROC(clist, top, OID_AUTO, "db_valid_mask", CTLTYPE_STRING |
CTLFLAG_RD | CTLFLAG_MPSAFE, tc, 0, sysctl_db_valid_mask_handle,
"A", "db valid mask");
SYSCTL_ADD_PROC(clist, top, OID_AUTO, "db_mask", CTLTYPE_STRING |
CTLFLAG_RWTUN | CTLFLAG_MPSAFE, tc, 0, sysctl_db_mask_handle,
"A", "db mask");
SYSCTL_ADD_PROC(clist, top, OID_AUTO, "db_event", CTLTYPE_STRING |
CTLFLAG_WR | CTLFLAG_MPSAFE, tc, 0, sysctl_db_event_handle,
"A", "db event");
SYSCTL_ADD_PROC(clist, top, OID_AUTO, "peer_db", CTLTYPE_STRING |
CTLFLAG_RWTUN | CTLFLAG_MPSAFE, tc, 0, sysctl_peer_db_handle,
"A", "peer db");
SYSCTL_ADD_PROC(clist, top, OID_AUTO, "peer_db_mask", CTLTYPE_STRING |
CTLFLAG_RWTUN | CTLFLAG_MPSAFE, tc, 0, sysctl_peer_db_mask_handle,
"IU", "peer db mask info");
if (tc->inspad_cnt != 0) {
for (sidx = 0; sidx < tc->inspad_cnt; sidx++) {
snprintf(buf, sizeof(buf), "spad%d", sidx);
snprintf(desc, sizeof(desc), "spad%d info", sidx);
SYSCTL_ADD_PROC(clist, top, OID_AUTO, buf,
CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
tc, sidx, sysctl_spad_handle, "IU", desc);
}
}
for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
snprintf(buf, sizeof(buf), "peer%d", pidx);
peer = SYSCTL_ADD_NODE(clist, top, OID_AUTO, buf,
CTLFLAG_RW | CTLFLAG_MPSAFE, 0, buf);
peer_top = SYSCTL_CHILDREN(peer);
SYSCTL_ADD_PROC(clist, peer_top, OID_AUTO, "port",
CTLTYPE_UINT | CTLFLAG_RDTUN | CTLFLAG_MPSAFE, tc, pidx,
sysctl_peer_port_number, "IU", "peer port number");
SYSCTL_ADD_PROC(clist, peer_top, OID_AUTO, "link",
CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, tc, pidx,
sysctl_peer_link_handle, "IU", "peer_link info");
SYSCTL_ADD_PROC(clist, peer_top, OID_AUTO, "link_event",
CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, tc, pidx,
sysctl_peer_link_event_handle, "IU", "link event");
for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++) {
snprintf(buf, sizeof(buf), "mw_trans%d", widx);
snprintf(desc, sizeof(desc), "mw trans%d info", widx);
SYSCTL_ADD_PROC(clist, peer_top, OID_AUTO, buf,
CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
&tc->peers[pidx].inmws[widx], 0,
sysctl_mw_trans_handler, "IU", desc);
snprintf(buf, sizeof(buf), "mw%d", widx);
snprintf(desc, sizeof(desc), "mw%d info", widx);
SYSCTL_ADD_PROC(clist, peer_top, OID_AUTO, buf,
CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
&tc->peers[pidx].inmws[widx], 0,
sysctl_mw_handle, "IU", desc);
snprintf(buf, sizeof(buf), "peer_mw%d", widx);
snprintf(desc, sizeof(desc), "peer_mw%d info", widx);
SYSCTL_ADD_PROC(clist, peer_top, OID_AUTO, buf,
CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
&tc->peers[pidx].inmws[widx], 0,
sysctl_peer_mw_handle, "IU", desc);
}
for (sidx = 0; sidx < tc->peers[pidx].outspad_cnt; sidx++) {
snprintf(buf, sizeof(buf), "spad%d", sidx);
snprintf(desc, sizeof(desc), "spad%d info", sidx);
SYSCTL_ADD_PROC(clist, peer_top, OID_AUTO, buf,
CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
tc, sidx, sysctl_peer_spad_handle, "IU", desc);
}
}
}
static int
ntb_tool_probe(device_t dev)
{
device_set_desc(dev, "NTB TOOL");
return (0);
}
static int
ntb_tool_attach(device_t dev)
{
struct tool_ctx *tc = device_get_softc(dev);
int rc = 0;
tc->dev = dev;
rc = tool_check_ntb(tc);
if (rc)
goto out;
tool_init_peers(tc);
rc = tool_init_mws(tc);
if (rc)
goto err_clear_data;
tool_init_spads(tc);
rc = tool_init_ntb(tc);
if (rc)
goto err_clear_spads;
tool_setup_sysctl(tc);
return (0);
err_clear_spads:
tool_clear_spads(tc);
tool_clear_mws(tc);
tool_clear_peers(tc);
err_clear_data:
tool_clear_data(tc);
out:
device_printf(dev, "ntb_tool attached failed with err=(%d).\n", rc);
return (rc);
}
static int
ntb_tool_detach(device_t dev)
{
struct tool_ctx *tc = device_get_softc(dev);
tool_clear_ntb(tc);
tool_clear_spads(tc);
tool_clear_mws(tc);
tool_clear_peers(tc);
tool_clear_data(tc);
return (0);
}
static device_method_t ntb_tool_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, ntb_tool_probe),
DEVMETHOD(device_attach, ntb_tool_attach),
DEVMETHOD(device_detach, ntb_tool_detach),
DEVMETHOD_END
};
devclass_t ntb_tool_devclass;
static DEFINE_CLASS_0(ntb_tool, ntb_tool_driver, ntb_tool_methods,
sizeof(struct tool_ctx));
DRIVER_MODULE(ntb_tool, ntb_hw, ntb_tool_driver, ntb_tool_devclass, NULL, NULL);
MODULE_DEPEND(ntb_tool, ntb, 1, 1, 1);
MODULE_VERSION(ntb_tool, 1.0);
|