blob: 029a98b401e3ecf19d85a34217f6adeaa961313a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
10 *
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 *
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 *
17 * Author: Andrew Christian
18 * 28 May 2002
19 */
20#include <linux/moduleparam.h>
21#include <linux/module.h>
22#include <linux/init.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Sujit Reddy Thumma55291992014-12-09 20:40:16 +020033#include <linux/bitops.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020034#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040035#include <linux/delay.h>
36#include <linux/capability.h>
37#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020038#include <linux/pm_runtime.h>
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -070039#include <linux/ioprio.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020040#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
John Calixtocb87ea22011-04-26 18:56:29 -040042#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mmc/card.h>
Dov Levenglicka0296392015-06-24 19:51:58 +030044#include <linux/mmc/core.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020045#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010046#include <linux/mmc/mmc.h>
47#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/uaccess.h>
50
Pierre Ossman98ac2162006-12-23 20:03:02 +010051#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080052#include "block.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000054MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040055#ifdef MODULE_PARAM_PREFIX
56#undef MODULE_PARAM_PREFIX
57#endif
58#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010059
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050060#define INAND_CMD38_ARG_EXT_CSD 113
61#define INAND_CMD38_ARG_ERASE 0x00
62#define INAND_CMD38_ARG_TRIM 0x01
63#define INAND_CMD38_ARG_SECERASE 0x80
64#define INAND_CMD38_ARG_SECTRIM1 0x81
65#define INAND_CMD38_ARG_SECTRIM2 0x88
Subhash Jadavani2fbab612014-12-04 15:16:17 +020066#define MMC_BLK_TIMEOUT_MS (30 * 1000) /* 30 sec timeout */
Maya Erez775a9362013-04-18 15:41:55 +030067#define MMC_SANITIZE_REQ_TIMEOUT 240000
68#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Asutosh Das02e30862015-05-20 16:52:04 +053069#define MMC_CMDQ_STOP_TIMEOUT_MS 100
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050070
Luca Porziod3df0462015-11-06 15:12:26 +000071#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090072 (rq_data_dir(req) == WRITE))
73#define PACKED_CMD_VER 0x01
74#define PACKED_CMD_WR 0x02
Lee Susman841fd132013-04-23 17:59:26 +030075#define PACKED_TRIGGER_MAX_ELEMENTS 5000
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090076
Maya Erezf93ca0a2014-12-09 23:34:41 +020077#define MMC_BLK_MAX_RETRIES 5 /* max # of retries before aborting a command */
Tatyana Brokhman08238ce2012-10-07 10:33:13 +020078#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
79 do { \
80 if (stats->enabled) \
81 stats->pack_stop_reason[reason]++; \
82 } while (0)
83
Asutosh Das02e30862015-05-20 16:52:04 +053084#define MAX_RETRIES 5
Lee Susman841fd132013-04-23 17:59:26 +030085#define PCKD_TRGR_INIT_MEAN_POTEN 17
86#define PCKD_TRGR_POTEN_LOWER_BOUND 5
87#define PCKD_TRGR_URGENT_PENALTY 2
88#define PCKD_TRGR_LOWER_BOUND 5
89#define PCKD_TRGR_PRECISION_MULTIPLIER 100
90
Sahitya Tummala9433a132015-06-09 09:38:36 +053091static struct mmc_cmdq_req *mmc_cmdq_prep_dcmd(
92 struct mmc_queue_req *mqrq, struct mmc_queue *mq);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020093static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040094
95/*
96 * The defaults come from config options but can be overriden by module
97 * or bootarg options.
98 */
99static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
100
101/*
102 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +0000103 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200104 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400105 */
106static int max_devices;
107
Ben Hutchingsa26eba62014-11-06 03:35:09 +0000108#define MAX_DEVICES 256
109
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200110static DEFINE_IDA(mmc_blk_ida);
111static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/*
114 * There is one mmc_blk_data per slot.
115 */
116struct mmc_blk_data {
117 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -0700118 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct gendisk *disk;
120 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500121 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500123 unsigned int flags;
124#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
125#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900126#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -0700127#define MMC_BLK_CMD_QUEUE (1 << 3) /* MMC command queue support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000130 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500131 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300132 unsigned int reset_done;
133#define MMC_BLK_READ BIT(0)
134#define MMC_BLK_WRITE BIT(1)
135#define MMC_BLK_DISCARD BIT(2)
136#define MMC_BLK_SECDISCARD BIT(3)
Talel Shenhar8a8e3b42015-02-11 12:58:16 +0200137#define MMC_BLK_FLUSH BIT(4)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500138
139 /*
140 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200141 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500142 * track of the current selected device partition.
143 */
144 unsigned int part_curr;
145 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100146 struct device_attribute power_ro_lock;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200147 struct device_attribute num_wr_reqs_to_start_packing;
Maya Erez5a8dae12014-12-04 15:13:59 +0200148 struct device_attribute no_pack_for_random;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100149 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
Arjan van de Vena621aae2006-01-12 18:43:35 +0000152static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900154enum {
155 MMC_PACKED_NR_IDX = -1,
156 MMC_PACKED_NR_ZERO,
157 MMC_PACKED_NR_SINGLE,
158};
159
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400160module_param(perdev_minors, int, 0444);
161MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
162
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200163static inline int mmc_blk_part_switch(struct mmc_card *card,
164 struct mmc_blk_data *md);
165static int get_card_status(struct mmc_card *card, u32 *status, int retries);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -0700166static int mmc_blk_cmdq_switch(struct mmc_card *card,
167 struct mmc_blk_data *md, bool enable);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200168
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900169static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
170{
171 struct mmc_packed *packed = mqrq->packed;
172
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900173 mqrq->cmd_type = MMC_PACKED_NONE;
174 packed->nr_entries = MMC_PACKED_NR_ZERO;
175 packed->idx_failure = MMC_PACKED_NR_IDX;
176 packed->retries = 0;
177 packed->blocks = 0;
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
181{
182 struct mmc_blk_data *md;
183
Arjan van de Vena621aae2006-01-12 18:43:35 +0000184 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 md = disk->private_data;
186 if (md && md->usage == 0)
187 md = NULL;
188 if (md)
189 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000190 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 return md;
193}
194
Andrei Warkentin371a6892011-04-11 18:10:25 -0500195static inline int mmc_get_devidx(struct gendisk *disk)
196{
Colin Cross382c55f2015-10-22 10:00:41 -0700197 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500198 return devidx;
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static void mmc_blk_put(struct mmc_blk_data *md)
202{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000203 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 md->usage--;
205 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500206 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800207 blk_cleanup_queue(md->queue.queue);
208
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200209 spin_lock(&mmc_blk_lock);
210 ida_remove(&mmc_blk_ida, devidx);
211 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 kfree(md);
215 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000216 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Johan Rudholmadd710e2011-12-02 08:51:06 +0100219static ssize_t power_ro_lock_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
221{
222 int ret;
223 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200224 struct mmc_card *card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100225 int locked = 0;
226
Asutosh Das507d9a72014-12-09 10:15:53 +0200227 if (!md)
228 return -EINVAL;
229
230 card = md->queue.card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100231 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
232 locked = 2;
233 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
234 locked = 1;
235
236 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
237
Tomas Winkler9098f842015-07-16 15:50:45 +0200238 mmc_blk_put(md);
239
Johan Rudholmadd710e2011-12-02 08:51:06 +0100240 return ret;
241}
242
243static ssize_t power_ro_lock_store(struct device *dev,
244 struct device_attribute *attr, const char *buf, size_t count)
245{
246 int ret;
247 struct mmc_blk_data *md, *part_md;
248 struct mmc_card *card;
249 unsigned long set;
250
251 if (kstrtoul(buf, 0, &set))
252 return -EINVAL;
253
254 if (set != 1)
255 return count;
256
257 md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200258 if (!md)
259 return -EINVAL;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100260 card = md->queue.card;
261
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200262 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100263
264 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
265 card->ext_csd.boot_ro_lock |
266 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
267 card->ext_csd.part_time);
268 if (ret)
269 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
270 else
271 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
272
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200273 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100274
275 if (!ret) {
276 pr_info("%s: Locking boot partition ro until next power on\n",
277 md->disk->disk_name);
278 set_disk_ro(md->disk, 1);
279
280 list_for_each_entry(part_md, &md->part, part)
281 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
282 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
283 set_disk_ro(part_md->disk, 1);
284 }
285 }
286
287 mmc_blk_put(md);
288 return count;
289}
290
Andrei Warkentin371a6892011-04-11 18:10:25 -0500291static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
292 char *buf)
293{
294 int ret;
295 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
296
Asutosh Das507d9a72014-12-09 10:15:53 +0200297 if (!md)
298 return -EINVAL;
299
Baruch Siach0031a982014-09-22 10:12:51 +0300300 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500301 get_disk_ro(dev_to_disk(dev)) ^
302 md->read_only);
303 mmc_blk_put(md);
304 return ret;
305}
306
307static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
308 const char *buf, size_t count)
309{
310 int ret;
311 char *end;
312 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
313 unsigned long set = simple_strtoul(buf, &end, 0);
Asutosh Das507d9a72014-12-09 10:15:53 +0200314
315 if (!md)
316 return -EINVAL;
317
Andrei Warkentin371a6892011-04-11 18:10:25 -0500318 if (end == buf) {
319 ret = -EINVAL;
320 goto out;
321 }
322
323 set_disk_ro(dev_to_disk(dev), set || md->read_only);
324 ret = count;
325out:
326 mmc_blk_put(md);
327 return ret;
328}
329
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200330static ssize_t
Maya Erez5a8dae12014-12-04 15:13:59 +0200331no_pack_for_random_show(struct device *dev,
332 struct device_attribute *attr, char *buf)
333{
334 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
335 int ret;
336
Asutosh Das507d9a72014-12-09 10:15:53 +0200337 if (!md)
338 return -EINVAL;
Maya Erez5a8dae12014-12-04 15:13:59 +0200339 ret = snprintf(buf, PAGE_SIZE, "%d\n", md->queue.no_pack_for_random);
340
341 mmc_blk_put(md);
342 return ret;
343}
344
345static ssize_t
346no_pack_for_random_store(struct device *dev,
347 struct device_attribute *attr,
348 const char *buf, size_t count)
349{
350 int value;
351 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200352 struct mmc_card *card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200353 int ret = count;
354
Asutosh Das507d9a72014-12-09 10:15:53 +0200355 if (!md)
356 return -EINVAL;
357
358 card = md->queue.card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200359 if (!card) {
360 ret = -EINVAL;
361 goto exit;
362 }
363
364 sscanf(buf, "%d", &value);
365
366 if (value < 0) {
367 pr_err("%s: value %d is not valid. old value remains = %d",
368 mmc_hostname(card->host), value,
369 md->queue.no_pack_for_random);
370 ret = -EINVAL;
371 goto exit;
372 }
373
374 md->queue.no_pack_for_random = (value > 0) ? true : false;
375
376 pr_debug("%s: no_pack_for_random: new value = %d",
377 mmc_hostname(card->host),
378 md->queue.no_pack_for_random);
379
380exit:
381 mmc_blk_put(md);
382 return ret;
383}
384
385static ssize_t
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200386num_wr_reqs_to_start_packing_show(struct device *dev,
387 struct device_attribute *attr, char *buf)
388{
389 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
390 int num_wr_reqs_to_start_packing;
391 int ret;
392
Asutosh Das507d9a72014-12-09 10:15:53 +0200393 if (!md)
394 return -EINVAL;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200395 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
396
397 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
398
399 mmc_blk_put(md);
400 return ret;
401}
402
403static ssize_t
404num_wr_reqs_to_start_packing_store(struct device *dev,
405 struct device_attribute *attr,
406 const char *buf, size_t count)
407{
408 int value;
409 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200410 struct mmc_card *card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200411 int ret = count;
412
Asutosh Das507d9a72014-12-09 10:15:53 +0200413 if (!md)
414 return -EINVAL;
415
416 card = md->queue.card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200417 if (!card) {
418 ret = -EINVAL;
419 goto exit;
420 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200421
422 sscanf(buf, "%d", &value);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200423
Yaniv Gardi42399822014-12-04 00:26:23 +0200424 if (value >= 0) {
425 md->queue.num_wr_reqs_to_start_packing =
426 min_t(int, value, (int)card->ext_csd.max_packed_writes);
427
428 pr_debug("%s: trigger to pack: new value = %d",
429 mmc_hostname(card->host),
430 md->queue.num_wr_reqs_to_start_packing);
431 } else {
432 pr_err("%s: value %d is not valid. old value remains = %d",
433 mmc_hostname(card->host), value,
434 md->queue.num_wr_reqs_to_start_packing);
435 ret = -EINVAL;
436 }
437
438exit:
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200439 mmc_blk_put(md);
Yaniv Gardi42399822014-12-04 00:26:23 +0200440 return ret;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200441}
442
Mark Salyzyn6904e432016-01-28 11:12:25 -0800443#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
444
445static int max_read_speed, max_write_speed, cache_size = 4;
446
447module_param(max_read_speed, int, S_IRUSR | S_IRGRP);
448MODULE_PARM_DESC(max_read_speed, "maximum KB/s read speed 0=off");
449module_param(max_write_speed, int, S_IRUSR | S_IRGRP);
450MODULE_PARM_DESC(max_write_speed, "maximum KB/s write speed 0=off");
451module_param(cache_size, int, S_IRUSR | S_IRGRP);
452MODULE_PARM_DESC(cache_size, "MB high speed memory or SLC cache");
453
454/*
455 * helper macros and expectations:
456 * size - unsigned long number of bytes
457 * jiffies - unsigned long HZ timestamp difference
458 * speed - unsigned KB/s transfer rate
459 */
460#define size_and_speed_to_jiffies(size, speed) \
461 ((size) * HZ / (speed) / 1024UL)
462#define jiffies_and_speed_to_size(jiffies, speed) \
463 (((speed) * (jiffies) * 1024UL) / HZ)
464#define jiffies_and_size_to_speed(jiffies, size) \
465 ((size) * HZ / (jiffies) / 1024UL)
466
467/* Limits to report warning */
468/* jiffies_and_size_to_speed(10*HZ, queue_max_hw_sectors(q) * 512UL) ~ 25 */
469#define MIN_SPEED(q) 250 /* 10 times faster than a floppy disk */
470#define MAX_SPEED(q) jiffies_and_size_to_speed(1, queue_max_sectors(q) * 512UL)
471
472#define speed_valid(speed) ((speed) > 0)
473
474static const char off[] = "off\n";
475
476static int max_speed_show(int speed, char *buf)
477{
478 if (speed)
479 return scnprintf(buf, PAGE_SIZE, "%uKB/s\n", speed);
480 else
481 return scnprintf(buf, PAGE_SIZE, off);
482}
483
484static int max_speed_store(const char *buf, struct request_queue *q)
485{
486 unsigned int limit, set = 0;
487
488 if (!strncasecmp(off, buf, sizeof(off) - 2))
489 return set;
490 if (kstrtouint(buf, 0, &set) || (set > INT_MAX))
491 return -EINVAL;
492 if (set == 0)
493 return set;
494 limit = MAX_SPEED(q);
495 if (set > limit)
496 pr_warn("max speed %u ineffective above %u\n", set, limit);
497 limit = MIN_SPEED(q);
498 if (set < limit)
499 pr_warn("max speed %u painful below %u\n", set, limit);
500 return set;
501}
502
503static ssize_t max_write_speed_show(struct device *dev,
504 struct device_attribute *attr, char *buf)
505{
506 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
507 int ret = max_speed_show(atomic_read(&md->queue.max_write_speed), buf);
508
509 mmc_blk_put(md);
510 return ret;
511}
512
513static ssize_t max_write_speed_store(struct device *dev,
514 struct device_attribute *attr,
515 const char *buf, size_t count)
516{
517 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
518 int set = max_speed_store(buf, md->queue.queue);
519
520 if (set < 0) {
521 mmc_blk_put(md);
522 return set;
523 }
524
525 atomic_set(&md->queue.max_write_speed, set);
526 mmc_blk_put(md);
527 return count;
528}
529
530static const DEVICE_ATTR(max_write_speed, S_IRUGO | S_IWUSR,
531 max_write_speed_show, max_write_speed_store);
532
533static ssize_t max_read_speed_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
535{
536 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
537 int ret = max_speed_show(atomic_read(&md->queue.max_read_speed), buf);
538
539 mmc_blk_put(md);
540 return ret;
541}
542
543static ssize_t max_read_speed_store(struct device *dev,
544 struct device_attribute *attr,
545 const char *buf, size_t count)
546{
547 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
548 int set = max_speed_store(buf, md->queue.queue);
549
550 if (set < 0) {
551 mmc_blk_put(md);
552 return set;
553 }
554
555 atomic_set(&md->queue.max_read_speed, set);
556 mmc_blk_put(md);
557 return count;
558}
559
560static const DEVICE_ATTR(max_read_speed, S_IRUGO | S_IWUSR,
561 max_read_speed_show, max_read_speed_store);
562
563static ssize_t cache_size_show(struct device *dev,
564 struct device_attribute *attr, char *buf)
565{
566 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
567 struct mmc_queue *mq = &md->queue;
568 int cache_size = atomic_read(&mq->cache_size);
569 int ret;
570
571 if (!cache_size)
572 ret = scnprintf(buf, PAGE_SIZE, off);
573 else {
574 int speed = atomic_read(&mq->max_write_speed);
575
576 if (!speed_valid(speed))
577 ret = scnprintf(buf, PAGE_SIZE, "%uMB\n", cache_size);
578 else { /* We accept race between cache_jiffies and cache_used */
579 unsigned long size = jiffies_and_speed_to_size(
580 jiffies - mq->cache_jiffies, speed);
581 long used = atomic_long_read(&mq->cache_used);
582
583 if (size >= used)
584 size = 0;
585 else
586 size = (used - size) * 100 / cache_size
587 / 1024UL / 1024UL;
588
589 ret = scnprintf(buf, PAGE_SIZE, "%uMB %lu%% used\n",
590 cache_size, size);
591 }
592 }
593
594 mmc_blk_put(md);
595 return ret;
596}
597
598static ssize_t cache_size_store(struct device *dev,
599 struct device_attribute *attr,
600 const char *buf, size_t count)
601{
602 struct mmc_blk_data *md;
603 unsigned int set = 0;
604
605 if (strncasecmp(off, buf, sizeof(off) - 2)
606 && (kstrtouint(buf, 0, &set) || (set > INT_MAX)))
607 return -EINVAL;
608
609 md = mmc_blk_get(dev_to_disk(dev));
610 atomic_set(&md->queue.cache_size, set);
611 mmc_blk_put(md);
612 return count;
613}
614
615static const DEVICE_ATTR(cache_size, S_IRUGO | S_IWUSR,
616 cache_size_show, cache_size_store);
617
618/* correct for write-back */
619static long mmc_blk_cache_used(struct mmc_queue *mq, unsigned long waitfor)
620{
621 long used = 0;
622 int speed = atomic_read(&mq->max_write_speed);
623
624 if (speed_valid(speed)) {
625 unsigned long size = jiffies_and_speed_to_size(
626 waitfor - mq->cache_jiffies, speed);
627 used = atomic_long_read(&mq->cache_used);
628
629 if (size >= used)
630 used = 0;
631 else
632 used -= size;
633 }
634
635 atomic_long_set(&mq->cache_used, used);
636 mq->cache_jiffies = waitfor;
637
638 return used;
639}
640
641static void mmc_blk_simulate_delay(
642 struct mmc_queue *mq,
643 struct request *req,
644 unsigned long waitfor)
645{
646 int max_speed;
647
648 if (!req)
649 return;
650
651 max_speed = (rq_data_dir(req) == READ)
652 ? atomic_read(&mq->max_read_speed)
653 : atomic_read(&mq->max_write_speed);
654 if (speed_valid(max_speed)) {
655 unsigned long bytes = blk_rq_bytes(req);
656
657 if (rq_data_dir(req) != READ) {
658 int cache_size = atomic_read(&mq->cache_size);
659
660 if (cache_size) {
661 unsigned long size = cache_size * 1024L * 1024L;
662 long used = mmc_blk_cache_used(mq, waitfor);
663
664 used += bytes;
665 atomic_long_set(&mq->cache_used, used);
666 bytes = 0;
667 if (used > size)
668 bytes = used - size;
669 }
670 }
671 waitfor += size_and_speed_to_jiffies(bytes, max_speed);
672 if (time_is_after_jiffies(waitfor)) {
673 long msecs = jiffies_to_msecs(waitfor - jiffies);
674
675 if (likely(msecs > 0))
676 msleep(msecs);
677 }
678 }
679}
680
681#else
682
683#define mmc_blk_simulate_delay(mq, req, waitfor)
684
685#endif
686
Al Viroa5a15612008-03-02 10:33:30 -0500687static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Al Viroa5a15612008-03-02 10:33:30 -0500689 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int ret = -ENXIO;
691
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200692 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (md) {
694 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500695 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700697
Al Viroa5a15612008-03-02 10:33:30 -0500698 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700699 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700700 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200703 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 return ret;
706}
707
Al Virodb2a1442013-05-05 21:52:57 -0400708static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Al Viroa5a15612008-03-02 10:33:30 -0500710 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200712 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200714 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800718mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800720 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
721 geo->heads = 4;
722 geo->sectors = 16;
723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
John Calixtocb87ea22011-04-26 18:56:29 -0400726struct mmc_blk_ioc_data {
727 struct mmc_ioc_cmd ic;
728 unsigned char *buf;
729 u64 buf_bytes;
730};
731
732static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
733 struct mmc_ioc_cmd __user *user)
734{
735 struct mmc_blk_ioc_data *idata;
736 int err;
737
yalin wang1ff89502015-11-12 19:27:11 +0800738 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400739 if (!idata) {
740 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400741 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400742 }
743
744 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
745 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400746 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400747 }
748
749 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
750 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
751 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400752 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400753 }
754
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300755 if (!idata->buf_bytes) {
756 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100757 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300758 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100759
yalin wang1ff89502015-11-12 19:27:11 +0800760 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400761 if (!idata->buf) {
762 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400763 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400764 }
765
766 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
767 idata->ic.data_ptr, idata->buf_bytes)) {
768 err = -EFAULT;
769 goto copy_err;
770 }
771
772 return idata;
773
774copy_err:
775 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400776idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400777 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400778out:
John Calixtocb87ea22011-04-26 18:56:29 -0400779 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400780}
781
Jon Huntera5f57742015-09-22 10:27:53 +0100782static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
783 struct mmc_blk_ioc_data *idata)
784{
785 struct mmc_ioc_cmd *ic = &idata->ic;
786
787 if (copy_to_user(&(ic_ptr->response), ic->response,
788 sizeof(ic->response)))
789 return -EFAULT;
790
791 if (!idata->ic.write_flag) {
792 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
793 idata->buf, idata->buf_bytes))
794 return -EFAULT;
795 }
796
797 return 0;
798}
799
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200800static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
801 u32 retries_max)
802{
803 int err;
804 u32 retry_count = 0;
805
806 if (!status || !retries_max)
807 return -EINVAL;
808
809 do {
810 err = get_card_status(card, status, 5);
811 if (err)
812 break;
813
814 if (!R1_STATUS(*status) &&
815 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
816 break; /* RPMB programming operation complete */
817
818 /*
819 * Rechedule to give the MMC device a chance to continue
820 * processing the previous command without being polled too
821 * frequently.
822 */
823 usleep_range(1000, 5000);
824 } while (++retry_count < retries_max);
825
826 if (retry_count == retries_max)
827 err = -EPERM;
828
829 return err;
830}
831
Maya Erez775a9362013-04-18 15:41:55 +0300832static int ioctl_do_sanitize(struct mmc_card *card)
833{
834 int err;
835
Pavan Anamula07d62ef2015-08-24 18:56:22 +0530836 if (!mmc_can_sanitize(card) &&
837 (card->host->caps2 & MMC_CAP2_SANITIZE)) {
838 pr_warn("%s: %s - SANITIZE is not supported\n",
Maya Erez775a9362013-04-18 15:41:55 +0300839 mmc_hostname(card->host), __func__);
Pavan Anamula07d62ef2015-08-24 18:56:22 +0530840 err = -EOPNOTSUPP;
841 goto out;
Maya Erez775a9362013-04-18 15:41:55 +0300842 }
843
844 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
845 mmc_hostname(card->host), __func__);
846
847 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
848 EXT_CSD_SANITIZE_START, 1,
849 MMC_SANITIZE_REQ_TIMEOUT);
850
851 if (err)
852 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
853 mmc_hostname(card->host), __func__, err);
854
855 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
856 __func__);
857out:
858 return err;
859}
860
Jon Huntera5f57742015-09-22 10:27:53 +0100861static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
862 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400863{
John Calixtocb87ea22011-04-26 18:56:29 -0400864 struct mmc_command cmd = {0};
865 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530866 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400867 struct scatterlist sg;
868 int err;
869
Jon Huntera5f57742015-09-22 10:27:53 +0100870 if (!card || !md || !idata)
871 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400872
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100873 cmd.opcode = idata->ic.opcode;
874 cmd.arg = idata->ic.arg;
875 cmd.flags = idata->ic.flags;
876
877 if (idata->buf_bytes) {
878 data.sg = &sg;
879 data.sg_len = 1;
880 data.blksz = idata->ic.blksz;
881 data.blocks = idata->ic.blocks;
882
883 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
884
885 if (idata->ic.write_flag)
886 data.flags = MMC_DATA_WRITE;
887 else
888 data.flags = MMC_DATA_READ;
889
890 /* data.flags must already be set before doing this. */
891 mmc_set_data_timeout(&data, card);
892
893 /* Allow overriding the timeout_ns for empirical tuning. */
894 if (idata->ic.data_timeout_ns)
895 data.timeout_ns = idata->ic.data_timeout_ns;
896
897 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
898 /*
899 * Pretend this is a data transfer and rely on the
900 * host driver to compute timeout. When all host
901 * drivers support cmd.cmd_timeout for R1B, this
902 * can be changed to:
903 *
904 * mrq.data = NULL;
905 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
906 */
907 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
908 }
909
910 mrq.data = &data;
911 }
912
913 mrq.cmd = &cmd;
914
Maya Erez70afbaf2015-10-01 11:27:17 +0300915 if (mmc_card_doing_bkops(card)) {
916 err = mmc_stop_bkops(card);
917 if (err) {
918 dev_err(mmc_dev(card->host),
919 "%s: stop_bkops failed %d\n", __func__, err);
920 return err;
921 }
922 }
923
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200924 err = mmc_blk_part_switch(card, md);
925 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100926 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200927
John Calixtocb87ea22011-04-26 18:56:29 -0400928 if (idata->ic.is_acmd) {
929 err = mmc_app_cmd(card->host, card);
930 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100931 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400932 }
933
Yaniv Gardia82e4842013-06-05 14:13:08 +0300934 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
935 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300936 err = ioctl_do_sanitize(card);
937
938 if (err)
939 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
940 __func__, err);
941
Jon Huntera5f57742015-09-22 10:27:53 +0100942 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300943 }
944
John Calixtocb87ea22011-04-26 18:56:29 -0400945 mmc_wait_for_req(card->host, &mrq);
946
947 if (cmd.error) {
948 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
949 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100950 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400951 }
952 if (data.error) {
953 dev_err(mmc_dev(card->host), "%s: data error %d\n",
954 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100955 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400956 }
957
958 /*
959 * According to the SD specs, some commands require a delay after
960 * issuing the command.
961 */
962 if (idata->ic.postsleep_min_us)
963 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
964
Jon Huntera5f57742015-09-22 10:27:53 +0100965 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400966
Krishna Kondae6711632014-12-04 15:20:57 +0200967 return err;
968}
969
970struct mmc_blk_ioc_rpmb_data {
971 struct mmc_blk_ioc_data *data[MMC_IOC_MAX_RPMB_CMD];
972};
973
974static struct mmc_blk_ioc_rpmb_data *mmc_blk_ioctl_rpmb_copy_from_user(
975 struct mmc_ioc_rpmb __user *user)
976{
977 struct mmc_blk_ioc_rpmb_data *idata;
978 int err, i;
979
980 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
981 if (!idata) {
982 err = -ENOMEM;
983 goto out;
984 }
985
986 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
987 idata->data[i] = mmc_blk_ioctl_copy_from_user(&(user->cmds[i]));
988 if (IS_ERR(idata->data[i])) {
989 err = PTR_ERR(idata->data[i]);
990 goto copy_err;
991 }
992 }
993
994 return idata;
995
996copy_err:
997 while (--i >= 0) {
998 kfree(idata->data[i]->buf);
999 kfree(idata->data[i]);
1000 }
1001 kfree(idata);
1002out:
1003 return ERR_PTR(err);
1004}
1005
1006static int mmc_blk_ioctl_rpmb_cmd(struct block_device *bdev,
1007 struct mmc_ioc_rpmb __user *ic_ptr)
1008{
1009 struct mmc_blk_ioc_rpmb_data *idata;
1010 struct mmc_blk_data *md;
1011 struct mmc_card *card;
1012 struct mmc_command cmd = {0};
1013 struct mmc_data data = {0};
1014 struct mmc_request mrq = {NULL};
1015 struct scatterlist sg;
1016 int err = 0, i = 0;
1017 u32 status = 0;
1018
1019 /* The caller must have CAP_SYS_RAWIO */
1020 if (!capable(CAP_SYS_RAWIO))
1021 return -EPERM;
1022
1023 md = mmc_blk_get(bdev->bd_disk);
1024 /* make sure this is a rpmb partition */
1025 if ((!md) || (!(md->area_type & MMC_BLK_DATA_AREA_RPMB))) {
1026 err = -EINVAL;
Asutosh Das507d9a72014-12-09 10:15:53 +02001027 return err;
Krishna Kondae6711632014-12-04 15:20:57 +02001028 }
1029
1030 idata = mmc_blk_ioctl_rpmb_copy_from_user(ic_ptr);
1031 if (IS_ERR(idata)) {
1032 err = PTR_ERR(idata);
1033 goto cmd_done;
1034 }
1035
1036 card = md->queue.card;
1037 if (IS_ERR(card)) {
1038 err = PTR_ERR(card);
1039 goto idata_free;
1040 }
1041
Maya Erezdd669562015-02-12 20:37:31 +02001042 mmc_get_card(card);
Krishna Kondae6711632014-12-04 15:20:57 +02001043
Maya Erez70afbaf2015-10-01 11:27:17 +03001044 if (mmc_card_doing_bkops(card)) {
1045 if (mmc_card_cmdq(card)) {
1046 err = mmc_cmdq_halt(card->host, true);
1047 if (err)
1048 goto cmd_rel_host;
1049 }
1050 err = mmc_stop_bkops(card);
1051 if (err) {
1052 dev_err(mmc_dev(card->host),
1053 "%s: stop_bkops failed %d\n", __func__, err);
1054 goto cmd_rel_host;
1055 }
1056 if (mmc_card_cmdq(card)) {
1057 err = mmc_cmdq_halt(card->host, false);
1058 if (err)
1059 goto cmd_rel_host;
1060 }
1061 }
1062
Krishna Kondae6711632014-12-04 15:20:57 +02001063 err = mmc_blk_part_switch(card, md);
1064 if (err)
1065 goto cmd_rel_host;
1066
1067 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1068 struct mmc_blk_ioc_data *curr_data;
1069 struct mmc_ioc_cmd *curr_cmd;
1070
1071 curr_data = idata->data[i];
1072 curr_cmd = &curr_data->ic;
1073 if (!curr_cmd->opcode)
1074 break;
1075
1076 cmd.opcode = curr_cmd->opcode;
1077 cmd.arg = curr_cmd->arg;
1078 cmd.flags = curr_cmd->flags;
1079
1080 if (curr_data->buf_bytes) {
1081 data.sg = &sg;
1082 data.sg_len = 1;
1083 data.blksz = curr_cmd->blksz;
1084 data.blocks = curr_cmd->blocks;
1085
1086 sg_init_one(data.sg, curr_data->buf,
1087 curr_data->buf_bytes);
1088
1089 if (curr_cmd->write_flag)
1090 data.flags = MMC_DATA_WRITE;
1091 else
1092 data.flags = MMC_DATA_READ;
1093
1094 /* data.flags must already be set before doing this. */
1095 mmc_set_data_timeout(&data, card);
1096
1097 /*
1098 * Allow overriding the timeout_ns for empirical tuning.
1099 */
1100 if (curr_cmd->data_timeout_ns)
1101 data.timeout_ns = curr_cmd->data_timeout_ns;
1102
1103 mrq.data = &data;
1104 }
1105
1106 mrq.cmd = &cmd;
1107
1108 err = mmc_set_blockcount(card, data.blocks,
1109 curr_cmd->write_flag & (1 << 31));
1110 if (err)
1111 goto cmd_rel_host;
1112
1113 mmc_wait_for_req(card->host, &mrq);
1114
1115 if (cmd.error) {
1116 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
1117 __func__, cmd.error);
1118 err = cmd.error;
1119 goto cmd_rel_host;
1120 }
1121 if (data.error) {
1122 dev_err(mmc_dev(card->host), "%s: data error %d\n",
1123 __func__, data.error);
1124 err = data.error;
1125 goto cmd_rel_host;
1126 }
1127
1128 if (copy_to_user(&(ic_ptr->cmds[i].response), cmd.resp,
1129 sizeof(cmd.resp))) {
1130 err = -EFAULT;
1131 goto cmd_rel_host;
1132 }
1133
1134 if (!curr_cmd->write_flag) {
1135 if (copy_to_user((void __user *)(unsigned long)
1136 curr_cmd->data_ptr,
1137 curr_data->buf,
1138 curr_data->buf_bytes)) {
1139 err = -EFAULT;
1140 goto cmd_rel_host;
1141 }
1142 }
1143
Loic Pallardy8d1e9772012-08-06 17:12:31 +02001144 /*
1145 * Ensure RPMB command has completed by polling CMD13
1146 * "Send Status".
1147 */
1148 err = ioctl_rpmb_card_status_poll(card, &status, 5);
1149 if (err)
1150 dev_err(mmc_dev(card->host),
1151 "%s: Card Status=0x%08X, error %d\n",
1152 __func__, status, err);
1153 }
1154
Krishna Kondae6711632014-12-04 15:20:57 +02001155cmd_rel_host:
1156 mmc_put_card(card);
1157
1158idata_free:
1159 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1160 kfree(idata->data[i]->buf);
1161 kfree(idata->data[i]);
1162 }
1163 kfree(idata);
1164
1165cmd_done:
1166 mmc_blk_put(md);
Jon Huntera5f57742015-09-22 10:27:53 +01001167 return err;
1168}
1169
1170static int mmc_blk_ioctl_cmd(struct block_device *bdev,
1171 struct mmc_ioc_cmd __user *ic_ptr)
1172{
1173 struct mmc_blk_ioc_data *idata;
1174 struct mmc_blk_data *md;
1175 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -07001176 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001177
Shawn Lin83c742c2016-03-16 18:15:47 +08001178 /*
1179 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1180 * whole block device, not on a partition. This prevents overspray
1181 * between sibling partitions.
1182 */
1183 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1184 return -EPERM;
1185
Jon Huntera5f57742015-09-22 10:27:53 +01001186 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
Asutosh Dasbbefab32013-10-07 14:53:32 +05301187 if (IS_ERR_OR_NULL(idata))
Jon Huntera5f57742015-09-22 10:27:53 +01001188 return PTR_ERR(idata);
1189
1190 md = mmc_blk_get(bdev->bd_disk);
1191 if (!md) {
1192 err = -EINVAL;
1193 goto cmd_err;
1194 }
1195
1196 card = md->queue.card;
Asutosh Dasbbefab32013-10-07 14:53:32 +05301197 if (IS_ERR_OR_NULL(card)) {
Jon Huntera5f57742015-09-22 10:27:53 +01001198 err = PTR_ERR(card);
1199 goto cmd_done;
1200 }
1201
1202 mmc_get_card(card);
1203
Ritesh Harjanicd62f1b2015-07-13 10:52:36 +05301204 if (mmc_card_cmdq(card)) {
1205 err = mmc_cmdq_halt_on_empty_queue(card->host);
1206 if (err) {
1207 pr_err("%s: halt failed while doing %s err (%d)\n",
1208 mmc_hostname(card->host),
1209 __func__, err);
1210 mmc_put_card(card);
1211 goto cmd_done;
1212 }
1213 }
1214
Grant Grundlerb0934102015-09-23 18:30:33 -07001215 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001216
Adrian Hunter3c866562016-05-04 14:38:12 +03001217 /* Always switch back to main area after RPMB access */
1218 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1219 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1220
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001221 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -04001222
Grant Grundlerb0934102015-09-23 18:30:33 -07001223 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001224
Ritesh Harjanicd62f1b2015-07-13 10:52:36 +05301225 if (mmc_card_cmdq(card)) {
1226 if (mmc_cmdq_halt(card->host, false))
1227 pr_err("%s: %s: cmdq unhalt failed\n",
1228 mmc_hostname(card->host), __func__);
1229 }
1230
John Calixtocb87ea22011-04-26 18:56:29 -04001231cmd_done:
1232 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +03001233cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -04001234 kfree(idata->buf);
1235 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001236 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -04001237}
1238
Jon Huntera5f57742015-09-22 10:27:53 +01001239static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
1240 struct mmc_ioc_multi_cmd __user *user)
1241{
1242 struct mmc_blk_ioc_data **idata = NULL;
1243 struct mmc_ioc_cmd __user *cmds = user->cmds;
1244 struct mmc_card *card;
1245 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -07001246 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001247 __u64 num_of_cmds;
1248
Shawn Lin83c742c2016-03-16 18:15:47 +08001249 /*
1250 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1251 * whole block device, not on a partition. This prevents overspray
1252 * between sibling partitions.
1253 */
1254 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1255 return -EPERM;
1256
Jon Huntera5f57742015-09-22 10:27:53 +01001257 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
1258 sizeof(num_of_cmds)))
1259 return -EFAULT;
1260
1261 if (num_of_cmds > MMC_IOC_MAX_CMDS)
1262 return -EINVAL;
1263
1264 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
1265 if (!idata)
1266 return -ENOMEM;
1267
1268 for (i = 0; i < num_of_cmds; i++) {
1269 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
1270 if (IS_ERR(idata[i])) {
1271 err = PTR_ERR(idata[i]);
1272 num_of_cmds = i;
1273 goto cmd_err;
1274 }
1275 }
1276
1277 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -08001278 if (!md) {
1279 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +01001280 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -08001281 }
Jon Huntera5f57742015-09-22 10:27:53 +01001282
1283 card = md->queue.card;
1284 if (IS_ERR(card)) {
1285 err = PTR_ERR(card);
1286 goto cmd_done;
1287 }
1288
1289 mmc_get_card(card);
1290
Grant Grundlerb0934102015-09-23 18:30:33 -07001291 for (i = 0; i < num_of_cmds && !ioc_err; i++)
1292 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001293
Adrian Hunter3c866562016-05-04 14:38:12 +03001294 /* Always switch back to main area after RPMB access */
1295 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1296 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1297
Jon Huntera5f57742015-09-22 10:27:53 +01001298 mmc_put_card(card);
1299
1300 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -07001301 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +01001302 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001303
1304cmd_done:
1305 mmc_blk_put(md);
1306cmd_err:
1307 for (i = 0; i < num_of_cmds; i++) {
1308 kfree(idata[i]->buf);
1309 kfree(idata[i]);
1310 }
1311 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001312 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +01001313}
1314
John Calixtocb87ea22011-04-26 18:56:29 -04001315static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
1316 unsigned int cmd, unsigned long arg)
1317{
Jon Huntera5f57742015-09-22 10:27:53 +01001318 switch (cmd) {
1319 case MMC_IOC_CMD:
1320 return mmc_blk_ioctl_cmd(bdev,
1321 (struct mmc_ioc_cmd __user *)arg);
Krishna Kondae6711632014-12-04 15:20:57 +02001322 case MMC_IOC_RPMB_CMD:
1323 return mmc_blk_ioctl_rpmb_cmd(bdev,
1324 (struct mmc_ioc_rpmb __user *)arg);
Jon Huntera5f57742015-09-22 10:27:53 +01001325 case MMC_IOC_MULTI_CMD:
1326 return mmc_blk_ioctl_multi_cmd(bdev,
1327 (struct mmc_ioc_multi_cmd __user *)arg);
1328 default:
1329 return -EINVAL;
1330 }
John Calixtocb87ea22011-04-26 18:56:29 -04001331}
1332
1333#ifdef CONFIG_COMPAT
1334static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
1335 unsigned int cmd, unsigned long arg)
1336{
1337 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
1338}
1339#endif
1340
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001341static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -05001342 .open = mmc_blk_open,
1343 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001344 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -04001346 .ioctl = mmc_blk_ioctl,
1347#ifdef CONFIG_COMPAT
1348 .compat_ioctl = mmc_blk_compat_ioctl,
1349#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350};
1351
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001352static int mmc_blk_cmdq_switch(struct mmc_card *card,
1353 struct mmc_blk_data *md, bool enable)
1354{
1355 int ret = 0;
1356 bool cmdq_mode = !!mmc_card_cmdq(card);
Sahitya Tummalad0afd2f2015-05-20 12:15:35 +05301357 struct mmc_host *host = card->host;
1358 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001359
1360 if (!(card->host->caps2 & MMC_CAP2_CMD_QUEUE) ||
1361 !card->ext_csd.cmdq_support ||
1362 (enable && !(md->flags & MMC_BLK_CMD_QUEUE)) ||
1363 (cmdq_mode == enable))
1364 return 0;
1365
1366 if (enable) {
1367 ret = mmc_set_blocklen(card, MMC_CARD_CMDQ_BLK_SIZE);
1368 if (ret) {
1369 pr_err("%s: failed (%d) to set block-size to %d\n",
1370 __func__, ret, MMC_CARD_CMDQ_BLK_SIZE);
1371 goto out;
1372 }
Sahitya Tummalad0afd2f2015-05-20 12:15:35 +05301373
1374 } else {
1375 if (!test_bit(CMDQ_STATE_HALT, &ctx->curr_state)) {
1376 ret = mmc_cmdq_halt(host, true);
1377 if (ret) {
1378 pr_err("%s: halt: failed: %d\n",
1379 mmc_hostname(host), ret);
1380 goto out;
1381 }
1382 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001383 }
1384
1385 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1386 EXT_CSD_CMDQ, enable,
1387 card->ext_csd.generic_cmd6_time);
1388 if (ret) {
1389 pr_err("%s: cmdq mode %sable failed %d\n",
1390 md->disk->disk_name, enable ? "en" : "dis", ret);
1391 goto out;
1392 }
1393
1394 if (enable)
1395 mmc_card_set_cmdq(card);
1396 else
1397 mmc_card_clr_cmdq(card);
1398out:
1399 return ret;
1400}
1401
Andrei Warkentin371a6892011-04-11 18:10:25 -05001402static inline int mmc_blk_part_switch(struct mmc_card *card,
1403 struct mmc_blk_data *md)
1404{
1405 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001406 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001407
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001408 if ((main_md->part_curr == md->part_type) &&
1409 (card->part_curr == md->part_type))
Andrei Warkentin371a6892011-04-11 18:10:25 -05001410 return 0;
1411
1412 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001413 u8 part_config = card->ext_csd.part_config;
1414
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001415 if (md->part_type) {
1416 /* disable CQ mode for non-user data partitions */
1417 ret = mmc_blk_cmdq_switch(card, md, false);
1418 if (ret)
1419 return ret;
1420 }
1421
Adrian Hunter57da0c02016-05-04 14:38:13 +03001422 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1423 mmc_retune_pause(card->host);
1424
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001425 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1426 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001427
1428 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001429 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001430 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001431 if (ret) {
1432 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1433 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001434 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001435 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001436
1437 card->ext_csd.part_config = part_config;
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001438 card->part_curr = md->part_type;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001439
1440 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1441 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001442 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001443
1444 main_md->part_curr = md->part_type;
1445 return 0;
1446}
1447
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001448static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1449{
1450 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001451 u32 result;
1452 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001453
Venkatraman Sad5fd972011-08-25 00:30:50 +05301454 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001455 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001456 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001457
1458 struct scatterlist sg;
1459
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001460 cmd.opcode = MMC_APP_CMD;
1461 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001462 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001463
1464 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001465 if (err)
1466 return (u32)-1;
1467 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001468 return (u32)-1;
1469
1470 memset(&cmd, 0, sizeof(struct mmc_command));
1471
1472 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1473 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001474 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001475
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001476 data.blksz = 4;
1477 data.blocks = 1;
1478 data.flags = MMC_DATA_READ;
1479 data.sg = &sg;
1480 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301481 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001482
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001483 mrq.cmd = &cmd;
1484 mrq.data = &data;
1485
Ben Dooks051913d2009-06-08 23:33:57 +01001486 blocks = kmalloc(4, GFP_KERNEL);
1487 if (!blocks)
1488 return (u32)-1;
1489
1490 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001491
1492 mmc_wait_for_req(card->host, &mrq);
1493
Ben Dooks051913d2009-06-08 23:33:57 +01001494 result = ntohl(*blocks);
1495 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001496
Ben Dooks051913d2009-06-08 23:33:57 +01001497 if (cmd.error || data.error)
1498 result = (u32)-1;
1499
1500 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001501}
1502
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001503static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001504{
Chris Ball1278dba2011-04-13 23:40:30 -04001505 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001506 int err;
1507
Adrian Hunter504f1912008-10-16 12:55:25 +03001508 cmd.opcode = MMC_SEND_STATUS;
1509 if (!mmc_host_is_spi(card->host))
1510 cmd.arg = card->rca << 16;
1511 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001512 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1513 if (err == 0)
1514 *status = cmd.resp[0];
1515 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001516}
1517
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001518static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001519 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001520{
1521 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1522 int err = 0;
1523 u32 status;
1524
1525 do {
1526 err = get_card_status(card, &status, 5);
1527 if (err) {
1528 pr_err("%s: error %d requesting status\n",
1529 req->rq_disk->disk_name, err);
1530 return err;
1531 }
1532
1533 if (status & R1_ERROR) {
1534 pr_err("%s: %s: error sending status cmd, status %#x\n",
1535 req->rq_disk->disk_name, __func__, status);
1536 *gen_err = 1;
1537 }
1538
Ulf Hansson95a91292014-01-29 13:11:27 +01001539 /* We may rely on the host hw to handle busy detection.*/
1540 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1541 hw_busy_detect)
1542 break;
1543
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001544 /*
1545 * Timeout if the device never becomes ready for data and never
1546 * leaves the program state.
1547 */
1548 if (time_after(jiffies, timeout)) {
1549 pr_err("%s: Card stuck in programming state! %s %s\n",
1550 mmc_hostname(card->host),
1551 req->rq_disk->disk_name, __func__);
1552 return -ETIMEDOUT;
1553 }
1554
1555 /*
1556 * Some cards mishandle the status bits,
1557 * so make sure to check both the busy
1558 * indication and the card state.
1559 */
1560 } while (!(status & R1_READY_FOR_DATA) ||
1561 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1562
1563 return err;
1564}
1565
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001566static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1567 struct request *req, int *gen_err, u32 *stop_status)
1568{
1569 struct mmc_host *host = card->host;
1570 struct mmc_command cmd = {0};
1571 int err;
1572 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1573
1574 /*
1575 * Normally we use R1B responses for WRITE, but in cases where the host
1576 * has specified a max_busy_timeout we need to validate it. A failure
1577 * means we need to prevent the host from doing hw busy detection, which
1578 * is done by converting to a R1 response instead.
1579 */
1580 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1581 use_r1b_resp = false;
1582
1583 cmd.opcode = MMC_STOP_TRANSMISSION;
1584 if (use_r1b_resp) {
1585 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1586 cmd.busy_timeout = timeout_ms;
1587 } else {
1588 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1589 }
1590
1591 err = mmc_wait_for_cmd(host, &cmd, 5);
1592 if (err)
1593 return err;
1594
1595 *stop_status = cmd.resp[0];
1596
1597 /* No need to check card status in case of READ. */
1598 if (rq_data_dir(req) == READ)
1599 return 0;
1600
1601 if (!mmc_host_is_spi(host) &&
1602 (*stop_status & R1_ERROR)) {
1603 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1604 req->rq_disk->disk_name, __func__, *stop_status);
1605 *gen_err = 1;
1606 }
1607
1608 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1609}
1610
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301611#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001612#define ERR_RETRY 2
1613#define ERR_ABORT 1
1614#define ERR_CONTINUE 0
1615
1616static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1617 bool status_valid, u32 status)
1618{
1619 switch (error) {
1620 case -EILSEQ:
1621 /* response crc error, retry the r/w cmd */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001622 pr_err_ratelimited(
1623 "%s: response CRC error sending %s command, card status %#x\n",
1624 req->rq_disk->disk_name,
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001625 name, status);
1626 return ERR_RETRY;
1627
1628 case -ETIMEDOUT:
Talel Shenhar0821fe852015-01-28 14:44:57 +02001629 pr_err_ratelimited(
1630 "%s: timed out sending %s command, card status %#x\n",
1631 req->rq_disk->disk_name, name, status);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001632
1633 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301634 if (!status_valid) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001635 pr_err_ratelimited("%s: status not valid, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301636 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001637 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301638 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001639
1640 /*
1641 * If it was a r/w cmd crc error, or illegal command
1642 * (eg, issued in wrong state) then retry - we should
1643 * have corrected the state problem above.
1644 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301645 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001646 pr_err_ratelimited(
1647 "%s: command error, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301648 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001649 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301650 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001651
1652 /* Otherwise abort the command */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001653 pr_err_ratelimited(
1654 "%s: not retrying timeout\n",
1655 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001656 return ERR_ABORT;
1657
1658 default:
1659 /* We don't understand the error code the driver gave us */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001660 pr_err_ratelimited(
1661 "%s: unknown error %d sending read/write command, card status %#x\n",
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001662 req->rq_disk->disk_name, error, status);
1663 return ERR_ABORT;
1664 }
1665}
1666
1667/*
1668 * Initial r/w and stop cmd error recovery.
1669 * We don't know whether the card received the r/w cmd or not, so try to
1670 * restore things back to a sane state. Essentially, we do this as follows:
1671 * - Obtain card status. If the first attempt to obtain card status fails,
1672 * the status word will reflect the failed status cmd, not the failed
1673 * r/w cmd. If we fail to obtain card status, it suggests we can no
1674 * longer communicate with the card.
1675 * - Check the card state. If the card received the cmd but there was a
1676 * transient problem with the response, it might still be in a data transfer
1677 * mode. Try to send it a stop command. If this fails, we can't recover.
1678 * - If the r/w cmd failed due to a response CRC error, it was probably
1679 * transient, so retry the cmd.
1680 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1681 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1682 * illegal cmd, retry.
1683 * Otherwise we don't understand what happened, so abort.
1684 */
1685static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001686 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001687{
1688 bool prev_cmd_status_valid = true;
1689 u32 status, stop_status = 0;
1690 int err, retry;
1691
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301692 if (mmc_card_removed(card))
1693 return ERR_NOMEDIUM;
1694
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001695 /*
1696 * Try to get card status which indicates both the card state
1697 * and why there was no response. If the first attempt fails,
1698 * we can't be sure the returned status is for the r/w command.
1699 */
1700 for (retry = 2; retry >= 0; retry--) {
1701 err = get_card_status(card, &status, 0);
1702 if (!err)
1703 break;
1704
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001705 /* Re-tune if needed */
1706 mmc_retune_recheck(card->host);
1707
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001708 prev_cmd_status_valid = false;
1709 pr_err("%s: error %d sending status command, %sing\n",
1710 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1711 }
1712
1713 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301714 if (err) {
1715 /* Check if the card is removed */
1716 if (mmc_detect_card_removed(card->host))
1717 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001718 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301719 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001720
Adrian Hunter67716322011-08-29 16:42:15 +03001721 /* Flag ECC errors */
1722 if ((status & R1_CARD_ECC_FAILED) ||
1723 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1724 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1725 *ecc_err = 1;
1726
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001727 /* Flag General errors */
1728 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1729 if ((status & R1_ERROR) ||
1730 (brq->stop.resp[0] & R1_ERROR)) {
1731 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1732 req->rq_disk->disk_name, __func__,
1733 brq->stop.resp[0], status);
1734 *gen_err = 1;
1735 }
1736
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001737 /*
1738 * Check the current card state. If it is in some data transfer
1739 * mode, tell it to stop (and hopefully transition back to TRAN.)
1740 */
1741 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1742 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001743 err = send_stop(card,
1744 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1745 req, gen_err, &stop_status);
1746 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001747 pr_err("%s: error %d sending stop command\n",
1748 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001749 /*
1750 * If the stop cmd also timed out, the card is probably
1751 * not present, so abort. Other errors are bad news too.
1752 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001753 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001754 }
1755
Adrian Hunter67716322011-08-29 16:42:15 +03001756 if (stop_status & R1_CARD_ECC_FAILED)
1757 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001758 }
1759
1760 /* Check for set block count errors */
1761 if (brq->sbc.error)
1762 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1763 prev_cmd_status_valid, status);
1764
1765 /* Check for r/w command errors */
1766 if (brq->cmd.error)
1767 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1768 prev_cmd_status_valid, status);
1769
Adrian Hunter67716322011-08-29 16:42:15 +03001770 /* Data errors */
1771 if (!brq->stop.error)
1772 return ERR_CONTINUE;
1773
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001774 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001775 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 +01001776 req->rq_disk->disk_name, brq->stop.error,
1777 brq->cmd.resp[0], status);
1778
1779 /*
1780 * Subsitute in our own stop status as this will give the error
1781 * state which happened during the execution of the r/w command.
1782 */
1783 if (stop_status) {
1784 brq->stop.resp[0] = stop_status;
1785 brq->stop.error = 0;
1786 }
1787 return ERR_CONTINUE;
1788}
1789
Adrian Hunter67716322011-08-29 16:42:15 +03001790static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1791 int type)
1792{
1793 int err;
1794
1795 if (md->reset_done & type)
1796 return -EEXIST;
1797
1798 md->reset_done |= type;
1799 err = mmc_hw_reset(host);
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301800 if (err && err != -EOPNOTSUPP) {
1801 /* We failed to reset so we need to abort the request */
1802 pr_err("%s: %s: failed to reset %d\n", mmc_hostname(host),
1803 __func__, err);
1804 return -ENODEV;
1805 }
1806
Adrian Hunter67716322011-08-29 16:42:15 +03001807 /* Ensure we switch back to the correct partition */
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301808 if (host->card) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001809 struct mmc_blk_data *main_md =
1810 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001811 int part_err;
1812
1813 main_md->part_curr = main_md->part_type;
1814 part_err = mmc_blk_part_switch(host->card, md);
1815 if (part_err) {
1816 /*
1817 * We have failed to get back into the correct
1818 * partition, so we need to abort the whole request.
1819 */
1820 return -ENODEV;
1821 }
1822 }
1823 return err;
1824}
1825
1826static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1827{
1828 md->reset_done &= ~type;
1829}
1830
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001831int mmc_access_rpmb(struct mmc_queue *mq)
1832{
1833 struct mmc_blk_data *md = mq->data;
1834 /*
1835 * If this is a RPMB partition access, return ture
1836 */
1837 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1838 return true;
1839
1840 return false;
1841}
1842
Sahitya Tummala9433a132015-06-09 09:38:36 +05301843static struct mmc_cmdq_req *mmc_blk_cmdq_prep_discard_req(struct mmc_queue *mq,
1844 struct request *req)
1845{
1846 struct mmc_blk_data *md = mq->data;
1847 struct mmc_card *card = md->queue.card;
1848 struct mmc_host *host = card->host;
1849 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1850 struct mmc_cmdq_req *cmdq_req;
1851 struct mmc_queue_req *active_mqrq;
1852
1853 BUG_ON(req->tag > card->ext_csd.cmdq_depth);
1854 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
1855
1856 set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
1857
1858 active_mqrq = &mq->mqrq_cmdq[req->tag];
1859 active_mqrq->req = req;
1860
1861 cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
1862 cmdq_req->cmdq_req_flags |= QBR;
1863 cmdq_req->mrq.cmd = &cmdq_req->cmd;
1864 cmdq_req->tag = req->tag;
1865 return cmdq_req;
1866}
1867
1868static int mmc_blk_cmdq_issue_discard_rq(struct mmc_queue *mq,
1869 struct request *req)
1870{
1871 struct mmc_blk_data *md = mq->data;
1872 struct mmc_card *card = md->queue.card;
1873 struct mmc_cmdq_req *cmdq_req = NULL;
1874 struct mmc_host *host = card->host;
1875 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1876 unsigned int from, nr, arg;
1877 int err = 0;
1878
1879 if (!mmc_can_erase(card)) {
1880 err = -EOPNOTSUPP;
1881 goto out;
1882 }
1883
1884 from = blk_rq_pos(req);
1885 nr = blk_rq_sectors(req);
1886
1887 if (mmc_can_discard(card))
1888 arg = MMC_DISCARD_ARG;
1889 else if (mmc_can_trim(card))
1890 arg = MMC_TRIM_ARG;
1891 else
1892 arg = MMC_ERASE_ARG;
1893
1894 cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
1895 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1896 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1897 EXT_CSD_CMD_SET_NORMAL,
1898 INAND_CMD38_ARG_EXT_CSD,
1899 arg == MMC_TRIM_ARG ?
1900 INAND_CMD38_ARG_TRIM :
1901 INAND_CMD38_ARG_ERASE,
1902 0, true, false);
1903 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
1904 if (err)
1905 goto clear_dcmd;
1906 }
1907 err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
1908clear_dcmd:
1909 /* clear pending request */
1910 if (cmdq_req) {
1911 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
1912 &ctx_info->active_reqs));
1913 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
1914 }
1915out:
1916 blk_end_request(req, err, blk_rq_bytes(req));
1917
1918 if (test_and_clear_bit(0, &ctx_info->req_starved))
1919 blk_run_queue(mq->queue);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03001920 mmc_put_card(card);
Sahitya Tummala9433a132015-06-09 09:38:36 +05301921 return err ? 1 : 0;
1922}
1923
Adrian Hunterbd788c92010-08-11 14:17:47 -07001924static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1925{
1926 struct mmc_blk_data *md = mq->data;
1927 struct mmc_card *card = md->queue.card;
1928 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001929 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001930
Adrian Hunterbd788c92010-08-11 14:17:47 -07001931 if (!mmc_can_erase(card)) {
1932 err = -EOPNOTSUPP;
1933 goto out;
1934 }
1935
1936 from = blk_rq_pos(req);
1937 nr = blk_rq_sectors(req);
1938
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001939 if (mmc_can_discard(card))
1940 arg = MMC_DISCARD_ARG;
1941 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001942 arg = MMC_TRIM_ARG;
1943 else
1944 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001945retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001946 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1947 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1948 INAND_CMD38_ARG_EXT_CSD,
1949 arg == MMC_TRIM_ARG ?
1950 INAND_CMD38_ARG_TRIM :
1951 INAND_CMD38_ARG_ERASE,
1952 0);
1953 if (err)
1954 goto out;
1955 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001956 err = mmc_erase(card, from, nr, arg);
1957out:
Adrian Hunter67716322011-08-29 16:42:15 +03001958 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1959 goto retry;
1960 if (!err)
1961 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301962 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001963
Adrian Hunterbd788c92010-08-11 14:17:47 -07001964 return err ? 0 : 1;
1965}
1966
Sahitya Tummala9433a132015-06-09 09:38:36 +05301967static int mmc_blk_cmdq_issue_secdiscard_rq(struct mmc_queue *mq,
1968 struct request *req)
1969{
1970 struct mmc_blk_data *md = mq->data;
1971 struct mmc_card *card = md->queue.card;
1972 struct mmc_cmdq_req *cmdq_req = NULL;
1973 unsigned int from, nr, arg;
1974 struct mmc_host *host = card->host;
1975 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
1976 int err = 0;
1977
1978 if (!(mmc_can_secure_erase_trim(card))) {
1979 err = -EOPNOTSUPP;
1980 goto out;
1981 }
1982
1983 from = blk_rq_pos(req);
1984 nr = blk_rq_sectors(req);
1985
1986 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1987 arg = MMC_SECURE_TRIM1_ARG;
1988 else
1989 arg = MMC_SECURE_ERASE_ARG;
1990
1991 cmdq_req = mmc_blk_cmdq_prep_discard_req(mq, req);
1992 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1993 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
1994 EXT_CSD_CMD_SET_NORMAL,
1995 INAND_CMD38_ARG_EXT_CSD,
1996 arg == MMC_SECURE_TRIM1_ARG ?
1997 INAND_CMD38_ARG_SECTRIM1 :
1998 INAND_CMD38_ARG_SECERASE,
1999 0, true, false);
2000 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
2001 if (err)
2002 goto clear_dcmd;
2003 }
2004
2005 err = mmc_cmdq_erase(cmdq_req, card, from, nr, arg);
2006 if (err)
2007 goto clear_dcmd;
2008
2009 if (arg == MMC_SECURE_TRIM1_ARG) {
2010 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2011 __mmc_switch_cmdq_mode(cmdq_req->mrq.cmd,
2012 EXT_CSD_CMD_SET_NORMAL,
2013 INAND_CMD38_ARG_EXT_CSD,
2014 INAND_CMD38_ARG_SECTRIM2,
2015 0, true, false);
2016 err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req);
2017 if (err)
2018 goto clear_dcmd;
2019 }
2020
2021 err = mmc_cmdq_erase(cmdq_req, card, from, nr,
2022 MMC_SECURE_TRIM2_ARG);
2023 }
2024clear_dcmd:
2025 /* clear pending request */
2026 if (cmdq_req) {
2027 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
2028 &ctx_info->active_reqs));
2029 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
2030 }
2031out:
2032 blk_end_request(req, err, blk_rq_bytes(req));
2033
2034 if (test_and_clear_bit(0, &ctx_info->req_starved))
2035 blk_run_queue(mq->queue);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03002036 mmc_put_card(card);
Sahitya Tummala9433a132015-06-09 09:38:36 +05302037 return err ? 1 : 0;
2038}
2039
Adrian Hunter49804542010-08-11 14:17:50 -07002040static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
2041 struct request *req)
2042{
2043 struct mmc_blk_data *md = mq->data;
2044 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03002045 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03002046 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07002047
Maya Erez775a9362013-04-18 15:41:55 +03002048 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07002049 err = -EOPNOTSUPP;
2050 goto out;
2051 }
2052
2053 from = blk_rq_pos(req);
2054 nr = blk_rq_sectors(req);
2055
Maya Erez775a9362013-04-18 15:41:55 +03002056 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
2057 arg = MMC_SECURE_TRIM1_ARG;
2058 else
2059 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03002060
Adrian Hunter67716322011-08-29 16:42:15 +03002061retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002062 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2063 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2064 INAND_CMD38_ARG_EXT_CSD,
2065 arg == MMC_SECURE_TRIM1_ARG ?
2066 INAND_CMD38_ARG_SECTRIM1 :
2067 INAND_CMD38_ARG_SECERASE,
2068 0);
2069 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03002070 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002071 }
Adrian Hunter28302812012-04-05 14:45:48 +03002072
Adrian Hunter49804542010-08-11 14:17:50 -07002073 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03002074 if (err == -EIO)
2075 goto out_retry;
2076 if (err)
2077 goto out;
2078
2079 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002080 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
2081 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2082 INAND_CMD38_ARG_EXT_CSD,
2083 INAND_CMD38_ARG_SECTRIM2,
2084 0);
2085 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03002086 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002087 }
Adrian Hunter28302812012-04-05 14:45:48 +03002088
Adrian Hunter49804542010-08-11 14:17:50 -07002089 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03002090 if (err == -EIO)
2091 goto out_retry;
2092 if (err)
2093 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05002094 }
Adrian Hunter28302812012-04-05 14:45:48 +03002095
Adrian Hunter28302812012-04-05 14:45:48 +03002096out_retry:
2097 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03002098 goto retry;
2099 if (!err)
2100 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03002101out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302102 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07002103
Adrian Hunter49804542010-08-11 14:17:50 -07002104 return err ? 0 : 1;
2105}
2106
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002107static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
2108{
2109 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002110 struct mmc_card *card = md->queue.card;
2111 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002112
Sahitya Tummala61868a42015-05-28 16:54:19 +05302113 if (!req)
2114 return 0;
2115
2116 if (req->cmd_flags & REQ_BARRIER) {
2117 /*
2118 * If eMMC cache flush policy is set to 1, then the device
2119 * shall flush the requests in First-In-First-Out (FIFO) order.
2120 * In this case, as per spec, the host must not send any cache
2121 * barrier requests as they are redundant and add unnecessary
2122 * overhead to both device and host.
2123 */
2124 if (card->ext_csd.cache_flush_policy & 1)
2125 goto end_req;
2126
2127 /*
2128 * In case barrier is not supported or enabled in the device,
2129 * use flush as a fallback option.
2130 */
2131 ret = mmc_cache_barrier(card);
2132 if (ret)
2133 ret = mmc_flush_cache(card);
2134 } else if (req_op(req) == REQ_OP_FLUSH) {
2135 ret = mmc_flush_cache(card);
2136 }
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02002137 if (ret == -ENODEV) {
2138 pr_err("%s: %s: restart mmc card",
2139 req->rq_disk->disk_name, __func__);
2140 if (mmc_blk_reset(md, card->host, MMC_BLK_FLUSH))
2141 pr_err("%s: %s: fail to restart mmc",
2142 req->rq_disk->disk_name, __func__);
2143 else
2144 mmc_blk_reset_success(md, MMC_BLK_FLUSH);
2145 }
2146
2147 if (ret) {
2148 pr_err("%s: %s: notify flush error to upper layers",
2149 req->rq_disk->disk_name, __func__);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002150 ret = -EIO;
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02002151 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002152
Mark Salyzyn6904e432016-01-28 11:12:25 -08002153#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2154 else if (atomic_read(&mq->cache_size)) {
2155 long used = mmc_blk_cache_used(mq, jiffies);
2156
2157 if (used) {
2158 int speed = atomic_read(&mq->max_write_speed);
2159
2160 if (speed_valid(speed)) {
2161 unsigned long msecs = jiffies_to_msecs(
2162 size_and_speed_to_jiffies(
2163 used, speed));
2164 if (msecs)
2165 msleep(msecs);
2166 }
2167 }
2168 }
2169#endif
Sahitya Tummala61868a42015-05-28 16:54:19 +05302170end_req:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302171 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002172
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002173 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002174}
2175
2176/*
2177 * Reformat current write as a reliable write, supporting
2178 * both legacy and the enhanced reliable write MMC cards.
2179 * In each transfer we'll handle only as much as a single
2180 * reliable write can handle, thus finish the request in
2181 * partial completions.
2182 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002183static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
2184 struct mmc_card *card,
2185 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002186{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002187 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
2188 /* Legacy mode imposes restrictions on transfers. */
2189 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
2190 brq->data.blocks = 1;
2191
2192 if (brq->data.blocks > card->ext_csd.rel_sectors)
2193 brq->data.blocks = card->ext_csd.rel_sectors;
2194 else if (brq->data.blocks < card->ext_csd.rel_sectors)
2195 brq->data.blocks = 1;
2196 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002197}
2198
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002199#define CMD_ERRORS \
2200 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
2201 R1_ADDRESS_ERROR | /* Misaligned address */ \
2202 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
2203 R1_WP_VIOLATION | /* Tried to write to protected block */ \
2204 R1_CC_ERROR | /* Card controller error */ \
2205 R1_ERROR) /* General/unknown error */
2206
Per Forlinee8a43a2011-07-01 18:55:33 +02002207static int mmc_blk_err_check(struct mmc_card *card,
2208 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02002209{
Per Forlinee8a43a2011-07-01 18:55:33 +02002210 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
2211 mmc_active);
2212 struct mmc_blk_request *brq = &mq_mrq->brq;
2213 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002214 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002215 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002216
2217 /*
2218 * sbc.error indicates a problem with the set block count
2219 * command. No data will have been transferred.
2220 *
2221 * cmd.error indicates a problem with the r/w command. No
2222 * data will have been transferred.
2223 *
2224 * stop.error indicates a problem with the stop command. Data
2225 * may have been transferred, or may still be transferring.
2226 */
Adrian Hunter67716322011-08-29 16:42:15 +03002227 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
2228 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002229 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02002230 case ERR_RETRY:
2231 return MMC_BLK_RETRY;
2232 case ERR_ABORT:
2233 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302234 case ERR_NOMEDIUM:
2235 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02002236 case ERR_CONTINUE:
2237 break;
2238 }
2239 }
2240
2241 /*
2242 * Check for errors relating to the execution of the
2243 * initial command - such as address errors. No data
2244 * has been transferred.
2245 */
2246 if (brq->cmd.resp[0] & CMD_ERRORS) {
2247 pr_err("%s: r/w command failed, status = %#x\n",
2248 req->rq_disk->disk_name, brq->cmd.resp[0]);
2249 return MMC_BLK_ABORT;
2250 }
2251
2252 /*
2253 * Everything else is either success, or a data error of some
2254 * kind. If it was a write, we may have transitioned to
2255 * program mode, which we have to wait for it to complete.
2256 */
2257 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002258 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06002259
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002260 /* Check stop command response */
2261 if (brq->stop.resp[0] & R1_ERROR) {
2262 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
2263 req->rq_disk->disk_name, __func__,
2264 brq->stop.resp[0]);
2265 gen_err = 1;
2266 }
2267
Ulf Hansson95a91292014-01-29 13:11:27 +01002268 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
2269 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002270 if (err)
2271 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002272 }
2273
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002274 /* if general error occurs, retry the write operation. */
2275 if (gen_err) {
2276 pr_warn("%s: retrying write for general error\n",
2277 req->rq_disk->disk_name);
2278 return MMC_BLK_RETRY;
2279 }
2280
Per Forlind78d4a82011-07-01 18:55:30 +02002281 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03002282 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00002283 pr_debug("%s: retrying because a re-tune was needed\n",
2284 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03002285 brq->retune_retry_done = 1;
2286 return MMC_BLK_RETRY;
2287 }
Per Forlind78d4a82011-07-01 18:55:30 +02002288 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
2289 req->rq_disk->disk_name, brq->data.error,
2290 (unsigned)blk_rq_pos(req),
2291 (unsigned)blk_rq_sectors(req),
2292 brq->cmd.resp[0], brq->stop.resp[0]);
2293
2294 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03002295 if (ecc_err)
2296 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002297 return MMC_BLK_DATA_ERR;
2298 } else {
2299 return MMC_BLK_CMD_ERR;
2300 }
2301 }
2302
Adrian Hunter67716322011-08-29 16:42:15 +03002303 if (!brq->data.bytes_xfered)
2304 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02002305
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002306 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
2307 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
2308 return MMC_BLK_PARTIAL;
2309 else
2310 return MMC_BLK_SUCCESS;
2311 }
2312
Adrian Hunter67716322011-08-29 16:42:15 +03002313 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
2314 return MMC_BLK_PARTIAL;
2315
2316 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02002317}
2318
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002319static int mmc_blk_packed_err_check(struct mmc_card *card,
2320 struct mmc_async_req *areq)
2321{
2322 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
2323 mmc_active);
2324 struct request *req = mq_rq->req;
2325 struct mmc_packed *packed = mq_rq->packed;
2326 int err, check, status;
2327 u8 *ext_csd;
2328
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002329 packed->retries--;
2330 check = mmc_blk_err_check(card, areq);
2331 err = get_card_status(card, &status, 0);
2332 if (err) {
2333 pr_err("%s: error %d sending status command\n",
2334 req->rq_disk->disk_name, err);
2335 return MMC_BLK_ABORT;
2336 }
2337
2338 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02002339 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002340 if (err) {
2341 pr_err("%s: error %d sending ext_csd\n",
2342 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02002343 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002344 }
2345
2346 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
2347 EXT_CSD_PACKED_FAILURE) &&
2348 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2349 EXT_CSD_PACKED_GENERIC_ERROR)) {
2350 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2351 EXT_CSD_PACKED_INDEXED_ERROR) {
2352 packed->idx_failure =
2353 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
2354 check = MMC_BLK_PARTIAL;
2355 }
2356 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
2357 "failure index: %d\n",
2358 req->rq_disk->disk_name, packed->nr_entries,
2359 packed->blocks, packed->idx_failure);
2360 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002361 kfree(ext_csd);
2362 }
2363
2364 return check;
2365}
2366
Per Forlin54d49d72011-07-01 18:55:29 +02002367static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
2368 struct mmc_card *card,
2369 int disable_multi,
2370 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
Per Forlin54d49d72011-07-01 18:55:29 +02002372 u32 readcmd, writecmd;
2373 struct mmc_blk_request *brq = &mqrq->brq;
2374 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05302376 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002378 /*
2379 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00002380 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002381 */
Luca Porziod3df0462015-11-06 15:12:26 +00002382 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002383 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002384 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002385
Per Forlin54d49d72011-07-01 18:55:29 +02002386 memset(brq, 0, sizeof(struct mmc_blk_request));
2387 brq->mrq.cmd = &brq->cmd;
2388 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Per Forlin54d49d72011-07-01 18:55:29 +02002390 brq->cmd.arg = blk_rq_pos(req);
2391 if (!mmc_card_blockaddr(card))
2392 brq->cmd.arg <<= 9;
2393 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2394 brq->data.blksz = 512;
2395 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2396 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002397 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Asutosh Dasf0665412012-07-27 18:10:19 +05302399 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02002400 /*
2401 * The block layer doesn't support all sector count
2402 * restrictions, so we need to be prepared for too big
2403 * requests.
2404 */
2405 if (brq->data.blocks > card->host->max_blk_count)
2406 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002408 if (brq->data.blocks > 1) {
2409 /*
2410 * After a read error, we redo the request one sector
2411 * at a time in order to accurately determine which
2412 * sectors can be read successfully.
2413 */
2414 if (disable_multi)
2415 brq->data.blocks = 1;
2416
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07002417 /*
2418 * Some controllers have HW issues while operating
2419 * in multiple I/O mode
2420 */
2421 if (card->host->ops->multi_io_quirk)
2422 brq->data.blocks = card->host->ops->multi_io_quirk(card,
2423 (rq_data_dir(req) == READ) ?
2424 MMC_DATA_READ : MMC_DATA_WRITE,
2425 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002426 }
Per Forlin54d49d72011-07-01 18:55:29 +02002427
2428 if (brq->data.blocks > 1 || do_rel_wr) {
2429 /* SPI multiblock writes terminate using a special
2430 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02002431 */
Per Forlin54d49d72011-07-01 18:55:29 +02002432 if (!mmc_host_is_spi(card->host) ||
2433 rq_data_dir(req) == READ)
2434 brq->mrq.stop = &brq->stop;
2435 readcmd = MMC_READ_MULTIPLE_BLOCK;
2436 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
2437 } else {
2438 brq->mrq.stop = NULL;
2439 readcmd = MMC_READ_SINGLE_BLOCK;
2440 writecmd = MMC_WRITE_BLOCK;
2441 }
2442 if (rq_data_dir(req) == READ) {
2443 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002444 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002445 if (brq->mrq.stop)
2446 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
2447 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002448 } else {
2449 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002450 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002451 if (brq->mrq.stop)
2452 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
2453 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002454 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02002455
Per Forlin54d49d72011-07-01 18:55:29 +02002456 if (do_rel_wr)
2457 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01002458
Per Forlin54d49d72011-07-01 18:55:29 +02002459 /*
Saugata Das42659002011-12-21 13:09:17 +05302460 * Data tag is used only during writing meta data to speed
2461 * up write and any subsequent read of this meta data
2462 */
2463 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2464 (req->cmd_flags & REQ_META) &&
2465 (rq_data_dir(req) == WRITE) &&
2466 ((brq->data.blocks * brq->data.blksz) >=
2467 card->ext_csd.data_tag_unit_size);
2468
2469 /*
Per Forlin54d49d72011-07-01 18:55:29 +02002470 * Pre-defined multi-block transfers are preferable to
2471 * open ended-ones (and necessary for reliable writes).
2472 * However, it is not sufficient to just send CMD23,
2473 * and avoid the final CMD12, as on an error condition
2474 * CMD12 (stop) needs to be sent anyway. This, coupled
2475 * with Auto-CMD23 enhancements provided by some
2476 * hosts, means that the complexity of dealing
2477 * with this is best left to the host. If CMD23 is
2478 * supported by card and host, we'll fill sbc in and let
2479 * the host deal with handling it correctly. This means
2480 * that for hosts that don't expose MMC_CAP_CMD23, no
2481 * change of behavior will be observed.
2482 *
2483 * N.B: Some MMC cards experience perf degradation.
2484 * We'll avoid using CMD23-bounded multiblock writes for
2485 * these, while retaining features like reliable writes.
2486 */
Saugata Das42659002011-12-21 13:09:17 +05302487 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
2488 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
2489 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02002490 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2491 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05302492 (do_rel_wr ? (1 << 31) : 0) |
2493 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02002494 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2495 brq->mrq.sbc = &brq->sbc;
2496 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002497
Per Forlin54d49d72011-07-01 18:55:29 +02002498 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002499
Per Forlin54d49d72011-07-01 18:55:29 +02002500 brq->data.sg = mqrq->sg;
2501 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002502
Per Forlin54d49d72011-07-01 18:55:29 +02002503 /*
2504 * Adjust the sg list so it is the same size as the
2505 * request.
2506 */
2507 if (brq->data.blocks != blk_rq_sectors(req)) {
2508 int i, data_size = brq->data.blocks << 9;
2509 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02002510
Per Forlin54d49d72011-07-01 18:55:29 +02002511 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
2512 data_size -= sg->length;
2513 if (data_size <= 0) {
2514 sg->length += data_size;
2515 i++;
2516 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01002517 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002518 }
Per Forlin54d49d72011-07-01 18:55:29 +02002519 brq->data.sg_len = i;
2520 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002521
Per Forlinee8a43a2011-07-01 18:55:33 +02002522 mqrq->mmc_active.mrq = &brq->mrq;
Sahitya Tummalac44de842015-05-08 11:12:30 +05302523 mqrq->mmc_active.mrq->req = mqrq->req;
Per Forlinee8a43a2011-07-01 18:55:33 +02002524 mqrq->mmc_active.err_check = mmc_blk_err_check;
2525
Per Forlin54d49d72011-07-01 18:55:29 +02002526 mmc_queue_bounce_pre(mqrq);
2527}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002529static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
2530 struct mmc_card *card)
2531{
2532 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
2533 unsigned int max_seg_sz = queue_max_segment_size(q);
2534 unsigned int len, nr_segs = 0;
2535
2536 do {
2537 len = min(hdr_sz, max_seg_sz);
2538 hdr_sz -= len;
2539 nr_segs++;
2540 } while (hdr_sz);
2541
2542 return nr_segs;
2543}
2544
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002545/**
2546 * mmc_blk_disable_wr_packing() - disables packing mode
2547 * @mq: MMC queue.
2548 *
2549 */
2550void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
2551{
2552 if (mq) {
2553 mq->wr_packing_enabled = false;
2554 mq->num_of_potential_packed_wr_reqs = 0;
2555 }
2556}
2557EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
2558
Lee Susman841fd132013-04-23 17:59:26 +03002559static int get_packed_trigger(int potential, struct mmc_card *card,
2560 struct request *req, int curr_trigger)
2561{
2562 static int num_mean_elements = 1;
2563 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2564 unsigned int trigger = curr_trigger;
2565 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
2566
2567 /* scale down the upper bound to 75% */
2568 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
2569
2570 /*
2571 * since the most common calls for this function are with small
2572 * potential write values and since we don't want these calls to affect
2573 * the packed trigger, set a lower bound and ignore calls with
2574 * potential lower than that bound
2575 */
2576 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
2577 return trigger;
2578
2579 /*
2580 * this is to prevent integer overflow in the following calculation:
2581 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
2582 */
2583 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
2584 num_mean_elements = 1;
2585 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2586 }
2587
2588 /*
2589 * get next mean value based on previous mean value and current
2590 * potential packed writes. Calculation is as follows:
2591 * mean_pot[i+1] =
2592 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
2593 */
2594 mean_potential *= num_mean_elements;
2595 /*
2596 * add num_mean_elements so that the division of two integers doesn't
2597 * lower mean_potential too much
2598 */
2599 if (potential > mean_potential)
2600 mean_potential += num_mean_elements;
2601 mean_potential += potential;
2602 /* this is for gaining more precision when dividing two integers */
2603 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
2604 /* this completes the mean calculation */
2605 mean_potential /= ++num_mean_elements;
2606 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
2607
2608 /*
2609 * if current potential packed writes is greater than the mean potential
2610 * then the heuristic is that the following workload will contain many
2611 * write requests, therefore we lower the packed trigger. In the
2612 * opposite case we want to increase the trigger in order to get less
2613 * packing events.
2614 */
2615 if (potential >= mean_potential)
2616 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
2617 PCKD_TRGR_LOWER_BOUND : trigger - 1;
2618 else
2619 trigger = (trigger >= pckd_trgr_upper_bound) ?
2620 pckd_trgr_upper_bound : trigger + 1;
2621
2622 /*
2623 * an urgent read request indicates a packed list being interrupted
2624 * by this read, therefore we aim for less packing, hence the trigger
2625 * gets increased
2626 */
2627 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
2628 trigger += PCKD_TRGR_URGENT_PENALTY;
2629
2630 return trigger;
2631}
2632
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002633static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2634 struct request *req)
2635{
2636 struct mmc_host *host = mq->card->host;
2637 int data_dir;
2638
2639 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2640 return;
2641
Maya Erez8e2b3c32012-12-02 13:27:15 +02002642 /* Support for the write packing on eMMC 4.5 or later */
2643 if (mq->card->ext_csd.rev <= 5)
2644 return;
2645
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002646 /*
2647 * In case the packing control is not supported by the host, it should
2648 * not have an effect on the write packing. Therefore we have to enable
2649 * the write packing
2650 */
2651 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2652 mq->wr_packing_enabled = true;
2653 return;
2654 }
2655
2656 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2657 if (mq->num_of_potential_packed_wr_reqs >
2658 mq->num_wr_reqs_to_start_packing)
2659 mq->wr_packing_enabled = true;
Lee Susman841fd132013-04-23 17:59:26 +03002660 mq->num_wr_reqs_to_start_packing =
2661 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2662 mq->card, req,
2663 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002664 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002665 return;
2666 }
2667
2668 data_dir = rq_data_dir(req);
2669
2670 if (data_dir == READ) {
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002671 mmc_blk_disable_wr_packing(mq);
Lee Susman841fd132013-04-23 17:59:26 +03002672 mq->num_wr_reqs_to_start_packing =
2673 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2674 mq->card, req,
2675 mq->num_wr_reqs_to_start_packing);
2676 mq->num_of_potential_packed_wr_reqs = 0;
2677 mq->wr_packing_enabled = false;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002678 return;
2679 } else if (data_dir == WRITE) {
2680 mq->num_of_potential_packed_wr_reqs++;
2681 }
2682
2683 if (mq->num_of_potential_packed_wr_reqs >
2684 mq->num_wr_reqs_to_start_packing)
2685 mq->wr_packing_enabled = true;
2686}
2687
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002688struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2689{
2690 if (!card)
2691 return NULL;
2692
2693 return &card->wr_pack_stats;
2694}
2695EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2696
2697void mmc_blk_init_packed_statistics(struct mmc_card *card)
2698{
2699 int max_num_of_packed_reqs = 0;
2700
2701 if (!card || !card->wr_pack_stats.packing_events)
2702 return;
2703
2704 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2705
2706 spin_lock(&card->wr_pack_stats.lock);
2707 memset(card->wr_pack_stats.packing_events, 0,
2708 (max_num_of_packed_reqs + 1) *
2709 sizeof(*card->wr_pack_stats.packing_events));
2710 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2711 sizeof(card->wr_pack_stats.pack_stop_reason));
2712 card->wr_pack_stats.enabled = true;
2713 spin_unlock(&card->wr_pack_stats.lock);
2714}
2715EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2716
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002717static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2718{
2719 struct request_queue *q = mq->queue;
2720 struct mmc_card *card = mq->card;
2721 struct request *cur = req, *next = NULL;
2722 struct mmc_blk_data *md = mq->data;
2723 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2724 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2725 unsigned int req_sectors = 0, phys_segments = 0;
2726 unsigned int max_blk_count, max_phys_segs;
2727 bool put_back = true;
2728 u8 max_packed_rw = 0;
2729 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002730 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002731
Shawn Lin96e52da2016-08-26 08:49:55 +08002732 /*
2733 * We don't need to check packed for any further
2734 * operation of packed stuff as we set MMC_PACKED_NONE
2735 * and return zero for reqs if geting null packed. Also
2736 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2737 * it again when removing blk req.
2738 */
2739 if (!mqrq->packed) {
2740 md->flags &= (~MMC_BLK_PACKED_CMD);
2741 goto no_packed;
2742 }
2743
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002744 if (!(md->flags & MMC_BLK_PACKED_CMD))
2745 goto no_packed;
2746
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002747 if (!mq->wr_packing_enabled)
2748 goto no_packed;
2749
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002750 if ((rq_data_dir(cur) == WRITE) &&
2751 mmc_host_packed_wr(card->host))
2752 max_packed_rw = card->ext_csd.max_packed_writes;
2753
2754 if (max_packed_rw == 0)
2755 goto no_packed;
2756
2757 if (mmc_req_rel_wr(cur) &&
2758 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2759 goto no_packed;
2760
2761 if (mmc_large_sector(card) &&
2762 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2763 goto no_packed;
2764
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002765 if (cur->cmd_flags & REQ_FUA)
2766 goto no_packed;
2767
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002768 mmc_blk_clear_packed(mqrq);
2769
2770 max_blk_count = min(card->host->max_blk_count,
2771 card->host->max_req_size >> 9);
2772 if (unlikely(max_blk_count > 0xffff))
2773 max_blk_count = 0xffff;
2774
2775 max_phys_segs = queue_max_segments(q);
2776 req_sectors += blk_rq_sectors(cur);
2777 phys_segments += cur->nr_phys_segments;
2778
2779 if (rq_data_dir(cur) == WRITE) {
2780 req_sectors += mmc_large_sector(card) ? 8 : 1;
2781 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2782 }
2783
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002784 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002785 do {
2786 if (reqs >= max_packed_rw - 1) {
2787 put_back = false;
2788 break;
2789 }
2790
2791 spin_lock_irq(q->queue_lock);
2792 next = blk_fetch_request(q);
2793 spin_unlock_irq(q->queue_lock);
2794 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002795 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002796 put_back = false;
2797 break;
2798 }
2799
2800 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002801 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2802 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002803 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002804 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002805
Mike Christie3a5e02c2016-06-05 14:32:23 -05002806 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002807 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002808 req_op(next) == REQ_OP_FLUSH) {
2809 if (req_op(next) != REQ_OP_SECURE_ERASE)
2810 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002811 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002812 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002813
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002814 if (next->cmd_flags & REQ_FUA) {
2815 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2816 break;
2817 }
2818
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002819 if (rq_data_dir(cur) != rq_data_dir(next)) {
2820 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002821 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002822 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002823
2824 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002825 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2826 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002827 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002828 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002829
2830 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002831 if (req_sectors > max_blk_count) {
2832 if (stats->enabled)
2833 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002834 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002835 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002836
2837 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002838 if (phys_segments > max_phys_segs) {
2839 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002840 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002841 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002842
Maya Erez5a8dae12014-12-04 15:13:59 +02002843 if (mq->no_pack_for_random) {
2844 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2845 blk_rq_pos(next)) {
2846 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2847 put_back = 1;
2848 break;
2849 }
2850 }
2851
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002852 if (rq_data_dir(next) == WRITE)
2853 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002854 list_add_tail(&next->queuelist, &mqrq->packed->list);
2855 cur = next;
2856 reqs++;
2857 } while (1);
2858
2859 if (put_back) {
2860 spin_lock_irq(q->queue_lock);
2861 blk_requeue_request(q, next);
2862 spin_unlock_irq(q->queue_lock);
2863 }
2864
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002865 if (stats->enabled) {
2866 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2867 stats->packing_events[reqs + 1]++;
2868 if (reqs + 1 == max_packed_rw)
2869 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2870 }
2871
2872 spin_unlock(&stats->lock);
2873
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002874 if (reqs > 0) {
2875 list_add(&req->queuelist, &mqrq->packed->list);
2876 mqrq->packed->nr_entries = ++reqs;
2877 mqrq->packed->retries = reqs;
2878 return reqs;
2879 }
2880
2881no_packed:
2882 mqrq->cmd_type = MMC_PACKED_NONE;
2883 return 0;
2884}
2885
2886static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2887 struct mmc_card *card,
2888 struct mmc_queue *mq)
2889{
2890 struct mmc_blk_request *brq = &mqrq->brq;
2891 struct request *req = mqrq->req;
2892 struct request *prq;
2893 struct mmc_blk_data *md = mq->data;
2894 struct mmc_packed *packed = mqrq->packed;
2895 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002896 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002897 u8 hdr_blocks;
2898 u8 i = 1;
2899
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002900 mqrq->cmd_type = MMC_PACKED_WRITE;
2901 packed->blocks = 0;
2902 packed->idx_failure = MMC_PACKED_NR_IDX;
2903
2904 packed_cmd_hdr = packed->cmd_hdr;
2905 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002906 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2907 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002908 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2909
2910 /*
2911 * Argument for each entry of packed group
2912 */
2913 list_for_each_entry(prq, &packed->list, queuelist) {
2914 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2915 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2916 (prq->cmd_flags & REQ_META) &&
2917 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002918 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002919 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002920 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002921 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2922 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002923 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002924 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002925 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002926 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002927 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002928 packed->blocks += blk_rq_sectors(prq);
2929 i++;
2930 }
2931
2932 memset(brq, 0, sizeof(struct mmc_blk_request));
2933 brq->mrq.cmd = &brq->cmd;
2934 brq->mrq.data = &brq->data;
2935 brq->mrq.sbc = &brq->sbc;
2936 brq->mrq.stop = &brq->stop;
2937
2938 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2939 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2940 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2941
2942 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2943 brq->cmd.arg = blk_rq_pos(req);
2944 if (!mmc_card_blockaddr(card))
2945 brq->cmd.arg <<= 9;
2946 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2947
2948 brq->data.blksz = 512;
2949 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002950 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302951 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002952
2953 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2954 brq->stop.arg = 0;
2955 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2956
2957 mmc_set_data_timeout(&brq->data, card);
2958
2959 brq->data.sg = mqrq->sg;
2960 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2961
2962 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002963
2964 /*
2965 * This is intended for packed commands tests usage - in case these
2966 * functions are not in use the respective pointers are NULL
2967 */
2968 if (mq->err_check_fn)
2969 mqrq->mmc_active.err_check = mq->err_check_fn;
2970 else
2971 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2972
2973 if (mq->packed_test_fn)
2974 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002975
2976 mmc_queue_bounce_pre(mqrq);
2977}
2978
Adrian Hunter67716322011-08-29 16:42:15 +03002979static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2980 struct mmc_blk_request *brq, struct request *req,
2981 int ret)
2982{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002983 struct mmc_queue_req *mq_rq;
2984 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2985
Adrian Hunter67716322011-08-29 16:42:15 +03002986 /*
2987 * If this is an SD card and we're writing, we can first
2988 * mark the known good sectors as ok.
2989 *
2990 * If the card is not SD, we can still ok written sectors
2991 * as reported by the controller (which might be less than
2992 * the real number of written sectors, but never more).
2993 */
2994 if (mmc_card_sd(card)) {
2995 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302996 if (!brq->data.fault_injected) {
2997 blocks = mmc_sd_num_wr_blocks(card);
2998 if (blocks != (u32)-1)
2999 ret = blk_end_request(req, 0, blocks << 9);
3000 } else
3001 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03003002 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003003 if (!mmc_packed_cmd(mq_rq->cmd_type))
3004 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03003005 }
3006 return ret;
3007}
3008
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003009static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
3010{
3011 struct request *prq;
3012 struct mmc_packed *packed = mq_rq->packed;
3013 int idx = packed->idx_failure, i = 0;
3014 int ret = 0;
3015
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003016 while (!list_empty(&packed->list)) {
3017 prq = list_entry_rq(packed->list.next);
3018 if (idx == i) {
3019 /* retry from error index */
3020 packed->nr_entries -= idx;
3021 mq_rq->req = prq;
3022 ret = 1;
3023
3024 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
3025 list_del_init(&prq->queuelist);
3026 mmc_blk_clear_packed(mq_rq);
3027 }
3028 return ret;
3029 }
3030 list_del_init(&prq->queuelist);
3031 blk_end_request(prq, 0, blk_rq_bytes(prq));
3032 i++;
3033 }
3034
3035 mmc_blk_clear_packed(mq_rq);
3036 return ret;
3037}
3038
3039static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
3040{
3041 struct request *prq;
3042 struct mmc_packed *packed = mq_rq->packed;
3043
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003044 while (!list_empty(&packed->list)) {
3045 prq = list_entry_rq(packed->list.next);
3046 list_del_init(&prq->queuelist);
3047 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
3048 }
3049
3050 mmc_blk_clear_packed(mq_rq);
3051}
3052
3053static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
3054 struct mmc_queue_req *mq_rq)
3055{
3056 struct request *prq;
3057 struct request_queue *q = mq->queue;
3058 struct mmc_packed *packed = mq_rq->packed;
3059
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003060 while (!list_empty(&packed->list)) {
3061 prq = list_entry_rq(packed->list.prev);
3062 if (prq->queuelist.prev != &packed->list) {
3063 list_del_init(&prq->queuelist);
3064 spin_lock_irq(q->queue_lock);
3065 blk_requeue_request(mq->queue, prq);
3066 spin_unlock_irq(q->queue_lock);
3067 } else {
3068 list_del_init(&prq->queuelist);
3069 }
3070 }
3071
3072 mmc_blk_clear_packed(mq_rq);
3073}
3074
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003075static int mmc_blk_cmdq_start_req(struct mmc_host *host,
3076 struct mmc_cmdq_req *cmdq_req)
3077{
3078 struct mmc_request *mrq = &cmdq_req->mrq;
3079
3080 mrq->done = mmc_blk_cmdq_req_done;
3081 return mmc_cmdq_start_req(host, cmdq_req);
3082}
3083
Asutosh Das5238e022015-04-23 16:00:45 +05303084/* prepare for non-data commands */
3085static struct mmc_cmdq_req *mmc_cmdq_prep_dcmd(
3086 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
3087{
3088 struct request *req = mqrq->req;
3089 struct mmc_cmdq_req *cmdq_req = &mqrq->cmdq_req;
3090
3091 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
3092
3093 cmdq_req->mrq.data = NULL;
3094 cmdq_req->cmd_flags = req->cmd_flags;
3095 cmdq_req->mrq.req = mqrq->req;
3096 req->special = mqrq;
3097 cmdq_req->cmdq_req_flags |= DCMD;
3098 cmdq_req->mrq.cmdq_req = cmdq_req;
3099
3100 return &mqrq->cmdq_req;
3101}
3102
3103
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003104#define IS_RT_CLASS_REQ(x) \
3105 (IOPRIO_PRIO_CLASS(req_get_ioprio(x)) == IOPRIO_CLASS_RT)
3106
3107static struct mmc_cmdq_req *mmc_blk_cmdq_rw_prep(
3108 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
3109{
3110 struct mmc_card *card = mq->card;
3111 struct request *req = mqrq->req;
3112 struct mmc_blk_data *md = mq->data;
3113 bool do_rel_wr = mmc_req_rel_wr(req) && (md->flags & MMC_BLK_REL_WR);
3114 bool do_data_tag;
3115 bool read_dir = (rq_data_dir(req) == READ);
3116 bool prio = IS_RT_CLASS_REQ(req);
3117 struct mmc_cmdq_req *cmdq_rq = &mqrq->cmdq_req;
3118
3119 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
3120
3121 cmdq_rq->tag = req->tag;
3122 if (read_dir) {
3123 cmdq_rq->cmdq_req_flags |= DIR;
3124 cmdq_rq->data.flags = MMC_DATA_READ;
3125 } else {
3126 cmdq_rq->data.flags = MMC_DATA_WRITE;
3127 }
3128 if (prio)
3129 cmdq_rq->cmdq_req_flags |= PRIO;
3130
3131 if (do_rel_wr)
3132 cmdq_rq->cmdq_req_flags |= REL_WR;
3133
3134 cmdq_rq->data.blocks = blk_rq_sectors(req);
3135 cmdq_rq->blk_addr = blk_rq_pos(req);
3136 cmdq_rq->data.blksz = MMC_CARD_CMDQ_BLK_SIZE;
3137
3138 mmc_set_data_timeout(&cmdq_rq->data, card);
3139
3140 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
3141 (req->cmd_flags & REQ_META) &&
3142 (rq_data_dir(req) == WRITE) &&
3143 ((cmdq_rq->data.blocks * cmdq_rq->data.blksz) >=
3144 card->ext_csd.data_tag_unit_size);
3145 if (do_data_tag)
3146 cmdq_rq->cmdq_req_flags |= DAT_TAG;
3147 cmdq_rq->data.sg = mqrq->sg;
3148 cmdq_rq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
3149
3150 /*
3151 * Adjust the sg list so it is the same size as the
3152 * request.
3153 */
3154 if (cmdq_rq->data.blocks > card->host->max_blk_count)
3155 cmdq_rq->data.blocks = card->host->max_blk_count;
3156
3157 if (cmdq_rq->data.blocks != blk_rq_sectors(req)) {
3158 int i, data_size = cmdq_rq->data.blocks << 9;
3159 struct scatterlist *sg;
3160
3161 for_each_sg(cmdq_rq->data.sg, sg, cmdq_rq->data.sg_len, i) {
3162 data_size -= sg->length;
3163 if (data_size <= 0) {
3164 sg->length += data_size;
3165 i++;
3166 break;
3167 }
3168 }
3169 cmdq_rq->data.sg_len = i;
3170 }
3171
3172 mqrq->cmdq_req.cmd_flags = req->cmd_flags;
3173 mqrq->cmdq_req.mrq.req = mqrq->req;
3174 mqrq->cmdq_req.mrq.cmdq_req = &mqrq->cmdq_req;
3175 mqrq->cmdq_req.mrq.data = &mqrq->cmdq_req.data;
3176 mqrq->req->special = mqrq;
3177
3178 pr_debug("%s: %s: mrq: 0x%p req: 0x%p mqrq: 0x%p bytes to xf: %d mmc_cmdq_req: 0x%p card-addr: 0x%08x dir(r-1/w-0): %d\n",
3179 mmc_hostname(card->host), __func__, &mqrq->cmdq_req.mrq,
3180 mqrq->req, mqrq, (cmdq_rq->data.blocks * cmdq_rq->data.blksz),
3181 cmdq_rq, cmdq_rq->blk_addr,
3182 (cmdq_rq->cmdq_req_flags & DIR) ? 1 : 0);
3183
3184 return &mqrq->cmdq_req;
3185}
3186
3187static int mmc_blk_cmdq_issue_rw_rq(struct mmc_queue *mq, struct request *req)
3188{
3189 struct mmc_queue_req *active_mqrq;
3190 struct mmc_card *card = mq->card;
3191 struct mmc_host *host = card->host;
3192 struct mmc_cmdq_req *mc_rq;
3193 int ret = 0;
3194
Subhash Jadavani09bd2e22015-08-10 15:54:26 -07003195 mmc_deferred_scaling(host);
3196 mmc_cmdq_clk_scaling_start_busy(host, true);
3197
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003198 BUG_ON((req->tag < 0) || (req->tag > card->ext_csd.cmdq_depth));
Subhash Jadavani09bd2e22015-08-10 15:54:26 -07003199 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.data_active_reqs));
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003200 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
3201
3202 active_mqrq = &mq->mqrq_cmdq[req->tag];
3203 active_mqrq->req = req;
3204
3205 mc_rq = mmc_blk_cmdq_rw_prep(active_mqrq, mq);
3206
3207 ret = mmc_blk_cmdq_start_req(card->host, mc_rq);
3208 return ret;
3209}
3210
Asutosh Das5238e022015-04-23 16:00:45 +05303211/*
3212 * Issues a flush (dcmd) request
3213 */
3214int mmc_blk_cmdq_issue_flush_rq(struct mmc_queue *mq, struct request *req)
3215{
3216 int err;
3217 struct mmc_queue_req *active_mqrq;
3218 struct mmc_card *card = mq->card;
3219 struct mmc_host *host;
3220 struct mmc_cmdq_req *cmdq_req;
3221 struct mmc_cmdq_context_info *ctx_info;
3222
3223 BUG_ON(!card);
3224 host = card->host;
3225 BUG_ON(!host);
3226 BUG_ON(req->tag > card->ext_csd.cmdq_depth);
3227 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
3228
3229 ctx_info = &host->cmdq_ctx;
3230
3231 set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
3232
3233 active_mqrq = &mq->mqrq_cmdq[req->tag];
3234 active_mqrq->req = req;
3235
3236 cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
3237 cmdq_req->cmdq_req_flags |= QBR;
3238 cmdq_req->mrq.cmd = &cmdq_req->cmd;
3239 cmdq_req->tag = req->tag;
3240
3241 err = mmc_cmdq_prepare_flush(cmdq_req->mrq.cmd);
3242 if (err) {
3243 pr_err("%s: failed (%d) preparing flush req\n",
3244 mmc_hostname(host), err);
3245 return err;
3246 }
3247 err = mmc_blk_cmdq_start_req(card->host, cmdq_req);
3248 return err;
3249}
3250EXPORT_SYMBOL(mmc_blk_cmdq_issue_flush_rq);
3251
Asutosh Das02e30862015-05-20 16:52:04 +05303252static void mmc_blk_cmdq_reset(struct mmc_host *host, bool clear_all)
3253{
3254 if (!host->cmdq_ops->reset)
3255 return;
3256
3257 if (!test_bit(CMDQ_STATE_HALT, &host->cmdq_ctx.curr_state)) {
3258 if (mmc_cmdq_halt(host, true)) {
3259 pr_err("%s: halt failed\n", mmc_hostname(host));
3260 goto reset;
3261 }
3262 }
3263
3264 if (clear_all)
3265 mmc_cmdq_discard_queue(host, 0);
3266reset:
3267 mmc_hw_reset(host);
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303268 mmc_host_clk_hold(host);
Asutosh Das02e30862015-05-20 16:52:04 +05303269 host->cmdq_ops->reset(host, true);
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303270 mmc_host_clk_release(host);
Asutosh Das02e30862015-05-20 16:52:04 +05303271 clear_bit(CMDQ_STATE_HALT, &host->cmdq_ctx.curr_state);
3272}
3273
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303274static void mmc_blk_cmdq_shutdown(struct mmc_queue *mq)
3275{
3276 int err;
3277 struct mmc_card *card = mq->card;
3278 struct mmc_host *host = card->host;
3279
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003280 mmc_get_card(card);
3281 mmc_host_clk_hold(host);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303282 err = mmc_cmdq_halt(host, true);
3283 if (err) {
3284 pr_err("%s: halt: failed: %d\n", __func__, err);
3285 return;
3286 }
3287
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303288 /* disable CQ mode in card */
3289 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3290 EXT_CSD_CMDQ, 0,
3291 card->ext_csd.generic_cmd6_time);
3292 if (err) {
3293 pr_err("%s: failed to switch card to legacy mode: %d\n",
3294 __func__, err);
3295 goto out;
3296 } else {
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003297 mmc_card_clr_cmdq(card);
3298 host->cmdq_ops->disable(host, false);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303299 host->card->cmdq_init = false;
3300 }
3301out:
Konstantin Dorfman6a8c2862015-08-02 16:10:44 +03003302 mmc_host_clk_release(host);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003303 mmc_put_card(card);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303304}
3305
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303306static enum blk_eh_timer_return mmc_blk_cmdq_req_timed_out(struct request *req)
3307{
3308 struct mmc_queue *mq = req->q->queuedata;
3309 struct mmc_host *host = mq->card->host;
3310 struct mmc_queue_req *mq_rq = req->special;
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003311 struct mmc_request *mrq;
3312 struct mmc_cmdq_req *cmdq_req;
3313
3314 BUG_ON(!host);
3315
3316 /*
3317 * The mmc_queue_req will be present only if the request
3318 * is issued to the LLD. The request could be fetched from
3319 * block layer queue but could be waiting to be issued
3320 * (for e.g. clock scaling is waiting for an empty cmdq queue)
3321 * Reset the timer in such cases to give LLD more time
3322 */
3323 if (!mq_rq) {
3324 pr_warn("%s: restart timer for tag: %d\n", __func__, req->tag);
3325 return BLK_EH_RESET_TIMER;
3326 }
3327
3328 mrq = &mq_rq->cmdq_req.mrq;
3329 cmdq_req = &mq_rq->cmdq_req;
3330
3331 BUG_ON(!mrq || !cmdq_req);
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303332
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303333 if (cmdq_req->cmdq_req_flags & DCMD)
3334 mrq->cmd->error = -ETIMEDOUT;
3335 else
3336 mrq->data->error = -ETIMEDOUT;
3337
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003338 BUG_ON(host->err_mrq != NULL);
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303339 host->err_mrq = mrq;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303340
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003341 mmc_host_clk_release(mrq->host);
3342 return BLK_EH_HANDLED;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05303343}
3344
Asutosh Das02e30862015-05-20 16:52:04 +05303345static void mmc_blk_cmdq_err(struct mmc_queue *mq)
3346{
3347 int err;
3348 int retry = 0;
3349 int gen_err;
3350 u32 status;
3351
3352 struct mmc_host *host = mq->card->host;
3353 struct mmc_request *mrq = host->err_mrq;
3354 struct mmc_card *card = mq->card;
3355 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3356
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003357 pm_runtime_get_sync(&card->dev);
Konstantin Dorfman9a9c9fd2015-06-30 17:39:43 +03003358 mmc_host_clk_hold(host);
3359 host->cmdq_ops->dumpstate(host);
3360 mmc_host_clk_release(host);
3361
Asutosh Das02e30862015-05-20 16:52:04 +05303362 err = mmc_cmdq_halt(host, true);
3363 if (err) {
3364 pr_err("halt: failed: %d\n", err);
3365 goto reset;
3366 }
3367
3368 /* RED error - Fatal: requires reset */
3369 if (mrq->cmdq_req->resp_err) {
3370 pr_crit("%s: Response error detected: Device in bad state\n",
3371 mmc_hostname(host));
3372 blk_end_request_all(mrq->req, -EIO);
3373 goto reset;
3374 }
3375
Venkat Gopalakrishnand36c45a2015-09-24 17:39:22 -07003376 if (mrq->data && mrq->data->error) {
Asutosh Das02e30862015-05-20 16:52:04 +05303377 blk_end_request_all(mrq->req, mrq->data->error);
3378 for (; retry < MAX_RETRIES; retry++) {
3379 err = get_card_status(card, &status, 0);
3380 if (!err)
3381 break;
3382 }
3383
3384 if (err) {
3385 pr_err("%s: No response from card !!!\n",
3386 mmc_hostname(host));
3387 goto reset;
3388 }
3389
3390 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
3391 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
3392 err = send_stop(card, MMC_CMDQ_STOP_TIMEOUT_MS,
3393 mrq->req, &gen_err, &status);
3394 if (err) {
3395 pr_err("%s: error %d sending stop (%d) command\n",
3396 mrq->req->rq_disk->disk_name,
3397 err, status);
3398 goto reset;
3399 }
3400 }
3401
3402 if (mmc_cmdq_discard_queue(host, mrq->req->tag))
3403 goto reset;
3404 else
3405 goto unhalt;
3406 }
3407
3408 /* DCMD commands */
Venkat Gopalakrishnand36c45a2015-09-24 17:39:22 -07003409 if (mrq->cmd && mrq->cmd->error) {
3410 /*
3411 * Notify completion for non flush commands like discard
3412 * that wait for DCMD finish.
3413 */
3414 if (!(mrq->req->cmd_flags & REQ_PREFLUSH)) {
3415 complete(&mrq->completion);
3416 goto reset;
3417 }
3418 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
Asutosh Das02e30862015-05-20 16:52:04 +05303419 blk_end_request_all(mrq->req, mrq->cmd->error);
Venkat Gopalakrishnand36c45a2015-09-24 17:39:22 -07003420 }
Asutosh Das02e30862015-05-20 16:52:04 +05303421
3422reset:
3423 spin_lock_irq(mq->queue->queue_lock);
3424 blk_queue_invalidate_tags(mrq->req->q);
3425 spin_unlock_irq(mq->queue->queue_lock);
3426 mmc_blk_cmdq_reset(host, true);
3427 goto out;
3428
3429unhalt:
3430 mmc_cmdq_halt(host, false);
3431
3432out:
Venkat Gopalakrishnan901b09c2015-08-28 17:18:54 -07003433 host->err_mrq = NULL;
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003434 pm_runtime_mark_last_busy(&card->dev);
Dov Levenglick2b678302015-07-01 14:24:20 +03003435 __mmc_put_card(card);
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003436
Asutosh Das02e30862015-05-20 16:52:04 +05303437 if (test_and_clear_bit(0, &ctx_info->req_starved))
3438 blk_run_queue(mrq->req->q);
3439}
3440
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003441/* invoked by block layer in softirq context */
3442void mmc_blk_cmdq_complete_rq(struct request *rq)
3443{
3444 struct mmc_queue_req *mq_rq = rq->special;
3445 struct mmc_request *mrq = &mq_rq->cmdq_req.mrq;
3446 struct mmc_host *host = mrq->host;
3447 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3448 struct mmc_cmdq_req *cmdq_req = &mq_rq->cmdq_req;
3449 struct mmc_queue *mq = (struct mmc_queue *)rq->q->queuedata;
3450 int err = 0;
Talel Shenhar339a4e12015-06-29 10:50:19 +03003451 bool is_dcmd = false;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003452
3453 if (mrq->cmd && mrq->cmd->error)
3454 err = mrq->cmd->error;
3455 else if (mrq->data && mrq->data->error)
3456 err = mrq->data->error;
3457
Asutosh Das02e30862015-05-20 16:52:04 +05303458 /* clear pending request */
3459 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
3460 &ctx_info->active_reqs));
3461
Subhash Jadavani09bd2e22015-08-10 15:54:26 -07003462 if (cmdq_req->cmdq_req_flags & DCMD)
3463 is_dcmd = true;
3464 else
3465 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
3466 &ctx_info->data_active_reqs));
Talel Shenhar339a4e12015-06-29 10:50:19 +03003467
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003468 mmc_cmdq_post_req(host, mrq, err);
3469 if (err) {
3470 pr_err("%s: %s: txfr error: %d\n", mmc_hostname(mrq->host),
3471 __func__, err);
Asutosh Das02e30862015-05-20 16:52:04 +05303472 if (test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state)) {
3473 pr_err("%s: CQ in error state, ending current req: %d\n",
3474 __func__, err);
3475 blk_end_request_all(rq, err);
3476 } else {
3477 set_bit(CMDQ_STATE_ERR, &ctx_info->curr_state);
3478 schedule_work(&mq->cmdq_err_work);
3479 }
3480 goto out;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003481 }
3482
Asutosh Das5238e022015-04-23 16:00:45 +05303483 if (cmdq_req->cmdq_req_flags & DCMD) {
3484 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
Asutosh Das02e30862015-05-20 16:52:04 +05303485 blk_end_request_all(rq, err);
Asutosh Das5238e022015-04-23 16:00:45 +05303486 goto out;
3487 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003488
3489 blk_end_request(rq, err, cmdq_req->data.bytes_xfered);
3490
Asutosh Das5238e022015-04-23 16:00:45 +05303491out:
Talel Shenhar339a4e12015-06-29 10:50:19 +03003492
3493 mmc_cmdq_clk_scaling_stop_busy(host, true, is_dcmd);
Asutosh Das02e30862015-05-20 16:52:04 +05303494 if (!test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state) &&
3495 test_and_clear_bit(0, &ctx_info->req_starved))
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003496 blk_run_queue(mq->queue);
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303497
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003498 mmc_put_card(host->card);
Talel Shenhar339a4e12015-06-29 10:50:19 +03003499 if (!ctx_info->active_reqs)
3500 wake_up_interruptible(&host->cmdq_ctx.queue_empty_wq);
3501
Asutosh Dasa0ba4922015-04-23 16:01:57 +05303502 if (blk_queue_stopped(mq->queue) && !ctx_info->active_reqs)
3503 complete(&mq->cmdq_shutdown_complete);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003504 return;
3505}
3506
3507/*
3508 * Complete reqs from block layer softirq context
3509 * Invoked in irq context
3510 */
3511void mmc_blk_cmdq_req_done(struct mmc_request *mrq)
3512{
3513 struct request *req = mrq->req;
3514
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05303515 mmc_host_clk_release(mrq->host);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003516 blk_complete_request(req);
3517}
3518EXPORT_SYMBOL(mmc_blk_cmdq_req_done);
3519
Per Forlinee8a43a2011-07-01 18:55:33 +02003520static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02003521{
3522 struct mmc_blk_data *md = mq->data;
3523 struct mmc_card *card = md->queue.card;
3524 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03003525 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02003526 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02003527 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303528 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02003529 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003530 const u8 packed_nr = 2;
3531 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003532#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3533 unsigned long waitfor = jiffies;
3534#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02003535
3536 if (!rqc && !mq->mqrq_prev->req)
3537 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02003538
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003539 if (rqc)
3540 reqs = mmc_blk_prep_packed_list(mq, rqc);
3541
Per Forlin54d49d72011-07-01 18:55:29 +02003542 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02003543 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303544 /*
3545 * When 4KB native sector is enabled, only 8 blocks
3546 * multiple read or write is allowed
3547 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00003548 if (mmc_large_sector(card) &&
3549 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303550 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
3551 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003552 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303553 goto cmd_abort;
3554 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003555
3556 if (reqs >= packed_nr)
3557 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
3558 card, mq);
3559 else
3560 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02003561 areq = &mq->mqrq_cur->mmc_active;
3562 } else
3563 areq = NULL;
3564 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003565 if (!areq) {
3566 if (status == MMC_BLK_NEW_REQUEST)
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003567 set_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Per Forlinee8a43a2011-07-01 18:55:33 +02003568 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003569 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02003570
Per Forlinee8a43a2011-07-01 18:55:33 +02003571 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
3572 brq = &mq_rq->brq;
3573 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03003574 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02003575 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02003576
Per Forlind78d4a82011-07-01 18:55:30 +02003577 switch (status) {
3578 case MMC_BLK_SUCCESS:
3579 case MMC_BLK_PARTIAL:
3580 /*
3581 * A block was successfully transferred.
3582 */
Adrian Hunter67716322011-08-29 16:42:15 +03003583 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003584
Mark Salyzyn6904e432016-01-28 11:12:25 -08003585 mmc_blk_simulate_delay(mq, rqc, waitfor);
3586
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003587 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3588 ret = mmc_blk_end_packed_req(mq_rq);
3589 break;
3590 } else {
3591 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02003592 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003593 }
3594
Adrian Hunter67716322011-08-29 16:42:15 +03003595 /*
3596 * If the blk_end_request function returns non-zero even
3597 * though all data has been transferred and no errors
3598 * were returned by the host controller, it's a bug.
3599 */
Per Forlinee8a43a2011-07-01 18:55:33 +02003600 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303601 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02003602 __func__, blk_rq_bytes(req),
3603 brq->data.bytes_xfered);
3604 rqc = NULL;
3605 goto cmd_abort;
3606 }
Per Forlind78d4a82011-07-01 18:55:30 +02003607 break;
3608 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03003609 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08003610 if (mmc_blk_reset(md, card->host, type))
3611 goto cmd_abort;
3612 if (!ret)
3613 goto start_new_req;
3614 break;
Per Forlind78d4a82011-07-01 18:55:30 +02003615 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03003616 retune_retry_done = brq->retune_retry_done;
Maya Erezf93ca0a2014-12-09 23:34:41 +02003617 if (retry++ < MMC_BLK_MAX_RETRIES)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003618 break;
Adrian Hunter67716322011-08-29 16:42:15 +03003619 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02003620 case MMC_BLK_ABORT:
Maya Erezf93ca0a2014-12-09 23:34:41 +02003621 if (!mmc_blk_reset(md, card->host, type) &&
3622 (retry++ < (MMC_BLK_MAX_RETRIES + 1)))
Adrian Hunter67716322011-08-29 16:42:15 +03003623 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003624 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003625 case MMC_BLK_DATA_ERR: {
3626 int err;
3627
3628 err = mmc_blk_reset(md, card->host, type);
3629 if (!err)
3630 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05303631 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003632 }
3633 case MMC_BLK_ECC_ERR:
3634 if (brq->data.blocks > 1) {
3635 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07003636 pr_warn("%s: retrying using single block read\n",
3637 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03003638 disable_multi = 1;
3639 break;
3640 }
Per Forlind78d4a82011-07-01 18:55:30 +02003641 /*
3642 * After an error, we redo I/O one sector at a
3643 * time, so we only reach here after trying to
3644 * read a single sector.
3645 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05303646 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02003647 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02003648 if (!ret)
3649 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02003650 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05303651 case MMC_BLK_NOMEDIUM:
3652 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003653 default:
3654 pr_err("%s: Unhandled return value (%d)",
3655 req->rq_disk->disk_name, status);
3656 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003657 }
3658
Per Forlinee8a43a2011-07-01 18:55:33 +02003659 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003660 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3661 if (!mq_rq->packed->retries)
3662 goto cmd_abort;
3663 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
3664 mmc_start_req(card->host,
3665 &mq_rq->mmc_active, NULL);
3666 } else {
3667
3668 /*
3669 * In case of a incomplete request
3670 * prepare it again and resend.
3671 */
3672 mmc_blk_rw_rq_prep(mq_rq, card,
3673 disable_multi, mq);
3674 mmc_start_req(card->host,
3675 &mq_rq->mmc_active, NULL);
3676 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03003677 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02003678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 } while (ret);
3680
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 return 1;
3682
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003683 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003684 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3685 mmc_blk_abort_packed_req(mq_rq);
3686 } else {
3687 if (mmc_card_removed(card))
3688 req->cmd_flags |= REQ_QUIET;
3689 while (ret)
3690 ret = blk_end_request(req, -EIO,
3691 blk_rq_cur_bytes(req));
3692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
Per Forlinee8a43a2011-07-01 18:55:33 +02003694 start_new_req:
3695 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09003696 if (mmc_card_removed(card)) {
3697 rqc->cmd_flags |= REQ_QUIET;
3698 blk_end_request_all(rqc, -EIO);
3699 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003700 /*
3701 * If current request is packed, it needs to put back.
3702 */
3703 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
3704 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
3705
Seungwon Jeon7a819022013-01-22 19:48:07 +09003706 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
3707 mmc_start_req(card->host,
3708 &mq->mqrq_cur->mmc_active, NULL);
3709 }
Per Forlinee8a43a2011-07-01 18:55:33 +02003710 }
3711
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 return 0;
3713}
3714
Asutosh Das8b594832015-04-23 09:55:43 +05303715static inline int mmc_blk_cmdq_part_switch(struct mmc_card *card,
3716 struct mmc_blk_data *md)
3717{
3718 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
3719 struct mmc_host *host = card->host;
3720 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
3721 u8 part_config = card->ext_csd.part_config;
3722
3723 if ((main_md->part_curr == md->part_type) &&
3724 (card->part_curr == md->part_type))
3725 return 0;
3726
3727 WARN_ON(!((card->host->caps2 & MMC_CAP2_CMD_QUEUE) &&
3728 card->ext_csd.cmdq_support &&
3729 (md->flags & MMC_BLK_CMD_QUEUE)));
3730
3731 if (!test_bit(CMDQ_STATE_HALT, &ctx->curr_state))
3732 WARN_ON(mmc_cmdq_halt(host, true));
3733
3734 /* disable CQ mode in card */
3735 if (mmc_card_cmdq(card)) {
3736 WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3737 EXT_CSD_CMDQ, 0,
3738 card->ext_csd.generic_cmd6_time));
3739 mmc_card_clr_cmdq(card);
3740 }
3741
3742 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
3743 part_config |= md->part_type;
3744
3745 WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3746 EXT_CSD_PART_CONFIG, part_config,
3747 card->ext_csd.part_time));
3748
3749 card->ext_csd.part_config = part_config;
3750 card->part_curr = md->part_type;
3751
3752 main_md->part_curr = md->part_type;
3753
3754 WARN_ON(mmc_blk_cmdq_switch(card, md, true));
3755 WARN_ON(mmc_cmdq_halt(host, false));
3756
3757 return 0;
3758}
3759
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003760static int mmc_blk_cmdq_issue_rq(struct mmc_queue *mq, struct request *req)
3761{
3762 int ret;
3763 struct mmc_blk_data *md = mq->data;
3764 struct mmc_card *card = md->queue.card;
3765
Konstantin Dorfman2747f642015-06-09 10:56:41 +03003766 mmc_get_card(card);
Maya Ereza2152152015-09-21 14:43:38 +03003767
3768 if (!card->host->cmdq_ctx.active_reqs && mmc_card_doing_bkops(card)) {
3769 ret = mmc_cmdq_halt(card->host, true);
3770 if (ret)
3771 goto out;
3772 ret = mmc_stop_bkops(card);
3773 if (ret) {
3774 pr_err("%s: %s: mmc_stop_bkops failed %d\n",
3775 md->disk->disk_name, __func__, ret);
3776 goto out;
3777 }
3778 ret = mmc_cmdq_halt(card->host, false);
3779 if (ret)
3780 goto out;
3781 }
3782
Asutosh Das8b594832015-04-23 09:55:43 +05303783 ret = mmc_blk_cmdq_part_switch(card, md);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003784 if (ret) {
3785 pr_err("%s: %s: partition switch failed %d\n",
3786 md->disk->disk_name, __func__, ret);
Maya Ereza2152152015-09-21 14:43:38 +03003787 goto out;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003788 }
3789
Asutosh Das5238e022015-04-23 16:00:45 +05303790 if (req) {
Sahitya Tummala9433a132015-06-09 09:38:36 +05303791 if (req_op(req) == REQ_OP_DISCARD) {
3792 ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
3793 } else if (req_op(req) == REQ_OP_SECURE_ERASE) {
3794 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
3795 ret = mmc_blk_cmdq_issue_secdiscard_rq(mq, req);
3796 else
3797 ret = mmc_blk_cmdq_issue_discard_rq(mq, req);
3798 } else if (req_op(req) == REQ_OP_FLUSH) {
Asutosh Das5238e022015-04-23 16:00:45 +05303799 ret = mmc_blk_cmdq_issue_flush_rq(mq, req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05303800 } else {
Asutosh Das5238e022015-04-23 16:00:45 +05303801 ret = mmc_blk_cmdq_issue_rw_rq(mq, req);
Sahitya Tummala9433a132015-06-09 09:38:36 +05303802 }
Asutosh Das5238e022015-04-23 16:00:45 +05303803 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003804
Maya Ereza2152152015-09-21 14:43:38 +03003805 return ret;
3806
3807out:
3808 if (req)
3809 blk_end_request_all(req, ret);
3810 mmc_put_card(card);
3811
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003812 return ret;
3813}
3814
Linus Walleij29eb7bd2016-09-20 11:34:38 +02003815int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07003816{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003817 int ret;
3818 struct mmc_blk_data *md = mq->data;
3819 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003820 struct mmc_host *host = card->host;
3821 unsigned long flags;
Sahitya Tummala61868a42015-05-28 16:54:19 +05303822 unsigned int cmd_flags = req ? req->cmd_flags : 0;
Adrian Hunter869c5542016-08-25 14:11:43 -06003823 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003824
Dov Levenglicka0296392015-06-24 19:51:58 +03003825 if (req && !mq->mqrq_prev->req) {
Per Forlinee8a43a2011-07-01 18:55:33 +02003826 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003827 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02003828
Dov Levenglicka0296392015-06-24 19:51:58 +03003829 if (mmc_card_doing_bkops(host->card)) {
3830 ret = mmc_stop_bkops(host->card);
3831 if (ret)
3832 goto out;
3833 }
3834 }
3835
Andrei Warkentin371a6892011-04-11 18:10:25 -05003836 ret = mmc_blk_part_switch(card, md);
3837 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03003838 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05303839 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03003840 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003841 ret = 0;
3842 goto out;
3843 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003844
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003845 mmc_blk_write_packing_control(mq, req);
3846
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003847 clear_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Mike Christiec2df40d2016-06-05 14:32:17 -05003848 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02003849 /* complete ongoing async transfer before issuing discard */
3850 if (card->host->areq)
3851 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02003852 ret = mmc_blk_issue_discard_rq(mq, req);
3853 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
3854 /* complete ongoing async transfer before issuing secure erase*/
3855 if (card->host->areq)
3856 mmc_blk_issue_rw_rq(mq, NULL);
Maya Erez0c0609f2014-12-09 23:31:55 +02003857 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
3858 ret = mmc_blk_issue_secdiscard_rq(mq, req);
3859 else
3860 ret = mmc_blk_issue_discard_rq(mq, req);
Sahitya Tummala61868a42015-05-28 16:54:19 +05303861 } else if ((req && req_op(req) == REQ_OP_FLUSH) ||
3862 (cmd_flags & REQ_BARRIER)) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09003863 /* complete ongoing async transfer before issuing flush */
3864 if (card->host->areq)
3865 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003866 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003867 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003868 if (!req && host->areq) {
3869 spin_lock_irqsave(&host->context_info.lock, flags);
3870 host->context_info.is_waiting_last_req = true;
3871 spin_unlock_irqrestore(&host->context_info.lock, flags);
3872 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003873 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003874 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003875
Andrei Warkentin371a6892011-04-11 18:10:25 -05003876out:
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003877 if ((!req && !(test_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags))) ||
3878 req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09003879 /*
3880 * Release host when there are no more requests
3881 * and after special request(discard, flush) is done.
3882 * In case sepecial request, there is no reentry to
3883 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
3884 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003885 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003886 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07003887}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
Russell Kinga6f6c962006-01-03 22:38:44 +00003889static inline int mmc_blk_readonly(struct mmc_card *card)
3890{
3891 return mmc_card_readonly(card) ||
3892 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
3893}
3894
Andrei Warkentin371a6892011-04-11 18:10:25 -05003895static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
3896 struct device *parent,
3897 sector_t size,
3898 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003899 const char *subname,
3900 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901{
3902 struct mmc_blk_data *md;
3903 int devidx, ret;
3904
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003905again:
3906 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
3907 return ERR_PTR(-ENOMEM);
3908
3909 spin_lock(&mmc_blk_lock);
3910 ret = ida_get_new(&mmc_blk_ida, &devidx);
3911 spin_unlock(&mmc_blk_lock);
3912
3913 if (ret == -EAGAIN)
3914 goto again;
3915 else if (ret)
3916 return ERR_PTR(ret);
3917
3918 if (devidx >= max_devices) {
3919 ret = -ENOSPC;
3920 goto out;
3921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003923 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00003924 if (!md) {
3925 ret = -ENOMEM;
3926 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 }
Russell Kinga6f6c962006-01-03 22:38:44 +00003928
Johan Rudholmadd710e2011-12-02 08:51:06 +01003929 md->area_type = area_type;
3930
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003931 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00003932 * Set the read-only status based on the supported commands
3933 * and the write protect switch.
3934 */
3935 md->read_only = mmc_blk_readonly(card);
3936
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003937 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00003938 if (md->disk == NULL) {
3939 ret = -ENOMEM;
3940 goto err_kfree;
3941 }
3942
3943 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003944 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00003945 md->usage = 1;
3946
Asutosh Das963469b2015-05-21 13:29:51 +05303947 ret = mmc_init_queue(&md->queue, card, &md->lock, subname, area_type);
Russell Kinga6f6c962006-01-03 22:38:44 +00003948 if (ret)
3949 goto err_putdisk;
3950
Russell Kinga6f6c962006-01-03 22:38:44 +00003951 md->queue.data = md;
3952
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003953 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003954 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00003955 md->disk->fops = &mmc_bdops;
3956 md->disk->private_data = md;
3957 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07003958 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003959 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07003960 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02003961 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02003962 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00003963
3964 /*
3965 * As discussed on lkml, GENHD_FL_REMOVABLE should:
3966 *
3967 * - be set for removable media with permanent block devices
3968 * - be unset for removable block devices with permanent media
3969 *
3970 * Since MMC block devices clearly fall under the second
3971 * case, we do not set GENHD_FL_REMOVABLE. Userspace
3972 * should use the block device creation/destruction hotplug
3973 * messages to tell when the card is present.
3974 */
3975
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003976 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02003977 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00003978
Saugata Dasa5075eb2012-05-17 16:32:21 +05303979 if (mmc_card_mmc(card))
3980 blk_queue_logical_block_size(md->queue.queue,
3981 card->ext_csd.data_sector_size);
3982 else
3983 blk_queue_logical_block_size(md->queue.queue, 512);
3984
Andrei Warkentin371a6892011-04-11 18:10:25 -05003985 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003986
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003987 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02003988 if ((mmc_card_mmc(card) &&
3989 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003990 (mmc_card_sd(card) &&
3991 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
3992 md->flags |= MMC_BLK_CMD23;
3993 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003994
3995 if (mmc_card_mmc(card) &&
3996 md->flags & MMC_BLK_CMD23 &&
3997 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
Asutosh Das5238e022015-04-23 16:00:45 +05303998 card->ext_csd.rel_sectors)) {
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003999 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06004000 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004001 }
4002
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004003 if (card->cmdq_init) {
4004 md->flags |= MMC_BLK_CMD_QUEUE;
4005 md->queue.cmdq_complete_fn = mmc_blk_cmdq_complete_rq;
4006 md->queue.cmdq_issue_fn = mmc_blk_cmdq_issue_rq;
Asutosh Das02e30862015-05-20 16:52:04 +05304007 md->queue.cmdq_error_fn = mmc_blk_cmdq_err;
Asutosh Dasfa8836b2015-03-02 23:14:05 +05304008 md->queue.cmdq_req_timed_out = mmc_blk_cmdq_req_timed_out;
Asutosh Dasa0ba4922015-04-23 16:01:57 +05304009 md->queue.cmdq_shutdown = mmc_blk_cmdq_shutdown;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004010 }
4011
4012 if (mmc_card_mmc(card) && !card->cmdq_init &&
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09004013 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
4014 (md->flags & MMC_BLK_CMD23) &&
4015 card->ext_csd.packed_event_en) {
4016 if (!mmc_packed_init(&md->queue, card))
4017 md->flags |= MMC_BLK_PACKED_CMD;
4018 }
4019
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00004021
4022 err_putdisk:
4023 put_disk(md->disk);
4024 err_kfree:
4025 kfree(md);
4026 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02004027 spin_lock(&mmc_blk_lock);
4028 ida_remove(&mmc_blk_ida, devidx);
4029 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00004030 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031}
4032
Andrei Warkentin371a6892011-04-11 18:10:25 -05004033static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
4034{
4035 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004036
4037 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
4038 /*
4039 * The EXT_CSD sector count is in number or 512 byte
4040 * sectors.
4041 */
4042 size = card->ext_csd.sectors;
4043 } else {
4044 /*
4045 * The CSD capacity field is in units of read_blkbits.
4046 * set_capacity takes units of 512 bytes.
4047 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00004048 size = (typeof(sector_t))card->csd.capacity
4049 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004050 }
4051
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01004052 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004053 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004054}
4055
4056static int mmc_blk_alloc_part(struct mmc_card *card,
4057 struct mmc_blk_data *md,
4058 unsigned int part_type,
4059 sector_t size,
4060 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004061 const char *subname,
4062 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05004063{
4064 char cap_str[10];
4065 struct mmc_blk_data *part_md;
4066
4067 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004068 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004069 if (IS_ERR(part_md))
4070 return PTR_ERR(part_md);
4071 part_md->part_type = part_type;
4072 list_add(&part_md->part, &md->part);
4073
James Bottomleyb9f28d82015-03-05 18:47:01 -08004074 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05004075 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05304076 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05004077 part_md->disk->disk_name, mmc_card_id(card),
4078 mmc_card_name(card), part_md->part_type, cap_str);
4079 return 0;
4080}
4081
Namjae Jeone0c368d2011-10-06 23:41:38 +09004082/* MMC Physical partitions consist of two boot partitions and
4083 * up to four general purpose partitions.
4084 * For each partition enabled in EXT_CSD a block device will be allocatedi
4085 * to provide access to the partition.
4086 */
4087
Andrei Warkentin371a6892011-04-11 18:10:25 -05004088static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
4089{
Namjae Jeone0c368d2011-10-06 23:41:38 +09004090 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004091
4092 if (!mmc_card_mmc(card))
4093 return 0;
4094
Namjae Jeone0c368d2011-10-06 23:41:38 +09004095 for (idx = 0; idx < card->nr_parts; idx++) {
4096 if (card->part[idx].size) {
4097 ret = mmc_blk_alloc_part(card, md,
4098 card->part[idx].part_cfg,
4099 card->part[idx].size >> 9,
4100 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01004101 card->part[idx].name,
4102 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09004103 if (ret)
4104 return ret;
4105 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05004106 }
4107
4108 return ret;
4109}
4110
Andrei Warkentin371a6892011-04-11 18:10:25 -05004111static void mmc_blk_remove_req(struct mmc_blk_data *md)
4112{
Johan Rudholmadd710e2011-12-02 08:51:06 +01004113 struct mmc_card *card;
4114
Andrei Warkentin371a6892011-04-11 18:10:25 -05004115 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07004116 /*
4117 * Flush remaining requests and free queues. It
4118 * is freeing the queue that stops new requests
4119 * from being accepted.
4120 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02004121 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07004122 mmc_cleanup_queue(&md->queue);
4123 if (md->flags & MMC_BLK_PACKED_CMD)
4124 mmc_packed_clean(&md->queue);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07004125 if (md->flags & MMC_BLK_CMD_QUEUE)
4126 mmc_cmdq_clean(&md->queue, card);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004127 device_remove_file(disk_to_dev(md->disk),
4128 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004129 if (md->disk->flags & GENHD_FL_UP) {
4130 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004131 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
4132 card->ext_csd.boot_ro_lockable)
4133 device_remove_file(disk_to_dev(md->disk),
4134 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08004135#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4136 device_remove_file(disk_to_dev(md->disk),
4137 &dev_attr_max_write_speed);
4138 device_remove_file(disk_to_dev(md->disk),
4139 &dev_attr_max_read_speed);
4140 device_remove_file(disk_to_dev(md->disk),
4141 &dev_attr_cache_size);
4142#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05004143
Andrei Warkentin371a6892011-04-11 18:10:25 -05004144 del_gendisk(md->disk);
4145 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05004146 mmc_blk_put(md);
4147 }
4148}
4149
4150static void mmc_blk_remove_parts(struct mmc_card *card,
4151 struct mmc_blk_data *md)
4152{
4153 struct list_head *pos, *q;
4154 struct mmc_blk_data *part_md;
4155
4156 list_for_each_safe(pos, q, &md->part) {
4157 part_md = list_entry(pos, struct mmc_blk_data, part);
4158 list_del(pos);
4159 mmc_blk_remove_req(part_md);
4160 }
4161}
4162
4163static int mmc_add_disk(struct mmc_blk_data *md)
4164{
4165 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01004166 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004167
Dan Williams307d8e62016-06-20 10:40:44 -07004168 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004169 md->force_ro.show = force_ro_show;
4170 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05304171 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004172 md->force_ro.attr.name = "force_ro";
4173 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
4174 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
4175 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01004176 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08004177#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4178 atomic_set(&md->queue.max_write_speed, max_write_speed);
4179 ret = device_create_file(disk_to_dev(md->disk),
4180 &dev_attr_max_write_speed);
4181 if (ret)
4182 goto max_write_speed_fail;
4183 atomic_set(&md->queue.max_read_speed, max_read_speed);
4184 ret = device_create_file(disk_to_dev(md->disk),
4185 &dev_attr_max_read_speed);
4186 if (ret)
4187 goto max_read_speed_fail;
4188 atomic_set(&md->queue.cache_size, cache_size);
4189 atomic_long_set(&md->queue.cache_used, 0);
4190 md->queue.cache_jiffies = jiffies;
4191 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
4192 if (ret)
4193 goto cache_size_fail;
4194#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01004195
4196 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
4197 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04004198 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01004199
4200 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
4201 mode = S_IRUGO;
4202 else
4203 mode = S_IRUGO | S_IWUSR;
4204
4205 md->power_ro_lock.show = power_ro_lock_show;
4206 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01004207 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004208 md->power_ro_lock.attr.mode = mode;
4209 md->power_ro_lock.attr.name =
4210 "ro_lock_until_next_power_on";
4211 ret = device_create_file(disk_to_dev(md->disk),
4212 &md->power_ro_lock);
4213 if (ret)
4214 goto power_ro_lock_fail;
4215 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004216
4217 md->num_wr_reqs_to_start_packing.show =
4218 num_wr_reqs_to_start_packing_show;
4219 md->num_wr_reqs_to_start_packing.store =
4220 num_wr_reqs_to_start_packing_store;
4221 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
4222 md->num_wr_reqs_to_start_packing.attr.name =
4223 "num_wr_reqs_to_start_packing";
4224 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
4225 ret = device_create_file(disk_to_dev(md->disk),
4226 &md->num_wr_reqs_to_start_packing);
4227 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02004228 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02004229
Maya Erez5a8dae12014-12-04 15:13:59 +02004230 md->no_pack_for_random.show = no_pack_for_random_show;
4231 md->no_pack_for_random.store = no_pack_for_random_store;
4232 sysfs_attr_init(&md->no_pack_for_random.attr);
4233 md->no_pack_for_random.attr.name = "no_pack_for_random";
4234 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
4235 ret = device_create_file(disk_to_dev(md->disk),
4236 &md->no_pack_for_random);
4237 if (ret)
4238 goto no_pack_for_random_fails;
4239
Johan Rudholmadd710e2011-12-02 08:51:06 +01004240 return ret;
4241
Maya Erez5a8dae12014-12-04 15:13:59 +02004242no_pack_for_random_fails:
4243 device_remove_file(disk_to_dev(md->disk),
4244 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02004245num_wr_reqs_to_start_packing_fail:
4246 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01004247power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08004248#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
4249 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
4250cache_size_fail:
4251 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
4252max_read_speed_fail:
4253 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
4254max_write_speed_fail:
4255#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01004256 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
4257force_ro_fail:
4258 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004259
4260 return ret;
4261}
4262
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004263static const struct mmc_fixup blk_fixups[] =
4264{
Chris Ballc59d4472011-11-11 22:01:43 -05004265 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
4266 MMC_QUIRK_INAND_CMD38),
4267 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
4268 MMC_QUIRK_INAND_CMD38),
4269 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
4270 MMC_QUIRK_INAND_CMD38),
4271 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
4272 MMC_QUIRK_INAND_CMD38),
4273 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
4274 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004275
4276 /*
4277 * Some MMC cards experience performance degradation with CMD23
4278 * instead of CMD12-bounded multiblock transfers. For now we'll
4279 * black list what's bad...
4280 * - Certain Toshiba cards.
4281 *
4282 * N.B. This doesn't affect SD cards.
4283 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08004284 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
4285 MMC_QUIRK_BLK_NO_CMD23),
4286 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
4287 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004288 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004289 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004290 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004291 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05004292 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05004293 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004294
4295 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03004296 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004297 */
Chris Ballc59d4472011-11-11 22:01:43 -05004298 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004299 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03004300 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
4301 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01004302
Ian Chen3550ccd2012-08-29 15:05:36 +09004303 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08004304 * Some Samsung MMC cards need longer data read timeout than
4305 * indicated in CSD.
4306 */
4307 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
4308 MMC_QUIRK_LONG_READ_TIME),
4309
4310 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09004311 * On these Samsung MoviNAND parts, performing secure erase or
4312 * secure trim can result in unrecoverable corruption due to a
4313 * firmware bug.
4314 */
4315 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4316 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4317 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4318 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4319 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4320 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4321 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4322 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4323 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4324 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4325 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4326 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4327 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4328 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4329 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
4330 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
4331
Shawn Linb5b4ff02015-08-12 13:08:32 +08004332 /*
4333 * On Some Kingston eMMCs, performing trim can result in
4334 * unrecoverable data conrruption occasionally due to a firmware bug.
4335 */
4336 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
4337 MMC_QUIRK_TRIM_BROKEN),
4338 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
4339 MMC_QUIRK_TRIM_BROKEN),
4340
Pratibhasagar V8d664e32014-12-03 18:26:42 +02004341 /* Some INAND MCP devices advertise incorrect timeout values */
4342 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
4343 MMC_QUIRK_INAND_DATA_TIMEOUT),
4344
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004345 END_FIXUP
4346};
4347
Ulf Hansson96541ba2015-04-14 13:06:12 +02004348static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004350 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004351 char cap_str[10];
4352
Pierre Ossman912490d2005-05-21 10:27:02 +01004353 /*
4354 * Check that the card supports the command class(es) we need.
4355 */
4356 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 return -ENODEV;
4358
Lukas Czerner5204d002014-06-18 13:18:07 +02004359 mmc_fixup_device(card, blk_fixups);
4360
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 md = mmc_blk_alloc(card);
4362 if (IS_ERR(md))
4363 return PTR_ERR(md);
4364
James Bottomleyb9f28d82015-03-05 18:47:01 -08004365 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004366 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05304367 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02004369 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
Andrei Warkentin371a6892011-04-11 18:10:25 -05004371 if (mmc_blk_alloc_parts(card, md))
4372 goto out;
4373
Ulf Hansson96541ba2015-04-14 13:06:12 +02004374 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04004375
Andrei Warkentin371a6892011-04-11 18:10:25 -05004376 if (mmc_add_disk(md))
4377 goto out;
4378
4379 list_for_each_entry(part_md, &md->part, part) {
4380 if (mmc_add_disk(part_md))
4381 goto out;
4382 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004383
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004384 pm_runtime_use_autosuspend(&card->dev);
Dov Levenglicka0296392015-06-24 19:51:58 +03004385 pm_runtime_set_autosuspend_delay(&card->dev, MMC_AUTOSUSPEND_DELAY_MS);
4386 /*
4387 * If there is a runtime_idle function, it should take care of
4388 * suspending the card
4389 */
4390 if (card->host->bus_ops->runtime_idle)
4391 pm_runtime_dont_use_autosuspend(&card->dev);
4392 else
4393 pm_runtime_use_autosuspend(&card->dev);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004394
4395 /*
4396 * Don't enable runtime PM for SD-combo cards here. Leave that
4397 * decision to be taken during the SDIO init sequence instead.
4398 */
4399 if (card->type != MMC_TYPE_SD_COMBO) {
4400 pm_runtime_set_active(&card->dev);
4401 pm_runtime_enable(&card->dev);
4402 }
4403
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 return 0;
4405
4406 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05004407 mmc_blk_remove_parts(card, md);
4408 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01004409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410}
4411
Ulf Hansson96541ba2015-04-14 13:06:12 +02004412static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004414 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
Andrei Warkentin371a6892011-04-11 18:10:25 -05004416 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004417 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03004418 mmc_claim_host(card->host);
4419 mmc_blk_part_switch(card, md);
4420 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004421 if (card->type != MMC_TYPE_SD_COMBO)
4422 pm_runtime_disable(&card->dev);
4423 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004424 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02004425 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426}
4427
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004428static int _mmc_blk_suspend(struct mmc_card *card, bool wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004430 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02004431 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304432 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
4434 if (md) {
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004435 rc = mmc_queue_suspend(&md->queue, wait);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304436 if (rc)
4437 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004438 list_for_each_entry(part_md, &md->part, part) {
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004439 rc = mmc_queue_suspend(&part_md->queue, wait);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304440 if (rc)
4441 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304444 goto out;
4445
4446 out_resume:
4447 mmc_queue_resume(&md->queue);
4448 list_for_each_entry(part_md, &md->part, part) {
4449 mmc_queue_resume(&part_md->queue);
4450 }
4451 out:
4452 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453}
4454
Ulf Hansson96541ba2015-04-14 13:06:12 +02004455static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02004456{
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004457 _mmc_blk_suspend(card, 1);
Krishna Kondaa7ea2dc2015-06-29 19:20:05 -07004458
4459 /* send power off notification */
4460 if (mmc_card_mmc(card))
4461 mmc_send_pon(card);
Ulf Hansson76287742013-06-10 17:03:40 +02004462}
4463
Ulf Hansson0967edc2014-10-06 11:29:42 +02004464#ifdef CONFIG_PM_SLEEP
4465static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02004466{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004467 struct mmc_card *card = mmc_dev_to_card(dev);
4468
Konstantin Dorfman497bd4b2015-08-02 16:07:05 +03004469 return _mmc_blk_suspend(card, 0);
Ulf Hansson76287742013-06-10 17:03:40 +02004470}
4471
Ulf Hansson0967edc2014-10-06 11:29:42 +02004472static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004474 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02004475 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
4477 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05004478 /*
4479 * Resume involves the card going into idle state,
4480 * so current partition is always the main one.
4481 */
4482 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004484 list_for_each_entry(part_md, &md->part, part) {
4485 mmc_queue_resume(&part_md->queue);
4486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 }
4488 return 0;
4489}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490#endif
4491
Ulf Hansson0967edc2014-10-06 11:29:42 +02004492static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
4493
Ulf Hansson96541ba2015-04-14 13:06:12 +02004494static struct mmc_driver mmc_driver = {
4495 .drv = {
4496 .name = "mmcblk",
4497 .pm = &mmc_blk_pm_ops,
4498 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 .probe = mmc_blk_probe,
4500 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02004501 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502};
4503
4504static int __init mmc_blk_init(void)
4505{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004506 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004508 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
4509 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
4510
Ben Hutchingsa26eba62014-11-06 03:35:09 +00004511 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004512
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004513 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
4514 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004517 res = mmc_register_driver(&mmc_driver);
4518 if (res)
4519 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004521 return 0;
4522 out2:
4523 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 out:
4525 return res;
4526}
4527
4528static void __exit mmc_blk_exit(void)
4529{
4530 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004531 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532}
4533
4534module_init(mmc_blk_init);
4535module_exit(mmc_blk_exit);
4536
4537MODULE_LICENSE("GPL");
4538MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
4539