blob: 8c76ace780f865f4a923523034b180916a8425b6 [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));
320
321 sscanf(buf, "%d", &value);
322 if (value >= 0)
323 md->queue.num_wr_reqs_to_start_packing = value;
324
325 mmc_blk_put(md);
326 return count;
327}
328
Mark Salyzyn6904e432016-01-28 11:12:25 -0800329#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
330
331static int max_read_speed, max_write_speed, cache_size = 4;
332
333module_param(max_read_speed, int, S_IRUSR | S_IRGRP);
334MODULE_PARM_DESC(max_read_speed, "maximum KB/s read speed 0=off");
335module_param(max_write_speed, int, S_IRUSR | S_IRGRP);
336MODULE_PARM_DESC(max_write_speed, "maximum KB/s write speed 0=off");
337module_param(cache_size, int, S_IRUSR | S_IRGRP);
338MODULE_PARM_DESC(cache_size, "MB high speed memory or SLC cache");
339
340/*
341 * helper macros and expectations:
342 * size - unsigned long number of bytes
343 * jiffies - unsigned long HZ timestamp difference
344 * speed - unsigned KB/s transfer rate
345 */
346#define size_and_speed_to_jiffies(size, speed) \
347 ((size) * HZ / (speed) / 1024UL)
348#define jiffies_and_speed_to_size(jiffies, speed) \
349 (((speed) * (jiffies) * 1024UL) / HZ)
350#define jiffies_and_size_to_speed(jiffies, size) \
351 ((size) * HZ / (jiffies) / 1024UL)
352
353/* Limits to report warning */
354/* jiffies_and_size_to_speed(10*HZ, queue_max_hw_sectors(q) * 512UL) ~ 25 */
355#define MIN_SPEED(q) 250 /* 10 times faster than a floppy disk */
356#define MAX_SPEED(q) jiffies_and_size_to_speed(1, queue_max_sectors(q) * 512UL)
357
358#define speed_valid(speed) ((speed) > 0)
359
360static const char off[] = "off\n";
361
362static int max_speed_show(int speed, char *buf)
363{
364 if (speed)
365 return scnprintf(buf, PAGE_SIZE, "%uKB/s\n", speed);
366 else
367 return scnprintf(buf, PAGE_SIZE, off);
368}
369
370static int max_speed_store(const char *buf, struct request_queue *q)
371{
372 unsigned int limit, set = 0;
373
374 if (!strncasecmp(off, buf, sizeof(off) - 2))
375 return set;
376 if (kstrtouint(buf, 0, &set) || (set > INT_MAX))
377 return -EINVAL;
378 if (set == 0)
379 return set;
380 limit = MAX_SPEED(q);
381 if (set > limit)
382 pr_warn("max speed %u ineffective above %u\n", set, limit);
383 limit = MIN_SPEED(q);
384 if (set < limit)
385 pr_warn("max speed %u painful below %u\n", set, limit);
386 return set;
387}
388
389static ssize_t max_write_speed_show(struct device *dev,
390 struct device_attribute *attr, char *buf)
391{
392 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
393 int ret = max_speed_show(atomic_read(&md->queue.max_write_speed), buf);
394
395 mmc_blk_put(md);
396 return ret;
397}
398
399static ssize_t max_write_speed_store(struct device *dev,
400 struct device_attribute *attr,
401 const char *buf, size_t count)
402{
403 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
404 int set = max_speed_store(buf, md->queue.queue);
405
406 if (set < 0) {
407 mmc_blk_put(md);
408 return set;
409 }
410
411 atomic_set(&md->queue.max_write_speed, set);
412 mmc_blk_put(md);
413 return count;
414}
415
416static const DEVICE_ATTR(max_write_speed, S_IRUGO | S_IWUSR,
417 max_write_speed_show, max_write_speed_store);
418
419static ssize_t max_read_speed_show(struct device *dev,
420 struct device_attribute *attr, char *buf)
421{
422 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
423 int ret = max_speed_show(atomic_read(&md->queue.max_read_speed), buf);
424
425 mmc_blk_put(md);
426 return ret;
427}
428
429static ssize_t max_read_speed_store(struct device *dev,
430 struct device_attribute *attr,
431 const char *buf, size_t count)
432{
433 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
434 int set = max_speed_store(buf, md->queue.queue);
435
436 if (set < 0) {
437 mmc_blk_put(md);
438 return set;
439 }
440
441 atomic_set(&md->queue.max_read_speed, set);
442 mmc_blk_put(md);
443 return count;
444}
445
446static const DEVICE_ATTR(max_read_speed, S_IRUGO | S_IWUSR,
447 max_read_speed_show, max_read_speed_store);
448
449static ssize_t cache_size_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
451{
452 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
453 struct mmc_queue *mq = &md->queue;
454 int cache_size = atomic_read(&mq->cache_size);
455 int ret;
456
457 if (!cache_size)
458 ret = scnprintf(buf, PAGE_SIZE, off);
459 else {
460 int speed = atomic_read(&mq->max_write_speed);
461
462 if (!speed_valid(speed))
463 ret = scnprintf(buf, PAGE_SIZE, "%uMB\n", cache_size);
464 else { /* We accept race between cache_jiffies and cache_used */
465 unsigned long size = jiffies_and_speed_to_size(
466 jiffies - mq->cache_jiffies, speed);
467 long used = atomic_long_read(&mq->cache_used);
468
469 if (size >= used)
470 size = 0;
471 else
472 size = (used - size) * 100 / cache_size
473 / 1024UL / 1024UL;
474
475 ret = scnprintf(buf, PAGE_SIZE, "%uMB %lu%% used\n",
476 cache_size, size);
477 }
478 }
479
480 mmc_blk_put(md);
481 return ret;
482}
483
484static ssize_t cache_size_store(struct device *dev,
485 struct device_attribute *attr,
486 const char *buf, size_t count)
487{
488 struct mmc_blk_data *md;
489 unsigned int set = 0;
490
491 if (strncasecmp(off, buf, sizeof(off) - 2)
492 && (kstrtouint(buf, 0, &set) || (set > INT_MAX)))
493 return -EINVAL;
494
495 md = mmc_blk_get(dev_to_disk(dev));
496 atomic_set(&md->queue.cache_size, set);
497 mmc_blk_put(md);
498 return count;
499}
500
501static const DEVICE_ATTR(cache_size, S_IRUGO | S_IWUSR,
502 cache_size_show, cache_size_store);
503
504/* correct for write-back */
505static long mmc_blk_cache_used(struct mmc_queue *mq, unsigned long waitfor)
506{
507 long used = 0;
508 int speed = atomic_read(&mq->max_write_speed);
509
510 if (speed_valid(speed)) {
511 unsigned long size = jiffies_and_speed_to_size(
512 waitfor - mq->cache_jiffies, speed);
513 used = atomic_long_read(&mq->cache_used);
514
515 if (size >= used)
516 used = 0;
517 else
518 used -= size;
519 }
520
521 atomic_long_set(&mq->cache_used, used);
522 mq->cache_jiffies = waitfor;
523
524 return used;
525}
526
527static void mmc_blk_simulate_delay(
528 struct mmc_queue *mq,
529 struct request *req,
530 unsigned long waitfor)
531{
532 int max_speed;
533
534 if (!req)
535 return;
536
537 max_speed = (rq_data_dir(req) == READ)
538 ? atomic_read(&mq->max_read_speed)
539 : atomic_read(&mq->max_write_speed);
540 if (speed_valid(max_speed)) {
541 unsigned long bytes = blk_rq_bytes(req);
542
543 if (rq_data_dir(req) != READ) {
544 int cache_size = atomic_read(&mq->cache_size);
545
546 if (cache_size) {
547 unsigned long size = cache_size * 1024L * 1024L;
548 long used = mmc_blk_cache_used(mq, waitfor);
549
550 used += bytes;
551 atomic_long_set(&mq->cache_used, used);
552 bytes = 0;
553 if (used > size)
554 bytes = used - size;
555 }
556 }
557 waitfor += size_and_speed_to_jiffies(bytes, max_speed);
558 if (time_is_after_jiffies(waitfor)) {
559 long msecs = jiffies_to_msecs(waitfor - jiffies);
560
561 if (likely(msecs > 0))
562 msleep(msecs);
563 }
564 }
565}
566
567#else
568
569#define mmc_blk_simulate_delay(mq, req, waitfor)
570
571#endif
572
Al Viroa5a15612008-03-02 10:33:30 -0500573static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Al Viroa5a15612008-03-02 10:33:30 -0500575 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 int ret = -ENXIO;
577
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200578 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (md) {
580 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500581 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700583
Al Viroa5a15612008-03-02 10:33:30 -0500584 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700585 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700586 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200589 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 return ret;
592}
593
Al Virodb2a1442013-05-05 21:52:57 -0400594static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Al Viroa5a15612008-03-02 10:33:30 -0500596 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200598 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200600 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800604mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800606 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
607 geo->heads = 4;
608 geo->sectors = 16;
609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
John Calixtocb87ea22011-04-26 18:56:29 -0400612struct mmc_blk_ioc_data {
613 struct mmc_ioc_cmd ic;
614 unsigned char *buf;
615 u64 buf_bytes;
616};
617
618static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
619 struct mmc_ioc_cmd __user *user)
620{
621 struct mmc_blk_ioc_data *idata;
622 int err;
623
yalin wang1ff89502015-11-12 19:27:11 +0800624 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400625 if (!idata) {
626 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400627 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400628 }
629
630 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
631 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400632 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400633 }
634
635 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
636 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
637 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400638 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400639 }
640
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300641 if (!idata->buf_bytes) {
642 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100643 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300644 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100645
yalin wang1ff89502015-11-12 19:27:11 +0800646 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400647 if (!idata->buf) {
648 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400649 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400650 }
651
652 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
653 idata->ic.data_ptr, idata->buf_bytes)) {
654 err = -EFAULT;
655 goto copy_err;
656 }
657
658 return idata;
659
660copy_err:
661 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400662idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400663 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400664out:
John Calixtocb87ea22011-04-26 18:56:29 -0400665 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400666}
667
Jon Huntera5f57742015-09-22 10:27:53 +0100668static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
669 struct mmc_blk_ioc_data *idata)
670{
671 struct mmc_ioc_cmd *ic = &idata->ic;
672
673 if (copy_to_user(&(ic_ptr->response), ic->response,
674 sizeof(ic->response)))
675 return -EFAULT;
676
677 if (!idata->ic.write_flag) {
678 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
679 idata->buf, idata->buf_bytes))
680 return -EFAULT;
681 }
682
683 return 0;
684}
685
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200686static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
687 u32 retries_max)
688{
689 int err;
690 u32 retry_count = 0;
691
692 if (!status || !retries_max)
693 return -EINVAL;
694
695 do {
696 err = get_card_status(card, status, 5);
697 if (err)
698 break;
699
700 if (!R1_STATUS(*status) &&
701 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
702 break; /* RPMB programming operation complete */
703
704 /*
705 * Rechedule to give the MMC device a chance to continue
706 * processing the previous command without being polled too
707 * frequently.
708 */
709 usleep_range(1000, 5000);
710 } while (++retry_count < retries_max);
711
712 if (retry_count == retries_max)
713 err = -EPERM;
714
715 return err;
716}
717
Maya Erez775a9362013-04-18 15:41:55 +0300718static int ioctl_do_sanitize(struct mmc_card *card)
719{
720 int err;
721
Ulf Hanssona2d10862013-12-16 14:37:26 +0100722 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300723 pr_warn("%s: %s - SANITIZE is not supported\n",
724 mmc_hostname(card->host), __func__);
725 err = -EOPNOTSUPP;
726 goto out;
727 }
728
729 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
730 mmc_hostname(card->host), __func__);
731
732 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
733 EXT_CSD_SANITIZE_START, 1,
734 MMC_SANITIZE_REQ_TIMEOUT);
735
736 if (err)
737 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
738 mmc_hostname(card->host), __func__, err);
739
740 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
741 __func__);
742out:
743 return err;
744}
745
Jon Huntera5f57742015-09-22 10:27:53 +0100746static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
747 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400748{
John Calixtocb87ea22011-04-26 18:56:29 -0400749 struct mmc_command cmd = {0};
750 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530751 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400752 struct scatterlist sg;
753 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200754 int is_rpmb = false;
755 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400756
Jon Huntera5f57742015-09-22 10:27:53 +0100757 if (!card || !md || !idata)
758 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400759
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200760 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
761 is_rpmb = true;
762
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100763 cmd.opcode = idata->ic.opcode;
764 cmd.arg = idata->ic.arg;
765 cmd.flags = idata->ic.flags;
766
767 if (idata->buf_bytes) {
768 data.sg = &sg;
769 data.sg_len = 1;
770 data.blksz = idata->ic.blksz;
771 data.blocks = idata->ic.blocks;
772
773 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
774
775 if (idata->ic.write_flag)
776 data.flags = MMC_DATA_WRITE;
777 else
778 data.flags = MMC_DATA_READ;
779
780 /* data.flags must already be set before doing this. */
781 mmc_set_data_timeout(&data, card);
782
783 /* Allow overriding the timeout_ns for empirical tuning. */
784 if (idata->ic.data_timeout_ns)
785 data.timeout_ns = idata->ic.data_timeout_ns;
786
787 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
788 /*
789 * Pretend this is a data transfer and rely on the
790 * host driver to compute timeout. When all host
791 * drivers support cmd.cmd_timeout for R1B, this
792 * can be changed to:
793 *
794 * mrq.data = NULL;
795 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
796 */
797 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
798 }
799
800 mrq.data = &data;
801 }
802
803 mrq.cmd = &cmd;
804
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200805 err = mmc_blk_part_switch(card, md);
806 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100807 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200808
John Calixtocb87ea22011-04-26 18:56:29 -0400809 if (idata->ic.is_acmd) {
810 err = mmc_app_cmd(card->host, card);
811 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100812 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400813 }
814
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200815 if (is_rpmb) {
816 err = mmc_set_blockcount(card, data.blocks,
817 idata->ic.write_flag & (1 << 31));
818 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100819 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200820 }
821
Yaniv Gardia82e4842013-06-05 14:13:08 +0300822 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
823 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300824 err = ioctl_do_sanitize(card);
825
826 if (err)
827 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
828 __func__, err);
829
Jon Huntera5f57742015-09-22 10:27:53 +0100830 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300831 }
832
John Calixtocb87ea22011-04-26 18:56:29 -0400833 mmc_wait_for_req(card->host, &mrq);
834
835 if (cmd.error) {
836 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
837 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100838 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400839 }
840 if (data.error) {
841 dev_err(mmc_dev(card->host), "%s: data error %d\n",
842 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100843 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400844 }
845
846 /*
847 * According to the SD specs, some commands require a delay after
848 * issuing the command.
849 */
850 if (idata->ic.postsleep_min_us)
851 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
852
Jon Huntera5f57742015-09-22 10:27:53 +0100853 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400854
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200855 if (is_rpmb) {
856 /*
857 * Ensure RPMB command has completed by polling CMD13
858 * "Send Status".
859 */
860 err = ioctl_rpmb_card_status_poll(card, &status, 5);
861 if (err)
862 dev_err(mmc_dev(card->host),
863 "%s: Card Status=0x%08X, error %d\n",
864 __func__, status, err);
865 }
866
Jon Huntera5f57742015-09-22 10:27:53 +0100867 return err;
868}
869
870static int mmc_blk_ioctl_cmd(struct block_device *bdev,
871 struct mmc_ioc_cmd __user *ic_ptr)
872{
873 struct mmc_blk_ioc_data *idata;
874 struct mmc_blk_data *md;
875 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700876 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100877
Shawn Lin83c742c2016-03-16 18:15:47 +0800878 /*
879 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
880 * whole block device, not on a partition. This prevents overspray
881 * between sibling partitions.
882 */
883 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
884 return -EPERM;
885
Jon Huntera5f57742015-09-22 10:27:53 +0100886 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
887 if (IS_ERR(idata))
888 return PTR_ERR(idata);
889
890 md = mmc_blk_get(bdev->bd_disk);
891 if (!md) {
892 err = -EINVAL;
893 goto cmd_err;
894 }
895
896 card = md->queue.card;
897 if (IS_ERR(card)) {
898 err = PTR_ERR(card);
899 goto cmd_done;
900 }
901
902 mmc_get_card(card);
903
Grant Grundlerb0934102015-09-23 18:30:33 -0700904 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100905
Adrian Hunter3c866562016-05-04 14:38:12 +0300906 /* Always switch back to main area after RPMB access */
907 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
908 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
909
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200910 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400911
Grant Grundlerb0934102015-09-23 18:30:33 -0700912 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100913
John Calixtocb87ea22011-04-26 18:56:29 -0400914cmd_done:
915 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300916cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400917 kfree(idata->buf);
918 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700919 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400920}
921
Jon Huntera5f57742015-09-22 10:27:53 +0100922static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
923 struct mmc_ioc_multi_cmd __user *user)
924{
925 struct mmc_blk_ioc_data **idata = NULL;
926 struct mmc_ioc_cmd __user *cmds = user->cmds;
927 struct mmc_card *card;
928 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -0700929 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100930 __u64 num_of_cmds;
931
Shawn Lin83c742c2016-03-16 18:15:47 +0800932 /*
933 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
934 * whole block device, not on a partition. This prevents overspray
935 * between sibling partitions.
936 */
937 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
938 return -EPERM;
939
Jon Huntera5f57742015-09-22 10:27:53 +0100940 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
941 sizeof(num_of_cmds)))
942 return -EFAULT;
943
944 if (num_of_cmds > MMC_IOC_MAX_CMDS)
945 return -EINVAL;
946
947 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
948 if (!idata)
949 return -ENOMEM;
950
951 for (i = 0; i < num_of_cmds; i++) {
952 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
953 if (IS_ERR(idata[i])) {
954 err = PTR_ERR(idata[i]);
955 num_of_cmds = i;
956 goto cmd_err;
957 }
958 }
959
960 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -0800961 if (!md) {
962 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +0100963 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -0800964 }
Jon Huntera5f57742015-09-22 10:27:53 +0100965
966 card = md->queue.card;
967 if (IS_ERR(card)) {
968 err = PTR_ERR(card);
969 goto cmd_done;
970 }
971
972 mmc_get_card(card);
973
Grant Grundlerb0934102015-09-23 18:30:33 -0700974 for (i = 0; i < num_of_cmds && !ioc_err; i++)
975 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100976
Adrian Hunter3c866562016-05-04 14:38:12 +0300977 /* Always switch back to main area after RPMB access */
978 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
979 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
980
Jon Huntera5f57742015-09-22 10:27:53 +0100981 mmc_put_card(card);
982
983 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700984 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100985 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100986
987cmd_done:
988 mmc_blk_put(md);
989cmd_err:
990 for (i = 0; i < num_of_cmds; i++) {
991 kfree(idata[i]->buf);
992 kfree(idata[i]);
993 }
994 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700995 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100996}
997
John Calixtocb87ea22011-04-26 18:56:29 -0400998static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
999 unsigned int cmd, unsigned long arg)
1000{
Jon Huntera5f57742015-09-22 10:27:53 +01001001 switch (cmd) {
1002 case MMC_IOC_CMD:
1003 return mmc_blk_ioctl_cmd(bdev,
1004 (struct mmc_ioc_cmd __user *)arg);
1005 case MMC_IOC_MULTI_CMD:
1006 return mmc_blk_ioctl_multi_cmd(bdev,
1007 (struct mmc_ioc_multi_cmd __user *)arg);
1008 default:
1009 return -EINVAL;
1010 }
John Calixtocb87ea22011-04-26 18:56:29 -04001011}
1012
1013#ifdef CONFIG_COMPAT
1014static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
1015 unsigned int cmd, unsigned long arg)
1016{
1017 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
1018}
1019#endif
1020
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001021static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -05001022 .open = mmc_blk_open,
1023 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001024 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -04001026 .ioctl = mmc_blk_ioctl,
1027#ifdef CONFIG_COMPAT
1028 .compat_ioctl = mmc_blk_compat_ioctl,
1029#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030};
1031
Andrei Warkentin371a6892011-04-11 18:10:25 -05001032static inline int mmc_blk_part_switch(struct mmc_card *card,
1033 struct mmc_blk_data *md)
1034{
1035 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001036 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001037
Andrei Warkentin371a6892011-04-11 18:10:25 -05001038 if (main_md->part_curr == md->part_type)
1039 return 0;
1040
1041 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001042 u8 part_config = card->ext_csd.part_config;
1043
Adrian Hunter57da0c02016-05-04 14:38:13 +03001044 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1045 mmc_retune_pause(card->host);
1046
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001047 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1048 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001049
1050 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001051 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001052 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001053 if (ret) {
1054 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1055 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001056 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001057 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001058
1059 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001060
1061 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1062 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001063 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001064
1065 main_md->part_curr = md->part_type;
1066 return 0;
1067}
1068
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001069static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1070{
1071 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001072 u32 result;
1073 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001074
Venkatraman Sad5fd972011-08-25 00:30:50 +05301075 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001076 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001077 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001078
1079 struct scatterlist sg;
1080
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001081 cmd.opcode = MMC_APP_CMD;
1082 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001083 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001084
1085 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001086 if (err)
1087 return (u32)-1;
1088 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001089 return (u32)-1;
1090
1091 memset(&cmd, 0, sizeof(struct mmc_command));
1092
1093 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1094 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001095 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001096
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001097 data.blksz = 4;
1098 data.blocks = 1;
1099 data.flags = MMC_DATA_READ;
1100 data.sg = &sg;
1101 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301102 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001103
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001104 mrq.cmd = &cmd;
1105 mrq.data = &data;
1106
Ben Dooks051913d2009-06-08 23:33:57 +01001107 blocks = kmalloc(4, GFP_KERNEL);
1108 if (!blocks)
1109 return (u32)-1;
1110
1111 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001112
1113 mmc_wait_for_req(card->host, &mrq);
1114
Ben Dooks051913d2009-06-08 23:33:57 +01001115 result = ntohl(*blocks);
1116 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001117
Ben Dooks051913d2009-06-08 23:33:57 +01001118 if (cmd.error || data.error)
1119 result = (u32)-1;
1120
1121 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001122}
1123
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001124static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001125{
Chris Ball1278dba2011-04-13 23:40:30 -04001126 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001127 int err;
1128
Adrian Hunter504f1912008-10-16 12:55:25 +03001129 cmd.opcode = MMC_SEND_STATUS;
1130 if (!mmc_host_is_spi(card->host))
1131 cmd.arg = card->rca << 16;
1132 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001133 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1134 if (err == 0)
1135 *status = cmd.resp[0];
1136 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001137}
1138
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001139static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001140 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001141{
1142 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1143 int err = 0;
1144 u32 status;
1145
1146 do {
1147 err = get_card_status(card, &status, 5);
1148 if (err) {
1149 pr_err("%s: error %d requesting status\n",
1150 req->rq_disk->disk_name, err);
1151 return err;
1152 }
1153
1154 if (status & R1_ERROR) {
1155 pr_err("%s: %s: error sending status cmd, status %#x\n",
1156 req->rq_disk->disk_name, __func__, status);
1157 *gen_err = 1;
1158 }
1159
Ulf Hansson95a91292014-01-29 13:11:27 +01001160 /* We may rely on the host hw to handle busy detection.*/
1161 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1162 hw_busy_detect)
1163 break;
1164
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001165 /*
1166 * Timeout if the device never becomes ready for data and never
1167 * leaves the program state.
1168 */
1169 if (time_after(jiffies, timeout)) {
1170 pr_err("%s: Card stuck in programming state! %s %s\n",
1171 mmc_hostname(card->host),
1172 req->rq_disk->disk_name, __func__);
1173 return -ETIMEDOUT;
1174 }
1175
1176 /*
1177 * Some cards mishandle the status bits,
1178 * so make sure to check both the busy
1179 * indication and the card state.
1180 */
1181 } while (!(status & R1_READY_FOR_DATA) ||
1182 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1183
1184 return err;
1185}
1186
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001187static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1188 struct request *req, int *gen_err, u32 *stop_status)
1189{
1190 struct mmc_host *host = card->host;
1191 struct mmc_command cmd = {0};
1192 int err;
1193 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1194
1195 /*
1196 * Normally we use R1B responses for WRITE, but in cases where the host
1197 * has specified a max_busy_timeout we need to validate it. A failure
1198 * means we need to prevent the host from doing hw busy detection, which
1199 * is done by converting to a R1 response instead.
1200 */
1201 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1202 use_r1b_resp = false;
1203
1204 cmd.opcode = MMC_STOP_TRANSMISSION;
1205 if (use_r1b_resp) {
1206 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1207 cmd.busy_timeout = timeout_ms;
1208 } else {
1209 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1210 }
1211
1212 err = mmc_wait_for_cmd(host, &cmd, 5);
1213 if (err)
1214 return err;
1215
1216 *stop_status = cmd.resp[0];
1217
1218 /* No need to check card status in case of READ. */
1219 if (rq_data_dir(req) == READ)
1220 return 0;
1221
1222 if (!mmc_host_is_spi(host) &&
1223 (*stop_status & R1_ERROR)) {
1224 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1225 req->rq_disk->disk_name, __func__, *stop_status);
1226 *gen_err = 1;
1227 }
1228
1229 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1230}
1231
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301232#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001233#define ERR_RETRY 2
1234#define ERR_ABORT 1
1235#define ERR_CONTINUE 0
1236
1237static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1238 bool status_valid, u32 status)
1239{
1240 switch (error) {
1241 case -EILSEQ:
1242 /* response crc error, retry the r/w cmd */
1243 pr_err("%s: %s sending %s command, card status %#x\n",
1244 req->rq_disk->disk_name, "response CRC error",
1245 name, status);
1246 return ERR_RETRY;
1247
1248 case -ETIMEDOUT:
1249 pr_err("%s: %s sending %s command, card status %#x\n",
1250 req->rq_disk->disk_name, "timed out", name, status);
1251
1252 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301253 if (!status_valid) {
1254 pr_err("%s: status not valid, retrying timeout\n",
1255 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001256 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301257 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001258
1259 /*
1260 * If it was a r/w cmd crc error, or illegal command
1261 * (eg, issued in wrong state) then retry - we should
1262 * have corrected the state problem above.
1263 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301264 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1265 pr_err("%s: command error, retrying timeout\n",
1266 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001267 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301268 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001269
1270 /* Otherwise abort the command */
1271 return ERR_ABORT;
1272
1273 default:
1274 /* We don't understand the error code the driver gave us */
1275 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1276 req->rq_disk->disk_name, error, status);
1277 return ERR_ABORT;
1278 }
1279}
1280
1281/*
1282 * Initial r/w and stop cmd error recovery.
1283 * We don't know whether the card received the r/w cmd or not, so try to
1284 * restore things back to a sane state. Essentially, we do this as follows:
1285 * - Obtain card status. If the first attempt to obtain card status fails,
1286 * the status word will reflect the failed status cmd, not the failed
1287 * r/w cmd. If we fail to obtain card status, it suggests we can no
1288 * longer communicate with the card.
1289 * - Check the card state. If the card received the cmd but there was a
1290 * transient problem with the response, it might still be in a data transfer
1291 * mode. Try to send it a stop command. If this fails, we can't recover.
1292 * - If the r/w cmd failed due to a response CRC error, it was probably
1293 * transient, so retry the cmd.
1294 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1295 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1296 * illegal cmd, retry.
1297 * Otherwise we don't understand what happened, so abort.
1298 */
1299static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001300 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001301{
1302 bool prev_cmd_status_valid = true;
1303 u32 status, stop_status = 0;
1304 int err, retry;
1305
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301306 if (mmc_card_removed(card))
1307 return ERR_NOMEDIUM;
1308
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001309 /*
1310 * Try to get card status which indicates both the card state
1311 * and why there was no response. If the first attempt fails,
1312 * we can't be sure the returned status is for the r/w command.
1313 */
1314 for (retry = 2; retry >= 0; retry--) {
1315 err = get_card_status(card, &status, 0);
1316 if (!err)
1317 break;
1318
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001319 /* Re-tune if needed */
1320 mmc_retune_recheck(card->host);
1321
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001322 prev_cmd_status_valid = false;
1323 pr_err("%s: error %d sending status command, %sing\n",
1324 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1325 }
1326
1327 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301328 if (err) {
1329 /* Check if the card is removed */
1330 if (mmc_detect_card_removed(card->host))
1331 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001332 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301333 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001334
Adrian Hunter67716322011-08-29 16:42:15 +03001335 /* Flag ECC errors */
1336 if ((status & R1_CARD_ECC_FAILED) ||
1337 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1338 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1339 *ecc_err = 1;
1340
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001341 /* Flag General errors */
1342 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1343 if ((status & R1_ERROR) ||
1344 (brq->stop.resp[0] & R1_ERROR)) {
1345 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1346 req->rq_disk->disk_name, __func__,
1347 brq->stop.resp[0], status);
1348 *gen_err = 1;
1349 }
1350
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001351 /*
1352 * Check the current card state. If it is in some data transfer
1353 * mode, tell it to stop (and hopefully transition back to TRAN.)
1354 */
1355 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1356 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001357 err = send_stop(card,
1358 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1359 req, gen_err, &stop_status);
1360 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001361 pr_err("%s: error %d sending stop command\n",
1362 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001363 /*
1364 * If the stop cmd also timed out, the card is probably
1365 * not present, so abort. Other errors are bad news too.
1366 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001367 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001368 }
1369
Adrian Hunter67716322011-08-29 16:42:15 +03001370 if (stop_status & R1_CARD_ECC_FAILED)
1371 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001372 }
1373
1374 /* Check for set block count errors */
1375 if (brq->sbc.error)
1376 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1377 prev_cmd_status_valid, status);
1378
1379 /* Check for r/w command errors */
1380 if (brq->cmd.error)
1381 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1382 prev_cmd_status_valid, status);
1383
Adrian Hunter67716322011-08-29 16:42:15 +03001384 /* Data errors */
1385 if (!brq->stop.error)
1386 return ERR_CONTINUE;
1387
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001388 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001389 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 +01001390 req->rq_disk->disk_name, brq->stop.error,
1391 brq->cmd.resp[0], status);
1392
1393 /*
1394 * Subsitute in our own stop status as this will give the error
1395 * state which happened during the execution of the r/w command.
1396 */
1397 if (stop_status) {
1398 brq->stop.resp[0] = stop_status;
1399 brq->stop.error = 0;
1400 }
1401 return ERR_CONTINUE;
1402}
1403
Adrian Hunter67716322011-08-29 16:42:15 +03001404static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1405 int type)
1406{
1407 int err;
1408
1409 if (md->reset_done & type)
1410 return -EEXIST;
1411
1412 md->reset_done |= type;
1413 err = mmc_hw_reset(host);
1414 /* Ensure we switch back to the correct partition */
1415 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001416 struct mmc_blk_data *main_md =
1417 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001418 int part_err;
1419
1420 main_md->part_curr = main_md->part_type;
1421 part_err = mmc_blk_part_switch(host->card, md);
1422 if (part_err) {
1423 /*
1424 * We have failed to get back into the correct
1425 * partition, so we need to abort the whole request.
1426 */
1427 return -ENODEV;
1428 }
1429 }
1430 return err;
1431}
1432
1433static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1434{
1435 md->reset_done &= ~type;
1436}
1437
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001438int mmc_access_rpmb(struct mmc_queue *mq)
1439{
1440 struct mmc_blk_data *md = mq->data;
1441 /*
1442 * If this is a RPMB partition access, return ture
1443 */
1444 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1445 return true;
1446
1447 return false;
1448}
1449
Adrian Hunterbd788c92010-08-11 14:17:47 -07001450static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1451{
1452 struct mmc_blk_data *md = mq->data;
1453 struct mmc_card *card = md->queue.card;
1454 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001455 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001456
Adrian Hunterbd788c92010-08-11 14:17:47 -07001457 if (!mmc_can_erase(card)) {
1458 err = -EOPNOTSUPP;
1459 goto out;
1460 }
1461
1462 from = blk_rq_pos(req);
1463 nr = blk_rq_sectors(req);
1464
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001465 if (mmc_can_discard(card))
1466 arg = MMC_DISCARD_ARG;
1467 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001468 arg = MMC_TRIM_ARG;
1469 else
1470 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001471retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001472 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1473 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1474 INAND_CMD38_ARG_EXT_CSD,
1475 arg == MMC_TRIM_ARG ?
1476 INAND_CMD38_ARG_TRIM :
1477 INAND_CMD38_ARG_ERASE,
1478 0);
1479 if (err)
1480 goto out;
1481 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001482 err = mmc_erase(card, from, nr, arg);
1483out:
Adrian Hunter67716322011-08-29 16:42:15 +03001484 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1485 goto retry;
1486 if (!err)
1487 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301488 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001489
Adrian Hunterbd788c92010-08-11 14:17:47 -07001490 return err ? 0 : 1;
1491}
1492
Adrian Hunter49804542010-08-11 14:17:50 -07001493static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1494 struct request *req)
1495{
1496 struct mmc_blk_data *md = mq->data;
1497 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001498 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001499 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001500
Maya Erez775a9362013-04-18 15:41:55 +03001501 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001502 err = -EOPNOTSUPP;
1503 goto out;
1504 }
1505
1506 from = blk_rq_pos(req);
1507 nr = blk_rq_sectors(req);
1508
Maya Erez775a9362013-04-18 15:41:55 +03001509 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1510 arg = MMC_SECURE_TRIM1_ARG;
1511 else
1512 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001513
Adrian Hunter67716322011-08-29 16:42:15 +03001514retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001515 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1516 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1517 INAND_CMD38_ARG_EXT_CSD,
1518 arg == MMC_SECURE_TRIM1_ARG ?
1519 INAND_CMD38_ARG_SECTRIM1 :
1520 INAND_CMD38_ARG_SECERASE,
1521 0);
1522 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001523 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001524 }
Adrian Hunter28302812012-04-05 14:45:48 +03001525
Adrian Hunter49804542010-08-11 14:17:50 -07001526 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001527 if (err == -EIO)
1528 goto out_retry;
1529 if (err)
1530 goto out;
1531
1532 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001533 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1534 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1535 INAND_CMD38_ARG_EXT_CSD,
1536 INAND_CMD38_ARG_SECTRIM2,
1537 0);
1538 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001539 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001540 }
Adrian Hunter28302812012-04-05 14:45:48 +03001541
Adrian Hunter49804542010-08-11 14:17:50 -07001542 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001543 if (err == -EIO)
1544 goto out_retry;
1545 if (err)
1546 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001547 }
Adrian Hunter28302812012-04-05 14:45:48 +03001548
Adrian Hunter28302812012-04-05 14:45:48 +03001549out_retry:
1550 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001551 goto retry;
1552 if (!err)
1553 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001554out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301555 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001556
Adrian Hunter49804542010-08-11 14:17:50 -07001557 return err ? 0 : 1;
1558}
1559
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001560static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1561{
1562 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001563 struct mmc_card *card = md->queue.card;
1564 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001565
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001566 ret = mmc_flush_cache(card);
1567 if (ret)
1568 ret = -EIO;
1569
Mark Salyzyn6904e432016-01-28 11:12:25 -08001570#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
1571 else if (atomic_read(&mq->cache_size)) {
1572 long used = mmc_blk_cache_used(mq, jiffies);
1573
1574 if (used) {
1575 int speed = atomic_read(&mq->max_write_speed);
1576
1577 if (speed_valid(speed)) {
1578 unsigned long msecs = jiffies_to_msecs(
1579 size_and_speed_to_jiffies(
1580 used, speed));
1581 if (msecs)
1582 msleep(msecs);
1583 }
1584 }
1585 }
1586#endif
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301587 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001588
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001589 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001590}
1591
1592/*
1593 * Reformat current write as a reliable write, supporting
1594 * both legacy and the enhanced reliable write MMC cards.
1595 * In each transfer we'll handle only as much as a single
1596 * reliable write can handle, thus finish the request in
1597 * partial completions.
1598 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001599static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1600 struct mmc_card *card,
1601 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001602{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001603 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1604 /* Legacy mode imposes restrictions on transfers. */
1605 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1606 brq->data.blocks = 1;
1607
1608 if (brq->data.blocks > card->ext_csd.rel_sectors)
1609 brq->data.blocks = card->ext_csd.rel_sectors;
1610 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1611 brq->data.blocks = 1;
1612 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001613}
1614
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001615#define CMD_ERRORS \
1616 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1617 R1_ADDRESS_ERROR | /* Misaligned address */ \
1618 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1619 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1620 R1_CC_ERROR | /* Card controller error */ \
1621 R1_ERROR) /* General/unknown error */
1622
Per Forlinee8a43a2011-07-01 18:55:33 +02001623static int mmc_blk_err_check(struct mmc_card *card,
1624 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001625{
Per Forlinee8a43a2011-07-01 18:55:33 +02001626 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1627 mmc_active);
1628 struct mmc_blk_request *brq = &mq_mrq->brq;
1629 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001630 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001631 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001632
1633 /*
1634 * sbc.error indicates a problem with the set block count
1635 * command. No data will have been transferred.
1636 *
1637 * cmd.error indicates a problem with the r/w command. No
1638 * data will have been transferred.
1639 *
1640 * stop.error indicates a problem with the stop command. Data
1641 * may have been transferred, or may still be transferring.
1642 */
Adrian Hunter67716322011-08-29 16:42:15 +03001643 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1644 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001645 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001646 case ERR_RETRY:
1647 return MMC_BLK_RETRY;
1648 case ERR_ABORT:
1649 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301650 case ERR_NOMEDIUM:
1651 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001652 case ERR_CONTINUE:
1653 break;
1654 }
1655 }
1656
1657 /*
1658 * Check for errors relating to the execution of the
1659 * initial command - such as address errors. No data
1660 * has been transferred.
1661 */
1662 if (brq->cmd.resp[0] & CMD_ERRORS) {
1663 pr_err("%s: r/w command failed, status = %#x\n",
1664 req->rq_disk->disk_name, brq->cmd.resp[0]);
1665 return MMC_BLK_ABORT;
1666 }
1667
1668 /*
1669 * Everything else is either success, or a data error of some
1670 * kind. If it was a write, we may have transitioned to
1671 * program mode, which we have to wait for it to complete.
1672 */
1673 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001674 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001675
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001676 /* Check stop command response */
1677 if (brq->stop.resp[0] & R1_ERROR) {
1678 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1679 req->rq_disk->disk_name, __func__,
1680 brq->stop.resp[0]);
1681 gen_err = 1;
1682 }
1683
Ulf Hansson95a91292014-01-29 13:11:27 +01001684 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1685 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001686 if (err)
1687 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001688 }
1689
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001690 /* if general error occurs, retry the write operation. */
1691 if (gen_err) {
1692 pr_warn("%s: retrying write for general error\n",
1693 req->rq_disk->disk_name);
1694 return MMC_BLK_RETRY;
1695 }
1696
Per Forlind78d4a82011-07-01 18:55:30 +02001697 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001698 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001699 pr_debug("%s: retrying because a re-tune was needed\n",
1700 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001701 brq->retune_retry_done = 1;
1702 return MMC_BLK_RETRY;
1703 }
Per Forlind78d4a82011-07-01 18:55:30 +02001704 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1705 req->rq_disk->disk_name, brq->data.error,
1706 (unsigned)blk_rq_pos(req),
1707 (unsigned)blk_rq_sectors(req),
1708 brq->cmd.resp[0], brq->stop.resp[0]);
1709
1710 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001711 if (ecc_err)
1712 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001713 return MMC_BLK_DATA_ERR;
1714 } else {
1715 return MMC_BLK_CMD_ERR;
1716 }
1717 }
1718
Adrian Hunter67716322011-08-29 16:42:15 +03001719 if (!brq->data.bytes_xfered)
1720 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001721
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001722 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1723 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1724 return MMC_BLK_PARTIAL;
1725 else
1726 return MMC_BLK_SUCCESS;
1727 }
1728
Adrian Hunter67716322011-08-29 16:42:15 +03001729 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1730 return MMC_BLK_PARTIAL;
1731
1732 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001733}
1734
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001735static int mmc_blk_packed_err_check(struct mmc_card *card,
1736 struct mmc_async_req *areq)
1737{
1738 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1739 mmc_active);
1740 struct request *req = mq_rq->req;
1741 struct mmc_packed *packed = mq_rq->packed;
1742 int err, check, status;
1743 u8 *ext_csd;
1744
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001745 packed->retries--;
1746 check = mmc_blk_err_check(card, areq);
1747 err = get_card_status(card, &status, 0);
1748 if (err) {
1749 pr_err("%s: error %d sending status command\n",
1750 req->rq_disk->disk_name, err);
1751 return MMC_BLK_ABORT;
1752 }
1753
1754 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02001755 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001756 if (err) {
1757 pr_err("%s: error %d sending ext_csd\n",
1758 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02001759 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001760 }
1761
1762 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1763 EXT_CSD_PACKED_FAILURE) &&
1764 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1765 EXT_CSD_PACKED_GENERIC_ERROR)) {
1766 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1767 EXT_CSD_PACKED_INDEXED_ERROR) {
1768 packed->idx_failure =
1769 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1770 check = MMC_BLK_PARTIAL;
1771 }
1772 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1773 "failure index: %d\n",
1774 req->rq_disk->disk_name, packed->nr_entries,
1775 packed->blocks, packed->idx_failure);
1776 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001777 kfree(ext_csd);
1778 }
1779
1780 return check;
1781}
1782
Per Forlin54d49d72011-07-01 18:55:29 +02001783static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1784 struct mmc_card *card,
1785 int disable_multi,
1786 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Per Forlin54d49d72011-07-01 18:55:29 +02001788 u32 readcmd, writecmd;
1789 struct mmc_blk_request *brq = &mqrq->brq;
1790 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301792 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001794 /*
1795 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001796 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001797 */
Luca Porziod3df0462015-11-06 15:12:26 +00001798 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001799 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001800 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001801
Per Forlin54d49d72011-07-01 18:55:29 +02001802 memset(brq, 0, sizeof(struct mmc_blk_request));
1803 brq->mrq.cmd = &brq->cmd;
1804 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Per Forlin54d49d72011-07-01 18:55:29 +02001806 brq->cmd.arg = blk_rq_pos(req);
1807 if (!mmc_card_blockaddr(card))
1808 brq->cmd.arg <<= 9;
1809 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1810 brq->data.blksz = 512;
1811 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1812 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001813 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Asutosh Dasf0665412012-07-27 18:10:19 +05301815 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02001816 /*
1817 * The block layer doesn't support all sector count
1818 * restrictions, so we need to be prepared for too big
1819 * requests.
1820 */
1821 if (brq->data.blocks > card->host->max_blk_count)
1822 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001824 if (brq->data.blocks > 1) {
1825 /*
1826 * After a read error, we redo the request one sector
1827 * at a time in order to accurately determine which
1828 * sectors can be read successfully.
1829 */
1830 if (disable_multi)
1831 brq->data.blocks = 1;
1832
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001833 /*
1834 * Some controllers have HW issues while operating
1835 * in multiple I/O mode
1836 */
1837 if (card->host->ops->multi_io_quirk)
1838 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1839 (rq_data_dir(req) == READ) ?
1840 MMC_DATA_READ : MMC_DATA_WRITE,
1841 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001842 }
Per Forlin54d49d72011-07-01 18:55:29 +02001843
1844 if (brq->data.blocks > 1 || do_rel_wr) {
1845 /* SPI multiblock writes terminate using a special
1846 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001847 */
Per Forlin54d49d72011-07-01 18:55:29 +02001848 if (!mmc_host_is_spi(card->host) ||
1849 rq_data_dir(req) == READ)
1850 brq->mrq.stop = &brq->stop;
1851 readcmd = MMC_READ_MULTIPLE_BLOCK;
1852 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1853 } else {
1854 brq->mrq.stop = NULL;
1855 readcmd = MMC_READ_SINGLE_BLOCK;
1856 writecmd = MMC_WRITE_BLOCK;
1857 }
1858 if (rq_data_dir(req) == READ) {
1859 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001860 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001861 if (brq->mrq.stop)
1862 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1863 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001864 } else {
1865 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001866 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001867 if (brq->mrq.stop)
1868 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1869 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001870 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001871
Per Forlin54d49d72011-07-01 18:55:29 +02001872 if (do_rel_wr)
1873 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001874
Per Forlin54d49d72011-07-01 18:55:29 +02001875 /*
Saugata Das42659002011-12-21 13:09:17 +05301876 * Data tag is used only during writing meta data to speed
1877 * up write and any subsequent read of this meta data
1878 */
1879 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1880 (req->cmd_flags & REQ_META) &&
1881 (rq_data_dir(req) == WRITE) &&
1882 ((brq->data.blocks * brq->data.blksz) >=
1883 card->ext_csd.data_tag_unit_size);
1884
1885 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001886 * Pre-defined multi-block transfers are preferable to
1887 * open ended-ones (and necessary for reliable writes).
1888 * However, it is not sufficient to just send CMD23,
1889 * and avoid the final CMD12, as on an error condition
1890 * CMD12 (stop) needs to be sent anyway. This, coupled
1891 * with Auto-CMD23 enhancements provided by some
1892 * hosts, means that the complexity of dealing
1893 * with this is best left to the host. If CMD23 is
1894 * supported by card and host, we'll fill sbc in and let
1895 * the host deal with handling it correctly. This means
1896 * that for hosts that don't expose MMC_CAP_CMD23, no
1897 * change of behavior will be observed.
1898 *
1899 * N.B: Some MMC cards experience perf degradation.
1900 * We'll avoid using CMD23-bounded multiblock writes for
1901 * these, while retaining features like reliable writes.
1902 */
Saugata Das42659002011-12-21 13:09:17 +05301903 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1904 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1905 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001906 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1907 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301908 (do_rel_wr ? (1 << 31) : 0) |
1909 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001910 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1911 brq->mrq.sbc = &brq->sbc;
1912 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001913
Per Forlin54d49d72011-07-01 18:55:29 +02001914 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001915
Per Forlin54d49d72011-07-01 18:55:29 +02001916 brq->data.sg = mqrq->sg;
1917 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001918
Per Forlin54d49d72011-07-01 18:55:29 +02001919 /*
1920 * Adjust the sg list so it is the same size as the
1921 * request.
1922 */
1923 if (brq->data.blocks != blk_rq_sectors(req)) {
1924 int i, data_size = brq->data.blocks << 9;
1925 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001926
Per Forlin54d49d72011-07-01 18:55:29 +02001927 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1928 data_size -= sg->length;
1929 if (data_size <= 0) {
1930 sg->length += data_size;
1931 i++;
1932 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001933 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001934 }
Per Forlin54d49d72011-07-01 18:55:29 +02001935 brq->data.sg_len = i;
1936 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001937
Per Forlinee8a43a2011-07-01 18:55:33 +02001938 mqrq->mmc_active.mrq = &brq->mrq;
1939 mqrq->mmc_active.err_check = mmc_blk_err_check;
1940
Per Forlin54d49d72011-07-01 18:55:29 +02001941 mmc_queue_bounce_pre(mqrq);
1942}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001944static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1945 struct mmc_card *card)
1946{
1947 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1948 unsigned int max_seg_sz = queue_max_segment_size(q);
1949 unsigned int len, nr_segs = 0;
1950
1951 do {
1952 len = min(hdr_sz, max_seg_sz);
1953 hdr_sz -= len;
1954 nr_segs++;
1955 } while (hdr_sz);
1956
1957 return nr_segs;
1958}
1959
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02001960static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1961 struct request *req)
1962{
1963 struct mmc_host *host = mq->card->host;
1964 int data_dir;
1965
1966 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1967 return;
1968
1969 /*
1970 * In case the packing control is not supported by the host, it should
1971 * not have an effect on the write packing. Therefore we have to enable
1972 * the write packing
1973 */
1974 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1975 mq->wr_packing_enabled = true;
1976 return;
1977 }
1978
1979 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
1980 if (mq->num_of_potential_packed_wr_reqs >
1981 mq->num_wr_reqs_to_start_packing)
1982 mq->wr_packing_enabled = true;
Tatyana Brokhman843915a2012-10-07 10:26:27 +02001983 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02001984 return;
1985 }
1986
1987 data_dir = rq_data_dir(req);
1988
1989 if (data_dir == READ) {
1990 mq->num_of_potential_packed_wr_reqs = 0;
1991 mq->wr_packing_enabled = false;
1992 return;
1993 } else if (data_dir == WRITE) {
1994 mq->num_of_potential_packed_wr_reqs++;
1995 }
1996
1997 if (mq->num_of_potential_packed_wr_reqs >
1998 mq->num_wr_reqs_to_start_packing)
1999 mq->wr_packing_enabled = true;
2000}
2001
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002002struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2003{
2004 if (!card)
2005 return NULL;
2006
2007 return &card->wr_pack_stats;
2008}
2009EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2010
2011void mmc_blk_init_packed_statistics(struct mmc_card *card)
2012{
2013 int max_num_of_packed_reqs = 0;
2014
2015 if (!card || !card->wr_pack_stats.packing_events)
2016 return;
2017
2018 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2019
2020 spin_lock(&card->wr_pack_stats.lock);
2021 memset(card->wr_pack_stats.packing_events, 0,
2022 (max_num_of_packed_reqs + 1) *
2023 sizeof(*card->wr_pack_stats.packing_events));
2024 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2025 sizeof(card->wr_pack_stats.pack_stop_reason));
2026 card->wr_pack_stats.enabled = true;
2027 spin_unlock(&card->wr_pack_stats.lock);
2028}
2029EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2030
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002031static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2032{
2033 struct request_queue *q = mq->queue;
2034 struct mmc_card *card = mq->card;
2035 struct request *cur = req, *next = NULL;
2036 struct mmc_blk_data *md = mq->data;
2037 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2038 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2039 unsigned int req_sectors = 0, phys_segments = 0;
2040 unsigned int max_blk_count, max_phys_segs;
2041 bool put_back = true;
2042 u8 max_packed_rw = 0;
2043 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002044 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002045
Shawn Lin96e52da2016-08-26 08:49:55 +08002046 /*
2047 * We don't need to check packed for any further
2048 * operation of packed stuff as we set MMC_PACKED_NONE
2049 * and return zero for reqs if geting null packed. Also
2050 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2051 * it again when removing blk req.
2052 */
2053 if (!mqrq->packed) {
2054 md->flags &= (~MMC_BLK_PACKED_CMD);
2055 goto no_packed;
2056 }
2057
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002058 if (!(md->flags & MMC_BLK_PACKED_CMD))
2059 goto no_packed;
2060
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002061 if (!mq->wr_packing_enabled)
2062 goto no_packed;
2063
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002064 if ((rq_data_dir(cur) == WRITE) &&
2065 mmc_host_packed_wr(card->host))
2066 max_packed_rw = card->ext_csd.max_packed_writes;
2067
2068 if (max_packed_rw == 0)
2069 goto no_packed;
2070
2071 if (mmc_req_rel_wr(cur) &&
2072 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2073 goto no_packed;
2074
2075 if (mmc_large_sector(card) &&
2076 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2077 goto no_packed;
2078
2079 mmc_blk_clear_packed(mqrq);
2080
2081 max_blk_count = min(card->host->max_blk_count,
2082 card->host->max_req_size >> 9);
2083 if (unlikely(max_blk_count > 0xffff))
2084 max_blk_count = 0xffff;
2085
2086 max_phys_segs = queue_max_segments(q);
2087 req_sectors += blk_rq_sectors(cur);
2088 phys_segments += cur->nr_phys_segments;
2089
2090 if (rq_data_dir(cur) == WRITE) {
2091 req_sectors += mmc_large_sector(card) ? 8 : 1;
2092 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2093 }
2094
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002095 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002096 do {
2097 if (reqs >= max_packed_rw - 1) {
2098 put_back = false;
2099 break;
2100 }
2101
2102 spin_lock_irq(q->queue_lock);
2103 next = blk_fetch_request(q);
2104 spin_unlock_irq(q->queue_lock);
2105 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002106 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002107 put_back = false;
2108 break;
2109 }
2110
2111 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002112 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2113 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002114 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002115 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002116
Mike Christie3a5e02c2016-06-05 14:32:23 -05002117 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002118 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002119 req_op(next) == REQ_OP_FLUSH) {
2120 if (req_op(next) != REQ_OP_SECURE_ERASE)
2121 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002122 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002123 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002124
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002125 if (rq_data_dir(cur) != rq_data_dir(next)) {
2126 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002127 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002128 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002129
2130 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002131 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2132 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002133 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002134 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002135
2136 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002137 if (req_sectors > max_blk_count) {
2138 if (stats->enabled)
2139 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002140 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002141 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002142
2143 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002144 if (phys_segments > max_phys_segs) {
2145 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002146 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002147 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002148
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002149 if (rq_data_dir(next) == WRITE)
2150 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002151 list_add_tail(&next->queuelist, &mqrq->packed->list);
2152 cur = next;
2153 reqs++;
2154 } while (1);
2155
2156 if (put_back) {
2157 spin_lock_irq(q->queue_lock);
2158 blk_requeue_request(q, next);
2159 spin_unlock_irq(q->queue_lock);
2160 }
2161
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002162 if (stats->enabled) {
2163 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2164 stats->packing_events[reqs + 1]++;
2165 if (reqs + 1 == max_packed_rw)
2166 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2167 }
2168
2169 spin_unlock(&stats->lock);
2170
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002171 if (reqs > 0) {
2172 list_add(&req->queuelist, &mqrq->packed->list);
2173 mqrq->packed->nr_entries = ++reqs;
2174 mqrq->packed->retries = reqs;
2175 return reqs;
2176 }
2177
2178no_packed:
2179 mqrq->cmd_type = MMC_PACKED_NONE;
2180 return 0;
2181}
2182
2183static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2184 struct mmc_card *card,
2185 struct mmc_queue *mq)
2186{
2187 struct mmc_blk_request *brq = &mqrq->brq;
2188 struct request *req = mqrq->req;
2189 struct request *prq;
2190 struct mmc_blk_data *md = mq->data;
2191 struct mmc_packed *packed = mqrq->packed;
2192 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002193 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002194 u8 hdr_blocks;
2195 u8 i = 1;
2196
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002197 mqrq->cmd_type = MMC_PACKED_WRITE;
2198 packed->blocks = 0;
2199 packed->idx_failure = MMC_PACKED_NR_IDX;
2200
2201 packed_cmd_hdr = packed->cmd_hdr;
2202 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002203 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2204 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002205 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2206
2207 /*
2208 * Argument for each entry of packed group
2209 */
2210 list_for_each_entry(prq, &packed->list, queuelist) {
2211 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2212 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2213 (prq->cmd_flags & REQ_META) &&
2214 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002215 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002216 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002217 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002218 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2219 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002220 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002221 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002222 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002223 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002224 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002225 packed->blocks += blk_rq_sectors(prq);
2226 i++;
2227 }
2228
2229 memset(brq, 0, sizeof(struct mmc_blk_request));
2230 brq->mrq.cmd = &brq->cmd;
2231 brq->mrq.data = &brq->data;
2232 brq->mrq.sbc = &brq->sbc;
2233 brq->mrq.stop = &brq->stop;
2234
2235 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2236 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2237 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2238
2239 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2240 brq->cmd.arg = blk_rq_pos(req);
2241 if (!mmc_card_blockaddr(card))
2242 brq->cmd.arg <<= 9;
2243 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2244
2245 brq->data.blksz = 512;
2246 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002247 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302248 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002249
2250 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2251 brq->stop.arg = 0;
2252 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2253
2254 mmc_set_data_timeout(&brq->data, card);
2255
2256 brq->data.sg = mqrq->sg;
2257 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2258
2259 mqrq->mmc_active.mrq = &brq->mrq;
2260 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2261
2262 mmc_queue_bounce_pre(mqrq);
2263}
2264
Adrian Hunter67716322011-08-29 16:42:15 +03002265static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2266 struct mmc_blk_request *brq, struct request *req,
2267 int ret)
2268{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002269 struct mmc_queue_req *mq_rq;
2270 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2271
Adrian Hunter67716322011-08-29 16:42:15 +03002272 /*
2273 * If this is an SD card and we're writing, we can first
2274 * mark the known good sectors as ok.
2275 *
2276 * If the card is not SD, we can still ok written sectors
2277 * as reported by the controller (which might be less than
2278 * the real number of written sectors, but never more).
2279 */
2280 if (mmc_card_sd(card)) {
2281 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302282 if (!brq->data.fault_injected) {
2283 blocks = mmc_sd_num_wr_blocks(card);
2284 if (blocks != (u32)-1)
2285 ret = blk_end_request(req, 0, blocks << 9);
2286 } else
2287 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002288 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002289 if (!mmc_packed_cmd(mq_rq->cmd_type))
2290 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002291 }
2292 return ret;
2293}
2294
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002295static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2296{
2297 struct request *prq;
2298 struct mmc_packed *packed = mq_rq->packed;
2299 int idx = packed->idx_failure, i = 0;
2300 int ret = 0;
2301
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002302 while (!list_empty(&packed->list)) {
2303 prq = list_entry_rq(packed->list.next);
2304 if (idx == i) {
2305 /* retry from error index */
2306 packed->nr_entries -= idx;
2307 mq_rq->req = prq;
2308 ret = 1;
2309
2310 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2311 list_del_init(&prq->queuelist);
2312 mmc_blk_clear_packed(mq_rq);
2313 }
2314 return ret;
2315 }
2316 list_del_init(&prq->queuelist);
2317 blk_end_request(prq, 0, blk_rq_bytes(prq));
2318 i++;
2319 }
2320
2321 mmc_blk_clear_packed(mq_rq);
2322 return ret;
2323}
2324
2325static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2326{
2327 struct request *prq;
2328 struct mmc_packed *packed = mq_rq->packed;
2329
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002330 while (!list_empty(&packed->list)) {
2331 prq = list_entry_rq(packed->list.next);
2332 list_del_init(&prq->queuelist);
2333 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2334 }
2335
2336 mmc_blk_clear_packed(mq_rq);
2337}
2338
2339static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2340 struct mmc_queue_req *mq_rq)
2341{
2342 struct request *prq;
2343 struct request_queue *q = mq->queue;
2344 struct mmc_packed *packed = mq_rq->packed;
2345
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002346 while (!list_empty(&packed->list)) {
2347 prq = list_entry_rq(packed->list.prev);
2348 if (prq->queuelist.prev != &packed->list) {
2349 list_del_init(&prq->queuelist);
2350 spin_lock_irq(q->queue_lock);
2351 blk_requeue_request(mq->queue, prq);
2352 spin_unlock_irq(q->queue_lock);
2353 } else {
2354 list_del_init(&prq->queuelist);
2355 }
2356 }
2357
2358 mmc_blk_clear_packed(mq_rq);
2359}
2360
Per Forlinee8a43a2011-07-01 18:55:33 +02002361static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02002362{
2363 struct mmc_blk_data *md = mq->data;
2364 struct mmc_card *card = md->queue.card;
2365 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002366 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002367 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002368 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302369 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02002370 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002371 const u8 packed_nr = 2;
2372 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002373#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2374 unsigned long waitfor = jiffies;
2375#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02002376
2377 if (!rqc && !mq->mqrq_prev->req)
2378 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002379
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002380 if (rqc)
2381 reqs = mmc_blk_prep_packed_list(mq, rqc);
2382
Per Forlin54d49d72011-07-01 18:55:29 +02002383 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002384 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302385 /*
2386 * When 4KB native sector is enabled, only 8 blocks
2387 * multiple read or write is allowed
2388 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00002389 if (mmc_large_sector(card) &&
2390 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302391 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
2392 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002393 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302394 goto cmd_abort;
2395 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002396
2397 if (reqs >= packed_nr)
2398 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2399 card, mq);
2400 else
2401 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002402 areq = &mq->mqrq_cur->mmc_active;
2403 } else
2404 areq = NULL;
2405 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002406 if (!areq) {
2407 if (status == MMC_BLK_NEW_REQUEST)
2408 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02002409 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002410 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002411
Per Forlinee8a43a2011-07-01 18:55:33 +02002412 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2413 brq = &mq_rq->brq;
2414 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002415 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002416 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002417
Per Forlind78d4a82011-07-01 18:55:30 +02002418 switch (status) {
2419 case MMC_BLK_SUCCESS:
2420 case MMC_BLK_PARTIAL:
2421 /*
2422 * A block was successfully transferred.
2423 */
Adrian Hunter67716322011-08-29 16:42:15 +03002424 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002425
Mark Salyzyn6904e432016-01-28 11:12:25 -08002426 mmc_blk_simulate_delay(mq, rqc, waitfor);
2427
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002428 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2429 ret = mmc_blk_end_packed_req(mq_rq);
2430 break;
2431 } else {
2432 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002433 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002434 }
2435
Adrian Hunter67716322011-08-29 16:42:15 +03002436 /*
2437 * If the blk_end_request function returns non-zero even
2438 * though all data has been transferred and no errors
2439 * were returned by the host controller, it's a bug.
2440 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002441 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302442 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002443 __func__, blk_rq_bytes(req),
2444 brq->data.bytes_xfered);
2445 rqc = NULL;
2446 goto cmd_abort;
2447 }
Per Forlind78d4a82011-07-01 18:55:30 +02002448 break;
2449 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002450 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002451 if (mmc_blk_reset(md, card->host, type))
2452 goto cmd_abort;
2453 if (!ret)
2454 goto start_new_req;
2455 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002456 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002457 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a82011-07-01 18:55:30 +02002458 if (retry++ < 5)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002459 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002460 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002461 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002462 if (!mmc_blk_reset(md, card->host, type))
2463 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002464 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002465 case MMC_BLK_DATA_ERR: {
2466 int err;
2467
2468 err = mmc_blk_reset(md, card->host, type);
2469 if (!err)
2470 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05302471 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002472 }
2473 case MMC_BLK_ECC_ERR:
2474 if (brq->data.blocks > 1) {
2475 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002476 pr_warn("%s: retrying using single block read\n",
2477 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002478 disable_multi = 1;
2479 break;
2480 }
Per Forlind78d4a82011-07-01 18:55:30 +02002481 /*
2482 * After an error, we redo I/O one sector at a
2483 * time, so we only reach here after trying to
2484 * read a single sector.
2485 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302486 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002487 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002488 if (!ret)
2489 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002490 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302491 case MMC_BLK_NOMEDIUM:
2492 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002493 default:
2494 pr_err("%s: Unhandled return value (%d)",
2495 req->rq_disk->disk_name, status);
2496 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002497 }
2498
Per Forlinee8a43a2011-07-01 18:55:33 +02002499 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002500 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2501 if (!mq_rq->packed->retries)
2502 goto cmd_abort;
2503 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2504 mmc_start_req(card->host,
2505 &mq_rq->mmc_active, NULL);
2506 } else {
2507
2508 /*
2509 * In case of a incomplete request
2510 * prepare it again and resend.
2511 */
2512 mmc_blk_rw_rq_prep(mq_rq, card,
2513 disable_multi, mq);
2514 mmc_start_req(card->host,
2515 &mq_rq->mmc_active, NULL);
2516 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002517 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 } while (ret);
2520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return 1;
2522
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002523 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002524 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2525 mmc_blk_abort_packed_req(mq_rq);
2526 } else {
2527 if (mmc_card_removed(card))
2528 req->cmd_flags |= REQ_QUIET;
2529 while (ret)
2530 ret = blk_end_request(req, -EIO,
2531 blk_rq_cur_bytes(req));
2532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Per Forlinee8a43a2011-07-01 18:55:33 +02002534 start_new_req:
2535 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002536 if (mmc_card_removed(card)) {
2537 rqc->cmd_flags |= REQ_QUIET;
2538 blk_end_request_all(rqc, -EIO);
2539 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002540 /*
2541 * If current request is packed, it needs to put back.
2542 */
2543 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2544 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2545
Seungwon Jeon7a819022013-01-22 19:48:07 +09002546 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2547 mmc_start_req(card->host,
2548 &mq->mqrq_cur->mmc_active, NULL);
2549 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002550 }
2551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 return 0;
2553}
2554
Linus Walleij29eb7bd2016-09-20 11:34:38 +02002555int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002556{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002557 int ret;
2558 struct mmc_blk_data *md = mq->data;
2559 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002560 struct mmc_host *host = card->host;
2561 unsigned long flags;
Adrian Hunter869c5542016-08-25 14:11:43 -06002562 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002563
Per Forlinee8a43a2011-07-01 18:55:33 +02002564 if (req && !mq->mqrq_prev->req)
2565 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002566 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002567
Andrei Warkentin371a6892011-04-11 18:10:25 -05002568 ret = mmc_blk_part_switch(card, md);
2569 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002570 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302571 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002572 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002573 ret = 0;
2574 goto out;
2575 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002576
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002577 mmc_blk_write_packing_control(mq, req);
2578
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002579 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05002580 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002581 /* complete ongoing async transfer before issuing discard */
2582 if (card->host->areq)
2583 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02002584 ret = mmc_blk_issue_discard_rq(mq, req);
2585 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
2586 /* complete ongoing async transfer before issuing secure erase*/
2587 if (card->host->areq)
2588 mmc_blk_issue_rw_rq(mq, NULL);
2589 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05002590 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002591 /* complete ongoing async transfer before issuing flush */
2592 if (card->host->areq)
2593 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002594 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002595 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002596 if (!req && host->areq) {
2597 spin_lock_irqsave(&host->context_info.lock, flags);
2598 host->context_info.is_waiting_last_req = true;
2599 spin_unlock_irqrestore(&host->context_info.lock, flags);
2600 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002601 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002602 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002603
Andrei Warkentin371a6892011-04-11 18:10:25 -05002604out:
Adrian Hunter869c5542016-08-25 14:11:43 -06002605 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002606 /*
2607 * Release host when there are no more requests
2608 * and after special request(discard, flush) is done.
2609 * In case sepecial request, there is no reentry to
2610 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2611 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002612 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002613 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002614}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Russell Kinga6f6c962006-01-03 22:38:44 +00002616static inline int mmc_blk_readonly(struct mmc_card *card)
2617{
2618 return mmc_card_readonly(card) ||
2619 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2620}
2621
Andrei Warkentin371a6892011-04-11 18:10:25 -05002622static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2623 struct device *parent,
2624 sector_t size,
2625 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002626 const char *subname,
2627 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
2629 struct mmc_blk_data *md;
2630 int devidx, ret;
2631
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002632again:
2633 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
2634 return ERR_PTR(-ENOMEM);
2635
2636 spin_lock(&mmc_blk_lock);
2637 ret = ida_get_new(&mmc_blk_ida, &devidx);
2638 spin_unlock(&mmc_blk_lock);
2639
2640 if (ret == -EAGAIN)
2641 goto again;
2642 else if (ret)
2643 return ERR_PTR(ret);
2644
2645 if (devidx >= max_devices) {
2646 ret = -ENOSPC;
2647 goto out;
2648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002650 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002651 if (!md) {
2652 ret = -ENOMEM;
2653 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002655
Johan Rudholmadd710e2011-12-02 08:51:06 +01002656 md->area_type = area_type;
2657
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002658 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002659 * Set the read-only status based on the supported commands
2660 * and the write protect switch.
2661 */
2662 md->read_only = mmc_blk_readonly(card);
2663
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002664 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002665 if (md->disk == NULL) {
2666 ret = -ENOMEM;
2667 goto err_kfree;
2668 }
2669
2670 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002671 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002672 md->usage = 1;
2673
Adrian Hunterd09408a2011-06-23 13:40:28 +03002674 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002675 if (ret)
2676 goto err_putdisk;
2677
Russell Kinga6f6c962006-01-03 22:38:44 +00002678 md->queue.data = md;
2679
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002680 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002681 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002682 md->disk->fops = &mmc_bdops;
2683 md->disk->private_data = md;
2684 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002685 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002686 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002687 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002688 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002689 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002690
2691 /*
2692 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2693 *
2694 * - be set for removable media with permanent block devices
2695 * - be unset for removable block devices with permanent media
2696 *
2697 * Since MMC block devices clearly fall under the second
2698 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2699 * should use the block device creation/destruction hotplug
2700 * messages to tell when the card is present.
2701 */
2702
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002703 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02002704 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002705
Saugata Dasa5075eb2012-05-17 16:32:21 +05302706 if (mmc_card_mmc(card))
2707 blk_queue_logical_block_size(md->queue.queue,
2708 card->ext_csd.data_sector_size);
2709 else
2710 blk_queue_logical_block_size(md->queue.queue, 512);
2711
Andrei Warkentin371a6892011-04-11 18:10:25 -05002712 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002713
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002714 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02002715 if ((mmc_card_mmc(card) &&
2716 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002717 (mmc_card_sd(card) &&
2718 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2719 md->flags |= MMC_BLK_CMD23;
2720 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002721
2722 if (mmc_card_mmc(card) &&
2723 md->flags & MMC_BLK_CMD23 &&
2724 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2725 card->ext_csd.rel_sectors)) {
2726 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06002727 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002728 }
2729
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002730 if (mmc_card_mmc(card) &&
2731 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2732 (md->flags & MMC_BLK_CMD23) &&
2733 card->ext_csd.packed_event_en) {
2734 if (!mmc_packed_init(&md->queue, card))
2735 md->flags |= MMC_BLK_PACKED_CMD;
2736 }
2737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002739
2740 err_putdisk:
2741 put_disk(md->disk);
2742 err_kfree:
2743 kfree(md);
2744 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002745 spin_lock(&mmc_blk_lock);
2746 ida_remove(&mmc_blk_ida, devidx);
2747 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00002748 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749}
2750
Andrei Warkentin371a6892011-04-11 18:10:25 -05002751static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2752{
2753 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002754
2755 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2756 /*
2757 * The EXT_CSD sector count is in number or 512 byte
2758 * sectors.
2759 */
2760 size = card->ext_csd.sectors;
2761 } else {
2762 /*
2763 * The CSD capacity field is in units of read_blkbits.
2764 * set_capacity takes units of 512 bytes.
2765 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002766 size = (typeof(sector_t))card->csd.capacity
2767 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002768 }
2769
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002770 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002771 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002772}
2773
2774static int mmc_blk_alloc_part(struct mmc_card *card,
2775 struct mmc_blk_data *md,
2776 unsigned int part_type,
2777 sector_t size,
2778 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002779 const char *subname,
2780 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002781{
2782 char cap_str[10];
2783 struct mmc_blk_data *part_md;
2784
2785 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002786 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002787 if (IS_ERR(part_md))
2788 return PTR_ERR(part_md);
2789 part_md->part_type = part_type;
2790 list_add(&part_md->part, &md->part);
2791
James Bottomleyb9f28d82015-03-05 18:47:01 -08002792 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002793 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302794 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002795 part_md->disk->disk_name, mmc_card_id(card),
2796 mmc_card_name(card), part_md->part_type, cap_str);
2797 return 0;
2798}
2799
Namjae Jeone0c368d2011-10-06 23:41:38 +09002800/* MMC Physical partitions consist of two boot partitions and
2801 * up to four general purpose partitions.
2802 * For each partition enabled in EXT_CSD a block device will be allocatedi
2803 * to provide access to the partition.
2804 */
2805
Andrei Warkentin371a6892011-04-11 18:10:25 -05002806static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2807{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002808 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002809
2810 if (!mmc_card_mmc(card))
2811 return 0;
2812
Namjae Jeone0c368d2011-10-06 23:41:38 +09002813 for (idx = 0; idx < card->nr_parts; idx++) {
2814 if (card->part[idx].size) {
2815 ret = mmc_blk_alloc_part(card, md,
2816 card->part[idx].part_cfg,
2817 card->part[idx].size >> 9,
2818 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002819 card->part[idx].name,
2820 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002821 if (ret)
2822 return ret;
2823 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002824 }
2825
2826 return ret;
2827}
2828
Andrei Warkentin371a6892011-04-11 18:10:25 -05002829static void mmc_blk_remove_req(struct mmc_blk_data *md)
2830{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002831 struct mmc_card *card;
2832
Andrei Warkentin371a6892011-04-11 18:10:25 -05002833 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002834 /*
2835 * Flush remaining requests and free queues. It
2836 * is freeing the queue that stops new requests
2837 * from being accepted.
2838 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002839 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002840 mmc_cleanup_queue(&md->queue);
2841 if (md->flags & MMC_BLK_PACKED_CMD)
2842 mmc_packed_clean(&md->queue);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002843 device_remove_file(disk_to_dev(md->disk),
2844 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002845 if (md->disk->flags & GENHD_FL_UP) {
2846 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002847 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2848 card->ext_csd.boot_ro_lockable)
2849 device_remove_file(disk_to_dev(md->disk),
2850 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08002851#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2852 device_remove_file(disk_to_dev(md->disk),
2853 &dev_attr_max_write_speed);
2854 device_remove_file(disk_to_dev(md->disk),
2855 &dev_attr_max_read_speed);
2856 device_remove_file(disk_to_dev(md->disk),
2857 &dev_attr_cache_size);
2858#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002859
Andrei Warkentin371a6892011-04-11 18:10:25 -05002860 del_gendisk(md->disk);
2861 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002862 mmc_blk_put(md);
2863 }
2864}
2865
2866static void mmc_blk_remove_parts(struct mmc_card *card,
2867 struct mmc_blk_data *md)
2868{
2869 struct list_head *pos, *q;
2870 struct mmc_blk_data *part_md;
2871
2872 list_for_each_safe(pos, q, &md->part) {
2873 part_md = list_entry(pos, struct mmc_blk_data, part);
2874 list_del(pos);
2875 mmc_blk_remove_req(part_md);
2876 }
2877}
2878
2879static int mmc_add_disk(struct mmc_blk_data *md)
2880{
2881 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002882 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002883
Dan Williams307d8e62016-06-20 10:40:44 -07002884 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002885 md->force_ro.show = force_ro_show;
2886 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302887 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002888 md->force_ro.attr.name = "force_ro";
2889 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2890 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2891 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002892 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002893#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2894 atomic_set(&md->queue.max_write_speed, max_write_speed);
2895 ret = device_create_file(disk_to_dev(md->disk),
2896 &dev_attr_max_write_speed);
2897 if (ret)
2898 goto max_write_speed_fail;
2899 atomic_set(&md->queue.max_read_speed, max_read_speed);
2900 ret = device_create_file(disk_to_dev(md->disk),
2901 &dev_attr_max_read_speed);
2902 if (ret)
2903 goto max_read_speed_fail;
2904 atomic_set(&md->queue.cache_size, cache_size);
2905 atomic_long_set(&md->queue.cache_used, 0);
2906 md->queue.cache_jiffies = jiffies;
2907 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
2908 if (ret)
2909 goto cache_size_fail;
2910#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01002911
2912 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2913 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002914 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002915
2916 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2917 mode = S_IRUGO;
2918 else
2919 mode = S_IRUGO | S_IWUSR;
2920
2921 md->power_ro_lock.show = power_ro_lock_show;
2922 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002923 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002924 md->power_ro_lock.attr.mode = mode;
2925 md->power_ro_lock.attr.name =
2926 "ro_lock_until_next_power_on";
2927 ret = device_create_file(disk_to_dev(md->disk),
2928 &md->power_ro_lock);
2929 if (ret)
2930 goto power_ro_lock_fail;
2931 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002932
2933 md->num_wr_reqs_to_start_packing.show =
2934 num_wr_reqs_to_start_packing_show;
2935 md->num_wr_reqs_to_start_packing.store =
2936 num_wr_reqs_to_start_packing_store;
2937 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2938 md->num_wr_reqs_to_start_packing.attr.name =
2939 "num_wr_reqs_to_start_packing";
2940 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2941 ret = device_create_file(disk_to_dev(md->disk),
2942 &md->num_wr_reqs_to_start_packing);
2943 if (ret)
2944 goto power_ro_lock_fail;
2945
Johan Rudholmadd710e2011-12-02 08:51:06 +01002946 return ret;
2947
2948power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08002949#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2950 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
2951cache_size_fail:
2952 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
2953max_read_speed_fail:
2954 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
2955max_write_speed_fail:
2956#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01002957 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2958force_ro_fail:
2959 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002960
2961 return ret;
2962}
2963
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002964static const struct mmc_fixup blk_fixups[] =
2965{
Chris Ballc59d4472011-11-11 22:01:43 -05002966 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2967 MMC_QUIRK_INAND_CMD38),
2968 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2969 MMC_QUIRK_INAND_CMD38),
2970 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2971 MMC_QUIRK_INAND_CMD38),
2972 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2973 MMC_QUIRK_INAND_CMD38),
2974 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2975 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002976
2977 /*
2978 * Some MMC cards experience performance degradation with CMD23
2979 * instead of CMD12-bounded multiblock transfers. For now we'll
2980 * black list what's bad...
2981 * - Certain Toshiba cards.
2982 *
2983 * N.B. This doesn't affect SD cards.
2984 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08002985 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2986 MMC_QUIRK_BLK_NO_CMD23),
2987 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2988 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002989 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002990 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002991 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002992 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002993 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002994 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002995
2996 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03002997 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002998 */
Chris Ballc59d4472011-11-11 22:01:43 -05002999 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003000 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03003001 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
3002 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003003
Ian Chen3550ccd2012-08-29 15:05:36 +09003004 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08003005 * Some Samsung MMC cards need longer data read timeout than
3006 * indicated in CSD.
3007 */
3008 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
3009 MMC_QUIRK_LONG_READ_TIME),
3010
3011 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09003012 * On these Samsung MoviNAND parts, performing secure erase or
3013 * secure trim can result in unrecoverable corruption due to a
3014 * firmware bug.
3015 */
3016 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3017 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3018 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3019 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3020 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3021 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3022 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3023 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3024 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3025 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3026 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3027 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3028 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3029 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3030 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3031 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3032
Shawn Linb5b4ff02015-08-12 13:08:32 +08003033 /*
3034 * On Some Kingston eMMCs, performing trim can result in
3035 * unrecoverable data conrruption occasionally due to a firmware bug.
3036 */
3037 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3038 MMC_QUIRK_TRIM_BROKEN),
3039 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3040 MMC_QUIRK_TRIM_BROKEN),
3041
Pratibhasagar V8d664e32014-12-03 18:26:42 +02003042 /* Some INAND MCP devices advertise incorrect timeout values */
3043 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
3044 MMC_QUIRK_INAND_DATA_TIMEOUT),
3045
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003046 END_FIXUP
3047};
3048
Ulf Hansson96541ba2015-04-14 13:06:12 +02003049static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003051 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003052 char cap_str[10];
3053
Pierre Ossman912490d2005-05-21 10:27:02 +01003054 /*
3055 * Check that the card supports the command class(es) we need.
3056 */
3057 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 return -ENODEV;
3059
Lukas Czerner5204d002014-06-18 13:18:07 +02003060 mmc_fixup_device(card, blk_fixups);
3061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 md = mmc_blk_alloc(card);
3063 if (IS_ERR(md))
3064 return PTR_ERR(md);
3065
James Bottomleyb9f28d82015-03-05 18:47:01 -08003066 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003067 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303068 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003070 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Andrei Warkentin371a6892011-04-11 18:10:25 -05003072 if (mmc_blk_alloc_parts(card, md))
3073 goto out;
3074
Ulf Hansson96541ba2015-04-14 13:06:12 +02003075 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003076
Andrei Warkentin371a6892011-04-11 18:10:25 -05003077 if (mmc_add_disk(md))
3078 goto out;
3079
3080 list_for_each_entry(part_md, &md->part, part) {
3081 if (mmc_add_disk(part_md))
3082 goto out;
3083 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003084
3085 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
3086 pm_runtime_use_autosuspend(&card->dev);
3087
3088 /*
3089 * Don't enable runtime PM for SD-combo cards here. Leave that
3090 * decision to be taken during the SDIO init sequence instead.
3091 */
3092 if (card->type != MMC_TYPE_SD_COMBO) {
3093 pm_runtime_set_active(&card->dev);
3094 pm_runtime_enable(&card->dev);
3095 }
3096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 return 0;
3098
3099 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05003100 mmc_blk_remove_parts(card, md);
3101 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01003102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103}
3104
Ulf Hansson96541ba2015-04-14 13:06:12 +02003105static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003107 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Andrei Warkentin371a6892011-04-11 18:10:25 -05003109 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003110 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003111 mmc_claim_host(card->host);
3112 mmc_blk_part_switch(card, md);
3113 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003114 if (card->type != MMC_TYPE_SD_COMBO)
3115 pm_runtime_disable(&card->dev);
3116 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003117 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02003118 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119}
3120
Ulf Hansson96541ba2015-04-14 13:06:12 +02003121static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003123 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02003124 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303125 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
3127 if (md) {
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303128 rc = mmc_queue_suspend(&md->queue);
3129 if (rc)
3130 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003131 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303132 rc = mmc_queue_suspend(&part_md->queue);
3133 if (rc)
3134 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303137 goto out;
3138
3139 out_resume:
3140 mmc_queue_resume(&md->queue);
3141 list_for_each_entry(part_md, &md->part, part) {
3142 mmc_queue_resume(&part_md->queue);
3143 }
3144 out:
3145 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146}
3147
Ulf Hansson96541ba2015-04-14 13:06:12 +02003148static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02003149{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003150 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003151}
3152
Ulf Hansson0967edc2014-10-06 11:29:42 +02003153#ifdef CONFIG_PM_SLEEP
3154static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02003155{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003156 struct mmc_card *card = mmc_dev_to_card(dev);
3157
3158 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003159}
3160
Ulf Hansson0967edc2014-10-06 11:29:42 +02003161static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003163 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02003164 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
3166 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003167 /*
3168 * Resume involves the card going into idle state,
3169 * so current partition is always the main one.
3170 */
3171 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003173 list_for_each_entry(part_md, &md->part, part) {
3174 mmc_queue_resume(&part_md->queue);
3175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 }
3177 return 0;
3178}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179#endif
3180
Ulf Hansson0967edc2014-10-06 11:29:42 +02003181static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3182
Ulf Hansson96541ba2015-04-14 13:06:12 +02003183static struct mmc_driver mmc_driver = {
3184 .drv = {
3185 .name = "mmcblk",
3186 .pm = &mmc_blk_pm_ops,
3187 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 .probe = mmc_blk_probe,
3189 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02003190 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191};
3192
3193static int __init mmc_blk_init(void)
3194{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003195 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003197 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3198 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3199
Ben Hutchingsa26eba62014-11-06 03:35:09 +00003200 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003201
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003202 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3203 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003206 res = mmc_register_driver(&mmc_driver);
3207 if (res)
3208 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003210 return 0;
3211 out2:
3212 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 out:
3214 return res;
3215}
3216
3217static void __exit mmc_blk_exit(void)
3218{
3219 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003220 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
3223module_init(mmc_blk_init);
3224module_exit(mmc_blk_exit);
3225
3226MODULE_LICENSE("GPL");
3227MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3228