blob: fffa83359c677e35827da326e6cf749eea462b63 [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>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020037#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
John Calixtocb87ea22011-04-26 18:56:29 -040039#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020041#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010042#include <linux/mmc/mmc.h>
43#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/uaccess.h>
46
Pierre Ossman98ac2162006-12-23 20:03:02 +010047#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000049MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040050#ifdef MODULE_PARAM_PREFIX
51#undef MODULE_PARAM_PREFIX
52#endif
53#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010054
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050055#define INAND_CMD38_ARG_EXT_CSD 113
56#define INAND_CMD38_ARG_ERASE 0x00
57#define INAND_CMD38_ARG_TRIM 0x01
58#define INAND_CMD38_ARG_SECERASE 0x80
59#define INAND_CMD38_ARG_SECTRIM1 0x81
60#define INAND_CMD38_ARG_SECTRIM2 0x88
Trey Ramsay8fee4762012-11-16 09:31:41 -060061#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030062#define MMC_SANITIZE_REQ_TIMEOUT 240000
63#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050064
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090065#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
66 (req->cmd_flags & REQ_META)) && \
67 (rq_data_dir(req) == WRITE))
68#define PACKED_CMD_VER 0x01
69#define PACKED_CMD_WR 0x02
70
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020071static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040072
73/*
74 * The defaults come from config options but can be overriden by module
75 * or bootarg options.
76 */
77static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
78
79/*
80 * We've only got one major, so number of mmcblk devices is
81 * limited to 256 / number of minors per device.
82 */
83static int max_devices;
84
85/* 256 minors, so at most 256 separate devices */
86static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050087static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * There is one mmc_blk_data per slot.
91 */
92struct mmc_blk_data {
93 spinlock_t lock;
94 struct gendisk *disk;
95 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050096 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Andrei Warkentind0c97cf2011-05-23 15:06:36 -050098 unsigned int flags;
99#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
100#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900101#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000104 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500105 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500106 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300107 unsigned int reset_done;
108#define MMC_BLK_READ BIT(0)
109#define MMC_BLK_WRITE BIT(1)
110#define MMC_BLK_DISCARD BIT(2)
111#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500112
113 /*
114 * Only set in main mmc_blk_data associated
115 * with mmc_card with mmc_set_drvdata, and keeps
116 * track of the current selected device partition.
117 */
118 unsigned int part_curr;
119 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100120 struct device_attribute power_ro_lock;
121 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
Arjan van de Vena621aae2006-01-12 18:43:35 +0000124static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900126enum {
127 MMC_PACKED_NR_IDX = -1,
128 MMC_PACKED_NR_ZERO,
129 MMC_PACKED_NR_SINGLE,
130};
131
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400132module_param(perdev_minors, int, 0444);
133MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
134
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200135static inline int mmc_blk_part_switch(struct mmc_card *card,
136 struct mmc_blk_data *md);
137static int get_card_status(struct mmc_card *card, u32 *status, int retries);
138
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900139static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
140{
141 struct mmc_packed *packed = mqrq->packed;
142
143 BUG_ON(!packed);
144
145 mqrq->cmd_type = MMC_PACKED_NONE;
146 packed->nr_entries = MMC_PACKED_NR_ZERO;
147 packed->idx_failure = MMC_PACKED_NR_IDX;
148 packed->retries = 0;
149 packed->blocks = 0;
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
153{
154 struct mmc_blk_data *md;
155
Arjan van de Vena621aae2006-01-12 18:43:35 +0000156 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 md = disk->private_data;
158 if (md && md->usage == 0)
159 md = NULL;
160 if (md)
161 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000162 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 return md;
165}
166
Andrei Warkentin371a6892011-04-11 18:10:25 -0500167static inline int mmc_get_devidx(struct gendisk *disk)
168{
169 int devmaj = MAJOR(disk_devt(disk));
170 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
171
172 if (!devmaj)
173 devidx = disk->first_minor / perdev_minors;
174 return devidx;
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static void mmc_blk_put(struct mmc_blk_data *md)
178{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000179 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 md->usage--;
181 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500182 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800183 blk_cleanup_queue(md->queue.queue);
184
David Woodhouse1dff3142007-11-21 18:45:12 +0100185 __clear_bit(devidx, dev_use);
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 kfree(md);
189 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000190 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Johan Rudholmadd710e2011-12-02 08:51:06 +0100193static ssize_t power_ro_lock_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
195{
196 int ret;
197 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
198 struct mmc_card *card = md->queue.card;
199 int locked = 0;
200
201 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
202 locked = 2;
203 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
204 locked = 1;
205
206 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
207
208 return ret;
209}
210
211static ssize_t power_ro_lock_store(struct device *dev,
212 struct device_attribute *attr, const char *buf, size_t count)
213{
214 int ret;
215 struct mmc_blk_data *md, *part_md;
216 struct mmc_card *card;
217 unsigned long set;
218
219 if (kstrtoul(buf, 0, &set))
220 return -EINVAL;
221
222 if (set != 1)
223 return count;
224
225 md = mmc_blk_get(dev_to_disk(dev));
226 card = md->queue.card;
227
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200228 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100229
230 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
231 card->ext_csd.boot_ro_lock |
232 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
233 card->ext_csd.part_time);
234 if (ret)
235 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
236 else
237 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
238
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200239 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100240
241 if (!ret) {
242 pr_info("%s: Locking boot partition ro until next power on\n",
243 md->disk->disk_name);
244 set_disk_ro(md->disk, 1);
245
246 list_for_each_entry(part_md, &md->part, part)
247 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
248 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
249 set_disk_ro(part_md->disk, 1);
250 }
251 }
252
253 mmc_blk_put(md);
254 return count;
255}
256
Andrei Warkentin371a6892011-04-11 18:10:25 -0500257static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
258 char *buf)
259{
260 int ret;
261 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
262
263 ret = snprintf(buf, PAGE_SIZE, "%d",
264 get_disk_ro(dev_to_disk(dev)) ^
265 md->read_only);
266 mmc_blk_put(md);
267 return ret;
268}
269
270static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
271 const char *buf, size_t count)
272{
273 int ret;
274 char *end;
275 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
276 unsigned long set = simple_strtoul(buf, &end, 0);
277 if (end == buf) {
278 ret = -EINVAL;
279 goto out;
280 }
281
282 set_disk_ro(dev_to_disk(dev), set || md->read_only);
283 ret = count;
284out:
285 mmc_blk_put(md);
286 return ret;
287}
288
Al Viroa5a15612008-03-02 10:33:30 -0500289static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Al Viroa5a15612008-03-02 10:33:30 -0500291 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 int ret = -ENXIO;
293
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200294 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (md) {
296 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500297 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700299
Al Viroa5a15612008-03-02 10:33:30 -0500300 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700301 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700302 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200305 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 return ret;
308}
309
Al Virodb2a1442013-05-05 21:52:57 -0400310static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Al Viroa5a15612008-03-02 10:33:30 -0500312 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200314 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200316 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
319static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800320mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800322 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
323 geo->heads = 4;
324 geo->sectors = 16;
325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
John Calixtocb87ea22011-04-26 18:56:29 -0400328struct mmc_blk_ioc_data {
329 struct mmc_ioc_cmd ic;
330 unsigned char *buf;
331 u64 buf_bytes;
332};
333
334static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
335 struct mmc_ioc_cmd __user *user)
336{
337 struct mmc_blk_ioc_data *idata;
338 int err;
339
340 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
341 if (!idata) {
342 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400343 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400344 }
345
346 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
347 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400348 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400349 }
350
351 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
352 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
353 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400354 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400355 }
356
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100357 if (!idata->buf_bytes)
358 return idata;
359
John Calixtocb87ea22011-04-26 18:56:29 -0400360 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
361 if (!idata->buf) {
362 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400363 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400364 }
365
366 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
367 idata->ic.data_ptr, idata->buf_bytes)) {
368 err = -EFAULT;
369 goto copy_err;
370 }
371
372 return idata;
373
374copy_err:
375 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400376idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400377 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400378out:
John Calixtocb87ea22011-04-26 18:56:29 -0400379 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400380}
381
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200382static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
383 u32 retries_max)
384{
385 int err;
386 u32 retry_count = 0;
387
388 if (!status || !retries_max)
389 return -EINVAL;
390
391 do {
392 err = get_card_status(card, status, 5);
393 if (err)
394 break;
395
396 if (!R1_STATUS(*status) &&
397 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
398 break; /* RPMB programming operation complete */
399
400 /*
401 * Rechedule to give the MMC device a chance to continue
402 * processing the previous command without being polled too
403 * frequently.
404 */
405 usleep_range(1000, 5000);
406 } while (++retry_count < retries_max);
407
408 if (retry_count == retries_max)
409 err = -EPERM;
410
411 return err;
412}
413
Maya Erez775a9362013-04-18 15:41:55 +0300414static int ioctl_do_sanitize(struct mmc_card *card)
415{
416 int err;
417
Ulf Hanssona2d10862013-12-16 14:37:26 +0100418 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300419 pr_warn("%s: %s - SANITIZE is not supported\n",
420 mmc_hostname(card->host), __func__);
421 err = -EOPNOTSUPP;
422 goto out;
423 }
424
425 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
426 mmc_hostname(card->host), __func__);
427
428 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
429 EXT_CSD_SANITIZE_START, 1,
430 MMC_SANITIZE_REQ_TIMEOUT);
431
432 if (err)
433 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
434 mmc_hostname(card->host), __func__, err);
435
436 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
437 __func__);
438out:
439 return err;
440}
441
John Calixtocb87ea22011-04-26 18:56:29 -0400442static int mmc_blk_ioctl_cmd(struct block_device *bdev,
443 struct mmc_ioc_cmd __user *ic_ptr)
444{
445 struct mmc_blk_ioc_data *idata;
446 struct mmc_blk_data *md;
447 struct mmc_card *card;
448 struct mmc_command cmd = {0};
449 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530450 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400451 struct scatterlist sg;
452 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200453 int is_rpmb = false;
454 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400455
456 /*
457 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
458 * whole block device, not on a partition. This prevents overspray
459 * between sibling partitions.
460 */
461 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
462 return -EPERM;
463
464 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
465 if (IS_ERR(idata))
466 return PTR_ERR(idata);
467
John Calixtocb87ea22011-04-26 18:56:29 -0400468 md = mmc_blk_get(bdev->bd_disk);
469 if (!md) {
470 err = -EINVAL;
Philippe De Swert1c02f002012-04-11 23:31:45 +0300471 goto cmd_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400472 }
473
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200474 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
475 is_rpmb = true;
476
John Calixtocb87ea22011-04-26 18:56:29 -0400477 card = md->queue.card;
478 if (IS_ERR(card)) {
479 err = PTR_ERR(card);
480 goto cmd_done;
481 }
482
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100483 cmd.opcode = idata->ic.opcode;
484 cmd.arg = idata->ic.arg;
485 cmd.flags = idata->ic.flags;
486
487 if (idata->buf_bytes) {
488 data.sg = &sg;
489 data.sg_len = 1;
490 data.blksz = idata->ic.blksz;
491 data.blocks = idata->ic.blocks;
492
493 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
494
495 if (idata->ic.write_flag)
496 data.flags = MMC_DATA_WRITE;
497 else
498 data.flags = MMC_DATA_READ;
499
500 /* data.flags must already be set before doing this. */
501 mmc_set_data_timeout(&data, card);
502
503 /* Allow overriding the timeout_ns for empirical tuning. */
504 if (idata->ic.data_timeout_ns)
505 data.timeout_ns = idata->ic.data_timeout_ns;
506
507 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
508 /*
509 * Pretend this is a data transfer and rely on the
510 * host driver to compute timeout. When all host
511 * drivers support cmd.cmd_timeout for R1B, this
512 * can be changed to:
513 *
514 * mrq.data = NULL;
515 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
516 */
517 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
518 }
519
520 mrq.data = &data;
521 }
522
523 mrq.cmd = &cmd;
524
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200525 mmc_get_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400526
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200527 err = mmc_blk_part_switch(card, md);
528 if (err)
529 goto cmd_rel_host;
530
John Calixtocb87ea22011-04-26 18:56:29 -0400531 if (idata->ic.is_acmd) {
532 err = mmc_app_cmd(card->host, card);
533 if (err)
534 goto cmd_rel_host;
535 }
536
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200537 if (is_rpmb) {
538 err = mmc_set_blockcount(card, data.blocks,
539 idata->ic.write_flag & (1 << 31));
540 if (err)
541 goto cmd_rel_host;
542 }
543
Yaniv Gardia82e4842013-06-05 14:13:08 +0300544 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
545 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300546 err = ioctl_do_sanitize(card);
547
548 if (err)
549 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
550 __func__, err);
551
552 goto cmd_rel_host;
553 }
554
John Calixtocb87ea22011-04-26 18:56:29 -0400555 mmc_wait_for_req(card->host, &mrq);
556
557 if (cmd.error) {
558 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
559 __func__, cmd.error);
560 err = cmd.error;
561 goto cmd_rel_host;
562 }
563 if (data.error) {
564 dev_err(mmc_dev(card->host), "%s: data error %d\n",
565 __func__, data.error);
566 err = data.error;
567 goto cmd_rel_host;
568 }
569
570 /*
571 * According to the SD specs, some commands require a delay after
572 * issuing the command.
573 */
574 if (idata->ic.postsleep_min_us)
575 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
576
577 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
578 err = -EFAULT;
579 goto cmd_rel_host;
580 }
581
582 if (!idata->ic.write_flag) {
583 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
584 idata->buf, idata->buf_bytes)) {
585 err = -EFAULT;
586 goto cmd_rel_host;
587 }
588 }
589
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200590 if (is_rpmb) {
591 /*
592 * Ensure RPMB command has completed by polling CMD13
593 * "Send Status".
594 */
595 err = ioctl_rpmb_card_status_poll(card, &status, 5);
596 if (err)
597 dev_err(mmc_dev(card->host),
598 "%s: Card Status=0x%08X, error %d\n",
599 __func__, status, err);
600 }
601
John Calixtocb87ea22011-04-26 18:56:29 -0400602cmd_rel_host:
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200603 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400604
605cmd_done:
606 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300607cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400608 kfree(idata->buf);
609 kfree(idata);
610 return err;
611}
612
613static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
614 unsigned int cmd, unsigned long arg)
615{
616 int ret = -EINVAL;
617 if (cmd == MMC_IOC_CMD)
618 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
619 return ret;
620}
621
622#ifdef CONFIG_COMPAT
623static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
624 unsigned int cmd, unsigned long arg)
625{
626 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
627}
628#endif
629
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700630static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500631 .open = mmc_blk_open,
632 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800633 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400635 .ioctl = mmc_blk_ioctl,
636#ifdef CONFIG_COMPAT
637 .compat_ioctl = mmc_blk_compat_ioctl,
638#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639};
640
Andrei Warkentin371a6892011-04-11 18:10:25 -0500641static inline int mmc_blk_part_switch(struct mmc_card *card,
642 struct mmc_blk_data *md)
643{
644 int ret;
645 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300646
Andrei Warkentin371a6892011-04-11 18:10:25 -0500647 if (main_md->part_curr == md->part_type)
648 return 0;
649
650 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300651 u8 part_config = card->ext_csd.part_config;
652
653 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
654 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500655
656 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300657 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500658 card->ext_csd.part_time);
659 if (ret)
660 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300661
662 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300663 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500664
665 main_md->part_curr = md->part_type;
666 return 0;
667}
668
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700669static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
670{
671 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100672 u32 result;
673 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700674
Venkatraman Sad5fd972011-08-25 00:30:50 +0530675 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400676 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400677 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700678
679 struct scatterlist sg;
680
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700681 cmd.opcode = MMC_APP_CMD;
682 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700683 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700684
685 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700686 if (err)
687 return (u32)-1;
688 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700689 return (u32)-1;
690
691 memset(&cmd, 0, sizeof(struct mmc_command));
692
693 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
694 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700695 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700696
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700697 data.blksz = 4;
698 data.blocks = 1;
699 data.flags = MMC_DATA_READ;
700 data.sg = &sg;
701 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530702 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700703
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700704 mrq.cmd = &cmd;
705 mrq.data = &data;
706
Ben Dooks051913d2009-06-08 23:33:57 +0100707 blocks = kmalloc(4, GFP_KERNEL);
708 if (!blocks)
709 return (u32)-1;
710
711 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700712
713 mmc_wait_for_req(card->host, &mrq);
714
Ben Dooks051913d2009-06-08 23:33:57 +0100715 result = ntohl(*blocks);
716 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700717
Ben Dooks051913d2009-06-08 23:33:57 +0100718 if (cmd.error || data.error)
719 result = (u32)-1;
720
721 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700722}
723
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100724static int send_stop(struct mmc_card *card, u32 *status)
725{
726 struct mmc_command cmd = {0};
727 int err;
728
729 cmd.opcode = MMC_STOP_TRANSMISSION;
730 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
731 err = mmc_wait_for_cmd(card->host, &cmd, 5);
732 if (err == 0)
733 *status = cmd.resp[0];
734 return err;
735}
736
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100737static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300738{
Chris Ball1278dba2011-04-13 23:40:30 -0400739 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300740 int err;
741
Adrian Hunter504f1912008-10-16 12:55:25 +0300742 cmd.opcode = MMC_SEND_STATUS;
743 if (!mmc_host_is_spi(card->host))
744 cmd.arg = card->rca << 16;
745 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100746 err = mmc_wait_for_cmd(card->host, &cmd, retries);
747 if (err == 0)
748 *status = cmd.resp[0];
749 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300750}
751
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100752static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
753 struct request *req, int *gen_err)
754{
755 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
756 int err = 0;
757 u32 status;
758
759 do {
760 err = get_card_status(card, &status, 5);
761 if (err) {
762 pr_err("%s: error %d requesting status\n",
763 req->rq_disk->disk_name, err);
764 return err;
765 }
766
767 if (status & R1_ERROR) {
768 pr_err("%s: %s: error sending status cmd, status %#x\n",
769 req->rq_disk->disk_name, __func__, status);
770 *gen_err = 1;
771 }
772
773 /*
774 * Timeout if the device never becomes ready for data and never
775 * leaves the program state.
776 */
777 if (time_after(jiffies, timeout)) {
778 pr_err("%s: Card stuck in programming state! %s %s\n",
779 mmc_hostname(card->host),
780 req->rq_disk->disk_name, __func__);
781 return -ETIMEDOUT;
782 }
783
784 /*
785 * Some cards mishandle the status bits,
786 * so make sure to check both the busy
787 * indication and the card state.
788 */
789 } while (!(status & R1_READY_FOR_DATA) ||
790 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
791
792 return err;
793}
794
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530795#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100796#define ERR_RETRY 2
797#define ERR_ABORT 1
798#define ERR_CONTINUE 0
799
800static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
801 bool status_valid, u32 status)
802{
803 switch (error) {
804 case -EILSEQ:
805 /* response crc error, retry the r/w cmd */
806 pr_err("%s: %s sending %s command, card status %#x\n",
807 req->rq_disk->disk_name, "response CRC error",
808 name, status);
809 return ERR_RETRY;
810
811 case -ETIMEDOUT:
812 pr_err("%s: %s sending %s command, card status %#x\n",
813 req->rq_disk->disk_name, "timed out", name, status);
814
815 /* If the status cmd initially failed, retry the r/w cmd */
816 if (!status_valid)
817 return ERR_RETRY;
818
819 /*
820 * If it was a r/w cmd crc error, or illegal command
821 * (eg, issued in wrong state) then retry - we should
822 * have corrected the state problem above.
823 */
824 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
825 return ERR_RETRY;
826
827 /* Otherwise abort the command */
828 return ERR_ABORT;
829
830 default:
831 /* We don't understand the error code the driver gave us */
832 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
833 req->rq_disk->disk_name, error, status);
834 return ERR_ABORT;
835 }
836}
837
838/*
839 * Initial r/w and stop cmd error recovery.
840 * We don't know whether the card received the r/w cmd or not, so try to
841 * restore things back to a sane state. Essentially, we do this as follows:
842 * - Obtain card status. If the first attempt to obtain card status fails,
843 * the status word will reflect the failed status cmd, not the failed
844 * r/w cmd. If we fail to obtain card status, it suggests we can no
845 * longer communicate with the card.
846 * - Check the card state. If the card received the cmd but there was a
847 * transient problem with the response, it might still be in a data transfer
848 * mode. Try to send it a stop command. If this fails, we can't recover.
849 * - If the r/w cmd failed due to a response CRC error, it was probably
850 * transient, so retry the cmd.
851 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
852 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
853 * illegal cmd, retry.
854 * Otherwise we don't understand what happened, so abort.
855 */
856static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +0900857 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100858{
859 bool prev_cmd_status_valid = true;
860 u32 status, stop_status = 0;
861 int err, retry;
862
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530863 if (mmc_card_removed(card))
864 return ERR_NOMEDIUM;
865
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100866 /*
867 * Try to get card status which indicates both the card state
868 * and why there was no response. If the first attempt fails,
869 * we can't be sure the returned status is for the r/w command.
870 */
871 for (retry = 2; retry >= 0; retry--) {
872 err = get_card_status(card, &status, 0);
873 if (!err)
874 break;
875
876 prev_cmd_status_valid = false;
877 pr_err("%s: error %d sending status command, %sing\n",
878 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
879 }
880
881 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530882 if (err) {
883 /* Check if the card is removed */
884 if (mmc_detect_card_removed(card->host))
885 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100886 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530887 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100888
Adrian Hunter67716322011-08-29 16:42:15 +0300889 /* Flag ECC errors */
890 if ((status & R1_CARD_ECC_FAILED) ||
891 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
892 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
893 *ecc_err = 1;
894
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +0900895 /* Flag General errors */
896 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
897 if ((status & R1_ERROR) ||
898 (brq->stop.resp[0] & R1_ERROR)) {
899 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
900 req->rq_disk->disk_name, __func__,
901 brq->stop.resp[0], status);
902 *gen_err = 1;
903 }
904
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100905 /*
906 * Check the current card state. If it is in some data transfer
907 * mode, tell it to stop (and hopefully transition back to TRAN.)
908 */
909 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
910 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
911 err = send_stop(card, &stop_status);
912 if (err)
913 pr_err("%s: error %d sending stop command\n",
914 req->rq_disk->disk_name, err);
915
916 /*
917 * If the stop cmd also timed out, the card is probably
918 * not present, so abort. Other errors are bad news too.
919 */
920 if (err)
921 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300922 if (stop_status & R1_CARD_ECC_FAILED)
923 *ecc_err = 1;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +0900924 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
925 if (stop_status & R1_ERROR) {
926 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
927 req->rq_disk->disk_name, __func__,
928 stop_status);
929 *gen_err = 1;
930 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100931 }
932
933 /* Check for set block count errors */
934 if (brq->sbc.error)
935 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
936 prev_cmd_status_valid, status);
937
938 /* Check for r/w command errors */
939 if (brq->cmd.error)
940 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
941 prev_cmd_status_valid, status);
942
Adrian Hunter67716322011-08-29 16:42:15 +0300943 /* Data errors */
944 if (!brq->stop.error)
945 return ERR_CONTINUE;
946
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +0100947 /* Now for stop errors. These aren't fatal to the transfer. */
948 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
949 req->rq_disk->disk_name, brq->stop.error,
950 brq->cmd.resp[0], status);
951
952 /*
953 * Subsitute in our own stop status as this will give the error
954 * state which happened during the execution of the r/w command.
955 */
956 if (stop_status) {
957 brq->stop.resp[0] = stop_status;
958 brq->stop.error = 0;
959 }
960 return ERR_CONTINUE;
961}
962
Adrian Hunter67716322011-08-29 16:42:15 +0300963static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
964 int type)
965{
966 int err;
967
968 if (md->reset_done & type)
969 return -EEXIST;
970
971 md->reset_done |= type;
972 err = mmc_hw_reset(host);
973 /* Ensure we switch back to the correct partition */
974 if (err != -EOPNOTSUPP) {
975 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
976 int part_err;
977
978 main_md->part_curr = main_md->part_type;
979 part_err = mmc_blk_part_switch(host->card, md);
980 if (part_err) {
981 /*
982 * We have failed to get back into the correct
983 * partition, so we need to abort the whole request.
984 */
985 return -ENODEV;
986 }
987 }
988 return err;
989}
990
991static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
992{
993 md->reset_done &= ~type;
994}
995
Adrian Hunterbd788c92010-08-11 14:17:47 -0700996static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
997{
998 struct mmc_blk_data *md = mq->data;
999 struct mmc_card *card = md->queue.card;
1000 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001001 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001002
Adrian Hunterbd788c92010-08-11 14:17:47 -07001003 if (!mmc_can_erase(card)) {
1004 err = -EOPNOTSUPP;
1005 goto out;
1006 }
1007
1008 from = blk_rq_pos(req);
1009 nr = blk_rq_sectors(req);
1010
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001011 if (mmc_can_discard(card))
1012 arg = MMC_DISCARD_ARG;
1013 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001014 arg = MMC_TRIM_ARG;
1015 else
1016 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001017retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001018 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1019 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1020 INAND_CMD38_ARG_EXT_CSD,
1021 arg == MMC_TRIM_ARG ?
1022 INAND_CMD38_ARG_TRIM :
1023 INAND_CMD38_ARG_ERASE,
1024 0);
1025 if (err)
1026 goto out;
1027 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001028 err = mmc_erase(card, from, nr, arg);
1029out:
Adrian Hunter67716322011-08-29 16:42:15 +03001030 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1031 goto retry;
1032 if (!err)
1033 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301034 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001035
Adrian Hunterbd788c92010-08-11 14:17:47 -07001036 return err ? 0 : 1;
1037}
1038
Adrian Hunter49804542010-08-11 14:17:50 -07001039static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1040 struct request *req)
1041{
1042 struct mmc_blk_data *md = mq->data;
1043 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001044 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001045 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001046
Maya Erez775a9362013-04-18 15:41:55 +03001047 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001048 err = -EOPNOTSUPP;
1049 goto out;
1050 }
1051
1052 from = blk_rq_pos(req);
1053 nr = blk_rq_sectors(req);
1054
Maya Erez775a9362013-04-18 15:41:55 +03001055 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1056 arg = MMC_SECURE_TRIM1_ARG;
1057 else
1058 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001059
Adrian Hunter67716322011-08-29 16:42:15 +03001060retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001061 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1062 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1063 INAND_CMD38_ARG_EXT_CSD,
1064 arg == MMC_SECURE_TRIM1_ARG ?
1065 INAND_CMD38_ARG_SECTRIM1 :
1066 INAND_CMD38_ARG_SECERASE,
1067 0);
1068 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001069 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001070 }
Adrian Hunter28302812012-04-05 14:45:48 +03001071
Adrian Hunter49804542010-08-11 14:17:50 -07001072 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001073 if (err == -EIO)
1074 goto out_retry;
1075 if (err)
1076 goto out;
1077
1078 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001079 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1080 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1081 INAND_CMD38_ARG_EXT_CSD,
1082 INAND_CMD38_ARG_SECTRIM2,
1083 0);
1084 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001085 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001086 }
Adrian Hunter28302812012-04-05 14:45:48 +03001087
Adrian Hunter49804542010-08-11 14:17:50 -07001088 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001089 if (err == -EIO)
1090 goto out_retry;
1091 if (err)
1092 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001093 }
Adrian Hunter28302812012-04-05 14:45:48 +03001094
Adrian Hunter28302812012-04-05 14:45:48 +03001095out_retry:
1096 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001097 goto retry;
1098 if (!err)
1099 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001100out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301101 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001102
Adrian Hunter49804542010-08-11 14:17:50 -07001103 return err ? 0 : 1;
1104}
1105
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001106static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1107{
1108 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001109 struct mmc_card *card = md->queue.card;
1110 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001111
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001112 ret = mmc_flush_cache(card);
1113 if (ret)
1114 ret = -EIO;
1115
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301116 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001117
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001118 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001119}
1120
1121/*
1122 * Reformat current write as a reliable write, supporting
1123 * both legacy and the enhanced reliable write MMC cards.
1124 * In each transfer we'll handle only as much as a single
1125 * reliable write can handle, thus finish the request in
1126 * partial completions.
1127 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001128static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1129 struct mmc_card *card,
1130 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001131{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001132 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1133 /* Legacy mode imposes restrictions on transfers. */
1134 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1135 brq->data.blocks = 1;
1136
1137 if (brq->data.blocks > card->ext_csd.rel_sectors)
1138 brq->data.blocks = card->ext_csd.rel_sectors;
1139 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1140 brq->data.blocks = 1;
1141 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001142}
1143
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001144#define CMD_ERRORS \
1145 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1146 R1_ADDRESS_ERROR | /* Misaligned address */ \
1147 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1148 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1149 R1_CC_ERROR | /* Card controller error */ \
1150 R1_ERROR) /* General/unknown error */
1151
Per Forlinee8a43a2011-07-01 18:55:33 +02001152static int mmc_blk_err_check(struct mmc_card *card,
1153 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001154{
Per Forlinee8a43a2011-07-01 18:55:33 +02001155 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1156 mmc_active);
1157 struct mmc_blk_request *brq = &mq_mrq->brq;
1158 struct request *req = mq_mrq->req;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001159 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001160
1161 /*
1162 * sbc.error indicates a problem with the set block count
1163 * command. No data will have been transferred.
1164 *
1165 * cmd.error indicates a problem with the r/w command. No
1166 * data will have been transferred.
1167 *
1168 * stop.error indicates a problem with the stop command. Data
1169 * may have been transferred, or may still be transferring.
1170 */
Adrian Hunter67716322011-08-29 16:42:15 +03001171 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1172 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001173 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001174 case ERR_RETRY:
1175 return MMC_BLK_RETRY;
1176 case ERR_ABORT:
1177 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301178 case ERR_NOMEDIUM:
1179 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001180 case ERR_CONTINUE:
1181 break;
1182 }
1183 }
1184
1185 /*
1186 * Check for errors relating to the execution of the
1187 * initial command - such as address errors. No data
1188 * has been transferred.
1189 */
1190 if (brq->cmd.resp[0] & CMD_ERRORS) {
1191 pr_err("%s: r/w command failed, status = %#x\n",
1192 req->rq_disk->disk_name, brq->cmd.resp[0]);
1193 return MMC_BLK_ABORT;
1194 }
1195
1196 /*
1197 * Everything else is either success, or a data error of some
1198 * kind. If it was a write, we may have transitioned to
1199 * program mode, which we have to wait for it to complete.
1200 */
1201 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001202 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001203
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001204 /* Check stop command response */
1205 if (brq->stop.resp[0] & R1_ERROR) {
1206 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1207 req->rq_disk->disk_name, __func__,
1208 brq->stop.resp[0]);
1209 gen_err = 1;
1210 }
1211
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001212 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, req, &gen_err);
1213 if (err)
1214 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001215 }
1216
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001217 /* if general error occurs, retry the write operation. */
1218 if (gen_err) {
1219 pr_warn("%s: retrying write for general error\n",
1220 req->rq_disk->disk_name);
1221 return MMC_BLK_RETRY;
1222 }
1223
Per Forlind78d4a82011-07-01 18:55:30 +02001224 if (brq->data.error) {
1225 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1226 req->rq_disk->disk_name, brq->data.error,
1227 (unsigned)blk_rq_pos(req),
1228 (unsigned)blk_rq_sectors(req),
1229 brq->cmd.resp[0], brq->stop.resp[0]);
1230
1231 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001232 if (ecc_err)
1233 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001234 return MMC_BLK_DATA_ERR;
1235 } else {
1236 return MMC_BLK_CMD_ERR;
1237 }
1238 }
1239
Adrian Hunter67716322011-08-29 16:42:15 +03001240 if (!brq->data.bytes_xfered)
1241 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001242
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001243 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1244 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1245 return MMC_BLK_PARTIAL;
1246 else
1247 return MMC_BLK_SUCCESS;
1248 }
1249
Adrian Hunter67716322011-08-29 16:42:15 +03001250 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1251 return MMC_BLK_PARTIAL;
1252
1253 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001254}
1255
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001256static int mmc_blk_packed_err_check(struct mmc_card *card,
1257 struct mmc_async_req *areq)
1258{
1259 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1260 mmc_active);
1261 struct request *req = mq_rq->req;
1262 struct mmc_packed *packed = mq_rq->packed;
1263 int err, check, status;
1264 u8 *ext_csd;
1265
1266 BUG_ON(!packed);
1267
1268 packed->retries--;
1269 check = mmc_blk_err_check(card, areq);
1270 err = get_card_status(card, &status, 0);
1271 if (err) {
1272 pr_err("%s: error %d sending status command\n",
1273 req->rq_disk->disk_name, err);
1274 return MMC_BLK_ABORT;
1275 }
1276
1277 if (status & R1_EXCEPTION_EVENT) {
1278 ext_csd = kzalloc(512, GFP_KERNEL);
1279 if (!ext_csd) {
1280 pr_err("%s: unable to allocate buffer for ext_csd\n",
1281 req->rq_disk->disk_name);
1282 return -ENOMEM;
1283 }
1284
1285 err = mmc_send_ext_csd(card, ext_csd);
1286 if (err) {
1287 pr_err("%s: error %d sending ext_csd\n",
1288 req->rq_disk->disk_name, err);
1289 check = MMC_BLK_ABORT;
1290 goto free;
1291 }
1292
1293 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1294 EXT_CSD_PACKED_FAILURE) &&
1295 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1296 EXT_CSD_PACKED_GENERIC_ERROR)) {
1297 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1298 EXT_CSD_PACKED_INDEXED_ERROR) {
1299 packed->idx_failure =
1300 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1301 check = MMC_BLK_PARTIAL;
1302 }
1303 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1304 "failure index: %d\n",
1305 req->rq_disk->disk_name, packed->nr_entries,
1306 packed->blocks, packed->idx_failure);
1307 }
1308free:
1309 kfree(ext_csd);
1310 }
1311
1312 return check;
1313}
1314
Per Forlin54d49d72011-07-01 18:55:29 +02001315static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1316 struct mmc_card *card,
1317 int disable_multi,
1318 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Per Forlin54d49d72011-07-01 18:55:29 +02001320 u32 readcmd, writecmd;
1321 struct mmc_blk_request *brq = &mqrq->brq;
1322 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301324 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001326 /*
1327 * Reliable writes are used to implement Forced Unit Access and
1328 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001329 *
1330 * XXX: this really needs a good explanation of why REQ_META
1331 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001332 */
1333 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1334 (req->cmd_flags & REQ_META)) &&
1335 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001336 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001337
Per Forlin54d49d72011-07-01 18:55:29 +02001338 memset(brq, 0, sizeof(struct mmc_blk_request));
1339 brq->mrq.cmd = &brq->cmd;
1340 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Per Forlin54d49d72011-07-01 18:55:29 +02001342 brq->cmd.arg = blk_rq_pos(req);
1343 if (!mmc_card_blockaddr(card))
1344 brq->cmd.arg <<= 9;
1345 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1346 brq->data.blksz = 512;
1347 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1348 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001349 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Per Forlin54d49d72011-07-01 18:55:29 +02001351 /*
1352 * The block layer doesn't support all sector count
1353 * restrictions, so we need to be prepared for too big
1354 * requests.
1355 */
1356 if (brq->data.blocks > card->host->max_blk_count)
1357 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001359 if (brq->data.blocks > 1) {
1360 /*
1361 * After a read error, we redo the request one sector
1362 * at a time in order to accurately determine which
1363 * sectors can be read successfully.
1364 */
1365 if (disable_multi)
1366 brq->data.blocks = 1;
1367
1368 /* Some controllers can't do multiblock reads due to hw bugs */
1369 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1370 rq_data_dir(req) == READ)
1371 brq->data.blocks = 1;
1372 }
Per Forlin54d49d72011-07-01 18:55:29 +02001373
1374 if (brq->data.blocks > 1 || do_rel_wr) {
1375 /* SPI multiblock writes terminate using a special
1376 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001377 */
Per Forlin54d49d72011-07-01 18:55:29 +02001378 if (!mmc_host_is_spi(card->host) ||
1379 rq_data_dir(req) == READ)
1380 brq->mrq.stop = &brq->stop;
1381 readcmd = MMC_READ_MULTIPLE_BLOCK;
1382 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1383 } else {
1384 brq->mrq.stop = NULL;
1385 readcmd = MMC_READ_SINGLE_BLOCK;
1386 writecmd = MMC_WRITE_BLOCK;
1387 }
1388 if (rq_data_dir(req) == READ) {
1389 brq->cmd.opcode = readcmd;
1390 brq->data.flags |= MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001391 if (brq->mrq.stop)
1392 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1393 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001394 } else {
1395 brq->cmd.opcode = writecmd;
1396 brq->data.flags |= MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001397 if (brq->mrq.stop)
1398 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1399 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001400 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001401
Per Forlin54d49d72011-07-01 18:55:29 +02001402 if (do_rel_wr)
1403 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001404
Per Forlin54d49d72011-07-01 18:55:29 +02001405 /*
Saugata Das42659002011-12-21 13:09:17 +05301406 * Data tag is used only during writing meta data to speed
1407 * up write and any subsequent read of this meta data
1408 */
1409 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1410 (req->cmd_flags & REQ_META) &&
1411 (rq_data_dir(req) == WRITE) &&
1412 ((brq->data.blocks * brq->data.blksz) >=
1413 card->ext_csd.data_tag_unit_size);
1414
1415 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001416 * Pre-defined multi-block transfers are preferable to
1417 * open ended-ones (and necessary for reliable writes).
1418 * However, it is not sufficient to just send CMD23,
1419 * and avoid the final CMD12, as on an error condition
1420 * CMD12 (stop) needs to be sent anyway. This, coupled
1421 * with Auto-CMD23 enhancements provided by some
1422 * hosts, means that the complexity of dealing
1423 * with this is best left to the host. If CMD23 is
1424 * supported by card and host, we'll fill sbc in and let
1425 * the host deal with handling it correctly. This means
1426 * that for hosts that don't expose MMC_CAP_CMD23, no
1427 * change of behavior will be observed.
1428 *
1429 * N.B: Some MMC cards experience perf degradation.
1430 * We'll avoid using CMD23-bounded multiblock writes for
1431 * these, while retaining features like reliable writes.
1432 */
Saugata Das42659002011-12-21 13:09:17 +05301433 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1434 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1435 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001436 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1437 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301438 (do_rel_wr ? (1 << 31) : 0) |
1439 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001440 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1441 brq->mrq.sbc = &brq->sbc;
1442 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001443
Per Forlin54d49d72011-07-01 18:55:29 +02001444 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001445
Per Forlin54d49d72011-07-01 18:55:29 +02001446 brq->data.sg = mqrq->sg;
1447 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001448
Per Forlin54d49d72011-07-01 18:55:29 +02001449 /*
1450 * Adjust the sg list so it is the same size as the
1451 * request.
1452 */
1453 if (brq->data.blocks != blk_rq_sectors(req)) {
1454 int i, data_size = brq->data.blocks << 9;
1455 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001456
Per Forlin54d49d72011-07-01 18:55:29 +02001457 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1458 data_size -= sg->length;
1459 if (data_size <= 0) {
1460 sg->length += data_size;
1461 i++;
1462 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001463 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001464 }
Per Forlin54d49d72011-07-01 18:55:29 +02001465 brq->data.sg_len = i;
1466 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001467
Per Forlinee8a43a2011-07-01 18:55:33 +02001468 mqrq->mmc_active.mrq = &brq->mrq;
1469 mqrq->mmc_active.err_check = mmc_blk_err_check;
1470
Per Forlin54d49d72011-07-01 18:55:29 +02001471 mmc_queue_bounce_pre(mqrq);
1472}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001474static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1475 struct mmc_card *card)
1476{
1477 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1478 unsigned int max_seg_sz = queue_max_segment_size(q);
1479 unsigned int len, nr_segs = 0;
1480
1481 do {
1482 len = min(hdr_sz, max_seg_sz);
1483 hdr_sz -= len;
1484 nr_segs++;
1485 } while (hdr_sz);
1486
1487 return nr_segs;
1488}
1489
1490static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1491{
1492 struct request_queue *q = mq->queue;
1493 struct mmc_card *card = mq->card;
1494 struct request *cur = req, *next = NULL;
1495 struct mmc_blk_data *md = mq->data;
1496 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1497 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1498 unsigned int req_sectors = 0, phys_segments = 0;
1499 unsigned int max_blk_count, max_phys_segs;
1500 bool put_back = true;
1501 u8 max_packed_rw = 0;
1502 u8 reqs = 0;
1503
1504 if (!(md->flags & MMC_BLK_PACKED_CMD))
1505 goto no_packed;
1506
1507 if ((rq_data_dir(cur) == WRITE) &&
1508 mmc_host_packed_wr(card->host))
1509 max_packed_rw = card->ext_csd.max_packed_writes;
1510
1511 if (max_packed_rw == 0)
1512 goto no_packed;
1513
1514 if (mmc_req_rel_wr(cur) &&
1515 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1516 goto no_packed;
1517
1518 if (mmc_large_sector(card) &&
1519 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1520 goto no_packed;
1521
1522 mmc_blk_clear_packed(mqrq);
1523
1524 max_blk_count = min(card->host->max_blk_count,
1525 card->host->max_req_size >> 9);
1526 if (unlikely(max_blk_count > 0xffff))
1527 max_blk_count = 0xffff;
1528
1529 max_phys_segs = queue_max_segments(q);
1530 req_sectors += blk_rq_sectors(cur);
1531 phys_segments += cur->nr_phys_segments;
1532
1533 if (rq_data_dir(cur) == WRITE) {
1534 req_sectors += mmc_large_sector(card) ? 8 : 1;
1535 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1536 }
1537
1538 do {
1539 if (reqs >= max_packed_rw - 1) {
1540 put_back = false;
1541 break;
1542 }
1543
1544 spin_lock_irq(q->queue_lock);
1545 next = blk_fetch_request(q);
1546 spin_unlock_irq(q->queue_lock);
1547 if (!next) {
1548 put_back = false;
1549 break;
1550 }
1551
1552 if (mmc_large_sector(card) &&
1553 !IS_ALIGNED(blk_rq_sectors(next), 8))
1554 break;
1555
1556 if (next->cmd_flags & REQ_DISCARD ||
1557 next->cmd_flags & REQ_FLUSH)
1558 break;
1559
1560 if (rq_data_dir(cur) != rq_data_dir(next))
1561 break;
1562
1563 if (mmc_req_rel_wr(next) &&
1564 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1565 break;
1566
1567 req_sectors += blk_rq_sectors(next);
1568 if (req_sectors > max_blk_count)
1569 break;
1570
1571 phys_segments += next->nr_phys_segments;
1572 if (phys_segments > max_phys_segs)
1573 break;
1574
1575 list_add_tail(&next->queuelist, &mqrq->packed->list);
1576 cur = next;
1577 reqs++;
1578 } while (1);
1579
1580 if (put_back) {
1581 spin_lock_irq(q->queue_lock);
1582 blk_requeue_request(q, next);
1583 spin_unlock_irq(q->queue_lock);
1584 }
1585
1586 if (reqs > 0) {
1587 list_add(&req->queuelist, &mqrq->packed->list);
1588 mqrq->packed->nr_entries = ++reqs;
1589 mqrq->packed->retries = reqs;
1590 return reqs;
1591 }
1592
1593no_packed:
1594 mqrq->cmd_type = MMC_PACKED_NONE;
1595 return 0;
1596}
1597
1598static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1599 struct mmc_card *card,
1600 struct mmc_queue *mq)
1601{
1602 struct mmc_blk_request *brq = &mqrq->brq;
1603 struct request *req = mqrq->req;
1604 struct request *prq;
1605 struct mmc_blk_data *md = mq->data;
1606 struct mmc_packed *packed = mqrq->packed;
1607 bool do_rel_wr, do_data_tag;
1608 u32 *packed_cmd_hdr;
1609 u8 hdr_blocks;
1610 u8 i = 1;
1611
1612 BUG_ON(!packed);
1613
1614 mqrq->cmd_type = MMC_PACKED_WRITE;
1615 packed->blocks = 0;
1616 packed->idx_failure = MMC_PACKED_NR_IDX;
1617
1618 packed_cmd_hdr = packed->cmd_hdr;
1619 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
1620 packed_cmd_hdr[0] = (packed->nr_entries << 16) |
1621 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1622 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1623
1624 /*
1625 * Argument for each entry of packed group
1626 */
1627 list_for_each_entry(prq, &packed->list, queuelist) {
1628 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1629 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1630 (prq->cmd_flags & REQ_META) &&
1631 (rq_data_dir(prq) == WRITE) &&
1632 ((brq->data.blocks * brq->data.blksz) >=
1633 card->ext_csd.data_tag_unit_size);
1634 /* Argument of CMD23 */
1635 packed_cmd_hdr[(i * 2)] =
1636 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1637 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1638 blk_rq_sectors(prq);
1639 /* Argument of CMD18 or CMD25 */
1640 packed_cmd_hdr[((i * 2)) + 1] =
1641 mmc_card_blockaddr(card) ?
1642 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1643 packed->blocks += blk_rq_sectors(prq);
1644 i++;
1645 }
1646
1647 memset(brq, 0, sizeof(struct mmc_blk_request));
1648 brq->mrq.cmd = &brq->cmd;
1649 brq->mrq.data = &brq->data;
1650 brq->mrq.sbc = &brq->sbc;
1651 brq->mrq.stop = &brq->stop;
1652
1653 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1654 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1655 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1656
1657 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1658 brq->cmd.arg = blk_rq_pos(req);
1659 if (!mmc_card_blockaddr(card))
1660 brq->cmd.arg <<= 9;
1661 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1662
1663 brq->data.blksz = 512;
1664 brq->data.blocks = packed->blocks + hdr_blocks;
1665 brq->data.flags |= MMC_DATA_WRITE;
1666
1667 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1668 brq->stop.arg = 0;
1669 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1670
1671 mmc_set_data_timeout(&brq->data, card);
1672
1673 brq->data.sg = mqrq->sg;
1674 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1675
1676 mqrq->mmc_active.mrq = &brq->mrq;
1677 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1678
1679 mmc_queue_bounce_pre(mqrq);
1680}
1681
Adrian Hunter67716322011-08-29 16:42:15 +03001682static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1683 struct mmc_blk_request *brq, struct request *req,
1684 int ret)
1685{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001686 struct mmc_queue_req *mq_rq;
1687 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1688
Adrian Hunter67716322011-08-29 16:42:15 +03001689 /*
1690 * If this is an SD card and we're writing, we can first
1691 * mark the known good sectors as ok.
1692 *
1693 * If the card is not SD, we can still ok written sectors
1694 * as reported by the controller (which might be less than
1695 * the real number of written sectors, but never more).
1696 */
1697 if (mmc_card_sd(card)) {
1698 u32 blocks;
1699
1700 blocks = mmc_sd_num_wr_blocks(card);
1701 if (blocks != (u32)-1) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301702 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001703 }
1704 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001705 if (!mmc_packed_cmd(mq_rq->cmd_type))
1706 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001707 }
1708 return ret;
1709}
1710
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001711static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1712{
1713 struct request *prq;
1714 struct mmc_packed *packed = mq_rq->packed;
1715 int idx = packed->idx_failure, i = 0;
1716 int ret = 0;
1717
1718 BUG_ON(!packed);
1719
1720 while (!list_empty(&packed->list)) {
1721 prq = list_entry_rq(packed->list.next);
1722 if (idx == i) {
1723 /* retry from error index */
1724 packed->nr_entries -= idx;
1725 mq_rq->req = prq;
1726 ret = 1;
1727
1728 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1729 list_del_init(&prq->queuelist);
1730 mmc_blk_clear_packed(mq_rq);
1731 }
1732 return ret;
1733 }
1734 list_del_init(&prq->queuelist);
1735 blk_end_request(prq, 0, blk_rq_bytes(prq));
1736 i++;
1737 }
1738
1739 mmc_blk_clear_packed(mq_rq);
1740 return ret;
1741}
1742
1743static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1744{
1745 struct request *prq;
1746 struct mmc_packed *packed = mq_rq->packed;
1747
1748 BUG_ON(!packed);
1749
1750 while (!list_empty(&packed->list)) {
1751 prq = list_entry_rq(packed->list.next);
1752 list_del_init(&prq->queuelist);
1753 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1754 }
1755
1756 mmc_blk_clear_packed(mq_rq);
1757}
1758
1759static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1760 struct mmc_queue_req *mq_rq)
1761{
1762 struct request *prq;
1763 struct request_queue *q = mq->queue;
1764 struct mmc_packed *packed = mq_rq->packed;
1765
1766 BUG_ON(!packed);
1767
1768 while (!list_empty(&packed->list)) {
1769 prq = list_entry_rq(packed->list.prev);
1770 if (prq->queuelist.prev != &packed->list) {
1771 list_del_init(&prq->queuelist);
1772 spin_lock_irq(q->queue_lock);
1773 blk_requeue_request(mq->queue, prq);
1774 spin_unlock_irq(q->queue_lock);
1775 } else {
1776 list_del_init(&prq->queuelist);
1777 }
1778 }
1779
1780 mmc_blk_clear_packed(mq_rq);
1781}
1782
Per Forlinee8a43a2011-07-01 18:55:33 +02001783static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02001784{
1785 struct mmc_blk_data *md = mq->data;
1786 struct mmc_card *card = md->queue.card;
1787 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001788 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001789 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001790 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301791 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02001792 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001793 const u8 packed_nr = 2;
1794 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001795
1796 if (!rqc && !mq->mqrq_prev->req)
1797 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001798
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001799 if (rqc)
1800 reqs = mmc_blk_prep_packed_list(mq, rqc);
1801
Per Forlin54d49d72011-07-01 18:55:29 +02001802 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001803 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301804 /*
1805 * When 4KB native sector is enabled, only 8 blocks
1806 * multiple read or write is allowed
1807 */
1808 if ((brq->data.blocks & 0x07) &&
1809 (card->ext_csd.data_sector_size == 4096)) {
1810 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1811 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001812 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301813 goto cmd_abort;
1814 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001815
1816 if (reqs >= packed_nr)
1817 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1818 card, mq);
1819 else
1820 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001821 areq = &mq->mqrq_cur->mmc_active;
1822 } else
1823 areq = NULL;
1824 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001825 if (!areq) {
1826 if (status == MMC_BLK_NEW_REQUEST)
1827 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02001828 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001829 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001830
Per Forlinee8a43a2011-07-01 18:55:33 +02001831 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1832 brq = &mq_rq->brq;
1833 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001834 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001835 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001836
Per Forlind78d4a82011-07-01 18:55:30 +02001837 switch (status) {
1838 case MMC_BLK_SUCCESS:
1839 case MMC_BLK_PARTIAL:
1840 /*
1841 * A block was successfully transferred.
1842 */
Adrian Hunter67716322011-08-29 16:42:15 +03001843 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001844
1845 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1846 ret = mmc_blk_end_packed_req(mq_rq);
1847 break;
1848 } else {
1849 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001850 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001851 }
1852
Adrian Hunter67716322011-08-29 16:42:15 +03001853 /*
1854 * If the blk_end_request function returns non-zero even
1855 * though all data has been transferred and no errors
1856 * were returned by the host controller, it's a bug.
1857 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001858 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301859 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001860 __func__, blk_rq_bytes(req),
1861 brq->data.bytes_xfered);
1862 rqc = NULL;
1863 goto cmd_abort;
1864 }
Per Forlind78d4a82011-07-01 18:55:30 +02001865 break;
1866 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001867 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1868 if (!mmc_blk_reset(md, card->host, type))
1869 break;
1870 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001871 case MMC_BLK_RETRY:
1872 if (retry++ < 5)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001873 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001874 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001875 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001876 if (!mmc_blk_reset(md, card->host, type))
1877 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001878 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001879 case MMC_BLK_DATA_ERR: {
1880 int err;
1881
1882 err = mmc_blk_reset(md, card->host, type);
1883 if (!err)
1884 break;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001885 if (err == -ENODEV ||
1886 mmc_packed_cmd(mq_rq->cmd_type))
Adrian Hunter67716322011-08-29 16:42:15 +03001887 goto cmd_abort;
1888 /* Fall through */
1889 }
1890 case MMC_BLK_ECC_ERR:
1891 if (brq->data.blocks > 1) {
1892 /* Redo read one sector at a time */
1893 pr_warning("%s: retrying using single block read\n",
1894 req->rq_disk->disk_name);
1895 disable_multi = 1;
1896 break;
1897 }
Per Forlind78d4a82011-07-01 18:55:30 +02001898 /*
1899 * After an error, we redo I/O one sector at a
1900 * time, so we only reach here after trying to
1901 * read a single sector.
1902 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301903 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001904 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001905 if (!ret)
1906 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001907 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301908 case MMC_BLK_NOMEDIUM:
1909 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001910 default:
1911 pr_err("%s: Unhandled return value (%d)",
1912 req->rq_disk->disk_name, status);
1913 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001914 }
1915
Per Forlinee8a43a2011-07-01 18:55:33 +02001916 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001917 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1918 if (!mq_rq->packed->retries)
1919 goto cmd_abort;
1920 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1921 mmc_start_req(card->host,
1922 &mq_rq->mmc_active, NULL);
1923 } else {
1924
1925 /*
1926 * In case of a incomplete request
1927 * prepare it again and resend.
1928 */
1929 mmc_blk_rw_rq_prep(mq_rq, card,
1930 disable_multi, mq);
1931 mmc_start_req(card->host,
1932 &mq_rq->mmc_active, NULL);
1933 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 } while (ret);
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return 1;
1938
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001939 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001940 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1941 mmc_blk_abort_packed_req(mq_rq);
1942 } else {
1943 if (mmc_card_removed(card))
1944 req->cmd_flags |= REQ_QUIET;
1945 while (ret)
1946 ret = blk_end_request(req, -EIO,
1947 blk_rq_cur_bytes(req));
1948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Per Forlinee8a43a2011-07-01 18:55:33 +02001950 start_new_req:
1951 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09001952 if (mmc_card_removed(card)) {
1953 rqc->cmd_flags |= REQ_QUIET;
1954 blk_end_request_all(rqc, -EIO);
1955 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001956 /*
1957 * If current request is packed, it needs to put back.
1958 */
1959 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
1960 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1961
Seungwon Jeon7a819022013-01-22 19:48:07 +09001962 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1963 mmc_start_req(card->host,
1964 &mq->mqrq_cur->mmc_active, NULL);
1965 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001966 }
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return 0;
1969}
1970
Adrian Hunterbd788c92010-08-11 14:17:47 -07001971static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1972{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001973 int ret;
1974 struct mmc_blk_data *md = mq->data;
1975 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001976 struct mmc_host *host = card->host;
1977 unsigned long flags;
Ray Juif662ae42013-10-26 11:03:44 -07001978 unsigned int cmd_flags = req ? req->cmd_flags : 0;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001979
Per Forlinee8a43a2011-07-01 18:55:33 +02001980 if (req && !mq->mqrq_prev->req)
1981 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001982 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02001983
Andrei Warkentin371a6892011-04-11 18:10:25 -05001984 ret = mmc_blk_part_switch(card, md);
1985 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001986 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301987 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001988 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001989 ret = 0;
1990 goto out;
1991 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001992
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001993 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Ray Juif662ae42013-10-26 11:03:44 -07001994 if (cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001995 /* complete ongoing async transfer before issuing discard */
1996 if (card->host->areq)
1997 mmc_blk_issue_rw_rq(mq, NULL);
Ian Chen3550ccd2012-08-29 15:05:36 +09001998 if (req->cmd_flags & REQ_SECURE &&
1999 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002000 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002001 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002002 ret = mmc_blk_issue_discard_rq(mq, req);
Ray Juif662ae42013-10-26 11:03:44 -07002003 } else if (cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002004 /* complete ongoing async transfer before issuing flush */
2005 if (card->host->areq)
2006 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002007 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002008 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002009 if (!req && host->areq) {
2010 spin_lock_irqsave(&host->context_info.lock, flags);
2011 host->context_info.is_waiting_last_req = true;
2012 spin_unlock_irqrestore(&host->context_info.lock, flags);
2013 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002014 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002015 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002016
Andrei Warkentin371a6892011-04-11 18:10:25 -05002017out:
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002018 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
Ray Juif662ae42013-10-26 11:03:44 -07002019 (cmd_flags & MMC_REQ_SPECIAL_MASK))
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002020 /*
2021 * Release host when there are no more requests
2022 * and after special request(discard, flush) is done.
2023 * In case sepecial request, there is no reentry to
2024 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2025 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002026 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002027 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002028}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Russell Kinga6f6c962006-01-03 22:38:44 +00002030static inline int mmc_blk_readonly(struct mmc_card *card)
2031{
2032 return mmc_card_readonly(card) ||
2033 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2034}
2035
Andrei Warkentin371a6892011-04-11 18:10:25 -05002036static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2037 struct device *parent,
2038 sector_t size,
2039 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002040 const char *subname,
2041 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 struct mmc_blk_data *md;
2044 int devidx, ret;
2045
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002046 devidx = find_first_zero_bit(dev_use, max_devices);
2047 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return ERR_PTR(-ENOSPC);
2049 __set_bit(devidx, dev_use);
2050
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002051 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002052 if (!md) {
2053 ret = -ENOMEM;
2054 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002056
Russell Kinga6f6c962006-01-03 22:38:44 +00002057 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002058 * !subname implies we are creating main mmc_blk_data that will be
2059 * associated with mmc_card with mmc_set_drvdata. Due to device
2060 * partitions, devidx will not coincide with a per-physical card
2061 * index anymore so we keep track of a name index.
2062 */
2063 if (!subname) {
2064 md->name_idx = find_first_zero_bit(name_use, max_devices);
2065 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002066 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002067 md->name_idx = ((struct mmc_blk_data *)
2068 dev_to_disk(parent)->private_data)->name_idx;
2069
Johan Rudholmadd710e2011-12-02 08:51:06 +01002070 md->area_type = area_type;
2071
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002072 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002073 * Set the read-only status based on the supported commands
2074 * and the write protect switch.
2075 */
2076 md->read_only = mmc_blk_readonly(card);
2077
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002078 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002079 if (md->disk == NULL) {
2080 ret = -ENOMEM;
2081 goto err_kfree;
2082 }
2083
2084 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002085 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002086 md->usage = 1;
2087
Adrian Hunterd09408a2011-06-23 13:40:28 +03002088 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002089 if (ret)
2090 goto err_putdisk;
2091
Russell Kinga6f6c962006-01-03 22:38:44 +00002092 md->queue.issue_fn = mmc_blk_issue_rq;
2093 md->queue.data = md;
2094
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002095 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002096 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002097 md->disk->fops = &mmc_bdops;
2098 md->disk->private_data = md;
2099 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002100 md->disk->driverfs_dev = parent;
2101 set_disk_ro(md->disk, md->read_only || default_ro);
Loic Pallardy53d8f972012-08-06 17:12:28 +02002102 if (area_type & MMC_BLK_DATA_AREA_RPMB)
2103 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002104
2105 /*
2106 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2107 *
2108 * - be set for removable media with permanent block devices
2109 * - be unset for removable block devices with permanent media
2110 *
2111 * Since MMC block devices clearly fall under the second
2112 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2113 * should use the block device creation/destruction hotplug
2114 * messages to tell when the card is present.
2115 */
2116
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002117 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2118 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002119
Saugata Dasa5075eb2012-05-17 16:32:21 +05302120 if (mmc_card_mmc(card))
2121 blk_queue_logical_block_size(md->queue.queue,
2122 card->ext_csd.data_sector_size);
2123 else
2124 blk_queue_logical_block_size(md->queue.queue, 512);
2125
Andrei Warkentin371a6892011-04-11 18:10:25 -05002126 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002127
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002128 if (mmc_host_cmd23(card->host)) {
2129 if (mmc_card_mmc(card) ||
2130 (mmc_card_sd(card) &&
2131 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2132 md->flags |= MMC_BLK_CMD23;
2133 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002134
2135 if (mmc_card_mmc(card) &&
2136 md->flags & MMC_BLK_CMD23 &&
2137 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2138 card->ext_csd.rel_sectors)) {
2139 md->flags |= MMC_BLK_REL_WR;
2140 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2141 }
2142
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002143 if (mmc_card_mmc(card) &&
2144 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2145 (md->flags & MMC_BLK_CMD23) &&
2146 card->ext_csd.packed_event_en) {
2147 if (!mmc_packed_init(&md->queue, card))
2148 md->flags |= MMC_BLK_PACKED_CMD;
2149 }
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002152
2153 err_putdisk:
2154 put_disk(md->disk);
2155 err_kfree:
2156 kfree(md);
2157 out:
2158 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
Andrei Warkentin371a6892011-04-11 18:10:25 -05002161static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2162{
2163 sector_t size;
2164 struct mmc_blk_data *md;
2165
2166 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2167 /*
2168 * The EXT_CSD sector count is in number or 512 byte
2169 * sectors.
2170 */
2171 size = card->ext_csd.sectors;
2172 } else {
2173 /*
2174 * The CSD capacity field is in units of read_blkbits.
2175 * set_capacity takes units of 512 bytes.
2176 */
2177 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2178 }
2179
Johan Rudholmadd710e2011-12-02 08:51:06 +01002180 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2181 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002182 return md;
2183}
2184
2185static int mmc_blk_alloc_part(struct mmc_card *card,
2186 struct mmc_blk_data *md,
2187 unsigned int part_type,
2188 sector_t size,
2189 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002190 const char *subname,
2191 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002192{
2193 char cap_str[10];
2194 struct mmc_blk_data *part_md;
2195
2196 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002197 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002198 if (IS_ERR(part_md))
2199 return PTR_ERR(part_md);
2200 part_md->part_type = part_type;
2201 list_add(&part_md->part, &md->part);
2202
2203 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2204 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302205 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002206 part_md->disk->disk_name, mmc_card_id(card),
2207 mmc_card_name(card), part_md->part_type, cap_str);
2208 return 0;
2209}
2210
Namjae Jeone0c368d2011-10-06 23:41:38 +09002211/* MMC Physical partitions consist of two boot partitions and
2212 * up to four general purpose partitions.
2213 * For each partition enabled in EXT_CSD a block device will be allocatedi
2214 * to provide access to the partition.
2215 */
2216
Andrei Warkentin371a6892011-04-11 18:10:25 -05002217static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2218{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002219 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002220
2221 if (!mmc_card_mmc(card))
2222 return 0;
2223
Namjae Jeone0c368d2011-10-06 23:41:38 +09002224 for (idx = 0; idx < card->nr_parts; idx++) {
2225 if (card->part[idx].size) {
2226 ret = mmc_blk_alloc_part(card, md,
2227 card->part[idx].part_cfg,
2228 card->part[idx].size >> 9,
2229 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002230 card->part[idx].name,
2231 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002232 if (ret)
2233 return ret;
2234 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002235 }
2236
2237 return ret;
2238}
2239
Andrei Warkentin371a6892011-04-11 18:10:25 -05002240static void mmc_blk_remove_req(struct mmc_blk_data *md)
2241{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002242 struct mmc_card *card;
2243
Andrei Warkentin371a6892011-04-11 18:10:25 -05002244 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002245 /*
2246 * Flush remaining requests and free queues. It
2247 * is freeing the queue that stops new requests
2248 * from being accepted.
2249 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002250 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002251 mmc_cleanup_queue(&md->queue);
2252 if (md->flags & MMC_BLK_PACKED_CMD)
2253 mmc_packed_clean(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002254 if (md->disk->flags & GENHD_FL_UP) {
2255 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002256 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2257 card->ext_csd.boot_ro_lockable)
2258 device_remove_file(disk_to_dev(md->disk),
2259 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002260
Andrei Warkentin371a6892011-04-11 18:10:25 -05002261 del_gendisk(md->disk);
2262 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002263 mmc_blk_put(md);
2264 }
2265}
2266
2267static void mmc_blk_remove_parts(struct mmc_card *card,
2268 struct mmc_blk_data *md)
2269{
2270 struct list_head *pos, *q;
2271 struct mmc_blk_data *part_md;
2272
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002273 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002274 list_for_each_safe(pos, q, &md->part) {
2275 part_md = list_entry(pos, struct mmc_blk_data, part);
2276 list_del(pos);
2277 mmc_blk_remove_req(part_md);
2278 }
2279}
2280
2281static int mmc_add_disk(struct mmc_blk_data *md)
2282{
2283 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002284 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002285
2286 add_disk(md->disk);
2287 md->force_ro.show = force_ro_show;
2288 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302289 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002290 md->force_ro.attr.name = "force_ro";
2291 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2292 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2293 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002294 goto force_ro_fail;
2295
2296 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2297 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002298 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002299
2300 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2301 mode = S_IRUGO;
2302 else
2303 mode = S_IRUGO | S_IWUSR;
2304
2305 md->power_ro_lock.show = power_ro_lock_show;
2306 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002307 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002308 md->power_ro_lock.attr.mode = mode;
2309 md->power_ro_lock.attr.name =
2310 "ro_lock_until_next_power_on";
2311 ret = device_create_file(disk_to_dev(md->disk),
2312 &md->power_ro_lock);
2313 if (ret)
2314 goto power_ro_lock_fail;
2315 }
2316 return ret;
2317
2318power_ro_lock_fail:
2319 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2320force_ro_fail:
2321 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002322
2323 return ret;
2324}
2325
Chris Ballc59d4472011-11-11 22:01:43 -05002326#define CID_MANFID_SANDISK 0x2
2327#define CID_MANFID_TOSHIBA 0x11
2328#define CID_MANFID_MICRON 0x13
Ian Chen3550ccd2012-08-29 15:05:36 +09002329#define CID_MANFID_SAMSUNG 0x15
Chris Ballc59d4472011-11-11 22:01:43 -05002330
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002331static const struct mmc_fixup blk_fixups[] =
2332{
Chris Ballc59d4472011-11-11 22:01:43 -05002333 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2334 MMC_QUIRK_INAND_CMD38),
2335 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2336 MMC_QUIRK_INAND_CMD38),
2337 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2338 MMC_QUIRK_INAND_CMD38),
2339 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2340 MMC_QUIRK_INAND_CMD38),
2341 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2342 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002343
2344 /*
2345 * Some MMC cards experience performance degradation with CMD23
2346 * instead of CMD12-bounded multiblock transfers. For now we'll
2347 * black list what's bad...
2348 * - Certain Toshiba cards.
2349 *
2350 * N.B. This doesn't affect SD cards.
2351 */
Chris Ballc59d4472011-11-11 22:01:43 -05002352 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002353 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002354 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002355 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002356 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002357 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002358
2359 /*
2360 * Some Micron MMC cards needs longer data read timeout than
2361 * indicated in CSD.
2362 */
Chris Ballc59d4472011-11-11 22:01:43 -05002363 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002364 MMC_QUIRK_LONG_READ_TIME),
2365
Ian Chen3550ccd2012-08-29 15:05:36 +09002366 /*
2367 * On these Samsung MoviNAND parts, performing secure erase or
2368 * secure trim can result in unrecoverable corruption due to a
2369 * firmware bug.
2370 */
2371 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2372 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2373 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2374 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2375 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2376 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2377 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2378 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2379 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2380 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2381 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2382 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2383 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2384 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2385 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2386 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2387
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002388 END_FIXUP
2389};
2390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391static int mmc_blk_probe(struct mmc_card *card)
2392{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002393 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002394 char cap_str[10];
2395
Pierre Ossman912490d2005-05-21 10:27:02 +01002396 /*
2397 * Check that the card supports the command class(es) we need.
2398 */
2399 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 return -ENODEV;
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 md = mmc_blk_alloc(card);
2403 if (IS_ERR(md))
2404 return PTR_ERR(md);
2405
Yi Li444122f2009-02-05 15:31:57 +08002406 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002407 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302408 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002410 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Andrei Warkentin371a6892011-04-11 18:10:25 -05002412 if (mmc_blk_alloc_parts(card, md))
2413 goto out;
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002416 mmc_fixup_device(card, blk_fixups);
2417
Andrei Warkentin371a6892011-04-11 18:10:25 -05002418 if (mmc_add_disk(md))
2419 goto out;
2420
2421 list_for_each_entry(part_md, &md->part, part) {
2422 if (mmc_add_disk(part_md))
2423 goto out;
2424 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002425
2426 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2427 pm_runtime_use_autosuspend(&card->dev);
2428
2429 /*
2430 * Don't enable runtime PM for SD-combo cards here. Leave that
2431 * decision to be taken during the SDIO init sequence instead.
2432 */
2433 if (card->type != MMC_TYPE_SD_COMBO) {
2434 pm_runtime_set_active(&card->dev);
2435 pm_runtime_enable(&card->dev);
2436 }
2437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return 0;
2439
2440 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002441 mmc_blk_remove_parts(card, md);
2442 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
2446static void mmc_blk_remove(struct mmc_card *card)
2447{
2448 struct mmc_blk_data *md = mmc_get_drvdata(card);
2449
Andrei Warkentin371a6892011-04-11 18:10:25 -05002450 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002451 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002452 mmc_claim_host(card->host);
2453 mmc_blk_part_switch(card, md);
2454 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002455 if (card->type != MMC_TYPE_SD_COMBO)
2456 pm_runtime_disable(&card->dev);
2457 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002458 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 mmc_set_drvdata(card, NULL);
2460}
2461
Ulf Hansson76287742013-06-10 17:03:40 +02002462static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002464 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 struct mmc_blk_data *md = mmc_get_drvdata(card);
2466
2467 if (md) {
2468 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002469 list_for_each_entry(part_md, &md->part, part) {
2470 mmc_queue_suspend(&part_md->queue);
2471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 }
2473 return 0;
2474}
2475
Ulf Hansson76287742013-06-10 17:03:40 +02002476static void mmc_blk_shutdown(struct mmc_card *card)
2477{
2478 _mmc_blk_suspend(card);
2479}
2480
2481#ifdef CONFIG_PM
2482static int mmc_blk_suspend(struct mmc_card *card)
2483{
2484 return _mmc_blk_suspend(card);
2485}
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487static int mmc_blk_resume(struct mmc_card *card)
2488{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002489 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 struct mmc_blk_data *md = mmc_get_drvdata(card);
2491
2492 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002493 /*
2494 * Resume involves the card going into idle state,
2495 * so current partition is always the main one.
2496 */
2497 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002499 list_for_each_entry(part_md, &md->part, part) {
2500 mmc_queue_resume(&part_md->queue);
2501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 }
2503 return 0;
2504}
2505#else
2506#define mmc_blk_suspend NULL
2507#define mmc_blk_resume NULL
2508#endif
2509
2510static struct mmc_driver mmc_driver = {
2511 .drv = {
2512 .name = "mmcblk",
2513 },
2514 .probe = mmc_blk_probe,
2515 .remove = mmc_blk_remove,
2516 .suspend = mmc_blk_suspend,
2517 .resume = mmc_blk_resume,
Ulf Hansson76287742013-06-10 17:03:40 +02002518 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519};
2520
2521static int __init mmc_blk_init(void)
2522{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002523 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002525 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2526 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2527
2528 max_devices = 256 / perdev_minors;
2529
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002530 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2531 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002534 res = mmc_register_driver(&mmc_driver);
2535 if (res)
2536 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002538 return 0;
2539 out2:
2540 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 out:
2542 return res;
2543}
2544
2545static void __exit mmc_blk_exit(void)
2546{
2547 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002548 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549}
2550
2551module_init(mmc_blk_init);
2552module_exit(mmc_blk_exit);
2553
2554MODULE_LICENSE("GPL");
2555MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2556