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