Skip to contentSkip to conversation

ufshcd: UFS Host Controller Driver

PATCH In review Santosh Y 4 participants 14 messages Feb 29, 2012 – Apr 10, 2012

Subsystems (3)
lore
Share & tools
lore

Santosh Y

author
#0 lore raw

v3 cover ufshcd: UFS Host Controller Driver

From: Santosh Yaraganavi <[email protected]>

UFS:
Universal Flash Storage is a storage specification for flash devices. It is aimed to provide a universal storage interface for both embedded and removable flash memory based storage in mobile devices such as smart phones and tablet computers. The specification is defined by JEDEC Solid State Technology Association. UFS is based on MIPI M-PHY physical layer standard. UFS uses MIPI M-PHY as the physical layer and MIPI Unipro as the link layer.

The main goals of UFS is to provide,
- Optimized performance:
For UFS version 1.0 and 1.1 the target performance is as follows, Support for Gear1 is mandatory (rate A: 1248Mbps, rate B: 1457.6Mbps) Support for Gear2 is optional (rate A: 2496Mbps, rate B: 2915.2Mbps) Future version of the standard,
Gear3 (rate A: 4992Mbps, rate B: 5830.4Mbps)
- Low power consumption
- High random IOPs and low latency

UFS Architecture Overview:
UFS has a layered communication architecture which is based on SCSI SAM-5 architectural model.

UFS communication architecture consists of following layers,

* Application Layer: It is composed of UFS command set layer(UCS), Task Manager and Device manager. The UFS interface is designed to be protocol agnostic, however SCSI has been selected as a baseline protocol for versions 1.0 and 1.1 of UFS protocol layer. UFS supports subset of SCSI commands defined by SPC-4 and SBC-3.
- UCS: It handles SCSI commands supported by UFS specification.
- Task manager: It handles task management functions defined by the UFS which are meant for command queue control.
- Device manager: It handles device level operations and device configuration operations. Device level operations mainly involve device power management operations and commands to Interconnect layers. Device level configurations involve handling of query requests which are used to modify and retrieve configuration information of the device.

* UFS Transport Protocol(UTP) layer: UTP layer provides services for the higher layers through Service Access Points. UTP defines 3 service access points for higher layers.
- UDM_SAP: Device manager service access point is exposed to device manager for device level operations. These device level operations are done through query requests.
- UTP_CMD_SAP: Command service access point is exposed to UFS command set layer(UCS) to transport commands.
- UTP_TM_SAP: Task management service access point is exposed to task manager to transport task management functions.
UTP transports messages through UFS protocol information unit(UPIU).

* UFS Interconnect Layer(UIC): This is the lowest layer of UFS layered architecture. It handles connection between UFS host and UFS device. UIC consists of MIPI UniPro and MIPI M-PHY. UIC provides 2 service access points to upper layer,
- UIC_SAP: To transport UPIU between UFS host and UFS device.
- UIO_SAP: To issue commands to Unipro layers.

UFSHCD:
The UFS host controller driver is based on Linux SCSI Framework. UFSHCD is a low level device driver which acts as an interface between SCSI Midlayer and PCIe based UFS host controllers.

The current UFSHCD implementation supports following functionality,
- UFS controller initialization: The initialization module brings UFS host controller to active state and prepares the controller to transfer commands/response between UFSHCD and UFS device.
- UTP Transfer requests: Transfer request handling module of UFSHCD receives SCSI commands from SCSI Midlayer, forms UPIUs and issues the UPIUs to UFS Host controller. Also, the module decodes responses received from UFS host controller in the form of UPIUs and intimates the SCSI Midlayer of the status of the command.
- UFS error handling: Error handling module handles Host controller fatal errors, Device fatal errors and UIC interconnect layer related errors.
- SCSI Error handling: This is done through UFSHCD SCSI error handling routines registered with SCSI Midlayer. Examples of some of the error handling commands issues by SCSI Midlayer are Abort task, Lun reset and host reset. UFSHCD Routines to perform these tasks are registered with SCSI Midlayer through .eh_abort_handler, .eh_device_reset_handler and .eh_host_reset_handler.

In this version of UFSHCD Query requests and power management functionality are not implemented.

This patchset is successfully applied on 3.3-rc5

UFS Specifications can be found at,
UFS - http://www.jedec.org/sites/default/files/docs/JESD220.pdf
UFSHCI - http://www.jedec.org/sites/default/files/docs/JESD223.pdf

Santosh Yaraganavi (5):
[SCSI] ufshcd: UFS Host controller driver [SCSI] ufshcd: UFS UTP Transfer requests handling [SCSI] ufshcd: UFSHCI error handling [SCSI] ufshcd: SCSI error handling
Documentation: UFS Host Controller Driver

 Documentation/scsi/00-INDEX |    2 +
 Documentation/scsi/ufs.txt  |  133 +++
 drivers/scsi/Kconfig        |    1 +
 drivers/scsi/Makefile       |    1 +
 drivers/scsi/ufs/Kconfig    |   49 ++
 drivers/scsi/ufs/Makefile   |    2 +
 drivers/scsi/ufs/ufs.h      |  207 +++++
 drivers/scsi/ufs/ufshcd.c   | 1978 +++++++++++++++++++++++++++++++++++++++++++
 drivers/scsi/ufs/ufshci.h   |  376 ++++++++
 9 files changed, 2749 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/scsi/ufs.txt
 create mode 100644 drivers/scsi/ufs/Kconfig
 create mode 100644 drivers/scsi/ufs/Makefile
 create mode 100644 drivers/scsi/ufs/ufs.h
 create mode 100644 drivers/scsi/ufs/ufshcd.c
 create mode 100644 drivers/scsi/ufs/ufshci.h
Signature · 1 line
1.7.5.4
author
#1 lore raw

PATCH v3 1/5 ufshcd: UFS Host controller driver

From: Santosh Yaraganavi <[email protected]>

This patch adds support for Universal Flash Storage(UFS) host controllers. The UFS host controller driver includes host controller initialization method.

The Initialization process involves following steps:
- Initiate UFS Host Controller initialization process by writing to Host controller enable register
- Configure UFS Host controller registers with host memory space datastructure offsets.
- Unipro link startup procedure
- Check for connected device
- Configure UFS host controller to process requests
- Enable required interrupts
- Configure interrupt aggregation

---
v2 -> v3:
- use shost_priv() instead of accessing hostdata directly.
v1 -> v2:
- ufshcd_memory_alloc(): remove explicit memory alignment.
- Re-use ufshcd_free_hba_memory() in ufshcd_memory_alloc
- ufshcd_make_hba_operational(): Remove -EINVAL and use -ENXIO, -EIO
- Remove NULL macro definition.
- Remove macros BYTES_TO_DWORDS and UFSHCD_MMIO_BASE.
- ufshcd_controller_enable(): send controller to reset state only if the controller is active.
- ufshcd_intr(): Use spin_lock() instead of spin_lock_irqsave().
- ufshcd_suspend/ufshcd_resume: add TODO comments.
- ufshcd_set_dma_mask(): Change DMA mask setting to 64/32 based on controller capability.
- Change conditional statement to match kernel coding style,
Ex: if (constant == variable) to if (variable == constant)
if (NULL != pointer) to if (pointer)

 drivers/scsi/Kconfig      |    1 +
 drivers/scsi/Makefile     |    1 +
 drivers/scsi/ufs/Kconfig  |   49 ++
 drivers/scsi/ufs/Makefile |    2 +
 drivers/scsi/ufs/ufs.h    |  207 +++++++++
 drivers/scsi/ufs/ufshcd.c | 1091 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/scsi/ufs/ufshci.h |  376 ++++++++++++++++
 7 files changed, 1727 insertions(+), 0 deletions(-)
 create mode 100644 drivers/scsi/ufs/Kconfig
 create mode 100644 drivers/scsi/ufs/Makefile
 create mode 100644 drivers/scsi/ufs/ufs.h
 create mode 100644 drivers/scsi/ufs/ufshcd.c
 create mode 100644 drivers/scsi/ufs/ufshci.h
drivers/scsi/Kconfig +1 -0
index 16570aa..477a91a 100644
@@ -619,6 +619,7 @@ config SCSI_ARCMSR
 
 source "drivers/scsi/megaraid/Kconfig.megaraid"
 source "drivers/scsi/mpt2sas/Kconfig"
+source "drivers/scsi/ufs/Kconfig"
 
 config SCSI_HPTIOP
 	tristate "HighPoint RocketRAID 3xxx/4xxx Controller support"
drivers/scsi/Makefile +1 -0
index 2b88749..c832974 100644
@@ -108,6 +108,7 @@ obj-$(CONFIG_MEGARAID_LEGACY)	+= megaraid.o
 obj-$(CONFIG_MEGARAID_NEWGEN)	+= megaraid/
 obj-$(CONFIG_MEGARAID_SAS)	+= megaraid/
 obj-$(CONFIG_SCSI_MPT2SAS)	+= mpt2sas/
