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