blob: d815bf60ca7066397e8a1f458d8126f656a7e713 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020037#include <linux/pm_runtime.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020038#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
John Calixtocb87ea22011-04-26 18:56:29 -040040#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020042#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010043#include <linux/mmc/mmc.h>
44#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/uaccess.h>
47
Pierre Ossman98ac2162006-12-23 20:03:02 +010048#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080049#include "block.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000051MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040052#ifdef MODULE_PARAM_PREFIX
53#undef MODULE_PARAM_PREFIX
54#endif
55#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010056
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050057#define INAND_CMD38_ARG_EXT_CSD 113
58#define INAND_CMD38_ARG_ERASE 0x00
59#define INAND_CMD38_ARG_TRIM 0x01
60#define INAND_CMD38_ARG_SECERASE 0x80
61#define INAND_CMD38_ARG_SECTRIM1 0x81
62#define INAND_CMD38_ARG_SECTRIM2 0x88
Trey Ramsay8fee4762012-11-16 09:31:41 -060063#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030064#define MMC_SANITIZE_REQ_TIMEOUT 240000
65#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050066
Luca Porziod3df0462015-11-06 15:12:26 +000067#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090068 (rq_data_dir(req) == WRITE))
69#define PACKED_CMD_VER 0x01
70#define PACKED_CMD_WR 0x02
71
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;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100131 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
Arjan van de Vena621aae2006-01-12 18:43:35 +0000134static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900136enum {
137 MMC_PACKED_NR_IDX = -1,
138 MMC_PACKED_NR_ZERO,
139 MMC_PACKED_NR_SINGLE,
140};
141
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400142module_param(perdev_minors, int, 0444);
143MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
144
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200145static inline int mmc_blk_part_switch(struct mmc_card *card,
146 struct mmc_blk_data *md);
147static int get_card_status(struct mmc_card *card, u32 *status, int retries);
148
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900149static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
150{
151 struct mmc_packed *packed = mqrq->packed;
152
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900153 mqrq->cmd_type = MMC_PACKED_NONE;
154 packed->nr_entries = MMC_PACKED_NR_ZERO;
155 packed->idx_failure = MMC_PACKED_NR_IDX;
156 packed->retries = 0;
157 packed->blocks = 0;
158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
161{
162 struct mmc_blk_data *md;
163
Arjan van de Vena621aae2006-01-12 18:43:35 +0000164 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 md = disk->private_data;
166 if (md && md->usage == 0)
167 md = NULL;
168 if (md)
169 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000170 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 return md;
173}
174
Andrei Warkentin371a6892011-04-11 18:10:25 -0500175static inline int mmc_get_devidx(struct gendisk *disk)
176{
Colin Cross382c55f2015-10-22 10:00:41 -0700177 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500178 return devidx;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181static void mmc_blk_put(struct mmc_blk_data *md)
182{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000183 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 md->usage--;
185 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500186 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800187 blk_cleanup_queue(md->queue.queue);
188
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200189 spin_lock(&mmc_blk_lock);
190 ida_remove(&mmc_blk_ida, devidx);
191 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 kfree(md);
195 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000196 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Johan Rudholmadd710e2011-12-02 08:51:06 +0100199static ssize_t power_ro_lock_show(struct device *dev,
200 struct device_attribute *attr, char *buf)
201{
202 int ret;
203 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
204 struct mmc_card *card = md->queue.card;
205 int locked = 0;
206
207 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
208 locked = 2;
209 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
210 locked = 1;
211
212 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
213
Tomas Winkler9098f842015-07-16 15:50:45 +0200214 mmc_blk_put(md);
215
Johan Rudholmadd710e2011-12-02 08:51:06 +0100216 return ret;
217}
218
219static ssize_t power_ro_lock_store(struct device *dev,
220 struct device_attribute *attr, const char *buf, size_t count)
221{
222 int ret;
223 struct mmc_blk_data *md, *part_md;
224 struct mmc_card *card;
225 unsigned long set;
226
227 if (kstrtoul(buf, 0, &set))
228 return -EINVAL;
229
230 if (set != 1)
231 return count;
232
233 md = mmc_blk_get(dev_to_disk(dev));
234 card = md->queue.card;
235
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200236 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100237
238 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
239 card->ext_csd.boot_ro_lock |
240 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
241 card->ext_csd.part_time);
242 if (ret)
243 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
244 else
245 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
246
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200247 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100248
249 if (!ret) {
250 pr_info("%s: Locking boot partition ro until next power on\n",
251 md->disk->disk_name);
252 set_disk_ro(md->disk, 1);
253
254 list_for_each_entry(part_md, &md->part, part)
255 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
256 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
257 set_disk_ro(part_md->disk, 1);
258 }
259 }
260
261 mmc_blk_put(md);
262 return count;
263}
264
Andrei Warkentin371a6892011-04-11 18:10:25 -0500265static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
266 char *buf)
267{
268 int ret;
269 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
270
Baruch Siach0031a982014-09-22 10:12:51 +0300271 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500272 get_disk_ro(dev_to_disk(dev)) ^
273 md->read_only);
274 mmc_blk_put(md);
275 return ret;
276}
277
278static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
279 const char *buf, size_t count)
280{
281 int ret;
282 char *end;
283 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
284 unsigned long set = simple_strtoul(buf, &end, 0);
285 if (end == buf) {
286 ret = -EINVAL;
287 goto out;
288 }
289
290 set_disk_ro(dev_to_disk(dev), set || md->read_only);
291 ret = count;
292out:
293 mmc_blk_put(md);
294 return ret;
295}
296
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200297static ssize_t
298num_wr_reqs_to_start_packing_show(struct device *dev,
299 struct device_attribute *attr, char *buf)
300{
301 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
302 int num_wr_reqs_to_start_packing;
303 int ret;
304
305 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
306
307 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
308
309 mmc_blk_put(md);
310 return ret;
311}
312
313static ssize_t
314num_wr_reqs_to_start_packing_store(struct device *dev,
315 struct device_attribute *attr,
316 const char *buf, size_t count)
317{
318 int value;
319 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Yaniv Gardi42399822014-12-04 00:26:23 +0200320 struct mmc_card *card = md->queue.card;
321 int ret = count;
322
323 if (!card) {
324 ret = -EINVAL;
325 goto exit;
326 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200327
328 sscanf(buf, "%d", &value);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200329
Yaniv Gardi42399822014-12-04 00:26:23 +0200330 if (value >= 0) {
331 md->queue.num_wr_reqs_to_start_packing =
332 min_t(int, value, (int)card->ext_csd.max_packed_writes);
333
334 pr_debug("%s: trigger to pack: new value = %d",
335 mmc_hostname(card->host),
336 md->queue.num_wr_reqs_to_start_packing);
337 } else {
338 pr_err("%s: value %d is not valid. old value remains = %d",
339 mmc_hostname(card->host), value,
340 md->queue.num_wr_reqs_to_start_packing);
341 ret = -EINVAL;
342 }
343
344exit:
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200345 mmc_blk_put(md);
Yaniv Gardi42399822014-12-04 00:26:23 +0200346 return ret;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200347}
348
Mark Salyzyn6904e432016-01-28 11:12:25 -0800349#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
350
351static int max_read_speed, max_write_speed, cache_size = 4;
352
353module_param(max_read_speed, int, S_IRUSR | S_IRGRP);
354MODULE_PARM_DESC(max_read_speed, "maximum KB/s read speed 0=off");
355module_param(max_write_speed, int, S_IRUSR | S_IRGRP);
356MODULE_PARM_DESC(max_write_speed, "maximum KB/s write speed 0=off");
357module_param(cache_size, int, S_IRUSR | S_IRGRP);
358MODULE_PARM_DESC(cache_size, "MB high speed memory or SLC cache");
359
360/*
361 * helper macros and expectations:
362 * size - unsigned long number of bytes
363 * jiffies - unsigned long HZ timestamp difference
364 * speed - unsigned KB/s transfer rate
365 */
366#define size_and_speed_to_jiffies(size, speed) \
367 ((size) * HZ / (speed) / 1024UL)
368#define jiffies_and_speed_to_size(jiffies, speed) \
369 (((speed) * (jiffies) * 1024UL) / HZ)
370#define jiffies_and_size_to_speed(jiffies, size) \
371 ((size) * HZ / (jiffies) / 1024UL)
372
373/* Limits to report warning */
374/* jiffies_and_size_to_speed(10*HZ, queue_max_hw_sectors(q) * 512UL) ~ 25 */
375#define MIN_SPEED(q) 250 /* 10 times faster than a floppy disk */
376#define MAX_SPEED(q) jiffies_and_size_to_speed(1, queue_max_sectors(q) * 512UL)
377
378#define speed_valid(speed) ((speed) > 0)
379
380static const char off[] = "off\n";
381
382static int max_speed_show(int speed, char *buf)
383{
384 if (speed)
385 return scnprintf(buf, PAGE_SIZE, "%uKB/s\n", speed);
386 else
387 return scnprintf(buf, PAGE_SIZE, off);
388}
389
390static int max_speed_store(const char *buf, struct request_queue *q)
391{
392 unsigned int limit, set = 0;
393
394 if (!strncasecmp(off, buf, sizeof(off) - 2))
395 return set;
396 if (kstrtouint(buf, 0, &set) || (set > INT_MAX))
397 return -EINVAL;
398 if (set == 0)
399 return set;
400 limit = MAX_SPEED(q);
401 if (set > limit)
402 pr_warn("max speed %u ineffective above %u\n", set, limit);
403 limit = MIN_SPEED(q);
404 if (set < limit)
405 pr_warn("max speed %u painful below %u\n", set, limit);
406 return set;
407}
408
409static ssize_t max_write_speed_show(struct device *dev,
410 struct device_attribute *attr, char *buf)
411{
412 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
413 int ret = max_speed_show(atomic_read(&md->queue.max_write_speed), buf);
414
415 mmc_blk_put(md);
416 return ret;
417}
418
419static ssize_t max_write_speed_store(struct device *dev,
420 struct device_attribute *attr,
421 const char *buf, size_t count)
422{
423 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
424 int set = max_speed_store(buf, md->queue.queue);
425
426 if (set < 0) {
427 mmc_blk_put(md);
428 return set;
429 }
430
431 atomic_set(&md->queue.max_write_speed, set);
432 mmc_blk_put(md);
433 return count;
434}
435
436static const DEVICE_ATTR(max_write_speed, S_IRUGO | S_IWUSR,
437 max_write_speed_show, max_write_speed_store);
438
439static ssize_t max_read_speed_show(struct device *dev,
440 struct device_attribute *attr, char *buf)
441{
442 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
443 int ret = max_speed_show(atomic_read(&md->queue.max_read_speed), buf);
444
445 mmc_blk_put(md);
446 return ret;
447}
448
449static ssize_t max_read_speed_store(struct device *dev,
450 struct device_attribute *attr,
451 const char *buf, size_t count)
452{
453 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
454 int set = max_speed_store(buf, md->queue.queue);
455
456 if (set < 0) {
457 mmc_blk_put(md);
458 return set;
459 }
460
461 atomic_set(&md->queue.max_read_speed, set);
462 mmc_blk_put(md);
463 return count;
464}
465
466static const DEVICE_ATTR(max_read_speed, S_IRUGO | S_IWUSR,
467 max_read_speed_show, max_read_speed_store);
468
469static ssize_t cache_size_show(struct device *dev,
470 struct device_attribute *attr, char *buf)
471{
472 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
473 struct mmc_queue *mq = &md->queue;
474 int cache_size = atomic_read(&mq->cache_size);
475 int ret;
476
477 if (!cache_size)
478 ret = scnprintf(buf, PAGE_SIZE, off);
479 else {
480 int speed = atomic_read(&mq->max_write_speed);
481
482 if (!speed_valid(speed))
483 ret = scnprintf(buf, PAGE_SIZE, "%uMB\n", cache_size);
484 else { /* We accept race between cache_jiffies and cache_used */
485 unsigned long size = jiffies_and_speed_to_size(
486 jiffies - mq->cache_jiffies, speed);
487 long used = atomic_long_read(&mq->cache_used);
488
489 if (size >= used)
490 size = 0;
491 else
492 size = (used - size) * 100 / cache_size
493 / 1024UL / 1024UL;
494
495 ret = scnprintf(buf, PAGE_SIZE, "%uMB %lu%% used\n",
496 cache_size, size);
497 }
498 }
499
500 mmc_blk_put(md);
501 return ret;
502}
503
504static ssize_t cache_size_store(struct device *dev,
505 struct device_attribute *attr,
506 const char *buf, size_t count)
507{
508 struct mmc_blk_data *md;
509 unsigned int set = 0;
510
511 if (strncasecmp(off, buf, sizeof(off) - 2)
512 && (kstrtouint(buf, 0, &set) || (set > INT_MAX)))
513 return -EINVAL;
514
515 md = mmc_blk_get(dev_to_disk(dev));
516 atomic_set(&md->queue.cache_size, set);
517 mmc_blk_put(md);
518 return count;
519}
520
521static const DEVICE_ATTR(cache_size, S_IRUGO | S_IWUSR,
522 cache_size_show, cache_size_store);
523
524/* correct for write-back */
525static long mmc_blk_cache_used(struct mmc_queue *mq, unsigned long waitfor)
526{
527 long used = 0;
528 int speed = atomic_read(&mq->max_write_speed);
529
530 if (speed_valid(speed)) {
531 unsigned long size = jiffies_and_speed_to_size(
532 waitfor - mq->cache_jiffies, speed);
533 used = atomic_long_read(&mq->cache_used);
534
535 if (size >= used)
536 used = 0;
537 else
538 used -= size;
539 }
540
541 atomic_long_set(&mq->cache_used, used);
542 mq->cache_jiffies = waitfor;
543
544 return used;
545}
546
547static void mmc_blk_simulate_delay(
548 struct mmc_queue *mq,
549 struct request *req,
550 unsigned long waitfor)
551{
552 int max_speed;
553
554 if (!req)
555 return;
556
557 max_speed = (rq_data_dir(req) == READ)
558 ? atomic_read(&mq->max_read_speed)
559 : atomic_read(&mq->max_write_speed);
560 if (speed_valid(max_speed)) {
561 unsigned long bytes = blk_rq_bytes(req);
562
563 if (rq_data_dir(req) != READ) {
564 int cache_size = atomic_read(&mq->cache_size);
565
566 if (cache_size) {
567 unsigned long size = cache_size * 1024L * 1024L;
568 long used = mmc_blk_cache_used(mq, waitfor);
569
570 used += bytes;
571 atomic_long_set(&mq->cache_used, used);
572 bytes = 0;
573 if (used > size)
574 bytes = used - size;
575 }
576 }
577 waitfor += size_and_speed_to_jiffies(bytes, max_speed);
578 if (time_is_after_jiffies(waitfor)) {
579 long msecs = jiffies_to_msecs(waitfor - jiffies);
580
581 if (likely(msecs > 0))
582 msleep(msecs);
583 }
584 }
585}
586
587#else
588
589#define mmc_blk_simulate_delay(mq, req, waitfor)
590
591#endif
592
Al Viroa5a15612008-03-02 10:33:30 -0500593static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Al Viroa5a15612008-03-02 10:33:30 -0500595 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 int ret = -ENXIO;
597
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200598 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (md) {
600 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500601 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700603
Al Viroa5a15612008-03-02 10:33:30 -0500604 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700605 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700606 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200609 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 return ret;
612}
613
Al Virodb2a1442013-05-05 21:52:57 -0400614static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Al Viroa5a15612008-03-02 10:33:30 -0500616 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200618 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200620 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800624mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800626 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
627 geo->heads = 4;
628 geo->sectors = 16;
629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
John Calixtocb87ea22011-04-26 18:56:29 -0400632struct mmc_blk_ioc_data {
633 struct mmc_ioc_cmd ic;
634 unsigned char *buf;
635 u64 buf_bytes;
636};
637
638static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
639 struct mmc_ioc_cmd __user *user)
640{
641 struct mmc_blk_ioc_data *idata;
642 int err;
643
yalin wang1ff89502015-11-12 19:27:11 +0800644 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400645 if (!idata) {
646 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400647 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400648 }
649
650 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
651 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400652 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400653 }
654
655 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
656 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
657 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400658 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400659 }
660
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300661 if (!idata->buf_bytes) {
662 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100663 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300664 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100665
yalin wang1ff89502015-11-12 19:27:11 +0800666 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400667 if (!idata->buf) {
668 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400669 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400670 }
671
672 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
673 idata->ic.data_ptr, idata->buf_bytes)) {
674 err = -EFAULT;
675 goto copy_err;
676 }
677
678 return idata;
679
680copy_err:
681 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400682idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400683 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400684out:
John Calixtocb87ea22011-04-26 18:56:29 -0400685 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400686}
687
Jon Huntera5f57742015-09-22 10:27:53 +0100688static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
689 struct mmc_blk_ioc_data *idata)
690{
691 struct mmc_ioc_cmd *ic = &idata->ic;
692
693 if (copy_to_user(&(ic_ptr->response), ic->response,
694 sizeof(ic->response)))
695 return -EFAULT;
696
697 if (!idata->ic.write_flag) {
698 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
699 idata->buf, idata->buf_bytes))
700 return -EFAULT;
701 }
702
703 return 0;
704}
705
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200706static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
707 u32 retries_max)
708{
709 int err;
710 u32 retry_count = 0;
711
712 if (!status || !retries_max)
713 return -EINVAL;
714
715 do {
716 err = get_card_status(card, status, 5);
717 if (err)
718 break;
719
720 if (!R1_STATUS(*status) &&
721 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
722 break; /* RPMB programming operation complete */
723
724 /*
725 * Rechedule to give the MMC device a chance to continue
726 * processing the previous command without being polled too
727 * frequently.
728 */
729 usleep_range(1000, 5000);
730 } while (++retry_count < retries_max);
731
732 if (retry_count == retries_max)
733 err = -EPERM;
734
735 return err;
736}
737
Maya Erez775a9362013-04-18 15:41:55 +0300738static int ioctl_do_sanitize(struct mmc_card *card)
739{
740 int err;
741
Ulf Hanssona2d10862013-12-16 14:37:26 +0100742 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300743 pr_warn("%s: %s - SANITIZE is not supported\n",
744 mmc_hostname(card->host), __func__);
745 err = -EOPNOTSUPP;
746 goto out;
747 }
748
749 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
750 mmc_hostname(card->host), __func__);
751
752 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
753 EXT_CSD_SANITIZE_START, 1,
754 MMC_SANITIZE_REQ_TIMEOUT);
755
756 if (err)
757 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
758 mmc_hostname(card->host), __func__, err);
759
760 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
761 __func__);
762out:
763 return err;
764}
765
Jon Huntera5f57742015-09-22 10:27:53 +0100766static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
767 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400768{
John Calixtocb87ea22011-04-26 18:56:29 -0400769 struct mmc_command cmd = {0};
770 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530771 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400772 struct scatterlist sg;
773 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200774 int is_rpmb = false;
775 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400776
Jon Huntera5f57742015-09-22 10:27:53 +0100777 if (!card || !md || !idata)
778 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400779
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200780 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
781 is_rpmb = true;
782
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100783 cmd.opcode = idata->ic.opcode;
784 cmd.arg = idata->ic.arg;
785 cmd.flags = idata->ic.flags;
786
787 if (idata->buf_bytes) {
788 data.sg = &sg;
789 data.sg_len = 1;
790 data.blksz = idata->ic.blksz;
791 data.blocks = idata->ic.blocks;
792
793 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
794
795 if (idata->ic.write_flag)
796 data.flags = MMC_DATA_WRITE;
797 else
798 data.flags = MMC_DATA_READ;
799
800 /* data.flags must already be set before doing this. */
801 mmc_set_data_timeout(&data, card);
802
803 /* Allow overriding the timeout_ns for empirical tuning. */
804 if (idata->ic.data_timeout_ns)
805 data.timeout_ns = idata->ic.data_timeout_ns;
806
807 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
808 /*
809 * Pretend this is a data transfer and rely on the
810 * host driver to compute timeout. When all host
811 * drivers support cmd.cmd_timeout for R1B, this
812 * can be changed to:
813 *
814 * mrq.data = NULL;
815 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
816 */
817 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
818 }
819
820 mrq.data = &data;
821 }
822
823 mrq.cmd = &cmd;
824
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200825 err = mmc_blk_part_switch(card, md);
826 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100827 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200828
John Calixtocb87ea22011-04-26 18:56:29 -0400829 if (idata->ic.is_acmd) {
830 err = mmc_app_cmd(card->host, card);
831 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100832 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400833 }
834
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200835 if (is_rpmb) {
836 err = mmc_set_blockcount(card, data.blocks,
837 idata->ic.write_flag & (1 << 31));
838 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100839 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200840 }
841
Yaniv Gardia82e4842013-06-05 14:13:08 +0300842 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
843 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300844 err = ioctl_do_sanitize(card);
845
846 if (err)
847 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
848 __func__, err);
849
Jon Huntera5f57742015-09-22 10:27:53 +0100850 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300851 }
852
John Calixtocb87ea22011-04-26 18:56:29 -0400853 mmc_wait_for_req(card->host, &mrq);
854
855 if (cmd.error) {
856 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
857 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100858 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400859 }
860 if (data.error) {
861 dev_err(mmc_dev(card->host), "%s: data error %d\n",
862 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100863 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400864 }
865
866 /*
867 * According to the SD specs, some commands require a delay after
868 * issuing the command.
869 */
870 if (idata->ic.postsleep_min_us)
871 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
872
Jon Huntera5f57742015-09-22 10:27:53 +0100873 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400874
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200875 if (is_rpmb) {
876 /*
877 * Ensure RPMB command has completed by polling CMD13
878 * "Send Status".
879 */
880 err = ioctl_rpmb_card_status_poll(card, &status, 5);
881 if (err)
882 dev_err(mmc_dev(card->host),
883 "%s: Card Status=0x%08X, error %d\n",
884 __func__, status, err);
885 }
886
Jon Huntera5f57742015-09-22 10:27:53 +0100887 return err;
888}
889
890static int mmc_blk_ioctl_cmd(struct block_device *bdev,
891 struct mmc_ioc_cmd __user *ic_ptr)
892{
893 struct mmc_blk_ioc_data *idata;
894 struct mmc_blk_data *md;
895 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700896 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100897
Shawn Lin83c742c2016-03-16 18:15:47 +0800898 /*
899 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
900 * whole block device, not on a partition. This prevents overspray
901 * between sibling partitions.
902 */
903 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
904 return -EPERM;
905
Jon Huntera5f57742015-09-22 10:27:53 +0100906 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
907 if (IS_ERR(idata))
908 return PTR_ERR(idata);
909
910 md = mmc_blk_get(bdev->bd_disk);
911 if (!md) {
912 err = -EINVAL;
913 goto cmd_err;
914 }
915
916 card = md->queue.card;
917 if (IS_ERR(card)) {
918 err = PTR_ERR(card);
919 goto cmd_done;
920 }
921
922 mmc_get_card(card);
923
Grant Grundlerb0934102015-09-23 18:30:33 -0700924 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100925
Adrian Hunter3c866562016-05-04 14:38:12 +0300926 /* Always switch back to main area after RPMB access */
927 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
928 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
929
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200930 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400931
Grant Grundlerb0934102015-09-23 18:30:33 -0700932 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100933
John Calixtocb87ea22011-04-26 18:56:29 -0400934cmd_done:
935 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300936cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400937 kfree(idata->buf);
938 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700939 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400940}
941
Jon Huntera5f57742015-09-22 10:27:53 +0100942static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
943 struct mmc_ioc_multi_cmd __user *user)
944{
945 struct mmc_blk_ioc_data **idata = NULL;
946 struct mmc_ioc_cmd __user *cmds = user->cmds;
947 struct mmc_card *card;
948 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -0700949 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100950 __u64 num_of_cmds;
951
Shawn Lin83c742c2016-03-16 18:15:47 +0800952 /*
953 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
954 * whole block device, not on a partition. This prevents overspray
955 * between sibling partitions.
956 */
957 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
958 return -EPERM;
959
Jon Huntera5f57742015-09-22 10:27:53 +0100960 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
961 sizeof(num_of_cmds)))
962 return -EFAULT;
963
964 if (num_of_cmds > MMC_IOC_MAX_CMDS)
965 return -EINVAL;
966
967 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
968 if (!idata)
969 return -ENOMEM;
970
971 for (i = 0; i < num_of_cmds; i++) {
972 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
973 if (IS_ERR(idata[i])) {
974 err = PTR_ERR(idata[i]);
975 num_of_cmds = i;
976 goto cmd_err;
977 }
978 }
979
980 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -0800981 if (!md) {
982 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +0100983 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -0800984 }
Jon Huntera5f57742015-09-22 10:27:53 +0100985
986 card = md->queue.card;
987 if (IS_ERR(card)) {
988 err = PTR_ERR(card);
989 goto cmd_done;
990 }
991
992 mmc_get_card(card);
993
Grant Grundlerb0934102015-09-23 18:30:33 -0700994 for (i = 0; i < num_of_cmds && !ioc_err; i++)
995 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100996
Adrian Hunter3c866562016-05-04 14:38:12 +0300997 /* Always switch back to main area after RPMB access */
998 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
999 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1000
Jon Huntera5f57742015-09-22 10:27:53 +01001001 mmc_put_card(card);
1002
1003 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -07001004 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +01001005 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001006
1007cmd_done:
1008 mmc_blk_put(md);
1009cmd_err:
1010 for (i = 0; i < num_of_cmds; i++) {
1011 kfree(idata[i]->buf);
1012 kfree(idata[i]);
1013 }
1014 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001015 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +01001016}
1017
John Calixtocb87ea22011-04-26 18:56:29 -04001018static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
1019 unsigned int cmd, unsigned long arg)
1020{
Jon Huntera5f57742015-09-22 10:27:53 +01001021 switch (cmd) {
1022 case MMC_IOC_CMD:
1023 return mmc_blk_ioctl_cmd(bdev,
1024 (struct mmc_ioc_cmd __user *)arg);
1025 case MMC_IOC_MULTI_CMD:
1026 return mmc_blk_ioctl_multi_cmd(bdev,
1027 (struct mmc_ioc_multi_cmd __user *)arg);
1028 default:
1029 return -EINVAL;
1030 }
John Calixtocb87ea22011-04-26 18:56:29 -04001031}
1032
1033#ifdef CONFIG_COMPAT
1034static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
1035 unsigned int cmd, unsigned long arg)
1036{
1037 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
1038}
1039#endif
1040
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001041static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -05001042 .open = mmc_blk_open,
1043 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001044 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -04001046 .ioctl = mmc_blk_ioctl,
1047#ifdef CONFIG_COMPAT
1048 .compat_ioctl = mmc_blk_compat_ioctl,
1049#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050};
1051
Andrei Warkentin371a6892011-04-11 18:10:25 -05001052static inline int mmc_blk_part_switch(struct mmc_card *card,
1053 struct mmc_blk_data *md)
1054{
1055 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001056 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001057
Andrei Warkentin371a6892011-04-11 18:10:25 -05001058 if (main_md->part_curr == md->part_type)
1059 return 0;
1060
1061 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001062 u8 part_config = card->ext_csd.part_config;
1063
Adrian Hunter57da0c02016-05-04 14:38:13 +03001064 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1065 mmc_retune_pause(card->host);
1066
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001067 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1068 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001069
1070 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001071 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001072 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001073 if (ret) {
1074 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1075 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001076 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001077 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001078
1079 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001080
1081 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1082 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001083 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001084
1085 main_md->part_curr = md->part_type;
1086 return 0;
1087}
1088
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001089static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1090{
1091 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001092 u32 result;
1093 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001094
Venkatraman Sad5fd972011-08-25 00:30:50 +05301095 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001096 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001097 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001098
1099 struct scatterlist sg;
1100
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001101 cmd.opcode = MMC_APP_CMD;
1102 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001103 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001104
1105 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001106 if (err)
1107 return (u32)-1;
1108 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001109 return (u32)-1;
1110
1111 memset(&cmd, 0, sizeof(struct mmc_command));
1112
1113 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1114 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001115 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001116
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001117 data.blksz = 4;
1118 data.blocks = 1;
1119 data.flags = MMC_DATA_READ;
1120 data.sg = &sg;
1121 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301122 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001123
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001124 mrq.cmd = &cmd;
1125 mrq.data = &data;
1126
Ben Dooks051913d2009-06-08 23:33:57 +01001127 blocks = kmalloc(4, GFP_KERNEL);
1128 if (!blocks)
1129 return (u32)-1;
1130
1131 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001132
1133 mmc_wait_for_req(card->host, &mrq);
1134
Ben Dooks051913d2009-06-08 23:33:57 +01001135 result = ntohl(*blocks);
1136 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001137
Ben Dooks051913d2009-06-08 23:33:57 +01001138 if (cmd.error || data.error)
1139 result = (u32)-1;
1140
1141 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001142}
1143
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001144static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001145{
Chris Ball1278dba2011-04-13 23:40:30 -04001146 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001147 int err;
1148
Adrian Hunter504f1912008-10-16 12:55:25 +03001149 cmd.opcode = MMC_SEND_STATUS;
1150 if (!mmc_host_is_spi(card->host))
1151 cmd.arg = card->rca << 16;
1152 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001153 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1154 if (err == 0)
1155 *status = cmd.resp[0];
1156 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001157}
1158
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001159static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001160 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001161{
1162 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1163 int err = 0;
1164 u32 status;
1165
1166 do {
1167 err = get_card_status(card, &status, 5);
1168 if (err) {
1169 pr_err("%s: error %d requesting status\n",
1170 req->rq_disk->disk_name, err);
1171 return err;
1172 }
1173
1174 if (status & R1_ERROR) {
1175 pr_err("%s: %s: error sending status cmd, status %#x\n",
1176 req->rq_disk->disk_name, __func__, status);
1177 *gen_err = 1;
1178 }
1179
Ulf Hansson95a91292014-01-29 13:11:27 +01001180 /* We may rely on the host hw to handle busy detection.*/
1181 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1182 hw_busy_detect)
1183 break;
1184
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001185 /*
1186 * Timeout if the device never becomes ready for data and never
1187 * leaves the program state.
1188 */
1189 if (time_after(jiffies, timeout)) {
1190 pr_err("%s: Card stuck in programming state! %s %s\n",
1191 mmc_hostname(card->host),
1192 req->rq_disk->disk_name, __func__);
1193 return -ETIMEDOUT;
1194 }
1195
1196 /*
1197 * Some cards mishandle the status bits,
1198 * so make sure to check both the busy
1199 * indication and the card state.
1200 */
1201 } while (!(status & R1_READY_FOR_DATA) ||
1202 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1203
1204 return err;
1205}
1206
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001207static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1208 struct request *req, int *gen_err, u32 *stop_status)
1209{
1210 struct mmc_host *host = card->host;
1211 struct mmc_command cmd = {0};
1212 int err;
1213 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1214
1215 /*
1216 * Normally we use R1B responses for WRITE, but in cases where the host
1217 * has specified a max_busy_timeout we need to validate it. A failure
1218 * means we need to prevent the host from doing hw busy detection, which
1219 * is done by converting to a R1 response instead.
1220 */
1221 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1222 use_r1b_resp = false;
1223
1224 cmd.opcode = MMC_STOP_TRANSMISSION;
1225 if (use_r1b_resp) {
1226 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1227 cmd.busy_timeout = timeout_ms;
1228 } else {
1229 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1230 }
1231
1232 err = mmc_wait_for_cmd(host, &cmd, 5);
1233 if (err)
1234 return err;
1235
1236 *stop_status = cmd.resp[0];
1237
1238 /* No need to check card status in case of READ. */
1239 if (rq_data_dir(req) == READ)
1240 return 0;
1241
1242 if (!mmc_host_is_spi(host) &&
1243 (*stop_status & R1_ERROR)) {
1244 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1245 req->rq_disk->disk_name, __func__, *stop_status);
1246 *gen_err = 1;
1247 }
1248
1249 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1250}
1251
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301252#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001253#define ERR_RETRY 2
1254#define ERR_ABORT 1
1255#define ERR_CONTINUE 0
1256
1257static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1258 bool status_valid, u32 status)
1259{
1260 switch (error) {
1261 case -EILSEQ:
1262 /* response crc error, retry the r/w cmd */
1263 pr_err("%s: %s sending %s command, card status %#x\n",
1264 req->rq_disk->disk_name, "response CRC error",
1265 name, status);
1266 return ERR_RETRY;
1267
1268 case -ETIMEDOUT:
1269 pr_err("%s: %s sending %s command, card status %#x\n",
1270 req->rq_disk->disk_name, "timed out", name, status);
1271
1272 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301273 if (!status_valid) {
1274 pr_err("%s: status not valid, retrying timeout\n",
1275 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001276 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301277 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001278
1279 /*
1280 * If it was a r/w cmd crc error, or illegal command
1281 * (eg, issued in wrong state) then retry - we should
1282 * have corrected the state problem above.
1283 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301284 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1285 pr_err("%s: command error, retrying timeout\n",
1286 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001287 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301288 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001289
1290 /* Otherwise abort the command */
1291 return ERR_ABORT;
1292
1293 default:
1294 /* We don't understand the error code the driver gave us */
1295 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1296 req->rq_disk->disk_name, error, status);
1297 return ERR_ABORT;
1298 }
1299}
1300
1301/*
1302 * Initial r/w and stop cmd error recovery.
1303 * We don't know whether the card received the r/w cmd or not, so try to
1304 * restore things back to a sane state. Essentially, we do this as follows:
1305 * - Obtain card status. If the first attempt to obtain card status fails,
1306 * the status word will reflect the failed status cmd, not the failed
1307 * r/w cmd. If we fail to obtain card status, it suggests we can no
1308 * longer communicate with the card.
1309 * - Check the card state. If the card received the cmd but there was a
1310 * transient problem with the response, it might still be in a data transfer
1311 * mode. Try to send it a stop command. If this fails, we can't recover.
1312 * - If the r/w cmd failed due to a response CRC error, it was probably
1313 * transient, so retry the cmd.
1314 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1315 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1316 * illegal cmd, retry.
1317 * Otherwise we don't understand what happened, so abort.
1318 */
1319static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001320 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001321{
1322 bool prev_cmd_status_valid = true;
1323 u32 status, stop_status = 0;
1324 int err, retry;
1325
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301326 if (mmc_card_removed(card))
1327 return ERR_NOMEDIUM;
1328
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001329 /*
1330 * Try to get card status which indicates both the card state
1331 * and why there was no response. If the first attempt fails,
1332 * we can't be sure the returned status is for the r/w command.
1333 */
1334 for (retry = 2; retry >= 0; retry--) {
1335 err = get_card_status(card, &status, 0);
1336 if (!err)
1337 break;
1338
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001339 /* Re-tune if needed */
1340 mmc_retune_recheck(card->host);
1341
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001342 prev_cmd_status_valid = false;
1343 pr_err("%s: error %d sending status command, %sing\n",
1344 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1345 }
1346
1347 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301348 if (err) {
1349 /* Check if the card is removed */
1350 if (mmc_detect_card_removed(card->host))
1351 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001352 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301353 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001354
Adrian Hunter67716322011-08-29 16:42:15 +03001355 /* Flag ECC errors */
1356 if ((status & R1_CARD_ECC_FAILED) ||
1357 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1358 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1359 *ecc_err = 1;
1360
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001361 /* Flag General errors */
1362 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1363 if ((status & R1_ERROR) ||
1364 (brq->stop.resp[0] & R1_ERROR)) {
1365 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1366 req->rq_disk->disk_name, __func__,
1367 brq->stop.resp[0], status);
1368 *gen_err = 1;
1369 }
1370
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001371 /*
1372 * Check the current card state. If it is in some data transfer
1373 * mode, tell it to stop (and hopefully transition back to TRAN.)
1374 */
1375 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1376 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001377 err = send_stop(card,
1378 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1379 req, gen_err, &stop_status);
1380 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001381 pr_err("%s: error %d sending stop command\n",
1382 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001383 /*
1384 * If the stop cmd also timed out, the card is probably
1385 * not present, so abort. Other errors are bad news too.
1386 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001387 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001388 }
1389
Adrian Hunter67716322011-08-29 16:42:15 +03001390 if (stop_status & R1_CARD_ECC_FAILED)
1391 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001392 }
1393
1394 /* Check for set block count errors */
1395 if (brq->sbc.error)
1396 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1397 prev_cmd_status_valid, status);
1398
1399 /* Check for r/w command errors */
1400 if (brq->cmd.error)
1401 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1402 prev_cmd_status_valid, status);
1403
Adrian Hunter67716322011-08-29 16:42:15 +03001404 /* Data errors */
1405 if (!brq->stop.error)
1406 return ERR_CONTINUE;
1407
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001408 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001409 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 +01001410 req->rq_disk->disk_name, brq->stop.error,
1411 brq->cmd.resp[0], status);
1412
1413 /*
1414 * Subsitute in our own stop status as this will give the error
1415 * state which happened during the execution of the r/w command.
1416 */
1417 if (stop_status) {
1418 brq->stop.resp[0] = stop_status;
1419 brq->stop.error = 0;
1420 }
1421 return ERR_CONTINUE;
1422}
1423
Adrian Hunter67716322011-08-29 16:42:15 +03001424static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1425 int type)
1426{
1427 int err;
1428
1429 if (md->reset_done & type)
1430 return -EEXIST;
1431
1432 md->reset_done |= type;
1433 err = mmc_hw_reset(host);
1434 /* Ensure we switch back to the correct partition */
1435 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001436 struct mmc_blk_data *main_md =
1437 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001438 int part_err;
1439
1440 main_md->part_curr = main_md->part_type;
1441 part_err = mmc_blk_part_switch(host->card, md);
1442 if (part_err) {
1443 /*
1444 * We have failed to get back into the correct
1445 * partition, so we need to abort the whole request.
1446 */
1447 return -ENODEV;
1448 }
1449 }
1450 return err;
1451}
1452
1453static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1454{
1455 md->reset_done &= ~type;
1456}
1457
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001458int mmc_access_rpmb(struct mmc_queue *mq)
1459{
1460 struct mmc_blk_data *md = mq->data;
1461 /*
1462 * If this is a RPMB partition access, return ture
1463 */
1464 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1465 return true;
1466
1467 return false;
1468}
1469
Adrian Hunterbd788c92010-08-11 14:17:47 -07001470static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1471{
1472 struct mmc_blk_data *md = mq->data;
1473 struct mmc_card *card = md->queue.card;
1474 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001475 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001476
Adrian Hunterbd788c92010-08-11 14:17:47 -07001477 if (!mmc_can_erase(card)) {
1478 err = -EOPNOTSUPP;
1479 goto out;
1480 }
1481
1482 from = blk_rq_pos(req);
1483 nr = blk_rq_sectors(req);
1484
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001485 if (mmc_can_discard(card))
1486 arg = MMC_DISCARD_ARG;
1487 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001488 arg = MMC_TRIM_ARG;
1489 else
1490 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001491retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001492 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1493 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1494 INAND_CMD38_ARG_EXT_CSD,
1495 arg == MMC_TRIM_ARG ?
1496 INAND_CMD38_ARG_TRIM :
1497 INAND_CMD38_ARG_ERASE,
1498 0);
1499 if (err)
1500 goto out;
1501 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001502 err = mmc_erase(card, from, nr, arg);
1503out:
Adrian Hunter67716322011-08-29 16:42:15 +03001504 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1505 goto retry;
1506 if (!err)
1507 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301508 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001509
Adrian Hunterbd788c92010-08-11 14:17:47 -07001510 return err ? 0 : 1;
1511}
1512
Adrian Hunter49804542010-08-11 14:17:50 -07001513static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1514 struct request *req)
1515{
1516 struct mmc_blk_data *md = mq->data;
1517 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001518 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001519 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001520
Maya Erez775a9362013-04-18 15:41:55 +03001521 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001522 err = -EOPNOTSUPP;
1523 goto out;
1524 }
1525
1526 from = blk_rq_pos(req);
1527 nr = blk_rq_sectors(req);
1528
Maya Erez775a9362013-04-18 15:41:55 +03001529 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1530 arg = MMC_SECURE_TRIM1_ARG;
1531 else
1532 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001533
Adrian Hunter67716322011-08-29 16:42:15 +03001534retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001535 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1536 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1537 INAND_CMD38_ARG_EXT_CSD,
1538 arg == MMC_SECURE_TRIM1_ARG ?
1539 INAND_CMD38_ARG_SECTRIM1 :
1540 INAND_CMD38_ARG_SECERASE,
1541 0);
1542 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001543 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001544 }
Adrian Hunter28302812012-04-05 14:45:48 +03001545
Adrian Hunter49804542010-08-11 14:17:50 -07001546 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001547 if (err == -EIO)
1548 goto out_retry;
1549 if (err)
1550 goto out;
1551
1552 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001553 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1554 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1555 INAND_CMD38_ARG_EXT_CSD,
1556 INAND_CMD38_ARG_SECTRIM2,
1557 0);
1558 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001559 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001560 }
Adrian Hunter28302812012-04-05 14:45:48 +03001561
Adrian Hunter49804542010-08-11 14:17:50 -07001562 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001563 if (err == -EIO)
1564 goto out_retry;
1565 if (err)
1566 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001567 }
Adrian Hunter28302812012-04-05 14:45:48 +03001568
Adrian Hunter28302812012-04-05 14:45:48 +03001569out_retry:
1570 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001571 goto retry;
1572 if (!err)
1573 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001574out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301575 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001576
Adrian Hunter49804542010-08-11 14:17:50 -07001577 return err ? 0 : 1;
1578}
1579
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001580static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1581{
1582 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001583 struct mmc_card *card = md->queue.card;
1584 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001585
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001586 ret = mmc_flush_cache(card);
1587 if (ret)
1588 ret = -EIO;
1589
Mark Salyzyn6904e432016-01-28 11:12:25 -08001590#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
1591 else if (atomic_read(&mq->cache_size)) {
1592 long used = mmc_blk_cache_used(mq, jiffies);
1593
1594 if (used) {
1595 int speed = atomic_read(&mq->max_write_speed);
1596
1597 if (speed_valid(speed)) {
1598 unsigned long msecs = jiffies_to_msecs(
1599 size_and_speed_to_jiffies(
1600 used, speed));
1601 if (msecs)
1602 msleep(msecs);
1603 }
1604 }
1605 }
1606#endif
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301607 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001608
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001609 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001610}
1611
1612/*
1613 * Reformat current write as a reliable write, supporting
1614 * both legacy and the enhanced reliable write MMC cards.
1615 * In each transfer we'll handle only as much as a single
1616 * reliable write can handle, thus finish the request in
1617 * partial completions.
1618 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001619static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1620 struct mmc_card *card,
1621 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001622{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001623 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1624 /* Legacy mode imposes restrictions on transfers. */
1625 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1626 brq->data.blocks = 1;
1627
1628 if (brq->data.blocks > card->ext_csd.rel_sectors)
1629 brq->data.blocks = card->ext_csd.rel_sectors;
1630 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1631 brq->data.blocks = 1;
1632 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001633}
1634
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001635#define CMD_ERRORS \
1636 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1637 R1_ADDRESS_ERROR | /* Misaligned address */ \
1638 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1639 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1640 R1_CC_ERROR | /* Card controller error */ \
1641 R1_ERROR) /* General/unknown error */
1642
Per Forlinee8a43a2011-07-01 18:55:33 +02001643static int mmc_blk_err_check(struct mmc_card *card,
1644 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001645{
Per Forlinee8a43a2011-07-01 18:55:33 +02001646 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1647 mmc_active);
1648 struct mmc_blk_request *brq = &mq_mrq->brq;
1649 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001650 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001651 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001652
1653 /*
1654 * sbc.error indicates a problem with the set block count
1655 * command. No data will have been transferred.
1656 *
1657 * cmd.error indicates a problem with the r/w command. No
1658 * data will have been transferred.
1659 *
1660 * stop.error indicates a problem with the stop command. Data
1661 * may have been transferred, or may still be transferring.
1662 */
Adrian Hunter67716322011-08-29 16:42:15 +03001663 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1664 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001665 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001666 case ERR_RETRY:
1667 return MMC_BLK_RETRY;
1668 case ERR_ABORT:
1669 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301670 case ERR_NOMEDIUM:
1671 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001672 case ERR_CONTINUE:
1673 break;
1674 }
1675 }
1676
1677 /*
1678 * Check for errors relating to the execution of the
1679 * initial command - such as address errors. No data
1680 * has been transferred.
1681 */
1682 if (brq->cmd.resp[0] & CMD_ERRORS) {
1683 pr_err("%s: r/w command failed, status = %#x\n",
1684 req->rq_disk->disk_name, brq->cmd.resp[0]);
1685 return MMC_BLK_ABORT;
1686 }
1687
1688 /*
1689 * Everything else is either success, or a data error of some
1690 * kind. If it was a write, we may have transitioned to
1691 * program mode, which we have to wait for it to complete.
1692 */
1693 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001694 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001695
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001696 /* Check stop command response */
1697 if (brq->stop.resp[0] & R1_ERROR) {
1698 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1699 req->rq_disk->disk_name, __func__,
1700 brq->stop.resp[0]);
1701 gen_err = 1;
1702 }
1703
Ulf Hansson95a91292014-01-29 13:11:27 +01001704 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1705 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001706 if (err)
1707 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001708 }
1709
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001710 /* if general error occurs, retry the write operation. */
1711 if (gen_err) {
1712 pr_warn("%s: retrying write for general error\n",
1713 req->rq_disk->disk_name);
1714 return MMC_BLK_RETRY;
1715 }
1716
Per Forlind78d4a82011-07-01 18:55:30 +02001717 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001718 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001719 pr_debug("%s: retrying because a re-tune was needed\n",
1720 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001721 brq->retune_retry_done = 1;
1722 return MMC_BLK_RETRY;
1723 }
Per Forlind78d4a82011-07-01 18:55:30 +02001724 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1725 req->rq_disk->disk_name, brq->data.error,
1726 (unsigned)blk_rq_pos(req),
1727 (unsigned)blk_rq_sectors(req),
1728 brq->cmd.resp[0], brq->stop.resp[0]);
1729
1730 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001731 if (ecc_err)
1732 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001733 return MMC_BLK_DATA_ERR;
1734 } else {
1735 return MMC_BLK_CMD_ERR;
1736 }
1737 }
1738
Adrian Hunter67716322011-08-29 16:42:15 +03001739 if (!brq->data.bytes_xfered)
1740 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001741
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001742 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1743 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1744 return MMC_BLK_PARTIAL;
1745 else
1746 return MMC_BLK_SUCCESS;
1747 }
1748
Adrian Hunter67716322011-08-29 16:42:15 +03001749 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1750 return MMC_BLK_PARTIAL;
1751
1752 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001753}
1754
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001755static int mmc_blk_packed_err_check(struct mmc_card *card,
1756 struct mmc_async_req *areq)
1757{
1758 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1759 mmc_active);
1760 struct request *req = mq_rq->req;
1761 struct mmc_packed *packed = mq_rq->packed;
1762 int err, check, status;
1763 u8 *ext_csd;
1764
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001765 packed->retries--;
1766 check = mmc_blk_err_check(card, areq);
1767 err = get_card_status(card, &status, 0);
1768 if (err) {
1769 pr_err("%s: error %d sending status command\n",
1770 req->rq_disk->disk_name, err);
1771 return MMC_BLK_ABORT;
1772 }
1773
1774 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02001775 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001776 if (err) {
1777 pr_err("%s: error %d sending ext_csd\n",
1778 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02001779 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001780 }
1781
1782 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1783 EXT_CSD_PACKED_FAILURE) &&
1784 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1785 EXT_CSD_PACKED_GENERIC_ERROR)) {
1786 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1787 EXT_CSD_PACKED_INDEXED_ERROR) {
1788 packed->idx_failure =
1789 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1790 check = MMC_BLK_PARTIAL;
1791 }
1792 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1793 "failure index: %d\n",
1794 req->rq_disk->disk_name, packed->nr_entries,
1795 packed->blocks, packed->idx_failure);
1796 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001797 kfree(ext_csd);
1798 }
1799
1800 return check;
1801}
1802
Per Forlin54d49d72011-07-01 18:55:29 +02001803static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1804 struct mmc_card *card,
1805 int disable_multi,
1806 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Per Forlin54d49d72011-07-01 18:55:29 +02001808 u32 readcmd, writecmd;
1809 struct mmc_blk_request *brq = &mqrq->brq;
1810 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301812 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001814 /*
1815 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001816 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001817 */
Luca Porziod3df0462015-11-06 15:12:26 +00001818 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001819 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001820 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001821
Per Forlin54d49d72011-07-01 18:55:29 +02001822 memset(brq, 0, sizeof(struct mmc_blk_request));
1823 brq->mrq.cmd = &brq->cmd;
1824 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Per Forlin54d49d72011-07-01 18:55:29 +02001826 brq->cmd.arg = blk_rq_pos(req);
1827 if (!mmc_card_blockaddr(card))
1828 brq->cmd.arg <<= 9;
1829 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1830 brq->data.blksz = 512;
1831 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1832 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001833 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Asutosh Dasf0665412012-07-27 18:10:19 +05301835 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02001836 /*
1837 * The block layer doesn't support all sector count
1838 * restrictions, so we need to be prepared for too big
1839 * requests.
1840 */
1841 if (brq->data.blocks > card->host->max_blk_count)
1842 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001844 if (brq->data.blocks > 1) {
1845 /*
1846 * After a read error, we redo the request one sector
1847 * at a time in order to accurately determine which
1848 * sectors can be read successfully.
1849 */
1850 if (disable_multi)
1851 brq->data.blocks = 1;
1852
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001853 /*
1854 * Some controllers have HW issues while operating
1855 * in multiple I/O mode
1856 */
1857 if (card->host->ops->multi_io_quirk)
1858 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1859 (rq_data_dir(req) == READ) ?
1860 MMC_DATA_READ : MMC_DATA_WRITE,
1861 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001862 }
Per Forlin54d49d72011-07-01 18:55:29 +02001863
1864 if (brq->data.blocks > 1 || do_rel_wr) {
1865 /* SPI multiblock writes terminate using a special
1866 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001867 */
Per Forlin54d49d72011-07-01 18:55:29 +02001868 if (!mmc_host_is_spi(card->host) ||
1869 rq_data_dir(req) == READ)
1870 brq->mrq.stop = &brq->stop;
1871 readcmd = MMC_READ_MULTIPLE_BLOCK;
1872 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1873 } else {
1874 brq->mrq.stop = NULL;
1875 readcmd = MMC_READ_SINGLE_BLOCK;
1876 writecmd = MMC_WRITE_BLOCK;
1877 }
1878 if (rq_data_dir(req) == READ) {
1879 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001880 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001881 if (brq->mrq.stop)
1882 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1883 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001884 } else {
1885 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001886 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001887 if (brq->mrq.stop)
1888 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1889 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001890 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001891
Per Forlin54d49d72011-07-01 18:55:29 +02001892 if (do_rel_wr)
1893 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001894
Per Forlin54d49d72011-07-01 18:55:29 +02001895 /*
Saugata Das42659002011-12-21 13:09:17 +05301896 * Data tag is used only during writing meta data to speed
1897 * up write and any subsequent read of this meta data
1898 */
1899 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1900 (req->cmd_flags & REQ_META) &&
1901 (rq_data_dir(req) == WRITE) &&
1902 ((brq->data.blocks * brq->data.blksz) >=
1903 card->ext_csd.data_tag_unit_size);
1904
1905 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001906 * Pre-defined multi-block transfers are preferable to
1907 * open ended-ones (and necessary for reliable writes).
1908 * However, it is not sufficient to just send CMD23,
1909 * and avoid the final CMD12, as on an error condition
1910 * CMD12 (stop) needs to be sent anyway. This, coupled
1911 * with Auto-CMD23 enhancements provided by some
1912 * hosts, means that the complexity of dealing
1913 * with this is best left to the host. If CMD23 is
1914 * supported by card and host, we'll fill sbc in and let
1915 * the host deal with handling it correctly. This means
1916 * that for hosts that don't expose MMC_CAP_CMD23, no
1917 * change of behavior will be observed.
1918 *
1919 * N.B: Some MMC cards experience perf degradation.
1920 * We'll avoid using CMD23-bounded multiblock writes for
1921 * these, while retaining features like reliable writes.
1922 */
Saugata Das42659002011-12-21 13:09:17 +05301923 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1924 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1925 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001926 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1927 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301928 (do_rel_wr ? (1 << 31) : 0) |
1929 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001930 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1931 brq->mrq.sbc = &brq->sbc;
1932 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001933
Per Forlin54d49d72011-07-01 18:55:29 +02001934 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001935
Per Forlin54d49d72011-07-01 18:55:29 +02001936 brq->data.sg = mqrq->sg;
1937 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001938
Per Forlin54d49d72011-07-01 18:55:29 +02001939 /*
1940 * Adjust the sg list so it is the same size as the
1941 * request.
1942 */
1943 if (brq->data.blocks != blk_rq_sectors(req)) {
1944 int i, data_size = brq->data.blocks << 9;
1945 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001946
Per Forlin54d49d72011-07-01 18:55:29 +02001947 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1948 data_size -= sg->length;
1949 if (data_size <= 0) {
1950 sg->length += data_size;
1951 i++;
1952 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001953 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001954 }
Per Forlin54d49d72011-07-01 18:55:29 +02001955 brq->data.sg_len = i;
1956 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001957
Per Forlinee8a43a2011-07-01 18:55:33 +02001958 mqrq->mmc_active.mrq = &brq->mrq;
1959 mqrq->mmc_active.err_check = mmc_blk_err_check;
1960
Per Forlin54d49d72011-07-01 18:55:29 +02001961 mmc_queue_bounce_pre(mqrq);
1962}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001964static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1965 struct mmc_card *card)
1966{
1967 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1968 unsigned int max_seg_sz = queue_max_segment_size(q);
1969 unsigned int len, nr_segs = 0;
1970
1971 do {
1972 len = min(hdr_sz, max_seg_sz);
1973 hdr_sz -= len;
1974 nr_segs++;
1975 } while (hdr_sz);
1976
1977 return nr_segs;
1978}
1979
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02001980static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1981 struct request *req)
1982{
1983 struct mmc_host *host = mq->card->host;
1984 int data_dir;
1985
1986 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1987 return;
1988
Maya Erez8e2b3c32012-12-02 13:27:15 +02001989 /* Support for the write packing on eMMC 4.5 or later */
1990 if (mq->card->ext_csd.rev <= 5)
1991 return;
1992
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02001993 /*
1994 * In case the packing control is not supported by the host, it should
1995 * not have an effect on the write packing. Therefore we have to enable
1996 * the write packing
1997 */
1998 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1999 mq->wr_packing_enabled = true;
2000 return;
2001 }
2002
2003 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2004 if (mq->num_of_potential_packed_wr_reqs >
2005 mq->num_wr_reqs_to_start_packing)
2006 mq->wr_packing_enabled = true;
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002007 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002008 return;
2009 }
2010
2011 data_dir = rq_data_dir(req);
2012
2013 if (data_dir == READ) {
2014 mq->num_of_potential_packed_wr_reqs = 0;
2015 mq->wr_packing_enabled = false;
2016 return;
2017 } else if (data_dir == WRITE) {
2018 mq->num_of_potential_packed_wr_reqs++;
2019 }
2020
2021 if (mq->num_of_potential_packed_wr_reqs >
2022 mq->num_wr_reqs_to_start_packing)
2023 mq->wr_packing_enabled = true;
2024}
2025
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002026struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2027{
2028 if (!card)
2029 return NULL;
2030
2031 return &card->wr_pack_stats;
2032}
2033EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2034
2035void mmc_blk_init_packed_statistics(struct mmc_card *card)
2036{
2037 int max_num_of_packed_reqs = 0;
2038
2039 if (!card || !card->wr_pack_stats.packing_events)
2040 return;
2041
2042 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2043
2044 spin_lock(&card->wr_pack_stats.lock);
2045 memset(card->wr_pack_stats.packing_events, 0,
2046 (max_num_of_packed_reqs + 1) *
2047 sizeof(*card->wr_pack_stats.packing_events));
2048 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2049 sizeof(card->wr_pack_stats.pack_stop_reason));
2050 card->wr_pack_stats.enabled = true;
2051 spin_unlock(&card->wr_pack_stats.lock);
2052}
2053EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2054
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002055static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2056{
2057 struct request_queue *q = mq->queue;
2058 struct mmc_card *card = mq->card;
2059 struct request *cur = req, *next = NULL;
2060 struct mmc_blk_data *md = mq->data;
2061 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2062 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2063 unsigned int req_sectors = 0, phys_segments = 0;
2064 unsigned int max_blk_count, max_phys_segs;
2065 bool put_back = true;
2066 u8 max_packed_rw = 0;
2067 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002068 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002069
Shawn Lin96e52da2016-08-26 08:49:55 +08002070 /*
2071 * We don't need to check packed for any further
2072 * operation of packed stuff as we set MMC_PACKED_NONE
2073 * and return zero for reqs if geting null packed. Also
2074 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2075 * it again when removing blk req.
2076 */
2077 if (!mqrq->packed) {
2078 md->flags &= (~MMC_BLK_PACKED_CMD);
2079 goto no_packed;
2080 }
2081
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002082 if (!(md->flags & MMC_BLK_PACKED_CMD))
2083 goto no_packed;
2084
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002085 if (!mq->wr_packing_enabled)
2086 goto no_packed;
2087
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002088 if ((rq_data_dir(cur) == WRITE) &&
2089 mmc_host_packed_wr(card->host))
2090 max_packed_rw = card->ext_csd.max_packed_writes;
2091
2092 if (max_packed_rw == 0)
2093 goto no_packed;
2094
2095 if (mmc_req_rel_wr(cur) &&
2096 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2097 goto no_packed;
2098
2099 if (mmc_large_sector(card) &&
2100 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2101 goto no_packed;
2102
2103 mmc_blk_clear_packed(mqrq);
2104
2105 max_blk_count = min(card->host->max_blk_count,
2106 card->host->max_req_size >> 9);
2107 if (unlikely(max_blk_count > 0xffff))
2108 max_blk_count = 0xffff;
2109
2110 max_phys_segs = queue_max_segments(q);
2111 req_sectors += blk_rq_sectors(cur);
2112 phys_segments += cur->nr_phys_segments;
2113
2114 if (rq_data_dir(cur) == WRITE) {
2115 req_sectors += mmc_large_sector(card) ? 8 : 1;
2116 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2117 }
2118
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002119 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002120 do {
2121 if (reqs >= max_packed_rw - 1) {
2122 put_back = false;
2123 break;
2124 }
2125
2126 spin_lock_irq(q->queue_lock);
2127 next = blk_fetch_request(q);
2128 spin_unlock_irq(q->queue_lock);
2129 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002130 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002131 put_back = false;
2132 break;
2133 }
2134
2135 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002136 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2137 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002138 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002139 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002140
Mike Christie3a5e02c2016-06-05 14:32:23 -05002141 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002142 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002143 req_op(next) == REQ_OP_FLUSH) {
2144 if (req_op(next) != REQ_OP_SECURE_ERASE)
2145 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002146 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002147 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002148
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002149 if (rq_data_dir(cur) != rq_data_dir(next)) {
2150 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002151 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002152 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002153
2154 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002155 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2156 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002157 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002158 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002159
2160 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002161 if (req_sectors > max_blk_count) {
2162 if (stats->enabled)
2163 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002164 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002165 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002166
2167 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002168 if (phys_segments > max_phys_segs) {
2169 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002170 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002171 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002172
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002173 if (rq_data_dir(next) == WRITE)
2174 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002175 list_add_tail(&next->queuelist, &mqrq->packed->list);
2176 cur = next;
2177 reqs++;
2178 } while (1);
2179
2180 if (put_back) {
2181 spin_lock_irq(q->queue_lock);
2182 blk_requeue_request(q, next);
2183 spin_unlock_irq(q->queue_lock);
2184 }
2185
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002186 if (stats->enabled) {
2187 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2188 stats->packing_events[reqs + 1]++;
2189 if (reqs + 1 == max_packed_rw)
2190 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2191 }
2192
2193 spin_unlock(&stats->lock);
2194
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002195 if (reqs > 0) {
2196 list_add(&req->queuelist, &mqrq->packed->list);
2197 mqrq->packed->nr_entries = ++reqs;
2198 mqrq->packed->retries = reqs;
2199 return reqs;
2200 }
2201
2202no_packed:
2203 mqrq->cmd_type = MMC_PACKED_NONE;
2204 return 0;
2205}
2206
2207static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2208 struct mmc_card *card,
2209 struct mmc_queue *mq)
2210{
2211 struct mmc_blk_request *brq = &mqrq->brq;
2212 struct request *req = mqrq->req;
2213 struct request *prq;
2214 struct mmc_blk_data *md = mq->data;
2215 struct mmc_packed *packed = mqrq->packed;
2216 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002217 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002218 u8 hdr_blocks;
2219 u8 i = 1;
2220
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002221 mqrq->cmd_type = MMC_PACKED_WRITE;
2222 packed->blocks = 0;
2223 packed->idx_failure = MMC_PACKED_NR_IDX;
2224
2225 packed_cmd_hdr = packed->cmd_hdr;
2226 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002227 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2228 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002229 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2230
2231 /*
2232 * Argument for each entry of packed group
2233 */
2234 list_for_each_entry(prq, &packed->list, queuelist) {
2235 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2236 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2237 (prq->cmd_flags & REQ_META) &&
2238 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002239 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002240 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002241 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002242 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2243 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002244 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002245 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002246 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002247 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002248 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002249 packed->blocks += blk_rq_sectors(prq);
2250 i++;
2251 }
2252
2253 memset(brq, 0, sizeof(struct mmc_blk_request));
2254 brq->mrq.cmd = &brq->cmd;
2255 brq->mrq.data = &brq->data;
2256 brq->mrq.sbc = &brq->sbc;
2257 brq->mrq.stop = &brq->stop;
2258
2259 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2260 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2261 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2262
2263 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2264 brq->cmd.arg = blk_rq_pos(req);
2265 if (!mmc_card_blockaddr(card))
2266 brq->cmd.arg <<= 9;
2267 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2268
2269 brq->data.blksz = 512;
2270 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002271 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302272 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002273
2274 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2275 brq->stop.arg = 0;
2276 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2277
2278 mmc_set_data_timeout(&brq->data, card);
2279
2280 brq->data.sg = mqrq->sg;
2281 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2282
2283 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002284
2285 /*
2286 * This is intended for packed commands tests usage - in case these
2287 * functions are not in use the respective pointers are NULL
2288 */
2289 if (mq->err_check_fn)
2290 mqrq->mmc_active.err_check = mq->err_check_fn;
2291 else
2292 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2293
2294 if (mq->packed_test_fn)
2295 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002296
2297 mmc_queue_bounce_pre(mqrq);
2298}
2299
Adrian Hunter67716322011-08-29 16:42:15 +03002300static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2301 struct mmc_blk_request *brq, struct request *req,
2302 int ret)
2303{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002304 struct mmc_queue_req *mq_rq;
2305 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2306
Adrian Hunter67716322011-08-29 16:42:15 +03002307 /*
2308 * If this is an SD card and we're writing, we can first
2309 * mark the known good sectors as ok.
2310 *
2311 * If the card is not SD, we can still ok written sectors
2312 * as reported by the controller (which might be less than
2313 * the real number of written sectors, but never more).
2314 */
2315 if (mmc_card_sd(card)) {
2316 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302317 if (!brq->data.fault_injected) {
2318 blocks = mmc_sd_num_wr_blocks(card);
2319 if (blocks != (u32)-1)
2320 ret = blk_end_request(req, 0, blocks << 9);
2321 } else
2322 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002323 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002324 if (!mmc_packed_cmd(mq_rq->cmd_type))
2325 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002326 }
2327 return ret;
2328}
2329
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002330static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2331{
2332 struct request *prq;
2333 struct mmc_packed *packed = mq_rq->packed;
2334 int idx = packed->idx_failure, i = 0;
2335 int ret = 0;
2336
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002337 while (!list_empty(&packed->list)) {
2338 prq = list_entry_rq(packed->list.next);
2339 if (idx == i) {
2340 /* retry from error index */
2341 packed->nr_entries -= idx;
2342 mq_rq->req = prq;
2343 ret = 1;
2344
2345 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2346 list_del_init(&prq->queuelist);
2347 mmc_blk_clear_packed(mq_rq);
2348 }
2349 return ret;
2350 }
2351 list_del_init(&prq->queuelist);
2352 blk_end_request(prq, 0, blk_rq_bytes(prq));
2353 i++;
2354 }
2355
2356 mmc_blk_clear_packed(mq_rq);
2357 return ret;
2358}
2359
2360static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2361{
2362 struct request *prq;
2363 struct mmc_packed *packed = mq_rq->packed;
2364
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002365 while (!list_empty(&packed->list)) {
2366 prq = list_entry_rq(packed->list.next);
2367 list_del_init(&prq->queuelist);
2368 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2369 }
2370
2371 mmc_blk_clear_packed(mq_rq);
2372}
2373
2374static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2375 struct mmc_queue_req *mq_rq)
2376{
2377 struct request *prq;
2378 struct request_queue *q = mq->queue;
2379 struct mmc_packed *packed = mq_rq->packed;
2380
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002381 while (!list_empty(&packed->list)) {
2382 prq = list_entry_rq(packed->list.prev);
2383 if (prq->queuelist.prev != &packed->list) {
2384 list_del_init(&prq->queuelist);
2385 spin_lock_irq(q->queue_lock);
2386 blk_requeue_request(mq->queue, prq);
2387 spin_unlock_irq(q->queue_lock);
2388 } else {
2389 list_del_init(&prq->queuelist);
2390 }
2391 }
2392
2393 mmc_blk_clear_packed(mq_rq);
2394}
2395
Per Forlinee8a43a2011-07-01 18:55:33 +02002396static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02002397{
2398 struct mmc_blk_data *md = mq->data;
2399 struct mmc_card *card = md->queue.card;
2400 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002401 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002402 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002403 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302404 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02002405 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002406 const u8 packed_nr = 2;
2407 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002408#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2409 unsigned long waitfor = jiffies;
2410#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02002411
2412 if (!rqc && !mq->mqrq_prev->req)
2413 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002414
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002415 if (rqc)
2416 reqs = mmc_blk_prep_packed_list(mq, rqc);
2417
Per Forlin54d49d72011-07-01 18:55:29 +02002418 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002419 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302420 /*
2421 * When 4KB native sector is enabled, only 8 blocks
2422 * multiple read or write is allowed
2423 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00002424 if (mmc_large_sector(card) &&
2425 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302426 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
2427 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002428 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302429 goto cmd_abort;
2430 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002431
2432 if (reqs >= packed_nr)
2433 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2434 card, mq);
2435 else
2436 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002437 areq = &mq->mqrq_cur->mmc_active;
2438 } else
2439 areq = NULL;
2440 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002441 if (!areq) {
2442 if (status == MMC_BLK_NEW_REQUEST)
2443 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02002444 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002445 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002446
Per Forlinee8a43a2011-07-01 18:55:33 +02002447 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2448 brq = &mq_rq->brq;
2449 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002450 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002451 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002452
Per Forlind78d4a82011-07-01 18:55:30 +02002453 switch (status) {
2454 case MMC_BLK_SUCCESS:
2455 case MMC_BLK_PARTIAL:
2456 /*
2457 * A block was successfully transferred.
2458 */
Adrian Hunter67716322011-08-29 16:42:15 +03002459 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002460
Mark Salyzyn6904e432016-01-28 11:12:25 -08002461 mmc_blk_simulate_delay(mq, rqc, waitfor);
2462
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002463 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2464 ret = mmc_blk_end_packed_req(mq_rq);
2465 break;
2466 } else {
2467 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002468 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002469 }
2470
Adrian Hunter67716322011-08-29 16:42:15 +03002471 /*
2472 * If the blk_end_request function returns non-zero even
2473 * though all data has been transferred and no errors
2474 * were returned by the host controller, it's a bug.
2475 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002476 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302477 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002478 __func__, blk_rq_bytes(req),
2479 brq->data.bytes_xfered);
2480 rqc = NULL;
2481 goto cmd_abort;
2482 }
Per Forlind78d4a82011-07-01 18:55:30 +02002483 break;
2484 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002485 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002486 if (mmc_blk_reset(md, card->host, type))
2487 goto cmd_abort;
2488 if (!ret)
2489 goto start_new_req;
2490 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002491 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002492 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a82011-07-01 18:55:30 +02002493 if (retry++ < 5)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002494 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002495 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002496 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002497 if (!mmc_blk_reset(md, card->host, type))
2498 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002499 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002500 case MMC_BLK_DATA_ERR: {
2501 int err;
2502
2503 err = mmc_blk_reset(md, card->host, type);
2504 if (!err)
2505 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05302506 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002507 }
2508 case MMC_BLK_ECC_ERR:
2509 if (brq->data.blocks > 1) {
2510 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002511 pr_warn("%s: retrying using single block read\n",
2512 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002513 disable_multi = 1;
2514 break;
2515 }
Per Forlind78d4a82011-07-01 18:55:30 +02002516 /*
2517 * After an error, we redo I/O one sector at a
2518 * time, so we only reach here after trying to
2519 * read a single sector.
2520 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302521 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002522 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002523 if (!ret)
2524 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002525 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302526 case MMC_BLK_NOMEDIUM:
2527 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002528 default:
2529 pr_err("%s: Unhandled return value (%d)",
2530 req->rq_disk->disk_name, status);
2531 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002532 }
2533
Per Forlinee8a43a2011-07-01 18:55:33 +02002534 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002535 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2536 if (!mq_rq->packed->retries)
2537 goto cmd_abort;
2538 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2539 mmc_start_req(card->host,
2540 &mq_rq->mmc_active, NULL);
2541 } else {
2542
2543 /*
2544 * In case of a incomplete request
2545 * prepare it again and resend.
2546 */
2547 mmc_blk_rw_rq_prep(mq_rq, card,
2548 disable_multi, mq);
2549 mmc_start_req(card->host,
2550 &mq_rq->mmc_active, NULL);
2551 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002552 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 } while (ret);
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 return 1;
2557
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002558 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002559 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2560 mmc_blk_abort_packed_req(mq_rq);
2561 } else {
2562 if (mmc_card_removed(card))
2563 req->cmd_flags |= REQ_QUIET;
2564 while (ret)
2565 ret = blk_end_request(req, -EIO,
2566 blk_rq_cur_bytes(req));
2567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Per Forlinee8a43a2011-07-01 18:55:33 +02002569 start_new_req:
2570 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002571 if (mmc_card_removed(card)) {
2572 rqc->cmd_flags |= REQ_QUIET;
2573 blk_end_request_all(rqc, -EIO);
2574 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002575 /*
2576 * If current request is packed, it needs to put back.
2577 */
2578 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2579 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2580
Seungwon Jeon7a819022013-01-22 19:48:07 +09002581 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2582 mmc_start_req(card->host,
2583 &mq->mqrq_cur->mmc_active, NULL);
2584 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002585 }
2586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 return 0;
2588}
2589
Linus Walleij29eb7bd2016-09-20 11:34:38 +02002590int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002591{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002592 int ret;
2593 struct mmc_blk_data *md = mq->data;
2594 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002595 struct mmc_host *host = card->host;
2596 unsigned long flags;
Adrian Hunter869c5542016-08-25 14:11:43 -06002597 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002598
Per Forlinee8a43a2011-07-01 18:55:33 +02002599 if (req && !mq->mqrq_prev->req)
2600 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002601 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002602
Andrei Warkentin371a6892011-04-11 18:10:25 -05002603 ret = mmc_blk_part_switch(card, md);
2604 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002605 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302606 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002607 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002608 ret = 0;
2609 goto out;
2610 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002611
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002612 mmc_blk_write_packing_control(mq, req);
2613
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002614 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05002615 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002616 /* complete ongoing async transfer before issuing discard */
2617 if (card->host->areq)
2618 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02002619 ret = mmc_blk_issue_discard_rq(mq, req);
2620 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
2621 /* complete ongoing async transfer before issuing secure erase*/
2622 if (card->host->areq)
2623 mmc_blk_issue_rw_rq(mq, NULL);
2624 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05002625 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002626 /* complete ongoing async transfer before issuing flush */
2627 if (card->host->areq)
2628 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002629 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002630 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002631 if (!req && host->areq) {
2632 spin_lock_irqsave(&host->context_info.lock, flags);
2633 host->context_info.is_waiting_last_req = true;
2634 spin_unlock_irqrestore(&host->context_info.lock, flags);
2635 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002636 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002637 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002638
Andrei Warkentin371a6892011-04-11 18:10:25 -05002639out:
Adrian Hunter869c5542016-08-25 14:11:43 -06002640 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002641 /*
2642 * Release host when there are no more requests
2643 * and after special request(discard, flush) is done.
2644 * In case sepecial request, there is no reentry to
2645 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2646 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002647 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002648 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002649}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Russell Kinga6f6c962006-01-03 22:38:44 +00002651static inline int mmc_blk_readonly(struct mmc_card *card)
2652{
2653 return mmc_card_readonly(card) ||
2654 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2655}
2656
Andrei Warkentin371a6892011-04-11 18:10:25 -05002657static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2658 struct device *parent,
2659 sector_t size,
2660 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002661 const char *subname,
2662 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
2664 struct mmc_blk_data *md;
2665 int devidx, ret;
2666
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002667again:
2668 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
2669 return ERR_PTR(-ENOMEM);
2670
2671 spin_lock(&mmc_blk_lock);
2672 ret = ida_get_new(&mmc_blk_ida, &devidx);
2673 spin_unlock(&mmc_blk_lock);
2674
2675 if (ret == -EAGAIN)
2676 goto again;
2677 else if (ret)
2678 return ERR_PTR(ret);
2679
2680 if (devidx >= max_devices) {
2681 ret = -ENOSPC;
2682 goto out;
2683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002685 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002686 if (!md) {
2687 ret = -ENOMEM;
2688 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002690
Johan Rudholmadd710e2011-12-02 08:51:06 +01002691 md->area_type = area_type;
2692
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002693 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002694 * Set the read-only status based on the supported commands
2695 * and the write protect switch.
2696 */
2697 md->read_only = mmc_blk_readonly(card);
2698
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002699 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002700 if (md->disk == NULL) {
2701 ret = -ENOMEM;
2702 goto err_kfree;
2703 }
2704
2705 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002706 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002707 md->usage = 1;
2708
Adrian Hunterd09408a2011-06-23 13:40:28 +03002709 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002710 if (ret)
2711 goto err_putdisk;
2712
Russell Kinga6f6c962006-01-03 22:38:44 +00002713 md->queue.data = md;
2714
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002715 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002716 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002717 md->disk->fops = &mmc_bdops;
2718 md->disk->private_data = md;
2719 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002720 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002721 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002722 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002723 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002724 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002725
2726 /*
2727 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2728 *
2729 * - be set for removable media with permanent block devices
2730 * - be unset for removable block devices with permanent media
2731 *
2732 * Since MMC block devices clearly fall under the second
2733 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2734 * should use the block device creation/destruction hotplug
2735 * messages to tell when the card is present.
2736 */
2737
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002738 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02002739 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002740
Saugata Dasa5075eb2012-05-17 16:32:21 +05302741 if (mmc_card_mmc(card))
2742 blk_queue_logical_block_size(md->queue.queue,
2743 card->ext_csd.data_sector_size);
2744 else
2745 blk_queue_logical_block_size(md->queue.queue, 512);
2746
Andrei Warkentin371a6892011-04-11 18:10:25 -05002747 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002748
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002749 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02002750 if ((mmc_card_mmc(card) &&
2751 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002752 (mmc_card_sd(card) &&
2753 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2754 md->flags |= MMC_BLK_CMD23;
2755 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002756
2757 if (mmc_card_mmc(card) &&
2758 md->flags & MMC_BLK_CMD23 &&
2759 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2760 card->ext_csd.rel_sectors)) {
2761 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06002762 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002763 }
2764
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002765 if (mmc_card_mmc(card) &&
2766 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2767 (md->flags & MMC_BLK_CMD23) &&
2768 card->ext_csd.packed_event_en) {
2769 if (!mmc_packed_init(&md->queue, card))
2770 md->flags |= MMC_BLK_PACKED_CMD;
2771 }
2772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002774
2775 err_putdisk:
2776 put_disk(md->disk);
2777 err_kfree:
2778 kfree(md);
2779 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002780 spin_lock(&mmc_blk_lock);
2781 ida_remove(&mmc_blk_ida, devidx);
2782 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00002783 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
Andrei Warkentin371a6892011-04-11 18:10:25 -05002786static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2787{
2788 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002789
2790 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2791 /*
2792 * The EXT_CSD sector count is in number or 512 byte
2793 * sectors.
2794 */
2795 size = card->ext_csd.sectors;
2796 } else {
2797 /*
2798 * The CSD capacity field is in units of read_blkbits.
2799 * set_capacity takes units of 512 bytes.
2800 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002801 size = (typeof(sector_t))card->csd.capacity
2802 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002803 }
2804
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002805 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002806 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002807}
2808
2809static int mmc_blk_alloc_part(struct mmc_card *card,
2810 struct mmc_blk_data *md,
2811 unsigned int part_type,
2812 sector_t size,
2813 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002814 const char *subname,
2815 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002816{
2817 char cap_str[10];
2818 struct mmc_blk_data *part_md;
2819
2820 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002821 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002822 if (IS_ERR(part_md))
2823 return PTR_ERR(part_md);
2824 part_md->part_type = part_type;
2825 list_add(&part_md->part, &md->part);
2826
James Bottomleyb9f28d82015-03-05 18:47:01 -08002827 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002828 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302829 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002830 part_md->disk->disk_name, mmc_card_id(card),
2831 mmc_card_name(card), part_md->part_type, cap_str);
2832 return 0;
2833}
2834
Namjae Jeone0c368d2011-10-06 23:41:38 +09002835/* MMC Physical partitions consist of two boot partitions and
2836 * up to four general purpose partitions.
2837 * For each partition enabled in EXT_CSD a block device will be allocatedi
2838 * to provide access to the partition.
2839 */
2840
Andrei Warkentin371a6892011-04-11 18:10:25 -05002841static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2842{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002843 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002844
2845 if (!mmc_card_mmc(card))
2846 return 0;
2847
Namjae Jeone0c368d2011-10-06 23:41:38 +09002848 for (idx = 0; idx < card->nr_parts; idx++) {
2849 if (card->part[idx].size) {
2850 ret = mmc_blk_alloc_part(card, md,
2851 card->part[idx].part_cfg,
2852 card->part[idx].size >> 9,
2853 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002854 card->part[idx].name,
2855 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002856 if (ret)
2857 return ret;
2858 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002859 }
2860
2861 return ret;
2862}
2863
Andrei Warkentin371a6892011-04-11 18:10:25 -05002864static void mmc_blk_remove_req(struct mmc_blk_data *md)
2865{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002866 struct mmc_card *card;
2867
Andrei Warkentin371a6892011-04-11 18:10:25 -05002868 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002869 /*
2870 * Flush remaining requests and free queues. It
2871 * is freeing the queue that stops new requests
2872 * from being accepted.
2873 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002874 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002875 mmc_cleanup_queue(&md->queue);
2876 if (md->flags & MMC_BLK_PACKED_CMD)
2877 mmc_packed_clean(&md->queue);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002878 device_remove_file(disk_to_dev(md->disk),
2879 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002880 if (md->disk->flags & GENHD_FL_UP) {
2881 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002882 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2883 card->ext_csd.boot_ro_lockable)
2884 device_remove_file(disk_to_dev(md->disk),
2885 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08002886#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2887 device_remove_file(disk_to_dev(md->disk),
2888 &dev_attr_max_write_speed);
2889 device_remove_file(disk_to_dev(md->disk),
2890 &dev_attr_max_read_speed);
2891 device_remove_file(disk_to_dev(md->disk),
2892 &dev_attr_cache_size);
2893#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002894
Andrei Warkentin371a6892011-04-11 18:10:25 -05002895 del_gendisk(md->disk);
2896 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002897 mmc_blk_put(md);
2898 }
2899}
2900
2901static void mmc_blk_remove_parts(struct mmc_card *card,
2902 struct mmc_blk_data *md)
2903{
2904 struct list_head *pos, *q;
2905 struct mmc_blk_data *part_md;
2906
2907 list_for_each_safe(pos, q, &md->part) {
2908 part_md = list_entry(pos, struct mmc_blk_data, part);
2909 list_del(pos);
2910 mmc_blk_remove_req(part_md);
2911 }
2912}
2913
2914static int mmc_add_disk(struct mmc_blk_data *md)
2915{
2916 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002917 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002918
Dan Williams307d8e62016-06-20 10:40:44 -07002919 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002920 md->force_ro.show = force_ro_show;
2921 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302922 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002923 md->force_ro.attr.name = "force_ro";
2924 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2925 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2926 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002927 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002928#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2929 atomic_set(&md->queue.max_write_speed, max_write_speed);
2930 ret = device_create_file(disk_to_dev(md->disk),
2931 &dev_attr_max_write_speed);
2932 if (ret)
2933 goto max_write_speed_fail;
2934 atomic_set(&md->queue.max_read_speed, max_read_speed);
2935 ret = device_create_file(disk_to_dev(md->disk),
2936 &dev_attr_max_read_speed);
2937 if (ret)
2938 goto max_read_speed_fail;
2939 atomic_set(&md->queue.cache_size, cache_size);
2940 atomic_long_set(&md->queue.cache_used, 0);
2941 md->queue.cache_jiffies = jiffies;
2942 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
2943 if (ret)
2944 goto cache_size_fail;
2945#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01002946
2947 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2948 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002949 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002950
2951 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2952 mode = S_IRUGO;
2953 else
2954 mode = S_IRUGO | S_IWUSR;
2955
2956 md->power_ro_lock.show = power_ro_lock_show;
2957 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002958 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002959 md->power_ro_lock.attr.mode = mode;
2960 md->power_ro_lock.attr.name =
2961 "ro_lock_until_next_power_on";
2962 ret = device_create_file(disk_to_dev(md->disk),
2963 &md->power_ro_lock);
2964 if (ret)
2965 goto power_ro_lock_fail;
2966 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002967
2968 md->num_wr_reqs_to_start_packing.show =
2969 num_wr_reqs_to_start_packing_show;
2970 md->num_wr_reqs_to_start_packing.store =
2971 num_wr_reqs_to_start_packing_store;
2972 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2973 md->num_wr_reqs_to_start_packing.attr.name =
2974 "num_wr_reqs_to_start_packing";
2975 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2976 ret = device_create_file(disk_to_dev(md->disk),
2977 &md->num_wr_reqs_to_start_packing);
2978 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02002979 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002980
Johan Rudholmadd710e2011-12-02 08:51:06 +01002981 return ret;
2982
Maya Erez17022402014-12-04 00:15:42 +02002983num_wr_reqs_to_start_packing_fail:
2984 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002985power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08002986#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2987 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
2988cache_size_fail:
2989 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
2990max_read_speed_fail:
2991 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
2992max_write_speed_fail:
2993#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01002994 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2995force_ro_fail:
2996 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002997
2998 return ret;
2999}
3000
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003001static const struct mmc_fixup blk_fixups[] =
3002{
Chris Ballc59d4472011-11-11 22:01:43 -05003003 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
3004 MMC_QUIRK_INAND_CMD38),
3005 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
3006 MMC_QUIRK_INAND_CMD38),
3007 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
3008 MMC_QUIRK_INAND_CMD38),
3009 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
3010 MMC_QUIRK_INAND_CMD38),
3011 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
3012 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003013
3014 /*
3015 * Some MMC cards experience performance degradation with CMD23
3016 * instead of CMD12-bounded multiblock transfers. For now we'll
3017 * black list what's bad...
3018 * - Certain Toshiba cards.
3019 *
3020 * N.B. This doesn't affect SD cards.
3021 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08003022 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3023 MMC_QUIRK_BLK_NO_CMD23),
3024 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3025 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003026 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003027 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003028 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003029 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003030 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003031 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003032
3033 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03003034 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003035 */
Chris Ballc59d4472011-11-11 22:01:43 -05003036 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003037 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03003038 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
3039 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003040
Ian Chen3550ccd2012-08-29 15:05:36 +09003041 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08003042 * Some Samsung MMC cards need longer data read timeout than
3043 * indicated in CSD.
3044 */
3045 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
3046 MMC_QUIRK_LONG_READ_TIME),
3047
3048 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09003049 * On these Samsung MoviNAND parts, performing secure erase or
3050 * secure trim can result in unrecoverable corruption due to a
3051 * firmware bug.
3052 */
3053 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3054 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3055 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3056 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3057 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3058 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3059 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3060 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3061 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3062 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3063 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3064 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3065 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3066 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3067 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3068 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3069
Shawn Linb5b4ff02015-08-12 13:08:32 +08003070 /*
3071 * On Some Kingston eMMCs, performing trim can result in
3072 * unrecoverable data conrruption occasionally due to a firmware bug.
3073 */
3074 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3075 MMC_QUIRK_TRIM_BROKEN),
3076 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3077 MMC_QUIRK_TRIM_BROKEN),
3078
Pratibhasagar V8d664e32014-12-03 18:26:42 +02003079 /* Some INAND MCP devices advertise incorrect timeout values */
3080 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
3081 MMC_QUIRK_INAND_DATA_TIMEOUT),
3082
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003083 END_FIXUP
3084};
3085
Ulf Hansson96541ba2015-04-14 13:06:12 +02003086static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003088 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003089 char cap_str[10];
3090
Pierre Ossman912490d2005-05-21 10:27:02 +01003091 /*
3092 * Check that the card supports the command class(es) we need.
3093 */
3094 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 return -ENODEV;
3096
Lukas Czerner5204d002014-06-18 13:18:07 +02003097 mmc_fixup_device(card, blk_fixups);
3098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 md = mmc_blk_alloc(card);
3100 if (IS_ERR(md))
3101 return PTR_ERR(md);
3102
James Bottomleyb9f28d82015-03-05 18:47:01 -08003103 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003104 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303105 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003107 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Andrei Warkentin371a6892011-04-11 18:10:25 -05003109 if (mmc_blk_alloc_parts(card, md))
3110 goto out;
3111
Ulf Hansson96541ba2015-04-14 13:06:12 +02003112 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003113
Andrei Warkentin371a6892011-04-11 18:10:25 -05003114 if (mmc_add_disk(md))
3115 goto out;
3116
3117 list_for_each_entry(part_md, &md->part, part) {
3118 if (mmc_add_disk(part_md))
3119 goto out;
3120 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003121
3122 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
3123 pm_runtime_use_autosuspend(&card->dev);
3124
3125 /*
3126 * Don't enable runtime PM for SD-combo cards here. Leave that
3127 * decision to be taken during the SDIO init sequence instead.
3128 */
3129 if (card->type != MMC_TYPE_SD_COMBO) {
3130 pm_runtime_set_active(&card->dev);
3131 pm_runtime_enable(&card->dev);
3132 }
3133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return 0;
3135
3136 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05003137 mmc_blk_remove_parts(card, md);
3138 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01003139 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140}
3141
Ulf Hansson96541ba2015-04-14 13:06:12 +02003142static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003144 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Andrei Warkentin371a6892011-04-11 18:10:25 -05003146 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003147 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003148 mmc_claim_host(card->host);
3149 mmc_blk_part_switch(card, md);
3150 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003151 if (card->type != MMC_TYPE_SD_COMBO)
3152 pm_runtime_disable(&card->dev);
3153 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003154 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02003155 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156}
3157
Ulf Hansson96541ba2015-04-14 13:06:12 +02003158static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003160 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02003161 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303162 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
3164 if (md) {
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303165 rc = mmc_queue_suspend(&md->queue);
3166 if (rc)
3167 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003168 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303169 rc = mmc_queue_suspend(&part_md->queue);
3170 if (rc)
3171 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303174 goto out;
3175
3176 out_resume:
3177 mmc_queue_resume(&md->queue);
3178 list_for_each_entry(part_md, &md->part, part) {
3179 mmc_queue_resume(&part_md->queue);
3180 }
3181 out:
3182 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183}
3184
Ulf Hansson96541ba2015-04-14 13:06:12 +02003185static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02003186{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003187 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003188}
3189
Ulf Hansson0967edc2014-10-06 11:29:42 +02003190#ifdef CONFIG_PM_SLEEP
3191static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02003192{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003193 struct mmc_card *card = mmc_dev_to_card(dev);
3194
3195 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003196}
3197
Ulf Hansson0967edc2014-10-06 11:29:42 +02003198static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003200 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02003201 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
3203 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003204 /*
3205 * Resume involves the card going into idle state,
3206 * so current partition is always the main one.
3207 */
3208 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003210 list_for_each_entry(part_md, &md->part, part) {
3211 mmc_queue_resume(&part_md->queue);
3212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 }
3214 return 0;
3215}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216#endif
3217
Ulf Hansson0967edc2014-10-06 11:29:42 +02003218static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3219
Ulf Hansson96541ba2015-04-14 13:06:12 +02003220static struct mmc_driver mmc_driver = {
3221 .drv = {
3222 .name = "mmcblk",
3223 .pm = &mmc_blk_pm_ops,
3224 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 .probe = mmc_blk_probe,
3226 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02003227 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228};
3229
3230static int __init mmc_blk_init(void)
3231{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003232 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003234 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3235 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3236
Ben Hutchingsa26eba62014-11-06 03:35:09 +00003237 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003238
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003239 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3240 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003243 res = mmc_register_driver(&mmc_driver);
3244 if (res)
3245 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003247 return 0;
3248 out2:
3249 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 out:
3251 return res;
3252}
3253
3254static void __exit mmc_blk_exit(void)
3255{
3256 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003257 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
3260module_init(mmc_blk_init);
3261module_exit(mmc_blk_exit);
3262
3263MODULE_LICENSE("GPL");
3264MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3265