Skip to contentSkip to conversation

misc: nnpi: New PCIe driver for Intel's NNP-I pcie device

PATCH Superseded Guy Zadicario 3 participants 20 messages May 12, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover misc: nnpi: New PCIe driver for Intel's NNP-I pcie device

Hi,

The following series is a driver for a new PCIe device from Intel named NNP-I (Nirvana Neural Processor for Inference). NNP-I is a PCIe connected compute device used for acceleration of AI deep learning inference applications in the data-center.

The reason that this driver should be in the kernel is that it aims to serve multiple users and user-space applications which might share the same NNP-I card. Workloads from multiple applications can be processed simultanously by the NNP-I card if enough compute resources exist.

Overview of the NNP-I device, driver structure and ABIs used in the driver is in patch#1, which adds the info as a document as it might be a useful info for anyone trying to understand the driver even past review.

In order to ease the review process, there will be multiple series for the entire driver code. This is the first series, and it implements everything necessary to initialize the NNP-I device and allow a user-space inference application to use it. Other features, which are mostly related to maintenance, device status visibility and error-handling, will be submitted on the next stage.

A basic user-space library and test application which illustrates the flow of an NNP-I inference application can be found here: https://github.com/IntelAI/nnpi-host
(This series is enough for the test application to run)

This patchset has gone through internal review inside Intel, the summary of the change log from the internal review follows.

I would appreciate any feedback, questions or comments to this series.

Changes in v22:
- Patch#13: remove back-to-back mutex acquire when calling to check if response ring buffer is empty.
- Patch#15: remove not needed output argument to do_map_hostres() by calling to send_map_hostres_req() from do_map_hostres().

Changes in v21:
- Fixed kerneldoc warnings.
- Patch#15 - break ioctl handling function map_hostres() to smaller functions.
- Few code style issues
- Removed not-needed error printouts.
- Removed channel "respq_corrupted" state, instead made chan_read return 0 without modifying the response ring-buffer.

Changes in v20:
- Patcg#13: Handle response ring-buffer corruption by adding "respq_corruped" state.
- Patch#15: protect host resource mappings using existing dev_mutex lock instead of a new spin lock.
- Patch#12: removed not needed "force" arg to nnpdev_submit_device_event_to_channels()
- Replaced two boolean states of cmd_chan to a single channel status state
- Few code style issues

Changes in v19:
- Use global mutex to protect device chardev clients list instead of per-device mutex. (to simplify handling of device disconnect during device removal).

Changes in v18:
- Added kref to device chardev client structure to deal with device removal possible race condition.
- Fixed lkp build failure on 32-bit arch.
- Some code style issues

