summaryrefstree
path: root/kernel/drivers/scsi/ufs/ufsfeature.c
blob: 897f0834134e0a9bb809bb635be24d995b55bfab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
 * Universal Flash Storage Feature Support
 *
 * Copyright (C) 2017-2018 Samsung Electronics Co., Ltd.
 *
 * Authors:
 *	Yongmyung Lee <ymhungry.lee@samsung.com>
 *	Jinyoung Choi <j-young.choi@samsung.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * See the COPYING file in the top-level directory or visit
 * <http://www.gnu.org/licenses/gpl-2.0.html>
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * This program is provided "AS IS" and "WITH ALL FAULTS" and
 * without warranty of any kind. You are solely responsible for
 * determining the appropriateness of using and distributing
 * the program and assume all risks associated with your exercise
 * of rights with respect to the program, including but not limited
 * to infringement of third party rights, the risks and costs of
 * program errors, damage to or loss of data, programs or equipment,
 * and unavailability or interruption of operations. Under no
 * circumstances will the contributor of this Program be liable for
 * any damages of any kind arising from your use or distribution of
 * this program.
 *
 * The Linux Foundation chooses to take subject only to the GPLv2
 * license terms, and distributes only under these terms.
 */

#include "ufsfeature.h"
#include "ufshcd.h"

#if defined(CONFIG_UFSHPB)
#include "ufshpb.h"
#endif

/*
 * ufs feature common functions.
 */
static int ufsf_read_desc(struct ufs_hba *hba, u8 desc_id, u8 desc_index,
			  u8 selector, u8 *desc_buf, u32 size)
{
	int err = 0;

	pm_runtime_get_sync(hba->dev);

	err = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
					    desc_id, desc_index,
					    selector,
					    desc_buf, &size);
	if (err)
		ERR_MSG("reading Device Desc failed. err = %d", err);

	pm_runtime_put_sync(hba->dev);

	return err;
}

static int ufsf_read_dev_desc(struct ufsf_feature *ufsf, u8 selector)
{
	u8 desc_buf[UFSF_QUERY_DESC_DEVICE_MAX_SIZE];
	int ret;

	ret = ufsf_read_desc(ufsf->hba, QUERY_DESC_IDN_DEVICE, 0, selector,
			     desc_buf, UFSF_QUERY_DESC_DEVICE_MAX_SIZE);
	if (ret)
		return ret;

	ufsf->num_lu = desc_buf[DEVICE_DESC_PARAM_NUM_LU];
	INIT_INFO("device lu count %d", ufsf->num_lu);

	INIT_INFO("sel=%u length=%u(0x%x) bSupport=0x%.2x, extend=0x%.2x_%.2x",
		  selector, desc_buf[DEVICE_DESC_PARAM_LEN],
		  desc_buf[DEVICE_DESC_PARAM_LEN],
		  desc_buf[DEVICE_DESC_PARAM_UFS_FEAT],
		  desc_buf[DEVICE_DESC_PARAM_EX_FEAT_SUP+2],
		  desc_buf[DEVICE_DESC_PARAM_EX_FEAT_SUP+3]);

#if defined(CONFIG_UFSHPB)
	ufshpb_get_dev_info(&ufsf->hpb_dev_info, desc_buf);
#endif
	return 0;
}

static int ufsf_read_geo_desc(struct ufsf_feature *ufsf, u8 selector)
{
	u8 geo_buf[UFSF_QUERY_DESC_GEOMETRY_MAX_SIZE];
	int ret;

	ret = ufsf_read_desc(ufsf->hba, QUERY_DESC_IDN_GEOMETRY, 0, selector,
			     geo_buf, UFSF_QUERY_DESC_GEOMETRY_MAX_SIZE);
	if (ret)
		return ret;

#if defined(CONFIG_UFSHPB)
	if (ufsf->hpb_dev_info.hpb_device)
		ufshpb_get_geo_info(&ufsf->hpb_dev_info, geo_buf);
#endif
	return 0;
}