+obj-$(CONFIG_SCSI_UFSHCD)	+= ufs/
 obj-$(CONFIG_SCSI_ACARD)	+= atp870u.o
 obj-$(CONFIG_SCSI_SUNESP)	+= esp_scsi.o	sun_esp.o
 obj-$(CONFIG_SCSI_GDTH)		+= gdth.o
drivers/scsi/ufs/Kconfig new file +49 -0

49 lines not on this page · raw message

drivers/scsi/ufs/Makefile new file +2 -0
new file mode 100644
index 0000000..adf7895
@@ -0,0 +1,2 @@
+# UFSHCD makefile
+obj-$(CONFIG_SCSI_UFSHCD) += ufshcd.o
drivers/scsi/ufs/ufs.h new file +207 -0

207 lines not on this page · raw message

drivers/scsi/ufs/ufshcd.c new file +1091 -0

1,091 lines not on this page · raw message

drivers/scsi/ufs/ufshci.h new file +376 -0

376 lines not on this page · raw message

Signature · 1 line
1.7.5.4
#2 lore raw

Re: [PATCH v3 1/5] [SCSI] ufshcd: UFS Host controller driver

Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+        utrdlp[i].response_upiu_length =
+                cpu_to_le16(ALIGNED_UPIU_SIZE);

Response UPIU Length(RUL) shows the response UPIU length in Dword. (JEDEC Standard No.223 6.2.1 UTP Transfer Request Descriptor) The following will be correct.

drivers/scsi/ufs/ufshcd.c +1 -1
index 07ec973..feffe65 100644
@@ -836,7 +836,7 @@ static void ufshcd_host_memory_configure(struct ufs_hba *hba)
                utrdlp[i].prd_table_offset =
                                cpu_to_le16((prdt_offset >> 2));
                utrdlp[i].response_upiu_length =
-                               cpu_to_le16(ALIGNED_UPIU_SIZE);
+                               cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
 
                hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
                hba->lrb[i].ucd_cmd_ptr =
author
#3 lore raw

Re: [PATCH v3 1/5] [SCSI] ufshcd: UFS Host controller driver

Quoted 21 lines · KOBAYASHI Yoshitake
Quoted 2 lines
Quoted 2 lines

+        utrdlp[i].response_upiu_length =
+                cpu_to_le16(ALIGNED_UPIU_SIZE);

Response UPIU Length(RUL) shows the response UPIU length in Dword. (JEDEC Standard No.223 6.2.1 UTP Transfer Request Descriptor) The following will be correct.

drivers/scsi/ufs/ufshcd.c +0 -0
index 07ec973..feffe65 100644
@@ -836,7 +836,7 @@ static void ufshcd_host_memory_configure(struct ufs_hba
 *hba)

               utrdlp[i].prd_table_offset = cpu_to_le16((prdt_offset >> 2)); utrdlp[i].response_upiu_length =
-                               cpu_to_le16(ALIGNED_UPIU_SIZE);
+                               cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);

Quoted 3 lines

               hba->lrb[i].utr_descriptor_ptr = (utrdlp + i); hba->lrb[i].ucd_cmd_ptr =

Thanks, i'll update it.

Signature · 1 line
~Santosh
author
#4 lore raw

PATCH v3 2/5 ufshcd: UFS UTP Transfer requests handling

In reply to Santosh Y

From: Santosh Yaraganavi <[email protected]>

This patch adds support for Transfer request handling.

ufshcd includes following implementations:
- SCSI queuecommand
- Compose UPIU(UFS Protocol information unit)
- Issue commands to UFS host controller
- Handle completed commands

---
v2 -> v3:
- use shost_priv() instead of accessing hostdata directly.
- update SCSI command result with appropriate byte codes.
v1 -> v2:
- Use bitops.h defined functions to perform bit operations.
Ex: set_bit(), __set_bit, clear_bit...
- ufshcd_map_sg(): remove BUG_ON on scsi_dma_map() failure and return error value.
- ufshcd_compose_upiu(): add missing break in switch case, remove unused parameter "hba".
- ufshcd_slave_alloc(): set sdev->use_10_for_ms = 1, since UFS does not support MODE_SENSE_6.
- ufshcd_scsi_cmd_status(): add missing break in switch case. Add ufshcd_adjust_lun_qdepth to adjust LUN queue depth if SAM_STAT_TASK_SET_FULL is received from the device.
- ufshcd_transfer_rsp_status(): combine cases with same error code.

 drivers/scsi/ufs/ufshcd.c |  494 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 494 insertions(+), 0 deletions(-)
drivers/scsi/ufs/ufshcd.c +494 -0
index 26b16c1..43b9c62 100644
@@ -63,6 +63,7 @@
 #include <scsi/scsi.h>
 #include <scsi/scsi_cmnd.h>
 #include <scsi/scsi_host.h>
+#include <scsi/scsi_tcq.h>
 #include <scsi/scsi_dbg.h>
 
 #include "ufs.h"
@@ -75,6 +76,8 @@ enum {
 	UFSHCD_MAX_CHANNEL	= 0,
 	UFSHCD_MAX_ID		= 1,
 	UFSHCD_MAX_LUNS		= 8,
+	UFSHCD_CMD_PER_LUN	= 32,
+	UFSHCD_CAN_QUEUE	= 32,
 };
 
 /* UFSHCD states */