Changes in v17:
- Fixes v16 review comments only.
- Added Alex's reviewed-by tag to patches 2,3,5,6,8,9 (after fixing requested comments)
- Created new patch#7 as suggested by Alexander Shishkin to pull some functionality from patch#6 to a separate patch.
- Fixed lkp build warning on arm arch by using:
ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT.
(This is in patch#2)
- Rebased on top of 5.12-rc3

Changes in v16:
- Fixed v15 review comments
- Added MODULE_DEVICE_TABLE() usage for pci driver.

Changes in v15:
- fixed v14 review comments
- few bug fixes found during testing:
+ made channel's resosponse ring buffer be resizable as some stress apps stretch it.
+ Use mutex instead of spin_lock for protecting nnpdev pointer in a channel. This is since it needs to be locked when applications do write to the channel file.

Changes in v14:
- converted overview document to .rst file
- refactor into two modules, NNP-I framework and PCIe device driver. (as suggested by Andy Shevchenko)
- changed device remove flow to disconnect device from active clients, rather then waiting for clients to exit.
- Fixed all review comments.

Changes in v13:
- Fixed errors from codespell
- Fixed errors reported by sparse and cppcheck
- Modified commit messages
- Fixed all comments from v12

Changes in v12:
- Removed sharing of boot image between multiple devices
- Fixed comments from v11

Changes in v11:
- Fixed comments from v10
- Removed the following features, reducing the size of the patch-set:
- Sysfs visibility
- Handling of device errors
- Visibility to static device system info (total memory, ice count, steping, ...)
- Access to device crash log
- BIOS capsule update
- enable/disable device for new inference contexts
- PCIe reset and error handling
- Visibility to device post code and bios flash progress
- Ability to get more command queue BW for inference contexts comparing to nnpiml channels
- Device hang detection
- PCIe error injection
- Host SW traces
- Support host resources from dma-buf objects (created by another driver)
- Block non-privileged users to do any nnpi_ctl commands and query SW counters of ALL contexts.

Changes in v10:
- removed bitfield in unions from protocol (using bit masks instead)
(as suggested by Andy Shevchenko)
- renamed function names to be more consistant
- changed logical/pci "layers" to be called device and HW layers.
- removed host resource allocation method from IOCTL. Support only pinning user memory.
- re-wrote most of the commit messages, based on Alexander Shishkin comments.
- fixed errors reported by lkp

Changes in v9:
- fixed potential dead-lock in boot load flow
- IOCTL structs alignment issues
- sysfs - one value per-attribute
- code-style: func arguments and multi-line alignment
- sparse errors reported by lkp

Changes in v8:
- broke the most of the big patches to little smaller ones.
- Reviewed and fixed all comments from v7.

Changes in v7:
- Added documentation sections to many structs and functions.
- Added "Terminology" section to Documentation/misc-devices/intel-nnpi.txt
- Removed use of function pointers with interface to PCIe "h/w layer"
- Fixed IOCTLs backward compatability support.
- Patch#13 has removed - ipc s/w counters
- Patch#15 has removed - handling PCIe link-down in absense of pciehp
- Byteorder consideration - Fixed data packets sent to the device to be in little-endian. Other communications with the device is through mmio which is little-endian anyway.
- Removed sysfs "reset" attribute
- Removed sysfs attribute which outputs application pids.
- Fixed and cleaned all other comments.

Changes in v6:
- lkp build fixes
- fixes build errors when tracing is enabled
- made trace compiled by default, define NNP_DISABLE_TRACE to disable.
- fixed reviewed-by tag to mention first name before last name.
- serialize pcie reset and remove flows.
- rebased on top of current linux master

Changes in v5:
- Makefile fix for out-of-tree builds

     (added $(srctree) when specifying include path)

Changes in v4:
- Per Dave-Hansen suggestion, abandon patch#20 (misc: nnpi: Added hostres_min_order module parameter) Realized that the benefit it brings not worth the risk.
- Fixes build failures found by lkp
- Some bug fixes found in v3
- Clean W=1 build warnings

Changes in v3:
- Few small BUG fixes found during testing
- Add device bios update flow - boot flow has changed to allow booting the device either with OS boot image or bios capsule image. The device bios will start bios update if capsule image is used.
- Fixed comments from previous version

Changes in v2:
- used --strict flag to checkpatch.pl, only left CHECK comments which will break the 80 chars line length limit if fixed.
- removed CONFIG_DMA_SHARED_BUFFER ifdefs
- moved high order page allocation optimization to separete (last) patch
- removed device list array
- removed all c++ style comments

Thanks,
Guy.

Guy Zadicario (15):
misc: nnpi: Document NNP-I's driver overview
misc: nnpi: Initialize NNP-I framework and PCIe modules
misc: nnpi: Manage and schedule messages to device
misc: nnpi: Define host/card ipc protocol
misc: nnpi: Manage host memory resources
misc: nnpi: Allow usermode to manage host resources
misc: nnpi: Disallow host memory resource access if no NNP-I devices
exist
misc: nnpi: Boot NNP-I device
misc: nnpi: Process device response messages
misc: nnpi: Query and verify device protocol
misc: nnpi: Create comm channel from app to device
misc: nnpi: Route device response messages
misc: nnpi: Expose command channel file interface
misc: nnpi: Create command channel from userspace
misc: nnpi: Map host resources to device channel

 Documentation/ABI/testing/sysfs-driver-intel_nnpi  |    5 +
 Documentation/misc-devices/index.rst               |    1 +
 Documentation/misc-devices/intel-nnpi.rst          |  237 +++++
 MAINTAINERS                                        |    6 +
 drivers/misc/Kconfig                               |    1 +
 drivers/misc/Makefile                              |    1 +
 drivers/misc/intel-nnpi/Kconfig                    |   18 +
 drivers/misc/intel-nnpi/Makefile                   |   13 +
 drivers/misc/intel-nnpi/bootimage.c                |  246 +++++
 drivers/misc/intel-nnpi/bootimage.h                |   43 +
 drivers/misc/intel-nnpi/cmd_chan.c                 |  790 ++++++++++++++
 drivers/misc/intel-nnpi/cmd_chan.h                 |  134 +++
 drivers/misc/intel-nnpi/device.c                   | 1081 ++++++++++++++++++++
 drivers/misc/intel-nnpi/device.h                   |  182 ++++
 drivers/misc/intel-nnpi/device_chardev.c           |  789 ++++++++++++++
 drivers/misc/intel-nnpi/device_chardev.h           |   14 +
 drivers/misc/intel-nnpi/host_chardev.c             |  353 +++++++
 drivers/misc/intel-nnpi/host_chardev.h             |   12 +
 drivers/misc/intel-nnpi/hostres.c                  |  627 ++++++++++++
 drivers/misc/intel-nnpi/hostres.h                  |  167 +++
 .../misc/intel-nnpi/ipc_include/ipc_c2h_events.h   |  198 ++++
 drivers/misc/intel-nnpi/ipc_include/ipc_protocol.h |  340 ++++++
 .../misc/intel-nnpi/ipc_include/nnp_boot_defs.h    |   71 ++
 drivers/misc/intel-nnpi/ipc_include/nnp_elbi.h     |   91 ++
 drivers/misc/intel-nnpi/msg_scheduler.c            |  319 ++++++
 drivers/misc/intel-nnpi/msg_scheduler.h            |  153 +++
 drivers/misc/intel-nnpi/nnp_pcie.c                 |  530 ++++++++++
 drivers/misc/intel-nnpi/nnp_user.c                 |  131 +++
 drivers/misc/intel-nnpi/nnp_user.h                 |   79 ++
 include/uapi/misc/intel_nnpi.h                     |  304 ++++++
 30 files changed, 6936 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-driver-intel_nnpi
 create mode 100644 Documentation/misc-devices/intel-nnpi.rst
 create mode 100644 drivers/misc/intel-nnpi/Kconfig
 create mode 100644 drivers/misc/intel-nnpi/Makefile
 create mode 100644 drivers/misc/intel-nnpi/bootimage.c
 create mode 100644 drivers/misc/intel-nnpi/bootimage.h
 create mode 100644 drivers/misc/intel-nnpi/cmd_chan.c
 create mode 100644 drivers/misc/intel-nnpi/cmd_chan.h
 create mode 100644 drivers/misc/intel-nnpi/device.c
 create mode 100644 drivers/misc/intel-nnpi/device.h
 create mode 100644 drivers/misc/intel-nnpi/device_chardev.c
 create mode 100644 drivers/misc/intel-nnpi/device_chardev.h
 create mode 100644 drivers/misc/intel-nnpi/host_chardev.c
 create mode 100644 drivers/misc/intel-nnpi/host_chardev.h
 create mode 100644 drivers/misc/intel-nnpi/hostres.c
 create mode 100644 drivers/misc/intel-nnpi/hostres.h
 create mode 100644 drivers/misc/intel-nnpi/ipc_include/ipc_c2h_events.h
 create mode 100644 drivers/misc/intel-nnpi/ipc_include/ipc_protocol.h
 create mode 100644 drivers/misc/intel-nnpi/ipc_include/nnp_boot_defs.h
 create mode 100644 drivers/misc/intel-nnpi/ipc_include/nnp_elbi.h
 create mode 100644 drivers/misc/intel-nnpi/msg_scheduler.c
 create mode 100644 drivers/misc/intel-nnpi/msg_scheduler.h
 create mode 100644 drivers/misc/intel-nnpi/nnp_pcie.c
 create mode 100644 drivers/misc/intel-nnpi/nnp_user.c
 create mode 100644 drivers/misc/intel-nnpi/nnp_user.h
 create mode 100644 include/uapi/misc/intel_nnpi.h
Signature · 9 lines
1.8.3.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
#1 lore raw

PATCH 1/15 misc: nnpi: Document NNP-I's driver overview

Introduce overview documentation for NNP-I card and driver to let new readers of the driver understand better the driver and the NNP-I device.

---
 Documentation/misc-devices/index.rst      |   1 +
 Documentation/misc-devices/intel-nnpi.rst | 237 ++++++++++++++++++++++++++++++
 2 files changed, 238 insertions(+)
 create mode 100644 Documentation/misc-devices/intel-nnpi.rst
Documentation/misc-devices/index.rst +1 -0
index 30ac58f..7f14fc4 100644
@@ -22,6 +22,7 @@ fit into other categories.
    dw-xdata-pcie
    ibmvmc
    ics932s401
+   intel-nnpi
    isl29003
    lis3lv02d
    max6875
Documentation/misc-devices/intel-nnpi.rst new file +237 -0

237 lines not on this page · raw message

Signature · 9 lines
1.8.3.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
#2 lore raw

PATCH 2/15 misc: nnpi: Initialize NNP-I framework and PCIe modules

In reply to Guy Zadicario

Initialize the NNP-I framework module and expose PCIe device driver.

The NNP-I driver is divided into two modules, the framework module and the PCIe driver module.

The PCIe driver probes the device and provides, to the framework module, functions for sending and receiving raw commands and responses to/from the device. It does not have semantic understanding of these commands and responses, and transfers them as abstract binary data.

The framework module unpacks command and response messages, and uses the PCIe driver module to send and receive them. It uses the PCIe driver module as an abstract communication layer, and is unaware of the PCIe HW used to communicate with the NNP-I card.

Using TCP/IP jargon, the PCIe driver module is the "transport" layer and the main framework module is the "application" layer. This logical seperation was done in order to ease maintenance and validation, and make adding support for other "transport" layers easier.

The header files under the ipc_include directory provide HW register definitions and protocol structures used to communicate with the NNP-I device. These header files are used also by the NNP-I card's SW stack.

---
 MAINTAINERS                                        |   5 +
 drivers/misc/Kconfig                               |   1 +
 drivers/misc/Makefile                              |   1 +
 drivers/misc/intel-nnpi/Kconfig                    |  18 ++
 drivers/misc/intel-nnpi/Makefile                   |  12 +
 drivers/misc/intel-nnpi/device.c                   |  84 +++++
 drivers/misc/intel-nnpi/device.h                   |  39 +++
 .../misc/intel-nnpi/ipc_include/nnp_boot_defs.h    |  71 ++++
 drivers/misc/intel-nnpi/ipc_include/nnp_elbi.h     |  91 ++++++
 drivers/misc/intel-nnpi/nnp_pcie.c                 | 358 +++++++++++++++++++++
 10 files changed, 680 insertions(+)
 create mode 100644 drivers/misc/intel-nnpi/Kconfig
 create mode 100644 drivers/misc/intel-nnpi/Makefile
 create mode 100644 drivers/misc/intel-nnpi/device.c
 create mode 100644 drivers/misc/intel-nnpi/device.h
 create mode 100644 drivers/misc/intel-nnpi/ipc_include/nnp_boot_defs.h
 create mode 100644 drivers/misc/intel-nnpi/ipc_include/nnp_elbi.h
 create mode 100644 drivers/misc/intel-nnpi/nnp_pcie.c
MAINTAINERS +5 -0
index bd7aff0c..ff0c3d7 100644
@@ -9316,6 +9316,11 @@ S:	Supported
 W:	https://01.org/linux-acpi
 F:	drivers/platform/x86/intel_menlow.c
 
+INTEL NNP-I PCI DRIVER
+M:	Guy Zadicario <[email protected]>
+S:	Supported
+F:	drivers/misc/intel-nnpi/
+
 INTEL P-Unit IPC DRIVER
 M:	Zha Qipeng <[email protected]>
 L:	[email protected]
drivers/misc/Kconfig +1 -0
index f4fb5c5..4c67fac 100644
@@ -462,6 +462,7 @@ source "drivers/misc/ocxl/Kconfig"
 source "drivers/misc/bcm-vk/Kconfig"
 source "drivers/misc/cardreader/Kconfig"
 source "drivers/misc/habanalabs/Kconfig"
+source "drivers/misc/intel-nnpi/Kconfig"
 source "drivers/misc/uacce/Kconfig"
 source "drivers/misc/pvpanic/Kconfig"
 endmenu
drivers/misc/Makefile +1 -0
index e92a56d..592ac64 100644
@@ -54,6 +54,7 @@ obj-$(CONFIG_BCM_VK)		+= bcm-vk/
 obj-y				+= cardreader/
 obj-$(CONFIG_PVPANIC)   	+= pvpanic/
 obj-$(CONFIG_HABANA_AI)		+= habanalabs/
+obj-$(CONFIG_INTEL_NNPI)        += intel-nnpi/
 obj-$(CONFIG_UACCE)		+= uacce/
 obj-$(CONFIG_XILINX_SDFEC)	+= xilinx_sdfec.o
 obj-$(CONFIG_HISI_HIKEY_USB)	+= hisi_hikey_usb.o
drivers/misc/intel-nnpi/Kconfig new file +18 -0
new file mode 100644
index 0000000..ccd39df
@@ -0,0 +1,18 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+#
+# Copyright (C) 2019-2021 Intel Corporation
+#
+#
+
+config INTEL_NNPI
+	tristate "Intel(R) PCIe NNP-I (AI accelerator for inference) device driver"
+	depends on PCI
+	select DMA_SHARED_BUFFER
+	help
+	  Device driver for Intel NNP-I PCIe accelerator card for AI inference.
+
+	  If unsure, say N.
+
+	  To compile this driver as a module, choose M here. Two modules will
+          get generated intel_nnpi and intel_nnpi_pcie.
drivers/misc/intel-nnpi/Makefile new file +12 -0
new file mode 100644
index 0000000..84b7528
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Copyright (C) 2019-2021 Intel Corporation
+#
+
+obj-$(CONFIG_INTEL_NNPI) := intel_nnpi.o intel_nnpi_pcie.o
+
+intel_nnpi-y := device.o
+
+intel_nnpi_pcie-y := nnp_pcie.o
+
+ccflags-y += -I$(srctree)/$(src)/ipc_include
drivers/misc/intel-nnpi/device.c new file +84 -0

84 lines not on this page · raw message

drivers/misc/intel-nnpi/device.h new file +39 -0
new file mode 100644
index 0000000..4ff7aa9
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (C) 2019-2021 Intel Corporation */
+
+#ifndef _NNPDRV_DEVICE_H
+#define _NNPDRV_DEVICE_H
+
+#define NNP_MAX_DEVS		256
+
+/**
+ * struct nnp_device - structure for NNP-I device info
+ * @ops: device operations implemented by the underlying device driver
+ * @dev: pointer to struct device representing the NNP-I card.
+ * @id: NNP-I device number
+ */
+struct nnp_device {
+	const struct nnp_device_ops *ops;
+	struct device               *dev;
+	int                         id;
+};
+
+/**
+ * struct nnp_device_ops - operations implemented by underlying device driver
+ * @cmdq_flush: empties the device command queue, discarding all queued
+ *              commands.
+ */
+struct nnp_device_ops {
+	int (*cmdq_flush)(struct nnp_device *hw_dev);
+};
+
+/*
+ * Functions exported by the device framework module which are
+ * called by the lower layer NNP-I device driver module
+ */
+int nnpdev_init(struct nnp_device *nnpdev, struct device *dev,
+		const struct nnp_device_ops *ops);
+void nnpdev_destroy(struct nnp_device *nnpdev);
+void nnpdev_card_doorbell_value_changed(struct nnp_device *nnpdev,
+					u32 doorbell_val);
+#endif
drivers/misc/intel-nnpi/ipc_include/nnp_boot_defs.h new file +71 -0

71 lines not on this page · raw message

drivers/misc/intel-nnpi/ipc_include/nnp_elbi.h new file +91 -0

91 lines not on this page · raw message

drivers/misc/intel-nnpi/nnp_pcie.c new file +358 -0

358 lines not on this page · raw message

Signature · 9 lines
1.8.3.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
reviewer
#3 lore raw

Re: [PATCH 02/15] misc: nnpi: Initialize NNP-I framework and PCIe modules

Quoted 14 lines · Guy Zadicario
drivers/misc/intel-nnpi/Kconfig new file +8 -0
new file mode 100644
index 0000000..ccd39df
@@ -0,0 +1,18 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+#
+# Copyright (C) 2019-2021 Intel Corporation
+#
+#
+
+config INTEL_NNPI

drivers/misc/intel-nnpi/Kconfig line 18

Quoted 10 lines
drivers/misc/intel-nnpi/Kconfig new file +10 -0
+	tristate "Intel(R) PCIe NNP-I (AI accelerator for inference) device driver"
+	depends on PCI
+	select DMA_SHARED_BUFFER
+	help
+	  Device driver for Intel NNP-I PCIe accelerator card for AI inference.
+
+	  If unsure, say N.
+
+	  To compile this driver as a module, choose M here. Two modules will
+          get generated intel_nnpi and intel_nnpi_pcie.

For the last help text line, use a tab + 2 spaces for indentation. Also 2 more changes: s/get generated/be generated:/

thanks.

Signature · 1 line
~Randy
#4 lore raw

PATCH 3/15 misc: nnpi: Manage and schedule messages to device

In reply to Guy Zadicario

Allocate a msg_scheduler object for each NNP-I device. Each object manages multiple command queues - one for driver initiated commands (called "cmdq"), and the rest for commands coming from user-space. A kernel thread in the msg_scheduler schedules sending the commands from these queues to the NNP-I device by using the cmdq_write_mesg function of the NNP-I device driver ops.

The msg_scheduler object allows multiple user-mode applications to put messages into the queue without blocking or waiting on a lock. It's created on device creation and destroyed on device removal.

---
 drivers/misc/intel-nnpi/Makefile        |   2 +-
 drivers/misc/intel-nnpi/device.c        |  20 ++
 drivers/misc/intel-nnpi/device.h        |   9 +
 drivers/misc/intel-nnpi/msg_scheduler.c | 319 ++++++++++++++++++++++++++++++++
 drivers/misc/intel-nnpi/msg_scheduler.h | 153 +++++++++++++++
 drivers/misc/intel-nnpi/nnp_pcie.c      | 153 +++++++++++++++
 6 files changed, 655 insertions(+), 1 deletion(-)
 create mode 100644 drivers/misc/intel-nnpi/msg_scheduler.c
 create mode 100644 drivers/misc/intel-nnpi/msg_scheduler.h
drivers/misc/intel-nnpi/Makefile +1 -1
index 84b7528..43f09c0 100644
@@ -5,7 +5,7 @@
 
 obj-$(CONFIG_INTEL_NNPI) := intel_nnpi.o intel_nnpi_pcie.o
 
-intel_nnpi-y := device.o
+intel_nnpi-y := device.o msg_scheduler.o
 
 intel_nnpi_pcie-y := nnp_pcie.o
 
drivers/misc/intel-nnpi/device.c +20 -0
index 3d80e95..60c5a94 100644
@@ -8,6 +8,7 @@
 #include <linux/module.h>
 
 #include "device.h"
+#include "msg_scheduler.h"
 
 static DEFINE_IDA(dev_ida);
 
@@ -45,7 +46,25 @@ int nnpdev_init(struct nnp_device *nnpdev, struct device *dev,
 	nnpdev->dev = dev;
 	nnpdev->ops = ops;
 
+	nnpdev->cmdq_sched = nnp_msched_create(nnpdev);
+	if (!nnpdev->cmdq_sched) {
+		ret = -ENOMEM;
+		goto err_ida;
+	}
+
+	nnpdev->cmdq = nnp_msched_queue_create(nnpdev->cmdq_sched);
+	if (!nnpdev->cmdq) {
+		ret = -ENOMEM;
+		goto err_msg_sched;
+	}
+
 	return 0;
+
+err_msg_sched:
+	nnp_msched_destroy(nnpdev->cmdq_sched);
+err_ida:
+	ida_simple_remove(&dev_ida, nnpdev->id);
+	return ret;
 }
 EXPORT_SYMBOL(nnpdev_init);
 
@@ -75,6 +94,7 @@ void nnpdev_destroy(struct nnp_device *nnpdev)
 {
 	dev_dbg(nnpdev->dev, "Destroying NNP-I device\n");
 
+	nnp_msched_destroy(nnpdev->cmdq_sched);
 	ida_simple_remove(&dev_ida, nnpdev->id);
 }
 EXPORT_SYMBOL(nnpdev_destroy);
drivers/misc/intel-nnpi/device.h +9 -0
index 4ff7aa9..7d7ef60 100644
@@ -11,20 +11,29 @@
  * @ops: device operations implemented by the underlying device driver
  * @dev: pointer to struct device representing the NNP-I card.
  * @id: NNP-I device number
+ * @cmdq_sched: message scheduler thread which schedules and serializes command
+ *              submissions to the device's command queue.
+ * @cmdq: input queue to @cmdq_sched used to schedule driver internal commands
+ *        to be sent to the device.
  */
 struct nnp_device {
 	const struct nnp_device_ops *ops;
 	struct device               *dev;
 	int                         id;
+
+	struct nnp_msched       *cmdq_sched;
+	struct nnp_msched_queue *cmdq;
 };
 
 /**
  * struct nnp_device_ops - operations implemented by underlying device driver
  * @cmdq_flush: empties the device command queue, discarding all queued
  *              commands.
+ * @cmdq_write_mesg: inserts a command message to the card's command queue.
  */
 struct nnp_device_ops {
 	int (*cmdq_flush)(struct nnp_device *hw_dev);
+	int (*cmdq_write_mesg)(struct nnp_device *nnpdev, u64 *msg, u32 size);
 };
 
 /*
drivers/misc/intel-nnpi/msg_scheduler.c new file +319 -0

319 lines not on this page · raw message

drivers/misc/intel-nnpi/msg_scheduler.h new file +153 -0

153 lines not on this page · raw message

drivers/misc/intel-nnpi/nnp_pcie.c +153 -0

195 lines not on this page · raw message

Signature · 9 lines
1.8.3.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
#5 lore raw

PATCH 4/15 misc: nnpi: Define host/card ipc protocol

In reply to Guy Zadicario

Define the commands and memory block structures which can be sent to the NNP-I device from the "device" layer.

---
 drivers/misc/intel-nnpi/ipc_include/ipc_protocol.h | 337 +++++++++++++++++++++
 1 file changed, 337 insertions(+)
 create mode 100644 drivers/misc/intel-nnpi/ipc_include/ipc_protocol.h
drivers/misc/intel-nnpi/ipc_include/ipc_protocol.h new file +337 -0
new file mode 100644
index 0000000..59b4a79
@@ -0,0 +1,337 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (C) 2019-2021 Intel Corporation */
+
+#ifndef _IPC_PROTOCOL_H
+#define _IPC_PROTOCOL_H
+
+#include <linux/bits.h>
+#include <linux/bitfield.h>
+#include <linux/dma-mapping.h>
+#include <linux/types.h>
+
+#define IPC_OP_MAX          BIT(6)
+#define NNP_IPC_OPCODE_MASK GENMASK(5, 0)
+
+#define NNP_MSG_SIZE(msg) (sizeof(msg) / sizeof(__le64))
+
+/*
+ * NNP_PAGE_SIZE is a size of a page in the protocol.
+ * We do not use just PAGE_SIZE since it may differ between
+ * card and host.
+ */
+#define NNP_PAGE_SHIFT       12
+#define NNP_PAGE_SIZE        BIT(12)
+
+#define NNP_VERSION_DOT_MASK   GENMASK(4, 0)
+#define NNP_VERSION_MINOR_MASK GENMASK(9, 5)
+#define NNP_VERSION_MAJOR_MASK GENMASK(14, 10)
+#define NNP_VERSION_MAJOR(ver) FIELD_GET(NNP_VERSION_MAJOR_MASK, (ver))
+#define NNP_VERSION_MINOR(ver) FIELD_GET(NNP_VERSION_MINOR_MASK, (ver))
+#define NNP_VERSION_DOT(ver)   FIELD_GET(NNP_VERSION_DOT_MASK, (ver))
+#define NNP_MAKE_VERSION(major, minor, dot) \
+	(FIELD_PREP(NNP_VERSION_MAJOR_MASK, (major)) | \
+	 FIELD_PREP(NNP_VERSION_MINOR_MASK, (minor)) | \
+	 FIELD_PREP(NNP_VERSION_DOT_MASK, (dot)))
+
+#define NNP_IPC_PROTOCOL_VERSION NNP_MAKE_VERSION(4, 1, 0)
+
+#define NNP_IPC_DMA_PFN_BITS         45 /* size of physical address in protocol */
+#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
+#define NNP_IPC_DMA_PFN_MASK         GENMASK_ULL(56, 12)
+#define NNP_IPC_DMA_MAX_ADDR         GENMASK_ULL(56, 0)
+#else
+#define NNP_IPC_DMA_PFN_MASK         GENMASK_ULL(31, 12)
+#define NNP_IPC_DMA_MAX_ADDR         GENMASK_ULL(31, 0)
+#endif
+#define NNP_IPC_DMA_ADDR_TO_PFN(dma_addr) \
+				     FIELD_GET(NNP_IPC_DMA_PFN_MASK, (dma_addr))
+#define NNP_IPC_DMA_PFN_TO_ADDR(dma_pfn)  \
+				     FIELD_PREP(NNP_IPC_DMA_PFN_MASK, (dma_pfn))
+
+#define NNP_IPC_INF_CHANNEL_BITS 8
+#define NNP_IPC_CHANNEL_BITS     10
+#define NNP_IPC_MAX_CHANNEL_RB   2
+
+/*
+ * Structures used inside data packets transferred in the protocol
+ */
+struct nnp_dma_chain_header {
+	__le64 dma_next;
+	__le32 total_nents;
+	__le32 start_offset;
+	__le64 size;
+} __packed;
+
+#define DMA_CHAIN_ENTRY_SIZE  sizeof(__le64)
+#define DMA_CHAIN_HEADER_SIZE  sizeof(struct nnp_dma_chain_header)
+#define DMA_CHAIN_ENTRY_NPAGES_BITS \
+	(sizeof(__le64) * __CHAR_BIT__ - NNP_IPC_DMA_PFN_BITS)
+#define NNP_MAX_CHUNK_SIZE \
+	(((1lu << DMA_CHAIN_ENTRY_NPAGES_BITS) - 1) << NNP_PAGE_SHIFT)
+
+struct nnp_dma_chain_entry {
+	u64 dma_chunk_pfn  : NNP_IPC_DMA_PFN_BITS;
+	u64 n_pages        : DMA_CHAIN_ENTRY_NPAGES_BITS;
+} __packed;
+
+#define DMA_CHAIN_ENTRY_PFN_MASK    GENMASK_ULL(NNP_IPC_DMA_PFN_BITS - 1, 0)
+#define DMA_CHAIN_ENTRY_NPAGES_MASK GENMASK_ULL(63, NNP_IPC_DMA_PFN_BITS)
+
+#define NENTS_PER_PAGE \
+	((NNP_PAGE_SIZE - DMA_CHAIN_HEADER_SIZE) / DMA_CHAIN_ENTRY_SIZE)
+
+/*
+ * IPC messages layout definition
+ */
+
+#define NNP_C2H_OP_MASK       GENMASK_ULL(5, 0)
+
+/* NNP_IPC_C2H_OP_QUERY_VERSION_REPLY3 - 3 qwords */
+/* qword 0: */
+#define NNP_C2H_VERSION_REPLY_QW0_OP_MASK       NNP_C2H_OP_MASK
+#define NNP_C2H_VERSION_REPLY_QW0_PROT_VER_MASK GENMASK_ULL(21, 6)
+#define NNP_C2H_VERSION_REPLY_QW0_FW_VER_MASK   GENMASK_ULL(37, 22)
+#define NNP_C2H_VERSION_REPLY_QW0_CHAN_VER_MASK GENMASK_ULL(53, 38)
+/* qword 1: two bits for each possible response opcode specifying its size */
+/* qword 2: two bits for each possible command opcode specifying its size */
+
+/* NNP_IPC_C2H_OP_EVENT_REPORT  - 1 qword */
+#define NNP_C2H_EVENT_REPORT_OP_MASK         NNP_C2H_OP_MASK
+#define NNP_C2H_EVENT_REPORT_CODE_MASK       GENMASK_ULL(12, 6)
+#define NNP_C2H_EVENT_REPORT_CHAN_ID_MASK    GENMASK_ULL(20, 13)
+#define NNP_C2H_EVENT_REPORT_OBJ_ID_MASK     GENMASK_ULL(36, 21)
+#define NNP_C2H_EVENT_REPORT_OBJ_ID2_MASK    GENMASK_ULL(52, 37)
+#define NNP_C2H_EVENT_REPORT_VAL_MASK        GENMASK_ULL(60, 53)
+#define NNP_C2H_EVENT_REPORT_CHAN_VALID_MASK BIT_ULL(61)
+#define NNP_C2H_EVENT_REPORT_OBJ_VALID_MASK  BIT_ULL(62)
+#define NNP_C2H_EVENT_REPORT_OBJ2_VALID_MASK BIT_ULL(63)
+
+/* NNP_IPC_C2H_OP_SYS_INFO - 1 qword */
+#define NNP_C2H_SYS_INFO_OP_MASK  NNP_C2H_OP_MASK
+
+#define NNP_H2C_OP_MASK       GENMASK_ULL(5, 0)
+
+/* NNP_IPC_H2C_OP_QUERY_VERSION - 1 qword */
+#define NNP_H2C_QUERY_VERSION_OP_MASK  NNP_H2C_OP_MASK
+
+/* NNP_IPC_H2C_OP_SETUP_CRASH_DUMP - 2 qwords */
+/* qword 0: */
+#define NNP_H2C_SETUP_CRASH_DUMP_QW0_OP_MASK       NNP_H2C_OP_MASK
+#define NNP_H2C_SETUP_CRASH_DUMP_QW0_DMA_ADDR_MASK GENMASK_ULL(63, 19)
+/* qword 1: physical address of BAR2 */
+
+/* NNP_IPC_H2C_OP_SETUP_SYS_INFO_PAGE - 1 qword */
+#define NNP_H2C_SETUP_SYS_INFO_OP_MASK       NNP_H2C_OP_MASK
+#define NNP_H2C_SETUP_SYS_INFO_NPAGES        GENMASK_ULL(15, 6)
+#define NNP_H2C_SETUP_SYS_INFO_DMA_ADDR_MASK GENMASK_ULL(63, 19)
+
+/* NNP_IPC_H2C_OP_CHANNEL_OP - 1 qword */
+#define NNP_H2C_CHANNEL_OP_OP_MASK      NNP_H2C_OP_MASK
+#define NNP_H2C_CHANNEL_OP_CHAN_ID_MASK GENMASK_ULL(15, 6)
+#define NNP_H2C_CHANNEL_OP_DESTROY_MASK BIT_ULL(16)
+#define NNP_H2C_CHANNEL_OP_PRIV_MASK    BIT_ULL(31)
+#define NNP_H2C_CHANNEL_OP_UID_MASK     GENMASK_ULL(63, 32)
+
+/* NNP_IPC_H2C_OP_CHANNEL_RB_OP - 1 qword */
+#define NNP_H2C_CHANNEL_RB_OP_OP_MASK      NNP_H2C_OP_MASK
+#define NNP_H2C_CHANNEL_RB_OP_CHAN_ID_MASK GENMASK_ULL(15, 6)
+#define NNP_H2C_CHANNEL_RB_OP_H2C_MASK     BIT_ULL(16)
+#define NNP_H2C_CHANNEL_RB_OP_ID_MASK      BIT_ULL(17)
+#define NNP_H2C_CHANNEL_RB_OP_DESTROY_MASK BIT_ULL(18)
+#define NNP_H2C_CHANNEL_RB_OP_HOST_PFN_MASK GENMASK_ULL(63, 19)
+
+/* NNP_IPC_H2C_OP_CHANNEL_HOSTRES_OP - 2 qwords */
+/* qword 0: */
+#define NNP_H2C_CHANNEL_HOSTRES_QW0_OP_MASK      NNP_H2C_OP_MASK
+#define NNP_H2C_CHANNEL_HOSTRES_QW0_CHAN_ID_MASK GENMASK_ULL(15, 6)
+#define NNP_H2C_CHANNEL_HOSTRES_QW0_ID_MASK      GENMASK_ULL(31, 16)
+#define NNP_H2C_CHANNEL_HOSTRES_QW0_UNMAP_MASK   BIT_ULL(32)
+/* qword 1: */
+#define NNP_H2C_CHANNEL_HOSTRES_QW1_HOST_PFN_MASK GENMASK_ULL(44, 0)
+
+/* NNP_IPC_H2C_OP_CLOCK_STAMP - 2 qwords */
+/* qword 0: */
+#define NNP_H2C_CLOCK_STAMP_QW0_OP_MASK   NNP_H2C_OP_MASK
+#define NNP_H2C_CLOCK_STAMP_QW0_TYPE_MASK GENMASK_ULL(63, 8)
+/* qword 1: clock stamp value */
+
+/*
+ * IPC messages opcodes and related utility macros
+ */
+#define H2C_OPCODE_NAME(name)          NNP_IPC_H2C_OP_ ## name
+#define H2C_OPCODE_NAME_STR(name)      #name
+#define C2H_OPCODE_NAME(name)          NNP_IPC_C2H_OP_ ## name
+#define C2H_OPCODE_NAME_STR(name)      #name
+
+/*
+ * Define Host-to-card opcodes  (valid range is 0 - 31)
+ */
+enum nnp_h2c_opcodes {
+	H2C_OPCODE_NAME(QUERY_VERSION)       = 0,
+	H2C_OPCODE_NAME(CLOCK_STAMP)         = 2,
+	H2C_OPCODE_NAME(SETUP_CRASH_DUMP)    = 6,
+	H2C_OPCODE_NAME(SETUP_SYS_INFO_PAGE) = 7,
+	H2C_OPCODE_NAME(CHANNEL_OP)          = 22,
+	H2C_OPCODE_NAME(CHANNEL_RB_OP)       = 23,
+	H2C_OPCODE_NAME(CHANNEL_HOSTRES_OP)  = 24,
+
+	H2C_OPCODE_NAME(BIOS_PROTOCOL)       = 31,
+	H2C_OPCODE_NAME(LAST)                = NNP_IPC_H2C_OP_BIOS_PROTOCOL,
+};
+
+/*
+ * Define Card-to-host opcodes
+ */
+enum nnp_c2h_opcodes {
+	NNP_IPC_C2H_OP_QUERY_VERSION_REPLY  = 0,
+	NNP_IPC_C2H_OP_QUERY_VERSION_REPLY2 = 1,
+	NNP_IPC_C2H_OP_QUERY_VERSION_REPLY3 = 2,
+	NNP_IPC_C2H_OP_EVENT_REPORT         = 4,
+	NNP_IPC_C2H_OP_SYS_INFO             = 11,
+
+	NNP_IPC_C2H_OP_BIOS_PROTOCOL        = 31,
+	NNP_IPC_C2H_OPCODE_LAST             = NNP_IPC_C2H_OP_BIOS_PROTOCOL,
+};
+
+/*
+ * IPC messages protocol between the host driver and card BIOS
+ */
+
+enum nnp_bios_c2h_msg_types {
+	NNP_IPC_C2H_TYPE_BIOS_VERSION  = 0x1,
+};
+
+enum nnp_bios_h2c_msg_types {
+	NNP_IPC_H2C_TYPE_BOOT_IMAGE_READY  = 0x10,
+	NNP_IPC_H2C_TYPE_SYSTEM_INFO_REQ   = 0x11,
+};
+
+/* NNP_IPC_C2H_OP_BIOS_PROTOCOL - 1 qword */
+#define NNP_C2H_BIOS_PROTOCOL_OP_MASK   NNP_C2H_OP_MASK
+/* bios message type */
+#define NNP_C2H_BIOS_PROTOCOL_TYPE_MASK GENMASK_ULL(15, 8)
+/* message size in bytes */
+#define NNP_C2H_BIOS_PROTOCOL_SIZE_MASK GENMASK_ULL(31, 16)
+
+/* BIOS Revision Identification Specification, Rev. 2.0, 01/30/2015 */
+struct nnp_c2h_bios_version {
+	__le16 board_id[7];
+	__le16 board_rev;
+	__le16 dot1;
+	__le16 board_ext[3];
+	__le16 dot2;
+	__le16 version_major[4];
+	__le16 dot3;
+	__le16 build_type;
+	__le16 version_minor[2];
+	__le16 dot4;
+	__le16 time_stamp[10];
+	__le16 null_terminator;
+} __packed;
+
+struct nnp_c2h_bios_fw_ver_ack_data {
+	__le16  code_minor;
+	__le16  code_major;
+	__le16  code_build_no;
+	__le16  code_hot_fix;
+	__le16  rcvyminor;
+	__le16  rcvymajor;
+	__le16  rcvybuildno;
+	__le16  rcvy_hot_fix;
+	__le16  fitc_minor;
+	__le16  fitc_major;
+	__le16  fitcbuildno;
+	__le16  fitc_hot_fix;
+} __packed;
+
+struct nnp_c2h_fw_version {
+	__le16  major;
+	__le16  minor;
+	__le16  hotfix;
+	__le16  build;
+} __packed;
+
+struct nnp_c2h_cpu_info {
+	__le32 cpu_family;      /* CPU Family: 0x000906D0 */
+	__u8  cpu_stepping;    /* CPU Stepping */
+	__u8  cpu_sku;         /* CPU SKU */
+	__le16 cpu_did;         /* range 0x4580-0x45FF */
+	__le16 cpu_core_count;   /* Number of enabled cores */
+	__le16 cpu_thread_count; /* Number of threads */
+} __packed;
+
+struct nnp_c2h_ice_info {
+	__le16 ice_count;
+	__le32 ice_available_mask;
+} __packed;
+
+struct nnp_c2h_system_info {
+	__u8  version; /* NNP_SYSTEM_INFO structure version */
+	__le16 board_id; /* Board identification- for RVP = 0x25 */
+	__u8  fab_id;   /* Board Revision identification */
+	__u8  bom_id;   /* Board Bill Of Material identification */
+	__u8  platform_type;   /* For RVP= 0x2, M.2 = 0x3 */
+	__u8  platform_flavor; /* For NNP = 0x5- Embedded */
+	struct nnp_c2h_cpu_info cpu_info; /* CPU Information */
+	struct nnp_c2h_ice_info ice_info; /* ICE Information */
+	struct nnp_c2h_bios_version bios_ver; /* BIOS version string */
+	struct nnp_c2h_bios_fw_ver_ack_data csme_version;
+	struct nnp_c2h_fw_version pmc_version;
+} __packed;
+
+/* NNP_H2C_BOOT_IMAGE_READY command - 3 qwords */
+/* qword 0: */
+/* op should be set to NNP_IPC_H2C_OP_BIOS_PROTOCOL */
+#define NNP_H2C_BOOT_IMAGE_READY_QW0_OP_MASK         NNP_H2C_OP_MASK
+/* bios message type = NNP_IPC_H2C_TYPE_BOOT_IMAGE_READY */
+#define NNP_H2C_BOOT_IMAGE_READY_QW0_TYPE_MASK       GENMASK_ULL(15, 8)
+/* message size in bytes = 2*sizeof(u64) */
+#define NNP_H2C_BOOT_IMAGE_READY_QW0_SIZE_MASK       GENMASK_ULL(31, 16)
+/* qword 1: page table describtor address */
+/* qword 2: */
+#define NNP_H2C_BOOT_IMAGE_READY_QW2_DESC_SIZE_MASK  GENMASK_ULL(31, 0)
+#define NNP_H2C_BOOT_IMAGE_READY_QW2_IMAGE_SIZE_MASK GENMASK_ULL(63, 32)
+
+/* NNP_H2C_BIOS_SYS_INFO_REQ command - 3 qwords */
+/* qword 0: */
+/* op should be set to NNP_IPC_H2C_OP_BIOS_PROTOCOL */
+#define NNP_H2C_BIOS_SYS_INFO_REQ_QW0_OP_MASK         NNP_H2C_OP_MASK
+/* bios message type = NNP_IPC_H2C_TYPE_SYSTEM_INFO_REQ */
+#define NNP_H2C_BIOS_SYS_INFO_REQ_QW0_TYPE_MASK       GENMASK_ULL(15, 8)
+/* message size in bytes = 2*sizeof(u64) */
+#define NNP_H2C_BIOS_SYS_INFO_REQ_QW0_SIZE_MASK       GENMASK_ULL(31, 16)
+/* qword 1: allocate sys info page address */
+/* qword 2: */
+#define NNP_H2C_BIOS_SYS_INFO_REQ_QW2_SIZE_MASK       GENMASK_ULL(31, 0)
+
+#define NNP_BIOS_VERSION_LEN  \
+		(sizeof(struct nnp_c2h_bios_version) / sizeof(__le16))
+#define NNP_BOARD_NAME_LEN      72
+#define NNP_IMAGE_VERSION_LEN   128
+#define NNP_PRD_SERIAL_LEN      16
+#define NNP_PART_NUM_LEN        12
+
+struct nnp_sys_info {
+	__le32 ice_mask;
+	char bios_version[NNP_BIOS_VERSION_LEN];
+	char board_name[NNP_BOARD_NAME_LEN];
+	char image_version[NNP_IMAGE_VERSION_LEN];
+	char prd_serial[NNP_PRD_SERIAL_LEN];
+	char brd_part_no[NNP_PART_NUM_LEN];
+	__le16  fpga_rev;
+	__le64 total_unprotected_memory;
+	__le64 total_ecc_memory;
+	__u8 stepping;
+} __packed;
+
+/*
+ * Define header structure for all "channel" message protocols.
+ * This protocol defines communication between host UMD and card.
+ */
+#define NNP_H2C_CHAN_MSG_OP_MASK      NNP_H2C_OP_MASK
+#define NNP_H2C_CHAN_MSG_CHAN_ID_MASK GENMASK_ULL(15, 6)
+
+#define NNP_C2H_CHAN_MSG_OP_MASK      NNP_C2H_OP_MASK
+#define NNP_C2H_CHAN_MSG_CHAN_ID_MASK GENMASK_ULL(15, 6)
+
+#endif /* of _IPC_PROTOCOL_H */
Signature · 9 lines
1.8.3.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
#6 lore raw

PATCH 5/15 misc: nnpi: Manage host memory resources

In reply to Guy Zadicario

Provide interface for creating a "host resource" - a memory object which can be mapped to a dma address space of one or more NNP-I devices. These host resource objects manage memory blocks from which big chunks of data, such as inference tensors, the device's embedded OS and telemtry data are DMA'ed to and from the NNP-I device.

---
 Documentation/ABI/testing/sysfs-driver-intel_nnpi |   5 +
 drivers/misc/intel-nnpi/Makefile                  |   2 +-
 drivers/misc/intel-nnpi/hostres.c                 | 627 ++++++++++++++++++++++
 drivers/misc/intel-nnpi/hostres.h                 | 167 ++++++
 4 files changed, 800 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/ABI/testing/sysfs-driver-intel_nnpi
 create mode 100644 drivers/misc/intel-nnpi/hostres.c
 create mode 100644 drivers/misc/intel-nnpi/hostres.h
Documentation/ABI/testing/sysfs-driver-intel_nnpi new file +5 -0
new file mode 100644
index 0000000..b09880f
@@ -0,0 +1,5 @@
+What:           /sys/class/nnpi_host/nnpi_host/total_hostres_size
+Date:           Mar 2021
+Kernelversion:  5.13
+Contact:        [email protected]
+Description:    Total size in bytes of all allocated NNP-I host resources.
drivers/misc/intel-nnpi/Makefile +1 -1
index 43f09c0..3713d51 100644
@@ -5,7 +5,7 @@
 
 obj-$(CONFIG_INTEL_NNPI) := intel_nnpi.o intel_nnpi_pcie.o
 
-intel_nnpi-y := device.o msg_scheduler.o
+intel_nnpi-y := device.o msg_scheduler.o hostres.o
 
 intel_nnpi_pcie-y := nnp_pcie.o
 
drivers/misc/intel-nnpi/hostres.c new file +627 -0

627 lines not on this page · raw message

drivers/misc/intel-nnpi/hostres.h new file +167 -0

167 lines not on this page · raw message

Signature · 9 lines
1.8.3.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
#7 lore raw

PATCH 6/15 misc: nnpi: Allow usermode to manage host resources

In reply to Guy Zadicario

Provide an IOCTL interface for creating and destroying host memory resources through a character device (/dev/nnpi_host).

There is a single instance of this character device in the system regardless of the number of NNP-I devices attached because it controls host resources which may be shared between different devices.

A nnp_user object, created when an application opens this character device, identifies the user (client) of the driver and holds a list of all host resources allocated by that user through the opened file descriptor.

Host memory resources created through this character device can be mapped to device access through IOCTLs made to a different, per-device, chardev (will be introduced on next commits).

All resources will be destroyed when the application closes the connection or exits.

The IOCTL interface is defined in: include/uapi/misc/intel_nnpi.h

---
 MAINTAINERS                            |   1 +
 drivers/misc/intel-nnpi/Makefile       |   2 +-
 drivers/misc/intel-nnpi/device.c       |  14 ++
 drivers/misc/intel-nnpi/host_chardev.c | 346 +++++++++++++++++++++++++++++++++
 drivers/misc/intel-nnpi/host_chardev.h |  12 ++
 drivers/misc/intel-nnpi/nnp_user.c     | 131 +++++++++++++
 drivers/misc/intel-nnpi/nnp_user.h     |  79 ++++++++
 include/uapi/misc/intel_nnpi.h         | 150 ++++++++++++++
 8 files changed, 734 insertions(+), 1 deletion(-)
 create mode 100644 drivers/misc/intel-nnpi/host_chardev.c
 create mode 100644 drivers/misc/intel-nnpi/host_chardev.h
 create mode 100644 drivers/misc/intel-nnpi/nnp_user.c
 create mode 100644 drivers/misc/intel-nnpi/nnp_user.h
 create mode 100644 include/uapi/misc/intel_nnpi.h
MAINTAINERS +1 -0
index ff0c3d7..42f3e54 100644
@@ -9320,6 +9320,7 @@ INTEL NNP-I PCI DRIVER
 M:	Guy Zadicario <[email protected]>
 S:	Supported
 F:	drivers/misc/intel-nnpi/
+F:	include/uapi/misc/intel_nnpi.h
 
 INTEL P-Unit IPC DRIVER
 M:	Zha Qipeng <[email protected]>
drivers/misc/intel-nnpi/Makefile +1 -1
index 3713d51..da2863b 100644
@@ -5,7 +5,7 @@
 
 obj-$(CONFIG_INTEL_NNPI) := intel_nnpi.o intel_nnpi_pcie.o
 
-intel_nnpi-y := device.o msg_scheduler.o hostres.o
+intel_nnpi-y := device.o msg_scheduler.o hostres.o host_chardev.o nnp_user.o
 
 intel_nnpi_pcie-y := nnp_pcie.o
 
drivers/misc/intel-nnpi/device.c +14 -0
index 60c5a94..0f98398 100644
@@ -8,6 +8,7 @@
 #include <linux/module.h>
 
 #include "device.h"
+#include "host_chardev.h"
 #include "msg_scheduler.h"
 
 static DEFINE_IDA(dev_ida);
@@ -99,6 +100,19 @@ void nnpdev_destroy(struct nnp_device *nnpdev)
 }
 EXPORT_SYMBOL(nnpdev_destroy);
 
