Skip to contentSkip to conversation

scsi: ufs: add ioctl interface for query request

PATCH Superseded Yaniv Gardi 2 participants 4 messages Oct 7, 2015

Subsystems (3)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 scsi: ufs: add ioctl interface for query request

This patch exposes the ioctl interface for UFS driver via SCSI device ioctl interface. As of now UFS driver would provide the ioctl for query interface to connected UFS device.

---
 drivers/scsi/ufs/ufs.h        |  53 +++-------
 drivers/scsi/ufs/ufshcd.c     | 219 +++++++++++++++++++++++++++++++++++++++++-
 include/scsi/scsi.h           |   1 +
 include/uapi/scsi/Kbuild      |   1 +
 include/uapi/scsi/ufs/Kbuild  |   3 +
 include/uapi/scsi/ufs/ioctl.h |  58 +++++++++++
 include/uapi/scsi/ufs/ufs.h   |  67 +++++++++++++
 7 files changed, 359 insertions(+), 43 deletions(-)
 create mode 100644 include/uapi/scsi/ufs/Kbuild
 create mode 100644 include/uapi/scsi/ufs/ioctl.h
 create mode 100644 include/uapi/scsi/ufs/ufs.h
drivers/scsi/ufs/ufs.h +11 -42
index 05e0f23..eab739d 100644
@@ -38,6 +38,7 @@
 
 #include <linux/mutex.h>
 #include <linux/types.h>
+#include <scsi/ufs/ufs.h>
 
 #define MAX_CDB_SIZE	16
 #define GENERAL_UPIU_REQUEST_SIZE 32
@@ -72,6 +73,16 @@ enum {
 	UFS_UPIU_RPMB_WLUN		= 0xC4,
 };
 
+/**
+ * ufs_is_valid_unit_desc_lun - checks if the given LUN has a unit descriptor
+ * @lun: LU number to check
+ * @return: true if the lun has a matching unit descriptor, false otherwise
+ */
+static inline bool ufs_is_valid_unit_desc_lun(u8 lun)
+{
+	return lun == UFS_UPIU_RPMB_WLUN || (lun < UFS_UPIU_MAX_GENERAL_LUN);
+}
+
 /*
  * UFS Protocol Information Unit related definitions
  */
@@ -127,35 +138,6 @@ enum {
 	UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST          = 0x81,
 };
 
