[PATCH v6 13/29] fpga: dfl-pci: add enumeration for feature devices

2018-06-12 Thread Wu Hao
The Device Feature List (DFL) is implemented in MMIO, and features
are linked via the DFLs. This patch enables pcie driver to prepare
enumeration information (e.g locations of all device feature lists
in MMIO) and use common APIs provided by the Device Feature List
framework to enumerate each feature device linked.

Signed-off-by: Tim Whisonant 
Signed-off-by: Enno Luebbers 
Signed-off-by: Shiva Rao 
Signed-off-by: Christopher Rauer 
Signed-off-by: Zhang Yi 
Signed-off-by: Xiao Guangrong 
Signed-off-by: Wu Hao 
Acked-by: Alan Tull 
---
v3: split from another patch
use common functions from DFL framework for enumeration.
v4: rebase
v5: rebase due to naming changes on DFL framework APIs.
add acked-by from Alan.
v6: reabse
---
 drivers/fpga/dfl-pci.c | 200 -
 1 file changed, 198 insertions(+), 2 deletions(-)

diff --git a/drivers/fpga/dfl-pci.c b/drivers/fpga/dfl-pci.c
index 5824c5e..1e6e4ee 100644
--- a/drivers/fpga/dfl-pci.c
+++ b/drivers/fpga/dfl-pci.c
@@ -22,9 +22,52 @@
 #include 
 #include 
 
+#include "dfl.h"
+
 #define DRV_VERSION"0.8"
 #define DRV_NAME   "dfl-pci"
 
+struct cci_drvdata {
+   struct dfl_fpga_cdev *cdev; /* container device */
+   struct list_head regions;   /* list of pci bar mapping region */
+};
+
+/* pci bar mapping info */
+struct cci_region {
+   int bar;
+   void __iomem *ioaddr;   /* pointer to mapped bar region */
+   struct list_head node;
+};
+
+static void __iomem *cci_pci_ioremap_bar(struct pci_dev *pcidev, int bar)
+{
+   struct cci_drvdata *drvdata = pci_get_drvdata(pcidev);
+   struct cci_region *region;
+
+   list_for_each_entry(region, >regions, node)
+   if (region->bar == bar) {
+   dev_dbg(>dev, "BAR %d region exists\n", bar);
+   return region->ioaddr;
+   }
+
+   region = devm_kzalloc(>dev, sizeof(*region), GFP_KERNEL);
+   if (!region)
+   return NULL;
+
+   region->bar = bar;
+   region->ioaddr = pci_ioremap_bar(pcidev, bar);
+   if (!region->ioaddr) {
+   dev_err(>dev, "can't ioremap memory from BAR %d.\n",
+   bar);
+   devm_kfree(>dev, region);
+   return NULL;
+   }
+
+   list_add(>node, >regions);
+
+   return region->ioaddr;
+}
+
 /* PCI Device ID */
 #define PCIE_DEVICE_ID_PF_INT_5_X  0xBCBD
 #define PCIE_DEVICE_ID_PF_INT_6_X  0xBCC0
@@ -45,6 +88,144 @@
 };
 MODULE_DEVICE_TABLE(pci, cci_pcie_id_tbl);
 
