Skip to content

Commit 23646f0

Browse files
Merge patch series "scsi: ufs: Allow RTT negotiation"
Avri Altman <[email protected]> says: 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 appear 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. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin K. Petersen <[email protected]>
2 parents 96281df + 600edc6 commit 23646f0

File tree

8 files changed

+132
-7
lines changed

8 files changed

+132
-7
lines changed

Documentation/ABI/testing/sysfs-driver-ufs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -920,14 +920,16 @@ Description: This file shows whether the configuration descriptor is locked.
920920

921921
What: /sys/bus/platform/drivers/ufshcd/*/attributes/max_number_of_rtt
922922
What: /sys/bus/platform/devices/*.ufs/attributes/max_number_of_rtt
923-
Date: February 2018
924-
Contact: Stanislav Nijnikov <stanislav.nijnikov@wdc.com>
923+
Date: May 2024
924+
Contact: Avri Altman <avri.altman@wdc.com>
925925
Description: This file provides the maximum current number of
926-
outstanding RTTs in device that is allowed. The full
927-
information about the attribute could be found at
928-
UFS specifications 2.1.
926+
outstanding RTTs in device that is allowed. bMaxNumOfRTT is a
927+
read-write persistent attribute and is equal to two after device
928+
manufacturing. It shall not be set to a value greater than
929+
bDeviceRTTCap value, and it may be set only when the hw queues are
930+
empty.
929931

930-
The file is read only.
932+
The file is read write.
931933

932934
What: /sys/bus/platform/drivers/ufshcd/*/attributes/exception_event_control
933935
What: /sys/bus/platform/devices/*.ufs/attributes/exception_event_control

drivers/ufs/core/ufs-sysfs.c

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,78 @@ static const struct attribute_group ufs_sysfs_flags_group = {
13401340
.attrs = ufs_sysfs_device_flags,
13411341
};
13421342

1343+
static ssize_t max_number_of_rtt_show(struct device *dev,
1344+
struct device_attribute *attr, char *buf)
1345+
{
1346+
struct ufs_hba *hba = dev_get_drvdata(dev);
1347+
u32 rtt;
1348+
int ret;
1349+
1350+
down(&hba->host_sem);
1351+
if (!ufshcd_is_user_access_allowed(hba)) {
1352+
up(&hba->host_sem);
1353+
return -EBUSY;
1354+
}
1355+
1356+
ufshcd_rpm_get_sync(hba);
1357+
ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
1358+
QUERY_ATTR_IDN_MAX_NUM_OF_RTT, 0, 0, &rtt);
1359+
ufshcd_rpm_put_sync(hba);
1360+
1361+
if (ret)
1362+
goto out;
1363+
1364+
ret = sysfs_emit(buf, "0x%08X\n", rtt);
1365+
1366+
out:
1367+
up(&hba->host_sem);
1368+
return ret;
1369+
}
1370+
1371+
static ssize_t max_number_of_rtt_store(struct device *dev,
1372+
struct device_attribute *attr,
1373+
const char *buf, size_t count)
1374+
{
1375+
struct ufs_hba *hba = dev_get_drvdata(dev);
1376+
struct ufs_dev_info *dev_info = &hba->dev_info;
1377+
struct scsi_device *sdev;
1378+
unsigned int rtt;
1379+
int ret;
1380+
1381+
if (kstrtouint(buf, 0, &rtt))
1382+
return -EINVAL;
1383+
1384+
if (rtt > dev_info->rtt_cap) {
1385+
dev_err(dev, "rtt can be at most bDeviceRTTCap\n");
1386+
return -EINVAL;
1387+
}
1388+
1389+
down(&hba->host_sem);
1390+
if (!ufshcd_is_user_access_allowed(hba)) {
1391+
ret = -EBUSY;
1392+
goto out;
1393+
}
1394+
1395+
ufshcd_rpm_get_sync(hba);
1396+
1397+
shost_for_each_device(sdev, hba->host)
1398+
blk_mq_freeze_queue(sdev->request_queue);
1399+
1400+
ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
1401+
QUERY_ATTR_IDN_MAX_NUM_OF_RTT, 0, 0, &rtt);
1402+
1403+
shost_for_each_device(sdev, hba->host)
1404+
blk_mq_unfreeze_queue(sdev->request_queue);
1405+
1406+
ufshcd_rpm_put_sync(hba);
1407+
1408+
out:
1409+
up(&hba->host_sem);
1410+
return ret < 0 ? ret : count;
1411+
}
1412+
1413+
static DEVICE_ATTR_RW(max_number_of_rtt);
1414+
13431415
static inline bool ufshcd_is_wb_attrs(enum attr_idn idn)
13441416
{
13451417
return idn >= QUERY_ATTR_IDN_WB_FLUSH_STATUS &&
@@ -1387,7 +1459,6 @@ UFS_ATTRIBUTE(max_data_in_size, _MAX_DATA_IN);
13871459
UFS_ATTRIBUTE(max_data_out_size, _MAX_DATA_OUT);
13881460
UFS_ATTRIBUTE(reference_clock_frequency, _REF_CLK_FREQ);
13891461
UFS_ATTRIBUTE(configuration_descriptor_lock, _CONF_DESC_LOCK);
1390-
UFS_ATTRIBUTE(max_number_of_rtt, _MAX_NUM_OF_RTT);
13911462
UFS_ATTRIBUTE(exception_event_control, _EE_CONTROL);
13921463
UFS_ATTRIBUTE(exception_event_status, _EE_STATUS);
13931464
UFS_ATTRIBUTE(ffu_status, _FFU_STATUS);

drivers/ufs/core/ufshcd.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@
102102
/* Default RTC update every 10 seconds */
103103
#define UFS_RTC_UPDATE_INTERVAL_MS (10 * MSEC_PER_SEC)
104104