static int ufsf_read_unit_desc(struct ufsf_feature *ufsf, int lun, u8 selector)
{
	u8 unit_buf[UFSF_QUERY_DESC_UNIT_MAX_SIZE];
	int lu_enable, ret = 0;

	ret = ufsf_read_desc(ufsf->hba, QUERY_DESC_IDN_UNIT, lun, selector,
			     unit_buf, UFSF_QUERY_DESC_UNIT_MAX_SIZE);
	if (ret) {
		ERR_MSG("read unit desc failed. ret %d", ret);
		goto out;
	}

	lu_enable = unit_buf[UNIT_DESC_PARAM_LU_ENABLE];
	if (!lu_enable)
		return 0;

#if defined(CONFIG_UFSHPB)
	if (ufsf->hpb_dev_info.hpb_device) {
		ret = ufshpb_get_lu_info(ufsf, lun, unit_buf);
		if (ret == -ENOMEM)
			goto out;
	}
#endif
out:
	return ret;
}

int ufsf_hpb_dt_check(struct ufs_hba *hba)
{
	struct device_node *node = hba->dev->of_node;
	struct ufsf_feature *ufsf = &hba->ufsf;
	int dt_hpb_enable = 1;
	bool dev_hpb_enable = 0;
	int err;

	if ((ufsf->hpb_dev_info.hpb_ver < UFSHPB_HPBEN_VER) ||
	    (ufsf->hpb_dev_info.hpb_ver > UFSHPB_MAX_VER))
		return dt_hpb_enable;

	err = of_property_read_u32(node, "hpb-enable", &dt_hpb_enable);
	if (err) {
		ERR_MSG("of_property_read_u32 failed...err %d", err);		
		dt_hpb_enable = 0;
	}

	pm_runtime_get_sync(hba->dev);
	err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
				      QUERY_FLAG_IDN_HPB_EN, &dev_hpb_enable);
	if (err) {
		ERR_MSG("read flag [0x%.2X] failed...err %d",
			QUERY_FLAG_IDN_HPB_EN, err);
		goto query_fail;
	}

	INIT_INFO("HPB enable DT %d - Dev %d", dt_hpb_enable, dev_hpb_enable);

	if (!dt_hpb_enable && dev_hpb_enable) {
		err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
					      QUERY_FLAG_IDN_HPB_EN, NULL);
		if (err) {
			ERR_MSG("clear flag [0x%.2X] failed...err%d",
				QUERY_FLAG_IDN_HPB_EN, err);
			goto query_fail;
		}
	} else if (dt_hpb_enable && !dev_hpb_enable) {
		err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
					      QUERY_FLAG_IDN_HPB_EN, NULL);
		if (err) {
			ERR_MSG("set flag [0x%.2X] failed...err %d",
				QUERY_FLAG_IDN_HPB_EN, err);
			goto query_fail;
		}
	}
	pm_runtime_put_sync(hba->dev);

	return dt_hpb_enable;

query_fail:
	pm_runtime_put_sync(hba->dev);
	return 0;
}

void ufsf_device_check(struct ufs_hba *hba)
{
	struct ufsf_feature *ufsf = &hba->ufsf;
	int ret, lun;

	ufsf->slave_conf_cnt = 0;

	ufsf->hba = hba;

	ret = ufsf_read_dev_desc(ufsf, UFSFEATURE_SELECTOR);
	if (ret)
		return;

	ret = ufsf_hpb_dt_check(hba);
	if (!ret)
		goto dt_hpb_disable;

	ret = ufsf_read_geo_desc(ufsf, UFSFEATURE_SELECTOR);
	if (ret)
		return;

	seq_scan_lu(lun) {
		ret = ufsf_read_unit_desc(ufsf, lun, UFSFEATURE_SELECTOR);
		if (ret == -ENOMEM)
			goto out_free_mem;
	}

	return;
out_free_mem:
#if defined(CONFIG_UFSHPB)
	seq_scan_lu(lun) {
		kfree(ufsf->ufshpb_lup[lun]);
		ufsf->ufshpb_lup[lun] = NULL;
	}

	/* don't call init handler */
	ufsf->ufshpb_state = HPB_FAILED;
#endif
	return;
dt_hpb_disable:
#if defined(CONFIG_UFSHPB)
	/* don't call init handler */
	ufsf->ufshpb_state = HPB_FAILED;
#endif
}

