Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

scsi: ufs: core: Allow RTT negotiation

The rtt-upiu packets precede any data-out upiu packets, thus synchronizing
the data input to the device: this mostly applies to write operations, but
there are other operations that requires rtt as well.

There are several rules binding this rtt - data-out dialog, specifically
There can be at most outstanding bMaxNumOfRTT such packets. This might
have an effect on write performance (sequential write in particular), as
each data-out upiu must wait for its rtt sibling.

UFSHCI expects bMaxNumOfRTT to be min(bDeviceRTTCap, NORTT). However, as of
today, there does not appears to be no-one who sets it: not the host
controller nor the driver. It wasn't an issue up to now: bMaxNumOfRTT is
set to 2 after manufacturing, and wasn't limiting the write performance.

UFS4.0, and specifically gear 5 changes this, and requires the device to be
more attentive. This doesn't come free - the device has to allocate more
resources to that end, but the sequential write performance improvement is
significant. Early measurements shows 25% gain when moving from rtt 2 to
9. Therefore, set bMaxNumOfRTT to be min(bDeviceRTTCap, NORTT) as UFSHCI
expects.

Signed-off-by: Avri Altman <avri.altman@wdc.com>
Link: https://lore.kernel.org/r/20240530142510.734-2-avri.altman@wdc.com
Reviewed-by: Bean Huo <beanhuo@micron.com>
Reviewed-by: Bart Van Assche <bvanassche@acm.org>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>

authored by

Avri Altman and committed by
Martin K. Petersen
9ec54934 1613e604

+43
+38
drivers/ufs/core/ufshcd.c
··· 102 102 /* Default RTC update every 10 seconds */ 103 103 #define UFS_RTC_UPDATE_INTERVAL_MS (10 * MSEC_PER_SEC) 104 104 105 + /* bMaxNumOfRTT is equal to two after device manufacturing */ 106 + #define DEFAULT_MAX_NUM_RTT 2 107 + 105 108 /* UFSHC 4.0 compliant HC support this mode. */ 106 109 static bool use_mcq_mode = true; 107 110 ··· 2407 2404 hba->nutmrs = 2408 2405 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1; 2409 2406 hba->reserved_slot = hba->nutrs - 1; 2407 + 2408 + hba->nortt = FIELD_GET(MASK_NUMBER_OUTSTANDING_RTT, hba->capabilities) + 1; 2410 2409 2411 2410 /* Read crypto capabilities */ 2412 2411 err = ufshcd_hba_init_crypto_capabilities(hba); ··· 8126 8121 dev_info->b_ext_iid_en = ext_iid_en; 8127 8122 } 8128 8123 8124 + static void ufshcd_set_rtt(struct ufs_hba *hba) 8125 + { 8126 + struct ufs_dev_info *dev_info = &hba->dev_info; 8127 + u32 rtt = 0; 8128 + u32 dev_rtt = 0; 8129 + 8130 + /* RTT override makes sense only for UFS-4.0 and above */ 8131 + if (dev_info->wspecversion < 0x400) 8132 + return; 8133 + 8134 + if (ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, 8135 + QUERY_ATTR_IDN_MAX_NUM_OF_RTT, 0, 0, &dev_rtt)) { 8136 + dev_err(hba->dev, "failed reading bMaxNumOfRTT\n"); 8137 + return; 8138 + } 8139 + 8140 + /* do not override if it was already written */ 8141 + if (dev_rtt != DEFAULT_MAX_NUM_RTT) 8142 + return; 8143 + 8144 + rtt = min_t(int, dev_info->rtt_cap, hba->nortt); 8145 + if (rtt == dev_rtt) 8146 + return; 8147 + 8148 + if (ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, 8149 + QUERY_ATTR_IDN_MAX_NUM_OF_RTT, 0, 0, &rtt)) 8150 + dev_err(hba->dev, "failed writing bMaxNumOfRTT\n"); 8151 + } 8152 + 8129 8153 void ufshcd_fixup_dev_quirks(struct ufs_hba *hba, 8130 8154 const struct ufs_dev_quirk *fixups) 8131 8155 { ··· 8289 8255 dev_info->wspecversion = desc_buf[DEVICE_DESC_PARAM_SPEC_VER] << 8 | 8290 8256 desc_buf[DEVICE_DESC_PARAM_SPEC_VER + 1]; 8291 8257 dev_info->bqueuedepth = desc_buf[DEVICE_DESC_PARAM_Q_DPTH]; 8258 + 8259 + dev_info->rtt_cap = desc_buf[DEVICE_DESC_PARAM_RTT_CAP]; 8292 8260 8293 8261 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME]; 8294 8262 ··· 8543 8507 __func__, ret); 8544 8508 goto out; 8545 8509 } 8510 + 8511 + ufshcd_set_rtt(hba); 8546 8512 8547 8513 ufshcd_get_ref_clk_gating_wait(hba); 8548 8514
+2
include/ufs/ufs.h
··· 592 592 enum ufs_rtc_time rtc_type; 593 593 time64_t rtc_time_baseline; 594 594 u32 rtc_update_period; 595 + 596 + u8 rtt_cap; /* bDeviceRTTCap */ 595 597 }; 596 598 597 599 /*
+2
include/ufs/ufshcd.h
··· 819 819 * @capabilities: UFS Controller Capabilities 820 820 * @mcq_capabilities: UFS Multi Circular Queue capabilities 821 821 * @nutrs: Transfer Request Queue depth supported by controller 822 + * @nortt - Max outstanding RTTs supported by controller 822 823 * @nutmrs: Task Management Queue depth supported by controller 823 824 * @reserved_slot: Used to submit device commands. Protected by @dev_cmd.lock. 824 825 * @ufs_version: UFS Version to which controller complies ··· 958 957 959 958 u32 capabilities; 960 959 int nutrs; 960 + int nortt; 961 961 u32 mcq_capabilities; 962 962 int nutmrs; 963 963 u32 reserved_slot;
+1
include/ufs/ufshci.h
··· 68 68 /* Controller capability masks */ 69 69 enum { 70 70 MASK_TRANSFER_REQUESTS_SLOTS = 0x0000001F, 71 + MASK_NUMBER_OUTSTANDING_RTT = 0x0000FF00, 71 72 MASK_TASK_MANAGEMENT_REQUEST_SLOTS = 0x00070000, 72 73 MASK_EHSLUTRD_SUPPORTED = 0x00400000, 73 74 MASK_AUTO_HIBERN8_SUPPORT = 0x00800000,