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 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 426 | static inline int mmc_blk_part_switch(struct mmc_card *card, |
| 427 | struct mmc_blk_data *md) |
| 428 | { |
| 429 | int ret; |
| 430 | struct mmc_blk_data *main_md = mmc_get_drvdata(card); |
| 431 | if (main_md->part_curr == md->part_type) |
| 432 | return 0; |
| 433 | |
| 434 | if (mmc_card_mmc(card)) { |
| 435 | card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK; |
| 436 | card->ext_csd.part_config |= md->part_type; |
| 437 | |
| 438 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 439 | EXT_CSD_PART_CONFIG, card->ext_csd.part_config, |
| 440 | card->ext_csd.part_time); |
| 441 | if (ret) |
| 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | main_md->part_curr = md->part_type; |
| 446 | return 0; |
| 447 | } |
| 448 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 449 | static u32 mmc_sd_num_wr_blocks(struct mmc_card *card) |
| 450 | { |
| 451 | int err; |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 452 | u32 result; |
| 453 | __be32 *blocks; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 454 | |
Chris Ball | 24f5b53 | 2011-04-13 23:49:45 -0400 | [diff] [blame] | 455 | struct mmc_request mrq = {0}; |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 456 | struct mmc_command cmd = {0}; |
Chris Ball | a61ad2b | 2011-04-13 23:46:05 -0400 | [diff] [blame] | 457 | struct mmc_data data = {0}; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 458 | unsigned int timeout_us; |
| 459 | |
| 460 | struct scatterlist sg; |
| 461 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 462 | cmd.opcode = MMC_APP_CMD; |
| 463 | cmd.arg = card->rca << 16; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 464 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 465 | |
| 466 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 467 | if (err) |
| 468 | return (u32)-1; |
| 469 | 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] | 470 | return (u32)-1; |
| 471 | |
| 472 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 473 | |
| 474 | cmd.opcode = SD_APP_SEND_NUM_WR_BLKS; |
| 475 | cmd.arg = 0; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 476 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 477 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 478 | data.timeout_ns = card->csd.tacc_ns * 100; |
| 479 | data.timeout_clks = card->csd.tacc_clks * 100; |
| 480 | |
| 481 | timeout_us = data.timeout_ns / 1000; |
| 482 | timeout_us += data.timeout_clks * 1000 / |
| 483 | (card->host->ios.clock / 1000); |
| 484 | |
| 485 | if (timeout_us > 100000) { |
| 486 | data.timeout_ns = 100000000; |
| 487 | data.timeout_clks = 0; |
| 488 | } |
| 489 | |
| 490 | data.blksz = 4; |
| 491 | data.blocks = 1; |
| 492 | data.flags = MMC_DATA_READ; |
| 493 | data.sg = &sg; |
| 494 | data.sg_len = 1; |
| 495 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 496 | mrq.cmd = &cmd; |
| 497 | mrq.data = &data; |
| 498 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 499 | blocks = kmalloc(4, GFP_KERNEL); |
| 500 | if (!blocks) |
| 501 | return (u32)-1; |
| 502 | |
| 503 | sg_init_one(&sg, blocks, 4); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 504 | |
| 505 | mmc_wait_for_req(card->host, &mrq); |
| 506 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 507 | result = ntohl(*blocks); |
| 508 | kfree(blocks); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 509 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 510 | if (cmd.error || data.error) |
| 511 | result = (u32)-1; |
| 512 | |
| 513 | return result; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 516 | static int send_stop(struct mmc_card *card, u32 *status) |
| 517 | { |
| 518 | struct mmc_command cmd = {0}; |
| 519 | int err; |
| 520 | |
| 521 | cmd.opcode = MMC_STOP_TRANSMISSION; |
| 522 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 523 | err = mmc_wait_for_cmd(card->host, &cmd, 5); |
| 524 | if (err == 0) |
| 525 | *status = cmd.resp[0]; |
| 526 | return err; |
| 527 | } |
| 528 | |
Russell King - ARM Linux | 6be918e | 2011-06-20 20:10:08 +0100 | [diff] [blame] | 529 | 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] | 530 | { |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 531 | struct mmc_command cmd = {0}; |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 532 | int err; |
| 533 | |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 534 | cmd.opcode = MMC_SEND_STATUS; |
| 535 | if (!mmc_host_is_spi(card->host)) |
| 536 | cmd.arg = card->rca << 16; |
| 537 | 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] | 538 | err = mmc_wait_for_cmd(card->host, &cmd, retries); |
| 539 | if (err == 0) |
| 540 | *status = cmd.resp[0]; |
| 541 | return err; |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 542 | } |
| 543 | |
Sujit Reddy Thumma | cfefa14 | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 544 | #define ERR_NOMEDIUM 3 |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 545 | #define ERR_RETRY 2 |
| 546 | #define ERR_ABORT 1 |
| 547 | #define ERR_CONTINUE 0 |
| 548 | |
| 549 | static int mmc_blk_cmd_error(struct request *req, const char *name, int error, |
| 550 | bool status_valid, u32 status) |
| 551 | { |
| 552 | switch (error) { |
| 553 | case -EILSEQ: |
| 554 | /* response crc error, retry the r/w cmd */ |
| 555 | pr_err("%s: %s sending %s command, card status %#x\n", |
| 556 | req->rq_disk->disk_name, "response CRC error", |
| 557 | name, status); |
| 558 | return ERR_RETRY; |
| 559 | |
| 560 | case -ETIMEDOUT: |
| 561 | pr_err("%s: %s sending %s command, card status %#x\n", |
| 562 | req->rq_disk->disk_name, "timed out", name, status); |
| 563 | |
| 564 | /* If the status cmd initially failed, retry the r/w cmd */ |
Ken Sumrall | f410473 | 2011-10-25 18:16:58 -0700 | [diff] [blame] | 565 | if (!status_valid) { |
| 566 | pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name); |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 567 | return ERR_RETRY; |
Ken Sumrall | f410473 | 2011-10-25 18:16:58 -0700 | [diff] [blame] | 568 | } |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 569 | /* |
| 570 | * If it was a r/w cmd crc error, or illegal command |
| 571 | * (eg, issued in wrong state) then retry - we should |
| 572 | * have corrected the state problem above. |
| 573 | */ |
Ken Sumrall | f410473 | 2011-10-25 18:16:58 -0700 | [diff] [blame] | 574 | if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) { |
| 575 | pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name); |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 576 | return ERR_RETRY; |
Ken Sumrall | f410473 | 2011-10-25 18:16:58 -0700 | [diff] [blame] | 577 | } |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 578 | |
| 579 | /* Otherwise abort the command */ |
Ken Sumrall | f410473 | 2011-10-25 18:16:58 -0700 | [diff] [blame] | 580 | pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name); |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 581 | return ERR_ABORT; |
| 582 | |
| 583 | default: |
| 584 | /* We don't understand the error code the driver gave us */ |
| 585 | pr_err("%s: unknown error %d sending read/write command, card status %#x\n", |
| 586 | req->rq_disk->disk_name, error, status); |
| 587 | return ERR_ABORT; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * Initial r/w and stop cmd error recovery. |
| 593 | * We don't know whether the card received the r/w cmd or not, so try to |
| 594 | * restore things back to a sane state. Essentially, we do this as follows: |
| 595 | * - Obtain card status. If the first attempt to obtain card status fails, |
| 596 | * the status word will reflect the failed status cmd, not the failed |
| 597 | * r/w cmd. If we fail to obtain card status, it suggests we can no |
| 598 | * longer communicate with the card. |
| 599 | * - Check the card state. If the card received the cmd but there was a |
| 600 | * transient problem with the response, it might still be in a data transfer |
| 601 | * mode. Try to send it a stop command. If this fails, we can't recover. |
| 602 | * - If the r/w cmd failed due to a response CRC error, it was probably |
| 603 | * transient, so retry the cmd. |
| 604 | * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry. |
| 605 | * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or |
| 606 | * illegal cmd, retry. |
| 607 | * Otherwise we don't understand what happened, so abort. |
| 608 | */ |
| 609 | static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req, |
| 610 | struct mmc_blk_request *brq) |
| 611 | { |
| 612 | bool prev_cmd_status_valid = true; |
| 613 | u32 status, stop_status = 0; |
| 614 | int err, retry; |
| 615 | |
Sujit Reddy Thumma | cfefa14 | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 616 | if (mmc_card_removed(card)) |
| 617 | return ERR_NOMEDIUM; |
| 618 | |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 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. */ |
Sujit Reddy Thumma | cfefa14 | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 635 | if (err) { |
| 636 | /* Check if the card is removed */ |
| 637 | if (mmc_detect_card_removed(card->host)) |
| 638 | return ERR_NOMEDIUM; |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 639 | return ERR_ABORT; |
Sujit Reddy Thumma | cfefa14 | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 640 | } |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 641 | |
| 642 | /* |
| 643 | * Check the current card state. If it is in some data transfer |
| 644 | * mode, tell it to stop (and hopefully transition back to TRAN.) |
| 645 | */ |
| 646 | if (R1_CURRENT_STATE(status) == R1_STATE_DATA || |
| 647 | R1_CURRENT_STATE(status) == R1_STATE_RCV) { |
| 648 | err = send_stop(card, &stop_status); |
| 649 | if (err) |
| 650 | pr_err("%s: error %d sending stop command\n", |
| 651 | req->rq_disk->disk_name, err); |
| 652 | |
| 653 | /* |
| 654 | * If the stop cmd also timed out, the card is probably |
| 655 | * not present, so abort. Other errors are bad news too. |
| 656 | */ |
| 657 | if (err) |
| 658 | return ERR_ABORT; |
| 659 | } |
| 660 | |
| 661 | /* Check for set block count errors */ |
| 662 | if (brq->sbc.error) |
| 663 | return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error, |
| 664 | prev_cmd_status_valid, status); |
| 665 | |
| 666 | /* Check for r/w command errors */ |
| 667 | if (brq->cmd.error) |
| 668 | return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error, |
| 669 | prev_cmd_status_valid, status); |
| 670 | |
| 671 | /* Now for stop errors. These aren't fatal to the transfer. */ |
| 672 | pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n", |
| 673 | req->rq_disk->disk_name, brq->stop.error, |
| 674 | brq->cmd.resp[0], status); |
| 675 | |
| 676 | /* |
| 677 | * Subsitute in our own stop status as this will give the error |
| 678 | * state which happened during the execution of the r/w command. |
| 679 | */ |
| 680 | if (stop_status) { |
| 681 | brq->stop.resp[0] = stop_status; |
| 682 | brq->stop.error = 0; |
| 683 | } |
| 684 | return ERR_CONTINUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 687 | static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req) |
| 688 | { |
| 689 | struct mmc_blk_data *md = mq->data; |
| 690 | struct mmc_card *card = md->queue.card; |
| 691 | unsigned int from, nr, arg; |
| 692 | int err = 0; |
| 693 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 694 | if (!mmc_can_erase(card)) { |
| 695 | err = -EOPNOTSUPP; |
| 696 | goto out; |
| 697 | } |
| 698 | |
| 699 | from = blk_rq_pos(req); |
| 700 | nr = blk_rq_sectors(req); |
| 701 | |
| 702 | if (mmc_can_trim(card)) |
| 703 | arg = MMC_TRIM_ARG; |
| 704 | else |
| 705 | arg = MMC_ERASE_ARG; |
| 706 | |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 707 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 708 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 709 | INAND_CMD38_ARG_EXT_CSD, |
| 710 | arg == MMC_TRIM_ARG ? |
| 711 | INAND_CMD38_ARG_TRIM : |
| 712 | INAND_CMD38_ARG_ERASE, |
| 713 | 0); |
| 714 | if (err) |
| 715 | goto out; |
| 716 | } |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 717 | err = mmc_erase(card, from, nr, arg); |
| 718 | out: |
| 719 | spin_lock_irq(&md->lock); |
| 720 | __blk_end_request(req, err, blk_rq_bytes(req)); |
| 721 | spin_unlock_irq(&md->lock); |
| 722 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 723 | return err ? 0 : 1; |
| 724 | } |
| 725 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 726 | static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, |
| 727 | struct request *req) |
| 728 | { |
| 729 | struct mmc_blk_data *md = mq->data; |
| 730 | struct mmc_card *card = md->queue.card; |
| 731 | unsigned int from, nr, arg; |
| 732 | int err = 0; |
| 733 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 734 | if (!mmc_can_secure_erase_trim(card)) { |
| 735 | err = -EOPNOTSUPP; |
| 736 | goto out; |
| 737 | } |
| 738 | |
| 739 | from = blk_rq_pos(req); |
| 740 | nr = blk_rq_sectors(req); |
| 741 | |
| 742 | if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr)) |
| 743 | arg = MMC_SECURE_TRIM1_ARG; |
| 744 | else |
| 745 | arg = MMC_SECURE_ERASE_ARG; |
| 746 | |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 747 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 748 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 749 | INAND_CMD38_ARG_EXT_CSD, |
| 750 | arg == MMC_SECURE_TRIM1_ARG ? |
| 751 | INAND_CMD38_ARG_SECTRIM1 : |
| 752 | INAND_CMD38_ARG_SECERASE, |
| 753 | 0); |
| 754 | if (err) |
| 755 | goto out; |
| 756 | } |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 757 | err = mmc_erase(card, from, nr, arg); |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 758 | if (!err && arg == MMC_SECURE_TRIM1_ARG) { |
| 759 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 760 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 761 | INAND_CMD38_ARG_EXT_CSD, |
| 762 | INAND_CMD38_ARG_SECTRIM2, |
| 763 | 0); |
| 764 | if (err) |
| 765 | goto out; |
| 766 | } |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 767 | err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG); |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 768 | } |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 769 | out: |
| 770 | spin_lock_irq(&md->lock); |
| 771 | __blk_end_request(req, err, blk_rq_bytes(req)); |
| 772 | spin_unlock_irq(&md->lock); |
| 773 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 774 | return err ? 0 : 1; |
| 775 | } |
| 776 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 777 | static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req) |
| 778 | { |
| 779 | struct mmc_blk_data *md = mq->data; |
| 780 | |
| 781 | /* |
| 782 | * No-op, only service this because we need REQ_FUA for reliable |
| 783 | * writes. |
| 784 | */ |
| 785 | spin_lock_irq(&md->lock); |
| 786 | __blk_end_request_all(req, 0); |
| 787 | spin_unlock_irq(&md->lock); |
| 788 | |
| 789 | return 1; |
| 790 | } |
| 791 | |
| 792 | /* |
| 793 | * Reformat current write as a reliable write, supporting |
| 794 | * both legacy and the enhanced reliable write MMC cards. |
| 795 | * In each transfer we'll handle only as much as a single |
| 796 | * reliable write can handle, thus finish the request in |
| 797 | * partial completions. |
| 798 | */ |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 799 | static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, |
| 800 | struct mmc_card *card, |
| 801 | struct request *req) |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 802 | { |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 803 | if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) { |
| 804 | /* Legacy mode imposes restrictions on transfers. */ |
| 805 | if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors)) |
| 806 | brq->data.blocks = 1; |
| 807 | |
| 808 | if (brq->data.blocks > card->ext_csd.rel_sectors) |
| 809 | brq->data.blocks = card->ext_csd.rel_sectors; |
| 810 | else if (brq->data.blocks < card->ext_csd.rel_sectors) |
| 811 | brq->data.blocks = 1; |
| 812 | } |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 813 | } |
| 814 | |
Russell King - ARM Linux | 2080390 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 815 | #define CMD_ERRORS \ |
| 816 | (R1_OUT_OF_RANGE | /* Command argument out of range */ \ |
| 817 | R1_ADDRESS_ERROR | /* Misaligned address */ \ |
| 818 | R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\ |
| 819 | R1_WP_VIOLATION | /* Tried to write to protected block */ \ |
| 820 | R1_CC_ERROR | /* Card controller error */ \ |
| 821 | R1_ERROR) /* General/unknown error */ |
| 822 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 823 | 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] | 824 | { |
| 825 | struct mmc_blk_data *md = mq->data; |
| 826 | struct mmc_card *card = md->queue.card; |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 827 | struct mmc_blk_request *brq = &mq->mqrq_cur->brq; |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 828 | int ret = 1, disable_multi = 0, retry = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 830 | /* |
| 831 | * Reliable writes are used to implement Forced Unit Access and |
| 832 | * REQ_META accesses, and are supported only on MMCs. |
| 833 | */ |
| 834 | bool do_rel_wr = ((req->cmd_flags & REQ_FUA) || |
| 835 | (req->cmd_flags & REQ_META)) && |
| 836 | (rq_data_dir(req) == WRITE) && |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 837 | (md->flags & MMC_BLK_REL_WR); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | do { |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 840 | u32 readcmd, writecmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 842 | memset(brq, 0, sizeof(struct mmc_blk_request)); |
| 843 | brq->mrq.cmd = &brq->cmd; |
| 844 | brq->mrq.data = &brq->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 846 | brq->cmd.arg = blk_rq_pos(req); |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 847 | if (!mmc_card_blockaddr(card)) |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 848 | brq->cmd.arg <<= 9; |
| 849 | brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
| 850 | brq->data.blksz = 512; |
| 851 | brq->stop.opcode = MMC_STOP_TRANSMISSION; |
| 852 | brq->stop.arg = 0; |
| 853 | brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 854 | brq->data.blocks = blk_rq_sectors(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 856 | /* |
Pierre Ossman | 548d2de | 2009-04-10 17:52:57 +0200 | [diff] [blame] | 857 | * The block layer doesn't support all sector count |
| 858 | * restrictions, so we need to be prepared for too big |
| 859 | * requests. |
| 860 | */ |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 861 | if (brq->data.blocks > card->host->max_blk_count) |
| 862 | brq->data.blocks = card->host->max_blk_count; |
Pierre Ossman | 548d2de | 2009-04-10 17:52:57 +0200 | [diff] [blame] | 863 | |
| 864 | /* |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 865 | * After a read error, we redo the request one sector at a time |
| 866 | * in order to accurately determine which sectors can be read |
| 867 | * successfully. |
| 868 | */ |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 869 | if (disable_multi && brq->data.blocks > 1) |
| 870 | brq->data.blocks = 1; |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 871 | |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 872 | if (brq->data.blocks > 1 || do_rel_wr) { |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 873 | /* SPI multiblock writes terminate using a special |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 874 | * token, not a STOP_TRANSMISSION request. |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 875 | */ |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 876 | if (!mmc_host_is_spi(card->host) || |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 877 | rq_data_dir(req) == READ) |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 878 | brq->mrq.stop = &brq->stop; |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 879 | readcmd = MMC_READ_MULTIPLE_BLOCK; |
| 880 | writecmd = MMC_WRITE_MULTIPLE_BLOCK; |
Russell King | 788ee7b | 2006-01-09 21:12:17 +0000 | [diff] [blame] | 881 | } else { |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 882 | brq->mrq.stop = NULL; |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 883 | readcmd = MMC_READ_SINGLE_BLOCK; |
| 884 | writecmd = MMC_WRITE_BLOCK; |
| 885 | } |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 886 | if (rq_data_dir(req) == READ) { |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 887 | brq->cmd.opcode = readcmd; |
| 888 | brq->data.flags |= MMC_DATA_READ; |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 889 | } else { |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 890 | brq->cmd.opcode = writecmd; |
| 891 | brq->data.flags |= MMC_DATA_WRITE; |
Russell King | 788ee7b | 2006-01-09 21:12:17 +0000 | [diff] [blame] | 892 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 894 | if (do_rel_wr) |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 895 | mmc_apply_rel_rw(brq, card, req); |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 896 | |
| 897 | /* |
| 898 | * Pre-defined multi-block transfers are preferable to |
| 899 | * open ended-ones (and necessary for reliable writes). |
| 900 | * However, it is not sufficient to just send CMD23, |
| 901 | * and avoid the final CMD12, as on an error condition |
| 902 | * CMD12 (stop) needs to be sent anyway. This, coupled |
| 903 | * with Auto-CMD23 enhancements provided by some |
| 904 | * hosts, means that the complexity of dealing |
| 905 | * with this is best left to the host. If CMD23 is |
| 906 | * supported by card and host, we'll fill sbc in and let |
| 907 | * the host deal with handling it correctly. This means |
| 908 | * that for hosts that don't expose MMC_CAP_CMD23, no |
| 909 | * change of behavior will be observed. |
| 910 | * |
| 911 | * N.B: Some MMC cards experience perf degradation. |
| 912 | * We'll avoid using CMD23-bounded multiblock writes for |
| 913 | * these, while retaining features like reliable writes. |
| 914 | */ |
| 915 | |
| 916 | if ((md->flags & MMC_BLK_CMD23) && |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 917 | mmc_op_multi(brq->cmd.opcode) && |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 918 | (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) { |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 919 | brq->sbc.opcode = MMC_SET_BLOCK_COUNT; |
| 920 | brq->sbc.arg = brq->data.blocks | |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 921 | (do_rel_wr ? (1 << 31) : 0); |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 922 | brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 923 | brq->mrq.sbc = &brq->sbc; |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 924 | } |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 925 | |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 926 | mmc_set_data_timeout(&brq->data, card); |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 927 | |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 928 | brq->data.sg = mq->mqrq_cur->sg; |
| 929 | brq->data.sg_len = mmc_queue_map_sg(mq, mq->mqrq_cur); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 930 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 931 | /* |
| 932 | * Adjust the sg list so it is the same size as the |
| 933 | * request. |
| 934 | */ |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 935 | if (brq->data.blocks != blk_rq_sectors(req)) { |
| 936 | int i, data_size = brq->data.blocks << 9; |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 937 | struct scatterlist *sg; |
| 938 | |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 939 | for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) { |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 940 | data_size -= sg->length; |
| 941 | if (data_size <= 0) { |
| 942 | sg->length += data_size; |
| 943 | i++; |
| 944 | break; |
| 945 | } |
| 946 | } |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 947 | brq->data.sg_len = i; |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 948 | } |
| 949 | |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 950 | mmc_queue_bounce_pre(mq->mqrq_cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 952 | mmc_wait_for_req(card->host, &brq->mrq); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 953 | |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 954 | mmc_queue_bounce_post(mq->mqrq_cur); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 955 | |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 956 | /* |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 957 | * sbc.error indicates a problem with the set block count |
| 958 | * command. No data will have been transferred. |
| 959 | * |
| 960 | * cmd.error indicates a problem with the r/w command. No |
| 961 | * data will have been transferred. |
| 962 | * |
| 963 | * stop.error indicates a problem with the stop command. Data |
| 964 | * may have been transferred, or may still be transferring. |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 965 | */ |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 966 | if (brq->sbc.error || brq->cmd.error || brq->stop.error) { |
| 967 | switch (mmc_blk_cmd_recovery(card, req, brq)) { |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 968 | case ERR_RETRY: |
| 969 | if (retry++ < 5) |
| 970 | continue; |
| 971 | case ERR_ABORT: |
Sujit Reddy Thumma | cfefa14 | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 972 | case ERR_NOMEDIUM: |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 973 | goto cmd_abort; |
| 974 | case ERR_CONTINUE: |
| 975 | break; |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 976 | } |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 977 | } |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 978 | |
Russell King - ARM Linux | 2080390 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 979 | /* |
| 980 | * Check for errors relating to the execution of the |
| 981 | * initial command - such as address errors. No data |
| 982 | * has been transferred. |
| 983 | */ |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 984 | if (brq->cmd.resp[0] & CMD_ERRORS) { |
Russell King - ARM Linux | 2080390 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 985 | pr_err("%s: r/w command failed, status = %#x\n", |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 986 | req->rq_disk->disk_name, brq->cmd.resp[0]); |
Russell King - ARM Linux | 2080390 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 987 | goto cmd_abort; |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 988 | } |
| 989 | |
Russell King - ARM Linux | 2080390 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 990 | /* |
| 991 | * Everything else is either success, or a data error of some |
| 992 | * kind. If it was a write, we may have transitioned to |
| 993 | * program mode, which we have to wait for it to complete. |
| 994 | */ |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 995 | 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] | 996 | u32 status; |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 997 | do { |
Russell King - ARM Linux | 6be918e | 2011-06-20 20:10:08 +0100 | [diff] [blame] | 998 | int err = get_card_status(card, &status, 5); |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 999 | if (err) { |
| 1000 | printk(KERN_ERR "%s: error %d requesting status\n", |
| 1001 | req->rq_disk->disk_name, err); |
| 1002 | goto cmd_err; |
| 1003 | } |
Pierre Ossman | d198f10 | 2007-11-02 18:21:13 +0100 | [diff] [blame] | 1004 | /* |
| 1005 | * Some cards mishandle the status bits, |
| 1006 | * so make sure to check both the busy |
| 1007 | * indication and the card state. |
| 1008 | */ |
Russell King - ARM Linux | 6be918e | 2011-06-20 20:10:08 +0100 | [diff] [blame] | 1009 | } while (!(status & R1_READY_FOR_DATA) || |
| 1010 | (R1_CURRENT_STATE(status) == R1_STATE_PRG)); |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 1011 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 1013 | if (brq->data.error) { |
Russell King - ARM Linux | 2080390 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 1014 | pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n", |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 1015 | req->rq_disk->disk_name, brq->data.error, |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1016 | (unsigned)blk_rq_pos(req), |
| 1017 | (unsigned)blk_rq_sectors(req), |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 1018 | brq->cmd.resp[0], brq->stop.resp[0]); |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1019 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1020 | if (rq_data_dir(req) == READ) { |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 1021 | if (brq->data.blocks > 1) { |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1022 | /* Redo read one sector at a time */ |
| 1023 | pr_warning("%s: retrying using single block read\n", |
| 1024 | req->rq_disk->disk_name); |
| 1025 | disable_multi = 1; |
| 1026 | continue; |
| 1027 | } |
| 1028 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1029 | /* |
| 1030 | * After an error, we redo I/O one sector at a |
| 1031 | * time, so we only reach here after trying to |
| 1032 | * read a single sector. |
| 1033 | */ |
| 1034 | spin_lock_irq(&md->lock); |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 1035 | ret = __blk_end_request(req, -EIO, |
| 1036 | brq->data.blksz); |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1037 | spin_unlock_irq(&md->lock); |
| 1038 | continue; |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1039 | } else { |
| 1040 | goto cmd_err; |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1041 | } |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1042 | } |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 1043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | /* |
| 1045 | * A block was successfully transferred. |
| 1046 | */ |
| 1047 | spin_lock_irq(&md->lock); |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 1048 | ret = __blk_end_request(req, 0, brq->data.bytes_xfered); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | spin_unlock_irq(&md->lock); |
| 1050 | } while (ret); |
| 1051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | return 1; |
| 1053 | |
| 1054 | cmd_err: |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1055 | /* |
| 1056 | * If this is an SD card and we're writing, we can first |
| 1057 | * mark the known good sectors as ok. |
| 1058 | * |
| 1059 | * If the card is not SD, we can still ok written sectors |
Pierre Ossman | 23af603 | 2008-07-06 01:10:27 +0200 | [diff] [blame] | 1060 | * as reported by the controller (which might be less than |
| 1061 | * the real number of written sectors, but never more). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | */ |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1063 | if (mmc_card_sd(card)) { |
| 1064 | u32 blocks; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1065 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1066 | blocks = mmc_sd_num_wr_blocks(card); |
| 1067 | if (blocks != (u32)-1) { |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1068 | spin_lock_irq(&md->lock); |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1069 | ret = __blk_end_request(req, 0, blocks << 9); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1070 | spin_unlock_irq(&md->lock); |
| 1071 | } |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1072 | } else { |
| 1073 | spin_lock_irq(&md->lock); |
Per Forlin | cb86e7b | 2011-07-09 17:12:36 -0400 | [diff] [blame^] | 1074 | ret = __blk_end_request(req, 0, brq->data.bytes_xfered); |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1075 | spin_unlock_irq(&md->lock); |
Pierre Ossman | 176f00f | 2006-10-04 02:15:41 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Russell King - ARM Linux | 91d56b5 | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1078 | cmd_abort: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | spin_lock_irq(&md->lock); |
Sujit Reddy Thumma | cfefa14 | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1080 | if (mmc_card_removed(card)) |
| 1081 | req->cmd_flags |= REQ_QUIET; |
Kiyoshi Ueda | fd53983 | 2007-12-11 17:48:29 -0500 | [diff] [blame] | 1082 | while (ret) |
| 1083 | ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | spin_unlock_irq(&md->lock); |
| 1085 | |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
San Mehat | c87f8d4 | 2009-07-30 08:21:19 -0700 | [diff] [blame] | 1089 | static int |
| 1090 | mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card); |
| 1091 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1092 | static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) |
| 1093 | { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1094 | int ret; |
| 1095 | struct mmc_blk_data *md = mq->data; |
| 1096 | struct mmc_card *card = md->queue.card; |
| 1097 | |
San Mehat | c87f8d4 | 2009-07-30 08:21:19 -0700 | [diff] [blame] | 1098 | #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME |
| 1099 | if (mmc_bus_needs_resume(card->host)) { |
| 1100 | mmc_resume_bus(card->host); |
| 1101 | mmc_blk_set_blksize(md, card); |
| 1102 | } |
| 1103 | #endif |
| 1104 | |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1105 | mmc_claim_host(card->host); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1106 | ret = mmc_blk_part_switch(card, md); |
| 1107 | if (ret) { |
| 1108 | ret = 0; |
| 1109 | goto out; |
| 1110 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1111 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1112 | if (req->cmd_flags & REQ_DISCARD) { |
| 1113 | if (req->cmd_flags & REQ_SECURE) |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1114 | ret = mmc_blk_issue_secdiscard_rq(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1115 | else |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1116 | ret = mmc_blk_issue_discard_rq(mq, req); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1117 | } else if (req->cmd_flags & REQ_FLUSH) { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1118 | ret = mmc_blk_issue_flush(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1119 | } else { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1120 | ret = mmc_blk_issue_rw_rq(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1121 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1122 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1123 | out: |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1124 | mmc_release_host(card->host); |
| 1125 | return ret; |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1126 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1128 | static inline int mmc_blk_readonly(struct mmc_card *card) |
| 1129 | { |
| 1130 | return mmc_card_readonly(card) || |
| 1131 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); |
| 1132 | } |
| 1133 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1134 | static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, |
| 1135 | struct device *parent, |
| 1136 | sector_t size, |
| 1137 | bool default_ro, |
| 1138 | const char *subname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | { |
| 1140 | struct mmc_blk_data *md; |
| 1141 | int devidx, ret; |
| 1142 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 1143 | devidx = find_first_zero_bit(dev_use, max_devices); |
| 1144 | if (devidx >= max_devices) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | return ERR_PTR(-ENOSPC); |
| 1146 | __set_bit(devidx, dev_use); |
| 1147 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 1148 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1149 | if (!md) { |
| 1150 | ret = -ENOMEM; |
| 1151 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | } |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1153 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1154 | /* |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 1155 | * !subname implies we are creating main mmc_blk_data that will be |
| 1156 | * associated with mmc_card with mmc_set_drvdata. Due to device |
| 1157 | * partitions, devidx will not coincide with a per-physical card |
| 1158 | * index anymore so we keep track of a name index. |
| 1159 | */ |
| 1160 | if (!subname) { |
| 1161 | md->name_idx = find_first_zero_bit(name_use, max_devices); |
| 1162 | __set_bit(md->name_idx, name_use); |
| 1163 | } |
| 1164 | else |
| 1165 | md->name_idx = ((struct mmc_blk_data *) |
| 1166 | dev_to_disk(parent)->private_data)->name_idx; |
| 1167 | |
| 1168 | /* |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1169 | * Set the read-only status based on the supported commands |
| 1170 | * and the write protect switch. |
| 1171 | */ |
| 1172 | md->read_only = mmc_blk_readonly(card); |
| 1173 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 1174 | md->disk = alloc_disk(perdev_minors); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1175 | if (md->disk == NULL) { |
| 1176 | ret = -ENOMEM; |
| 1177 | goto err_kfree; |
| 1178 | } |
| 1179 | |
| 1180 | spin_lock_init(&md->lock); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1181 | INIT_LIST_HEAD(&md->part); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1182 | md->usage = 1; |
| 1183 | |
Adrian Hunter | d09408a | 2011-06-23 13:40:28 +0300 | [diff] [blame] | 1184 | ret = mmc_init_queue(&md->queue, card, &md->lock, subname); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1185 | if (ret) |
| 1186 | goto err_putdisk; |
| 1187 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1188 | md->queue.issue_fn = mmc_blk_issue_rq; |
| 1189 | md->queue.data = md; |
| 1190 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 1191 | md->disk->major = MMC_BLOCK_MAJOR; |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 1192 | md->disk->first_minor = devidx * perdev_minors; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1193 | md->disk->fops = &mmc_bdops; |
| 1194 | md->disk->private_data = md; |
| 1195 | md->disk->queue = md->queue.queue; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1196 | md->disk->driverfs_dev = &card->dev; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1197 | set_disk_ro(md->disk, md->read_only || default_ro); |
Colin Cross | fa746fa | 2010-09-03 12:41:21 -0700 | [diff] [blame] | 1198 | md->disk->flags = GENHD_FL_EXT_DEVT; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1199 | |
| 1200 | /* |
| 1201 | * As discussed on lkml, GENHD_FL_REMOVABLE should: |
| 1202 | * |
| 1203 | * - be set for removable media with permanent block devices |
| 1204 | * - be unset for removable block devices with permanent media |
| 1205 | * |
| 1206 | * Since MMC block devices clearly fall under the second |
| 1207 | * case, we do not set GENHD_FL_REMOVABLE. Userspace |
| 1208 | * should use the block device creation/destruction hotplug |
| 1209 | * messages to tell when the card is present. |
| 1210 | */ |
| 1211 | |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 1212 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
| 1213 | "mmcblk%d%s", md->name_idx, subname ? subname : ""); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1214 | |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1215 | blk_queue_logical_block_size(md->queue.queue, 512); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1216 | set_capacity(md->disk, size); |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 1217 | |
Andrei Warkentin | f0d8997 | 2011-05-23 15:06:38 -0500 | [diff] [blame] | 1218 | if (mmc_host_cmd23(card->host)) { |
| 1219 | if (mmc_card_mmc(card) || |
| 1220 | (mmc_card_sd(card) && |
| 1221 | card->scr.cmds & SD_SCR_CMD23_SUPPORT)) |
| 1222 | md->flags |= MMC_BLK_CMD23; |
| 1223 | } |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 1224 | |
| 1225 | if (mmc_card_mmc(card) && |
| 1226 | md->flags & MMC_BLK_CMD23 && |
| 1227 | ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || |
| 1228 | card->ext_csd.rel_sectors)) { |
| 1229 | md->flags |= MMC_BLK_REL_WR; |
| 1230 | blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA); |
| 1231 | } |
| 1232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | return md; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1234 | |
| 1235 | err_putdisk: |
| 1236 | put_disk(md->disk); |
| 1237 | err_kfree: |
| 1238 | kfree(md); |
| 1239 | out: |
| 1240 | return ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1243 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) |
| 1244 | { |
| 1245 | sector_t size; |
| 1246 | struct mmc_blk_data *md; |
| 1247 | |
| 1248 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { |
| 1249 | /* |
| 1250 | * The EXT_CSD sector count is in number or 512 byte |
| 1251 | * sectors. |
| 1252 | */ |
| 1253 | size = card->ext_csd.sectors; |
| 1254 | } else { |
| 1255 | /* |
| 1256 | * The CSD capacity field is in units of read_blkbits. |
| 1257 | * set_capacity takes units of 512 bytes. |
| 1258 | */ |
| 1259 | size = card->csd.capacity << (card->csd.read_blkbits - 9); |
| 1260 | } |
| 1261 | |
| 1262 | md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL); |
| 1263 | return md; |
| 1264 | } |
| 1265 | |
| 1266 | static int mmc_blk_alloc_part(struct mmc_card *card, |
| 1267 | struct mmc_blk_data *md, |
| 1268 | unsigned int part_type, |
| 1269 | sector_t size, |
| 1270 | bool default_ro, |
| 1271 | const char *subname) |
| 1272 | { |
| 1273 | char cap_str[10]; |
| 1274 | struct mmc_blk_data *part_md; |
| 1275 | |
| 1276 | part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro, |
| 1277 | subname); |
| 1278 | if (IS_ERR(part_md)) |
| 1279 | return PTR_ERR(part_md); |
| 1280 | part_md->part_type = part_type; |
| 1281 | list_add(&part_md->part, &md->part); |
| 1282 | |
| 1283 | string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2, |
| 1284 | cap_str, sizeof(cap_str)); |
| 1285 | printk(KERN_INFO "%s: %s %s partition %u %s\n", |
| 1286 | part_md->disk->disk_name, mmc_card_id(card), |
| 1287 | mmc_card_name(card), part_md->part_type, cap_str); |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
| 1291 | static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) |
| 1292 | { |
| 1293 | int ret = 0; |
| 1294 | |
| 1295 | if (!mmc_card_mmc(card)) |
| 1296 | return 0; |
| 1297 | |
| 1298 | if (card->ext_csd.boot_size) { |
| 1299 | ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0, |
| 1300 | card->ext_csd.boot_size >> 9, |
| 1301 | true, |
| 1302 | "boot0"); |
| 1303 | if (ret) |
| 1304 | return ret; |
| 1305 | ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1, |
| 1306 | card->ext_csd.boot_size >> 9, |
| 1307 | true, |
| 1308 | "boot1"); |
| 1309 | if (ret) |
| 1310 | return ret; |
| 1311 | } |
| 1312 | |
| 1313 | return ret; |
| 1314 | } |
| 1315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | static int |
| 1317 | mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card) |
| 1318 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | int err; |
| 1320 | |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 1321 | mmc_claim_host(card->host); |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 1322 | err = mmc_set_blocklen(card, 512); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 1323 | mmc_release_host(card->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | |
| 1325 | if (err) { |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 1326 | printk(KERN_ERR "%s: unable to set block size to 512: %d\n", |
| 1327 | md->disk->disk_name, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | return -EINVAL; |
| 1329 | } |
| 1330 | |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1334 | static void mmc_blk_remove_req(struct mmc_blk_data *md) |
| 1335 | { |
| 1336 | if (md) { |
| 1337 | if (md->disk->flags & GENHD_FL_UP) { |
| 1338 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); |
| 1339 | |
| 1340 | /* Stop new requests from getting into the queue */ |
| 1341 | del_gendisk(md->disk); |
| 1342 | } |
| 1343 | |
| 1344 | /* Then flush out any already in there */ |
| 1345 | mmc_cleanup_queue(&md->queue); |
| 1346 | mmc_blk_put(md); |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | static void mmc_blk_remove_parts(struct mmc_card *card, |
| 1351 | struct mmc_blk_data *md) |
| 1352 | { |
| 1353 | struct list_head *pos, *q; |
| 1354 | struct mmc_blk_data *part_md; |
| 1355 | |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 1356 | __clear_bit(md->name_idx, name_use); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1357 | list_for_each_safe(pos, q, &md->part) { |
| 1358 | part_md = list_entry(pos, struct mmc_blk_data, part); |
| 1359 | list_del(pos); |
| 1360 | mmc_blk_remove_req(part_md); |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | static int mmc_add_disk(struct mmc_blk_data *md) |
| 1365 | { |
| 1366 | int ret; |
| 1367 | |
| 1368 | add_disk(md->disk); |
| 1369 | md->force_ro.show = force_ro_show; |
| 1370 | md->force_ro.store = force_ro_store; |
Rabin Vincent | 641c318 | 2011-04-23 20:52:58 +0530 | [diff] [blame] | 1371 | sysfs_attr_init(&md->force_ro.attr); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1372 | md->force_ro.attr.name = "force_ro"; |
| 1373 | md->force_ro.attr.mode = S_IRUGO | S_IWUSR; |
| 1374 | ret = device_create_file(disk_to_dev(md->disk), &md->force_ro); |
| 1375 | if (ret) |
| 1376 | del_gendisk(md->disk); |
| 1377 | |
| 1378 | return ret; |
| 1379 | } |
| 1380 | |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 1381 | static const struct mmc_fixup blk_fixups[] = |
| 1382 | { |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1383 | MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
| 1384 | MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
| 1385 | MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
| 1386 | MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
| 1387 | MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 1388 | |
| 1389 | /* |
| 1390 | * Some MMC cards experience performance degradation with CMD23 |
| 1391 | * instead of CMD12-bounded multiblock transfers. For now we'll |
| 1392 | * black list what's bad... |
| 1393 | * - Certain Toshiba cards. |
| 1394 | * |
| 1395 | * N.B. This doesn't affect SD cards. |
| 1396 | */ |
| 1397 | MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc, |
| 1398 | MMC_QUIRK_BLK_NO_CMD23), |
| 1399 | MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc, |
| 1400 | MMC_QUIRK_BLK_NO_CMD23), |
| 1401 | MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc, |
| 1402 | MMC_QUIRK_BLK_NO_CMD23), |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 1403 | END_FIXUP |
| 1404 | }; |
| 1405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | static int mmc_blk_probe(struct mmc_card *card) |
| 1407 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1408 | struct mmc_blk_data *md, *part_md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | int err; |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 1410 | char cap_str[10]; |
| 1411 | |
Pierre Ossman | 912490d | 2005-05-21 10:27:02 +0100 | [diff] [blame] | 1412 | /* |
| 1413 | * Check that the card supports the command class(es) we need. |
| 1414 | */ |
| 1415 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | return -ENODEV; |
| 1417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | md = mmc_blk_alloc(card); |
| 1419 | if (IS_ERR(md)) |
| 1420 | return PTR_ERR(md); |
| 1421 | |
| 1422 | err = mmc_blk_set_blksize(md, card); |
| 1423 | if (err) |
| 1424 | goto out; |
| 1425 | |
Yi Li | 444122f | 2009-02-05 15:31:57 +0800 | [diff] [blame] | 1426 | string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2, |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 1427 | cap_str, sizeof(cap_str)); |
| 1428 | printk(KERN_INFO "%s: %s %s %s %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 1430 | cap_str, md->read_only ? "(ro)" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1432 | if (mmc_blk_alloc_parts(card, md)) |
| 1433 | goto out; |
| 1434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | mmc_set_drvdata(card, md); |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 1436 | mmc_fixup_device(card, blk_fixups); |
| 1437 | |
San Mehat | c87f8d4 | 2009-07-30 08:21:19 -0700 | [diff] [blame] | 1438 | #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME |
| 1439 | mmc_set_bus_resume_policy(card->host, 1); |
| 1440 | #endif |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1441 | if (mmc_add_disk(md)) |
| 1442 | goto out; |
| 1443 | |
| 1444 | list_for_each_entry(part_md, &md->part, part) { |
| 1445 | if (mmc_add_disk(part_md)) |
| 1446 | goto out; |
| 1447 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | return 0; |
| 1449 | |
| 1450 | out: |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1451 | mmc_blk_remove_parts(card, md); |
| 1452 | mmc_blk_remove_req(md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | return err; |
| 1454 | } |
| 1455 | |
| 1456 | static void mmc_blk_remove(struct mmc_card *card) |
| 1457 | { |
| 1458 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 1459 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1460 | mmc_blk_remove_parts(card, md); |
Adrian Hunter | ddd6fa7 | 2011-06-23 13:40:26 +0300 | [diff] [blame] | 1461 | mmc_claim_host(card->host); |
| 1462 | mmc_blk_part_switch(card, md); |
| 1463 | mmc_release_host(card->host); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1464 | mmc_blk_remove_req(md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | mmc_set_drvdata(card, NULL); |
San Mehat | c87f8d4 | 2009-07-30 08:21:19 -0700 | [diff] [blame] | 1466 | #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME |
| 1467 | mmc_set_bus_resume_policy(card->host, 0); |
| 1468 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | #ifdef CONFIG_PM |
Chuanxiao Dong | 72407e9 | 2011-08-24 14:00:41 +0530 | [diff] [blame] | 1472 | static int mmc_blk_suspend(struct mmc_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1474 | struct mmc_blk_data *part_md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 1476 | |
| 1477 | if (md) { |
| 1478 | mmc_queue_suspend(&md->queue); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1479 | list_for_each_entry(part_md, &md->part, part) { |
| 1480 | mmc_queue_suspend(&part_md->queue); |
| 1481 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | } |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | static int mmc_blk_resume(struct mmc_card *card) |
| 1487 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1488 | struct mmc_blk_data *part_md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 1490 | |
| 1491 | if (md) { |
San Mehat | c87f8d4 | 2009-07-30 08:21:19 -0700 | [diff] [blame] | 1492 | #ifndef CONFIG_MMC_BLOCK_DEFERRED_RESUME |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | mmc_blk_set_blksize(md, card); |
San Mehat | c87f8d4 | 2009-07-30 08:21:19 -0700 | [diff] [blame] | 1494 | #endif |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1495 | |
| 1496 | /* |
| 1497 | * Resume involves the card going into idle state, |
| 1498 | * so current partition is always the main one. |
| 1499 | */ |
| 1500 | md->part_curr = md->part_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | mmc_queue_resume(&md->queue); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1502 | list_for_each_entry(part_md, &md->part, part) { |
| 1503 | mmc_queue_resume(&part_md->queue); |
| 1504 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | } |
| 1506 | return 0; |
| 1507 | } |
| 1508 | #else |
| 1509 | #define mmc_blk_suspend NULL |
| 1510 | #define mmc_blk_resume NULL |
| 1511 | #endif |
| 1512 | |
| 1513 | static struct mmc_driver mmc_driver = { |
| 1514 | .drv = { |
| 1515 | .name = "mmcblk", |
| 1516 | }, |
| 1517 | .probe = mmc_blk_probe, |
| 1518 | .remove = mmc_blk_remove, |
| 1519 | .suspend = mmc_blk_suspend, |
| 1520 | .resume = mmc_blk_resume, |
| 1521 | }; |
| 1522 | |
| 1523 | static int __init mmc_blk_init(void) |
| 1524 | { |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 1525 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 1527 | if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) |
| 1528 | pr_info("mmcblk: using %d minors per device\n", perdev_minors); |
| 1529 | |
| 1530 | max_devices = 256 / perdev_minors; |
| 1531 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 1532 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
| 1533 | if (res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 1536 | res = mmc_register_driver(&mmc_driver); |
| 1537 | if (res) |
| 1538 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 1540 | return 0; |
| 1541 | out2: |
| 1542 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | out: |
| 1544 | return res; |
| 1545 | } |
| 1546 | |
| 1547 | static void __exit mmc_blk_exit(void) |
| 1548 | { |
| 1549 | mmc_unregister_driver(&mmc_driver); |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 1550 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | module_init(mmc_blk_init); |
| 1554 | module_exit(mmc_blk_exit); |
| 1555 | |
| 1556 | MODULE_LICENSE("GPL"); |
| 1557 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); |
| 1558 | |