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