+static int cci_init_drvdata(struct pci_dev *pcidev)
+{
+   struct cci_drvdata *drvdata;
+
+   drvdata = devm_kzalloc(>dev, sizeof(*drvdata), GFP_KERNEL);
+   if (!drvdata)
+   return -ENOMEM;
+
+   INIT_LIST_HEAD(>regions);
+
+   pci_set_drvdata(pcidev, drvdata);
+
+   return 0;
+}
+
+static void cci_pci_release_regions(struct pci_dev *pcidev)
+{
+   struct cci_drvdata *drvdata = pci_get_drvdata(pcidev);
+   struct cci_region *tmp, *region;
+
+   list_for_each_entry_safe(region, tmp, >regions, node) {
+   list_del(>node);
+   if (region->ioaddr)
+   pci_iounmap(pcidev, region->ioaddr);
+   devm_kfree(>dev, region);
+   }
+}
+
+static void cci_remove_drvdata(struct pci_dev *pcidev)
+{
+   struct cci_drvdata *drvdata = pci_get_drvdata(pcidev);
+
+   cci_pci_release_regions(pcidev);
+   pci_set_drvdata(pcidev, NULL);
+   devm_kfree(>dev, drvdata);
+}
+
+static void cci_remove_feature_devs(struct pci_dev *pcidev)
+{
+   struct cci_drvdata *drvdata = pci_get_drvdata(pcidev);
+
+   /* remove all children feature devices */
+   dfl_fpga_feature_devs_remove(drvdata->cdev);
+}
+
+/* enumerate feature devices under pci device */
+static int cci_enumerate_feature_devs(struct pci_dev *pcidev)
+{
+   struct cci_drvdata *drvdata = pci_get_drvdata(pcidev);
+   struct dfl_fpga_enum_info *info;
+   struct dfl_fpga_cdev *cdev;
+   resource_size_t start, len;
+   int port_num, bar, i, ret = 0;
+   void __iomem *base;
+   u32 offset;
+   u64 v;
+
+   /* allocate enumeration info via pci_dev */
+   info = dfl_fpga_enum_info_alloc(>dev);
+   if (!info)
+   return -ENOMEM;
+
+   /* start to find Device Feature List from Bar 0 */
+   base = cci_pci_ioremap_bar(pcidev, 0);
+   if (!base) {
+   ret = -ENOMEM;
+   goto enum_info_free_exit;
+   }
+
+   /*
+* PF device has FME and Ports/AFUs, and VF device only has 1 Port/AFU.
+* check them and add related "Device Feature List" info for the next
+* step enumeration.
+*/
+   if (dfl_feature_is_fme(base)) {
+   start = pci_resource_start(pcidev, 0);
+   len = pci_resource_len(pcidev, 0);
+
+   dfl_fpga_enum_info_add_dfl(info, start, len, base);
+
+   

[PATCH v6 13/29] fpga: dfl-pci: add enumeration for feature devices

2018-06-12 Thread Wu Hao
The Device Feature List (DFL) is implemented in MMIO, and features
are linked via the DFLs. This patch enables pcie driver to prepare
enumeration information (e.g locations of all device feature lists
in MMIO) and use common APIs provided by the Device Feature List
framework to enumerate each feature device linked.

Signed-off-by: Tim Whisonant 
Signed-off-by: Enno Luebbers 
Signed-off-by: Shiva Rao 
Signed-off-by: Christopher Rauer 
Signed-off-by: Zhang Yi 
Signed-off-by: Xiao Guangrong 
Signed-off-by: Wu Hao 
Acked-by: Alan Tull 
---
v3: split from another patch
use common functions from DFL framework for enumeration.
v4: rebase
v5: rebase due to naming changes on DFL framework APIs.
add acked-by from Alan.
v6: reabse
---
 drivers/fpga/dfl-pci.c | 200 -
 1 file changed, 198 insertions(+), 2 deletions(-)

diff --git a/drivers/fpga/dfl-pci.c b/drivers/fpga/dfl-pci.c
index 5824c5e..1e6e4ee 100644
--- a/drivers/fpga/dfl-pci.c
+++ b/drivers/fpga/dfl-pci.c
@@ -22,9 +22,52 @@
 #include 
 #include 
 
+#include "dfl.h"
+
 #define DRV_VERSION"0.8"
 #define DRV_NAME   "dfl-pci"
 
+struct cci_drvdata {
+   struct dfl_fpga_cdev *cdev; /* container device */
+   struct list_head regions;   /* list of pci bar mapping region */
+};
+
+/* pci bar mapping info */
+struct cci_region {
+   int bar;
+   void __iomem *ioaddr;   /* pointer to mapped bar region */
+   struct list_head node;
+};
+
+static void __iomem *cci_pci_ioremap_bar(struct pci_dev *pcidev, int bar)
+{
+   struct cci_drvdata *drvdata = pci_get_drvdata(pcidev);
+   struct cci_region *region;
+
+   list_for_each_entry(region, >regions, node)
+   if (region->bar == bar) {
+   dev_dbg(>dev, "BAR %d region exists\n", bar);
+   return region->ioaddr;
+   }
+
+   region = devm_kzalloc(>dev, sizeof(*region), GFP_KERNEL);
+   if (!region)
+   return NULL;
+
+   region->bar = bar;
+   region->ioaddr = pci_ioremap_bar(pcidev, bar);
+   if (!region->ioaddr) {
+   dev_err(>dev, "can't ioremap memory from BAR %d.\n",
+   bar);
+   devm_kfree(>dev, region);
+   return NULL;
+   }
+
+   list_add(>node, >regions);
+
+   return region->ioaddr;
+}
+
 /* PCI Device ID */
 #define PCIE_DEVICE_ID_PF_INT_5_X  0xBCBD
 #define PCIE_DEVICE_ID_PF_INT_6_X  0xBCC0
@@ -45,6 +88,144 @@
 };
 MODULE_DEVICE_TABLE(pci, cci_pcie_id_tbl);
 
+static int cci_init_drvdata(struct pci_dev *pcidev)
+{
+   struct cci_drvdata *drvdata;
+
+   drvdata = devm_kzalloc(>dev, sizeof(*drvdata), GFP_KERNEL);
+   if (!drvdata)
+   return -ENOMEM;
+
+   INIT_LIST_HEAD(>regions);
+
+   pci_set_drvdata(pcidev, drvdata);
+
+   return 0;
+}
+
+static void cci_pci_release_regions(struct pci_dev *pcidev)
+{
+   struct cci_drvdata *drvdata = pci_get_drvdata(pcidev);
+   struct cci_region *tmp, *region;
+
+   list_for_each_entry_safe(region, tmp, >regions, node) {
+   list_del(>node);
+   if (region->ioaddr)
+   pci_iounmap(pcidev, region->ioaddr);
+   devm_kfree(>dev, region);
+   }
+}
+
+static void cci_remove_drvdata(struct pci_dev *pcidev)
+{
+   struct cci_drvdata *drvdata = pci_get_drvdata(pcidev);
+
+   cci_pci_release_regions(pcidev);
+   pci_set_drvdata(pcidev, NULL);
+   devm_kfree(>dev, drvdata);
+}
+
+static void cci_remove_feature_devs(struct pci_dev *pcidev)
+{
+   struct cci_drvdata *drvdata = pci_get_drvdata(pcidev);
+
+   /* remove all children feature devices */
+   dfl_fpga_feature_devs_remove(drvdata->cdev);
+}
+
+/* enumerate feature devices under pci device */
+static int cci_enumerate_feature_devs(struct pci_dev *pcidev)
+{
+   struct cci_drvdata *drvdata = pci_get_drvdata(pcidev);
+   struct dfl_fpga_enum_info *info;
+   struct dfl_fpga_cdev *cdev;
+   resource_size_t start, len;
+   int port_num, bar, i, ret = 0;
+   void __iomem *base;
+   u32 offset;
+   u64 v;
+
+   /* allocate enumeration info via pci_dev */
+   info = dfl_fpga_enum_info_alloc(>dev);
+   if (!info)
+   return -ENOMEM;
+
+   /* start to find Device Feature List from Bar 0 */
+   base = cci_pci_ioremap_bar(pcidev, 0);
+   if (!base) {
+   ret = -ENOMEM;
+   goto enum_info_free_exit;
+   }
+
+   /*
+* PF device has FME and Ports/AFUs, and VF device only has 1 Port/AFU.
+* check them and add related "Device Feature List" info for the next
+* step enumeration.
+*/
+   if (dfl_feature_is_fme(base)) {
+   start = pci_resource_start(pcidev, 0);
+   len = pci_resource_len(pcidev, 0);
+
+   dfl_fpga_enum_info_add_dfl(info, start, len, base);
+
+