-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibxdma.c
More file actions
3744 lines (3206 loc) · 94 KB
/
Copy pathlibxdma.c
File metadata and controls
3744 lines (3206 loc) · 94 KB
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
/******************************************************************************
* Copyright (c) 2020 ASIX Electronic Corporation All rights reserved.
*
* This is unpublished proprietary source code of ASIX Electronic
* Corporation
*
* The copyright notice above does not evidence any actual or intended
* publication of such source code.
*****************************************************************************/
/*
* This file is part of the Xilinx DMA IP Core driver for Linux
*
* Copyright (c) 2016-present, Xilinx, Inc.
* All rights reserved.
*
* This source code is licensed under both the BSD-style license (found in the
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
* in the COPYING file in the root directory of this source tree).
* You may select, at your option, one of the above-listed licenses.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/vmalloc.h>
#include "libxdma.h"
#include "libxdma_api.h"
#include "xdma_mod.h"
#include "switch/ax_ptp.h"
#include "switch/ax_switch.h"
/* SECTION: Module licensing */
#ifdef __LIBXDMA_MOD__
#include "version.h"
#define DRV_MODULE_NAME "AXM57104"
#define DRV_MODULE_DESC "ASIX PCIe NIC Driver"
#define DRV_MODULE_RELDATE "2023/07"
static char version[] =
DRV_MODULE_DESC " " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
MODULE_AUTHOR("Xilinx, Inc.");
MODULE_DESCRIPTION(DRV_MODULE_DESC);
MODULE_VERSION(DRV_MODULE_VERSION);
MODULE_LICENSE("Dual BSD/GPL");
#endif
/* Module Parameters */
static unsigned int enable_credit_mp;
static unsigned int interrupt_mode;
module_param(interrupt_mode, uint, 0644);
MODULE_PARM_DESC(interrupt_mode, "0 - MSI-x , 1 - MSI, 2 - Legacy");
/*
* xdma device management
* maintains a list of the xdma devices
*/
static LIST_HEAD(xdev_list);
static DEFINE_MUTEX(xdev_mutex);
static LIST_HEAD(xdev_rcu_list);
static DEFINE_SPINLOCK(xdev_rcu_lock);
#ifndef list_last_entry
#define list_last_entry(ptr, type, member) \
list_entry((ptr)->prev, type, member)
#endif
#ifdef IRQ_AFFINITY_HINT
u32 channel_vector[3] = {0};
u32 user_vector[8] = {0};
#endif
#ifdef ENABLE_TASKLET
static void tasklet_ptp(unsigned long input);
static struct tasklet_struct *tsk_ptp;
struct ax_private *ax_local_tsk;
#endif
extern u32 ax_read_register(void *iomem);
static inline void xdev_list_add(struct xdma_dev *xdev)
{
mutex_lock(&xdev_mutex);
if (list_empty(&xdev_list)) {
xdev->idx = 0;
} else {
struct xdma_dev *last;
last = list_last_entry(&xdev_list, struct xdma_dev, list_head);
xdev->idx = last->idx + 1;
}
list_add_tail(&xdev->list_head, &xdev_list);
mutex_unlock(&xdev_mutex);
dbg_init("dev %s, xdev 0x%p, xdma idx %d.\n",
dev_name(&xdev->pdev->dev), xdev, xdev->idx);
spin_lock(&xdev_rcu_lock);
list_add_tail_rcu(&xdev->rcu_node, &xdev_rcu_list);
spin_unlock(&xdev_rcu_lock);
}
#undef list_last_entry
static inline void xdev_list_remove(struct xdma_dev *xdev)
{
mutex_lock(&xdev_mutex);
list_del(&xdev->list_head);
mutex_unlock(&xdev_mutex);
spin_lock(&xdev_rcu_lock);
list_del_rcu(&xdev->rcu_node);
spin_unlock(&xdev_rcu_lock);
synchronize_rcu();
}
struct xdma_dev *xdev_find_by_pdev(struct pci_dev *pdev)
{
struct xdma_dev *xdev, *tmp;
mutex_lock(&xdev_mutex);
list_for_each_entry_safe(xdev, tmp, &xdev_list, list_head) {
if (xdev->pdev == pdev) {
mutex_unlock(&xdev_mutex);
return xdev;
}
}
mutex_unlock(&xdev_mutex);
return NULL;
}
EXPORT_SYMBOL_GPL(xdev_find_by_pdev);
//
static inline int debug_check_dev_hndl(const char *fname, struct pci_dev *pdev,
void *hndl)
{
struct xdma_dev *xdev;
if (!pdev)
return -EINVAL;
xdev = xdev_find_by_pdev(pdev);
if (!xdev) {
pr_info("%s pdev 0x%p, hndl 0x%p, NO match found!\n",
fname, pdev, hndl);
return -EINVAL;
}
if (xdev != hndl) {
pr_err("%s pdev 0x%p, hndl 0x%p != 0x%p!\n",
fname, pdev, hndl, xdev);
return -EINVAL;
}
return 0;
}
#ifdef __LIBXDMA_DEBUG__
/* SECTION: Function definitions */
inline void __write_register
(const char *fn, u32 value, void *iomem, unsigned long off)
{
iowrite32(value, iomem);
}
#define write_register(v, mem, off) __write_register(__func__, v, mem, off)
#else
#define write_register(v, mem, off) iowrite32(v, mem)
#endif
inline u32 read_register(void *iomem)
{
return ioread32(iomem);
}
static inline u32 build_u32(u32 hi, u32 lo)
{
return ((hi & 0xFFFFUL) << 16) | (lo & 0xFFFFUL);
}
static inline u64 build_u64(u64 hi, u64 lo)
{
return ((hi & 0xFFFFFFFULL) << 32) | (lo & 0xFFFFFFFFULL);
}
static void check_nonzero_interrupt_status(struct xdma_dev *xdev)
{
struct interrupt_regs *reg = (struct interrupt_regs *)
(xdev->bar[xdev->config_bar_idx] + XDMA_OFS_INT_CTRL);
u32 w;
w = read_register(®->user_int_enable);
if (w) {
pr_info("%s xdma%d user_int_enable = 0x%08x\n",
dev_name(&xdev->pdev->dev), xdev->idx, w);
}
w = read_register(®->channel_int_enable);
if (w) {
pr_info("%s xdma%d channel_int_enable = 0x%08x\n",
dev_name(&xdev->pdev->dev), xdev->idx, w);
}
w = read_register(®->user_int_request);
if (w) {
pr_info("%s xdma%d user_int_request = 0x%08x\n",
dev_name(&xdev->pdev->dev), xdev->idx, w);
}
w = read_register(®->channel_int_request);
if (w) {
pr_info("%s xdma%d channel_int_request = 0x%08x\n",
dev_name(&xdev->pdev->dev), xdev->idx, w);
}
w = read_register(®->user_int_pending);
if (w) {
pr_info("%s xdma%d user_int_pending = 0x%08x\n",
dev_name(&xdev->pdev->dev), xdev->idx, w);
}
w = read_register(®->channel_int_pending);
if (w) {
pr_info("%s xdma%d channel_int_pending = 0x%08x\n",
dev_name(&xdev->pdev->dev), xdev->idx, w);
}
}
/* channel_interrupts_enable -- Enable interrupts we are interested in */
static void channel_interrupts_enable(struct xdma_dev *xdev, u32 mask)
{
struct interrupt_regs *reg = (struct interrupt_regs *)
(xdev->bar[xdev->config_bar_idx] + XDMA_OFS_INT_CTRL);
write_register(mask, ®->channel_int_enable_w1s, XDMA_OFS_INT_CTRL);
}
#ifdef CONFIG_NAPI
static void channel_interrupts_enable_polling(struct xdma_dev *xdev, u32 mask)
{
struct interrupt_regs *reg = (struct interrupt_regs *)
(xdev->bar[xdev->config_bar_idx] + XDMA_OFS_INT_CTRL);
iowrite32(mask, ®->channel_int_enable_w1s);
}
#endif
static void msi_x_C2H_channel_interrupts_disable
(struct xdma_engine *engine, u32 mask)
{
write_register(mask,
&engine->regs->interrupt_enable_mask_w1c,
(unsigned long)
(&engine->regs->interrupt_enable_mask_w1c) -
(unsigned long)(&engine->regs));
}
static void msi_x_C2H_channel_interrupts_enable
(struct xdma_engine *engine, u32 mask)
{
write_register(mask,
&engine->regs->interrupt_enable_mask_w1s,
(unsigned long)
(&engine->regs->interrupt_enable_mask_w1s) -
(unsigned long)(&engine->regs));
}
static void msi_x_H2C_channel_interrupts_disable
(struct xdma_engine *engine, u32 mask)
{
write_register(mask,
&engine->regs->interrupt_enable_mask_w1c,
(unsigned long)
(&engine->regs->interrupt_enable_mask_w1c) -
(unsigned long)(&engine->regs));
}
static void msi_x_H2C_channel_interrupts_enable
(struct xdma_engine *engine, u32 mask)
{
write_register(mask,
&engine->regs->interrupt_enable_mask_w1s,
(unsigned long)
(&engine->regs->interrupt_enable_mask_w1s) -
(unsigned long)(&engine->regs));
}
/* channel_interrupts_disable -- Disable interrupts we not interested in */
static void channel_interrupts_disable(struct xdma_dev *xdev, u32 mask)
{
struct interrupt_regs *reg = (struct interrupt_regs *)
(xdev->bar[xdev->config_bar_idx] + XDMA_OFS_INT_CTRL);
write_register(mask, ®->channel_int_enable_w1c, XDMA_OFS_INT_CTRL);
}
/* user_interrupts_enable -- Enable interrupts we are interested in */
static void user_interrupts_enable(struct xdma_dev *xdev, u32 mask)
{
struct interrupt_regs *reg = (struct interrupt_regs *)
(xdev->bar[xdev->config_bar_idx] + XDMA_OFS_INT_CTRL);
write_register(mask, ®->user_int_enable_w1s, XDMA_OFS_INT_CTRL);
}
/* user_interrupts_disable -- Disable interrupts we not interested in */
static void user_interrupts_disable(struct xdma_dev *xdev, u32 mask)
{
struct interrupt_regs *reg = (struct interrupt_regs *)
(xdev->bar[xdev->config_bar_idx] + XDMA_OFS_INT_CTRL);
write_register(mask, ®->user_int_enable_w1c, XDMA_OFS_INT_CTRL);
}
/* read_interrupts -- Print the interrupt controller status */
static u32 read_interrupts(struct xdma_dev *xdev)
{
struct interrupt_regs *reg = (struct interrupt_regs *)
(xdev->bar[xdev->config_bar_idx] + XDMA_OFS_INT_CTRL);
u32 lo;
u32 hi;
/* extra debugging; inspect complete engine set of registers */
hi = read_register(®->user_int_request);
dbg_io("ioread32(0x%p) returned 0x%08x (user_int_request).\n",
®->user_int_request, hi);
lo = read_register(®->channel_int_request);
dbg_io("ioread32(0x%p) returned 0x%08x (channel_int_request)\n",
®->channel_int_request, lo);
/* return interrupts: user in upper 16-bits, channel in lower 16-bits */
return build_u32(hi, lo);
}
static void engine_reg_dump(struct xdma_engine *engine)
{
u32 w;
BUG_ON(!engine);
w = read_register(&engine->regs->identifier);
pr_info("%s: ioread32(0x%p) = 0x%08x (id).\n",
engine->name, &engine->regs->identifier, w);
w &= BLOCK_ID_MASK;
if (w != BLOCK_ID_HEAD) {
pr_info("%s: engine id missing, 0x%08x exp.& 0x%x = 0x%x\n",
engine->name, w, BLOCK_ID_MASK, BLOCK_ID_HEAD);
return;
}
/* extra debugging; inspect complete engine set of registers */
w = read_register(&engine->regs->status);
pr_info("%s: ioread32(0x%p) = 0x%08x (status).\n",
engine->name, &engine->regs->status, w);
w = read_register(&engine->regs->control);
pr_info("%s: ioread32(0x%p) = 0x%08x (control)\n",
engine->name, &engine->regs->control, w);
w = read_register(&engine->sgdma_regs->first_desc_lo);
pr_info("%s: ioread32(0x%p) = 0x%08x (first_desc_lo)\n",
engine->name, &engine->sgdma_regs->first_desc_lo, w);
w = read_register(&engine->sgdma_regs->first_desc_hi);
pr_info("%s: ioread32(0x%p) = 0x%08x (first_desc_hi)\n",
engine->name, &engine->sgdma_regs->first_desc_hi, w);
w = read_register(&engine->sgdma_regs->first_desc_adjacent);
pr_info("%s: ioread32(0x%p) = 0x%08x (first_desc_adjacent).\n",
engine->name, &engine->sgdma_regs->first_desc_adjacent, w);
w = read_register(&engine->regs->completed_desc_count);
pr_info("%s: ioread32(0x%p) = 0x%08x (completed_desc_count).\n",
engine->name, &engine->regs->completed_desc_count, w);
w = read_register(&engine->regs->interrupt_enable_mask);
pr_info("%s: ioread32(0x%p) = 0x%08x (interrupt_enable_mask)\n",
engine->name, &engine->regs->interrupt_enable_mask, w);
}
/**
* engine_status_read() - read status of SG DMA engine (optionally reset)
*
* Stores status in engine->status.
*
* @return -1 on failure, status register otherwise
*/
static void engine_status_dump(struct xdma_engine *engine)
{
u32 v = engine->status;
char buffer[256];
char *buf = buffer;
int len = 0;
len = sprintf(buf, "SG engine %s status: 0x%08x: ", engine->name, v);
if ((v & XDMA_STAT_BUSY)) {
len += sprintf(buf + len, "BUSY,");
}
if ((v & XDMA_STAT_DESC_STOPPED)) {
len += sprintf(buf + len, "DESC_STOPPED,");
}
if ((v & XDMA_STAT_DESC_COMPLETED)) {
len += sprintf(buf + len, "DESC_COMPL,");
}
/* common H2C & C2H */
if ((v & XDMA_STAT_COMMON_ERR_MASK)) {
if ((v & XDMA_STAT_ALIGN_MISMATCH)) {
len += sprintf(buf + len, "ALIGN_MISMATCH ");
}
if ((v & XDMA_STAT_MAGIC_STOPPED)) {
len += sprintf(buf + len, "MAGIC_STOPPED ");
}
if ((v & XDMA_STAT_INVALID_LEN)) {
len += sprintf(buf + len, "INVLIAD_LEN ");
}
if ((v & XDMA_STAT_IDLE_STOPPED)) {
len += sprintf(buf + len, "IDLE_STOPPED ");
}
buf[len - 1] = ',';
}
if ((engine->dir == DMA_TO_DEVICE)) {
/* H2C only */
if ((v & XDMA_STAT_H2C_R_ERR_MASK)) {
len += sprintf(buf + len, "R:");
if ((v & XDMA_STAT_H2C_R_UNSUPP_REQ)) {
len += sprintf(buf + len, "UNSUPP_REQ ");
}
if ((v & XDMA_STAT_H2C_R_COMPL_ABORT)) {
len += sprintf(buf + len, "COMPL_ABORT ");
}
if ((v & XDMA_STAT_H2C_R_PARITY_ERR)) {
len += sprintf(buf + len, "PARITY ");
}
if ((v & XDMA_STAT_H2C_R_HEADER_EP)) {
len += sprintf(buf + len, "HEADER_EP ");
}
if ((v & XDMA_STAT_H2C_R_UNEXP_COMPL)) {
len += sprintf(buf + len, "UNEXP_COMPL ");
}
buf[len - 1] = ',';
}
if ((v & XDMA_STAT_H2C_W_ERR_MASK)) {
len += sprintf(buf + len, "W:");
if ((v & XDMA_STAT_H2C_W_DECODE_ERR)) {
len += sprintf(buf + len, "DECODE_ERR ");
}
if ((v & XDMA_STAT_H2C_W_SLAVE_ERR)) {
len += sprintf(buf + len, "SLAVE_ERR ");
}
buf[len - 1] = ',';
}
} else {
/* C2H only */
if ((v & XDMA_STAT_C2H_R_ERR_MASK)) {
len += sprintf(buf + len, "R:");
if ((v & XDMA_STAT_C2H_R_DECODE_ERR)) {
len += sprintf(buf + len, "DECODE_ERR ");
}
if ((v & XDMA_STAT_C2H_R_SLAVE_ERR)) {
len += sprintf(buf + len, "SLAVE_ERR ");
}
buf[len - 1] = ',';
}
}
/* common H2C & C2H */
if ((v & XDMA_STAT_DESC_ERR_MASK)) {
len += sprintf(buf + len, "DESC_ERR:");
if ((v & XDMA_STAT_DESC_UNSUPP_REQ)) {
len += sprintf(buf + len, "UNSUPP_REQ ");
}
if ((v & XDMA_STAT_DESC_COMPL_ABORT)) {
len += sprintf(buf + len, "COMPL_ABORT ");
}
if ((v & XDMA_STAT_DESC_PARITY_ERR)) {
len += sprintf(buf + len, "PARITY ");
}
if ((v & XDMA_STAT_DESC_HEADER_EP)) {
len += sprintf(buf + len, "HEADER_EP ");
}
if ((v & XDMA_STAT_DESC_UNEXP_COMPL)) {
len += sprintf(buf + len, "UNEXP_COMPL ");
}
buf[len - 1] = ',';
}
buf[len - 1] = '\0';
pr_info("%s\n", buffer);
}
u32 engine_status_read(struct xdma_engine *engine, bool clear, bool dump)
{
u32 value;
BUG_ON(!engine);
if (dump) {
engine_reg_dump(engine);
}
/* read status register */
if (clear) {
#if 0
value = engine->status =
read_register(&engine->regs->status_rc);
#else
value = engine->status =
read_register(&engine->regs->status);
write_register(value, &engine->regs->status, 0);
#endif
} else
value = engine->status = read_register(&engine->regs->status);
if (dump) {
engine_status_dump(engine);
}
return value;
}
/**
* xdma_engine_stop() - stop an SG DMA engine
*
*/
void xdma_engine_stop(struct xdma_engine *engine)
{
u32 w;
BUG_ON(!engine);
ASIX_DEBUG("xdma_engine_stop(engine=%p), call by %s\n");
w = 0;
w |= (u32)XDMA_CTRL_IE_DESC_ALIGN_MISMATCH;
w |= (u32)XDMA_CTRL_IE_MAGIC_STOPPED;
w |= (u32)XDMA_CTRL_IE_READ_ERROR;
w |= (u32)XDMA_CTRL_IE_DESC_ERROR;
w |= (u32)XDMA_CTRL_IE_DESC_STOPPED;
w |= (u32)XDMA_CTRL_IE_DESC_COMPLETED;
dbg_tfr("Stopping SG DMA %s engine; writing 0x%08x to 0x%p.\n",
engine->name, w, (u32 *)&engine->regs->control);
write_register(w, &engine->regs->control,
(unsigned long)(&engine->regs->control) -
(unsigned long)(&engine->regs));
/* dummy read of status register to flush all previous writes */
dbg_tfr("xdma_engine_stop(%s) done\n", engine->name);
engine->running = 0;
}
static void engine_start_mode_config(struct xdma_engine *engine)
{
u32 w = 0;
BUG_ON(!engine);
w = XDMA_CTRL_IE_DESC_STOPPED;
w |= XDMA_CTRL_IE_DESC_COMPLETED;
w |= XDMA_CTRL_IE_DESC_ALIGN_MISMATCH;
w |= XDMA_CTRL_IE_READ_ERROR;
w |= XDMA_CTRL_IE_DESC_ERROR;
write_register(w, &engine->regs->interrupt_enable_mask,
(unsigned long)(&engine->regs->interrupt_enable_mask) -
(unsigned long)(&engine->regs));
/* write control register of SG DMA engine */
w = (u32)XDMA_CTRL_RUN_STOP;
w |= (u32)XDMA_CTRL_IE_READ_ERROR;
w |= (u32)XDMA_CTRL_IE_DESC_ERROR;
w |= (u32)XDMA_CTRL_IE_DESC_ALIGN_MISMATCH;
w |= (u32)XDMA_CTRL_IE_DESC_STOPPED;
w |= (u32)XDMA_CTRL_IE_DESC_COMPLETED;
dbg_tfr("iowrite32(0x%08x to 0x%p) (control)\n", w,
(void *)&engine->regs->control);
/* start the engine */
write_register(w, &engine->regs->control,
(unsigned long)(&engine->regs->control) -
(unsigned long)(&engine->regs));
/* dummy read of status register to flush all previous writes */
w = read_register(&engine->regs->status);
dbg_tfr("ioread32(0x%p) = 0x%08x (dummy read flushes writes).\n",
&engine->regs->status, w);
}
/**
* engine_start() - start an idle engine with its first transfer on queue
*
* The engine will run and process all transfers that are queued using
* transfer_queue() and thus have their descriptor lists chained.
*
* During the run, new transfers will be processed if transfer_queue() has
* chained the descriptors before the hardware fetches the last descriptor.
* A transfer that was chained too late will invoke a new run of the engine
* initiated from the engine_service() routine.
*
* The engine must be idle and at least one transfer must be queued.
* This function does not take locks; the engine spinlock must already be
* taken.
*
*/
struct xdma_transfer *engine_start(struct xdma_engine *engine)
{
u32 w;
int extra_adj = 0;
dma_addr_t desc_bus;
struct xdma_transfer *transfer = engine->transfer;
if (transfer == NULL)
return 0;
desc_bus = transfer->desc_bus;
/* engine must be idle */
BUG_ON(engine->running);
BUG_ON(!transfer);
/* engine is no longer shutdown */
engine->shutdown = ENGINE_SHUTDOWN_NONE;
dbg_tfr("engine_start(%s): transfer=0x%p.\n"
, engine->name, transfer);
/* initialize number of descriptors of dequeued transfers */
engine->desc_dequeued = 0;
desc_bus = transfer->list_desc[transfer->current_list];
/* write lower 32-bit of bus address of transfer first descriptor */
w = cpu_to_le32(PCI_DMA_L(desc_bus));
dbg_tfr("iowrite32(0x%08x to 0x%p) (first_desc_lo)\n", w,
(void *)&engine->sgdma_regs->first_desc_lo);
write_register(w, &engine->sgdma_regs->first_desc_lo,
(unsigned long)(&engine->sgdma_regs->first_desc_lo) -
(unsigned long)(&engine->sgdma_regs));
/* write upper 32-bit of bus address of transfer first descriptor */
w = cpu_to_le32(PCI_DMA_H(desc_bus));
dbg_tfr("iowrite32(0x%08x to 0x%p) (first_desc_hi)\n", w,
(void *)&engine->sgdma_regs->first_desc_hi);
write_register(w, &engine->sgdma_regs->first_desc_hi,
(unsigned long)(&engine->sgdma_regs->first_desc_hi) -
(unsigned long)(&engine->sgdma_regs));
dbg_tfr("iowrite32(0x%08x to 0x%p) (first_desc_adjacent)\n",
extra_adj, (void *)&engine->sgdma_regs->first_desc_adjacent);
write_register(extra_adj, &engine->sgdma_regs->first_desc_adjacent,
(unsigned long)(&engine->sgdma_regs->first_desc_adjacent) -
(unsigned long)(&engine->sgdma_regs));
dbg_tfr("ioread32(0x%p) (dummy read flushes writes).\n",
&engine->regs->status);
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 1, 0)
mmiowb();
#endif
engine_start_mode_config(engine);
dbg_tfr("%s engine 0x%p now running\n", engine->name, engine);
/* remember the engine is running */
engine->running = 1;
return transfer;
}
#if 0
#ifndef arch_msi_check_device //remove from kernel
int arch_msi_check_device(struct pci_dev *dev, int nvec, int type)
{
return 0;
}
#endif
#endif
/* type = PCI_CAP_ID_MSI or PCI_CAP_ID_MSIX */
static int msi_msix_capable(struct pci_dev *dev, int type)
{
struct pci_bus *bus;
// int ret;
if (!dev || dev->no_msi)
return 0;
for (bus = dev->bus; bus; bus = bus->parent)
if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
return 0;
/*
ret = arch_msi_check_device(dev, 1, type);
if (ret)
return 0;
*/
if (!pci_find_capability(dev, type))
return 0;
return 1;
}
static irqreturn_t msix_user_irq_service
(int irq, struct xdma_user_irq *user_irq)
{
BUG_ON(!user_irq);
if (user_irq->handler) {
return IRQ_HANDLED;
}
return IRQ_HANDLED;
}
static irqreturn_t user_irq_service(int irq, struct xdma_user_irq *user_irq,
struct ax_private *ax_local)
{
BUG_ON(!user_irq);
if (user_irq->handler) {
return user_irq->handler(user_irq->user_idx, (void *)ax_local);
}
return IRQ_HANDLED;
}
#if 0
void showpktPTPInfo(PSKB_TSTAMP_MSG pSkbptp, int idx)
{
struct ax_switch *pSwitch = pSkbptp->pSwitch;
struct sk_buff *skb = pSkbptp->skb;
struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
unsigned char ptp_header[PTP_HDR_SIZE];
int ptp_msg_offset = pSkbptp->ptp_msg_offset;
CAPTURE_DATA ts_d;
skb_copy_from_linear_data_offset(skb, ptp_msg_offset,
ptp_header, PTP_HDR_SIZE);
ts_d.msg_type = *(ptp_header + PTP_MSG_TYPE_OFFSET) & 0x0F;
ts_d.sequence_id = ntohs(*(u16 *)(ptp_header + PTP_SEQ_ID_OFFSET));
ts_d.domain_number = *(ptp_header + PTP_SUBDOMAIN_OFFSET) & 0xFF;
ts_d.port_id = pSkbptp->port_tag;
printk("[PKT INFO,%d] port_id: ts_0x%x\n",idx,ts_d.port_id);
printk("[PKT INFO,%d] msg_type: ts_0x%x\n",idx,ts_d.msg_type);
printk("[PKT INFO,%d] sequence_id: ts_0x%x\n",idx,ts_d.sequence_id);
printk("[PKT INFO,%d] domain_number: ts_0x%x\n",idx,ts_d.domain_number);
}
#endif
static void
ax_rx_check_timestamp(struct sk_buff *skb, struct ax_switch *pSwitch, struct ax_private *ax_local)
{
const struct ethhdr *eth;
SKB_TSTAMP_MSG msg;
u16 tmp, rx_ethertype, source_port;
struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
u64 sec, nsec;
u64 time64;
unsigned long flags;
msg.skb = skb;
msg.pSwitch = pSwitch;
rx_ethertype = ntohs(skb->protocol);
if (rx_ethertype == ETH_P_XDSA) {
eth = eth_hdr(skb);
rx_ethertype = ntohs(eth->h_proto);
if (rx_ethertype == AX_SDSA) {
skb_copy_from_linear_data_offset
(skb, AX_RX_PTPHDR_OFFSET_L2 + 2, &tmp, 2);
source_port = (ntohs(tmp) & 0x1FF8) >> 3;
skb_copy_from_linear_data_offset (skb,
AX_RX_PTPHDR_OFFSET_L2 + AX_SDSA_TAG_LENGTH_RX,
&tmp,
2);
rx_ethertype = ntohs(tmp);
if (rx_ethertype == ETH_P_1588) {
u8 msg_type;
skb_copy_from_linear_data_offset(skb,
AX_RX_PTPHDR_OFFSET_L2 + AX_SDSA_TAG_LENGTH_RX + 2,
&msg_type,
1);
if ((msg_type & 0xf) <= 7) {
msg.ptp_msg_offset =
AX_RX_PTPHDR_OFFSET_L2 +
AX_SDSA_TAG_LENGTH_RX + 2;
msg.ptp_vlan_id = 0;
msg.port_tag = source_port;
spin_lock_irqsave(&ax_local->txrx_timestamp_lock,flags);
ax_retrieve_rx_hw_timestamps(pSwitch);
//spin_unlock_irqrestore(&ax_local->txrx_timestamp_lock,flags);
ax_tsn_rx_hwtstamp(&msg);
spin_unlock_irqrestore(&ax_local->txrx_timestamp_lock,flags);
}
} else if (rx_ethertype == ETH_P_NET_LATENCY) {/*Netlatency Rx side as AXM57104*/
sec = (u64)read_register(ax_local->xdev->bar[2] + 0x114);
nsec = (u64)read_register(ax_local->xdev->bar[2] + 0x110);
time64 = (sec * 1000000000) + nsec;
shhwtstamps->hwtstamp = ns_to_ktime(time64);
} else if (rx_ethertype == ETH_P_IP) {
u8 transport_poto;
u16 dst_port;
skb_copy_from_linear_data_offset(skb,
AX_IP_PROTO_OFFSET + AX_SDSA_TAG_LENGTH_RX + 2,
&transport_poto, 1);
if (transport_poto == IPPROTO_UDP) {
skb_copy_from_linear_data_offset(skb,
AX_UDP_PORT_OFFSET + AX_SDSA_TAG_LENGTH_RX + 2,
&dst_port, 2);
if (ntohs(dst_port) ==
AX_PTP_EVENT_PORT_NUM) {
msg.ptp_msg_offset =
AX_RX_PTPHDR_OFFSET_L3 +
AX_SDSA_TAG_LENGTH_RX +
2;
msg.ptp_vlan_id = 0;
msg.port_tag = source_port;
spin_lock_irqsave(&ax_local->txrx_timestamp_lock,flags);
ax_retrieve_rx_hw_timestamps(pSwitch);
//spin_unlock_irqrestore(&ax_local->txrx_timestamp_lock,flags);
ax_tsn_rx_hwtstamp(&msg);
spin_unlock_irqrestore(&ax_local->txrx_timestamp_lock,flags);
}
}
}
}
}
}
static void
ax_tx_check_timestamp(struct sk_buff *skb, struct ax_switch *pSwitch)
{
u16 tmp, tx_ethertype, port_id = 0;
SKB_TSTAMP_MSG msg;
u8 msg_type;
msg.skb = skb;
msg.pSwitch = pSwitch;
if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
skb_copy_from_linear_data_offset(skb, AX_ETHTYPE_OFFSET, &tmp,2);
tx_ethertype = ntohs(tmp);
if (tx_ethertype == AX_SDSA) {
skb_copy_from_linear_data_offset(skb,AX_ETHTYPE_OFFSET + 4,&tmp, 2);
port_id = (ntohs(tmp) & 0x1FF8) >> 3;
skb_copy_from_linear_data_offset(skb,AX_ETHTYPE_OFFSET + AX_SDSA_TAG_LENGTH_TX, &tmp,2);
tx_ethertype = ntohs(tmp);
msg.ptp_vlan_id = 0;
msg.port_tag = port_id;
if (tx_ethertype == ETH_P_1588) {
skb_copy_from_linear_data_offset(skb,AX_ETHTYPE_OFFSET + AX_SDSA_TAG_LENGTH_TX + 2,&msg_type,1);
if ((msg_type & 0xF) <= 7) {
msg.ptp_msg_offset =
AX_TX_PTPHDR_OFFSET_L2
+ AX_SDSA_TAG_LENGTH_TX;
ax_tsn_tx_hwtstamp(&msg);
}
} else if (tx_ethertype == ETH_P_IP) {
msg.ptp_msg_offset =
AX_TX_PTPHDR_OFFSET_L3 + AX_SDSA_TAG_LENGTH_TX;
ax_tsn_tx_hwtstamp(&msg);
}
}
}
}
/**
* eth_type_trans - determine the packet's protocol ID.
* @skb: received socket data
* @dev: receiving network device
*
* The rule here is that we
* assume 802.3 if the type field is short enough to be a length.
* This is normal practice and works for any 'now in use' protocol.
*/
static __be16 ax_eth_type_trans(struct sk_buff *skb,
struct ax_private *ax_local)
{
struct net_device *dev = ax_local->dev;
unsigned short _service_access_point;
const unsigned short *sap;
const struct ethhdr *eth;
skb->dev = dev;
skb_reset_mac_header(skb);
eth = (struct ethhdr *)skb->data;
skb_pull_inline(skb, ETH_HLEN);
if (unlikely(!ether_addr_equal_64bits(eth->h_dest,
dev->dev_addr))) {
if (unlikely(is_multicast_ether_addr_64bits(eth->h_dest))) {
if (ether_addr_equal_64bits(eth->h_dest,
dev->broadcast)) {
skb->pkt_type = PACKET_BROADCAST;
} else {
skb->pkt_type = PACKET_MULTICAST;
}
} else {
skb->pkt_type = PACKET_OTHERHOST;
}
}
/*
* Some variants of DSA tagging don't have an ethertype field
* at all, so we check here whether one of those tagging
* variants has been configured on the receiving interface,
* and if so, set skb->protocol without looking at the packet.
*/
//if (unlikely(netdev_uses_dsa(dev))) {
if (unlikely(netdev_uses_dsa(dev))){
if (eth->h_proto == 0xDCDC) {
return htons(ETH_P_XDSA);
}
}
if (likely(eth_proto_is_802_3(eth->h_proto))) {
return eth->h_proto;
}
/*
*This is a magic hack to spot IPX packets. Older Novell breaks
*the protocol design and runs IPX over 802.3 without an 802.2 LLC
*layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
*won't work for fault tolerant netware but does for the rest.
*/
sap = skb_header_pointer(skb, 0, sizeof(*sap), &_service_access_point);
if (sap && *sap == 0xFFFF) {
return htons(ETH_P_802_3);
}
/*
* Real 802.2 LLC
*/
return htons(ETH_P_802_2);
}
static void ax_net_rx(struct ax_private *ax_local, int *work_done, int budget)
{
struct xdma_dev *xdev = ax_local->xdev;
struct xdma_engine *engine = &xdev->engine_c2h[0];
struct xdma_result *cyclic_result = engine->cyclic_result;
struct sk_buff *skb;
#ifdef CONFIG_NAPI
struct napi_struct *napi = &ax_local->napi;
#else
struct net_device *netdev = ax_local->dev;
#endif
struct packet_buff *rx_buff = engine->rx_buff;
u32 entry;
u32 length;
#ifdef ASIX_GPTP_DEBUG
u16 rx_ethertype, tmp;
const struct ethhdr *eth;
struct timespec64 tmv_64;
u64 nanoSec;
#endif
entry = ax_local->cur_rx;
ASIX_DEBUG("[%s %d] cyclic_result status = %x ",
__FUNCTION__,
__LINE__,
cyclic_result[entry].status);
while (((cyclic_result[entry].status & 0xFFFF0000) == C2H_WB)
&& budget) {
if (*work_done >= budget) {
break;
}
length = cyclic_result[entry].length;
#ifdef CONFIG_NAPI
skb = napi_alloc_skb(napi, length + 2);
#else
skb = netdev_alloc_skb(netdev, length + 2);
#endif
if (skb == NULL) {
ax_local->cur_rx = entry;
return;
}
skb_reserve(skb, 2);
memcpy(skb->data, rx_buff[entry].data, length);
#if 0
printk("Rx:");
print_hex_dump(KERN_DEBUG,"",DUMP_PREFIX_NONE,16,1,skb->data,length,0);
printk("\r\n");
#endif
skb_put(skb, length);
skb->protocol = ax_eth_type_trans(skb, ax_local);
#ifdef ASIX_GPTP_DEBUG
rx_ethertype = ntohs(skb->protocol);
if(rx_ethertype == ETH_P_XDSA)
{
eth = eth_hdr(skb);
rx_ethertype = ntohs(eth->h_proto);
if (rx_ethertype == AX_SDSA) {
skb_copy_from_linear_data_offset(skb, AX_RX_PTPHDR_OFFSET_L2 + 2, &tmp, 2);
skb_copy_from_linear_data_offset (skb,AX_RX_PTPHDR_OFFSET_L2 + AX_SDSA_TAG_LENGTH_RX,
&tmp,2);
rx_ethertype = ntohs(tmp);
if (rx_ethertype == ETH_P_1588)
{
u8 msg_type;
skb_copy_from_linear_data_offset(skb,