[v3,4/8] raw/ioat: create device on probe and destroy on release

Message ID 20190627104055.8244-5-bruce.richardson@intel.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series raw/ioat: driver for Intel QuickData Technology |

Checks

Context Check Description
ci/checkpatch warning coding style issues
ci/Intel-compilation fail Compilation issues

Commit Message

Bruce Richardson June 27, 2019, 10:40 a.m. UTC
  Add the create/destroy driver functions so that we can actually allocate
a rawdev and destroy it when done. No rawdev API functions are actually
implemented at this point.

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
 doc/guides/rawdevs/ioat_rawdev.rst | 11 ++++
 drivers/raw/ioat/ioat_rawdev.c     | 93 +++++++++++++++++++++++++++++-
 drivers/raw/ioat/rte_ioat_rawdev.h | 20 +++++++
 3 files changed, 121 insertions(+), 3 deletions(-)
  

Comments

Burakov, Anatoly June 27, 2019, 12:09 p.m. UTC | #1
On 27-Jun-19 11:40 AM, Bruce Richardson wrote:
> Add the create/destroy driver functions so that we can actually allocate
> a rawdev and destroy it when done. No rawdev API functions are actually
> implemented at this point.
> 
> Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
> ---

<snip>

>   ioat_rawdev_create(const char *name, struct rte_pci_device *dev)
>   {
> -	RTE_SET_USED(name);
> -	RTE_SET_USED(dev);
> +	static const struct rte_rawdev_ops ioat_rawdev_ops = {
> +	};
> +
> +	struct rte_rawdev *rawdev = NULL;
> +	struct rte_ioat_rawdev *ioat = NULL;
> +	int ret = 0;
> +	int retry = 0;
> +
> +	if (!name) {
> +		IOAT_PMD_ERR("Invalid name of the device!");
> +		ret = -EINVAL;
> +		goto cleanup;
> +	}

Is checking `dev` not necessary here?

> +
> +	/* Allocate device structure */
> +	rawdev = rte_rawdev_pmd_allocate(name, sizeof(struct rte_ioat_rawdev),
> +					 dev->device.numa_node);
> +	if (rawdev == NULL) {
> +		IOAT_PMD_ERR("Unable to allocate raw device");
> +		ret = -EINVAL;
> +		goto cleanup;

EINVAL is supposed to be used to indicate invalid arguments. Inability 
to allocate is not an "invalid arguments" condition. Does 
rte_rawdev_pmd_allocate() set its own errno value? If so, perhaps it 
would be worth passing it on? If not, perhaps -ENOMEM would be a better 
return value?
  
Burakov, Anatoly June 27, 2019, 12:28 p.m. UTC | #2
On 27-Jun-19 11:40 AM, Bruce Richardson wrote:
> Add the create/destroy driver functions so that we can actually allocate
> a rawdev and destroy it when done. No rawdev API functions are actually
> implemented at this point.
> 
> Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
> ---

<snip>

> +	rawdev->driver_name = dev->device.driver->name;
> +
> +	ioat = rawdev->dev_private;
> +	ioat->rawdev = rawdev;
> +	ioat->regs = dev->mem_resource[0].addr;
> +	ioat->ring_size = 0;
> +	ioat->desc_ring = NULL;
> +	ioat->status_addr = rte_malloc_virt2iova(ioat) +
> +			offsetof(struct rte_ioat_rawdev, status);

While reviewing other patch, i remembered that i've seen this here. You 
can't make any guarantees about IOVA addresses in rte_malloc-allocated 
memory. Are you sure you don't require IOVA-contiguous memory here?
  
Bruce Richardson June 28, 2019, 12:46 p.m. UTC | #3
On Thu, Jun 27, 2019 at 01:28:04PM +0100, Burakov, Anatoly wrote:
> On 27-Jun-19 11:40 AM, Bruce Richardson wrote:
> > Add the create/destroy driver functions so that we can actually allocate
> > a rawdev and destroy it when done. No rawdev API functions are actually
> > implemented at this point.
> > 
> > Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
> > ---
> 
> <snip>
> 
> > +	rawdev->driver_name = dev->device.driver->name;
> > +
> > +	ioat = rawdev->dev_private;
> > +	ioat->rawdev = rawdev;
> > +	ioat->regs = dev->mem_resource[0].addr;
> > +	ioat->ring_size = 0;
> > +	ioat->desc_ring = NULL;
> > +	ioat->status_addr = rte_malloc_virt2iova(ioat) +
> > +			offsetof(struct rte_ioat_rawdev, status);
> 
> While reviewing other patch, i remembered that i've seen this here. You
> can't make any guarantees about IOVA addresses in rte_malloc-allocated
> memory. Are you sure you don't require IOVA-contiguous memory here?
> 
Presumably we can guarantee that for structures less than 1 page in size,
this will work? I believe the device structure should be within that page
limit.
  
Burakov, Anatoly June 28, 2019, 12:59 p.m. UTC | #4
On 28-Jun-19 1:46 PM, Bruce Richardson wrote:
> On Thu, Jun 27, 2019 at 01:28:04PM +0100, Burakov, Anatoly wrote:
>> On 27-Jun-19 11:40 AM, Bruce Richardson wrote:
>>> Add the create/destroy driver functions so that we can actually allocate
>>> a rawdev and destroy it when done. No rawdev API functions are actually
>>> implemented at this point.
>>>
>>> Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
>>> ---
>>
>> <snip>
>>
>>> +	rawdev->driver_name = dev->device.driver->name;
>>> +
>>> +	ioat = rawdev->dev_private;
>>> +	ioat->rawdev = rawdev;
>>> +	ioat->regs = dev->mem_resource[0].addr;
>>> +	ioat->ring_size = 0;
>>> +	ioat->desc_ring = NULL;
>>> +	ioat->status_addr = rte_malloc_virt2iova(ioat) +
>>> +			offsetof(struct rte_ioat_rawdev, status);
>>
>> While reviewing other patch, i remembered that i've seen this here. You
>> can't make any guarantees about IOVA addresses in rte_malloc-allocated
>> memory. Are you sure you don't require IOVA-contiguous memory here?
>>
> Presumably we can guarantee that for structures less than 1 page in size,
> this will work? I believe the device structure should be within that page
> limit.
> 

No, we can't. That would only be true if you were allocating 
IOVA-contiguous memory. Otherwise there's nothing stopping the allocator 
to allocate even a few kilobytes across page boundary.

You can only ever guarantee that *one cache line* will not cross the 
page boundary with rte_malloc. With rte_memzone and IOVA_CONTIG flag, 
you'll be able to guarantee IOVA-contiguousness in all cases (or 
allocation failure).
  
Bruce Richardson June 28, 2019, 1:15 p.m. UTC | #5
On Fri, Jun 28, 2019 at 01:59:26PM +0100, Burakov, Anatoly wrote:
> On 28-Jun-19 1:46 PM, Bruce Richardson wrote:
> > On Thu, Jun 27, 2019 at 01:28:04PM +0100, Burakov, Anatoly wrote:
> > > On 27-Jun-19 11:40 AM, Bruce Richardson wrote:
> > > > Add the create/destroy driver functions so that we can actually allocate
> > > > a rawdev and destroy it when done. No rawdev API functions are actually
> > > > implemented at this point.
> > > > 
> > > > Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
> > > > ---
> > > 
> > > <snip>
> > > 
> > > > +	rawdev->driver_name = dev->device.driver->name;
> > > > +
> > > > +	ioat = rawdev->dev_private;
> > > > +	ioat->rawdev = rawdev;
> > > > +	ioat->regs = dev->mem_resource[0].addr;
> > > > +	ioat->ring_size = 0;
> > > > +	ioat->desc_ring = NULL;
> > > > +	ioat->status_addr = rte_malloc_virt2iova(ioat) +
> > > > +			offsetof(struct rte_ioat_rawdev, status);
> > > 
> > > While reviewing other patch, i remembered that i've seen this here. You
> > > can't make any guarantees about IOVA addresses in rte_malloc-allocated
> > > memory. Are you sure you don't require IOVA-contiguous memory here?
> > > 
> > Presumably we can guarantee that for structures less than 1 page in size,
> > this will work? I believe the device structure should be within that page
> > limit.
> > 
> 
> No, we can't. That would only be true if you were allocating IOVA-contiguous
> memory. Otherwise there's nothing stopping the allocator to allocate even a
> few kilobytes across page boundary.
> 
> You can only ever guarantee that *one cache line* will not cross the page
> boundary with rte_malloc. With rte_memzone and IOVA_CONTIG flag, you'll be
> able to guarantee IOVA-contiguousness in all cases (or allocation failure).
> 
Ok, so I either need to move this field to the start of the structure, i.e.
have offset zero, or else use contiguous allocation. Will fix in next
version.

/Bruce
  
Burakov, Anatoly June 28, 2019, 1:28 p.m. UTC | #6
On 28-Jun-19 2:15 PM, Bruce Richardson wrote:
> On Fri, Jun 28, 2019 at 01:59:26PM +0100, Burakov, Anatoly wrote:
>> On 28-Jun-19 1:46 PM, Bruce Richardson wrote:
>>> On Thu, Jun 27, 2019 at 01:28:04PM +0100, Burakov, Anatoly wrote:
>>>> On 27-Jun-19 11:40 AM, Bruce Richardson wrote:
>>>>> Add the create/destroy driver functions so that we can actually allocate
>>>>> a rawdev and destroy it when done. No rawdev API functions are actually
>>>>> implemented at this point.
>>>>>
>>>>> Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
>>>>> ---
>>>>
>>>> <snip>
>>>>
>>>>> +	rawdev->driver_name = dev->device.driver->name;
>>>>> +
>>>>> +	ioat = rawdev->dev_private;
>>>>> +	ioat->rawdev = rawdev;
>>>>> +	ioat->regs = dev->mem_resource[0].addr;
>>>>> +	ioat->ring_size = 0;
>>>>> +	ioat->desc_ring = NULL;
>>>>> +	ioat->status_addr = rte_malloc_virt2iova(ioat) +
>>>>> +			offsetof(struct rte_ioat_rawdev, status);
>>>>
>>>> While reviewing other patch, i remembered that i've seen this here. You
>>>> can't make any guarantees about IOVA addresses in rte_malloc-allocated
>>>> memory. Are you sure you don't require IOVA-contiguous memory here?
>>>>
>>> Presumably we can guarantee that for structures less than 1 page in size,
>>> this will work? I believe the device structure should be within that page
>>> limit.
>>>
>>
>> No, we can't. That would only be true if you were allocating IOVA-contiguous
>> memory. Otherwise there's nothing stopping the allocator to allocate even a
>> few kilobytes across page boundary.
>>
>> You can only ever guarantee that *one cache line* will not cross the page
>> boundary with rte_malloc. With rte_memzone and IOVA_CONTIG flag, you'll be
>> able to guarantee IOVA-contiguousness in all cases (or allocation failure).
>>
> Ok, so I either need to move this field to the start of the structure, i.e.
> have offset zero, or else use contiguous allocation. Will fix in next
> version.
> 
> /Bruce
> 

The latter is probably more explicit in intention, i'd rather the code 
not rely on details of rte_malloc implementation :)
  
