Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Block driver for media (i.e., flash cards) | |
3 | * | |
4 | * Copyright 2002 Hewlett-Packard Company | |
979ce720 | 5 | * Copyright 2005-2008 Pierre Ossman |
1da177e4 LT |
6 | * |
7 | * Use consistent with the GNU GPL is permitted, | |
8 | * provided that this copyright notice is | |
9 | * preserved in its entirety in all copies and derived works. | |
10 | * | |
11 | * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED, | |
12 | * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS | |
13 | * FITNESS FOR ANY PARTICULAR PURPOSE. | |
14 | * | |
15 | * Many thanks to Alessandro Rubini and Jonathan Corbet! | |
16 | * | |
17 | * Author: Andrew Christian | |
18 | * 28 May 2002 | |
19 | */ | |
20 | #include <linux/moduleparam.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/init.h> | |
23 | ||
1da177e4 LT |
24 | #include <linux/kernel.h> |
25 | #include <linux/fs.h> | |
5a0e3ad6 | 26 | #include <linux/slab.h> |
1da177e4 LT |
27 | #include <linux/errno.h> |
28 | #include <linux/hdreg.h> | |
29 | #include <linux/kdev_t.h> | |
30 | #include <linux/blkdev.h> | |
97548575 | 31 | #include <linux/cdev.h> |
a621aaed | 32 | #include <linux/mutex.h> |
ec5a19dd | 33 | #include <linux/scatterlist.h> |
a7bbb573 | 34 | #include <linux/string_helpers.h> |
cb87ea28 JC |
35 | #include <linux/delay.h> |
36 | #include <linux/capability.h> | |
37 | #include <linux/compat.h> | |
e94cfef6 | 38 | #include <linux/pm_runtime.h> |
b10fa99e | 39 | #include <linux/idr.h> |
627c3ccf | 40 | #include <linux/debugfs.h> |
1da177e4 | 41 | |
cb87ea28 | 42 | #include <linux/mmc/ioctl.h> |
1da177e4 | 43 | #include <linux/mmc/card.h> |
385e3227 | 44 | #include <linux/mmc/host.h> |
da7fbe58 PO |
45 | #include <linux/mmc/mmc.h> |
46 | #include <linux/mmc/sd.h> | |
1da177e4 | 47 | |
7c0f6ba6 | 48 | #include <linux/uaccess.h> |
1da177e4 | 49 | |
98ac2162 | 50 | #include "queue.h" |
48ab086d | 51 | #include "block.h" |
55244c56 | 52 | #include "core.h" |
4facdde1 | 53 | #include "card.h" |
5857b29b | 54 | #include "host.h" |
4facdde1 | 55 | #include "bus.h" |
55244c56 | 56 | #include "mmc_ops.h" |
28fc64af | 57 | #include "quirks.h" |
55244c56 | 58 | #include "sd_ops.h" |
1da177e4 | 59 | |
6b0b6285 | 60 | MODULE_ALIAS("mmc:block"); |
5e71b7a6 OJ |
61 | #ifdef MODULE_PARAM_PREFIX |
62 | #undef MODULE_PARAM_PREFIX | |
63 | #endif | |
64 | #define MODULE_PARAM_PREFIX "mmcblk." | |
65 | ||
8fee476b | 66 | #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */ |
775a9362 ME |
67 | #define MMC_SANITIZE_REQ_TIMEOUT 240000 |
68 | #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16) | |
6a7a6b45 | 69 | |
d3df0465 | 70 | #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \ |
ce39f9d1 | 71 | (rq_data_dir(req) == WRITE)) |
5e71b7a6 | 72 | static DEFINE_MUTEX(block_mutex); |
6b0b6285 | 73 | |
1da177e4 | 74 | /* |
5e71b7a6 OJ |
75 | * The defaults come from config options but can be overriden by module |
76 | * or bootarg options. | |
1da177e4 | 77 | */ |
5e71b7a6 | 78 | static int perdev_minors = CONFIG_MMC_BLOCK_MINORS; |
1dff3144 | 79 | |
5e71b7a6 OJ |
80 | /* |
81 | * We've only got one major, so number of mmcblk devices is | |
a26eba61 | 82 | * limited to (1 << 20) / number of minors per device. It is also |
b10fa99e | 83 | * limited by the MAX_DEVICES below. |
5e71b7a6 OJ |
84 | */ |
85 | static int max_devices; | |
86 | ||
a26eba61 BH |
87 | #define MAX_DEVICES 256 |
88 | ||
b10fa99e | 89 | static DEFINE_IDA(mmc_blk_ida); |
97548575 | 90 | static DEFINE_IDA(mmc_rpmb_ida); |
1da177e4 | 91 | |
1da177e4 LT |
92 | /* |
93 | * There is one mmc_blk_data per slot. | |
94 | */ | |
95 | struct mmc_blk_data { | |
96 | spinlock_t lock; | |
307d8e6f | 97 | struct device *parent; |
1da177e4 LT |
98 | struct gendisk *disk; |
99 | struct mmc_queue queue; | |
371a689f | 100 | struct list_head part; |
97548575 | 101 | struct list_head rpmbs; |
1da177e4 | 102 | |
d0c97cfb AW |
103 | unsigned int flags; |
104 | #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */ | |
105 | #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */ | |
106 | ||
1da177e4 | 107 | unsigned int usage; |
a6f6c96b | 108 | unsigned int read_only; |
371a689f | 109 | unsigned int part_type; |
67716327 AH |
110 | unsigned int reset_done; |
111 | #define MMC_BLK_READ BIT(0) | |
112 | #define MMC_BLK_WRITE BIT(1) | |
113 | #define MMC_BLK_DISCARD BIT(2) | |
114 | #define MMC_BLK_SECDISCARD BIT(3) | |
1e8e55b6 | 115 | #define MMC_BLK_CQE_RECOVERY BIT(4) |
371a689f AW |
116 | |
117 | /* | |
118 | * Only set in main mmc_blk_data associated | |
fc95e30b | 119 | * with mmc_card with dev_set_drvdata, and keeps |
371a689f AW |
120 | * track of the current selected device partition. |
121 | */ | |
122 | unsigned int part_curr; | |
123 | struct device_attribute force_ro; | |
add710ea JR |
124 | struct device_attribute power_ro_lock; |
125 | int area_type; | |
f9f0da98 AH |
126 | |
127 | /* debugfs files (only in main mmc_blk_data) */ | |
128 | struct dentry *status_dentry; | |
129 | struct dentry *ext_csd_dentry; | |
1da177e4 LT |
130 | }; |
131 | ||
97548575 LW |
132 | /* Device type for RPMB character devices */ |
133 | static dev_t mmc_rpmb_devt; | |
134 | ||
135 | /* Bus type for RPMB character devices */ | |
136 | static struct bus_type mmc_rpmb_bus_type = { | |
137 | .name = "mmc_rpmb", | |
138 | }; | |
139 | ||
140 | /** | |
141 | * struct mmc_rpmb_data - special RPMB device type for these areas | |
142 | * @dev: the device for the RPMB area | |
143 | * @chrdev: character device for the RPMB area | |
144 | * @id: unique device ID number | |
145 | * @part_index: partition index (0 on first) | |
146 | * @md: parent MMC block device | |
147 | * @node: list item, so we can put this device on a list | |
148 | */ | |
149 | struct mmc_rpmb_data { | |
150 | struct device dev; | |
151 | struct cdev chrdev; | |
152 | int id; | |
153 | unsigned int part_index; | |
154 | struct mmc_blk_data *md; | |
155 | struct list_head node; | |
156 | }; | |
157 | ||
a621aaed | 158 | static DEFINE_MUTEX(open_lock); |
1da177e4 | 159 | |
5e71b7a6 OJ |
160 | module_param(perdev_minors, int, 0444); |
161 | MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device"); | |
162 | ||
8d1e977d | 163 | static inline int mmc_blk_part_switch(struct mmc_card *card, |
1f797edc | 164 | unsigned int part_type); |
cdf8a6fb | 165 | |
1da177e4 LT |
166 | static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk) |
167 | { | |
168 | struct mmc_blk_data *md; | |
169 | ||
a621aaed | 170 | mutex_lock(&open_lock); |
1da177e4 LT |
171 | md = disk->private_data; |
172 | if (md && md->usage == 0) | |
173 | md = NULL; | |
174 | if (md) | |
175 | md->usage++; | |
a621aaed | 176 | mutex_unlock(&open_lock); |
1da177e4 LT |
177 | |
178 | return md; | |
179 | } | |
180 | ||
371a689f AW |
181 | static inline int mmc_get_devidx(struct gendisk *disk) |
182 | { | |
382c55f8 | 183 | int devidx = disk->first_minor / perdev_minors; |
371a689f AW |
184 | return devidx; |
185 | } | |
186 | ||
1da177e4 LT |
187 | static void mmc_blk_put(struct mmc_blk_data *md) |
188 | { | |
a621aaed | 189 | mutex_lock(&open_lock); |
1da177e4 LT |
190 | md->usage--; |
191 | if (md->usage == 0) { | |
371a689f | 192 | int devidx = mmc_get_devidx(md->disk); |
41e3efd0 | 193 | blk_put_queue(md->queue.queue); |
a04848c7 | 194 | ida_simple_remove(&mmc_blk_ida, devidx); |
1da177e4 | 195 | put_disk(md->disk); |
1da177e4 LT |
196 | kfree(md); |
197 | } | |
a621aaed | 198 | mutex_unlock(&open_lock); |
1da177e4 LT |
199 | } |
200 | ||
add710ea JR |
201 | static ssize_t power_ro_lock_show(struct device *dev, |
202 | struct device_attribute *attr, char *buf) | |
203 | { | |
204 | int ret; | |
205 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); | |
206 | struct mmc_card *card = md->queue.card; | |
207 | int locked = 0; | |
208 | ||
209 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN) | |
210 | locked = 2; | |
211 | else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN) | |
212 | locked = 1; | |
213 | ||
214 | ret = snprintf(buf, PAGE_SIZE, "%d\n", locked); | |
215 | ||
9098f84c TW |
216 | mmc_blk_put(md); |
217 | ||
add710ea JR |
218 | return ret; |
219 | } | |
220 | ||
221 | static ssize_t power_ro_lock_store(struct device *dev, | |
222 | struct device_attribute *attr, const char *buf, size_t count) | |
223 | { | |
224 | int ret; | |
225 | struct mmc_blk_data *md, *part_md; | |
0493f6fe LW |
226 | struct mmc_queue *mq; |
227 | struct request *req; | |
add710ea JR |
228 | unsigned long set; |
229 | ||
230 | if (kstrtoul(buf, 0, &set)) | |
231 | return -EINVAL; | |
232 | ||
233 | if (set != 1) | |
234 | return count; | |
235 | ||
236 | md = mmc_blk_get(dev_to_disk(dev)); | |
0493f6fe | 237 | mq = &md->queue; |
add710ea | 238 | |
0493f6fe LW |
239 | /* Dispatch locking to the block layer */ |
240 | req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, __GFP_RECLAIM); | |
fb8e456e AH |
241 | if (IS_ERR(req)) { |
242 | count = PTR_ERR(req); | |
243 | goto out_put; | |
244 | } | |
0493f6fe LW |
245 | req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP; |
246 | blk_execute_rq(mq->queue, NULL, req, 0); | |
247 | ret = req_to_mmc_queue_req(req)->drv_op_result; | |
34c089e8 | 248 | blk_put_request(req); |
add710ea JR |
249 | |
250 | if (!ret) { | |
251 | pr_info("%s: Locking boot partition ro until next power on\n", | |
252 | md->disk->disk_name); | |
253 | set_disk_ro(md->disk, 1); | |
254 | ||
255 | list_for_each_entry(part_md, &md->part, part) | |
256 | if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) { | |
257 | pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name); | |
258 | set_disk_ro(part_md->disk, 1); | |
259 | } | |
260 | } | |
fb8e456e | 261 | out_put: |
add710ea JR |
262 | mmc_blk_put(md); |
263 | return count; | |
264 | } | |
265 | ||
371a689f AW |
266 | static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr, |
267 | char *buf) | |
268 | { | |
269 | int ret; | |
270 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); | |
271 | ||
0031a98a | 272 | ret = snprintf(buf, PAGE_SIZE, "%d\n", |
371a689f AW |
273 | get_disk_ro(dev_to_disk(dev)) ^ |
274 | md->read_only); | |
275 | mmc_blk_put(md); | |
276 | return ret; | |
277 | } | |
278 | ||
279 | static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr, | |
280 | const char *buf, size_t count) | |
281 | { | |
282 | int ret; | |
283 | char *end; | |
284 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); | |
285 | unsigned long set = simple_strtoul(buf, &end, 0); | |
286 | if (end == buf) { | |
287 | ret = -EINVAL; | |
288 | goto out; | |
289 | } | |
290 | ||
291 | set_disk_ro(dev_to_disk(dev), set || md->read_only); | |
292 | ret = count; | |
293 | out: | |
294 | mmc_blk_put(md); | |
295 | return ret; | |
296 | } | |
297 | ||
a5a1561f | 298 | static int mmc_blk_open(struct block_device *bdev, fmode_t mode) |
1da177e4 | 299 | { |
a5a1561f | 300 | struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk); |
1da177e4 LT |
301 | int ret = -ENXIO; |
302 | ||
2a48fc0a | 303 | mutex_lock(&block_mutex); |
1da177e4 LT |
304 | if (md) { |
305 | if (md->usage == 2) | |
a5a1561f | 306 | check_disk_change(bdev); |
1da177e4 | 307 | ret = 0; |
a00fc090 | 308 | |
a5a1561f | 309 | if ((mode & FMODE_WRITE) && md->read_only) { |
70bb0896 | 310 | mmc_blk_put(md); |
a00fc090 | 311 | ret = -EROFS; |
70bb0896 | 312 | } |
1da177e4 | 313 | } |
2a48fc0a | 314 | mutex_unlock(&block_mutex); |
1da177e4 LT |
315 | |
316 | return ret; | |
317 | } | |
318 | ||
db2a144b | 319 | static void mmc_blk_release(struct gendisk *disk, fmode_t mode) |
1da177e4 | 320 | { |
a5a1561f | 321 | struct mmc_blk_data *md = disk->private_data; |
1da177e4 | 322 | |
2a48fc0a | 323 | mutex_lock(&block_mutex); |
1da177e4 | 324 | mmc_blk_put(md); |
2a48fc0a | 325 | mutex_unlock(&block_mutex); |
1da177e4 LT |
326 | } |
327 | ||
328 | static int | |
a885c8c4 | 329 | mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
1da177e4 | 330 | { |
a885c8c4 CH |
331 | geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16); |
332 | geo->heads = 4; | |
333 | geo->sectors = 16; | |
334 | return 0; | |
1da177e4 LT |
335 | } |
336 | ||
cb87ea28 JC |
337 | struct mmc_blk_ioc_data { |
338 | struct mmc_ioc_cmd ic; | |
339 | unsigned char *buf; | |
340 | u64 buf_bytes; | |
97548575 | 341 | struct mmc_rpmb_data *rpmb; |
cb87ea28 JC |
342 | }; |
343 | ||
344 | static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user( | |
345 | struct mmc_ioc_cmd __user *user) | |
346 | { | |
347 | struct mmc_blk_ioc_data *idata; | |
348 | int err; | |
349 | ||
1ff8950c | 350 | idata = kmalloc(sizeof(*idata), GFP_KERNEL); |
cb87ea28 JC |
351 | if (!idata) { |
352 | err = -ENOMEM; | |
aea253ec | 353 | goto out; |
cb87ea28 JC |
354 | } |
355 | ||
356 | if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) { | |
357 | err = -EFAULT; | |
aea253ec | 358 | goto idata_err; |
cb87ea28 JC |
359 | } |
360 | ||
361 | idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks; | |
362 | if (idata->buf_bytes > MMC_IOC_MAX_BYTES) { | |
363 | err = -EOVERFLOW; | |
aea253ec | 364 | goto idata_err; |
cb87ea28 JC |
365 | } |
366 | ||
bfe5b1b1 VV |
367 | if (!idata->buf_bytes) { |
368 | idata->buf = NULL; | |
4d6144de | 369 | return idata; |
bfe5b1b1 | 370 | } |
4d6144de | 371 | |
1ff8950c | 372 | idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL); |
cb87ea28 JC |
373 | if (!idata->buf) { |
374 | err = -ENOMEM; | |
aea253ec | 375 | goto idata_err; |
cb87ea28 JC |
376 | } |
377 | ||
378 | if (copy_from_user(idata->buf, (void __user *)(unsigned long) | |
379 | idata->ic.data_ptr, idata->buf_bytes)) { | |
380 | err = -EFAULT; | |
381 | goto copy_err; | |
382 | } | |
383 | ||
384 | return idata; | |
385 | ||
386 | copy_err: | |
387 | kfree(idata->buf); | |
aea253ec | 388 | idata_err: |
cb87ea28 | 389 | kfree(idata); |
aea253ec | 390 | out: |
cb87ea28 | 391 | return ERR_PTR(err); |
cb87ea28 JC |
392 | } |
393 | ||
a5f5774c JH |
394 | static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr, |
395 | struct mmc_blk_ioc_data *idata) | |
396 | { | |
397 | struct mmc_ioc_cmd *ic = &idata->ic; | |
398 | ||
399 | if (copy_to_user(&(ic_ptr->response), ic->response, | |
400 | sizeof(ic->response))) | |
401 | return -EFAULT; | |
402 | ||
403 | if (!idata->ic.write_flag) { | |
404 | if (copy_to_user((void __user *)(unsigned long)ic->data_ptr, | |
405 | idata->buf, idata->buf_bytes)) | |
406 | return -EFAULT; | |
407 | } | |
408 | ||
409 | return 0; | |
410 | } | |
411 | ||
8d1e977d LP |
412 | static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status, |
413 | u32 retries_max) | |
414 | { | |
415 | int err; | |
416 | u32 retry_count = 0; | |
417 | ||
418 | if (!status || !retries_max) | |
419 | return -EINVAL; | |
420 | ||
421 | do { | |
2185bc2c | 422 | err = __mmc_send_status(card, status, 5); |
8d1e977d LP |
423 | if (err) |
424 | break; | |
425 | ||
426 | if (!R1_STATUS(*status) && | |
427 | (R1_CURRENT_STATE(*status) != R1_STATE_PRG)) | |
428 | break; /* RPMB programming operation complete */ | |
429 | ||
430 | /* | |
431 | * Rechedule to give the MMC device a chance to continue | |
432 | * processing the previous command without being polled too | |
433 | * frequently. | |
434 | */ | |
435 | usleep_range(1000, 5000); | |
436 | } while (++retry_count < retries_max); | |
437 | ||
438 | if (retry_count == retries_max) | |
439 | err = -EPERM; | |
440 | ||
441 | return err; | |
442 | } | |
443 | ||
775a9362 ME |
444 | static int ioctl_do_sanitize(struct mmc_card *card) |
445 | { | |
446 | int err; | |
447 | ||
a2d1086d | 448 | if (!mmc_can_sanitize(card)) { |
775a9362 ME |
449 | pr_warn("%s: %s - SANITIZE is not supported\n", |
450 | mmc_hostname(card->host), __func__); | |
451 | err = -EOPNOTSUPP; | |
452 | goto out; | |
453 | } | |
454 | ||
455 | pr_debug("%s: %s - SANITIZE IN PROGRESS...\n", | |
456 | mmc_hostname(card->host), __func__); | |
457 | ||
458 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
459 | EXT_CSD_SANITIZE_START, 1, | |
460 | MMC_SANITIZE_REQ_TIMEOUT); | |
461 | ||
462 | if (err) | |
463 | pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n", | |
464 | mmc_hostname(card->host), __func__, err); | |
465 | ||
466 | pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host), | |
467 | __func__); | |
468 | out: | |
469 | return err; | |
470 | } | |
471 | ||
a5f5774c JH |
472 | static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md, |
473 | struct mmc_blk_ioc_data *idata) | |
cb87ea28 | 474 | { |
c7836d15 MY |
475 | struct mmc_command cmd = {}; |
476 | struct mmc_data data = {}; | |
477 | struct mmc_request mrq = {}; | |
cb87ea28 JC |
478 | struct scatterlist sg; |
479 | int err; | |
97548575 | 480 | unsigned int target_part; |
8d1e977d | 481 | u32 status = 0; |
cb87ea28 | 482 | |
a5f5774c JH |
483 | if (!card || !md || !idata) |
484 | return -EINVAL; | |
cb87ea28 | 485 | |
97548575 LW |
486 | /* |
487 | * The RPMB accesses comes in from the character device, so we | |
488 | * need to target these explicitly. Else we just target the | |
489 | * partition type for the block device the ioctl() was issued | |
490 | * on. | |
491 | */ | |
492 | if (idata->rpmb) { | |
493 | /* Support multiple RPMB partitions */ | |
494 | target_part = idata->rpmb->part_index; | |
495 | target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB; | |
496 | } else { | |
497 | target_part = md->part_type; | |
498 | } | |
8d1e977d | 499 | |
4d6144de JR |
500 | cmd.opcode = idata->ic.opcode; |
501 | cmd.arg = idata->ic.arg; | |
502 | cmd.flags = idata->ic.flags; | |
503 | ||
504 | if (idata->buf_bytes) { | |
505 | data.sg = &sg; | |
506 | data.sg_len = 1; | |
507 | data.blksz = idata->ic.blksz; | |
508 | data.blocks = idata->ic.blocks; | |
509 | ||
510 | sg_init_one(data.sg, idata->buf, idata->buf_bytes); | |
511 | ||
512 | if (idata->ic.write_flag) | |
513 | data.flags = MMC_DATA_WRITE; | |
514 | else | |
515 | data.flags = MMC_DATA_READ; | |
516 | ||
517 | /* data.flags must already be set before doing this. */ | |
518 | mmc_set_data_timeout(&data, card); | |
519 | ||
520 | /* Allow overriding the timeout_ns for empirical tuning. */ | |
521 | if (idata->ic.data_timeout_ns) | |
522 | data.timeout_ns = idata->ic.data_timeout_ns; | |
523 | ||
524 | if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) { | |
525 | /* | |
526 | * Pretend this is a data transfer and rely on the | |
527 | * host driver to compute timeout. When all host | |
528 | * drivers support cmd.cmd_timeout for R1B, this | |
529 | * can be changed to: | |
530 | * | |
531 | * mrq.data = NULL; | |
532 | * cmd.cmd_timeout = idata->ic.cmd_timeout_ms; | |
533 | */ | |
534 | data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000; | |
535 | } | |
536 | ||
537 | mrq.data = &data; | |
538 | } | |
539 | ||
540 | mrq.cmd = &cmd; | |
541 | ||
97548575 | 542 | err = mmc_blk_part_switch(card, target_part); |
8d1e977d | 543 | if (err) |
a5f5774c | 544 | return err; |
8d1e977d | 545 | |
cb87ea28 JC |
546 | if (idata->ic.is_acmd) { |
547 | err = mmc_app_cmd(card->host, card); | |
548 | if (err) | |
a5f5774c | 549 | return err; |
cb87ea28 JC |
550 | } |
551 | ||
97548575 | 552 | if (idata->rpmb) { |
8d1e977d LP |
553 | err = mmc_set_blockcount(card, data.blocks, |
554 | idata->ic.write_flag & (1 << 31)); | |
555 | if (err) | |
a5f5774c | 556 | return err; |
8d1e977d LP |
557 | } |
558 | ||
a82e484e YG |
559 | if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) && |
560 | (cmd.opcode == MMC_SWITCH)) { | |
775a9362 ME |
561 | err = ioctl_do_sanitize(card); |
562 | ||
563 | if (err) | |
564 | pr_err("%s: ioctl_do_sanitize() failed. err = %d", | |
565 | __func__, err); | |
566 | ||
a5f5774c | 567 | return err; |
775a9362 ME |
568 | } |
569 | ||
cb87ea28 JC |
570 | mmc_wait_for_req(card->host, &mrq); |
571 | ||
572 | if (cmd.error) { | |
573 | dev_err(mmc_dev(card->host), "%s: cmd error %d\n", | |
574 | __func__, cmd.error); | |
a5f5774c | 575 | return cmd.error; |
cb87ea28 JC |
576 | } |
577 | if (data.error) { | |
578 | dev_err(mmc_dev(card->host), "%s: data error %d\n", | |
579 | __func__, data.error); | |
a5f5774c | 580 | return data.error; |
cb87ea28 JC |
581 | } |
582 | ||
583 | /* | |
584 | * According to the SD specs, some commands require a delay after | |
585 | * issuing the command. | |
586 | */ | |
587 | if (idata->ic.postsleep_min_us) | |
588 | usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us); | |
589 | ||
a5f5774c | 590 | memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp)); |
cb87ea28 | 591 | |
97548575 | 592 | if (idata->rpmb) { |
8d1e977d LP |
593 | /* |
594 | * Ensure RPMB command has completed by polling CMD13 | |
595 | * "Send Status". | |
596 | */ | |
597 | err = ioctl_rpmb_card_status_poll(card, &status, 5); | |
598 | if (err) | |
599 | dev_err(mmc_dev(card->host), | |
600 | "%s: Card Status=0x%08X, error %d\n", | |
601 | __func__, status, err); | |
602 | } | |
603 | ||
a5f5774c JH |
604 | return err; |
605 | } | |
606 | ||
2fe20bae | 607 | static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md, |
97548575 LW |
608 | struct mmc_ioc_cmd __user *ic_ptr, |
609 | struct mmc_rpmb_data *rpmb) | |
a5f5774c JH |
610 | { |
611 | struct mmc_blk_ioc_data *idata; | |
3ecd8cf2 | 612 | struct mmc_blk_ioc_data *idatas[1]; |
614f0388 | 613 | struct mmc_queue *mq; |
a5f5774c | 614 | struct mmc_card *card; |
b093410c | 615 | int err = 0, ioc_err = 0; |
614f0388 | 616 | struct request *req; |
a5f5774c JH |
617 | |
618 | idata = mmc_blk_ioctl_copy_from_user(ic_ptr); | |
619 | if (IS_ERR(idata)) | |
620 | return PTR_ERR(idata); | |
97548575 LW |
621 | /* This will be NULL on non-RPMB ioctl():s */ |
622 | idata->rpmb = rpmb; | |
a5f5774c | 623 | |
a5f5774c JH |
624 | card = md->queue.card; |
625 | if (IS_ERR(card)) { | |
626 | err = PTR_ERR(card); | |
627 | goto cmd_done; | |
628 | } | |
629 | ||
614f0388 LW |
630 | /* |
631 | * Dispatch the ioctl() into the block request queue. | |
632 | */ | |
633 | mq = &md->queue; | |
634 | req = blk_get_request(mq->queue, | |
635 | idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, | |
636 | __GFP_RECLAIM); | |
fb8e456e AH |
637 | if (IS_ERR(req)) { |
638 | err = PTR_ERR(req); | |
639 | goto cmd_done; | |
640 | } | |
3ecd8cf2 | 641 | idatas[0] = idata; |
97548575 LW |
642 | req_to_mmc_queue_req(req)->drv_op = |
643 | rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL; | |
69f7599e | 644 | req_to_mmc_queue_req(req)->drv_op_data = idatas; |
3ecd8cf2 | 645 | req_to_mmc_queue_req(req)->ioc_count = 1; |
614f0388 | 646 | blk_execute_rq(mq->queue, NULL, req, 0); |
0493f6fe | 647 | ioc_err = req_to_mmc_queue_req(req)->drv_op_result; |
b093410c | 648 | err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata); |
614f0388 | 649 | blk_put_request(req); |
a5f5774c | 650 | |
cb87ea28 | 651 | cmd_done: |
cb87ea28 JC |
652 | kfree(idata->buf); |
653 | kfree(idata); | |
b093410c | 654 | return ioc_err ? ioc_err : err; |
cb87ea28 JC |
655 | } |
656 | ||
2fe20bae | 657 | static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md, |
97548575 LW |
658 | struct mmc_ioc_multi_cmd __user *user, |
659 | struct mmc_rpmb_data *rpmb) | |
a5f5774c JH |
660 | { |
661 | struct mmc_blk_ioc_data **idata = NULL; | |
662 | struct mmc_ioc_cmd __user *cmds = user->cmds; | |
663 | struct mmc_card *card; | |
3ecd8cf2 | 664 | struct mmc_queue *mq; |
b093410c | 665 | int i, err = 0, ioc_err = 0; |
a5f5774c | 666 | __u64 num_of_cmds; |
3ecd8cf2 | 667 | struct request *req; |
a5f5774c JH |
668 | |
669 | if (copy_from_user(&num_of_cmds, &user->num_of_cmds, | |
670 | sizeof(num_of_cmds))) | |
671 | return -EFAULT; | |
672 | ||
aab2ee03 GU |
673 | if (!num_of_cmds) |
674 | return 0; | |
675 | ||
a5f5774c JH |
676 | if (num_of_cmds > MMC_IOC_MAX_CMDS) |
677 | return -EINVAL; | |
678 | ||
679 | idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL); | |
680 | if (!idata) | |
681 | return -ENOMEM; | |
682 | ||
683 | for (i = 0; i < num_of_cmds; i++) { | |
684 | idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]); | |
685 | if (IS_ERR(idata[i])) { | |
686 | err = PTR_ERR(idata[i]); | |
687 | num_of_cmds = i; | |
688 | goto cmd_err; | |
689 | } | |
97548575 LW |
690 | /* This will be NULL on non-RPMB ioctl():s */ |
691 | idata[i]->rpmb = rpmb; | |
a5f5774c JH |
692 | } |
693 | ||
a5f5774c JH |
694 | card = md->queue.card; |
695 | if (IS_ERR(card)) { | |
696 | err = PTR_ERR(card); | |
2fe20bae | 697 | goto cmd_err; |
a5f5774c JH |
698 | } |
699 | ||
a5f5774c | 700 | |
3ecd8cf2 LW |
701 | /* |
702 | * Dispatch the ioctl()s into the block request queue. | |
703 | */ | |
704 | mq = &md->queue; | |
705 | req = blk_get_request(mq->queue, | |
706 | idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, | |
707 | __GFP_RECLAIM); | |
fb8e456e AH |
708 | if (IS_ERR(req)) { |
709 | err = PTR_ERR(req); | |
710 | goto cmd_err; | |
711 | } | |
97548575 LW |
712 | req_to_mmc_queue_req(req)->drv_op = |
713 | rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL; | |
69f7599e | 714 | req_to_mmc_queue_req(req)->drv_op_data = idata; |
3ecd8cf2 LW |
715 | req_to_mmc_queue_req(req)->ioc_count = num_of_cmds; |
716 | blk_execute_rq(mq->queue, NULL, req, 0); | |
0493f6fe | 717 | ioc_err = req_to_mmc_queue_req(req)->drv_op_result; |
a5f5774c JH |
718 | |
719 | /* copy to user if data and response */ | |
b093410c | 720 | for (i = 0; i < num_of_cmds && !err; i++) |
a5f5774c | 721 | err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]); |
a5f5774c | 722 | |
3ecd8cf2 LW |
723 | blk_put_request(req); |
724 | ||
a5f5774c JH |
725 | cmd_err: |
726 | for (i = 0; i < num_of_cmds; i++) { | |
727 | kfree(idata[i]->buf); | |
728 | kfree(idata[i]); | |
729 | } | |
730 | kfree(idata); | |
b093410c | 731 | return ioc_err ? ioc_err : err; |
a5f5774c JH |
732 | } |
733 | ||
61fe0e2b LW |
734 | static int mmc_blk_check_blkdev(struct block_device *bdev) |
735 | { | |
736 | /* | |
737 | * The caller must have CAP_SYS_RAWIO, and must be calling this on the | |
738 | * whole block device, not on a partition. This prevents overspray | |
739 | * between sibling partitions. | |
740 | */ | |
741 | if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains)) | |
742 | return -EPERM; | |
743 | return 0; | |
744 | } | |
745 | ||
cb87ea28 JC |
746 | static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode, |
747 | unsigned int cmd, unsigned long arg) | |
748 | { | |
2fe20bae | 749 | struct mmc_blk_data *md; |
61fe0e2b LW |
750 | int ret; |
751 | ||
a5f5774c JH |
752 | switch (cmd) { |
753 | case MMC_IOC_CMD: | |
61fe0e2b LW |
754 | ret = mmc_blk_check_blkdev(bdev); |
755 | if (ret) | |
756 | return ret; | |
2fe20bae LW |
757 | md = mmc_blk_get(bdev->bd_disk); |
758 | if (!md) | |
759 | return -EINVAL; | |
760 | ret = mmc_blk_ioctl_cmd(md, | |
97548575 LW |
761 | (struct mmc_ioc_cmd __user *)arg, |
762 | NULL); | |
2fe20bae LW |
763 | mmc_blk_put(md); |
764 | return ret; | |
a5f5774c | 765 | case MMC_IOC_MULTI_CMD: |
61fe0e2b LW |
766 | ret = mmc_blk_check_blkdev(bdev); |
767 | if (ret) | |
768 | return ret; | |
2fe20bae LW |
769 | md = mmc_blk_get(bdev->bd_disk); |
770 | if (!md) | |
771 | return -EINVAL; | |
772 | ret = mmc_blk_ioctl_multi_cmd(md, | |
97548575 LW |
773 | (struct mmc_ioc_multi_cmd __user *)arg, |
774 | NULL); | |
2fe20bae LW |
775 | mmc_blk_put(md); |
776 | return ret; | |
a5f5774c JH |
777 | default: |
778 | return -EINVAL; | |
779 | } | |
cb87ea28 JC |
780 | } |
781 | ||
782 | #ifdef CONFIG_COMPAT | |
783 | static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode, | |
784 | unsigned int cmd, unsigned long arg) | |
785 | { | |
786 | return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg)); | |
787 | } | |
788 | #endif | |
789 | ||
83d5cde4 | 790 | static const struct block_device_operations mmc_bdops = { |
a5a1561f AV |
791 | .open = mmc_blk_open, |
792 | .release = mmc_blk_release, | |
a885c8c4 | 793 | .getgeo = mmc_blk_getgeo, |
1da177e4 | 794 | .owner = THIS_MODULE, |
cb87ea28 JC |
795 | .ioctl = mmc_blk_ioctl, |
796 | #ifdef CONFIG_COMPAT | |
797 | .compat_ioctl = mmc_blk_compat_ioctl, | |
798 | #endif | |
1da177e4 LT |
799 | }; |
800 | ||
025e3d5f AH |
801 | static int mmc_blk_part_switch_pre(struct mmc_card *card, |
802 | unsigned int part_type) | |
803 | { | |
804 | int ret = 0; | |
805 | ||
806 | if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) { | |
807 | if (card->ext_csd.cmdq_en) { | |
808 | ret = mmc_cmdq_disable(card); | |
809 | if (ret) | |
810 | return ret; | |
811 | } | |
812 | mmc_retune_pause(card->host); | |
813 | } | |
814 | ||
815 | return ret; | |
816 | } | |
817 | ||
818 | static int mmc_blk_part_switch_post(struct mmc_card *card, | |
819 | unsigned int part_type) | |
820 | { | |
821 | int ret = 0; | |
822 | ||
823 | if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) { | |
824 | mmc_retune_unpause(card->host); | |
825 | if (card->reenable_cmdq && !card->ext_csd.cmdq_en) | |
826 | ret = mmc_cmdq_enable(card); | |
827 | } | |
828 | ||
829 | return ret; | |
830 | } | |
831 | ||
371a689f | 832 | static inline int mmc_blk_part_switch(struct mmc_card *card, |
1f797edc | 833 | unsigned int part_type) |
371a689f | 834 | { |
025e3d5f | 835 | int ret = 0; |
fc95e30b | 836 | struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev); |
0d7d85ca | 837 | |
1f797edc | 838 | if (main_md->part_curr == part_type) |
371a689f AW |
839 | return 0; |
840 | ||
841 | if (mmc_card_mmc(card)) { | |
0d7d85ca AH |
842 | u8 part_config = card->ext_csd.part_config; |
843 | ||
1f797edc | 844 | ret = mmc_blk_part_switch_pre(card, part_type); |
025e3d5f AH |
845 | if (ret) |
846 | return ret; | |
57da0c04 | 847 | |
0d7d85ca | 848 | part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK; |
1f797edc | 849 | part_config |= part_type; |
371a689f AW |
850 | |
851 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
0d7d85ca | 852 | EXT_CSD_PART_CONFIG, part_config, |
371a689f | 853 | card->ext_csd.part_time); |
57da0c04 | 854 | if (ret) { |
1f797edc | 855 | mmc_blk_part_switch_post(card, part_type); |
371a689f | 856 | return ret; |
57da0c04 | 857 | } |
0d7d85ca AH |
858 | |
859 | card->ext_csd.part_config = part_config; | |
57da0c04 | 860 | |
025e3d5f | 861 | ret = mmc_blk_part_switch_post(card, main_md->part_curr); |
67716327 | 862 | } |
371a689f | 863 | |
1f797edc | 864 | main_md->part_curr = part_type; |
025e3d5f | 865 | return ret; |
371a689f AW |
866 | } |
867 | ||
169f03a0 | 868 | static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks) |
ec5a19dd PO |
869 | { |
870 | int err; | |
051913da BD |
871 | u32 result; |
872 | __be32 *blocks; | |
ec5a19dd | 873 | |
c7836d15 MY |
874 | struct mmc_request mrq = {}; |
875 | struct mmc_command cmd = {}; | |
876 | struct mmc_data data = {}; | |
ec5a19dd PO |
877 | |
878 | struct scatterlist sg; | |
879 | ||
ec5a19dd PO |
880 | cmd.opcode = MMC_APP_CMD; |
881 | cmd.arg = card->rca << 16; | |
7213d175 | 882 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
ec5a19dd PO |
883 | |
884 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | |
7213d175 | 885 | if (err) |
169f03a0 | 886 | return err; |
7213d175 | 887 | if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD)) |
169f03a0 | 888 | return -EIO; |
ec5a19dd PO |
889 | |
890 | memset(&cmd, 0, sizeof(struct mmc_command)); | |
891 | ||
892 | cmd.opcode = SD_APP_SEND_NUM_WR_BLKS; | |
893 | cmd.arg = 0; | |
7213d175 | 894 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
ec5a19dd | 895 | |
ec5a19dd PO |
896 | data.blksz = 4; |
897 | data.blocks = 1; | |
898 | data.flags = MMC_DATA_READ; | |
899 | data.sg = &sg; | |
900 | data.sg_len = 1; | |
d380443c | 901 | mmc_set_data_timeout(&data, card); |
ec5a19dd | 902 | |
ec5a19dd PO |
903 | mrq.cmd = &cmd; |
904 | mrq.data = &data; | |
905 | ||
051913da BD |
906 | blocks = kmalloc(4, GFP_KERNEL); |
907 | if (!blocks) | |
169f03a0 | 908 | return -ENOMEM; |
051913da BD |
909 | |
910 | sg_init_one(&sg, blocks, 4); | |
ec5a19dd PO |
911 | |
912 | mmc_wait_for_req(card->host, &mrq); | |
913 | ||
051913da BD |
914 | result = ntohl(*blocks); |
915 | kfree(blocks); | |
916 | ||
17b0429d | 917 | if (cmd.error || data.error) |
169f03a0 LW |
918 | return -EIO; |
919 | ||
920 | *written_blocks = result; | |
ec5a19dd | 921 | |
169f03a0 | 922 | return 0; |
ec5a19dd PO |
923 | } |
924 | ||
c49433fb | 925 | static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms, |
c89b4851 AH |
926 | bool hw_busy_detect, struct request *req, |
927 | u32 *resp_errs) | |
c49433fb UH |
928 | { |
929 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); | |
930 | int err = 0; | |
931 | u32 status; | |
932 | ||
933 | do { | |
2185bc2c | 934 | err = __mmc_send_status(card, &status, 5); |
c49433fb UH |
935 | if (err) { |
936 | pr_err("%s: error %d requesting status\n", | |
937 | req->rq_disk->disk_name, err); | |
938 | return err; | |
939 | } | |
940 | ||
c89b4851 AH |
941 | /* Accumulate any response error bits seen */ |
942 | if (resp_errs) | |
943 | *resp_errs |= status; | |
c49433fb | 944 | |
95a91298 UH |
945 | /* We may rely on the host hw to handle busy detection.*/ |
946 | if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && | |
947 | hw_busy_detect) | |
948 | break; | |
949 | ||
c49433fb UH |
950 | /* |
951 | * Timeout if the device never becomes ready for data and never | |
952 | * leaves the program state. | |
953 | */ | |
954 | if (time_after(jiffies, timeout)) { | |
955 | pr_err("%s: Card stuck in programming state! %s %s\n", | |
956 | mmc_hostname(card->host), | |
957 | req->rq_disk->disk_name, __func__); | |
958 | return -ETIMEDOUT; | |
959 | } | |
960 | ||
961 | /* | |
962 | * Some cards mishandle the status bits, | |
963 | * so make sure to check both the busy | |
964 | * indication and the card state. | |
965 | */ | |
966 | } while (!(status & R1_READY_FOR_DATA) || | |
967 | (R1_CURRENT_STATE(status) == R1_STATE_PRG)); | |
968 | ||
969 | return err; | |
970 | } | |
971 | ||
c89b4851 AH |
972 | static int card_busy_detect_err(struct mmc_card *card, unsigned int timeout_ms, |
973 | bool hw_busy_detect, struct request *req, | |
974 | bool *gen_err) | |
975 | { | |
976 | u32 resp_errs = 0; | |
977 | int err; | |
978 | ||
979 | err = card_busy_detect(card, timeout_ms, hw_busy_detect, req, | |
980 | &resp_errs); | |
981 | if (resp_errs & R1_ERROR) { | |
982 | pr_err("%s: %s: error sending status cmd, status %#x\n", | |
983 | req->rq_disk->disk_name, __func__, resp_errs); | |
984 | *gen_err = true; | |
985 | } | |
986 | ||
987 | return err; | |
988 | } | |
989 | ||
bb5cba40 | 990 | static int send_stop(struct mmc_card *card, unsigned int timeout_ms, |
c44d6cef | 991 | struct request *req, bool *gen_err, u32 *stop_status) |
bb5cba40 UH |
992 | { |
993 | struct mmc_host *host = card->host; | |
c7836d15 | 994 | struct mmc_command cmd = {}; |
bb5cba40 UH |
995 | int err; |
996 | bool use_r1b_resp = rq_data_dir(req) == WRITE; | |
997 | ||
998 | /* | |
999 | * Normally we use R1B responses for WRITE, but in cases where the host | |
1000 | * has specified a max_busy_timeout we need to validate it. A failure | |
1001 | * means we need to prevent the host from doing hw busy detection, which | |
1002 | * is done by converting to a R1 response instead. | |
1003 | */ | |
1004 | if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) | |
1005 | use_r1b_resp = false; | |
1006 | ||
1007 | cmd.opcode = MMC_STOP_TRANSMISSION; | |
1008 | if (use_r1b_resp) { | |
1009 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; | |
1010 | cmd.busy_timeout = timeout_ms; | |
1011 | } else { | |
1012 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; | |
1013 | } | |
1014 | ||
1015 | err = mmc_wait_for_cmd(host, &cmd, 5); | |
1016 | if (err) | |
1017 | return err; | |
1018 | ||
1019 | *stop_status = cmd.resp[0]; | |
1020 | ||
1021 | /* No need to check card status in case of READ. */ | |
1022 | if (rq_data_dir(req) == READ) | |
1023 | return 0; | |
1024 | ||
1025 | if (!mmc_host_is_spi(host) && | |
1026 | (*stop_status & R1_ERROR)) { | |
1027 | pr_err("%s: %s: general error sending stop command, resp %#x\n", | |
1028 | req->rq_disk->disk_name, __func__, *stop_status); | |
c44d6cef | 1029 | *gen_err = true; |
bb5cba40 UH |
1030 | } |
1031 | ||
c89b4851 AH |
1032 | return card_busy_detect_err(card, timeout_ms, use_r1b_resp, req, |
1033 | gen_err); | |
bb5cba40 UH |
1034 | } |
1035 | ||
a8ad82cc | 1036 | #define ERR_NOMEDIUM 3 |
a01f3ccf RKAL |
1037 | #define ERR_RETRY 2 |
1038 | #define ERR_ABORT 1 | |
1039 | #define ERR_CONTINUE 0 | |
1040 | ||
1041 | static int mmc_blk_cmd_error(struct request *req, const char *name, int error, | |
1042 | bool status_valid, u32 status) | |
1043 | { | |
1044 | switch (error) { | |
1045 | case -EILSEQ: | |
1046 | /* response crc error, retry the r/w cmd */ | |
1047 | pr_err("%s: %s sending %s command, card status %#x\n", | |
1048 | req->rq_disk->disk_name, "response CRC error", | |
1049 | name, status); | |
1050 | return ERR_RETRY; | |
1051 | ||
1052 | case -ETIMEDOUT: | |
1053 | pr_err("%s: %s sending %s command, card status %#x\n", | |
1054 | req->rq_disk->disk_name, "timed out", name, status); | |
1055 | ||
1056 | /* If the status cmd initially failed, retry the r/w cmd */ | |
cc4d04be KS |
1057 | if (!status_valid) { |
1058 | pr_err("%s: status not valid, retrying timeout\n", | |
1059 | req->rq_disk->disk_name); | |
a01f3ccf | 1060 | return ERR_RETRY; |
cc4d04be | 1061 | } |
a01f3ccf RKAL |
1062 | |
1063 | /* | |
1064 | * If it was a r/w cmd crc error, or illegal command | |
1065 | * (eg, issued in wrong state) then retry - we should | |
1066 | * have corrected the state problem above. | |
1067 | */ | |
cc4d04be KS |
1068 | if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) { |
1069 | pr_err("%s: command error, retrying timeout\n", | |
1070 | req->rq_disk->disk_name); | |
a01f3ccf | 1071 | return ERR_RETRY; |
cc4d04be | 1072 | } |
a01f3ccf RKAL |
1073 | |
1074 | /* Otherwise abort the command */ | |
1075 | return ERR_ABORT; | |
1076 | ||
1077 | default: | |
1078 | /* We don't understand the error code the driver gave us */ | |
1079 | pr_err("%s: unknown error %d sending read/write command, card status %#x\n", | |
1080 | req->rq_disk->disk_name, error, status); | |
1081 | return ERR_ABORT; | |
1082 | } | |
1083 | } | |
1084 | ||
1085 | /* | |
1086 | * Initial r/w and stop cmd error recovery. | |
1087 | * We don't know whether the card received the r/w cmd or not, so try to | |
1088 | * restore things back to a sane state. Essentially, we do this as follows: | |
1089 | * - Obtain card status. If the first attempt to obtain card status fails, | |
1090 | * the status word will reflect the failed status cmd, not the failed | |
1091 | * r/w cmd. If we fail to obtain card status, it suggests we can no | |
1092 | * longer communicate with the card. | |
1093 | * - Check the card state. If the card received the cmd but there was a | |
1094 | * transient problem with the response, it might still be in a data transfer | |
1095 | * mode. Try to send it a stop command. If this fails, we can't recover. | |
1096 | * - If the r/w cmd failed due to a response CRC error, it was probably | |
1097 | * transient, so retry the cmd. | |
1098 | * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry. | |
1099 | * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or | |
1100 | * illegal cmd, retry. | |
1101 | * Otherwise we don't understand what happened, so abort. | |
1102 | */ | |
1103 | static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req, | |
2cc64587 | 1104 | struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err) |
a01f3ccf RKAL |
1105 | { |
1106 | bool prev_cmd_status_valid = true; | |
1107 | u32 status, stop_status = 0; | |
1108 | int err, retry; | |
1109 | ||
a8ad82cc SRT |
1110 | if (mmc_card_removed(card)) |
1111 | return ERR_NOMEDIUM; | |
1112 | ||
a01f3ccf RKAL |
1113 | /* |
1114 | * Try to get card status which indicates both the card state | |
1115 | * and why there was no response. If the first attempt fails, | |
1116 | * we can't be sure the returned status is for the r/w command. | |
1117 | */ | |
1118 | for (retry = 2; retry >= 0; retry--) { | |
2185bc2c | 1119 | err = __mmc_send_status(card, &status, 0); |
a01f3ccf RKAL |
1120 | if (!err) |
1121 | break; | |
1122 | ||
6f398ad2 AH |
1123 | /* Re-tune if needed */ |
1124 | mmc_retune_recheck(card->host); | |
1125 | ||
a01f3ccf RKAL |
1126 | prev_cmd_status_valid = false; |
1127 | pr_err("%s: error %d sending status command, %sing\n", | |
1128 | req->rq_disk->disk_name, err, retry ? "retry" : "abort"); | |
1129 | } | |
1130 | ||
1131 | /* We couldn't get a response from the card. Give up. */ | |
a8ad82cc SRT |
1132 | if (err) { |
1133 | /* Check if the card is removed */ | |
1134 | if (mmc_detect_card_removed(card->host)) | |
1135 | return ERR_NOMEDIUM; | |
a01f3ccf | 1136 | return ERR_ABORT; |
a8ad82cc | 1137 | } |
a01f3ccf | 1138 | |
67716327 AH |
1139 | /* Flag ECC errors */ |
1140 | if ((status & R1_CARD_ECC_FAILED) || | |
1141 | (brq->stop.resp[0] & R1_CARD_ECC_FAILED) || | |
1142 | (brq->cmd.resp[0] & R1_CARD_ECC_FAILED)) | |
2cc64587 | 1143 | *ecc_err = true; |
67716327 | 1144 | |
c8760069 KY |
1145 | /* Flag General errors */ |
1146 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) | |
1147 | if ((status & R1_ERROR) || | |
1148 | (brq->stop.resp[0] & R1_ERROR)) { | |
1149 | pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n", | |
1150 | req->rq_disk->disk_name, __func__, | |
1151 | brq->stop.resp[0], status); | |
c44d6cef | 1152 | *gen_err = true; |
c8760069 KY |
1153 | } |
1154 | ||
a01f3ccf RKAL |
1155 | /* |
1156 | * Check the current card state. If it is in some data transfer | |
1157 | * mode, tell it to stop (and hopefully transition back to TRAN.) | |
1158 | */ | |
1159 | if (R1_CURRENT_STATE(status) == R1_STATE_DATA || | |
1160 | R1_CURRENT_STATE(status) == R1_STATE_RCV) { | |
bb5cba40 UH |
1161 | err = send_stop(card, |
1162 | DIV_ROUND_UP(brq->data.timeout_ns, 1000000), | |
1163 | req, gen_err, &stop_status); | |
1164 | if (err) { | |
a01f3ccf RKAL |
1165 | pr_err("%s: error %d sending stop command\n", |
1166 | req->rq_disk->disk_name, err); | |
bb5cba40 UH |
1167 | /* |
1168 | * If the stop cmd also timed out, the card is probably | |
1169 | * not present, so abort. Other errors are bad news too. | |
1170 | */ | |
a01f3ccf | 1171 | return ERR_ABORT; |
bb5cba40 UH |
1172 | } |
1173 | ||
67716327 | 1174 | if (stop_status & R1_CARD_ECC_FAILED) |
2cc64587 | 1175 | *ecc_err = true; |
a01f3ccf RKAL |
1176 | } |
1177 | ||
1178 | /* Check for set block count errors */ | |
1179 | if (brq->sbc.error) | |
1180 | return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error, | |
1181 | prev_cmd_status_valid, status); | |
1182 | ||
1183 | /* Check for r/w command errors */ | |
1184 | if (brq->cmd.error) | |
1185 | return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error, | |
1186 | prev_cmd_status_valid, status); | |
1187 | ||
67716327 AH |
1188 | /* Data errors */ |
1189 | if (!brq->stop.error) | |
1190 | return ERR_CONTINUE; | |
1191 | ||
a01f3ccf | 1192 | /* Now for stop errors. These aren't fatal to the transfer. */ |
5e1344eb | 1193 | pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n", |
a01f3ccf RKAL |
1194 | req->rq_disk->disk_name, brq->stop.error, |
1195 | brq->cmd.resp[0], status); | |
1196 | ||
1197 | /* | |
1198 | * Subsitute in our own stop status as this will give the error | |
1199 | * state which happened during the execution of the r/w command. | |
1200 | */ | |
1201 | if (stop_status) { | |
1202 | brq->stop.resp[0] = stop_status; | |
1203 | brq->stop.error = 0; | |
1204 | } | |
1205 | return ERR_CONTINUE; | |
1206 | } | |
1207 | ||
67716327 AH |
1208 | static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host, |
1209 | int type) | |
1210 | { | |
1211 | int err; | |
1212 | ||
1213 | if (md->reset_done & type) | |
1214 | return -EEXIST; | |
1215 | ||
1216 | md->reset_done |= type; | |
1217 | err = mmc_hw_reset(host); | |
1218 | /* Ensure we switch back to the correct partition */ | |
1219 | if (err != -EOPNOTSUPP) { | |
fc95e30b UH |
1220 | struct mmc_blk_data *main_md = |
1221 | dev_get_drvdata(&host->card->dev); | |
67716327 AH |
1222 | int part_err; |
1223 | ||
1224 | main_md->part_curr = main_md->part_type; | |
1f797edc | 1225 | part_err = mmc_blk_part_switch(host->card, md->part_type); |
67716327 AH |
1226 | if (part_err) { |
1227 | /* | |
1228 | * We have failed to get back into the correct | |
1229 | * partition, so we need to abort the whole request. | |
1230 | */ | |
1231 | return -ENODEV; | |
1232 | } | |
1233 | } | |
1234 | return err; | |
1235 | } | |
1236 | ||
1237 | static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type) | |
1238 | { | |
1239 | md->reset_done &= ~type; | |
1240 | } | |
1241 | ||
81196976 AH |
1242 | static void mmc_blk_end_request(struct request *req, blk_status_t error) |
1243 | { | |
1244 | if (req->mq_ctx) | |
1245 | blk_mq_end_request(req, error); | |
1246 | else | |
1247 | blk_end_request_all(req, error); | |
1248 | } | |
1249 | ||
5ec12396 LW |
1250 | /* |
1251 | * The non-block commands come back from the block layer after it queued it and | |
1252 | * processed it with all other requests and then they get issued in this | |
1253 | * function. | |
1254 | */ | |
1255 | static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req) | |
1256 | { | |
1257 | struct mmc_queue_req *mq_rq; | |
1258 | struct mmc_card *card = mq->card; | |
1259 | struct mmc_blk_data *md = mq->blkdata; | |
69f7599e | 1260 | struct mmc_blk_ioc_data **idata; |
97548575 | 1261 | bool rpmb_ioctl; |
627c3ccf LW |
1262 | u8 **ext_csd; |
1263 | u32 status; | |
0493f6fe | 1264 | int ret; |
5ec12396 LW |
1265 | int i; |
1266 | ||
1267 | mq_rq = req_to_mmc_queue_req(req); | |
97548575 | 1268 | rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB); |
5ec12396 LW |
1269 | |
1270 | switch (mq_rq->drv_op) { | |
1271 | case MMC_DRV_OP_IOCTL: | |
97548575 | 1272 | case MMC_DRV_OP_IOCTL_RPMB: |
69f7599e | 1273 | idata = mq_rq->drv_op_data; |
7432b49b | 1274 | for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) { |
69f7599e | 1275 | ret = __mmc_blk_ioctl_cmd(card, md, idata[i]); |
0493f6fe | 1276 | if (ret) |
5ec12396 LW |
1277 | break; |
1278 | } | |
5ec12396 | 1279 | /* Always switch back to main area after RPMB access */ |
97548575 LW |
1280 | if (rpmb_ioctl) |
1281 | mmc_blk_part_switch(card, 0); | |
0493f6fe LW |
1282 | break; |
1283 | case MMC_DRV_OP_BOOT_WP: | |
1284 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP, | |
1285 | card->ext_csd.boot_ro_lock | | |
1286 | EXT_CSD_BOOT_WP_B_PWR_WP_EN, | |
1287 | card->ext_csd.part_time); | |
1288 | if (ret) | |
1289 | pr_err("%s: Locking boot partition ro until next power on failed: %d\n", | |
1290 | md->disk->disk_name, ret); | |
1291 | else | |
1292 | card->ext_csd.boot_ro_lock |= | |
1293 | EXT_CSD_BOOT_WP_B_PWR_WP_EN; | |
5ec12396 | 1294 | break; |
627c3ccf LW |
1295 | case MMC_DRV_OP_GET_CARD_STATUS: |
1296 | ret = mmc_send_status(card, &status); | |
1297 | if (!ret) | |
1298 | ret = status; | |
1299 | break; | |
1300 | case MMC_DRV_OP_GET_EXT_CSD: | |
1301 | ext_csd = mq_rq->drv_op_data; | |
1302 | ret = mmc_get_ext_csd(card, ext_csd); | |
1303 | break; | |
5ec12396 | 1304 | default: |
0493f6fe LW |
1305 | pr_err("%s: unknown driver specific operation\n", |
1306 | md->disk->disk_name); | |
1307 | ret = -EINVAL; | |
5ec12396 LW |
1308 | break; |
1309 | } | |
0493f6fe | 1310 | mq_rq->drv_op_result = ret; |
81196976 | 1311 | mmc_blk_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK); |
5ec12396 LW |
1312 | } |
1313 | ||
df061588 | 1314 | static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req) |
bd788c96 | 1315 | { |
7db3028e | 1316 | struct mmc_blk_data *md = mq->blkdata; |
bd788c96 AH |
1317 | struct mmc_card *card = md->queue.card; |
1318 | unsigned int from, nr, arg; | |
67716327 | 1319 | int err = 0, type = MMC_BLK_DISCARD; |
2a842aca | 1320 | blk_status_t status = BLK_STS_OK; |
bd788c96 | 1321 | |
bd788c96 | 1322 | if (!mmc_can_erase(card)) { |
2a842aca | 1323 | status = BLK_STS_NOTSUPP; |
8cb6ed17 | 1324 | goto fail; |
bd788c96 AH |
1325 | } |
1326 | ||
1327 | from = blk_rq_pos(req); | |
1328 | nr = blk_rq_sectors(req); | |
1329 | ||
b3bf9153 KP |
1330 | if (mmc_can_discard(card)) |
1331 | arg = MMC_DISCARD_ARG; | |
1332 | else if (mmc_can_trim(card)) | |
bd788c96 AH |
1333 | arg = MMC_TRIM_ARG; |
1334 | else | |
1335 | arg = MMC_ERASE_ARG; | |
164b50b3 GU |
1336 | do { |
1337 | err = 0; | |
1338 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { | |
1339 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
1340 | INAND_CMD38_ARG_EXT_CSD, | |
1341 | arg == MMC_TRIM_ARG ? | |
1342 | INAND_CMD38_ARG_TRIM : | |
1343 | INAND_CMD38_ARG_ERASE, | |
1344 | 0); | |
1345 | } | |
1346 | if (!err) | |
1347 | err = mmc_erase(card, from, nr, arg); | |
1348 | } while (err == -EIO && !mmc_blk_reset(md, card->host, type)); | |
2a842aca CH |
1349 | if (err) |
1350 | status = BLK_STS_IOERR; | |
1351 | else | |
67716327 | 1352 | mmc_blk_reset_success(md, type); |
8cb6ed17 | 1353 | fail: |
81196976 | 1354 | mmc_blk_end_request(req, status); |
bd788c96 AH |
1355 | } |
1356 | ||
df061588 | 1357 | static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, |
49804548 AH |
1358 | struct request *req) |
1359 | { | |
7db3028e | 1360 | struct mmc_blk_data *md = mq->blkdata; |
49804548 | 1361 | struct mmc_card *card = md->queue.card; |
775a9362 | 1362 | unsigned int from, nr, arg; |
67716327 | 1363 | int err = 0, type = MMC_BLK_SECDISCARD; |
2a842aca | 1364 | blk_status_t status = BLK_STS_OK; |
49804548 | 1365 | |
775a9362 | 1366 | if (!(mmc_can_secure_erase_trim(card))) { |
2a842aca | 1367 | status = BLK_STS_NOTSUPP; |
49804548 AH |
1368 | goto out; |
1369 | } | |
1370 | ||
28302812 AH |
1371 | from = blk_rq_pos(req); |
1372 | nr = blk_rq_sectors(req); | |
1373 | ||
775a9362 ME |
1374 | if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr)) |
1375 | arg = MMC_SECURE_TRIM1_ARG; | |
1376 | else | |
1377 | arg = MMC_SECURE_ERASE_ARG; | |
d9ddd629 | 1378 | |
67716327 | 1379 | retry: |
6a7a6b45 AW |
1380 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
1381 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
1382 | INAND_CMD38_ARG_EXT_CSD, | |
1383 | arg == MMC_SECURE_TRIM1_ARG ? | |
1384 | INAND_CMD38_ARG_SECTRIM1 : | |
1385 | INAND_CMD38_ARG_SECERASE, | |
1386 | 0); | |
1387 | if (err) | |
28302812 | 1388 | goto out_retry; |
6a7a6b45 | 1389 | } |
28302812 | 1390 | |
49804548 | 1391 | err = mmc_erase(card, from, nr, arg); |
28302812 AH |
1392 | if (err == -EIO) |
1393 | goto out_retry; | |
2a842aca CH |
1394 | if (err) { |
1395 | status = BLK_STS_IOERR; | |
28302812 | 1396 | goto out; |
2a842aca | 1397 | } |
28302812 AH |
1398 | |
1399 | if (arg == MMC_SECURE_TRIM1_ARG) { | |
6a7a6b45 AW |
1400 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
1401 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
1402 | INAND_CMD38_ARG_EXT_CSD, | |
1403 | INAND_CMD38_ARG_SECTRIM2, | |
1404 | 0); | |
1405 | if (err) | |
28302812 | 1406 | goto out_retry; |
6a7a6b45 | 1407 | } |
28302812 | 1408 | |
49804548 | 1409 | err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG); |
28302812 AH |
1410 | if (err == -EIO) |
1411 | goto out_retry; | |
2a842aca CH |
1412 | if (err) { |
1413 | status = BLK_STS_IOERR; | |
28302812 | 1414 | goto out; |
2a842aca | 1415 | } |
6a7a6b45 | 1416 | } |
28302812 | 1417 | |
28302812 AH |
1418 | out_retry: |
1419 | if (err && !mmc_blk_reset(md, card->host, type)) | |
67716327 AH |
1420 | goto retry; |
1421 | if (!err) | |
1422 | mmc_blk_reset_success(md, type); | |
28302812 | 1423 | out: |
81196976 | 1424 | mmc_blk_end_request(req, status); |
49804548 AH |
1425 | } |
1426 | ||
df061588 | 1427 | static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req) |
f4c5522b | 1428 | { |
7db3028e | 1429 | struct mmc_blk_data *md = mq->blkdata; |
881d1c25 SJ |
1430 | struct mmc_card *card = md->queue.card; |
1431 | int ret = 0; | |
1432 | ||
1433 | ret = mmc_flush_cache(card); | |
81196976 | 1434 | mmc_blk_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK); |
f4c5522b AW |
1435 | } |
1436 | ||
1437 | /* | |
1438 | * Reformat current write as a reliable write, supporting | |
1439 | * both legacy and the enhanced reliable write MMC cards. | |
1440 | * In each transfer we'll handle only as much as a single | |
1441 | * reliable write can handle, thus finish the request in | |
1442 | * partial completions. | |
1443 | */ | |
d0c97cfb AW |
1444 | static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, |
1445 | struct mmc_card *card, | |
1446 | struct request *req) | |
f4c5522b | 1447 | { |
f4c5522b AW |
1448 | if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) { |
1449 | /* Legacy mode imposes restrictions on transfers. */ | |
9cb38f7a | 1450 | if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors)) |
f4c5522b AW |
1451 | brq->data.blocks = 1; |
1452 | ||
1453 | if (brq->data.blocks > card->ext_csd.rel_sectors) | |
1454 | brq->data.blocks = card->ext_csd.rel_sectors; | |
1455 | else if (brq->data.blocks < card->ext_csd.rel_sectors) | |
1456 | brq->data.blocks = 1; | |
1457 | } | |
f4c5522b AW |
1458 | } |
1459 | ||
f47a1fe3 AH |
1460 | #define CMD_ERRORS_EXCL_OOR \ |
1461 | (R1_ADDRESS_ERROR | /* Misaligned address */ \ | |
4c2b8f26 RKAL |
1462 | R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\ |
1463 | R1_WP_VIOLATION | /* Tried to write to protected block */ \ | |
a04e6bae | 1464 | R1_CARD_ECC_FAILED | /* Card ECC failed */ \ |
4c2b8f26 RKAL |
1465 | R1_CC_ERROR | /* Card controller error */ \ |
1466 | R1_ERROR) /* General/unknown error */ | |
1467 | ||
f47a1fe3 AH |
1468 | #define CMD_ERRORS \ |
1469 | (CMD_ERRORS_EXCL_OOR | \ | |
1470 | R1_OUT_OF_RANGE) /* Command argument out of range */ \ | |
1471 | ||
d83c2dba | 1472 | static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq) |
a04e6bae | 1473 | { |
d83c2dba | 1474 | u32 val; |
a04e6bae | 1475 | |
d83c2dba SL |
1476 | /* |
1477 | * Per the SD specification(physical layer version 4.10)[1], | |
1478 | * section 4.3.3, it explicitly states that "When the last | |
1479 | * block of user area is read using CMD18, the host should | |
1480 | * ignore OUT_OF_RANGE error that may occur even the sequence | |
1481 | * is correct". And JESD84-B51 for eMMC also has a similar | |
1482 | * statement on section 6.8.3. | |
1483 | * | |
1484 | * Multiple block read/write could be done by either predefined | |
1485 | * method, namely CMD23, or open-ending mode. For open-ending mode, | |
1486 | * we should ignore the OUT_OF_RANGE error as it's normal behaviour. | |
1487 | * | |
1488 | * However the spec[1] doesn't tell us whether we should also | |
1489 | * ignore that for predefined method. But per the spec[1], section | |
1490 | * 4.15 Set Block Count Command, it says"If illegal block count | |
1491 | * is set, out of range error will be indicated during read/write | |
1492 | * operation (For example, data transfer is stopped at user area | |
1493 | * boundary)." In another word, we could expect a out of range error | |
1494 | * in the response for the following CMD18/25. And if argument of | |
1495 | * CMD23 + the argument of CMD18/25 exceed the max number of blocks, | |
1496 | * we could also expect to get a -ETIMEDOUT or any error number from | |
1497 | * the host drivers due to missing data response(for write)/data(for | |
1498 | * read), as the cards will stop the data transfer by itself per the | |
1499 | * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode. | |
1500 | */ | |
1501 | ||
1502 | if (!brq->stop.error) { | |
1503 | bool oor_with_open_end; | |
1504 | /* If there is no error yet, check R1 response */ | |
1505 | ||
1506 | val = brq->stop.resp[0] & CMD_ERRORS; | |
1507 | oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc; | |
1508 | ||
1509 | if (val && !oor_with_open_end) | |
1510 | brq->stop.error = -EIO; | |
1511 | } | |
a04e6bae WS |
1512 | } |
1513 | ||
81196976 AH |
1514 | static enum mmc_blk_status __mmc_blk_err_check(struct mmc_card *card, |
1515 | struct mmc_queue_req *mq_mrq) | |
d78d4a8a | 1516 | { |
ee8a43a5 | 1517 | struct mmc_blk_request *brq = &mq_mrq->brq; |
67e69d52 | 1518 | struct request *req = mmc_queue_req_to_req(mq_mrq); |
b8360a49 | 1519 | int need_retune = card->host->need_retune; |
2cc64587 | 1520 | bool ecc_err = false; |
c44d6cef | 1521 | bool gen_err = false; |
d78d4a8a PF |
1522 | |
1523 | /* | |
1524 | * sbc.error indicates a problem with the set block count | |
1525 | * command. No data will have been transferred. | |
1526 | * | |
1527 | * cmd.error indicates a problem with the r/w command. No | |
1528 | * data will have been transferred. | |
1529 | * | |
1530 | * stop.error indicates a problem with the stop command. Data | |
1531 | * may have been transferred, or may still be transferring. | |
1532 | */ | |
d83c2dba SL |
1533 | |
1534 | mmc_blk_eval_resp_error(brq); | |
1535 | ||
1536 | if (brq->sbc.error || brq->cmd.error || | |
1537 | brq->stop.error || brq->data.error) { | |
c8760069 | 1538 | switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) { |
d78d4a8a PF |
1539 | case ERR_RETRY: |
1540 | return MMC_BLK_RETRY; | |
1541 | case ERR_ABORT: | |
1542 | return MMC_BLK_ABORT; | |
a8ad82cc SRT |
1543 | case ERR_NOMEDIUM: |
1544 | return MMC_BLK_NOMEDIUM; | |
d78d4a8a PF |
1545 | case ERR_CONTINUE: |
1546 | break; | |
1547 | } | |
1548 | } | |
1549 | ||
1550 | /* | |
1551 | * Check for errors relating to the execution of the | |
1552 | * initial command - such as address errors. No data | |
1553 | * has been transferred. | |
1554 | */ | |
1555 | if (brq->cmd.resp[0] & CMD_ERRORS) { | |
1556 | pr_err("%s: r/w command failed, status = %#x\n", | |
1557 | req->rq_disk->disk_name, brq->cmd.resp[0]); | |
1558 | return MMC_BLK_ABORT; | |
1559 | } | |
1560 | ||
1561 | /* | |
1562 | * Everything else is either success, or a data error of some | |
1563 | * kind. If it was a write, we may have transitioned to | |
1564 | * program mode, which we have to wait for it to complete. | |
1565 | */ | |
1566 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) { | |
c49433fb | 1567 | int err; |
8fee476b | 1568 | |
c8760069 KY |
1569 | /* Check stop command response */ |
1570 | if (brq->stop.resp[0] & R1_ERROR) { | |
1571 | pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n", | |
1572 | req->rq_disk->disk_name, __func__, | |
1573 | brq->stop.resp[0]); | |
c44d6cef | 1574 | gen_err = true; |
c8760069 KY |
1575 | } |
1576 | ||
c89b4851 AH |
1577 | err = card_busy_detect_err(card, MMC_BLK_TIMEOUT_MS, false, req, |
1578 | &gen_err); | |
c49433fb UH |
1579 | if (err) |
1580 | return MMC_BLK_CMD_ERR; | |
d78d4a8a PF |
1581 | } |
1582 | ||
c8760069 KY |
1583 | /* if general error occurs, retry the write operation. */ |
1584 | if (gen_err) { | |
1585 | pr_warn("%s: retrying write for general error\n", | |
1586 | req->rq_disk->disk_name); | |
1587 | return MMC_BLK_RETRY; | |
1588 | } | |
1589 | ||
9820a5b1 WS |
1590 | /* Some errors (ECC) are flagged on the next commmand, so check stop, too */ |
1591 | if (brq->data.error || brq->stop.error) { | |
b8360a49 | 1592 | if (need_retune && !brq->retune_retry_done) { |
09faf61d RK |
1593 | pr_debug("%s: retrying because a re-tune was needed\n", |
1594 | req->rq_disk->disk_name); | |
b8360a49 AH |
1595 | brq->retune_retry_done = 1; |
1596 | return MMC_BLK_RETRY; | |
1597 | } | |
d78d4a8a | 1598 | pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n", |
9820a5b1 | 1599 | req->rq_disk->disk_name, brq->data.error ?: brq->stop.error, |
d78d4a8a PF |
1600 | (unsigned)blk_rq_pos(req), |
1601 | (unsigned)blk_rq_sectors(req), | |
1602 | brq->cmd.resp[0], brq->stop.resp[0]); | |
1603 | ||
1604 | if (rq_data_dir(req) == READ) { | |
67716327 AH |
1605 | if (ecc_err) |
1606 | return MMC_BLK_ECC_ERR; | |
d78d4a8a PF |
1607 | return MMC_BLK_DATA_ERR; |
1608 | } else { | |
1609 | return MMC_BLK_CMD_ERR; | |
1610 | } | |
1611 | } | |
1612 | ||
67716327 AH |
1613 | if (!brq->data.bytes_xfered) |
1614 | return MMC_BLK_RETRY; | |
d78d4a8a | 1615 | |
67716327 AH |
1616 | if (blk_rq_bytes(req) != brq->data.bytes_xfered) |
1617 | return MMC_BLK_PARTIAL; | |
1618 | ||
1619 | return MMC_BLK_SUCCESS; | |
d78d4a8a PF |
1620 | } |
1621 | ||
81196976 AH |
1622 | static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card, |
1623 | struct mmc_async_req *areq) | |
1624 | { | |
1625 | struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req, | |
1626 | areq); | |
1627 | ||
1628 | return __mmc_blk_err_check(card, mq_mrq); | |
1629 | } | |
1630 | ||
ca5717f7 | 1631 | static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq, |
d3377c01 AH |
1632 | int disable_multi, bool *do_rel_wr_p, |
1633 | bool *do_data_tag_p) | |
1da177e4 | 1634 | { |
ca5717f7 AH |
1635 | struct mmc_blk_data *md = mq->blkdata; |
1636 | struct mmc_card *card = md->queue.card; | |
54d49d77 | 1637 | struct mmc_blk_request *brq = &mqrq->brq; |
67e69d52 | 1638 | struct request *req = mmc_queue_req_to_req(mqrq); |
d3377c01 | 1639 | bool do_rel_wr, do_data_tag; |
1da177e4 | 1640 | |
f4c5522b AW |
1641 | /* |
1642 | * Reliable writes are used to implement Forced Unit Access and | |
d3df0465 | 1643 | * are supported only on MMCs. |
f4c5522b | 1644 | */ |
d3377c01 AH |
1645 | do_rel_wr = (req->cmd_flags & REQ_FUA) && |
1646 | rq_data_dir(req) == WRITE && | |
1647 | (md->flags & MMC_BLK_REL_WR); | |
f4c5522b | 1648 | |
54d49d77 | 1649 | memset(brq, 0, sizeof(struct mmc_blk_request)); |
ca5717f7 | 1650 | |
54d49d77 | 1651 | brq->mrq.data = &brq->data; |
93482b3d | 1652 | brq->mrq.tag = req->tag; |
1da177e4 | 1653 | |
54d49d77 PF |
1654 | brq->stop.opcode = MMC_STOP_TRANSMISSION; |
1655 | brq->stop.arg = 0; | |
ca5717f7 AH |
1656 | |
1657 | if (rq_data_dir(req) == READ) { | |
1658 | brq->data.flags = MMC_DATA_READ; | |
1659 | brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; | |
1660 | } else { | |
1661 | brq->data.flags = MMC_DATA_WRITE; | |
1662 | brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; | |
1663 | } | |
1664 | ||
1665 | brq->data.blksz = 512; | |
54d49d77 | 1666 | brq->data.blocks = blk_rq_sectors(req); |
93482b3d AH |
1667 | brq->data.blk_addr = blk_rq_pos(req); |
1668 | ||
1669 | /* | |
1670 | * The command queue supports 2 priorities: "high" (1) and "simple" (0). | |
1671 | * The eMMC will give "high" priority tasks priority over "simple" | |
1672 | * priority tasks. Here we always set "simple" priority by not setting | |
1673 | * MMC_DATA_PRIO. | |
1674 | */ | |
6a79e391 | 1675 | |
54d49d77 PF |
1676 | /* |
1677 | * The block layer doesn't support all sector count | |
1678 | * restrictions, so we need to be prepared for too big | |
1679 | * requests. | |
1680 | */ | |
1681 | if (brq->data.blocks > card->host->max_blk_count) | |
1682 | brq->data.blocks = card->host->max_blk_count; | |
1da177e4 | 1683 | |
2bf22b39 PW |
1684 | if (brq->data.blocks > 1) { |
1685 | /* | |
1686 | * After a read error, we redo the request one sector | |
1687 | * at a time in order to accurately determine which | |
1688 | * sectors can be read successfully. | |
1689 | */ | |
1690 | if (disable_multi) | |
1691 | brq->data.blocks = 1; | |
1692 | ||
2e47e842 KM |
1693 | /* |
1694 | * Some controllers have HW issues while operating | |
1695 | * in multiple I/O mode | |
1696 | */ | |
1697 | if (card->host->ops->multi_io_quirk) | |
1698 | brq->data.blocks = card->host->ops->multi_io_quirk(card, | |
1699 | (rq_data_dir(req) == READ) ? | |
1700 | MMC_DATA_READ : MMC_DATA_WRITE, | |
1701 | brq->data.blocks); | |
2bf22b39 | 1702 | } |
d0c97cfb | 1703 | |
93482b3d | 1704 | if (do_rel_wr) { |
ca5717f7 | 1705 | mmc_apply_rel_rw(brq, card, req); |
93482b3d AH |
1706 | brq->data.flags |= MMC_DATA_REL_WR; |
1707 | } | |
ca5717f7 AH |
1708 | |
1709 | /* | |
1710 | * Data tag is used only during writing meta data to speed | |
1711 | * up write and any subsequent read of this meta data | |
1712 | */ | |
d3377c01 AH |
1713 | do_data_tag = card->ext_csd.data_tag_unit_size && |
1714 | (req->cmd_flags & REQ_META) && | |
1715 | (rq_data_dir(req) == WRITE) && | |
1716 | ((brq->data.blocks * brq->data.blksz) >= | |
1717 | card->ext_csd.data_tag_unit_size); | |
ca5717f7 | 1718 | |
93482b3d AH |
1719 | if (do_data_tag) |
1720 | brq->data.flags |= MMC_DATA_DAT_TAG; | |
1721 | ||
ca5717f7 AH |
1722 | mmc_set_data_timeout(&brq->data, card); |
1723 | ||
1724 | brq->data.sg = mqrq->sg; | |
1725 | brq->data.sg_len = mmc_queue_map_sg(mq, mqrq); | |
1726 | ||
1727 | /* | |
1728 | * Adjust the sg list so it is the same size as the | |
1729 | * request. | |
1730 | */ | |
1731 | if (brq->data.blocks != blk_rq_sectors(req)) { | |
1732 | int i, data_size = brq->data.blocks << 9; | |
1733 | struct scatterlist *sg; | |
1734 | ||
1735 | for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) { | |
1736 | data_size -= sg->length; | |
1737 | if (data_size <= 0) { | |
1738 | sg->length += data_size; | |
1739 | i++; | |
1740 | break; | |
1741 | } | |
1742 | } | |
1743 | brq->data.sg_len = i; | |
1744 | } | |
1745 | ||
1746 | mqrq->areq.mrq = &brq->mrq; | |
d3377c01 AH |
1747 | |
1748 | if (do_rel_wr_p) | |
1749 | *do_rel_wr_p = do_rel_wr; | |
1750 | ||
1751 | if (do_data_tag_p) | |
1752 | *do_data_tag_p = do_data_tag; | |
ca5717f7 AH |
1753 | } |
1754 | ||
1e8e55b6 AH |
1755 | #define MMC_CQE_RETRIES 2 |
1756 | ||
1757 | static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req) | |
1758 | { | |
1759 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
1760 | struct mmc_request *mrq = &mqrq->brq.mrq; | |
1761 | struct request_queue *q = req->q; | |
1762 | struct mmc_host *host = mq->card->host; | |
1763 | unsigned long flags; | |
1764 | bool put_card; | |
1765 | int err; | |
1766 | ||
1767 | mmc_cqe_post_req(host, mrq); | |
1768 | ||
1769 | if (mrq->cmd && mrq->cmd->error) | |
1770 | err = mrq->cmd->error; | |
1771 | else if (mrq->data && mrq->data->error) | |
1772 | err = mrq->data->error; | |
1773 | else | |
1774 | err = 0; | |
1775 | ||
1776 | if (err) { | |
1777 | if (mqrq->retries++ < MMC_CQE_RETRIES) | |
1778 | blk_mq_requeue_request(req, true); | |
1779 | else | |
1780 | blk_mq_end_request(req, BLK_STS_IOERR); | |
1781 | } else if (mrq->data) { | |
1782 | if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered)) | |
1783 | blk_mq_requeue_request(req, true); | |
1784 | else | |
1785 | __blk_mq_end_request(req, BLK_STS_OK); | |
1786 | } else { | |
1787 | blk_mq_end_request(req, BLK_STS_OK); | |
1788 | } | |
1789 | ||
1790 | spin_lock_irqsave(q->queue_lock, flags); | |
1791 | ||
1792 | mq->in_flight[mmc_issue_type(mq, req)] -= 1; | |
1793 | ||
1794 | put_card = (mmc_tot_in_flight(mq) == 0); | |
1795 | ||
1796 | mmc_cqe_check_busy(mq); | |
1797 | ||
1798 | spin_unlock_irqrestore(q->queue_lock, flags); | |
1799 | ||
1800 | if (!mq->cqe_busy) | |
1801 | blk_mq_run_hw_queues(q, true); | |
1802 | ||
1803 | if (put_card) | |
1804 | mmc_put_card(mq->card, &mq->ctx); | |
1805 | } | |
1806 | ||
1807 | void mmc_blk_cqe_recovery(struct mmc_queue *mq) | |
1808 | { | |
1809 | struct mmc_card *card = mq->card; | |
1810 | struct mmc_host *host = card->host; | |
1811 | int err; | |
1812 | ||
1813 | pr_debug("%s: CQE recovery start\n", mmc_hostname(host)); | |
1814 | ||
1815 | err = mmc_cqe_recovery(host); | |
1816 | if (err) | |
1817 | mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY); | |
1818 | else | |
1819 | mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY); | |
1820 | ||
1821 | pr_debug("%s: CQE recovery done\n", mmc_hostname(host)); | |
1822 | } | |
1823 | ||
1824 | static void mmc_blk_cqe_req_done(struct mmc_request *mrq) | |
1825 | { | |
1826 | struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req, | |
1827 | brq.mrq); | |
1828 | struct request *req = mmc_queue_req_to_req(mqrq); | |
1829 | struct request_queue *q = req->q; | |
1830 | struct mmc_queue *mq = q->queuedata; | |
1831 | ||
1832 | /* | |
1833 | * Block layer timeouts race with completions which means the normal | |
1834 | * completion path cannot be used during recovery. | |
1835 | */ | |
1836 | if (mq->in_recovery) | |
1837 | mmc_blk_cqe_complete_rq(mq, req); | |
1838 | else | |
1839 | blk_mq_complete_request(req); | |
1840 | } | |
1841 | ||
1842 | static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq) | |
1843 | { | |
1844 | mrq->done = mmc_blk_cqe_req_done; | |
1845 | mrq->recovery_notifier = mmc_cqe_recovery_notifier; | |
1846 | ||
1847 | return mmc_cqe_start_req(host, mrq); | |
1848 | } | |
1849 | ||
1850 | static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq, | |
1851 | struct request *req) | |
1852 | { | |
1853 | struct mmc_blk_request *brq = &mqrq->brq; | |
1854 | ||
1855 | memset(brq, 0, sizeof(*brq)); | |
1856 | ||
1857 | brq->mrq.cmd = &brq->cmd; | |
1858 | brq->mrq.tag = req->tag; | |
1859 | ||
1860 | return &brq->mrq; | |
1861 | } | |
1862 | ||
1863 | static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req) | |
1864 | { | |
1865 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
1866 | struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req); | |
1867 | ||
1868 | mrq->cmd->opcode = MMC_SWITCH; | |
1869 | mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | | |
1870 | (EXT_CSD_FLUSH_CACHE << 16) | | |
1871 | (1 << 8) | | |
1872 | EXT_CSD_CMD_SET_NORMAL; | |
1873 | mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B; | |
1874 | ||
1875 | return mmc_blk_cqe_start_req(mq->card->host, mrq); | |
1876 | } | |
1877 | ||
1878 | static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req) | |
1879 | { | |
1880 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
1881 | ||
1882 | mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL); | |
1883 | ||
1884 | return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq); | |
1885 | } | |
1886 | ||
ca5717f7 AH |
1887 | static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, |
1888 | struct mmc_card *card, | |
1889 | int disable_multi, | |
1890 | struct mmc_queue *mq) | |
1891 | { | |
1892 | u32 readcmd, writecmd; | |
1893 | struct mmc_blk_request *brq = &mqrq->brq; | |
67e69d52 | 1894 | struct request *req = mmc_queue_req_to_req(mqrq); |
ca5717f7 AH |
1895 | struct mmc_blk_data *md = mq->blkdata; |
1896 | bool do_rel_wr, do_data_tag; | |
1897 | ||
1898 | mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag); | |
1899 | ||
1900 | brq->mrq.cmd = &brq->cmd; | |
1901 | ||
1902 | brq->cmd.arg = blk_rq_pos(req); | |
1903 | if (!mmc_card_blockaddr(card)) | |
1904 | brq->cmd.arg <<= 9; | |
1905 | brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; | |
1906 | ||
54d49d77 PF |
1907 | if (brq->data.blocks > 1 || do_rel_wr) { |
1908 | /* SPI multiblock writes terminate using a special | |
1909 | * token, not a STOP_TRANSMISSION request. | |
d0c97cfb | 1910 | */ |
54d49d77 PF |
1911 | if (!mmc_host_is_spi(card->host) || |
1912 | rq_data_dir(req) == READ) | |
1913 | brq->mrq.stop = &brq->stop; | |
1914 | readcmd = MMC_READ_MULTIPLE_BLOCK; | |
1915 | writecmd = MMC_WRITE_MULTIPLE_BLOCK; | |
1916 | } else { | |
1917 | brq->mrq.stop = NULL; | |
1918 | readcmd = MMC_READ_SINGLE_BLOCK; | |
1919 | writecmd = MMC_WRITE_BLOCK; | |
1920 | } | |
ca5717f7 | 1921 | brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd; |
4265900e | 1922 | |
54d49d77 PF |
1923 | /* |
1924 | * Pre-defined multi-block transfers are preferable to | |
1925 | * open ended-ones (and necessary for reliable writes). | |
1926 | * However, it is not sufficient to just send CMD23, | |
1927 | * and avoid the final CMD12, as on an error condition | |
1928 | * CMD12 (stop) needs to be sent anyway. This, coupled | |
1929 | * with Auto-CMD23 enhancements provided by some | |
1930 | * hosts, means that the complexity of dealing | |
1931 | * with this is best left to the host. If CMD23 is | |
1932 | * supported by card and host, we'll fill sbc in and let | |
1933 | * the host deal with handling it correctly. This means | |
1934 | * that for hosts that don't expose MMC_CAP_CMD23, no | |
1935 | * change of behavior will be observed. | |
1936 | * | |
1937 | * N.B: Some MMC cards experience perf degradation. | |
1938 | * We'll avoid using CMD23-bounded multiblock writes for | |
1939 | * these, while retaining features like reliable writes. | |
1940 | */ | |
4265900e SD |
1941 | if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) && |
1942 | (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) || | |
1943 | do_data_tag)) { | |
54d49d77 PF |
1944 | brq->sbc.opcode = MMC_SET_BLOCK_COUNT; |
1945 | brq->sbc.arg = brq->data.blocks | | |
4265900e SD |
1946 | (do_rel_wr ? (1 << 31) : 0) | |
1947 | (do_data_tag ? (1 << 29) : 0); | |
54d49d77 PF |
1948 | brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; |
1949 | brq->mrq.sbc = &brq->sbc; | |
1950 | } | |
98ccf149 | 1951 | |
74f5ba35 | 1952 | mqrq->areq.err_check = mmc_blk_err_check; |
54d49d77 | 1953 | } |
6a79e391 | 1954 | |
81196976 AH |
1955 | #define MMC_MAX_RETRIES 5 |
1956 | #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1) | |
1957 | ||
1958 | #define MMC_READ_SINGLE_RETRIES 2 | |
1959 | ||
1960 | /* Single sector read during recovery */ | |
1961 | static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req) | |
1962 | { | |
1963 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
1964 | struct mmc_request *mrq = &mqrq->brq.mrq; | |
1965 | struct mmc_card *card = mq->card; | |
1966 | struct mmc_host *host = card->host; | |
1967 | blk_status_t error = BLK_STS_OK; | |
1968 | int retries = 0; | |
1969 | ||
1970 | do { | |
1971 | u32 status; | |
1972 | int err; | |
1973 | ||
1974 | mmc_blk_rw_rq_prep(mqrq, card, 1, mq); | |
1975 | ||
1976 | mmc_wait_for_req(host, mrq); | |
1977 | ||
1978 | err = mmc_send_status(card, &status); | |
1979 | if (err) | |
1980 | goto error_exit; | |
1981 | ||
1982 | if (!mmc_host_is_spi(host) && | |
1983 | R1_CURRENT_STATE(status) != R1_STATE_TRAN) { | |
1984 | u32 stop_status = 0; | |
1985 | bool gen_err = false; | |
1986 | ||
1987 | err = send_stop(card, | |
1988 | DIV_ROUND_UP(mrq->data->timeout_ns, | |
1989 | 1000000), | |
1990 | req, &gen_err, &stop_status); | |
1991 | if (err) | |
1992 | goto error_exit; | |
1993 | } | |
1994 | ||
1995 | if (mrq->cmd->error && retries++ < MMC_READ_SINGLE_RETRIES) | |
1996 | continue; | |
1997 | ||
1998 | retries = 0; | |
1999 | ||
2000 | if (mrq->cmd->error || | |
2001 | mrq->data->error || | |
2002 | (!mmc_host_is_spi(host) && | |
2003 | (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS))) | |
2004 | error = BLK_STS_IOERR; | |
2005 | else | |
2006 | error = BLK_STS_OK; | |
2007 | ||
2008 | } while (blk_update_request(req, error, 512)); | |
2009 | ||
2010 | return; | |
2011 | ||
2012 | error_exit: | |
2013 | mrq->data->bytes_xfered = 0; | |
2014 | blk_update_request(req, BLK_STS_IOERR, 512); | |
2015 | /* Let it try the remaining request again */ | |
2016 | if (mqrq->retries > MMC_MAX_RETRIES - 1) | |
2017 | mqrq->retries = MMC_MAX_RETRIES - 1; | |
2018 | } | |
2019 | ||
2020 | static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req) | |
2021 | { | |
2022 | int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; | |
2023 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
2024 | struct mmc_blk_request *brq = &mqrq->brq; | |
2025 | struct mmc_blk_data *md = mq->blkdata; | |
2026 | struct mmc_card *card = mq->card; | |
2027 | static enum mmc_blk_status status; | |
2028 | ||
2029 | brq->retune_retry_done = mqrq->retries; | |
2030 | ||
2031 | status = __mmc_blk_err_check(card, mqrq); | |
2032 | ||
2033 | mmc_retune_release(card->host); | |
2034 | ||
2035 | /* | |
2036 | * Requests are completed by mmc_blk_mq_complete_rq() which sets simple | |
2037 | * policy: | |
2038 | * 1. A request that has transferred at least some data is considered | |
2039 | * successful and will be requeued if there is remaining data to | |
2040 | * transfer. | |
2041 | * 2. Otherwise the number of retries is incremented and the request | |
2042 | * will be requeued if there are remaining retries. | |
2043 | * 3. Otherwise the request will be errored out. | |
2044 | * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and | |
2045 | * mqrq->retries. So there are only 4 possible actions here: | |
2046 | * 1. do not accept the bytes_xfered value i.e. set it to zero | |
2047 | * 2. change mqrq->retries to determine the number of retries | |
2048 | * 3. try to reset the card | |
2049 | * 4. read one sector at a time | |
2050 | */ | |
2051 | switch (status) { | |
2052 | case MMC_BLK_SUCCESS: | |
2053 | case MMC_BLK_PARTIAL: | |
2054 | /* Reset success, and accept bytes_xfered */ | |
2055 | mmc_blk_reset_success(md, type); | |
2056 | break; | |
2057 | case MMC_BLK_CMD_ERR: | |
2058 | /* | |
2059 | * For SD cards, get bytes written, but do not accept | |
2060 | * bytes_xfered if that fails. For MMC cards accept | |
2061 | * bytes_xfered. Then try to reset. If reset fails then | |
2062 | * error out the remaining request, otherwise retry | |
2063 | * once (N.B mmc_blk_reset() will not succeed twice in a | |
2064 | * row). | |
2065 | */ | |
2066 | if (mmc_card_sd(card)) { | |
2067 | u32 blocks; | |
2068 | int err; | |
2069 | ||
2070 | err = mmc_sd_num_wr_blocks(card, &blocks); | |
2071 | if (err) | |
2072 | brq->data.bytes_xfered = 0; | |
2073 | else | |
2074 | brq->data.bytes_xfered = blocks << 9; | |
2075 | } | |
2076 | if (mmc_blk_reset(md, card->host, type)) | |
2077 | mqrq->retries = MMC_NO_RETRIES; | |
2078 | else | |
2079 | mqrq->retries = MMC_MAX_RETRIES - 1; | |
2080 | break; | |
2081 | case MMC_BLK_RETRY: | |
2082 | /* | |
2083 | * Do not accept bytes_xfered, but retry up to 5 times, | |
2084 | * otherwise same as abort. | |
2085 | */ | |
2086 | brq->data.bytes_xfered = 0; | |
2087 | if (mqrq->retries < MMC_MAX_RETRIES) | |
2088 | break; | |
2089 | /* Fall through */ | |
2090 | case MMC_BLK_ABORT: | |
2091 | /* | |
2092 | * Do not accept bytes_xfered, but try to reset. If | |
2093 | * reset succeeds, try once more, otherwise error out | |
2094 | * the request. | |
2095 | */ | |
2096 | brq->data.bytes_xfered = 0; | |
2097 | if (mmc_blk_reset(md, card->host, type)) | |
2098 | mqrq->retries = MMC_NO_RETRIES; | |
2099 | else | |
2100 | mqrq->retries = MMC_MAX_RETRIES - 1; | |
2101 | break; | |
2102 | case MMC_BLK_DATA_ERR: { | |
2103 | int err; | |
2104 | ||
2105 | /* | |
2106 | * Do not accept bytes_xfered, but try to reset. If | |
2107 | * reset succeeds, try once more. If reset fails with | |
2108 | * ENODEV which means the partition is wrong, then error | |
2109 | * out the request. Otherwise attempt to read one sector | |
2110 | * at a time. | |
2111 | */ | |
2112 | brq->data.bytes_xfered = 0; | |
2113 | err = mmc_blk_reset(md, card->host, type); | |
2114 | if (!err) { | |
2115 | mqrq->retries = MMC_MAX_RETRIES - 1; | |
2116 | break; | |
2117 | } | |
2118 | if (err == -ENODEV) { | |
2119 | mqrq->retries = MMC_NO_RETRIES; | |
2120 | break; | |
2121 | } | |
2122 | /* Fall through */ | |
2123 | } | |
2124 | case MMC_BLK_ECC_ERR: | |
2125 | /* | |
2126 | * Do not accept bytes_xfered. If reading more than one | |
2127 | * sector, try reading one sector at a time. | |
2128 | */ | |
2129 | brq->data.bytes_xfered = 0; | |
2130 | /* FIXME: Missing single sector read for large sector size */ | |
2131 | if (brq->data.blocks > 1 && !mmc_large_sector(card)) { | |
2132 | /* Redo read one sector at a time */ | |
2133 | pr_warn("%s: retrying using single block read\n", | |
2134 | req->rq_disk->disk_name); | |
2135 | mmc_blk_read_single(mq, req); | |
2136 | } else { | |
2137 | mqrq->retries = MMC_NO_RETRIES; | |
2138 | } | |
2139 | break; | |
2140 | case MMC_BLK_NOMEDIUM: | |
2141 | /* Do not accept bytes_xfered. Error out the request */ | |
2142 | brq->data.bytes_xfered = 0; | |
2143 | mqrq->retries = MMC_NO_RETRIES; | |
2144 | break; | |
2145 | default: | |
2146 | /* Do not accept bytes_xfered. Error out the request */ | |
2147 | brq->data.bytes_xfered = 0; | |
2148 | mqrq->retries = MMC_NO_RETRIES; | |
2149 | pr_err("%s: Unhandled return value (%d)", | |
2150 | req->rq_disk->disk_name, status); | |
2151 | break; | |
2152 | } | |
2153 | } | |
2154 | ||
10f21df4 AH |
2155 | static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq) |
2156 | { | |
2157 | mmc_blk_eval_resp_error(brq); | |
2158 | ||
2159 | return brq->sbc.error || brq->cmd.error || brq->stop.error || | |
2160 | brq->data.error || brq->cmd.resp[0] & CMD_ERRORS; | |
2161 | } | |
2162 | ||
f47a1fe3 AH |
2163 | static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq) |
2164 | { | |
2165 | return !!brq->mrq.sbc; | |
2166 | } | |
2167 | ||
2168 | static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq) | |
2169 | { | |
2170 | return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR; | |
2171 | } | |
2172 | ||
88a51646 AH |
2173 | static int mmc_blk_card_busy(struct mmc_card *card, struct request *req) |
2174 | { | |
2175 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
f47a1fe3 | 2176 | u32 status = 0; |
88a51646 AH |
2177 | int err; |
2178 | ||
2179 | if (mmc_host_is_spi(card->host) || rq_data_dir(req) == READ) | |
2180 | return 0; | |
2181 | ||
f47a1fe3 | 2182 | err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req, &status); |
88a51646 | 2183 | |
f47a1fe3 AH |
2184 | /* |
2185 | * Do not assume data transferred correctly if there are any error bits | |
2186 | * set. | |
2187 | */ | |
2188 | if (status & mmc_blk_stop_err_bits(&mqrq->brq)) { | |
2189 | mqrq->brq.data.bytes_xfered = 0; | |
88a51646 AH |
2190 | err = err ? err : -EIO; |
2191 | } | |
2192 | ||
f47a1fe3 AH |
2193 | /* Copy the exception bit so it will be seen later on */ |
2194 | if (mmc_card_mmc(card) && status & R1_EXCEPTION_EVENT) | |
2195 | mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT; | |
2196 | ||
88a51646 AH |
2197 | return err; |
2198 | } | |
2199 | ||
10f21df4 AH |
2200 | static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq, |
2201 | struct request *req) | |
2202 | { | |
2203 | int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; | |
2204 | ||
2205 | mmc_blk_reset_success(mq->blkdata, type); | |
2206 | } | |
2207 | ||
81196976 AH |
2208 | static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req) |
2209 | { | |
2210 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
2211 | unsigned int nr_bytes = mqrq->brq.data.bytes_xfered; | |
2212 | ||
2213 | if (nr_bytes) { | |
2214 | if (blk_update_request(req, BLK_STS_OK, nr_bytes)) | |
2215 | blk_mq_requeue_request(req, true); | |
2216 | else | |
2217 | __blk_mq_end_request(req, BLK_STS_OK); | |
2218 | } else if (!blk_rq_bytes(req)) { | |
2219 | __blk_mq_end_request(req, BLK_STS_IOERR); | |
2220 | } else if (mqrq->retries++ < MMC_MAX_RETRIES) { | |
2221 | blk_mq_requeue_request(req, true); | |
2222 | } else { | |
2223 | if (mmc_card_removed(mq->card)) | |
2224 | req->rq_flags |= RQF_QUIET; | |
2225 | blk_mq_end_request(req, BLK_STS_IOERR); | |
2226 | } | |
2227 | } | |
2228 | ||
2229 | static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq, | |
2230 | struct mmc_queue_req *mqrq) | |
2231 | { | |
2232 | return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) && | |
2233 | (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT || | |
2234 | mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT); | |
2235 | } | |
2236 | ||
2237 | static void mmc_blk_urgent_bkops(struct mmc_queue *mq, | |
2238 | struct mmc_queue_req *mqrq) | |
2239 | { | |
2240 | if (mmc_blk_urgent_bkops_needed(mq, mqrq)) | |
2241 | mmc_start_bkops(mq->card, true); | |
2242 | } | |
2243 | ||
2244 | void mmc_blk_mq_complete(struct request *req) | |
2245 | { | |
2246 | struct mmc_queue *mq = req->q->queuedata; | |
2247 | ||
1e8e55b6 AH |
2248 | if (mq->use_cqe) |
2249 | mmc_blk_cqe_complete_rq(mq, req); | |
2250 | else | |
2251 | mmc_blk_mq_complete_rq(mq, req); | |
81196976 AH |
2252 | } |
2253 | ||
2254 | static void mmc_blk_mq_poll_completion(struct mmc_queue *mq, | |
2255 | struct request *req) | |
2256 | { | |
2257 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
88a51646 | 2258 | struct mmc_host *host = mq->card->host; |
81196976 | 2259 | |
88a51646 AH |
2260 | if (mmc_blk_rq_error(&mqrq->brq) || |
2261 | mmc_blk_card_busy(mq->card, req)) { | |
2262 | mmc_blk_mq_rw_recovery(mq, req); | |
2263 | } else { | |
2264 | mmc_blk_rw_reset_success(mq, req); | |
2265 | mmc_retune_release(host); | |
2266 | } | |
81196976 AH |
2267 | |
2268 | mmc_blk_urgent_bkops(mq, mqrq); | |
2269 | } | |
2270 | ||
2271 | static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, struct request *req) | |
2272 | { | |
2273 | struct request_queue *q = req->q; | |
2274 | unsigned long flags; | |
2275 | bool put_card; | |
2276 | ||
2277 | spin_lock_irqsave(q->queue_lock, flags); | |
2278 | ||
2279 | mq->in_flight[mmc_issue_type(mq, req)] -= 1; | |
2280 | ||
2281 | put_card = (mmc_tot_in_flight(mq) == 0); | |
2282 | ||
2283 | spin_unlock_irqrestore(q->queue_lock, flags); | |
2284 | ||
2285 | if (put_card) | |
2286 | mmc_put_card(mq->card, &mq->ctx); | |
2287 | } | |
2288 | ||
2289 | static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req) | |
2290 | { | |
2291 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
2292 | struct mmc_request *mrq = &mqrq->brq.mrq; | |
2293 | struct mmc_host *host = mq->card->host; | |
2294 | ||
2295 | mmc_post_req(host, mrq, 0); | |
2296 | ||
10f21df4 AH |
2297 | /* |
2298 | * Block layer timeouts race with completions which means the normal | |
2299 | * completion path cannot be used during recovery. | |
2300 | */ | |
2301 | if (mq->in_recovery) | |
2302 | mmc_blk_mq_complete_rq(mq, req); | |
2303 | else | |
2304 | blk_mq_complete_request(req); | |
81196976 AH |
2305 | |
2306 | mmc_blk_mq_dec_in_flight(mq, req); | |
2307 | } | |
2308 | ||
10f21df4 AH |
2309 | void mmc_blk_mq_recovery(struct mmc_queue *mq) |
2310 | { | |
2311 | struct request *req = mq->recovery_req; | |
2312 | struct mmc_host *host = mq->card->host; | |
2313 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
2314 | ||
2315 | mq->recovery_req = NULL; | |
2316 | mq->rw_wait = false; | |
2317 | ||
2318 | if (mmc_blk_rq_error(&mqrq->brq)) { | |
2319 | mmc_retune_hold_now(host); | |
2320 | mmc_blk_mq_rw_recovery(mq, req); | |
2321 | } | |
2322 | ||
2323 | mmc_blk_urgent_bkops(mq, mqrq); | |
2324 | ||
2325 | mmc_blk_mq_post_req(mq, req); | |
2326 | } | |
2327 | ||
81196976 AH |
2328 | static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq, |
2329 | struct request **prev_req) | |
2330 | { | |
10f21df4 AH |
2331 | if (mmc_host_done_complete(mq->card->host)) |
2332 | return; | |
2333 | ||
81196976 AH |
2334 | mutex_lock(&mq->complete_lock); |
2335 | ||
2336 | if (!mq->complete_req) | |
2337 | goto out_unlock; | |
2338 | ||
2339 | mmc_blk_mq_poll_completion(mq, mq->complete_req); | |
2340 | ||
2341 | if (prev_req) | |
2342 | *prev_req = mq->complete_req; | |
2343 | else | |
2344 | mmc_blk_mq_post_req(mq, mq->complete_req); | |
2345 | ||
2346 | mq->complete_req = NULL; | |
2347 | ||
2348 | out_unlock: | |
2349 | mutex_unlock(&mq->complete_lock); | |
2350 | } | |
2351 | ||
2352 | void mmc_blk_mq_complete_work(struct work_struct *work) | |
2353 | { | |
2354 | struct mmc_queue *mq = container_of(work, struct mmc_queue, | |
2355 | complete_work); | |
2356 | ||
2357 | mmc_blk_mq_complete_prev_req(mq, NULL); | |
2358 | } | |
2359 | ||
2360 | static void mmc_blk_mq_req_done(struct mmc_request *mrq) | |
2361 | { | |
2362 | struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req, | |
2363 | brq.mrq); | |
2364 | struct request *req = mmc_queue_req_to_req(mqrq); | |
2365 | struct request_queue *q = req->q; | |
2366 | struct mmc_queue *mq = q->queuedata; | |
10f21df4 | 2367 | struct mmc_host *host = mq->card->host; |
81196976 | 2368 | unsigned long flags; |
81196976 | 2369 | |
10f21df4 AH |
2370 | if (!mmc_host_done_complete(host)) { |
2371 | bool waiting; | |
81196976 | 2372 | |
10f21df4 AH |
2373 | /* |
2374 | * We cannot complete the request in this context, so record | |
2375 | * that there is a request to complete, and that a following | |
2376 | * request does not need to wait (although it does need to | |
2377 | * complete complete_req first). | |
2378 | */ | |
2379 | spin_lock_irqsave(q->queue_lock, flags); | |
2380 | mq->complete_req = req; | |
2381 | mq->rw_wait = false; | |
2382 | waiting = mq->waiting; | |
2383 | spin_unlock_irqrestore(q->queue_lock, flags); | |
2384 | ||
2385 | /* | |
2386 | * If 'waiting' then the waiting task will complete this | |
2387 | * request, otherwise queue a work to do it. Note that | |
2388 | * complete_work may still race with the dispatch of a following | |
2389 | * request. | |
2390 | */ | |
2391 | if (waiting) | |
2392 | wake_up(&mq->wait); | |
2393 | else | |
2394 | kblockd_schedule_work(&mq->complete_work); | |
2395 | ||
2396 | return; | |
2397 | } | |
2398 | ||
2399 | /* Take the recovery path for errors or urgent background operations */ | |
2400 | if (mmc_blk_rq_error(&mqrq->brq) || | |
2401 | mmc_blk_urgent_bkops_needed(mq, mqrq)) { | |
2402 | spin_lock_irqsave(q->queue_lock, flags); | |
2403 | mq->recovery_needed = true; | |
2404 | mq->recovery_req = req; | |
2405 | spin_unlock_irqrestore(q->queue_lock, flags); | |
81196976 | 2406 | wake_up(&mq->wait); |
10f21df4 AH |
2407 | schedule_work(&mq->recovery_work); |
2408 | return; | |
2409 | } | |
2410 | ||
2411 | mmc_blk_rw_reset_success(mq, req); | |
2412 | ||
2413 | mq->rw_wait = false; | |
2414 | wake_up(&mq->wait); | |
2415 | ||
2416 | mmc_blk_mq_post_req(mq, req); | |
81196976 AH |
2417 | } |
2418 | ||
2419 | static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err) | |
2420 | { | |
2421 | struct request_queue *q = mq->queue; | |
2422 | unsigned long flags; | |
2423 | bool done; | |
2424 | ||
2425 | /* | |
10f21df4 AH |
2426 | * Wait while there is another request in progress, but not if recovery |
2427 | * is needed. Also indicate whether there is a request waiting to start. | |
81196976 AH |
2428 | */ |
2429 | spin_lock_irqsave(q->queue_lock, flags); | |
10f21df4 AH |
2430 | if (mq->recovery_needed) { |
2431 | *err = -EBUSY; | |
2432 | done = true; | |
2433 | } else { | |
2434 | done = !mq->rw_wait; | |
2435 | } | |
81196976 AH |
2436 | mq->waiting = !done; |
2437 | spin_unlock_irqrestore(q->queue_lock, flags); | |
2438 | ||
2439 | return done; | |
2440 | } | |
2441 | ||
2442 | static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req) | |
2443 | { | |
2444 | int err = 0; | |
2445 | ||
2446 | wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err)); | |
2447 | ||
2448 | /* Always complete the previous request if there is one */ | |
2449 | mmc_blk_mq_complete_prev_req(mq, prev_req); | |
2450 | ||
2451 | return err; | |
2452 | } | |
2453 | ||
2454 | static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq, | |
2455 | struct request *req) | |
2456 | { | |
2457 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
2458 | struct mmc_host *host = mq->card->host; | |
2459 | struct request *prev_req = NULL; | |
2460 | int err = 0; | |
2461 | ||
2462 | mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq); | |
2463 | ||
2464 | mqrq->brq.mrq.done = mmc_blk_mq_req_done; | |
2465 | ||
2466 | mmc_pre_req(host, &mqrq->brq.mrq); | |
2467 | ||
2468 | err = mmc_blk_rw_wait(mq, &prev_req); | |
2469 | if (err) | |
2470 | goto out_post_req; | |
2471 | ||
2472 | mq->rw_wait = true; | |
2473 | ||
2474 | err = mmc_start_request(host, &mqrq->brq.mrq); | |
2475 | ||
2476 | if (prev_req) | |
2477 | mmc_blk_mq_post_req(mq, prev_req); | |
2478 | ||
10f21df4 | 2479 | if (err) |
81196976 | 2480 | mq->rw_wait = false; |
10f21df4 AH |
2481 | |
2482 | /* Release re-tuning here where there is no synchronization required */ | |
2483 | if (err || mmc_host_done_complete(host)) | |
81196976 | 2484 | mmc_retune_release(host); |
81196976 AH |
2485 | |
2486 | out_post_req: | |
2487 | if (err) | |
2488 | mmc_post_req(host, &mqrq->brq.mrq, err); | |
2489 | ||
2490 | return err; | |
2491 | } | |
2492 | ||
2493 | static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host) | |
2494 | { | |
1e8e55b6 AH |
2495 | if (mq->use_cqe) |
2496 | return host->cqe_ops->cqe_wait_for_idle(host); | |
2497 | ||
81196976 AH |
2498 | return mmc_blk_rw_wait(mq, NULL); |
2499 | } | |
2500 | ||
2501 | enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req) | |
2502 | { | |
2503 | struct mmc_blk_data *md = mq->blkdata; | |
2504 | struct mmc_card *card = md->queue.card; | |
2505 | struct mmc_host *host = card->host; | |
2506 | int ret; | |
2507 | ||
2508 | ret = mmc_blk_part_switch(card, md->part_type); | |
2509 | if (ret) | |
2510 | return MMC_REQ_FAILED_TO_START; | |
2511 | ||
2512 | switch (mmc_issue_type(mq, req)) { | |
2513 | case MMC_ISSUE_SYNC: | |
2514 | ret = mmc_blk_wait_for_idle(mq, host); | |
2515 | if (ret) | |
2516 | return MMC_REQ_BUSY; | |
2517 | switch (req_op(req)) { | |
2518 | case REQ_OP_DRV_IN: | |
2519 | case REQ_OP_DRV_OUT: | |
2520 | mmc_blk_issue_drv_op(mq, req); | |
2521 | break; | |
2522 | case REQ_OP_DISCARD: | |
2523 | mmc_blk_issue_discard_rq(mq, req); | |
2524 | break; | |
2525 | case REQ_OP_SECURE_ERASE: | |
2526 | mmc_blk_issue_secdiscard_rq(mq, req); | |
2527 | break; | |
2528 | case REQ_OP_FLUSH: | |
2529 | mmc_blk_issue_flush(mq, req); | |
2530 | break; | |
2531 | default: | |
2532 | WARN_ON_ONCE(1); | |
2533 | return MMC_REQ_FAILED_TO_START; | |
2534 | } | |
2535 | return MMC_REQ_FINISHED; | |
1e8e55b6 | 2536 | case MMC_ISSUE_DCMD: |
81196976 AH |
2537 | case MMC_ISSUE_ASYNC: |
2538 | switch (req_op(req)) { | |
1e8e55b6 AH |
2539 | case REQ_OP_FLUSH: |
2540 | ret = mmc_blk_cqe_issue_flush(mq, req); | |
2541 | break; | |
81196976 AH |
2542 | case REQ_OP_READ: |
2543 | case REQ_OP_WRITE: | |
1e8e55b6 AH |
2544 | if (mq->use_cqe) |
2545 | ret = mmc_blk_cqe_issue_rw_rq(mq, req); | |
2546 | else | |
2547 | ret = mmc_blk_mq_issue_rw_rq(mq, req); | |
81196976 AH |
2548 | break; |
2549 | default: | |
2550 | WARN_ON_ONCE(1); | |
2551 | ret = -EINVAL; | |
2552 | } | |
2553 | if (!ret) | |
2554 | return MMC_REQ_STARTED; | |
2555 | return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START; | |
2556 | default: | |
2557 | WARN_ON_ONCE(1); | |
2558 | return MMC_REQ_FAILED_TO_START; | |
2559 | } | |
2560 | } | |
2561 | ||
0e65f10c LW |
2562 | static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card, |
2563 | struct mmc_blk_request *brq, struct request *req, | |
2564 | bool old_req_pending) | |
67716327 | 2565 | { |
0e65f10c LW |
2566 | bool req_pending; |
2567 | ||
67716327 AH |
2568 | /* |
2569 | * If this is an SD card and we're writing, we can first | |
2570 | * mark the known good sectors as ok. | |
2571 | * | |
2572 | * If the card is not SD, we can still ok written sectors | |
2573 | * as reported by the controller (which might be less than | |
2574 | * the real number of written sectors, but never more). | |
2575 | */ | |
2576 | if (mmc_card_sd(card)) { | |
2577 | u32 blocks; | |
169f03a0 | 2578 | int err; |
67716327 | 2579 | |
169f03a0 | 2580 | err = mmc_sd_num_wr_blocks(card, &blocks); |
0e65f10c LW |
2581 | if (err) |
2582 | req_pending = old_req_pending; | |
2583 | else | |
a7c17d8a | 2584 | req_pending = blk_end_request(req, BLK_STS_OK, blocks << 9); |
5dd784d2 | 2585 | } else { |
a7c17d8a | 2586 | req_pending = blk_end_request(req, BLK_STS_OK, brq->data.bytes_xfered); |
ce39f9d1 | 2587 | } |
0e65f10c | 2588 | return req_pending; |
ce39f9d1 SJ |
2589 | } |
2590 | ||
cdf8a6fb AH |
2591 | static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card, |
2592 | struct request *req, | |
2593 | struct mmc_queue_req *mqrq) | |
4e1f7800 | 2594 | { |
4e1f7800 LW |
2595 | if (mmc_card_removed(card)) |
2596 | req->rq_flags |= RQF_QUIET; | |
2a842aca | 2597 | while (blk_end_request(req, BLK_STS_IOERR, blk_rq_cur_bytes(req))); |
304419d8 | 2598 | mq->qcnt--; |
4e1f7800 LW |
2599 | } |
2600 | ||
b2928e10 LW |
2601 | /** |
2602 | * mmc_blk_rw_try_restart() - tries to restart the current async request | |
2603 | * @mq: the queue with the card and host to restart | |
2604 | * @req: a new request that want to be started after the current one | |
2605 | */ | |
8ddfe07e AH |
2606 | static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req, |
2607 | struct mmc_queue_req *mqrq) | |
efb5a05e LW |
2608 | { |
2609 | if (!req) | |
2610 | return; | |
2611 | ||
b2928e10 LW |
2612 | /* |
2613 | * If the card was removed, just cancel everything and return. | |
2614 | */ | |
2615 | if (mmc_card_removed(mq->card)) { | |
efb5a05e | 2616 | req->rq_flags |= RQF_QUIET; |
2a842aca | 2617 | blk_end_request_all(req, BLK_STS_IOERR); |
304419d8 | 2618 | mq->qcnt--; /* FIXME: just set to 0? */ |
b2928e10 | 2619 | return; |
efb5a05e | 2620 | } |
b2928e10 | 2621 | /* Else proceed and try to restart the current async request */ |
8ddfe07e AH |
2622 | mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq); |
2623 | mmc_start_areq(mq->card->host, &mqrq->areq, NULL); | |
efb5a05e LW |
2624 | } |
2625 | ||
acd8dbd6 | 2626 | static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req) |
54d49d77 | 2627 | { |
7db3028e | 2628 | struct mmc_blk_data *md = mq->blkdata; |
54d49d77 | 2629 | struct mmc_card *card = md->queue.card; |
5be80375 | 2630 | struct mmc_blk_request *brq; |
0e65f10c | 2631 | int disable_multi = 0, retry = 0, type, retune_retry_done = 0; |
d78d4a8a | 2632 | enum mmc_blk_status status; |
cdf8a6fb | 2633 | struct mmc_queue_req *mqrq_cur = NULL; |
ee8a43a5 | 2634 | struct mmc_queue_req *mq_rq; |
acd8dbd6 | 2635 | struct request *old_req; |
7d552a48 LW |
2636 | struct mmc_async_req *new_areq; |
2637 | struct mmc_async_req *old_areq; | |
0e65f10c | 2638 | bool req_pending = true; |
1da177e4 | 2639 | |
cdf8a6fb | 2640 | if (new_req) { |
304419d8 LW |
2641 | mqrq_cur = req_to_mmc_queue_req(new_req); |
2642 | mq->qcnt++; | |
cdf8a6fb AH |
2643 | } |
2644 | ||
2645 | if (!mq->qcnt) | |
df061588 | 2646 | return; |
98ccf149 | 2647 | |
ee8a43a5 | 2648 | do { |
acd8dbd6 | 2649 | if (new_req) { |
a5075eb9 SD |
2650 | /* |
2651 | * When 4KB native sector is enabled, only 8 blocks | |
2652 | * multiple read or write is allowed | |
2653 | */ | |
e87c8561 | 2654 | if (mmc_large_sector(card) && |
acd8dbd6 | 2655 | !IS_ALIGNED(blk_rq_sectors(new_req), 8)) { |
a5075eb9 | 2656 | pr_err("%s: Transfer size is not 4KB sector size aligned\n", |
acd8dbd6 | 2657 | new_req->rq_disk->disk_name); |
cdf8a6fb | 2658 | mmc_blk_rw_cmd_abort(mq, card, new_req, mqrq_cur); |
df061588 | 2659 | return; |
a5075eb9 | 2660 | } |
ce39f9d1 | 2661 | |
8ddfe07e AH |
2662 | mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq); |
2663 | new_areq = &mqrq_cur->areq; | |
ee8a43a5 | 2664 | } else |
7d552a48 LW |
2665 | new_areq = NULL; |
2666 | ||
c3399ef5 | 2667 | old_areq = mmc_start_areq(card->host, new_areq, &status); |
7d552a48 | 2668 | if (!old_areq) { |
da0dbaff LW |
2669 | /* |
2670 | * We have just put the first request into the pipeline | |
2671 | * and there is nothing more to do until it is | |
2672 | * complete. | |
2673 | */ | |
df061588 | 2674 | return; |
2220eedf | 2675 | } |
ee8a43a5 | 2676 | |
da0dbaff LW |
2677 | /* |
2678 | * An asynchronous request has been completed and we proceed | |
2679 | * to handle the result of it. | |
2680 | */ | |
74f5ba35 | 2681 | mq_rq = container_of(old_areq, struct mmc_queue_req, areq); |
ee8a43a5 | 2682 | brq = &mq_rq->brq; |
67e69d52 | 2683 | old_req = mmc_queue_req_to_req(mq_rq); |
acd8dbd6 | 2684 | type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; |
98ccf149 | 2685 | |
d78d4a8a PF |
2686 | switch (status) { |
2687 | case MMC_BLK_SUCCESS: | |
2688 | case MMC_BLK_PARTIAL: | |
2689 | /* | |
6d3898a6 AH |
2690 | * Reset success, and accept bytes_xfered. For |
2691 | * MMC_BLK_PARTIAL re-submit the remaining request. For | |
2692 | * MMC_BLK_SUCCESS error out the remaining request (it | |
2693 | * could not be re-submitted anyway if a next request | |
2694 | * had already begun). | |
d78d4a8a | 2695 | */ |
67716327 | 2696 | mmc_blk_reset_success(md, type); |
ce39f9d1 | 2697 | |
2a842aca | 2698 | req_pending = blk_end_request(old_req, BLK_STS_OK, |
0e65f10c | 2699 | brq->data.bytes_xfered); |
67716327 AH |
2700 | /* |
2701 | * If the blk_end_request function returns non-zero even | |
2702 | * though all data has been transferred and no errors | |
2703 | * were returned by the host controller, it's a bug. | |
2704 | */ | |
0e65f10c | 2705 | if (status == MMC_BLK_SUCCESS && req_pending) { |
a3c76eb9 | 2706 | pr_err("%s BUG rq_tot %d d_xfer %d\n", |
acd8dbd6 | 2707 | __func__, blk_rq_bytes(old_req), |
ee8a43a5 | 2708 | brq->data.bytes_xfered); |
cdf8a6fb | 2709 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
df061588 | 2710 | return; |
ee8a43a5 | 2711 | } |
d78d4a8a PF |
2712 | break; |
2713 | case MMC_BLK_CMD_ERR: | |
6d3898a6 AH |
2714 | /* |
2715 | * For SD cards, get bytes written, but do not accept | |
2716 | * bytes_xfered if that fails. For MMC cards accept | |
2717 | * bytes_xfered. Then try to reset. If reset fails then | |
2718 | * error out the remaining request, otherwise retry | |
2719 | * once (N.B mmc_blk_reset() will not succeed twice in a | |
2720 | * row). | |
2721 | */ | |
0e65f10c | 2722 | req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending); |
db435505 | 2723 | if (mmc_blk_reset(md, card->host, type)) { |
8ecc3444 | 2724 | if (req_pending) |
cdf8a6fb AH |
2725 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
2726 | else | |
304419d8 | 2727 | mq->qcnt--; |
8ddfe07e | 2728 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
db435505 LW |
2729 | return; |
2730 | } | |
0e65f10c | 2731 | if (!req_pending) { |
304419d8 | 2732 | mq->qcnt--; |
8ddfe07e | 2733 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
db435505 LW |
2734 | return; |
2735 | } | |
29535f7b | 2736 | break; |
d78d4a8a | 2737 | case MMC_BLK_RETRY: |
6d3898a6 AH |
2738 | /* |
2739 | * Do not accept bytes_xfered, but retry up to 5 times, | |
2740 | * otherwise same as abort. | |
2741 | */ | |
b8360a49 | 2742 | retune_retry_done = brq->retune_retry_done; |
d78d4a8a | 2743 | if (retry++ < 5) |
a01f3ccf | 2744 | break; |
67716327 | 2745 | /* Fall through */ |
d78d4a8a | 2746 | case MMC_BLK_ABORT: |
6d3898a6 AH |
2747 | /* |
2748 | * Do not accept bytes_xfered, but try to reset. If | |
2749 | * reset succeeds, try once more, otherwise error out | |
2750 | * the request. | |
2751 | */ | |
67716327 AH |
2752 | if (!mmc_blk_reset(md, card->host, type)) |
2753 | break; | |
cdf8a6fb | 2754 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
8ddfe07e | 2755 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
db435505 | 2756 | return; |
67716327 AH |
2757 | case MMC_BLK_DATA_ERR: { |
2758 | int err; | |
2759 | ||
6d3898a6 AH |
2760 | /* |
2761 | * Do not accept bytes_xfered, but try to reset. If | |
2762 | * reset succeeds, try once more. If reset fails with | |
2763 | * ENODEV which means the partition is wrong, then error | |
2764 | * out the request. Otherwise attempt to read one sector | |
2765 | * at a time. | |
2766 | */ | |
67716327 AH |
2767 | err = mmc_blk_reset(md, card->host, type); |
2768 | if (!err) | |
2769 | break; | |
db435505 | 2770 | if (err == -ENODEV) { |
cdf8a6fb | 2771 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
8ddfe07e | 2772 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
db435505 LW |
2773 | return; |
2774 | } | |
67716327 AH |
2775 | /* Fall through */ |
2776 | } | |
2777 | case MMC_BLK_ECC_ERR: | |
6d3898a6 AH |
2778 | /* |
2779 | * Do not accept bytes_xfered. If reading more than one | |
2780 | * sector, try reading one sector at a time. | |
2781 | */ | |
67716327 AH |
2782 | if (brq->data.blocks > 1) { |
2783 | /* Redo read one sector at a time */ | |
6606110d | 2784 | pr_warn("%s: retrying using single block read\n", |
acd8dbd6 | 2785 | old_req->rq_disk->disk_name); |
67716327 AH |
2786 | disable_multi = 1; |
2787 | break; | |
2788 | } | |
d78d4a8a PF |
2789 | /* |
2790 | * After an error, we redo I/O one sector at a | |
2791 | * time, so we only reach here after trying to | |
2792 | * read a single sector. | |
2793 | */ | |
2a842aca | 2794 | req_pending = blk_end_request(old_req, BLK_STS_IOERR, |
0e65f10c LW |
2795 | brq->data.blksz); |
2796 | if (!req_pending) { | |
304419d8 | 2797 | mq->qcnt--; |
8ddfe07e | 2798 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
db435505 LW |
2799 | return; |
2800 | } | |
d78d4a8a | 2801 | break; |
a8ad82cc | 2802 | case MMC_BLK_NOMEDIUM: |
6d3898a6 | 2803 | /* Do not accept bytes_xfered. Error out the request */ |
cdf8a6fb | 2804 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
8ddfe07e | 2805 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
db435505 | 2806 | return; |
2220eedf | 2807 | default: |
6d3898a6 | 2808 | /* Do not accept bytes_xfered. Error out the request */ |
2220eedf | 2809 | pr_err("%s: Unhandled return value (%d)", |
acd8dbd6 | 2810 | old_req->rq_disk->disk_name, status); |
cdf8a6fb | 2811 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
8ddfe07e | 2812 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
db435505 | 2813 | return; |
4c2b8f26 RKAL |
2814 | } |
2815 | ||
0e65f10c | 2816 | if (req_pending) { |
03d640ae LW |
2817 | /* |
2818 | * In case of a incomplete request | |
2819 | * prepare it again and resend. | |
2820 | */ | |
2821 | mmc_blk_rw_rq_prep(mq_rq, card, | |
2822 | disable_multi, mq); | |
c3399ef5 | 2823 | mmc_start_areq(card->host, |
74f5ba35 | 2824 | &mq_rq->areq, NULL); |
b8360a49 | 2825 | mq_rq->brq.retune_retry_done = retune_retry_done; |
ee8a43a5 | 2826 | } |
0e65f10c | 2827 | } while (req_pending); |
cdf8a6fb | 2828 | |
304419d8 | 2829 | mq->qcnt--; |
1da177e4 LT |
2830 | } |
2831 | ||
df061588 | 2832 | void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) |
bd788c96 | 2833 | { |
1a258db6 | 2834 | int ret; |
7db3028e | 2835 | struct mmc_blk_data *md = mq->blkdata; |
1a258db6 AW |
2836 | struct mmc_card *card = md->queue.card; |
2837 | ||
cdf8a6fb | 2838 | if (req && !mq->qcnt) |
ee8a43a5 | 2839 | /* claim host only for the first request */ |
6c0cedd1 | 2840 | mmc_get_card(card, NULL); |
ee8a43a5 | 2841 | |
1f797edc | 2842 | ret = mmc_blk_part_switch(card, md->part_type); |
371a689f | 2843 | if (ret) { |
0d7d85ca | 2844 | if (req) { |
2a842aca | 2845 | blk_end_request_all(req, BLK_STS_IOERR); |
0d7d85ca | 2846 | } |
371a689f AW |
2847 | goto out; |
2848 | } | |
1a258db6 | 2849 | |
614f0388 LW |
2850 | if (req) { |
2851 | switch (req_op(req)) { | |
2852 | case REQ_OP_DRV_IN: | |
2853 | case REQ_OP_DRV_OUT: | |
2854 | /* | |
2855 | * Complete ongoing async transfer before issuing | |
2856 | * ioctl()s | |
2857 | */ | |
2858 | if (mq->qcnt) | |
2859 | mmc_blk_issue_rw_rq(mq, NULL); | |
02166a01 | 2860 | mmc_blk_issue_drv_op(mq, req); |
614f0388 LW |
2861 | break; |
2862 | case REQ_OP_DISCARD: | |
2863 | /* | |
2864 | * Complete ongoing async transfer before issuing | |
2865 | * discard. | |
2866 | */ | |
2867 | if (mq->qcnt) | |
2868 | mmc_blk_issue_rw_rq(mq, NULL); | |
2869 | mmc_blk_issue_discard_rq(mq, req); | |
2870 | break; | |
2871 | case REQ_OP_SECURE_ERASE: | |
2872 | /* | |
2873 | * Complete ongoing async transfer before issuing | |
2874 | * secure erase. | |
2875 | */ | |
2876 | if (mq->qcnt) | |
2877 | mmc_blk_issue_rw_rq(mq, NULL); | |
2878 | mmc_blk_issue_secdiscard_rq(mq, req); | |
2879 | break; | |
2880 | case REQ_OP_FLUSH: | |
2881 | /* | |
2882 | * Complete ongoing async transfer before issuing | |
2883 | * flush. | |
2884 | */ | |
2885 | if (mq->qcnt) | |
2886 | mmc_blk_issue_rw_rq(mq, NULL); | |
2887 | mmc_blk_issue_flush(mq, req); | |
2888 | break; | |
2889 | default: | |
2890 | /* Normal request, just issue it */ | |
2891 | mmc_blk_issue_rw_rq(mq, req); | |
2892 | card->host->context_info.is_waiting_last_req = false; | |
2893 | break; | |
7322238f | 2894 | } |
49804548 | 2895 | } else { |
614f0388 LW |
2896 | /* No request, flushing the pipeline with NULL */ |
2897 | mmc_blk_issue_rw_rq(mq, NULL); | |
2602b740 | 2898 | card->host->context_info.is_waiting_last_req = false; |
49804548 | 2899 | } |
1a258db6 | 2900 | |
371a689f | 2901 | out: |
cdf8a6fb | 2902 | if (!mq->qcnt) |
6c0cedd1 | 2903 | mmc_put_card(card, NULL); |
bd788c96 | 2904 | } |
1da177e4 | 2905 | |
a6f6c96b RK |
2906 | static inline int mmc_blk_readonly(struct mmc_card *card) |
2907 | { | |
2908 | return mmc_card_readonly(card) || | |
2909 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); | |
2910 | } | |
2911 | ||
371a689f AW |
2912 | static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, |
2913 | struct device *parent, | |
2914 | sector_t size, | |
2915 | bool default_ro, | |
add710ea JR |
2916 | const char *subname, |
2917 | int area_type) | |
1da177e4 LT |
2918 | { |
2919 | struct mmc_blk_data *md; | |
2920 | int devidx, ret; | |
2921 | ||
a04848c7 | 2922 | devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL); |
e7b42769 SL |
2923 | if (devidx < 0) { |
2924 | /* | |
2925 | * We get -ENOSPC because there are no more any available | |
2926 | * devidx. The reason may be that, either userspace haven't yet | |
2927 | * unmounted the partitions, which postpones mmc_blk_release() | |
2928 | * from being called, or the device has more partitions than | |
2929 | * what we support. | |
2930 | */ | |
2931 | if (devidx == -ENOSPC) | |
2932 | dev_err(mmc_dev(card->host), | |
2933 | "no more device IDs available\n"); | |
2934 | ||
a04848c7 | 2935 | return ERR_PTR(devidx); |
e7b42769 | 2936 | } |
1da177e4 | 2937 | |
dd00cc48 | 2938 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); |
a6f6c96b RK |
2939 | if (!md) { |
2940 | ret = -ENOMEM; | |
2941 | goto out; | |
2942 | } | |
1da177e4 | 2943 | |
add710ea JR |
2944 | md->area_type = area_type; |
2945 | ||
a6f6c96b RK |
2946 | /* |
2947 | * Set the read-only status based on the supported commands | |
2948 | * and the write protect switch. | |
2949 | */ | |
2950 | md->read_only = mmc_blk_readonly(card); | |
1da177e4 | 2951 | |
5e71b7a6 | 2952 | md->disk = alloc_disk(perdev_minors); |
a6f6c96b RK |
2953 | if (md->disk == NULL) { |
2954 | ret = -ENOMEM; | |
2955 | goto err_kfree; | |
2956 | } | |
1da177e4 | 2957 | |
a6f6c96b | 2958 | spin_lock_init(&md->lock); |
371a689f | 2959 | INIT_LIST_HEAD(&md->part); |
97548575 | 2960 | INIT_LIST_HEAD(&md->rpmbs); |
a6f6c96b | 2961 | md->usage = 1; |
1da177e4 | 2962 | |
d09408ad | 2963 | ret = mmc_init_queue(&md->queue, card, &md->lock, subname); |
a6f6c96b RK |
2964 | if (ret) |
2965 | goto err_putdisk; | |
1da177e4 | 2966 | |
7db3028e | 2967 | md->queue.blkdata = md; |
d2b18394 | 2968 | |
41e3efd0 AH |
2969 | /* |
2970 | * Keep an extra reference to the queue so that we can shutdown the | |
2971 | * queue (i.e. call blk_cleanup_queue()) while there are still | |
2972 | * references to the 'md'. The corresponding blk_put_queue() is in | |
2973 | * mmc_blk_put(). | |
2974 | */ | |
2975 | if (!blk_get_queue(md->queue.queue)) { | |
2976 | mmc_cleanup_queue(&md->queue); | |
2977 | goto err_putdisk; | |
2978 | } | |
2979 | ||
fe6b4c88 | 2980 | md->disk->major = MMC_BLOCK_MAJOR; |
5e71b7a6 | 2981 | md->disk->first_minor = devidx * perdev_minors; |
a6f6c96b RK |
2982 | md->disk->fops = &mmc_bdops; |
2983 | md->disk->private_data = md; | |
2984 | md->disk->queue = md->queue.queue; | |
307d8e6f | 2985 | md->parent = parent; |
371a689f | 2986 | set_disk_ro(md->disk, md->read_only || default_ro); |
382c55f8 | 2987 | md->disk->flags = GENHD_FL_EXT_DEVT; |
f5b4d71f | 2988 | if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT)) |
53d8f974 | 2989 | md->disk->flags |= GENHD_FL_NO_PART_SCAN; |
a6f6c96b RK |
2990 | |
2991 | /* | |
2992 | * As discussed on lkml, GENHD_FL_REMOVABLE should: | |
2993 | * | |
2994 | * - be set for removable media with permanent block devices | |
2995 | * - be unset for removable block devices with permanent media | |
2996 | * | |
2997 | * Since MMC block devices clearly fall under the second | |
2998 | * case, we do not set GENHD_FL_REMOVABLE. Userspace | |
2999 | * should use the block device creation/destruction hotplug | |
3000 | * messages to tell when the card is present. | |
3001 | */ | |
3002 | ||
f06c9153 | 3003 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
9aaf3437 | 3004 | "mmcblk%u%s", card->host->index, subname ? subname : ""); |
a6f6c96b | 3005 | |
a5075eb9 SD |
3006 | if (mmc_card_mmc(card)) |
3007 | blk_queue_logical_block_size(md->queue.queue, | |
3008 | card->ext_csd.data_sector_size); | |
3009 | else | |
3010 | blk_queue_logical_block_size(md->queue.queue, 512); | |
3011 | ||
371a689f | 3012 | set_capacity(md->disk, size); |
d0c97cfb | 3013 | |
f0d89972 | 3014 | if (mmc_host_cmd23(card->host)) { |
0ed50abb DG |
3015 | if ((mmc_card_mmc(card) && |
3016 | card->csd.mmca_vsn >= CSD_SPEC_VER_3) || | |
f0d89972 AW |
3017 | (mmc_card_sd(card) && |
3018 | card->scr.cmds & SD_SCR_CMD23_SUPPORT)) | |
3019 | md->flags |= MMC_BLK_CMD23; | |
3020 | } | |
d0c97cfb AW |
3021 | |
3022 | if (mmc_card_mmc(card) && | |
3023 | md->flags & MMC_BLK_CMD23 && | |
3024 | ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || | |
3025 | card->ext_csd.rel_sectors)) { | |
3026 | md->flags |= MMC_BLK_REL_WR; | |
e9d5c746 | 3027 | blk_queue_write_cache(md->queue.queue, true, true); |
d0c97cfb AW |
3028 | } |
3029 | ||
371a689f AW |
3030 | return md; |
3031 | ||
3032 | err_putdisk: | |
3033 | put_disk(md->disk); | |
3034 | err_kfree: | |
3035 | kfree(md); | |
3036 | out: | |
a04848c7 | 3037 | ida_simple_remove(&mmc_blk_ida, devidx); |
371a689f AW |
3038 | return ERR_PTR(ret); |
3039 | } | |
3040 | ||
3041 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) | |
3042 | { | |
3043 | sector_t size; | |
a6f6c96b | 3044 | |
85a18ad9 PO |
3045 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { |
3046 | /* | |
3047 | * The EXT_CSD sector count is in number or 512 byte | |
3048 | * sectors. | |
3049 | */ | |
371a689f | 3050 | size = card->ext_csd.sectors; |
85a18ad9 PO |
3051 | } else { |
3052 | /* | |
3053 | * The CSD capacity field is in units of read_blkbits. | |
3054 | * set_capacity takes units of 512 bytes. | |
3055 | */ | |
087de9ed KM |
3056 | size = (typeof(sector_t))card->csd.capacity |
3057 | << (card->csd.read_blkbits - 9); | |
85a18ad9 | 3058 | } |
371a689f | 3059 | |
7a30f2af | 3060 | return mmc_blk_alloc_req(card, &card->dev, size, false, NULL, |
add710ea | 3061 | MMC_BLK_DATA_AREA_MAIN); |
371a689f | 3062 | } |
a6f6c96b | 3063 | |
371a689f AW |
3064 | static int mmc_blk_alloc_part(struct mmc_card *card, |
3065 | struct mmc_blk_data *md, | |
3066 | unsigned int part_type, | |
3067 | sector_t size, | |
3068 | bool default_ro, | |
add710ea JR |
3069 | const char *subname, |
3070 | int area_type) | |
371a689f AW |
3071 | { |
3072 | char cap_str[10]; | |
3073 | struct mmc_blk_data *part_md; | |
3074 | ||
3075 | part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro, | |
add710ea | 3076 | subname, area_type); |
371a689f AW |
3077 | if (IS_ERR(part_md)) |
3078 | return PTR_ERR(part_md); | |
3079 | part_md->part_type = part_type; | |
3080 | list_add(&part_md->part, &md->part); | |
3081 | ||
b9f28d86 | 3082 | string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2, |
371a689f | 3083 | cap_str, sizeof(cap_str)); |
a3c76eb9 | 3084 | pr_info("%s: %s %s partition %u %s\n", |
371a689f AW |
3085 | part_md->disk->disk_name, mmc_card_id(card), |
3086 | mmc_card_name(card), part_md->part_type, cap_str); | |
3087 | return 0; | |
3088 | } | |
3089 | ||
97548575 LW |
3090 | /** |
3091 | * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev | |
3092 | * @filp: the character device file | |
3093 | * @cmd: the ioctl() command | |
3094 | * @arg: the argument from userspace | |
3095 | * | |
3096 | * This will essentially just redirect the ioctl()s coming in over to | |
3097 | * the main block device spawning the RPMB character device. | |
3098 | */ | |
3099 | static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd, | |
3100 | unsigned long arg) | |
3101 | { | |
3102 | struct mmc_rpmb_data *rpmb = filp->private_data; | |
3103 | int ret; | |
3104 | ||
3105 | switch (cmd) { | |
3106 | case MMC_IOC_CMD: | |
3107 | ret = mmc_blk_ioctl_cmd(rpmb->md, | |
3108 | (struct mmc_ioc_cmd __user *)arg, | |
3109 | rpmb); | |
3110 | break; | |
3111 | case MMC_IOC_MULTI_CMD: | |
3112 | ret = mmc_blk_ioctl_multi_cmd(rpmb->md, | |
3113 | (struct mmc_ioc_multi_cmd __user *)arg, | |
3114 | rpmb); | |
3115 | break; | |
3116 | default: | |
3117 | ret = -EINVAL; | |
3118 | break; | |
3119 | } | |
3120 | ||
3121 | return 0; | |
3122 | } | |
3123 | ||
3124 | #ifdef CONFIG_COMPAT | |
3125 | static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd, | |
3126 | unsigned long arg) | |
3127 | { | |
3128 | return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); | |
3129 | } | |
3130 | #endif | |
3131 | ||
3132 | static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp) | |
3133 | { | |
3134 | struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev, | |
3135 | struct mmc_rpmb_data, chrdev); | |
3136 | ||
3137 | get_device(&rpmb->dev); | |
3138 | filp->private_data = rpmb; | |
1c87f735 | 3139 | mmc_blk_get(rpmb->md->disk); |
97548575 LW |
3140 | |
3141 | return nonseekable_open(inode, filp); | |
3142 | } | |
3143 | ||
3144 | static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp) | |
3145 | { | |
3146 | struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev, | |
3147 | struct mmc_rpmb_data, chrdev); | |
3148 | ||
3149 | put_device(&rpmb->dev); | |
1c87f735 | 3150 | mmc_blk_put(rpmb->md); |
97548575 LW |
3151 | |
3152 | return 0; | |
3153 | } | |
3154 | ||
3155 | static const struct file_operations mmc_rpmb_fileops = { | |
3156 | .release = mmc_rpmb_chrdev_release, | |
3157 | .open = mmc_rpmb_chrdev_open, | |
3158 | .owner = THIS_MODULE, | |
3159 | .llseek = no_llseek, | |
3160 | .unlocked_ioctl = mmc_rpmb_ioctl, | |
3161 | #ifdef CONFIG_COMPAT | |
3162 | .compat_ioctl = mmc_rpmb_ioctl_compat, | |
3163 | #endif | |
3164 | }; | |
3165 | ||
1c87f735 LW |
3166 | static void mmc_blk_rpmb_device_release(struct device *dev) |
3167 | { | |
3168 | struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev); | |
3169 | ||
3170 | ida_simple_remove(&mmc_rpmb_ida, rpmb->id); | |
3171 | kfree(rpmb); | |
3172 | } | |
97548575 LW |
3173 | |
3174 | static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, | |
3175 | struct mmc_blk_data *md, | |
3176 | unsigned int part_index, | |
3177 | sector_t size, | |
3178 | const char *subname) | |
3179 | { | |
3180 | int devidx, ret; | |
3181 | char rpmb_name[DISK_NAME_LEN]; | |
3182 | char cap_str[10]; | |
3183 | struct mmc_rpmb_data *rpmb; | |
3184 | ||
3185 | /* This creates the minor number for the RPMB char device */ | |
3186 | devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL); | |
3187 | if (devidx < 0) | |
3188 | return devidx; | |
3189 | ||
3190 | rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL); | |
1c87f735 LW |
3191 | if (!rpmb) { |
3192 | ida_simple_remove(&mmc_rpmb_ida, devidx); | |
97548575 | 3193 | return -ENOMEM; |
1c87f735 | 3194 | } |
97548575 LW |
3195 | |
3196 | snprintf(rpmb_name, sizeof(rpmb_name), | |
3197 | "mmcblk%u%s", card->host->index, subname ? subname : ""); | |
3198 | ||
3199 | rpmb->id = devidx; | |
3200 | rpmb->part_index = part_index; | |
3201 | rpmb->dev.init_name = rpmb_name; | |
3202 | rpmb->dev.bus = &mmc_rpmb_bus_type; | |
3203 | rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id); | |
3204 | rpmb->dev.parent = &card->dev; | |
1c87f735 | 3205 | rpmb->dev.release = mmc_blk_rpmb_device_release; |
97548575 LW |
3206 | device_initialize(&rpmb->dev); |
3207 | dev_set_drvdata(&rpmb->dev, rpmb); | |
3208 | rpmb->md = md; | |
3209 | ||
3210 | cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops); | |
3211 | rpmb->chrdev.owner = THIS_MODULE; | |
3212 | ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev); | |
3213 | if (ret) { | |
3214 | pr_err("%s: could not add character device\n", rpmb_name); | |
1c87f735 | 3215 | goto out_put_device; |
97548575 LW |
3216 | } |
3217 | ||
3218 | list_add(&rpmb->node, &md->rpmbs); | |
3219 | ||
3220 | string_get_size((u64)size, 512, STRING_UNITS_2, | |
3221 | cap_str, sizeof(cap_str)); | |
3222 | ||
3223 | pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n", | |
3224 | rpmb_name, mmc_card_id(card), | |
3225 | mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str, | |
3226 | MAJOR(mmc_rpmb_devt), rpmb->id); | |
3227 | ||
3228 | return 0; | |
3229 | ||
1c87f735 LW |
3230 | out_put_device: |
3231 | put_device(&rpmb->dev); | |
97548575 LW |
3232 | return ret; |
3233 | } | |
3234 | ||
3235 | static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb) | |
1c87f735 | 3236 | |
97548575 LW |
3237 | { |
3238 | cdev_device_del(&rpmb->chrdev, &rpmb->dev); | |
1c87f735 | 3239 | put_device(&rpmb->dev); |
97548575 LW |
3240 | } |
3241 | ||
e0c368d5 NJ |
3242 | /* MMC Physical partitions consist of two boot partitions and |
3243 | * up to four general purpose partitions. | |
3244 | * For each partition enabled in EXT_CSD a block device will be allocatedi | |
3245 | * to provide access to the partition. | |
3246 | */ | |
3247 | ||
371a689f AW |
3248 | static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) |
3249 | { | |
97548575 | 3250 | int idx, ret; |
371a689f AW |
3251 | |
3252 | if (!mmc_card_mmc(card)) | |
3253 | return 0; | |
3254 | ||
e0c368d5 | 3255 | for (idx = 0; idx < card->nr_parts; idx++) { |
97548575 LW |
3256 | if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) { |
3257 | /* | |
3258 | * RPMB partitions does not provide block access, they | |
3259 | * are only accessed using ioctl():s. Thus create | |
3260 | * special RPMB block devices that do not have a | |
3261 | * backing block queue for these. | |
3262 | */ | |
3263 | ret = mmc_blk_alloc_rpmb_part(card, md, | |
3264 | card->part[idx].part_cfg, | |
3265 | card->part[idx].size >> 9, | |
3266 | card->part[idx].name); | |
3267 | if (ret) | |
3268 | return ret; | |
3269 | } else if (card->part[idx].size) { | |
e0c368d5 NJ |
3270 | ret = mmc_blk_alloc_part(card, md, |
3271 | card->part[idx].part_cfg, | |
3272 | card->part[idx].size >> 9, | |
3273 | card->part[idx].force_ro, | |
add710ea JR |
3274 | card->part[idx].name, |
3275 | card->part[idx].area_type); | |
e0c368d5 NJ |
3276 | if (ret) |
3277 | return ret; | |
3278 | } | |
371a689f AW |
3279 | } |
3280 | ||
97548575 | 3281 | return 0; |
1da177e4 LT |
3282 | } |
3283 | ||
371a689f AW |
3284 | static void mmc_blk_remove_req(struct mmc_blk_data *md) |
3285 | { | |
add710ea JR |
3286 | struct mmc_card *card; |
3287 | ||
371a689f | 3288 | if (md) { |
fdfa20c1 PT |
3289 | /* |
3290 | * Flush remaining requests and free queues. It | |
3291 | * is freeing the queue that stops new requests | |
3292 | * from being accepted. | |
3293 | */ | |
8efb83a2 | 3294 | card = md->queue.card; |
fdfa20c1 | 3295 | mmc_cleanup_queue(&md->queue); |
371a689f AW |
3296 | if (md->disk->flags & GENHD_FL_UP) { |
3297 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); | |
add710ea JR |
3298 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && |
3299 | card->ext_csd.boot_ro_lockable) | |
3300 | device_remove_file(disk_to_dev(md->disk), | |
3301 | &md->power_ro_lock); | |
371a689f | 3302 | |
371a689f AW |
3303 | del_gendisk(md->disk); |
3304 | } | |
371a689f AW |
3305 | mmc_blk_put(md); |
3306 | } | |
3307 | } | |
3308 | ||
3309 | static void mmc_blk_remove_parts(struct mmc_card *card, | |
3310 | struct mmc_blk_data *md) | |
3311 | { | |
3312 | struct list_head *pos, *q; | |
3313 | struct mmc_blk_data *part_md; | |
97548575 | 3314 | struct mmc_rpmb_data *rpmb; |
371a689f | 3315 | |
97548575 LW |
3316 | /* Remove RPMB partitions */ |
3317 | list_for_each_safe(pos, q, &md->rpmbs) { | |
3318 | rpmb = list_entry(pos, struct mmc_rpmb_data, node); | |
3319 | list_del(pos); | |
3320 | mmc_blk_remove_rpmb_part(rpmb); | |
3321 | } | |
3322 | /* Remove block partitions */ | |
371a689f AW |
3323 | list_for_each_safe(pos, q, &md->part) { |
3324 | part_md = list_entry(pos, struct mmc_blk_data, part); | |
3325 | list_del(pos); | |
3326 | mmc_blk_remove_req(part_md); | |
3327 | } | |
3328 | } | |
3329 | ||
3330 | static int mmc_add_disk(struct mmc_blk_data *md) | |
3331 | { | |
3332 | int ret; | |
add710ea | 3333 | struct mmc_card *card = md->queue.card; |
371a689f | 3334 | |
307d8e6f | 3335 | device_add_disk(md->parent, md->disk); |
371a689f AW |
3336 | md->force_ro.show = force_ro_show; |
3337 | md->force_ro.store = force_ro_store; | |
641c3187 | 3338 | sysfs_attr_init(&md->force_ro.attr); |
371a689f AW |
3339 | md->force_ro.attr.name = "force_ro"; |
3340 | md->force_ro.attr.mode = S_IRUGO | S_IWUSR; | |
3341 | ret = device_create_file(disk_to_dev(md->disk), &md->force_ro); | |
3342 | if (ret) | |
add710ea JR |
3343 | goto force_ro_fail; |
3344 | ||
3345 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && | |
3346 | card->ext_csd.boot_ro_lockable) { | |
88187398 | 3347 | umode_t mode; |
add710ea JR |
3348 | |
3349 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS) | |
3350 | mode = S_IRUGO; | |
3351 | else | |
3352 | mode = S_IRUGO | S_IWUSR; | |
3353 | ||
3354 | md->power_ro_lock.show = power_ro_lock_show; | |
3355 | md->power_ro_lock.store = power_ro_lock_store; | |
00d9ac08 | 3356 | sysfs_attr_init(&md->power_ro_lock.attr); |
add710ea JR |
3357 | md->power_ro_lock.attr.mode = mode; |
3358 | md->power_ro_lock.attr.name = | |
3359 | "ro_lock_until_next_power_on"; | |
3360 | ret = device_create_file(disk_to_dev(md->disk), | |
3361 | &md->power_ro_lock); | |
3362 | if (ret) | |
3363 | goto power_ro_lock_fail; | |
3364 | } | |
3365 | return ret; | |
3366 | ||
3367 | power_ro_lock_fail: | |
3368 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); | |
3369 | force_ro_fail: | |
3370 | del_gendisk(md->disk); | |
371a689f AW |
3371 | |
3372 | return ret; | |
3373 | } | |
3374 | ||
627c3ccf LW |
3375 | #ifdef CONFIG_DEBUG_FS |
3376 | ||
3377 | static int mmc_dbg_card_status_get(void *data, u64 *val) | |
3378 | { | |
3379 | struct mmc_card *card = data; | |
3380 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); | |
3381 | struct mmc_queue *mq = &md->queue; | |
3382 | struct request *req; | |
3383 | int ret; | |
3384 | ||
3385 | /* Ask the block layer about the card status */ | |
3386 | req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM); | |
fb8e456e AH |
3387 | if (IS_ERR(req)) |
3388 | return PTR_ERR(req); | |
627c3ccf LW |
3389 | req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS; |
3390 | blk_execute_rq(mq->queue, NULL, req, 0); | |
3391 | ret = req_to_mmc_queue_req(req)->drv_op_result; | |
3392 | if (ret >= 0) { | |
3393 | *val = ret; | |
3394 | ret = 0; | |
3395 | } | |
34c089e8 | 3396 | blk_put_request(req); |
627c3ccf LW |
3397 | |
3398 | return ret; | |
3399 | } | |
3400 | DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get, | |
3401 | NULL, "%08llx\n"); | |
3402 | ||
3403 | /* That is two digits * 512 + 1 for newline */ | |
3404 | #define EXT_CSD_STR_LEN 1025 | |
3405 | ||
3406 | static int mmc_ext_csd_open(struct inode *inode, struct file *filp) | |
3407 | { | |
3408 | struct mmc_card *card = inode->i_private; | |
3409 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); | |
3410 | struct mmc_queue *mq = &md->queue; | |
3411 | struct request *req; | |
3412 | char *buf; | |
3413 | ssize_t n = 0; | |
3414 | u8 *ext_csd; | |
3415 | int err, i; | |
3416 | ||
3417 | buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL); | |
3418 | if (!buf) | |
3419 | return -ENOMEM; | |
3420 | ||
3421 | /* Ask the block layer for the EXT CSD */ | |
3422 | req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM); | |
fb8e456e AH |
3423 | if (IS_ERR(req)) { |
3424 | err = PTR_ERR(req); | |
3425 | goto out_free; | |
3426 | } | |
627c3ccf LW |
3427 | req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD; |
3428 | req_to_mmc_queue_req(req)->drv_op_data = &ext_csd; | |
3429 | blk_execute_rq(mq->queue, NULL, req, 0); | |
3430 | err = req_to_mmc_queue_req(req)->drv_op_result; | |
34c089e8 | 3431 | blk_put_request(req); |
627c3ccf LW |
3432 | if (err) { |
3433 | pr_err("FAILED %d\n", err); | |
3434 | goto out_free; | |
3435 | } | |
3436 | ||
3437 | for (i = 0; i < 512; i++) | |
3438 | n += sprintf(buf + n, "%02x", ext_csd[i]); | |
3439 | n += sprintf(buf + n, "\n"); | |
3440 | ||
3441 | if (n != EXT_CSD_STR_LEN) { | |
3442 | err = -EINVAL; | |
3443 | goto out_free; | |
3444 | } | |
3445 | ||
3446 | filp->private_data = buf; | |
3447 | kfree(ext_csd); | |
3448 | return 0; | |
3449 | ||
3450 | out_free: | |
3451 | kfree(buf); | |
3452 | return err; | |
3453 | } | |
3454 | ||
3455 | static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf, | |
3456 | size_t cnt, loff_t *ppos) | |
3457 | { | |
3458 | char *buf = filp->private_data; | |
3459 | ||
3460 | return simple_read_from_buffer(ubuf, cnt, ppos, | |
3461 | buf, EXT_CSD_STR_LEN); | |
3462 | } | |
3463 | ||
3464 | static int mmc_ext_csd_release(struct inode *inode, struct file *file) | |
3465 | { | |
3466 | kfree(file->private_data); | |
3467 | return 0; | |
3468 | } | |
3469 | ||
3470 | static const struct file_operations mmc_dbg_ext_csd_fops = { | |
3471 | .open = mmc_ext_csd_open, | |
3472 | .read = mmc_ext_csd_read, | |
3473 | .release = mmc_ext_csd_release, | |
3474 | .llseek = default_llseek, | |
3475 | }; | |
3476 | ||
f9f0da98 | 3477 | static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md) |
627c3ccf LW |
3478 | { |
3479 | struct dentry *root; | |
3480 | ||
3481 | if (!card->debugfs_root) | |
3482 | return 0; | |
3483 | ||
3484 | root = card->debugfs_root; | |
3485 | ||
3486 | if (mmc_card_mmc(card) || mmc_card_sd(card)) { | |
f9f0da98 AH |
3487 | md->status_dentry = |
3488 | debugfs_create_file("status", S_IRUSR, root, card, | |
3489 | &mmc_dbg_card_status_fops); | |
3490 | if (!md->status_dentry) | |
627c3ccf LW |
3491 | return -EIO; |
3492 | } | |
3493 | ||
3494 | if (mmc_card_mmc(card)) { | |
f9f0da98 AH |
3495 | md->ext_csd_dentry = |
3496 | debugfs_create_file("ext_csd", S_IRUSR, root, card, | |
3497 | &mmc_dbg_ext_csd_fops); | |
3498 | if (!md->ext_csd_dentry) | |
627c3ccf LW |
3499 | return -EIO; |
3500 | } | |
3501 | ||
3502 | return 0; | |
3503 | } | |
3504 | ||
f9f0da98 AH |
3505 | static void mmc_blk_remove_debugfs(struct mmc_card *card, |
3506 | struct mmc_blk_data *md) | |
3507 | { | |
3508 | if (!card->debugfs_root) | |
3509 | return; | |
3510 | ||
3511 | if (!IS_ERR_OR_NULL(md->status_dentry)) { | |
3512 | debugfs_remove(md->status_dentry); | |
3513 | md->status_dentry = NULL; | |
3514 | } | |
3515 | ||
3516 | if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) { | |
3517 | debugfs_remove(md->ext_csd_dentry); | |
3518 | md->ext_csd_dentry = NULL; | |
3519 | } | |
3520 | } | |
627c3ccf LW |
3521 | |
3522 | #else | |
3523 | ||
f9f0da98 | 3524 | static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md) |
627c3ccf LW |
3525 | { |
3526 | return 0; | |
3527 | } | |
3528 | ||
f9f0da98 AH |
3529 | static void mmc_blk_remove_debugfs(struct mmc_card *card, |
3530 | struct mmc_blk_data *md) | |
3531 | { | |
3532 | } | |
3533 | ||
627c3ccf LW |
3534 | #endif /* CONFIG_DEBUG_FS */ |
3535 | ||
96541bac | 3536 | static int mmc_blk_probe(struct mmc_card *card) |
1da177e4 | 3537 | { |
371a689f | 3538 | struct mmc_blk_data *md, *part_md; |
a7bbb573 PO |
3539 | char cap_str[10]; |
3540 | ||
912490db PO |
3541 | /* |
3542 | * Check that the card supports the command class(es) we need. | |
3543 | */ | |
3544 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) | |
1da177e4 LT |
3545 | return -ENODEV; |
3546 | ||
8c7cdbf9 | 3547 | mmc_fixup_device(card, mmc_blk_fixups); |
5204d00f | 3548 | |
1da177e4 | 3549 | md = mmc_blk_alloc(card); |
304419d8 | 3550 | if (IS_ERR(md)) |
1da177e4 LT |
3551 | return PTR_ERR(md); |
3552 | ||
b9f28d86 | 3553 | string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2, |
a7bbb573 | 3554 | cap_str, sizeof(cap_str)); |
a3c76eb9 | 3555 | pr_info("%s: %s %s %s %s\n", |
1da177e4 | 3556 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), |
a7bbb573 | 3557 | cap_str, md->read_only ? "(ro)" : ""); |
1da177e4 | 3558 | |
371a689f AW |
3559 | if (mmc_blk_alloc_parts(card, md)) |
3560 | goto out; | |
3561 | ||
96541bac | 3562 | dev_set_drvdata(&card->dev, md); |
6f60c222 | 3563 | |
371a689f AW |
3564 | if (mmc_add_disk(md)) |
3565 | goto out; | |
3566 | ||
3567 | list_for_each_entry(part_md, &md->part, part) { | |
3568 | if (mmc_add_disk(part_md)) | |
3569 | goto out; | |
3570 | } | |
e94cfef6 | 3571 | |
627c3ccf | 3572 | /* Add two debugfs entries */ |
f9f0da98 | 3573 | mmc_blk_add_debugfs(card, md); |
627c3ccf | 3574 | |
e94cfef6 UH |
3575 | pm_runtime_set_autosuspend_delay(&card->dev, 3000); |
3576 | pm_runtime_use_autosuspend(&card->dev); | |
3577 | ||
3578 | /* | |
3579 | * Don't enable runtime PM for SD-combo cards here. Leave that | |
3580 | * decision to be taken during the SDIO init sequence instead. | |
3581 | */ | |
3582 | if (card->type != MMC_TYPE_SD_COMBO) { | |
3583 | pm_runtime_set_active(&card->dev); | |
3584 | pm_runtime_enable(&card->dev); | |
3585 | } | |
3586 | ||
1da177e4 LT |
3587 | return 0; |
3588 | ||
3589 | out: | |
371a689f AW |
3590 | mmc_blk_remove_parts(card, md); |
3591 | mmc_blk_remove_req(md); | |
5865f287 | 3592 | return 0; |
1da177e4 LT |
3593 | } |
3594 | ||
96541bac | 3595 | static void mmc_blk_remove(struct mmc_card *card) |
1da177e4 | 3596 | { |
96541bac | 3597 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
1da177e4 | 3598 | |
f9f0da98 | 3599 | mmc_blk_remove_debugfs(card, md); |
371a689f | 3600 | mmc_blk_remove_parts(card, md); |
e94cfef6 | 3601 | pm_runtime_get_sync(&card->dev); |
ddd6fa7e | 3602 | mmc_claim_host(card->host); |
1f797edc | 3603 | mmc_blk_part_switch(card, md->part_type); |
ddd6fa7e | 3604 | mmc_release_host(card->host); |
e94cfef6 UH |
3605 | if (card->type != MMC_TYPE_SD_COMBO) |
3606 | pm_runtime_disable(&card->dev); | |
3607 | pm_runtime_put_noidle(&card->dev); | |
371a689f | 3608 | mmc_blk_remove_req(md); |
96541bac | 3609 | dev_set_drvdata(&card->dev, NULL); |
1da177e4 LT |
3610 | } |
3611 | ||
96541bac | 3612 | static int _mmc_blk_suspend(struct mmc_card *card) |
1da177e4 | 3613 | { |
371a689f | 3614 | struct mmc_blk_data *part_md; |
96541bac | 3615 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
1da177e4 LT |
3616 | |
3617 | if (md) { | |
3618 | mmc_queue_suspend(&md->queue); | |
371a689f AW |
3619 | list_for_each_entry(part_md, &md->part, part) { |
3620 | mmc_queue_suspend(&part_md->queue); | |
3621 | } | |
1da177e4 LT |
3622 | } |
3623 | return 0; | |
3624 | } | |
3625 | ||
96541bac | 3626 | static void mmc_blk_shutdown(struct mmc_card *card) |
76287748 | 3627 | { |
96541bac | 3628 | _mmc_blk_suspend(card); |
76287748 UH |
3629 | } |
3630 | ||
0967edc6 UH |
3631 | #ifdef CONFIG_PM_SLEEP |
3632 | static int mmc_blk_suspend(struct device *dev) | |
76287748 | 3633 | { |
96541bac UH |
3634 | struct mmc_card *card = mmc_dev_to_card(dev); |
3635 | ||
3636 | return _mmc_blk_suspend(card); | |
76287748 UH |
3637 | } |
3638 | ||
0967edc6 | 3639 | static int mmc_blk_resume(struct device *dev) |
1da177e4 | 3640 | { |
371a689f | 3641 | struct mmc_blk_data *part_md; |
fc95e30b | 3642 | struct mmc_blk_data *md = dev_get_drvdata(dev); |
1da177e4 LT |
3643 | |
3644 | if (md) { | |
371a689f AW |
3645 | /* |
3646 | * Resume involves the card going into idle state, | |
3647 | * so current partition is always the main one. | |
3648 | */ | |
3649 | md->part_curr = md->part_type; | |
1da177e4 | 3650 | mmc_queue_resume(&md->queue); |
371a689f AW |
3651 | list_for_each_entry(part_md, &md->part, part) { |
3652 | mmc_queue_resume(&part_md->queue); | |
3653 | } | |
1da177e4 LT |
3654 | } |
3655 | return 0; | |
3656 | } | |
1da177e4 LT |
3657 | #endif |
3658 | ||
0967edc6 UH |
3659 | static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume); |
3660 | ||
96541bac UH |
3661 | static struct mmc_driver mmc_driver = { |
3662 | .drv = { | |
3663 | .name = "mmcblk", | |
3664 | .pm = &mmc_blk_pm_ops, | |
3665 | }, | |
1da177e4 LT |
3666 | .probe = mmc_blk_probe, |
3667 | .remove = mmc_blk_remove, | |
76287748 | 3668 | .shutdown = mmc_blk_shutdown, |
1da177e4 LT |
3669 | }; |
3670 | ||
3671 | static int __init mmc_blk_init(void) | |
3672 | { | |
9d4e98e9 | 3673 | int res; |
1da177e4 | 3674 | |
97548575 LW |
3675 | res = bus_register(&mmc_rpmb_bus_type); |
3676 | if (res < 0) { | |
3677 | pr_err("mmcblk: could not register RPMB bus type\n"); | |
3678 | return res; | |
3679 | } | |
3680 | res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb"); | |
3681 | if (res < 0) { | |
3682 | pr_err("mmcblk: failed to allocate rpmb chrdev region\n"); | |
3683 | goto out_bus_unreg; | |
3684 | } | |
3685 | ||
5e71b7a6 OJ |
3686 | if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) |
3687 | pr_info("mmcblk: using %d minors per device\n", perdev_minors); | |
3688 | ||
a26eba61 | 3689 | max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors); |
5e71b7a6 | 3690 | |
fe6b4c88 PO |
3691 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
3692 | if (res) | |
97548575 | 3693 | goto out_chrdev_unreg; |
1da177e4 | 3694 | |
9d4e98e9 AM |
3695 | res = mmc_register_driver(&mmc_driver); |
3696 | if (res) | |
97548575 | 3697 | goto out_blkdev_unreg; |
1da177e4 | 3698 | |
9d4e98e9 | 3699 | return 0; |
97548575 LW |
3700 | |
3701 | out_blkdev_unreg: | |
9d4e98e9 | 3702 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
97548575 LW |
3703 | out_chrdev_unreg: |
3704 | unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES); | |
3705 | out_bus_unreg: | |
3706 | bus_unregister(&mmc_rpmb_bus_type); | |
1da177e4 LT |
3707 | return res; |
3708 | } | |
3709 | ||
3710 | static void __exit mmc_blk_exit(void) | |
3711 | { | |
3712 | mmc_unregister_driver(&mmc_driver); | |
fe6b4c88 | 3713 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
97548575 | 3714 | unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES); |
1da177e4 LT |
3715 | } |
3716 | ||
3717 | module_init(mmc_blk_init); | |
3718 | module_exit(mmc_blk_exit); | |
3719 | ||
3720 | MODULE_LICENSE("GPL"); | |
3721 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); | |
3722 |