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