blob: 817fcf8c0ac6b83ae898c51fff3b7bda36eb11f9 [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>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020038#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
John Calixtocb87ea22011-04-26 18:56:29 -040040#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020042#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010043#include <linux/mmc/mmc.h>
44#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/uaccess.h>
47
Pierre Ossman98ac2162006-12-23 20:03:02 +010048#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080049#include "block.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000051MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040052#ifdef MODULE_PARAM_PREFIX
53#undef MODULE_PARAM_PREFIX
54#endif
55#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010056
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050057#define INAND_CMD38_ARG_EXT_CSD 113
58#define INAND_CMD38_ARG_ERASE 0x00
59#define INAND_CMD38_ARG_TRIM 0x01
60#define INAND_CMD38_ARG_SECERASE 0x80
61#define INAND_CMD38_ARG_SECTRIM1 0x81
62#define INAND_CMD38_ARG_SECTRIM2 0x88
Trey Ramsay8fee4762012-11-16 09:31:41 -060063#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030064#define MMC_SANITIZE_REQ_TIMEOUT 240000
65#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050066
Luca Porziod3df0462015-11-06 15:12:26 +000067#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090068 (rq_data_dir(req) == WRITE))
69#define PACKED_CMD_VER 0x01
70#define PACKED_CMD_WR 0x02
71
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020072static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040073
74/*
75 * The defaults come from config options but can be overriden by module
76 * or bootarg options.
77 */
78static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
79
80/*
81 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000082 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020083 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040084 */
85static int max_devices;
86
Ben Hutchingsa26eba62014-11-06 03:35:09 +000087#define MAX_DEVICES 256
88
Ulf Hanssonb10fa992016-04-07 14:36:46 +020089static DEFINE_IDA(mmc_blk_ida);
90static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * There is one mmc_blk_data per slot.
94 */
95struct mmc_blk_data {
96 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -070097 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct gendisk *disk;
99 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500100 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500102 unsigned int flags;
103#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
104#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900105#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000108 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500109 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300110 unsigned int reset_done;
111#define MMC_BLK_READ BIT(0)
112#define MMC_BLK_WRITE BIT(1)
113#define MMC_BLK_DISCARD BIT(2)
114#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500115
116 /*
117 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200118 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500119 * track of the current selected device partition.
120 */
121 unsigned int part_curr;
122 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100123 struct device_attribute power_ro_lock;
124 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
Arjan van de Vena621aae2006-01-12 18:43:35 +0000127static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900129enum {
130 MMC_PACKED_NR_IDX = -1,
131 MMC_PACKED_NR_ZERO,
132 MMC_PACKED_NR_SINGLE,
133};
134
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400135module_param(perdev_minors, int, 0444);
136MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
137
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200138static inline int mmc_blk_part_switch(struct mmc_card *card,
139 struct mmc_blk_data *md);
140static int get_card_status(struct mmc_card *card, u32 *status, int retries);
141
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900142static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
143{
144 struct mmc_packed *packed = mqrq->packed;
145
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900146 mqrq->cmd_type = MMC_PACKED_NONE;
147 packed->nr_entries = MMC_PACKED_NR_ZERO;
148 packed->idx_failure = MMC_PACKED_NR_IDX;
149 packed->retries = 0;
150 packed->blocks = 0;
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
154{
155 struct mmc_blk_data *md;
156
Arjan van de Vena621aae2006-01-12 18:43:35 +0000157 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 md = disk->private_data;
159 if (md && md->usage == 0)
160 md = NULL;
161 if (md)
162 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000163 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 return md;
166}
167
Andrei Warkentin371a6892011-04-11 18:10:25 -0500168static inline int mmc_get_devidx(struct gendisk *disk)
169{
Colin Cross382c55f2015-10-22 10:00:41 -0700170 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500171 return devidx;
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static void mmc_blk_put(struct mmc_blk_data *md)
175{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000176 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 md->usage--;
178 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500179 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800180 blk_cleanup_queue(md->queue.queue);
181
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200182 spin_lock(&mmc_blk_lock);
183 ida_remove(&mmc_blk_ida, devidx);
184 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 kfree(md);
188 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000189 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Johan Rudholmadd710e2011-12-02 08:51:06 +0100192static ssize_t power_ro_lock_show(struct device *dev,
193 struct device_attribute *attr, char *buf)
194{
195 int ret;
196 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
197 struct mmc_card *card = md->queue.card;
198 int locked = 0;
199
200 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
201 locked = 2;
202 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
203 locked = 1;
204
205 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
206
Tomas Winkler9098f842015-07-16 15:50:45 +0200207 mmc_blk_put(md);
208
Johan Rudholmadd710e2011-12-02 08:51:06 +0100209 return ret;
210}
211
212static ssize_t power_ro_lock_store(struct device *dev,
213 struct device_attribute *attr, const char *buf, size_t count)
214{
215 int ret;
216 struct mmc_blk_data *md, *part_md;
217 struct mmc_card *card;
218 unsigned long set;
219
220 if (kstrtoul(buf, 0, &set))
221 return -EINVAL;
222
223 if (set != 1)
224 return count;
225
226 md = mmc_blk_get(dev_to_disk(dev));
227 card = md->queue.card;
228
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200229 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100230
231 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
232 card->ext_csd.boot_ro_lock |
233 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
234 card->ext_csd.part_time);
235 if (ret)
236 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
237 else
238 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
239
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200240 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100241
242 if (!ret) {
243 pr_info("%s: Locking boot partition ro until next power on\n",
244 md->disk->disk_name);
245 set_disk_ro(md->disk, 1);
246
247 list_for_each_entry(part_md, &md->part, part)
248 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
249 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
250 set_disk_ro(part_md->disk, 1);
251 }
252 }
253
254 mmc_blk_put(md);
255 return count;
256}
257
Andrei Warkentin371a6892011-04-11 18:10:25 -0500258static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
259 char *buf)
260{
261 int ret;
262 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
263
Baruch Siach0031a982014-09-22 10:12:51 +0300264 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500265 get_disk_ro(dev_to_disk(dev)) ^
266 md->read_only);
267 mmc_blk_put(md);
268 return ret;
269}
270
271static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
272 const char *buf, size_t count)
273{
274 int ret;
275 char *end;
276 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
277 unsigned long set = simple_strtoul(buf, &end, 0);
278 if (end == buf) {
279 ret = -EINVAL;
280 goto out;
281 }
282
283 set_disk_ro(dev_to_disk(dev), set || md->read_only);
284 ret = count;
285out:
286 mmc_blk_put(md);
287 return ret;
288}
289
Mark Salyzyn92f31302016-01-28 11:12:25 -0800290#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
291
292static int max_read_speed, max_write_speed, cache_size = 4;
293
294module_param(max_read_speed, int, S_IRUSR | S_IRGRP);
295MODULE_PARM_DESC(max_read_speed, "maximum KB/s read speed 0=off");
296module_param(max_write_speed, int, S_IRUSR | S_IRGRP);
297MODULE_PARM_DESC(max_write_speed, "maximum KB/s write speed 0=off");
298module_param(cache_size, int, S_IRUSR | S_IRGRP);
299MODULE_PARM_DESC(cache_size, "MB high speed memory or SLC cache");
300
301/*
302 * helper macros and expectations:
303 * size - unsigned long number of bytes
304 * jiffies - unsigned long HZ timestamp difference
305 * speed - unsigned KB/s transfer rate
306 */
307#define size_and_speed_to_jiffies(size, speed) \
308 ((size) * HZ / (speed) / 1024UL)
309#define jiffies_and_speed_to_size(jiffies, speed) \
310 (((speed) * (jiffies) * 1024UL) / HZ)
311#define jiffies_and_size_to_speed(jiffies, size) \
312 ((size) * HZ / (jiffies) / 1024UL)
313
314/* Limits to report warning */
315/* jiffies_and_size_to_speed(10*HZ, queue_max_hw_sectors(q) * 512UL) ~ 25 */
316#define MIN_SPEED(q) 250 /* 10 times faster than a floppy disk */
317#define MAX_SPEED(q) jiffies_and_size_to_speed(1, queue_max_sectors(q) * 512UL)
318
319#define speed_valid(speed) ((speed) > 0)
320
321static const char off[] = "off\n";
322
323static int max_speed_show(int speed, char *buf)
324{
325 if (speed)
326 return scnprintf(buf, PAGE_SIZE, "%uKB/s\n", speed);
327 else
328 return scnprintf(buf, PAGE_SIZE, off);
329}
330
331static int max_speed_store(const char *buf, struct request_queue *q)
332{
333 unsigned int limit, set = 0;
334
335 if (!strncasecmp(off, buf, sizeof(off) - 2))
336 return set;
337 if (kstrtouint(buf, 0, &set) || (set > INT_MAX))
338 return -EINVAL;
339 if (set == 0)
340 return set;
341 limit = MAX_SPEED(q);
342 if (set > limit)
343 pr_warn("max speed %u ineffective above %u\n", set, limit);
344 limit = MIN_SPEED(q);
345 if (set < limit)
346 pr_warn("max speed %u painful below %u\n", set, limit);
347 return set;
348}
349
350static ssize_t max_write_speed_show(struct device *dev,
351 struct device_attribute *attr, char *buf)
352{
353 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
354 int ret = max_speed_show(atomic_read(&md->queue.max_write_speed), buf);
355
356 mmc_blk_put(md);
357 return ret;
358}
359
360static ssize_t max_write_speed_store(struct device *dev,
361 struct device_attribute *attr,
362 const char *buf, size_t count)
363{
364 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
365 int set = max_speed_store(buf, md->queue.queue);
366
367 if (set < 0) {
368 mmc_blk_put(md);
369 return set;
370 }
371
372 atomic_set(&md->queue.max_write_speed, set);
373 mmc_blk_put(md);
374 return count;
375}
376
377static const DEVICE_ATTR(max_write_speed, S_IRUGO | S_IWUSR,
378 max_write_speed_show, max_write_speed_store);
379
380static ssize_t max_read_speed_show(struct device *dev,
381 struct device_attribute *attr, char *buf)
382{
383 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
384 int ret = max_speed_show(atomic_read(&md->queue.max_read_speed), buf);
385
386 mmc_blk_put(md);
387 return ret;
388}
389
390static ssize_t max_read_speed_store(struct device *dev,
391 struct device_attribute *attr,
392 const char *buf, size_t count)
393{
394 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
395 int set = max_speed_store(buf, md->queue.queue);
396
397 if (set < 0) {
398 mmc_blk_put(md);
399 return set;
400 }
401
402 atomic_set(&md->queue.max_read_speed, set);
403 mmc_blk_put(md);
404 return count;
405}
406
407static const DEVICE_ATTR(max_read_speed, S_IRUGO | S_IWUSR,
408 max_read_speed_show, max_read_speed_store);
409
410static ssize_t cache_size_show(struct device *dev,
411 struct device_attribute *attr, char *buf)
412{
413 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
414 struct mmc_queue *mq = &md->queue;
415 int cache_size = atomic_read(&mq->cache_size);
416 int ret;
417
418 if (!cache_size)
419 ret = scnprintf(buf, PAGE_SIZE, off);
420 else {
421 int speed = atomic_read(&mq->max_write_speed);
422
423 if (!speed_valid(speed))
424 ret = scnprintf(buf, PAGE_SIZE, "%uMB\n", cache_size);
425 else { /* We accept race between cache_jiffies and cache_used */
426 unsigned long size = jiffies_and_speed_to_size(
427 jiffies - mq->cache_jiffies, speed);
428 long used = atomic_long_read(&mq->cache_used);
429
430 if (size >= used)
431 size = 0;
432 else
433 size = (used - size) * 100 / cache_size
434 / 1024UL / 1024UL;
435
436 ret = scnprintf(buf, PAGE_SIZE, "%uMB %lu%% used\n",
437 cache_size, size);
438 }
439 }
440
441 mmc_blk_put(md);
442 return ret;
443}
444
445static ssize_t cache_size_store(struct device *dev,
446 struct device_attribute *attr,
447 const char *buf, size_t count)
448{
449 struct mmc_blk_data *md;
450 unsigned int set = 0;
451
452 if (strncasecmp(off, buf, sizeof(off) - 2)
453 && (kstrtouint(buf, 0, &set) || (set > INT_MAX)))
454 return -EINVAL;
455
456 md = mmc_blk_get(dev_to_disk(dev));
457 atomic_set(&md->queue.cache_size, set);
458 mmc_blk_put(md);
459 return count;
460}
461
462static const DEVICE_ATTR(cache_size, S_IRUGO | S_IWUSR,
463 cache_size_show, cache_size_store);
464
465/* correct for write-back */
466static long mmc_blk_cache_used(struct mmc_queue *mq, unsigned long waitfor)
467{
468 long used = 0;
469 int speed = atomic_read(&mq->max_write_speed);
470
471 if (speed_valid(speed)) {
472 unsigned long size = jiffies_and_speed_to_size(
473 waitfor - mq->cache_jiffies, speed);
474 used = atomic_long_read(&mq->cache_used);
475
476 if (size >= used)
477 used = 0;
478 else
479 used -= size;
480 }
481
482 atomic_long_set(&mq->cache_used, used);
483 mq->cache_jiffies = waitfor;
484
485 return used;
486}
487
488static void mmc_blk_simulate_delay(
489 struct mmc_queue *mq,
490 struct request *req,
491 unsigned long waitfor)
492{
493 int max_speed;
494
495 if (!req)
496 return;
497
498 max_speed = (rq_data_dir(req) == READ)
499 ? atomic_read(&mq->max_read_speed)
500 : atomic_read(&mq->max_write_speed);
501 if (speed_valid(max_speed)) {
502 unsigned long bytes = blk_rq_bytes(req);
503
504 if (rq_data_dir(req) != READ) {
505 int cache_size = atomic_read(&mq->cache_size);
506
507 if (cache_size) {
508 unsigned long size = cache_size * 1024L * 1024L;
509 long used = mmc_blk_cache_used(mq, waitfor);
510
511 used += bytes;
512 atomic_long_set(&mq->cache_used, used);
513 bytes = 0;
514 if (used > size)
515 bytes = used - size;
516 }
517 }
518 waitfor += size_and_speed_to_jiffies(bytes, max_speed);
519 if (time_is_after_jiffies(waitfor)) {
520 long msecs = jiffies_to_msecs(waitfor - jiffies);
521
522 if (likely(msecs > 0))
523 msleep(msecs);
524 }
525 }
526}
527
528#else
529
530#define mmc_blk_simulate_delay(mq, req, waitfor)
531
532#endif
533
Al Viroa5a15612008-03-02 10:33:30 -0500534static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Al Viroa5a15612008-03-02 10:33:30 -0500536 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int ret = -ENXIO;
538
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200539 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (md) {
541 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500542 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700544
Al Viroa5a15612008-03-02 10:33:30 -0500545 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700546 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700547 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200550 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 return ret;
553}
554
Al Virodb2a1442013-05-05 21:52:57 -0400555static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Al Viroa5a15612008-03-02 10:33:30 -0500557 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200559 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200561 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800565mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800567 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
568 geo->heads = 4;
569 geo->sectors = 16;
570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
John Calixtocb87ea22011-04-26 18:56:29 -0400573struct mmc_blk_ioc_data {
574 struct mmc_ioc_cmd ic;
575 unsigned char *buf;
576 u64 buf_bytes;
577};
578
579static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
580 struct mmc_ioc_cmd __user *user)
581{
582 struct mmc_blk_ioc_data *idata;
583 int err;
584
yalin wang1ff89502015-11-12 19:27:11 +0800585 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400586 if (!idata) {
587 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400588 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400589 }
590
591 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
592 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400593 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400594 }
595
596 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
597 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
598 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400599 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400600 }
601
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300602 if (!idata->buf_bytes) {
603 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100604 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300605 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100606
yalin wang1ff89502015-11-12 19:27:11 +0800607 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400608 if (!idata->buf) {
609 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400610 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400611 }
612
613 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
614 idata->ic.data_ptr, idata->buf_bytes)) {
615 err = -EFAULT;
616 goto copy_err;
617 }
618
619 return idata;
620
621copy_err:
622 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400623idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400624 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400625out:
John Calixtocb87ea22011-04-26 18:56:29 -0400626 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400627}
628
Jon Huntera5f57742015-09-22 10:27:53 +0100629static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
630 struct mmc_blk_ioc_data *idata)
631{
632 struct mmc_ioc_cmd *ic = &idata->ic;
633
634 if (copy_to_user(&(ic_ptr->response), ic->response,
635 sizeof(ic->response)))
636 return -EFAULT;
637
638 if (!idata->ic.write_flag) {
639 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
640 idata->buf, idata->buf_bytes))
641 return -EFAULT;
642 }
643
644 return 0;
645}
646
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200647static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
648 u32 retries_max)
649{
650 int err;
651 u32 retry_count = 0;
652
653 if (!status || !retries_max)
654 return -EINVAL;
655
656 do {
657 err = get_card_status(card, status, 5);
658 if (err)
659 break;
660
661 if (!R1_STATUS(*status) &&
662 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
663 break; /* RPMB programming operation complete */
664
665 /*
666 * Rechedule to give the MMC device a chance to continue
667 * processing the previous command without being polled too
668 * frequently.
669 */
670 usleep_range(1000, 5000);
671 } while (++retry_count < retries_max);
672
673 if (retry_count == retries_max)
674 err = -EPERM;
675
676 return err;
677}
678
Maya Erez775a9362013-04-18 15:41:55 +0300679static int ioctl_do_sanitize(struct mmc_card *card)
680{
681 int err;
682
Ulf Hanssona2d10862013-12-16 14:37:26 +0100683 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300684 pr_warn("%s: %s - SANITIZE is not supported\n",
685 mmc_hostname(card->host), __func__);
686 err = -EOPNOTSUPP;
687 goto out;
688 }
689
690 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
691 mmc_hostname(card->host), __func__);
692
693 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
694 EXT_CSD_SANITIZE_START, 1,
695 MMC_SANITIZE_REQ_TIMEOUT);
696
697 if (err)
698 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
699 mmc_hostname(card->host), __func__, err);
700
701 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
702 __func__);
703out:
704 return err;
705}
706
Jon Huntera5f57742015-09-22 10:27:53 +0100707static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
708 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400709{
John Calixtocb87ea22011-04-26 18:56:29 -0400710 struct mmc_command cmd = {0};
711 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530712 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400713 struct scatterlist sg;
714 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200715 int is_rpmb = false;
716 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400717
Jon Huntera5f57742015-09-22 10:27:53 +0100718 if (!card || !md || !idata)
719 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400720
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200721 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
722 is_rpmb = true;
723
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100724 cmd.opcode = idata->ic.opcode;
725 cmd.arg = idata->ic.arg;
726 cmd.flags = idata->ic.flags;
727
728 if (idata->buf_bytes) {
729 data.sg = &sg;
730 data.sg_len = 1;
731 data.blksz = idata->ic.blksz;
732 data.blocks = idata->ic.blocks;
733
734 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
735
736 if (idata->ic.write_flag)
737 data.flags = MMC_DATA_WRITE;
738 else
739 data.flags = MMC_DATA_READ;
740
741 /* data.flags must already be set before doing this. */
742 mmc_set_data_timeout(&data, card);
743
744 /* Allow overriding the timeout_ns for empirical tuning. */
745 if (idata->ic.data_timeout_ns)
746 data.timeout_ns = idata->ic.data_timeout_ns;
747
748 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
749 /*
750 * Pretend this is a data transfer and rely on the
751 * host driver to compute timeout. When all host
752 * drivers support cmd.cmd_timeout for R1B, this
753 * can be changed to:
754 *
755 * mrq.data = NULL;
756 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
757 */
758 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
759 }
760
761 mrq.data = &data;
762 }
763
764 mrq.cmd = &cmd;
765
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200766 err = mmc_blk_part_switch(card, md);
767 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100768 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200769
John Calixtocb87ea22011-04-26 18:56:29 -0400770 if (idata->ic.is_acmd) {
771 err = mmc_app_cmd(card->host, card);
772 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100773 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400774 }
775
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200776 if (is_rpmb) {
777 err = mmc_set_blockcount(card, data.blocks,
778 idata->ic.write_flag & (1 << 31));
779 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100780 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200781 }
782
Yaniv Gardia82e4842013-06-05 14:13:08 +0300783 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
784 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300785 err = ioctl_do_sanitize(card);
786
787 if (err)
788 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
789 __func__, err);
790
Jon Huntera5f57742015-09-22 10:27:53 +0100791 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300792 }
793
John Calixtocb87ea22011-04-26 18:56:29 -0400794 mmc_wait_for_req(card->host, &mrq);
795
796 if (cmd.error) {
797 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
798 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100799 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400800 }
801 if (data.error) {
802 dev_err(mmc_dev(card->host), "%s: data error %d\n",
803 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100804 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400805 }
806
807 /*
808 * According to the SD specs, some commands require a delay after
809 * issuing the command.
810 */
811 if (idata->ic.postsleep_min_us)
812 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
813
Jon Huntera5f57742015-09-22 10:27:53 +0100814 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400815
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200816 if (is_rpmb) {
817 /*
818 * Ensure RPMB command has completed by polling CMD13
819 * "Send Status".
820 */
821 err = ioctl_rpmb_card_status_poll(card, &status, 5);
822 if (err)
823 dev_err(mmc_dev(card->host),
824 "%s: Card Status=0x%08X, error %d\n",
825 __func__, status, err);
826 }
827
Jon Huntera5f57742015-09-22 10:27:53 +0100828 return err;
829}
830
831static int mmc_blk_ioctl_cmd(struct block_device *bdev,
832 struct mmc_ioc_cmd __user *ic_ptr)
833{
834 struct mmc_blk_ioc_data *idata;
835 struct mmc_blk_data *md;
836 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700837 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100838
Shawn Lin83c742c2016-03-16 18:15:47 +0800839 /*
840 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
841 * whole block device, not on a partition. This prevents overspray
842 * between sibling partitions.
843 */
844 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
845 return -EPERM;
846
Jon Huntera5f57742015-09-22 10:27:53 +0100847 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
848 if (IS_ERR(idata))
849 return PTR_ERR(idata);
850
851 md = mmc_blk_get(bdev->bd_disk);
852 if (!md) {
853 err = -EINVAL;
854 goto cmd_err;
855 }
856
857 card = md->queue.card;
858 if (IS_ERR(card)) {
859 err = PTR_ERR(card);
860 goto cmd_done;
861 }
862
863 mmc_get_card(card);
864
Grant Grundlerb0934102015-09-23 18:30:33 -0700865 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100866
Adrian Hunter3c866562016-05-04 14:38:12 +0300867 /* Always switch back to main area after RPMB access */
868 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
869 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
870
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200871 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400872
Grant Grundlerb0934102015-09-23 18:30:33 -0700873 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100874
John Calixtocb87ea22011-04-26 18:56:29 -0400875cmd_done:
876 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300877cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400878 kfree(idata->buf);
879 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700880 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400881}
882
Jon Huntera5f57742015-09-22 10:27:53 +0100883static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
884 struct mmc_ioc_multi_cmd __user *user)
885{
886 struct mmc_blk_ioc_data **idata = NULL;
887 struct mmc_ioc_cmd __user *cmds = user->cmds;
888 struct mmc_card *card;
889 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -0700890 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100891 __u64 num_of_cmds;
892
Shawn Lin83c742c2016-03-16 18:15:47 +0800893 /*
894 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
895 * whole block device, not on a partition. This prevents overspray
896 * between sibling partitions.
897 */
898 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
899 return -EPERM;
900
Jon Huntera5f57742015-09-22 10:27:53 +0100901 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
902 sizeof(num_of_cmds)))
903 return -EFAULT;
904
905 if (num_of_cmds > MMC_IOC_MAX_CMDS)
906 return -EINVAL;
907
908 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
909 if (!idata)
910 return -ENOMEM;
911
912 for (i = 0; i < num_of_cmds; i++) {
913 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
914 if (IS_ERR(idata[i])) {
915 err = PTR_ERR(idata[i]);
916 num_of_cmds = i;
917 goto cmd_err;
918 }
919 }
920
921 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -0800922 if (!md) {
923 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +0100924 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -0800925 }
Jon Huntera5f57742015-09-22 10:27:53 +0100926
927 card = md->queue.card;
928 if (IS_ERR(card)) {
929 err = PTR_ERR(card);
930 goto cmd_done;
931 }
932
933 mmc_get_card(card);
934
Grant Grundlerb0934102015-09-23 18:30:33 -0700935 for (i = 0; i < num_of_cmds && !ioc_err; i++)
936 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100937
Adrian Hunter3c866562016-05-04 14:38:12 +0300938 /* Always switch back to main area after RPMB access */
939 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
940 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
941
Jon Huntera5f57742015-09-22 10:27:53 +0100942 mmc_put_card(card);
943
944 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700945 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100946 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100947
948cmd_done:
949 mmc_blk_put(md);
950cmd_err:
951 for (i = 0; i < num_of_cmds; i++) {
952 kfree(idata[i]->buf);
953 kfree(idata[i]);
954 }
955 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700956 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100957}
958
John Calixtocb87ea22011-04-26 18:56:29 -0400959static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
960 unsigned int cmd, unsigned long arg)
961{
Jon Huntera5f57742015-09-22 10:27:53 +0100962 switch (cmd) {
963 case MMC_IOC_CMD:
964 return mmc_blk_ioctl_cmd(bdev,
965 (struct mmc_ioc_cmd __user *)arg);
966 case MMC_IOC_MULTI_CMD:
967 return mmc_blk_ioctl_multi_cmd(bdev,
968 (struct mmc_ioc_multi_cmd __user *)arg);
969 default:
970 return -EINVAL;
971 }
John Calixtocb87ea22011-04-26 18:56:29 -0400972}
973
974#ifdef CONFIG_COMPAT
975static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
976 unsigned int cmd, unsigned long arg)
977{
978 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
979}
980#endif
981
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700982static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500983 .open = mmc_blk_open,
984 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800985 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400987 .ioctl = mmc_blk_ioctl,
988#ifdef CONFIG_COMPAT
989 .compat_ioctl = mmc_blk_compat_ioctl,
990#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991};
992
Andrei Warkentin371a6892011-04-11 18:10:25 -0500993static inline int mmc_blk_part_switch(struct mmc_card *card,
994 struct mmc_blk_data *md)
995{
996 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200997 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300998
Andrei Warkentin371a6892011-04-11 18:10:25 -0500999 if (main_md->part_curr == md->part_type)
1000 return 0;
1001
1002 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001003 u8 part_config = card->ext_csd.part_config;
1004
Adrian Hunter57da0c02016-05-04 14:38:13 +03001005 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1006 mmc_retune_pause(card->host);
1007
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001008 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1009 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001010
1011 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001012 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001013 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001014 if (ret) {
1015 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1016 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001017 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001018 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001019
1020 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001021
1022 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1023 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001024 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001025
1026 main_md->part_curr = md->part_type;
1027 return 0;
1028}
1029
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001030static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1031{
1032 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001033 u32 result;
1034 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001035
Venkatraman Sad5fd972011-08-25 00:30:50 +05301036 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001037 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001038 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001039
1040 struct scatterlist sg;
1041
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001042 cmd.opcode = MMC_APP_CMD;
1043 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001044 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001045
1046 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001047 if (err)
1048 return (u32)-1;
1049 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001050 return (u32)-1;
1051
1052 memset(&cmd, 0, sizeof(struct mmc_command));
1053
1054 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1055 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001056 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001057
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001058 data.blksz = 4;
1059 data.blocks = 1;
1060 data.flags = MMC_DATA_READ;
1061 data.sg = &sg;
1062 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301063 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001064
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001065 mrq.cmd = &cmd;
1066 mrq.data = &data;
1067
Ben Dooks051913d2009-06-08 23:33:57 +01001068 blocks = kmalloc(4, GFP_KERNEL);
1069 if (!blocks)
1070 return (u32)-1;
1071
1072 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001073
1074 mmc_wait_for_req(card->host, &mrq);
1075
Ben Dooks051913d2009-06-08 23:33:57 +01001076 result = ntohl(*blocks);
1077 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001078
Ben Dooks051913d2009-06-08 23:33:57 +01001079 if (cmd.error || data.error)
1080 result = (u32)-1;
1081
1082 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001083}
1084
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001085static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001086{
Chris Ball1278dba2011-04-13 23:40:30 -04001087 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001088 int err;
1089
Adrian Hunter504f1912008-10-16 12:55:25 +03001090 cmd.opcode = MMC_SEND_STATUS;
1091 if (!mmc_host_is_spi(card->host))
1092 cmd.arg = card->rca << 16;
1093 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001094 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1095 if (err == 0)
1096 *status = cmd.resp[0];
1097 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001098}
1099
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001100static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001101 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001102{
1103 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1104 int err = 0;
1105 u32 status;
1106
1107 do {
1108 err = get_card_status(card, &status, 5);
1109 if (err) {
1110 pr_err("%s: error %d requesting status\n",
1111 req->rq_disk->disk_name, err);
1112 return err;
1113 }
1114
1115 if (status & R1_ERROR) {
1116 pr_err("%s: %s: error sending status cmd, status %#x\n",
1117 req->rq_disk->disk_name, __func__, status);
1118 *gen_err = 1;
1119 }
1120
Ulf Hansson95a91292014-01-29 13:11:27 +01001121 /* We may rely on the host hw to handle busy detection.*/
1122 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1123 hw_busy_detect)
1124 break;
1125
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001126 /*
1127 * Timeout if the device never becomes ready for data and never
1128 * leaves the program state.
1129 */
1130 if (time_after(jiffies, timeout)) {
1131 pr_err("%s: Card stuck in programming state! %s %s\n",
1132 mmc_hostname(card->host),
1133 req->rq_disk->disk_name, __func__);
1134 return -ETIMEDOUT;
1135 }
1136
1137 /*
1138 * Some cards mishandle the status bits,
1139 * so make sure to check both the busy
1140 * indication and the card state.
1141 */
1142 } while (!(status & R1_READY_FOR_DATA) ||
1143 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1144
1145 return err;
1146}
1147
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001148static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1149 struct request *req, int *gen_err, u32 *stop_status)
1150{
1151 struct mmc_host *host = card->host;
1152 struct mmc_command cmd = {0};
1153 int err;
1154 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1155
1156 /*
1157 * Normally we use R1B responses for WRITE, but in cases where the host
1158 * has specified a max_busy_timeout we need to validate it. A failure
1159 * means we need to prevent the host from doing hw busy detection, which
1160 * is done by converting to a R1 response instead.
1161 */
1162 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1163 use_r1b_resp = false;
1164
1165 cmd.opcode = MMC_STOP_TRANSMISSION;
1166 if (use_r1b_resp) {
1167 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1168 cmd.busy_timeout = timeout_ms;
1169 } else {
1170 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1171 }
1172
1173 err = mmc_wait_for_cmd(host, &cmd, 5);
1174 if (err)
1175 return err;
1176
1177 *stop_status = cmd.resp[0];
1178
1179 /* No need to check card status in case of READ. */
1180 if (rq_data_dir(req) == READ)
1181 return 0;
1182
1183 if (!mmc_host_is_spi(host) &&
1184 (*stop_status & R1_ERROR)) {
1185 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1186 req->rq_disk->disk_name, __func__, *stop_status);
1187 *gen_err = 1;
1188 }
1189
1190 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1191}
1192
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301193#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001194#define ERR_RETRY 2
1195#define ERR_ABORT 1
1196#define ERR_CONTINUE 0
1197
1198static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1199 bool status_valid, u32 status)
1200{
1201 switch (error) {
1202 case -EILSEQ:
1203 /* response crc error, retry the r/w cmd */
1204 pr_err("%s: %s sending %s command, card status %#x\n",
1205 req->rq_disk->disk_name, "response CRC error",
1206 name, status);
1207 return ERR_RETRY;
1208
1209 case -ETIMEDOUT:
1210 pr_err("%s: %s sending %s command, card status %#x\n",
1211 req->rq_disk->disk_name, "timed out", name, status);
1212
1213 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301214 if (!status_valid) {
1215 pr_err("%s: status not valid, retrying timeout\n",
1216 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001217 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301218 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001219
1220 /*
1221 * If it was a r/w cmd crc error, or illegal command
1222 * (eg, issued in wrong state) then retry - we should
1223 * have corrected the state problem above.
1224 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301225 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1226 pr_err("%s: command error, retrying timeout\n",
1227 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001228 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301229 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001230
1231 /* Otherwise abort the command */
1232 return ERR_ABORT;
1233
1234 default:
1235 /* We don't understand the error code the driver gave us */
1236 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1237 req->rq_disk->disk_name, error, status);
1238 return ERR_ABORT;
1239 }
1240}
1241
1242/*
1243 * Initial r/w and stop cmd error recovery.
1244 * We don't know whether the card received the r/w cmd or not, so try to
1245 * restore things back to a sane state. Essentially, we do this as follows:
1246 * - Obtain card status. If the first attempt to obtain card status fails,
1247 * the status word will reflect the failed status cmd, not the failed
1248 * r/w cmd. If we fail to obtain card status, it suggests we can no
1249 * longer communicate with the card.
1250 * - Check the card state. If the card received the cmd but there was a
1251 * transient problem with the response, it might still be in a data transfer
1252 * mode. Try to send it a stop command. If this fails, we can't recover.
1253 * - If the r/w cmd failed due to a response CRC error, it was probably
1254 * transient, so retry the cmd.
1255 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1256 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1257 * illegal cmd, retry.
1258 * Otherwise we don't understand what happened, so abort.
1259 */
1260static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001261 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001262{
1263 bool prev_cmd_status_valid = true;
1264 u32 status, stop_status = 0;
1265 int err, retry;
1266
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301267 if (mmc_card_removed(card))
1268 return ERR_NOMEDIUM;
1269
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001270 /*
1271 * Try to get card status which indicates both the card state
1272 * and why there was no response. If the first attempt fails,
1273 * we can't be sure the returned status is for the r/w command.
1274 */
1275 for (retry = 2; retry >= 0; retry--) {
1276 err = get_card_status(card, &status, 0);
1277 if (!err)
1278 break;
1279
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001280 /* Re-tune if needed */
1281 mmc_retune_recheck(card->host);
1282
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001283 prev_cmd_status_valid = false;
1284 pr_err("%s: error %d sending status command, %sing\n",
1285 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1286 }
1287
1288 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301289 if (err) {
1290 /* Check if the card is removed */
1291 if (mmc_detect_card_removed(card->host))
1292 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001293 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301294 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001295
Adrian Hunter67716322011-08-29 16:42:15 +03001296 /* Flag ECC errors */
1297 if ((status & R1_CARD_ECC_FAILED) ||
1298 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1299 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1300 *ecc_err = 1;
1301
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001302 /* Flag General errors */
1303 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1304 if ((status & R1_ERROR) ||
1305 (brq->stop.resp[0] & R1_ERROR)) {
1306 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1307 req->rq_disk->disk_name, __func__,
1308 brq->stop.resp[0], status);
1309 *gen_err = 1;
1310 }
1311
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001312 /*
1313 * Check the current card state. If it is in some data transfer
1314 * mode, tell it to stop (and hopefully transition back to TRAN.)
1315 */
1316 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1317 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001318 err = send_stop(card,
1319 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1320 req, gen_err, &stop_status);
1321 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001322 pr_err("%s: error %d sending stop command\n",
1323 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001324 /*
1325 * If the stop cmd also timed out, the card is probably
1326 * not present, so abort. Other errors are bad news too.
1327 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001328 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001329 }
1330
Adrian Hunter67716322011-08-29 16:42:15 +03001331 if (stop_status & R1_CARD_ECC_FAILED)
1332 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001333 }
1334
1335 /* Check for set block count errors */
1336 if (brq->sbc.error)
1337 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1338 prev_cmd_status_valid, status);
1339
1340 /* Check for r/w command errors */
1341 if (brq->cmd.error)
1342 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1343 prev_cmd_status_valid, status);
1344
Adrian Hunter67716322011-08-29 16:42:15 +03001345 /* Data errors */
1346 if (!brq->stop.error)
1347 return ERR_CONTINUE;
1348
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001349 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001350 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001351 req->rq_disk->disk_name, brq->stop.error,
1352 brq->cmd.resp[0], status);
1353
1354 /*
1355 * Subsitute in our own stop status as this will give the error
1356 * state which happened during the execution of the r/w command.
1357 */
1358 if (stop_status) {
1359 brq->stop.resp[0] = stop_status;
1360 brq->stop.error = 0;
1361 }
1362 return ERR_CONTINUE;
1363}
1364
Adrian Hunter67716322011-08-29 16:42:15 +03001365static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1366 int type)
1367{
1368 int err;
1369
1370 if (md->reset_done & type)
1371 return -EEXIST;
1372
1373 md->reset_done |= type;
1374 err = mmc_hw_reset(host);
1375 /* Ensure we switch back to the correct partition */
1376 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001377 struct mmc_blk_data *main_md =
1378 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001379 int part_err;
1380
1381 main_md->part_curr = main_md->part_type;
1382 part_err = mmc_blk_part_switch(host->card, md);
1383 if (part_err) {
1384 /*
1385 * We have failed to get back into the correct
1386 * partition, so we need to abort the whole request.
1387 */
1388 return -ENODEV;
1389 }
1390 }
1391 return err;
1392}
1393
1394static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1395{
1396 md->reset_done &= ~type;
1397}
1398
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001399int mmc_access_rpmb(struct mmc_queue *mq)
1400{
1401 struct mmc_blk_data *md = mq->data;
1402 /*
1403 * If this is a RPMB partition access, return ture
1404 */
1405 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1406 return true;
1407
1408 return false;
1409}
1410
Adrian Hunterbd788c92010-08-11 14:17:47 -07001411static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1412{
1413 struct mmc_blk_data *md = mq->data;
1414 struct mmc_card *card = md->queue.card;
1415 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001416 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001417
Adrian Hunterbd788c92010-08-11 14:17:47 -07001418 if (!mmc_can_erase(card)) {
1419 err = -EOPNOTSUPP;
1420 goto out;
1421 }
1422
1423 from = blk_rq_pos(req);
1424 nr = blk_rq_sectors(req);
1425
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001426 if (mmc_can_discard(card))
1427 arg = MMC_DISCARD_ARG;
1428 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001429 arg = MMC_TRIM_ARG;
1430 else
1431 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001432retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001433 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1434 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1435 INAND_CMD38_ARG_EXT_CSD,
1436 arg == MMC_TRIM_ARG ?
1437 INAND_CMD38_ARG_TRIM :
1438 INAND_CMD38_ARG_ERASE,
1439 0);
1440 if (err)
1441 goto out;
1442 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001443 err = mmc_erase(card, from, nr, arg);
1444out:
Adrian Hunter67716322011-08-29 16:42:15 +03001445 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1446 goto retry;
1447 if (!err)
1448 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301449 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001450
Adrian Hunterbd788c92010-08-11 14:17:47 -07001451 return err ? 0 : 1;
1452}
1453
Adrian Hunter49804542010-08-11 14:17:50 -07001454static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1455 struct request *req)
1456{
1457 struct mmc_blk_data *md = mq->data;
1458 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001459 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001460 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001461
Maya Erez775a9362013-04-18 15:41:55 +03001462 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001463 err = -EOPNOTSUPP;
1464 goto out;
1465 }
1466
1467 from = blk_rq_pos(req);
1468 nr = blk_rq_sectors(req);
1469
Maya Erez775a9362013-04-18 15:41:55 +03001470 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1471 arg = MMC_SECURE_TRIM1_ARG;
1472 else
1473 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001474
Adrian Hunter67716322011-08-29 16:42:15 +03001475retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001476 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1477 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1478 INAND_CMD38_ARG_EXT_CSD,
1479 arg == MMC_SECURE_TRIM1_ARG ?
1480 INAND_CMD38_ARG_SECTRIM1 :
1481 INAND_CMD38_ARG_SECERASE,
1482 0);
1483 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001484 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001485 }
Adrian Hunter28302812012-04-05 14:45:48 +03001486
Adrian Hunter49804542010-08-11 14:17:50 -07001487 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001488 if (err == -EIO)
1489 goto out_retry;
1490 if (err)
1491 goto out;
1492
1493 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001494 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1495 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1496 INAND_CMD38_ARG_EXT_CSD,
1497 INAND_CMD38_ARG_SECTRIM2,
1498 0);
1499 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001500 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001501 }
Adrian Hunter28302812012-04-05 14:45:48 +03001502
Adrian Hunter49804542010-08-11 14:17:50 -07001503 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001504 if (err == -EIO)
1505 goto out_retry;
1506 if (err)
1507 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001508 }
Adrian Hunter28302812012-04-05 14:45:48 +03001509
Adrian Hunter28302812012-04-05 14:45:48 +03001510out_retry:
1511 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001512 goto retry;
1513 if (!err)
1514 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001515out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301516 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001517
Adrian Hunter49804542010-08-11 14:17:50 -07001518 return err ? 0 : 1;
1519}
1520
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001521static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1522{
1523 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001524 struct mmc_card *card = md->queue.card;
1525 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001526
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001527 ret = mmc_flush_cache(card);
1528 if (ret)
1529 ret = -EIO;
1530
Mark Salyzyn92f31302016-01-28 11:12:25 -08001531#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
1532 else if (atomic_read(&mq->cache_size)) {
1533 long used = mmc_blk_cache_used(mq, jiffies);
1534
1535 if (used) {
1536 int speed = atomic_read(&mq->max_write_speed);
1537
1538 if (speed_valid(speed)) {
1539 unsigned long msecs = jiffies_to_msecs(
1540 size_and_speed_to_jiffies(
1541 used, speed));
1542 if (msecs)
1543 msleep(msecs);
1544 }
1545 }
1546 }
1547#endif
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301548 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001549
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001550 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001551}
1552
1553/*
1554 * Reformat current write as a reliable write, supporting
1555 * both legacy and the enhanced reliable write MMC cards.
1556 * In each transfer we'll handle only as much as a single
1557 * reliable write can handle, thus finish the request in
1558 * partial completions.
1559 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001560static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1561 struct mmc_card *card,
1562 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001563{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001564 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1565 /* Legacy mode imposes restrictions on transfers. */
1566 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1567 brq->data.blocks = 1;
1568
1569 if (brq->data.blocks > card->ext_csd.rel_sectors)
1570 brq->data.blocks = card->ext_csd.rel_sectors;
1571 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1572 brq->data.blocks = 1;
1573 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001574}
1575
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001576#define CMD_ERRORS \
1577 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1578 R1_ADDRESS_ERROR | /* Misaligned address */ \
1579 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1580 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1581 R1_CC_ERROR | /* Card controller error */ \
1582 R1_ERROR) /* General/unknown error */
1583
Per Forlinee8a43a2011-07-01 18:55:33 +02001584static int mmc_blk_err_check(struct mmc_card *card,
1585 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001586{
Per Forlinee8a43a2011-07-01 18:55:33 +02001587 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1588 mmc_active);
1589 struct mmc_blk_request *brq = &mq_mrq->brq;
1590 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001591 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001592 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001593
1594 /*
1595 * sbc.error indicates a problem with the set block count
1596 * command. No data will have been transferred.
1597 *
1598 * cmd.error indicates a problem with the r/w command. No
1599 * data will have been transferred.
1600 *
1601 * stop.error indicates a problem with the stop command. Data
1602 * may have been transferred, or may still be transferring.
1603 */
Adrian Hunter67716322011-08-29 16:42:15 +03001604 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1605 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001606 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001607 case ERR_RETRY:
1608 return MMC_BLK_RETRY;
1609 case ERR_ABORT:
1610 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301611 case ERR_NOMEDIUM:
1612 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001613 case ERR_CONTINUE:
1614 break;
1615 }
1616 }
1617
1618 /*
1619 * Check for errors relating to the execution of the
1620 * initial command - such as address errors. No data
1621 * has been transferred.
1622 */
1623 if (brq->cmd.resp[0] & CMD_ERRORS) {
1624 pr_err("%s: r/w command failed, status = %#x\n",
1625 req->rq_disk->disk_name, brq->cmd.resp[0]);
1626 return MMC_BLK_ABORT;
1627 }
1628
1629 /*
1630 * Everything else is either success, or a data error of some
1631 * kind. If it was a write, we may have transitioned to
1632 * program mode, which we have to wait for it to complete.
1633 */
1634 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001635 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001636
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001637 /* Check stop command response */
1638 if (brq->stop.resp[0] & R1_ERROR) {
1639 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1640 req->rq_disk->disk_name, __func__,
1641 brq->stop.resp[0]);
1642 gen_err = 1;
1643 }
1644
Ulf Hansson95a91292014-01-29 13:11:27 +01001645 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1646 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001647 if (err)
1648 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001649 }
1650
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001651 /* if general error occurs, retry the write operation. */
1652 if (gen_err) {
1653 pr_warn("%s: retrying write for general error\n",
1654 req->rq_disk->disk_name);
1655 return MMC_BLK_RETRY;
1656 }
1657
Per Forlind78d4a82011-07-01 18:55:30 +02001658 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001659 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001660 pr_debug("%s: retrying because a re-tune was needed\n",
1661 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001662 brq->retune_retry_done = 1;
1663 return MMC_BLK_RETRY;
1664 }
Per Forlind78d4a82011-07-01 18:55:30 +02001665 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1666 req->rq_disk->disk_name, brq->data.error,
1667 (unsigned)blk_rq_pos(req),
1668 (unsigned)blk_rq_sectors(req),
1669 brq->cmd.resp[0], brq->stop.resp[0]);
1670
1671 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001672 if (ecc_err)
1673 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001674 return MMC_BLK_DATA_ERR;
1675 } else {
1676 return MMC_BLK_CMD_ERR;
1677 }
1678 }
1679
Adrian Hunter67716322011-08-29 16:42:15 +03001680 if (!brq->data.bytes_xfered)
1681 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001682
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001683 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1684 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1685 return MMC_BLK_PARTIAL;
1686 else
1687 return MMC_BLK_SUCCESS;
1688 }
1689
Adrian Hunter67716322011-08-29 16:42:15 +03001690 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1691 return MMC_BLK_PARTIAL;
1692
1693 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001694}
1695
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001696static int mmc_blk_packed_err_check(struct mmc_card *card,
1697 struct mmc_async_req *areq)
1698{
1699 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1700 mmc_active);
1701 struct request *req = mq_rq->req;
1702 struct mmc_packed *packed = mq_rq->packed;
1703 int err, check, status;
1704 u8 *ext_csd;
1705
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001706 packed->retries--;
1707 check = mmc_blk_err_check(card, areq);
1708 err = get_card_status(card, &status, 0);
1709 if (err) {
1710 pr_err("%s: error %d sending status command\n",
1711 req->rq_disk->disk_name, err);
1712 return MMC_BLK_ABORT;
1713 }
1714
1715 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02001716 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001717 if (err) {
1718 pr_err("%s: error %d sending ext_csd\n",
1719 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02001720 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001721 }
1722
1723 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1724 EXT_CSD_PACKED_FAILURE) &&
1725 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1726 EXT_CSD_PACKED_GENERIC_ERROR)) {
1727 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1728 EXT_CSD_PACKED_INDEXED_ERROR) {
1729 packed->idx_failure =
1730 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1731 check = MMC_BLK_PARTIAL;
1732 }
1733 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1734 "failure index: %d\n",
1735 req->rq_disk->disk_name, packed->nr_entries,
1736 packed->blocks, packed->idx_failure);
1737 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001738 kfree(ext_csd);
1739 }
1740
1741 return check;
1742}
1743
Per Forlin54d49d72011-07-01 18:55:29 +02001744static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1745 struct mmc_card *card,
1746 int disable_multi,
1747 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Per Forlin54d49d72011-07-01 18:55:29 +02001749 u32 readcmd, writecmd;
1750 struct mmc_blk_request *brq = &mqrq->brq;
1751 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301753 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001755 /*
1756 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001757 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001758 */
Luca Porziod3df0462015-11-06 15:12:26 +00001759 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001760 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001761 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001762
Per Forlin54d49d72011-07-01 18:55:29 +02001763 memset(brq, 0, sizeof(struct mmc_blk_request));
1764 brq->mrq.cmd = &brq->cmd;
1765 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Per Forlin54d49d72011-07-01 18:55:29 +02001767 brq->cmd.arg = blk_rq_pos(req);
1768 if (!mmc_card_blockaddr(card))
1769 brq->cmd.arg <<= 9;
1770 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1771 brq->data.blksz = 512;
1772 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1773 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001774 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Per Forlin54d49d72011-07-01 18:55:29 +02001776 /*
1777 * The block layer doesn't support all sector count
1778 * restrictions, so we need to be prepared for too big
1779 * requests.
1780 */
1781 if (brq->data.blocks > card->host->max_blk_count)
1782 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001784 if (brq->data.blocks > 1) {
1785 /*
1786 * After a read error, we redo the request one sector
1787 * at a time in order to accurately determine which
1788 * sectors can be read successfully.
1789 */
1790 if (disable_multi)
1791 brq->data.blocks = 1;
1792
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001793 /*
1794 * Some controllers have HW issues while operating
1795 * in multiple I/O mode
1796 */
1797 if (card->host->ops->multi_io_quirk)
1798 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1799 (rq_data_dir(req) == READ) ?
1800 MMC_DATA_READ : MMC_DATA_WRITE,
1801 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001802 }
Per Forlin54d49d72011-07-01 18:55:29 +02001803
1804 if (brq->data.blocks > 1 || do_rel_wr) {
1805 /* SPI multiblock writes terminate using a special
1806 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001807 */
Per Forlin54d49d72011-07-01 18:55:29 +02001808 if (!mmc_host_is_spi(card->host) ||
1809 rq_data_dir(req) == READ)
1810 brq->mrq.stop = &brq->stop;
1811 readcmd = MMC_READ_MULTIPLE_BLOCK;
1812 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1813 } else {
1814 brq->mrq.stop = NULL;
1815 readcmd = MMC_READ_SINGLE_BLOCK;
1816 writecmd = MMC_WRITE_BLOCK;
1817 }
1818 if (rq_data_dir(req) == READ) {
1819 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001820 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001821 if (brq->mrq.stop)
1822 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1823 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001824 } else {
1825 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001826 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001827 if (brq->mrq.stop)
1828 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1829 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001830 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001831
Per Forlin54d49d72011-07-01 18:55:29 +02001832 if (do_rel_wr)
1833 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001834
Per Forlin54d49d72011-07-01 18:55:29 +02001835 /*
Saugata Das42659002011-12-21 13:09:17 +05301836 * Data tag is used only during writing meta data to speed
1837 * up write and any subsequent read of this meta data
1838 */
1839 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1840 (req->cmd_flags & REQ_META) &&
1841 (rq_data_dir(req) == WRITE) &&
1842 ((brq->data.blocks * brq->data.blksz) >=
1843 card->ext_csd.data_tag_unit_size);
1844
1845 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001846 * Pre-defined multi-block transfers are preferable to
1847 * open ended-ones (and necessary for reliable writes).
1848 * However, it is not sufficient to just send CMD23,
1849 * and avoid the final CMD12, as on an error condition
1850 * CMD12 (stop) needs to be sent anyway. This, coupled
1851 * with Auto-CMD23 enhancements provided by some
1852 * hosts, means that the complexity of dealing
1853 * with this is best left to the host. If CMD23 is
1854 * supported by card and host, we'll fill sbc in and let
1855 * the host deal with handling it correctly. This means
1856 * that for hosts that don't expose MMC_CAP_CMD23, no
1857 * change of behavior will be observed.
1858 *
1859 * N.B: Some MMC cards experience perf degradation.
1860 * We'll avoid using CMD23-bounded multiblock writes for
1861 * these, while retaining features like reliable writes.
1862 */
Saugata Das42659002011-12-21 13:09:17 +05301863 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1864 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1865 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001866 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1867 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301868 (do_rel_wr ? (1 << 31) : 0) |
1869 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001870 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1871 brq->mrq.sbc = &brq->sbc;
1872 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001873
Per Forlin54d49d72011-07-01 18:55:29 +02001874 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001875
Per Forlin54d49d72011-07-01 18:55:29 +02001876 brq->data.sg = mqrq->sg;
1877 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001878
Per Forlin54d49d72011-07-01 18:55:29 +02001879 /*
1880 * Adjust the sg list so it is the same size as the
1881 * request.
1882 */
1883 if (brq->data.blocks != blk_rq_sectors(req)) {
1884 int i, data_size = brq->data.blocks << 9;
1885 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001886
Per Forlin54d49d72011-07-01 18:55:29 +02001887 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1888 data_size -= sg->length;
1889 if (data_size <= 0) {
1890 sg->length += data_size;
1891 i++;
1892 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001893 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001894 }
Per Forlin54d49d72011-07-01 18:55:29 +02001895 brq->data.sg_len = i;
1896 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001897
Per Forlinee8a43a2011-07-01 18:55:33 +02001898 mqrq->mmc_active.mrq = &brq->mrq;
1899 mqrq->mmc_active.err_check = mmc_blk_err_check;
1900
Per Forlin54d49d72011-07-01 18:55:29 +02001901 mmc_queue_bounce_pre(mqrq);
1902}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001904static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1905 struct mmc_card *card)
1906{
1907 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1908 unsigned int max_seg_sz = queue_max_segment_size(q);
1909 unsigned int len, nr_segs = 0;
1910
1911 do {
1912 len = min(hdr_sz, max_seg_sz);
1913 hdr_sz -= len;
1914 nr_segs++;
1915 } while (hdr_sz);
1916
1917 return nr_segs;
1918}
1919
1920static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1921{
1922 struct request_queue *q = mq->queue;
1923 struct mmc_card *card = mq->card;
1924 struct request *cur = req, *next = NULL;
1925 struct mmc_blk_data *md = mq->data;
1926 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1927 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1928 unsigned int req_sectors = 0, phys_segments = 0;
1929 unsigned int max_blk_count, max_phys_segs;
1930 bool put_back = true;
1931 u8 max_packed_rw = 0;
1932 u8 reqs = 0;
1933
Shawn Lin96e52da2016-08-26 08:49:55 +08001934 /*
1935 * We don't need to check packed for any further
1936 * operation of packed stuff as we set MMC_PACKED_NONE
1937 * and return zero for reqs if geting null packed. Also
1938 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
1939 * it again when removing blk req.
1940 */
1941 if (!mqrq->packed) {
1942 md->flags &= (~MMC_BLK_PACKED_CMD);
1943 goto no_packed;
1944 }
1945
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001946 if (!(md->flags & MMC_BLK_PACKED_CMD))
1947 goto no_packed;
1948
1949 if ((rq_data_dir(cur) == WRITE) &&
1950 mmc_host_packed_wr(card->host))
1951 max_packed_rw = card->ext_csd.max_packed_writes;
1952
1953 if (max_packed_rw == 0)
1954 goto no_packed;
1955
1956 if (mmc_req_rel_wr(cur) &&
1957 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1958 goto no_packed;
1959
1960 if (mmc_large_sector(card) &&
1961 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1962 goto no_packed;
1963
1964 mmc_blk_clear_packed(mqrq);
1965
1966 max_blk_count = min(card->host->max_blk_count,
1967 card->host->max_req_size >> 9);
1968 if (unlikely(max_blk_count > 0xffff))
1969 max_blk_count = 0xffff;
1970
1971 max_phys_segs = queue_max_segments(q);
1972 req_sectors += blk_rq_sectors(cur);
1973 phys_segments += cur->nr_phys_segments;
1974
1975 if (rq_data_dir(cur) == WRITE) {
1976 req_sectors += mmc_large_sector(card) ? 8 : 1;
1977 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1978 }
1979
1980 do {
1981 if (reqs >= max_packed_rw - 1) {
1982 put_back = false;
1983 break;
1984 }
1985
1986 spin_lock_irq(q->queue_lock);
1987 next = blk_fetch_request(q);
1988 spin_unlock_irq(q->queue_lock);
1989 if (!next) {
1990 put_back = false;
1991 break;
1992 }
1993
1994 if (mmc_large_sector(card) &&
1995 !IS_ALIGNED(blk_rq_sectors(next), 8))
1996 break;
1997
Mike Christie3a5e02c2016-06-05 14:32:23 -05001998 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03001999 req_op(next) == REQ_OP_SECURE_ERASE ||
Mike Christie3a5e02c2016-06-05 14:32:23 -05002000 req_op(next) == REQ_OP_FLUSH)
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002001 break;
2002
2003 if (rq_data_dir(cur) != rq_data_dir(next))
2004 break;
2005
2006 if (mmc_req_rel_wr(next) &&
2007 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2008 break;
2009
2010 req_sectors += blk_rq_sectors(next);
2011 if (req_sectors > max_blk_count)
2012 break;
2013
2014 phys_segments += next->nr_phys_segments;
2015 if (phys_segments > max_phys_segs)
2016 break;
2017
2018 list_add_tail(&next->queuelist, &mqrq->packed->list);
2019 cur = next;
2020 reqs++;
2021 } while (1);
2022
2023 if (put_back) {
2024 spin_lock_irq(q->queue_lock);
2025 blk_requeue_request(q, next);
2026 spin_unlock_irq(q->queue_lock);
2027 }
2028
2029 if (reqs > 0) {
2030 list_add(&req->queuelist, &mqrq->packed->list);
2031 mqrq->packed->nr_entries = ++reqs;
2032 mqrq->packed->retries = reqs;
2033 return reqs;
2034 }
2035
2036no_packed:
2037 mqrq->cmd_type = MMC_PACKED_NONE;
2038 return 0;
2039}
2040
2041static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2042 struct mmc_card *card,
2043 struct mmc_queue *mq)
2044{
2045 struct mmc_blk_request *brq = &mqrq->brq;
2046 struct request *req = mqrq->req;
2047 struct request *prq;
2048 struct mmc_blk_data *md = mq->data;
2049 struct mmc_packed *packed = mqrq->packed;
2050 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002051 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002052 u8 hdr_blocks;
2053 u8 i = 1;
2054
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002055 mqrq->cmd_type = MMC_PACKED_WRITE;
2056 packed->blocks = 0;
2057 packed->idx_failure = MMC_PACKED_NR_IDX;
2058
2059 packed_cmd_hdr = packed->cmd_hdr;
2060 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002061 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2062 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002063 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2064
2065 /*
2066 * Argument for each entry of packed group
2067 */
2068 list_for_each_entry(prq, &packed->list, queuelist) {
2069 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2070 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2071 (prq->cmd_flags & REQ_META) &&
2072 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002073 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002074 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002075 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002076 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2077 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002078 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002079 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002080 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002081 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002082 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002083 packed->blocks += blk_rq_sectors(prq);
2084 i++;
2085 }
2086
2087 memset(brq, 0, sizeof(struct mmc_blk_request));
2088 brq->mrq.cmd = &brq->cmd;
2089 brq->mrq.data = &brq->data;
2090 brq->mrq.sbc = &brq->sbc;
2091 brq->mrq.stop = &brq->stop;
2092
2093 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2094 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2095 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2096
2097 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2098 brq->cmd.arg = blk_rq_pos(req);
2099 if (!mmc_card_blockaddr(card))
2100 brq->cmd.arg <<= 9;
2101 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2102
2103 brq->data.blksz = 512;
2104 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002105 brq->data.flags = MMC_DATA_WRITE;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002106
2107 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2108 brq->stop.arg = 0;
2109 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2110
2111 mmc_set_data_timeout(&brq->data, card);
2112
2113 brq->data.sg = mqrq->sg;
2114 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2115
2116 mqrq->mmc_active.mrq = &brq->mrq;
2117 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2118
2119 mmc_queue_bounce_pre(mqrq);
2120}
2121
Adrian Hunter67716322011-08-29 16:42:15 +03002122static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2123 struct mmc_blk_request *brq, struct request *req,
2124 int ret)
2125{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002126 struct mmc_queue_req *mq_rq;
2127 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2128
Adrian Hunter67716322011-08-29 16:42:15 +03002129 /*
2130 * If this is an SD card and we're writing, we can first
2131 * mark the known good sectors as ok.
2132 *
2133 * If the card is not SD, we can still ok written sectors
2134 * as reported by the controller (which might be less than
2135 * the real number of written sectors, but never more).
2136 */
2137 if (mmc_card_sd(card)) {
2138 u32 blocks;
2139
2140 blocks = mmc_sd_num_wr_blocks(card);
2141 if (blocks != (u32)-1) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302142 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03002143 }
2144 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002145 if (!mmc_packed_cmd(mq_rq->cmd_type))
2146 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002147 }
2148 return ret;
2149}
2150
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002151static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2152{
2153 struct request *prq;
2154 struct mmc_packed *packed = mq_rq->packed;
2155 int idx = packed->idx_failure, i = 0;
2156 int ret = 0;
2157
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002158 while (!list_empty(&packed->list)) {
2159 prq = list_entry_rq(packed->list.next);
2160 if (idx == i) {
2161 /* retry from error index */
2162 packed->nr_entries -= idx;
2163 mq_rq->req = prq;
2164 ret = 1;
2165
2166 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2167 list_del_init(&prq->queuelist);
2168 mmc_blk_clear_packed(mq_rq);
2169 }
2170 return ret;
2171 }
2172 list_del_init(&prq->queuelist);
2173 blk_end_request(prq, 0, blk_rq_bytes(prq));
2174 i++;
2175 }
2176
2177 mmc_blk_clear_packed(mq_rq);
2178 return ret;
2179}
2180
2181static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2182{
2183 struct request *prq;
2184 struct mmc_packed *packed = mq_rq->packed;
2185
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002186 while (!list_empty(&packed->list)) {
2187 prq = list_entry_rq(packed->list.next);
2188 list_del_init(&prq->queuelist);
2189 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2190 }
2191
2192 mmc_blk_clear_packed(mq_rq);
2193}
2194
2195static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2196 struct mmc_queue_req *mq_rq)
2197{
2198 struct request *prq;
2199 struct request_queue *q = mq->queue;
2200 struct mmc_packed *packed = mq_rq->packed;
2201
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002202 while (!list_empty(&packed->list)) {
2203 prq = list_entry_rq(packed->list.prev);
2204 if (prq->queuelist.prev != &packed->list) {
2205 list_del_init(&prq->queuelist);
2206 spin_lock_irq(q->queue_lock);
2207 blk_requeue_request(mq->queue, prq);
2208 spin_unlock_irq(q->queue_lock);
2209 } else {
2210 list_del_init(&prq->queuelist);
2211 }
2212 }
2213
2214 mmc_blk_clear_packed(mq_rq);
2215}
2216
Per Forlinee8a43a2011-07-01 18:55:33 +02002217static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02002218{
2219 struct mmc_blk_data *md = mq->data;
2220 struct mmc_card *card = md->queue.card;
2221 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002222 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002223 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002224 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302225 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02002226 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002227 const u8 packed_nr = 2;
2228 u8 reqs = 0;
Mark Salyzyn92f31302016-01-28 11:12:25 -08002229#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2230 unsigned long waitfor = jiffies;
2231#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02002232
2233 if (!rqc && !mq->mqrq_prev->req)
2234 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002235
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002236 if (rqc)
2237 reqs = mmc_blk_prep_packed_list(mq, rqc);
2238
Per Forlin54d49d72011-07-01 18:55:29 +02002239 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002240 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302241 /*
2242 * When 4KB native sector is enabled, only 8 blocks
2243 * multiple read or write is allowed
2244 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00002245 if (mmc_large_sector(card) &&
2246 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302247 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
2248 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002249 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302250 goto cmd_abort;
2251 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002252
2253 if (reqs >= packed_nr)
2254 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2255 card, mq);
2256 else
2257 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002258 areq = &mq->mqrq_cur->mmc_active;
2259 } else
2260 areq = NULL;
2261 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002262 if (!areq) {
2263 if (status == MMC_BLK_NEW_REQUEST)
2264 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02002265 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002266 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002267
Per Forlinee8a43a2011-07-01 18:55:33 +02002268 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2269 brq = &mq_rq->brq;
2270 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002271 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002272 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002273
Per Forlind78d4a82011-07-01 18:55:30 +02002274 switch (status) {
2275 case MMC_BLK_SUCCESS:
2276 case MMC_BLK_PARTIAL:
2277 /*
2278 * A block was successfully transferred.
2279 */
Adrian Hunter67716322011-08-29 16:42:15 +03002280 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002281
Mark Salyzyn92f31302016-01-28 11:12:25 -08002282 mmc_blk_simulate_delay(mq, rqc, waitfor);
2283
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002284 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2285 ret = mmc_blk_end_packed_req(mq_rq);
2286 break;
2287 } else {
2288 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002289 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002290 }
2291
Adrian Hunter67716322011-08-29 16:42:15 +03002292 /*
2293 * If the blk_end_request function returns non-zero even
2294 * though all data has been transferred and no errors
2295 * were returned by the host controller, it's a bug.
2296 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002297 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302298 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002299 __func__, blk_rq_bytes(req),
2300 brq->data.bytes_xfered);
2301 rqc = NULL;
2302 goto cmd_abort;
2303 }
Per Forlind78d4a82011-07-01 18:55:30 +02002304 break;
2305 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002306 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002307 if (mmc_blk_reset(md, card->host, type))
2308 goto cmd_abort;
2309 if (!ret)
2310 goto start_new_req;
2311 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002312 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002313 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a82011-07-01 18:55:30 +02002314 if (retry++ < 5)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002315 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002316 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002317 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002318 if (!mmc_blk_reset(md, card->host, type))
2319 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002320 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002321 case MMC_BLK_DATA_ERR: {
2322 int err;
2323
2324 err = mmc_blk_reset(md, card->host, type);
2325 if (!err)
2326 break;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002327 if (err == -ENODEV ||
2328 mmc_packed_cmd(mq_rq->cmd_type))
Adrian Hunter67716322011-08-29 16:42:15 +03002329 goto cmd_abort;
2330 /* Fall through */
2331 }
2332 case MMC_BLK_ECC_ERR:
2333 if (brq->data.blocks > 1) {
2334 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002335 pr_warn("%s: retrying using single block read\n",
2336 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002337 disable_multi = 1;
2338 break;
2339 }
Per Forlind78d4a82011-07-01 18:55:30 +02002340 /*
2341 * After an error, we redo I/O one sector at a
2342 * time, so we only reach here after trying to
2343 * read a single sector.
2344 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302345 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002346 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002347 if (!ret)
2348 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002349 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302350 case MMC_BLK_NOMEDIUM:
2351 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002352 default:
2353 pr_err("%s: Unhandled return value (%d)",
2354 req->rq_disk->disk_name, status);
2355 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002356 }
2357
Per Forlinee8a43a2011-07-01 18:55:33 +02002358 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002359 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2360 if (!mq_rq->packed->retries)
2361 goto cmd_abort;
2362 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2363 mmc_start_req(card->host,
2364 &mq_rq->mmc_active, NULL);
2365 } else {
2366
2367 /*
2368 * In case of a incomplete request
2369 * prepare it again and resend.
2370 */
2371 mmc_blk_rw_rq_prep(mq_rq, card,
2372 disable_multi, mq);
2373 mmc_start_req(card->host,
2374 &mq_rq->mmc_active, NULL);
2375 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002376 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 } while (ret);
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return 1;
2381
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002382 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002383 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2384 mmc_blk_abort_packed_req(mq_rq);
2385 } else {
2386 if (mmc_card_removed(card))
2387 req->cmd_flags |= REQ_QUIET;
2388 while (ret)
2389 ret = blk_end_request(req, -EIO,
2390 blk_rq_cur_bytes(req));
2391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Per Forlinee8a43a2011-07-01 18:55:33 +02002393 start_new_req:
2394 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002395 if (mmc_card_removed(card)) {
2396 rqc->cmd_flags |= REQ_QUIET;
2397 blk_end_request_all(rqc, -EIO);
2398 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002399 /*
2400 * If current request is packed, it needs to put back.
2401 */
2402 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2403 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2404
Seungwon Jeon7a819022013-01-22 19:48:07 +09002405 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2406 mmc_start_req(card->host,
2407 &mq->mqrq_cur->mmc_active, NULL);
2408 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002409 }
2410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 return 0;
2412}
2413
Linus Walleij29eb7bd2016-09-20 11:34:38 +02002414int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002415{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002416 int ret;
2417 struct mmc_blk_data *md = mq->data;
2418 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002419 struct mmc_host *host = card->host;
2420 unsigned long flags;
Adrian Hunter869c5542016-08-25 14:11:43 -06002421 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002422
Per Forlinee8a43a2011-07-01 18:55:33 +02002423 if (req && !mq->mqrq_prev->req)
2424 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002425 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002426
Andrei Warkentin371a6892011-04-11 18:10:25 -05002427 ret = mmc_blk_part_switch(card, md);
2428 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002429 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302430 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002431 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002432 ret = 0;
2433 goto out;
2434 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002435
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002436 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05002437 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002438 /* complete ongoing async transfer before issuing discard */
2439 if (card->host->areq)
2440 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02002441 ret = mmc_blk_issue_discard_rq(mq, req);
2442 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
2443 /* complete ongoing async transfer before issuing secure erase*/
2444 if (card->host->areq)
2445 mmc_blk_issue_rw_rq(mq, NULL);
2446 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05002447 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002448 /* complete ongoing async transfer before issuing flush */
2449 if (card->host->areq)
2450 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002451 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002452 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002453 if (!req && host->areq) {
2454 spin_lock_irqsave(&host->context_info.lock, flags);
2455 host->context_info.is_waiting_last_req = true;
2456 spin_unlock_irqrestore(&host->context_info.lock, flags);
2457 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002458 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002459 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002460
Andrei Warkentin371a6892011-04-11 18:10:25 -05002461out:
Adrian Hunter869c5542016-08-25 14:11:43 -06002462 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002463 /*
2464 * Release host when there are no more requests
2465 * and after special request(discard, flush) is done.
2466 * In case sepecial request, there is no reentry to
2467 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2468 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002469 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002470 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002471}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Russell Kinga6f6c962006-01-03 22:38:44 +00002473static inline int mmc_blk_readonly(struct mmc_card *card)
2474{
2475 return mmc_card_readonly(card) ||
2476 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2477}
2478
Andrei Warkentin371a6892011-04-11 18:10:25 -05002479static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2480 struct device *parent,
2481 sector_t size,
2482 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002483 const char *subname,
2484 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
2486 struct mmc_blk_data *md;
2487 int devidx, ret;
2488
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002489again:
2490 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
2491 return ERR_PTR(-ENOMEM);
2492
2493 spin_lock(&mmc_blk_lock);
2494 ret = ida_get_new(&mmc_blk_ida, &devidx);
2495 spin_unlock(&mmc_blk_lock);
2496
2497 if (ret == -EAGAIN)
2498 goto again;
2499 else if (ret)
2500 return ERR_PTR(ret);
2501
2502 if (devidx >= max_devices) {
2503 ret = -ENOSPC;
2504 goto out;
2505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002507 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002508 if (!md) {
2509 ret = -ENOMEM;
2510 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002512
Johan Rudholmadd710e2011-12-02 08:51:06 +01002513 md->area_type = area_type;
2514
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002515 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002516 * Set the read-only status based on the supported commands
2517 * and the write protect switch.
2518 */
2519 md->read_only = mmc_blk_readonly(card);
2520
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002521 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002522 if (md->disk == NULL) {
2523 ret = -ENOMEM;
2524 goto err_kfree;
2525 }
2526
2527 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002528 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002529 md->usage = 1;
2530
Adrian Hunterd09408a2011-06-23 13:40:28 +03002531 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002532 if (ret)
2533 goto err_putdisk;
2534
Russell Kinga6f6c962006-01-03 22:38:44 +00002535 md->queue.data = md;
2536
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002537 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002538 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002539 md->disk->fops = &mmc_bdops;
2540 md->disk->private_data = md;
2541 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002542 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002543 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002544 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002545 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002546 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002547
2548 /*
2549 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2550 *
2551 * - be set for removable media with permanent block devices
2552 * - be unset for removable block devices with permanent media
2553 *
2554 * Since MMC block devices clearly fall under the second
2555 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2556 * should use the block device creation/destruction hotplug
2557 * messages to tell when the card is present.
2558 */
2559
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002560 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02002561 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002562
Saugata Dasa5075eb2012-05-17 16:32:21 +05302563 if (mmc_card_mmc(card))
2564 blk_queue_logical_block_size(md->queue.queue,
2565 card->ext_csd.data_sector_size);
2566 else
2567 blk_queue_logical_block_size(md->queue.queue, 512);
2568
Andrei Warkentin371a6892011-04-11 18:10:25 -05002569 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002570
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002571 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02002572 if ((mmc_card_mmc(card) &&
2573 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002574 (mmc_card_sd(card) &&
2575 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2576 md->flags |= MMC_BLK_CMD23;
2577 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002578
2579 if (mmc_card_mmc(card) &&
2580 md->flags & MMC_BLK_CMD23 &&
2581 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2582 card->ext_csd.rel_sectors)) {
2583 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06002584 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002585 }
2586
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002587 if (mmc_card_mmc(card) &&
2588 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2589 (md->flags & MMC_BLK_CMD23) &&
2590 card->ext_csd.packed_event_en) {
2591 if (!mmc_packed_init(&md->queue, card))
2592 md->flags |= MMC_BLK_PACKED_CMD;
2593 }
2594
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002596
2597 err_putdisk:
2598 put_disk(md->disk);
2599 err_kfree:
2600 kfree(md);
2601 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002602 spin_lock(&mmc_blk_lock);
2603 ida_remove(&mmc_blk_ida, devidx);
2604 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00002605 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607
Andrei Warkentin371a6892011-04-11 18:10:25 -05002608static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2609{
2610 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002611
2612 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2613 /*
2614 * The EXT_CSD sector count is in number or 512 byte
2615 * sectors.
2616 */
2617 size = card->ext_csd.sectors;
2618 } else {
2619 /*
2620 * The CSD capacity field is in units of read_blkbits.
2621 * set_capacity takes units of 512 bytes.
2622 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002623 size = (typeof(sector_t))card->csd.capacity
2624 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002625 }
2626
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002627 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002628 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002629}
2630
2631static int mmc_blk_alloc_part(struct mmc_card *card,
2632 struct mmc_blk_data *md,
2633 unsigned int part_type,
2634 sector_t size,
2635 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002636 const char *subname,
2637 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002638{
2639 char cap_str[10];
2640 struct mmc_blk_data *part_md;
2641
2642 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002643 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002644 if (IS_ERR(part_md))
2645 return PTR_ERR(part_md);
2646 part_md->part_type = part_type;
2647 list_add(&part_md->part, &md->part);
2648
James Bottomleyb9f28d82015-03-05 18:47:01 -08002649 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002650 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302651 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002652 part_md->disk->disk_name, mmc_card_id(card),
2653 mmc_card_name(card), part_md->part_type, cap_str);
2654 return 0;
2655}
2656
Namjae Jeone0c368d2011-10-06 23:41:38 +09002657/* MMC Physical partitions consist of two boot partitions and
2658 * up to four general purpose partitions.
2659 * For each partition enabled in EXT_CSD a block device will be allocatedi
2660 * to provide access to the partition.
2661 */
2662
Andrei Warkentin371a6892011-04-11 18:10:25 -05002663static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2664{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002665 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002666
2667 if (!mmc_card_mmc(card))
2668 return 0;
2669
Namjae Jeone0c368d2011-10-06 23:41:38 +09002670 for (idx = 0; idx < card->nr_parts; idx++) {
2671 if (card->part[idx].size) {
2672 ret = mmc_blk_alloc_part(card, md,
2673 card->part[idx].part_cfg,
2674 card->part[idx].size >> 9,
2675 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002676 card->part[idx].name,
2677 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002678 if (ret)
2679 return ret;
2680 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002681 }
2682
2683 return ret;
2684}
2685
Andrei Warkentin371a6892011-04-11 18:10:25 -05002686static void mmc_blk_remove_req(struct mmc_blk_data *md)
2687{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002688 struct mmc_card *card;
2689
Andrei Warkentin371a6892011-04-11 18:10:25 -05002690 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002691 /*
2692 * Flush remaining requests and free queues. It
2693 * is freeing the queue that stops new requests
2694 * from being accepted.
2695 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002696 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002697 mmc_cleanup_queue(&md->queue);
2698 if (md->flags & MMC_BLK_PACKED_CMD)
2699 mmc_packed_clean(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002700 if (md->disk->flags & GENHD_FL_UP) {
2701 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002702 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2703 card->ext_csd.boot_ro_lockable)
2704 device_remove_file(disk_to_dev(md->disk),
2705 &md->power_ro_lock);
Mark Salyzyn92f31302016-01-28 11:12:25 -08002706#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2707 device_remove_file(disk_to_dev(md->disk),
2708 &dev_attr_max_write_speed);
2709 device_remove_file(disk_to_dev(md->disk),
2710 &dev_attr_max_read_speed);
2711 device_remove_file(disk_to_dev(md->disk),
2712 &dev_attr_cache_size);
2713#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002714
Andrei Warkentin371a6892011-04-11 18:10:25 -05002715 del_gendisk(md->disk);
2716 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002717 mmc_blk_put(md);
2718 }
2719}
2720
2721static void mmc_blk_remove_parts(struct mmc_card *card,
2722 struct mmc_blk_data *md)
2723{
2724 struct list_head *pos, *q;
2725 struct mmc_blk_data *part_md;
2726
2727 list_for_each_safe(pos, q, &md->part) {
2728 part_md = list_entry(pos, struct mmc_blk_data, part);
2729 list_del(pos);
2730 mmc_blk_remove_req(part_md);
2731 }
2732}
2733
2734static int mmc_add_disk(struct mmc_blk_data *md)
2735{
2736 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002737 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002738
Dan Williams307d8e62016-06-20 10:40:44 -07002739 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002740 md->force_ro.show = force_ro_show;
2741 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302742 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002743 md->force_ro.attr.name = "force_ro";
2744 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2745 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2746 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002747 goto force_ro_fail;
Mark Salyzyn92f31302016-01-28 11:12:25 -08002748#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2749 atomic_set(&md->queue.max_write_speed, max_write_speed);
2750 ret = device_create_file(disk_to_dev(md->disk),
2751 &dev_attr_max_write_speed);
2752 if (ret)
2753 goto max_write_speed_fail;
2754 atomic_set(&md->queue.max_read_speed, max_read_speed);
2755 ret = device_create_file(disk_to_dev(md->disk),
2756 &dev_attr_max_read_speed);
2757 if (ret)
2758 goto max_read_speed_fail;
2759 atomic_set(&md->queue.cache_size, cache_size);
2760 atomic_long_set(&md->queue.cache_used, 0);
2761 md->queue.cache_jiffies = jiffies;
2762 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
2763 if (ret)
2764 goto cache_size_fail;
2765#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01002766
2767 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2768 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002769 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002770
2771 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2772 mode = S_IRUGO;
2773 else
2774 mode = S_IRUGO | S_IWUSR;
2775
2776 md->power_ro_lock.show = power_ro_lock_show;
2777 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002778 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002779 md->power_ro_lock.attr.mode = mode;
2780 md->power_ro_lock.attr.name =
2781 "ro_lock_until_next_power_on";
2782 ret = device_create_file(disk_to_dev(md->disk),
2783 &md->power_ro_lock);
2784 if (ret)
2785 goto power_ro_lock_fail;
2786 }
2787 return ret;
2788
2789power_ro_lock_fail:
Mark Salyzyn92f31302016-01-28 11:12:25 -08002790#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2791 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
2792cache_size_fail:
2793 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
2794max_read_speed_fail:
2795 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
2796max_write_speed_fail:
2797#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01002798 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2799force_ro_fail:
2800 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002801
2802 return ret;
2803}
2804
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002805static const struct mmc_fixup blk_fixups[] =
2806{
Chris Ballc59d4472011-11-11 22:01:43 -05002807 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2808 MMC_QUIRK_INAND_CMD38),
2809 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2810 MMC_QUIRK_INAND_CMD38),
2811 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2812 MMC_QUIRK_INAND_CMD38),
2813 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2814 MMC_QUIRK_INAND_CMD38),
2815 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2816 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002817
2818 /*
2819 * Some MMC cards experience performance degradation with CMD23
2820 * instead of CMD12-bounded multiblock transfers. For now we'll
2821 * black list what's bad...
2822 * - Certain Toshiba cards.
2823 *
2824 * N.B. This doesn't affect SD cards.
2825 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08002826 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2827 MMC_QUIRK_BLK_NO_CMD23),
2828 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2829 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002830 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002831 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002832 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002833 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002834 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002835 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002836
2837 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03002838 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002839 */
Chris Ballc59d4472011-11-11 22:01:43 -05002840 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002841 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03002842 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2843 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002844
Ian Chen3550ccd2012-08-29 15:05:36 +09002845 /*
2846 * On these Samsung MoviNAND parts, performing secure erase or
2847 * secure trim can result in unrecoverable corruption due to a
2848 * firmware bug.
2849 */
2850 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2851 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2852 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2853 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2854 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2855 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2856 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2857 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2858 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2859 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2860 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2861 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2862 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2863 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2864 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2865 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2866
Shawn Linb5b4ff02015-08-12 13:08:32 +08002867 /*
2868 * On Some Kingston eMMCs, performing trim can result in
2869 * unrecoverable data conrruption occasionally due to a firmware bug.
2870 */
2871 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2872 MMC_QUIRK_TRIM_BROKEN),
2873 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2874 MMC_QUIRK_TRIM_BROKEN),
2875
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002876 END_FIXUP
2877};
2878
Ulf Hansson96541ba2015-04-14 13:06:12 +02002879static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002881 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002882 char cap_str[10];
2883
Pierre Ossman912490d2005-05-21 10:27:02 +01002884 /*
2885 * Check that the card supports the command class(es) we need.
2886 */
2887 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return -ENODEV;
2889
Lukas Czerner5204d002014-06-18 13:18:07 +02002890 mmc_fixup_device(card, blk_fixups);
2891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 md = mmc_blk_alloc(card);
2893 if (IS_ERR(md))
2894 return PTR_ERR(md);
2895
James Bottomleyb9f28d82015-03-05 18:47:01 -08002896 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002897 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302898 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002900 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Andrei Warkentin371a6892011-04-11 18:10:25 -05002902 if (mmc_blk_alloc_parts(card, md))
2903 goto out;
2904
Ulf Hansson96541ba2015-04-14 13:06:12 +02002905 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002906
Andrei Warkentin371a6892011-04-11 18:10:25 -05002907 if (mmc_add_disk(md))
2908 goto out;
2909
2910 list_for_each_entry(part_md, &md->part, part) {
2911 if (mmc_add_disk(part_md))
2912 goto out;
2913 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002914
2915 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2916 pm_runtime_use_autosuspend(&card->dev);
2917
2918 /*
2919 * Don't enable runtime PM for SD-combo cards here. Leave that
2920 * decision to be taken during the SDIO init sequence instead.
2921 */
2922 if (card->type != MMC_TYPE_SD_COMBO) {
2923 pm_runtime_set_active(&card->dev);
2924 pm_runtime_enable(&card->dev);
2925 }
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return 0;
2928
2929 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002930 mmc_blk_remove_parts(card, md);
2931 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002932 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933}
2934
Ulf Hansson96541ba2015-04-14 13:06:12 +02002935static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002937 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Andrei Warkentin371a6892011-04-11 18:10:25 -05002939 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002940 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002941 mmc_claim_host(card->host);
2942 mmc_blk_part_switch(card, md);
2943 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002944 if (card->type != MMC_TYPE_SD_COMBO)
2945 pm_runtime_disable(&card->dev);
2946 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002947 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002948 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949}
2950
Ulf Hansson96541ba2015-04-14 13:06:12 +02002951static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002953 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002954 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956 if (md) {
2957 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002958 list_for_each_entry(part_md, &md->part, part) {
2959 mmc_queue_suspend(&part_md->queue);
2960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 }
2962 return 0;
2963}
2964
Ulf Hansson96541ba2015-04-14 13:06:12 +02002965static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002966{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002967 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002968}
2969
Ulf Hansson0967edc2014-10-06 11:29:42 +02002970#ifdef CONFIG_PM_SLEEP
2971static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002972{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002973 struct mmc_card *card = mmc_dev_to_card(dev);
2974
2975 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002976}
2977
Ulf Hansson0967edc2014-10-06 11:29:42 +02002978static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002980 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002981 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002984 /*
2985 * Resume involves the card going into idle state,
2986 * so current partition is always the main one.
2987 */
2988 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002990 list_for_each_entry(part_md, &md->part, part) {
2991 mmc_queue_resume(&part_md->queue);
2992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 }
2994 return 0;
2995}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996#endif
2997
Ulf Hansson0967edc2014-10-06 11:29:42 +02002998static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2999
Ulf Hansson96541ba2015-04-14 13:06:12 +02003000static struct mmc_driver mmc_driver = {
3001 .drv = {
3002 .name = "mmcblk",
3003 .pm = &mmc_blk_pm_ops,
3004 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 .probe = mmc_blk_probe,
3006 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02003007 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008};
3009
3010static int __init mmc_blk_init(void)
3011{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003012 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003014 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3015 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3016
Ben Hutchingsa26eba62014-11-06 03:35:09 +00003017 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003018
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003019 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3020 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003023 res = mmc_register_driver(&mmc_driver);
3024 if (res)
3025 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003027 return 0;
3028 out2:
3029 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 out:
3031 return res;
3032}
3033
3034static void __exit mmc_blk_exit(void)
3035{
3036 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003037 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
3039
3040module_init(mmc_blk_init);
3041module_exit(mmc_blk_exit);
3042
3043MODULE_LICENSE("GPL");
3044MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3045