static void ufsf_print_query_buf(unsigned char *field, int size)
{
	unsigned char buf[255];
	int count = 0;
	int i;

	count += snprintf(buf, 8, "(0x00):");

	for (i = 0; i < size; i++) {
		count += snprintf(buf + count, 4, " %.2X", field[i]);

		if ((i + 1) % 16 == 0) {
			buf[count] = '\n';
			buf[count + 1] = '\0';
			printk("%s", buf);
			count = 0;
			count += snprintf(buf, 8, "(0x%.2X):", i + 1);
		} else if ((i + 1) % 4 == 0)
			count += snprintf(buf + count, 3, " :");
	}
	buf[count] = '\n';
	buf[count + 1] = '\0';
	printk("%s",buf);
}

inline int ufsf_check_query(__u32 opcode)
{
	return (opcode & 0xffff0000) >> 16 == UFSFEATURE_QUERY_OPCODE;
}

int ufsf_query_ioctl(struct ufsf_feature *ufsf, int lun, void __user *buffer,
		     struct ufs_ioctl_query_data *ioctl_data, u8 selector)
{
	unsigned char *kernel_buf;
	int opcode;
	int err = 0;
	int index = 0;
	int length = 0;
	unsigned int buf_len = 0;

	opcode = ioctl_data->opcode & 0xffff;

	INFO_MSG("op %u idn %u sel %u size %u(0x%X)", opcode, ioctl_data->idn,
		 selector, ioctl_data->buf_size, ioctl_data->buf_size);

	buf_len = (ioctl_data->idn == QUERY_DESC_IDN_STRING) ?
		IOCTL_DEV_CTX_MAX_SIZE : QUERY_DESC_MAX_SIZE;

	 if (buf_len < ioctl_data->buf_size) {
		 ERR_MSG("Invalid ioctl_data->buf_size : %u, buf_len : %u", ioctl_data->buf_size, buf_len);
		 err = -EINVAL;
		 goto out;
	 }

	kernel_buf = kzalloc(buf_len, GFP_KERNEL);
	if (!kernel_buf) {
		err = -ENOMEM;
		goto out;
	}

	switch (opcode) {
	case UPIU_QUERY_OPCODE_WRITE_DESC:
		err = copy_from_user(kernel_buf, buffer +
				     sizeof(struct ufs_ioctl_query_data),
				     ioctl_data->buf_size);
		INFO_MSG("buf size %d", ioctl_data->buf_size);
		ufsf_print_query_buf(kernel_buf, ioctl_data->buf_size);
		if (err)
			goto out_release_mem;
		break;

	case UPIU_QUERY_OPCODE_READ_DESC:
		switch (ioctl_data->idn) {
		case QUERY_DESC_IDN_UNIT:
			if (!ufs_is_valid_unit_desc_lun(lun)) {
				ERR_MSG("No unit descriptor for lun 0x%x", lun);
				err = -EINVAL;
				goto out_release_mem;
			}
			index = lun;
			INFO_MSG("read lu desc lun: %d", index);
			break;

		case QUERY_DESC_IDN_STRING:
#if defined(CONFIG_UFSHPB)
			if (!ufs_is_valid_unit_desc_lun(lun)) {
				ERR_MSG("No unit descriptor for lun 0x%x", lun);
				err = -EINVAL;
				goto out_release_mem;
			}
			err = ufshpb_issue_req_dev_ctx(ufsf->ufshpb_lup[lun],
						       kernel_buf,
						       ioctl_data->buf_size);
			if (err < 0)
				goto out_release_mem;

			goto copy_buffer;
#endif
		case QUERY_DESC_IDN_DEVICE:
		case QUERY_DESC_IDN_GEOMETRY:
		case QUERY_DESC_IDN_CONFIGURATION:
			break;

		default:
			ERR_MSG("invalid idn %d", ioctl_data->idn);
			err = -EINVAL;
			goto out_release_mem;
		}
		break;
	default:
		ERR_MSG("invalid opcode %d", opcode);
		err = -EINVAL;
		goto out_release_mem;
	}