-/* Flag idn for Query Requests*/
-enum flag_idn {
-	QUERY_FLAG_IDN_FDEVICEINIT      = 0x01,
-	QUERY_FLAG_IDN_PWR_ON_WPE	= 0x03,
-	QUERY_FLAG_IDN_BKOPS_EN         = 0x04,
-};
-
-/* Attribute idn for Query requests */
-enum attr_idn {
-	QUERY_ATTR_IDN_ACTIVE_ICC_LVL	= 0x03,
-	QUERY_ATTR_IDN_BKOPS_STATUS	= 0x05,
-	QUERY_ATTR_IDN_EE_CONTROL	= 0x0D,
-	QUERY_ATTR_IDN_EE_STATUS	= 0x0E,
-};
-
-/* Descriptor idn for Query requests */
-enum desc_idn {
-	QUERY_DESC_IDN_DEVICE		= 0x0,
-	QUERY_DESC_IDN_CONFIGURAION	= 0x1,
-	QUERY_DESC_IDN_UNIT		= 0x2,
-	QUERY_DESC_IDN_RFU_0		= 0x3,
-	QUERY_DESC_IDN_INTERCONNECT	= 0x4,
-	QUERY_DESC_IDN_STRING		= 0x5,
-	QUERY_DESC_IDN_RFU_1		= 0x6,
-	QUERY_DESC_IDN_GEOMETRY		= 0x7,
-	QUERY_DESC_IDN_POWER		= 0x8,
-	QUERY_DESC_IDN_MAX,
-};
-
 enum desc_header_offset {
 	QUERY_DESC_LENGTH_OFFSET	= 0x00,
 	QUERY_DESC_DESC_TYPE_OFFSET	= 0x01,
@@ -278,19 +260,6 @@ enum bkops_status {
 	BKOPS_STATUS_MAX		 = BKOPS_STATUS_CRITICAL,
 };
 
-/* UTP QUERY Transaction Specific Fields OpCode */
-enum query_opcode {
-	UPIU_QUERY_OPCODE_NOP		= 0x0,
-	UPIU_QUERY_OPCODE_READ_DESC	= 0x1,
-	UPIU_QUERY_OPCODE_WRITE_DESC	= 0x2,
-	UPIU_QUERY_OPCODE_READ_ATTR	= 0x3,
-	UPIU_QUERY_OPCODE_WRITE_ATTR	= 0x4,
-	UPIU_QUERY_OPCODE_READ_FLAG	= 0x5,
-	UPIU_QUERY_OPCODE_SET_FLAG	= 0x6,
-	UPIU_QUERY_OPCODE_CLEAR_FLAG	= 0x7,
-	UPIU_QUERY_OPCODE_TOGGLE_FLAG	= 0x8,
-};
-
 /* Query response result code */
 enum {
 	QUERY_RESULT_SUCCESS                    = 0x00,
drivers/scsi/ufs/ufshcd.c +218 -1

249 lines not on this page · raw message

include/scsi/scsi.h +1 -0
index e0a3398..fada160 100644
@@ -284,6 +284,7 @@ static inline int scsi_is_wlun(u64 lun)
  * Here are some scsi specific ioctl commands which are sometimes useful.
  *
  * Note that include/linux/cdrom.h also defines IOCTL 0x5300 - 0x5395
+ * include/uapi/scsi/ufs/ioctl.h defines 0x5388
  */
 
 /* Used to obtain PUN and LUN info.  Conflicts with CDROMAUDIOBUFSIZ */
include/uapi/scsi/Kbuild +1 -0
index 75746d5..d404525 100644
@@ -1,5 +1,6 @@
 # UAPI Header export list
 header-y += fc/
+header-y += ufs/
 header-y += scsi_bsg_fc.h
 header-y += scsi_netlink.h
 header-y += scsi_netlink_fc.h
include/uapi/scsi/ufs/Kbuild new file +3 -0
new file mode 100644
index 0000000..cc3ef20
@@ -0,0 +1,3 @@
+# UAPI Header export list
+header-y += ioctl.h
+header-y += ufs.h
include/uapi/scsi/ufs/ioctl.h new file +58 -0

58 lines not on this page · raw message

include/uapi/scsi/ufs/ufs.h new file +67 -0

67 lines not on this page · raw message

Signature · 1 line
1.8.5.2
#1 lore raw

drivers/scsi/ufs/ufshcd.c line 95

Quoted 4 lines · On Wednesday 07 October 2015 10:54:03 Yaniv Gardi wrote:
drivers/scsi/ufs/ufshcd.c +3 -0
+/* IOCTL opcode for command - ufs set device read only */
+#define UFS_IOCTL_BLKROSET      BLKROSET
+

What is this for? Can't you just use the normal BLKROSET definition in user space?

drivers/scsi/ufs/ufshcd.c out: · line 5079

Quoted 6 lines
drivers/scsi/ufs/ufshcd.c +6 -0
+
+       ioctl_data = kzalloc(sizeof(struct ufs_ioctl_query_data), GFP_KERNEL);
+       if (!ioctl_data) {
+               err = -ENOMEM;
+               goto out;
+       }

ufs_ioctl_query_data is short enough to just be on the stack.

drivers/scsi/ufs/ufshcd.c out: · line 5119

Quoted 10 lines
drivers/scsi/ufs/ufshcd.c +10 -0
+		}
+		length = min_t(int, QUERY_DESC_MAX_SIZE,
+				ioctl_data->buf_size);
+		desc = kzalloc(length, GFP_KERNEL);
+		if (!desc) {
+			dev_err(hba->dev, "%s: Failed allocating %d bytes\n",
+					__func__, length);
+			err = -ENOMEM;
+			goto out_release_mem;
+		}

Better check for a maximum length as well, not just a minimum length. For overly long requests, just return an error without trying the kzalloc.

You can also use memdup_user to avoid the extra zeroing and and simplify the calling code.

Quoted 9 lines
drivers/scsi/ufs/ufshcd.c +9 -0
+/**
+ * ufshcd_ioctl - ufs ioctl callback registered in scsi_host
+ * @dev: scsi device required for per LUN queries
+ * @cmd: command opcode
+ * @buffer: user space buffer for transferring data
+ *
+ * Supported commands:
+ * UFS_IOCTL_QUERY
+ */

drivers/scsi/ufs/ufshcd.c out: · line 5243

Quoted 10 lines
drivers/scsi/ufs/ufshcd.c +10 -0
+static int ufshcd_ioctl(struct scsi_device *dev, int cmd, void __user *buffer)
+{
+	struct ufs_hba *hba = shost_priv(dev->host);
+	int err = 0;
+
+	BUG_ON(!hba);
+	if (!buffer) {
+		dev_err(hba->dev, "%s: User buffer is NULL!\n", __func__);
+		return -EINVAL;
+	}

Don't print an error here, you don't want users to be able to flood syslog that easily, and the program that caused the error does not see the log anyway.

Quoted 6 lines
drivers/scsi/ufs/ufshcd.c +6 -0
+
+	switch (cmd) {
+	case UFS_IOCTL_QUERY:
+		pm_runtime_get_sync(hba->dev);
+		err = ufshcd_query_ioctl(hba, ufshcd_scsi_to_upiu_lun(dev->lun),
+				buffer);

drivers/scsi/ufs/ufshcd.c out: · line 5259

Quoted 10 lines
drivers/scsi/ufs/ufshcd.c +10 -0
+		pm_runtime_put_sync(hba->dev);
+		break;
+	case UFS_IOCTL_BLKROSET:
+		err = -ENOIOCTLCMD;
+		break;
+	default:
+		err = -EINVAL;
+		dev_err(hba->dev, "%s: Illegal ufs-IOCTL cmd %d\n", __func__,
+				cmd);
+		break;

same here.

Quoted 10 lines
drivers/scsi/ufs/ufshcd.c +6 -0
+	}
+
+	return err;
+}
+
+/**
  * ufshcd_async_scan - asynchronous execution for probing hba
  * @data: data pointer to pass to this function
  * @cookie: cookie data

drivers/scsi/ufs/ufshcd.c static struct scsi_host_template ufshcd_driver_template = { · line 5328

Quoted 7 lines
drivers/scsi/ufs/ufshcd.c +1 -0
@@ -5106,6 +5322,7 @@ static struct scsi_host_template ufshcd_driver_template = {
 	.eh_device_reset_handler = ufshcd_eh_device_reset_handler,
 	.eh_host_reset_handler   = ufshcd_eh_host_reset_handler,
 	.eh_timed_out		= ufshcd_eh_timed_out,
+	.ioctl			= ufshcd_ioctl,
 	.this_id		= -1,
 	.sg_tablesize		= SG_ALL,
 	.cmd_per_lun		= UFSHCD_CMD_PER_LUN,

The ioctl data is compatible between 32-bit and 64-bit user space, so better add a .compat_ioctl line right away to make this work on 64-bit architectures with 32-bit user space.

include/uapi/scsi/ufs/ioctl.h line 10

Quoted 6 lines
include/uapi/scsi/ufs/ioctl.h new file +6 -0
+
+/*
+ *  IOCTL opcode for ufs queries has the following opcode after
+ *  SCSI_IOCTL_GET_PCI
+ */
+#define UFS_IOCTL_QUERY			0x5388

Use _IOWR() to define that number with the correct argument length

Quoted 35 lines
include/uapi/scsi/ufs/ioctl.h new file +35 -0
+/**
+ * struct ufs_ioctl_query_data - used to transfer data to and from
+ * user via ioctl
+ * @opcode: type of data to query (descriptor/attribute/flag)
+ * @idn: id of the data structure
+ * @buf_size: number of allocated bytes/data size on return
+ * @buffer: data location
+ *
+ * Received: buffer and buf_size (available space for transferred data)
+ * Submitted: opcode, idn, length, buf_size
+ */
+struct ufs_ioctl_query_data {
+	/*
+	 * User should select one of the opcode defined in "enum query_opcode".
+	 * Please check include/uapi/scsi/ufs/ufs.h for the definition of it.
+	 * Note that only UPIU_QUERY_OPCODE_READ_DESC,
+	 * UPIU_QUERY_OPCODE_READ_ATTR & UPIU_QUERY_OPCODE_READ_FLAG are
+	 * supported as of now. All other query_opcode would be considered
+	 * invalid.
+	 * As of now only read query operations are supported.
+	 */
+	__u32 opcode;
+	/*
+	 * User should select one of the idn from "enum flag_idn" or "enum
+	 * attr_idn" or "enum desc_idn" based on whether opcode above is
+	 * attribute, flag or descriptor.
+	 * Please check include/uapi/scsi/ufs/ufs.h for the definition of it.
+	 */
+	__u8 idn;
+	/*
+	 * User should specify the size of the buffer (buffer[0] below) where
+	 * it wants to read the query data (attribute/flag/descriptor).
+	 * As we might end up reading less data then what is specified in
+	 * buf_size. So we are updating buf_size to what exactly we have read.
+	 */

include/uapi/scsi/ufs/ioctl.h line 56

Quoted 10 lines
include/uapi/scsi/ufs/ioctl.h new file +10 -0
+	__u16 buf_size;
+	/*
+	 * placeholder for the start of the data buffer where kernel will copy
+	 * the query data (attribute/flag/descriptor) read from the UFS device
+	 * Note:
+	 * For Read Attribute you will have to allocate 4 bytes
+	 * For Read Flag you will have to allocate 1 byte
+	 */
+	__u8 buffer[0];
+};

Better rearrange the structure to avoid the implied padding, either by making idn a __u16, or by adding an explicit __u8 member behind it.

Arnd

#2 lore raw

Thanks Arnd.
comments inline and will upload V3 shortly

Yaniv

Quoted 9 lines · On Wednesday 07 October 2015 10:54:03 Yaniv Gardi wrote:

drivers/scsi/ufs/ufshcd.c line 95

Quoted 4 lines
drivers/scsi/ufs/ufshcd.c +3 -0
+/* IOCTL opcode for command - ufs set device read only */
+#define UFS_IOCTL_BLKROSET      BLKROSET
+

What is this for? Can't you just use the normal BLKROSET definition in user space?

correct. will be removed

Quoted 9 lines

drivers/scsi/ufs/ufshcd.c out: · line 5079

Quoted 7 lines
(quoted diff) +2 -0
+
+       ioctl_data = kzalloc(sizeof(struct ufs_ioctl_query_data),

GFP_KERNEL);

drivers/scsi/ufs/ufshcd.c +4 -0
+       if (!ioctl_data) {
+               err = -ENOMEM;
+               goto out;
+       }

ufs_ioctl_query_data is short enough to just be on the stack.

ok

Quoted 34 lines

drivers/scsi/ufs/ufshcd.c out: · line 5119

Quoted 11 lines
drivers/scsi/ufs/ufshcd.c +10 -0
+		}
+		length = min_t(int, QUERY_DESC_MAX_SIZE,
+				ioctl_data->buf_size);
+		desc = kzalloc(length, GFP_KERNEL);
+		if (!desc) {
+			dev_err(hba->dev, "%s: Failed allocating %d bytes\n",
+					__func__, length);
+			err = -ENOMEM;
+			goto out_release_mem;
+		}

