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