	length = ioctl_data->buf_size;

	err = ufshcd_query_descriptor_retry(ufsf->hba, opcode, ioctl_data->idn,
					    index, selector, kernel_buf,
					    &length);
	if (err)
		goto out_release_mem;

#if defined(CONFIG_UFSHPB)
copy_buffer:
#endif
	if (opcode == UPIU_QUERY_OPCODE_READ_DESC) {
		err = copy_to_user(buffer, ioctl_data,
				   sizeof(struct ufs_ioctl_query_data));
		if (err)
			ERR_MSG("Failed copying back to user.");

		err = copy_to_user(buffer + sizeof(struct ufs_ioctl_query_data),
				   kernel_buf, ioctl_data->buf_size);
		if (err)
			ERR_MSG("Fail: copy rsp_buffer to user space.");
	}
out_release_mem:
	kfree(kernel_buf);
out:
	return err;
}

inline bool ufsf_is_valid_lun(int lun)
{
	return lun < UFS_UPIU_MAX_GENERAL_LUN;
}

/*
 * Wrapper functions for ufshpb.
 */
#if defined(CONFIG_UFSHPB)
inline int ufsf_hpb_prepare_pre_req(struct ufsf_feature *ufsf,
				    struct scsi_cmnd *cmd, int lun)
{
	if (ufsf->ufshpb_state == HPB_PRESENT)
		return ufshpb_prepare_pre_req(ufsf, cmd, lun);
	return -ENODEV;
}

inline int ufsf_hpb_prepare_add_lrbp(struct ufsf_feature *ufsf, int add_tag)
{
	if (ufsf->ufshpb_state == HPB_PRESENT)
		return ufshpb_prepare_add_lrbp(ufsf, add_tag);
	return -ENODEV;
}

inline void ufsf_hpb_end_pre_req(struct ufsf_feature *ufsf,
				 struct request *req)
{
	ufshpb_end_pre_req(ufsf, req);
}

inline void ufsf_hpb_change_lun(struct ufsf_feature *ufsf,
				struct ufshcd_lrb *lrbp)
{
	int ctx_lba = LI_EN_32(lrbp->cmd->cmnd + 2);

	if (ufsf->ufshpb_state == HPB_PRESENT &&
	    ufsf->issue_ioctl == true && ctx_lba == READ10_DEBUG_LBA) {
		lrbp->lun = READ10_DEBUG_LUN;
		INFO_MSG("lun 0x%X lba 0x%X", lrbp->lun, ctx_lba);
	}
}

inline void ufsf_hpb_prep_fn(struct ufsf_feature *ufsf,
			     struct ufshcd_lrb *lrbp)
{
	if (ufsf->ufshpb_state == HPB_PRESENT
	    && ufsf->issue_ioctl == false)
		ufshpb_prep_fn(ufsf, lrbp);
}

inline void ufsf_hpb_noti_rb(struct ufsf_feature *ufsf, struct ufshcd_lrb *lrbp)
{
	if (ufsf->ufshpb_state == HPB_PRESENT)
		ufshpb_rsp_upiu(ufsf, lrbp);
}

inline void ufsf_hpb_wakeup_worker_on_idle(struct ufsf_feature *ufsf)
{
	if (ufsf->ufshpb_state == HPB_PRESENT)
		ufshpb_wakeup_worker_on_idle(ufsf);
}

