aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/ufs
diff options
context:
space:
mode:
authorSujit Reddy Thumma <sthumma@codeaurora.org>2013-07-29 15:05:57 -0400
committerJames Bottomley <JBottomley@Parallels.com>2013-08-26 04:50:39 -0400
commit5a0b0cb9bee767ef10ff9ce2fb4141af06416288 (patch)
tree42d6f09b2c724885e2b6ada7778789548fb9ae6f /drivers/scsi/ufs
parent5f09e1f310a57fd500d6a05e3e108c432691e2dc (diff)
[SCSI] ufs: Add support for sending NOP OUT UPIU
As part of device initialization sequence, sending NOP OUT UPIU and waiting for NOP IN UPIU response is mandatory. This confirms that the device UFS Transport (UTP) layer is functional and the host can configure the device with further commands. Add support for sending NOP OUT UPIU to check the device connection path and test whether the UTP layer on the device side is functional during initialization. A tag is acquired from the SCSI tag map space in order to send the device management command. When the tag is acquired by internal command the scsi command is rejected with host busy flag in order to requeue the request. To avoid frequent collisions between internal commands and scsi commands the device management command tag is allocated in the opposite direction w.r.t block layer tag allocation. Signed-off-by: Sujit Reddy Thumma <sthumma@codeaurora.org> Signed-off-by: Dolev Raviv <draviv@codeaurora.org> Signed-off-by: Santosh Y <santoshsy@gmail.com> Signed-off-by: James Bottomley <JBottomley@Parallels.com>
Diffstat (limited to 'drivers/scsi/ufs')
-rw-r--r--drivers/scsi/ufs/ufs.h40
-rw-r--r--drivers/scsi/ufs/ufshcd.c585
-rw-r--r--drivers/scsi/ufs/ufshcd.h29
3 files changed, 539 insertions, 115 deletions
diff --git a/drivers/scsi/ufs/ufs.h b/drivers/scsi/ufs/ufs.h
index 139bc0647b41..51b5e3f72bcb 100644
--- a/drivers/scsi/ufs/ufs.h
+++ b/drivers/scsi/ufs/ufs.h
@@ -36,10 +36,13 @@
36#ifndef _UFS_H 36#ifndef _UFS_H
37#define _UFS_H 37#define _UFS_H
38 38
39#include <linux/mutex.h>
40#include <linux/types.h>
41
39#define MAX_CDB_SIZE 16 42#define MAX_CDB_SIZE 16
40 43
41#define UPIU_HEADER_DWORD(byte3, byte2, byte1, byte0)\ 44#define UPIU_HEADER_DWORD(byte3, byte2, byte1, byte0)\
42 ((byte3 << 24) | (byte2 << 16) |\ 45 cpu_to_be32((byte3 << 24) | (byte2 << 16) |\
43 (byte1 << 8) | (byte0)) 46 (byte1 << 8) | (byte0))
44 47
45/* 48/*
@@ -73,6 +76,7 @@ enum {
73 UPIU_TRANSACTION_TASK_RSP = 0x24, 76 UPIU_TRANSACTION_TASK_RSP = 0x24,
74 UPIU_TRANSACTION_READY_XFER = 0x31, 77 UPIU_TRANSACTION_READY_XFER = 0x31,
75 UPIU_TRANSACTION_QUERY_RSP = 0x36, 78 UPIU_TRANSACTION_QUERY_RSP = 0x36,
79 UPIU_TRANSACTION_REJECT_UPIU = 0x3F,
76}; 80};
77 81
78/* UPIU Read/Write flags */ 82/* UPIU Read/Write flags */
@@ -110,6 +114,12 @@ enum {
110 UPIU_COMMAND_SET_TYPE_QUERY = 0x2, 114 UPIU_COMMAND_SET_TYPE_QUERY = 0x2,
111}; 115};
112 116
117/* UTP Transfer Request Command Offset */
118#define UPIU_COMMAND_TYPE_OFFSET 28
119
120/* Offset of the response code in the UPIU header */
121#define UPIU_RSP_CODE_OFFSET 8
122
113enum { 123enum {
114 MASK_SCSI_STATUS = 0xFF, 124 MASK_SCSI_STATUS = 0xFF,
115 MASK_TASK_RESPONSE = 0xFF00, 125 MASK_TASK_RESPONSE = 0xFF00,
@@ -138,26 +148,32 @@ struct utp_upiu_header {
138 148
139/** 149/**
140 * struct utp_upiu_cmd - Command UPIU structure 150 * struct utp_upiu_cmd - Command UPIU structure
141 * @header: UPIU header structure DW-0 to DW-2
142 * @data_transfer_len: Data Transfer Length DW-3 151 * @data_transfer_len: Data Transfer Length DW-3
143 * @cdb: Command Descriptor Block CDB DW-4 to DW-7 152 * @cdb: Command Descriptor Block CDB DW-4 to DW-7
144 */ 153 */
145struct utp_upiu_cmd { 154struct utp_upiu_cmd {
146 struct utp_upiu_header header;
147 u32 exp_data_transfer_len; 155 u32 exp_data_transfer_len;
148 u8 cdb[MAX_CDB_SIZE]; 156 u8 cdb[MAX_CDB_SIZE];
149}; 157};
150 158
151/** 159/**
152 * struct utp_upiu_rsp - Response UPIU structure 160 * struct utp_upiu_req - general upiu request structure
153 * @header: UPIU header DW-0 to DW-2 161 * @header:UPIU header structure DW-0 to DW-2
162 * @sc: fields structure for scsi command DW-3 to DW-7
163 */
164struct utp_upiu_req {
165 struct utp_upiu_header header;
166 struct utp_upiu_cmd sc;
167};
168
169/**
170 * struct utp_cmd_rsp - Response UPIU structure
154 * @residual_transfer_count: Residual transfer count DW-3 171 * @residual_transfer_count: Residual transfer count DW-3
155 * @reserved: Reserved double words DW-4 to DW-7 172 * @reserved: Reserved double words DW-4 to DW-7
156 * @sense_data_len: Sense data length DW-8 U16 173 * @sense_data_len: Sense data length DW-8 U16
157 * @sense_data: Sense data field DW-8 to DW-12 174 * @sense_data: Sense data field DW-8 to DW-12
158 */ 175 */
159struct utp_upiu_rsp { 176struct utp_cmd_rsp {
160 struct utp_upiu_header header;
161 u32 residual_transfer_count; 177 u32 residual_transfer_count;
162 u32 reserved[4]; 178 u32 reserved[4];
163 u16 sense_data_len; 179 u16 sense_data_len;
@@ -165,6 +181,16 @@ struct utp_upiu_rsp {
165}; 181};
166 182
167/** 183/**
184 * struct utp_upiu_rsp - general upiu response structure
185 * @header: UPIU header structure DW-0 to DW-2
186 * @sr: fields structure for scsi command DW-3 to DW-12
187 */
188struct utp_upiu_rsp {
189 struct utp_upiu_header header;
190 struct utp_cmd_rsp sr;
191};
192
193/**
168 * struct utp_upiu_task_req - Task request UPIU structure 194 * struct utp_upiu_task_req - Task request UPIU structure
169 * @header - UPIU header structure DW0 to DW-2 195 * @header - UPIU header structure DW0 to DW-2
170 * @input_param1: Input parameter 1 DW-3 196 * @input_param1: Input parameter 1 DW-3
diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
index b743bd6fce6b..ed2015672982 100644
--- a/drivers/scsi/ufs/ufshcd.c
+++ b/drivers/scsi/ufs/ufshcd.c
@@ -43,6 +43,11 @@
43/* UIC command timeout, unit: ms */ 43/* UIC command timeout, unit: ms */
44#define UIC_CMD_TIMEOUT 500 44#define UIC_CMD_TIMEOUT 500
45 45
46/* NOP OUT retries waiting for NOP IN response */
47#define NOP_OUT_RETRIES 10
48/* Timeout after 30 msecs if NOP OUT hangs without response */
49#define NOP_OUT_TIMEOUT 30 /* msecs */
50
46enum { 51enum {
47 UFSHCD_MAX_CHANNEL = 0, 52 UFSHCD_MAX_CHANNEL = 0,
48 UFSHCD_MAX_ID = 1, 53 UFSHCD_MAX_ID = 1,
@@ -71,6 +76,40 @@ enum {
71 INT_AGGR_CONFIG, 76 INT_AGGR_CONFIG,
72}; 77};
73 78
79/*
80 * ufshcd_wait_for_register - wait for register value to change
81 * @hba - per-adapter interface
82 * @reg - mmio register offset
83 * @mask - mask to apply to read register value
84 * @val - wait condition
85 * @interval_us - polling interval in microsecs
86 * @timeout_ms - timeout in millisecs
87 *
88 * Returns -ETIMEDOUT on error, zero on success
89 */
90static int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
91 u32 val, unsigned long interval_us, unsigned long timeout_ms)
92{
93 int err = 0;
94 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
95
96 /* ignore bits that we don't intend to wait on */
97 val = val & mask;
98
99 while ((ufshcd_readl(hba, reg) & mask) != val) {
100 /* wakeup within 50us of expiry */
101 usleep_range(interval_us, interval_us + 50);
102
103 if (time_after(jiffies, timeout)) {
104 if ((ufshcd_readl(hba, reg) & mask) != val)
105 err = -ETIMEDOUT;
106 break;
107 }
108 }
109
110 return err;
111}
112
74/** 113/**
75 * ufshcd_get_intr_mask - Get the interrupt bit mask 114 * ufshcd_get_intr_mask - Get the interrupt bit mask
76 * @hba - Pointer to adapter instance 115 * @hba - Pointer to adapter instance
@@ -191,18 +230,13 @@ static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
191} 230}
192 231
193/** 232/**
194 * ufshcd_is_valid_req_rsp - checks if controller TR response is valid 233 * ufshcd_get_req_rsp - returns the TR response transaction type
195 * @ucd_rsp_ptr: pointer to response UPIU 234 * @ucd_rsp_ptr: pointer to response UPIU
196 *
197 * This function checks the response UPIU for valid transaction type in
198 * response field
199 * Returns 0 on success, non-zero on failure
200 */ 235 */
201static inline int 236static inline int
202ufshcd_is_valid_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr) 237ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
203{ 238{
204 return ((be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24) == 239 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
205 UPIU_TRANSACTION_RESPONSE) ? 0 : DID_ERROR << 16;
206} 240}
207 241
208/** 242/**
@@ -299,9 +333,9 @@ static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
299{ 333{
300 int len; 334 int len;
301 if (lrbp->sense_buffer) { 335 if (lrbp->sense_buffer) {
302 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sense_data_len); 336 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
303 memcpy(lrbp->sense_buffer, 337 memcpy(lrbp->sense_buffer,
304 lrbp->ucd_rsp_ptr->sense_data, 338 lrbp->ucd_rsp_ptr->sr.sense_data,
305 min_t(int, len, SCSI_SENSE_BUFFERSIZE)); 339 min_t(int, len, SCSI_SENSE_BUFFERSIZE));
306 } 340 }
307} 341}
@@ -519,76 +553,128 @@ static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
519} 553}
520 554
521/** 555/**
556 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
557 * descriptor according to request
558 * @lrbp: pointer to local reference block
559 * @upiu_flags: flags required in the header
560 * @cmd_dir: requests data direction
561 */
562static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
563 u32 *upiu_flags, enum dma_data_direction cmd_dir)
564{
565 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
566 u32 data_direction;
567 u32 dword_0;
568
569 if (cmd_dir == DMA_FROM_DEVICE) {
570 data_direction = UTP_DEVICE_TO_HOST;
571 *upiu_flags = UPIU_CMD_FLAGS_READ;
572 } else if (cmd_dir == DMA_TO_DEVICE) {
573 data_direction = UTP_HOST_TO_DEVICE;
574 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
575 } else {
576 data_direction = UTP_NO_DATA_TRANSFER;
577 *upiu_flags = UPIU_CMD_FLAGS_NONE;
578 }
579
580 dword_0 = data_direction | (lrbp->command_type
581 << UPIU_COMMAND_TYPE_OFFSET);
582 if (lrbp->intr_cmd)
583 dword_0 |= UTP_REQ_DESC_INT_CMD;
584
585 /* Transfer request descriptor header fields */
586 req_desc->header.dword_0 = cpu_to_le32(dword_0);
587
588 /*
589 * assigning invalid value for command status. Controller
590 * updates OCS on command completion, with the command
591 * status
592 */
593 req_desc->header.dword_2 =
594 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
595}
596
597/**
598 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
599 * for scsi commands
600 * @lrbp - local reference block pointer
601 * @upiu_flags - flags
602 */
603static
604void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
605{
606 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
607
608 /* command descriptor fields */
609 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
610 UPIU_TRANSACTION_COMMAND, upiu_flags,
611 lrbp->lun, lrbp->task_tag);
612 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
613 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
614
615 /* Total EHS length and Data segment length will be zero */
616 ucd_req_ptr->header.dword_2 = 0;
617
618 ucd_req_ptr->sc.exp_data_transfer_len =
619 cpu_to_be32(lrbp->cmd->sdb.length);
620
621 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd,
622 (min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE)));
623}
624
625static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
626{
627 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
628
629 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
630
631 /* command descriptor fields */
632 ucd_req_ptr->header.dword_0 =
633 UPIU_HEADER_DWORD(
634 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
635}
636
637/**
522 * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU) 638 * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
639 * @hba - per adapter instance
523 * @lrb - pointer to local reference block 640 * @lrb - pointer to local reference block
524 */ 641 */
525static void ufshcd_compose_upiu(struct ufshcd_lrb *lrbp) 642static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
526{ 643{
527 struct utp_transfer_req_desc *req_desc;
528 struct utp_upiu_cmd *ucd_cmd_ptr;
529 u32 data_direction;
530 u32 upiu_flags; 644 u32 upiu_flags;
531 645 int ret = 0;
532 ucd_cmd_ptr = lrbp->ucd_cmd_ptr;
533 req_desc = lrbp->utr_descriptor_ptr;
534 646
535 switch (lrbp->command_type) { 647 switch (lrbp->command_type) {
536 case UTP_CMD_TYPE_SCSI: 648 case UTP_CMD_TYPE_SCSI:
537 if (lrbp->cmd->sc_data_direction == DMA_FROM_DEVICE) { 649 if (likely(lrbp->cmd)) {
538 data_direction = UTP_DEVICE_TO_HOST; 650 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
539 upiu_flags = UPIU_CMD_FLAGS_READ; 651 lrbp->cmd->sc_data_direction);
540 } else if (lrbp->cmd->sc_data_direction == DMA_TO_DEVICE) { 652 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
541 data_direction = UTP_HOST_TO_DEVICE;
542 upiu_flags = UPIU_CMD_FLAGS_WRITE;
543 } else { 653 } else {
544 data_direction = UTP_NO_DATA_TRANSFER; 654 ret = -EINVAL;
545 upiu_flags = UPIU_CMD_FLAGS_NONE;
546 } 655 }
547
548 /* Transfer request descriptor header fields */
549 req_desc->header.dword_0 =
550 cpu_to_le32(data_direction | UTP_SCSI_COMMAND);
551
552 /*
553 * assigning invalid value for command status. Controller
554 * updates OCS on command completion, with the command
555 * status
556 */
557 req_desc->header.dword_2 =
558 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
559
560 /* command descriptor fields */
561 ucd_cmd_ptr->header.dword_0 =
562 cpu_to_be32(UPIU_HEADER_DWORD(UPIU_TRANSACTION_COMMAND,
563 upiu_flags,
564 lrbp->lun,
565 lrbp->task_tag));
566 ucd_cmd_ptr->header.dword_1 =
567 cpu_to_be32(
568 UPIU_HEADER_DWORD(UPIU_COMMAND_SET_TYPE_SCSI,
569 0,
570 0,
571 0));
572
573 /* Total EHS length and Data segment length will be zero */
574 ucd_cmd_ptr->header.dword_2 = 0;
575
576 ucd_cmd_ptr->exp_data_transfer_len =
577 cpu_to_be32(lrbp->cmd->sdb.length);
578
579 memcpy(ucd_cmd_ptr->cdb,
580 lrbp->cmd->cmnd,
581 (min_t(unsigned short,
582 lrbp->cmd->cmd_len,
583 MAX_CDB_SIZE)));
584 break; 656 break;
585 case UTP_CMD_TYPE_DEV_MANAGE: 657 case UTP_CMD_TYPE_DEV_MANAGE:
586 /* For query function implementation */ 658 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
659 if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
660 ufshcd_prepare_utp_nop_upiu(lrbp);
661 else
662 ret = -EINVAL;
587 break; 663 break;
588 case UTP_CMD_TYPE_UFS: 664 case UTP_CMD_TYPE_UFS:
589 /* For UFS native command implementation */ 665 /* For UFS native command implementation */
666 ret = -ENOTSUPP;
667 dev_err(hba->dev, "%s: UFS native command are not supported\n",
668 __func__);
669 break;
670 default:
671 ret = -ENOTSUPP;
672 dev_err(hba->dev, "%s: unknown command type: 0x%x\n",
673 __func__, lrbp->command_type);
590 break; 674 break;
591 } /* end of switch */ 675 } /* end of switch */
676
677 return ret;
592} 678}
593 679
594/** 680/**
@@ -615,21 +701,37 @@ static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
615 goto out; 701 goto out;
616 } 702 }
617 703
704 /* acquire the tag to make sure device cmds don't use it */
705 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
706 /*
707 * Dev manage command in progress, requeue the command.
708 * Requeuing the command helps in cases where the request *may*
709 * find different tag instead of waiting for dev manage command
710 * completion.
711 */
712 err = SCSI_MLQUEUE_HOST_BUSY;
713 goto out;
714 }
715
618 lrbp = &hba->lrb[tag]; 716 lrbp = &hba->lrb[tag];
619 717
718 WARN_ON(lrbp->cmd);
620 lrbp->cmd = cmd; 719 lrbp->cmd = cmd;
621 lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE; 720 lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
622 lrbp->sense_buffer = cmd->sense_buffer; 721 lrbp->sense_buffer = cmd->sense_buffer;
623 lrbp->task_tag = tag; 722 lrbp->task_tag = tag;
624 lrbp->lun = cmd->device->lun; 723 lrbp->lun = cmd->device->lun;
625 724 lrbp->intr_cmd = false;
626 lrbp->command_type = UTP_CMD_TYPE_SCSI; 725 lrbp->command_type = UTP_CMD_TYPE_SCSI;
627 726
628 /* form UPIU before issuing the command */ 727 /* form UPIU before issuing the command */
629 ufshcd_compose_upiu(lrbp); 728 ufshcd_compose_upiu(hba, lrbp);
630 err = ufshcd_map_sg(lrbp); 729 err = ufshcd_map_sg(lrbp);
631 if (err) 730 if (err) {
731 lrbp->cmd = NULL;
732 clear_bit_unlock(tag, &hba->lrb_in_use);
632 goto out; 733 goto out;
734 }
633 735
634 /* issue command to the controller */ 736 /* issue command to the controller */
635 spin_lock_irqsave(hba->host->host_lock, flags); 737 spin_lock_irqsave(hba->host->host_lock, flags);
@@ -639,6 +741,194 @@ out:
639 return err; 741 return err;
640} 742}
641 743
744static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
745 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
746{
747 lrbp->cmd = NULL;
748 lrbp->sense_bufflen = 0;
749 lrbp->sense_buffer = NULL;
750 lrbp->task_tag = tag;
751 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
752 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
753 lrbp->intr_cmd = true; /* No interrupt aggregation */
754 hba->dev_cmd.type = cmd_type;
755
756 return ufshcd_compose_upiu(hba, lrbp);
757}
758
759static int
760ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
761{
762 int err = 0;
763 unsigned long flags;
764 u32 mask = 1 << tag;
765
766 /* clear outstanding transaction before retry */
767 spin_lock_irqsave(hba->host->host_lock, flags);
768 ufshcd_utrl_clear(hba, tag);
769 spin_unlock_irqrestore(hba->host->host_lock, flags);
770
771 /*
772 * wait for for h/w to clear corresponding bit in door-bell.
773 * max. wait is 1 sec.
774 */
775 err = ufshcd_wait_for_register(hba,
776 REG_UTP_TRANSFER_REQ_DOOR_BELL,
777 mask, ~mask, 1000, 1000);
778
779 return err;
780}
781
782/**
783 * ufshcd_dev_cmd_completion() - handles device management command responses
784 * @hba: per adapter instance
785 * @lrbp: pointer to local reference block
786 */
787static int
788ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
789{
790 int resp;
791 int err = 0;
792
793 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
794
795 switch (resp) {
796 case UPIU_TRANSACTION_NOP_IN:
797 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
798 err = -EINVAL;
799 dev_err(hba->dev, "%s: unexpected response %x\n",
800 __func__, resp);
801 }
802 break;
803 case UPIU_TRANSACTION_REJECT_UPIU:
804 /* TODO: handle Reject UPIU Response */
805 err = -EPERM;
806 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
807 __func__);
808 break;
809 default:
810 err = -EINVAL;
811 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
812 __func__, resp);
813 break;
814 }
815
816 return err;
817}
818
819static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
820 struct ufshcd_lrb *lrbp, int max_timeout)
821{
822 int err = 0;
823 unsigned long time_left;
824 unsigned long flags;
825
826 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
827 msecs_to_jiffies(max_timeout));
828
829 spin_lock_irqsave(hba->host->host_lock, flags);
830 hba->dev_cmd.complete = NULL;
831 if (likely(time_left)) {
832 err = ufshcd_get_tr_ocs(lrbp);
833 if (!err)
834 err = ufshcd_dev_cmd_completion(hba, lrbp);
835 }
836 spin_unlock_irqrestore(hba->host->host_lock, flags);
837
838 if (!time_left) {
839 err = -ETIMEDOUT;
840 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
841 /* sucessfully cleared the command, retry if needed */
842 err = -EAGAIN;
843 }
844
845 return err;
846}
847
848/**
849 * ufshcd_get_dev_cmd_tag - Get device management command tag
850 * @hba: per-adapter instance
851 * @tag: pointer to variable with available slot value
852 *
853 * Get a free slot and lock it until device management command
854 * completes.
855 *
856 * Returns false if free slot is unavailable for locking, else
857 * return true with tag value in @tag.
858 */
859static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
860{
861 int tag;
862 bool ret = false;
863 unsigned long tmp;
864
865 if (!tag_out)
866 goto out;
867
868 do {
869 tmp = ~hba->lrb_in_use;
870 tag = find_last_bit(&tmp, hba->nutrs);
871 if (tag >= hba->nutrs)
872 goto out;
873 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
874
875 *tag_out = tag;
876 ret = true;
877out:
878 return ret;
879}
880
881static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
882{
883 clear_bit_unlock(tag, &hba->lrb_in_use);
884}
885
886/**
887 * ufshcd_exec_dev_cmd - API for sending device management requests
888 * @hba - UFS hba
889 * @cmd_type - specifies the type (NOP, Query...)
890 * @timeout - time in seconds
891 *
892 * NOTE: There is only one available tag for device management commands. Thus
893 * synchronisation is the responsibilty of the user.
894 */
895static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
896 enum dev_cmd_type cmd_type, int timeout)
897{
898 struct ufshcd_lrb *lrbp;
899 int err;
900 int tag;
901 struct completion wait;
902 unsigned long flags;
903
904 /*
905 * Get free slot, sleep if slots are unavailable.
906 * Even though we use wait_event() which sleeps indefinitely,
907 * the maximum wait time is bounded by SCSI request timeout.
908 */
909 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
910
911 init_completion(&wait);
912 lrbp = &hba->lrb[tag];
913 WARN_ON(lrbp->cmd);
914 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
915 if (unlikely(err))
916 goto out_put_tag;
917
918 hba->dev_cmd.complete = &wait;
919
920 spin_lock_irqsave(hba->host->host_lock, flags);
921 ufshcd_send_command(hba, tag);
922 spin_unlock_irqrestore(hba->host->host_lock, flags);
923
924 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
925
926out_put_tag:
927 ufshcd_put_dev_cmd_tag(hba, tag);
928 wake_up(&hba->dev_cmd.tag_wq);
929 return err;
930}
931
642/** 932/**
643 * ufshcd_memory_alloc - allocate memory for host memory space data structures 933 * ufshcd_memory_alloc - allocate memory for host memory space data structures
644 * @hba: per adapter instance 934 * @hba: per adapter instance
@@ -774,8 +1064,8 @@ static void ufshcd_host_memory_configure(struct ufs_hba *hba)
774 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2); 1064 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
775 1065
776 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i); 1066 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
777 hba->lrb[i].ucd_cmd_ptr = 1067 hba->lrb[i].ucd_req_ptr =
778 (struct utp_upiu_cmd *)(cmd_descp + i); 1068 (struct utp_upiu_req *)(cmd_descp + i);
779 hba->lrb[i].ucd_rsp_ptr = 1069 hba->lrb[i].ucd_rsp_ptr =
780 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu; 1070 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
781 hba->lrb[i].ucd_prdt_ptr = 1071 hba->lrb[i].ucd_prdt_ptr =
@@ -961,6 +1251,38 @@ out:
961} 1251}
962 1252
963/** 1253/**
1254 * ufshcd_verify_dev_init() - Verify device initialization
1255 * @hba: per-adapter instance
1256 *
1257 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
1258 * device Transport Protocol (UTP) layer is ready after a reset.
1259 * If the UTP layer at the device side is not initialized, it may
1260 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
1261 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
1262 */
1263static int ufshcd_verify_dev_init(struct ufs_hba *hba)
1264{
1265 int err = 0;
1266 int retries;
1267
1268 mutex_lock(&hba->dev_cmd.lock);
1269 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
1270 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
1271 NOP_OUT_TIMEOUT);
1272
1273 if (!err || err == -ETIMEDOUT)
1274 break;
1275
1276 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
1277 }
1278 mutex_unlock(&hba->dev_cmd.lock);
1279
1280 if (err)
1281 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
1282 return err;
1283}
1284
1285/**
964 * ufshcd_do_reset - reset the host controller 1286 * ufshcd_do_reset - reset the host controller
965 * @hba: per adapter instance 1287 * @hba: per adapter instance
966 * 1288 *
@@ -986,13 +1308,20 @@ static int ufshcd_do_reset(struct ufs_hba *hba)
986 for (tag = 0; tag < hba->nutrs; tag++) { 1308 for (tag = 0; tag < hba->nutrs; tag++) {
987 if (test_bit(tag, &hba->outstanding_reqs)) { 1309 if (test_bit(tag, &hba->outstanding_reqs)) {
988 lrbp = &hba->lrb[tag]; 1310 lrbp = &hba->lrb[tag];
989 scsi_dma_unmap(lrbp->cmd); 1311 if (lrbp->cmd) {
990 lrbp->cmd->result = DID_RESET << 16; 1312 scsi_dma_unmap(lrbp->cmd);
991 lrbp->cmd->scsi_done(lrbp->cmd); 1313 lrbp->cmd->result = DID_RESET << 16;
992 lrbp->cmd = NULL; 1314 lrbp->cmd->scsi_done(lrbp->cmd);
1315 lrbp->cmd = NULL;
1316 clear_bit_unlock(tag, &hba->lrb_in_use);
1317 }
993 } 1318 }
994 } 1319 }
995 1320
1321 /* complete device management command */
1322 if (hba->dev_cmd.complete)
1323 complete(hba->dev_cmd.complete);
1324
996 /* clear outstanding request/task bit maps */ 1325 /* clear outstanding request/task bit maps */
997 hba->outstanding_reqs = 0; 1326 hba->outstanding_reqs = 0;
998 hba->outstanding_tasks = 0; 1327 hba->outstanding_tasks = 0;
@@ -1199,27 +1528,36 @@ ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1199 1528
1200 switch (ocs) { 1529 switch (ocs) {
1201 case OCS_SUCCESS: 1530 case OCS_SUCCESS:
1531 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1202 1532
1203 /* check if the returned transfer response is valid */ 1533 switch (result) {
1204 result = ufshcd_is_valid_req_rsp(lrbp->ucd_rsp_ptr); 1534 case UPIU_TRANSACTION_RESPONSE:
1205 if (result) { 1535 /*
1536 * get the response UPIU result to extract
1537 * the SCSI command status
1538 */
1539 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
1540
1541 /*
1542 * get the result based on SCSI status response
1543 * to notify the SCSI midlayer of the command status
1544 */
1545 scsi_status = result & MASK_SCSI_STATUS;
1546 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
1547 break;
1548 case UPIU_TRANSACTION_REJECT_UPIU:
1549 /* TODO: handle Reject UPIU Response */
1550 result = DID_ERROR << 16;
1206 dev_err(hba->dev, 1551 dev_err(hba->dev,
1207 "Invalid response = %x\n", result); 1552 "Reject UPIU not fully implemented\n");
1553 break;
1554 default:
1555 result = DID_ERROR << 16;
1556 dev_err(hba->dev,
1557 "Unexpected request response code = %x\n",
1558 result);
1208 break; 1559 break;
1209 } 1560 }
1210
1211 /*
1212 * get the response UPIU result to extract
1213 * the SCSI command status
1214 */
1215 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
1216
1217 /*
1218 * get the result based on SCSI status response
1219 * to notify the SCSI midlayer of the command status
1220 */
1221 scsi_status = result & MASK_SCSI_STATUS;
1222 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
1223 break; 1561 break;
1224 case OCS_ABORTED: 1562 case OCS_ABORTED:
1225 result |= DID_ABORT << 16; 1563 result |= DID_ABORT << 16;
@@ -1259,28 +1597,40 @@ static void ufshcd_uic_cmd_compl(struct ufs_hba *hba)
1259 */ 1597 */
1260static void ufshcd_transfer_req_compl(struct ufs_hba *hba) 1598static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
1261{ 1599{
1262 struct ufshcd_lrb *lrb; 1600 struct ufshcd_lrb *lrbp;
1601 struct scsi_cmnd *cmd;
1263 unsigned long completed_reqs; 1602 unsigned long completed_reqs;
1264 u32 tr_doorbell; 1603 u32 tr_doorbell;
1265 int result; 1604 int result;
1266 int index; 1605 int index;
1606 bool int_aggr_reset = false;
1267 1607
1268 lrb = hba->lrb;
1269 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); 1608 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
1270 completed_reqs = tr_doorbell ^ hba->outstanding_reqs; 1609 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
1271 1610
1272 for (index = 0; index < hba->nutrs; index++) { 1611 for (index = 0; index < hba->nutrs; index++) {
1273 if (test_bit(index, &completed_reqs)) { 1612 if (test_bit(index, &completed_reqs)) {
1613 lrbp = &hba->lrb[index];
1614 cmd = lrbp->cmd;
1615 /*
1616 * Don't skip resetting interrupt aggregation counters
1617 * if a regular command is present.
1618 */
1619 int_aggr_reset |= !lrbp->intr_cmd;
1274 1620
1275 result = ufshcd_transfer_rsp_status(hba, &lrb[index]); 1621 if (cmd) {
1276 1622 result = ufshcd_transfer_rsp_status(hba, lrbp);
1277 if (lrb[index].cmd) { 1623 scsi_dma_unmap(cmd);
1278 scsi_dma_unmap(lrb[index].cmd); 1624 cmd->result = result;
1279 lrb[index].cmd->result = result;
1280 lrb[index].cmd->scsi_done(lrb[index].cmd);
1281
1282 /* Mark completed command as NULL in LRB */ 1625 /* Mark completed command as NULL in LRB */
1283 lrb[index].cmd = NULL; 1626 lrbp->cmd = NULL;
1627 clear_bit_unlock(index, &hba->lrb_in_use);
1628 /* Do not touch lrbp after scsi done */
1629 cmd->scsi_done(cmd);
1630 } else if (lrbp->command_type ==
1631 UTP_CMD_TYPE_DEV_MANAGE) {
1632 if (hba->dev_cmd.complete)
1633 complete(hba->dev_cmd.complete);
1284 } 1634 }
1285 } /* end of if */ 1635 } /* end of if */
1286 } /* end of for */ 1636 } /* end of for */
@@ -1288,8 +1638,12 @@ static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
1288 /* clear corresponding bits of completed commands */ 1638 /* clear corresponding bits of completed commands */
1289 hba->outstanding_reqs ^= completed_reqs; 1639 hba->outstanding_reqs ^= completed_reqs;
1290 1640
1641 /* we might have free'd some tags above */
1642 wake_up(&hba->dev_cmd.tag_wq);
1643
1291 /* Reset interrupt aggregation counters */ 1644 /* Reset interrupt aggregation counters */
1292 ufshcd_config_int_aggr(hba, INT_AGGR_RESET); 1645 if (int_aggr_reset)
1646 ufshcd_config_int_aggr(hba, INT_AGGR_RESET);
1293} 1647}
1294 1648
1295/** 1649/**
@@ -1432,10 +1786,10 @@ ufshcd_issue_tm_cmd(struct ufs_hba *hba,
1432 task_req_upiup = 1786 task_req_upiup =
1433 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu; 1787 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
1434 task_req_upiup->header.dword_0 = 1788 task_req_upiup->header.dword_0 =
1435 cpu_to_be32(UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0, 1789 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
1436 lrbp->lun, lrbp->task_tag)); 1790 lrbp->lun, lrbp->task_tag);
1437 task_req_upiup->header.dword_1 = 1791 task_req_upiup->header.dword_1 =
1438 cpu_to_be32(UPIU_HEADER_DWORD(0, tm_function, 0, 0)); 1792 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
1439 1793
1440 task_req_upiup->input_param1 = lrbp->lun; 1794 task_req_upiup->input_param1 = lrbp->lun;
1441 task_req_upiup->input_param1 = 1795 task_req_upiup->input_param1 =
@@ -1502,9 +1856,11 @@ static int ufshcd_device_reset(struct scsi_cmnd *cmd)
1502 if (hba->lrb[pos].cmd) { 1856 if (hba->lrb[pos].cmd) {
1503 scsi_dma_unmap(hba->lrb[pos].cmd); 1857 scsi_dma_unmap(hba->lrb[pos].cmd);
1504 hba->lrb[pos].cmd->result = 1858 hba->lrb[pos].cmd->result =
1505 DID_ABORT << 16; 1859 DID_ABORT << 16;
1506 hba->lrb[pos].cmd->scsi_done(cmd); 1860 hba->lrb[pos].cmd->scsi_done(cmd);
1507 hba->lrb[pos].cmd = NULL; 1861 hba->lrb[pos].cmd = NULL;
1862 clear_bit_unlock(pos, &hba->lrb_in_use);
1863 wake_up(&hba->dev_cmd.tag_wq);
1508 } 1864 }
1509 } 1865 }
1510 } /* end of for */ 1866 } /* end of for */
@@ -1572,6 +1928,9 @@ static int ufshcd_abort(struct scsi_cmnd *cmd)
1572 __clear_bit(tag, &hba->outstanding_reqs); 1928 __clear_bit(tag, &hba->outstanding_reqs);
1573 hba->lrb[tag].cmd = NULL; 1929 hba->lrb[tag].cmd = NULL;
1574 spin_unlock_irqrestore(host->host_lock, flags); 1930 spin_unlock_irqrestore(host->host_lock, flags);
1931
1932 clear_bit_unlock(tag, &hba->lrb_in_use);
1933 wake_up(&hba->dev_cmd.tag_wq);
1575out: 1934out:
1576 return err; 1935 return err;
1577} 1936}
@@ -1587,8 +1946,16 @@ static void ufshcd_async_scan(void *data, async_cookie_t cookie)
1587 int ret; 1946 int ret;
1588 1947
1589 ret = ufshcd_link_startup(hba); 1948 ret = ufshcd_link_startup(hba);
1590 if (!ret) 1949 if (ret)
1591 scsi_scan_host(hba->host); 1950 goto out;
1951
1952 ret = ufshcd_verify_dev_init(hba);
1953 if (ret)
1954 goto out;
1955
1956 scsi_scan_host(hba->host);
1957out:
1958 return;
1592} 1959}
1593 1960
1594static struct scsi_host_template ufshcd_driver_template = { 1961static struct scsi_host_template ufshcd_driver_template = {
@@ -1744,6 +2111,12 @@ int ufshcd_init(struct device *dev, struct ufs_hba **hba_handle,
1744 /* Initialize UIC command mutex */ 2111 /* Initialize UIC command mutex */
1745 mutex_init(&hba->uic_cmd_mutex); 2112 mutex_init(&hba->uic_cmd_mutex);
1746 2113
2114 /* Initialize mutex for device management commands */
2115 mutex_init(&hba->dev_cmd.lock);
2116
2117 /* Initialize device management tag acquire wait queue */
2118 init_waitqueue_head(&hba->dev_cmd.tag_wq);
2119
1747 /* IRQ registration */ 2120 /* IRQ registration */
1748 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba); 2121 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
1749 if (err) { 2122 if (err) {
diff --git a/drivers/scsi/ufs/ufshcd.h b/drivers/scsi/ufs/ufshcd.h
index 49590ee07acc..c750a90eb9da 100644
--- a/drivers/scsi/ufs/ufshcd.h
+++ b/drivers/scsi/ufs/ufshcd.h
@@ -68,6 +68,10 @@
68#define UFSHCD "ufshcd" 68#define UFSHCD "ufshcd"
69#define UFSHCD_DRIVER_VERSION "0.2" 69#define UFSHCD_DRIVER_VERSION "0.2"
70 70
71enum dev_cmd_type {
72 DEV_CMD_TYPE_NOP = 0x0,
73};
74
71/** 75/**
72 * struct uic_command - UIC command structure 76 * struct uic_command - UIC command structure
73 * @command: UIC command 77 * @command: UIC command
@@ -91,7 +95,7 @@ struct uic_command {
91/** 95/**
92 * struct ufshcd_lrb - local reference block 96 * struct ufshcd_lrb - local reference block
93 * @utr_descriptor_ptr: UTRD address of the command 97 * @utr_descriptor_ptr: UTRD address of the command
94 * @ucd_cmd_ptr: UCD address of the command 98 * @ucd_req_ptr: UCD address of the command
95 * @ucd_rsp_ptr: Response UPIU address for this command 99 * @ucd_rsp_ptr: Response UPIU address for this command
96 * @ucd_prdt_ptr: PRDT address of the command 100 * @ucd_prdt_ptr: PRDT address of the command
97 * @cmd: pointer to SCSI command 101 * @cmd: pointer to SCSI command
@@ -101,10 +105,11 @@ struct uic_command {
101 * @command_type: SCSI, UFS, Query. 105 * @command_type: SCSI, UFS, Query.
102 * @task_tag: Task tag of the command 106 * @task_tag: Task tag of the command
103 * @lun: LUN of the command 107 * @lun: LUN of the command
108 * @intr_cmd: Interrupt command (doesn't participate in interrupt aggregation)
104 */ 109 */
105struct ufshcd_lrb { 110struct ufshcd_lrb {
106 struct utp_transfer_req_desc *utr_descriptor_ptr; 111 struct utp_transfer_req_desc *utr_descriptor_ptr;
107 struct utp_upiu_cmd *ucd_cmd_ptr; 112 struct utp_upiu_req *ucd_req_ptr;
108 struct utp_upiu_rsp *ucd_rsp_ptr; 113 struct utp_upiu_rsp *ucd_rsp_ptr;
109 struct ufshcd_sg_entry *ucd_prdt_ptr; 114 struct ufshcd_sg_entry *ucd_prdt_ptr;
110 115
@@ -116,8 +121,22 @@ struct ufshcd_lrb {
116 int command_type; 121 int command_type;
117 int task_tag; 122 int task_tag;
118 unsigned int lun; 123 unsigned int lun;
124 bool intr_cmd;
119}; 125};
120 126
127/**
128 * struct ufs_dev_cmd - all assosiated fields with device management commands
129 * @type: device management command type - Query, NOP OUT
130 * @lock: lock to allow one command at a time
131 * @complete: internal commands completion
132 * @tag_wq: wait queue until free command slot is available
133 */
134struct ufs_dev_cmd {
135 enum dev_cmd_type type;
136 struct mutex lock;
137 struct completion *complete;
138 wait_queue_head_t tag_wq;
139};
121 140
122/** 141/**
123 * struct ufs_hba - per adapter private structure 142 * struct ufs_hba - per adapter private structure
@@ -131,6 +150,7 @@ struct ufshcd_lrb {
131 * @host: Scsi_Host instance of the driver 150 * @host: Scsi_Host instance of the driver
132 * @dev: device handle 151 * @dev: device handle
133 * @lrb: local reference block 152 * @lrb: local reference block
153 * @lrb_in_use: lrb in use
134 * @outstanding_tasks: Bits representing outstanding task requests 154 * @outstanding_tasks: Bits representing outstanding task requests
135 * @outstanding_reqs: Bits representing outstanding transfer requests 155 * @outstanding_reqs: Bits representing outstanding transfer requests
136 * @capabilities: UFS Controller Capabilities 156 * @capabilities: UFS Controller Capabilities
@@ -146,6 +166,7 @@ struct ufshcd_lrb {
146 * @intr_mask: Interrupt Mask Bits 166 * @intr_mask: Interrupt Mask Bits
147 * @feh_workq: Work queue for fatal controller error handling 167 * @feh_workq: Work queue for fatal controller error handling
148 * @errors: HBA errors 168 * @errors: HBA errors
169 * @dev_cmd: ufs device management command information
149 */ 170 */
150struct ufs_hba { 171struct ufs_hba {
151 void __iomem *mmio_base; 172 void __iomem *mmio_base;
@@ -164,6 +185,7 @@ struct ufs_hba {
164 struct device *dev; 185 struct device *dev;
165 186
166 struct ufshcd_lrb *lrb; 187 struct ufshcd_lrb *lrb;
188 unsigned long lrb_in_use;
167 189
168 unsigned long outstanding_tasks; 190 unsigned long outstanding_tasks;
169 unsigned long outstanding_reqs; 191 unsigned long outstanding_reqs;
@@ -188,6 +210,9 @@ struct ufs_hba {
188 210
189 /* HBA Errors */ 211 /* HBA Errors */
190 u32 errors; 212 u32 errors;
213
214 /* Device management request data */
215 struct ufs_dev_cmd dev_cmd;
191}; 216};
192 217
193#define ufshcd_writel(hba, val, reg) \ 218#define ufshcd_writel(hba, val, reg) \