blob: a9f1b53b8139e45ade196d531449e5197ccc6885 [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;
118 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
Arjan van de Vena621aae2006-01-12 18:43:35 +0000121static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Maya Erezb52c7182012-11-28 23:46:50 +0200123enum mmc_blk_status {
124 MMC_BLK_SUCCESS = 0,
125 MMC_BLK_PARTIAL,
126 MMC_BLK_CMD_ERR,
127 MMC_BLK_RETRY,
128 MMC_BLK_ABORT,
129 MMC_BLK_DATA_ERR,
130 MMC_BLK_ECC_ERR,
131 MMC_BLK_NOMEDIUM,
Seungwon Jeon968c7742012-05-31 11:54:47 +0300132};
133
Seungwon Jeon121b8e82012-09-27 15:00:26 +0200134enum {
135 MMC_PACKED_N_IDX = -1,
136 MMC_PACKED_N_ZERO,
137 MMC_PACKED_N_SINGLE,
138};
139
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400140module_param(perdev_minors, int, 0444);
141MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
142
Seungwon Jeon121b8e82012-09-27 15:00:26 +0200143static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
144{
145 mqrq->packed_cmd = MMC_PACKED_NONE;
146 mqrq->packed_num = MMC_PACKED_N_ZERO;
147}
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
150{
151 struct mmc_blk_data *md;
152
Arjan van de Vena621aae2006-01-12 18:43:35 +0000153 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 md = disk->private_data;
155 if (md && md->usage == 0)
156 md = NULL;
157 if (md)
158 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000159 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 return md;
162}
163
Andrei Warkentin371a6892011-04-11 18:10:25 -0500164static inline int mmc_get_devidx(struct gendisk *disk)
165{
Colin Cross71b54d42010-09-03 12:41:21 -0700166 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500167 return devidx;
168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static void mmc_blk_put(struct mmc_blk_data *md)
171{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000172 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 md->usage--;
174 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500175 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800176 blk_cleanup_queue(md->queue.queue);
177
David Woodhouse1dff3142007-11-21 18:45:12 +0100178 __clear_bit(devidx, dev_use);
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 kfree(md);
182 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000183 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Johan Rudholmadd710e2011-12-02 08:51:06 +0100186static ssize_t power_ro_lock_show(struct device *dev,
187 struct device_attribute *attr, char *buf)
188{
189 int ret;
190 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
191 struct mmc_card *card = md->queue.card;
192 int locked = 0;
193
194 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
195 locked = 2;
196 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
197 locked = 1;
198
199 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
200
201 return ret;
202}
203
204static ssize_t power_ro_lock_store(struct device *dev,
205 struct device_attribute *attr, const char *buf, size_t count)
206{
207 int ret;
208 struct mmc_blk_data *md, *part_md;
209 struct mmc_card *card;
210 unsigned long set;
211
212 if (kstrtoul(buf, 0, &set))
213 return -EINVAL;
214
215 if (set != 1)
216 return count;
217
218 md = mmc_blk_get(dev_to_disk(dev));
219 card = md->queue.card;
220
221 mmc_claim_host(card->host);
222
223 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
224 card->ext_csd.boot_ro_lock |
225 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
226 card->ext_csd.part_time);
227 if (ret)
228 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
229 else
230 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
231
232 mmc_release_host(card->host);
233
234 if (!ret) {
235 pr_info("%s: Locking boot partition ro until next power on\n",
236 md->disk->disk_name);
237 set_disk_ro(md->disk, 1);
238
239 list_for_each_entry(part_md, &md->part, part)
240 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
241 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
242 set_disk_ro(part_md->disk, 1);
243 }
244 }
245
246 mmc_blk_put(md);
247 return count;
248}
249
Andrei Warkentin371a6892011-04-11 18:10:25 -0500250static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
251 char *buf)
252{
253 int ret;
254 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
255
256 ret = snprintf(buf, PAGE_SIZE, "%d",
257 get_disk_ro(dev_to_disk(dev)) ^
258 md->read_only);
259 mmc_blk_put(md);
260 return ret;
261}
262
263static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
264 const char *buf, size_t count)
265{
266 int ret;
267 char *end;
268 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
269 unsigned long set = simple_strtoul(buf, &end, 0);
270 if (end == buf) {
271 ret = -EINVAL;
272 goto out;
273 }
274
275 set_disk_ro(dev_to_disk(dev), set || md->read_only);
276 ret = count;
277out:
278 mmc_blk_put(md);
279 return ret;
280}
281
Al Viroa5a15612008-03-02 10:33:30 -0500282static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Al Viroa5a15612008-03-02 10:33:30 -0500284 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 int ret = -ENXIO;
286
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200287 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (md) {
289 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500290 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700292
Al Viroa5a15612008-03-02 10:33:30 -0500293 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700294 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700295 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200298 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 return ret;
301}
302
Al Viroa5a15612008-03-02 10:33:30 -0500303static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Al Viroa5a15612008-03-02 10:33:30 -0500305 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200307 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200309 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return 0;
311}
312
313static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800314mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800316 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
317 geo->heads = 4;
318 geo->sectors = 16;
319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
John Calixtocb87ea22011-04-26 18:56:29 -0400322struct mmc_blk_ioc_data {
323 struct mmc_ioc_cmd ic;
324 unsigned char *buf;
325 u64 buf_bytes;
326};
327
328static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
329 struct mmc_ioc_cmd __user *user)
330{
331 struct mmc_blk_ioc_data *idata;
332 int err;
333
334 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
335 if (!idata) {
336 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400337 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400338 }
339
340 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
341 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400342 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400343 }
344
345 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
346 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
347 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400348 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400349 }
350
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100351 if (!idata->buf_bytes)
352 return idata;
353
John Calixtocb87ea22011-04-26 18:56:29 -0400354 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
355 if (!idata->buf) {
356 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400357 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400358 }
359
360 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
361 idata->ic.data_ptr, idata->buf_bytes)) {
362 err = -EFAULT;
363 goto copy_err;
364 }
365
366 return idata;
367
368copy_err:
369 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400370idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400371 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400372out:
John Calixtocb87ea22011-04-26 18:56:29 -0400373 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400374}
375
376static int mmc_blk_ioctl_cmd(struct block_device *bdev,
377 struct mmc_ioc_cmd __user *ic_ptr)
378{
379 struct mmc_blk_ioc_data *idata;
380 struct mmc_blk_data *md;
381 struct mmc_card *card;
382 struct mmc_command cmd = {0};
383 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530384 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400385 struct scatterlist sg;
386 int err;
387
388 /*
389 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
390 * whole block device, not on a partition. This prevents overspray
391 * between sibling partitions.
392 */
393 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
394 return -EPERM;
395
396 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
397 if (IS_ERR(idata))
398 return PTR_ERR(idata);
399
John Calixtocb87ea22011-04-26 18:56:29 -0400400 md = mmc_blk_get(bdev->bd_disk);
401 if (!md) {
402 err = -EINVAL;
403 goto cmd_done;
404 }
405
406 card = md->queue.card;
407 if (IS_ERR(card)) {
408 err = PTR_ERR(card);
409 goto cmd_done;
410 }
411
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100412 cmd.opcode = idata->ic.opcode;
413 cmd.arg = idata->ic.arg;
414 cmd.flags = idata->ic.flags;
415
416 if (idata->buf_bytes) {
417 data.sg = &sg;
418 data.sg_len = 1;
419 data.blksz = idata->ic.blksz;
420 data.blocks = idata->ic.blocks;
421
422 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
423
424 if (idata->ic.write_flag)
425 data.flags = MMC_DATA_WRITE;
426 else
427 data.flags = MMC_DATA_READ;
428
429 /* data.flags must already be set before doing this. */
430 mmc_set_data_timeout(&data, card);
431
432 /* Allow overriding the timeout_ns for empirical tuning. */
433 if (idata->ic.data_timeout_ns)
434 data.timeout_ns = idata->ic.data_timeout_ns;
435
436 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
437 /*
438 * Pretend this is a data transfer and rely on the
439 * host driver to compute timeout. When all host
440 * drivers support cmd.cmd_timeout for R1B, this
441 * can be changed to:
442 *
443 * mrq.data = NULL;
444 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
445 */
446 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
447 }
448
449 mrq.data = &data;
450 }
451
452 mrq.cmd = &cmd;
453
John Calixtocb87ea22011-04-26 18:56:29 -0400454 mmc_claim_host(card->host);
455
456 if (idata->ic.is_acmd) {
457 err = mmc_app_cmd(card->host, card);
458 if (err)
459 goto cmd_rel_host;
460 }
461
John Calixtocb87ea22011-04-26 18:56:29 -0400462 mmc_wait_for_req(card->host, &mrq);
463
464 if (cmd.error) {
465 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
466 __func__, cmd.error);
467 err = cmd.error;
468 goto cmd_rel_host;
469 }
470 if (data.error) {
471 dev_err(mmc_dev(card->host), "%s: data error %d\n",
472 __func__, data.error);
473 err = data.error;
474 goto cmd_rel_host;
475 }
476
477 /*
478 * According to the SD specs, some commands require a delay after
479 * issuing the command.
480 */
481 if (idata->ic.postsleep_min_us)
482 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
483
484 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
485 err = -EFAULT;
486 goto cmd_rel_host;
487 }
488
489 if (!idata->ic.write_flag) {
490 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
491 idata->buf, idata->buf_bytes)) {
492 err = -EFAULT;
493 goto cmd_rel_host;
494 }
495 }
496
497cmd_rel_host:
498 mmc_release_host(card->host);
499
500cmd_done:
501 mmc_blk_put(md);
502 kfree(idata->buf);
503 kfree(idata);
504 return err;
505}
506
507static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
508 unsigned int cmd, unsigned long arg)
509{
510 int ret = -EINVAL;
511 if (cmd == MMC_IOC_CMD)
512 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
513 return ret;
514}
515
516#ifdef CONFIG_COMPAT
517static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
518 unsigned int cmd, unsigned long arg)
519{
520 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
521}
522#endif
523
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700524static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500525 .open = mmc_blk_open,
526 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800527 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400529 .ioctl = mmc_blk_ioctl,
530#ifdef CONFIG_COMPAT
531 .compat_ioctl = mmc_blk_compat_ioctl,
532#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533};
534
Andrei Warkentin371a6892011-04-11 18:10:25 -0500535static inline int mmc_blk_part_switch(struct mmc_card *card,
536 struct mmc_blk_data *md)
537{
538 int ret;
539 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300540
Andrei Warkentin371a6892011-04-11 18:10:25 -0500541 if (main_md->part_curr == md->part_type)
542 return 0;
543
544 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300545 u8 part_config = card->ext_csd.part_config;
546
547 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
548 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500549
550 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300551 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500552 card->ext_csd.part_time);
553 if (ret)
554 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300555
556 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300557 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500558
559 main_md->part_curr = md->part_type;
560 return 0;
561}
562
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700563static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
564{
565 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100566 u32 result;
567 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700568
Venkatraman Sad5fd972011-08-25 00:30:50 +0530569 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400570 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400571 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700572
573 struct scatterlist sg;
574
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700575 cmd.opcode = MMC_APP_CMD;
576 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700577 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700578
579 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700580 if (err)
581 return (u32)-1;
582 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700583 return (u32)-1;
584
585 memset(&cmd, 0, sizeof(struct mmc_command));
586
587 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
588 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700589 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700590
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700591 data.blksz = 4;
592 data.blocks = 1;
593 data.flags = MMC_DATA_READ;
594 data.sg = &sg;
595 data.sg_len = 1;
Subhash Jadavani0126ae22012-06-12 14:46:06 +0530596 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700597
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700598 mrq.cmd = &cmd;
599 mrq.data = &data;
600
Ben Dooks051913d2009-06-08 23:33:57 +0100601 blocks = kmalloc(4, GFP_KERNEL);
602 if (!blocks)
603 return (u32)-1;
604
605 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700606
607 mmc_wait_for_req(card->host, &mrq);
608
Ben Dooks051913d2009-06-08 23:33:57 +0100609 result = ntohl(*blocks);
610 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700611
Ben Dooks051913d2009-06-08 23:33:57 +0100612 if (cmd.error || data.error)
613 result = (u32)-1;
614
615 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700616}
617
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100618static int send_stop(struct mmc_card *card, u32 *status)
619{
620 struct mmc_command cmd = {0};
621 int err;
622
623 cmd.opcode = MMC_STOP_TRANSMISSION;
624 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
625 err = mmc_wait_for_cmd(card->host, &cmd, 5);
626 if (err == 0)
627 *status = cmd.resp[0];
628 return err;
629}
630
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100631static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300632{
Chris Ball1278dba2011-04-13 23:40:30 -0400633 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300634 int err;
635
Adrian Hunter504f1912008-10-16 12:55:25 +0300636 cmd.opcode = MMC_SEND_STATUS;
637 if (!mmc_host_is_spi(card->host))
638 cmd.arg = card->rca << 16;
639 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100640 err = mmc_wait_for_cmd(card->host, &cmd, retries);
641 if (err == 0)
642 *status = cmd.resp[0];
643 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300644}
645
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530646#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100647#define ERR_RETRY 2
648#define ERR_ABORT 1
649#define ERR_CONTINUE 0
650
651static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
652 bool status_valid, u32 status)
653{
654 switch (error) {
655 case -EILSEQ:
656 /* response crc error, retry the r/w cmd */
657 pr_err("%s: %s sending %s command, card status %#x\n",
658 req->rq_disk->disk_name, "response CRC error",
659 name, status);
660 return ERR_RETRY;
661
662 case -ETIMEDOUT:
663 pr_err("%s: %s sending %s command, card status %#x\n",
664 req->rq_disk->disk_name, "timed out", name, status);
665
666 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700667 if (!status_valid) {
668 pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100669 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700670 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100671 /*
672 * If it was a r/w cmd crc error, or illegal command
673 * (eg, issued in wrong state) then retry - we should
674 * have corrected the state problem above.
675 */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700676 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
677 pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100678 return ERR_RETRY;
Ken Sumrall15ce8292011-10-25 18:16:58 -0700679 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100680
681 /* Otherwise abort the command */
Ken Sumrall15ce8292011-10-25 18:16:58 -0700682 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100683 return ERR_ABORT;
684
685 default:
686 /* We don't understand the error code the driver gave us */
687 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
688 req->rq_disk->disk_name, error, status);
689 return ERR_ABORT;
690 }
691}
692
693/*
694 * Initial r/w and stop cmd error recovery.
695 * We don't know whether the card received the r/w cmd or not, so try to
696 * restore things back to a sane state. Essentially, we do this as follows:
697 * - Obtain card status. If the first attempt to obtain card status fails,
698 * the status word will reflect the failed status cmd, not the failed
699 * r/w cmd. If we fail to obtain card status, it suggests we can no
700 * longer communicate with the card.
701 * - Check the card state. If the card received the cmd but there was a
702 * transient problem with the response, it might still be in a data transfer
703 * mode. Try to send it a stop command. If this fails, we can't recover.
704 * - If the r/w cmd failed due to a response CRC error, it was probably
705 * transient, so retry the cmd.
706 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
707 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
708 * illegal cmd, retry.
709 * Otherwise we don't understand what happened, so abort.
710 */
711static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
Adrian Hunter67716322011-08-29 16:42:15 +0300712 struct mmc_blk_request *brq, int *ecc_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100713{
714 bool prev_cmd_status_valid = true;
715 u32 status, stop_status = 0;
716 int err, retry;
717
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530718 if (mmc_card_removed(card))
719 return ERR_NOMEDIUM;
720
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100721 /*
722 * Try to get card status which indicates both the card state
723 * and why there was no response. If the first attempt fails,
724 * we can't be sure the returned status is for the r/w command.
725 */
726 for (retry = 2; retry >= 0; retry--) {
727 err = get_card_status(card, &status, 0);
728 if (!err)
729 break;
730
731 prev_cmd_status_valid = false;
732 pr_err("%s: error %d sending status command, %sing\n",
733 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
734 }
735
736 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530737 if (err) {
738 /* Check if the card is removed */
739 if (mmc_detect_card_removed(card->host))
740 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100741 return ERR_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +0530742 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100743
Adrian Hunter67716322011-08-29 16:42:15 +0300744 /* Flag ECC errors */
745 if ((status & R1_CARD_ECC_FAILED) ||
746 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
747 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
748 *ecc_err = 1;
749
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100750 /*
751 * Check the current card state. If it is in some data transfer
752 * mode, tell it to stop (and hopefully transition back to TRAN.)
753 */
754 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
755 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
756 err = send_stop(card, &stop_status);
757 if (err)
758 pr_err("%s: error %d sending stop command\n",
759 req->rq_disk->disk_name, err);
760
761 /*
762 * If the stop cmd also timed out, the card is probably
763 * not present, so abort. Other errors are bad news too.
764 */
765 if (err)
766 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300767 if (stop_status & R1_CARD_ECC_FAILED)
768 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100769 }
770
771 /* Check for set block count errors */
772 if (brq->sbc.error)
773 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
774 prev_cmd_status_valid, status);
775
776 /* Check for r/w command errors */
777 if (brq->cmd.error)
778 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
779 prev_cmd_status_valid, status);
780
Adrian Hunter67716322011-08-29 16:42:15 +0300781 /* Data errors */
782 if (!brq->stop.error)
783 return ERR_CONTINUE;
784
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100785 /* Now for stop errors. These aren't fatal to the transfer. */
786 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
787 req->rq_disk->disk_name, brq->stop.error,
788 brq->cmd.resp[0], status);
789
790 /*
791 * Subsitute in our own stop status as this will give the error
792 * state which happened during the execution of the r/w command.
793 */
794 if (stop_status) {
795 brq->stop.resp[0] = stop_status;
796 brq->stop.error = 0;
797 }
798 return ERR_CONTINUE;
799}
800
Adrian Hunter67716322011-08-29 16:42:15 +0300801static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
802 int type)
803{
804 int err;
805
806 if (md->reset_done & type)
807 return -EEXIST;
808
809 md->reset_done |= type;
810 err = mmc_hw_reset(host);
811 /* Ensure we switch back to the correct partition */
812 if (err != -EOPNOTSUPP) {
813 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
814 int part_err;
815
816 main_md->part_curr = main_md->part_type;
817 part_err = mmc_blk_part_switch(host->card, md);
818 if (part_err) {
819 /*
820 * We have failed to get back into the correct
821 * partition, so we need to abort the whole request.
822 */
823 return -ENODEV;
824 }
825 }
826 return err;
827}
828
829static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
830{
831 md->reset_done &= ~type;
832}
833
Adrian Hunterbd788c92010-08-11 14:17:47 -0700834static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
835{
836 struct mmc_blk_data *md = mq->data;
837 struct mmc_card *card = md->queue.card;
838 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300839 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700840
Adrian Hunterbd788c92010-08-11 14:17:47 -0700841 if (!mmc_can_erase(card)) {
842 err = -EOPNOTSUPP;
843 goto out;
844 }
845
846 from = blk_rq_pos(req);
847 nr = blk_rq_sectors(req);
848
Kyungmin Parkb3bf9152011-10-18 09:34:04 +0900849 if (mmc_can_discard(card))
850 arg = MMC_DISCARD_ARG;
851 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -0700852 arg = MMC_TRIM_ARG;
853 else
854 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300855retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500856 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
857 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
858 INAND_CMD38_ARG_EXT_CSD,
859 arg == MMC_TRIM_ARG ?
860 INAND_CMD38_ARG_TRIM :
861 INAND_CMD38_ARG_ERASE,
862 0);
863 if (err)
864 goto out;
865 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700866 err = mmc_erase(card, from, nr, arg);
867out:
Adrian Hunter67716322011-08-29 16:42:15 +0300868 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
869 goto retry;
870 if (!err)
871 mmc_blk_reset_success(md, type);
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530872 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -0700873
Adrian Hunterbd788c92010-08-11 14:17:47 -0700874 return err ? 0 : 1;
875}
876
Adrian Hunter49804542010-08-11 14:17:50 -0700877static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
878 struct request *req)
879{
880 struct mmc_blk_data *md = mq->data;
881 struct mmc_card *card = md->queue.card;
882 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300883 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700884
Maya Erez463bb952012-05-24 23:46:29 +0300885 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700886 err = -EOPNOTSUPP;
887 goto out;
888 }
889
890 from = blk_rq_pos(req);
891 nr = blk_rq_sectors(req);
892
893 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
894 arg = MMC_SECURE_TRIM1_ARG;
895 else
896 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300897retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500898 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
899 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
900 INAND_CMD38_ARG_EXT_CSD,
901 arg == MMC_SECURE_TRIM1_ARG ?
902 INAND_CMD38_ARG_SECTRIM1 :
903 INAND_CMD38_ARG_SECERASE,
904 0);
905 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300906 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500907 }
Adrian Hunter28302812012-04-05 14:45:48 +0300908
Adrian Hunter49804542010-08-11 14:17:50 -0700909 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +0300910 if (err == -EIO)
911 goto out_retry;
912 if (err)
913 goto out;
914
915 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500916 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
917 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
918 INAND_CMD38_ARG_EXT_CSD,
919 INAND_CMD38_ARG_SECTRIM2,
920 0);
921 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +0300922 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500923 }
Adrian Hunter28302812012-04-05 14:45:48 +0300924
Adrian Hunter49804542010-08-11 14:17:50 -0700925 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +0300926 if (err == -EIO)
927 goto out_retry;
928 if (err)
929 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500930 }
Adrian Hunter28302812012-04-05 14:45:48 +0300931
932 if (mmc_can_sanitize(card))
933 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
934 EXT_CSD_SANITIZE_START, 1, 0);
935out_retry:
936 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +0300937 goto retry;
938 if (!err)
939 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +0300940out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530941 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -0700942
Adrian Hunter49804542010-08-11 14:17:50 -0700943 return err ? 0 : 1;
944}
945
Maya Erez463bb952012-05-24 23:46:29 +0300946static int mmc_blk_issue_sanitize_rq(struct mmc_queue *mq,
947 struct request *req)
948{
949 struct mmc_blk_data *md = mq->data;
950 struct mmc_card *card = md->queue.card;
951 int err = 0;
952
953 BUG_ON(!card);
954 BUG_ON(!card->host);
955
956 if (!(mmc_can_sanitize(card) &&
957 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
958 pr_warning("%s: %s - SANITIZE is not supported\n",
959 mmc_hostname(card->host), __func__);
960 err = -EOPNOTSUPP;
961 goto out;
962 }
963
964 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
965 mmc_hostname(card->host), __func__);
966
967 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Yaniv Gardi7fc4a6a2012-05-29 21:10:55 +0300968 EXT_CSD_SANITIZE_START, 1,
969 MMC_SANITIZE_REQ_TIMEOUT);
Maya Erez463bb952012-05-24 23:46:29 +0300970
971 if (err)
972 pr_err("%s: %s - mmc_switch() with "
973 "EXT_CSD_SANITIZE_START failed. err=%d\n",
974 mmc_hostname(card->host), __func__, err);
975
976 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
977 __func__);
978
979out:
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530980 blk_end_request(req, err, blk_rq_bytes(req));
Maya Erez463bb952012-05-24 23:46:29 +0300981
982 return err ? 0 : 1;
983}
984
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500985static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
986{
987 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900988 struct mmc_card *card = md->queue.card;
989 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500990
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900991 ret = mmc_flush_cache(card);
992 if (ret)
993 ret = -EIO;
994
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +0530995 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500996
Seungwon Jeon881d1c22011-10-14 14:03:21 +0900997 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -0500998}
999
1000/*
1001 * Reformat current write as a reliable write, supporting
1002 * both legacy and the enhanced reliable write MMC cards.
1003 * In each transfer we'll handle only as much as a single
1004 * reliable write can handle, thus finish the request in
1005 * partial completions.
1006 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001007static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1008 struct mmc_card *card,
1009 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001010{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001011 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1012 /* Legacy mode imposes restrictions on transfers. */
1013 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1014 brq->data.blocks = 1;
1015
1016 if (brq->data.blocks > card->ext_csd.rel_sectors)
1017 brq->data.blocks = card->ext_csd.rel_sectors;
1018 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1019 brq->data.blocks = 1;
1020 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001021}
1022
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001023#define CMD_ERRORS \
1024 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1025 R1_ADDRESS_ERROR | /* Misaligned address */ \
1026 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1027 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1028 R1_CC_ERROR | /* Card controller error */ \
1029 R1_ERROR) /* General/unknown error */
1030
Per Forlinee8a43a2011-07-01 18:55:33 +02001031static int mmc_blk_err_check(struct mmc_card *card,
1032 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001033{
Per Forlinee8a43a2011-07-01 18:55:33 +02001034 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1035 mmc_active);
1036 struct mmc_blk_request *brq = &mq_mrq->brq;
1037 struct request *req = mq_mrq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001038 int ecc_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001039
1040 /*
1041 * sbc.error indicates a problem with the set block count
1042 * command. No data will have been transferred.
1043 *
1044 * cmd.error indicates a problem with the r/w command. No
1045 * data will have been transferred.
1046 *
1047 * stop.error indicates a problem with the stop command. Data
1048 * may have been transferred, or may still be transferring.
1049 */
Adrian Hunter67716322011-08-29 16:42:15 +03001050 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1051 brq->data.error) {
1052 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001053 case ERR_RETRY:
1054 return MMC_BLK_RETRY;
1055 case ERR_ABORT:
1056 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301057 case ERR_NOMEDIUM:
1058 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001059 case ERR_CONTINUE:
1060 break;
1061 }
1062 }
1063
1064 /*
1065 * Check for errors relating to the execution of the
1066 * initial command - such as address errors. No data
1067 * has been transferred.
1068 */
1069 if (brq->cmd.resp[0] & CMD_ERRORS) {
1070 pr_err("%s: r/w command failed, status = %#x\n",
1071 req->rq_disk->disk_name, brq->cmd.resp[0]);
1072 return MMC_BLK_ABORT;
1073 }
1074
1075 /*
1076 * Everything else is either success, or a data error of some
1077 * kind. If it was a write, we may have transitioned to
1078 * program mode, which we have to wait for it to complete.
1079 */
1080 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1081 u32 status;
1082 do {
1083 int err = get_card_status(card, &status, 5);
1084 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301085 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001086 req->rq_disk->disk_name, err);
1087 return MMC_BLK_CMD_ERR;
1088 }
1089 /*
1090 * Some cards mishandle the status bits,
1091 * so make sure to check both the busy
1092 * indication and the card state.
1093 */
1094 } while (!(status & R1_READY_FOR_DATA) ||
1095 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1096 }
1097
1098 if (brq->data.error) {
1099 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1100 req->rq_disk->disk_name, brq->data.error,
1101 (unsigned)blk_rq_pos(req),
1102 (unsigned)blk_rq_sectors(req),
1103 brq->cmd.resp[0], brq->stop.resp[0]);
1104
1105 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001106 if (ecc_err)
1107 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001108 return MMC_BLK_DATA_ERR;
1109 } else {
1110 return MMC_BLK_CMD_ERR;
1111 }
1112 }
1113
Adrian Hunter67716322011-08-29 16:42:15 +03001114 if (!brq->data.bytes_xfered)
1115 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001116
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001117 if (mq_mrq->packed_cmd != MMC_PACKED_NONE) {
1118 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1119 return MMC_BLK_PARTIAL;
1120 else
1121 return MMC_BLK_SUCCESS;
1122 }
1123
Adrian Hunter67716322011-08-29 16:42:15 +03001124 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1125 return MMC_BLK_PARTIAL;
1126
1127 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001128}
1129
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001130static int mmc_blk_packed_err_check(struct mmc_card *card,
1131 struct mmc_async_req *areq)
1132{
1133 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1134 mmc_active);
1135 struct request *req = mq_rq->req;
1136 int err, check, status;
1137 u8 ext_csd[512];
1138
1139 mq_rq->packed_retries--;
1140 check = mmc_blk_err_check(card, areq);
1141 err = get_card_status(card, &status, 0);
1142 if (err) {
1143 pr_err("%s: error %d sending status command\n",
1144 req->rq_disk->disk_name, err);
1145 return MMC_BLK_ABORT;
1146 }
1147
1148 if (status & R1_EXCEPTION_EVENT) {
1149 err = mmc_send_ext_csd(card, ext_csd);
1150 if (err) {
1151 pr_err("%s: error %d sending ext_csd\n",
1152 req->rq_disk->disk_name, err);
1153 return MMC_BLK_ABORT;
1154 }
1155
1156 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1157 EXT_CSD_PACKED_FAILURE) &&
1158 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1159 EXT_CSD_PACKED_GENERIC_ERROR)) {
1160 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1161 EXT_CSD_PACKED_INDEXED_ERROR) {
1162 mq_rq->packed_fail_idx =
1163 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1164 return MMC_BLK_PARTIAL;
1165 }
1166 }
1167 }
1168
1169 return check;
1170}
1171
Per Forlin54d49d772011-07-01 18:55:29 +02001172static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1173 struct mmc_card *card,
1174 int disable_multi,
1175 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Per Forlin54d49d772011-07-01 18:55:29 +02001177 u32 readcmd, writecmd;
1178 struct mmc_blk_request *brq = &mqrq->brq;
1179 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301181 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001183 /*
1184 * Reliable writes are used to implement Forced Unit Access and
1185 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001186 *
1187 * XXX: this really needs a good explanation of why REQ_META
1188 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001189 */
1190 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1191 (req->cmd_flags & REQ_META)) &&
1192 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001193 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001194
Per Forlin54d49d772011-07-01 18:55:29 +02001195 memset(brq, 0, sizeof(struct mmc_blk_request));
1196 brq->mrq.cmd = &brq->cmd;
1197 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Per Forlin54d49d772011-07-01 18:55:29 +02001199 brq->cmd.arg = blk_rq_pos(req);
1200 if (!mmc_card_blockaddr(card))
1201 brq->cmd.arg <<= 9;
1202 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1203 brq->data.blksz = 512;
1204 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1205 brq->stop.arg = 0;
1206 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1207 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Asutosh Das1a897142012-07-27 18:10:19 +05301209 brq->data.fault_injected = false;
Per Forlin54d49d772011-07-01 18:55:29 +02001210 /*
1211 * The block layer doesn't support all sector count
1212 * restrictions, so we need to be prepared for too big
1213 * requests.
1214 */
1215 if (brq->data.blocks > card->host->max_blk_count)
1216 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001218 if (brq->data.blocks > 1) {
1219 /*
1220 * After a read error, we redo the request one sector
1221 * at a time in order to accurately determine which
1222 * sectors can be read successfully.
1223 */
1224 if (disable_multi)
1225 brq->data.blocks = 1;
1226
1227 /* Some controllers can't do multiblock reads due to hw bugs */
1228 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1229 rq_data_dir(req) == READ)
1230 brq->data.blocks = 1;
1231 }
Per Forlin54d49d772011-07-01 18:55:29 +02001232
1233 if (brq->data.blocks > 1 || do_rel_wr) {
1234 /* SPI multiblock writes terminate using a special
1235 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001236 */
Per Forlin54d49d772011-07-01 18:55:29 +02001237 if (!mmc_host_is_spi(card->host) ||
1238 rq_data_dir(req) == READ)
1239 brq->mrq.stop = &brq->stop;
1240 readcmd = MMC_READ_MULTIPLE_BLOCK;
1241 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1242 } else {
1243 brq->mrq.stop = NULL;
1244 readcmd = MMC_READ_SINGLE_BLOCK;
1245 writecmd = MMC_WRITE_BLOCK;
1246 }
1247 if (rq_data_dir(req) == READ) {
1248 brq->cmd.opcode = readcmd;
1249 brq->data.flags |= MMC_DATA_READ;
1250 } else {
1251 brq->cmd.opcode = writecmd;
1252 brq->data.flags |= MMC_DATA_WRITE;
1253 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001254
Per Forlin54d49d772011-07-01 18:55:29 +02001255 if (do_rel_wr)
1256 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001257
Per Forlin54d49d772011-07-01 18:55:29 +02001258 /*
Saugata Das42659002011-12-21 13:09:17 +05301259 * Data tag is used only during writing meta data to speed
1260 * up write and any subsequent read of this meta data
1261 */
1262 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1263 (req->cmd_flags & REQ_META) &&
1264 (rq_data_dir(req) == WRITE) &&
1265 ((brq->data.blocks * brq->data.blksz) >=
1266 card->ext_csd.data_tag_unit_size);
1267
1268 /*
Per Forlin54d49d772011-07-01 18:55:29 +02001269 * Pre-defined multi-block transfers are preferable to
1270 * open ended-ones (and necessary for reliable writes).
1271 * However, it is not sufficient to just send CMD23,
1272 * and avoid the final CMD12, as on an error condition
1273 * CMD12 (stop) needs to be sent anyway. This, coupled
1274 * with Auto-CMD23 enhancements provided by some
1275 * hosts, means that the complexity of dealing
1276 * with this is best left to the host. If CMD23 is
1277 * supported by card and host, we'll fill sbc in and let
1278 * the host deal with handling it correctly. This means
1279 * that for hosts that don't expose MMC_CAP_CMD23, no
1280 * change of behavior will be observed.
1281 *
1282 * N.B: Some MMC cards experience perf degradation.
1283 * We'll avoid using CMD23-bounded multiblock writes for
1284 * these, while retaining features like reliable writes.
1285 */
Saugata Das42659002011-12-21 13:09:17 +05301286 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1287 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1288 do_data_tag)) {
Per Forlin54d49d772011-07-01 18:55:29 +02001289 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1290 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301291 (do_rel_wr ? (1 << 31) : 0) |
1292 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d772011-07-01 18:55:29 +02001293 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1294 brq->mrq.sbc = &brq->sbc;
1295 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001296
Per Forlin54d49d772011-07-01 18:55:29 +02001297 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001298
Per Forlin54d49d772011-07-01 18:55:29 +02001299 brq->data.sg = mqrq->sg;
1300 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001301
Per Forlin54d49d772011-07-01 18:55:29 +02001302 /*
1303 * Adjust the sg list so it is the same size as the
1304 * request.
1305 */
1306 if (brq->data.blocks != blk_rq_sectors(req)) {
1307 int i, data_size = brq->data.blocks << 9;
1308 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001309
Per Forlin54d49d772011-07-01 18:55:29 +02001310 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1311 data_size -= sg->length;
1312 if (data_size <= 0) {
1313 sg->length += data_size;
1314 i++;
1315 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001316 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001317 }
Per Forlin54d49d772011-07-01 18:55:29 +02001318 brq->data.sg_len = i;
1319 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001320
Per Forlinee8a43a2011-07-01 18:55:33 +02001321 mqrq->mmc_active.mrq = &brq->mrq;
1322 mqrq->mmc_active.err_check = mmc_blk_err_check;
1323
Per Forlin54d49d772011-07-01 18:55:29 +02001324 mmc_queue_bounce_pre(mqrq);
1325}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001327static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1328{
1329 struct request_queue *q = mq->queue;
1330 struct mmc_card *card = mq->card;
1331 struct request *cur = req, *next = NULL;
1332 struct mmc_blk_data *md = mq->data;
1333 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1334 unsigned int req_sectors = 0, phys_segments = 0;
1335 unsigned int max_blk_count, max_phys_segs;
1336 u8 put_back = 0;
1337 u8 max_packed_rw = 0;
1338 u8 reqs = 0;
1339
1340 mmc_blk_clear_packed(mq->mqrq_cur);
1341
1342 if (!(md->flags & MMC_BLK_CMD23) ||
1343 !card->ext_csd.packed_event_en)
1344 goto no_packed;
1345
1346 if ((rq_data_dir(cur) == WRITE) &&
1347 (card->host->caps2 & MMC_CAP2_PACKED_WR))
1348 max_packed_rw = card->ext_csd.max_packed_writes;
1349
1350 if (max_packed_rw == 0)
1351 goto no_packed;
1352
1353 if (mmc_req_rel_wr(cur) &&
1354 (md->flags & MMC_BLK_REL_WR) &&
1355 !en_rel_wr)
1356 goto no_packed;
1357
1358 if (mmc_large_sec(card) &&
1359 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1360 goto no_packed;
1361
1362 max_blk_count = min(card->host->max_blk_count,
1363 card->host->max_req_size >> 9);
1364 if (unlikely(max_blk_count > 0xffff))
1365 max_blk_count = 0xffff;
1366
1367 max_phys_segs = queue_max_segments(q);
1368 req_sectors += blk_rq_sectors(cur);
1369 phys_segments += cur->nr_phys_segments;
1370
1371 if (rq_data_dir(cur) == WRITE) {
1372 req_sectors++;
1373 phys_segments++;
1374 }
1375
1376 while (reqs < max_packed_rw - 1) {
1377 spin_lock_irq(q->queue_lock);
1378 next = blk_fetch_request(q);
1379 spin_unlock_irq(q->queue_lock);
1380 if (!next)
1381 break;
1382
1383 if (mmc_large_sec(card) &&
1384 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
1385 put_back = 1;
1386 break;
1387 }
1388
1389 if (next->cmd_flags & REQ_DISCARD ||
1390 next->cmd_flags & REQ_FLUSH) {
1391 put_back = 1;
1392 break;
1393 }
1394
1395 if (rq_data_dir(cur) != rq_data_dir(next)) {
1396 put_back = 1;
1397 break;
1398 }
1399
1400 if (mmc_req_rel_wr(next) &&
1401 (md->flags & MMC_BLK_REL_WR) &&
1402 !en_rel_wr) {
1403 put_back = 1;
1404 break;
1405 }
1406
1407 req_sectors += blk_rq_sectors(next);
1408 if (req_sectors > max_blk_count) {
1409 put_back = 1;
1410 break;
1411 }
1412
1413 phys_segments += next->nr_phys_segments;
1414 if (phys_segments > max_phys_segs) {
1415 put_back = 1;
1416 break;
1417 }
1418
1419 list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list);
1420 cur = next;
1421 reqs++;
1422 }
1423
1424 if (put_back) {
1425 spin_lock_irq(q->queue_lock);
1426 blk_requeue_request(q, next);
1427 spin_unlock_irq(q->queue_lock);
1428 }
1429
1430 if (reqs > 0) {
1431 list_add(&req->queuelist, &mq->mqrq_cur->packed_list);
1432 mq->mqrq_cur->packed_num = ++reqs;
1433 mq->mqrq_cur->packed_retries = reqs;
1434 return reqs;
1435 }
1436
1437no_packed:
1438 mmc_blk_clear_packed(mq->mqrq_cur);
1439 return 0;
1440}
1441
1442static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1443 struct mmc_card *card,
1444 struct mmc_queue *mq)
1445{
1446 struct mmc_blk_request *brq = &mqrq->brq;
1447 struct request *req = mqrq->req;
1448 struct request *prq;
1449 struct mmc_blk_data *md = mq->data;
1450 bool do_rel_wr, do_data_tag;
1451 u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr;
1452 u8 i = 1;
1453
1454 mqrq->packed_cmd = MMC_PACKED_WRITE;
1455 mqrq->packed_blocks = 0;
1456 mqrq->packed_fail_idx = MMC_PACKED_N_IDX;
1457
1458 memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr));
1459 packed_cmd_hdr[0] = (mqrq->packed_num << 16) |
1460 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1461
1462 /*
1463 * Argument for each entry of packed group
1464 */
1465 list_for_each_entry(prq, &mqrq->packed_list, queuelist) {
1466 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1467 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1468 (prq->cmd_flags & REQ_META) &&
1469 (rq_data_dir(prq) == WRITE) &&
1470 ((brq->data.blocks * brq->data.blksz) >=
1471 card->ext_csd.data_tag_unit_size);
1472 /* Argument of CMD23 */
1473 packed_cmd_hdr[(i * 2)] =
1474 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1475 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1476 blk_rq_sectors(prq);
1477 /* Argument of CMD18 or CMD25 */
1478 packed_cmd_hdr[((i * 2)) + 1] =
1479 mmc_card_blockaddr(card) ?
1480 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1481 mqrq->packed_blocks += blk_rq_sectors(prq);
1482 i++;
1483 }
1484
1485 memset(brq, 0, sizeof(struct mmc_blk_request));
1486 brq->mrq.cmd = &brq->cmd;
1487 brq->mrq.data = &brq->data;
1488 brq->mrq.sbc = &brq->sbc;
1489 brq->mrq.stop = &brq->stop;
1490
1491 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1492 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1);
1493 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1494
1495 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1496 brq->cmd.arg = blk_rq_pos(req);
1497 if (!mmc_card_blockaddr(card))
1498 brq->cmd.arg <<= 9;
1499 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1500
1501 brq->data.blksz = 512;
1502 brq->data.blocks = mqrq->packed_blocks + 1;
1503 brq->data.flags |= MMC_DATA_WRITE;
1504
1505 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1506 brq->stop.arg = 0;
1507 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1508
1509 mmc_set_data_timeout(&brq->data, card);
1510
1511 brq->data.sg = mqrq->sg;
1512 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1513
1514 mqrq->mmc_active.mrq = &brq->mrq;
1515 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1516
1517 mmc_queue_bounce_pre(mqrq);
1518}
1519
Adrian Hunter67716322011-08-29 16:42:15 +03001520static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1521 struct mmc_blk_request *brq, struct request *req,
1522 int ret)
1523{
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001524 struct mmc_queue_req *mq_rq;
1525 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1526
Adrian Hunter67716322011-08-29 16:42:15 +03001527 /*
1528 * If this is an SD card and we're writing, we can first
1529 * mark the known good sectors as ok.
1530 *
1531 * If the card is not SD, we can still ok written sectors
1532 * as reported by the controller (which might be less than
1533 * the real number of written sectors, but never more).
1534 */
1535 if (mmc_card_sd(card)) {
1536 u32 blocks;
Asutosh Das1a897142012-07-27 18:10:19 +05301537 if (!brq->data.fault_injected) {
1538 blocks = mmc_sd_num_wr_blocks(card);
1539 if (blocks != (u32)-1)
1540 ret = blk_end_request(req, 0, blocks << 9);
1541 } else
1542 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001543 } else {
1544 if (mq_rq->packed_cmd == MMC_PACKED_NONE)
1545 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1546 }
Seungwon Jeon968c7742012-05-31 11:54:47 +03001547 return ret;
1548}
1549
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001550static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1551{
1552 struct request *prq;
1553 int idx = mq_rq->packed_fail_idx, i = 0;
1554 int ret = 0;
1555
1556 while (!list_empty(&mq_rq->packed_list)) {
1557 prq = list_entry_rq(mq_rq->packed_list.next);
1558 if (idx == i) {
1559 /* retry from error index */
1560 mq_rq->packed_num -= idx;
1561 mq_rq->req = prq;
1562 ret = 1;
1563
1564 if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) {
1565 list_del_init(&prq->queuelist);
1566 mmc_blk_clear_packed(mq_rq);
1567 }
1568 return ret;
1569 }
1570 list_del_init(&prq->queuelist);
1571 blk_end_request(prq, 0, blk_rq_bytes(prq));
1572 i++;
1573 }
1574
1575 mmc_blk_clear_packed(mq_rq);
1576 return ret;
1577}
1578static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1579{
1580 struct request *prq;
1581
1582 while (!list_empty(&mq_rq->packed_list)) {
1583 prq = list_entry_rq(mq_rq->packed_list.next);
1584 list_del_init(&prq->queuelist);
1585 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1586 }
1587
1588 mmc_blk_clear_packed(mq_rq);
1589}
1590
1591static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1592 struct mmc_queue_req *mq_rq)
1593{
1594 struct request *prq;
1595 struct request_queue *q = mq->queue;
1596
1597 while (!list_empty(&mq_rq->packed_list)) {
1598 prq = list_entry_rq(mq_rq->packed_list.prev);
1599 if (prq->queuelist.prev != &mq_rq->packed_list) {
1600 list_del_init(&prq->queuelist);
1601 spin_lock_irq(q->queue_lock);
1602 blk_requeue_request(mq->queue, prq);
1603 spin_unlock_irq(q->queue_lock);
1604 } else {
1605 list_del_init(&prq->queuelist);
1606 }
1607 }
1608
1609 mmc_blk_clear_packed(mq_rq);
1610}
1611
Per Forlinee8a43a2011-07-01 18:55:33 +02001612static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d772011-07-01 18:55:29 +02001613{
1614 struct mmc_blk_data *md = mq->data;
1615 struct mmc_card *card = md->queue.card;
1616 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001617 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001618 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001619 struct mmc_queue_req *mq_rq;
Maya Erezb52c7182012-11-28 23:46:50 +02001620 struct request *req;
Per Forlinee8a43a2011-07-01 18:55:33 +02001621 struct mmc_async_req *areq;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001622 const u8 packed_num = 2;
1623 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001624
1625 if (!rqc && !mq->mqrq_prev->req)
1626 return 0;
Per Forlin54d49d772011-07-01 18:55:29 +02001627
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001628 if (rqc)
1629 reqs = mmc_blk_prep_packed_list(mq, rqc);
1630
Per Forlin54d49d772011-07-01 18:55:29 +02001631 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001632 if (rqc) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001633 if (reqs >= packed_num)
1634 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1635 card, mq);
1636 else
1637 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001638 areq = &mq->mqrq_cur->mmc_active;
1639 } else
1640 areq = NULL;
1641 areq = mmc_start_req(card->host, areq, (int *) &status);
1642 if (!areq)
1643 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +02001644
Per Forlinee8a43a2011-07-01 18:55:33 +02001645 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1646 brq = &mq_rq->brq;
1647 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001648 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001649 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001650
Per Forlind78d4a82011-07-01 18:55:30 +02001651 switch (status) {
1652 case MMC_BLK_SUCCESS:
1653 case MMC_BLK_PARTIAL:
1654 /*
1655 * A block was successfully transferred.
1656 */
Adrian Hunter67716322011-08-29 16:42:15 +03001657 mmc_blk_reset_success(md, type);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001658
1659 if (mq_rq->packed_cmd != MMC_PACKED_NONE) {
1660 ret = mmc_blk_end_packed_req(mq_rq);
1661 break;
1662 } else {
1663 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001664 brq->data.bytes_xfered);
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001665 }
1666
Adrian Hunter67716322011-08-29 16:42:15 +03001667 /*
1668 * If the blk_end_request function returns non-zero even
1669 * though all data has been transferred and no errors
1670 * were returned by the host controller, it's a bug.
1671 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001672 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301673 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001674 __func__, blk_rq_bytes(req),
1675 brq->data.bytes_xfered);
1676 rqc = NULL;
1677 goto cmd_abort;
1678 }
Per Forlind78d4a82011-07-01 18:55:30 +02001679 break;
1680 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001681 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1682 if (!mmc_blk_reset(md, card->host, type))
1683 break;
1684 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001685 case MMC_BLK_RETRY:
1686 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001687 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001688 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001689 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001690 if (!mmc_blk_reset(md, card->host, type))
1691 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001692 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001693 case MMC_BLK_DATA_ERR: {
1694 int err;
1695
1696 err = mmc_blk_reset(md, card->host, type);
1697 if (!err)
1698 break;
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001699 if (err == -ENODEV ||
1700 mq_rq->packed_cmd != MMC_PACKED_NONE)
Adrian Hunter67716322011-08-29 16:42:15 +03001701 goto cmd_abort;
1702 /* Fall through */
1703 }
1704 case MMC_BLK_ECC_ERR:
1705 if (brq->data.blocks > 1) {
1706 /* Redo read one sector at a time */
1707 pr_warning("%s: retrying using single block read\n",
1708 req->rq_disk->disk_name);
1709 disable_multi = 1;
1710 break;
1711 }
Per Forlind78d4a82011-07-01 18:55:30 +02001712 /*
1713 * After an error, we redo I/O one sector at a
1714 * time, so we only reach here after trying to
1715 * read a single sector.
1716 */
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301717 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001718 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001719 if (!ret)
1720 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001721 break;
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +05301722 case MMC_BLK_NOMEDIUM:
1723 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001724 }
1725
Per Forlinee8a43a2011-07-01 18:55:33 +02001726 if (ret) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001727 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1728 /*
1729 * In case of a incomplete request
1730 * prepare it again and resend.
1731 */
1732 mmc_blk_rw_rq_prep(mq_rq, card,
1733 disable_multi, mq);
1734 mmc_start_req(card->host,
1735 &mq_rq->mmc_active, NULL);
1736 } else {
1737 if (!mq_rq->packed_retries)
1738 goto cmd_abort;
1739 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1740 mmc_start_req(card->host,
1741 &mq_rq->mmc_active, NULL);
1742 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 } while (ret);
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 return 1;
1747
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001748 cmd_abort:
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001749 if (mq_rq->packed_cmd == MMC_PACKED_NONE) {
1750 if (mmc_card_removed(card))
1751 req->cmd_flags |= REQ_QUIET;
1752 while (ret)
1753 ret = blk_end_request(req, -EIO,
1754 blk_rq_cur_bytes(req));
1755 } else {
1756 mmc_blk_abort_packed_req(mq_rq);
1757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Per Forlinee8a43a2011-07-01 18:55:33 +02001759 start_new_req:
1760 if (rqc) {
Seungwon Jeon121b8e82012-09-27 15:00:26 +02001761 /*
1762 * If current request is packed, it needs to put back.
1763 */
1764 if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE)
1765 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1766
Per Forlinee8a43a2011-07-01 18:55:33 +02001767 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1768 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1769 }
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 return 0;
1772}
1773
Adrian Hunterbd788c92010-08-11 14:17:47 -07001774static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1775{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001776 int ret;
1777 struct mmc_blk_data *md = mq->data;
1778 struct mmc_card *card = md->queue.card;
1779
San Mehat148c57a2009-07-30 08:21:19 -07001780#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1781 if (mmc_bus_needs_resume(card->host)) {
1782 mmc_resume_bus(card->host);
1783 mmc_blk_set_blksize(md, card);
1784 }
1785#endif
1786
Per Forlinee8a43a2011-07-01 18:55:33 +02001787 if (req && !mq->mqrq_prev->req)
1788 /* claim host only for the first request */
1789 mmc_claim_host(card->host);
1790
Andrei Warkentin371a6892011-04-11 18:10:25 -05001791 ret = mmc_blk_part_switch(card, md);
1792 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001793 if (req) {
Subhash Jadavani4d63b1d2012-06-08 16:17:44 +05301794 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001795 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001796 ret = 0;
1797 goto out;
1798 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001799
Maya Erez463bb952012-05-24 23:46:29 +03001800 if (req && req->cmd_flags & REQ_SANITIZE) {
1801 /* complete ongoing async transfer before issuing sanitize */
1802 if (card->host && card->host->areq)
1803 mmc_blk_issue_rw_rq(mq, NULL);
1804 ret = mmc_blk_issue_sanitize_rq(mq, req);
1805 } else if (req && req->cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001806 /* complete ongoing async transfer before issuing discard */
1807 if (card->host->areq)
1808 mmc_blk_issue_rw_rq(mq, NULL);
Adrian Hunter49804542010-08-11 14:17:50 -07001809 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001810 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001811 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001812 ret = mmc_blk_issue_discard_rq(mq, req);
Per Forlinee8a43a2011-07-01 18:55:33 +02001813 } else if (req && req->cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001814 /* complete ongoing async transfer before issuing flush */
1815 if (card->host->areq)
1816 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001817 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001818 } else {
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001819 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001820 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001821
Andrei Warkentin371a6892011-04-11 18:10:25 -05001822out:
Per Forlinee8a43a2011-07-01 18:55:33 +02001823 if (!req)
1824 /* release host only when there are no more requests */
1825 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001826 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001827}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Russell Kinga6f6c962006-01-03 22:38:44 +00001829static inline int mmc_blk_readonly(struct mmc_card *card)
1830{
1831 return mmc_card_readonly(card) ||
1832 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1833}
1834
Andrei Warkentin371a6892011-04-11 18:10:25 -05001835static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1836 struct device *parent,
1837 sector_t size,
1838 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001839 const char *subname,
1840 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 struct mmc_blk_data *md;
1843 int devidx, ret;
1844
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001845 devidx = find_first_zero_bit(dev_use, max_devices);
1846 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return ERR_PTR(-ENOSPC);
1848 __set_bit(devidx, dev_use);
1849
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001850 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00001851 if (!md) {
1852 ret = -ENOMEM;
1853 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
Russell Kinga6f6c962006-01-03 22:38:44 +00001855
Russell Kinga6f6c962006-01-03 22:38:44 +00001856 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001857 * !subname implies we are creating main mmc_blk_data that will be
1858 * associated with mmc_card with mmc_set_drvdata. Due to device
1859 * partitions, devidx will not coincide with a per-physical card
1860 * index anymore so we keep track of a name index.
1861 */
1862 if (!subname) {
1863 md->name_idx = find_first_zero_bit(name_use, max_devices);
1864 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01001865 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001866 md->name_idx = ((struct mmc_blk_data *)
1867 dev_to_disk(parent)->private_data)->name_idx;
1868
Johan Rudholmadd710e2011-12-02 08:51:06 +01001869 md->area_type = area_type;
1870
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001871 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00001872 * Set the read-only status based on the supported commands
1873 * and the write protect switch.
1874 */
1875 md->read_only = mmc_blk_readonly(card);
1876
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001877 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00001878 if (md->disk == NULL) {
1879 ret = -ENOMEM;
1880 goto err_kfree;
1881 }
1882
1883 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001884 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00001885 md->usage = 1;
1886
Adrian Hunterd09408a2011-06-23 13:40:28 +03001887 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00001888 if (ret)
1889 goto err_putdisk;
1890
Russell Kinga6f6c962006-01-03 22:38:44 +00001891 md->queue.issue_fn = mmc_blk_issue_rq;
1892 md->queue.data = md;
1893
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02001894 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04001895 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00001896 md->disk->fops = &mmc_bdops;
1897 md->disk->private_data = md;
1898 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001899 md->disk->driverfs_dev = parent;
1900 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross71b54d42010-09-03 12:41:21 -07001901 md->disk->flags = GENHD_FL_EXT_DEVT;
Russell Kinga6f6c962006-01-03 22:38:44 +00001902
1903 /*
1904 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1905 *
1906 * - be set for removable media with permanent block devices
1907 * - be unset for removable block devices with permanent media
1908 *
1909 * Since MMC block devices clearly fall under the second
1910 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1911 * should use the block device creation/destruction hotplug
1912 * messages to tell when the card is present.
1913 */
1914
Andrei Warkentinf06c9152011-04-21 22:46:13 -05001915 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1916 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00001917
Martin K. Petersene1defc42009-05-22 17:17:49 -04001918 blk_queue_logical_block_size(md->queue.queue, 512);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001919 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001920
Andrei Warkentinf0d89972011-05-23 15:06:38 -05001921 if (mmc_host_cmd23(card->host)) {
1922 if (mmc_card_mmc(card) ||
1923 (mmc_card_sd(card) &&
1924 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1925 md->flags |= MMC_BLK_CMD23;
1926 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001927
1928 if (mmc_card_mmc(card) &&
1929 md->flags & MMC_BLK_CMD23 &&
1930 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1931 card->ext_csd.rel_sectors)) {
1932 md->flags |= MMC_BLK_REL_WR;
1933 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1934 }
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00001937
1938 err_putdisk:
1939 put_disk(md->disk);
1940 err_kfree:
1941 kfree(md);
1942 out:
1943 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
Andrei Warkentin371a6892011-04-11 18:10:25 -05001946static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1947{
1948 sector_t size;
1949 struct mmc_blk_data *md;
1950
1951 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1952 /*
1953 * The EXT_CSD sector count is in number or 512 byte
1954 * sectors.
1955 */
1956 size = card->ext_csd.sectors;
1957 } else {
1958 /*
1959 * The CSD capacity field is in units of read_blkbits.
1960 * set_capacity takes units of 512 bytes.
1961 */
1962 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1963 }
1964
Johan Rudholmadd710e2011-12-02 08:51:06 +01001965 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
1966 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001967 return md;
1968}
1969
1970static int mmc_blk_alloc_part(struct mmc_card *card,
1971 struct mmc_blk_data *md,
1972 unsigned int part_type,
1973 sector_t size,
1974 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001975 const char *subname,
1976 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05001977{
1978 char cap_str[10];
1979 struct mmc_blk_data *part_md;
1980
1981 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01001982 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001983 if (IS_ERR(part_md))
1984 return PTR_ERR(part_md);
1985 part_md->part_type = part_type;
1986 list_add(&part_md->part, &md->part);
1987
1988 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1989 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301990 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05001991 part_md->disk->disk_name, mmc_card_id(card),
1992 mmc_card_name(card), part_md->part_type, cap_str);
1993 return 0;
1994}
1995
Namjae Jeone0c368d2011-10-06 23:41:38 +09001996/* MMC Physical partitions consist of two boot partitions and
1997 * up to four general purpose partitions.
1998 * For each partition enabled in EXT_CSD a block device will be allocatedi
1999 * to provide access to the partition.
2000 */
2001
Andrei Warkentin371a6892011-04-11 18:10:25 -05002002static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2003{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002004 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002005
2006 if (!mmc_card_mmc(card))
2007 return 0;
2008
Namjae Jeone0c368d2011-10-06 23:41:38 +09002009 for (idx = 0; idx < card->nr_parts; idx++) {
2010 if (card->part[idx].size) {
2011 ret = mmc_blk_alloc_part(card, md,
2012 card->part[idx].part_cfg,
2013 card->part[idx].size >> 9,
2014 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002015 card->part[idx].name,
2016 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002017 if (ret)
2018 return ret;
2019 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002020 }
2021
2022 return ret;
2023}
2024
Andrei Warkentin371a6892011-04-11 18:10:25 -05002025static void mmc_blk_remove_req(struct mmc_blk_data *md)
2026{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002027 struct mmc_card *card;
2028
Andrei Warkentin371a6892011-04-11 18:10:25 -05002029 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002030 card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002031 if (md->disk->flags & GENHD_FL_UP) {
2032 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002033 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2034 card->ext_csd.boot_ro_lockable)
2035 device_remove_file(disk_to_dev(md->disk),
2036 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002037
2038 /* Stop new requests from getting into the queue */
2039 del_gendisk(md->disk);
2040 }
2041
2042 /* Then flush out any already in there */
2043 mmc_cleanup_queue(&md->queue);
2044 mmc_blk_put(md);
2045 }
2046}
2047
2048static void mmc_blk_remove_parts(struct mmc_card *card,
2049 struct mmc_blk_data *md)
2050{
2051 struct list_head *pos, *q;
2052 struct mmc_blk_data *part_md;
2053
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002054 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002055 list_for_each_safe(pos, q, &md->part) {
2056 part_md = list_entry(pos, struct mmc_blk_data, part);
2057 list_del(pos);
2058 mmc_blk_remove_req(part_md);
2059 }
2060}
2061
2062static int mmc_add_disk(struct mmc_blk_data *md)
2063{
2064 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002065 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002066
2067 add_disk(md->disk);
2068 md->force_ro.show = force_ro_show;
2069 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302070 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002071 md->force_ro.attr.name = "force_ro";
2072 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2073 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2074 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002075 goto force_ro_fail;
2076
2077 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2078 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002079 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002080
2081 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2082 mode = S_IRUGO;
2083 else
2084 mode = S_IRUGO | S_IWUSR;
2085
2086 md->power_ro_lock.show = power_ro_lock_show;
2087 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002088 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002089 md->power_ro_lock.attr.mode = mode;
2090 md->power_ro_lock.attr.name =
2091 "ro_lock_until_next_power_on";
2092 ret = device_create_file(disk_to_dev(md->disk),
2093 &md->power_ro_lock);
2094 if (ret)
2095 goto power_ro_lock_fail;
2096 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002097
Steve Mucklef132c6c2012-06-06 18:30:57 -07002098 if (ret)
2099 goto power_ro_lock_fail;
Maya Erez63c61d62012-05-31 21:00:18 +03002100
Johan Rudholmadd710e2011-12-02 08:51:06 +01002101 return ret;
2102
2103power_ro_lock_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002104 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002105force_ro_fail:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002106 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002107
2108 return ret;
2109}
2110
Chris Ballc59d4472011-11-11 22:01:43 -05002111#define CID_MANFID_SANDISK 0x2
2112#define CID_MANFID_TOSHIBA 0x11
2113#define CID_MANFID_MICRON 0x13
2114
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002115static const struct mmc_fixup blk_fixups[] =
2116{
Chris Ballc59d4472011-11-11 22:01:43 -05002117 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2118 MMC_QUIRK_INAND_CMD38),
2119 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2120 MMC_QUIRK_INAND_CMD38),
2121 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2122 MMC_QUIRK_INAND_CMD38),
2123 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2124 MMC_QUIRK_INAND_CMD38),
2125 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2126 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002127
2128 /*
2129 * Some MMC cards experience performance degradation with CMD23
2130 * instead of CMD12-bounded multiblock transfers. For now we'll
2131 * black list what's bad...
2132 * - Certain Toshiba cards.
2133 *
2134 * N.B. This doesn't affect SD cards.
2135 */
Chris Ballc59d4472011-11-11 22:01:43 -05002136 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002137 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002138 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002139 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002140 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002141 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002142
2143 /*
2144 * Some Micron MMC cards needs longer data read timeout than
2145 * indicated in CSD.
2146 */
Chris Ballc59d4472011-11-11 22:01:43 -05002147 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002148 MMC_QUIRK_LONG_READ_TIME),
2149
Pratibhasagar V3a18bbd2012-04-17 14:41:19 +05302150 /* Some INAND MCP devices advertise incorrect timeout values */
2151 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2152 MMC_QUIRK_INAND_DATA_TIMEOUT),
2153
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002154 END_FIXUP
2155};
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157static int mmc_blk_probe(struct mmc_card *card)
2158{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002159 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002160 char cap_str[10];
2161
Pierre Ossman912490d2005-05-21 10:27:02 +01002162 /*
2163 * Check that the card supports the command class(es) we need.
2164 */
2165 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return -ENODEV;
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 md = mmc_blk_alloc(card);
2169 if (IS_ERR(md))
2170 return PTR_ERR(md);
2171
Yi Li444122f2009-02-05 15:31:57 +08002172 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002173 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302174 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002176 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Andrei Warkentin371a6892011-04-11 18:10:25 -05002178 if (mmc_blk_alloc_parts(card, md))
2179 goto out;
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002182 mmc_fixup_device(card, blk_fixups);
2183
San Mehat148c57a2009-07-30 08:21:19 -07002184#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2185 mmc_set_bus_resume_policy(card->host, 1);
2186#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002187 if (mmc_add_disk(md))
2188 goto out;
2189
2190 list_for_each_entry(part_md, &md->part, part) {
2191 if (mmc_add_disk(part_md))
2192 goto out;
2193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 return 0;
2195
2196 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002197 mmc_blk_remove_parts(card, md);
2198 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
2201
2202static void mmc_blk_remove(struct mmc_card *card)
2203{
2204 struct mmc_blk_data *md = mmc_get_drvdata(card);
2205
Andrei Warkentin371a6892011-04-11 18:10:25 -05002206 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002207 mmc_claim_host(card->host);
2208 mmc_blk_part_switch(card, md);
2209 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002210 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 mmc_set_drvdata(card, NULL);
San Mehat148c57a2009-07-30 08:21:19 -07002212#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2213 mmc_set_bus_resume_policy(card->host, 0);
2214#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
2217#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002218static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002220 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 struct mmc_blk_data *md = mmc_get_drvdata(card);
2222
2223 if (md) {
2224 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002225 list_for_each_entry(part_md, &md->part, part) {
2226 mmc_queue_suspend(&part_md->queue);
2227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 }
2229 return 0;
2230}
2231
2232static int mmc_blk_resume(struct mmc_card *card)
2233{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002234 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 struct mmc_blk_data *md = mmc_get_drvdata(card);
2236
2237 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002238 /*
2239 * Resume involves the card going into idle state,
2240 * so current partition is always the main one.
2241 */
2242 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002244 list_for_each_entry(part_md, &md->part, part) {
2245 mmc_queue_resume(&part_md->queue);
2246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248 return 0;
2249}
2250#else
2251#define mmc_blk_suspend NULL
2252#define mmc_blk_resume NULL
2253#endif
2254
2255static struct mmc_driver mmc_driver = {
2256 .drv = {
2257 .name = "mmcblk",
2258 },
2259 .probe = mmc_blk_probe,
2260 .remove = mmc_blk_remove,
2261 .suspend = mmc_blk_suspend,
2262 .resume = mmc_blk_resume,
2263};
2264
2265static int __init mmc_blk_init(void)
2266{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002267 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002269 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2270 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2271
2272 max_devices = 256 / perdev_minors;
2273
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002274 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2275 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002278 res = mmc_register_driver(&mmc_driver);
2279 if (res)
2280 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002282 return 0;
2283 out2:
2284 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 out:
2286 return res;
2287}
2288
2289static void __exit mmc_blk_exit(void)
2290{
2291 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002292 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293}
2294
2295module_init(mmc_blk_init);
2296module_exit(mmc_blk_exit);
2297
2298MODULE_LICENSE("GPL");
2299MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2300