inline void ufsf_hpb_reset_lu(struct ufsf_feature *ufsf)
{
	ufsf->ufshpb_state = HPB_RESET;
	schedule_work(&ufsf->ufshpb_reset_work);
}

inline void ufsf_hpb_reset_host(struct ufsf_feature *ufsf)
{
	if (ufsf->ufshpb_state == HPB_PRESENT)
		ufsf->ufshpb_state = HPB_RESET;
}

inline void ufsf_hpb_init(struct ufsf_feature *ufsf)
{
	if (ufsf->hpb_dev_info.hpb_device &&
	    ufsf->ufshpb_state == HPB_NEED_INIT) {
		INIT_DELAYED_WORK(&ufsf->ufshpb_init_work, ufshpb_init_handler);
		schedule_delayed_work(&ufsf->ufshpb_init_work,
				      msecs_to_jiffies(10000));
	}
}

inline void ufsf_hpb_reset(struct ufsf_feature *ufsf)
{
	if (ufsf->hpb_dev_info.hpb_device &&
	    ufsf->ufshpb_state == HPB_RESET) {
		schedule_work(&ufsf->ufshpb_reset_work);
	}
}

inline void ufsf_hpb_suspend(struct ufsf_feature *ufsf)
{
	/*
	 * if suspend failed, pm could call the suspend function again,
	 * in this case, ufshpb state already had been changed to SUSPEND state.
	 * so, we will not call ufshpb_suspend.
	 * */
	if (ufsf->ufshpb_state == HPB_PRESENT)
		ufshpb_suspend(ufsf);
}

inline void ufsf_hpb_resume(struct ufsf_feature *ufsf)
{
	if (ufsf->ufshpb_state == HPB_SUSPEND ||
	    ufsf->ufshpb_state == HPB_PRESENT) {
		if (ufsf->ufshpb_state == HPB_PRESENT)
			WARNING_MSG("warning.. hpb state PRESENT in resuming");
		ufshpb_resume(ufsf);
	}
}

inline void ufsf_hpb_release(struct ufsf_feature *ufsf)
{
	ufshpb_release(ufsf, HPB_NEED_INIT);
}

inline void ufsf_hpb_set_init_state(struct ufsf_feature *ufsf)
{
	ufsf->ufshpb_state = HPB_NEED_INIT;
}
#else
inline int ufsf_hpb_prepare_pre_req(struct ufsf_feature *ufsf,
				    struct scsi_cmnd *cmd, int lun)
{
	return 0;
}

inline int ufsf_hpb_prepare_add_lrbp(struct ufsf_feature *ufsf, int add_tag)
{
	return 0;
}

inline void ufsf_hpb_end_pre_req(struct ufsf_feature *ufsf,
				 struct request *req) {}
inline void ufsf_hpb_change_lun(struct ufsf_feature *ufsf,
				struct ufshcd_lrb *lrbp) {}
inline void ufsf_hpb_prep_fn(struct ufsf_feature *ufsf,
			     struct ufshcd_lrb *lrbp) {}
inline void ufsf_hpb_wakeup_worker_on_idle(struct ufsf_feature *ufsf) {}
inline void ufsf_hpb_noti_rb(struct ufsf_feature *ufsf,
			     struct ufshcd_lrb *lrbp) {}
inline void ufsf_hpb_reset_lu(struct ufsf_feature *ufsf) {}
inline void ufsf_hpb_reset_host(struct ufsf_feature *ufsf) {}
inline void ufsf_hpb_init(struct ufsf_feature *ufsf) {}
inline void ufsf_hpb_reset(struct ufsf_feature *ufsf) {}
inline void ufsf_hpb_suspend(struct ufsf_feature *ufsf) {}
inline void ufsf_hpb_resume(struct ufsf_feature *ufsf) {}
inline void ufsf_hpb_release(struct ufsf_feature *ufsf) {}
inline void ufsf_hpb_set_init_state(struct ufsf_feature *ufsf) {}
#endif