blob: c568859c2be664cc6c586f028c8cf881fb13cd97 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020037#include <linux/pm_runtime.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020038#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
John Calixtocb87ea22011-04-26 18:56:29 -040040#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020042#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010043#include <linux/mmc/mmc.h>
44#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/uaccess.h>
47
Pierre Ossman98ac2162006-12-23 20:03:02 +010048#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080049#include "block.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000051MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040052#ifdef MODULE_PARAM_PREFIX
53#undef MODULE_PARAM_PREFIX
54#endif
55#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010056
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050057#define INAND_CMD38_ARG_EXT_CSD 113
58#define INAND_CMD38_ARG_ERASE 0x00
59#define INAND_CMD38_ARG_TRIM 0x01
60#define INAND_CMD38_ARG_SECERASE 0x80
61#define INAND_CMD38_ARG_SECTRIM1 0x81
62#define INAND_CMD38_ARG_SECTRIM2 0x88
Subhash Jadavani2fbab612014-12-04 15:16:17 +020063#define MMC_BLK_TIMEOUT_MS (30 * 1000) /* 30 sec timeout */
Maya Erez775a9362013-04-18 15:41:55 +030064#define MMC_SANITIZE_REQ_TIMEOUT 240000
65#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050066
Luca Porziod3df0462015-11-06 15:12:26 +000067#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090068 (rq_data_dir(req) == WRITE))
69#define PACKED_CMD_VER 0x01
70#define PACKED_CMD_WR 0x02
71
Tatyana Brokhman08238ce2012-10-07 10:33:13 +020072#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
73 do { \
74 if (stats->enabled) \
75 stats->pack_stop_reason[reason]++; \
76 } while (0)
77
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020078static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040079
80/*
81 * The defaults come from config options but can be overriden by module
82 * or bootarg options.
83 */
84static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
85
86/*
87 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000088 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020089 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040090 */
91static int max_devices;
92
Ben Hutchingsa26eba62014-11-06 03:35:09 +000093#define MAX_DEVICES 256
94
Ulf Hanssonb10fa992016-04-07 14:36:46 +020095static DEFINE_IDA(mmc_blk_ida);
96static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
99 * There is one mmc_blk_data per slot.
100 */
101struct mmc_blk_data {
102 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -0700103 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct gendisk *disk;
105 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500106 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500108 unsigned int flags;
109#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
110#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900111#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000114 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500115 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300116 unsigned int reset_done;
117#define MMC_BLK_READ BIT(0)
118#define MMC_BLK_WRITE BIT(1)
119#define MMC_BLK_DISCARD BIT(2)
120#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500121
122 /*
123 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200124 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500125 * track of the current selected device partition.
126 */
127 unsigned int part_curr;
128 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100129 struct device_attribute power_ro_lock;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200130 struct device_attribute num_wr_reqs_to_start_packing;
Maya Erez5a8dae12014-12-04 15:13:59 +0200131 struct device_attribute no_pack_for_random;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100132 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
Arjan van de Vena621aae2006-01-12 18:43:35 +0000135static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900137enum {
138 MMC_PACKED_NR_IDX = -1,
139 MMC_PACKED_NR_ZERO,
140 MMC_PACKED_NR_SINGLE,
141};
142
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400143module_param(perdev_minors, int, 0444);
144MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
145
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200146static inline int mmc_blk_part_switch(struct mmc_card *card,
147 struct mmc_blk_data *md);
148static int get_card_status(struct mmc_card *card, u32 *status, int retries);
149
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900150static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
151{
152 struct mmc_packed *packed = mqrq->packed;
153
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900154 mqrq->cmd_type = MMC_PACKED_NONE;
155 packed->nr_entries = MMC_PACKED_NR_ZERO;
156 packed->idx_failure = MMC_PACKED_NR_IDX;
157 packed->retries = 0;
158 packed->blocks = 0;
159}
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
162{
163 struct mmc_blk_data *md;
164
Arjan van de Vena621aae2006-01-12 18:43:35 +0000165 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 md = disk->private_data;
167 if (md && md->usage == 0)
168 md = NULL;
169 if (md)
170 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000171 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 return md;
174}
175
Andrei Warkentin371a6892011-04-11 18:10:25 -0500176static inline int mmc_get_devidx(struct gendisk *disk)
177{
Colin Cross382c55f2015-10-22 10:00:41 -0700178 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500179 return devidx;
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static void mmc_blk_put(struct mmc_blk_data *md)
183{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000184 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 md->usage--;
186 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500187 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800188 blk_cleanup_queue(md->queue.queue);
189
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200190 spin_lock(&mmc_blk_lock);
191 ida_remove(&mmc_blk_ida, devidx);
192 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 kfree(md);
196 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000197 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Johan Rudholmadd710e2011-12-02 08:51:06 +0100200static ssize_t power_ro_lock_show(struct device *dev,
201 struct device_attribute *attr, char *buf)
202{
203 int ret;
204 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
205 struct mmc_card *card = md->queue.card;
206 int locked = 0;
207
208 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
209 locked = 2;
210 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
211 locked = 1;
212
213 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
214
Tomas Winkler9098f842015-07-16 15:50:45 +0200215 mmc_blk_put(md);
216
Johan Rudholmadd710e2011-12-02 08:51:06 +0100217 return ret;
218}
219
220static ssize_t power_ro_lock_store(struct device *dev,
221 struct device_attribute *attr, const char *buf, size_t count)
222{
223 int ret;
224 struct mmc_blk_data *md, *part_md;
225 struct mmc_card *card;
226 unsigned long set;
227
228 if (kstrtoul(buf, 0, &set))
229 return -EINVAL;
230
231 if (set != 1)
232 return count;
233
234 md = mmc_blk_get(dev_to_disk(dev));
235 card = md->queue.card;
236
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200237 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100238
239 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
240 card->ext_csd.boot_ro_lock |
241 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
242 card->ext_csd.part_time);
243 if (ret)
244 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
245 else
246 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
247
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200248 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100249
250 if (!ret) {
251 pr_info("%s: Locking boot partition ro until next power on\n",
252 md->disk->disk_name);
253 set_disk_ro(md->disk, 1);
254
255 list_for_each_entry(part_md, &md->part, part)
256 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
257 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
258 set_disk_ro(part_md->disk, 1);
259 }
260 }
261
262 mmc_blk_put(md);
263 return count;
264}
265
Andrei Warkentin371a6892011-04-11 18:10:25 -0500266static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
267 char *buf)
268{
269 int ret;
270 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
271
Baruch Siach0031a982014-09-22 10:12:51 +0300272 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500273 get_disk_ro(dev_to_disk(dev)) ^
274 md->read_only);
275 mmc_blk_put(md);
276 return ret;
277}
278
279static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
280 const char *buf, size_t count)
281{
282 int ret;
283 char *end;
284 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
285 unsigned long set = simple_strtoul(buf, &end, 0);
286 if (end == buf) {
287 ret = -EINVAL;
288 goto out;
289 }
290
291 set_disk_ro(dev_to_disk(dev), set || md->read_only);
292 ret = count;
293out:
294 mmc_blk_put(md);
295 return ret;
296}
297
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200298static ssize_t
Maya Erez5a8dae12014-12-04 15:13:59 +0200299no_pack_for_random_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
301{
302 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
303 int ret;
304
305 ret = snprintf(buf, PAGE_SIZE, "%d\n", md->queue.no_pack_for_random);
306
307 mmc_blk_put(md);
308 return ret;
309}
310
311static ssize_t
312no_pack_for_random_store(struct device *dev,
313 struct device_attribute *attr,
314 const char *buf, size_t count)
315{
316 int value;
317 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
318 struct mmc_card *card = md->queue.card;
319 int ret = count;
320
321 if (!card) {
322 ret = -EINVAL;
323 goto exit;
324 }
325
326 sscanf(buf, "%d", &value);
327
328 if (value < 0) {
329 pr_err("%s: value %d is not valid. old value remains = %d",
330 mmc_hostname(card->host), value,
331 md->queue.no_pack_for_random);
332 ret = -EINVAL;
333 goto exit;
334 }
335
336 md->queue.no_pack_for_random = (value > 0) ? true : false;
337
338 pr_debug("%s: no_pack_for_random: new value = %d",
339 mmc_hostname(card->host),
340 md->queue.no_pack_for_random);
341
342exit:
343 mmc_blk_put(md);
344 return ret;
345}
346
347static ssize_t
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200348num_wr_reqs_to_start_packing_show(struct device *dev,
349 struct device_attribute *attr, char *buf)
350{
351 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
352 int num_wr_reqs_to_start_packing;
353 int ret;
354
355 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
356
357 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
358
359 mmc_blk_put(md);
360 return ret;
361}
362
363static ssize_t
364num_wr_reqs_to_start_packing_store(struct device *dev,
365 struct device_attribute *attr,
366 const char *buf, size_t count)
367{
368 int value;
369 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Yaniv Gardi42399822014-12-04 00:26:23 +0200370 struct mmc_card *card = md->queue.card;
371 int ret = count;
372
373 if (!card) {
374 ret = -EINVAL;
375 goto exit;
376 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200377
378 sscanf(buf, "%d", &value);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200379
Yaniv Gardi42399822014-12-04 00:26:23 +0200380 if (value >= 0) {
381 md->queue.num_wr_reqs_to_start_packing =
382 min_t(int, value, (int)card->ext_csd.max_packed_writes);
383
384 pr_debug("%s: trigger to pack: new value = %d",
385 mmc_hostname(card->host),
386 md->queue.num_wr_reqs_to_start_packing);
387 } else {
388 pr_err("%s: value %d is not valid. old value remains = %d",
389 mmc_hostname(card->host), value,
390 md->queue.num_wr_reqs_to_start_packing);
391 ret = -EINVAL;
392 }
393
394exit:
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200395 mmc_blk_put(md);
Yaniv Gardi42399822014-12-04 00:26:23 +0200396 return ret;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200397}
398
Mark Salyzyn6904e432016-01-28 11:12:25 -0800399#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
400
401static int max_read_speed, max_write_speed, cache_size = 4;
402
403module_param(max_read_speed, int, S_IRUSR | S_IRGRP);
404MODULE_PARM_DESC(max_read_speed, "maximum KB/s read speed 0=off");
405module_param(max_write_speed, int, S_IRUSR | S_IRGRP);
406MODULE_PARM_DESC(max_write_speed, "maximum KB/s write speed 0=off");
407module_param(cache_size, int, S_IRUSR | S_IRGRP);
408MODULE_PARM_DESC(cache_size, "MB high speed memory or SLC cache");
409
410/*
411 * helper macros and expectations:
412 * size - unsigned long number of bytes
413 * jiffies - unsigned long HZ timestamp difference
414 * speed - unsigned KB/s transfer rate
415 */
416#define size_and_speed_to_jiffies(size, speed) \
417 ((size) * HZ / (speed) / 1024UL)
418#define jiffies_and_speed_to_size(jiffies, speed) \
419 (((speed) * (jiffies) * 1024UL) / HZ)
420#define jiffies_and_size_to_speed(jiffies, size) \
421 ((size) * HZ / (jiffies) / 1024UL)
422
423/* Limits to report warning */
424/* jiffies_and_size_to_speed(10*HZ, queue_max_hw_sectors(q) * 512UL) ~ 25 */
425#define MIN_SPEED(q) 250 /* 10 times faster than a floppy disk */
426#define MAX_SPEED(q) jiffies_and_size_to_speed(1, queue_max_sectors(q) * 512UL)
427
428#define speed_valid(speed) ((speed) > 0)
429
430static const char off[] = "off\n";
431
432static int max_speed_show(int speed, char *buf)
433{
434 if (speed)
435 return scnprintf(buf, PAGE_SIZE, "%uKB/s\n", speed);
436 else
437 return scnprintf(buf, PAGE_SIZE, off);
438}
439
440static int max_speed_store(const char *buf, struct request_queue *q)
441{
442 unsigned int limit, set = 0;
443
444 if (!strncasecmp(off, buf, sizeof(off) - 2))
445 return set;
446 if (kstrtouint(buf, 0, &set) || (set > INT_MAX))
447 return -EINVAL;
448 if (set == 0)
449 return set;
450 limit = MAX_SPEED(q);
451 if (set > limit)
452 pr_warn("max speed %u ineffective above %u\n", set, limit);
453 limit = MIN_SPEED(q);
454 if (set < limit)
455 pr_warn("max speed %u painful below %u\n", set, limit);
456 return set;
457}
458
459static ssize_t max_write_speed_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
461{
462 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
463 int ret = max_speed_show(atomic_read(&md->queue.max_write_speed), buf);
464
465 mmc_blk_put(md);
466 return ret;
467}
468
469static ssize_t max_write_speed_store(struct device *dev,
470 struct device_attribute *attr,
471 const char *buf, size_t count)
472{
473 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
474 int set = max_speed_store(buf, md->queue.queue);
475
476 if (set < 0) {
477 mmc_blk_put(md);
478 return set;
479 }
480
481 atomic_set(&md->queue.max_write_speed, set);
482 mmc_blk_put(md);
483 return count;
484}
485
486static const DEVICE_ATTR(max_write_speed, S_IRUGO | S_IWUSR,
487 max_write_speed_show, max_write_speed_store);
488
489static ssize_t max_read_speed_show(struct device *dev,
490 struct device_attribute *attr, char *buf)
491{
492 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
493 int ret = max_speed_show(atomic_read(&md->queue.max_read_speed), buf);
494
495 mmc_blk_put(md);
496 return ret;
497}
498
499static ssize_t max_read_speed_store(struct device *dev,
500 struct device_attribute *attr,
501 const char *buf, size_t count)
502{
503 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
504 int set = max_speed_store(buf, md->queue.queue);
505
506 if (set < 0) {
507 mmc_blk_put(md);
508 return set;
509 }
510
511 atomic_set(&md->queue.max_read_speed, set);
512 mmc_blk_put(md);
513 return count;
514}
515
516static const DEVICE_ATTR(max_read_speed, S_IRUGO | S_IWUSR,
517 max_read_speed_show, max_read_speed_store);
518
519static ssize_t cache_size_show(struct device *dev,
520 struct device_attribute *attr, char *buf)
521{
522 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
523 struct mmc_queue *mq = &md->queue;
524 int cache_size = atomic_read(&mq->cache_size);
525 int ret;
526
527 if (!cache_size)
528 ret = scnprintf(buf, PAGE_SIZE, off);
529 else {
530 int speed = atomic_read(&mq->max_write_speed);
531
532 if (!speed_valid(speed))
533 ret = scnprintf(buf, PAGE_SIZE, "%uMB\n", cache_size);
534 else { /* We accept race between cache_jiffies and cache_used */
535 unsigned long size = jiffies_and_speed_to_size(
536 jiffies - mq->cache_jiffies, speed);
537 long used = atomic_long_read(&mq->cache_used);
538
539 if (size >= used)
540 size = 0;
541 else
542 size = (used - size) * 100 / cache_size
543 / 1024UL / 1024UL;
544
545 ret = scnprintf(buf, PAGE_SIZE, "%uMB %lu%% used\n",
546 cache_size, size);
547 }
548 }
549
550 mmc_blk_put(md);
551 return ret;
552}
553
554static ssize_t cache_size_store(struct device *dev,
555 struct device_attribute *attr,
556 const char *buf, size_t count)
557{
558 struct mmc_blk_data *md;
559 unsigned int set = 0;
560
561 if (strncasecmp(off, buf, sizeof(off) - 2)
562 && (kstrtouint(buf, 0, &set) || (set > INT_MAX)))
563 return -EINVAL;
564
565 md = mmc_blk_get(dev_to_disk(dev));
566 atomic_set(&md->queue.cache_size, set);
567 mmc_blk_put(md);
568 return count;
569}
570
571static const DEVICE_ATTR(cache_size, S_IRUGO | S_IWUSR,
572 cache_size_show, cache_size_store);
573
574/* correct for write-back */
575static long mmc_blk_cache_used(struct mmc_queue *mq, unsigned long waitfor)
576{
577 long used = 0;
578 int speed = atomic_read(&mq->max_write_speed);
579
580 if (speed_valid(speed)) {
581 unsigned long size = jiffies_and_speed_to_size(
582 waitfor - mq->cache_jiffies, speed);
583 used = atomic_long_read(&mq->cache_used);
584
585 if (size >= used)
586 used = 0;
587 else
588 used -= size;
589 }
590
591 atomic_long_set(&mq->cache_used, used);
592 mq->cache_jiffies = waitfor;
593
594 return used;
595}
596
597static void mmc_blk_simulate_delay(
598 struct mmc_queue *mq,
599 struct request *req,
600 unsigned long waitfor)
601{
602 int max_speed;
603
604 if (!req)
605 return;
606
607 max_speed = (rq_data_dir(req) == READ)
608 ? atomic_read(&mq->max_read_speed)
609 : atomic_read(&mq->max_write_speed);
610 if (speed_valid(max_speed)) {
611 unsigned long bytes = blk_rq_bytes(req);
612
613 if (rq_data_dir(req) != READ) {
614 int cache_size = atomic_read(&mq->cache_size);
615
616 if (cache_size) {
617 unsigned long size = cache_size * 1024L * 1024L;
618 long used = mmc_blk_cache_used(mq, waitfor);
619
620 used += bytes;
621 atomic_long_set(&mq->cache_used, used);
622 bytes = 0;
623 if (used > size)
624 bytes = used - size;
625 }
626 }
627 waitfor += size_and_speed_to_jiffies(bytes, max_speed);
628 if (time_is_after_jiffies(waitfor)) {
629 long msecs = jiffies_to_msecs(waitfor - jiffies);
630
631 if (likely(msecs > 0))
632 msleep(msecs);
633 }
634 }
635}
636
637#else
638
639#define mmc_blk_simulate_delay(mq, req, waitfor)
640
641#endif
642
Al Viroa5a15612008-03-02 10:33:30 -0500643static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Al Viroa5a15612008-03-02 10:33:30 -0500645 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int ret = -ENXIO;
647
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200648 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (md) {
650 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500651 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700653
Al Viroa5a15612008-03-02 10:33:30 -0500654 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700655 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700656 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200659 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 return ret;
662}
663
Al Virodb2a1442013-05-05 21:52:57 -0400664static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Al Viroa5a15612008-03-02 10:33:30 -0500666 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200668 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200670 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800674mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800676 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
677 geo->heads = 4;
678 geo->sectors = 16;
679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
John Calixtocb87ea22011-04-26 18:56:29 -0400682struct mmc_blk_ioc_data {
683 struct mmc_ioc_cmd ic;
684 unsigned char *buf;
685 u64 buf_bytes;
686};
687
688static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
689 struct mmc_ioc_cmd __user *user)
690{
691 struct mmc_blk_ioc_data *idata;
692 int err;
693
yalin wang1ff89502015-11-12 19:27:11 +0800694 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400695 if (!idata) {
696 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400697 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400698 }
699
700 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
701 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400702 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400703 }
704
705 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
706 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
707 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400708 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400709 }
710
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300711 if (!idata->buf_bytes) {
712 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100713 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300714 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100715
yalin wang1ff89502015-11-12 19:27:11 +0800716 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400717 if (!idata->buf) {
718 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400719 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400720 }
721
722 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
723 idata->ic.data_ptr, idata->buf_bytes)) {
724 err = -EFAULT;
725 goto copy_err;
726 }
727
728 return idata;
729
730copy_err:
731 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400732idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400733 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400734out:
John Calixtocb87ea22011-04-26 18:56:29 -0400735 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400736}
737
Jon Huntera5f57742015-09-22 10:27:53 +0100738static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
739 struct mmc_blk_ioc_data *idata)
740{
741 struct mmc_ioc_cmd *ic = &idata->ic;
742
743 if (copy_to_user(&(ic_ptr->response), ic->response,
744 sizeof(ic->response)))
745 return -EFAULT;
746
747 if (!idata->ic.write_flag) {
748 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
749 idata->buf, idata->buf_bytes))
750 return -EFAULT;
751 }
752
753 return 0;
754}
755
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200756static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
757 u32 retries_max)
758{
759 int err;
760 u32 retry_count = 0;
761
762 if (!status || !retries_max)
763 return -EINVAL;
764
765 do {
766 err = get_card_status(card, status, 5);
767 if (err)
768 break;
769
770 if (!R1_STATUS(*status) &&
771 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
772 break; /* RPMB programming operation complete */
773
774 /*
775 * Rechedule to give the MMC device a chance to continue
776 * processing the previous command without being polled too
777 * frequently.
778 */
779 usleep_range(1000, 5000);
780 } while (++retry_count < retries_max);
781
782 if (retry_count == retries_max)
783 err = -EPERM;
784
785 return err;
786}
787
Maya Erez775a9362013-04-18 15:41:55 +0300788static int ioctl_do_sanitize(struct mmc_card *card)
789{
790 int err;
791
Ulf Hanssona2d10862013-12-16 14:37:26 +0100792 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300793 pr_warn("%s: %s - SANITIZE is not supported\n",
794 mmc_hostname(card->host), __func__);
795 err = -EOPNOTSUPP;
796 goto out;
797 }
798
799 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
800 mmc_hostname(card->host), __func__);
801
802 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
803 EXT_CSD_SANITIZE_START, 1,
804 MMC_SANITIZE_REQ_TIMEOUT);
805
806 if (err)
807 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
808 mmc_hostname(card->host), __func__, err);
809
810 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
811 __func__);
812out:
813 return err;
814}
815
Jon Huntera5f57742015-09-22 10:27:53 +0100816static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
817 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400818{
John Calixtocb87ea22011-04-26 18:56:29 -0400819 struct mmc_command cmd = {0};
820 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530821 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400822 struct scatterlist sg;
823 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200824 int is_rpmb = false;
825 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400826
Jon Huntera5f57742015-09-22 10:27:53 +0100827 if (!card || !md || !idata)
828 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400829
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200830 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
831 is_rpmb = true;
832
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100833 cmd.opcode = idata->ic.opcode;
834 cmd.arg = idata->ic.arg;
835 cmd.flags = idata->ic.flags;
836
837 if (idata->buf_bytes) {
838 data.sg = &sg;
839 data.sg_len = 1;
840 data.blksz = idata->ic.blksz;
841 data.blocks = idata->ic.blocks;
842
843 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
844
845 if (idata->ic.write_flag)
846 data.flags = MMC_DATA_WRITE;
847 else
848 data.flags = MMC_DATA_READ;
849
850 /* data.flags must already be set before doing this. */
851 mmc_set_data_timeout(&data, card);
852
853 /* Allow overriding the timeout_ns for empirical tuning. */
854 if (idata->ic.data_timeout_ns)
855 data.timeout_ns = idata->ic.data_timeout_ns;
856
857 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
858 /*
859 * Pretend this is a data transfer and rely on the
860 * host driver to compute timeout. When all host
861 * drivers support cmd.cmd_timeout for R1B, this
862 * can be changed to:
863 *
864 * mrq.data = NULL;
865 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
866 */
867 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
868 }
869
870 mrq.data = &data;
871 }
872
873 mrq.cmd = &cmd;
874
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200875 err = mmc_blk_part_switch(card, md);
876 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100877 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200878
John Calixtocb87ea22011-04-26 18:56:29 -0400879 if (idata->ic.is_acmd) {
880 err = mmc_app_cmd(card->host, card);
881 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100882 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400883 }
884
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200885 if (is_rpmb) {
886 err = mmc_set_blockcount(card, data.blocks,
887 idata->ic.write_flag & (1 << 31));
888 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100889 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200890 }
891
Yaniv Gardia82e4842013-06-05 14:13:08 +0300892 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
893 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300894 err = ioctl_do_sanitize(card);
895
896 if (err)
897 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
898 __func__, err);
899
Jon Huntera5f57742015-09-22 10:27:53 +0100900 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300901 }
902
John Calixtocb87ea22011-04-26 18:56:29 -0400903 mmc_wait_for_req(card->host, &mrq);
904
905 if (cmd.error) {
906 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
907 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100908 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400909 }
910 if (data.error) {
911 dev_err(mmc_dev(card->host), "%s: data error %d\n",
912 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100913 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400914 }
915
916 /*
917 * According to the SD specs, some commands require a delay after
918 * issuing the command.
919 */
920 if (idata->ic.postsleep_min_us)
921 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
922
Jon Huntera5f57742015-09-22 10:27:53 +0100923 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400924
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200925 if (is_rpmb) {
926 /*
927 * Ensure RPMB command has completed by polling CMD13
928 * "Send Status".
929 */
930 err = ioctl_rpmb_card_status_poll(card, &status, 5);
931 if (err)
932 dev_err(mmc_dev(card->host),
933 "%s: Card Status=0x%08X, error %d\n",
934 __func__, status, err);
935 }
936
Jon Huntera5f57742015-09-22 10:27:53 +0100937 return err;
938}
939
940static int mmc_blk_ioctl_cmd(struct block_device *bdev,
941 struct mmc_ioc_cmd __user *ic_ptr)
942{
943 struct mmc_blk_ioc_data *idata;
944 struct mmc_blk_data *md;
945 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700946 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100947
Shawn Lin83c742c2016-03-16 18:15:47 +0800948 /*
949 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
950 * whole block device, not on a partition. This prevents overspray
951 * between sibling partitions.
952 */
953 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
954 return -EPERM;
955
Jon Huntera5f57742015-09-22 10:27:53 +0100956 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
957 if (IS_ERR(idata))
958 return PTR_ERR(idata);
959
960 md = mmc_blk_get(bdev->bd_disk);
961 if (!md) {
962 err = -EINVAL;
963 goto cmd_err;
964 }
965
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 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100975
Adrian Hunter3c866562016-05-04 14:38:12 +0300976 /* Always switch back to main area after RPMB access */
977 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
978 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
979
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200980 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400981
Grant Grundlerb0934102015-09-23 18:30:33 -0700982 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100983
John Calixtocb87ea22011-04-26 18:56:29 -0400984cmd_done:
985 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300986cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400987 kfree(idata->buf);
988 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700989 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400990}
991
Jon Huntera5f57742015-09-22 10:27:53 +0100992static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
993 struct mmc_ioc_multi_cmd __user *user)
994{
995 struct mmc_blk_ioc_data **idata = NULL;
996 struct mmc_ioc_cmd __user *cmds = user->cmds;
997 struct mmc_card *card;
998 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -0700999 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001000 __u64 num_of_cmds;
1001
Shawn Lin83c742c2016-03-16 18:15:47 +08001002 /*
1003 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1004 * whole block device, not on a partition. This prevents overspray
1005 * between sibling partitions.
1006 */
1007 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1008 return -EPERM;
1009
Jon Huntera5f57742015-09-22 10:27:53 +01001010 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
1011 sizeof(num_of_cmds)))
1012 return -EFAULT;
1013
1014 if (num_of_cmds > MMC_IOC_MAX_CMDS)
1015 return -EINVAL;
1016
1017 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
1018 if (!idata)
1019 return -ENOMEM;
1020
1021 for (i = 0; i < num_of_cmds; i++) {
1022 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
1023 if (IS_ERR(idata[i])) {
1024 err = PTR_ERR(idata[i]);
1025 num_of_cmds = i;
1026 goto cmd_err;
1027 }
1028 }
1029
1030 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -08001031 if (!md) {
1032 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +01001033 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -08001034 }
Jon Huntera5f57742015-09-22 10:27:53 +01001035
1036 card = md->queue.card;
1037 if (IS_ERR(card)) {
1038 err = PTR_ERR(card);
1039 goto cmd_done;
1040 }
1041
1042 mmc_get_card(card);
1043
Grant Grundlerb0934102015-09-23 18:30:33 -07001044 for (i = 0; i < num_of_cmds && !ioc_err; i++)
1045 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001046
Adrian Hunter3c866562016-05-04 14:38:12 +03001047 /* Always switch back to main area after RPMB access */
1048 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1049 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1050
Jon Huntera5f57742015-09-22 10:27:53 +01001051 mmc_put_card(card);
1052
1053 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -07001054 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +01001055 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001056
1057cmd_done:
1058 mmc_blk_put(md);
1059cmd_err:
1060 for (i = 0; i < num_of_cmds; i++) {
1061 kfree(idata[i]->buf);
1062 kfree(idata[i]);
1063 }
1064 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001065 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +01001066}
1067
John Calixtocb87ea22011-04-26 18:56:29 -04001068static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
1069 unsigned int cmd, unsigned long arg)
1070{
Jon Huntera5f57742015-09-22 10:27:53 +01001071 switch (cmd) {
1072 case MMC_IOC_CMD:
1073 return mmc_blk_ioctl_cmd(bdev,
1074 (struct mmc_ioc_cmd __user *)arg);
1075 case MMC_IOC_MULTI_CMD:
1076 return mmc_blk_ioctl_multi_cmd(bdev,
1077 (struct mmc_ioc_multi_cmd __user *)arg);
1078 default:
1079 return -EINVAL;
1080 }
John Calixtocb87ea22011-04-26 18:56:29 -04001081}
1082
1083#ifdef CONFIG_COMPAT
1084static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
1085 unsigned int cmd, unsigned long arg)
1086{
1087 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
1088}
1089#endif
1090
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001091static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -05001092 .open = mmc_blk_open,
1093 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001094 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -04001096 .ioctl = mmc_blk_ioctl,
1097#ifdef CONFIG_COMPAT
1098 .compat_ioctl = mmc_blk_compat_ioctl,
1099#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100};
1101
Andrei Warkentin371a6892011-04-11 18:10:25 -05001102static inline int mmc_blk_part_switch(struct mmc_card *card,
1103 struct mmc_blk_data *md)
1104{
1105 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001106 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001107
Andrei Warkentin371a6892011-04-11 18:10:25 -05001108 if (main_md->part_curr == md->part_type)
1109 return 0;
1110
1111 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001112 u8 part_config = card->ext_csd.part_config;
1113
Adrian Hunter57da0c02016-05-04 14:38:13 +03001114 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1115 mmc_retune_pause(card->host);
1116
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001117 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1118 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001119
1120 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001121 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001122 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001123 if (ret) {
1124 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1125 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001126 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001127 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001128
1129 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001130
1131 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1132 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001133 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001134
1135 main_md->part_curr = md->part_type;
1136 return 0;
1137}
1138
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001139static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1140{
1141 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001142 u32 result;
1143 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001144
Venkatraman Sad5fd972011-08-25 00:30:50 +05301145 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001146 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001147 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001148
1149 struct scatterlist sg;
1150
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001151 cmd.opcode = MMC_APP_CMD;
1152 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001153 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001154
1155 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001156 if (err)
1157 return (u32)-1;
1158 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001159 return (u32)-1;
1160
1161 memset(&cmd, 0, sizeof(struct mmc_command));
1162
1163 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1164 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001165 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001166
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001167 data.blksz = 4;
1168 data.blocks = 1;
1169 data.flags = MMC_DATA_READ;
1170 data.sg = &sg;
1171 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301172 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001173
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001174 mrq.cmd = &cmd;
1175 mrq.data = &data;
1176
Ben Dooks051913d2009-06-08 23:33:57 +01001177 blocks = kmalloc(4, GFP_KERNEL);
1178 if (!blocks)
1179 return (u32)-1;
1180
1181 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001182
1183 mmc_wait_for_req(card->host, &mrq);
1184
Ben Dooks051913d2009-06-08 23:33:57 +01001185 result = ntohl(*blocks);
1186 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001187
Ben Dooks051913d2009-06-08 23:33:57 +01001188 if (cmd.error || data.error)
1189 result = (u32)-1;
1190
1191 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001192}
1193
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001194static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001195{
Chris Ball1278dba2011-04-13 23:40:30 -04001196 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001197 int err;
1198
Adrian Hunter504f1912008-10-16 12:55:25 +03001199 cmd.opcode = MMC_SEND_STATUS;
1200 if (!mmc_host_is_spi(card->host))
1201 cmd.arg = card->rca << 16;
1202 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001203 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1204 if (err == 0)
1205 *status = cmd.resp[0];
1206 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001207}
1208
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001209static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001210 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001211{
1212 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1213 int err = 0;
1214 u32 status;
1215
1216 do {
1217 err = get_card_status(card, &status, 5);
1218 if (err) {
1219 pr_err("%s: error %d requesting status\n",
1220 req->rq_disk->disk_name, err);
1221 return err;
1222 }
1223
1224 if (status & R1_ERROR) {
1225 pr_err("%s: %s: error sending status cmd, status %#x\n",
1226 req->rq_disk->disk_name, __func__, status);
1227 *gen_err = 1;
1228 }
1229
Ulf Hansson95a91292014-01-29 13:11:27 +01001230 /* We may rely on the host hw to handle busy detection.*/
1231 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1232 hw_busy_detect)
1233 break;
1234
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001235 /*
1236 * Timeout if the device never becomes ready for data and never
1237 * leaves the program state.
1238 */
1239 if (time_after(jiffies, timeout)) {
1240 pr_err("%s: Card stuck in programming state! %s %s\n",
1241 mmc_hostname(card->host),
1242 req->rq_disk->disk_name, __func__);
1243 return -ETIMEDOUT;
1244 }
1245
1246 /*
1247 * Some cards mishandle the status bits,
1248 * so make sure to check both the busy
1249 * indication and the card state.
1250 */
1251 } while (!(status & R1_READY_FOR_DATA) ||
1252 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1253
1254 return err;
1255}
1256
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001257static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1258 struct request *req, int *gen_err, u32 *stop_status)
1259{
1260 struct mmc_host *host = card->host;
1261 struct mmc_command cmd = {0};
1262 int err;
1263 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1264
1265 /*
1266 * Normally we use R1B responses for WRITE, but in cases where the host
1267 * has specified a max_busy_timeout we need to validate it. A failure
1268 * means we need to prevent the host from doing hw busy detection, which
1269 * is done by converting to a R1 response instead.
1270 */
1271 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1272 use_r1b_resp = false;
1273
1274 cmd.opcode = MMC_STOP_TRANSMISSION;
1275 if (use_r1b_resp) {
1276 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1277 cmd.busy_timeout = timeout_ms;
1278 } else {
1279 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1280 }
1281
1282 err = mmc_wait_for_cmd(host, &cmd, 5);
1283 if (err)
1284 return err;
1285
1286 *stop_status = cmd.resp[0];
1287
1288 /* No need to check card status in case of READ. */
1289 if (rq_data_dir(req) == READ)
1290 return 0;
1291
1292 if (!mmc_host_is_spi(host) &&
1293 (*stop_status & R1_ERROR)) {
1294 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1295 req->rq_disk->disk_name, __func__, *stop_status);
1296 *gen_err = 1;
1297 }
1298
1299 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1300}
1301
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301302#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001303#define ERR_RETRY 2
1304#define ERR_ABORT 1
1305#define ERR_CONTINUE 0
1306
1307static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1308 bool status_valid, u32 status)
1309{
1310 switch (error) {
1311 case -EILSEQ:
1312 /* response crc error, retry the r/w cmd */
1313 pr_err("%s: %s sending %s command, card status %#x\n",
1314 req->rq_disk->disk_name, "response CRC error",
1315 name, status);
1316 return ERR_RETRY;
1317
1318 case -ETIMEDOUT:
1319 pr_err("%s: %s sending %s command, card status %#x\n",
1320 req->rq_disk->disk_name, "timed out", name, status);
1321
1322 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301323 if (!status_valid) {
1324 pr_err("%s: status not valid, retrying timeout\n",
1325 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001326 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301327 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001328
1329 /*
1330 * If it was a r/w cmd crc error, or illegal command
1331 * (eg, issued in wrong state) then retry - we should
1332 * have corrected the state problem above.
1333 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301334 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1335 pr_err("%s: command error, retrying timeout\n",
1336 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001337 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301338 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001339
1340 /* Otherwise abort the command */
1341 return ERR_ABORT;
1342
1343 default:
1344 /* We don't understand the error code the driver gave us */
1345 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1346 req->rq_disk->disk_name, error, status);
1347 return ERR_ABORT;
1348 }
1349}
1350
1351/*
1352 * Initial r/w and stop cmd error recovery.
1353 * We don't know whether the card received the r/w cmd or not, so try to
1354 * restore things back to a sane state. Essentially, we do this as follows:
1355 * - Obtain card status. If the first attempt to obtain card status fails,
1356 * the status word will reflect the failed status cmd, not the failed
1357 * r/w cmd. If we fail to obtain card status, it suggests we can no
1358 * longer communicate with the card.
1359 * - Check the card state. If the card received the cmd but there was a
1360 * transient problem with the response, it might still be in a data transfer
1361 * mode. Try to send it a stop command. If this fails, we can't recover.
1362 * - If the r/w cmd failed due to a response CRC error, it was probably
1363 * transient, so retry the cmd.
1364 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1365 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1366 * illegal cmd, retry.
1367 * Otherwise we don't understand what happened, so abort.
1368 */
1369static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001370 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001371{
1372 bool prev_cmd_status_valid = true;
1373 u32 status, stop_status = 0;
1374 int err, retry;
1375
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301376 if (mmc_card_removed(card))
1377 return ERR_NOMEDIUM;
1378
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001379 /*
1380 * Try to get card status which indicates both the card state
1381 * and why there was no response. If the first attempt fails,
1382 * we can't be sure the returned status is for the r/w command.
1383 */
1384 for (retry = 2; retry >= 0; retry--) {
1385 err = get_card_status(card, &status, 0);
1386 if (!err)
1387 break;
1388
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001389 /* Re-tune if needed */
1390 mmc_retune_recheck(card->host);
1391
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001392 prev_cmd_status_valid = false;
1393 pr_err("%s: error %d sending status command, %sing\n",
1394 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1395 }
1396
1397 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301398 if (err) {
1399 /* Check if the card is removed */
1400 if (mmc_detect_card_removed(card->host))
1401 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001402 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301403 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001404
Adrian Hunter67716322011-08-29 16:42:15 +03001405 /* Flag ECC errors */
1406 if ((status & R1_CARD_ECC_FAILED) ||
1407 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1408 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1409 *ecc_err = 1;
1410
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001411 /* Flag General errors */
1412 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1413 if ((status & R1_ERROR) ||
1414 (brq->stop.resp[0] & R1_ERROR)) {
1415 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1416 req->rq_disk->disk_name, __func__,
1417 brq->stop.resp[0], status);
1418 *gen_err = 1;
1419 }
1420
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001421 /*
1422 * Check the current card state. If it is in some data transfer
1423 * mode, tell it to stop (and hopefully transition back to TRAN.)
1424 */
1425 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1426 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001427 err = send_stop(card,
1428 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1429 req, gen_err, &stop_status);
1430 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001431 pr_err("%s: error %d sending stop command\n",
1432 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001433 /*
1434 * If the stop cmd also timed out, the card is probably
1435 * not present, so abort. Other errors are bad news too.
1436 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001437 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001438 }
1439
Adrian Hunter67716322011-08-29 16:42:15 +03001440 if (stop_status & R1_CARD_ECC_FAILED)
1441 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001442 }
1443
1444 /* Check for set block count errors */
1445 if (brq->sbc.error)
1446 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1447 prev_cmd_status_valid, status);
1448
1449 /* Check for r/w command errors */
1450 if (brq->cmd.error)
1451 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1452 prev_cmd_status_valid, status);
1453
Adrian Hunter67716322011-08-29 16:42:15 +03001454 /* Data errors */
1455 if (!brq->stop.error)
1456 return ERR_CONTINUE;
1457
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001458 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001459 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 +01001460 req->rq_disk->disk_name, brq->stop.error,
1461 brq->cmd.resp[0], status);
1462
1463 /*
1464 * Subsitute in our own stop status as this will give the error
1465 * state which happened during the execution of the r/w command.
1466 */
1467 if (stop_status) {
1468 brq->stop.resp[0] = stop_status;
1469 brq->stop.error = 0;
1470 }
1471 return ERR_CONTINUE;
1472}
1473
Adrian Hunter67716322011-08-29 16:42:15 +03001474static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1475 int type)
1476{
1477 int err;
1478
1479 if (md->reset_done & type)
1480 return -EEXIST;
1481
1482 md->reset_done |= type;
1483 err = mmc_hw_reset(host);
1484 /* Ensure we switch back to the correct partition */
1485 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001486 struct mmc_blk_data *main_md =
1487 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001488 int part_err;
1489
1490 main_md->part_curr = main_md->part_type;
1491 part_err = mmc_blk_part_switch(host->card, md);
1492 if (part_err) {
1493 /*
1494 * We have failed to get back into the correct
1495 * partition, so we need to abort the whole request.
1496 */
1497 return -ENODEV;
1498 }
1499 }
1500 return err;
1501}
1502
1503static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1504{
1505 md->reset_done &= ~type;
1506}
1507
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001508int mmc_access_rpmb(struct mmc_queue *mq)
1509{
1510 struct mmc_blk_data *md = mq->data;
1511 /*
1512 * If this is a RPMB partition access, return ture
1513 */
1514 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1515 return true;
1516
1517 return false;
1518}
1519
Adrian Hunterbd788c92010-08-11 14:17:47 -07001520static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1521{
1522 struct mmc_blk_data *md = mq->data;
1523 struct mmc_card *card = md->queue.card;
1524 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001525 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001526
Adrian Hunterbd788c92010-08-11 14:17:47 -07001527 if (!mmc_can_erase(card)) {
1528 err = -EOPNOTSUPP;
1529 goto out;
1530 }
1531
1532 from = blk_rq_pos(req);
1533 nr = blk_rq_sectors(req);
1534
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001535 if (mmc_can_discard(card))
1536 arg = MMC_DISCARD_ARG;
1537 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001538 arg = MMC_TRIM_ARG;
1539 else
1540 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001541retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001542 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1543 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1544 INAND_CMD38_ARG_EXT_CSD,
1545 arg == MMC_TRIM_ARG ?
1546 INAND_CMD38_ARG_TRIM :
1547 INAND_CMD38_ARG_ERASE,
1548 0);
1549 if (err)
1550 goto out;
1551 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001552 err = mmc_erase(card, from, nr, arg);
1553out:
Adrian Hunter67716322011-08-29 16:42:15 +03001554 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1555 goto retry;
1556 if (!err)
1557 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301558 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001559
Adrian Hunterbd788c92010-08-11 14:17:47 -07001560 return err ? 0 : 1;
1561}
1562
Adrian Hunter49804542010-08-11 14:17:50 -07001563static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1564 struct request *req)
1565{
1566 struct mmc_blk_data *md = mq->data;
1567 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001568 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001569 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001570
Maya Erez775a9362013-04-18 15:41:55 +03001571 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001572 err = -EOPNOTSUPP;
1573 goto out;
1574 }
1575
1576 from = blk_rq_pos(req);
1577 nr = blk_rq_sectors(req);
1578
Maya Erez775a9362013-04-18 15:41:55 +03001579 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1580 arg = MMC_SECURE_TRIM1_ARG;
1581 else
1582 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001583
Adrian Hunter67716322011-08-29 16:42:15 +03001584retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001585 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1586 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1587 INAND_CMD38_ARG_EXT_CSD,
1588 arg == MMC_SECURE_TRIM1_ARG ?
1589 INAND_CMD38_ARG_SECTRIM1 :
1590 INAND_CMD38_ARG_SECERASE,
1591 0);
1592 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001593 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001594 }
Adrian Hunter28302812012-04-05 14:45:48 +03001595
Adrian Hunter49804542010-08-11 14:17:50 -07001596 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001597 if (err == -EIO)
1598 goto out_retry;
1599 if (err)
1600 goto out;
1601
1602 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001603 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1604 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1605 INAND_CMD38_ARG_EXT_CSD,
1606 INAND_CMD38_ARG_SECTRIM2,
1607 0);
1608 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001609 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001610 }
Adrian Hunter28302812012-04-05 14:45:48 +03001611
Adrian Hunter49804542010-08-11 14:17:50 -07001612 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001613 if (err == -EIO)
1614 goto out_retry;
1615 if (err)
1616 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001617 }
Adrian Hunter28302812012-04-05 14:45:48 +03001618
Adrian Hunter28302812012-04-05 14:45:48 +03001619out_retry:
1620 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001621 goto retry;
1622 if (!err)
1623 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001624out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301625 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001626
Adrian Hunter49804542010-08-11 14:17:50 -07001627 return err ? 0 : 1;
1628}
1629
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001630static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1631{
1632 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001633 struct mmc_card *card = md->queue.card;
1634 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001635
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001636 ret = mmc_flush_cache(card);
1637 if (ret)
1638 ret = -EIO;
1639
Mark Salyzyn6904e432016-01-28 11:12:25 -08001640#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
1641 else if (atomic_read(&mq->cache_size)) {
1642 long used = mmc_blk_cache_used(mq, jiffies);
1643
1644 if (used) {
1645 int speed = atomic_read(&mq->max_write_speed);
1646
1647 if (speed_valid(speed)) {
1648 unsigned long msecs = jiffies_to_msecs(
1649 size_and_speed_to_jiffies(
1650 used, speed));
1651 if (msecs)
1652 msleep(msecs);
1653 }
1654 }
1655 }
1656#endif
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301657 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001658
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001659 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001660}
1661
1662/*
1663 * Reformat current write as a reliable write, supporting
1664 * both legacy and the enhanced reliable write MMC cards.
1665 * In each transfer we'll handle only as much as a single
1666 * reliable write can handle, thus finish the request in
1667 * partial completions.
1668 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001669static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1670 struct mmc_card *card,
1671 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001672{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001673 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1674 /* Legacy mode imposes restrictions on transfers. */
1675 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1676 brq->data.blocks = 1;
1677
1678 if (brq->data.blocks > card->ext_csd.rel_sectors)
1679 brq->data.blocks = card->ext_csd.rel_sectors;
1680 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1681 brq->data.blocks = 1;
1682 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001683}
1684
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001685#define CMD_ERRORS \
1686 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1687 R1_ADDRESS_ERROR | /* Misaligned address */ \
1688 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1689 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1690 R1_CC_ERROR | /* Card controller error */ \
1691 R1_ERROR) /* General/unknown error */
1692
Per Forlinee8a43a2011-07-01 18:55:33 +02001693static int mmc_blk_err_check(struct mmc_card *card,
1694 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001695{
Per Forlinee8a43a2011-07-01 18:55:33 +02001696 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1697 mmc_active);
1698 struct mmc_blk_request *brq = &mq_mrq->brq;
1699 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001700 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001701 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001702
1703 /*
1704 * sbc.error indicates a problem with the set block count
1705 * command. No data will have been transferred.
1706 *
1707 * cmd.error indicates a problem with the r/w command. No
1708 * data will have been transferred.
1709 *
1710 * stop.error indicates a problem with the stop command. Data
1711 * may have been transferred, or may still be transferring.
1712 */
Adrian Hunter67716322011-08-29 16:42:15 +03001713 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1714 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001715 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001716 case ERR_RETRY:
1717 return MMC_BLK_RETRY;
1718 case ERR_ABORT:
1719 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301720 case ERR_NOMEDIUM:
1721 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001722 case ERR_CONTINUE:
1723 break;
1724 }
1725 }
1726
1727 /*
1728 * Check for errors relating to the execution of the
1729 * initial command - such as address errors. No data
1730 * has been transferred.
1731 */
1732 if (brq->cmd.resp[0] & CMD_ERRORS) {
1733 pr_err("%s: r/w command failed, status = %#x\n",
1734 req->rq_disk->disk_name, brq->cmd.resp[0]);
1735 return MMC_BLK_ABORT;
1736 }
1737
1738 /*
1739 * Everything else is either success, or a data error of some
1740 * kind. If it was a write, we may have transitioned to
1741 * program mode, which we have to wait for it to complete.
1742 */
1743 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001744 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001745
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001746 /* Check stop command response */
1747 if (brq->stop.resp[0] & R1_ERROR) {
1748 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1749 req->rq_disk->disk_name, __func__,
1750 brq->stop.resp[0]);
1751 gen_err = 1;
1752 }
1753
Ulf Hansson95a91292014-01-29 13:11:27 +01001754 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1755 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001756 if (err)
1757 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001758 }
1759
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001760 /* if general error occurs, retry the write operation. */
1761 if (gen_err) {
1762 pr_warn("%s: retrying write for general error\n",
1763 req->rq_disk->disk_name);
1764 return MMC_BLK_RETRY;
1765 }
1766
Per Forlind78d4a82011-07-01 18:55:30 +02001767 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001768 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001769 pr_debug("%s: retrying because a re-tune was needed\n",
1770 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001771 brq->retune_retry_done = 1;
1772 return MMC_BLK_RETRY;
1773 }
Per Forlind78d4a82011-07-01 18:55:30 +02001774 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1775 req->rq_disk->disk_name, brq->data.error,
1776 (unsigned)blk_rq_pos(req),
1777 (unsigned)blk_rq_sectors(req),
1778 brq->cmd.resp[0], brq->stop.resp[0]);
1779
1780 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001781 if (ecc_err)
1782 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001783 return MMC_BLK_DATA_ERR;
1784 } else {
1785 return MMC_BLK_CMD_ERR;
1786 }
1787 }
1788
Adrian Hunter67716322011-08-29 16:42:15 +03001789 if (!brq->data.bytes_xfered)
1790 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001791
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001792 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1793 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1794 return MMC_BLK_PARTIAL;
1795 else
1796 return MMC_BLK_SUCCESS;
1797 }
1798
Adrian Hunter67716322011-08-29 16:42:15 +03001799 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1800 return MMC_BLK_PARTIAL;
1801
1802 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001803}
1804
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001805static int mmc_blk_packed_err_check(struct mmc_card *card,
1806 struct mmc_async_req *areq)
1807{
1808 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1809 mmc_active);
1810 struct request *req = mq_rq->req;
1811 struct mmc_packed *packed = mq_rq->packed;
1812 int err, check, status;
1813 u8 *ext_csd;
1814
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001815 packed->retries--;
1816 check = mmc_blk_err_check(card, areq);
1817 err = get_card_status(card, &status, 0);
1818 if (err) {
1819 pr_err("%s: error %d sending status command\n",
1820 req->rq_disk->disk_name, err);
1821 return MMC_BLK_ABORT;
1822 }
1823
1824 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02001825 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001826 if (err) {
1827 pr_err("%s: error %d sending ext_csd\n",
1828 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02001829 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001830 }
1831
1832 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1833 EXT_CSD_PACKED_FAILURE) &&
1834 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1835 EXT_CSD_PACKED_GENERIC_ERROR)) {
1836 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1837 EXT_CSD_PACKED_INDEXED_ERROR) {
1838 packed->idx_failure =
1839 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1840 check = MMC_BLK_PARTIAL;
1841 }
1842 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1843 "failure index: %d\n",
1844 req->rq_disk->disk_name, packed->nr_entries,
1845 packed->blocks, packed->idx_failure);
1846 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001847 kfree(ext_csd);
1848 }
1849
1850 return check;
1851}
1852
Per Forlin54d49d72011-07-01 18:55:29 +02001853static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1854 struct mmc_card *card,
1855 int disable_multi,
1856 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Per Forlin54d49d72011-07-01 18:55:29 +02001858 u32 readcmd, writecmd;
1859 struct mmc_blk_request *brq = &mqrq->brq;
1860 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301862 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001864 /*
1865 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001866 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001867 */
Luca Porziod3df0462015-11-06 15:12:26 +00001868 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001869 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001870 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001871
Per Forlin54d49d72011-07-01 18:55:29 +02001872 memset(brq, 0, sizeof(struct mmc_blk_request));
1873 brq->mrq.cmd = &brq->cmd;
1874 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Per Forlin54d49d72011-07-01 18:55:29 +02001876 brq->cmd.arg = blk_rq_pos(req);
1877 if (!mmc_card_blockaddr(card))
1878 brq->cmd.arg <<= 9;
1879 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1880 brq->data.blksz = 512;
1881 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1882 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001883 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Asutosh Dasf0665412012-07-27 18:10:19 +05301885 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02001886 /*
1887 * The block layer doesn't support all sector count
1888 * restrictions, so we need to be prepared for too big
1889 * requests.
1890 */
1891 if (brq->data.blocks > card->host->max_blk_count)
1892 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001894 if (brq->data.blocks > 1) {
1895 /*
1896 * After a read error, we redo the request one sector
1897 * at a time in order to accurately determine which
1898 * sectors can be read successfully.
1899 */
1900 if (disable_multi)
1901 brq->data.blocks = 1;
1902
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001903 /*
1904 * Some controllers have HW issues while operating
1905 * in multiple I/O mode
1906 */
1907 if (card->host->ops->multi_io_quirk)
1908 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1909 (rq_data_dir(req) == READ) ?
1910 MMC_DATA_READ : MMC_DATA_WRITE,
1911 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001912 }
Per Forlin54d49d72011-07-01 18:55:29 +02001913
1914 if (brq->data.blocks > 1 || do_rel_wr) {
1915 /* SPI multiblock writes terminate using a special
1916 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001917 */
Per Forlin54d49d72011-07-01 18:55:29 +02001918 if (!mmc_host_is_spi(card->host) ||
1919 rq_data_dir(req) == READ)
1920 brq->mrq.stop = &brq->stop;
1921 readcmd = MMC_READ_MULTIPLE_BLOCK;
1922 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1923 } else {
1924 brq->mrq.stop = NULL;
1925 readcmd = MMC_READ_SINGLE_BLOCK;
1926 writecmd = MMC_WRITE_BLOCK;
1927 }
1928 if (rq_data_dir(req) == READ) {
1929 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001930 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001931 if (brq->mrq.stop)
1932 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1933 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001934 } else {
1935 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001936 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001937 if (brq->mrq.stop)
1938 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1939 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001940 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001941
Per Forlin54d49d72011-07-01 18:55:29 +02001942 if (do_rel_wr)
1943 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001944
Per Forlin54d49d72011-07-01 18:55:29 +02001945 /*
Saugata Das42659002011-12-21 13:09:17 +05301946 * Data tag is used only during writing meta data to speed
1947 * up write and any subsequent read of this meta data
1948 */
1949 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1950 (req->cmd_flags & REQ_META) &&
1951 (rq_data_dir(req) == WRITE) &&
1952 ((brq->data.blocks * brq->data.blksz) >=
1953 card->ext_csd.data_tag_unit_size);
1954
1955 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001956 * Pre-defined multi-block transfers are preferable to
1957 * open ended-ones (and necessary for reliable writes).
1958 * However, it is not sufficient to just send CMD23,
1959 * and avoid the final CMD12, as on an error condition
1960 * CMD12 (stop) needs to be sent anyway. This, coupled
1961 * with Auto-CMD23 enhancements provided by some
1962 * hosts, means that the complexity of dealing
1963 * with this is best left to the host. If CMD23 is
1964 * supported by card and host, we'll fill sbc in and let
1965 * the host deal with handling it correctly. This means
1966 * that for hosts that don't expose MMC_CAP_CMD23, no
1967 * change of behavior will be observed.
1968 *
1969 * N.B: Some MMC cards experience perf degradation.
1970 * We'll avoid using CMD23-bounded multiblock writes for
1971 * these, while retaining features like reliable writes.
1972 */
Saugata Das42659002011-12-21 13:09:17 +05301973 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1974 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1975 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001976 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1977 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301978 (do_rel_wr ? (1 << 31) : 0) |
1979 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001980 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1981 brq->mrq.sbc = &brq->sbc;
1982 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001983
Per Forlin54d49d72011-07-01 18:55:29 +02001984 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001985
Per Forlin54d49d72011-07-01 18:55:29 +02001986 brq->data.sg = mqrq->sg;
1987 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001988
Per Forlin54d49d72011-07-01 18:55:29 +02001989 /*
1990 * Adjust the sg list so it is the same size as the
1991 * request.
1992 */
1993 if (brq->data.blocks != blk_rq_sectors(req)) {
1994 int i, data_size = brq->data.blocks << 9;
1995 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001996
Per Forlin54d49d72011-07-01 18:55:29 +02001997 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1998 data_size -= sg->length;
1999 if (data_size <= 0) {
2000 sg->length += data_size;
2001 i++;
2002 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01002003 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002004 }
Per Forlin54d49d72011-07-01 18:55:29 +02002005 brq->data.sg_len = i;
2006 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002007
Per Forlinee8a43a2011-07-01 18:55:33 +02002008 mqrq->mmc_active.mrq = &brq->mrq;
2009 mqrq->mmc_active.err_check = mmc_blk_err_check;
2010
Per Forlin54d49d72011-07-01 18:55:29 +02002011 mmc_queue_bounce_pre(mqrq);
2012}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002014static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
2015 struct mmc_card *card)
2016{
2017 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
2018 unsigned int max_seg_sz = queue_max_segment_size(q);
2019 unsigned int len, nr_segs = 0;
2020
2021 do {
2022 len = min(hdr_sz, max_seg_sz);
2023 hdr_sz -= len;
2024 nr_segs++;
2025 } while (hdr_sz);
2026
2027 return nr_segs;
2028}
2029
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002030static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2031 struct request *req)
2032{
2033 struct mmc_host *host = mq->card->host;
2034 int data_dir;
2035
2036 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2037 return;
2038
Maya Erez8e2b3c32012-12-02 13:27:15 +02002039 /* Support for the write packing on eMMC 4.5 or later */
2040 if (mq->card->ext_csd.rev <= 5)
2041 return;
2042
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002043 /*
2044 * In case the packing control is not supported by the host, it should
2045 * not have an effect on the write packing. Therefore we have to enable
2046 * the write packing
2047 */
2048 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2049 mq->wr_packing_enabled = true;
2050 return;
2051 }
2052
2053 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2054 if (mq->num_of_potential_packed_wr_reqs >
2055 mq->num_wr_reqs_to_start_packing)
2056 mq->wr_packing_enabled = true;
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002057 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002058 return;
2059 }
2060
2061 data_dir = rq_data_dir(req);
2062
2063 if (data_dir == READ) {
2064 mq->num_of_potential_packed_wr_reqs = 0;
2065 mq->wr_packing_enabled = false;
2066 return;
2067 } else if (data_dir == WRITE) {
2068 mq->num_of_potential_packed_wr_reqs++;
2069 }
2070
2071 if (mq->num_of_potential_packed_wr_reqs >
2072 mq->num_wr_reqs_to_start_packing)
2073 mq->wr_packing_enabled = true;
2074}
2075
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002076struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2077{
2078 if (!card)
2079 return NULL;
2080
2081 return &card->wr_pack_stats;
2082}
2083EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2084
2085void mmc_blk_init_packed_statistics(struct mmc_card *card)
2086{
2087 int max_num_of_packed_reqs = 0;
2088
2089 if (!card || !card->wr_pack_stats.packing_events)
2090 return;
2091
2092 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2093
2094 spin_lock(&card->wr_pack_stats.lock);
2095 memset(card->wr_pack_stats.packing_events, 0,
2096 (max_num_of_packed_reqs + 1) *
2097 sizeof(*card->wr_pack_stats.packing_events));
2098 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2099 sizeof(card->wr_pack_stats.pack_stop_reason));
2100 card->wr_pack_stats.enabled = true;
2101 spin_unlock(&card->wr_pack_stats.lock);
2102}
2103EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2104
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002105static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2106{
2107 struct request_queue *q = mq->queue;
2108 struct mmc_card *card = mq->card;
2109 struct request *cur = req, *next = NULL;
2110 struct mmc_blk_data *md = mq->data;
2111 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2112 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2113 unsigned int req_sectors = 0, phys_segments = 0;
2114 unsigned int max_blk_count, max_phys_segs;
2115 bool put_back = true;
2116 u8 max_packed_rw = 0;
2117 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002118 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002119
Shawn Lin96e52da2016-08-26 08:49:55 +08002120 /*
2121 * We don't need to check packed for any further
2122 * operation of packed stuff as we set MMC_PACKED_NONE
2123 * and return zero for reqs if geting null packed. Also
2124 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2125 * it again when removing blk req.
2126 */
2127 if (!mqrq->packed) {
2128 md->flags &= (~MMC_BLK_PACKED_CMD);
2129 goto no_packed;
2130 }
2131
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002132 if (!(md->flags & MMC_BLK_PACKED_CMD))
2133 goto no_packed;
2134
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002135 if (!mq->wr_packing_enabled)
2136 goto no_packed;
2137
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002138 if ((rq_data_dir(cur) == WRITE) &&
2139 mmc_host_packed_wr(card->host))
2140 max_packed_rw = card->ext_csd.max_packed_writes;
2141
2142 if (max_packed_rw == 0)
2143 goto no_packed;
2144
2145 if (mmc_req_rel_wr(cur) &&
2146 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2147 goto no_packed;
2148
2149 if (mmc_large_sector(card) &&
2150 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2151 goto no_packed;
2152
2153 mmc_blk_clear_packed(mqrq);
2154
2155 max_blk_count = min(card->host->max_blk_count,
2156 card->host->max_req_size >> 9);
2157 if (unlikely(max_blk_count > 0xffff))
2158 max_blk_count = 0xffff;
2159
2160 max_phys_segs = queue_max_segments(q);
2161 req_sectors += blk_rq_sectors(cur);
2162 phys_segments += cur->nr_phys_segments;
2163
2164 if (rq_data_dir(cur) == WRITE) {
2165 req_sectors += mmc_large_sector(card) ? 8 : 1;
2166 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2167 }
2168
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002169 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002170 do {
2171 if (reqs >= max_packed_rw - 1) {
2172 put_back = false;
2173 break;
2174 }
2175
2176 spin_lock_irq(q->queue_lock);
2177 next = blk_fetch_request(q);
2178 spin_unlock_irq(q->queue_lock);
2179 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002180 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002181 put_back = false;
2182 break;
2183 }
2184
2185 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002186 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2187 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002188 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002189 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002190
Mike Christie3a5e02c2016-06-05 14:32:23 -05002191 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002192 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002193 req_op(next) == REQ_OP_FLUSH) {
2194 if (req_op(next) != REQ_OP_SECURE_ERASE)
2195 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002196 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002197 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002198
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002199 if (rq_data_dir(cur) != rq_data_dir(next)) {
2200 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002201 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002202 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002203
2204 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002205 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2206 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002207 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002208 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002209
2210 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002211 if (req_sectors > max_blk_count) {
2212 if (stats->enabled)
2213 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002214 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002215 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002216
2217 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002218 if (phys_segments > max_phys_segs) {
2219 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002220 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002221 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002222
Maya Erez5a8dae12014-12-04 15:13:59 +02002223 if (mq->no_pack_for_random) {
2224 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2225 blk_rq_pos(next)) {
2226 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2227 put_back = 1;
2228 break;
2229 }
2230 }
2231
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002232 if (rq_data_dir(next) == WRITE)
2233 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002234 list_add_tail(&next->queuelist, &mqrq->packed->list);
2235 cur = next;
2236 reqs++;
2237 } while (1);
2238
2239 if (put_back) {
2240 spin_lock_irq(q->queue_lock);
2241 blk_requeue_request(q, next);
2242 spin_unlock_irq(q->queue_lock);
2243 }
2244
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002245 if (stats->enabled) {
2246 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2247 stats->packing_events[reqs + 1]++;
2248 if (reqs + 1 == max_packed_rw)
2249 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2250 }
2251
2252 spin_unlock(&stats->lock);
2253
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002254 if (reqs > 0) {
2255 list_add(&req->queuelist, &mqrq->packed->list);
2256 mqrq->packed->nr_entries = ++reqs;
2257 mqrq->packed->retries = reqs;
2258 return reqs;
2259 }
2260
2261no_packed:
2262 mqrq->cmd_type = MMC_PACKED_NONE;
2263 return 0;
2264}
2265
2266static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2267 struct mmc_card *card,
2268 struct mmc_queue *mq)
2269{
2270 struct mmc_blk_request *brq = &mqrq->brq;
2271 struct request *req = mqrq->req;
2272 struct request *prq;
2273 struct mmc_blk_data *md = mq->data;
2274 struct mmc_packed *packed = mqrq->packed;
2275 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002276 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002277 u8 hdr_blocks;
2278 u8 i = 1;
2279
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002280 mqrq->cmd_type = MMC_PACKED_WRITE;
2281 packed->blocks = 0;
2282 packed->idx_failure = MMC_PACKED_NR_IDX;
2283
2284 packed_cmd_hdr = packed->cmd_hdr;
2285 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002286 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2287 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002288 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2289
2290 /*
2291 * Argument for each entry of packed group
2292 */
2293 list_for_each_entry(prq, &packed->list, queuelist) {
2294 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2295 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2296 (prq->cmd_flags & REQ_META) &&
2297 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002298 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002299 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002300 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002301 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2302 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002303 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002304 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002305 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002306 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002307 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002308 packed->blocks += blk_rq_sectors(prq);
2309 i++;
2310 }
2311
2312 memset(brq, 0, sizeof(struct mmc_blk_request));
2313 brq->mrq.cmd = &brq->cmd;
2314 brq->mrq.data = &brq->data;
2315 brq->mrq.sbc = &brq->sbc;
2316 brq->mrq.stop = &brq->stop;
2317
2318 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2319 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2320 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2321
2322 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2323 brq->cmd.arg = blk_rq_pos(req);
2324 if (!mmc_card_blockaddr(card))
2325 brq->cmd.arg <<= 9;
2326 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2327
2328 brq->data.blksz = 512;
2329 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002330 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302331 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002332
2333 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2334 brq->stop.arg = 0;
2335 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2336
2337 mmc_set_data_timeout(&brq->data, card);
2338
2339 brq->data.sg = mqrq->sg;
2340 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2341
2342 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002343
2344 /*
2345 * This is intended for packed commands tests usage - in case these
2346 * functions are not in use the respective pointers are NULL
2347 */
2348 if (mq->err_check_fn)
2349 mqrq->mmc_active.err_check = mq->err_check_fn;
2350 else
2351 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2352
2353 if (mq->packed_test_fn)
2354 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002355
2356 mmc_queue_bounce_pre(mqrq);
2357}
2358
Adrian Hunter67716322011-08-29 16:42:15 +03002359static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2360 struct mmc_blk_request *brq, struct request *req,
2361 int ret)
2362{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002363 struct mmc_queue_req *mq_rq;
2364 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2365
Adrian Hunter67716322011-08-29 16:42:15 +03002366 /*
2367 * If this is an SD card and we're writing, we can first
2368 * mark the known good sectors as ok.
2369 *
2370 * If the card is not SD, we can still ok written sectors
2371 * as reported by the controller (which might be less than
2372 * the real number of written sectors, but never more).
2373 */
2374 if (mmc_card_sd(card)) {
2375 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302376 if (!brq->data.fault_injected) {
2377 blocks = mmc_sd_num_wr_blocks(card);
2378 if (blocks != (u32)-1)
2379 ret = blk_end_request(req, 0, blocks << 9);
2380 } else
2381 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002382 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002383 if (!mmc_packed_cmd(mq_rq->cmd_type))
2384 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002385 }
2386 return ret;
2387}
2388
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002389static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2390{
2391 struct request *prq;
2392 struct mmc_packed *packed = mq_rq->packed;
2393 int idx = packed->idx_failure, i = 0;
2394 int ret = 0;
2395
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002396 while (!list_empty(&packed->list)) {
2397 prq = list_entry_rq(packed->list.next);
2398 if (idx == i) {
2399 /* retry from error index */
2400 packed->nr_entries -= idx;
2401 mq_rq->req = prq;
2402 ret = 1;
2403
2404 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2405 list_del_init(&prq->queuelist);
2406 mmc_blk_clear_packed(mq_rq);
2407 }
2408 return ret;
2409 }
2410 list_del_init(&prq->queuelist);
2411 blk_end_request(prq, 0, blk_rq_bytes(prq));
2412 i++;
2413 }
2414
2415 mmc_blk_clear_packed(mq_rq);
2416 return ret;
2417}
2418
2419static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2420{
2421 struct request *prq;
2422 struct mmc_packed *packed = mq_rq->packed;
2423
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002424 while (!list_empty(&packed->list)) {
2425 prq = list_entry_rq(packed->list.next);
2426 list_del_init(&prq->queuelist);
2427 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2428 }
2429
2430 mmc_blk_clear_packed(mq_rq);
2431}
2432
2433static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2434 struct mmc_queue_req *mq_rq)
2435{
2436 struct request *prq;
2437 struct request_queue *q = mq->queue;
2438 struct mmc_packed *packed = mq_rq->packed;
2439
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002440 while (!list_empty(&packed->list)) {
2441 prq = list_entry_rq(packed->list.prev);
2442 if (prq->queuelist.prev != &packed->list) {
2443 list_del_init(&prq->queuelist);
2444 spin_lock_irq(q->queue_lock);
2445 blk_requeue_request(mq->queue, prq);
2446 spin_unlock_irq(q->queue_lock);
2447 } else {
2448 list_del_init(&prq->queuelist);
2449 }
2450 }
2451
2452 mmc_blk_clear_packed(mq_rq);
2453}
2454
Per Forlinee8a43a2011-07-01 18:55:33 +02002455static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02002456{
2457 struct mmc_blk_data *md = mq->data;
2458 struct mmc_card *card = md->queue.card;
2459 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002460 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002461 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002462 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302463 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02002464 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002465 const u8 packed_nr = 2;
2466 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002467#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2468 unsigned long waitfor = jiffies;
2469#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02002470
2471 if (!rqc && !mq->mqrq_prev->req)
2472 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002473
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002474 if (rqc)
2475 reqs = mmc_blk_prep_packed_list(mq, rqc);
2476
Per Forlin54d49d72011-07-01 18:55:29 +02002477 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002478 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302479 /*
2480 * When 4KB native sector is enabled, only 8 blocks
2481 * multiple read or write is allowed
2482 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00002483 if (mmc_large_sector(card) &&
2484 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302485 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
2486 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002487 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302488 goto cmd_abort;
2489 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002490
2491 if (reqs >= packed_nr)
2492 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2493 card, mq);
2494 else
2495 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002496 areq = &mq->mqrq_cur->mmc_active;
2497 } else
2498 areq = NULL;
2499 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002500 if (!areq) {
2501 if (status == MMC_BLK_NEW_REQUEST)
2502 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02002503 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002504 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002505
Per Forlinee8a43a2011-07-01 18:55:33 +02002506 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2507 brq = &mq_rq->brq;
2508 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002509 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002510 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002511
Per Forlind78d4a82011-07-01 18:55:30 +02002512 switch (status) {
2513 case MMC_BLK_SUCCESS:
2514 case MMC_BLK_PARTIAL:
2515 /*
2516 * A block was successfully transferred.
2517 */
Adrian Hunter67716322011-08-29 16:42:15 +03002518 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002519
Mark Salyzyn6904e432016-01-28 11:12:25 -08002520 mmc_blk_simulate_delay(mq, rqc, waitfor);
2521
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002522 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2523 ret = mmc_blk_end_packed_req(mq_rq);
2524 break;
2525 } else {
2526 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002527 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002528 }
2529
Adrian Hunter67716322011-08-29 16:42:15 +03002530 /*
2531 * If the blk_end_request function returns non-zero even
2532 * though all data has been transferred and no errors
2533 * were returned by the host controller, it's a bug.
2534 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002535 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302536 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002537 __func__, blk_rq_bytes(req),
2538 brq->data.bytes_xfered);
2539 rqc = NULL;
2540 goto cmd_abort;
2541 }
Per Forlind78d4a82011-07-01 18:55:30 +02002542 break;
2543 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002544 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002545 if (mmc_blk_reset(md, card->host, type))
2546 goto cmd_abort;
2547 if (!ret)
2548 goto start_new_req;
2549 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002550 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002551 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a82011-07-01 18:55:30 +02002552 if (retry++ < 5)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002553 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002554 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002555 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002556 if (!mmc_blk_reset(md, card->host, type))
2557 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002558 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002559 case MMC_BLK_DATA_ERR: {
2560 int err;
2561
2562 err = mmc_blk_reset(md, card->host, type);
2563 if (!err)
2564 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05302565 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002566 }
2567 case MMC_BLK_ECC_ERR:
2568 if (brq->data.blocks > 1) {
2569 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002570 pr_warn("%s: retrying using single block read\n",
2571 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002572 disable_multi = 1;
2573 break;
2574 }
Per Forlind78d4a82011-07-01 18:55:30 +02002575 /*
2576 * After an error, we redo I/O one sector at a
2577 * time, so we only reach here after trying to
2578 * read a single sector.
2579 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302580 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002581 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002582 if (!ret)
2583 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002584 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302585 case MMC_BLK_NOMEDIUM:
2586 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002587 default:
2588 pr_err("%s: Unhandled return value (%d)",
2589 req->rq_disk->disk_name, status);
2590 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002591 }
2592
Per Forlinee8a43a2011-07-01 18:55:33 +02002593 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002594 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2595 if (!mq_rq->packed->retries)
2596 goto cmd_abort;
2597 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2598 mmc_start_req(card->host,
2599 &mq_rq->mmc_active, NULL);
2600 } else {
2601
2602 /*
2603 * In case of a incomplete request
2604 * prepare it again and resend.
2605 */
2606 mmc_blk_rw_rq_prep(mq_rq, card,
2607 disable_multi, mq);
2608 mmc_start_req(card->host,
2609 &mq_rq->mmc_active, NULL);
2610 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002611 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 } while (ret);
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 return 1;
2616
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002617 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002618 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2619 mmc_blk_abort_packed_req(mq_rq);
2620 } else {
2621 if (mmc_card_removed(card))
2622 req->cmd_flags |= REQ_QUIET;
2623 while (ret)
2624 ret = blk_end_request(req, -EIO,
2625 blk_rq_cur_bytes(req));
2626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Per Forlinee8a43a2011-07-01 18:55:33 +02002628 start_new_req:
2629 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002630 if (mmc_card_removed(card)) {
2631 rqc->cmd_flags |= REQ_QUIET;
2632 blk_end_request_all(rqc, -EIO);
2633 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002634 /*
2635 * If current request is packed, it needs to put back.
2636 */
2637 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2638 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2639
Seungwon Jeon7a819022013-01-22 19:48:07 +09002640 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2641 mmc_start_req(card->host,
2642 &mq->mqrq_cur->mmc_active, NULL);
2643 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002644 }
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 return 0;
2647}
2648
Linus Walleij29eb7bd2016-09-20 11:34:38 +02002649int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002650{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002651 int ret;
2652 struct mmc_blk_data *md = mq->data;
2653 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002654 struct mmc_host *host = card->host;
2655 unsigned long flags;
Adrian Hunter869c5542016-08-25 14:11:43 -06002656 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002657
Per Forlinee8a43a2011-07-01 18:55:33 +02002658 if (req && !mq->mqrq_prev->req)
2659 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002660 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002661
Andrei Warkentin371a6892011-04-11 18:10:25 -05002662 ret = mmc_blk_part_switch(card, md);
2663 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002664 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302665 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002666 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002667 ret = 0;
2668 goto out;
2669 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002670
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002671 mmc_blk_write_packing_control(mq, req);
2672
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002673 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05002674 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002675 /* complete ongoing async transfer before issuing discard */
2676 if (card->host->areq)
2677 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02002678 ret = mmc_blk_issue_discard_rq(mq, req);
2679 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
2680 /* complete ongoing async transfer before issuing secure erase*/
2681 if (card->host->areq)
2682 mmc_blk_issue_rw_rq(mq, NULL);
2683 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05002684 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002685 /* complete ongoing async transfer before issuing flush */
2686 if (card->host->areq)
2687 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002688 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002689 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002690 if (!req && host->areq) {
2691 spin_lock_irqsave(&host->context_info.lock, flags);
2692 host->context_info.is_waiting_last_req = true;
2693 spin_unlock_irqrestore(&host->context_info.lock, flags);
2694 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002695 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002696 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002697
Andrei Warkentin371a6892011-04-11 18:10:25 -05002698out:
Adrian Hunter869c5542016-08-25 14:11:43 -06002699 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002700 /*
2701 * Release host when there are no more requests
2702 * and after special request(discard, flush) is done.
2703 * In case sepecial request, there is no reentry to
2704 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2705 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002706 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002707 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002708}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Russell Kinga6f6c962006-01-03 22:38:44 +00002710static inline int mmc_blk_readonly(struct mmc_card *card)
2711{
2712 return mmc_card_readonly(card) ||
2713 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2714}
2715
Andrei Warkentin371a6892011-04-11 18:10:25 -05002716static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2717 struct device *parent,
2718 sector_t size,
2719 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002720 const char *subname,
2721 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
2723 struct mmc_blk_data *md;
2724 int devidx, ret;
2725
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002726again:
2727 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
2728 return ERR_PTR(-ENOMEM);
2729
2730 spin_lock(&mmc_blk_lock);
2731 ret = ida_get_new(&mmc_blk_ida, &devidx);
2732 spin_unlock(&mmc_blk_lock);
2733
2734 if (ret == -EAGAIN)
2735 goto again;
2736 else if (ret)
2737 return ERR_PTR(ret);
2738
2739 if (devidx >= max_devices) {
2740 ret = -ENOSPC;
2741 goto out;
2742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002744 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002745 if (!md) {
2746 ret = -ENOMEM;
2747 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002749
Johan Rudholmadd710e2011-12-02 08:51:06 +01002750 md->area_type = area_type;
2751
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002752 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002753 * Set the read-only status based on the supported commands
2754 * and the write protect switch.
2755 */
2756 md->read_only = mmc_blk_readonly(card);
2757
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002758 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002759 if (md->disk == NULL) {
2760 ret = -ENOMEM;
2761 goto err_kfree;
2762 }
2763
2764 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002765 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002766 md->usage = 1;
2767
Adrian Hunterd09408a2011-06-23 13:40:28 +03002768 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002769 if (ret)
2770 goto err_putdisk;
2771
Russell Kinga6f6c962006-01-03 22:38:44 +00002772 md->queue.data = md;
2773
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002774 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002775 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002776 md->disk->fops = &mmc_bdops;
2777 md->disk->private_data = md;
2778 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002779 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002780 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002781 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002782 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002783 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002784
2785 /*
2786 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2787 *
2788 * - be set for removable media with permanent block devices
2789 * - be unset for removable block devices with permanent media
2790 *
2791 * Since MMC block devices clearly fall under the second
2792 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2793 * should use the block device creation/destruction hotplug
2794 * messages to tell when the card is present.
2795 */
2796
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002797 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02002798 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002799
Saugata Dasa5075eb2012-05-17 16:32:21 +05302800 if (mmc_card_mmc(card))
2801 blk_queue_logical_block_size(md->queue.queue,
2802 card->ext_csd.data_sector_size);
2803 else
2804 blk_queue_logical_block_size(md->queue.queue, 512);
2805
Andrei Warkentin371a6892011-04-11 18:10:25 -05002806 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002807
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002808 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02002809 if ((mmc_card_mmc(card) &&
2810 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002811 (mmc_card_sd(card) &&
2812 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2813 md->flags |= MMC_BLK_CMD23;
2814 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002815
2816 if (mmc_card_mmc(card) &&
2817 md->flags & MMC_BLK_CMD23 &&
2818 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2819 card->ext_csd.rel_sectors)) {
2820 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06002821 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002822 }
2823
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002824 if (mmc_card_mmc(card) &&
2825 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2826 (md->flags & MMC_BLK_CMD23) &&
2827 card->ext_csd.packed_event_en) {
2828 if (!mmc_packed_init(&md->queue, card))
2829 md->flags |= MMC_BLK_PACKED_CMD;
2830 }
2831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002833
2834 err_putdisk:
2835 put_disk(md->disk);
2836 err_kfree:
2837 kfree(md);
2838 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002839 spin_lock(&mmc_blk_lock);
2840 ida_remove(&mmc_blk_ida, devidx);
2841 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00002842 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843}
2844
Andrei Warkentin371a6892011-04-11 18:10:25 -05002845static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2846{
2847 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002848
2849 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2850 /*
2851 * The EXT_CSD sector count is in number or 512 byte
2852 * sectors.
2853 */
2854 size = card->ext_csd.sectors;
2855 } else {
2856 /*
2857 * The CSD capacity field is in units of read_blkbits.
2858 * set_capacity takes units of 512 bytes.
2859 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002860 size = (typeof(sector_t))card->csd.capacity
2861 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002862 }
2863
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002864 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002865 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002866}
2867
2868static int mmc_blk_alloc_part(struct mmc_card *card,
2869 struct mmc_blk_data *md,
2870 unsigned int part_type,
2871 sector_t size,
2872 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002873 const char *subname,
2874 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002875{
2876 char cap_str[10];
2877 struct mmc_blk_data *part_md;
2878
2879 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002880 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002881 if (IS_ERR(part_md))
2882 return PTR_ERR(part_md);
2883 part_md->part_type = part_type;
2884 list_add(&part_md->part, &md->part);
2885
James Bottomleyb9f28d82015-03-05 18:47:01 -08002886 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002887 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302888 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002889 part_md->disk->disk_name, mmc_card_id(card),
2890 mmc_card_name(card), part_md->part_type, cap_str);
2891 return 0;
2892}
2893
Namjae Jeone0c368d2011-10-06 23:41:38 +09002894/* MMC Physical partitions consist of two boot partitions and
2895 * up to four general purpose partitions.
2896 * For each partition enabled in EXT_CSD a block device will be allocatedi
2897 * to provide access to the partition.
2898 */
2899
Andrei Warkentin371a6892011-04-11 18:10:25 -05002900static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2901{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002902 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002903
2904 if (!mmc_card_mmc(card))
2905 return 0;
2906
Namjae Jeone0c368d2011-10-06 23:41:38 +09002907 for (idx = 0; idx < card->nr_parts; idx++) {
2908 if (card->part[idx].size) {
2909 ret = mmc_blk_alloc_part(card, md,
2910 card->part[idx].part_cfg,
2911 card->part[idx].size >> 9,
2912 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002913 card->part[idx].name,
2914 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002915 if (ret)
2916 return ret;
2917 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002918 }
2919
2920 return ret;
2921}
2922
Andrei Warkentin371a6892011-04-11 18:10:25 -05002923static void mmc_blk_remove_req(struct mmc_blk_data *md)
2924{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002925 struct mmc_card *card;
2926
Andrei Warkentin371a6892011-04-11 18:10:25 -05002927 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002928 /*
2929 * Flush remaining requests and free queues. It
2930 * is freeing the queue that stops new requests
2931 * from being accepted.
2932 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002933 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002934 mmc_cleanup_queue(&md->queue);
2935 if (md->flags & MMC_BLK_PACKED_CMD)
2936 mmc_packed_clean(&md->queue);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002937 device_remove_file(disk_to_dev(md->disk),
2938 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002939 if (md->disk->flags & GENHD_FL_UP) {
2940 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002941 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2942 card->ext_csd.boot_ro_lockable)
2943 device_remove_file(disk_to_dev(md->disk),
2944 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08002945#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2946 device_remove_file(disk_to_dev(md->disk),
2947 &dev_attr_max_write_speed);
2948 device_remove_file(disk_to_dev(md->disk),
2949 &dev_attr_max_read_speed);
2950 device_remove_file(disk_to_dev(md->disk),
2951 &dev_attr_cache_size);
2952#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002953
Andrei Warkentin371a6892011-04-11 18:10:25 -05002954 del_gendisk(md->disk);
2955 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002956 mmc_blk_put(md);
2957 }
2958}
2959
2960static void mmc_blk_remove_parts(struct mmc_card *card,
2961 struct mmc_blk_data *md)
2962{
2963 struct list_head *pos, *q;
2964 struct mmc_blk_data *part_md;
2965
2966 list_for_each_safe(pos, q, &md->part) {
2967 part_md = list_entry(pos, struct mmc_blk_data, part);
2968 list_del(pos);
2969 mmc_blk_remove_req(part_md);
2970 }
2971}
2972
2973static int mmc_add_disk(struct mmc_blk_data *md)
2974{
2975 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002976 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002977
Dan Williams307d8e62016-06-20 10:40:44 -07002978 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002979 md->force_ro.show = force_ro_show;
2980 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302981 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002982 md->force_ro.attr.name = "force_ro";
2983 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2984 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2985 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002986 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002987#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2988 atomic_set(&md->queue.max_write_speed, max_write_speed);
2989 ret = device_create_file(disk_to_dev(md->disk),
2990 &dev_attr_max_write_speed);
2991 if (ret)
2992 goto max_write_speed_fail;
2993 atomic_set(&md->queue.max_read_speed, max_read_speed);
2994 ret = device_create_file(disk_to_dev(md->disk),
2995 &dev_attr_max_read_speed);
2996 if (ret)
2997 goto max_read_speed_fail;
2998 atomic_set(&md->queue.cache_size, cache_size);
2999 atomic_long_set(&md->queue.cache_used, 0);
3000 md->queue.cache_jiffies = jiffies;
3001 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3002 if (ret)
3003 goto cache_size_fail;
3004#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003005
3006 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3007 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04003008 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003009
3010 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
3011 mode = S_IRUGO;
3012 else
3013 mode = S_IRUGO | S_IWUSR;
3014
3015 md->power_ro_lock.show = power_ro_lock_show;
3016 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01003017 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003018 md->power_ro_lock.attr.mode = mode;
3019 md->power_ro_lock.attr.name =
3020 "ro_lock_until_next_power_on";
3021 ret = device_create_file(disk_to_dev(md->disk),
3022 &md->power_ro_lock);
3023 if (ret)
3024 goto power_ro_lock_fail;
3025 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003026
3027 md->num_wr_reqs_to_start_packing.show =
3028 num_wr_reqs_to_start_packing_show;
3029 md->num_wr_reqs_to_start_packing.store =
3030 num_wr_reqs_to_start_packing_store;
3031 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
3032 md->num_wr_reqs_to_start_packing.attr.name =
3033 "num_wr_reqs_to_start_packing";
3034 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
3035 ret = device_create_file(disk_to_dev(md->disk),
3036 &md->num_wr_reqs_to_start_packing);
3037 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02003038 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003039
Maya Erez5a8dae12014-12-04 15:13:59 +02003040 md->no_pack_for_random.show = no_pack_for_random_show;
3041 md->no_pack_for_random.store = no_pack_for_random_store;
3042 sysfs_attr_init(&md->no_pack_for_random.attr);
3043 md->no_pack_for_random.attr.name = "no_pack_for_random";
3044 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
3045 ret = device_create_file(disk_to_dev(md->disk),
3046 &md->no_pack_for_random);
3047 if (ret)
3048 goto no_pack_for_random_fails;
3049
Johan Rudholmadd710e2011-12-02 08:51:06 +01003050 return ret;
3051
Maya Erez5a8dae12014-12-04 15:13:59 +02003052no_pack_for_random_fails:
3053 device_remove_file(disk_to_dev(md->disk),
3054 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02003055num_wr_reqs_to_start_packing_fail:
3056 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003057power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08003058#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3059 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3060cache_size_fail:
3061 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
3062max_read_speed_fail:
3063 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
3064max_write_speed_fail:
3065#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003066 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
3067force_ro_fail:
3068 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003069
3070 return ret;
3071}
3072
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003073static const struct mmc_fixup blk_fixups[] =
3074{
Chris Ballc59d4472011-11-11 22:01:43 -05003075 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
3076 MMC_QUIRK_INAND_CMD38),
3077 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
3078 MMC_QUIRK_INAND_CMD38),
3079 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
3080 MMC_QUIRK_INAND_CMD38),
3081 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
3082 MMC_QUIRK_INAND_CMD38),
3083 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
3084 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003085
3086 /*
3087 * Some MMC cards experience performance degradation with CMD23
3088 * instead of CMD12-bounded multiblock transfers. For now we'll
3089 * black list what's bad...
3090 * - Certain Toshiba cards.
3091 *
3092 * N.B. This doesn't affect SD cards.
3093 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08003094 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3095 MMC_QUIRK_BLK_NO_CMD23),
3096 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3097 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003098 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003099 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003100 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003101 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003102 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003103 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003104
3105 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03003106 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003107 */
Chris Ballc59d4472011-11-11 22:01:43 -05003108 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003109 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03003110 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
3111 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003112
Ian Chen3550ccd2012-08-29 15:05:36 +09003113 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08003114 * Some Samsung MMC cards need longer data read timeout than
3115 * indicated in CSD.
3116 */
3117 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
3118 MMC_QUIRK_LONG_READ_TIME),
3119
3120 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09003121 * On these Samsung MoviNAND parts, performing secure erase or
3122 * secure trim can result in unrecoverable corruption due to a
3123 * firmware bug.
3124 */
3125 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3126 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3127 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3128 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3129 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3130 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3131 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3132 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3133 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3134 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3135 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3136 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3137 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3138 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3139 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3140 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3141
Shawn Linb5b4ff02015-08-12 13:08:32 +08003142 /*
3143 * On Some Kingston eMMCs, performing trim can result in
3144 * unrecoverable data conrruption occasionally due to a firmware bug.
3145 */
3146 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3147 MMC_QUIRK_TRIM_BROKEN),
3148 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3149 MMC_QUIRK_TRIM_BROKEN),
3150
Pratibhasagar V8d664e32014-12-03 18:26:42 +02003151 /* Some INAND MCP devices advertise incorrect timeout values */
3152 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
3153 MMC_QUIRK_INAND_DATA_TIMEOUT),
3154
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003155 END_FIXUP
3156};
3157
Ulf Hansson96541ba2015-04-14 13:06:12 +02003158static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003160 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003161 char cap_str[10];
3162
Pierre Ossman912490d2005-05-21 10:27:02 +01003163 /*
3164 * Check that the card supports the command class(es) we need.
3165 */
3166 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 return -ENODEV;
3168
Lukas Czerner5204d002014-06-18 13:18:07 +02003169 mmc_fixup_device(card, blk_fixups);
3170
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 md = mmc_blk_alloc(card);
3172 if (IS_ERR(md))
3173 return PTR_ERR(md);
3174
James Bottomleyb9f28d82015-03-05 18:47:01 -08003175 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003176 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303177 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003179 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Andrei Warkentin371a6892011-04-11 18:10:25 -05003181 if (mmc_blk_alloc_parts(card, md))
3182 goto out;
3183
Ulf Hansson96541ba2015-04-14 13:06:12 +02003184 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003185
Andrei Warkentin371a6892011-04-11 18:10:25 -05003186 if (mmc_add_disk(md))
3187 goto out;
3188
3189 list_for_each_entry(part_md, &md->part, part) {
3190 if (mmc_add_disk(part_md))
3191 goto out;
3192 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003193
3194 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
3195 pm_runtime_use_autosuspend(&card->dev);
3196
3197 /*
3198 * Don't enable runtime PM for SD-combo cards here. Leave that
3199 * decision to be taken during the SDIO init sequence instead.
3200 */
3201 if (card->type != MMC_TYPE_SD_COMBO) {
3202 pm_runtime_set_active(&card->dev);
3203 pm_runtime_enable(&card->dev);
3204 }
3205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 return 0;
3207
3208 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05003209 mmc_blk_remove_parts(card, md);
3210 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01003211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212}
3213
Ulf Hansson96541ba2015-04-14 13:06:12 +02003214static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003216 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Andrei Warkentin371a6892011-04-11 18:10:25 -05003218 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003219 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003220 mmc_claim_host(card->host);
3221 mmc_blk_part_switch(card, md);
3222 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003223 if (card->type != MMC_TYPE_SD_COMBO)
3224 pm_runtime_disable(&card->dev);
3225 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003226 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02003227 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
Ulf Hansson96541ba2015-04-14 13:06:12 +02003230static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003232 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02003233 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303234 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
3236 if (md) {
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303237 rc = mmc_queue_suspend(&md->queue);
3238 if (rc)
3239 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003240 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303241 rc = mmc_queue_suspend(&part_md->queue);
3242 if (rc)
3243 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303246 goto out;
3247
3248 out_resume:
3249 mmc_queue_resume(&md->queue);
3250 list_for_each_entry(part_md, &md->part, part) {
3251 mmc_queue_resume(&part_md->queue);
3252 }
3253 out:
3254 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255}
3256
Ulf Hansson96541ba2015-04-14 13:06:12 +02003257static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02003258{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003259 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003260}
3261
Ulf Hansson0967edc2014-10-06 11:29:42 +02003262#ifdef CONFIG_PM_SLEEP
3263static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02003264{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003265 struct mmc_card *card = mmc_dev_to_card(dev);
3266
3267 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003268}
3269
Ulf Hansson0967edc2014-10-06 11:29:42 +02003270static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003272 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02003273 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
3275 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003276 /*
3277 * Resume involves the card going into idle state,
3278 * so current partition is always the main one.
3279 */
3280 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003282 list_for_each_entry(part_md, &md->part, part) {
3283 mmc_queue_resume(&part_md->queue);
3284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 }
3286 return 0;
3287}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288#endif
3289
Ulf Hansson0967edc2014-10-06 11:29:42 +02003290static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3291
Ulf Hansson96541ba2015-04-14 13:06:12 +02003292static struct mmc_driver mmc_driver = {
3293 .drv = {
3294 .name = "mmcblk",
3295 .pm = &mmc_blk_pm_ops,
3296 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 .probe = mmc_blk_probe,
3298 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02003299 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300};
3301
3302static int __init mmc_blk_init(void)
3303{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003304 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003306 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3307 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3308
Ben Hutchingsa26eba62014-11-06 03:35:09 +00003309 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003310
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003311 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3312 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003315 res = mmc_register_driver(&mmc_driver);
3316 if (res)
3317 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003319 return 0;
3320 out2:
3321 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 out:
3323 return res;
3324}
3325
3326static void __exit mmc_blk_exit(void)
3327{
3328 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003329 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330}
3331
3332module_init(mmc_blk_init);
3333module_exit(mmc_blk_exit);
3334
3335MODULE_LICENSE("GPL");
3336MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3337