forked from aws/aws-fpga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdma_mod.c
380 lines (314 loc) · 9.6 KB
/
xdma_mod.c
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
/*******************************************************************************
*
* Xilinx XDMA IP Core Linux Driver
* Copyright(c) 2015 - 2020 Xilinx, Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* The full GNU General Public License is included in this distribution in
* the file called "LICENSE".
*
* Karen Xie <[email protected]>
*
******************************************************************************/
#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
#include <linux/ioctl.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/aer.h>
/* include early, to verify it depends only on the headers above */
#include "libxdma_api.h"
#include "libxdma.h"
#include "xdma_mod.h"
#include "xdma_cdev.h"
#include "version.h"
#define DRV_MODULE_NAME "xdma"
#define DRV_MODULE_DESC "Xilinx XDMA Reference Driver"
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("GPL v2");
/* SECTION: Module global variables */
static int xpdev_cnt;
static const struct pci_device_id pci_ids[] = {
{ PCI_DEVICE(0x10ee, 0x9048), },
{ PCI_DEVICE(0x10ee, 0x9044), },
{ PCI_DEVICE(0x10ee, 0x9042), },
{ PCI_DEVICE(0x10ee, 0x9041), },
{ PCI_DEVICE(0x10ee, 0x903f), },
{ PCI_DEVICE(0x10ee, 0x9038), },
{ PCI_DEVICE(0x10ee, 0x9028), },
{ PCI_DEVICE(0x10ee, 0x9018), },
{ PCI_DEVICE(0x10ee, 0x9034), },
{ PCI_DEVICE(0x10ee, 0x9024), },
{ PCI_DEVICE(0x10ee, 0x9014), },
{ PCI_DEVICE(0x10ee, 0x9032), },
{ PCI_DEVICE(0x10ee, 0x9022), },
{ PCI_DEVICE(0x10ee, 0x9012), },
{ PCI_DEVICE(0x10ee, 0x9031), },
{ PCI_DEVICE(0x10ee, 0x9021), },
{ PCI_DEVICE(0x10ee, 0x9011), },
{ PCI_DEVICE(0x10ee, 0x8011), },
{ PCI_DEVICE(0x10ee, 0x8012), },
{ PCI_DEVICE(0x10ee, 0x8014), },
{ PCI_DEVICE(0x10ee, 0x8018), },
{ PCI_DEVICE(0x10ee, 0x8021), },
{ PCI_DEVICE(0x10ee, 0x8022), },
{ PCI_DEVICE(0x10ee, 0x8024), },
{ PCI_DEVICE(0x10ee, 0x8028), },
{ PCI_DEVICE(0x10ee, 0x8031), },
{ PCI_DEVICE(0x10ee, 0x8032), },
{ PCI_DEVICE(0x10ee, 0x8034), },
{ PCI_DEVICE(0x10ee, 0x8038), },
{ PCI_DEVICE(0x10ee, 0x7011), },
{ PCI_DEVICE(0x10ee, 0x7012), },
{ PCI_DEVICE(0x10ee, 0x7014), },
{ PCI_DEVICE(0x10ee, 0x7018), },
{ PCI_DEVICE(0x10ee, 0x7021), },
{ PCI_DEVICE(0x10ee, 0x7022), },
{ PCI_DEVICE(0x10ee, 0x7024), },
{ PCI_DEVICE(0x10ee, 0x7028), },
{ PCI_DEVICE(0x10ee, 0x7031), },
{ PCI_DEVICE(0x10ee, 0x7032), },
{ PCI_DEVICE(0x10ee, 0x7034), },
{ PCI_DEVICE(0x10ee, 0x7038), },
{ PCI_DEVICE(0x10ee, 0x6828), },
{ PCI_DEVICE(0x10ee, 0x6830), },
{ PCI_DEVICE(0x10ee, 0x6928), },
{ PCI_DEVICE(0x10ee, 0x6930), },
{ PCI_DEVICE(0x10ee, 0x6A28), },
{ PCI_DEVICE(0x10ee, 0x6A30), },
{ PCI_DEVICE(0x10ee, 0x6D30), },
{ PCI_DEVICE(0x10ee, 0x4808), },
{ PCI_DEVICE(0x10ee, 0x4828), },
{ PCI_DEVICE(0x10ee, 0x4908), },
{ PCI_DEVICE(0x10ee, 0x4A28), },
{ PCI_DEVICE(0x10ee, 0x4B28), },
{ PCI_DEVICE(0x10ee, 0x2808), },
{ PCI_DEVICE(0x1d0f, 0xf000), },
{ PCI_DEVICE(0x1d0f, 0xf001), },
#ifdef INTERNAL_TESTING
{ PCI_DEVICE(0x1d0f, 0x1042), 0},
#endif
{0,}
};
MODULE_DEVICE_TABLE(pci, pci_ids);
static void xpdev_free(struct xdma_pci_dev *xpdev)
{
struct xdma_dev *xdev = xpdev->xdev;
pr_info("xpdev 0x%p, destroy_interfaces, xdev 0x%p.\n", xpdev, xdev);
xpdev_destroy_interfaces(xpdev);
xpdev->xdev = NULL;
pr_info("xpdev 0x%p, xdev 0x%p xdma_device_close.\n", xpdev, xdev);
xdma_device_close(xpdev->pdev, xdev);
xpdev_cnt--;
kfree(xpdev);
}
static struct xdma_pci_dev *xpdev_alloc(struct pci_dev *pdev)
{
struct xdma_pci_dev *xpdev = kmalloc(sizeof(*xpdev), GFP_KERNEL);
if (!xpdev)
return NULL;
memset(xpdev, 0, sizeof(*xpdev));
xpdev->magic = MAGIC_DEVICE;
xpdev->pdev = pdev;
xpdev->user_max = MAX_USER_IRQ;
xpdev->h2c_channel_max = XDMA_CHANNEL_NUM_MAX;
xpdev->c2h_channel_max = XDMA_CHANNEL_NUM_MAX;
xpdev_cnt++;
return xpdev;
}
static int probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
{
int rv = 0;
struct xdma_pci_dev *xpdev = NULL;
struct xdma_dev *xdev;
void *hndl;
xpdev = xpdev_alloc(pdev);
if (!xpdev)
return -ENOMEM;
hndl = xdma_device_open(DRV_MODULE_NAME, pdev, &xpdev->user_max,
&xpdev->h2c_channel_max, &xpdev->c2h_channel_max);
if (!hndl) {
rv = -EINVAL;
goto err_out;
}
if (xpdev->user_max > MAX_USER_IRQ) {
pr_err("Maximum users limit reached\n");
rv = -EINVAL;
goto err_out;
}
if (xpdev->h2c_channel_max > XDMA_CHANNEL_NUM_MAX) {
pr_err("Maximun H2C channel limit reached\n");
rv = -EINVAL;
goto err_out;
}
if (xpdev->c2h_channel_max > XDMA_CHANNEL_NUM_MAX) {
pr_err("Maximun C2H channel limit reached\n");
rv = -EINVAL;
goto err_out;
}
if (!xpdev->h2c_channel_max && !xpdev->c2h_channel_max)
pr_warn("NO engine found!\n");
if (xpdev->user_max) {
u32 mask = (1 << (xpdev->user_max + 1)) - 1;
rv = xdma_user_isr_enable(hndl, mask);
if (rv)
goto err_out;
}
/* make sure no duplicate */
xdev = xdev_find_by_pdev(pdev);
if (!xdev) {
pr_warn("NO xdev found!\n");
rv = -EINVAL;
goto err_out;
}
if (hndl != xdev) {
pr_err("xdev handle mismatch\n");
rv = -EINVAL;
goto err_out;
}
pr_info("%s xdma%d, pdev 0x%p, xdev 0x%p, 0x%p, usr %d, ch %d,%d.\n",
dev_name(&pdev->dev), xdev->idx, pdev, xpdev, xdev,
xpdev->user_max, xpdev->h2c_channel_max,
xpdev->c2h_channel_max);
xpdev->xdev = hndl;
rv = xpdev_create_interfaces(xpdev);
if (rv)
goto err_out;
dev_set_drvdata(&pdev->dev, xpdev);
return 0;
err_out:
pr_err("pdev 0x%p, err %d.\n", pdev, rv);
xpdev_free(xpdev);
return rv;
}
static void remove_one(struct pci_dev *pdev)
{
struct xdma_pci_dev *xpdev;
if (!pdev)
return;
xpdev = dev_get_drvdata(&pdev->dev);
if (!xpdev)
return;
pr_info("pdev 0x%p, xdev 0x%p, 0x%p.\n",
pdev, xpdev, xpdev->xdev);
xpdev_free(xpdev);
dev_set_drvdata(&pdev->dev, NULL);
}
static pci_ers_result_t xdma_error_detected(struct pci_dev *pdev,
pci_channel_state_t state)
{
struct xdma_pci_dev *xpdev = dev_get_drvdata(&pdev->dev);
switch (state) {
case pci_channel_io_normal:
return PCI_ERS_RESULT_CAN_RECOVER;
case pci_channel_io_frozen:
pr_warn("dev 0x%p,0x%p, frozen state error, reset controller\n",
pdev, xpdev);
xdma_device_offline(pdev, xpdev->xdev);
pci_disable_device(pdev);
return PCI_ERS_RESULT_NEED_RESET;
case pci_channel_io_perm_failure:
pr_warn("dev 0x%p,0x%p, failure state error, req. disconnect\n",
pdev, xpdev);
return PCI_ERS_RESULT_DISCONNECT;
}
return PCI_ERS_RESULT_NEED_RESET;
}
static pci_ers_result_t xdma_slot_reset(struct pci_dev *pdev)
{
struct xdma_pci_dev *xpdev = dev_get_drvdata(&pdev->dev);
pr_info("0x%p restart after slot reset\n", xpdev);
if (pci_enable_device_mem(pdev)) {
pr_info("0x%p failed to renable after slot reset\n", xpdev);
return PCI_ERS_RESULT_DISCONNECT;
}
pci_set_master(pdev);
pci_restore_state(pdev);
pci_save_state(pdev);
xdma_device_online(pdev, xpdev->xdev);
return PCI_ERS_RESULT_RECOVERED;
}
static void xdma_error_resume(struct pci_dev *pdev)
{
struct xdma_pci_dev *xpdev = dev_get_drvdata(&pdev->dev);
pr_info("dev 0x%p,0x%p.\n", pdev, xpdev);
pci_cleanup_aer_uncorrect_error_status(pdev);
}
#if KERNEL_VERSION(4, 13, 0) <= LINUX_VERSION_CODE
static void xdma_reset_prepare(struct pci_dev *pdev)
{
struct xdma_pci_dev *xpdev = dev_get_drvdata(&pdev->dev);
pr_info("dev 0x%p,0x%p.\n", pdev, xpdev);
xdma_device_offline(pdev, xpdev->xdev);
}
static void xdma_reset_done(struct pci_dev *pdev)
{
struct xdma_pci_dev *xpdev = dev_get_drvdata(&pdev->dev);
pr_info("dev 0x%p,0x%p.\n", pdev, xpdev);
xdma_device_online(pdev, xpdev->xdev);
}
#elif KERNEL_VERSION(3, 16, 0) <= LINUX_VERSION_CODE
static void xdma_reset_notify(struct pci_dev *pdev, bool prepare)
{
struct xdma_pci_dev *xpdev = dev_get_drvdata(&pdev->dev);
pr_info("dev 0x%p,0x%p, prepare %d.\n", pdev, xpdev, prepare);
if (prepare)
xdma_device_offline(pdev, xpdev->xdev);
else
xdma_device_online(pdev, xpdev->xdev);
}
#endif
static const struct pci_error_handlers xdma_err_handler = {
.error_detected = xdma_error_detected,
.slot_reset = xdma_slot_reset,
.resume = xdma_error_resume,
#if KERNEL_VERSION(4, 13, 0) <= LINUX_VERSION_CODE
.reset_prepare = xdma_reset_prepare,
.reset_done = xdma_reset_done,
#elif KERNEL_VERSION(3, 16, 0) <= LINUX_VERSION_CODE
.reset_notify = xdma_reset_notify,
#endif
};
static struct pci_driver pci_driver = {
.name = DRV_MODULE_NAME,
.id_table = pci_ids,
.probe = probe_one,
.remove = remove_one,
.err_handler = &xdma_err_handler,
};
static int __init xdma_mod_init(void)
{
int rv;
pr_info("%s", version);
if (desc_blen_max > XDMA_DESC_BLEN_MAX)
desc_blen_max = XDMA_DESC_BLEN_MAX;
pr_info("desc_blen_max: 0x%x/%u, sgdma_timeout: %u sec.\n",
desc_blen_max, desc_blen_max, sgdma_timeout);
rv = xdma_cdev_init();
if (rv < 0)
return rv;
return pci_register_driver(&pci_driver);
}
static void __exit xdma_mod_exit(void)
{
/* unregister this driver from the PCI bus driver */
dbg_init("pci_unregister_driver.\n");
pci_unregister_driver(&pci_driver);
xdma_cdev_cleanup();
}
module_init(xdma_mod_init);
module_exit(xdma_mod_exit);