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> | |
a621aaed | 31 | #include <linux/mutex.h> |
ec5a19dd | 32 | #include <linux/scatterlist.h> |
a7bbb573 | 33 | #include <linux/string_helpers.h> |
cb87ea28 JC |
34 | #include <linux/delay.h> |
35 | #include <linux/capability.h> | |
36 | #include <linux/compat.h> | |
e94cfef6 | 37 | #include <linux/pm_runtime.h> |
b10fa99e | 38 | #include <linux/idr.h> |
1da177e4 | 39 | |
cb87ea28 | 40 | #include <linux/mmc/ioctl.h> |
1da177e4 | 41 | #include <linux/mmc/card.h> |
385e3227 | 42 | #include <linux/mmc/host.h> |
da7fbe58 PO |
43 | #include <linux/mmc/mmc.h> |
44 | #include <linux/mmc/sd.h> | |
1da177e4 | 45 | |
7c0f6ba6 | 46 | #include <linux/uaccess.h> |
1da177e4 | 47 | |
98ac2162 | 48 | #include "queue.h" |
48ab086d | 49 | #include "block.h" |
55244c56 | 50 | #include "core.h" |
4facdde1 | 51 | #include "card.h" |
5857b29b | 52 | #include "host.h" |
4facdde1 | 53 | #include "bus.h" |
55244c56 UH |
54 | #include "mmc_ops.h" |
55 | #include "sd_ops.h" | |
1da177e4 | 56 | |
6b0b6285 | 57 | MODULE_ALIAS("mmc:block"); |
5e71b7a6 OJ |
58 | #ifdef MODULE_PARAM_PREFIX |
59 | #undef MODULE_PARAM_PREFIX | |
60 | #endif | |
61 | #define MODULE_PARAM_PREFIX "mmcblk." | |
62 | ||
6a7a6b45 AW |
63 | #define INAND_CMD38_ARG_EXT_CSD 113 |
64 | #define INAND_CMD38_ARG_ERASE 0x00 | |
65 | #define INAND_CMD38_ARG_TRIM 0x01 | |
66 | #define INAND_CMD38_ARG_SECERASE 0x80 | |
67 | #define INAND_CMD38_ARG_SECTRIM1 0x81 | |
68 | #define INAND_CMD38_ARG_SECTRIM2 0x88 | |
8fee476b | 69 | #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */ |
775a9362 ME |
70 | #define MMC_SANITIZE_REQ_TIMEOUT 240000 |
71 | #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16) | |
6a7a6b45 | 72 | |
d3df0465 | 73 | #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \ |
ce39f9d1 | 74 | (rq_data_dir(req) == WRITE)) |
5e71b7a6 | 75 | static DEFINE_MUTEX(block_mutex); |
6b0b6285 | 76 | |
1da177e4 | 77 | /* |
5e71b7a6 OJ |
78 | * The defaults come from config options but can be overriden by module |
79 | * or bootarg options. | |
1da177e4 | 80 | */ |
5e71b7a6 | 81 | static int perdev_minors = CONFIG_MMC_BLOCK_MINORS; |
1dff3144 | 82 | |
5e71b7a6 OJ |
83 | /* |
84 | * We've only got one major, so number of mmcblk devices is | |
a26eba61 | 85 | * limited to (1 << 20) / number of minors per device. It is also |
b10fa99e | 86 | * limited by the MAX_DEVICES below. |
5e71b7a6 OJ |
87 | */ |
88 | static int max_devices; | |
89 | ||
a26eba61 BH |
90 | #define MAX_DEVICES 256 |
91 | ||
b10fa99e UH |
92 | static DEFINE_IDA(mmc_blk_ida); |
93 | static DEFINE_SPINLOCK(mmc_blk_lock); | |
1da177e4 | 94 | |
1da177e4 LT |
95 | /* |
96 | * There is one mmc_blk_data per slot. | |
97 | */ | |
98 | struct mmc_blk_data { | |
99 | spinlock_t lock; | |
307d8e6f | 100 | struct device *parent; |
1da177e4 LT |
101 | struct gendisk *disk; |
102 | struct mmc_queue queue; | |
371a689f | 103 | struct list_head part; |
1da177e4 | 104 | |
d0c97cfb AW |
105 | unsigned int flags; |
106 | #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */ | |
107 | #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */ | |
108 | ||
1da177e4 | 109 | unsigned int usage; |
a6f6c96b | 110 | unsigned int read_only; |
371a689f | 111 | unsigned int part_type; |
67716327 AH |
112 | unsigned int reset_done; |
113 | #define MMC_BLK_READ BIT(0) | |
114 | #define MMC_BLK_WRITE BIT(1) | |
115 | #define MMC_BLK_DISCARD BIT(2) | |
116 | #define MMC_BLK_SECDISCARD BIT(3) | |
371a689f AW |
117 | |
118 | /* | |
119 | * Only set in main mmc_blk_data associated | |
fc95e30b | 120 | * with mmc_card with dev_set_drvdata, and keeps |
371a689f AW |
121 | * track of the current selected device partition. |
122 | */ | |
123 | unsigned int part_curr; | |
124 | struct device_attribute force_ro; | |
add710ea JR |
125 | struct device_attribute power_ro_lock; |
126 | int area_type; | |
1da177e4 LT |
127 | }; |
128 | ||
a621aaed | 129 | static DEFINE_MUTEX(open_lock); |
1da177e4 | 130 | |
5e71b7a6 OJ |
131 | module_param(perdev_minors, int, 0444); |
132 | MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device"); | |
133 | ||
8d1e977d LP |
134 | static inline int mmc_blk_part_switch(struct mmc_card *card, |
135 | struct mmc_blk_data *md); | |
136 | static int get_card_status(struct mmc_card *card, u32 *status, int retries); | |
137 | ||
1da177e4 LT |
138 | static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk) |
139 | { | |
140 | struct mmc_blk_data *md; | |
141 | ||
a621aaed | 142 | mutex_lock(&open_lock); |
1da177e4 LT |
143 | md = disk->private_data; |
144 | if (md && md->usage == 0) | |
145 | md = NULL; | |
146 | if (md) | |
147 | md->usage++; | |
a621aaed | 148 | mutex_unlock(&open_lock); |
1da177e4 LT |
149 | |
150 | return md; | |
151 | } | |
152 | ||
371a689f AW |
153 | static inline int mmc_get_devidx(struct gendisk *disk) |
154 | { | |
382c55f8 | 155 | int devidx = disk->first_minor / perdev_minors; |
371a689f AW |
156 | return devidx; |
157 | } | |
158 | ||
1da177e4 LT |
159 | static void mmc_blk_put(struct mmc_blk_data *md) |
160 | { | |
a621aaed | 161 | mutex_lock(&open_lock); |
1da177e4 LT |
162 | md->usage--; |
163 | if (md->usage == 0) { | |
371a689f | 164 | int devidx = mmc_get_devidx(md->disk); |
5fa83ce2 AH |
165 | blk_cleanup_queue(md->queue.queue); |
166 | ||
b10fa99e UH |
167 | spin_lock(&mmc_blk_lock); |
168 | ida_remove(&mmc_blk_ida, devidx); | |
169 | spin_unlock(&mmc_blk_lock); | |
1dff3144 | 170 | |
1da177e4 | 171 | put_disk(md->disk); |
1da177e4 LT |
172 | kfree(md); |
173 | } | |
a621aaed | 174 | mutex_unlock(&open_lock); |
1da177e4 LT |
175 | } |
176 | ||
add710ea JR |
177 | static ssize_t power_ro_lock_show(struct device *dev, |
178 | struct device_attribute *attr, char *buf) | |
179 | { | |
180 | int ret; | |
181 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); | |
182 | struct mmc_card *card = md->queue.card; | |
183 | int locked = 0; | |
184 | ||
185 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN) | |
186 | locked = 2; | |
187 | else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN) | |
188 | locked = 1; | |
189 | ||
190 | ret = snprintf(buf, PAGE_SIZE, "%d\n", locked); | |
191 | ||
9098f84c TW |
192 | mmc_blk_put(md); |
193 | ||
add710ea JR |
194 | return ret; |
195 | } | |
196 | ||
197 | static ssize_t power_ro_lock_store(struct device *dev, | |
198 | struct device_attribute *attr, const char *buf, size_t count) | |
199 | { | |
200 | int ret; | |
201 | struct mmc_blk_data *md, *part_md; | |
202 | struct mmc_card *card; | |
203 | unsigned long set; | |
204 | ||
205 | if (kstrtoul(buf, 0, &set)) | |
206 | return -EINVAL; | |
207 | ||
208 | if (set != 1) | |
209 | return count; | |
210 | ||
211 | md = mmc_blk_get(dev_to_disk(dev)); | |
212 | card = md->queue.card; | |
213 | ||
e94cfef6 | 214 | mmc_get_card(card); |
add710ea JR |
215 | |
216 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP, | |
217 | card->ext_csd.boot_ro_lock | | |
218 | EXT_CSD_BOOT_WP_B_PWR_WP_EN, | |
219 | card->ext_csd.part_time); | |
220 | if (ret) | |
221 | pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret); | |
222 | else | |
223 | card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN; | |
224 | ||
e94cfef6 | 225 | mmc_put_card(card); |
add710ea JR |
226 | |
227 | if (!ret) { | |
228 | pr_info("%s: Locking boot partition ro until next power on\n", | |
229 | md->disk->disk_name); | |
230 | set_disk_ro(md->disk, 1); | |
231 | ||
232 | list_for_each_entry(part_md, &md->part, part) | |
233 | if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) { | |
234 | pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name); | |
235 | set_disk_ro(part_md->disk, 1); | |
236 | } | |
237 | } | |
238 | ||
239 | mmc_blk_put(md); | |
240 | return count; | |
241 | } | |
242 | ||
371a689f AW |
243 | static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr, |
244 | char *buf) | |
245 | { | |
246 | int ret; | |
247 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); | |
248 | ||
0031a98a | 249 | ret = snprintf(buf, PAGE_SIZE, "%d\n", |
371a689f AW |
250 | get_disk_ro(dev_to_disk(dev)) ^ |
251 | md->read_only); | |
252 | mmc_blk_put(md); | |
253 | return ret; | |
254 | } | |
255 | ||
256 | static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr, | |
257 | const char *buf, size_t count) | |
258 | { | |
259 | int ret; | |
260 | char *end; | |
261 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); | |
262 | unsigned long set = simple_strtoul(buf, &end, 0); | |
263 | if (end == buf) { | |
264 | ret = -EINVAL; | |
265 | goto out; | |
266 | } | |
267 | ||
268 | set_disk_ro(dev_to_disk(dev), set || md->read_only); | |
269 | ret = count; | |
270 | out: | |
271 | mmc_blk_put(md); | |
272 | return ret; | |
273 | } | |
274 | ||
a5a1561f | 275 | static int mmc_blk_open(struct block_device *bdev, fmode_t mode) |
1da177e4 | 276 | { |
a5a1561f | 277 | struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk); |
1da177e4 LT |
278 | int ret = -ENXIO; |
279 | ||
2a48fc0a | 280 | mutex_lock(&block_mutex); |
1da177e4 LT |
281 | if (md) { |
282 | if (md->usage == 2) | |
a5a1561f | 283 | check_disk_change(bdev); |
1da177e4 | 284 | ret = 0; |
a00fc090 | 285 | |
a5a1561f | 286 | if ((mode & FMODE_WRITE) && md->read_only) { |
70bb0896 | 287 | mmc_blk_put(md); |
a00fc090 | 288 | ret = -EROFS; |
70bb0896 | 289 | } |
1da177e4 | 290 | } |
2a48fc0a | 291 | mutex_unlock(&block_mutex); |
1da177e4 LT |
292 | |
293 | return ret; | |
294 | } | |
295 | ||
db2a144b | 296 | static void mmc_blk_release(struct gendisk *disk, fmode_t mode) |
1da177e4 | 297 | { |
a5a1561f | 298 | struct mmc_blk_data *md = disk->private_data; |
1da177e4 | 299 | |
2a48fc0a | 300 | mutex_lock(&block_mutex); |
1da177e4 | 301 | mmc_blk_put(md); |
2a48fc0a | 302 | mutex_unlock(&block_mutex); |
1da177e4 LT |
303 | } |
304 | ||
305 | static int | |
a885c8c4 | 306 | mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
1da177e4 | 307 | { |
a885c8c4 CH |
308 | geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16); |
309 | geo->heads = 4; | |
310 | geo->sectors = 16; | |
311 | return 0; | |
1da177e4 LT |
312 | } |
313 | ||
cb87ea28 JC |
314 | struct mmc_blk_ioc_data { |
315 | struct mmc_ioc_cmd ic; | |
316 | unsigned char *buf; | |
317 | u64 buf_bytes; | |
318 | }; | |
319 | ||
320 | static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user( | |
321 | struct mmc_ioc_cmd __user *user) | |
322 | { | |
323 | struct mmc_blk_ioc_data *idata; | |
324 | int err; | |
325 | ||
1ff8950c | 326 | idata = kmalloc(sizeof(*idata), GFP_KERNEL); |
cb87ea28 JC |
327 | if (!idata) { |
328 | err = -ENOMEM; | |
aea253ec | 329 | goto out; |
cb87ea28 JC |
330 | } |
331 | ||
332 | if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) { | |
333 | err = -EFAULT; | |
aea253ec | 334 | goto idata_err; |
cb87ea28 JC |
335 | } |
336 | ||
337 | idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks; | |
338 | if (idata->buf_bytes > MMC_IOC_MAX_BYTES) { | |
339 | err = -EOVERFLOW; | |
aea253ec | 340 | goto idata_err; |
cb87ea28 JC |
341 | } |
342 | ||
bfe5b1b1 VV |
343 | if (!idata->buf_bytes) { |
344 | idata->buf = NULL; | |
4d6144de | 345 | return idata; |
bfe5b1b1 | 346 | } |
4d6144de | 347 | |
1ff8950c | 348 | idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL); |
cb87ea28 JC |
349 | if (!idata->buf) { |
350 | err = -ENOMEM; | |
aea253ec | 351 | goto idata_err; |
cb87ea28 JC |
352 | } |
353 | ||
354 | if (copy_from_user(idata->buf, (void __user *)(unsigned long) | |
355 | idata->ic.data_ptr, idata->buf_bytes)) { | |
356 | err = -EFAULT; | |
357 | goto copy_err; | |
358 | } | |
359 | ||
360 | return idata; | |
361 | ||
362 | copy_err: | |
363 | kfree(idata->buf); | |
aea253ec | 364 | idata_err: |
cb87ea28 | 365 | kfree(idata); |
aea253ec | 366 | out: |
cb87ea28 | 367 | return ERR_PTR(err); |
cb87ea28 JC |
368 | } |
369 | ||
a5f5774c JH |
370 | static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr, |
371 | struct mmc_blk_ioc_data *idata) | |
372 | { | |
373 | struct mmc_ioc_cmd *ic = &idata->ic; | |
374 | ||
375 | if (copy_to_user(&(ic_ptr->response), ic->response, | |
376 | sizeof(ic->response))) | |
377 | return -EFAULT; | |
378 | ||
379 | if (!idata->ic.write_flag) { | |
380 | if (copy_to_user((void __user *)(unsigned long)ic->data_ptr, | |
381 | idata->buf, idata->buf_bytes)) | |
382 | return -EFAULT; | |
383 | } | |
384 | ||
385 | return 0; | |
386 | } | |
387 | ||
8d1e977d LP |
388 | static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status, |
389 | u32 retries_max) | |
390 | { | |
391 | int err; | |
392 | u32 retry_count = 0; | |
393 | ||
394 | if (!status || !retries_max) | |
395 | return -EINVAL; | |
396 | ||
397 | do { | |
398 | err = get_card_status(card, status, 5); | |
399 | if (err) | |
400 | break; | |
401 | ||
402 | if (!R1_STATUS(*status) && | |
403 | (R1_CURRENT_STATE(*status) != R1_STATE_PRG)) | |
404 | break; /* RPMB programming operation complete */ | |
405 | ||
406 | /* | |
407 | * Rechedule to give the MMC device a chance to continue | |
408 | * processing the previous command without being polled too | |
409 | * frequently. | |
410 | */ | |
411 | usleep_range(1000, 5000); | |
412 | } while (++retry_count < retries_max); | |
413 | ||
414 | if (retry_count == retries_max) | |
415 | err = -EPERM; | |
416 | ||
417 | return err; | |
418 | } | |
419 | ||
775a9362 ME |
420 | static int ioctl_do_sanitize(struct mmc_card *card) |
421 | { | |
422 | int err; | |
423 | ||
a2d1086d | 424 | if (!mmc_can_sanitize(card)) { |
775a9362 ME |
425 | pr_warn("%s: %s - SANITIZE is not supported\n", |
426 | mmc_hostname(card->host), __func__); | |
427 | err = -EOPNOTSUPP; | |
428 | goto out; | |
429 | } | |
430 | ||
431 | pr_debug("%s: %s - SANITIZE IN PROGRESS...\n", | |
432 | mmc_hostname(card->host), __func__); | |
433 | ||
434 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
435 | EXT_CSD_SANITIZE_START, 1, | |
436 | MMC_SANITIZE_REQ_TIMEOUT); | |
437 | ||
438 | if (err) | |
439 | pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n", | |
440 | mmc_hostname(card->host), __func__, err); | |
441 | ||
442 | pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host), | |
443 | __func__); | |
444 | out: | |
445 | return err; | |
446 | } | |
447 | ||
a5f5774c JH |
448 | static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md, |
449 | struct mmc_blk_ioc_data *idata) | |
cb87ea28 | 450 | { |
c7836d15 MY |
451 | struct mmc_command cmd = {}; |
452 | struct mmc_data data = {}; | |
453 | struct mmc_request mrq = {}; | |
cb87ea28 JC |
454 | struct scatterlist sg; |
455 | int err; | |
8d1e977d LP |
456 | int is_rpmb = false; |
457 | u32 status = 0; | |
cb87ea28 | 458 | |
a5f5774c JH |
459 | if (!card || !md || !idata) |
460 | return -EINVAL; | |
cb87ea28 | 461 | |
8d1e977d LP |
462 | if (md->area_type & MMC_BLK_DATA_AREA_RPMB) |
463 | is_rpmb = true; | |
464 | ||
4d6144de JR |
465 | cmd.opcode = idata->ic.opcode; |
466 | cmd.arg = idata->ic.arg; | |
467 | cmd.flags = idata->ic.flags; | |
468 | ||
469 | if (idata->buf_bytes) { | |
470 | data.sg = &sg; | |
471 | data.sg_len = 1; | |
472 | data.blksz = idata->ic.blksz; | |
473 | data.blocks = idata->ic.blocks; | |
474 | ||
475 | sg_init_one(data.sg, idata->buf, idata->buf_bytes); | |
476 | ||
477 | if (idata->ic.write_flag) | |
478 | data.flags = MMC_DATA_WRITE; | |
479 | else | |
480 | data.flags = MMC_DATA_READ; | |
481 | ||
482 | /* data.flags must already be set before doing this. */ | |
483 | mmc_set_data_timeout(&data, card); | |
484 | ||
485 | /* Allow overriding the timeout_ns for empirical tuning. */ | |
486 | if (idata->ic.data_timeout_ns) | |
487 | data.timeout_ns = idata->ic.data_timeout_ns; | |
488 | ||
489 | if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) { | |
490 | /* | |
491 | * Pretend this is a data transfer and rely on the | |
492 | * host driver to compute timeout. When all host | |
493 | * drivers support cmd.cmd_timeout for R1B, this | |
494 | * can be changed to: | |
495 | * | |
496 | * mrq.data = NULL; | |
497 | * cmd.cmd_timeout = idata->ic.cmd_timeout_ms; | |
498 | */ | |
499 | data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000; | |
500 | } | |
501 | ||
502 | mrq.data = &data; | |
503 | } | |
504 | ||
505 | mrq.cmd = &cmd; | |
506 | ||
8d1e977d LP |
507 | err = mmc_blk_part_switch(card, md); |
508 | if (err) | |
a5f5774c | 509 | return err; |
8d1e977d | 510 | |
cb87ea28 JC |
511 | if (idata->ic.is_acmd) { |
512 | err = mmc_app_cmd(card->host, card); | |
513 | if (err) | |
a5f5774c | 514 | return err; |
cb87ea28 JC |
515 | } |
516 | ||
8d1e977d LP |
517 | if (is_rpmb) { |
518 | err = mmc_set_blockcount(card, data.blocks, | |
519 | idata->ic.write_flag & (1 << 31)); | |
520 | if (err) | |
a5f5774c | 521 | return err; |
8d1e977d LP |
522 | } |
523 | ||
a82e484e YG |
524 | if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) && |
525 | (cmd.opcode == MMC_SWITCH)) { | |
775a9362 ME |
526 | err = ioctl_do_sanitize(card); |
527 | ||
528 | if (err) | |
529 | pr_err("%s: ioctl_do_sanitize() failed. err = %d", | |
530 | __func__, err); | |
531 | ||
a5f5774c | 532 | return err; |
775a9362 ME |
533 | } |
534 | ||
cb87ea28 JC |
535 | mmc_wait_for_req(card->host, &mrq); |
536 | ||
537 | if (cmd.error) { | |
538 | dev_err(mmc_dev(card->host), "%s: cmd error %d\n", | |
539 | __func__, cmd.error); | |
a5f5774c | 540 | return cmd.error; |
cb87ea28 JC |
541 | } |
542 | if (data.error) { | |
543 | dev_err(mmc_dev(card->host), "%s: data error %d\n", | |
544 | __func__, data.error); | |
a5f5774c | 545 | return data.error; |
cb87ea28 JC |
546 | } |
547 | ||
548 | /* | |
549 | * According to the SD specs, some commands require a delay after | |
550 | * issuing the command. | |
551 | */ | |
552 | if (idata->ic.postsleep_min_us) | |
553 | usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us); | |
554 | ||
a5f5774c | 555 | memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp)); |
cb87ea28 | 556 | |
8d1e977d LP |
557 | if (is_rpmb) { |
558 | /* | |
559 | * Ensure RPMB command has completed by polling CMD13 | |
560 | * "Send Status". | |
561 | */ | |
562 | err = ioctl_rpmb_card_status_poll(card, &status, 5); | |
563 | if (err) | |
564 | dev_err(mmc_dev(card->host), | |
565 | "%s: Card Status=0x%08X, error %d\n", | |
566 | __func__, status, err); | |
567 | } | |
568 | ||
a5f5774c JH |
569 | return err; |
570 | } | |
571 | ||
572 | static int mmc_blk_ioctl_cmd(struct block_device *bdev, | |
573 | struct mmc_ioc_cmd __user *ic_ptr) | |
574 | { | |
575 | struct mmc_blk_ioc_data *idata; | |
576 | struct mmc_blk_data *md; | |
577 | struct mmc_card *card; | |
b093410c | 578 | int err = 0, ioc_err = 0; |
a5f5774c | 579 | |
83c742c3 SL |
580 | /* |
581 | * The caller must have CAP_SYS_RAWIO, and must be calling this on the | |
582 | * whole block device, not on a partition. This prevents overspray | |
583 | * between sibling partitions. | |
584 | */ | |
585 | if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains)) | |
586 | return -EPERM; | |
587 | ||
a5f5774c JH |
588 | idata = mmc_blk_ioctl_copy_from_user(ic_ptr); |
589 | if (IS_ERR(idata)) | |
590 | return PTR_ERR(idata); | |
591 | ||
592 | md = mmc_blk_get(bdev->bd_disk); | |
593 | if (!md) { | |
594 | err = -EINVAL; | |
595 | goto cmd_err; | |
596 | } | |
597 | ||
598 | card = md->queue.card; | |
599 | if (IS_ERR(card)) { | |
600 | err = PTR_ERR(card); | |
601 | goto cmd_done; | |
602 | } | |
603 | ||
604 | mmc_get_card(card); | |
605 | ||
b093410c | 606 | ioc_err = __mmc_blk_ioctl_cmd(card, md, idata); |
a5f5774c | 607 | |
3c866568 AH |
608 | /* Always switch back to main area after RPMB access */ |
609 | if (md->area_type & MMC_BLK_DATA_AREA_RPMB) | |
610 | mmc_blk_part_switch(card, dev_get_drvdata(&card->dev)); | |
611 | ||
e94cfef6 | 612 | mmc_put_card(card); |
cb87ea28 | 613 | |
b093410c | 614 | err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata); |
a5f5774c | 615 | |
cb87ea28 JC |
616 | cmd_done: |
617 | mmc_blk_put(md); | |
1c02f000 | 618 | cmd_err: |
cb87ea28 JC |
619 | kfree(idata->buf); |
620 | kfree(idata); | |
b093410c | 621 | return ioc_err ? ioc_err : err; |
cb87ea28 JC |
622 | } |
623 | ||
a5f5774c JH |
624 | static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev, |
625 | struct mmc_ioc_multi_cmd __user *user) | |
626 | { | |
627 | struct mmc_blk_ioc_data **idata = NULL; | |
628 | struct mmc_ioc_cmd __user *cmds = user->cmds; | |
629 | struct mmc_card *card; | |
630 | struct mmc_blk_data *md; | |
b093410c | 631 | int i, err = 0, ioc_err = 0; |
a5f5774c JH |
632 | __u64 num_of_cmds; |
633 | ||
83c742c3 SL |
634 | /* |
635 | * The caller must have CAP_SYS_RAWIO, and must be calling this on the | |
636 | * whole block device, not on a partition. This prevents overspray | |
637 | * between sibling partitions. | |
638 | */ | |
639 | if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains)) | |
640 | return -EPERM; | |
641 | ||
a5f5774c JH |
642 | if (copy_from_user(&num_of_cmds, &user->num_of_cmds, |
643 | sizeof(num_of_cmds))) | |
644 | return -EFAULT; | |
645 | ||
646 | if (num_of_cmds > MMC_IOC_MAX_CMDS) | |
647 | return -EINVAL; | |
648 | ||
649 | idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL); | |
650 | if (!idata) | |
651 | return -ENOMEM; | |
652 | ||
653 | for (i = 0; i < num_of_cmds; i++) { | |
654 | idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]); | |
655 | if (IS_ERR(idata[i])) { | |
656 | err = PTR_ERR(idata[i]); | |
657 | num_of_cmds = i; | |
658 | goto cmd_err; | |
659 | } | |
660 | } | |
661 | ||
662 | md = mmc_blk_get(bdev->bd_disk); | |
f00ab14c OJ |
663 | if (!md) { |
664 | err = -EINVAL; | |
a5f5774c | 665 | goto cmd_err; |
f00ab14c | 666 | } |
a5f5774c JH |
667 | |
668 | card = md->queue.card; | |
669 | if (IS_ERR(card)) { | |
670 | err = PTR_ERR(card); | |
671 | goto cmd_done; | |
672 | } | |
673 | ||
674 | mmc_get_card(card); | |
675 | ||
b093410c GG |
676 | for (i = 0; i < num_of_cmds && !ioc_err; i++) |
677 | ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]); | |
a5f5774c | 678 | |
3c866568 AH |
679 | /* Always switch back to main area after RPMB access */ |
680 | if (md->area_type & MMC_BLK_DATA_AREA_RPMB) | |
681 | mmc_blk_part_switch(card, dev_get_drvdata(&card->dev)); | |
682 | ||
a5f5774c JH |
683 | mmc_put_card(card); |
684 | ||
685 | /* copy to user if data and response */ | |
b093410c | 686 | for (i = 0; i < num_of_cmds && !err; i++) |
a5f5774c | 687 | err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]); |
a5f5774c JH |
688 | |
689 | cmd_done: | |
690 | mmc_blk_put(md); | |
691 | cmd_err: | |
692 | for (i = 0; i < num_of_cmds; i++) { | |
693 | kfree(idata[i]->buf); | |
694 | kfree(idata[i]); | |
695 | } | |
696 | kfree(idata); | |
b093410c | 697 | return ioc_err ? ioc_err : err; |
a5f5774c JH |
698 | } |
699 | ||
cb87ea28 JC |
700 | static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode, |
701 | unsigned int cmd, unsigned long arg) | |
702 | { | |
a5f5774c JH |
703 | switch (cmd) { |
704 | case MMC_IOC_CMD: | |
705 | return mmc_blk_ioctl_cmd(bdev, | |
706 | (struct mmc_ioc_cmd __user *)arg); | |
707 | case MMC_IOC_MULTI_CMD: | |
708 | return mmc_blk_ioctl_multi_cmd(bdev, | |
709 | (struct mmc_ioc_multi_cmd __user *)arg); | |
710 | default: | |
711 | return -EINVAL; | |
712 | } | |
cb87ea28 JC |
713 | } |
714 | ||
715 | #ifdef CONFIG_COMPAT | |
716 | static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode, | |
717 | unsigned int cmd, unsigned long arg) | |
718 | { | |
719 | return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg)); | |
720 | } | |
721 | #endif | |
722 | ||
83d5cde4 | 723 | static const struct block_device_operations mmc_bdops = { |
a5a1561f AV |
724 | .open = mmc_blk_open, |
725 | .release = mmc_blk_release, | |
a885c8c4 | 726 | .getgeo = mmc_blk_getgeo, |
1da177e4 | 727 | .owner = THIS_MODULE, |
cb87ea28 JC |
728 | .ioctl = mmc_blk_ioctl, |
729 | #ifdef CONFIG_COMPAT | |
730 | .compat_ioctl = mmc_blk_compat_ioctl, | |
731 | #endif | |
1da177e4 LT |
732 | }; |
733 | ||
371a689f AW |
734 | static inline int mmc_blk_part_switch(struct mmc_card *card, |
735 | struct mmc_blk_data *md) | |
736 | { | |
737 | int ret; | |
fc95e30b | 738 | struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev); |
0d7d85ca | 739 | |
371a689f AW |
740 | if (main_md->part_curr == md->part_type) |
741 | return 0; | |
742 | ||
743 | if (mmc_card_mmc(card)) { | |
0d7d85ca AH |
744 | u8 part_config = card->ext_csd.part_config; |
745 | ||
57da0c04 AH |
746 | if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) |
747 | mmc_retune_pause(card->host); | |
748 | ||
0d7d85ca AH |
749 | part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK; |
750 | part_config |= md->part_type; | |
371a689f AW |
751 | |
752 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
0d7d85ca | 753 | EXT_CSD_PART_CONFIG, part_config, |
371a689f | 754 | card->ext_csd.part_time); |
57da0c04 AH |
755 | if (ret) { |
756 | if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) | |
757 | mmc_retune_unpause(card->host); | |
371a689f | 758 | return ret; |
57da0c04 | 759 | } |
0d7d85ca AH |
760 | |
761 | card->ext_csd.part_config = part_config; | |
57da0c04 AH |
762 | |
763 | if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB) | |
764 | mmc_retune_unpause(card->host); | |
67716327 | 765 | } |
371a689f AW |
766 | |
767 | main_md->part_curr = md->part_type; | |
768 | return 0; | |
769 | } | |
770 | ||
ec5a19dd PO |
771 | static u32 mmc_sd_num_wr_blocks(struct mmc_card *card) |
772 | { | |
773 | int err; | |
051913da BD |
774 | u32 result; |
775 | __be32 *blocks; | |
ec5a19dd | 776 | |
c7836d15 MY |
777 | struct mmc_request mrq = {}; |
778 | struct mmc_command cmd = {}; | |
779 | struct mmc_data data = {}; | |
ec5a19dd PO |
780 | |
781 | struct scatterlist sg; | |
782 | ||
ec5a19dd PO |
783 | cmd.opcode = MMC_APP_CMD; |
784 | cmd.arg = card->rca << 16; | |
7213d175 | 785 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
ec5a19dd PO |
786 | |
787 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | |
7213d175 DB |
788 | if (err) |
789 | return (u32)-1; | |
790 | if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD)) | |
ec5a19dd PO |
791 | return (u32)-1; |
792 | ||
793 | memset(&cmd, 0, sizeof(struct mmc_command)); | |
794 | ||
795 | cmd.opcode = SD_APP_SEND_NUM_WR_BLKS; | |
796 | cmd.arg = 0; | |
7213d175 | 797 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
ec5a19dd | 798 | |
ec5a19dd PO |
799 | data.blksz = 4; |
800 | data.blocks = 1; | |
801 | data.flags = MMC_DATA_READ; | |
802 | data.sg = &sg; | |
803 | data.sg_len = 1; | |
d380443c | 804 | mmc_set_data_timeout(&data, card); |
ec5a19dd | 805 | |
ec5a19dd PO |
806 | mrq.cmd = &cmd; |
807 | mrq.data = &data; | |
808 | ||
051913da BD |
809 | blocks = kmalloc(4, GFP_KERNEL); |
810 | if (!blocks) | |
811 | return (u32)-1; | |
812 | ||
813 | sg_init_one(&sg, blocks, 4); | |
ec5a19dd PO |
814 | |
815 | mmc_wait_for_req(card->host, &mrq); | |
816 | ||
051913da BD |
817 | result = ntohl(*blocks); |
818 | kfree(blocks); | |
819 | ||
17b0429d | 820 | if (cmd.error || data.error) |
051913da | 821 | result = (u32)-1; |
ec5a19dd | 822 | |
051913da | 823 | return result; |
ec5a19dd PO |
824 | } |
825 | ||
0a2d4048 | 826 | static int get_card_status(struct mmc_card *card, u32 *status, int retries) |
504f191f | 827 | { |
c7836d15 | 828 | struct mmc_command cmd = {}; |
504f191f AH |
829 | int err; |
830 | ||
504f191f AH |
831 | cmd.opcode = MMC_SEND_STATUS; |
832 | if (!mmc_host_is_spi(card->host)) | |
833 | cmd.arg = card->rca << 16; | |
834 | cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC; | |
0a2d4048 RKAL |
835 | err = mmc_wait_for_cmd(card->host, &cmd, retries); |
836 | if (err == 0) | |
837 | *status = cmd.resp[0]; | |
838 | return err; | |
504f191f AH |
839 | } |
840 | ||
c49433fb | 841 | static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms, |
c44d6cef | 842 | bool hw_busy_detect, struct request *req, bool *gen_err) |
c49433fb UH |
843 | { |
844 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); | |
845 | int err = 0; | |
846 | u32 status; | |
847 | ||
848 | do { | |
849 | err = get_card_status(card, &status, 5); | |
850 | if (err) { | |
851 | pr_err("%s: error %d requesting status\n", | |
852 | req->rq_disk->disk_name, err); | |
853 | return err; | |
854 | } | |
855 | ||
856 | if (status & R1_ERROR) { | |
857 | pr_err("%s: %s: error sending status cmd, status %#x\n", | |
858 | req->rq_disk->disk_name, __func__, status); | |
c44d6cef | 859 | *gen_err = true; |
c49433fb UH |
860 | } |
861 | ||
95a91298 UH |
862 | /* We may rely on the host hw to handle busy detection.*/ |
863 | if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && | |
864 | hw_busy_detect) | |
865 | break; | |
866 | ||
c49433fb UH |
867 | /* |
868 | * Timeout if the device never becomes ready for data and never | |
869 | * leaves the program state. | |
870 | */ | |
871 | if (time_after(jiffies, timeout)) { | |
872 | pr_err("%s: Card stuck in programming state! %s %s\n", | |
873 | mmc_hostname(card->host), | |
874 | req->rq_disk->disk_name, __func__); | |
875 | return -ETIMEDOUT; | |
876 | } | |
877 | ||
878 | /* | |
879 | * Some cards mishandle the status bits, | |
880 | * so make sure to check both the busy | |
881 | * indication and the card state. | |
882 | */ | |
883 | } while (!(status & R1_READY_FOR_DATA) || | |
884 | (R1_CURRENT_STATE(status) == R1_STATE_PRG)); | |
885 | ||
886 | return err; | |
887 | } | |
888 | ||
bb5cba40 | 889 | static int send_stop(struct mmc_card *card, unsigned int timeout_ms, |
c44d6cef | 890 | struct request *req, bool *gen_err, u32 *stop_status) |
bb5cba40 UH |
891 | { |
892 | struct mmc_host *host = card->host; | |
c7836d15 | 893 | struct mmc_command cmd = {}; |
bb5cba40 UH |
894 | int err; |
895 | bool use_r1b_resp = rq_data_dir(req) == WRITE; | |
896 | ||
897 | /* | |
898 | * Normally we use R1B responses for WRITE, but in cases where the host | |
899 | * has specified a max_busy_timeout we need to validate it. A failure | |
900 | * means we need to prevent the host from doing hw busy detection, which | |
901 | * is done by converting to a R1 response instead. | |
902 | */ | |
903 | if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) | |
904 | use_r1b_resp = false; | |
905 | ||
906 | cmd.opcode = MMC_STOP_TRANSMISSION; | |
907 | if (use_r1b_resp) { | |
908 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; | |
909 | cmd.busy_timeout = timeout_ms; | |
910 | } else { | |
911 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; | |
912 | } | |
913 | ||
914 | err = mmc_wait_for_cmd(host, &cmd, 5); | |
915 | if (err) | |
916 | return err; | |
917 | ||
918 | *stop_status = cmd.resp[0]; | |
919 | ||
920 | /* No need to check card status in case of READ. */ | |
921 | if (rq_data_dir(req) == READ) | |
922 | return 0; | |
923 | ||
924 | if (!mmc_host_is_spi(host) && | |
925 | (*stop_status & R1_ERROR)) { | |
926 | pr_err("%s: %s: general error sending stop command, resp %#x\n", | |
927 | req->rq_disk->disk_name, __func__, *stop_status); | |
c44d6cef | 928 | *gen_err = true; |
bb5cba40 UH |
929 | } |
930 | ||
931 | return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err); | |
932 | } | |
933 | ||
a8ad82cc | 934 | #define ERR_NOMEDIUM 3 |
a01f3ccf RKAL |
935 | #define ERR_RETRY 2 |
936 | #define ERR_ABORT 1 | |
937 | #define ERR_CONTINUE 0 | |
938 | ||
939 | static int mmc_blk_cmd_error(struct request *req, const char *name, int error, | |
940 | bool status_valid, u32 status) | |
941 | { | |
942 | switch (error) { | |
943 | case -EILSEQ: | |
944 | /* response crc error, retry the r/w cmd */ | |
945 | pr_err("%s: %s sending %s command, card status %#x\n", | |
946 | req->rq_disk->disk_name, "response CRC error", | |
947 | name, status); | |
948 | return ERR_RETRY; | |
949 | ||
950 | case -ETIMEDOUT: | |
951 | pr_err("%s: %s sending %s command, card status %#x\n", | |
952 | req->rq_disk->disk_name, "timed out", name, status); | |
953 | ||
954 | /* If the status cmd initially failed, retry the r/w cmd */ | |
cc4d04be KS |
955 | if (!status_valid) { |
956 | pr_err("%s: status not valid, retrying timeout\n", | |
957 | req->rq_disk->disk_name); | |
a01f3ccf | 958 | return ERR_RETRY; |
cc4d04be | 959 | } |
a01f3ccf RKAL |
960 | |
961 | /* | |
962 | * If it was a r/w cmd crc error, or illegal command | |
963 | * (eg, issued in wrong state) then retry - we should | |
964 | * have corrected the state problem above. | |
965 | */ | |
cc4d04be KS |
966 | if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) { |
967 | pr_err("%s: command error, retrying timeout\n", | |
968 | req->rq_disk->disk_name); | |
a01f3ccf | 969 | return ERR_RETRY; |
cc4d04be | 970 | } |
a01f3ccf RKAL |
971 | |
972 | /* Otherwise abort the command */ | |
973 | return ERR_ABORT; | |
974 | ||
975 | default: | |
976 | /* We don't understand the error code the driver gave us */ | |
977 | pr_err("%s: unknown error %d sending read/write command, card status %#x\n", | |
978 | req->rq_disk->disk_name, error, status); | |
979 | return ERR_ABORT; | |
980 | } | |
981 | } | |
982 | ||
983 | /* | |
984 | * Initial r/w and stop cmd error recovery. | |
985 | * We don't know whether the card received the r/w cmd or not, so try to | |
986 | * restore things back to a sane state. Essentially, we do this as follows: | |
987 | * - Obtain card status. If the first attempt to obtain card status fails, | |
988 | * the status word will reflect the failed status cmd, not the failed | |
989 | * r/w cmd. If we fail to obtain card status, it suggests we can no | |
990 | * longer communicate with the card. | |
991 | * - Check the card state. If the card received the cmd but there was a | |
992 | * transient problem with the response, it might still be in a data transfer | |
993 | * mode. Try to send it a stop command. If this fails, we can't recover. | |
994 | * - If the r/w cmd failed due to a response CRC error, it was probably | |
995 | * transient, so retry the cmd. | |
996 | * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry. | |
997 | * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or | |
998 | * illegal cmd, retry. | |
999 | * Otherwise we don't understand what happened, so abort. | |
1000 | */ | |
1001 | static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req, | |
2cc64587 | 1002 | struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err) |
a01f3ccf RKAL |
1003 | { |
1004 | bool prev_cmd_status_valid = true; | |
1005 | u32 status, stop_status = 0; | |
1006 | int err, retry; | |
1007 | ||
a8ad82cc SRT |
1008 | if (mmc_card_removed(card)) |
1009 | return ERR_NOMEDIUM; | |
1010 | ||
a01f3ccf RKAL |
1011 | /* |
1012 | * Try to get card status which indicates both the card state | |
1013 | * and why there was no response. If the first attempt fails, | |
1014 | * we can't be sure the returned status is for the r/w command. | |
1015 | */ | |
1016 | for (retry = 2; retry >= 0; retry--) { | |
1017 | err = get_card_status(card, &status, 0); | |
1018 | if (!err) | |
1019 | break; | |
1020 | ||
6f398ad2 AH |
1021 | /* Re-tune if needed */ |
1022 | mmc_retune_recheck(card->host); | |
1023 | ||
a01f3ccf RKAL |
1024 | prev_cmd_status_valid = false; |
1025 | pr_err("%s: error %d sending status command, %sing\n", | |
1026 | req->rq_disk->disk_name, err, retry ? "retry" : "abort"); | |
1027 | } | |
1028 | ||
1029 | /* We couldn't get a response from the card. Give up. */ | |
a8ad82cc SRT |
1030 | if (err) { |
1031 | /* Check if the card is removed */ | |
1032 | if (mmc_detect_card_removed(card->host)) | |
1033 | return ERR_NOMEDIUM; | |
a01f3ccf | 1034 | return ERR_ABORT; |
a8ad82cc | 1035 | } |
a01f3ccf | 1036 | |
67716327 AH |
1037 | /* Flag ECC errors */ |
1038 | if ((status & R1_CARD_ECC_FAILED) || | |
1039 | (brq->stop.resp[0] & R1_CARD_ECC_FAILED) || | |
1040 | (brq->cmd.resp[0] & R1_CARD_ECC_FAILED)) | |
2cc64587 | 1041 | *ecc_err = true; |
67716327 | 1042 | |
c8760069 KY |
1043 | /* Flag General errors */ |
1044 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) | |
1045 | if ((status & R1_ERROR) || | |
1046 | (brq->stop.resp[0] & R1_ERROR)) { | |
1047 | pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n", | |
1048 | req->rq_disk->disk_name, __func__, | |
1049 | brq->stop.resp[0], status); | |
c44d6cef | 1050 | *gen_err = true; |
c8760069 KY |
1051 | } |
1052 | ||
a01f3ccf RKAL |
1053 | /* |
1054 | * Check the current card state. If it is in some data transfer | |
1055 | * mode, tell it to stop (and hopefully transition back to TRAN.) | |
1056 | */ | |
1057 | if (R1_CURRENT_STATE(status) == R1_STATE_DATA || | |
1058 | R1_CURRENT_STATE(status) == R1_STATE_RCV) { | |
bb5cba40 UH |
1059 | err = send_stop(card, |
1060 | DIV_ROUND_UP(brq->data.timeout_ns, 1000000), | |
1061 | req, gen_err, &stop_status); | |
1062 | if (err) { | |
a01f3ccf RKAL |
1063 | pr_err("%s: error %d sending stop command\n", |
1064 | req->rq_disk->disk_name, err); | |
bb5cba40 UH |
1065 | /* |
1066 | * If the stop cmd also timed out, the card is probably | |
1067 | * not present, so abort. Other errors are bad news too. | |
1068 | */ | |
a01f3ccf | 1069 | return ERR_ABORT; |
bb5cba40 UH |
1070 | } |
1071 | ||
67716327 | 1072 | if (stop_status & R1_CARD_ECC_FAILED) |
2cc64587 | 1073 | *ecc_err = true; |
a01f3ccf RKAL |
1074 | } |
1075 | ||
1076 | /* Check for set block count errors */ | |
1077 | if (brq->sbc.error) | |
1078 | return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error, | |
1079 | prev_cmd_status_valid, status); | |
1080 | ||
1081 | /* Check for r/w command errors */ | |
1082 | if (brq->cmd.error) | |
1083 | return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error, | |
1084 | prev_cmd_status_valid, status); | |
1085 | ||
67716327 AH |
1086 | /* Data errors */ |
1087 | if (!brq->stop.error) | |
1088 | return ERR_CONTINUE; | |
1089 | ||
a01f3ccf | 1090 | /* Now for stop errors. These aren't fatal to the transfer. */ |
5e1344eb | 1091 | pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n", |
a01f3ccf RKAL |
1092 | req->rq_disk->disk_name, brq->stop.error, |
1093 | brq->cmd.resp[0], status); | |
1094 | ||
1095 | /* | |
1096 | * Subsitute in our own stop status as this will give the error | |
1097 | * state which happened during the execution of the r/w command. | |
1098 | */ | |
1099 | if (stop_status) { | |
1100 | brq->stop.resp[0] = stop_status; | |
1101 | brq->stop.error = 0; | |
1102 | } | |
1103 | return ERR_CONTINUE; | |
1104 | } | |
1105 | ||
67716327 AH |
1106 | static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host, |
1107 | int type) | |
1108 | { | |
1109 | int err; | |
1110 | ||
1111 | if (md->reset_done & type) | |
1112 | return -EEXIST; | |
1113 | ||
1114 | md->reset_done |= type; | |
1115 | err = mmc_hw_reset(host); | |
1116 | /* Ensure we switch back to the correct partition */ | |
1117 | if (err != -EOPNOTSUPP) { | |
fc95e30b UH |
1118 | struct mmc_blk_data *main_md = |
1119 | dev_get_drvdata(&host->card->dev); | |
67716327 AH |
1120 | int part_err; |
1121 | ||
1122 | main_md->part_curr = main_md->part_type; | |
1123 | part_err = mmc_blk_part_switch(host->card, md); | |
1124 | if (part_err) { | |
1125 | /* | |
1126 | * We have failed to get back into the correct | |
1127 | * partition, so we need to abort the whole request. | |
1128 | */ | |
1129 | return -ENODEV; | |
1130 | } | |
1131 | } | |
1132 | return err; | |
1133 | } | |
1134 | ||
1135 | static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type) | |
1136 | { | |
1137 | md->reset_done &= ~type; | |
1138 | } | |
1139 | ||
4e93b9a6 CD |
1140 | int mmc_access_rpmb(struct mmc_queue *mq) |
1141 | { | |
7db3028e | 1142 | struct mmc_blk_data *md = mq->blkdata; |
4e93b9a6 CD |
1143 | /* |
1144 | * If this is a RPMB partition access, return ture | |
1145 | */ | |
1146 | if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) | |
1147 | return true; | |
1148 | ||
1149 | return false; | |
1150 | } | |
1151 | ||
bd788c96 AH |
1152 | static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req) |
1153 | { | |
7db3028e | 1154 | struct mmc_blk_data *md = mq->blkdata; |
bd788c96 AH |
1155 | struct mmc_card *card = md->queue.card; |
1156 | unsigned int from, nr, arg; | |
67716327 | 1157 | int err = 0, type = MMC_BLK_DISCARD; |
bd788c96 | 1158 | |
bd788c96 AH |
1159 | if (!mmc_can_erase(card)) { |
1160 | err = -EOPNOTSUPP; | |
8cb6ed17 | 1161 | goto fail; |
bd788c96 AH |
1162 | } |
1163 | ||
1164 | from = blk_rq_pos(req); | |
1165 | nr = blk_rq_sectors(req); | |
1166 | ||
b3bf9153 KP |
1167 | if (mmc_can_discard(card)) |
1168 | arg = MMC_DISCARD_ARG; | |
1169 | else if (mmc_can_trim(card)) | |
bd788c96 AH |
1170 | arg = MMC_TRIM_ARG; |
1171 | else | |
1172 | arg = MMC_ERASE_ARG; | |
164b50b3 GU |
1173 | do { |
1174 | err = 0; | |
1175 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { | |
1176 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
1177 | INAND_CMD38_ARG_EXT_CSD, | |
1178 | arg == MMC_TRIM_ARG ? | |
1179 | INAND_CMD38_ARG_TRIM : | |
1180 | INAND_CMD38_ARG_ERASE, | |
1181 | 0); | |
1182 | } | |
1183 | if (!err) | |
1184 | err = mmc_erase(card, from, nr, arg); | |
1185 | } while (err == -EIO && !mmc_blk_reset(md, card->host, type)); | |
67716327 AH |
1186 | if (!err) |
1187 | mmc_blk_reset_success(md, type); | |
8cb6ed17 | 1188 | fail: |
ecf8b5d0 | 1189 | blk_end_request(req, err, blk_rq_bytes(req)); |
bd788c96 | 1190 | |
bd788c96 AH |
1191 | return err ? 0 : 1; |
1192 | } | |
1193 | ||
49804548 AH |
1194 | static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, |
1195 | struct request *req) | |
1196 | { | |
7db3028e | 1197 | struct mmc_blk_data *md = mq->blkdata; |
49804548 | 1198 | struct mmc_card *card = md->queue.card; |
775a9362 | 1199 | unsigned int from, nr, arg; |
67716327 | 1200 | int err = 0, type = MMC_BLK_SECDISCARD; |
49804548 | 1201 | |
775a9362 | 1202 | if (!(mmc_can_secure_erase_trim(card))) { |
49804548 AH |
1203 | err = -EOPNOTSUPP; |
1204 | goto out; | |
1205 | } | |
1206 | ||
28302812 AH |
1207 | from = blk_rq_pos(req); |
1208 | nr = blk_rq_sectors(req); | |
1209 | ||
775a9362 ME |
1210 | if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr)) |
1211 | arg = MMC_SECURE_TRIM1_ARG; | |
1212 | else | |
1213 | arg = MMC_SECURE_ERASE_ARG; | |
d9ddd629 | 1214 | |
67716327 | 1215 | retry: |
6a7a6b45 AW |
1216 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
1217 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
1218 | INAND_CMD38_ARG_EXT_CSD, | |
1219 | arg == MMC_SECURE_TRIM1_ARG ? | |
1220 | INAND_CMD38_ARG_SECTRIM1 : | |
1221 | INAND_CMD38_ARG_SECERASE, | |
1222 | 0); | |
1223 | if (err) | |
28302812 | 1224 | goto out_retry; |
6a7a6b45 | 1225 | } |
28302812 | 1226 | |
49804548 | 1227 | err = mmc_erase(card, from, nr, arg); |
28302812 AH |
1228 | if (err == -EIO) |
1229 | goto out_retry; | |
1230 | if (err) | |
1231 | goto out; | |
1232 | ||
1233 | if (arg == MMC_SECURE_TRIM1_ARG) { | |
6a7a6b45 AW |
1234 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
1235 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
1236 | INAND_CMD38_ARG_EXT_CSD, | |
1237 | INAND_CMD38_ARG_SECTRIM2, | |
1238 | 0); | |
1239 | if (err) | |
28302812 | 1240 | goto out_retry; |
6a7a6b45 | 1241 | } |
28302812 | 1242 | |
49804548 | 1243 | err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG); |
28302812 AH |
1244 | if (err == -EIO) |
1245 | goto out_retry; | |
1246 | if (err) | |
1247 | goto out; | |
6a7a6b45 | 1248 | } |
28302812 | 1249 | |
28302812 AH |
1250 | out_retry: |
1251 | if (err && !mmc_blk_reset(md, card->host, type)) | |
67716327 AH |
1252 | goto retry; |
1253 | if (!err) | |
1254 | mmc_blk_reset_success(md, type); | |
28302812 | 1255 | out: |
ecf8b5d0 | 1256 | blk_end_request(req, err, blk_rq_bytes(req)); |
49804548 | 1257 | |
49804548 AH |
1258 | return err ? 0 : 1; |
1259 | } | |
1260 | ||
f4c5522b AW |
1261 | static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req) |
1262 | { | |
7db3028e | 1263 | struct mmc_blk_data *md = mq->blkdata; |
881d1c25 SJ |
1264 | struct mmc_card *card = md->queue.card; |
1265 | int ret = 0; | |
1266 | ||
1267 | ret = mmc_flush_cache(card); | |
1268 | if (ret) | |
1269 | ret = -EIO; | |
f4c5522b | 1270 | |
ecf8b5d0 | 1271 | blk_end_request_all(req, ret); |
f4c5522b | 1272 | |
881d1c25 | 1273 | return ret ? 0 : 1; |
f4c5522b AW |
1274 | } |
1275 | ||
1276 | /* | |
1277 | * Reformat current write as a reliable write, supporting | |
1278 | * both legacy and the enhanced reliable write MMC cards. | |
1279 | * In each transfer we'll handle only as much as a single | |
1280 | * reliable write can handle, thus finish the request in | |
1281 | * partial completions. | |
1282 | */ | |
d0c97cfb AW |
1283 | static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, |
1284 | struct mmc_card *card, | |
1285 | struct request *req) | |
f4c5522b | 1286 | { |
f4c5522b AW |
1287 | if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) { |
1288 | /* Legacy mode imposes restrictions on transfers. */ | |
1289 | if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors)) | |
1290 | brq->data.blocks = 1; | |
1291 | ||
1292 | if (brq->data.blocks > card->ext_csd.rel_sectors) | |
1293 | brq->data.blocks = card->ext_csd.rel_sectors; | |
1294 | else if (brq->data.blocks < card->ext_csd.rel_sectors) | |
1295 | brq->data.blocks = 1; | |
1296 | } | |
f4c5522b AW |
1297 | } |
1298 | ||
4c2b8f26 RKAL |
1299 | #define CMD_ERRORS \ |
1300 | (R1_OUT_OF_RANGE | /* Command argument out of range */ \ | |
1301 | R1_ADDRESS_ERROR | /* Misaligned address */ \ | |
1302 | R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\ | |
1303 | R1_WP_VIOLATION | /* Tried to write to protected block */ \ | |
1304 | R1_CC_ERROR | /* Card controller error */ \ | |
1305 | R1_ERROR) /* General/unknown error */ | |
1306 | ||
8e8b3f51 LW |
1307 | static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card, |
1308 | struct mmc_async_req *areq) | |
d78d4a8a | 1309 | { |
ee8a43a5 PF |
1310 | struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req, |
1311 | mmc_active); | |
1312 | struct mmc_blk_request *brq = &mq_mrq->brq; | |
1313 | struct request *req = mq_mrq->req; | |
b8360a49 | 1314 | int need_retune = card->host->need_retune; |
2cc64587 | 1315 | bool ecc_err = false; |
c44d6cef | 1316 | bool gen_err = false; |
d78d4a8a PF |
1317 | |
1318 | /* | |
1319 | * sbc.error indicates a problem with the set block count | |
1320 | * command. No data will have been transferred. | |
1321 | * | |
1322 | * cmd.error indicates a problem with the r/w command. No | |
1323 | * data will have been transferred. | |
1324 | * | |
1325 | * stop.error indicates a problem with the stop command. Data | |
1326 | * may have been transferred, or may still be transferring. | |
1327 | */ | |
67716327 AH |
1328 | if (brq->sbc.error || brq->cmd.error || brq->stop.error || |
1329 | brq->data.error) { | |
c8760069 | 1330 | switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) { |
d78d4a8a PF |
1331 | case ERR_RETRY: |
1332 | return MMC_BLK_RETRY; | |
1333 | case ERR_ABORT: | |
1334 | return MMC_BLK_ABORT; | |
a8ad82cc SRT |
1335 | case ERR_NOMEDIUM: |
1336 | return MMC_BLK_NOMEDIUM; | |
d78d4a8a PF |
1337 | case ERR_CONTINUE: |
1338 | break; | |
1339 | } | |
1340 | } | |
1341 | ||
1342 | /* | |
1343 | * Check for errors relating to the execution of the | |
1344 | * initial command - such as address errors. No data | |
1345 | * has been transferred. | |
1346 | */ | |
1347 | if (brq->cmd.resp[0] & CMD_ERRORS) { | |
1348 | pr_err("%s: r/w command failed, status = %#x\n", | |
1349 | req->rq_disk->disk_name, brq->cmd.resp[0]); | |
1350 | return MMC_BLK_ABORT; | |
1351 | } | |
1352 | ||
1353 | /* | |
1354 | * Everything else is either success, or a data error of some | |
1355 | * kind. If it was a write, we may have transitioned to | |
1356 | * program mode, which we have to wait for it to complete. | |
1357 | */ | |
1358 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) { | |
c49433fb | 1359 | int err; |
8fee476b | 1360 | |
c8760069 KY |
1361 | /* Check stop command response */ |
1362 | if (brq->stop.resp[0] & R1_ERROR) { | |
1363 | pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n", | |
1364 | req->rq_disk->disk_name, __func__, | |
1365 | brq->stop.resp[0]); | |
c44d6cef | 1366 | gen_err = true; |
c8760069 KY |
1367 | } |
1368 | ||
95a91298 UH |
1369 | err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req, |
1370 | &gen_err); | |
c49433fb UH |
1371 | if (err) |
1372 | return MMC_BLK_CMD_ERR; | |
d78d4a8a PF |
1373 | } |
1374 | ||
c8760069 KY |
1375 | /* if general error occurs, retry the write operation. */ |
1376 | if (gen_err) { | |
1377 | pr_warn("%s: retrying write for general error\n", | |
1378 | req->rq_disk->disk_name); | |
1379 | return MMC_BLK_RETRY; | |
1380 | } | |
1381 | ||
d78d4a8a | 1382 | if (brq->data.error) { |
b8360a49 | 1383 | if (need_retune && !brq->retune_retry_done) { |
09faf61d RK |
1384 | pr_debug("%s: retrying because a re-tune was needed\n", |
1385 | req->rq_disk->disk_name); | |
b8360a49 AH |
1386 | brq->retune_retry_done = 1; |
1387 | return MMC_BLK_RETRY; | |
1388 | } | |
d78d4a8a PF |
1389 | pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n", |
1390 | req->rq_disk->disk_name, brq->data.error, | |
1391 | (unsigned)blk_rq_pos(req), | |
1392 | (unsigned)blk_rq_sectors(req), | |
1393 | brq->cmd.resp[0], brq->stop.resp[0]); | |
1394 | ||
1395 | if (rq_data_dir(req) == READ) { | |
67716327 AH |
1396 | if (ecc_err) |
1397 | return MMC_BLK_ECC_ERR; | |
d78d4a8a PF |
1398 | return MMC_BLK_DATA_ERR; |
1399 | } else { | |
1400 | return MMC_BLK_CMD_ERR; | |
1401 | } | |
1402 | } | |
1403 | ||
67716327 AH |
1404 | if (!brq->data.bytes_xfered) |
1405 | return MMC_BLK_RETRY; | |
d78d4a8a | 1406 | |
67716327 AH |
1407 | if (blk_rq_bytes(req) != brq->data.bytes_xfered) |
1408 | return MMC_BLK_PARTIAL; | |
1409 | ||
1410 | return MMC_BLK_SUCCESS; | |
d78d4a8a PF |
1411 | } |
1412 | ||
54d49d77 PF |
1413 | static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, |
1414 | struct mmc_card *card, | |
1415 | int disable_multi, | |
1416 | struct mmc_queue *mq) | |
1da177e4 | 1417 | { |
54d49d77 PF |
1418 | u32 readcmd, writecmd; |
1419 | struct mmc_blk_request *brq = &mqrq->brq; | |
1420 | struct request *req = mqrq->req; | |
7db3028e | 1421 | struct mmc_blk_data *md = mq->blkdata; |
4265900e | 1422 | bool do_data_tag; |
1da177e4 | 1423 | |
f4c5522b AW |
1424 | /* |
1425 | * Reliable writes are used to implement Forced Unit Access and | |
d3df0465 | 1426 | * are supported only on MMCs. |
f4c5522b | 1427 | */ |
d3df0465 | 1428 | bool do_rel_wr = (req->cmd_flags & REQ_FUA) && |
f4c5522b | 1429 | (rq_data_dir(req) == WRITE) && |
d0c97cfb | 1430 | (md->flags & MMC_BLK_REL_WR); |
f4c5522b | 1431 | |
54d49d77 PF |
1432 | memset(brq, 0, sizeof(struct mmc_blk_request)); |
1433 | brq->mrq.cmd = &brq->cmd; | |
1434 | brq->mrq.data = &brq->data; | |
1da177e4 | 1435 | |
54d49d77 PF |
1436 | brq->cmd.arg = blk_rq_pos(req); |
1437 | if (!mmc_card_blockaddr(card)) | |
1438 | brq->cmd.arg <<= 9; | |
1439 | brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; | |
1440 | brq->data.blksz = 512; | |
1441 | brq->stop.opcode = MMC_STOP_TRANSMISSION; | |
1442 | brq->stop.arg = 0; | |
54d49d77 | 1443 | brq->data.blocks = blk_rq_sectors(req); |
6a79e391 | 1444 | |
54d49d77 PF |
1445 | /* |
1446 | * The block layer doesn't support all sector count | |
1447 | * restrictions, so we need to be prepared for too big | |
1448 | * requests. | |
1449 | */ | |
1450 | if (brq->data.blocks > card->host->max_blk_count) | |
1451 | brq->data.blocks = card->host->max_blk_count; | |
1da177e4 | 1452 | |
2bf22b39 PW |
1453 | if (brq->data.blocks > 1) { |
1454 | /* | |
1455 | * After a read error, we redo the request one sector | |
1456 | * at a time in order to accurately determine which | |
1457 | * sectors can be read successfully. | |
1458 | */ | |
1459 | if (disable_multi) | |
1460 | brq->data.blocks = 1; | |
1461 | ||
2e47e842 KM |
1462 | /* |
1463 | * Some controllers have HW issues while operating | |
1464 | * in multiple I/O mode | |
1465 | */ | |
1466 | if (card->host->ops->multi_io_quirk) | |
1467 | brq->data.blocks = card->host->ops->multi_io_quirk(card, | |
1468 | (rq_data_dir(req) == READ) ? | |
1469 | MMC_DATA_READ : MMC_DATA_WRITE, | |
1470 | brq->data.blocks); | |
2bf22b39 | 1471 | } |
d0c97cfb | 1472 | |
54d49d77 PF |
1473 | if (brq->data.blocks > 1 || do_rel_wr) { |
1474 | /* SPI multiblock writes terminate using a special | |
1475 | * token, not a STOP_TRANSMISSION request. | |
d0c97cfb | 1476 | */ |
54d49d77 PF |
1477 | if (!mmc_host_is_spi(card->host) || |
1478 | rq_data_dir(req) == READ) | |
1479 | brq->mrq.stop = &brq->stop; | |
1480 | readcmd = MMC_READ_MULTIPLE_BLOCK; | |
1481 | writecmd = MMC_WRITE_MULTIPLE_BLOCK; | |
1482 | } else { | |
1483 | brq->mrq.stop = NULL; | |
1484 | readcmd = MMC_READ_SINGLE_BLOCK; | |
1485 | writecmd = MMC_WRITE_BLOCK; | |
1486 | } | |
1487 | if (rq_data_dir(req) == READ) { | |
1488 | brq->cmd.opcode = readcmd; | |
f53f1102 | 1489 | brq->data.flags = MMC_DATA_READ; |
bcc3e172 UH |
1490 | if (brq->mrq.stop) |
1491 | brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | | |
1492 | MMC_CMD_AC; | |
54d49d77 PF |
1493 | } else { |
1494 | brq->cmd.opcode = writecmd; | |
f53f1102 | 1495 | brq->data.flags = MMC_DATA_WRITE; |
bcc3e172 UH |
1496 | if (brq->mrq.stop) |
1497 | brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | | |
1498 | MMC_CMD_AC; | |
54d49d77 | 1499 | } |
d0c97cfb | 1500 | |
54d49d77 PF |
1501 | if (do_rel_wr) |
1502 | mmc_apply_rel_rw(brq, card, req); | |
f4c5522b | 1503 | |
4265900e SD |
1504 | /* |
1505 | * Data tag is used only during writing meta data to speed | |
1506 | * up write and any subsequent read of this meta data | |
1507 | */ | |
1508 | do_data_tag = (card->ext_csd.data_tag_unit_size) && | |
1509 | (req->cmd_flags & REQ_META) && | |
1510 | (rq_data_dir(req) == WRITE) && | |
1511 | ((brq->data.blocks * brq->data.blksz) >= | |
1512 | card->ext_csd.data_tag_unit_size); | |
1513 | ||
54d49d77 PF |
1514 | /* |
1515 | * Pre-defined multi-block transfers are preferable to | |
1516 | * open ended-ones (and necessary for reliable writes). | |
1517 | * However, it is not sufficient to just send CMD23, | |
1518 | * and avoid the final CMD12, as on an error condition | |
1519 | * CMD12 (stop) needs to be sent anyway. This, coupled | |
1520 | * with Auto-CMD23 enhancements provided by some | |
1521 | * hosts, means that the complexity of dealing | |
1522 | * with this is best left to the host. If CMD23 is | |
1523 | * supported by card and host, we'll fill sbc in and let | |
1524 | * the host deal with handling it correctly. This means | |
1525 | * that for hosts that don't expose MMC_CAP_CMD23, no | |
1526 | * change of behavior will be observed. | |
1527 | * | |
1528 | * N.B: Some MMC cards experience perf degradation. | |
1529 | * We'll avoid using CMD23-bounded multiblock writes for | |
1530 | * these, while retaining features like reliable writes. | |
1531 | */ | |
4265900e SD |
1532 | if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) && |
1533 | (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) || | |
1534 | do_data_tag)) { | |
54d49d77 PF |
1535 | brq->sbc.opcode = MMC_SET_BLOCK_COUNT; |
1536 | brq->sbc.arg = brq->data.blocks | | |
4265900e SD |
1537 | (do_rel_wr ? (1 << 31) : 0) | |
1538 | (do_data_tag ? (1 << 29) : 0); | |
54d49d77 PF |
1539 | brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; |
1540 | brq->mrq.sbc = &brq->sbc; | |
1541 | } | |
98ccf149 | 1542 | |
54d49d77 PF |
1543 | mmc_set_data_timeout(&brq->data, card); |
1544 | ||
1545 | brq->data.sg = mqrq->sg; | |
1546 | brq->data.sg_len = mmc_queue_map_sg(mq, mqrq); | |
1547 | ||
1548 | /* | |
1549 | * Adjust the sg list so it is the same size as the | |
1550 | * request. | |
1551 | */ | |
1552 | if (brq->data.blocks != blk_rq_sectors(req)) { | |
1553 | int i, data_size = brq->data.blocks << 9; | |
1554 | struct scatterlist *sg; | |
1555 | ||
1556 | for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) { | |
1557 | data_size -= sg->length; | |
1558 | if (data_size <= 0) { | |
1559 | sg->length += data_size; | |
1560 | i++; | |
1561 | break; | |
6a79e391 | 1562 | } |
6a79e391 | 1563 | } |
54d49d77 PF |
1564 | brq->data.sg_len = i; |
1565 | } | |
1566 | ||
ee8a43a5 PF |
1567 | mqrq->mmc_active.mrq = &brq->mrq; |
1568 | mqrq->mmc_active.err_check = mmc_blk_err_check; | |
1569 | ||
54d49d77 PF |
1570 | mmc_queue_bounce_pre(mqrq); |
1571 | } | |
6a79e391 | 1572 | |
67716327 AH |
1573 | static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card, |
1574 | struct mmc_blk_request *brq, struct request *req, | |
1575 | int ret) | |
1576 | { | |
ce39f9d1 SJ |
1577 | struct mmc_queue_req *mq_rq; |
1578 | mq_rq = container_of(brq, struct mmc_queue_req, brq); | |
1579 | ||
67716327 AH |
1580 | /* |
1581 | * If this is an SD card and we're writing, we can first | |
1582 | * mark the known good sectors as ok. | |
1583 | * | |
1584 | * If the card is not SD, we can still ok written sectors | |
1585 | * as reported by the controller (which might be less than | |
1586 | * the real number of written sectors, but never more). | |
1587 | */ | |
1588 | if (mmc_card_sd(card)) { | |
1589 | u32 blocks; | |
1590 | ||
1591 | blocks = mmc_sd_num_wr_blocks(card); | |
1592 | if (blocks != (u32)-1) { | |
ecf8b5d0 | 1593 | ret = blk_end_request(req, 0, blocks << 9); |
67716327 | 1594 | } |
5dd784d2 AH |
1595 | } else { |
1596 | ret = blk_end_request(req, 0, brq->data.bytes_xfered); | |
ce39f9d1 | 1597 | } |
ce39f9d1 SJ |
1598 | return ret; |
1599 | } | |
1600 | ||
4e1f7800 LW |
1601 | static void mmc_blk_rw_cmd_abort(struct mmc_card *card, struct request *req) |
1602 | { | |
1603 | int ret = 1; | |
1604 | ||
1605 | if (mmc_card_removed(card)) | |
1606 | req->rq_flags |= RQF_QUIET; | |
1607 | while (ret) | |
1608 | ret = blk_end_request(req, -EIO, | |
1609 | blk_rq_cur_bytes(req)); | |
1610 | } | |
1611 | ||
efb5a05e LW |
1612 | static void mmc_blk_rw_start_new(struct mmc_queue *mq, struct mmc_card *card, |
1613 | struct request *req) | |
1614 | { | |
1615 | if (!req) | |
1616 | return; | |
1617 | ||
1618 | if (mmc_card_removed(card)) { | |
1619 | req->rq_flags |= RQF_QUIET; | |
1620 | blk_end_request_all(req, -EIO); | |
1621 | } else { | |
1622 | mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq); | |
1623 | mmc_start_req(card->host, | |
1624 | &mq->mqrq_cur->mmc_active, NULL); | |
1625 | } | |
1626 | } | |
1627 | ||
ee8a43a5 | 1628 | static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc) |
54d49d77 | 1629 | { |
7db3028e | 1630 | struct mmc_blk_data *md = mq->blkdata; |
54d49d77 | 1631 | struct mmc_card *card = md->queue.card; |
5be80375 | 1632 | struct mmc_blk_request *brq; |
b8360a49 | 1633 | int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0; |
d78d4a8a | 1634 | enum mmc_blk_status status; |
ee8a43a5 | 1635 | struct mmc_queue_req *mq_rq; |
5be80375 | 1636 | struct request *req; |
7d552a48 LW |
1637 | struct mmc_async_req *new_areq; |
1638 | struct mmc_async_req *old_areq; | |
1da177e4 | 1639 | |
ee8a43a5 PF |
1640 | if (!rqc && !mq->mqrq_prev->req) |
1641 | return 0; | |
98ccf149 | 1642 | |
ee8a43a5 PF |
1643 | do { |
1644 | if (rqc) { | |
a5075eb9 SD |
1645 | /* |
1646 | * When 4KB native sector is enabled, only 8 blocks | |
1647 | * multiple read or write is allowed | |
1648 | */ | |
e87c8561 YJ |
1649 | if (mmc_large_sector(card) && |
1650 | !IS_ALIGNED(blk_rq_sectors(rqc), 8)) { | |
a5075eb9 | 1651 | pr_err("%s: Transfer size is not 4KB sector size aligned\n", |
5be80375 | 1652 | rqc->rq_disk->disk_name); |
da0dbaff LW |
1653 | mmc_blk_rw_cmd_abort(card, rqc); |
1654 | return 0; | |
a5075eb9 | 1655 | } |
ce39f9d1 | 1656 | |
03d640ae | 1657 | mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq); |
7d552a48 | 1658 | new_areq = &mq->mqrq_cur->mmc_active; |
ee8a43a5 | 1659 | } else |
7d552a48 LW |
1660 | new_areq = NULL; |
1661 | ||
1662 | old_areq = mmc_start_req(card->host, new_areq, &status); | |
1663 | if (!old_areq) { | |
da0dbaff LW |
1664 | /* |
1665 | * We have just put the first request into the pipeline | |
1666 | * and there is nothing more to do until it is | |
1667 | * complete. | |
1668 | */ | |
2220eedf KD |
1669 | if (status == MMC_BLK_NEW_REQUEST) |
1670 | mq->flags |= MMC_QUEUE_NEW_REQUEST; | |
ee8a43a5 | 1671 | return 0; |
2220eedf | 1672 | } |
ee8a43a5 | 1673 | |
da0dbaff LW |
1674 | /* |
1675 | * An asynchronous request has been completed and we proceed | |
1676 | * to handle the result of it. | |
1677 | */ | |
7d552a48 | 1678 | mq_rq = container_of(old_areq, struct mmc_queue_req, mmc_active); |
ee8a43a5 PF |
1679 | brq = &mq_rq->brq; |
1680 | req = mq_rq->req; | |
67716327 | 1681 | type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; |
ee8a43a5 | 1682 | mmc_queue_bounce_post(mq_rq); |
98ccf149 | 1683 | |
d78d4a8a PF |
1684 | switch (status) { |
1685 | case MMC_BLK_SUCCESS: | |
1686 | case MMC_BLK_PARTIAL: | |
1687 | /* | |
1688 | * A block was successfully transferred. | |
1689 | */ | |
67716327 | 1690 | mmc_blk_reset_success(md, type); |
ce39f9d1 | 1691 | |
03d640ae LW |
1692 | ret = blk_end_request(req, 0, |
1693 | brq->data.bytes_xfered); | |
ce39f9d1 | 1694 | |
67716327 AH |
1695 | /* |
1696 | * If the blk_end_request function returns non-zero even | |
1697 | * though all data has been transferred and no errors | |
1698 | * were returned by the host controller, it's a bug. | |
1699 | */ | |
ee8a43a5 | 1700 | if (status == MMC_BLK_SUCCESS && ret) { |
a3c76eb9 | 1701 | pr_err("%s BUG rq_tot %d d_xfer %d\n", |
ee8a43a5 PF |
1702 | __func__, blk_rq_bytes(req), |
1703 | brq->data.bytes_xfered); | |
da0dbaff LW |
1704 | mmc_blk_rw_cmd_abort(card, req); |
1705 | return 0; | |
ee8a43a5 | 1706 | } |
d78d4a8a PF |
1707 | break; |
1708 | case MMC_BLK_CMD_ERR: | |
67716327 | 1709 | ret = mmc_blk_cmd_err(md, card, brq, req, ret); |
29535f7b DW |
1710 | if (mmc_blk_reset(md, card->host, type)) |
1711 | goto cmd_abort; | |
1712 | if (!ret) | |
1713 | goto start_new_req; | |
1714 | break; | |
d78d4a8a | 1715 | case MMC_BLK_RETRY: |
b8360a49 | 1716 | retune_retry_done = brq->retune_retry_done; |
d78d4a8a | 1717 | if (retry++ < 5) |
a01f3ccf | 1718 | break; |
67716327 | 1719 | /* Fall through */ |
d78d4a8a | 1720 | case MMC_BLK_ABORT: |
67716327 AH |
1721 | if (!mmc_blk_reset(md, card->host, type)) |
1722 | break; | |
4c2b8f26 | 1723 | goto cmd_abort; |
67716327 AH |
1724 | case MMC_BLK_DATA_ERR: { |
1725 | int err; | |
1726 | ||
1727 | err = mmc_blk_reset(md, card->host, type); | |
1728 | if (!err) | |
1729 | break; | |
03d640ae | 1730 | if (err == -ENODEV) |
67716327 AH |
1731 | goto cmd_abort; |
1732 | /* Fall through */ | |
1733 | } | |
1734 | case MMC_BLK_ECC_ERR: | |
1735 | if (brq->data.blocks > 1) { | |
1736 | /* Redo read one sector at a time */ | |
6606110d JP |
1737 | pr_warn("%s: retrying using single block read\n", |
1738 | req->rq_disk->disk_name); | |
67716327 AH |
1739 | disable_multi = 1; |
1740 | break; | |
1741 | } | |
d78d4a8a PF |
1742 | /* |
1743 | * After an error, we redo I/O one sector at a | |
1744 | * time, so we only reach here after trying to | |
1745 | * read a single sector. | |
1746 | */ | |
ecf8b5d0 | 1747 | ret = blk_end_request(req, -EIO, |
d78d4a8a | 1748 | brq->data.blksz); |
ee8a43a5 PF |
1749 | if (!ret) |
1750 | goto start_new_req; | |
d78d4a8a | 1751 | break; |
a8ad82cc SRT |
1752 | case MMC_BLK_NOMEDIUM: |
1753 | goto cmd_abort; | |
2220eedf KD |
1754 | default: |
1755 | pr_err("%s: Unhandled return value (%d)", | |
1756 | req->rq_disk->disk_name, status); | |
1757 | goto cmd_abort; | |
4c2b8f26 RKAL |
1758 | } |
1759 | ||
ee8a43a5 | 1760 | if (ret) { |
03d640ae LW |
1761 | /* |
1762 | * In case of a incomplete request | |
1763 | * prepare it again and resend. | |
1764 | */ | |
1765 | mmc_blk_rw_rq_prep(mq_rq, card, | |
1766 | disable_multi, mq); | |
1767 | mmc_start_req(card->host, | |
1768 | &mq_rq->mmc_active, NULL); | |
b8360a49 | 1769 | mq_rq->brq.retune_retry_done = retune_retry_done; |
ee8a43a5 | 1770 | } |
1da177e4 LT |
1771 | } while (ret); |
1772 | ||
1da177e4 LT |
1773 | return 1; |
1774 | ||
a01f3ccf | 1775 | cmd_abort: |
4e1f7800 | 1776 | mmc_blk_rw_cmd_abort(card, req); |
1da177e4 | 1777 | |
ee8a43a5 | 1778 | start_new_req: |
efb5a05e | 1779 | mmc_blk_rw_start_new(mq, card, rqc); |
ee8a43a5 | 1780 | |
1da177e4 LT |
1781 | return 0; |
1782 | } | |
1783 | ||
29eb7bd0 | 1784 | int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) |
bd788c96 | 1785 | { |
1a258db6 | 1786 | int ret; |
7db3028e | 1787 | struct mmc_blk_data *md = mq->blkdata; |
1a258db6 | 1788 | struct mmc_card *card = md->queue.card; |
869c5548 | 1789 | bool req_is_special = mmc_req_is_special(req); |
1a258db6 | 1790 | |
ee8a43a5 PF |
1791 | if (req && !mq->mqrq_prev->req) |
1792 | /* claim host only for the first request */ | |
e94cfef6 | 1793 | mmc_get_card(card); |
ee8a43a5 | 1794 | |
371a689f AW |
1795 | ret = mmc_blk_part_switch(card, md); |
1796 | if (ret) { | |
0d7d85ca | 1797 | if (req) { |
ecf8b5d0 | 1798 | blk_end_request_all(req, -EIO); |
0d7d85ca | 1799 | } |
371a689f AW |
1800 | ret = 0; |
1801 | goto out; | |
1802 | } | |
1a258db6 | 1803 | |
2220eedf | 1804 | mq->flags &= ~MMC_QUEUE_NEW_REQUEST; |
c2df40df | 1805 | if (req && req_op(req) == REQ_OP_DISCARD) { |
ee8a43a5 PF |
1806 | /* complete ongoing async transfer before issuing discard */ |
1807 | if (card->host->areq) | |
1808 | mmc_blk_issue_rw_rq(mq, NULL); | |
288dab8a CH |
1809 | ret = mmc_blk_issue_discard_rq(mq, req); |
1810 | } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) { | |
1811 | /* complete ongoing async transfer before issuing secure erase*/ | |
1812 | if (card->host->areq) | |
1813 | mmc_blk_issue_rw_rq(mq, NULL); | |
1814 | ret = mmc_blk_issue_secdiscard_rq(mq, req); | |
3a5e02ce | 1815 | } else if (req && req_op(req) == REQ_OP_FLUSH) { |
393f9a08 JC |
1816 | /* complete ongoing async transfer before issuing flush */ |
1817 | if (card->host->areq) | |
1818 | mmc_blk_issue_rw_rq(mq, NULL); | |
1a258db6 | 1819 | ret = mmc_blk_issue_flush(mq, req); |
49804548 | 1820 | } else { |
1a258db6 | 1821 | ret = mmc_blk_issue_rw_rq(mq, req); |
49804548 | 1822 | } |
1a258db6 | 1823 | |
371a689f | 1824 | out: |
869c5548 | 1825 | if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special) |
ef3a69c7 SJ |
1826 | /* |
1827 | * Release host when there are no more requests | |
1828 | * and after special request(discard, flush) is done. | |
1829 | * In case sepecial request, there is no reentry to | |
1830 | * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'. | |
1831 | */ | |
e94cfef6 | 1832 | mmc_put_card(card); |
1a258db6 | 1833 | return ret; |
bd788c96 | 1834 | } |
1da177e4 | 1835 | |
a6f6c96b RK |
1836 | static inline int mmc_blk_readonly(struct mmc_card *card) |
1837 | { | |
1838 | return mmc_card_readonly(card) || | |
1839 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); | |
1840 | } | |
1841 | ||
371a689f AW |
1842 | static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, |
1843 | struct device *parent, | |
1844 | sector_t size, | |
1845 | bool default_ro, | |
add710ea JR |
1846 | const char *subname, |
1847 | int area_type) | |
1da177e4 LT |
1848 | { |
1849 | struct mmc_blk_data *md; | |
1850 | int devidx, ret; | |
1851 | ||
b10fa99e UH |
1852 | again: |
1853 | if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL)) | |
1854 | return ERR_PTR(-ENOMEM); | |
1855 | ||
1856 | spin_lock(&mmc_blk_lock); | |
1857 | ret = ida_get_new(&mmc_blk_ida, &devidx); | |
1858 | spin_unlock(&mmc_blk_lock); | |
1859 | ||
1860 | if (ret == -EAGAIN) | |
1861 | goto again; | |
1862 | else if (ret) | |
1863 | return ERR_PTR(ret); | |
1864 | ||
1865 | if (devidx >= max_devices) { | |
1866 | ret = -ENOSPC; | |
1867 | goto out; | |
1868 | } | |
1da177e4 | 1869 | |
dd00cc48 | 1870 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); |
a6f6c96b RK |
1871 | if (!md) { |
1872 | ret = -ENOMEM; | |
1873 | goto out; | |
1874 | } | |
1da177e4 | 1875 | |
add710ea JR |
1876 | md->area_type = area_type; |
1877 | ||
a6f6c96b RK |
1878 | /* |
1879 | * Set the read-only status based on the supported commands | |
1880 | * and the write protect switch. | |
1881 | */ | |
1882 | md->read_only = mmc_blk_readonly(card); | |
1da177e4 | 1883 | |
5e71b7a6 | 1884 | md->disk = alloc_disk(perdev_minors); |
a6f6c96b RK |
1885 | if (md->disk == NULL) { |
1886 | ret = -ENOMEM; | |
1887 | goto err_kfree; | |
1888 | } | |
1da177e4 | 1889 | |
a6f6c96b | 1890 | spin_lock_init(&md->lock); |
371a689f | 1891 | INIT_LIST_HEAD(&md->part); |
a6f6c96b | 1892 | md->usage = 1; |
1da177e4 | 1893 | |
d09408ad | 1894 | ret = mmc_init_queue(&md->queue, card, &md->lock, subname); |
a6f6c96b RK |
1895 | if (ret) |
1896 | goto err_putdisk; | |
1da177e4 | 1897 | |
7db3028e | 1898 | md->queue.blkdata = md; |
d2b18394 | 1899 | |
fe6b4c88 | 1900 | md->disk->major = MMC_BLOCK_MAJOR; |
5e71b7a6 | 1901 | md->disk->first_minor = devidx * perdev_minors; |
a6f6c96b RK |
1902 | md->disk->fops = &mmc_bdops; |
1903 | md->disk->private_data = md; | |
1904 | md->disk->queue = md->queue.queue; | |
307d8e6f | 1905 | md->parent = parent; |
371a689f | 1906 | set_disk_ro(md->disk, md->read_only || default_ro); |
382c55f8 | 1907 | md->disk->flags = GENHD_FL_EXT_DEVT; |
f5b4d71f | 1908 | if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT)) |
53d8f974 | 1909 | md->disk->flags |= GENHD_FL_NO_PART_SCAN; |
a6f6c96b RK |
1910 | |
1911 | /* | |
1912 | * As discussed on lkml, GENHD_FL_REMOVABLE should: | |
1913 | * | |
1914 | * - be set for removable media with permanent block devices | |
1915 | * - be unset for removable block devices with permanent media | |
1916 | * | |
1917 | * Since MMC block devices clearly fall under the second | |
1918 | * case, we do not set GENHD_FL_REMOVABLE. Userspace | |
1919 | * should use the block device creation/destruction hotplug | |
1920 | * messages to tell when the card is present. | |
1921 | */ | |
1922 | ||
f06c9153 | 1923 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
9aaf3437 | 1924 | "mmcblk%u%s", card->host->index, subname ? subname : ""); |
a6f6c96b | 1925 | |
a5075eb9 SD |
1926 | if (mmc_card_mmc(card)) |
1927 | blk_queue_logical_block_size(md->queue.queue, | |
1928 | card->ext_csd.data_sector_size); | |
1929 | else | |
1930 | blk_queue_logical_block_size(md->queue.queue, 512); | |
1931 | ||
371a689f | 1932 | set_capacity(md->disk, size); |
d0c97cfb | 1933 | |
f0d89972 | 1934 | if (mmc_host_cmd23(card->host)) { |
0ed50abb DG |
1935 | if ((mmc_card_mmc(card) && |
1936 | card->csd.mmca_vsn >= CSD_SPEC_VER_3) || | |
f0d89972 AW |
1937 | (mmc_card_sd(card) && |
1938 | card->scr.cmds & SD_SCR_CMD23_SUPPORT)) | |
1939 | md->flags |= MMC_BLK_CMD23; | |
1940 | } | |
d0c97cfb AW |
1941 | |
1942 | if (mmc_card_mmc(card) && | |
1943 | md->flags & MMC_BLK_CMD23 && | |
1944 | ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || | |
1945 | card->ext_csd.rel_sectors)) { | |
1946 | md->flags |= MMC_BLK_REL_WR; | |
e9d5c746 | 1947 | blk_queue_write_cache(md->queue.queue, true, true); |
d0c97cfb AW |
1948 | } |
1949 | ||
371a689f AW |
1950 | return md; |
1951 | ||
1952 | err_putdisk: | |
1953 | put_disk(md->disk); | |
1954 | err_kfree: | |
1955 | kfree(md); | |
1956 | out: | |
b10fa99e UH |
1957 | spin_lock(&mmc_blk_lock); |
1958 | ida_remove(&mmc_blk_ida, devidx); | |
1959 | spin_unlock(&mmc_blk_lock); | |
371a689f AW |
1960 | return ERR_PTR(ret); |
1961 | } | |
1962 | ||
1963 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) | |
1964 | { | |
1965 | sector_t size; | |
a6f6c96b | 1966 | |
85a18ad9 PO |
1967 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { |
1968 | /* | |
1969 | * The EXT_CSD sector count is in number or 512 byte | |
1970 | * sectors. | |
1971 | */ | |
371a689f | 1972 | size = card->ext_csd.sectors; |
85a18ad9 PO |
1973 | } else { |
1974 | /* | |
1975 | * The CSD capacity field is in units of read_blkbits. | |
1976 | * set_capacity takes units of 512 bytes. | |
1977 | */ | |
087de9ed KM |
1978 | size = (typeof(sector_t))card->csd.capacity |
1979 | << (card->csd.read_blkbits - 9); | |
85a18ad9 | 1980 | } |
371a689f | 1981 | |
7a30f2af | 1982 | return mmc_blk_alloc_req(card, &card->dev, size, false, NULL, |
add710ea | 1983 | MMC_BLK_DATA_AREA_MAIN); |
371a689f | 1984 | } |
a6f6c96b | 1985 | |
371a689f AW |
1986 | static int mmc_blk_alloc_part(struct mmc_card *card, |
1987 | struct mmc_blk_data *md, | |
1988 | unsigned int part_type, | |
1989 | sector_t size, | |
1990 | bool default_ro, | |
add710ea JR |
1991 | const char *subname, |
1992 | int area_type) | |
371a689f AW |
1993 | { |
1994 | char cap_str[10]; | |
1995 | struct mmc_blk_data *part_md; | |
1996 | ||
1997 | part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro, | |
add710ea | 1998 | subname, area_type); |
371a689f AW |
1999 | if (IS_ERR(part_md)) |
2000 | return PTR_ERR(part_md); | |
2001 | part_md->part_type = part_type; | |
2002 | list_add(&part_md->part, &md->part); | |
2003 | ||
b9f28d86 | 2004 | string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2, |
371a689f | 2005 | cap_str, sizeof(cap_str)); |
a3c76eb9 | 2006 | pr_info("%s: %s %s partition %u %s\n", |
371a689f AW |
2007 | part_md->disk->disk_name, mmc_card_id(card), |
2008 | mmc_card_name(card), part_md->part_type, cap_str); | |
2009 | return 0; | |
2010 | } | |
2011 | ||
e0c368d5 NJ |
2012 | /* MMC Physical partitions consist of two boot partitions and |
2013 | * up to four general purpose partitions. | |
2014 | * For each partition enabled in EXT_CSD a block device will be allocatedi | |
2015 | * to provide access to the partition. | |
2016 | */ | |
2017 | ||
371a689f AW |
2018 | static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) |
2019 | { | |
e0c368d5 | 2020 | int idx, ret = 0; |
371a689f AW |
2021 | |
2022 | if (!mmc_card_mmc(card)) | |
2023 | return 0; | |
2024 | ||
e0c368d5 NJ |
2025 | for (idx = 0; idx < card->nr_parts; idx++) { |
2026 | if (card->part[idx].size) { | |
2027 | ret = mmc_blk_alloc_part(card, md, | |
2028 | card->part[idx].part_cfg, | |
2029 | card->part[idx].size >> 9, | |
2030 | card->part[idx].force_ro, | |
add710ea JR |
2031 | card->part[idx].name, |
2032 | card->part[idx].area_type); | |
e0c368d5 NJ |
2033 | if (ret) |
2034 | return ret; | |
2035 | } | |
371a689f AW |
2036 | } |
2037 | ||
2038 | return ret; | |
1da177e4 LT |
2039 | } |
2040 | ||
371a689f AW |
2041 | static void mmc_blk_remove_req(struct mmc_blk_data *md) |
2042 | { | |
add710ea JR |
2043 | struct mmc_card *card; |
2044 | ||
371a689f | 2045 | if (md) { |
fdfa20c1 PT |
2046 | /* |
2047 | * Flush remaining requests and free queues. It | |
2048 | * is freeing the queue that stops new requests | |
2049 | * from being accepted. | |
2050 | */ | |
8efb83a2 | 2051 | card = md->queue.card; |
fdfa20c1 | 2052 | mmc_cleanup_queue(&md->queue); |
371a689f AW |
2053 | if (md->disk->flags & GENHD_FL_UP) { |
2054 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); | |
add710ea JR |
2055 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && |
2056 | card->ext_csd.boot_ro_lockable) | |
2057 | device_remove_file(disk_to_dev(md->disk), | |
2058 | &md->power_ro_lock); | |
371a689f | 2059 | |
371a689f AW |
2060 | del_gendisk(md->disk); |
2061 | } | |
371a689f AW |
2062 | mmc_blk_put(md); |
2063 | } | |
2064 | } | |
2065 | ||
2066 | static void mmc_blk_remove_parts(struct mmc_card *card, | |
2067 | struct mmc_blk_data *md) | |
2068 | { | |
2069 | struct list_head *pos, *q; | |
2070 | struct mmc_blk_data *part_md; | |
2071 | ||
2072 | list_for_each_safe(pos, q, &md->part) { | |
2073 | part_md = list_entry(pos, struct mmc_blk_data, part); | |
2074 | list_del(pos); | |
2075 | mmc_blk_remove_req(part_md); | |
2076 | } | |
2077 | } | |
2078 | ||
2079 | static int mmc_add_disk(struct mmc_blk_data *md) | |
2080 | { | |
2081 | int ret; | |
add710ea | 2082 | struct mmc_card *card = md->queue.card; |
371a689f | 2083 | |
307d8e6f | 2084 | device_add_disk(md->parent, md->disk); |
371a689f AW |
2085 | md->force_ro.show = force_ro_show; |
2086 | md->force_ro.store = force_ro_store; | |
641c3187 | 2087 | sysfs_attr_init(&md->force_ro.attr); |
371a689f AW |
2088 | md->force_ro.attr.name = "force_ro"; |
2089 | md->force_ro.attr.mode = S_IRUGO | S_IWUSR; | |
2090 | ret = device_create_file(disk_to_dev(md->disk), &md->force_ro); | |
2091 | if (ret) | |
add710ea JR |
2092 | goto force_ro_fail; |
2093 | ||
2094 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && | |
2095 | card->ext_csd.boot_ro_lockable) { | |
88187398 | 2096 | umode_t mode; |
add710ea JR |
2097 | |
2098 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS) | |
2099 | mode = S_IRUGO; | |
2100 | else | |
2101 | mode = S_IRUGO | S_IWUSR; | |
2102 | ||
2103 | md->power_ro_lock.show = power_ro_lock_show; | |
2104 | md->power_ro_lock.store = power_ro_lock_store; | |
00d9ac08 | 2105 | sysfs_attr_init(&md->power_ro_lock.attr); |
add710ea JR |
2106 | md->power_ro_lock.attr.mode = mode; |
2107 | md->power_ro_lock.attr.name = | |
2108 | "ro_lock_until_next_power_on"; | |
2109 | ret = device_create_file(disk_to_dev(md->disk), | |
2110 | &md->power_ro_lock); | |
2111 | if (ret) | |
2112 | goto power_ro_lock_fail; | |
2113 | } | |
2114 | return ret; | |
2115 | ||
2116 | power_ro_lock_fail: | |
2117 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); | |
2118 | force_ro_fail: | |
2119 | del_gendisk(md->disk); | |
371a689f AW |
2120 | |
2121 | return ret; | |
2122 | } | |
2123 | ||
6f60c222 AW |
2124 | static const struct mmc_fixup blk_fixups[] = |
2125 | { | |
c59d4473 CB |
2126 | MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk, |
2127 | MMC_QUIRK_INAND_CMD38), | |
2128 | MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk, | |
2129 | MMC_QUIRK_INAND_CMD38), | |
2130 | MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk, | |
2131 | MMC_QUIRK_INAND_CMD38), | |
2132 | MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk, | |
2133 | MMC_QUIRK_INAND_CMD38), | |
2134 | MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk, | |
2135 | MMC_QUIRK_INAND_CMD38), | |
d0c97cfb AW |
2136 | |
2137 | /* | |
2138 | * Some MMC cards experience performance degradation with CMD23 | |
2139 | * instead of CMD12-bounded multiblock transfers. For now we'll | |
2140 | * black list what's bad... | |
2141 | * - Certain Toshiba cards. | |
2142 | * | |
2143 | * N.B. This doesn't affect SD cards. | |
2144 | */ | |
7d70d476 YL |
2145 | MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc, |
2146 | MMC_QUIRK_BLK_NO_CMD23), | |
2147 | MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc, | |
2148 | MMC_QUIRK_BLK_NO_CMD23), | |
c59d4473 | 2149 | MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
d0c97cfb | 2150 | MMC_QUIRK_BLK_NO_CMD23), |
c59d4473 | 2151 | MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
d0c97cfb | 2152 | MMC_QUIRK_BLK_NO_CMD23), |
c59d4473 | 2153 | MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
d0c97cfb | 2154 | MMC_QUIRK_BLK_NO_CMD23), |
6de5fc9c SNX |
2155 | |
2156 | /* | |
32ecd320 | 2157 | * Some MMC cards need longer data read timeout than indicated in CSD. |
6de5fc9c | 2158 | */ |
c59d4473 | 2159 | MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc, |
6de5fc9c | 2160 | MMC_QUIRK_LONG_READ_TIME), |
32ecd320 MG |
2161 | MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
2162 | MMC_QUIRK_LONG_READ_TIME), | |
6de5fc9c | 2163 | |
3550ccdb IC |
2164 | /* |
2165 | * On these Samsung MoviNAND parts, performing secure erase or | |
2166 | * secure trim can result in unrecoverable corruption due to a | |
2167 | * firmware bug. | |
2168 | */ | |
2169 | MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, | |
2170 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), | |
2171 | MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, | |
2172 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), | |
2173 | MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, | |
2174 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), | |
2175 | MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, | |
2176 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), | |
2177 | MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, | |
2178 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), | |
2179 | MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, | |
2180 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), | |
2181 | MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, | |
2182 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), | |
2183 | MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, | |
2184 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), | |
2185 | ||
b5b4ff0a SL |
2186 | /* |
2187 | * On Some Kingston eMMCs, performing trim can result in | |
2188 | * unrecoverable data conrruption occasionally due to a firmware bug. | |
2189 | */ | |
2190 | MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc, | |
2191 | MMC_QUIRK_TRIM_BROKEN), | |
2192 | MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc, | |
2193 | MMC_QUIRK_TRIM_BROKEN), | |
2194 | ||
6f60c222 AW |
2195 | END_FIXUP |
2196 | }; | |
2197 | ||
96541bac | 2198 | static int mmc_blk_probe(struct mmc_card *card) |
1da177e4 | 2199 | { |
371a689f | 2200 | struct mmc_blk_data *md, *part_md; |
a7bbb573 PO |
2201 | char cap_str[10]; |
2202 | ||
912490db PO |
2203 | /* |
2204 | * Check that the card supports the command class(es) we need. | |
2205 | */ | |
2206 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) | |
1da177e4 LT |
2207 | return -ENODEV; |
2208 | ||
5204d00f LC |
2209 | mmc_fixup_device(card, blk_fixups); |
2210 | ||
1da177e4 LT |
2211 | md = mmc_blk_alloc(card); |
2212 | if (IS_ERR(md)) | |
2213 | return PTR_ERR(md); | |
2214 | ||
b9f28d86 | 2215 | string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2, |
a7bbb573 | 2216 | cap_str, sizeof(cap_str)); |
a3c76eb9 | 2217 | pr_info("%s: %s %s %s %s\n", |
1da177e4 | 2218 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), |
a7bbb573 | 2219 | cap_str, md->read_only ? "(ro)" : ""); |
1da177e4 | 2220 | |
371a689f AW |
2221 | if (mmc_blk_alloc_parts(card, md)) |
2222 | goto out; | |
2223 | ||
96541bac | 2224 | dev_set_drvdata(&card->dev, md); |
6f60c222 | 2225 | |
371a689f AW |
2226 | if (mmc_add_disk(md)) |
2227 | goto out; | |
2228 | ||
2229 | list_for_each_entry(part_md, &md->part, part) { | |
2230 | if (mmc_add_disk(part_md)) | |
2231 | goto out; | |
2232 | } | |
e94cfef6 UH |
2233 | |
2234 | pm_runtime_set_autosuspend_delay(&card->dev, 3000); | |
2235 | pm_runtime_use_autosuspend(&card->dev); | |
2236 | ||
2237 | /* | |
2238 | * Don't enable runtime PM for SD-combo cards here. Leave that | |
2239 | * decision to be taken during the SDIO init sequence instead. | |
2240 | */ | |
2241 | if (card->type != MMC_TYPE_SD_COMBO) { | |
2242 | pm_runtime_set_active(&card->dev); | |
2243 | pm_runtime_enable(&card->dev); | |
2244 | } | |
2245 | ||
1da177e4 LT |
2246 | return 0; |
2247 | ||
2248 | out: | |
371a689f AW |
2249 | mmc_blk_remove_parts(card, md); |
2250 | mmc_blk_remove_req(md); | |
5865f287 | 2251 | return 0; |
1da177e4 LT |
2252 | } |
2253 | ||
96541bac | 2254 | static void mmc_blk_remove(struct mmc_card *card) |
1da177e4 | 2255 | { |
96541bac | 2256 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
1da177e4 | 2257 | |
371a689f | 2258 | mmc_blk_remove_parts(card, md); |
e94cfef6 | 2259 | pm_runtime_get_sync(&card->dev); |
ddd6fa7e AH |
2260 | mmc_claim_host(card->host); |
2261 | mmc_blk_part_switch(card, md); | |
2262 | mmc_release_host(card->host); | |
e94cfef6 UH |
2263 | if (card->type != MMC_TYPE_SD_COMBO) |
2264 | pm_runtime_disable(&card->dev); | |
2265 | pm_runtime_put_noidle(&card->dev); | |
371a689f | 2266 | mmc_blk_remove_req(md); |
96541bac | 2267 | dev_set_drvdata(&card->dev, NULL); |
1da177e4 LT |
2268 | } |
2269 | ||
96541bac | 2270 | static int _mmc_blk_suspend(struct mmc_card *card) |
1da177e4 | 2271 | { |
371a689f | 2272 | struct mmc_blk_data *part_md; |
96541bac | 2273 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
1da177e4 LT |
2274 | |
2275 | if (md) { | |
2276 | mmc_queue_suspend(&md->queue); | |
371a689f AW |
2277 | list_for_each_entry(part_md, &md->part, part) { |
2278 | mmc_queue_suspend(&part_md->queue); | |
2279 | } | |
1da177e4 LT |
2280 | } |
2281 | return 0; | |
2282 | } | |
2283 | ||
96541bac | 2284 | static void mmc_blk_shutdown(struct mmc_card *card) |
76287748 | 2285 | { |
96541bac | 2286 | _mmc_blk_suspend(card); |
76287748 UH |
2287 | } |
2288 | ||
0967edc6 UH |
2289 | #ifdef CONFIG_PM_SLEEP |
2290 | static int mmc_blk_suspend(struct device *dev) | |
76287748 | 2291 | { |
96541bac UH |
2292 | struct mmc_card *card = mmc_dev_to_card(dev); |
2293 | ||
2294 | return _mmc_blk_suspend(card); | |
76287748 UH |
2295 | } |
2296 | ||
0967edc6 | 2297 | static int mmc_blk_resume(struct device *dev) |
1da177e4 | 2298 | { |
371a689f | 2299 | struct mmc_blk_data *part_md; |
fc95e30b | 2300 | struct mmc_blk_data *md = dev_get_drvdata(dev); |
1da177e4 LT |
2301 | |
2302 | if (md) { | |
371a689f AW |
2303 | /* |
2304 | * Resume involves the card going into idle state, | |
2305 | * so current partition is always the main one. | |
2306 | */ | |
2307 | md->part_curr = md->part_type; | |
1da177e4 | 2308 | mmc_queue_resume(&md->queue); |
371a689f AW |
2309 | list_for_each_entry(part_md, &md->part, part) { |
2310 | mmc_queue_resume(&part_md->queue); | |
2311 | } | |
1da177e4 LT |
2312 | } |
2313 | return 0; | |
2314 | } | |
1da177e4 LT |
2315 | #endif |
2316 | ||
0967edc6 UH |
2317 | static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume); |
2318 | ||
96541bac UH |
2319 | static struct mmc_driver mmc_driver = { |
2320 | .drv = { | |
2321 | .name = "mmcblk", | |
2322 | .pm = &mmc_blk_pm_ops, | |
2323 | }, | |
1da177e4 LT |
2324 | .probe = mmc_blk_probe, |
2325 | .remove = mmc_blk_remove, | |
76287748 | 2326 | .shutdown = mmc_blk_shutdown, |
1da177e4 LT |
2327 | }; |
2328 | ||
2329 | static int __init mmc_blk_init(void) | |
2330 | { | |
9d4e98e9 | 2331 | int res; |
1da177e4 | 2332 | |
5e71b7a6 OJ |
2333 | if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) |
2334 | pr_info("mmcblk: using %d minors per device\n", perdev_minors); | |
2335 | ||
a26eba61 | 2336 | max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors); |
5e71b7a6 | 2337 | |
fe6b4c88 PO |
2338 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
2339 | if (res) | |
1da177e4 | 2340 | goto out; |
1da177e4 | 2341 | |
9d4e98e9 AM |
2342 | res = mmc_register_driver(&mmc_driver); |
2343 | if (res) | |
2344 | goto out2; | |
1da177e4 | 2345 | |
9d4e98e9 AM |
2346 | return 0; |
2347 | out2: | |
2348 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); | |
1da177e4 LT |
2349 | out: |
2350 | return res; | |
2351 | } | |
2352 | ||
2353 | static void __exit mmc_blk_exit(void) | |
2354 | { | |
2355 | mmc_unregister_driver(&mmc_driver); | |
fe6b4c88 | 2356 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
1da177e4 LT |
2357 | } |
2358 | ||
2359 | module_init(mmc_blk_init); | |
2360 | module_exit(mmc_blk_exit); | |
2361 | ||
2362 | MODULE_LICENSE("GPL"); | |
2363 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); | |
2364 |