+static int __init nnp_init(void)
+{
+	return nnp_init_host_interface();
+}
+subsys_initcall(nnp_init);
+
+static void __exit nnp_cleanup(void)
+{
+	nnp_release_host_interface();
+	/* dev_ida is already empty here - no point calling ida_destroy */
+}
+module_exit(nnp_cleanup);
+
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Intel(R) NNPI Framework");
 MODULE_AUTHOR("Intel Corporation");
drivers/misc/intel-nnpi/host_chardev.c new file +346 -0

346 lines not on this page · raw message

drivers/misc/intel-nnpi/host_chardev.h new file +12 -0
new file mode 100644
index 0000000..5812e0f
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (C) 2019-2021 Intel Corporation */
+
+#ifndef _NNPDRV_INFERENCE_H
+#define _NNPDRV_INFERENCE_H
+
+int nnp_init_host_interface(void);
+void nnp_release_host_interface(void);
+
+struct file *nnp_host_file_get(int host_fd);
+
+#endif
drivers/misc/intel-nnpi/nnp_user.c new file +131 -0

131 lines not on this page · raw message

drivers/misc/intel-nnpi/nnp_user.h new file +79 -0

79 lines not on this page · raw message

include/uapi/misc/intel_nnpi.h new file +150 -0

150 lines not on this page · raw message