@@ -127,6 +130,7 @@ struct uic_command {
  * @host: Scsi_Host instance of the driver
  * @pdev: PCI device handle
  * @lrb: local reference block
+ * @outstanding_reqs: Bits representing outstanding transfer requests
  * @capabilities: UFS Controller Capabilities
  * @nutrs: Transfer Request Queue depth supported by controller
  * @nutmrs: Task Management Queue depth supported by controller
@@ -153,6 +157,8 @@ struct ufs_hba {
 
 	struct ufshcd_lrb *lrb;
 
+	unsigned long outstanding_reqs;
+
 	u32 capabilities;
 	int nutrs;
 	int nutmrs;
@@ -173,12 +179,28 @@ struct ufs_hba {
  * @ucd_cmd_ptr: UCD address of the command
  * @ucd_rsp_ptr: Response UPIU address for this command
  * @ucd_prdt_ptr: PRDT address of the command
+ * @cmd: pointer to SCSI command
+ * @sense_buffer: pointer to sense buffer address of the SCSI command
+ * @sense_bufflen: Length of the sense buffer
+ * @scsi_status: SCSI status of the command
+ * @command_type: SCSI, UFS, Query.
+ * @task_tag: Task tag of the command
+ * @lun: LUN of the command
  */
 struct ufshcd_lrb {
 	struct utp_transfer_req_desc *utr_descriptor_ptr;
 	struct utp_upiu_cmd *ucd_cmd_ptr;
 	struct utp_upiu_rsp *ucd_rsp_ptr;
 	struct ufshcd_sg_entry *ucd_prdt_ptr;
+
+	struct scsi_cmnd *cmd;
+	u8 *sense_buffer;
+	unsigned int sense_bufflen;
+	int scsi_status;
+
+	int command_type;
+	int task_tag;
+	unsigned int lun;
 };
 
 /**
@@ -205,6 +227,18 @@ static inline int ufshcd_is_device_present(u32 reg_hcs)
 }
 
 /**
+ * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
+ * @lrb: pointer to local command reference block
+ *
+ * This function is used to get the OCS field from UTRD
+ * Returns the OCS field in the UTRD
+ */
+static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
+{
+	return lrbp->utr_descriptor_ptr->header.dword_2 & MASK_OCS;
+}
+
+/**
  * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
  * @reg: Register value of host controller status
  *
@@ -272,6 +306,34 @@ static inline void ufshcd_free_hba_memory(struct ufs_hba *hba)
 }
 
 /**
+ * ufshcd_is_valid_req_rsp - checks if controller TR response is valid
+ * @ucd_rsp_ptr: pointer to response UPIU
+ *
+ * This function checks the response UPIU for valid transaction type in
+ * response field
+ * Returns 0 on success, non-zero on failure
+ */
+static inline int
+ufshcd_is_valid_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
+{
+	return ((be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24) ==
+		 UPIU_TRANSACTION_RESPONSE) ? 0 : DID_ERROR << 16;
+}
+
+/**
+ * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
+ * @ucd_rsp_ptr: pointer to response UPIU
+ *
+ * This function gets the response status and scsi_status from response UPIU
+ * Returns the response result code.
+ */
+static inline int
+ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
+{
+	return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
+}
+
+/**
  * ufshcd_config_int_aggr - Configure interrupt aggregation values.
  *		Currently there is no use case where we want to configure
  *		interrupt aggregation dynamically. So to configure interrupt
@@ -347,6 +409,34 @@ static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
 }
 
 /**
+ * ufshcd_send_command - Send SCSI or device management commands
+ * @hba: per adapter instance
+ * @task_tag: Task tag of the command
+ */
+static inline
+void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
+{
+	__set_bit(task_tag, &hba->outstanding_reqs);
+	writel((1 << task_tag),
+	       (hba->mmio_base + REG_UTP_TRANSFER_REQ_DOOR_BELL));
+}
+
+/**
+ * ufshcd_copy_sense_data - Copy sense data in case of check condition
+ * @lrb - pointer to local reference block
+ */
+static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
+{
+	int len;
+	if (lrbp->sense_buffer) {
+		len = be16_to_cpu(lrbp->ucd_rsp_ptr->sense_data_len);
+		memcpy(lrbp->sense_buffer,
+			lrbp->ucd_rsp_ptr->sense_data,
+			min_t(int, len, SCSI_SENSE_BUFFERSIZE));
+	}
+}
+
+/**
  * ufshcd_hba_capabilities - Read controller capabilities
  * @hba: per adapter instance
  */
@@ -383,6 +473,46 @@ ufshcd_send_uic_command(struct ufs_hba *hba, struct uic_command *uic_cmnd)
 }
 
 /**
+ * ufshcd_map_sg - Map scatter-gather list to prdt
+ * @lrbp - pointer to local reference block
+ *
+ * Returns 0 in case of success, non-zero value in case of failure
+ */
+static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
+{
+	struct ufshcd_sg_entry *prd_table;
+	struct scatterlist *sg;
+	struct scsi_cmnd *cmd;
+	int sg_segments;
+	int i;
+
+	cmd = lrbp->cmd;
+	sg_segments = scsi_dma_map(cmd);
+	if (sg_segments < 0)
+		return sg_segments;
+
+	if (sg_segments) {
+		lrbp->utr_descriptor_ptr->prd_table_length =
+					cpu_to_le16((u16) (sg_segments));
+
+		prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
+
+		scsi_for_each_sg(cmd, sg, sg_segments, i) {
+			prd_table[i].size  =
+				cpu_to_le32(((u32) sg_dma_len(sg))-1);
+			prd_table[i].base_addr =
+				cpu_to_le32(sg->dma_address);
+			prd_table[i].upper_addr =
+				cpu_to_le32((sg->dma_address >> 32));
+		}
+	} else {
+		lrbp->utr_descriptor_ptr->prd_table_length = 0;
+	}
+
+	return 0;
+}
+
+/**
  * ufshcd_int_config - enable/disable interrupts
  * @hba: per adapter instance
  * @option: interrupt option
@@ -406,6 +536,127 @@ static void ufshcd_int_config(struct ufs_hba *hba, u32 option)
 }
 
 /**
+ * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
+ * @lrb - pointer to local reference block
+ */
+static void ufshcd_compose_upiu(struct ufshcd_lrb *lrbp)
+{
+	struct utp_transfer_req_desc *req_desc;
+	struct utp_upiu_cmd *ucd_cmd_ptr;
+	u32 data_direction;
+	u32 upiu_flags;
+
+	ucd_cmd_ptr = lrbp->ucd_cmd_ptr;
+	req_desc = lrbp->utr_descriptor_ptr;
+
+	switch (lrbp->command_type) {
+	case UTP_CMD_TYPE_SCSI:
+		if (lrbp->cmd->sc_data_direction == DMA_FROM_DEVICE) {
+			data_direction = UTP_DEVICE_TO_HOST;
+			upiu_flags = UPIU_CMD_FLAGS_READ;
+		} else if (lrbp->cmd->sc_data_direction == DMA_TO_DEVICE) {
+			data_direction = UTP_HOST_TO_DEVICE;
+			upiu_flags = UPIU_CMD_FLAGS_WRITE;
+		} else {
+			data_direction = UTP_NO_DATA_TRANSFER;
+			upiu_flags = UPIU_CMD_FLAGS_NONE;
+		}
+
+		/* Transfer request descriptor header fields */
+		req_desc->header.dword_0 =
+			cpu_to_le32(data_direction | UTP_SCSI_COMMAND);
+
+		/*
+		 * assigning invalid value for command status. Controller
+		 * updates OCS on command completion, with the command
+		 * status
+		 */
+		req_desc->header.dword_2 =
+			cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
+
+		/* command descriptor fields */
+		ucd_cmd_ptr->header.dword_0 =
+			cpu_to_be32(UPIU_HEADER_DWORD(UPIU_TRANSACTION_COMMAND,
+						      upiu_flags,
+						      lrbp->lun,
+						      lrbp->task_tag));
+		ucd_cmd_ptr->header.dword_1 =
+			cpu_to_be32(
+				UPIU_HEADER_DWORD(UPIU_COMMAND_SET_TYPE_SCSI,
+						  0,
+						  0,
+						  0));
+
+		/* Total EHS length and Data segment length will be zero */
+		ucd_cmd_ptr->header.dword_2 = 0;
+
+		ucd_cmd_ptr->exp_data_transfer_len =
+			cpu_to_be32(lrbp->cmd->transfersize);
+
+		memcpy(ucd_cmd_ptr->cdb,
+		       lrbp->cmd->cmnd,
+		       (min_t(unsigned short,
+			      lrbp->cmd->cmd_len,
+			      MAX_CDB_SIZE)));
+		break;
+	case UTP_CMD_TYPE_DEV_MANAGE:
+		/* For query function implementation */
+		break;
+	case UTP_CMD_TYPE_UFS:
+		/* For UFS native command implementation */
+		break;
+	} /* end of switch */
+}
+
+/**
+ * ufshcd_queuecommand - main entry point for SCSI requests
+ * @cmd: command from SCSI Midlayer
+ * @done: call back function
+ *
+ * Returns 0 for success, non-zero in case of failure
+ */
+static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
+{
+	struct ufshcd_lrb *lrbp;
+	struct ufs_hba *hba;
+	unsigned long flags;
+	int tag;
+	int err = 0;
+
+	hba = shost_priv(host);
+
+	tag = cmd->request->tag;
+
+	if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
+		err = SCSI_MLQUEUE_HOST_BUSY;
+		goto out;
+	}
+
+	lrbp = &hba->lrb[tag];
+
+	lrbp->cmd = cmd;
+	lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
+	lrbp->sense_buffer = cmd->sense_buffer;
+	lrbp->task_tag = tag;
+	lrbp->lun = cmd->device->lun;
+
+	lrbp->command_type = UTP_CMD_TYPE_SCSI;
+
+	/* form UPIU before issuing the command */
+	ufshcd_compose_upiu(lrbp);
+	err = ufshcd_map_sg(lrbp);
+	if (err)
+		goto out;
+
+	/* issue command to the controller */
+	spin_lock_irqsave(hba->host->host_lock, flags);
+	ufshcd_send_command(hba, tag);
+	spin_unlock_irqrestore(hba->host->host_lock, flags);
+out:
+	return err;
+}
+
+/**
  * ufshcd_memory_alloc - allocate memory for host memory space data structures
  * @hba: per adapter instance
  *
@@ -643,6 +894,7 @@ static int ufshcd_make_hba_operational(struct ufs_hba *hba)
 	ufshcd_config_int_aggr(hba, INT_AGGR_CONFIG);
 
 	hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
+	scsi_scan_host(hba->host);
 out:
 	return err;
 }
@@ -742,6 +994,230 @@ static int ufshcd_initialize_hba(struct ufs_hba *hba)
 }
 
 /**
+ * ufshcd_slave_alloc - handle initial SCSI device configurations
+ * @sdev: pointer to SCSI device
+ *
+ * Returns success
+ */
+static int ufshcd_slave_alloc(struct scsi_device *sdev)
+{
+	struct ufs_hba *hba;
+
+	hba = shost_priv(sdev->host);
+	sdev->tagged_supported = 1;
+
+	/* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
+	sdev->use_10_for_ms = 1;
+	scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
+
+	/*
+	 * Inform SCSI Midlayer that the LUN queue depth is same as the
+	 * controller queue depth. If a LUN queue depth is less than the
+	 * controller queue depth and if the LUN reports
+	 * SAM_STAT_TASK_SET_FULL, the LUN queue depth will be adjusted
+	 * with scsi_adjust_queue_depth.
+	 */
+	scsi_activate_tcq(sdev, hba->nutrs);
+	return 0;
+}
+
+/**
+ * ufshcd_slave_destroy - remove SCSI device configurations
+ * @sdev: pointer to SCSI device
+ */
+static void ufshcd_slave_destroy(struct scsi_device *sdev)
+{
+	struct ufs_hba *hba;
+
+	hba = shost_priv(sdev->host);
+	scsi_deactivate_tcq(sdev, hba->nutrs);
+}
+
+/**
+ * ufshcd_adjust_lun_qdepth - Update LUN queue depth if device responds with
+ *			      SAM_STAT_TASK_SET_FULL SCSI command status.
+ * @cmd: pointer to SCSI command
+ */
+static void ufshcd_adjust_lun_qdepth(struct scsi_cmnd *cmd)
+{
+	struct ufs_hba *hba;
+	int i;
+	int lun_qdepth = 0;
+
+	hba = shost_priv(cmd->device->host);
+
+	/*
+	 * LUN queue depth can be obtained by counting outstanding commands
+	 * on the LUN.
+	 */
+	for (i = 0; i < hba->nutrs; i++) {
+		if (test_bit(i, &hba->outstanding_reqs)) {
+
+			/*
+			 * Check if the outstanding command belongs
+			 * to the LUN which reported SAM_STAT_TASK_SET_FULL.
+			 */
+			if (cmd->device->lun == hba->lrb[i].lun)
+				lun_qdepth++;
+		}
+	}
+
+	/*
+	 * LUN queue depth will be total outstanding commands, except the
+	 * command for which the LUN reported SAM_STAT_TASK_SET_FULL.
+	 */
+	scsi_adjust_queue_depth(cmd->device, MSG_SIMPLE_TAG, lun_qdepth - 1);
+}
+
+/**
+ * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
+ * @lrb: pointer to local reference block of completed command
+ * @scsi_status: SCSI command status
+ *
+ * Returns value base on SCSI command status
+ */
+static inline int
+ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
+{
+	int result = 0;
+
+	switch (scsi_status) {
+	case SAM_STAT_GOOD:
+		result |= DID_OK << 16 |
+			  COMMAND_COMPLETE << 8 |
+			  SAM_STAT_GOOD;
+		break;
+	case SAM_STAT_CHECK_CONDITION:
+		result |= DID_OK << 16 |
+			  COMMAND_COMPLETE << 8 |
+			  SAM_STAT_CHECK_CONDITION;
+		ufshcd_copy_sense_data(lrbp);
+		break;
+	case SAM_STAT_BUSY:
+		result |= SAM_STAT_BUSY;
+		break;
+	case SAM_STAT_TASK_SET_FULL:
+
+		/*
+		 * If a LUN reports SAM_STAT_TASK_SET_FULL, then the LUN queue
+		 * depth needs to be adjusted to the exact number of
+		 * outstanding commands the LUN can handle at any given time.
+		 */
+		ufshcd_adjust_lun_qdepth(lrbp->cmd);
+		result |= SAM_STAT_TASK_SET_FULL;
+		break;
+	case SAM_STAT_TASK_ABORTED:
+		result |= SAM_STAT_TASK_ABORTED;
+		break;
+	default:
+		result |= DID_ERROR << 16;
+		break;
+	} /* end of switch */
+
+	return result;
+}
+
+/**
+ * ufshcd_transfer_rsp_status - Get overall status of the response
+ * @hba: per adapter instance
+ * @lrb: pointer to local reference block of completed command
+ *
+ * Returns result of the command to notify SCSI midlayer
+ */
+static inline int
+ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
+{
+	int result = 0;
+	int scsi_status;
+	int ocs;
+
+	/* overall command status of utrd */
+	ocs = ufshcd_get_tr_ocs(lrbp);
+
+	switch (ocs) {
+	case OCS_SUCCESS:
+
+		/* check if the returned transfer response is valid */
+		result = ufshcd_is_valid_req_rsp(lrbp->ucd_rsp_ptr);
+		if (result) {
+			dev_err(&hba->pdev->dev,
+				"Invalid response = %x\n", result);
+			break;
+		}
+
+		/*
+		 * get the response UPIU result to extract
+		 * the SCSI command status
+		 */
+		result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
+
+		/*
+		 * get the result based on SCSI status response
+		 * to notify the SCSI midlayer of the command status
+		 */
+		scsi_status = result & MASK_SCSI_STATUS;
+		result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
+		break;
+	case OCS_ABORTED:
+		result |= DID_ABORT << 16;
+		break;
+	case OCS_INVALID_CMD_TABLE_ATTR:
+	case OCS_INVALID_PRDT_ATTR:
+	case OCS_MISMATCH_DATA_BUF_SIZE:
+	case OCS_MISMATCH_RESP_UPIU_SIZE:
+	case OCS_PEER_COMM_FAILURE:
+	case OCS_FATAL_ERROR:
+	default:
+		result |= DID_ERROR << 16;
+		dev_err(&hba->pdev->dev,
+		"OCS error from controller = %x\n", ocs);
+		break;
+	} /* end of switch */
+
+	return result;
+}
+
+/**
+ * ufshcd_transfer_req_compl - handle SCSI and query command completion
+ * @hba: per adapter instance
+ */
+static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
+{
+	struct ufshcd_lrb *lrb;
+	unsigned long completed_reqs;
+	u32 tr_doorbell;
+	int result;
+	int index;
+
+	lrb = hba->lrb;
+	tr_doorbell =
+		readl(hba->mmio_base + REG_UTP_TRANSFER_REQ_DOOR_BELL);
+	completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
+
+	for (index = 0; index < hba->nutrs; index++) {
+		if (test_bit(index, &completed_reqs)) {
+
+			result = ufshcd_transfer_rsp_status(hba, &lrb[index]);
+
+			if (lrb[index].cmd) {
+				scsi_dma_unmap(lrb[index].cmd);
+				lrb[index].cmd->result = result;
+				lrb[index].cmd->scsi_done(lrb[index].cmd);
+
+				/* Mark completed command as NULL in LRB */
+				lrb[index].cmd = NULL;
+			}
+		} /* end of if */
+	} /* end of for */
+
+	/* clear corresponding bits of completed commands */
+	hba->outstanding_reqs ^= completed_reqs;
+
+	/* Reset interrupt aggregation counters */
+	ufshcd_config_int_aggr(hba, INT_AGGR_RESET);
+}
+
+/**
  * ufshcd_uic_cc_handler - handle UIC command completion
  * @work: pointer to a work queue structure
  *
@@ -772,6 +1248,9 @@ static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
 {
 	if (intr_status & UIC_COMMAND_COMPL)
 		schedule_work(&hba->uic_workq);
+
+	if (intr_status & UTP_TRANSFER_REQ_COMPL)
+		ufshcd_transfer_req_compl(hba);
 }
 
 /**
@@ -808,7 +1287,13 @@ static struct scsi_host_template ufshcd_driver_template = {
 	.module			= THIS_MODULE,
 	.name			= UFSHCD,
 	.proc_name		= UFSHCD,
+	.queuecommand		= ufshcd_queuecommand,
+	.slave_alloc		= ufshcd_slave_alloc,
+	.slave_destroy		= ufshcd_slave_destroy,
 	.this_id		= -1,
+	.sg_tablesize		= SG_ALL,
+	.cmd_per_lun		= UFSHCD_CMD_PER_LUN,
+	.can_queue		= UFSHCD_CAN_QUEUE,
 };
 
 /**
@@ -998,10 +1483,12 @@ ufshcd_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	ufshcd_host_memory_configure(hba);
 
 	host->can_queue = hba->nutrs;
+	host->cmd_per_lun = hba->nutrs;
 	host->max_id = UFSHCD_MAX_ID;
 	host->max_lun = UFSHCD_MAX_LUNS;
 	host->max_channel = UFSHCD_MAX_CHANNEL;
 	host->unique_id = host->host_no;
+	host->max_cmd_len = MAX_CDB_SIZE;
 
 	/* Initialize work queues */
 	INIT_WORK(&hba->uic_workq, ufshcd_uic_cc_handler);
@@ -1013,6 +1500,13 @@ ufshcd_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		goto out_lrb_free;
 	}
 
