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