Signature · 9 lines
1.8.3.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
#8 lore raw

PATCH 7/15 misc: nnpi: Disallow host memory resource access if no NNP-I devices exist

In reply to Guy Zadicario

There is no point allowing a user application to create host resources on a system that does not equipped with any NNP-I devices. Fail openning the nnpi_host character device when no NNP-I devices are attached.
It is OK to do that check without any synchronization as a race would not be an issue.

---
 drivers/misc/intel-nnpi/device.c       | 5 +++++
 drivers/misc/intel-nnpi/device.h       | 2 ++
 drivers/misc/intel-nnpi/host_chardev.c | 7 +++++++
 3 files changed, 14 insertions(+)
drivers/misc/intel-nnpi/device.c +5 -0
index 0f98398..a3c6a1d 100644
@@ -13,6 +13,11 @@
 
 static DEFINE_IDA(dev_ida);
 
+bool nnpdev_no_devices(void)
+{
+	return ida_is_empty(&dev_ida);
+}
+
 /**
  * nnpdev_init() - initialize NNP-I device structure.
  * @nnpdev: device to be initialized
drivers/misc/intel-nnpi/device.h +2 -0
index 7d7ef60..562bbc4 100644
@@ -36,6 +36,8 @@ struct nnp_device_ops {
 	int (*cmdq_write_mesg)(struct nnp_device *nnpdev, u64 *msg, u32 size);
 };
 
+bool nnpdev_no_devices(void);
+
 /*
  * Functions exported by the device framework module which are
  * called by the lower layer NNP-I device driver module
drivers/misc/intel-nnpi/host_chardev.c +7 -0
index 6048fda..fad5954 100644
@@ -217,6 +217,13 @@ static int host_open(struct inode *inode, struct file *f)
 	if (!is_host_file(f))
 		return -EINVAL;
 
+	/*
+	 * No point to serve host resource creation while no
+	 * NNP-I devices exist in the system.
+	 */
+	if (nnpdev_no_devices())
+		return -ENODEV;
+
 	user_info = kzalloc(sizeof(*user_info), GFP_KERNEL);
 	if (!user_info)
 		return -ENOMEM;
Signature · 9 lines
1.8.3.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
#9 lore raw

PATCH 8/15 misc: nnpi: Boot NNP-I device

In reply to Guy Zadicario

Boot the NNP-I device after the card is powered-on or reset. When the NNP-I card comes up, it's flashed BIOS starts running.
Since the persistent storage on the device is rather small, it needs to get its OS boot image from the host driver. So once the card's BIOS is running, the host driver loads and provides a boot image to the card BIOS, which uses it to run the Embedded Linux image and SW stack on the card.

---
 drivers/misc/intel-nnpi/Makefile    |   3 +-
 drivers/misc/intel-nnpi/bootimage.c | 246 ++++++++++++++++++++++++++++++
 drivers/misc/intel-nnpi/bootimage.h |  43 ++++++
 drivers/misc/intel-nnpi/device.c    | 291 ++++++++++++++++++++++++++++++++++++
 drivers/misc/intel-nnpi/device.h    |  77 ++++++++++
 drivers/misc/intel-nnpi/nnp_pcie.c  |  16 ++
 6 files changed, 675 insertions(+), 1 deletion(-)
 create mode 100644 drivers/misc/intel-nnpi/bootimage.c
 create mode 100644 drivers/misc/intel-nnpi/bootimage.h
drivers/misc/intel-nnpi/Makefile +2 -1
index da2863b..e46c89f 100644
@@ -5,7 +5,8 @@
 
 obj-$(CONFIG_INTEL_NNPI) := intel_nnpi.o intel_nnpi_pcie.o
 
-intel_nnpi-y := device.o msg_scheduler.o hostres.o host_chardev.o nnp_user.o
+intel_nnpi-y := device.o msg_scheduler.o hostres.o host_chardev.o nnp_user.o \
+                bootimage.o
 
 intel_nnpi_pcie-y := nnp_pcie.o
 
drivers/misc/intel-nnpi/bootimage.c new file +246 -0

246 lines not on this page · raw message

drivers/misc/intel-nnpi/bootimage.h new file +43 -0

43 lines not on this page · raw message

drivers/misc/intel-nnpi/device.c +291 -0

340 lines not on this page · raw message

drivers/misc/intel-nnpi/device.h +77 -0

111 lines not on this page · raw message

drivers/misc/intel-nnpi/nnp_pcie.c +16 -0
index 7aa9074..3840a53 100644
@@ -375,9 +375,25 @@ static int nnp_cmdq_flush(struct nnp_device *nnpdev)
 	return 0;
 }
 
+static int nnp_set_host_doorbell_value(struct nnp_device *nnpdev, u32 value)
+{
+	struct nnp_pci *nnp_pci = container_of(nnpdev, struct nnp_pci, nnpdev);
+
+	/*
+	 * The SELF_RESET bit is set only by the h/w layer,
+	 * do not allow higher layer to set it
+	 */
+	value &= ~NNP_HOST_DRV_REQUEST_SELF_RESET_MASK;
+
+	nnp_mmio_write(nnp_pci, ELBI_PCI_HOST_DOORBELL_VALUE, value);
+
+	return 0;
+}
+
 static struct nnp_device_ops nnp_device_ops = {
 	.cmdq_flush = nnp_cmdq_flush,
 	.cmdq_write_mesg = nnp_cmdq_write_mesg,
+	.set_host_doorbell_value = nnp_set_host_doorbell_value,
 };
 
 static void set_host_boot_state(struct nnp_pci *nnp_pci, int boot_state)
Signature · 9 lines
1.8.3.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
#10 lore raw

PATCH 9/15 misc: nnpi: Process device response messages

In reply to Guy Zadicario

The nnpdev_process_messages function handles all messages coming from a NNP-I device. Based on an opcode field attached to each message, it calls the correct response processing handler. The function is called from the NNP-I device driver, from a threaded interrupt handler, when responses arrive in the HW response queue.

---
 drivers/misc/intel-nnpi/device.c   | 136 +++++++++++++++++++++++++++++++++++++
 drivers/misc/intel-nnpi/device.h   |  10 +++
 drivers/misc/intel-nnpi/nnp_pcie.c |   3 +
 3 files changed, 149 insertions(+)
drivers/misc/intel-nnpi/device.c +136 -0
index f4fc975..2e6ab82 100644
@@ -23,6 +23,142 @@ bool nnpdev_no_devices(void)
 	return ida_is_empty(&dev_ida);
 }
 
