xref: /linux/drivers/fpga/dfl.c (revision 97733180fafbeb7cc3fd1c8be60d05980615f5d6)
1 // 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  */
13 #include <linux/dfl.h>
14 #include <linux/fpga-dfl.h>
15 #include <linux/module.h>
16 #include <linux/uaccess.h>
17 
18 #include "dfl.h"
19 
20 static 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).
28  *
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).
33  */
34 enum dfl_fpga_devt_type {
35 	DFL_FPGA_DEVT_FME,
36 	DFL_FPGA_DEVT_PORT,
37 	DFL_FPGA_DEVT_MAX,
38 };
39 
40 static struct lock_class_key dfl_pdata_keys[DFL_ID_MAX];
41 
42 static const char *dfl_pdata_key_strings[DFL_ID_MAX] = {
43 	"dfl-fme-pdata",
44 	"dfl-port-pdata",
45 };
46 
47 /**
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.
52  * @devt_type: index to dfl_chrdevs[].
53  */
54 struct dfl_dev_info {
55 	const char *name;
56 	u16 dfh_id;
57 	struct idr id;
58 	enum dfl_fpga_devt_type devt_type;
59 };
60 
61 /* it is indexed by dfl_id_type */
62 static struct dfl_dev_info dfl_devs[] = {
63 	{.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  */
74 struct dfl_chardev_info {
75 	const char *name;
76 	dev_t devt;
77 };
78 
79 /* indexed by enum dfl_fpga_devt_type */
80 static struct dfl_chardev_info dfl_chrdevs[] = {
81 	{.name = DFL_FPGA_FEATURE_DEV_FME},
82 	{.name = DFL_FPGA_FEATURE_DEV_PORT},
83 };
84 
85 static 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 
93 static 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 
101 static 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 
113 static 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 
121 static 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 
132 static enum dfl_id_type dfh_id_to_type(u16 id)
133 {
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 
143 /*
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 
153 static DEFINE_MUTEX(dfl_port_ops_mutex);
154 static 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  */
163 struct 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;
181 done:
182 	mutex_unlock(&dfl_port_ops_mutex);
183 	return ops;
184 }
185 EXPORT_SYMBOL_GPL(dfl_fpga_port_ops_get);
186 
187 /**
188  * dfl_fpga_port_ops_put - put port ops
189  * @ops: port ops.
190  */
191 void dfl_fpga_port_ops_put(struct dfl_fpga_port_ops *ops)
192 {
193 	if (ops && ops->owner)
194 		module_put(ops->owner);
195 }
196 EXPORT_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  */
202 void 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 }
208 EXPORT_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  */
214 void 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 }
220 EXPORT_SYMBOL_GPL(dfl_fpga_port_ops_del);
221 
222 /**
223  * 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  */
229 int dfl_fpga_check_port_id(struct platform_device *pdev, void *pport_id)
230 {
231 	struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
232 	struct dfl_fpga_port_ops *port_ops;
233 
234 	if (pdata->id != FEATURE_DEV_ID_UNUSED)
235 		return pdata->id == *(int *)pport_id;
236 
237 	port_ops = dfl_fpga_port_ops_get(pdev);
238 	if (!port_ops || !port_ops->get_id)
239 		return 0;
240 
241 	pdata->id = port_ops->get_id(pdev);
242 	dfl_fpga_port_ops_put(port_ops);
243 
244 	return pdata->id == *(int *)pport_id;
245 }
246 EXPORT_SYMBOL_GPL(dfl_fpga_check_port_id);
247 
248 static DEFINE_IDA(dfl_device_ida);
249 
250 static const struct dfl_device_id *
251 dfl_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 
259 static 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 
279 static 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 
287 static void 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 
296 static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
297 {
298 	struct dfl_device *ddev = to_dfl_dev(dev);
299 
300 	return add_uevent_var(env, "MODALIAS=dfl:t%04Xf%04X",
301 			      ddev->type, ddev->feature_id);
302 }
303 
304 static ssize_t
305 type_show(struct device *dev, struct device_attribute *attr, char *buf)
306 {
307 	struct dfl_device *ddev = to_dfl_dev(dev);
308 
309 	return sprintf(buf, "0x%x\n", ddev->type);
310 }
311 static DEVICE_ATTR_RO(type);
312 
313 static ssize_t
314 feature_id_show(struct device *dev, struct device_attribute *attr, char *buf)
315 {
316 	struct dfl_device *ddev = to_dfl_dev(dev);
317 
318 	return sprintf(buf, "0x%x\n", ddev->feature_id);
319 }
320 static DEVICE_ATTR_RO(feature_id);
321 
322 static struct attribute *dfl_dev_attrs[] = {
323 	&dev_attr_type.attr,
324 	&dev_attr_feature_id.attr,
325 	NULL,
326 };
327 ATTRIBUTE_GROUPS(dfl_dev);
328 
329 static struct bus_type dfl_bus_type = {
330 	.name		= "dfl",
331 	.match		= dfl_bus_match,
332 	.probe		= dfl_bus_probe,
333 	.remove		= dfl_bus_remove,
334 	.uevent		= dfl_bus_uevent,
335 	.dev_groups	= dfl_dev_groups,
336 };
337 
338 static void release_dfl_dev(struct device *dev)
339 {
340 	struct dfl_device *ddev = to_dfl_dev(dev);
341 
342 	if (ddev->mmio_res.parent)
343 		release_resource(&ddev->mmio_res);
344 
345 	ida_simple_remove(&dfl_device_ida, ddev->id);
346 	kfree(ddev->irqs);
347 	kfree(ddev);
348 }
349 
350 static struct dfl_device *
351 dfl_dev_add(struct dfl_feature_platform_data *pdata,
352 	    struct dfl_feature *feature)
353 {
354 	struct platform_device *pdev = pdata->dev;
355 	struct resource *parent_res;
356 	struct dfl_device *ddev;
357 	int id, i, ret;
358 
359 	ddev = kzalloc(sizeof(*ddev), GFP_KERNEL);
360 	if (!ddev)
361 		return ERR_PTR(-ENOMEM);
362 
363 	id = ida_simple_get(&dfl_device_ida, 0, 0, GFP_KERNEL);
364 	if (id < 0) {
365 		dev_err(&pdev->dev, "unable to get id\n");
366 		kfree(ddev);
367 		return ERR_PTR(id);
368 	}
369 
370 	/* freeing resources by put_device() after device_initialize() */
371 	device_initialize(&ddev->dev);
372 	ddev->dev.parent = &pdev->dev;
373 	ddev->dev.bus = &dfl_bus_type;
374 	ddev->dev.release = release_dfl_dev;
375 	ddev->id = id;
376 	ret = dev_set_name(&ddev->dev, "dfl_dev.%d", id);
377 	if (ret)
378 		goto put_dev;
379 
380 	ddev->type = feature_dev_id_type(pdev);
381 	ddev->feature_id = feature->id;
382 	ddev->revision = feature->revision;
383 	ddev->cdev = pdata->dfl_cdev;
384 
385 	/* add mmio resource */
386 	parent_res = &pdev->resource[feature->resource_index];
387 	ddev->mmio_res.flags = IORESOURCE_MEM;
388 	ddev->mmio_res.start = parent_res->start;
389 	ddev->mmio_res.end = parent_res->end;
390 	ddev->mmio_res.name = dev_name(&ddev->dev);
391 	ret = insert_resource(parent_res, &ddev->mmio_res);
392 	if (ret) {
393 		dev_err(&pdev->dev, "%s failed to claim resource: %pR\n",
394 			dev_name(&ddev->dev), &ddev->mmio_res);
395 		goto put_dev;
396 	}
397 
398 	/* then add irq resource */
399 	if (feature->nr_irqs) {
400 		ddev->irqs = kcalloc(feature->nr_irqs,
401 				     sizeof(*ddev->irqs), GFP_KERNEL);
402 		if (!ddev->irqs) {
403 			ret = -ENOMEM;
404 			goto put_dev;
405 		}
406 
407 		for (i = 0; i < feature->nr_irqs; i++)
408 			ddev->irqs[i] = feature->irq_ctx[i].irq;
409 
410 		ddev->num_irqs = feature->nr_irqs;
411 	}
412 
413 	ret = device_add(&ddev->dev);
414 	if (ret)
415 		goto put_dev;
416 
417 	dev_dbg(&pdev->dev, "add dfl_dev: %s\n", dev_name(&ddev->dev));
418 	return ddev;
419 
420 put_dev:
421 	/* calls release_dfl_dev() which does the clean up  */
422 	put_device(&ddev->dev);
423 	return ERR_PTR(ret);
424 }
425 
426 static void dfl_devs_remove(struct dfl_feature_platform_data *pdata)
427 {
428 	struct dfl_feature *feature;
429 
430 	dfl_fpga_dev_for_each_feature(pdata, feature) {
431 		if (feature->ddev) {
432 			device_unregister(&feature->ddev->dev);
433 			feature->ddev = NULL;
434 		}
435 	}
436 }
437 
438 static int dfl_devs_add(struct dfl_feature_platform_data *pdata)
439 {
440 	struct dfl_feature *feature;
441 	struct dfl_device *ddev;
442 	int ret;
443 
444 	dfl_fpga_dev_for_each_feature(pdata, feature) {
445 		if (feature->ioaddr)
446 			continue;
447 
448 		if (feature->ddev) {
449 			ret = -EEXIST;
450 			goto err;
451 		}
452 
453 		ddev = dfl_dev_add(pdata, feature);
454 		if (IS_ERR(ddev)) {
455 			ret = PTR_ERR(ddev);
456 			goto err;
457 		}
458 
459 		feature->ddev = ddev;
460 	}
461 
462 	return 0;
463 
464 err:
465 	dfl_devs_remove(pdata);
466 	return ret;
467 }
468 
469 int __dfl_driver_register(struct dfl_driver *dfl_drv, struct module *owner)
470 {
471 	if (!dfl_drv || !dfl_drv->probe || !dfl_drv->id_table)
472 		return -EINVAL;
473 
474 	dfl_drv->drv.owner = owner;
475 	dfl_drv->drv.bus = &dfl_bus_type;
476 
477 	return driver_register(&dfl_drv->drv);
478 }
479 EXPORT_SYMBOL(__dfl_driver_register);
480 
481 void dfl_driver_unregister(struct dfl_driver *dfl_drv)
482 {
483 	driver_unregister(&dfl_drv->drv);
484 }
485 EXPORT_SYMBOL(dfl_driver_unregister);
486 
487 #define is_header_feature(feature) ((feature)->id == FEATURE_ID_FIU_HEADER)
488 
489 /**
490  * dfl_fpga_dev_feature_uinit - uinit for sub features of dfl feature device
491  * @pdev: feature device.
492  */
493 void dfl_fpga_dev_feature_uinit(struct platform_device *pdev)
494 {
495 	struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
496 	struct dfl_feature *feature;
497 
498 	dfl_devs_remove(pdata);
499 
500 	dfl_fpga_dev_for_each_feature(pdata, feature) {
501 		if (feature->ops) {
502 			if (feature->ops->uinit)
503 				feature->ops->uinit(pdev, feature);
504 			feature->ops = NULL;
505 		}
506 	}
507 }
508 EXPORT_SYMBOL_GPL(dfl_fpga_dev_feature_uinit);
509 
510 static int dfl_feature_instance_init(struct platform_device *pdev,
511 				     struct dfl_feature_platform_data *pdata,
512 				     struct dfl_feature *feature,
513 				     struct dfl_feature_driver *drv)
514 {
515 	void __iomem *base;
516 	int ret = 0;
517 
518 	if (!is_header_feature(feature)) {
519 		base = devm_platform_ioremap_resource(pdev,
520 						      feature->resource_index);
521 		if (IS_ERR(base)) {
522 			dev_err(&pdev->dev,
523 				"ioremap failed for feature 0x%x!\n",
524 				feature->id);
525 			return PTR_ERR(base);
526 		}
527 
528 		feature->ioaddr = base;
529 	}
530 
531 	if (drv->ops->init) {
532 		ret = drv->ops->init(pdev, feature);
533 		if (ret)
534 			return ret;
535 	}
536 
537 	feature->ops = drv->ops;
538 
539 	return ret;
540 }
541 
542 static bool dfl_feature_drv_match(struct dfl_feature *feature,
543 				  struct dfl_feature_driver *driver)
544 {
545 	const struct dfl_feature_id *ids = driver->id_table;
546 
547 	if (ids) {
548 		while (ids->id) {
549 			if (ids->id == feature->id)
550 				return true;
551 			ids++;
552 		}
553 	}
554 	return false;
555 }
556 
557 /**
558  * dfl_fpga_dev_feature_init - init for sub features of dfl feature device
559  * @pdev: feature device.
560  * @feature_drvs: drvs for sub features.
561  *
562  * This function will match sub features with given feature drvs list and
563  * use matched drv to init related sub feature.
564  *
565  * Return: 0 on success, negative error code otherwise.
566  */
567 int dfl_fpga_dev_feature_init(struct platform_device *pdev,
568 			      struct dfl_feature_driver *feature_drvs)
569 {
570 	struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
571 	struct dfl_feature_driver *drv = feature_drvs;
572 	struct dfl_feature *feature;
573 	int ret;
574 
575 	while (drv->ops) {
576 		dfl_fpga_dev_for_each_feature(pdata, feature) {
577 			if (dfl_feature_drv_match(feature, drv)) {
578 				ret = dfl_feature_instance_init(pdev, pdata,
579 								feature, drv);
580 				if (ret)
581 					goto exit;
582 			}
583 		}
584 		drv++;
585 	}
586 
587 	ret = dfl_devs_add(pdata);
588 	if (ret)
589 		goto exit;
590 
591 	return 0;
592 exit:
593 	dfl_fpga_dev_feature_uinit(pdev);
594 	return ret;
595 }
596 EXPORT_SYMBOL_GPL(dfl_fpga_dev_feature_init);
597 
598 static void dfl_chardev_uinit(void)
599 {
600 	int i;
601 
602 	for (i = 0; i < DFL_FPGA_DEVT_MAX; i++)
603 		if (MAJOR(dfl_chrdevs[i].devt)) {
604 			unregister_chrdev_region(dfl_chrdevs[i].devt,
605 						 MINORMASK + 1);
606 			dfl_chrdevs[i].devt = MKDEV(0, 0);
607 		}
608 }
609 
610 static int dfl_chardev_init(void)
611 {
612 	int i, ret;
613 
614 	for (i = 0; i < DFL_FPGA_DEVT_MAX; i++) {
615 		ret = alloc_chrdev_region(&dfl_chrdevs[i].devt, 0,
616 					  MINORMASK + 1, dfl_chrdevs[i].name);
617 		if (ret)
618 			goto exit;
619 	}
620 
621 	return 0;
622 
623 exit:
624 	dfl_chardev_uinit();
625 	return ret;
626 }
627 
628 static dev_t dfl_get_devt(enum dfl_fpga_devt_type type, int id)
629 {
630 	if (type >= DFL_FPGA_DEVT_MAX)
631 		return 0;
632 
633 	return MKDEV(MAJOR(dfl_chrdevs[type].devt), id);
634 }
635 
636 /**
637  * dfl_fpga_dev_ops_register - register cdev ops for feature dev
638  *
639  * @pdev: feature dev.
640  * @fops: file operations for feature dev's cdev.
641  * @owner: owning module/driver.
642  *
643  * Return: 0 on success, negative error code otherwise.
644  */
645 int dfl_fpga_dev_ops_register(struct platform_device *pdev,
646 			      const struct file_operations *fops,
647 			      struct module *owner)
648 {
649 	struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
650 
651 	cdev_init(&pdata->cdev, fops);
652 	pdata->cdev.owner = owner;
653 
654 	/*
655 	 * set parent to the feature device so that its refcount is
656 	 * decreased after the last refcount of cdev is gone, that
657 	 * makes sure the feature device is valid during device
658 	 * file's life-cycle.
659 	 */
660 	pdata->cdev.kobj.parent = &pdev->dev.kobj;
661 
662 	return cdev_add(&pdata->cdev, pdev->dev.devt, 1);
663 }
664 EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_register);
665 
666 /**
667  * dfl_fpga_dev_ops_unregister - unregister cdev ops for feature dev
668  * @pdev: feature dev.
669  */
670 void dfl_fpga_dev_ops_unregister(struct platform_device *pdev)
671 {
672 	struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
673 
674 	cdev_del(&pdata->cdev);
675 }
676 EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_unregister);
677 
678 /**
679  * struct build_feature_devs_info - info collected during feature dev build.
680  *
681  * @dev: device to enumerate.
682  * @cdev: the container device for all feature devices.
683  * @nr_irqs: number of irqs for all feature devices.
684  * @irq_table: Linux IRQ numbers for all irqs, indexed by local irq index of
685  *	       this device.
686  * @feature_dev: current feature device.
687  * @ioaddr: header register region address of current FIU in enumeration.
688  * @start: register resource start of current FIU.
689  * @len: max register resource length of current FIU.
690  * @sub_features: a sub features linked list for feature device in enumeration.
691  * @feature_num: number of sub features for feature device in enumeration.
692  */
693 struct build_feature_devs_info {
694 	struct device *dev;
695 	struct dfl_fpga_cdev *cdev;
696 	unsigned int nr_irqs;
697 	int *irq_table;
698 
699 	struct platform_device *feature_dev;
700 	void __iomem *ioaddr;
701 	resource_size_t start;
702 	resource_size_t len;
703 	struct list_head sub_features;
704 	int feature_num;
705 };
706 
707 /**
708  * struct dfl_feature_info - sub feature info collected during feature dev build
709  *
710  * @fid: id of this sub feature.
711  * @mmio_res: mmio resource of this sub feature.
712  * @ioaddr: mapped base address of mmio resource.
713  * @node: node in sub_features linked list.
714  * @irq_base: start of irq index in this sub feature.
715  * @nr_irqs: number of irqs of this sub feature.
716  */
717 struct dfl_feature_info {
718 	u16 fid;
719 	u8 revision;
720 	struct resource mmio_res;
721 	void __iomem *ioaddr;
722 	struct list_head node;
723 	unsigned int irq_base;
724 	unsigned int nr_irqs;
725 };
726 
727 static void dfl_fpga_cdev_add_port_dev(struct dfl_fpga_cdev *cdev,
728 				       struct platform_device *port)
729 {
730 	struct dfl_feature_platform_data *pdata = dev_get_platdata(&port->dev);
731 
732 	mutex_lock(&cdev->lock);
733 	list_add(&pdata->node, &cdev->port_dev_list);
734 	get_device(&pdata->dev->dev);
735 	mutex_unlock(&cdev->lock);
736 }
737 
738 /*
739  * register current feature device, it is called when we need to switch to
740  * another feature parsing or we have parsed all features on given device
741  * feature list.
742  */
743 static int build_info_commit_dev(struct build_feature_devs_info *binfo)
744 {
745 	struct platform_device *fdev = binfo->feature_dev;
746 	struct dfl_feature_platform_data *pdata;
747 	struct dfl_feature_info *finfo, *p;
748 	enum dfl_id_type type;
749 	int ret, index = 0, res_idx = 0;
750 
751 	type = feature_dev_id_type(fdev);
752 	if (WARN_ON_ONCE(type >= DFL_ID_MAX))
753 		return -EINVAL;
754 
755 	/*
756 	 * we do not need to care for the memory which is associated with
757 	 * the platform device. After calling platform_device_unregister(),
758 	 * it will be automatically freed by device's release() callback,
759 	 * platform_device_release().
760 	 */
761 	pdata = kzalloc(struct_size(pdata, features, binfo->feature_num), GFP_KERNEL);
762 	if (!pdata)
763 		return -ENOMEM;
764 
765 	pdata->dev = fdev;
766 	pdata->num = binfo->feature_num;
767 	pdata->dfl_cdev = binfo->cdev;
768 	pdata->id = FEATURE_DEV_ID_UNUSED;
769 	mutex_init(&pdata->lock);
770 	lockdep_set_class_and_name(&pdata->lock, &dfl_pdata_keys[type],
771 				   dfl_pdata_key_strings[type]);
772 
773 	/*
774 	 * the count should be initialized to 0 to make sure
775 	 *__fpga_port_enable() following __fpga_port_disable()
776 	 * works properly for port device.
777 	 * and it should always be 0 for fme device.
778 	 */
779 	WARN_ON(pdata->disable_count);
780 
781 	fdev->dev.platform_data = pdata;
782 
783 	/* each sub feature has one MMIO resource */
784 	fdev->num_resources = binfo->feature_num;
785 	fdev->resource = kcalloc(binfo->feature_num, sizeof(*fdev->resource),
786 				 GFP_KERNEL);
787 	if (!fdev->resource)
788 		return -ENOMEM;
789 
790 	/* fill features and resource information for feature dev */
791 	list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
792 		struct dfl_feature *feature = &pdata->features[index++];
793 		struct dfl_feature_irq_ctx *ctx;
794 		unsigned int i;
795 
796 		/* save resource information for each feature */
797 		feature->dev = fdev;
798 		feature->id = finfo->fid;
799 		feature->revision = finfo->revision;
800 
801 		/*
802 		 * the FIU header feature has some fundamental functions (sriov
803 		 * set, port enable/disable) needed for the dfl bus device and
804 		 * other sub features. So its mmio resource should be mapped by
805 		 * DFL bus device. And we should not assign it to feature
806 		 * devices (dfl-fme/afu) again.
807 		 */
808 		if (is_header_feature(feature)) {
809 			feature->resource_index = -1;
810 			feature->ioaddr =
811 				devm_ioremap_resource(binfo->dev,
812 						      &finfo->mmio_res);
813 			if (IS_ERR(feature->ioaddr))
814 				return PTR_ERR(feature->ioaddr);
815 		} else {
816 			feature->resource_index = res_idx;
817 			fdev->resource[res_idx++] = finfo->mmio_res;
818 		}
819 
820 		if (finfo->nr_irqs) {
821 			ctx = devm_kcalloc(binfo->dev, finfo->nr_irqs,
822 					   sizeof(*ctx), GFP_KERNEL);
823 			if (!ctx)
824 				return -ENOMEM;
825 
826 			for (i = 0; i < finfo->nr_irqs; i++)
827 				ctx[i].irq =
828 					binfo->irq_table[finfo->irq_base + i];
829 
830 			feature->irq_ctx = ctx;
831 			feature->nr_irqs = finfo->nr_irqs;
832 		}
833 
834 		list_del(&finfo->node);
835 		kfree(finfo);
836 	}
837 
838 	ret = platform_device_add(binfo->feature_dev);
839 	if (!ret) {
840 		if (type == PORT_ID)
841 			dfl_fpga_cdev_add_port_dev(binfo->cdev,
842 						   binfo->feature_dev);
843 		else
844 			binfo->cdev->fme_dev =
845 					get_device(&binfo->feature_dev->dev);
846 		/*
847 		 * reset it to avoid build_info_free() freeing their resource.
848 		 *
849 		 * The resource of successfully registered feature devices
850 		 * will be freed by platform_device_unregister(). See the
851 		 * comments in build_info_create_dev().
852 		 */
853 		binfo->feature_dev = NULL;
854 	}
855 
856 	return ret;
857 }
858 
859 static int
860 build_info_create_dev(struct build_feature_devs_info *binfo,
861 		      enum dfl_id_type type)
862 {
863 	struct platform_device *fdev;
864 
865 	if (type >= DFL_ID_MAX)
866 		return -EINVAL;
867 
868 	/*
869 	 * we use -ENODEV as the initialization indicator which indicates
870 	 * whether the id need to be reclaimed
871 	 */
872 	fdev = platform_device_alloc(dfl_devs[type].name, -ENODEV);
873 	if (!fdev)
874 		return -ENOMEM;
875 
876 	binfo->feature_dev = fdev;
877 	binfo->feature_num = 0;
878 
879 	INIT_LIST_HEAD(&binfo->sub_features);
880 
881 	fdev->id = dfl_id_alloc(type, &fdev->dev);
882 	if (fdev->id < 0)
883 		return fdev->id;
884 
885 	fdev->dev.parent = &binfo->cdev->region->dev;
886 	fdev->dev.devt = dfl_get_devt(dfl_devs[type].devt_type, fdev->id);
887 
888 	return 0;
889 }
890 
891 static void build_info_free(struct build_feature_devs_info *binfo)
892 {
893 	struct dfl_feature_info *finfo, *p;
894 
895 	/*
896 	 * it is a valid id, free it. See comments in
897 	 * build_info_create_dev()
898 	 */
899 	if (binfo->feature_dev && binfo->feature_dev->id >= 0) {
900 		dfl_id_free(feature_dev_id_type(binfo->feature_dev),
901 			    binfo->feature_dev->id);
902 
903 		list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
904 			list_del(&finfo->node);
905 			kfree(finfo);
906 		}
907 	}
908 
909 	platform_device_put(binfo->feature_dev);
910 
911 	devm_kfree(binfo->dev, binfo);
912 }
913 
914 static inline u32 feature_size(u64 value)
915 {
916 	u32 ofst = FIELD_GET(DFH_NEXT_HDR_OFST, value);
917 	/* workaround for private features with invalid size, use 4K instead */
918 	return ofst ? ofst : 4096;
919 }
920 
921 static u16 feature_id(u64 value)
922 {
923 	u16 id = FIELD_GET(DFH_ID, value);
924 	u8 type = FIELD_GET(DFH_TYPE, value);
925 
926 	if (type == DFH_TYPE_FIU)
927 		return FEATURE_ID_FIU_HEADER;
928 	else if (type == DFH_TYPE_PRIVATE)
929 		return id;
930 	else if (type == DFH_TYPE_AFU)
931 		return FEATURE_ID_AFU;
932 
933 	WARN_ON(1);
934 	return 0;
935 }
936 
937 static int parse_feature_irqs(struct build_feature_devs_info *binfo,
938 			      resource_size_t ofst, u16 fid,
939 			      unsigned int *irq_base, unsigned int *nr_irqs)
940 {
941 	void __iomem *base = binfo->ioaddr + ofst;
942 	unsigned int i, ibase, inr = 0;
943 	int virq;
944 	u64 v;
945 
946 	/*
947 	 * Ideally DFL framework should only read info from DFL header, but
948 	 * current version DFL only provides mmio resources information for
949 	 * each feature in DFL Header, no field for interrupt resources.
950 	 * Interrupt resource information is provided by specific mmio
951 	 * registers of each private feature which supports interrupt. So in
952 	 * order to parse and assign irq resources, DFL framework has to look
953 	 * into specific capability registers of these private features.
954 	 *
955 	 * Once future DFL version supports generic interrupt resource
956 	 * information in common DFL headers, the generic interrupt parsing
957 	 * code will be added. But in order to be compatible to old version
958 	 * DFL, the driver may still fall back to these quirks.
959 	 */
960 	switch (fid) {
961 	case PORT_FEATURE_ID_UINT:
962 		v = readq(base + PORT_UINT_CAP);
963 		ibase = FIELD_GET(PORT_UINT_CAP_FST_VECT, v);
964 		inr = FIELD_GET(PORT_UINT_CAP_INT_NUM, v);
965 		break;
966 	case PORT_FEATURE_ID_ERROR:
967 		v = readq(base + PORT_ERROR_CAP);
968 		ibase = FIELD_GET(PORT_ERROR_CAP_INT_VECT, v);
969 		inr = FIELD_GET(PORT_ERROR_CAP_SUPP_INT, v);
970 		break;
971 	case FME_FEATURE_ID_GLOBAL_ERR:
972 		v = readq(base + FME_ERROR_CAP);
973 		ibase = FIELD_GET(FME_ERROR_CAP_INT_VECT, v);
974 		inr = FIELD_GET(FME_ERROR_CAP_SUPP_INT, v);
975 		break;
976 	}
977 
978 	if (!inr) {
979 		*irq_base = 0;
980 		*nr_irqs = 0;
981 		return 0;
982 	}
983 
984 	dev_dbg(binfo->dev, "feature: 0x%x, irq_base: %u, nr_irqs: %u\n",
985 		fid, ibase, inr);
986 
987 	if (ibase + inr > binfo->nr_irqs) {
988 		dev_err(binfo->dev,
989 			"Invalid interrupt number in feature 0x%x\n", fid);
990 		return -EINVAL;
991 	}
992 
993 	for (i = 0; i < inr; i++) {
994 		virq = binfo->irq_table[ibase + i];
995 		if (virq < 0 || virq > NR_IRQS) {
996 			dev_err(binfo->dev,
997 				"Invalid irq table entry for feature 0x%x\n",
998 				fid);
999 			return -EINVAL;
1000 		}
1001 	}
1002 
1003 	*irq_base = ibase;
1004 	*nr_irqs = inr;
1005 
1006 	return 0;
1007 }
1008 
1009 /*
1010  * when create sub feature instances, for private features, it doesn't need
1011  * to provide resource size and feature id as they could be read from DFH
1012  * register. For afu sub feature, its register region only contains user
1013  * defined registers, so never trust any information from it, just use the
1014  * resource size information provided by its parent FIU.
1015  */
1016 static int
1017 create_feature_instance(struct build_feature_devs_info *binfo,
1018 			resource_size_t ofst, resource_size_t size, u16 fid)
1019 {
1020 	unsigned int irq_base, nr_irqs;
1021 	struct dfl_feature_info *finfo;
1022 	u8 revision = 0;
1023 	int ret;
1024 	u64 v;
1025 
1026 	if (fid != FEATURE_ID_AFU) {
1027 		v = readq(binfo->ioaddr + ofst);
1028 		revision = FIELD_GET(DFH_REVISION, v);
1029 
1030 		/* read feature size and id if inputs are invalid */
1031 		size = size ? size : feature_size(v);
1032 		fid = fid ? fid : feature_id(v);
1033 	}
1034 
1035 	if (binfo->len - ofst < size)
1036 		return -EINVAL;
1037 
1038 	ret = parse_feature_irqs(binfo, ofst, fid, &irq_base, &nr_irqs);
1039 	if (ret)
1040 		return ret;
1041 
1042 	finfo = kzalloc(sizeof(*finfo), GFP_KERNEL);
1043 	if (!finfo)
1044 		return -ENOMEM;
1045 
1046 	finfo->fid = fid;
1047 	finfo->revision = revision;
1048 	finfo->mmio_res.start = binfo->start + ofst;
1049 	finfo->mmio_res.end = finfo->mmio_res.start + size - 1;
1050 	finfo->mmio_res.flags = IORESOURCE_MEM;
1051 	finfo->irq_base = irq_base;
1052 	finfo->nr_irqs = nr_irqs;
1053 
1054 	list_add_tail(&finfo->node, &binfo->sub_features);
1055 	binfo->feature_num++;
1056 
1057 	return 0;
1058 }
1059 
1060 static int parse_feature_port_afu(struct build_feature_devs_info *binfo,
1061 				  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 
1068 	return create_feature_instance(binfo, ofst, size, FEATURE_ID_AFU);
1069 }
1070 
1071 #define is_feature_dev_detected(binfo) (!!(binfo)->feature_dev)
1072 
1073 static int parse_feature_afu(struct build_feature_devs_info *binfo,
1074 			     resource_size_t ofst)
1075 {
1076 	if (!is_feature_dev_detected(binfo)) {
1077 		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:
1083 		return parse_feature_port_afu(binfo, ofst);
1084 	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 
1092 static 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 
1118 static 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 
1124 static int parse_feature_fiu(struct build_feature_devs_info *binfo,
1125 			     resource_size_t ofst)
1126 {
1127 	int ret = 0;
1128 	u32 offset;
1129 	u16 id;
1130 	u64 v;
1131 
1132 	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);
1146 	id = FIELD_GET(DFH_ID, v);
1147 
1148 	/* create platform device for dfl feature dev */
1149 	ret = build_info_create_dev(binfo, dfh_id_to_type(id));
1150 	if (ret)
1151 		return ret;
1152 
1153 	ret = create_feature_instance(binfo, 0, 0, 0);
1154 	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 	 */
1160 	v = readq(binfo->ioaddr + NEXT_AFU);
1161 
1162 	offset = FIELD_GET(NEXT_AFU_NEXT_DFH_OFST, v);
1163 	if (offset)
1164 		return parse_feature_afu(binfo, offset);
1165 
1166 	dev_dbg(binfo->dev, "No AFUs detected on FIU %d\n", id);
1167 
1168 	return ret;
1169 }
1170 
1171 static int parse_feature_private(struct build_feature_devs_info *binfo,
1172 				 resource_size_t ofst)
1173 {
1174 	if (!is_feature_dev_detected(binfo)) {
1175 		dev_err(binfo->dev, "the private feature 0x%x does not belong to any AFU.\n",
1176 			feature_id(readq(binfo->ioaddr + ofst)));
1177 		return -EINVAL;
1178 	}
1179 
1180 	return create_feature_instance(binfo, ofst, 0, 0);
1181 }
1182 
1183 /**
1184  * parse_feature - parse a feature on given device feature list
1185  *
1186  * @binfo: build feature devices information.
1187  * @ofst: offset to current FIU header
1188  */
1189 static int parse_feature(struct build_feature_devs_info *binfo,
1190 			 resource_size_t ofst)
1191 {
1192 	u64 v;
1193 	u32 type;
1194 
1195 	v = readq(binfo->ioaddr + ofst + DFH);
1196 	type = FIELD_GET(DFH_TYPE, v);
1197 
1198 	switch (type) {
1199 	case DFH_TYPE_AFU:
1200 		return parse_feature_afu(binfo, ofst);
1201 	case DFH_TYPE_PRIVATE:
1202 		return parse_feature_private(binfo, ofst);
1203 	case DFH_TYPE_FIU:
1204 		return parse_feature_fiu(binfo, ofst);
1205 	default:
1206 		dev_info(binfo->dev,
1207 			 "Feature Type %x is not supported.\n", type);
1208 	}
1209 
1210 	return 0;
1211 }
1212 
1213 static int parse_feature_list(struct build_feature_devs_info *binfo,
1214 			      resource_size_t start, resource_size_t len)
1215 {
1216 	resource_size_t end = start + len;
1217 	int ret = 0;
1218 	u32 ofst = 0;
1219 	u64 v;
1220 
1221 	ret = build_info_prepare(binfo, start, len);
1222 	if (ret)
1223 		return ret;
1224 
1225 	/* 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 
1232 		ret = parse_feature(binfo, start - binfo->start);
1233 		if (ret)
1234 			return ret;
1235 
1236 		v = readq(binfo->ioaddr + start - binfo->start + DFH);
1237 		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 */
1245 	build_info_complete(binfo);
1246 
1247 	if (is_feature_dev_detected(binfo))
1248 		ret = build_info_commit_dev(binfo);
1249 
1250 	return ret;
1251 }
1252 
1253 struct 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 }
1270 EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_alloc);
1271 
1272 void 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 
1288 	/* remove irq table */
1289 	if (info->irq_table)
1290 		devm_kfree(dev, info->irq_table);
1291 
1292 	devm_kfree(dev, info);
1293 	put_device(dev);
1294 }
1295 EXPORT_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.
1303  *
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  */
1310 int dfl_fpga_enum_info_add_dfl(struct dfl_fpga_enum_info *info,
1311 			       resource_size_t start, resource_size_t len)
1312 {
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;
1321 
1322 	list_add_tail(&dfl->node, &info->dfls);
1323 
1324 	return 0;
1325 }
1326 EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_add_dfl);
1327 
1328 /**
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  */
1347 int 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 }
1365 EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_add_irq);
1366 
1367 static 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 
1380 static 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  */
1395 struct dfl_fpga_cdev *
1396 dfl_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 
1410 	cdev->parent = info->dev;
1411 	mutex_init(&cdev->lock);
1412 	INIT_LIST_HEAD(&cdev->port_dev_list);
1413 
1414 	cdev->region = fpga_region_register(info->dev, NULL, NULL);
1415 	if (IS_ERR(cdev->region)) {
1416 		ret = PTR_ERR(cdev->region);
1417 		goto free_cdev_exit;
1418 	}
1419 
1420 	/* create and init build info for enumeration */
1421 	binfo = devm_kzalloc(info->dev, sizeof(*binfo), GFP_KERNEL);
1422 	if (!binfo) {
1423 		ret = -ENOMEM;
1424 		goto unregister_region_exit;
1425 	}
1426 
1427 	binfo->dev = info->dev;
1428 	binfo->cdev = cdev;
1429 
1430 	binfo->nr_irqs = info->nr_irqs;
1431 	if (info->nr_irqs)
1432 		binfo->irq_table = info->irq_table;
1433 
1434 	/*
1435 	 * start enumeration for all feature devices based on Device Feature
1436 	 * Lists.
1437 	 */
1438 	list_for_each_entry(dfl, &info->dfls, node) {
1439 		ret = parse_feature_list(binfo, dfl->start, dfl->len);
1440 		if (ret) {
1441 			remove_feature_devs(cdev);
1442 			build_info_free(binfo);
1443 			goto unregister_region_exit;
1444 		}
1445 	}
1446 
1447 	build_info_free(binfo);
1448 
1449 	return cdev;
1450 
1451 unregister_region_exit:
1452 	fpga_region_unregister(cdev->region);
1453 free_cdev_exit:
1454 	devm_kfree(info->dev, cdev);
1455 	return ERR_PTR(ret);
1456 }
1457 EXPORT_SYMBOL_GPL(dfl_fpga_feature_devs_enumerate);
1458 
1459 /**
1460  * dfl_fpga_feature_devs_remove - remove all feature devices
1461  * @cdev: fpga container device.
1462  *
1463  * Remove the container device and all feature devices under given container
1464  * devices.
1465  */
1466 void dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev *cdev)
1467 {
1468 	struct dfl_feature_platform_data *pdata, *ptmp;
1469 
1470 	mutex_lock(&cdev->lock);
1471 	if (cdev->fme_dev)
1472 		put_device(cdev->fme_dev);
1473 
1474 	list_for_each_entry_safe(pdata, ptmp, &cdev->port_dev_list, node) {
1475 		struct platform_device *port_dev = pdata->dev;
1476 
1477 		/* remove released ports */
1478 		if (!device_is_registered(&port_dev->dev)) {
1479 			dfl_id_free(feature_dev_id_type(port_dev),
1480 				    port_dev->id);
1481 			platform_device_put(port_dev);
1482 		}
1483 
1484 		list_del(&pdata->node);
1485 		put_device(&port_dev->dev);
1486 	}
1487 	mutex_unlock(&cdev->lock);
1488 
1489 	remove_feature_devs(cdev);
1490 
1491 	fpga_region_unregister(cdev->region);
1492 	devm_kfree(cdev->parent, cdev);
1493 }
1494 EXPORT_SYMBOL_GPL(dfl_fpga_feature_devs_remove);
1495 
1496 /**
1497  * __dfl_fpga_cdev_find_port - find a port under given container device
1498  *
1499  * @cdev: container device
1500  * @data: data passed to match function
1501  * @match: match function used to find specific port from the port device list
1502  *
1503  * Find a port device under container device. This function needs to be
1504  * invoked with lock held.
1505  *
1506  * Return: pointer to port's platform device if successful, NULL otherwise.
1507  *
1508  * NOTE: you will need to drop the device reference with put_device() after use.
1509  */
1510 struct platform_device *
1511 __dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
1512 			  int (*match)(struct platform_device *, void *))
1513 {
1514 	struct dfl_feature_platform_data *pdata;
1515 	struct platform_device *port_dev;
1516 
1517 	list_for_each_entry(pdata, &cdev->port_dev_list, node) {
1518 		port_dev = pdata->dev;
1519 
1520 		if (match(port_dev, data) && get_device(&port_dev->dev))
1521 			return port_dev;
1522 	}
1523 
1524 	return NULL;
1525 }
1526 EXPORT_SYMBOL_GPL(__dfl_fpga_cdev_find_port);
1527 
1528 static int __init dfl_fpga_init(void)
1529 {
1530 	int ret;
1531 
1532 	ret = bus_register(&dfl_bus_type);
1533 	if (ret)
1534 		return ret;
1535 
1536 	dfl_ids_init();
1537 
1538 	ret = dfl_chardev_init();
1539 	if (ret) {
1540 		dfl_ids_destroy();
1541 		bus_unregister(&dfl_bus_type);
1542 	}
1543 
1544 	return ret;
1545 }
1546 
1547 /**
1548  * dfl_fpga_cdev_release_port - release a port platform device
1549  *
1550  * @cdev: parent container device.
1551  * @port_id: id of the port platform device.
1552  *
1553  * This function allows user to release a port platform device. This is a
1554  * mandatory step before turn a port from PF into VF for SRIOV support.
1555  *
1556  * Return: 0 on success, negative error code otherwise.
1557  */
1558 int dfl_fpga_cdev_release_port(struct dfl_fpga_cdev *cdev, int port_id)
1559 {
1560 	struct dfl_feature_platform_data *pdata;
1561 	struct platform_device *port_pdev;
1562 	int ret = -ENODEV;
1563 
1564 	mutex_lock(&cdev->lock);
1565 	port_pdev = __dfl_fpga_cdev_find_port(cdev, &port_id,
1566 					      dfl_fpga_check_port_id);
1567 	if (!port_pdev)
1568 		goto unlock_exit;
1569 
1570 	if (!device_is_registered(&port_pdev->dev)) {
1571 		ret = -EBUSY;
1572 		goto put_dev_exit;
1573 	}
1574 
1575 	pdata = dev_get_platdata(&port_pdev->dev);
1576 
1577 	mutex_lock(&pdata->lock);
1578 	ret = dfl_feature_dev_use_begin(pdata, true);
1579 	mutex_unlock(&pdata->lock);
1580 	if (ret)
1581 		goto put_dev_exit;
1582 
1583 	platform_device_del(port_pdev);
1584 	cdev->released_port_num++;
1585 put_dev_exit:
1586 	put_device(&port_pdev->dev);
1587 unlock_exit:
1588 	mutex_unlock(&cdev->lock);
1589 	return ret;
1590 }
1591 EXPORT_SYMBOL_GPL(dfl_fpga_cdev_release_port);
1592 
1593 /**
1594  * dfl_fpga_cdev_assign_port - assign a port platform device back
1595  *
1596  * @cdev: parent container device.
1597  * @port_id: id of the port platform device.
1598  *
1599  * This function allows user to assign a port platform device back. This is
1600  * a mandatory step after disable SRIOV support.
1601  *
1602  * Return: 0 on success, negative error code otherwise.
1603  */
1604 int dfl_fpga_cdev_assign_port(struct dfl_fpga_cdev *cdev, int port_id)
1605 {
1606 	struct dfl_feature_platform_data *pdata;
1607 	struct platform_device *port_pdev;
1608 	int ret = -ENODEV;
1609 
1610 	mutex_lock(&cdev->lock);
1611 	port_pdev = __dfl_fpga_cdev_find_port(cdev, &port_id,
1612 					      dfl_fpga_check_port_id);
1613 	if (!port_pdev)
1614 		goto unlock_exit;
1615 
1616 	if (device_is_registered(&port_pdev->dev)) {
1617 		ret = -EBUSY;
1618 		goto put_dev_exit;
1619 	}
1620 
1621 	ret = platform_device_add(port_pdev);
1622 	if (ret)
1623 		goto put_dev_exit;
1624 
1625 	pdata = dev_get_platdata(&port_pdev->dev);
1626 
1627 	mutex_lock(&pdata->lock);
1628 	dfl_feature_dev_use_end(pdata);
1629 	mutex_unlock(&pdata->lock);
1630 
1631 	cdev->released_port_num--;
1632 put_dev_exit:
1633 	put_device(&port_pdev->dev);
1634 unlock_exit:
1635 	mutex_unlock(&cdev->lock);
1636 	return ret;
1637 }
1638 EXPORT_SYMBOL_GPL(dfl_fpga_cdev_assign_port);
1639 
1640 static void config_port_access_mode(struct device *fme_dev, int port_id,
1641 				    bool is_vf)
1642 {
1643 	void __iomem *base;
1644 	u64 v;
1645 
1646 	base = dfl_get_feature_ioaddr_by_id(fme_dev, FME_FEATURE_ID_HEADER);
1647 
1648 	v = readq(base + FME_HDR_PORT_OFST(port_id));
1649 
1650 	v &= ~FME_PORT_OFST_ACC_CTRL;
1651 	v |= FIELD_PREP(FME_PORT_OFST_ACC_CTRL,
1652 			is_vf ? FME_PORT_OFST_ACC_VF : FME_PORT_OFST_ACC_PF);
1653 
1654 	writeq(v, base + FME_HDR_PORT_OFST(port_id));
1655 }
1656 
1657 #define config_port_vf_mode(dev, id) config_port_access_mode(dev, id, true)
1658 #define config_port_pf_mode(dev, id) config_port_access_mode(dev, id, false)
1659 
1660 /**
1661  * dfl_fpga_cdev_config_ports_pf - configure ports to PF access mode
1662  *
1663  * @cdev: parent container device.
1664  *
1665  * This function is needed in sriov configuration routine. It could be used to
1666  * configure the all released ports from VF access mode to PF.
1667  */
1668 void dfl_fpga_cdev_config_ports_pf(struct dfl_fpga_cdev *cdev)
1669 {
1670 	struct dfl_feature_platform_data *pdata;
1671 
1672 	mutex_lock(&cdev->lock);
1673 	list_for_each_entry(pdata, &cdev->port_dev_list, node) {
1674 		if (device_is_registered(&pdata->dev->dev))
1675 			continue;
1676 
1677 		config_port_pf_mode(cdev->fme_dev, pdata->id);
1678 	}
1679 	mutex_unlock(&cdev->lock);
1680 }
1681 EXPORT_SYMBOL_GPL(dfl_fpga_cdev_config_ports_pf);
1682 
1683 /**
1684  * dfl_fpga_cdev_config_ports_vf - configure ports to VF access mode
1685  *
1686  * @cdev: parent container device.
1687  * @num_vfs: VF device number.
1688  *
1689  * This function is needed in sriov configuration routine. It could be used to
1690  * configure the released ports from PF access mode to VF.
1691  *
1692  * Return: 0 on success, negative error code otherwise.
1693  */
1694 int dfl_fpga_cdev_config_ports_vf(struct dfl_fpga_cdev *cdev, int num_vfs)
1695 {
1696 	struct dfl_feature_platform_data *pdata;
1697 	int ret = 0;
1698 
1699 	mutex_lock(&cdev->lock);
1700 	/*
1701 	 * can't turn multiple ports into 1 VF device, only 1 port for 1 VF
1702 	 * device, so if released port number doesn't match VF device number,
1703 	 * then reject the request with -EINVAL error code.
1704 	 */
1705 	if (cdev->released_port_num != num_vfs) {
1706 		ret = -EINVAL;
1707 		goto done;
1708 	}
1709 
1710 	list_for_each_entry(pdata, &cdev->port_dev_list, node) {
1711 		if (device_is_registered(&pdata->dev->dev))
1712 			continue;
1713 
1714 		config_port_vf_mode(cdev->fme_dev, pdata->id);
1715 	}
1716 done:
1717 	mutex_unlock(&cdev->lock);
1718 	return ret;
1719 }
1720 EXPORT_SYMBOL_GPL(dfl_fpga_cdev_config_ports_vf);
1721 
1722 static irqreturn_t dfl_irq_handler(int irq, void *arg)
1723 {
1724 	struct eventfd_ctx *trigger = arg;
1725 
1726 	eventfd_signal(trigger, 1);
1727 	return IRQ_HANDLED;
1728 }
1729 
1730 static int do_set_irq_trigger(struct dfl_feature *feature, unsigned int idx,
1731 			      int fd)
1732 {
1733 	struct platform_device *pdev = feature->dev;
1734 	struct eventfd_ctx *trigger;
1735 	int irq, ret;
1736 
1737 	irq = feature->irq_ctx[idx].irq;
1738 
1739 	if (feature->irq_ctx[idx].trigger) {
1740 		free_irq(irq, feature->irq_ctx[idx].trigger);
1741 		kfree(feature->irq_ctx[idx].name);
1742 		eventfd_ctx_put(feature->irq_ctx[idx].trigger);
1743 		feature->irq_ctx[idx].trigger = NULL;
1744 	}
1745 
1746 	if (fd < 0)
1747 		return 0;
1748 
1749 	feature->irq_ctx[idx].name =
1750 		kasprintf(GFP_KERNEL, "fpga-irq[%u](%s-%x)", idx,
1751 			  dev_name(&pdev->dev), feature->id);
1752 	if (!feature->irq_ctx[idx].name)
1753 		return -ENOMEM;
1754 
1755 	trigger = eventfd_ctx_fdget(fd);
1756 	if (IS_ERR(trigger)) {
1757 		ret = PTR_ERR(trigger);
1758 		goto free_name;
1759 	}
1760 
1761 	ret = request_irq(irq, dfl_irq_handler, 0,
1762 			  feature->irq_ctx[idx].name, trigger);
1763 	if (!ret) {
1764 		feature->irq_ctx[idx].trigger = trigger;
1765 		return ret;
1766 	}
1767 
1768 	eventfd_ctx_put(trigger);
1769 free_name:
1770 	kfree(feature->irq_ctx[idx].name);
1771 
1772 	return ret;
1773 }
1774 
1775 /**
1776  * dfl_fpga_set_irq_triggers - set eventfd triggers for dfl feature interrupts
1777  *
1778  * @feature: dfl sub feature.
1779  * @start: start of irq index in this dfl sub feature.
1780  * @count: number of irqs.
1781  * @fds: eventfds to bind with irqs. unbind related irq if fds[n] is negative.
1782  *	 unbind "count" specified number of irqs if fds ptr is NULL.
1783  *
1784  * Bind given eventfds with irqs in this dfl sub feature. Unbind related irq if
1785  * fds[n] is negative. Unbind "count" specified number of irqs if fds ptr is
1786  * NULL.
1787  *
1788  * Return: 0 on success, negative error code otherwise.
1789  */
1790 int dfl_fpga_set_irq_triggers(struct dfl_feature *feature, unsigned int start,
1791 			      unsigned int count, int32_t *fds)
1792 {
1793 	unsigned int i;
1794 	int ret = 0;
1795 
1796 	/* overflow */
1797 	if (unlikely(start + count < start))
1798 		return -EINVAL;
1799 
1800 	/* exceeds nr_irqs */
1801 	if (start + count > feature->nr_irqs)
1802 		return -EINVAL;
1803 
1804 	for (i = 0; i < count; i++) {
1805 		int fd = fds ? fds[i] : -1;
1806 
1807 		ret = do_set_irq_trigger(feature, start + i, fd);
1808 		if (ret) {
1809 			while (i--)
1810 				do_set_irq_trigger(feature, start + i, -1);
1811 			break;
1812 		}
1813 	}
1814 
1815 	return ret;
1816 }
1817 EXPORT_SYMBOL_GPL(dfl_fpga_set_irq_triggers);
1818 
1819 /**
1820  * dfl_feature_ioctl_get_num_irqs - dfl feature _GET_IRQ_NUM ioctl interface.
1821  * @pdev: the feature device which has the sub feature
1822  * @feature: the dfl sub feature
1823  * @arg: ioctl argument
1824  *
1825  * Return: 0 on success, negative error code otherwise.
1826  */
1827 long dfl_feature_ioctl_get_num_irqs(struct platform_device *pdev,
1828 				    struct dfl_feature *feature,
1829 				    unsigned long arg)
1830 {
1831 	return put_user(feature->nr_irqs, (__u32 __user *)arg);
1832 }
1833 EXPORT_SYMBOL_GPL(dfl_feature_ioctl_get_num_irqs);
1834 
1835 /**
1836  * dfl_feature_ioctl_set_irq - dfl feature _SET_IRQ ioctl interface.
1837  * @pdev: the feature device which has the sub feature
1838  * @feature: the dfl sub feature
1839  * @arg: ioctl argument
1840  *
1841  * Return: 0 on success, negative error code otherwise.
1842  */
1843 long dfl_feature_ioctl_set_irq(struct platform_device *pdev,
1844 			       struct dfl_feature *feature,
1845 			       unsigned long arg)
1846 {
1847 	struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
1848 	struct dfl_fpga_irq_set hdr;
1849 	s32 *fds;
1850 	long ret;
1851 
1852 	if (!feature->nr_irqs)
1853 		return -ENOENT;
1854 
1855 	if (copy_from_user(&hdr, (void __user *)arg, sizeof(hdr)))
1856 		return -EFAULT;
1857 
1858 	if (!hdr.count || (hdr.start + hdr.count > feature->nr_irqs) ||
1859 	    (hdr.start + hdr.count < hdr.start))
1860 		return -EINVAL;
1861 
1862 	fds = memdup_user((void __user *)(arg + sizeof(hdr)),
1863 			  hdr.count * sizeof(s32));
1864 	if (IS_ERR(fds))
1865 		return PTR_ERR(fds);
1866 
1867 	mutex_lock(&pdata->lock);
1868 	ret = dfl_fpga_set_irq_triggers(feature, hdr.start, hdr.count, fds);
1869 	mutex_unlock(&pdata->lock);
1870 
1871 	kfree(fds);
1872 	return ret;
1873 }
1874 EXPORT_SYMBOL_GPL(dfl_feature_ioctl_set_irq);
1875 
1876 static void __exit dfl_fpga_exit(void)
1877 {
1878 	dfl_chardev_uinit();
1879 	dfl_ids_destroy();
1880 	bus_unregister(&dfl_bus_type);
1881 }
1882 
1883 module_init(dfl_fpga_init);
1884 module_exit(dfl_fpga_exit);
1885 
1886 MODULE_DESCRIPTION("FPGA Device Feature List (DFL) Support");
1887 MODULE_AUTHOR("Intel Corporation");
1888 MODULE_LICENSE("GPL v2");
1889