Exercise common setup and teardown flows for a sample platform TSM driver that implements the TSM 'connect' and 'disconnect' flows. This is both a template for platform specific implementations and a simple integration test for the PCI core infrastructure + ABI. Cc: Bjorn Helgaas <bhelgaas@xxxxxxxxxx> Cc: Lukas Wunner <lukas@xxxxxxxxx> Cc: Samuel Ortiz <sameo@xxxxxxxxxxxx> Cc: Alexey Kardashevskiy <aik@xxxxxxx> Cc: Xu Yilun <yilun.xu@xxxxxxxxxxxxxxx> Cc: Suzuki K Poulose <suzuki.poulose@xxxxxxx> Cc: "Aneesh Kumar K.V" <aneesh.kumar@xxxxxxxxxx> Signed-off-by: Dan Williams <dan.j.williams@xxxxxxxxx> --- samples/devsec/bus.c | 3 ++ samples/devsec/tsm.c | 70 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/samples/devsec/bus.c b/samples/devsec/bus.c index 675e185fcf79..efd7a650b20d 100644 --- a/samples/devsec/bus.c +++ b/samples/devsec/bus.c @@ -15,6 +15,7 @@ #define NR_DEVSEC_BUSES 1 #define NR_DEVSEC_ROOT_PORTS 1 +#define NR_PLATFORM_STREAMS 4 #define NR_PORT_STREAMS 1 #define NR_ADDR_ASSOC 1 #define NR_DEVSEC_DEVS 1 @@ -662,6 +663,7 @@ static int __init devsec_bus_probe(struct platform_device *pdev) hb->dev.parent = dev; hb->sysdata = sd; hb->ops = &devsec_ops; + pci_ide_init_nr_streams(hb, NR_PLATFORM_STREAMS); rc = pci_scan_root_bus_bridge(hb); if (rc) @@ -704,5 +706,6 @@ static void __exit devsec_bus_exit(void) } module_exit(devsec_bus_exit); +MODULE_IMPORT_NS("PCI_IDE"); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Device Security Sample Infrastructure: TDISP Device Emulation"); diff --git a/samples/devsec/tsm.c b/samples/devsec/tsm.c index a4705212a7e4..b93396ca0c92 100644 --- a/samples/devsec/tsm.c +++ b/samples/devsec/tsm.c @@ -4,6 +4,7 @@ #define dev_fmt(fmt) "devsec: " fmt #include <linux/device/faux.h> #include <linux/pci-tsm.h> +#include <linux/pci-ide.h> #include <linux/module.h> #include <linux/pci.h> #include <linux/tsm.h> @@ -92,6 +93,23 @@ static void devsec_tsm_pci_remove(struct pci_tsm *tsm) } } +/* protected by tsm_ops lock */ +static DECLARE_BITMAP(devsec_stream_ids, NR_TSM_STREAMS); +static struct pci_ide *devsec_streams[NR_TSM_STREAMS]; + +static unsigned long *alloc_devsec_stream_id(unsigned long *stream_id) +{ + unsigned long id; + + id = find_first_zero_bit(devsec_stream_ids, NR_TSM_STREAMS); + if (id == NR_TSM_STREAMS) + return NULL; + set_bit(id, devsec_stream_ids); + *stream_id = id; + return stream_id; +} +DEFINE_FREE(free_devsec_stream, unsigned long *, if (_T) clear_bit(*_T, devsec_stream_ids)) + /* * Reference consumer for a TSM driver "connect" operation callback. The * low-level TSM driver understands details about the platform the PCI @@ -116,11 +134,61 @@ static void devsec_tsm_pci_remove(struct pci_tsm *tsm) */ static int devsec_tsm_connect(struct pci_dev *pdev) { - return -ENXIO; + struct pci_dev *rp = pcie_find_root_port(pdev); + unsigned long __stream_id; + int rc; + + unsigned long *stream_id __free(free_devsec_stream) = + alloc_devsec_stream_id(&__stream_id); + if (!stream_id) + return -EBUSY; + + struct pci_ide *ide __free(pci_ide_stream_release) = + pci_ide_stream_alloc(pdev); + if (!ide) + return -ENOMEM; + + ide->stream_id = *stream_id; + rc = pci_ide_stream_register(ide); + if (rc) + return rc; + + pci_ide_stream_setup(pdev, ide); + pci_ide_stream_setup(rp, ide); + + rc = tsm_ide_stream_register(ide); + if (rc) + return rc; + + /* + * Model a TSM that handled enabling the stream at + * tsm_ide_stream_register() time + */ + rc = pci_ide_stream_enable(pdev, ide); + if (rc) + return rc; + + devsec_streams[*no_free_ptr(stream_id)] = no_free_ptr(ide); + + return 0; } static void devsec_tsm_disconnect(struct pci_dev *pdev) { + struct pci_ide *ide; + unsigned long i; + + for_each_set_bit(i, devsec_stream_ids, NR_TSM_STREAMS) + if (devsec_streams[i]->pdev == pdev) + break; + + if (i >= NR_TSM_STREAMS) + return; + + ide = devsec_streams[i]; + devsec_streams[i] = NULL; + pci_ide_stream_release(ide); + clear_bit(i, devsec_stream_ids); } static struct pci_tsm_ops devsec_pci_ops = { -- 2.50.1