+	/* Enable SCSI tag mapping */
+	err = scsi_init_shared_tag_map(host, host->can_queue);
+	if (err) {
+		dev_err(&pdev->dev, "init shared queue failed\n");
+		goto out_free_irq;
+	}
+
 	pci_set_drvdata(pdev, hba);
 
 	err = scsi_add_host(host, &pdev->dev);
Signature · 1 line
1.7.5.4
#5 lore raw

Re: [PATCH v3 2/5] [SCSI] ufshcd: UFS UTP Transfer requests handling

Review

Quoted 8 lines · Santosh Y
(quoted diff) +8 -0
+        ucd_cmd_ptr->exp_data_transfer_len =
+            cpu_to_be32(lrbp->cmd->transfersize);
+
+        memcpy(ucd_cmd_ptr->cdb,
+               lrbp->cmd->cmnd,
+               (min_t(unsigned short,
+                  lrbp->cmd->cmd_len,
+                  MAX_CDB_SIZE)));

"Exptected Data Transfer Length" field contains a value that represents the number of bytes that are required to complete the
SCSI command request and the number of bytes that the Initiator expects to be transferred to/from the Target.
(JEDEC Standard 220 Table7-7)
On the other hand, "transfersize" in a scsi_cmnd struct is trimmed
to the sector size when requested transfer length exceeds it.
You may derive the actual transfer length from SCSI command itself like following.

drivers/scsi/ufs/ufshcd.c +28 -0
index feffe65..e63fecb 100644
@@ -647,6 +647,34 @@ static void ufshcd_compose_upiu(struct ufshcd_lrb *lrbp)
                       (min_t(unsigned short,
                              lrbp->cmd->cmd_len,
                              MAX_CDB_SIZE)));
