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
|
// SPDX-License-Identifier: GPL-2.0
/*
* Microsemi Switchtec(tm) PCIe Management Driver
* Copyright (c) 2019, Logan Gunthorpe <logang@deltatee.com>
* Copyright (c) 2019, GigaIO Networks, Inc
*/
#include "dmaengine.h"
#include <linux/dmaengine.h>
#include <linux/kref.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/pci.h>
MODULE_DESCRIPTION("PLX ExpressLane PEX PCI Switch DMA Engine");
MODULE_VERSION("0.1");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Logan Gunthorpe");
#define PLX_REG_DESC_RING_ADDR 0x214
#define PLX_REG_DESC_RING_ADDR_HI 0x218
#define PLX_REG_DESC_RING_NEXT_ADDR 0x21C
#define PLX_REG_DESC_RING_COUNT 0x220
#define PLX_REG_DESC_RING_LAST_ADDR 0x224
#define PLX_REG_DESC_RING_LAST_SIZE 0x228
#define PLX_REG_PREF_LIMIT 0x234
#define PLX_REG_CTRL 0x238
#define PLX_REG_CTRL2 0x23A
#define PLX_REG_INTR_CTRL 0x23C
#define PLX_REG_INTR_STATUS 0x23E
#define PLX_REG_PREF_LIMIT_PREF_FOUR 8
#define PLX_REG_CTRL_GRACEFUL_PAUSE BIT(0)
#define PLX_REG_CTRL_ABORT BIT(1)
#define PLX_REG_CTRL_WRITE_BACK_EN BIT(2)
#define PLX_REG_CTRL_START BIT(3)
#define PLX_REG_CTRL_RING_STOP_MODE BIT(4)
#define PLX_REG_CTRL_DESC_MODE_BLOCK (0 << 5)
#define PLX_REG_CTRL_DESC_MODE_ON_CHIP (1 << 5)
#define PLX_REG_CTRL_DESC_MODE_OFF_CHIP (2 << 5)
#define PLX_REG_CTRL_DESC_INVALID BIT(8)
#define PLX_REG_CTRL_GRACEFUL_PAUSE_DONE BIT(9)
#define PLX_REG_CTRL_ABORT_DONE BIT(10)
#define PLX_REG_CTRL_IMM_PAUSE_DONE BIT(12)
#define PLX_REG_CTRL_IN_PROGRESS BIT(30)
#define PLX_REG_CTRL_RESET_VAL (PLX_REG_CTRL_DESC_INVALID | \
PLX_REG_CTRL_GRACEFUL_PAUSE_DONE | \
PLX_REG_CTRL_ABORT_DONE | \
PLX_REG_CTRL_IMM_PAUSE_DONE)
#define PLX_REG_CTRL_START_VAL (PLX_REG_CTRL_WRITE_BACK_EN | \
PLX_REG_CTRL_DESC_MODE_OFF_CHIP | \
PLX_REG_CTRL_START | \
PLX_REG_CTRL_RESET_VAL)
#define PLX_REG_CTRL2_MAX_TXFR_SIZE_64B 0
#define PLX_REG_CTRL2_MAX_TXFR_SIZE_128B 1
#define PLX_REG_CTRL2_MAX_TXFR_SIZE_256B 2
#define PLX_REG_CTRL2_MAX_TXFR_SIZE_512B 3
#define PLX_REG_CTRL2_MAX_TXFR_SIZE_1KB 4
#define PLX_REG_CTRL2_MAX_TXFR_SIZE_2KB 5
#define PLX_REG_CTRL2_MAX_TXFR_SIZE_4B 7
#define PLX_REG_INTR_CRTL_ERROR_EN BIT(0)
#define PLX_REG_INTR_CRTL_INV_DESC_EN BIT(1)
#define PLX_REG_INTR_CRTL_ABORT_DONE_EN BIT(3)
#define PLX_REG_INTR_CRTL_PAUSE_DONE_EN BIT(4)
#define PLX_REG_INTR_CRTL_IMM_PAUSE_DONE_EN BIT(5)
#define PLX_REG_INTR_STATUS_ERROR BIT(0)
#define PLX_REG_INTR_STATUS_INV_DESC BIT(1)
#define PLX_REG_INTR_STATUS_DESC_DONE BIT(2)
#define PLX_REG_INTR_CRTL_ABORT_DONE BIT(3)
struct plx_dma_hw_std_desc {
__le32 flags_and_size;
__le16 dst_addr_hi;
__le16 src_addr_hi;
__le32 dst_addr_lo;
__le32 src_addr_lo;
};
#define PLX_DESC_SIZE_MASK 0x7ffffff
#define PLX_DESC_FLAG_VALID BIT(31)
#define PLX_DESC_FLAG_INT_WHEN_DONE BIT(30)
#define PLX_DESC_WB_SUCCESS BIT(30)
#define PLX_DESC_WB_RD_FAIL BIT(29)
#define PLX_DESC_WB_WR_FAIL BIT(28)
#define PLX_DMA_RING_COUNT 2048
struct plx_dma_desc {
struct dma_async_tx_descriptor txd;
struct plx_dma_hw_std_desc *hw;
u32 orig_size;
};
struct plx_dma_dev {
struct dma_device dma_dev;
struct dma_chan dma_chan;
struct pci_dev __rcu *pdev;
void __iomem *bar;
struct tasklet_struct desc_task;
spinlock_t ring_lock;
bool ring_active;
int head;
int tail;
struct plx_dma_hw_std_desc *hw_ring;
dma_addr_t hw_ring_dma;
struct plx_dma_desc **desc_ring;
};
static struct plx_dma_dev *chan_to_plx_dma_dev(struct dma_chan *c)
{
return container_of(c, struct plx_dma_dev, dma_chan);
}
static struct plx_dma_desc *plx_dma_get_desc(struct plx_dma_dev *plxdev, int i)
{
return plxdev->desc_ring[i & (PLX_DMA_RING_COUNT - 1)];
}
static void plx_dma_process_desc(struct plx_dma_dev *plxdev)
{
struct dmaengine_result res;
struct plx_dma_desc *desc;
u32 flags;
spin_lock_bh(&plxdev->ring_lock);
while (plxdev->tail != plxdev->head) {
desc = plx_dma_get_desc(plxdev, plxdev->tail);
flags = le32_to_cpu(READ_ONCE(desc->hw->flags_and_size));
if (flags & PLX_DESC_FLAG_VALID)
break;
res.residue = desc->orig_size - (flags & PLX_DESC_SIZE_MASK);
if (flags & PLX_DESC_WB_SUCCESS)
res.result = DMA_TRANS_NOERROR;
else if (flags & PLX_DESC_WB_WR_FAIL)
res.result = DMA_TRANS_WRITE_FAILED;
else
res.result = DMA_TRANS_READ_FAILED;
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;
plxdev->tail++;
}
spin_unlock_bh(&plxdev->ring_lock);
}
static void plx_dma_abort_desc(struct plx_dma_dev *plxdev)
{
struct dmaengine_result res;
struct plx_dma_desc *desc;
plx_dma_process_desc(plxdev);
spin_lock_bh(&plxdev->ring_lock);
while (plxdev->tail != plxdev->head) {
desc = plx_dma_get_desc(plxdev, plxdev->tail);
res.residue = desc->orig_size;
res.result = DMA_TRANS_ABORTED;
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;
plxdev->tail++;
}
spin_unlock_bh(&plxdev->ring_lock);
}
static void __plx_dma_stop(struct plx_dma_dev *plxdev)
{
unsigned long timeout = jiffies + msecs_to_jiffies(1000);
u32 val;
val = readl(plxdev->bar + PLX_REG_CTRL);
if (!(val & ~PLX_REG_CTRL_GRACEFUL_PAUSE))
return;
writel(PLX_REG_CTRL_RESET_VAL | PLX_REG_CTRL_GRACEFUL_PAUSE,
plxdev->bar + PLX_REG_CTRL);
while (!time_after(jiffies, timeout)) {
val = readl(plxdev->bar + PLX_REG_CTRL);
if (val & PLX_REG_CTRL_GRACEFUL_PAUSE_DONE)
break;
cpu_relax();
}
if (!(val & PLX_REG_CTRL_GRACEFUL_PAUSE_DONE))
dev_err(plxdev->dma_dev.dev,
"Timeout waiting for graceful pause!\n");
writel(PLX_REG_CTRL_RESET_VAL | PLX_REG_CTRL_GRACEFUL_PAUSE,
plxdev->bar + PLX_REG_CTRL);
writel(0, plxdev->bar + PLX_REG_DESC_RING_COUNT);
writel(0, plxdev->bar + PLX_REG_DESC_RING_ADDR);
writel(0, plxdev->bar + PLX_REG_DESC_RING_ADDR_HI);
writel(0, plxdev->bar + PLX_REG_DESC_RING_NEXT_ADDR);
}
static void plx_dma_stop(struct plx_dma_dev *plxdev)
{
rcu_read_lock();
if (!rcu_dereference(plxdev->pdev)) {
rcu_read_unlock();
return;
}
__plx_dma_stop(plxdev);
rcu_read_unlock();
}
static void plx_dma_desc_task(unsigned long data)
{
struct plx_dma_dev *plxdev = (void *)data;
plx_dma_process_desc(plxdev);
}
static irqreturn_t plx_dma_isr(int irq, void *devid)
{
struct plx_dma_dev *plxdev = devid;
u32 status;
status = readw(plxdev->bar + PLX_REG_INTR_STATUS);
if (!status)
return IRQ_NONE;
if (status & PLX_REG_INTR_STATUS_DESC_DONE && plxdev->ring_active)
tasklet_schedule(&plxdev->desc_task);
writew(status, plxdev->bar + PLX_REG_INTR_STATUS);
return IRQ_HANDLED;
}
static int plx_dma_alloc_desc(struct plx_dma_dev *plxdev)
{
struct plx_dma_desc *desc;
int i;
plxdev->desc_ring = kcalloc(PLX_DMA_RING_COUNT,
sizeof(*plxdev->desc_ring), GFP_KERNEL);
if (!plxdev->desc_ring)
return -ENOMEM;
for (i = 0; i < PLX_DMA_RING_COUNT; i++) {
desc = kzalloc(sizeof(*desc), GFP_KERNEL);
if (!desc)
goto free_and_exit;
dma_async_tx_descriptor_init(&desc->txd, &plxdev->dma_chan);
desc->hw = &plxdev->hw_ring[i];
plxdev->desc_ring[i] = desc;
}
return 0;
free_and_exit:
for (i = 0; i < PLX_DMA_RING_COUNT; i++)
kfree(plxdev->desc_ring[i]);
kfree(plxdev->desc_ring);
return -ENOMEM;
}
static int plx_dma_alloc_chan_resources(struct dma_chan *chan)
{
struct plx_dma_dev *plxdev = chan_to_plx_dma_dev(chan);
size_t ring_sz = PLX_DMA_RING_COUNT * sizeof(*plxdev->hw_ring);
int rc;
plxdev->head = plxdev->tail = 0;
plxdev->hw_ring = dma_alloc_coherent(plxdev->dma_dev.dev, ring_sz,
&plxdev->hw_ring_dma, GFP_KERNEL);
if (!plxdev->hw_ring)
return -ENOMEM;
rc = plx_dma_alloc_desc(plxdev);
if (rc)
goto out_free_hw_ring;
rcu_read_lock();
if (!rcu_dereference(plxdev->pdev)) {
rcu_read_unlock();
rc = -ENODEV;
goto out_free_hw_ring;
}
writel(PLX_REG_CTRL_RESET_VAL, plxdev->bar + PLX_REG_CTRL);
writel(lower_32_bits(plxdev->hw_ring_dma),
plxdev->bar + PLX_REG_DESC_RING_ADDR);
writel(upper_32_bits(plxdev->hw_ring_dma),
plxdev->bar + PLX_REG_DESC_RING_ADDR_HI);
writel(lower_32_bits(plxdev->hw_ring_dma),
plxdev->bar + PLX_REG_DESC_RING_NEXT_ADDR);
writel(PLX_DMA_RING_COUNT, plxdev->bar + PLX_REG_DESC_RING_COUNT);
writel(PLX_REG_PREF_LIMIT_PREF_FOUR, plxdev->bar + PLX_REG_PREF_LIMIT);
plxdev->ring_active = true;
rcu_read_unlock();
return PLX_DMA_RING_COUNT;
out_free_hw_ring:
dma_free_coherent(plxdev->dma_dev.dev, ring_sz, plxdev->hw_ring,
plxdev->hw_ring_dma);
return rc;
}
static void plx_dma_free_chan_resources(struct dma_chan *chan)
{
struct plx_dma_dev *plxdev = chan_to_plx_dma_dev(chan);
size_t ring_sz = PLX_DMA_RING_COUNT * sizeof(*plxdev->hw_ring);
struct pci_dev *pdev;
int irq = -1;
int i;
spin_lock_bh(&plxdev->ring_lock);
plxdev->ring_active = false;
spin_unlock_bh(&plxdev->ring_lock);
plx_dma_stop(plxdev);
rcu_read_lock();
pdev = rcu_dereference(plxdev->pdev);
if (pdev)
irq = pci_irq_vector(pdev, 0);
rcu_read_unlock();
if (irq > 0)
synchronize_irq(irq);
tasklet_kill(&plxdev->desc_task);
plx_dma_abort_desc(plxdev);
for (i = 0; i < PLX_DMA_RING_COUNT; i++)
kfree(plxdev->desc_ring[i]);
kfree(plxdev->desc_ring);
dma_free_coherent(plxdev->dma_dev.dev, ring_sz, plxdev->hw_ring,
plxdev->hw_ring_dma);
}
static void plx_dma_release(struct dma_device *dma_dev)
{
struct plx_dma_dev *plxdev =
container_of(dma_dev, struct plx_dma_dev, dma_dev);
put_device(dma_dev->dev);
kfree(plxdev);
}
static int plx_dma_create(struct pci_dev *pdev)
{
struct plx_dma_dev *plxdev;
struct dma_device *dma;
struct dma_chan *chan;
int rc;
plxdev = kzalloc(sizeof(*plxdev), GFP_KERNEL);
if (!plxdev)
return -ENOMEM;
rc = request_irq(pci_irq_vector(pdev, 0), plx_dma_isr, 0,
KBUILD_MODNAME, plxdev);
if (rc) {
kfree(plxdev);
return rc;
}
spin_lock_init(&plxdev->ring_lock);
tasklet_init(&plxdev->desc_task, plx_dma_desc_task,
(unsigned long)plxdev);
RCU_INIT_POINTER(plxdev->pdev, pdev);
plxdev->bar = pcim_iomap_table(pdev)[0];
dma = &plxdev->dma_dev;
dma->chancnt = 1;
INIT_LIST_HEAD(&dma->channels);
dma->copy_align = DMAENGINE_ALIGN_1_BYTE;
dma->dev = get_device(&pdev->dev);
dma->device_alloc_chan_resources = plx_dma_alloc_chan_resources;
dma->device_free_chan_resources = plx_dma_free_chan_resources;
dma->device_release = plx_dma_release;
chan = &plxdev->dma_chan;
chan->device = dma;
dma_cookie_init(chan);
list_add_tail(&chan->device_node, &dma->channels);
rc = dma_async_device_register(dma);
if (rc) {
pci_err(pdev, "Failed to register dma device: %d\n", rc);
free_irq(pci_irq_vector(pdev, 0), plxdev);
kfree(plxdev);
return rc;
}
pci_set_drvdata(pdev, plxdev);
return 0;
}
static int plx_dma_probe(struct pci_dev *pdev,
const struct pci_device_id *id)
{
int rc;
rc = pcim_enable_device(pdev);
if (rc)
return rc;
rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(48));
if (rc)
rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
if (rc)
return rc;
rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(48));
if (rc)
rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
if (rc)
return rc;
rc = pcim_iomap_regions(pdev, 1, KBUILD_MODNAME);
if (rc)
return rc;
rc = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
if (rc <= 0)
return rc;
pci_set_master(pdev);
rc = plx_dma_create(pdev);
if (rc)
goto err_free_irq_vectors;
pci_info(pdev, "PLX DMA Channel Registered\n");
return 0;
err_free_irq_vectors:
pci_free_irq_vectors(pdev);
return rc;
}
static void plx_dma_remove(struct pci_dev *pdev)
{
struct plx_dma_dev *plxdev = pci_get_drvdata(pdev);
free_irq(pci_irq_vector(pdev, 0), plxdev);
rcu_assign_pointer(plxdev->pdev, NULL);
synchronize_rcu();
spin_lock_bh(&plxdev->ring_lock);
plxdev->ring_active = false;
spin_unlock_bh(&plxdev->ring_lock);
__plx_dma_stop(plxdev);
plx_dma_abort_desc(plxdev);
plxdev->bar = NULL;
dma_async_device_unregister(&plxdev->dma_dev);
pci_free_irq_vectors(pdev);
}
static const struct pci_device_id plx_dma_pci_tbl[] = {
{
.vendor = PCI_VENDOR_ID_PLX,
.device = 0x87D0,
.subvendor = PCI_ANY_ID,
.subdevice = PCI_ANY_ID,
.class = PCI_CLASS_SYSTEM_OTHER << 8,
.class_mask = 0xFFFFFFFF,
},
{0}
};
MODULE_DEVICE_TABLE(pci, plx_dma_pci_tbl);
static struct pci_driver plx_dma_pci_driver = {
.name = KBUILD_MODNAME,
.id_table = plx_dma_pci_tbl,
.probe = plx_dma_probe,
.remove = plx_dma_remove,
};
module_pci_driver(plx_dma_pci_driver);
|