blob: f79b4688e4718f83f792d31a3221f40fd6450edb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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 Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
John Calixtocb87ea22011-04-26 18:56:29 -040038#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020040#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010041#include <linux/mmc/mmc.h>
42#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45
Pierre Ossman98ac2162006-12-23 20:03:02 +010046#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000048MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040049#ifdef MODULE_PARAM_PREFIX
50#undef MODULE_PARAM_PREFIX
51#endif
52#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010053
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050054#define INAND_CMD38_ARG_EXT_CSD 113
55#define INAND_CMD38_ARG_ERASE 0x00
56#define INAND_CMD38_ARG_TRIM 0x01
57#define INAND_CMD38_ARG_SECERASE 0x80
58#define INAND_CMD38_ARG_SECTRIM1 0x81
59#define INAND_CMD38_ARG_SECTRIM2 0x88
Trey Ramsay8fee4762012-11-16 09:31:41 -060060#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050061
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020062static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040063
64/*
65 * The defaults come from config options but can be overriden by module
66 * or bootarg options.
67 */
68static 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 */
74static int max_devices;
75
76/* 256 minors, so at most 256 separate devices */
77static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050078static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
81 * There is one mmc_blk_data per slot.
82 */
83struct mmc_blk_data {
84 spinlock_t lock;
85 struct gendisk *disk;
86 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050087 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Andrei Warkentind0c97cf2011-05-23 15:06:36 -050089 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 Torvalds1da177e2005-04-16 15:20:36 -070093 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +000094 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -050095 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -050096 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +030097 unsigned int reset_done;
98#define MMC_BLK_READ BIT(0)
99#define MMC_BLK_WRITE BIT(1)
100#define MMC_BLK_DISCARD BIT(2)
101#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500102
103 /*
104 * Only set in main mmc_blk_data associated
105 * with mmc_card with mmc_set_drvdata, and keeps
106 * track of the current selected device partition.
107 */
108 unsigned int part_curr;
109 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100110 struct device_attribute power_ro_lock;
111 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
Arjan van de Vena621aae2006-01-12 18:43:35 +0000114static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400116module_param(perdev_minors, int, 0444);
117MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
118
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200119static inline int mmc_blk_part_switch(struct mmc_card *card,
120 struct mmc_blk_data *md);
121static int get_card_status(struct mmc_card *card, u32 *status, int retries);
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
124{
125 struct mmc_blk_data *md;
126
Arjan van de Vena621aae2006-01-12 18:43:35 +0000127 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 md = disk->private_data;
129 if (md && md->usage == 0)
130 md = NULL;
131 if (md)
132 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000133 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 return md;
136}
137
Andrei Warkentin371a6892011-04-11 18:10:25 -0500138static inline int mmc_get_devidx(struct gendisk *disk)
139{
140 int devmaj = MAJOR(disk_devt(disk));
141 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
142
143 if (!devmaj)
144 devidx = disk->first_minor / perdev_minors;
145 return devidx;
146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static void mmc_blk_put(struct mmc_blk_data *md)
149{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000150 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 md->usage--;
152 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500153 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800154 blk_cleanup_queue(md->queue.queue);
155
David Woodhouse1dff3142007-11-21 18:45:12 +0100156 __clear_bit(devidx, dev_use);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 kfree(md);
160 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000161 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Johan Rudholmadd710e2011-12-02 08:51:06 +0100164static ssize_t power_ro_lock_show(struct device *dev,
165 struct device_attribute *attr, char *buf)
166{
167 int ret;
168 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
169 struct mmc_card *card = md->queue.card;
170 int locked = 0;
171
172 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
173 locked = 2;
174 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
175 locked = 1;
176
177 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
178
179 return ret;
180}
181
182static ssize_t power_ro_lock_store(struct device *dev,
183 struct device_attribute *attr, const char *buf, size_t count)
184{
185 int ret;
186 struct mmc_blk_data *md, *part_md;
187 struct mmc_card *card;
188 unsigned long set;
189
190 if (kstrtoul(buf, 0, &set))
191 return -EINVAL;
192
193 if (set != 1)
194 return count;
195
196 md = mmc_blk_get(dev_to_disk(dev));
197 card = md->queue.card;
198
199 mmc_claim_host(card->host);
200
201 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
202 card->ext_csd.boot_ro_lock |
203 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
204 card->ext_csd.part_time);
205 if (ret)
206 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
207 else
208 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
209
210 mmc_release_host(card->host);
211
212 if (!ret) {
213 pr_info("%s: Locking boot partition ro until next power on\n",
214 md->disk->disk_name);
215 set_disk_ro(md->disk, 1);
216
217 list_for_each_entry(part_md, &md->part, part)
218 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
219 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
220 set_disk_ro(part_md->disk, 1);
221 }
222 }
223
224 mmc_blk_put(md);
225 return count;
226}
227
Andrei Warkentin371a6892011-04-11 18:10:25 -0500228static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
229 char *buf)
230{
231 int ret;
232 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
233
234 ret = snprintf(buf, PAGE_SIZE, "%d",
235 get_disk_ro(dev_to_disk(dev)) ^
236 md->read_only);
237 mmc_blk_put(md);
238 return ret;
239}
240
241static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
242 const char *buf, size_t count)
243{
244 int ret;
245 char *end;
246 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
247 unsigned long set = simple_strtoul(buf, &end, 0);
248 if (end == buf) {
249 ret = -EINVAL;
250 goto out;
251 }
252
253 set_disk_ro(dev_to_disk(dev), set || md->read_only);
254 ret = count;
255out:
256 mmc_blk_put(md);
257 return ret;
258}
259
Al Viroa5a15612008-03-02 10:33:30 -0500260static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Al Viroa5a15612008-03-02 10:33:30 -0500262 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 int ret = -ENXIO;
264
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200265 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (md) {
267 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500268 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700270
Al Viroa5a15612008-03-02 10:33:30 -0500271 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700272 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700273 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200276 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 return ret;
279}
280
Al Viroa5a15612008-03-02 10:33:30 -0500281static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Al Viroa5a15612008-03-02 10:33:30 -0500283 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200285 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200287 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return 0;
289}
290
291static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800292mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800294 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
295 geo->heads = 4;
296 geo->sectors = 16;
297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
John Calixtocb87ea22011-04-26 18:56:29 -0400300struct mmc_blk_ioc_data {
301 struct mmc_ioc_cmd ic;
302 unsigned char *buf;
303 u64 buf_bytes;
304};
305
306static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
307 struct mmc_ioc_cmd __user *user)
308{
309 struct mmc_blk_ioc_data *idata;
310 int err;
311
312 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
313 if (!idata) {
314 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400315 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400316 }
317
318 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
319 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400320 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400321 }
322
323 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
324 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
325 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400326 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400327 }
328
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100329 if (!idata->buf_bytes)
330 return idata;
331
John Calixtocb87ea22011-04-26 18:56:29 -0400332 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
333 if (!idata->buf) {
334 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400335 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400336 }
337
338 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
339 idata->ic.data_ptr, idata->buf_bytes)) {
340 err = -EFAULT;
341 goto copy_err;
342 }
343
344 return idata;
345
346copy_err:
347 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400348idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400349 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400350out:
John Calixtocb87ea22011-04-26 18:56:29 -0400351 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400352}
353
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200354static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
355 u32 retries_max)
356{
357 int err;
358 u32 retry_count = 0;
359
360 if (!status || !retries_max)
361 return -EINVAL;
362
363 do {
364 err = get_card_status(card, status, 5);
365 if (err)
366 break;
367
368 if (!R1_STATUS(*status) &&
369 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
370 break; /* RPMB programming operation complete */
371
372 /*
373 * Rechedule to give the MMC device a chance to continue
374 * processing the previous command without being polled too
375 * frequently.
376 */
377 usleep_range(1000, 5000);
378 } while (++retry_count < retries_max);
379
380 if (retry_count == retries_max)
381 err = -EPERM;
382
383 return err;
384}
385
John Calixtocb87ea22011-04-26 18:56:29 -0400386static int mmc_blk_ioctl_cmd(struct block_device *bdev,
387 struct mmc_ioc_cmd __user *ic_ptr)
388{
389 struct mmc_blk_ioc_data *idata;
390 struct mmc_blk_data *md;
391 struct mmc_card *card;
392 struct mmc_command cmd = {0};
393 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530394 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400395 struct scatterlist sg;
396 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200397 int is_rpmb = false;
398 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400399
400 /*
401 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
402 * whole block device, not on a partition. This prevents overspray
403 * between sibling partitions.
404 */
405 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
406 return -EPERM;
407
408 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
409 if (IS_ERR(idata))
410 return PTR_ERR(idata);
411
John Calixtocb87ea22011-04-26 18:56:29 -0400412 md = mmc_blk_get(bdev->bd_disk);
413 if (!md) {
414 err = -EINVAL;
Philippe De Swert1c02f002012-04-11 23:31:45 +0300415 goto cmd_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400416 }
417
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200418 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
419 is_rpmb = true;
420
John Calixtocb87ea22011-04-26 18:56:29 -0400421 card = md->queue.card;
422 if (IS_ERR(card)) {
423 err = PTR_ERR(card);
424 goto cmd_done;
425 }
426
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100427 cmd.opcode = idata->ic.opcode;
428 cmd.arg = idata->ic.arg;
429 cmd.flags = idata->ic.flags;
430
431 if (idata->buf_bytes) {
432 data.sg = &sg;
433 data.sg_len = 1;
434 data.blksz = idata->ic.blksz;
435 data.blocks = idata->ic.blocks;
436
437 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
438
439 if (idata->ic.write_flag)
440 data.flags = MMC_DATA_WRITE;
441 else
442 data.flags = MMC_DATA_READ;
443
444 /* data.flags must already be set before doing this. */
445 mmc_set_data_timeout(&data, card);
446
447 /* Allow overriding the timeout_ns for empirical tuning. */
448 if (idata->ic.data_timeout_ns)
449 data.timeout_ns = idata->ic.data_timeout_ns;
450
451 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
452 /*
453 * Pretend this is a data transfer and rely on the
454 * host driver to compute timeout. When all host
455 * drivers support cmd.cmd_timeout for R1B, this
456 * can be changed to:
457 *
458 * mrq.data = NULL;
459 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
460 */
461 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
462 }
463
464 mrq.data = &data;
465 }
466
467 mrq.cmd = &cmd;
468
John Calixtocb87ea22011-04-26 18:56:29 -0400469 mmc_claim_host(card->host);
470
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200471 err = mmc_blk_part_switch(card, md);
472 if (err)
473 goto cmd_rel_host;
474
John Calixtocb87ea22011-04-26 18:56:29 -0400475 if (idata->ic.is_acmd) {
476 err = mmc_app_cmd(card->host, card);
477 if (err)
478 goto cmd_rel_host;
479 }
480
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200481 if (is_rpmb) {
482 err = mmc_set_blockcount(card, data.blocks,
483 idata->ic.write_flag & (1 << 31));
484 if (err)
485 goto cmd_rel_host;
486 }
487
John Calixtocb87ea22011-04-26 18:56:29 -0400488 mmc_wait_for_req(card->host, &mrq);
489
490 if (cmd.error) {
491 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
492 __func__, cmd.error);
493 err = cmd.error;
494 goto cmd_rel_host;
495 }
496 if (data.error) {
497 dev_err(mmc_dev(card->host), "%s: data error %d\n",
498 __func__, data.error);
499 err = data.error;
500 goto cmd_rel_host;
501 }
502
503 /*
504 * According to the SD specs, some commands require a delay after
505 * issuing the command.
506 */
507 if (idata->ic.postsleep_min_us)
508 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
509
510 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
511 err = -EFAULT;
512 goto cmd_rel_host;
513 }
514
515 if (!idata->ic.write_flag) {
516 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
517 idata->buf, idata->buf_bytes)) {
518 err = -EFAULT;
519 goto cmd_rel_host;
520 }
521 }
522
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200523 if (is_rpmb) {
524 /*
525 * Ensure RPMB command has completed by polling CMD13
526 * "Send Status".
527 */
528 err = ioctl_rpmb_card_status_poll(card, &status, 5);
529 if (err)
530 dev_err(mmc_dev(card->host),
531 "%s: Card Status=0x%08X, error %d\n",
532 __func__, status, err);
533 }
534
John Calixtocb87ea22011-04-26 18:56:29 -0400535cmd_rel_host:
536 mmc_release_host(card->host);
537
538cmd_done:
539 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300540cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400541 kfree(idata->buf);
542 kfree(idata);
543 return err;
544}
545
546static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
547 unsigned int cmd, unsigned long arg)
548{
549 int ret = -EINVAL;
550 if (cmd == MMC_IOC_CMD)
551 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
552 return ret;
553}
554
555#ifdef CONFIG_COMPAT
556static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
557 unsigned int cmd, unsigned long arg)
558{
559 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
560}
561#endif
562
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700563static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500564 .open = mmc_blk_open,
565 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800566 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400568 .ioctl = mmc_blk_ioctl,
569#ifdef CONFIG_COMPAT
570 .compat_ioctl = mmc_blk_compat_ioctl,
571#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572};
573
Andrei Warkentin371a6892011-04-11 18:10:25 -0500574static inline int mmc_blk_part_switch(struct mmc_card *card,
575 struct mmc_blk_data *md)
576{
577 int ret;
578 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300579
Andrei Warkentin371a6892011-04-11 18:10:25 -0500580 if (main_md->part_curr == md->part_type)
581 return 0;
582
583 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300584 u8 part_config = card->ext_csd.part_config;
585
586 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
587 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500588
589 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300590 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500591 card->ext_csd.part_time);
592 if (ret)
593 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300594
595 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300596 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500597
598 main_md->part_curr = md->part_type;
599 return 0;
600}
601
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700602static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
603{
604 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100605 u32 result;
606 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700607
Venkatraman Sad5fd972011-08-25 00:30:50 +0530608 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400609 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400610 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700611
612 struct scatterlist sg;
613
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700614 cmd.opcode = MMC_APP_CMD;
615 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700616 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700617
618 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700619 if (err)
620 return (u32)-1;
621 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700622 return (u32)-1;
623
624 memset(&cmd, 0, sizeof(struct mmc_command));
625
626 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
627 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700628 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700629
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700630 data.blksz = 4;
631 data.blocks = 1;
632 data.flags = MMC_DATA_READ;
633 data.sg = &sg;
634 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530635 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700636
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700637 mrq.cmd = &cmd;
638 mrq.data = &data;
639
Ben Dooks051913d2009-06-08 23:33:57 +0100640 blocks = kmalloc(4, GFP_KERNEL);
641 if (!blocks)
642 return (u32)-1;
643
644 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700645
646 mmc_wait_for_req(card->host, &mrq);
647
Ben Dooks051913d2009-06-08 23:33:57 +0100648 result = ntohl(*blocks);
649 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700650
Ben Dooks051913d2009-06-08 23:33:57 +0100651 if (cmd.error || data.error)
652 result = (u32)-1;
653
654 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700655}
656
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100657static int send_stop(struct mmc_card *card, u32 *status)
658{
659 struct mmc_command cmd = {0};
660 int err;
661
662 cmd.opcode = MMC_STOP_TRANSMISSION;
663 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
664 err = mmc_wait_for_cmd(card->host, &cmd, 5);
665 if (err == 0)
666 *status = cmd.resp[0];
667 return err;
668}
669
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100670static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300671{
Chris Ball1278dba2011-04-13 23:40:30 -0400672 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300673 int err;
674
Adrian Hunter504f1912008-10-16 12:55:25 +0300675 cmd.opcode = MMC_SEND_STATUS;
676 if (!mmc_host_is_spi(card->host))
677 cmd.arg = card->rca << 16;
678 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100679 err = mmc_wait_for_cmd(card->host, &cmd, retries);
680 if (err == 0)
681 *status = cmd.resp[0];
682 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300683}
684
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530685#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100686#define ERR_RETRY 2
687#define ERR_ABORT 1
688#define ERR_CONTINUE 0
689
690static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
691 bool status_valid, u32 status)
692{
693 switch (error) {
694 case -EILSEQ:
695 /* response crc error, retry the r/w cmd */
696 pr_err("%s: %s sending %s command, card status %#x\n",
697 req->rq_disk->disk_name, "response CRC error",
698 name, status);
699 return ERR_RETRY;
700
701 case -ETIMEDOUT:
702 pr_err("%s: %s sending %s command, card status %#x\n",
703 req->rq_disk->disk_name, "timed out", name, status);
704
705 /* If the status cmd initially failed, retry the r/w cmd */
706 if (!status_valid)
707 return ERR_RETRY;
708
709 /*
710 * If it was a r/w cmd crc error, or illegal command
711 * (eg, issued in wrong state) then retry - we should
712 * have corrected the state problem above.
713 */
714 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
715 return ERR_RETRY;
716
717 /* Otherwise abort the command */
718 return ERR_ABORT;
719
720 default:
721 /* We don't understand the error code the driver gave us */
722 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
723 req->rq_disk->disk_name, error, status);
724 return ERR_ABORT;
725 }
726}
727
728/*
729 * Initial r/w and stop cmd error recovery.
730 * We don't know whether the card received the r/w cmd or not, so try to
731 * restore things back to a sane state. Essentially, we do this as follows:
732 * - Obtain card status. If the first attempt to obtain card status fails,
733 * the status word will reflect the failed status cmd, not the failed
734 * r/w cmd. If we fail to obtain card status, it suggests we can no
735 * longer communicate with the card.
736 * - Check the card state. If the card received the cmd but there was a
737 * transient problem with the response, it might still be in a data transfer
738 * mode. Try to send it a stop command. If this fails, we can't recover.
739 * - If the r/w cmd failed due to a response CRC error, it was probably
740 * transient, so retry the cmd.
741 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
742 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
743 * illegal cmd, retry.
744 * Otherwise we don't understand what happened, so abort.
745 */
746static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +0300747 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100748{
749 bool prev_cmd_status_valid = true;
750 u32 status, stop_status = 0;
751 int err, retry;
752
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530753 if (mmc_card_removed(card))
754 return ERR_NOMEDIUM;
755
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100756 /*
757 * Try to get card status which indicates both the card state
758 * and why there was no response. If the first attempt fails,
759 * we can't be sure the returned status is for the r/w command.
760 */
761 for (retry = 2; retry >= 0; retry--) {
762 err = get_card_status(card, &status, 0);
763 if (!err)
764 break;
765
766 prev_cmd_status_valid = false;
767 pr_err("%s: error %d sending status command, %sing\n",
768 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
769 }
770
771 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530772 if (err) {
773 /* Check if the card is removed */
774 if (mmc_detect_card_removed(card->host))
775 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100776 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530777 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100778
Adrian Hunter67716322011-08-29 16:42:15 +0300779 /* Flag ECC errors */
780 if ((status & R1_CARD_ECC_FAILED) ||
781 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
782 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
783 *ecc_err = 1;
784
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100785 /*
786 * Check the current card state. If it is in some data transfer
787 * mode, tell it to stop (and hopefully transition back to TRAN.)
788 */
789 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
790 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
791 err = send_stop(card, &stop_status);
792 if (err)
793 pr_err("%s: error %d sending stop command\n",
794 req->rq_disk->disk_name, err);
795
796 /*
797 * If the stop cmd also timed out, the card is probably
798 * not present, so abort. Other errors are bad news too.
799 */
800 if (err)
801 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300802 if (stop_status & R1_CARD_ECC_FAILED)
803 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100804 }
805
806 /* Check for set block count errors */
807 if (brq->sbc.error)
808 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
809 prev_cmd_status_valid, status);
810
811 /* Check for r/w command errors */
812 if (brq->cmd.error)
813 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
814 prev_cmd_status_valid, status);
815
Adrian Hunter67716322011-08-29 16:42:15 +0300816 /* Data errors */
817 if (!brq->stop.error)
818 return ERR_CONTINUE;
819
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100820 /* Now for stop errors. These aren't fatal to the transfer. */
821 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
822 req->rq_disk->disk_name, brq->stop.error,
823 brq->cmd.resp[0], status);
824
825 /*
826 * Subsitute in our own stop status as this will give the error
827 * state which happened during the execution of the r/w command.
828 */
829 if (stop_status) {
830 brq->stop.resp[0] = stop_status;
831 brq->stop.error = 0;
832 }
833 return ERR_CONTINUE;
834}
835
Adrian Hunter67716322011-08-29 16:42:15 +0300836static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
837 int type)
838{
839 int err;
840
841 if (md->reset_done & type)
842 return -EEXIST;
843
844 md->reset_done |= type;
845 err = mmc_hw_reset(host);
846 /* Ensure we switch back to the correct partition */
847 if (err != -EOPNOTSUPP) {
848 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
849 int part_err;
850
851 main_md->part_curr = main_md->part_type;
852 part_err = mmc_blk_part_switch(host->card, md);
853 if (part_err) {
854 /*
855 * We have failed to get back into the correct
856 * partition, so we need to abort the whole request.
857 */
858 return -ENODEV;
859 }
860 }
861 return err;
862}
863
864static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
865{
866 md->reset_done &= ~type;
867}
868
Adrian Hunterbd788c92010-08-11 14:17:47 -0700869static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
870{
871 struct mmc_blk_data *md = mq->data;
872 struct mmc_card *card = md->queue.card;
873 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300874 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700875
Adrian Hunterbd788c92010-08-11 14:17:47 -0700876 if (!mmc_can_erase(card)) {
877 err = -EOPNOTSUPP;
878 goto out;
879 }
880
881 from = blk_rq_pos(req);
882 nr = blk_rq_sectors(req);
883
Kyungmin Parkb3bf9152011-10-18 09:34:04 +0900884 if (mmc_can_discard(card))
885 arg = MMC_DISCARD_ARG;
886 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -0700887 arg = MMC_TRIM_ARG;
888 else
889 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300890retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500891 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
892 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
893 INAND_CMD38_ARG_EXT_CSD,
894 arg == MMC_TRIM_ARG ?
895 INAND_CMD38_ARG_TRIM :
896 INAND_CMD38_ARG_ERASE,
897 0);
898 if (err)
899 goto out;
900 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700901 err = mmc_erase(card, from, nr, arg);
902out:
Adrian Hunter67716322011-08-29 16:42:15 +0300903 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
904 goto retry;
905 if (!err)
906 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +0530907 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -0700908
Adrian Hunterbd788c92010-08-11 14:17:47 -0700909 return err ? 0 : 1;
910}
911
Adrian Hunter49804542010-08-11 14:17:50 -0700912static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
913 struct request *req)
914{
915 struct mmc_blk_data *md = mq->data;
916 struct mmc_card *card = md->queue.card;
Adrian Hunter28302812012-04-05 14:45:48 +0300917 unsigned int from, nr, arg, trim_arg, erase_arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300918 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700919
Kyungmin Parkd9ddd622011-10-14 14:15:48 +0900920 if (!(mmc_can_secure_erase_trim(card) || mmc_can_sanitize(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700921 err = -EOPNOTSUPP;
922 goto out;
923 }
924
925 from = blk_rq_pos(req);
926 nr = blk_rq_sectors(req);
927
Adrian Hunter28302812012-04-05 14:45:48 +0300928 /* The sanitize operation is supported at v4.5 only */
929 if (mmc_can_sanitize(card)) {
930 erase_arg = MMC_ERASE_ARG;
931 trim_arg = MMC_TRIM_ARG;
932 } else {
933 erase_arg = MMC_SECURE_ERASE_ARG;
934 trim_arg = MMC_SECURE_TRIM1_ARG;
935 }
936
937 if (mmc_erase_group_aligned(card, from, nr))
938 arg = erase_arg;
939 else if (mmc_can_trim(card))
940 arg = trim_arg;
941 else {
942 err = -EINVAL;
943 goto out;
944 }
Adrian Hunter67716322011-08-29 16:42:15 +0300945retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500946 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
947 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
948 INAND_CMD38_ARG_EXT_CSD,
949 arg == MMC_SECURE_TRIM1_ARG ?
950 INAND_CMD38_ARG_SECTRIM1 :
951 INAND_CMD38_ARG_SECERASE,
952 0);
953 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300954 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500955 }
Adrian Hunter28302812012-04-05 14:45:48 +0300956
Adrian Hunter49804542010-08-11 14:17:50 -0700957 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +0300958 if (err == -EIO)
959 goto out_retry;
960 if (err)
961 goto out;
962
963 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500964 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
965 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
966 INAND_CMD38_ARG_EXT_CSD,
967 INAND_CMD38_ARG_SECTRIM2,
968 0);
969 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300970 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500971 }
Adrian Hunter28302812012-04-05 14:45:48 +0300972
Adrian Hunter49804542010-08-11 14:17:50 -0700973 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +0300974 if (err == -EIO)
975 goto out_retry;
976 if (err)
977 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500978 }
Adrian Hunter28302812012-04-05 14:45:48 +0300979
980 if (mmc_can_sanitize(card))
981 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
982 EXT_CSD_SANITIZE_START, 1, 0);
983out_retry:
984 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +0300985 goto retry;
986 if (!err)
987 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +0300988out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +0530989 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -0700990
Adrian Hunter49804542010-08-11 14:17:50 -0700991 return err ? 0 : 1;
992}
993
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500994static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
995{
996 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900997 struct mmc_card *card = md->queue.card;
998 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500999
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001000 ret = mmc_flush_cache(card);
1001 if (ret)
1002 ret = -EIO;
1003
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301004 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001005
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001006 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001007}
1008
1009/*
1010 * Reformat current write as a reliable write, supporting
1011 * both legacy and the enhanced reliable write MMC cards.
1012 * In each transfer we'll handle only as much as a single
1013 * reliable write can handle, thus finish the request in
1014 * partial completions.
1015 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001016static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1017 struct mmc_card *card,
1018 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001019{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001020 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1021 /* Legacy mode imposes restrictions on transfers. */
1022 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1023 brq->data.blocks = 1;
1024
1025 if (brq->data.blocks > card->ext_csd.rel_sectors)
1026 brq->data.blocks = card->ext_csd.rel_sectors;
1027 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1028 brq->data.blocks = 1;
1029 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001030}
1031
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001032#define CMD_ERRORS \
1033 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1034 R1_ADDRESS_ERROR | /* Misaligned address */ \
1035 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1036 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1037 R1_CC_ERROR | /* Card controller error */ \
1038 R1_ERROR) /* General/unknown error */
1039
Per Forlinee8a43a2011-07-01 18:55:33 +02001040static int mmc_blk_err_check(struct mmc_card *card,
1041 struct mmc_async_req *areq)
Per Forlind78d4a8a2011-07-01 18:55:30 +02001042{
Per Forlinee8a43a2011-07-01 18:55:33 +02001043 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1044 mmc_active);
1045 struct mmc_blk_request *brq = &mq_mrq->brq;
1046 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001047 int ecc_err = 0;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001048
1049 /*
1050 * sbc.error indicates a problem with the set block count
1051 * command. No data will have been transferred.
1052 *
1053 * cmd.error indicates a problem with the r/w command. No
1054 * data will have been transferred.
1055 *
1056 * stop.error indicates a problem with the stop command. Data
1057 * may have been transferred, or may still be transferring.
1058 */
Adrian Hunter67716322011-08-29 16:42:15 +03001059 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1060 brq->data.error) {
1061 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a8a2011-07-01 18:55:30 +02001062 case ERR_RETRY:
1063 return MMC_BLK_RETRY;
1064 case ERR_ABORT:
1065 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301066 case ERR_NOMEDIUM:
1067 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001068 case ERR_CONTINUE:
1069 break;
1070 }
1071 }
1072
1073 /*
1074 * Check for errors relating to the execution of the
1075 * initial command - such as address errors. No data
1076 * has been transferred.
1077 */
1078 if (brq->cmd.resp[0] & CMD_ERRORS) {
1079 pr_err("%s: r/w command failed, status = %#x\n",
1080 req->rq_disk->disk_name, brq->cmd.resp[0]);
1081 return MMC_BLK_ABORT;
1082 }
1083
1084 /*
1085 * Everything else is either success, or a data error of some
1086 * kind. If it was a write, we may have transitioned to
1087 * program mode, which we have to wait for it to complete.
1088 */
1089 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1090 u32 status;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001091 unsigned long timeout;
1092
1093 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
Per Forlind78d4a8a2011-07-01 18:55:30 +02001094 do {
1095 int err = get_card_status(card, &status, 5);
1096 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301097 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a8a2011-07-01 18:55:30 +02001098 req->rq_disk->disk_name, err);
1099 return MMC_BLK_CMD_ERR;
1100 }
Trey Ramsay8fee4762012-11-16 09:31:41 -06001101
1102 /* Timeout if the device never becomes ready for data
1103 * and never leaves the program state.
1104 */
1105 if (time_after(jiffies, timeout)) {
1106 pr_err("%s: Card stuck in programming state!"\
1107 " %s %s\n", mmc_hostname(card->host),
1108 req->rq_disk->disk_name, __func__);
1109
1110 return MMC_BLK_CMD_ERR;
1111 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001112 /*
1113 * Some cards mishandle the status bits,
1114 * so make sure to check both the busy
1115 * indication and the card state.
1116 */
1117 } while (!(status & R1_READY_FOR_DATA) ||
1118 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1119 }
1120
1121 if (brq->data.error) {
1122 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1123 req->rq_disk->disk_name, brq->data.error,
1124 (unsigned)blk_rq_pos(req),
1125 (unsigned)blk_rq_sectors(req),
1126 brq->cmd.resp[0], brq->stop.resp[0]);
1127
1128 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001129 if (ecc_err)
1130 return MMC_BLK_ECC_ERR;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001131 return MMC_BLK_DATA_ERR;
1132 } else {
1133 return MMC_BLK_CMD_ERR;
1134 }
1135 }
1136
Adrian Hunter67716322011-08-29 16:42:15 +03001137 if (!brq->data.bytes_xfered)
1138 return MMC_BLK_RETRY;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001139
Adrian Hunter67716322011-08-29 16:42:15 +03001140 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1141 return MMC_BLK_PARTIAL;
1142
1143 return MMC_BLK_SUCCESS;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001144}
1145
Per Forlin54d49d72011-07-01 18:55:29 +02001146static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1147 struct mmc_card *card,
1148 int disable_multi,
1149 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Per Forlin54d49d72011-07-01 18:55:29 +02001151 u32 readcmd, writecmd;
1152 struct mmc_blk_request *brq = &mqrq->brq;
1153 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301155 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001157 /*
1158 * Reliable writes are used to implement Forced Unit Access and
1159 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001160 *
1161 * XXX: this really needs a good explanation of why REQ_META
1162 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001163 */
1164 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1165 (req->cmd_flags & REQ_META)) &&
1166 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001167 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001168
Per Forlin54d49d72011-07-01 18:55:29 +02001169 memset(brq, 0, sizeof(struct mmc_blk_request));
1170 brq->mrq.cmd = &brq->cmd;
1171 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Per Forlin54d49d72011-07-01 18:55:29 +02001173 brq->cmd.arg = blk_rq_pos(req);
1174 if (!mmc_card_blockaddr(card))
1175 brq->cmd.arg <<= 9;
1176 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1177 brq->data.blksz = 512;
1178 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1179 brq->stop.arg = 0;
1180 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1181 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Per Forlin54d49d72011-07-01 18:55:29 +02001183 /*
1184 * The block layer doesn't support all sector count
1185 * restrictions, so we need to be prepared for too big
1186 * requests.
1187 */
1188 if (brq->data.blocks > card->host->max_blk_count)
1189 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001191 if (brq->data.blocks > 1) {
1192 /*
1193 * After a read error, we redo the request one sector
1194 * at a time in order to accurately determine which
1195 * sectors can be read successfully.
1196 */
1197 if (disable_multi)
1198 brq->data.blocks = 1;
1199
1200 /* Some controllers can't do multiblock reads due to hw bugs */
1201 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1202 rq_data_dir(req) == READ)
1203 brq->data.blocks = 1;
1204 }
Per Forlin54d49d72011-07-01 18:55:29 +02001205
1206 if (brq->data.blocks > 1 || do_rel_wr) {
1207 /* SPI multiblock writes terminate using a special
1208 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001209 */
Per Forlin54d49d72011-07-01 18:55:29 +02001210 if (!mmc_host_is_spi(card->host) ||
1211 rq_data_dir(req) == READ)
1212 brq->mrq.stop = &brq->stop;
1213 readcmd = MMC_READ_MULTIPLE_BLOCK;
1214 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1215 } else {
1216 brq->mrq.stop = NULL;
1217 readcmd = MMC_READ_SINGLE_BLOCK;
1218 writecmd = MMC_WRITE_BLOCK;
1219 }
1220 if (rq_data_dir(req) == READ) {
1221 brq->cmd.opcode = readcmd;
1222 brq->data.flags |= MMC_DATA_READ;
1223 } else {
1224 brq->cmd.opcode = writecmd;
1225 brq->data.flags |= MMC_DATA_WRITE;
1226 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001227
Per Forlin54d49d72011-07-01 18:55:29 +02001228 if (do_rel_wr)
1229 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001230
Per Forlin54d49d72011-07-01 18:55:29 +02001231 /*
Saugata Das42659002011-12-21 13:09:17 +05301232 * Data tag is used only during writing meta data to speed
1233 * up write and any subsequent read of this meta data
1234 */
1235 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1236 (req->cmd_flags & REQ_META) &&
1237 (rq_data_dir(req) == WRITE) &&
1238 ((brq->data.blocks * brq->data.blksz) >=
1239 card->ext_csd.data_tag_unit_size);
1240
1241 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001242 * Pre-defined multi-block transfers are preferable to
1243 * open ended-ones (and necessary for reliable writes).
1244 * However, it is not sufficient to just send CMD23,
1245 * and avoid the final CMD12, as on an error condition
1246 * CMD12 (stop) needs to be sent anyway. This, coupled
1247 * with Auto-CMD23 enhancements provided by some
1248 * hosts, means that the complexity of dealing
1249 * with this is best left to the host. If CMD23 is
1250 * supported by card and host, we'll fill sbc in and let
1251 * the host deal with handling it correctly. This means
1252 * that for hosts that don't expose MMC_CAP_CMD23, no
1253 * change of behavior will be observed.
1254 *
1255 * N.B: Some MMC cards experience perf degradation.
1256 * We'll avoid using CMD23-bounded multiblock writes for
1257 * these, while retaining features like reliable writes.
1258 */
Saugata Das42659002011-12-21 13:09:17 +05301259 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1260 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1261 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001262 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1263 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301264 (do_rel_wr ? (1 << 31) : 0) |
1265 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001266 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1267 brq->mrq.sbc = &brq->sbc;
1268 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001269
Per Forlin54d49d72011-07-01 18:55:29 +02001270 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001271
Per Forlin54d49d72011-07-01 18:55:29 +02001272 brq->data.sg = mqrq->sg;
1273 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001274
Per Forlin54d49d72011-07-01 18:55:29 +02001275 /*
1276 * Adjust the sg list so it is the same size as the
1277 * request.
1278 */
1279 if (brq->data.blocks != blk_rq_sectors(req)) {
1280 int i, data_size = brq->data.blocks << 9;
1281 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001282
Per Forlin54d49d72011-07-01 18:55:29 +02001283 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1284 data_size -= sg->length;
1285 if (data_size <= 0) {
1286 sg->length += data_size;
1287 i++;
1288 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001289 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001290 }
Per Forlin54d49d72011-07-01 18:55:29 +02001291 brq->data.sg_len = i;
1292 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001293
Per Forlinee8a43a2011-07-01 18:55:33 +02001294 mqrq->mmc_active.mrq = &brq->mrq;
1295 mqrq->mmc_active.err_check = mmc_blk_err_check;
1296
Per Forlin54d49d72011-07-01 18:55:29 +02001297 mmc_queue_bounce_pre(mqrq);
1298}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Adrian Hunter67716322011-08-29 16:42:15 +03001300static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1301 struct mmc_blk_request *brq, struct request *req,
1302 int ret)
1303{
1304 /*
1305 * If this is an SD card and we're writing, we can first
1306 * mark the known good sectors as ok.
1307 *
1308 * If the card is not SD, we can still ok written sectors
1309 * as reported by the controller (which might be less than
1310 * the real number of written sectors, but never more).
1311 */
1312 if (mmc_card_sd(card)) {
1313 u32 blocks;
1314
1315 blocks = mmc_sd_num_wr_blocks(card);
1316 if (blocks != (u32)-1) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301317 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001318 }
1319 } else {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301320 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001321 }
1322 return ret;
1323}
1324
Per Forlinee8a43a2011-07-01 18:55:33 +02001325static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02001326{
1327 struct mmc_blk_data *md = mq->data;
1328 struct mmc_card *card = md->queue.card;
1329 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001330 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001331 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001332 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301333 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02001334 struct mmc_async_req *areq;
1335
1336 if (!rqc && !mq->mqrq_prev->req)
1337 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001338
1339 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001340 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301341 /*
1342 * When 4KB native sector is enabled, only 8 blocks
1343 * multiple read or write is allowed
1344 */
1345 if ((brq->data.blocks & 0x07) &&
1346 (card->ext_csd.data_sector_size == 4096)) {
1347 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1348 req->rq_disk->disk_name);
1349 goto cmd_abort;
1350 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001351 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1352 areq = &mq->mqrq_cur->mmc_active;
1353 } else
1354 areq = NULL;
1355 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001356 if (!areq) {
1357 if (status == MMC_BLK_NEW_REQUEST)
1358 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02001359 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001360 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001361
Per Forlinee8a43a2011-07-01 18:55:33 +02001362 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1363 brq = &mq_rq->brq;
1364 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001365 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001366 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001367
Per Forlind78d4a8a2011-07-01 18:55:30 +02001368 switch (status) {
1369 case MMC_BLK_SUCCESS:
1370 case MMC_BLK_PARTIAL:
1371 /*
1372 * A block was successfully transferred.
1373 */
Adrian Hunter67716322011-08-29 16:42:15 +03001374 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301375 ret = blk_end_request(req, 0,
Per Forlind78d4a8a2011-07-01 18:55:30 +02001376 brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001377 /*
1378 * If the blk_end_request function returns non-zero even
1379 * though all data has been transferred and no errors
1380 * were returned by the host controller, it's a bug.
1381 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001382 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301383 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001384 __func__, blk_rq_bytes(req),
1385 brq->data.bytes_xfered);
1386 rqc = NULL;
1387 goto cmd_abort;
1388 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001389 break;
1390 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001391 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1392 if (!mmc_blk_reset(md, card->host, type))
1393 break;
1394 goto cmd_abort;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001395 case MMC_BLK_RETRY:
1396 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001397 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001398 /* Fall through */
Per Forlind78d4a8a2011-07-01 18:55:30 +02001399 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001400 if (!mmc_blk_reset(md, card->host, type))
1401 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001402 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001403 case MMC_BLK_DATA_ERR: {
1404 int err;
1405
1406 err = mmc_blk_reset(md, card->host, type);
1407 if (!err)
1408 break;
1409 if (err == -ENODEV)
1410 goto cmd_abort;
1411 /* Fall through */
1412 }
1413 case MMC_BLK_ECC_ERR:
1414 if (brq->data.blocks > 1) {
1415 /* Redo read one sector at a time */
1416 pr_warning("%s: retrying using single block read\n",
1417 req->rq_disk->disk_name);
1418 disable_multi = 1;
1419 break;
1420 }
Per Forlind78d4a8a2011-07-01 18:55:30 +02001421 /*
1422 * After an error, we redo I/O one sector at a
1423 * time, so we only reach here after trying to
1424 * read a single sector.
1425 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301426 ret = blk_end_request(req, -EIO,
Per Forlind78d4a8a2011-07-01 18:55:30 +02001427 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001428 if (!ret)
1429 goto start_new_req;
Per Forlind78d4a8a2011-07-01 18:55:30 +02001430 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301431 case MMC_BLK_NOMEDIUM:
1432 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001433 default:
1434 pr_err("%s: Unhandled return value (%d)",
1435 req->rq_disk->disk_name, status);
1436 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001437 }
1438
Per Forlinee8a43a2011-07-01 18:55:33 +02001439 if (ret) {
1440 /*
Adrian Hunter67716322011-08-29 16:42:15 +03001441 * In case of a incomplete request
Per Forlinee8a43a2011-07-01 18:55:33 +02001442 * prepare it again and resend.
1443 */
1444 mmc_blk_rw_rq_prep(mq_rq, card, disable_multi, mq);
1445 mmc_start_req(card->host, &mq_rq->mmc_active, NULL);
1446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 } while (ret);
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return 1;
1450
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001451 cmd_abort:
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301452 if (mmc_card_removed(card))
1453 req->cmd_flags |= REQ_QUIET;
Kiyoshi Uedafd539832007-12-11 17:48:29 -05001454 while (ret)
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301455 ret = blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Per Forlinee8a43a2011-07-01 18:55:33 +02001457 start_new_req:
1458 if (rqc) {
1459 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1460 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1461 }
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return 0;
1464}
1465
Adrian Hunterbd788c92010-08-11 14:17:47 -07001466static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1467{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001468 int ret;
1469 struct mmc_blk_data *md = mq->data;
1470 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001471 struct mmc_host *host = card->host;
1472 unsigned long flags;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001473
Per Forlinee8a43a2011-07-01 18:55:33 +02001474 if (req && !mq->mqrq_prev->req)
1475 /* claim host only for the first request */
1476 mmc_claim_host(card->host);
1477
Andrei Warkentin371a6892011-04-11 18:10:25 -05001478 ret = mmc_blk_part_switch(card, md);
1479 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001480 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301481 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001482 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001483 ret = 0;
1484 goto out;
1485 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001486
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001487 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02001488 if (req && req->cmd_flags & REQ_DISCARD) {
1489 /* complete ongoing async transfer before issuing discard */
1490 if (card->host->areq)
1491 mmc_blk_issue_rw_rq(mq, NULL);
Ian Chen3550ccd2012-08-29 15:05:36 +09001492 if (req->cmd_flags & REQ_SECURE &&
1493 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001494 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001495 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001496 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02001497 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001498 /* complete ongoing async transfer before issuing flush */
1499 if (card->host->areq)
1500 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001501 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001502 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001503 if (!req && host->areq) {
1504 spin_lock_irqsave(&host->context_info.lock, flags);
1505 host->context_info.is_waiting_last_req = true;
1506 spin_unlock_irqrestore(&host->context_info.lock, flags);
1507 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001508 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001509 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001510
Andrei Warkentin371a6892011-04-11 18:10:25 -05001511out:
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001512 if (!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST))
Per Forlinee8a43a2011-07-01 18:55:33 +02001513 /* release host only when there are no more requests */
1514 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001515 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001516}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Russell Kinga6f6c962006-01-03 22:38:44 +00001518static inline int mmc_blk_readonly(struct mmc_card *card)
1519{
1520 return mmc_card_readonly(card) ||
1521 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1522}
1523
Andrei Warkentin371a6892011-04-11 18:10:25 -05001524static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1525 struct device *parent,
1526 sector_t size,
1527 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001528 const char *subname,
1529 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 struct mmc_blk_data *md;
1532 int devidx, ret;
1533
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001534 devidx = find_first_zero_bit(dev_use, max_devices);
1535 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return ERR_PTR(-ENOSPC);
1537 __set_bit(devidx, dev_use);
1538
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001539 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001540 if (!md) {
1541 ret = -ENOMEM;
1542 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001544
Russell Kinga6f6c962006-01-03 22:38:44 +00001545 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001546 * !subname implies we are creating main mmc_blk_data that will be
1547 * associated with mmc_card with mmc_set_drvdata. Due to device
1548 * partitions, devidx will not coincide with a per-physical card
1549 * index anymore so we keep track of a name index.
1550 */
1551 if (!subname) {
1552 md->name_idx = find_first_zero_bit(name_use, max_devices);
1553 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001554 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001555 md->name_idx = ((struct mmc_blk_data *)
1556 dev_to_disk(parent)->private_data)->name_idx;
1557
Johan Rudholmadd710e2011-12-02 08:51:06 +01001558 md->area_type = area_type;
1559
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001560 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001561 * Set the read-only status based on the supported commands
1562 * and the write protect switch.
1563 */
1564 md->read_only = mmc_blk_readonly(card);
1565
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001566 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001567 if (md->disk == NULL) {
1568 ret = -ENOMEM;
1569 goto err_kfree;
1570 }
1571
1572 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001573 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001574 md->usage = 1;
1575
Adrian Hunterd09408a2011-06-23 13:40:28 +03001576 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001577 if (ret)
1578 goto err_putdisk;
1579
Russell Kinga6f6c962006-01-03 22:38:44 +00001580 md->queue.issue_fn = mmc_blk_issue_rq;
1581 md->queue.data = md;
1582
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001583 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001584 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001585 md->disk->fops = &mmc_bdops;
1586 md->disk->private_data = md;
1587 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001588 md->disk->driverfs_dev = parent;
1589 set_disk_ro(md->disk, md->read_only || default_ro);
Loic Pallardy53d8f972012-08-06 17:12:28 +02001590 if (area_type & MMC_BLK_DATA_AREA_RPMB)
1591 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00001592
1593 /*
1594 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1595 *
1596 * - be set for removable media with permanent block devices
1597 * - be unset for removable block devices with permanent media
1598 *
1599 * Since MMC block devices clearly fall under the second
1600 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1601 * should use the block device creation/destruction hotplug
1602 * messages to tell when the card is present.
1603 */
1604
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001605 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1606 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001607
Saugata Dasa5075eb2012-05-17 16:32:21 +05301608 if (mmc_card_mmc(card))
1609 blk_queue_logical_block_size(md->queue.queue,
1610 card->ext_csd.data_sector_size);
1611 else
1612 blk_queue_logical_block_size(md->queue.queue, 512);
1613
Andrei Warkentin371a6892011-04-11 18:10:25 -05001614 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001615
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001616 if (mmc_host_cmd23(card->host)) {
1617 if (mmc_card_mmc(card) ||
1618 (mmc_card_sd(card) &&
1619 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1620 md->flags |= MMC_BLK_CMD23;
1621 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001622
1623 if (mmc_card_mmc(card) &&
1624 md->flags & MMC_BLK_CMD23 &&
1625 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1626 card->ext_csd.rel_sectors)) {
1627 md->flags |= MMC_BLK_REL_WR;
1628 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1629 }
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001632
1633 err_putdisk:
1634 put_disk(md->disk);
1635 err_kfree:
1636 kfree(md);
1637 out:
1638 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
Andrei Warkentin371a6892011-04-11 18:10:25 -05001641static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1642{
1643 sector_t size;
1644 struct mmc_blk_data *md;
1645
1646 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1647 /*
1648 * The EXT_CSD sector count is in number or 512 byte
1649 * sectors.
1650 */
1651 size = card->ext_csd.sectors;
1652 } else {
1653 /*
1654 * The CSD capacity field is in units of read_blkbits.
1655 * set_capacity takes units of 512 bytes.
1656 */
1657 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1658 }
1659
Johan Rudholmadd710e2011-12-02 08:51:06 +01001660 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
1661 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001662 return md;
1663}
1664
1665static int mmc_blk_alloc_part(struct mmc_card *card,
1666 struct mmc_blk_data *md,
1667 unsigned int part_type,
1668 sector_t size,
1669 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001670 const char *subname,
1671 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05001672{
1673 char cap_str[10];
1674 struct mmc_blk_data *part_md;
1675
1676 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001677 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001678 if (IS_ERR(part_md))
1679 return PTR_ERR(part_md);
1680 part_md->part_type = part_type;
1681 list_add(&part_md->part, &md->part);
1682
1683 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1684 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301685 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05001686 part_md->disk->disk_name, mmc_card_id(card),
1687 mmc_card_name(card), part_md->part_type, cap_str);
1688 return 0;
1689}
1690
Namjae Jeone0c368d2011-10-06 23:41:38 +09001691/* MMC Physical partitions consist of two boot partitions and
1692 * up to four general purpose partitions.
1693 * For each partition enabled in EXT_CSD a block device will be allocatedi
1694 * to provide access to the partition.
1695 */
1696
Andrei Warkentin371a6892011-04-11 18:10:25 -05001697static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1698{
Namjae Jeone0c368d2011-10-06 23:41:38 +09001699 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001700
1701 if (!mmc_card_mmc(card))
1702 return 0;
1703
Namjae Jeone0c368d2011-10-06 23:41:38 +09001704 for (idx = 0; idx < card->nr_parts; idx++) {
1705 if (card->part[idx].size) {
1706 ret = mmc_blk_alloc_part(card, md,
1707 card->part[idx].part_cfg,
1708 card->part[idx].size >> 9,
1709 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001710 card->part[idx].name,
1711 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09001712 if (ret)
1713 return ret;
1714 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001715 }
1716
1717 return ret;
1718}
1719
Andrei Warkentin371a6892011-04-11 18:10:25 -05001720static void mmc_blk_remove_req(struct mmc_blk_data *md)
1721{
Johan Rudholmadd710e2011-12-02 08:51:06 +01001722 struct mmc_card *card;
1723
Andrei Warkentin371a6892011-04-11 18:10:25 -05001724 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01001725 card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001726 if (md->disk->flags & GENHD_FL_UP) {
1727 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001728 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
1729 card->ext_csd.boot_ro_lockable)
1730 device_remove_file(disk_to_dev(md->disk),
1731 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001732
1733 /* Stop new requests from getting into the queue */
1734 del_gendisk(md->disk);
1735 }
1736
1737 /* Then flush out any already in there */
1738 mmc_cleanup_queue(&md->queue);
1739 mmc_blk_put(md);
1740 }
1741}
1742
1743static void mmc_blk_remove_parts(struct mmc_card *card,
1744 struct mmc_blk_data *md)
1745{
1746 struct list_head *pos, *q;
1747 struct mmc_blk_data *part_md;
1748
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001749 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001750 list_for_each_safe(pos, q, &md->part) {
1751 part_md = list_entry(pos, struct mmc_blk_data, part);
1752 list_del(pos);
1753 mmc_blk_remove_req(part_md);
1754 }
1755}
1756
1757static int mmc_add_disk(struct mmc_blk_data *md)
1758{
1759 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01001760 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001761
1762 add_disk(md->disk);
1763 md->force_ro.show = force_ro_show;
1764 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05301765 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001766 md->force_ro.attr.name = "force_ro";
1767 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1768 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1769 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01001770 goto force_ro_fail;
1771
1772 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
1773 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04001774 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01001775
1776 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
1777 mode = S_IRUGO;
1778 else
1779 mode = S_IRUGO | S_IWUSR;
1780
1781 md->power_ro_lock.show = power_ro_lock_show;
1782 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01001783 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001784 md->power_ro_lock.attr.mode = mode;
1785 md->power_ro_lock.attr.name =
1786 "ro_lock_until_next_power_on";
1787 ret = device_create_file(disk_to_dev(md->disk),
1788 &md->power_ro_lock);
1789 if (ret)
1790 goto power_ro_lock_fail;
1791 }
1792 return ret;
1793
1794power_ro_lock_fail:
1795 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1796force_ro_fail:
1797 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001798
1799 return ret;
1800}
1801
Chris Ballc59d4472011-11-11 22:01:43 -05001802#define CID_MANFID_SANDISK 0x2
1803#define CID_MANFID_TOSHIBA 0x11
1804#define CID_MANFID_MICRON 0x13
Ian Chen3550ccd2012-08-29 15:05:36 +09001805#define CID_MANFID_SAMSUNG 0x15
Chris Ballc59d4472011-11-11 22:01:43 -05001806
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001807static const struct mmc_fixup blk_fixups[] =
1808{
Chris Ballc59d4472011-11-11 22:01:43 -05001809 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
1810 MMC_QUIRK_INAND_CMD38),
1811 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
1812 MMC_QUIRK_INAND_CMD38),
1813 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
1814 MMC_QUIRK_INAND_CMD38),
1815 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
1816 MMC_QUIRK_INAND_CMD38),
1817 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
1818 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001819
1820 /*
1821 * Some MMC cards experience performance degradation with CMD23
1822 * instead of CMD12-bounded multiblock transfers. For now we'll
1823 * black list what's bad...
1824 * - Certain Toshiba cards.
1825 *
1826 * N.B. This doesn't affect SD cards.
1827 */
Chris Ballc59d4472011-11-11 22:01:43 -05001828 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001829 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05001830 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001831 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05001832 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001833 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01001834
1835 /*
1836 * Some Micron MMC cards needs longer data read timeout than
1837 * indicated in CSD.
1838 */
Chris Ballc59d4472011-11-11 22:01:43 -05001839 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01001840 MMC_QUIRK_LONG_READ_TIME),
1841
Ian Chen3550ccd2012-08-29 15:05:36 +09001842 /*
1843 * On these Samsung MoviNAND parts, performing secure erase or
1844 * secure trim can result in unrecoverable corruption due to a
1845 * firmware bug.
1846 */
1847 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1848 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1849 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1850 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1851 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1852 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1853 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1854 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1855 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1856 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1857 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1858 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1859 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1860 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1861 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1862 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1863
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001864 END_FIXUP
1865};
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867static int mmc_blk_probe(struct mmc_card *card)
1868{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001869 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001870 char cap_str[10];
1871
Pierre Ossman912490d2005-05-21 10:27:02 +01001872 /*
1873 * Check that the card supports the command class(es) we need.
1874 */
1875 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return -ENODEV;
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 md = mmc_blk_alloc(card);
1879 if (IS_ERR(md))
1880 return PTR_ERR(md);
1881
Yi Li444122f2009-02-05 15:31:57 +08001882 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001883 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301884 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001886 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Andrei Warkentin371a6892011-04-11 18:10:25 -05001888 if (mmc_blk_alloc_parts(card, md))
1889 goto out;
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001892 mmc_fixup_device(card, blk_fixups);
1893
Andrei Warkentin371a6892011-04-11 18:10:25 -05001894 if (mmc_add_disk(md))
1895 goto out;
1896
1897 list_for_each_entry(part_md, &md->part, part) {
1898 if (mmc_add_disk(part_md))
1899 goto out;
1900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return 0;
1902
1903 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05001904 mmc_blk_remove_parts(card, md);
1905 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01001906 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
1909static void mmc_blk_remove(struct mmc_card *card)
1910{
1911 struct mmc_blk_data *md = mmc_get_drvdata(card);
1912
Andrei Warkentin371a6892011-04-11 18:10:25 -05001913 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03001914 mmc_claim_host(card->host);
1915 mmc_blk_part_switch(card, md);
1916 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001917 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 mmc_set_drvdata(card, NULL);
1919}
1920
1921#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08001922static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001924 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 struct mmc_blk_data *md = mmc_get_drvdata(card);
1926
1927 if (md) {
1928 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001929 list_for_each_entry(part_md, &md->part, part) {
1930 mmc_queue_suspend(&part_md->queue);
1931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933 return 0;
1934}
1935
1936static int mmc_blk_resume(struct mmc_card *card)
1937{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001938 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 struct mmc_blk_data *md = mmc_get_drvdata(card);
1940
1941 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05001942 /*
1943 * Resume involves the card going into idle state,
1944 * so current partition is always the main one.
1945 */
1946 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001948 list_for_each_entry(part_md, &md->part, part) {
1949 mmc_queue_resume(&part_md->queue);
1950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952 return 0;
1953}
1954#else
1955#define mmc_blk_suspend NULL
1956#define mmc_blk_resume NULL
1957#endif
1958
1959static struct mmc_driver mmc_driver = {
1960 .drv = {
1961 .name = "mmcblk",
1962 },
1963 .probe = mmc_blk_probe,
1964 .remove = mmc_blk_remove,
1965 .suspend = mmc_blk_suspend,
1966 .resume = mmc_blk_resume,
1967};
1968
1969static int __init mmc_blk_init(void)
1970{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001971 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001973 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1974 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1975
1976 max_devices = 256 / perdev_minors;
1977
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001978 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1979 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001982 res = mmc_register_driver(&mmc_driver);
1983 if (res)
1984 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001986 return 0;
1987 out2:
1988 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 out:
1990 return res;
1991}
1992
1993static void __exit mmc_blk_exit(void)
1994{
1995 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001996 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
1999module_init(mmc_blk_init);
2000module_exit(mmc_blk_exit);
2001
2002MODULE_LICENSE("GPL");
2003MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2004