blob: 3244d047da6505eb30dee0ca4ead4d0084637f70 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020037#include <linux/pm_runtime.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020038#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
John Calixtocb87ea22011-04-26 18:56:29 -040040#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020042#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010043#include <linux/mmc/mmc.h>
44#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/uaccess.h>
47
Pierre Ossman98ac2162006-12-23 20:03:02 +010048#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080049#include "block.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000051MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040052#ifdef MODULE_PARAM_PREFIX
53#undef MODULE_PARAM_PREFIX
54#endif
55#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010056
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050057#define INAND_CMD38_ARG_EXT_CSD 113
58#define INAND_CMD38_ARG_ERASE 0x00
59#define INAND_CMD38_ARG_TRIM 0x01
60#define INAND_CMD38_ARG_SECERASE 0x80
61#define INAND_CMD38_ARG_SECTRIM1 0x81
62#define INAND_CMD38_ARG_SECTRIM2 0x88
Trey Ramsay8fee4762012-11-16 09:31:41 -060063#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030064#define MMC_SANITIZE_REQ_TIMEOUT 240000
65#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050066
Luca Porziod3df0462015-11-06 15:12:26 +000067#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090068 (rq_data_dir(req) == WRITE))
69#define PACKED_CMD_VER 0x01
70#define PACKED_CMD_WR 0x02
71
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020072static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040073
74/*
75 * The defaults come from config options but can be overriden by module
76 * or bootarg options.
77 */
78static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
79
80/*
81 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000082 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020083 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040084 */
85static int max_devices;
86
Ben Hutchingsa26eba62014-11-06 03:35:09 +000087#define MAX_DEVICES 256
88
Ulf Hanssonb10fa992016-04-07 14:36:46 +020089static DEFINE_IDA(mmc_blk_ida);
90static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * There is one mmc_blk_data per slot.
94 */
95struct mmc_blk_data {
96 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -070097 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct gendisk *disk;
99 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500100 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500102 unsigned int flags;
103#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
104#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900105#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000108 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500109 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300110 unsigned int reset_done;
111#define MMC_BLK_READ BIT(0)
112#define MMC_BLK_WRITE BIT(1)
113#define MMC_BLK_DISCARD BIT(2)
114#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500115
116 /*
117 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200118 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500119 * track of the current selected device partition.
120 */
121 unsigned int part_curr;
122 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100123 struct device_attribute power_ro_lock;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200124 struct device_attribute num_wr_reqs_to_start_packing;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100125 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
Arjan van de Vena621aae2006-01-12 18:43:35 +0000128static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900130enum {
131 MMC_PACKED_NR_IDX = -1,
132 MMC_PACKED_NR_ZERO,
133 MMC_PACKED_NR_SINGLE,
134};
135
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400136module_param(perdev_minors, int, 0444);
137MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
138
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200139static inline int mmc_blk_part_switch(struct mmc_card *card,
140 struct mmc_blk_data *md);
141static int get_card_status(struct mmc_card *card, u32 *status, int retries);
142
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900143static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
144{
145 struct mmc_packed *packed = mqrq->packed;
146
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900147 mqrq->cmd_type = MMC_PACKED_NONE;
148 packed->nr_entries = MMC_PACKED_NR_ZERO;
149 packed->idx_failure = MMC_PACKED_NR_IDX;
150 packed->retries = 0;
151 packed->blocks = 0;
152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
155{
156 struct mmc_blk_data *md;
157
Arjan van de Vena621aae2006-01-12 18:43:35 +0000158 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 md = disk->private_data;
160 if (md && md->usage == 0)
161 md = NULL;
162 if (md)
163 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000164 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 return md;
167}
168
Andrei Warkentin371a6892011-04-11 18:10:25 -0500169static inline int mmc_get_devidx(struct gendisk *disk)
170{
Colin Cross382c55f2015-10-22 10:00:41 -0700171 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500172 return devidx;
173}
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static void mmc_blk_put(struct mmc_blk_data *md)
176{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000177 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 md->usage--;
179 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500180 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800181 blk_cleanup_queue(md->queue.queue);
182
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200183 spin_lock(&mmc_blk_lock);
184 ida_remove(&mmc_blk_ida, devidx);
185 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 kfree(md);
189 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000190 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Johan Rudholmadd710e2011-12-02 08:51:06 +0100193static ssize_t power_ro_lock_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
195{
196 int ret;
197 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
198 struct mmc_card *card = md->queue.card;
199 int locked = 0;
200
201 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
202 locked = 2;
203 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
204 locked = 1;
205
206 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
207
Tomas Winkler9098f842015-07-16 15:50:45 +0200208 mmc_blk_put(md);
209
Johan Rudholmadd710e2011-12-02 08:51:06 +0100210 return ret;
211}
212
213static ssize_t power_ro_lock_store(struct device *dev,
214 struct device_attribute *attr, const char *buf, size_t count)
215{
216 int ret;
217 struct mmc_blk_data *md, *part_md;
218 struct mmc_card *card;
219 unsigned long set;
220
221 if (kstrtoul(buf, 0, &set))
222 return -EINVAL;
223
224 if (set != 1)
225 return count;
226
227 md = mmc_blk_get(dev_to_disk(dev));
228 card = md->queue.card;
229
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200230 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100231
232 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
233 card->ext_csd.boot_ro_lock |
234 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
235 card->ext_csd.part_time);
236 if (ret)
237 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
238 else
239 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
240
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200241 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100242
243 if (!ret) {
244 pr_info("%s: Locking boot partition ro until next power on\n",
245 md->disk->disk_name);
246 set_disk_ro(md->disk, 1);
247
248 list_for_each_entry(part_md, &md->part, part)
249 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
250 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
251 set_disk_ro(part_md->disk, 1);
252 }
253 }
254
255 mmc_blk_put(md);
256 return count;
257}
258
Andrei Warkentin371a6892011-04-11 18:10:25 -0500259static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
260 char *buf)
261{
262 int ret;
263 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
264
Baruch Siach0031a982014-09-22 10:12:51 +0300265 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500266 get_disk_ro(dev_to_disk(dev)) ^
267 md->read_only);
268 mmc_blk_put(md);
269 return ret;
270}
271
272static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
273 const char *buf, size_t count)
274{
275 int ret;
276 char *end;
277 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
278 unsigned long set = simple_strtoul(buf, &end, 0);
279 if (end == buf) {
280 ret = -EINVAL;
281 goto out;
282 }
283
284 set_disk_ro(dev_to_disk(dev), set || md->read_only);
285 ret = count;
286out:
287 mmc_blk_put(md);
288 return ret;
289}
290
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200291static ssize_t
292num_wr_reqs_to_start_packing_show(struct device *dev,
293 struct device_attribute *attr, char *buf)
294{
295 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
296 int num_wr_reqs_to_start_packing;
297 int ret;
298
299 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
300
301 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
302
303 mmc_blk_put(md);
304 return ret;
305}
306
307static ssize_t
308num_wr_reqs_to_start_packing_store(struct device *dev,
309 struct device_attribute *attr,
310 const char *buf, size_t count)
311{
312 int value;
313 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
314
315 sscanf(buf, "%d", &value);
316 if (value >= 0)
317 md->queue.num_wr_reqs_to_start_packing = value;
318
319 mmc_blk_put(md);
320 return count;
321}
322
Mark Salyzyn6904e432016-01-28 11:12:25 -0800323#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
324
325static int max_read_speed, max_write_speed, cache_size = 4;
326
327module_param(max_read_speed, int, S_IRUSR | S_IRGRP);
328MODULE_PARM_DESC(max_read_speed, "maximum KB/s read speed 0=off");
329module_param(max_write_speed, int, S_IRUSR | S_IRGRP);
330MODULE_PARM_DESC(max_write_speed, "maximum KB/s write speed 0=off");
331module_param(cache_size, int, S_IRUSR | S_IRGRP);
332MODULE_PARM_DESC(cache_size, "MB high speed memory or SLC cache");
333
334/*
335 * helper macros and expectations:
336 * size - unsigned long number of bytes
337 * jiffies - unsigned long HZ timestamp difference
338 * speed - unsigned KB/s transfer rate
339 */
340#define size_and_speed_to_jiffies(size, speed) \
341 ((size) * HZ / (speed) / 1024UL)
342#define jiffies_and_speed_to_size(jiffies, speed) \
343 (((speed) * (jiffies) * 1024UL) / HZ)
344#define jiffies_and_size_to_speed(jiffies, size) \
345 ((size) * HZ / (jiffies) / 1024UL)
346
347/* Limits to report warning */
348/* jiffies_and_size_to_speed(10*HZ, queue_max_hw_sectors(q) * 512UL) ~ 25 */
349#define MIN_SPEED(q) 250 /* 10 times faster than a floppy disk */
350#define MAX_SPEED(q) jiffies_and_size_to_speed(1, queue_max_sectors(q) * 512UL)
351
352#define speed_valid(speed) ((speed) > 0)
353
354static const char off[] = "off\n";
355
356static int max_speed_show(int speed, char *buf)
357{
358 if (speed)
359 return scnprintf(buf, PAGE_SIZE, "%uKB/s\n", speed);
360 else
361 return scnprintf(buf, PAGE_SIZE, off);
362}
363
364static int max_speed_store(const char *buf, struct request_queue *q)
365{
366 unsigned int limit, set = 0;
367
368 if (!strncasecmp(off, buf, sizeof(off) - 2))
369 return set;
370 if (kstrtouint(buf, 0, &set) || (set > INT_MAX))
371 return -EINVAL;
372 if (set == 0)
373 return set;
374 limit = MAX_SPEED(q);
375 if (set > limit)
376 pr_warn("max speed %u ineffective above %u\n", set, limit);
377 limit = MIN_SPEED(q);
378 if (set < limit)
379 pr_warn("max speed %u painful below %u\n", set, limit);
380 return set;
381}
382
383static ssize_t max_write_speed_show(struct device *dev,
384 struct device_attribute *attr, char *buf)
385{
386 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
387 int ret = max_speed_show(atomic_read(&md->queue.max_write_speed), buf);
388
389 mmc_blk_put(md);
390 return ret;
391}
392
393static ssize_t max_write_speed_store(struct device *dev,
394 struct device_attribute *attr,
395 const char *buf, size_t count)
396{
397 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
398 int set = max_speed_store(buf, md->queue.queue);
399
400 if (set < 0) {
401 mmc_blk_put(md);
402 return set;
403 }
404
405 atomic_set(&md->queue.max_write_speed, set);
406 mmc_blk_put(md);
407 return count;
408}
409
410static const DEVICE_ATTR(max_write_speed, S_IRUGO | S_IWUSR,
411 max_write_speed_show, max_write_speed_store);
412
413static ssize_t max_read_speed_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
415{
416 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
417 int ret = max_speed_show(atomic_read(&md->queue.max_read_speed), buf);
418
419 mmc_blk_put(md);
420 return ret;
421}
422
423static ssize_t max_read_speed_store(struct device *dev,
424 struct device_attribute *attr,
425 const char *buf, size_t count)
426{
427 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
428 int set = max_speed_store(buf, md->queue.queue);
429
430 if (set < 0) {
431 mmc_blk_put(md);
432 return set;
433 }
434
435 atomic_set(&md->queue.max_read_speed, set);
436 mmc_blk_put(md);
437 return count;
438}
439
440static const DEVICE_ATTR(max_read_speed, S_IRUGO | S_IWUSR,
441 max_read_speed_show, max_read_speed_store);
442
443static ssize_t cache_size_show(struct device *dev,
444 struct device_attribute *attr, char *buf)
445{
446 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
447 struct mmc_queue *mq = &md->queue;
448 int cache_size = atomic_read(&mq->cache_size);
449 int ret;
450
451 if (!cache_size)
452 ret = scnprintf(buf, PAGE_SIZE, off);
453 else {
454 int speed = atomic_read(&mq->max_write_speed);
455
456 if (!speed_valid(speed))
457 ret = scnprintf(buf, PAGE_SIZE, "%uMB\n", cache_size);
458 else { /* We accept race between cache_jiffies and cache_used */
459 unsigned long size = jiffies_and_speed_to_size(
460 jiffies - mq->cache_jiffies, speed);
461 long used = atomic_long_read(&mq->cache_used);
462
463 if (size >= used)
464 size = 0;
465 else
466 size = (used - size) * 100 / cache_size
467 / 1024UL / 1024UL;
468
469 ret = scnprintf(buf, PAGE_SIZE, "%uMB %lu%% used\n",
470 cache_size, size);
471 }
472 }
473
474 mmc_blk_put(md);
475 return ret;
476}
477
478static ssize_t cache_size_store(struct device *dev,
479 struct device_attribute *attr,
480 const char *buf, size_t count)
481{
482 struct mmc_blk_data *md;
483 unsigned int set = 0;
484
485 if (strncasecmp(off, buf, sizeof(off) - 2)
486 && (kstrtouint(buf, 0, &set) || (set > INT_MAX)))
487 return -EINVAL;
488
489 md = mmc_blk_get(dev_to_disk(dev));
490 atomic_set(&md->queue.cache_size, set);
491 mmc_blk_put(md);
492 return count;
493}
494
495static const DEVICE_ATTR(cache_size, S_IRUGO | S_IWUSR,
496 cache_size_show, cache_size_store);
497
498/* correct for write-back */
499static long mmc_blk_cache_used(struct mmc_queue *mq, unsigned long waitfor)
500{
501 long used = 0;
502 int speed = atomic_read(&mq->max_write_speed);
503
504 if (speed_valid(speed)) {
505 unsigned long size = jiffies_and_speed_to_size(
506 waitfor - mq->cache_jiffies, speed);
507 used = atomic_long_read(&mq->cache_used);
508
509 if (size >= used)
510 used = 0;
511 else
512 used -= size;
513 }
514
515 atomic_long_set(&mq->cache_used, used);
516 mq->cache_jiffies = waitfor;
517
518 return used;
519}
520
521static void mmc_blk_simulate_delay(
522 struct mmc_queue *mq,
523 struct request *req,
524 unsigned long waitfor)
525{
526 int max_speed;
527
528 if (!req)
529 return;
530
531 max_speed = (rq_data_dir(req) == READ)
532 ? atomic_read(&mq->max_read_speed)
533 : atomic_read(&mq->max_write_speed);
534 if (speed_valid(max_speed)) {
535 unsigned long bytes = blk_rq_bytes(req);
536
537 if (rq_data_dir(req) != READ) {
538 int cache_size = atomic_read(&mq->cache_size);
539
540 if (cache_size) {
541 unsigned long size = cache_size * 1024L * 1024L;
542 long used = mmc_blk_cache_used(mq, waitfor);
543
544 used += bytes;
545 atomic_long_set(&mq->cache_used, used);
546 bytes = 0;
547 if (used > size)
548 bytes = used - size;
549 }
550 }
551 waitfor += size_and_speed_to_jiffies(bytes, max_speed);
552 if (time_is_after_jiffies(waitfor)) {
553 long msecs = jiffies_to_msecs(waitfor - jiffies);
554
555 if (likely(msecs > 0))
556 msleep(msecs);
557 }
558 }
559}
560
561#else
562
563#define mmc_blk_simulate_delay(mq, req, waitfor)
564
565#endif
566
Al Viroa5a15612008-03-02 10:33:30 -0500567static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Al Viroa5a15612008-03-02 10:33:30 -0500569 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 int ret = -ENXIO;
571
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200572 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (md) {
574 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500575 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700577
Al Viroa5a15612008-03-02 10:33:30 -0500578 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700579 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700580 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200583 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 return ret;
586}
587
Al Virodb2a1442013-05-05 21:52:57 -0400588static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Al Viroa5a15612008-03-02 10:33:30 -0500590 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200592 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200594 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
597static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800598mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800600 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
601 geo->heads = 4;
602 geo->sectors = 16;
603 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
John Calixtocb87ea22011-04-26 18:56:29 -0400606struct mmc_blk_ioc_data {
607 struct mmc_ioc_cmd ic;
608 unsigned char *buf;
609 u64 buf_bytes;
610};
611
612static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
613 struct mmc_ioc_cmd __user *user)
614{
615 struct mmc_blk_ioc_data *idata;
616 int err;
617
yalin wang1ff89502015-11-12 19:27:11 +0800618 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400619 if (!idata) {
620 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400621 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400622 }
623
624 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
625 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400626 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400627 }
628
629 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
630 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
631 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400632 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400633 }
634
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300635 if (!idata->buf_bytes) {
636 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100637 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300638 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100639
yalin wang1ff89502015-11-12 19:27:11 +0800640 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400641 if (!idata->buf) {
642 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400643 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400644 }
645
646 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
647 idata->ic.data_ptr, idata->buf_bytes)) {
648 err = -EFAULT;
649 goto copy_err;
650 }
651
652 return idata;
653
654copy_err:
655 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400656idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400657 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400658out:
John Calixtocb87ea22011-04-26 18:56:29 -0400659 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400660}
661
Jon Huntera5f57742015-09-22 10:27:53 +0100662static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
663 struct mmc_blk_ioc_data *idata)
664{
665 struct mmc_ioc_cmd *ic = &idata->ic;
666
667 if (copy_to_user(&(ic_ptr->response), ic->response,
668 sizeof(ic->response)))
669 return -EFAULT;
670
671 if (!idata->ic.write_flag) {
672 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
673 idata->buf, idata->buf_bytes))
674 return -EFAULT;
675 }
676
677 return 0;
678}
679
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200680static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
681 u32 retries_max)
682{
683 int err;
684 u32 retry_count = 0;
685
686 if (!status || !retries_max)
687 return -EINVAL;
688
689 do {
690 err = get_card_status(card, status, 5);
691 if (err)
692 break;
693
694 if (!R1_STATUS(*status) &&
695 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
696 break; /* RPMB programming operation complete */
697
698 /*
699 * Rechedule to give the MMC device a chance to continue
700 * processing the previous command without being polled too
701 * frequently.
702 */
703 usleep_range(1000, 5000);
704 } while (++retry_count < retries_max);
705
706 if (retry_count == retries_max)
707 err = -EPERM;
708
709 return err;
710}
711
Maya Erez775a9362013-04-18 15:41:55 +0300712static int ioctl_do_sanitize(struct mmc_card *card)
713{
714 int err;
715
Ulf Hanssona2d10862013-12-16 14:37:26 +0100716 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300717 pr_warn("%s: %s - SANITIZE is not supported\n",
718 mmc_hostname(card->host), __func__);
719 err = -EOPNOTSUPP;
720 goto out;
721 }
722
723 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
724 mmc_hostname(card->host), __func__);
725
726 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
727 EXT_CSD_SANITIZE_START, 1,
728 MMC_SANITIZE_REQ_TIMEOUT);
729
730 if (err)
731 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
732 mmc_hostname(card->host), __func__, err);
733
734 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
735 __func__);
736out:
737 return err;
738}
739
Jon Huntera5f57742015-09-22 10:27:53 +0100740static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
741 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400742{
John Calixtocb87ea22011-04-26 18:56:29 -0400743 struct mmc_command cmd = {0};
744 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530745 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400746 struct scatterlist sg;
747 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200748 int is_rpmb = false;
749 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400750
Jon Huntera5f57742015-09-22 10:27:53 +0100751 if (!card || !md || !idata)
752 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400753
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200754 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
755 is_rpmb = true;
756
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100757 cmd.opcode = idata->ic.opcode;
758 cmd.arg = idata->ic.arg;
759 cmd.flags = idata->ic.flags;
760
761 if (idata->buf_bytes) {
762 data.sg = &sg;
763 data.sg_len = 1;
764 data.blksz = idata->ic.blksz;
765 data.blocks = idata->ic.blocks;
766
767 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
768
769 if (idata->ic.write_flag)
770 data.flags = MMC_DATA_WRITE;
771 else
772 data.flags = MMC_DATA_READ;
773
774 /* data.flags must already be set before doing this. */
775 mmc_set_data_timeout(&data, card);
776
777 /* Allow overriding the timeout_ns for empirical tuning. */
778 if (idata->ic.data_timeout_ns)
779 data.timeout_ns = idata->ic.data_timeout_ns;
780
781 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
782 /*
783 * Pretend this is a data transfer and rely on the
784 * host driver to compute timeout. When all host
785 * drivers support cmd.cmd_timeout for R1B, this
786 * can be changed to:
787 *
788 * mrq.data = NULL;
789 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
790 */
791 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
792 }
793
794 mrq.data = &data;
795 }
796
797 mrq.cmd = &cmd;
798
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200799 err = mmc_blk_part_switch(card, md);
800 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100801 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200802
John Calixtocb87ea22011-04-26 18:56:29 -0400803 if (idata->ic.is_acmd) {
804 err = mmc_app_cmd(card->host, card);
805 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100806 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400807 }
808
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200809 if (is_rpmb) {
810 err = mmc_set_blockcount(card, data.blocks,
811 idata->ic.write_flag & (1 << 31));
812 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100813 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200814 }
815
Yaniv Gardia82e4842013-06-05 14:13:08 +0300816 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
817 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300818 err = ioctl_do_sanitize(card);
819
820 if (err)
821 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
822 __func__, err);
823
Jon Huntera5f57742015-09-22 10:27:53 +0100824 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300825 }
826
John Calixtocb87ea22011-04-26 18:56:29 -0400827 mmc_wait_for_req(card->host, &mrq);
828
829 if (cmd.error) {
830 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
831 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100832 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400833 }
834 if (data.error) {
835 dev_err(mmc_dev(card->host), "%s: data error %d\n",
836 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100837 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400838 }
839
840 /*
841 * According to the SD specs, some commands require a delay after
842 * issuing the command.
843 */
844 if (idata->ic.postsleep_min_us)
845 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
846
Jon Huntera5f57742015-09-22 10:27:53 +0100847 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400848
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200849 if (is_rpmb) {
850 /*
851 * Ensure RPMB command has completed by polling CMD13
852 * "Send Status".
853 */
854 err = ioctl_rpmb_card_status_poll(card, &status, 5);
855 if (err)
856 dev_err(mmc_dev(card->host),
857 "%s: Card Status=0x%08X, error %d\n",
858 __func__, status, err);
859 }
860
Jon Huntera5f57742015-09-22 10:27:53 +0100861 return err;
862}
863
864static int mmc_blk_ioctl_cmd(struct block_device *bdev,
865 struct mmc_ioc_cmd __user *ic_ptr)
866{
867 struct mmc_blk_ioc_data *idata;
868 struct mmc_blk_data *md;
869 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700870 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100871
Shawn Lin83c742c2016-03-16 18:15:47 +0800872 /*
873 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
874 * whole block device, not on a partition. This prevents overspray
875 * between sibling partitions.
876 */
877 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
878 return -EPERM;
879
Jon Huntera5f57742015-09-22 10:27:53 +0100880 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
881 if (IS_ERR(idata))
882 return PTR_ERR(idata);
883
884 md = mmc_blk_get(bdev->bd_disk);
885 if (!md) {
886 err = -EINVAL;
887 goto cmd_err;
888 }
889
890 card = md->queue.card;
891 if (IS_ERR(card)) {
892 err = PTR_ERR(card);
893 goto cmd_done;
894 }
895
896 mmc_get_card(card);
897
Grant Grundlerb0934102015-09-23 18:30:33 -0700898 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100899
Adrian Hunter3c866562016-05-04 14:38:12 +0300900 /* Always switch back to main area after RPMB access */
901 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
902 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
903
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200904 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400905
Grant Grundlerb0934102015-09-23 18:30:33 -0700906 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100907
John Calixtocb87ea22011-04-26 18:56:29 -0400908cmd_done:
909 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300910cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400911 kfree(idata->buf);
912 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700913 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400914}
915
Jon Huntera5f57742015-09-22 10:27:53 +0100916static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
917 struct mmc_ioc_multi_cmd __user *user)
918{
919 struct mmc_blk_ioc_data **idata = NULL;
920 struct mmc_ioc_cmd __user *cmds = user->cmds;
921 struct mmc_card *card;
922 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -0700923 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100924 __u64 num_of_cmds;
925
Shawn Lin83c742c2016-03-16 18:15:47 +0800926 /*
927 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
928 * whole block device, not on a partition. This prevents overspray
929 * between sibling partitions.
930 */
931 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
932 return -EPERM;
933
Jon Huntera5f57742015-09-22 10:27:53 +0100934 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
935 sizeof(num_of_cmds)))
936 return -EFAULT;
937
938 if (num_of_cmds > MMC_IOC_MAX_CMDS)
939 return -EINVAL;
940
941 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
942 if (!idata)
943 return -ENOMEM;
944
945 for (i = 0; i < num_of_cmds; i++) {
946 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
947 if (IS_ERR(idata[i])) {
948 err = PTR_ERR(idata[i]);
949 num_of_cmds = i;
950 goto cmd_err;
951 }
952 }
953
954 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -0800955 if (!md) {
956 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +0100957 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -0800958 }
Jon Huntera5f57742015-09-22 10:27:53 +0100959
960 card = md->queue.card;
961 if (IS_ERR(card)) {
962 err = PTR_ERR(card);
963 goto cmd_done;
964 }
965
966 mmc_get_card(card);
967
Grant Grundlerb0934102015-09-23 18:30:33 -0700968 for (i = 0; i < num_of_cmds && !ioc_err; i++)
969 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100970
Adrian Hunter3c866562016-05-04 14:38:12 +0300971 /* Always switch back to main area after RPMB access */
972 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
973 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
974
Jon Huntera5f57742015-09-22 10:27:53 +0100975 mmc_put_card(card);
976
977 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700978 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100979 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100980
981cmd_done:
982 mmc_blk_put(md);
983cmd_err:
984 for (i = 0; i < num_of_cmds; i++) {
985 kfree(idata[i]->buf);
986 kfree(idata[i]);
987 }
988 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700989 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100990}
991
John Calixtocb87ea22011-04-26 18:56:29 -0400992static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
993 unsigned int cmd, unsigned long arg)
994{
Jon Huntera5f57742015-09-22 10:27:53 +0100995 switch (cmd) {
996 case MMC_IOC_CMD:
997 return mmc_blk_ioctl_cmd(bdev,
998 (struct mmc_ioc_cmd __user *)arg);
999 case MMC_IOC_MULTI_CMD:
1000 return mmc_blk_ioctl_multi_cmd(bdev,
1001 (struct mmc_ioc_multi_cmd __user *)arg);
1002 default:
1003 return -EINVAL;
1004 }
John Calixtocb87ea22011-04-26 18:56:29 -04001005}
1006
1007#ifdef CONFIG_COMPAT
1008static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
1009 unsigned int cmd, unsigned long arg)
1010{
1011 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
1012}
1013#endif
1014
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001015static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -05001016 .open = mmc_blk_open,
1017 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001018 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -04001020 .ioctl = mmc_blk_ioctl,
1021#ifdef CONFIG_COMPAT
1022 .compat_ioctl = mmc_blk_compat_ioctl,
1023#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024};
1025
Andrei Warkentin371a6892011-04-11 18:10:25 -05001026static inline int mmc_blk_part_switch(struct mmc_card *card,
1027 struct mmc_blk_data *md)
1028{
1029 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001030 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001031
Andrei Warkentin371a6892011-04-11 18:10:25 -05001032 if (main_md->part_curr == md->part_type)
1033 return 0;
1034
1035 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001036 u8 part_config = card->ext_csd.part_config;
1037
Adrian Hunter57da0c02016-05-04 14:38:13 +03001038 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1039 mmc_retune_pause(card->host);
1040
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001041 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1042 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001043
1044 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001045 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001046 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001047 if (ret) {
1048 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1049 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001050 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001051 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001052
1053 card->ext_csd.part_config = part_config;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001054
1055 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1056 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001057 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001058
1059 main_md->part_curr = md->part_type;
1060 return 0;
1061}
1062
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001063static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1064{
1065 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001066 u32 result;
1067 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001068
Venkatraman Sad5fd972011-08-25 00:30:50 +05301069 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001070 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001071 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001072
1073 struct scatterlist sg;
1074
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001075 cmd.opcode = MMC_APP_CMD;
1076 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001077 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001078
1079 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001080 if (err)
1081 return (u32)-1;
1082 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001083 return (u32)-1;
1084
1085 memset(&cmd, 0, sizeof(struct mmc_command));
1086
1087 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1088 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001089 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001090
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001091 data.blksz = 4;
1092 data.blocks = 1;
1093 data.flags = MMC_DATA_READ;
1094 data.sg = &sg;
1095 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301096 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001097
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001098 mrq.cmd = &cmd;
1099 mrq.data = &data;
1100
Ben Dooks051913d2009-06-08 23:33:57 +01001101 blocks = kmalloc(4, GFP_KERNEL);
1102 if (!blocks)
1103 return (u32)-1;
1104
1105 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001106
1107 mmc_wait_for_req(card->host, &mrq);
1108
Ben Dooks051913d2009-06-08 23:33:57 +01001109 result = ntohl(*blocks);
1110 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001111
Ben Dooks051913d2009-06-08 23:33:57 +01001112 if (cmd.error || data.error)
1113 result = (u32)-1;
1114
1115 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001116}
1117
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001118static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001119{
Chris Ball1278dba2011-04-13 23:40:30 -04001120 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001121 int err;
1122
Adrian Hunter504f1912008-10-16 12:55:25 +03001123 cmd.opcode = MMC_SEND_STATUS;
1124 if (!mmc_host_is_spi(card->host))
1125 cmd.arg = card->rca << 16;
1126 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001127 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1128 if (err == 0)
1129 *status = cmd.resp[0];
1130 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001131}
1132
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001133static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001134 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001135{
1136 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1137 int err = 0;
1138 u32 status;
1139
1140 do {
1141 err = get_card_status(card, &status, 5);
1142 if (err) {
1143 pr_err("%s: error %d requesting status\n",
1144 req->rq_disk->disk_name, err);
1145 return err;
1146 }
1147
1148 if (status & R1_ERROR) {
1149 pr_err("%s: %s: error sending status cmd, status %#x\n",
1150 req->rq_disk->disk_name, __func__, status);
1151 *gen_err = 1;
1152 }
1153
Ulf Hansson95a91292014-01-29 13:11:27 +01001154 /* We may rely on the host hw to handle busy detection.*/
1155 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1156 hw_busy_detect)
1157 break;
1158
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001159 /*
1160 * Timeout if the device never becomes ready for data and never
1161 * leaves the program state.
1162 */
1163 if (time_after(jiffies, timeout)) {
1164 pr_err("%s: Card stuck in programming state! %s %s\n",
1165 mmc_hostname(card->host),
1166 req->rq_disk->disk_name, __func__);
1167 return -ETIMEDOUT;
1168 }
1169
1170 /*
1171 * Some cards mishandle the status bits,
1172 * so make sure to check both the busy
1173 * indication and the card state.
1174 */
1175 } while (!(status & R1_READY_FOR_DATA) ||
1176 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1177
1178 return err;
1179}
1180
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001181static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1182 struct request *req, int *gen_err, u32 *stop_status)
1183{
1184 struct mmc_host *host = card->host;
1185 struct mmc_command cmd = {0};
1186 int err;
1187 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1188
1189 /*
1190 * Normally we use R1B responses for WRITE, but in cases where the host
1191 * has specified a max_busy_timeout we need to validate it. A failure
1192 * means we need to prevent the host from doing hw busy detection, which
1193 * is done by converting to a R1 response instead.
1194 */
1195 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1196 use_r1b_resp = false;
1197
1198 cmd.opcode = MMC_STOP_TRANSMISSION;
1199 if (use_r1b_resp) {
1200 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1201 cmd.busy_timeout = timeout_ms;
1202 } else {
1203 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1204 }
1205
1206 err = mmc_wait_for_cmd(host, &cmd, 5);
1207 if (err)
1208 return err;
1209
1210 *stop_status = cmd.resp[0];
1211
1212 /* No need to check card status in case of READ. */
1213 if (rq_data_dir(req) == READ)
1214 return 0;
1215
1216 if (!mmc_host_is_spi(host) &&
1217 (*stop_status & R1_ERROR)) {
1218 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1219 req->rq_disk->disk_name, __func__, *stop_status);
1220 *gen_err = 1;
1221 }
1222
1223 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1224}
1225
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301226#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001227#define ERR_RETRY 2
1228#define ERR_ABORT 1
1229#define ERR_CONTINUE 0
1230
1231static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1232 bool status_valid, u32 status)
1233{
1234 switch (error) {
1235 case -EILSEQ:
1236 /* response crc error, retry the r/w cmd */
1237 pr_err("%s: %s sending %s command, card status %#x\n",
1238 req->rq_disk->disk_name, "response CRC error",
1239 name, status);
1240 return ERR_RETRY;
1241
1242 case -ETIMEDOUT:
1243 pr_err("%s: %s sending %s command, card status %#x\n",
1244 req->rq_disk->disk_name, "timed out", name, status);
1245
1246 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301247 if (!status_valid) {
1248 pr_err("%s: status not valid, retrying timeout\n",
1249 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001250 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301251 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001252
1253 /*
1254 * If it was a r/w cmd crc error, or illegal command
1255 * (eg, issued in wrong state) then retry - we should
1256 * have corrected the state problem above.
1257 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301258 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1259 pr_err("%s: command error, retrying timeout\n",
1260 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001261 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301262 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001263
1264 /* Otherwise abort the command */
1265 return ERR_ABORT;
1266
1267 default:
1268 /* We don't understand the error code the driver gave us */
1269 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1270 req->rq_disk->disk_name, error, status);
1271 return ERR_ABORT;
1272 }
1273}
1274
1275/*
1276 * Initial r/w and stop cmd error recovery.
1277 * We don't know whether the card received the r/w cmd or not, so try to
1278 * restore things back to a sane state. Essentially, we do this as follows:
1279 * - Obtain card status. If the first attempt to obtain card status fails,
1280 * the status word will reflect the failed status cmd, not the failed
1281 * r/w cmd. If we fail to obtain card status, it suggests we can no
1282 * longer communicate with the card.
1283 * - Check the card state. If the card received the cmd but there was a
1284 * transient problem with the response, it might still be in a data transfer
1285 * mode. Try to send it a stop command. If this fails, we can't recover.
1286 * - If the r/w cmd failed due to a response CRC error, it was probably
1287 * transient, so retry the cmd.
1288 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1289 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1290 * illegal cmd, retry.
1291 * Otherwise we don't understand what happened, so abort.
1292 */
1293static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001294 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001295{
1296 bool prev_cmd_status_valid = true;
1297 u32 status, stop_status = 0;
1298 int err, retry;
1299
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301300 if (mmc_card_removed(card))
1301 return ERR_NOMEDIUM;
1302
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001303 /*
1304 * Try to get card status which indicates both the card state
1305 * and why there was no response. If the first attempt fails,
1306 * we can't be sure the returned status is for the r/w command.
1307 */
1308 for (retry = 2; retry >= 0; retry--) {
1309 err = get_card_status(card, &status, 0);
1310 if (!err)
1311 break;
1312
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001313 /* Re-tune if needed */
1314 mmc_retune_recheck(card->host);
1315
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001316 prev_cmd_status_valid = false;
1317 pr_err("%s: error %d sending status command, %sing\n",
1318 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1319 }
1320
1321 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301322 if (err) {
1323 /* Check if the card is removed */
1324 if (mmc_detect_card_removed(card->host))
1325 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001326 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301327 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001328
Adrian Hunter67716322011-08-29 16:42:15 +03001329 /* Flag ECC errors */
1330 if ((status & R1_CARD_ECC_FAILED) ||
1331 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1332 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1333 *ecc_err = 1;
1334
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001335 /* Flag General errors */
1336 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1337 if ((status & R1_ERROR) ||
1338 (brq->stop.resp[0] & R1_ERROR)) {
1339 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1340 req->rq_disk->disk_name, __func__,
1341 brq->stop.resp[0], status);
1342 *gen_err = 1;
1343 }
1344
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001345 /*
1346 * Check the current card state. If it is in some data transfer
1347 * mode, tell it to stop (and hopefully transition back to TRAN.)
1348 */
1349 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1350 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001351 err = send_stop(card,
1352 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1353 req, gen_err, &stop_status);
1354 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001355 pr_err("%s: error %d sending stop command\n",
1356 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001357 /*
1358 * If the stop cmd also timed out, the card is probably
1359 * not present, so abort. Other errors are bad news too.
1360 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001361 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001362 }
1363
Adrian Hunter67716322011-08-29 16:42:15 +03001364 if (stop_status & R1_CARD_ECC_FAILED)
1365 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001366 }
1367
1368 /* Check for set block count errors */
1369 if (brq->sbc.error)
1370 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1371 prev_cmd_status_valid, status);
1372
1373 /* Check for r/w command errors */
1374 if (brq->cmd.error)
1375 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1376 prev_cmd_status_valid, status);
1377
Adrian Hunter67716322011-08-29 16:42:15 +03001378 /* Data errors */
1379 if (!brq->stop.error)
1380 return ERR_CONTINUE;
1381
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001382 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001383 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 +01001384 req->rq_disk->disk_name, brq->stop.error,
1385 brq->cmd.resp[0], status);
1386
1387 /*
1388 * Subsitute in our own stop status as this will give the error
1389 * state which happened during the execution of the r/w command.
1390 */
1391 if (stop_status) {
1392 brq->stop.resp[0] = stop_status;
1393 brq->stop.error = 0;
1394 }
1395 return ERR_CONTINUE;
1396}
1397
Adrian Hunter67716322011-08-29 16:42:15 +03001398static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1399 int type)
1400{
1401 int err;
1402
1403 if (md->reset_done & type)
1404 return -EEXIST;
1405
1406 md->reset_done |= type;
1407 err = mmc_hw_reset(host);
1408 /* Ensure we switch back to the correct partition */
1409 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001410 struct mmc_blk_data *main_md =
1411 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001412 int part_err;
1413
1414 main_md->part_curr = main_md->part_type;
1415 part_err = mmc_blk_part_switch(host->card, md);
1416 if (part_err) {
1417 /*
1418 * We have failed to get back into the correct
1419 * partition, so we need to abort the whole request.
1420 */
1421 return -ENODEV;
1422 }
1423 }
1424 return err;
1425}
1426
1427static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1428{
1429 md->reset_done &= ~type;
1430}
1431
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001432int mmc_access_rpmb(struct mmc_queue *mq)
1433{
1434 struct mmc_blk_data *md = mq->data;
1435 /*
1436 * If this is a RPMB partition access, return ture
1437 */
1438 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1439 return true;
1440
1441 return false;
1442}
1443
Adrian Hunterbd788c92010-08-11 14:17:47 -07001444static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1445{
1446 struct mmc_blk_data *md = mq->data;
1447 struct mmc_card *card = md->queue.card;
1448 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001449 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001450
Adrian Hunterbd788c92010-08-11 14:17:47 -07001451 if (!mmc_can_erase(card)) {
1452 err = -EOPNOTSUPP;
1453 goto out;
1454 }
1455
1456 from = blk_rq_pos(req);
1457 nr = blk_rq_sectors(req);
1458
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001459 if (mmc_can_discard(card))
1460 arg = MMC_DISCARD_ARG;
1461 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001462 arg = MMC_TRIM_ARG;
1463 else
1464 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001465retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001466 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1467 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1468 INAND_CMD38_ARG_EXT_CSD,
1469 arg == MMC_TRIM_ARG ?
1470 INAND_CMD38_ARG_TRIM :
1471 INAND_CMD38_ARG_ERASE,
1472 0);
1473 if (err)
1474 goto out;
1475 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001476 err = mmc_erase(card, from, nr, arg);
1477out:
Adrian Hunter67716322011-08-29 16:42:15 +03001478 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1479 goto retry;
1480 if (!err)
1481 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301482 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001483
Adrian Hunterbd788c92010-08-11 14:17:47 -07001484 return err ? 0 : 1;
1485}
1486
Adrian Hunter49804542010-08-11 14:17:50 -07001487static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1488 struct request *req)
1489{
1490 struct mmc_blk_data *md = mq->data;
1491 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001492 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001493 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001494
Maya Erez775a9362013-04-18 15:41:55 +03001495 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001496 err = -EOPNOTSUPP;
1497 goto out;
1498 }
1499
1500 from = blk_rq_pos(req);
1501 nr = blk_rq_sectors(req);
1502
Maya Erez775a9362013-04-18 15:41:55 +03001503 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1504 arg = MMC_SECURE_TRIM1_ARG;
1505 else
1506 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001507
Adrian Hunter67716322011-08-29 16:42:15 +03001508retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001509 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1510 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1511 INAND_CMD38_ARG_EXT_CSD,
1512 arg == MMC_SECURE_TRIM1_ARG ?
1513 INAND_CMD38_ARG_SECTRIM1 :
1514 INAND_CMD38_ARG_SECERASE,
1515 0);
1516 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001517 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001518 }
Adrian Hunter28302812012-04-05 14:45:48 +03001519
Adrian Hunter49804542010-08-11 14:17:50 -07001520 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001521 if (err == -EIO)
1522 goto out_retry;
1523 if (err)
1524 goto out;
1525
1526 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001527 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1528 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1529 INAND_CMD38_ARG_EXT_CSD,
1530 INAND_CMD38_ARG_SECTRIM2,
1531 0);
1532 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001533 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001534 }
Adrian Hunter28302812012-04-05 14:45:48 +03001535
Adrian Hunter49804542010-08-11 14:17:50 -07001536 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001537 if (err == -EIO)
1538 goto out_retry;
1539 if (err)
1540 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001541 }
Adrian Hunter28302812012-04-05 14:45:48 +03001542
Adrian Hunter28302812012-04-05 14:45:48 +03001543out_retry:
1544 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001545 goto retry;
1546 if (!err)
1547 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001548out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301549 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001550
Adrian Hunter49804542010-08-11 14:17:50 -07001551 return err ? 0 : 1;
1552}
1553
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001554static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1555{
1556 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001557 struct mmc_card *card = md->queue.card;
1558 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001559
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001560 ret = mmc_flush_cache(card);
1561 if (ret)
1562 ret = -EIO;
1563
Mark Salyzyn6904e432016-01-28 11:12:25 -08001564#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
1565 else if (atomic_read(&mq->cache_size)) {
1566 long used = mmc_blk_cache_used(mq, jiffies);
1567
1568 if (used) {
1569 int speed = atomic_read(&mq->max_write_speed);
1570
1571 if (speed_valid(speed)) {
1572 unsigned long msecs = jiffies_to_msecs(
1573 size_and_speed_to_jiffies(
1574 used, speed));
1575 if (msecs)
1576 msleep(msecs);
1577 }
1578 }
1579 }
1580#endif
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301581 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001582
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001583 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001584}
1585
1586/*
1587 * Reformat current write as a reliable write, supporting
1588 * both legacy and the enhanced reliable write MMC cards.
1589 * In each transfer we'll handle only as much as a single
1590 * reliable write can handle, thus finish the request in
1591 * partial completions.
1592 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001593static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1594 struct mmc_card *card,
1595 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001596{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001597 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1598 /* Legacy mode imposes restrictions on transfers. */
1599 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1600 brq->data.blocks = 1;
1601
1602 if (brq->data.blocks > card->ext_csd.rel_sectors)
1603 brq->data.blocks = card->ext_csd.rel_sectors;
1604 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1605 brq->data.blocks = 1;
1606 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001607}
1608
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001609#define CMD_ERRORS \
1610 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1611 R1_ADDRESS_ERROR | /* Misaligned address */ \
1612 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1613 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1614 R1_CC_ERROR | /* Card controller error */ \
1615 R1_ERROR) /* General/unknown error */
1616
Per Forlinee8a43a2011-07-01 18:55:33 +02001617static int mmc_blk_err_check(struct mmc_card *card,
1618 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001619{
Per Forlinee8a43a2011-07-01 18:55:33 +02001620 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1621 mmc_active);
1622 struct mmc_blk_request *brq = &mq_mrq->brq;
1623 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001624 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001625 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001626
1627 /*
1628 * sbc.error indicates a problem with the set block count
1629 * command. No data will have been transferred.
1630 *
1631 * cmd.error indicates a problem with the r/w command. No
1632 * data will have been transferred.
1633 *
1634 * stop.error indicates a problem with the stop command. Data
1635 * may have been transferred, or may still be transferring.
1636 */
Adrian Hunter67716322011-08-29 16:42:15 +03001637 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1638 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001639 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001640 case ERR_RETRY:
1641 return MMC_BLK_RETRY;
1642 case ERR_ABORT:
1643 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301644 case ERR_NOMEDIUM:
1645 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001646 case ERR_CONTINUE:
1647 break;
1648 }
1649 }
1650
1651 /*
1652 * Check for errors relating to the execution of the
1653 * initial command - such as address errors. No data
1654 * has been transferred.
1655 */
1656 if (brq->cmd.resp[0] & CMD_ERRORS) {
1657 pr_err("%s: r/w command failed, status = %#x\n",
1658 req->rq_disk->disk_name, brq->cmd.resp[0]);
1659 return MMC_BLK_ABORT;
1660 }
1661
1662 /*
1663 * Everything else is either success, or a data error of some
1664 * kind. If it was a write, we may have transitioned to
1665 * program mode, which we have to wait for it to complete.
1666 */
1667 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001668 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001669
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001670 /* Check stop command response */
1671 if (brq->stop.resp[0] & R1_ERROR) {
1672 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1673 req->rq_disk->disk_name, __func__,
1674 brq->stop.resp[0]);
1675 gen_err = 1;
1676 }
1677
Ulf Hansson95a91292014-01-29 13:11:27 +01001678 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1679 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001680 if (err)
1681 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001682 }
1683
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001684 /* if general error occurs, retry the write operation. */
1685 if (gen_err) {
1686 pr_warn("%s: retrying write for general error\n",
1687 req->rq_disk->disk_name);
1688 return MMC_BLK_RETRY;
1689 }
1690
Per Forlind78d4a82011-07-01 18:55:30 +02001691 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001692 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001693 pr_debug("%s: retrying because a re-tune was needed\n",
1694 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001695 brq->retune_retry_done = 1;
1696 return MMC_BLK_RETRY;
1697 }
Per Forlind78d4a82011-07-01 18:55:30 +02001698 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1699 req->rq_disk->disk_name, brq->data.error,
1700 (unsigned)blk_rq_pos(req),
1701 (unsigned)blk_rq_sectors(req),
1702 brq->cmd.resp[0], brq->stop.resp[0]);
1703
1704 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001705 if (ecc_err)
1706 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001707 return MMC_BLK_DATA_ERR;
1708 } else {
1709 return MMC_BLK_CMD_ERR;
1710 }
1711 }
1712
Adrian Hunter67716322011-08-29 16:42:15 +03001713 if (!brq->data.bytes_xfered)
1714 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001715
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001716 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1717 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1718 return MMC_BLK_PARTIAL;
1719 else
1720 return MMC_BLK_SUCCESS;
1721 }
1722
Adrian Hunter67716322011-08-29 16:42:15 +03001723 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1724 return MMC_BLK_PARTIAL;
1725
1726 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001727}
1728
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001729static int mmc_blk_packed_err_check(struct mmc_card *card,
1730 struct mmc_async_req *areq)
1731{
1732 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1733 mmc_active);
1734 struct request *req = mq_rq->req;
1735 struct mmc_packed *packed = mq_rq->packed;
1736 int err, check, status;
1737 u8 *ext_csd;
1738
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001739 packed->retries--;
1740 check = mmc_blk_err_check(card, areq);
1741 err = get_card_status(card, &status, 0);
1742 if (err) {
1743 pr_err("%s: error %d sending status command\n",
1744 req->rq_disk->disk_name, err);
1745 return MMC_BLK_ABORT;
1746 }
1747
1748 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02001749 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001750 if (err) {
1751 pr_err("%s: error %d sending ext_csd\n",
1752 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02001753 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001754 }
1755
1756 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1757 EXT_CSD_PACKED_FAILURE) &&
1758 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1759 EXT_CSD_PACKED_GENERIC_ERROR)) {
1760 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1761 EXT_CSD_PACKED_INDEXED_ERROR) {
1762 packed->idx_failure =
1763 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1764 check = MMC_BLK_PARTIAL;
1765 }
1766 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1767 "failure index: %d\n",
1768 req->rq_disk->disk_name, packed->nr_entries,
1769 packed->blocks, packed->idx_failure);
1770 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001771 kfree(ext_csd);
1772 }
1773
1774 return check;
1775}
1776
Per Forlin54d49d72011-07-01 18:55:29 +02001777static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1778 struct mmc_card *card,
1779 int disable_multi,
1780 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Per Forlin54d49d72011-07-01 18:55:29 +02001782 u32 readcmd, writecmd;
1783 struct mmc_blk_request *brq = &mqrq->brq;
1784 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301786 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001788 /*
1789 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001790 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001791 */
Luca Porziod3df0462015-11-06 15:12:26 +00001792 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001793 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001794 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001795
Per Forlin54d49d72011-07-01 18:55:29 +02001796 memset(brq, 0, sizeof(struct mmc_blk_request));
1797 brq->mrq.cmd = &brq->cmd;
1798 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Per Forlin54d49d72011-07-01 18:55:29 +02001800 brq->cmd.arg = blk_rq_pos(req);
1801 if (!mmc_card_blockaddr(card))
1802 brq->cmd.arg <<= 9;
1803 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1804 brq->data.blksz = 512;
1805 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1806 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001807 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Asutosh Dasf0665412012-07-27 18:10:19 +05301809 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02001810 /*
1811 * The block layer doesn't support all sector count
1812 * restrictions, so we need to be prepared for too big
1813 * requests.
1814 */
1815 if (brq->data.blocks > card->host->max_blk_count)
1816 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001818 if (brq->data.blocks > 1) {
1819 /*
1820 * After a read error, we redo the request one sector
1821 * at a time in order to accurately determine which
1822 * sectors can be read successfully.
1823 */
1824 if (disable_multi)
1825 brq->data.blocks = 1;
1826
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001827 /*
1828 * Some controllers have HW issues while operating
1829 * in multiple I/O mode
1830 */
1831 if (card->host->ops->multi_io_quirk)
1832 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1833 (rq_data_dir(req) == READ) ?
1834 MMC_DATA_READ : MMC_DATA_WRITE,
1835 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001836 }
Per Forlin54d49d72011-07-01 18:55:29 +02001837
1838 if (brq->data.blocks > 1 || do_rel_wr) {
1839 /* SPI multiblock writes terminate using a special
1840 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001841 */
Per Forlin54d49d72011-07-01 18:55:29 +02001842 if (!mmc_host_is_spi(card->host) ||
1843 rq_data_dir(req) == READ)
1844 brq->mrq.stop = &brq->stop;
1845 readcmd = MMC_READ_MULTIPLE_BLOCK;
1846 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1847 } else {
1848 brq->mrq.stop = NULL;
1849 readcmd = MMC_READ_SINGLE_BLOCK;
1850 writecmd = MMC_WRITE_BLOCK;
1851 }
1852 if (rq_data_dir(req) == READ) {
1853 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001854 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001855 if (brq->mrq.stop)
1856 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1857 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001858 } else {
1859 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09001860 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001861 if (brq->mrq.stop)
1862 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1863 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001864 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001865
Per Forlin54d49d72011-07-01 18:55:29 +02001866 if (do_rel_wr)
1867 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001868
Per Forlin54d49d72011-07-01 18:55:29 +02001869 /*
Saugata Das42659002011-12-21 13:09:17 +05301870 * Data tag is used only during writing meta data to speed
1871 * up write and any subsequent read of this meta data
1872 */
1873 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1874 (req->cmd_flags & REQ_META) &&
1875 (rq_data_dir(req) == WRITE) &&
1876 ((brq->data.blocks * brq->data.blksz) >=
1877 card->ext_csd.data_tag_unit_size);
1878
1879 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001880 * Pre-defined multi-block transfers are preferable to
1881 * open ended-ones (and necessary for reliable writes).
1882 * However, it is not sufficient to just send CMD23,
1883 * and avoid the final CMD12, as on an error condition
1884 * CMD12 (stop) needs to be sent anyway. This, coupled
1885 * with Auto-CMD23 enhancements provided by some
1886 * hosts, means that the complexity of dealing
1887 * with this is best left to the host. If CMD23 is
1888 * supported by card and host, we'll fill sbc in and let
1889 * the host deal with handling it correctly. This means
1890 * that for hosts that don't expose MMC_CAP_CMD23, no
1891 * change of behavior will be observed.
1892 *
1893 * N.B: Some MMC cards experience perf degradation.
1894 * We'll avoid using CMD23-bounded multiblock writes for
1895 * these, while retaining features like reliable writes.
1896 */
Saugata Das42659002011-12-21 13:09:17 +05301897 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1898 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1899 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001900 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1901 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301902 (do_rel_wr ? (1 << 31) : 0) |
1903 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001904 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1905 brq->mrq.sbc = &brq->sbc;
1906 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001907
Per Forlin54d49d72011-07-01 18:55:29 +02001908 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001909
Per Forlin54d49d72011-07-01 18:55:29 +02001910 brq->data.sg = mqrq->sg;
1911 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001912
Per Forlin54d49d72011-07-01 18:55:29 +02001913 /*
1914 * Adjust the sg list so it is the same size as the
1915 * request.
1916 */
1917 if (brq->data.blocks != blk_rq_sectors(req)) {
1918 int i, data_size = brq->data.blocks << 9;
1919 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001920
Per Forlin54d49d72011-07-01 18:55:29 +02001921 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1922 data_size -= sg->length;
1923 if (data_size <= 0) {
1924 sg->length += data_size;
1925 i++;
1926 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001927 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001928 }
Per Forlin54d49d72011-07-01 18:55:29 +02001929 brq->data.sg_len = i;
1930 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001931
Per Forlinee8a43a2011-07-01 18:55:33 +02001932 mqrq->mmc_active.mrq = &brq->mrq;
1933 mqrq->mmc_active.err_check = mmc_blk_err_check;
1934
Per Forlin54d49d72011-07-01 18:55:29 +02001935 mmc_queue_bounce_pre(mqrq);
1936}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001938static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1939 struct mmc_card *card)
1940{
1941 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1942 unsigned int max_seg_sz = queue_max_segment_size(q);
1943 unsigned int len, nr_segs = 0;
1944
1945 do {
1946 len = min(hdr_sz, max_seg_sz);
1947 hdr_sz -= len;
1948 nr_segs++;
1949 } while (hdr_sz);
1950
1951 return nr_segs;
1952}
1953
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02001954static void mmc_blk_write_packing_control(struct mmc_queue *mq,
1955 struct request *req)
1956{
1957 struct mmc_host *host = mq->card->host;
1958 int data_dir;
1959
1960 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
1961 return;
1962
1963 /*
1964 * In case the packing control is not supported by the host, it should
1965 * not have an effect on the write packing. Therefore we have to enable
1966 * the write packing
1967 */
1968 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
1969 mq->wr_packing_enabled = true;
1970 return;
1971 }
1972
1973 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
1974 if (mq->num_of_potential_packed_wr_reqs >
1975 mq->num_wr_reqs_to_start_packing)
1976 mq->wr_packing_enabled = true;
1977 return;
1978 }
1979
1980 data_dir = rq_data_dir(req);
1981
1982 if (data_dir == READ) {
1983 mq->num_of_potential_packed_wr_reqs = 0;
1984 mq->wr_packing_enabled = false;
1985 return;
1986 } else if (data_dir == WRITE) {
1987 mq->num_of_potential_packed_wr_reqs++;
1988 }
1989
1990 if (mq->num_of_potential_packed_wr_reqs >
1991 mq->num_wr_reqs_to_start_packing)
1992 mq->wr_packing_enabled = true;
1993}
1994
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001995static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1996{
1997 struct request_queue *q = mq->queue;
1998 struct mmc_card *card = mq->card;
1999 struct request *cur = req, *next = NULL;
2000 struct mmc_blk_data *md = mq->data;
2001 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2002 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2003 unsigned int req_sectors = 0, phys_segments = 0;
2004 unsigned int max_blk_count, max_phys_segs;
2005 bool put_back = true;
2006 u8 max_packed_rw = 0;
2007 u8 reqs = 0;
2008
Shawn Lin96e52da2016-08-26 08:49:55 +08002009 /*
2010 * We don't need to check packed for any further
2011 * operation of packed stuff as we set MMC_PACKED_NONE
2012 * and return zero for reqs if geting null packed. Also
2013 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2014 * it again when removing blk req.
2015 */
2016 if (!mqrq->packed) {
2017 md->flags &= (~MMC_BLK_PACKED_CMD);
2018 goto no_packed;
2019 }
2020
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002021 if (!(md->flags & MMC_BLK_PACKED_CMD))
2022 goto no_packed;
2023
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002024 if (!mq->wr_packing_enabled)
2025 goto no_packed;
2026
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002027 if ((rq_data_dir(cur) == WRITE) &&
2028 mmc_host_packed_wr(card->host))
2029 max_packed_rw = card->ext_csd.max_packed_writes;
2030
2031 if (max_packed_rw == 0)
2032 goto no_packed;
2033
2034 if (mmc_req_rel_wr(cur) &&
2035 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2036 goto no_packed;
2037
2038 if (mmc_large_sector(card) &&
2039 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2040 goto no_packed;
2041
2042 mmc_blk_clear_packed(mqrq);
2043
2044 max_blk_count = min(card->host->max_blk_count,
2045 card->host->max_req_size >> 9);
2046 if (unlikely(max_blk_count > 0xffff))
2047 max_blk_count = 0xffff;
2048
2049 max_phys_segs = queue_max_segments(q);
2050 req_sectors += blk_rq_sectors(cur);
2051 phys_segments += cur->nr_phys_segments;
2052
2053 if (rq_data_dir(cur) == WRITE) {
2054 req_sectors += mmc_large_sector(card) ? 8 : 1;
2055 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2056 }
2057
2058 do {
2059 if (reqs >= max_packed_rw - 1) {
2060 put_back = false;
2061 break;
2062 }
2063
2064 spin_lock_irq(q->queue_lock);
2065 next = blk_fetch_request(q);
2066 spin_unlock_irq(q->queue_lock);
2067 if (!next) {
2068 put_back = false;
2069 break;
2070 }
2071
2072 if (mmc_large_sector(card) &&
2073 !IS_ALIGNED(blk_rq_sectors(next), 8))
2074 break;
2075
Mike Christie3a5e02c2016-06-05 14:32:23 -05002076 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002077 req_op(next) == REQ_OP_SECURE_ERASE ||
Mike Christie3a5e02c2016-06-05 14:32:23 -05002078 req_op(next) == REQ_OP_FLUSH)
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002079 break;
2080
2081 if (rq_data_dir(cur) != rq_data_dir(next))
2082 break;
2083
2084 if (mmc_req_rel_wr(next) &&
2085 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2086 break;
2087
2088 req_sectors += blk_rq_sectors(next);
2089 if (req_sectors > max_blk_count)
2090 break;
2091
2092 phys_segments += next->nr_phys_segments;
2093 if (phys_segments > max_phys_segs)
2094 break;
2095
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002096 if (rq_data_dir(next) == WRITE)
2097 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002098 list_add_tail(&next->queuelist, &mqrq->packed->list);
2099 cur = next;
2100 reqs++;
2101 } while (1);
2102
2103 if (put_back) {
2104 spin_lock_irq(q->queue_lock);
2105 blk_requeue_request(q, next);
2106 spin_unlock_irq(q->queue_lock);
2107 }
2108
2109 if (reqs > 0) {
2110 list_add(&req->queuelist, &mqrq->packed->list);
2111 mqrq->packed->nr_entries = ++reqs;
2112 mqrq->packed->retries = reqs;
2113 return reqs;
2114 }
2115
2116no_packed:
2117 mqrq->cmd_type = MMC_PACKED_NONE;
2118 return 0;
2119}
2120
2121static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2122 struct mmc_card *card,
2123 struct mmc_queue *mq)
2124{
2125 struct mmc_blk_request *brq = &mqrq->brq;
2126 struct request *req = mqrq->req;
2127 struct request *prq;
2128 struct mmc_blk_data *md = mq->data;
2129 struct mmc_packed *packed = mqrq->packed;
2130 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002131 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002132 u8 hdr_blocks;
2133 u8 i = 1;
2134
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002135 mqrq->cmd_type = MMC_PACKED_WRITE;
2136 packed->blocks = 0;
2137 packed->idx_failure = MMC_PACKED_NR_IDX;
2138
2139 packed_cmd_hdr = packed->cmd_hdr;
2140 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002141 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2142 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002143 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2144
2145 /*
2146 * Argument for each entry of packed group
2147 */
2148 list_for_each_entry(prq, &packed->list, queuelist) {
2149 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2150 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2151 (prq->cmd_flags & REQ_META) &&
2152 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002153 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002154 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002155 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002156 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2157 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002158 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002159 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002160 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002161 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002162 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002163 packed->blocks += blk_rq_sectors(prq);
2164 i++;
2165 }
2166
2167 memset(brq, 0, sizeof(struct mmc_blk_request));
2168 brq->mrq.cmd = &brq->cmd;
2169 brq->mrq.data = &brq->data;
2170 brq->mrq.sbc = &brq->sbc;
2171 brq->mrq.stop = &brq->stop;
2172
2173 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2174 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2175 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2176
2177 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2178 brq->cmd.arg = blk_rq_pos(req);
2179 if (!mmc_card_blockaddr(card))
2180 brq->cmd.arg <<= 9;
2181 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2182
2183 brq->data.blksz = 512;
2184 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002185 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302186 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002187
2188 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2189 brq->stop.arg = 0;
2190 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2191
2192 mmc_set_data_timeout(&brq->data, card);
2193
2194 brq->data.sg = mqrq->sg;
2195 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2196
2197 mqrq->mmc_active.mrq = &brq->mrq;
2198 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2199
2200 mmc_queue_bounce_pre(mqrq);
2201}
2202
Adrian Hunter67716322011-08-29 16:42:15 +03002203static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2204 struct mmc_blk_request *brq, struct request *req,
2205 int ret)
2206{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002207 struct mmc_queue_req *mq_rq;
2208 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2209
Adrian Hunter67716322011-08-29 16:42:15 +03002210 /*
2211 * If this is an SD card and we're writing, we can first
2212 * mark the known good sectors as ok.
2213 *
2214 * If the card is not SD, we can still ok written sectors
2215 * as reported by the controller (which might be less than
2216 * the real number of written sectors, but never more).
2217 */
2218 if (mmc_card_sd(card)) {
2219 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302220 if (!brq->data.fault_injected) {
2221 blocks = mmc_sd_num_wr_blocks(card);
2222 if (blocks != (u32)-1)
2223 ret = blk_end_request(req, 0, blocks << 9);
2224 } else
2225 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002226 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002227 if (!mmc_packed_cmd(mq_rq->cmd_type))
2228 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002229 }
2230 return ret;
2231}
2232
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002233static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2234{
2235 struct request *prq;
2236 struct mmc_packed *packed = mq_rq->packed;
2237 int idx = packed->idx_failure, i = 0;
2238 int ret = 0;
2239
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002240 while (!list_empty(&packed->list)) {
2241 prq = list_entry_rq(packed->list.next);
2242 if (idx == i) {
2243 /* retry from error index */
2244 packed->nr_entries -= idx;
2245 mq_rq->req = prq;
2246 ret = 1;
2247
2248 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2249 list_del_init(&prq->queuelist);
2250 mmc_blk_clear_packed(mq_rq);
2251 }
2252 return ret;
2253 }
2254 list_del_init(&prq->queuelist);
2255 blk_end_request(prq, 0, blk_rq_bytes(prq));
2256 i++;
2257 }
2258
2259 mmc_blk_clear_packed(mq_rq);
2260 return ret;
2261}
2262
2263static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2264{
2265 struct request *prq;
2266 struct mmc_packed *packed = mq_rq->packed;
2267
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002268 while (!list_empty(&packed->list)) {
2269 prq = list_entry_rq(packed->list.next);
2270 list_del_init(&prq->queuelist);
2271 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2272 }
2273
2274 mmc_blk_clear_packed(mq_rq);
2275}
2276
2277static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2278 struct mmc_queue_req *mq_rq)
2279{
2280 struct request *prq;
2281 struct request_queue *q = mq->queue;
2282 struct mmc_packed *packed = mq_rq->packed;
2283
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002284 while (!list_empty(&packed->list)) {
2285 prq = list_entry_rq(packed->list.prev);
2286 if (prq->queuelist.prev != &packed->list) {
2287 list_del_init(&prq->queuelist);
2288 spin_lock_irq(q->queue_lock);
2289 blk_requeue_request(mq->queue, prq);
2290 spin_unlock_irq(q->queue_lock);
2291 } else {
2292 list_del_init(&prq->queuelist);
2293 }
2294 }
2295
2296 mmc_blk_clear_packed(mq_rq);
2297}
2298
Per Forlinee8a43a2011-07-01 18:55:33 +02002299static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02002300{
2301 struct mmc_blk_data *md = mq->data;
2302 struct mmc_card *card = md->queue.card;
2303 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002304 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002305 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002306 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302307 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02002308 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002309 const u8 packed_nr = 2;
2310 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002311#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2312 unsigned long waitfor = jiffies;
2313#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02002314
2315 if (!rqc && !mq->mqrq_prev->req)
2316 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002317
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002318 if (rqc)
2319 reqs = mmc_blk_prep_packed_list(mq, rqc);
2320
Per Forlin54d49d72011-07-01 18:55:29 +02002321 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002322 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302323 /*
2324 * When 4KB native sector is enabled, only 8 blocks
2325 * multiple read or write is allowed
2326 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00002327 if (mmc_large_sector(card) &&
2328 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302329 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
2330 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002331 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302332 goto cmd_abort;
2333 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002334
2335 if (reqs >= packed_nr)
2336 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2337 card, mq);
2338 else
2339 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002340 areq = &mq->mqrq_cur->mmc_active;
2341 } else
2342 areq = NULL;
2343 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002344 if (!areq) {
2345 if (status == MMC_BLK_NEW_REQUEST)
2346 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02002347 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002348 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002349
Per Forlinee8a43a2011-07-01 18:55:33 +02002350 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2351 brq = &mq_rq->brq;
2352 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002353 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002354 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002355
Per Forlind78d4a82011-07-01 18:55:30 +02002356 switch (status) {
2357 case MMC_BLK_SUCCESS:
2358 case MMC_BLK_PARTIAL:
2359 /*
2360 * A block was successfully transferred.
2361 */
Adrian Hunter67716322011-08-29 16:42:15 +03002362 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002363
Mark Salyzyn6904e432016-01-28 11:12:25 -08002364 mmc_blk_simulate_delay(mq, rqc, waitfor);
2365
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002366 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2367 ret = mmc_blk_end_packed_req(mq_rq);
2368 break;
2369 } else {
2370 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002371 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002372 }
2373
Adrian Hunter67716322011-08-29 16:42:15 +03002374 /*
2375 * If the blk_end_request function returns non-zero even
2376 * though all data has been transferred and no errors
2377 * were returned by the host controller, it's a bug.
2378 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002379 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302380 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002381 __func__, blk_rq_bytes(req),
2382 brq->data.bytes_xfered);
2383 rqc = NULL;
2384 goto cmd_abort;
2385 }
Per Forlind78d4a82011-07-01 18:55:30 +02002386 break;
2387 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002388 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002389 if (mmc_blk_reset(md, card->host, type))
2390 goto cmd_abort;
2391 if (!ret)
2392 goto start_new_req;
2393 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002394 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002395 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a82011-07-01 18:55:30 +02002396 if (retry++ < 5)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002397 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002398 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002399 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002400 if (!mmc_blk_reset(md, card->host, type))
2401 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002402 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002403 case MMC_BLK_DATA_ERR: {
2404 int err;
2405
2406 err = mmc_blk_reset(md, card->host, type);
2407 if (!err)
2408 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05302409 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002410 }
2411 case MMC_BLK_ECC_ERR:
2412 if (brq->data.blocks > 1) {
2413 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002414 pr_warn("%s: retrying using single block read\n",
2415 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002416 disable_multi = 1;
2417 break;
2418 }
Per Forlind78d4a82011-07-01 18:55:30 +02002419 /*
2420 * After an error, we redo I/O one sector at a
2421 * time, so we only reach here after trying to
2422 * read a single sector.
2423 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302424 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002425 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002426 if (!ret)
2427 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002428 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302429 case MMC_BLK_NOMEDIUM:
2430 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002431 default:
2432 pr_err("%s: Unhandled return value (%d)",
2433 req->rq_disk->disk_name, status);
2434 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002435 }
2436
Per Forlinee8a43a2011-07-01 18:55:33 +02002437 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002438 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2439 if (!mq_rq->packed->retries)
2440 goto cmd_abort;
2441 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2442 mmc_start_req(card->host,
2443 &mq_rq->mmc_active, NULL);
2444 } else {
2445
2446 /*
2447 * In case of a incomplete request
2448 * prepare it again and resend.
2449 */
2450 mmc_blk_rw_rq_prep(mq_rq, card,
2451 disable_multi, mq);
2452 mmc_start_req(card->host,
2453 &mq_rq->mmc_active, NULL);
2454 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002455 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 } while (ret);
2458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 return 1;
2460
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002461 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002462 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2463 mmc_blk_abort_packed_req(mq_rq);
2464 } else {
2465 if (mmc_card_removed(card))
2466 req->cmd_flags |= REQ_QUIET;
2467 while (ret)
2468 ret = blk_end_request(req, -EIO,
2469 blk_rq_cur_bytes(req));
2470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Per Forlinee8a43a2011-07-01 18:55:33 +02002472 start_new_req:
2473 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002474 if (mmc_card_removed(card)) {
2475 rqc->cmd_flags |= REQ_QUIET;
2476 blk_end_request_all(rqc, -EIO);
2477 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002478 /*
2479 * If current request is packed, it needs to put back.
2480 */
2481 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2482 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2483
Seungwon Jeon7a819022013-01-22 19:48:07 +09002484 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2485 mmc_start_req(card->host,
2486 &mq->mqrq_cur->mmc_active, NULL);
2487 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002488 }
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return 0;
2491}
2492
Linus Walleij29eb7bd2016-09-20 11:34:38 +02002493int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002494{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002495 int ret;
2496 struct mmc_blk_data *md = mq->data;
2497 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002498 struct mmc_host *host = card->host;
2499 unsigned long flags;
Adrian Hunter869c5542016-08-25 14:11:43 -06002500 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002501
Per Forlinee8a43a2011-07-01 18:55:33 +02002502 if (req && !mq->mqrq_prev->req)
2503 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002504 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002505
Andrei Warkentin371a6892011-04-11 18:10:25 -05002506 ret = mmc_blk_part_switch(card, md);
2507 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002508 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302509 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002510 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002511 ret = 0;
2512 goto out;
2513 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002514
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002515 mmc_blk_write_packing_control(mq, req);
2516
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002517 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05002518 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002519 /* complete ongoing async transfer before issuing discard */
2520 if (card->host->areq)
2521 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02002522 ret = mmc_blk_issue_discard_rq(mq, req);
2523 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
2524 /* complete ongoing async transfer before issuing secure erase*/
2525 if (card->host->areq)
2526 mmc_blk_issue_rw_rq(mq, NULL);
2527 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05002528 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002529 /* complete ongoing async transfer before issuing flush */
2530 if (card->host->areq)
2531 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002532 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002533 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002534 if (!req && host->areq) {
2535 spin_lock_irqsave(&host->context_info.lock, flags);
2536 host->context_info.is_waiting_last_req = true;
2537 spin_unlock_irqrestore(&host->context_info.lock, flags);
2538 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002539 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002540 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002541
Andrei Warkentin371a6892011-04-11 18:10:25 -05002542out:
Adrian Hunter869c5542016-08-25 14:11:43 -06002543 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002544 /*
2545 * Release host when there are no more requests
2546 * and after special request(discard, flush) is done.
2547 * In case sepecial request, there is no reentry to
2548 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2549 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002550 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002551 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002552}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Russell Kinga6f6c962006-01-03 22:38:44 +00002554static inline int mmc_blk_readonly(struct mmc_card *card)
2555{
2556 return mmc_card_readonly(card) ||
2557 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2558}
2559
Andrei Warkentin371a6892011-04-11 18:10:25 -05002560static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2561 struct device *parent,
2562 sector_t size,
2563 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002564 const char *subname,
2565 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
2567 struct mmc_blk_data *md;
2568 int devidx, ret;
2569
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002570again:
2571 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
2572 return ERR_PTR(-ENOMEM);
2573
2574 spin_lock(&mmc_blk_lock);
2575 ret = ida_get_new(&mmc_blk_ida, &devidx);
2576 spin_unlock(&mmc_blk_lock);
2577
2578 if (ret == -EAGAIN)
2579 goto again;
2580 else if (ret)
2581 return ERR_PTR(ret);
2582
2583 if (devidx >= max_devices) {
2584 ret = -ENOSPC;
2585 goto out;
2586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002588 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002589 if (!md) {
2590 ret = -ENOMEM;
2591 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002593
Johan Rudholmadd710e2011-12-02 08:51:06 +01002594 md->area_type = area_type;
2595
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002596 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002597 * Set the read-only status based on the supported commands
2598 * and the write protect switch.
2599 */
2600 md->read_only = mmc_blk_readonly(card);
2601
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002602 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002603 if (md->disk == NULL) {
2604 ret = -ENOMEM;
2605 goto err_kfree;
2606 }
2607
2608 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002609 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002610 md->usage = 1;
2611
Adrian Hunterd09408a2011-06-23 13:40:28 +03002612 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002613 if (ret)
2614 goto err_putdisk;
2615
Russell Kinga6f6c962006-01-03 22:38:44 +00002616 md->queue.data = md;
2617
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002618 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002619 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002620 md->disk->fops = &mmc_bdops;
2621 md->disk->private_data = md;
2622 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07002623 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002624 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07002625 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002626 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002627 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002628
2629 /*
2630 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2631 *
2632 * - be set for removable media with permanent block devices
2633 * - be unset for removable block devices with permanent media
2634 *
2635 * Since MMC block devices clearly fall under the second
2636 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2637 * should use the block device creation/destruction hotplug
2638 * messages to tell when the card is present.
2639 */
2640
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002641 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02002642 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002643
Saugata Dasa5075eb2012-05-17 16:32:21 +05302644 if (mmc_card_mmc(card))
2645 blk_queue_logical_block_size(md->queue.queue,
2646 card->ext_csd.data_sector_size);
2647 else
2648 blk_queue_logical_block_size(md->queue.queue, 512);
2649
Andrei Warkentin371a6892011-04-11 18:10:25 -05002650 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002651
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002652 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02002653 if ((mmc_card_mmc(card) &&
2654 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002655 (mmc_card_sd(card) &&
2656 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2657 md->flags |= MMC_BLK_CMD23;
2658 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002659
2660 if (mmc_card_mmc(card) &&
2661 md->flags & MMC_BLK_CMD23 &&
2662 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2663 card->ext_csd.rel_sectors)) {
2664 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06002665 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002666 }
2667
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002668 if (mmc_card_mmc(card) &&
2669 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2670 (md->flags & MMC_BLK_CMD23) &&
2671 card->ext_csd.packed_event_en) {
2672 if (!mmc_packed_init(&md->queue, card))
2673 md->flags |= MMC_BLK_PACKED_CMD;
2674 }
2675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002677
2678 err_putdisk:
2679 put_disk(md->disk);
2680 err_kfree:
2681 kfree(md);
2682 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02002683 spin_lock(&mmc_blk_lock);
2684 ida_remove(&mmc_blk_ida, devidx);
2685 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00002686 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687}
2688
Andrei Warkentin371a6892011-04-11 18:10:25 -05002689static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2690{
2691 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002692
2693 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2694 /*
2695 * The EXT_CSD sector count is in number or 512 byte
2696 * sectors.
2697 */
2698 size = card->ext_csd.sectors;
2699 } else {
2700 /*
2701 * The CSD capacity field is in units of read_blkbits.
2702 * set_capacity takes units of 512 bytes.
2703 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002704 size = (typeof(sector_t))card->csd.capacity
2705 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002706 }
2707
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002708 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002709 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002710}
2711
2712static int mmc_blk_alloc_part(struct mmc_card *card,
2713 struct mmc_blk_data *md,
2714 unsigned int part_type,
2715 sector_t size,
2716 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002717 const char *subname,
2718 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002719{
2720 char cap_str[10];
2721 struct mmc_blk_data *part_md;
2722
2723 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002724 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002725 if (IS_ERR(part_md))
2726 return PTR_ERR(part_md);
2727 part_md->part_type = part_type;
2728 list_add(&part_md->part, &md->part);
2729
James Bottomleyb9f28d82015-03-05 18:47:01 -08002730 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002731 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302732 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002733 part_md->disk->disk_name, mmc_card_id(card),
2734 mmc_card_name(card), part_md->part_type, cap_str);
2735 return 0;
2736}
2737
Namjae Jeone0c368d2011-10-06 23:41:38 +09002738/* MMC Physical partitions consist of two boot partitions and
2739 * up to four general purpose partitions.
2740 * For each partition enabled in EXT_CSD a block device will be allocatedi
2741 * to provide access to the partition.
2742 */
2743
Andrei Warkentin371a6892011-04-11 18:10:25 -05002744static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2745{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002746 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002747
2748 if (!mmc_card_mmc(card))
2749 return 0;
2750
Namjae Jeone0c368d2011-10-06 23:41:38 +09002751 for (idx = 0; idx < card->nr_parts; idx++) {
2752 if (card->part[idx].size) {
2753 ret = mmc_blk_alloc_part(card, md,
2754 card->part[idx].part_cfg,
2755 card->part[idx].size >> 9,
2756 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002757 card->part[idx].name,
2758 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002759 if (ret)
2760 return ret;
2761 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002762 }
2763
2764 return ret;
2765}
2766
Andrei Warkentin371a6892011-04-11 18:10:25 -05002767static void mmc_blk_remove_req(struct mmc_blk_data *md)
2768{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002769 struct mmc_card *card;
2770
Andrei Warkentin371a6892011-04-11 18:10:25 -05002771 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002772 /*
2773 * Flush remaining requests and free queues. It
2774 * is freeing the queue that stops new requests
2775 * from being accepted.
2776 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002777 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002778 mmc_cleanup_queue(&md->queue);
2779 if (md->flags & MMC_BLK_PACKED_CMD)
2780 mmc_packed_clean(&md->queue);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002781 device_remove_file(disk_to_dev(md->disk),
2782 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002783 if (md->disk->flags & GENHD_FL_UP) {
2784 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002785 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2786 card->ext_csd.boot_ro_lockable)
2787 device_remove_file(disk_to_dev(md->disk),
2788 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08002789#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2790 device_remove_file(disk_to_dev(md->disk),
2791 &dev_attr_max_write_speed);
2792 device_remove_file(disk_to_dev(md->disk),
2793 &dev_attr_max_read_speed);
2794 device_remove_file(disk_to_dev(md->disk),
2795 &dev_attr_cache_size);
2796#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05002797
Andrei Warkentin371a6892011-04-11 18:10:25 -05002798 del_gendisk(md->disk);
2799 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002800 mmc_blk_put(md);
2801 }
2802}
2803
2804static void mmc_blk_remove_parts(struct mmc_card *card,
2805 struct mmc_blk_data *md)
2806{
2807 struct list_head *pos, *q;
2808 struct mmc_blk_data *part_md;
2809
2810 list_for_each_safe(pos, q, &md->part) {
2811 part_md = list_entry(pos, struct mmc_blk_data, part);
2812 list_del(pos);
2813 mmc_blk_remove_req(part_md);
2814 }
2815}
2816
2817static int mmc_add_disk(struct mmc_blk_data *md)
2818{
2819 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002820 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002821
Dan Williams307d8e62016-06-20 10:40:44 -07002822 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002823 md->force_ro.show = force_ro_show;
2824 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302825 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002826 md->force_ro.attr.name = "force_ro";
2827 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2828 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2829 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002830 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002831#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2832 atomic_set(&md->queue.max_write_speed, max_write_speed);
2833 ret = device_create_file(disk_to_dev(md->disk),
2834 &dev_attr_max_write_speed);
2835 if (ret)
2836 goto max_write_speed_fail;
2837 atomic_set(&md->queue.max_read_speed, max_read_speed);
2838 ret = device_create_file(disk_to_dev(md->disk),
2839 &dev_attr_max_read_speed);
2840 if (ret)
2841 goto max_read_speed_fail;
2842 atomic_set(&md->queue.cache_size, cache_size);
2843 atomic_long_set(&md->queue.cache_used, 0);
2844 md->queue.cache_jiffies = jiffies;
2845 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
2846 if (ret)
2847 goto cache_size_fail;
2848#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01002849
2850 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2851 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002852 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002853
2854 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2855 mode = S_IRUGO;
2856 else
2857 mode = S_IRUGO | S_IWUSR;
2858
2859 md->power_ro_lock.show = power_ro_lock_show;
2860 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002861 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002862 md->power_ro_lock.attr.mode = mode;
2863 md->power_ro_lock.attr.name =
2864 "ro_lock_until_next_power_on";
2865 ret = device_create_file(disk_to_dev(md->disk),
2866 &md->power_ro_lock);
2867 if (ret)
2868 goto power_ro_lock_fail;
2869 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002870
2871 md->num_wr_reqs_to_start_packing.show =
2872 num_wr_reqs_to_start_packing_show;
2873 md->num_wr_reqs_to_start_packing.store =
2874 num_wr_reqs_to_start_packing_store;
2875 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
2876 md->num_wr_reqs_to_start_packing.attr.name =
2877 "num_wr_reqs_to_start_packing";
2878 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
2879 ret = device_create_file(disk_to_dev(md->disk),
2880 &md->num_wr_reqs_to_start_packing);
2881 if (ret)
2882 goto power_ro_lock_fail;
2883
Johan Rudholmadd710e2011-12-02 08:51:06 +01002884 return ret;
2885
2886power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08002887#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2888 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
2889cache_size_fail:
2890 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
2891max_read_speed_fail:
2892 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
2893max_write_speed_fail:
2894#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01002895 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2896force_ro_fail:
2897 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002898
2899 return ret;
2900}
2901
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002902static const struct mmc_fixup blk_fixups[] =
2903{
Chris Ballc59d4472011-11-11 22:01:43 -05002904 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2905 MMC_QUIRK_INAND_CMD38),
2906 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2907 MMC_QUIRK_INAND_CMD38),
2908 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2909 MMC_QUIRK_INAND_CMD38),
2910 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2911 MMC_QUIRK_INAND_CMD38),
2912 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2913 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002914
2915 /*
2916 * Some MMC cards experience performance degradation with CMD23
2917 * instead of CMD12-bounded multiblock transfers. For now we'll
2918 * black list what's bad...
2919 * - Certain Toshiba cards.
2920 *
2921 * N.B. This doesn't affect SD cards.
2922 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08002923 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2924 MMC_QUIRK_BLK_NO_CMD23),
2925 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2926 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002927 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002928 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002929 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002930 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002931 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002932 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002933
2934 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03002935 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002936 */
Chris Ballc59d4472011-11-11 22:01:43 -05002937 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002938 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03002939 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2940 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002941
Ian Chen3550ccd2012-08-29 15:05:36 +09002942 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08002943 * Some Samsung MMC cards need longer data read timeout than
2944 * indicated in CSD.
2945 */
2946 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
2947 MMC_QUIRK_LONG_READ_TIME),
2948
2949 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09002950 * On these Samsung MoviNAND parts, performing secure erase or
2951 * secure trim can result in unrecoverable corruption due to a
2952 * firmware bug.
2953 */
2954 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2955 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2956 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2957 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2958 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2959 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2960 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2961 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2962 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2963 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2964 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2965 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2966 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2967 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2968 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2969 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2970
Shawn Linb5b4ff02015-08-12 13:08:32 +08002971 /*
2972 * On Some Kingston eMMCs, performing trim can result in
2973 * unrecoverable data conrruption occasionally due to a firmware bug.
2974 */
2975 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2976 MMC_QUIRK_TRIM_BROKEN),
2977 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2978 MMC_QUIRK_TRIM_BROKEN),
2979
Pratibhasagar V8d664e32014-12-03 18:26:42 +02002980 /* Some INAND MCP devices advertise incorrect timeout values */
2981 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
2982 MMC_QUIRK_INAND_DATA_TIMEOUT),
2983
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002984 END_FIXUP
2985};
2986
Ulf Hansson96541ba2015-04-14 13:06:12 +02002987static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002989 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002990 char cap_str[10];
2991
Pierre Ossman912490d2005-05-21 10:27:02 +01002992 /*
2993 * Check that the card supports the command class(es) we need.
2994 */
2995 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 return -ENODEV;
2997
Lukas Czerner5204d002014-06-18 13:18:07 +02002998 mmc_fixup_device(card, blk_fixups);
2999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 md = mmc_blk_alloc(card);
3001 if (IS_ERR(md))
3002 return PTR_ERR(md);
3003
James Bottomleyb9f28d82015-03-05 18:47:01 -08003004 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003005 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303006 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003008 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Andrei Warkentin371a6892011-04-11 18:10:25 -05003010 if (mmc_blk_alloc_parts(card, md))
3011 goto out;
3012
Ulf Hansson96541ba2015-04-14 13:06:12 +02003013 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003014
Andrei Warkentin371a6892011-04-11 18:10:25 -05003015 if (mmc_add_disk(md))
3016 goto out;
3017
3018 list_for_each_entry(part_md, &md->part, part) {
3019 if (mmc_add_disk(part_md))
3020 goto out;
3021 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003022
3023 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
3024 pm_runtime_use_autosuspend(&card->dev);
3025
3026 /*
3027 * Don't enable runtime PM for SD-combo cards here. Leave that
3028 * decision to be taken during the SDIO init sequence instead.
3029 */
3030 if (card->type != MMC_TYPE_SD_COMBO) {
3031 pm_runtime_set_active(&card->dev);
3032 pm_runtime_enable(&card->dev);
3033 }
3034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 return 0;
3036
3037 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05003038 mmc_blk_remove_parts(card, md);
3039 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01003040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041}
3042
Ulf Hansson96541ba2015-04-14 13:06:12 +02003043static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003045 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Andrei Warkentin371a6892011-04-11 18:10:25 -05003047 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003048 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003049 mmc_claim_host(card->host);
3050 mmc_blk_part_switch(card, md);
3051 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003052 if (card->type != MMC_TYPE_SD_COMBO)
3053 pm_runtime_disable(&card->dev);
3054 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003055 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02003056 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058
Ulf Hansson96541ba2015-04-14 13:06:12 +02003059static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003061 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02003062 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303063 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
3065 if (md) {
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303066 rc = mmc_queue_suspend(&md->queue);
3067 if (rc)
3068 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003069 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303070 rc = mmc_queue_suspend(&part_md->queue);
3071 if (rc)
3072 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303075 goto out;
3076
3077 out_resume:
3078 mmc_queue_resume(&md->queue);
3079 list_for_each_entry(part_md, &md->part, part) {
3080 mmc_queue_resume(&part_md->queue);
3081 }
3082 out:
3083 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084}
3085
Ulf Hansson96541ba2015-04-14 13:06:12 +02003086static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02003087{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003088 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003089}
3090
Ulf Hansson0967edc2014-10-06 11:29:42 +02003091#ifdef CONFIG_PM_SLEEP
3092static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02003093{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003094 struct mmc_card *card = mmc_dev_to_card(dev);
3095
3096 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003097}
3098
Ulf Hansson0967edc2014-10-06 11:29:42 +02003099static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003101 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02003102 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003105 /*
3106 * Resume involves the card going into idle state,
3107 * so current partition is always the main one.
3108 */
3109 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003111 list_for_each_entry(part_md, &md->part, part) {
3112 mmc_queue_resume(&part_md->queue);
3113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 }
3115 return 0;
3116}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117#endif
3118
Ulf Hansson0967edc2014-10-06 11:29:42 +02003119static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3120
Ulf Hansson96541ba2015-04-14 13:06:12 +02003121static struct mmc_driver mmc_driver = {
3122 .drv = {
3123 .name = "mmcblk",
3124 .pm = &mmc_blk_pm_ops,
3125 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 .probe = mmc_blk_probe,
3127 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02003128 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129};
3130
3131static int __init mmc_blk_init(void)
3132{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003133 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003135 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3136 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3137
Ben Hutchingsa26eba62014-11-06 03:35:09 +00003138 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003139
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003140 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3141 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003144 res = mmc_register_driver(&mmc_driver);
3145 if (res)
3146 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003148 return 0;
3149 out2:
3150 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 out:
3152 return res;
3153}
3154
3155static void __exit mmc_blk_exit(void)
3156{
3157 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003158 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
3160
3161module_init(mmc_blk_init);
3162module_exit(mmc_blk_exit);
3163
3164MODULE_LICENSE("GPL");
3165MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3166