blob: 1ef088b0d3d9ca07b2c395a174c861c1818f0c7d [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
Seungwon Jeon53f8f572012-09-27 15:00:26 +020063#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
64 (req->cmd_flags & REQ_META)) && \
65 (rq_data_dir(req) == WRITE))
66#define PACKED_CMD_VER 0x01
67#define PACKED_CMD_WR 0x02
68
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020069static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040070
71/*
72 * The defaults come from config options but can be overriden by module
73 * or bootarg options.
74 */
75static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
76
77/*
78 * We've only got one major, so number of mmcblk devices is
79 * limited to 256 / number of minors per device.
80 */
81static int max_devices;
82
83/* 256 minors, so at most 256 separate devices */
84static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050085static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
88 * There is one mmc_blk_data per slot.
89 */
90struct mmc_blk_data {
91 spinlock_t lock;
92 struct gendisk *disk;
93 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050094 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Andrei Warkentind0c97cf2011-05-23 15:06:36 -050096 unsigned int flags;
97#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
98#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000101 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500102 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500103 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300104 unsigned int reset_done;
105#define MMC_BLK_READ BIT(0)
106#define MMC_BLK_WRITE BIT(1)
107#define MMC_BLK_DISCARD BIT(2)
108#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500109
110 /*
111 * Only set in main mmc_blk_data associated
112 * with mmc_card with mmc_set_drvdata, and keeps
113 * track of the current selected device partition.
114 */
115 unsigned int part_curr;
116 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100117 struct device_attribute power_ro_lock;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200118 struct device_attribute num_wr_reqs_to_start_packing;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100119 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
Arjan van de Vena621aae2006-01-12 18:43:35 +0000122static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Tatyana Brokhmanf94cf3d2012-09-27 11:00:02 +0200124enum mmc_blk_status {
125 MMC_BLK_SUCCESS = 0,
126 MMC_BLK_PARTIAL,
127 MMC_BLK_CMD_ERR,
128 MMC_BLK_RETRY,
129 MMC_BLK_ABORT,
130 MMC_BLK_DATA_ERR,
131 MMC_BLK_ECC_ERR,
132 MMC_BLK_NOMEDIUM,
Seungwon Jeon968c7742012-05-31 11:54:47 +0300133};
134
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200135enum {
136 MMC_PACKED_N_IDX = -1,
137 MMC_PACKED_N_ZERO,
138 MMC_PACKED_N_SINGLE,
139};
140
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400141module_param(perdev_minors, int, 0444);
142MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
143
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200144static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
145{
146 mqrq->packed_cmd = MMC_PACKED_NONE;
147 mqrq->packed_num = MMC_PACKED_N_ZERO;
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
151{
152 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 = disk->private_data;
156 if (md && md->usage == 0)
157 md = NULL;
158 if (md)
159 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000160 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 return md;
163}
164
Andrei Warkentin371a6892011-04-11 18:10:25 -0500165static inline int mmc_get_devidx(struct gendisk *disk)
166{
Colin Cross71b54d42010-09-03 12:41:21 -0700167 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500168 return devidx;
169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static void mmc_blk_put(struct mmc_blk_data *md)
172{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000173 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 md->usage--;
175 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500176 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800177 blk_cleanup_queue(md->queue.queue);
178
David Woodhouse1dff3142007-11-21 18:45:12 +0100179 __clear_bit(devidx, dev_use);
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 kfree(md);
183 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000184 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Johan Rudholmadd710e2011-12-02 08:51:06 +0100187static ssize_t power_ro_lock_show(struct device *dev,
188 struct device_attribute *attr, char *buf)
189{
190 int ret;
191 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
192 struct mmc_card *card = md->queue.card;
193 int locked = 0;
194
195 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
196 locked = 2;
197 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
198 locked = 1;
199
200 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
201
202 return ret;
203}
204
205static ssize_t power_ro_lock_store(struct device *dev,
206 struct device_attribute *attr, const char *buf, size_t count)
207{
208 int ret;
209 struct mmc_blk_data *md, *part_md;
210 struct mmc_card *card;
211 unsigned long set;
212
213 if (kstrtoul(buf, 0, &set))
214 return -EINVAL;
215
216 if (set != 1)
217 return count;
218
219 md = mmc_blk_get(dev_to_disk(dev));
220 card = md->queue.card;
221
222 mmc_claim_host(card->host);
223
224 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
225 card->ext_csd.boot_ro_lock |
226 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
227 card->ext_csd.part_time);
228 if (ret)
229 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
230 else
231 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
232
233 mmc_release_host(card->host);
234
235 if (!ret) {
236 pr_info("%s: Locking boot partition ro until next power on\n",
237 md->disk->disk_name);
238 set_disk_ro(md->disk, 1);
239
240 list_for_each_entry(part_md, &md->part, part)
241 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
242 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
243 set_disk_ro(part_md->disk, 1);
244 }
245 }
246
247 mmc_blk_put(md);
248 return count;
249}
250
Andrei Warkentin371a6892011-04-11 18:10:25 -0500251static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
252 char *buf)
253{
254 int ret;
255 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
256
257 ret = snprintf(buf, PAGE_SIZE, "%d",
258 get_disk_ro(dev_to_disk(dev)) ^
259 md->read_only);
260 mmc_blk_put(md);
261 return ret;
262}
263
264static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
265 const char *buf, size_t count)
266{
267 int ret;
268 char *end;
269 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
270 unsigned long set = simple_strtoul(buf, &end, 0);
271 if (end == buf) {
272 ret = -EINVAL;
273 goto out;
274 }
275
276 set_disk_ro(dev_to_disk(dev), set || md->read_only);
277 ret = count;
278out:
279 mmc_blk_put(md);
280 return ret;
281}
282
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200283static ssize_t
284num_wr_reqs_to_start_packing_show(struct device *dev,
285 struct device_attribute *attr, char *buf)
286{
287 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
288 int num_wr_reqs_to_start_packing;
289 int ret;
290
291 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
292
293 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
294
295 mmc_blk_put(md);
296 return ret;
297}
298
299static ssize_t
300num_wr_reqs_to_start_packing_store(struct device *dev,
301 struct device_attribute *attr,
302 const char *buf, size_t count)
303{
304 int value;
305 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
306
307 sscanf(buf, "%d", &value);
308 if (value >= 0)
309 md->queue.num_wr_reqs_to_start_packing = value;
310
311 mmc_blk_put(md);
312 return count;
313}
314
Al Viroa5a15612008-03-02 10:33:30 -0500315static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Al Viroa5a15612008-03-02 10:33:30 -0500317 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 int ret = -ENXIO;
319
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200320 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (md) {
322 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500323 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700325
Al Viroa5a15612008-03-02 10:33:30 -0500326 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700327 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700328 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200331 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 return ret;
334}
335
Al Viroa5a15612008-03-02 10:33:30 -0500336static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Al Viroa5a15612008-03-02 10:33:30 -0500338 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200340 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200342 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return 0;
344}
345
346static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800347mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800349 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
350 geo->heads = 4;
351 geo->sectors = 16;
352 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
John Calixtocb87ea22011-04-26 18:56:29 -0400355struct mmc_blk_ioc_data {
356 struct mmc_ioc_cmd ic;
357 unsigned char *buf;
358 u64 buf_bytes;
359};
360
361static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
362 struct mmc_ioc_cmd __user *user)
363{
364 struct mmc_blk_ioc_data *idata;
365 int err;
366
367 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
368 if (!idata) {
369 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400370 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400371 }
372
373 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
374 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400375 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400376 }
377
378 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
379 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
380 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400381 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400382 }
383
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100384 if (!idata->buf_bytes)
385 return idata;
386
John Calixtocb87ea22011-04-26 18:56:29 -0400387 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
388 if (!idata->buf) {
389 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400390 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400391 }
392
393 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
394 idata->ic.data_ptr, idata->buf_bytes)) {
395 err = -EFAULT;
396 goto copy_err;
397 }
398
399 return idata;
400
401copy_err:
402 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400403idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400404 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400405out:
John Calixtocb87ea22011-04-26 18:56:29 -0400406 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400407}
408
409static int mmc_blk_ioctl_cmd(struct block_device *bdev,
410 struct mmc_ioc_cmd __user *ic_ptr)
411{
412 struct mmc_blk_ioc_data *idata;
413 struct mmc_blk_data *md;
414 struct mmc_card *card;
415 struct mmc_command cmd = {0};
416 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530417 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400418 struct scatterlist sg;
419 int err;
420
421 /*
422 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
423 * whole block device, not on a partition. This prevents overspray
424 * between sibling partitions.
425 */
426 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
427 return -EPERM;
428
429 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
430 if (IS_ERR(idata))
431 return PTR_ERR(idata);
432
John Calixtocb87ea22011-04-26 18:56:29 -0400433 md = mmc_blk_get(bdev->bd_disk);
434 if (!md) {
435 err = -EINVAL;
436 goto cmd_done;
437 }
438
439 card = md->queue.card;
440 if (IS_ERR(card)) {
441 err = PTR_ERR(card);
442 goto cmd_done;
443 }
444
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100445 cmd.opcode = idata->ic.opcode;
446 cmd.arg = idata->ic.arg;
447 cmd.flags = idata->ic.flags;
448
449 if (idata->buf_bytes) {
450 data.sg = &sg;
451 data.sg_len = 1;
452 data.blksz = idata->ic.blksz;
453 data.blocks = idata->ic.blocks;
454
455 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
456
457 if (idata->ic.write_flag)
458 data.flags = MMC_DATA_WRITE;
459 else
460 data.flags = MMC_DATA_READ;
461
462 /* data.flags must already be set before doing this. */
463 mmc_set_data_timeout(&data, card);
464
465 /* Allow overriding the timeout_ns for empirical tuning. */
466 if (idata->ic.data_timeout_ns)
467 data.timeout_ns = idata->ic.data_timeout_ns;
468
469 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
470 /*
471 * Pretend this is a data transfer and rely on the
472 * host driver to compute timeout. When all host
473 * drivers support cmd.cmd_timeout for R1B, this
474 * can be changed to:
475 *
476 * mrq.data = NULL;
477 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
478 */
479 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
480 }
481
482 mrq.data = &data;
483 }
484
485 mrq.cmd = &cmd;
486
John Calixtocb87ea22011-04-26 18:56:29 -0400487 mmc_claim_host(card->host);
488
489 if (idata->ic.is_acmd) {
490 err = mmc_app_cmd(card->host, card);
491 if (err)
492 goto cmd_rel_host;
493 }
494
John Calixtocb87ea22011-04-26 18:56:29 -0400495 mmc_wait_for_req(card->host, &mrq);
496
497 if (cmd.error) {
498 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
499 __func__, cmd.error);
500 err = cmd.error;
501 goto cmd_rel_host;
502 }
503 if (data.error) {
504 dev_err(mmc_dev(card->host), "%s: data error %d\n",
505 __func__, data.error);
506 err = data.error;
507 goto cmd_rel_host;
508 }
509
510 /*
511 * According to the SD specs, some commands require a delay after
512 * issuing the command.
513 */
514 if (idata->ic.postsleep_min_us)
515 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
516
517 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
518 err = -EFAULT;
519 goto cmd_rel_host;
520 }
521
522 if (!idata->ic.write_flag) {
523 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
524 idata->buf, idata->buf_bytes)) {
525 err = -EFAULT;
526 goto cmd_rel_host;
527 }
528 }
529
530cmd_rel_host:
531 mmc_release_host(card->host);
532
533cmd_done:
534 mmc_blk_put(md);
535 kfree(idata->buf);
536 kfree(idata);
537 return err;
538}
539
540static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
541 unsigned int cmd, unsigned long arg)
542{
543 int ret = -EINVAL;
544 if (cmd == MMC_IOC_CMD)
545 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
546 return ret;
547}
548
549#ifdef CONFIG_COMPAT
550static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
551 unsigned int cmd, unsigned long arg)
552{
553 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
554}
555#endif
556
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700557static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500558 .open = mmc_blk_open,
559 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800560 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400562 .ioctl = mmc_blk_ioctl,
563#ifdef CONFIG_COMPAT
564 .compat_ioctl = mmc_blk_compat_ioctl,
565#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566};
567
Andrei Warkentin371a6892011-04-11 18:10:25 -0500568static inline int mmc_blk_part_switch(struct mmc_card *card,
569 struct mmc_blk_data *md)
570{
571 int ret;
572 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300573
Andrei Warkentin371a6892011-04-11 18:10:25 -0500574 if (main_md->part_curr == md->part_type)
575 return 0;
576
577 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300578 u8 part_config = card->ext_csd.part_config;
579
580 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
581 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500582
583 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300584 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500585 card->ext_csd.part_time);
586 if (ret)
587 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300588
589 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300590 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500591
592 main_md->part_curr = md->part_type;
593 return 0;
594}
595
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700596static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
597{
598 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100599 u32 result;
600 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700601
Venkatraman Sad5fd972011-08-25 00:30:50 +0530602 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400603 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400604 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700605
606 struct scatterlist sg;
607
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700608 cmd.opcode = MMC_APP_CMD;
609 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700610 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700611
612 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700613 if (err)
614 return (u32)-1;
615 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700616 return (u32)-1;
617
618 memset(&cmd, 0, sizeof(struct mmc_command));
619
620 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
621 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700622 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700623
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700624 data.blksz = 4;
625 data.blocks = 1;
626 data.flags = MMC_DATA_READ;
627 data.sg = &sg;
628 data.sg_len = 1;
Subhash Jadavani0126ae22012-06-12 14:46:06 +0530629 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700630
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700631 mrq.cmd = &cmd;
632 mrq.data = &data;
633
Ben Dooks051913d2009-06-08 23:33:57 +0100634 blocks = kmalloc(4, GFP_KERNEL);
635 if (!blocks)
636 return (u32)-1;
637
638 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700639
640 mmc_wait_for_req(card->host, &mrq);
641
Ben Dooks051913d2009-06-08 23:33:57 +0100642 result = ntohl(*blocks);
643 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700644
Ben Dooks051913d2009-06-08 23:33:57 +0100645 if (cmd.error || data.error)
646 result = (u32)-1;
647
648 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700649}
650
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100651static int send_stop(struct mmc_card *card, u32 *status)
652{
653 struct mmc_command cmd = {0};
654 int err;
655
656 cmd.opcode = MMC_STOP_TRANSMISSION;
657 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
658 err = mmc_wait_for_cmd(card->host, &cmd, 5);
659 if (err == 0)
660 *status = cmd.resp[0];
661 return err;
662}
663
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100664static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300665{
Chris Ball1278dba2011-04-13 23:40:30 -0400666 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300667 int err;
668
Adrian Hunter504f1912008-10-16 12:55:25 +0300669 cmd.opcode = MMC_SEND_STATUS;
670 if (!mmc_host_is_spi(card->host))
671 cmd.arg = card->rca << 16;
672 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100673 err = mmc_wait_for_cmd(card->host, &cmd, retries);
674 if (err == 0)
675 *status = cmd.resp[0];
676 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300677}
678
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530679#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100680#define ERR_RETRY 2
681#define ERR_ABORT 1
682#define ERR_CONTINUE 0
683
684static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
685 bool status_valid, u32 status)
686{
687 switch (error) {
688 case -EILSEQ:
689 /* response crc error, retry the r/w cmd */
690 pr_err("%s: %s sending %s command, card status %#x\n",
691 req->rq_disk->disk_name, "response CRC error",
692 name, status);
693 return ERR_RETRY;
694
695 case -ETIMEDOUT:
696 pr_err("%s: %s sending %s command, card status %#x\n",
697 req->rq_disk->disk_name, "timed out", name, status);
698
699 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700700 if (!status_valid) {
701 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100702 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700703 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100704 /*
705 * If it was a r/w cmd crc error, or illegal command
706 * (eg, issued in wrong state) then retry - we should
707 * have corrected the state problem above.
708 */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700709 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
710 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100711 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700712 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100713
714 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700715 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100716 return ERR_ABORT;
717
718 default:
719 /* We don't understand the error code the driver gave us */
720 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
721 req->rq_disk->disk_name, error, status);
722 return ERR_ABORT;
723 }
724}
725
726/*
727 * Initial r/w and stop cmd error recovery.
728 * We don't know whether the card received the r/w cmd or not, so try to
729 * restore things back to a sane state. Essentially, we do this as follows:
730 * - Obtain card status. If the first attempt to obtain card status fails,
731 * the status word will reflect the failed status cmd, not the failed
732 * r/w cmd. If we fail to obtain card status, it suggests we can no
733 * longer communicate with the card.
734 * - Check the card state. If the card received the cmd but there was a
735 * transient problem with the response, it might still be in a data transfer
736 * mode. Try to send it a stop command. If this fails, we can't recover.
737 * - If the r/w cmd failed due to a response CRC error, it was probably
738 * transient, so retry the cmd.
739 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
740 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
741 * illegal cmd, retry.
742 * Otherwise we don't understand what happened, so abort.
743 */
744static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +0300745 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100746{
747 bool prev_cmd_status_valid = true;
748 u32 status, stop_status = 0;
749 int err, retry;
750
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530751 if (mmc_card_removed(card))
752 return ERR_NOMEDIUM;
753
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100754 /*
755 * Try to get card status which indicates both the card state
756 * and why there was no response. If the first attempt fails,
757 * we can't be sure the returned status is for the r/w command.
758 */
759 for (retry = 2; retry >= 0; retry--) {
760 err = get_card_status(card, &status, 0);
761 if (!err)
762 break;
763
764 prev_cmd_status_valid = false;
765 pr_err("%s: error %d sending status command, %sing\n",
766 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
767 }
768
769 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530770 if (err) {
771 /* Check if the card is removed */
772 if (mmc_detect_card_removed(card->host))
773 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100774 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530775 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100776
Adrian Hunter67716322011-08-29 16:42:15 +0300777 /* Flag ECC errors */
778 if ((status & R1_CARD_ECC_FAILED) ||
779 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
780 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
781 *ecc_err = 1;
782
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100783 /*
784 * Check the current card state. If it is in some data transfer
785 * mode, tell it to stop (and hopefully transition back to TRAN.)
786 */
787 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
788 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
789 err = send_stop(card, &stop_status);
790 if (err)
791 pr_err("%s: error %d sending stop command\n",
792 req->rq_disk->disk_name, err);
793
794 /*
795 * If the stop cmd also timed out, the card is probably
796 * not present, so abort. Other errors are bad news too.
797 */
798 if (err)
799 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300800 if (stop_status & R1_CARD_ECC_FAILED)
801 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100802 }
803
804 /* Check for set block count errors */
805 if (brq->sbc.error)
806 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
807 prev_cmd_status_valid, status);
808
809 /* Check for r/w command errors */
810 if (brq->cmd.error)
811 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
812 prev_cmd_status_valid, status);
813
Adrian Hunter67716322011-08-29 16:42:15 +0300814 /* Data errors */
815 if (!brq->stop.error)
816 return ERR_CONTINUE;
817
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100818 /* Now for stop errors. These aren't fatal to the transfer. */
819 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
820 req->rq_disk->disk_name, brq->stop.error,
821 brq->cmd.resp[0], status);
822
823 /*
824 * Subsitute in our own stop status as this will give the error
825 * state which happened during the execution of the r/w command.
826 */
827 if (stop_status) {
828 brq->stop.resp[0] = stop_status;
829 brq->stop.error = 0;
830 }
831 return ERR_CONTINUE;
832}
833
Adrian Hunter67716322011-08-29 16:42:15 +0300834static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
835 int type)
836{
837 int err;
838
839 if (md->reset_done & type)
840 return -EEXIST;
841
842 md->reset_done |= type;
843 err = mmc_hw_reset(host);
844 /* Ensure we switch back to the correct partition */
845 if (err != -EOPNOTSUPP) {
846 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
847 int part_err;
848
849 main_md->part_curr = main_md->part_type;
850 part_err = mmc_blk_part_switch(host->card, md);
851 if (part_err) {
852 /*
853 * We have failed to get back into the correct
854 * partition, so we need to abort the whole request.
855 */
856 return -ENODEV;
857 }
858 }
859 return err;
860}
861
862static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
863{
864 md->reset_done &= ~type;
865}
866
Adrian Hunterbd788c92010-08-11 14:17:47 -0700867static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
868{
869 struct mmc_blk_data *md = mq->data;
870 struct mmc_card *card = md->queue.card;
871 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300872 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700873
Adrian Hunterbd788c92010-08-11 14:17:47 -0700874 if (!mmc_can_erase(card)) {
875 err = -EOPNOTSUPP;
876 goto out;
877 }
878
879 from = blk_rq_pos(req);
880 nr = blk_rq_sectors(req);
881
Kyungmin Parkb3bf9152011-10-18 09:34:04 +0900882 if (mmc_can_discard(card))
883 arg = MMC_DISCARD_ARG;
884 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -0700885 arg = MMC_TRIM_ARG;
886 else
887 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300888retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500889 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
890 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
891 INAND_CMD38_ARG_EXT_CSD,
892 arg == MMC_TRIM_ARG ?
893 INAND_CMD38_ARG_TRIM :
894 INAND_CMD38_ARG_ERASE,
895 0);
896 if (err)
897 goto out;
898 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700899 err = mmc_erase(card, from, nr, arg);
900out:
Adrian Hunter67716322011-08-29 16:42:15 +0300901 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
902 goto retry;
903 if (!err)
904 mmc_blk_reset_success(md, type);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530905 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -0700906
Adrian Hunterbd788c92010-08-11 14:17:47 -0700907 return err ? 0 : 1;
908}
909
Adrian Hunter49804542010-08-11 14:17:50 -0700910static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
911 struct request *req)
912{
913 struct mmc_blk_data *md = mq->data;
914 struct mmc_card *card = md->queue.card;
915 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300916 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700917
Maya Erez463bb952012-05-24 23:46:29 +0300918 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700919 err = -EOPNOTSUPP;
920 goto out;
921 }
922
923 from = blk_rq_pos(req);
924 nr = blk_rq_sectors(req);
925
926 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
927 arg = MMC_SECURE_TRIM1_ARG;
928 else
929 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300930retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500931 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
932 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
933 INAND_CMD38_ARG_EXT_CSD,
934 arg == MMC_SECURE_TRIM1_ARG ?
935 INAND_CMD38_ARG_SECTRIM1 :
936 INAND_CMD38_ARG_SECERASE,
937 0);
938 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300939 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500940 }
Adrian Hunter28302812012-04-05 14:45:48 +0300941
Adrian Hunter49804542010-08-11 14:17:50 -0700942 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +0300943 if (err == -EIO)
944 goto out_retry;
945 if (err)
946 goto out;
947
948 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500949 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
950 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
951 INAND_CMD38_ARG_EXT_CSD,
952 INAND_CMD38_ARG_SECTRIM2,
953 0);
954 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300955 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500956 }
Adrian Hunter28302812012-04-05 14:45:48 +0300957
Adrian Hunter49804542010-08-11 14:17:50 -0700958 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +0300959 if (err == -EIO)
960 goto out_retry;
961 if (err)
962 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500963 }
Adrian Hunter28302812012-04-05 14:45:48 +0300964
965 if (mmc_can_sanitize(card))
966 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
967 EXT_CSD_SANITIZE_START, 1, 0);
968out_retry:
969 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +0300970 goto retry;
971 if (!err)
972 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +0300973out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530974 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -0700975
Adrian Hunter49804542010-08-11 14:17:50 -0700976 return err ? 0 : 1;
977}
978
Maya Erez463bb952012-05-24 23:46:29 +0300979static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
980 struct request *req)
981{
982 struct mmc_blk_data *md = mq->data;
983 struct mmc_card *card = md->queue.card;
984 int err = 0;
985
986 BUG_ON(!card);
987 BUG_ON(!card->host);
988
989 if (!(mmc_can_sanitize(card) &&
990 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
991 pr_warning("%s: %s - SANITIZE is not supported\n",
992 mmc_hostname(card->host), __func__);
993 err = -EOPNOTSUPP;
994 goto out;
995 }
996
997 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
998 mmc_hostname(card->host), __func__);
999
1000 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +03001001 EXT_CSD_SANITIZE_START, 1,
1002 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +03001003
1004 if (err)
1005 pr_err("%s: %s - mmc_switch() with "
1006 "EXT_CSD_SANITIZE_START failed. err=%d\n",
1007 mmc_hostname(card->host), __func__, err);
1008
1009 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
1010 __func__);
1011
1012out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301013 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +03001014
1015 return err ? 0 : 1;
1016}
1017
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001018static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1019{
1020 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001021 struct mmc_card *card = md->queue.card;
1022 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001023
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001024 ret = mmc_flush_cache(card);
1025 if (ret)
1026 ret = -EIO;
1027
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301028 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001029
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001030 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001031}
1032
1033/*
1034 * Reformat current write as a reliable write, supporting
1035 * both legacy and the enhanced reliable write MMC cards.
1036 * In each transfer we'll handle only as much as a single
1037 * reliable write can handle, thus finish the request in
1038 * partial completions.
1039 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001040static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1041 struct mmc_card *card,
1042 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001043{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001044 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1045 /* Legacy mode imposes restrictions on transfers. */
1046 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1047 brq->data.blocks = 1;
1048
1049 if (brq->data.blocks > card->ext_csd.rel_sectors)
1050 brq->data.blocks = card->ext_csd.rel_sectors;
1051 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1052 brq->data.blocks = 1;
1053 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001054}
1055
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001056#define CMD_ERRORS \
1057 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1058 R1_ADDRESS_ERROR | /* Misaligned address */ \
1059 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1060 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1061 R1_CC_ERROR | /* Card controller error */ \
1062 R1_ERROR) /* General/unknown error */
1063
Per Forlinee8a43a2011-07-01 18:55:33 +02001064static int mmc_blk_err_check(struct mmc_card *card,
1065 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001066{
Per Forlinee8a43a2011-07-01 18:55:33 +02001067 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1068 mmc_active);
1069 struct mmc_blk_request *brq = &mq_mrq->brq;
1070 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001071 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001072
1073 /*
1074 * sbc.error indicates a problem with the set block count
1075 * command. No data will have been transferred.
1076 *
1077 * cmd.error indicates a problem with the r/w command. No
1078 * data will have been transferred.
1079 *
1080 * stop.error indicates a problem with the stop command. Data
1081 * may have been transferred, or may still be transferring.
1082 */
Adrian Hunter67716322011-08-29 16:42:15 +03001083 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1084 brq->data.error) {
1085 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001086 case ERR_RETRY:
1087 return MMC_BLK_RETRY;
1088 case ERR_ABORT:
1089 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301090 case ERR_NOMEDIUM:
1091 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001092 case ERR_CONTINUE:
1093 break;
1094 }
1095 }
1096
1097 /*
1098 * Check for errors relating to the execution of the
1099 * initial command - such as address errors. No data
1100 * has been transferred.
1101 */
1102 if (brq->cmd.resp[0] & CMD_ERRORS) {
1103 pr_err("%s: r/w command failed, status = %#x\n",
1104 req->rq_disk->disk_name, brq->cmd.resp[0]);
1105 return MMC_BLK_ABORT;
1106 }
1107
1108 /*
1109 * Everything else is either success, or a data error of some
1110 * kind. If it was a write, we may have transitioned to
1111 * program mode, which we have to wait for it to complete.
1112 */
1113 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1114 u32 status;
1115 do {
1116 int err = get_card_status(card, &status, 5);
1117 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301118 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001119 req->rq_disk->disk_name, err);
1120 return MMC_BLK_CMD_ERR;
1121 }
1122 /*
1123 * Some cards mishandle the status bits,
1124 * so make sure to check both the busy
1125 * indication and the card state.
1126 */
1127 } while (!(status & R1_READY_FOR_DATA) ||
1128 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1129 }
1130
1131 if (brq->data.error) {
1132 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1133 req->rq_disk->disk_name, brq->data.error,
1134 (unsigned)blk_rq_pos(req),
1135 (unsigned)blk_rq_sectors(req),
1136 brq->cmd.resp[0], brq->stop.resp[0]);
1137
1138 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001139 if (ecc_err)
1140 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001141 return MMC_BLK_DATA_ERR;
1142 } else {
1143 return MMC_BLK_CMD_ERR;
1144 }
1145 }
1146
Adrian Hunter67716322011-08-29 16:42:15 +03001147 if (!brq->data.bytes_xfered)
1148 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001149
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001150 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1151 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1152 return MMC_BLK_PARTIAL;
1153 else
1154 return MMC_BLK_SUCCESS;
1155 }
1156
Adrian Hunter67716322011-08-29 16:42:15 +03001157 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1158 return MMC_BLK_PARTIAL;
1159
1160 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001161}
1162
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001163static int mmc_blk_packed_err_check(struct mmc_card *card,
1164 struct mmc_async_req *areq)
1165{
1166 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1167 mmc_active);
1168 struct request *req = mq_rq->req;
1169 int err, check, status;
1170 u8 ext_csd[512];
1171
1172 mq_rq->packed_retries--;
1173 check = mmc_blk_err_check(card, areq);
1174 err = get_card_status(card, &status, 0);
1175 if (err) {
1176 pr_err("%s: error %d sending status command\n",
1177 req->rq_disk->disk_name, err);
1178 return MMC_BLK_ABORT;
1179 }
1180
1181 if (status & R1_EXCEPTION_EVENT) {
1182 err = mmc_send_ext_csd(card, ext_csd);
1183 if (err) {
1184 pr_err("%s: error %d sending ext_csd\n",
1185 req->rq_disk->disk_name, err);
1186 return MMC_BLK_ABORT;
1187 }
1188
1189 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1190 EXT_CSD_PACKED_FAILURE) &&
1191 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1192 EXT_CSD_PACKED_GENERIC_ERROR)) {
1193 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1194 EXT_CSD_PACKED_INDEXED_ERROR) {
1195 mq_rq->packed_fail_idx =
1196 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1197 return MMC_BLK_PARTIAL;
1198 }
1199 }
1200 }
1201
1202 return check;
1203}
1204
Per Forlin54d49d772011-07-01 18:55:29 +02001205static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1206 struct mmc_card *card,
1207 int disable_multi,
1208 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Per Forlin54d49d772011-07-01 18:55:29 +02001210 u32 readcmd, writecmd;
1211 struct mmc_blk_request *brq = &mqrq->brq;
1212 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301214 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001216 /*
1217 * Reliable writes are used to implement Forced Unit Access and
1218 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001219 *
1220 * XXX: this really needs a good explanation of why REQ_META
1221 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001222 */
1223 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1224 (req->cmd_flags & REQ_META)) &&
1225 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001226 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001227
Per Forlin54d49d772011-07-01 18:55:29 +02001228 memset(brq, 0, sizeof(struct mmc_blk_request));
1229 brq->mrq.cmd = &brq->cmd;
1230 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Per Forlin54d49d772011-07-01 18:55:29 +02001232 brq->cmd.arg = blk_rq_pos(req);
1233 if (!mmc_card_blockaddr(card))
1234 brq->cmd.arg <<= 9;
1235 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1236 brq->data.blksz = 512;
1237 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1238 brq->stop.arg = 0;
1239 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1240 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Asutosh Das1a897142012-07-27 18:10:19 +05301242 brq->data.fault_injected = false;
Per Forlin54d49d772011-07-01 18:55:29 +02001243 /*
1244 * The block layer doesn't support all sector count
1245 * restrictions, so we need to be prepared for too big
1246 * requests.
1247 */
1248 if (brq->data.blocks > card->host->max_blk_count)
1249 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001251 if (brq->data.blocks > 1) {
1252 /*
1253 * After a read error, we redo the request one sector
1254 * at a time in order to accurately determine which
1255 * sectors can be read successfully.
1256 */
1257 if (disable_multi)
1258 brq->data.blocks = 1;
1259
1260 /* Some controllers can't do multiblock reads due to hw bugs */
1261 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1262 rq_data_dir(req) == READ)
1263 brq->data.blocks = 1;
1264 }
Per Forlin54d49d772011-07-01 18:55:29 +02001265
1266 if (brq->data.blocks > 1 || do_rel_wr) {
1267 /* SPI multiblock writes terminate using a special
1268 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001269 */
Per Forlin54d49d772011-07-01 18:55:29 +02001270 if (!mmc_host_is_spi(card->host) ||
1271 rq_data_dir(req) == READ)
1272 brq->mrq.stop = &brq->stop;
1273 readcmd = MMC_READ_MULTIPLE_BLOCK;
1274 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1275 } else {
1276 brq->mrq.stop = NULL;
1277 readcmd = MMC_READ_SINGLE_BLOCK;
1278 writecmd = MMC_WRITE_BLOCK;
1279 }
1280 if (rq_data_dir(req) == READ) {
1281 brq->cmd.opcode = readcmd;
1282 brq->data.flags |= MMC_DATA_READ;
1283 } else {
1284 brq->cmd.opcode = writecmd;
1285 brq->data.flags |= MMC_DATA_WRITE;
1286 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001287
Per Forlin54d49d772011-07-01 18:55:29 +02001288 if (do_rel_wr)
1289 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001290
Per Forlin54d49d772011-07-01 18:55:29 +02001291 /*
Saugata Das42659002011-12-21 13:09:17 +05301292 * Data tag is used only during writing meta data to speed
1293 * up write and any subsequent read of this meta data
1294 */
1295 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1296 (req->cmd_flags & REQ_META) &&
1297 (rq_data_dir(req) == WRITE) &&
1298 ((brq->data.blocks * brq->data.blksz) >=
1299 card->ext_csd.data_tag_unit_size);
1300
1301 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001302 * Pre-defined multi-block transfers are preferable to
1303 * open ended-ones (and necessary for reliable writes).
1304 * However, it is not sufficient to just send CMD23,
1305 * and avoid the final CMD12, as on an error condition
1306 * CMD12 (stop) needs to be sent anyway. This, coupled
1307 * with Auto-CMD23 enhancements provided by some
1308 * hosts, means that the complexity of dealing
1309 * with this is best left to the host. If CMD23 is
1310 * supported by card and host, we'll fill sbc in and let
1311 * the host deal with handling it correctly. This means
1312 * that for hosts that don't expose MMC_CAP_CMD23, no
1313 * change of behavior will be observed.
1314 *
1315 * N.B: Some MMC cards experience perf degradation.
1316 * We'll avoid using CMD23-bounded multiblock writes for
1317 * these, while retaining features like reliable writes.
1318 */
Saugata Das42659002011-12-21 13:09:17 +05301319 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1320 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1321 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001322 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1323 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301324 (do_rel_wr ? (1 << 31) : 0) |
1325 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001326 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1327 brq->mrq.sbc = &brq->sbc;
1328 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001329
Per Forlin54d49d772011-07-01 18:55:29 +02001330 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001331
Per Forlin54d49d772011-07-01 18:55:29 +02001332 brq->data.sg = mqrq->sg;
1333 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001334
Per Forlin54d49d772011-07-01 18:55:29 +02001335 /*
1336 * Adjust the sg list so it is the same size as the
1337 * request.
1338 */
1339 if (brq->data.blocks != blk_rq_sectors(req)) {
1340 int i, data_size = brq->data.blocks << 9;
1341 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001342
Per Forlin54d49d772011-07-01 18:55:29 +02001343 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1344 data_size -= sg->length;
1345 if (data_size <= 0) {
1346 sg->length += data_size;
1347 i++;
1348 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001349 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001350 }
Per Forlin54d49d772011-07-01 18:55:29 +02001351 brq->data.sg_len = i;
1352 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001353
Per Forlinee8a43a2011-07-01 18:55:33 +02001354 mqrq->mmc_active.mrq = &brq->mrq;
1355 mqrq->mmc_active.err_check = mmc_blk_err_check;
1356
Per Forlin54d49d772011-07-01 18:55:29 +02001357 mmc_queue_bounce_pre(mqrq);
1358}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001360static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1361 struct request *req)
1362{
1363 struct mmc_host *host = mq->card->host;
1364 int data_dir;
1365
1366 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1367 return;
1368
1369 /*
1370 * In case the packing control is not supported by the host, it should
1371 * not have an effect on the write packing. Therefore we have to enable
1372 * the write packing
1373 */
1374 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1375 mq->wr_packing_enabled = true;
1376 return;
1377 }
1378
1379 if (!req || (req && (req->cmd_flags & REQ_FLUSH))) {
1380 if (mq->num_of_potential_packed_wr_reqs >
1381 mq->num_wr_reqs_to_start_packing)
1382 mq->wr_packing_enabled = true;
Tatyana Brokhman285ee172012-10-07 10:26:27 +02001383 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001384 return;
1385 }
1386
1387 data_dir = rq_data_dir(req);
1388
1389 if (data_dir == READ) {
1390 mq->num_of_potential_packed_wr_reqs = 0;
1391 mq->wr_packing_enabled = false;
1392 return;
1393 } else if (data_dir == WRITE) {
1394 mq->num_of_potential_packed_wr_reqs++;
1395 }
1396
1397 if (mq->num_of_potential_packed_wr_reqs >
1398 mq->num_wr_reqs_to_start_packing)
1399 mq->wr_packing_enabled = true;
1400
1401}
1402
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001403static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1404{
1405 struct request_queue *q = mq->queue;
1406 struct mmc_card *card = mq->card;
1407 struct request *cur = req, *next = NULL;
1408 struct mmc_blk_data *md = mq->data;
1409 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1410 unsigned int req_sectors = 0, phys_segments = 0;
1411 unsigned int max_blk_count, max_phys_segs;
1412 u8 put_back = 0;
1413 u8 max_packed_rw = 0;
1414 u8 reqs = 0;
1415
1416 mmc_blk_clear_packed(mq->mqrq_cur);
1417
1418 if (!(md->flags & MMC_BLK_CMD23) ||
1419 !card->ext_csd.packed_event_en)
1420 goto no_packed;
1421
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001422 if (!mq->wr_packing_enabled)
1423 goto no_packed;
1424
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001425 if ((rq_data_dir(cur) == WRITE) &&
1426 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1427 max_packed_rw = card->ext_csd.max_packed_writes;
1428
1429 if (max_packed_rw == 0)
1430 goto no_packed;
1431
1432 if (mmc_req_rel_wr(cur) &&
1433 (md->flags & MMC_BLK_REL_WR) &&
1434 !en_rel_wr)
1435 goto no_packed;
1436
1437 if (mmc_large_sec(card) &&
1438 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1439 goto no_packed;
1440
1441 max_blk_count = min(card->host->max_blk_count,
1442 card->host->max_req_size >> 9);
1443 if (unlikely(max_blk_count > 0xffff))
1444 max_blk_count = 0xffff;
1445
1446 max_phys_segs = queue_max_segments(q);
1447 req_sectors += blk_rq_sectors(cur);
1448 phys_segments += cur->nr_phys_segments;
1449
1450 if (rq_data_dir(cur) == WRITE) {
1451 req_sectors++;
1452 phys_segments++;
1453 }
1454
1455 while (reqs < max_packed_rw - 1) {
1456 spin_lock_irq(q->queue_lock);
1457 next = blk_fetch_request(q);
1458 spin_unlock_irq(q->queue_lock);
1459 if (!next)
1460 break;
1461
1462 if (mmc_large_sec(card) &&
1463 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
1464 put_back = 1;
1465 break;
1466 }
1467
1468 if (next->cmd_flags & REQ_DISCARD ||
1469 next->cmd_flags & REQ_FLUSH) {
1470 put_back = 1;
1471 break;
1472 }
1473
1474 if (rq_data_dir(cur) != rq_data_dir(next)) {
1475 put_back = 1;
1476 break;
1477 }
1478
1479 if (mmc_req_rel_wr(next) &&
1480 (md->flags & MMC_BLK_REL_WR) &&
1481 !en_rel_wr) {
1482 put_back = 1;
1483 break;
1484 }
1485
1486 req_sectors += blk_rq_sectors(next);
1487 if (req_sectors > max_blk_count) {
1488 put_back = 1;
1489 break;
1490 }
1491
1492 phys_segments += next->nr_phys_segments;
1493 if (phys_segments > max_phys_segs) {
1494 put_back = 1;
1495 break;
1496 }
1497
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001498 if (rq_data_dir(next) == WRITE)
1499 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001500 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1501 cur = next;
1502 reqs++;
1503 }
1504
1505 if (put_back) {
1506 spin_lock_irq(q->queue_lock);
1507 blk_requeue_request(q, next);
1508 spin_unlock_irq(q->queue_lock);
1509 }
1510
1511 if (reqs > 0) {
1512 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1513 mq->mqrq_cur->packed_num = ++reqs;
1514 mq->mqrq_cur->packed_retries = reqs;
1515 return reqs;
1516 }
1517
1518no_packed:
1519 mmc_blk_clear_packed(mq->mqrq_cur);
1520 return 0;
1521}
1522
1523static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1524 struct mmc_card *card,
1525 struct mmc_queue *mq)
1526{
1527 struct mmc_blk_request *brq = &mqrq->brq;
1528 struct request *req = mqrq->req;
1529 struct request *prq;
1530 struct mmc_blk_data *md = mq->data;
1531 bool do_rel_wr, do_data_tag;
1532 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1533 u8 i = 1;
1534
1535 mqrq->packed_cmd = MMC_PACKED_WRITE;
1536 mqrq->packed_blocks = 0;
1537 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1538
1539 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1540 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1541 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1542
1543 /*
1544 * Argument for each entry of packed group
1545 */
1546 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1547 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1548 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1549 (prq->cmd_flags & REQ_META) &&
1550 (rq_data_dir(prq) == WRITE) &&
1551 ((brq->data.blocks * brq->data.blksz) >=
1552 card->ext_csd.data_tag_unit_size);
1553 /* Argument of CMD23 */
1554 packed_cmd_hdr[(i * 2)] =
1555 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1556 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1557 blk_rq_sectors(prq);
1558 /* Argument of CMD18 or CMD25 */
1559 packed_cmd_hdr[((i * 2)) + 1] =
1560 mmc_card_blockaddr(card) ?
1561 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1562 mqrq->packed_blocks += blk_rq_sectors(prq);
1563 i++;
1564 }
1565
1566 memset(brq, 0, sizeof(struct mmc_blk_request));
1567 brq->mrq.cmd = &brq->cmd;
1568 brq->mrq.data = &brq->data;
1569 brq->mrq.sbc = &brq->sbc;
1570 brq->mrq.stop = &brq->stop;
1571
1572 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1573 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1574 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1575
1576 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1577 brq->cmd.arg = blk_rq_pos(req);
1578 if (!mmc_card_blockaddr(card))
1579 brq->cmd.arg <<= 9;
1580 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1581
1582 brq->data.blksz = 512;
1583 brq->data.blocks = mqrq->packed_blocks + 1;
1584 brq->data.flags |= MMC_DATA_WRITE;
1585
1586 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1587 brq->stop.arg = 0;
1588 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1589
1590 mmc_set_data_timeout(&brq->data, card);
1591
1592 brq->data.sg = mqrq->sg;
1593 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1594
1595 mqrq->mmc_active.mrq = &brq->mrq;
1596 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1597
1598 mmc_queue_bounce_pre(mqrq);
1599}
1600
Adrian Hunter67716322011-08-29 16:42:15 +03001601static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1602 struct mmc_blk_request *brq, struct request *req,
1603 int ret)
1604{
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001605 struct mmc_queue_req *mq_rq;
1606 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1607
Adrian Hunter67716322011-08-29 16:42:15 +03001608 /*
1609 * If this is an SD card and we're writing, we can first
1610 * mark the known good sectors as ok.
1611 *
1612 * If the card is not SD, we can still ok written sectors
1613 * as reported by the controller (which might be less than
1614 * the real number of written sectors, but never more).
1615 */
1616 if (mmc_card_sd(card)) {
1617 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05301618 if (!brq->data.fault_injected) {
1619 blocks = mmc_sd_num_wr_blocks(card);
1620 if (blocks != (u32)-1)
1621 ret = blk_end_request(req, 0, blocks << 9);
1622 } else
1623 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001624 } else {
1625 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1626 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1627 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001628 return ret;
1629}
1630
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001631static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1632{
1633 struct request *prq;
1634 int idx = mq_rq->packed_fail_idx, i = 0;
1635 int ret = 0;
1636
1637 while (!list_empty(&mq_rq->packed_list)) {
1638 prq = list_entry_rq(mq_rq->packed_list.next);
1639 if (idx == i) {
1640 /* retry from error index */
1641 mq_rq->packed_num -= idx;
1642 mq_rq->req = prq;
1643 ret = 1;
1644
1645 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1646 list_del_init(&prq->queuelist);
1647 mmc_blk_clear_packed(mq_rq);
1648 }
1649 return ret;
1650 }
1651 list_del_init(&prq->queuelist);
1652 blk_end_request(prq, 0, blk_rq_bytes(prq));
1653 i++;
1654 }
1655
1656 mmc_blk_clear_packed(mq_rq);
1657 return ret;
1658}
1659static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1660{
1661 struct request *prq;
1662
1663 while (!list_empty(&mq_rq->packed_list)) {
1664 prq = list_entry_rq(mq_rq->packed_list.next);
1665 list_del_init(&prq->queuelist);
1666 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1667 }
1668
1669 mmc_blk_clear_packed(mq_rq);
1670}
1671
1672static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1673 struct mmc_queue_req *mq_rq)
1674{
1675 struct request *prq;
1676 struct request_queue *q = mq->queue;
1677
1678 while (!list_empty(&mq_rq->packed_list)) {
1679 prq = list_entry_rq(mq_rq->packed_list.prev);
1680 if (prq->queuelist.prev != &mq_rq->packed_list) {
1681 list_del_init(&prq->queuelist);
1682 spin_lock_irq(q->queue_lock);
1683 blk_requeue_request(mq->queue, prq);
1684 spin_unlock_irq(q->queue_lock);
1685 } else {
1686 list_del_init(&prq->queuelist);
1687 }
1688 }
1689
1690 mmc_blk_clear_packed(mq_rq);
1691}
1692
Per Forlinee8a43a2011-07-01 18:55:33 +02001693static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001694{
1695 struct mmc_blk_data *md = mq->data;
1696 struct mmc_card *card = md->queue.card;
1697 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001698 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001699 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001700 struct mmc_queue_req *mq_rq;
Tatyana Brokhmanf94cf3d2012-09-27 11:00:02 +02001701 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001702 struct mmc_async_req *areq;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001703 const u8 packed_num = 2;
1704 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001705
1706 if (!rqc && !mq->mqrq_prev->req)
1707 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001708
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001709 if (rqc)
1710 reqs = mmc_blk_prep_packed_list(mq, rqc);
1711
Per Forlin54d49d772011-07-01 18:55:29 +02001712 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001713 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001714 if (reqs >= packed_num)
1715 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1716 card, mq);
1717 else
1718 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001719 areq = &mq->mqrq_cur->mmc_active;
1720 } else
1721 areq = NULL;
1722 areq = mmc_start_req(card->host, areq, (int *) &status);
1723 if (!areq)
1724 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001725
Per Forlinee8a43a2011-07-01 18:55:33 +02001726 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1727 brq = &mq_rq->brq;
1728 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001729 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001730 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001731
Per Forlind78d4a82011-07-01 18:55:30 +02001732 switch (status) {
1733 case MMC_BLK_SUCCESS:
1734 case MMC_BLK_PARTIAL:
1735 /*
1736 * A block was successfully transferred.
1737 */
Adrian Hunter67716322011-08-29 16:42:15 +03001738 mmc_blk_reset_success(md, type);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001739
1740 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1741 ret = mmc_blk_end_packed_req(mq_rq);
1742 break;
1743 } else {
1744 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001745 brq->data.bytes_xfered);
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001746 }
1747
Adrian Hunter67716322011-08-29 16:42:15 +03001748 /*
1749 * If the blk_end_request function returns non-zero even
1750 * though all data has been transferred and no errors
1751 * were returned by the host controller, it's a bug.
1752 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001753 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301754 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001755 __func__, blk_rq_bytes(req),
1756 brq->data.bytes_xfered);
1757 rqc = NULL;
1758 goto cmd_abort;
1759 }
Per Forlind78d4a82011-07-01 18:55:30 +02001760 break;
1761 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001762 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1763 if (!mmc_blk_reset(md, card->host, type))
1764 break;
1765 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001766 case MMC_BLK_RETRY:
1767 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001768 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001769 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001770 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001771 if (!mmc_blk_reset(md, card->host, type))
1772 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001773 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001774 case MMC_BLK_DATA_ERR: {
1775 int err;
1776
1777 err = mmc_blk_reset(md, card->host, type);
1778 if (!err)
1779 break;
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001780 if (err == -ENODEV ||
1781 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03001782 goto cmd_abort;
1783 /* Fall through */
1784 }
1785 case MMC_BLK_ECC_ERR:
1786 if (brq->data.blocks > 1) {
1787 /* Redo read one sector at a time */
1788 pr_warning("%s: retrying using single block read\n",
1789 req->rq_disk->disk_name);
1790 disable_multi = 1;
1791 break;
1792 }
Per Forlind78d4a82011-07-01 18:55:30 +02001793 /*
1794 * After an error, we redo I/O one sector at a
1795 * time, so we only reach here after trying to
1796 * read a single sector.
1797 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301798 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001799 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001800 if (!ret)
1801 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001802 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301803 case MMC_BLK_NOMEDIUM:
1804 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001805 }
1806
Per Forlinee8a43a2011-07-01 18:55:33 +02001807 if (ret) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001808 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1809 /*
1810 * In case of a incomplete request
1811 * prepare it again and resend.
1812 */
1813 mmc_blk_rw_rq_prep(mq_rq, card,
1814 disable_multi, mq);
1815 mmc_start_req(card->host,
1816 &mq_rq->mmc_active, NULL);
1817 } else {
1818 if (!mq_rq->packed_retries)
1819 goto cmd_abort;
1820 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1821 mmc_start_req(card->host,
1822 &mq_rq->mmc_active, NULL);
1823 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 } while (ret);
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return 1;
1828
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001829 cmd_abort:
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001830 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1831 if (mmc_card_removed(card))
1832 req->cmd_flags |= REQ_QUIET;
1833 while (ret)
1834 ret = blk_end_request(req, -EIO,
1835 blk_rq_cur_bytes(req));
1836 } else {
1837 mmc_blk_abort_packed_req(mq_rq);
1838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Per Forlinee8a43a2011-07-01 18:55:33 +02001840 start_new_req:
1841 if (rqc) {
Seungwon Jeon53f8f572012-09-27 15:00:26 +02001842 /*
1843 * If current request is packed, it needs to put back.
1844 */
1845 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE)
1846 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1847
Per Forlinee8a43a2011-07-01 18:55:33 +02001848 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1849 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1850 }
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return 0;
1853}
1854
Adrian Hunterbd788c92010-08-11 14:17:47 -07001855static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1856{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001857 int ret;
1858 struct mmc_blk_data *md = mq->data;
1859 struct mmc_card *card = md->queue.card;
1860
San Mehat148c57a2009-07-30 08:21:19 -07001861#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1862 if (mmc_bus_needs_resume(card->host)) {
1863 mmc_resume_bus(card->host);
1864 mmc_blk_set_blksize(md, card);
1865 }
1866#endif
1867
Per Forlinee8a43a2011-07-01 18:55:33 +02001868 if (req && !mq->mqrq_prev->req)
1869 /* claim host only for the first request */
1870 mmc_claim_host(card->host);
1871
Andrei Warkentin371a6892011-04-11 18:10:25 -05001872 ret = mmc_blk_part_switch(card, md);
1873 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001874 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301875 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001876 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001877 ret = 0;
1878 goto out;
1879 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001880
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02001881 mmc_blk_write_packing_control(mq, req);
1882
Maya Erez463bb952012-05-24 23:46:29 +03001883 if (req && req->cmd_flags & REQ_SANITIZE) {
1884 /* complete ongoing async transfer before issuing sanitize */
1885 if (card->host && card->host->areq)
1886 mmc_blk_issue_rw_rq(mq, NULL);
1887 ret = mmc_blk_issue_sanitize_rq(mq, req);
1888 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001889 /* complete ongoing async transfer before issuing discard */
1890 if (card->host->areq)
1891 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter49804542010-08-11 14:17:50 -07001892 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001893 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001894 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001895 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02001896 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001897 /* complete ongoing async transfer before issuing flush */
1898 if (card->host->areq)
1899 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001900 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001901 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001902 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001903 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001904
Andrei Warkentin371a6892011-04-11 18:10:25 -05001905out:
Per Forlinee8a43a2011-07-01 18:55:33 +02001906 if (!req)
1907 /* release host only when there are no more requests */
1908 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001909 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001910}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Russell Kinga6f6c962006-01-03 22:38:44 +00001912static inline int mmc_blk_readonly(struct mmc_card *card)
1913{
1914 return mmc_card_readonly(card) ||
1915 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1916}
1917
Andrei Warkentin371a6892011-04-11 18:10:25 -05001918static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1919 struct device *parent,
1920 sector_t size,
1921 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001922 const char *subname,
1923 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 struct mmc_blk_data *md;
1926 int devidx, ret;
1927
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001928 devidx = find_first_zero_bit(dev_use, max_devices);
1929 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return ERR_PTR(-ENOSPC);
1931 __set_bit(devidx, dev_use);
1932
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001933 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001934 if (!md) {
1935 ret = -ENOMEM;
1936 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001938
Russell Kinga6f6c962006-01-03 22:38:44 +00001939 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001940 * !subname implies we are creating main mmc_blk_data that will be
1941 * associated with mmc_card with mmc_set_drvdata. Due to device
1942 * partitions, devidx will not coincide with a per-physical card
1943 * index anymore so we keep track of a name index.
1944 */
1945 if (!subname) {
1946 md->name_idx = find_first_zero_bit(name_use, max_devices);
1947 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001948 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001949 md->name_idx = ((struct mmc_blk_data *)
1950 dev_to_disk(parent)->private_data)->name_idx;
1951
Johan Rudholmadd710e2011-12-02 08:51:06 +01001952 md->area_type = area_type;
1953
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001954 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001955 * Set the read-only status based on the supported commands
1956 * and the write protect switch.
1957 */
1958 md->read_only = mmc_blk_readonly(card);
1959
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001960 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001961 if (md->disk == NULL) {
1962 ret = -ENOMEM;
1963 goto err_kfree;
1964 }
1965
1966 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001967 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001968 md->usage = 1;
1969
Adrian Hunterd09408a2011-06-23 13:40:28 +03001970 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001971 if (ret)
1972 goto err_putdisk;
1973
Russell Kinga6f6c962006-01-03 22:38:44 +00001974 md->queue.issue_fn = mmc_blk_issue_rq;
1975 md->queue.data = md;
1976
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001977 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001978 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001979 md->disk->fops = &mmc_bdops;
1980 md->disk->private_data = md;
1981 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001982 md->disk->driverfs_dev = parent;
1983 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07001984 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00001985
1986 /*
1987 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1988 *
1989 * - be set for removable media with permanent block devices
1990 * - be unset for removable block devices with permanent media
1991 *
1992 * Since MMC block devices clearly fall under the second
1993 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1994 * should use the block device creation/destruction hotplug
1995 * messages to tell when the card is present.
1996 */
1997
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001998 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1999 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002000
Martin K. Petersene1defc42009-05-22 17:17:49 -04002001 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002002 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002003
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002004 if (mmc_host_cmd23(card->host)) {
2005 if (mmc_card_mmc(card) ||
2006 (mmc_card_sd(card) &&
2007 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2008 md->flags |= MMC_BLK_CMD23;
2009 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002010
2011 if (mmc_card_mmc(card) &&
2012 md->flags & MMC_BLK_CMD23 &&
2013 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2014 card->ext_csd.rel_sectors)) {
2015 md->flags |= MMC_BLK_REL_WR;
2016 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2017 }
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002020
2021 err_putdisk:
2022 put_disk(md->disk);
2023 err_kfree:
2024 kfree(md);
2025 out:
2026 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
2028
Andrei Warkentin371a6892011-04-11 18:10:25 -05002029static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2030{
2031 sector_t size;
2032 struct mmc_blk_data *md;
2033
2034 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2035 /*
2036 * The EXT_CSD sector count is in number or 512 byte
2037 * sectors.
2038 */
2039 size = card->ext_csd.sectors;
2040 } else {
2041 /*
2042 * The CSD capacity field is in units of read_blkbits.
2043 * set_capacity takes units of 512 bytes.
2044 */
2045 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2046 }
2047
Johan Rudholmadd710e2011-12-02 08:51:06 +01002048 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2049 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002050 return md;
2051}
2052
2053static int mmc_blk_alloc_part(struct mmc_card *card,
2054 struct mmc_blk_data *md,
2055 unsigned int part_type,
2056 sector_t size,
2057 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002058 const char *subname,
2059 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002060{
2061 char cap_str[10];
2062 struct mmc_blk_data *part_md;
2063
2064 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002065 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002066 if (IS_ERR(part_md))
2067 return PTR_ERR(part_md);
2068 part_md->part_type = part_type;
2069 list_add(&part_md->part, &md->part);
2070
2071 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2072 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302073 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002074 part_md->disk->disk_name, mmc_card_id(card),
2075 mmc_card_name(card), part_md->part_type, cap_str);
2076 return 0;
2077}
2078
Namjae Jeone0c368d2011-10-06 23:41:38 +09002079/* MMC Physical partitions consist of two boot partitions and
2080 * up to four general purpose partitions.
2081 * For each partition enabled in EXT_CSD a block device will be allocatedi
2082 * to provide access to the partition.
2083 */
2084
Andrei Warkentin371a6892011-04-11 18:10:25 -05002085static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2086{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002087 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002088
2089 if (!mmc_card_mmc(card))
2090 return 0;
2091
Namjae Jeone0c368d2011-10-06 23:41:38 +09002092 for (idx = 0; idx < card->nr_parts; idx++) {
2093 if (card->part[idx].size) {
2094 ret = mmc_blk_alloc_part(card, md,
2095 card->part[idx].part_cfg,
2096 card->part[idx].size >> 9,
2097 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002098 card->part[idx].name,
2099 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002100 if (ret)
2101 return ret;
2102 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002103 }
2104
2105 return ret;
2106}
2107
Andrei Warkentin371a6892011-04-11 18:10:25 -05002108static void mmc_blk_remove_req(struct mmc_blk_data *md)
2109{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002110 struct mmc_card *card;
2111
Andrei Warkentin371a6892011-04-11 18:10:25 -05002112 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002113 card = md->queue.card;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002114 device_remove_file(disk_to_dev(md->disk),
2115 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002116 if (md->disk->flags & GENHD_FL_UP) {
2117 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002118 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2119 card->ext_csd.boot_ro_lockable)
2120 device_remove_file(disk_to_dev(md->disk),
2121 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002122
2123 /* Stop new requests from getting into the queue */
2124 del_gendisk(md->disk);
2125 }
2126
2127 /* Then flush out any already in there */
2128 mmc_cleanup_queue(&md->queue);
2129 mmc_blk_put(md);
2130 }
2131}
2132
2133static void mmc_blk_remove_parts(struct mmc_card *card,
2134 struct mmc_blk_data *md)
2135{
2136 struct list_head *pos, *q;
2137 struct mmc_blk_data *part_md;
2138
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002139 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002140 list_for_each_safe(pos, q, &md->part) {
2141 part_md = list_entry(pos, struct mmc_blk_data, part);
2142 list_del(pos);
2143 mmc_blk_remove_req(part_md);
2144 }
2145}
2146
2147static int mmc_add_disk(struct mmc_blk_data *md)
2148{
2149 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002150 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002151
2152 add_disk(md->disk);
2153 md->force_ro.show = force_ro_show;
2154 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302155 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002156 md->force_ro.attr.name = "force_ro";
2157 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2158 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2159 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002160 goto force_ro_fail;
2161
2162 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2163 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002164 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002165
2166 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2167 mode = S_IRUGO;
2168 else
2169 mode = S_IRUGO | S_IWUSR;
2170
2171 md->power_ro_lock.show = power_ro_lock_show;
2172 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002173 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002174 md->power_ro_lock.attr.mode = mode;
2175 md->power_ro_lock.attr.name =
2176 "ro_lock_until_next_power_on";
2177 ret = device_create_file(disk_to_dev(md->disk),
2178 &md->power_ro_lock);
2179 if (ret)
2180 goto power_ro_lock_fail;
2181 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002182
Tatyana Brokhman0cc76402012-10-07 09:52:16 +02002183 md->num_wr_reqs_to_start_packing.show =
2184 num_wr_reqs_to_start_packing_show;
2185 md->num_wr_reqs_to_start_packing.store =
2186 num_wr_reqs_to_start_packing_store;
2187 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2188 md->num_wr_reqs_to_start_packing.attr.name =
2189 "num_wr_reqs_to_start_packing";
2190 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2191 ret = device_create_file(disk_to_dev(md->disk),
2192 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002193 if (ret)
2194 goto power_ro_lock_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002195
Johan Rudholmadd710e2011-12-02 08:51:06 +01002196 return ret;
2197
2198power_ro_lock_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002199 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002200force_ro_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002201 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002202
2203 return ret;
2204}
2205
Chris Ballc59d4472011-11-11 22:01:43 -05002206#define CID_MANFID_SANDISK 0x2
2207#define CID_MANFID_TOSHIBA 0x11
2208#define CID_MANFID_MICRON 0x13
2209
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002210static const struct mmc_fixup blk_fixups[] =
2211{
Chris Ballc59d4472011-11-11 22:01:43 -05002212 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2213 MMC_QUIRK_INAND_CMD38),
2214 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2215 MMC_QUIRK_INAND_CMD38),
2216 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2217 MMC_QUIRK_INAND_CMD38),
2218 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2219 MMC_QUIRK_INAND_CMD38),
2220 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2221 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002222
2223 /*
2224 * Some MMC cards experience performance degradation with CMD23
2225 * instead of CMD12-bounded multiblock transfers. For now we'll
2226 * black list what's bad...
2227 * - Certain Toshiba cards.
2228 *
2229 * N.B. This doesn't affect SD cards.
2230 */
Chris Ballc59d4472011-11-11 22:01:43 -05002231 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002232 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002233 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002234 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002235 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002236 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002237
2238 /*
2239 * Some Micron MMC cards needs longer data read timeout than
2240 * indicated in CSD.
2241 */
Chris Ballc59d4472011-11-11 22:01:43 -05002242 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002243 MMC_QUIRK_LONG_READ_TIME),
2244
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302245 /* Some INAND MCP devices advertise incorrect timeout values */
2246 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2247 MMC_QUIRK_INAND_DATA_TIMEOUT),
2248
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002249 END_FIXUP
2250};
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252static int mmc_blk_probe(struct mmc_card *card)
2253{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002254 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002255 char cap_str[10];
2256
Pierre Ossman912490d2005-05-21 10:27:02 +01002257 /*
2258 * Check that the card supports the command class(es) we need.
2259 */
2260 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return -ENODEV;
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 md = mmc_blk_alloc(card);
2264 if (IS_ERR(md))
2265 return PTR_ERR(md);
2266
Yi Li444122f2009-02-05 15:31:57 +08002267 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002268 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302269 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002271 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Andrei Warkentin371a6892011-04-11 18:10:25 -05002273 if (mmc_blk_alloc_parts(card, md))
2274 goto out;
2275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002277 mmc_fixup_device(card, blk_fixups);
2278
San Mehat148c57a2009-07-30 08:21:19 -07002279#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2280 mmc_set_bus_resume_policy(card->host, 1);
2281#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002282 if (mmc_add_disk(md))
2283 goto out;
2284
2285 list_for_each_entry(part_md, &md->part, part) {
2286 if (mmc_add_disk(part_md))
2287 goto out;
2288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return 0;
2290
2291 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002292 mmc_blk_remove_parts(card, md);
2293 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295}
2296
2297static void mmc_blk_remove(struct mmc_card *card)
2298{
2299 struct mmc_blk_data *md = mmc_get_drvdata(card);
2300
Andrei Warkentin371a6892011-04-11 18:10:25 -05002301 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002302 mmc_claim_host(card->host);
2303 mmc_blk_part_switch(card, md);
2304 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002305 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002307#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2308 mmc_set_bus_resume_policy(card->host, 0);
2309#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310}
2311
2312#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002313static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002315 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 struct mmc_blk_data *md = mmc_get_drvdata(card);
2317
2318 if (md) {
2319 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002320 list_for_each_entry(part_md, &md->part, part) {
2321 mmc_queue_suspend(&part_md->queue);
2322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
2324 return 0;
2325}
2326
2327static int mmc_blk_resume(struct mmc_card *card)
2328{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002329 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 struct mmc_blk_data *md = mmc_get_drvdata(card);
2331
2332 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002333 /*
2334 * Resume involves the card going into idle state,
2335 * so current partition is always the main one.
2336 */
2337 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002339 list_for_each_entry(part_md, &md->part, part) {
2340 mmc_queue_resume(&part_md->queue);
2341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
2343 return 0;
2344}
2345#else
2346#define mmc_blk_suspend NULL
2347#define mmc_blk_resume NULL
2348#endif
2349
2350static struct mmc_driver mmc_driver = {
2351 .drv = {
2352 .name = "mmcblk",
2353 },
2354 .probe = mmc_blk_probe,
2355 .remove = mmc_blk_remove,
2356 .suspend = mmc_blk_suspend,
2357 .resume = mmc_blk_resume,
2358};
2359
2360static int __init mmc_blk_init(void)
2361{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002362 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002364 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2365 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2366
2367 max_devices = 256 / perdev_minors;
2368
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002369 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2370 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002373 res = mmc_register_driver(&mmc_driver);
2374 if (res)
2375 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002377 return 0;
2378 out2:
2379 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 out:
2381 return res;
2382}
2383
2384static void __exit mmc_blk_exit(void)
2385{
2386 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002387 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388}
2389
2390module_init(mmc_blk_init);
2391module_exit(mmc_blk_exit);
2392
2393MODULE_LICENSE("GPL");
2394MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2395