Better check for a maximum length as well, not just a minimum length. For overly long requests, just return an error without trying the kzalloc.

You can also use memdup_user to avoid the extra zeroing and and simplify the calling code.

Quoted 16 lines
drivers/scsi/ufs/ufshcd.c +10 -0
+/**
+ * ufshcd_ioctl - ufs ioctl callback registered in scsi_host
+ * @dev: scsi device required for per LUN queries
+ * @cmd: command opcode
+ * @buffer: user space buffer for transferring data
+ *
+ * Supported commands:
+ * UFS_IOCTL_QUERY
+ */
+static int ufshcd_ioctl(struct scsi_device *dev, int cmd, void __user

*buffer)

drivers/scsi/ufs/ufshcd.c +5 -0
+{
+	struct ufs_hba *hba = shost_priv(dev->host);
+	int err = 0;
+
+	BUG_ON(!hba);
Quoted 9 lines

drivers/scsi/ufs/ufshcd.c out: · line 5243

Quoted 4 lines
drivers/scsi/ufs/ufshcd.c +4 -0
+	if (!buffer) {
+		dev_err(hba->dev, "%s: User buffer is NULL!\n", __func__);
+		return -EINVAL;
+	}

Don't print an error here, you don't want users to be able to flood syslog that easily, and the program that caused the error does not see the log anyway.

ok

Quoted 10 lines
Quoted 10 lines
drivers/scsi/ufs/ufshcd.c +10 -0
+
+	switch (cmd) {
+	case UFS_IOCTL_QUERY:
+		pm_runtime_get_sync(hba->dev);
+		err = ufshcd_query_ioctl(hba, ufshcd_scsi_to_upiu_lun(dev->lun),
+				buffer);
+		pm_runtime_put_sync(hba->dev);
+		break;
+	case UFS_IOCTL_BLKROSET:
+		err = -ENOIOCTLCMD;
Quoted 9 lines

drivers/scsi/ufs/ufshcd.c out: · line 5259

Quoted 6 lines
drivers/scsi/ufs/ufshcd.c +6 -0
+		break;
+	default:
+		err = -EINVAL;
+		dev_err(hba->dev, "%s: Illegal ufs-IOCTL cmd %d\n", __func__,
+				cmd);
+		break;

same here.

ok

Quoted 14 lines
Quoted 14 lines
drivers/scsi/ufs/ufshcd.c +6 -0
+	}
+
+	return err;
+}
+
+/**
  * ufshcd_async_scan - asynchronous execution for probing hba
  * @data: data pointer to pass to this function
  * @cookie: cookie data
@@ -5106,6 +5322,7 @@ static struct scsi_host_template
 ufshcd_driver_template = {
 	.eh_device_reset_handler = ufshcd_eh_device_reset_handler,
 	.eh_host_reset_handler   = ufshcd_eh_host_reset_handler,
 	.eh_timed_out		= ufshcd_eh_timed_out,
Quoted 9 lines
Quoted 4 lines
drivers/scsi/ufs/ufshcd.c +1 -0
+	.ioctl			= ufshcd_ioctl,
 	.this_id		= -1,
 	.sg_tablesize		= SG_ALL,

.cmd_per_lun = UFSHCD_CMD_PER_LUN,

The ioctl data is compatible between 32-bit and 64-bit user space, so better add a .compat_ioctl line right away to make this work on 64-bit architectures with 32-bit user space.

ok

Quoted 8 lines

include/uapi/scsi/ufs/ioctl.h line 10

Quoted 6 lines
include/uapi/scsi/ufs/ioctl.h new file +6 -0
+
+/*
+ *  IOCTL opcode for ufs queries has the following opcode after
+ *  SCSI_IOCTL_GET_PCI
+ */
+#define UFS_IOCTL_QUERY			0x5388

