blob: 63d2fa16a05a3e48f04aa3831930581ffc13458d [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 Ossman385e3222006-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
60
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +030061#define MMC_SANITIZE_REQ_TIMEOUT 240000 /* msec */
62
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020063static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040064
65/*
66 * The defaults come from config options but can be overriden by module
67 * or bootarg options.
68 */
69static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
70
71/*
72 * We've only got one major, so number of mmcblk devices is
73 * limited to 256 / number of minors per device.
74 */
75static int max_devices;
76
77/* 256 minors, so at most 256 separate devices */
78static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050079static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/*
82 * There is one mmc_blk_data per slot.
83 */
84struct mmc_blk_data {
85 spinlock_t lock;
86 struct gendisk *disk;
87 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050088 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Andrei Warkentind0c97cf2011-05-23 15:06:36 -050090 unsigned int flags;
91#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
92#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +000095 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -050096 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -050097 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +030098 unsigned int reset_done;
99#define MMC_BLK_READ BIT(0)
100#define MMC_BLK_WRITE BIT(1)
101#define MMC_BLK_DISCARD BIT(2)
102#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500103
104 /*
105 * Only set in main mmc_blk_data associated
106 * with mmc_card with mmc_set_drvdata, and keeps
107 * track of the current selected device partition.
108 */
109 unsigned int part_curr;
110 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100111 struct device_attribute power_ro_lock;
112 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
Arjan van de Vena621aae2006-01-12 18:43:35 +0000115static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Maya Erezb52c7182012-11-28 23:46:50 +0200117enum mmc_blk_status {
118 MMC_BLK_SUCCESS = 0,
119 MMC_BLK_PARTIAL,
120 MMC_BLK_CMD_ERR,
121 MMC_BLK_RETRY,
122 MMC_BLK_ABORT,
123 MMC_BLK_DATA_ERR,
124 MMC_BLK_ECC_ERR,
125 MMC_BLK_NOMEDIUM,
Seungwon Jeon968c7742012-05-31 11:54:47 +0300126};
127
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400128module_param(perdev_minors, int, 0444);
129MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
132{
133 struct mmc_blk_data *md;
134
Arjan van de Vena621aae2006-01-12 18:43:35 +0000135 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 md = disk->private_data;
137 if (md && md->usage == 0)
138 md = NULL;
139 if (md)
140 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000141 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 return md;
144}
145
Andrei Warkentin371a6892011-04-11 18:10:25 -0500146static inline int mmc_get_devidx(struct gendisk *disk)
147{
Colin Cross71b54d42010-09-03 12:41:21 -0700148 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500149 return devidx;
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static void mmc_blk_put(struct mmc_blk_data *md)
153{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000154 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 md->usage--;
156 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500157 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800158 blk_cleanup_queue(md->queue.queue);
159
David Woodhouse1dff3142007-11-21 18:45:12 +0100160 __clear_bit(devidx, dev_use);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 kfree(md);
164 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000165 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Johan Rudholmadd710e2011-12-02 08:51:06 +0100168static ssize_t power_ro_lock_show(struct device *dev,
169 struct device_attribute *attr, char *buf)
170{
171 int ret;
172 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
173 struct mmc_card *card = md->queue.card;
174 int locked = 0;
175
176 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
177 locked = 2;
178 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
179 locked = 1;
180
181 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
182
183 return ret;
184}
185
186static ssize_t power_ro_lock_store(struct device *dev,
187 struct device_attribute *attr, const char *buf, size_t count)
188{
189 int ret;
190 struct mmc_blk_data *md, *part_md;
191 struct mmc_card *card;
192 unsigned long set;
193
194 if (kstrtoul(buf, 0, &set))
195 return -EINVAL;
196
197 if (set != 1)
198 return count;
199
200 md = mmc_blk_get(dev_to_disk(dev));
201 card = md->queue.card;
202
203 mmc_claim_host(card->host);
204
205 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
206 card->ext_csd.boot_ro_lock |
207 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
208 card->ext_csd.part_time);
209 if (ret)
210 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
211 else
212 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
213
214 mmc_release_host(card->host);
215
216 if (!ret) {
217 pr_info("%s: Locking boot partition ro until next power on\n",
218 md->disk->disk_name);
219 set_disk_ro(md->disk, 1);
220
221 list_for_each_entry(part_md, &md->part, part)
222 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
223 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
224 set_disk_ro(part_md->disk, 1);
225 }
226 }
227
228 mmc_blk_put(md);
229 return count;
230}
231
Andrei Warkentin371a6892011-04-11 18:10:25 -0500232static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
233 char *buf)
234{
235 int ret;
236 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
237
238 ret = snprintf(buf, PAGE_SIZE, "%d",
239 get_disk_ro(dev_to_disk(dev)) ^
240 md->read_only);
241 mmc_blk_put(md);
242 return ret;
243}
244
245static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
246 const char *buf, size_t count)
247{
248 int ret;
249 char *end;
250 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
251 unsigned long set = simple_strtoul(buf, &end, 0);
252 if (end == buf) {
253 ret = -EINVAL;
254 goto out;
255 }
256
257 set_disk_ro(dev_to_disk(dev), set || md->read_only);
258 ret = count;
259out:
260 mmc_blk_put(md);
261 return ret;
262}
263
Al Viroa5a15612008-03-02 10:33:30 -0500264static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Al Viroa5a15612008-03-02 10:33:30 -0500266 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 int ret = -ENXIO;
268
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200269 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (md) {
271 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500272 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700274
Al Viroa5a15612008-03-02 10:33:30 -0500275 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700276 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700277 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200280 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 return ret;
283}
284
Al Viroa5a15612008-03-02 10:33:30 -0500285static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Al Viroa5a15612008-03-02 10:33:30 -0500287 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200289 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200291 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return 0;
293}
294
295static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800296mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800298 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
299 geo->heads = 4;
300 geo->sectors = 16;
301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
John Calixtocb87ea22011-04-26 18:56:29 -0400304struct mmc_blk_ioc_data {
305 struct mmc_ioc_cmd ic;
306 unsigned char *buf;
307 u64 buf_bytes;
308};
309
310static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
311 struct mmc_ioc_cmd __user *user)
312{
313 struct mmc_blk_ioc_data *idata;
314 int err;
315
316 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
317 if (!idata) {
318 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400319 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400320 }
321
322 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
323 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400324 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400325 }
326
327 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
328 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
329 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400330 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400331 }
332
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100333 if (!idata->buf_bytes)
334 return idata;
335
John Calixtocb87ea22011-04-26 18:56:29 -0400336 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
337 if (!idata->buf) {
338 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400339 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400340 }
341
342 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
343 idata->ic.data_ptr, idata->buf_bytes)) {
344 err = -EFAULT;
345 goto copy_err;
346 }
347
348 return idata;
349
350copy_err:
351 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400352idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400353 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400354out:
John Calixtocb87ea22011-04-26 18:56:29 -0400355 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400356}
357
358static int mmc_blk_ioctl_cmd(struct block_device *bdev,
359 struct mmc_ioc_cmd __user *ic_ptr)
360{
361 struct mmc_blk_ioc_data *idata;
362 struct mmc_blk_data *md;
363 struct mmc_card *card;
364 struct mmc_command cmd = {0};
365 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530366 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400367 struct scatterlist sg;
368 int err;
369
370 /*
371 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
372 * whole block device, not on a partition. This prevents overspray
373 * between sibling partitions.
374 */
375 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
376 return -EPERM;
377
378 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
379 if (IS_ERR(idata))
380 return PTR_ERR(idata);
381
John Calixtocb87ea22011-04-26 18:56:29 -0400382 md = mmc_blk_get(bdev->bd_disk);
383 if (!md) {
384 err = -EINVAL;
385 goto cmd_done;
386 }
387
388 card = md->queue.card;
389 if (IS_ERR(card)) {
390 err = PTR_ERR(card);
391 goto cmd_done;
392 }
393
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100394 cmd.opcode = idata->ic.opcode;
395 cmd.arg = idata->ic.arg;
396 cmd.flags = idata->ic.flags;
397
398 if (idata->buf_bytes) {
399 data.sg = &sg;
400 data.sg_len = 1;
401 data.blksz = idata->ic.blksz;
402 data.blocks = idata->ic.blocks;
403
404 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
405
406 if (idata->ic.write_flag)
407 data.flags = MMC_DATA_WRITE;
408 else
409 data.flags = MMC_DATA_READ;
410
411 /* data.flags must already be set before doing this. */
412 mmc_set_data_timeout(&data, card);
413
414 /* Allow overriding the timeout_ns for empirical tuning. */
415 if (idata->ic.data_timeout_ns)
416 data.timeout_ns = idata->ic.data_timeout_ns;
417
418 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
419 /*
420 * Pretend this is a data transfer and rely on the
421 * host driver to compute timeout. When all host
422 * drivers support cmd.cmd_timeout for R1B, this
423 * can be changed to:
424 *
425 * mrq.data = NULL;
426 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
427 */
428 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
429 }
430
431 mrq.data = &data;
432 }
433
434 mrq.cmd = &cmd;
435
John Calixtocb87ea22011-04-26 18:56:29 -0400436 mmc_claim_host(card->host);
437
438 if (idata->ic.is_acmd) {
439 err = mmc_app_cmd(card->host, card);
440 if (err)
441 goto cmd_rel_host;
442 }
443
John Calixtocb87ea22011-04-26 18:56:29 -0400444 mmc_wait_for_req(card->host, &mrq);
445
446 if (cmd.error) {
447 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
448 __func__, cmd.error);
449 err = cmd.error;
450 goto cmd_rel_host;
451 }
452 if (data.error) {
453 dev_err(mmc_dev(card->host), "%s: data error %d\n",
454 __func__, data.error);
455 err = data.error;
456 goto cmd_rel_host;
457 }
458
459 /*
460 * According to the SD specs, some commands require a delay after
461 * issuing the command.
462 */
463 if (idata->ic.postsleep_min_us)
464 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
465
466 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
467 err = -EFAULT;
468 goto cmd_rel_host;
469 }
470
471 if (!idata->ic.write_flag) {
472 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
473 idata->buf, idata->buf_bytes)) {
474 err = -EFAULT;
475 goto cmd_rel_host;
476 }
477 }
478
479cmd_rel_host:
480 mmc_release_host(card->host);
481
482cmd_done:
483 mmc_blk_put(md);
484 kfree(idata->buf);
485 kfree(idata);
486 return err;
487}
488
489static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
490 unsigned int cmd, unsigned long arg)
491{
492 int ret = -EINVAL;
493 if (cmd == MMC_IOC_CMD)
494 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
495 return ret;
496}
497
498#ifdef CONFIG_COMPAT
499static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
500 unsigned int cmd, unsigned long arg)
501{
502 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
503}
504#endif
505
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700506static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500507 .open = mmc_blk_open,
508 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800509 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400511 .ioctl = mmc_blk_ioctl,
512#ifdef CONFIG_COMPAT
513 .compat_ioctl = mmc_blk_compat_ioctl,
514#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515};
516
Andrei Warkentin371a6892011-04-11 18:10:25 -0500517static inline int mmc_blk_part_switch(struct mmc_card *card,
518 struct mmc_blk_data *md)
519{
520 int ret;
521 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300522
Andrei Warkentin371a6892011-04-11 18:10:25 -0500523 if (main_md->part_curr == md->part_type)
524 return 0;
525
526 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300527 u8 part_config = card->ext_csd.part_config;
528
529 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
530 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500531
532 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300533 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500534 card->ext_csd.part_time);
535 if (ret)
536 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300537
538 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300539 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500540
541 main_md->part_curr = md->part_type;
542 return 0;
543}
544
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700545static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
546{
547 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100548 u32 result;
549 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700550
Venkatraman Sad5fd972011-08-25 00:30:50 +0530551 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400552 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400553 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700554
555 struct scatterlist sg;
556
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700557 cmd.opcode = MMC_APP_CMD;
558 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700559 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700560
561 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700562 if (err)
563 return (u32)-1;
564 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700565 return (u32)-1;
566
567 memset(&cmd, 0, sizeof(struct mmc_command));
568
569 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
570 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700571 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700572
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700573 data.blksz = 4;
574 data.blocks = 1;
575 data.flags = MMC_DATA_READ;
576 data.sg = &sg;
577 data.sg_len = 1;
Subhash Jadavani0126ae22012-06-12 14:46:06 +0530578 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700579
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700580 mrq.cmd = &cmd;
581 mrq.data = &data;
582
Ben Dooks051913d2009-06-08 23:33:57 +0100583 blocks = kmalloc(4, GFP_KERNEL);
584 if (!blocks)
585 return (u32)-1;
586
587 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700588
589 mmc_wait_for_req(card->host, &mrq);
590
Ben Dooks051913d2009-06-08 23:33:57 +0100591 result = ntohl(*blocks);
592 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700593
Ben Dooks051913d2009-06-08 23:33:57 +0100594 if (cmd.error || data.error)
595 result = (u32)-1;
596
597 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700598}
599
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100600static int send_stop(struct mmc_card *card, u32 *status)
601{
602 struct mmc_command cmd = {0};
603 int err;
604
605 cmd.opcode = MMC_STOP_TRANSMISSION;
606 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
607 err = mmc_wait_for_cmd(card->host, &cmd, 5);
608 if (err == 0)
609 *status = cmd.resp[0];
610 return err;
611}
612
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100613static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300614{
Chris Ball1278dba2011-04-13 23:40:30 -0400615 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300616 int err;
617
Adrian Hunter504f1912008-10-16 12:55:25 +0300618 cmd.opcode = MMC_SEND_STATUS;
619 if (!mmc_host_is_spi(card->host))
620 cmd.arg = card->rca << 16;
621 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100622 err = mmc_wait_for_cmd(card->host, &cmd, retries);
623 if (err == 0)
624 *status = cmd.resp[0];
625 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300626}
627
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530628#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100629#define ERR_RETRY 2
630#define ERR_ABORT 1
631#define ERR_CONTINUE 0
632
633static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
634 bool status_valid, u32 status)
635{
636 switch (error) {
637 case -EILSEQ:
638 /* response crc error, retry the r/w cmd */
639 pr_err("%s: %s sending %s command, card status %#x\n",
640 req->rq_disk->disk_name, "response CRC error",
641 name, status);
642 return ERR_RETRY;
643
644 case -ETIMEDOUT:
645 pr_err("%s: %s sending %s command, card status %#x\n",
646 req->rq_disk->disk_name, "timed out", name, status);
647
648 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700649 if (!status_valid) {
650 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100651 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700652 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100653 /*
654 * If it was a r/w cmd crc error, or illegal command
655 * (eg, issued in wrong state) then retry - we should
656 * have corrected the state problem above.
657 */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700658 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
659 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100660 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700661 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100662
663 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700664 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100665 return ERR_ABORT;
666
667 default:
668 /* We don't understand the error code the driver gave us */
669 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
670 req->rq_disk->disk_name, error, status);
671 return ERR_ABORT;
672 }
673}
674
675/*
676 * Initial r/w and stop cmd error recovery.
677 * We don't know whether the card received the r/w cmd or not, so try to
678 * restore things back to a sane state. Essentially, we do this as follows:
679 * - Obtain card status. If the first attempt to obtain card status fails,
680 * the status word will reflect the failed status cmd, not the failed
681 * r/w cmd. If we fail to obtain card status, it suggests we can no
682 * longer communicate with the card.
683 * - Check the card state. If the card received the cmd but there was a
684 * transient problem with the response, it might still be in a data transfer
685 * mode. Try to send it a stop command. If this fails, we can't recover.
686 * - If the r/w cmd failed due to a response CRC error, it was probably
687 * transient, so retry the cmd.
688 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
689 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
690 * illegal cmd, retry.
691 * Otherwise we don't understand what happened, so abort.
692 */
693static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +0300694 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100695{
696 bool prev_cmd_status_valid = true;
697 u32 status, stop_status = 0;
698 int err, retry;
699
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530700 if (mmc_card_removed(card))
701 return ERR_NOMEDIUM;
702
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100703 /*
704 * Try to get card status which indicates both the card state
705 * and why there was no response. If the first attempt fails,
706 * we can't be sure the returned status is for the r/w command.
707 */
708 for (retry = 2; retry >= 0; retry--) {
709 err = get_card_status(card, &status, 0);
710 if (!err)
711 break;
712
713 prev_cmd_status_valid = false;
714 pr_err("%s: error %d sending status command, %sing\n",
715 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
716 }
717
718 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530719 if (err) {
720 /* Check if the card is removed */
721 if (mmc_detect_card_removed(card->host))
722 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100723 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530724 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100725
Adrian Hunter67716322011-08-29 16:42:15 +0300726 /* Flag ECC errors */
727 if ((status & R1_CARD_ECC_FAILED) ||
728 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
729 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
730 *ecc_err = 1;
731
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100732 /*
733 * Check the current card state. If it is in some data transfer
734 * mode, tell it to stop (and hopefully transition back to TRAN.)
735 */
736 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
737 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
738 err = send_stop(card, &stop_status);
739 if (err)
740 pr_err("%s: error %d sending stop command\n",
741 req->rq_disk->disk_name, err);
742
743 /*
744 * If the stop cmd also timed out, the card is probably
745 * not present, so abort. Other errors are bad news too.
746 */
747 if (err)
748 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300749 if (stop_status & R1_CARD_ECC_FAILED)
750 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100751 }
752
753 /* Check for set block count errors */
754 if (brq->sbc.error)
755 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
756 prev_cmd_status_valid, status);
757
758 /* Check for r/w command errors */
759 if (brq->cmd.error)
760 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
761 prev_cmd_status_valid, status);
762
Adrian Hunter67716322011-08-29 16:42:15 +0300763 /* Data errors */
764 if (!brq->stop.error)
765 return ERR_CONTINUE;
766
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100767 /* Now for stop errors. These aren't fatal to the transfer. */
768 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
769 req->rq_disk->disk_name, brq->stop.error,
770 brq->cmd.resp[0], status);
771
772 /*
773 * Subsitute in our own stop status as this will give the error
774 * state which happened during the execution of the r/w command.
775 */
776 if (stop_status) {
777 brq->stop.resp[0] = stop_status;
778 brq->stop.error = 0;
779 }
780 return ERR_CONTINUE;
781}
782
Adrian Hunter67716322011-08-29 16:42:15 +0300783static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
784 int type)
785{
786 int err;
787
788 if (md->reset_done & type)
789 return -EEXIST;
790
791 md->reset_done |= type;
792 err = mmc_hw_reset(host);
793 /* Ensure we switch back to the correct partition */
794 if (err != -EOPNOTSUPP) {
795 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
796 int part_err;
797
798 main_md->part_curr = main_md->part_type;
799 part_err = mmc_blk_part_switch(host->card, md);
800 if (part_err) {
801 /*
802 * We have failed to get back into the correct
803 * partition, so we need to abort the whole request.
804 */
805 return -ENODEV;
806 }
807 }
808 return err;
809}
810
811static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
812{
813 md->reset_done &= ~type;
814}
815
Adrian Hunterbd788c92010-08-11 14:17:47 -0700816static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
817{
818 struct mmc_blk_data *md = mq->data;
819 struct mmc_card *card = md->queue.card;
820 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300821 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700822
Adrian Hunterbd788c92010-08-11 14:17:47 -0700823 if (!mmc_can_erase(card)) {
824 err = -EOPNOTSUPP;
825 goto out;
826 }
827
828 from = blk_rq_pos(req);
829 nr = blk_rq_sectors(req);
830
Kyungmin Parkb3bf9152011-10-18 09:34:04 +0900831 if (mmc_can_discard(card))
832 arg = MMC_DISCARD_ARG;
833 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -0700834 arg = MMC_TRIM_ARG;
835 else
836 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300837retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500838 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
839 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
840 INAND_CMD38_ARG_EXT_CSD,
841 arg == MMC_TRIM_ARG ?
842 INAND_CMD38_ARG_TRIM :
843 INAND_CMD38_ARG_ERASE,
844 0);
845 if (err)
846 goto out;
847 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700848 err = mmc_erase(card, from, nr, arg);
849out:
Adrian Hunter67716322011-08-29 16:42:15 +0300850 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
851 goto retry;
852 if (!err)
853 mmc_blk_reset_success(md, type);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530854 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -0700855
Adrian Hunterbd788c92010-08-11 14:17:47 -0700856 return err ? 0 : 1;
857}
858
Adrian Hunter49804542010-08-11 14:17:50 -0700859static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
860 struct request *req)
861{
862 struct mmc_blk_data *md = mq->data;
863 struct mmc_card *card = md->queue.card;
864 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300865 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700866
Maya Erez463bb952012-05-24 23:46:29 +0300867 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700868 err = -EOPNOTSUPP;
869 goto out;
870 }
871
872 from = blk_rq_pos(req);
873 nr = blk_rq_sectors(req);
874
875 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
876 arg = MMC_SECURE_TRIM1_ARG;
877 else
878 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300879retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500880 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
881 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
882 INAND_CMD38_ARG_EXT_CSD,
883 arg == MMC_SECURE_TRIM1_ARG ?
884 INAND_CMD38_ARG_SECTRIM1 :
885 INAND_CMD38_ARG_SECERASE,
886 0);
887 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300888 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500889 }
Adrian Hunter28302812012-04-05 14:45:48 +0300890
Adrian Hunter49804542010-08-11 14:17:50 -0700891 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +0300892 if (err == -EIO)
893 goto out_retry;
894 if (err)
895 goto out;
896
897 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500898 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
899 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
900 INAND_CMD38_ARG_EXT_CSD,
901 INAND_CMD38_ARG_SECTRIM2,
902 0);
903 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300904 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500905 }
Adrian Hunter28302812012-04-05 14:45:48 +0300906
Adrian Hunter49804542010-08-11 14:17:50 -0700907 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +0300908 if (err == -EIO)
909 goto out_retry;
910 if (err)
911 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500912 }
Adrian Hunter28302812012-04-05 14:45:48 +0300913
914 if (mmc_can_sanitize(card))
915 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
916 EXT_CSD_SANITIZE_START, 1, 0);
917out_retry:
918 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +0300919 goto retry;
920 if (!err)
921 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +0300922out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530923 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -0700924
Adrian Hunter49804542010-08-11 14:17:50 -0700925 return err ? 0 : 1;
926}
927
Maya Erez463bb952012-05-24 23:46:29 +0300928static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
929 struct request *req)
930{
931 struct mmc_blk_data *md = mq->data;
932 struct mmc_card *card = md->queue.card;
933 int err = 0;
934
935 BUG_ON(!card);
936 BUG_ON(!card->host);
937
938 if (!(mmc_can_sanitize(card) &&
939 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
940 pr_warning("%s: %s - SANITIZE is not supported\n",
941 mmc_hostname(card->host), __func__);
942 err = -EOPNOTSUPP;
943 goto out;
944 }
945
946 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
947 mmc_hostname(card->host), __func__);
948
949 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +0300950 EXT_CSD_SANITIZE_START, 1,
951 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +0300952
953 if (err)
954 pr_err("%s: %s - mmc_switch() with "
955 "EXT_CSD_SANITIZE_START failed. err=%d\n",
956 mmc_hostname(card->host), __func__, err);
957
958 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
959 __func__);
960
961out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530962 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +0300963
964 return err ? 0 : 1;
965}
966
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500967static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
968{
969 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900970 struct mmc_card *card = md->queue.card;
971 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500972
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900973 ret = mmc_flush_cache(card);
974 if (ret)
975 ret = -EIO;
976
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530977 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500978
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900979 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500980}
981
982/*
983 * Reformat current write as a reliable write, supporting
984 * both legacy and the enhanced reliable write MMC cards.
985 * In each transfer we'll handle only as much as a single
986 * reliable write can handle, thus finish the request in
987 * partial completions.
988 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500989static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
990 struct mmc_card *card,
991 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500992{
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500993 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
994 /* Legacy mode imposes restrictions on transfers. */
995 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
996 brq->data.blocks = 1;
997
998 if (brq->data.blocks > card->ext_csd.rel_sectors)
999 brq->data.blocks = card->ext_csd.rel_sectors;
1000 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1001 brq->data.blocks = 1;
1002 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001003}
1004
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001005#define CMD_ERRORS \
1006 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1007 R1_ADDRESS_ERROR | /* Misaligned address */ \
1008 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1009 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1010 R1_CC_ERROR | /* Card controller error */ \
1011 R1_ERROR) /* General/unknown error */
1012
Per Forlinee8a43a2011-07-01 18:55:33 +02001013static int mmc_blk_err_check(struct mmc_card *card,
1014 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001015{
Per Forlinee8a43a2011-07-01 18:55:33 +02001016 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1017 mmc_active);
1018 struct mmc_blk_request *brq = &mq_mrq->brq;
1019 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001020 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001021
1022 /*
1023 * sbc.error indicates a problem with the set block count
1024 * command. No data will have been transferred.
1025 *
1026 * cmd.error indicates a problem with the r/w command. No
1027 * data will have been transferred.
1028 *
1029 * stop.error indicates a problem with the stop command. Data
1030 * may have been transferred, or may still be transferring.
1031 */
Adrian Hunter67716322011-08-29 16:42:15 +03001032 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1033 brq->data.error) {
1034 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001035 case ERR_RETRY:
1036 return MMC_BLK_RETRY;
1037 case ERR_ABORT:
1038 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301039 case ERR_NOMEDIUM:
1040 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001041 case ERR_CONTINUE:
1042 break;
1043 }
1044 }
1045
1046 /*
1047 * Check for errors relating to the execution of the
1048 * initial command - such as address errors. No data
1049 * has been transferred.
1050 */
1051 if (brq->cmd.resp[0] & CMD_ERRORS) {
1052 pr_err("%s: r/w command failed, status = %#x\n",
1053 req->rq_disk->disk_name, brq->cmd.resp[0]);
1054 return MMC_BLK_ABORT;
1055 }
1056
1057 /*
1058 * Everything else is either success, or a data error of some
1059 * kind. If it was a write, we may have transitioned to
1060 * program mode, which we have to wait for it to complete.
1061 */
1062 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1063 u32 status;
1064 do {
1065 int err = get_card_status(card, &status, 5);
1066 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301067 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001068 req->rq_disk->disk_name, err);
1069 return MMC_BLK_CMD_ERR;
1070 }
1071 /*
1072 * Some cards mishandle the status bits,
1073 * so make sure to check both the busy
1074 * indication and the card state.
1075 */
1076 } while (!(status & R1_READY_FOR_DATA) ||
1077 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1078 }
1079
1080 if (brq->data.error) {
1081 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1082 req->rq_disk->disk_name, brq->data.error,
1083 (unsigned)blk_rq_pos(req),
1084 (unsigned)blk_rq_sectors(req),
1085 brq->cmd.resp[0], brq->stop.resp[0]);
1086
1087 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001088 if (ecc_err)
1089 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001090 return MMC_BLK_DATA_ERR;
1091 } else {
1092 return MMC_BLK_CMD_ERR;
1093 }
1094 }
1095
Adrian Hunter67716322011-08-29 16:42:15 +03001096 if (!brq->data.bytes_xfered)
1097 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001098
Adrian Hunter67716322011-08-29 16:42:15 +03001099 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1100 return MMC_BLK_PARTIAL;
1101
1102 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001103}
1104
Per Forlin54d49d772011-07-01 18:55:29 +02001105static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1106 struct mmc_card *card,
1107 int disable_multi,
1108 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Per Forlin54d49d772011-07-01 18:55:29 +02001110 u32 readcmd, writecmd;
1111 struct mmc_blk_request *brq = &mqrq->brq;
1112 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301114 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001116 /*
1117 * Reliable writes are used to implement Forced Unit Access and
1118 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001119 *
1120 * XXX: this really needs a good explanation of why REQ_META
1121 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001122 */
1123 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1124 (req->cmd_flags & REQ_META)) &&
1125 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001126 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001127
Per Forlin54d49d772011-07-01 18:55:29 +02001128 memset(brq, 0, sizeof(struct mmc_blk_request));
1129 brq->mrq.cmd = &brq->cmd;
1130 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Per Forlin54d49d772011-07-01 18:55:29 +02001132 brq->cmd.arg = blk_rq_pos(req);
1133 if (!mmc_card_blockaddr(card))
1134 brq->cmd.arg <<= 9;
1135 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1136 brq->data.blksz = 512;
1137 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1138 brq->stop.arg = 0;
1139 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1140 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Asutosh Das1a897142012-07-27 18:10:19 +05301142 brq->data.fault_injected = false;
Per Forlin54d49d772011-07-01 18:55:29 +02001143 /*
1144 * The block layer doesn't support all sector count
1145 * restrictions, so we need to be prepared for too big
1146 * requests.
1147 */
1148 if (brq->data.blocks > card->host->max_blk_count)
1149 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001151 if (brq->data.blocks > 1) {
1152 /*
1153 * After a read error, we redo the request one sector
1154 * at a time in order to accurately determine which
1155 * sectors can be read successfully.
1156 */
1157 if (disable_multi)
1158 brq->data.blocks = 1;
1159
1160 /* Some controllers can't do multiblock reads due to hw bugs */
1161 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1162 rq_data_dir(req) == READ)
1163 brq->data.blocks = 1;
1164 }
Per Forlin54d49d772011-07-01 18:55:29 +02001165
1166 if (brq->data.blocks > 1 || do_rel_wr) {
1167 /* SPI multiblock writes terminate using a special
1168 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001169 */
Per Forlin54d49d772011-07-01 18:55:29 +02001170 if (!mmc_host_is_spi(card->host) ||
1171 rq_data_dir(req) == READ)
1172 brq->mrq.stop = &brq->stop;
1173 readcmd = MMC_READ_MULTIPLE_BLOCK;
1174 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1175 } else {
1176 brq->mrq.stop = NULL;
1177 readcmd = MMC_READ_SINGLE_BLOCK;
1178 writecmd = MMC_WRITE_BLOCK;
1179 }
1180 if (rq_data_dir(req) == READ) {
1181 brq->cmd.opcode = readcmd;
1182 brq->data.flags |= MMC_DATA_READ;
1183 } else {
1184 brq->cmd.opcode = writecmd;
1185 brq->data.flags |= MMC_DATA_WRITE;
1186 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001187
Per Forlin54d49d772011-07-01 18:55:29 +02001188 if (do_rel_wr)
1189 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001190
Per Forlin54d49d772011-07-01 18:55:29 +02001191 /*
Saugata Das42659002011-12-21 13:09:17 +05301192 * Data tag is used only during writing meta data to speed
1193 * up write and any subsequent read of this meta data
1194 */
1195 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1196 (req->cmd_flags & REQ_META) &&
1197 (rq_data_dir(req) == WRITE) &&
1198 ((brq->data.blocks * brq->data.blksz) >=
1199 card->ext_csd.data_tag_unit_size);
1200
1201 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001202 * Pre-defined multi-block transfers are preferable to
1203 * open ended-ones (and necessary for reliable writes).
1204 * However, it is not sufficient to just send CMD23,
1205 * and avoid the final CMD12, as on an error condition
1206 * CMD12 (stop) needs to be sent anyway. This, coupled
1207 * with Auto-CMD23 enhancements provided by some
1208 * hosts, means that the complexity of dealing
1209 * with this is best left to the host. If CMD23 is
1210 * supported by card and host, we'll fill sbc in and let
1211 * the host deal with handling it correctly. This means
1212 * that for hosts that don't expose MMC_CAP_CMD23, no
1213 * change of behavior will be observed.
1214 *
1215 * N.B: Some MMC cards experience perf degradation.
1216 * We'll avoid using CMD23-bounded multiblock writes for
1217 * these, while retaining features like reliable writes.
1218 */
Saugata Das42659002011-12-21 13:09:17 +05301219 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1220 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1221 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001222 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1223 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301224 (do_rel_wr ? (1 << 31) : 0) |
1225 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001226 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1227 brq->mrq.sbc = &brq->sbc;
1228 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001229
Per Forlin54d49d772011-07-01 18:55:29 +02001230 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001231
Per Forlin54d49d772011-07-01 18:55:29 +02001232 brq->data.sg = mqrq->sg;
1233 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001234
Per Forlin54d49d772011-07-01 18:55:29 +02001235 /*
1236 * Adjust the sg list so it is the same size as the
1237 * request.
1238 */
1239 if (brq->data.blocks != blk_rq_sectors(req)) {
1240 int i, data_size = brq->data.blocks << 9;
1241 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001242
Per Forlin54d49d772011-07-01 18:55:29 +02001243 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1244 data_size -= sg->length;
1245 if (data_size <= 0) {
1246 sg->length += data_size;
1247 i++;
1248 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001249 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001250 }
Per Forlin54d49d772011-07-01 18:55:29 +02001251 brq->data.sg_len = i;
1252 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001253
Per Forlinee8a43a2011-07-01 18:55:33 +02001254 mqrq->mmc_active.mrq = &brq->mrq;
1255 mqrq->mmc_active.err_check = mmc_blk_err_check;
1256
Per Forlin54d49d772011-07-01 18:55:29 +02001257 mmc_queue_bounce_pre(mqrq);
1258}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Adrian Hunter67716322011-08-29 16:42:15 +03001260static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1261 struct mmc_blk_request *brq, struct request *req,
1262 int ret)
1263{
1264 /*
1265 * If this is an SD card and we're writing, we can first
1266 * mark the known good sectors as ok.
1267 *
1268 * If the card is not SD, we can still ok written sectors
1269 * as reported by the controller (which might be less than
1270 * the real number of written sectors, but never more).
1271 */
1272 if (mmc_card_sd(card)) {
1273 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05301274 if (!brq->data.fault_injected) {
1275 blocks = mmc_sd_num_wr_blocks(card);
1276 if (blocks != (u32)-1)
1277 ret = blk_end_request(req, 0, blocks << 9);
1278 } else
1279 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Maya Erezb52c7182012-11-28 23:46:50 +02001280 } else
1281 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon968c7742012-05-31 11:54:47 +03001282 return ret;
1283}
1284
Per Forlinee8a43a2011-07-01 18:55:33 +02001285static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001286{
1287 struct mmc_blk_data *md = mq->data;
1288 struct mmc_card *card = md->queue.card;
1289 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001290 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001291 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001292 struct mmc_queue_req *mq_rq;
Maya Erezb52c7182012-11-28 23:46:50 +02001293 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001294 struct mmc_async_req *areq;
1295
1296 if (!rqc && !mq->mqrq_prev->req)
1297 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001298
1299 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001300 if (rqc) {
Maya Erezb52c7182012-11-28 23:46:50 +02001301 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001302 areq = &mq->mqrq_cur->mmc_active;
1303 } else
1304 areq = NULL;
1305 areq = mmc_start_req(card->host, areq, (int *) &status);
1306 if (!areq)
1307 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001308
Per Forlinee8a43a2011-07-01 18:55:33 +02001309 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1310 brq = &mq_rq->brq;
1311 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001312 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001313 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001314
Per Forlind78d4a82011-07-01 18:55:30 +02001315 switch (status) {
1316 case MMC_BLK_SUCCESS:
1317 case MMC_BLK_PARTIAL:
1318 /*
1319 * A block was successfully transferred.
1320 */
Adrian Hunter67716322011-08-29 16:42:15 +03001321 mmc_blk_reset_success(md, type);
Maya Erezb52c7182012-11-28 23:46:50 +02001322 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001323 brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001324 /*
1325 * If the blk_end_request function returns non-zero even
1326 * though all data has been transferred and no errors
1327 * were returned by the host controller, it's a bug.
1328 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001329 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301330 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001331 __func__, blk_rq_bytes(req),
1332 brq->data.bytes_xfered);
1333 rqc = NULL;
1334 goto cmd_abort;
1335 }
Per Forlind78d4a82011-07-01 18:55:30 +02001336 break;
1337 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001338 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1339 if (!mmc_blk_reset(md, card->host, type))
1340 break;
1341 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001342 case MMC_BLK_RETRY:
1343 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001344 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001345 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001346 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001347 if (!mmc_blk_reset(md, card->host, type))
1348 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001349 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001350 case MMC_BLK_DATA_ERR: {
1351 int err;
1352
1353 err = mmc_blk_reset(md, card->host, type);
1354 if (!err)
1355 break;
Maya Erezb52c7182012-11-28 23:46:50 +02001356 if (err == -ENODEV)
Adrian Hunter67716322011-08-29 16:42:15 +03001357 goto cmd_abort;
1358 /* Fall through */
1359 }
1360 case MMC_BLK_ECC_ERR:
1361 if (brq->data.blocks > 1) {
1362 /* Redo read one sector at a time */
1363 pr_warning("%s: retrying using single block read\n",
1364 req->rq_disk->disk_name);
1365 disable_multi = 1;
1366 break;
1367 }
Per Forlind78d4a82011-07-01 18:55:30 +02001368 /*
1369 * After an error, we redo I/O one sector at a
1370 * time, so we only reach here after trying to
1371 * read a single sector.
1372 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301373 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001374 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001375 if (!ret)
1376 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001377 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301378 case MMC_BLK_NOMEDIUM:
1379 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001380 }
1381
Per Forlinee8a43a2011-07-01 18:55:33 +02001382 if (ret) {
Maya Erezb52c7182012-11-28 23:46:50 +02001383 /*
1384 * In case of a incomplete request
1385 * prepare it again and resend.
1386 */
1387 mmc_blk_rw_rq_prep(mq_rq, card, disable_multi, mq);
1388 mmc_start_req(card->host, &mq_rq->mmc_active, NULL);
Per Forlinee8a43a2011-07-01 18:55:33 +02001389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 } while (ret);
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return 1;
1393
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001394 cmd_abort:
Maya Erezb52c7182012-11-28 23:46:50 +02001395 if (mmc_card_removed(card))
1396 req->cmd_flags |= REQ_QUIET;
1397 while (ret)
1398 ret = blk_end_request(req, -EIO,
1399 blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Per Forlinee8a43a2011-07-01 18:55:33 +02001401 start_new_req:
1402 if (rqc) {
1403 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1404 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1405 }
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return 0;
1408}
1409
Adrian Hunterbd788c92010-08-11 14:17:47 -07001410static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1411{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001412 int ret;
1413 struct mmc_blk_data *md = mq->data;
1414 struct mmc_card *card = md->queue.card;
1415
San Mehat148c57a2009-07-30 08:21:19 -07001416#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1417 if (mmc_bus_needs_resume(card->host)) {
1418 mmc_resume_bus(card->host);
1419 mmc_blk_set_blksize(md, card);
1420 }
1421#endif
1422
Per Forlinee8a43a2011-07-01 18:55:33 +02001423 if (req && !mq->mqrq_prev->req)
1424 /* claim host only for the first request */
1425 mmc_claim_host(card->host);
1426
Andrei Warkentin371a6892011-04-11 18:10:25 -05001427 ret = mmc_blk_part_switch(card, md);
1428 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001429 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301430 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001431 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001432 ret = 0;
1433 goto out;
1434 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001435
Maya Erez463bb952012-05-24 23:46:29 +03001436 if (req && req->cmd_flags & REQ_SANITIZE) {
1437 /* complete ongoing async transfer before issuing sanitize */
1438 if (card->host && card->host->areq)
1439 mmc_blk_issue_rw_rq(mq, NULL);
1440 ret = mmc_blk_issue_sanitize_rq(mq, req);
1441 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001442 /* complete ongoing async transfer before issuing discard */
1443 if (card->host->areq)
1444 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter49804542010-08-11 14:17:50 -07001445 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001446 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001447 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001448 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02001449 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001450 /* complete ongoing async transfer before issuing flush */
1451 if (card->host->areq)
1452 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001453 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001454 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001455 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001456 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001457
Andrei Warkentin371a6892011-04-11 18:10:25 -05001458out:
Per Forlinee8a43a2011-07-01 18:55:33 +02001459 if (!req)
1460 /* release host only when there are no more requests */
1461 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001462 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001463}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Russell Kinga6f6c962006-01-03 22:38:44 +00001465static inline int mmc_blk_readonly(struct mmc_card *card)
1466{
1467 return mmc_card_readonly(card) ||
1468 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1469}
1470
Andrei Warkentin371a6892011-04-11 18:10:25 -05001471static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1472 struct device *parent,
1473 sector_t size,
1474 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001475 const char *subname,
1476 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 struct mmc_blk_data *md;
1479 int devidx, ret;
1480
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001481 devidx = find_first_zero_bit(dev_use, max_devices);
1482 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return ERR_PTR(-ENOSPC);
1484 __set_bit(devidx, dev_use);
1485
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001486 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001487 if (!md) {
1488 ret = -ENOMEM;
1489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001491
Russell Kinga6f6c962006-01-03 22:38:44 +00001492 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001493 * !subname implies we are creating main mmc_blk_data that will be
1494 * associated with mmc_card with mmc_set_drvdata. Due to device
1495 * partitions, devidx will not coincide with a per-physical card
1496 * index anymore so we keep track of a name index.
1497 */
1498 if (!subname) {
1499 md->name_idx = find_first_zero_bit(name_use, max_devices);
1500 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001501 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001502 md->name_idx = ((struct mmc_blk_data *)
1503 dev_to_disk(parent)->private_data)->name_idx;
1504
Johan Rudholmadd710e2011-12-02 08:51:06 +01001505 md->area_type = area_type;
1506
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001507 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001508 * Set the read-only status based on the supported commands
1509 * and the write protect switch.
1510 */
1511 md->read_only = mmc_blk_readonly(card);
1512
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001513 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001514 if (md->disk == NULL) {
1515 ret = -ENOMEM;
1516 goto err_kfree;
1517 }
1518
1519 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001520 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001521 md->usage = 1;
1522
Adrian Hunterd09408a2011-06-23 13:40:28 +03001523 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001524 if (ret)
1525 goto err_putdisk;
1526
Russell Kinga6f6c962006-01-03 22:38:44 +00001527 md->queue.issue_fn = mmc_blk_issue_rq;
1528 md->queue.data = md;
1529
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001530 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001531 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001532 md->disk->fops = &mmc_bdops;
1533 md->disk->private_data = md;
1534 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001535 md->disk->driverfs_dev = parent;
1536 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07001537 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00001538
1539 /*
1540 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1541 *
1542 * - be set for removable media with permanent block devices
1543 * - be unset for removable block devices with permanent media
1544 *
1545 * Since MMC block devices clearly fall under the second
1546 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1547 * should use the block device creation/destruction hotplug
1548 * messages to tell when the card is present.
1549 */
1550
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001551 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1552 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001553
Martin K. Petersene1defc42009-05-22 17:17:49 -04001554 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001555 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001556
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001557 if (mmc_host_cmd23(card->host)) {
1558 if (mmc_card_mmc(card) ||
1559 (mmc_card_sd(card) &&
1560 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1561 md->flags |= MMC_BLK_CMD23;
1562 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001563
1564 if (mmc_card_mmc(card) &&
1565 md->flags & MMC_BLK_CMD23 &&
1566 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1567 card->ext_csd.rel_sectors)) {
1568 md->flags |= MMC_BLK_REL_WR;
1569 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1570 }
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001573
1574 err_putdisk:
1575 put_disk(md->disk);
1576 err_kfree:
1577 kfree(md);
1578 out:
1579 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
Andrei Warkentin371a6892011-04-11 18:10:25 -05001582static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1583{
1584 sector_t size;
1585 struct mmc_blk_data *md;
1586
1587 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1588 /*
1589 * The EXT_CSD sector count is in number or 512 byte
1590 * sectors.
1591 */
1592 size = card->ext_csd.sectors;
1593 } else {
1594 /*
1595 * The CSD capacity field is in units of read_blkbits.
1596 * set_capacity takes units of 512 bytes.
1597 */
1598 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1599 }
1600
Johan Rudholmadd710e2011-12-02 08:51:06 +01001601 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
1602 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001603 return md;
1604}
1605
1606static int mmc_blk_alloc_part(struct mmc_card *card,
1607 struct mmc_blk_data *md,
1608 unsigned int part_type,
1609 sector_t size,
1610 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001611 const char *subname,
1612 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05001613{
1614 char cap_str[10];
1615 struct mmc_blk_data *part_md;
1616
1617 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001618 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001619 if (IS_ERR(part_md))
1620 return PTR_ERR(part_md);
1621 part_md->part_type = part_type;
1622 list_add(&part_md->part, &md->part);
1623
1624 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1625 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301626 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05001627 part_md->disk->disk_name, mmc_card_id(card),
1628 mmc_card_name(card), part_md->part_type, cap_str);
1629 return 0;
1630}
1631
Namjae Jeone0c368d2011-10-06 23:41:38 +09001632/* MMC Physical partitions consist of two boot partitions and
1633 * up to four general purpose partitions.
1634 * For each partition enabled in EXT_CSD a block device will be allocatedi
1635 * to provide access to the partition.
1636 */
1637
Andrei Warkentin371a6892011-04-11 18:10:25 -05001638static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1639{
Namjae Jeone0c368d2011-10-06 23:41:38 +09001640 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001641
1642 if (!mmc_card_mmc(card))
1643 return 0;
1644
Namjae Jeone0c368d2011-10-06 23:41:38 +09001645 for (idx = 0; idx < card->nr_parts; idx++) {
1646 if (card->part[idx].size) {
1647 ret = mmc_blk_alloc_part(card, md,
1648 card->part[idx].part_cfg,
1649 card->part[idx].size >> 9,
1650 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001651 card->part[idx].name,
1652 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09001653 if (ret)
1654 return ret;
1655 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001656 }
1657
1658 return ret;
1659}
1660
Andrei Warkentin371a6892011-04-11 18:10:25 -05001661static void mmc_blk_remove_req(struct mmc_blk_data *md)
1662{
Johan Rudholmadd710e2011-12-02 08:51:06 +01001663 struct mmc_card *card;
1664
Andrei Warkentin371a6892011-04-11 18:10:25 -05001665 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01001666 card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001667 if (md->disk->flags & GENHD_FL_UP) {
1668 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001669 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
1670 card->ext_csd.boot_ro_lockable)
1671 device_remove_file(disk_to_dev(md->disk),
1672 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001673
1674 /* Stop new requests from getting into the queue */
1675 del_gendisk(md->disk);
1676 }
1677
1678 /* Then flush out any already in there */
1679 mmc_cleanup_queue(&md->queue);
1680 mmc_blk_put(md);
1681 }
1682}
1683
1684static void mmc_blk_remove_parts(struct mmc_card *card,
1685 struct mmc_blk_data *md)
1686{
1687 struct list_head *pos, *q;
1688 struct mmc_blk_data *part_md;
1689
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001690 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001691 list_for_each_safe(pos, q, &md->part) {
1692 part_md = list_entry(pos, struct mmc_blk_data, part);
1693 list_del(pos);
1694 mmc_blk_remove_req(part_md);
1695 }
1696}
1697
1698static int mmc_add_disk(struct mmc_blk_data *md)
1699{
1700 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01001701 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001702
1703 add_disk(md->disk);
1704 md->force_ro.show = force_ro_show;
1705 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05301706 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001707 md->force_ro.attr.name = "force_ro";
1708 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1709 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1710 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01001711 goto force_ro_fail;
1712
1713 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
1714 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04001715 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01001716
1717 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
1718 mode = S_IRUGO;
1719 else
1720 mode = S_IRUGO | S_IWUSR;
1721
1722 md->power_ro_lock.show = power_ro_lock_show;
1723 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01001724 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001725 md->power_ro_lock.attr.mode = mode;
1726 md->power_ro_lock.attr.name =
1727 "ro_lock_until_next_power_on";
1728 ret = device_create_file(disk_to_dev(md->disk),
1729 &md->power_ro_lock);
1730 if (ret)
1731 goto power_ro_lock_fail;
1732 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001733
Steve Mucklef132c6c2012-06-06 18:30:57 -07001734 if (ret)
1735 goto power_ro_lock_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03001736
Johan Rudholmadd710e2011-12-02 08:51:06 +01001737 return ret;
1738
1739power_ro_lock_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001740 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001741force_ro_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001742 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001743
1744 return ret;
1745}
1746
Chris Ballc59d4472011-11-11 22:01:43 -05001747#define CID_MANFID_SANDISK 0x2
1748#define CID_MANFID_TOSHIBA 0x11
1749#define CID_MANFID_MICRON 0x13
1750
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001751static const struct mmc_fixup blk_fixups[] =
1752{
Chris Ballc59d4472011-11-11 22:01:43 -05001753 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
1754 MMC_QUIRK_INAND_CMD38),
1755 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
1756 MMC_QUIRK_INAND_CMD38),
1757 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
1758 MMC_QUIRK_INAND_CMD38),
1759 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
1760 MMC_QUIRK_INAND_CMD38),
1761 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
1762 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001763
1764 /*
1765 * Some MMC cards experience performance degradation with CMD23
1766 * instead of CMD12-bounded multiblock transfers. For now we'll
1767 * black list what's bad...
1768 * - Certain Toshiba cards.
1769 *
1770 * N.B. This doesn't affect SD cards.
1771 */
Chris Ballc59d4472011-11-11 22:01:43 -05001772 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001773 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05001774 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001775 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05001776 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001777 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01001778
1779 /*
1780 * Some Micron MMC cards needs longer data read timeout than
1781 * indicated in CSD.
1782 */
Chris Ballc59d4472011-11-11 22:01:43 -05001783 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01001784 MMC_QUIRK_LONG_READ_TIME),
1785
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05301786 /* Some INAND MCP devices advertise incorrect timeout values */
1787 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
1788 MMC_QUIRK_INAND_DATA_TIMEOUT),
1789
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001790 END_FIXUP
1791};
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793static int mmc_blk_probe(struct mmc_card *card)
1794{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001795 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001796 char cap_str[10];
1797
Pierre Ossman912490d2005-05-21 10:27:02 +01001798 /*
1799 * Check that the card supports the command class(es) we need.
1800 */
1801 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return -ENODEV;
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 md = mmc_blk_alloc(card);
1805 if (IS_ERR(md))
1806 return PTR_ERR(md);
1807
Yi Li444122f2009-02-05 15:31:57 +08001808 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001809 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301810 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02001812 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Andrei Warkentin371a6892011-04-11 18:10:25 -05001814 if (mmc_blk_alloc_parts(card, md))
1815 goto out;
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04001818 mmc_fixup_device(card, blk_fixups);
1819
San Mehat148c57a2009-07-30 08:21:19 -07001820#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1821 mmc_set_bus_resume_policy(card->host, 1);
1822#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05001823 if (mmc_add_disk(md))
1824 goto out;
1825
1826 list_for_each_entry(part_md, &md->part, part) {
1827 if (mmc_add_disk(part_md))
1828 goto out;
1829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return 0;
1831
1832 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05001833 mmc_blk_remove_parts(card, md);
1834 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01001835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
1838static void mmc_blk_remove(struct mmc_card *card)
1839{
1840 struct mmc_blk_data *md = mmc_get_drvdata(card);
1841
Andrei Warkentin371a6892011-04-11 18:10:25 -05001842 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03001843 mmc_claim_host(card->host);
1844 mmc_blk_part_switch(card, md);
1845 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001846 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07001848#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1849 mmc_set_bus_resume_policy(card->host, 0);
1850#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
1853#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08001854static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001856 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 struct mmc_blk_data *md = mmc_get_drvdata(card);
1858
1859 if (md) {
1860 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001861 list_for_each_entry(part_md, &md->part, part) {
1862 mmc_queue_suspend(&part_md->queue);
1863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865 return 0;
1866}
1867
1868static int mmc_blk_resume(struct mmc_card *card)
1869{
Andrei Warkentin371a6892011-04-11 18:10:25 -05001870 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 struct mmc_blk_data *md = mmc_get_drvdata(card);
1872
1873 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05001874 /*
1875 * Resume involves the card going into idle state,
1876 * so current partition is always the main one.
1877 */
1878 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001880 list_for_each_entry(part_md, &md->part, part) {
1881 mmc_queue_resume(&part_md->queue);
1882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
1884 return 0;
1885}
1886#else
1887#define mmc_blk_suspend NULL
1888#define mmc_blk_resume NULL
1889#endif
1890
1891static struct mmc_driver mmc_driver = {
1892 .drv = {
1893 .name = "mmcblk",
1894 },
1895 .probe = mmc_blk_probe,
1896 .remove = mmc_blk_remove,
1897 .suspend = mmc_blk_suspend,
1898 .resume = mmc_blk_resume,
1899};
1900
1901static int __init mmc_blk_init(void)
1902{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001903 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001905 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1906 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1907
1908 max_devices = 256 / perdev_minors;
1909
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001910 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1911 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001914 res = mmc_register_driver(&mmc_driver);
1915 if (res)
1916 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09001918 return 0;
1919 out2:
1920 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 out:
1922 return res;
1923}
1924
1925static void __exit mmc_blk_exit(void)
1926{
1927 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001928 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
1931module_init(mmc_blk_init);
1932module_exit(mmc_blk_exit);
1933
1934MODULE_LICENSE("GPL");
1935MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
1936