blob: 3717564e1428617a20066562a68c2cd08403df8a [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
Subhash Jadavani2fbab612014-12-04 15:16:17 +020063#define MMC_BLK_TIMEOUT_MS (30 * 1000) /* 30 sec 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
Lee Susman841fd132013-04-23 17:59:26 +030071#define PACKED_TRIGGER_MAX_ELEMENTS 5000
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090072
Tatyana Brokhman08238ce2012-10-07 10:33:13 +020073#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
74 do { \
75 if (stats->enabled) \
76 stats->pack_stop_reason[reason]++; \
77 } while (0)
78
Lee Susman841fd132013-04-23 17:59:26 +030079#define PCKD_TRGR_INIT_MEAN_POTEN 17
80#define PCKD_TRGR_POTEN_LOWER_BOUND 5
81#define PCKD_TRGR_URGENT_PENALTY 2
82#define PCKD_TRGR_LOWER_BOUND 5
83#define PCKD_TRGR_PRECISION_MULTIPLIER 100
84
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020085static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040086
87/*
88 * The defaults come from config options but can be overriden by module
89 * or bootarg options.
90 */
91static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
92
93/*
94 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000095 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020096 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040097 */
98static int max_devices;
99
Ben Hutchingsa26eba62014-11-06 03:35:09 +0000100#define MAX_DEVICES 256
101
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200102static DEFINE_IDA(mmc_blk_ida);
103static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
106 * There is one mmc_blk_data per slot.
107 */
108struct mmc_blk_data {
109 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -0700110 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 struct gendisk *disk;
112 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500113 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500115 unsigned int flags;
116#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
117#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900118#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000121 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500122 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300123 unsigned int reset_done;
124#define MMC_BLK_READ BIT(0)
125#define MMC_BLK_WRITE BIT(1)
126#define MMC_BLK_DISCARD BIT(2)
127#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500128
129 /*
130 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200131 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500132 * track of the current selected device partition.
133 */
134 unsigned int part_curr;
135 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100136 struct device_attribute power_ro_lock;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200137 struct device_attribute num_wr_reqs_to_start_packing;
Maya Erez5a8dae12014-12-04 15:13:59 +0200138 struct device_attribute no_pack_for_random;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100139 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
Arjan van de Vena621aae2006-01-12 18:43:35 +0000142static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900144enum {
145 MMC_PACKED_NR_IDX = -1,
146 MMC_PACKED_NR_ZERO,
147 MMC_PACKED_NR_SINGLE,
148};
149
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400150module_param(perdev_minors, int, 0444);
151MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
152
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200153static inline int mmc_blk_part_switch(struct mmc_card *card,
154 struct mmc_blk_data *md);
155static int get_card_status(struct mmc_card *card, u32 *status, int retries);
156
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900157static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
158{
159 struct mmc_packed *packed = mqrq->packed;
160
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900161 mqrq->cmd_type = MMC_PACKED_NONE;
162 packed->nr_entries = MMC_PACKED_NR_ZERO;
163 packed->idx_failure = MMC_PACKED_NR_IDX;
164 packed->retries = 0;
165 packed->blocks = 0;
166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
169{
170 struct mmc_blk_data *md;
171
Arjan van de Vena621aae2006-01-12 18:43:35 +0000172 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 md = disk->private_data;
174 if (md && md->usage == 0)
175 md = NULL;
176 if (md)
177 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000178 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 return md;
181}
182
Andrei Warkentin371a6892011-04-11 18:10:25 -0500183static inline int mmc_get_devidx(struct gendisk *disk)
184{
Colin Cross382c55f2015-10-22 10:00:41 -0700185 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500186 return devidx;
187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static void mmc_blk_put(struct mmc_blk_data *md)
190{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000191 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 md->usage--;
193 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500194 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800195 blk_cleanup_queue(md->queue.queue);
196
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200197 spin_lock(&mmc_blk_lock);
198 ida_remove(&mmc_blk_ida, devidx);
199 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 kfree(md);
203 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000204 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Johan Rudholmadd710e2011-12-02 08:51:06 +0100207static ssize_t power_ro_lock_show(struct device *dev,
208 struct device_attribute *attr, char *buf)
209{
210 int ret;
211 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
212 struct mmc_card *card = md->queue.card;
213 int locked = 0;
214
215 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
216 locked = 2;
217 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
218 locked = 1;
219
220 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
221
Tomas Winkler9098f842015-07-16 15:50:45 +0200222 mmc_blk_put(md);
223
Johan Rudholmadd710e2011-12-02 08:51:06 +0100224 return ret;
225}
226
227static ssize_t power_ro_lock_store(struct device *dev,
228 struct device_attribute *attr, const char *buf, size_t count)
229{
230 int ret;
231 struct mmc_blk_data *md, *part_md;
232 struct mmc_card *card;
233 unsigned long set;
234
235 if (kstrtoul(buf, 0, &set))
236 return -EINVAL;
237
238 if (set != 1)
239 return count;
240
241 md = mmc_blk_get(dev_to_disk(dev));
242 card = md->queue.card;
243
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200244 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100245
246 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
247 card->ext_csd.boot_ro_lock |
248 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
249 card->ext_csd.part_time);
250 if (ret)
251 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
252 else
253 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
254
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200255 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100256
257 if (!ret) {
258 pr_info("%s: Locking boot partition ro until next power on\n",
259 md->disk->disk_name);
260 set_disk_ro(md->disk, 1);
261
262 list_for_each_entry(part_md, &md->part, part)
263 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
264 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
265 set_disk_ro(part_md->disk, 1);
266 }
267 }
268
269 mmc_blk_put(md);
270 return count;
271}
272
Andrei Warkentin371a6892011-04-11 18:10:25 -0500273static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
274 char *buf)
275{
276 int ret;
277 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
278
Baruch Siach0031a982014-09-22 10:12:51 +0300279 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500280 get_disk_ro(dev_to_disk(dev)) ^
281 md->read_only);
282 mmc_blk_put(md);
283 return ret;
284}
285
286static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
287 const char *buf, size_t count)
288{
289 int ret;
290 char *end;
291 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
292 unsigned long set = simple_strtoul(buf, &end, 0);
293 if (end == buf) {
294 ret = -EINVAL;
295 goto out;
296 }
297
298 set_disk_ro(dev_to_disk(dev), set || md->read_only);
299 ret = count;
300out:
301 mmc_blk_put(md);
302 return ret;
303}
304
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200305static ssize_t
Maya Erez5a8dae12014-12-04 15:13:59 +0200306no_pack_for_random_show(struct device *dev,
307 struct device_attribute *attr, char *buf)
308{
309 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
310 int ret;
311
312 ret = snprintf(buf, PAGE_SIZE, "%d\n", md->queue.no_pack_for_random);
313
314 mmc_blk_put(md);
315 return ret;
316}
317
318static ssize_t
319no_pack_for_random_store(struct device *dev,
320 struct device_attribute *attr,
321 const char *buf, size_t count)
322{
323 int value;
324 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
325 struct mmc_card *card = md->queue.card;
326 int ret = count;
327
328 if (!card) {
329 ret = -EINVAL;
330 goto exit;
331 }
332
333 sscanf(buf, "%d", &value);
334
335 if (value < 0) {
336 pr_err("%s: value %d is not valid. old value remains = %d",
337 mmc_hostname(card->host), value,
338 md->queue.no_pack_for_random);
339 ret = -EINVAL;
340 goto exit;
341 }
342
343 md->queue.no_pack_for_random = (value > 0) ? true : false;
344
345 pr_debug("%s: no_pack_for_random: new value = %d",
346 mmc_hostname(card->host),
347 md->queue.no_pack_for_random);
348
349exit:
350 mmc_blk_put(md);
351 return ret;
352}
353
354static ssize_t
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200355num_wr_reqs_to_start_packing_show(struct device *dev,
356 struct device_attribute *attr, char *buf)
357{
358 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
359 int num_wr_reqs_to_start_packing;
360 int ret;
361
362 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
363
364 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
365
366 mmc_blk_put(md);
367 return ret;
368}
369
370static ssize_t
371num_wr_reqs_to_start_packing_store(struct device *dev,
372 struct device_attribute *attr,
373 const char *buf, size_t count)
374{
375 int value;
376 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Yaniv Gardi42399822014-12-04 00:26:23 +0200377 struct mmc_card *card = md->queue.card;
378 int ret = count;
379
380 if (!card) {
381 ret = -EINVAL;
382 goto exit;
383 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200384
385 sscanf(buf, "%d", &value);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200386
Yaniv Gardi42399822014-12-04 00:26:23 +0200387 if (value >= 0) {
388 md->queue.num_wr_reqs_to_start_packing =
389 min_t(int, value, (int)card->ext_csd.max_packed_writes);
390
391 pr_debug("%s: trigger to pack: new value = %d",
392 mmc_hostname(card->host),
393 md->queue.num_wr_reqs_to_start_packing);
394 } else {
395 pr_err("%s: value %d is not valid. old value remains = %d",
396 mmc_hostname(card->host), value,
397 md->queue.num_wr_reqs_to_start_packing);
398 ret = -EINVAL;
399 }
400
401exit:
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200402 mmc_blk_put(md);
Yaniv Gardi42399822014-12-04 00:26:23 +0200403 return ret;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200404}
405
Mark Salyzyn6904e432016-01-28 11:12:25 -0800406#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
407
408static int max_read_speed, max_write_speed, cache_size = 4;
409
410module_param(max_read_speed, int, S_IRUSR | S_IRGRP);
411MODULE_PARM_DESC(max_read_speed, "maximum KB/s read speed 0=off");
412module_param(max_write_speed, int, S_IRUSR | S_IRGRP);
413MODULE_PARM_DESC(max_write_speed, "maximum KB/s write speed 0=off");
414module_param(cache_size, int, S_IRUSR | S_IRGRP);
415MODULE_PARM_DESC(cache_size, "MB high speed memory or SLC cache");
416
417/*
418 * helper macros and expectations:
419 * size - unsigned long number of bytes
420 * jiffies - unsigned long HZ timestamp difference
421 * speed - unsigned KB/s transfer rate
422 */
423#define size_and_speed_to_jiffies(size, speed) \
424 ((size) * HZ / (speed) / 1024UL)
425#define jiffies_and_speed_to_size(jiffies, speed) \
426 (((speed) * (jiffies) * 1024UL) / HZ)
427#define jiffies_and_size_to_speed(jiffies, size) \
428 ((size) * HZ / (jiffies) / 1024UL)
429
430/* Limits to report warning */
431/* jiffies_and_size_to_speed(10*HZ, queue_max_hw_sectors(q) * 512UL) ~ 25 */
432#define MIN_SPEED(q) 250 /* 10 times faster than a floppy disk */
433#define MAX_SPEED(q) jiffies_and_size_to_speed(1, queue_max_sectors(q) * 512UL)
434
435#define speed_valid(speed) ((speed) > 0)
436
437static const char off[] = "off\n";
438
439static int max_speed_show(int speed, char *buf)
440{
441 if (speed)
442 return scnprintf(buf, PAGE_SIZE, "%uKB/s\n", speed);
443 else
444 return scnprintf(buf, PAGE_SIZE, off);
445}
446
447static int max_speed_store(const char *buf, struct request_queue *q)
448{
449 unsigned int limit, set = 0;
450
451 if (!strncasecmp(off, buf, sizeof(off) - 2))
452 return set;
453 if (kstrtouint(buf, 0, &set) || (set > INT_MAX))
454 return -EINVAL;
455 if (set == 0)
456 return set;
457 limit = MAX_SPEED(q);
458 if (set > limit)
459 pr_warn("max speed %u ineffective above %u\n", set, limit);
460 limit = MIN_SPEED(q);
461 if (set < limit)
462 pr_warn("max speed %u painful below %u\n", set, limit);
463 return set;
464}
465
466static ssize_t max_write_speed_show(struct device *dev,
467 struct device_attribute *attr, char *buf)
468{
469 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
470 int ret = max_speed_show(atomic_read(&md->queue.max_write_speed), buf);
471
472 mmc_blk_put(md);
473 return ret;
474}
475
476static ssize_t max_write_speed_store(struct device *dev,
477 struct device_attribute *attr,
478 const char *buf, size_t count)
479{
480 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
481 int set = max_speed_store(buf, md->queue.queue);
482
483 if (set < 0) {
484 mmc_blk_put(md);
485 return set;
486 }
487
488 atomic_set(&md->queue.max_write_speed, set);
489 mmc_blk_put(md);
490 return count;
491}
492
493static const DEVICE_ATTR(max_write_speed, S_IRUGO | S_IWUSR,
494 max_write_speed_show, max_write_speed_store);
495
496static ssize_t max_read_speed_show(struct device *dev,
497 struct device_attribute *attr, char *buf)
498{
499 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
500 int ret = max_speed_show(atomic_read(&md->queue.max_read_speed), buf);
501
502 mmc_blk_put(md);
503 return ret;
504}
505
506static ssize_t max_read_speed_store(struct device *dev,
507 struct device_attribute *attr,
508 const char *buf, size_t count)
509{
510 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
511 int set = max_speed_store(buf, md->queue.queue);
512
513 if (set < 0) {
514 mmc_blk_put(md);
515 return set;
516 }
517
518 atomic_set(&md->queue.max_read_speed, set);
519 mmc_blk_put(md);
520 return count;
521}
522
523static const DEVICE_ATTR(max_read_speed, S_IRUGO | S_IWUSR,
524 max_read_speed_show, max_read_speed_store);
525
526static ssize_t cache_size_show(struct device *dev,
527 struct device_attribute *attr, char *buf)
528{
529 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
530 struct mmc_queue *mq = &md->queue;
531 int cache_size = atomic_read(&mq->cache_size);
532 int ret;
533
534 if (!cache_size)
535 ret = scnprintf(buf, PAGE_SIZE, off);
536 else {
537 int speed = atomic_read(&mq->max_write_speed);
538
539 if (!speed_valid(speed))
540 ret = scnprintf(buf, PAGE_SIZE, "%uMB\n", cache_size);
541 else { /* We accept race between cache_jiffies and cache_used */
542 unsigned long size = jiffies_and_speed_to_size(
543 jiffies - mq->cache_jiffies, speed);
544 long used = atomic_long_read(&mq->cache_used);
545
546 if (size >= used)
547 size = 0;
548 else
549 size = (used - size) * 100 / cache_size
550 / 1024UL / 1024UL;
551
552 ret = scnprintf(buf, PAGE_SIZE, "%uMB %lu%% used\n",
553 cache_size, size);
554 }
555 }
556
557 mmc_blk_put(md);
558 return ret;
559}
560
561static ssize_t cache_size_store(struct device *dev,
562 struct device_attribute *attr,
563 const char *buf, size_t count)
564{
565 struct mmc_blk_data *md;
566 unsigned int set = 0;
567
568 if (strncasecmp(off, buf, sizeof(off) - 2)
569 && (kstrtouint(buf, 0, &set) || (set > INT_MAX)))
570 return -EINVAL;
571
572 md = mmc_blk_get(dev_to_disk(dev));
573 atomic_set(&md->queue.cache_size, set);
574 mmc_blk_put(md);
575 return count;
576}
577
578static const DEVICE_ATTR(cache_size, S_IRUGO | S_IWUSR,
579 cache_size_show, cache_size_store);
580
581/* correct for write-back */
582static long mmc_blk_cache_used(struct mmc_queue *mq, unsigned long waitfor)
583{
584 long used = 0;
585 int speed = atomic_read(&mq->max_write_speed);
586
587 if (speed_valid(speed)) {
588 unsigned long size = jiffies_and_speed_to_size(
589 waitfor - mq->cache_jiffies, speed);
590 used = atomic_long_read(&mq->cache_used);
591
592 if (size >= used)
593 used = 0;
594 else
595 used -= size;
596 }
597
598 atomic_long_set(&mq->cache_used, used);
599 mq->cache_jiffies = waitfor;
600
601 return used;
602}
603
604static void mmc_blk_simulate_delay(
605 struct mmc_queue *mq,
606 struct request *req,
607 unsigned long waitfor)
608{
609 int max_speed;
610
611 if (!req)
612 return;
613
614 max_speed = (rq_data_dir(req) == READ)
615 ? atomic_read(&mq->max_read_speed)
616 : atomic_read(&mq->max_write_speed);
617 if (speed_valid(max_speed)) {
618 unsigned long bytes = blk_rq_bytes(req);
619
620 if (rq_data_dir(req) != READ) {
621 int cache_size = atomic_read(&mq->cache_size);
622
623 if (cache_size) {
624 unsigned long size = cache_size * 1024L * 1024L;
625 long used = mmc_blk_cache_used(mq, waitfor);
626
627 used += bytes;
628 atomic_long_set(&mq->cache_used, used);
629 bytes = 0;
630 if (used > size)
631 bytes = used - size;
632 }
633 }
634 waitfor += size_and_speed_to_jiffies(bytes, max_speed);
635 if (time_is_after_jiffies(waitfor)) {
636 long msecs = jiffies_to_msecs(waitfor - jiffies);
637
638 if (likely(msecs > 0))
639 msleep(msecs);
640 }
641 }
642}
643
644#else
645
646#define mmc_blk_simulate_delay(mq, req, waitfor)
647
648#endif
649
Al Viroa5a15612008-03-02 10:33:30 -0500650static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Al Viroa5a15612008-03-02 10:33:30 -0500652 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 int ret = -ENXIO;
654
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200655 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (md) {
657 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500658 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700660
Al Viroa5a15612008-03-02 10:33:30 -0500661 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700662 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700663 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200666 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 return ret;
669}
670
Al Virodb2a1442013-05-05 21:52:57 -0400671static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Al Viroa5a15612008-03-02 10:33:30 -0500673 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200675 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200677 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
680static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800681mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800683 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
684 geo->heads = 4;
685 geo->sectors = 16;
686 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
John Calixtocb87ea22011-04-26 18:56:29 -0400689struct mmc_blk_ioc_data {
690 struct mmc_ioc_cmd ic;
691 unsigned char *buf;
692 u64 buf_bytes;
693};
694
695static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
696 struct mmc_ioc_cmd __user *user)
697{
698 struct mmc_blk_ioc_data *idata;
699 int err;
700
yalin wang1ff89502015-11-12 19:27:11 +0800701 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400702 if (!idata) {
703 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400704 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400705 }
706
707 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
708 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400709 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400710 }
711
712 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
713 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
714 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400715 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400716 }
717
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300718 if (!idata->buf_bytes) {
719 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100720 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300721 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100722
yalin wang1ff89502015-11-12 19:27:11 +0800723 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400724 if (!idata->buf) {
725 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400726 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400727 }
728
729 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
730 idata->ic.data_ptr, idata->buf_bytes)) {
731 err = -EFAULT;
732 goto copy_err;
733 }
734
735 return idata;
736
737copy_err:
738 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400739idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400740 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400741out:
John Calixtocb87ea22011-04-26 18:56:29 -0400742 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400743}
744
Jon Huntera5f57742015-09-22 10:27:53 +0100745static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
746 struct mmc_blk_ioc_data *idata)
747{
748 struct mmc_ioc_cmd *ic = &idata->ic;
749
750 if (copy_to_user(&(ic_ptr->response), ic->response,
751 sizeof(ic->response)))
752 return -EFAULT;
753
754 if (!idata->ic.write_flag) {
755 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
756 idata->buf, idata->buf_bytes))
757 return -EFAULT;
758 }
759
760 return 0;
761}
762
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200763static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
764 u32 retries_max)
765{
766 int err;
767 u32 retry_count = 0;
768
769 if (!status || !retries_max)
770 return -EINVAL;
771
772 do {
773 err = get_card_status(card, status, 5);
774 if (err)
775 break;
776
777 if (!R1_STATUS(*status) &&
778 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
779 break; /* RPMB programming operation complete */
780
781 /*
782 * Rechedule to give the MMC device a chance to continue
783 * processing the previous command without being polled too
784 * frequently.
785 */
786 usleep_range(1000, 5000);
787 } while (++retry_count < retries_max);
788
789 if (retry_count == retries_max)
790 err = -EPERM;
791
792 return err;
793}
794
Maya Erez775a9362013-04-18 15:41:55 +0300795static int ioctl_do_sanitize(struct mmc_card *card)
796{
797 int err;
798
Ulf Hanssona2d10862013-12-16 14:37:26 +0100799 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300800 pr_warn("%s: %s - SANITIZE is not supported\n",
801 mmc_hostname(card->host), __func__);
802 err = -EOPNOTSUPP;
803 goto out;
804 }
805
806 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
807 mmc_hostname(card->host), __func__);
808
809 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
810 EXT_CSD_SANITIZE_START, 1,
811 MMC_SANITIZE_REQ_TIMEOUT);
812
813 if (err)
814 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
815 mmc_hostname(card->host), __func__, err);
816
817 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
818 __func__);
819out:
820 return err;
821}
822
Jon Huntera5f57742015-09-22 10:27:53 +0100823static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
824 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400825{
John Calixtocb87ea22011-04-26 18:56:29 -0400826 struct mmc_command cmd = {0};
827 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530828 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400829 struct scatterlist sg;
830 int err;
831
Jon Huntera5f57742015-09-22 10:27:53 +0100832 if (!card || !md || !idata)
833 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400834
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100835 cmd.opcode = idata->ic.opcode;
836 cmd.arg = idata->ic.arg;
837 cmd.flags = idata->ic.flags;
838
839 if (idata->buf_bytes) {
840 data.sg = &sg;
841 data.sg_len = 1;
842 data.blksz = idata->ic.blksz;
843 data.blocks = idata->ic.blocks;
844
845 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
846
847 if (idata->ic.write_flag)
848 data.flags = MMC_DATA_WRITE;
849 else
850 data.flags = MMC_DATA_READ;
851
852 /* data.flags must already be set before doing this. */
853 mmc_set_data_timeout(&data, card);
854
855 /* Allow overriding the timeout_ns for empirical tuning. */
856 if (idata->ic.data_timeout_ns)
857 data.timeout_ns = idata->ic.data_timeout_ns;
858
859 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
860 /*
861 * Pretend this is a data transfer and rely on the
862 * host driver to compute timeout. When all host
863 * drivers support cmd.cmd_timeout for R1B, this
864 * can be changed to:
865 *
866 * mrq.data = NULL;
867 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
868 */
869 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
870 }
871
872 mrq.data = &data;
873 }
874
875 mrq.cmd = &cmd;
876
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200877 err = mmc_blk_part_switch(card, md);
878 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100879 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200880
John Calixtocb87ea22011-04-26 18:56:29 -0400881 if (idata->ic.is_acmd) {
882 err = mmc_app_cmd(card->host, card);
883 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100884 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400885 }
886
Yaniv Gardia82e4842013-06-05 14:13:08 +0300887 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
888 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300889 err = ioctl_do_sanitize(card);
890
891 if (err)
892 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
893 __func__, err);
894
Jon Huntera5f57742015-09-22 10:27:53 +0100895 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300896 }
897
John Calixtocb87ea22011-04-26 18:56:29 -0400898 mmc_wait_for_req(card->host, &mrq);
899
900 if (cmd.error) {
901 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
902 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100903 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400904 }
905 if (data.error) {
906 dev_err(mmc_dev(card->host), "%s: data error %d\n",
907 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100908 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400909 }
910
911 /*
912 * According to the SD specs, some commands require a delay after
913 * issuing the command.
914 */
915 if (idata->ic.postsleep_min_us)
916 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
917
Jon Huntera5f57742015-09-22 10:27:53 +0100918 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400919
Krishna Kondae6711632014-12-04 15:20:57 +0200920 return err;
921}
922
923struct mmc_blk_ioc_rpmb_data {
924 struct mmc_blk_ioc_data *data[MMC_IOC_MAX_RPMB_CMD];
925};
926
927static struct mmc_blk_ioc_rpmb_data *mmc_blk_ioctl_rpmb_copy_from_user(
928 struct mmc_ioc_rpmb __user *user)
929{
930 struct mmc_blk_ioc_rpmb_data *idata;
931 int err, i;
932
933 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
934 if (!idata) {
935 err = -ENOMEM;
936 goto out;
937 }
938
939 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
940 idata->data[i] = mmc_blk_ioctl_copy_from_user(&(user->cmds[i]));
941 if (IS_ERR(idata->data[i])) {
942 err = PTR_ERR(idata->data[i]);
943 goto copy_err;
944 }
945 }
946
947 return idata;
948
949copy_err:
950 while (--i >= 0) {
951 kfree(idata->data[i]->buf);
952 kfree(idata->data[i]);
953 }
954 kfree(idata);
955out:
956 return ERR_PTR(err);
957}
958
959static int mmc_blk_ioctl_rpmb_cmd(struct block_device *bdev,
960 struct mmc_ioc_rpmb __user *ic_ptr)
961{
962 struct mmc_blk_ioc_rpmb_data *idata;
963 struct mmc_blk_data *md;
964 struct mmc_card *card;
965 struct mmc_command cmd = {0};
966 struct mmc_data data = {0};
967 struct mmc_request mrq = {NULL};
968 struct scatterlist sg;
969 int err = 0, i = 0;
970 u32 status = 0;
971
972 /* The caller must have CAP_SYS_RAWIO */
973 if (!capable(CAP_SYS_RAWIO))
974 return -EPERM;
975
976 md = mmc_blk_get(bdev->bd_disk);
977 /* make sure this is a rpmb partition */
978 if ((!md) || (!(md->area_type & MMC_BLK_DATA_AREA_RPMB))) {
979 err = -EINVAL;
980 goto cmd_done;
981 }
982
983 idata = mmc_blk_ioctl_rpmb_copy_from_user(ic_ptr);
984 if (IS_ERR(idata)) {
985 err = PTR_ERR(idata);
986 goto cmd_done;
987 }
988
989 card = md->queue.card;
990 if (IS_ERR(card)) {
991 err = PTR_ERR(card);
992 goto idata_free;
993 }
994
995 mmc_claim_host(card->host);
996
997 err = mmc_blk_part_switch(card, md);
998 if (err)
999 goto cmd_rel_host;
1000
1001 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1002 struct mmc_blk_ioc_data *curr_data;
1003 struct mmc_ioc_cmd *curr_cmd;
1004
1005 curr_data = idata->data[i];
1006 curr_cmd = &curr_data->ic;
1007 if (!curr_cmd->opcode)
1008 break;
1009
1010 cmd.opcode = curr_cmd->opcode;
1011 cmd.arg = curr_cmd->arg;
1012 cmd.flags = curr_cmd->flags;
1013
1014 if (curr_data->buf_bytes) {
1015 data.sg = &sg;
1016 data.sg_len = 1;
1017 data.blksz = curr_cmd->blksz;
1018 data.blocks = curr_cmd->blocks;
1019
1020 sg_init_one(data.sg, curr_data->buf,
1021 curr_data->buf_bytes);
1022
1023 if (curr_cmd->write_flag)
1024 data.flags = MMC_DATA_WRITE;
1025 else
1026 data.flags = MMC_DATA_READ;
1027
1028 /* data.flags must already be set before doing this. */
1029 mmc_set_data_timeout(&data, card);
1030
1031 /*
1032 * Allow overriding the timeout_ns for empirical tuning.
1033 */
1034 if (curr_cmd->data_timeout_ns)
1035 data.timeout_ns = curr_cmd->data_timeout_ns;
1036
1037 mrq.data = &data;
1038 }
1039
1040 mrq.cmd = &cmd;
1041
1042 err = mmc_set_blockcount(card, data.blocks,
1043 curr_cmd->write_flag & (1 << 31));
1044 if (err)
1045 goto cmd_rel_host;
1046
1047 mmc_wait_for_req(card->host, &mrq);
1048
1049 if (cmd.error) {
1050 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
1051 __func__, cmd.error);
1052 err = cmd.error;
1053 goto cmd_rel_host;
1054 }
1055 if (data.error) {
1056 dev_err(mmc_dev(card->host), "%s: data error %d\n",
1057 __func__, data.error);
1058 err = data.error;
1059 goto cmd_rel_host;
1060 }
1061
1062 if (copy_to_user(&(ic_ptr->cmds[i].response), cmd.resp,
1063 sizeof(cmd.resp))) {
1064 err = -EFAULT;
1065 goto cmd_rel_host;
1066 }
1067
1068 if (!curr_cmd->write_flag) {
1069 if (copy_to_user((void __user *)(unsigned long)
1070 curr_cmd->data_ptr,
1071 curr_data->buf,
1072 curr_data->buf_bytes)) {
1073 err = -EFAULT;
1074 goto cmd_rel_host;
1075 }
1076 }
1077
Loic Pallardy8d1e9772012-08-06 17:12:31 +02001078 /*
1079 * Ensure RPMB command has completed by polling CMD13
1080 * "Send Status".
1081 */
1082 err = ioctl_rpmb_card_status_poll(card, &status, 5);
1083 if (err)
1084 dev_err(mmc_dev(card->host),
1085 "%s: Card Status=0x%08X, error %d\n",
1086 __func__, status, err);
1087 }
1088
Krishna Kondae6711632014-12-04 15:20:57 +02001089cmd_rel_host:
1090 mmc_put_card(card);
1091
1092idata_free:
1093 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1094 kfree(idata->data[i]->buf);
1095 kfree(idata->data[i]);
1096 }
1097 kfree(idata);
1098
1099cmd_done:
1100 mmc_blk_put(md);
Jon Huntera5f57742015-09-22 10:27:53 +01001101 return err;
1102}
1103
1104static int mmc_blk_ioctl_cmd(struct block_device *bdev,
1105 struct mmc_ioc_cmd __user *ic_ptr)
1106{
1107 struct mmc_blk_ioc_data *idata;
1108 struct mmc_blk_data *md;
1109 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -07001110 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001111
Shawn Lin83c742c2016-03-16 18:15:47 +08001112 /*
1113 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1114 * whole block device, not on a partition. This prevents overspray
1115 * between sibling partitions.
1116 */
1117 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1118 return -EPERM;
1119
Jon Huntera5f57742015-09-22 10:27:53 +01001120 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
Asutosh Dasbbefab32013-10-07 14:53:32 +05301121 if (IS_ERR_OR_NULL(idata))
Jon Huntera5f57742015-09-22 10:27:53 +01001122 return PTR_ERR(idata);
1123
1124 md = mmc_blk_get(bdev->bd_disk);
1125 if (!md) {
1126 err = -EINVAL;
1127 goto cmd_err;
1128 }
1129
1130 card = md->queue.card;
Asutosh Dasbbefab32013-10-07 14:53:32 +05301131 if (IS_ERR_OR_NULL(card)) {
Jon Huntera5f57742015-09-22 10:27:53 +01001132 err = PTR_ERR(card);
1133 goto cmd_done;
1134 }
1135
1136 mmc_get_card(card);
1137
Grant Grundlerb0934102015-09-23 18:30:33 -07001138 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001139
Adrian Hunter3c866562016-05-04 14:38:12 +03001140 /* Always switch back to main area after RPMB access */
1141 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1142 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1143
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001144 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -04001145
Grant Grundlerb0934102015-09-23 18:30:33 -07001146 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001147
John Calixtocb87ea22011-04-26 18:56:29 -04001148cmd_done:
1149 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +03001150cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -04001151 kfree(idata->buf);
1152 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001153 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -04001154}
1155
Jon Huntera5f57742015-09-22 10:27:53 +01001156static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
1157 struct mmc_ioc_multi_cmd __user *user)
1158{
1159 struct mmc_blk_ioc_data **idata = NULL;
1160 struct mmc_ioc_cmd __user *cmds = user->cmds;
1161 struct mmc_card *card;
1162 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -07001163 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001164 __u64 num_of_cmds;
1165
Shawn Lin83c742c2016-03-16 18:15:47 +08001166 /*
1167 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1168 * whole block device, not on a partition. This prevents overspray
1169 * between sibling partitions.
1170 */
1171 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1172 return -EPERM;
1173
Jon Huntera5f57742015-09-22 10:27:53 +01001174 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
1175 sizeof(num_of_cmds)))
1176 return -EFAULT;
1177
1178 if (num_of_cmds > MMC_IOC_MAX_CMDS)
1179 return -EINVAL;
1180
1181 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
1182 if (!idata)
1183 return -ENOMEM;
1184
1185 for (i = 0; i < num_of_cmds; i++) {
1186 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
1187 if (IS_ERR(idata[i])) {
1188 err = PTR_ERR(idata[i]);
1189 num_of_cmds = i;
1190 goto cmd_err;
1191 }
1192 }
1193
1194 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -08001195 if (!md) {
1196 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +01001197 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -08001198 }
Jon Huntera5f57742015-09-22 10:27:53 +01001199
1200 card = md->queue.card;
1201 if (IS_ERR(card)) {
1202 err = PTR_ERR(card);
1203 goto cmd_done;
1204 }
1205
1206 mmc_get_card(card);
1207
Grant Grundlerb0934102015-09-23 18:30:33 -07001208 for (i = 0; i < num_of_cmds && !ioc_err; i++)
1209 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001210
Adrian Hunter3c866562016-05-04 14:38:12 +03001211 /* Always switch back to main area after RPMB access */
1212 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1213 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1214
Jon Huntera5f57742015-09-22 10:27:53 +01001215 mmc_put_card(card);
1216
1217 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -07001218 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +01001219 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001220
1221cmd_done:
1222 mmc_blk_put(md);
1223cmd_err:
1224 for (i = 0; i < num_of_cmds; i++) {
1225 kfree(idata[i]->buf);
1226 kfree(idata[i]);
1227 }
1228 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001229 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +01001230}
1231
John Calixtocb87ea22011-04-26 18:56:29 -04001232static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
1233 unsigned int cmd, unsigned long arg)
1234{
Jon Huntera5f57742015-09-22 10:27:53 +01001235 switch (cmd) {
1236 case MMC_IOC_CMD:
1237 return mmc_blk_ioctl_cmd(bdev,
1238 (struct mmc_ioc_cmd __user *)arg);
Krishna Kondae6711632014-12-04 15:20:57 +02001239 case MMC_IOC_RPMB_CMD:
1240 return mmc_blk_ioctl_rpmb_cmd(bdev,
1241 (struct mmc_ioc_rpmb __user *)arg);
Jon Huntera5f57742015-09-22 10:27:53 +01001242 case MMC_IOC_MULTI_CMD:
1243 return mmc_blk_ioctl_multi_cmd(bdev,
1244 (struct mmc_ioc_multi_cmd __user *)arg);
1245 default:
1246 return -EINVAL;
1247 }
John Calixtocb87ea22011-04-26 18:56:29 -04001248}
1249
1250#ifdef CONFIG_COMPAT
1251static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
1252 unsigned int cmd, unsigned long arg)
1253{
1254 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
1255}
1256#endif
1257
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001258static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -05001259 .open = mmc_blk_open,
1260 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001261 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -04001263 .ioctl = mmc_blk_ioctl,
1264#ifdef CONFIG_COMPAT
1265 .compat_ioctl = mmc_blk_compat_ioctl,
1266#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267};
1268
Andrei Warkentin371a6892011-04-11 18:10:25 -05001269static inline int mmc_blk_part_switch(struct mmc_card *card,
1270 struct mmc_blk_data *md)
1271{
1272 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001273 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001274
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001275 if ((main_md->part_curr == md->part_type) &&
1276 (card->part_curr == md->part_type))
Andrei Warkentin371a6892011-04-11 18:10:25 -05001277 return 0;
1278
1279 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001280 u8 part_config = card->ext_csd.part_config;
1281
Adrian Hunter57da0c02016-05-04 14:38:13 +03001282 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1283 mmc_retune_pause(card->host);
1284
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001285 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1286 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001287
1288 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001289 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001290 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001291 if (ret) {
1292 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1293 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001294 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001295 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001296
1297 card->ext_csd.part_config = part_config;
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001298 card->part_curr = md->part_type;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001299
1300 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1301 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001302 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001303
1304 main_md->part_curr = md->part_type;
1305 return 0;
1306}
1307
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001308static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1309{
1310 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001311 u32 result;
1312 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001313
Venkatraman Sad5fd972011-08-25 00:30:50 +05301314 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001315 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001316 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001317
1318 struct scatterlist sg;
1319
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001320 cmd.opcode = MMC_APP_CMD;
1321 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001322 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001323
1324 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001325 if (err)
1326 return (u32)-1;
1327 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001328 return (u32)-1;
1329
1330 memset(&cmd, 0, sizeof(struct mmc_command));
1331
1332 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1333 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001334 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001335
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001336 data.blksz = 4;
1337 data.blocks = 1;
1338 data.flags = MMC_DATA_READ;
1339 data.sg = &sg;
1340 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301341 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001342
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001343 mrq.cmd = &cmd;
1344 mrq.data = &data;
1345
Ben Dooks051913d2009-06-08 23:33:57 +01001346 blocks = kmalloc(4, GFP_KERNEL);
1347 if (!blocks)
1348 return (u32)-1;
1349
1350 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001351
1352 mmc_wait_for_req(card->host, &mrq);
1353
Ben Dooks051913d2009-06-08 23:33:57 +01001354 result = ntohl(*blocks);
1355 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001356
Ben Dooks051913d2009-06-08 23:33:57 +01001357 if (cmd.error || data.error)
1358 result = (u32)-1;
1359
1360 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001361}
1362
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001363static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001364{
Chris Ball1278dba2011-04-13 23:40:30 -04001365 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001366 int err;
1367
Adrian Hunter504f1912008-10-16 12:55:25 +03001368 cmd.opcode = MMC_SEND_STATUS;
1369 if (!mmc_host_is_spi(card->host))
1370 cmd.arg = card->rca << 16;
1371 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001372 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1373 if (err == 0)
1374 *status = cmd.resp[0];
1375 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001376}
1377
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001378static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001379 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001380{
1381 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1382 int err = 0;
1383 u32 status;
1384
1385 do {
1386 err = get_card_status(card, &status, 5);
1387 if (err) {
1388 pr_err("%s: error %d requesting status\n",
1389 req->rq_disk->disk_name, err);
1390 return err;
1391 }
1392
1393 if (status & R1_ERROR) {
1394 pr_err("%s: %s: error sending status cmd, status %#x\n",
1395 req->rq_disk->disk_name, __func__, status);
1396 *gen_err = 1;
1397 }
1398
Ulf Hansson95a91292014-01-29 13:11:27 +01001399 /* We may rely on the host hw to handle busy detection.*/
1400 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1401 hw_busy_detect)
1402 break;
1403
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001404 /*
1405 * Timeout if the device never becomes ready for data and never
1406 * leaves the program state.
1407 */
1408 if (time_after(jiffies, timeout)) {
1409 pr_err("%s: Card stuck in programming state! %s %s\n",
1410 mmc_hostname(card->host),
1411 req->rq_disk->disk_name, __func__);
1412 return -ETIMEDOUT;
1413 }
1414
1415 /*
1416 * Some cards mishandle the status bits,
1417 * so make sure to check both the busy
1418 * indication and the card state.
1419 */
1420 } while (!(status & R1_READY_FOR_DATA) ||
1421 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1422
1423 return err;
1424}
1425
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001426static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1427 struct request *req, int *gen_err, u32 *stop_status)
1428{
1429 struct mmc_host *host = card->host;
1430 struct mmc_command cmd = {0};
1431 int err;
1432 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1433
1434 /*
1435 * Normally we use R1B responses for WRITE, but in cases where the host
1436 * has specified a max_busy_timeout we need to validate it. A failure
1437 * means we need to prevent the host from doing hw busy detection, which
1438 * is done by converting to a R1 response instead.
1439 */
1440 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1441 use_r1b_resp = false;
1442
1443 cmd.opcode = MMC_STOP_TRANSMISSION;
1444 if (use_r1b_resp) {
1445 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1446 cmd.busy_timeout = timeout_ms;
1447 } else {
1448 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1449 }
1450
1451 err = mmc_wait_for_cmd(host, &cmd, 5);
1452 if (err)
1453 return err;
1454
1455 *stop_status = cmd.resp[0];
1456
1457 /* No need to check card status in case of READ. */
1458 if (rq_data_dir(req) == READ)
1459 return 0;
1460
1461 if (!mmc_host_is_spi(host) &&
1462 (*stop_status & R1_ERROR)) {
1463 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1464 req->rq_disk->disk_name, __func__, *stop_status);
1465 *gen_err = 1;
1466 }
1467
1468 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1469}
1470
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301471#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001472#define ERR_RETRY 2
1473#define ERR_ABORT 1
1474#define ERR_CONTINUE 0
1475
1476static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1477 bool status_valid, u32 status)
1478{
1479 switch (error) {
1480 case -EILSEQ:
1481 /* response crc error, retry the r/w cmd */
1482 pr_err("%s: %s sending %s command, card status %#x\n",
1483 req->rq_disk->disk_name, "response CRC error",
1484 name, status);
1485 return ERR_RETRY;
1486
1487 case -ETIMEDOUT:
1488 pr_err("%s: %s sending %s command, card status %#x\n",
1489 req->rq_disk->disk_name, "timed out", name, status);
1490
1491 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301492 if (!status_valid) {
1493 pr_err("%s: status not valid, retrying timeout\n",
1494 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001495 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301496 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001497
1498 /*
1499 * If it was a r/w cmd crc error, or illegal command
1500 * (eg, issued in wrong state) then retry - we should
1501 * have corrected the state problem above.
1502 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301503 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1504 pr_err("%s: command error, retrying timeout\n",
1505 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001506 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301507 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001508
1509 /* Otherwise abort the command */
1510 return ERR_ABORT;
1511
1512 default:
1513 /* We don't understand the error code the driver gave us */
1514 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1515 req->rq_disk->disk_name, error, status);
1516 return ERR_ABORT;
1517 }
1518}
1519
1520/*
1521 * Initial r/w and stop cmd error recovery.
1522 * We don't know whether the card received the r/w cmd or not, so try to
1523 * restore things back to a sane state. Essentially, we do this as follows:
1524 * - Obtain card status. If the first attempt to obtain card status fails,
1525 * the status word will reflect the failed status cmd, not the failed
1526 * r/w cmd. If we fail to obtain card status, it suggests we can no
1527 * longer communicate with the card.
1528 * - Check the card state. If the card received the cmd but there was a
1529 * transient problem with the response, it might still be in a data transfer
1530 * mode. Try to send it a stop command. If this fails, we can't recover.
1531 * - If the r/w cmd failed due to a response CRC error, it was probably
1532 * transient, so retry the cmd.
1533 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1534 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1535 * illegal cmd, retry.
1536 * Otherwise we don't understand what happened, so abort.
1537 */
1538static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001539 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001540{
1541 bool prev_cmd_status_valid = true;
1542 u32 status, stop_status = 0;
1543 int err, retry;
1544
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301545 if (mmc_card_removed(card))
1546 return ERR_NOMEDIUM;
1547
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001548 /*
1549 * Try to get card status which indicates both the card state
1550 * and why there was no response. If the first attempt fails,
1551 * we can't be sure the returned status is for the r/w command.
1552 */
1553 for (retry = 2; retry >= 0; retry--) {
1554 err = get_card_status(card, &status, 0);
1555 if (!err)
1556 break;
1557
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001558 /* Re-tune if needed */
1559 mmc_retune_recheck(card->host);
1560
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001561 prev_cmd_status_valid = false;
1562 pr_err("%s: error %d sending status command, %sing\n",
1563 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1564 }
1565
1566 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301567 if (err) {
1568 /* Check if the card is removed */
1569 if (mmc_detect_card_removed(card->host))
1570 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001571 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301572 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001573
Adrian Hunter67716322011-08-29 16:42:15 +03001574 /* Flag ECC errors */
1575 if ((status & R1_CARD_ECC_FAILED) ||
1576 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1577 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1578 *ecc_err = 1;
1579
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001580 /* Flag General errors */
1581 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1582 if ((status & R1_ERROR) ||
1583 (brq->stop.resp[0] & R1_ERROR)) {
1584 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1585 req->rq_disk->disk_name, __func__,
1586 brq->stop.resp[0], status);
1587 *gen_err = 1;
1588 }
1589
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001590 /*
1591 * Check the current card state. If it is in some data transfer
1592 * mode, tell it to stop (and hopefully transition back to TRAN.)
1593 */
1594 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1595 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001596 err = send_stop(card,
1597 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1598 req, gen_err, &stop_status);
1599 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001600 pr_err("%s: error %d sending stop command\n",
1601 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001602 /*
1603 * If the stop cmd also timed out, the card is probably
1604 * not present, so abort. Other errors are bad news too.
1605 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001606 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001607 }
1608
Adrian Hunter67716322011-08-29 16:42:15 +03001609 if (stop_status & R1_CARD_ECC_FAILED)
1610 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001611 }
1612
1613 /* Check for set block count errors */
1614 if (brq->sbc.error)
1615 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1616 prev_cmd_status_valid, status);
1617
1618 /* Check for r/w command errors */
1619 if (brq->cmd.error)
1620 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1621 prev_cmd_status_valid, status);
1622
Adrian Hunter67716322011-08-29 16:42:15 +03001623 /* Data errors */
1624 if (!brq->stop.error)
1625 return ERR_CONTINUE;
1626
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001627 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001628 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 +01001629 req->rq_disk->disk_name, brq->stop.error,
1630 brq->cmd.resp[0], status);
1631
1632 /*
1633 * Subsitute in our own stop status as this will give the error
1634 * state which happened during the execution of the r/w command.
1635 */
1636 if (stop_status) {
1637 brq->stop.resp[0] = stop_status;
1638 brq->stop.error = 0;
1639 }
1640 return ERR_CONTINUE;
1641}
1642
Adrian Hunter67716322011-08-29 16:42:15 +03001643static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1644 int type)
1645{
1646 int err;
1647
1648 if (md->reset_done & type)
1649 return -EEXIST;
1650
1651 md->reset_done |= type;
1652 err = mmc_hw_reset(host);
1653 /* Ensure we switch back to the correct partition */
1654 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001655 struct mmc_blk_data *main_md =
1656 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001657 int part_err;
1658
1659 main_md->part_curr = main_md->part_type;
1660 part_err = mmc_blk_part_switch(host->card, md);
1661 if (part_err) {
1662 /*
1663 * We have failed to get back into the correct
1664 * partition, so we need to abort the whole request.
1665 */
1666 return -ENODEV;
1667 }
1668 }
1669 return err;
1670}
1671
1672static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1673{
1674 md->reset_done &= ~type;
1675}
1676
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001677int mmc_access_rpmb(struct mmc_queue *mq)
1678{
1679 struct mmc_blk_data *md = mq->data;
1680 /*
1681 * If this is a RPMB partition access, return ture
1682 */
1683 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1684 return true;
1685
1686 return false;
1687}
1688
Adrian Hunterbd788c92010-08-11 14:17:47 -07001689static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1690{
1691 struct mmc_blk_data *md = mq->data;
1692 struct mmc_card *card = md->queue.card;
1693 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001694 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001695
Adrian Hunterbd788c92010-08-11 14:17:47 -07001696 if (!mmc_can_erase(card)) {
1697 err = -EOPNOTSUPP;
1698 goto out;
1699 }
1700
1701 from = blk_rq_pos(req);
1702 nr = blk_rq_sectors(req);
1703
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001704 if (mmc_can_discard(card))
1705 arg = MMC_DISCARD_ARG;
1706 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001707 arg = MMC_TRIM_ARG;
1708 else
1709 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001710retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001711 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1712 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1713 INAND_CMD38_ARG_EXT_CSD,
1714 arg == MMC_TRIM_ARG ?
1715 INAND_CMD38_ARG_TRIM :
1716 INAND_CMD38_ARG_ERASE,
1717 0);
1718 if (err)
1719 goto out;
1720 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001721 err = mmc_erase(card, from, nr, arg);
1722out:
Adrian Hunter67716322011-08-29 16:42:15 +03001723 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1724 goto retry;
1725 if (!err)
1726 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301727 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001728
Adrian Hunterbd788c92010-08-11 14:17:47 -07001729 return err ? 0 : 1;
1730}
1731
Adrian Hunter49804542010-08-11 14:17:50 -07001732static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1733 struct request *req)
1734{
1735 struct mmc_blk_data *md = mq->data;
1736 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001737 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001738 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001739
Maya Erez775a9362013-04-18 15:41:55 +03001740 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001741 err = -EOPNOTSUPP;
1742 goto out;
1743 }
1744
1745 from = blk_rq_pos(req);
1746 nr = blk_rq_sectors(req);
1747
Maya Erez775a9362013-04-18 15:41:55 +03001748 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1749 arg = MMC_SECURE_TRIM1_ARG;
1750 else
1751 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001752
Adrian Hunter67716322011-08-29 16:42:15 +03001753retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001754 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1755 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1756 INAND_CMD38_ARG_EXT_CSD,
1757 arg == MMC_SECURE_TRIM1_ARG ?
1758 INAND_CMD38_ARG_SECTRIM1 :
1759 INAND_CMD38_ARG_SECERASE,
1760 0);
1761 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001762 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001763 }
Adrian Hunter28302812012-04-05 14:45:48 +03001764
Adrian Hunter49804542010-08-11 14:17:50 -07001765 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001766 if (err == -EIO)
1767 goto out_retry;
1768 if (err)
1769 goto out;
1770
1771 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001772 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1773 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1774 INAND_CMD38_ARG_EXT_CSD,
1775 INAND_CMD38_ARG_SECTRIM2,
1776 0);
1777 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001778 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001779 }
Adrian Hunter28302812012-04-05 14:45:48 +03001780
Adrian Hunter49804542010-08-11 14:17:50 -07001781 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001782 if (err == -EIO)
1783 goto out_retry;
1784 if (err)
1785 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001786 }
Adrian Hunter28302812012-04-05 14:45:48 +03001787
Adrian Hunter28302812012-04-05 14:45:48 +03001788out_retry:
1789 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001790 goto retry;
1791 if (!err)
1792 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001793out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301794 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001795
Adrian Hunter49804542010-08-11 14:17:50 -07001796 return err ? 0 : 1;
1797}
1798
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001799static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1800{
1801 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001802 struct mmc_card *card = md->queue.card;
1803 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001804
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001805 ret = mmc_flush_cache(card);
1806 if (ret)
1807 ret = -EIO;
1808
Mark Salyzyn6904e432016-01-28 11:12:25 -08001809#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
1810 else if (atomic_read(&mq->cache_size)) {
1811 long used = mmc_blk_cache_used(mq, jiffies);
1812
1813 if (used) {
1814 int speed = atomic_read(&mq->max_write_speed);
1815
1816 if (speed_valid(speed)) {
1817 unsigned long msecs = jiffies_to_msecs(
1818 size_and_speed_to_jiffies(
1819 used, speed));
1820 if (msecs)
1821 msleep(msecs);
1822 }
1823 }
1824 }
1825#endif
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301826 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001827
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001828 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001829}
1830
1831/*
1832 * Reformat current write as a reliable write, supporting
1833 * both legacy and the enhanced reliable write MMC cards.
1834 * In each transfer we'll handle only as much as a single
1835 * reliable write can handle, thus finish the request in
1836 * partial completions.
1837 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001838static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1839 struct mmc_card *card,
1840 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001841{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001842 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1843 /* Legacy mode imposes restrictions on transfers. */
1844 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1845 brq->data.blocks = 1;
1846
1847 if (brq->data.blocks > card->ext_csd.rel_sectors)
1848 brq->data.blocks = card->ext_csd.rel_sectors;
1849 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1850 brq->data.blocks = 1;
1851 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001852}
1853
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001854#define CMD_ERRORS \
1855 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1856 R1_ADDRESS_ERROR | /* Misaligned address */ \
1857 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1858 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1859 R1_CC_ERROR | /* Card controller error */ \
1860 R1_ERROR) /* General/unknown error */
1861
Per Forlinee8a43a2011-07-01 18:55:33 +02001862static int mmc_blk_err_check(struct mmc_card *card,
1863 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001864{
Per Forlinee8a43a2011-07-01 18:55:33 +02001865 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1866 mmc_active);
1867 struct mmc_blk_request *brq = &mq_mrq->brq;
1868 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001869 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001870 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001871
1872 /*
1873 * sbc.error indicates a problem with the set block count
1874 * command. No data will have been transferred.
1875 *
1876 * cmd.error indicates a problem with the r/w command. No
1877 * data will have been transferred.
1878 *
1879 * stop.error indicates a problem with the stop command. Data
1880 * may have been transferred, or may still be transferring.
1881 */
Adrian Hunter67716322011-08-29 16:42:15 +03001882 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1883 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001884 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001885 case ERR_RETRY:
1886 return MMC_BLK_RETRY;
1887 case ERR_ABORT:
1888 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301889 case ERR_NOMEDIUM:
1890 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001891 case ERR_CONTINUE:
1892 break;
1893 }
1894 }
1895
1896 /*
1897 * Check for errors relating to the execution of the
1898 * initial command - such as address errors. No data
1899 * has been transferred.
1900 */
1901 if (brq->cmd.resp[0] & CMD_ERRORS) {
1902 pr_err("%s: r/w command failed, status = %#x\n",
1903 req->rq_disk->disk_name, brq->cmd.resp[0]);
1904 return MMC_BLK_ABORT;
1905 }
1906
1907 /*
1908 * Everything else is either success, or a data error of some
1909 * kind. If it was a write, we may have transitioned to
1910 * program mode, which we have to wait for it to complete.
1911 */
1912 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001913 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001914
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001915 /* Check stop command response */
1916 if (brq->stop.resp[0] & R1_ERROR) {
1917 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1918 req->rq_disk->disk_name, __func__,
1919 brq->stop.resp[0]);
1920 gen_err = 1;
1921 }
1922
Ulf Hansson95a91292014-01-29 13:11:27 +01001923 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1924 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001925 if (err)
1926 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001927 }
1928
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001929 /* if general error occurs, retry the write operation. */
1930 if (gen_err) {
1931 pr_warn("%s: retrying write for general error\n",
1932 req->rq_disk->disk_name);
1933 return MMC_BLK_RETRY;
1934 }
1935
Per Forlind78d4a82011-07-01 18:55:30 +02001936 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001937 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001938 pr_debug("%s: retrying because a re-tune was needed\n",
1939 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001940 brq->retune_retry_done = 1;
1941 return MMC_BLK_RETRY;
1942 }
Per Forlind78d4a82011-07-01 18:55:30 +02001943 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1944 req->rq_disk->disk_name, brq->data.error,
1945 (unsigned)blk_rq_pos(req),
1946 (unsigned)blk_rq_sectors(req),
1947 brq->cmd.resp[0], brq->stop.resp[0]);
1948
1949 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001950 if (ecc_err)
1951 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001952 return MMC_BLK_DATA_ERR;
1953 } else {
1954 return MMC_BLK_CMD_ERR;
1955 }
1956 }
1957
Adrian Hunter67716322011-08-29 16:42:15 +03001958 if (!brq->data.bytes_xfered)
1959 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001960
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001961 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1962 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1963 return MMC_BLK_PARTIAL;
1964 else
1965 return MMC_BLK_SUCCESS;
1966 }
1967
Adrian Hunter67716322011-08-29 16:42:15 +03001968 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1969 return MMC_BLK_PARTIAL;
1970
1971 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001972}
1973
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001974static int mmc_blk_packed_err_check(struct mmc_card *card,
1975 struct mmc_async_req *areq)
1976{
1977 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1978 mmc_active);
1979 struct request *req = mq_rq->req;
1980 struct mmc_packed *packed = mq_rq->packed;
1981 int err, check, status;
1982 u8 *ext_csd;
1983
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001984 packed->retries--;
1985 check = mmc_blk_err_check(card, areq);
1986 err = get_card_status(card, &status, 0);
1987 if (err) {
1988 pr_err("%s: error %d sending status command\n",
1989 req->rq_disk->disk_name, err);
1990 return MMC_BLK_ABORT;
1991 }
1992
1993 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02001994 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001995 if (err) {
1996 pr_err("%s: error %d sending ext_csd\n",
1997 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02001998 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001999 }
2000
2001 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
2002 EXT_CSD_PACKED_FAILURE) &&
2003 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2004 EXT_CSD_PACKED_GENERIC_ERROR)) {
2005 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2006 EXT_CSD_PACKED_INDEXED_ERROR) {
2007 packed->idx_failure =
2008 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
2009 check = MMC_BLK_PARTIAL;
2010 }
2011 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
2012 "failure index: %d\n",
2013 req->rq_disk->disk_name, packed->nr_entries,
2014 packed->blocks, packed->idx_failure);
2015 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002016 kfree(ext_csd);
2017 }
2018
2019 return check;
2020}
2021
Per Forlin54d49d72011-07-01 18:55:29 +02002022static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
2023 struct mmc_card *card,
2024 int disable_multi,
2025 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Per Forlin54d49d72011-07-01 18:55:29 +02002027 u32 readcmd, writecmd;
2028 struct mmc_blk_request *brq = &mqrq->brq;
2029 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05302031 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002033 /*
2034 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00002035 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002036 */
Luca Porziod3df0462015-11-06 15:12:26 +00002037 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002038 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002039 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002040
Per Forlin54d49d72011-07-01 18:55:29 +02002041 memset(brq, 0, sizeof(struct mmc_blk_request));
2042 brq->mrq.cmd = &brq->cmd;
2043 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Per Forlin54d49d72011-07-01 18:55:29 +02002045 brq->cmd.arg = blk_rq_pos(req);
2046 if (!mmc_card_blockaddr(card))
2047 brq->cmd.arg <<= 9;
2048 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2049 brq->data.blksz = 512;
2050 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2051 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002052 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Asutosh Dasf0665412012-07-27 18:10:19 +05302054 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02002055 /*
2056 * The block layer doesn't support all sector count
2057 * restrictions, so we need to be prepared for too big
2058 * requests.
2059 */
2060 if (brq->data.blocks > card->host->max_blk_count)
2061 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002063 if (brq->data.blocks > 1) {
2064 /*
2065 * After a read error, we redo the request one sector
2066 * at a time in order to accurately determine which
2067 * sectors can be read successfully.
2068 */
2069 if (disable_multi)
2070 brq->data.blocks = 1;
2071
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07002072 /*
2073 * Some controllers have HW issues while operating
2074 * in multiple I/O mode
2075 */
2076 if (card->host->ops->multi_io_quirk)
2077 brq->data.blocks = card->host->ops->multi_io_quirk(card,
2078 (rq_data_dir(req) == READ) ?
2079 MMC_DATA_READ : MMC_DATA_WRITE,
2080 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002081 }
Per Forlin54d49d72011-07-01 18:55:29 +02002082
2083 if (brq->data.blocks > 1 || do_rel_wr) {
2084 /* SPI multiblock writes terminate using a special
2085 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02002086 */
Per Forlin54d49d72011-07-01 18:55:29 +02002087 if (!mmc_host_is_spi(card->host) ||
2088 rq_data_dir(req) == READ)
2089 brq->mrq.stop = &brq->stop;
2090 readcmd = MMC_READ_MULTIPLE_BLOCK;
2091 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
2092 } else {
2093 brq->mrq.stop = NULL;
2094 readcmd = MMC_READ_SINGLE_BLOCK;
2095 writecmd = MMC_WRITE_BLOCK;
2096 }
2097 if (rq_data_dir(req) == READ) {
2098 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002099 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002100 if (brq->mrq.stop)
2101 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
2102 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002103 } else {
2104 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002105 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002106 if (brq->mrq.stop)
2107 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
2108 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002109 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02002110
Per Forlin54d49d72011-07-01 18:55:29 +02002111 if (do_rel_wr)
2112 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01002113
Per Forlin54d49d72011-07-01 18:55:29 +02002114 /*
Saugata Das42659002011-12-21 13:09:17 +05302115 * Data tag is used only during writing meta data to speed
2116 * up write and any subsequent read of this meta data
2117 */
2118 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2119 (req->cmd_flags & REQ_META) &&
2120 (rq_data_dir(req) == WRITE) &&
2121 ((brq->data.blocks * brq->data.blksz) >=
2122 card->ext_csd.data_tag_unit_size);
2123
2124 /*
Per Forlin54d49d72011-07-01 18:55:29 +02002125 * Pre-defined multi-block transfers are preferable to
2126 * open ended-ones (and necessary for reliable writes).
2127 * However, it is not sufficient to just send CMD23,
2128 * and avoid the final CMD12, as on an error condition
2129 * CMD12 (stop) needs to be sent anyway. This, coupled
2130 * with Auto-CMD23 enhancements provided by some
2131 * hosts, means that the complexity of dealing
2132 * with this is best left to the host. If CMD23 is
2133 * supported by card and host, we'll fill sbc in and let
2134 * the host deal with handling it correctly. This means
2135 * that for hosts that don't expose MMC_CAP_CMD23, no
2136 * change of behavior will be observed.
2137 *
2138 * N.B: Some MMC cards experience perf degradation.
2139 * We'll avoid using CMD23-bounded multiblock writes for
2140 * these, while retaining features like reliable writes.
2141 */
Saugata Das42659002011-12-21 13:09:17 +05302142 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
2143 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
2144 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02002145 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2146 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05302147 (do_rel_wr ? (1 << 31) : 0) |
2148 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02002149 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2150 brq->mrq.sbc = &brq->sbc;
2151 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002152
Per Forlin54d49d72011-07-01 18:55:29 +02002153 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002154
Per Forlin54d49d72011-07-01 18:55:29 +02002155 brq->data.sg = mqrq->sg;
2156 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002157
Per Forlin54d49d72011-07-01 18:55:29 +02002158 /*
2159 * Adjust the sg list so it is the same size as the
2160 * request.
2161 */
2162 if (brq->data.blocks != blk_rq_sectors(req)) {
2163 int i, data_size = brq->data.blocks << 9;
2164 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02002165
Per Forlin54d49d72011-07-01 18:55:29 +02002166 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
2167 data_size -= sg->length;
2168 if (data_size <= 0) {
2169 sg->length += data_size;
2170 i++;
2171 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01002172 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002173 }
Per Forlin54d49d72011-07-01 18:55:29 +02002174 brq->data.sg_len = i;
2175 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002176
Per Forlinee8a43a2011-07-01 18:55:33 +02002177 mqrq->mmc_active.mrq = &brq->mrq;
2178 mqrq->mmc_active.err_check = mmc_blk_err_check;
2179
Per Forlin54d49d72011-07-01 18:55:29 +02002180 mmc_queue_bounce_pre(mqrq);
2181}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002183static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
2184 struct mmc_card *card)
2185{
2186 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
2187 unsigned int max_seg_sz = queue_max_segment_size(q);
2188 unsigned int len, nr_segs = 0;
2189
2190 do {
2191 len = min(hdr_sz, max_seg_sz);
2192 hdr_sz -= len;
2193 nr_segs++;
2194 } while (hdr_sz);
2195
2196 return nr_segs;
2197}
2198
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002199/**
2200 * mmc_blk_disable_wr_packing() - disables packing mode
2201 * @mq: MMC queue.
2202 *
2203 */
2204void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
2205{
2206 if (mq) {
2207 mq->wr_packing_enabled = false;
2208 mq->num_of_potential_packed_wr_reqs = 0;
2209 }
2210}
2211EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
2212
Lee Susman841fd132013-04-23 17:59:26 +03002213static int get_packed_trigger(int potential, struct mmc_card *card,
2214 struct request *req, int curr_trigger)
2215{
2216 static int num_mean_elements = 1;
2217 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2218 unsigned int trigger = curr_trigger;
2219 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
2220
2221 /* scale down the upper bound to 75% */
2222 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
2223
2224 /*
2225 * since the most common calls for this function are with small
2226 * potential write values and since we don't want these calls to affect
2227 * the packed trigger, set a lower bound and ignore calls with
2228 * potential lower than that bound
2229 */
2230 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
2231 return trigger;
2232
2233 /*
2234 * this is to prevent integer overflow in the following calculation:
2235 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
2236 */
2237 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
2238 num_mean_elements = 1;
2239 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2240 }
2241
2242 /*
2243 * get next mean value based on previous mean value and current
2244 * potential packed writes. Calculation is as follows:
2245 * mean_pot[i+1] =
2246 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
2247 */
2248 mean_potential *= num_mean_elements;
2249 /*
2250 * add num_mean_elements so that the division of two integers doesn't
2251 * lower mean_potential too much
2252 */
2253 if (potential > mean_potential)
2254 mean_potential += num_mean_elements;
2255 mean_potential += potential;
2256 /* this is for gaining more precision when dividing two integers */
2257 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
2258 /* this completes the mean calculation */
2259 mean_potential /= ++num_mean_elements;
2260 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
2261
2262 /*
2263 * if current potential packed writes is greater than the mean potential
2264 * then the heuristic is that the following workload will contain many
2265 * write requests, therefore we lower the packed trigger. In the
2266 * opposite case we want to increase the trigger in order to get less
2267 * packing events.
2268 */
2269 if (potential >= mean_potential)
2270 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
2271 PCKD_TRGR_LOWER_BOUND : trigger - 1;
2272 else
2273 trigger = (trigger >= pckd_trgr_upper_bound) ?
2274 pckd_trgr_upper_bound : trigger + 1;
2275
2276 /*
2277 * an urgent read request indicates a packed list being interrupted
2278 * by this read, therefore we aim for less packing, hence the trigger
2279 * gets increased
2280 */
2281 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
2282 trigger += PCKD_TRGR_URGENT_PENALTY;
2283
2284 return trigger;
2285}
2286
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002287static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2288 struct request *req)
2289{
2290 struct mmc_host *host = mq->card->host;
2291 int data_dir;
2292
2293 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2294 return;
2295
Maya Erez8e2b3c32012-12-02 13:27:15 +02002296 /* Support for the write packing on eMMC 4.5 or later */
2297 if (mq->card->ext_csd.rev <= 5)
2298 return;
2299
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002300 /*
2301 * In case the packing control is not supported by the host, it should
2302 * not have an effect on the write packing. Therefore we have to enable
2303 * the write packing
2304 */
2305 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2306 mq->wr_packing_enabled = true;
2307 return;
2308 }
2309
2310 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2311 if (mq->num_of_potential_packed_wr_reqs >
2312 mq->num_wr_reqs_to_start_packing)
2313 mq->wr_packing_enabled = true;
Lee Susman841fd132013-04-23 17:59:26 +03002314 mq->num_wr_reqs_to_start_packing =
2315 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2316 mq->card, req,
2317 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002318 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002319 return;
2320 }
2321
2322 data_dir = rq_data_dir(req);
2323
2324 if (data_dir == READ) {
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002325 mmc_blk_disable_wr_packing(mq);
Lee Susman841fd132013-04-23 17:59:26 +03002326 mq->num_wr_reqs_to_start_packing =
2327 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2328 mq->card, req,
2329 mq->num_wr_reqs_to_start_packing);
2330 mq->num_of_potential_packed_wr_reqs = 0;
2331 mq->wr_packing_enabled = false;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002332 return;
2333 } else if (data_dir == WRITE) {
2334 mq->num_of_potential_packed_wr_reqs++;
2335 }
2336
2337 if (mq->num_of_potential_packed_wr_reqs >
2338 mq->num_wr_reqs_to_start_packing)
2339 mq->wr_packing_enabled = true;
2340}
2341
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002342struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2343{
2344 if (!card)
2345 return NULL;
2346
2347 return &card->wr_pack_stats;
2348}
2349EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2350
2351void mmc_blk_init_packed_statistics(struct mmc_card *card)
2352{
2353 int max_num_of_packed_reqs = 0;
2354
2355 if (!card || !card->wr_pack_stats.packing_events)
2356 return;
2357
2358 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2359
2360 spin_lock(&card->wr_pack_stats.lock);
2361 memset(card->wr_pack_stats.packing_events, 0,
2362 (max_num_of_packed_reqs + 1) *
2363 sizeof(*card->wr_pack_stats.packing_events));
2364 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2365 sizeof(card->wr_pack_stats.pack_stop_reason));
2366 card->wr_pack_stats.enabled = true;
2367 spin_unlock(&card->wr_pack_stats.lock);
2368}
2369EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2370
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002371static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2372{
2373 struct request_queue *q = mq->queue;
2374 struct mmc_card *card = mq->card;
2375 struct request *cur = req, *next = NULL;
2376 struct mmc_blk_data *md = mq->data;
2377 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2378 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2379 unsigned int req_sectors = 0, phys_segments = 0;
2380 unsigned int max_blk_count, max_phys_segs;
2381 bool put_back = true;
2382 u8 max_packed_rw = 0;
2383 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002384 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002385
Shawn Lin96e52da2016-08-26 08:49:55 +08002386 /*
2387 * We don't need to check packed for any further
2388 * operation of packed stuff as we set MMC_PACKED_NONE
2389 * and return zero for reqs if geting null packed. Also
2390 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2391 * it again when removing blk req.
2392 */
2393 if (!mqrq->packed) {
2394 md->flags &= (~MMC_BLK_PACKED_CMD);
2395 goto no_packed;
2396 }
2397
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002398 if (!(md->flags & MMC_BLK_PACKED_CMD))
2399 goto no_packed;
2400
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002401 if (!mq->wr_packing_enabled)
2402 goto no_packed;
2403
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002404 if ((rq_data_dir(cur) == WRITE) &&
2405 mmc_host_packed_wr(card->host))
2406 max_packed_rw = card->ext_csd.max_packed_writes;
2407
2408 if (max_packed_rw == 0)
2409 goto no_packed;
2410
2411 if (mmc_req_rel_wr(cur) &&
2412 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2413 goto no_packed;
2414
2415 if (mmc_large_sector(card) &&
2416 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2417 goto no_packed;
2418
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002419 if (cur->cmd_flags & REQ_FUA)
2420 goto no_packed;
2421
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002422 mmc_blk_clear_packed(mqrq);
2423
2424 max_blk_count = min(card->host->max_blk_count,
2425 card->host->max_req_size >> 9);
2426 if (unlikely(max_blk_count > 0xffff))
2427 max_blk_count = 0xffff;
2428
2429 max_phys_segs = queue_max_segments(q);
2430 req_sectors += blk_rq_sectors(cur);
2431 phys_segments += cur->nr_phys_segments;
2432
2433 if (rq_data_dir(cur) == WRITE) {
2434 req_sectors += mmc_large_sector(card) ? 8 : 1;
2435 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2436 }
2437
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002438 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002439 do {
2440 if (reqs >= max_packed_rw - 1) {
2441 put_back = false;
2442 break;
2443 }
2444
2445 spin_lock_irq(q->queue_lock);
2446 next = blk_fetch_request(q);
2447 spin_unlock_irq(q->queue_lock);
2448 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002449 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002450 put_back = false;
2451 break;
2452 }
2453
2454 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002455 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2456 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002457 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002458 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002459
Mike Christie3a5e02c2016-06-05 14:32:23 -05002460 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002461 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002462 req_op(next) == REQ_OP_FLUSH) {
2463 if (req_op(next) != REQ_OP_SECURE_ERASE)
2464 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002465 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002466 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002467
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002468 if (next->cmd_flags & REQ_FUA) {
2469 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2470 break;
2471 }
2472
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002473 if (rq_data_dir(cur) != rq_data_dir(next)) {
2474 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002475 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002476 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002477
2478 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002479 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2480 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002481 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002482 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002483
2484 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002485 if (req_sectors > max_blk_count) {
2486 if (stats->enabled)
2487 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002488 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002489 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002490
2491 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002492 if (phys_segments > max_phys_segs) {
2493 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002494 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002495 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002496
Maya Erez5a8dae12014-12-04 15:13:59 +02002497 if (mq->no_pack_for_random) {
2498 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2499 blk_rq_pos(next)) {
2500 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2501 put_back = 1;
2502 break;
2503 }
2504 }
2505
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002506 if (rq_data_dir(next) == WRITE)
2507 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002508 list_add_tail(&next->queuelist, &mqrq->packed->list);
2509 cur = next;
2510 reqs++;
2511 } while (1);
2512
2513 if (put_back) {
2514 spin_lock_irq(q->queue_lock);
2515 blk_requeue_request(q, next);
2516 spin_unlock_irq(q->queue_lock);
2517 }
2518
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002519 if (stats->enabled) {
2520 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2521 stats->packing_events[reqs + 1]++;
2522 if (reqs + 1 == max_packed_rw)
2523 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2524 }
2525
2526 spin_unlock(&stats->lock);
2527
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002528 if (reqs > 0) {
2529 list_add(&req->queuelist, &mqrq->packed->list);
2530 mqrq->packed->nr_entries = ++reqs;
2531 mqrq->packed->retries = reqs;
2532 return reqs;
2533 }
2534
2535no_packed:
2536 mqrq->cmd_type = MMC_PACKED_NONE;
2537 return 0;
2538}
2539
2540static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2541 struct mmc_card *card,
2542 struct mmc_queue *mq)
2543{
2544 struct mmc_blk_request *brq = &mqrq->brq;
2545 struct request *req = mqrq->req;
2546 struct request *prq;
2547 struct mmc_blk_data *md = mq->data;
2548 struct mmc_packed *packed = mqrq->packed;
2549 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002550 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002551 u8 hdr_blocks;
2552 u8 i = 1;
2553
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002554 mqrq->cmd_type = MMC_PACKED_WRITE;
2555 packed->blocks = 0;
2556 packed->idx_failure = MMC_PACKED_NR_IDX;
2557
2558 packed_cmd_hdr = packed->cmd_hdr;
2559 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002560 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2561 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002562 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2563
2564 /*
2565 * Argument for each entry of packed group
2566 */
2567 list_for_each_entry(prq, &packed->list, queuelist) {
2568 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2569 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2570 (prq->cmd_flags & REQ_META) &&
2571 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002572 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002573 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002574 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002575 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2576 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002577 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002578 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002579 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002580 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002581 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002582 packed->blocks += blk_rq_sectors(prq);
2583 i++;
2584 }
2585
2586 memset(brq, 0, sizeof(struct mmc_blk_request));
2587 brq->mrq.cmd = &brq->cmd;
2588 brq->mrq.data = &brq->data;
2589 brq->mrq.sbc = &brq->sbc;
2590 brq->mrq.stop = &brq->stop;
2591
2592 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2593 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2594 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2595
2596 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2597 brq->cmd.arg = blk_rq_pos(req);
2598 if (!mmc_card_blockaddr(card))
2599 brq->cmd.arg <<= 9;
2600 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2601
2602 brq->data.blksz = 512;
2603 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002604 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302605 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002606
2607 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2608 brq->stop.arg = 0;
2609 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2610
2611 mmc_set_data_timeout(&brq->data, card);
2612
2613 brq->data.sg = mqrq->sg;
2614 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2615
2616 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002617
2618 /*
2619 * This is intended for packed commands tests usage - in case these
2620 * functions are not in use the respective pointers are NULL
2621 */
2622 if (mq->err_check_fn)
2623 mqrq->mmc_active.err_check = mq->err_check_fn;
2624 else
2625 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2626
2627 if (mq->packed_test_fn)
2628 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002629
2630 mmc_queue_bounce_pre(mqrq);
2631}
2632
Adrian Hunter67716322011-08-29 16:42:15 +03002633static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2634 struct mmc_blk_request *brq, struct request *req,
2635 int ret)
2636{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002637 struct mmc_queue_req *mq_rq;
2638 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2639
Adrian Hunter67716322011-08-29 16:42:15 +03002640 /*
2641 * If this is an SD card and we're writing, we can first
2642 * mark the known good sectors as ok.
2643 *
2644 * If the card is not SD, we can still ok written sectors
2645 * as reported by the controller (which might be less than
2646 * the real number of written sectors, but never more).
2647 */
2648 if (mmc_card_sd(card)) {
2649 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302650 if (!brq->data.fault_injected) {
2651 blocks = mmc_sd_num_wr_blocks(card);
2652 if (blocks != (u32)-1)
2653 ret = blk_end_request(req, 0, blocks << 9);
2654 } else
2655 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002656 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002657 if (!mmc_packed_cmd(mq_rq->cmd_type))
2658 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002659 }
2660 return ret;
2661}
2662
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002663static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2664{
2665 struct request *prq;
2666 struct mmc_packed *packed = mq_rq->packed;
2667 int idx = packed->idx_failure, i = 0;
2668 int ret = 0;
2669
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002670 while (!list_empty(&packed->list)) {
2671 prq = list_entry_rq(packed->list.next);
2672 if (idx == i) {
2673 /* retry from error index */
2674 packed->nr_entries -= idx;
2675 mq_rq->req = prq;
2676 ret = 1;
2677
2678 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2679 list_del_init(&prq->queuelist);
2680 mmc_blk_clear_packed(mq_rq);
2681 }
2682 return ret;
2683 }
2684 list_del_init(&prq->queuelist);
2685 blk_end_request(prq, 0, blk_rq_bytes(prq));
2686 i++;
2687 }
2688
2689 mmc_blk_clear_packed(mq_rq);
2690 return ret;
2691}
2692
2693static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2694{
2695 struct request *prq;
2696 struct mmc_packed *packed = mq_rq->packed;
2697
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002698 while (!list_empty(&packed->list)) {
2699 prq = list_entry_rq(packed->list.next);
2700 list_del_init(&prq->queuelist);
2701 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2702 }
2703
2704 mmc_blk_clear_packed(mq_rq);
2705}
2706
2707static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2708 struct mmc_queue_req *mq_rq)
2709{
2710 struct request *prq;
2711 struct request_queue *q = mq->queue;
2712 struct mmc_packed *packed = mq_rq->packed;
2713
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002714 while (!list_empty(&packed->list)) {
2715 prq = list_entry_rq(packed->list.prev);
2716 if (prq->queuelist.prev != &packed->list) {
2717 list_del_init(&prq->queuelist);
2718 spin_lock_irq(q->queue_lock);
2719 blk_requeue_request(mq->queue, prq);
2720 spin_unlock_irq(q->queue_lock);
2721 } else {
2722 list_del_init(&prq->queuelist);
2723 }
2724 }
2725
2726 mmc_blk_clear_packed(mq_rq);
2727}
2728
Per Forlinee8a43a2011-07-01 18:55:33 +02002729static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02002730{
2731 struct mmc_blk_data *md = mq->data;
2732 struct mmc_card *card = md->queue.card;
2733 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002734 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002735 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002736 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302737 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02002738 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002739 const u8 packed_nr = 2;
2740 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002741#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2742 unsigned long waitfor = jiffies;
2743#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02002744
2745 if (!rqc && !mq->mqrq_prev->req)
2746 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002747
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002748 if (rqc)
2749 reqs = mmc_blk_prep_packed_list(mq, rqc);
2750
Per Forlin54d49d72011-07-01 18:55:29 +02002751 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002752 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302753 /*
2754 * When 4KB native sector is enabled, only 8 blocks
2755 * multiple read or write is allowed
2756 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00002757 if (mmc_large_sector(card) &&
2758 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302759 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
2760 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002761 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302762 goto cmd_abort;
2763 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002764
2765 if (reqs >= packed_nr)
2766 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2767 card, mq);
2768 else
2769 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002770 areq = &mq->mqrq_cur->mmc_active;
2771 } else
2772 areq = NULL;
2773 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002774 if (!areq) {
2775 if (status == MMC_BLK_NEW_REQUEST)
2776 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02002777 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002778 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002779
Per Forlinee8a43a2011-07-01 18:55:33 +02002780 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2781 brq = &mq_rq->brq;
2782 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002783 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002784 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002785
Per Forlind78d4a82011-07-01 18:55:30 +02002786 switch (status) {
2787 case MMC_BLK_SUCCESS:
2788 case MMC_BLK_PARTIAL:
2789 /*
2790 * A block was successfully transferred.
2791 */
Adrian Hunter67716322011-08-29 16:42:15 +03002792 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002793
Mark Salyzyn6904e432016-01-28 11:12:25 -08002794 mmc_blk_simulate_delay(mq, rqc, waitfor);
2795
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002796 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2797 ret = mmc_blk_end_packed_req(mq_rq);
2798 break;
2799 } else {
2800 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002801 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002802 }
2803
Adrian Hunter67716322011-08-29 16:42:15 +03002804 /*
2805 * If the blk_end_request function returns non-zero even
2806 * though all data has been transferred and no errors
2807 * were returned by the host controller, it's a bug.
2808 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002809 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302810 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002811 __func__, blk_rq_bytes(req),
2812 brq->data.bytes_xfered);
2813 rqc = NULL;
2814 goto cmd_abort;
2815 }
Per Forlind78d4a82011-07-01 18:55:30 +02002816 break;
2817 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002818 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002819 if (mmc_blk_reset(md, card->host, type))
2820 goto cmd_abort;
2821 if (!ret)
2822 goto start_new_req;
2823 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002824 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002825 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a82011-07-01 18:55:30 +02002826 if (retry++ < 5)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002827 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002828 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002829 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002830 if (!mmc_blk_reset(md, card->host, type))
2831 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002832 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002833 case MMC_BLK_DATA_ERR: {
2834 int err;
2835
2836 err = mmc_blk_reset(md, card->host, type);
2837 if (!err)
2838 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05302839 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002840 }
2841 case MMC_BLK_ECC_ERR:
2842 if (brq->data.blocks > 1) {
2843 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002844 pr_warn("%s: retrying using single block read\n",
2845 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002846 disable_multi = 1;
2847 break;
2848 }
Per Forlind78d4a82011-07-01 18:55:30 +02002849 /*
2850 * After an error, we redo I/O one sector at a
2851 * time, so we only reach here after trying to
2852 * read a single sector.
2853 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302854 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002855 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002856 if (!ret)
2857 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002858 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302859 case MMC_BLK_NOMEDIUM:
2860 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002861 default:
2862 pr_err("%s: Unhandled return value (%d)",
2863 req->rq_disk->disk_name, status);
2864 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002865 }
2866
Per Forlinee8a43a2011-07-01 18:55:33 +02002867 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002868 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2869 if (!mq_rq->packed->retries)
2870 goto cmd_abort;
2871 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2872 mmc_start_req(card->host,
2873 &mq_rq->mmc_active, NULL);
2874 } else {
2875
2876 /*
2877 * In case of a incomplete request
2878 * prepare it again and resend.
2879 */
2880 mmc_blk_rw_rq_prep(mq_rq, card,
2881 disable_multi, mq);
2882 mmc_start_req(card->host,
2883 &mq_rq->mmc_active, NULL);
2884 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002885 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 } while (ret);
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return 1;
2890
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002891 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002892 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2893 mmc_blk_abort_packed_req(mq_rq);
2894 } else {
2895 if (mmc_card_removed(card))
2896 req->cmd_flags |= REQ_QUIET;
2897 while (ret)
2898 ret = blk_end_request(req, -EIO,
2899 blk_rq_cur_bytes(req));
2900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Per Forlinee8a43a2011-07-01 18:55:33 +02002902 start_new_req:
2903 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002904 if (mmc_card_removed(card)) {
2905 rqc->cmd_flags |= REQ_QUIET;
2906 blk_end_request_all(rqc, -EIO);
2907 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002908 /*
2909 * If current request is packed, it needs to put back.
2910 */
2911 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2912 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2913
Seungwon Jeon7a819022013-01-22 19:48:07 +09002914 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2915 mmc_start_req(card->host,
2916 &mq->mqrq_cur->mmc_active, NULL);
2917 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002918 }
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return 0;
2921}
2922
Linus Walleij29eb7bd2016-09-20 11:34:38 +02002923int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002924{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002925 int ret;
2926 struct mmc_blk_data *md = mq->data;
2927 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002928 struct mmc_host *host = card->host;
2929 unsigned long flags;
Adrian Hunter869c5542016-08-25 14:11:43 -06002930 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002931
Per Forlinee8a43a2011-07-01 18:55:33 +02002932 if (req && !mq->mqrq_prev->req)
2933 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002934 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002935
Andrei Warkentin371a6892011-04-11 18:10:25 -05002936 ret = mmc_blk_part_switch(card, md);
2937 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002938 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302939 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002940 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002941 ret = 0;
2942 goto out;
2943 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002944
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002945 mmc_blk_write_packing_control(mq, req);
2946
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002947 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05002948 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002949 /* complete ongoing async transfer before issuing discard */
2950 if (card->host->areq)
2951 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02002952 ret = mmc_blk_issue_discard_rq(mq, req);
2953 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
2954 /* complete ongoing async transfer before issuing secure erase*/
2955 if (card->host->areq)
2956 mmc_blk_issue_rw_rq(mq, NULL);
2957 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05002958 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002959 /* complete ongoing async transfer before issuing flush */
2960 if (card->host->areq)
2961 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002962 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002963 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002964 if (!req && host->areq) {
2965 spin_lock_irqsave(&host->context_info.lock, flags);
2966 host->context_info.is_waiting_last_req = true;
2967 spin_unlock_irqrestore(&host->context_info.lock, flags);
2968 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002969 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002970 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002971
Andrei Warkentin371a6892011-04-11 18:10:25 -05002972out:
Adrian Hunter869c5542016-08-25 14:11:43 -06002973 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002974 /*
2975 * Release host when there are no more requests
2976 * and after special request(discard, flush) is done.
2977 * In case sepecial request, there is no reentry to
2978 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2979 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002980 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002981 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002982}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Russell Kinga6f6c962006-01-03 22:38:44 +00002984static inline int mmc_blk_readonly(struct mmc_card *card)
2985{
2986 return mmc_card_readonly(card) ||
2987 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2988}
2989
Andrei Warkentin371a6892011-04-11 18:10:25 -05002990static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2991 struct device *parent,
2992 sector_t size,
2993 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002994 const char *subname,
2995 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
2997 struct mmc_blk_data *md;
2998 int devidx, ret;
2999
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003000again:
3001 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
3002 return ERR_PTR(-ENOMEM);
3003
3004 spin_lock(&mmc_blk_lock);
3005 ret = ida_get_new(&mmc_blk_ida, &devidx);
3006 spin_unlock(&mmc_blk_lock);
3007
3008 if (ret == -EAGAIN)
3009 goto again;
3010 else if (ret)
3011 return ERR_PTR(ret);
3012
3013 if (devidx >= max_devices) {
3014 ret = -ENOSPC;
3015 goto out;
3016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003018 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00003019 if (!md) {
3020 ret = -ENOMEM;
3021 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 }
Russell Kinga6f6c962006-01-03 22:38:44 +00003023
Johan Rudholmadd710e2011-12-02 08:51:06 +01003024 md->area_type = area_type;
3025
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003026 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00003027 * Set the read-only status based on the supported commands
3028 * and the write protect switch.
3029 */
3030 md->read_only = mmc_blk_readonly(card);
3031
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003032 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00003033 if (md->disk == NULL) {
3034 ret = -ENOMEM;
3035 goto err_kfree;
3036 }
3037
3038 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003039 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00003040 md->usage = 1;
3041
Adrian Hunterd09408a2011-06-23 13:40:28 +03003042 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00003043 if (ret)
3044 goto err_putdisk;
3045
Russell Kinga6f6c962006-01-03 22:38:44 +00003046 md->queue.data = md;
3047
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003048 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003049 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00003050 md->disk->fops = &mmc_bdops;
3051 md->disk->private_data = md;
3052 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07003053 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003054 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07003055 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02003056 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02003057 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00003058
3059 /*
3060 * As discussed on lkml, GENHD_FL_REMOVABLE should:
3061 *
3062 * - be set for removable media with permanent block devices
3063 * - be unset for removable block devices with permanent media
3064 *
3065 * Since MMC block devices clearly fall under the second
3066 * case, we do not set GENHD_FL_REMOVABLE. Userspace
3067 * should use the block device creation/destruction hotplug
3068 * messages to tell when the card is present.
3069 */
3070
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003071 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02003072 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00003073
Saugata Dasa5075eb2012-05-17 16:32:21 +05303074 if (mmc_card_mmc(card))
3075 blk_queue_logical_block_size(md->queue.queue,
3076 card->ext_csd.data_sector_size);
3077 else
3078 blk_queue_logical_block_size(md->queue.queue, 512);
3079
Andrei Warkentin371a6892011-04-11 18:10:25 -05003080 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003081
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003082 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02003083 if ((mmc_card_mmc(card) &&
3084 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003085 (mmc_card_sd(card) &&
3086 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
3087 md->flags |= MMC_BLK_CMD23;
3088 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003089
3090 if (mmc_card_mmc(card) &&
3091 md->flags & MMC_BLK_CMD23 &&
3092 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
3093 card->ext_csd.rel_sectors)) {
3094 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06003095 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003096 }
3097
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003098 if (mmc_card_mmc(card) &&
3099 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
3100 (md->flags & MMC_BLK_CMD23) &&
3101 card->ext_csd.packed_event_en) {
3102 if (!mmc_packed_init(&md->queue, card))
3103 md->flags |= MMC_BLK_PACKED_CMD;
3104 }
3105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00003107
3108 err_putdisk:
3109 put_disk(md->disk);
3110 err_kfree:
3111 kfree(md);
3112 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003113 spin_lock(&mmc_blk_lock);
3114 ida_remove(&mmc_blk_ida, devidx);
3115 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00003116 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117}
3118
Andrei Warkentin371a6892011-04-11 18:10:25 -05003119static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
3120{
3121 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003122
3123 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
3124 /*
3125 * The EXT_CSD sector count is in number or 512 byte
3126 * sectors.
3127 */
3128 size = card->ext_csd.sectors;
3129 } else {
3130 /*
3131 * The CSD capacity field is in units of read_blkbits.
3132 * set_capacity takes units of 512 bytes.
3133 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00003134 size = (typeof(sector_t))card->csd.capacity
3135 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003136 }
3137
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01003138 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003139 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003140}
3141
3142static int mmc_blk_alloc_part(struct mmc_card *card,
3143 struct mmc_blk_data *md,
3144 unsigned int part_type,
3145 sector_t size,
3146 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003147 const char *subname,
3148 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05003149{
3150 char cap_str[10];
3151 struct mmc_blk_data *part_md;
3152
3153 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003154 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003155 if (IS_ERR(part_md))
3156 return PTR_ERR(part_md);
3157 part_md->part_type = part_type;
3158 list_add(&part_md->part, &md->part);
3159
James Bottomleyb9f28d82015-03-05 18:47:01 -08003160 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05003161 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303162 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05003163 part_md->disk->disk_name, mmc_card_id(card),
3164 mmc_card_name(card), part_md->part_type, cap_str);
3165 return 0;
3166}
3167
Namjae Jeone0c368d2011-10-06 23:41:38 +09003168/* MMC Physical partitions consist of two boot partitions and
3169 * up to four general purpose partitions.
3170 * For each partition enabled in EXT_CSD a block device will be allocatedi
3171 * to provide access to the partition.
3172 */
3173
Andrei Warkentin371a6892011-04-11 18:10:25 -05003174static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
3175{
Namjae Jeone0c368d2011-10-06 23:41:38 +09003176 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003177
3178 if (!mmc_card_mmc(card))
3179 return 0;
3180
Namjae Jeone0c368d2011-10-06 23:41:38 +09003181 for (idx = 0; idx < card->nr_parts; idx++) {
3182 if (card->part[idx].size) {
3183 ret = mmc_blk_alloc_part(card, md,
3184 card->part[idx].part_cfg,
3185 card->part[idx].size >> 9,
3186 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003187 card->part[idx].name,
3188 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09003189 if (ret)
3190 return ret;
3191 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003192 }
3193
3194 return ret;
3195}
3196
Andrei Warkentin371a6892011-04-11 18:10:25 -05003197static void mmc_blk_remove_req(struct mmc_blk_data *md)
3198{
Johan Rudholmadd710e2011-12-02 08:51:06 +01003199 struct mmc_card *card;
3200
Andrei Warkentin371a6892011-04-11 18:10:25 -05003201 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003202 /*
3203 * Flush remaining requests and free queues. It
3204 * is freeing the queue that stops new requests
3205 * from being accepted.
3206 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02003207 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003208 mmc_cleanup_queue(&md->queue);
3209 if (md->flags & MMC_BLK_PACKED_CMD)
3210 mmc_packed_clean(&md->queue);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003211 device_remove_file(disk_to_dev(md->disk),
3212 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003213 if (md->disk->flags & GENHD_FL_UP) {
3214 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003215 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3216 card->ext_csd.boot_ro_lockable)
3217 device_remove_file(disk_to_dev(md->disk),
3218 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08003219#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3220 device_remove_file(disk_to_dev(md->disk),
3221 &dev_attr_max_write_speed);
3222 device_remove_file(disk_to_dev(md->disk),
3223 &dev_attr_max_read_speed);
3224 device_remove_file(disk_to_dev(md->disk),
3225 &dev_attr_cache_size);
3226#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05003227
Andrei Warkentin371a6892011-04-11 18:10:25 -05003228 del_gendisk(md->disk);
3229 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003230 mmc_blk_put(md);
3231 }
3232}
3233
3234static void mmc_blk_remove_parts(struct mmc_card *card,
3235 struct mmc_blk_data *md)
3236{
3237 struct list_head *pos, *q;
3238 struct mmc_blk_data *part_md;
3239
3240 list_for_each_safe(pos, q, &md->part) {
3241 part_md = list_entry(pos, struct mmc_blk_data, part);
3242 list_del(pos);
3243 mmc_blk_remove_req(part_md);
3244 }
3245}
3246
3247static int mmc_add_disk(struct mmc_blk_data *md)
3248{
3249 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003250 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003251
Dan Williams307d8e62016-06-20 10:40:44 -07003252 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003253 md->force_ro.show = force_ro_show;
3254 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05303255 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003256 md->force_ro.attr.name = "force_ro";
3257 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
3258 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
3259 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01003260 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003261#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3262 atomic_set(&md->queue.max_write_speed, max_write_speed);
3263 ret = device_create_file(disk_to_dev(md->disk),
3264 &dev_attr_max_write_speed);
3265 if (ret)
3266 goto max_write_speed_fail;
3267 atomic_set(&md->queue.max_read_speed, max_read_speed);
3268 ret = device_create_file(disk_to_dev(md->disk),
3269 &dev_attr_max_read_speed);
3270 if (ret)
3271 goto max_read_speed_fail;
3272 atomic_set(&md->queue.cache_size, cache_size);
3273 atomic_long_set(&md->queue.cache_used, 0);
3274 md->queue.cache_jiffies = jiffies;
3275 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3276 if (ret)
3277 goto cache_size_fail;
3278#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003279
3280 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3281 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04003282 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003283
3284 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
3285 mode = S_IRUGO;
3286 else
3287 mode = S_IRUGO | S_IWUSR;
3288
3289 md->power_ro_lock.show = power_ro_lock_show;
3290 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01003291 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003292 md->power_ro_lock.attr.mode = mode;
3293 md->power_ro_lock.attr.name =
3294 "ro_lock_until_next_power_on";
3295 ret = device_create_file(disk_to_dev(md->disk),
3296 &md->power_ro_lock);
3297 if (ret)
3298 goto power_ro_lock_fail;
3299 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003300
3301 md->num_wr_reqs_to_start_packing.show =
3302 num_wr_reqs_to_start_packing_show;
3303 md->num_wr_reqs_to_start_packing.store =
3304 num_wr_reqs_to_start_packing_store;
3305 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
3306 md->num_wr_reqs_to_start_packing.attr.name =
3307 "num_wr_reqs_to_start_packing";
3308 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
3309 ret = device_create_file(disk_to_dev(md->disk),
3310 &md->num_wr_reqs_to_start_packing);
3311 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02003312 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003313
Maya Erez5a8dae12014-12-04 15:13:59 +02003314 md->no_pack_for_random.show = no_pack_for_random_show;
3315 md->no_pack_for_random.store = no_pack_for_random_store;
3316 sysfs_attr_init(&md->no_pack_for_random.attr);
3317 md->no_pack_for_random.attr.name = "no_pack_for_random";
3318 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
3319 ret = device_create_file(disk_to_dev(md->disk),
3320 &md->no_pack_for_random);
3321 if (ret)
3322 goto no_pack_for_random_fails;
3323
Johan Rudholmadd710e2011-12-02 08:51:06 +01003324 return ret;
3325
Maya Erez5a8dae12014-12-04 15:13:59 +02003326no_pack_for_random_fails:
3327 device_remove_file(disk_to_dev(md->disk),
3328 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02003329num_wr_reqs_to_start_packing_fail:
3330 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003331power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08003332#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3333 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3334cache_size_fail:
3335 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
3336max_read_speed_fail:
3337 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
3338max_write_speed_fail:
3339#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003340 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
3341force_ro_fail:
3342 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003343
3344 return ret;
3345}
3346
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003347static const struct mmc_fixup blk_fixups[] =
3348{
Chris Ballc59d4472011-11-11 22:01:43 -05003349 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
3350 MMC_QUIRK_INAND_CMD38),
3351 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
3352 MMC_QUIRK_INAND_CMD38),
3353 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
3354 MMC_QUIRK_INAND_CMD38),
3355 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
3356 MMC_QUIRK_INAND_CMD38),
3357 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
3358 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003359
3360 /*
3361 * Some MMC cards experience performance degradation with CMD23
3362 * instead of CMD12-bounded multiblock transfers. For now we'll
3363 * black list what's bad...
3364 * - Certain Toshiba cards.
3365 *
3366 * N.B. This doesn't affect SD cards.
3367 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08003368 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3369 MMC_QUIRK_BLK_NO_CMD23),
3370 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3371 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003372 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003373 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003374 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003375 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003376 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003377 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003378
3379 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03003380 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003381 */
Chris Ballc59d4472011-11-11 22:01:43 -05003382 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003383 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03003384 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
3385 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003386
Ian Chen3550ccd2012-08-29 15:05:36 +09003387 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08003388 * Some Samsung MMC cards need longer data read timeout than
3389 * indicated in CSD.
3390 */
3391 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
3392 MMC_QUIRK_LONG_READ_TIME),
3393
3394 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09003395 * On these Samsung MoviNAND parts, performing secure erase or
3396 * secure trim can result in unrecoverable corruption due to a
3397 * firmware bug.
3398 */
3399 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3400 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3401 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3402 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3403 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3404 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3405 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3406 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3407 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3408 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3409 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3410 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3411 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3412 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3413 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3414 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3415
Shawn Linb5b4ff02015-08-12 13:08:32 +08003416 /*
3417 * On Some Kingston eMMCs, performing trim can result in
3418 * unrecoverable data conrruption occasionally due to a firmware bug.
3419 */
3420 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3421 MMC_QUIRK_TRIM_BROKEN),
3422 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3423 MMC_QUIRK_TRIM_BROKEN),
3424
Pratibhasagar V8d664e32014-12-03 18:26:42 +02003425 /* Some INAND MCP devices advertise incorrect timeout values */
3426 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
3427 MMC_QUIRK_INAND_DATA_TIMEOUT),
3428
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003429 END_FIXUP
3430};
3431
Ulf Hansson96541ba2015-04-14 13:06:12 +02003432static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003434 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003435 char cap_str[10];
3436
Pierre Ossman912490d2005-05-21 10:27:02 +01003437 /*
3438 * Check that the card supports the command class(es) we need.
3439 */
3440 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 return -ENODEV;
3442
Lukas Czerner5204d002014-06-18 13:18:07 +02003443 mmc_fixup_device(card, blk_fixups);
3444
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 md = mmc_blk_alloc(card);
3446 if (IS_ERR(md))
3447 return PTR_ERR(md);
3448
James Bottomleyb9f28d82015-03-05 18:47:01 -08003449 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003450 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303451 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003453 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Andrei Warkentin371a6892011-04-11 18:10:25 -05003455 if (mmc_blk_alloc_parts(card, md))
3456 goto out;
3457
Ulf Hansson96541ba2015-04-14 13:06:12 +02003458 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003459
Andrei Warkentin371a6892011-04-11 18:10:25 -05003460 if (mmc_add_disk(md))
3461 goto out;
3462
3463 list_for_each_entry(part_md, &md->part, part) {
3464 if (mmc_add_disk(part_md))
3465 goto out;
3466 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003467
3468 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
3469 pm_runtime_use_autosuspend(&card->dev);
3470
3471 /*
3472 * Don't enable runtime PM for SD-combo cards here. Leave that
3473 * decision to be taken during the SDIO init sequence instead.
3474 */
3475 if (card->type != MMC_TYPE_SD_COMBO) {
3476 pm_runtime_set_active(&card->dev);
3477 pm_runtime_enable(&card->dev);
3478 }
3479
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 return 0;
3481
3482 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05003483 mmc_blk_remove_parts(card, md);
3484 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01003485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486}
3487
Ulf Hansson96541ba2015-04-14 13:06:12 +02003488static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003490 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
Andrei Warkentin371a6892011-04-11 18:10:25 -05003492 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003493 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003494 mmc_claim_host(card->host);
3495 mmc_blk_part_switch(card, md);
3496 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003497 if (card->type != MMC_TYPE_SD_COMBO)
3498 pm_runtime_disable(&card->dev);
3499 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003500 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02003501 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502}
3503
Ulf Hansson96541ba2015-04-14 13:06:12 +02003504static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003506 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02003507 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303508 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
3510 if (md) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05303511 rc = mmc_queue_suspend(&md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303512 if (rc)
3513 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003514 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05303515 rc = mmc_queue_suspend(&part_md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303516 if (rc)
3517 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303520 goto out;
3521
3522 out_resume:
3523 mmc_queue_resume(&md->queue);
3524 list_for_each_entry(part_md, &md->part, part) {
3525 mmc_queue_resume(&part_md->queue);
3526 }
3527 out:
3528 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529}
3530
Ulf Hansson96541ba2015-04-14 13:06:12 +02003531static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02003532{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003533 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003534}
3535
Ulf Hansson0967edc2014-10-06 11:29:42 +02003536#ifdef CONFIG_PM_SLEEP
3537static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02003538{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003539 struct mmc_card *card = mmc_dev_to_card(dev);
3540
3541 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003542}
3543
Ulf Hansson0967edc2014-10-06 11:29:42 +02003544static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003546 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02003547 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
3549 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003550 /*
3551 * Resume involves the card going into idle state,
3552 * so current partition is always the main one.
3553 */
3554 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003556 list_for_each_entry(part_md, &md->part, part) {
3557 mmc_queue_resume(&part_md->queue);
3558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 }
3560 return 0;
3561}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562#endif
3563
Ulf Hansson0967edc2014-10-06 11:29:42 +02003564static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3565
Ulf Hansson96541ba2015-04-14 13:06:12 +02003566static struct mmc_driver mmc_driver = {
3567 .drv = {
3568 .name = "mmcblk",
3569 .pm = &mmc_blk_pm_ops,
3570 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 .probe = mmc_blk_probe,
3572 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02003573 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574};
3575
3576static int __init mmc_blk_init(void)
3577{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003578 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003580 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3581 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3582
Ben Hutchingsa26eba62014-11-06 03:35:09 +00003583 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003584
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003585 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3586 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003589 res = mmc_register_driver(&mmc_driver);
3590 if (res)
3591 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003593 return 0;
3594 out2:
3595 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 out:
3597 return res;
3598}
3599
3600static void __exit mmc_blk_exit(void)
3601{
3602 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003603 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604}
3605
3606module_init(mmc_blk_init);
3607module_exit(mmc_blk_exit);
3608
3609MODULE_LICENSE("GPL");
3610MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3611