+
+               /* Overwrite exptected transfer length by using TRANSFER_LENGTH in SCSI commands */
+               switch(ucd_cmd_ptr->cdb[0]) {
+               case READ_6:
+               case WRITE_6:
+                       ucd_cmd_ptr->exp_data_transfer_len =
+                               cpu_to_be32(lrbp->cmd->transfersize *
+                                           ucd_cmd_ptr->cdb[4]);
+                       break;
+               case READ_10:
+               case WRITE_10:
+                       ucd_cmd_ptr->exp_data_transfer_len =
+                               cpu_to_be32(lrbp->cmd->transfersize *
+                                           ((ucd_cmd_ptr->cdb[7] << 8) |
+                                            ucd_cmd_ptr->cdb[8]));
+                       break;
+               case READ_12:
+               case WRITE_12:
+                       ucd_cmd_ptr->exp_data_transfer_len =
+                               cpu_to_be32(lrbp->cmd->transfersize *
+                                           ((ucd_cmd_ptr->cdb[10] << 24) |
+                                            (ucd_cmd_ptr->cdb[11] << 16) |
+                                            (ucd_cmd_ptr->cdb[12] << 8) |
+                                            ucd_cmd_ptr->cdb[13]));
+                       break;
+               default:
+                       break;
+               }
                break;
        case UTP_CMD_TYPE_DEV_MANAGE:
                /* For query function implementation */
author
#6 lore raw

Re: [PATCH v3 2/5] [SCSI] ufshcd: UFS UTP Transfer requests handling

Quoted 63 lines · KOBAYASHI Yoshitake
Quoted 9 lines · Santosh Y

+        ucd_cmd_ptr->exp_data_transfer_len =
+            cpu_to_be32(lrbp->cmd->transfersize); +
+        memcpy(ucd_cmd_ptr->cdb,
+               lrbp->cmd->cmnd,
+               (min_t(unsigned short,
+                  lrbp->cmd->cmd_len,
+                  MAX_CDB_SIZE)));

"Exptected Data Transfer Length" field contains a value that represents the number of bytes that are required to complete the SCSI command request and the number of bytes that the Initiator expects to be transferred to/from the Target.
(JEDEC Standard 220 Table7-7)
On the other hand, "transfersize" in a scsi_cmnd struct is trimmed to the sector size when requested transfer length exceeds it. You may derive the actual transfer length from SCSI command itself like following.

drivers/scsi/ufs/ufshcd.c +2 -0
index feffe65..e63fecb 100644
@@ -647,6 +647,34 @@ static void ufshcd_compose_upiu(struct ufshcd_lrb
 *lrbp)
                       (min_t(unsigned short,
                              lrbp->cmd->cmd_len,
                              MAX_CDB_SIZE)));
+
+               /* Overwrite exptected transfer length by using

TRANSFER_LENGTH in SCSI commands */
+               switch(ucd_cmd_ptr->cdb[0]) {
+               case READ_6:
+               case WRITE_6:
+                       ucd_cmd_ptr->exp_data_transfer_len =
+                               cpu_to_be32(lrbp->cmd->transfersize *
+                                           ucd_cmd_ptr->cdb[4]);
+                       break;
+               case READ_10:
+               case WRITE_10:
+                       ucd_cmd_ptr->exp_data_transfer_len =
+                               cpu_to_be32(lrbp->cmd->transfersize *
+                                           ((ucd_cmd_ptr->cdb[7] << 8) |
+                                            ucd_cmd_ptr->cdb[8]));
+                       break;
+               case READ_12:
+               case WRITE_12:
+                       ucd_cmd_ptr->exp_data_transfer_len =
+                               cpu_to_be32(lrbp->cmd->transfersize *
+                                           ((ucd_cmd_ptr->cdb[10] << 24) |
+                                            (ucd_cmd_ptr->cdb[11] << 16) |
+                                            (ucd_cmd_ptr->cdb[12] << 8) |
+                                            ucd_cmd_ptr->cdb[13]));
+                       break;
+               default:
+                       break;
+               }
               break;
       case UTP_CMD_TYPE_DEV_MANAGE:

Quoted 2 lines

               /* For query function implementation */

Thanks, will incorporate the changes accordingly.

Signature · 1 line
~Santosh
author
#7 lore raw

Re: [PATCH v3 2/5] [SCSI] ufshcd: UFS UTP Transfer requests handling

In reply to KOBAYASHI Yoshitake
Quoted 42 lines · KOBAYASHI Yoshitake
Quoted 9 lines · Santosh Y

+        ucd_cmd_ptr->exp_data_transfer_len =
+            cpu_to_be32(lrbp->cmd->transfersize); +
+        memcpy(ucd_cmd_ptr->cdb,
+               lrbp->cmd->cmnd,
+               (min_t(unsigned short,
+                  lrbp->cmd->cmd_len,
+                  MAX_CDB_SIZE)));

"Exptected Data Transfer Length" field contains a value that represents the number of bytes that are required to complete the SCSI command request and the number of bytes that the Initiator expects to be transferred to/from the Target.
(JEDEC Standard 220 Table7-7)
On the other hand, "transfersize" in a scsi_cmnd struct is trimmed to the sector size when requested transfer length exceeds it. You may derive the actual transfer length from SCSI command itself like following.

drivers/scsi/ufs/ufshcd.c +2 -0
index feffe65..e63fecb 100644
@@ -647,6 +647,34 @@ static void ufshcd_compose_upiu(struct ufshcd_lrb
 *lrbp)
                       (min_t(unsigned short,
                              lrbp->cmd->cmd_len,
                              MAX_CDB_SIZE)));
+
+               /* Overwrite exptected transfer length by using

TRANSFER_LENGTH in SCSI commands */
+               switch(ucd_cmd_ptr->cdb[0]) {
+               case READ_6:
+               case WRITE_6:
+                       ucd_cmd_ptr->exp_data_transfer_len =
+                               cpu_to_be32(lrbp->cmd->transfersize *
+                                           ucd_cmd_ptr->cdb[4]);
+                       break;
+               case READ_10:

Quoted 8 lines

+               case WRITE_10:
+                       ucd_cmd_ptr->exp_data_transfer_len =
+                               cpu_to_be32(lrbp->cmd->transfersize *
+                                           ((ucd_cmd_ptr->cdb[7] << 8) |
+                                            ucd_cmd_ptr->cdb[8]));
+                       break;
+               case READ_12:
+               case WRITE_12:

Just a minor change, this will be READ_16 and WRITE_16.

Quoted 15 lines

+                       ucd_cmd_ptr->exp_data_transfer_len =
+                               cpu_to_be32(lrbp->cmd->transfersize *
+                                           ((ucd_cmd_ptr->cdb[10] << 24) |
+                                            (ucd_cmd_ptr->cdb[11] << 16) |
+                                            (ucd_cmd_ptr->cdb[12] << 8) |
+                                            ucd_cmd_ptr->cdb[13]));
+                       break;
+               default:
+                       break;
+               }
               break;
       case UTP_CMD_TYPE_DEV_MANAGE:

               /* For query function implementation */

Signature · 1 line
~Santosh
#8 lore raw

Re: [PATCH v3 2/5] [SCSI] ufshcd: UFS UTP Transfer requests handling

Quoted 4 lines · Santosh Y

drivers/scsi/ufs/ufshcd.c static void ufshcd_compose_upiu(struct ufshcd_lrb *lrbp) · line 667

Quoted 2 lines
drivers/scsi/ufs/ufshcd.c +2 -0
+               case READ_12:
+               case WRITE_12:

Just a minor change, this will be READ_16 and WRITE_16.

Thanks. Your feedback will be correct.

#9 lore raw

Re: [PATCH v3 2/5] [SCSI] ufshcd: UFS UTP Transfer requests handling

In reply to KOBAYASHI Yoshitake
Quoted 12 lines · KOBAYASHI Yoshitake
Quoted 9 lines · Santosh Y

+        ucd_cmd_ptr->exp_data_transfer_len =
+            cpu_to_be32(lrbp->cmd->transfersize); +
+        memcpy(ucd_cmd_ptr->cdb,
+               lrbp->cmd->cmnd,
+               (min_t(unsigned short,
+                  lrbp->cmd->cmd_len,
+                  MAX_CDB_SIZE)));

Quoted 9 lines

"Exptected Data Transfer Length" field contains a value that represents the number of bytes that are required to complete the SCSI command request and the number of bytes that the Initiator expects to be transferred to/from the Target.
(JEDEC Standard 220 Table7-7)
On the other hand, "transfersize" in a scsi_cmnd struct is trimmed to the sector size when requested transfer length exceeds it. You may derive the actual transfer length from SCSI command itself like following.

Hi,
Can you please send signed-off patches for the two issues that you reported ?
I would like to use them on my development tree.

Thanks and regards,
Venkat.

Quoted 44 lines
drivers/scsi/ufs/ufshcd.c +2 -0
index feffe65..e63fecb 100644
@@ -647,6 +647,34 @@ static void ufshcd_compose_upiu(struct ufshcd_lrb
 *lrbp)
                       (min_t(unsigned short,
                              lrbp->cmd->cmd_len,
                              MAX_CDB_SIZE)));