105+
/* bMaxNumOfRTT is equal to two after device manufacturing */
106+
#define DEFAULT_MAX_NUM_RTT 2
107+
105108
/* UFSHC 4.0 compliant HC support this mode. */
106109
static bool use_mcq_mode = true;
107110

@@ -2405,6 +2408,8 @@ static inline int ufshcd_hba_capabilities(struct ufs_hba *hba)
24052408
((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
24062409
hba->reserved_slot = hba->nutrs - 1;
24072410

2411+
hba->nortt = FIELD_GET(MASK_NUMBER_OUTSTANDING_RTT, hba->capabilities) + 1;
2412+
24082413
/* Read crypto capabilities */
24092414
err = ufshcd_hba_init_crypto_capabilities(hba);
24102415
if (err) {
@@ -8121,6 +8126,38 @@ static void ufshcd_ext_iid_probe(struct ufs_hba *hba, u8 *desc_buf)
81218126
dev_info->b_ext_iid_en = ext_iid_en;
81228127
}
81238128

8129+
static void ufshcd_set_rtt(struct ufs_hba *hba)
8130+
{
8131+
struct ufs_dev_info *dev_info = &hba->dev_info;
8132+
u32 rtt = 0;
8133+
u32 dev_rtt = 0;
8134+
int host_rtt_cap = hba->vops && hba->vops->max_num_rtt ?
8135+
hba->vops->max_num_rtt : hba->nortt;
8136+
8137+
/* RTT override makes sense only for UFS-4.0 and above */
8138+
if (dev_info->wspecversion < 0x400)
8139+
return;
8140+
8141+
if (ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
8142+
QUERY_ATTR_IDN_MAX_NUM_OF_RTT, 0, 0, &dev_rtt)) {
8143+
dev_err(hba->dev, "failed reading bMaxNumOfRTT\n");
8144+
return;
8145+
}
8146+
8147+
/* do not override if it was already written */
8148+
if (dev_rtt != DEFAULT_MAX_NUM_RTT)
8149+
return;
8150+
8151+
rtt = min_t(int, dev_info->rtt_cap, host_rtt_cap);
8152+
8153+
if (rtt == dev_rtt)
8154+
return;
8155+
8156+
if (ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
8157+
QUERY_ATTR_IDN_MAX_NUM_OF_RTT, 0, 0, &rtt))
8158+
dev_err(hba->dev, "failed writing bMaxNumOfRTT\n");
8159+
}
8160+
81248161
void ufshcd_fixup_dev_quirks(struct ufs_hba *hba,
81258162
const struct ufs_dev_quirk *fixups)
81268163
{
@@ -8256,6 +8293,8 @@ static int ufs_get_device_desc(struct ufs_hba *hba)
82568293
desc_buf[DEVICE_DESC_PARAM_SPEC_VER + 1];
82578294
dev_info->bqueuedepth = desc_buf[DEVICE_DESC_PARAM_Q_DPTH];
82588295

8296+
dev_info->rtt_cap = desc_buf[DEVICE_DESC_PARAM_RTT_CAP];
8297+
82598298
model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
82608299

82618300
err = ufshcd_read_string_desc(hba, model_index,
@@ -8508,6 +8547,8 @@ static int ufshcd_device_params_init(struct ufs_hba *hba)
85088547
goto out;
85098548
}
85108549

8550+
ufshcd_set_rtt(hba);
8551+
85118552
ufshcd_get_ref_clk_gating_wait(hba);
85128553

85138554
if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,

drivers/ufs/host/ufs-mediatek.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,6 +1785,7 @@ static int ufs_mtk_config_esi(struct ufs_hba *hba)
17851785
*/
17861786
static const struct ufs_hba_variant_ops ufs_hba_mtk_vops = {
17871787
.name = "mediatek.ufshci",
1788+
.max_num_rtt = MTK_MAX_NUM_RTT,
17881789
.init = ufs_mtk_init,
17891790
.get_ufs_hci_version = ufs_mtk_get_ufs_hci_version,
17901791
.setup_clocks = ufs_mtk_setup_clocks,

drivers/ufs/host/ufs-mediatek.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,7 @@ struct ufs_mtk_host {
189189
/* MTK delay of autosuspend: 500 ms */
190190
#define MTK_RPM_AUTOSUSPEND_DELAY_MS 500
191191

192+
/* MTK RTT support number */
193+
#define MTK_MAX_NUM_RTT 2
194+
192195
#endif /* !_UFS_MEDIATEK_H */

include/ufs/ufs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,8 @@ struct ufs_dev_info {
592592
enum ufs_rtc_time rtc_type;
593593
time64_t rtc_time_baseline;
594594
u32 rtc_update_period;
595+
596+
u8 rtt_cap; /* bDeviceRTTCap */
595597
};
596598

597599
/*

include/ufs/ufshcd.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ struct ufs_pwr_mode_info {
295295
/**
296296
* struct ufs_hba_variant_ops - variant specific callbacks
297297
* @name: variant name
298+
* @max_num_rtt: maximum RTT supported by the host
298299
* @init: called when the driver is initialized
299300
* @exit: called to cleanup everything done in init
300301
* @get_ufs_hci_version: called to get UFS HCI version
@@ -332,6 +333,7 @@ struct ufs_pwr_mode_info {
332333
*/
333334
struct ufs_hba_variant_ops {
334335
const char *name;
336+
int max_num_rtt;
335337
int (*init)(struct ufs_hba *);
336338
void (*exit)(struct ufs_hba *);
337339
u32 (*get_ufs_hci_version)(struct ufs_hba *);
@@ -819,6 +821,7 @@ enum ufshcd_mcq_opr {
819821
* @capabilities: UFS Controller Capabilities
820822
* @mcq_capabilities: UFS Multi Circular Queue capabilities
821823
* @nutrs: Transfer Request Queue depth supported by controller
824+
* @nortt - Max outstanding RTTs supported by controller
822825
* @nutmrs: Task Management Queue depth supported by controller
823826
* @reserved_slot: Used to submit device commands. Protected by @dev_cmd.lock.
824827
* @ufs_version: UFS Version to which controller complies
@@ -957,6 +960,7 @@ struct ufs_hba {
957960

958961
u32 capabilities;
959962
int nutrs;
963+
int nortt;
960964
u32 mcq_capabilities;
961965
int nutmrs;
962966
u32 reserved_slot;

include/ufs/ufshci.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ enum {
6868
/* Controller capability masks */
6969
enum {
7070
MASK_TRANSFER_REQUESTS_SLOTS = 0x0000001F,
71+
MASK_NUMBER_OUTSTANDING_RTT = 0x0000FF00,
7172
MASK_TASK_MANAGEMENT_REQUEST_SLOTS = 0x00070000,
7273
MASK_EHSLUTRD_SUPPORTED = 0x00400000,
7374
MASK_AUTO_HIBERN8_SUPPORT = 0x00800000,

0 commit comments

Comments
 (0)