Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Block driver for media (i.e., flash cards) |
| 3 | * |
| 4 | * Copyright 2002 Hewlett-Packard Company |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 5 | * Copyright 2005-2008 Pierre Ossman |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/kernel.h> |
| 25 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/errno.h> |
| 28 | #include <linux/hdreg.h> |
| 29 | #include <linux/kdev_t.h> |
| 30 | #include <linux/blkdev.h> |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 31 | #include <linux/mutex.h> |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 32 | #include <linux/scatterlist.h> |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 33 | #include <linux/string_helpers.h> |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 34 | #include <linux/delay.h> |
| 35 | #include <linux/capability.h> |
| 36 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 38 | #include <linux/mmc/ioctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/mmc/card.h> |
Pierre Ossman | 385e322 | 2006-06-18 14:34:37 +0200 | [diff] [blame] | 40 | #include <linux/mmc/host.h> |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 41 | #include <linux/mmc/mmc.h> |
| 42 | #include <linux/mmc/sd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #include <asm/system.h> |
| 45 | #include <asm/uaccess.h> |
| 46 | |
Pierre Ossman | 98ac216 | 2006-12-23 20:03:02 +0100 | [diff] [blame] | 47 | #include "queue.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Andy Whitcroft | 6b0b628 | 2009-02-23 12:38:41 +0000 | [diff] [blame] | 49 | MODULE_ALIAS("mmc:block"); |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 50 | #ifdef MODULE_PARAM_PREFIX |
| 51 | #undef MODULE_PARAM_PREFIX |
| 52 | #endif |
| 53 | #define MODULE_PARAM_PREFIX "mmcblk." |
David Woodhouse | 1dff314 | 2007-11-21 18:45:12 +0100 | [diff] [blame] | 54 | |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 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 |
| 61 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 62 | static DEFINE_MUTEX(block_mutex); |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | * The defaults come from config options but can be overriden by module |
| 66 | * or bootarg options. |
| 67 | */ |
| 68 | static int perdev_minors = CONFIG_MMC_BLOCK_MINORS; |
| 69 | |
| 70 | /* |
| 71 | * We've only got one major, so number of mmcblk devices is |
| 72 | * limited to 256 / number of minors per device. |
| 73 | */ |
| 74 | static int max_devices; |
| 75 | |
| 76 | /* 256 minors, so at most 256 separate devices */ |
| 77 | static DECLARE_BITMAP(dev_use, 256); |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 78 | static DECLARE_BITMAP(name_use, 256); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | /* |
| 81 | * There is one mmc_blk_data per slot. |
| 82 | */ |
| 83 | struct mmc_blk_data { |
| 84 | spinlock_t lock; |
| 85 | struct gendisk *disk; |
| 86 | struct mmc_queue queue; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 87 | struct list_head part; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 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 */ |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | unsigned int usage; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 94 | unsigned int read_only; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 95 | unsigned int part_type; |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 96 | unsigned int name_idx; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 97 | |
| 98 | /* |
| 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. |
| 102 | */ |
| 103 | unsigned int part_curr; |
| 104 | struct device_attribute force_ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 107 | static DEFINE_MUTEX(open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 109 | module_param(perdev_minors, int, 0444); |
| 110 | MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device"); |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk) |
| 113 | { |
| 114 | struct mmc_blk_data *md; |
| 115 | |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 116 | mutex_lock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | md = disk->private_data; |
| 118 | if (md && md->usage == 0) |
| 119 | md = NULL; |
| 120 | if (md) |
| 121 | md->usage++; |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 122 | mutex_unlock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
| 124 | return md; |
| 125 | } |
| 126 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 127 | static inline int mmc_get_devidx(struct gendisk *disk) |
| 128 | { |
Colin Cross | fa746fa | 2010-09-03 12:41:21 -0700 | [diff] [blame] | 129 | int devidx = disk->first_minor / perdev_minors; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 130 | return devidx; |
| 131 | } |
| 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | static void mmc_blk_put(struct mmc_blk_data *md) |
| 134 | { |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 135 | mutex_lock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | md->usage--; |
| 137 | if (md->usage == 0) { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 138 | int devidx = mmc_get_devidx(md->disk); |
Adrian Hunter | 5fa83ce | 2010-01-08 14:43:00 -0800 | [diff] [blame] | 139 | blk_cleanup_queue(md->queue.queue); |
| 140 | |
David Woodhouse | 1dff314 | 2007-11-21 18:45:12 +0100 | [diff] [blame] | 141 | __clear_bit(devidx, dev_use); |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | put_disk(md->disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | kfree(md); |
| 145 | } |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 146 | mutex_unlock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 149 | static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr, |
| 150 | char *buf) |
| 151 | { |
| 152 | int ret; |
| 153 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); |
| 154 | |
| 155 | ret = snprintf(buf, PAGE_SIZE, "%d", |
| 156 | get_disk_ro(dev_to_disk(dev)) ^ |
| 157 | md->read_only); |
| 158 | mmc_blk_put(md); |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr, |
| 163 | const char *buf, size_t count) |
| 164 | { |
| 165 | int ret; |
| 166 | char *end; |
| 167 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); |
| 168 | unsigned long set = simple_strtoul(buf, &end, 0); |
| 169 | if (end == buf) { |
| 170 | ret = -EINVAL; |
| 171 | goto out; |
| 172 | } |
| 173 | |
| 174 | set_disk_ro(dev_to_disk(dev), set || md->read_only); |
| 175 | ret = count; |
| 176 | out: |
| 177 | mmc_blk_put(md); |
| 178 | return ret; |
| 179 | } |
| 180 | |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 181 | static int mmc_blk_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 183 | struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | int ret = -ENXIO; |
| 185 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 186 | mutex_lock(&block_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | if (md) { |
| 188 | if (md->usage == 2) |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 189 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | ret = 0; |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 191 | |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 192 | if ((mode & FMODE_WRITE) && md->read_only) { |
Andrew Morton | 70bb089 | 2008-09-05 14:00:24 -0700 | [diff] [blame] | 193 | mmc_blk_put(md); |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 194 | ret = -EROFS; |
Andrew Morton | 70bb089 | 2008-09-05 14:00:24 -0700 | [diff] [blame] | 195 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 197 | mutex_unlock(&block_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | return ret; |
| 200 | } |
| 201 | |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 202 | static int mmc_blk_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 204 | struct mmc_blk_data *md = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 206 | mutex_lock(&block_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | mmc_blk_put(md); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 208 | mutex_unlock(&block_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 213 | mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 215 | geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16); |
| 216 | geo->heads = 4; |
| 217 | geo->sectors = 16; |
| 218 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 221 | struct mmc_blk_ioc_data { |
| 222 | struct mmc_ioc_cmd ic; |
| 223 | unsigned char *buf; |
| 224 | u64 buf_bytes; |
| 225 | }; |
| 226 | |
| 227 | static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user( |
| 228 | struct mmc_ioc_cmd __user *user) |
| 229 | { |
| 230 | struct mmc_blk_ioc_data *idata; |
| 231 | int err; |
| 232 | |
| 233 | idata = kzalloc(sizeof(*idata), GFP_KERNEL); |
| 234 | if (!idata) { |
| 235 | err = -ENOMEM; |
Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 236 | goto out; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) { |
| 240 | err = -EFAULT; |
Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 241 | goto idata_err; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks; |
| 245 | if (idata->buf_bytes > MMC_IOC_MAX_BYTES) { |
| 246 | err = -EOVERFLOW; |
Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 247 | goto idata_err; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL); |
| 251 | if (!idata->buf) { |
| 252 | err = -ENOMEM; |
Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 253 | goto idata_err; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | if (copy_from_user(idata->buf, (void __user *)(unsigned long) |
| 257 | idata->ic.data_ptr, idata->buf_bytes)) { |
| 258 | err = -EFAULT; |
| 259 | goto copy_err; |
| 260 | } |
| 261 | |
| 262 | return idata; |
| 263 | |
| 264 | copy_err: |
| 265 | kfree(idata->buf); |
Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 266 | idata_err: |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 267 | kfree(idata); |
Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 268 | out: |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 269 | return ERR_PTR(err); |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static int mmc_blk_ioctl_cmd(struct block_device *bdev, |
| 273 | struct mmc_ioc_cmd __user *ic_ptr) |
| 274 | { |
| 275 | struct mmc_blk_ioc_data *idata; |
| 276 | struct mmc_blk_data *md; |
| 277 | struct mmc_card *card; |
| 278 | struct mmc_command cmd = {0}; |
| 279 | struct mmc_data data = {0}; |
| 280 | struct mmc_request mrq = {0}; |
| 281 | struct scatterlist sg; |
| 282 | int err; |
| 283 | |
| 284 | /* |
| 285 | * The caller must have CAP_SYS_RAWIO, and must be calling this on the |
| 286 | * whole block device, not on a partition. This prevents overspray |
| 287 | * between sibling partitions. |
| 288 | */ |
| 289 | if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains)) |
| 290 | return -EPERM; |
| 291 | |
| 292 | idata = mmc_blk_ioctl_copy_from_user(ic_ptr); |
| 293 | if (IS_ERR(idata)) |
| 294 | return PTR_ERR(idata); |
| 295 | |
| 296 | cmd.opcode = idata->ic.opcode; |
| 297 | cmd.arg = idata->ic.arg; |
| 298 | cmd.flags = idata->ic.flags; |
| 299 | |
| 300 | data.sg = &sg; |
| 301 | data.sg_len = 1; |
| 302 | data.blksz = idata->ic.blksz; |
| 303 | data.blocks = idata->ic.blocks; |
| 304 | |
| 305 | sg_init_one(data.sg, idata->buf, idata->buf_bytes); |
| 306 | |
| 307 | if (idata->ic.write_flag) |
| 308 | data.flags = MMC_DATA_WRITE; |
| 309 | else |
| 310 | data.flags = MMC_DATA_READ; |
| 311 | |
| 312 | mrq.cmd = &cmd; |
| 313 | mrq.data = &data; |
| 314 | |
| 315 | md = mmc_blk_get(bdev->bd_disk); |
| 316 | if (!md) { |
| 317 | err = -EINVAL; |
| 318 | goto cmd_done; |
| 319 | } |
| 320 | |
| 321 | card = md->queue.card; |
| 322 | if (IS_ERR(card)) { |
| 323 | err = PTR_ERR(card); |
| 324 | goto cmd_done; |
| 325 | } |
| 326 | |
| 327 | mmc_claim_host(card->host); |
| 328 | |
| 329 | if (idata->ic.is_acmd) { |
| 330 | err = mmc_app_cmd(card->host, card); |
| 331 | if (err) |
| 332 | goto cmd_rel_host; |
| 333 | } |
| 334 | |
| 335 | /* data.flags must already be set before doing this. */ |
| 336 | mmc_set_data_timeout(&data, card); |
| 337 | /* Allow overriding the timeout_ns for empirical tuning. */ |
| 338 | if (idata->ic.data_timeout_ns) |
| 339 | data.timeout_ns = idata->ic.data_timeout_ns; |
| 340 | |
| 341 | if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) { |
| 342 | /* |
| 343 | * Pretend this is a data transfer and rely on the host driver |
| 344 | * to compute timeout. When all host drivers support |
| 345 | * cmd.cmd_timeout for R1B, this can be changed to: |
| 346 | * |
| 347 | * mrq.data = NULL; |
| 348 | * cmd.cmd_timeout = idata->ic.cmd_timeout_ms; |
| 349 | */ |
| 350 | data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000; |
| 351 | } |
| 352 | |
| 353 | mmc_wait_for_req(card->host, &mrq); |
| 354 | |
| 355 | if (cmd.error) { |
| 356 | dev_err(mmc_dev(card->host), "%s: cmd error %d\n", |
| 357 | __func__, cmd.error); |
| 358 | err = cmd.error; |
| 359 | goto cmd_rel_host; |
| 360 | } |
| 361 | if (data.error) { |
| 362 | dev_err(mmc_dev(card->host), "%s: data error %d\n", |
| 363 | __func__, data.error); |
| 364 | err = data.error; |
| 365 | goto cmd_rel_host; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * According to the SD specs, some commands require a delay after |
| 370 | * issuing the command. |
| 371 | */ |
| 372 | if (idata->ic.postsleep_min_us) |
| 373 | usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us); |
| 374 | |
| 375 | if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) { |
| 376 | err = -EFAULT; |
| 377 | goto cmd_rel_host; |
| 378 | } |
| 379 | |
| 380 | if (!idata->ic.write_flag) { |
| 381 | if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr, |
| 382 | idata->buf, idata->buf_bytes)) { |
| 383 | err = -EFAULT; |
| 384 | goto cmd_rel_host; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | cmd_rel_host: |
| 389 | mmc_release_host(card->host); |
| 390 | |
| 391 | cmd_done: |
| 392 | mmc_blk_put(md); |
| 393 | kfree(idata->buf); |
| 394 | kfree(idata); |
| 395 | return err; |
| 396 | } |
| 397 | |
| 398 | static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode, |
| 399 | unsigned int cmd, unsigned long arg) |
| 400 | { |
| 401 | int ret = -EINVAL; |
| 402 | if (cmd == MMC_IOC_CMD) |
| 403 | ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg); |
| 404 | return ret; |
| 405 | } |
| 406 | |
| 407 | #ifdef CONFIG_COMPAT |
| 408 | static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 409 | unsigned int cmd, unsigned long arg) |
| 410 | { |
| 411 | return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg)); |
| 412 | } |
| 413 | #endif |
| 414 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 415 | static const struct block_device_operations mmc_bdops = { |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 416 | .open = mmc_blk_open, |
| 417 | .release = mmc_blk_release, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 418 | .getgeo = mmc_blk_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | .owner = THIS_MODULE, |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 420 | .ioctl = mmc_blk_ioctl, |
| 421 | #ifdef CONFIG_COMPAT |
| 422 | .compat_ioctl = mmc_blk_compat_ioctl, |
| 423 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | }; |
| 425 | |
| 426 | struct mmc_blk_request { |
| 427 | struct mmc_request mrq; |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 428 | struct mmc_command sbc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | struct mmc_command cmd; |
| 430 | struct mmc_command stop; |
| 431 | struct mmc_data data; |
| 432 | }; |
| 433 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 434 | static inline int mmc_blk_part_switch(struct mmc_card *card, |
| 435 | struct mmc_blk_data *md) |
| 436 | { |
| 437 | int ret; |
| 438 | struct mmc_blk_data *main_md = mmc_get_drvdata(card); |
| 439 | if (main_md->part_curr == md->part_type) |
| 440 | return 0; |
| 441 | |
| 442 | if (mmc_card_mmc(card)) { |
| 443 | card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK; |
| 444 | card->ext_csd.part_config |= md->part_type; |
| 445 | |
| 446 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 447 | EXT_CSD_PART_CONFIG, card->ext_csd.part_config, |
| 448 | card->ext_csd.part_time); |
| 449 | if (ret) |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | main_md->part_curr = md->part_type; |
| 454 | return 0; |
| 455 | } |
| 456 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 457 | static u32 mmc_sd_num_wr_blocks(struct mmc_card *card) |
| 458 | { |
| 459 | int err; |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 460 | u32 result; |
| 461 | __be32 *blocks; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 462 | |
Chris Ball | 24f5b53 | 2011-04-13 23:49:45 -0400 | [diff] [blame] | 463 | struct mmc_request mrq = {0}; |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 464 | struct mmc_command cmd = {0}; |
Chris Ball | a61ad2b | 2011-04-13 23:46:05 -0400 | [diff] [blame] | 465 | struct mmc_data data = {0}; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 466 | unsigned int timeout_us; |
| 467 | |
| 468 | struct scatterlist sg; |
| 469 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 470 | cmd.opcode = MMC_APP_CMD; |
| 471 | cmd.arg = card->rca << 16; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 472 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 473 | |
| 474 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 475 | if (err) |
| 476 | return (u32)-1; |
| 477 | if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD)) |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 478 | return (u32)-1; |
| 479 | |
| 480 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 481 | |
| 482 | cmd.opcode = SD_APP_SEND_NUM_WR_BLKS; |
| 483 | cmd.arg = 0; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 484 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 485 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 486 | data.timeout_ns = card->csd.tacc_ns * 100; |
| 487 | data.timeout_clks = card->csd.tacc_clks * 100; |
| 488 | |
| 489 | timeout_us = data.timeout_ns / 1000; |
| 490 | timeout_us += data.timeout_clks * 1000 / |
| 491 | (card->host->ios.clock / 1000); |
| 492 | |
| 493 | if (timeout_us > 100000) { |
| 494 | data.timeout_ns = 100000000; |
| 495 | data.timeout_clks = 0; |
| 496 | } |
| 497 | |
| 498 | data.blksz = 4; |
| 499 | data.blocks = 1; |
| 500 | data.flags = MMC_DATA_READ; |
| 501 | data.sg = &sg; |
| 502 | data.sg_len = 1; |
| 503 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 504 | mrq.cmd = &cmd; |
| 505 | mrq.data = &data; |
| 506 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 507 | blocks = kmalloc(4, GFP_KERNEL); |
| 508 | if (!blocks) |
| 509 | return (u32)-1; |
| 510 | |
| 511 | sg_init_one(&sg, blocks, 4); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 512 | |
| 513 | mmc_wait_for_req(card->host, &mrq); |
| 514 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 515 | result = ntohl(*blocks); |
| 516 | kfree(blocks); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 517 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 518 | if (cmd.error || data.error) |
| 519 | result = (u32)-1; |
| 520 | |
| 521 | return result; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame^] | 524 | static int send_stop(struct mmc_card *card, u32 *status) |
| 525 | { |
| 526 | struct mmc_command cmd = {0}; |
| 527 | int err; |
| 528 | |
| 529 | cmd.opcode = MMC_STOP_TRANSMISSION; |
| 530 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 531 | err = mmc_wait_for_cmd(card->host, &cmd, 5); |
| 532 | if (err == 0) |
| 533 | *status = cmd.resp[0]; |
| 534 | return err; |
| 535 | } |
| 536 | |
Russell King - ARM Linux | 6be918e | 2011-06-20 20:10:08 +0100 | [diff] [blame] | 537 | static int get_card_status(struct mmc_card *card, u32 *status, int retries) |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 538 | { |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 539 | struct mmc_command cmd = {0}; |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 540 | int err; |
| 541 | |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 542 | cmd.opcode = MMC_SEND_STATUS; |
| 543 | if (!mmc_host_is_spi(card->host)) |
| 544 | cmd.arg = card->rca << 16; |
| 545 | cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC; |
Russell King - ARM Linux | 6be918e | 2011-06-20 20:10:08 +0100 | [diff] [blame] | 546 | err = mmc_wait_for_cmd(card->host, &cmd, retries); |
| 547 | if (err == 0) |
| 548 | *status = cmd.resp[0]; |
| 549 | return err; |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 550 | } |
| 551 | |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame^] | 552 | #define ERR_RETRY 2 |
| 553 | #define ERR_ABORT 1 |
| 554 | #define ERR_CONTINUE 0 |
| 555 | |
| 556 | static int mmc_blk_cmd_error(struct request *req, const char *name, int error, |
| 557 | bool status_valid, u32 status) |
| 558 | { |
| 559 | switch (error) { |
| 560 | case -EILSEQ: |
| 561 | /* response crc error, retry the r/w cmd */ |
| 562 | pr_err("%s: %s sending %s command, card status %#x\n", |
| 563 | req->rq_disk->disk_name, "response CRC error", |
| 564 | name, status); |
| 565 | return ERR_RETRY; |
| 566 | |
| 567 | case -ETIMEDOUT: |
| 568 | pr_err("%s: %s sending %s command, card status %#x\n", |
| 569 | req->rq_disk->disk_name, "timed out", name, status); |
| 570 | |
| 571 | /* If the status cmd initially failed, retry the r/w cmd */ |
| 572 | if (!status_valid) |
| 573 | return ERR_RETRY; |
| 574 | |
| 575 | /* |
| 576 | * If it was a r/w cmd crc error, or illegal command |
| 577 | * (eg, issued in wrong state) then retry - we should |
| 578 | * have corrected the state problem above. |
| 579 | */ |
| 580 | if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) |
| 581 | return ERR_RETRY; |
| 582 | |
| 583 | /* Otherwise abort the command */ |
| 584 | return ERR_ABORT; |
| 585 | |
| 586 | default: |
| 587 | /* We don't understand the error code the driver gave us */ |
| 588 | pr_err("%s: unknown error %d sending read/write command, card status %#x\n", |
| 589 | req->rq_disk->disk_name, error, status); |
| 590 | return ERR_ABORT; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | /* |
| 595 | * Initial r/w and stop cmd error recovery. |
| 596 | * We don't know whether the card received the r/w cmd or not, so try to |
| 597 | * restore things back to a sane state. Essentially, we do this as follows: |
| 598 | * - Obtain card status. If the first attempt to obtain card status fails, |
| 599 | * the status word will reflect the failed status cmd, not the failed |
| 600 | * r/w cmd. If we fail to obtain card status, it suggests we can no |
| 601 | * longer communicate with the card. |
| 602 | * - Check the card state. If the card received the cmd but there was a |
| 603 | * transient problem with the response, it might still be in a data transfer |
| 604 | * mode. Try to send it a stop command. If this fails, we can't recover. |
| 605 | * - If the r/w cmd failed due to a response CRC error, it was probably |
| 606 | * transient, so retry the cmd. |
| 607 | * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry. |
| 608 | * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or |
| 609 | * illegal cmd, retry. |
| 610 | * Otherwise we don't understand what happened, so abort. |
| 611 | */ |
| 612 | static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req, |
| 613 | struct mmc_blk_request *brq) |
| 614 | { |
| 615 | bool prev_cmd_status_valid = true; |
| 616 | u32 status, stop_status = 0; |
| 617 | int err, retry; |
| 618 | |
| 619 | /* |
| 620 | * Try to get card status which indicates both the card state |
| 621 | * and why there was no response. If the first attempt fails, |
| 622 | * we can't be sure the returned status is for the r/w command. |
| 623 | */ |
| 624 | for (retry = 2; retry >= 0; retry--) { |
| 625 | err = get_card_status(card, &status, 0); |
| 626 | if (!err) |
| 627 | break; |
| 628 | |
| 629 | prev_cmd_status_valid = false; |
| 630 | pr_err("%s: error %d sending status command, %sing\n", |
| 631 | req->rq_disk->disk_name, err, retry ? "retry" : "abort"); |
| 632 | } |
| 633 | |
| 634 | /* We couldn't get a response from the card. Give up. */ |
| 635 | if (err) |
| 636 | return ERR_ABORT; |
| 637 | |
| 638 | /* |
| 639 | * Check the current card state. If it is in some data transfer |
| 640 | * mode, tell it to stop (and hopefully transition back to TRAN.) |
| 641 | */ |
| 642 | if (R1_CURRENT_STATE(status) == R1_STATE_DATA || |
| 643 | R1_CURRENT_STATE(status) == R1_STATE_RCV) { |
| 644 | err = send_stop(card, &stop_status); |
| 645 | if (err) |
| 646 | pr_err("%s: error %d sending stop command\n", |
| 647 | req->rq_disk->disk_name, err); |
| 648 | |
| 649 | /* |
| 650 | * If the stop cmd also timed out, the card is probably |
| 651 | * not present, so abort. Other errors are bad news too. |
| 652 | */ |
| 653 | if (err) |
| 654 | return ERR_ABORT; |
| 655 | } |
| 656 | |
| 657 | /* Check for set block count errors */ |
| 658 | if (brq->sbc.error) |
| 659 | return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error, |
| 660 | prev_cmd_status_valid, status); |
| 661 | |
| 662 | /* Check for r/w command errors */ |
| 663 | if (brq->cmd.error) |
| 664 | return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error, |
| 665 | prev_cmd_status_valid, status); |
| 666 | |
| 667 | /* Now for stop errors. These aren't fatal to the transfer. */ |
| 668 | pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n", |
| 669 | req->rq_disk->disk_name, brq->stop.error, |
| 670 | brq->cmd.resp[0], status); |
| 671 | |
| 672 | /* |
| 673 | * Subsitute in our own stop status as this will give the error |
| 674 | * state which happened during the execution of the r/w command. |
| 675 | */ |
| 676 | if (stop_status) { |
| 677 | brq->stop.resp[0] = stop_status; |
| 678 | brq->stop.error = 0; |
| 679 | } |
| 680 | return ERR_CONTINUE; |
| 681 | } |
| 682 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 683 | static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req) |
| 684 | { |
| 685 | struct mmc_blk_data *md = mq->data; |
| 686 | struct mmc_card *card = md->queue.card; |
| 687 | unsigned int from, nr, arg; |
| 688 | int err = 0; |
| 689 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 690 | if (!mmc_can_erase(card)) { |
| 691 | err = -EOPNOTSUPP; |
| 692 | goto out; |
| 693 | } |
| 694 | |
| 695 | from = blk_rq_pos(req); |
| 696 | nr = blk_rq_sectors(req); |
| 697 | |
| 698 | if (mmc_can_trim(card)) |
| 699 | arg = MMC_TRIM_ARG; |
| 700 | else |
| 701 | arg = MMC_ERASE_ARG; |
| 702 | |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 703 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 704 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 705 | INAND_CMD38_ARG_EXT_CSD, |
| 706 | arg == MMC_TRIM_ARG ? |
| 707 | INAND_CMD38_ARG_TRIM : |
| 708 | INAND_CMD38_ARG_ERASE, |
| 709 | 0); |
| 710 | if (err) |
| 711 | goto out; |
| 712 | } |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 713 | err = mmc_erase(card, from, nr, arg); |
| 714 | out: |
| 715 | spin_lock_irq(&md->lock); |
| 716 | __blk_end_request(req, err, blk_rq_bytes(req)); |
| 717 | spin_unlock_irq(&md->lock); |
| 718 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 719 | return err ? 0 : 1; |
| 720 | } |
| 721 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 722 | static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, |
| 723 | struct request *req) |
| 724 | { |
| 725 | struct mmc_blk_data *md = mq->data; |
| 726 | struct mmc_card *card = md->queue.card; |
| 727 | unsigned int from, nr, arg; |
| 728 | int err = 0; |
| 729 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 730 | if (!mmc_can_secure_erase_trim(card)) { |
| 731 | err = -EOPNOTSUPP; |
| 732 | goto out; |
| 733 | } |
| 734 | |
| 735 | from = blk_rq_pos(req); |
| 736 | nr = blk_rq_sectors(req); |
| 737 | |
| 738 | if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr)) |
| 739 | arg = MMC_SECURE_TRIM1_ARG; |
| 740 | else |
| 741 | arg = MMC_SECURE_ERASE_ARG; |
| 742 | |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 743 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 744 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 745 | INAND_CMD38_ARG_EXT_CSD, |
| 746 | arg == MMC_SECURE_TRIM1_ARG ? |
| 747 | INAND_CMD38_ARG_SECTRIM1 : |
| 748 | INAND_CMD38_ARG_SECERASE, |
| 749 | 0); |
| 750 | if (err) |
| 751 | goto out; |
| 752 | } |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 753 | err = mmc_erase(card, from, nr, arg); |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 754 | if (!err && arg == MMC_SECURE_TRIM1_ARG) { |
| 755 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 756 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 757 | INAND_CMD38_ARG_EXT_CSD, |
| 758 | INAND_CMD38_ARG_SECTRIM2, |
| 759 | 0); |
| 760 | if (err) |
| 761 | goto out; |
| 762 | } |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 763 | err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG); |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 764 | } |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 765 | out: |
| 766 | spin_lock_irq(&md->lock); |
| 767 | __blk_end_request(req, err, blk_rq_bytes(req)); |
| 768 | spin_unlock_irq(&md->lock); |
| 769 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 770 | return err ? 0 : 1; |
| 771 | } |
| 772 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 773 | static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req) |
| 774 | { |
| 775 | struct mmc_blk_data *md = mq->data; |
| 776 | |
| 777 | /* |
| 778 | * No-op, only service this because we need REQ_FUA for reliable |
| 779 | * writes. |
| 780 | */ |
| 781 | spin_lock_irq(&md->lock); |
| 782 | __blk_end_request_all(req, 0); |
| 783 | spin_unlock_irq(&md->lock); |
| 784 | |
| 785 | return 1; |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * Reformat current write as a reliable write, supporting |
| 790 | * both legacy and the enhanced reliable write MMC cards. |
| 791 | * In each transfer we'll handle only as much as a single |
| 792 | * reliable write can handle, thus finish the request in |
| 793 | * partial completions. |
| 794 | */ |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 795 | static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, |
| 796 | struct mmc_card *card, |
| 797 | struct request *req) |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 798 | { |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 799 | if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) { |
| 800 | /* Legacy mode imposes restrictions on transfers. */ |
| 801 | if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors)) |
| 802 | brq->data.blocks = 1; |
| 803 | |
| 804 | if (brq->data.blocks > card->ext_csd.rel_sectors) |
| 805 | brq->data.blocks = card->ext_csd.rel_sectors; |
| 806 | else if (brq->data.blocks < card->ext_csd.rel_sectors) |
| 807 | brq->data.blocks = 1; |
| 808 | } |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 809 | } |
| 810 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 811 | static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | { |
| 813 | struct mmc_blk_data *md = mq->data; |
| 814 | struct mmc_card *card = md->queue.card; |
Pierre Ossman | 176f00f | 2006-10-04 02:15:41 -0700 | [diff] [blame] | 815 | struct mmc_blk_request brq; |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame^] | 816 | int ret = 1, disable_multi = 0, retry = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 818 | /* |
| 819 | * Reliable writes are used to implement Forced Unit Access and |
| 820 | * REQ_META accesses, and are supported only on MMCs. |
| 821 | */ |
| 822 | bool do_rel_wr = ((req->cmd_flags & REQ_FUA) || |
| 823 | (req->cmd_flags & REQ_META)) && |
| 824 | (rq_data_dir(req) == WRITE) && |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 825 | (md->flags & MMC_BLK_REL_WR); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | do { |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame^] | 828 | u32 readcmd, writecmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
| 830 | memset(&brq, 0, sizeof(struct mmc_blk_request)); |
| 831 | brq.mrq.cmd = &brq.cmd; |
| 832 | brq.mrq.data = &brq.data; |
| 833 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 834 | brq.cmd.arg = blk_rq_pos(req); |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 835 | if (!mmc_card_blockaddr(card)) |
| 836 | brq.cmd.arg <<= 9; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 837 | brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
Pierre Ossman | 0884669 | 2008-08-31 14:10:08 +0200 | [diff] [blame] | 838 | brq.data.blksz = 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | brq.stop.opcode = MMC_STOP_TRANSMISSION; |
| 840 | brq.stop.arg = 0; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 841 | brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 842 | brq.data.blocks = blk_rq_sectors(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 844 | /* |
Pierre Ossman | 548d2de | 2009-04-10 17:52:57 +0200 | [diff] [blame] | 845 | * The block layer doesn't support all sector count |
| 846 | * restrictions, so we need to be prepared for too big |
| 847 | * requests. |
| 848 | */ |
| 849 | if (brq.data.blocks > card->host->max_blk_count) |
| 850 | brq.data.blocks = card->host->max_blk_count; |
| 851 | |
| 852 | /* |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 853 | * After a read error, we redo the request one sector at a time |
| 854 | * in order to accurately determine which sectors can be read |
| 855 | * successfully. |
| 856 | */ |
| 857 | if (disable_multi && brq.data.blocks > 1) |
| 858 | brq.data.blocks = 1; |
| 859 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 860 | if (brq.data.blocks > 1 || do_rel_wr) { |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 861 | /* SPI multiblock writes terminate using a special |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 862 | * token, not a STOP_TRANSMISSION request. |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 863 | */ |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 864 | if (!mmc_host_is_spi(card->host) || |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 865 | rq_data_dir(req) == READ) |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 866 | brq.mrq.stop = &brq.stop; |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 867 | readcmd = MMC_READ_MULTIPLE_BLOCK; |
| 868 | writecmd = MMC_WRITE_MULTIPLE_BLOCK; |
Russell King | 788ee7b | 2006-01-09 21:12:17 +0000 | [diff] [blame] | 869 | } else { |
| 870 | brq.mrq.stop = NULL; |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 871 | readcmd = MMC_READ_SINGLE_BLOCK; |
| 872 | writecmd = MMC_WRITE_BLOCK; |
| 873 | } |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 874 | if (rq_data_dir(req) == READ) { |
| 875 | brq.cmd.opcode = readcmd; |
| 876 | brq.data.flags |= MMC_DATA_READ; |
| 877 | } else { |
| 878 | brq.cmd.opcode = writecmd; |
| 879 | brq.data.flags |= MMC_DATA_WRITE; |
Russell King | 788ee7b | 2006-01-09 21:12:17 +0000 | [diff] [blame] | 880 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 882 | if (do_rel_wr) |
| 883 | mmc_apply_rel_rw(&brq, card, req); |
| 884 | |
| 885 | /* |
| 886 | * Pre-defined multi-block transfers are preferable to |
| 887 | * open ended-ones (and necessary for reliable writes). |
| 888 | * However, it is not sufficient to just send CMD23, |
| 889 | * and avoid the final CMD12, as on an error condition |
| 890 | * CMD12 (stop) needs to be sent anyway. This, coupled |
| 891 | * with Auto-CMD23 enhancements provided by some |
| 892 | * hosts, means that the complexity of dealing |
| 893 | * with this is best left to the host. If CMD23 is |
| 894 | * supported by card and host, we'll fill sbc in and let |
| 895 | * the host deal with handling it correctly. This means |
| 896 | * that for hosts that don't expose MMC_CAP_CMD23, no |
| 897 | * change of behavior will be observed. |
| 898 | * |
| 899 | * N.B: Some MMC cards experience perf degradation. |
| 900 | * We'll avoid using CMD23-bounded multiblock writes for |
| 901 | * these, while retaining features like reliable writes. |
| 902 | */ |
| 903 | |
| 904 | if ((md->flags & MMC_BLK_CMD23) && |
| 905 | mmc_op_multi(brq.cmd.opcode) && |
| 906 | (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) { |
| 907 | brq.sbc.opcode = MMC_SET_BLOCK_COUNT; |
| 908 | brq.sbc.arg = brq.data.blocks | |
| 909 | (do_rel_wr ? (1 << 31) : 0); |
| 910 | brq.sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 911 | brq.mrq.sbc = &brq.sbc; |
| 912 | } |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 913 | |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 914 | mmc_set_data_timeout(&brq.data, card); |
| 915 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | brq.data.sg = mq->sg; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 917 | brq.data.sg_len = mmc_queue_map_sg(mq); |
| 918 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 919 | /* |
| 920 | * Adjust the sg list so it is the same size as the |
| 921 | * request. |
| 922 | */ |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 923 | if (brq.data.blocks != blk_rq_sectors(req)) { |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 924 | int i, data_size = brq.data.blocks << 9; |
| 925 | struct scatterlist *sg; |
| 926 | |
| 927 | for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) { |
| 928 | data_size -= sg->length; |
| 929 | if (data_size <= 0) { |
| 930 | sg->length += data_size; |
| 931 | i++; |
| 932 | break; |
| 933 | } |
| 934 | } |
| 935 | brq.data.sg_len = i; |
| 936 | } |
| 937 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 938 | mmc_queue_bounce_pre(mq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | |
| 940 | mmc_wait_for_req(card->host, &brq.mrq); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 941 | |
| 942 | mmc_queue_bounce_post(mq); |
| 943 | |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 944 | /* |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame^] | 945 | * sbc.error indicates a problem with the set block count |
| 946 | * command. No data will have been transferred. |
| 947 | * |
| 948 | * cmd.error indicates a problem with the r/w command. No |
| 949 | * data will have been transferred. |
| 950 | * |
| 951 | * stop.error indicates a problem with the stop command. Data |
| 952 | * may have been transferred, or may still be transferring. |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 953 | */ |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame^] | 954 | if (brq.sbc.error || brq.cmd.error || brq.stop.error) { |
| 955 | switch (mmc_blk_cmd_recovery(card, req, &brq)) { |
| 956 | case ERR_RETRY: |
| 957 | if (retry++ < 5) |
| 958 | continue; |
| 959 | case ERR_ABORT: |
| 960 | goto cmd_abort; |
| 961 | case ERR_CONTINUE: |
| 962 | break; |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 963 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } |
| 965 | |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 966 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) { |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame^] | 967 | u32 status; |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 968 | do { |
Russell King - ARM Linux | 6be918e | 2011-06-20 20:10:08 +0100 | [diff] [blame] | 969 | int err = get_card_status(card, &status, 5); |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 970 | if (err) { |
| 971 | printk(KERN_ERR "%s: error %d requesting status\n", |
| 972 | req->rq_disk->disk_name, err); |
| 973 | goto cmd_err; |
| 974 | } |
Pierre Ossman | d198f10 | 2007-11-02 18:21:13 +0100 | [diff] [blame] | 975 | /* |
| 976 | * Some cards mishandle the status bits, |
| 977 | * so make sure to check both the busy |
| 978 | * indication and the card state. |
| 979 | */ |
Russell King - ARM Linux | 6be918e | 2011-06-20 20:10:08 +0100 | [diff] [blame] | 980 | } while (!(status & R1_READY_FOR_DATA) || |
| 981 | (R1_CURRENT_STATE(status) == R1_STATE_PRG)); |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 982 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame^] | 984 | if (brq.data.error) { |
| 985 | pr_err("%s: error %d transferring data, sector %u nr %u, cmd response %#x card status %#x\n", |
| 986 | req->rq_disk->disk_name, brq.data.error, |
| 987 | (unsigned)blk_rq_pos(req), |
| 988 | (unsigned)blk_rq_sectors(req), |
| 989 | brq.cmd.resp[0], brq.stop.resp[0]); |
| 990 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 991 | if (rq_data_dir(req) == READ) { |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame^] | 992 | if (brq.data.blocks > 1) { |
| 993 | /* Redo read one sector at a time */ |
| 994 | pr_warning("%s: retrying using single block read\n", |
| 995 | req->rq_disk->disk_name); |
| 996 | disable_multi = 1; |
| 997 | continue; |
| 998 | } |
| 999 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1000 | /* |
| 1001 | * After an error, we redo I/O one sector at a |
| 1002 | * time, so we only reach here after trying to |
| 1003 | * read a single sector. |
| 1004 | */ |
| 1005 | spin_lock_irq(&md->lock); |
| 1006 | ret = __blk_end_request(req, -EIO, brq.data.blksz); |
| 1007 | spin_unlock_irq(&md->lock); |
| 1008 | continue; |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame^] | 1009 | } else { |
| 1010 | goto cmd_err; |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1011 | } |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1012 | } |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 1013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | /* |
| 1015 | * A block was successfully transferred. |
| 1016 | */ |
| 1017 | spin_lock_irq(&md->lock); |
Kiyoshi Ueda | fd53983 | 2007-12-11 17:48:29 -0500 | [diff] [blame] | 1018 | ret = __blk_end_request(req, 0, brq.data.bytes_xfered); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | spin_unlock_irq(&md->lock); |
| 1020 | } while (ret); |
| 1021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | return 1; |
| 1023 | |
| 1024 | cmd_err: |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1025 | /* |
| 1026 | * If this is an SD card and we're writing, we can first |
| 1027 | * mark the known good sectors as ok. |
| 1028 | * |
| 1029 | * If the card is not SD, we can still ok written sectors |
Pierre Ossman | 23af603 | 2008-07-06 01:10:27 +0200 | [diff] [blame] | 1030 | * as reported by the controller (which might be less than |
| 1031 | * the real number of written sectors, but never more). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | */ |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1033 | if (mmc_card_sd(card)) { |
| 1034 | u32 blocks; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1035 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1036 | blocks = mmc_sd_num_wr_blocks(card); |
| 1037 | if (blocks != (u32)-1) { |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1038 | spin_lock_irq(&md->lock); |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1039 | ret = __blk_end_request(req, 0, blocks << 9); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1040 | spin_unlock_irq(&md->lock); |
| 1041 | } |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1042 | } else { |
| 1043 | spin_lock_irq(&md->lock); |
| 1044 | ret = __blk_end_request(req, 0, brq.data.bytes_xfered); |
| 1045 | spin_unlock_irq(&md->lock); |
Pierre Ossman | 176f00f | 2006-10-04 02:15:41 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame^] | 1048 | cmd_abort: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | spin_lock_irq(&md->lock); |
Kiyoshi Ueda | fd53983 | 2007-12-11 17:48:29 -0500 | [diff] [blame] | 1050 | while (ret) |
| 1051 | ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | spin_unlock_irq(&md->lock); |
| 1053 | |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
San Mehat | c87f8d4 | 2009-07-30 08:21:19 -0700 | [diff] [blame] | 1057 | static int |
| 1058 | mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card); |
| 1059 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1060 | static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) |
| 1061 | { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1062 | int ret; |
| 1063 | struct mmc_blk_data *md = mq->data; |
| 1064 | struct mmc_card *card = md->queue.card; |
| 1065 | |
San Mehat | c87f8d4 | 2009-07-30 08:21:19 -0700 | [diff] [blame] | 1066 | #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME |
| 1067 | if (mmc_bus_needs_resume(card->host)) { |
| 1068 | mmc_resume_bus(card->host); |
| 1069 | mmc_blk_set_blksize(md, card); |
| 1070 | } |
| 1071 | #endif |
| 1072 | |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1073 | mmc_claim_host(card->host); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1074 | ret = mmc_blk_part_switch(card, md); |
| 1075 | if (ret) { |
| 1076 | ret = 0; |
| 1077 | goto out; |
| 1078 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1079 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1080 | if (req->cmd_flags & REQ_DISCARD) { |
| 1081 | if (req->cmd_flags & REQ_SECURE) |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1082 | ret = mmc_blk_issue_secdiscard_rq(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1083 | else |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1084 | ret = mmc_blk_issue_discard_rq(mq, req); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1085 | } else if (req->cmd_flags & REQ_FLUSH) { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1086 | ret = mmc_blk_issue_flush(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1087 | } else { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1088 | ret = mmc_blk_issue_rw_rq(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1089 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1090 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1091 | out: |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1092 | mmc_release_host(card->host); |
| 1093 | return ret; |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1094 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1096 | static inline int mmc_blk_readonly(struct mmc_card *card) |
| 1097 | { |
| 1098 | return mmc_card_readonly(card) || |
| 1099 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); |
| 1100 | } |
| 1101 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1102 | static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, |
| 1103 | struct device *parent, |
| 1104 | sector_t size, |
| 1105 | bool default_ro, |
| 1106 | const char *subname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | { |
| 1108 | struct mmc_blk_data *md; |
| 1109 | int devidx, ret; |
| 1110 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 1111 | devidx = find_first_zero_bit(dev_use, max_devices); |
| 1112 | if (devidx >= max_devices) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | return ERR_PTR(-ENOSPC); |
| 1114 | __set_bit(devidx, dev_use); |
| 1115 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 1116 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1117 | if (!md) { |
| 1118 | ret = -ENOMEM; |
| 1119 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | } |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1121 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1122 | /* |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 1123 | * !subname implies we are creating main mmc_blk_data that will be |
| 1124 | * associated with mmc_card with mmc_set_drvdata. Due to device |
| 1125 | * partitions, devidx will not coincide with a per-physical card |
| 1126 | * index anymore so we keep track of a name index. |
| 1127 | */ |
| 1128 | if (!subname) { |
| 1129 | md->name_idx = find_first_zero_bit(name_use, max_devices); |
| 1130 | __set_bit(md->name_idx, name_use); |
| 1131 | } |
| 1132 | else |
| 1133 | md->name_idx = ((struct mmc_blk_data *) |
| 1134 | dev_to_disk(parent)->private_data)->name_idx; |
| 1135 | |
| 1136 | /* |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1137 | * Set the read-only status based on the supported commands |
| 1138 | * and the write protect switch. |
| 1139 | */ |
| 1140 | md->read_only = mmc_blk_readonly(card); |
| 1141 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 1142 | md->disk = alloc_disk(perdev_minors); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1143 | if (md->disk == NULL) { |
| 1144 | ret = -ENOMEM; |
| 1145 | goto err_kfree; |
| 1146 | } |
| 1147 | |
| 1148 | spin_lock_init(&md->lock); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1149 | INIT_LIST_HEAD(&md->part); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1150 | md->usage = 1; |
| 1151 | |
Adrian Hunter | d09408a | 2011-06-23 13:40:28 +0300 | [diff] [blame] | 1152 | ret = mmc_init_queue(&md->queue, card, &md->lock, subname); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1153 | if (ret) |
| 1154 | goto err_putdisk; |
| 1155 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1156 | md->queue.issue_fn = mmc_blk_issue_rq; |
| 1157 | md->queue.data = md; |
| 1158 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 1159 | md->disk->major = MMC_BLOCK_MAJOR; |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 1160 | md->disk->first_minor = devidx * perdev_minors; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1161 | md->disk->fops = &mmc_bdops; |
| 1162 | md->disk->private_data = md; |
| 1163 | md->disk->queue = md->queue.queue; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1164 | md->disk->driverfs_dev = parent; |
| 1165 | set_disk_ro(md->disk, md->read_only || default_ro); |
Colin Cross | fa746fa | 2010-09-03 12:41:21 -0700 | [diff] [blame] | 1166 | md->disk->flags = GENHD_FL_EXT_DEVT; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1167 | |
| 1168 | /* |
| 1169 | * As discussed on lkml, GENHD_FL_REMOVABLE should: |
| 1170 | * |
| 1171 | * - be set for removable media with permanent block devices |
| 1172 | * - be unset for removable block devices with permanent media |
| 1173 | * |
| 1174 | * Since MMC block devices clearly fall under the second |
| 1175 | * case, we do not set GENHD_FL_REMOVABLE. Userspace |
| 1176 | * should use the block device creation/destruction hotplug |
| 1177 | * messages to tell when the card is present. |
| 1178 | */ |
| 1179 | |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 1180 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
| 1181 | "mmcblk%d%s", md->name_idx, subname ? subname : ""); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1182 | |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1183 | blk_queue_logical_block_size(md->queue.queue, 512); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1184 | set_capacity(md->disk, size); |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 1185 | |
Andrei Warkentin | f0d8997 | 2011-05-23 15:06:38 -0500 | [diff] [blame] | 1186 | if (mmc_host_cmd23(card->host)) { |
| 1187 | if (mmc_card_mmc(card) || |
| 1188 | (mmc_card_sd(card) && |
| 1189 | card->scr.cmds & SD_SCR_CMD23_SUPPORT)) |
| 1190 | md->flags |= MMC_BLK_CMD23; |
| 1191 | } |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 1192 | |
| 1193 | if (mmc_card_mmc(card) && |
| 1194 | md->flags & MMC_BLK_CMD23 && |
| 1195 | ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || |
| 1196 | card->ext_csd.rel_sectors)) { |
| 1197 | md->flags |= MMC_BLK_REL_WR; |
| 1198 | blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA); |
| 1199 | } |
| 1200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | return md; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1202 | |
| 1203 | err_putdisk: |
| 1204 | put_disk(md->disk); |
| 1205 | err_kfree: |
| 1206 | kfree(md); |
| 1207 | out: |
| 1208 | return ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | } |
| 1210 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1211 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) |
| 1212 | { |
| 1213 | sector_t size; |
| 1214 | struct mmc_blk_data *md; |
| 1215 | |
| 1216 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { |
| 1217 | /* |
| 1218 | * The EXT_CSD sector count is in number or 512 byte |
| 1219 | * sectors. |
| 1220 | */ |
| 1221 | size = card->ext_csd.sectors; |
| 1222 | } else { |
| 1223 | /* |
| 1224 | * The CSD capacity field is in units of read_blkbits. |
| 1225 | * set_capacity takes units of 512 bytes. |
| 1226 | */ |
| 1227 | size = card->csd.capacity << (card->csd.read_blkbits - 9); |
| 1228 | } |
| 1229 | |
| 1230 | md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL); |
| 1231 | return md; |
| 1232 | } |
| 1233 | |
| 1234 | static int mmc_blk_alloc_part(struct mmc_card *card, |
| 1235 | struct mmc_blk_data *md, |
| 1236 | unsigned int part_type, |
| 1237 | sector_t size, |
| 1238 | bool default_ro, |
| 1239 | const char *subname) |
| 1240 | { |
| 1241 | char cap_str[10]; |
| 1242 | struct mmc_blk_data *part_md; |
| 1243 | |
| 1244 | part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro, |
| 1245 | subname); |
| 1246 | if (IS_ERR(part_md)) |
| 1247 | return PTR_ERR(part_md); |
| 1248 | part_md->part_type = part_type; |
| 1249 | list_add(&part_md->part, &md->part); |
| 1250 | |
| 1251 | string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2, |
| 1252 | cap_str, sizeof(cap_str)); |
| 1253 | printk(KERN_INFO "%s: %s %s partition %u %s\n", |
| 1254 | part_md->disk->disk_name, mmc_card_id(card), |
| 1255 | mmc_card_name(card), part_md->part_type, cap_str); |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
| 1259 | static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) |
| 1260 | { |
| 1261 | int ret = 0; |
| 1262 | |
| 1263 | if (!mmc_card_mmc(card)) |
| 1264 | return 0; |
| 1265 | |
| 1266 | if (card->ext_csd.boot_size) { |
| 1267 | ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0, |
| 1268 | card->ext_csd.boot_size >> 9, |
| 1269 | true, |
| 1270 | "boot0"); |
| 1271 | if (ret) |
| 1272 | return ret; |
| 1273 | ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1, |
| 1274 | card->ext_csd.boot_size >> 9, |
| 1275 | true, |
| 1276 | "boot1"); |
| 1277 | if (ret) |
| 1278 | return ret; |
| 1279 | } |
| 1280 | |
| 1281 | return ret; |
| 1282 | } |
| 1283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | static int |
| 1285 | mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card) |
| 1286 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | int err; |
| 1288 | |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 1289 | mmc_claim_host(card->host); |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 1290 | err = mmc_set_blocklen(card, 512); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 1291 | mmc_release_host(card->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | |
| 1293 | if (err) { |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 1294 | printk(KERN_ERR "%s: unable to set block size to 512: %d\n", |
| 1295 | md->disk->disk_name, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | return -EINVAL; |
| 1297 | } |
| 1298 | |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1302 | static void mmc_blk_remove_req(struct mmc_blk_data *md) |
| 1303 | { |
| 1304 | if (md) { |
| 1305 | if (md->disk->flags & GENHD_FL_UP) { |
| 1306 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); |
| 1307 | |
| 1308 | /* Stop new requests from getting into the queue */ |
| 1309 | del_gendisk(md->disk); |
| 1310 | } |
| 1311 | |
| 1312 | /* Then flush out any already in there */ |
| 1313 | mmc_cleanup_queue(&md->queue); |
| 1314 | mmc_blk_put(md); |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | static void mmc_blk_remove_parts(struct mmc_card *card, |
| 1319 | struct mmc_blk_data *md) |
| 1320 | { |
| 1321 | struct list_head *pos, *q; |
| 1322 | struct mmc_blk_data *part_md; |
| 1323 | |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 1324 | __clear_bit(md->name_idx, name_use); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1325 | list_for_each_safe(pos, q, &md->part) { |
| 1326 | part_md = list_entry(pos, struct mmc_blk_data, part); |
| 1327 | list_del(pos); |
| 1328 | mmc_blk_remove_req(part_md); |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | static int mmc_add_disk(struct mmc_blk_data *md) |
| 1333 | { |
| 1334 | int ret; |
| 1335 | |
| 1336 | add_disk(md->disk); |
| 1337 | md->force_ro.show = force_ro_show; |
| 1338 | md->force_ro.store = force_ro_store; |
Rabin Vincent | 641c318 | 2011-04-23 20:52:58 +0530 | [diff] [blame] | 1339 | sysfs_attr_init(&md->force_ro.attr); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1340 | md->force_ro.attr.name = "force_ro"; |
| 1341 | md->force_ro.attr.mode = S_IRUGO | S_IWUSR; |
| 1342 | ret = device_create_file(disk_to_dev(md->disk), &md->force_ro); |
| 1343 | if (ret) |
| 1344 | del_gendisk(md->disk); |
| 1345 | |
| 1346 | return ret; |
| 1347 | } |
| 1348 | |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 1349 | static const struct mmc_fixup blk_fixups[] = |
| 1350 | { |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1351 | MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
| 1352 | MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
| 1353 | MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
| 1354 | MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
| 1355 | MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 1356 | |
| 1357 | /* |
| 1358 | * Some MMC cards experience performance degradation with CMD23 |
| 1359 | * instead of CMD12-bounded multiblock transfers. For now we'll |
| 1360 | * black list what's bad... |
| 1361 | * - Certain Toshiba cards. |
| 1362 | * |
| 1363 | * N.B. This doesn't affect SD cards. |
| 1364 | */ |
| 1365 | MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc, |
| 1366 | MMC_QUIRK_BLK_NO_CMD23), |
| 1367 | MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc, |
| 1368 | MMC_QUIRK_BLK_NO_CMD23), |
| 1369 | MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc, |
| 1370 | MMC_QUIRK_BLK_NO_CMD23), |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 1371 | END_FIXUP |
| 1372 | }; |
| 1373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | static int mmc_blk_probe(struct mmc_card *card) |
| 1375 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1376 | struct mmc_blk_data *md, *part_md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | int err; |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 1378 | char cap_str[10]; |
| 1379 | |
Pierre Ossman | 912490d | 2005-05-21 10:27:02 +0100 | [diff] [blame] | 1380 | /* |
| 1381 | * Check that the card supports the command class(es) we need. |
| 1382 | */ |
| 1383 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | return -ENODEV; |
| 1385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | md = mmc_blk_alloc(card); |
| 1387 | if (IS_ERR(md)) |
| 1388 | return PTR_ERR(md); |
| 1389 | |
| 1390 | err = mmc_blk_set_blksize(md, card); |
| 1391 | if (err) |
| 1392 | goto out; |
| 1393 | |
Yi Li | 444122f | 2009-02-05 15:31:57 +0800 | [diff] [blame] | 1394 | string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2, |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 1395 | cap_str, sizeof(cap_str)); |
| 1396 | printk(KERN_INFO "%s: %s %s %s %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 1398 | cap_str, md->read_only ? "(ro)" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1400 | if (mmc_blk_alloc_parts(card, md)) |
| 1401 | goto out; |
| 1402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | mmc_set_drvdata(card, md); |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 1404 | mmc_fixup_device(card, blk_fixups); |
| 1405 | |
San Mehat | c87f8d4 | 2009-07-30 08:21:19 -0700 | [diff] [blame] | 1406 | #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME |
| 1407 | mmc_set_bus_resume_policy(card->host, 1); |
| 1408 | #endif |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1409 | if (mmc_add_disk(md)) |
| 1410 | goto out; |
| 1411 | |
| 1412 | list_for_each_entry(part_md, &md->part, part) { |
| 1413 | if (mmc_add_disk(part_md)) |
| 1414 | goto out; |
| 1415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | return 0; |
| 1417 | |
| 1418 | out: |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1419 | mmc_blk_remove_parts(card, md); |
| 1420 | mmc_blk_remove_req(md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | return err; |
| 1422 | } |
| 1423 | |
| 1424 | static void mmc_blk_remove(struct mmc_card *card) |
| 1425 | { |
| 1426 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 1427 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1428 | mmc_blk_remove_parts(card, md); |
Adrian Hunter | ddd6fa7 | 2011-06-23 13:40:26 +0300 | [diff] [blame] | 1429 | mmc_claim_host(card->host); |
| 1430 | mmc_blk_part_switch(card, md); |
| 1431 | mmc_release_host(card->host); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1432 | mmc_blk_remove_req(md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | mmc_set_drvdata(card, NULL); |
San Mehat | c87f8d4 | 2009-07-30 08:21:19 -0700 | [diff] [blame] | 1434 | #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME |
| 1435 | mmc_set_bus_resume_policy(card->host, 0); |
| 1436 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | #ifdef CONFIG_PM |
| 1440 | static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state) |
| 1441 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1442 | struct mmc_blk_data *part_md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 1444 | |
| 1445 | if (md) { |
| 1446 | mmc_queue_suspend(&md->queue); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1447 | list_for_each_entry(part_md, &md->part, part) { |
| 1448 | mmc_queue_suspend(&part_md->queue); |
| 1449 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | } |
| 1451 | return 0; |
| 1452 | } |
| 1453 | |
| 1454 | static int mmc_blk_resume(struct mmc_card *card) |
| 1455 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1456 | struct mmc_blk_data *part_md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 1458 | |
| 1459 | if (md) { |
San Mehat | c87f8d4 | 2009-07-30 08:21:19 -0700 | [diff] [blame] | 1460 | #ifndef CONFIG_MMC_BLOCK_DEFERRED_RESUME |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | mmc_blk_set_blksize(md, card); |
San Mehat | c87f8d4 | 2009-07-30 08:21:19 -0700 | [diff] [blame] | 1462 | #endif |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1463 | |
| 1464 | /* |
| 1465 | * Resume involves the card going into idle state, |
| 1466 | * so current partition is always the main one. |
| 1467 | */ |
| 1468 | md->part_curr = md->part_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | mmc_queue_resume(&md->queue); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1470 | list_for_each_entry(part_md, &md->part, part) { |
| 1471 | mmc_queue_resume(&part_md->queue); |
| 1472 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | } |
| 1474 | return 0; |
| 1475 | } |
| 1476 | #else |
| 1477 | #define mmc_blk_suspend NULL |
| 1478 | #define mmc_blk_resume NULL |
| 1479 | #endif |
| 1480 | |
| 1481 | static struct mmc_driver mmc_driver = { |
| 1482 | .drv = { |
| 1483 | .name = "mmcblk", |
| 1484 | }, |
| 1485 | .probe = mmc_blk_probe, |
| 1486 | .remove = mmc_blk_remove, |
| 1487 | .suspend = mmc_blk_suspend, |
| 1488 | .resume = mmc_blk_resume, |
| 1489 | }; |
| 1490 | |
| 1491 | static int __init mmc_blk_init(void) |
| 1492 | { |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 1493 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 1495 | if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) |
| 1496 | pr_info("mmcblk: using %d minors per device\n", perdev_minors); |
| 1497 | |
| 1498 | max_devices = 256 / perdev_minors; |
| 1499 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 1500 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
| 1501 | if (res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 1504 | res = mmc_register_driver(&mmc_driver); |
| 1505 | if (res) |
| 1506 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 1508 | return 0; |
| 1509 | out2: |
| 1510 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | out: |
| 1512 | return res; |
| 1513 | } |
| 1514 | |
| 1515 | static void __exit mmc_blk_exit(void) |
| 1516 | { |
| 1517 | mmc_unregister_driver(&mmc_driver); |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 1518 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | module_init(mmc_blk_init); |
| 1522 | module_exit(mmc_blk_exit); |
| 1523 | |
| 1524 | MODULE_LICENSE("GPL"); |
| 1525 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); |
| 1526 | |