ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / drivers / mmc / card / block.c
blobc0839d48f6c33588c9a53838c601052878c092fe
1 /*
2 * Block driver for media (i.e., flash cards)
4 * Copyright 2002 Hewlett-Packard Company
5 * Copyright 2005-2008 Pierre Ossman
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.
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.
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
17 * Author: Andrew Christian
18 * 28 May 2002
20 #include <linux/moduleparam.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/fs.h>
26 #include <linux/slab.h>
27 #include <linux/errno.h>
28 #include <linux/hdreg.h>
29 #include <linux/kdev_t.h>
30 #include <linux/blkdev.h>
31 #include <linux/mutex.h>
32 #include <linux/scatterlist.h>
33 #include <linux/string_helpers.h>
34 #include <linux/delay.h>
35 #include <linux/capability.h>
36 #include <linux/compat.h>
38 #include <linux/mmc/ioctl.h>
39 #include <linux/mmc/card.h>
40 #include <linux/mmc/host.h>
41 #include <linux/mmc/mmc.h>
42 #include <linux/mmc/sd.h>
44 #include <asm/system.h>
45 #include <asm/uaccess.h>
47 #include "queue.h"
49 MODULE_ALIAS("mmc:block");
50 #ifdef MODULE_PARAM_PREFIX
51 #undef MODULE_PARAM_PREFIX
52 #endif
53 #define MODULE_PARAM_PREFIX "mmcblk."
55 #define INAND_CMD38_ARG_EXT_CSD 113
56 #define INAND_CMD38_ARG_ERASE 0x00
57 #define INAND_CMD38_ARG_TRIM 0x01
58 #define INAND_CMD38_ARG_SECERASE 0x80
59 #define INAND_CMD38_ARG_SECTRIM1 0x81
60 #define INAND_CMD38_ARG_SECTRIM2 0x88
62 static DEFINE_MUTEX(block_mutex);
65 * The defaults come from config options but can be overriden by module
66 * or bootarg options.
68 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
71 * We've only got one major, so number of mmcblk devices is
72 * limited to 256 / number of minors per device.
74 static int max_devices;
76 /* 256 minors, so at most 256 separate devices */
77 static DECLARE_BITMAP(dev_use, 256);
78 static DECLARE_BITMAP(name_use, 256);
81 * There is one mmc_blk_data per slot.
83 struct mmc_blk_data {
84 spinlock_t lock;
85 struct gendisk *disk;
86 struct mmc_queue queue;
87 struct list_head part;
89 unsigned int flags;
90 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
91 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
93 unsigned int usage;
94 unsigned int read_only;
95 unsigned int part_type;
96 unsigned int name_idx;
99 * Only set in main mmc_blk_data associated
100 * with mmc_card with mmc_set_drvdata, and keeps
101 * track of the current selected device partition.
103 unsigned int part_curr;
104 struct device_attribute force_ro;
107 static DEFINE_MUTEX(open_lock);
109 module_param(perdev_minors, int, 0444);
110 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
112 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
114 struct mmc_blk_data *md;
116 mutex_lock(&open_lock);
117 md = disk->private_data;
118 if (md && md->usage == 0)
119 md = NULL;
120 if (md)
121 md->usage++;
122 mutex_unlock(&open_lock);
124 return md;
127 static inline int mmc_get_devidx(struct gendisk *disk)
129 int devmaj = MAJOR(disk_devt(disk));
130 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
132 if (!devmaj)
133 devidx = disk->first_minor / perdev_minors;
134 return devidx;
137 static void mmc_blk_put(struct mmc_blk_data *md)
139 mutex_lock(&open_lock);
140 md->usage--;
141 if (md->usage == 0) {
142 int devidx = mmc_get_devidx(md->disk);
143 blk_cleanup_queue(md->queue.queue);
145 __clear_bit(devidx, dev_use);
147 put_disk(md->disk);
148 kfree(md);
150 mutex_unlock(&open_lock);
153 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
154 char *buf)
156 int ret;
157 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
159 ret = snprintf(buf, PAGE_SIZE, "%d",
160 get_disk_ro(dev_to_disk(dev)) ^
161 md->read_only);
162 mmc_blk_put(md);
163 return ret;
166 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
167 const char *buf, size_t count)
169 int ret;
170 char *end;
171 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
172 unsigned long set = simple_strtoul(buf, &end, 0);
173 if (end == buf) {
174 ret = -EINVAL;
175 goto out;
178 set_disk_ro(dev_to_disk(dev), set || md->read_only);
179 ret = count;
180 out:
181 mmc_blk_put(md);
182 return ret;
185 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
187 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
188 int ret = -ENXIO;
190 mutex_lock(&block_mutex);
191 if (md) {
192 if (md->usage == 2)
193 check_disk_change(bdev);
194 ret = 0;
196 if ((mode & FMODE_WRITE) && md->read_only) {
197 mmc_blk_put(md);
198 ret = -EROFS;
201 mutex_unlock(&block_mutex);
203 return ret;
206 static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
208 struct mmc_blk_data *md = disk->private_data;
210 mutex_lock(&block_mutex);
211 mmc_blk_put(md);
212 mutex_unlock(&block_mutex);
213 return 0;
216 static int
217 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
219 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
220 geo->heads = 4;
221 geo->sectors = 16;
222 return 0;
225 struct mmc_blk_ioc_data {
226 struct mmc_ioc_cmd ic;
227 unsigned char *buf;
228 u64 buf_bytes;
231 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
232 struct mmc_ioc_cmd __user *user)
234 struct mmc_blk_ioc_data *idata;
235 int err;
237 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
238 if (!idata) {
239 err = -ENOMEM;
240 goto out;
243 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
244 err = -EFAULT;
245 goto idata_err;
248 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
249 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
250 err = -EOVERFLOW;
251 goto idata_err;
254 if (!idata->buf_bytes)
255 return idata;
257 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
258 if (!idata->buf) {
259 err = -ENOMEM;
260 goto idata_err;
263 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
264 idata->ic.data_ptr, idata->buf_bytes)) {
265 err = -EFAULT;
266 goto copy_err;
269 return idata;
271 copy_err:
272 kfree(idata->buf);
273 idata_err:
274 kfree(idata);
275 out:
276 return ERR_PTR(err);
279 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
280 struct mmc_ioc_cmd __user *ic_ptr)
282 struct mmc_blk_ioc_data *idata;
283 struct mmc_blk_data *md;
284 struct mmc_card *card;
285 struct mmc_command cmd = {0};
286 struct mmc_data data = {0};
287 struct mmc_request mrq = {0};
288 struct scatterlist sg;
289 int err;
292 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
293 * whole block device, not on a partition. This prevents overspray
294 * between sibling partitions.
296 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
297 return -EPERM;
299 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
300 if (IS_ERR(idata))
301 return PTR_ERR(idata);
303 md = mmc_blk_get(bdev->bd_disk);
304 if (!md) {
305 err = -EINVAL;
306 goto cmd_done;
309 card = md->queue.card;
310 if (IS_ERR(card)) {
311 err = PTR_ERR(card);
312 goto cmd_done;
315 cmd.opcode = idata->ic.opcode;
316 cmd.arg = idata->ic.arg;
317 cmd.flags = idata->ic.flags;
319 if (idata->buf_bytes) {
320 data.sg = &sg;
321 data.sg_len = 1;
322 data.blksz = idata->ic.blksz;
323 data.blocks = idata->ic.blocks;
325 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
327 if (idata->ic.write_flag)
328 data.flags = MMC_DATA_WRITE;
329 else
330 data.flags = MMC_DATA_READ;
332 /* data.flags must already be set before doing this. */
333 mmc_set_data_timeout(&data, card);
335 /* Allow overriding the timeout_ns for empirical tuning. */
336 if (idata->ic.data_timeout_ns)
337 data.timeout_ns = idata->ic.data_timeout_ns;
339 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
341 * Pretend this is a data transfer and rely on the
342 * host driver to compute timeout. When all host
343 * drivers support cmd.cmd_timeout for R1B, this
344 * can be changed to:
346 * mrq.data = NULL;
347 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
349 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
352 mrq.data = &data;
355 mrq.cmd = &cmd;
357 mmc_claim_host(card->host);
359 if (idata->ic.is_acmd) {
360 err = mmc_app_cmd(card->host, card);
361 if (err)
362 goto cmd_rel_host;
365 mmc_wait_for_req(card->host, &mrq);
367 if (cmd.error) {
368 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
369 __func__, cmd.error);
370 err = cmd.error;
371 goto cmd_rel_host;
373 if (data.error) {
374 dev_err(mmc_dev(card->host), "%s: data error %d\n",
375 __func__, data.error);
376 err = data.error;
377 goto cmd_rel_host;
381 * According to the SD specs, some commands require a delay after
382 * issuing the command.
384 if (idata->ic.postsleep_min_us)
385 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
387 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
388 err = -EFAULT;
389 goto cmd_rel_host;
392 if (!idata->ic.write_flag) {
393 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
394 idata->buf, idata->buf_bytes)) {
395 err = -EFAULT;
396 goto cmd_rel_host;
400 cmd_rel_host:
401 mmc_release_host(card->host);
403 cmd_done:
404 mmc_blk_put(md);
405 kfree(idata->buf);
406 kfree(idata);
407 return err;
410 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
411 unsigned int cmd, unsigned long arg)
413 int ret = -EINVAL;
414 if (cmd == MMC_IOC_CMD)
415 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
416 return ret;
419 #ifdef CONFIG_COMPAT
420 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
421 unsigned int cmd, unsigned long arg)
423 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
425 #endif
427 static const struct block_device_operations mmc_bdops = {
428 .open = mmc_blk_open,
429 .release = mmc_blk_release,
430 .getgeo = mmc_blk_getgeo,
431 .owner = THIS_MODULE,
432 .ioctl = mmc_blk_ioctl,
433 #ifdef CONFIG_COMPAT
434 .compat_ioctl = mmc_blk_compat_ioctl,
435 #endif
438 struct mmc_blk_request {
439 struct mmc_request mrq;
440 struct mmc_command sbc;
441 struct mmc_command cmd;
442 struct mmc_command stop;
443 struct mmc_data data;
446 static inline int mmc_blk_part_switch(struct mmc_card *card,
447 struct mmc_blk_data *md)
449 int ret;
450 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
451 if (main_md->part_curr == md->part_type)
452 return 0;
454 if (mmc_card_mmc(card)) {
455 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
456 card->ext_csd.part_config |= md->part_type;
458 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
459 EXT_CSD_PART_CONFIG, card->ext_csd.part_config,
460 card->ext_csd.part_time);
461 if (ret)
462 return ret;
465 main_md->part_curr = md->part_type;
466 return 0;
469 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
471 int err;
472 u32 result;
473 __be32 *blocks;
475 struct mmc_request mrq = {0};
476 struct mmc_command cmd = {0};
477 struct mmc_data data = {0};
478 unsigned int timeout_us;
480 struct scatterlist sg;
482 cmd.opcode = MMC_APP_CMD;
483 cmd.arg = card->rca << 16;
484 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
486 err = mmc_wait_for_cmd(card->host, &cmd, 0);
487 if (err)
488 return (u32)-1;
489 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
490 return (u32)-1;
492 memset(&cmd, 0, sizeof(struct mmc_command));
494 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
495 cmd.arg = 0;
496 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
498 data.timeout_ns = card->csd.tacc_ns * 100;
499 data.timeout_clks = card->csd.tacc_clks * 100;
501 timeout_us = data.timeout_ns / 1000;
502 timeout_us += data.timeout_clks * 1000 /
503 (card->host->ios.clock / 1000);
505 if (timeout_us > 100000) {
506 data.timeout_ns = 100000000;
507 data.timeout_clks = 0;
510 data.blksz = 4;
511 data.blocks = 1;
512 data.flags = MMC_DATA_READ;
513 data.sg = &sg;
514 data.sg_len = 1;
516 mrq.cmd = &cmd;
517 mrq.data = &data;
519 blocks = kmalloc(4, GFP_KERNEL);
520 if (!blocks)
521 return (u32)-1;
523 sg_init_one(&sg, blocks, 4);
525 mmc_wait_for_req(card->host, &mrq);
527 result = ntohl(*blocks);
528 kfree(blocks);
530 if (cmd.error || data.error)
531 result = (u32)-1;
533 return result;
536 static u32 get_card_status(struct mmc_card *card, struct request *req)
538 struct mmc_command cmd = {0};
539 int err;
541 cmd.opcode = MMC_SEND_STATUS;
542 if (!mmc_host_is_spi(card->host))
543 cmd.arg = card->rca << 16;
544 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
545 err = mmc_wait_for_cmd(card->host, &cmd, 0);
546 if (err)
547 printk(KERN_ERR "%s: error %d sending status command",
548 req->rq_disk->disk_name, err);
549 return cmd.resp[0];
552 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
554 struct mmc_blk_data *md = mq->data;
555 struct mmc_card *card = md->queue.card;
556 unsigned int from, nr, arg;
557 int err = 0;
559 if (!mmc_can_erase(card)) {
560 err = -EOPNOTSUPP;
561 goto out;
564 from = blk_rq_pos(req);
565 nr = blk_rq_sectors(req);
567 if (mmc_can_trim(card))
568 arg = MMC_TRIM_ARG;
569 else
570 arg = MMC_ERASE_ARG;
572 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
573 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
574 INAND_CMD38_ARG_EXT_CSD,
575 arg == MMC_TRIM_ARG ?
576 INAND_CMD38_ARG_TRIM :
577 INAND_CMD38_ARG_ERASE,
579 if (err)
580 goto out;
582 err = mmc_erase(card, from, nr, arg);
583 out:
584 spin_lock_irq(&md->lock);
585 __blk_end_request(req, err, blk_rq_bytes(req));
586 spin_unlock_irq(&md->lock);
588 return err ? 0 : 1;
591 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
592 struct request *req)
594 struct mmc_blk_data *md = mq->data;
595 struct mmc_card *card = md->queue.card;
596 unsigned int from, nr, arg;
597 int err = 0;
599 if (!mmc_can_secure_erase_trim(card)) {
600 err = -EOPNOTSUPP;
601 goto out;
604 from = blk_rq_pos(req);
605 nr = blk_rq_sectors(req);
607 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
608 arg = MMC_SECURE_TRIM1_ARG;
609 else
610 arg = MMC_SECURE_ERASE_ARG;
612 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
613 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
614 INAND_CMD38_ARG_EXT_CSD,
615 arg == MMC_SECURE_TRIM1_ARG ?
616 INAND_CMD38_ARG_SECTRIM1 :
617 INAND_CMD38_ARG_SECERASE,
619 if (err)
620 goto out;
622 err = mmc_erase(card, from, nr, arg);
623 if (!err && arg == MMC_SECURE_TRIM1_ARG) {
624 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
625 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
626 INAND_CMD38_ARG_EXT_CSD,
627 INAND_CMD38_ARG_SECTRIM2,
629 if (err)
630 goto out;
632 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
634 out:
635 spin_lock_irq(&md->lock);
636 __blk_end_request(req, err, blk_rq_bytes(req));
637 spin_unlock_irq(&md->lock);
639 return err ? 0 : 1;
642 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
644 struct mmc_blk_data *md = mq->data;
647 * No-op, only service this because we need REQ_FUA for reliable
648 * writes.
650 spin_lock_irq(&md->lock);
651 __blk_end_request_all(req, 0);
652 spin_unlock_irq(&md->lock);
654 return 1;
658 * Reformat current write as a reliable write, supporting
659 * both legacy and the enhanced reliable write MMC cards.
660 * In each transfer we'll handle only as much as a single
661 * reliable write can handle, thus finish the request in
662 * partial completions.
664 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
665 struct mmc_card *card,
666 struct request *req)
668 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
669 /* Legacy mode imposes restrictions on transfers. */
670 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
671 brq->data.blocks = 1;
673 if (brq->data.blocks > card->ext_csd.rel_sectors)
674 brq->data.blocks = card->ext_csd.rel_sectors;
675 else if (brq->data.blocks < card->ext_csd.rel_sectors)
676 brq->data.blocks = 1;
680 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
682 struct mmc_blk_data *md = mq->data;
683 struct mmc_card *card = md->queue.card;
684 struct mmc_blk_request brq;
685 int ret = 1, disable_multi = 0;
688 * Reliable writes are used to implement Forced Unit Access and
689 * REQ_META accesses, and are supported only on MMCs.
691 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
692 (req->cmd_flags & REQ_META)) &&
693 (rq_data_dir(req) == WRITE) &&
694 (md->flags & MMC_BLK_REL_WR);
696 do {
697 struct mmc_command cmd = {0};
698 u32 readcmd, writecmd, status = 0;
700 memset(&brq, 0, sizeof(struct mmc_blk_request));
701 brq.mrq.cmd = &brq.cmd;
702 brq.mrq.data = &brq.data;
704 brq.cmd.arg = blk_rq_pos(req);
705 if (!mmc_card_blockaddr(card))
706 brq.cmd.arg <<= 9;
707 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
708 brq.data.blksz = 512;
709 brq.stop.opcode = MMC_STOP_TRANSMISSION;
710 brq.stop.arg = 0;
711 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
712 brq.data.blocks = blk_rq_sectors(req);
715 * The block layer doesn't support all sector count
716 * restrictions, so we need to be prepared for too big
717 * requests.
719 if (brq.data.blocks > card->host->max_blk_count)
720 brq.data.blocks = card->host->max_blk_count;
723 * After a read error, we redo the request one sector at a time
724 * in order to accurately determine which sectors can be read
725 * successfully.
727 if (disable_multi && brq.data.blocks > 1)
728 brq.data.blocks = 1;
730 if (brq.data.blocks > 1 || do_rel_wr) {
731 /* SPI multiblock writes terminate using a special
732 * token, not a STOP_TRANSMISSION request.
734 if (!mmc_host_is_spi(card->host) ||
735 rq_data_dir(req) == READ)
736 brq.mrq.stop = &brq.stop;
737 readcmd = MMC_READ_MULTIPLE_BLOCK;
738 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
739 } else {
740 brq.mrq.stop = NULL;
741 readcmd = MMC_READ_SINGLE_BLOCK;
742 writecmd = MMC_WRITE_BLOCK;
744 if (rq_data_dir(req) == READ) {
745 brq.cmd.opcode = readcmd;
746 brq.data.flags |= MMC_DATA_READ;
747 } else {
748 brq.cmd.opcode = writecmd;
749 brq.data.flags |= MMC_DATA_WRITE;
752 if (do_rel_wr)
753 mmc_apply_rel_rw(&brq, card, req);
756 * Pre-defined multi-block transfers are preferable to
757 * open ended-ones (and necessary for reliable writes).
758 * However, it is not sufficient to just send CMD23,
759 * and avoid the final CMD12, as on an error condition
760 * CMD12 (stop) needs to be sent anyway. This, coupled
761 * with Auto-CMD23 enhancements provided by some
762 * hosts, means that the complexity of dealing
763 * with this is best left to the host. If CMD23 is
764 * supported by card and host, we'll fill sbc in and let
765 * the host deal with handling it correctly. This means
766 * that for hosts that don't expose MMC_CAP_CMD23, no
767 * change of behavior will be observed.
769 * N.B: Some MMC cards experience perf degradation.
770 * We'll avoid using CMD23-bounded multiblock writes for
771 * these, while retaining features like reliable writes.
774 if ((md->flags & MMC_BLK_CMD23) &&
775 mmc_op_multi(brq.cmd.opcode) &&
776 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
777 brq.sbc.opcode = MMC_SET_BLOCK_COUNT;
778 brq.sbc.arg = brq.data.blocks |
779 (do_rel_wr ? (1 << 31) : 0);
780 brq.sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
781 brq.mrq.sbc = &brq.sbc;
784 mmc_set_data_timeout(&brq.data, card);
786 brq.data.sg = mq->sg;
787 brq.data.sg_len = mmc_queue_map_sg(mq);
790 * Adjust the sg list so it is the same size as the
791 * request.
793 if (brq.data.blocks != blk_rq_sectors(req)) {
794 int i, data_size = brq.data.blocks << 9;
795 struct scatterlist *sg;
797 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
798 data_size -= sg->length;
799 if (data_size <= 0) {
800 sg->length += data_size;
801 i++;
802 break;
805 brq.data.sg_len = i;
808 mmc_queue_bounce_pre(mq);
810 mmc_wait_for_req(card->host, &brq.mrq);
812 mmc_queue_bounce_post(mq);
815 * Check for errors here, but don't jump to cmd_err
816 * until later as we need to wait for the card to leave
817 * programming mode even when things go wrong.
819 if (brq.sbc.error || brq.cmd.error ||
820 brq.data.error || brq.stop.error) {
821 if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
822 /* Redo read one sector at a time */
823 printk(KERN_WARNING "%s: retrying using single "
824 "block read\n", req->rq_disk->disk_name);
825 disable_multi = 1;
826 continue;
828 status = get_card_status(card, req);
831 if (brq.sbc.error) {
832 printk(KERN_ERR "%s: error %d sending SET_BLOCK_COUNT "
833 "command, response %#x, card status %#x\n",
834 req->rq_disk->disk_name, brq.sbc.error,
835 brq.sbc.resp[0], status);
838 if (brq.cmd.error) {
839 printk(KERN_ERR "%s: error %d sending read/write "
840 "command, response %#x, card status %#x\n",
841 req->rq_disk->disk_name, brq.cmd.error,
842 brq.cmd.resp[0], status);
845 if (brq.data.error) {
846 if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
847 /* 'Stop' response contains card status */
848 status = brq.mrq.stop->resp[0];
849 printk(KERN_ERR "%s: error %d transferring data,"
850 " sector %u, nr %u, card status %#x\n",
851 req->rq_disk->disk_name, brq.data.error,
852 (unsigned)blk_rq_pos(req),
853 (unsigned)blk_rq_sectors(req), status);
856 if (brq.stop.error) {
857 printk(KERN_ERR "%s: error %d sending stop command, "
858 "response %#x, card status %#x\n",
859 req->rq_disk->disk_name, brq.stop.error,
860 brq.stop.resp[0], status);
863 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
864 do {
865 int err;
867 cmd.opcode = MMC_SEND_STATUS;
868 cmd.arg = card->rca << 16;
869 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
870 err = mmc_wait_for_cmd(card->host, &cmd, 5);
871 if (err) {
872 printk(KERN_ERR "%s: error %d requesting status\n",
873 req->rq_disk->disk_name, err);
874 goto cmd_err;
877 * Some cards mishandle the status bits,
878 * so make sure to check both the busy
879 * indication and the card state.
881 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
882 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
884 #if 0
885 if (cmd.resp[0] & ~0x00000900)
886 printk(KERN_ERR "%s: status = %08x\n",
887 req->rq_disk->disk_name, cmd.resp[0]);
888 if (mmc_decode_status(cmd.resp))
889 goto cmd_err;
890 #endif
893 if (brq.cmd.error || brq.stop.error || brq.data.error) {
894 if (rq_data_dir(req) == READ) {
896 * After an error, we redo I/O one sector at a
897 * time, so we only reach here after trying to
898 * read a single sector.
900 spin_lock_irq(&md->lock);
901 ret = __blk_end_request(req, -EIO, brq.data.blksz);
902 spin_unlock_irq(&md->lock);
903 continue;
905 goto cmd_err;
909 * A block was successfully transferred.
911 spin_lock_irq(&md->lock);
912 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
913 spin_unlock_irq(&md->lock);
914 } while (ret);
916 return 1;
918 cmd_err:
920 * If this is an SD card and we're writing, we can first
921 * mark the known good sectors as ok.
923 * If the card is not SD, we can still ok written sectors
924 * as reported by the controller (which might be less than
925 * the real number of written sectors, but never more).
927 if (mmc_card_sd(card)) {
928 u32 blocks;
930 blocks = mmc_sd_num_wr_blocks(card);
931 if (blocks != (u32)-1) {
932 spin_lock_irq(&md->lock);
933 ret = __blk_end_request(req, 0, blocks << 9);
934 spin_unlock_irq(&md->lock);
936 } else {
937 spin_lock_irq(&md->lock);
938 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
939 spin_unlock_irq(&md->lock);
942 spin_lock_irq(&md->lock);
943 while (ret)
944 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
945 spin_unlock_irq(&md->lock);
947 return 0;
950 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
952 int ret;
953 struct mmc_blk_data *md = mq->data;
954 struct mmc_card *card = md->queue.card;
956 mmc_claim_host(card->host);
957 ret = mmc_blk_part_switch(card, md);
958 if (ret) {
959 ret = 0;
960 goto out;
963 if (req->cmd_flags & REQ_DISCARD) {
964 if (req->cmd_flags & REQ_SECURE)
965 ret = mmc_blk_issue_secdiscard_rq(mq, req);
966 else
967 ret = mmc_blk_issue_discard_rq(mq, req);
968 } else if (req->cmd_flags & REQ_FLUSH) {
969 ret = mmc_blk_issue_flush(mq, req);
970 } else {
971 ret = mmc_blk_issue_rw_rq(mq, req);
974 out:
975 mmc_release_host(card->host);
976 return ret;
979 static inline int mmc_blk_readonly(struct mmc_card *card)
981 return mmc_card_readonly(card) ||
982 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
985 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
986 struct device *parent,
987 sector_t size,
988 bool default_ro,
989 const char *subname)
991 struct mmc_blk_data *md;
992 int devidx, ret;
994 devidx = find_first_zero_bit(dev_use, max_devices);
995 if (devidx >= max_devices)
996 return ERR_PTR(-ENOSPC);
997 __set_bit(devidx, dev_use);
999 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
1000 if (!md) {
1001 ret = -ENOMEM;
1002 goto out;
1006 * !subname implies we are creating main mmc_blk_data that will be
1007 * associated with mmc_card with mmc_set_drvdata. Due to device
1008 * partitions, devidx will not coincide with a per-physical card
1009 * index anymore so we keep track of a name index.
1011 if (!subname) {
1012 md->name_idx = find_first_zero_bit(name_use, max_devices);
1013 __set_bit(md->name_idx, name_use);
1015 else
1016 md->name_idx = ((struct mmc_blk_data *)
1017 dev_to_disk(parent)->private_data)->name_idx;
1020 * Set the read-only status based on the supported commands
1021 * and the write protect switch.
1023 md->read_only = mmc_blk_readonly(card);
1025 md->disk = alloc_disk(perdev_minors);
1026 if (md->disk == NULL) {
1027 ret = -ENOMEM;
1028 goto err_kfree;
1031 spin_lock_init(&md->lock);
1032 INIT_LIST_HEAD(&md->part);
1033 md->usage = 1;
1035 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
1036 if (ret)
1037 goto err_putdisk;
1039 md->queue.issue_fn = mmc_blk_issue_rq;
1040 md->queue.data = md;
1042 md->disk->major = MMC_BLOCK_MAJOR;
1043 md->disk->first_minor = devidx * perdev_minors;
1044 md->disk->fops = &mmc_bdops;
1045 md->disk->private_data = md;
1046 md->disk->queue = md->queue.queue;
1047 md->disk->driverfs_dev = parent;
1048 set_disk_ro(md->disk, md->read_only || default_ro);
1051 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1053 * - be set for removable media with permanent block devices
1054 * - be unset for removable block devices with permanent media
1056 * Since MMC block devices clearly fall under the second
1057 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1058 * should use the block device creation/destruction hotplug
1059 * messages to tell when the card is present.
1062 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1063 "mmcblk%d%s", md->name_idx, subname ? subname : "");
1065 blk_queue_logical_block_size(md->queue.queue, 512);
1066 set_capacity(md->disk, size);
1068 if (mmc_host_cmd23(card->host)) {
1069 if (mmc_card_mmc(card) ||
1070 (mmc_card_sd(card) &&
1071 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1072 md->flags |= MMC_BLK_CMD23;
1075 if (mmc_card_mmc(card) &&
1076 md->flags & MMC_BLK_CMD23 &&
1077 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1078 card->ext_csd.rel_sectors)) {
1079 md->flags |= MMC_BLK_REL_WR;
1080 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1083 return md;
1085 err_putdisk:
1086 put_disk(md->disk);
1087 err_kfree:
1088 kfree(md);
1089 out:
1090 return ERR_PTR(ret);
1093 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1095 sector_t size;
1096 struct mmc_blk_data *md;
1098 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1100 * The EXT_CSD sector count is in number or 512 byte
1101 * sectors.
1103 size = card->ext_csd.sectors;
1104 } else {
1106 * The CSD capacity field is in units of read_blkbits.
1107 * set_capacity takes units of 512 bytes.
1109 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1112 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
1113 return md;
1116 static int mmc_blk_alloc_part(struct mmc_card *card,
1117 struct mmc_blk_data *md,
1118 unsigned int part_type,
1119 sector_t size,
1120 bool default_ro,
1121 const char *subname)
1123 char cap_str[10];
1124 struct mmc_blk_data *part_md;
1126 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1127 subname);
1128 if (IS_ERR(part_md))
1129 return PTR_ERR(part_md);
1130 part_md->part_type = part_type;
1131 list_add(&part_md->part, &md->part);
1133 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1134 cap_str, sizeof(cap_str));
1135 printk(KERN_INFO "%s: %s %s partition %u %s\n",
1136 part_md->disk->disk_name, mmc_card_id(card),
1137 mmc_card_name(card), part_md->part_type, cap_str);
1138 return 0;
1141 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1143 int ret = 0;
1145 if (!mmc_card_mmc(card))
1146 return 0;
1148 if (card->ext_csd.boot_size) {
1149 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
1150 card->ext_csd.boot_size >> 9,
1151 true,
1152 "boot0");
1153 if (ret)
1154 return ret;
1155 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
1156 card->ext_csd.boot_size >> 9,
1157 true,
1158 "boot1");
1159 if (ret)
1160 return ret;
1163 return ret;
1166 static int
1167 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
1169 int err;
1171 mmc_claim_host(card->host);
1172 err = mmc_set_blocklen(card, 512);
1173 mmc_release_host(card->host);
1175 if (err) {
1176 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
1177 md->disk->disk_name, err);
1178 return -EINVAL;
1181 return 0;
1184 static void mmc_blk_remove_req(struct mmc_blk_data *md)
1186 if (md) {
1187 if (md->disk->flags & GENHD_FL_UP) {
1188 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1190 /* Stop new requests from getting into the queue */
1191 del_gendisk(md->disk);
1194 /* Then flush out any already in there */
1195 mmc_cleanup_queue(&md->queue);
1196 mmc_blk_put(md);
1200 static void mmc_blk_remove_parts(struct mmc_card *card,
1201 struct mmc_blk_data *md)
1203 struct list_head *pos, *q;
1204 struct mmc_blk_data *part_md;
1206 __clear_bit(md->name_idx, name_use);
1207 list_for_each_safe(pos, q, &md->part) {
1208 part_md = list_entry(pos, struct mmc_blk_data, part);
1209 list_del(pos);
1210 mmc_blk_remove_req(part_md);
1214 static int mmc_add_disk(struct mmc_blk_data *md)
1216 int ret;
1218 add_disk(md->disk);
1219 md->force_ro.show = force_ro_show;
1220 md->force_ro.store = force_ro_store;
1221 sysfs_attr_init(&md->force_ro.attr);
1222 md->force_ro.attr.name = "force_ro";
1223 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1224 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1225 if (ret)
1226 del_gendisk(md->disk);
1228 return ret;
1231 static const struct mmc_fixup blk_fixups[] =
1233 MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1234 MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1235 MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1236 MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1237 MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1240 * Some MMC cards experience performance degradation with CMD23
1241 * instead of CMD12-bounded multiblock transfers. For now we'll
1242 * black list what's bad...
1243 * - Certain Toshiba cards.
1245 * N.B. This doesn't affect SD cards.
1247 MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1248 MMC_QUIRK_BLK_NO_CMD23),
1249 MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1250 MMC_QUIRK_BLK_NO_CMD23),
1251 MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1252 MMC_QUIRK_BLK_NO_CMD23),
1253 END_FIXUP
1256 static int mmc_blk_probe(struct mmc_card *card)
1258 struct mmc_blk_data *md, *part_md;
1259 int err;
1260 char cap_str[10];
1263 * Check that the card supports the command class(es) we need.
1265 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
1266 return -ENODEV;
1268 md = mmc_blk_alloc(card);
1269 if (IS_ERR(md))
1270 return PTR_ERR(md);
1272 err = mmc_blk_set_blksize(md, card);
1273 if (err)
1274 goto out;
1276 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
1277 cap_str, sizeof(cap_str));
1278 printk(KERN_INFO "%s: %s %s %s %s\n",
1279 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
1280 cap_str, md->read_only ? "(ro)" : "");
1282 if (mmc_blk_alloc_parts(card, md))
1283 goto out;
1285 mmc_set_drvdata(card, md);
1286 mmc_fixup_device(card, blk_fixups);
1288 if (mmc_add_disk(md))
1289 goto out;
1291 list_for_each_entry(part_md, &md->part, part) {
1292 if (mmc_add_disk(part_md))
1293 goto out;
1295 return 0;
1297 out:
1298 mmc_blk_remove_parts(card, md);
1299 mmc_blk_remove_req(md);
1300 return err;
1303 static void mmc_blk_remove(struct mmc_card *card)
1305 struct mmc_blk_data *md = mmc_get_drvdata(card);
1307 mmc_blk_remove_parts(card, md);
1308 mmc_claim_host(card->host);
1309 mmc_blk_part_switch(card, md);
1310 mmc_release_host(card->host);
1311 mmc_blk_remove_req(md);
1312 mmc_set_drvdata(card, NULL);
1315 #ifdef CONFIG_PM
1316 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
1318 struct mmc_blk_data *part_md;
1319 struct mmc_blk_data *md = mmc_get_drvdata(card);
1321 if (md) {
1322 mmc_queue_suspend(&md->queue);
1323 list_for_each_entry(part_md, &md->part, part) {
1324 mmc_queue_suspend(&part_md->queue);
1327 return 0;
1330 static int mmc_blk_resume(struct mmc_card *card)
1332 struct mmc_blk_data *part_md;
1333 struct mmc_blk_data *md = mmc_get_drvdata(card);
1335 if (md) {
1336 mmc_blk_set_blksize(md, card);
1339 * Resume involves the card going into idle state,
1340 * so current partition is always the main one.
1342 md->part_curr = md->part_type;
1343 mmc_queue_resume(&md->queue);
1344 list_for_each_entry(part_md, &md->part, part) {
1345 mmc_queue_resume(&part_md->queue);
1348 return 0;
1350 #else
1351 #define mmc_blk_suspend NULL
1352 #define mmc_blk_resume NULL
1353 #endif
1355 static struct mmc_driver mmc_driver = {
1356 .drv = {
1357 .name = "mmcblk",
1359 .probe = mmc_blk_probe,
1360 .remove = mmc_blk_remove,
1361 .suspend = mmc_blk_suspend,
1362 .resume = mmc_blk_resume,
1365 static int __init mmc_blk_init(void)
1367 int res;
1369 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1370 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1372 max_devices = 256 / perdev_minors;
1374 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1375 if (res)
1376 goto out;
1378 res = mmc_register_driver(&mmc_driver);
1379 if (res)
1380 goto out2;
1382 return 0;
1383 out2:
1384 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1385 out:
1386 return res;
1389 static void __exit mmc_blk_exit(void)
1391 mmc_unregister_driver(&mmc_driver);
1392 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1395 module_init(mmc_blk_init);
1396 module_exit(mmc_blk_exit);
1398 MODULE_LICENSE("GPL");
1399 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");