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