blob: 989c53ade817ed4fec611ef2a9a50da7922651a1 [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 Jeon121b8e82012-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 Brokhman98e67812012-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
Maya Erezb52c7182012-11-28 23:46:50 +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 Jeon121b8e82012-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 Jeon121b8e82012-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 Brokhman98e67812012-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 Jeon121b8e82012-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 Jeon121b8e82012-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 Brokhman98e67812012-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;
1383 return;
1384 }
1385
1386 data_dir = rq_data_dir(req);
1387
1388 if (data_dir == READ) {
1389 mq->num_of_potential_packed_wr_reqs = 0;
1390 mq->wr_packing_enabled = false;
1391 return;
1392 } else if (data_dir == WRITE) {
1393 mq->num_of_potential_packed_wr_reqs++;
1394 }
1395
1396 if (mq->num_of_potential_packed_wr_reqs >
1397 mq->num_wr_reqs_to_start_packing)
1398 mq->wr_packing_enabled = true;
1399
1400}
1401
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001402static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1403{
1404 struct request_queue *q = mq->queue;
1405 struct mmc_card *card = mq->card;
1406 struct request *cur = req, *next = NULL;
1407 struct mmc_blk_data *md = mq->data;
1408 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1409 unsigned int req_sectors = 0, phys_segments = 0;
1410 unsigned int max_blk_count, max_phys_segs;
1411 u8 put_back = 0;
1412 u8 max_packed_rw = 0;
1413 u8 reqs = 0;
1414
1415 mmc_blk_clear_packed(mq->mqrq_cur);
1416
1417 if (!(md->flags & MMC_BLK_CMD23) ||
1418 !card->ext_csd.packed_event_en)
1419 goto no_packed;
1420
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001421 if (!mq->wr_packing_enabled)
1422 goto no_packed;
1423
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001424 if ((rq_data_dir(cur) == WRITE) &&
1425 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1426 max_packed_rw = card->ext_csd.max_packed_writes;
1427
1428 if (max_packed_rw == 0)
1429 goto no_packed;
1430
1431 if (mmc_req_rel_wr(cur) &&
1432 (md->flags & MMC_BLK_REL_WR) &&
1433 !en_rel_wr)
1434 goto no_packed;
1435
1436 if (mmc_large_sec(card) &&
1437 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1438 goto no_packed;
1439
1440 max_blk_count = min(card->host->max_blk_count,
1441 card->host->max_req_size >> 9);
1442 if (unlikely(max_blk_count > 0xffff))
1443 max_blk_count = 0xffff;
1444
1445 max_phys_segs = queue_max_segments(q);
1446 req_sectors += blk_rq_sectors(cur);
1447 phys_segments += cur->nr_phys_segments;
1448
1449 if (rq_data_dir(cur) == WRITE) {
1450 req_sectors++;
1451 phys_segments++;
1452 }
1453
1454 while (reqs < max_packed_rw - 1) {
1455 spin_lock_irq(q->queue_lock);
1456 next = blk_fetch_request(q);
1457 spin_unlock_irq(q->queue_lock);
1458 if (!next)
1459 break;
1460
1461 if (mmc_large_sec(card) &&
1462 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
1463 put_back = 1;
1464 break;
1465 }
1466
1467 if (next->cmd_flags & REQ_DISCARD ||
1468 next->cmd_flags & REQ_FLUSH) {
1469 put_back = 1;
1470 break;
1471 }
1472
1473 if (rq_data_dir(cur) != rq_data_dir(next)) {
1474 put_back = 1;
1475 break;
1476 }
1477
1478 if (mmc_req_rel_wr(next) &&
1479 (md->flags & MMC_BLK_REL_WR) &&
1480 !en_rel_wr) {
1481 put_back = 1;
1482 break;
1483 }
1484
1485 req_sectors += blk_rq_sectors(next);
1486 if (req_sectors > max_blk_count) {
1487 put_back = 1;
1488 break;
1489 }
1490
1491 phys_segments += next->nr_phys_segments;
1492 if (phys_segments > max_phys_segs) {
1493 put_back = 1;
1494 break;
1495 }
1496
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001497 if (rq_data_dir(next) == WRITE)
1498 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001499 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1500 cur = next;
1501 reqs++;
1502 }
1503
1504 if (put_back) {
1505 spin_lock_irq(q->queue_lock);
1506 blk_requeue_request(q, next);
1507 spin_unlock_irq(q->queue_lock);
1508 }
1509
1510 if (reqs > 0) {
1511 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1512 mq->mqrq_cur->packed_num = ++reqs;
1513 mq->mqrq_cur->packed_retries = reqs;
1514 return reqs;
1515 }
1516
1517no_packed:
1518 mmc_blk_clear_packed(mq->mqrq_cur);
1519 return 0;
1520}
1521
1522static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1523 struct mmc_card *card,
1524 struct mmc_queue *mq)
1525{
1526 struct mmc_blk_request *brq = &mqrq->brq;
1527 struct request *req = mqrq->req;
1528 struct request *prq;
1529 struct mmc_blk_data *md = mq->data;
1530 bool do_rel_wr, do_data_tag;
1531 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1532 u8 i = 1;
1533
1534 mqrq->packed_cmd = MMC_PACKED_WRITE;
1535 mqrq->packed_blocks = 0;
1536 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1537
1538 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1539 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1540 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1541
1542 /*
1543 * Argument for each entry of packed group
1544 */
1545 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1546 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1547 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1548 (prq->cmd_flags & REQ_META) &&
1549 (rq_data_dir(prq) == WRITE) &&
1550 ((brq->data.blocks * brq->data.blksz) >=
1551 card->ext_csd.data_tag_unit_size);
1552 /* Argument of CMD23 */
1553 packed_cmd_hdr[(i * 2)] =
1554 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1555 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1556 blk_rq_sectors(prq);
1557 /* Argument of CMD18 or CMD25 */
1558 packed_cmd_hdr[((i * 2)) + 1] =
1559 mmc_card_blockaddr(card) ?
1560 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1561 mqrq->packed_blocks += blk_rq_sectors(prq);
1562 i++;
1563 }
1564
1565 memset(brq, 0, sizeof(struct mmc_blk_request));
1566 brq->mrq.cmd = &brq->cmd;
1567 brq->mrq.data = &brq->data;
1568 brq->mrq.sbc = &brq->sbc;
1569 brq->mrq.stop = &brq->stop;
1570
1571 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1572 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1573 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1574
1575 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1576 brq->cmd.arg = blk_rq_pos(req);
1577 if (!mmc_card_blockaddr(card))
1578 brq->cmd.arg <<= 9;
1579 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1580
1581 brq->data.blksz = 512;
1582 brq->data.blocks = mqrq->packed_blocks + 1;
1583 brq->data.flags |= MMC_DATA_WRITE;
1584
1585 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1586 brq->stop.arg = 0;
1587 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1588
1589 mmc_set_data_timeout(&brq->data, card);
1590
1591 brq->data.sg = mqrq->sg;
1592 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1593
1594 mqrq->mmc_active.mrq = &brq->mrq;
1595 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1596
1597 mmc_queue_bounce_pre(mqrq);
1598}
1599
Adrian Hunter67716322011-08-29 16:42:15 +03001600static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1601 struct mmc_blk_request *brq, struct request *req,
1602 int ret)
1603{
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001604 struct mmc_queue_req *mq_rq;
1605 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1606
Adrian Hunter67716322011-08-29 16:42:15 +03001607 /*
1608 * If this is an SD card and we're writing, we can first
1609 * mark the known good sectors as ok.
1610 *
1611 * If the card is not SD, we can still ok written sectors
1612 * as reported by the controller (which might be less than
1613 * the real number of written sectors, but never more).
1614 */
1615 if (mmc_card_sd(card)) {
1616 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05301617 if (!brq->data.fault_injected) {
1618 blocks = mmc_sd_num_wr_blocks(card);
1619 if (blocks != (u32)-1)
1620 ret = blk_end_request(req, 0, blocks << 9);
1621 } else
1622 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001623 } else {
1624 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1625 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1626 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001627 return ret;
1628}
1629
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001630static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1631{
1632 struct request *prq;
1633 int idx = mq_rq->packed_fail_idx, i = 0;
1634 int ret = 0;
1635
1636 while (!list_empty(&mq_rq->packed_list)) {
1637 prq = list_entry_rq(mq_rq->packed_list.next);
1638 if (idx == i) {
1639 /* retry from error index */
1640 mq_rq->packed_num -= idx;
1641 mq_rq->req = prq;
1642 ret = 1;
1643
1644 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1645 list_del_init(&prq->queuelist);
1646 mmc_blk_clear_packed(mq_rq);
1647 }
1648 return ret;
1649 }
1650 list_del_init(&prq->queuelist);
1651 blk_end_request(prq, 0, blk_rq_bytes(prq));
1652 i++;
1653 }
1654
1655 mmc_blk_clear_packed(mq_rq);
1656 return ret;
1657}
1658static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1659{
1660 struct request *prq;
1661
1662 while (!list_empty(&mq_rq->packed_list)) {
1663 prq = list_entry_rq(mq_rq->packed_list.next);
1664 list_del_init(&prq->queuelist);
1665 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1666 }
1667
1668 mmc_blk_clear_packed(mq_rq);
1669}
1670
1671static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1672 struct mmc_queue_req *mq_rq)
1673{
1674 struct request *prq;
1675 struct request_queue *q = mq->queue;
1676
1677 while (!list_empty(&mq_rq->packed_list)) {
1678 prq = list_entry_rq(mq_rq->packed_list.prev);
1679 if (prq->queuelist.prev != &mq_rq->packed_list) {
1680 list_del_init(&prq->queuelist);
1681 spin_lock_irq(q->queue_lock);
1682 blk_requeue_request(mq->queue, prq);
1683 spin_unlock_irq(q->queue_lock);
1684 } else {
1685 list_del_init(&prq->queuelist);
1686 }
1687 }
1688
1689 mmc_blk_clear_packed(mq_rq);
1690}
1691
Per Forlinee8a43a2011-07-01 18:55:33 +02001692static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001693{
1694 struct mmc_blk_data *md = mq->data;
1695 struct mmc_card *card = md->queue.card;
1696 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001697 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001698 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001699 struct mmc_queue_req *mq_rq;
Maya Erezb52c7182012-11-28 23:46:50 +02001700 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001701 struct mmc_async_req *areq;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001702 const u8 packed_num = 2;
1703 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001704
1705 if (!rqc && !mq->mqrq_prev->req)
1706 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001707
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001708 if (rqc)
1709 reqs = mmc_blk_prep_packed_list(mq, rqc);
1710
Per Forlin54d49d772011-07-01 18:55:29 +02001711 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001712 if (rqc) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001713 if (reqs >= packed_num)
1714 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1715 card, mq);
1716 else
1717 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001718 areq = &mq->mqrq_cur->mmc_active;
1719 } else
1720 areq = NULL;
1721 areq = mmc_start_req(card->host, areq, (int *) &status);
1722 if (!areq)
1723 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001724
Per Forlinee8a43a2011-07-01 18:55:33 +02001725 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1726 brq = &mq_rq->brq;
1727 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001728 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001729 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001730
Per Forlind78d4a82011-07-01 18:55:30 +02001731 switch (status) {
1732 case MMC_BLK_SUCCESS:
1733 case MMC_BLK_PARTIAL:
1734 /*
1735 * A block was successfully transferred.
1736 */
Adrian Hunter67716322011-08-29 16:42:15 +03001737 mmc_blk_reset_success(md, type);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001738
1739 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1740 ret = mmc_blk_end_packed_req(mq_rq);
1741 break;
1742 } else {
1743 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001744 brq->data.bytes_xfered);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001745 }
1746
Adrian Hunter67716322011-08-29 16:42:15 +03001747 /*
1748 * If the blk_end_request function returns non-zero even
1749 * though all data has been transferred and no errors
1750 * were returned by the host controller, it's a bug.
1751 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001752 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301753 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001754 __func__, blk_rq_bytes(req),
1755 brq->data.bytes_xfered);
1756 rqc = NULL;
1757 goto cmd_abort;
1758 }
Per Forlind78d4a82011-07-01 18:55:30 +02001759 break;
1760 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001761 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1762 if (!mmc_blk_reset(md, card->host, type))
1763 break;
1764 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001765 case MMC_BLK_RETRY:
1766 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001767 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001768 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001769 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001770 if (!mmc_blk_reset(md, card->host, type))
1771 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001772 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001773 case MMC_BLK_DATA_ERR: {
1774 int err;
1775
1776 err = mmc_blk_reset(md, card->host, type);
1777 if (!err)
1778 break;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001779 if (err == -ENODEV ||
1780 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03001781 goto cmd_abort;
1782 /* Fall through */
1783 }
1784 case MMC_BLK_ECC_ERR:
1785 if (brq->data.blocks > 1) {
1786 /* Redo read one sector at a time */
1787 pr_warning("%s: retrying using single block read\n",
1788 req->rq_disk->disk_name);
1789 disable_multi = 1;
1790 break;
1791 }
Per Forlind78d4a82011-07-01 18:55:30 +02001792 /*
1793 * After an error, we redo I/O one sector at a
1794 * time, so we only reach here after trying to
1795 * read a single sector.
1796 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301797 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001798 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001799 if (!ret)
1800 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001801 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301802 case MMC_BLK_NOMEDIUM:
1803 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001804 }
1805
Per Forlinee8a43a2011-07-01 18:55:33 +02001806 if (ret) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001807 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1808 /*
1809 * In case of a incomplete request
1810 * prepare it again and resend.
1811 */
1812 mmc_blk_rw_rq_prep(mq_rq, card,
1813 disable_multi, mq);
1814 mmc_start_req(card->host,
1815 &mq_rq->mmc_active, NULL);
1816 } else {
1817 if (!mq_rq->packed_retries)
1818 goto cmd_abort;
1819 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1820 mmc_start_req(card->host,
1821 &mq_rq->mmc_active, NULL);
1822 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 } while (ret);
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return 1;
1827
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001828 cmd_abort:
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001829 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1830 if (mmc_card_removed(card))
1831 req->cmd_flags |= REQ_QUIET;
1832 while (ret)
1833 ret = blk_end_request(req, -EIO,
1834 blk_rq_cur_bytes(req));
1835 } else {
1836 mmc_blk_abort_packed_req(mq_rq);
1837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Per Forlinee8a43a2011-07-01 18:55:33 +02001839 start_new_req:
1840 if (rqc) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001841 /*
1842 * If current request is packed, it needs to put back.
1843 */
1844 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE)
1845 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1846
Per Forlinee8a43a2011-07-01 18:55:33 +02001847 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1848 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1849 }
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return 0;
1852}
1853
Adrian Hunterbd788c92010-08-11 14:17:47 -07001854static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1855{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001856 int ret;
1857 struct mmc_blk_data *md = mq->data;
1858 struct mmc_card *card = md->queue.card;
1859
San Mehat148c57a2009-07-30 08:21:19 -07001860#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1861 if (mmc_bus_needs_resume(card->host)) {
1862 mmc_resume_bus(card->host);
1863 mmc_blk_set_blksize(md, card);
1864 }
1865#endif
1866
Per Forlinee8a43a2011-07-01 18:55:33 +02001867 if (req && !mq->mqrq_prev->req)
1868 /* claim host only for the first request */
1869 mmc_claim_host(card->host);
1870
Andrei Warkentin371a6892011-04-11 18:10:25 -05001871 ret = mmc_blk_part_switch(card, md);
1872 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001873 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301874 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001875 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001876 ret = 0;
1877 goto out;
1878 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001879
Tatyana Brokhman98e67812012-10-07 09:52:16 +02001880 mmc_blk_write_packing_control(mq, req);
1881
Maya Erez463bb952012-05-24 23:46:29 +03001882 if (req && req->cmd_flags & REQ_SANITIZE) {
1883 /* complete ongoing async transfer before issuing sanitize */
1884 if (card->host && card->host->areq)
1885 mmc_blk_issue_rw_rq(mq, NULL);
1886 ret = mmc_blk_issue_sanitize_rq(mq, req);
1887 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001888 /* complete ongoing async transfer before issuing discard */
1889 if (card->host->areq)
1890 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter49804542010-08-11 14:17:50 -07001891 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001892 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001893 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001894 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02001895 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001896 /* complete ongoing async transfer before issuing flush */
1897 if (card->host->areq)
1898 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001899 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001900 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001901 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001902 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001903
Andrei Warkentin371a6892011-04-11 18:10:25 -05001904out:
Per Forlinee8a43a2011-07-01 18:55:33 +02001905 if (!req)
1906 /* release host only when there are no more requests */
1907 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001908 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001909}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Russell Kinga6f6c962006-01-03 22:38:44 +00001911static inline int mmc_blk_readonly(struct mmc_card *card)
1912{
1913 return mmc_card_readonly(card) ||
1914 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1915}
1916
Andrei Warkentin371a6892011-04-11 18:10:25 -05001917static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1918 struct device *parent,
1919 sector_t size,
1920 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001921 const char *subname,
1922 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
1924 struct mmc_blk_data *md;
1925 int devidx, ret;
1926
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001927 devidx = find_first_zero_bit(dev_use, max_devices);
1928 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return ERR_PTR(-ENOSPC);
1930 __set_bit(devidx, dev_use);
1931
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001932 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001933 if (!md) {
1934 ret = -ENOMEM;
1935 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001937
Russell Kinga6f6c962006-01-03 22:38:44 +00001938 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001939 * !subname implies we are creating main mmc_blk_data that will be
1940 * associated with mmc_card with mmc_set_drvdata. Due to device
1941 * partitions, devidx will not coincide with a per-physical card
1942 * index anymore so we keep track of a name index.
1943 */
1944 if (!subname) {
1945 md->name_idx = find_first_zero_bit(name_use, max_devices);
1946 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001947 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001948 md->name_idx = ((struct mmc_blk_data *)
1949 dev_to_disk(parent)->private_data)->name_idx;
1950
Johan Rudholmadd710e2011-12-02 08:51:06 +01001951 md->area_type = area_type;
1952
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001953 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001954 * Set the read-only status based on the supported commands
1955 * and the write protect switch.
1956 */
1957 md->read_only = mmc_blk_readonly(card);
1958
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001959 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001960 if (md->disk == NULL) {
1961 ret = -ENOMEM;
1962 goto err_kfree;
1963 }
1964
1965 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001966 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001967 md->usage = 1;
1968
Adrian Hunterd09408a2011-06-23 13:40:28 +03001969 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001970 if (ret)
1971 goto err_putdisk;
1972
Russell Kinga6f6c962006-01-03 22:38:44 +00001973 md->queue.issue_fn = mmc_blk_issue_rq;
1974 md->queue.data = md;
1975
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001976 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001977 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001978 md->disk->fops = &mmc_bdops;
1979 md->disk->private_data = md;
1980 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001981 md->disk->driverfs_dev = parent;
1982 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07001983 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00001984
1985 /*
1986 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1987 *
1988 * - be set for removable media with permanent block devices
1989 * - be unset for removable block devices with permanent media
1990 *
1991 * Since MMC block devices clearly fall under the second
1992 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1993 * should use the block device creation/destruction hotplug
1994 * messages to tell when the card is present.
1995 */
1996
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001997 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1998 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001999
Martin K. Petersene1defc42009-05-22 17:17:49 -04002000 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002001 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002002
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002003 if (mmc_host_cmd23(card->host)) {
2004 if (mmc_card_mmc(card) ||
2005 (mmc_card_sd(card) &&
2006 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2007 md->flags |= MMC_BLK_CMD23;
2008 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002009
2010 if (mmc_card_mmc(card) &&
2011 md->flags & MMC_BLK_CMD23 &&
2012 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2013 card->ext_csd.rel_sectors)) {
2014 md->flags |= MMC_BLK_REL_WR;
2015 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2016 }
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002019
2020 err_putdisk:
2021 put_disk(md->disk);
2022 err_kfree:
2023 kfree(md);
2024 out:
2025 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Andrei Warkentin371a6892011-04-11 18:10:25 -05002028static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2029{
2030 sector_t size;
2031 struct mmc_blk_data *md;
2032
2033 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2034 /*
2035 * The EXT_CSD sector count is in number or 512 byte
2036 * sectors.
2037 */
2038 size = card->ext_csd.sectors;
2039 } else {
2040 /*
2041 * The CSD capacity field is in units of read_blkbits.
2042 * set_capacity takes units of 512 bytes.
2043 */
2044 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2045 }
2046
Johan Rudholmadd710e2011-12-02 08:51:06 +01002047 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2048 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002049 return md;
2050}
2051
2052static int mmc_blk_alloc_part(struct mmc_card *card,
2053 struct mmc_blk_data *md,
2054 unsigned int part_type,
2055 sector_t size,
2056 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002057 const char *subname,
2058 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002059{
2060 char cap_str[10];
2061 struct mmc_blk_data *part_md;
2062
2063 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002064 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002065 if (IS_ERR(part_md))
2066 return PTR_ERR(part_md);
2067 part_md->part_type = part_type;
2068 list_add(&part_md->part, &md->part);
2069
2070 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2071 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302072 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002073 part_md->disk->disk_name, mmc_card_id(card),
2074 mmc_card_name(card), part_md->part_type, cap_str);
2075 return 0;
2076}
2077
Namjae Jeone0c368d2011-10-06 23:41:38 +09002078/* MMC Physical partitions consist of two boot partitions and
2079 * up to four general purpose partitions.
2080 * For each partition enabled in EXT_CSD a block device will be allocatedi
2081 * to provide access to the partition.
2082 */
2083
Andrei Warkentin371a6892011-04-11 18:10:25 -05002084static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2085{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002086 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002087
2088 if (!mmc_card_mmc(card))
2089 return 0;
2090
Namjae Jeone0c368d2011-10-06 23:41:38 +09002091 for (idx = 0; idx < card->nr_parts; idx++) {
2092 if (card->part[idx].size) {
2093 ret = mmc_blk_alloc_part(card, md,
2094 card->part[idx].part_cfg,
2095 card->part[idx].size >> 9,
2096 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002097 card->part[idx].name,
2098 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002099 if (ret)
2100 return ret;
2101 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002102 }
2103
2104 return ret;
2105}
2106
Andrei Warkentin371a6892011-04-11 18:10:25 -05002107static void mmc_blk_remove_req(struct mmc_blk_data *md)
2108{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002109 struct mmc_card *card;
2110
Andrei Warkentin371a6892011-04-11 18:10:25 -05002111 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002112 card = md->queue.card;
Tatyana Brokhman98e67812012-10-07 09:52:16 +02002113 device_remove_file(disk_to_dev(md->disk),
2114 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002115 if (md->disk->flags & GENHD_FL_UP) {
2116 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002117 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2118 card->ext_csd.boot_ro_lockable)
2119 device_remove_file(disk_to_dev(md->disk),
2120 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002121
2122 /* Stop new requests from getting into the queue */
2123 del_gendisk(md->disk);
2124 }
2125
2126 /* Then flush out any already in there */
2127 mmc_cleanup_queue(&md->queue);
2128 mmc_blk_put(md);
2129 }
2130}
2131
2132static void mmc_blk_remove_parts(struct mmc_card *card,
2133 struct mmc_blk_data *md)
2134{
2135 struct list_head *pos, *q;
2136 struct mmc_blk_data *part_md;
2137
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002138 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002139 list_for_each_safe(pos, q, &md->part) {
2140 part_md = list_entry(pos, struct mmc_blk_data, part);
2141 list_del(pos);
2142 mmc_blk_remove_req(part_md);
2143 }
2144}
2145
2146static int mmc_add_disk(struct mmc_blk_data *md)
2147{
2148 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002149 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002150
2151 add_disk(md->disk);
2152 md->force_ro.show = force_ro_show;
2153 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302154 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002155 md->force_ro.attr.name = "force_ro";
2156 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2157 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2158 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002159 goto force_ro_fail;
2160
2161 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2162 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002163 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002164
2165 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2166 mode = S_IRUGO;
2167 else
2168 mode = S_IRUGO | S_IWUSR;
2169
2170 md->power_ro_lock.show = power_ro_lock_show;
2171 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002172 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002173 md->power_ro_lock.attr.mode = mode;
2174 md->power_ro_lock.attr.name =
2175 "ro_lock_until_next_power_on";
2176 ret = device_create_file(disk_to_dev(md->disk),
2177 &md->power_ro_lock);
2178 if (ret)
2179 goto power_ro_lock_fail;
2180 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002181
Tatyana Brokhman98e67812012-10-07 09:52:16 +02002182 md->num_wr_reqs_to_start_packing.show =
2183 num_wr_reqs_to_start_packing_show;
2184 md->num_wr_reqs_to_start_packing.store =
2185 num_wr_reqs_to_start_packing_store;
2186 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2187 md->num_wr_reqs_to_start_packing.attr.name =
2188 "num_wr_reqs_to_start_packing";
2189 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2190 ret = device_create_file(disk_to_dev(md->disk),
2191 &md->num_wr_reqs_to_start_packing);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002192 if (ret)
2193 goto power_ro_lock_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002194
Johan Rudholmadd710e2011-12-02 08:51:06 +01002195 return ret;
2196
2197power_ro_lock_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002198 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002199force_ro_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002200 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002201
2202 return ret;
2203}
2204
Chris Ballc59d4472011-11-11 22:01:43 -05002205#define CID_MANFID_SANDISK 0x2
2206#define CID_MANFID_TOSHIBA 0x11
2207#define CID_MANFID_MICRON 0x13
2208
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002209static const struct mmc_fixup blk_fixups[] =
2210{
Chris Ballc59d4472011-11-11 22:01:43 -05002211 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2212 MMC_QUIRK_INAND_CMD38),
2213 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2214 MMC_QUIRK_INAND_CMD38),
2215 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2216 MMC_QUIRK_INAND_CMD38),
2217 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2218 MMC_QUIRK_INAND_CMD38),
2219 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2220 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002221
2222 /*
2223 * Some MMC cards experience performance degradation with CMD23
2224 * instead of CMD12-bounded multiblock transfers. For now we'll
2225 * black list what's bad...
2226 * - Certain Toshiba cards.
2227 *
2228 * N.B. This doesn't affect SD cards.
2229 */
Chris Ballc59d4472011-11-11 22:01:43 -05002230 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002231 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002232 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002233 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002234 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002235 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002236
2237 /*
2238 * Some Micron MMC cards needs longer data read timeout than
2239 * indicated in CSD.
2240 */
Chris Ballc59d4472011-11-11 22:01:43 -05002241 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002242 MMC_QUIRK_LONG_READ_TIME),
2243
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302244 /* Some INAND MCP devices advertise incorrect timeout values */
2245 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2246 MMC_QUIRK_INAND_DATA_TIMEOUT),
2247
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002248 END_FIXUP
2249};
2250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251static int mmc_blk_probe(struct mmc_card *card)
2252{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002253 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002254 char cap_str[10];
2255
Pierre Ossman912490d2005-05-21 10:27:02 +01002256 /*
2257 * Check that the card supports the command class(es) we need.
2258 */
2259 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return -ENODEV;
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 md = mmc_blk_alloc(card);
2263 if (IS_ERR(md))
2264 return PTR_ERR(md);
2265
Yi Li444122f2009-02-05 15:31:57 +08002266 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002267 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302268 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002270 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Andrei Warkentin371a6892011-04-11 18:10:25 -05002272 if (mmc_blk_alloc_parts(card, md))
2273 goto out;
2274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002276 mmc_fixup_device(card, blk_fixups);
2277
San Mehat148c57a2009-07-30 08:21:19 -07002278#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2279 mmc_set_bus_resume_policy(card->host, 1);
2280#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002281 if (mmc_add_disk(md))
2282 goto out;
2283
2284 list_for_each_entry(part_md, &md->part, part) {
2285 if (mmc_add_disk(part_md))
2286 goto out;
2287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 return 0;
2289
2290 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002291 mmc_blk_remove_parts(card, md);
2292 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002293 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
2296static void mmc_blk_remove(struct mmc_card *card)
2297{
2298 struct mmc_blk_data *md = mmc_get_drvdata(card);
2299
Andrei Warkentin371a6892011-04-11 18:10:25 -05002300 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002301 mmc_claim_host(card->host);
2302 mmc_blk_part_switch(card, md);
2303 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002304 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002306#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2307 mmc_set_bus_resume_policy(card->host, 0);
2308#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
2311#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002312static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002314 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 struct mmc_blk_data *md = mmc_get_drvdata(card);
2316
2317 if (md) {
2318 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002319 list_for_each_entry(part_md, &md->part, part) {
2320 mmc_queue_suspend(&part_md->queue);
2321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
2323 return 0;
2324}
2325
2326static int mmc_blk_resume(struct mmc_card *card)
2327{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002328 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 struct mmc_blk_data *md = mmc_get_drvdata(card);
2330
2331 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002332 /*
2333 * Resume involves the card going into idle state,
2334 * so current partition is always the main one.
2335 */
2336 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002338 list_for_each_entry(part_md, &md->part, part) {
2339 mmc_queue_resume(&part_md->queue);
2340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 }
2342 return 0;
2343}
2344#else
2345#define mmc_blk_suspend NULL
2346#define mmc_blk_resume NULL
2347#endif
2348
2349static struct mmc_driver mmc_driver = {
2350 .drv = {
2351 .name = "mmcblk",
2352 },
2353 .probe = mmc_blk_probe,
2354 .remove = mmc_blk_remove,
2355 .suspend = mmc_blk_suspend,
2356 .resume = mmc_blk_resume,
2357};
2358
2359static int __init mmc_blk_init(void)
2360{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002361 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002363 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2364 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2365
2366 max_devices = 256 / perdev_minors;
2367
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002368 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2369 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002372 res = mmc_register_driver(&mmc_driver);
2373 if (res)
2374 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002376 return 0;
2377 out2:
2378 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 out:
2380 return res;
2381}
2382
2383static void __exit mmc_blk_exit(void)
2384{
2385 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002386 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387}
2388
2389module_init(mmc_blk_init);
2390module_exit(mmc_blk_exit);
2391
2392MODULE_LICENSE("GPL");
2393MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2394