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, |
c44d6cef | 926 | bool hw_busy_detect, struct request *req, bool *gen_err) |
c49433fb UH |
927 | { |
928 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); | |
929 | int err = 0; | |
930 | u32 status; | |
931 | ||
932 | do { | |
2185bc2c | 933 | err = __mmc_send_status(card, &status, 5); |
c49433fb UH |
934 | if (err) { |
935 | pr_err("%s: error %d requesting status\n", | |
936 | req->rq_disk->disk_name, err); | |
937 | return err; | |
938 | } | |
939 | ||
940 | if (status & R1_ERROR) { | |
941 | pr_err("%s: %s: error sending status cmd, status %#x\n", | |
942 | req->rq_disk->disk_name, __func__, status); | |
c44d6cef | 943 | *gen_err = true; |
c49433fb UH |
944 | } |
945 | ||
95a91298 UH |
946 | /* We may rely on the host hw to handle busy detection.*/ |
947 | if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && | |
948 | hw_busy_detect) | |
949 | break; | |
950 | ||
c49433fb UH |
951 | /* |
952 | * Timeout if the device never becomes ready for data and never | |
953 | * leaves the program state. | |
954 | */ | |
955 | if (time_after(jiffies, timeout)) { | |
956 | pr_err("%s: Card stuck in programming state! %s %s\n", | |
957 | mmc_hostname(card->host), | |
958 | req->rq_disk->disk_name, __func__); | |
959 | return -ETIMEDOUT; | |
960 | } | |
961 | ||
962 | /* | |
963 | * Some cards mishandle the status bits, | |
964 | * so make sure to check both the busy | |
965 | * indication and the card state. | |
966 | */ | |
967 | } while (!(status & R1_READY_FOR_DATA) || | |
968 | (R1_CURRENT_STATE(status) == R1_STATE_PRG)); | |
969 | ||
970 | return err; | |
971 | } | |
972 | ||
bb5cba40 | 973 | static int send_stop(struct mmc_card *card, unsigned int timeout_ms, |
c44d6cef | 974 | struct request *req, bool *gen_err, u32 *stop_status) |
bb5cba40 UH |
975 | { |
976 | struct mmc_host *host = card->host; | |
c7836d15 | 977 | struct mmc_command cmd = {}; |
bb5cba40 UH |
978 | int err; |
979 | bool use_r1b_resp = rq_data_dir(req) == WRITE; | |
980 | ||
981 | /* | |
982 | * Normally we use R1B responses for WRITE, but in cases where the host | |
983 | * has specified a max_busy_timeout we need to validate it. A failure | |
984 | * means we need to prevent the host from doing hw busy detection, which | |
985 | * is done by converting to a R1 response instead. | |
986 | */ | |
987 | if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) | |
988 | use_r1b_resp = false; | |
989 | ||
990 | cmd.opcode = MMC_STOP_TRANSMISSION; | |
991 | if (use_r1b_resp) { | |
992 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; | |
993 | cmd.busy_timeout = timeout_ms; | |
994 | } else { | |
995 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; | |
996 | } | |
997 | ||
998 | err = mmc_wait_for_cmd(host, &cmd, 5); | |
999 | if (err) | |
1000 | return err; | |
1001 | ||
1002 | *stop_status = cmd.resp[0]; | |
1003 | ||
1004 | /* No need to check card status in case of READ. */ | |
1005 | if (rq_data_dir(req) == READ) | |
1006 | return 0; | |
1007 | ||
1008 | if (!mmc_host_is_spi(host) && | |
1009 | (*stop_status & R1_ERROR)) { | |
1010 | pr_err("%s: %s: general error sending stop command, resp %#x\n", | |
1011 | req->rq_disk->disk_name, __func__, *stop_status); | |
c44d6cef | 1012 | *gen_err = true; |
bb5cba40 UH |
1013 | } |
1014 | ||
1015 | return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err); | |
1016 | } | |
1017 | ||
a8ad82cc | 1018 | #define ERR_NOMEDIUM 3 |
a01f3ccf RKAL |
1019 | #define ERR_RETRY 2 |
1020 | #define ERR_ABORT 1 | |
1021 | #define ERR_CONTINUE 0 | |
1022 | ||
1023 | static int mmc_blk_cmd_error(struct request *req, const char *name, int error, | |
1024 | bool status_valid, u32 status) | |
1025 | { | |
1026 | switch (error) { | |
1027 | case -EILSEQ: | |
1028 | /* response crc error, retry the r/w cmd */ | |
1029 | pr_err("%s: %s sending %s command, card status %#x\n", | |
1030 | req->rq_disk->disk_name, "response CRC error", | |
1031 | name, status); | |
1032 | return ERR_RETRY; | |
1033 | ||
1034 | case -ETIMEDOUT: | |
1035 | pr_err("%s: %s sending %s command, card status %#x\n", | |
1036 | req->rq_disk->disk_name, "timed out", name, status); | |
1037 | ||
1038 | /* If the status cmd initially failed, retry the r/w cmd */ | |
cc4d04be KS |
1039 | if (!status_valid) { |
1040 | pr_err("%s: status not valid, retrying timeout\n", | |
1041 | req->rq_disk->disk_name); | |
a01f3ccf | 1042 | return ERR_RETRY; |
cc4d04be | 1043 | } |
a01f3ccf RKAL |
1044 | |
1045 | /* | |
1046 | * If it was a r/w cmd crc error, or illegal command | |
1047 | * (eg, issued in wrong state) then retry - we should | |
1048 | * have corrected the state problem above. | |
1049 | */ | |
cc4d04be KS |
1050 | if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) { |
1051 | pr_err("%s: command error, retrying timeout\n", | |
1052 | req->rq_disk->disk_name); | |
a01f3ccf | 1053 | return ERR_RETRY; |
cc4d04be | 1054 | } |
a01f3ccf RKAL |
1055 | |
1056 | /* Otherwise abort the command */ | |
1057 | return ERR_ABORT; | |
1058 | ||
1059 | default: | |
1060 | /* We don't understand the error code the driver gave us */ | |
1061 | pr_err("%s: unknown error %d sending read/write command, card status %#x\n", | |
1062 | req->rq_disk->disk_name, error, status); | |
1063 | return ERR_ABORT; | |
1064 | } | |
1065 | } | |
1066 | ||
1067 | /* | |
1068 | * Initial r/w and stop cmd error recovery. | |
1069 | * We don't know whether the card received the r/w cmd or not, so try to | |
1070 | * restore things back to a sane state. Essentially, we do this as follows: | |
1071 | * - Obtain card status. If the first attempt to obtain card status fails, | |
1072 | * the status word will reflect the failed status cmd, not the failed | |
1073 | * r/w cmd. If we fail to obtain card status, it suggests we can no | |
1074 | * longer communicate with the card. | |
1075 | * - Check the card state. If the card received the cmd but there was a | |
1076 | * transient problem with the response, it might still be in a data transfer | |
1077 | * mode. Try to send it a stop command. If this fails, we can't recover. | |
1078 | * - If the r/w cmd failed due to a response CRC error, it was probably | |
1079 | * transient, so retry the cmd. | |
1080 | * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry. | |
1081 | * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or | |
1082 | * illegal cmd, retry. | |
1083 | * Otherwise we don't understand what happened, so abort. | |
1084 | */ | |
1085 | static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req, | |
2cc64587 | 1086 | struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err) |
a01f3ccf RKAL |
1087 | { |
1088 | bool prev_cmd_status_valid = true; | |
1089 | u32 status, stop_status = 0; | |
1090 | int err, retry; | |
1091 | ||
a8ad82cc SRT |
1092 | if (mmc_card_removed(card)) |
1093 | return ERR_NOMEDIUM; | |
1094 | ||
a01f3ccf RKAL |
1095 | /* |
1096 | * Try to get card status which indicates both the card state | |
1097 | * and why there was no response. If the first attempt fails, | |
1098 | * we can't be sure the returned status is for the r/w command. | |
1099 | */ | |
1100 | for (retry = 2; retry >= 0; retry--) { | |
2185bc2c | 1101 | err = __mmc_send_status(card, &status, 0); |
a01f3ccf RKAL |
1102 | if (!err) |
1103 | break; | |
1104 | ||
6f398ad2 AH |
1105 | /* Re-tune if needed */ |
1106 | mmc_retune_recheck(card->host); | |
1107 | ||
a01f3ccf RKAL |
1108 | prev_cmd_status_valid = false; |
1109 | pr_err("%s: error %d sending status command, %sing\n", | |
1110 | req->rq_disk->disk_name, err, retry ? "retry" : "abort"); | |
1111 | } | |
1112 | ||
1113 | /* We couldn't get a response from the card. Give up. */ | |
a8ad82cc SRT |
1114 | if (err) { |
1115 | /* Check if the card is removed */ | |
1116 | if (mmc_detect_card_removed(card->host)) | |
1117 | return ERR_NOMEDIUM; | |
a01f3ccf | 1118 | return ERR_ABORT; |
a8ad82cc | 1119 | } |
a01f3ccf | 1120 | |
67716327 AH |
1121 | /* Flag ECC errors */ |
1122 | if ((status & R1_CARD_ECC_FAILED) || | |
1123 | (brq->stop.resp[0] & R1_CARD_ECC_FAILED) || | |
1124 | (brq->cmd.resp[0] & R1_CARD_ECC_FAILED)) | |
2cc64587 | 1125 | *ecc_err = true; |
67716327 | 1126 | |
c8760069 KY |
1127 | /* Flag General errors */ |
1128 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) | |
1129 | if ((status & R1_ERROR) || | |
1130 | (brq->stop.resp[0] & R1_ERROR)) { | |
1131 | pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n", | |
1132 | req->rq_disk->disk_name, __func__, | |
1133 | brq->stop.resp[0], status); | |
c44d6cef | 1134 | *gen_err = true; |
c8760069 KY |
1135 | } |
1136 | ||
a01f3ccf RKAL |
1137 | /* |
1138 | * Check the current card state. If it is in some data transfer | |
1139 | * mode, tell it to stop (and hopefully transition back to TRAN.) | |
1140 | */ | |
1141 | if (R1_CURRENT_STATE(status) == R1_STATE_DATA || | |
1142 | R1_CURRENT_STATE(status) == R1_STATE_RCV) { | |
bb5cba40 UH |
1143 | err = send_stop(card, |
1144 | DIV_ROUND_UP(brq->data.timeout_ns, 1000000), | |
1145 | req, gen_err, &stop_status); | |
1146 | if (err) { | |
a01f3ccf RKAL |
1147 | pr_err("%s: error %d sending stop command\n", |
1148 | req->rq_disk->disk_name, err); | |
bb5cba40 UH |
1149 | /* |
1150 | * If the stop cmd also timed out, the card is probably | |
1151 | * not present, so abort. Other errors are bad news too. | |
1152 | */ | |
a01f3ccf | 1153 | return ERR_ABORT; |
bb5cba40 UH |
1154 | } |
1155 | ||
67716327 | 1156 | if (stop_status & R1_CARD_ECC_FAILED) |
2cc64587 | 1157 | *ecc_err = true; |
a01f3ccf RKAL |
1158 | } |
1159 | ||
1160 | /* Check for set block count errors */ | |
1161 | if (brq->sbc.error) | |
1162 | return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error, | |
1163 | prev_cmd_status_valid, status); | |
1164 | ||
1165 | /* Check for r/w command errors */ | |
1166 | if (brq->cmd.error) | |
1167 | return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error, | |
1168 | prev_cmd_status_valid, status); | |
1169 | ||
67716327 AH |
1170 | /* Data errors */ |
1171 | if (!brq->stop.error) | |
1172 | return ERR_CONTINUE; | |
1173 | ||
a01f3ccf | 1174 | /* Now for stop errors. These aren't fatal to the transfer. */ |
5e1344eb | 1175 | pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n", |
a01f3ccf RKAL |
1176 | req->rq_disk->disk_name, brq->stop.error, |
1177 | brq->cmd.resp[0], status); | |
1178 | ||
1179 | /* | |
1180 | * Subsitute in our own stop status as this will give the error | |
1181 | * state which happened during the execution of the r/w command. | |
1182 | */ | |
1183 | if (stop_status) { | |
1184 | brq->stop.resp[0] = stop_status; | |
1185 | brq->stop.error = 0; | |
1186 | } | |
1187 | return ERR_CONTINUE; | |
1188 | } | |
1189 | ||
67716327 AH |
1190 | static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host, |
1191 | int type) | |
1192 | { | |
1193 | int err; | |
1194 | ||
1195 | if (md->reset_done & type) | |
1196 | return -EEXIST; | |
1197 | ||
1198 | md->reset_done |= type; | |
1199 | err = mmc_hw_reset(host); | |
1200 | /* Ensure we switch back to the correct partition */ | |
1201 | if (err != -EOPNOTSUPP) { | |
fc95e30b UH |
1202 | struct mmc_blk_data *main_md = |
1203 | dev_get_drvdata(&host->card->dev); | |
67716327 AH |
1204 | int part_err; |
1205 | ||
1206 | main_md->part_curr = main_md->part_type; | |
1f797edc | 1207 | part_err = mmc_blk_part_switch(host->card, md->part_type); |
67716327 AH |
1208 | if (part_err) { |
1209 | /* | |
1210 | * We have failed to get back into the correct | |
1211 | * partition, so we need to abort the whole request. | |
1212 | */ | |
1213 | return -ENODEV; | |
1214 | } | |
1215 | } | |
1216 | return err; | |
1217 | } | |
1218 | ||
1219 | static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type) | |
1220 | { | |
1221 | md->reset_done &= ~type; | |
1222 | } | |
1223 | ||
81196976 AH |
1224 | static void mmc_blk_end_request(struct request *req, blk_status_t error) |
1225 | { | |
1226 | if (req->mq_ctx) | |
1227 | blk_mq_end_request(req, error); | |
1228 | else | |
1229 | blk_end_request_all(req, error); | |
1230 | } | |
1231 | ||
5ec12396 LW |
1232 | /* |
1233 | * The non-block commands come back from the block layer after it queued it and | |
1234 | * processed it with all other requests and then they get issued in this | |
1235 | * function. | |
1236 | */ | |
1237 | static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req) | |
1238 | { | |
1239 | struct mmc_queue_req *mq_rq; | |
1240 | struct mmc_card *card = mq->card; | |
1241 | struct mmc_blk_data *md = mq->blkdata; | |
69f7599e | 1242 | struct mmc_blk_ioc_data **idata; |
97548575 | 1243 | bool rpmb_ioctl; |
627c3ccf LW |
1244 | u8 **ext_csd; |
1245 | u32 status; | |
0493f6fe | 1246 | int ret; |
5ec12396 LW |
1247 | int i; |
1248 | ||
1249 | mq_rq = req_to_mmc_queue_req(req); | |
97548575 | 1250 | rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB); |
5ec12396 LW |
1251 | |
1252 | switch (mq_rq->drv_op) { | |
1253 | case MMC_DRV_OP_IOCTL: | |
97548575 | 1254 | case MMC_DRV_OP_IOCTL_RPMB: |
69f7599e | 1255 | idata = mq_rq->drv_op_data; |
7432b49b | 1256 | for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) { |
69f7599e | 1257 | ret = __mmc_blk_ioctl_cmd(card, md, idata[i]); |
0493f6fe | 1258 | if (ret) |
5ec12396 LW |
1259 | break; |
1260 | } | |
5ec12396 | 1261 | /* Always switch back to main area after RPMB access */ |
97548575 LW |
1262 | if (rpmb_ioctl) |
1263 | mmc_blk_part_switch(card, 0); | |
0493f6fe LW |
1264 | break; |
1265 | case MMC_DRV_OP_BOOT_WP: | |
1266 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP, | |
1267 | card->ext_csd.boot_ro_lock | | |
1268 | EXT_CSD_BOOT_WP_B_PWR_WP_EN, | |
1269 | card->ext_csd.part_time); | |
1270 | if (ret) | |
1271 | pr_err("%s: Locking boot partition ro until next power on failed: %d\n", | |
1272 | md->disk->disk_name, ret); | |
1273 | else | |
1274 | card->ext_csd.boot_ro_lock |= | |
1275 | EXT_CSD_BOOT_WP_B_PWR_WP_EN; | |
5ec12396 | 1276 | break; |
627c3ccf LW |
1277 | case MMC_DRV_OP_GET_CARD_STATUS: |
1278 | ret = mmc_send_status(card, &status); | |
1279 | if (!ret) | |
1280 | ret = status; | |
1281 | break; | |
1282 | case MMC_DRV_OP_GET_EXT_CSD: | |
1283 | ext_csd = mq_rq->drv_op_data; | |
1284 | ret = mmc_get_ext_csd(card, ext_csd); | |
1285 | break; | |
5ec12396 | 1286 | default: |
0493f6fe LW |
1287 | pr_err("%s: unknown driver specific operation\n", |
1288 | md->disk->disk_name); | |
1289 | ret = -EINVAL; | |
5ec12396 LW |
1290 | break; |
1291 | } | |
0493f6fe | 1292 | mq_rq->drv_op_result = ret; |
81196976 | 1293 | mmc_blk_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK); |
5ec12396 LW |
1294 | } |
1295 | ||
df061588 | 1296 | static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req) |
bd788c96 | 1297 | { |
7db3028e | 1298 | struct mmc_blk_data *md = mq->blkdata; |
bd788c96 AH |
1299 | struct mmc_card *card = md->queue.card; |
1300 | unsigned int from, nr, arg; | |
67716327 | 1301 | int err = 0, type = MMC_BLK_DISCARD; |
2a842aca | 1302 | blk_status_t status = BLK_STS_OK; |
bd788c96 | 1303 | |
bd788c96 | 1304 | if (!mmc_can_erase(card)) { |
2a842aca | 1305 | status = BLK_STS_NOTSUPP; |
8cb6ed17 | 1306 | goto fail; |
bd788c96 AH |
1307 | } |
1308 | ||
1309 | from = blk_rq_pos(req); | |
1310 | nr = blk_rq_sectors(req); | |
1311 | ||
b3bf9153 KP |
1312 | if (mmc_can_discard(card)) |
1313 | arg = MMC_DISCARD_ARG; | |
1314 | else if (mmc_can_trim(card)) | |
bd788c96 AH |
1315 | arg = MMC_TRIM_ARG; |
1316 | else | |
1317 | arg = MMC_ERASE_ARG; | |
164b50b3 GU |
1318 | do { |
1319 | err = 0; | |
1320 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { | |
1321 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
1322 | INAND_CMD38_ARG_EXT_CSD, | |
1323 | arg == MMC_TRIM_ARG ? | |
1324 | INAND_CMD38_ARG_TRIM : | |
1325 | INAND_CMD38_ARG_ERASE, | |
1326 | 0); | |
1327 | } | |
1328 | if (!err) | |
1329 | err = mmc_erase(card, from, nr, arg); | |
1330 | } while (err == -EIO && !mmc_blk_reset(md, card->host, type)); | |
2a842aca CH |
1331 | if (err) |
1332 | status = BLK_STS_IOERR; | |
1333 | else | |
67716327 | 1334 | mmc_blk_reset_success(md, type); |
8cb6ed17 | 1335 | fail: |
81196976 | 1336 | mmc_blk_end_request(req, status); |
bd788c96 AH |
1337 | } |
1338 | ||
df061588 | 1339 | static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, |
49804548 AH |
1340 | struct request *req) |
1341 | { | |
7db3028e | 1342 | struct mmc_blk_data *md = mq->blkdata; |
49804548 | 1343 | struct mmc_card *card = md->queue.card; |
775a9362 | 1344 | unsigned int from, nr, arg; |
67716327 | 1345 | int err = 0, type = MMC_BLK_SECDISCARD; |
2a842aca | 1346 | blk_status_t status = BLK_STS_OK; |
49804548 | 1347 | |
775a9362 | 1348 | if (!(mmc_can_secure_erase_trim(card))) { |
2a842aca | 1349 | status = BLK_STS_NOTSUPP; |
49804548 AH |
1350 | goto out; |
1351 | } | |
1352 | ||
28302812 AH |
1353 | from = blk_rq_pos(req); |
1354 | nr = blk_rq_sectors(req); | |
1355 | ||
775a9362 ME |
1356 | if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr)) |
1357 | arg = MMC_SECURE_TRIM1_ARG; | |
1358 | else | |
1359 | arg = MMC_SECURE_ERASE_ARG; | |
d9ddd629 | 1360 | |
67716327 | 1361 | retry: |
6a7a6b45 AW |
1362 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
1363 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
1364 | INAND_CMD38_ARG_EXT_CSD, | |
1365 | arg == MMC_SECURE_TRIM1_ARG ? | |
1366 | INAND_CMD38_ARG_SECTRIM1 : | |
1367 | INAND_CMD38_ARG_SECERASE, | |
1368 | 0); | |
1369 | if (err) | |
28302812 | 1370 | goto out_retry; |
6a7a6b45 | 1371 | } |
28302812 | 1372 | |
49804548 | 1373 | err = mmc_erase(card, from, nr, arg); |
28302812 AH |
1374 | if (err == -EIO) |
1375 | goto out_retry; | |
2a842aca CH |
1376 | if (err) { |
1377 | status = BLK_STS_IOERR; | |
28302812 | 1378 | goto out; |
2a842aca | 1379 | } |
28302812 AH |
1380 | |
1381 | if (arg == MMC_SECURE_TRIM1_ARG) { | |
6a7a6b45 AW |
1382 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
1383 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
1384 | INAND_CMD38_ARG_EXT_CSD, | |
1385 | INAND_CMD38_ARG_SECTRIM2, | |
1386 | 0); | |
1387 | if (err) | |
28302812 | 1388 | goto out_retry; |
6a7a6b45 | 1389 | } |
28302812 | 1390 | |
49804548 | 1391 | err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_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 | } |
6a7a6b45 | 1398 | } |
28302812 | 1399 | |
28302812 AH |
1400 | out_retry: |
1401 | if (err && !mmc_blk_reset(md, card->host, type)) | |
67716327 AH |
1402 | goto retry; |
1403 | if (!err) | |
1404 | mmc_blk_reset_success(md, type); | |
28302812 | 1405 | out: |
81196976 | 1406 | mmc_blk_end_request(req, status); |
49804548 AH |
1407 | } |
1408 | ||
df061588 | 1409 | static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req) |
f4c5522b | 1410 | { |
7db3028e | 1411 | struct mmc_blk_data *md = mq->blkdata; |
881d1c25 SJ |
1412 | struct mmc_card *card = md->queue.card; |
1413 | int ret = 0; | |
1414 | ||
1415 | ret = mmc_flush_cache(card); | |
81196976 | 1416 | mmc_blk_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK); |
f4c5522b AW |
1417 | } |
1418 | ||
1419 | /* | |
1420 | * Reformat current write as a reliable write, supporting | |
1421 | * both legacy and the enhanced reliable write MMC cards. | |
1422 | * In each transfer we'll handle only as much as a single | |
1423 | * reliable write can handle, thus finish the request in | |
1424 | * partial completions. | |
1425 | */ | |
d0c97cfb AW |
1426 | static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, |
1427 | struct mmc_card *card, | |
1428 | struct request *req) | |
f4c5522b | 1429 | { |
f4c5522b AW |
1430 | if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) { |
1431 | /* Legacy mode imposes restrictions on transfers. */ | |
9cb38f7a | 1432 | if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors)) |
f4c5522b AW |
1433 | brq->data.blocks = 1; |
1434 | ||
1435 | if (brq->data.blocks > card->ext_csd.rel_sectors) | |
1436 | brq->data.blocks = card->ext_csd.rel_sectors; | |
1437 | else if (brq->data.blocks < card->ext_csd.rel_sectors) | |
1438 | brq->data.blocks = 1; | |
1439 | } | |
f4c5522b AW |
1440 | } |
1441 | ||
4c2b8f26 RKAL |
1442 | #define CMD_ERRORS \ |
1443 | (R1_OUT_OF_RANGE | /* Command argument out of range */ \ | |
1444 | R1_ADDRESS_ERROR | /* Misaligned address */ \ | |
1445 | R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\ | |
1446 | R1_WP_VIOLATION | /* Tried to write to protected block */ \ | |
a04e6bae | 1447 | R1_CARD_ECC_FAILED | /* Card ECC failed */ \ |
4c2b8f26 RKAL |
1448 | R1_CC_ERROR | /* Card controller error */ \ |
1449 | R1_ERROR) /* General/unknown error */ | |
1450 | ||
d83c2dba | 1451 | static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq) |
a04e6bae | 1452 | { |
d83c2dba | 1453 | u32 val; |
a04e6bae | 1454 | |
d83c2dba SL |
1455 | /* |
1456 | * Per the SD specification(physical layer version 4.10)[1], | |
1457 | * section 4.3.3, it explicitly states that "When the last | |
1458 | * block of user area is read using CMD18, the host should | |
1459 | * ignore OUT_OF_RANGE error that may occur even the sequence | |
1460 | * is correct". And JESD84-B51 for eMMC also has a similar | |
1461 | * statement on section 6.8.3. | |
1462 | * | |
1463 | * Multiple block read/write could be done by either predefined | |
1464 | * method, namely CMD23, or open-ending mode. For open-ending mode, | |
1465 | * we should ignore the OUT_OF_RANGE error as it's normal behaviour. | |
1466 | * | |
1467 | * However the spec[1] doesn't tell us whether we should also | |
1468 | * ignore that for predefined method. But per the spec[1], section | |
1469 | * 4.15 Set Block Count Command, it says"If illegal block count | |
1470 | * is set, out of range error will be indicated during read/write | |
1471 | * operation (For example, data transfer is stopped at user area | |
1472 | * boundary)." In another word, we could expect a out of range error | |
1473 | * in the response for the following CMD18/25. And if argument of | |
1474 | * CMD23 + the argument of CMD18/25 exceed the max number of blocks, | |
1475 | * we could also expect to get a -ETIMEDOUT or any error number from | |
1476 | * the host drivers due to missing data response(for write)/data(for | |
1477 | * read), as the cards will stop the data transfer by itself per the | |
1478 | * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode. | |
1479 | */ | |
1480 | ||
1481 | if (!brq->stop.error) { | |
1482 | bool oor_with_open_end; | |
1483 | /* If there is no error yet, check R1 response */ | |
1484 | ||
1485 | val = brq->stop.resp[0] & CMD_ERRORS; | |
1486 | oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc; | |
1487 | ||
1488 | if (val && !oor_with_open_end) | |
1489 | brq->stop.error = -EIO; | |
1490 | } | |
a04e6bae WS |
1491 | } |
1492 | ||
81196976 AH |
1493 | static enum mmc_blk_status __mmc_blk_err_check(struct mmc_card *card, |
1494 | struct mmc_queue_req *mq_mrq) | |
d78d4a8a | 1495 | { |
ee8a43a5 | 1496 | struct mmc_blk_request *brq = &mq_mrq->brq; |
67e69d52 | 1497 | struct request *req = mmc_queue_req_to_req(mq_mrq); |
b8360a49 | 1498 | int need_retune = card->host->need_retune; |
2cc64587 | 1499 | bool ecc_err = false; |
c44d6cef | 1500 | bool gen_err = false; |
d78d4a8a PF |
1501 | |
1502 | /* | |
1503 | * sbc.error indicates a problem with the set block count | |
1504 | * command. No data will have been transferred. | |
1505 | * | |
1506 | * cmd.error indicates a problem with the r/w command. No | |
1507 | * data will have been transferred. | |
1508 | * | |
1509 | * stop.error indicates a problem with the stop command. Data | |
1510 | * may have been transferred, or may still be transferring. | |
1511 | */ | |
d83c2dba SL |
1512 | |
1513 | mmc_blk_eval_resp_error(brq); | |
1514 | ||
1515 | if (brq->sbc.error || brq->cmd.error || | |
1516 | brq->stop.error || brq->data.error) { | |
c8760069 | 1517 | switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) { |
d78d4a8a PF |
1518 | case ERR_RETRY: |
1519 | return MMC_BLK_RETRY; | |
1520 | case ERR_ABORT: | |
1521 | return MMC_BLK_ABORT; | |
a8ad82cc SRT |
1522 | case ERR_NOMEDIUM: |
1523 | return MMC_BLK_NOMEDIUM; | |
d78d4a8a PF |
1524 | case ERR_CONTINUE: |
1525 | break; | |
1526 | } | |
1527 | } | |
1528 | ||
1529 | /* | |
1530 | * Check for errors relating to the execution of the | |
1531 | * initial command - such as address errors. No data | |
1532 | * has been transferred. | |
1533 | */ | |
1534 | if (brq->cmd.resp[0] & CMD_ERRORS) { | |
1535 | pr_err("%s: r/w command failed, status = %#x\n", | |
1536 | req->rq_disk->disk_name, brq->cmd.resp[0]); | |
1537 | return MMC_BLK_ABORT; | |
1538 | } | |
1539 | ||
1540 | /* | |
1541 | * Everything else is either success, or a data error of some | |
1542 | * kind. If it was a write, we may have transitioned to | |
1543 | * program mode, which we have to wait for it to complete. | |
1544 | */ | |
1545 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) { | |
c49433fb | 1546 | int err; |
8fee476b | 1547 | |
c8760069 KY |
1548 | /* Check stop command response */ |
1549 | if (brq->stop.resp[0] & R1_ERROR) { | |
1550 | pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n", | |
1551 | req->rq_disk->disk_name, __func__, | |
1552 | brq->stop.resp[0]); | |
c44d6cef | 1553 | gen_err = true; |
c8760069 KY |
1554 | } |
1555 | ||
95a91298 UH |
1556 | err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req, |
1557 | &gen_err); | |
c49433fb UH |
1558 | if (err) |
1559 | return MMC_BLK_CMD_ERR; | |
d78d4a8a PF |
1560 | } |
1561 | ||
c8760069 KY |
1562 | /* if general error occurs, retry the write operation. */ |
1563 | if (gen_err) { | |
1564 | pr_warn("%s: retrying write for general error\n", | |
1565 | req->rq_disk->disk_name); | |
1566 | return MMC_BLK_RETRY; | |
1567 | } | |
1568 | ||
9820a5b1 WS |
1569 | /* Some errors (ECC) are flagged on the next commmand, so check stop, too */ |
1570 | if (brq->data.error || brq->stop.error) { | |
b8360a49 | 1571 | if (need_retune && !brq->retune_retry_done) { |
09faf61d RK |
1572 | pr_debug("%s: retrying because a re-tune was needed\n", |
1573 | req->rq_disk->disk_name); | |
b8360a49 AH |
1574 | brq->retune_retry_done = 1; |
1575 | return MMC_BLK_RETRY; | |
1576 | } | |
d78d4a8a | 1577 | pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n", |
9820a5b1 | 1578 | req->rq_disk->disk_name, brq->data.error ?: brq->stop.error, |
d78d4a8a PF |
1579 | (unsigned)blk_rq_pos(req), |
1580 | (unsigned)blk_rq_sectors(req), | |
1581 | brq->cmd.resp[0], brq->stop.resp[0]); | |
1582 | ||
1583 | if (rq_data_dir(req) == READ) { | |
67716327 AH |
1584 | if (ecc_err) |
1585 | return MMC_BLK_ECC_ERR; | |
d78d4a8a PF |
1586 | return MMC_BLK_DATA_ERR; |
1587 | } else { | |
1588 | return MMC_BLK_CMD_ERR; | |
1589 | } | |
1590 | } | |
1591 | ||
67716327 AH |
1592 | if (!brq->data.bytes_xfered) |
1593 | return MMC_BLK_RETRY; | |
d78d4a8a | 1594 | |
67716327 AH |
1595 | if (blk_rq_bytes(req) != brq->data.bytes_xfered) |
1596 | return MMC_BLK_PARTIAL; | |
1597 | ||
1598 | return MMC_BLK_SUCCESS; | |
d78d4a8a PF |
1599 | } |
1600 | ||
81196976 AH |
1601 | static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card, |
1602 | struct mmc_async_req *areq) | |
1603 | { | |
1604 | struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req, | |
1605 | areq); | |
1606 | ||
1607 | return __mmc_blk_err_check(card, mq_mrq); | |
1608 | } | |
1609 | ||
ca5717f7 | 1610 | static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq, |
d3377c01 AH |
1611 | int disable_multi, bool *do_rel_wr_p, |
1612 | bool *do_data_tag_p) | |
1da177e4 | 1613 | { |
ca5717f7 AH |
1614 | struct mmc_blk_data *md = mq->blkdata; |
1615 | struct mmc_card *card = md->queue.card; | |
54d49d77 | 1616 | struct mmc_blk_request *brq = &mqrq->brq; |
67e69d52 | 1617 | struct request *req = mmc_queue_req_to_req(mqrq); |
d3377c01 | 1618 | bool do_rel_wr, do_data_tag; |
1da177e4 | 1619 | |
f4c5522b AW |
1620 | /* |
1621 | * Reliable writes are used to implement Forced Unit Access and | |
d3df0465 | 1622 | * are supported only on MMCs. |
f4c5522b | 1623 | */ |
d3377c01 AH |
1624 | do_rel_wr = (req->cmd_flags & REQ_FUA) && |
1625 | rq_data_dir(req) == WRITE && | |
1626 | (md->flags & MMC_BLK_REL_WR); | |
f4c5522b | 1627 | |
54d49d77 | 1628 | memset(brq, 0, sizeof(struct mmc_blk_request)); |
ca5717f7 | 1629 | |
54d49d77 | 1630 | brq->mrq.data = &brq->data; |
93482b3d | 1631 | brq->mrq.tag = req->tag; |
1da177e4 | 1632 | |
54d49d77 PF |
1633 | brq->stop.opcode = MMC_STOP_TRANSMISSION; |
1634 | brq->stop.arg = 0; | |
ca5717f7 AH |
1635 | |
1636 | if (rq_data_dir(req) == READ) { | |
1637 | brq->data.flags = MMC_DATA_READ; | |
1638 | brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; | |
1639 | } else { | |
1640 | brq->data.flags = MMC_DATA_WRITE; | |
1641 | brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; | |
1642 | } | |
1643 | ||
1644 | brq->data.blksz = 512; | |
54d49d77 | 1645 | brq->data.blocks = blk_rq_sectors(req); |
93482b3d AH |
1646 | brq->data.blk_addr = blk_rq_pos(req); |
1647 | ||
1648 | /* | |
1649 | * The command queue supports 2 priorities: "high" (1) and "simple" (0). | |
1650 | * The eMMC will give "high" priority tasks priority over "simple" | |
1651 | * priority tasks. Here we always set "simple" priority by not setting | |
1652 | * MMC_DATA_PRIO. | |
1653 | */ | |
6a79e391 | 1654 | |
54d49d77 PF |
1655 | /* |
1656 | * The block layer doesn't support all sector count | |
1657 | * restrictions, so we need to be prepared for too big | |
1658 | * requests. | |
1659 | */ | |
1660 | if (brq->data.blocks > card->host->max_blk_count) | |
1661 | brq->data.blocks = card->host->max_blk_count; | |
1da177e4 | 1662 | |
2bf22b39 PW |
1663 | if (brq->data.blocks > 1) { |
1664 | /* | |
1665 | * After a read error, we redo the request one sector | |
1666 | * at a time in order to accurately determine which | |
1667 | * sectors can be read successfully. | |
1668 | */ | |
1669 | if (disable_multi) | |
1670 | brq->data.blocks = 1; | |
1671 | ||
2e47e842 KM |
1672 | /* |
1673 | * Some controllers have HW issues while operating | |
1674 | * in multiple I/O mode | |
1675 | */ | |
1676 | if (card->host->ops->multi_io_quirk) | |
1677 | brq->data.blocks = card->host->ops->multi_io_quirk(card, | |
1678 | (rq_data_dir(req) == READ) ? | |
1679 | MMC_DATA_READ : MMC_DATA_WRITE, | |
1680 | brq->data.blocks); | |
2bf22b39 | 1681 | } |
d0c97cfb | 1682 | |
93482b3d | 1683 | if (do_rel_wr) { |
ca5717f7 | 1684 | mmc_apply_rel_rw(brq, card, req); |
93482b3d AH |
1685 | brq->data.flags |= MMC_DATA_REL_WR; |
1686 | } | |
ca5717f7 AH |
1687 | |
1688 | /* | |
1689 | * Data tag is used only during writing meta data to speed | |
1690 | * up write and any subsequent read of this meta data | |
1691 | */ | |
d3377c01 AH |
1692 | do_data_tag = card->ext_csd.data_tag_unit_size && |
1693 | (req->cmd_flags & REQ_META) && | |
1694 | (rq_data_dir(req) == WRITE) && | |
1695 | ((brq->data.blocks * brq->data.blksz) >= | |
1696 | card->ext_csd.data_tag_unit_size); | |
ca5717f7 | 1697 | |
93482b3d AH |
1698 | if (do_data_tag) |
1699 | brq->data.flags |= MMC_DATA_DAT_TAG; | |
1700 | ||
ca5717f7 AH |
1701 | mmc_set_data_timeout(&brq->data, card); |
1702 | ||
1703 | brq->data.sg = mqrq->sg; | |
1704 | brq->data.sg_len = mmc_queue_map_sg(mq, mqrq); | |
1705 | ||
1706 | /* | |
1707 | * Adjust the sg list so it is the same size as the | |
1708 | * request. | |
1709 | */ | |
1710 | if (brq->data.blocks != blk_rq_sectors(req)) { | |
1711 | int i, data_size = brq->data.blocks << 9; | |
1712 | struct scatterlist *sg; | |
1713 | ||
1714 | for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) { | |
1715 | data_size -= sg->length; | |
1716 | if (data_size <= 0) { | |
1717 | sg->length += data_size; | |
1718 | i++; | |
1719 | break; | |
1720 | } | |
1721 | } | |
1722 | brq->data.sg_len = i; | |
1723 | } | |
1724 | ||
1725 | mqrq->areq.mrq = &brq->mrq; | |
d3377c01 AH |
1726 | |
1727 | if (do_rel_wr_p) | |
1728 | *do_rel_wr_p = do_rel_wr; | |
1729 | ||
1730 | if (do_data_tag_p) | |
1731 | *do_data_tag_p = do_data_tag; | |
ca5717f7 AH |
1732 | } |
1733 | ||
1e8e55b6 AH |
1734 | #define MMC_CQE_RETRIES 2 |
1735 | ||
1736 | static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req) | |
1737 | { | |
1738 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
1739 | struct mmc_request *mrq = &mqrq->brq.mrq; | |
1740 | struct request_queue *q = req->q; | |
1741 | struct mmc_host *host = mq->card->host; | |
1742 | unsigned long flags; | |
1743 | bool put_card; | |
1744 | int err; | |
1745 | ||
1746 | mmc_cqe_post_req(host, mrq); | |
1747 | ||
1748 | if (mrq->cmd && mrq->cmd->error) | |
1749 | err = mrq->cmd->error; | |
1750 | else if (mrq->data && mrq->data->error) | |
1751 | err = mrq->data->error; | |
1752 | else | |
1753 | err = 0; | |
1754 | ||
1755 | if (err) { | |
1756 | if (mqrq->retries++ < MMC_CQE_RETRIES) | |
1757 | blk_mq_requeue_request(req, true); | |
1758 | else | |
1759 | blk_mq_end_request(req, BLK_STS_IOERR); | |
1760 | } else if (mrq->data) { | |
1761 | if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered)) | |
1762 | blk_mq_requeue_request(req, true); | |
1763 | else | |
1764 | __blk_mq_end_request(req, BLK_STS_OK); | |
1765 | } else { | |
1766 | blk_mq_end_request(req, BLK_STS_OK); | |
1767 | } | |
1768 | ||
1769 | spin_lock_irqsave(q->queue_lock, flags); | |
1770 | ||
1771 | mq->in_flight[mmc_issue_type(mq, req)] -= 1; | |
1772 | ||
1773 | put_card = (mmc_tot_in_flight(mq) == 0); | |
1774 | ||
1775 | mmc_cqe_check_busy(mq); | |
1776 | ||
1777 | spin_unlock_irqrestore(q->queue_lock, flags); | |
1778 | ||
1779 | if (!mq->cqe_busy) | |
1780 | blk_mq_run_hw_queues(q, true); | |
1781 | ||
1782 | if (put_card) | |
1783 | mmc_put_card(mq->card, &mq->ctx); | |
1784 | } | |
1785 | ||
1786 | void mmc_blk_cqe_recovery(struct mmc_queue *mq) | |
1787 | { | |
1788 | struct mmc_card *card = mq->card; | |
1789 | struct mmc_host *host = card->host; | |
1790 | int err; | |
1791 | ||
1792 | pr_debug("%s: CQE recovery start\n", mmc_hostname(host)); | |
1793 | ||
1794 | err = mmc_cqe_recovery(host); | |
1795 | if (err) | |
1796 | mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY); | |
1797 | else | |
1798 | mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY); | |
1799 | ||
1800 | pr_debug("%s: CQE recovery done\n", mmc_hostname(host)); | |
1801 | } | |
1802 | ||
1803 | static void mmc_blk_cqe_req_done(struct mmc_request *mrq) | |
1804 | { | |
1805 | struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req, | |
1806 | brq.mrq); | |
1807 | struct request *req = mmc_queue_req_to_req(mqrq); | |
1808 | struct request_queue *q = req->q; | |
1809 | struct mmc_queue *mq = q->queuedata; | |
1810 | ||
1811 | /* | |
1812 | * Block layer timeouts race with completions which means the normal | |
1813 | * completion path cannot be used during recovery. | |
1814 | */ | |
1815 | if (mq->in_recovery) | |
1816 | mmc_blk_cqe_complete_rq(mq, req); | |
1817 | else | |
1818 | blk_mq_complete_request(req); | |
1819 | } | |
1820 | ||
1821 | static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq) | |
1822 | { | |
1823 | mrq->done = mmc_blk_cqe_req_done; | |
1824 | mrq->recovery_notifier = mmc_cqe_recovery_notifier; | |
1825 | ||
1826 | return mmc_cqe_start_req(host, mrq); | |
1827 | } | |
1828 | ||
1829 | static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq, | |
1830 | struct request *req) | |
1831 | { | |
1832 | struct mmc_blk_request *brq = &mqrq->brq; | |
1833 | ||
1834 | memset(brq, 0, sizeof(*brq)); | |
1835 | ||
1836 | brq->mrq.cmd = &brq->cmd; | |
1837 | brq->mrq.tag = req->tag; | |
1838 | ||
1839 | return &brq->mrq; | |
1840 | } | |
1841 | ||
1842 | static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req) | |
1843 | { | |
1844 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
1845 | struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req); | |
1846 | ||
1847 | mrq->cmd->opcode = MMC_SWITCH; | |
1848 | mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | | |
1849 | (EXT_CSD_FLUSH_CACHE << 16) | | |
1850 | (1 << 8) | | |
1851 | EXT_CSD_CMD_SET_NORMAL; | |
1852 | mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B; | |
1853 | ||
1854 | return mmc_blk_cqe_start_req(mq->card->host, mrq); | |
1855 | } | |
1856 | ||
1857 | static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req) | |
1858 | { | |
1859 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
1860 | ||
1861 | mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL); | |
1862 | ||
1863 | return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq); | |
1864 | } | |
1865 | ||
ca5717f7 AH |
1866 | static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, |
1867 | struct mmc_card *card, | |
1868 | int disable_multi, | |
1869 | struct mmc_queue *mq) | |
1870 | { | |
1871 | u32 readcmd, writecmd; | |
1872 | struct mmc_blk_request *brq = &mqrq->brq; | |
67e69d52 | 1873 | struct request *req = mmc_queue_req_to_req(mqrq); |
ca5717f7 AH |
1874 | struct mmc_blk_data *md = mq->blkdata; |
1875 | bool do_rel_wr, do_data_tag; | |
1876 | ||
1877 | mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag); | |
1878 | ||
1879 | brq->mrq.cmd = &brq->cmd; | |
1880 | ||
1881 | brq->cmd.arg = blk_rq_pos(req); | |
1882 | if (!mmc_card_blockaddr(card)) | |
1883 | brq->cmd.arg <<= 9; | |
1884 | brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; | |
1885 | ||
54d49d77 PF |
1886 | if (brq->data.blocks > 1 || do_rel_wr) { |
1887 | /* SPI multiblock writes terminate using a special | |
1888 | * token, not a STOP_TRANSMISSION request. | |
d0c97cfb | 1889 | */ |
54d49d77 PF |
1890 | if (!mmc_host_is_spi(card->host) || |
1891 | rq_data_dir(req) == READ) | |
1892 | brq->mrq.stop = &brq->stop; | |
1893 | readcmd = MMC_READ_MULTIPLE_BLOCK; | |
1894 | writecmd = MMC_WRITE_MULTIPLE_BLOCK; | |
1895 | } else { | |
1896 | brq->mrq.stop = NULL; | |
1897 | readcmd = MMC_READ_SINGLE_BLOCK; | |
1898 | writecmd = MMC_WRITE_BLOCK; | |
1899 | } | |
ca5717f7 | 1900 | brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd; |
4265900e | 1901 | |
54d49d77 PF |
1902 | /* |
1903 | * Pre-defined multi-block transfers are preferable to | |
1904 | * open ended-ones (and necessary for reliable writes). | |
1905 | * However, it is not sufficient to just send CMD23, | |
1906 | * and avoid the final CMD12, as on an error condition | |
1907 | * CMD12 (stop) needs to be sent anyway. This, coupled | |
1908 | * with Auto-CMD23 enhancements provided by some | |
1909 | * hosts, means that the complexity of dealing | |
1910 | * with this is best left to the host. If CMD23 is | |
1911 | * supported by card and host, we'll fill sbc in and let | |
1912 | * the host deal with handling it correctly. This means | |
1913 | * that for hosts that don't expose MMC_CAP_CMD23, no | |
1914 | * change of behavior will be observed. | |
1915 | * | |
1916 | * N.B: Some MMC cards experience perf degradation. | |
1917 | * We'll avoid using CMD23-bounded multiblock writes for | |
1918 | * these, while retaining features like reliable writes. | |
1919 | */ | |
4265900e SD |
1920 | if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) && |
1921 | (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) || | |
1922 | do_data_tag)) { | |
54d49d77 PF |
1923 | brq->sbc.opcode = MMC_SET_BLOCK_COUNT; |
1924 | brq->sbc.arg = brq->data.blocks | | |
4265900e SD |
1925 | (do_rel_wr ? (1 << 31) : 0) | |
1926 | (do_data_tag ? (1 << 29) : 0); | |
54d49d77 PF |
1927 | brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; |
1928 | brq->mrq.sbc = &brq->sbc; | |
1929 | } | |
98ccf149 | 1930 | |
74f5ba35 | 1931 | mqrq->areq.err_check = mmc_blk_err_check; |
54d49d77 | 1932 | } |
6a79e391 | 1933 | |
81196976 AH |
1934 | #define MMC_MAX_RETRIES 5 |
1935 | #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1) | |
1936 | ||
1937 | #define MMC_READ_SINGLE_RETRIES 2 | |
1938 | ||
1939 | /* Single sector read during recovery */ | |
1940 | static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req) | |
1941 | { | |
1942 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
1943 | struct mmc_request *mrq = &mqrq->brq.mrq; | |
1944 | struct mmc_card *card = mq->card; | |
1945 | struct mmc_host *host = card->host; | |
1946 | blk_status_t error = BLK_STS_OK; | |
1947 | int retries = 0; | |
1948 | ||
1949 | do { | |
1950 | u32 status; | |
1951 | int err; | |
1952 | ||
1953 | mmc_blk_rw_rq_prep(mqrq, card, 1, mq); | |
1954 | ||
1955 | mmc_wait_for_req(host, mrq); | |
1956 | ||
1957 | err = mmc_send_status(card, &status); | |
1958 | if (err) | |
1959 | goto error_exit; | |
1960 | ||
1961 | if (!mmc_host_is_spi(host) && | |
1962 | R1_CURRENT_STATE(status) != R1_STATE_TRAN) { | |
1963 | u32 stop_status = 0; | |
1964 | bool gen_err = false; | |
1965 | ||
1966 | err = send_stop(card, | |
1967 | DIV_ROUND_UP(mrq->data->timeout_ns, | |
1968 | 1000000), | |
1969 | req, &gen_err, &stop_status); | |
1970 | if (err) | |
1971 | goto error_exit; | |
1972 | } | |
1973 | ||
1974 | if (mrq->cmd->error && retries++ < MMC_READ_SINGLE_RETRIES) | |
1975 | continue; | |
1976 | ||
1977 | retries = 0; | |
1978 | ||
1979 | if (mrq->cmd->error || | |
1980 | mrq->data->error || | |
1981 | (!mmc_host_is_spi(host) && | |
1982 | (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS))) | |
1983 | error = BLK_STS_IOERR; | |
1984 | else | |
1985 | error = BLK_STS_OK; | |
1986 | ||
1987 | } while (blk_update_request(req, error, 512)); | |
1988 | ||
1989 | return; | |
1990 | ||
1991 | error_exit: | |
1992 | mrq->data->bytes_xfered = 0; | |
1993 | blk_update_request(req, BLK_STS_IOERR, 512); | |
1994 | /* Let it try the remaining request again */ | |
1995 | if (mqrq->retries > MMC_MAX_RETRIES - 1) | |
1996 | mqrq->retries = MMC_MAX_RETRIES - 1; | |
1997 | } | |
1998 | ||
1999 | static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req) | |
2000 | { | |
2001 | int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; | |
2002 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
2003 | struct mmc_blk_request *brq = &mqrq->brq; | |
2004 | struct mmc_blk_data *md = mq->blkdata; | |
2005 | struct mmc_card *card = mq->card; | |
2006 | static enum mmc_blk_status status; | |
2007 | ||
2008 | brq->retune_retry_done = mqrq->retries; | |
2009 | ||
2010 | status = __mmc_blk_err_check(card, mqrq); | |
2011 | ||
2012 | mmc_retune_release(card->host); | |
2013 | ||
2014 | /* | |
2015 | * Requests are completed by mmc_blk_mq_complete_rq() which sets simple | |
2016 | * policy: | |
2017 | * 1. A request that has transferred at least some data is considered | |
2018 | * successful and will be requeued if there is remaining data to | |
2019 | * transfer. | |
2020 | * 2. Otherwise the number of retries is incremented and the request | |
2021 | * will be requeued if there are remaining retries. | |
2022 | * 3. Otherwise the request will be errored out. | |
2023 | * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and | |
2024 | * mqrq->retries. So there are only 4 possible actions here: | |
2025 | * 1. do not accept the bytes_xfered value i.e. set it to zero | |
2026 | * 2. change mqrq->retries to determine the number of retries | |
2027 | * 3. try to reset the card | |
2028 | * 4. read one sector at a time | |
2029 | */ | |
2030 | switch (status) { | |
2031 | case MMC_BLK_SUCCESS: | |
2032 | case MMC_BLK_PARTIAL: | |
2033 | /* Reset success, and accept bytes_xfered */ | |
2034 | mmc_blk_reset_success(md, type); | |
2035 | break; | |
2036 | case MMC_BLK_CMD_ERR: | |
2037 | /* | |
2038 | * For SD cards, get bytes written, but do not accept | |
2039 | * bytes_xfered if that fails. For MMC cards accept | |
2040 | * bytes_xfered. Then try to reset. If reset fails then | |
2041 | * error out the remaining request, otherwise retry | |
2042 | * once (N.B mmc_blk_reset() will not succeed twice in a | |
2043 | * row). | |
2044 | */ | |
2045 | if (mmc_card_sd(card)) { | |
2046 | u32 blocks; | |
2047 | int err; | |
2048 | ||
2049 | err = mmc_sd_num_wr_blocks(card, &blocks); | |
2050 | if (err) | |
2051 | brq->data.bytes_xfered = 0; | |
2052 | else | |
2053 | brq->data.bytes_xfered = blocks << 9; | |
2054 | } | |
2055 | if (mmc_blk_reset(md, card->host, type)) | |
2056 | mqrq->retries = MMC_NO_RETRIES; | |
2057 | else | |
2058 | mqrq->retries = MMC_MAX_RETRIES - 1; | |
2059 | break; | |
2060 | case MMC_BLK_RETRY: | |
2061 | /* | |
2062 | * Do not accept bytes_xfered, but retry up to 5 times, | |
2063 | * otherwise same as abort. | |
2064 | */ | |
2065 | brq->data.bytes_xfered = 0; | |
2066 | if (mqrq->retries < MMC_MAX_RETRIES) | |
2067 | break; | |
2068 | /* Fall through */ | |
2069 | case MMC_BLK_ABORT: | |
2070 | /* | |
2071 | * Do not accept bytes_xfered, but try to reset. If | |
2072 | * reset succeeds, try once more, otherwise error out | |
2073 | * the request. | |
2074 | */ | |
2075 | brq->data.bytes_xfered = 0; | |
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_DATA_ERR: { | |
2082 | int err; | |
2083 | ||
2084 | /* | |
2085 | * Do not accept bytes_xfered, but try to reset. If | |
2086 | * reset succeeds, try once more. If reset fails with | |
2087 | * ENODEV which means the partition is wrong, then error | |
2088 | * out the request. Otherwise attempt to read one sector | |
2089 | * at a time. | |
2090 | */ | |
2091 | brq->data.bytes_xfered = 0; | |
2092 | err = mmc_blk_reset(md, card->host, type); | |
2093 | if (!err) { | |
2094 | mqrq->retries = MMC_MAX_RETRIES - 1; | |
2095 | break; | |
2096 | } | |
2097 | if (err == -ENODEV) { | |
2098 | mqrq->retries = MMC_NO_RETRIES; | |
2099 | break; | |
2100 | } | |
2101 | /* Fall through */ | |
2102 | } | |
2103 | case MMC_BLK_ECC_ERR: | |
2104 | /* | |
2105 | * Do not accept bytes_xfered. If reading more than one | |
2106 | * sector, try reading one sector at a time. | |
2107 | */ | |
2108 | brq->data.bytes_xfered = 0; | |
2109 | /* FIXME: Missing single sector read for large sector size */ | |
2110 | if (brq->data.blocks > 1 && !mmc_large_sector(card)) { | |
2111 | /* Redo read one sector at a time */ | |
2112 | pr_warn("%s: retrying using single block read\n", | |
2113 | req->rq_disk->disk_name); | |
2114 | mmc_blk_read_single(mq, req); | |
2115 | } else { | |
2116 | mqrq->retries = MMC_NO_RETRIES; | |
2117 | } | |
2118 | break; | |
2119 | case MMC_BLK_NOMEDIUM: | |
2120 | /* Do not accept bytes_xfered. Error out the request */ | |
2121 | brq->data.bytes_xfered = 0; | |
2122 | mqrq->retries = MMC_NO_RETRIES; | |
2123 | break; | |
2124 | default: | |
2125 | /* Do not accept bytes_xfered. Error out the request */ | |
2126 | brq->data.bytes_xfered = 0; | |
2127 | mqrq->retries = MMC_NO_RETRIES; | |
2128 | pr_err("%s: Unhandled return value (%d)", | |
2129 | req->rq_disk->disk_name, status); | |
2130 | break; | |
2131 | } | |
2132 | } | |
2133 | ||
10f21df4 AH |
2134 | static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq) |
2135 | { | |
2136 | mmc_blk_eval_resp_error(brq); | |
2137 | ||
2138 | return brq->sbc.error || brq->cmd.error || brq->stop.error || | |
2139 | brq->data.error || brq->cmd.resp[0] & CMD_ERRORS; | |
2140 | } | |
2141 | ||
2142 | static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq, | |
2143 | struct request *req) | |
2144 | { | |
2145 | int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; | |
2146 | ||
2147 | mmc_blk_reset_success(mq->blkdata, type); | |
2148 | } | |
2149 | ||
81196976 AH |
2150 | static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req) |
2151 | { | |
2152 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
2153 | unsigned int nr_bytes = mqrq->brq.data.bytes_xfered; | |
2154 | ||
2155 | if (nr_bytes) { | |
2156 | if (blk_update_request(req, BLK_STS_OK, nr_bytes)) | |
2157 | blk_mq_requeue_request(req, true); | |
2158 | else | |
2159 | __blk_mq_end_request(req, BLK_STS_OK); | |
2160 | } else if (!blk_rq_bytes(req)) { | |
2161 | __blk_mq_end_request(req, BLK_STS_IOERR); | |
2162 | } else if (mqrq->retries++ < MMC_MAX_RETRIES) { | |
2163 | blk_mq_requeue_request(req, true); | |
2164 | } else { | |
2165 | if (mmc_card_removed(mq->card)) | |
2166 | req->rq_flags |= RQF_QUIET; | |
2167 | blk_mq_end_request(req, BLK_STS_IOERR); | |
2168 | } | |
2169 | } | |
2170 | ||
2171 | static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq, | |
2172 | struct mmc_queue_req *mqrq) | |
2173 | { | |
2174 | return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) && | |
2175 | (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT || | |
2176 | mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT); | |
2177 | } | |
2178 | ||
2179 | static void mmc_blk_urgent_bkops(struct mmc_queue *mq, | |
2180 | struct mmc_queue_req *mqrq) | |
2181 | { | |
2182 | if (mmc_blk_urgent_bkops_needed(mq, mqrq)) | |
2183 | mmc_start_bkops(mq->card, true); | |
2184 | } | |
2185 | ||
2186 | void mmc_blk_mq_complete(struct request *req) | |
2187 | { | |
2188 | struct mmc_queue *mq = req->q->queuedata; | |
2189 | ||
1e8e55b6 AH |
2190 | if (mq->use_cqe) |
2191 | mmc_blk_cqe_complete_rq(mq, req); | |
2192 | else | |
2193 | mmc_blk_mq_complete_rq(mq, req); | |
81196976 AH |
2194 | } |
2195 | ||
2196 | static void mmc_blk_mq_poll_completion(struct mmc_queue *mq, | |
2197 | struct request *req) | |
2198 | { | |
2199 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
2200 | ||
2201 | mmc_blk_mq_rw_recovery(mq, req); | |
2202 | ||
2203 | mmc_blk_urgent_bkops(mq, mqrq); | |
2204 | } | |
2205 | ||
2206 | static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, struct request *req) | |
2207 | { | |
2208 | struct request_queue *q = req->q; | |
2209 | unsigned long flags; | |
2210 | bool put_card; | |
2211 | ||
2212 | spin_lock_irqsave(q->queue_lock, flags); | |
2213 | ||
2214 | mq->in_flight[mmc_issue_type(mq, req)] -= 1; | |
2215 | ||
2216 | put_card = (mmc_tot_in_flight(mq) == 0); | |
2217 | ||
2218 | spin_unlock_irqrestore(q->queue_lock, flags); | |
2219 | ||
2220 | if (put_card) | |
2221 | mmc_put_card(mq->card, &mq->ctx); | |
2222 | } | |
2223 | ||
2224 | static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req) | |
2225 | { | |
2226 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
2227 | struct mmc_request *mrq = &mqrq->brq.mrq; | |
2228 | struct mmc_host *host = mq->card->host; | |
2229 | ||
2230 | mmc_post_req(host, mrq, 0); | |
2231 | ||
10f21df4 AH |
2232 | /* |
2233 | * Block layer timeouts race with completions which means the normal | |
2234 | * completion path cannot be used during recovery. | |
2235 | */ | |
2236 | if (mq->in_recovery) | |
2237 | mmc_blk_mq_complete_rq(mq, req); | |
2238 | else | |
2239 | blk_mq_complete_request(req); | |
81196976 AH |
2240 | |
2241 | mmc_blk_mq_dec_in_flight(mq, req); | |
2242 | } | |
2243 | ||
10f21df4 AH |
2244 | void mmc_blk_mq_recovery(struct mmc_queue *mq) |
2245 | { | |
2246 | struct request *req = mq->recovery_req; | |
2247 | struct mmc_host *host = mq->card->host; | |
2248 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
2249 | ||
2250 | mq->recovery_req = NULL; | |
2251 | mq->rw_wait = false; | |
2252 | ||
2253 | if (mmc_blk_rq_error(&mqrq->brq)) { | |
2254 | mmc_retune_hold_now(host); | |
2255 | mmc_blk_mq_rw_recovery(mq, req); | |
2256 | } | |
2257 | ||
2258 | mmc_blk_urgent_bkops(mq, mqrq); | |
2259 | ||
2260 | mmc_blk_mq_post_req(mq, req); | |
2261 | } | |
2262 | ||
81196976 AH |
2263 | static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq, |
2264 | struct request **prev_req) | |
2265 | { | |
10f21df4 AH |
2266 | if (mmc_host_done_complete(mq->card->host)) |
2267 | return; | |
2268 | ||
81196976 AH |
2269 | mutex_lock(&mq->complete_lock); |
2270 | ||
2271 | if (!mq->complete_req) | |
2272 | goto out_unlock; | |
2273 | ||
2274 | mmc_blk_mq_poll_completion(mq, mq->complete_req); | |
2275 | ||
2276 | if (prev_req) | |
2277 | *prev_req = mq->complete_req; | |
2278 | else | |
2279 | mmc_blk_mq_post_req(mq, mq->complete_req); | |
2280 | ||
2281 | mq->complete_req = NULL; | |
2282 | ||
2283 | out_unlock: | |
2284 | mutex_unlock(&mq->complete_lock); | |
2285 | } | |
2286 | ||
2287 | void mmc_blk_mq_complete_work(struct work_struct *work) | |
2288 | { | |
2289 | struct mmc_queue *mq = container_of(work, struct mmc_queue, | |
2290 | complete_work); | |
2291 | ||
2292 | mmc_blk_mq_complete_prev_req(mq, NULL); | |
2293 | } | |
2294 | ||
2295 | static void mmc_blk_mq_req_done(struct mmc_request *mrq) | |
2296 | { | |
2297 | struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req, | |
2298 | brq.mrq); | |
2299 | struct request *req = mmc_queue_req_to_req(mqrq); | |
2300 | struct request_queue *q = req->q; | |
2301 | struct mmc_queue *mq = q->queuedata; | |
10f21df4 | 2302 | struct mmc_host *host = mq->card->host; |
81196976 | 2303 | unsigned long flags; |
81196976 | 2304 | |
10f21df4 AH |
2305 | if (!mmc_host_done_complete(host)) { |
2306 | bool waiting; | |
81196976 | 2307 | |
10f21df4 AH |
2308 | /* |
2309 | * We cannot complete the request in this context, so record | |
2310 | * that there is a request to complete, and that a following | |
2311 | * request does not need to wait (although it does need to | |
2312 | * complete complete_req first). | |
2313 | */ | |
2314 | spin_lock_irqsave(q->queue_lock, flags); | |
2315 | mq->complete_req = req; | |
2316 | mq->rw_wait = false; | |
2317 | waiting = mq->waiting; | |
2318 | spin_unlock_irqrestore(q->queue_lock, flags); | |
2319 | ||
2320 | /* | |
2321 | * If 'waiting' then the waiting task will complete this | |
2322 | * request, otherwise queue a work to do it. Note that | |
2323 | * complete_work may still race with the dispatch of a following | |
2324 | * request. | |
2325 | */ | |
2326 | if (waiting) | |
2327 | wake_up(&mq->wait); | |
2328 | else | |
2329 | kblockd_schedule_work(&mq->complete_work); | |
2330 | ||
2331 | return; | |
2332 | } | |
2333 | ||
2334 | /* Take the recovery path for errors or urgent background operations */ | |
2335 | if (mmc_blk_rq_error(&mqrq->brq) || | |
2336 | mmc_blk_urgent_bkops_needed(mq, mqrq)) { | |
2337 | spin_lock_irqsave(q->queue_lock, flags); | |
2338 | mq->recovery_needed = true; | |
2339 | mq->recovery_req = req; | |
2340 | spin_unlock_irqrestore(q->queue_lock, flags); | |
81196976 | 2341 | wake_up(&mq->wait); |
10f21df4 AH |
2342 | schedule_work(&mq->recovery_work); |
2343 | return; | |
2344 | } | |
2345 | ||
2346 | mmc_blk_rw_reset_success(mq, req); | |
2347 | ||
2348 | mq->rw_wait = false; | |
2349 | wake_up(&mq->wait); | |
2350 | ||
2351 | mmc_blk_mq_post_req(mq, req); | |
81196976 AH |
2352 | } |
2353 | ||
2354 | static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err) | |
2355 | { | |
2356 | struct request_queue *q = mq->queue; | |
2357 | unsigned long flags; | |
2358 | bool done; | |
2359 | ||
2360 | /* | |
10f21df4 AH |
2361 | * Wait while there is another request in progress, but not if recovery |
2362 | * is needed. Also indicate whether there is a request waiting to start. | |
81196976 AH |
2363 | */ |
2364 | spin_lock_irqsave(q->queue_lock, flags); | |
10f21df4 AH |
2365 | if (mq->recovery_needed) { |
2366 | *err = -EBUSY; | |
2367 | done = true; | |
2368 | } else { | |
2369 | done = !mq->rw_wait; | |
2370 | } | |
81196976 AH |
2371 | mq->waiting = !done; |
2372 | spin_unlock_irqrestore(q->queue_lock, flags); | |
2373 | ||
2374 | return done; | |
2375 | } | |
2376 | ||
2377 | static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req) | |
2378 | { | |
2379 | int err = 0; | |
2380 | ||
2381 | wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err)); | |
2382 | ||
2383 | /* Always complete the previous request if there is one */ | |
2384 | mmc_blk_mq_complete_prev_req(mq, prev_req); | |
2385 | ||
2386 | return err; | |
2387 | } | |
2388 | ||
2389 | static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq, | |
2390 | struct request *req) | |
2391 | { | |
2392 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
2393 | struct mmc_host *host = mq->card->host; | |
2394 | struct request *prev_req = NULL; | |
2395 | int err = 0; | |
2396 | ||
2397 | mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq); | |
2398 | ||
2399 | mqrq->brq.mrq.done = mmc_blk_mq_req_done; | |
2400 | ||
2401 | mmc_pre_req(host, &mqrq->brq.mrq); | |
2402 | ||
2403 | err = mmc_blk_rw_wait(mq, &prev_req); | |
2404 | if (err) | |
2405 | goto out_post_req; | |
2406 | ||
2407 | mq->rw_wait = true; | |
2408 | ||
2409 | err = mmc_start_request(host, &mqrq->brq.mrq); | |
2410 | ||
2411 | if (prev_req) | |
2412 | mmc_blk_mq_post_req(mq, prev_req); | |
2413 | ||
10f21df4 | 2414 | if (err) |
81196976 | 2415 | mq->rw_wait = false; |
10f21df4 AH |
2416 | |
2417 | /* Release re-tuning here where there is no synchronization required */ | |
2418 | if (err || mmc_host_done_complete(host)) | |
81196976 | 2419 | mmc_retune_release(host); |
81196976 AH |
2420 | |
2421 | out_post_req: | |
2422 | if (err) | |
2423 | mmc_post_req(host, &mqrq->brq.mrq, err); | |
2424 | ||
2425 | return err; | |
2426 | } | |
2427 | ||
2428 | static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host) | |
2429 | { | |
1e8e55b6 AH |
2430 | if (mq->use_cqe) |
2431 | return host->cqe_ops->cqe_wait_for_idle(host); | |
2432 | ||
81196976 AH |
2433 | return mmc_blk_rw_wait(mq, NULL); |
2434 | } | |
2435 | ||
2436 | enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req) | |
2437 | { | |
2438 | struct mmc_blk_data *md = mq->blkdata; | |
2439 | struct mmc_card *card = md->queue.card; | |
2440 | struct mmc_host *host = card->host; | |
2441 | int ret; | |
2442 | ||
2443 | ret = mmc_blk_part_switch(card, md->part_type); | |
2444 | if (ret) | |
2445 | return MMC_REQ_FAILED_TO_START; | |
2446 | ||
2447 | switch (mmc_issue_type(mq, req)) { | |
2448 | case MMC_ISSUE_SYNC: | |
2449 | ret = mmc_blk_wait_for_idle(mq, host); | |
2450 | if (ret) | |
2451 | return MMC_REQ_BUSY; | |
2452 | switch (req_op(req)) { | |
2453 | case REQ_OP_DRV_IN: | |
2454 | case REQ_OP_DRV_OUT: | |
2455 | mmc_blk_issue_drv_op(mq, req); | |
2456 | break; | |
2457 | case REQ_OP_DISCARD: | |
2458 | mmc_blk_issue_discard_rq(mq, req); | |
2459 | break; | |
2460 | case REQ_OP_SECURE_ERASE: | |
2461 | mmc_blk_issue_secdiscard_rq(mq, req); | |
2462 | break; | |
2463 | case REQ_OP_FLUSH: | |
2464 | mmc_blk_issue_flush(mq, req); | |
2465 | break; | |
2466 | default: | |
2467 | WARN_ON_ONCE(1); | |
2468 | return MMC_REQ_FAILED_TO_START; | |
2469 | } | |
2470 | return MMC_REQ_FINISHED; | |
1e8e55b6 | 2471 | case MMC_ISSUE_DCMD: |
81196976 AH |
2472 | case MMC_ISSUE_ASYNC: |
2473 | switch (req_op(req)) { | |
1e8e55b6 AH |
2474 | case REQ_OP_FLUSH: |
2475 | ret = mmc_blk_cqe_issue_flush(mq, req); | |
2476 | break; | |
81196976 AH |
2477 | case REQ_OP_READ: |
2478 | case REQ_OP_WRITE: | |
1e8e55b6 AH |
2479 | if (mq->use_cqe) |
2480 | ret = mmc_blk_cqe_issue_rw_rq(mq, req); | |
2481 | else | |
2482 | ret = mmc_blk_mq_issue_rw_rq(mq, req); | |
81196976 AH |
2483 | break; |
2484 | default: | |
2485 | WARN_ON_ONCE(1); | |
2486 | ret = -EINVAL; | |
2487 | } | |
2488 | if (!ret) | |
2489 | return MMC_REQ_STARTED; | |
2490 | return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START; | |
2491 | default: | |
2492 | WARN_ON_ONCE(1); | |
2493 | return MMC_REQ_FAILED_TO_START; | |
2494 | } | |
2495 | } | |
2496 | ||
0e65f10c LW |
2497 | static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card, |
2498 | struct mmc_blk_request *brq, struct request *req, | |
2499 | bool old_req_pending) | |
67716327 | 2500 | { |
0e65f10c LW |
2501 | bool req_pending; |
2502 | ||
67716327 AH |
2503 | /* |
2504 | * If this is an SD card and we're writing, we can first | |
2505 | * mark the known good sectors as ok. | |
2506 | * | |
2507 | * If the card is not SD, we can still ok written sectors | |
2508 | * as reported by the controller (which might be less than | |
2509 | * the real number of written sectors, but never more). | |
2510 | */ | |
2511 | if (mmc_card_sd(card)) { | |
2512 | u32 blocks; | |
169f03a0 | 2513 | int err; |
67716327 | 2514 | |
169f03a0 | 2515 | err = mmc_sd_num_wr_blocks(card, &blocks); |
0e65f10c LW |
2516 | if (err) |
2517 | req_pending = old_req_pending; | |
2518 | else | |
a7c17d8a | 2519 | req_pending = blk_end_request(req, BLK_STS_OK, blocks << 9); |
5dd784d2 | 2520 | } else { |
a7c17d8a | 2521 | req_pending = blk_end_request(req, BLK_STS_OK, brq->data.bytes_xfered); |
ce39f9d1 | 2522 | } |
0e65f10c | 2523 | return req_pending; |
ce39f9d1 SJ |
2524 | } |
2525 | ||
cdf8a6fb AH |
2526 | static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card, |
2527 | struct request *req, | |
2528 | struct mmc_queue_req *mqrq) | |
4e1f7800 | 2529 | { |
4e1f7800 LW |
2530 | if (mmc_card_removed(card)) |
2531 | req->rq_flags |= RQF_QUIET; | |
2a842aca | 2532 | while (blk_end_request(req, BLK_STS_IOERR, blk_rq_cur_bytes(req))); |
304419d8 | 2533 | mq->qcnt--; |
4e1f7800 LW |
2534 | } |
2535 | ||
b2928e10 LW |
2536 | /** |
2537 | * mmc_blk_rw_try_restart() - tries to restart the current async request | |
2538 | * @mq: the queue with the card and host to restart | |
2539 | * @req: a new request that want to be started after the current one | |
2540 | */ | |
8ddfe07e AH |
2541 | static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req, |
2542 | struct mmc_queue_req *mqrq) | |
efb5a05e LW |
2543 | { |
2544 | if (!req) | |
2545 | return; | |
2546 | ||
b2928e10 LW |
2547 | /* |
2548 | * If the card was removed, just cancel everything and return. | |
2549 | */ | |
2550 | if (mmc_card_removed(mq->card)) { | |
efb5a05e | 2551 | req->rq_flags |= RQF_QUIET; |
2a842aca | 2552 | blk_end_request_all(req, BLK_STS_IOERR); |
304419d8 | 2553 | mq->qcnt--; /* FIXME: just set to 0? */ |
b2928e10 | 2554 | return; |
efb5a05e | 2555 | } |
b2928e10 | 2556 | /* Else proceed and try to restart the current async request */ |
8ddfe07e AH |
2557 | mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq); |
2558 | mmc_start_areq(mq->card->host, &mqrq->areq, NULL); | |
efb5a05e LW |
2559 | } |
2560 | ||
acd8dbd6 | 2561 | static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req) |
54d49d77 | 2562 | { |
7db3028e | 2563 | struct mmc_blk_data *md = mq->blkdata; |
54d49d77 | 2564 | struct mmc_card *card = md->queue.card; |
5be80375 | 2565 | struct mmc_blk_request *brq; |
0e65f10c | 2566 | int disable_multi = 0, retry = 0, type, retune_retry_done = 0; |
d78d4a8a | 2567 | enum mmc_blk_status status; |
cdf8a6fb | 2568 | struct mmc_queue_req *mqrq_cur = NULL; |
ee8a43a5 | 2569 | struct mmc_queue_req *mq_rq; |
acd8dbd6 | 2570 | struct request *old_req; |
7d552a48 LW |
2571 | struct mmc_async_req *new_areq; |
2572 | struct mmc_async_req *old_areq; | |
0e65f10c | 2573 | bool req_pending = true; |
1da177e4 | 2574 | |
cdf8a6fb | 2575 | if (new_req) { |
304419d8 LW |
2576 | mqrq_cur = req_to_mmc_queue_req(new_req); |
2577 | mq->qcnt++; | |
cdf8a6fb AH |
2578 | } |
2579 | ||
2580 | if (!mq->qcnt) | |
df061588 | 2581 | return; |
98ccf149 | 2582 | |
ee8a43a5 | 2583 | do { |
acd8dbd6 | 2584 | if (new_req) { |
a5075eb9 SD |
2585 | /* |
2586 | * When 4KB native sector is enabled, only 8 blocks | |
2587 | * multiple read or write is allowed | |
2588 | */ | |
e87c8561 | 2589 | if (mmc_large_sector(card) && |
acd8dbd6 | 2590 | !IS_ALIGNED(blk_rq_sectors(new_req), 8)) { |
a5075eb9 | 2591 | pr_err("%s: Transfer size is not 4KB sector size aligned\n", |
acd8dbd6 | 2592 | new_req->rq_disk->disk_name); |
cdf8a6fb | 2593 | mmc_blk_rw_cmd_abort(mq, card, new_req, mqrq_cur); |
df061588 | 2594 | return; |
a5075eb9 | 2595 | } |
ce39f9d1 | 2596 | |
8ddfe07e AH |
2597 | mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq); |
2598 | new_areq = &mqrq_cur->areq; | |
ee8a43a5 | 2599 | } else |
7d552a48 LW |
2600 | new_areq = NULL; |
2601 | ||
c3399ef5 | 2602 | old_areq = mmc_start_areq(card->host, new_areq, &status); |
7d552a48 | 2603 | if (!old_areq) { |
da0dbaff LW |
2604 | /* |
2605 | * We have just put the first request into the pipeline | |
2606 | * and there is nothing more to do until it is | |
2607 | * complete. | |
2608 | */ | |
df061588 | 2609 | return; |
2220eedf | 2610 | } |
ee8a43a5 | 2611 | |
da0dbaff LW |
2612 | /* |
2613 | * An asynchronous request has been completed and we proceed | |
2614 | * to handle the result of it. | |
2615 | */ | |
74f5ba35 | 2616 | mq_rq = container_of(old_areq, struct mmc_queue_req, areq); |
ee8a43a5 | 2617 | brq = &mq_rq->brq; |
67e69d52 | 2618 | old_req = mmc_queue_req_to_req(mq_rq); |
acd8dbd6 | 2619 | type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; |
98ccf149 | 2620 | |
d78d4a8a PF |
2621 | switch (status) { |
2622 | case MMC_BLK_SUCCESS: | |
2623 | case MMC_BLK_PARTIAL: | |
2624 | /* | |
6d3898a6 AH |
2625 | * Reset success, and accept bytes_xfered. For |
2626 | * MMC_BLK_PARTIAL re-submit the remaining request. For | |
2627 | * MMC_BLK_SUCCESS error out the remaining request (it | |
2628 | * could not be re-submitted anyway if a next request | |
2629 | * had already begun). | |
d78d4a8a | 2630 | */ |
67716327 | 2631 | mmc_blk_reset_success(md, type); |
ce39f9d1 | 2632 | |
2a842aca | 2633 | req_pending = blk_end_request(old_req, BLK_STS_OK, |
0e65f10c | 2634 | brq->data.bytes_xfered); |
67716327 AH |
2635 | /* |
2636 | * If the blk_end_request function returns non-zero even | |
2637 | * though all data has been transferred and no errors | |
2638 | * were returned by the host controller, it's a bug. | |
2639 | */ | |
0e65f10c | 2640 | if (status == MMC_BLK_SUCCESS && req_pending) { |
a3c76eb9 | 2641 | pr_err("%s BUG rq_tot %d d_xfer %d\n", |
acd8dbd6 | 2642 | __func__, blk_rq_bytes(old_req), |
ee8a43a5 | 2643 | brq->data.bytes_xfered); |
cdf8a6fb | 2644 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
df061588 | 2645 | return; |
ee8a43a5 | 2646 | } |
d78d4a8a PF |
2647 | break; |
2648 | case MMC_BLK_CMD_ERR: | |
6d3898a6 AH |
2649 | /* |
2650 | * For SD cards, get bytes written, but do not accept | |
2651 | * bytes_xfered if that fails. For MMC cards accept | |
2652 | * bytes_xfered. Then try to reset. If reset fails then | |
2653 | * error out the remaining request, otherwise retry | |
2654 | * once (N.B mmc_blk_reset() will not succeed twice in a | |
2655 | * row). | |
2656 | */ | |
0e65f10c | 2657 | req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending); |
db435505 | 2658 | if (mmc_blk_reset(md, card->host, type)) { |
8ecc3444 | 2659 | if (req_pending) |
cdf8a6fb AH |
2660 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
2661 | else | |
304419d8 | 2662 | mq->qcnt--; |
8ddfe07e | 2663 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
db435505 LW |
2664 | return; |
2665 | } | |
0e65f10c | 2666 | if (!req_pending) { |
304419d8 | 2667 | mq->qcnt--; |
8ddfe07e | 2668 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
db435505 LW |
2669 | return; |
2670 | } | |
29535f7b | 2671 | break; |
d78d4a8a | 2672 | case MMC_BLK_RETRY: |
6d3898a6 AH |
2673 | /* |
2674 | * Do not accept bytes_xfered, but retry up to 5 times, | |
2675 | * otherwise same as abort. | |
2676 | */ | |
b8360a49 | 2677 | retune_retry_done = brq->retune_retry_done; |
d78d4a8a | 2678 | if (retry++ < 5) |
a01f3ccf | 2679 | break; |
67716327 | 2680 | /* Fall through */ |
d78d4a8a | 2681 | case MMC_BLK_ABORT: |
6d3898a6 AH |
2682 | /* |
2683 | * Do not accept bytes_xfered, but try to reset. If | |
2684 | * reset succeeds, try once more, otherwise error out | |
2685 | * the request. | |
2686 | */ | |
67716327 AH |
2687 | if (!mmc_blk_reset(md, card->host, type)) |
2688 | break; | |
cdf8a6fb | 2689 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
8ddfe07e | 2690 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
db435505 | 2691 | return; |
67716327 AH |
2692 | case MMC_BLK_DATA_ERR: { |
2693 | int err; | |
2694 | ||
6d3898a6 AH |
2695 | /* |
2696 | * Do not accept bytes_xfered, but try to reset. If | |
2697 | * reset succeeds, try once more. If reset fails with | |
2698 | * ENODEV which means the partition is wrong, then error | |
2699 | * out the request. Otherwise attempt to read one sector | |
2700 | * at a time. | |
2701 | */ | |
67716327 AH |
2702 | err = mmc_blk_reset(md, card->host, type); |
2703 | if (!err) | |
2704 | break; | |
db435505 | 2705 | if (err == -ENODEV) { |
cdf8a6fb | 2706 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
8ddfe07e | 2707 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
db435505 LW |
2708 | return; |
2709 | } | |
67716327 AH |
2710 | /* Fall through */ |
2711 | } | |
2712 | case MMC_BLK_ECC_ERR: | |
6d3898a6 AH |
2713 | /* |
2714 | * Do not accept bytes_xfered. If reading more than one | |
2715 | * sector, try reading one sector at a time. | |
2716 | */ | |
67716327 AH |
2717 | if (brq->data.blocks > 1) { |
2718 | /* Redo read one sector at a time */ | |
6606110d | 2719 | pr_warn("%s: retrying using single block read\n", |
acd8dbd6 | 2720 | old_req->rq_disk->disk_name); |
67716327 AH |
2721 | disable_multi = 1; |
2722 | break; | |
2723 | } | |
d78d4a8a PF |
2724 | /* |
2725 | * After an error, we redo I/O one sector at a | |
2726 | * time, so we only reach here after trying to | |
2727 | * read a single sector. | |
2728 | */ | |
2a842aca | 2729 | req_pending = blk_end_request(old_req, BLK_STS_IOERR, |
0e65f10c LW |
2730 | brq->data.blksz); |
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 | } | |
d78d4a8a | 2736 | break; |
a8ad82cc | 2737 | case MMC_BLK_NOMEDIUM: |
6d3898a6 | 2738 | /* Do not accept bytes_xfered. Error out the request */ |
cdf8a6fb | 2739 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
8ddfe07e | 2740 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
db435505 | 2741 | return; |
2220eedf | 2742 | default: |
6d3898a6 | 2743 | /* Do not accept bytes_xfered. Error out the request */ |
2220eedf | 2744 | pr_err("%s: Unhandled return value (%d)", |
acd8dbd6 | 2745 | old_req->rq_disk->disk_name, status); |
cdf8a6fb | 2746 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
8ddfe07e | 2747 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
db435505 | 2748 | return; |
4c2b8f26 RKAL |
2749 | } |
2750 | ||
0e65f10c | 2751 | if (req_pending) { |
03d640ae LW |
2752 | /* |
2753 | * In case of a incomplete request | |
2754 | * prepare it again and resend. | |
2755 | */ | |
2756 | mmc_blk_rw_rq_prep(mq_rq, card, | |
2757 | disable_multi, mq); | |
c3399ef5 | 2758 | mmc_start_areq(card->host, |
74f5ba35 | 2759 | &mq_rq->areq, NULL); |
b8360a49 | 2760 | mq_rq->brq.retune_retry_done = retune_retry_done; |
ee8a43a5 | 2761 | } |
0e65f10c | 2762 | } while (req_pending); |
cdf8a6fb | 2763 | |
304419d8 | 2764 | mq->qcnt--; |
1da177e4 LT |
2765 | } |
2766 | ||
df061588 | 2767 | void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) |
bd788c96 | 2768 | { |
1a258db6 | 2769 | int ret; |
7db3028e | 2770 | struct mmc_blk_data *md = mq->blkdata; |
1a258db6 AW |
2771 | struct mmc_card *card = md->queue.card; |
2772 | ||
cdf8a6fb | 2773 | if (req && !mq->qcnt) |
ee8a43a5 | 2774 | /* claim host only for the first request */ |
6c0cedd1 | 2775 | mmc_get_card(card, NULL); |
ee8a43a5 | 2776 | |
1f797edc | 2777 | ret = mmc_blk_part_switch(card, md->part_type); |
371a689f | 2778 | if (ret) { |
0d7d85ca | 2779 | if (req) { |
2a842aca | 2780 | blk_end_request_all(req, BLK_STS_IOERR); |
0d7d85ca | 2781 | } |
371a689f AW |
2782 | goto out; |
2783 | } | |
1a258db6 | 2784 | |
614f0388 LW |
2785 | if (req) { |
2786 | switch (req_op(req)) { | |
2787 | case REQ_OP_DRV_IN: | |
2788 | case REQ_OP_DRV_OUT: | |
2789 | /* | |
2790 | * Complete ongoing async transfer before issuing | |
2791 | * ioctl()s | |
2792 | */ | |
2793 | if (mq->qcnt) | |
2794 | mmc_blk_issue_rw_rq(mq, NULL); | |
02166a01 | 2795 | mmc_blk_issue_drv_op(mq, req); |
614f0388 LW |
2796 | break; |
2797 | case REQ_OP_DISCARD: | |
2798 | /* | |
2799 | * Complete ongoing async transfer before issuing | |
2800 | * discard. | |
2801 | */ | |
2802 | if (mq->qcnt) | |
2803 | mmc_blk_issue_rw_rq(mq, NULL); | |
2804 | mmc_blk_issue_discard_rq(mq, req); | |
2805 | break; | |
2806 | case REQ_OP_SECURE_ERASE: | |
2807 | /* | |
2808 | * Complete ongoing async transfer before issuing | |
2809 | * secure erase. | |
2810 | */ | |
2811 | if (mq->qcnt) | |
2812 | mmc_blk_issue_rw_rq(mq, NULL); | |
2813 | mmc_blk_issue_secdiscard_rq(mq, req); | |
2814 | break; | |
2815 | case REQ_OP_FLUSH: | |
2816 | /* | |
2817 | * Complete ongoing async transfer before issuing | |
2818 | * flush. | |
2819 | */ | |
2820 | if (mq->qcnt) | |
2821 | mmc_blk_issue_rw_rq(mq, NULL); | |
2822 | mmc_blk_issue_flush(mq, req); | |
2823 | break; | |
2824 | default: | |
2825 | /* Normal request, just issue it */ | |
2826 | mmc_blk_issue_rw_rq(mq, req); | |
2827 | card->host->context_info.is_waiting_last_req = false; | |
2828 | break; | |
7322238f | 2829 | } |
49804548 | 2830 | } else { |
614f0388 LW |
2831 | /* No request, flushing the pipeline with NULL */ |
2832 | mmc_blk_issue_rw_rq(mq, NULL); | |
2602b740 | 2833 | card->host->context_info.is_waiting_last_req = false; |
49804548 | 2834 | } |
1a258db6 | 2835 | |
371a689f | 2836 | out: |
cdf8a6fb | 2837 | if (!mq->qcnt) |
6c0cedd1 | 2838 | mmc_put_card(card, NULL); |
bd788c96 | 2839 | } |
1da177e4 | 2840 | |
a6f6c96b RK |
2841 | static inline int mmc_blk_readonly(struct mmc_card *card) |
2842 | { | |
2843 | return mmc_card_readonly(card) || | |
2844 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); | |
2845 | } | |
2846 | ||
371a689f AW |
2847 | static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, |
2848 | struct device *parent, | |
2849 | sector_t size, | |
2850 | bool default_ro, | |
add710ea JR |
2851 | const char *subname, |
2852 | int area_type) | |
1da177e4 LT |
2853 | { |
2854 | struct mmc_blk_data *md; | |
2855 | int devidx, ret; | |
2856 | ||
a04848c7 | 2857 | devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL); |
e7b42769 SL |
2858 | if (devidx < 0) { |
2859 | /* | |
2860 | * We get -ENOSPC because there are no more any available | |
2861 | * devidx. The reason may be that, either userspace haven't yet | |
2862 | * unmounted the partitions, which postpones mmc_blk_release() | |
2863 | * from being called, or the device has more partitions than | |
2864 | * what we support. | |
2865 | */ | |
2866 | if (devidx == -ENOSPC) | |
2867 | dev_err(mmc_dev(card->host), | |
2868 | "no more device IDs available\n"); | |
2869 | ||
a04848c7 | 2870 | return ERR_PTR(devidx); |
e7b42769 | 2871 | } |
1da177e4 | 2872 | |
dd00cc48 | 2873 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); |
a6f6c96b RK |
2874 | if (!md) { |
2875 | ret = -ENOMEM; | |
2876 | goto out; | |
2877 | } | |
1da177e4 | 2878 | |
add710ea JR |
2879 | md->area_type = area_type; |
2880 | ||
a6f6c96b RK |
2881 | /* |
2882 | * Set the read-only status based on the supported commands | |
2883 | * and the write protect switch. | |
2884 | */ | |
2885 | md->read_only = mmc_blk_readonly(card); | |
1da177e4 | 2886 | |
5e71b7a6 | 2887 | md->disk = alloc_disk(perdev_minors); |
a6f6c96b RK |
2888 | if (md->disk == NULL) { |
2889 | ret = -ENOMEM; | |
2890 | goto err_kfree; | |
2891 | } | |
1da177e4 | 2892 | |
a6f6c96b | 2893 | spin_lock_init(&md->lock); |
371a689f | 2894 | INIT_LIST_HEAD(&md->part); |
97548575 | 2895 | INIT_LIST_HEAD(&md->rpmbs); |
a6f6c96b | 2896 | md->usage = 1; |
1da177e4 | 2897 | |
d09408ad | 2898 | ret = mmc_init_queue(&md->queue, card, &md->lock, subname); |
a6f6c96b RK |
2899 | if (ret) |
2900 | goto err_putdisk; | |
1da177e4 | 2901 | |
7db3028e | 2902 | md->queue.blkdata = md; |
d2b18394 | 2903 | |
41e3efd0 AH |
2904 | /* |
2905 | * Keep an extra reference to the queue so that we can shutdown the | |
2906 | * queue (i.e. call blk_cleanup_queue()) while there are still | |
2907 | * references to the 'md'. The corresponding blk_put_queue() is in | |
2908 | * mmc_blk_put(). | |
2909 | */ | |
2910 | if (!blk_get_queue(md->queue.queue)) { | |
2911 | mmc_cleanup_queue(&md->queue); | |
2912 | goto err_putdisk; | |
2913 | } | |
2914 | ||
fe6b4c88 | 2915 | md->disk->major = MMC_BLOCK_MAJOR; |
5e71b7a6 | 2916 | md->disk->first_minor = devidx * perdev_minors; |
a6f6c96b RK |
2917 | md->disk->fops = &mmc_bdops; |
2918 | md->disk->private_data = md; | |
2919 | md->disk->queue = md->queue.queue; | |
307d8e6f | 2920 | md->parent = parent; |
371a689f | 2921 | set_disk_ro(md->disk, md->read_only || default_ro); |
382c55f8 | 2922 | md->disk->flags = GENHD_FL_EXT_DEVT; |
f5b4d71f | 2923 | if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT)) |
53d8f974 | 2924 | md->disk->flags |= GENHD_FL_NO_PART_SCAN; |
a6f6c96b RK |
2925 | |
2926 | /* | |
2927 | * As discussed on lkml, GENHD_FL_REMOVABLE should: | |
2928 | * | |
2929 | * - be set for removable media with permanent block devices | |
2930 | * - be unset for removable block devices with permanent media | |
2931 | * | |
2932 | * Since MMC block devices clearly fall under the second | |
2933 | * case, we do not set GENHD_FL_REMOVABLE. Userspace | |
2934 | * should use the block device creation/destruction hotplug | |
2935 | * messages to tell when the card is present. | |
2936 | */ | |
2937 | ||
f06c9153 | 2938 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
9aaf3437 | 2939 | "mmcblk%u%s", card->host->index, subname ? subname : ""); |
a6f6c96b | 2940 | |
a5075eb9 SD |
2941 | if (mmc_card_mmc(card)) |
2942 | blk_queue_logical_block_size(md->queue.queue, | |
2943 | card->ext_csd.data_sector_size); | |
2944 | else | |
2945 | blk_queue_logical_block_size(md->queue.queue, 512); | |
2946 | ||
371a689f | 2947 | set_capacity(md->disk, size); |
d0c97cfb | 2948 | |
f0d89972 | 2949 | if (mmc_host_cmd23(card->host)) { |
0ed50abb DG |
2950 | if ((mmc_card_mmc(card) && |
2951 | card->csd.mmca_vsn >= CSD_SPEC_VER_3) || | |
f0d89972 AW |
2952 | (mmc_card_sd(card) && |
2953 | card->scr.cmds & SD_SCR_CMD23_SUPPORT)) | |
2954 | md->flags |= MMC_BLK_CMD23; | |
2955 | } | |
d0c97cfb AW |
2956 | |
2957 | if (mmc_card_mmc(card) && | |
2958 | md->flags & MMC_BLK_CMD23 && | |
2959 | ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || | |
2960 | card->ext_csd.rel_sectors)) { | |
2961 | md->flags |= MMC_BLK_REL_WR; | |
e9d5c746 | 2962 | blk_queue_write_cache(md->queue.queue, true, true); |
d0c97cfb AW |
2963 | } |
2964 | ||
371a689f AW |
2965 | return md; |
2966 | ||
2967 | err_putdisk: | |
2968 | put_disk(md->disk); | |
2969 | err_kfree: | |
2970 | kfree(md); | |
2971 | out: | |
a04848c7 | 2972 | ida_simple_remove(&mmc_blk_ida, devidx); |
371a689f AW |
2973 | return ERR_PTR(ret); |
2974 | } | |
2975 | ||
2976 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) | |
2977 | { | |
2978 | sector_t size; | |
a6f6c96b | 2979 | |
85a18ad9 PO |
2980 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { |
2981 | /* | |
2982 | * The EXT_CSD sector count is in number or 512 byte | |
2983 | * sectors. | |
2984 | */ | |
371a689f | 2985 | size = card->ext_csd.sectors; |
85a18ad9 PO |
2986 | } else { |
2987 | /* | |
2988 | * The CSD capacity field is in units of read_blkbits. | |
2989 | * set_capacity takes units of 512 bytes. | |
2990 | */ | |
087de9ed KM |
2991 | size = (typeof(sector_t))card->csd.capacity |
2992 | << (card->csd.read_blkbits - 9); | |
85a18ad9 | 2993 | } |
371a689f | 2994 | |
7a30f2af | 2995 | return mmc_blk_alloc_req(card, &card->dev, size, false, NULL, |
add710ea | 2996 | MMC_BLK_DATA_AREA_MAIN); |
371a689f | 2997 | } |
a6f6c96b | 2998 | |
371a689f AW |
2999 | static int mmc_blk_alloc_part(struct mmc_card *card, |
3000 | struct mmc_blk_data *md, | |
3001 | unsigned int part_type, | |
3002 | sector_t size, | |
3003 | bool default_ro, | |
add710ea JR |
3004 | const char *subname, |
3005 | int area_type) | |
371a689f AW |
3006 | { |
3007 | char cap_str[10]; | |
3008 | struct mmc_blk_data *part_md; | |
3009 | ||
3010 | part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro, | |
add710ea | 3011 | subname, area_type); |
371a689f AW |
3012 | if (IS_ERR(part_md)) |
3013 | return PTR_ERR(part_md); | |
3014 | part_md->part_type = part_type; | |
3015 | list_add(&part_md->part, &md->part); | |
3016 | ||
b9f28d86 | 3017 | string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2, |
371a689f | 3018 | cap_str, sizeof(cap_str)); |
a3c76eb9 | 3019 | pr_info("%s: %s %s partition %u %s\n", |
371a689f AW |
3020 | part_md->disk->disk_name, mmc_card_id(card), |
3021 | mmc_card_name(card), part_md->part_type, cap_str); | |
3022 | return 0; | |
3023 | } | |
3024 | ||
97548575 LW |
3025 | /** |
3026 | * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev | |
3027 | * @filp: the character device file | |
3028 | * @cmd: the ioctl() command | |
3029 | * @arg: the argument from userspace | |
3030 | * | |
3031 | * This will essentially just redirect the ioctl()s coming in over to | |
3032 | * the main block device spawning the RPMB character device. | |
3033 | */ | |
3034 | static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd, | |
3035 | unsigned long arg) | |
3036 | { | |
3037 | struct mmc_rpmb_data *rpmb = filp->private_data; | |
3038 | int ret; | |
3039 | ||
3040 | switch (cmd) { | |
3041 | case MMC_IOC_CMD: | |
3042 | ret = mmc_blk_ioctl_cmd(rpmb->md, | |
3043 | (struct mmc_ioc_cmd __user *)arg, | |
3044 | rpmb); | |
3045 | break; | |
3046 | case MMC_IOC_MULTI_CMD: | |
3047 | ret = mmc_blk_ioctl_multi_cmd(rpmb->md, | |
3048 | (struct mmc_ioc_multi_cmd __user *)arg, | |
3049 | rpmb); | |
3050 | break; | |
3051 | default: | |
3052 | ret = -EINVAL; | |
3053 | break; | |
3054 | } | |
3055 | ||
3056 | return 0; | |
3057 | } | |
3058 | ||
3059 | #ifdef CONFIG_COMPAT | |
3060 | static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd, | |
3061 | unsigned long arg) | |
3062 | { | |
3063 | return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); | |
3064 | } | |
3065 | #endif | |
3066 | ||
3067 | static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp) | |
3068 | { | |
3069 | struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev, | |
3070 | struct mmc_rpmb_data, chrdev); | |
3071 | ||
3072 | get_device(&rpmb->dev); | |
3073 | filp->private_data = rpmb; | |
1c87f735 | 3074 | mmc_blk_get(rpmb->md->disk); |
97548575 LW |
3075 | |
3076 | return nonseekable_open(inode, filp); | |
3077 | } | |
3078 | ||
3079 | static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp) | |
3080 | { | |
3081 | struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev, | |
3082 | struct mmc_rpmb_data, chrdev); | |
3083 | ||
3084 | put_device(&rpmb->dev); | |
1c87f735 | 3085 | mmc_blk_put(rpmb->md); |
97548575 LW |
3086 | |
3087 | return 0; | |
3088 | } | |
3089 | ||
3090 | static const struct file_operations mmc_rpmb_fileops = { | |
3091 | .release = mmc_rpmb_chrdev_release, | |
3092 | .open = mmc_rpmb_chrdev_open, | |
3093 | .owner = THIS_MODULE, | |
3094 | .llseek = no_llseek, | |
3095 | .unlocked_ioctl = mmc_rpmb_ioctl, | |
3096 | #ifdef CONFIG_COMPAT | |
3097 | .compat_ioctl = mmc_rpmb_ioctl_compat, | |
3098 | #endif | |
3099 | }; | |
3100 | ||
1c87f735 LW |
3101 | static void mmc_blk_rpmb_device_release(struct device *dev) |
3102 | { | |
3103 | struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev); | |
3104 | ||
3105 | ida_simple_remove(&mmc_rpmb_ida, rpmb->id); | |
3106 | kfree(rpmb); | |
3107 | } | |
97548575 LW |
3108 | |
3109 | static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, | |
3110 | struct mmc_blk_data *md, | |
3111 | unsigned int part_index, | |
3112 | sector_t size, | |
3113 | const char *subname) | |
3114 | { | |
3115 | int devidx, ret; | |
3116 | char rpmb_name[DISK_NAME_LEN]; | |
3117 | char cap_str[10]; | |
3118 | struct mmc_rpmb_data *rpmb; | |
3119 | ||
3120 | /* This creates the minor number for the RPMB char device */ | |
3121 | devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL); | |
3122 | if (devidx < 0) | |
3123 | return devidx; | |
3124 | ||
3125 | rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL); | |
1c87f735 LW |
3126 | if (!rpmb) { |
3127 | ida_simple_remove(&mmc_rpmb_ida, devidx); | |
97548575 | 3128 | return -ENOMEM; |
1c87f735 | 3129 | } |
97548575 LW |
3130 | |
3131 | snprintf(rpmb_name, sizeof(rpmb_name), | |
3132 | "mmcblk%u%s", card->host->index, subname ? subname : ""); | |
3133 | ||
3134 | rpmb->id = devidx; | |
3135 | rpmb->part_index = part_index; | |
3136 | rpmb->dev.init_name = rpmb_name; | |
3137 | rpmb->dev.bus = &mmc_rpmb_bus_type; | |
3138 | rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id); | |
3139 | rpmb->dev.parent = &card->dev; | |
1c87f735 | 3140 | rpmb->dev.release = mmc_blk_rpmb_device_release; |
97548575 LW |
3141 | device_initialize(&rpmb->dev); |
3142 | dev_set_drvdata(&rpmb->dev, rpmb); | |
3143 | rpmb->md = md; | |
3144 | ||
3145 | cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops); | |
3146 | rpmb->chrdev.owner = THIS_MODULE; | |
3147 | ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev); | |
3148 | if (ret) { | |
3149 | pr_err("%s: could not add character device\n", rpmb_name); | |
1c87f735 | 3150 | goto out_put_device; |
97548575 LW |
3151 | } |
3152 | ||
3153 | list_add(&rpmb->node, &md->rpmbs); | |
3154 | ||
3155 | string_get_size((u64)size, 512, STRING_UNITS_2, | |
3156 | cap_str, sizeof(cap_str)); | |
3157 | ||
3158 | pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n", | |
3159 | rpmb_name, mmc_card_id(card), | |
3160 | mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str, | |
3161 | MAJOR(mmc_rpmb_devt), rpmb->id); | |
3162 | ||
3163 | return 0; | |
3164 | ||
1c87f735 LW |
3165 | out_put_device: |
3166 | put_device(&rpmb->dev); | |
97548575 LW |
3167 | return ret; |
3168 | } | |
3169 | ||
3170 | static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb) | |
1c87f735 | 3171 | |
97548575 LW |
3172 | { |
3173 | cdev_device_del(&rpmb->chrdev, &rpmb->dev); | |
1c87f735 | 3174 | put_device(&rpmb->dev); |
97548575 LW |
3175 | } |
3176 | ||
e0c368d5 NJ |
3177 | /* MMC Physical partitions consist of two boot partitions and |
3178 | * up to four general purpose partitions. | |
3179 | * For each partition enabled in EXT_CSD a block device will be allocatedi | |
3180 | * to provide access to the partition. | |
3181 | */ | |
3182 | ||
371a689f AW |
3183 | static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) |
3184 | { | |
97548575 | 3185 | int idx, ret; |
371a689f AW |
3186 | |
3187 | if (!mmc_card_mmc(card)) | |
3188 | return 0; | |
3189 | ||
e0c368d5 | 3190 | for (idx = 0; idx < card->nr_parts; idx++) { |
97548575 LW |
3191 | if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) { |
3192 | /* | |
3193 | * RPMB partitions does not provide block access, they | |
3194 | * are only accessed using ioctl():s. Thus create | |
3195 | * special RPMB block devices that do not have a | |
3196 | * backing block queue for these. | |
3197 | */ | |
3198 | ret = mmc_blk_alloc_rpmb_part(card, md, | |
3199 | card->part[idx].part_cfg, | |
3200 | card->part[idx].size >> 9, | |
3201 | card->part[idx].name); | |
3202 | if (ret) | |
3203 | return ret; | |
3204 | } else if (card->part[idx].size) { | |
e0c368d5 NJ |
3205 | ret = mmc_blk_alloc_part(card, md, |
3206 | card->part[idx].part_cfg, | |
3207 | card->part[idx].size >> 9, | |
3208 | card->part[idx].force_ro, | |
add710ea JR |
3209 | card->part[idx].name, |
3210 | card->part[idx].area_type); | |
e0c368d5 NJ |
3211 | if (ret) |
3212 | return ret; | |
3213 | } | |
371a689f AW |
3214 | } |
3215 | ||
97548575 | 3216 | return 0; |
1da177e4 LT |
3217 | } |
3218 | ||
371a689f AW |
3219 | static void mmc_blk_remove_req(struct mmc_blk_data *md) |
3220 | { | |
add710ea JR |
3221 | struct mmc_card *card; |
3222 | ||
371a689f | 3223 | if (md) { |
fdfa20c1 PT |
3224 | /* |
3225 | * Flush remaining requests and free queues. It | |
3226 | * is freeing the queue that stops new requests | |
3227 | * from being accepted. | |
3228 | */ | |
8efb83a2 | 3229 | card = md->queue.card; |
fdfa20c1 | 3230 | mmc_cleanup_queue(&md->queue); |
371a689f AW |
3231 | if (md->disk->flags & GENHD_FL_UP) { |
3232 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); | |
add710ea JR |
3233 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && |
3234 | card->ext_csd.boot_ro_lockable) | |
3235 | device_remove_file(disk_to_dev(md->disk), | |
3236 | &md->power_ro_lock); | |
371a689f | 3237 | |
371a689f AW |
3238 | del_gendisk(md->disk); |
3239 | } | |
371a689f AW |
3240 | mmc_blk_put(md); |
3241 | } | |
3242 | } | |
3243 | ||
3244 | static void mmc_blk_remove_parts(struct mmc_card *card, | |
3245 | struct mmc_blk_data *md) | |
3246 | { | |
3247 | struct list_head *pos, *q; | |
3248 | struct mmc_blk_data *part_md; | |
97548575 | 3249 | struct mmc_rpmb_data *rpmb; |
371a689f | 3250 | |
97548575 LW |
3251 | /* Remove RPMB partitions */ |
3252 | list_for_each_safe(pos, q, &md->rpmbs) { | |
3253 | rpmb = list_entry(pos, struct mmc_rpmb_data, node); | |
3254 | list_del(pos); | |
3255 | mmc_blk_remove_rpmb_part(rpmb); | |
3256 | } | |
3257 | /* Remove block partitions */ | |
371a689f AW |
3258 | list_for_each_safe(pos, q, &md->part) { |
3259 | part_md = list_entry(pos, struct mmc_blk_data, part); | |
3260 | list_del(pos); | |
3261 | mmc_blk_remove_req(part_md); | |
3262 | } | |
3263 | } | |
3264 | ||
3265 | static int mmc_add_disk(struct mmc_blk_data *md) | |
3266 | { | |
3267 | int ret; | |
add710ea | 3268 | struct mmc_card *card = md->queue.card; |
371a689f | 3269 | |
307d8e6f | 3270 | device_add_disk(md->parent, md->disk); |
371a689f AW |
3271 | md->force_ro.show = force_ro_show; |
3272 | md->force_ro.store = force_ro_store; | |
641c3187 | 3273 | sysfs_attr_init(&md->force_ro.attr); |
371a689f AW |
3274 | md->force_ro.attr.name = "force_ro"; |
3275 | md->force_ro.attr.mode = S_IRUGO | S_IWUSR; | |
3276 | ret = device_create_file(disk_to_dev(md->disk), &md->force_ro); | |
3277 | if (ret) | |
add710ea JR |
3278 | goto force_ro_fail; |
3279 | ||
3280 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && | |
3281 | card->ext_csd.boot_ro_lockable) { | |
88187398 | 3282 | umode_t mode; |
add710ea JR |
3283 | |
3284 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS) | |
3285 | mode = S_IRUGO; | |
3286 | else | |
3287 | mode = S_IRUGO | S_IWUSR; | |
3288 | ||
3289 | md->power_ro_lock.show = power_ro_lock_show; | |
3290 | md->power_ro_lock.store = power_ro_lock_store; | |
00d9ac08 | 3291 | sysfs_attr_init(&md->power_ro_lock.attr); |
add710ea JR |
3292 | md->power_ro_lock.attr.mode = mode; |
3293 | md->power_ro_lock.attr.name = | |
3294 | "ro_lock_until_next_power_on"; | |
3295 | ret = device_create_file(disk_to_dev(md->disk), | |
3296 | &md->power_ro_lock); | |
3297 | if (ret) | |
3298 | goto power_ro_lock_fail; | |
3299 | } | |
3300 | return ret; | |
3301 | ||
3302 | power_ro_lock_fail: | |
3303 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); | |
3304 | force_ro_fail: | |
3305 | del_gendisk(md->disk); | |
371a689f AW |
3306 | |
3307 | return ret; | |
3308 | } | |
3309 | ||
627c3ccf LW |
3310 | #ifdef CONFIG_DEBUG_FS |
3311 | ||
3312 | static int mmc_dbg_card_status_get(void *data, u64 *val) | |
3313 | { | |
3314 | struct mmc_card *card = data; | |
3315 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); | |
3316 | struct mmc_queue *mq = &md->queue; | |
3317 | struct request *req; | |
3318 | int ret; | |
3319 | ||
3320 | /* Ask the block layer about the card status */ | |
3321 | req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM); | |
fb8e456e AH |
3322 | if (IS_ERR(req)) |
3323 | return PTR_ERR(req); | |
627c3ccf LW |
3324 | req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS; |
3325 | blk_execute_rq(mq->queue, NULL, req, 0); | |
3326 | ret = req_to_mmc_queue_req(req)->drv_op_result; | |
3327 | if (ret >= 0) { | |
3328 | *val = ret; | |
3329 | ret = 0; | |
3330 | } | |
34c089e8 | 3331 | blk_put_request(req); |
627c3ccf LW |
3332 | |
3333 | return ret; | |
3334 | } | |
3335 | DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get, | |
3336 | NULL, "%08llx\n"); | |
3337 | ||
3338 | /* That is two digits * 512 + 1 for newline */ | |
3339 | #define EXT_CSD_STR_LEN 1025 | |
3340 | ||
3341 | static int mmc_ext_csd_open(struct inode *inode, struct file *filp) | |
3342 | { | |
3343 | struct mmc_card *card = inode->i_private; | |
3344 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); | |
3345 | struct mmc_queue *mq = &md->queue; | |
3346 | struct request *req; | |
3347 | char *buf; | |
3348 | ssize_t n = 0; | |
3349 | u8 *ext_csd; | |
3350 | int err, i; | |
3351 | ||
3352 | buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL); | |
3353 | if (!buf) | |
3354 | return -ENOMEM; | |
3355 | ||
3356 | /* Ask the block layer for the EXT CSD */ | |
3357 | req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM); | |
fb8e456e AH |
3358 | if (IS_ERR(req)) { |
3359 | err = PTR_ERR(req); | |
3360 | goto out_free; | |
3361 | } | |
627c3ccf LW |
3362 | req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD; |
3363 | req_to_mmc_queue_req(req)->drv_op_data = &ext_csd; | |
3364 | blk_execute_rq(mq->queue, NULL, req, 0); | |
3365 | err = req_to_mmc_queue_req(req)->drv_op_result; | |
34c089e8 | 3366 | blk_put_request(req); |
627c3ccf LW |
3367 | if (err) { |
3368 | pr_err("FAILED %d\n", err); | |
3369 | goto out_free; | |
3370 | } | |
3371 | ||
3372 | for (i = 0; i < 512; i++) | |
3373 | n += sprintf(buf + n, "%02x", ext_csd[i]); | |
3374 | n += sprintf(buf + n, "\n"); | |
3375 | ||
3376 | if (n != EXT_CSD_STR_LEN) { | |
3377 | err = -EINVAL; | |
3378 | goto out_free; | |
3379 | } | |
3380 | ||
3381 | filp->private_data = buf; | |
3382 | kfree(ext_csd); | |
3383 | return 0; | |
3384 | ||
3385 | out_free: | |
3386 | kfree(buf); | |
3387 | return err; | |
3388 | } | |
3389 | ||
3390 | static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf, | |
3391 | size_t cnt, loff_t *ppos) | |
3392 | { | |
3393 | char *buf = filp->private_data; | |
3394 | ||
3395 | return simple_read_from_buffer(ubuf, cnt, ppos, | |
3396 | buf, EXT_CSD_STR_LEN); | |
3397 | } | |
3398 | ||
3399 | static int mmc_ext_csd_release(struct inode *inode, struct file *file) | |
3400 | { | |
3401 | kfree(file->private_data); | |
3402 | return 0; | |
3403 | } | |
3404 | ||
3405 | static const struct file_operations mmc_dbg_ext_csd_fops = { | |
3406 | .open = mmc_ext_csd_open, | |
3407 | .read = mmc_ext_csd_read, | |
3408 | .release = mmc_ext_csd_release, | |
3409 | .llseek = default_llseek, | |
3410 | }; | |
3411 | ||
f9f0da98 | 3412 | static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md) |
627c3ccf LW |
3413 | { |
3414 | struct dentry *root; | |
3415 | ||
3416 | if (!card->debugfs_root) | |
3417 | return 0; | |
3418 | ||
3419 | root = card->debugfs_root; | |
3420 | ||
3421 | if (mmc_card_mmc(card) || mmc_card_sd(card)) { | |
f9f0da98 AH |
3422 | md->status_dentry = |
3423 | debugfs_create_file("status", S_IRUSR, root, card, | |
3424 | &mmc_dbg_card_status_fops); | |
3425 | if (!md->status_dentry) | |
627c3ccf LW |
3426 | return -EIO; |
3427 | } | |
3428 | ||
3429 | if (mmc_card_mmc(card)) { | |
f9f0da98 AH |
3430 | md->ext_csd_dentry = |
3431 | debugfs_create_file("ext_csd", S_IRUSR, root, card, | |
3432 | &mmc_dbg_ext_csd_fops); | |
3433 | if (!md->ext_csd_dentry) | |
627c3ccf LW |
3434 | return -EIO; |
3435 | } | |
3436 | ||
3437 | return 0; | |
3438 | } | |
3439 | ||
f9f0da98 AH |
3440 | static void mmc_blk_remove_debugfs(struct mmc_card *card, |
3441 | struct mmc_blk_data *md) | |
3442 | { | |
3443 | if (!card->debugfs_root) | |
3444 | return; | |
3445 | ||
3446 | if (!IS_ERR_OR_NULL(md->status_dentry)) { | |
3447 | debugfs_remove(md->status_dentry); | |
3448 | md->status_dentry = NULL; | |
3449 | } | |
3450 | ||
3451 | if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) { | |
3452 | debugfs_remove(md->ext_csd_dentry); | |
3453 | md->ext_csd_dentry = NULL; | |
3454 | } | |
3455 | } | |
627c3ccf LW |
3456 | |
3457 | #else | |
3458 | ||
f9f0da98 | 3459 | static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md) |
627c3ccf LW |
3460 | { |
3461 | return 0; | |
3462 | } | |
3463 | ||
f9f0da98 AH |
3464 | static void mmc_blk_remove_debugfs(struct mmc_card *card, |
3465 | struct mmc_blk_data *md) | |
3466 | { | |
3467 | } | |
3468 | ||
627c3ccf LW |
3469 | #endif /* CONFIG_DEBUG_FS */ |
3470 | ||
96541bac | 3471 | static int mmc_blk_probe(struct mmc_card *card) |
1da177e4 | 3472 | { |
371a689f | 3473 | struct mmc_blk_data *md, *part_md; |
a7bbb573 PO |
3474 | char cap_str[10]; |
3475 | ||
912490db PO |
3476 | /* |
3477 | * Check that the card supports the command class(es) we need. | |
3478 | */ | |
3479 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) | |
1da177e4 LT |
3480 | return -ENODEV; |
3481 | ||
8c7cdbf9 | 3482 | mmc_fixup_device(card, mmc_blk_fixups); |
5204d00f | 3483 | |
1da177e4 | 3484 | md = mmc_blk_alloc(card); |
304419d8 | 3485 | if (IS_ERR(md)) |
1da177e4 LT |
3486 | return PTR_ERR(md); |
3487 | ||
b9f28d86 | 3488 | string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2, |
a7bbb573 | 3489 | cap_str, sizeof(cap_str)); |
a3c76eb9 | 3490 | pr_info("%s: %s %s %s %s\n", |
1da177e4 | 3491 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), |
a7bbb573 | 3492 | cap_str, md->read_only ? "(ro)" : ""); |
1da177e4 | 3493 | |
371a689f AW |
3494 | if (mmc_blk_alloc_parts(card, md)) |
3495 | goto out; | |
3496 | ||
96541bac | 3497 | dev_set_drvdata(&card->dev, md); |
6f60c222 | 3498 | |
371a689f AW |
3499 | if (mmc_add_disk(md)) |
3500 | goto out; | |
3501 | ||
3502 | list_for_each_entry(part_md, &md->part, part) { | |
3503 | if (mmc_add_disk(part_md)) | |
3504 | goto out; | |
3505 | } | |
e94cfef6 | 3506 | |
627c3ccf | 3507 | /* Add two debugfs entries */ |
f9f0da98 | 3508 | mmc_blk_add_debugfs(card, md); |
627c3ccf | 3509 | |
e94cfef6 UH |
3510 | pm_runtime_set_autosuspend_delay(&card->dev, 3000); |
3511 | pm_runtime_use_autosuspend(&card->dev); | |
3512 | ||
3513 | /* | |
3514 | * Don't enable runtime PM for SD-combo cards here. Leave that | |
3515 | * decision to be taken during the SDIO init sequence instead. | |
3516 | */ | |
3517 | if (card->type != MMC_TYPE_SD_COMBO) { | |
3518 | pm_runtime_set_active(&card->dev); | |
3519 | pm_runtime_enable(&card->dev); | |
3520 | } | |
3521 | ||
1da177e4 LT |
3522 | return 0; |
3523 | ||
3524 | out: | |
371a689f AW |
3525 | mmc_blk_remove_parts(card, md); |
3526 | mmc_blk_remove_req(md); | |
5865f287 | 3527 | return 0; |
1da177e4 LT |
3528 | } |
3529 | ||
96541bac | 3530 | static void mmc_blk_remove(struct mmc_card *card) |
1da177e4 | 3531 | { |
96541bac | 3532 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
1da177e4 | 3533 | |
f9f0da98 | 3534 | mmc_blk_remove_debugfs(card, md); |
371a689f | 3535 | mmc_blk_remove_parts(card, md); |
e94cfef6 | 3536 | pm_runtime_get_sync(&card->dev); |
ddd6fa7e | 3537 | mmc_claim_host(card->host); |
1f797edc | 3538 | mmc_blk_part_switch(card, md->part_type); |
ddd6fa7e | 3539 | mmc_release_host(card->host); |
e94cfef6 UH |
3540 | if (card->type != MMC_TYPE_SD_COMBO) |
3541 | pm_runtime_disable(&card->dev); | |
3542 | pm_runtime_put_noidle(&card->dev); | |
371a689f | 3543 | mmc_blk_remove_req(md); |
96541bac | 3544 | dev_set_drvdata(&card->dev, NULL); |
1da177e4 LT |
3545 | } |
3546 | ||
96541bac | 3547 | static int _mmc_blk_suspend(struct mmc_card *card) |
1da177e4 | 3548 | { |
371a689f | 3549 | struct mmc_blk_data *part_md; |
96541bac | 3550 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
1da177e4 LT |
3551 | |
3552 | if (md) { | |
3553 | mmc_queue_suspend(&md->queue); | |
371a689f AW |
3554 | list_for_each_entry(part_md, &md->part, part) { |
3555 | mmc_queue_suspend(&part_md->queue); | |
3556 | } | |
1da177e4 LT |
3557 | } |
3558 | return 0; | |
3559 | } | |
3560 | ||
96541bac | 3561 | static void mmc_blk_shutdown(struct mmc_card *card) |
76287748 | 3562 | { |
96541bac | 3563 | _mmc_blk_suspend(card); |
76287748 UH |
3564 | } |
3565 | ||
0967edc6 UH |
3566 | #ifdef CONFIG_PM_SLEEP |
3567 | static int mmc_blk_suspend(struct device *dev) | |
76287748 | 3568 | { |
96541bac UH |
3569 | struct mmc_card *card = mmc_dev_to_card(dev); |
3570 | ||
3571 | return _mmc_blk_suspend(card); | |
76287748 UH |
3572 | } |
3573 | ||
0967edc6 | 3574 | static int mmc_blk_resume(struct device *dev) |
1da177e4 | 3575 | { |
371a689f | 3576 | struct mmc_blk_data *part_md; |
fc95e30b | 3577 | struct mmc_blk_data *md = dev_get_drvdata(dev); |
1da177e4 LT |
3578 | |
3579 | if (md) { | |
371a689f AW |
3580 | /* |
3581 | * Resume involves the card going into idle state, | |
3582 | * so current partition is always the main one. | |
3583 | */ | |
3584 | md->part_curr = md->part_type; | |
1da177e4 | 3585 | mmc_queue_resume(&md->queue); |
371a689f AW |
3586 | list_for_each_entry(part_md, &md->part, part) { |
3587 | mmc_queue_resume(&part_md->queue); | |
3588 | } | |
1da177e4 LT |
3589 | } |
3590 | return 0; | |
3591 | } | |
1da177e4 LT |
3592 | #endif |
3593 | ||
0967edc6 UH |
3594 | static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume); |
3595 | ||
96541bac UH |
3596 | static struct mmc_driver mmc_driver = { |
3597 | .drv = { | |
3598 | .name = "mmcblk", | |
3599 | .pm = &mmc_blk_pm_ops, | |
3600 | }, | |
1da177e4 LT |
3601 | .probe = mmc_blk_probe, |
3602 | .remove = mmc_blk_remove, | |
76287748 | 3603 | .shutdown = mmc_blk_shutdown, |
1da177e4 LT |
3604 | }; |
3605 | ||
3606 | static int __init mmc_blk_init(void) | |
3607 | { | |
9d4e98e9 | 3608 | int res; |
1da177e4 | 3609 | |
97548575 LW |
3610 | res = bus_register(&mmc_rpmb_bus_type); |
3611 | if (res < 0) { | |
3612 | pr_err("mmcblk: could not register RPMB bus type\n"); | |
3613 | return res; | |
3614 | } | |
3615 | res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb"); | |
3616 | if (res < 0) { | |
3617 | pr_err("mmcblk: failed to allocate rpmb chrdev region\n"); | |
3618 | goto out_bus_unreg; | |
3619 | } | |
3620 | ||
5e71b7a6 OJ |
3621 | if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) |
3622 | pr_info("mmcblk: using %d minors per device\n", perdev_minors); | |
3623 | ||
a26eba61 | 3624 | max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors); |
5e71b7a6 | 3625 | |
fe6b4c88 PO |
3626 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
3627 | if (res) | |
97548575 | 3628 | goto out_chrdev_unreg; |
1da177e4 | 3629 | |
9d4e98e9 AM |
3630 | res = mmc_register_driver(&mmc_driver); |
3631 | if (res) | |
97548575 | 3632 | goto out_blkdev_unreg; |
1da177e4 | 3633 | |
9d4e98e9 | 3634 | return 0; |
97548575 LW |
3635 | |
3636 | out_blkdev_unreg: | |
9d4e98e9 | 3637 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
97548575 LW |
3638 | out_chrdev_unreg: |
3639 | unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES); | |
3640 | out_bus_unreg: | |
3641 | bus_unregister(&mmc_rpmb_bus_type); | |
1da177e4 LT |
3642 | return res; |
3643 | } | |
3644 | ||
3645 | static void __exit mmc_blk_exit(void) | |
3646 | { | |
3647 | mmc_unregister_driver(&mmc_driver); | |
fe6b4c88 | 3648 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
97548575 | 3649 | unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES); |
1da177e4 LT |
3650 | } |
3651 | ||
3652 | module_init(mmc_blk_init); | |
3653 | module_exit(mmc_blk_exit); | |
3654 | ||
3655 | MODULE_LICENSE("GPL"); | |
3656 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); | |
3657 |