blob: 69a81e091606ebe1ae899b1d6455ffcd4cd060ad [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>
Sujit Reddy Thumma55291992014-12-09 20:40:16 +020033#include <linux/bitops.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020034#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040035#include <linux/delay.h>
36#include <linux/capability.h>
37#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020038#include <linux/pm_runtime.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020039#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
John Calixtocb87ea22011-04-26 18:56:29 -040041#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020043#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010044#include <linux/mmc/mmc.h>
45#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/uaccess.h>
48
Pierre Ossman98ac2162006-12-23 20:03:02 +010049#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080050#include "block.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000052MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040053#ifdef MODULE_PARAM_PREFIX
54#undef MODULE_PARAM_PREFIX
55#endif
56#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010057
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050058#define INAND_CMD38_ARG_EXT_CSD 113
59#define INAND_CMD38_ARG_ERASE 0x00
60#define INAND_CMD38_ARG_TRIM 0x01
61#define INAND_CMD38_ARG_SECERASE 0x80
62#define INAND_CMD38_ARG_SECTRIM1 0x81
63#define INAND_CMD38_ARG_SECTRIM2 0x88
Subhash Jadavani2fbab612014-12-04 15:16:17 +020064#define MMC_BLK_TIMEOUT_MS (30 * 1000) /* 30 sec timeout */
Maya Erez775a9362013-04-18 15:41:55 +030065#define MMC_SANITIZE_REQ_TIMEOUT 240000
66#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050067
Luca Porziod3df0462015-11-06 15:12:26 +000068#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090069 (rq_data_dir(req) == WRITE))
70#define PACKED_CMD_VER 0x01
71#define PACKED_CMD_WR 0x02
Lee Susman841fd132013-04-23 17:59:26 +030072#define PACKED_TRIGGER_MAX_ELEMENTS 5000
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090073
Maya Erezf93ca0a2014-12-09 23:34:41 +020074#define MMC_BLK_MAX_RETRIES 5 /* max # of retries before aborting a command */
Tatyana Brokhman08238ce2012-10-07 10:33:13 +020075#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
76 do { \
77 if (stats->enabled) \
78 stats->pack_stop_reason[reason]++; \
79 } while (0)
80
Lee Susman841fd132013-04-23 17:59:26 +030081#define PCKD_TRGR_INIT_MEAN_POTEN 17
82#define PCKD_TRGR_POTEN_LOWER_BOUND 5
83#define PCKD_TRGR_URGENT_PENALTY 2
84#define PCKD_TRGR_LOWER_BOUND 5
85#define PCKD_TRGR_PRECISION_MULTIPLIER 100
86
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020087static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040088
89/*
90 * The defaults come from config options but can be overriden by module
91 * or bootarg options.
92 */
93static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
94
95/*
96 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000097 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020098 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040099 */
100static int max_devices;
101
Ben Hutchingsa26eba62014-11-06 03:35:09 +0000102#define MAX_DEVICES 256
103
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200104static DEFINE_IDA(mmc_blk_ida);
105static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/*
108 * There is one mmc_blk_data per slot.
109 */
110struct mmc_blk_data {
111 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -0700112 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 struct gendisk *disk;
114 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500115 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500117 unsigned int flags;
118#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
119#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900120#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000123 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500124 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300125 unsigned int reset_done;
126#define MMC_BLK_READ BIT(0)
127#define MMC_BLK_WRITE BIT(1)
128#define MMC_BLK_DISCARD BIT(2)
129#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500130
131 /*
132 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200133 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500134 * track of the current selected device partition.
135 */
136 unsigned int part_curr;
137 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100138 struct device_attribute power_ro_lock;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200139 struct device_attribute num_wr_reqs_to_start_packing;
Maya Erez5a8dae12014-12-04 15:13:59 +0200140 struct device_attribute no_pack_for_random;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100141 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
Arjan van de Vena621aae2006-01-12 18:43:35 +0000144static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900146enum {
147 MMC_PACKED_NR_IDX = -1,
148 MMC_PACKED_NR_ZERO,
149 MMC_PACKED_NR_SINGLE,
150};
151
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400152module_param(perdev_minors, int, 0444);
153MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
154
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200155static inline int mmc_blk_part_switch(struct mmc_card *card,
156 struct mmc_blk_data *md);
157static int get_card_status(struct mmc_card *card, u32 *status, int retries);
158
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900159static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
160{
161 struct mmc_packed *packed = mqrq->packed;
162
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900163 mqrq->cmd_type = MMC_PACKED_NONE;
164 packed->nr_entries = MMC_PACKED_NR_ZERO;
165 packed->idx_failure = MMC_PACKED_NR_IDX;
166 packed->retries = 0;
167 packed->blocks = 0;
168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
171{
172 struct mmc_blk_data *md;
173
Arjan van de Vena621aae2006-01-12 18:43:35 +0000174 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 md = disk->private_data;
176 if (md && md->usage == 0)
177 md = NULL;
178 if (md)
179 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000180 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 return md;
183}
184
Andrei Warkentin371a6892011-04-11 18:10:25 -0500185static inline int mmc_get_devidx(struct gendisk *disk)
186{
Colin Cross382c55f2015-10-22 10:00:41 -0700187 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500188 return devidx;
189}
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static void mmc_blk_put(struct mmc_blk_data *md)
192{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000193 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 md->usage--;
195 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500196 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800197 blk_cleanup_queue(md->queue.queue);
198
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200199 spin_lock(&mmc_blk_lock);
200 ida_remove(&mmc_blk_ida, devidx);
201 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 kfree(md);
205 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000206 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Johan Rudholmadd710e2011-12-02 08:51:06 +0100209static ssize_t power_ro_lock_show(struct device *dev,
210 struct device_attribute *attr, char *buf)
211{
212 int ret;
213 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200214 struct mmc_card *card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100215 int locked = 0;
216
Asutosh Das507d9a72014-12-09 10:15:53 +0200217 if (!md)
218 return -EINVAL;
219
220 card = md->queue.card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100221 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
222 locked = 2;
223 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
224 locked = 1;
225
226 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
227
Tomas Winkler9098f842015-07-16 15:50:45 +0200228 mmc_blk_put(md);
229
Johan Rudholmadd710e2011-12-02 08:51:06 +0100230 return ret;
231}
232
233static ssize_t power_ro_lock_store(struct device *dev,
234 struct device_attribute *attr, const char *buf, size_t count)
235{
236 int ret;
237 struct mmc_blk_data *md, *part_md;
238 struct mmc_card *card;
239 unsigned long set;
240
241 if (kstrtoul(buf, 0, &set))
242 return -EINVAL;
243
244 if (set != 1)
245 return count;
246
247 md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200248 if (!md)
249 return -EINVAL;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100250 card = md->queue.card;
251
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200252 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100253
254 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
255 card->ext_csd.boot_ro_lock |
256 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
257 card->ext_csd.part_time);
258 if (ret)
259 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
260 else
261 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
262
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200263 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100264
265 if (!ret) {
266 pr_info("%s: Locking boot partition ro until next power on\n",
267 md->disk->disk_name);
268 set_disk_ro(md->disk, 1);
269
270 list_for_each_entry(part_md, &md->part, part)
271 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
272 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
273 set_disk_ro(part_md->disk, 1);
274 }
275 }
276
277 mmc_blk_put(md);
278 return count;
279}
280
Andrei Warkentin371a6892011-04-11 18:10:25 -0500281static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
282 char *buf)
283{
284 int ret;
285 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
286
Asutosh Das507d9a72014-12-09 10:15:53 +0200287 if (!md)
288 return -EINVAL;
289
Baruch Siach0031a982014-09-22 10:12:51 +0300290 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500291 get_disk_ro(dev_to_disk(dev)) ^
292 md->read_only);
293 mmc_blk_put(md);
294 return ret;
295}
296
297static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
298 const char *buf, size_t count)
299{
300 int ret;
301 char *end;
302 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
303 unsigned long set = simple_strtoul(buf, &end, 0);
Asutosh Das507d9a72014-12-09 10:15:53 +0200304
305 if (!md)
306 return -EINVAL;
307
Andrei Warkentin371a6892011-04-11 18:10:25 -0500308 if (end == buf) {
309 ret = -EINVAL;
310 goto out;
311 }
312
313 set_disk_ro(dev_to_disk(dev), set || md->read_only);
314 ret = count;
315out:
316 mmc_blk_put(md);
317 return ret;
318}
319
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200320static ssize_t
Maya Erez5a8dae12014-12-04 15:13:59 +0200321no_pack_for_random_show(struct device *dev,
322 struct device_attribute *attr, char *buf)
323{
324 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
325 int ret;
326
Asutosh Das507d9a72014-12-09 10:15:53 +0200327 if (!md)
328 return -EINVAL;
Maya Erez5a8dae12014-12-04 15:13:59 +0200329 ret = snprintf(buf, PAGE_SIZE, "%d\n", md->queue.no_pack_for_random);
330
331 mmc_blk_put(md);
332 return ret;
333}
334
335static ssize_t
336no_pack_for_random_store(struct device *dev,
337 struct device_attribute *attr,
338 const char *buf, size_t count)
339{
340 int value;
341 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200342 struct mmc_card *card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200343 int ret = count;
344
Asutosh Das507d9a72014-12-09 10:15:53 +0200345 if (!md)
346 return -EINVAL;
347
348 card = md->queue.card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200349 if (!card) {
350 ret = -EINVAL;
351 goto exit;
352 }
353
354 sscanf(buf, "%d", &value);
355
356 if (value < 0) {
357 pr_err("%s: value %d is not valid. old value remains = %d",
358 mmc_hostname(card->host), value,
359 md->queue.no_pack_for_random);
360 ret = -EINVAL;
361 goto exit;
362 }
363
364 md->queue.no_pack_for_random = (value > 0) ? true : false;
365
366 pr_debug("%s: no_pack_for_random: new value = %d",
367 mmc_hostname(card->host),
368 md->queue.no_pack_for_random);
369
370exit:
371 mmc_blk_put(md);
372 return ret;
373}
374
375static ssize_t
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200376num_wr_reqs_to_start_packing_show(struct device *dev,
377 struct device_attribute *attr, char *buf)
378{
379 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
380 int num_wr_reqs_to_start_packing;
381 int ret;
382
Asutosh Das507d9a72014-12-09 10:15:53 +0200383 if (!md)
384 return -EINVAL;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200385 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
386
387 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
388
389 mmc_blk_put(md);
390 return ret;
391}
392
393static ssize_t
394num_wr_reqs_to_start_packing_store(struct device *dev,
395 struct device_attribute *attr,
396 const char *buf, size_t count)
397{
398 int value;
399 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200400 struct mmc_card *card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200401 int ret = count;
402
Asutosh Das507d9a72014-12-09 10:15:53 +0200403 if (!md)
404 return -EINVAL;
405
406 card = md->queue.card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200407 if (!card) {
408 ret = -EINVAL;
409 goto exit;
410 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200411
412 sscanf(buf, "%d", &value);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200413
Yaniv Gardi42399822014-12-04 00:26:23 +0200414 if (value >= 0) {
415 md->queue.num_wr_reqs_to_start_packing =
416 min_t(int, value, (int)card->ext_csd.max_packed_writes);
417
418 pr_debug("%s: trigger to pack: new value = %d",
419 mmc_hostname(card->host),
420 md->queue.num_wr_reqs_to_start_packing);
421 } else {
422 pr_err("%s: value %d is not valid. old value remains = %d",
423 mmc_hostname(card->host), value,
424 md->queue.num_wr_reqs_to_start_packing);
425 ret = -EINVAL;
426 }
427
428exit:
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200429 mmc_blk_put(md);
Yaniv Gardi42399822014-12-04 00:26:23 +0200430 return ret;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200431}
432
Mark Salyzyn6904e432016-01-28 11:12:25 -0800433#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
434
435static int max_read_speed, max_write_speed, cache_size = 4;
436
437module_param(max_read_speed, int, S_IRUSR | S_IRGRP);
438MODULE_PARM_DESC(max_read_speed, "maximum KB/s read speed 0=off");
439module_param(max_write_speed, int, S_IRUSR | S_IRGRP);
440MODULE_PARM_DESC(max_write_speed, "maximum KB/s write speed 0=off");
441module_param(cache_size, int, S_IRUSR | S_IRGRP);
442MODULE_PARM_DESC(cache_size, "MB high speed memory or SLC cache");
443
444/*
445 * helper macros and expectations:
446 * size - unsigned long number of bytes
447 * jiffies - unsigned long HZ timestamp difference
448 * speed - unsigned KB/s transfer rate
449 */
450#define size_and_speed_to_jiffies(size, speed) \
451 ((size) * HZ / (speed) / 1024UL)
452#define jiffies_and_speed_to_size(jiffies, speed) \
453 (((speed) * (jiffies) * 1024UL) / HZ)
454#define jiffies_and_size_to_speed(jiffies, size) \
455 ((size) * HZ / (jiffies) / 1024UL)
456
457/* Limits to report warning */
458/* jiffies_and_size_to_speed(10*HZ, queue_max_hw_sectors(q) * 512UL) ~ 25 */
459#define MIN_SPEED(q) 250 /* 10 times faster than a floppy disk */
460#define MAX_SPEED(q) jiffies_and_size_to_speed(1, queue_max_sectors(q) * 512UL)
461
462#define speed_valid(speed) ((speed) > 0)
463
464static const char off[] = "off\n";
465
466static int max_speed_show(int speed, char *buf)
467{
468 if (speed)
469 return scnprintf(buf, PAGE_SIZE, "%uKB/s\n", speed);
470 else
471 return scnprintf(buf, PAGE_SIZE, off);
472}
473
474static int max_speed_store(const char *buf, struct request_queue *q)
475{
476 unsigned int limit, set = 0;
477
478 if (!strncasecmp(off, buf, sizeof(off) - 2))
479 return set;
480 if (kstrtouint(buf, 0, &set) || (set > INT_MAX))
481 return -EINVAL;
482 if (set == 0)
483 return set;
484 limit = MAX_SPEED(q);
485 if (set > limit)
486 pr_warn("max speed %u ineffective above %u\n", set, limit);
487 limit = MIN_SPEED(q);
488 if (set < limit)
489 pr_warn("max speed %u painful below %u\n", set, limit);
490 return set;
491}
492
493static ssize_t max_write_speed_show(struct device *dev,
494 struct device_attribute *attr, char *buf)
495{
496 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
497 int ret = max_speed_show(atomic_read(&md->queue.max_write_speed), buf);
498
499 mmc_blk_put(md);
500 return ret;
501}
502
503static ssize_t max_write_speed_store(struct device *dev,
504 struct device_attribute *attr,
505 const char *buf, size_t count)
506{
507 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
508 int set = max_speed_store(buf, md->queue.queue);
509
510 if (set < 0) {
511 mmc_blk_put(md);
512 return set;
513 }
514
515 atomic_set(&md->queue.max_write_speed, set);
516 mmc_blk_put(md);
517 return count;
518}
519
520static const DEVICE_ATTR(max_write_speed, S_IRUGO | S_IWUSR,
521 max_write_speed_show, max_write_speed_store);
522
523static ssize_t max_read_speed_show(struct device *dev,
524 struct device_attribute *attr, char *buf)
525{
526 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
527 int ret = max_speed_show(atomic_read(&md->queue.max_read_speed), buf);
528
529 mmc_blk_put(md);
530 return ret;
531}
532
533static ssize_t max_read_speed_store(struct device *dev,
534 struct device_attribute *attr,
535 const char *buf, size_t count)
536{
537 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
538 int set = max_speed_store(buf, md->queue.queue);
539
540 if (set < 0) {
541 mmc_blk_put(md);
542 return set;
543 }
544
545 atomic_set(&md->queue.max_read_speed, set);
546 mmc_blk_put(md);
547 return count;
548}
549
550static const DEVICE_ATTR(max_read_speed, S_IRUGO | S_IWUSR,
551 max_read_speed_show, max_read_speed_store);
552
553static ssize_t cache_size_show(struct device *dev,
554 struct device_attribute *attr, char *buf)
555{
556 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
557 struct mmc_queue *mq = &md->queue;
558 int cache_size = atomic_read(&mq->cache_size);
559 int ret;
560
561 if (!cache_size)
562 ret = scnprintf(buf, PAGE_SIZE, off);
563 else {
564 int speed = atomic_read(&mq->max_write_speed);
565
566 if (!speed_valid(speed))
567 ret = scnprintf(buf, PAGE_SIZE, "%uMB\n", cache_size);
568 else { /* We accept race between cache_jiffies and cache_used */
569 unsigned long size = jiffies_and_speed_to_size(
570 jiffies - mq->cache_jiffies, speed);
571 long used = atomic_long_read(&mq->cache_used);
572
573 if (size >= used)
574 size = 0;
575 else
576 size = (used - size) * 100 / cache_size
577 / 1024UL / 1024UL;
578
579 ret = scnprintf(buf, PAGE_SIZE, "%uMB %lu%% used\n",
580 cache_size, size);
581 }
582 }
583
584 mmc_blk_put(md);
585 return ret;
586}
587
588static ssize_t cache_size_store(struct device *dev,
589 struct device_attribute *attr,
590 const char *buf, size_t count)
591{
592 struct mmc_blk_data *md;
593 unsigned int set = 0;
594
595 if (strncasecmp(off, buf, sizeof(off) - 2)
596 && (kstrtouint(buf, 0, &set) || (set > INT_MAX)))
597 return -EINVAL;
598
599 md = mmc_blk_get(dev_to_disk(dev));
600 atomic_set(&md->queue.cache_size, set);
601 mmc_blk_put(md);
602 return count;
603}
604
605static const DEVICE_ATTR(cache_size, S_IRUGO | S_IWUSR,
606 cache_size_show, cache_size_store);
607
608/* correct for write-back */
609static long mmc_blk_cache_used(struct mmc_queue *mq, unsigned long waitfor)
610{
611 long used = 0;
612 int speed = atomic_read(&mq->max_write_speed);
613
614 if (speed_valid(speed)) {
615 unsigned long size = jiffies_and_speed_to_size(
616 waitfor - mq->cache_jiffies, speed);
617 used = atomic_long_read(&mq->cache_used);
618
619 if (size >= used)
620 used = 0;
621 else
622 used -= size;
623 }
624
625 atomic_long_set(&mq->cache_used, used);
626 mq->cache_jiffies = waitfor;
627
628 return used;
629}
630
631static void mmc_blk_simulate_delay(
632 struct mmc_queue *mq,
633 struct request *req,
634 unsigned long waitfor)
635{
636 int max_speed;
637
638 if (!req)
639 return;
640
641 max_speed = (rq_data_dir(req) == READ)
642 ? atomic_read(&mq->max_read_speed)
643 : atomic_read(&mq->max_write_speed);
644 if (speed_valid(max_speed)) {
645 unsigned long bytes = blk_rq_bytes(req);
646
647 if (rq_data_dir(req) != READ) {
648 int cache_size = atomic_read(&mq->cache_size);
649
650 if (cache_size) {
651 unsigned long size = cache_size * 1024L * 1024L;
652 long used = mmc_blk_cache_used(mq, waitfor);
653
654 used += bytes;
655 atomic_long_set(&mq->cache_used, used);
656 bytes = 0;
657 if (used > size)
658 bytes = used - size;
659 }
660 }
661 waitfor += size_and_speed_to_jiffies(bytes, max_speed);
662 if (time_is_after_jiffies(waitfor)) {
663 long msecs = jiffies_to_msecs(waitfor - jiffies);
664
665 if (likely(msecs > 0))
666 msleep(msecs);
667 }
668 }
669}
670
671#else
672
673#define mmc_blk_simulate_delay(mq, req, waitfor)
674
675#endif
676
Al Viroa5a15612008-03-02 10:33:30 -0500677static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Al Viroa5a15612008-03-02 10:33:30 -0500679 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int ret = -ENXIO;
681
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200682 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (md) {
684 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500685 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700687
Al Viroa5a15612008-03-02 10:33:30 -0500688 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700689 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700690 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200693 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 return ret;
696}
697
Al Virodb2a1442013-05-05 21:52:57 -0400698static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Al Viroa5a15612008-03-02 10:33:30 -0500700 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200702 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200704 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800708mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800710 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
711 geo->heads = 4;
712 geo->sectors = 16;
713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
John Calixtocb87ea22011-04-26 18:56:29 -0400716struct mmc_blk_ioc_data {
717 struct mmc_ioc_cmd ic;
718 unsigned char *buf;
719 u64 buf_bytes;
720};
721
722static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
723 struct mmc_ioc_cmd __user *user)
724{
725 struct mmc_blk_ioc_data *idata;
726 int err;
727
yalin wang1ff89502015-11-12 19:27:11 +0800728 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400729 if (!idata) {
730 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400731 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400732 }
733
734 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
735 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400736 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400737 }
738
739 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
740 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
741 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400742 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400743 }
744
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300745 if (!idata->buf_bytes) {
746 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100747 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300748 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100749
yalin wang1ff89502015-11-12 19:27:11 +0800750 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400751 if (!idata->buf) {
752 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400753 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400754 }
755
756 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
757 idata->ic.data_ptr, idata->buf_bytes)) {
758 err = -EFAULT;
759 goto copy_err;
760 }
761
762 return idata;
763
764copy_err:
765 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400766idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400767 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400768out:
John Calixtocb87ea22011-04-26 18:56:29 -0400769 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400770}
771
Jon Huntera5f57742015-09-22 10:27:53 +0100772static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
773 struct mmc_blk_ioc_data *idata)
774{
775 struct mmc_ioc_cmd *ic = &idata->ic;
776
777 if (copy_to_user(&(ic_ptr->response), ic->response,
778 sizeof(ic->response)))
779 return -EFAULT;
780
781 if (!idata->ic.write_flag) {
782 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
783 idata->buf, idata->buf_bytes))
784 return -EFAULT;
785 }
786
787 return 0;
788}
789
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200790static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
791 u32 retries_max)
792{
793 int err;
794 u32 retry_count = 0;
795
796 if (!status || !retries_max)
797 return -EINVAL;
798
799 do {
800 err = get_card_status(card, status, 5);
801 if (err)
802 break;
803
804 if (!R1_STATUS(*status) &&
805 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
806 break; /* RPMB programming operation complete */
807
808 /*
809 * Rechedule to give the MMC device a chance to continue
810 * processing the previous command without being polled too
811 * frequently.
812 */
813 usleep_range(1000, 5000);
814 } while (++retry_count < retries_max);
815
816 if (retry_count == retries_max)
817 err = -EPERM;
818
819 return err;
820}
821
Maya Erez775a9362013-04-18 15:41:55 +0300822static int ioctl_do_sanitize(struct mmc_card *card)
823{
824 int err;
825
Ulf Hanssona2d10862013-12-16 14:37:26 +0100826 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300827 pr_warn("%s: %s - SANITIZE is not supported\n",
828 mmc_hostname(card->host), __func__);
829 err = -EOPNOTSUPP;
830 goto out;
831 }
832
833 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
834 mmc_hostname(card->host), __func__);
835
836 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
837 EXT_CSD_SANITIZE_START, 1,
838 MMC_SANITIZE_REQ_TIMEOUT);
839
840 if (err)
841 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
842 mmc_hostname(card->host), __func__, err);
843
844 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
845 __func__);
846out:
847 return err;
848}
849
Jon Huntera5f57742015-09-22 10:27:53 +0100850static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
851 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400852{
John Calixtocb87ea22011-04-26 18:56:29 -0400853 struct mmc_command cmd = {0};
854 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530855 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400856 struct scatterlist sg;
857 int err;
858
Jon Huntera5f57742015-09-22 10:27:53 +0100859 if (!card || !md || !idata)
860 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400861
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100862 cmd.opcode = idata->ic.opcode;
863 cmd.arg = idata->ic.arg;
864 cmd.flags = idata->ic.flags;
865
866 if (idata->buf_bytes) {
867 data.sg = &sg;
868 data.sg_len = 1;
869 data.blksz = idata->ic.blksz;
870 data.blocks = idata->ic.blocks;
871
872 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
873
874 if (idata->ic.write_flag)
875 data.flags = MMC_DATA_WRITE;
876 else
877 data.flags = MMC_DATA_READ;
878
879 /* data.flags must already be set before doing this. */
880 mmc_set_data_timeout(&data, card);
881
882 /* Allow overriding the timeout_ns for empirical tuning. */
883 if (idata->ic.data_timeout_ns)
884 data.timeout_ns = idata->ic.data_timeout_ns;
885
886 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
887 /*
888 * Pretend this is a data transfer and rely on the
889 * host driver to compute timeout. When all host
890 * drivers support cmd.cmd_timeout for R1B, this
891 * can be changed to:
892 *
893 * mrq.data = NULL;
894 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
895 */
896 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
897 }
898
899 mrq.data = &data;
900 }
901
902 mrq.cmd = &cmd;
903
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200904 err = mmc_blk_part_switch(card, md);
905 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100906 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200907
John Calixtocb87ea22011-04-26 18:56:29 -0400908 if (idata->ic.is_acmd) {
909 err = mmc_app_cmd(card->host, card);
910 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100911 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400912 }
913
Yaniv Gardia82e4842013-06-05 14:13:08 +0300914 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
915 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300916 err = ioctl_do_sanitize(card);
917
918 if (err)
919 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
920 __func__, err);
921
Jon Huntera5f57742015-09-22 10:27:53 +0100922 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300923 }
924
John Calixtocb87ea22011-04-26 18:56:29 -0400925 mmc_wait_for_req(card->host, &mrq);
926
927 if (cmd.error) {
928 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
929 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100930 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400931 }
932 if (data.error) {
933 dev_err(mmc_dev(card->host), "%s: data error %d\n",
934 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100935 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400936 }
937
938 /*
939 * According to the SD specs, some commands require a delay after
940 * issuing the command.
941 */
942 if (idata->ic.postsleep_min_us)
943 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
944
Jon Huntera5f57742015-09-22 10:27:53 +0100945 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400946
Krishna Kondae6711632014-12-04 15:20:57 +0200947 return err;
948}
949
950struct mmc_blk_ioc_rpmb_data {
951 struct mmc_blk_ioc_data *data[MMC_IOC_MAX_RPMB_CMD];
952};
953
954static struct mmc_blk_ioc_rpmb_data *mmc_blk_ioctl_rpmb_copy_from_user(
955 struct mmc_ioc_rpmb __user *user)
956{
957 struct mmc_blk_ioc_rpmb_data *idata;
958 int err, i;
959
960 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
961 if (!idata) {
962 err = -ENOMEM;
963 goto out;
964 }
965
966 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
967 idata->data[i] = mmc_blk_ioctl_copy_from_user(&(user->cmds[i]));
968 if (IS_ERR(idata->data[i])) {
969 err = PTR_ERR(idata->data[i]);
970 goto copy_err;
971 }
972 }
973
974 return idata;
975
976copy_err:
977 while (--i >= 0) {
978 kfree(idata->data[i]->buf);
979 kfree(idata->data[i]);
980 }
981 kfree(idata);
982out:
983 return ERR_PTR(err);
984}
985
986static int mmc_blk_ioctl_rpmb_cmd(struct block_device *bdev,
987 struct mmc_ioc_rpmb __user *ic_ptr)
988{
989 struct mmc_blk_ioc_rpmb_data *idata;
990 struct mmc_blk_data *md;
991 struct mmc_card *card;
992 struct mmc_command cmd = {0};
993 struct mmc_data data = {0};
994 struct mmc_request mrq = {NULL};
995 struct scatterlist sg;
996 int err = 0, i = 0;
997 u32 status = 0;
998
999 /* The caller must have CAP_SYS_RAWIO */
1000 if (!capable(CAP_SYS_RAWIO))
1001 return -EPERM;
1002
1003 md = mmc_blk_get(bdev->bd_disk);
1004 /* make sure this is a rpmb partition */
1005 if ((!md) || (!(md->area_type & MMC_BLK_DATA_AREA_RPMB))) {
1006 err = -EINVAL;
Asutosh Das507d9a72014-12-09 10:15:53 +02001007 return err;
Krishna Kondae6711632014-12-04 15:20:57 +02001008 }
1009
1010 idata = mmc_blk_ioctl_rpmb_copy_from_user(ic_ptr);
1011 if (IS_ERR(idata)) {
1012 err = PTR_ERR(idata);
1013 goto cmd_done;
1014 }
1015
1016 card = md->queue.card;
1017 if (IS_ERR(card)) {
1018 err = PTR_ERR(card);
1019 goto idata_free;
1020 }
1021
1022 mmc_claim_host(card->host);
1023
1024 err = mmc_blk_part_switch(card, md);
1025 if (err)
1026 goto cmd_rel_host;
1027
1028 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1029 struct mmc_blk_ioc_data *curr_data;
1030 struct mmc_ioc_cmd *curr_cmd;
1031
1032 curr_data = idata->data[i];
1033 curr_cmd = &curr_data->ic;
1034 if (!curr_cmd->opcode)
1035 break;
1036
1037 cmd.opcode = curr_cmd->opcode;
1038 cmd.arg = curr_cmd->arg;
1039 cmd.flags = curr_cmd->flags;
1040
1041 if (curr_data->buf_bytes) {
1042 data.sg = &sg;
1043 data.sg_len = 1;
1044 data.blksz = curr_cmd->blksz;
1045 data.blocks = curr_cmd->blocks;
1046
1047 sg_init_one(data.sg, curr_data->buf,
1048 curr_data->buf_bytes);
1049
1050 if (curr_cmd->write_flag)
1051 data.flags = MMC_DATA_WRITE;
1052 else
1053 data.flags = MMC_DATA_READ;
1054
1055 /* data.flags must already be set before doing this. */
1056 mmc_set_data_timeout(&data, card);
1057
1058 /*
1059 * Allow overriding the timeout_ns for empirical tuning.
1060 */
1061 if (curr_cmd->data_timeout_ns)
1062 data.timeout_ns = curr_cmd->data_timeout_ns;
1063
1064 mrq.data = &data;
1065 }
1066
1067 mrq.cmd = &cmd;
1068
1069 err = mmc_set_blockcount(card, data.blocks,
1070 curr_cmd->write_flag & (1 << 31));
1071 if (err)
1072 goto cmd_rel_host;
1073
1074 mmc_wait_for_req(card->host, &mrq);
1075
1076 if (cmd.error) {
1077 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
1078 __func__, cmd.error);
1079 err = cmd.error;
1080 goto cmd_rel_host;
1081 }
1082 if (data.error) {
1083 dev_err(mmc_dev(card->host), "%s: data error %d\n",
1084 __func__, data.error);
1085 err = data.error;
1086 goto cmd_rel_host;
1087 }
1088
1089 if (copy_to_user(&(ic_ptr->cmds[i].response), cmd.resp,
1090 sizeof(cmd.resp))) {
1091 err = -EFAULT;
1092 goto cmd_rel_host;
1093 }
1094
1095 if (!curr_cmd->write_flag) {
1096 if (copy_to_user((void __user *)(unsigned long)
1097 curr_cmd->data_ptr,
1098 curr_data->buf,
1099 curr_data->buf_bytes)) {
1100 err = -EFAULT;
1101 goto cmd_rel_host;
1102 }
1103 }
1104
Loic Pallardy8d1e9772012-08-06 17:12:31 +02001105 /*
1106 * Ensure RPMB command has completed by polling CMD13
1107 * "Send Status".
1108 */
1109 err = ioctl_rpmb_card_status_poll(card, &status, 5);
1110 if (err)
1111 dev_err(mmc_dev(card->host),
1112 "%s: Card Status=0x%08X, error %d\n",
1113 __func__, status, err);
1114 }
1115
Krishna Kondae6711632014-12-04 15:20:57 +02001116cmd_rel_host:
1117 mmc_put_card(card);
1118
1119idata_free:
1120 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1121 kfree(idata->data[i]->buf);
1122 kfree(idata->data[i]);
1123 }
1124 kfree(idata);
1125
1126cmd_done:
1127 mmc_blk_put(md);
Jon Huntera5f57742015-09-22 10:27:53 +01001128 return err;
1129}
1130
1131static int mmc_blk_ioctl_cmd(struct block_device *bdev,
1132 struct mmc_ioc_cmd __user *ic_ptr)
1133{
1134 struct mmc_blk_ioc_data *idata;
1135 struct mmc_blk_data *md;
1136 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -07001137 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001138
Shawn Lin83c742c2016-03-16 18:15:47 +08001139 /*
1140 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1141 * whole block device, not on a partition. This prevents overspray
1142 * between sibling partitions.
1143 */
1144 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1145 return -EPERM;
1146
Jon Huntera5f57742015-09-22 10:27:53 +01001147 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
Asutosh Dasbbefab32013-10-07 14:53:32 +05301148 if (IS_ERR_OR_NULL(idata))
Jon Huntera5f57742015-09-22 10:27:53 +01001149 return PTR_ERR(idata);
1150
1151 md = mmc_blk_get(bdev->bd_disk);
1152 if (!md) {
1153 err = -EINVAL;
1154 goto cmd_err;
1155 }
1156
1157 card = md->queue.card;
Asutosh Dasbbefab32013-10-07 14:53:32 +05301158 if (IS_ERR_OR_NULL(card)) {
Jon Huntera5f57742015-09-22 10:27:53 +01001159 err = PTR_ERR(card);
1160 goto cmd_done;
1161 }
1162
1163 mmc_get_card(card);
1164
Grant Grundlerb0934102015-09-23 18:30:33 -07001165 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001166
Adrian Hunter3c866562016-05-04 14:38:12 +03001167 /* Always switch back to main area after RPMB access */
1168 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1169 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1170
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001171 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -04001172
Grant Grundlerb0934102015-09-23 18:30:33 -07001173 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001174
John Calixtocb87ea22011-04-26 18:56:29 -04001175cmd_done:
1176 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +03001177cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -04001178 kfree(idata->buf);
1179 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001180 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -04001181}
1182
Jon Huntera5f57742015-09-22 10:27:53 +01001183static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
1184 struct mmc_ioc_multi_cmd __user *user)
1185{
1186 struct mmc_blk_ioc_data **idata = NULL;
1187 struct mmc_ioc_cmd __user *cmds = user->cmds;
1188 struct mmc_card *card;
1189 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -07001190 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001191 __u64 num_of_cmds;
1192
Shawn Lin83c742c2016-03-16 18:15:47 +08001193 /*
1194 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1195 * whole block device, not on a partition. This prevents overspray
1196 * between sibling partitions.
1197 */
1198 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1199 return -EPERM;
1200
Jon Huntera5f57742015-09-22 10:27:53 +01001201 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
1202 sizeof(num_of_cmds)))
1203 return -EFAULT;
1204
1205 if (num_of_cmds > MMC_IOC_MAX_CMDS)
1206 return -EINVAL;
1207
1208 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
1209 if (!idata)
1210 return -ENOMEM;
1211
1212 for (i = 0; i < num_of_cmds; i++) {
1213 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
1214 if (IS_ERR(idata[i])) {
1215 err = PTR_ERR(idata[i]);
1216 num_of_cmds = i;
1217 goto cmd_err;
1218 }
1219 }
1220
1221 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -08001222 if (!md) {
1223 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +01001224 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -08001225 }
Jon Huntera5f57742015-09-22 10:27:53 +01001226
1227 card = md->queue.card;
1228 if (IS_ERR(card)) {
1229 err = PTR_ERR(card);
1230 goto cmd_done;
1231 }
1232
1233 mmc_get_card(card);
1234
Grant Grundlerb0934102015-09-23 18:30:33 -07001235 for (i = 0; i < num_of_cmds && !ioc_err; i++)
1236 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001237
Adrian Hunter3c866562016-05-04 14:38:12 +03001238 /* Always switch back to main area after RPMB access */
1239 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1240 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1241
Jon Huntera5f57742015-09-22 10:27:53 +01001242 mmc_put_card(card);
1243
1244 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -07001245 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +01001246 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001247
1248cmd_done:
1249 mmc_blk_put(md);
1250cmd_err:
1251 for (i = 0; i < num_of_cmds; i++) {
1252 kfree(idata[i]->buf);
1253 kfree(idata[i]);
1254 }
1255 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001256 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +01001257}
1258
John Calixtocb87ea22011-04-26 18:56:29 -04001259static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
1260 unsigned int cmd, unsigned long arg)
1261{
Jon Huntera5f57742015-09-22 10:27:53 +01001262 switch (cmd) {
1263 case MMC_IOC_CMD:
1264 return mmc_blk_ioctl_cmd(bdev,
1265 (struct mmc_ioc_cmd __user *)arg);
Krishna Kondae6711632014-12-04 15:20:57 +02001266 case MMC_IOC_RPMB_CMD:
1267 return mmc_blk_ioctl_rpmb_cmd(bdev,
1268 (struct mmc_ioc_rpmb __user *)arg);
Jon Huntera5f57742015-09-22 10:27:53 +01001269 case MMC_IOC_MULTI_CMD:
1270 return mmc_blk_ioctl_multi_cmd(bdev,
1271 (struct mmc_ioc_multi_cmd __user *)arg);
1272 default:
1273 return -EINVAL;
1274 }
John Calixtocb87ea22011-04-26 18:56:29 -04001275}
1276
1277#ifdef CONFIG_COMPAT
1278static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
1279 unsigned int cmd, unsigned long arg)
1280{
1281 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
1282}
1283#endif
1284
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001285static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -05001286 .open = mmc_blk_open,
1287 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001288 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -04001290 .ioctl = mmc_blk_ioctl,
1291#ifdef CONFIG_COMPAT
1292 .compat_ioctl = mmc_blk_compat_ioctl,
1293#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294};
1295
Andrei Warkentin371a6892011-04-11 18:10:25 -05001296static inline int mmc_blk_part_switch(struct mmc_card *card,
1297 struct mmc_blk_data *md)
1298{
1299 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001300 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001301
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001302 if ((main_md->part_curr == md->part_type) &&
1303 (card->part_curr == md->part_type))
Andrei Warkentin371a6892011-04-11 18:10:25 -05001304 return 0;
1305
1306 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001307 u8 part_config = card->ext_csd.part_config;
1308
Adrian Hunter57da0c02016-05-04 14:38:13 +03001309 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1310 mmc_retune_pause(card->host);
1311
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001312 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1313 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001314
1315 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001316 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001317 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001318 if (ret) {
1319 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1320 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001321 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001322 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001323
1324 card->ext_csd.part_config = part_config;
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001325 card->part_curr = md->part_type;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001326
1327 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1328 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001329 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001330
1331 main_md->part_curr = md->part_type;
1332 return 0;
1333}
1334
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001335static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1336{
1337 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001338 u32 result;
1339 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001340
Venkatraman Sad5fd972011-08-25 00:30:50 +05301341 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001342 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001343 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001344
1345 struct scatterlist sg;
1346
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001347 cmd.opcode = MMC_APP_CMD;
1348 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001349 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001350
1351 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001352 if (err)
1353 return (u32)-1;
1354 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001355 return (u32)-1;
1356
1357 memset(&cmd, 0, sizeof(struct mmc_command));
1358
1359 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1360 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001361 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001362
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001363 data.blksz = 4;
1364 data.blocks = 1;
1365 data.flags = MMC_DATA_READ;
1366 data.sg = &sg;
1367 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301368 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001369
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001370 mrq.cmd = &cmd;
1371 mrq.data = &data;
1372
Ben Dooks051913d2009-06-08 23:33:57 +01001373 blocks = kmalloc(4, GFP_KERNEL);
1374 if (!blocks)
1375 return (u32)-1;
1376
1377 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001378
1379 mmc_wait_for_req(card->host, &mrq);
1380
Ben Dooks051913d2009-06-08 23:33:57 +01001381 result = ntohl(*blocks);
1382 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001383
Ben Dooks051913d2009-06-08 23:33:57 +01001384 if (cmd.error || data.error)
1385 result = (u32)-1;
1386
1387 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001388}
1389
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001390static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001391{
Chris Ball1278dba2011-04-13 23:40:30 -04001392 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001393 int err;
1394
Adrian Hunter504f1912008-10-16 12:55:25 +03001395 cmd.opcode = MMC_SEND_STATUS;
1396 if (!mmc_host_is_spi(card->host))
1397 cmd.arg = card->rca << 16;
1398 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001399 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1400 if (err == 0)
1401 *status = cmd.resp[0];
1402 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001403}
1404
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001405static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001406 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001407{
1408 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1409 int err = 0;
1410 u32 status;
1411
1412 do {
1413 err = get_card_status(card, &status, 5);
1414 if (err) {
1415 pr_err("%s: error %d requesting status\n",
1416 req->rq_disk->disk_name, err);
1417 return err;
1418 }
1419
1420 if (status & R1_ERROR) {
1421 pr_err("%s: %s: error sending status cmd, status %#x\n",
1422 req->rq_disk->disk_name, __func__, status);
1423 *gen_err = 1;
1424 }
1425
Ulf Hansson95a91292014-01-29 13:11:27 +01001426 /* We may rely on the host hw to handle busy detection.*/
1427 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1428 hw_busy_detect)
1429 break;
1430
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001431 /*
1432 * Timeout if the device never becomes ready for data and never
1433 * leaves the program state.
1434 */
1435 if (time_after(jiffies, timeout)) {
1436 pr_err("%s: Card stuck in programming state! %s %s\n",
1437 mmc_hostname(card->host),
1438 req->rq_disk->disk_name, __func__);
1439 return -ETIMEDOUT;
1440 }
1441
1442 /*
1443 * Some cards mishandle the status bits,
1444 * so make sure to check both the busy
1445 * indication and the card state.
1446 */
1447 } while (!(status & R1_READY_FOR_DATA) ||
1448 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1449
1450 return err;
1451}
1452
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001453static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1454 struct request *req, int *gen_err, u32 *stop_status)
1455{
1456 struct mmc_host *host = card->host;
1457 struct mmc_command cmd = {0};
1458 int err;
1459 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1460
1461 /*
1462 * Normally we use R1B responses for WRITE, but in cases where the host
1463 * has specified a max_busy_timeout we need to validate it. A failure
1464 * means we need to prevent the host from doing hw busy detection, which
1465 * is done by converting to a R1 response instead.
1466 */
1467 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1468 use_r1b_resp = false;
1469
1470 cmd.opcode = MMC_STOP_TRANSMISSION;
1471 if (use_r1b_resp) {
1472 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1473 cmd.busy_timeout = timeout_ms;
1474 } else {
1475 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1476 }
1477
1478 err = mmc_wait_for_cmd(host, &cmd, 5);
1479 if (err)
1480 return err;
1481
1482 *stop_status = cmd.resp[0];
1483
1484 /* No need to check card status in case of READ. */
1485 if (rq_data_dir(req) == READ)
1486 return 0;
1487
1488 if (!mmc_host_is_spi(host) &&
1489 (*stop_status & R1_ERROR)) {
1490 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1491 req->rq_disk->disk_name, __func__, *stop_status);
1492 *gen_err = 1;
1493 }
1494
1495 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1496}
1497
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301498#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001499#define ERR_RETRY 2
1500#define ERR_ABORT 1
1501#define ERR_CONTINUE 0
1502
1503static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1504 bool status_valid, u32 status)
1505{
1506 switch (error) {
1507 case -EILSEQ:
1508 /* response crc error, retry the r/w cmd */
1509 pr_err("%s: %s sending %s command, card status %#x\n",
1510 req->rq_disk->disk_name, "response CRC error",
1511 name, status);
1512 return ERR_RETRY;
1513
1514 case -ETIMEDOUT:
1515 pr_err("%s: %s sending %s command, card status %#x\n",
1516 req->rq_disk->disk_name, "timed out", name, status);
1517
1518 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301519 if (!status_valid) {
1520 pr_err("%s: status not valid, retrying timeout\n",
1521 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001522 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301523 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001524
1525 /*
1526 * If it was a r/w cmd crc error, or illegal command
1527 * (eg, issued in wrong state) then retry - we should
1528 * have corrected the state problem above.
1529 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301530 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1531 pr_err("%s: command error, retrying timeout\n",
1532 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001533 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301534 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001535
1536 /* Otherwise abort the command */
1537 return ERR_ABORT;
1538
1539 default:
1540 /* We don't understand the error code the driver gave us */
1541 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1542 req->rq_disk->disk_name, error, status);
1543 return ERR_ABORT;
1544 }
1545}
1546
1547/*
1548 * Initial r/w and stop cmd error recovery.
1549 * We don't know whether the card received the r/w cmd or not, so try to
1550 * restore things back to a sane state. Essentially, we do this as follows:
1551 * - Obtain card status. If the first attempt to obtain card status fails,
1552 * the status word will reflect the failed status cmd, not the failed
1553 * r/w cmd. If we fail to obtain card status, it suggests we can no
1554 * longer communicate with the card.
1555 * - Check the card state. If the card received the cmd but there was a
1556 * transient problem with the response, it might still be in a data transfer
1557 * mode. Try to send it a stop command. If this fails, we can't recover.
1558 * - If the r/w cmd failed due to a response CRC error, it was probably
1559 * transient, so retry the cmd.
1560 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1561 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1562 * illegal cmd, retry.
1563 * Otherwise we don't understand what happened, so abort.
1564 */
1565static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001566 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001567{
1568 bool prev_cmd_status_valid = true;
1569 u32 status, stop_status = 0;
1570 int err, retry;
1571
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301572 if (mmc_card_removed(card))
1573 return ERR_NOMEDIUM;
1574
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001575 /*
1576 * Try to get card status which indicates both the card state
1577 * and why there was no response. If the first attempt fails,
1578 * we can't be sure the returned status is for the r/w command.
1579 */
1580 for (retry = 2; retry >= 0; retry--) {
1581 err = get_card_status(card, &status, 0);
1582 if (!err)
1583 break;
1584
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001585 /* Re-tune if needed */
1586 mmc_retune_recheck(card->host);
1587
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001588 prev_cmd_status_valid = false;
1589 pr_err("%s: error %d sending status command, %sing\n",
1590 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1591 }
1592
1593 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301594 if (err) {
1595 /* Check if the card is removed */
1596 if (mmc_detect_card_removed(card->host))
1597 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001598 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301599 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001600
Adrian Hunter67716322011-08-29 16:42:15 +03001601 /* Flag ECC errors */
1602 if ((status & R1_CARD_ECC_FAILED) ||
1603 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1604 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1605 *ecc_err = 1;
1606
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001607 /* Flag General errors */
1608 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1609 if ((status & R1_ERROR) ||
1610 (brq->stop.resp[0] & R1_ERROR)) {
1611 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1612 req->rq_disk->disk_name, __func__,
1613 brq->stop.resp[0], status);
1614 *gen_err = 1;
1615 }
1616
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001617 /*
1618 * Check the current card state. If it is in some data transfer
1619 * mode, tell it to stop (and hopefully transition back to TRAN.)
1620 */
1621 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1622 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001623 err = send_stop(card,
1624 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1625 req, gen_err, &stop_status);
1626 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001627 pr_err("%s: error %d sending stop command\n",
1628 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001629 /*
1630 * If the stop cmd also timed out, the card is probably
1631 * not present, so abort. Other errors are bad news too.
1632 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001633 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001634 }
1635
Adrian Hunter67716322011-08-29 16:42:15 +03001636 if (stop_status & R1_CARD_ECC_FAILED)
1637 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001638 }
1639
1640 /* Check for set block count errors */
1641 if (brq->sbc.error)
1642 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1643 prev_cmd_status_valid, status);
1644
1645 /* Check for r/w command errors */
1646 if (brq->cmd.error)
1647 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1648 prev_cmd_status_valid, status);
1649
Adrian Hunter67716322011-08-29 16:42:15 +03001650 /* Data errors */
1651 if (!brq->stop.error)
1652 return ERR_CONTINUE;
1653
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001654 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001655 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 +01001656 req->rq_disk->disk_name, brq->stop.error,
1657 brq->cmd.resp[0], status);
1658
1659 /*
1660 * Subsitute in our own stop status as this will give the error
1661 * state which happened during the execution of the r/w command.
1662 */
1663 if (stop_status) {
1664 brq->stop.resp[0] = stop_status;
1665 brq->stop.error = 0;
1666 }
1667 return ERR_CONTINUE;
1668}
1669
Adrian Hunter67716322011-08-29 16:42:15 +03001670static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1671 int type)
1672{
1673 int err;
1674
1675 if (md->reset_done & type)
1676 return -EEXIST;
1677
1678 md->reset_done |= type;
1679 err = mmc_hw_reset(host);
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301680 if (err && err != -EOPNOTSUPP) {
1681 /* We failed to reset so we need to abort the request */
1682 pr_err("%s: %s: failed to reset %d\n", mmc_hostname(host),
1683 __func__, err);
1684 return -ENODEV;
1685 }
1686
Adrian Hunter67716322011-08-29 16:42:15 +03001687 /* Ensure we switch back to the correct partition */
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301688 if (host->card) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001689 struct mmc_blk_data *main_md =
1690 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001691 int part_err;
1692
1693 main_md->part_curr = main_md->part_type;
1694 part_err = mmc_blk_part_switch(host->card, md);
1695 if (part_err) {
1696 /*
1697 * We have failed to get back into the correct
1698 * partition, so we need to abort the whole request.
1699 */
1700 return -ENODEV;
1701 }
1702 }
1703 return err;
1704}
1705
1706static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1707{
1708 md->reset_done &= ~type;
1709}
1710
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001711int mmc_access_rpmb(struct mmc_queue *mq)
1712{
1713 struct mmc_blk_data *md = mq->data;
1714 /*
1715 * If this is a RPMB partition access, return ture
1716 */
1717 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1718 return true;
1719
1720 return false;
1721}
1722
Adrian Hunterbd788c92010-08-11 14:17:47 -07001723static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1724{
1725 struct mmc_blk_data *md = mq->data;
1726 struct mmc_card *card = md->queue.card;
1727 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001728 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001729
Adrian Hunterbd788c92010-08-11 14:17:47 -07001730 if (!mmc_can_erase(card)) {
1731 err = -EOPNOTSUPP;
1732 goto out;
1733 }
1734
1735 from = blk_rq_pos(req);
1736 nr = blk_rq_sectors(req);
1737
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001738 if (mmc_can_discard(card))
1739 arg = MMC_DISCARD_ARG;
1740 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001741 arg = MMC_TRIM_ARG;
1742 else
1743 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001744retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001745 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1746 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1747 INAND_CMD38_ARG_EXT_CSD,
1748 arg == MMC_TRIM_ARG ?
1749 INAND_CMD38_ARG_TRIM :
1750 INAND_CMD38_ARG_ERASE,
1751 0);
1752 if (err)
1753 goto out;
1754 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001755 err = mmc_erase(card, from, nr, arg);
1756out:
Adrian Hunter67716322011-08-29 16:42:15 +03001757 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1758 goto retry;
1759 if (!err)
1760 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301761 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001762
Adrian Hunterbd788c92010-08-11 14:17:47 -07001763 return err ? 0 : 1;
1764}
1765
Adrian Hunter49804542010-08-11 14:17:50 -07001766static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1767 struct request *req)
1768{
1769 struct mmc_blk_data *md = mq->data;
1770 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001771 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001772 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001773
Maya Erez775a9362013-04-18 15:41:55 +03001774 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001775 err = -EOPNOTSUPP;
1776 goto out;
1777 }
1778
1779 from = blk_rq_pos(req);
1780 nr = blk_rq_sectors(req);
1781
Maya Erez775a9362013-04-18 15:41:55 +03001782 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1783 arg = MMC_SECURE_TRIM1_ARG;
1784 else
1785 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001786
Adrian Hunter67716322011-08-29 16:42:15 +03001787retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001788 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1789 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1790 INAND_CMD38_ARG_EXT_CSD,
1791 arg == MMC_SECURE_TRIM1_ARG ?
1792 INAND_CMD38_ARG_SECTRIM1 :
1793 INAND_CMD38_ARG_SECERASE,
1794 0);
1795 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001796 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001797 }
Adrian Hunter28302812012-04-05 14:45:48 +03001798
Adrian Hunter49804542010-08-11 14:17:50 -07001799 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001800 if (err == -EIO)
1801 goto out_retry;
1802 if (err)
1803 goto out;
1804
1805 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001806 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1807 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1808 INAND_CMD38_ARG_EXT_CSD,
1809 INAND_CMD38_ARG_SECTRIM2,
1810 0);
1811 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001812 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001813 }
Adrian Hunter28302812012-04-05 14:45:48 +03001814
Adrian Hunter49804542010-08-11 14:17:50 -07001815 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001816 if (err == -EIO)
1817 goto out_retry;
1818 if (err)
1819 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001820 }
Adrian Hunter28302812012-04-05 14:45:48 +03001821
Adrian Hunter28302812012-04-05 14:45:48 +03001822out_retry:
1823 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001824 goto retry;
1825 if (!err)
1826 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001827out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301828 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001829
Adrian Hunter49804542010-08-11 14:17:50 -07001830 return err ? 0 : 1;
1831}
1832
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001833static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1834{
1835 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001836 struct mmc_card *card = md->queue.card;
1837 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001838
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001839 ret = mmc_flush_cache(card);
1840 if (ret)
1841 ret = -EIO;
1842
Mark Salyzyn6904e432016-01-28 11:12:25 -08001843#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
1844 else if (atomic_read(&mq->cache_size)) {
1845 long used = mmc_blk_cache_used(mq, jiffies);
1846
1847 if (used) {
1848 int speed = atomic_read(&mq->max_write_speed);
1849
1850 if (speed_valid(speed)) {
1851 unsigned long msecs = jiffies_to_msecs(
1852 size_and_speed_to_jiffies(
1853 used, speed));
1854 if (msecs)
1855 msleep(msecs);
1856 }
1857 }
1858 }
1859#endif
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301860 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001861
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001862 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001863}
1864
1865/*
1866 * Reformat current write as a reliable write, supporting
1867 * both legacy and the enhanced reliable write MMC cards.
1868 * In each transfer we'll handle only as much as a single
1869 * reliable write can handle, thus finish the request in
1870 * partial completions.
1871 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001872static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1873 struct mmc_card *card,
1874 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001875{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001876 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1877 /* Legacy mode imposes restrictions on transfers. */
1878 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1879 brq->data.blocks = 1;
1880
1881 if (brq->data.blocks > card->ext_csd.rel_sectors)
1882 brq->data.blocks = card->ext_csd.rel_sectors;
1883 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1884 brq->data.blocks = 1;
1885 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001886}
1887
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001888#define CMD_ERRORS \
1889 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1890 R1_ADDRESS_ERROR | /* Misaligned address */ \
1891 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1892 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1893 R1_CC_ERROR | /* Card controller error */ \
1894 R1_ERROR) /* General/unknown error */
1895
Per Forlinee8a43a2011-07-01 18:55:33 +02001896static int mmc_blk_err_check(struct mmc_card *card,
1897 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001898{
Per Forlinee8a43a2011-07-01 18:55:33 +02001899 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1900 mmc_active);
1901 struct mmc_blk_request *brq = &mq_mrq->brq;
1902 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001903 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001904 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001905
1906 /*
1907 * sbc.error indicates a problem with the set block count
1908 * command. No data will have been transferred.
1909 *
1910 * cmd.error indicates a problem with the r/w command. No
1911 * data will have been transferred.
1912 *
1913 * stop.error indicates a problem with the stop command. Data
1914 * may have been transferred, or may still be transferring.
1915 */
Adrian Hunter67716322011-08-29 16:42:15 +03001916 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1917 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001918 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001919 case ERR_RETRY:
1920 return MMC_BLK_RETRY;
1921 case ERR_ABORT:
1922 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301923 case ERR_NOMEDIUM:
1924 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001925 case ERR_CONTINUE:
1926 break;
1927 }
1928 }
1929
1930 /*
1931 * Check for errors relating to the execution of the
1932 * initial command - such as address errors. No data
1933 * has been transferred.
1934 */
1935 if (brq->cmd.resp[0] & CMD_ERRORS) {
1936 pr_err("%s: r/w command failed, status = %#x\n",
1937 req->rq_disk->disk_name, brq->cmd.resp[0]);
1938 return MMC_BLK_ABORT;
1939 }
1940
1941 /*
1942 * Everything else is either success, or a data error of some
1943 * kind. If it was a write, we may have transitioned to
1944 * program mode, which we have to wait for it to complete.
1945 */
1946 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001947 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001948
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001949 /* Check stop command response */
1950 if (brq->stop.resp[0] & R1_ERROR) {
1951 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1952 req->rq_disk->disk_name, __func__,
1953 brq->stop.resp[0]);
1954 gen_err = 1;
1955 }
1956
Ulf Hansson95a91292014-01-29 13:11:27 +01001957 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1958 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001959 if (err)
1960 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001961 }
1962
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001963 /* if general error occurs, retry the write operation. */
1964 if (gen_err) {
1965 pr_warn("%s: retrying write for general error\n",
1966 req->rq_disk->disk_name);
1967 return MMC_BLK_RETRY;
1968 }
1969
Per Forlind78d4a82011-07-01 18:55:30 +02001970 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001971 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001972 pr_debug("%s: retrying because a re-tune was needed\n",
1973 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001974 brq->retune_retry_done = 1;
1975 return MMC_BLK_RETRY;
1976 }
Per Forlind78d4a82011-07-01 18:55:30 +02001977 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1978 req->rq_disk->disk_name, brq->data.error,
1979 (unsigned)blk_rq_pos(req),
1980 (unsigned)blk_rq_sectors(req),
1981 brq->cmd.resp[0], brq->stop.resp[0]);
1982
1983 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001984 if (ecc_err)
1985 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001986 return MMC_BLK_DATA_ERR;
1987 } else {
1988 return MMC_BLK_CMD_ERR;
1989 }
1990 }
1991
Adrian Hunter67716322011-08-29 16:42:15 +03001992 if (!brq->data.bytes_xfered)
1993 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001994
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001995 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1996 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1997 return MMC_BLK_PARTIAL;
1998 else
1999 return MMC_BLK_SUCCESS;
2000 }
2001
Adrian Hunter67716322011-08-29 16:42:15 +03002002 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
2003 return MMC_BLK_PARTIAL;
2004
2005 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02002006}
2007
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002008static int mmc_blk_packed_err_check(struct mmc_card *card,
2009 struct mmc_async_req *areq)
2010{
2011 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
2012 mmc_active);
2013 struct request *req = mq_rq->req;
2014 struct mmc_packed *packed = mq_rq->packed;
2015 int err, check, status;
2016 u8 *ext_csd;
2017
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002018 packed->retries--;
2019 check = mmc_blk_err_check(card, areq);
2020 err = get_card_status(card, &status, 0);
2021 if (err) {
2022 pr_err("%s: error %d sending status command\n",
2023 req->rq_disk->disk_name, err);
2024 return MMC_BLK_ABORT;
2025 }
2026
2027 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02002028 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002029 if (err) {
2030 pr_err("%s: error %d sending ext_csd\n",
2031 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02002032 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002033 }
2034
2035 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
2036 EXT_CSD_PACKED_FAILURE) &&
2037 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2038 EXT_CSD_PACKED_GENERIC_ERROR)) {
2039 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2040 EXT_CSD_PACKED_INDEXED_ERROR) {
2041 packed->idx_failure =
2042 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
2043 check = MMC_BLK_PARTIAL;
2044 }
2045 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
2046 "failure index: %d\n",
2047 req->rq_disk->disk_name, packed->nr_entries,
2048 packed->blocks, packed->idx_failure);
2049 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002050 kfree(ext_csd);
2051 }
2052
2053 return check;
2054}
2055
Per Forlin54d49d72011-07-01 18:55:29 +02002056static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
2057 struct mmc_card *card,
2058 int disable_multi,
2059 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Per Forlin54d49d72011-07-01 18:55:29 +02002061 u32 readcmd, writecmd;
2062 struct mmc_blk_request *brq = &mqrq->brq;
2063 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05302065 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002067 /*
2068 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00002069 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002070 */
Luca Porziod3df0462015-11-06 15:12:26 +00002071 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002072 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002073 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002074
Per Forlin54d49d72011-07-01 18:55:29 +02002075 memset(brq, 0, sizeof(struct mmc_blk_request));
2076 brq->mrq.cmd = &brq->cmd;
2077 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Per Forlin54d49d72011-07-01 18:55:29 +02002079 brq->cmd.arg = blk_rq_pos(req);
2080 if (!mmc_card_blockaddr(card))
2081 brq->cmd.arg <<= 9;
2082 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2083 brq->data.blksz = 512;
2084 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2085 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002086 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Asutosh Dasf0665412012-07-27 18:10:19 +05302088 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02002089 /*
2090 * The block layer doesn't support all sector count
2091 * restrictions, so we need to be prepared for too big
2092 * requests.
2093 */
2094 if (brq->data.blocks > card->host->max_blk_count)
2095 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002097 if (brq->data.blocks > 1) {
2098 /*
2099 * After a read error, we redo the request one sector
2100 * at a time in order to accurately determine which
2101 * sectors can be read successfully.
2102 */
2103 if (disable_multi)
2104 brq->data.blocks = 1;
2105
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07002106 /*
2107 * Some controllers have HW issues while operating
2108 * in multiple I/O mode
2109 */
2110 if (card->host->ops->multi_io_quirk)
2111 brq->data.blocks = card->host->ops->multi_io_quirk(card,
2112 (rq_data_dir(req) == READ) ?
2113 MMC_DATA_READ : MMC_DATA_WRITE,
2114 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002115 }
Per Forlin54d49d72011-07-01 18:55:29 +02002116
2117 if (brq->data.blocks > 1 || do_rel_wr) {
2118 /* SPI multiblock writes terminate using a special
2119 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02002120 */
Per Forlin54d49d72011-07-01 18:55:29 +02002121 if (!mmc_host_is_spi(card->host) ||
2122 rq_data_dir(req) == READ)
2123 brq->mrq.stop = &brq->stop;
2124 readcmd = MMC_READ_MULTIPLE_BLOCK;
2125 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
2126 } else {
2127 brq->mrq.stop = NULL;
2128 readcmd = MMC_READ_SINGLE_BLOCK;
2129 writecmd = MMC_WRITE_BLOCK;
2130 }
2131 if (rq_data_dir(req) == READ) {
2132 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002133 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002134 if (brq->mrq.stop)
2135 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
2136 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002137 } else {
2138 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002139 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002140 if (brq->mrq.stop)
2141 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
2142 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002143 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02002144
Per Forlin54d49d72011-07-01 18:55:29 +02002145 if (do_rel_wr)
2146 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01002147
Per Forlin54d49d72011-07-01 18:55:29 +02002148 /*
Saugata Das42659002011-12-21 13:09:17 +05302149 * Data tag is used only during writing meta data to speed
2150 * up write and any subsequent read of this meta data
2151 */
2152 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2153 (req->cmd_flags & REQ_META) &&
2154 (rq_data_dir(req) == WRITE) &&
2155 ((brq->data.blocks * brq->data.blksz) >=
2156 card->ext_csd.data_tag_unit_size);
2157
2158 /*
Per Forlin54d49d72011-07-01 18:55:29 +02002159 * Pre-defined multi-block transfers are preferable to
2160 * open ended-ones (and necessary for reliable writes).
2161 * However, it is not sufficient to just send CMD23,
2162 * and avoid the final CMD12, as on an error condition
2163 * CMD12 (stop) needs to be sent anyway. This, coupled
2164 * with Auto-CMD23 enhancements provided by some
2165 * hosts, means that the complexity of dealing
2166 * with this is best left to the host. If CMD23 is
2167 * supported by card and host, we'll fill sbc in and let
2168 * the host deal with handling it correctly. This means
2169 * that for hosts that don't expose MMC_CAP_CMD23, no
2170 * change of behavior will be observed.
2171 *
2172 * N.B: Some MMC cards experience perf degradation.
2173 * We'll avoid using CMD23-bounded multiblock writes for
2174 * these, while retaining features like reliable writes.
2175 */
Saugata Das42659002011-12-21 13:09:17 +05302176 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
2177 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
2178 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02002179 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2180 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05302181 (do_rel_wr ? (1 << 31) : 0) |
2182 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02002183 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2184 brq->mrq.sbc = &brq->sbc;
2185 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002186
Per Forlin54d49d72011-07-01 18:55:29 +02002187 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002188
Per Forlin54d49d72011-07-01 18:55:29 +02002189 brq->data.sg = mqrq->sg;
2190 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002191
Per Forlin54d49d72011-07-01 18:55:29 +02002192 /*
2193 * Adjust the sg list so it is the same size as the
2194 * request.
2195 */
2196 if (brq->data.blocks != blk_rq_sectors(req)) {
2197 int i, data_size = brq->data.blocks << 9;
2198 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02002199
Per Forlin54d49d72011-07-01 18:55:29 +02002200 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
2201 data_size -= sg->length;
2202 if (data_size <= 0) {
2203 sg->length += data_size;
2204 i++;
2205 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01002206 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002207 }
Per Forlin54d49d72011-07-01 18:55:29 +02002208 brq->data.sg_len = i;
2209 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002210
Per Forlinee8a43a2011-07-01 18:55:33 +02002211 mqrq->mmc_active.mrq = &brq->mrq;
2212 mqrq->mmc_active.err_check = mmc_blk_err_check;
2213
Per Forlin54d49d72011-07-01 18:55:29 +02002214 mmc_queue_bounce_pre(mqrq);
2215}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002217static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
2218 struct mmc_card *card)
2219{
2220 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
2221 unsigned int max_seg_sz = queue_max_segment_size(q);
2222 unsigned int len, nr_segs = 0;
2223
2224 do {
2225 len = min(hdr_sz, max_seg_sz);
2226 hdr_sz -= len;
2227 nr_segs++;
2228 } while (hdr_sz);
2229
2230 return nr_segs;
2231}
2232
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002233/**
2234 * mmc_blk_disable_wr_packing() - disables packing mode
2235 * @mq: MMC queue.
2236 *
2237 */
2238void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
2239{
2240 if (mq) {
2241 mq->wr_packing_enabled = false;
2242 mq->num_of_potential_packed_wr_reqs = 0;
2243 }
2244}
2245EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
2246
Lee Susman841fd132013-04-23 17:59:26 +03002247static int get_packed_trigger(int potential, struct mmc_card *card,
2248 struct request *req, int curr_trigger)
2249{
2250 static int num_mean_elements = 1;
2251 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2252 unsigned int trigger = curr_trigger;
2253 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
2254
2255 /* scale down the upper bound to 75% */
2256 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
2257
2258 /*
2259 * since the most common calls for this function are with small
2260 * potential write values and since we don't want these calls to affect
2261 * the packed trigger, set a lower bound and ignore calls with
2262 * potential lower than that bound
2263 */
2264 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
2265 return trigger;
2266
2267 /*
2268 * this is to prevent integer overflow in the following calculation:
2269 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
2270 */
2271 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
2272 num_mean_elements = 1;
2273 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2274 }
2275
2276 /*
2277 * get next mean value based on previous mean value and current
2278 * potential packed writes. Calculation is as follows:
2279 * mean_pot[i+1] =
2280 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
2281 */
2282 mean_potential *= num_mean_elements;
2283 /*
2284 * add num_mean_elements so that the division of two integers doesn't
2285 * lower mean_potential too much
2286 */
2287 if (potential > mean_potential)
2288 mean_potential += num_mean_elements;
2289 mean_potential += potential;
2290 /* this is for gaining more precision when dividing two integers */
2291 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
2292 /* this completes the mean calculation */
2293 mean_potential /= ++num_mean_elements;
2294 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
2295
2296 /*
2297 * if current potential packed writes is greater than the mean potential
2298 * then the heuristic is that the following workload will contain many
2299 * write requests, therefore we lower the packed trigger. In the
2300 * opposite case we want to increase the trigger in order to get less
2301 * packing events.
2302 */
2303 if (potential >= mean_potential)
2304 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
2305 PCKD_TRGR_LOWER_BOUND : trigger - 1;
2306 else
2307 trigger = (trigger >= pckd_trgr_upper_bound) ?
2308 pckd_trgr_upper_bound : trigger + 1;
2309
2310 /*
2311 * an urgent read request indicates a packed list being interrupted
2312 * by this read, therefore we aim for less packing, hence the trigger
2313 * gets increased
2314 */
2315 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
2316 trigger += PCKD_TRGR_URGENT_PENALTY;
2317
2318 return trigger;
2319}
2320
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002321static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2322 struct request *req)
2323{
2324 struct mmc_host *host = mq->card->host;
2325 int data_dir;
2326
2327 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2328 return;
2329
Maya Erez8e2b3c32012-12-02 13:27:15 +02002330 /* Support for the write packing on eMMC 4.5 or later */
2331 if (mq->card->ext_csd.rev <= 5)
2332 return;
2333
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002334 /*
2335 * In case the packing control is not supported by the host, it should
2336 * not have an effect on the write packing. Therefore we have to enable
2337 * the write packing
2338 */
2339 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2340 mq->wr_packing_enabled = true;
2341 return;
2342 }
2343
2344 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2345 if (mq->num_of_potential_packed_wr_reqs >
2346 mq->num_wr_reqs_to_start_packing)
2347 mq->wr_packing_enabled = true;
Lee Susman841fd132013-04-23 17:59:26 +03002348 mq->num_wr_reqs_to_start_packing =
2349 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2350 mq->card, req,
2351 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002352 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002353 return;
2354 }
2355
2356 data_dir = rq_data_dir(req);
2357
2358 if (data_dir == READ) {
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002359 mmc_blk_disable_wr_packing(mq);
Lee Susman841fd132013-04-23 17:59:26 +03002360 mq->num_wr_reqs_to_start_packing =
2361 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2362 mq->card, req,
2363 mq->num_wr_reqs_to_start_packing);
2364 mq->num_of_potential_packed_wr_reqs = 0;
2365 mq->wr_packing_enabled = false;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002366 return;
2367 } else if (data_dir == WRITE) {
2368 mq->num_of_potential_packed_wr_reqs++;
2369 }
2370
2371 if (mq->num_of_potential_packed_wr_reqs >
2372 mq->num_wr_reqs_to_start_packing)
2373 mq->wr_packing_enabled = true;
2374}
2375
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002376struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2377{
2378 if (!card)
2379 return NULL;
2380
2381 return &card->wr_pack_stats;
2382}
2383EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2384
2385void mmc_blk_init_packed_statistics(struct mmc_card *card)
2386{
2387 int max_num_of_packed_reqs = 0;
2388
2389 if (!card || !card->wr_pack_stats.packing_events)
2390 return;
2391
2392 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2393
2394 spin_lock(&card->wr_pack_stats.lock);
2395 memset(card->wr_pack_stats.packing_events, 0,
2396 (max_num_of_packed_reqs + 1) *
2397 sizeof(*card->wr_pack_stats.packing_events));
2398 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2399 sizeof(card->wr_pack_stats.pack_stop_reason));
2400 card->wr_pack_stats.enabled = true;
2401 spin_unlock(&card->wr_pack_stats.lock);
2402}
2403EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2404
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002405static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2406{
2407 struct request_queue *q = mq->queue;
2408 struct mmc_card *card = mq->card;
2409 struct request *cur = req, *next = NULL;
2410 struct mmc_blk_data *md = mq->data;
2411 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2412 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2413 unsigned int req_sectors = 0, phys_segments = 0;
2414 unsigned int max_blk_count, max_phys_segs;
2415 bool put_back = true;
2416 u8 max_packed_rw = 0;
2417 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002418 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002419
Shawn Lin96e52da2016-08-26 08:49:55 +08002420 /*
2421 * We don't need to check packed for any further
2422 * operation of packed stuff as we set MMC_PACKED_NONE
2423 * and return zero for reqs if geting null packed. Also
2424 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2425 * it again when removing blk req.
2426 */
2427 if (!mqrq->packed) {
2428 md->flags &= (~MMC_BLK_PACKED_CMD);
2429 goto no_packed;
2430 }
2431
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002432 if (!(md->flags & MMC_BLK_PACKED_CMD))
2433 goto no_packed;
2434
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002435 if (!mq->wr_packing_enabled)
2436 goto no_packed;
2437
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002438 if ((rq_data_dir(cur) == WRITE) &&
2439 mmc_host_packed_wr(card->host))
2440 max_packed_rw = card->ext_csd.max_packed_writes;
2441
2442 if (max_packed_rw == 0)
2443 goto no_packed;
2444
2445 if (mmc_req_rel_wr(cur) &&
2446 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2447 goto no_packed;
2448
2449 if (mmc_large_sector(card) &&
2450 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2451 goto no_packed;
2452
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002453 if (cur->cmd_flags & REQ_FUA)
2454 goto no_packed;
2455
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002456 mmc_blk_clear_packed(mqrq);
2457
2458 max_blk_count = min(card->host->max_blk_count,
2459 card->host->max_req_size >> 9);
2460 if (unlikely(max_blk_count > 0xffff))
2461 max_blk_count = 0xffff;
2462
2463 max_phys_segs = queue_max_segments(q);
2464 req_sectors += blk_rq_sectors(cur);
2465 phys_segments += cur->nr_phys_segments;
2466
2467 if (rq_data_dir(cur) == WRITE) {
2468 req_sectors += mmc_large_sector(card) ? 8 : 1;
2469 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2470 }
2471
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002472 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002473 do {
2474 if (reqs >= max_packed_rw - 1) {
2475 put_back = false;
2476 break;
2477 }
2478
2479 spin_lock_irq(q->queue_lock);
2480 next = blk_fetch_request(q);
2481 spin_unlock_irq(q->queue_lock);
2482 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002483 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002484 put_back = false;
2485 break;
2486 }
2487
2488 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002489 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2490 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002491 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002492 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002493
Mike Christie3a5e02c2016-06-05 14:32:23 -05002494 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002495 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002496 req_op(next) == REQ_OP_FLUSH) {
2497 if (req_op(next) != REQ_OP_SECURE_ERASE)
2498 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002499 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002500 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002501
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002502 if (next->cmd_flags & REQ_FUA) {
2503 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2504 break;
2505 }
2506
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002507 if (rq_data_dir(cur) != rq_data_dir(next)) {
2508 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002509 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002510 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002511
2512 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002513 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2514 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002515 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002516 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002517
2518 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002519 if (req_sectors > max_blk_count) {
2520 if (stats->enabled)
2521 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002522 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002523 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002524
2525 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002526 if (phys_segments > max_phys_segs) {
2527 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002528 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002529 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002530
Maya Erez5a8dae12014-12-04 15:13:59 +02002531 if (mq->no_pack_for_random) {
2532 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2533 blk_rq_pos(next)) {
2534 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2535 put_back = 1;
2536 break;
2537 }
2538 }
2539
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002540 if (rq_data_dir(next) == WRITE)
2541 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002542 list_add_tail(&next->queuelist, &mqrq->packed->list);
2543 cur = next;
2544 reqs++;
2545 } while (1);
2546
2547 if (put_back) {
2548 spin_lock_irq(q->queue_lock);
2549 blk_requeue_request(q, next);
2550 spin_unlock_irq(q->queue_lock);
2551 }
2552
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002553 if (stats->enabled) {
2554 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2555 stats->packing_events[reqs + 1]++;
2556 if (reqs + 1 == max_packed_rw)
2557 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2558 }
2559
2560 spin_unlock(&stats->lock);
2561
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002562 if (reqs > 0) {
2563 list_add(&req->queuelist, &mqrq->packed->list);
2564 mqrq->packed->nr_entries = ++reqs;
2565 mqrq->packed->retries = reqs;
2566 return reqs;
2567 }
2568
2569no_packed:
2570 mqrq->cmd_type = MMC_PACKED_NONE;
2571 return 0;
2572}
2573
2574static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2575 struct mmc_card *card,
2576 struct mmc_queue *mq)
2577{
2578 struct mmc_blk_request *brq = &mqrq->brq;
2579 struct request *req = mqrq->req;
2580 struct request *prq;
2581 struct mmc_blk_data *md = mq->data;
2582 struct mmc_packed *packed = mqrq->packed;
2583 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002584 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002585 u8 hdr_blocks;
2586 u8 i = 1;
2587
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002588 mqrq->cmd_type = MMC_PACKED_WRITE;
2589 packed->blocks = 0;
2590 packed->idx_failure = MMC_PACKED_NR_IDX;
2591
2592 packed_cmd_hdr = packed->cmd_hdr;
2593 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002594 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2595 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002596 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2597
2598 /*
2599 * Argument for each entry of packed group
2600 */
2601 list_for_each_entry(prq, &packed->list, queuelist) {
2602 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2603 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2604 (prq->cmd_flags & REQ_META) &&
2605 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002606 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002607 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002608 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002609 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2610 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002611 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002612 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002613 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002614 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002615 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002616 packed->blocks += blk_rq_sectors(prq);
2617 i++;
2618 }
2619
2620 memset(brq, 0, sizeof(struct mmc_blk_request));
2621 brq->mrq.cmd = &brq->cmd;
2622 brq->mrq.data = &brq->data;
2623 brq->mrq.sbc = &brq->sbc;
2624 brq->mrq.stop = &brq->stop;
2625
2626 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2627 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2628 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2629
2630 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2631 brq->cmd.arg = blk_rq_pos(req);
2632 if (!mmc_card_blockaddr(card))
2633 brq->cmd.arg <<= 9;
2634 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2635
2636 brq->data.blksz = 512;
2637 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002638 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302639 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002640
2641 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2642 brq->stop.arg = 0;
2643 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2644
2645 mmc_set_data_timeout(&brq->data, card);
2646
2647 brq->data.sg = mqrq->sg;
2648 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2649
2650 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002651
2652 /*
2653 * This is intended for packed commands tests usage - in case these
2654 * functions are not in use the respective pointers are NULL
2655 */
2656 if (mq->err_check_fn)
2657 mqrq->mmc_active.err_check = mq->err_check_fn;
2658 else
2659 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2660
2661 if (mq->packed_test_fn)
2662 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002663
2664 mmc_queue_bounce_pre(mqrq);
2665}
2666
Adrian Hunter67716322011-08-29 16:42:15 +03002667static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2668 struct mmc_blk_request *brq, struct request *req,
2669 int ret)
2670{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002671 struct mmc_queue_req *mq_rq;
2672 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2673
Adrian Hunter67716322011-08-29 16:42:15 +03002674 /*
2675 * If this is an SD card and we're writing, we can first
2676 * mark the known good sectors as ok.
2677 *
2678 * If the card is not SD, we can still ok written sectors
2679 * as reported by the controller (which might be less than
2680 * the real number of written sectors, but never more).
2681 */
2682 if (mmc_card_sd(card)) {
2683 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302684 if (!brq->data.fault_injected) {
2685 blocks = mmc_sd_num_wr_blocks(card);
2686 if (blocks != (u32)-1)
2687 ret = blk_end_request(req, 0, blocks << 9);
2688 } else
2689 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002690 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002691 if (!mmc_packed_cmd(mq_rq->cmd_type))
2692 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002693 }
2694 return ret;
2695}
2696
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002697static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2698{
2699 struct request *prq;
2700 struct mmc_packed *packed = mq_rq->packed;
2701 int idx = packed->idx_failure, i = 0;
2702 int ret = 0;
2703
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002704 while (!list_empty(&packed->list)) {
2705 prq = list_entry_rq(packed->list.next);
2706 if (idx == i) {
2707 /* retry from error index */
2708 packed->nr_entries -= idx;
2709 mq_rq->req = prq;
2710 ret = 1;
2711
2712 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2713 list_del_init(&prq->queuelist);
2714 mmc_blk_clear_packed(mq_rq);
2715 }
2716 return ret;
2717 }
2718 list_del_init(&prq->queuelist);
2719 blk_end_request(prq, 0, blk_rq_bytes(prq));
2720 i++;
2721 }
2722
2723 mmc_blk_clear_packed(mq_rq);
2724 return ret;
2725}
2726
2727static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2728{
2729 struct request *prq;
2730 struct mmc_packed *packed = mq_rq->packed;
2731
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002732 while (!list_empty(&packed->list)) {
2733 prq = list_entry_rq(packed->list.next);
2734 list_del_init(&prq->queuelist);
2735 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2736 }
2737
2738 mmc_blk_clear_packed(mq_rq);
2739}
2740
2741static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2742 struct mmc_queue_req *mq_rq)
2743{
2744 struct request *prq;
2745 struct request_queue *q = mq->queue;
2746 struct mmc_packed *packed = mq_rq->packed;
2747
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002748 while (!list_empty(&packed->list)) {
2749 prq = list_entry_rq(packed->list.prev);
2750 if (prq->queuelist.prev != &packed->list) {
2751 list_del_init(&prq->queuelist);
2752 spin_lock_irq(q->queue_lock);
2753 blk_requeue_request(mq->queue, prq);
2754 spin_unlock_irq(q->queue_lock);
2755 } else {
2756 list_del_init(&prq->queuelist);
2757 }
2758 }
2759
2760 mmc_blk_clear_packed(mq_rq);
2761}
2762
Per Forlinee8a43a2011-07-01 18:55:33 +02002763static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02002764{
2765 struct mmc_blk_data *md = mq->data;
2766 struct mmc_card *card = md->queue.card;
2767 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002768 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002769 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002770 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302771 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02002772 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002773 const u8 packed_nr = 2;
2774 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002775#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2776 unsigned long waitfor = jiffies;
2777#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02002778
2779 if (!rqc && !mq->mqrq_prev->req)
2780 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002781
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002782 if (rqc)
2783 reqs = mmc_blk_prep_packed_list(mq, rqc);
2784
Per Forlin54d49d72011-07-01 18:55:29 +02002785 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002786 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302787 /*
2788 * When 4KB native sector is enabled, only 8 blocks
2789 * multiple read or write is allowed
2790 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00002791 if (mmc_large_sector(card) &&
2792 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302793 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
2794 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002795 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302796 goto cmd_abort;
2797 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002798
2799 if (reqs >= packed_nr)
2800 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2801 card, mq);
2802 else
2803 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002804 areq = &mq->mqrq_cur->mmc_active;
2805 } else
2806 areq = NULL;
2807 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002808 if (!areq) {
2809 if (status == MMC_BLK_NEW_REQUEST)
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02002810 set_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Per Forlinee8a43a2011-07-01 18:55:33 +02002811 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002812 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002813
Per Forlinee8a43a2011-07-01 18:55:33 +02002814 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2815 brq = &mq_rq->brq;
2816 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002817 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002818 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002819
Per Forlind78d4a82011-07-01 18:55:30 +02002820 switch (status) {
2821 case MMC_BLK_SUCCESS:
2822 case MMC_BLK_PARTIAL:
2823 /*
2824 * A block was successfully transferred.
2825 */
Adrian Hunter67716322011-08-29 16:42:15 +03002826 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002827
Mark Salyzyn6904e432016-01-28 11:12:25 -08002828 mmc_blk_simulate_delay(mq, rqc, waitfor);
2829
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002830 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2831 ret = mmc_blk_end_packed_req(mq_rq);
2832 break;
2833 } else {
2834 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002835 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002836 }
2837
Adrian Hunter67716322011-08-29 16:42:15 +03002838 /*
2839 * If the blk_end_request function returns non-zero even
2840 * though all data has been transferred and no errors
2841 * were returned by the host controller, it's a bug.
2842 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002843 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302844 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002845 __func__, blk_rq_bytes(req),
2846 brq->data.bytes_xfered);
2847 rqc = NULL;
2848 goto cmd_abort;
2849 }
Per Forlind78d4a82011-07-01 18:55:30 +02002850 break;
2851 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002852 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002853 if (mmc_blk_reset(md, card->host, type))
2854 goto cmd_abort;
2855 if (!ret)
2856 goto start_new_req;
2857 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002858 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002859 retune_retry_done = brq->retune_retry_done;
Maya Erezf93ca0a2014-12-09 23:34:41 +02002860 if (retry++ < MMC_BLK_MAX_RETRIES)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002861 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002862 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002863 case MMC_BLK_ABORT:
Maya Erezf93ca0a2014-12-09 23:34:41 +02002864 if (!mmc_blk_reset(md, card->host, type) &&
2865 (retry++ < (MMC_BLK_MAX_RETRIES + 1)))
Adrian Hunter67716322011-08-29 16:42:15 +03002866 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002867 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002868 case MMC_BLK_DATA_ERR: {
2869 int err;
2870
2871 err = mmc_blk_reset(md, card->host, type);
2872 if (!err)
2873 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05302874 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002875 }
2876 case MMC_BLK_ECC_ERR:
2877 if (brq->data.blocks > 1) {
2878 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002879 pr_warn("%s: retrying using single block read\n",
2880 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002881 disable_multi = 1;
2882 break;
2883 }
Per Forlind78d4a82011-07-01 18:55:30 +02002884 /*
2885 * After an error, we redo I/O one sector at a
2886 * time, so we only reach here after trying to
2887 * read a single sector.
2888 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302889 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002890 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002891 if (!ret)
2892 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002893 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302894 case MMC_BLK_NOMEDIUM:
2895 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002896 default:
2897 pr_err("%s: Unhandled return value (%d)",
2898 req->rq_disk->disk_name, status);
2899 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002900 }
2901
Per Forlinee8a43a2011-07-01 18:55:33 +02002902 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002903 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2904 if (!mq_rq->packed->retries)
2905 goto cmd_abort;
2906 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2907 mmc_start_req(card->host,
2908 &mq_rq->mmc_active, NULL);
2909 } else {
2910
2911 /*
2912 * In case of a incomplete request
2913 * prepare it again and resend.
2914 */
2915 mmc_blk_rw_rq_prep(mq_rq, card,
2916 disable_multi, mq);
2917 mmc_start_req(card->host,
2918 &mq_rq->mmc_active, NULL);
2919 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002920 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 } while (ret);
2923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return 1;
2925
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002926 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002927 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2928 mmc_blk_abort_packed_req(mq_rq);
2929 } else {
2930 if (mmc_card_removed(card))
2931 req->cmd_flags |= REQ_QUIET;
2932 while (ret)
2933 ret = blk_end_request(req, -EIO,
2934 blk_rq_cur_bytes(req));
2935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Per Forlinee8a43a2011-07-01 18:55:33 +02002937 start_new_req:
2938 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002939 if (mmc_card_removed(card)) {
2940 rqc->cmd_flags |= REQ_QUIET;
2941 blk_end_request_all(rqc, -EIO);
2942 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002943 /*
2944 * If current request is packed, it needs to put back.
2945 */
2946 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2947 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2948
Seungwon Jeon7a819022013-01-22 19:48:07 +09002949 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2950 mmc_start_req(card->host,
2951 &mq->mqrq_cur->mmc_active, NULL);
2952 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002953 }
2954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 return 0;
2956}
2957
Linus Walleij29eb7bd2016-09-20 11:34:38 +02002958int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002959{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002960 int ret;
2961 struct mmc_blk_data *md = mq->data;
2962 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002963 struct mmc_host *host = card->host;
2964 unsigned long flags;
Adrian Hunter869c5542016-08-25 14:11:43 -06002965 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002966
Per Forlinee8a43a2011-07-01 18:55:33 +02002967 if (req && !mq->mqrq_prev->req)
2968 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002969 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002970
Andrei Warkentin371a6892011-04-11 18:10:25 -05002971 ret = mmc_blk_part_switch(card, md);
2972 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002973 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302974 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002975 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002976 ret = 0;
2977 goto out;
2978 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002979
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002980 mmc_blk_write_packing_control(mq, req);
2981
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02002982 clear_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Mike Christiec2df40d2016-06-05 14:32:17 -05002983 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002984 /* complete ongoing async transfer before issuing discard */
2985 if (card->host->areq)
2986 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02002987 ret = mmc_blk_issue_discard_rq(mq, req);
2988 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
2989 /* complete ongoing async transfer before issuing secure erase*/
2990 if (card->host->areq)
2991 mmc_blk_issue_rw_rq(mq, NULL);
Maya Erez0c0609f2014-12-09 23:31:55 +02002992 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
2993 ret = mmc_blk_issue_secdiscard_rq(mq, req);
2994 else
2995 ret = mmc_blk_issue_discard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05002996 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002997 /* complete ongoing async transfer before issuing flush */
2998 if (card->host->areq)
2999 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003000 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003001 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003002 if (!req && host->areq) {
3003 spin_lock_irqsave(&host->context_info.lock, flags);
3004 host->context_info.is_waiting_last_req = true;
3005 spin_unlock_irqrestore(&host->context_info.lock, flags);
3006 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003007 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003008 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003009
Andrei Warkentin371a6892011-04-11 18:10:25 -05003010out:
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003011 if ((!req && !(test_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags))) ||
3012 req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09003013 /*
3014 * Release host when there are no more requests
3015 * and after special request(discard, flush) is done.
3016 * In case sepecial request, there is no reentry to
3017 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
3018 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003019 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003020 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07003021}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Russell Kinga6f6c962006-01-03 22:38:44 +00003023static inline int mmc_blk_readonly(struct mmc_card *card)
3024{
3025 return mmc_card_readonly(card) ||
3026 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
3027}
3028
Andrei Warkentin371a6892011-04-11 18:10:25 -05003029static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
3030 struct device *parent,
3031 sector_t size,
3032 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003033 const char *subname,
3034 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
3036 struct mmc_blk_data *md;
3037 int devidx, ret;
3038
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003039again:
3040 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
3041 return ERR_PTR(-ENOMEM);
3042
3043 spin_lock(&mmc_blk_lock);
3044 ret = ida_get_new(&mmc_blk_ida, &devidx);
3045 spin_unlock(&mmc_blk_lock);
3046
3047 if (ret == -EAGAIN)
3048 goto again;
3049 else if (ret)
3050 return ERR_PTR(ret);
3051
3052 if (devidx >= max_devices) {
3053 ret = -ENOSPC;
3054 goto out;
3055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003057 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00003058 if (!md) {
3059 ret = -ENOMEM;
3060 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 }
Russell Kinga6f6c962006-01-03 22:38:44 +00003062
Johan Rudholmadd710e2011-12-02 08:51:06 +01003063 md->area_type = area_type;
3064
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003065 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00003066 * Set the read-only status based on the supported commands
3067 * and the write protect switch.
3068 */
3069 md->read_only = mmc_blk_readonly(card);
3070
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003071 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00003072 if (md->disk == NULL) {
3073 ret = -ENOMEM;
3074 goto err_kfree;
3075 }
3076
3077 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003078 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00003079 md->usage = 1;
3080
Adrian Hunterd09408a2011-06-23 13:40:28 +03003081 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00003082 if (ret)
3083 goto err_putdisk;
3084
Russell Kinga6f6c962006-01-03 22:38:44 +00003085 md->queue.data = md;
3086
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003087 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003088 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00003089 md->disk->fops = &mmc_bdops;
3090 md->disk->private_data = md;
3091 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07003092 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003093 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07003094 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02003095 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02003096 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00003097
3098 /*
3099 * As discussed on lkml, GENHD_FL_REMOVABLE should:
3100 *
3101 * - be set for removable media with permanent block devices
3102 * - be unset for removable block devices with permanent media
3103 *
3104 * Since MMC block devices clearly fall under the second
3105 * case, we do not set GENHD_FL_REMOVABLE. Userspace
3106 * should use the block device creation/destruction hotplug
3107 * messages to tell when the card is present.
3108 */
3109
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003110 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02003111 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00003112
Saugata Dasa5075eb2012-05-17 16:32:21 +05303113 if (mmc_card_mmc(card))
3114 blk_queue_logical_block_size(md->queue.queue,
3115 card->ext_csd.data_sector_size);
3116 else
3117 blk_queue_logical_block_size(md->queue.queue, 512);
3118
Andrei Warkentin371a6892011-04-11 18:10:25 -05003119 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003120
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003121 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02003122 if ((mmc_card_mmc(card) &&
3123 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003124 (mmc_card_sd(card) &&
3125 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
3126 md->flags |= MMC_BLK_CMD23;
3127 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003128
3129 if (mmc_card_mmc(card) &&
3130 md->flags & MMC_BLK_CMD23 &&
3131 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
3132 card->ext_csd.rel_sectors)) {
3133 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06003134 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003135 }
3136
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003137 if (mmc_card_mmc(card) &&
3138 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
3139 (md->flags & MMC_BLK_CMD23) &&
3140 card->ext_csd.packed_event_en) {
3141 if (!mmc_packed_init(&md->queue, card))
3142 md->flags |= MMC_BLK_PACKED_CMD;
3143 }
3144
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00003146
3147 err_putdisk:
3148 put_disk(md->disk);
3149 err_kfree:
3150 kfree(md);
3151 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003152 spin_lock(&mmc_blk_lock);
3153 ida_remove(&mmc_blk_ida, devidx);
3154 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00003155 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156}
3157
Andrei Warkentin371a6892011-04-11 18:10:25 -05003158static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
3159{
3160 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003161
3162 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
3163 /*
3164 * The EXT_CSD sector count is in number or 512 byte
3165 * sectors.
3166 */
3167 size = card->ext_csd.sectors;
3168 } else {
3169 /*
3170 * The CSD capacity field is in units of read_blkbits.
3171 * set_capacity takes units of 512 bytes.
3172 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00003173 size = (typeof(sector_t))card->csd.capacity
3174 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003175 }
3176
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01003177 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003178 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003179}
3180
3181static int mmc_blk_alloc_part(struct mmc_card *card,
3182 struct mmc_blk_data *md,
3183 unsigned int part_type,
3184 sector_t size,
3185 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003186 const char *subname,
3187 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05003188{
3189 char cap_str[10];
3190 struct mmc_blk_data *part_md;
3191
3192 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003193 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003194 if (IS_ERR(part_md))
3195 return PTR_ERR(part_md);
3196 part_md->part_type = part_type;
3197 list_add(&part_md->part, &md->part);
3198
James Bottomleyb9f28d82015-03-05 18:47:01 -08003199 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05003200 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303201 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05003202 part_md->disk->disk_name, mmc_card_id(card),
3203 mmc_card_name(card), part_md->part_type, cap_str);
3204 return 0;
3205}
3206
Namjae Jeone0c368d2011-10-06 23:41:38 +09003207/* MMC Physical partitions consist of two boot partitions and
3208 * up to four general purpose partitions.
3209 * For each partition enabled in EXT_CSD a block device will be allocatedi
3210 * to provide access to the partition.
3211 */
3212
Andrei Warkentin371a6892011-04-11 18:10:25 -05003213static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
3214{
Namjae Jeone0c368d2011-10-06 23:41:38 +09003215 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003216
3217 if (!mmc_card_mmc(card))
3218 return 0;
3219
Namjae Jeone0c368d2011-10-06 23:41:38 +09003220 for (idx = 0; idx < card->nr_parts; idx++) {
3221 if (card->part[idx].size) {
3222 ret = mmc_blk_alloc_part(card, md,
3223 card->part[idx].part_cfg,
3224 card->part[idx].size >> 9,
3225 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003226 card->part[idx].name,
3227 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09003228 if (ret)
3229 return ret;
3230 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003231 }
3232
3233 return ret;
3234}
3235
Andrei Warkentin371a6892011-04-11 18:10:25 -05003236static void mmc_blk_remove_req(struct mmc_blk_data *md)
3237{
Johan Rudholmadd710e2011-12-02 08:51:06 +01003238 struct mmc_card *card;
3239
Andrei Warkentin371a6892011-04-11 18:10:25 -05003240 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003241 /*
3242 * Flush remaining requests and free queues. It
3243 * is freeing the queue that stops new requests
3244 * from being accepted.
3245 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02003246 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003247 mmc_cleanup_queue(&md->queue);
3248 if (md->flags & MMC_BLK_PACKED_CMD)
3249 mmc_packed_clean(&md->queue);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003250 device_remove_file(disk_to_dev(md->disk),
3251 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003252 if (md->disk->flags & GENHD_FL_UP) {
3253 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003254 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3255 card->ext_csd.boot_ro_lockable)
3256 device_remove_file(disk_to_dev(md->disk),
3257 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08003258#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3259 device_remove_file(disk_to_dev(md->disk),
3260 &dev_attr_max_write_speed);
3261 device_remove_file(disk_to_dev(md->disk),
3262 &dev_attr_max_read_speed);
3263 device_remove_file(disk_to_dev(md->disk),
3264 &dev_attr_cache_size);
3265#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05003266
Andrei Warkentin371a6892011-04-11 18:10:25 -05003267 del_gendisk(md->disk);
3268 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003269 mmc_blk_put(md);
3270 }
3271}
3272
3273static void mmc_blk_remove_parts(struct mmc_card *card,
3274 struct mmc_blk_data *md)
3275{
3276 struct list_head *pos, *q;
3277 struct mmc_blk_data *part_md;
3278
3279 list_for_each_safe(pos, q, &md->part) {
3280 part_md = list_entry(pos, struct mmc_blk_data, part);
3281 list_del(pos);
3282 mmc_blk_remove_req(part_md);
3283 }
3284}
3285
3286static int mmc_add_disk(struct mmc_blk_data *md)
3287{
3288 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003289 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003290
Dan Williams307d8e62016-06-20 10:40:44 -07003291 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003292 md->force_ro.show = force_ro_show;
3293 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05303294 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003295 md->force_ro.attr.name = "force_ro";
3296 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
3297 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
3298 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01003299 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003300#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3301 atomic_set(&md->queue.max_write_speed, max_write_speed);
3302 ret = device_create_file(disk_to_dev(md->disk),
3303 &dev_attr_max_write_speed);
3304 if (ret)
3305 goto max_write_speed_fail;
3306 atomic_set(&md->queue.max_read_speed, max_read_speed);
3307 ret = device_create_file(disk_to_dev(md->disk),
3308 &dev_attr_max_read_speed);
3309 if (ret)
3310 goto max_read_speed_fail;
3311 atomic_set(&md->queue.cache_size, cache_size);
3312 atomic_long_set(&md->queue.cache_used, 0);
3313 md->queue.cache_jiffies = jiffies;
3314 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3315 if (ret)
3316 goto cache_size_fail;
3317#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003318
3319 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3320 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04003321 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003322
3323 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
3324 mode = S_IRUGO;
3325 else
3326 mode = S_IRUGO | S_IWUSR;
3327
3328 md->power_ro_lock.show = power_ro_lock_show;
3329 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01003330 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003331 md->power_ro_lock.attr.mode = mode;
3332 md->power_ro_lock.attr.name =
3333 "ro_lock_until_next_power_on";
3334 ret = device_create_file(disk_to_dev(md->disk),
3335 &md->power_ro_lock);
3336 if (ret)
3337 goto power_ro_lock_fail;
3338 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003339
3340 md->num_wr_reqs_to_start_packing.show =
3341 num_wr_reqs_to_start_packing_show;
3342 md->num_wr_reqs_to_start_packing.store =
3343 num_wr_reqs_to_start_packing_store;
3344 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
3345 md->num_wr_reqs_to_start_packing.attr.name =
3346 "num_wr_reqs_to_start_packing";
3347 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
3348 ret = device_create_file(disk_to_dev(md->disk),
3349 &md->num_wr_reqs_to_start_packing);
3350 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02003351 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003352
Maya Erez5a8dae12014-12-04 15:13:59 +02003353 md->no_pack_for_random.show = no_pack_for_random_show;
3354 md->no_pack_for_random.store = no_pack_for_random_store;
3355 sysfs_attr_init(&md->no_pack_for_random.attr);
3356 md->no_pack_for_random.attr.name = "no_pack_for_random";
3357 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
3358 ret = device_create_file(disk_to_dev(md->disk),
3359 &md->no_pack_for_random);
3360 if (ret)
3361 goto no_pack_for_random_fails;
3362
Johan Rudholmadd710e2011-12-02 08:51:06 +01003363 return ret;
3364
Maya Erez5a8dae12014-12-04 15:13:59 +02003365no_pack_for_random_fails:
3366 device_remove_file(disk_to_dev(md->disk),
3367 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02003368num_wr_reqs_to_start_packing_fail:
3369 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003370power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08003371#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3372 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3373cache_size_fail:
3374 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
3375max_read_speed_fail:
3376 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
3377max_write_speed_fail:
3378#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003379 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
3380force_ro_fail:
3381 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003382
3383 return ret;
3384}
3385
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003386static const struct mmc_fixup blk_fixups[] =
3387{
Chris Ballc59d4472011-11-11 22:01:43 -05003388 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
3389 MMC_QUIRK_INAND_CMD38),
3390 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
3391 MMC_QUIRK_INAND_CMD38),
3392 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
3393 MMC_QUIRK_INAND_CMD38),
3394 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
3395 MMC_QUIRK_INAND_CMD38),
3396 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
3397 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003398
3399 /*
3400 * Some MMC cards experience performance degradation with CMD23
3401 * instead of CMD12-bounded multiblock transfers. For now we'll
3402 * black list what's bad...
3403 * - Certain Toshiba cards.
3404 *
3405 * N.B. This doesn't affect SD cards.
3406 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08003407 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3408 MMC_QUIRK_BLK_NO_CMD23),
3409 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3410 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003411 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003412 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003413 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003414 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003415 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003416 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003417
3418 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03003419 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003420 */
Chris Ballc59d4472011-11-11 22:01:43 -05003421 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003422 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03003423 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
3424 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003425
Ian Chen3550ccd2012-08-29 15:05:36 +09003426 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08003427 * Some Samsung MMC cards need longer data read timeout than
3428 * indicated in CSD.
3429 */
3430 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
3431 MMC_QUIRK_LONG_READ_TIME),
3432
3433 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09003434 * On these Samsung MoviNAND parts, performing secure erase or
3435 * secure trim can result in unrecoverable corruption due to a
3436 * firmware bug.
3437 */
3438 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3439 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3440 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3441 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3442 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3443 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3444 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3445 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3446 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3447 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3448 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3449 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3450 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3451 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3452 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3453 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3454
Shawn Linb5b4ff02015-08-12 13:08:32 +08003455 /*
3456 * On Some Kingston eMMCs, performing trim can result in
3457 * unrecoverable data conrruption occasionally due to a firmware bug.
3458 */
3459 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3460 MMC_QUIRK_TRIM_BROKEN),
3461 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3462 MMC_QUIRK_TRIM_BROKEN),
3463
Pratibhasagar V8d664e32014-12-03 18:26:42 +02003464 /* Some INAND MCP devices advertise incorrect timeout values */
3465 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
3466 MMC_QUIRK_INAND_DATA_TIMEOUT),
3467
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003468 END_FIXUP
3469};
3470
Ulf Hansson96541ba2015-04-14 13:06:12 +02003471static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003473 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003474 char cap_str[10];
3475
Pierre Ossman912490d2005-05-21 10:27:02 +01003476 /*
3477 * Check that the card supports the command class(es) we need.
3478 */
3479 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 return -ENODEV;
3481
Lukas Czerner5204d002014-06-18 13:18:07 +02003482 mmc_fixup_device(card, blk_fixups);
3483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 md = mmc_blk_alloc(card);
3485 if (IS_ERR(md))
3486 return PTR_ERR(md);
3487
James Bottomleyb9f28d82015-03-05 18:47:01 -08003488 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003489 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303490 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003492 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Andrei Warkentin371a6892011-04-11 18:10:25 -05003494 if (mmc_blk_alloc_parts(card, md))
3495 goto out;
3496
Ulf Hansson96541ba2015-04-14 13:06:12 +02003497 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003498
Andrei Warkentin371a6892011-04-11 18:10:25 -05003499 if (mmc_add_disk(md))
3500 goto out;
3501
3502 list_for_each_entry(part_md, &md->part, part) {
3503 if (mmc_add_disk(part_md))
3504 goto out;
3505 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003506
3507 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
3508 pm_runtime_use_autosuspend(&card->dev);
3509
3510 /*
3511 * Don't enable runtime PM for SD-combo cards here. Leave that
3512 * decision to be taken during the SDIO init sequence instead.
3513 */
3514 if (card->type != MMC_TYPE_SD_COMBO) {
3515 pm_runtime_set_active(&card->dev);
3516 pm_runtime_enable(&card->dev);
3517 }
3518
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 return 0;
3520
3521 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05003522 mmc_blk_remove_parts(card, md);
3523 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01003524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525}
3526
Ulf Hansson96541ba2015-04-14 13:06:12 +02003527static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003529 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
Andrei Warkentin371a6892011-04-11 18:10:25 -05003531 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003532 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003533 mmc_claim_host(card->host);
3534 mmc_blk_part_switch(card, md);
3535 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003536 if (card->type != MMC_TYPE_SD_COMBO)
3537 pm_runtime_disable(&card->dev);
3538 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003539 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02003540 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541}
3542
Ulf Hansson96541ba2015-04-14 13:06:12 +02003543static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003545 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02003546 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303547 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
3549 if (md) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05303550 rc = mmc_queue_suspend(&md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303551 if (rc)
3552 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003553 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05303554 rc = mmc_queue_suspend(&part_md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303555 if (rc)
3556 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303559 goto out;
3560
3561 out_resume:
3562 mmc_queue_resume(&md->queue);
3563 list_for_each_entry(part_md, &md->part, part) {
3564 mmc_queue_resume(&part_md->queue);
3565 }
3566 out:
3567 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568}
3569
Ulf Hansson96541ba2015-04-14 13:06:12 +02003570static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02003571{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003572 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003573}
3574
Ulf Hansson0967edc2014-10-06 11:29:42 +02003575#ifdef CONFIG_PM_SLEEP
3576static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02003577{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003578 struct mmc_card *card = mmc_dev_to_card(dev);
3579
3580 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003581}
3582
Ulf Hansson0967edc2014-10-06 11:29:42 +02003583static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003585 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02003586 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
3588 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003589 /*
3590 * Resume involves the card going into idle state,
3591 * so current partition is always the main one.
3592 */
3593 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003595 list_for_each_entry(part_md, &md->part, part) {
3596 mmc_queue_resume(&part_md->queue);
3597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 }
3599 return 0;
3600}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601#endif
3602
Ulf Hansson0967edc2014-10-06 11:29:42 +02003603static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3604
Ulf Hansson96541ba2015-04-14 13:06:12 +02003605static struct mmc_driver mmc_driver = {
3606 .drv = {
3607 .name = "mmcblk",
3608 .pm = &mmc_blk_pm_ops,
3609 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 .probe = mmc_blk_probe,
3611 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02003612 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613};
3614
3615static int __init mmc_blk_init(void)
3616{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003617 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003619 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3620 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3621
Ben Hutchingsa26eba62014-11-06 03:35:09 +00003622 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003623
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003624 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3625 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003628 res = mmc_register_driver(&mmc_driver);
3629 if (res)
3630 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003632 return 0;
3633 out2:
3634 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 out:
3636 return res;
3637}
3638
3639static void __exit mmc_blk_exit(void)
3640{
3641 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003642 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643}
3644
3645module_init(mmc_blk_init);
3646module_exit(mmc_blk_exit);
3647
3648MODULE_LICENSE("GPL");
3649MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3650