+
+               /* Overwrite exptected transfer length by using

TRANSFER_LENGTH in SCSI commands */
+               switch(ucd_cmd_ptr->cdb[0]) {
+               case READ_6:
+               case WRITE_6:
+                       ucd_cmd_ptr->exp_data_transfer_len =
+                               cpu_to_be32(lrbp->cmd->transfersize *
+                                           ucd_cmd_ptr->cdb[4]);
+                       break;
+               case READ_10:
+               case WRITE_10:
+                       ucd_cmd_ptr->exp_data_transfer_len =
+                               cpu_to_be32(lrbp->cmd->transfersize *
+                                           ((ucd_cmd_ptr->cdb[7] << 8) |
+                                            ucd_cmd_ptr->cdb[8]));
+                       break;
+               case READ_12:
+               case WRITE_12:
+                       ucd_cmd_ptr->exp_data_transfer_len =
+                               cpu_to_be32(lrbp->cmd->transfersize *
+                                           ((ucd_cmd_ptr->cdb[10] << 24) |
+                                            (ucd_cmd_ptr->cdb[11] << 16) |
+                                            (ucd_cmd_ptr->cdb[12] << 8) |
+                                            ucd_cmd_ptr->cdb[13]));
+                       break;
+               default:
+                       break;
+               }
               break;
       case UTP_CMD_TYPE_DEV_MANAGE:

               /* For query function implementation */

author
#10 lore raw

PATCH v3 3/5 ufshcd: UFSHCI error handling

In reply to Santosh Y

From: Santosh Yaraganavi <[email protected]>

UFSHCI error handling includes support for:
- UFS host controller errors
- System bus errors
- Unipro errors

---
v2 -> v3:
- use shost_priv() instead of accessing hostdata directly.
v1 -> v2:
- ufshcd_do_reset(): change return value to SUCCESS/FAILED, since the function can also be called from UFSHCD registered SCSI midlayer error handling routines.
Ex: .eh_host_reset_handler which expects return value SUCCESS/FAILED
- ufshcd_do_reset(): aquire spin_lock_irqsave only for instructions which share common data structure.

 drivers/scsi/ufs/ufshcd.c |   97 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 97 insertions(+), 0 deletions(-)
