-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathswitchtec_dma.c
More file actions
3418 lines (2790 loc) · 83.7 KB
/
switchtec_dma.c
File metadata and controls
3418 lines (2790 loc) · 83.7 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
// SPDX-License-Identifier: GPL-2.0
/*
* Microchip Switchtec(tm) DMA Controller Driver
* Copyright (c) 2019, Kelvin Cao <kelvin.cao@microchip.com>
* Copyright (c) 2019, Microchip Corporation
*/
#include "../drivers/dma/dmaengine.h"
#include <linux/circ_buf.h>
#include <linux/dmaengine.h>
#include <linux/kref.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include "linux/switchtec_fabric_dma.h"
#include "version.h"
MODULE_DESCRIPTION("Switchtec PCIe Switch DMA Engine");
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Kelvin Cao");
static char *se_thresh_policy = "";
module_param(se_thresh_policy, charp, 0644);
MODULE_PARM_DESC(se_thresh_policy, "<thresh-policy>\n"
" Syntax: <se_threshold>@<channel-index>[,<se_thresholdx>@<channel-a>,...]\n"
" e.g. c@1,d@2,6@5\n"
" c@1 => 'config' policy - use previously configured threshold value for channel 1\n"
" d@2 => 'driver' policy - ensure threshold value is less than se_buf_len for channel 2\n"
" 6@5 => 'user' policy - set user-provided threshold value 6 for channel 5\n"
" NOTE: Unspecified channels use driver policy"
);
enum switchtec_reg_offset {
SWITCHTEC_DMAC_VERSION_OFFSET = 0,
SWITCHTEC_DMAC_CAPABILITY_OFFSET = 0x80,
SWITCHTEC_DMAC_STATUS_OFFSET = 0x100,
SWITCHTEC_DMAC_CONTROL_OFFSET = 0x180,
SWITCHTEC_DMAC_CHAN_CTRL_OFFSET = 0x1000,
SWITCHTEC_DMAC_CHAN_CFG_STS_OFFSET = 0x160000,
SWITCHTEC_DMAC_FABRIC_CMD_OFFSET = 0x164000,
SWITCHTEC_DMAC_FABRIC_CTRL_OFFSET = 0x165000,
};
#define SWITCHTEC_DESC_MAX_SIZE 0x100000
struct dmac_version_regs {
u32 fw_ver;
u32 dma_prot_ver;
u32 hw_dmac_ver;
} __packed;
struct dmac_capability_regs {
u32 cap;
u32 chan_cnt;
u32 rsvd1;
u32 cplt_tmo;
u32 tag_limit;
u16 chan_sts_vec;
u16 rsvd2;
u16 se_buf_cnt;
u16 se_buf_base;
} __packed;
struct dmac_status_regs {
u32 state;
u32 internal_err;
u32 chan_halt_sum_lo;
u32 chan_halt_sum_hi;
u32 rsvd[2];
u32 chan_pause_sum_lo;
u32 chan_pause_sum_hi;
} __packed;
#define IER_OB_PF_RD_ERR_I BIT(14)
#define IER_OB_TLP_RD_ERR_I BIT(15)
#define IER_ECC_ER_0_I BIT(20)
#define IER_ECC_ER_1_I BIT(21)
#define IER_PARITY_ERR_I BIT(22)
#define IER_IB_IER_I BIT(23)
struct dmac_control_regs {
u32 reset_halt;
} __packed;
struct dmac_fabric_cmd_regs {
u32 input[256];
u32 rsvd1[256];
u16 command;
u16 rsvd2;
} __packed;
struct dmac_fabric_control_regs {
u16 cmd_vec;
u16 rsvd1;
u32 cmd_dma_addr_lo;
u32 cmd_dma_addr_hi;
u16 event_vec;
u16 rsvd2;
u32 event_dma_addr_lo;
u32 event_dma_addr_hi;
u32 event_dma_size;
u32 event_queue_tail;
u32 cmd_event_enable;
u16 local_hfid;
u16 rsvd3;
u32 requestor_id;
} __packed;
#define SWITCHTEC_CHAN_CTRL_PAUSE BIT(0)
#define SWITCHTEC_CHAN_CTRL_HALT BIT(1)
#define SWITCHTEC_CHAN_CTRL_RESET BIT(2)
#define SWITCHTEC_CHAN_CTRL_ERR_PAUSE BIT(3)
#define SWITCHTEC_CHAN_STS_PAUSED BIT(9)
#define SWITCHTEC_CHAN_STS_HALTED BIT(10)
#define SWITCHTEC_CHAN_STS_PAUSED_MASK GENMASK(29, 13)
static const char *channel_status_str[] = {
[13] = "received a VDM with length error status",
[14] = "received a VDM or Cpl with Unsupported Request error status",
[15] = "received a VDM or Cpl with Completion Abort error status",
[16] = "received a VDM with ECRC error status",
[17] = "received a VDM with EP error status",
[18] = "received a VDM with Reserved Cpl error status",
[19] = "received only part of split SE CplD",
[20] = "the ISP_DMAC detected a Completion Time Out",
[21] = "received a Cpl with Unsupported Request status",
[22] = "received a Cpl with Completion Abort status",
[23] = "received a Cpl with a reserved status",
[24] = "received a TLP with ECRC error status in its metadata",
[25] = "received a TLP with the EP bit set in the header",
[26] = "the ISP_DMAC tried to process a SE with an invalid Connection ID",
[27] = "the ISP_DMAC tried to process a SE with an invalid Remote Host interrupt",
[28] = "a reserved opcode was detected in an SE",
[29] = "received a SE Cpl with error status",
};
struct chan_hw_regs {
u16 cq_head;
u16 rsvd1;
u16 sq_tail;
u16 rsvd2;
u8 ctrl;
u8 rsvd3[3];
u16 status;
u16 rsvd4;
} __packed;
enum {
PERF_BURST_SCALE = 0x1,
PERF_BURST_SIZE = 0x6,
PERF_INTERVAL = 0x0,
PERF_MRRS = 0x3,
PERF_ARB_WEIGHT = 0x1,
};
enum {
PERF_BURST_SCALE_SHIFT = 2,
PERF_BURST_SCALE_MASK = 0x3,
PERF_MRRS_SHIFT = 4,
PERF_MRRS_MASK = 0x7,
PERF_INTERVAL_SHIFT = 8,
PERF_INTERVAL_MASK = 0x7,
PERF_BURST_SIZE_SHIFT = 12,
PERF_BURST_SIZE_MASK = 0x7,
PERF_ARB_WEIGHT_SHIFT = 24,
PERF_ARB_WEIGHT_MASK = 0xff,
};
enum {
PERF_MIN_INTERVAL = 0,
PERF_MAX_INTERVAL = 7,
PERF_MIN_BURST_SIZE = 0,
PERF_MAX_BURST_SIZE = 7,
PERF_MIN_BURST_SCALE = 0,
PERF_MAX_BURST_SCALE = 2,
PERF_MIN_MRRS = 0,
PERF_MAX_MRRS = 7,
};
enum {
SE_BUF_BASE_SHIFT = 2,
SE_BUF_BASE_MASK = 0x1ff,
SE_BUF_LEN_SHIFT = 12,
SE_BUF_LEN_MASK = 0x1ff,
SE_THRESH_SHIFT = 23,
SE_THRESH_MASK = 0x1ff,
};
#define SWITCHTEC_CHAN_ENABLE BIT(1)
#define SWITCHTEC_LAT_SE_FETCH BIT(0)
#define SWITCHTEC_LAT_VDM BIT(1)
#define SWITCHTEC_LAT_RD_IMM BIT(2)
#define SWITCHTEC_LAT_FW_NP BIT(3)
#define SWITCHTEC_LAT_SE_PROCESS BIT(4)
struct chan_fw_regs {
u32 valid_en_se;
u32 cq_base_lo;
u32 cq_base_hi;
u16 cq_size;
u16 rsvd1;
u32 sq_base_lo;
u32 sq_base_hi;
u16 sq_size;
u16 rsvd2;
u32 int_vec;
u32 perf_cfg;
u32 rsvd3;
u32 perf_latency_selector;
u32 perf_fetched_se_cnt_lo;
u32 perf_fetched_se_cnt_hi;
u32 perf_byte_cnt_lo;
u32 perf_byte_cnt_hi;
u32 rsvd4;
u16 perf_se_pending;
u16 perf_se_buf_empty;
u32 perf_chan_idle;
u32 perf_lat_max;
u32 perf_lat_min;
u32 perf_lat_last;
u16 sq_current;
u16 sq_phase;
u16 cq_current;
u16 cq_phase;
} __packed;
enum cmd {
CMD_GET_HOST_LIST = 1,
CMD_REGISTER_BUF = 2,
CMD_UNREGISTER_BUF = 3,
CMD_GET_BUF_LIST = 4,
CMD_GET_OWN_BUF_LIST = 5,
};
enum cmd_status {
CMD_STATUS_IDLE = 0,
CMD_STATUS_INPROGRESS = 1,
CMD_STATUS_DONE = 2,
CMD_STATUS_ERROR = 0xFF,
};
#define CMD_TIMEOUT_MSECS 200
#define SWITCHTEC_CHAN_INTERVAL 1
#define SWITCHTEC_CHAN_BURST_SZ 1
#define SWITCHTEC_CHAN_BURST_SCALE 1
#define SWITCHTEC_CHAN_MRRS 1
static LIST_HEAD(chan_list);
static LIST_HEAD(dma_list);
struct switchtec_dma_chan {
struct switchtec_dma_dev *swdma_dev;
struct dma_chan dma_chan;
struct chan_hw_regs __iomem *mmio_chan_hw;
struct chan_fw_regs __iomem *mmio_chan_fw;
spinlock_t hw_ctrl_lock;
struct tasklet_struct desc_task;
spinlock_t submit_lock;
bool ring_active;
int cid;
spinlock_t complete_lock;
bool comp_ring_active;
/* channel index and irq */
int index;
int irq;
/*
* In driver context, head is advanced by producer while
* tail is advanced by consumer.
*/
/* the head and tail for both desc_ring and hw_sq */
int head;
int tail;
int phase_tag;
struct switchtec_dma_desc **desc_ring;
struct switchtec_dma_hw_se_desc *hw_sq;
dma_addr_t dma_addr_sq;
/* the tail for hw_cq */
int cq_tail;
struct switchtec_dma_hw_ce *hw_cq;
dma_addr_t dma_addr_cq;
struct kobject config_kobj;
struct kobject pmon_kobj;
bool is_fabric;
struct list_head list;
};
#define CMD_OUTPUT_SIZE 1024
struct cmd_output{
u32 status;
u32 cmd_id;
u32 rtn_val;
u32 output_size;
u8 data[CMD_OUTPUT_SIZE];
};
#define SWITCHTEC_DMA_EQ_SIZE SZ_1K
struct fabric_event_queue {
u32 head;
u32 rsvd[3];
struct switchtec_fabric_event entries[];
};
struct switchtec_dma_dev {
struct dma_device dma_dev;
struct pci_dev __rcu *pdev;
struct switchtec_dma_chan **swdma_chans;
int chan_cnt;
int chan_status_irq;
void __iomem *bar;
struct dmac_version_regs __iomem *mmio_dmac_ver;
struct dmac_capability_regs __iomem *mmio_dmac_cap;
struct dmac_status_regs __iomem *mmio_dmac_status;
struct dmac_control_regs __iomem *mmio_dmac_ctrl;
struct dmac_fabric_cmd_regs __iomem *mmio_fabric_cmd;
struct dmac_fabric_control_regs __iomem *mmio_fabric_ctrl;
void __iomem *mmio_chan_hw_all;
void __iomem *mmio_chan_fw_all;
struct tasklet_struct chan_status_task;
bool is_fabric;
u16 hfid;
/*
* Only one cmd can be executed at a time.
*/
struct mutex cmd_mutex;
struct cmd_output *cmd;
dma_addr_t cmd_dma_addr;
int cmd_irq;
struct atomic_notifier_head rhi_notifier_list;
struct fabric_event_queue *eq;
dma_addr_t eq_dma_addr;
int eq_tail;
int event_irq;
struct tasklet_struct fabric_event_task;
struct atomic_notifier_head event_notifier_list;
struct work_struct release_work;
struct list_head list;
};
static struct switchtec_dma_dev *to_switchtec_dma(struct dma_device *d)
{
return container_of(d, struct switchtec_dma_dev, dma_dev);
}
static struct switchtec_dma_chan *to_switchtec_dma_chan(struct dma_chan *c)
{
return container_of(c, struct switchtec_dma_chan, dma_chan);
}
static struct device *to_chan_dev(struct switchtec_dma_chan *swdma_chan)
{
return &swdma_chan->dma_chan.dev->device;
}
enum switchtec_dma_opcode {
SWITCHTEC_DMA_OPC_MEMCPY = 0x0,
SWITCHTEC_DMA_OPC_RDIMM = 0x1,
SWITCHTEC_DMA_OPC_WRIMM = 0x2,
SWITCHTEC_DMA_OPC_RHI = 0x6,
SWITCHTEC_DMA_OPC_NOP = 0x7,
};
struct switchtec_dma_hw_se_desc {
u8 opc;
u8 ctrl;
__le16 tlp_setting;
__le16 rsvd1;
__le16 cid;
__le32 byte_cnt;
union {
__le32 saddr_lo;
__le32 widata_lo;
};
union {
__le32 saddr_hi;
__le32 widata_hi;
};
__le32 daddr_lo;
__le32 daddr_hi;
__le16 dfid;
__le16 sfid;
};
#define SWITCHTEC_SE_DFM BIT(5)
#define SWITCHTEC_SE_LIOF BIT(6)
#define SWITCHTEC_SE_BRR BIT(7)
#define SWITCHTEC_SE_CID_MASK GENMASK(15, 0)
#define SWITCHTEC_CE_SC_LEN_ERR BIT(0)
#define SWITCHTEC_CE_SC_UR BIT(1)
#define SWITCHTEC_CE_SC_CA BIT(2)
#define SWITCHTEC_CE_SC_RSVD_CPL BIT(3)
#define SWITCHTEC_CE_SC_ECRC_ERR BIT(4)
#define SWITCHTEC_CE_SC_EP_SET BIT(5)
#define SWITCHTEC_CE_SC_D_RD_CTO BIT(8)
#define SWITCHTEC_CE_SC_D_RIMM_UR BIT(9)
#define SWITCHTEC_CE_SC_D_RIMM_CA BIT(10)
#define SWITCHTEC_CE_SC_D_RIMM_RSVD_CPL BIT(11)
#define SWITCHTEC_CE_SC_D_ECRC BIT(12)
#define SWITCHTEC_CE_SC_D_EP_SET BIT(13)
#define SWITCHTEC_CE_SC_D_BAD_CONNID BIT(14)
#define SWITCHTEC_CE_SC_D_BAD_RHI_ADDR BIT(15)
#define SWITCHTEC_CE_SC_D_INVD_CMD BIT(16)
#define SWITCHTEC_CE_SC_MASK GENMASK(16, 0)
struct switchtec_dma_hw_ce {
__le32 rdimm_cpl_dw0;
__le32 rdimm_cpl_dw1;
__le32 rsvd1;
__le32 cpl_byte_cnt;
__le16 sq_head;
__le16 rsvd2;
__le32 rsvd3;
__le32 sts_code;
__le16 cid;
__le16 phase_tag;
};
struct switchtec_dma_desc {
struct dma_async_tx_descriptor txd;
struct switchtec_dma_hw_se_desc *hw;
u32 orig_size;
bool completed;
};
#define HALT_RETRY 100
static int halt_channel(struct switchtec_dma_chan *swdma_chan)
{
u32 status;
struct chan_hw_regs __iomem *chan_hw = swdma_chan->mmio_chan_hw;
int retry = HALT_RETRY;
struct pci_dev *pdev;
int ret;
rcu_read_lock();
pdev = rcu_dereference(swdma_chan->swdma_dev->pdev);
if (!pdev) {
ret = -ENODEV;
goto unlock_and_exit;
}
spin_lock(&swdma_chan->hw_ctrl_lock);
writeb(SWITCHTEC_CHAN_CTRL_HALT, &chan_hw->ctrl);
ret = -EIO;
do {
status = readl(&chan_hw->status);
if (status & SWITCHTEC_CHAN_STS_HALTED) {
ret = 0;
break;
} else {
udelay(1000);
}
} while (retry--);
spin_unlock(&swdma_chan->hw_ctrl_lock);
unlock_and_exit:
rcu_read_unlock();
return ret;
}
static int unhalt_channel(struct switchtec_dma_chan *swdma_chan)
{
u8 ctrl;
u32 status;
struct chan_hw_regs __iomem *chan_hw = swdma_chan->mmio_chan_hw;
int retry = HALT_RETRY;
struct pci_dev *pdev;
int ret;
rcu_read_lock();
pdev = rcu_dereference(swdma_chan->swdma_dev->pdev);
if (!pdev) {
ret = -ENODEV;
goto unlock_and_exit;
}
spin_lock(&swdma_chan->hw_ctrl_lock);
ctrl = readb(&chan_hw->ctrl);
ctrl &= ~SWITCHTEC_CHAN_CTRL_HALT;
writeb(ctrl, &chan_hw->ctrl);
ret = -EIO;
do {
status = readl(&chan_hw->status);
if (!(status & SWITCHTEC_CHAN_STS_HALTED)) {
ret = 0;
break;
} else {
udelay(1000);
}
} while (retry--);
spin_unlock(&swdma_chan->hw_ctrl_lock);
unlock_and_exit:
rcu_read_unlock();
return ret;
}
static int reset_channel(struct switchtec_dma_chan *swdma_chan)
{
u8 ctrl;
struct chan_hw_regs __iomem *chan_hw = swdma_chan->mmio_chan_hw;
struct pci_dev *pdev;
rcu_read_lock();
pdev = rcu_dereference(swdma_chan->swdma_dev->pdev);
if (!pdev) {
rcu_read_unlock();
return -ENODEV;
}
/*
* This function is only called during initialization, no need to
* protect the access to chan_hw->ctrl with hw_ctrl_lock.
*/
ctrl = SWITCHTEC_CHAN_CTRL_RESET;
ctrl |= SWITCHTEC_CHAN_CTRL_ERR_PAUSE;
writel(ctrl, &chan_hw->ctrl);
udelay(1000);
ctrl = SWITCHTEC_CHAN_CTRL_ERR_PAUSE;
writel(ctrl, &chan_hw->ctrl);
rcu_read_unlock();
return 0;
}
static int pause_reset_channel(struct switchtec_dma_chan *swdma_chan)
{
struct chan_hw_regs __iomem *chan_hw = swdma_chan->mmio_chan_hw;
struct pci_dev *pdev;
rcu_read_lock();
pdev = rcu_dereference(swdma_chan->swdma_dev->pdev);
if (!pdev) {
rcu_read_unlock();
return -ENODEV;
}
/*
* This function is only called during initialization, no need to
* protect the access to chan_hw->ctrl with hw_ctrl_lock.
*/
/* pause channel */
writeb(SWITCHTEC_CHAN_CTRL_PAUSE, &chan_hw->ctrl);
rcu_read_unlock();
/* wait 60ms to ensure no pending CEs */
msleep(60);
/* reset channel */
return reset_channel(swdma_chan);
}
#define PAUSE_RESUME_RETRY 100
static int switchtec_dma_pause(struct dma_chan *chan)
{
struct switchtec_dma_chan *swdma_chan = to_switchtec_dma_chan(chan);
u32 status;
struct chan_hw_regs __iomem *chan_hw = swdma_chan->mmio_chan_hw;
int retry = PAUSE_RESUME_RETRY;
struct pci_dev *pdev;
int ret;
rcu_read_lock();
pdev = rcu_dereference(swdma_chan->swdma_dev->pdev);
if (!pdev) {
ret = -ENODEV;
goto unlock_and_exit;
}
spin_lock(&swdma_chan->hw_ctrl_lock);
writeb(SWITCHTEC_CHAN_CTRL_PAUSE, &chan_hw->ctrl);
ret = -EIO;
do {
status = readl(&chan_hw->status);
if (status & SWITCHTEC_CHAN_STS_PAUSED) {
ret = 0;
break;
} else {
udelay(1000);
}
} while (retry--);
spin_unlock(&swdma_chan->hw_ctrl_lock);
unlock_and_exit:
rcu_read_unlock();
return ret;
}
static int switchtec_dma_resume(struct dma_chan *chan)
{
struct switchtec_dma_chan *swdma_chan = to_switchtec_dma_chan(chan);
u32 status;
struct chan_hw_regs __iomem *chan_hw = swdma_chan->mmio_chan_hw;
int retry = PAUSE_RESUME_RETRY;
struct pci_dev *pdev;
int ret;
rcu_read_lock();
pdev = rcu_dereference(swdma_chan->swdma_dev->pdev);
if (!pdev) {
ret = -ENODEV;
goto unlock_and_exit;
}
spin_lock(&swdma_chan->hw_ctrl_lock);
writeb(0, &chan_hw->ctrl);
ret = -EIO;
do {
status = readl(&chan_hw->status);
if (!(status & SWITCHTEC_CHAN_STS_PAUSED)) {
ret = 0;
break;
} else {
udelay(1000);
}
} while (retry--);
spin_unlock(&swdma_chan->hw_ctrl_lock);
unlock_and_exit:
rcu_read_unlock();
return ret;
}
static int enable_channel(struct switchtec_dma_chan *swdma_chan)
{
u32 valid_en_se;
struct chan_fw_regs __iomem *chan_fw = swdma_chan->mmio_chan_fw;
struct pci_dev *pdev;
rcu_read_lock();
pdev = rcu_dereference(swdma_chan->swdma_dev->pdev);
if (!pdev) {
rcu_read_unlock();
return -ENODEV;
}
valid_en_se = readl(&chan_fw->valid_en_se);
valid_en_se |= SWITCHTEC_CHAN_ENABLE;
writel(valid_en_se, &chan_fw->valid_en_se);
rcu_read_unlock();
return 0;
}
static int disable_channel(struct switchtec_dma_chan *swdma_chan)
{
u32 valid_en_se;
struct chan_fw_regs __iomem *chan_fw = swdma_chan->mmio_chan_fw;
struct pci_dev *pdev;
rcu_read_lock();
pdev = rcu_dereference(swdma_chan->swdma_dev->pdev);
if (!pdev) {
rcu_read_unlock();
return -ENODEV;
}
valid_en_se = readl(&chan_fw->valid_en_se);
valid_en_se &= ~SWITCHTEC_CHAN_ENABLE;
writel(valid_en_se, &chan_fw->valid_en_se);
rcu_read_unlock();
return 0;
}
#define SWITCHTEC_DMA_SQ_SIZE SZ_32K
#define SWITCHTEC_DMA_CQ_SIZE SZ_32K
#define SWITCHTEC_DMA_RING_SIZE SWITCHTEC_DMA_SQ_SIZE
static struct switchtec_dma_desc *switchtec_dma_get_desc(
struct switchtec_dma_chan *swdma_chan, int i)
{
return swdma_chan->desc_ring[i];
}
static struct switchtec_dma_hw_ce * switchtec_dma_get_ce(
struct switchtec_dma_chan *swdma_chan, int i)
{
return &swdma_chan->hw_cq[i];
}
static void switchtec_dma_process_desc(struct switchtec_dma_chan *swdma_chan)
{
struct device *chan_dev = to_chan_dev(swdma_chan);
struct dmaengine_result res;
struct switchtec_dma_desc *desc;
struct switchtec_dma_hw_ce *ce;
__le16 phase_tag;
int tail;
int cid;
int se_idx;
u32 sts_code;
int i = 0;
int *p;
do {
spin_lock_bh(&swdma_chan->complete_lock);
if (!swdma_chan->comp_ring_active) {
spin_unlock_bh(&swdma_chan->complete_lock);
break;
}
ce = switchtec_dma_get_ce(swdma_chan, swdma_chan->cq_tail);
phase_tag = smp_load_acquire(&ce->phase_tag);
if (le16_to_cpu(phase_tag) == swdma_chan->phase_tag) {
spin_unlock_bh(&swdma_chan->complete_lock);
break;
}
cid = le16_to_cpu(ce->cid);
se_idx = cid & (SWITCHTEC_DMA_SQ_SIZE - 1);
desc = switchtec_dma_get_desc(swdma_chan, se_idx);
tail = swdma_chan->tail;
res.residue = desc->orig_size - le32_to_cpu(ce->cpl_byte_cnt);
sts_code = le32_to_cpu(ce->sts_code);
if (!(sts_code & SWITCHTEC_CE_SC_MASK)) {
res.result = DMA_TRANS_NOERROR;
} else {
if (sts_code & SWITCHTEC_CE_SC_D_RD_CTO)
res.result = DMA_TRANS_READ_FAILED;
else
res.result = DMA_TRANS_WRITE_FAILED;
dev_err(chan_dev, "CID 0x%04x failed, SC 0x%08x\n", cid,
(u32)(sts_code & SWITCHTEC_CE_SC_MASK));
p = (int *)ce;
for (i = 0; i < sizeof(*ce)/4; i++) {
dev_err(chan_dev, "CE DW%d: 0x%08x\n", i,
le32_to_cpu((__force __le32)*p));
p++;
}
}
desc->completed = true;
swdma_chan->cq_tail++;
swdma_chan->cq_tail &= SWITCHTEC_DMA_CQ_SIZE - 1;
rcu_read_lock();
if (!rcu_dereference(swdma_chan->swdma_dev->pdev)) {
rcu_read_unlock();
spin_unlock_bh(&swdma_chan->complete_lock);
return;
}
writew(swdma_chan->cq_tail, &swdma_chan->mmio_chan_hw->cq_head);
rcu_read_unlock();
if (swdma_chan->cq_tail == 0)
swdma_chan->phase_tag = !swdma_chan->phase_tag;
/* Out of order CE */
if (se_idx != tail) {
spin_unlock_bh(&swdma_chan->complete_lock);
continue;
}
do {
dma_cookie_complete(&desc->txd);
dma_descriptor_unmap(&desc->txd);
dmaengine_desc_get_callback_invoke(&desc->txd, &res);
desc->txd.callback = NULL;
desc->txd.callback_result = NULL;
desc->completed = false;
tail++;
tail &= SWITCHTEC_DMA_SQ_SIZE - 1;
smp_store_release(&swdma_chan->tail, tail);
desc = switchtec_dma_get_desc(swdma_chan,
swdma_chan->tail);
if (!desc->completed)
break;
} while (CIRC_CNT(READ_ONCE(swdma_chan->head), swdma_chan->tail,
SWITCHTEC_DMA_SQ_SIZE));
spin_unlock_bh(&swdma_chan->complete_lock);
} while (1);
}
static void switchtec_dma_abort_desc(struct switchtec_dma_chan *swdma_chan,
int force)
{
struct dmaengine_result res;
struct switchtec_dma_desc *desc;
if (!force)
switchtec_dma_process_desc(swdma_chan);
spin_lock_bh(&swdma_chan->complete_lock);
while (CIRC_CNT(swdma_chan->head, swdma_chan->tail,
SWITCHTEC_DMA_SQ_SIZE) >= 1) {
desc = switchtec_dma_get_desc(swdma_chan, swdma_chan->tail);
res.residue = desc->orig_size;
res.result = DMA_TRANS_ABORTED;
dma_cookie_complete(&desc->txd);
dma_descriptor_unmap(&desc->txd);
if (!force)
dmaengine_desc_get_callback_invoke(&desc->txd, &res);
desc->txd.callback = NULL;
desc->txd.callback_result = NULL;
swdma_chan->tail++;
swdma_chan->tail &= SWITCHTEC_DMA_SQ_SIZE - 1;
}
spin_unlock_bh(&swdma_chan->complete_lock);
}
static void switchtec_dma_chan_stop(struct switchtec_dma_chan *swdma_chan)
{
int rc;
rc = halt_channel(swdma_chan);
if (rc) {
dev_err(to_chan_dev(swdma_chan), "stop channel failed\n");
return;
}
rcu_read_lock();
if (!rcu_dereference(swdma_chan->swdma_dev->pdev)) {
rcu_read_unlock();
return;
}
writel(0, &swdma_chan->mmio_chan_fw->sq_base_lo);
writel(0, &swdma_chan->mmio_chan_fw->sq_base_hi);
writel(0, &swdma_chan->mmio_chan_fw->cq_base_lo);
writel(0, &swdma_chan->mmio_chan_fw->cq_base_hi);
rcu_read_unlock();
}
static int switchtec_dma_terminate_all(struct dma_chan *chan)
{
struct switchtec_dma_chan *swdma_chan = to_switchtec_dma_chan(chan);
int rc = 0;
spin_lock_bh(&swdma_chan->complete_lock);
swdma_chan->comp_ring_active = false;
spin_unlock_bh(&swdma_chan->complete_lock);
rc = pause_reset_channel(swdma_chan);
if (rc)
dev_err(to_chan_dev(swdma_chan),
"%s: pause reset channel failed\n",
dma_chan_name(chan));
return rc;
}
static void switchtec_dma_synchronize(struct dma_chan *chan)
{
struct pci_dev *pdev;
struct switchtec_dma_chan *swdma_chan = to_switchtec_dma_chan(chan);
int rc;
rcu_read_lock();
pdev = rcu_dereference(swdma_chan->swdma_dev->pdev);
if (pdev)
synchronize_irq(swdma_chan->irq);
rcu_read_unlock();
switchtec_dma_abort_desc(swdma_chan, 1);
rc = enable_channel(swdma_chan);
if (rc)
return;
rc = reset_channel(swdma_chan);
if (rc)
return;
rc = unhalt_channel(swdma_chan);
if (rc)
return;
spin_lock_bh(&swdma_chan->submit_lock);
swdma_chan->head = 0;
spin_unlock_bh(&swdma_chan->submit_lock);
spin_lock_bh(&swdma_chan->complete_lock);
swdma_chan->comp_ring_active = true;
swdma_chan->phase_tag = 0;
swdma_chan->tail = 0;
swdma_chan->cq_tail = 0;
swdma_chan->cid = 0;
dma_cookie_init(chan);
spin_unlock_bh(&swdma_chan->complete_lock);
}
static void switchtec_dma_desc_task(unsigned long data)
{
struct switchtec_dma_chan *swdma_chan = (void *)data;
switchtec_dma_process_desc(swdma_chan);
}
static void switchtec_dma_chan_status_task(unsigned long data)
{
struct switchtec_dma_dev *swdma_dev = (void *)data;
struct dma_device *dma_dev = &swdma_dev->dma_dev;
struct dma_chan *chan;
struct switchtec_dma_chan *swdma_chan;
struct chan_hw_regs __iomem *chan_hw;
struct device *chan_dev;
u32 chan_status;
int bit;
list_for_each_entry(chan, &dma_dev->channels, device_node) {
swdma_chan = to_switchtec_dma_chan(chan);
chan_dev = to_chan_dev(swdma_chan);
chan_hw = swdma_chan->mmio_chan_hw;
rcu_read_lock();
if (!rcu_dereference(swdma_dev->pdev)) {
rcu_read_unlock();
return;
}
chan_status = readl(&chan_hw->status);
chan_status &= SWITCHTEC_CHAN_STS_PAUSED_MASK;
rcu_read_unlock();
bit = ffs(chan_status);
if (!bit)
dev_dbg(chan_dev, "No pause bit set.");
else
dev_err(chan_dev, "Paused, %s\n",
channel_status_str[bit - 1]);
}
}
#define SWITCHTEC_INVALID_HFID 0xffff
enum desc_type{
MEMCPY,
WIMM,
UNKNOWN_TRANSACTION,
};
static struct dma_async_tx_descriptor *switchtec_dma_prep_desc(
struct dma_chan *c, enum desc_type type, u16 dst_fid,
dma_addr_t dma_dst, u16 src_fid, dma_addr_t dma_src, u64 data,
size_t len, unsigned long flags)
__acquires(swdma_chan->submit_lock)
{
struct switchtec_dma_chan *swdma_chan = to_switchtec_dma_chan(c);
struct device *chan_dev = to_chan_dev(swdma_chan);
struct switchtec_dma_desc *desc;
int head;
int tail;
spin_lock_bh(&swdma_chan->submit_lock);
if (type >= UNKNOWN_TRANSACTION)
goto err_unlock;