blob: 3b540d69af4686a1d0c2309e4b668999ca35039f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020037#include <linux/pm_runtime.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020038#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
John Calixtocb87ea22011-04-26 18:56:29 -040040#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020042#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010043#include <linux/mmc/mmc.h>
44#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/uaccess.h>
47
Pierre Ossman98ac2162006-12-23 20:03:02 +010048#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080049#include "block.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000051MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040052#ifdef MODULE_PARAM_PREFIX
53#undef MODULE_PARAM_PREFIX
54#endif
55#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010056
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050057#define INAND_CMD38_ARG_EXT_CSD 113
58#define INAND_CMD38_ARG_ERASE 0x00
59#define INAND_CMD38_ARG_TRIM 0x01
60#define INAND_CMD38_ARG_SECERASE 0x80
61#define INAND_CMD38_ARG_SECTRIM1 0x81
62#define INAND_CMD38_ARG_SECTRIM2 0x88
Subhash Jadavani2fbab612014-12-04 15:16:17 +020063#define MMC_BLK_TIMEOUT_MS (30 * 1000) /* 30 sec timeout */
Maya Erez775a9362013-04-18 15:41:55 +030064#define MMC_SANITIZE_REQ_TIMEOUT 240000
65#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050066
Luca Porziod3df0462015-11-06 15:12:26 +000067#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090068 (rq_data_dir(req) == WRITE))
69#define PACKED_CMD_VER 0x01
70#define PACKED_CMD_WR 0x02
Lee Susman841fd132013-04-23 17:59:26 +030071#define PACKED_TRIGGER_MAX_ELEMENTS 5000
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090072
Tatyana Brokhman08238ce2012-10-07 10:33:13 +020073#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
74 do { \
75 if (stats->enabled) \
76 stats->pack_stop_reason[reason]++; \
77 } while (0)
78
Lee Susman841fd132013-04-23 17:59:26 +030079#define PCKD_TRGR_INIT_MEAN_POTEN 17
80#define PCKD_TRGR_POTEN_LOWER_BOUND 5
81#define PCKD_TRGR_URGENT_PENALTY 2
82#define PCKD_TRGR_LOWER_BOUND 5
83#define PCKD_TRGR_PRECISION_MULTIPLIER 100
84
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020085static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040086
87/*
88 * The defaults come from config options but can be overriden by module
89 * or bootarg options.
90 */
91static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
92
93/*
94 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000095 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020096 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040097 */
98static int max_devices;
99
Ben Hutchingsa26eba62014-11-06 03:35:09 +0000100#define MAX_DEVICES 256
101
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200102static DEFINE_IDA(mmc_blk_ida);
103static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
106 * There is one mmc_blk_data per slot.
107 */
108struct mmc_blk_data {
109 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -0700110 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 struct gendisk *disk;
112 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500113 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500115 unsigned int flags;
116#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
117#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900118#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000121 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500122 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300123 unsigned int reset_done;
124#define MMC_BLK_READ BIT(0)
125#define MMC_BLK_WRITE BIT(1)
126#define MMC_BLK_DISCARD BIT(2)
127#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500128
129 /*
130 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200131 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500132 * track of the current selected device partition.
133 */
134 unsigned int part_curr;
135 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100136 struct device_attribute power_ro_lock;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200137 struct device_attribute num_wr_reqs_to_start_packing;
Maya Erez5a8dae12014-12-04 15:13:59 +0200138 struct device_attribute no_pack_for_random;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100139 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
Arjan van de Vena621aae2006-01-12 18:43:35 +0000142static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900144enum {
145 MMC_PACKED_NR_IDX = -1,
146 MMC_PACKED_NR_ZERO,
147 MMC_PACKED_NR_SINGLE,
148};
149
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400150module_param(perdev_minors, int, 0444);
151MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
152
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200153static inline int mmc_blk_part_switch(struct mmc_card *card,
154 struct mmc_blk_data *md);
155static int get_card_status(struct mmc_card *card, u32 *status, int retries);
156
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900157static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
158{
159 struct mmc_packed *packed = mqrq->packed;
160
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900161 mqrq->cmd_type = MMC_PACKED_NONE;
162 packed->nr_entries = MMC_PACKED_NR_ZERO;
163 packed->idx_failure = MMC_PACKED_NR_IDX;
164 packed->retries = 0;
165 packed->blocks = 0;
166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
169{
170 struct mmc_blk_data *md;
171
Arjan van de Vena621aae2006-01-12 18:43:35 +0000172 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 md = disk->private_data;
174 if (md && md->usage == 0)
175 md = NULL;
176 if (md)
177 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000178 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 return md;
181}
182
Andrei Warkentin371a6892011-04-11 18:10:25 -0500183static inline int mmc_get_devidx(struct gendisk *disk)
184{
Colin Cross382c55f2015-10-22 10:00:41 -0700185 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500186 return devidx;
187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static void mmc_blk_put(struct mmc_blk_data *md)
190{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000191 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 md->usage--;
193 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500194 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800195 blk_cleanup_queue(md->queue.queue);
196
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200197 spin_lock(&mmc_blk_lock);
198 ida_remove(&mmc_blk_ida, devidx);
199 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 kfree(md);
203 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000204 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Johan Rudholmadd710e2011-12-02 08:51:06 +0100207static ssize_t power_ro_lock_show(struct device *dev,
208 struct device_attribute *attr, char *buf)
209{
210 int ret;
211 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200212 struct mmc_card *card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100213 int locked = 0;
214
Asutosh Das507d9a72014-12-09 10:15:53 +0200215 if (!md)
216 return -EINVAL;
217
218 card = md->queue.card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100219 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
220 locked = 2;
221 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
222 locked = 1;
223
224 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
225
Tomas Winkler9098f842015-07-16 15:50:45 +0200226 mmc_blk_put(md);
227
Johan Rudholmadd710e2011-12-02 08:51:06 +0100228 return ret;
229}
230
231static ssize_t power_ro_lock_store(struct device *dev,
232 struct device_attribute *attr, const char *buf, size_t count)
233{
234 int ret;
235 struct mmc_blk_data *md, *part_md;
236 struct mmc_card *card;
237 unsigned long set;
238
239 if (kstrtoul(buf, 0, &set))
240 return -EINVAL;
241
242 if (set != 1)
243 return count;
244
245 md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200246 if (!md)
247 return -EINVAL;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100248 card = md->queue.card;
249
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200250 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100251
252 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
253 card->ext_csd.boot_ro_lock |
254 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
255 card->ext_csd.part_time);
256 if (ret)
257 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
258 else
259 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
260
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200261 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100262
263 if (!ret) {
264 pr_info("%s: Locking boot partition ro until next power on\n",
265 md->disk->disk_name);
266 set_disk_ro(md->disk, 1);
267
268 list_for_each_entry(part_md, &md->part, part)
269 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
270 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
271 set_disk_ro(part_md->disk, 1);
272 }
273 }
274
275 mmc_blk_put(md);
276 return count;
277}
278
Andrei Warkentin371a6892011-04-11 18:10:25 -0500279static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
280 char *buf)
281{
282 int ret;
283 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
284
Asutosh Das507d9a72014-12-09 10:15:53 +0200285 if (!md)
286 return -EINVAL;
287
Baruch Siach0031a982014-09-22 10:12:51 +0300288 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500289 get_disk_ro(dev_to_disk(dev)) ^
290 md->read_only);
291 mmc_blk_put(md);
292 return ret;
293}
294
295static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
296 const char *buf, size_t count)
297{
298 int ret;
299 char *end;
300 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
301 unsigned long set = simple_strtoul(buf, &end, 0);
Asutosh Das507d9a72014-12-09 10:15:53 +0200302
303 if (!md)
304 return -EINVAL;
305
Andrei Warkentin371a6892011-04-11 18:10:25 -0500306 if (end == buf) {
307 ret = -EINVAL;
308 goto out;
309 }
310
311 set_disk_ro(dev_to_disk(dev), set || md->read_only);
312 ret = count;
313out:
314 mmc_blk_put(md);
315 return ret;
316}
317
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200318static ssize_t
Maya Erez5a8dae12014-12-04 15:13:59 +0200319no_pack_for_random_show(struct device *dev,
320 struct device_attribute *attr, char *buf)
321{
322 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
323 int ret;
324
Asutosh Das507d9a72014-12-09 10:15:53 +0200325 if (!md)
326 return -EINVAL;
Maya Erez5a8dae12014-12-04 15:13:59 +0200327 ret = snprintf(buf, PAGE_SIZE, "%d\n", md->queue.no_pack_for_random);
328
329 mmc_blk_put(md);
330 return ret;
331}
332
333static ssize_t
334no_pack_for_random_store(struct device *dev,
335 struct device_attribute *attr,
336 const char *buf, size_t count)
337{
338 int value;
339 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200340 struct mmc_card *card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200341 int ret = count;
342
Asutosh Das507d9a72014-12-09 10:15:53 +0200343 if (!md)
344 return -EINVAL;
345
346 card = md->queue.card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200347 if (!card) {
348 ret = -EINVAL;
349 goto exit;
350 }
351
352 sscanf(buf, "%d", &value);
353
354 if (value < 0) {
355 pr_err("%s: value %d is not valid. old value remains = %d",
356 mmc_hostname(card->host), value,
357 md->queue.no_pack_for_random);
358 ret = -EINVAL;
359 goto exit;
360 }
361
362 md->queue.no_pack_for_random = (value > 0) ? true : false;
363
364 pr_debug("%s: no_pack_for_random: new value = %d",
365 mmc_hostname(card->host),
366 md->queue.no_pack_for_random);
367
368exit:
369 mmc_blk_put(md);
370 return ret;
371}
372
373static ssize_t
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200374num_wr_reqs_to_start_packing_show(struct device *dev,
375 struct device_attribute *attr, char *buf)
376{
377 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
378 int num_wr_reqs_to_start_packing;
379 int ret;
380
Asutosh Das507d9a72014-12-09 10:15:53 +0200381 if (!md)
382 return -EINVAL;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200383 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
384
385 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
386
387 mmc_blk_put(md);
388 return ret;
389}
390
391static ssize_t
392num_wr_reqs_to_start_packing_store(struct device *dev,
393 struct device_attribute *attr,
394 const char *buf, size_t count)
395{
396 int value;
397 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200398 struct mmc_card *card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200399 int ret = count;
400
Asutosh Das507d9a72014-12-09 10:15:53 +0200401 if (!md)
402 return -EINVAL;
403
404 card = md->queue.card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200405 if (!card) {
406 ret = -EINVAL;
407 goto exit;
408 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200409
410 sscanf(buf, "%d", &value);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200411
Yaniv Gardi42399822014-12-04 00:26:23 +0200412 if (value >= 0) {
413 md->queue.num_wr_reqs_to_start_packing =
414 min_t(int, value, (int)card->ext_csd.max_packed_writes);
415
416 pr_debug("%s: trigger to pack: new value = %d",
417 mmc_hostname(card->host),
418 md->queue.num_wr_reqs_to_start_packing);
419 } else {
420 pr_err("%s: value %d is not valid. old value remains = %d",
421 mmc_hostname(card->host), value,
422 md->queue.num_wr_reqs_to_start_packing);
423 ret = -EINVAL;
424 }
425
426exit:
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200427 mmc_blk_put(md);
Yaniv Gardi42399822014-12-04 00:26:23 +0200428 return ret;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200429}
430
Mark Salyzyn6904e432016-01-28 11:12:25 -0800431#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
432
433static int max_read_speed, max_write_speed, cache_size = 4;
434
435module_param(max_read_speed, int, S_IRUSR | S_IRGRP);
436MODULE_PARM_DESC(max_read_speed, "maximum KB/s read speed 0=off");
437module_param(max_write_speed, int, S_IRUSR | S_IRGRP);
438MODULE_PARM_DESC(max_write_speed, "maximum KB/s write speed 0=off");
439module_param(cache_size, int, S_IRUSR | S_IRGRP);
440MODULE_PARM_DESC(cache_size, "MB high speed memory or SLC cache");
441
442/*
443 * helper macros and expectations:
444 * size - unsigned long number of bytes
445 * jiffies - unsigned long HZ timestamp difference
446 * speed - unsigned KB/s transfer rate
447 */
448#define size_and_speed_to_jiffies(size, speed) \
449 ((size) * HZ / (speed) / 1024UL)
450#define jiffies_and_speed_to_size(jiffies, speed) \
451 (((speed) * (jiffies) * 1024UL) / HZ)
452#define jiffies_and_size_to_speed(jiffies, size) \
453 ((size) * HZ / (jiffies) / 1024UL)
454
455/* Limits to report warning */
456/* jiffies_and_size_to_speed(10*HZ, queue_max_hw_sectors(q) * 512UL) ~ 25 */
457#define MIN_SPEED(q) 250 /* 10 times faster than a floppy disk */
458#define MAX_SPEED(q) jiffies_and_size_to_speed(1, queue_max_sectors(q) * 512UL)
459
460#define speed_valid(speed) ((speed) > 0)
461
462static const char off[] = "off\n";
463
464static int max_speed_show(int speed, char *buf)
465{
466 if (speed)
467 return scnprintf(buf, PAGE_SIZE, "%uKB/s\n", speed);
468 else
469 return scnprintf(buf, PAGE_SIZE, off);
470}
471
472static int max_speed_store(const char *buf, struct request_queue *q)
473{
474 unsigned int limit, set = 0;
475
476 if (!strncasecmp(off, buf, sizeof(off) - 2))
477 return set;
478 if (kstrtouint(buf, 0, &set) || (set > INT_MAX))
479 return -EINVAL;
480 if (set == 0)
481 return set;
482 limit = MAX_SPEED(q);
483 if (set > limit)
484 pr_warn("max speed %u ineffective above %u\n", set, limit);
485 limit = MIN_SPEED(q);
486 if (set < limit)
487 pr_warn("max speed %u painful below %u\n", set, limit);
488 return set;
489}
490
491static ssize_t max_write_speed_show(struct device *dev,
492 struct device_attribute *attr, char *buf)
493{
494 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
495 int ret = max_speed_show(atomic_read(&md->queue.max_write_speed), buf);
496
497 mmc_blk_put(md);
498 return ret;
499}
500
501static ssize_t max_write_speed_store(struct device *dev,
502 struct device_attribute *attr,
503 const char *buf, size_t count)
504{
505 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
506 int set = max_speed_store(buf, md->queue.queue);
507
508 if (set < 0) {
509 mmc_blk_put(md);
510 return set;
511 }
512
513 atomic_set(&md->queue.max_write_speed, set);
514 mmc_blk_put(md);
515 return count;
516}
517
518static const DEVICE_ATTR(max_write_speed, S_IRUGO | S_IWUSR,
519 max_write_speed_show, max_write_speed_store);
520
521static ssize_t max_read_speed_show(struct device *dev,
522 struct device_attribute *attr, char *buf)
523{
524 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
525 int ret = max_speed_show(atomic_read(&md->queue.max_read_speed), buf);
526
527 mmc_blk_put(md);
528 return ret;
529}
530
531static ssize_t max_read_speed_store(struct device *dev,
532 struct device_attribute *attr,
533 const char *buf, size_t count)
534{
535 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
536 int set = max_speed_store(buf, md->queue.queue);
537
538 if (set < 0) {
539 mmc_blk_put(md);
540 return set;
541 }
542
543 atomic_set(&md->queue.max_read_speed, set);
544 mmc_blk_put(md);
545 return count;
546}
547
548static const DEVICE_ATTR(max_read_speed, S_IRUGO | S_IWUSR,
549 max_read_speed_show, max_read_speed_store);
550
551static ssize_t cache_size_show(struct device *dev,
552 struct device_attribute *attr, char *buf)
553{
554 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
555 struct mmc_queue *mq = &md->queue;
556 int cache_size = atomic_read(&mq->cache_size);
557 int ret;
558
559 if (!cache_size)
560 ret = scnprintf(buf, PAGE_SIZE, off);
561 else {
562 int speed = atomic_read(&mq->max_write_speed);
563
564 if (!speed_valid(speed))
565 ret = scnprintf(buf, PAGE_SIZE, "%uMB\n", cache_size);
566 else { /* We accept race between cache_jiffies and cache_used */
567 unsigned long size = jiffies_and_speed_to_size(
568 jiffies - mq->cache_jiffies, speed);
569 long used = atomic_long_read(&mq->cache_used);
570
571 if (size >= used)
572 size = 0;
573 else
574 size = (used - size) * 100 / cache_size
575 / 1024UL / 1024UL;
576
577 ret = scnprintf(buf, PAGE_SIZE, "%uMB %lu%% used\n",
578 cache_size, size);
579 }
580 }
581
582 mmc_blk_put(md);
583 return ret;
584}
585
586static ssize_t cache_size_store(struct device *dev,
587 struct device_attribute *attr,
588 const char *buf, size_t count)
589{
590 struct mmc_blk_data *md;
591 unsigned int set = 0;
592
593 if (strncasecmp(off, buf, sizeof(off) - 2)
594 && (kstrtouint(buf, 0, &set) || (set > INT_MAX)))
595 return -EINVAL;
596
597 md = mmc_blk_get(dev_to_disk(dev));
598 atomic_set(&md->queue.cache_size, set);
599 mmc_blk_put(md);
600 return count;
601}
602
603static const DEVICE_ATTR(cache_size, S_IRUGO | S_IWUSR,
604 cache_size_show, cache_size_store);
605
606/* correct for write-back */
607static long mmc_blk_cache_used(struct mmc_queue *mq, unsigned long waitfor)
608{
609 long used = 0;
610 int speed = atomic_read(&mq->max_write_speed);
611
612 if (speed_valid(speed)) {
613 unsigned long size = jiffies_and_speed_to_size(
614 waitfor - mq->cache_jiffies, speed);
615 used = atomic_long_read(&mq->cache_used);
616
617 if (size >= used)
618 used = 0;
619 else
620 used -= size;
621 }
622
623 atomic_long_set(&mq->cache_used, used);
624 mq->cache_jiffies = waitfor;
625
626 return used;
627}
628
629static void mmc_blk_simulate_delay(
630 struct mmc_queue *mq,
631 struct request *req,
632 unsigned long waitfor)
633{
634 int max_speed;
635
636 if (!req)
637 return;
638
639 max_speed = (rq_data_dir(req) == READ)
640 ? atomic_read(&mq->max_read_speed)
641 : atomic_read(&mq->max_write_speed);
642 if (speed_valid(max_speed)) {
643 unsigned long bytes = blk_rq_bytes(req);
644
645 if (rq_data_dir(req) != READ) {
646 int cache_size = atomic_read(&mq->cache_size);
647
648 if (cache_size) {
649 unsigned long size = cache_size * 1024L * 1024L;
650 long used = mmc_blk_cache_used(mq, waitfor);
651
652 used += bytes;
653 atomic_long_set(&mq->cache_used, used);
654 bytes = 0;
655 if (used > size)
656 bytes = used - size;
657 }
658 }
659 waitfor += size_and_speed_to_jiffies(bytes, max_speed);
660 if (time_is_after_jiffies(waitfor)) {
661 long msecs = jiffies_to_msecs(waitfor - jiffies);
662
663 if (likely(msecs > 0))
664 msleep(msecs);
665 }
666 }
667}
668
669#else
670
671#define mmc_blk_simulate_delay(mq, req, waitfor)
672
673#endif
674
Al Viroa5a15612008-03-02 10:33:30 -0500675static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Al Viroa5a15612008-03-02 10:33:30 -0500677 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 int ret = -ENXIO;
679
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200680 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (md) {
682 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500683 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700685
Al Viroa5a15612008-03-02 10:33:30 -0500686 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700687 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700688 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200691 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 return ret;
694}
695
Al Virodb2a1442013-05-05 21:52:57 -0400696static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Al Viroa5a15612008-03-02 10:33:30 -0500698 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200700 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200702 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
705static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800706mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800708 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
709 geo->heads = 4;
710 geo->sectors = 16;
711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
John Calixtocb87ea22011-04-26 18:56:29 -0400714struct mmc_blk_ioc_data {
715 struct mmc_ioc_cmd ic;
716 unsigned char *buf;
717 u64 buf_bytes;
718};
719
720static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
721 struct mmc_ioc_cmd __user *user)
722{
723 struct mmc_blk_ioc_data *idata;
724 int err;
725
yalin wang1ff89502015-11-12 19:27:11 +0800726 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400727 if (!idata) {
728 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400729 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400730 }
731
732 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
733 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400734 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400735 }
736
737 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
738 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
739 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400740 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400741 }
742
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300743 if (!idata->buf_bytes) {
744 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100745 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300746 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100747
yalin wang1ff89502015-11-12 19:27:11 +0800748 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400749 if (!idata->buf) {
750 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400751 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400752 }
753
754 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
755 idata->ic.data_ptr, idata->buf_bytes)) {
756 err = -EFAULT;
757 goto copy_err;
758 }
759
760 return idata;
761
762copy_err:
763 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400764idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400765 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400766out:
John Calixtocb87ea22011-04-26 18:56:29 -0400767 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400768}
769
Jon Huntera5f57742015-09-22 10:27:53 +0100770static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
771 struct mmc_blk_ioc_data *idata)
772{
773 struct mmc_ioc_cmd *ic = &idata->ic;
774
775 if (copy_to_user(&(ic_ptr->response), ic->response,
776 sizeof(ic->response)))
777 return -EFAULT;
778
779 if (!idata->ic.write_flag) {
780 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
781 idata->buf, idata->buf_bytes))
782 return -EFAULT;
783 }
784
785 return 0;
786}
787
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200788static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
789 u32 retries_max)
790{
791 int err;
792 u32 retry_count = 0;
793
794 if (!status || !retries_max)
795 return -EINVAL;
796
797 do {
798 err = get_card_status(card, status, 5);
799 if (err)
800 break;
801
802 if (!R1_STATUS(*status) &&
803 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
804 break; /* RPMB programming operation complete */
805
806 /*
807 * Rechedule to give the MMC device a chance to continue
808 * processing the previous command without being polled too
809 * frequently.
810 */
811 usleep_range(1000, 5000);
812 } while (++retry_count < retries_max);
813
814 if (retry_count == retries_max)
815 err = -EPERM;
816
817 return err;
818}
819
Maya Erez775a9362013-04-18 15:41:55 +0300820static int ioctl_do_sanitize(struct mmc_card *card)
821{
822 int err;
823
Ulf Hanssona2d10862013-12-16 14:37:26 +0100824 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300825 pr_warn("%s: %s - SANITIZE is not supported\n",
826 mmc_hostname(card->host), __func__);
827 err = -EOPNOTSUPP;
828 goto out;
829 }
830
831 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
832 mmc_hostname(card->host), __func__);
833
834 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
835 EXT_CSD_SANITIZE_START, 1,
836 MMC_SANITIZE_REQ_TIMEOUT);
837
838 if (err)
839 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
840 mmc_hostname(card->host), __func__, err);
841
842 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
843 __func__);
844out:
845 return err;
846}
847
Jon Huntera5f57742015-09-22 10:27:53 +0100848static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
849 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400850{
John Calixtocb87ea22011-04-26 18:56:29 -0400851 struct mmc_command cmd = {0};
852 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530853 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400854 struct scatterlist sg;
855 int err;
856
Jon Huntera5f57742015-09-22 10:27:53 +0100857 if (!card || !md || !idata)
858 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400859
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100860 cmd.opcode = idata->ic.opcode;
861 cmd.arg = idata->ic.arg;
862 cmd.flags = idata->ic.flags;
863
864 if (idata->buf_bytes) {
865 data.sg = &sg;
866 data.sg_len = 1;
867 data.blksz = idata->ic.blksz;
868 data.blocks = idata->ic.blocks;
869
870 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
871
872 if (idata->ic.write_flag)
873 data.flags = MMC_DATA_WRITE;
874 else
875 data.flags = MMC_DATA_READ;
876
877 /* data.flags must already be set before doing this. */
878 mmc_set_data_timeout(&data, card);
879
880 /* Allow overriding the timeout_ns for empirical tuning. */
881 if (idata->ic.data_timeout_ns)
882 data.timeout_ns = idata->ic.data_timeout_ns;
883
884 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
885 /*
886 * Pretend this is a data transfer and rely on the
887 * host driver to compute timeout. When all host
888 * drivers support cmd.cmd_timeout for R1B, this
889 * can be changed to:
890 *
891 * mrq.data = NULL;
892 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
893 */
894 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
895 }
896
897 mrq.data = &data;
898 }
899
900 mrq.cmd = &cmd;
901
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200902 err = mmc_blk_part_switch(card, md);
903 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100904 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200905
John Calixtocb87ea22011-04-26 18:56:29 -0400906 if (idata->ic.is_acmd) {
907 err = mmc_app_cmd(card->host, card);
908 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100909 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400910 }
911
Yaniv Gardia82e4842013-06-05 14:13:08 +0300912 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
913 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300914 err = ioctl_do_sanitize(card);
915
916 if (err)
917 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
918 __func__, err);
919
Jon Huntera5f57742015-09-22 10:27:53 +0100920 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300921 }
922
John Calixtocb87ea22011-04-26 18:56:29 -0400923 mmc_wait_for_req(card->host, &mrq);
924
925 if (cmd.error) {
926 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
927 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100928 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400929 }
930 if (data.error) {
931 dev_err(mmc_dev(card->host), "%s: data error %d\n",
932 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100933 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400934 }
935
936 /*
937 * According to the SD specs, some commands require a delay after
938 * issuing the command.
939 */
940 if (idata->ic.postsleep_min_us)
941 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
942
Jon Huntera5f57742015-09-22 10:27:53 +0100943 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400944
Krishna Kondae6711632014-12-04 15:20:57 +0200945 return err;
946}
947
948struct mmc_blk_ioc_rpmb_data {
949 struct mmc_blk_ioc_data *data[MMC_IOC_MAX_RPMB_CMD];
950};
951
952static struct mmc_blk_ioc_rpmb_data *mmc_blk_ioctl_rpmb_copy_from_user(
953 struct mmc_ioc_rpmb __user *user)
954{
955 struct mmc_blk_ioc_rpmb_data *idata;
956 int err, i;
957
958 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
959 if (!idata) {
960 err = -ENOMEM;
961 goto out;
962 }
963
964 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
965 idata->data[i] = mmc_blk_ioctl_copy_from_user(&(user->cmds[i]));
966 if (IS_ERR(idata->data[i])) {
967 err = PTR_ERR(idata->data[i]);
968 goto copy_err;
969 }
970 }
971
972 return idata;
973
974copy_err:
975 while (--i >= 0) {
976 kfree(idata->data[i]->buf);
977 kfree(idata->data[i]);
978 }
979 kfree(idata);
980out:
981 return ERR_PTR(err);
982}
983
984static int mmc_blk_ioctl_rpmb_cmd(struct block_device *bdev,
985 struct mmc_ioc_rpmb __user *ic_ptr)
986{
987 struct mmc_blk_ioc_rpmb_data *idata;
988 struct mmc_blk_data *md;
989 struct mmc_card *card;
990 struct mmc_command cmd = {0};
991 struct mmc_data data = {0};
992 struct mmc_request mrq = {NULL};
993 struct scatterlist sg;
994 int err = 0, i = 0;
995 u32 status = 0;
996
997 /* The caller must have CAP_SYS_RAWIO */
998 if (!capable(CAP_SYS_RAWIO))
999 return -EPERM;
1000
1001 md = mmc_blk_get(bdev->bd_disk);
1002 /* make sure this is a rpmb partition */
1003 if ((!md) || (!(md->area_type & MMC_BLK_DATA_AREA_RPMB))) {
1004 err = -EINVAL;
Asutosh Das507d9a72014-12-09 10:15:53 +02001005 return err;
Krishna Kondae6711632014-12-04 15:20:57 +02001006 }
1007
1008 idata = mmc_blk_ioctl_rpmb_copy_from_user(ic_ptr);
1009 if (IS_ERR(idata)) {
1010 err = PTR_ERR(idata);
1011 goto cmd_done;
1012 }
1013
1014 card = md->queue.card;
1015 if (IS_ERR(card)) {
1016 err = PTR_ERR(card);
1017 goto idata_free;
1018 }
1019
1020 mmc_claim_host(card->host);
1021
1022 err = mmc_blk_part_switch(card, md);
1023 if (err)
1024 goto cmd_rel_host;
1025
1026 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1027 struct mmc_blk_ioc_data *curr_data;
1028 struct mmc_ioc_cmd *curr_cmd;
1029
1030 curr_data = idata->data[i];
1031 curr_cmd = &curr_data->ic;
1032 if (!curr_cmd->opcode)
1033 break;
1034
1035 cmd.opcode = curr_cmd->opcode;
1036 cmd.arg = curr_cmd->arg;
1037 cmd.flags = curr_cmd->flags;
1038
1039 if (curr_data->buf_bytes) {
1040 data.sg = &sg;
1041 data.sg_len = 1;
1042 data.blksz = curr_cmd->blksz;
1043 data.blocks = curr_cmd->blocks;
1044
1045 sg_init_one(data.sg, curr_data->buf,
1046 curr_data->buf_bytes);
1047
1048 if (curr_cmd->write_flag)
1049 data.flags = MMC_DATA_WRITE;
1050 else
1051 data.flags = MMC_DATA_READ;
1052
1053 /* data.flags must already be set before doing this. */
1054 mmc_set_data_timeout(&data, card);
1055
1056 /*
1057 * Allow overriding the timeout_ns for empirical tuning.
1058 */
1059 if (curr_cmd->data_timeout_ns)
1060 data.timeout_ns = curr_cmd->data_timeout_ns;
1061
1062 mrq.data = &data;
1063 }
1064
1065 mrq.cmd = &cmd;
1066
1067 err = mmc_set_blockcount(card, data.blocks,
1068 curr_cmd->write_flag & (1 << 31));
1069 if (err)
1070 goto cmd_rel_host;
1071
1072 mmc_wait_for_req(card->host, &mrq);
1073
1074 if (cmd.error) {
1075 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
1076 __func__, cmd.error);
1077 err = cmd.error;
1078 goto cmd_rel_host;
1079 }
1080 if (data.error) {
1081 dev_err(mmc_dev(card->host), "%s: data error %d\n",
1082 __func__, data.error);
1083 err = data.error;
1084 goto cmd_rel_host;
1085 }
1086
1087 if (copy_to_user(&(ic_ptr->cmds[i].response), cmd.resp,
1088 sizeof(cmd.resp))) {
1089 err = -EFAULT;
1090 goto cmd_rel_host;
1091 }
1092
1093 if (!curr_cmd->write_flag) {
1094 if (copy_to_user((void __user *)(unsigned long)
1095 curr_cmd->data_ptr,
1096 curr_data->buf,
1097 curr_data->buf_bytes)) {
1098 err = -EFAULT;
1099 goto cmd_rel_host;
1100 }
1101 }
1102
Loic Pallardy8d1e9772012-08-06 17:12:31 +02001103 /*
1104 * Ensure RPMB command has completed by polling CMD13
1105 * "Send Status".
1106 */
1107 err = ioctl_rpmb_card_status_poll(card, &status, 5);
1108 if (err)
1109 dev_err(mmc_dev(card->host),
1110 "%s: Card Status=0x%08X, error %d\n",
1111 __func__, status, err);
1112 }
1113
Krishna Kondae6711632014-12-04 15:20:57 +02001114cmd_rel_host:
1115 mmc_put_card(card);
1116
1117idata_free:
1118 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1119 kfree(idata->data[i]->buf);
1120 kfree(idata->data[i]);
1121 }
1122 kfree(idata);
1123
1124cmd_done:
1125 mmc_blk_put(md);
Jon Huntera5f57742015-09-22 10:27:53 +01001126 return err;
1127}
1128
1129static int mmc_blk_ioctl_cmd(struct block_device *bdev,
1130 struct mmc_ioc_cmd __user *ic_ptr)
1131{
1132 struct mmc_blk_ioc_data *idata;
1133 struct mmc_blk_data *md;
1134 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -07001135 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001136
Shawn Lin83c742c2016-03-16 18:15:47 +08001137 /*
1138 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1139 * whole block device, not on a partition. This prevents overspray
1140 * between sibling partitions.
1141 */
1142 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1143 return -EPERM;
1144
Jon Huntera5f57742015-09-22 10:27:53 +01001145 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
Asutosh Dasbbefab32013-10-07 14:53:32 +05301146 if (IS_ERR_OR_NULL(idata))
Jon Huntera5f57742015-09-22 10:27:53 +01001147 return PTR_ERR(idata);
1148
1149 md = mmc_blk_get(bdev->bd_disk);
1150 if (!md) {
1151 err = -EINVAL;
1152 goto cmd_err;
1153 }
1154
1155 card = md->queue.card;
Asutosh Dasbbefab32013-10-07 14:53:32 +05301156 if (IS_ERR_OR_NULL(card)) {
Jon Huntera5f57742015-09-22 10:27:53 +01001157 err = PTR_ERR(card);
1158 goto cmd_done;
1159 }
1160
1161 mmc_get_card(card);
1162
Grant Grundlerb0934102015-09-23 18:30:33 -07001163 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001164
Adrian Hunter3c866562016-05-04 14:38:12 +03001165 /* Always switch back to main area after RPMB access */
1166 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1167 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1168
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001169 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -04001170
Grant Grundlerb0934102015-09-23 18:30:33 -07001171 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001172
John Calixtocb87ea22011-04-26 18:56:29 -04001173cmd_done:
1174 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +03001175cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -04001176 kfree(idata->buf);
1177 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001178 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -04001179}
1180
Jon Huntera5f57742015-09-22 10:27:53 +01001181static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
1182 struct mmc_ioc_multi_cmd __user *user)
1183{
1184 struct mmc_blk_ioc_data **idata = NULL;
1185 struct mmc_ioc_cmd __user *cmds = user->cmds;
1186 struct mmc_card *card;
1187 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -07001188 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001189 __u64 num_of_cmds;
1190
Shawn Lin83c742c2016-03-16 18:15:47 +08001191 /*
1192 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1193 * whole block device, not on a partition. This prevents overspray
1194 * between sibling partitions.
1195 */
1196 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1197 return -EPERM;
1198
Jon Huntera5f57742015-09-22 10:27:53 +01001199 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
1200 sizeof(num_of_cmds)))
1201 return -EFAULT;
1202
1203 if (num_of_cmds > MMC_IOC_MAX_CMDS)
1204 return -EINVAL;
1205
1206 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
1207 if (!idata)
1208 return -ENOMEM;
1209
1210 for (i = 0; i < num_of_cmds; i++) {
1211 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
1212 if (IS_ERR(idata[i])) {
1213 err = PTR_ERR(idata[i]);
1214 num_of_cmds = i;
1215 goto cmd_err;
1216 }
1217 }
1218
1219 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -08001220 if (!md) {
1221 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +01001222 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -08001223 }
Jon Huntera5f57742015-09-22 10:27:53 +01001224
1225 card = md->queue.card;
1226 if (IS_ERR(card)) {
1227 err = PTR_ERR(card);
1228 goto cmd_done;
1229 }
1230
1231 mmc_get_card(card);
1232
Grant Grundlerb0934102015-09-23 18:30:33 -07001233 for (i = 0; i < num_of_cmds && !ioc_err; i++)
1234 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001235
Adrian Hunter3c866562016-05-04 14:38:12 +03001236 /* Always switch back to main area after RPMB access */
1237 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1238 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1239
Jon Huntera5f57742015-09-22 10:27:53 +01001240 mmc_put_card(card);
1241
1242 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -07001243 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +01001244 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001245
1246cmd_done:
1247 mmc_blk_put(md);
1248cmd_err:
1249 for (i = 0; i < num_of_cmds; i++) {
1250 kfree(idata[i]->buf);
1251 kfree(idata[i]);
1252 }
1253 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001254 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +01001255}
1256
John Calixtocb87ea22011-04-26 18:56:29 -04001257static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
1258 unsigned int cmd, unsigned long arg)
1259{
Jon Huntera5f57742015-09-22 10:27:53 +01001260 switch (cmd) {
1261 case MMC_IOC_CMD:
1262 return mmc_blk_ioctl_cmd(bdev,
1263 (struct mmc_ioc_cmd __user *)arg);
Krishna Kondae6711632014-12-04 15:20:57 +02001264 case MMC_IOC_RPMB_CMD:
1265 return mmc_blk_ioctl_rpmb_cmd(bdev,
1266 (struct mmc_ioc_rpmb __user *)arg);
Jon Huntera5f57742015-09-22 10:27:53 +01001267 case MMC_IOC_MULTI_CMD:
1268 return mmc_blk_ioctl_multi_cmd(bdev,
1269 (struct mmc_ioc_multi_cmd __user *)arg);
1270 default:
1271 return -EINVAL;
1272 }
John Calixtocb87ea22011-04-26 18:56:29 -04001273}
1274
1275#ifdef CONFIG_COMPAT
1276static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
1277 unsigned int cmd, unsigned long arg)
1278{
1279 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
1280}
1281#endif
1282
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001283static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -05001284 .open = mmc_blk_open,
1285 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001286 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -04001288 .ioctl = mmc_blk_ioctl,
1289#ifdef CONFIG_COMPAT
1290 .compat_ioctl = mmc_blk_compat_ioctl,
1291#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292};
1293
Andrei Warkentin371a6892011-04-11 18:10:25 -05001294static inline int mmc_blk_part_switch(struct mmc_card *card,
1295 struct mmc_blk_data *md)
1296{
1297 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001298 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001299
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001300 if ((main_md->part_curr == md->part_type) &&
1301 (card->part_curr == md->part_type))
Andrei Warkentin371a6892011-04-11 18:10:25 -05001302 return 0;
1303
1304 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001305 u8 part_config = card->ext_csd.part_config;
1306
Adrian Hunter57da0c02016-05-04 14:38:13 +03001307 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1308 mmc_retune_pause(card->host);
1309
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001310 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1311 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001312
1313 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001314 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001315 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001316 if (ret) {
1317 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1318 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001319 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001320 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001321
1322 card->ext_csd.part_config = part_config;
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001323 card->part_curr = md->part_type;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001324
1325 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1326 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001327 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001328
1329 main_md->part_curr = md->part_type;
1330 return 0;
1331}
1332
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001333static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1334{
1335 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001336 u32 result;
1337 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001338
Venkatraman Sad5fd972011-08-25 00:30:50 +05301339 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001340 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001341 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001342
1343 struct scatterlist sg;
1344
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001345 cmd.opcode = MMC_APP_CMD;
1346 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001347 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001348
1349 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001350 if (err)
1351 return (u32)-1;
1352 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001353 return (u32)-1;
1354
1355 memset(&cmd, 0, sizeof(struct mmc_command));
1356
1357 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1358 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001359 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001360
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001361 data.blksz = 4;
1362 data.blocks = 1;
1363 data.flags = MMC_DATA_READ;
1364 data.sg = &sg;
1365 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301366 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001367
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001368 mrq.cmd = &cmd;
1369 mrq.data = &data;
1370
Ben Dooks051913d2009-06-08 23:33:57 +01001371 blocks = kmalloc(4, GFP_KERNEL);
1372 if (!blocks)
1373 return (u32)-1;
1374
1375 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001376
1377 mmc_wait_for_req(card->host, &mrq);
1378
Ben Dooks051913d2009-06-08 23:33:57 +01001379 result = ntohl(*blocks);
1380 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001381
Ben Dooks051913d2009-06-08 23:33:57 +01001382 if (cmd.error || data.error)
1383 result = (u32)-1;
1384
1385 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001386}
1387
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001388static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001389{
Chris Ball1278dba2011-04-13 23:40:30 -04001390 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001391 int err;
1392
Adrian Hunter504f1912008-10-16 12:55:25 +03001393 cmd.opcode = MMC_SEND_STATUS;
1394 if (!mmc_host_is_spi(card->host))
1395 cmd.arg = card->rca << 16;
1396 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001397 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1398 if (err == 0)
1399 *status = cmd.resp[0];
1400 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001401}
1402
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001403static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001404 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001405{
1406 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1407 int err = 0;
1408 u32 status;
1409
1410 do {
1411 err = get_card_status(card, &status, 5);
1412 if (err) {
1413 pr_err("%s: error %d requesting status\n",
1414 req->rq_disk->disk_name, err);
1415 return err;
1416 }
1417
1418 if (status & R1_ERROR) {
1419 pr_err("%s: %s: error sending status cmd, status %#x\n",
1420 req->rq_disk->disk_name, __func__, status);
1421 *gen_err = 1;
1422 }
1423
Ulf Hansson95a91292014-01-29 13:11:27 +01001424 /* We may rely on the host hw to handle busy detection.*/
1425 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1426 hw_busy_detect)
1427 break;
1428
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001429 /*
1430 * Timeout if the device never becomes ready for data and never
1431 * leaves the program state.
1432 */
1433 if (time_after(jiffies, timeout)) {
1434 pr_err("%s: Card stuck in programming state! %s %s\n",
1435 mmc_hostname(card->host),
1436 req->rq_disk->disk_name, __func__);
1437 return -ETIMEDOUT;
1438 }
1439
1440 /*
1441 * Some cards mishandle the status bits,
1442 * so make sure to check both the busy
1443 * indication and the card state.
1444 */
1445 } while (!(status & R1_READY_FOR_DATA) ||
1446 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1447
1448 return err;
1449}
1450
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001451static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1452 struct request *req, int *gen_err, u32 *stop_status)
1453{
1454 struct mmc_host *host = card->host;
1455 struct mmc_command cmd = {0};
1456 int err;
1457 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1458
1459 /*
1460 * Normally we use R1B responses for WRITE, but in cases where the host
1461 * has specified a max_busy_timeout we need to validate it. A failure
1462 * means we need to prevent the host from doing hw busy detection, which
1463 * is done by converting to a R1 response instead.
1464 */
1465 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1466 use_r1b_resp = false;
1467
1468 cmd.opcode = MMC_STOP_TRANSMISSION;
1469 if (use_r1b_resp) {
1470 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1471 cmd.busy_timeout = timeout_ms;
1472 } else {
1473 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1474 }
1475
1476 err = mmc_wait_for_cmd(host, &cmd, 5);
1477 if (err)
1478 return err;
1479
1480 *stop_status = cmd.resp[0];
1481
1482 /* No need to check card status in case of READ. */
1483 if (rq_data_dir(req) == READ)
1484 return 0;
1485
1486 if (!mmc_host_is_spi(host) &&
1487 (*stop_status & R1_ERROR)) {
1488 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1489 req->rq_disk->disk_name, __func__, *stop_status);
1490 *gen_err = 1;
1491 }
1492
1493 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1494}
1495
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301496#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001497#define ERR_RETRY 2
1498#define ERR_ABORT 1
1499#define ERR_CONTINUE 0
1500
1501static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1502 bool status_valid, u32 status)
1503{
1504 switch (error) {
1505 case -EILSEQ:
1506 /* response crc error, retry the r/w cmd */
1507 pr_err("%s: %s sending %s command, card status %#x\n",
1508 req->rq_disk->disk_name, "response CRC error",
1509 name, status);
1510 return ERR_RETRY;
1511
1512 case -ETIMEDOUT:
1513 pr_err("%s: %s sending %s command, card status %#x\n",
1514 req->rq_disk->disk_name, "timed out", name, status);
1515
1516 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301517 if (!status_valid) {
1518 pr_err("%s: status not valid, retrying timeout\n",
1519 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001520 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301521 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001522
1523 /*
1524 * If it was a r/w cmd crc error, or illegal command
1525 * (eg, issued in wrong state) then retry - we should
1526 * have corrected the state problem above.
1527 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301528 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1529 pr_err("%s: command error, retrying timeout\n",
1530 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001531 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301532 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001533
1534 /* Otherwise abort the command */
1535 return ERR_ABORT;
1536
1537 default:
1538 /* We don't understand the error code the driver gave us */
1539 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1540 req->rq_disk->disk_name, error, status);
1541 return ERR_ABORT;
1542 }
1543}
1544
1545/*
1546 * Initial r/w and stop cmd error recovery.
1547 * We don't know whether the card received the r/w cmd or not, so try to
1548 * restore things back to a sane state. Essentially, we do this as follows:
1549 * - Obtain card status. If the first attempt to obtain card status fails,
1550 * the status word will reflect the failed status cmd, not the failed
1551 * r/w cmd. If we fail to obtain card status, it suggests we can no
1552 * longer communicate with the card.
1553 * - Check the card state. If the card received the cmd but there was a
1554 * transient problem with the response, it might still be in a data transfer
1555 * mode. Try to send it a stop command. If this fails, we can't recover.
1556 * - If the r/w cmd failed due to a response CRC error, it was probably
1557 * transient, so retry the cmd.
1558 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1559 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1560 * illegal cmd, retry.
1561 * Otherwise we don't understand what happened, so abort.
1562 */
1563static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001564 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001565{
1566 bool prev_cmd_status_valid = true;
1567 u32 status, stop_status = 0;
1568 int err, retry;
1569
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301570 if (mmc_card_removed(card))
1571 return ERR_NOMEDIUM;
1572
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001573 /*
1574 * Try to get card status which indicates both the card state
1575 * and why there was no response. If the first attempt fails,
1576 * we can't be sure the returned status is for the r/w command.
1577 */
1578 for (retry = 2; retry >= 0; retry--) {
1579 err = get_card_status(card, &status, 0);
1580 if (!err)
1581 break;
1582
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001583 /* Re-tune if needed */
1584 mmc_retune_recheck(card->host);
1585
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001586 prev_cmd_status_valid = false;
1587 pr_err("%s: error %d sending status command, %sing\n",
1588 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1589 }
1590
1591 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301592 if (err) {
1593 /* Check if the card is removed */
1594 if (mmc_detect_card_removed(card->host))
1595 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001596 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301597 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001598
Adrian Hunter67716322011-08-29 16:42:15 +03001599 /* Flag ECC errors */
1600 if ((status & R1_CARD_ECC_FAILED) ||
1601 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1602 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1603 *ecc_err = 1;
1604
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001605 /* Flag General errors */
1606 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1607 if ((status & R1_ERROR) ||
1608 (brq->stop.resp[0] & R1_ERROR)) {
1609 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1610 req->rq_disk->disk_name, __func__,
1611 brq->stop.resp[0], status);
1612 *gen_err = 1;
1613 }
1614
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001615 /*
1616 * Check the current card state. If it is in some data transfer
1617 * mode, tell it to stop (and hopefully transition back to TRAN.)
1618 */
1619 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1620 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001621 err = send_stop(card,
1622 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1623 req, gen_err, &stop_status);
1624 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001625 pr_err("%s: error %d sending stop command\n",
1626 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001627 /*
1628 * If the stop cmd also timed out, the card is probably
1629 * not present, so abort. Other errors are bad news too.
1630 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001631 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001632 }
1633
Adrian Hunter67716322011-08-29 16:42:15 +03001634 if (stop_status & R1_CARD_ECC_FAILED)
1635 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001636 }
1637
1638 /* Check for set block count errors */
1639 if (brq->sbc.error)
1640 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1641 prev_cmd_status_valid, status);
1642
1643 /* Check for r/w command errors */
1644 if (brq->cmd.error)
1645 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1646 prev_cmd_status_valid, status);
1647
Adrian Hunter67716322011-08-29 16:42:15 +03001648 /* Data errors */
1649 if (!brq->stop.error)
1650 return ERR_CONTINUE;
1651
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001652 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001653 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 +01001654 req->rq_disk->disk_name, brq->stop.error,
1655 brq->cmd.resp[0], status);
1656
1657 /*
1658 * Subsitute in our own stop status as this will give the error
1659 * state which happened during the execution of the r/w command.
1660 */
1661 if (stop_status) {
1662 brq->stop.resp[0] = stop_status;
1663 brq->stop.error = 0;
1664 }
1665 return ERR_CONTINUE;
1666}
1667
Adrian Hunter67716322011-08-29 16:42:15 +03001668static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1669 int type)
1670{
1671 int err;
1672
1673 if (md->reset_done & type)
1674 return -EEXIST;
1675
1676 md->reset_done |= type;
1677 err = mmc_hw_reset(host);
1678 /* Ensure we switch back to the correct partition */
1679 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001680 struct mmc_blk_data *main_md =
1681 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001682 int part_err;
1683
1684 main_md->part_curr = main_md->part_type;
1685 part_err = mmc_blk_part_switch(host->card, md);
1686 if (part_err) {
1687 /*
1688 * We have failed to get back into the correct
1689 * partition, so we need to abort the whole request.
1690 */
1691 return -ENODEV;
1692 }
1693 }
1694 return err;
1695}
1696
1697static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1698{
1699 md->reset_done &= ~type;
1700}
1701
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001702int mmc_access_rpmb(struct mmc_queue *mq)
1703{
1704 struct mmc_blk_data *md = mq->data;
1705 /*
1706 * If this is a RPMB partition access, return ture
1707 */
1708 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1709 return true;
1710
1711 return false;
1712}
1713
Adrian Hunterbd788c92010-08-11 14:17:47 -07001714static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1715{
1716 struct mmc_blk_data *md = mq->data;
1717 struct mmc_card *card = md->queue.card;
1718 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001719 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001720
Adrian Hunterbd788c92010-08-11 14:17:47 -07001721 if (!mmc_can_erase(card)) {
1722 err = -EOPNOTSUPP;
1723 goto out;
1724 }
1725
1726 from = blk_rq_pos(req);
1727 nr = blk_rq_sectors(req);
1728
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001729 if (mmc_can_discard(card))
1730 arg = MMC_DISCARD_ARG;
1731 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001732 arg = MMC_TRIM_ARG;
1733 else
1734 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001735retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001736 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1737 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1738 INAND_CMD38_ARG_EXT_CSD,
1739 arg == MMC_TRIM_ARG ?
1740 INAND_CMD38_ARG_TRIM :
1741 INAND_CMD38_ARG_ERASE,
1742 0);
1743 if (err)
1744 goto out;
1745 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001746 err = mmc_erase(card, from, nr, arg);
1747out:
Adrian Hunter67716322011-08-29 16:42:15 +03001748 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1749 goto retry;
1750 if (!err)
1751 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301752 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001753
Adrian Hunterbd788c92010-08-11 14:17:47 -07001754 return err ? 0 : 1;
1755}
1756
Adrian Hunter49804542010-08-11 14:17:50 -07001757static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1758 struct request *req)
1759{
1760 struct mmc_blk_data *md = mq->data;
1761 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001762 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001763 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001764
Maya Erez775a9362013-04-18 15:41:55 +03001765 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001766 err = -EOPNOTSUPP;
1767 goto out;
1768 }
1769
1770 from = blk_rq_pos(req);
1771 nr = blk_rq_sectors(req);
1772
Maya Erez775a9362013-04-18 15:41:55 +03001773 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1774 arg = MMC_SECURE_TRIM1_ARG;
1775 else
1776 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001777
Adrian Hunter67716322011-08-29 16:42:15 +03001778retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001779 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1780 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1781 INAND_CMD38_ARG_EXT_CSD,
1782 arg == MMC_SECURE_TRIM1_ARG ?
1783 INAND_CMD38_ARG_SECTRIM1 :
1784 INAND_CMD38_ARG_SECERASE,
1785 0);
1786 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001787 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001788 }
Adrian Hunter28302812012-04-05 14:45:48 +03001789
Adrian Hunter49804542010-08-11 14:17:50 -07001790 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001791 if (err == -EIO)
1792 goto out_retry;
1793 if (err)
1794 goto out;
1795
1796 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001797 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1798 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1799 INAND_CMD38_ARG_EXT_CSD,
1800 INAND_CMD38_ARG_SECTRIM2,
1801 0);
1802 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001803 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001804 }
Adrian Hunter28302812012-04-05 14:45:48 +03001805
Adrian Hunter49804542010-08-11 14:17:50 -07001806 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001807 if (err == -EIO)
1808 goto out_retry;
1809 if (err)
1810 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001811 }
Adrian Hunter28302812012-04-05 14:45:48 +03001812
Adrian Hunter28302812012-04-05 14:45:48 +03001813out_retry:
1814 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001815 goto retry;
1816 if (!err)
1817 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001818out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301819 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001820
Adrian Hunter49804542010-08-11 14:17:50 -07001821 return err ? 0 : 1;
1822}
1823
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001824static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1825{
1826 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001827 struct mmc_card *card = md->queue.card;
1828 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001829
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001830 ret = mmc_flush_cache(card);
1831 if (ret)
1832 ret = -EIO;
1833
Mark Salyzyn6904e432016-01-28 11:12:25 -08001834#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
1835 else if (atomic_read(&mq->cache_size)) {
1836 long used = mmc_blk_cache_used(mq, jiffies);
1837
1838 if (used) {
1839 int speed = atomic_read(&mq->max_write_speed);
1840
1841 if (speed_valid(speed)) {
1842 unsigned long msecs = jiffies_to_msecs(
1843 size_and_speed_to_jiffies(
1844 used, speed));
1845 if (msecs)
1846 msleep(msecs);
1847 }
1848 }
1849 }
1850#endif
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301851 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001852
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001853 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001854}
1855
1856/*
1857 * Reformat current write as a reliable write, supporting
1858 * both legacy and the enhanced reliable write MMC cards.
1859 * In each transfer we'll handle only as much as a single
1860 * reliable write can handle, thus finish the request in
1861 * partial completions.
1862 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001863static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1864 struct mmc_card *card,
1865 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001866{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001867 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1868 /* Legacy mode imposes restrictions on transfers. */
1869 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1870 brq->data.blocks = 1;
1871
1872 if (brq->data.blocks > card->ext_csd.rel_sectors)
1873 brq->data.blocks = card->ext_csd.rel_sectors;
1874 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1875 brq->data.blocks = 1;
1876 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001877}
1878
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001879#define CMD_ERRORS \
1880 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1881 R1_ADDRESS_ERROR | /* Misaligned address */ \
1882 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1883 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1884 R1_CC_ERROR | /* Card controller error */ \
1885 R1_ERROR) /* General/unknown error */
1886
Per Forlinee8a43a2011-07-01 18:55:33 +02001887static int mmc_blk_err_check(struct mmc_card *card,
1888 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001889{
Per Forlinee8a43a2011-07-01 18:55:33 +02001890 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1891 mmc_active);
1892 struct mmc_blk_request *brq = &mq_mrq->brq;
1893 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001894 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001895 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001896
1897 /*
1898 * sbc.error indicates a problem with the set block count
1899 * command. No data will have been transferred.
1900 *
1901 * cmd.error indicates a problem with the r/w command. No
1902 * data will have been transferred.
1903 *
1904 * stop.error indicates a problem with the stop command. Data
1905 * may have been transferred, or may still be transferring.
1906 */
Adrian Hunter67716322011-08-29 16:42:15 +03001907 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1908 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001909 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001910 case ERR_RETRY:
1911 return MMC_BLK_RETRY;
1912 case ERR_ABORT:
1913 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301914 case ERR_NOMEDIUM:
1915 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001916 case ERR_CONTINUE:
1917 break;
1918 }
1919 }
1920
1921 /*
1922 * Check for errors relating to the execution of the
1923 * initial command - such as address errors. No data
1924 * has been transferred.
1925 */
1926 if (brq->cmd.resp[0] & CMD_ERRORS) {
1927 pr_err("%s: r/w command failed, status = %#x\n",
1928 req->rq_disk->disk_name, brq->cmd.resp[0]);
1929 return MMC_BLK_ABORT;
1930 }
1931
1932 /*
1933 * Everything else is either success, or a data error of some
1934 * kind. If it was a write, we may have transitioned to
1935 * program mode, which we have to wait for it to complete.
1936 */
1937 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001938 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001939
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001940 /* Check stop command response */
1941 if (brq->stop.resp[0] & R1_ERROR) {
1942 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1943 req->rq_disk->disk_name, __func__,
1944 brq->stop.resp[0]);
1945 gen_err = 1;
1946 }
1947
Ulf Hansson95a91292014-01-29 13:11:27 +01001948 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1949 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001950 if (err)
1951 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001952 }
1953
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001954 /* if general error occurs, retry the write operation. */
1955 if (gen_err) {
1956 pr_warn("%s: retrying write for general error\n",
1957 req->rq_disk->disk_name);
1958 return MMC_BLK_RETRY;
1959 }
1960
Per Forlind78d4a82011-07-01 18:55:30 +02001961 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001962 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001963 pr_debug("%s: retrying because a re-tune was needed\n",
1964 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001965 brq->retune_retry_done = 1;
1966 return MMC_BLK_RETRY;
1967 }
Per Forlind78d4a82011-07-01 18:55:30 +02001968 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1969 req->rq_disk->disk_name, brq->data.error,
1970 (unsigned)blk_rq_pos(req),
1971 (unsigned)blk_rq_sectors(req),
1972 brq->cmd.resp[0], brq->stop.resp[0]);
1973
1974 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001975 if (ecc_err)
1976 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001977 return MMC_BLK_DATA_ERR;
1978 } else {
1979 return MMC_BLK_CMD_ERR;
1980 }
1981 }
1982
Adrian Hunter67716322011-08-29 16:42:15 +03001983 if (!brq->data.bytes_xfered)
1984 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001985
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001986 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1987 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1988 return MMC_BLK_PARTIAL;
1989 else
1990 return MMC_BLK_SUCCESS;
1991 }
1992
Adrian Hunter67716322011-08-29 16:42:15 +03001993 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1994 return MMC_BLK_PARTIAL;
1995
1996 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001997}
1998
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001999static int mmc_blk_packed_err_check(struct mmc_card *card,
2000 struct mmc_async_req *areq)
2001{
2002 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
2003 mmc_active);
2004 struct request *req = mq_rq->req;
2005 struct mmc_packed *packed = mq_rq->packed;
2006 int err, check, status;
2007 u8 *ext_csd;
2008
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002009 packed->retries--;
2010 check = mmc_blk_err_check(card, areq);
2011 err = get_card_status(card, &status, 0);
2012 if (err) {
2013 pr_err("%s: error %d sending status command\n",
2014 req->rq_disk->disk_name, err);
2015 return MMC_BLK_ABORT;
2016 }
2017
2018 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02002019 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002020 if (err) {
2021 pr_err("%s: error %d sending ext_csd\n",
2022 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02002023 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002024 }
2025
2026 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
2027 EXT_CSD_PACKED_FAILURE) &&
2028 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2029 EXT_CSD_PACKED_GENERIC_ERROR)) {
2030 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2031 EXT_CSD_PACKED_INDEXED_ERROR) {
2032 packed->idx_failure =
2033 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
2034 check = MMC_BLK_PARTIAL;
2035 }
2036 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
2037 "failure index: %d\n",
2038 req->rq_disk->disk_name, packed->nr_entries,
2039 packed->blocks, packed->idx_failure);
2040 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002041 kfree(ext_csd);
2042 }
2043
2044 return check;
2045}
2046
Per Forlin54d49d72011-07-01 18:55:29 +02002047static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
2048 struct mmc_card *card,
2049 int disable_multi,
2050 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
Per Forlin54d49d72011-07-01 18:55:29 +02002052 u32 readcmd, writecmd;
2053 struct mmc_blk_request *brq = &mqrq->brq;
2054 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05302056 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002058 /*
2059 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00002060 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002061 */
Luca Porziod3df0462015-11-06 15:12:26 +00002062 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002063 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002064 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002065
Per Forlin54d49d72011-07-01 18:55:29 +02002066 memset(brq, 0, sizeof(struct mmc_blk_request));
2067 brq->mrq.cmd = &brq->cmd;
2068 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Per Forlin54d49d72011-07-01 18:55:29 +02002070 brq->cmd.arg = blk_rq_pos(req);
2071 if (!mmc_card_blockaddr(card))
2072 brq->cmd.arg <<= 9;
2073 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2074 brq->data.blksz = 512;
2075 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2076 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002077 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Asutosh Dasf0665412012-07-27 18:10:19 +05302079 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02002080 /*
2081 * The block layer doesn't support all sector count
2082 * restrictions, so we need to be prepared for too big
2083 * requests.
2084 */
2085 if (brq->data.blocks > card->host->max_blk_count)
2086 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002088 if (brq->data.blocks > 1) {
2089 /*
2090 * After a read error, we redo the request one sector
2091 * at a time in order to accurately determine which
2092 * sectors can be read successfully.
2093 */
2094 if (disable_multi)
2095 brq->data.blocks = 1;
2096
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07002097 /*
2098 * Some controllers have HW issues while operating
2099 * in multiple I/O mode
2100 */
2101 if (card->host->ops->multi_io_quirk)
2102 brq->data.blocks = card->host->ops->multi_io_quirk(card,
2103 (rq_data_dir(req) == READ) ?
2104 MMC_DATA_READ : MMC_DATA_WRITE,
2105 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002106 }
Per Forlin54d49d72011-07-01 18:55:29 +02002107
2108 if (brq->data.blocks > 1 || do_rel_wr) {
2109 /* SPI multiblock writes terminate using a special
2110 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02002111 */
Per Forlin54d49d72011-07-01 18:55:29 +02002112 if (!mmc_host_is_spi(card->host) ||
2113 rq_data_dir(req) == READ)
2114 brq->mrq.stop = &brq->stop;
2115 readcmd = MMC_READ_MULTIPLE_BLOCK;
2116 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
2117 } else {
2118 brq->mrq.stop = NULL;
2119 readcmd = MMC_READ_SINGLE_BLOCK;
2120 writecmd = MMC_WRITE_BLOCK;
2121 }
2122 if (rq_data_dir(req) == READ) {
2123 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002124 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002125 if (brq->mrq.stop)
2126 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
2127 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002128 } else {
2129 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002130 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002131 if (brq->mrq.stop)
2132 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
2133 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002134 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02002135
Per Forlin54d49d72011-07-01 18:55:29 +02002136 if (do_rel_wr)
2137 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01002138
Per Forlin54d49d72011-07-01 18:55:29 +02002139 /*
Saugata Das42659002011-12-21 13:09:17 +05302140 * Data tag is used only during writing meta data to speed
2141 * up write and any subsequent read of this meta data
2142 */
2143 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2144 (req->cmd_flags & REQ_META) &&
2145 (rq_data_dir(req) == WRITE) &&
2146 ((brq->data.blocks * brq->data.blksz) >=
2147 card->ext_csd.data_tag_unit_size);
2148
2149 /*
Per Forlin54d49d72011-07-01 18:55:29 +02002150 * Pre-defined multi-block transfers are preferable to
2151 * open ended-ones (and necessary for reliable writes).
2152 * However, it is not sufficient to just send CMD23,
2153 * and avoid the final CMD12, as on an error condition
2154 * CMD12 (stop) needs to be sent anyway. This, coupled
2155 * with Auto-CMD23 enhancements provided by some
2156 * hosts, means that the complexity of dealing
2157 * with this is best left to the host. If CMD23 is
2158 * supported by card and host, we'll fill sbc in and let
2159 * the host deal with handling it correctly. This means
2160 * that for hosts that don't expose MMC_CAP_CMD23, no
2161 * change of behavior will be observed.
2162 *
2163 * N.B: Some MMC cards experience perf degradation.
2164 * We'll avoid using CMD23-bounded multiblock writes for
2165 * these, while retaining features like reliable writes.
2166 */
Saugata Das42659002011-12-21 13:09:17 +05302167 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
2168 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
2169 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02002170 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2171 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05302172 (do_rel_wr ? (1 << 31) : 0) |
2173 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02002174 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2175 brq->mrq.sbc = &brq->sbc;
2176 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002177
Per Forlin54d49d72011-07-01 18:55:29 +02002178 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002179
Per Forlin54d49d72011-07-01 18:55:29 +02002180 brq->data.sg = mqrq->sg;
2181 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002182
Per Forlin54d49d72011-07-01 18:55:29 +02002183 /*
2184 * Adjust the sg list so it is the same size as the
2185 * request.
2186 */
2187 if (brq->data.blocks != blk_rq_sectors(req)) {
2188 int i, data_size = brq->data.blocks << 9;
2189 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02002190
Per Forlin54d49d72011-07-01 18:55:29 +02002191 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
2192 data_size -= sg->length;
2193 if (data_size <= 0) {
2194 sg->length += data_size;
2195 i++;
2196 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01002197 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002198 }
Per Forlin54d49d72011-07-01 18:55:29 +02002199 brq->data.sg_len = i;
2200 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002201
Per Forlinee8a43a2011-07-01 18:55:33 +02002202 mqrq->mmc_active.mrq = &brq->mrq;
2203 mqrq->mmc_active.err_check = mmc_blk_err_check;
2204
Per Forlin54d49d72011-07-01 18:55:29 +02002205 mmc_queue_bounce_pre(mqrq);
2206}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002208static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
2209 struct mmc_card *card)
2210{
2211 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
2212 unsigned int max_seg_sz = queue_max_segment_size(q);
2213 unsigned int len, nr_segs = 0;
2214
2215 do {
2216 len = min(hdr_sz, max_seg_sz);
2217 hdr_sz -= len;
2218 nr_segs++;
2219 } while (hdr_sz);
2220
2221 return nr_segs;
2222}
2223
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002224/**
2225 * mmc_blk_disable_wr_packing() - disables packing mode
2226 * @mq: MMC queue.
2227 *
2228 */
2229void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
2230{
2231 if (mq) {
2232 mq->wr_packing_enabled = false;
2233 mq->num_of_potential_packed_wr_reqs = 0;
2234 }
2235}
2236EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
2237
Lee Susman841fd132013-04-23 17:59:26 +03002238static int get_packed_trigger(int potential, struct mmc_card *card,
2239 struct request *req, int curr_trigger)
2240{
2241 static int num_mean_elements = 1;
2242 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2243 unsigned int trigger = curr_trigger;
2244 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
2245
2246 /* scale down the upper bound to 75% */
2247 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
2248
2249 /*
2250 * since the most common calls for this function are with small
2251 * potential write values and since we don't want these calls to affect
2252 * the packed trigger, set a lower bound and ignore calls with
2253 * potential lower than that bound
2254 */
2255 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
2256 return trigger;
2257
2258 /*
2259 * this is to prevent integer overflow in the following calculation:
2260 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
2261 */
2262 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
2263 num_mean_elements = 1;
2264 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2265 }
2266
2267 /*
2268 * get next mean value based on previous mean value and current
2269 * potential packed writes. Calculation is as follows:
2270 * mean_pot[i+1] =
2271 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
2272 */
2273 mean_potential *= num_mean_elements;
2274 /*
2275 * add num_mean_elements so that the division of two integers doesn't
2276 * lower mean_potential too much
2277 */
2278 if (potential > mean_potential)
2279 mean_potential += num_mean_elements;
2280 mean_potential += potential;
2281 /* this is for gaining more precision when dividing two integers */
2282 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
2283 /* this completes the mean calculation */
2284 mean_potential /= ++num_mean_elements;
2285 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
2286
2287 /*
2288 * if current potential packed writes is greater than the mean potential
2289 * then the heuristic is that the following workload will contain many
2290 * write requests, therefore we lower the packed trigger. In the
2291 * opposite case we want to increase the trigger in order to get less
2292 * packing events.
2293 */
2294 if (potential >= mean_potential)
2295 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
2296 PCKD_TRGR_LOWER_BOUND : trigger - 1;
2297 else
2298 trigger = (trigger >= pckd_trgr_upper_bound) ?
2299 pckd_trgr_upper_bound : trigger + 1;
2300
2301 /*
2302 * an urgent read request indicates a packed list being interrupted
2303 * by this read, therefore we aim for less packing, hence the trigger
2304 * gets increased
2305 */
2306 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
2307 trigger += PCKD_TRGR_URGENT_PENALTY;
2308
2309 return trigger;
2310}
2311
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002312static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2313 struct request *req)
2314{
2315 struct mmc_host *host = mq->card->host;
2316 int data_dir;
2317
2318 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2319 return;
2320
Maya Erez8e2b3c32012-12-02 13:27:15 +02002321 /* Support for the write packing on eMMC 4.5 or later */
2322 if (mq->card->ext_csd.rev <= 5)
2323 return;
2324
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002325 /*
2326 * In case the packing control is not supported by the host, it should
2327 * not have an effect on the write packing. Therefore we have to enable
2328 * the write packing
2329 */
2330 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2331 mq->wr_packing_enabled = true;
2332 return;
2333 }
2334
2335 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2336 if (mq->num_of_potential_packed_wr_reqs >
2337 mq->num_wr_reqs_to_start_packing)
2338 mq->wr_packing_enabled = true;
Lee Susman841fd132013-04-23 17:59:26 +03002339 mq->num_wr_reqs_to_start_packing =
2340 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2341 mq->card, req,
2342 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002343 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002344 return;
2345 }
2346
2347 data_dir = rq_data_dir(req);
2348
2349 if (data_dir == READ) {
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002350 mmc_blk_disable_wr_packing(mq);
Lee Susman841fd132013-04-23 17:59:26 +03002351 mq->num_wr_reqs_to_start_packing =
2352 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2353 mq->card, req,
2354 mq->num_wr_reqs_to_start_packing);
2355 mq->num_of_potential_packed_wr_reqs = 0;
2356 mq->wr_packing_enabled = false;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002357 return;
2358 } else if (data_dir == WRITE) {
2359 mq->num_of_potential_packed_wr_reqs++;
2360 }
2361
2362 if (mq->num_of_potential_packed_wr_reqs >
2363 mq->num_wr_reqs_to_start_packing)
2364 mq->wr_packing_enabled = true;
2365}
2366
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002367struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2368{
2369 if (!card)
2370 return NULL;
2371
2372 return &card->wr_pack_stats;
2373}
2374EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2375
2376void mmc_blk_init_packed_statistics(struct mmc_card *card)
2377{
2378 int max_num_of_packed_reqs = 0;
2379
2380 if (!card || !card->wr_pack_stats.packing_events)
2381 return;
2382
2383 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2384
2385 spin_lock(&card->wr_pack_stats.lock);
2386 memset(card->wr_pack_stats.packing_events, 0,
2387 (max_num_of_packed_reqs + 1) *
2388 sizeof(*card->wr_pack_stats.packing_events));
2389 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2390 sizeof(card->wr_pack_stats.pack_stop_reason));
2391 card->wr_pack_stats.enabled = true;
2392 spin_unlock(&card->wr_pack_stats.lock);
2393}
2394EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2395
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002396static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2397{
2398 struct request_queue *q = mq->queue;
2399 struct mmc_card *card = mq->card;
2400 struct request *cur = req, *next = NULL;
2401 struct mmc_blk_data *md = mq->data;
2402 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2403 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2404 unsigned int req_sectors = 0, phys_segments = 0;
2405 unsigned int max_blk_count, max_phys_segs;
2406 bool put_back = true;
2407 u8 max_packed_rw = 0;
2408 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002409 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002410
Shawn Lin96e52da2016-08-26 08:49:55 +08002411 /*
2412 * We don't need to check packed for any further
2413 * operation of packed stuff as we set MMC_PACKED_NONE
2414 * and return zero for reqs if geting null packed. Also
2415 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2416 * it again when removing blk req.
2417 */
2418 if (!mqrq->packed) {
2419 md->flags &= (~MMC_BLK_PACKED_CMD);
2420 goto no_packed;
2421 }
2422
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002423 if (!(md->flags & MMC_BLK_PACKED_CMD))
2424 goto no_packed;
2425
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002426 if (!mq->wr_packing_enabled)
2427 goto no_packed;
2428
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002429 if ((rq_data_dir(cur) == WRITE) &&
2430 mmc_host_packed_wr(card->host))
2431 max_packed_rw = card->ext_csd.max_packed_writes;
2432
2433 if (max_packed_rw == 0)
2434 goto no_packed;
2435
2436 if (mmc_req_rel_wr(cur) &&
2437 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2438 goto no_packed;
2439
2440 if (mmc_large_sector(card) &&
2441 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2442 goto no_packed;
2443
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002444 if (cur->cmd_flags & REQ_FUA)
2445 goto no_packed;
2446
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002447 mmc_blk_clear_packed(mqrq);
2448
2449 max_blk_count = min(card->host->max_blk_count,
2450 card->host->max_req_size >> 9);
2451 if (unlikely(max_blk_count > 0xffff))
2452 max_blk_count = 0xffff;
2453
2454 max_phys_segs = queue_max_segments(q);
2455 req_sectors += blk_rq_sectors(cur);
2456 phys_segments += cur->nr_phys_segments;
2457
2458 if (rq_data_dir(cur) == WRITE) {
2459 req_sectors += mmc_large_sector(card) ? 8 : 1;
2460 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2461 }
2462
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002463 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002464 do {
2465 if (reqs >= max_packed_rw - 1) {
2466 put_back = false;
2467 break;
2468 }
2469
2470 spin_lock_irq(q->queue_lock);
2471 next = blk_fetch_request(q);
2472 spin_unlock_irq(q->queue_lock);
2473 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002474 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002475 put_back = false;
2476 break;
2477 }
2478
2479 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002480 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2481 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002482 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002483 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002484
Mike Christie3a5e02c2016-06-05 14:32:23 -05002485 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002486 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002487 req_op(next) == REQ_OP_FLUSH) {
2488 if (req_op(next) != REQ_OP_SECURE_ERASE)
2489 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002490 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002491 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002492
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002493 if (next->cmd_flags & REQ_FUA) {
2494 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2495 break;
2496 }
2497
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002498 if (rq_data_dir(cur) != rq_data_dir(next)) {
2499 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002500 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002501 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002502
2503 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002504 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2505 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002506 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002507 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002508
2509 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002510 if (req_sectors > max_blk_count) {
2511 if (stats->enabled)
2512 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002513 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002514 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002515
2516 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002517 if (phys_segments > max_phys_segs) {
2518 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002519 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002520 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002521
Maya Erez5a8dae12014-12-04 15:13:59 +02002522 if (mq->no_pack_for_random) {
2523 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2524 blk_rq_pos(next)) {
2525 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2526 put_back = 1;
2527 break;
2528 }
2529 }
2530
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002531 if (rq_data_dir(next) == WRITE)
2532 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002533 list_add_tail(&next->queuelist, &mqrq->packed->list);
2534 cur = next;
2535 reqs++;
2536 } while (1);
2537
2538 if (put_back) {
2539 spin_lock_irq(q->queue_lock);
2540 blk_requeue_request(q, next);
2541 spin_unlock_irq(q->queue_lock);
2542 }
2543
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002544 if (stats->enabled) {
2545 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2546 stats->packing_events[reqs + 1]++;
2547 if (reqs + 1 == max_packed_rw)
2548 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2549 }
2550
2551 spin_unlock(&stats->lock);
2552
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002553 if (reqs > 0) {
2554 list_add(&req->queuelist, &mqrq->packed->list);
2555 mqrq->packed->nr_entries = ++reqs;
2556 mqrq->packed->retries = reqs;
2557 return reqs;
2558 }
2559
2560no_packed:
2561 mqrq->cmd_type = MMC_PACKED_NONE;
2562 return 0;
2563}
2564
2565static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2566 struct mmc_card *card,
2567 struct mmc_queue *mq)
2568{
2569 struct mmc_blk_request *brq = &mqrq->brq;
2570 struct request *req = mqrq->req;
2571 struct request *prq;
2572 struct mmc_blk_data *md = mq->data;
2573 struct mmc_packed *packed = mqrq->packed;
2574 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002575 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002576 u8 hdr_blocks;
2577 u8 i = 1;
2578
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002579 mqrq->cmd_type = MMC_PACKED_WRITE;
2580 packed->blocks = 0;
2581 packed->idx_failure = MMC_PACKED_NR_IDX;
2582
2583 packed_cmd_hdr = packed->cmd_hdr;
2584 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002585 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2586 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002587 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2588
2589 /*
2590 * Argument for each entry of packed group
2591 */
2592 list_for_each_entry(prq, &packed->list, queuelist) {
2593 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2594 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2595 (prq->cmd_flags & REQ_META) &&
2596 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002597 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002598 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002599 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002600 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2601 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002602 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002603 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002604 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002605 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002606 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002607 packed->blocks += blk_rq_sectors(prq);
2608 i++;
2609 }
2610
2611 memset(brq, 0, sizeof(struct mmc_blk_request));
2612 brq->mrq.cmd = &brq->cmd;
2613 brq->mrq.data = &brq->data;
2614 brq->mrq.sbc = &brq->sbc;
2615 brq->mrq.stop = &brq->stop;
2616
2617 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2618 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2619 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2620
2621 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2622 brq->cmd.arg = blk_rq_pos(req);
2623 if (!mmc_card_blockaddr(card))
2624 brq->cmd.arg <<= 9;
2625 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2626
2627 brq->data.blksz = 512;
2628 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002629 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302630 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002631
2632 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2633 brq->stop.arg = 0;
2634 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2635
2636 mmc_set_data_timeout(&brq->data, card);
2637
2638 brq->data.sg = mqrq->sg;
2639 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2640
2641 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002642
2643 /*
2644 * This is intended for packed commands tests usage - in case these
2645 * functions are not in use the respective pointers are NULL
2646 */
2647 if (mq->err_check_fn)
2648 mqrq->mmc_active.err_check = mq->err_check_fn;
2649 else
2650 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2651
2652 if (mq->packed_test_fn)
2653 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002654
2655 mmc_queue_bounce_pre(mqrq);
2656}
2657
Adrian Hunter67716322011-08-29 16:42:15 +03002658static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2659 struct mmc_blk_request *brq, struct request *req,
2660 int ret)
2661{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002662 struct mmc_queue_req *mq_rq;
2663 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2664
Adrian Hunter67716322011-08-29 16:42:15 +03002665 /*
2666 * If this is an SD card and we're writing, we can first
2667 * mark the known good sectors as ok.
2668 *
2669 * If the card is not SD, we can still ok written sectors
2670 * as reported by the controller (which might be less than
2671 * the real number of written sectors, but never more).
2672 */
2673 if (mmc_card_sd(card)) {
2674 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302675 if (!brq->data.fault_injected) {
2676 blocks = mmc_sd_num_wr_blocks(card);
2677 if (blocks != (u32)-1)
2678 ret = blk_end_request(req, 0, blocks << 9);
2679 } else
2680 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002681 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002682 if (!mmc_packed_cmd(mq_rq->cmd_type))
2683 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002684 }
2685 return ret;
2686}
2687
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002688static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2689{
2690 struct request *prq;
2691 struct mmc_packed *packed = mq_rq->packed;
2692 int idx = packed->idx_failure, i = 0;
2693 int ret = 0;
2694
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002695 while (!list_empty(&packed->list)) {
2696 prq = list_entry_rq(packed->list.next);
2697 if (idx == i) {
2698 /* retry from error index */
2699 packed->nr_entries -= idx;
2700 mq_rq->req = prq;
2701 ret = 1;
2702
2703 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2704 list_del_init(&prq->queuelist);
2705 mmc_blk_clear_packed(mq_rq);
2706 }
2707 return ret;
2708 }
2709 list_del_init(&prq->queuelist);
2710 blk_end_request(prq, 0, blk_rq_bytes(prq));
2711 i++;
2712 }
2713
2714 mmc_blk_clear_packed(mq_rq);
2715 return ret;
2716}
2717
2718static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2719{
2720 struct request *prq;
2721 struct mmc_packed *packed = mq_rq->packed;
2722
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002723 while (!list_empty(&packed->list)) {
2724 prq = list_entry_rq(packed->list.next);
2725 list_del_init(&prq->queuelist);
2726 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2727 }
2728
2729 mmc_blk_clear_packed(mq_rq);
2730}
2731
2732static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2733 struct mmc_queue_req *mq_rq)
2734{
2735 struct request *prq;
2736 struct request_queue *q = mq->queue;
2737 struct mmc_packed *packed = mq_rq->packed;
2738
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002739 while (!list_empty(&packed->list)) {
2740 prq = list_entry_rq(packed->list.prev);
2741 if (prq->queuelist.prev != &packed->list) {
2742 list_del_init(&prq->queuelist);
2743 spin_lock_irq(q->queue_lock);
2744 blk_requeue_request(mq->queue, prq);
2745 spin_unlock_irq(q->queue_lock);
2746 } else {
2747 list_del_init(&prq->queuelist);
2748 }
2749 }
2750
2751 mmc_blk_clear_packed(mq_rq);
2752}
2753
Per Forlinee8a43a2011-07-01 18:55:33 +02002754static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02002755{
2756 struct mmc_blk_data *md = mq->data;
2757 struct mmc_card *card = md->queue.card;
2758 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002759 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002760 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002761 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302762 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02002763 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002764 const u8 packed_nr = 2;
2765 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002766#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2767 unsigned long waitfor = jiffies;
2768#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02002769
2770 if (!rqc && !mq->mqrq_prev->req)
2771 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002772
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002773 if (rqc)
2774 reqs = mmc_blk_prep_packed_list(mq, rqc);
2775
Per Forlin54d49d72011-07-01 18:55:29 +02002776 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002777 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302778 /*
2779 * When 4KB native sector is enabled, only 8 blocks
2780 * multiple read or write is allowed
2781 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00002782 if (mmc_large_sector(card) &&
2783 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302784 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
2785 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002786 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302787 goto cmd_abort;
2788 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002789
2790 if (reqs >= packed_nr)
2791 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2792 card, mq);
2793 else
2794 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002795 areq = &mq->mqrq_cur->mmc_active;
2796 } else
2797 areq = NULL;
2798 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002799 if (!areq) {
2800 if (status == MMC_BLK_NEW_REQUEST)
2801 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02002802 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002803 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002804
Per Forlinee8a43a2011-07-01 18:55:33 +02002805 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2806 brq = &mq_rq->brq;
2807 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002808 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002809 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002810
Per Forlind78d4a82011-07-01 18:55:30 +02002811 switch (status) {
2812 case MMC_BLK_SUCCESS:
2813 case MMC_BLK_PARTIAL:
2814 /*
2815 * A block was successfully transferred.
2816 */
Adrian Hunter67716322011-08-29 16:42:15 +03002817 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002818
Mark Salyzyn6904e432016-01-28 11:12:25 -08002819 mmc_blk_simulate_delay(mq, rqc, waitfor);
2820
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002821 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2822 ret = mmc_blk_end_packed_req(mq_rq);
2823 break;
2824 } else {
2825 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002826 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002827 }
2828
Adrian Hunter67716322011-08-29 16:42:15 +03002829 /*
2830 * If the blk_end_request function returns non-zero even
2831 * though all data has been transferred and no errors
2832 * were returned by the host controller, it's a bug.
2833 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002834 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302835 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002836 __func__, blk_rq_bytes(req),
2837 brq->data.bytes_xfered);
2838 rqc = NULL;
2839 goto cmd_abort;
2840 }
Per Forlind78d4a82011-07-01 18:55:30 +02002841 break;
2842 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002843 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002844 if (mmc_blk_reset(md, card->host, type))
2845 goto cmd_abort;
2846 if (!ret)
2847 goto start_new_req;
2848 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002849 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002850 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a82011-07-01 18:55:30 +02002851 if (retry++ < 5)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002852 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002853 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002854 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002855 if (!mmc_blk_reset(md, card->host, type))
2856 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002857 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002858 case MMC_BLK_DATA_ERR: {
2859 int err;
2860
2861 err = mmc_blk_reset(md, card->host, type);
2862 if (!err)
2863 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05302864 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002865 }
2866 case MMC_BLK_ECC_ERR:
2867 if (brq->data.blocks > 1) {
2868 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002869 pr_warn("%s: retrying using single block read\n",
2870 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002871 disable_multi = 1;
2872 break;
2873 }
Per Forlind78d4a82011-07-01 18:55:30 +02002874 /*
2875 * After an error, we redo I/O one sector at a
2876 * time, so we only reach here after trying to
2877 * read a single sector.
2878 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302879 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002880 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002881 if (!ret)
2882 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002883 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302884 case MMC_BLK_NOMEDIUM:
2885 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002886 default:
2887 pr_err("%s: Unhandled return value (%d)",
2888 req->rq_disk->disk_name, status);
2889 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002890 }
2891
Per Forlinee8a43a2011-07-01 18:55:33 +02002892 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002893 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2894 if (!mq_rq->packed->retries)
2895 goto cmd_abort;
2896 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2897 mmc_start_req(card->host,
2898 &mq_rq->mmc_active, NULL);
2899 } else {
2900
2901 /*
2902 * In case of a incomplete request
2903 * prepare it again and resend.
2904 */
2905 mmc_blk_rw_rq_prep(mq_rq, card,
2906 disable_multi, mq);
2907 mmc_start_req(card->host,
2908 &mq_rq->mmc_active, NULL);
2909 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002910 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 } while (ret);
2913
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 return 1;
2915
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002916 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002917 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2918 mmc_blk_abort_packed_req(mq_rq);
2919 } else {
2920 if (mmc_card_removed(card))
2921 req->cmd_flags |= REQ_QUIET;
2922 while (ret)
2923 ret = blk_end_request(req, -EIO,
2924 blk_rq_cur_bytes(req));
2925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Per Forlinee8a43a2011-07-01 18:55:33 +02002927 start_new_req:
2928 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002929 if (mmc_card_removed(card)) {
2930 rqc->cmd_flags |= REQ_QUIET;
2931 blk_end_request_all(rqc, -EIO);
2932 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002933 /*
2934 * If current request is packed, it needs to put back.
2935 */
2936 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2937 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2938
Seungwon Jeon7a819022013-01-22 19:48:07 +09002939 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2940 mmc_start_req(card->host,
2941 &mq->mqrq_cur->mmc_active, NULL);
2942 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002943 }
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return 0;
2946}
2947
Linus Walleij29eb7bd2016-09-20 11:34:38 +02002948int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002949{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002950 int ret;
2951 struct mmc_blk_data *md = mq->data;
2952 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002953 struct mmc_host *host = card->host;
2954 unsigned long flags;
Adrian Hunter869c5542016-08-25 14:11:43 -06002955 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002956
Per Forlinee8a43a2011-07-01 18:55:33 +02002957 if (req && !mq->mqrq_prev->req)
2958 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002959 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002960
Andrei Warkentin371a6892011-04-11 18:10:25 -05002961 ret = mmc_blk_part_switch(card, md);
2962 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002963 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302964 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002965 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002966 ret = 0;
2967 goto out;
2968 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002969
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002970 mmc_blk_write_packing_control(mq, req);
2971
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002972 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Mike Christiec2df40d2016-06-05 14:32:17 -05002973 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002974 /* complete ongoing async transfer before issuing discard */
2975 if (card->host->areq)
2976 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02002977 ret = mmc_blk_issue_discard_rq(mq, req);
2978 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
2979 /* complete ongoing async transfer before issuing secure erase*/
2980 if (card->host->areq)
2981 mmc_blk_issue_rw_rq(mq, NULL);
2982 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05002983 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002984 /* complete ongoing async transfer before issuing flush */
2985 if (card->host->areq)
2986 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002987 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002988 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002989 if (!req && host->areq) {
2990 spin_lock_irqsave(&host->context_info.lock, flags);
2991 host->context_info.is_waiting_last_req = true;
2992 spin_unlock_irqrestore(&host->context_info.lock, flags);
2993 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002994 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002995 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002996
Andrei Warkentin371a6892011-04-11 18:10:25 -05002997out:
Adrian Hunter869c5542016-08-25 14:11:43 -06002998 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002999 /*
3000 * Release host when there are no more requests
3001 * and after special request(discard, flush) is done.
3002 * In case sepecial request, there is no reentry to
3003 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
3004 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003005 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003006 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07003007}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Russell Kinga6f6c962006-01-03 22:38:44 +00003009static inline int mmc_blk_readonly(struct mmc_card *card)
3010{
3011 return mmc_card_readonly(card) ||
3012 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
3013}
3014
Andrei Warkentin371a6892011-04-11 18:10:25 -05003015static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
3016 struct device *parent,
3017 sector_t size,
3018 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003019 const char *subname,
3020 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
3022 struct mmc_blk_data *md;
3023 int devidx, ret;
3024
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003025again:
3026 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
3027 return ERR_PTR(-ENOMEM);
3028
3029 spin_lock(&mmc_blk_lock);
3030 ret = ida_get_new(&mmc_blk_ida, &devidx);
3031 spin_unlock(&mmc_blk_lock);
3032
3033 if (ret == -EAGAIN)
3034 goto again;
3035 else if (ret)
3036 return ERR_PTR(ret);
3037
3038 if (devidx >= max_devices) {
3039 ret = -ENOSPC;
3040 goto out;
3041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003043 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00003044 if (!md) {
3045 ret = -ENOMEM;
3046 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 }
Russell Kinga6f6c962006-01-03 22:38:44 +00003048
Johan Rudholmadd710e2011-12-02 08:51:06 +01003049 md->area_type = area_type;
3050
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003051 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00003052 * Set the read-only status based on the supported commands
3053 * and the write protect switch.
3054 */
3055 md->read_only = mmc_blk_readonly(card);
3056
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003057 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00003058 if (md->disk == NULL) {
3059 ret = -ENOMEM;
3060 goto err_kfree;
3061 }
3062
3063 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003064 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00003065 md->usage = 1;
3066
Adrian Hunterd09408a2011-06-23 13:40:28 +03003067 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00003068 if (ret)
3069 goto err_putdisk;
3070
Russell Kinga6f6c962006-01-03 22:38:44 +00003071 md->queue.data = md;
3072
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003073 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003074 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00003075 md->disk->fops = &mmc_bdops;
3076 md->disk->private_data = md;
3077 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07003078 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003079 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07003080 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02003081 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02003082 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00003083
3084 /*
3085 * As discussed on lkml, GENHD_FL_REMOVABLE should:
3086 *
3087 * - be set for removable media with permanent block devices
3088 * - be unset for removable block devices with permanent media
3089 *
3090 * Since MMC block devices clearly fall under the second
3091 * case, we do not set GENHD_FL_REMOVABLE. Userspace
3092 * should use the block device creation/destruction hotplug
3093 * messages to tell when the card is present.
3094 */
3095
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003096 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02003097 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00003098
Saugata Dasa5075eb2012-05-17 16:32:21 +05303099 if (mmc_card_mmc(card))
3100 blk_queue_logical_block_size(md->queue.queue,
3101 card->ext_csd.data_sector_size);
3102 else
3103 blk_queue_logical_block_size(md->queue.queue, 512);
3104
Andrei Warkentin371a6892011-04-11 18:10:25 -05003105 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003106
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003107 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02003108 if ((mmc_card_mmc(card) &&
3109 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003110 (mmc_card_sd(card) &&
3111 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
3112 md->flags |= MMC_BLK_CMD23;
3113 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003114
3115 if (mmc_card_mmc(card) &&
3116 md->flags & MMC_BLK_CMD23 &&
3117 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
3118 card->ext_csd.rel_sectors)) {
3119 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06003120 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003121 }
3122
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003123 if (mmc_card_mmc(card) &&
3124 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
3125 (md->flags & MMC_BLK_CMD23) &&
3126 card->ext_csd.packed_event_en) {
3127 if (!mmc_packed_init(&md->queue, card))
3128 md->flags |= MMC_BLK_PACKED_CMD;
3129 }
3130
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00003132
3133 err_putdisk:
3134 put_disk(md->disk);
3135 err_kfree:
3136 kfree(md);
3137 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003138 spin_lock(&mmc_blk_lock);
3139 ida_remove(&mmc_blk_ida, devidx);
3140 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00003141 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142}
3143
Andrei Warkentin371a6892011-04-11 18:10:25 -05003144static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
3145{
3146 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003147
3148 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
3149 /*
3150 * The EXT_CSD sector count is in number or 512 byte
3151 * sectors.
3152 */
3153 size = card->ext_csd.sectors;
3154 } else {
3155 /*
3156 * The CSD capacity field is in units of read_blkbits.
3157 * set_capacity takes units of 512 bytes.
3158 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00003159 size = (typeof(sector_t))card->csd.capacity
3160 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003161 }
3162
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01003163 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003164 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003165}
3166
3167static int mmc_blk_alloc_part(struct mmc_card *card,
3168 struct mmc_blk_data *md,
3169 unsigned int part_type,
3170 sector_t size,
3171 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003172 const char *subname,
3173 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05003174{
3175 char cap_str[10];
3176 struct mmc_blk_data *part_md;
3177
3178 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003179 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003180 if (IS_ERR(part_md))
3181 return PTR_ERR(part_md);
3182 part_md->part_type = part_type;
3183 list_add(&part_md->part, &md->part);
3184
James Bottomleyb9f28d82015-03-05 18:47:01 -08003185 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05003186 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303187 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05003188 part_md->disk->disk_name, mmc_card_id(card),
3189 mmc_card_name(card), part_md->part_type, cap_str);
3190 return 0;
3191}
3192
Namjae Jeone0c368d2011-10-06 23:41:38 +09003193/* MMC Physical partitions consist of two boot partitions and
3194 * up to four general purpose partitions.
3195 * For each partition enabled in EXT_CSD a block device will be allocatedi
3196 * to provide access to the partition.
3197 */
3198
Andrei Warkentin371a6892011-04-11 18:10:25 -05003199static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
3200{
Namjae Jeone0c368d2011-10-06 23:41:38 +09003201 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003202
3203 if (!mmc_card_mmc(card))
3204 return 0;
3205
Namjae Jeone0c368d2011-10-06 23:41:38 +09003206 for (idx = 0; idx < card->nr_parts; idx++) {
3207 if (card->part[idx].size) {
3208 ret = mmc_blk_alloc_part(card, md,
3209 card->part[idx].part_cfg,
3210 card->part[idx].size >> 9,
3211 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003212 card->part[idx].name,
3213 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09003214 if (ret)
3215 return ret;
3216 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003217 }
3218
3219 return ret;
3220}
3221
Andrei Warkentin371a6892011-04-11 18:10:25 -05003222static void mmc_blk_remove_req(struct mmc_blk_data *md)
3223{
Johan Rudholmadd710e2011-12-02 08:51:06 +01003224 struct mmc_card *card;
3225
Andrei Warkentin371a6892011-04-11 18:10:25 -05003226 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003227 /*
3228 * Flush remaining requests and free queues. It
3229 * is freeing the queue that stops new requests
3230 * from being accepted.
3231 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02003232 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003233 mmc_cleanup_queue(&md->queue);
3234 if (md->flags & MMC_BLK_PACKED_CMD)
3235 mmc_packed_clean(&md->queue);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003236 device_remove_file(disk_to_dev(md->disk),
3237 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003238 if (md->disk->flags & GENHD_FL_UP) {
3239 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003240 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3241 card->ext_csd.boot_ro_lockable)
3242 device_remove_file(disk_to_dev(md->disk),
3243 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08003244#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3245 device_remove_file(disk_to_dev(md->disk),
3246 &dev_attr_max_write_speed);
3247 device_remove_file(disk_to_dev(md->disk),
3248 &dev_attr_max_read_speed);
3249 device_remove_file(disk_to_dev(md->disk),
3250 &dev_attr_cache_size);
3251#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05003252
Andrei Warkentin371a6892011-04-11 18:10:25 -05003253 del_gendisk(md->disk);
3254 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003255 mmc_blk_put(md);
3256 }
3257}
3258
3259static void mmc_blk_remove_parts(struct mmc_card *card,
3260 struct mmc_blk_data *md)
3261{
3262 struct list_head *pos, *q;
3263 struct mmc_blk_data *part_md;
3264
3265 list_for_each_safe(pos, q, &md->part) {
3266 part_md = list_entry(pos, struct mmc_blk_data, part);
3267 list_del(pos);
3268 mmc_blk_remove_req(part_md);
3269 }
3270}
3271
3272static int mmc_add_disk(struct mmc_blk_data *md)
3273{
3274 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003275 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003276
Dan Williams307d8e62016-06-20 10:40:44 -07003277 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003278 md->force_ro.show = force_ro_show;
3279 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05303280 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003281 md->force_ro.attr.name = "force_ro";
3282 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
3283 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
3284 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01003285 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003286#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3287 atomic_set(&md->queue.max_write_speed, max_write_speed);
3288 ret = device_create_file(disk_to_dev(md->disk),
3289 &dev_attr_max_write_speed);
3290 if (ret)
3291 goto max_write_speed_fail;
3292 atomic_set(&md->queue.max_read_speed, max_read_speed);
3293 ret = device_create_file(disk_to_dev(md->disk),
3294 &dev_attr_max_read_speed);
3295 if (ret)
3296 goto max_read_speed_fail;
3297 atomic_set(&md->queue.cache_size, cache_size);
3298 atomic_long_set(&md->queue.cache_used, 0);
3299 md->queue.cache_jiffies = jiffies;
3300 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3301 if (ret)
3302 goto cache_size_fail;
3303#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003304
3305 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3306 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04003307 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003308
3309 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
3310 mode = S_IRUGO;
3311 else
3312 mode = S_IRUGO | S_IWUSR;
3313
3314 md->power_ro_lock.show = power_ro_lock_show;
3315 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01003316 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003317 md->power_ro_lock.attr.mode = mode;
3318 md->power_ro_lock.attr.name =
3319 "ro_lock_until_next_power_on";
3320 ret = device_create_file(disk_to_dev(md->disk),
3321 &md->power_ro_lock);
3322 if (ret)
3323 goto power_ro_lock_fail;
3324 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003325
3326 md->num_wr_reqs_to_start_packing.show =
3327 num_wr_reqs_to_start_packing_show;
3328 md->num_wr_reqs_to_start_packing.store =
3329 num_wr_reqs_to_start_packing_store;
3330 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
3331 md->num_wr_reqs_to_start_packing.attr.name =
3332 "num_wr_reqs_to_start_packing";
3333 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
3334 ret = device_create_file(disk_to_dev(md->disk),
3335 &md->num_wr_reqs_to_start_packing);
3336 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02003337 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003338
Maya Erez5a8dae12014-12-04 15:13:59 +02003339 md->no_pack_for_random.show = no_pack_for_random_show;
3340 md->no_pack_for_random.store = no_pack_for_random_store;
3341 sysfs_attr_init(&md->no_pack_for_random.attr);
3342 md->no_pack_for_random.attr.name = "no_pack_for_random";
3343 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
3344 ret = device_create_file(disk_to_dev(md->disk),
3345 &md->no_pack_for_random);
3346 if (ret)
3347 goto no_pack_for_random_fails;
3348
Johan Rudholmadd710e2011-12-02 08:51:06 +01003349 return ret;
3350
Maya Erez5a8dae12014-12-04 15:13:59 +02003351no_pack_for_random_fails:
3352 device_remove_file(disk_to_dev(md->disk),
3353 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02003354num_wr_reqs_to_start_packing_fail:
3355 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003356power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08003357#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3358 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3359cache_size_fail:
3360 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
3361max_read_speed_fail:
3362 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
3363max_write_speed_fail:
3364#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003365 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
3366force_ro_fail:
3367 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003368
3369 return ret;
3370}
3371
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003372static const struct mmc_fixup blk_fixups[] =
3373{
Chris Ballc59d4472011-11-11 22:01:43 -05003374 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
3375 MMC_QUIRK_INAND_CMD38),
3376 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
3377 MMC_QUIRK_INAND_CMD38),
3378 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
3379 MMC_QUIRK_INAND_CMD38),
3380 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
3381 MMC_QUIRK_INAND_CMD38),
3382 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
3383 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003384
3385 /*
3386 * Some MMC cards experience performance degradation with CMD23
3387 * instead of CMD12-bounded multiblock transfers. For now we'll
3388 * black list what's bad...
3389 * - Certain Toshiba cards.
3390 *
3391 * N.B. This doesn't affect SD cards.
3392 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08003393 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3394 MMC_QUIRK_BLK_NO_CMD23),
3395 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3396 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003397 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003398 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003399 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003400 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003401 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003402 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003403
3404 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03003405 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003406 */
Chris Ballc59d4472011-11-11 22:01:43 -05003407 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003408 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03003409 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
3410 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003411
Ian Chen3550ccd2012-08-29 15:05:36 +09003412 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08003413 * Some Samsung MMC cards need longer data read timeout than
3414 * indicated in CSD.
3415 */
3416 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
3417 MMC_QUIRK_LONG_READ_TIME),
3418
3419 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09003420 * On these Samsung MoviNAND parts, performing secure erase or
3421 * secure trim can result in unrecoverable corruption due to a
3422 * firmware bug.
3423 */
3424 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3425 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3426 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3427 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3428 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3429 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3430 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3431 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3432 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3433 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3434 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3435 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3436 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3437 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3438 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3439 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3440
Shawn Linb5b4ff02015-08-12 13:08:32 +08003441 /*
3442 * On Some Kingston eMMCs, performing trim can result in
3443 * unrecoverable data conrruption occasionally due to a firmware bug.
3444 */
3445 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3446 MMC_QUIRK_TRIM_BROKEN),
3447 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3448 MMC_QUIRK_TRIM_BROKEN),
3449
Pratibhasagar V8d664e32014-12-03 18:26:42 +02003450 /* Some INAND MCP devices advertise incorrect timeout values */
3451 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
3452 MMC_QUIRK_INAND_DATA_TIMEOUT),
3453
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003454 END_FIXUP
3455};
3456
Ulf Hansson96541ba2015-04-14 13:06:12 +02003457static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003459 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003460 char cap_str[10];
3461
Pierre Ossman912490d2005-05-21 10:27:02 +01003462 /*
3463 * Check that the card supports the command class(es) we need.
3464 */
3465 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 return -ENODEV;
3467
Lukas Czerner5204d002014-06-18 13:18:07 +02003468 mmc_fixup_device(card, blk_fixups);
3469
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 md = mmc_blk_alloc(card);
3471 if (IS_ERR(md))
3472 return PTR_ERR(md);
3473
James Bottomleyb9f28d82015-03-05 18:47:01 -08003474 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003475 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303476 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003478 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
Andrei Warkentin371a6892011-04-11 18:10:25 -05003480 if (mmc_blk_alloc_parts(card, md))
3481 goto out;
3482
Ulf Hansson96541ba2015-04-14 13:06:12 +02003483 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003484
Andrei Warkentin371a6892011-04-11 18:10:25 -05003485 if (mmc_add_disk(md))
3486 goto out;
3487
3488 list_for_each_entry(part_md, &md->part, part) {
3489 if (mmc_add_disk(part_md))
3490 goto out;
3491 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003492
3493 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
3494 pm_runtime_use_autosuspend(&card->dev);
3495
3496 /*
3497 * Don't enable runtime PM for SD-combo cards here. Leave that
3498 * decision to be taken during the SDIO init sequence instead.
3499 */
3500 if (card->type != MMC_TYPE_SD_COMBO) {
3501 pm_runtime_set_active(&card->dev);
3502 pm_runtime_enable(&card->dev);
3503 }
3504
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 return 0;
3506
3507 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05003508 mmc_blk_remove_parts(card, md);
3509 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01003510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511}
3512
Ulf Hansson96541ba2015-04-14 13:06:12 +02003513static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003515 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
Andrei Warkentin371a6892011-04-11 18:10:25 -05003517 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003518 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003519 mmc_claim_host(card->host);
3520 mmc_blk_part_switch(card, md);
3521 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003522 if (card->type != MMC_TYPE_SD_COMBO)
3523 pm_runtime_disable(&card->dev);
3524 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003525 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02003526 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527}
3528
Ulf Hansson96541ba2015-04-14 13:06:12 +02003529static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003531 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02003532 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303533 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
3535 if (md) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05303536 rc = mmc_queue_suspend(&md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303537 if (rc)
3538 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003539 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05303540 rc = mmc_queue_suspend(&part_md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303541 if (rc)
3542 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303545 goto out;
3546
3547 out_resume:
3548 mmc_queue_resume(&md->queue);
3549 list_for_each_entry(part_md, &md->part, part) {
3550 mmc_queue_resume(&part_md->queue);
3551 }
3552 out:
3553 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554}
3555
Ulf Hansson96541ba2015-04-14 13:06:12 +02003556static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02003557{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003558 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003559}
3560
Ulf Hansson0967edc2014-10-06 11:29:42 +02003561#ifdef CONFIG_PM_SLEEP
3562static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02003563{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003564 struct mmc_card *card = mmc_dev_to_card(dev);
3565
3566 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003567}
3568
Ulf Hansson0967edc2014-10-06 11:29:42 +02003569static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003571 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02003572 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
3574 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003575 /*
3576 * Resume involves the card going into idle state,
3577 * so current partition is always the main one.
3578 */
3579 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003581 list_for_each_entry(part_md, &md->part, part) {
3582 mmc_queue_resume(&part_md->queue);
3583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 }
3585 return 0;
3586}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587#endif
3588
Ulf Hansson0967edc2014-10-06 11:29:42 +02003589static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3590
Ulf Hansson96541ba2015-04-14 13:06:12 +02003591static struct mmc_driver mmc_driver = {
3592 .drv = {
3593 .name = "mmcblk",
3594 .pm = &mmc_blk_pm_ops,
3595 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 .probe = mmc_blk_probe,
3597 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02003598 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599};
3600
3601static int __init mmc_blk_init(void)
3602{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003603 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003605 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3606 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3607
Ben Hutchingsa26eba62014-11-06 03:35:09 +00003608 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003609
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003610 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3611 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003614 res = mmc_register_driver(&mmc_driver);
3615 if (res)
3616 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003618 return 0;
3619 out2:
3620 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 out:
3622 return res;
3623}
3624
3625static void __exit mmc_blk_exit(void)
3626{
3627 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003628 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629}
3630
3631module_init(mmc_blk_init);
3632module_exit(mmc_blk_exit);
3633
3634MODULE_LICENSE("GPL");
3635MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3636