1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Universal Flash Storage Host Performance Booster
5 * Copyright (C) 2017-2021 Samsung Electronics Co., Ltd.
8 * Yongmyung Lee <ymhungry.lee@samsung.com>
9 * Jinyoung Choi <j-young.choi@samsung.com>
15 /* hpb response UPIU macro */
16 #define HPB_RSP_NONE 0x0
17 #define HPB_RSP_REQ_REGION_UPDATE 0x1
18 #define HPB_RSP_DEV_RESET 0x2
19 #define MAX_ACTIVE_NUM 2
20 #define MAX_INACTIVE_NUM 2
21 #define DEV_DATA_SEG_LEN 0x14
22 #define DEV_SENSE_SEG_LEN 0x12
23 #define DEV_DES_TYPE 0x80
24 #define DEV_ADDITIONAL_LEN 0x10
26 /* hpb map & entries macro */
27 #define HPB_RGN_SIZE_UNIT 512
28 #define HPB_ENTRY_BLOCK_SIZE 4096
29 #define HPB_ENTRY_SIZE 0x8
30 #define PINNED_NOT_SET U32_MAX
32 /* hpb support chunk size */
33 #define HPB_LEGACY_CHUNK_HIGH 1
34 #define HPB_MULTI_CHUNK_LOW 7
35 #define HPB_MULTI_CHUNK_HIGH 255
37 /* hpb vender defined opcode */
38 #define UFSHPB_READ 0xF8
39 #define UFSHPB_READ_BUFFER 0xF9
40 #define UFSHPB_READ_BUFFER_ID 0x01
41 #define UFSHPB_WRITE_BUFFER 0xFA
42 #define UFSHPB_WRITE_BUFFER_INACT_SINGLE_ID 0x01
43 #define UFSHPB_WRITE_BUFFER_PREFETCH_ID 0x02
44 #define UFSHPB_WRITE_BUFFER_INACT_ALL_ID 0x03
45 #define HPB_WRITE_BUFFER_CMD_LENGTH 10
46 #define MAX_HPB_READ_ID 0x7F
47 #define HPB_READ_BUFFER_CMD_LENGTH 10
48 #define LU_ENABLED_HPB_FUNC 0x02
50 #define HPB_RESET_REQ_RETRIES 10
51 #define HPB_MAP_REQ_RETRIES 5
52 #define HPB_REQUEUE_TIME_MS 0
54 #define HPB_SUPPORT_VERSION 0x200
55 #define HPB_SUPPORT_LEGACY_VERSION 0x100
73 /* pinned regions are always active */
85 * struct ufshpb_lu_info - UFSHPB logical unit related info
86 * @num_blocks: the number of logical block
87 * @pinned_start: the start region number of pinned region
88 * @num_pinned: the number of pinned regions
89 * @max_active_rgns: maximum number of active regions
91 struct ufshpb_lu_info {
98 struct ufshpb_map_ctx {
100 unsigned long *ppn_dirty;
103 struct ufshpb_subregion {
104 struct ufshpb_map_ctx *mctx;
105 enum HPB_SRGN_STATE srgn_state;
110 /* subregion reads - for host mode */
113 /* below information is used by rsp_list */
114 struct list_head list_act_srgn;
117 struct ufshpb_region {
118 struct ufshpb_lu *hpb;
119 struct ufshpb_subregion *srgn_tbl;
120 enum HPB_RGN_STATE rgn_state;
124 /* below information is used by rsp_list */
125 struct list_head list_inact_rgn;
127 /* below information is used by lru */
128 struct list_head list_lru_rgn;
129 unsigned long rgn_flags;
130 #define RGN_FLAG_DIRTY 0
131 #define RGN_FLAG_UPDATE 1
133 /* region reads - for host mode */
136 /* region "cold" timer - for host mode */
137 ktime_t read_timeout;
138 unsigned int read_timeout_expiries;
139 struct list_head list_expired_rgn;
142 #define for_each_sub_region(rgn, i, srgn) \
144 ((i) < (rgn)->srgn_cnt) && ((srgn) = &(rgn)->srgn_tbl[i]); \
148 * struct ufshpb_req - HPB related request structure (write/read buffer)
149 * @req: block layer request structure
150 * @bio: bio for this request
151 * @hpb: ufshpb_lu structure that related to
152 * @list_req: ufshpb_req mempool list
153 * @sense: store its sense data
154 * @mctx: L2P map information
155 * @rgn_idx: target region index
156 * @srgn_idx: target sub-region index
157 * @lun: target logical unit number
158 * @m_page: L2P map information data for pre-request
159 * @len: length of host-side cached L2P map in m_page
160 * @lpn: start LPN of L2P map in m_page
165 struct ufshpb_lu *hpb;
166 struct list_head list_req;
169 struct ufshpb_map_ctx *mctx;
170 unsigned int rgn_idx;
171 unsigned int srgn_idx;
182 struct victim_select_info {
183 struct list_head lh_lru_rgn; /* LRU list of regions */
184 int max_lru_active_cnt; /* supported hpb #region - pinned #region */
189 * ufshpb_params - ufs hpb parameters
190 * @requeue_timeout_ms - requeue threshold of wb command (0x2)
191 * @activation_thld - min reads [IOs] to activate/update a region
192 * @normalization_factor - shift right the region's reads
193 * @eviction_thld_enter - min reads [IOs] for the entering region in eviction
194 * @eviction_thld_exit - max reads [IOs] for the exiting region in eviction
195 * @read_timeout_ms - timeout [ms] from the last read IO to the region
196 * @read_timeout_expiries - amount of allowable timeout expireis
197 * @timeout_polling_interval_ms - frequency in which timeouts are checked
198 * @inflight_map_req - number of inflight map requests
200 struct ufshpb_params {
201 unsigned int requeue_timeout_ms;
202 unsigned int activation_thld;
203 unsigned int normalization_factor;
204 unsigned int eviction_thld_enter;
205 unsigned int eviction_thld_exit;
206 unsigned int read_timeout_ms;
207 unsigned int read_timeout_expiries;
208 unsigned int timeout_polling_interval_ms;
209 unsigned int inflight_map_req;
212 struct ufshpb_stats {
225 struct scsi_device *sdev_ufs_lu;
227 spinlock_t rgn_state_lock; /* for protect rgn/srgn state */
228 struct ufshpb_region *rgn_tbl;
232 spinlock_t rsp_list_lock;
233 struct list_head lh_act_srgn; /* hold rsp_list_lock */
234 struct list_head lh_inact_rgn; /* hold rsp_list_lock */
236 /* pre request information */
237 struct ufshpb_req *pre_req;
238 int num_inflight_pre_req;
239 int throttle_pre_req;
240 int num_inflight_map_req; /* hold param_lock */
241 spinlock_t param_lock;
243 struct list_head lh_pre_req_free;
244 int pre_req_max_tr_len;
246 /* cached L2P map management worker */
247 struct work_struct map_work;
249 /* for selecting victim */
250 struct victim_select_info lru_info;
251 struct work_struct ufshpb_normalization_work;
252 struct delayed_work ufshpb_read_to_work;
253 unsigned long work_data_bits;
254 #define TIMEOUT_WORK_RUNNING 0
256 /* pinned region information */
260 /* HPB related configuration */
263 u32 last_srgn_entries;
266 u32 entries_per_rgn_mask;
267 u32 entries_per_rgn_shift;
268 u32 entries_per_srgn;
269 u32 entries_per_srgn_mask;
270 u32 entries_per_srgn_shift;
275 struct ufshpb_stats stats;
276 struct ufshpb_params params;
278 struct kmem_cache *map_req_cache;
279 struct kmem_cache *m_page_cache;
281 struct list_head list_hpb_lu;
287 #ifndef CONFIG_SCSI_UFS_HPB
288 static int ufshpb_prep(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) { return 0; }
289 static void ufshpb_rsp_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) {}
290 static void ufshpb_resume(struct ufs_hba *hba) {}
291 static void ufshpb_suspend(struct ufs_hba *hba) {}
292 static void ufshpb_reset(struct ufs_hba *hba) {}
293 static void ufshpb_reset_host(struct ufs_hba *hba) {}
294 static void ufshpb_init(struct ufs_hba *hba) {}
295 static void ufshpb_init_hpb_lu(struct ufs_hba *hba, struct scsi_device *sdev) {}
296 static void ufshpb_destroy_lu(struct ufs_hba *hba, struct scsi_device *sdev) {}
297 static void ufshpb_remove(struct ufs_hba *hba) {}
298 static bool ufshpb_is_allowed(struct ufs_hba *hba) { return false; }
299 static void ufshpb_get_geo_info(struct ufs_hba *hba, u8 *geo_buf) {}
300 static void ufshpb_get_dev_info(struct ufs_hba *hba, u8 *desc_buf) {}
301 static bool ufshpb_is_legacy(struct ufs_hba *hba) { return false; }
303 int ufshpb_prep(struct ufs_hba *hba, struct ufshcd_lrb *lrbp);
304 void ufshpb_rsp_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp);
305 void ufshpb_resume(struct ufs_hba *hba);
306 void ufshpb_suspend(struct ufs_hba *hba);
307 void ufshpb_reset(struct ufs_hba *hba);
308 void ufshpb_reset_host(struct ufs_hba *hba);
309 void ufshpb_init(struct ufs_hba *hba);
310 void ufshpb_init_hpb_lu(struct ufs_hba *hba, struct scsi_device *sdev);
311 void ufshpb_destroy_lu(struct ufs_hba *hba, struct scsi_device *sdev);
312 void ufshpb_remove(struct ufs_hba *hba);
313 bool ufshpb_is_allowed(struct ufs_hba *hba);
314 void ufshpb_get_geo_info(struct ufs_hba *hba, u8 *geo_buf);
315 void ufshpb_get_dev_info(struct ufs_hba *hba, u8 *desc_buf);
316 bool ufshpb_is_legacy(struct ufs_hba *hba);
317 extern struct attribute_group ufs_sysfs_hpb_stat_group;
318 extern struct attribute_group ufs_sysfs_hpb_param_group;
321 #endif /* End of Header */