+/**
+ * handle_bios_protocol() - process response coming from card's BIOS.
+ * @nnpdev: The nnp device
+ * @msgbuf: pointer to response message content
+ * @avail_qwords: number of 64-bit units available in @msgbuf
+ *
+ * IPC protocol with card's BIOS may have different response sizes.
+ * @avail_qwords is the number of 64-bit units available in @msgbuf buffer.
+ * If the actual response size is larger then available data in the buffer,
+ * the function returns 0 to indicate that this is a partial response. Otherwise
+ * the actual response size is returned (in units of qwords).
+ *
+ * Return: 0 if @msgbuf contains a partial response otherwise the number of
+ * qwords of the response in @msgbuf.
+ */
+static int handle_bios_protocol(struct nnp_device *nnpdev, const u64 *msgbuf,
+				int avail_qwords)
+{
+	int msg_size, msg_qwords;
+
+	msg_size = FIELD_GET(NNP_C2H_BIOS_PROTOCOL_TYPE_MASK, msgbuf[0]);
+
+	/* The +1 is because size field does not include header */
+	msg_qwords = DIV_ROUND_UP(msg_size, 8) + 1;
+
+	if (msg_qwords > avail_qwords)
+		return 0;
+
+	return msg_qwords;
+}
+
+typedef int (*response_handler)(struct nnp_device *nnpdev, const u64 *msgbuf,
+				int avail_qwords);
+
+static response_handler resp_handlers[NNP_IPC_C2H_OPCODE_LAST + 1] = {
+	[NNP_IPC_C2H_OP_BIOS_PROTOCOL] = handle_bios_protocol
+};
+
+/**
+ * nnpdev_process_messages() - process response messages from nnpi device
+ * @nnpdev: The nnp device
+ * @hw_msg: pointer to response message content
+ * @hw_nof_msg: number of 64-bit units available in hw_msg buffer.
+ *
+ * This function is called from the PCIe device driver when response messages
+ * are arrived in the HWQ. It is called in sequence, should not be re-entrant.
+ * The function may not block !
+ */
+void nnpdev_process_messages(struct nnp_device *nnpdev, u64 *hw_msg,
+			     unsigned int hw_nof_msg)
+{
+	int j = 0;
+	int msg_size;
+	u64 *msg;
+	unsigned int nof_msg;
+	bool fatal_protocol_error = false;
+
+	/* ignore any response if protocol error detected */
+	if ((nnpdev->state & NNP_DEVICE_PROTOCOL_ERROR) != 0)
+		return;
+
+	/*
+	 * If we have pending messages from previous round
+	 * copy the new messages to the pending list and process
+	 * the pending list.
+	 * otherwise process the messages received from HW directly
+	 */
+	msg = hw_msg;
+	nof_msg = hw_nof_msg;
+	if (nnpdev->response_num_msgs > 0) {
+		/*
+		 * Check to prevent response buffer overrun.
+		 * This should never happen since the buffer is twice
+		 * the size of the HW response queue. This check is
+		 * for safety and debug purposes.
+		 */
+		if (hw_nof_msg + nnpdev->response_num_msgs >=
+		    NNP_DEVICE_RESPONSE_BUFFER_LEN) {
+			dev_dbg(nnpdev->dev,
+				"device response buffer would overrun: %d + %d !!\n",
+				nnpdev->response_num_msgs, hw_nof_msg);
+			return;
+		}
+
+		memcpy(&nnpdev->response_buf[nnpdev->response_num_msgs], hw_msg,
+		       hw_nof_msg * sizeof(u64));
+		msg = nnpdev->response_buf;
+		nof_msg = nnpdev->response_num_msgs + hw_nof_msg;
+	}
+
+	/*
+	 * loop for each message
+	 */
+	do {
+		int op_code = FIELD_GET(NNP_C2H_OP_MASK, msg[j]);
+		response_handler handler;
+
+		/* opcodes above OP_BIOS_PROTOCOL are not yet supported */
+		if (op_code > NNP_IPC_C2H_OP_BIOS_PROTOCOL) {
+			fatal_protocol_error = true;
+			break;
+		}
+
+		/* dispatch the message request */
+		handler = resp_handlers[op_code];
+		if (!handler) {
+			/* Should not happen! */
+			dev_dbg(nnpdev->dev,
+				"Unknown response opcode received %d (0x%llx)\n",
+				op_code, msg[j]);
+			fatal_protocol_error = true;
+			break;
+		}
+
+		msg_size = (*handler)(nnpdev, &msg[j], (nof_msg - j));
+
+		j += msg_size;
+	} while (j < nof_msg || !msg_size);
+
+	if (fatal_protocol_error)
+		nnpdev->state |= NNP_DEVICE_PROTOCOL_ERROR;
+
+	/*
+	 * If unprocessed messages left, copy it to the pending messages buffer
+	 * for the next time this function will be called.
+	 */
+	if (j < nof_msg) {
+		memcpy(&nnpdev->response_buf[0], &msg[j],
+		       (nof_msg - j) * sizeof(u64));
+		nnpdev->response_num_msgs = nof_msg - j;
+	} else {
+		nnpdev->response_num_msgs = 0;
+	}
+}
+EXPORT_SYMBOL(nnpdev_process_messages);
+
 static void send_sysinfo_request_to_bios(struct nnp_device *nnpdev)
 {
 	u64 cmd[3];
drivers/misc/intel-nnpi/device.h +10 -0
index 3745f5c..b83f67a 100644
@@ -48,6 +48,9 @@
 #define NNP_DEVICE_CORRUPTED_BOOT_IMAGE BIT(30)
 #define NNP_DEVICE_ERROR_MASK        GENMASK(31, 16)
 
+#define NNP_DEVICE_RESPONSE_FIFO_LEN    16
+#define NNP_DEVICE_RESPONSE_BUFFER_LEN  (NNP_DEVICE_RESPONSE_FIFO_LEN * 2)
+
 /**
  * struct nnp_device - structure for NNP-I device info
  * @ops: device operations implemented by the underlying device driver
@@ -61,6 +64,8 @@
  * @lock: protects accesses to @state
  * @is_recovery_bios: true if device has booted from the recovery bios flash
  * @boot_image_loaded: true if boot image load has started
+ * @response_buf: buffer of device response messages arrived from "pci" layer.
+ * @response_num_msgs: number of qwords available in @response_buf
  * @bios_system_info_dma_addr: dma page allocated for bios system info.
  * @bios_system_info: virtual pointer to bios system info page
  * @bios_version_str: the device's started bios version string
@@ -83,6 +88,9 @@ struct nnp_device {
 	bool           is_recovery_bios;
 	bool           boot_image_loaded;
 
+	u64            response_buf[NNP_DEVICE_RESPONSE_BUFFER_LEN];
+	unsigned int   response_num_msgs;
+
 	dma_addr_t                  bios_system_info_dma_addr;
 	struct nnp_c2h_system_info  *bios_system_info;
 	char                        bios_version_str[NNP_BIOS_VERSION_LEN];
@@ -118,6 +126,8 @@ int nnpdev_init(struct nnp_device *nnpdev, struct device *dev,
 void nnpdev_destroy(struct nnp_device *nnpdev);
 void nnpdev_card_doorbell_value_changed(struct nnp_device *nnpdev,
 					u32 doorbell_val);
+void nnpdev_process_messages(struct nnp_device *nnpdev, u64 *hw_msg,
+			     unsigned int hw_nof_msg);
 
 /*
  * Framework internal functions (not exported)
drivers/misc/intel-nnpi/nnp_pcie.c +3 -0
index 3840a53..4c9b4c6 100644
@@ -150,6 +150,9 @@ static void nnp_process_commands(struct nnp_pci *nnp_pci)
 	response_pci_control |= FIELD_PREP(RESPQ_READ_PTR_MASK, read_pointer);
 	nnp_mmio_write(nnp_pci, ELBI_RESPONSE_PCI_CONTROL,
 		       response_pci_control);
+
+	nnpdev_process_messages(&nnp_pci->nnpdev, nnp_pci->response_buf,
+				avail_slots);
 }
 
 static void mask_all_interrupts(struct nnp_pci *nnp_pci)
Signature · 9 lines
1.8.3.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
#11 lore raw

PATCH 10/15 misc: nnpi: Query and verify device protocol

In reply to Guy Zadicario

Check that the card booted SW stack is compatible with the driver running on the host. The IPC protocol between the driver and cards's SW stack may change from time to time when releasing new versions of the card boot image.
When the card boots and signals to the host that it is booted and ready (through doorbell change interrupt), the driver sends it a "query version" command. In response, the device sends back a "query version reply" response with information of the IPC protocol version which it supports. Only when the version check passes, the device is considered booted and ready for operation.

If the version check fails, the device is put in error state. In order to recover from this error condition, a device reset is required (probably preceded by installation of a new, compatile boot image).

---
 drivers/misc/intel-nnpi/device.c | 145 +++++++++++++++++++++++++++++++++++++++
 drivers/misc/intel-nnpi/device.h |  17 +++++
 2 files changed, 162 insertions(+)
drivers/misc/intel-nnpi/device.c +145 -0
index 2e6ab82..7bcab563 100644
@@ -24,6 +24,119 @@ bool nnpdev_no_devices(void)
 }
 
 /**
+ * process_query_version_reply() - process a "query_version_reply" response
+ * @work: work struct of the calling work
+ *
+ * This function processes a "query_version_reply" response message from
+ * the card which is sent as reply to query_version command submitted
+ * earlier.
+ * The function checks that the IPC protocol version that is supported by the
+ * device matches the one supported by the driver. If there is no match the
+ * device state is put in error.
+ * There are two IPC protocol versions which are checked:
+ * 'protocol_version': is IPC protocol version of command and response messages
+ *         That are built (for commands) and processed by this kernel mode
+ *         driver. The protocol is defined in ipc_include/ipc_protocol.h
+ *         A mismatch is possible in cases that the device has booted with
+ *         a wrong/older version of the card boot image.
+ * 'chan_protocol_version': is IPC protocol of command and responses which are
+ *         supported by the device but are built and processed in user-space.
+ *         The structure of the commands and responses are mostly opaque to
+ *         the kernel mode driver. This separation allows to update the
+ *         device boot image and user-space library to support new sets
+ *         of commands without changing the kernel driver.
+ *         The restriction for such commands and responses is that the lowest
+ *         16-bits of the command/response are defined to include the
+ *         command/response opcode and the channel id.
+ *         The kernel driver should also know for each possible command and
+ *         response opcode the size of the message. This info is received
+ *         from the device within this "query_version_reply" response
+ *         encoded in the chan_resp_op_size and chan_cmd_op_size fields
+ *         of the response.
+ */
+static void process_query_version_reply(struct work_struct *work)
+{
+	struct query_version_work *query_version_work;
+	struct nnp_device *nnpdev;
+	u32 protocol_version;
+	u32 card_boot_state;
+	u32 val;
+
+	query_version_work =
+		container_of(work, struct query_version_work, work);
+	nnpdev = container_of(query_version_work, struct nnp_device,
+			      query_version_work);
+	protocol_version = NNP_IPC_PROTOCOL_VERSION;
+	card_boot_state = FIELD_GET(NNP_CARD_BOOT_STATE_MASK,
+				    nnpdev->card_doorbell_val);
+
+	nnpdev->protocol_version =
+		query_version_work->protocol_version;
+	nnpdev->chan_protocol_version =
+		query_version_work->chan_protocol_version;
+
+	/*
+	 * NOTE: The card firmware and host driver protocol version must
+	 * exactly match in the major and minor version components.
+	 * There is no backwards compatibility on the protocol!
+	 * When a device is put in a protocol version error state, the
+	 * user must install a matching device firmware and reset the device
+	 * in order to allow the device to function.
+	 */
+	if (NNP_VERSION_MAJOR(query_version_work->protocol_version) !=
+	    NNP_VERSION_MAJOR(protocol_version) ||
+	    NNP_VERSION_MINOR(query_version_work->protocol_version) !=
+	    NNP_VERSION_MINOR(protocol_version) ||
+	    query_version_work->chan_resp_op_size == 0) {
+		nnpdev_set_boot_state(nnpdev, NNP_DEVICE_FAILED_VERSION);
+		/* set host driver state in doorbell register */
+		val = FIELD_PREP(NNP_HOST_DRV_STATE_MASK,
+				 NNP_HOST_DRV_STATE_VERSION_ERROR);
+		nnpdev->ops->set_host_doorbell_value(nnpdev, val);
+	} else if (card_boot_state == NNP_CARD_BOOT_STATE_DRV_READY) {
+		nnpdev_set_boot_state(nnpdev, NNP_DEVICE_CARD_DRIVER_READY);
+	} else if (card_boot_state == NNP_CARD_BOOT_STATE_CARD_READY) {
+		/* Card driver finished initialization */
+		nnpdev_set_boot_state(nnpdev,
+				      NNP_DEVICE_CARD_DRIVER_READY |
+				      NNP_DEVICE_CARD_READY |
+				      NNP_DEVICE_CARD_ENABLED);
+	}
+
+	query_version_work->running = false;
+}
+
+static int handle_query_version_reply3(struct nnp_device *nnpdev,
+				       const u64 *msgbuf, int avail_qwords)
+{
+	int msg_qwords = 3; /* QUERY_VERSION_REPLY3 response len is 3 qwords */
+
+	if (avail_qwords < msg_qwords)
+		return 0;
+
+	/*
+	 * This should not happen, but if it does, just ignore the message
+	 * There is no fear in race condition on "running" flag as only
+	 * single version reply message should be processed after each
+	 * device reset.
+	 */
+	if (nnpdev->query_version_work.running)
+		return msg_qwords;
+
+	nnpdev->query_version_work.running = true;
+	nnpdev->query_version_work.protocol_version =
+		FIELD_GET(NNP_C2H_VERSION_REPLY_QW0_PROT_VER_MASK, msgbuf[0]);
+	nnpdev->query_version_work.chan_protocol_version =
+		FIELD_GET(NNP_C2H_VERSION_REPLY_QW0_CHAN_VER_MASK, msgbuf[0]);
+	nnpdev->query_version_work.chan_resp_op_size = msgbuf[1];
+	nnpdev->query_version_work.chan_cmd_op_size = msgbuf[2];
+
+	queue_work(nnpdev->wq, &nnpdev->query_version_work.work);
+
+	return msg_qwords;
+}
+
+/**
  * handle_bios_protocol() - process response coming from card's BIOS.
  * @nnpdev: The nnp device
  * @msgbuf: pointer to response message content
@@ -58,6 +171,7 @@ typedef int (*response_handler)(struct nnp_device *nnpdev, const u64 *msgbuf,
 				int avail_qwords);
 
 static response_handler resp_handlers[NNP_IPC_C2H_OPCODE_LAST + 1] = {
+	[NNP_IPC_C2H_OP_QUERY_VERSION_REPLY3] = handle_query_version_reply3,
 	[NNP_IPC_C2H_OP_BIOS_PROTOCOL] = handle_bios_protocol
 };
 
@@ -329,6 +443,18 @@ void nnpdev_set_boot_state(struct nnp_device *nnpdev, u32 mask)
 				"Unexpected error while loading boot image. rc=%d\n",
 				ret);
 	}
+
+	/* Handle transition to active state */
+	if (((state & NNP_DEVICE_CARD_DRIVER_READY) ||
+	     (state & NNP_DEVICE_CARD_READY)) &&
+	    !(prev_state & NNP_DEVICE_CARD_DRIVER_READY) &&
+	    !(prev_state & NNP_DEVICE_CARD_READY)) {
+		u32 val;
+
+		/* set host driver state to "Driver ready" */
+		val = FIELD_PREP(NNP_HOST_DRV_STATE_MASK, NNP_HOST_DRV_STATE_READY);
+		nnpdev->ops->set_host_doorbell_value(nnpdev, val);
+	}
 }
 
 /**
@@ -364,6 +490,7 @@ int nnpdev_init(struct nnp_device *nnpdev, struct device *dev,
 	 */
 	nnpdev->dev = dev;
 	nnpdev->ops = ops;