Use _IOWR() to define that number with the correct argument length

i'd rather leave it as is, to be compatible with the SCSI_IOCTL opcodes that are defined in scsi_ioctl.h

Quoted 42 lines
Quoted 42 lines
include/uapi/scsi/ufs/ioctl.h new file +14 -0
+/**
+ * struct ufs_ioctl_query_data - used to transfer data to and from
+ * user via ioctl
+ * @opcode: type of data to query (descriptor/attribute/flag)
+ * @idn: id of the data structure
+ * @buf_size: number of allocated bytes/data size on return
+ * @buffer: data location
+ *
+ * Received: buffer and buf_size (available space for transferred data)
+ * Submitted: opcode, idn, length, buf_size
+ */
+struct ufs_ioctl_query_data {
+	/*
+	 * User should select one of the opcode defined in "enum

query_opcode".

include/uapi/scsi/ufs/ioctl.h new file +26 -0
+	 * Please check include/uapi/scsi/ufs/ufs.h for the definition of it.
+	 * Note that only UPIU_QUERY_OPCODE_READ_DESC,
+	 * UPIU_QUERY_OPCODE_READ_ATTR & UPIU_QUERY_OPCODE_READ_FLAG are
+	 * supported as of now. All other query_opcode would be considered
+	 * invalid.
+	 * As of now only read query operations are supported.
+	 */
+	__u32 opcode;
+	/*
+	 * User should select one of the idn from "enum flag_idn" or "enum
+	 * attr_idn" or "enum desc_idn" based on whether opcode above is
+	 * attribute, flag or descriptor.
+	 * Please check include/uapi/scsi/ufs/ufs.h for the definition of it.
+	 */
+	__u8 idn;
+	/*
+	 * User should specify the size of the buffer (buffer[0] below) where
+	 * it wants to read the query data (attribute/flag/descriptor).
+	 * As we might end up reading less data then what is specified in
+	 * buf_size. So we are updating buf_size to what exactly we have read.
+	 */
+	__u16 buf_size;
+	/*
+	 * placeholder for the start of the data buffer where kernel will copy
+	 * the query data (attribute/flag/descriptor) read from the UFS device
+	 * Note:
Quoted 9 lines

include/uapi/scsi/ufs/ioctl.h line 56

Quoted 5 lines
include/uapi/scsi/ufs/ioctl.h new file +5 -0
+	 * For Read Attribute you will have to allocate 4 bytes
+	 * For Read Flag you will have to allocate 1 byte
+	 */
+	__u8 buffer[0];
+};

Better rearrange the structure to avoid the implied padding, either by making idn a __u16, or by adding an explicit __u8 member behind it.

correct

Quoted 6 lines

Arnd
--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 10 lines · On Wednesday 07 October 2015 12:27:54 [email protected]…
Quoted 9 lines

include/uapi/scsi/ufs/ioctl.h line 10

Quoted 7 lines
include/uapi/scsi/ufs/ioctl.h new file +6 -0
+
+/*
+ *  IOCTL opcode for ufs queries has the following opcode after
+ *  SCSI_IOCTL_GET_PCI
+ */
+#define UFS_IOCTL_QUERY                     0x5388

Use _IOWR() to define that number with the correct argument length

Quoted 2 lines

i'd rather leave it as is, to be compatible with the SCSI_IOCTL opcodes that are defined in scsi_ioctl.h

Do you mean you require this code to work on SunOS? ;-)

If not, better use the normal conventions that Linux and BSD have for new ioctls.

Note that the 0x53xx ioctl command numbers are also used by include/uapi/linux/cdrom.h, this one would be right in the middle of that.

Arnd