blob: e73a7005390688a37c68eb602979b89652adc39f [file] [log] [blame]
Wu Hao543be3d2018-06-30 08:53:13 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Driver for FPGA Device Feature List (DFL) Support
4 *
5 * Copyright (C) 2017-2018 Intel Corporation, Inc.
6 *
7 * Authors:
8 * Kang Luwei <luwei.kang@intel.com>
9 * Zhang Yi <yi.z.zhang@intel.com>
10 * Wu Hao <hao.wu@intel.com>
11 * Xiao Guangrong <guangrong.xiao@linux.intel.com>
12 */
Xu Yilunecc16412021-01-06 20:37:12 -080013#include <linux/dfl.h>
Xu Yilun322b5982020-06-16 12:08:44 +080014#include <linux/fpga-dfl.h>
Wu Hao543be3d2018-06-30 08:53:13 +080015#include <linux/module.h>
Xu Yilun322b5982020-06-16 12:08:44 +080016#include <linux/uaccess.h>
Wu Hao543be3d2018-06-30 08:53:13 +080017
18#include "dfl.h"
19
20static DEFINE_MUTEX(dfl_id_mutex);
21
22/*
23 * when adding a new feature dev support in DFL framework, it's required to
24 * add a new item in enum dfl_id_type and provide related information in below
25 * dfl_devs table which is indexed by dfl_id_type, e.g. name string used for
26 * platform device creation (define name strings in dfl.h, as they could be
27 * reused by platform device drivers).
Wu Haob16c5142018-06-30 08:53:14 +080028 *
29 * if the new feature dev needs chardev support, then it's required to add
30 * a new item in dfl_chardevs table and configure dfl_devs[i].devt_type as
31 * index to dfl_chardevs table. If no chardev support just set devt_type
32 * as one invalid index (DFL_FPGA_DEVT_MAX).
Wu Hao543be3d2018-06-30 08:53:13 +080033 */
Wu Haob16c5142018-06-30 08:53:14 +080034enum dfl_fpga_devt_type {
35 DFL_FPGA_DEVT_FME,
36 DFL_FPGA_DEVT_PORT,
37 DFL_FPGA_DEVT_MAX,
38};
39
Scott Wooddfe3de82019-05-09 16:08:28 -050040static struct lock_class_key dfl_pdata_keys[DFL_ID_MAX];
41
42static const char *dfl_pdata_key_strings[DFL_ID_MAX] = {
43 "dfl-fme-pdata",
44 "dfl-port-pdata",
45};
46
Wu Hao543be3d2018-06-30 08:53:13 +080047/**
48 * dfl_dev_info - dfl feature device information.
49 * @name: name string of the feature platform device.
50 * @dfh_id: id value in Device Feature Header (DFH) register by DFL spec.
51 * @id: idr id of the feature dev.
Wu Haob16c5142018-06-30 08:53:14 +080052 * @devt_type: index to dfl_chrdevs[].
Wu Hao543be3d2018-06-30 08:53:13 +080053 */
54struct dfl_dev_info {
55 const char *name;
Xu Yilun8a5de2d2020-08-10 10:41:10 +080056 u16 dfh_id;
Wu Hao543be3d2018-06-30 08:53:13 +080057 struct idr id;
Wu Haob16c5142018-06-30 08:53:14 +080058 enum dfl_fpga_devt_type devt_type;
Wu Hao543be3d2018-06-30 08:53:13 +080059};
60
61/* it is indexed by dfl_id_type */
62static struct dfl_dev_info dfl_devs[] = {
Wu Haob16c5142018-06-30 08:53:14 +080063 {.name = DFL_FPGA_FEATURE_DEV_FME, .dfh_id = DFH_ID_FIU_FME,
64 .devt_type = DFL_FPGA_DEVT_FME},
65 {.name = DFL_FPGA_FEATURE_DEV_PORT, .dfh_id = DFH_ID_FIU_PORT,
66 .devt_type = DFL_FPGA_DEVT_PORT},
67};
68
69/**
70 * dfl_chardev_info - chardev information of dfl feature device
71 * @name: nmae string of the char device.
72 * @devt: devt of the char device.
73 */
74struct dfl_chardev_info {
75 const char *name;
76 dev_t devt;
77};
78
79/* indexed by enum dfl_fpga_devt_type */
80static struct dfl_chardev_info dfl_chrdevs[] = {
81 {.name = DFL_FPGA_FEATURE_DEV_FME},
82 {.name = DFL_FPGA_FEATURE_DEV_PORT},
Wu Hao543be3d2018-06-30 08:53:13 +080083};
84
85static void dfl_ids_init(void)
86{
87 int i;
88
89 for (i = 0; i < ARRAY_SIZE(dfl_devs); i++)
90 idr_init(&dfl_devs[i].id);
91}
92
93static void dfl_ids_destroy(void)
94{
95 int i;
96
97 for (i = 0; i < ARRAY_SIZE(dfl_devs); i++)
98 idr_destroy(&dfl_devs[i].id);
99}
100
101static int dfl_id_alloc(enum dfl_id_type type, struct device *dev)
102{
103 int id;
104
105 WARN_ON(type >= DFL_ID_MAX);
106 mutex_lock(&dfl_id_mutex);
107 id = idr_alloc(&dfl_devs[type].id, dev, 0, 0, GFP_KERNEL);
108 mutex_unlock(&dfl_id_mutex);
109
110 return id;
111}
112
113static void dfl_id_free(enum dfl_id_type type, int id)
114{
115 WARN_ON(type >= DFL_ID_MAX);
116 mutex_lock(&dfl_id_mutex);
117 idr_remove(&dfl_devs[type].id, id);
118 mutex_unlock(&dfl_id_mutex);
119}
120
121static enum dfl_id_type feature_dev_id_type(struct platform_device *pdev)
122{
123 int i;
124
125 for (i = 0; i < ARRAY_SIZE(dfl_devs); i++)
126 if (!strcmp(dfl_devs[i].name, pdev->name))
127 return i;
128
129 return DFL_ID_MAX;
130}
131
Xu Yilun8a5de2d2020-08-10 10:41:10 +0800132static enum dfl_id_type dfh_id_to_type(u16 id)
Wu Hao543be3d2018-06-30 08:53:13 +0800133{
134 int i;
135
136 for (i = 0; i < ARRAY_SIZE(dfl_devs); i++)
137 if (dfl_devs[i].dfh_id == id)
138 return i;
139
140 return DFL_ID_MAX;
141}
142
Wu Hao6e8fd6e2018-06-30 08:53:17 +0800143/*
144 * introduce a global port_ops list, it allows port drivers to register ops
145 * in such list, then other feature devices (e.g. FME), could use the port
146 * functions even related port platform device is hidden. Below is one example,
147 * in virtualization case of PCIe-based FPGA DFL device, when SRIOV is
148 * enabled, port (and it's AFU) is turned into VF and port platform device
149 * is hidden from system but it's still required to access port to finish FPGA
150 * reconfiguration function in FME.
151 */
152
153static DEFINE_MUTEX(dfl_port_ops_mutex);
154static LIST_HEAD(dfl_port_ops_list);
155
156/**
157 * dfl_fpga_port_ops_get - get matched port ops from the global list
158 * @pdev: platform device to match with associated port ops.
159 * Return: matched port ops on success, NULL otherwise.
160 *
161 * Please note that must dfl_fpga_port_ops_put after use the port_ops.
162 */
163struct dfl_fpga_port_ops *dfl_fpga_port_ops_get(struct platform_device *pdev)
164{
165 struct dfl_fpga_port_ops *ops = NULL;
166
167 mutex_lock(&dfl_port_ops_mutex);
168 if (list_empty(&dfl_port_ops_list))
169 goto done;
170
171 list_for_each_entry(ops, &dfl_port_ops_list, node) {
172 /* match port_ops using the name of platform device */
173 if (!strcmp(pdev->name, ops->name)) {
174 if (!try_module_get(ops->owner))
175 ops = NULL;
176 goto done;
177 }
178 }
179
180 ops = NULL;
181done:
182 mutex_unlock(&dfl_port_ops_mutex);
183 return ops;
184}
185EXPORT_SYMBOL_GPL(dfl_fpga_port_ops_get);
186
187/**
188 * dfl_fpga_port_ops_put - put port ops
189 * @ops: port ops.
190 */
191void dfl_fpga_port_ops_put(struct dfl_fpga_port_ops *ops)
192{
193 if (ops && ops->owner)
194 module_put(ops->owner);
195}
196EXPORT_SYMBOL_GPL(dfl_fpga_port_ops_put);
197
198/**
199 * dfl_fpga_port_ops_add - add port_ops to global list
200 * @ops: port ops to add.
201 */
202void dfl_fpga_port_ops_add(struct dfl_fpga_port_ops *ops)
203{
204 mutex_lock(&dfl_port_ops_mutex);
205 list_add_tail(&ops->node, &dfl_port_ops_list);
206 mutex_unlock(&dfl_port_ops_mutex);
207}
208EXPORT_SYMBOL_GPL(dfl_fpga_port_ops_add);
209
210/**
211 * dfl_fpga_port_ops_del - remove port_ops from global list
212 * @ops: port ops to del.
213 */
214void dfl_fpga_port_ops_del(struct dfl_fpga_port_ops *ops)
215{
216 mutex_lock(&dfl_port_ops_mutex);
217 list_del(&ops->node);
218 mutex_unlock(&dfl_port_ops_mutex);
219}
220EXPORT_SYMBOL_GPL(dfl_fpga_port_ops_del);
221
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800222/**
Wu Haod06b0042018-06-30 08:53:18 +0800223 * dfl_fpga_check_port_id - check the port id
224 * @pdev: port platform device.
225 * @pport_id: port id to compare.
226 *
227 * Return: 1 if port device matches with given port id, otherwise 0.
228 */
229int dfl_fpga_check_port_id(struct platform_device *pdev, void *pport_id)
230{
Wu Hao69bb18d2019-08-04 18:20:11 +0800231 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
232 struct dfl_fpga_port_ops *port_ops;
Wu Haod06b0042018-06-30 08:53:18 +0800233
Wu Hao69bb18d2019-08-04 18:20:11 +0800234 if (pdata->id != FEATURE_DEV_ID_UNUSED)
235 return pdata->id == *(int *)pport_id;
236
237 port_ops = dfl_fpga_port_ops_get(pdev);
Wu Haod06b0042018-06-30 08:53:18 +0800238 if (!port_ops || !port_ops->get_id)
239 return 0;
240
Wu Hao69bb18d2019-08-04 18:20:11 +0800241 pdata->id = port_ops->get_id(pdev);
Wu Haod06b0042018-06-30 08:53:18 +0800242 dfl_fpga_port_ops_put(port_ops);
243
Wu Hao69bb18d2019-08-04 18:20:11 +0800244 return pdata->id == *(int *)pport_id;
Wu Haod06b0042018-06-30 08:53:18 +0800245}
246EXPORT_SYMBOL_GPL(dfl_fpga_check_port_id);
247
Xu Yilun9ba3a0a2020-09-07 22:23:13 +0800248static DEFINE_IDA(dfl_device_ida);
249
250static const struct dfl_device_id *
251dfl_match_one_device(const struct dfl_device_id *id, struct dfl_device *ddev)
252{
253 if (id->type == ddev->type && id->feature_id == ddev->feature_id)
254 return id;
255
256 return NULL;
257}
258
259static int dfl_bus_match(struct device *dev, struct device_driver *drv)
260{
261 struct dfl_device *ddev = to_dfl_dev(dev);
262 struct dfl_driver *ddrv = to_dfl_drv(drv);
263 const struct dfl_device_id *id_entry;
264
265 id_entry = ddrv->id_table;
266 if (id_entry) {
267 while (id_entry->feature_id) {
268 if (dfl_match_one_device(id_entry, ddev)) {
269 ddev->id_entry = id_entry;
270 return 1;
271 }
272 id_entry++;
273 }
274 }
275
276 return 0;
277}
278
279static int dfl_bus_probe(struct device *dev)
280{
281 struct dfl_driver *ddrv = to_dfl_drv(dev->driver);
282 struct dfl_device *ddev = to_dfl_dev(dev);
283
284 return ddrv->probe(ddev);
285}
286
287static int dfl_bus_remove(struct device *dev)
288{
289 struct dfl_driver *ddrv = to_dfl_drv(dev->driver);
290 struct dfl_device *ddev = to_dfl_dev(dev);
291
292 if (ddrv->remove)
293 ddrv->remove(ddev);
294
295 return 0;
296}
297
298static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
299{
300 struct dfl_device *ddev = to_dfl_dev(dev);
301
Xu Yilune08b9e62021-01-06 20:37:09 -0800302 return add_uevent_var(env, "MODALIAS=dfl:t%04Xf%04X",
Xu Yilun9ba3a0a2020-09-07 22:23:13 +0800303 ddev->type, ddev->feature_id);
304}
305
306static ssize_t
307type_show(struct device *dev, struct device_attribute *attr, char *buf)
308{
309 struct dfl_device *ddev = to_dfl_dev(dev);
310
311 return sprintf(buf, "0x%x\n", ddev->type);
312}
313static DEVICE_ATTR_RO(type);
314
315static ssize_t
316feature_id_show(struct device *dev, struct device_attribute *attr, char *buf)
317{
318 struct dfl_device *ddev = to_dfl_dev(dev);
319
320 return sprintf(buf, "0x%x\n", ddev->feature_id);
321}
322static DEVICE_ATTR_RO(feature_id);
323
324static struct attribute *dfl_dev_attrs[] = {
325 &dev_attr_type.attr,
326 &dev_attr_feature_id.attr,
327 NULL,
328};
329ATTRIBUTE_GROUPS(dfl_dev);
330
331static struct bus_type dfl_bus_type = {
332 .name = "dfl",
333 .match = dfl_bus_match,
334 .probe = dfl_bus_probe,
335 .remove = dfl_bus_remove,
336 .uevent = dfl_bus_uevent,
337 .dev_groups = dfl_dev_groups,
338};
339
340static void release_dfl_dev(struct device *dev)
341{
342 struct dfl_device *ddev = to_dfl_dev(dev);
343
344 if (ddev->mmio_res.parent)
345 release_resource(&ddev->mmio_res);
346
347 ida_simple_remove(&dfl_device_ida, ddev->id);
348 kfree(ddev->irqs);
349 kfree(ddev);
350}
351
352static struct dfl_device *
353dfl_dev_add(struct dfl_feature_platform_data *pdata,
354 struct dfl_feature *feature)
355{
356 struct platform_device *pdev = pdata->dev;
357 struct resource *parent_res;
358 struct dfl_device *ddev;
359 int id, i, ret;
360
361 ddev = kzalloc(sizeof(*ddev), GFP_KERNEL);
362 if (!ddev)
363 return ERR_PTR(-ENOMEM);
364
365 id = ida_simple_get(&dfl_device_ida, 0, 0, GFP_KERNEL);
366 if (id < 0) {
367 dev_err(&pdev->dev, "unable to get id\n");
368 kfree(ddev);
369 return ERR_PTR(id);
370 }
371
372 /* freeing resources by put_device() after device_initialize() */
373 device_initialize(&ddev->dev);
374 ddev->dev.parent = &pdev->dev;
375 ddev->dev.bus = &dfl_bus_type;
376 ddev->dev.release = release_dfl_dev;
377 ddev->id = id;
378 ret = dev_set_name(&ddev->dev, "dfl_dev.%d", id);
379 if (ret)
380 goto put_dev;
381
382 ddev->type = feature_dev_id_type(pdev);
383 ddev->feature_id = feature->id;
Martin Hundebøll16049862021-07-16 15:54:39 +0200384 ddev->revision = feature->revision;
Xu Yilun9ba3a0a2020-09-07 22:23:13 +0800385 ddev->cdev = pdata->dfl_cdev;
386
387 /* add mmio resource */
388 parent_res = &pdev->resource[feature->resource_index];
389 ddev->mmio_res.flags = IORESOURCE_MEM;
390 ddev->mmio_res.start = parent_res->start;
391 ddev->mmio_res.end = parent_res->end;
392 ddev->mmio_res.name = dev_name(&ddev->dev);
393 ret = insert_resource(parent_res, &ddev->mmio_res);
394 if (ret) {
395 dev_err(&pdev->dev, "%s failed to claim resource: %pR\n",
396 dev_name(&ddev->dev), &ddev->mmio_res);
397 goto put_dev;
398 }
399
400 /* then add irq resource */
401 if (feature->nr_irqs) {
402 ddev->irqs = kcalloc(feature->nr_irqs,
403 sizeof(*ddev->irqs), GFP_KERNEL);
404 if (!ddev->irqs) {
405 ret = -ENOMEM;
406 goto put_dev;
407 }
408
409 for (i = 0; i < feature->nr_irqs; i++)
410 ddev->irqs[i] = feature->irq_ctx[i].irq;
411
412 ddev->num_irqs = feature->nr_irqs;
413 }
414
415 ret = device_add(&ddev->dev);
416 if (ret)
417 goto put_dev;
418
419 dev_dbg(&pdev->dev, "add dfl_dev: %s\n", dev_name(&ddev->dev));
420 return ddev;
421
422put_dev:
423 /* calls release_dfl_dev() which does the clean up */
424 put_device(&ddev->dev);
425 return ERR_PTR(ret);
426}
427
428static void dfl_devs_remove(struct dfl_feature_platform_data *pdata)
429{
430 struct dfl_feature *feature;
431
432 dfl_fpga_dev_for_each_feature(pdata, feature) {
433 if (feature->ddev) {
434 device_unregister(&feature->ddev->dev);
435 feature->ddev = NULL;
436 }
437 }
438}
439
440static int dfl_devs_add(struct dfl_feature_platform_data *pdata)
441{
442 struct dfl_feature *feature;
443 struct dfl_device *ddev;
444 int ret;
445
446 dfl_fpga_dev_for_each_feature(pdata, feature) {
447 if (feature->ioaddr)
448 continue;
449
450 if (feature->ddev) {
451 ret = -EEXIST;
452 goto err;
453 }
454
455 ddev = dfl_dev_add(pdata, feature);
456 if (IS_ERR(ddev)) {
457 ret = PTR_ERR(ddev);
458 goto err;
459 }
460
461 feature->ddev = ddev;
462 }
463
464 return 0;
465
466err:
467 dfl_devs_remove(pdata);
468 return ret;
469}
470
471int __dfl_driver_register(struct dfl_driver *dfl_drv, struct module *owner)
472{
473 if (!dfl_drv || !dfl_drv->probe || !dfl_drv->id_table)
474 return -EINVAL;
475
476 dfl_drv->drv.owner = owner;
477 dfl_drv->drv.bus = &dfl_bus_type;
478
479 return driver_register(&dfl_drv->drv);
480}
481EXPORT_SYMBOL(__dfl_driver_register);
482
483void dfl_driver_unregister(struct dfl_driver *dfl_drv)
484{
485 driver_unregister(&dfl_drv->drv);
486}
487EXPORT_SYMBOL(dfl_driver_unregister);
488
Xu Yilun89eb35e2020-08-19 15:45:19 +0800489#define is_header_feature(feature) ((feature)->id == FEATURE_ID_FIU_HEADER)
490
Wu Haod06b0042018-06-30 08:53:18 +0800491/**
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800492 * dfl_fpga_dev_feature_uinit - uinit for sub features of dfl feature device
493 * @pdev: feature device.
494 */
495void dfl_fpga_dev_feature_uinit(struct platform_device *pdev)
496{
497 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
498 struct dfl_feature *feature;
499
Xu Yilun9ba3a0a2020-09-07 22:23:13 +0800500 dfl_devs_remove(pdata);
501
502 dfl_fpga_dev_for_each_feature(pdata, feature) {
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800503 if (feature->ops) {
Wu Hao3c51ff72019-08-04 18:20:18 +0800504 if (feature->ops->uinit)
505 feature->ops->uinit(pdev, feature);
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800506 feature->ops = NULL;
507 }
Xu Yilun9ba3a0a2020-09-07 22:23:13 +0800508 }
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800509}
510EXPORT_SYMBOL_GPL(dfl_fpga_dev_feature_uinit);
511
512static int dfl_feature_instance_init(struct platform_device *pdev,
513 struct dfl_feature_platform_data *pdata,
514 struct dfl_feature *feature,
515 struct dfl_feature_driver *drv)
516{
Xu Yilun89eb35e2020-08-19 15:45:19 +0800517 void __iomem *base;
Wu Hao84b693e2019-08-12 10:49:56 +0800518 int ret = 0;
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800519
Xu Yilun89eb35e2020-08-19 15:45:19 +0800520 if (!is_header_feature(feature)) {
521 base = devm_platform_ioremap_resource(pdev,
522 feature->resource_index);
523 if (IS_ERR(base)) {
524 dev_err(&pdev->dev,
525 "ioremap failed for feature 0x%x!\n",
526 feature->id);
527 return PTR_ERR(base);
528 }
529
530 feature->ioaddr = base;
531 }
532
Wu Hao84b693e2019-08-12 10:49:56 +0800533 if (drv->ops->init) {
534 ret = drv->ops->init(pdev, feature);
535 if (ret)
536 return ret;
537 }
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800538
539 feature->ops = drv->ops;
540
541 return ret;
542}
543
Wu Hao15bbb302019-08-04 18:20:15 +0800544static bool dfl_feature_drv_match(struct dfl_feature *feature,
545 struct dfl_feature_driver *driver)
546{
547 const struct dfl_feature_id *ids = driver->id_table;
548
549 if (ids) {
550 while (ids->id) {
551 if (ids->id == feature->id)
552 return true;
553 ids++;
554 }
555 }
556 return false;
557}
558
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800559/**
560 * dfl_fpga_dev_feature_init - init for sub features of dfl feature device
561 * @pdev: feature device.
562 * @feature_drvs: drvs for sub features.
563 *
564 * This function will match sub features with given feature drvs list and
565 * use matched drv to init related sub feature.
566 *
567 * Return: 0 on success, negative error code otherwise.
568 */
569int dfl_fpga_dev_feature_init(struct platform_device *pdev,
570 struct dfl_feature_driver *feature_drvs)
571{
572 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
573 struct dfl_feature_driver *drv = feature_drvs;
574 struct dfl_feature *feature;
575 int ret;
576
577 while (drv->ops) {
578 dfl_fpga_dev_for_each_feature(pdata, feature) {
Wu Hao15bbb302019-08-04 18:20:15 +0800579 if (dfl_feature_drv_match(feature, drv)) {
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800580 ret = dfl_feature_instance_init(pdev, pdata,
581 feature, drv);
582 if (ret)
583 goto exit;
584 }
585 }
586 drv++;
587 }
588
Xu Yilun9ba3a0a2020-09-07 22:23:13 +0800589 ret = dfl_devs_add(pdata);
590 if (ret)
591 goto exit;
592
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800593 return 0;
594exit:
595 dfl_fpga_dev_feature_uinit(pdev);
596 return ret;
597}
598EXPORT_SYMBOL_GPL(dfl_fpga_dev_feature_init);
599
Wu Haob16c5142018-06-30 08:53:14 +0800600static void dfl_chardev_uinit(void)
601{
602 int i;
603
604 for (i = 0; i < DFL_FPGA_DEVT_MAX; i++)
605 if (MAJOR(dfl_chrdevs[i].devt)) {
606 unregister_chrdev_region(dfl_chrdevs[i].devt,
Chengguang Xude9a7f62019-05-09 16:08:29 -0500607 MINORMASK + 1);
Wu Haob16c5142018-06-30 08:53:14 +0800608 dfl_chrdevs[i].devt = MKDEV(0, 0);
609 }
610}
611
612static int dfl_chardev_init(void)
613{
614 int i, ret;
615
616 for (i = 0; i < DFL_FPGA_DEVT_MAX; i++) {
Chengguang Xude9a7f62019-05-09 16:08:29 -0500617 ret = alloc_chrdev_region(&dfl_chrdevs[i].devt, 0,
618 MINORMASK + 1, dfl_chrdevs[i].name);
Wu Haob16c5142018-06-30 08:53:14 +0800619 if (ret)
620 goto exit;
621 }
622
623 return 0;
624
625exit:
626 dfl_chardev_uinit();
627 return ret;
628}
629
630static dev_t dfl_get_devt(enum dfl_fpga_devt_type type, int id)
631{
632 if (type >= DFL_FPGA_DEVT_MAX)
633 return 0;
634
635 return MKDEV(MAJOR(dfl_chrdevs[type].devt), id);
636}
637
638/**
639 * dfl_fpga_dev_ops_register - register cdev ops for feature dev
640 *
641 * @pdev: feature dev.
642 * @fops: file operations for feature dev's cdev.
643 * @owner: owning module/driver.
644 *
645 * Return: 0 on success, negative error code otherwise.
646 */
647int dfl_fpga_dev_ops_register(struct platform_device *pdev,
648 const struct file_operations *fops,
649 struct module *owner)
650{
651 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
652
653 cdev_init(&pdata->cdev, fops);
654 pdata->cdev.owner = owner;
655
656 /*
657 * set parent to the feature device so that its refcount is
658 * decreased after the last refcount of cdev is gone, that
659 * makes sure the feature device is valid during device
660 * file's life-cycle.
661 */
662 pdata->cdev.kobj.parent = &pdev->dev.kobj;
663
664 return cdev_add(&pdata->cdev, pdev->dev.devt, 1);
665}
666EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_register);
667
668/**
669 * dfl_fpga_dev_ops_unregister - unregister cdev ops for feature dev
670 * @pdev: feature dev.
671 */
672void dfl_fpga_dev_ops_unregister(struct platform_device *pdev)
673{
674 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
675
676 cdev_del(&pdata->cdev);
677}
678EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_unregister);
679
Wu Hao543be3d2018-06-30 08:53:13 +0800680/**
681 * struct build_feature_devs_info - info collected during feature dev build.
682 *
683 * @dev: device to enumerate.
684 * @cdev: the container device for all feature devices.
Xu Yilun8d021032020-06-16 12:08:42 +0800685 * @nr_irqs: number of irqs for all feature devices.
686 * @irq_table: Linux IRQ numbers for all irqs, indexed by local irq index of
687 * this device.
Wu Hao543be3d2018-06-30 08:53:13 +0800688 * @feature_dev: current feature device.
Xu Yilun89eb35e2020-08-19 15:45:19 +0800689 * @ioaddr: header register region address of current FIU in enumeration.
690 * @start: register resource start of current FIU.
691 * @len: max register resource length of current FIU.
Wu Hao543be3d2018-06-30 08:53:13 +0800692 * @sub_features: a sub features linked list for feature device in enumeration.
693 * @feature_num: number of sub features for feature device in enumeration.
694 */
695struct build_feature_devs_info {
696 struct device *dev;
697 struct dfl_fpga_cdev *cdev;
Xu Yilun8d021032020-06-16 12:08:42 +0800698 unsigned int nr_irqs;
699 int *irq_table;
700
Wu Hao543be3d2018-06-30 08:53:13 +0800701 struct platform_device *feature_dev;
702 void __iomem *ioaddr;
Xu Yilun89eb35e2020-08-19 15:45:19 +0800703 resource_size_t start;
704 resource_size_t len;
Wu Hao543be3d2018-06-30 08:53:13 +0800705 struct list_head sub_features;
706 int feature_num;
707};
708
709/**
710 * struct dfl_feature_info - sub feature info collected during feature dev build
711 *
712 * @fid: id of this sub feature.
713 * @mmio_res: mmio resource of this sub feature.
714 * @ioaddr: mapped base address of mmio resource.
715 * @node: node in sub_features linked list.
Xu Yilun8d021032020-06-16 12:08:42 +0800716 * @irq_base: start of irq index in this sub feature.
717 * @nr_irqs: number of irqs of this sub feature.
Wu Hao543be3d2018-06-30 08:53:13 +0800718 */
719struct dfl_feature_info {
Xu Yilun8a5de2d2020-08-10 10:41:10 +0800720 u16 fid;
Martin Hundebøll16049862021-07-16 15:54:39 +0200721 u8 revision;
Wu Hao543be3d2018-06-30 08:53:13 +0800722 struct resource mmio_res;
723 void __iomem *ioaddr;
724 struct list_head node;
Xu Yilun8d021032020-06-16 12:08:42 +0800725 unsigned int irq_base;
726 unsigned int nr_irqs;
Wu Hao543be3d2018-06-30 08:53:13 +0800727};
728
729static void dfl_fpga_cdev_add_port_dev(struct dfl_fpga_cdev *cdev,
730 struct platform_device *port)
731{
732 struct dfl_feature_platform_data *pdata = dev_get_platdata(&port->dev);
733
734 mutex_lock(&cdev->lock);
735 list_add(&pdata->node, &cdev->port_dev_list);
736 get_device(&pdata->dev->dev);
737 mutex_unlock(&cdev->lock);
738}
739
740/*
741 * register current feature device, it is called when we need to switch to
742 * another feature parsing or we have parsed all features on given device
743 * feature list.
744 */
745static int build_info_commit_dev(struct build_feature_devs_info *binfo)
746{
747 struct platform_device *fdev = binfo->feature_dev;
748 struct dfl_feature_platform_data *pdata;
749 struct dfl_feature_info *finfo, *p;
Scott Wooddfe3de82019-05-09 16:08:28 -0500750 enum dfl_id_type type;
Xu Yilun89eb35e2020-08-19 15:45:19 +0800751 int ret, index = 0, res_idx = 0;
Wu Hao543be3d2018-06-30 08:53:13 +0800752
Scott Wooddfe3de82019-05-09 16:08:28 -0500753 type = feature_dev_id_type(fdev);
754 if (WARN_ON_ONCE(type >= DFL_ID_MAX))
755 return -EINVAL;
756
Wu Hao543be3d2018-06-30 08:53:13 +0800757 /*
758 * we do not need to care for the memory which is associated with
759 * the platform device. After calling platform_device_unregister(),
760 * it will be automatically freed by device's release() callback,
761 * platform_device_release().
762 */
Gustavo A. R. Silvae1d9ec32020-06-17 17:10:39 -0500763 pdata = kzalloc(struct_size(pdata, features, binfo->feature_num), GFP_KERNEL);
Wu Hao543be3d2018-06-30 08:53:13 +0800764 if (!pdata)
765 return -ENOMEM;
766
767 pdata->dev = fdev;
768 pdata->num = binfo->feature_num;
769 pdata->dfl_cdev = binfo->cdev;
Wu Hao69bb18d2019-08-04 18:20:11 +0800770 pdata->id = FEATURE_DEV_ID_UNUSED;
Wu Hao543be3d2018-06-30 08:53:13 +0800771 mutex_init(&pdata->lock);
Scott Wooddfe3de82019-05-09 16:08:28 -0500772 lockdep_set_class_and_name(&pdata->lock, &dfl_pdata_keys[type],
773 dfl_pdata_key_strings[type]);
Wu Hao543be3d2018-06-30 08:53:13 +0800774
775 /*
776 * the count should be initialized to 0 to make sure
777 *__fpga_port_enable() following __fpga_port_disable()
778 * works properly for port device.
779 * and it should always be 0 for fme device.
780 */
781 WARN_ON(pdata->disable_count);
782
783 fdev->dev.platform_data = pdata;
784
785 /* each sub feature has one MMIO resource */
786 fdev->num_resources = binfo->feature_num;
787 fdev->resource = kcalloc(binfo->feature_num, sizeof(*fdev->resource),
788 GFP_KERNEL);
789 if (!fdev->resource)
790 return -ENOMEM;
791
792 /* fill features and resource information for feature dev */
793 list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
Xu Yilun89eb35e2020-08-19 15:45:19 +0800794 struct dfl_feature *feature = &pdata->features[index++];
Xu Yilun8d021032020-06-16 12:08:42 +0800795 struct dfl_feature_irq_ctx *ctx;
796 unsigned int i;
Wu Hao543be3d2018-06-30 08:53:13 +0800797
798 /* save resource information for each feature */
Xu Yilun322b5982020-06-16 12:08:44 +0800799 feature->dev = fdev;
Wu Hao543be3d2018-06-30 08:53:13 +0800800 feature->id = finfo->fid;
Martin Hundebøll16049862021-07-16 15:54:39 +0200801 feature->revision = finfo->revision;
Xu Yilun89eb35e2020-08-19 15:45:19 +0800802
803 /*
804 * the FIU header feature has some fundamental functions (sriov
805 * set, port enable/disable) needed for the dfl bus device and
806 * other sub features. So its mmio resource should be mapped by
807 * DFL bus device. And we should not assign it to feature
808 * devices (dfl-fme/afu) again.
809 */
810 if (is_header_feature(feature)) {
811 feature->resource_index = -1;
812 feature->ioaddr =
813 devm_ioremap_resource(binfo->dev,
814 &finfo->mmio_res);
815 if (IS_ERR(feature->ioaddr))
816 return PTR_ERR(feature->ioaddr);
817 } else {
818 feature->resource_index = res_idx;
819 fdev->resource[res_idx++] = finfo->mmio_res;
820 }
Wu Hao543be3d2018-06-30 08:53:13 +0800821
Xu Yilun8d021032020-06-16 12:08:42 +0800822 if (finfo->nr_irqs) {
823 ctx = devm_kcalloc(binfo->dev, finfo->nr_irqs,
824 sizeof(*ctx), GFP_KERNEL);
825 if (!ctx)
826 return -ENOMEM;
827
828 for (i = 0; i < finfo->nr_irqs; i++)
829 ctx[i].irq =
830 binfo->irq_table[finfo->irq_base + i];
831
832 feature->irq_ctx = ctx;
833 feature->nr_irqs = finfo->nr_irqs;
834 }
835
Wu Hao543be3d2018-06-30 08:53:13 +0800836 list_del(&finfo->node);
837 kfree(finfo);
838 }
839
840 ret = platform_device_add(binfo->feature_dev);
841 if (!ret) {
Scott Wooddfe3de82019-05-09 16:08:28 -0500842 if (type == PORT_ID)
Wu Hao543be3d2018-06-30 08:53:13 +0800843 dfl_fpga_cdev_add_port_dev(binfo->cdev,
844 binfo->feature_dev);
845 else
846 binfo->cdev->fme_dev =
847 get_device(&binfo->feature_dev->dev);
848 /*
849 * reset it to avoid build_info_free() freeing their resource.
850 *
851 * The resource of successfully registered feature devices
852 * will be freed by platform_device_unregister(). See the
853 * comments in build_info_create_dev().
854 */
855 binfo->feature_dev = NULL;
856 }
857
858 return ret;
859}
860
861static int
862build_info_create_dev(struct build_feature_devs_info *binfo,
Xu Yilun89eb35e2020-08-19 15:45:19 +0800863 enum dfl_id_type type)
Wu Hao543be3d2018-06-30 08:53:13 +0800864{
865 struct platform_device *fdev;
Wu Hao543be3d2018-06-30 08:53:13 +0800866
867 if (type >= DFL_ID_MAX)
868 return -EINVAL;
869
Wu Hao543be3d2018-06-30 08:53:13 +0800870 /*
871 * we use -ENODEV as the initialization indicator which indicates
872 * whether the id need to be reclaimed
873 */
874 fdev = platform_device_alloc(dfl_devs[type].name, -ENODEV);
875 if (!fdev)
876 return -ENOMEM;
877
878 binfo->feature_dev = fdev;
879 binfo->feature_num = 0;
Xu Yilun89eb35e2020-08-19 15:45:19 +0800880
Wu Hao543be3d2018-06-30 08:53:13 +0800881 INIT_LIST_HEAD(&binfo->sub_features);
882
883 fdev->id = dfl_id_alloc(type, &fdev->dev);
884 if (fdev->id < 0)
885 return fdev->id;
886
887 fdev->dev.parent = &binfo->cdev->region->dev;
Wu Haob16c5142018-06-30 08:53:14 +0800888 fdev->dev.devt = dfl_get_devt(dfl_devs[type].devt_type, fdev->id);
Wu Hao543be3d2018-06-30 08:53:13 +0800889
890 return 0;
891}
892
893static void build_info_free(struct build_feature_devs_info *binfo)
894{
895 struct dfl_feature_info *finfo, *p;
896
897 /*
898 * it is a valid id, free it. See comments in
899 * build_info_create_dev()
900 */
901 if (binfo->feature_dev && binfo->feature_dev->id >= 0) {
902 dfl_id_free(feature_dev_id_type(binfo->feature_dev),
903 binfo->feature_dev->id);
904
905 list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
906 list_del(&finfo->node);
907 kfree(finfo);
908 }
909 }
910
911 platform_device_put(binfo->feature_dev);
912
913 devm_kfree(binfo->dev, binfo);
914}
915
Martin Hundebøll16049862021-07-16 15:54:39 +0200916static inline u32 feature_size(u64 value)
Wu Hao543be3d2018-06-30 08:53:13 +0800917{
Martin Hundebøll16049862021-07-16 15:54:39 +0200918 u32 ofst = FIELD_GET(DFH_NEXT_HDR_OFST, value);
Wu Hao543be3d2018-06-30 08:53:13 +0800919 /* workaround for private features with invalid size, use 4K instead */
920 return ofst ? ofst : 4096;
921}
922
Martin Hundebøll16049862021-07-16 15:54:39 +0200923static u16 feature_id(u64 value)
Wu Hao543be3d2018-06-30 08:53:13 +0800924{
Martin Hundebøll16049862021-07-16 15:54:39 +0200925 u16 id = FIELD_GET(DFH_ID, value);
926 u8 type = FIELD_GET(DFH_TYPE, value);
Wu Hao543be3d2018-06-30 08:53:13 +0800927
928 if (type == DFH_TYPE_FIU)
929 return FEATURE_ID_FIU_HEADER;
930 else if (type == DFH_TYPE_PRIVATE)
931 return id;
932 else if (type == DFH_TYPE_AFU)
933 return FEATURE_ID_AFU;
934
935 WARN_ON(1);
936 return 0;
937}
938
Xu Yilun8d021032020-06-16 12:08:42 +0800939static int parse_feature_irqs(struct build_feature_devs_info *binfo,
Xu Yilun8a5de2d2020-08-10 10:41:10 +0800940 resource_size_t ofst, u16 fid,
Xu Yilun8d021032020-06-16 12:08:42 +0800941 unsigned int *irq_base, unsigned int *nr_irqs)
942{
943 void __iomem *base = binfo->ioaddr + ofst;
944 unsigned int i, ibase, inr = 0;
945 int virq;
946 u64 v;
947
948 /*
949 * Ideally DFL framework should only read info from DFL header, but
950 * current version DFL only provides mmio resources information for
951 * each feature in DFL Header, no field for interrupt resources.
952 * Interrupt resource information is provided by specific mmio
953 * registers of each private feature which supports interrupt. So in
954 * order to parse and assign irq resources, DFL framework has to look
955 * into specific capability registers of these private features.
956 *
957 * Once future DFL version supports generic interrupt resource
958 * information in common DFL headers, the generic interrupt parsing
959 * code will be added. But in order to be compatible to old version
960 * DFL, the driver may still fall back to these quirks.
961 */
962 switch (fid) {
963 case PORT_FEATURE_ID_UINT:
964 v = readq(base + PORT_UINT_CAP);
965 ibase = FIELD_GET(PORT_UINT_CAP_FST_VECT, v);
966 inr = FIELD_GET(PORT_UINT_CAP_INT_NUM, v);
967 break;
968 case PORT_FEATURE_ID_ERROR:
969 v = readq(base + PORT_ERROR_CAP);
970 ibase = FIELD_GET(PORT_ERROR_CAP_INT_VECT, v);
971 inr = FIELD_GET(PORT_ERROR_CAP_SUPP_INT, v);
972 break;
973 case FME_FEATURE_ID_GLOBAL_ERR:
974 v = readq(base + FME_ERROR_CAP);
975 ibase = FIELD_GET(FME_ERROR_CAP_INT_VECT, v);
976 inr = FIELD_GET(FME_ERROR_CAP_SUPP_INT, v);
977 break;
978 }
979
980 if (!inr) {
981 *irq_base = 0;
982 *nr_irqs = 0;
983 return 0;
984 }
985
Xu Yilun8a5de2d2020-08-10 10:41:10 +0800986 dev_dbg(binfo->dev, "feature: 0x%x, irq_base: %u, nr_irqs: %u\n",
Xu Yilun8d021032020-06-16 12:08:42 +0800987 fid, ibase, inr);
988
989 if (ibase + inr > binfo->nr_irqs) {
990 dev_err(binfo->dev,
Xu Yilun8a5de2d2020-08-10 10:41:10 +0800991 "Invalid interrupt number in feature 0x%x\n", fid);
Xu Yilun8d021032020-06-16 12:08:42 +0800992 return -EINVAL;
993 }
994
995 for (i = 0; i < inr; i++) {
996 virq = binfo->irq_table[ibase + i];
997 if (virq < 0 || virq > NR_IRQS) {
998 dev_err(binfo->dev,
Xu Yilun8a5de2d2020-08-10 10:41:10 +0800999 "Invalid irq table entry for feature 0x%x\n",
Xu Yilun8d021032020-06-16 12:08:42 +08001000 fid);
1001 return -EINVAL;
1002 }
1003 }
1004
1005 *irq_base = ibase;
1006 *nr_irqs = inr;
1007
1008 return 0;
1009}
1010
Wu Hao543be3d2018-06-30 08:53:13 +08001011/*
1012 * when create sub feature instances, for private features, it doesn't need
1013 * to provide resource size and feature id as they could be read from DFH
1014 * register. For afu sub feature, its register region only contains user
1015 * defined registers, so never trust any information from it, just use the
1016 * resource size information provided by its parent FIU.
1017 */
1018static int
1019create_feature_instance(struct build_feature_devs_info *binfo,
Xu Yilun89eb35e2020-08-19 15:45:19 +08001020 resource_size_t ofst, resource_size_t size, u16 fid)
Wu Hao543be3d2018-06-30 08:53:13 +08001021{
Xu Yilun8d021032020-06-16 12:08:42 +08001022 unsigned int irq_base, nr_irqs;
Wu Hao543be3d2018-06-30 08:53:13 +08001023 struct dfl_feature_info *finfo;
Xu Yilun8d021032020-06-16 12:08:42 +08001024 int ret;
Martin Hundebøll16049862021-07-16 15:54:39 +02001025 u8 revision;
1026 u64 v;
1027
1028 v = readq(binfo->ioaddr + ofst);
1029 revision = FIELD_GET(DFH_REVISION, v);
Wu Hao543be3d2018-06-30 08:53:13 +08001030
1031 /* read feature size and id if inputs are invalid */
Martin Hundebøll16049862021-07-16 15:54:39 +02001032 size = size ? size : feature_size(v);
1033 fid = fid ? fid : feature_id(v);
Wu Hao543be3d2018-06-30 08:53:13 +08001034
Xu Yilun89eb35e2020-08-19 15:45:19 +08001035 if (binfo->len - ofst < size)
Wu Hao543be3d2018-06-30 08:53:13 +08001036 return -EINVAL;
1037
Xu Yilun8d021032020-06-16 12:08:42 +08001038 ret = parse_feature_irqs(binfo, ofst, fid, &irq_base, &nr_irqs);
1039 if (ret)
1040 return ret;
1041
Wu Hao543be3d2018-06-30 08:53:13 +08001042 finfo = kzalloc(sizeof(*finfo), GFP_KERNEL);
1043 if (!finfo)
1044 return -ENOMEM;
1045
1046 finfo->fid = fid;
Martin Hundebøll16049862021-07-16 15:54:39 +02001047 finfo->revision = revision;
Xu Yilun89eb35e2020-08-19 15:45:19 +08001048 finfo->mmio_res.start = binfo->start + ofst;
Wu Hao543be3d2018-06-30 08:53:13 +08001049 finfo->mmio_res.end = finfo->mmio_res.start + size - 1;
1050 finfo->mmio_res.flags = IORESOURCE_MEM;
Xu Yilun8d021032020-06-16 12:08:42 +08001051 finfo->irq_base = irq_base;
1052 finfo->nr_irqs = nr_irqs;
Wu Hao543be3d2018-06-30 08:53:13 +08001053
1054 list_add_tail(&finfo->node, &binfo->sub_features);
1055 binfo->feature_num++;
1056
1057 return 0;
1058}
1059
1060static int parse_feature_port_afu(struct build_feature_devs_info *binfo,
Wu Hao543be3d2018-06-30 08:53:13 +08001061 resource_size_t ofst)
1062{
1063 u64 v = readq(binfo->ioaddr + PORT_HDR_CAP);
1064 u32 size = FIELD_GET(PORT_CAP_MMIO_SIZE, v) << 10;
1065
1066 WARN_ON(!size);
1067
Xu Yilun89eb35e2020-08-19 15:45:19 +08001068 return create_feature_instance(binfo, ofst, size, FEATURE_ID_AFU);
Wu Hao543be3d2018-06-30 08:53:13 +08001069}
1070
Xu Yilun89eb35e2020-08-19 15:45:19 +08001071#define is_feature_dev_detected(binfo) (!!(binfo)->feature_dev)
1072
Wu Hao543be3d2018-06-30 08:53:13 +08001073static int parse_feature_afu(struct build_feature_devs_info *binfo,
Wu Hao543be3d2018-06-30 08:53:13 +08001074 resource_size_t ofst)
1075{
Xu Yilun89eb35e2020-08-19 15:45:19 +08001076 if (!is_feature_dev_detected(binfo)) {
Wu Hao543be3d2018-06-30 08:53:13 +08001077 dev_err(binfo->dev, "this AFU does not belong to any FIU.\n");
1078 return -EINVAL;
1079 }
1080
1081 switch (feature_dev_id_type(binfo->feature_dev)) {
1082 case PORT_ID:
Xu Yilun89eb35e2020-08-19 15:45:19 +08001083 return parse_feature_port_afu(binfo, ofst);
Wu Hao543be3d2018-06-30 08:53:13 +08001084 default:
1085 dev_info(binfo->dev, "AFU belonging to FIU %s is not supported yet.\n",
1086 binfo->feature_dev->name);
1087 }
1088
1089 return 0;
1090}
1091
Xu Yilun89eb35e2020-08-19 15:45:19 +08001092static int build_info_prepare(struct build_feature_devs_info *binfo,
1093 resource_size_t start, resource_size_t len)
1094{
1095 struct device *dev = binfo->dev;
1096 void __iomem *ioaddr;
1097
1098 if (!devm_request_mem_region(dev, start, len, dev_name(dev))) {
1099 dev_err(dev, "request region fail, start:%pa, len:%pa\n",
1100 &start, &len);
1101 return -EBUSY;
1102 }
1103
1104 ioaddr = devm_ioremap(dev, start, len);
1105 if (!ioaddr) {
1106 dev_err(dev, "ioremap region fail, start:%pa, len:%pa\n",
1107 &start, &len);
1108 return -ENOMEM;
1109 }
1110
1111 binfo->start = start;
1112 binfo->len = len;
1113 binfo->ioaddr = ioaddr;
1114
1115 return 0;
1116}
1117
1118static void build_info_complete(struct build_feature_devs_info *binfo)
1119{
1120 devm_iounmap(binfo->dev, binfo->ioaddr);
1121 devm_release_mem_region(binfo->dev, binfo->start, binfo->len);
1122}
1123
Wu Hao543be3d2018-06-30 08:53:13 +08001124static int parse_feature_fiu(struct build_feature_devs_info *binfo,
Wu Hao543be3d2018-06-30 08:53:13 +08001125 resource_size_t ofst)
1126{
Wu Hao543be3d2018-06-30 08:53:13 +08001127 int ret = 0;
Xu Yilun8a5de2d2020-08-10 10:41:10 +08001128 u32 offset;
1129 u16 id;
1130 u64 v;
Wu Hao543be3d2018-06-30 08:53:13 +08001131
Xu Yilun89eb35e2020-08-19 15:45:19 +08001132 if (is_feature_dev_detected(binfo)) {
1133 build_info_complete(binfo);
1134
1135 ret = build_info_commit_dev(binfo);
1136 if (ret)
1137 return ret;
1138
1139 ret = build_info_prepare(binfo, binfo->start + ofst,
1140 binfo->len - ofst);
1141 if (ret)
1142 return ret;
1143 }
1144
1145 v = readq(binfo->ioaddr + DFH);
Wu Hao543be3d2018-06-30 08:53:13 +08001146 id = FIELD_GET(DFH_ID, v);
1147
1148 /* create platform device for dfl feature dev */
Xu Yilun89eb35e2020-08-19 15:45:19 +08001149 ret = build_info_create_dev(binfo, dfh_id_to_type(id));
Wu Hao543be3d2018-06-30 08:53:13 +08001150 if (ret)
1151 return ret;
1152
Xu Yilun89eb35e2020-08-19 15:45:19 +08001153 ret = create_feature_instance(binfo, 0, 0, 0);
Wu Hao543be3d2018-06-30 08:53:13 +08001154 if (ret)
1155 return ret;
1156 /*
1157 * find and parse FIU's child AFU via its NEXT_AFU register.
1158 * please note that only Port has valid NEXT_AFU pointer per spec.
1159 */
Xu Yilun89eb35e2020-08-19 15:45:19 +08001160 v = readq(binfo->ioaddr + NEXT_AFU);
Wu Hao543be3d2018-06-30 08:53:13 +08001161
1162 offset = FIELD_GET(NEXT_AFU_NEXT_DFH_OFST, v);
1163 if (offset)
Xu Yilun89eb35e2020-08-19 15:45:19 +08001164 return parse_feature_afu(binfo, offset);
Wu Hao543be3d2018-06-30 08:53:13 +08001165
1166 dev_dbg(binfo->dev, "No AFUs detected on FIU %d\n", id);
1167
1168 return ret;
1169}
1170
1171static int parse_feature_private(struct build_feature_devs_info *binfo,
Wu Hao543be3d2018-06-30 08:53:13 +08001172 resource_size_t ofst)
1173{
Xu Yilun89eb35e2020-08-19 15:45:19 +08001174 if (!is_feature_dev_detected(binfo)) {
Xu Yilun8a5de2d2020-08-10 10:41:10 +08001175 dev_err(binfo->dev, "the private feature 0x%x does not belong to any AFU.\n",
Martin Hundebøll16049862021-07-16 15:54:39 +02001176 feature_id(readq(binfo->ioaddr + ofst)));
Wu Hao543be3d2018-06-30 08:53:13 +08001177 return -EINVAL;
1178 }
1179
Xu Yilun89eb35e2020-08-19 15:45:19 +08001180 return create_feature_instance(binfo, ofst, 0, 0);
Wu Hao543be3d2018-06-30 08:53:13 +08001181}
1182
1183/**
1184 * parse_feature - parse a feature on given device feature list
1185 *
1186 * @binfo: build feature devices information.
Xu Yilun89eb35e2020-08-19 15:45:19 +08001187 * @ofst: offset to current FIU header
Wu Hao543be3d2018-06-30 08:53:13 +08001188 */
1189static int parse_feature(struct build_feature_devs_info *binfo,
Xu Yilun89eb35e2020-08-19 15:45:19 +08001190 resource_size_t ofst)
Wu Hao543be3d2018-06-30 08:53:13 +08001191{
1192 u64 v;
1193 u32 type;
1194
Xu Yilun89eb35e2020-08-19 15:45:19 +08001195 v = readq(binfo->ioaddr + ofst + DFH);
Wu Hao543be3d2018-06-30 08:53:13 +08001196 type = FIELD_GET(DFH_TYPE, v);
1197
1198 switch (type) {
1199 case DFH_TYPE_AFU:
Xu Yilun89eb35e2020-08-19 15:45:19 +08001200 return parse_feature_afu(binfo, ofst);
Wu Hao543be3d2018-06-30 08:53:13 +08001201 case DFH_TYPE_PRIVATE:
Xu Yilun89eb35e2020-08-19 15:45:19 +08001202 return parse_feature_private(binfo, ofst);
Wu Hao543be3d2018-06-30 08:53:13 +08001203 case DFH_TYPE_FIU:
Xu Yilun89eb35e2020-08-19 15:45:19 +08001204 return parse_feature_fiu(binfo, ofst);
Wu Hao543be3d2018-06-30 08:53:13 +08001205 default:
1206 dev_info(binfo->dev,
1207 "Feature Type %x is not supported.\n", type);
1208 }
1209
1210 return 0;
1211}
1212
1213static int parse_feature_list(struct build_feature_devs_info *binfo,
Xu Yilun89eb35e2020-08-19 15:45:19 +08001214 resource_size_t start, resource_size_t len)
Wu Hao543be3d2018-06-30 08:53:13 +08001215{
Xu Yilun89eb35e2020-08-19 15:45:19 +08001216 resource_size_t end = start + len;
Wu Hao543be3d2018-06-30 08:53:13 +08001217 int ret = 0;
1218 u32 ofst = 0;
1219 u64 v;
1220
Xu Yilun89eb35e2020-08-19 15:45:19 +08001221 ret = build_info_prepare(binfo, start, len);
1222 if (ret)
1223 return ret;
1224
Wu Hao543be3d2018-06-30 08:53:13 +08001225 /* walk through the device feature list via DFH's next DFH pointer. */
1226 for (; start < end; start += ofst) {
1227 if (end - start < DFH_SIZE) {
1228 dev_err(binfo->dev, "The region is too small to contain a feature.\n");
1229 return -EINVAL;
1230 }
1231
Xu Yilun89eb35e2020-08-19 15:45:19 +08001232 ret = parse_feature(binfo, start - binfo->start);
Wu Hao543be3d2018-06-30 08:53:13 +08001233 if (ret)
1234 return ret;
1235
Xu Yilun89eb35e2020-08-19 15:45:19 +08001236 v = readq(binfo->ioaddr + start - binfo->start + DFH);
Wu Hao543be3d2018-06-30 08:53:13 +08001237 ofst = FIELD_GET(DFH_NEXT_HDR_OFST, v);
1238
1239 /* stop parsing if EOL(End of List) is set or offset is 0 */
1240 if ((v & DFH_EOL) || !ofst)
1241 break;
1242 }
1243
1244 /* commit current feature device when reach the end of list */
Xu Yilun89eb35e2020-08-19 15:45:19 +08001245 build_info_complete(binfo);
1246
1247 if (is_feature_dev_detected(binfo))
1248 ret = build_info_commit_dev(binfo);
1249
1250 return ret;
Wu Hao543be3d2018-06-30 08:53:13 +08001251}
1252
1253struct dfl_fpga_enum_info *dfl_fpga_enum_info_alloc(struct device *dev)
1254{
1255 struct dfl_fpga_enum_info *info;
1256
1257 get_device(dev);
1258
1259 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1260 if (!info) {
1261 put_device(dev);
1262 return NULL;
1263 }
1264
1265 info->dev = dev;
1266 INIT_LIST_HEAD(&info->dfls);
1267
1268 return info;
1269}
1270EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_alloc);
1271
1272void dfl_fpga_enum_info_free(struct dfl_fpga_enum_info *info)
1273{
1274 struct dfl_fpga_enum_dfl *tmp, *dfl;
1275 struct device *dev;
1276
1277 if (!info)
1278 return;
1279
1280 dev = info->dev;
1281
1282 /* remove all device feature lists in the list. */
1283 list_for_each_entry_safe(dfl, tmp, &info->dfls, node) {
1284 list_del(&dfl->node);
1285 devm_kfree(dev, dfl);
1286 }
1287
Xu Yilun8d021032020-06-16 12:08:42 +08001288 /* remove irq table */
1289 if (info->irq_table)
1290 devm_kfree(dev, info->irq_table);
1291
Wu Hao543be3d2018-06-30 08:53:13 +08001292 devm_kfree(dev, info);
1293 put_device(dev);
1294}
1295EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_free);
1296
1297/**
1298 * dfl_fpga_enum_info_add_dfl - add info of a device feature list to enum info
1299 *
1300 * @info: ptr to dfl_fpga_enum_info
1301 * @start: mmio resource address of the device feature list.
1302 * @len: mmio resource length of the device feature list.
Wu Hao543be3d2018-06-30 08:53:13 +08001303 *
1304 * One FPGA device may have one or more Device Feature Lists (DFLs), use this
1305 * function to add information of each DFL to common data structure for next
1306 * step enumeration.
1307 *
1308 * Return: 0 on success, negative error code otherwise.
1309 */
1310int dfl_fpga_enum_info_add_dfl(struct dfl_fpga_enum_info *info,
Xu Yilun89eb35e2020-08-19 15:45:19 +08001311 resource_size_t start, resource_size_t len)
Wu Hao543be3d2018-06-30 08:53:13 +08001312{
1313 struct dfl_fpga_enum_dfl *dfl;
1314
1315 dfl = devm_kzalloc(info->dev, sizeof(*dfl), GFP_KERNEL);
1316 if (!dfl)
1317 return -ENOMEM;
1318
1319 dfl->start = start;
1320 dfl->len = len;
Wu Hao543be3d2018-06-30 08:53:13 +08001321
1322 list_add_tail(&dfl->node, &info->dfls);
1323
1324 return 0;
1325}
1326EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_add_dfl);
1327
Xu Yilun8d021032020-06-16 12:08:42 +08001328/**
1329 * dfl_fpga_enum_info_add_irq - add irq table to enum info
1330 *
1331 * @info: ptr to dfl_fpga_enum_info
1332 * @nr_irqs: number of irqs of the DFL fpga device to be enumerated.
1333 * @irq_table: Linux IRQ numbers for all irqs, indexed by local irq index of
1334 * this device.
1335 *
1336 * One FPGA device may have several interrupts. This function adds irq
1337 * information of the DFL fpga device to enum info for next step enumeration.
1338 * This function should be called before dfl_fpga_feature_devs_enumerate().
1339 * As we only support one irq domain for all DFLs in the same enum info, adding
1340 * irq table a second time for the same enum info will return error.
1341 *
1342 * If we need to enumerate DFLs which belong to different irq domains, we
1343 * should fill more enum info and enumerate them one by one.
1344 *
1345 * Return: 0 on success, negative error code otherwise.
1346 */
1347int dfl_fpga_enum_info_add_irq(struct dfl_fpga_enum_info *info,
1348 unsigned int nr_irqs, int *irq_table)
1349{
1350 if (!nr_irqs || !irq_table)
1351 return -EINVAL;
1352
1353 if (info->irq_table)
1354 return -EEXIST;
1355
1356 info->irq_table = devm_kmemdup(info->dev, irq_table,
1357 sizeof(int) * nr_irqs, GFP_KERNEL);
1358 if (!info->irq_table)
1359 return -ENOMEM;
1360
1361 info->nr_irqs = nr_irqs;
1362
1363 return 0;
1364}
1365EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_add_irq);
1366
Wu Hao543be3d2018-06-30 08:53:13 +08001367static int remove_feature_dev(struct device *dev, void *data)
1368{
1369 struct platform_device *pdev = to_platform_device(dev);
1370 enum dfl_id_type type = feature_dev_id_type(pdev);
1371 int id = pdev->id;
1372
1373 platform_device_unregister(pdev);
1374
1375 dfl_id_free(type, id);
1376
1377 return 0;
1378}
1379
1380static void remove_feature_devs(struct dfl_fpga_cdev *cdev)
1381{
1382 device_for_each_child(&cdev->region->dev, NULL, remove_feature_dev);
1383}
1384
1385/**
1386 * dfl_fpga_feature_devs_enumerate - enumerate feature devices
1387 * @info: information for enumeration.
1388 *
1389 * This function creates a container device (base FPGA region), enumerates
1390 * feature devices based on the enumeration info and creates platform devices
1391 * under the container device.
1392 *
1393 * Return: dfl_fpga_cdev struct on success, -errno on failure
1394 */
1395struct dfl_fpga_cdev *
1396dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info)
1397{
1398 struct build_feature_devs_info *binfo;
1399 struct dfl_fpga_enum_dfl *dfl;
1400 struct dfl_fpga_cdev *cdev;
1401 int ret = 0;
1402
1403 if (!info->dev)
1404 return ERR_PTR(-ENODEV);
1405
1406 cdev = devm_kzalloc(info->dev, sizeof(*cdev), GFP_KERNEL);
1407 if (!cdev)
1408 return ERR_PTR(-ENOMEM);
1409
Alan Tullfea82b72018-10-15 17:20:03 -05001410 cdev->region = devm_fpga_region_create(info->dev, NULL, NULL);
Wu Hao543be3d2018-06-30 08:53:13 +08001411 if (!cdev->region) {
1412 ret = -ENOMEM;
1413 goto free_cdev_exit;
1414 }
1415
1416 cdev->parent = info->dev;
1417 mutex_init(&cdev->lock);
1418 INIT_LIST_HEAD(&cdev->port_dev_list);
1419
1420 ret = fpga_region_register(cdev->region);
1421 if (ret)
Alan Tullfea82b72018-10-15 17:20:03 -05001422 goto free_cdev_exit;
Wu Hao543be3d2018-06-30 08:53:13 +08001423
1424 /* create and init build info for enumeration */
1425 binfo = devm_kzalloc(info->dev, sizeof(*binfo), GFP_KERNEL);
1426 if (!binfo) {
1427 ret = -ENOMEM;
1428 goto unregister_region_exit;
1429 }
1430
1431 binfo->dev = info->dev;
1432 binfo->cdev = cdev;
1433
Xu Yilun8d021032020-06-16 12:08:42 +08001434 binfo->nr_irqs = info->nr_irqs;
1435 if (info->nr_irqs)
1436 binfo->irq_table = info->irq_table;
1437
Wu Hao543be3d2018-06-30 08:53:13 +08001438 /*
1439 * start enumeration for all feature devices based on Device Feature
1440 * Lists.
1441 */
1442 list_for_each_entry(dfl, &info->dfls, node) {
Xu Yilun89eb35e2020-08-19 15:45:19 +08001443 ret = parse_feature_list(binfo, dfl->start, dfl->len);
Wu Hao543be3d2018-06-30 08:53:13 +08001444 if (ret) {
1445 remove_feature_devs(cdev);
1446 build_info_free(binfo);
1447 goto unregister_region_exit;
1448 }
1449 }
1450
1451 build_info_free(binfo);
1452
1453 return cdev;
1454
1455unregister_region_exit:
1456 fpga_region_unregister(cdev->region);
Wu Hao543be3d2018-06-30 08:53:13 +08001457free_cdev_exit:
1458 devm_kfree(info->dev, cdev);
1459 return ERR_PTR(ret);
1460}
1461EXPORT_SYMBOL_GPL(dfl_fpga_feature_devs_enumerate);
1462
1463/**
1464 * dfl_fpga_feature_devs_remove - remove all feature devices
1465 * @cdev: fpga container device.
1466 *
1467 * Remove the container device and all feature devices under given container
1468 * devices.
1469 */
1470void dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev *cdev)
1471{
1472 struct dfl_feature_platform_data *pdata, *ptmp;
1473
Wu Hao543be3d2018-06-30 08:53:13 +08001474 mutex_lock(&cdev->lock);
Wu Hao69bb18d2019-08-04 18:20:11 +08001475 if (cdev->fme_dev)
Wu Hao543be3d2018-06-30 08:53:13 +08001476 put_device(cdev->fme_dev);
Wu Hao543be3d2018-06-30 08:53:13 +08001477
1478 list_for_each_entry_safe(pdata, ptmp, &cdev->port_dev_list, node) {
1479 struct platform_device *port_dev = pdata->dev;
1480
Wu Hao69bb18d2019-08-04 18:20:11 +08001481 /* remove released ports */
1482 if (!device_is_registered(&port_dev->dev)) {
1483 dfl_id_free(feature_dev_id_type(port_dev),
1484 port_dev->id);
1485 platform_device_put(port_dev);
1486 }
1487
Wu Hao543be3d2018-06-30 08:53:13 +08001488 list_del(&pdata->node);
1489 put_device(&port_dev->dev);
1490 }
1491 mutex_unlock(&cdev->lock);
1492
Wu Hao69bb18d2019-08-04 18:20:11 +08001493 remove_feature_devs(cdev);
1494
Wu Hao543be3d2018-06-30 08:53:13 +08001495 fpga_region_unregister(cdev->region);
1496 devm_kfree(cdev->parent, cdev);
1497}
1498EXPORT_SYMBOL_GPL(dfl_fpga_feature_devs_remove);
1499
Wu Hao5d56e112018-06-30 08:53:15 +08001500/**
1501 * __dfl_fpga_cdev_find_port - find a port under given container device
1502 *
1503 * @cdev: container device
1504 * @data: data passed to match function
1505 * @match: match function used to find specific port from the port device list
1506 *
1507 * Find a port device under container device. This function needs to be
1508 * invoked with lock held.
1509 *
1510 * Return: pointer to port's platform device if successful, NULL otherwise.
1511 *
1512 * NOTE: you will need to drop the device reference with put_device() after use.
1513 */
1514struct platform_device *
1515__dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
1516 int (*match)(struct platform_device *, void *))
1517{
1518 struct dfl_feature_platform_data *pdata;
1519 struct platform_device *port_dev;
1520
1521 list_for_each_entry(pdata, &cdev->port_dev_list, node) {
1522 port_dev = pdata->dev;
1523
1524 if (match(port_dev, data) && get_device(&port_dev->dev))
1525 return port_dev;
1526 }
1527
1528 return NULL;
1529}
1530EXPORT_SYMBOL_GPL(__dfl_fpga_cdev_find_port);
1531
Wu Hao543be3d2018-06-30 08:53:13 +08001532static int __init dfl_fpga_init(void)
1533{
Wu Haob16c5142018-06-30 08:53:14 +08001534 int ret;
1535
Xu Yilun9ba3a0a2020-09-07 22:23:13 +08001536 ret = bus_register(&dfl_bus_type);
1537 if (ret)
1538 return ret;
1539
Wu Hao543be3d2018-06-30 08:53:13 +08001540 dfl_ids_init();
1541
Wu Haob16c5142018-06-30 08:53:14 +08001542 ret = dfl_chardev_init();
Xu Yilun9ba3a0a2020-09-07 22:23:13 +08001543 if (ret) {
Wu Haob16c5142018-06-30 08:53:14 +08001544 dfl_ids_destroy();
Xu Yilun9ba3a0a2020-09-07 22:23:13 +08001545 bus_unregister(&dfl_bus_type);
1546 }
Wu Haob16c5142018-06-30 08:53:14 +08001547
1548 return ret;
Wu Hao543be3d2018-06-30 08:53:13 +08001549}
1550
Wu Hao69bb18d2019-08-04 18:20:11 +08001551/**
1552 * dfl_fpga_cdev_release_port - release a port platform device
1553 *
1554 * @cdev: parent container device.
1555 * @port_id: id of the port platform device.
1556 *
1557 * This function allows user to release a port platform device. This is a
1558 * mandatory step before turn a port from PF into VF for SRIOV support.
1559 *
1560 * Return: 0 on success, negative error code otherwise.
1561 */
1562int dfl_fpga_cdev_release_port(struct dfl_fpga_cdev *cdev, int port_id)
1563{
Xu Yilunb6862192019-11-18 13:20:41 +08001564 struct dfl_feature_platform_data *pdata;
Wu Hao69bb18d2019-08-04 18:20:11 +08001565 struct platform_device *port_pdev;
1566 int ret = -ENODEV;
1567
1568 mutex_lock(&cdev->lock);
1569 port_pdev = __dfl_fpga_cdev_find_port(cdev, &port_id,
1570 dfl_fpga_check_port_id);
1571 if (!port_pdev)
1572 goto unlock_exit;
1573
1574 if (!device_is_registered(&port_pdev->dev)) {
1575 ret = -EBUSY;
1576 goto put_dev_exit;
1577 }
1578
Xu Yilunb6862192019-11-18 13:20:41 +08001579 pdata = dev_get_platdata(&port_pdev->dev);
1580
1581 mutex_lock(&pdata->lock);
1582 ret = dfl_feature_dev_use_begin(pdata, true);
1583 mutex_unlock(&pdata->lock);
Wu Hao69bb18d2019-08-04 18:20:11 +08001584 if (ret)
1585 goto put_dev_exit;
1586
1587 platform_device_del(port_pdev);
1588 cdev->released_port_num++;
1589put_dev_exit:
1590 put_device(&port_pdev->dev);
1591unlock_exit:
1592 mutex_unlock(&cdev->lock);
1593 return ret;
1594}
1595EXPORT_SYMBOL_GPL(dfl_fpga_cdev_release_port);
1596
1597/**
1598 * dfl_fpga_cdev_assign_port - assign a port platform device back
1599 *
1600 * @cdev: parent container device.
1601 * @port_id: id of the port platform device.
1602 *
1603 * This function allows user to assign a port platform device back. This is
1604 * a mandatory step after disable SRIOV support.
1605 *
1606 * Return: 0 on success, negative error code otherwise.
1607 */
1608int dfl_fpga_cdev_assign_port(struct dfl_fpga_cdev *cdev, int port_id)
1609{
Xu Yilunb6862192019-11-18 13:20:41 +08001610 struct dfl_feature_platform_data *pdata;
Wu Hao69bb18d2019-08-04 18:20:11 +08001611 struct platform_device *port_pdev;
1612 int ret = -ENODEV;
1613
1614 mutex_lock(&cdev->lock);
1615 port_pdev = __dfl_fpga_cdev_find_port(cdev, &port_id,
1616 dfl_fpga_check_port_id);
1617 if (!port_pdev)
1618 goto unlock_exit;
1619
1620 if (device_is_registered(&port_pdev->dev)) {
1621 ret = -EBUSY;
1622 goto put_dev_exit;
1623 }
1624
1625 ret = platform_device_add(port_pdev);
1626 if (ret)
1627 goto put_dev_exit;
1628
Xu Yilunb6862192019-11-18 13:20:41 +08001629 pdata = dev_get_platdata(&port_pdev->dev);
1630
1631 mutex_lock(&pdata->lock);
1632 dfl_feature_dev_use_end(pdata);
1633 mutex_unlock(&pdata->lock);
1634
Wu Hao69bb18d2019-08-04 18:20:11 +08001635 cdev->released_port_num--;
1636put_dev_exit:
1637 put_device(&port_pdev->dev);
1638unlock_exit:
1639 mutex_unlock(&cdev->lock);
1640 return ret;
1641}
1642EXPORT_SYMBOL_GPL(dfl_fpga_cdev_assign_port);
1643
Wu Haobdd4f302019-08-04 18:20:12 +08001644static void config_port_access_mode(struct device *fme_dev, int port_id,
1645 bool is_vf)
1646{
1647 void __iomem *base;
1648 u64 v;
1649
1650 base = dfl_get_feature_ioaddr_by_id(fme_dev, FME_FEATURE_ID_HEADER);
1651
1652 v = readq(base + FME_HDR_PORT_OFST(port_id));
1653
1654 v &= ~FME_PORT_OFST_ACC_CTRL;
1655 v |= FIELD_PREP(FME_PORT_OFST_ACC_CTRL,
1656 is_vf ? FME_PORT_OFST_ACC_VF : FME_PORT_OFST_ACC_PF);
1657
1658 writeq(v, base + FME_HDR_PORT_OFST(port_id));
1659}
1660
1661#define config_port_vf_mode(dev, id) config_port_access_mode(dev, id, true)
1662#define config_port_pf_mode(dev, id) config_port_access_mode(dev, id, false)
1663
1664/**
1665 * dfl_fpga_cdev_config_ports_pf - configure ports to PF access mode
1666 *
1667 * @cdev: parent container device.
1668 *
1669 * This function is needed in sriov configuration routine. It could be used to
1670 * configure the all released ports from VF access mode to PF.
1671 */
1672void dfl_fpga_cdev_config_ports_pf(struct dfl_fpga_cdev *cdev)
1673{
1674 struct dfl_feature_platform_data *pdata;
1675
1676 mutex_lock(&cdev->lock);
1677 list_for_each_entry(pdata, &cdev->port_dev_list, node) {
1678 if (device_is_registered(&pdata->dev->dev))
1679 continue;
1680
1681 config_port_pf_mode(cdev->fme_dev, pdata->id);
1682 }
1683 mutex_unlock(&cdev->lock);
1684}
1685EXPORT_SYMBOL_GPL(dfl_fpga_cdev_config_ports_pf);
1686
1687/**
1688 * dfl_fpga_cdev_config_ports_vf - configure ports to VF access mode
1689 *
1690 * @cdev: parent container device.
1691 * @num_vfs: VF device number.
1692 *
1693 * This function is needed in sriov configuration routine. It could be used to
1694 * configure the released ports from PF access mode to VF.
1695 *
1696 * Return: 0 on success, negative error code otherwise.
1697 */
1698int dfl_fpga_cdev_config_ports_vf(struct dfl_fpga_cdev *cdev, int num_vfs)
1699{
1700 struct dfl_feature_platform_data *pdata;
1701 int ret = 0;
1702
1703 mutex_lock(&cdev->lock);
1704 /*
1705 * can't turn multiple ports into 1 VF device, only 1 port for 1 VF
1706 * device, so if released port number doesn't match VF device number,
1707 * then reject the request with -EINVAL error code.
1708 */
1709 if (cdev->released_port_num != num_vfs) {
1710 ret = -EINVAL;
1711 goto done;
1712 }
1713
1714 list_for_each_entry(pdata, &cdev->port_dev_list, node) {
1715 if (device_is_registered(&pdata->dev->dev))
1716 continue;
1717
1718 config_port_vf_mode(cdev->fme_dev, pdata->id);
1719 }
1720done:
1721 mutex_unlock(&cdev->lock);
1722 return ret;
1723}
1724EXPORT_SYMBOL_GPL(dfl_fpga_cdev_config_ports_vf);
1725
Xu Yilun322b5982020-06-16 12:08:44 +08001726static irqreturn_t dfl_irq_handler(int irq, void *arg)
1727{
1728 struct eventfd_ctx *trigger = arg;
1729
1730 eventfd_signal(trigger, 1);
1731 return IRQ_HANDLED;
1732}
1733
1734static int do_set_irq_trigger(struct dfl_feature *feature, unsigned int idx,
1735 int fd)
1736{
1737 struct platform_device *pdev = feature->dev;
1738 struct eventfd_ctx *trigger;
1739 int irq, ret;
1740
1741 irq = feature->irq_ctx[idx].irq;
1742
1743 if (feature->irq_ctx[idx].trigger) {
1744 free_irq(irq, feature->irq_ctx[idx].trigger);
1745 kfree(feature->irq_ctx[idx].name);
1746 eventfd_ctx_put(feature->irq_ctx[idx].trigger);
1747 feature->irq_ctx[idx].trigger = NULL;
1748 }
1749
1750 if (fd < 0)
1751 return 0;
1752
1753 feature->irq_ctx[idx].name =
Xu Yilun8a5de2d2020-08-10 10:41:10 +08001754 kasprintf(GFP_KERNEL, "fpga-irq[%u](%s-%x)", idx,
Xu Yilun322b5982020-06-16 12:08:44 +08001755 dev_name(&pdev->dev), feature->id);
1756 if (!feature->irq_ctx[idx].name)
1757 return -ENOMEM;
1758
1759 trigger = eventfd_ctx_fdget(fd);
1760 if (IS_ERR(trigger)) {
1761 ret = PTR_ERR(trigger);
1762 goto free_name;
1763 }
1764
1765 ret = request_irq(irq, dfl_irq_handler, 0,
1766 feature->irq_ctx[idx].name, trigger);
1767 if (!ret) {
1768 feature->irq_ctx[idx].trigger = trigger;
1769 return ret;
1770 }
1771
1772 eventfd_ctx_put(trigger);
1773free_name:
1774 kfree(feature->irq_ctx[idx].name);
1775
1776 return ret;
1777}
1778
1779/**
1780 * dfl_fpga_set_irq_triggers - set eventfd triggers for dfl feature interrupts
1781 *
1782 * @feature: dfl sub feature.
1783 * @start: start of irq index in this dfl sub feature.
1784 * @count: number of irqs.
1785 * @fds: eventfds to bind with irqs. unbind related irq if fds[n] is negative.
1786 * unbind "count" specified number of irqs if fds ptr is NULL.
1787 *
1788 * Bind given eventfds with irqs in this dfl sub feature. Unbind related irq if
1789 * fds[n] is negative. Unbind "count" specified number of irqs if fds ptr is
1790 * NULL.
1791 *
1792 * Return: 0 on success, negative error code otherwise.
1793 */
1794int dfl_fpga_set_irq_triggers(struct dfl_feature *feature, unsigned int start,
1795 unsigned int count, int32_t *fds)
1796{
1797 unsigned int i;
1798 int ret = 0;
1799
1800 /* overflow */
1801 if (unlikely(start + count < start))
1802 return -EINVAL;
1803
1804 /* exceeds nr_irqs */
1805 if (start + count > feature->nr_irqs)
1806 return -EINVAL;
1807
1808 for (i = 0; i < count; i++) {
1809 int fd = fds ? fds[i] : -1;
1810
1811 ret = do_set_irq_trigger(feature, start + i, fd);
1812 if (ret) {
1813 while (i--)
1814 do_set_irq_trigger(feature, start + i, -1);
1815 break;
1816 }
1817 }
1818
1819 return ret;
1820}
1821EXPORT_SYMBOL_GPL(dfl_fpga_set_irq_triggers);
1822
1823/**
1824 * dfl_feature_ioctl_get_num_irqs - dfl feature _GET_IRQ_NUM ioctl interface.
1825 * @pdev: the feature device which has the sub feature
1826 * @feature: the dfl sub feature
1827 * @arg: ioctl argument
1828 *
1829 * Return: 0 on success, negative error code otherwise.
1830 */
1831long dfl_feature_ioctl_get_num_irqs(struct platform_device *pdev,
1832 struct dfl_feature *feature,
1833 unsigned long arg)
1834{
1835 return put_user(feature->nr_irqs, (__u32 __user *)arg);
1836}
1837EXPORT_SYMBOL_GPL(dfl_feature_ioctl_get_num_irqs);
1838
1839/**
1840 * dfl_feature_ioctl_set_irq - dfl feature _SET_IRQ ioctl interface.
1841 * @pdev: the feature device which has the sub feature
1842 * @feature: the dfl sub feature
1843 * @arg: ioctl argument
1844 *
1845 * Return: 0 on success, negative error code otherwise.
1846 */
1847long dfl_feature_ioctl_set_irq(struct platform_device *pdev,
1848 struct dfl_feature *feature,
1849 unsigned long arg)
1850{
1851 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
1852 struct dfl_fpga_irq_set hdr;
1853 s32 *fds;
1854 long ret;
1855
1856 if (!feature->nr_irqs)
1857 return -ENOENT;
1858
1859 if (copy_from_user(&hdr, (void __user *)arg, sizeof(hdr)))
1860 return -EFAULT;
1861
1862 if (!hdr.count || (hdr.start + hdr.count > feature->nr_irqs) ||
1863 (hdr.start + hdr.count < hdr.start))
1864 return -EINVAL;
1865
1866 fds = memdup_user((void __user *)(arg + sizeof(hdr)),
1867 hdr.count * sizeof(s32));
1868 if (IS_ERR(fds))
1869 return PTR_ERR(fds);
1870
1871 mutex_lock(&pdata->lock);
1872 ret = dfl_fpga_set_irq_triggers(feature, hdr.start, hdr.count, fds);
1873 mutex_unlock(&pdata->lock);
1874
1875 kfree(fds);
1876 return ret;
1877}
1878EXPORT_SYMBOL_GPL(dfl_feature_ioctl_set_irq);
1879
Wu Hao543be3d2018-06-30 08:53:13 +08001880static void __exit dfl_fpga_exit(void)
1881{
Wu Haob16c5142018-06-30 08:53:14 +08001882 dfl_chardev_uinit();
Wu Hao543be3d2018-06-30 08:53:13 +08001883 dfl_ids_destroy();
Xu Yilun9ba3a0a2020-09-07 22:23:13 +08001884 bus_unregister(&dfl_bus_type);
Wu Hao543be3d2018-06-30 08:53:13 +08001885}
1886
1887module_init(dfl_fpga_init);
1888module_exit(dfl_fpga_exit);
1889
1890MODULE_DESCRIPTION("FPGA Device Feature List (DFL) Support");
1891MODULE_AUTHOR("Intel Corporation");
1892MODULE_LICENSE("GPL v2");