Bruce Richardson June 28, 2019, 4:21 p.m. UTC | #7
On Thu, Jun 27, 2019 at 01:09:03PM +0100, Burakov, Anatoly wrote:
> On 27-Jun-19 11:40 AM, Bruce Richardson wrote:
> > Add the create/destroy driver functions so that we can actually allocate
> > a rawdev and destroy it when done. No rawdev API functions are actually
> > implemented at this point.
> > 
> > Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
> > ---
> 
> <snip>
> 
> >   ioat_rawdev_create(const char *name, struct rte_pci_device *dev)
> >   {
> > -	RTE_SET_USED(name);
> > -	RTE_SET_USED(dev);
> > +	static const struct rte_rawdev_ops ioat_rawdev_ops = {
> > +	};
> > +
> > +	struct rte_rawdev *rawdev = NULL;
> > +	struct rte_ioat_rawdev *ioat = NULL;
> > +	int ret = 0;
> > +	int retry = 0;
> > +
> > +	if (!name) {
> > +		IOAT_PMD_ERR("Invalid name of the device!");
> > +		ret = -EINVAL;
> > +		goto cleanup;
> > +	}
> 
> Is checking `dev` not necessary here?
> 

No, the only place it's called from is already using the PCI device
structure. I don't think the probe function can ever be called with a NULL
parameter.

> > +
> > +	/* Allocate device structure */
> > +	rawdev = rte_rawdev_pmd_allocate(name, sizeof(struct rte_ioat_rawdev),
> > +					 dev->device.numa_node);
> > +	if (rawdev == NULL) {
> > +		IOAT_PMD_ERR("Unable to allocate raw device");
> > +		ret = -EINVAL;
> > +		goto cleanup;
> 
> EINVAL is supposed to be used to indicate invalid arguments. Inability to
> allocate is not an "invalid arguments" condition. Does
> rte_rawdev_pmd_allocate() set its own errno value? If so, perhaps it would
> be worth passing it on? If not, perhaps -ENOMEM would be a better return
> value?
> 
Fixing in v4.
  

Patch

diff --git a/doc/guides/rawdevs/ioat_rawdev.rst b/doc/guides/rawdevs/ioat_rawdev.rst
index 1a4b0e03e..4b7fe8a8f 100644
--- a/doc/guides/rawdevs/ioat_rawdev.rst
+++ b/doc/guides/rawdevs/ioat_rawdev.rst
@@ -72,3 +72,14 @@  them to a suitable DPDK-supported kernel driver. When querying the status
 of the devices, they will appear under the category of "Misc (rawdev)
 devices", i.e. the command ``dpdk-devbind.py --status-dev misc`` can be
 used to see the state of those devices alone.
+
+Device Probing and Initialization
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Once bound to a suitable kernel device driver, the HW devices will be found
+as part of the PCI scan done at application initialization time. No vdev
+parameters need to be passed to create or initialize the device.
+
+Once probed successfully, the device will appear as a ``rawdev``, that is a
+"raw device type" inside DPDK, and can be accessed using APIs from the
+``rte_rawdev`` library.
diff --git a/drivers/raw/ioat/ioat_rawdev.c b/drivers/raw/ioat/ioat_rawdev.c
index d9fc3091a..d13391dd5 100644
--- a/drivers/raw/ioat/ioat_rawdev.c
+++ b/drivers/raw/ioat/ioat_rawdev.c
@@ -2,6 +2,7 @@ 
  * Copyright(c) 2019 Intel Corporation
  */
 
+#include <rte_cycles.h>
 #include <rte_bus_pci.h>
 #include <rte_rawdev_pmd.h>
 
@@ -26,15 +27,101 @@  static struct rte_pci_driver ioat_pmd_drv;
 static int
 ioat_rawdev_create(const char *name, struct rte_pci_device *dev)
 {
-	RTE_SET_USED(name);
-	RTE_SET_USED(dev);
+	static const struct rte_rawdev_ops ioat_rawdev_ops = {
+	};
+
+	struct rte_rawdev *rawdev = NULL;
+	struct rte_ioat_rawdev *ioat = NULL;
+	int ret = 0;
+	int retry = 0;
+
+	if (!name) {
+		IOAT_PMD_ERR("Invalid name of the device!");
+		ret = -EINVAL;
+		goto cleanup;
+	}
+
+	/* Allocate device structure */
+	rawdev = rte_rawdev_pmd_allocate(name, sizeof(struct rte_ioat_rawdev),
+					 dev->device.numa_node);
+	if (rawdev == NULL) {
+		IOAT_PMD_ERR("Unable to allocate raw device");
+		ret = -EINVAL;
+		goto cleanup;
+	}
+
+	rawdev->dev_ops = &ioat_rawdev_ops;
+	rawdev->device = &dev->device;
+	rawdev->driver_name = dev->device.driver->name;
+
+	ioat = rawdev->dev_private;
+	ioat->rawdev = rawdev;
+	ioat->regs = dev->mem_resource[0].addr;
+	ioat->ring_size = 0;
+	ioat->desc_ring = NULL;
+	ioat->status_addr = rte_malloc_virt2iova(ioat) +
+			offsetof(struct rte_ioat_rawdev, status);
+
+	/* do device initialization - reset and set error behaviour */
+	if (ioat->regs->chancnt != 1)
+		IOAT_PMD_ERR("%s: Channel count == %d\n", __func__,
+				ioat->regs->chancnt);
+
+	if (ioat->regs->chanctrl & 0x100) { /* locked by someone else */
+		IOAT_PMD_WARN("%s: Channel appears locked\n", __func__);
+		ioat->regs->chanctrl = 0;
+	}
+
+	ioat->regs->chancmd = RTE_IOAT_CHANCMD_SUSPEND;
+	rte_delay_ms(1);
+	ioat->regs->chancmd = RTE_IOAT_CHANCMD_RESET;
+	rte_delay_ms(1);
+	while (ioat->regs->chancmd & RTE_IOAT_CHANCMD_RESET) {
+		ioat->regs->chainaddr = 0;
+		rte_delay_ms(1);
+		if (++retry >= 200) {
+			IOAT_PMD_ERR("%s: cannot reset device. CHANCMD=0x%"PRIx8", CHANSTS=0x%"PRIx64", CHANERR=0x%"PRIx32"\n",
+					__func__,
+					ioat->regs->chancmd,
+					ioat->regs->chansts,
+					ioat->regs->chanerr);
+			ret = -EIO;
+		}
+	}
+	ioat->regs->chanctrl = RTE_IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
+			RTE_IOAT_CHANCTRL_ERR_COMPLETION_EN;
+
 	return 0;
+
+cleanup:
+	if (rawdev)
+		rte_rawdev_pmd_release(rawdev);
+
+	return ret;
 }
 
 static int
 ioat_rawdev_destroy(const char *name)
 {
-	RTE_SET_USED(name);
+	int ret;
+	struct rte_rawdev *rdev;
+
+	if (!name) {
+		IOAT_PMD_ERR("Invalid device name");
+		return -EINVAL;
+	}
+
+	rdev = rte_rawdev_pmd_get_named_dev(name);
+	if (!rdev) {
+		IOAT_PMD_ERR("Invalid device name (%s)", name);
+		return -EINVAL;
+	}
+
+	/* rte_rawdev_close is called by pmd_release */
+	ret = rte_rawdev_pmd_release(rdev);
+	if (ret)
+		IOAT_PMD_DEBUG("Device cleanup failed");
+
 	return 0;
 }
 
diff --git a/drivers/raw/ioat/rte_ioat_rawdev.h b/drivers/raw/ioat/rte_ioat_rawdev.h
index e77406403..c3216a174 100644
--- a/drivers/raw/ioat/rte_ioat_rawdev.h
+++ b/drivers/raw/ioat/rte_ioat_rawdev.h
@@ -14,6 +14,9 @@ 
  * @b EXPERIMENTAL: these structures and APIs may change without prior notice
  */
 
+#include <rte_memory.h>
+#include <rte_ioat_spec.h>
+
 /** Name of the device driver */
 #define IOAT_PMD_RAWDEV_NAME rawdev_ioat
 /** String reported as the device driver name by rte_rawdev_info_get() */
@@ -21,4 +24,21 @@ 
 /** Name used to adjust the log level for this driver */
 #define IOAT_PMD_LOG_NAME "rawdev.ioat"
 
+/**
+ * @internal
+ * Structure representing a device instance
+ */
+struct rte_ioat_rawdev {
+	struct rte_rawdev *rawdev;
+	volatile struct rte_ioat_registers *regs;
+	phys_addr_t status_addr;
+	phys_addr_t ring_addr;
+
+	unsigned short ring_size;
+	struct rte_ioat_desc *desc_ring;
+
+	/* to report completions, the device will write status back here */
+	volatile uint64_t status __rte_cache_aligned;
+};
+
 #endif