blob: f9fa2e6bb1e6c9bc5b368cd24b7869e04605ee45 [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
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002192static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2193 struct request *req)
2194{
2195 struct mmc_host *host = mq->card->host;
2196 int data_dir;
2197
2198 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2199 return;
2200
Maya Erez8e2b3c32012-12-02 13:27:15 +02002201 /* Support for the write packing on eMMC 4.5 or later */
2202 if (mq->card->ext_csd.rev <= 5)
2203 return;
2204
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002205 /*
2206 * In case the packing control is not supported by the host, it should
2207 * not have an effect on the write packing. Therefore we have to enable
2208 * the write packing
2209 */
2210 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2211 mq->wr_packing_enabled = true;
2212 return;
2213 }
2214
2215 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2216 if (mq->num_of_potential_packed_wr_reqs >
2217 mq->num_wr_reqs_to_start_packing)
2218 mq->wr_packing_enabled = true;
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002219 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002220 return;
2221 }
2222
2223 data_dir = rq_data_dir(req);
2224
2225 if (data_dir == READ) {
2226 mq->num_of_potential_packed_wr_reqs = 0;
2227 mq->wr_packing_enabled = false;
2228 return;
2229 } else if (data_dir == WRITE) {
2230 mq->num_of_potential_packed_wr_reqs++;
2231 }
2232
2233 if (mq->num_of_potential_packed_wr_reqs >
2234 mq->num_wr_reqs_to_start_packing)
2235 mq->wr_packing_enabled = true;
2236}
2237
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002238struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2239{
2240 if (!card)
2241 return NULL;
2242
2243 return &card->wr_pack_stats;
2244}
2245EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2246
2247void mmc_blk_init_packed_statistics(struct mmc_card *card)
2248{
2249 int max_num_of_packed_reqs = 0;
2250
2251 if (!card || !card->wr_pack_stats.packing_events)
2252 return;
2253
2254 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2255
2256 spin_lock(&card->wr_pack_stats.lock);
2257 memset(card->wr_pack_stats.packing_events, 0,
2258 (max_num_of_packed_reqs + 1) *
2259 sizeof(*card->wr_pack_stats.packing_events));
2260 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2261 sizeof(card->wr_pack_stats.pack_stop_reason));
2262 card->wr_pack_stats.enabled = true;
2263 spin_unlock(&card->wr_pack_stats.lock);
2264}
2265EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2266
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002267static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2268{
2269 struct request_queue *q = mq->queue;
2270 struct mmc_card *card = mq->card;
2271 struct request *cur = req, *next = NULL;
2272 struct mmc_blk_data *md = mq->data;
2273 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2274 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2275 unsigned int req_sectors = 0, phys_segments = 0;
2276 unsigned int max_blk_count, max_phys_segs;
2277 bool put_back = true;
2278 u8 max_packed_rw = 0;
2279 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002280 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002281
Shawn Lin96e52da2016-08-26 08:49:55 +08002282 /*
2283 * We don't need to check packed for any further
2284 * operation of packed stuff as we set MMC_PACKED_NONE
2285 * and return zero for reqs if geting null packed. Also
2286 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2287 * it again when removing blk req.
2288 */
2289 if (!mqrq->packed) {
2290 md->flags &= (~MMC_BLK_PACKED_CMD);
2291 goto no_packed;
2292 }
2293
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002294 if (!(md->flags & MMC_BLK_PACKED_CMD))
2295 goto no_packed;
2296
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002297 if (!mq->wr_packing_enabled)
2298 goto no_packed;
2299
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002300 if ((rq_data_dir(cur) == WRITE) &&
2301 mmc_host_packed_wr(card->host))
2302 max_packed_rw = card->ext_csd.max_packed_writes;
2303
2304 if (max_packed_rw == 0)
2305 goto no_packed;
2306
2307 if (mmc_req_rel_wr(cur) &&
2308 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2309 goto no_packed;
2310
2311 if (mmc_large_sector(card) &&
2312 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2313 goto no_packed;
2314
2315 mmc_blk_clear_packed(mqrq);
2316
2317 max_blk_count = min(card->host->max_blk_count,
2318 card->host->max_req_size >> 9);
2319 if (unlikely(max_blk_count > 0xffff))
2320 max_blk_count = 0xffff;
2321
2322 max_phys_segs = queue_max_segments(q);
2323 req_sectors += blk_rq_sectors(cur);
2324 phys_segments += cur->nr_phys_segments;
2325
2326 if (rq_data_dir(cur) == WRITE) {
2327 req_sectors += mmc_large_sector(card) ? 8 : 1;
2328 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2329 }
2330
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002331 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002332 do {
2333 if (reqs >= max_packed_rw - 1) {
2334 put_back = false;
2335 break;
2336 }
2337
2338 spin_lock_irq(q->queue_lock);
2339 next = blk_fetch_request(q);
2340 spin_unlock_irq(q->queue_lock);
2341 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002342 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002343 put_back = false;
2344 break;
2345 }
2346
2347 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002348 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2349 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002350 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002351 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002352
Mike Christie3a5e02c2016-06-05 14:32:23 -05002353 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002354 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002355 req_op(next) == REQ_OP_FLUSH) {
2356 if (req_op(next) != REQ_OP_SECURE_ERASE)
2357 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002358 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002359 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002360
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002361 if (rq_data_dir(cur) != rq_data_dir(next)) {
2362 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002363 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002364 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002365
2366 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002367 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2368 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002369 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002370 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002371
2372 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002373 if (req_sectors > max_blk_count) {
2374 if (stats->enabled)
2375 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002376 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002377 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002378
2379 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002380 if (phys_segments > max_phys_segs) {
2381 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002382 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002383 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002384
Maya Erez5a8dae12014-12-04 15:13:59 +02002385 if (mq->no_pack_for_random) {
2386 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2387 blk_rq_pos(next)) {
2388 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2389 put_back = 1;
2390 break;
2391 }
2392 }
2393
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002394 if (rq_data_dir(next) == WRITE)
2395 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002396 list_add_tail(&next->queuelist, &mqrq->packed->list);
2397 cur = next;
2398 reqs++;
2399 } while (1);
2400
2401 if (put_back) {
2402 spin_lock_irq(q->queue_lock);
2403 blk_requeue_request(q, next);
2404 spin_unlock_irq(q->queue_lock);
2405 }
2406
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002407 if (stats->enabled) {
2408 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2409 stats->packing_events[reqs + 1]++;
2410 if (reqs + 1 == max_packed_rw)
2411 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2412 }
2413
2414 spin_unlock(&stats->lock);
2415
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002416 if (reqs > 0) {
2417 list_add(&req->queuelist, &mqrq->packed->list);
2418 mqrq->packed->nr_entries = ++reqs;
2419 mqrq->packed->retries = reqs;
2420 return reqs;
2421 }
2422
2423no_packed:
2424 mqrq->cmd_type = MMC_PACKED_NONE;
2425 return 0;
2426}
2427
2428static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2429 struct mmc_card *card,
2430 struct mmc_queue *mq)
2431{
2432 struct mmc_blk_request *brq = &mqrq->brq;
2433 struct request *req = mqrq->req;
2434 struct request *prq;
2435 struct mmc_blk_data *md = mq->data;
2436 struct mmc_packed *packed = mqrq->packed;
2437 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002438 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002439 u8 hdr_blocks;
2440 u8 i = 1;
2441
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002442 mqrq->cmd_type = MMC_PACKED_WRITE;
2443 packed->blocks = 0;
2444 packed->idx_failure = MMC_PACKED_NR_IDX;
2445
2446 packed_cmd_hdr = packed->cmd_hdr;
2447 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002448 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2449 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002450 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2451
2452 /*
2453 * Argument for each entry of packed group
2454 */
2455 list_for_each_entry(prq, &packed->list, queuelist) {
2456 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2457 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2458 (prq->cmd_flags & REQ_META) &&
2459 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002460 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002461 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002462 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002463 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2464 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002465 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002466 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002467 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002468 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002469 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002470 packed->blocks += blk_rq_sectors(prq);
2471 i++;
2472 }
2473
2474 memset(brq, 0, sizeof(struct mmc_blk_request));
2475 brq->mrq.cmd = &brq->cmd;
2476 brq->mrq.data = &brq->data;
2477 brq->mrq.sbc = &brq->sbc;
2478 brq->mrq.stop = &brq->stop;
2479
2480 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2481 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2482 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2483
2484 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2485 brq->cmd.arg = blk_rq_pos(req);
2486 if (!mmc_card_blockaddr(card))
2487 brq->cmd.arg <<= 9;
2488 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2489
2490 brq->data.blksz = 512;
2491 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002492 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302493 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002494
2495 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2496 brq->stop.arg = 0;
2497 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2498
2499 mmc_set_data_timeout(&brq->data, card);
2500
2501 brq->data.sg = mqrq->sg;
2502 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2503
2504 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002505
2506 /*
2507 * This is intended for packed commands tests usage - in case these
2508 * functions are not in use the respective pointers are NULL
2509 */
2510 if (mq->err_check_fn)
2511 mqrq->mmc_active.err_check = mq->err_check_fn;
2512 else
2513 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2514
2515 if (mq->packed_test_fn)
2516 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002517
2518 mmc_queue_bounce_pre(mqrq);
2519}
2520
Adrian Hunter67716322011-08-29 16:42:15 +03002521static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2522 struct mmc_blk_request *brq, struct request *req,
2523 int ret)
2524{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002525 struct mmc_queue_req *mq_rq;
2526 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2527
Adrian Hunter67716322011-08-29 16:42:15 +03002528 /*
2529 * If this is an SD card and we're writing, we can first
2530 * mark the known good sectors as ok.
2531 *
2532 * If the card is not SD, we can still ok written sectors
2533 * as reported by the controller (which might be less than
2534 * the real number of written sectors, but never more).
2535 */
2536 if (mmc_card_sd(card)) {
2537 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302538 if (!brq->data.fault_injected) {
2539 blocks = mmc_sd_num_wr_blocks(card);
2540 if (blocks != (u32)-1)
2541 ret = blk_end_request(req, 0, blocks << 9);
2542 } else
2543 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002544 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002545 if (!mmc_packed_cmd(mq_rq->cmd_type))
2546 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002547 }
2548 return ret;
2549}
2550
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002551static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2552{
2553 struct request *prq;
2554 struct mmc_packed *packed = mq_rq->packed;
2555 int idx = packed->idx_failure, i = 0;
2556 int ret = 0;
2557
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002558 while (!list_empty(&packed->list)) {
2559 prq = list_entry_rq(packed->list.next);
2560 if (idx == i) {
2561 /* retry from error index */
2562 packed->nr_entries -= idx;
2563 mq_rq->req = prq;
2564 ret = 1;
2565
2566 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2567 list_del_init(&prq->queuelist);
2568 mmc_blk_clear_packed(mq_rq);
2569 }
2570 return ret;
2571 }
2572 list_del_init(&prq->queuelist);
2573 blk_end_request(prq, 0, blk_rq_bytes(prq));
2574 i++;
2575 }
2576
2577 mmc_blk_clear_packed(mq_rq);
2578 return ret;
2579}
2580
2581static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2582{
2583 struct request *prq;
2584 struct mmc_packed *packed = mq_rq->packed;
2585
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002586 while (!list_empty(&packed->list)) {
2587 prq = list_entry_rq(packed->list.next);
2588 list_del_init(&prq->queuelist);
2589 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2590 }
2591
2592 mmc_blk_clear_packed(mq_rq);
2593}
2594
2595static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2596 struct mmc_queue_req *mq_rq)
2597{
2598 struct request *prq;
2599 struct request_queue *q = mq->queue;
2600 struct mmc_packed *packed = mq_rq->packed;
2601
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002602 while (!list_empty(&packed->list)) {
2603 prq = list_entry_rq(packed->list.prev);
2604 if (prq->queuelist.prev != &packed->list) {
2605 list_del_init(&prq->queuelist);
2606 spin_lock_irq(q->queue_lock);
2607 blk_requeue_request(mq->queue, prq);
2608 spin_unlock_irq(q->queue_lock);
2609 } else {
2610 list_del_init(&prq->queuelist);
2611 }
2612 }
2613
2614 mmc_blk_clear_packed(mq_rq);
2615}
2616
Per Forlinee8a43a2011-07-01 18:55:33 +02002617static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02002618{
2619 struct mmc_blk_data *md = mq->data;
2620 struct mmc_card *card = md->queue.card;
2621 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002622 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002623 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002624 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302625 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02002626 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002627 const u8 packed_nr = 2;
2628 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002629#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2630 unsigned long waitfor = jiffies;
2631#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02002632
2633 if (!rqc && !mq->mqrq_prev->req)
2634 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002635
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002636 if (rqc)
2637 reqs = mmc_blk_prep_packed_list(mq, rqc);
2638
Per Forlin54d49d72011-07-01 18:55:29 +02002639 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002640 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302641 /*
2642 * When 4KB native sector is enabled, only 8 blocks
2643 * multiple read or write is allowed
2644 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00002645 if (mmc_large_sector(card) &&
2646 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302647 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
2648 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002649 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302650 goto cmd_abort;
2651 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002652
2653 if (reqs >= packed_nr)
2654 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2655 card, mq);
2656 else
2657 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002658 areq = &mq->mqrq_cur->mmc_active;
2659 } else
2660 areq = NULL;
2661 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002662 if (!areq) {
2663 if (status == MMC_BLK_NEW_REQUEST)
2664 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02002665 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002666 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002667
Per Forlinee8a43a2011-07-01 18:55:33 +02002668 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2669 brq = &mq_rq->brq;
2670 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002671 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002672 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002673
Per Forlind78d4a82011-07-01 18:55:30 +02002674 switch (status) {
2675 case MMC_BLK_SUCCESS:
2676 case MMC_BLK_PARTIAL:
2677 /*
2678 * A block was successfully transferred.
2679 */
Adrian Hunter67716322011-08-29 16:42:15 +03002680 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002681
Mark Salyzyn6904e432016-01-28 11:12:25 -08002682 mmc_blk_simulate_delay(mq, rqc, waitfor);
2683
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002684 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2685 ret = mmc_blk_end_packed_req(mq_rq);
2686 break;
2687 } else {
2688 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002689 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002690 }
2691
Adrian Hunter67716322011-08-29 16:42:15 +03002692 /*
2693 * If the blk_end_request function returns non-zero even
2694 * though all data has been transferred and no errors
2695 * were returned by the host controller, it's a bug.
2696 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002697 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302698 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002699 __func__, blk_rq_bytes(req),
2700 brq->data.bytes_xfered);
2701 rqc = NULL;
2702 goto cmd_abort;
2703 }
Per Forlind78d4a82011-07-01 18:55:30 +02002704 break;
2705 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002706 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002707 if (mmc_blk_reset(md, card->host, type))
2708 goto cmd_abort;
2709 if (!ret)
2710 goto start_new_req;
2711 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002712 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002713 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a82011-07-01 18:55:30 +02002714 if (retry++ < 5)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002715 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002716 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002717 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002718 if (!mmc_blk_reset(md, card->host, type))
2719 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002720 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002721 case MMC_BLK_DATA_ERR: {
2722 int err;
2723
2724 err = mmc_blk_reset(md, card->host, type);
2725 if (!err)
2726 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05302727 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002728 }
2729 case MMC_BLK_ECC_ERR:
2730 if (brq->data.blocks > 1) {
2731 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002732 pr_warn("%s: retrying using single block read\n",
2733 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002734 disable_multi = 1;
2735 break;
2736 }
Per Forlind78d4a82011-07-01 18:55:30 +02002737 /*
2738 * After an error, we redo I/O one sector at a
2739 * time, so we only reach here after trying to
2740 * read a single sector.
2741 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302742 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002743 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002744 if (!ret)
2745 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002746 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302747 case MMC_BLK_NOMEDIUM:
2748 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002749 default:
2750 pr_err("%s: Unhandled return value (%d)",
2751 req->rq_disk->disk_name, status);
2752 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002753 }
2754
Per Forlinee8a43a2011-07-01 18:55:33 +02002755 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002756 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2757 if (!mq_rq->packed->retries)
2758 goto cmd_abort;
2759 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2760 mmc_start_req(card->host,
2761 &mq_rq->mmc_active, NULL);
2762 } else {
2763
2764 /*
2765 * In case of a incomplete request
2766 * prepare it again and resend.
2767 */
2768 mmc_blk_rw_rq_prep(mq_rq, card,
2769 disable_multi, mq);
2770 mmc_start_req(card->host,
2771 &mq_rq->mmc_active, NULL);
2772 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002773 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 } while (ret);
2776
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 return 1;
2778
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002779 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002780 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2781 mmc_blk_abort_packed_req(mq_rq);
2782 } else {
2783 if (mmc_card_removed(card))
2784 req->cmd_flags |= REQ_QUIET;
2785 while (ret)
2786 ret = blk_end_request(req, -EIO,
2787 blk_rq_cur_bytes(req));
2788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Per Forlinee8a43a2011-07-01 18:55:33 +02002790 start_new_req:
2791 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002792 if (mmc_card_removed(card)) {
2793 rqc->cmd_flags |= REQ_QUIET;
2794 blk_end_request_all(rqc, -EIO);
2795 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002796 /*
2797 * If current request is packed, it needs to put back.
2798 */
2799 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2800 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2801
Seungwon Jeon7a819022013-01-22 19:48:07 +09002802 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2803 mmc_start_req(card->host,
2804 &mq->mqrq_cur->mmc_active, NULL);
2805 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002806 }
2807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 return 0;
2809}
2810
Linus Walleij29eb7bd2016-09-20 11:34:38 +02002811int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002812{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002813 int ret;
2814 struct mmc_blk_data *md = mq->data;
2815 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002816 struct mmc_host *host = card->host;
2817 unsigned long flags;
Adrian Hunter869c5542016-08-25 14:11:43 -06002818 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002819
Per Forlinee8a43a2011-07-01 18:55:33 +02002820 if (req && !mq->mqrq_prev->req)
2821 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002822 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002823
Andrei Warkentin371a6892011-04-11 18:10:25 -05002824 ret = mmc_blk_part_switch(card, md);
2825 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002826 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302827 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002828 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002829 ret = 0;
2830 goto out;
2831 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002832
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002833 mmc_blk_write_packing_control(mq, req);
2834
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002835 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05002836 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002837 /* complete ongoing async transfer before issuing discard */
2838 if (card->host->areq)
2839 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02002840 ret = mmc_blk_issue_discard_rq(mq, req);
2841 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
2842 /* complete ongoing async transfer before issuing secure erase*/
2843 if (card->host->areq)
2844 mmc_blk_issue_rw_rq(mq, NULL);
2845 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05002846 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002847 /* complete ongoing async transfer before issuing flush */
2848 if (card->host->areq)
2849 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002850 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002851 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002852 if (!req && host->areq) {
2853 spin_lock_irqsave(&host->context_info.lock, flags);
2854 host->context_info.is_waiting_last_req = true;
2855 spin_unlock_irqrestore(&host->context_info.lock, flags);
2856 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002857 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002858 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002859
Andrei Warkentin371a6892011-04-11 18:10:25 -05002860out:
Adrian Hunter869c5542016-08-25 14:11:43 -06002861 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002862 /*
2863 * Release host when there are no more requests
2864 * and after special request(discard, flush) is done.
2865 * In case sepecial request, there is no reentry to
2866 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2867 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002868 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002869 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002870}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Russell Kinga6f6c962006-01-03 22:38:44 +00002872static inline int mmc_blk_readonly(struct mmc_card *card)
2873{
2874 return mmc_card_readonly(card) ||
2875 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2876}
2877
Andrei Warkentin371a6892011-04-11 18:10:25 -05002878static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2879 struct device *parent,
2880 sector_t size,
2881 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002882 const char *subname,
2883 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
2885 struct mmc_blk_data *md;
2886 int devidx, ret;
2887
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002888again:
2889 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
2890 return ERR_PTR(-ENOMEM);
2891
2892 spin_lock(&mmc_blk_lock);
2893 ret = ida_get_new(&mmc_blk_ida, &devidx);
2894 spin_unlock(&mmc_blk_lock);
2895
2896 if (ret == -EAGAIN)
2897 goto again;
2898 else if (ret)
2899 return ERR_PTR(ret);
2900
2901 if (devidx >= max_devices) {
2902 ret = -ENOSPC;
2903 goto out;
2904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002906 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002907 if (!md) {
2908 ret = -ENOMEM;
2909 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002911
Johan Rudholmadd710e2011-12-02 08:51:06 +01002912 md->area_type = area_type;
2913
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002914 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002915 * Set the read-only status based on the supported commands
2916 * and the write protect switch.
2917 */
2918 md->read_only = mmc_blk_readonly(card);
2919
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002920 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002921 if (md->disk == NULL) {
2922 ret = -ENOMEM;
2923 goto err_kfree;
2924 }
2925
2926 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002927 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002928 md->usage = 1;
2929
Adrian Hunterd09408a2011-06-23 13:40:28 +03002930 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002931 if (ret)
2932 goto err_putdisk;
2933
Russell Kinga6f6c962006-01-03 22:38:44 +00002934 md->queue.data = md;
2935
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002936 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002937 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002938 md->disk->fops = &mmc_bdops;
2939 md->disk->private_data = md;
2940 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002941 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002942 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002943 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002944 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002945 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002946
2947 /*
2948 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2949 *
2950 * - be set for removable media with permanent block devices
2951 * - be unset for removable block devices with permanent media
2952 *
2953 * Since MMC block devices clearly fall under the second
2954 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2955 * should use the block device creation/destruction hotplug
2956 * messages to tell when the card is present.
2957 */
2958
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002959 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02002960 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002961
Saugata Dasa5075eb2012-05-17 16:32:21 +05302962 if (mmc_card_mmc(card))
2963 blk_queue_logical_block_size(md->queue.queue,
2964 card->ext_csd.data_sector_size);
2965 else
2966 blk_queue_logical_block_size(md->queue.queue, 512);
2967
Andrei Warkentin371a6892011-04-11 18:10:25 -05002968 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002969
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002970 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02002971 if ((mmc_card_mmc(card) &&
2972 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002973 (mmc_card_sd(card) &&
2974 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2975 md->flags |= MMC_BLK_CMD23;
2976 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002977
2978 if (mmc_card_mmc(card) &&
2979 md->flags & MMC_BLK_CMD23 &&
2980 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2981 card->ext_csd.rel_sectors)) {
2982 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06002983 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002984 }
2985
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002986 if (mmc_card_mmc(card) &&
2987 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2988 (md->flags & MMC_BLK_CMD23) &&
2989 card->ext_csd.packed_event_en) {
2990 if (!mmc_packed_init(&md->queue, card))
2991 md->flags |= MMC_BLK_PACKED_CMD;
2992 }
2993
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002995
2996 err_putdisk:
2997 put_disk(md->disk);
2998 err_kfree:
2999 kfree(md);
3000 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003001 spin_lock(&mmc_blk_lock);
3002 ida_remove(&mmc_blk_ida, devidx);
3003 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00003004 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005}
3006
Andrei Warkentin371a6892011-04-11 18:10:25 -05003007static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
3008{
3009 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003010
3011 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
3012 /*
3013 * The EXT_CSD sector count is in number or 512 byte
3014 * sectors.
3015 */
3016 size = card->ext_csd.sectors;
3017 } else {
3018 /*
3019 * The CSD capacity field is in units of read_blkbits.
3020 * set_capacity takes units of 512 bytes.
3021 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00003022 size = (typeof(sector_t))card->csd.capacity
3023 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003024 }
3025
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01003026 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003027 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003028}
3029
3030static int mmc_blk_alloc_part(struct mmc_card *card,
3031 struct mmc_blk_data *md,
3032 unsigned int part_type,
3033 sector_t size,
3034 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003035 const char *subname,
3036 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05003037{
3038 char cap_str[10];
3039 struct mmc_blk_data *part_md;
3040
3041 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003042 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003043 if (IS_ERR(part_md))
3044 return PTR_ERR(part_md);
3045 part_md->part_type = part_type;
3046 list_add(&part_md->part, &md->part);
3047
James Bottomleyb9f28d82015-03-05 18:47:01 -08003048 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05003049 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303050 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05003051 part_md->disk->disk_name, mmc_card_id(card),
3052 mmc_card_name(card), part_md->part_type, cap_str);
3053 return 0;
3054}
3055
Namjae Jeone0c368d2011-10-06 23:41:38 +09003056/* MMC Physical partitions consist of two boot partitions and
3057 * up to four general purpose partitions.
3058 * For each partition enabled in EXT_CSD a block device will be allocatedi
3059 * to provide access to the partition.
3060 */
3061
Andrei Warkentin371a6892011-04-11 18:10:25 -05003062static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
3063{
Namjae Jeone0c368d2011-10-06 23:41:38 +09003064 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003065
3066 if (!mmc_card_mmc(card))
3067 return 0;
3068
Namjae Jeone0c368d2011-10-06 23:41:38 +09003069 for (idx = 0; idx < card->nr_parts; idx++) {
3070 if (card->part[idx].size) {
3071 ret = mmc_blk_alloc_part(card, md,
3072 card->part[idx].part_cfg,
3073 card->part[idx].size >> 9,
3074 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003075 card->part[idx].name,
3076 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09003077 if (ret)
3078 return ret;
3079 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003080 }
3081
3082 return ret;
3083}
3084
Andrei Warkentin371a6892011-04-11 18:10:25 -05003085static void mmc_blk_remove_req(struct mmc_blk_data *md)
3086{
Johan Rudholmadd710e2011-12-02 08:51:06 +01003087 struct mmc_card *card;
3088
Andrei Warkentin371a6892011-04-11 18:10:25 -05003089 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003090 /*
3091 * Flush remaining requests and free queues. It
3092 * is freeing the queue that stops new requests
3093 * from being accepted.
3094 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02003095 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003096 mmc_cleanup_queue(&md->queue);
3097 if (md->flags & MMC_BLK_PACKED_CMD)
3098 mmc_packed_clean(&md->queue);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003099 device_remove_file(disk_to_dev(md->disk),
3100 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003101 if (md->disk->flags & GENHD_FL_UP) {
3102 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003103 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3104 card->ext_csd.boot_ro_lockable)
3105 device_remove_file(disk_to_dev(md->disk),
3106 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08003107#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3108 device_remove_file(disk_to_dev(md->disk),
3109 &dev_attr_max_write_speed);
3110 device_remove_file(disk_to_dev(md->disk),
3111 &dev_attr_max_read_speed);
3112 device_remove_file(disk_to_dev(md->disk),
3113 &dev_attr_cache_size);
3114#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05003115
Andrei Warkentin371a6892011-04-11 18:10:25 -05003116 del_gendisk(md->disk);
3117 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003118 mmc_blk_put(md);
3119 }
3120}
3121
3122static void mmc_blk_remove_parts(struct mmc_card *card,
3123 struct mmc_blk_data *md)
3124{
3125 struct list_head *pos, *q;
3126 struct mmc_blk_data *part_md;
3127
3128 list_for_each_safe(pos, q, &md->part) {
3129 part_md = list_entry(pos, struct mmc_blk_data, part);
3130 list_del(pos);
3131 mmc_blk_remove_req(part_md);
3132 }
3133}
3134
3135static int mmc_add_disk(struct mmc_blk_data *md)
3136{
3137 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003138 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003139
Dan Williams307d8e62016-06-20 10:40:44 -07003140 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003141 md->force_ro.show = force_ro_show;
3142 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05303143 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003144 md->force_ro.attr.name = "force_ro";
3145 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
3146 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
3147 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01003148 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003149#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3150 atomic_set(&md->queue.max_write_speed, max_write_speed);
3151 ret = device_create_file(disk_to_dev(md->disk),
3152 &dev_attr_max_write_speed);
3153 if (ret)
3154 goto max_write_speed_fail;
3155 atomic_set(&md->queue.max_read_speed, max_read_speed);
3156 ret = device_create_file(disk_to_dev(md->disk),
3157 &dev_attr_max_read_speed);
3158 if (ret)
3159 goto max_read_speed_fail;
3160 atomic_set(&md->queue.cache_size, cache_size);
3161 atomic_long_set(&md->queue.cache_used, 0);
3162 md->queue.cache_jiffies = jiffies;
3163 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3164 if (ret)
3165 goto cache_size_fail;
3166#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003167
3168 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3169 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04003170 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003171
3172 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
3173 mode = S_IRUGO;
3174 else
3175 mode = S_IRUGO | S_IWUSR;
3176
3177 md->power_ro_lock.show = power_ro_lock_show;
3178 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01003179 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003180 md->power_ro_lock.attr.mode = mode;
3181 md->power_ro_lock.attr.name =
3182 "ro_lock_until_next_power_on";
3183 ret = device_create_file(disk_to_dev(md->disk),
3184 &md->power_ro_lock);
3185 if (ret)
3186 goto power_ro_lock_fail;
3187 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003188
3189 md->num_wr_reqs_to_start_packing.show =
3190 num_wr_reqs_to_start_packing_show;
3191 md->num_wr_reqs_to_start_packing.store =
3192 num_wr_reqs_to_start_packing_store;
3193 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
3194 md->num_wr_reqs_to_start_packing.attr.name =
3195 "num_wr_reqs_to_start_packing";
3196 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
3197 ret = device_create_file(disk_to_dev(md->disk),
3198 &md->num_wr_reqs_to_start_packing);
3199 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02003200 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003201
Maya Erez5a8dae12014-12-04 15:13:59 +02003202 md->no_pack_for_random.show = no_pack_for_random_show;
3203 md->no_pack_for_random.store = no_pack_for_random_store;
3204 sysfs_attr_init(&md->no_pack_for_random.attr);
3205 md->no_pack_for_random.attr.name = "no_pack_for_random";
3206 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
3207 ret = device_create_file(disk_to_dev(md->disk),
3208 &md->no_pack_for_random);
3209 if (ret)
3210 goto no_pack_for_random_fails;
3211
Johan Rudholmadd710e2011-12-02 08:51:06 +01003212 return ret;
3213
Maya Erez5a8dae12014-12-04 15:13:59 +02003214no_pack_for_random_fails:
3215 device_remove_file(disk_to_dev(md->disk),
3216 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02003217num_wr_reqs_to_start_packing_fail:
3218 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003219power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08003220#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3221 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3222cache_size_fail:
3223 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
3224max_read_speed_fail:
3225 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
3226max_write_speed_fail:
3227#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003228 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
3229force_ro_fail:
3230 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003231
3232 return ret;
3233}
3234
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003235static const struct mmc_fixup blk_fixups[] =
3236{
Chris Ballc59d4472011-11-11 22:01:43 -05003237 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
3238 MMC_QUIRK_INAND_CMD38),
3239 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
3240 MMC_QUIRK_INAND_CMD38),
3241 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
3242 MMC_QUIRK_INAND_CMD38),
3243 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
3244 MMC_QUIRK_INAND_CMD38),
3245 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
3246 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003247
3248 /*
3249 * Some MMC cards experience performance degradation with CMD23
3250 * instead of CMD12-bounded multiblock transfers. For now we'll
3251 * black list what's bad...
3252 * - Certain Toshiba cards.
3253 *
3254 * N.B. This doesn't affect SD cards.
3255 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08003256 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3257 MMC_QUIRK_BLK_NO_CMD23),
3258 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3259 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003260 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003261 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003262 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003263 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003264 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003265 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003266
3267 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03003268 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003269 */
Chris Ballc59d4472011-11-11 22:01:43 -05003270 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003271 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03003272 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
3273 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003274
Ian Chen3550ccd2012-08-29 15:05:36 +09003275 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08003276 * Some Samsung MMC cards need longer data read timeout than
3277 * indicated in CSD.
3278 */
3279 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
3280 MMC_QUIRK_LONG_READ_TIME),
3281
3282 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09003283 * On these Samsung MoviNAND parts, performing secure erase or
3284 * secure trim can result in unrecoverable corruption due to a
3285 * firmware bug.
3286 */
3287 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3288 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3289 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3290 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3291 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3292 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3293 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3294 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3295 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3296 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3297 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3298 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3299 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3300 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3301 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3302 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3303
Shawn Linb5b4ff02015-08-12 13:08:32 +08003304 /*
3305 * On Some Kingston eMMCs, performing trim can result in
3306 * unrecoverable data conrruption occasionally due to a firmware bug.
3307 */
3308 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3309 MMC_QUIRK_TRIM_BROKEN),
3310 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3311 MMC_QUIRK_TRIM_BROKEN),
3312
Pratibhasagar V8d664e32014-12-03 18:26:42 +02003313 /* Some INAND MCP devices advertise incorrect timeout values */
3314 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
3315 MMC_QUIRK_INAND_DATA_TIMEOUT),
3316
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003317 END_FIXUP
3318};
3319
Ulf Hansson96541ba2015-04-14 13:06:12 +02003320static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003322 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003323 char cap_str[10];
3324
Pierre Ossman912490d2005-05-21 10:27:02 +01003325 /*
3326 * Check that the card supports the command class(es) we need.
3327 */
3328 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 return -ENODEV;
3330
Lukas Czerner5204d002014-06-18 13:18:07 +02003331 mmc_fixup_device(card, blk_fixups);
3332
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 md = mmc_blk_alloc(card);
3334 if (IS_ERR(md))
3335 return PTR_ERR(md);
3336
James Bottomleyb9f28d82015-03-05 18:47:01 -08003337 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003338 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303339 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003341 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
Andrei Warkentin371a6892011-04-11 18:10:25 -05003343 if (mmc_blk_alloc_parts(card, md))
3344 goto out;
3345
Ulf Hansson96541ba2015-04-14 13:06:12 +02003346 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003347
Andrei Warkentin371a6892011-04-11 18:10:25 -05003348 if (mmc_add_disk(md))
3349 goto out;
3350
3351 list_for_each_entry(part_md, &md->part, part) {
3352 if (mmc_add_disk(part_md))
3353 goto out;
3354 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003355
3356 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
3357 pm_runtime_use_autosuspend(&card->dev);
3358
3359 /*
3360 * Don't enable runtime PM for SD-combo cards here. Leave that
3361 * decision to be taken during the SDIO init sequence instead.
3362 */
3363 if (card->type != MMC_TYPE_SD_COMBO) {
3364 pm_runtime_set_active(&card->dev);
3365 pm_runtime_enable(&card->dev);
3366 }
3367
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 return 0;
3369
3370 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05003371 mmc_blk_remove_parts(card, md);
3372 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01003373 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374}
3375
Ulf Hansson96541ba2015-04-14 13:06:12 +02003376static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003378 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Andrei Warkentin371a6892011-04-11 18:10:25 -05003380 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003381 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003382 mmc_claim_host(card->host);
3383 mmc_blk_part_switch(card, md);
3384 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003385 if (card->type != MMC_TYPE_SD_COMBO)
3386 pm_runtime_disable(&card->dev);
3387 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003388 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02003389 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390}
3391
Ulf Hansson96541ba2015-04-14 13:06:12 +02003392static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003394 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02003395 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303396 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
3398 if (md) {
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303399 rc = mmc_queue_suspend(&md->queue);
3400 if (rc)
3401 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003402 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303403 rc = mmc_queue_suspend(&part_md->queue);
3404 if (rc)
3405 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303408 goto out;
3409
3410 out_resume:
3411 mmc_queue_resume(&md->queue);
3412 list_for_each_entry(part_md, &md->part, part) {
3413 mmc_queue_resume(&part_md->queue);
3414 }
3415 out:
3416 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417}
3418
Ulf Hansson96541ba2015-04-14 13:06:12 +02003419static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02003420{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003421 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003422}
3423
Ulf Hansson0967edc2014-10-06 11:29:42 +02003424#ifdef CONFIG_PM_SLEEP
3425static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02003426{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003427 struct mmc_card *card = mmc_dev_to_card(dev);
3428
3429 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003430}
3431
Ulf Hansson0967edc2014-10-06 11:29:42 +02003432static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003434 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02003435 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
3437 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003438 /*
3439 * Resume involves the card going into idle state,
3440 * so current partition is always the main one.
3441 */
3442 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003444 list_for_each_entry(part_md, &md->part, part) {
3445 mmc_queue_resume(&part_md->queue);
3446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 }
3448 return 0;
3449}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450#endif
3451
Ulf Hansson0967edc2014-10-06 11:29:42 +02003452static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3453
Ulf Hansson96541ba2015-04-14 13:06:12 +02003454static struct mmc_driver mmc_driver = {
3455 .drv = {
3456 .name = "mmcblk",
3457 .pm = &mmc_blk_pm_ops,
3458 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 .probe = mmc_blk_probe,
3460 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02003461 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462};
3463
3464static int __init mmc_blk_init(void)
3465{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003466 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003468 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3469 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3470
Ben Hutchingsa26eba62014-11-06 03:35:09 +00003471 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003472
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003473 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3474 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003477 res = mmc_register_driver(&mmc_driver);
3478 if (res)
3479 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003481 return 0;
3482 out2:
3483 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 out:
3485 return res;
3486}
3487
3488static void __exit mmc_blk_exit(void)
3489{
3490 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003491 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492}
3493
3494module_init(mmc_blk_init);
3495module_exit(mmc_blk_exit);
3496
3497MODULE_LICENSE("GPL");
3498MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3499