drivers/scsi/ufs/ufshcd.c +97 -0
index 43b9c62..fa2fa96 100644
@@ -138,6 +138,8 @@ struct uic_command {
  * @ufshcd_state: UFSHCD states
  * @int_enable_mask: Interrupt Mask Bits
  * @uic_workq: Work queue for UIC completion handling
+ * @feh_workq: Work queue for fatal controller error handling
+ * @errors: HBA errors
  */
 struct ufs_hba {
 	void __iomem *mmio_base;
@@ -171,6 +173,10 @@ struct ufs_hba {
 
 	/* Work Queues */
 	struct work_struct uic_workq;
+	struct work_struct feh_workq;
+
+	/* HBA Errors */
+	u32 errors;
 };
 
 /**
@@ -893,6 +899,9 @@ static int ufshcd_make_hba_operational(struct ufs_hba *hba)
 	/* Configure interrupt aggregation */
 	ufshcd_config_int_aggr(hba, INT_AGGR_CONFIG);
 
+	if (hba->ufshcd_state == UFSHCD_STATE_RESET)
+		scsi_unblock_requests(hba->host);
+
 	hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
 	scsi_scan_host(hba->host);
 out:
@@ -994,6 +1003,51 @@ static int ufshcd_initialize_hba(struct ufs_hba *hba)
 }
 
 /**
+ * ufshcd_do_reset - reset the host controller
+ * @hba: per adapter instance
+ *
+ * Returns SUCCESS/FAILED
+ */
+static int ufshcd_do_reset(struct ufs_hba *hba)
+{
+	struct ufshcd_lrb *lrbp;
+	unsigned long flags;
+	int tag;
+
+	/* block commands from midlayer */
+	scsi_block_requests(hba->host);
+
+	spin_lock_irqsave(hba->host->host_lock, flags);
+	hba->ufshcd_state = UFSHCD_STATE_RESET;
+
+	/* send controller to reset state */
+	ufshcd_hba_stop(hba);
+	spin_unlock_irqrestore(hba->host->host_lock, flags);
+
+	/* abort outstanding commands */
+	for (tag = 0; tag < hba->nutrs; tag++) {
+		if (test_bit(tag, &hba->outstanding_reqs)) {
+			lrbp = &hba->lrb[tag];
+			scsi_dma_unmap(lrbp->cmd);
+			lrbp->cmd->result = DID_RESET << 16;
+			lrbp->cmd->scsi_done(lrbp->cmd);
+			lrbp->cmd = NULL;
+		}
+	}
+
+	/* clear outstanding request bit maps */
+	hba->outstanding_reqs = 0;
+
+	/* start the initialization process */
+	if (ufshcd_initialize_hba(hba)) {
+		dev_err(&hba->pdev->dev,
+			"Reset: Controller initialization failed\n");
+		return FAILED;
+	}
+	return SUCCESS;
+}
+
+/**
  * ufshcd_slave_alloc - handle initial SCSI device configurations
  * @sdev: pointer to SCSI device
  *
@@ -1240,12 +1294,54 @@ static void ufshcd_uic_cc_handler (struct work_struct *work)
 }
 
 /**
+ * ufshcd_fatal_err_handler - handle fatal errors
+ * @hba: per adapter instance
+ */
+static void ufshcd_fatal_err_handler(struct work_struct *work)
+{
+	struct ufs_hba *hba;
+	hba = container_of(work, struct ufs_hba, feh_workq);
+
+	/* check if reset is already in progress */
+	if (hba->ufshcd_state != UFSHCD_STATE_RESET)
+		ufshcd_do_reset(hba);
+}
+
+/**
+ * ufshcd_err_handler - Check for fatal errors
+ * @work: pointer to a work queue structure
+ */
+static void ufshcd_err_handler(struct ufs_hba *hba)
+{
+	u32 reg;
+
+	if (hba->errors & INT_FATAL_ERRORS)
+		goto fatal_eh;
+
+	if (hba->errors & UIC_ERROR) {
+
+		reg = readl(hba->mmio_base +
+			    REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
+		if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
+			goto fatal_eh;
+	}
+	return;
+fatal_eh:
+	hba->ufshcd_state = UFSHCD_STATE_ERROR;
+	schedule_work(&hba->feh_workq);
+}
+
+/**
  * ufshcd_sl_intr - Interrupt service routine
  * @hba: per adapter instance
  * @intr_status: contains interrupts generated by the controller
  */
 static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
 {
+	hba->errors = UFSHCD_ERROR_MASK & intr_status;
+	if (hba->errors)
+		ufshcd_err_handler(hba);
+
 	if (intr_status & UIC_COMMAND_COMPL)
 		schedule_work(&hba->uic_workq);
 
@@ -1492,6 +1588,7 @@ ufshcd_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
 	/* Initialize work queues */
 	INIT_WORK(&hba->uic_workq, ufshcd_uic_cc_handler);
+	INIT_WORK(&hba->feh_workq, ufshcd_fatal_err_handler);
 
 	/* IRQ registration */
 	err = request_irq(pdev->irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Signature · 1 line
1.7.5.4
author
#11 lore raw

PATCH v3 4/5 ufshcd: SCSI error handling

In reply to Santosh Y

From: Santosh Yaraganavi <[email protected]>

UFSHCD SCSI error handling includes following implementations,
- Abort task
- Device reset
- Host reset

---
v2 -> v3:
- use shost_priv() instead of accessing hostdata directly.
- change ufshcd_is_tmq_full() to ufshcd_get_tm_free_slot() to provide correct meaning, according to its functionality.
v1 -> v2:
- Change task management function return value to SUCCESS/FAILED, SCSI midlayer expects SUCCESS/FAILED return value for error handling routines.
Ex: .eh_host_reset_handler, eh_abort_handler...
- ufshcd_tmc_handler(): use wake_up_interruptible() outside for loop to wake up all the waiting threads at once.
Change hba->tm_condition[] to hba->tm_condition bitmask.
- ufshcd_abort(): add missing spin_unlock_irqrestore().

 drivers/scsi/ufs/ufshcd.c |  298 ++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 297 insertions(+), 1 deletions(-)
drivers/scsi/ufs/ufshcd.c +297 -1
index fa2fa96..07ec973 100644
@@ -65,6 +65,7 @@
 #include <scsi/scsi_host.h>
 #include <scsi/scsi_tcq.h>
 #include <scsi/scsi_dbg.h>
+#include <scsi/scsi_eh.h>
 
 #include "ufs.h"
 #include "ufshci.h"
@@ -130,11 +131,14 @@ struct uic_command {
  * @host: Scsi_Host instance of the driver
  * @pdev: PCI device handle
  * @lrb: local reference block
+ * @outstanding_tasks: Bits representing outstanding task requests
  * @outstanding_reqs: Bits representing outstanding transfer requests
  * @capabilities: UFS Controller Capabilities
  * @nutrs: Transfer Request Queue depth supported by controller
  * @nutmrs: Task Management Queue depth supported by controller
  * @active_uic_cmd: handle of active UIC command
+ * @ufshcd_tm_wait_queue: wait queue for task management
+ * @tm_condition: condition variable for task management
  * @ufshcd_state: UFSHCD states
  * @int_enable_mask: Interrupt Mask Bits
  * @uic_workq: Work queue for UIC completion handling
@@ -159,6 +163,7 @@ struct ufs_hba {
 
 	struct ufshcd_lrb *lrb;
 
+	unsigned long outstanding_tasks;
 	unsigned long outstanding_reqs;
 
 	u32 capabilities;
@@ -167,6 +172,8 @@ struct ufs_hba {
 	u32 ufs_version;
 
 	struct uic_command active_uic_cmd;
+	wait_queue_head_t ufshcd_tm_wait_queue;
+	unsigned long tm_condition;
 
 	u32 ufshcd_state;
 	u32 int_enable_mask;
@@ -245,6 +252,42 @@ static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
 }
 
 /**
+ * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
+ * @task_req_descp: pointer to utp_task_req_desc structure
+ *
+ * This function is used to get the OCS field from UTMRD
+ * Returns the OCS field in the UTMRD
+ */
+static inline int
+ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
+{
+	return task_req_descp->header.dword_2 & MASK_OCS;
+}
+
+/**
+ * ufshcd_get_tm_free_slot - get a free slot for task management request
+ * @hba: per adapter instance
+ *
+ * Returns maximum number of task management request slots in case of
+ * task management queue full or returns the free slot number
+ */
+static inline int ufshcd_get_tm_free_slot(struct ufs_hba *hba)
+{
+	return find_first_zero_bit(&hba->outstanding_tasks, hba->nutmrs);
+}
+
+/**
+ * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
+ * @hba: per adapter instance
+ * @pos: position of the bit to be cleared
+ */
+static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
+{
+	writel(~(1 << pos),
+		(hba->mmio_base + REG_UTP_TRANSFER_REQ_LIST_CLEAR));
+}
+
+/**
  * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
  * @reg: Register value of host controller status
  *
@@ -1035,8 +1078,9 @@ static int ufshcd_do_reset(struct ufs_hba *hba)
 		}
 	}
 
-	/* clear outstanding request bit maps */
+	/* clear outstanding request/task bit maps */
 	hba->outstanding_reqs = 0;
+	hba->outstanding_tasks = 0;
 
 	/* start the initialization process */
 	if (ufshcd_initialize_hba(hba)) {
@@ -1088,6 +1132,47 @@ static void ufshcd_slave_destroy(struct scsi_device *sdev)
 }
 
 /**
+ * ufshcd_task_req_compl - handle task management request completion
+ * @hba: per adapter instance
+ * @index: index of the completed request
+ *
+ * Returns SUCCESS/FAILED
+ */
+static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index)
+{
+	struct utp_task_req_desc *task_req_descp;
+	struct utp_upiu_task_rsp *task_rsp_upiup;
+	unsigned long flags;
+	int ocs_value;
+	int task_result;
+
+	spin_lock_irqsave(hba->host->host_lock, flags);
+
+	/* Clear completed tasks from outstanding_tasks */
+	__clear_bit(index, &hba->outstanding_tasks);
+
+	task_req_descp = hba->utmrdl_base_addr;
+	ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
+
+	if (ocs_value == OCS_SUCCESS) {
+		task_rsp_upiup = (struct utp_upiu_task_rsp *)
+				task_req_descp[index].task_rsp_upiu;
+		task_result = be32_to_cpu(task_rsp_upiup->header.dword_1);
+		task_result = ((task_result & MASK_TASK_RESPONSE) >> 8);
+
+		if (task_result != UPIU_TASK_MANAGEMENT_FUNC_COMPL ||
+		    task_result != UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED)
+			task_result = FAILED;
+	} else {
+		task_result = FAILED;
+		dev_err(&hba->pdev->dev,
+			"trc: Invalid ocs = %x\n", ocs_value);
+	}
+	spin_unlock_irqrestore(hba->host->host_lock, flags);
+	return task_result;
+}
+
+/**
  * ufshcd_adjust_lun_qdepth - Update LUN queue depth if device responds with
  *			      SAM_STAT_TASK_SET_FULL SCSI command status.
  * @cmd: pointer to SCSI command
@@ -1332,6 +1417,19 @@ fatal_eh:
 }
 
 /**
+ * ufshcd_tmc_handler - handle task management function completion
+ * @hba: per adapter instance
+ */
+static void ufshcd_tmc_handler(struct ufs_hba *hba)
+{
+	u32 tm_doorbell;
+
+	tm_doorbell = readl(hba->mmio_base + REG_UTP_TASK_REQ_DOOR_BELL);
+	hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
+	wake_up_interruptible(&hba->ufshcd_tm_wait_queue);
+}
+
+/**
  * ufshcd_sl_intr - Interrupt service routine
  * @hba: per adapter instance
  * @intr_status: contains interrupts generated by the controller
@@ -1345,6 +1443,9 @@ static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
 	if (intr_status & UIC_COMMAND_COMPL)
 		schedule_work(&hba->uic_workq);
 
+	if (intr_status & UTP_TASK_REQ_COMPL)
+		ufshcd_tmc_handler(hba);
+
 	if (intr_status & UTP_TRANSFER_REQ_COMPL)
 		ufshcd_transfer_req_compl(hba);
 }
@@ -1379,6 +1480,195 @@ static irqreturn_t ufshcd_intr(int irq, void *__hba)
 	return retval;
 }
 
+/**
+ * ufshcd_issue_tm_cmd - issues task management commands to controller
+ * @hba: per adapter instance
+ * @lrbp: pointer to local reference block
+ *
+ * Returns SUCCESS/FAILED
+ */
+static int
+ufshcd_issue_tm_cmd(struct ufs_hba *hba,
+		    struct ufshcd_lrb *lrbp,
+		    u8 tm_function)
+{
+	struct utp_task_req_desc *task_req_descp;
+	struct utp_upiu_task_req *task_req_upiup;
+	struct Scsi_Host *host;
+	unsigned long flags;
+	int free_slot = 0;
+	int err;
+
+	host = hba->host;
+
+	spin_lock_irqsave(host->host_lock, flags);
+
+	/* If task management queue is full */
+	free_slot = ufshcd_get_tm_free_slot(hba);
+	if (free_slot >= hba->nutmrs) {
+		spin_unlock_irqrestore(host->host_lock, flags);
+		dev_err(&hba->pdev->dev, "Task management queue full\n");
+		err = FAILED;
+		goto out;
+	}
+
+	task_req_descp = hba->utmrdl_base_addr;
+	task_req_descp += free_slot;
+
+	/* Configure task request descriptor */
+	task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
+	task_req_descp->header.dword_2 =
+			cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
+
+	/* Configure task request UPIU */
+	task_req_upiup =
+		(struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
+	task_req_upiup->header.dword_0 =
+		cpu_to_be32(UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
+					      lrbp->lun, lrbp->task_tag));
+	task_req_upiup->header.dword_1 =
+	cpu_to_be32(UPIU_HEADER_DWORD(0, tm_function, 0, 0));
+
+	task_req_upiup->input_param1 = lrbp->lun;
+	task_req_upiup->input_param1 =
+		cpu_to_be32(task_req_upiup->input_param1);
+	task_req_upiup->input_param2 = lrbp->task_tag;
+	task_req_upiup->input_param2 =
+		cpu_to_be32(task_req_upiup->input_param2);
+
+	/* send command to the controller */
+	__set_bit(free_slot, &hba->outstanding_tasks);
+	writel((1 << free_slot),
+	       (hba->mmio_base + REG_UTP_TASK_REQ_DOOR_BELL));
+
+	spin_unlock_irqrestore(host->host_lock, flags);
+
+	/* wait until the task management command is completed */
+	err =
+	wait_event_interruptible_timeout(hba->ufshcd_tm_wait_queue,
+					 (test_bit(free_slot,
+					 &hba->tm_condition) != 0),
+					 60 * HZ);
+	if (!err) {
+		dev_err(&hba->pdev->dev,
+			"Task management command timed-out\n");
+		err = FAILED;
+		goto out;
+	}
+	clear_bit(free_slot, &hba->tm_condition);
+	return ufshcd_task_req_compl(hba, free_slot);
+out:
+	return err;
+}
+
+/**
+ * ufshcd_device_reset - reset device and abort all the pending commands
+ * @cmd: SCSI command pointer
+ *
+ * Returns SUCCESS/FAILED
+ */
+static int ufshcd_device_reset(struct scsi_cmnd *cmd)
+{
+	struct Scsi_Host *host;
+	struct ufs_hba *hba;
+	unsigned int tag;
+	u32 pos;
+	int err;
+
+	host = cmd->device->host;
+	hba = shost_priv(host);
+	tag = cmd->request->tag;
+
+	err = ufshcd_issue_tm_cmd(hba, &hba->lrb[tag], UFS_LOGICAL_RESET);
+	if (err)
+		goto out;
+
+	for (pos = 0; pos < hba->nutrs; pos++) {
+		if (test_bit(pos, &hba->outstanding_reqs) &&
+		    (hba->lrb[tag].lun == hba->lrb[pos].lun)) {
+
+			/* clear the respective UTRLCLR register bit */
+			ufshcd_utrl_clear(hba, pos);
+
+			clear_bit(pos, &hba->outstanding_reqs);
+
+			if (hba->lrb[pos].cmd) {
+				scsi_dma_unmap(hba->lrb[pos].cmd);
+				hba->lrb[pos].cmd->result =
+						DID_ABORT << 16;
+				hba->lrb[pos].cmd->scsi_done(cmd);
+				hba->lrb[pos].cmd = NULL;
+			}
+		}
+	} /* end of for */
+out:
+	return err;
+}
+
+/**
+ * ufshcd_host_reset - Main reset function registered with scsi layer
+ * @cmd: SCSI command pointer
+ *
+ * Returns SUCCESS/FAILED
+ */
+static int ufshcd_host_reset(struct scsi_cmnd *cmd)
+{
+	struct ufs_hba *hba;
+
+	hba = shost_priv(cmd->device->host);
+
+	if (hba->ufshcd_state == UFSHCD_STATE_RESET)
+		return SUCCESS;
+
+	return (ufshcd_do_reset(hba) == SUCCESS) ? SUCCESS : FAILED;
+}
+
+/**
+ * ufshcd_abort - abort a specific command
+ * @cmd: SCSI command pointer
+ *
+ * Returns SUCCESS/FAILED
+ */
+static int ufshcd_abort(struct scsi_cmnd *cmd)
+{
+	struct Scsi_Host *host;
+	struct ufs_hba *hba;
+	unsigned long flags;
+	unsigned int tag;
+	int err;
+
+	host = cmd->device->host;
+	hba = shost_priv(host);
+	tag = cmd->request->tag;
+
+	spin_lock_irqsave(host->host_lock, flags);
+
+	/* check if command is still pending */
+	if (!(test_bit(tag, &hba->outstanding_reqs))) {
+		err = FAILED;
+		spin_unlock_irqrestore(host->host_lock, flags);
+		goto out;
+	}
+	spin_unlock_irqrestore(host->host_lock, flags);
+
+	err = ufshcd_issue_tm_cmd(hba, &hba->lrb[tag], UFS_ABORT_TASK);
+	if (err)
+		goto out;
+
+	scsi_dma_unmap(cmd);
+
+	spin_lock_irqsave(host->host_lock, flags);
+
+	/* clear the respective UTRLCLR register bit */
+	ufshcd_utrl_clear(hba, tag);
+
+	__clear_bit(tag, &hba->outstanding_reqs);
+	hba->lrb[tag].cmd = NULL;
+	spin_unlock_irqrestore(host->host_lock, flags);
+out:
+	return err;
+}
+
 static struct scsi_host_template ufshcd_driver_template = {
 	.module			= THIS_MODULE,
 	.name			= UFSHCD,
@@ -1386,6 +1676,9 @@ static struct scsi_host_template ufshcd_driver_template = {
 	.queuecommand		= ufshcd_queuecommand,
 	.slave_alloc		= ufshcd_slave_alloc,
 	.slave_destroy		= ufshcd_slave_destroy,
+	.eh_abort_handler	= ufshcd_abort,
+	.eh_device_reset_handler = ufshcd_device_reset,
+	.eh_host_reset_handler	= ufshcd_host_reset,
 	.this_id		= -1,
 	.sg_tablesize		= SG_ALL,
 	.cmd_per_lun		= UFSHCD_CMD_PER_LUN,
@@ -1586,6 +1879,9 @@ ufshcd_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	host->unique_id = host->host_no;
 	host->max_cmd_len = MAX_CDB_SIZE;
 
+	/* Initailize wait queue for task management */
+	init_waitqueue_head(&hba->ufshcd_tm_wait_queue);
+
 	/* Initialize work queues */
 	INIT_WORK(&hba->uic_workq, ufshcd_uic_cc_handler);
 	INIT_WORK(&hba->feh_workq, ufshcd_fatal_err_handler);
Signature · 1 line
1.7.5.4
author
#12 lore raw

PATCH v3 5/5 Documentation: UFS Host Controller Driver

In reply to Santosh Y

From: Santosh Yaraganavi <[email protected]>

The document provides Universal Flash Storage(UFS) and UFS host controller driver overview.

---
 Documentation/scsi/00-INDEX |    2 +
 Documentation/scsi/ufs.txt  |  133 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 135 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/scsi/ufs.txt
Documentation/scsi/00-INDEX +2 -0
index b48ded5..b7dd650 100644
@@ -94,3 +94,5 @@ sym53c8xx_2.txt
 	- info on second generation driver for sym53c8xx based adapters
 tmscsim.txt
 	- info on driver for AM53c974 based adapters
+ufs.txt
+	- info on Universal Flash Storage(UFS) and UFS host controller driver.
Documentation/scsi/ufs.txt new file +133 -0

133 lines not on this page · raw message

Signature · 1 line
1.7.5.4
maintainer
#13 lore raw
In reply to Santosh Y
Quoted 10 lines · Santosh Y

From: Santosh Yaraganavi <[email protected]>

UFS:
Universal Flash Storage is a storage specification for flash devices. It is aimed to provide a universal storage interface for both embedded and removable flash memory based storage in mobile devices such as smart phones and tablet computers. The specification is defined by JEDEC Solid State Technology Association. UFS is based on MIPI M-PHY physical layer standard. UFS uses MIPI M-PHY as the physical layer and MIPI Unipro as the link layer.

This doesn't compile correctly on a 32 bit platform:

CC [M] drivers/scsi/ufs/ufshcd.o
drivers/scsi/ufs/ufshcd.c: In function ‘ufshcd_map_sg’:
drivers/scsi/ufs/ufshcd.c:555:5: warning: right shift count >= width of type [enabled by default]
drivers/scsi/ufs/ufshcd.c: In function ‘ufshcd_host_memory_configure’:
drivers/scsi/ufs/ufshcd.c:831:5: warning: right shift count >= width of type [enabled by default]
drivers/scsi/ufs/ufshcd.c: In function ‘ufshcd_initialize_hba’:
drivers/scsi/ufs/ufshcd.c:1037:2: warning: right shift count >= width of type [enabled by default]
drivers/scsi/ufs/ufshcd.c:1041:2: warning: right shift count >= width of type [enabled by default]

That's because you use >> 32 for the upper address bits.

I did the replacement below to use macros which do this correctly (basically trick the compiler).

James

---

drivers/scsi/ufs/ufshcd.c +6 -6
index 07ec973..52b96e8 100644
@@ -550,9 +550,9 @@ static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
 			prd_table[i].size  =
 				cpu_to_le32(((u32) sg_dma_len(sg))-1);
 			prd_table[i].base_addr =
-				cpu_to_le32(sg->dma_address);
+				cpu_to_le32(lower_32_bits(sg->dma_address));
 			prd_table[i].upper_addr =
-				cpu_to_le32((sg->dma_address >> 32));
+				cpu_to_le32(upper_32_bits(sg->dma_address));
 		}
 	} else {
 		lrbp->utr_descriptor_ptr->prd_table_length = 0;
@@ -826,9 +826,9 @@ static void ufshcd_host_memory_configure(struct ufs_hba *hba)
 		cmd_desc_element_addr =
 				(cmd_desc_dma_addr + (cmd_desc_size * i));
 		utrdlp[i].command_desc_base_addr_lo =
-				cpu_to_le32(cmd_desc_element_addr);
+				cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
 		utrdlp[i].command_desc_base_addr_hi =
-				cpu_to_le32(cmd_desc_element_addr >> 32);
+				cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
 
 		/* Response upiu and prdt offset should be in double words */
 		utrdlp[i].response_upiu_offset =
@@ -1034,11 +1034,11 @@ static int ufshcd_initialize_hba(struct ufs_hba *hba)
 	/* Configure UTRL and UTMRL base address registers */
 	writel(hba->utrdl_dma_addr,
 	       (hba->mmio_base + REG_UTP_TRANSFER_REQ_LIST_BASE_L));
-	writel((hba->utrdl_dma_addr >> 32),
+	writel(lower_32_bits(hba->utrdl_dma_addr),
 	       (hba->mmio_base + REG_UTP_TRANSFER_REQ_LIST_BASE_H));
 	writel(hba->utmrdl_dma_addr,
 	       (hba->mmio_base + REG_UTP_TASK_REQ_LIST_BASE_L));
-	writel((hba->utmrdl_dma_addr >> 32),
+	writel(upper_32_bits(hba->utmrdl_dma_addr),
 	       (hba->mmio_base + REG_UTP_TASK_REQ_LIST_BASE_H));
 
 	/* Initialize unipro link startup procedure */