+	nnpdev->protocol_version = 0;
 
 	nnpdev->cmdq_sched = nnp_msched_create(nnpdev);
 	if (!nnpdev->cmdq_sched) {
@@ -397,6 +524,7 @@ int nnpdev_init(struct nnp_device *nnpdev, struct device *dev,
 
 	spin_lock_init(&nnpdev->lock);
 	nnpdev_boot_image_init(&nnpdev->boot_image);
+	INIT_WORK(&nnpdev->query_version_work.work, process_query_version_reply);
 
 	return 0;
 
@@ -426,6 +554,7 @@ static void doorbell_changed_handler(struct work_struct *work)
 	u32 error_state;
 	u32 doorbell_val = req->val;
 	struct nnp_device *nnpdev = req->nnpdev;
+	u64 query_cmd;
 
 	nnpdev->card_doorbell_val = doorbell_val;
 
@@ -466,6 +595,22 @@ static void doorbell_changed_handler(struct work_struct *work)
 		case NNP_CARD_BOOT_STATE_BIOS_FLASH_STARTED:
 			state = NNP_DEVICE_BIOS_UPDATE_STARTED;
 			break;
+		case NNP_CARD_BOOT_STATE_DRV_READY:
+		case NNP_CARD_BOOT_STATE_CARD_READY:
+			/* card is up - send "query_version" command */
+			query_cmd = FIELD_PREP(NNP_H2C_OP_MASK,
+					       NNP_IPC_H2C_OP_QUERY_VERSION);
+			if (nnp_msched_queue_msg(nnpdev->cmdq, query_cmd) ||
+			    nnp_msched_queue_sync(nnpdev->cmdq))
+				dev_err(nnpdev->dev, "Query version msg error\n");
+			break;
+
+		case NNP_CARD_BOOT_STATE_NOT_READY:
+			/* card is down reset the device boot and error state */
+			spin_lock(&nnpdev->lock);
+			nnpdev->state = 0;
+			spin_unlock(&nnpdev->lock);
+			break;
 		default:
 			break;
 		}
drivers/misc/intel-nnpi/device.h +17 -0
index b83f67a..63bc54d 100644
@@ -51,6 +51,15 @@
 #define NNP_DEVICE_RESPONSE_FIFO_LEN    16
 #define NNP_DEVICE_RESPONSE_BUFFER_LEN  (NNP_DEVICE_RESPONSE_FIFO_LEN * 2)
 
+struct query_version_work {
+	struct work_struct work;
+	u64 chan_resp_op_size;
+	u64 chan_cmd_op_size;
+	u16 protocol_version;
+	u16 chan_protocol_version;
+	bool running;
+};
+
 /**
  * struct nnp_device - structure for NNP-I device info
  * @ops: device operations implemented by the underlying device driver
@@ -71,9 +80,13 @@
  * @bios_version_str: the device's started bios version string
  * @bios_system_info_valid: true if @bios_system_info has been filled and valid
  * @state: current device boot state mask (see device state bits above)
+ * @protocol_version: version of host->card IPC protocol
+ * @chan_protocol_version: version of user-space->card IPC protocol
  * @curr_boot_state: last boot state field received from device doorbell reg
  * @card_doorbell_val: last received device doorbell register value.
  * @boot_image: boot image object used to boot the card
+ * @query_version_work: work struct used to schedule processing of version
+ *                      reply response message arrived from card.
  */
 struct nnp_device {
 	const struct nnp_device_ops *ops;
@@ -98,8 +111,12 @@ struct nnp_device {
 
 	u32            state;
 	u32            curr_boot_state;
+	unsigned short protocol_version;
+	unsigned short chan_protocol_version;
 	u32            card_doorbell_val;
 	struct image_info boot_image;
+
+	struct query_version_work query_version_work;
 };
 
 /**
Signature · 9 lines
1.8.3.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
#12 lore raw

PATCH 11/15 misc: nnpi: Create comm channel from app to device

In reply to Guy Zadicario

Establish bi-directional communication channels between user-mode processes and NNP-I devices. Each command channel object ("channel") holds a queue of messages from a single user-mode connection to a single NNP-I device, as well as a ring-buffer to hold response messages from this NNP-I device back to the user-mode process.

Messages to the NNP-I device are put by the channel into a command queue. Response messages coming back from the device are being routed to the intended channel's ring-buffer, where they are consumed by this channel's user-mode connection. Routing of messages to and from the device is done based on a channel's 10-bit unique id, which is included in the messages.

The interface for consuming responses from the ring-buffer and writing command messages into the msg_scheduler's command queue will be added in a future patch. This patch only adds the channel creation code and response message routing to the targeted channel.

When creating a "command channel", the user should give an open file descriptor to the /dev/nnpi_host device. This file descriptor associates the channel with a particular "nnp_user" object. The channel can only reference host resources created by that "user".

---
 drivers/misc/intel-nnpi/Makefile                   |   2 +-
 drivers/misc/intel-nnpi/cmd_chan.c                 | 314 +++++++++++++++++++++
 drivers/misc/intel-nnpi/cmd_chan.h                 |  72 +++++
 drivers/misc/intel-nnpi/device.c                   | 113 +++++++-
 drivers/misc/intel-nnpi/device.h                   |  17 +-
 drivers/misc/intel-nnpi/ipc_include/ipc_protocol.h |   3 +
 6 files changed, 516 insertions(+), 5 deletions(-)
 create mode 100644 drivers/misc/intel-nnpi/cmd_chan.c
 create mode 100644 drivers/misc/intel-nnpi/cmd_chan.h
drivers/misc/intel-nnpi/Makefile +1 -1
index e46c89f..b3bab2a 100644
@@ -6,7 +6,7 @@
 obj-$(CONFIG_INTEL_NNPI) := intel_nnpi.o intel_nnpi_pcie.o
 
 intel_nnpi-y := device.o msg_scheduler.o hostres.o host_chardev.o nnp_user.o \
-                bootimage.o
+                bootimage.o cmd_chan.o
 
 intel_nnpi_pcie-y := nnp_pcie.o
 
drivers/misc/intel-nnpi/cmd_chan.c new file +314 -0

314 lines not on this page · raw message

drivers/misc/intel-nnpi/cmd_chan.h new file +72 -0

72 lines not on this page · raw message

drivers/misc/intel-nnpi/device.c +110 -3

166 lines not on this page · raw message

drivers/misc/intel-nnpi/device.h +16 -1
index 63bc54d..9b6383e 100644
@@ -4,6 +4,8 @@
 #ifndef _NNPDRV_DEVICE_H
 #define _NNPDRV_DEVICE_H
 
+#include <linux/hashtable.h>
+#include <linux/idr.h>
 #include <linux/spinlock.h>
 #include <linux/workqueue.h>
 
@@ -70,11 +72,13 @@ struct query_version_work {
  * @cmdq: input queue to @cmdq_sched used to schedule driver internal commands
  *        to be sent to the device.
  * @wq: singlethread workqueue for processing device's response messages.
- * @lock: protects accesses to @state
+ * @lock: protects accesses to @state and @cmd_chan_hash
  * @is_recovery_bios: true if device has booted from the recovery bios flash
  * @boot_image_loaded: true if boot image load has started
  * @response_buf: buffer of device response messages arrived from "pci" layer.
  * @response_num_msgs: number of qwords available in @response_buf
+ * @cmd_chan_ida: allocate channel ids to be used in ipc protocol.
+ * @cmd_chan_hash: maps command channel id to its struct pointer.
  * @bios_system_info_dma_addr: dma page allocated for bios system info.
  * @bios_system_info: virtual pointer to bios system info page
  * @bios_version_str: the device's started bios version string
@@ -87,6 +91,9 @@ struct query_version_work {
  * @boot_image: boot image object used to boot the card
  * @query_version_work: work struct used to schedule processing of version
  *                      reply response message arrived from card.
+ * @ipc_chan_resp_op_size: holds response size for each possible channel
+ *                         response.
+ * @ipc_chan_cmd_op_size: holds command size for each possible channel command.
  */
 struct nnp_device {
 	const struct nnp_device_ops *ops;
@@ -104,6 +111,9 @@ struct nnp_device {
 	u64            response_buf[NNP_DEVICE_RESPONSE_BUFFER_LEN];
 	unsigned int   response_num_msgs;
 
+	struct ida cmd_chan_ida;
+	DECLARE_HASHTABLE(cmd_chan_hash, 6);
+
 	dma_addr_t                  bios_system_info_dma_addr;
 	struct nnp_c2h_system_info  *bios_system_info;
 	char                        bios_version_str[NNP_BIOS_VERSION_LEN];
@@ -117,6 +127,9 @@ struct nnp_device {
 	struct image_info boot_image;
 
 	struct query_version_work query_version_work;
+
+	u8   ipc_chan_resp_op_size[NNP_IPC_NUM_USER_OPS];
+	u8   ipc_chan_cmd_op_size[NNP_IPC_NUM_USER_OPS];
 };
 
 /**
@@ -151,4 +164,6 @@ void nnpdev_process_messages(struct nnp_device *nnpdev, u64 *hw_msg,
  */
 void nnpdev_set_boot_state(struct nnp_device *nnpdev, u32 mask);
 
+struct nnp_chan *nnpdev_find_channel(struct nnp_device *nnpdev, u16 chan_id);
+
 #endif
drivers/misc/intel-nnpi/ipc_include/ipc_protocol.h +3 -0
index 59b4a79..037c362 100644
@@ -11,6 +11,9 @@
 
 #define IPC_OP_MAX          BIT(6)
 #define NNP_IPC_OPCODE_MASK GENMASK(5, 0)
+#define NNP_IPC_MIN_USER_OP  32
+#define NNP_IPC_MAX_USER_OP  63
+#define NNP_IPC_NUM_USER_OPS (NNP_IPC_MAX_USER_OP - NNP_IPC_MIN_USER_OP + 1)
 
 #define NNP_MSG_SIZE(msg) (sizeof(msg) / sizeof(__le64))
 
Signature · 9 lines
1.8.3.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
#13 lore raw

PATCH 12/15 misc: nnpi: Route device response messages

In reply to Guy Zadicario

Route both types of messages coming from the NNP-I card - event report messages, which are handled by the driver, and command-response messages, which should be routed to a specific command channel object ("channel").

Event report messages are device-level messages which are not associated with a specific channel. They are typically initiated by the NNP-I card, to indicate an error, status change, or any event which is not a response to a message sent from a spcific channel. These event report messages are handled by the driver.

In contrast, command-response messages are associated with a specific channel, and are typically sent from the NNP-I card in response to a message sent from a channel to the card. These command-response messages are added to the intended channel ring-buffer for consumption by this channel.

The list of messages, of both types, coming from the card, is defined in ipc_include/ipc_c2h_events.h included in this patch.

---
 drivers/misc/intel-nnpi/cmd_chan.h                 |   6 +
 drivers/misc/intel-nnpi/device.c                   | 104 +++++++++++
 drivers/misc/intel-nnpi/device.h                   |   2 +
 .../misc/intel-nnpi/ipc_include/ipc_c2h_events.h   | 198 +++++++++++++++++++++
 4 files changed, 310 insertions(+)
 create mode 100644 drivers/misc/intel-nnpi/ipc_include/ipc_c2h_events.h
drivers/misc/intel-nnpi/cmd_chan.h +6 -0
index 8cb1a5e..2be88c6 100644
@@ -4,6 +4,7 @@
 #ifndef NNPDRV_CMD_CHAN_H
 #define NNPDRV_CMD_CHAN_H
 
+#include <linux/bitfield.h>
 #include <linux/circ_buf.h>
 #include <linux/hashtable.h>
 #include <linux/kref.h>
@@ -12,6 +13,7 @@
 #include <linux/spinlock.h>
 
 #include "device.h"
+#include "ipc_c2h_events.h"
 
 /**
  * struct nnp_chan - structure object for user<->device communication
@@ -21,6 +23,7 @@
  * @chan_id: the ipc channel id for this channel
  * @hash_node: node to include this object in list of channels
  *             hash is in (cmd_chan_hash in nnp_device).
+ * @card_critical_error_msg: last critical event report received from device
  * @get_device_events: true if device-level events received from card should
  *                     be sent over this channel to user.
  * @cmdq: message queue added to msg_scheduler, for user commands to be sent
@@ -43,6 +46,7 @@ struct nnp_chan {
 	struct nnp_device      *nnpdev;
 	u16                    chan_id;
 	struct hlist_node      hash_node;
+	u64                    card_critical_error_msg;
 	bool                   get_device_events;
 
 	struct nnp_msched_queue    *cmdq;
@@ -59,6 +63,8 @@ struct nnp_chan {
 	unsigned int      resp_lost;
 };
 
+#define chan_broken(chan) FIELD_GET(NNP_C2H_EVENT_REPORT_CODE_MASK, (chan)->card_critical_error_msg)
+
 struct nnp_chan *nnpdev_chan_create(struct nnp_device *nnpdev, int host_fd,
 				    unsigned int min_id, unsigned int max_id,
 				    bool get_device_events);
drivers/misc/intel-nnpi/device.c +104 -0

127 lines not on this page · raw message

drivers/misc/intel-nnpi/device.h +2 -0
index 9b6383e..c37f1da 100644
@@ -79,6 +79,7 @@ struct query_version_work {
  * @response_num_msgs: number of qwords available in @response_buf
  * @cmd_chan_ida: allocate channel ids to be used in ipc protocol.
  * @cmd_chan_hash: maps command channel id to its struct pointer.
+ * @waitq: used to wait for device response messages
  * @bios_system_info_dma_addr: dma page allocated for bios system info.
  * @bios_system_info: virtual pointer to bios system info page
  * @bios_version_str: the device's started bios version string
@@ -113,6 +114,7 @@ struct nnp_device {
 
 	struct ida cmd_chan_ida;
 	DECLARE_HASHTABLE(cmd_chan_hash, 6);
+	wait_queue_head_t waitq;
 
 	dma_addr_t                  bios_system_info_dma_addr;
 	struct nnp_c2h_system_info  *bios_system_info;
drivers/misc/intel-nnpi/ipc_include/ipc_c2h_events.h new file +198 -0

198 lines not on this page · raw message

Signature · 9 lines
1.8.3.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
#14 lore raw

PATCH 13/15 misc: nnpi: Expose command channel file interface

In reply to Guy Zadicario

Expose an anon file descriptor interface to a command channel object which allows user-space to send commands to the device by writing to that file as well as consume device response messages by reading the file.

When the file is released (closed), a channel shut-down sequence starts. First, a message is sent to the device notifying it that the channel is closing. Once the response to this message is received from the device, the command channel object is destroyed.

---
 drivers/misc/intel-nnpi/cmd_chan.c | 362 +++++++++++++++++++++++++++++++++++++
 drivers/misc/intel-nnpi/cmd_chan.h |  22 ++-
 drivers/misc/intel-nnpi/device.c   |  71 ++++++++
 3 files changed, 454 insertions(+), 1 deletion(-)
drivers/misc/intel-nnpi/cmd_chan.c +362 -0
index b5518e0..89ae604 100644
@@ -4,13 +4,16 @@
 #define pr_fmt(fmt)   KBUILD_MODNAME ": " fmt
 
 #include <linux/anon_inodes.h>
+#include <linux/bitfield.h>
 #include <linux/dev_printk.h>
 #include <linux/file.h>
 #include <linux/minmax.h>
+#include <linux/poll.h>
 #include <linux/slab.h>
 
 #include "cmd_chan.h"
 #include "host_chardev.h"
+#include "ipc_c2h_events.h"
 #include "ipc_protocol.h"
 #include "nnp_user.h"
 
@@ -52,6 +55,258 @@ static inline void respq_pop(struct nnp_chan *chan, void *buf, int count)
 	chan->respq.tail = (chan->respq.tail + count) & (chan->respq_size - 1);
 }
 
+static inline void respq_unpop(struct nnp_chan *chan, int count)
+{
+	chan->respq.tail = (chan->respq.tail - count) & (chan->respq_size - 1);
+}
+
+enum respq_state {
+	RESPQ_EMPTY = 0,
+	RESPQ_MSG_AVAIL,
+	RESPQ_DISCONNECTED
+};
+
+/**
+ * respq_state() - check if a response message is available to be popped
+ * @chan: the cmd_chan object
+ *
+ * Checks if new response message is available or channel has been destroyed.
+ *
+ * Return:
+ *  * RESPQ_EMPTY        - response queue is empty
+ *  * RESPQ_MSG_AVAIL    - a response message is available in the queue
+ *  * RESPQ_DISCONNECTED - the channel in a destroyed state
+ */
+static enum respq_state respq_state(struct nnp_chan *chan)
+{
+	bool ret;
+
+	mutex_lock(&chan->dev_mutex);
+	if (chan->state == NNP_CHAN_DESTROYED) {
+		mutex_unlock(&chan->dev_mutex);
+		return RESPQ_DISCONNECTED;
+	}
+
+	spin_lock(&chan->respq_lock);
+	/*
+	 * response messages are pushed into the respq ring-buffer by pushing
+	 * the size of the message (as u32) followed by message content.
+	 * So an entire message is available only if more than sizeof(u32)
+	 * bytes are available (there is no message with zero size).
+	 */
+	if (CIRC_CNT(chan->respq.head, chan->respq.tail, chan->respq_size) >
+	    sizeof(u32))
+		ret = RESPQ_MSG_AVAIL;
+	else
+		ret = RESPQ_EMPTY;
+	spin_unlock(&chan->respq_lock);
+
+	mutex_unlock(&chan->dev_mutex);
+
+	return ret;
+}
+
+static inline int is_cmd_chan_file(struct file *f);
+
+static int cmd_chan_file_release(struct inode *inode, struct file *f)
+{
+	struct nnp_chan *chan = f->private_data;
+	struct file *host_file;
+
+	if (!is_cmd_chan_file(f))
+		return -EINVAL;
+
+	nnp_chan_send_destroy(chan);
+
+	host_file = chan->host_file;
+	nnp_chan_put(chan);
+	fput(host_file);
+
+	return 0;
+}
+
+/**
+ * cmd_chan_file_read() - reads a single response message arrived from device
+ * @f: cmd_chan file descriptor
+ * @buf: buffer to receive the message
+ * @size: size of buf, must be at least 16 qwords (16 * sizeof(u64))
+ * @off: ignored.
+ *
+ * This function will block and wait until interrupted or a response
+ * message from device is available.
+ * When message(s) are available, it reads a single message, copy it to
+ * @buf and returns the message size.
+ * The given @buf and @size must be large enough to receive the largest
+ * possible response which is 16 qwords, otherwise -EINVAL is returned.
+ * The function returns the size of the received message, a return value
+ * of zero means that corrupted message has been detected and no more reads
+ * can be made from this channel.
+ *
+ * Return: if positive, the size in bytes of the read message.
+ *         zero, if a corrupted message has been detected.
+ *         error code otherwise
+ */
+static ssize_t cmd_chan_file_read(struct file *f, char __user *buf, size_t size,
+				  loff_t *off)
+{
+	struct nnp_chan *chan = f->private_data;
+	u64 msg[NNP_DEVICE_RESPONSE_FIFO_LEN];
+	enum respq_state state;
+	u32 msg_size;
+	int ret;
+
+	if (!is_cmd_chan_file(f))
+		return -EINVAL;
+
+	if (size < sizeof(msg))
+		return -EINVAL;
+
+	/*
+	 * wait for response message to be available, interrupted or channel
+	 * has been destroyed on us.
+	 */
+	ret = wait_event_interruptible(chan->resp_waitq,
+				       (state = respq_state(chan)) != RESPQ_EMPTY);
+	if (ret < 0)
+		return ret;
+
+	if (state == RESPQ_DISCONNECTED)
+		return -EPIPE;
+
+	spin_lock(&chan->respq_lock);
+	respq_pop(chan, &msg_size, sizeof(msg_size));
+	/*
+	 * Check msg_size does not overrun msg size.
+	 * This will never happen unless the response ring buffer got
+	 * corrupted in some way.
+	 * We detect it here for safety and return zero
+	 */
+	if (msg_size > sizeof(msg)) {
+		/*
+		 * unpop the bad size to let subsequent read attempts
+		 * to fail as well.
+		 */
+		respq_unpop(chan, sizeof(msg_size));
+		spin_unlock(&chan->respq_lock);
+		return 0;
+	}
+	respq_pop(chan, msg, msg_size);
+	spin_unlock(&chan->respq_lock);
+
+	if (copy_to_user(buf, msg, msg_size))
+		return -EFAULT;
+
+	return (ssize_t)msg_size;
+}
+
+/**
+ * cmd_chan_file_write() - schedule a command message to be sent to the device.
+ * @f: a cmd_chan file descriptor
+ * @buf: the command message content
+ * @size: size in bytes of the message, must be multiple of 8 and not larger
+ *        than 3 qwords.
+ * @off: ignored
+ *
+ * This function reads a command message from buffer and puts it in the
+ * channel's message queue to schedule it to be delivered to the device.
+ * The function returns when the message is copied to the message scheduler
+ * queue without waiting for it to be sent out.
+ * A valid command message size must be qword aligned and not larger than
+ * the maximum size the message scheduler support, which is 3 qwords.
+ *
+ * The function also validate the command content and fail if the chan_id
+ * field of the command header does not belong to the same channel of this
+ * file descriptor, or the command opcode is out of range, or the command
+ * size does not fit the size of this opcode.
+ *
+ * Return: the size of the message written or error code.
+ */
+static ssize_t cmd_chan_file_write(struct file *f, const char __user *buf,
+				   size_t size, loff_t *off)
+{
+	struct nnp_chan *chan = f->private_data;
+	u64 msg[MSG_SCHED_MAX_MSG_SIZE];
+	unsigned int chan_id, opcode;
+	unsigned int op;
+	int rc = 0;
+
+	if (!is_cmd_chan_file(f))
+		return -EINVAL;
+
+	/*
+	 * size must be positive, multiple of 8 bytes and
+	 * cannot exceed maximum message size
+	 */
+	if (!size || size > sizeof(msg) || (size &  0x7) != 0)
+		return -EINVAL;
+
+	if (copy_from_user(msg, buf, size))
+		return -EFAULT;
+
+	/*
+	 * Check chan_id, opcode and message size are valid
+	 */
+	opcode = FIELD_GET(NNP_H2C_CHAN_MSG_OP_MASK, msg[0]);
+	chan_id = FIELD_GET(NNP_H2C_CHAN_MSG_CHAN_ID_MASK, msg[0]);
+	if (chan_id != chan->chan_id)
+		return -EINVAL;
+	if (opcode < NNP_IPC_MIN_USER_OP)
+		return -EINVAL;
+	op = opcode - NNP_IPC_MIN_USER_OP;
+
+	mutex_lock(&chan->dev_mutex);
+	if (!chan->nnpdev) {
+		/* The device was removed */
+		mutex_unlock(&chan->dev_mutex);
+		return -EPIPE;
+	}
+	if (size != chan->nnpdev->ipc_chan_cmd_op_size[op] * 8) {
+		mutex_unlock(&chan->dev_mutex);
+		return -EINVAL;
+	}
+
+	if (!is_card_fatal_drv_event(chan_broken(chan)))
+		rc  = nnp_msched_queue_add_msg(chan->cmdq, msg, size / 8);
+	mutex_unlock(&chan->dev_mutex);
+
+	if (rc < 0)
+		return rc;
+
+	return size;
+}
+
+static unsigned int cmd_chan_file_poll(struct file *f, struct poll_table_struct *pt)
+{
+	struct nnp_chan *chan = f->private_data;
+	unsigned int mask = POLLOUT | POLLWRNORM;
+	enum respq_state state;
+
+	if (!is_cmd_chan_file(f))
+		return 0;
+
+	poll_wait(f, &chan->resp_waitq, pt);
+	state = respq_state(chan);
+	if (state != RESPQ_EMPTY)
+		mask |= POLLIN | POLLRDNORM;
+	if (state == RESPQ_DISCONNECTED)
+		mask |= POLLHUP;
+
+	return mask;
+}
+
+static const struct file_operations nnp_chan_fops = {
+	.owner = THIS_MODULE,
+	.release = cmd_chan_file_release,
+	.read = cmd_chan_file_read,
+	.write = cmd_chan_file_write,
+	.poll = cmd_chan_file_poll,
+};
+
+static inline int is_cmd_chan_file(struct file *f)
+{
+	return f->f_op == &nnp_chan_fops;
+}
+
 /**
  * nnpdev_chan_create() - creates a command channel object
  * @nnpdev: the device
@@ -119,6 +374,7 @@ struct nnp_chan *nnpdev_chan_create(struct nnp_device *nnpdev, int host_fd,
 	kref_init(&cmd_chan->ref);
 	cmd_chan->chan_id = chan_id;
 	cmd_chan->nnpdev = nnpdev;
+	cmd_chan->fd = -1;
 	cmd_chan->get_device_events = get_device_events;
 
 	cmd_chan->nnp_user = cmd_chan->host_file->private_data;
@@ -154,6 +410,17 @@ static void nnp_chan_release(struct kref *kref)
 
 	nnp_chan_disconnect(cmd_chan);
 
+	/*
+	 * If a chan file was created (through nnp_chan_create_file),
+	 * the host_file was already put when the file has released, otherwise
+	 * we put it here.
+	 * This is because we want to release host_file once the channel file
+	 * has been closed even though the channel object may continue to exist
+	 * until the card will send a respond that it was destroyed.
+	 */
+	if (cmd_chan->fd < 0)
+		fput(cmd_chan->host_file);
+
 	nnp_user_put(cmd_chan->nnp_user);
 
 	kfree(cmd_chan->respq_buf);
@@ -170,6 +437,99 @@ void nnp_chan_put(struct nnp_chan *cmd_chan)
 	kref_put(&cmd_chan->ref, nnp_chan_release);
 }
 
+int nnp_chan_create_file(struct nnp_chan *cmd_chan)
+{
+	/*
+	 * get refcount to the channel that will drop when
+	 * the file is released.
+	 */
+	nnp_chan_get(cmd_chan);
+
+	cmd_chan->fd = anon_inode_getfd("nnpi_chan", &nnp_chan_fops, cmd_chan,
+					O_RDWR | O_CLOEXEC);
+	if (cmd_chan->fd < 0)
+		nnp_chan_put(cmd_chan);
+
+	return cmd_chan->fd;
+}
+
+/**
+ * nnp_chan_set_destroyed() - atomically mark the channel "destroyed"
+ * @chan: the cmd_chan
+ *
+ * This function sets the command channel state to "destroyed" and returns
+ * the previous destroyed state.
+ * This function should be called once the channel has been destructed on the
+ * device and a "channel destroyed" response message arrived.
+ *
+ * Return: true if the channel was already marked destroyed.
+ */
+bool nnp_chan_set_destroyed(struct nnp_chan *chan)
+{
+	bool ret;
+
+	mutex_lock(&chan->dev_mutex);
+	ret = (chan->state == NNP_CHAN_DESTROYED);
+	chan->state = NNP_CHAN_DESTROYED;
+	mutex_unlock(&chan->dev_mutex);
+
+	wake_up_all(&chan->resp_waitq);
+
+	return ret;
+}
+
+/**
+ * nnp_chan_send_destroy() - sends a "destroy channel" command to device
+ * @chan: the cmd_chan to destroy.
+ *
+ * This function sends a command to the device to destroy a command channel,
+ * The channel object remains to exist, it will be dropped only when the device
+ * send back a "channel destroyed" response message.
+ * In case the device is in critical error state, we treat it as not
+ * functional, and the function will immediately drop the channel object without
+ * sending any command and will return with success.
+ *
+ * Return: 0 on success, error value otherwise.
+ */
+int nnp_chan_send_destroy(struct nnp_chan *chan)
+{
+	u64 cmd;
+	int ret = 0;
+	bool do_put = false;
+
+	mutex_lock(&chan->dev_mutex);
+	if (chan->state == NNP_CHAN_DESTROYED || !chan->nnpdev)
+		goto done;
+
+	cmd = FIELD_PREP(NNP_H2C_OP_MASK, NNP_IPC_H2C_OP_CHANNEL_OP);
+	cmd |= FIELD_PREP(NNP_H2C_CHANNEL_OP_CHAN_ID_MASK, chan->chan_id);
+	cmd |= FIELD_PREP(NNP_H2C_CHANNEL_OP_DESTROY_MASK, 1);
+
+	chan->event_msg = 0;
+
+	/*
+	 * If card is in critical state (or was during the channel lifetime)
+	 * we destroy the channel.
+	 * otherwise, we send a destroy command to card and will destroy when
+	 * the destroy reply arrives.
+	 */
+	if (is_card_fatal_drv_event(chan_broken(chan))) {
+		chan->state = NNP_CHAN_DESTROYED;
+		do_put = true;
+		goto done;
+	}
+
+	ret = nnp_msched_queue_msg(chan->cmdq, cmd);
+
+done:
+	mutex_unlock(&chan->dev_mutex);
+	if (do_put) {
+		wake_up_all(&chan->resp_waitq);
+		nnp_chan_put(chan);
+	}
+	return ret;
+}
+
 /**
  * nnp_chan_disconnect() - disconnect the channel from the NNP-I device object
  * @cmd_chan: the command channel object
@@ -194,8 +554,10 @@ void nnp_chan_disconnect(struct nnp_chan *cmd_chan)
 	spin_lock(&nnpdev->lock);
 	hash_del(&cmd_chan->hash_node);
 	spin_unlock(&nnpdev->lock);
+	cmd_chan->state = NNP_CHAN_DESTROYED;
 	mutex_unlock(&cmd_chan->dev_mutex);
 
+	wake_up_all(&cmd_chan->resp_waitq);
 	nnp_msched_queue_sync(cmd_chan->cmdq);
 	nnp_msched_queue_destroy(cmd_chan->cmdq);
 
drivers/misc/intel-nnpi/cmd_chan.h +21 -1
index 2be88c6..d60abf4 100644
@@ -16,6 +16,16 @@
 #include "ipc_c2h_events.h"
 
 /**
+ * enum nnp_chan_state - indicate special state of a command channel
+ * @NNP_CHAN_NORMAL: channel is in normal state.
+ * @NNP_CHAN_DESTROYED: channel should be treated as no-longer-exist on card.
+ */
+enum nnp_chan_state {
+	NNP_CHAN_NORMAL = 0,
+	NNP_CHAN_DESTROYED,
+};
+
+/**
  * struct nnp_chan - structure object for user<->device communication
  * @ref: refcount for this object
  * @nnpdev: the device this channel is connected to. May be NULL after device
@@ -23,9 +33,11 @@
  * @chan_id: the ipc channel id for this channel
  * @hash_node: node to include this object in list of channels
  *             hash is in (cmd_chan_hash in nnp_device).
+ * @event_msg: ipc event response received from device during create channel
  * @card_critical_error_msg: last critical event report received from device
  * @get_device_events: true if device-level events received from card should
  *                     be sent over this channel to user.
+ * @fd: file descriptor created for the channel (implements read/write)
  * @cmdq: message queue added to msg_scheduler, for user commands to be sent
  *        to the device.
  * @host_file: reference to opened "/dev/nnpi_host" object which defines the
@@ -33,7 +45,8 @@
  * @nnp_user: the nnp_user this channel belongs to.
  *             the channel can reference host resources created by this
  *             nnp_user object.
- * @dev_mutex: protects @nnpdev
+ * @dev_mutex: protects @nnpdev and @state
+ * @state: the current state of this channel.
  * @resp_waitq: waitqueue used for waiting for response messages be available.
  * @respq: circular buffer object that receive response messages from device.
  * @respq_lock: protects @respq
@@ -46,15 +59,18 @@ struct nnp_chan {
 	struct nnp_device      *nnpdev;
 	u16                    chan_id;
 	struct hlist_node      hash_node;
+	u64                    event_msg;
 	u64                    card_critical_error_msg;
 	bool                   get_device_events;
 
+	int fd;
 	struct nnp_msched_queue    *cmdq;
 	struct file                *host_file;
 	struct nnp_user_info       *nnp_user;
 
 	struct mutex      dev_mutex;
 	wait_queue_head_t resp_waitq;
+	enum nnp_chan_state state;
 
 	struct circ_buf   respq;
 	spinlock_t        respq_lock;
@@ -73,6 +89,10 @@ struct nnp_chan *nnpdev_chan_create(struct nnp_device *nnpdev, int host_fd,
 void nnp_chan_put(struct nnp_chan *cmd_chan);
 void nnp_chan_disconnect(struct nnp_chan *cmd_chan);
 
+int nnp_chan_create_file(struct nnp_chan *cmd_chan);
+int nnp_chan_send_destroy(struct nnp_chan *chan);
+bool nnp_chan_set_destroyed(struct nnp_chan *chan);
+
 int nnp_chan_add_response(struct nnp_chan *cmd_chan, u64 *hw_msg, u32 size);
 
 #endif
drivers/misc/intel-nnpi/device.c +71 -0

83 lines not on this page · raw message

Signature · 9 lines
1.8.3.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
#15 lore raw

PATCH 14/15 misc: nnpi: Create command channel from userspace

In reply to Guy Zadicario

Expose a character device for each NNP-I device (/dev/nnpi%d) with IOCTL interface. Using this character device, user-space can create a command channel object, through which it can send and receive messages to and from the device.

---
 drivers/misc/intel-nnpi/Makefile         |   2 +-
 drivers/misc/intel-nnpi/cmd_chan.c       |  11 +
 drivers/misc/intel-nnpi/cmd_chan.h       |   1 +
 drivers/misc/intel-nnpi/device.c         |  50 ++++-
 drivers/misc/intel-nnpi/device.h         |  11 +
 drivers/misc/intel-nnpi/device_chardev.c | 348 +++++++++++++++++++++++++++++++
 drivers/misc/intel-nnpi/device_chardev.h |  14 ++
 include/uapi/misc/intel_nnpi.h           |  43 ++++
 8 files changed, 478 insertions(+), 2 deletions(-)
 create mode 100644 drivers/misc/intel-nnpi/device_chardev.c
 create mode 100644 drivers/misc/intel-nnpi/device_chardev.h
drivers/misc/intel-nnpi/Makefile +1 -1
index b3bab2a..a294cf0c 100644
@@ -6,7 +6,7 @@
 obj-$(CONFIG_INTEL_NNPI) := intel_nnpi.o intel_nnpi_pcie.o
 
 intel_nnpi-y := device.o msg_scheduler.o hostres.o host_chardev.o nnp_user.o \
-                bootimage.o cmd_chan.o
+                bootimage.o cmd_chan.o device_chardev.o
 
 intel_nnpi_pcie-y := nnp_pcie.o
 
drivers/misc/intel-nnpi/cmd_chan.c +11 -0
index 89ae604..0ad281a 100644
@@ -557,6 +557,17 @@ void nnp_chan_disconnect(struct nnp_chan *cmd_chan)
 	cmd_chan->state = NNP_CHAN_DESTROYED;
 	mutex_unlock(&cmd_chan->dev_mutex);
 
+	/*
+	 * If the channel is not in critical state,
+	 * put it in critical state and wake any user
+	 * which might wait for the device.
+	 */
+	if (!chan_drv_fatal(cmd_chan)) {
+		cmd_chan->card_critical_error_msg = FIELD_PREP(NNP_C2H_EVENT_REPORT_CODE_MASK,
+							       NNP_IPC_ERROR_CHANNEL_KILLED);
+		wake_up_all(&nnpdev->waitq);
+	}
+
 	wake_up_all(&cmd_chan->resp_waitq);
 	nnp_msched_queue_sync(cmd_chan->cmdq);
 	nnp_msched_queue_destroy(cmd_chan->cmdq);
drivers/misc/intel-nnpi/cmd_chan.h +1 -0
index d60abf4..3eb5c1c 100644
@@ -80,6 +80,7 @@ struct nnp_chan {
 };
 
 #define chan_broken(chan) FIELD_GET(NNP_C2H_EVENT_REPORT_CODE_MASK, (chan)->card_critical_error_msg)
+#define chan_drv_fatal(chan) (is_card_fatal_drv_event(chan_broken(chan)))
 
 struct nnp_chan *nnpdev_chan_create(struct nnp_device *nnpdev, int host_fd,
 				    unsigned int min_id, unsigned int max_id,
drivers/misc/intel-nnpi/device.c +49 -1

98 lines not on this page · raw message

drivers/misc/intel-nnpi/device.h +11 -0
index c37f1da..e7e66d6 100644
@@ -4,8 +4,10 @@
 #ifndef _NNPDRV_DEVICE_H
 #define _NNPDRV_DEVICE_H
 
+#include <linux/cdev.h>
 #include <linux/hashtable.h>
 #include <linux/idr.h>
+#include <linux/list.h>
 #include <linux/spinlock.h>
 #include <linux/workqueue.h>
 
@@ -53,6 +55,9 @@
 #define NNP_DEVICE_RESPONSE_FIFO_LEN    16
 #define NNP_DEVICE_RESPONSE_BUFFER_LEN  (NNP_DEVICE_RESPONSE_FIFO_LEN * 2)
 
+#define NNP_MAX_CHANNEL_ID             1023  /* has 10 bits in ipc protocol */
+#define NNP_MAX_INF_CONTEXT_CHANNEL_ID 255   /* [0, 255] are reserved for inference contexts */
+
 struct query_version_work {
 	struct work_struct work;
 	u64 chan_resp_op_size;
@@ -92,6 +97,9 @@ struct query_version_work {
  * @boot_image: boot image object used to boot the card
  * @query_version_work: work struct used to schedule processing of version
  *                      reply response message arrived from card.
+ * @cdev: cdev object of NNP-I device char dev.
+ * @chardev: character device for this device
+ * @cdev_clients: list of opened struct file to the chardev of this device.
  * @ipc_chan_resp_op_size: holds response size for each possible channel
  *                         response.
  * @ipc_chan_cmd_op_size: holds command size for each possible channel command.
@@ -130,6 +138,9 @@ struct nnp_device {
 
 	struct query_version_work query_version_work;
 
+	struct cdev      cdev;
+	struct device    *chardev;
+	struct list_head cdev_clients;
 	u8   ipc_chan_resp_op_size[NNP_IPC_NUM_USER_OPS];
 	u8   ipc_chan_cmd_op_size[NNP_IPC_NUM_USER_OPS];
 };
drivers/misc/intel-nnpi/device_chardev.c new file +348 -0

348 lines not on this page · raw message

drivers/misc/intel-nnpi/device_chardev.h new file +14 -0
new file mode 100644
index 0000000..0db919d
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (C) 2019-2021 Intel Corporation */
+
+#ifndef _NNPDRV_DEVICE_CHARDEV_H
+#define _NNPDRV_DEVICE_CHARDEV_H
+
+#include "device.h"
+
+int nnpdev_cdev_create(struct nnp_device *nnpdev);
+void nnpdev_cdev_destroy(struct nnp_device *nnpdev);
+int nnpdev_cdev_class_init(void);
+void nnpdev_cdev_class_cleanup(void);
+
+#endif
include/uapi/misc/intel_nnpi.h +43 -0

49 lines not on this page · raw message

Signature · 9 lines
1.8.3.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
#16 lore raw

PATCH 15/15 misc: nnpi: Map host resources to device channel

In reply to Guy Zadicario

Provide an IOCTL interface for mapping and unmapping host resources to a channel, through the device's /dev/nnpi%d char device. The mapping gets a uniqueue ID and the page list of the host resource is transferred to the device. Later, commands to the device can reference the resource by the channel ID and map ID.

There is a special interface to map host resources which serve as host-to-card and card-to-host ring buffers. These ring buffers can be referenced later by the ring-buffer direction and index, rather than by a map ID.

---
 drivers/misc/intel-nnpi/cmd_chan.c       | 103 ++++++++
 drivers/misc/intel-nnpi/cmd_chan.h       |  37 ++-
 drivers/misc/intel-nnpi/device.c         |  58 +++-
 drivers/misc/intel-nnpi/device_chardev.c | 441 +++++++++++++++++++++++++++++++
 include/uapi/misc/intel_nnpi.h           | 111 ++++++++
 5 files changed, 748 insertions(+), 2 deletions(-)
drivers/misc/intel-nnpi/cmd_chan.c +103 -0
index 0ad281a..d98d02b 100644
@@ -383,6 +383,9 @@ struct nnp_chan *nnpdev_chan_create(struct nnp_device *nnpdev, int host_fd,
 	init_waitqueue_head(&cmd_chan->resp_waitq);
 	mutex_init(&cmd_chan->dev_mutex);
 
+	ida_init(&cmd_chan->hostres_map_ida);
+	hash_init(cmd_chan->hostres_hash);
+
 	/*
 	 * Add channel to the channel hash
 	 */
@@ -411,6 +414,11 @@ static void nnp_chan_release(struct kref *kref)
 	nnp_chan_disconnect(cmd_chan);
 
 	/*
+	 * cmd_chan->hostres_map_ida is empty at this point,
+	 * calling ida_destroy is not needed
+	 */
+
+	/*
 	 * If a chan file was created (through nnp_chan_create_file),
 	 * the host_file was already put when the file has released, otherwise
 	 * we put it here.
@@ -542,6 +550,9 @@ int nnp_chan_send_destroy(struct nnp_chan *chan)
 void nnp_chan_disconnect(struct nnp_chan *cmd_chan)
 {
 	struct nnp_device *nnpdev;
+	struct chan_hostres_map *hostres_map;
+	struct hlist_node *tmp;
+	int i;
 
 	mutex_lock(&cmd_chan->dev_mutex);
 	if (!cmd_chan->nnpdev) {
@@ -573,6 +584,32 @@ void nnp_chan_disconnect(struct nnp_chan *cmd_chan)
 	nnp_msched_queue_destroy(cmd_chan->cmdq);
 
 	ida_simple_remove(&nnpdev->cmd_chan_ida, cmd_chan->chan_id);
+
+	/*
+	 * Unmap ring buffers
+	 */
+	for (i = 0; i < NNP_IPC_MAX_CHANNEL_RB; i++) {
+		if (cmd_chan->h2c_rb_hostres_map[i]) {
+			nnp_hostres_unmap_device(cmd_chan->h2c_rb_hostres_map[i]);
+			cmd_chan->h2c_rb_hostres_map[i] = NULL;
+		}
+		if (cmd_chan->c2h_rb_hostres_map[i]) {
+			nnp_hostres_unmap_device(cmd_chan->c2h_rb_hostres_map[i]);
+			cmd_chan->c2h_rb_hostres_map[i] = NULL;
+		}
+	}
+
+	/*
+	 * Destroy all host resource maps
+	 */
+	mutex_lock(&cmd_chan->dev_mutex);
+	hash_for_each_safe(cmd_chan->hostres_hash, i, tmp, hostres_map, hash_node) {
+		hash_del(&hostres_map->hash_node);
+		ida_simple_remove(&cmd_chan->hostres_map_ida, hostres_map->id);
+		nnp_hostres_unmap_device(hostres_map->hostres_map);
+		kfree(hostres_map);
+	}
+	mutex_unlock(&cmd_chan->dev_mutex);
 }
 
 static int resize_respq(struct nnp_chan *cmd_chan)
@@ -685,3 +722,69 @@ int nnp_chan_add_response(struct nnp_chan *cmd_chan, u64 *hw_msg, u32 size)
 
 	return ret;
 }
+
+int nnp_chan_set_ringbuf(struct nnp_chan *chan, bool h2c, unsigned int id,
+			 struct nnpdev_mapping *hostres_map)
+{
+	if (id >= NNP_IPC_MAX_CHANNEL_RB)
+		return -EINVAL;
+
+	mutex_lock(&chan->dev_mutex);
+	if (h2c) {
+		if (chan->h2c_rb_hostres_map[id])
+			nnp_hostres_unmap_device(chan->h2c_rb_hostres_map[id]);
+		chan->h2c_rb_hostres_map[id] = hostres_map;
+	} else {
+		if (chan->c2h_rb_hostres_map[id])
+			nnp_hostres_unmap_device(chan->c2h_rb_hostres_map[id]);
+		chan->c2h_rb_hostres_map[id] = hostres_map;
+	}
+	mutex_unlock(&chan->dev_mutex);
+
+	return 0;
+}
+
+static struct chan_hostres_map *find_map(struct nnp_chan *chan, unsigned int map_id)
+{
+	struct chan_hostres_map *hostres_map;
+
+	lockdep_assert_held(&chan->dev_mutex);
+
+	hash_for_each_possible(chan->hostres_hash, hostres_map, hash_node,
+			       map_id)
+		if (hostres_map->id == map_id)
+			return hostres_map;
+
+	return NULL;
+}
+
+struct chan_hostres_map *nnp_chan_find_map(struct nnp_chan *chan, unsigned int map_id)
+{
+	struct chan_hostres_map *map;
+
+	mutex_lock(&chan->dev_mutex);
+	map = find_map(chan, map_id);
+	mutex_unlock(&chan->dev_mutex);
+
+	return map;
+}
+
+int nnp_chan_unmap_hostres(struct nnp_chan *chan, unsigned int map_id)
+{
+	struct chan_hostres_map *hostres_map;
+
+	mutex_lock(&chan->dev_mutex);
+	hostres_map = find_map(chan, map_id);
+	if (!hostres_map) {
+		mutex_unlock(&chan->dev_mutex);
+		return -ENXIO;
+	}
+	hash_del(&hostres_map->hash_node);
+	ida_simple_remove(&chan->hostres_map_ida, hostres_map->id);
+	nnp_hostres_unmap_device(hostres_map->hostres_map);
+	mutex_unlock(&chan->dev_mutex);
+
+	kfree(hostres_map);
+
+	return 0;
+}
drivers/misc/intel-nnpi/cmd_chan.h +36 -1
index 3eb5c1c..2913595 100644
@@ -19,10 +19,13 @@
  * enum nnp_chan_state - indicate special state of a command channel
  * @NNP_CHAN_NORMAL: channel is in normal state.
  * @NNP_CHAN_DESTROYED: channel should be treated as no-longer-exist on card.
+ * @NNP_CHAN_RB_OP_IN_FLIGHT: a ring-buffer create or destroy command has been
+ *                            sent to device and response did not yet arrived.
  */
 enum nnp_chan_state {
 	NNP_CHAN_NORMAL = 0,
 	NNP_CHAN_DESTROYED,
+	NNP_CHAN_RB_OP_IN_FLIGHT,
 };
 
 /**
@@ -45,14 +48,20 @@ enum nnp_chan_state {
  * @nnp_user: the nnp_user this channel belongs to.
  *             the channel can reference host resources created by this
  *             nnp_user object.
- * @dev_mutex: protects @nnpdev and @state
+ * @dev_mutex: protects @nnpdev, @state, @hostres_hash and @hostres_map_ida
  * @state: the current state of this channel.
+ * @hostres_map_ida: generate ipc ids for hostres mapping
+ * @hostres_hash: hash table to store all host resource mapping, key is ipc id
  * @resp_waitq: waitqueue used for waiting for response messages be available.
  * @respq: circular buffer object that receive response messages from device.
  * @respq_lock: protects @respq
  * @respq_buf: buffer space allocated for circular response buffer.
  * @respq_size: current allocated size of circular response buffer.
  * @resp_lost: number of response messages lost due to response buffer full.
+ * @h2c_rb_hostres_map: host resource mapping used for each host-to-card ring buffer
+ *                  There may be up to 2 such ring buffers, both can be NULL.
+ * @c2h_rb_hostres_map: host resource mapping used for each card-to-host ring buffer
+ *                  There may be up to 2 such ring buffers, both can be NULL.
  */
 struct nnp_chan {
 	struct kref            ref;
@@ -72,11 +81,32 @@ struct nnp_chan {
 	wait_queue_head_t resp_waitq;
 	enum nnp_chan_state state;
 
+	struct ida        hostres_map_ida;
+	DECLARE_HASHTABLE(hostres_hash, 6);
+
 	struct circ_buf   respq;
 	spinlock_t        respq_lock;
 	char              *respq_buf;
 	unsigned int      respq_size;
 	unsigned int      resp_lost;
+
+	struct nnpdev_mapping *h2c_rb_hostres_map[NNP_IPC_MAX_CHANNEL_RB];
+	struct nnpdev_mapping *c2h_rb_hostres_map[NNP_IPC_MAX_CHANNEL_RB];
+};
+
+/**
+ * struct chan_hostres_map - holds host resource mapping to channel
+ *
+ * @id: ipc map id of the mapping
+ * @hash_node: node to include this mapping in @hostres_hash of nnpdrv_cmd_chan
+ * @hostres_map: the host resource mapping object
+ * @event_msg: device response to the map create request
+ */
+struct chan_hostres_map {
+	unsigned int           id;
+	struct hlist_node      hash_node;
+	struct nnpdev_mapping  *hostres_map;
+	u64                    event_msg;
 };
 
 #define chan_broken(chan) FIELD_GET(NNP_C2H_EVENT_REPORT_CODE_MASK, (chan)->card_critical_error_msg)
@@ -96,4 +126,9 @@ struct nnp_chan *nnpdev_chan_create(struct nnp_device *nnpdev, int host_fd,
 
 int nnp_chan_add_response(struct nnp_chan *cmd_chan, u64 *hw_msg, u32 size);
 
+int nnp_chan_set_ringbuf(struct nnp_chan *chan, bool h2c, unsigned int id,
+			 struct nnpdev_mapping *hostres_map);
+
+struct chan_hostres_map *nnp_chan_find_map(struct nnp_chan *chan, unsigned int map_id);
+int nnp_chan_unmap_hostres(struct nnp_chan *chan, unsigned int map_id);
 #endif
drivers/misc/intel-nnpi/device.c +57 -1

83 lines not on this page · raw message

drivers/misc/intel-nnpi/device_chardev.c +441 -0

465 lines not on this page · raw message

include/uapi/misc/intel_nnpi.h +111 -0

123 lines not on this page · raw message

Signature · 9 lines
1.8.3.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
maintainer
#17 lore raw
In reply to Guy Zadicario
Quoted 26 lines · Guy Zadicario

Hi,

The following series is a driver for a new PCIe device from Intel named NNP-I (Nirvana Neural Processor for Inference). NNP-I is a PCIe connected compute device used for acceleration of AI deep learning inference applications in the data-center.

The reason that this driver should be in the kernel is that it aims to serve multiple users and user-space applications which might share the same NNP-I card. Workloads from multiple applications can be processed simultanously by the NNP-I card if enough compute resources exist.

Overview of the NNP-I device, driver structure and ABIs used in the driver is in patch#1, which adds the info as a document as it might be a useful info for anyone trying to understand the driver even past review.

In order to ease the review process, there will be multiple series for the entire driver code. This is the first series, and it implements everything necessary to initialize the NNP-I device and allow a user-space inference application to use it. Other features, which are mostly related to maintenance, device status visibility and error-handling, will be submitted on the next stage.

A basic user-space library and test application which illustrates the flow of an NNP-I inference application can be found here: https://github.com/IntelAI/nnpi-host
(This series is enough for the test application to run)

Quoted 6 lines

This patchset has gone through internal review inside Intel, the summary of the change log from the internal review follows.

I would appreciate any feedback, questions or comments to this series.

Changes in v22:

Why is "v22" not in the [PATCH...] part of the subjects here?

And has there really been 21 other series posted to lkml for this?

thanks,

greg k-h

#18 lore raw
Quoted 30 lines · Greg KH
Quoted 29 lines · Guy Zadicario

Hi,

The following series is a driver for a new PCIe device from Intel named NNP-I (Nirvana Neural Processor for Inference). NNP-I is a PCIe connected compute device used for acceleration of AI deep learning inference applications in the data-center.

The reason that this driver should be in the kernel is that it aims to serve multiple users and user-space applications which might share the same NNP-I card. Workloads from multiple applications can be processed simultanously by the NNP-I card if enough compute resources exist.

Overview of the NNP-I device, driver structure and ABIs used in the driver is in patch#1, which adds the info as a document as it might be a useful info for anyone trying to understand the driver even past review.

In order to ease the review process, there will be multiple series for the entire driver code. This is the first series, and it implements everything necessary to initialize the NNP-I device and allow a user-space inference application to use it. Other features, which are mostly related to maintenance, device status visibility and error-handling, will be submitted on the next stage.

A basic user-space library and test application which illustrates the flow of an NNP-I inference application can be found here: https://github.com/IntelAI/nnpi-host
(This series is enough for the test application to run)

This patchset has gone through internal review inside Intel, the summary of the change log from the internal review follows.

Quoted 8 lines
Quoted 3 lines

I would appreciate any feedback, questions or comments to this series.

Changes in v22:

Why is "v22" not in the [PATCH...] part of the subjects here?

And has there really been 21 other series posted to lkml for this?

This is the first post to lkml. The first 21 versions was sent for internal review in Intel only.
I could possibly remove the long change log from the cover letter.

Thanks,
Guy.
---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.

maintainer
#19 lore raw
Quoted 5 lines · Guy Zadicario

This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.

Now deleted, this is not compatible with kernel development :(