blob: a236425956422a6226e7aeba553a3ca55bcca786 [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
71
Tatyana Brokhman08238ce2012-10-07 10:33:13 +020072#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
73 do { \
74 if (stats->enabled) \
75 stats->pack_stop_reason[reason]++; \
76 } while (0)
77
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020078static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040079
80/*
81 * The defaults come from config options but can be overriden by module
82 * or bootarg options.
83 */
84static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
85
86/*
87 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000088 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020089 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040090 */
91static int max_devices;
92
Ben Hutchingsa26eba62014-11-06 03:35:09 +000093#define MAX_DEVICES 256
94
Ulf Hanssonb10fa992016-04-07 14:36:46 +020095static DEFINE_IDA(mmc_blk_ida);
96static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
99 * There is one mmc_blk_data per slot.
100 */
101struct mmc_blk_data {
102 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -0700103 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct gendisk *disk;
105 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500106 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500108 unsigned int flags;
109#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
110#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900111#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000114 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500115 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300116 unsigned int reset_done;
117#define MMC_BLK_READ BIT(0)
118#define MMC_BLK_WRITE BIT(1)
119#define MMC_BLK_DISCARD BIT(2)
120#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500121
122 /*
123 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200124 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500125 * track of the current selected device partition.
126 */
127 unsigned int part_curr;
128 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100129 struct device_attribute power_ro_lock;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200130 struct device_attribute num_wr_reqs_to_start_packing;
Maya Erez5a8dae12014-12-04 15:13:59 +0200131 struct device_attribute no_pack_for_random;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100132 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
Arjan van de Vena621aae2006-01-12 18:43:35 +0000135static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900137enum {
138 MMC_PACKED_NR_IDX = -1,
139 MMC_PACKED_NR_ZERO,
140 MMC_PACKED_NR_SINGLE,
141};
142
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400143module_param(perdev_minors, int, 0444);
144MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
145
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200146static inline int mmc_blk_part_switch(struct mmc_card *card,
147 struct mmc_blk_data *md);
148static int get_card_status(struct mmc_card *card, u32 *status, int retries);
149
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900150static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
151{
152 struct mmc_packed *packed = mqrq->packed;
153
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900154 mqrq->cmd_type = MMC_PACKED_NONE;
155 packed->nr_entries = MMC_PACKED_NR_ZERO;
156 packed->idx_failure = MMC_PACKED_NR_IDX;
157 packed->retries = 0;
158 packed->blocks = 0;
159}
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
162{
163 struct mmc_blk_data *md;
164
Arjan van de Vena621aae2006-01-12 18:43:35 +0000165 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 md = disk->private_data;
167 if (md && md->usage == 0)
168 md = NULL;
169 if (md)
170 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000171 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 return md;
174}
175
Andrei Warkentin371a6892011-04-11 18:10:25 -0500176static inline int mmc_get_devidx(struct gendisk *disk)
177{
Colin Cross382c55f2015-10-22 10:00:41 -0700178 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500179 return devidx;
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static void mmc_blk_put(struct mmc_blk_data *md)
183{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000184 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 md->usage--;
186 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500187 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800188 blk_cleanup_queue(md->queue.queue);
189
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200190 spin_lock(&mmc_blk_lock);
191 ida_remove(&mmc_blk_ida, devidx);
192 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 kfree(md);
196 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000197 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Johan Rudholmadd710e2011-12-02 08:51:06 +0100200static ssize_t power_ro_lock_show(struct device *dev,
201 struct device_attribute *attr, char *buf)
202{
203 int ret;
204 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
205 struct mmc_card *card = md->queue.card;
206 int locked = 0;
207
208 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
209 locked = 2;
210 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
211 locked = 1;
212
213 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
214
Tomas Winkler9098f842015-07-16 15:50:45 +0200215 mmc_blk_put(md);
216
Johan Rudholmadd710e2011-12-02 08:51:06 +0100217 return ret;
218}
219
220static ssize_t power_ro_lock_store(struct device *dev,
221 struct device_attribute *attr, const char *buf, size_t count)
222{
223 int ret;
224 struct mmc_blk_data *md, *part_md;
225 struct mmc_card *card;
226 unsigned long set;
227
228 if (kstrtoul(buf, 0, &set))
229 return -EINVAL;
230
231 if (set != 1)
232 return count;
233
234 md = mmc_blk_get(dev_to_disk(dev));
235 card = md->queue.card;
236
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200237 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100238
239 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
240 card->ext_csd.boot_ro_lock |
241 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
242 card->ext_csd.part_time);
243 if (ret)
244 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
245 else
246 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
247
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200248 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100249
250 if (!ret) {
251 pr_info("%s: Locking boot partition ro until next power on\n",
252 md->disk->disk_name);
253 set_disk_ro(md->disk, 1);
254
255 list_for_each_entry(part_md, &md->part, part)
256 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
257 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
258 set_disk_ro(part_md->disk, 1);
259 }
260 }
261
262 mmc_blk_put(md);
263 return count;
264}
265
Andrei Warkentin371a6892011-04-11 18:10:25 -0500266static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
267 char *buf)
268{
269 int ret;
270 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
271
Baruch Siach0031a982014-09-22 10:12:51 +0300272 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500273 get_disk_ro(dev_to_disk(dev)) ^
274 md->read_only);
275 mmc_blk_put(md);
276 return ret;
277}
278
279static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
280 const char *buf, size_t count)
281{
282 int ret;
283 char *end;
284 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
285 unsigned long set = simple_strtoul(buf, &end, 0);
286 if (end == buf) {
287 ret = -EINVAL;
288 goto out;
289 }
290
291 set_disk_ro(dev_to_disk(dev), set || md->read_only);
292 ret = count;
293out:
294 mmc_blk_put(md);
295 return ret;
296}
297
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200298static ssize_t
Maya Erez5a8dae12014-12-04 15:13:59 +0200299no_pack_for_random_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
301{
302 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
303 int ret;
304
305 ret = snprintf(buf, PAGE_SIZE, "%d\n", md->queue.no_pack_for_random);
306
307 mmc_blk_put(md);
308 return ret;
309}
310
311static ssize_t
312no_pack_for_random_store(struct device *dev,
313 struct device_attribute *attr,
314 const char *buf, size_t count)
315{
316 int value;
317 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
318 struct mmc_card *card = md->queue.card;
319 int ret = count;
320
321 if (!card) {
322 ret = -EINVAL;
323 goto exit;
324 }
325
326 sscanf(buf, "%d", &value);
327
328 if (value < 0) {
329 pr_err("%s: value %d is not valid. old value remains = %d",
330 mmc_hostname(card->host), value,
331 md->queue.no_pack_for_random);
332 ret = -EINVAL;
333 goto exit;
334 }
335
336 md->queue.no_pack_for_random = (value > 0) ? true : false;
337
338 pr_debug("%s: no_pack_for_random: new value = %d",
339 mmc_hostname(card->host),
340 md->queue.no_pack_for_random);
341
342exit:
343 mmc_blk_put(md);
344 return ret;
345}
346
347static ssize_t
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200348num_wr_reqs_to_start_packing_show(struct device *dev,
349 struct device_attribute *attr, char *buf)
350{
351 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
352 int num_wr_reqs_to_start_packing;
353 int ret;
354
355 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
356
357 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
358
359 mmc_blk_put(md);
360 return ret;
361}
362
363static ssize_t
364num_wr_reqs_to_start_packing_store(struct device *dev,
365 struct device_attribute *attr,
366 const char *buf, size_t count)
367{
368 int value;
369 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Yaniv Gardi42399822014-12-04 00:26:23 +0200370 struct mmc_card *card = md->queue.card;
371 int ret = count;
372
373 if (!card) {
374 ret = -EINVAL;
375 goto exit;
376 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200377
378 sscanf(buf, "%d", &value);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200379
Yaniv Gardi42399822014-12-04 00:26:23 +0200380 if (value >= 0) {
381 md->queue.num_wr_reqs_to_start_packing =
382 min_t(int, value, (int)card->ext_csd.max_packed_writes);
383
384 pr_debug("%s: trigger to pack: new value = %d",
385 mmc_hostname(card->host),
386 md->queue.num_wr_reqs_to_start_packing);
387 } else {
388 pr_err("%s: value %d is not valid. old value remains = %d",
389 mmc_hostname(card->host), value,
390 md->queue.num_wr_reqs_to_start_packing);
391 ret = -EINVAL;
392 }
393
394exit:
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200395 mmc_blk_put(md);
Yaniv Gardi42399822014-12-04 00:26:23 +0200396 return ret;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200397}
398
Mark Salyzyn6904e432016-01-28 11:12:25 -0800399#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
400
401static int max_read_speed, max_write_speed, cache_size = 4;
402
403module_param(max_read_speed, int, S_IRUSR | S_IRGRP);
404MODULE_PARM_DESC(max_read_speed, "maximum KB/s read speed 0=off");
405module_param(max_write_speed, int, S_IRUSR | S_IRGRP);
406MODULE_PARM_DESC(max_write_speed, "maximum KB/s write speed 0=off");
407module_param(cache_size, int, S_IRUSR | S_IRGRP);
408MODULE_PARM_DESC(cache_size, "MB high speed memory or SLC cache");
409
410/*
411 * helper macros and expectations:
412 * size - unsigned long number of bytes
413 * jiffies - unsigned long HZ timestamp difference
414 * speed - unsigned KB/s transfer rate
415 */
416#define size_and_speed_to_jiffies(size, speed) \
417 ((size) * HZ / (speed) / 1024UL)
418#define jiffies_and_speed_to_size(jiffies, speed) \
419 (((speed) * (jiffies) * 1024UL) / HZ)
420#define jiffies_and_size_to_speed(jiffies, size) \
421 ((size) * HZ / (jiffies) / 1024UL)
422
423/* Limits to report warning */
424/* jiffies_and_size_to_speed(10*HZ, queue_max_hw_sectors(q) * 512UL) ~ 25 */
425#define MIN_SPEED(q) 250 /* 10 times faster than a floppy disk */
426#define MAX_SPEED(q) jiffies_and_size_to_speed(1, queue_max_sectors(q) * 512UL)
427
428#define speed_valid(speed) ((speed) > 0)
429
430static const char off[] = "off\n";
431
432static int max_speed_show(int speed, char *buf)
433{
434 if (speed)
435 return scnprintf(buf, PAGE_SIZE, "%uKB/s\n", speed);
436 else
437 return scnprintf(buf, PAGE_SIZE, off);
438}
439
440static int max_speed_store(const char *buf, struct request_queue *q)
441{
442 unsigned int limit, set = 0;
443
444 if (!strncasecmp(off, buf, sizeof(off) - 2))
445 return set;
446 if (kstrtouint(buf, 0, &set) || (set > INT_MAX))
447 return -EINVAL;
448 if (set == 0)
449 return set;
450 limit = MAX_SPEED(q);
451 if (set > limit)
452 pr_warn("max speed %u ineffective above %u\n", set, limit);
453 limit = MIN_SPEED(q);
454 if (set < limit)
455 pr_warn("max speed %u painful below %u\n", set, limit);
456 return set;
457}
458
459static ssize_t max_write_speed_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
461{
462 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
463 int ret = max_speed_show(atomic_read(&md->queue.max_write_speed), buf);
464
465 mmc_blk_put(md);
466 return ret;
467}
468
469static ssize_t max_write_speed_store(struct device *dev,
470 struct device_attribute *attr,
471 const char *buf, size_t count)
472{
473 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
474 int set = max_speed_store(buf, md->queue.queue);
475
476 if (set < 0) {
477 mmc_blk_put(md);
478 return set;
479 }
480
481 atomic_set(&md->queue.max_write_speed, set);
482 mmc_blk_put(md);
483 return count;
484}
485
486static const DEVICE_ATTR(max_write_speed, S_IRUGO | S_IWUSR,
487 max_write_speed_show, max_write_speed_store);
488
489static ssize_t max_read_speed_show(struct device *dev,
490 struct device_attribute *attr, char *buf)
491{
492 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
493 int ret = max_speed_show(atomic_read(&md->queue.max_read_speed), buf);
494
495 mmc_blk_put(md);
496 return ret;
497}
498
499static ssize_t max_read_speed_store(struct device *dev,
500 struct device_attribute *attr,
501 const char *buf, size_t count)
502{
503 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
504 int set = max_speed_store(buf, md->queue.queue);
505
506 if (set < 0) {
507 mmc_blk_put(md);
508 return set;
509 }
510
511 atomic_set(&md->queue.max_read_speed, set);
512 mmc_blk_put(md);
513 return count;
514}
515
516static const DEVICE_ATTR(max_read_speed, S_IRUGO | S_IWUSR,
517 max_read_speed_show, max_read_speed_store);
518
519static ssize_t cache_size_show(struct device *dev,
520 struct device_attribute *attr, char *buf)
521{
522 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
523 struct mmc_queue *mq = &md->queue;
524 int cache_size = atomic_read(&mq->cache_size);
525 int ret;
526
527 if (!cache_size)
528 ret = scnprintf(buf, PAGE_SIZE, off);
529 else {
530 int speed = atomic_read(&mq->max_write_speed);
531
532 if (!speed_valid(speed))
533 ret = scnprintf(buf, PAGE_SIZE, "%uMB\n", cache_size);
534 else { /* We accept race between cache_jiffies and cache_used */
535 unsigned long size = jiffies_and_speed_to_size(
536 jiffies - mq->cache_jiffies, speed);
537 long used = atomic_long_read(&mq->cache_used);
538
539 if (size >= used)
540 size = 0;
541 else
542 size = (used - size) * 100 / cache_size
543 / 1024UL / 1024UL;
544
545 ret = scnprintf(buf, PAGE_SIZE, "%uMB %lu%% used\n",
546 cache_size, size);
547 }
548 }
549
550 mmc_blk_put(md);
551 return ret;
552}
553
554static ssize_t cache_size_store(struct device *dev,
555 struct device_attribute *attr,
556 const char *buf, size_t count)
557{
558 struct mmc_blk_data *md;
559 unsigned int set = 0;
560
561 if (strncasecmp(off, buf, sizeof(off) - 2)
562 && (kstrtouint(buf, 0, &set) || (set > INT_MAX)))
563 return -EINVAL;
564
565 md = mmc_blk_get(dev_to_disk(dev));
566 atomic_set(&md->queue.cache_size, set);
567 mmc_blk_put(md);
568 return count;
569}
570
571static const DEVICE_ATTR(cache_size, S_IRUGO | S_IWUSR,
572 cache_size_show, cache_size_store);
573
574/* correct for write-back */
575static long mmc_blk_cache_used(struct mmc_queue *mq, unsigned long waitfor)
576{
577 long used = 0;
578 int speed = atomic_read(&mq->max_write_speed);
579
580 if (speed_valid(speed)) {
581 unsigned long size = jiffies_and_speed_to_size(
582 waitfor - mq->cache_jiffies, speed);
583 used = atomic_long_read(&mq->cache_used);
584
585 if (size >= used)
586 used = 0;
587 else
588 used -= size;
589 }
590
591 atomic_long_set(&mq->cache_used, used);
592 mq->cache_jiffies = waitfor;
593
594 return used;
595}
596
597static void mmc_blk_simulate_delay(
598 struct mmc_queue *mq,
599 struct request *req,
600 unsigned long waitfor)
601{
602 int max_speed;
603
604 if (!req)
605 return;
606
607 max_speed = (rq_data_dir(req) == READ)
608 ? atomic_read(&mq->max_read_speed)
609 : atomic_read(&mq->max_write_speed);
610 if (speed_valid(max_speed)) {
611 unsigned long bytes = blk_rq_bytes(req);
612
613 if (rq_data_dir(req) != READ) {
614 int cache_size = atomic_read(&mq->cache_size);
615
616 if (cache_size) {
617 unsigned long size = cache_size * 1024L * 1024L;
618 long used = mmc_blk_cache_used(mq, waitfor);
619
620 used += bytes;
621 atomic_long_set(&mq->cache_used, used);
622 bytes = 0;
623 if (used > size)
624 bytes = used - size;
625 }
626 }
627 waitfor += size_and_speed_to_jiffies(bytes, max_speed);
628 if (time_is_after_jiffies(waitfor)) {
629 long msecs = jiffies_to_msecs(waitfor - jiffies);
630
631 if (likely(msecs > 0))
632 msleep(msecs);
633 }
634 }
635}
636
637#else
638
639#define mmc_blk_simulate_delay(mq, req, waitfor)
640
641#endif
642
Al Viroa5a15612008-03-02 10:33:30 -0500643static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Al Viroa5a15612008-03-02 10:33:30 -0500645 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int ret = -ENXIO;
647
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200648 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (md) {
650 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500651 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700653
Al Viroa5a15612008-03-02 10:33:30 -0500654 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700655 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700656 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200659 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 return ret;
662}
663
Al Virodb2a1442013-05-05 21:52:57 -0400664static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Al Viroa5a15612008-03-02 10:33:30 -0500666 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200668 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200670 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800674mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800676 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
677 geo->heads = 4;
678 geo->sectors = 16;
679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
John Calixtocb87ea22011-04-26 18:56:29 -0400682struct mmc_blk_ioc_data {
683 struct mmc_ioc_cmd ic;
684 unsigned char *buf;
685 u64 buf_bytes;
686};
687
688static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
689 struct mmc_ioc_cmd __user *user)
690{
691 struct mmc_blk_ioc_data *idata;
692 int err;
693
yalin wang1ff89502015-11-12 19:27:11 +0800694 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400695 if (!idata) {
696 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400697 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400698 }
699
700 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
701 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400702 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400703 }
704
705 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
706 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
707 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400708 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400709 }
710
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300711 if (!idata->buf_bytes) {
712 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100713 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300714 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100715
yalin wang1ff89502015-11-12 19:27:11 +0800716 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400717 if (!idata->buf) {
718 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400719 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400720 }
721
722 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
723 idata->ic.data_ptr, idata->buf_bytes)) {
724 err = -EFAULT;
725 goto copy_err;
726 }
727
728 return idata;
729
730copy_err:
731 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400732idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400733 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400734out:
John Calixtocb87ea22011-04-26 18:56:29 -0400735 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400736}
737
Jon Huntera5f57742015-09-22 10:27:53 +0100738static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
739 struct mmc_blk_ioc_data *idata)
740{
741 struct mmc_ioc_cmd *ic = &idata->ic;
742
743 if (copy_to_user(&(ic_ptr->response), ic->response,
744 sizeof(ic->response)))
745 return -EFAULT;
746
747 if (!idata->ic.write_flag) {
748 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
749 idata->buf, idata->buf_bytes))
750 return -EFAULT;
751 }
752
753 return 0;
754}
755
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200756static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
757 u32 retries_max)
758{
759 int err;
760 u32 retry_count = 0;
761
762 if (!status || !retries_max)
763 return -EINVAL;
764
765 do {
766 err = get_card_status(card, status, 5);
767 if (err)
768 break;
769
770 if (!R1_STATUS(*status) &&
771 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
772 break; /* RPMB programming operation complete */
773
774 /*
775 * Rechedule to give the MMC device a chance to continue
776 * processing the previous command without being polled too
777 * frequently.
778 */
779 usleep_range(1000, 5000);
780 } while (++retry_count < retries_max);
781
782 if (retry_count == retries_max)
783 err = -EPERM;
784
785 return err;
786}
787
Maya Erez775a9362013-04-18 15:41:55 +0300788static int ioctl_do_sanitize(struct mmc_card *card)
789{
790 int err;
791
Ulf Hanssona2d10862013-12-16 14:37:26 +0100792 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300793 pr_warn("%s: %s - SANITIZE is not supported\n",
794 mmc_hostname(card->host), __func__);
795 err = -EOPNOTSUPP;
796 goto out;
797 }
798
799 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
800 mmc_hostname(card->host), __func__);
801
802 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
803 EXT_CSD_SANITIZE_START, 1,
804 MMC_SANITIZE_REQ_TIMEOUT);
805
806 if (err)
807 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
808 mmc_hostname(card->host), __func__, err);
809
810 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
811 __func__);
812out:
813 return err;
814}
815
Jon Huntera5f57742015-09-22 10:27:53 +0100816static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
817 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400818{
John Calixtocb87ea22011-04-26 18:56:29 -0400819 struct mmc_command cmd = {0};
820 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530821 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400822 struct scatterlist sg;
823 int err;
824
Jon Huntera5f57742015-09-22 10:27:53 +0100825 if (!card || !md || !idata)
826 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400827
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100828 cmd.opcode = idata->ic.opcode;
829 cmd.arg = idata->ic.arg;
830 cmd.flags = idata->ic.flags;
831
832 if (idata->buf_bytes) {
833 data.sg = &sg;
834 data.sg_len = 1;
835 data.blksz = idata->ic.blksz;
836 data.blocks = idata->ic.blocks;
837
838 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
839
840 if (idata->ic.write_flag)
841 data.flags = MMC_DATA_WRITE;
842 else
843 data.flags = MMC_DATA_READ;
844
845 /* data.flags must already be set before doing this. */
846 mmc_set_data_timeout(&data, card);
847
848 /* Allow overriding the timeout_ns for empirical tuning. */
849 if (idata->ic.data_timeout_ns)
850 data.timeout_ns = idata->ic.data_timeout_ns;
851
852 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
853 /*
854 * Pretend this is a data transfer and rely on the
855 * host driver to compute timeout. When all host
856 * drivers support cmd.cmd_timeout for R1B, this
857 * can be changed to:
858 *
859 * mrq.data = NULL;
860 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
861 */
862 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
863 }
864
865 mrq.data = &data;
866 }
867
868 mrq.cmd = &cmd;
869
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200870 err = mmc_blk_part_switch(card, md);
871 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100872 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200873
John Calixtocb87ea22011-04-26 18:56:29 -0400874 if (idata->ic.is_acmd) {
875 err = mmc_app_cmd(card->host, card);
876 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100877 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400878 }
879
Yaniv Gardia82e4842013-06-05 14:13:08 +0300880 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
881 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300882 err = ioctl_do_sanitize(card);
883
884 if (err)
885 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
886 __func__, err);
887
Jon Huntera5f57742015-09-22 10:27:53 +0100888 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300889 }
890
John Calixtocb87ea22011-04-26 18:56:29 -0400891 mmc_wait_for_req(card->host, &mrq);
892
893 if (cmd.error) {
894 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
895 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100896 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400897 }
898 if (data.error) {
899 dev_err(mmc_dev(card->host), "%s: data error %d\n",
900 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100901 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400902 }
903
904 /*
905 * According to the SD specs, some commands require a delay after
906 * issuing the command.
907 */
908 if (idata->ic.postsleep_min_us)
909 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
910
Jon Huntera5f57742015-09-22 10:27:53 +0100911 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400912
Krishna Kondae6711632014-12-04 15:20:57 +0200913 return err;
914}
915
916struct mmc_blk_ioc_rpmb_data {
917 struct mmc_blk_ioc_data *data[MMC_IOC_MAX_RPMB_CMD];
918};
919
920static struct mmc_blk_ioc_rpmb_data *mmc_blk_ioctl_rpmb_copy_from_user(
921 struct mmc_ioc_rpmb __user *user)
922{
923 struct mmc_blk_ioc_rpmb_data *idata;
924 int err, i;
925
926 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
927 if (!idata) {
928 err = -ENOMEM;
929 goto out;
930 }
931
932 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
933 idata->data[i] = mmc_blk_ioctl_copy_from_user(&(user->cmds[i]));
934 if (IS_ERR(idata->data[i])) {
935 err = PTR_ERR(idata->data[i]);
936 goto copy_err;
937 }
938 }
939
940 return idata;
941
942copy_err:
943 while (--i >= 0) {
944 kfree(idata->data[i]->buf);
945 kfree(idata->data[i]);
946 }
947 kfree(idata);
948out:
949 return ERR_PTR(err);
950}
951
952static int mmc_blk_ioctl_rpmb_cmd(struct block_device *bdev,
953 struct mmc_ioc_rpmb __user *ic_ptr)
954{
955 struct mmc_blk_ioc_rpmb_data *idata;
956 struct mmc_blk_data *md;
957 struct mmc_card *card;
958 struct mmc_command cmd = {0};
959 struct mmc_data data = {0};
960 struct mmc_request mrq = {NULL};
961 struct scatterlist sg;
962 int err = 0, i = 0;
963 u32 status = 0;
964
965 /* The caller must have CAP_SYS_RAWIO */
966 if (!capable(CAP_SYS_RAWIO))
967 return -EPERM;
968
969 md = mmc_blk_get(bdev->bd_disk);
970 /* make sure this is a rpmb partition */
971 if ((!md) || (!(md->area_type & MMC_BLK_DATA_AREA_RPMB))) {
972 err = -EINVAL;
973 goto cmd_done;
974 }
975
976 idata = mmc_blk_ioctl_rpmb_copy_from_user(ic_ptr);
977 if (IS_ERR(idata)) {
978 err = PTR_ERR(idata);
979 goto cmd_done;
980 }
981
982 card = md->queue.card;
983 if (IS_ERR(card)) {
984 err = PTR_ERR(card);
985 goto idata_free;
986 }
987
988 mmc_claim_host(card->host);
989
990 err = mmc_blk_part_switch(card, md);
991 if (err)
992 goto cmd_rel_host;
993
994 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
995 struct mmc_blk_ioc_data *curr_data;
996 struct mmc_ioc_cmd *curr_cmd;
997
998 curr_data = idata->data[i];
999 curr_cmd = &curr_data->ic;
1000 if (!curr_cmd->opcode)
1001 break;
1002
1003 cmd.opcode = curr_cmd->opcode;
1004 cmd.arg = curr_cmd->arg;
1005 cmd.flags = curr_cmd->flags;
1006
1007 if (curr_data->buf_bytes) {
1008 data.sg = &sg;
1009 data.sg_len = 1;
1010 data.blksz = curr_cmd->blksz;
1011 data.blocks = curr_cmd->blocks;
1012
1013 sg_init_one(data.sg, curr_data->buf,
1014 curr_data->buf_bytes);
1015
1016 if (curr_cmd->write_flag)
1017 data.flags = MMC_DATA_WRITE;
1018 else
1019 data.flags = MMC_DATA_READ;
1020
1021 /* data.flags must already be set before doing this. */
1022 mmc_set_data_timeout(&data, card);
1023
1024 /*
1025 * Allow overriding the timeout_ns for empirical tuning.
1026 */
1027 if (curr_cmd->data_timeout_ns)
1028 data.timeout_ns = curr_cmd->data_timeout_ns;
1029
1030 mrq.data = &data;
1031 }
1032
1033 mrq.cmd = &cmd;
1034
1035 err = mmc_set_blockcount(card, data.blocks,
1036 curr_cmd->write_flag & (1 << 31));
1037 if (err)
1038 goto cmd_rel_host;
1039
1040 mmc_wait_for_req(card->host, &mrq);
1041
1042 if (cmd.error) {
1043 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
1044 __func__, cmd.error);
1045 err = cmd.error;
1046 goto cmd_rel_host;
1047 }
1048 if (data.error) {
1049 dev_err(mmc_dev(card->host), "%s: data error %d\n",
1050 __func__, data.error);
1051 err = data.error;
1052 goto cmd_rel_host;
1053 }
1054
1055 if (copy_to_user(&(ic_ptr->cmds[i].response), cmd.resp,
1056 sizeof(cmd.resp))) {
1057 err = -EFAULT;
1058 goto cmd_rel_host;
1059 }
1060
1061 if (!curr_cmd->write_flag) {
1062 if (copy_to_user((void __user *)(unsigned long)
1063 curr_cmd->data_ptr,
1064 curr_data->buf,
1065 curr_data->buf_bytes)) {
1066 err = -EFAULT;
1067 goto cmd_rel_host;
1068 }
1069 }
1070
Loic Pallardy8d1e9772012-08-06 17:12:31 +02001071 /*
1072 * Ensure RPMB command has completed by polling CMD13
1073 * "Send Status".
1074 */
1075 err = ioctl_rpmb_card_status_poll(card, &status, 5);
1076 if (err)
1077 dev_err(mmc_dev(card->host),
1078 "%s: Card Status=0x%08X, error %d\n",
1079 __func__, status, err);
1080 }
1081
Krishna Kondae6711632014-12-04 15:20:57 +02001082cmd_rel_host:
1083 mmc_put_card(card);
1084
1085idata_free:
1086 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1087 kfree(idata->data[i]->buf);
1088 kfree(idata->data[i]);
1089 }
1090 kfree(idata);
1091
1092cmd_done:
1093 mmc_blk_put(md);
Jon Huntera5f57742015-09-22 10:27:53 +01001094 return err;
1095}
1096
1097static int mmc_blk_ioctl_cmd(struct block_device *bdev,
1098 struct mmc_ioc_cmd __user *ic_ptr)
1099{
1100 struct mmc_blk_ioc_data *idata;
1101 struct mmc_blk_data *md;
1102 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -07001103 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001104
Shawn Lin83c742c2016-03-16 18:15:47 +08001105 /*
1106 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1107 * whole block device, not on a partition. This prevents overspray
1108 * between sibling partitions.
1109 */
1110 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1111 return -EPERM;
1112
Jon Huntera5f57742015-09-22 10:27:53 +01001113 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
1114 if (IS_ERR(idata))
1115 return PTR_ERR(idata);
1116
1117 md = mmc_blk_get(bdev->bd_disk);
1118 if (!md) {
1119 err = -EINVAL;
1120 goto cmd_err;
1121 }
1122
1123 card = md->queue.card;
1124 if (IS_ERR(card)) {
1125 err = PTR_ERR(card);
1126 goto cmd_done;
1127 }
1128
1129 mmc_get_card(card);
1130
Grant Grundlerb0934102015-09-23 18:30:33 -07001131 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001132
Adrian Hunter3c866562016-05-04 14:38:12 +03001133 /* Always switch back to main area after RPMB access */
1134 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1135 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1136
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001137 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -04001138
Grant Grundlerb0934102015-09-23 18:30:33 -07001139 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001140
John Calixtocb87ea22011-04-26 18:56:29 -04001141cmd_done:
1142 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +03001143cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -04001144 kfree(idata->buf);
1145 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001146 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -04001147}
1148
Jon Huntera5f57742015-09-22 10:27:53 +01001149static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
1150 struct mmc_ioc_multi_cmd __user *user)
1151{
1152 struct mmc_blk_ioc_data **idata = NULL;
1153 struct mmc_ioc_cmd __user *cmds = user->cmds;
1154 struct mmc_card *card;
1155 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -07001156 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001157 __u64 num_of_cmds;
1158
Shawn Lin83c742c2016-03-16 18:15:47 +08001159 /*
1160 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1161 * whole block device, not on a partition. This prevents overspray
1162 * between sibling partitions.
1163 */
1164 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1165 return -EPERM;
1166
Jon Huntera5f57742015-09-22 10:27:53 +01001167 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
1168 sizeof(num_of_cmds)))
1169 return -EFAULT;
1170
1171 if (num_of_cmds > MMC_IOC_MAX_CMDS)
1172 return -EINVAL;
1173
1174 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
1175 if (!idata)
1176 return -ENOMEM;
1177
1178 for (i = 0; i < num_of_cmds; i++) {
1179 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
1180 if (IS_ERR(idata[i])) {
1181 err = PTR_ERR(idata[i]);
1182 num_of_cmds = i;
1183 goto cmd_err;
1184 }
1185 }
1186
1187 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -08001188 if (!md) {
1189 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +01001190 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -08001191 }
Jon Huntera5f57742015-09-22 10:27:53 +01001192
1193 card = md->queue.card;
1194 if (IS_ERR(card)) {
1195 err = PTR_ERR(card);
1196 goto cmd_done;
1197 }
1198
1199 mmc_get_card(card);
1200
Grant Grundlerb0934102015-09-23 18:30:33 -07001201 for (i = 0; i < num_of_cmds && !ioc_err; i++)
1202 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001203
Adrian Hunter3c866562016-05-04 14:38:12 +03001204 /* Always switch back to main area after RPMB access */
1205 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1206 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1207
Jon Huntera5f57742015-09-22 10:27:53 +01001208 mmc_put_card(card);
1209
1210 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -07001211 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +01001212 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001213
1214cmd_done:
1215 mmc_blk_put(md);
1216cmd_err:
1217 for (i = 0; i < num_of_cmds; i++) {
1218 kfree(idata[i]->buf);
1219 kfree(idata[i]);
1220 }
1221 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001222 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +01001223}
1224
John Calixtocb87ea22011-04-26 18:56:29 -04001225static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
1226 unsigned int cmd, unsigned long arg)
1227{
Jon Huntera5f57742015-09-22 10:27:53 +01001228 switch (cmd) {
1229 case MMC_IOC_CMD:
1230 return mmc_blk_ioctl_cmd(bdev,
1231 (struct mmc_ioc_cmd __user *)arg);
Krishna Kondae6711632014-12-04 15:20:57 +02001232 case MMC_IOC_RPMB_CMD:
1233 return mmc_blk_ioctl_rpmb_cmd(bdev,
1234 (struct mmc_ioc_rpmb __user *)arg);
Jon Huntera5f57742015-09-22 10:27:53 +01001235 case MMC_IOC_MULTI_CMD:
1236 return mmc_blk_ioctl_multi_cmd(bdev,
1237 (struct mmc_ioc_multi_cmd __user *)arg);
1238 default:
1239 return -EINVAL;
1240 }
John Calixtocb87ea22011-04-26 18:56:29 -04001241}
1242
1243#ifdef CONFIG_COMPAT
1244static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
1245 unsigned int cmd, unsigned long arg)
1246{
1247 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
1248}
1249#endif
1250
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001251static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -05001252 .open = mmc_blk_open,
1253 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001254 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -04001256 .ioctl = mmc_blk_ioctl,
1257#ifdef CONFIG_COMPAT
1258 .compat_ioctl = mmc_blk_compat_ioctl,
1259#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260};
1261
Andrei Warkentin371a6892011-04-11 18:10:25 -05001262static inline int mmc_blk_part_switch(struct mmc_card *card,
1263 struct mmc_blk_data *md)
1264{
1265 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001266 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001267
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001268 if ((main_md->part_curr == md->part_type) &&
1269 (card->part_curr == md->part_type))
Andrei Warkentin371a6892011-04-11 18:10:25 -05001270 return 0;
1271
1272 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001273 u8 part_config = card->ext_csd.part_config;
1274
Adrian Hunter57da0c02016-05-04 14:38:13 +03001275 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1276 mmc_retune_pause(card->host);
1277
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001278 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1279 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001280
1281 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001282 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001283 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001284 if (ret) {
1285 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1286 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001287 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001288 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001289
1290 card->ext_csd.part_config = part_config;
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001291 card->part_curr = md->part_type;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001292
1293 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1294 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001295 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001296
1297 main_md->part_curr = md->part_type;
1298 return 0;
1299}
1300
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001301static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1302{
1303 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001304 u32 result;
1305 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001306
Venkatraman Sad5fd972011-08-25 00:30:50 +05301307 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001308 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001309 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001310
1311 struct scatterlist sg;
1312
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001313 cmd.opcode = MMC_APP_CMD;
1314 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001315 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001316
1317 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001318 if (err)
1319 return (u32)-1;
1320 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001321 return (u32)-1;
1322
1323 memset(&cmd, 0, sizeof(struct mmc_command));
1324
1325 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1326 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001327 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001328
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001329 data.blksz = 4;
1330 data.blocks = 1;
1331 data.flags = MMC_DATA_READ;
1332 data.sg = &sg;
1333 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301334 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001335
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001336 mrq.cmd = &cmd;
1337 mrq.data = &data;
1338
Ben Dooks051913d2009-06-08 23:33:57 +01001339 blocks = kmalloc(4, GFP_KERNEL);
1340 if (!blocks)
1341 return (u32)-1;
1342
1343 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001344
1345 mmc_wait_for_req(card->host, &mrq);
1346
Ben Dooks051913d2009-06-08 23:33:57 +01001347 result = ntohl(*blocks);
1348 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001349
Ben Dooks051913d2009-06-08 23:33:57 +01001350 if (cmd.error || data.error)
1351 result = (u32)-1;
1352
1353 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001354}
1355
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001356static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001357{
Chris Ball1278dba2011-04-13 23:40:30 -04001358 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001359 int err;
1360
Adrian Hunter504f1912008-10-16 12:55:25 +03001361 cmd.opcode = MMC_SEND_STATUS;
1362 if (!mmc_host_is_spi(card->host))
1363 cmd.arg = card->rca << 16;
1364 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001365 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1366 if (err == 0)
1367 *status = cmd.resp[0];
1368 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001369}
1370
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001371static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001372 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001373{
1374 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1375 int err = 0;
1376 u32 status;
1377
1378 do {
1379 err = get_card_status(card, &status, 5);
1380 if (err) {
1381 pr_err("%s: error %d requesting status\n",
1382 req->rq_disk->disk_name, err);
1383 return err;
1384 }
1385
1386 if (status & R1_ERROR) {
1387 pr_err("%s: %s: error sending status cmd, status %#x\n",
1388 req->rq_disk->disk_name, __func__, status);
1389 *gen_err = 1;
1390 }
1391
Ulf Hansson95a91292014-01-29 13:11:27 +01001392 /* We may rely on the host hw to handle busy detection.*/
1393 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1394 hw_busy_detect)
1395 break;
1396
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001397 /*
1398 * Timeout if the device never becomes ready for data and never
1399 * leaves the program state.
1400 */
1401 if (time_after(jiffies, timeout)) {
1402 pr_err("%s: Card stuck in programming state! %s %s\n",
1403 mmc_hostname(card->host),
1404 req->rq_disk->disk_name, __func__);
1405 return -ETIMEDOUT;
1406 }
1407
1408 /*
1409 * Some cards mishandle the status bits,
1410 * so make sure to check both the busy
1411 * indication and the card state.
1412 */
1413 } while (!(status & R1_READY_FOR_DATA) ||
1414 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1415
1416 return err;
1417}
1418
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001419static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1420 struct request *req, int *gen_err, u32 *stop_status)
1421{
1422 struct mmc_host *host = card->host;
1423 struct mmc_command cmd = {0};
1424 int err;
1425 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1426
1427 /*
1428 * Normally we use R1B responses for WRITE, but in cases where the host
1429 * has specified a max_busy_timeout we need to validate it. A failure
1430 * means we need to prevent the host from doing hw busy detection, which
1431 * is done by converting to a R1 response instead.
1432 */
1433 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1434 use_r1b_resp = false;
1435
1436 cmd.opcode = MMC_STOP_TRANSMISSION;
1437 if (use_r1b_resp) {
1438 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1439 cmd.busy_timeout = timeout_ms;
1440 } else {
1441 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1442 }
1443
1444 err = mmc_wait_for_cmd(host, &cmd, 5);
1445 if (err)
1446 return err;
1447
1448 *stop_status = cmd.resp[0];
1449
1450 /* No need to check card status in case of READ. */
1451 if (rq_data_dir(req) == READ)
1452 return 0;
1453
1454 if (!mmc_host_is_spi(host) &&
1455 (*stop_status & R1_ERROR)) {
1456 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1457 req->rq_disk->disk_name, __func__, *stop_status);
1458 *gen_err = 1;
1459 }
1460
1461 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1462}
1463
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301464#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001465#define ERR_RETRY 2
1466#define ERR_ABORT 1
1467#define ERR_CONTINUE 0
1468
1469static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1470 bool status_valid, u32 status)
1471{
1472 switch (error) {
1473 case -EILSEQ:
1474 /* response crc error, retry the r/w cmd */
1475 pr_err("%s: %s sending %s command, card status %#x\n",
1476 req->rq_disk->disk_name, "response CRC error",
1477 name, status);
1478 return ERR_RETRY;
1479
1480 case -ETIMEDOUT:
1481 pr_err("%s: %s sending %s command, card status %#x\n",
1482 req->rq_disk->disk_name, "timed out", name, status);
1483
1484 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301485 if (!status_valid) {
1486 pr_err("%s: status not valid, retrying timeout\n",
1487 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001488 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301489 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001490
1491 /*
1492 * If it was a r/w cmd crc error, or illegal command
1493 * (eg, issued in wrong state) then retry - we should
1494 * have corrected the state problem above.
1495 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301496 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1497 pr_err("%s: command error, retrying timeout\n",
1498 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001499 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301500 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001501
1502 /* Otherwise abort the command */
1503 return ERR_ABORT;
1504
1505 default:
1506 /* We don't understand the error code the driver gave us */
1507 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1508 req->rq_disk->disk_name, error, status);
1509 return ERR_ABORT;
1510 }
1511}
1512
1513/*
1514 * Initial r/w and stop cmd error recovery.
1515 * We don't know whether the card received the r/w cmd or not, so try to
1516 * restore things back to a sane state. Essentially, we do this as follows:
1517 * - Obtain card status. If the first attempt to obtain card status fails,
1518 * the status word will reflect the failed status cmd, not the failed
1519 * r/w cmd. If we fail to obtain card status, it suggests we can no
1520 * longer communicate with the card.
1521 * - Check the card state. If the card received the cmd but there was a
1522 * transient problem with the response, it might still be in a data transfer
1523 * mode. Try to send it a stop command. If this fails, we can't recover.
1524 * - If the r/w cmd failed due to a response CRC error, it was probably
1525 * transient, so retry the cmd.
1526 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1527 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1528 * illegal cmd, retry.
1529 * Otherwise we don't understand what happened, so abort.
1530 */
1531static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001532 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001533{
1534 bool prev_cmd_status_valid = true;
1535 u32 status, stop_status = 0;
1536 int err, retry;
1537
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301538 if (mmc_card_removed(card))
1539 return ERR_NOMEDIUM;
1540
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001541 /*
1542 * Try to get card status which indicates both the card state
1543 * and why there was no response. If the first attempt fails,
1544 * we can't be sure the returned status is for the r/w command.
1545 */
1546 for (retry = 2; retry >= 0; retry--) {
1547 err = get_card_status(card, &status, 0);
1548 if (!err)
1549 break;
1550
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001551 /* Re-tune if needed */
1552 mmc_retune_recheck(card->host);
1553
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001554 prev_cmd_status_valid = false;
1555 pr_err("%s: error %d sending status command, %sing\n",
1556 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1557 }
1558
1559 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301560 if (err) {
1561 /* Check if the card is removed */
1562 if (mmc_detect_card_removed(card->host))
1563 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001564 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301565 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001566
Adrian Hunter67716322011-08-29 16:42:15 +03001567 /* Flag ECC errors */
1568 if ((status & R1_CARD_ECC_FAILED) ||
1569 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1570 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1571 *ecc_err = 1;
1572
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001573 /* Flag General errors */
1574 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1575 if ((status & R1_ERROR) ||
1576 (brq->stop.resp[0] & R1_ERROR)) {
1577 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1578 req->rq_disk->disk_name, __func__,
1579 brq->stop.resp[0], status);
1580 *gen_err = 1;
1581 }
1582
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001583 /*
1584 * Check the current card state. If it is in some data transfer
1585 * mode, tell it to stop (and hopefully transition back to TRAN.)
1586 */
1587 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1588 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001589 err = send_stop(card,
1590 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1591 req, gen_err, &stop_status);
1592 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001593 pr_err("%s: error %d sending stop command\n",
1594 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001595 /*
1596 * If the stop cmd also timed out, the card is probably
1597 * not present, so abort. Other errors are bad news too.
1598 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001599 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001600 }
1601
Adrian Hunter67716322011-08-29 16:42:15 +03001602 if (stop_status & R1_CARD_ECC_FAILED)
1603 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001604 }
1605
1606 /* Check for set block count errors */
1607 if (brq->sbc.error)
1608 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1609 prev_cmd_status_valid, status);
1610
1611 /* Check for r/w command errors */
1612 if (brq->cmd.error)
1613 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1614 prev_cmd_status_valid, status);
1615
Adrian Hunter67716322011-08-29 16:42:15 +03001616 /* Data errors */
1617 if (!brq->stop.error)
1618 return ERR_CONTINUE;
1619
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001620 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001621 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 +01001622 req->rq_disk->disk_name, brq->stop.error,
1623 brq->cmd.resp[0], status);
1624
1625 /*
1626 * Subsitute in our own stop status as this will give the error
1627 * state which happened during the execution of the r/w command.
1628 */
1629 if (stop_status) {
1630 brq->stop.resp[0] = stop_status;
1631 brq->stop.error = 0;
1632 }
1633 return ERR_CONTINUE;
1634}
1635
Adrian Hunter67716322011-08-29 16:42:15 +03001636static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1637 int type)
1638{
1639 int err;
1640
1641 if (md->reset_done & type)
1642 return -EEXIST;
1643
1644 md->reset_done |= type;
1645 err = mmc_hw_reset(host);
1646 /* Ensure we switch back to the correct partition */
1647 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001648 struct mmc_blk_data *main_md =
1649 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001650 int part_err;
1651
1652 main_md->part_curr = main_md->part_type;
1653 part_err = mmc_blk_part_switch(host->card, md);
1654 if (part_err) {
1655 /*
1656 * We have failed to get back into the correct
1657 * partition, so we need to abort the whole request.
1658 */
1659 return -ENODEV;
1660 }
1661 }
1662 return err;
1663}
1664
1665static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1666{
1667 md->reset_done &= ~type;
1668}
1669
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001670int mmc_access_rpmb(struct mmc_queue *mq)
1671{
1672 struct mmc_blk_data *md = mq->data;
1673 /*
1674 * If this is a RPMB partition access, return ture
1675 */
1676 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1677 return true;
1678
1679 return false;
1680}
1681
Adrian Hunterbd788c92010-08-11 14:17:47 -07001682static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1683{
1684 struct mmc_blk_data *md = mq->data;
1685 struct mmc_card *card = md->queue.card;
1686 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001687 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001688
Adrian Hunterbd788c92010-08-11 14:17:47 -07001689 if (!mmc_can_erase(card)) {
1690 err = -EOPNOTSUPP;
1691 goto out;
1692 }
1693
1694 from = blk_rq_pos(req);
1695 nr = blk_rq_sectors(req);
1696
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001697 if (mmc_can_discard(card))
1698 arg = MMC_DISCARD_ARG;
1699 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001700 arg = MMC_TRIM_ARG;
1701 else
1702 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001703retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001704 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1705 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1706 INAND_CMD38_ARG_EXT_CSD,
1707 arg == MMC_TRIM_ARG ?
1708 INAND_CMD38_ARG_TRIM :
1709 INAND_CMD38_ARG_ERASE,
1710 0);
1711 if (err)
1712 goto out;
1713 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001714 err = mmc_erase(card, from, nr, arg);
1715out:
Adrian Hunter67716322011-08-29 16:42:15 +03001716 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1717 goto retry;
1718 if (!err)
1719 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301720 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001721
Adrian Hunterbd788c92010-08-11 14:17:47 -07001722 return err ? 0 : 1;
1723}
1724
Adrian Hunter49804542010-08-11 14:17:50 -07001725static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1726 struct request *req)
1727{
1728 struct mmc_blk_data *md = mq->data;
1729 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001730 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001731 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001732
Maya Erez775a9362013-04-18 15:41:55 +03001733 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001734 err = -EOPNOTSUPP;
1735 goto out;
1736 }
1737
1738 from = blk_rq_pos(req);
1739 nr = blk_rq_sectors(req);
1740
Maya Erez775a9362013-04-18 15:41:55 +03001741 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1742 arg = MMC_SECURE_TRIM1_ARG;
1743 else
1744 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001745
Adrian Hunter67716322011-08-29 16:42:15 +03001746retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001747 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1748 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1749 INAND_CMD38_ARG_EXT_CSD,
1750 arg == MMC_SECURE_TRIM1_ARG ?
1751 INAND_CMD38_ARG_SECTRIM1 :
1752 INAND_CMD38_ARG_SECERASE,
1753 0);
1754 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001755 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001756 }
Adrian Hunter28302812012-04-05 14:45:48 +03001757
Adrian Hunter49804542010-08-11 14:17:50 -07001758 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001759 if (err == -EIO)
1760 goto out_retry;
1761 if (err)
1762 goto out;
1763
1764 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001765 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1766 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1767 INAND_CMD38_ARG_EXT_CSD,
1768 INAND_CMD38_ARG_SECTRIM2,
1769 0);
1770 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001771 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001772 }
Adrian Hunter28302812012-04-05 14:45:48 +03001773
Adrian Hunter49804542010-08-11 14:17:50 -07001774 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001775 if (err == -EIO)
1776 goto out_retry;
1777 if (err)
1778 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001779 }
Adrian Hunter28302812012-04-05 14:45:48 +03001780
Adrian Hunter28302812012-04-05 14:45:48 +03001781out_retry:
1782 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001783 goto retry;
1784 if (!err)
1785 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001786out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301787 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001788
Adrian Hunter49804542010-08-11 14:17:50 -07001789 return err ? 0 : 1;
1790}
1791
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001792static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1793{
1794 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001795 struct mmc_card *card = md->queue.card;
1796 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001797
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001798 ret = mmc_flush_cache(card);
1799 if (ret)
1800 ret = -EIO;
1801
Mark Salyzyn6904e432016-01-28 11:12:25 -08001802#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
1803 else if (atomic_read(&mq->cache_size)) {
1804 long used = mmc_blk_cache_used(mq, jiffies);
1805
1806 if (used) {
1807 int speed = atomic_read(&mq->max_write_speed);
1808
1809 if (speed_valid(speed)) {
1810 unsigned long msecs = jiffies_to_msecs(
1811 size_and_speed_to_jiffies(
1812 used, speed));
1813 if (msecs)
1814 msleep(msecs);
1815 }
1816 }
1817 }
1818#endif
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301819 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001820
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001821 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001822}
1823
1824/*
1825 * Reformat current write as a reliable write, supporting
1826 * both legacy and the enhanced reliable write MMC cards.
1827 * In each transfer we'll handle only as much as a single
1828 * reliable write can handle, thus finish the request in
1829 * partial completions.
1830 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001831static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1832 struct mmc_card *card,
1833 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001834{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001835 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1836 /* Legacy mode imposes restrictions on transfers. */
1837 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1838 brq->data.blocks = 1;
1839
1840 if (brq->data.blocks > card->ext_csd.rel_sectors)
1841 brq->data.blocks = card->ext_csd.rel_sectors;
1842 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1843 brq->data.blocks = 1;
1844 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001845}
1846
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001847#define CMD_ERRORS \
1848 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1849 R1_ADDRESS_ERROR | /* Misaligned address */ \
1850 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1851 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1852 R1_CC_ERROR | /* Card controller error */ \
1853 R1_ERROR) /* General/unknown error */
1854
Per Forlinee8a43a2011-07-01 18:55:33 +02001855static int mmc_blk_err_check(struct mmc_card *card,
1856 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001857{
Per Forlinee8a43a2011-07-01 18:55:33 +02001858 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1859 mmc_active);
1860 struct mmc_blk_request *brq = &mq_mrq->brq;
1861 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001862 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001863 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001864
1865 /*
1866 * sbc.error indicates a problem with the set block count
1867 * command. No data will have been transferred.
1868 *
1869 * cmd.error indicates a problem with the r/w command. No
1870 * data will have been transferred.
1871 *
1872 * stop.error indicates a problem with the stop command. Data
1873 * may have been transferred, or may still be transferring.
1874 */
Adrian Hunter67716322011-08-29 16:42:15 +03001875 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1876 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001877 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001878 case ERR_RETRY:
1879 return MMC_BLK_RETRY;
1880 case ERR_ABORT:
1881 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301882 case ERR_NOMEDIUM:
1883 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001884 case ERR_CONTINUE:
1885 break;
1886 }
1887 }
1888
1889 /*
1890 * Check for errors relating to the execution of the
1891 * initial command - such as address errors. No data
1892 * has been transferred.
1893 */
1894 if (brq->cmd.resp[0] & CMD_ERRORS) {
1895 pr_err("%s: r/w command failed, status = %#x\n",
1896 req->rq_disk->disk_name, brq->cmd.resp[0]);
1897 return MMC_BLK_ABORT;
1898 }
1899
1900 /*
1901 * Everything else is either success, or a data error of some
1902 * kind. If it was a write, we may have transitioned to
1903 * program mode, which we have to wait for it to complete.
1904 */
1905 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001906 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001907
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001908 /* Check stop command response */
1909 if (brq->stop.resp[0] & R1_ERROR) {
1910 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1911 req->rq_disk->disk_name, __func__,
1912 brq->stop.resp[0]);
1913 gen_err = 1;
1914 }
1915
Ulf Hansson95a91292014-01-29 13:11:27 +01001916 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1917 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001918 if (err)
1919 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001920 }
1921
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001922 /* if general error occurs, retry the write operation. */
1923 if (gen_err) {
1924 pr_warn("%s: retrying write for general error\n",
1925 req->rq_disk->disk_name);
1926 return MMC_BLK_RETRY;
1927 }
1928
Per Forlind78d4a82011-07-01 18:55:30 +02001929 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001930 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001931 pr_debug("%s: retrying because a re-tune was needed\n",
1932 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001933 brq->retune_retry_done = 1;
1934 return MMC_BLK_RETRY;
1935 }
Per Forlind78d4a82011-07-01 18:55:30 +02001936 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1937 req->rq_disk->disk_name, brq->data.error,
1938 (unsigned)blk_rq_pos(req),
1939 (unsigned)blk_rq_sectors(req),
1940 brq->cmd.resp[0], brq->stop.resp[0]);
1941
1942 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001943 if (ecc_err)
1944 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001945 return MMC_BLK_DATA_ERR;
1946 } else {
1947 return MMC_BLK_CMD_ERR;
1948 }
1949 }
1950
Adrian Hunter67716322011-08-29 16:42:15 +03001951 if (!brq->data.bytes_xfered)
1952 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001953
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001954 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1955 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1956 return MMC_BLK_PARTIAL;
1957 else
1958 return MMC_BLK_SUCCESS;
1959 }
1960
Adrian Hunter67716322011-08-29 16:42:15 +03001961 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1962 return MMC_BLK_PARTIAL;
1963
1964 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001965}
1966
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001967static int mmc_blk_packed_err_check(struct mmc_card *card,
1968 struct mmc_async_req *areq)
1969{
1970 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1971 mmc_active);
1972 struct request *req = mq_rq->req;
1973 struct mmc_packed *packed = mq_rq->packed;
1974 int err, check, status;
1975 u8 *ext_csd;
1976
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001977 packed->retries--;
1978 check = mmc_blk_err_check(card, areq);
1979 err = get_card_status(card, &status, 0);
1980 if (err) {
1981 pr_err("%s: error %d sending status command\n",
1982 req->rq_disk->disk_name, err);
1983 return MMC_BLK_ABORT;
1984 }
1985
1986 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02001987 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001988 if (err) {
1989 pr_err("%s: error %d sending ext_csd\n",
1990 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02001991 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001992 }
1993
1994 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1995 EXT_CSD_PACKED_FAILURE) &&
1996 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1997 EXT_CSD_PACKED_GENERIC_ERROR)) {
1998 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1999 EXT_CSD_PACKED_INDEXED_ERROR) {
2000 packed->idx_failure =
2001 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
2002 check = MMC_BLK_PARTIAL;
2003 }
2004 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
2005 "failure index: %d\n",
2006 req->rq_disk->disk_name, packed->nr_entries,
2007 packed->blocks, packed->idx_failure);
2008 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002009 kfree(ext_csd);
2010 }
2011
2012 return check;
2013}
2014
Per Forlin54d49d72011-07-01 18:55:29 +02002015static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
2016 struct mmc_card *card,
2017 int disable_multi,
2018 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Per Forlin54d49d72011-07-01 18:55:29 +02002020 u32 readcmd, writecmd;
2021 struct mmc_blk_request *brq = &mqrq->brq;
2022 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05302024 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002026 /*
2027 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00002028 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002029 */
Luca Porziod3df0462015-11-06 15:12:26 +00002030 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002031 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002032 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002033
Per Forlin54d49d72011-07-01 18:55:29 +02002034 memset(brq, 0, sizeof(struct mmc_blk_request));
2035 brq->mrq.cmd = &brq->cmd;
2036 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Per Forlin54d49d72011-07-01 18:55:29 +02002038 brq->cmd.arg = blk_rq_pos(req);
2039 if (!mmc_card_blockaddr(card))
2040 brq->cmd.arg <<= 9;
2041 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2042 brq->data.blksz = 512;
2043 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2044 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002045 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Asutosh Dasf0665412012-07-27 18:10:19 +05302047 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02002048 /*
2049 * The block layer doesn't support all sector count
2050 * restrictions, so we need to be prepared for too big
2051 * requests.
2052 */
2053 if (brq->data.blocks > card->host->max_blk_count)
2054 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002056 if (brq->data.blocks > 1) {
2057 /*
2058 * After a read error, we redo the request one sector
2059 * at a time in order to accurately determine which
2060 * sectors can be read successfully.
2061 */
2062 if (disable_multi)
2063 brq->data.blocks = 1;
2064
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07002065 /*
2066 * Some controllers have HW issues while operating
2067 * in multiple I/O mode
2068 */
2069 if (card->host->ops->multi_io_quirk)
2070 brq->data.blocks = card->host->ops->multi_io_quirk(card,
2071 (rq_data_dir(req) == READ) ?
2072 MMC_DATA_READ : MMC_DATA_WRITE,
2073 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002074 }
Per Forlin54d49d72011-07-01 18:55:29 +02002075
2076 if (brq->data.blocks > 1 || do_rel_wr) {
2077 /* SPI multiblock writes terminate using a special
2078 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02002079 */
Per Forlin54d49d72011-07-01 18:55:29 +02002080 if (!mmc_host_is_spi(card->host) ||
2081 rq_data_dir(req) == READ)
2082 brq->mrq.stop = &brq->stop;
2083 readcmd = MMC_READ_MULTIPLE_BLOCK;
2084 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
2085 } else {
2086 brq->mrq.stop = NULL;
2087 readcmd = MMC_READ_SINGLE_BLOCK;
2088 writecmd = MMC_WRITE_BLOCK;
2089 }
2090 if (rq_data_dir(req) == READ) {
2091 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002092 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002093 if (brq->mrq.stop)
2094 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
2095 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002096 } else {
2097 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002098 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002099 if (brq->mrq.stop)
2100 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
2101 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002102 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02002103
Per Forlin54d49d72011-07-01 18:55:29 +02002104 if (do_rel_wr)
2105 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01002106
Per Forlin54d49d72011-07-01 18:55:29 +02002107 /*
Saugata Das42659002011-12-21 13:09:17 +05302108 * Data tag is used only during writing meta data to speed
2109 * up write and any subsequent read of this meta data
2110 */
2111 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2112 (req->cmd_flags & REQ_META) &&
2113 (rq_data_dir(req) == WRITE) &&
2114 ((brq->data.blocks * brq->data.blksz) >=
2115 card->ext_csd.data_tag_unit_size);
2116
2117 /*
Per Forlin54d49d72011-07-01 18:55:29 +02002118 * Pre-defined multi-block transfers are preferable to
2119 * open ended-ones (and necessary for reliable writes).
2120 * However, it is not sufficient to just send CMD23,
2121 * and avoid the final CMD12, as on an error condition
2122 * CMD12 (stop) needs to be sent anyway. This, coupled
2123 * with Auto-CMD23 enhancements provided by some
2124 * hosts, means that the complexity of dealing
2125 * with this is best left to the host. If CMD23 is
2126 * supported by card and host, we'll fill sbc in and let
2127 * the host deal with handling it correctly. This means
2128 * that for hosts that don't expose MMC_CAP_CMD23, no
2129 * change of behavior will be observed.
2130 *
2131 * N.B: Some MMC cards experience perf degradation.
2132 * We'll avoid using CMD23-bounded multiblock writes for
2133 * these, while retaining features like reliable writes.
2134 */
Saugata Das42659002011-12-21 13:09:17 +05302135 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
2136 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
2137 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02002138 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2139 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05302140 (do_rel_wr ? (1 << 31) : 0) |
2141 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02002142 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2143 brq->mrq.sbc = &brq->sbc;
2144 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002145
Per Forlin54d49d72011-07-01 18:55:29 +02002146 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002147
Per Forlin54d49d72011-07-01 18:55:29 +02002148 brq->data.sg = mqrq->sg;
2149 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002150
Per Forlin54d49d72011-07-01 18:55:29 +02002151 /*
2152 * Adjust the sg list so it is the same size as the
2153 * request.
2154 */
2155 if (brq->data.blocks != blk_rq_sectors(req)) {
2156 int i, data_size = brq->data.blocks << 9;
2157 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02002158
Per Forlin54d49d72011-07-01 18:55:29 +02002159 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
2160 data_size -= sg->length;
2161 if (data_size <= 0) {
2162 sg->length += data_size;
2163 i++;
2164 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01002165 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002166 }
Per Forlin54d49d72011-07-01 18:55:29 +02002167 brq->data.sg_len = i;
2168 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002169
Per Forlinee8a43a2011-07-01 18:55:33 +02002170 mqrq->mmc_active.mrq = &brq->mrq;
2171 mqrq->mmc_active.err_check = mmc_blk_err_check;
2172
Per Forlin54d49d72011-07-01 18:55:29 +02002173 mmc_queue_bounce_pre(mqrq);
2174}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002176static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
2177 struct mmc_card *card)
2178{
2179 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
2180 unsigned int max_seg_sz = queue_max_segment_size(q);
2181 unsigned int len, nr_segs = 0;
2182
2183 do {
2184 len = min(hdr_sz, max_seg_sz);
2185 hdr_sz -= len;
2186 nr_segs++;
2187 } while (hdr_sz);
2188
2189 return nr_segs;
2190}
2191
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002192/**
2193 * mmc_blk_disable_wr_packing() - disables packing mode
2194 * @mq: MMC queue.
2195 *
2196 */
2197void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
2198{
2199 if (mq) {
2200 mq->wr_packing_enabled = false;
2201 mq->num_of_potential_packed_wr_reqs = 0;
2202 }
2203}
2204EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
2205
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002206static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2207 struct request *req)
2208{
2209 struct mmc_host *host = mq->card->host;
2210 int data_dir;
2211
2212 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2213 return;
2214
Maya Erez8e2b3c32012-12-02 13:27:15 +02002215 /* Support for the write packing on eMMC 4.5 or later */
2216 if (mq->card->ext_csd.rev <= 5)
2217 return;
2218
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002219 /*
2220 * In case the packing control is not supported by the host, it should
2221 * not have an effect on the write packing. Therefore we have to enable
2222 * the write packing
2223 */
2224 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2225 mq->wr_packing_enabled = true;
2226 return;
2227 }
2228
2229 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2230 if (mq->num_of_potential_packed_wr_reqs >
2231 mq->num_wr_reqs_to_start_packing)
2232 mq->wr_packing_enabled = true;
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002233 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002234 return;
2235 }
2236
2237 data_dir = rq_data_dir(req);
2238
2239 if (data_dir == READ) {
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002240 mmc_blk_disable_wr_packing(mq);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002241 return;
2242 } else if (data_dir == WRITE) {
2243 mq->num_of_potential_packed_wr_reqs++;
2244 }
2245
2246 if (mq->num_of_potential_packed_wr_reqs >
2247 mq->num_wr_reqs_to_start_packing)
2248 mq->wr_packing_enabled = true;
2249}
2250
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002251struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2252{
2253 if (!card)
2254 return NULL;
2255
2256 return &card->wr_pack_stats;
2257}
2258EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2259
2260void mmc_blk_init_packed_statistics(struct mmc_card *card)
2261{
2262 int max_num_of_packed_reqs = 0;
2263
2264 if (!card || !card->wr_pack_stats.packing_events)
2265 return;
2266
2267 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2268
2269 spin_lock(&card->wr_pack_stats.lock);
2270 memset(card->wr_pack_stats.packing_events, 0,
2271 (max_num_of_packed_reqs + 1) *
2272 sizeof(*card->wr_pack_stats.packing_events));
2273 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2274 sizeof(card->wr_pack_stats.pack_stop_reason));
2275 card->wr_pack_stats.enabled = true;
2276 spin_unlock(&card->wr_pack_stats.lock);
2277}
2278EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2279
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002280static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2281{
2282 struct request_queue *q = mq->queue;
2283 struct mmc_card *card = mq->card;
2284 struct request *cur = req, *next = NULL;
2285 struct mmc_blk_data *md = mq->data;
2286 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2287 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2288 unsigned int req_sectors = 0, phys_segments = 0;
2289 unsigned int max_blk_count, max_phys_segs;
2290 bool put_back = true;
2291 u8 max_packed_rw = 0;
2292 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002293 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002294
Shawn Lin96e52da2016-08-26 08:49:55 +08002295 /*
2296 * We don't need to check packed for any further
2297 * operation of packed stuff as we set MMC_PACKED_NONE
2298 * and return zero for reqs if geting null packed. Also
2299 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2300 * it again when removing blk req.
2301 */
2302 if (!mqrq->packed) {
2303 md->flags &= (~MMC_BLK_PACKED_CMD);
2304 goto no_packed;
2305 }
2306
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002307 if (!(md->flags & MMC_BLK_PACKED_CMD))
2308 goto no_packed;
2309
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002310 if (!mq->wr_packing_enabled)
2311 goto no_packed;
2312
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002313 if ((rq_data_dir(cur) == WRITE) &&
2314 mmc_host_packed_wr(card->host))
2315 max_packed_rw = card->ext_csd.max_packed_writes;
2316
2317 if (max_packed_rw == 0)
2318 goto no_packed;
2319
2320 if (mmc_req_rel_wr(cur) &&
2321 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2322 goto no_packed;
2323
2324 if (mmc_large_sector(card) &&
2325 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2326 goto no_packed;
2327
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002328 if (cur->cmd_flags & REQ_FUA)
2329 goto no_packed;
2330
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002331 mmc_blk_clear_packed(mqrq);
2332
2333 max_blk_count = min(card->host->max_blk_count,
2334 card->host->max_req_size >> 9);
2335 if (unlikely(max_blk_count > 0xffff))
2336 max_blk_count = 0xffff;
2337
2338 max_phys_segs = queue_max_segments(q);
2339 req_sectors += blk_rq_sectors(cur);
2340 phys_segments += cur->nr_phys_segments;
2341
2342 if (rq_data_dir(cur) == WRITE) {
2343 req_sectors += mmc_large_sector(card) ? 8 : 1;
2344 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2345 }
2346
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002347 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002348 do {
2349 if (reqs >= max_packed_rw - 1) {
2350 put_back = false;
2351 break;
2352 }
2353
2354 spin_lock_irq(q->queue_lock);
2355 next = blk_fetch_request(q);
2356 spin_unlock_irq(q->queue_lock);
2357 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002358 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002359 put_back = false;
2360 break;
2361 }
2362
2363 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002364 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2365 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002366 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002367 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002368
Mike Christie3a5e02c2016-06-05 14:32:23 -05002369 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002370 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002371 req_op(next) == REQ_OP_FLUSH) {
2372 if (req_op(next) != REQ_OP_SECURE_ERASE)
2373 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002374 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002375 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002376
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002377 if (next->cmd_flags & REQ_FUA) {
2378 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2379 break;
2380 }
2381
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002382 if (rq_data_dir(cur) != rq_data_dir(next)) {
2383 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002384 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002385 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002386
2387 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002388 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2389 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002390 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002391 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002392
2393 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002394 if (req_sectors > max_blk_count) {
2395 if (stats->enabled)
2396 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002397 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002398 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002399
2400 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002401 if (phys_segments > max_phys_segs) {
2402 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002403 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002404 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002405
Maya Erez5a8dae12014-12-04 15:13:59 +02002406 if (mq->no_pack_for_random) {
2407 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2408 blk_rq_pos(next)) {
2409 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2410 put_back = 1;
2411 break;
2412 }
2413 }
2414
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002415 if (rq_data_dir(next) == WRITE)
2416 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002417 list_add_tail(&next->queuelist, &mqrq->packed->list);
2418 cur = next;
2419 reqs++;
2420 } while (1);
2421
2422 if (put_back) {
2423 spin_lock_irq(q->queue_lock);
2424 blk_requeue_request(q, next);
2425 spin_unlock_irq(q->queue_lock);
2426 }
2427
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002428 if (stats->enabled) {
2429 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2430 stats->packing_events[reqs + 1]++;
2431 if (reqs + 1 == max_packed_rw)
2432 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2433 }
2434
2435 spin_unlock(&stats->lock);
2436
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002437 if (reqs > 0) {
2438 list_add(&req->queuelist, &mqrq->packed->list);
2439 mqrq->packed->nr_entries = ++reqs;
2440 mqrq->packed->retries = reqs;
2441 return reqs;
2442 }
2443
2444no_packed:
2445 mqrq->cmd_type = MMC_PACKED_NONE;
2446 return 0;
2447}
2448
2449static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2450 struct mmc_card *card,
2451 struct mmc_queue *mq)
2452{
2453 struct mmc_blk_request *brq = &mqrq->brq;
2454 struct request *req = mqrq->req;
2455 struct request *prq;
2456 struct mmc_blk_data *md = mq->data;
2457 struct mmc_packed *packed = mqrq->packed;
2458 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002459 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002460 u8 hdr_blocks;
2461 u8 i = 1;
2462
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002463 mqrq->cmd_type = MMC_PACKED_WRITE;
2464 packed->blocks = 0;
2465 packed->idx_failure = MMC_PACKED_NR_IDX;
2466
2467 packed_cmd_hdr = packed->cmd_hdr;
2468 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002469 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2470 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002471 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2472
2473 /*
2474 * Argument for each entry of packed group
2475 */
2476 list_for_each_entry(prq, &packed->list, queuelist) {
2477 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2478 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2479 (prq->cmd_flags & REQ_META) &&
2480 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002481 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002482 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002483 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002484 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2485 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002486 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002487 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002488 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002489 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002490 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002491 packed->blocks += blk_rq_sectors(prq);
2492 i++;
2493 }
2494
2495 memset(brq, 0, sizeof(struct mmc_blk_request));
2496 brq->mrq.cmd = &brq->cmd;
2497 brq->mrq.data = &brq->data;
2498 brq->mrq.sbc = &brq->sbc;
2499 brq->mrq.stop = &brq->stop;
2500
2501 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2502 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2503 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2504
2505 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2506 brq->cmd.arg = blk_rq_pos(req);
2507 if (!mmc_card_blockaddr(card))
2508 brq->cmd.arg <<= 9;
2509 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2510
2511 brq->data.blksz = 512;
2512 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002513 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302514 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002515
2516 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2517 brq->stop.arg = 0;
2518 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2519
2520 mmc_set_data_timeout(&brq->data, card);
2521
2522 brq->data.sg = mqrq->sg;
2523 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2524
2525 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002526
2527 /*
2528 * This is intended for packed commands tests usage - in case these
2529 * functions are not in use the respective pointers are NULL
2530 */
2531 if (mq->err_check_fn)
2532 mqrq->mmc_active.err_check = mq->err_check_fn;
2533 else
2534 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2535
2536 if (mq->packed_test_fn)
2537 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002538
2539 mmc_queue_bounce_pre(mqrq);
2540}
2541
Adrian Hunter67716322011-08-29 16:42:15 +03002542static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2543 struct mmc_blk_request *brq, struct request *req,
2544 int ret)
2545{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002546 struct mmc_queue_req *mq_rq;
2547 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2548
Adrian Hunter67716322011-08-29 16:42:15 +03002549 /*
2550 * If this is an SD card and we're writing, we can first
2551 * mark the known good sectors as ok.
2552 *
2553 * If the card is not SD, we can still ok written sectors
2554 * as reported by the controller (which might be less than
2555 * the real number of written sectors, but never more).
2556 */
2557 if (mmc_card_sd(card)) {
2558 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302559 if (!brq->data.fault_injected) {
2560 blocks = mmc_sd_num_wr_blocks(card);
2561 if (blocks != (u32)-1)
2562 ret = blk_end_request(req, 0, blocks << 9);
2563 } else
2564 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002565 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002566 if (!mmc_packed_cmd(mq_rq->cmd_type))
2567 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002568 }
2569 return ret;
2570}
2571
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002572static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2573{
2574 struct request *prq;
2575 struct mmc_packed *packed = mq_rq->packed;
2576 int idx = packed->idx_failure, i = 0;
2577 int ret = 0;
2578
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002579 while (!list_empty(&packed->list)) {
2580 prq = list_entry_rq(packed->list.next);
2581 if (idx == i) {
2582 /* retry from error index */
2583 packed->nr_entries -= idx;
2584 mq_rq->req = prq;
2585 ret = 1;
2586
2587 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2588 list_del_init(&prq->queuelist);
2589 mmc_blk_clear_packed(mq_rq);
2590 }
2591 return ret;
2592 }
2593 list_del_init(&prq->queuelist);
2594 blk_end_request(prq, 0, blk_rq_bytes(prq));
2595 i++;
2596 }
2597
2598 mmc_blk_clear_packed(mq_rq);
2599 return ret;
2600}
2601
2602static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2603{
2604 struct request *prq;
2605 struct mmc_packed *packed = mq_rq->packed;
2606
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002607 while (!list_empty(&packed->list)) {
2608 prq = list_entry_rq(packed->list.next);
2609 list_del_init(&prq->queuelist);
2610 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2611 }
2612
2613 mmc_blk_clear_packed(mq_rq);
2614}
2615
2616static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2617 struct mmc_queue_req *mq_rq)
2618{
2619 struct request *prq;
2620 struct request_queue *q = mq->queue;
2621 struct mmc_packed *packed = mq_rq->packed;
2622
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002623 while (!list_empty(&packed->list)) {
2624 prq = list_entry_rq(packed->list.prev);
2625 if (prq->queuelist.prev != &packed->list) {
2626 list_del_init(&prq->queuelist);
2627 spin_lock_irq(q->queue_lock);
2628 blk_requeue_request(mq->queue, prq);
2629 spin_unlock_irq(q->queue_lock);
2630 } else {
2631 list_del_init(&prq->queuelist);
2632 }
2633 }
2634
2635 mmc_blk_clear_packed(mq_rq);
2636}
2637
Per Forlinee8a43a2011-07-01 18:55:33 +02002638static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02002639{
2640 struct mmc_blk_data *md = mq->data;
2641 struct mmc_card *card = md->queue.card;
2642 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002643 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002644 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002645 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302646 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02002647 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002648 const u8 packed_nr = 2;
2649 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002650#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2651 unsigned long waitfor = jiffies;
2652#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02002653
2654 if (!rqc && !mq->mqrq_prev->req)
2655 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002656
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002657 if (rqc)
2658 reqs = mmc_blk_prep_packed_list(mq, rqc);
2659
Per Forlin54d49d72011-07-01 18:55:29 +02002660 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002661 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302662 /*
2663 * When 4KB native sector is enabled, only 8 blocks
2664 * multiple read or write is allowed
2665 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00002666 if (mmc_large_sector(card) &&
2667 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302668 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
2669 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002670 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302671 goto cmd_abort;
2672 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002673
2674 if (reqs >= packed_nr)
2675 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2676 card, mq);
2677 else
2678 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002679 areq = &mq->mqrq_cur->mmc_active;
2680 } else
2681 areq = NULL;
2682 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002683 if (!areq) {
2684 if (status == MMC_BLK_NEW_REQUEST)
2685 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02002686 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002687 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002688
Per Forlinee8a43a2011-07-01 18:55:33 +02002689 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2690 brq = &mq_rq->brq;
2691 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002692 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002693 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002694
Per Forlind78d4a82011-07-01 18:55:30 +02002695 switch (status) {
2696 case MMC_BLK_SUCCESS:
2697 case MMC_BLK_PARTIAL:
2698 /*
2699 * A block was successfully transferred.
2700 */
Adrian Hunter67716322011-08-29 16:42:15 +03002701 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002702
Mark Salyzyn6904e432016-01-28 11:12:25 -08002703 mmc_blk_simulate_delay(mq, rqc, waitfor);
2704
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002705 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2706 ret = mmc_blk_end_packed_req(mq_rq);
2707 break;
2708 } else {
2709 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002710 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002711 }
2712
Adrian Hunter67716322011-08-29 16:42:15 +03002713 /*
2714 * If the blk_end_request function returns non-zero even
2715 * though all data has been transferred and no errors
2716 * were returned by the host controller, it's a bug.
2717 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002718 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302719 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002720 __func__, blk_rq_bytes(req),
2721 brq->data.bytes_xfered);
2722 rqc = NULL;
2723 goto cmd_abort;
2724 }
Per Forlind78d4a82011-07-01 18:55:30 +02002725 break;
2726 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002727 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002728 if (mmc_blk_reset(md, card->host, type))
2729 goto cmd_abort;
2730 if (!ret)
2731 goto start_new_req;
2732 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002733 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002734 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a82011-07-01 18:55:30 +02002735 if (retry++ < 5)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002736 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002737 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002738 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002739 if (!mmc_blk_reset(md, card->host, type))
2740 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002741 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002742 case MMC_BLK_DATA_ERR: {
2743 int err;
2744
2745 err = mmc_blk_reset(md, card->host, type);
2746 if (!err)
2747 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05302748 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002749 }
2750 case MMC_BLK_ECC_ERR:
2751 if (brq->data.blocks > 1) {
2752 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002753 pr_warn("%s: retrying using single block read\n",
2754 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002755 disable_multi = 1;
2756 break;
2757 }
Per Forlind78d4a82011-07-01 18:55:30 +02002758 /*
2759 * After an error, we redo I/O one sector at a
2760 * time, so we only reach here after trying to
2761 * read a single sector.
2762 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302763 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002764 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002765 if (!ret)
2766 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002767 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302768 case MMC_BLK_NOMEDIUM:
2769 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002770 default:
2771 pr_err("%s: Unhandled return value (%d)",
2772 req->rq_disk->disk_name, status);
2773 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002774 }
2775
Per Forlinee8a43a2011-07-01 18:55:33 +02002776 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002777 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2778 if (!mq_rq->packed->retries)
2779 goto cmd_abort;
2780 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2781 mmc_start_req(card->host,
2782 &mq_rq->mmc_active, NULL);
2783 } else {
2784
2785 /*
2786 * In case of a incomplete request
2787 * prepare it again and resend.
2788 */
2789 mmc_blk_rw_rq_prep(mq_rq, card,
2790 disable_multi, mq);
2791 mmc_start_req(card->host,
2792 &mq_rq->mmc_active, NULL);
2793 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002794 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 } while (ret);
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 return 1;
2799
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002800 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002801 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2802 mmc_blk_abort_packed_req(mq_rq);
2803 } else {
2804 if (mmc_card_removed(card))
2805 req->cmd_flags |= REQ_QUIET;
2806 while (ret)
2807 ret = blk_end_request(req, -EIO,
2808 blk_rq_cur_bytes(req));
2809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Per Forlinee8a43a2011-07-01 18:55:33 +02002811 start_new_req:
2812 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002813 if (mmc_card_removed(card)) {
2814 rqc->cmd_flags |= REQ_QUIET;
2815 blk_end_request_all(rqc, -EIO);
2816 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002817 /*
2818 * If current request is packed, it needs to put back.
2819 */
2820 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2821 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2822
Seungwon Jeon7a819022013-01-22 19:48:07 +09002823 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2824 mmc_start_req(card->host,
2825 &mq->mqrq_cur->mmc_active, NULL);
2826 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002827 }
2828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 return 0;
2830}
2831
Linus Walleij29eb7bd2016-09-20 11:34:38 +02002832int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002833{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002834 int ret;
2835 struct mmc_blk_data *md = mq->data;
2836 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002837 struct mmc_host *host = card->host;
2838 unsigned long flags;
Adrian Hunter869c5542016-08-25 14:11:43 -06002839 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002840
Per Forlinee8a43a2011-07-01 18:55:33 +02002841 if (req && !mq->mqrq_prev->req)
2842 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002843 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002844
Andrei Warkentin371a6892011-04-11 18:10:25 -05002845 ret = mmc_blk_part_switch(card, md);
2846 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002847 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302848 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002849 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002850 ret = 0;
2851 goto out;
2852 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002853
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002854 mmc_blk_write_packing_control(mq, req);
2855
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002856 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05002857 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002858 /* complete ongoing async transfer before issuing discard */
2859 if (card->host->areq)
2860 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02002861 ret = mmc_blk_issue_discard_rq(mq, req);
2862 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
2863 /* complete ongoing async transfer before issuing secure erase*/
2864 if (card->host->areq)
2865 mmc_blk_issue_rw_rq(mq, NULL);
2866 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05002867 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002868 /* complete ongoing async transfer before issuing flush */
2869 if (card->host->areq)
2870 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002871 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002872 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002873 if (!req && host->areq) {
2874 spin_lock_irqsave(&host->context_info.lock, flags);
2875 host->context_info.is_waiting_last_req = true;
2876 spin_unlock_irqrestore(&host->context_info.lock, flags);
2877 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002878 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002879 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002880
Andrei Warkentin371a6892011-04-11 18:10:25 -05002881out:
Adrian Hunter869c5542016-08-25 14:11:43 -06002882 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002883 /*
2884 * Release host when there are no more requests
2885 * and after special request(discard, flush) is done.
2886 * In case sepecial request, there is no reentry to
2887 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2888 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002889 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002890 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002891}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Russell Kinga6f6c962006-01-03 22:38:44 +00002893static inline int mmc_blk_readonly(struct mmc_card *card)
2894{
2895 return mmc_card_readonly(card) ||
2896 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2897}
2898
Andrei Warkentin371a6892011-04-11 18:10:25 -05002899static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2900 struct device *parent,
2901 sector_t size,
2902 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002903 const char *subname,
2904 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
2906 struct mmc_blk_data *md;
2907 int devidx, ret;
2908
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002909again:
2910 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
2911 return ERR_PTR(-ENOMEM);
2912
2913 spin_lock(&mmc_blk_lock);
2914 ret = ida_get_new(&mmc_blk_ida, &devidx);
2915 spin_unlock(&mmc_blk_lock);
2916
2917 if (ret == -EAGAIN)
2918 goto again;
2919 else if (ret)
2920 return ERR_PTR(ret);
2921
2922 if (devidx >= max_devices) {
2923 ret = -ENOSPC;
2924 goto out;
2925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002927 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002928 if (!md) {
2929 ret = -ENOMEM;
2930 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002932
Johan Rudholmadd710e2011-12-02 08:51:06 +01002933 md->area_type = area_type;
2934
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002935 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002936 * Set the read-only status based on the supported commands
2937 * and the write protect switch.
2938 */
2939 md->read_only = mmc_blk_readonly(card);
2940
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002941 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002942 if (md->disk == NULL) {
2943 ret = -ENOMEM;
2944 goto err_kfree;
2945 }
2946
2947 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002948 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002949 md->usage = 1;
2950
Adrian Hunterd09408a2011-06-23 13:40:28 +03002951 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002952 if (ret)
2953 goto err_putdisk;
2954
Russell Kinga6f6c962006-01-03 22:38:44 +00002955 md->queue.data = md;
2956
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002957 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002958 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002959 md->disk->fops = &mmc_bdops;
2960 md->disk->private_data = md;
2961 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002962 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002963 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002964 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002965 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002966 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002967
2968 /*
2969 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2970 *
2971 * - be set for removable media with permanent block devices
2972 * - be unset for removable block devices with permanent media
2973 *
2974 * Since MMC block devices clearly fall under the second
2975 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2976 * should use the block device creation/destruction hotplug
2977 * messages to tell when the card is present.
2978 */
2979
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002980 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02002981 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002982
Saugata Dasa5075eb2012-05-17 16:32:21 +05302983 if (mmc_card_mmc(card))
2984 blk_queue_logical_block_size(md->queue.queue,
2985 card->ext_csd.data_sector_size);
2986 else
2987 blk_queue_logical_block_size(md->queue.queue, 512);
2988
Andrei Warkentin371a6892011-04-11 18:10:25 -05002989 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002990
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002991 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02002992 if ((mmc_card_mmc(card) &&
2993 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002994 (mmc_card_sd(card) &&
2995 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2996 md->flags |= MMC_BLK_CMD23;
2997 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002998
2999 if (mmc_card_mmc(card) &&
3000 md->flags & MMC_BLK_CMD23 &&
3001 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
3002 card->ext_csd.rel_sectors)) {
3003 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06003004 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003005 }
3006
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003007 if (mmc_card_mmc(card) &&
3008 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
3009 (md->flags & MMC_BLK_CMD23) &&
3010 card->ext_csd.packed_event_en) {
3011 if (!mmc_packed_init(&md->queue, card))
3012 md->flags |= MMC_BLK_PACKED_CMD;
3013 }
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00003016
3017 err_putdisk:
3018 put_disk(md->disk);
3019 err_kfree:
3020 kfree(md);
3021 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003022 spin_lock(&mmc_blk_lock);
3023 ida_remove(&mmc_blk_ida, devidx);
3024 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00003025 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026}
3027
Andrei Warkentin371a6892011-04-11 18:10:25 -05003028static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
3029{
3030 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003031
3032 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
3033 /*
3034 * The EXT_CSD sector count is in number or 512 byte
3035 * sectors.
3036 */
3037 size = card->ext_csd.sectors;
3038 } else {
3039 /*
3040 * The CSD capacity field is in units of read_blkbits.
3041 * set_capacity takes units of 512 bytes.
3042 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00003043 size = (typeof(sector_t))card->csd.capacity
3044 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003045 }
3046
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01003047 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003048 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003049}
3050
3051static int mmc_blk_alloc_part(struct mmc_card *card,
3052 struct mmc_blk_data *md,
3053 unsigned int part_type,
3054 sector_t size,
3055 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003056 const char *subname,
3057 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05003058{
3059 char cap_str[10];
3060 struct mmc_blk_data *part_md;
3061
3062 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003063 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003064 if (IS_ERR(part_md))
3065 return PTR_ERR(part_md);
3066 part_md->part_type = part_type;
3067 list_add(&part_md->part, &md->part);
3068
James Bottomleyb9f28d82015-03-05 18:47:01 -08003069 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05003070 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303071 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05003072 part_md->disk->disk_name, mmc_card_id(card),
3073 mmc_card_name(card), part_md->part_type, cap_str);
3074 return 0;
3075}
3076
Namjae Jeone0c368d2011-10-06 23:41:38 +09003077/* MMC Physical partitions consist of two boot partitions and
3078 * up to four general purpose partitions.
3079 * For each partition enabled in EXT_CSD a block device will be allocatedi
3080 * to provide access to the partition.
3081 */
3082
Andrei Warkentin371a6892011-04-11 18:10:25 -05003083static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
3084{
Namjae Jeone0c368d2011-10-06 23:41:38 +09003085 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003086
3087 if (!mmc_card_mmc(card))
3088 return 0;
3089
Namjae Jeone0c368d2011-10-06 23:41:38 +09003090 for (idx = 0; idx < card->nr_parts; idx++) {
3091 if (card->part[idx].size) {
3092 ret = mmc_blk_alloc_part(card, md,
3093 card->part[idx].part_cfg,
3094 card->part[idx].size >> 9,
3095 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003096 card->part[idx].name,
3097 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09003098 if (ret)
3099 return ret;
3100 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003101 }
3102
3103 return ret;
3104}
3105
Andrei Warkentin371a6892011-04-11 18:10:25 -05003106static void mmc_blk_remove_req(struct mmc_blk_data *md)
3107{
Johan Rudholmadd710e2011-12-02 08:51:06 +01003108 struct mmc_card *card;
3109
Andrei Warkentin371a6892011-04-11 18:10:25 -05003110 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003111 /*
3112 * Flush remaining requests and free queues. It
3113 * is freeing the queue that stops new requests
3114 * from being accepted.
3115 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02003116 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003117 mmc_cleanup_queue(&md->queue);
3118 if (md->flags & MMC_BLK_PACKED_CMD)
3119 mmc_packed_clean(&md->queue);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003120 device_remove_file(disk_to_dev(md->disk),
3121 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003122 if (md->disk->flags & GENHD_FL_UP) {
3123 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003124 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3125 card->ext_csd.boot_ro_lockable)
3126 device_remove_file(disk_to_dev(md->disk),
3127 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08003128#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3129 device_remove_file(disk_to_dev(md->disk),
3130 &dev_attr_max_write_speed);
3131 device_remove_file(disk_to_dev(md->disk),
3132 &dev_attr_max_read_speed);
3133 device_remove_file(disk_to_dev(md->disk),
3134 &dev_attr_cache_size);
3135#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05003136
Andrei Warkentin371a6892011-04-11 18:10:25 -05003137 del_gendisk(md->disk);
3138 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003139 mmc_blk_put(md);
3140 }
3141}
3142
3143static void mmc_blk_remove_parts(struct mmc_card *card,
3144 struct mmc_blk_data *md)
3145{
3146 struct list_head *pos, *q;
3147 struct mmc_blk_data *part_md;
3148
3149 list_for_each_safe(pos, q, &md->part) {
3150 part_md = list_entry(pos, struct mmc_blk_data, part);
3151 list_del(pos);
3152 mmc_blk_remove_req(part_md);
3153 }
3154}
3155
3156static int mmc_add_disk(struct mmc_blk_data *md)
3157{
3158 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003159 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003160
Dan Williams307d8e62016-06-20 10:40:44 -07003161 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003162 md->force_ro.show = force_ro_show;
3163 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05303164 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003165 md->force_ro.attr.name = "force_ro";
3166 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
3167 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
3168 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01003169 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003170#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3171 atomic_set(&md->queue.max_write_speed, max_write_speed);
3172 ret = device_create_file(disk_to_dev(md->disk),
3173 &dev_attr_max_write_speed);
3174 if (ret)
3175 goto max_write_speed_fail;
3176 atomic_set(&md->queue.max_read_speed, max_read_speed);
3177 ret = device_create_file(disk_to_dev(md->disk),
3178 &dev_attr_max_read_speed);
3179 if (ret)
3180 goto max_read_speed_fail;
3181 atomic_set(&md->queue.cache_size, cache_size);
3182 atomic_long_set(&md->queue.cache_used, 0);
3183 md->queue.cache_jiffies = jiffies;
3184 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3185 if (ret)
3186 goto cache_size_fail;
3187#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003188
3189 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3190 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04003191 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003192
3193 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
3194 mode = S_IRUGO;
3195 else
3196 mode = S_IRUGO | S_IWUSR;
3197
3198 md->power_ro_lock.show = power_ro_lock_show;
3199 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01003200 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003201 md->power_ro_lock.attr.mode = mode;
3202 md->power_ro_lock.attr.name =
3203 "ro_lock_until_next_power_on";
3204 ret = device_create_file(disk_to_dev(md->disk),
3205 &md->power_ro_lock);
3206 if (ret)
3207 goto power_ro_lock_fail;
3208 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003209
3210 md->num_wr_reqs_to_start_packing.show =
3211 num_wr_reqs_to_start_packing_show;
3212 md->num_wr_reqs_to_start_packing.store =
3213 num_wr_reqs_to_start_packing_store;
3214 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
3215 md->num_wr_reqs_to_start_packing.attr.name =
3216 "num_wr_reqs_to_start_packing";
3217 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
3218 ret = device_create_file(disk_to_dev(md->disk),
3219 &md->num_wr_reqs_to_start_packing);
3220 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02003221 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003222
Maya Erez5a8dae12014-12-04 15:13:59 +02003223 md->no_pack_for_random.show = no_pack_for_random_show;
3224 md->no_pack_for_random.store = no_pack_for_random_store;
3225 sysfs_attr_init(&md->no_pack_for_random.attr);
3226 md->no_pack_for_random.attr.name = "no_pack_for_random";
3227 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
3228 ret = device_create_file(disk_to_dev(md->disk),
3229 &md->no_pack_for_random);
3230 if (ret)
3231 goto no_pack_for_random_fails;
3232
Johan Rudholmadd710e2011-12-02 08:51:06 +01003233 return ret;
3234
Maya Erez5a8dae12014-12-04 15:13:59 +02003235no_pack_for_random_fails:
3236 device_remove_file(disk_to_dev(md->disk),
3237 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02003238num_wr_reqs_to_start_packing_fail:
3239 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003240power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08003241#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3242 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3243cache_size_fail:
3244 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
3245max_read_speed_fail:
3246 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
3247max_write_speed_fail:
3248#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003249 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
3250force_ro_fail:
3251 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003252
3253 return ret;
3254}
3255
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003256static const struct mmc_fixup blk_fixups[] =
3257{
Chris Ballc59d4472011-11-11 22:01:43 -05003258 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
3259 MMC_QUIRK_INAND_CMD38),
3260 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
3261 MMC_QUIRK_INAND_CMD38),
3262 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
3263 MMC_QUIRK_INAND_CMD38),
3264 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
3265 MMC_QUIRK_INAND_CMD38),
3266 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
3267 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003268
3269 /*
3270 * Some MMC cards experience performance degradation with CMD23
3271 * instead of CMD12-bounded multiblock transfers. For now we'll
3272 * black list what's bad...
3273 * - Certain Toshiba cards.
3274 *
3275 * N.B. This doesn't affect SD cards.
3276 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08003277 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3278 MMC_QUIRK_BLK_NO_CMD23),
3279 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3280 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003281 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003282 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003283 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003284 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003285 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003286 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003287
3288 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03003289 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003290 */
Chris Ballc59d4472011-11-11 22:01:43 -05003291 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003292 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03003293 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
3294 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003295
Ian Chen3550ccd2012-08-29 15:05:36 +09003296 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08003297 * Some Samsung MMC cards need longer data read timeout than
3298 * indicated in CSD.
3299 */
3300 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
3301 MMC_QUIRK_LONG_READ_TIME),
3302
3303 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09003304 * On these Samsung MoviNAND parts, performing secure erase or
3305 * secure trim can result in unrecoverable corruption due to a
3306 * firmware bug.
3307 */
3308 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3309 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3310 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3311 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3312 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3313 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3314 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3315 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3316 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3317 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3318 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3319 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3320 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3321 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3322 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3323 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3324
Shawn Linb5b4ff02015-08-12 13:08:32 +08003325 /*
3326 * On Some Kingston eMMCs, performing trim can result in
3327 * unrecoverable data conrruption occasionally due to a firmware bug.
3328 */
3329 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3330 MMC_QUIRK_TRIM_BROKEN),
3331 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3332 MMC_QUIRK_TRIM_BROKEN),
3333
Pratibhasagar V8d664e32014-12-03 18:26:42 +02003334 /* Some INAND MCP devices advertise incorrect timeout values */
3335 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
3336 MMC_QUIRK_INAND_DATA_TIMEOUT),
3337
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003338 END_FIXUP
3339};
3340
Ulf Hansson96541ba2015-04-14 13:06:12 +02003341static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003343 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003344 char cap_str[10];
3345
Pierre Ossman912490d2005-05-21 10:27:02 +01003346 /*
3347 * Check that the card supports the command class(es) we need.
3348 */
3349 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 return -ENODEV;
3351
Lukas Czerner5204d002014-06-18 13:18:07 +02003352 mmc_fixup_device(card, blk_fixups);
3353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 md = mmc_blk_alloc(card);
3355 if (IS_ERR(md))
3356 return PTR_ERR(md);
3357
James Bottomleyb9f28d82015-03-05 18:47:01 -08003358 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003359 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303360 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003362 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
Andrei Warkentin371a6892011-04-11 18:10:25 -05003364 if (mmc_blk_alloc_parts(card, md))
3365 goto out;
3366
Ulf Hansson96541ba2015-04-14 13:06:12 +02003367 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003368
Andrei Warkentin371a6892011-04-11 18:10:25 -05003369 if (mmc_add_disk(md))
3370 goto out;
3371
3372 list_for_each_entry(part_md, &md->part, part) {
3373 if (mmc_add_disk(part_md))
3374 goto out;
3375 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003376
3377 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
3378 pm_runtime_use_autosuspend(&card->dev);
3379
3380 /*
3381 * Don't enable runtime PM for SD-combo cards here. Leave that
3382 * decision to be taken during the SDIO init sequence instead.
3383 */
3384 if (card->type != MMC_TYPE_SD_COMBO) {
3385 pm_runtime_set_active(&card->dev);
3386 pm_runtime_enable(&card->dev);
3387 }
3388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 return 0;
3390
3391 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05003392 mmc_blk_remove_parts(card, md);
3393 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01003394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
Ulf Hansson96541ba2015-04-14 13:06:12 +02003397static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003399 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
Andrei Warkentin371a6892011-04-11 18:10:25 -05003401 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003402 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003403 mmc_claim_host(card->host);
3404 mmc_blk_part_switch(card, md);
3405 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003406 if (card->type != MMC_TYPE_SD_COMBO)
3407 pm_runtime_disable(&card->dev);
3408 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003409 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02003410 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411}
3412
Ulf Hansson96541ba2015-04-14 13:06:12 +02003413static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003415 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02003416 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303417 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
3419 if (md) {
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303420 rc = mmc_queue_suspend(&md->queue);
3421 if (rc)
3422 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003423 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303424 rc = mmc_queue_suspend(&part_md->queue);
3425 if (rc)
3426 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303429 goto out;
3430
3431 out_resume:
3432 mmc_queue_resume(&md->queue);
3433 list_for_each_entry(part_md, &md->part, part) {
3434 mmc_queue_resume(&part_md->queue);
3435 }
3436 out:
3437 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438}
3439
Ulf Hansson96541ba2015-04-14 13:06:12 +02003440static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02003441{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003442 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003443}
3444
Ulf Hansson0967edc2014-10-06 11:29:42 +02003445#ifdef CONFIG_PM_SLEEP
3446static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02003447{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003448 struct mmc_card *card = mmc_dev_to_card(dev);
3449
3450 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003451}
3452
Ulf Hansson0967edc2014-10-06 11:29:42 +02003453static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003455 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02003456 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
3458 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003459 /*
3460 * Resume involves the card going into idle state,
3461 * so current partition is always the main one.
3462 */
3463 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003465 list_for_each_entry(part_md, &md->part, part) {
3466 mmc_queue_resume(&part_md->queue);
3467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 }
3469 return 0;
3470}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471#endif
3472
Ulf Hansson0967edc2014-10-06 11:29:42 +02003473static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3474
Ulf Hansson96541ba2015-04-14 13:06:12 +02003475static struct mmc_driver mmc_driver = {
3476 .drv = {
3477 .name = "mmcblk",
3478 .pm = &mmc_blk_pm_ops,
3479 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 .probe = mmc_blk_probe,
3481 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02003482 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483};
3484
3485static int __init mmc_blk_init(void)
3486{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003487 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003489 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3490 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3491
Ben Hutchingsa26eba62014-11-06 03:35:09 +00003492 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003493
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003494 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3495 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003498 res = mmc_register_driver(&mmc_driver);
3499 if (res)
3500 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003502 return 0;
3503 out2:
3504 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 out:
3506 return res;
3507}
3508
3509static void __exit mmc_blk_exit(void)
3510{
3511 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003512 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513}
3514
3515module_init(mmc_blk_init);
3516module_exit(mmc_blk_exit);
3517
3518MODULE_LICENSE("GPL");
3519MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3520