blob: e7d493ad216178e28c441b52a9213173959e64a8 [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
1989 /*
1990 * In case the packing control is not supported by the host, it should
1991 * not have an effect on the write packing. Therefore we have to enable
1992 * the write packing
1993 */
1994 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1995 mq->wr_packing_enabled = true;
1996 return;
1997 }
1998
1999 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2000 if (mq->num_of_potential_packed_wr_reqs >
2001 mq->num_wr_reqs_to_start_packing)
2002 mq->wr_packing_enabled = true;
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002003 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002004 return;
2005 }
2006
2007 data_dir = rq_data_dir(req);
2008
2009 if (data_dir == READ) {
2010 mq->num_of_potential_packed_wr_reqs = 0;
2011 mq->wr_packing_enabled = false;
2012 return;
2013 } else if (data_dir == WRITE) {
2014 mq->num_of_potential_packed_wr_reqs++;
2015 }
2016
2017 if (mq->num_of_potential_packed_wr_reqs >
2018 mq->num_wr_reqs_to_start_packing)
2019 mq->wr_packing_enabled = true;
2020}
2021
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002022struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2023{
2024 if (!card)
2025 return NULL;
2026
2027 return &card->wr_pack_stats;
2028}
2029EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2030
2031void mmc_blk_init_packed_statistics(struct mmc_card *card)
2032{
2033 int max_num_of_packed_reqs = 0;
2034
2035 if (!card || !card->wr_pack_stats.packing_events)
2036 return;
2037
2038 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2039
2040 spin_lock(&card->wr_pack_stats.lock);
2041 memset(card->wr_pack_stats.packing_events, 0,
2042 (max_num_of_packed_reqs + 1) *
2043 sizeof(*card->wr_pack_stats.packing_events));
2044 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2045 sizeof(card->wr_pack_stats.pack_stop_reason));
2046 card->wr_pack_stats.enabled = true;
2047 spin_unlock(&card->wr_pack_stats.lock);
2048}
2049EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2050
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002051static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2052{
2053 struct request_queue *q = mq->queue;
2054 struct mmc_card *card = mq->card;
2055 struct request *cur = req, *next = NULL;
2056 struct mmc_blk_data *md = mq->data;
2057 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2058 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2059 unsigned int req_sectors = 0, phys_segments = 0;
2060 unsigned int max_blk_count, max_phys_segs;
2061 bool put_back = true;
2062 u8 max_packed_rw = 0;
2063 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002064 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002065
Shawn Lin96e52da2016-08-26 08:49:55 +08002066 /*
2067 * We don't need to check packed for any further
2068 * operation of packed stuff as we set MMC_PACKED_NONE
2069 * and return zero for reqs if geting null packed. Also
2070 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2071 * it again when removing blk req.
2072 */
2073 if (!mqrq->packed) {
2074 md->flags &= (~MMC_BLK_PACKED_CMD);
2075 goto no_packed;
2076 }
2077
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002078 if (!(md->flags & MMC_BLK_PACKED_CMD))
2079 goto no_packed;
2080
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002081 if (!mq->wr_packing_enabled)
2082 goto no_packed;
2083
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002084 if ((rq_data_dir(cur) == WRITE) &&
2085 mmc_host_packed_wr(card->host))
2086 max_packed_rw = card->ext_csd.max_packed_writes;
2087
2088 if (max_packed_rw == 0)
2089 goto no_packed;
2090
2091 if (mmc_req_rel_wr(cur) &&
2092 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2093 goto no_packed;
2094
2095 if (mmc_large_sector(card) &&
2096 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2097 goto no_packed;
2098
2099 mmc_blk_clear_packed(mqrq);
2100
2101 max_blk_count = min(card->host->max_blk_count,
2102 card->host->max_req_size >> 9);
2103 if (unlikely(max_blk_count > 0xffff))
2104 max_blk_count = 0xffff;
2105
2106 max_phys_segs = queue_max_segments(q);
2107 req_sectors += blk_rq_sectors(cur);
2108 phys_segments += cur->nr_phys_segments;
2109
2110 if (rq_data_dir(cur) == WRITE) {
2111 req_sectors += mmc_large_sector(card) ? 8 : 1;
2112 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2113 }
2114
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002115 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002116 do {
2117 if (reqs >= max_packed_rw - 1) {
2118 put_back = false;
2119 break;
2120 }
2121
2122 spin_lock_irq(q->queue_lock);
2123 next = blk_fetch_request(q);
2124 spin_unlock_irq(q->queue_lock);
2125 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002126 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002127 put_back = false;
2128 break;
2129 }
2130
2131 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002132 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2133 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002134 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002135 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002136
Mike Christie3a5e02c2016-06-05 14:32:23 -05002137 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002138 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002139 req_op(next) == REQ_OP_FLUSH) {
2140 if (req_op(next) != REQ_OP_SECURE_ERASE)
2141 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002142 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002143 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002144
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002145 if (rq_data_dir(cur) != rq_data_dir(next)) {
2146 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002147 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002148 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002149
2150 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002151 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2152 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002153 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002154 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002155
2156 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002157 if (req_sectors > max_blk_count) {
2158 if (stats->enabled)
2159 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002160 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002161 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002162
2163 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002164 if (phys_segments > max_phys_segs) {
2165 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002166 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002167 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002168
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002169 if (rq_data_dir(next) == WRITE)
2170 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002171 list_add_tail(&next->queuelist, &mqrq->packed->list);
2172 cur = next;
2173 reqs++;
2174 } while (1);
2175
2176 if (put_back) {
2177 spin_lock_irq(q->queue_lock);
2178 blk_requeue_request(q, next);
2179 spin_unlock_irq(q->queue_lock);
2180 }
2181
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002182 if (stats->enabled) {
2183 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2184 stats->packing_events[reqs + 1]++;
2185 if (reqs + 1 == max_packed_rw)
2186 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2187 }
2188
2189 spin_unlock(&stats->lock);
2190
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002191 if (reqs > 0) {
2192 list_add(&req->queuelist, &mqrq->packed->list);
2193 mqrq->packed->nr_entries = ++reqs;
2194 mqrq->packed->retries = reqs;
2195 return reqs;
2196 }
2197
2198no_packed:
2199 mqrq->cmd_type = MMC_PACKED_NONE;
2200 return 0;
2201}
2202
2203static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2204 struct mmc_card *card,
2205 struct mmc_queue *mq)
2206{
2207 struct mmc_blk_request *brq = &mqrq->brq;
2208 struct request *req = mqrq->req;
2209 struct request *prq;
2210 struct mmc_blk_data *md = mq->data;
2211 struct mmc_packed *packed = mqrq->packed;
2212 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002213 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002214 u8 hdr_blocks;
2215 u8 i = 1;
2216
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002217 mqrq->cmd_type = MMC_PACKED_WRITE;
2218 packed->blocks = 0;
2219 packed->idx_failure = MMC_PACKED_NR_IDX;
2220
2221 packed_cmd_hdr = packed->cmd_hdr;
2222 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002223 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2224 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002225 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2226
2227 /*
2228 * Argument for each entry of packed group
2229 */
2230 list_for_each_entry(prq, &packed->list, queuelist) {
2231 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2232 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2233 (prq->cmd_flags & REQ_META) &&
2234 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002235 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002236 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002237 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002238 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2239 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002240 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002241 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002242 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002243 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002244 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002245 packed->blocks += blk_rq_sectors(prq);
2246 i++;
2247 }
2248
2249 memset(brq, 0, sizeof(struct mmc_blk_request));
2250 brq->mrq.cmd = &brq->cmd;
2251 brq->mrq.data = &brq->data;
2252 brq->mrq.sbc = &brq->sbc;
2253 brq->mrq.stop = &brq->stop;
2254
2255 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2256 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2257 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2258
2259 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2260 brq->cmd.arg = blk_rq_pos(req);
2261 if (!mmc_card_blockaddr(card))
2262 brq->cmd.arg <<= 9;
2263 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2264
2265 brq->data.blksz = 512;
2266 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002267 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302268 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002269
2270 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2271 brq->stop.arg = 0;
2272 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2273
2274 mmc_set_data_timeout(&brq->data, card);
2275
2276 brq->data.sg = mqrq->sg;
2277 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2278
2279 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002280
2281 /*
2282 * This is intended for packed commands tests usage - in case these
2283 * functions are not in use the respective pointers are NULL
2284 */
2285 if (mq->err_check_fn)
2286 mqrq->mmc_active.err_check = mq->err_check_fn;
2287 else
2288 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2289
2290 if (mq->packed_test_fn)
2291 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002292
2293 mmc_queue_bounce_pre(mqrq);
2294}
2295
Adrian Hunter67716322011-08-29 16:42:15 +03002296static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2297 struct mmc_blk_request *brq, struct request *req,
2298 int ret)
2299{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002300 struct mmc_queue_req *mq_rq;
2301 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2302
Adrian Hunter67716322011-08-29 16:42:15 +03002303 /*
2304 * If this is an SD card and we're writing, we can first
2305 * mark the known good sectors as ok.
2306 *
2307 * If the card is not SD, we can still ok written sectors
2308 * as reported by the controller (which might be less than
2309 * the real number of written sectors, but never more).
2310 */
2311 if (mmc_card_sd(card)) {
2312 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302313 if (!brq->data.fault_injected) {
2314 blocks = mmc_sd_num_wr_blocks(card);
2315 if (blocks != (u32)-1)
2316 ret = blk_end_request(req, 0, blocks << 9);
2317 } else
2318 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002319 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002320 if (!mmc_packed_cmd(mq_rq->cmd_type))
2321 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002322 }
2323 return ret;
2324}
2325
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002326static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2327{
2328 struct request *prq;
2329 struct mmc_packed *packed = mq_rq->packed;
2330 int idx = packed->idx_failure, i = 0;
2331 int ret = 0;
2332
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002333 while (!list_empty(&packed->list)) {
2334 prq = list_entry_rq(packed->list.next);
2335 if (idx == i) {
2336 /* retry from error index */
2337 packed->nr_entries -= idx;
2338 mq_rq->req = prq;
2339 ret = 1;
2340
2341 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2342 list_del_init(&prq->queuelist);
2343 mmc_blk_clear_packed(mq_rq);
2344 }
2345 return ret;
2346 }
2347 list_del_init(&prq->queuelist);
2348 blk_end_request(prq, 0, blk_rq_bytes(prq));
2349 i++;
2350 }
2351
2352 mmc_blk_clear_packed(mq_rq);
2353 return ret;
2354}
2355
2356static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2357{
2358 struct request *prq;
2359 struct mmc_packed *packed = mq_rq->packed;
2360
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002361 while (!list_empty(&packed->list)) {
2362 prq = list_entry_rq(packed->list.next);
2363 list_del_init(&prq->queuelist);
2364 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2365 }
2366
2367 mmc_blk_clear_packed(mq_rq);
2368}
2369
2370static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2371 struct mmc_queue_req *mq_rq)
2372{
2373 struct request *prq;
2374 struct request_queue *q = mq->queue;
2375 struct mmc_packed *packed = mq_rq->packed;
2376
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002377 while (!list_empty(&packed->list)) {
2378 prq = list_entry_rq(packed->list.prev);
2379 if (prq->queuelist.prev != &packed->list) {
2380 list_del_init(&prq->queuelist);
2381 spin_lock_irq(q->queue_lock);
2382 blk_requeue_request(mq->queue, prq);
2383 spin_unlock_irq(q->queue_lock);
2384 } else {
2385 list_del_init(&prq->queuelist);
2386 }
2387 }
2388
2389 mmc_blk_clear_packed(mq_rq);
2390}
2391
Per Forlinee8a43a2011-07-01 18:55:33 +02002392static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02002393{
2394 struct mmc_blk_data *md = mq->data;
2395 struct mmc_card *card = md->queue.card;
2396 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002397 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002398 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002399 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302400 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02002401 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002402 const u8 packed_nr = 2;
2403 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002404#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2405 unsigned long waitfor = jiffies;
2406#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02002407
2408 if (!rqc && !mq->mqrq_prev->req)
2409 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002410
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002411 if (rqc)
2412 reqs = mmc_blk_prep_packed_list(mq, rqc);
2413
Per Forlin54d49d72011-07-01 18:55:29 +02002414 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002415 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302416 /*
2417 * When 4KB native sector is enabled, only 8 blocks
2418 * multiple read or write is allowed
2419 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00002420 if (mmc_large_sector(card) &&
2421 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302422 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
2423 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002424 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302425 goto cmd_abort;
2426 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002427
2428 if (reqs >= packed_nr)
2429 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2430 card, mq);
2431 else
2432 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002433 areq = &mq->mqrq_cur->mmc_active;
2434 } else
2435 areq = NULL;
2436 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002437 if (!areq) {
2438 if (status == MMC_BLK_NEW_REQUEST)
2439 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02002440 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002441 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002442
Per Forlinee8a43a2011-07-01 18:55:33 +02002443 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2444 brq = &mq_rq->brq;
2445 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002446 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002447 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002448
Per Forlind78d4a82011-07-01 18:55:30 +02002449 switch (status) {
2450 case MMC_BLK_SUCCESS:
2451 case MMC_BLK_PARTIAL:
2452 /*
2453 * A block was successfully transferred.
2454 */
Adrian Hunter67716322011-08-29 16:42:15 +03002455 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002456
Mark Salyzyn6904e432016-01-28 11:12:25 -08002457 mmc_blk_simulate_delay(mq, rqc, waitfor);
2458
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002459 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2460 ret = mmc_blk_end_packed_req(mq_rq);
2461 break;
2462 } else {
2463 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002464 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002465 }
2466
Adrian Hunter67716322011-08-29 16:42:15 +03002467 /*
2468 * If the blk_end_request function returns non-zero even
2469 * though all data has been transferred and no errors
2470 * were returned by the host controller, it's a bug.
2471 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002472 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302473 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002474 __func__, blk_rq_bytes(req),
2475 brq->data.bytes_xfered);
2476 rqc = NULL;
2477 goto cmd_abort;
2478 }
Per Forlind78d4a82011-07-01 18:55:30 +02002479 break;
2480 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002481 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002482 if (mmc_blk_reset(md, card->host, type))
2483 goto cmd_abort;
2484 if (!ret)
2485 goto start_new_req;
2486 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002487 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002488 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a82011-07-01 18:55:30 +02002489 if (retry++ < 5)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002490 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002491 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002492 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002493 if (!mmc_blk_reset(md, card->host, type))
2494 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002495 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002496 case MMC_BLK_DATA_ERR: {
2497 int err;
2498
2499 err = mmc_blk_reset(md, card->host, type);
2500 if (!err)
2501 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05302502 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002503 }
2504 case MMC_BLK_ECC_ERR:
2505 if (brq->data.blocks > 1) {
2506 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002507 pr_warn("%s: retrying using single block read\n",
2508 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002509 disable_multi = 1;
2510 break;
2511 }
Per Forlind78d4a82011-07-01 18:55:30 +02002512 /*
2513 * After an error, we redo I/O one sector at a
2514 * time, so we only reach here after trying to
2515 * read a single sector.
2516 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302517 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002518 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002519 if (!ret)
2520 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002521 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302522 case MMC_BLK_NOMEDIUM:
2523 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002524 default:
2525 pr_err("%s: Unhandled return value (%d)",
2526 req->rq_disk->disk_name, status);
2527 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002528 }
2529
Per Forlinee8a43a2011-07-01 18:55:33 +02002530 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002531 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2532 if (!mq_rq->packed->retries)
2533 goto cmd_abort;
2534 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2535 mmc_start_req(card->host,
2536 &mq_rq->mmc_active, NULL);
2537 } else {
2538
2539 /*
2540 * In case of a incomplete request
2541 * prepare it again and resend.
2542 */
2543 mmc_blk_rw_rq_prep(mq_rq, card,
2544 disable_multi, mq);
2545 mmc_start_req(card->host,
2546 &mq_rq->mmc_active, NULL);
2547 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002548 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 } while (ret);
2551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 return 1;
2553
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002554 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002555 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2556 mmc_blk_abort_packed_req(mq_rq);
2557 } else {
2558 if (mmc_card_removed(card))
2559 req->cmd_flags |= REQ_QUIET;
2560 while (ret)
2561 ret = blk_end_request(req, -EIO,
2562 blk_rq_cur_bytes(req));
2563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Per Forlinee8a43a2011-07-01 18:55:33 +02002565 start_new_req:
2566 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002567 if (mmc_card_removed(card)) {
2568 rqc->cmd_flags |= REQ_QUIET;
2569 blk_end_request_all(rqc, -EIO);
2570 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002571 /*
2572 * If current request is packed, it needs to put back.
2573 */
2574 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2575 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2576
Seungwon Jeon7a819022013-01-22 19:48:07 +09002577 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2578 mmc_start_req(card->host,
2579 &mq->mqrq_cur->mmc_active, NULL);
2580 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002581 }
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return 0;
2584}
2585
Linus Walleij29eb7bd2016-09-20 11:34:38 +02002586int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002587{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002588 int ret;
2589 struct mmc_blk_data *md = mq->data;
2590 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002591 struct mmc_host *host = card->host;
2592 unsigned long flags;
Adrian Hunter869c5542016-08-25 14:11:43 -06002593 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002594
Per Forlinee8a43a2011-07-01 18:55:33 +02002595 if (req && !mq->mqrq_prev->req)
2596 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002597 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002598
Andrei Warkentin371a6892011-04-11 18:10:25 -05002599 ret = mmc_blk_part_switch(card, md);
2600 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002601 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302602 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002603 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002604 ret = 0;
2605 goto out;
2606 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002607
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002608 mmc_blk_write_packing_control(mq, req);
2609
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002610 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05002611 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002612 /* complete ongoing async transfer before issuing discard */
2613 if (card->host->areq)
2614 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02002615 ret = mmc_blk_issue_discard_rq(mq, req);
2616 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
2617 /* complete ongoing async transfer before issuing secure erase*/
2618 if (card->host->areq)
2619 mmc_blk_issue_rw_rq(mq, NULL);
2620 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05002621 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002622 /* complete ongoing async transfer before issuing flush */
2623 if (card->host->areq)
2624 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002625 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002626 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002627 if (!req && host->areq) {
2628 spin_lock_irqsave(&host->context_info.lock, flags);
2629 host->context_info.is_waiting_last_req = true;
2630 spin_unlock_irqrestore(&host->context_info.lock, flags);
2631 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002632 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002633 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002634
Andrei Warkentin371a6892011-04-11 18:10:25 -05002635out:
Adrian Hunter869c5542016-08-25 14:11:43 -06002636 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002637 /*
2638 * Release host when there are no more requests
2639 * and after special request(discard, flush) is done.
2640 * In case sepecial request, there is no reentry to
2641 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2642 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002643 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002644 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002645}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Russell Kinga6f6c962006-01-03 22:38:44 +00002647static inline int mmc_blk_readonly(struct mmc_card *card)
2648{
2649 return mmc_card_readonly(card) ||
2650 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2651}
2652
Andrei Warkentin371a6892011-04-11 18:10:25 -05002653static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2654 struct device *parent,
2655 sector_t size,
2656 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002657 const char *subname,
2658 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
2660 struct mmc_blk_data *md;
2661 int devidx, ret;
2662
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002663again:
2664 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
2665 return ERR_PTR(-ENOMEM);
2666
2667 spin_lock(&mmc_blk_lock);
2668 ret = ida_get_new(&mmc_blk_ida, &devidx);
2669 spin_unlock(&mmc_blk_lock);
2670
2671 if (ret == -EAGAIN)
2672 goto again;
2673 else if (ret)
2674 return ERR_PTR(ret);
2675
2676 if (devidx >= max_devices) {
2677 ret = -ENOSPC;
2678 goto out;
2679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002681 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002682 if (!md) {
2683 ret = -ENOMEM;
2684 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002686
Johan Rudholmadd710e2011-12-02 08:51:06 +01002687 md->area_type = area_type;
2688
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002689 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002690 * Set the read-only status based on the supported commands
2691 * and the write protect switch.
2692 */
2693 md->read_only = mmc_blk_readonly(card);
2694
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002695 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002696 if (md->disk == NULL) {
2697 ret = -ENOMEM;
2698 goto err_kfree;
2699 }
2700
2701 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002702 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002703 md->usage = 1;
2704
Adrian Hunterd09408a2011-06-23 13:40:28 +03002705 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002706 if (ret)
2707 goto err_putdisk;
2708
Russell Kinga6f6c962006-01-03 22:38:44 +00002709 md->queue.data = md;
2710
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002711 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002712 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002713 md->disk->fops = &mmc_bdops;
2714 md->disk->private_data = md;
2715 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002716 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002717 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002718 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002719 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002720 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002721
2722 /*
2723 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2724 *
2725 * - be set for removable media with permanent block devices
2726 * - be unset for removable block devices with permanent media
2727 *
2728 * Since MMC block devices clearly fall under the second
2729 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2730 * should use the block device creation/destruction hotplug
2731 * messages to tell when the card is present.
2732 */
2733
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002734 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02002735 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002736
Saugata Dasa5075eb2012-05-17 16:32:21 +05302737 if (mmc_card_mmc(card))
2738 blk_queue_logical_block_size(md->queue.queue,
2739 card->ext_csd.data_sector_size);
2740 else
2741 blk_queue_logical_block_size(md->queue.queue, 512);
2742
Andrei Warkentin371a6892011-04-11 18:10:25 -05002743 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002744
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002745 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02002746 if ((mmc_card_mmc(card) &&
2747 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002748 (mmc_card_sd(card) &&
2749 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2750 md->flags |= MMC_BLK_CMD23;
2751 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002752
2753 if (mmc_card_mmc(card) &&
2754 md->flags & MMC_BLK_CMD23 &&
2755 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2756 card->ext_csd.rel_sectors)) {
2757 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06002758 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002759 }
2760
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002761 if (mmc_card_mmc(card) &&
2762 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2763 (md->flags & MMC_BLK_CMD23) &&
2764 card->ext_csd.packed_event_en) {
2765 if (!mmc_packed_init(&md->queue, card))
2766 md->flags |= MMC_BLK_PACKED_CMD;
2767 }
2768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002770
2771 err_putdisk:
2772 put_disk(md->disk);
2773 err_kfree:
2774 kfree(md);
2775 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002776 spin_lock(&mmc_blk_lock);
2777 ida_remove(&mmc_blk_ida, devidx);
2778 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00002779 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}
2781
Andrei Warkentin371a6892011-04-11 18:10:25 -05002782static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2783{
2784 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002785
2786 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2787 /*
2788 * The EXT_CSD sector count is in number or 512 byte
2789 * sectors.
2790 */
2791 size = card->ext_csd.sectors;
2792 } else {
2793 /*
2794 * The CSD capacity field is in units of read_blkbits.
2795 * set_capacity takes units of 512 bytes.
2796 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002797 size = (typeof(sector_t))card->csd.capacity
2798 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002799 }
2800
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002801 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002802 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002803}
2804
2805static int mmc_blk_alloc_part(struct mmc_card *card,
2806 struct mmc_blk_data *md,
2807 unsigned int part_type,
2808 sector_t size,
2809 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002810 const char *subname,
2811 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002812{
2813 char cap_str[10];
2814 struct mmc_blk_data *part_md;
2815
2816 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002817 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002818 if (IS_ERR(part_md))
2819 return PTR_ERR(part_md);
2820 part_md->part_type = part_type;
2821 list_add(&part_md->part, &md->part);
2822
James Bottomleyb9f28d82015-03-05 18:47:01 -08002823 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002824 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302825 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002826 part_md->disk->disk_name, mmc_card_id(card),
2827 mmc_card_name(card), part_md->part_type, cap_str);
2828 return 0;
2829}
2830
Namjae Jeone0c368d2011-10-06 23:41:38 +09002831/* MMC Physical partitions consist of two boot partitions and
2832 * up to four general purpose partitions.
2833 * For each partition enabled in EXT_CSD a block device will be allocatedi
2834 * to provide access to the partition.
2835 */
2836
Andrei Warkentin371a6892011-04-11 18:10:25 -05002837static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2838{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002839 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002840
2841 if (!mmc_card_mmc(card))
2842 return 0;
2843
Namjae Jeone0c368d2011-10-06 23:41:38 +09002844 for (idx = 0; idx < card->nr_parts; idx++) {
2845 if (card->part[idx].size) {
2846 ret = mmc_blk_alloc_part(card, md,
2847 card->part[idx].part_cfg,
2848 card->part[idx].size >> 9,
2849 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002850 card->part[idx].name,
2851 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002852 if (ret)
2853 return ret;
2854 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002855 }
2856
2857 return ret;
2858}
2859
Andrei Warkentin371a6892011-04-11 18:10:25 -05002860static void mmc_blk_remove_req(struct mmc_blk_data *md)
2861{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002862 struct mmc_card *card;
2863
Andrei Warkentin371a6892011-04-11 18:10:25 -05002864 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002865 /*
2866 * Flush remaining requests and free queues. It
2867 * is freeing the queue that stops new requests
2868 * from being accepted.
2869 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002870 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002871 mmc_cleanup_queue(&md->queue);
2872 if (md->flags & MMC_BLK_PACKED_CMD)
2873 mmc_packed_clean(&md->queue);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002874 device_remove_file(disk_to_dev(md->disk),
2875 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002876 if (md->disk->flags & GENHD_FL_UP) {
2877 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002878 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2879 card->ext_csd.boot_ro_lockable)
2880 device_remove_file(disk_to_dev(md->disk),
2881 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08002882#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2883 device_remove_file(disk_to_dev(md->disk),
2884 &dev_attr_max_write_speed);
2885 device_remove_file(disk_to_dev(md->disk),
2886 &dev_attr_max_read_speed);
2887 device_remove_file(disk_to_dev(md->disk),
2888 &dev_attr_cache_size);
2889#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002890
Andrei Warkentin371a6892011-04-11 18:10:25 -05002891 del_gendisk(md->disk);
2892 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002893 mmc_blk_put(md);
2894 }
2895}
2896
2897static void mmc_blk_remove_parts(struct mmc_card *card,
2898 struct mmc_blk_data *md)
2899{
2900 struct list_head *pos, *q;
2901 struct mmc_blk_data *part_md;
2902
2903 list_for_each_safe(pos, q, &md->part) {
2904 part_md = list_entry(pos, struct mmc_blk_data, part);
2905 list_del(pos);
2906 mmc_blk_remove_req(part_md);
2907 }
2908}
2909
2910static int mmc_add_disk(struct mmc_blk_data *md)
2911{
2912 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002913 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002914
Dan Williams307d8e62016-06-20 10:40:44 -07002915 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002916 md->force_ro.show = force_ro_show;
2917 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302918 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002919 md->force_ro.attr.name = "force_ro";
2920 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2921 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2922 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002923 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002924#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2925 atomic_set(&md->queue.max_write_speed, max_write_speed);
2926 ret = device_create_file(disk_to_dev(md->disk),
2927 &dev_attr_max_write_speed);
2928 if (ret)
2929 goto max_write_speed_fail;
2930 atomic_set(&md->queue.max_read_speed, max_read_speed);
2931 ret = device_create_file(disk_to_dev(md->disk),
2932 &dev_attr_max_read_speed);
2933 if (ret)
2934 goto max_read_speed_fail;
2935 atomic_set(&md->queue.cache_size, cache_size);
2936 atomic_long_set(&md->queue.cache_used, 0);
2937 md->queue.cache_jiffies = jiffies;
2938 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
2939 if (ret)
2940 goto cache_size_fail;
2941#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01002942
2943 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2944 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002945 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002946
2947 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2948 mode = S_IRUGO;
2949 else
2950 mode = S_IRUGO | S_IWUSR;
2951
2952 md->power_ro_lock.show = power_ro_lock_show;
2953 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002954 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002955 md->power_ro_lock.attr.mode = mode;
2956 md->power_ro_lock.attr.name =
2957 "ro_lock_until_next_power_on";
2958 ret = device_create_file(disk_to_dev(md->disk),
2959 &md->power_ro_lock);
2960 if (ret)
2961 goto power_ro_lock_fail;
2962 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002963
2964 md->num_wr_reqs_to_start_packing.show =
2965 num_wr_reqs_to_start_packing_show;
2966 md->num_wr_reqs_to_start_packing.store =
2967 num_wr_reqs_to_start_packing_store;
2968 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2969 md->num_wr_reqs_to_start_packing.attr.name =
2970 "num_wr_reqs_to_start_packing";
2971 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2972 ret = device_create_file(disk_to_dev(md->disk),
2973 &md->num_wr_reqs_to_start_packing);
2974 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02002975 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002976
Johan Rudholmadd710e2011-12-02 08:51:06 +01002977 return ret;
2978
Maya Erez17022402014-12-04 00:15:42 +02002979num_wr_reqs_to_start_packing_fail:
2980 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002981power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08002982#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2983 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
2984cache_size_fail:
2985 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
2986max_read_speed_fail:
2987 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
2988max_write_speed_fail:
2989#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01002990 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2991force_ro_fail:
2992 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002993
2994 return ret;
2995}
2996
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002997static const struct mmc_fixup blk_fixups[] =
2998{
Chris Ballc59d4472011-11-11 22:01:43 -05002999 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
3000 MMC_QUIRK_INAND_CMD38),
3001 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
3002 MMC_QUIRK_INAND_CMD38),
3003 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
3004 MMC_QUIRK_INAND_CMD38),
3005 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
3006 MMC_QUIRK_INAND_CMD38),
3007 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
3008 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003009
3010 /*
3011 * Some MMC cards experience performance degradation with CMD23
3012 * instead of CMD12-bounded multiblock transfers. For now we'll
3013 * black list what's bad...
3014 * - Certain Toshiba cards.
3015 *
3016 * N.B. This doesn't affect SD cards.
3017 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08003018 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3019 MMC_QUIRK_BLK_NO_CMD23),
3020 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3021 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003022 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003023 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003024 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003025 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003026 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003027 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003028
3029 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03003030 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003031 */
Chris Ballc59d4472011-11-11 22:01:43 -05003032 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003033 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03003034 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
3035 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003036
Ian Chen3550ccd2012-08-29 15:05:36 +09003037 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08003038 * Some Samsung MMC cards need longer data read timeout than
3039 * indicated in CSD.
3040 */
3041 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
3042 MMC_QUIRK_LONG_READ_TIME),
3043
3044 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09003045 * On these Samsung MoviNAND parts, performing secure erase or
3046 * secure trim can result in unrecoverable corruption due to a
3047 * firmware bug.
3048 */
3049 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3050 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3051 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3052 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3053 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3054 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3055 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3056 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3057 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3058 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3059 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3060 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3061 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3062 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3063 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3064 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3065
Shawn Linb5b4ff02015-08-12 13:08:32 +08003066 /*
3067 * On Some Kingston eMMCs, performing trim can result in
3068 * unrecoverable data conrruption occasionally due to a firmware bug.
3069 */
3070 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3071 MMC_QUIRK_TRIM_BROKEN),
3072 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3073 MMC_QUIRK_TRIM_BROKEN),
3074
Pratibhasagar V8d664e32014-12-03 18:26:42 +02003075 /* Some INAND MCP devices advertise incorrect timeout values */
3076 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
3077 MMC_QUIRK_INAND_DATA_TIMEOUT),
3078
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003079 END_FIXUP
3080};
3081
Ulf Hansson96541ba2015-04-14 13:06:12 +02003082static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003084 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003085 char cap_str[10];
3086
Pierre Ossman912490d2005-05-21 10:27:02 +01003087 /*
3088 * Check that the card supports the command class(es) we need.
3089 */
3090 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return -ENODEV;
3092
Lukas Czerner5204d002014-06-18 13:18:07 +02003093 mmc_fixup_device(card, blk_fixups);
3094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 md = mmc_blk_alloc(card);
3096 if (IS_ERR(md))
3097 return PTR_ERR(md);
3098
James Bottomleyb9f28d82015-03-05 18:47:01 -08003099 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003100 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303101 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003103 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Andrei Warkentin371a6892011-04-11 18:10:25 -05003105 if (mmc_blk_alloc_parts(card, md))
3106 goto out;
3107
Ulf Hansson96541ba2015-04-14 13:06:12 +02003108 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003109
Andrei Warkentin371a6892011-04-11 18:10:25 -05003110 if (mmc_add_disk(md))
3111 goto out;
3112
3113 list_for_each_entry(part_md, &md->part, part) {
3114 if (mmc_add_disk(part_md))
3115 goto out;
3116 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003117
3118 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
3119 pm_runtime_use_autosuspend(&card->dev);
3120
3121 /*
3122 * Don't enable runtime PM for SD-combo cards here. Leave that
3123 * decision to be taken during the SDIO init sequence instead.
3124 */
3125 if (card->type != MMC_TYPE_SD_COMBO) {
3126 pm_runtime_set_active(&card->dev);
3127 pm_runtime_enable(&card->dev);
3128 }
3129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 return 0;
3131
3132 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05003133 mmc_blk_remove_parts(card, md);
3134 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01003135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
Ulf Hansson96541ba2015-04-14 13:06:12 +02003138static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003140 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Andrei Warkentin371a6892011-04-11 18:10:25 -05003142 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003143 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003144 mmc_claim_host(card->host);
3145 mmc_blk_part_switch(card, md);
3146 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003147 if (card->type != MMC_TYPE_SD_COMBO)
3148 pm_runtime_disable(&card->dev);
3149 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003150 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02003151 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152}
3153
Ulf Hansson96541ba2015-04-14 13:06:12 +02003154static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003156 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02003157 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303158 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
3160 if (md) {
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303161 rc = mmc_queue_suspend(&md->queue);
3162 if (rc)
3163 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003164 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303165 rc = mmc_queue_suspend(&part_md->queue);
3166 if (rc)
3167 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303170 goto out;
3171
3172 out_resume:
3173 mmc_queue_resume(&md->queue);
3174 list_for_each_entry(part_md, &md->part, part) {
3175 mmc_queue_resume(&part_md->queue);
3176 }
3177 out:
3178 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179}
3180
Ulf Hansson96541ba2015-04-14 13:06:12 +02003181static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02003182{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003183 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003184}
3185
Ulf Hansson0967edc2014-10-06 11:29:42 +02003186#ifdef CONFIG_PM_SLEEP
3187static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02003188{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003189 struct mmc_card *card = mmc_dev_to_card(dev);
3190
3191 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003192}
3193
Ulf Hansson0967edc2014-10-06 11:29:42 +02003194static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003196 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02003197 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
3199 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003200 /*
3201 * Resume involves the card going into idle state,
3202 * so current partition is always the main one.
3203 */
3204 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003206 list_for_each_entry(part_md, &md->part, part) {
3207 mmc_queue_resume(&part_md->queue);
3208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 }
3210 return 0;
3211}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212#endif
3213
Ulf Hansson0967edc2014-10-06 11:29:42 +02003214static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3215
Ulf Hansson96541ba2015-04-14 13:06:12 +02003216static struct mmc_driver mmc_driver = {
3217 .drv = {
3218 .name = "mmcblk",
3219 .pm = &mmc_blk_pm_ops,
3220 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 .probe = mmc_blk_probe,
3222 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02003223 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224};
3225
3226static int __init mmc_blk_init(void)
3227{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003228 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003230 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3231 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3232
Ben Hutchingsa26eba62014-11-06 03:35:09 +00003233 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003234
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003235 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3236 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003239 res = mmc_register_driver(&mmc_driver);
3240 if (res)
3241 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003243 return 0;
3244 out2:
3245 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 out:
3247 return res;
3248}
3249
3250static void __exit mmc_blk_exit(void)
3251{
3252 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003253 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254}
3255
3256module_init(mmc_blk_init);
3257module_exit(mmc_blk_exit);
3258
3259MODULE_LICENSE("GPL");
3260MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3261